tracepoint.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 *
  82. func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
  83. int prio)
  84. {
  85. struct tracepoint_func *old, *new;
  86. int nr_probes = 0;
  87. int pos = -1;
  88. if (WARN_ON(!tp_func->func))
  89. return ERR_PTR(-EINVAL);
  90. debug_print_probes(*funcs);
  91. old = *funcs;
  92. if (old) {
  93. /* (N -> N+1), (N != 0, 1) probes */
  94. for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
  95. /* Insert before probes of lower priority */
  96. if (pos < 0 && old[nr_probes].prio < prio)
  97. pos = nr_probes;
  98. if (old[nr_probes].func == tp_func->func &&
  99. old[nr_probes].data == tp_func->data)
  100. return ERR_PTR(-EEXIST);
  101. }
  102. }
  103. /* + 2 : one for new probe, one for NULL func */
  104. new = allocate_probes(nr_probes + 2);
  105. if (new == NULL)
  106. return ERR_PTR(-ENOMEM);
  107. if (old) {
  108. if (pos < 0) {
  109. pos = nr_probes;
  110. memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
  111. } else {
  112. /* Copy higher priority probes ahead of the new probe */
  113. memcpy(new, old, pos * sizeof(struct tracepoint_func));
  114. /* Copy the rest after it. */
  115. memcpy(new + pos + 1, old + pos,
  116. (nr_probes - pos) * sizeof(struct tracepoint_func));
  117. }
  118. } else
  119. pos = 0;
  120. new[pos] = *tp_func;
  121. new[nr_probes + 1].func = NULL;
  122. *funcs = new;
  123. debug_print_probes(*funcs);
  124. return old;
  125. }
  126. static void *func_remove(struct tracepoint_func **funcs,
  127. struct tracepoint_func *tp_func)
  128. {
  129. int nr_probes = 0, nr_del = 0, i;
  130. struct tracepoint_func *old, *new;
  131. old = *funcs;
  132. if (!old)
  133. return ERR_PTR(-ENOENT);
  134. debug_print_probes(*funcs);
  135. /* (N -> M), (N > 1, M >= 0) probes */
  136. if (tp_func->func) {
  137. for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
  138. if (old[nr_probes].func == tp_func->func &&
  139. old[nr_probes].data == tp_func->data)
  140. nr_del++;
  141. }
  142. }
  143. /*
  144. * If probe is NULL, then nr_probes = nr_del = 0, and then the
  145. * entire entry will be removed.
  146. */
  147. if (nr_probes - nr_del == 0) {
  148. /* N -> 0, (N > 1) */
  149. *funcs = NULL;
  150. debug_print_probes(*funcs);
  151. return old;
  152. } else {
  153. int j = 0;
  154. /* N -> M, (N > 1, M > 0) */
  155. /* + 1 for NULL */
  156. new = allocate_probes(nr_probes - nr_del + 1);
  157. if (new == NULL)
  158. return ERR_PTR(-ENOMEM);
  159. for (i = 0; old[i].func; i++)
  160. if (old[i].func != tp_func->func
  161. || old[i].data != tp_func->data)
  162. new[j++] = old[i];
  163. new[nr_probes - nr_del].func = NULL;
  164. *funcs = new;
  165. }
  166. debug_print_probes(*funcs);
  167. return old;
  168. }
  169. /*
  170. * Add the probe function to a tracepoint.
  171. */
  172. static int tracepoint_add_func(struct tracepoint *tp,
  173. struct tracepoint_func *func, int prio)
  174. {
  175. struct tracepoint_func *old, *tp_funcs;
  176. if (tp->regfunc && !static_key_enabled(&tp->key))
  177. tp->regfunc();
  178. tp_funcs = rcu_dereference_protected(tp->funcs,
  179. lockdep_is_held(&tracepoints_mutex));
  180. old = func_add(&tp_funcs, func, prio);
  181. if (IS_ERR(old)) {
  182. WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
  183. return PTR_ERR(old);
  184. }
  185. /*
  186. * rcu_assign_pointer has a smp_wmb() which makes sure that the new
  187. * probe callbacks array is consistent before setting a pointer to it.
  188. * This array is referenced by __DO_TRACE from
  189. * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
  190. * is used.
  191. */
  192. rcu_assign_pointer(tp->funcs, tp_funcs);
  193. if (!static_key_enabled(&tp->key))
  194. static_key_slow_inc(&tp->key);
  195. release_probes(old);
  196. return 0;
  197. }
  198. /*
  199. * Remove a probe function from a tracepoint.
  200. * Note: only waiting an RCU period after setting elem->call to the empty
  201. * function insures that the original callback is not used anymore. This insured
  202. * by preempt_disable around the call site.
  203. */
  204. static int tracepoint_remove_func(struct tracepoint *tp,
  205. struct tracepoint_func *func)
  206. {
  207. struct tracepoint_func *old, *tp_funcs;
  208. tp_funcs = rcu_dereference_protected(tp->funcs,
  209. lockdep_is_held(&tracepoints_mutex));
  210. old = func_remove(&tp_funcs, func);
  211. if (IS_ERR(old)) {
  212. WARN_ON_ONCE(PTR_ERR(old) != -ENOMEM);
  213. return PTR_ERR(old);
  214. }
  215. if (!tp_funcs) {
  216. /* Removed last function */
  217. if (tp->unregfunc && static_key_enabled(&tp->key))
  218. tp->unregfunc();
  219. if (static_key_enabled(&tp->key))
  220. static_key_slow_dec(&tp->key);
  221. }
  222. rcu_assign_pointer(tp->funcs, tp_funcs);
  223. release_probes(old);
  224. return 0;
  225. }
  226. /**
  227. * tracepoint_probe_register - Connect a probe to a tracepoint
  228. * @tp: tracepoint
  229. * @probe: probe handler
  230. * @data: tracepoint data
  231. * @prio: priority of this function over other registered functions
  232. *
  233. * Returns 0 if ok, error value on error.
  234. * Note: if @tp is within a module, the caller is responsible for
  235. * unregistering the probe before the module is gone. This can be
  236. * performed either with a tracepoint module going notifier, or from
  237. * within module exit functions.
  238. */
  239. int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
  240. void *data, int prio)
  241. {
  242. struct tracepoint_func tp_func;
  243. int ret;
  244. mutex_lock(&tracepoints_mutex);
  245. tp_func.func = probe;
  246. tp_func.data = data;
  247. tp_func.prio = prio;
  248. ret = tracepoint_add_func(tp, &tp_func, prio);
  249. mutex_unlock(&tracepoints_mutex);
  250. return ret;
  251. }
  252. EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
  253. /**
  254. * tracepoint_probe_register - Connect a probe to a tracepoint
  255. * @tp: tracepoint
  256. * @probe: probe handler
  257. * @data: tracepoint data
  258. * @prio: priority of this function over other registered functions
  259. *
  260. * Returns 0 if ok, error value on error.
  261. * Note: if @tp is within a module, the caller is responsible for
  262. * unregistering the probe before the module is gone. This can be
  263. * performed either with a tracepoint module going notifier, or from
  264. * within module exit functions.
  265. */
  266. int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
  267. {
  268. return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
  269. }
  270. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  271. /**
  272. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  273. * @tp: tracepoint
  274. * @probe: probe function pointer
  275. * @data: tracepoint data
  276. *
  277. * Returns 0 if ok, error value on error.
  278. */
  279. int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
  280. {
  281. struct tracepoint_func tp_func;
  282. int ret;
  283. mutex_lock(&tracepoints_mutex);
  284. tp_func.func = probe;
  285. tp_func.data = data;
  286. ret = tracepoint_remove_func(tp, &tp_func);
  287. mutex_unlock(&tracepoints_mutex);
  288. return ret;
  289. }
  290. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  291. #ifdef CONFIG_MODULES
  292. bool trace_module_has_bad_taint(struct module *mod)
  293. {
  294. return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
  295. (1 << TAINT_UNSIGNED_MODULE));
  296. }
  297. static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
  298. /**
  299. * register_tracepoint_notifier - register tracepoint coming/going notifier
  300. * @nb: notifier block
  301. *
  302. * Notifiers registered with this function are called on module
  303. * coming/going with the tracepoint_module_list_mutex held.
  304. * The notifier block callback should expect a "struct tp_module" data
  305. * pointer.
  306. */
  307. int register_tracepoint_module_notifier(struct notifier_block *nb)
  308. {
  309. struct tp_module *tp_mod;
  310. int ret;
  311. mutex_lock(&tracepoint_module_list_mutex);
  312. ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
  313. if (ret)
  314. goto end;
  315. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  316. (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
  317. end:
  318. mutex_unlock(&tracepoint_module_list_mutex);
  319. return ret;
  320. }
  321. EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
  322. /**
  323. * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
  324. * @nb: notifier block
  325. *
  326. * The notifier block callback should expect a "struct tp_module" data
  327. * pointer.
  328. */
  329. int unregister_tracepoint_module_notifier(struct notifier_block *nb)
  330. {
  331. struct tp_module *tp_mod;
  332. int ret;
  333. mutex_lock(&tracepoint_module_list_mutex);
  334. ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
  335. if (ret)
  336. goto end;
  337. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  338. (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
  339. end:
  340. mutex_unlock(&tracepoint_module_list_mutex);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
  344. /*
  345. * Ensure the tracer unregistered the module's probes before the module
  346. * teardown is performed. Prevents leaks of probe and data pointers.
  347. */
  348. static void tp_module_going_check_quiescent(struct tracepoint * const *begin,
  349. struct tracepoint * const *end)
  350. {
  351. struct tracepoint * const *iter;
  352. if (!begin)
  353. return;
  354. for (iter = begin; iter < end; iter++)
  355. WARN_ON_ONCE((*iter)->funcs);
  356. }
  357. static int tracepoint_module_coming(struct module *mod)
  358. {
  359. struct tp_module *tp_mod;
  360. int ret = 0;
  361. if (!mod->num_tracepoints)
  362. return 0;
  363. /*
  364. * We skip modules that taint the kernel, especially those with different
  365. * module headers (for forced load), to make sure we don't cause a crash.
  366. * Staging, out-of-tree, and unsigned GPL modules are fine.
  367. */
  368. if (trace_module_has_bad_taint(mod))
  369. return 0;
  370. mutex_lock(&tracepoint_module_list_mutex);
  371. tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
  372. if (!tp_mod) {
  373. ret = -ENOMEM;
  374. goto end;
  375. }
  376. tp_mod->mod = mod;
  377. list_add_tail(&tp_mod->list, &tracepoint_module_list);
  378. blocking_notifier_call_chain(&tracepoint_notify_list,
  379. MODULE_STATE_COMING, tp_mod);
  380. end:
  381. mutex_unlock(&tracepoint_module_list_mutex);
  382. return ret;
  383. }
  384. static void tracepoint_module_going(struct module *mod)
  385. {
  386. struct tp_module *tp_mod;
  387. if (!mod->num_tracepoints)
  388. return;
  389. mutex_lock(&tracepoint_module_list_mutex);
  390. list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
  391. if (tp_mod->mod == mod) {
  392. blocking_notifier_call_chain(&tracepoint_notify_list,
  393. MODULE_STATE_GOING, tp_mod);
  394. list_del(&tp_mod->list);
  395. kfree(tp_mod);
  396. /*
  397. * Called the going notifier before checking for
  398. * quiescence.
  399. */
  400. tp_module_going_check_quiescent(mod->tracepoints_ptrs,
  401. mod->tracepoints_ptrs + mod->num_tracepoints);
  402. break;
  403. }
  404. }
  405. /*
  406. * In the case of modules that were tainted at "coming", we'll simply
  407. * walk through the list without finding it. We cannot use the "tainted"
  408. * flag on "going", in case a module taints the kernel only after being
  409. * loaded.
  410. */
  411. mutex_unlock(&tracepoint_module_list_mutex);
  412. }
  413. static int tracepoint_module_notify(struct notifier_block *self,
  414. unsigned long val, void *data)
  415. {
  416. struct module *mod = data;
  417. int ret = 0;
  418. switch (val) {
  419. case MODULE_STATE_COMING:
  420. ret = tracepoint_module_coming(mod);
  421. break;
  422. case MODULE_STATE_LIVE:
  423. break;
  424. case MODULE_STATE_GOING:
  425. tracepoint_module_going(mod);
  426. break;
  427. case MODULE_STATE_UNFORMED:
  428. break;
  429. }
  430. return ret;
  431. }
  432. static struct notifier_block tracepoint_module_nb = {
  433. .notifier_call = tracepoint_module_notify,
  434. .priority = 0,
  435. };
  436. static __init int init_tracepoints(void)
  437. {
  438. int ret;
  439. ret = register_module_notifier(&tracepoint_module_nb);
  440. if (ret)
  441. pr_warn("Failed to register tracepoint module enter notifier\n");
  442. return ret;
  443. }
  444. __initcall(init_tracepoints);
  445. #endif /* CONFIG_MODULES */
  446. static void for_each_tracepoint_range(struct tracepoint * const *begin,
  447. struct tracepoint * const *end,
  448. void (*fct)(struct tracepoint *tp, void *priv),
  449. void *priv)
  450. {
  451. struct tracepoint * const *iter;
  452. if (!begin)
  453. return;
  454. for (iter = begin; iter < end; iter++)
  455. fct(*iter, priv);
  456. }
  457. /**
  458. * for_each_kernel_tracepoint - iteration on all kernel tracepoints
  459. * @fct: callback
  460. * @priv: private data
  461. */
  462. void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
  463. void *priv)
  464. {
  465. for_each_tracepoint_range(__start___tracepoints_ptrs,
  466. __stop___tracepoints_ptrs, fct, priv);
  467. }
  468. EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
  469. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  470. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  471. static int sys_tracepoint_refcount;
  472. void syscall_regfunc(void)
  473. {
  474. struct task_struct *p, *t;
  475. if (!sys_tracepoint_refcount) {
  476. read_lock(&tasklist_lock);
  477. for_each_process_thread(p, t) {
  478. set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  479. }
  480. read_unlock(&tasklist_lock);
  481. }
  482. sys_tracepoint_refcount++;
  483. }
  484. void syscall_unregfunc(void)
  485. {
  486. struct task_struct *p, *t;
  487. sys_tracepoint_refcount--;
  488. if (!sys_tracepoint_refcount) {
  489. read_lock(&tasklist_lock);
  490. for_each_process_thread(p, t) {
  491. clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  492. }
  493. read_unlock(&tasklist_lock);
  494. }
  495. }
  496. #endif