hv-24x7.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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/module.h>
  15. #include <linux/slab.h>
  16. #include <asm/firmware.h>
  17. #include <asm/hvcall.h>
  18. #include <asm/io.h>
  19. #include "hv-24x7.h"
  20. #include "hv-24x7-catalog.h"
  21. #include "hv-common.h"
  22. /*
  23. * TODO: Merging events:
  24. * - Think of the hcall as an interface to a 4d array of counters:
  25. * - x = domains
  26. * - y = indexes in the domain (core, chip, vcpu, node, etc)
  27. * - z = offset into the counter space
  28. * - w = lpars (guest vms, "logical partitions")
  29. * - A single request is: x,y,y_last,z,z_last,w,w_last
  30. * - this means we can retrieve a rectangle of counters in y,z for a single x.
  31. *
  32. * - Things to consider (ignoring w):
  33. * - input cost_per_request = 16
  34. * - output cost_per_result(ys,zs) = 8 + 8 * ys + ys * zs
  35. * - limited number of requests per hcall (must fit into 4K bytes)
  36. * - 4k = 16 [buffer header] - 16 [request size] * request_count
  37. * - 255 requests per hcall
  38. * - sometimes it will be more efficient to read extra data and discard
  39. */
  40. /*
  41. * Example usage:
  42. * perf stat -e 'hv_24x7/domain=2,offset=8,starting_index=0,lpar=0xffffffff/'
  43. */
  44. /* u3 0-6, one of HV_24X7_PERF_DOMAIN */
  45. EVENT_DEFINE_RANGE_FORMAT(domain, config, 0, 3);
  46. /* u16 */
  47. EVENT_DEFINE_RANGE_FORMAT(starting_index, config, 16, 31);
  48. /* u32, see "data_offset" */
  49. EVENT_DEFINE_RANGE_FORMAT(offset, config, 32, 63);
  50. /* u16 */
  51. EVENT_DEFINE_RANGE_FORMAT(lpar, config1, 0, 15);
  52. EVENT_DEFINE_RANGE(reserved1, config, 4, 15);
  53. EVENT_DEFINE_RANGE(reserved2, config1, 16, 63);
  54. EVENT_DEFINE_RANGE(reserved3, config2, 0, 63);
  55. static struct attribute *format_attrs[] = {
  56. &format_attr_domain.attr,
  57. &format_attr_offset.attr,
  58. &format_attr_starting_index.attr,
  59. &format_attr_lpar.attr,
  60. NULL,
  61. };
  62. static struct attribute_group format_group = {
  63. .name = "format",
  64. .attrs = format_attrs,
  65. };
  66. static struct kmem_cache *hv_page_cache;
  67. static unsigned long h_get_24x7_catalog_page_(unsigned long phys_4096,
  68. unsigned long version,
  69. unsigned long index)
  70. {
  71. pr_devel("h_get_24x7_catalog_page(0x%lx, %lu, %lu)",
  72. phys_4096,
  73. version,
  74. index);
  75. WARN_ON(!IS_ALIGNED(phys_4096, 4096));
  76. return plpar_hcall_norets(H_GET_24X7_CATALOG_PAGE,
  77. phys_4096,
  78. version,
  79. index);
  80. }
  81. static unsigned long h_get_24x7_catalog_page(char page[],
  82. u64 version, u32 index)
  83. {
  84. return h_get_24x7_catalog_page_(virt_to_phys(page),
  85. version, index);
  86. }
  87. static ssize_t catalog_read(struct file *filp, struct kobject *kobj,
  88. struct bin_attribute *bin_attr, char *buf,
  89. loff_t offset, size_t count)
  90. {
  91. unsigned long hret;
  92. ssize_t ret = 0;
  93. size_t catalog_len = 0, catalog_page_len = 0;
  94. loff_t page_offset = 0;
  95. loff_t offset_in_page;
  96. size_t copy_len;
  97. uint64_t catalog_version_num = 0;
  98. void *page = kmem_cache_alloc(hv_page_cache, GFP_USER);
  99. struct hv_24x7_catalog_page_0 *page_0 = page;
  100. if (!page)
  101. return -ENOMEM;
  102. hret = h_get_24x7_catalog_page(page, 0, 0);
  103. if (hret) {
  104. ret = -EIO;
  105. goto e_free;
  106. }
  107. catalog_version_num = be64_to_cpu(page_0->version);
  108. catalog_page_len = be32_to_cpu(page_0->length);
  109. catalog_len = catalog_page_len * 4096;
  110. page_offset = offset / 4096;
  111. offset_in_page = offset % 4096;
  112. if (page_offset >= catalog_page_len)
  113. goto e_free;
  114. if (page_offset != 0) {
  115. hret = h_get_24x7_catalog_page(page, catalog_version_num,
  116. page_offset);
  117. if (hret) {
  118. ret = -EIO;
  119. goto e_free;
  120. }
  121. }
  122. copy_len = 4096 - offset_in_page;
  123. if (copy_len > count)
  124. copy_len = count;
  125. memcpy(buf, page+offset_in_page, copy_len);
  126. ret = copy_len;
  127. e_free:
  128. if (hret)
  129. pr_err("h_get_24x7_catalog_page(ver=%lld, page=%lld) failed:"
  130. " rc=%ld\n",
  131. catalog_version_num, page_offset, hret);
  132. kmem_cache_free(hv_page_cache, page);
  133. pr_devel("catalog_read: offset=%lld(%lld) count=%zu "
  134. "catalog_len=%zu(%zu) => %zd\n", offset, page_offset,
  135. count, catalog_len, catalog_page_len, ret);
  136. return ret;
  137. }
  138. #define PAGE_0_ATTR(_name, _fmt, _expr) \
  139. static ssize_t _name##_show(struct device *dev, \
  140. struct device_attribute *dev_attr, \
  141. char *buf) \
  142. { \
  143. unsigned long hret; \
  144. ssize_t ret = 0; \
  145. void *page = kmem_cache_alloc(hv_page_cache, GFP_USER); \
  146. struct hv_24x7_catalog_page_0 *page_0 = page; \
  147. if (!page) \
  148. return -ENOMEM; \
  149. hret = h_get_24x7_catalog_page(page, 0, 0); \
  150. if (hret) { \
  151. ret = -EIO; \
  152. goto e_free; \
  153. } \
  154. ret = sprintf(buf, _fmt, _expr); \
  155. e_free: \
  156. kfree(page); \
  157. return ret; \
  158. } \
  159. static DEVICE_ATTR_RO(_name)
  160. PAGE_0_ATTR(catalog_version, "%lld\n",
  161. (unsigned long long)be64_to_cpu(page_0->version));
  162. PAGE_0_ATTR(catalog_len, "%lld\n",
  163. (unsigned long long)be32_to_cpu(page_0->length) * 4096);
  164. static BIN_ATTR_RO(catalog, 0/* real length varies */);
  165. static struct bin_attribute *if_bin_attrs[] = {
  166. &bin_attr_catalog,
  167. NULL,
  168. };
  169. static struct attribute *if_attrs[] = {
  170. &dev_attr_catalog_len.attr,
  171. &dev_attr_catalog_version.attr,
  172. NULL,
  173. };
  174. static struct attribute_group if_group = {
  175. .name = "interface",
  176. .bin_attrs = if_bin_attrs,
  177. .attrs = if_attrs,
  178. };
  179. static const struct attribute_group *attr_groups[] = {
  180. &format_group,
  181. &if_group,
  182. NULL,
  183. };
  184. static bool is_physical_domain(int domain)
  185. {
  186. return domain == HV_24X7_PERF_DOMAIN_PHYSICAL_CHIP ||
  187. domain == HV_24X7_PERF_DOMAIN_PHYSICAL_CORE;
  188. }
  189. static unsigned long single_24x7_request(u8 domain, u32 offset, u16 ix,
  190. u16 lpar, u64 *res,
  191. bool success_expected)
  192. {
  193. unsigned long ret = -ENOMEM;
  194. /*
  195. * request_buffer and result_buffer are not required to be 4k aligned,
  196. * but are not allowed to cross any 4k boundary. Aligning them to 4k is
  197. * the simplest way to ensure that.
  198. */
  199. struct reqb {
  200. struct hv_24x7_request_buffer buf;
  201. struct hv_24x7_request req;
  202. } __packed *request_buffer;
  203. struct {
  204. struct hv_24x7_data_result_buffer buf;
  205. struct hv_24x7_result res;
  206. struct hv_24x7_result_element elem;
  207. __be64 result;
  208. } __packed *result_buffer;
  209. BUILD_BUG_ON(sizeof(*request_buffer) > 4096);
  210. BUILD_BUG_ON(sizeof(*result_buffer) > 4096);
  211. request_buffer = kmem_cache_zalloc(hv_page_cache, GFP_USER);
  212. if (!request_buffer)
  213. goto out;
  214. result_buffer = kmem_cache_zalloc(hv_page_cache, GFP_USER);
  215. if (!result_buffer)
  216. goto out_free_request_buffer;
  217. *request_buffer = (struct reqb) {
  218. .buf = {
  219. .interface_version = HV_24X7_IF_VERSION_CURRENT,
  220. .num_requests = 1,
  221. },
  222. .req = {
  223. .performance_domain = domain,
  224. .data_size = cpu_to_be16(8),
  225. .data_offset = cpu_to_be32(offset),
  226. .starting_lpar_ix = cpu_to_be16(lpar),
  227. .max_num_lpars = cpu_to_be16(1),
  228. .starting_ix = cpu_to_be16(ix),
  229. .max_ix = cpu_to_be16(1),
  230. }
  231. };
  232. ret = plpar_hcall_norets(H_GET_24X7_DATA,
  233. virt_to_phys(request_buffer), sizeof(*request_buffer),
  234. virt_to_phys(result_buffer), sizeof(*result_buffer));
  235. if (ret) {
  236. if (success_expected)
  237. pr_err_ratelimited("hcall failed: %d %#x %#x %d => "
  238. "0x%lx (%ld) detail=0x%x failing ix=%x\n",
  239. domain, offset, ix, lpar, ret, ret,
  240. result_buffer->buf.detailed_rc,
  241. result_buffer->buf.failing_request_ix);
  242. goto out_free_result_buffer;
  243. }
  244. *res = be64_to_cpu(result_buffer->result);
  245. out_free_result_buffer:
  246. kfree(result_buffer);
  247. out_free_request_buffer:
  248. kfree(request_buffer);
  249. out:
  250. return ret;
  251. }
  252. static unsigned long event_24x7_request(struct perf_event *event, u64 *res,
  253. bool success_expected)
  254. {
  255. return single_24x7_request(event_get_domain(event),
  256. event_get_offset(event),
  257. event_get_starting_index(event),
  258. event_get_lpar(event),
  259. res,
  260. success_expected);
  261. }
  262. static int h_24x7_event_init(struct perf_event *event)
  263. {
  264. struct hv_perf_caps caps;
  265. unsigned domain;
  266. unsigned long hret;
  267. u64 ct;
  268. /* Not our event */
  269. if (event->attr.type != event->pmu->type)
  270. return -ENOENT;
  271. /* Unused areas must be 0 */
  272. if (event_get_reserved1(event) ||
  273. event_get_reserved2(event) ||
  274. event_get_reserved3(event)) {
  275. pr_devel("reserved set when forbidden 0x%llx(0x%llx) 0x%llx(0x%llx) 0x%llx(0x%llx)\n",
  276. event->attr.config,
  277. event_get_reserved1(event),
  278. event->attr.config1,
  279. event_get_reserved2(event),
  280. event->attr.config2,
  281. event_get_reserved3(event));
  282. return -EINVAL;
  283. }
  284. /* unsupported modes and filters */
  285. if (event->attr.exclude_user ||
  286. event->attr.exclude_kernel ||
  287. event->attr.exclude_hv ||
  288. event->attr.exclude_idle ||
  289. event->attr.exclude_host ||
  290. event->attr.exclude_guest)
  291. return -EINVAL;
  292. /* no branch sampling */
  293. if (has_branch_stack(event))
  294. return -EOPNOTSUPP;
  295. /* offset must be 8 byte aligned */
  296. if (event_get_offset(event) % 8) {
  297. pr_devel("bad alignment\n");
  298. return -EINVAL;
  299. }
  300. /* Domains above 6 are invalid */
  301. domain = event_get_domain(event);
  302. if (domain > 6) {
  303. pr_devel("invalid domain %d\n", domain);
  304. return -EINVAL;
  305. }
  306. hret = hv_perf_caps_get(&caps);
  307. if (hret) {
  308. pr_devel("could not get capabilities: rc=%ld\n", hret);
  309. return -EIO;
  310. }
  311. /* PHYSICAL domains & other lpars require extra capabilities */
  312. if (!caps.collect_privileged && (is_physical_domain(domain) ||
  313. (event_get_lpar(event) != event_get_lpar_max()))) {
  314. pr_devel("hv permisions disallow: is_physical_domain:%d, lpar=0x%llx\n",
  315. is_physical_domain(domain),
  316. event_get_lpar(event));
  317. return -EACCES;
  318. }
  319. /* see if the event complains */
  320. if (event_24x7_request(event, &ct, false)) {
  321. pr_devel("test hcall failed\n");
  322. return -EIO;
  323. }
  324. return 0;
  325. }
  326. static u64 h_24x7_get_value(struct perf_event *event)
  327. {
  328. unsigned long ret;
  329. u64 ct;
  330. ret = event_24x7_request(event, &ct, true);
  331. if (ret)
  332. /* We checked this in event init, shouldn't fail here... */
  333. return 0;
  334. return ct;
  335. }
  336. static void h_24x7_event_update(struct perf_event *event)
  337. {
  338. s64 prev;
  339. u64 now;
  340. now = h_24x7_get_value(event);
  341. prev = local64_xchg(&event->hw.prev_count, now);
  342. local64_add(now - prev, &event->count);
  343. }
  344. static void h_24x7_event_start(struct perf_event *event, int flags)
  345. {
  346. if (flags & PERF_EF_RELOAD)
  347. local64_set(&event->hw.prev_count, h_24x7_get_value(event));
  348. }
  349. static void h_24x7_event_stop(struct perf_event *event, int flags)
  350. {
  351. h_24x7_event_update(event);
  352. }
  353. static int h_24x7_event_add(struct perf_event *event, int flags)
  354. {
  355. if (flags & PERF_EF_START)
  356. h_24x7_event_start(event, flags);
  357. return 0;
  358. }
  359. static int h_24x7_event_idx(struct perf_event *event)
  360. {
  361. return 0;
  362. }
  363. static struct pmu h_24x7_pmu = {
  364. .task_ctx_nr = perf_invalid_context,
  365. .name = "hv_24x7",
  366. .attr_groups = attr_groups,
  367. .event_init = h_24x7_event_init,
  368. .add = h_24x7_event_add,
  369. .del = h_24x7_event_stop,
  370. .start = h_24x7_event_start,
  371. .stop = h_24x7_event_stop,
  372. .read = h_24x7_event_update,
  373. .event_idx = h_24x7_event_idx,
  374. };
  375. static int hv_24x7_init(void)
  376. {
  377. int r;
  378. unsigned long hret;
  379. struct hv_perf_caps caps;
  380. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  381. pr_debug("not a virtualized system, not enabling\n");
  382. return -ENODEV;
  383. }
  384. hret = hv_perf_caps_get(&caps);
  385. if (hret) {
  386. pr_debug("could not obtain capabilities, not enabling, rc=%ld\n",
  387. hret);
  388. return -ENODEV;
  389. }
  390. hv_page_cache = kmem_cache_create("hv-page-4096", 4096, 4096, 0, NULL);
  391. if (!hv_page_cache)
  392. return -ENOMEM;
  393. /* sampling not supported */
  394. h_24x7_pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  395. r = perf_pmu_register(&h_24x7_pmu, h_24x7_pmu.name, -1);
  396. if (r)
  397. return r;
  398. return 0;
  399. }
  400. device_initcall(hv_24x7_init);