tracepoint.c 16 KB

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