tracepoint.c 15 KB

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