trace_event_perf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. struct hlist_head __percpu *pcpu_list;
  197. struct hlist_head *list;
  198. pcpu_list = tp_event->perf_events;
  199. if (WARN_ON_ONCE(!pcpu_list))
  200. return -EINVAL;
  201. if (!(flags & PERF_EF_START))
  202. p_event->hw.state = PERF_HES_STOPPED;
  203. list = this_cpu_ptr(pcpu_list);
  204. hlist_add_head_rcu(&p_event->hlist_entry, list);
  205. return tp_event->class->reg(tp_event, TRACE_REG_PERF_ADD, p_event);
  206. }
  207. void perf_trace_del(struct perf_event *p_event, int flags)
  208. {
  209. struct trace_event_call *tp_event = p_event->tp_event;
  210. hlist_del_rcu(&p_event->hlist_entry);
  211. tp_event->class->reg(tp_event, TRACE_REG_PERF_DEL, p_event);
  212. }
  213. void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp)
  214. {
  215. char *raw_data;
  216. int rctx;
  217. BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long));
  218. if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
  219. "perf buffer not large enough"))
  220. return NULL;
  221. *rctxp = rctx = perf_swevent_get_recursion_context();
  222. if (rctx < 0)
  223. return NULL;
  224. if (regs)
  225. *regs = this_cpu_ptr(&__perf_regs[rctx]);
  226. raw_data = this_cpu_ptr(perf_trace_buf[rctx]);
  227. /* zero the dead bytes from align to not leak stack to user */
  228. memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64));
  229. return raw_data;
  230. }
  231. EXPORT_SYMBOL_GPL(perf_trace_buf_alloc);
  232. NOKPROBE_SYMBOL(perf_trace_buf_alloc);
  233. void perf_trace_buf_update(void *record, u16 type)
  234. {
  235. struct trace_entry *entry = record;
  236. int pc = preempt_count();
  237. unsigned long flags;
  238. local_save_flags(flags);
  239. tracing_generic_entry_update(entry, flags, pc);
  240. entry->type = type;
  241. }
  242. NOKPROBE_SYMBOL(perf_trace_buf_update);
  243. #ifdef CONFIG_FUNCTION_TRACER
  244. static void
  245. perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip,
  246. struct ftrace_ops *ops, struct pt_regs *pt_regs)
  247. {
  248. struct ftrace_entry *entry;
  249. struct hlist_head *head;
  250. struct pt_regs regs;
  251. int rctx;
  252. head = this_cpu_ptr(event_function.perf_events);
  253. if (hlist_empty(head))
  254. return;
  255. #define ENTRY_SIZE (ALIGN(sizeof(struct ftrace_entry) + sizeof(u32), \
  256. sizeof(u64)) - sizeof(u32))
  257. BUILD_BUG_ON(ENTRY_SIZE > PERF_MAX_TRACE_SIZE);
  258. memset(&regs, 0, sizeof(regs));
  259. perf_fetch_caller_regs(&regs);
  260. entry = perf_trace_buf_alloc(ENTRY_SIZE, NULL, &rctx);
  261. if (!entry)
  262. return;
  263. entry->ip = ip;
  264. entry->parent_ip = parent_ip;
  265. perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, TRACE_FN,
  266. 1, &regs, head, NULL);
  267. #undef ENTRY_SIZE
  268. }
  269. static int perf_ftrace_function_register(struct perf_event *event)
  270. {
  271. struct ftrace_ops *ops = &event->ftrace_ops;
  272. ops->flags |= FTRACE_OPS_FL_PER_CPU | FTRACE_OPS_FL_RCU;
  273. ops->func = perf_ftrace_function_call;
  274. return register_ftrace_function(ops);
  275. }
  276. static int perf_ftrace_function_unregister(struct perf_event *event)
  277. {
  278. struct ftrace_ops *ops = &event->ftrace_ops;
  279. int ret = unregister_ftrace_function(ops);
  280. ftrace_free_filter(ops);
  281. return ret;
  282. }
  283. static void perf_ftrace_function_enable(struct perf_event *event)
  284. {
  285. ftrace_function_local_enable(&event->ftrace_ops);
  286. }
  287. static void perf_ftrace_function_disable(struct perf_event *event)
  288. {
  289. ftrace_function_local_disable(&event->ftrace_ops);
  290. }
  291. int perf_ftrace_event_register(struct trace_event_call *call,
  292. enum trace_reg type, void *data)
  293. {
  294. switch (type) {
  295. case TRACE_REG_REGISTER:
  296. case TRACE_REG_UNREGISTER:
  297. break;
  298. case TRACE_REG_PERF_REGISTER:
  299. case TRACE_REG_PERF_UNREGISTER:
  300. return 0;
  301. case TRACE_REG_PERF_OPEN:
  302. return perf_ftrace_function_register(data);
  303. case TRACE_REG_PERF_CLOSE:
  304. return perf_ftrace_function_unregister(data);
  305. case TRACE_REG_PERF_ADD:
  306. perf_ftrace_function_enable(data);
  307. return 0;
  308. case TRACE_REG_PERF_DEL:
  309. perf_ftrace_function_disable(data);
  310. return 0;
  311. }
  312. return -EINVAL;
  313. }
  314. #endif /* CONFIG_FUNCTION_TRACER */