hv-24x7.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. /*
  2. * Hypervisor supplied "24x7" performance counter support
  3. *
  4. * Author: Cody P Schafer <cody@linux.vnet.ibm.com>
  5. * Copyright 2014 IBM Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #define pr_fmt(fmt) "hv-24x7: " fmt
  13. #include <linux/perf_event.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <asm/firmware.h>
  19. #include <asm/hvcall.h>
  20. #include <asm/io.h>
  21. #include <linux/byteorder/generic.h>
  22. #include "hv-24x7.h"
  23. #include "hv-24x7-catalog.h"
  24. #include "hv-common.h"
  25. static const char *event_domain_suffix(unsigned domain)
  26. {
  27. switch (domain) {
  28. #define DOMAIN(n, v, x, c) \
  29. case HV_PERF_DOMAIN_##n: \
  30. return "__" #n;
  31. #include "hv-24x7-domains.h"
  32. #undef DOMAIN
  33. default:
  34. WARN(1, "unknown domain %d\n", domain);
  35. return "__UNKNOWN_DOMAIN_SUFFIX";
  36. }
  37. }
  38. static bool domain_is_valid(unsigned domain)
  39. {
  40. switch (domain) {
  41. #define DOMAIN(n, v, x, c) \
  42. case HV_PERF_DOMAIN_##n: \
  43. /* fall through */
  44. #include "hv-24x7-domains.h"
  45. #undef DOMAIN
  46. return true;
  47. default:
  48. return false;
  49. }
  50. }
  51. static bool is_physical_domain(unsigned domain)
  52. {
  53. switch (domain) {
  54. #define DOMAIN(n, v, x, c) \
  55. case HV_PERF_DOMAIN_##n: \
  56. return c;
  57. #include "hv-24x7-domains.h"
  58. #undef DOMAIN
  59. default:
  60. return false;
  61. }
  62. }
  63. static bool catalog_entry_domain_is_valid(unsigned domain)
  64. {
  65. return is_physical_domain(domain);
  66. }
  67. /*
  68. * TODO: Merging events:
  69. * - Think of the hcall as an interface to a 4d array of counters:
  70. * - x = domains
  71. * - y = indexes in the domain (core, chip, vcpu, node, etc)
  72. * - z = offset into the counter space
  73. * - w = lpars (guest vms, "logical partitions")
  74. * - A single request is: x,y,y_last,z,z_last,w,w_last
  75. * - this means we can retrieve a rectangle of counters in y,z for a single x.
  76. *
  77. * - Things to consider (ignoring w):
  78. * - input cost_per_request = 16
  79. * - output cost_per_result(ys,zs) = 8 + 8 * ys + ys * zs
  80. * - limited number of requests per hcall (must fit into 4K bytes)
  81. * - 4k = 16 [buffer header] - 16 [request size] * request_count
  82. * - 255 requests per hcall
  83. * - sometimes it will be more efficient to read extra data and discard
  84. */
  85. /*
  86. * Example usage:
  87. * perf stat -e 'hv_24x7/domain=2,offset=8,vcpu=0,lpar=0xffffffff/'
  88. */
  89. /* u3 0-6, one of HV_24X7_PERF_DOMAIN */
  90. EVENT_DEFINE_RANGE_FORMAT(domain, config, 0, 3);
  91. /* u16 */
  92. EVENT_DEFINE_RANGE_FORMAT(core, config, 16, 31);
  93. EVENT_DEFINE_RANGE_FORMAT(vcpu, config, 16, 31);
  94. /* u32, see "data_offset" */
  95. EVENT_DEFINE_RANGE_FORMAT(offset, config, 32, 63);
  96. /* u16 */
  97. EVENT_DEFINE_RANGE_FORMAT(lpar, config1, 0, 15);
  98. EVENT_DEFINE_RANGE(reserved1, config, 4, 15);
  99. EVENT_DEFINE_RANGE(reserved2, config1, 16, 63);
  100. EVENT_DEFINE_RANGE(reserved3, config2, 0, 63);
  101. static struct attribute *format_attrs[] = {
  102. &format_attr_domain.attr,
  103. &format_attr_offset.attr,
  104. &format_attr_core.attr,
  105. &format_attr_vcpu.attr,
  106. &format_attr_lpar.attr,
  107. NULL,
  108. };
  109. static struct attribute_group format_group = {
  110. .name = "format",
  111. .attrs = format_attrs,
  112. };
  113. static struct attribute_group event_group = {
  114. .name = "events",
  115. /* .attrs is set in init */
  116. };
  117. static struct attribute_group event_desc_group = {
  118. .name = "event_descs",
  119. /* .attrs is set in init */
  120. };
  121. static struct attribute_group event_long_desc_group = {
  122. .name = "event_long_descs",
  123. /* .attrs is set in init */
  124. };
  125. static struct kmem_cache *hv_page_cache;
  126. DEFINE_PER_CPU(int, hv_24x7_txn_flags);
  127. DEFINE_PER_CPU(int, hv_24x7_txn_err);
  128. struct hv_24x7_hw {
  129. struct perf_event *events[255];
  130. };
  131. DEFINE_PER_CPU(struct hv_24x7_hw, hv_24x7_hw);
  132. /*
  133. * request_buffer and result_buffer are not required to be 4k aligned,
  134. * but are not allowed to cross any 4k boundary. Aligning them to 4k is
  135. * the simplest way to ensure that.
  136. */
  137. #define H24x7_DATA_BUFFER_SIZE 4096
  138. DEFINE_PER_CPU(char, hv_24x7_reqb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096);
  139. DEFINE_PER_CPU(char, hv_24x7_resb[H24x7_DATA_BUFFER_SIZE]) __aligned(4096);
  140. static char *event_name(struct hv_24x7_event_data *ev, int *len)
  141. {
  142. *len = be16_to_cpu(ev->event_name_len) - 2;
  143. return (char *)ev->remainder;
  144. }
  145. static char *event_desc(struct hv_24x7_event_data *ev, int *len)
  146. {
  147. unsigned nl = be16_to_cpu(ev->event_name_len);
  148. __be16 *desc_len = (__be16 *)(ev->remainder + nl - 2);
  149. *len = be16_to_cpu(*desc_len) - 2;
  150. return (char *)ev->remainder + nl;
  151. }
  152. static char *event_long_desc(struct hv_24x7_event_data *ev, int *len)
  153. {
  154. unsigned nl = be16_to_cpu(ev->event_name_len);
  155. __be16 *desc_len_ = (__be16 *)(ev->remainder + nl - 2);
  156. unsigned desc_len = be16_to_cpu(*desc_len_);
  157. __be16 *long_desc_len = (__be16 *)(ev->remainder + nl + desc_len - 2);
  158. *len = be16_to_cpu(*long_desc_len) - 2;
  159. return (char *)ev->remainder + nl + desc_len;
  160. }
  161. static bool event_fixed_portion_is_within(struct hv_24x7_event_data *ev,
  162. void *end)
  163. {
  164. void *start = ev;
  165. return (start + offsetof(struct hv_24x7_event_data, remainder)) < end;
  166. }
  167. /*
  168. * Things we don't check:
  169. * - padding for desc, name, and long/detailed desc is required to be '\0'
  170. * bytes.
  171. *
  172. * Return NULL if we pass end,
  173. * Otherwise return the address of the byte just following the event.
  174. */
  175. static void *event_end(struct hv_24x7_event_data *ev, void *end)
  176. {
  177. void *start = ev;
  178. __be16 *dl_, *ldl_;
  179. unsigned dl, ldl;
  180. unsigned nl = be16_to_cpu(ev->event_name_len);
  181. if (nl < 2) {
  182. pr_debug("%s: name length too short: %d", __func__, nl);
  183. return NULL;
  184. }
  185. if (start + nl > end) {
  186. pr_debug("%s: start=%p + nl=%u > end=%p",
  187. __func__, start, nl, end);
  188. return NULL;
  189. }
  190. dl_ = (__be16 *)(ev->remainder + nl - 2);
  191. if (!IS_ALIGNED((uintptr_t)dl_, 2))
  192. pr_warn("desc len not aligned %p", dl_);
  193. dl = be16_to_cpu(*dl_);
  194. if (dl < 2) {
  195. pr_debug("%s: desc len too short: %d", __func__, dl);
  196. return NULL;
  197. }
  198. if (start + nl + dl > end) {
  199. pr_debug("%s: (start=%p + nl=%u + dl=%u)=%p > end=%p",
  200. __func__, start, nl, dl, start + nl + dl, end);
  201. return NULL;
  202. }
  203. ldl_ = (__be16 *)(ev->remainder + nl + dl - 2);
  204. if (!IS_ALIGNED((uintptr_t)ldl_, 2))
  205. pr_warn("long desc len not aligned %p", ldl_);
  206. ldl = be16_to_cpu(*ldl_);
  207. if (ldl < 2) {
  208. pr_debug("%s: long desc len too short (ldl=%u)",
  209. __func__, ldl);
  210. return NULL;
  211. }
  212. if (start + nl + dl + ldl > end) {
  213. pr_debug("%s: start=%p + nl=%u + dl=%u + ldl=%u > end=%p",
  214. __func__, start, nl, dl, ldl, end);
  215. return NULL;
  216. }
  217. return start + nl + dl + ldl;
  218. }
  219. static unsigned long h_get_24x7_catalog_page_(unsigned long phys_4096,
  220. unsigned long version,
  221. unsigned long index)
  222. {
  223. pr_devel("h_get_24x7_catalog_page(0x%lx, %lu, %lu)",
  224. phys_4096, version, index);
  225. WARN_ON(!IS_ALIGNED(phys_4096, 4096));
  226. return plpar_hcall_norets(H_GET_24X7_CATALOG_PAGE,
  227. phys_4096, version, index);
  228. }
  229. static unsigned long h_get_24x7_catalog_page(char page[],
  230. u64 version, u32 index)
  231. {
  232. return h_get_24x7_catalog_page_(virt_to_phys(page),
  233. version, index);
  234. }
  235. static unsigned core_domains[] = {
  236. HV_PERF_DOMAIN_PHYS_CORE,
  237. HV_PERF_DOMAIN_VCPU_HOME_CORE,
  238. HV_PERF_DOMAIN_VCPU_HOME_CHIP,
  239. HV_PERF_DOMAIN_VCPU_HOME_NODE,
  240. HV_PERF_DOMAIN_VCPU_REMOTE_NODE,
  241. };
  242. /* chip event data always yeilds a single event, core yeilds multiple */
  243. #define MAX_EVENTS_PER_EVENT_DATA ARRAY_SIZE(core_domains)
  244. static char *event_fmt(struct hv_24x7_event_data *event, unsigned domain)
  245. {
  246. const char *sindex;
  247. const char *lpar;
  248. if (is_physical_domain(domain)) {
  249. lpar = "0x0";
  250. sindex = "core";
  251. } else {
  252. lpar = "?";
  253. sindex = "vcpu";
  254. }
  255. return kasprintf(GFP_KERNEL,
  256. "domain=0x%x,offset=0x%x,%s=?,lpar=%s",
  257. domain,
  258. be16_to_cpu(event->event_counter_offs) +
  259. be16_to_cpu(event->event_group_record_offs),
  260. sindex,
  261. lpar);
  262. }
  263. /* Avoid trusting fw to NUL terminate strings */
  264. static char *memdup_to_str(char *maybe_str, int max_len, gfp_t gfp)
  265. {
  266. return kasprintf(gfp, "%.*s", max_len, maybe_str);
  267. }
  268. static ssize_t device_show_string(struct device *dev,
  269. struct device_attribute *attr, char *buf)
  270. {
  271. struct dev_ext_attribute *d;
  272. d = container_of(attr, struct dev_ext_attribute, attr);
  273. return sprintf(buf, "%s\n", (char *)d->var);
  274. }
  275. static struct attribute *device_str_attr_create_(char *name, char *str)
  276. {
  277. struct dev_ext_attribute *attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  278. if (!attr)
  279. return NULL;
  280. sysfs_attr_init(&attr->attr.attr);
  281. attr->var = str;
  282. attr->attr.attr.name = name;
  283. attr->attr.attr.mode = 0444;
  284. attr->attr.show = device_show_string;
  285. return &attr->attr.attr;
  286. }
  287. static struct attribute *device_str_attr_create(char *name, int name_max,
  288. int name_nonce,
  289. char *str, size_t str_max)
  290. {
  291. char *n;
  292. char *s = memdup_to_str(str, str_max, GFP_KERNEL);
  293. struct attribute *a;
  294. if (!s)
  295. return NULL;
  296. if (!name_nonce)
  297. n = kasprintf(GFP_KERNEL, "%.*s", name_max, name);
  298. else
  299. n = kasprintf(GFP_KERNEL, "%.*s__%d", name_max, name,
  300. name_nonce);
  301. if (!n)
  302. goto out_s;
  303. a = device_str_attr_create_(n, s);
  304. if (!a)
  305. goto out_n;
  306. return a;
  307. out_n:
  308. kfree(n);
  309. out_s:
  310. kfree(s);
  311. return NULL;
  312. }
  313. static void device_str_attr_destroy(struct attribute *attr)
  314. {
  315. struct dev_ext_attribute *d;
  316. d = container_of(attr, struct dev_ext_attribute, attr.attr);
  317. kfree(d->var);
  318. kfree(d->attr.attr.name);
  319. kfree(d);
  320. }
  321. static struct attribute *event_to_attr(unsigned ix,
  322. struct hv_24x7_event_data *event,
  323. unsigned domain,
  324. int nonce)
  325. {
  326. int event_name_len;
  327. char *ev_name, *a_ev_name, *val;
  328. const char *ev_suffix;
  329. struct attribute *attr;
  330. if (!domain_is_valid(domain)) {
  331. pr_warn("catalog event %u has invalid domain %u\n",
  332. ix, domain);
  333. return NULL;
  334. }
  335. val = event_fmt(event, domain);
  336. if (!val)
  337. return NULL;
  338. ev_suffix = event_domain_suffix(domain);
  339. ev_name = event_name(event, &event_name_len);
  340. if (!nonce)
  341. a_ev_name = kasprintf(GFP_KERNEL, "%.*s%s",
  342. (int)event_name_len, ev_name, ev_suffix);
  343. else
  344. a_ev_name = kasprintf(GFP_KERNEL, "%.*s%s__%d",
  345. (int)event_name_len, ev_name, ev_suffix, nonce);
  346. if (!a_ev_name)
  347. goto out_val;
  348. attr = device_str_attr_create_(a_ev_name, val);
  349. if (!attr)
  350. goto out_name;
  351. return attr;
  352. out_name:
  353. kfree(a_ev_name);
  354. out_val:
  355. kfree(val);
  356. return NULL;
  357. }
  358. static struct attribute *event_to_desc_attr(struct hv_24x7_event_data *event,
  359. int nonce)
  360. {
  361. int nl, dl;
  362. char *name = event_name(event, &nl);
  363. char *desc = event_desc(event, &dl);
  364. /* If there isn't a description, don't create the sysfs file */
  365. if (!dl)
  366. return NULL;
  367. return device_str_attr_create(name, nl, nonce, desc, dl);
  368. }
  369. static struct attribute *
  370. event_to_long_desc_attr(struct hv_24x7_event_data *event, int nonce)
  371. {
  372. int nl, dl;
  373. char *name = event_name(event, &nl);
  374. char *desc = event_long_desc(event, &dl);
  375. /* If there isn't a description, don't create the sysfs file */
  376. if (!dl)
  377. return NULL;
  378. return device_str_attr_create(name, nl, nonce, desc, dl);
  379. }
  380. static ssize_t event_data_to_attrs(unsigned ix, struct attribute **attrs,
  381. struct hv_24x7_event_data *event, int nonce)
  382. {
  383. unsigned i;
  384. switch (event->domain) {
  385. case HV_PERF_DOMAIN_PHYS_CHIP:
  386. *attrs = event_to_attr(ix, event, event->domain, nonce);
  387. return 1;
  388. case HV_PERF_DOMAIN_PHYS_CORE:
  389. for (i = 0; i < ARRAY_SIZE(core_domains); i++) {
  390. attrs[i] = event_to_attr(ix, event, core_domains[i],
  391. nonce);
  392. if (!attrs[i]) {
  393. pr_warn("catalog event %u: individual attr %u "
  394. "creation failure\n", ix, i);
  395. for (; i; i--)
  396. device_str_attr_destroy(attrs[i - 1]);
  397. return -1;
  398. }
  399. }
  400. return i;
  401. default:
  402. pr_warn("catalog event %u: domain %u is not allowed in the "
  403. "catalog\n", ix, event->domain);
  404. return -1;
  405. }
  406. }
  407. static size_t event_to_attr_ct(struct hv_24x7_event_data *event)
  408. {
  409. switch (event->domain) {
  410. case HV_PERF_DOMAIN_PHYS_CHIP:
  411. return 1;
  412. case HV_PERF_DOMAIN_PHYS_CORE:
  413. return ARRAY_SIZE(core_domains);
  414. default:
  415. return 0;
  416. }
  417. }
  418. static unsigned long vmalloc_to_phys(void *v)
  419. {
  420. struct page *p = vmalloc_to_page(v);
  421. BUG_ON(!p);
  422. return page_to_phys(p) + offset_in_page(v);
  423. }
  424. /* */
  425. struct event_uniq {
  426. struct rb_node node;
  427. const char *name;
  428. int nl;
  429. unsigned ct;
  430. unsigned domain;
  431. };
  432. static int memord(const void *d1, size_t s1, const void *d2, size_t s2)
  433. {
  434. if (s1 < s2)
  435. return 1;
  436. if (s1 > s2)
  437. return -1;
  438. return memcmp(d1, d2, s1);
  439. }
  440. static int ev_uniq_ord(const void *v1, size_t s1, unsigned d1, const void *v2,
  441. size_t s2, unsigned d2)
  442. {
  443. int r = memord(v1, s1, v2, s2);
  444. if (r)
  445. return r;
  446. if (d1 > d2)
  447. return 1;
  448. if (d2 > d1)
  449. return -1;
  450. return 0;
  451. }
  452. static int event_uniq_add(struct rb_root *root, const char *name, int nl,
  453. unsigned domain)
  454. {
  455. struct rb_node **new = &(root->rb_node), *parent = NULL;
  456. struct event_uniq *data;
  457. /* Figure out where to put new node */
  458. while (*new) {
  459. struct event_uniq *it;
  460. int result;
  461. it = container_of(*new, struct event_uniq, node);
  462. result = ev_uniq_ord(name, nl, domain, it->name, it->nl,
  463. it->domain);
  464. parent = *new;
  465. if (result < 0)
  466. new = &((*new)->rb_left);
  467. else if (result > 0)
  468. new = &((*new)->rb_right);
  469. else {
  470. it->ct++;
  471. pr_info("found a duplicate event %.*s, ct=%u\n", nl,
  472. name, it->ct);
  473. return it->ct;
  474. }
  475. }
  476. data = kmalloc(sizeof(*data), GFP_KERNEL);
  477. if (!data)
  478. return -ENOMEM;
  479. *data = (struct event_uniq) {
  480. .name = name,
  481. .nl = nl,
  482. .ct = 0,
  483. .domain = domain,
  484. };
  485. /* Add new node and rebalance tree. */
  486. rb_link_node(&data->node, parent, new);
  487. rb_insert_color(&data->node, root);
  488. /* data->ct */
  489. return 0;
  490. }
  491. static void event_uniq_destroy(struct rb_root *root)
  492. {
  493. /*
  494. * the strings we point to are in the giant block of memory filled by
  495. * the catalog, and are freed separately.
  496. */
  497. struct event_uniq *pos, *n;
  498. rbtree_postorder_for_each_entry_safe(pos, n, root, node)
  499. kfree(pos);
  500. }
  501. /*
  502. * ensure the event structure's sizes are self consistent and don't cause us to
  503. * read outside of the event
  504. *
  505. * On success, return the event length in bytes.
  506. * Otherwise, return -1 (and print as appropriate).
  507. */
  508. static ssize_t catalog_event_len_validate(struct hv_24x7_event_data *event,
  509. size_t event_idx,
  510. size_t event_data_bytes,
  511. size_t event_entry_count,
  512. size_t offset, void *end)
  513. {
  514. ssize_t ev_len;
  515. void *ev_end, *calc_ev_end;
  516. if (offset >= event_data_bytes)
  517. return -1;
  518. if (event_idx >= event_entry_count) {
  519. pr_devel("catalog event data has %zu bytes of padding after last event\n",
  520. event_data_bytes - offset);
  521. return -1;
  522. }
  523. if (!event_fixed_portion_is_within(event, end)) {
  524. pr_warn("event %zu fixed portion is not within range\n",
  525. event_idx);
  526. return -1;
  527. }
  528. ev_len = be16_to_cpu(event->length);
  529. if (ev_len % 16)
  530. pr_info("event %zu has length %zu not divisible by 16: event=%pK\n",
  531. event_idx, ev_len, event);
  532. ev_end = (__u8 *)event + ev_len;
  533. if (ev_end > end) {
  534. pr_warn("event %zu has .length=%zu, ends after buffer end: ev_end=%pK > end=%pK, offset=%zu\n",
  535. event_idx, ev_len, ev_end, end,
  536. offset);
  537. return -1;
  538. }
  539. calc_ev_end = event_end(event, end);
  540. if (!calc_ev_end) {
  541. pr_warn("event %zu has a calculated length which exceeds buffer length %zu: event=%pK end=%pK, offset=%zu\n",
  542. event_idx, event_data_bytes, event, end,
  543. offset);
  544. return -1;
  545. }
  546. if (calc_ev_end > ev_end) {
  547. pr_warn("event %zu exceeds it's own length: event=%pK, end=%pK, offset=%zu, calc_ev_end=%pK\n",
  548. event_idx, event, ev_end, offset, calc_ev_end);
  549. return -1;
  550. }
  551. return ev_len;
  552. }
  553. #define MAX_4K (SIZE_MAX / 4096)
  554. static int create_events_from_catalog(struct attribute ***events_,
  555. struct attribute ***event_descs_,
  556. struct attribute ***event_long_descs_)
  557. {
  558. unsigned long hret;
  559. size_t catalog_len, catalog_page_len, event_entry_count,
  560. event_data_len, event_data_offs,
  561. event_data_bytes, junk_events, event_idx, event_attr_ct, i,
  562. attr_max, event_idx_last, desc_ct, long_desc_ct;
  563. ssize_t ct, ev_len;
  564. uint32_t catalog_version_num;
  565. struct attribute **events, **event_descs, **event_long_descs;
  566. struct hv_24x7_catalog_page_0 *page_0 =
  567. kmem_cache_alloc(hv_page_cache, GFP_KERNEL);
  568. void *page = page_0;
  569. void *event_data, *end;
  570. struct hv_24x7_event_data *event;
  571. struct rb_root ev_uniq = RB_ROOT;
  572. int ret = 0;
  573. if (!page) {
  574. ret = -ENOMEM;
  575. goto e_out;
  576. }
  577. hret = h_get_24x7_catalog_page(page, 0, 0);
  578. if (hret) {
  579. ret = -EIO;
  580. goto e_free;
  581. }
  582. catalog_version_num = be64_to_cpu(page_0->version);
  583. catalog_page_len = be32_to_cpu(page_0->length);
  584. if (MAX_4K < catalog_page_len) {
  585. pr_err("invalid page count: %zu\n", catalog_page_len);
  586. ret = -EIO;
  587. goto e_free;
  588. }
  589. catalog_len = catalog_page_len * 4096;
  590. event_entry_count = be16_to_cpu(page_0->event_entry_count);
  591. event_data_offs = be16_to_cpu(page_0->event_data_offs);
  592. event_data_len = be16_to_cpu(page_0->event_data_len);
  593. pr_devel("cv %zu cl %zu eec %zu edo %zu edl %zu\n",
  594. (size_t)catalog_version_num, catalog_len,
  595. event_entry_count, event_data_offs, event_data_len);
  596. if ((MAX_4K < event_data_len)
  597. || (MAX_4K < event_data_offs)
  598. || (MAX_4K - event_data_offs < event_data_len)) {
  599. pr_err("invalid event data offs %zu and/or len %zu\n",
  600. event_data_offs, event_data_len);
  601. ret = -EIO;
  602. goto e_free;
  603. }
  604. if ((event_data_offs + event_data_len) > catalog_page_len) {
  605. pr_err("event data %zu-%zu does not fit inside catalog 0-%zu\n",
  606. event_data_offs,
  607. event_data_offs + event_data_len,
  608. catalog_page_len);
  609. ret = -EIO;
  610. goto e_free;
  611. }
  612. if (SIZE_MAX / MAX_EVENTS_PER_EVENT_DATA - 1 < event_entry_count) {
  613. pr_err("event_entry_count %zu is invalid\n",
  614. event_entry_count);
  615. ret = -EIO;
  616. goto e_free;
  617. }
  618. event_data_bytes = event_data_len * 4096;
  619. /*
  620. * event data can span several pages, events can cross between these
  621. * pages. Use vmalloc to make this easier.
  622. */
  623. event_data = vmalloc(event_data_bytes);
  624. if (!event_data) {
  625. pr_err("could not allocate event data\n");
  626. ret = -ENOMEM;
  627. goto e_free;
  628. }
  629. end = event_data + event_data_bytes;
  630. /*
  631. * using vmalloc_to_phys() like this only works if PAGE_SIZE is
  632. * divisible by 4096
  633. */
  634. BUILD_BUG_ON(PAGE_SIZE % 4096);
  635. for (i = 0; i < event_data_len; i++) {
  636. hret = h_get_24x7_catalog_page_(
  637. vmalloc_to_phys(event_data + i * 4096),
  638. catalog_version_num,
  639. i + event_data_offs);
  640. if (hret) {
  641. pr_err("failed to get event data in page %zu\n",
  642. i + event_data_offs);
  643. ret = -EIO;
  644. goto e_event_data;
  645. }
  646. }
  647. /*
  648. * scan the catalog to determine the number of attributes we need, and
  649. * verify it at the same time.
  650. */
  651. for (junk_events = 0, event = event_data, event_idx = 0, attr_max = 0;
  652. ;
  653. event_idx++, event = (void *)event + ev_len) {
  654. size_t offset = (void *)event - (void *)event_data;
  655. char *name;
  656. int nl;
  657. ev_len = catalog_event_len_validate(event, event_idx,
  658. event_data_bytes,
  659. event_entry_count,
  660. offset, end);
  661. if (ev_len < 0)
  662. break;
  663. name = event_name(event, &nl);
  664. if (event->event_group_record_len == 0) {
  665. pr_devel("invalid event %zu (%.*s): group_record_len == 0, skipping\n",
  666. event_idx, nl, name);
  667. junk_events++;
  668. continue;
  669. }
  670. if (!catalog_entry_domain_is_valid(event->domain)) {
  671. pr_info("event %zu (%.*s) has invalid domain %d\n",
  672. event_idx, nl, name, event->domain);
  673. junk_events++;
  674. continue;
  675. }
  676. attr_max += event_to_attr_ct(event);
  677. }
  678. event_idx_last = event_idx;
  679. if (event_idx_last != event_entry_count)
  680. pr_warn("event buffer ended before listed # of events were parsed (got %zu, wanted %zu, junk %zu)\n",
  681. event_idx_last, event_entry_count, junk_events);
  682. events = kmalloc_array(attr_max + 1, sizeof(*events), GFP_KERNEL);
  683. if (!events) {
  684. ret = -ENOMEM;
  685. goto e_event_data;
  686. }
  687. event_descs = kmalloc_array(event_idx + 1, sizeof(*event_descs),
  688. GFP_KERNEL);
  689. if (!event_descs) {
  690. ret = -ENOMEM;
  691. goto e_event_attrs;
  692. }
  693. event_long_descs = kmalloc_array(event_idx + 1,
  694. sizeof(*event_long_descs), GFP_KERNEL);
  695. if (!event_long_descs) {
  696. ret = -ENOMEM;
  697. goto e_event_descs;
  698. }
  699. /* Iterate over the catalog filling in the attribute vector */
  700. for (junk_events = 0, event_attr_ct = 0, desc_ct = 0, long_desc_ct = 0,
  701. event = event_data, event_idx = 0;
  702. event_idx < event_idx_last;
  703. event_idx++, ev_len = be16_to_cpu(event->length),
  704. event = (void *)event + ev_len) {
  705. char *name;
  706. int nl;
  707. int nonce;
  708. /*
  709. * these are the only "bad" events that are intermixed and that
  710. * we can ignore without issue. make sure to skip them here
  711. */
  712. if (event->event_group_record_len == 0)
  713. continue;
  714. if (!catalog_entry_domain_is_valid(event->domain))
  715. continue;
  716. name = event_name(event, &nl);
  717. nonce = event_uniq_add(&ev_uniq, name, nl, event->domain);
  718. ct = event_data_to_attrs(event_idx, events + event_attr_ct,
  719. event, nonce);
  720. if (ct <= 0) {
  721. pr_warn("event %zu (%.*s) creation failure, skipping\n",
  722. event_idx, nl, name);
  723. junk_events++;
  724. } else {
  725. event_attr_ct += ct;
  726. event_descs[desc_ct] = event_to_desc_attr(event, nonce);
  727. if (event_descs[desc_ct])
  728. desc_ct++;
  729. event_long_descs[long_desc_ct] =
  730. event_to_long_desc_attr(event, nonce);
  731. if (event_long_descs[long_desc_ct])
  732. long_desc_ct++;
  733. }
  734. }
  735. pr_info("read %zu catalog entries, created %zu event attrs (%zu failures), %zu descs\n",
  736. event_idx, event_attr_ct, junk_events, desc_ct);
  737. events[event_attr_ct] = NULL;
  738. event_descs[desc_ct] = NULL;
  739. event_long_descs[long_desc_ct] = NULL;
  740. event_uniq_destroy(&ev_uniq);
  741. vfree(event_data);
  742. kmem_cache_free(hv_page_cache, page);
  743. *events_ = events;
  744. *event_descs_ = event_descs;
  745. *event_long_descs_ = event_long_descs;
  746. return 0;
  747. e_event_descs:
  748. kfree(event_descs);
  749. e_event_attrs:
  750. kfree(events);
  751. e_event_data:
  752. vfree(event_data);
  753. e_free:
  754. kmem_cache_free(hv_page_cache, page);
  755. e_out:
  756. *events_ = NULL;
  757. *event_descs_ = NULL;
  758. *event_long_descs_ = NULL;
  759. return ret;
  760. }
  761. static ssize_t catalog_read(struct file *filp, struct kobject *kobj,
  762. struct bin_attribute *bin_attr, char *buf,
  763. loff_t offset, size_t count)
  764. {
  765. unsigned long hret;
  766. ssize_t ret = 0;
  767. size_t catalog_len = 0, catalog_page_len = 0;
  768. loff_t page_offset = 0;
  769. loff_t offset_in_page;
  770. size_t copy_len;
  771. uint64_t catalog_version_num = 0;
  772. void *page = kmem_cache_alloc(hv_page_cache, GFP_USER);
  773. struct hv_24x7_catalog_page_0 *page_0 = page;
  774. if (!page)
  775. return -ENOMEM;
  776. hret = h_get_24x7_catalog_page(page, 0, 0);
  777. if (hret) {
  778. ret = -EIO;
  779. goto e_free;
  780. }
  781. catalog_version_num = be64_to_cpu(page_0->version);
  782. catalog_page_len = be32_to_cpu(page_0->length);
  783. catalog_len = catalog_page_len * 4096;
  784. page_offset = offset / 4096;
  785. offset_in_page = offset % 4096;
  786. if (page_offset >= catalog_page_len)
  787. goto e_free;
  788. if (page_offset != 0) {
  789. hret = h_get_24x7_catalog_page(page, catalog_version_num,
  790. page_offset);
  791. if (hret) {
  792. ret = -EIO;
  793. goto e_free;
  794. }
  795. }
  796. copy_len = 4096 - offset_in_page;
  797. if (copy_len > count)
  798. copy_len = count;
  799. memcpy(buf, page+offset_in_page, copy_len);
  800. ret = copy_len;
  801. e_free:
  802. if (hret)
  803. pr_err("h_get_24x7_catalog_page(ver=%lld, page=%lld) failed:"
  804. " rc=%ld\n",
  805. catalog_version_num, page_offset, hret);
  806. kmem_cache_free(hv_page_cache, page);
  807. pr_devel("catalog_read: offset=%lld(%lld) count=%zu "
  808. "catalog_len=%zu(%zu) => %zd\n", offset, page_offset,
  809. count, catalog_len, catalog_page_len, ret);
  810. return ret;
  811. }
  812. #define PAGE_0_ATTR(_name, _fmt, _expr) \
  813. static ssize_t _name##_show(struct device *dev, \
  814. struct device_attribute *dev_attr, \
  815. char *buf) \
  816. { \
  817. unsigned long hret; \
  818. ssize_t ret = 0; \
  819. void *page = kmem_cache_alloc(hv_page_cache, GFP_USER); \
  820. struct hv_24x7_catalog_page_0 *page_0 = page; \
  821. if (!page) \
  822. return -ENOMEM; \
  823. hret = h_get_24x7_catalog_page(page, 0, 0); \
  824. if (hret) { \
  825. ret = -EIO; \
  826. goto e_free; \
  827. } \
  828. ret = sprintf(buf, _fmt, _expr); \
  829. e_free: \
  830. kmem_cache_free(hv_page_cache, page); \
  831. return ret; \
  832. } \
  833. static DEVICE_ATTR_RO(_name)
  834. PAGE_0_ATTR(catalog_version, "%lld\n",
  835. (unsigned long long)be64_to_cpu(page_0->version));
  836. PAGE_0_ATTR(catalog_len, "%lld\n",
  837. (unsigned long long)be32_to_cpu(page_0->length) * 4096);
  838. static BIN_ATTR_RO(catalog, 0/* real length varies */);
  839. static struct bin_attribute *if_bin_attrs[] = {
  840. &bin_attr_catalog,
  841. NULL,
  842. };
  843. static struct attribute *if_attrs[] = {
  844. &dev_attr_catalog_len.attr,
  845. &dev_attr_catalog_version.attr,
  846. NULL,
  847. };
  848. static struct attribute_group if_group = {
  849. .name = "interface",
  850. .bin_attrs = if_bin_attrs,
  851. .attrs = if_attrs,
  852. };
  853. static const struct attribute_group *attr_groups[] = {
  854. &format_group,
  855. &event_group,
  856. &event_desc_group,
  857. &event_long_desc_group,
  858. &if_group,
  859. NULL,
  860. };
  861. static void log_24x7_hcall(struct hv_24x7_request_buffer *request_buffer,
  862. struct hv_24x7_data_result_buffer *result_buffer,
  863. unsigned long ret)
  864. {
  865. struct hv_24x7_request *req;
  866. req = &request_buffer->requests[0];
  867. pr_notice_ratelimited("hcall failed: [%d %#x %#x %d] => "
  868. "ret 0x%lx (%ld) detail=0x%x failing ix=%x\n",
  869. req->performance_domain, req->data_offset,
  870. req->starting_ix, req->starting_lpar_ix, ret, ret,
  871. result_buffer->detailed_rc,
  872. result_buffer->failing_request_ix);
  873. }
  874. /*
  875. * Start the process for a new H_GET_24x7_DATA hcall.
  876. */
  877. static void init_24x7_request(struct hv_24x7_request_buffer *request_buffer,
  878. struct hv_24x7_data_result_buffer *result_buffer)
  879. {
  880. memset(request_buffer, 0, 4096);
  881. memset(result_buffer, 0, 4096);
  882. request_buffer->interface_version = HV_24X7_IF_VERSION_CURRENT;
  883. /* memset above set request_buffer->num_requests to 0 */
  884. }
  885. /*
  886. * Commit (i.e perform) the H_GET_24x7_DATA hcall using the data collected
  887. * by 'init_24x7_request()' and 'add_event_to_24x7_request()'.
  888. */
  889. static int make_24x7_request(struct hv_24x7_request_buffer *request_buffer,
  890. struct hv_24x7_data_result_buffer *result_buffer)
  891. {
  892. unsigned long ret;
  893. /*
  894. * NOTE: Due to variable number of array elements in request and
  895. * result buffer(s), sizeof() is not reliable. Use the actual
  896. * allocated buffer size, H24x7_DATA_BUFFER_SIZE.
  897. */
  898. ret = plpar_hcall_norets(H_GET_24X7_DATA,
  899. virt_to_phys(request_buffer), H24x7_DATA_BUFFER_SIZE,
  900. virt_to_phys(result_buffer), H24x7_DATA_BUFFER_SIZE);
  901. if (ret)
  902. log_24x7_hcall(request_buffer, result_buffer, ret);
  903. return ret;
  904. }
  905. /*
  906. * Add the given @event to the next slot in the 24x7 request_buffer.
  907. *
  908. * Note that H_GET_24X7_DATA hcall allows reading several counters'
  909. * values in a single HCALL. We expect the caller to add events to the
  910. * request buffer one by one, make the HCALL and process the results.
  911. */
  912. static int add_event_to_24x7_request(struct perf_event *event,
  913. struct hv_24x7_request_buffer *request_buffer)
  914. {
  915. u16 idx;
  916. int i;
  917. struct hv_24x7_request *req;
  918. if (request_buffer->num_requests > 254) {
  919. pr_devel("Too many requests for 24x7 HCALL %d\n",
  920. request_buffer->num_requests);
  921. return -EINVAL;
  922. }
  923. if (is_physical_domain(event_get_domain(event)))
  924. idx = event_get_core(event);
  925. else
  926. idx = event_get_vcpu(event);
  927. i = request_buffer->num_requests++;
  928. req = &request_buffer->requests[i];
  929. req->performance_domain = event_get_domain(event);
  930. req->data_size = cpu_to_be16(8);
  931. req->data_offset = cpu_to_be32(event_get_offset(event));
  932. req->starting_lpar_ix = cpu_to_be16(event_get_lpar(event)),
  933. req->max_num_lpars = cpu_to_be16(1);
  934. req->starting_ix = cpu_to_be16(idx);
  935. req->max_ix = cpu_to_be16(1);
  936. return 0;
  937. }
  938. static unsigned long single_24x7_request(struct perf_event *event, u64 *count)
  939. {
  940. unsigned long ret;
  941. struct hv_24x7_request_buffer *request_buffer;
  942. struct hv_24x7_data_result_buffer *result_buffer;
  943. BUILD_BUG_ON(sizeof(*request_buffer) > 4096);
  944. BUILD_BUG_ON(sizeof(*result_buffer) > 4096);
  945. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  946. result_buffer = (void *)get_cpu_var(hv_24x7_resb);
  947. init_24x7_request(request_buffer, result_buffer);
  948. ret = add_event_to_24x7_request(event, request_buffer);
  949. if (ret)
  950. goto out;
  951. ret = make_24x7_request(request_buffer, result_buffer);
  952. if (ret) {
  953. log_24x7_hcall(request_buffer, result_buffer, ret);
  954. goto out;
  955. }
  956. /* process result from hcall */
  957. *count = be64_to_cpu(result_buffer->results[0].elements[0].element_data[0]);
  958. out:
  959. put_cpu_var(hv_24x7_reqb);
  960. put_cpu_var(hv_24x7_resb);
  961. return ret;
  962. }
  963. static int h_24x7_event_init(struct perf_event *event)
  964. {
  965. struct hv_perf_caps caps;
  966. unsigned domain;
  967. unsigned long hret;
  968. u64 ct;
  969. /* Not our event */
  970. if (event->attr.type != event->pmu->type)
  971. return -ENOENT;
  972. /* Unused areas must be 0 */
  973. if (event_get_reserved1(event) ||
  974. event_get_reserved2(event) ||
  975. event_get_reserved3(event)) {
  976. pr_devel("reserved set when forbidden 0x%llx(0x%llx) 0x%llx(0x%llx) 0x%llx(0x%llx)\n",
  977. event->attr.config,
  978. event_get_reserved1(event),
  979. event->attr.config1,
  980. event_get_reserved2(event),
  981. event->attr.config2,
  982. event_get_reserved3(event));
  983. return -EINVAL;
  984. }
  985. /* unsupported modes and filters */
  986. if (event->attr.exclude_user ||
  987. event->attr.exclude_kernel ||
  988. event->attr.exclude_hv ||
  989. event->attr.exclude_idle ||
  990. event->attr.exclude_host ||
  991. event->attr.exclude_guest)
  992. return -EINVAL;
  993. /* no branch sampling */
  994. if (has_branch_stack(event))
  995. return -EOPNOTSUPP;
  996. /* offset must be 8 byte aligned */
  997. if (event_get_offset(event) % 8) {
  998. pr_devel("bad alignment\n");
  999. return -EINVAL;
  1000. }
  1001. /* Domains above 6 are invalid */
  1002. domain = event_get_domain(event);
  1003. if (domain > 6) {
  1004. pr_devel("invalid domain %d\n", domain);
  1005. return -EINVAL;
  1006. }
  1007. hret = hv_perf_caps_get(&caps);
  1008. if (hret) {
  1009. pr_devel("could not get capabilities: rc=%ld\n", hret);
  1010. return -EIO;
  1011. }
  1012. /* Physical domains & other lpars require extra capabilities */
  1013. if (!caps.collect_privileged && (is_physical_domain(domain) ||
  1014. (event_get_lpar(event) != event_get_lpar_max()))) {
  1015. pr_devel("hv permissions disallow: is_physical_domain:%d, lpar=0x%llx\n",
  1016. is_physical_domain(domain),
  1017. event_get_lpar(event));
  1018. return -EACCES;
  1019. }
  1020. /* see if the event complains */
  1021. if (single_24x7_request(event, &ct)) {
  1022. pr_devel("test hcall failed\n");
  1023. return -EIO;
  1024. }
  1025. return 0;
  1026. }
  1027. static u64 h_24x7_get_value(struct perf_event *event)
  1028. {
  1029. unsigned long ret;
  1030. u64 ct;
  1031. ret = single_24x7_request(event, &ct);
  1032. if (ret)
  1033. /* We checked this in event init, shouldn't fail here... */
  1034. return 0;
  1035. return ct;
  1036. }
  1037. static void update_event_count(struct perf_event *event, u64 now)
  1038. {
  1039. s64 prev;
  1040. prev = local64_xchg(&event->hw.prev_count, now);
  1041. local64_add(now - prev, &event->count);
  1042. }
  1043. static void h_24x7_event_read(struct perf_event *event)
  1044. {
  1045. u64 now;
  1046. struct hv_24x7_request_buffer *request_buffer;
  1047. struct hv_24x7_hw *h24x7hw;
  1048. int txn_flags;
  1049. txn_flags = __this_cpu_read(hv_24x7_txn_flags);
  1050. /*
  1051. * If in a READ transaction, add this counter to the list of
  1052. * counters to read during the next HCALL (i.e commit_txn()).
  1053. * If not in a READ transaction, go ahead and make the HCALL
  1054. * to read this counter by itself.
  1055. */
  1056. if (txn_flags & PERF_PMU_TXN_READ) {
  1057. int i;
  1058. int ret;
  1059. if (__this_cpu_read(hv_24x7_txn_err))
  1060. return;
  1061. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  1062. ret = add_event_to_24x7_request(event, request_buffer);
  1063. if (ret) {
  1064. __this_cpu_write(hv_24x7_txn_err, ret);
  1065. } else {
  1066. /*
  1067. * Assoicate the event with the HCALL request index,
  1068. * so ->commit_txn() can quickly find/update count.
  1069. */
  1070. i = request_buffer->num_requests - 1;
  1071. h24x7hw = &get_cpu_var(hv_24x7_hw);
  1072. h24x7hw->events[i] = event;
  1073. put_cpu_var(h24x7hw);
  1074. }
  1075. put_cpu_var(hv_24x7_reqb);
  1076. } else {
  1077. now = h_24x7_get_value(event);
  1078. update_event_count(event, now);
  1079. }
  1080. }
  1081. static void h_24x7_event_start(struct perf_event *event, int flags)
  1082. {
  1083. if (flags & PERF_EF_RELOAD)
  1084. local64_set(&event->hw.prev_count, h_24x7_get_value(event));
  1085. }
  1086. static void h_24x7_event_stop(struct perf_event *event, int flags)
  1087. {
  1088. h_24x7_event_read(event);
  1089. }
  1090. static int h_24x7_event_add(struct perf_event *event, int flags)
  1091. {
  1092. if (flags & PERF_EF_START)
  1093. h_24x7_event_start(event, flags);
  1094. return 0;
  1095. }
  1096. /*
  1097. * 24x7 counters only support READ transactions. They are
  1098. * always counting and dont need/support ADD transactions.
  1099. * Cache the flags, but otherwise ignore transactions that
  1100. * are not PERF_PMU_TXN_READ.
  1101. */
  1102. static void h_24x7_event_start_txn(struct pmu *pmu, unsigned int flags)
  1103. {
  1104. struct hv_24x7_request_buffer *request_buffer;
  1105. struct hv_24x7_data_result_buffer *result_buffer;
  1106. /* We should not be called if we are already in a txn */
  1107. WARN_ON_ONCE(__this_cpu_read(hv_24x7_txn_flags));
  1108. __this_cpu_write(hv_24x7_txn_flags, flags);
  1109. if (flags & ~PERF_PMU_TXN_READ)
  1110. return;
  1111. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  1112. result_buffer = (void *)get_cpu_var(hv_24x7_resb);
  1113. init_24x7_request(request_buffer, result_buffer);
  1114. put_cpu_var(hv_24x7_resb);
  1115. put_cpu_var(hv_24x7_reqb);
  1116. }
  1117. /*
  1118. * Clean up transaction state.
  1119. *
  1120. * NOTE: Ignore state of request and result buffers for now.
  1121. * We will initialize them during the next read/txn.
  1122. */
  1123. static void reset_txn(void)
  1124. {
  1125. __this_cpu_write(hv_24x7_txn_flags, 0);
  1126. __this_cpu_write(hv_24x7_txn_err, 0);
  1127. }
  1128. /*
  1129. * 24x7 counters only support READ transactions. They are always counting
  1130. * and dont need/support ADD transactions. Clear ->txn_flags but otherwise
  1131. * ignore transactions that are not of type PERF_PMU_TXN_READ.
  1132. *
  1133. * For READ transactions, submit all pending 24x7 requests (i.e requests
  1134. * that were queued by h_24x7_event_read()), to the hypervisor and update
  1135. * the event counts.
  1136. */
  1137. static int h_24x7_event_commit_txn(struct pmu *pmu)
  1138. {
  1139. struct hv_24x7_request_buffer *request_buffer;
  1140. struct hv_24x7_data_result_buffer *result_buffer;
  1141. struct hv_24x7_result *resb;
  1142. struct perf_event *event;
  1143. u64 count;
  1144. int i, ret, txn_flags;
  1145. struct hv_24x7_hw *h24x7hw;
  1146. txn_flags = __this_cpu_read(hv_24x7_txn_flags);
  1147. WARN_ON_ONCE(!txn_flags);
  1148. ret = 0;
  1149. if (txn_flags & ~PERF_PMU_TXN_READ)
  1150. goto out;
  1151. ret = __this_cpu_read(hv_24x7_txn_err);
  1152. if (ret)
  1153. goto out;
  1154. request_buffer = (void *)get_cpu_var(hv_24x7_reqb);
  1155. result_buffer = (void *)get_cpu_var(hv_24x7_resb);
  1156. ret = make_24x7_request(request_buffer, result_buffer);
  1157. if (ret) {
  1158. log_24x7_hcall(request_buffer, result_buffer, ret);
  1159. goto put_reqb;
  1160. }
  1161. h24x7hw = &get_cpu_var(hv_24x7_hw);
  1162. /* Update event counts from hcall */
  1163. for (i = 0; i < request_buffer->num_requests; i++) {
  1164. resb = &result_buffer->results[i];
  1165. count = be64_to_cpu(resb->elements[0].element_data[0]);
  1166. event = h24x7hw->events[i];
  1167. h24x7hw->events[i] = NULL;
  1168. update_event_count(event, count);
  1169. }
  1170. put_cpu_var(hv_24x7_hw);
  1171. put_reqb:
  1172. put_cpu_var(hv_24x7_resb);
  1173. put_cpu_var(hv_24x7_reqb);
  1174. out:
  1175. reset_txn();
  1176. return ret;
  1177. }
  1178. /*
  1179. * 24x7 counters only support READ transactions. They are always counting
  1180. * and dont need/support ADD transactions. However, regardless of type
  1181. * of transaction, all we need to do is cleanup, so we don't have to check
  1182. * the type of transaction.
  1183. */
  1184. static void h_24x7_event_cancel_txn(struct pmu *pmu)
  1185. {
  1186. WARN_ON_ONCE(!__this_cpu_read(hv_24x7_txn_flags));
  1187. reset_txn();
  1188. }
  1189. static struct pmu h_24x7_pmu = {
  1190. .task_ctx_nr = perf_invalid_context,
  1191. .name = "hv_24x7",
  1192. .attr_groups = attr_groups,
  1193. .event_init = h_24x7_event_init,
  1194. .add = h_24x7_event_add,
  1195. .del = h_24x7_event_stop,
  1196. .start = h_24x7_event_start,
  1197. .stop = h_24x7_event_stop,
  1198. .read = h_24x7_event_read,
  1199. .start_txn = h_24x7_event_start_txn,
  1200. .commit_txn = h_24x7_event_commit_txn,
  1201. .cancel_txn = h_24x7_event_cancel_txn,
  1202. };
  1203. static int hv_24x7_init(void)
  1204. {
  1205. int r;
  1206. unsigned long hret;
  1207. struct hv_perf_caps caps;
  1208. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  1209. pr_debug("not a virtualized system, not enabling\n");
  1210. return -ENODEV;
  1211. }
  1212. hret = hv_perf_caps_get(&caps);
  1213. if (hret) {
  1214. pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
  1215. hret);
  1216. return -ENODEV;
  1217. }
  1218. hv_page_cache = kmem_cache_create("hv-page-4096", 4096, 4096, 0, NULL);
  1219. if (!hv_page_cache)
  1220. return -ENOMEM;
  1221. /* sampling not supported */
  1222. h_24x7_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  1223. r = create_events_from_catalog(&event_group.attrs,
  1224. &event_desc_group.attrs,
  1225. &event_long_desc_group.attrs);
  1226. if (r)
  1227. return r;
  1228. r = perf_pmu_register(&h_24x7_pmu, h_24x7_pmu.name, -1);
  1229. if (r)
  1230. return r;
  1231. return 0;
  1232. }
  1233. device_initcall(hv_24x7_init);