callchain.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Performance events callchain code, extracted from core.c:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
  7. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  8. *
  9. * For licensing details see kernel-base/COPYING
  10. */
  11. #include <linux/perf_event.h>
  12. #include <linux/slab.h>
  13. #include "internal.h"
  14. struct callchain_cpus_entries {
  15. struct rcu_head rcu_head;
  16. struct perf_callchain_entry *cpu_entries[0];
  17. };
  18. int sysctl_perf_event_max_stack __read_mostly = PERF_MAX_STACK_DEPTH;
  19. int sysctl_perf_event_max_contexts_per_stack __read_mostly = PERF_MAX_CONTEXTS_PER_STACK;
  20. static inline size_t perf_callchain_entry__sizeof(void)
  21. {
  22. return (sizeof(struct perf_callchain_entry) +
  23. sizeof(__u64) * (sysctl_perf_event_max_stack +
  24. sysctl_perf_event_max_contexts_per_stack));
  25. }
  26. static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
  27. static atomic_t nr_callchain_events;
  28. static DEFINE_MUTEX(callchain_mutex);
  29. static struct callchain_cpus_entries *callchain_cpus_entries;
  30. __weak void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  31. struct pt_regs *regs)
  32. {
  33. }
  34. __weak void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
  35. struct pt_regs *regs)
  36. {
  37. }
  38. static void release_callchain_buffers_rcu(struct rcu_head *head)
  39. {
  40. struct callchain_cpus_entries *entries;
  41. int cpu;
  42. entries = container_of(head, struct callchain_cpus_entries, rcu_head);
  43. for_each_possible_cpu(cpu)
  44. kfree(entries->cpu_entries[cpu]);
  45. kfree(entries);
  46. }
  47. static void release_callchain_buffers(void)
  48. {
  49. struct callchain_cpus_entries *entries;
  50. entries = callchain_cpus_entries;
  51. RCU_INIT_POINTER(callchain_cpus_entries, NULL);
  52. call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  53. }
  54. static int alloc_callchain_buffers(void)
  55. {
  56. int cpu;
  57. int size;
  58. struct callchain_cpus_entries *entries;
  59. /*
  60. * We can't use the percpu allocation API for data that can be
  61. * accessed from NMI. Use a temporary manual per cpu allocation
  62. * until that gets sorted out.
  63. */
  64. size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
  65. entries = kzalloc(size, GFP_KERNEL);
  66. if (!entries)
  67. return -ENOMEM;
  68. size = perf_callchain_entry__sizeof() * PERF_NR_CONTEXTS;
  69. for_each_possible_cpu(cpu) {
  70. entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  71. cpu_to_node(cpu));
  72. if (!entries->cpu_entries[cpu])
  73. goto fail;
  74. }
  75. rcu_assign_pointer(callchain_cpus_entries, entries);
  76. return 0;
  77. fail:
  78. for_each_possible_cpu(cpu)
  79. kfree(entries->cpu_entries[cpu]);
  80. kfree(entries);
  81. return -ENOMEM;
  82. }
  83. int get_callchain_buffers(int event_max_stack)
  84. {
  85. int err = 0;
  86. int count;
  87. mutex_lock(&callchain_mutex);
  88. count = atomic_inc_return(&nr_callchain_events);
  89. if (WARN_ON_ONCE(count < 1)) {
  90. err = -EINVAL;
  91. goto exit;
  92. }
  93. /*
  94. * If requesting per event more than the global cap,
  95. * return a different error to help userspace figure
  96. * this out.
  97. *
  98. * And also do it here so that we have &callchain_mutex held.
  99. */
  100. if (event_max_stack > sysctl_perf_event_max_stack) {
  101. err = -EOVERFLOW;
  102. goto exit;
  103. }
  104. if (count == 1)
  105. err = alloc_callchain_buffers();
  106. exit:
  107. if (err)
  108. atomic_dec(&nr_callchain_events);
  109. mutex_unlock(&callchain_mutex);
  110. return err;
  111. }
  112. void put_callchain_buffers(void)
  113. {
  114. if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  115. release_callchain_buffers();
  116. mutex_unlock(&callchain_mutex);
  117. }
  118. }
  119. static struct perf_callchain_entry *get_callchain_entry(int *rctx)
  120. {
  121. int cpu;
  122. struct callchain_cpus_entries *entries;
  123. *rctx = get_recursion_context(this_cpu_ptr(callchain_recursion));
  124. if (*rctx == -1)
  125. return NULL;
  126. entries = rcu_dereference(callchain_cpus_entries);
  127. if (!entries)
  128. return NULL;
  129. cpu = smp_processor_id();
  130. return (((void *)entries->cpu_entries[cpu]) +
  131. (*rctx * perf_callchain_entry__sizeof()));
  132. }
  133. static void
  134. put_callchain_entry(int rctx)
  135. {
  136. put_recursion_context(this_cpu_ptr(callchain_recursion), rctx);
  137. }
  138. struct perf_callchain_entry *
  139. perf_callchain(struct perf_event *event, struct pt_regs *regs)
  140. {
  141. bool kernel = !event->attr.exclude_callchain_kernel;
  142. bool user = !event->attr.exclude_callchain_user;
  143. /* Disallow cross-task user callchains. */
  144. bool crosstask = event->ctx->task && event->ctx->task != current;
  145. const u32 max_stack = event->attr.sample_max_stack;
  146. if (!kernel && !user)
  147. return NULL;
  148. return get_perf_callchain(regs, 0, kernel, user, max_stack, crosstask, true);
  149. }
  150. struct perf_callchain_entry *
  151. get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
  152. u32 max_stack, bool crosstask, bool add_mark)
  153. {
  154. struct perf_callchain_entry *entry;
  155. struct perf_callchain_entry_ctx ctx;
  156. int rctx;
  157. entry = get_callchain_entry(&rctx);
  158. if (rctx == -1)
  159. return NULL;
  160. if (!entry)
  161. goto exit_put;
  162. ctx.entry = entry;
  163. ctx.max_stack = max_stack;
  164. ctx.nr = entry->nr = init_nr;
  165. ctx.contexts = 0;
  166. ctx.contexts_maxed = false;
  167. if (kernel && !user_mode(regs)) {
  168. if (add_mark)
  169. perf_callchain_store_context(&ctx, PERF_CONTEXT_KERNEL);
  170. perf_callchain_kernel(&ctx, regs);
  171. }
  172. if (user) {
  173. if (!user_mode(regs)) {
  174. if (current->mm)
  175. regs = task_pt_regs(current);
  176. else
  177. regs = NULL;
  178. }
  179. if (regs) {
  180. mm_segment_t fs;
  181. if (crosstask)
  182. goto exit_put;
  183. if (add_mark)
  184. perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
  185. fs = get_fs();
  186. set_fs(USER_DS);
  187. perf_callchain_user(&ctx, regs);
  188. set_fs(fs);
  189. }
  190. }
  191. exit_put:
  192. put_callchain_entry(rctx);
  193. return entry;
  194. }
  195. /*
  196. * Used for sysctl_perf_event_max_stack and
  197. * sysctl_perf_event_max_contexts_per_stack.
  198. */
  199. int perf_event_max_stack_handler(struct ctl_table *table, int write,
  200. void __user *buffer, size_t *lenp, loff_t *ppos)
  201. {
  202. int *value = table->data;
  203. int new_value = *value, ret;
  204. struct ctl_table new_table = *table;
  205. new_table.data = &new_value;
  206. ret = proc_dointvec_minmax(&new_table, write, buffer, lenp, ppos);
  207. if (ret || !write)
  208. return ret;
  209. mutex_lock(&callchain_mutex);
  210. if (atomic_read(&nr_callchain_events))
  211. ret = -EBUSY;
  212. else
  213. *value = new_value;
  214. mutex_unlock(&callchain_mutex);
  215. return ret;
  216. }