tracepoint.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (C) 2008-2014 Mathieu Desnoyers
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/types.h>
  21. #include <linux/jhash.h>
  22. #include <linux/list.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/tracepoint.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/static_key.h>
  29. extern struct tracepoint * const __start___tracepoints_ptrs[];
  30. extern struct tracepoint * const __stop___tracepoints_ptrs[];
  31. /* Set to 1 to enable tracepoint debug output */
  32. static const int tracepoint_debug;
  33. #ifdef CONFIG_MODULES
  34. /*
  35. * Tracepoint module list mutex protects the local module list.
  36. */
  37. static DEFINE_MUTEX(tracepoint_module_list_mutex);
  38. /* Local list of struct tp_module */
  39. static LIST_HEAD(tracepoint_module_list);
  40. #endif /* CONFIG_MODULES */
  41. /*
  42. * tracepoints_mutex protects the builtin and module tracepoints.
  43. * tracepoints_mutex nests inside tracepoint_module_list_mutex.
  44. */
  45. static DEFINE_MUTEX(tracepoints_mutex);
  46. /*
  47. * Note about RCU :
  48. * It is used to delay the free of multiple probes array until a quiescent
  49. * state is reached.
  50. */
  51. struct tp_probes {
  52. struct rcu_head rcu;
  53. struct tracepoint_func probes[0];
  54. };
  55. static inline void *allocate_probes(int count)
  56. {
  57. struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
  58. + sizeof(struct tp_probes), GFP_KERNEL);
  59. return p == NULL ? NULL : p->probes;
  60. }
  61. static void rcu_free_old_probes(struct rcu_head *head)
  62. {
  63. kfree(container_of(head, struct tp_probes, rcu));
  64. }
  65. static inline void release_probes(struct tracepoint_func *old)
  66. {
  67. if (old) {
  68. struct tp_probes *tp_probes = container_of(old,
  69. struct tp_probes, probes[0]);
  70. call_rcu_sched(&tp_probes->rcu, rcu_free_old_probes);
  71. }
  72. }
  73. static void debug_print_probes(struct tracepoint_func *funcs)
  74. {
  75. int i;
  76. if (!tracepoint_debug || !funcs)
  77. return;
  78. for (i = 0; funcs[i].func; i++)
  79. printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
  80. }
  81. static struct tracepoint_func *func_add(struct tracepoint_func **funcs,
  82. struct tracepoint_func *tp_func)
  83. {
  84. int nr_probes = 0;
  85. struct tracepoint_func *old, *new;
  86. if (WARN_ON(!tp_func->func))
  87. return ERR_PTR(-EINVAL);
  88. debug_print_probes(*funcs);
  89. old = *funcs;
  90. if (old) {
  91. /* (N -> N+1), (N != 0, 1) probes */
  92. for (nr_probes = 0; old[nr_probes].func; nr_probes++)
  93. if (old[nr_probes].func == tp_func->func &&
  94. old[nr_probes].data == tp_func->data)
  95. return ERR_PTR(-EEXIST);
  96. }
  97. /* + 2 : one for new probe, one for NULL func */
  98. new = allocate_probes(nr_probes + 2);
  99. if (new == NULL)
  100. return ERR_PTR(-ENOMEM);
  101. if (old)
  102. memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
  103. new[nr_probes] = *tp_func;
  104. new[nr_probes + 1].func = NULL;
  105. *funcs = new;
  106. debug_print_probes(*funcs);
  107. return old;
  108. }
  109. static void *func_remove(struct tracepoint_func **funcs,
  110. struct tracepoint_func *tp_func)
  111. {
  112. int nr_probes = 0, nr_del = 0, i;
  113. struct tracepoint_func *old, *new;
  114. old = *funcs;
  115. if (!old)
  116. return ERR_PTR(-ENOENT);
  117. debug_print_probes(*funcs);
  118. /* (N -> M), (N > 1, M >= 0) probes */
  119. if (tp_func->func) {
  120. for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
  121. if (old[nr_probes].func == tp_func->func &&
  122. old[nr_probes].data == tp_func->data)
  123. nr_del++;
  124. }
  125. }
  126. /*
  127. * If probe is NULL, then nr_probes = nr_del = 0, and then the
  128. * entire entry will be removed.
  129. */
  130. if (nr_probes - nr_del == 0) {
  131. /* N -> 0, (N > 1) */
  132. *funcs = NULL;
  133. debug_print_probes(*funcs);
  134. return old;
  135. } else {
  136. int j = 0;
  137. /* N -> M, (N > 1, M > 0) */
  138. /* + 1 for NULL */
  139. new = allocate_probes(nr_probes - nr_del + 1);
  140. if (new == NULL)
  141. return ERR_PTR(-ENOMEM);
  142. for (i = 0; old[i].func; i++)
  143. if (old[i].func != tp_func->func
  144. || old[i].data != tp_func->data)
  145. new[j++] = old[i];
  146. new[nr_probes - nr_del].func = NULL;
  147. *funcs = new;
  148. }
  149. debug_print_probes(*funcs);
  150. return old;
  151. }
  152. /*
  153. * Add the probe function to a tracepoint.
  154. */
  155. static int tracepoint_add_func(struct tracepoint *tp,
  156. struct tracepoint_func *func)
  157. {
  158. struct tracepoint_func *old, *tp_funcs;
  159. if (tp->regfunc && !static_key_enabled(&tp->key))
  160. tp->regfunc();
  161. tp_funcs = rcu_dereference_protected(tp->funcs,
  162. lockdep_is_held(&tracepoints_mutex));
  163. old = func_add(&tp_funcs, func);
  164. if (IS_ERR(old)) {
  165. WARN_ON_ONCE(1);
  166. return PTR_ERR(old);
  167. }
  168. release_probes(old);
  169. /*
  170. * rcu_assign_pointer has a smp_wmb() which makes sure that the new
  171. * probe callbacks array is consistent before setting a pointer to it.
  172. * This array is referenced by __DO_TRACE from
  173. * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
  174. * is used.
  175. */
  176. rcu_assign_pointer(tp->funcs, tp_funcs);
  177. if (!static_key_enabled(&tp->key))
  178. static_key_slow_inc(&tp->key);
  179. return 0;
  180. }
  181. /*
  182. * Remove a probe function from a tracepoint.
  183. * Note: only waiting an RCU period after setting elem->call to the empty
  184. * function insures that the original callback is not used anymore. This insured
  185. * by preempt_disable around the call site.
  186. */
  187. static int tracepoint_remove_func(struct tracepoint *tp,
  188. struct tracepoint_func *func)
  189. {
  190. struct tracepoint_func *old, *tp_funcs;
  191. tp_funcs = rcu_dereference_protected(tp->funcs,
  192. lockdep_is_held(&tracepoints_mutex));
  193. old = func_remove(&tp_funcs, func);
  194. if (IS_ERR(old)) {
  195. WARN_ON_ONCE(1);
  196. return PTR_ERR(old);
  197. }
  198. release_probes(old);
  199. if (!tp_funcs) {
  200. /* Removed last function */
  201. if (tp->unregfunc && static_key_enabled(&tp->key))
  202. tp->unregfunc();
  203. if (static_key_enabled(&tp->key))
  204. static_key_slow_dec(&tp->key);
  205. }
  206. rcu_assign_pointer(tp->funcs, tp_funcs);
  207. return 0;
  208. }
  209. /**
  210. * tracepoint_probe_register - Connect a probe to a tracepoint
  211. * @tp: tracepoint
  212. * @probe: probe handler
  213. *
  214. * Returns 0 if ok, error value on error.
  215. * Note: if @tp is within a module, the caller is responsible for
  216. * unregistering the probe before the module is gone. This can be
  217. * performed either with a tracepoint module going notifier, or from
  218. * within module exit functions.
  219. */
  220. int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
  221. {
  222. struct tracepoint_func tp_func;
  223. int ret;
  224. mutex_lock(&tracepoints_mutex);
  225. tp_func.func = probe;
  226. tp_func.data = data;
  227. ret = tracepoint_add_func(tp, &tp_func);
  228. mutex_unlock(&tracepoints_mutex);
  229. return ret;
  230. }
  231. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  232. /**
  233. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  234. * @tp: tracepoint
  235. * @probe: probe function pointer
  236. *
  237. * Returns 0 if ok, error value on error.
  238. */
  239. int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
  240. {
  241. struct tracepoint_func tp_func;
  242. int ret;
  243. mutex_lock(&tracepoints_mutex);
  244. tp_func.func = probe;
  245. tp_func.data = data;
  246. ret = tracepoint_remove_func(tp, &tp_func);
  247. mutex_unlock(&tracepoints_mutex);
  248. return ret;
  249. }
  250. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  251. #ifdef CONFIG_MODULES
  252. bool trace_module_has_bad_taint(struct module *mod)
  253. {
  254. return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP));
  255. }
  256. static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
  257. /**
  258. * register_tracepoint_notifier - register tracepoint coming/going notifier
  259. * @nb: notifier block
  260. *
  261. * Notifiers registered with this function are called on module
  262. * coming/going with the tracepoint_module_list_mutex held.
  263. * The notifier block callback should expect a "struct tp_module" data
  264. * pointer.
  265. */
  266. int register_tracepoint_module_notifier(struct notifier_block *nb)
  267. {
  268. struct tp_module *tp_mod;
  269. int ret;
  270. mutex_lock(&tracepoint_module_list_mutex);
  271. ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
  272. if (ret)
  273. goto end;
  274. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  275. (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
  276. end:
  277. mutex_unlock(&tracepoint_module_list_mutex);
  278. return ret;
  279. }
  280. EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
  281. /**
  282. * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
  283. * @nb: notifier block
  284. *
  285. * The notifier block callback should expect a "struct tp_module" data
  286. * pointer.
  287. */
  288. int unregister_tracepoint_module_notifier(struct notifier_block *nb)
  289. {
  290. struct tp_module *tp_mod;
  291. int ret;
  292. mutex_lock(&tracepoint_module_list_mutex);
  293. ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
  294. if (ret)
  295. goto end;
  296. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  297. (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
  298. end:
  299. mutex_unlock(&tracepoint_module_list_mutex);
  300. return ret;
  301. }
  302. EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
  303. /*
  304. * Ensure the tracer unregistered the module's probes before the module
  305. * teardown is performed. Prevents leaks of probe and data pointers.
  306. */
  307. static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
  308. struct tracepoint * const *end)
  309. {
  310. struct tracepoint * const *iter;
  311. if (!begin)
  312. return;
  313. for (iter = begin; iter < end; iter++)
  314. WARN_ON_ONCE((*iter)->funcs);
  315. }
  316. static int tracepoint_module_coming(struct module *mod)
  317. {
  318. struct tp_module *tp_mod;
  319. int ret = 0;
  320. if (!mod->num_tracepoints)
  321. return 0;
  322. /*
  323. * We skip modules that taint the kernel, especially those with different
  324. * module headers (for forced load), to make sure we don't cause a crash.
  325. * Staging and out-of-tree GPL modules are fine.
  326. */
  327. if (trace_module_has_bad_taint(mod))
  328. return 0;
  329. mutex_lock(&tracepoint_module_list_mutex);
  330. tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
  331. if (!tp_mod) {
  332. ret = -ENOMEM;
  333. goto end;
  334. }
  335. tp_mod->mod = mod;
  336. list_add_tail(&tp_mod->list, &tracepoint_module_list);
  337. blocking_notifier_call_chain(&tracepoint_notify_list,
  338. MODULE_STATE_COMING, tp_mod);
  339. end:
  340. mutex_unlock(&tracepoint_module_list_mutex);
  341. return ret;
  342. }
  343. static void tracepoint_module_going(struct module *mod)
  344. {
  345. struct tp_module *tp_mod;
  346. if (!mod->num_tracepoints)
  347. return;
  348. mutex_lock(&tracepoint_module_list_mutex);
  349. list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
  350. if (tp_mod->mod == mod) {
  351. blocking_notifier_call_chain(&tracepoint_notify_list,
  352. MODULE_STATE_GOING, tp_mod);
  353. list_del(&tp_mod->list);
  354. kfree(tp_mod);
  355. /*
  356. * Called the going notifier before checking for
  357. * quiescence.
  358. */
  359. tp_module_going_check_quiescent(mod->tracepoints_ptrs,
  360. mod->tracepoints_ptrs + mod->num_tracepoints);
  361. break;
  362. }
  363. }
  364. /*
  365. * In the case of modules that were tainted at "coming", we'll simply
  366. * walk through the list without finding it. We cannot use the "tainted"
  367. * flag on "going", in case a module taints the kernel only after being
  368. * loaded.
  369. */
  370. mutex_unlock(&tracepoint_module_list_mutex);
  371. }
  372. static int tracepoint_module_notify(struct notifier_block *self,
  373. unsigned long val, void *data)
  374. {
  375. struct module *mod = data;
  376. int ret = 0;
  377. switch (val) {
  378. case MODULE_STATE_COMING:
  379. ret = tracepoint_module_coming(mod);
  380. break;
  381. case MODULE_STATE_LIVE:
  382. break;
  383. case MODULE_STATE_GOING:
  384. tracepoint_module_going(mod);
  385. break;
  386. case MODULE_STATE_UNFORMED:
  387. break;
  388. }
  389. return ret;
  390. }
  391. static struct notifier_block tracepoint_module_nb = {
  392. .notifier_call = tracepoint_module_notify,
  393. .priority = 0,
  394. };
  395. static __init int init_tracepoints(void)
  396. {
  397. int ret;
  398. ret = register_module_notifier(&tracepoint_module_nb);
  399. if (ret)
  400. pr_warning("Failed to register tracepoint module enter notifier\n");
  401. return ret;
  402. }
  403. __initcall(init_tracepoints);
  404. #endif /* CONFIG_MODULES */
  405. static void for_each_tracepoint_range(struct tracepoint * const *begin,
  406. struct tracepoint * const *end,
  407. void (*fct)(struct tracepoint *tp, void *priv),
  408. void *priv)
  409. {
  410. struct tracepoint * const *iter;
  411. if (!begin)
  412. return;
  413. for (iter = begin; iter < end; iter++)
  414. fct(*iter, priv);
  415. }
  416. /**
  417. * for_each_kernel_tracepoint - iteration on all kernel tracepoints
  418. * @fct: callback
  419. * @priv: private data
  420. */
  421. void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
  422. void *priv)
  423. {
  424. for_each_tracepoint_range(__start___tracepoints_ptrs,
  425. __stop___tracepoints_ptrs, fct, priv);
  426. }
  427. EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
  428. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  429. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  430. static int sys_tracepoint_refcount;
  431. void syscall_regfunc(void)
  432. {
  433. unsigned long flags;
  434. struct task_struct *g, *t;
  435. if (!sys_tracepoint_refcount) {
  436. read_lock_irqsave(&tasklist_lock, flags);
  437. do_each_thread(g, t) {
  438. /* Skip kernel threads. */
  439. if (t->mm)
  440. set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  441. } while_each_thread(g, t);
  442. read_unlock_irqrestore(&tasklist_lock, flags);
  443. }
  444. sys_tracepoint_refcount++;
  445. }
  446. void syscall_unregfunc(void)
  447. {
  448. unsigned long flags;
  449. struct task_struct *g, *t;
  450. sys_tracepoint_refcount--;
  451. if (!sys_tracepoint_refcount) {
  452. read_lock_irqsave(&tasklist_lock, flags);
  453. do_each_thread(g, t) {
  454. clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  455. } while_each_thread(g, t);
  456. read_unlock_irqrestore(&tasklist_lock, flags);
  457. }
  458. }
  459. #endif