tracepoint.c 15 KB

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