tracepoint.c 16 KB

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