tracepoint.c 17 KB

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