tracepoint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. 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(
  331. tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
  332. void (*fct)(struct tracepoint *tp, void *priv),
  333. void *priv)
  334. {
  335. tracepoint_ptr_t *iter;
  336. if (!begin)
  337. return;
  338. for (iter = begin; iter < end; iter++)
  339. fct(tracepoint_ptr_deref(iter), priv);
  340. }
  341. #ifdef CONFIG_MODULES
  342. bool trace_module_has_bad_taint(struct module *mod)
  343. {
  344. return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
  345. (1 << TAINT_UNSIGNED_MODULE));
  346. }
  347. static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
  348. /**
  349. * register_tracepoint_notifier - register tracepoint coming/going notifier
  350. * @nb: notifier block
  351. *
  352. * Notifiers registered with this function are called on module
  353. * coming/going with the tracepoint_module_list_mutex held.
  354. * The notifier block callback should expect a "struct tp_module" data
  355. * pointer.
  356. */
  357. int register_tracepoint_module_notifier(struct notifier_block *nb)
  358. {
  359. struct tp_module *tp_mod;
  360. int ret;
  361. mutex_lock(&tracepoint_module_list_mutex);
  362. ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
  363. if (ret)
  364. goto end;
  365. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  366. (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
  367. end:
  368. mutex_unlock(&tracepoint_module_list_mutex);
  369. return ret;
  370. }
  371. EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
  372. /**
  373. * unregister_tracepoint_notifier - unregister tracepoint coming/going notifier
  374. * @nb: notifier block
  375. *
  376. * The notifier block callback should expect a "struct tp_module" data
  377. * pointer.
  378. */
  379. int unregister_tracepoint_module_notifier(struct notifier_block *nb)
  380. {
  381. struct tp_module *tp_mod;
  382. int ret;
  383. mutex_lock(&tracepoint_module_list_mutex);
  384. ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
  385. if (ret)
  386. goto end;
  387. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  388. (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
  389. end:
  390. mutex_unlock(&tracepoint_module_list_mutex);
  391. return ret;
  392. }
  393. EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
  394. /*
  395. * Ensure the tracer unregistered the module's probes before the module
  396. * teardown is performed. Prevents leaks of probe and data pointers.
  397. */
  398. static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
  399. {
  400. WARN_ON_ONCE(tp->funcs);
  401. }
  402. static int tracepoint_module_coming(struct module *mod)
  403. {
  404. struct tp_module *tp_mod;
  405. int ret = 0;
  406. if (!mod->num_tracepoints)
  407. return 0;
  408. /*
  409. * We skip modules that taint the kernel, especially those with different
  410. * module headers (for forced load), to make sure we don't cause a crash.
  411. * Staging, out-of-tree, and unsigned GPL modules are fine.
  412. */
  413. if (trace_module_has_bad_taint(mod))
  414. return 0;
  415. mutex_lock(&tracepoint_module_list_mutex);
  416. tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
  417. if (!tp_mod) {
  418. ret = -ENOMEM;
  419. goto end;
  420. }
  421. tp_mod->mod = mod;
  422. list_add_tail(&tp_mod->list, &tracepoint_module_list);
  423. blocking_notifier_call_chain(&tracepoint_notify_list,
  424. MODULE_STATE_COMING, tp_mod);
  425. end:
  426. mutex_unlock(&tracepoint_module_list_mutex);
  427. return ret;
  428. }
  429. static void tracepoint_module_going(struct module *mod)
  430. {
  431. struct tp_module *tp_mod;
  432. if (!mod->num_tracepoints)
  433. return;
  434. mutex_lock(&tracepoint_module_list_mutex);
  435. list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
  436. if (tp_mod->mod == mod) {
  437. blocking_notifier_call_chain(&tracepoint_notify_list,
  438. MODULE_STATE_GOING, tp_mod);
  439. list_del(&tp_mod->list);
  440. kfree(tp_mod);
  441. /*
  442. * Called the going notifier before checking for
  443. * quiescence.
  444. */
  445. for_each_tracepoint_range(mod->tracepoints_ptrs,
  446. mod->tracepoints_ptrs + mod->num_tracepoints,
  447. tp_module_going_check_quiescent, NULL);
  448. break;
  449. }
  450. }
  451. /*
  452. * In the case of modules that were tainted at "coming", we'll simply
  453. * walk through the list without finding it. We cannot use the "tainted"
  454. * flag on "going", in case a module taints the kernel only after being
  455. * loaded.
  456. */
  457. mutex_unlock(&tracepoint_module_list_mutex);
  458. }
  459. static int tracepoint_module_notify(struct notifier_block *self,
  460. unsigned long val, void *data)
  461. {
  462. struct module *mod = data;
  463. int ret = 0;
  464. switch (val) {
  465. case MODULE_STATE_COMING:
  466. ret = tracepoint_module_coming(mod);
  467. break;
  468. case MODULE_STATE_LIVE:
  469. break;
  470. case MODULE_STATE_GOING:
  471. tracepoint_module_going(mod);
  472. break;
  473. case MODULE_STATE_UNFORMED:
  474. break;
  475. }
  476. return ret;
  477. }
  478. static struct notifier_block tracepoint_module_nb = {
  479. .notifier_call = tracepoint_module_notify,
  480. .priority = 0,
  481. };
  482. static __init int init_tracepoints(void)
  483. {
  484. int ret;
  485. ret = register_module_notifier(&tracepoint_module_nb);
  486. if (ret)
  487. pr_warn("Failed to register tracepoint module enter notifier\n");
  488. return ret;
  489. }
  490. __initcall(init_tracepoints);
  491. #endif /* CONFIG_MODULES */
  492. /**
  493. * for_each_kernel_tracepoint - iteration on all kernel tracepoints
  494. * @fct: callback
  495. * @priv: private data
  496. */
  497. void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
  498. void *priv)
  499. {
  500. for_each_tracepoint_range(__start___tracepoints_ptrs,
  501. __stop___tracepoints_ptrs, fct, priv);
  502. }
  503. EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
  504. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  505. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  506. static int sys_tracepoint_refcount;
  507. int syscall_regfunc(void)
  508. {
  509. struct task_struct *p, *t;
  510. if (!sys_tracepoint_refcount) {
  511. read_lock(&tasklist_lock);
  512. for_each_process_thread(p, t) {
  513. set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  514. }
  515. read_unlock(&tasklist_lock);
  516. }
  517. sys_tracepoint_refcount++;
  518. return 0;
  519. }
  520. void syscall_unregfunc(void)
  521. {
  522. struct task_struct *p, *t;
  523. sys_tracepoint_refcount--;
  524. if (!sys_tracepoint_refcount) {
  525. read_lock(&tasklist_lock);
  526. for_each_process_thread(p, t) {
  527. clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  528. }
  529. read_unlock(&tasklist_lock);
  530. }
  531. }
  532. #endif