tracepoint.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. /*
  169. * rcu_assign_pointer has a smp_wmb() which makes sure that the new
  170. * probe callbacks array is consistent before setting a pointer to it.
  171. * This array is referenced by __DO_TRACE from
  172. * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
  173. * is used.
  174. */
  175. rcu_assign_pointer(tp->funcs, tp_funcs);
  176. if (!static_key_enabled(&tp->key))
  177. static_key_slow_inc(&tp->key);
  178. release_probes(old);
  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. if (!tp_funcs) {
  199. /* Removed last function */
  200. if (tp->unregfunc && static_key_enabled(&tp->key))
  201. tp->unregfunc();
  202. if (static_key_enabled(&tp->key))
  203. static_key_slow_dec(&tp->key);
  204. }
  205. rcu_assign_pointer(tp->funcs, tp_funcs);
  206. release_probes(old);
  207. return 0;
  208. }
  209. /**
  210. * tracepoint_probe_register - Connect a probe to a tracepoint
  211. * @tp: tracepoint
  212. * @probe: probe handler
  213. * @data: tracepoint data
  214. *
  215. * Returns 0 if ok, error value on error.
  216. * Note: if @tp is within a module, the caller is responsible for
  217. * unregistering the probe before the module is gone. This can be
  218. * performed either with a tracepoint module going notifier, or from
  219. * within module exit functions.
  220. */
  221. int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
  222. {
  223. struct tracepoint_func tp_func;
  224. int ret;
  225. mutex_lock(&tracepoints_mutex);
  226. tp_func.func = probe;
  227. tp_func.data = data;
  228. ret = tracepoint_add_func(tp, &tp_func);
  229. mutex_unlock(&tracepoints_mutex);
  230. return ret;
  231. }
  232. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  233. /**
  234. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  235. * @tp: tracepoint
  236. * @probe: probe function pointer
  237. * @data: tracepoint data
  238. *
  239. * Returns 0 if ok, error value on error.
  240. */
  241. int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
  242. {
  243. struct tracepoint_func tp_func;
  244. int ret;
  245. mutex_lock(&tracepoints_mutex);
  246. tp_func.func = probe;
  247. tp_func.data = data;
  248. ret = tracepoint_remove_func(tp, &tp_func);
  249. mutex_unlock(&tracepoints_mutex);
  250. return ret;
  251. }
  252. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  253. #ifdef CONFIG_MODULES
  254. bool trace_module_has_bad_taint(struct module *mod)
  255. {
  256. return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
  257. (1 << TAINT_UNSIGNED_MODULE));
  258. }
  259. static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
  260. /**
  261. * register_tracepoint_notifier - register tracepoint coming/going notifier
  262. * @nb: notifier block
  263. *
  264. * Notifiers registered with this function are called on module
  265. * coming/going with the tracepoint_module_list_mutex held.
  266. * The notifier block callback should expect a "struct tp_module" data
  267. * pointer.
  268. */
  269. int register_tracepoint_module_notifier(struct notifier_block *nb)
  270. {
  271. struct tp_module *tp_mod;
  272. int ret;
  273. mutex_lock(&tracepoint_module_list_mutex);
  274. ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
  275. if (ret)
  276. goto end;
  277. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  278. (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
  279. end:
  280. mutex_unlock(&tracepoint_module_list_mutex);
  281. return ret;
  282. }
  283. EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
  284. /**
  285. * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
  286. * @nb: notifier block
  287. *
  288. * The notifier block callback should expect a "struct tp_module" data
  289. * pointer.
  290. */
  291. int unregister_tracepoint_module_notifier(struct notifier_block *nb)
  292. {
  293. struct tp_module *tp_mod;
  294. int ret;
  295. mutex_lock(&tracepoint_module_list_mutex);
  296. ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
  297. if (ret)
  298. goto end;
  299. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  300. (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
  301. end:
  302. mutex_unlock(&tracepoint_module_list_mutex);
  303. return ret;
  304. }
  305. EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
  306. /*
  307. * Ensure the tracer unregistered the module's probes before the module
  308. * teardown is performed. Prevents leaks of probe and data pointers.
  309. */
  310. static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
  311. struct tracepoint * const *end)
  312. {
  313. struct tracepoint * const *iter;
  314. if (!begin)
  315. return;
  316. for (iter = begin; iter < end; iter++)
  317. WARN_ON_ONCE((*iter)->funcs);
  318. }
  319. static int tracepoint_module_coming(struct module *mod)
  320. {
  321. struct tp_module *tp_mod;
  322. int ret = 0;
  323. if (!mod->num_tracepoints)
  324. return 0;
  325. /*
  326. * We skip modules that taint the kernel, especially those with different
  327. * module headers (for forced load), to make sure we don't cause a crash.
  328. * Staging, out-of-tree, and unsigned GPL modules are fine.
  329. */
  330. if (trace_module_has_bad_taint(mod))
  331. return 0;
  332. mutex_lock(&tracepoint_module_list_mutex);
  333. tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
  334. if (!tp_mod) {
  335. ret = -ENOMEM;
  336. goto end;
  337. }
  338. tp_mod->mod = mod;
  339. list_add_tail(&tp_mod->list, &tracepoint_module_list);
  340. blocking_notifier_call_chain(&tracepoint_notify_list,
  341. MODULE_STATE_COMING, tp_mod);
  342. end:
  343. mutex_unlock(&tracepoint_module_list_mutex);
  344. return ret;
  345. }
  346. static void tracepoint_module_going(struct module *mod)
  347. {
  348. struct tp_module *tp_mod;
  349. if (!mod->num_tracepoints)
  350. return;
  351. mutex_lock(&tracepoint_module_list_mutex);
  352. list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
  353. if (tp_mod->mod == mod) {
  354. blocking_notifier_call_chain(&tracepoint_notify_list,
  355. MODULE_STATE_GOING, tp_mod);
  356. list_del(&tp_mod->list);
  357. kfree(tp_mod);
  358. /*
  359. * Called the going notifier before checking for
  360. * quiescence.
  361. */
  362. tp_module_going_check_quiescent(mod->tracepoints_ptrs,
  363. mod->tracepoints_ptrs + mod->num_tracepoints);
  364. break;
  365. }
  366. }
  367. /*
  368. * In the case of modules that were tainted at "coming", we'll simply
  369. * walk through the list without finding it. We cannot use the "tainted"
  370. * flag on "going", in case a module taints the kernel only after being
  371. * loaded.
  372. */
  373. mutex_unlock(&tracepoint_module_list_mutex);
  374. }
  375. static int tracepoint_module_notify(struct notifier_block *self,
  376. unsigned long val, void *data)
  377. {
  378. struct module *mod = data;
  379. int ret = 0;
  380. switch (val) {
  381. case MODULE_STATE_COMING:
  382. ret = tracepoint_module_coming(mod);
  383. break;
  384. case MODULE_STATE_LIVE:
  385. break;
  386. case MODULE_STATE_GOING:
  387. tracepoint_module_going(mod);
  388. break;
  389. case MODULE_STATE_UNFORMED:
  390. break;
  391. }
  392. return ret;
  393. }
  394. static struct notifier_block tracepoint_module_nb = {
  395. .notifier_call = tracepoint_module_notify,
  396. .priority = 0,
  397. };
  398. static __init int init_tracepoints(void)
  399. {
  400. int ret;
  401. ret = register_module_notifier(&tracepoint_module_nb);
  402. if (ret)
  403. pr_warning("Failed to register tracepoint module enter notifier\n");
  404. return ret;
  405. }
  406. __initcall(init_tracepoints);
  407. #endif /* CONFIG_MODULES */
  408. static void for_each_tracepoint_range(struct tracepoint * const *begin,
  409. struct tracepoint * const *end,
  410. void (*fct)(struct tracepoint *tp, void *priv),
  411. void *priv)
  412. {
  413. struct tracepoint * const *iter;
  414. if (!begin)
  415. return;
  416. for (iter = begin; iter < end; iter++)
  417. fct(*iter, priv);
  418. }
  419. /**
  420. * for_each_kernel_tracepoint - iteration on all kernel tracepoints
  421. * @fct: callback
  422. * @priv: private data
  423. */
  424. void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
  425. void *priv)
  426. {
  427. for_each_tracepoint_range(__start___tracepoints_ptrs,
  428. __stop___tracepoints_ptrs, fct, priv);
  429. }
  430. EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
  431. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  432. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  433. static int sys_tracepoint_refcount;
  434. void syscall_regfunc(void)
  435. {
  436. struct task_struct *p, *t;
  437. if (!sys_tracepoint_refcount) {
  438. read_lock(&tasklist_lock);
  439. for_each_process_thread(p, t) {
  440. set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  441. }
  442. read_unlock(&tasklist_lock);
  443. }
  444. sys_tracepoint_refcount++;
  445. }
  446. void syscall_unregfunc(void)
  447. {
  448. struct task_struct *p, *t;
  449. sys_tracepoint_refcount--;
  450. if (!sys_tracepoint_refcount) {
  451. read_lock(&tasklist_lock);
  452. for_each_process_thread(p, t) {
  453. clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  454. }
  455. read_unlock(&tasklist_lock);
  456. }
  457. }
  458. #endif