oprofile_perf.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2010 ARM Ltd.
  4. * Copyright 2012 Advanced Micro Devices, Inc., Robert Richter
  5. *
  6. * Perf-events backend for OProfile.
  7. */
  8. #include <linux/perf_event.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/oprofile.h>
  11. #include <linux/slab.h>
  12. /*
  13. * Per performance monitor configuration as set via oprofilefs.
  14. */
  15. struct op_counter_config {
  16. unsigned long count;
  17. unsigned long enabled;
  18. unsigned long event;
  19. unsigned long unit_mask;
  20. unsigned long kernel;
  21. unsigned long user;
  22. struct perf_event_attr attr;
  23. };
  24. static int oprofile_perf_enabled;
  25. static DEFINE_MUTEX(oprofile_perf_mutex);
  26. static struct op_counter_config *counter_config;
  27. static DEFINE_PER_CPU(struct perf_event **, perf_events);
  28. static int num_counters;
  29. /*
  30. * Overflow callback for oprofile.
  31. */
  32. static void op_overflow_handler(struct perf_event *event,
  33. struct perf_sample_data *data, struct pt_regs *regs)
  34. {
  35. int id;
  36. u32 cpu = smp_processor_id();
  37. for (id = 0; id < num_counters; ++id)
  38. if (per_cpu(perf_events, cpu)[id] == event)
  39. break;
  40. if (id != num_counters)
  41. oprofile_add_sample(regs, id);
  42. else
  43. pr_warn("oprofile: ignoring spurious overflow on cpu %u\n",
  44. cpu);
  45. }
  46. /*
  47. * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
  48. * settings in counter_config. Attributes are created as `pinned' events and
  49. * so are permanently scheduled on the PMU.
  50. */
  51. static void op_perf_setup(void)
  52. {
  53. int i;
  54. u32 size = sizeof(struct perf_event_attr);
  55. struct perf_event_attr *attr;
  56. for (i = 0; i < num_counters; ++i) {
  57. attr = &counter_config[i].attr;
  58. memset(attr, 0, size);
  59. attr->type = PERF_TYPE_RAW;
  60. attr->size = size;
  61. attr->config = counter_config[i].event;
  62. attr->sample_period = counter_config[i].count;
  63. attr->pinned = 1;
  64. }
  65. }
  66. static int op_create_counter(int cpu, int event)
  67. {
  68. struct perf_event *pevent;
  69. if (!counter_config[event].enabled || per_cpu(perf_events, cpu)[event])
  70. return 0;
  71. pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
  72. cpu, NULL,
  73. op_overflow_handler, NULL);
  74. if (IS_ERR(pevent))
  75. return PTR_ERR(pevent);
  76. if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
  77. perf_event_release_kernel(pevent);
  78. pr_warn("oprofile: failed to enable event %d on CPU %d\n",
  79. event, cpu);
  80. return -EBUSY;
  81. }
  82. per_cpu(perf_events, cpu)[event] = pevent;
  83. return 0;
  84. }
  85. static void op_destroy_counter(int cpu, int event)
  86. {
  87. struct perf_event *pevent = per_cpu(perf_events, cpu)[event];
  88. if (pevent) {
  89. perf_event_release_kernel(pevent);
  90. per_cpu(perf_events, cpu)[event] = NULL;
  91. }
  92. }
  93. /*
  94. * Called by oprofile_perf_start to create active perf events based on the
  95. * perviously configured attributes.
  96. */
  97. static int op_perf_start(void)
  98. {
  99. int cpu, event, ret = 0;
  100. for_each_online_cpu(cpu) {
  101. for (event = 0; event < num_counters; ++event) {
  102. ret = op_create_counter(cpu, event);
  103. if (ret)
  104. return ret;
  105. }
  106. }
  107. return ret;
  108. }
  109. /*
  110. * Called by oprofile_perf_stop at the end of a profiling run.
  111. */
  112. static void op_perf_stop(void)
  113. {
  114. int cpu, event;
  115. for_each_online_cpu(cpu)
  116. for (event = 0; event < num_counters; ++event)
  117. op_destroy_counter(cpu, event);
  118. }
  119. static int oprofile_perf_create_files(struct dentry *root)
  120. {
  121. unsigned int i;
  122. for (i = 0; i < num_counters; i++) {
  123. struct dentry *dir;
  124. char buf[4];
  125. snprintf(buf, sizeof buf, "%d", i);
  126. dir = oprofilefs_mkdir(root, buf);
  127. oprofilefs_create_ulong(dir, "enabled", &counter_config[i].enabled);
  128. oprofilefs_create_ulong(dir, "event", &counter_config[i].event);
  129. oprofilefs_create_ulong(dir, "count", &counter_config[i].count);
  130. oprofilefs_create_ulong(dir, "unit_mask", &counter_config[i].unit_mask);
  131. oprofilefs_create_ulong(dir, "kernel", &counter_config[i].kernel);
  132. oprofilefs_create_ulong(dir, "user", &counter_config[i].user);
  133. }
  134. return 0;
  135. }
  136. static int oprofile_perf_setup(void)
  137. {
  138. raw_spin_lock(&oprofilefs_lock);
  139. op_perf_setup();
  140. raw_spin_unlock(&oprofilefs_lock);
  141. return 0;
  142. }
  143. static int oprofile_perf_start(void)
  144. {
  145. int ret = -EBUSY;
  146. mutex_lock(&oprofile_perf_mutex);
  147. if (!oprofile_perf_enabled) {
  148. ret = 0;
  149. op_perf_start();
  150. oprofile_perf_enabled = 1;
  151. }
  152. mutex_unlock(&oprofile_perf_mutex);
  153. return ret;
  154. }
  155. static void oprofile_perf_stop(void)
  156. {
  157. mutex_lock(&oprofile_perf_mutex);
  158. if (oprofile_perf_enabled)
  159. op_perf_stop();
  160. oprofile_perf_enabled = 0;
  161. mutex_unlock(&oprofile_perf_mutex);
  162. }
  163. #ifdef CONFIG_PM
  164. static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state)
  165. {
  166. mutex_lock(&oprofile_perf_mutex);
  167. if (oprofile_perf_enabled)
  168. op_perf_stop();
  169. mutex_unlock(&oprofile_perf_mutex);
  170. return 0;
  171. }
  172. static int oprofile_perf_resume(struct platform_device *dev)
  173. {
  174. mutex_lock(&oprofile_perf_mutex);
  175. if (oprofile_perf_enabled && op_perf_start())
  176. oprofile_perf_enabled = 0;
  177. mutex_unlock(&oprofile_perf_mutex);
  178. return 0;
  179. }
  180. static struct platform_driver oprofile_driver = {
  181. .driver = {
  182. .name = "oprofile-perf",
  183. },
  184. .resume = oprofile_perf_resume,
  185. .suspend = oprofile_perf_suspend,
  186. };
  187. static struct platform_device *oprofile_pdev;
  188. static int __init init_driverfs(void)
  189. {
  190. int ret;
  191. ret = platform_driver_register(&oprofile_driver);
  192. if (ret)
  193. return ret;
  194. oprofile_pdev = platform_device_register_simple(
  195. oprofile_driver.driver.name, 0, NULL, 0);
  196. if (IS_ERR(oprofile_pdev)) {
  197. ret = PTR_ERR(oprofile_pdev);
  198. platform_driver_unregister(&oprofile_driver);
  199. }
  200. return ret;
  201. }
  202. static void exit_driverfs(void)
  203. {
  204. platform_device_unregister(oprofile_pdev);
  205. platform_driver_unregister(&oprofile_driver);
  206. }
  207. #else
  208. static inline int init_driverfs(void) { return 0; }
  209. static inline void exit_driverfs(void) { }
  210. #endif /* CONFIG_PM */
  211. void oprofile_perf_exit(void)
  212. {
  213. int cpu, id;
  214. struct perf_event *event;
  215. for_each_possible_cpu(cpu) {
  216. for (id = 0; id < num_counters; ++id) {
  217. event = per_cpu(perf_events, cpu)[id];
  218. if (event)
  219. perf_event_release_kernel(event);
  220. }
  221. kfree(per_cpu(perf_events, cpu));
  222. }
  223. kfree(counter_config);
  224. exit_driverfs();
  225. }
  226. int __init oprofile_perf_init(struct oprofile_operations *ops)
  227. {
  228. int cpu, ret = 0;
  229. ret = init_driverfs();
  230. if (ret)
  231. return ret;
  232. num_counters = perf_num_counters();
  233. if (num_counters <= 0) {
  234. pr_info("oprofile: no performance counters\n");
  235. ret = -ENODEV;
  236. goto out;
  237. }
  238. counter_config = kcalloc(num_counters,
  239. sizeof(struct op_counter_config), GFP_KERNEL);
  240. if (!counter_config) {
  241. pr_info("oprofile: failed to allocate %d "
  242. "counters\n", num_counters);
  243. ret = -ENOMEM;
  244. num_counters = 0;
  245. goto out;
  246. }
  247. for_each_possible_cpu(cpu) {
  248. per_cpu(perf_events, cpu) = kcalloc(num_counters,
  249. sizeof(struct perf_event *), GFP_KERNEL);
  250. if (!per_cpu(perf_events, cpu)) {
  251. pr_info("oprofile: failed to allocate %d perf events "
  252. "for cpu %d\n", num_counters, cpu);
  253. ret = -ENOMEM;
  254. goto out;
  255. }
  256. }
  257. ops->create_files = oprofile_perf_create_files;
  258. ops->setup = oprofile_perf_setup;
  259. ops->start = oprofile_perf_start;
  260. ops->stop = oprofile_perf_stop;
  261. ops->shutdown = oprofile_perf_stop;
  262. ops->cpu_type = op_name_from_perf_id();
  263. if (!ops->cpu_type)
  264. ret = -ENODEV;
  265. else
  266. pr_info("oprofile: using %s\n", ops->cpu_type);
  267. out:
  268. if (ret)
  269. oprofile_perf_exit();
  270. return ret;
  271. }