hv-24x7.c 38 KB

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