callchain.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Performance events callchain code, extracted from core.c:
  4. *
  5. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  6. * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
  7. * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
  8. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  9. */
  10. #include <linux/perf_event.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched/task_stack.h>
  13. #include <linux/uprobes.h>
  14. #include "internal.h"
  15. struct callchain_cpus_entries {
  16. struct rcu_head rcu_head;
  17. struct perf_callchain_entry *cpu_entries[];
  18. };
  19. int sysctl_perf_event_max_stack __read_mostly = PERF_MAX_STACK_DEPTH;
  20. int sysctl_perf_event_max_contexts_per_stack __read_mostly = PERF_MAX_CONTEXTS_PER_STACK;
  21. static inline size_t perf_callchain_entry__sizeof(void)
  22. {
  23. return (sizeof(struct perf_callchain_entry) +
  24. sizeof(__u64) * (sysctl_perf_event_max_stack +
  25. sysctl_perf_event_max_contexts_per_stack));
  26. }
  27. static DEFINE_PER_CPU(u8, callchain_recursion[PERF_NR_CONTEXTS]);
  28. static atomic_t nr_callchain_events;
  29. static DEFINE_MUTEX(callchain_mutex);
  30. static struct callchain_cpus_entries *callchain_cpus_entries;
  31. __weak void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  32. struct pt_regs *regs)
  33. {
  34. }
  35. __weak void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
  36. struct pt_regs *regs)
  37. {
  38. }
  39. static void release_callchain_buffers_rcu(struct rcu_head *head)
  40. {
  41. struct callchain_cpus_entries *entries;
  42. int cpu;
  43. entries = container_of(head, struct callchain_cpus_entries, rcu_head);
  44. for_each_possible_cpu(cpu)
  45. kfree(entries->cpu_entries[cpu]);
  46. kfree(entries);
  47. }
  48. static void release_callchain_buffers(void)
  49. {
  50. struct callchain_cpus_entries *entries;
  51. entries = callchain_cpus_entries;
  52. RCU_INIT_POINTER(callchain_cpus_entries, NULL);
  53. call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  54. }
  55. static int alloc_callchain_buffers(void)
  56. {
  57. int cpu;
  58. int size;
  59. struct callchain_cpus_entries *entries;
  60. /*
  61. * We can't use the percpu allocation API for data that can be
  62. * accessed from NMI. Use a temporary manual per cpu allocation
  63. * until that gets sorted out.
  64. */
  65. size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
  66. entries = kzalloc(size, GFP_KERNEL);
  67. if (!entries)
  68. return -ENOMEM;
  69. size = perf_callchain_entry__sizeof() * PERF_NR_CONTEXTS;
  70. for_each_possible_cpu(cpu) {
  71. entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  72. cpu_to_node(cpu));
  73. if (!entries->cpu_entries[cpu])
  74. goto fail;
  75. }
  76. rcu_assign_pointer(callchain_cpus_entries, entries);
  77. return 0;
  78. fail:
  79. for_each_possible_cpu(cpu)
  80. kfree(entries->cpu_entries[cpu]);
  81. kfree(entries);
  82. return -ENOMEM;
  83. }
  84. int get_callchain_buffers(int event_max_stack)
  85. {
  86. int err = 0;
  87. int count;
  88. mutex_lock(&callchain_mutex);
  89. count = atomic_inc_return(&nr_callchain_events);
  90. if (WARN_ON_ONCE(count < 1)) {
  91. err = -EINVAL;
  92. goto exit;
  93. }
  94. /*
  95. * If requesting per event more than the global cap,
  96. * return a different error to help userspace figure
  97. * this out.
  98. *
  99. * And also do it here so that we have &callchain_mutex held.
  100. */
  101. if (event_max_stack > sysctl_perf_event_max_stack) {
  102. err = -EOVERFLOW;
  103. goto exit;
  104. }
  105. if (count == 1)
  106. err = alloc_callchain_buffers();
  107. exit:
  108. if (err)
  109. atomic_dec(&nr_callchain_events);
  110. mutex_unlock(&callchain_mutex);
  111. return err;
  112. }
  113. void put_callchain_buffers(void)
  114. {
  115. if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  116. release_callchain_buffers();
  117. mutex_unlock(&callchain_mutex);
  118. }
  119. }
  120. struct perf_callchain_entry *get_callchain_entry(int *rctx)
  121. {
  122. int cpu;
  123. struct callchain_cpus_entries *entries;
  124. *rctx = get_recursion_context(this_cpu_ptr(callchain_recursion));
  125. if (*rctx == -1)
  126. return NULL;
  127. entries = rcu_dereference(callchain_cpus_entries);
  128. if (!entries) {
  129. put_recursion_context(this_cpu_ptr(callchain_recursion), *rctx);
  130. return NULL;
  131. }
  132. cpu = smp_processor_id();
  133. return (((void *)entries->cpu_entries[cpu]) +
  134. (*rctx * perf_callchain_entry__sizeof()));
  135. }
  136. void
  137. put_callchain_entry(int rctx)
  138. {
  139. put_recursion_context(this_cpu_ptr(callchain_recursion), rctx);
  140. }
  141. static void fixup_uretprobe_trampoline_entries(struct perf_callchain_entry *entry,
  142. int start_entry_idx)
  143. {
  144. #ifdef CONFIG_UPROBES
  145. struct uprobe_task *utask = current->utask;
  146. struct return_instance *ri;
  147. __u64 *cur_ip, *last_ip, tramp_addr;
  148. if (likely(!utask || !utask->return_instances))
  149. return;
  150. cur_ip = &entry->ip[start_entry_idx];
  151. last_ip = &entry->ip[entry->nr - 1];
  152. ri = utask->return_instances;
  153. tramp_addr = uprobe_get_trampoline_vaddr();
  154. /*
  155. * If there are pending uretprobes for the current thread, they are
  156. * recorded in a list inside utask->return_instances; each such
  157. * pending uretprobe replaces traced user function's return address on
  158. * the stack, so when stack trace is captured, instead of seeing
  159. * actual function's return address, we'll have one or many uretprobe
  160. * trampoline addresses in the stack trace, which are not helpful and
  161. * misleading to users.
  162. * So here we go over the pending list of uretprobes, and each
  163. * encountered trampoline address is replaced with actual return
  164. * address.
  165. */
  166. while (ri && cur_ip <= last_ip) {
  167. if (*cur_ip == tramp_addr) {
  168. *cur_ip = ri->orig_ret_vaddr;
  169. ri = ri->next;
  170. }
  171. cur_ip++;
  172. }
  173. #endif
  174. }
  175. struct perf_callchain_entry *
  176. get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
  177. u32 max_stack, bool crosstask, bool add_mark)
  178. {
  179. struct perf_callchain_entry *entry;
  180. struct perf_callchain_entry_ctx ctx;
  181. int rctx, start_entry_idx;
  182. entry = get_callchain_entry(&rctx);
  183. if (!entry)
  184. return NULL;
  185. ctx.entry = entry;
  186. ctx.max_stack = max_stack;
  187. ctx.nr = entry->nr = init_nr;
  188. ctx.contexts = 0;
  189. ctx.contexts_maxed = false;
  190. if (kernel && !user_mode(regs)) {
  191. if (add_mark)
  192. perf_callchain_store_context(&ctx, PERF_CONTEXT_KERNEL);
  193. perf_callchain_kernel(&ctx, regs);
  194. }
  195. if (user) {
  196. if (!user_mode(regs)) {
  197. if (current->mm)
  198. regs = task_pt_regs(current);
  199. else
  200. regs = NULL;
  201. }
  202. if (regs) {
  203. if (crosstask)
  204. goto exit_put;
  205. if (add_mark)
  206. perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
  207. start_entry_idx = entry->nr;
  208. perf_callchain_user(&ctx, regs);
  209. fixup_uretprobe_trampoline_entries(entry, start_entry_idx);
  210. }
  211. }
  212. exit_put:
  213. put_callchain_entry(rctx);
  214. return entry;
  215. }
  216. /*
  217. * Used for sysctl_perf_event_max_stack and
  218. * sysctl_perf_event_max_contexts_per_stack.
  219. */
  220. int perf_event_max_stack_handler(const struct ctl_table *table, int write,
  221. void *buffer, size_t *lenp, loff_t *ppos)
  222. {
  223. int *value = table->data;
  224. int new_value = *value, ret;
  225. struct ctl_table new_table = *table;
  226. new_table.data = &new_value;
  227. ret = proc_dointvec_minmax(&new_table, write, buffer, lenp, ppos);
  228. if (ret || !write)
  229. return ret;
  230. mutex_lock(&callchain_mutex);
  231. if (atomic_read(&nr_callchain_events))
  232. ret = -EBUSY;
  233. else
  234. *value = new_value;
  235. mutex_unlock(&callchain_mutex);
  236. return ret;
  237. }