trace_event_perf.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * trace event based perf event profiling/tracing
  3. *
  4. * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra
  5. * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kprobes.h>
  9. #include "trace.h"
  10. static char __percpu *perf_trace_buf[PERF_NR_CONTEXTS];
  11. /*
  12. * Force it to be aligned to unsigned long to avoid misaligned accesses
  13. * suprises
  14. */
  15. typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)])
  16. perf_trace_t;
  17. /* Count the events in use (per event id, not per instance) */
  18. static int total_ref_count;
  19. static int perf_trace_event_perm(struct trace_event_call *tp_event,
  20. struct perf_event *p_event)
  21. {
  22. if (tp_event->perf_perm) {
  23. int ret = tp_event->perf_perm(tp_event, p_event);
  24. if (ret)
  25. return ret;
  26. }
  27. /*
  28. * We checked and allowed to create parent,
  29. * allow children without checking.
  30. */
  31. if (p_event->parent)
  32. return 0;
  33. /*
  34. * It's ok to check current process (owner) permissions in here,
  35. * because code below is called only via perf_event_open syscall.
  36. */
  37. /* The ftrace function trace is allowed only for root. */
  38. if (ftrace_event_is_function(tp_event)) {
  39. if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN))
  40. return -EPERM;
  41. if (!is_sampling_event(p_event))
  42. return 0;
  43. /*
  44. * We don't allow user space callchains for function trace
  45. * event, due to issues with page faults while tracing page
  46. * fault handler and its overall trickiness nature.
  47. */
  48. if (!p_event->attr.exclude_callchain_user)
  49. return -EINVAL;
  50. /*
  51. * Same reason to disable user stack dump as for user space
  52. * callchains above.
  53. */
  54. if (p_event->attr.sample_type & PERF_SAMPLE_STACK_USER)
  55. return -EINVAL;
  56. }
  57. /* No tracing, just counting, so no obvious leak */
  58. if (!(p_event->attr.sample_type & PERF_SAMPLE_RAW))
  59. return 0;
  60. /* Some events are ok to be traced by non-root users... */
  61. if (p_event->attach_state == PERF_ATTACH_TASK) {
  62. if (tp_event->flags & TRACE_EVENT_FL_CAP_ANY)
  63. return 0;
  64. }
  65. /*
  66. * ...otherwise raw tracepoint data can be a severe data leak,
  67. * only allow root to have these.
  68. */
  69. if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN))
  70. return -EPERM;
  71. return 0;
  72. }
  73. static int perf_trace_event_reg(struct trace_event_call *tp_event,
  74. struct perf_event *p_event)
  75. {
  76. struct hlist_head __percpu *list;
  77. int ret = -ENOMEM;
  78. int cpu;
  79. p_event->tp_event = tp_event;
  80. if (tp_event->perf_refcount++ > 0)
  81. return 0;
  82. list = alloc_percpu(struct hlist_head);
  83. if (!list)
  84. goto fail;
  85. for_each_possible_cpu(cpu)
  86. INIT_HLIST_HEAD(per_cpu_ptr(list, cpu));
  87. tp_event->perf_events = list;
  88. if (!total_ref_count) {
  89. char __percpu *buf;
  90. int i;
  91. for (i = 0; i < PERF_NR_CONTEXTS; i++) {
  92. buf = (char __percpu *)alloc_percpu(perf_trace_t);
  93. if (!buf)
  94. goto fail;
  95. perf_trace_buf[i] = buf;
  96. }
  97. }
  98. ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER, NULL);
  99. if (ret)
  100. goto fail;
  101. total_ref_count++;
  102. return 0;
  103. fail:
  104. if (!total_ref_count) {
  105. int i;
  106. for (i = 0; i < PERF_NR_CONTEXTS; i++) {
  107. free_percpu(perf_trace_buf[i]);
  108. perf_trace_buf[i] = NULL;
  109. }
  110. }
  111. if (!--tp_event->perf_refcount) {
  112. free_percpu(tp_event->perf_events);
  113. tp_event->perf_events = NULL;
  114. }
  115. return ret;
  116. }
  117. static void perf_trace_event_unreg(struct perf_event *p_event)
  118. {
  119. struct trace_event_call *tp_event = p_event->tp_event;
  120. int i;
  121. if (--tp_event->perf_refcount > 0)
  122. goto out;
  123. tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER, NULL);
  124. /*
  125. * Ensure our callback won't be called anymore. The buffers
  126. * will be freed after that.
  127. */
  128. tracepoint_synchronize_unregister();
  129. free_percpu(tp_event->perf_events);
  130. tp_event->perf_events = NULL;
  131. if (!--total_ref_count) {
  132. for (i = 0; i < PERF_NR_CONTEXTS; i++) {
  133. free_percpu(perf_trace_buf[i]);
  134. perf_trace_buf[i] = NULL;
  135. }
  136. }
  137. out:
  138. module_put(tp_event->mod);
  139. }
  140. static int perf_trace_event_open(struct perf_event *p_event)
  141. {
  142. struct trace_event_call *tp_event = p_event->tp_event;
  143. return tp_event->class->reg(tp_event, TRACE_REG_PERF_OPEN, p_event);
  144. }
  145. static void perf_trace_event_close(struct perf_event *p_event)
  146. {
  147. struct trace_event_call *tp_event = p_event->tp_event;
  148. tp_event->class->reg(tp_event, TRACE_REG_PERF_CLOSE, p_event);
  149. }
  150. static int perf_trace_event_init(struct trace_event_call *tp_event,
  151. struct perf_event *p_event)
  152. {
  153. int ret;
  154. ret = perf_trace_event_perm(tp_event, p_event);
  155. if (ret)
  156. return ret;
  157. ret = perf_trace_event_reg(tp_event, p_event);
  158. if (ret)
  159. return ret;
  160. ret = perf_trace_event_open(p_event);
  161. if (ret) {
  162. perf_trace_event_unreg(p_event);
  163. return ret;
  164. }
  165. return 0;
  166. }
  167. int perf_trace_init(struct perf_event *p_event)
  168. {
  169. struct trace_event_call *tp_event;
  170. u64 event_id = p_event->attr.config;
  171. int ret = -EINVAL;
  172. mutex_lock(&event_mutex);
  173. list_for_each_entry(tp_event, &ftrace_events, list) {
  174. if (tp_event->event.type == event_id &&
  175. tp_event->class && tp_event->class->reg &&
  176. try_module_get(tp_event->mod)) {
  177. ret = perf_trace_event_init(tp_event, p_event);
  178. if (ret)
  179. module_put(tp_event->mod);
  180. break;
  181. }
  182. }
  183. mutex_unlock(&event_mutex);
  184. return ret;
  185. }
  186. void perf_trace_destroy(struct perf_event *p_event)
  187. {
  188. mutex_lock(&event_mutex);
  189. perf_trace_event_close(p_event);
  190. perf_trace_event_unreg(p_event);
  191. mutex_unlock(&event_mutex);
  192. }
  193. int perf_trace_add(struct perf_event *p_event, int flags)
  194. {
  195. struct trace_event_call *tp_event = p_event->tp_event;
  196. if (!(flags & PERF_EF_START))
  197. p_event->hw.state = PERF_HES_STOPPED;
  198. /*
  199. * If TRACE_REG_PERF_ADD returns false; no custom action was performed
  200. * and we need to take the default action of enqueueing our event on
  201. * the right per-cpu hlist.
  202. */
  203. if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_ADD, p_event)) {
  204. struct hlist_head __percpu *pcpu_list;
  205. struct hlist_head *list;
  206. pcpu_list = tp_event->perf_events;
  207. if (WARN_ON_ONCE(!pcpu_list))
  208. return -EINVAL;
  209. list = this_cpu_ptr(pcpu_list);
  210. hlist_add_head_rcu(&p_event->hlist_entry, list);
  211. }
  212. return 0;
  213. }
  214. void perf_trace_del(struct perf_event *p_event, int flags)
  215. {
  216. struct trace_event_call *tp_event = p_event->tp_event;
  217. /*
  218. * If TRACE_REG_PERF_DEL returns false; no custom action was performed
  219. * and we need to take the default action of dequeueing our event from
  220. * the right per-cpu hlist.
  221. */
  222. if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_DEL, p_event))
  223. hlist_del_rcu(&p_event->hlist_entry);
  224. }
  225. void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp)
  226. {
  227. char *raw_data;
  228. int rctx;
  229. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long));
  230. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  231. "perf buffer not large enough"))
  232. return NULL;
  233. *rctxp = rctx = perf_swevent_get_recursion_context();
  234. if (rctx < 0)
  235. return NULL;
  236. if (regs)
  237. *regs = this_cpu_ptr(&__perf_regs[rctx]);
  238. raw_data = this_cpu_ptr(perf_trace_buf[rctx]);
  239. /* zero the dead bytes from align to not leak stack to user */
  240. memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64));
  241. return raw_data;
  242. }
  243. EXPORT_SYMBOL_GPL(perf_trace_buf_alloc);
  244. NOKPROBE_SYMBOL(perf_trace_buf_alloc);
  245. void perf_trace_buf_update(void *record, u16 type)
  246. {
  247. struct trace_entry *entry = record;
  248. int pc = preempt_count();
  249. unsigned long flags;
  250. local_save_flags(flags);
  251. tracing_generic_entry_update(entry, flags, pc);
  252. entry->type = type;
  253. }
  254. NOKPROBE_SYMBOL(perf_trace_buf_update);
  255. #ifdef CONFIG_FUNCTION_TRACER
  256. static void
  257. perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip,
  258. struct ftrace_ops *ops, struct pt_regs *pt_regs)
  259. {
  260. struct ftrace_entry *entry;
  261. struct perf_event *event;
  262. struct hlist_head head;
  263. struct pt_regs regs;
  264. int rctx;
  265. if ((unsigned long)ops->private != smp_processor_id())
  266. return;
  267. event = container_of(ops, struct perf_event, ftrace_ops);
  268. /*
  269. * @event->hlist entry is NULL (per INIT_HLIST_NODE), and all
  270. * the perf code does is hlist_for_each_entry_rcu(), so we can
  271. * get away with simply setting the @head.first pointer in order
  272. * to create a singular list.
  273. */
  274. head.first = &event->hlist_entry;
  275. #define ENTRY_SIZE (ALIGN(sizeof(struct ftrace_entry) + sizeof(u32), \
  276. sizeof(u64)) - sizeof(u32))
  277. BUILD_BUG_ON(ENTRY_SIZE > PERF_MAX_TRACE_SIZE);
  278. memset(&regs, 0, sizeof(regs));
  279. perf_fetch_caller_regs(&regs);
  280. entry = perf_trace_buf_alloc(ENTRY_SIZE, NULL, &rctx);
  281. if (!entry)
  282. return;
  283. entry->ip = ip;
  284. entry->parent_ip = parent_ip;
  285. perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, TRACE_FN,
  286. 1, &regs, &head, NULL);
  287. #undef ENTRY_SIZE
  288. }
  289. static int perf_ftrace_function_register(struct perf_event *event)
  290. {
  291. struct ftrace_ops *ops = &event->ftrace_ops;
  292. ops->flags = FTRACE_OPS_FL_RCU;
  293. ops->func = perf_ftrace_function_call;
  294. ops->private = (void *)(unsigned long)nr_cpu_ids;
  295. return register_ftrace_function(ops);
  296. }
  297. static int perf_ftrace_function_unregister(struct perf_event *event)
  298. {
  299. struct ftrace_ops *ops = &event->ftrace_ops;
  300. int ret = unregister_ftrace_function(ops);
  301. ftrace_free_filter(ops);
  302. return ret;
  303. }
  304. int perf_ftrace_event_register(struct trace_event_call *call,
  305. enum trace_reg type, void *data)
  306. {
  307. struct perf_event *event = data;
  308. switch (type) {
  309. case TRACE_REG_REGISTER:
  310. case TRACE_REG_UNREGISTER:
  311. break;
  312. case TRACE_REG_PERF_REGISTER:
  313. case TRACE_REG_PERF_UNREGISTER:
  314. return 0;
  315. case TRACE_REG_PERF_OPEN:
  316. return perf_ftrace_function_register(data);
  317. case TRACE_REG_PERF_CLOSE:
  318. return perf_ftrace_function_unregister(data);
  319. case TRACE_REG_PERF_ADD:
  320. event->ftrace_ops.private = (void *)(unsigned long)smp_processor_id();
  321. return 1;
  322. case TRACE_REG_PERF_DEL:
  323. event->ftrace_ops.private = (void *)(unsigned long)nr_cpu_ids;
  324. return 1;
  325. }
  326. return -EINVAL;
  327. }
  328. #endif /* CONFIG_FUNCTION_TRACER */