tracepoint.c 15 KB

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