hv-24x7.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hypervisor supplied "24x7" performance counter support
  4. *
  5. * Author: Cody P Schafer <cody@linux.vnet.ibm.com>
  6. * Copyright 2014 IBM Corporation.
  7. */
  8. #define pr_fmt(fmt) "hv-24x7: " fmt
  9. #include <linux/perf_event.h>
  10. #include <linux/rbtree.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <asm/cputhreads.h>
  15. #include <asm/firmware.h>
  16. #include <asm/hvcall.h>
  17. #include <asm/io.h>
  18. #include <asm/papr-sysparm.h>
  19. #include <linux/byteorder/generic.h>
  20. #include <asm/rtas.h>
  21. #include "hv-24x7.h"
  22. #include "hv-24x7-catalog.h"
  23. #include "hv-common.h"
  24. /* Version of the 24x7 hypervisor API that we should use in this machine. */
  25. static int interface_version;
  26. /* Whether we have to aggregate result data for some domains. */
  27. static bool aggregate_result_elements;
  28. static cpumask_t hv_24x7_cpumask;
  29. static bool domain_is_valid(unsigned int domain)
  30. {
  31. switch (domain) {
  32. #define DOMAIN(n, v, x, c) \
  33. case HV_PERF_DOMAIN_##n: \
  34. /* fall through */
  35. #include "hv-24x7-domains.h"
  36. #undef DOMAIN
  37. return true;
  38. default:
  39. return false;
  40. }
  41. }
  42. static bool is_physical_domain(unsigned int domain)
  43. {
  44. switch (domain) {
  45. #define DOMAIN(n, v, x, c) \
  46. case HV_PERF_DOMAIN_##n: \
  47. return c;
  48. #include "hv-24x7-domains.h"
  49. #undef DOMAIN
  50. default:
  51. return false;
  52. }
  53. }
  54. /*
  55. * The Processor Module Information system parameter allows transferring
  56. * of certain processor module information from the platform to the OS.
  57. * Refer PAPR+ document to get parameter token value as '43'.
  58. */
  59. static u32 phys_sockets; /* Physical sockets */
  60. static u32 phys_chipspersocket; /* Physical chips per socket*/
  61. static u32 phys_coresperchip; /* Physical cores per chip */
  62. /*
  63. * read_24x7_sys_info()
  64. * Retrieve the number of sockets and chips per socket and cores per
  65. * chip details through the get-system-parameter rtas call.
  66. */
  67. void read_24x7_sys_info(void)
  68. {
  69. struct papr_sysparm_buf *buf;
  70. /*
  71. * Making system parameter: chips and sockets and cores per chip
  72. * default to 1.
  73. */
  74. phys_sockets = 1;
  75. phys_chipspersocket = 1;
  76. phys_coresperchip = 1;
  77. buf = papr_sysparm_buf_alloc();
  78. if (!buf)
  79. return;
  80. if (!papr_sysparm_get(PAPR_SYSPARM_PROC_MODULE_INFO, buf)) {
  81. int ntypes = be16_to_cpup((__be16 *)&buf->val[0]);
  82. int len = be16_to_cpu(buf->len);
  83. if (len >= 8 && ntypes != 0) {
  84. phys_sockets = be16_to_cpup((__be16 *)&buf->val[2]);
  85. phys_chipspersocket = be16_to_cpup((__be16 *)&buf->val[4]);
  86. phys_coresperchip = be16_to_cpup((__be16 *)&buf->val[6]);
  87. }
  88. }
  89. papr_sysparm_buf_free(buf);
  90. }
  91. /* Domains for which more than one result element are returned for each event. */
  92. static bool domain_needs_aggregation(unsigned int domain)
  93. {
  94. return aggregate_result_elements &&
  95. (domain == HV_PERF_DOMAIN_PHYS_CORE ||
  96. (domain >= HV_PERF_DOMAIN_VCPU_HOME_CORE &&
  97. domain <= HV_PERF_DOMAIN_VCPU_REMOTE_NODE));
  98. }
  99. static const char *domain_name(unsigned int domain)
  100. {
  101. if (!domain_is_valid(domain))
  102. return NULL;
  103. switch (domain) {
  104. case HV_PERF_DOMAIN_PHYS_CHIP: return "Physical Chip";
  105. case HV_PERF_DOMAIN_PHYS_CORE: return "Physical Core";
  106. case HV_PERF_DOMAIN_VCPU_HOME_CORE: return "VCPU Home Core";
  107. case HV_PERF_DOMAIN_VCPU_HOME_CHIP: return "VCPU Home Chip";
  108. case HV_PERF_DOMAIN_VCPU_HOME_NODE: return "VCPU Home Node";
  109. case HV_PERF_DOMAIN_VCPU_REMOTE_NODE: return "VCPU Remote Node";
  110. }
  111. WARN_ON_ONCE(domain);
  112. return NULL;
  113. }
  114. static bool catalog_entry_domain_is_valid(unsigned int domain)
  115. {
  116. /* POWER8 doesn't support virtual domains. */
  117. if (interface_version == 1)
  118. return is_physical_domain(domain);
  119. else
  120. return domain_is_valid(domain);
  121. }
  122. /*
  123. * TODO: Merging events:
  124. * - Think of the hcall as an interface to a 4d array of counters:
  125. * - x = domains
  126. * - y = indexes in the domain (core, chip, vcpu, node, etc)
  127. * - z = offset into the counter space
  128. * - w = lpars (guest vms, "logical partitions")
  129. * - A single request is: x,y,y_last,z,z_last,w,w_last
  130. * - this means we can retrieve a rectangle of counters in y,z for a single x.
  131. *
  132. * - Things to consider (ignoring w):
  133. * - input cost_per_request = 16
  134. * - output cost_per_result(ys,zs) = 8 + 8 * ys + ys * zs
  135. * - limited number of requests per hcall (must fit into 4K bytes)
  136. * - 4k = 16 [buffer header] - 16 [request size] * request_count
  137. * - 255 requests per hcall
  138. * - sometimes it will be more efficient to read extra data and discard
  139. */
  140. /*
  141. * Example usage:
  142. * perf stat -e 'hv_24x7/domain=2,offset=8,vcpu=0,lpar=0xffffffff/'
  143. */
  144. /* u3 0-6, one of HV_24X7_PERF_DOMAIN */
  145. EVENT_DEFINE_RANGE_FORMAT(domain, config, 0, 3);
  146. /* u16 */
  147. EVENT_DEFINE_RANGE_FORMAT(core, config, 16, 31);
  148. EVENT_DEFINE_RANGE_FORMAT(chip, config, 16, 31);
  149. EVENT_DEFINE_RANGE_FORMAT(vcpu, config, 16, 31);
  150. /* u32, see "data_offset" */
  151. EVENT_DEFINE_RANGE_FORMAT(offset, config, 32, 63);
  152. /* u16 */
  153. EVENT_DEFINE_RANGE_FORMAT(lpar, config1, 0, 15);
  154. EVENT_DEFINE_RANGE(reserved1, config, 4, 15);
  155. EVENT_DEFINE_RANGE(reserved2, config1, 16, 63);
  156. EVENT_DEFINE_RANGE(reserved3, config2, 0, 63);
  157. static struct attribute *format_attrs[] = {
  158. &format_attr_domain.attr,
  159. &format_attr_offset.attr,
  160. &format_attr_core.attr,
  161. &format_attr_chip.attr,
  162. &format_attr_vcpu.attr,
  163. &format_attr_lpar.attr,
  164. NULL,
  165. };
  166. static const struct attribute_group format_group = {
  167. .name = "format",
  168. .attrs = format_attrs,
  169. };
  170. static struct attribute_group event_group = {
  171. .name = "events",
  172. /* .attrs is set in init */
  173. };
  174. static struct attribute_group event_desc_group = {
  175. .name = "event_descs",
  176. /* .attrs is set in init */
  177. };
  178. static struct attribute_group event_long_desc_group = {
  179. .name = "event_long_descs",
  180. /* .attrs is set in init */
  181. };
  182. static struct kmem_cache *hv_page_cache;
  183. static DEFINE_PER_CPU(int, hv_24x7_txn_flags);
  184. static DEFINE_PER_CPU(int, hv_24x7_txn_err);
  185. struct hv_24x7_hw {
  186. struct perf_event *events[255];
  187. };
  188. static DEFINE_PER_CPU(struct hv_24x7_hw, hv_24x7_hw);
  189. /*
  190. * request_buffer and result_buffer are not required to be 4k aligned,
  191. * but are not allowed to cross any 4k boundary. Aligning them to 4k is
  192. * the simplest way to ensure that.
  193. */
  194. #define H24x7_DATA_BUFFER_SIZE 4096
  195. static DEFINE_PER_CPU(char, hv_24x7_reqb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096);
  196. static DEFINE_PER_CPU(char, hv_24x7_resb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096);
  197. static unsigned int max_num_requests(int interface_version)
  198. {
  199. return (H24x7_DATA_BUFFER_SIZE - sizeof(struct hv_24x7_request_buffer))
  200. / H24x7_REQUEST_SIZE(interface_version);
  201. }
  202. static char *event_name(struct hv_24x7_event_data *ev, int *len)
  203. {
  204. *len = be16_to_cpu(ev->event_name_len) - 2;
  205. return (char *)ev->remainder;
  206. }
  207. static char *event_desc(struct hv_24x7_event_data *ev, int *len)
  208. {
  209. unsigned int nl = be16_to_cpu(ev->event_name_len);
  210. __be16 *desc_len = (__be16 *)(ev->remainder + nl - 2);
  211. *len = be16_to_cpu(*desc_len) - 2;
  212. return (char *)ev->remainder + nl;
  213. }
  214. static char *event_long_desc(struct hv_24x7_event_data *ev, int *len)
  215. {
  216. unsigned int nl = be16_to_cpu(ev->event_name_len);
  217. __be16 *desc_len_ = (__be16 *)(ev->remainder + nl - 2);
  218. unsigned int desc_len = be16_to_cpu(*desc_len_);
  219. __be16 *long_desc_len = (__be16 *)(ev->remainder + nl + desc_len - 2);
  220. *len = be16_to_cpu(*long_desc_len) - 2;
  221. return (char *)ev->remainder + nl + desc_len;
  222. }
  223. static bool event_fixed_portion_is_within(struct hv_24x7_event_data *ev,
  224. void *end)
  225. {
  226. void *start = ev;
  227. return (start + offsetof(struct hv_24x7_event_data, remainder)) < end;
  228. }
  229. /*
  230. * Things we don't check:
  231. * - padding for desc, name, and long/detailed desc is required to be '\0'
  232. * bytes.
  233. *
  234. * Return NULL if we pass end,
  235. * Otherwise return the address of the byte just following the event.
  236. */
  237. static void *event_end(struct hv_24x7_event_data *ev, void *end)
  238. {
  239. void *start = ev;
  240. __be16 *dl_, *ldl_;
  241. unsigned int dl, ldl;
  242. unsigned int nl = be16_to_cpu(ev->event_name_len);
  243. if (nl < 2) {
  244. pr_debug("%s: name length too short: %d", __func__, nl);
  245. return NULL;
  246. }
  247. if (start + nl > end) {
  248. pr_debug("%s: start=%p + nl=%u > end=%p",
  249. __func__, start, nl, end);
  250. return NULL;
  251. }
  252. dl_ = (__be16 *)(ev->remainder + nl - 2);
  253. if (!IS_ALIGNED((uintptr_t)dl_, 2))
  254. pr_warn("desc len not aligned %p", dl_);
  255. dl = be16_to_cpu(*dl_);
  256. if (dl < 2) {
  257. pr_debug("%s: desc len too short: %d", __func__, dl);
  258. return NULL;
  259. }
  260. if (start + nl + dl > end) {
  261. pr_debug("%s: (start=%p + nl=%u + dl=%u)=%p > end=%p",
  262. __func__, start, nl, dl, start + nl + dl, end);
  263. return NULL;
  264. }
  265. ldl_ = (__be16 *)(ev->remainder + nl + dl - 2);
  266. if (!IS_ALIGNED((uintptr_t)ldl_, 2))
  267. pr_warn("long desc len not aligned %p", ldl_);
  268. ldl = be16_to_cpu(*ldl_);
  269. if (ldl < 2) {
  270. pr_debug("%s: long desc len too short (ldl=%u)",
  271. __func__, ldl);
  272. return NULL;
  273. }
  274. if (start + nl + dl + ldl > end) {
  275. pr_debug("%s: start=%p + nl=%u + dl=%u + ldl=%u > end=%p",
  276. __func__, start, nl, dl, ldl, end);
  277. return NULL;
  278. }
  279. return start + nl + dl + ldl;
  280. }
  281. static long h_get_24x7_catalog_page_(unsigned long phys_4096,
  282. unsigned long version, unsigned long index)
  283. {
  284. pr_devel("h_get_24x7_catalog_page(0x%lx, %lu, %lu)",
  285. phys_4096, version, index);
  286. WARN_ON(!IS_ALIGNED(phys_4096, 4096));
  287. return plpar_hcall_norets(H_GET_24X7_CATALOG_PAGE,
  288. phys_4096, version, index);
  289. }
  290. static long h_get_24x7_catalog_page(char page[], u64 version, u32 index)
  291. {
  292. return h_get_24x7_catalog_page_(virt_to_phys(page),
  293. version, index);
  294. }
  295. /*
  296. * Each event we find in the catalog, will have a sysfs entry. Format the
  297. * data for this sysfs entry based on the event's domain.
  298. *
  299. * Events belonging to the Chip domain can only be monitored in that domain.
  300. * i.e the domain for these events is a fixed/knwon value.
  301. *
  302. * Events belonging to the Core domain can be monitored either in the physical
  303. * core or in one of the virtual CPU domains. So the domain value for these
  304. * events must be specified by the user (i.e is a required parameter). Format
  305. * the Core events with 'domain=?' so the perf-tool can error check required
  306. * parameters.
  307. *
  308. * NOTE: For the Core domain events, rather than making domain a required
  309. * parameter we could default it to PHYS_CORE and allowe users to
  310. * override the domain to one of the VCPU domains.
  311. *
  312. * However, this can make the interface a little inconsistent.
  313. *
  314. * If we set domain=2 (PHYS_CHIP) and allow user to override this field
  315. * the user may be tempted to also modify the "offset=x" field in which
  316. * can lead to confusing usage. Consider the HPM_PCYC (offset=0x18) and
  317. * HPM_INST (offset=0x20) events. With:
  318. *
  319. * perf stat -e hv_24x7/HPM_PCYC,offset=0x20/
  320. *
  321. * we end up monitoring HPM_INST, while the command line has HPM_PCYC.
  322. *
  323. * By not assigning a default value to the domain for the Core events,
  324. * we can have simple guidelines:
  325. *
  326. * - Specifying values for parameters with "=?" is required.
  327. *
  328. * - Specifying (i.e overriding) values for other parameters
  329. * is undefined.
  330. */
  331. static char *event_fmt(struct hv_24x7_event_data *event, unsigned int domain)
  332. {
  333. const char *sindex;
  334. const char *lpar;
  335. const char *domain_str;
  336. char buf[8];
  337. switch (domain) {
  338. case HV_PERF_DOMAIN_PHYS_CHIP:
  339. snprintf(buf, sizeof(buf), "%d", domain);
  340. domain_str = buf;
  341. lpar = "0x0";
  342. sindex = "chip";
  343. break;
  344. case HV_PERF_DOMAIN_PHYS_CORE:
  345. domain_str = "?";
  346. lpar = "0x0";
  347. sindex = "core";
  348. break;
  349. default:
  350. domain_str = "?";
  351. lpar = "?";
  352. sindex = "vcpu";
  353. }
  354. return kasprintf(GFP_KERNEL,
  355. "domain=%s,offset=0x%x,%s=?,lpar=%s",
  356. domain_str,
  357. be16_to_cpu(event->event_counter_offs) +
  358. be16_to_cpu(event->event_group_record_offs),
  359. sindex,
  360. lpar);
  361. }
  362. /* Avoid trusting fw to NUL terminate strings */
  363. static char *memdup_to_str(char *maybe_str, int max_len, gfp_t gfp)
  364. {
  365. return kasprintf(gfp, "%.*s", max_len, maybe_str);
  366. }
  367. static ssize_t cpumask_show(struct device *dev,
  368. struct device_attribute *attr, char *buf)
  369. {
  370. return cpumap_print_to_pagebuf(true, buf, &hv_24x7_cpumask);
  371. }
  372. static ssize_t sockets_show(struct device *dev,
  373. struct device_attribute *attr, char *buf)
  374. {
  375. return sprintf(buf, "%d\n", phys_sockets);
  376. }
  377. static ssize_t chipspersocket_show(struct device *dev,
  378. struct device_attribute *attr, char *buf)
  379. {
  380. return sprintf(buf, "%d\n", phys_chipspersocket);
  381. }
  382. static ssize_t coresperchip_show(struct device *dev,
  383. struct device_attribute *attr, char *buf)
  384. {
  385. return sprintf(buf, "%d\n", phys_coresperchip);
  386. }
  387. static struct attribute *device_str_attr_create_(char *name, char *str)
  388. {
  389. struct dev_ext_attribute *attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  390. if (!attr)
  391. return NULL;
  392. sysfs_attr_init(&attr->attr.attr);
  393. attr->var = str;
  394. attr->attr.attr.name = name;
  395. attr->attr.attr.mode = 0444;
  396. attr->attr.show = device_show_string;
  397. return &attr->attr.attr;
  398. }
  399. /*
  400. * Allocate and initialize strings representing event attributes.
  401. *
  402. * NOTE: The strings allocated here are never destroyed and continue to
  403. * exist till shutdown. This is to allow us to create as many events
  404. * from the catalog as possible, even if we encounter errors with some.
  405. * In case of changes to error paths in future, these may need to be
  406. * freed by the caller.
  407. */
  408. static struct attribute *device_str_attr_create(char *name, int name_max,
  409. int name_nonce,
  410. char *str, size_t str_max)
  411. {
  412. char *n;
  413. char *s = memdup_to_str(str, str_max, GFP_KERNEL);
  414. struct attribute *a;
  415. if (!s)
  416. return NULL;
  417. if (!name_nonce)
  418. n = kasprintf(GFP_KERNEL, "%.*s", name_max, name);
  419. else
  420. n = kasprintf(GFP_KERNEL, "%.*s__%d", name_max, name,
  421. name_nonce);
  422. if (!n)
  423. goto out_s;
  424. a = device_str_attr_create_(n, s);
  425. if (!a)
  426. goto out_n;
  427. return a;
  428. out_n:
  429. kfree(n);
  430. out_s:
  431. kfree(s);
  432. return NULL;
  433. }
  434. static struct attribute *event_to_attr(unsigned int ix,
  435. struct hv_24x7_event_data *event,
  436. unsigned int domain,
  437. int nonce)
  438. {
  439. int event_name_len;
  440. char *ev_name, *a_ev_name, *val;
  441. struct attribute *attr;
  442. if (!domain_is_valid(domain)) {
  443. pr_warn("catalog event %u has invalid domain %u\n",
  444. ix, domain);
  445. return NULL;
  446. }
  447. val = event_fmt(event, domain);
  448. if (!val)
  449. return NULL;
  450. ev_name = event_name(event, &event_name_len);
  451. if (!nonce)
  452. a_ev_name = kasprintf(GFP_KERNEL, "%.*s",
  453. (int)event_name_len, ev_name);
  454. else
  455. a_ev_name = kasprintf(GFP_KERNEL, "%.*s__%d",
  456. (int)event_name_len, ev_name, nonce);
  457. if (!a_ev_name)
  458. goto out_val;
  459. attr = device_str_attr_create_(a_ev_name, val);
  460. if (!attr)
  461. goto out_name;
  462. return attr;
  463. out_name:
  464. kfree(a_ev_name);
  465. out_val:
  466. kfree(val);
  467. return NULL;
  468. }
  469. static struct attribute *event_to_desc_attr(struct hv_24x7_event_data *event,
  470. int nonce)
  471. {
  472. int nl, dl;
  473. char *name = event_name(event, &nl);
  474. char *desc = event_desc(event, &dl);
  475. /* If there isn't a description, don't create the sysfs file */
  476. if (!dl)
  477. return NULL;
  478. return device_str_attr_create(name, nl, nonce, desc, dl);
  479. }
  480. static struct attribute *
  481. event_to_long_desc_attr(struct hv_24x7_event_data *event, int nonce)
  482. {
  483. int nl, dl;
  484. char *name = event_name(event, &nl);
  485. char *desc = event_long_desc(event, &dl);
  486. /* If there isn't a description, don't create the sysfs file */
  487. if (!dl)
  488. return NULL;
  489. return device_str_attr_create(name, nl, nonce, desc, dl);
  490. }
  491. static int event_data_to_attrs(unsigned int ix, struct attribute **attrs,
  492. struct hv_24x7_event_data *event, int nonce)
  493. {
  494. *attrs = event_to_attr(ix, event, event->domain, nonce);
  495. if (!*attrs)
  496. return -1;
  497. return 0;
  498. }
  499. /* */
  500. struct event_uniq {
  501. struct rb_node node;
  502. const char *name;
  503. int nl;
  504. unsigned int ct;
  505. unsigned int domain;
  506. };
  507. static int memord(const void *d1, size_t s1, const void *d2, size_t s2)
  508. {
  509. if (s1 < s2)
  510. return 1;
  511. if (s1 > s2)
  512. return -1;
  513. return memcmp(d1, d2, s1);
  514. }
  515. static int ev_uniq_ord(const void *v1, size_t s1, unsigned int d1,
  516. const void *v2, size_t s2, unsigned int d2)
  517. {
  518. int r = memord(v1, s1, v2, s2);
  519. if (r)
  520. return r;
  521. if (d1 > d2)
  522. return 1;
  523. if (d2 > d1)
  524. return -1;
  525. return 0;
  526. }
  527. static int event_uniq_add(struct rb_root *root, const char *name, int nl,
  528. unsigned int domain)
  529. {
  530. struct rb_node **new = &(root->rb_node), *parent = NULL;
  531. struct event_uniq *data;
  532. /* Figure out where to put new node */
  533. while (*new) {
  534. struct event_uniq *it;
  535. int result;
  536. it = rb_entry(*new, struct event_uniq, node);
  537. result = ev_uniq_ord(name, nl, domain, it->name, it->nl,
  538. it->domain);
  539. parent = *new;
  540. if (result < 0)
  541. new = &((*new)->rb_left);
  542. else if (result > 0)
  543. new = &((*new)->rb_right);
  544. else {
  545. it->ct++;
  546. pr_info("found a duplicate event %.*s, ct=%u\n", nl,
  547. name, it->ct);
  548. return it->ct;
  549. }
  550. }
  551. data = kmalloc(sizeof(*data), GFP_KERNEL);
  552. if (!data)
  553. return -ENOMEM;
  554. *data = (struct event_uniq) {
  555. .name = name,
  556. .nl = nl,
  557. .ct = 0,
  558. .domain = domain,
  559. };
  560. /* Add new node and rebalance tree. */
  561. rb_link_node(&data->node, parent, new);
  562. rb_insert_color(&data->node, root);
  563. /* data->ct */
  564. return 0;
  565. }
  566. static void event_uniq_destroy(struct rb_root *root)
  567. {
  568. /*
  569. * the strings we point to are in the giant block of memory filled by
  570. * the catalog, and are freed separately.
  571. */
  572. struct event_uniq *pos, *n;
  573. rbtree_postorder_for_each_entry_safe(pos, n, root, node)
  574. kfree(pos);
  575. }
  576. /*
  577. * ensure the event structure's sizes are self consistent and don't cause us to
  578. * read outside of the event
  579. *
  580. * On success, return the event length in bytes.
  581. * Otherwise, return -1 (and print as appropriate).
  582. */
  583. static ssize_t catalog_event_len_validate(struct hv_24x7_event_data *event,
  584. size_t event_idx,
  585. size_t event_data_bytes,
  586. size_t event_entry_count,
  587. size_t offset, void *end)
  588. {
  589. ssize_t ev_len;
  590. void *ev_end, *calc_ev_end;
  591. if (offset >= event_data_bytes)
  592. return -1;
  593. if (event_idx >= event_entry_count) {
  594. pr_devel("catalog event data has %zu bytes of padding after last event\n",
  595. event_data_bytes - offset);
  596. return -1;
  597. }
  598. if (!event_fixed_portion_is_within(event, end)) {
  599. pr_warn("event %zu fixed portion is not within range\n",
  600. event_idx);
  601. return -1;
  602. }
  603. ev_len = be16_to_cpu(event->length);
  604. if (ev_len % 16)
  605. pr_info("event %zu has length %zu not divisible by 16: event=%pK\n",
  606. event_idx, ev_len, event);
  607. ev_end = (__u8 *)event + ev_len;
  608. if (ev_end > end) {
  609. pr_warn("event %zu has .length=%zu, ends after buffer end: ev_end=%pK > end=%pK, offset=%zu\n",
  610. event_idx, ev_len, ev_end, end,
  611. offset);
  612. return -1;
  613. }
  614. calc_ev_end = event_end(event, end);
  615. if (!calc_ev_end) {
  616. pr_warn("event %zu has a calculated length which exceeds buffer length %zu: event=%pK end=%pK, offset=%zu\n",
  617. event_idx, event_data_bytes, event, end,
  618. offset);
  619. return -1;
  620. }
  621. if (calc_ev_end > ev_end) {
  622. pr_warn("event %zu exceeds its own length: event=%pK, end=%pK, offset=%zu, calc_ev_end=%pK\n",
  623. event_idx, event, ev_end, offset, calc_ev_end);
  624. return -1;
  625. }
  626. return ev_len;
  627. }
  628. /*
  629. * Return true incase of invalid or dummy events with names like RESERVED*
  630. */
  631. static bool ignore_event(const char *name)
  632. {
  633. return strncmp(name, "RESERVED", 8) == 0;
  634. }
  635. #define MAX_4K (SIZE_MAX / 4096)
  636. static int create_events_from_catalog(struct attribute ***events_,
  637. struct attribute ***event_descs_,
  638. struct attribute ***event_long_descs_)
  639. {
  640. long hret;
  641. size_t catalog_len, catalog_page_len, event_entry_count,
  642. event_data_len, event_data_offs,
  643. event_data_bytes, junk_events, event_idx, event_attr_ct, i,
  644. attr_max, event_idx_last, desc_ct, long_desc_ct;
  645. ssize_t ct, ev_len;
  646. uint64_t catalog_version_num;
  647. struct attribute **events, **event_descs, **event_long_descs;
  648. struct hv_24x7_catalog_page_0 *page_0 =
  649. kmem_cache_alloc(hv_page_cache, GFP_KERNEL);
  650. void *page = page_0;
  651. void *event_data, *end;
  652. struct hv_24x7_event_data *event;
  653. struct rb_root ev_uniq = RB_ROOT;
  654. int ret = 0;
  655. if (!page) {
  656. ret = -ENOMEM;
  657. goto e_out;
  658. }
  659. hret = h_get_24x7_catalog_page(page, 0, 0);
  660. if (hret) {
  661. ret = -EIO;
  662. goto e_free;
  663. }
  664. catalog_version_num = be64_to_cpu(page_0->version);
  665. catalog_page_len = be32_to_cpu(page_0->length);
  666. if (MAX_4K < catalog_page_len) {
  667. pr_err("invalid page count: %zu\n", catalog_page_len);
  668. ret = -EIO;
  669. goto e_free;
  670. }
  671. catalog_len = catalog_page_len * 4096;
  672. event_entry_count = be16_to_cpu(page_0->event_entry_count);
  673. event_data_offs = be16_to_cpu(page_0->event_data_offs);
  674. event_data_len = be16_to_cpu(page_0->event_data_len);
  675. pr_devel("cv %llu cl %zu eec %zu edo %zu edl %zu\n",
  676. catalog_version_num, catalog_len,
  677. event_entry_count, event_data_offs, event_data_len);
  678. if ((MAX_4K < event_data_len)
  679. || (MAX_4K < event_data_offs)
  680. || (MAX_4K - event_data_offs < event_data_len)) {
  681. pr_err("invalid event data offs %zu and/or len %zu\n",
  682. event_data_offs, event_data_len);
  683. ret = -EIO;
  684. goto e_free;
  685. }
  686. if ((event_data_offs + event_data_len) > catalog_page_len) {
  687. pr_err("event data %zu-%zu does not fit inside catalog 0-%zu\n",
  688. event_data_offs,
  689. event_data_offs + event_data_len,
  690. catalog_page_len);
  691. ret = -EIO;
  692. goto e_free;
  693. }
  694. if (SIZE_MAX - 1 < event_entry_count) {
  695. pr_err("event_entry_count %zu is invalid\n", event_entry_count);
  696. ret = -EIO;
  697. goto e_free;
  698. }
  699. event_data_bytes = event_data_len * 4096;
  700. /*
  701. * event data can span several pages, events can cross between these
  702. * pages. Use vmalloc to make this easier.
  703. */
  704. event_data = vmalloc(event_data_bytes);
  705. if (!event_data) {
  706. pr_err("could not allocate event data\n");
  707. ret = -ENOMEM;
  708. goto e_free;
  709. }
  710. end = event_data + event_data_bytes;
  711. /*
  712. * using vmalloc_to_phys() like this only works if PAGE_SIZE is
  713. * divisible by 4096
  714. */
  715. BUILD_BUG_ON(PAGE_SIZE % 4096);
  716. for (i = 0; i < event_data_len; i++) {
  717. hret = h_get_24x7_catalog_page_(
  718. vmalloc_to_phys(event_data + i * 4096),
  719. catalog_version_num,
  720. i + event_data_offs);
  721. if (hret) {
  722. pr_err("Failed to get event data in page %zu: rc=%ld\n",
  723. i + event_data_offs, hret);
  724. ret = -EIO;
  725. goto e_event_data;
  726. }
  727. }
  728. /*
  729. * scan the catalog to determine the number of attributes we need, and
  730. * verify it at the same time.
  731. */
  732. for (junk_events = 0, event = event_data, event_idx = 0, attr_max = 0;
  733. ;
  734. event_idx++, event = (void *)event + ev_len) {
  735. size_t offset = (void *)event - (void *)event_data;
  736. char *name;
  737. int nl;
  738. ev_len = catalog_event_len_validate(event, event_idx,
  739. event_data_bytes,
  740. event_entry_count,
  741. offset, end);
  742. if (ev_len < 0)
  743. break;
  744. name = event_name(event, &nl);
  745. if (ignore_event(name)) {
  746. junk_events++;
  747. continue;
  748. }
  749. if (event->event_group_record_len == 0) {
  750. pr_devel("invalid event %zu (%.*s): group_record_len == 0, skipping\n",
  751. event_idx, nl, name);
  752. junk_events++;
  753. continue;
  754. }
  755. if (!catalog_entry_domain_is_valid(event->domain)) {
  756. pr_info("event %zu (%.*s) has invalid domain %d\n",
  757. event_idx, nl, name, event->domain);
  758. junk_events++;
  759. continue;
  760. }
  761. attr_max++;
  762. }
  763. event_idx_last = event_idx;
  764. if (event_idx_last != event_entry_count)
  765. pr_warn("event buffer ended before listed # of events were parsed (got %zu, wanted %zu, junk %zu)\n",
  766. event_idx_last, event_entry_count, junk_events);
  767. events = kmalloc_array(attr_max + 1, sizeof(*events), GFP_KERNEL);
  768. if (!events) {
  769. ret = -ENOMEM;
  770. goto e_event_data;
  771. }
  772. event_descs = kmalloc_array(event_idx + 1, sizeof(*event_descs),
  773. GFP_KERNEL);
  774. if (!event_descs) {
  775. ret = -ENOMEM;
  776. goto e_event_attrs;
  777. }
  778. event_long_descs = kmalloc_array(event_idx + 1,
  779. sizeof(*event_long_descs), GFP_KERNEL);
  780. if (!event_long_descs) {
  781. ret = -ENOMEM;
  782. goto e_event_descs;
  783. }
  784. /* Iterate over the catalog filling in the attribute vector */
  785. for (junk_events = 0, event_attr_ct = 0, desc_ct = 0, long_desc_ct = 0,
  786. event = event_data, event_idx = 0;
  787. event_idx < event_idx_last;
  788. event_idx++, ev_len = be16_to_cpu(event->length),
  789. event = (void *)event + ev_len) {
  790. char *name;
  791. int nl;
  792. int nonce;
  793. /*
  794. * these are the only "bad" events that are intermixed and that
  795. * we can ignore without issue. make sure to skip them here
  796. */
  797. if (event->event_group_record_len == 0)
  798. continue;
  799. if (!catalog_entry_domain_is_valid(event->domain))
  800. continue;
  801. name = event_name(event, &nl);
  802. if (ignore_event(name))
  803. continue;
  804. nonce = event_uniq_add(&ev_uniq, name, nl, event->domain);
  805. ct = event_data_to_attrs(event_idx, events + event_attr_ct,
  806. event, nonce);
  807. if (ct < 0) {
  808. pr_warn("event %zu (%.*s) creation failure, skipping\n",
  809. event_idx, nl, name);
  810. junk_events++;
  811. } else {
  812. event_attr_ct++;
  813. event_descs[desc_ct] = event_to_desc_attr(event, nonce);
  814. if (event_descs[desc_ct])
  815. desc_ct++;
  816. event_long_descs[long_desc_ct] =
  817. event_to_long_desc_attr(event, nonce);
  818. if (event_long_descs[long_desc_ct])
  819. long_desc_ct++;
  820. }
  821. }
  822. pr_info("read %zu catalog entries, created %zu event attrs (%zu failures), %zu descs\n",
  823. event_idx, event_attr_ct, junk_events, desc_ct);
  824. events[event_attr_ct] = NULL;
  825. event_descs[desc_ct] = NULL;
  826. event_long_descs[long_desc_ct] = NULL;
  827. event_uniq_destroy(&ev_uniq);
  828. vfree(event_data);
  829. kmem_cache_free(hv_page_cache, page);
  830. *events_ = events;
  831. *event_descs_ = event_descs;
  832. *event_long_descs_ = event_long_descs;
  833. return 0;
  834. e_event_descs:
  835. kfree(event_descs);
  836. e_event_attrs:
  837. kfree(events);
  838. e_event_data:
  839. vfree(event_data);
  840. e_free:
  841. kmem_cache_free(hv_page_cache, page);
  842. e_out:
  843. *events_ = NULL;
  844. *event_descs_ = NULL;
  845. *event_long_descs_ = NULL;
  846. return ret;
  847. }
  848. static ssize_t catalog_read(struct file *filp, struct kobject *kobj,
  849. struct bin_attribute *bin_attr, char *buf,
  850. loff_t offset, size_t count)
  851. {
  852. long hret;
  853. ssize_t ret = 0;
  854. size_t catalog_len = 0, catalog_page_len = 0;
  855. loff_t page_offset = 0;
  856. loff_t offset_in_page;
  857. size_t copy_len;
  858. uint64_t catalog_version_num = 0;
  859. void *page = kmem_cache_alloc(hv_page_cache, GFP_USER);
  860. struct hv_24x7_catalog_page_0 *page_0 = page;
  861. if (!page)
  862. return -ENOMEM;
  863. hret = h_get_24x7_catalog_page(page, 0, 0);
  864. if (hret) {
  865. ret = -EIO;
  866. goto e_free;
  867. }
  868. catalog_version_num = be64_to_cpu(page_0->version);
  869. catalog_page_len = be32_to_cpu(page_0->length);
  870. catalog_len = catalog_page_len * 4096;
  871. page_offset = offset / 4096;
  872. offset_in_page = offset % 4096;
  873. if (page_offset >= catalog_page_len)
  874. goto e_free;
  875. if (page_offset != 0) {
  876. hret = h_get_24x7_catalog_page(page, catalog_version_num,
  877. page_offset);
  878. if (hret) {
  879. ret = -EIO;
  880. goto e_free;
  881. }
  882. }
  883. copy_len = 4096 - offset_in_page;
  884. if (copy_len > count)
  885. copy_len = count;
  886. memcpy(buf, page+offset_in_page, copy_len);
  887. ret = copy_len;
  888. e_free:
  889. if (hret)
  890. pr_err("h_get_24x7_catalog_page(ver=%lld, page=%lld) failed:"
  891. " rc=%ld\n",
  892. catalog_version_num, page_offset, hret);
  893. kmem_cache_free(hv_page_cache, page);
  894. pr_devel("catalog_read: offset=%lld(%lld) count=%zu "
  895. "catalog_len=%zu(%zu) => %zd\n", offset, page_offset,
  896. count, catalog_len, catalog_page_len, ret);
  897. return ret;
  898. }
  899. static ssize_t domains_show(struct device *dev, struct device_attribute *attr,
  900. char *page)
  901. {
  902. int d, n, count = 0;
  903. const char *str;
  904. for (d = 0; d < HV_PERF_DOMAIN_MAX; d++) {
  905. str = domain_name(d);
  906. if (!str)
  907. continue;
  908. n = sprintf(page, "%d: %s\n", d, str);
  909. if (n < 0)
  910. break;
  911. count += n;
  912. page += n;
  913. }
  914. return count;
  915. }
  916. #define PAGE_0_ATTR(_name, _fmt, _expr) \
  917. static ssize_t _name##_show(struct device *dev, \
  918. struct device_attribute *dev_attr, \
  919. char *buf) \
  920. { \
  921. long hret; \
  922. ssize_t ret = 0; \
  923. void *page = kmem_cache_alloc(hv_page_cache, GFP_USER); \
  924. struct hv_24x7_catalog_page_0 *page_0 = page; \
  925. if (!page) \
  926. return -ENOMEM; \
  927. hret = h_get_24x7_catalog_page(page, 0, 0); \
  928. if (hret) { \
  929. ret = -EIO; \
  930. goto e_free; \
  931. } \
  932. ret = sprintf(buf, _fmt, _expr); \
  933. e_free: \
  934. kmem_cache_free(hv_page_cache, page); \
  935. return ret; \
  936. } \
  937. static DEVICE_ATTR_RO(_name)
  938. PAGE_0_ATTR(catalog_version, "%lld\n",
  939. (unsigned long long)be64_to_cpu(page_0->version));
  940. PAGE_0_ATTR(catalog_len, "%lld\n",
  941. (unsigned long long)be32_to_cpu(page_0->length) * 4096);
  942. static BIN_ATTR_RO(catalog, 0/* real length varies */);
  943. static DEVICE_ATTR_RO(domains);
  944. static DEVICE_ATTR_RO(sockets);
  945. static DEVICE_ATTR_RO(chipspersocket);
  946. static DEVICE_ATTR_RO(coresperchip);
  947. static DEVICE_ATTR_RO(cpumask);
  948. static struct bin_attribute *if_bin_attrs[] = {
  949. &bin_attr_catalog,
  950. NULL,
  951. };
  952. static struct attribute *cpumask_attrs[] = {
  953. &dev_attr_cpumask.attr,
  954. NULL,
  955. };
  956. static const struct attribute_group cpumask_attr_group = {
  957. .attrs = cpumask_attrs,
  958. };
  959. static struct attribute *if_attrs[] = {
  960. &dev_attr_catalog_len.attr,
  961. &dev_attr_catalog_version.attr,
  962. &dev_attr_domains.attr,
  963. &dev_attr_sockets.attr,
  964. &dev_attr_chipspersocket.attr,
  965. &dev_attr_coresperchip.attr,
  966. NULL,
  967. };
  968. static const struct attribute_group if_group = {
  969. .name = "interface",
  970. .bin_attrs = if_bin_attrs,
  971. .attrs = if_attrs,
  972. };
  973. static const struct attribute_group *attr_groups[] = {
  974. &format_group,
  975. &event_group,
  976. &event_desc_group,
  977. &event_long_desc_group,
  978. &if_group,
  979. &cpumask_attr_group,
  980. NULL,
  981. };
  982. /*
  983. * Start the process for a new H_GET_24x7_DATA hcall.
  984. */
  985. static void init_24x7_request(struct hv_24x7_request_buffer *request_buffer,
  986. struct hv_24x7_data_result_buffer *result_buffer)
  987. {
  988. memset(request_buffer, 0, H24x7_DATA_BUFFER_SIZE);
  989. memset(result_buffer, 0, H24x7_DATA_BUFFER_SIZE);
  990. request_buffer->interface_version = interface_version;
  991. /* memset above set request_buffer->num_requests to 0 */
  992. }
  993. /*
  994. * Commit (i.e perform) the H_GET_24x7_DATA hcall using the data collected
  995. * by 'init_24x7_request()' and 'add_event_to_24x7_request()'.
  996. */
  997. static int make_24x7_request(struct hv_24x7_request_buffer *request_buffer,
  998. struct hv_24x7_data_result_buffer *result_buffer)
  999. {
  1000. long ret;
  1001. /*
  1002. * NOTE: Due to variable number of array elements in request and
  1003. * result buffer(s), sizeof() is not reliable. Use the actual
  1004. * allocated buffer size, H24x7_DATA_BUFFER_SIZE.
  1005. */
  1006. ret = plpar_hcall_norets(H_GET_24X7_DATA,
  1007. virt_to_phys(request_buffer), H24x7_DATA_BUFFER_SIZE,
  1008. virt_to_phys(result_buffer), H24x7_DATA_BUFFER_SIZE);
  1009. if (ret) {
  1010. struct hv_24x7_request *req;
  1011. req = request_buffer->requests;
  1012. pr_notice_ratelimited("hcall failed: [%d %#x %#x %d] => ret 0x%lx (%ld) detail=0x%x failing ix=%x\n",
  1013. req->performance_domain, req->data_offset,
  1014. req->starting_ix, req->starting_lpar_ix,
  1015. ret, ret, result_buffer->detailed_rc,
  1016. result_buffer->failing_request_ix);
  1017. return -EIO;
  1018. }
  1019. return 0;
  1020. }
  1021. /*
  1022. * Add the given @event to the next slot in the 24x7 request_buffer.
  1023. *
  1024. * Note that H_GET_24X7_DATA hcall allows reading several counters'
  1025. * values in a single HCALL. We expect the caller to add events to the
  1026. * request buffer one by one, make the HCALL and process the results.
  1027. */
  1028. static int add_event_to_24x7_request(struct perf_event *event,
  1029. struct hv_24x7_request_buffer *request_buffer)
  1030. {
  1031. u16 idx;
  1032. int i;
  1033. size_t req_size;
  1034. struct hv_24x7_request *req;
  1035. if (request_buffer->num_requests >=
  1036. max_num_requests(request_buffer->interface_version)) {
  1037. pr_devel("Too many requests for 24x7 HCALL %d\n",
  1038. request_buffer->num_requests);
  1039. return -EINVAL;
  1040. }
  1041. switch (event_get_domain(event)) {
  1042. case HV_PERF_DOMAIN_PHYS_CHIP:
  1043. idx = event_get_chip(event);
  1044. break;
  1045. case HV_PERF_DOMAIN_PHYS_CORE:
  1046. idx = event_get_core(event);
  1047. break;
  1048. default:
  1049. idx = event_get_vcpu(event);
  1050. }
  1051. req_size = H24x7_REQUEST_SIZE(request_buffer->interface_version);
  1052. i = request_buffer->num_requests++;
  1053. req = (void *) request_buffer->requests + i * req_size;
  1054. req->performance_domain = event_get_domain(event);
  1055. req->data_size = cpu_to_be16(8);
  1056. req->data_offset = cpu_to_be32(event_get_offset(event));
  1057. req->starting_lpar_ix = cpu_to_be16(event_get_lpar(event));
  1058. req->max_num_lpars = cpu_to_be16(1);
  1059. req->starting_ix = cpu_to_be16(idx);
  1060. req->max_ix = cpu_to_be16(1);
  1061. if (request_buffer->interface_version > 1) {
  1062. if (domain_needs_aggregation(req->performance_domain))
  1063. req->max_num_thread_groups = -1;
  1064. else if (req->performance_domain != HV_PERF_DOMAIN_PHYS_CHIP) {
  1065. req->starting_thread_group_ix = idx % 2;
  1066. req->max_num_thread_groups = 1;
  1067. }
  1068. }
  1069. return 0;
  1070. }
  1071. /**
  1072. * get_count_from_result - get event count from all result elements in result
  1073. *
  1074. * If the event corresponding to this result needs aggregation of the result
  1075. * element values, then this function does that.
  1076. *
  1077. * @event: Event associated with @res.
  1078. * @resb: Result buffer containing @res.
  1079. * @res: Result to work on.
  1080. * @countp: Output variable containing the event count.
  1081. * @next: Optional output variable pointing to the next result in @resb.
  1082. */
  1083. static int get_count_from_result(struct perf_event *event,
  1084. struct hv_24x7_data_result_buffer *resb,
  1085. struct hv_24x7_result *res, u64 *countp,
  1086. struct hv_24x7_result **next)
  1087. {
  1088. u16 num_elements = be16_to_cpu(res->num_elements_returned);
  1089. u16 data_size = be16_to_cpu(res->result_element_data_size);
  1090. unsigned int data_offset;
  1091. void *element_data;
  1092. int i;
  1093. u64 count;
  1094. /*
  1095. * We can bail out early if the result is empty.
  1096. */
  1097. if (!num_elements) {
  1098. pr_debug("Result of request %hhu is empty, nothing to do\n",
  1099. res->result_ix);
  1100. if (next)
  1101. *next = (struct hv_24x7_result *) res->elements;
  1102. return -ENODATA;
  1103. }
  1104. /*
  1105. * Since we always specify 1 as the maximum for the smallest resource
  1106. * we're requesting, there should to be only one element per result.
  1107. * Except when an event needs aggregation, in which case there are more.
  1108. */
  1109. if (num_elements != 1 &&
  1110. !domain_needs_aggregation(event_get_domain(event))) {
  1111. pr_err("Error: result of request %hhu has %hu elements\n",
  1112. res->result_ix, num_elements);
  1113. return -EIO;
  1114. }
  1115. if (data_size != sizeof(u64)) {
  1116. pr_debug("Error: result of request %hhu has data of %hu bytes\n",
  1117. res->result_ix, data_size);
  1118. return -ENOTSUPP;
  1119. }
  1120. if (resb->interface_version == 1)
  1121. data_offset = offsetof(struct hv_24x7_result_element_v1,
  1122. element_data);
  1123. else
  1124. data_offset = offsetof(struct hv_24x7_result_element_v2,
  1125. element_data);
  1126. /* Go through the result elements in the result. */
  1127. for (i = count = 0, element_data = res->elements + data_offset;
  1128. i < num_elements;
  1129. i++, element_data += data_size + data_offset)
  1130. count += be64_to_cpu(*((__be64 *)element_data));
  1131. *countp = count;
  1132. /* The next result is after the last result element. */
  1133. if (next)
  1134. *next = element_data - data_offset;
  1135. return 0;
  1136. }
  1137. static int single_24x7_request(struct perf_event *event, u64 *count)
  1138. {
  1139. int ret;
  1140. struct hv_24x7_request_buffer *request_buffer;
  1141. struct hv_24x7_data_result_buffer *result_buffer;
  1142. BUILD_BUG_ON(sizeof(*request_buffer) > 4096);
  1143. BUILD_BUG_ON(sizeof(*result_buffer) > 4096);
  1144. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  1145. result_buffer = (void *)get_cpu_var(hv_24x7_resb);
  1146. init_24x7_request(request_buffer, result_buffer);
  1147. ret = add_event_to_24x7_request(event, request_buffer);
  1148. if (ret)
  1149. goto out;
  1150. ret = make_24x7_request(request_buffer, result_buffer);
  1151. if (ret)
  1152. goto out;
  1153. /* process result from hcall */
  1154. ret = get_count_from_result(event, result_buffer,
  1155. result_buffer->results, count, NULL);
  1156. out:
  1157. put_cpu_var(hv_24x7_reqb);
  1158. put_cpu_var(hv_24x7_resb);
  1159. return ret;
  1160. }
  1161. static int h_24x7_event_init(struct perf_event *event)
  1162. {
  1163. struct hv_perf_caps caps;
  1164. unsigned int domain;
  1165. unsigned long hret;
  1166. u64 ct;
  1167. /* Not our event */
  1168. if (event->attr.type != event->pmu->type)
  1169. return -ENOENT;
  1170. /* Unused areas must be 0 */
  1171. if (event_get_reserved1(event) ||
  1172. event_get_reserved2(event) ||
  1173. event_get_reserved3(event)) {
  1174. pr_devel("reserved set when forbidden 0x%llx(0x%llx) 0x%llx(0x%llx) 0x%llx(0x%llx)\n",
  1175. event->attr.config,
  1176. event_get_reserved1(event),
  1177. event->attr.config1,
  1178. event_get_reserved2(event),
  1179. event->attr.config2,
  1180. event_get_reserved3(event));
  1181. return -EINVAL;
  1182. }
  1183. /* no branch sampling */
  1184. if (has_branch_stack(event))
  1185. return -EOPNOTSUPP;
  1186. /* offset must be 8 byte aligned */
  1187. if (event_get_offset(event) % 8) {
  1188. pr_devel("bad alignment\n");
  1189. return -EINVAL;
  1190. }
  1191. domain = event_get_domain(event);
  1192. if (domain == 0 || domain >= HV_PERF_DOMAIN_MAX) {
  1193. pr_devel("invalid domain %d\n", domain);
  1194. return -EINVAL;
  1195. }
  1196. hret = hv_perf_caps_get(&caps);
  1197. if (hret) {
  1198. pr_devel("could not get capabilities: rc=%ld\n", hret);
  1199. return -EIO;
  1200. }
  1201. /* Physical domains & other lpars require extra capabilities */
  1202. if (!caps.collect_privileged && (is_physical_domain(domain) ||
  1203. (event_get_lpar(event) != event_get_lpar_max()))) {
  1204. pr_devel("hv permissions disallow: is_physical_domain:%d, lpar=0x%llx\n",
  1205. is_physical_domain(domain),
  1206. event_get_lpar(event));
  1207. return -EACCES;
  1208. }
  1209. /* Get the initial value of the counter for this event */
  1210. if (single_24x7_request(event, &ct)) {
  1211. pr_devel("test hcall failed\n");
  1212. return -EIO;
  1213. }
  1214. (void)local64_xchg(&event->hw.prev_count, ct);
  1215. return 0;
  1216. }
  1217. static u64 h_24x7_get_value(struct perf_event *event)
  1218. {
  1219. u64 ct;
  1220. if (single_24x7_request(event, &ct))
  1221. /* We checked this in event init, shouldn't fail here... */
  1222. return 0;
  1223. return ct;
  1224. }
  1225. static void update_event_count(struct perf_event *event, u64 now)
  1226. {
  1227. s64 prev;
  1228. prev = local64_xchg(&event->hw.prev_count, now);
  1229. local64_add(now - prev, &event->count);
  1230. }
  1231. static void h_24x7_event_read(struct perf_event *event)
  1232. {
  1233. u64 now;
  1234. struct hv_24x7_request_buffer *request_buffer;
  1235. struct hv_24x7_hw *h24x7hw;
  1236. int txn_flags;
  1237. txn_flags = __this_cpu_read(hv_24x7_txn_flags);
  1238. /*
  1239. * If in a READ transaction, add this counter to the list of
  1240. * counters to read during the next HCALL (i.e commit_txn()).
  1241. * If not in a READ transaction, go ahead and make the HCALL
  1242. * to read this counter by itself.
  1243. */
  1244. if (txn_flags & PERF_PMU_TXN_READ) {
  1245. int i;
  1246. int ret;
  1247. if (__this_cpu_read(hv_24x7_txn_err))
  1248. return;
  1249. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  1250. ret = add_event_to_24x7_request(event, request_buffer);
  1251. if (ret) {
  1252. __this_cpu_write(hv_24x7_txn_err, ret);
  1253. } else {
  1254. /*
  1255. * Associate the event with the HCALL request index,
  1256. * so ->commit_txn() can quickly find/update count.
  1257. */
  1258. i = request_buffer->num_requests - 1;
  1259. h24x7hw = &get_cpu_var(hv_24x7_hw);
  1260. h24x7hw->events[i] = event;
  1261. put_cpu_var(h24x7hw);
  1262. }
  1263. put_cpu_var(hv_24x7_reqb);
  1264. } else {
  1265. now = h_24x7_get_value(event);
  1266. update_event_count(event, now);
  1267. }
  1268. }
  1269. static void h_24x7_event_start(struct perf_event *event, int flags)
  1270. {
  1271. if (flags & PERF_EF_RELOAD)
  1272. local64_set(&event->hw.prev_count, h_24x7_get_value(event));
  1273. }
  1274. static void h_24x7_event_stop(struct perf_event *event, int flags)
  1275. {
  1276. h_24x7_event_read(event);
  1277. }
  1278. static int h_24x7_event_add(struct perf_event *event, int flags)
  1279. {
  1280. if (flags & PERF_EF_START)
  1281. h_24x7_event_start(event, flags);
  1282. return 0;
  1283. }
  1284. /*
  1285. * 24x7 counters only support READ transactions. They are
  1286. * always counting and dont need/support ADD transactions.
  1287. * Cache the flags, but otherwise ignore transactions that
  1288. * are not PERF_PMU_TXN_READ.
  1289. */
  1290. static void h_24x7_event_start_txn(struct pmu *pmu, unsigned int flags)
  1291. {
  1292. struct hv_24x7_request_buffer *request_buffer;
  1293. struct hv_24x7_data_result_buffer *result_buffer;
  1294. /* We should not be called if we are already in a txn */
  1295. WARN_ON_ONCE(__this_cpu_read(hv_24x7_txn_flags));
  1296. __this_cpu_write(hv_24x7_txn_flags, flags);
  1297. if (flags & ~PERF_PMU_TXN_READ)
  1298. return;
  1299. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  1300. result_buffer = (void *)get_cpu_var(hv_24x7_resb);
  1301. init_24x7_request(request_buffer, result_buffer);
  1302. put_cpu_var(hv_24x7_resb);
  1303. put_cpu_var(hv_24x7_reqb);
  1304. }
  1305. /*
  1306. * Clean up transaction state.
  1307. *
  1308. * NOTE: Ignore state of request and result buffers for now.
  1309. * We will initialize them during the next read/txn.
  1310. */
  1311. static void reset_txn(void)
  1312. {
  1313. __this_cpu_write(hv_24x7_txn_flags, 0);
  1314. __this_cpu_write(hv_24x7_txn_err, 0);
  1315. }
  1316. /*
  1317. * 24x7 counters only support READ transactions. They are always counting
  1318. * and dont need/support ADD transactions. Clear ->txn_flags but otherwise
  1319. * ignore transactions that are not of type PERF_PMU_TXN_READ.
  1320. *
  1321. * For READ transactions, submit all pending 24x7 requests (i.e requests
  1322. * that were queued by h_24x7_event_read()), to the hypervisor and update
  1323. * the event counts.
  1324. */
  1325. static int h_24x7_event_commit_txn(struct pmu *pmu)
  1326. {
  1327. struct hv_24x7_request_buffer *request_buffer;
  1328. struct hv_24x7_data_result_buffer *result_buffer;
  1329. struct hv_24x7_result *res, *next_res;
  1330. u64 count;
  1331. int i, ret, txn_flags;
  1332. struct hv_24x7_hw *h24x7hw;
  1333. txn_flags = __this_cpu_read(hv_24x7_txn_flags);
  1334. WARN_ON_ONCE(!txn_flags);
  1335. ret = 0;
  1336. if (txn_flags & ~PERF_PMU_TXN_READ)
  1337. goto out;
  1338. ret = __this_cpu_read(hv_24x7_txn_err);
  1339. if (ret)
  1340. goto out;
  1341. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  1342. result_buffer = (void *)get_cpu_var(hv_24x7_resb);
  1343. ret = make_24x7_request(request_buffer, result_buffer);
  1344. if (ret)
  1345. goto put_reqb;
  1346. h24x7hw = &get_cpu_var(hv_24x7_hw);
  1347. /* Go through results in the result buffer to update event counts. */
  1348. for (i = 0, res = result_buffer->results;
  1349. i < result_buffer->num_results; i++, res = next_res) {
  1350. struct perf_event *event = h24x7hw->events[res->result_ix];
  1351. ret = get_count_from_result(event, result_buffer, res, &count,
  1352. &next_res);
  1353. if (ret)
  1354. break;
  1355. update_event_count(event, count);
  1356. }
  1357. put_cpu_var(hv_24x7_hw);
  1358. put_reqb:
  1359. put_cpu_var(hv_24x7_resb);
  1360. put_cpu_var(hv_24x7_reqb);
  1361. out:
  1362. reset_txn();
  1363. return ret;
  1364. }
  1365. /*
  1366. * 24x7 counters only support READ transactions. They are always counting
  1367. * and dont need/support ADD transactions. However, regardless of type
  1368. * of transaction, all we need to do is cleanup, so we don't have to check
  1369. * the type of transaction.
  1370. */
  1371. static void h_24x7_event_cancel_txn(struct pmu *pmu)
  1372. {
  1373. WARN_ON_ONCE(!__this_cpu_read(hv_24x7_txn_flags));
  1374. reset_txn();
  1375. }
  1376. static struct pmu h_24x7_pmu = {
  1377. .task_ctx_nr = perf_invalid_context,
  1378. .name = "hv_24x7",
  1379. .attr_groups = attr_groups,
  1380. .event_init = h_24x7_event_init,
  1381. .add = h_24x7_event_add,
  1382. .del = h_24x7_event_stop,
  1383. .start = h_24x7_event_start,
  1384. .stop = h_24x7_event_stop,
  1385. .read = h_24x7_event_read,
  1386. .start_txn = h_24x7_event_start_txn,
  1387. .commit_txn = h_24x7_event_commit_txn,
  1388. .cancel_txn = h_24x7_event_cancel_txn,
  1389. .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
  1390. };
  1391. static int ppc_hv_24x7_cpu_online(unsigned int cpu)
  1392. {
  1393. if (cpumask_empty(&hv_24x7_cpumask))
  1394. cpumask_set_cpu(cpu, &hv_24x7_cpumask);
  1395. return 0;
  1396. }
  1397. static int ppc_hv_24x7_cpu_offline(unsigned int cpu)
  1398. {
  1399. int target;
  1400. /* Check if exiting cpu is used for collecting 24x7 events */
  1401. if (!cpumask_test_and_clear_cpu(cpu, &hv_24x7_cpumask))
  1402. return 0;
  1403. /* Find a new cpu to collect 24x7 events */
  1404. target = cpumask_last(cpu_active_mask);
  1405. if (target < 0 || target >= nr_cpu_ids) {
  1406. pr_err("hv_24x7: CPU hotplug init failed\n");
  1407. return -1;
  1408. }
  1409. /* Migrate 24x7 events to the new target */
  1410. cpumask_set_cpu(target, &hv_24x7_cpumask);
  1411. perf_pmu_migrate_context(&h_24x7_pmu, cpu, target);
  1412. return 0;
  1413. }
  1414. static int hv_24x7_cpu_hotplug_init(void)
  1415. {
  1416. return cpuhp_setup_state(CPUHP_AP_PERF_POWERPC_HV_24x7_ONLINE,
  1417. "perf/powerpc/hv_24x7:online",
  1418. ppc_hv_24x7_cpu_online,
  1419. ppc_hv_24x7_cpu_offline);
  1420. }
  1421. static int hv_24x7_init(void)
  1422. {
  1423. int r;
  1424. unsigned long hret;
  1425. unsigned int pvr = mfspr(SPRN_PVR);
  1426. struct hv_perf_caps caps;
  1427. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  1428. pr_debug("not a virtualized system, not enabling\n");
  1429. return -ENODEV;
  1430. }
  1431. /* POWER8 only supports v1, while POWER9 only supports v2. */
  1432. if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
  1433. PVR_VER(pvr) == PVR_POWER8NVL)
  1434. interface_version = 1;
  1435. else {
  1436. interface_version = 2;
  1437. /* SMT8 in POWER9 needs to aggregate result elements. */
  1438. if (threads_per_core == 8)
  1439. aggregate_result_elements = true;
  1440. }
  1441. hret = hv_perf_caps_get(&caps);
  1442. if (hret) {
  1443. pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
  1444. hret);
  1445. return -ENODEV;
  1446. }
  1447. hv_page_cache = kmem_cache_create("hv-page-4096", 4096, 4096, 0, NULL);
  1448. if (!hv_page_cache)
  1449. return -ENOMEM;
  1450. /* sampling not supported */
  1451. h_24x7_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  1452. r = create_events_from_catalog(&event_group.attrs,
  1453. &event_desc_group.attrs,
  1454. &event_long_desc_group.attrs);
  1455. if (r)
  1456. return r;
  1457. /* init cpuhotplug */
  1458. r = hv_24x7_cpu_hotplug_init();
  1459. if (r)
  1460. return r;
  1461. r = perf_pmu_register(&h_24x7_pmu, h_24x7_pmu.name, -1);
  1462. if (r)
  1463. return r;
  1464. read_24x7_sys_info();
  1465. return 0;
  1466. }
  1467. device_initcall(hv_24x7_init);