hv-24x7.c 44 KB

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