common.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**
  2. * @file common.c
  3. *
  4. * @remark Copyright 2004 Oprofile Authors
  5. * @remark Copyright 2010 ARM Ltd.
  6. * @remark Read the file COPYING
  7. *
  8. * @author Zwane Mwaikambo
  9. * @author Will Deacon [move to perf]
  10. */
  11. #include <linux/cpumask.h>
  12. #include <linux/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/mutex.h>
  16. #include <linux/oprofile.h>
  17. #include <linux/perf_event.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <asm/stacktrace.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/perf_event.h>
  23. #include <asm/ptrace.h>
  24. #ifdef CONFIG_HW_PERF_EVENTS
  25. /*
  26. * Per performance monitor configuration as set via oprofilefs.
  27. */
  28. struct op_counter_config {
  29. unsigned long count;
  30. unsigned long enabled;
  31. unsigned long event;
  32. unsigned long unit_mask;
  33. unsigned long kernel;
  34. unsigned long user;
  35. struct perf_event_attr attr;
  36. };
  37. static int oprofile_perf_enabled;
  38. static DEFINE_MUTEX(oprofile_perf_mutex);
  39. static struct op_counter_config *counter_config;
  40. static struct perf_event **perf_events[nr_cpumask_bits];
  41. static int num_counters;
  42. /*
  43. * Overflow callback for oprofile.
  44. */
  45. static void op_overflow_handler(struct perf_event *event, int unused,
  46. struct perf_sample_data *data, struct pt_regs *regs)
  47. {
  48. int id;
  49. u32 cpu = smp_processor_id();
  50. for (id = 0; id < num_counters; ++id)
  51. if (perf_events[cpu][id] == event)
  52. break;
  53. if (id != num_counters)
  54. oprofile_add_sample(regs, id);
  55. else
  56. pr_warning("oprofile: ignoring spurious overflow "
  57. "on cpu %u\n", cpu);
  58. }
  59. /*
  60. * Called by oprofile_perf_setup to create perf attributes to mirror the oprofile
  61. * settings in counter_config. Attributes are created as `pinned' events and
  62. * so are permanently scheduled on the PMU.
  63. */
  64. static void op_perf_setup(void)
  65. {
  66. int i;
  67. u32 size = sizeof(struct perf_event_attr);
  68. struct perf_event_attr *attr;
  69. for (i = 0; i < num_counters; ++i) {
  70. attr = &counter_config[i].attr;
  71. memset(attr, 0, size);
  72. attr->type = PERF_TYPE_RAW;
  73. attr->size = size;
  74. attr->config = counter_config[i].event;
  75. attr->sample_period = counter_config[i].count;
  76. attr->pinned = 1;
  77. }
  78. }
  79. static int op_create_counter(int cpu, int event)
  80. {
  81. int ret = 0;
  82. struct perf_event *pevent;
  83. if (!counter_config[event].enabled || (perf_events[cpu][event] != NULL))
  84. return ret;
  85. pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
  86. cpu, -1,
  87. op_overflow_handler);
  88. if (IS_ERR(pevent)) {
  89. ret = PTR_ERR(pevent);
  90. } else if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
  91. pr_warning("oprofile: failed to enable event %d "
  92. "on CPU %d\n", event, cpu);
  93. ret = -EBUSY;
  94. } else {
  95. perf_events[cpu][event] = pevent;
  96. }
  97. return ret;
  98. }
  99. static void op_destroy_counter(int cpu, int event)
  100. {
  101. struct perf_event *pevent = perf_events[cpu][event];
  102. if (pevent) {
  103. perf_event_release_kernel(pevent);
  104. perf_events[cpu][event] = NULL;
  105. }
  106. }
  107. /*
  108. * Called by oprofile_perf_start to create active perf events based on the
  109. * perviously configured attributes.
  110. */
  111. static int op_perf_start(void)
  112. {
  113. int cpu, event, ret = 0;
  114. for_each_online_cpu(cpu) {
  115. for (event = 0; event < num_counters; ++event) {
  116. ret = op_create_counter(cpu, event);
  117. if (ret)
  118. goto out;
  119. }
  120. }
  121. out:
  122. return ret;
  123. }
  124. /*
  125. * Called by oprofile_perf_stop at the end of a profiling run.
  126. */
  127. static void op_perf_stop(void)
  128. {
  129. int cpu, event;
  130. for_each_online_cpu(cpu)
  131. for (event = 0; event < num_counters; ++event)
  132. op_destroy_counter(cpu, event);
  133. }
  134. char *op_name_from_perf_id(void)
  135. {
  136. enum arm_perf_pmu_ids id = armpmu_get_pmu_id();
  137. switch (id) {
  138. case ARM_PERF_PMU_ID_XSCALE1:
  139. return "arm/xscale1";
  140. case ARM_PERF_PMU_ID_XSCALE2:
  141. return "arm/xscale2";
  142. case ARM_PERF_PMU_ID_V6:
  143. return "arm/armv6";
  144. case ARM_PERF_PMU_ID_V6MP:
  145. return "arm/mpcore";
  146. case ARM_PERF_PMU_ID_CA8:
  147. return "arm/armv7";
  148. case ARM_PERF_PMU_ID_CA9:
  149. return "arm/armv7-ca9";
  150. default:
  151. return NULL;
  152. }
  153. }
  154. static int oprofile_perf_create_files(struct super_block *sb, struct dentry *root)
  155. {
  156. unsigned int i;
  157. for (i = 0; i < num_counters; i++) {
  158. struct dentry *dir;
  159. char buf[4];
  160. snprintf(buf, sizeof buf, "%d", i);
  161. dir = oprofilefs_mkdir(sb, root, buf);
  162. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  163. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  164. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  165. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  166. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  167. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  168. }
  169. return 0;
  170. }
  171. static int oprofile_perf_setup(void)
  172. {
  173. spin_lock(&oprofilefs_lock);
  174. op_perf_setup();
  175. spin_unlock(&oprofilefs_lock);
  176. return 0;
  177. }
  178. static int oprofile_perf_start(void)
  179. {
  180. int ret = -EBUSY;
  181. mutex_lock(&oprofile_perf_mutex);
  182. if (!oprofile_perf_enabled) {
  183. ret = 0;
  184. op_perf_start();
  185. oprofile_perf_enabled = 1;
  186. }
  187. mutex_unlock(&oprofile_perf_mutex);
  188. return ret;
  189. }
  190. static void oprofile_perf_stop(void)
  191. {
  192. mutex_lock(&oprofile_perf_mutex);
  193. if (oprofile_perf_enabled)
  194. op_perf_stop();
  195. oprofile_perf_enabled = 0;
  196. mutex_unlock(&oprofile_perf_mutex);
  197. }
  198. #ifdef CONFIG_PM
  199. static int oprofile_perf_suspend(struct platform_device *dev, pm_message_t state)
  200. {
  201. mutex_lock(&oprofile_perf_mutex);
  202. if (oprofile_perf_enabled)
  203. op_perf_stop();
  204. mutex_unlock(&oprofile_perf_mutex);
  205. return 0;
  206. }
  207. static int oprofile_perf_resume(struct platform_device *dev)
  208. {
  209. mutex_lock(&oprofile_perf_mutex);
  210. if (oprofile_perf_enabled && op_perf_start())
  211. oprofile_perf_enabled = 0;
  212. mutex_unlock(&oprofile_perf_mutex);
  213. return 0;
  214. }
  215. static struct platform_driver oprofile_driver = {
  216. .driver = {
  217. .name = "oprofile-perf",
  218. },
  219. .resume = oprofile_perf_resume,
  220. .suspend = oprofile_perf_suspend,
  221. };
  222. static struct platform_device *oprofile_pdev;
  223. static int __init init_driverfs(void)
  224. {
  225. int ret;
  226. ret = platform_driver_register(&oprofile_driver);
  227. if (ret)
  228. goto out;
  229. oprofile_pdev = platform_device_register_simple(
  230. oprofile_driver.driver.name, 0, NULL, 0);
  231. if (IS_ERR(oprofile_pdev)) {
  232. ret = PTR_ERR(oprofile_pdev);
  233. platform_driver_unregister(&oprofile_driver);
  234. }
  235. out:
  236. return ret;
  237. }
  238. static void __exit exit_driverfs(void)
  239. {
  240. platform_device_unregister(oprofile_pdev);
  241. platform_driver_unregister(&oprofile_driver);
  242. }
  243. #else
  244. static int __init init_driverfs(void) { return 0; }
  245. #define exit_driverfs() do { } while (0)
  246. #endif /* CONFIG_PM */
  247. static int report_trace(struct stackframe *frame, void *d)
  248. {
  249. unsigned int *depth = d;
  250. if (*depth) {
  251. oprofile_add_trace(frame->pc);
  252. (*depth)--;
  253. }
  254. return *depth == 0;
  255. }
  256. /*
  257. * The registers we're interested in are at the end of the variable
  258. * length saved register structure. The fp points at the end of this
  259. * structure so the address of this struct is:
  260. * (struct frame_tail *)(xxx->fp)-1
  261. */
  262. struct frame_tail {
  263. struct frame_tail *fp;
  264. unsigned long sp;
  265. unsigned long lr;
  266. } __attribute__((packed));
  267. static struct frame_tail* user_backtrace(struct frame_tail *tail)
  268. {
  269. struct frame_tail buftail[2];
  270. /* Also check accessibility of one struct frame_tail beyond */
  271. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  272. return NULL;
  273. if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
  274. return NULL;
  275. oprofile_add_trace(buftail[0].lr);
  276. /* frame pointers should strictly progress back up the stack
  277. * (towards higher addresses) */
  278. if (tail >= buftail[0].fp)
  279. return NULL;
  280. return buftail[0].fp-1;
  281. }
  282. static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  283. {
  284. struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  285. if (!user_mode(regs)) {
  286. struct stackframe frame;
  287. frame.fp = regs->ARM_fp;
  288. frame.sp = regs->ARM_sp;
  289. frame.lr = regs->ARM_lr;
  290. frame.pc = regs->ARM_pc;
  291. walk_stackframe(&frame, report_trace, &depth);
  292. return;
  293. }
  294. while (depth-- && tail && !((unsigned long) tail & 3))
  295. tail = user_backtrace(tail);
  296. }
  297. int __init oprofile_arch_init(struct oprofile_operations *ops)
  298. {
  299. int cpu, ret = 0;
  300. memset(&perf_events, 0, sizeof(perf_events));
  301. num_counters = perf_num_counters();
  302. if (num_counters <= 0) {
  303. pr_info("oprofile: no performance counters\n");
  304. ret = -ENODEV;
  305. goto out;
  306. }
  307. counter_config = kcalloc(num_counters,
  308. sizeof(struct op_counter_config), GFP_KERNEL);
  309. if (!counter_config) {
  310. pr_info("oprofile: failed to allocate %d "
  311. "counters\n", num_counters);
  312. ret = -ENOMEM;
  313. goto out;
  314. }
  315. ret = init_driverfs();
  316. if (ret)
  317. goto out;
  318. for_each_possible_cpu(cpu) {
  319. perf_events[cpu] = kcalloc(num_counters,
  320. sizeof(struct perf_event *), GFP_KERNEL);
  321. if (!perf_events[cpu]) {
  322. pr_info("oprofile: failed to allocate %d perf events "
  323. "for cpu %d\n", num_counters, cpu);
  324. ret = -ENOMEM;
  325. goto out;
  326. }
  327. }
  328. ops->backtrace = arm_backtrace;
  329. ops->create_files = oprofile_perf_create_files;
  330. ops->setup = oprofile_perf_setup;
  331. ops->start = oprofile_perf_start;
  332. ops->stop = oprofile_perf_stop;
  333. ops->shutdown = oprofile_perf_stop;
  334. ops->cpu_type = op_name_from_perf_id();
  335. if (!ops->cpu_type)
  336. ret = -ENODEV;
  337. else
  338. pr_info("oprofile: using %s\n", ops->cpu_type);
  339. out:
  340. if (ret) {
  341. for_each_possible_cpu(cpu)
  342. kfree(perf_events[cpu]);
  343. kfree(counter_config);
  344. }
  345. return ret;
  346. }
  347. void __exit oprofile_arch_exit(void)
  348. {
  349. int cpu, id;
  350. struct perf_event *event;
  351. for_each_possible_cpu(cpu) {
  352. for (id = 0; id < num_counters; ++id) {
  353. event = perf_events[cpu][id];
  354. if (event)
  355. perf_event_release_kernel(event);
  356. }
  357. kfree(perf_events[cpu]);
  358. }
  359. kfree(counter_config);
  360. exit_driverfs();
  361. }
  362. #else
  363. int __init oprofile_arch_init(struct oprofile_operations *ops)
  364. {
  365. pr_info("oprofile: hardware counters not available\n");
  366. return -ENODEV;
  367. }
  368. void __exit oprofile_arch_exit(void) {}
  369. #endif /* CONFIG_HW_PERF_EVENTS */