common.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 op_arm_enabled;
  38. static DEFINE_MUTEX(op_arm_mutex);
  39. static struct op_counter_config *counter_config;
  40. static struct perf_event **perf_events[nr_cpumask_bits];
  41. static int perf_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 < perf_num_counters; ++id)
  51. if (perf_events[cpu][id] == event)
  52. break;
  53. if (id != perf_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 op_arm_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 < perf_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. struct perf_event *pevent;
  82. if (!counter_config[event].enabled || perf_events[cpu][event])
  83. return 0;
  84. pevent = perf_event_create_kernel_counter(&counter_config[event].attr,
  85. cpu, -1,
  86. op_overflow_handler);
  87. if (IS_ERR(pevent))
  88. return PTR_ERR(pevent);
  89. if (pevent->state != PERF_EVENT_STATE_ACTIVE) {
  90. perf_event_release_kernel(pevent);
  91. pr_warning("oprofile: failed to enable event %d "
  92. "on CPU %d\n", event, cpu);
  93. return -EBUSY;
  94. }
  95. perf_events[cpu][event] = pevent;
  96. return 0;
  97. }
  98. static void op_destroy_counter(int cpu, int event)
  99. {
  100. struct perf_event *pevent = perf_events[cpu][event];
  101. if (pevent) {
  102. perf_event_release_kernel(pevent);
  103. perf_events[cpu][event] = NULL;
  104. }
  105. }
  106. /*
  107. * Called by op_arm_start to create active perf events based on the
  108. * perviously configured attributes.
  109. */
  110. static int op_perf_start(void)
  111. {
  112. int cpu, event, ret = 0;
  113. for_each_online_cpu(cpu) {
  114. for (event = 0; event < perf_num_counters; ++event) {
  115. ret = op_create_counter(cpu, event);
  116. if (ret)
  117. return ret;
  118. }
  119. }
  120. return ret;
  121. }
  122. /*
  123. * Called by op_arm_stop at the end of a profiling run.
  124. */
  125. static void op_perf_stop(void)
  126. {
  127. int cpu, event;
  128. for_each_online_cpu(cpu)
  129. for (event = 0; event < perf_num_counters; ++event)
  130. op_destroy_counter(cpu, event);
  131. }
  132. static char *op_name_from_perf_id(enum arm_perf_pmu_ids id)
  133. {
  134. switch (id) {
  135. case ARM_PERF_PMU_ID_XSCALE1:
  136. return "arm/xscale1";
  137. case ARM_PERF_PMU_ID_XSCALE2:
  138. return "arm/xscale2";
  139. case ARM_PERF_PMU_ID_V6:
  140. return "arm/armv6";
  141. case ARM_PERF_PMU_ID_V6MP:
  142. return "arm/mpcore";
  143. case ARM_PERF_PMU_ID_CA8:
  144. return "arm/armv7";
  145. case ARM_PERF_PMU_ID_CA9:
  146. return "arm/armv7-ca9";
  147. default:
  148. return NULL;
  149. }
  150. }
  151. static int op_arm_create_files(struct super_block *sb, struct dentry *root)
  152. {
  153. unsigned int i;
  154. for (i = 0; i < perf_num_counters; i++) {
  155. struct dentry *dir;
  156. char buf[4];
  157. snprintf(buf, sizeof buf, "%d", i);
  158. dir = oprofilefs_mkdir(sb, root, buf);
  159. oprofilefs_create_ulong(sb, dir, "enabled", &counter_config[i].enabled);
  160. oprofilefs_create_ulong(sb, dir, "event", &counter_config[i].event);
  161. oprofilefs_create_ulong(sb, dir, "count", &counter_config[i].count);
  162. oprofilefs_create_ulong(sb, dir, "unit_mask", &counter_config[i].unit_mask);
  163. oprofilefs_create_ulong(sb, dir, "kernel", &counter_config[i].kernel);
  164. oprofilefs_create_ulong(sb, dir, "user", &counter_config[i].user);
  165. }
  166. return 0;
  167. }
  168. static int op_arm_setup(void)
  169. {
  170. spin_lock(&oprofilefs_lock);
  171. op_perf_setup();
  172. spin_unlock(&oprofilefs_lock);
  173. return 0;
  174. }
  175. static int op_arm_start(void)
  176. {
  177. int ret = -EBUSY;
  178. mutex_lock(&op_arm_mutex);
  179. if (!op_arm_enabled) {
  180. ret = 0;
  181. op_perf_start();
  182. op_arm_enabled = 1;
  183. }
  184. mutex_unlock(&op_arm_mutex);
  185. return ret;
  186. }
  187. static void op_arm_stop(void)
  188. {
  189. mutex_lock(&op_arm_mutex);
  190. if (op_arm_enabled)
  191. op_perf_stop();
  192. op_arm_enabled = 0;
  193. mutex_unlock(&op_arm_mutex);
  194. }
  195. #ifdef CONFIG_PM
  196. static int op_arm_suspend(struct platform_device *dev, pm_message_t state)
  197. {
  198. mutex_lock(&op_arm_mutex);
  199. if (op_arm_enabled)
  200. op_perf_stop();
  201. mutex_unlock(&op_arm_mutex);
  202. return 0;
  203. }
  204. static int op_arm_resume(struct platform_device *dev)
  205. {
  206. mutex_lock(&op_arm_mutex);
  207. if (op_arm_enabled && op_perf_start())
  208. op_arm_enabled = 0;
  209. mutex_unlock(&op_arm_mutex);
  210. return 0;
  211. }
  212. static struct platform_driver oprofile_driver = {
  213. .driver = {
  214. .name = "arm-oprofile",
  215. },
  216. .resume = op_arm_resume,
  217. .suspend = op_arm_suspend,
  218. };
  219. static struct platform_device *oprofile_pdev;
  220. static int __init init_driverfs(void)
  221. {
  222. int ret;
  223. ret = platform_driver_register(&oprofile_driver);
  224. if (ret)
  225. return ret;
  226. oprofile_pdev = platform_device_register_simple(
  227. oprofile_driver.driver.name, 0, NULL, 0);
  228. if (IS_ERR(oprofile_pdev)) {
  229. ret = PTR_ERR(oprofile_pdev);
  230. platform_driver_unregister(&oprofile_driver);
  231. }
  232. return ret;
  233. }
  234. static void __exit exit_driverfs(void)
  235. {
  236. platform_device_unregister(oprofile_pdev);
  237. platform_driver_unregister(&oprofile_driver);
  238. }
  239. #else
  240. static int __init init_driverfs(void) { return 0; }
  241. #define exit_driverfs() do { } while (0)
  242. #endif /* CONFIG_PM */
  243. static int report_trace(struct stackframe *frame, void *d)
  244. {
  245. unsigned int *depth = d;
  246. if (*depth) {
  247. oprofile_add_trace(frame->pc);
  248. (*depth)--;
  249. }
  250. return *depth == 0;
  251. }
  252. /*
  253. * The registers we're interested in are at the end of the variable
  254. * length saved register structure. The fp points at the end of this
  255. * structure so the address of this struct is:
  256. * (struct frame_tail *)(xxx->fp)-1
  257. */
  258. struct frame_tail {
  259. struct frame_tail *fp;
  260. unsigned long sp;
  261. unsigned long lr;
  262. } __attribute__((packed));
  263. static struct frame_tail* user_backtrace(struct frame_tail *tail)
  264. {
  265. struct frame_tail buftail[2];
  266. /* Also check accessibility of one struct frame_tail beyond */
  267. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  268. return NULL;
  269. if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
  270. return NULL;
  271. oprofile_add_trace(buftail[0].lr);
  272. /* frame pointers should strictly progress back up the stack
  273. * (towards higher addresses) */
  274. if (tail >= buftail[0].fp)
  275. return NULL;
  276. return buftail[0].fp-1;
  277. }
  278. static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  279. {
  280. struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  281. if (!user_mode(regs)) {
  282. struct stackframe frame;
  283. frame.fp = regs->ARM_fp;
  284. frame.sp = regs->ARM_sp;
  285. frame.lr = regs->ARM_lr;
  286. frame.pc = regs->ARM_pc;
  287. walk_stackframe(&frame, report_trace, &depth);
  288. return;
  289. }
  290. while (depth-- && tail && !((unsigned long) tail & 3))
  291. tail = user_backtrace(tail);
  292. }
  293. void oprofile_arch_exit(void)
  294. {
  295. int cpu, id;
  296. struct perf_event *event;
  297. for_each_possible_cpu(cpu) {
  298. for (id = 0; id < perf_num_counters; ++id) {
  299. event = perf_events[cpu][id];
  300. if (event)
  301. perf_event_release_kernel(event);
  302. }
  303. kfree(perf_events[cpu]);
  304. }
  305. kfree(counter_config);
  306. exit_driverfs();
  307. }
  308. int __init oprofile_arch_init(struct oprofile_operations *ops)
  309. {
  310. int cpu, ret = 0;
  311. ret = init_driverfs();
  312. if (ret)
  313. return ret;
  314. memset(&perf_events, 0, sizeof(perf_events));
  315. perf_num_counters = armpmu_get_max_events();
  316. counter_config = kcalloc(perf_num_counters,
  317. sizeof(struct op_counter_config), GFP_KERNEL);
  318. if (!counter_config) {
  319. pr_info("oprofile: failed to allocate %d "
  320. "counters\n", perf_num_counters);
  321. ret = -ENOMEM;
  322. perf_num_counters = 0;
  323. goto out;
  324. }
  325. for_each_possible_cpu(cpu) {
  326. perf_events[cpu] = kcalloc(perf_num_counters,
  327. sizeof(struct perf_event *), GFP_KERNEL);
  328. if (!perf_events[cpu]) {
  329. pr_info("oprofile: failed to allocate %d perf events "
  330. "for cpu %d\n", perf_num_counters, cpu);
  331. ret = -ENOMEM;
  332. goto out;
  333. }
  334. }
  335. ops->backtrace = arm_backtrace;
  336. ops->create_files = op_arm_create_files;
  337. ops->setup = op_arm_setup;
  338. ops->start = op_arm_start;
  339. ops->stop = op_arm_stop;
  340. ops->shutdown = op_arm_stop;
  341. ops->cpu_type = op_name_from_perf_id(armpmu_get_pmu_id());
  342. if (!ops->cpu_type)
  343. ret = -ENODEV;
  344. else
  345. pr_info("oprofile: using %s\n", ops->cpu_type);
  346. out:
  347. if (ret)
  348. oprofile_arch_exit();
  349. return ret;
  350. }
  351. #else
  352. int __init oprofile_arch_init(struct oprofile_operations *ops)
  353. {
  354. pr_info("oprofile: hardware counters not available\n");
  355. return -ENODEV;
  356. }
  357. void __exit oprofile_arch_exit(void) {}
  358. #endif /* CONFIG_HW_PERF_EVENTS */