tracepoint.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2008-2014 Mathieu Desnoyers
  4. */
  5. #include <linux/module.h>
  6. #include <linux/mutex.h>
  7. #include <linux/types.h>
  8. #include <linux/jhash.h>
  9. #include <linux/list.h>
  10. #include <linux/rcupdate.h>
  11. #include <linux/tracepoint.h>
  12. #include <linux/err.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/sched/task.h>
  16. #include <linux/static_key.h>
  17. enum tp_func_state {
  18. TP_FUNC_0,
  19. TP_FUNC_1,
  20. TP_FUNC_2,
  21. TP_FUNC_N,
  22. };
  23. extern tracepoint_ptr_t __start___tracepoints_ptrs[];
  24. extern tracepoint_ptr_t __stop___tracepoints_ptrs[];
  25. DEFINE_SRCU(tracepoint_srcu);
  26. EXPORT_SYMBOL_GPL(tracepoint_srcu);
  27. enum tp_transition_sync {
  28. TP_TRANSITION_SYNC_1_0_1,
  29. TP_TRANSITION_SYNC_N_2_1,
  30. _NR_TP_TRANSITION_SYNC,
  31. };
  32. struct tp_transition_snapshot {
  33. unsigned long rcu;
  34. unsigned long srcu;
  35. bool ongoing;
  36. };
  37. /* Protected by tracepoints_mutex */
  38. static struct tp_transition_snapshot tp_transition_snapshot[_NR_TP_TRANSITION_SYNC];
  39. static void tp_rcu_get_state(enum tp_transition_sync sync)
  40. {
  41. struct tp_transition_snapshot *snapshot = &tp_transition_snapshot[sync];
  42. /* Keep the latest get_state snapshot. */
  43. snapshot->rcu = get_state_synchronize_rcu();
  44. snapshot->srcu = start_poll_synchronize_srcu(&tracepoint_srcu);
  45. snapshot->ongoing = true;
  46. }
  47. static void tp_rcu_cond_sync(enum tp_transition_sync sync)
  48. {
  49. struct tp_transition_snapshot *snapshot = &tp_transition_snapshot[sync];
  50. if (!snapshot->ongoing)
  51. return;
  52. cond_synchronize_rcu(snapshot->rcu);
  53. if (!poll_state_synchronize_srcu(&tracepoint_srcu, snapshot->srcu))
  54. synchronize_srcu(&tracepoint_srcu);
  55. snapshot->ongoing = false;
  56. }
  57. /* Set to 1 to enable tracepoint debug output */
  58. static const int tracepoint_debug;
  59. #ifdef CONFIG_MODULES
  60. /*
  61. * Tracepoint module list mutex protects the local module list.
  62. */
  63. static DEFINE_MUTEX(tracepoint_module_list_mutex);
  64. /* Local list of struct tp_module */
  65. static LIST_HEAD(tracepoint_module_list);
  66. #endif /* CONFIG_MODULES */
  67. /*
  68. * tracepoints_mutex protects the builtin and module tracepoints.
  69. * tracepoints_mutex nests inside tracepoint_module_list_mutex.
  70. */
  71. static DEFINE_MUTEX(tracepoints_mutex);
  72. static struct rcu_head *early_probes;
  73. static bool ok_to_free_tracepoints;
  74. /*
  75. * Note about RCU :
  76. * It is used to delay the free of multiple probes array until a quiescent
  77. * state is reached.
  78. */
  79. struct tp_probes {
  80. struct rcu_head rcu;
  81. struct tracepoint_func probes[];
  82. };
  83. /* Called in removal of a func but failed to allocate a new tp_funcs */
  84. static void tp_stub_func(void)
  85. {
  86. return;
  87. }
  88. static inline void *allocate_probes(int count)
  89. {
  90. struct tp_probes *p = kmalloc(struct_size(p, probes, count),
  91. GFP_KERNEL);
  92. return p == NULL ? NULL : p->probes;
  93. }
  94. static void srcu_free_old_probes(struct rcu_head *head)
  95. {
  96. kfree(container_of(head, struct tp_probes, rcu));
  97. }
  98. static void rcu_free_old_probes(struct rcu_head *head)
  99. {
  100. call_srcu(&tracepoint_srcu, head, srcu_free_old_probes);
  101. }
  102. static __init int release_early_probes(void)
  103. {
  104. struct rcu_head *tmp;
  105. ok_to_free_tracepoints = true;
  106. while (early_probes) {
  107. tmp = early_probes;
  108. early_probes = tmp->next;
  109. call_rcu(tmp, rcu_free_old_probes);
  110. }
  111. return 0;
  112. }
  113. /* SRCU is initialized at core_initcall */
  114. postcore_initcall(release_early_probes);
  115. static inline void release_probes(struct tracepoint_func *old)
  116. {
  117. if (old) {
  118. struct tp_probes *tp_probes = container_of(old,
  119. struct tp_probes, probes[0]);
  120. /*
  121. * We can't free probes if SRCU is not initialized yet.
  122. * Postpone the freeing till after SRCU is initialized.
  123. */
  124. if (unlikely(!ok_to_free_tracepoints)) {
  125. tp_probes->rcu.next = early_probes;
  126. early_probes = &tp_probes->rcu;
  127. return;
  128. }
  129. /*
  130. * Tracepoint probes are protected by both sched RCU and SRCU,
  131. * by calling the SRCU callback in the sched RCU callback we
  132. * cover both cases. So let us chain the SRCU and sched RCU
  133. * callbacks to wait for both grace periods.
  134. */
  135. call_rcu(&tp_probes->rcu, rcu_free_old_probes);
  136. }
  137. }
  138. static void debug_print_probes(struct tracepoint_func *funcs)
  139. {
  140. int i;
  141. if (!tracepoint_debug || !funcs)
  142. return;
  143. for (i = 0; funcs[i].func; i++)
  144. printk(KERN_DEBUG "Probe %d : %p\n", i, funcs[i].func);
  145. }
  146. static struct tracepoint_func *
  147. func_add(struct tracepoint_func **funcs, struct tracepoint_func *tp_func,
  148. int prio)
  149. {
  150. struct tracepoint_func *old, *new;
  151. int iter_probes; /* Iterate over old probe array. */
  152. int nr_probes = 0; /* Counter for probes */
  153. int pos = -1; /* Insertion position into new array */
  154. if (WARN_ON(!tp_func->func))
  155. return ERR_PTR(-EINVAL);
  156. debug_print_probes(*funcs);
  157. old = *funcs;
  158. if (old) {
  159. /* (N -> N+1), (N != 0, 1) probes */
  160. for (iter_probes = 0; old[iter_probes].func; iter_probes++) {
  161. if (old[iter_probes].func == tp_stub_func)
  162. continue; /* Skip stub functions. */
  163. if (old[iter_probes].func == tp_func->func &&
  164. old[iter_probes].data == tp_func->data)
  165. return ERR_PTR(-EEXIST);
  166. nr_probes++;
  167. }
  168. }
  169. /* + 2 : one for new probe, one for NULL func */
  170. new = allocate_probes(nr_probes + 2);
  171. if (new == NULL)
  172. return ERR_PTR(-ENOMEM);
  173. if (old) {
  174. nr_probes = 0;
  175. for (iter_probes = 0; old[iter_probes].func; iter_probes++) {
  176. if (old[iter_probes].func == tp_stub_func)
  177. continue;
  178. /* Insert before probes of lower priority */
  179. if (pos < 0 && old[iter_probes].prio < prio)
  180. pos = nr_probes++;
  181. new[nr_probes++] = old[iter_probes];
  182. }
  183. if (pos < 0)
  184. pos = nr_probes++;
  185. /* nr_probes now points to the end of the new array */
  186. } else {
  187. pos = 0;
  188. nr_probes = 1; /* must point at end of array */
  189. }
  190. new[pos] = *tp_func;
  191. new[nr_probes].func = NULL;
  192. *funcs = new;
  193. debug_print_probes(*funcs);
  194. return old;
  195. }
  196. static void *func_remove(struct tracepoint_func **funcs,
  197. struct tracepoint_func *tp_func)
  198. {
  199. int nr_probes = 0, nr_del = 0, i;
  200. struct tracepoint_func *old, *new;
  201. old = *funcs;
  202. if (!old)
  203. return ERR_PTR(-ENOENT);
  204. debug_print_probes(*funcs);
  205. /* (N -> M), (N > 1, M >= 0) probes */
  206. if (tp_func->func) {
  207. for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
  208. if ((old[nr_probes].func == tp_func->func &&
  209. old[nr_probes].data == tp_func->data) ||
  210. old[nr_probes].func == tp_stub_func)
  211. nr_del++;
  212. }
  213. }
  214. /*
  215. * If probe is NULL, then nr_probes = nr_del = 0, and then the
  216. * entire entry will be removed.
  217. */
  218. if (nr_probes - nr_del == 0) {
  219. /* N -> 0, (N > 1) */
  220. *funcs = NULL;
  221. debug_print_probes(*funcs);
  222. return old;
  223. } else {
  224. int j = 0;
  225. /* N -> M, (N > 1, M > 0) */
  226. /* + 1 for NULL */
  227. new = allocate_probes(nr_probes - nr_del + 1);
  228. if (new) {
  229. for (i = 0; old[i].func; i++) {
  230. if ((old[i].func != tp_func->func ||
  231. old[i].data != tp_func->data) &&
  232. old[i].func != tp_stub_func)
  233. new[j++] = old[i];
  234. }
  235. new[nr_probes - nr_del].func = NULL;
  236. *funcs = new;
  237. } else {
  238. /*
  239. * Failed to allocate, replace the old function
  240. * with calls to tp_stub_func.
  241. */
  242. for (i = 0; old[i].func; i++) {
  243. if (old[i].func == tp_func->func &&
  244. old[i].data == tp_func->data)
  245. WRITE_ONCE(old[i].func, tp_stub_func);
  246. }
  247. *funcs = old;
  248. }
  249. }
  250. debug_print_probes(*funcs);
  251. return old;
  252. }
  253. /*
  254. * Count the number of functions (enum tp_func_state) in a tp_funcs array.
  255. */
  256. static enum tp_func_state nr_func_state(const struct tracepoint_func *tp_funcs)
  257. {
  258. if (!tp_funcs)
  259. return TP_FUNC_0;
  260. if (!tp_funcs[1].func)
  261. return TP_FUNC_1;
  262. if (!tp_funcs[2].func)
  263. return TP_FUNC_2;
  264. return TP_FUNC_N; /* 3 or more */
  265. }
  266. static void tracepoint_update_call(struct tracepoint *tp, struct tracepoint_func *tp_funcs)
  267. {
  268. void *func = tp->iterator;
  269. /* Synthetic events do not have static call sites */
  270. if (!tp->static_call_key)
  271. return;
  272. if (nr_func_state(tp_funcs) == TP_FUNC_1)
  273. func = tp_funcs[0].func;
  274. __static_call_update(tp->static_call_key, tp->static_call_tramp, func);
  275. }
  276. /*
  277. * Add the probe function to a tracepoint.
  278. */
  279. static int tracepoint_add_func(struct tracepoint *tp,
  280. struct tracepoint_func *func, int prio,
  281. bool warn)
  282. {
  283. struct tracepoint_func *old, *tp_funcs;
  284. int ret;
  285. if (tp->regfunc && !static_key_enabled(&tp->key)) {
  286. ret = tp->regfunc();
  287. if (ret < 0)
  288. return ret;
  289. }
  290. tp_funcs = rcu_dereference_protected(tp->funcs,
  291. lockdep_is_held(&tracepoints_mutex));
  292. old = func_add(&tp_funcs, func, prio);
  293. if (IS_ERR(old)) {
  294. WARN_ON_ONCE(warn && PTR_ERR(old) != -ENOMEM);
  295. return PTR_ERR(old);
  296. }
  297. /*
  298. * rcu_assign_pointer has as smp_store_release() which makes sure
  299. * that the new probe callbacks array is consistent before setting
  300. * a pointer to it. This array is referenced by __DO_TRACE from
  301. * include/linux/tracepoint.h using rcu_dereference_sched().
  302. */
  303. switch (nr_func_state(tp_funcs)) {
  304. case TP_FUNC_1: /* 0->1 */
  305. /*
  306. * Make sure new static func never uses old data after a
  307. * 1->0->1 transition sequence.
  308. */
  309. tp_rcu_cond_sync(TP_TRANSITION_SYNC_1_0_1);
  310. /* Set static call to first function */
  311. tracepoint_update_call(tp, tp_funcs);
  312. /* Both iterator and static call handle NULL tp->funcs */
  313. rcu_assign_pointer(tp->funcs, tp_funcs);
  314. static_key_enable(&tp->key);
  315. break;
  316. case TP_FUNC_2: /* 1->2 */
  317. /* Set iterator static call */
  318. tracepoint_update_call(tp, tp_funcs);
  319. /*
  320. * Iterator callback installed before updating tp->funcs.
  321. * Requires ordering between RCU assign/dereference and
  322. * static call update/call.
  323. */
  324. fallthrough;
  325. case TP_FUNC_N: /* N->N+1 (N>1) */
  326. rcu_assign_pointer(tp->funcs, tp_funcs);
  327. /*
  328. * Make sure static func never uses incorrect data after a
  329. * N->...->2->1 (N>1) transition sequence.
  330. */
  331. if (tp_funcs[0].data != old[0].data)
  332. tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1);
  333. break;
  334. default:
  335. WARN_ON_ONCE(1);
  336. break;
  337. }
  338. release_probes(old);
  339. return 0;
  340. }
  341. /*
  342. * Remove a probe function from a tracepoint.
  343. * Note: only waiting an RCU period after setting elem->call to the empty
  344. * function insures that the original callback is not used anymore. This insured
  345. * by preempt_disable around the call site.
  346. */
  347. static int tracepoint_remove_func(struct tracepoint *tp,
  348. struct tracepoint_func *func)
  349. {
  350. struct tracepoint_func *old, *tp_funcs;
  351. tp_funcs = rcu_dereference_protected(tp->funcs,
  352. lockdep_is_held(&tracepoints_mutex));
  353. old = func_remove(&tp_funcs, func);
  354. if (WARN_ON_ONCE(IS_ERR(old)))
  355. return PTR_ERR(old);
  356. if (tp_funcs == old)
  357. /* Failed allocating new tp_funcs, replaced func with stub */
  358. return 0;
  359. switch (nr_func_state(tp_funcs)) {
  360. case TP_FUNC_0: /* 1->0 */
  361. /* Removed last function */
  362. if (tp->unregfunc && static_key_enabled(&tp->key))
  363. tp->unregfunc();
  364. static_key_disable(&tp->key);
  365. /* Set iterator static call */
  366. tracepoint_update_call(tp, tp_funcs);
  367. /* Both iterator and static call handle NULL tp->funcs */
  368. rcu_assign_pointer(tp->funcs, NULL);
  369. /*
  370. * Make sure new static func never uses old data after a
  371. * 1->0->1 transition sequence.
  372. */
  373. tp_rcu_get_state(TP_TRANSITION_SYNC_1_0_1);
  374. break;
  375. case TP_FUNC_1: /* 2->1 */
  376. rcu_assign_pointer(tp->funcs, tp_funcs);
  377. /*
  378. * Make sure static func never uses incorrect data after a
  379. * N->...->2->1 (N>2) transition sequence. If the first
  380. * element's data has changed, then force the synchronization
  381. * to prevent current readers that have loaded the old data
  382. * from calling the new function.
  383. */
  384. if (tp_funcs[0].data != old[0].data)
  385. tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1);
  386. tp_rcu_cond_sync(TP_TRANSITION_SYNC_N_2_1);
  387. /* Set static call to first function */
  388. tracepoint_update_call(tp, tp_funcs);
  389. break;
  390. case TP_FUNC_2: /* N->N-1 (N>2) */
  391. fallthrough;
  392. case TP_FUNC_N:
  393. rcu_assign_pointer(tp->funcs, tp_funcs);
  394. /*
  395. * Make sure static func never uses incorrect data after a
  396. * N->...->2->1 (N>2) transition sequence.
  397. */
  398. if (tp_funcs[0].data != old[0].data)
  399. tp_rcu_get_state(TP_TRANSITION_SYNC_N_2_1);
  400. break;
  401. default:
  402. WARN_ON_ONCE(1);
  403. break;
  404. }
  405. release_probes(old);
  406. return 0;
  407. }
  408. /**
  409. * tracepoint_probe_register_prio_may_exist - Connect a probe to a tracepoint with priority
  410. * @tp: tracepoint
  411. * @probe: probe handler
  412. * @data: tracepoint data
  413. * @prio: priority of this function over other registered functions
  414. *
  415. * Same as tracepoint_probe_register_prio() except that it will not warn
  416. * if the tracepoint is already registered.
  417. */
  418. int tracepoint_probe_register_prio_may_exist(struct tracepoint *tp, void *probe,
  419. void *data, int prio)
  420. {
  421. struct tracepoint_func tp_func;
  422. int ret;
  423. mutex_lock(&tracepoints_mutex);
  424. tp_func.func = probe;
  425. tp_func.data = data;
  426. tp_func.prio = prio;
  427. ret = tracepoint_add_func(tp, &tp_func, prio, false);
  428. mutex_unlock(&tracepoints_mutex);
  429. return ret;
  430. }
  431. EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio_may_exist);
  432. /**
  433. * tracepoint_probe_register_prio - Connect a probe to a tracepoint with priority
  434. * @tp: tracepoint
  435. * @probe: probe handler
  436. * @data: tracepoint data
  437. * @prio: priority of this function over other registered functions
  438. *
  439. * Returns 0 if ok, error value on error.
  440. * Note: if @tp is within a module, the caller is responsible for
  441. * unregistering the probe before the module is gone. This can be
  442. * performed either with a tracepoint module going notifier, or from
  443. * within module exit functions.
  444. */
  445. int tracepoint_probe_register_prio(struct tracepoint *tp, void *probe,
  446. void *data, int prio)
  447. {
  448. struct tracepoint_func tp_func;
  449. int ret;
  450. mutex_lock(&tracepoints_mutex);
  451. tp_func.func = probe;
  452. tp_func.data = data;
  453. tp_func.prio = prio;
  454. ret = tracepoint_add_func(tp, &tp_func, prio, true);
  455. mutex_unlock(&tracepoints_mutex);
  456. return ret;
  457. }
  458. EXPORT_SYMBOL_GPL(tracepoint_probe_register_prio);
  459. /**
  460. * tracepoint_probe_register - Connect a probe to a tracepoint
  461. * @tp: tracepoint
  462. * @probe: probe handler
  463. * @data: tracepoint data
  464. *
  465. * Returns 0 if ok, error value on error.
  466. * Note: if @tp is within a module, the caller is responsible for
  467. * unregistering the probe before the module is gone. This can be
  468. * performed either with a tracepoint module going notifier, or from
  469. * within module exit functions.
  470. */
  471. int tracepoint_probe_register(struct tracepoint *tp, void *probe, void *data)
  472. {
  473. return tracepoint_probe_register_prio(tp, probe, data, TRACEPOINT_DEFAULT_PRIO);
  474. }
  475. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  476. /**
  477. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  478. * @tp: tracepoint
  479. * @probe: probe function pointer
  480. * @data: tracepoint data
  481. *
  482. * Returns 0 if ok, error value on error.
  483. */
  484. int tracepoint_probe_unregister(struct tracepoint *tp, void *probe, void *data)
  485. {
  486. struct tracepoint_func tp_func;
  487. int ret;
  488. mutex_lock(&tracepoints_mutex);
  489. tp_func.func = probe;
  490. tp_func.data = data;
  491. ret = tracepoint_remove_func(tp, &tp_func);
  492. mutex_unlock(&tracepoints_mutex);
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  496. static void for_each_tracepoint_range(
  497. tracepoint_ptr_t *begin, tracepoint_ptr_t *end,
  498. void (*fct)(struct tracepoint *tp, void *priv),
  499. void *priv)
  500. {
  501. tracepoint_ptr_t *iter;
  502. if (!begin)
  503. return;
  504. for (iter = begin; iter < end; iter++)
  505. fct(tracepoint_ptr_deref(iter), priv);
  506. }
  507. #ifdef CONFIG_MODULES
  508. bool trace_module_has_bad_taint(struct module *mod)
  509. {
  510. return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP) |
  511. (1 << TAINT_UNSIGNED_MODULE) |
  512. (1 << TAINT_TEST));
  513. }
  514. static BLOCKING_NOTIFIER_HEAD(tracepoint_notify_list);
  515. /**
  516. * register_tracepoint_module_notifier - register tracepoint coming/going notifier
  517. * @nb: notifier block
  518. *
  519. * Notifiers registered with this function are called on module
  520. * coming/going with the tracepoint_module_list_mutex held.
  521. * The notifier block callback should expect a "struct tp_module" data
  522. * pointer.
  523. */
  524. int register_tracepoint_module_notifier(struct notifier_block *nb)
  525. {
  526. struct tp_module *tp_mod;
  527. int ret;
  528. mutex_lock(&tracepoint_module_list_mutex);
  529. ret = blocking_notifier_chain_register(&tracepoint_notify_list, nb);
  530. if (ret)
  531. goto end;
  532. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  533. (void) nb->notifier_call(nb, MODULE_STATE_COMING, tp_mod);
  534. end:
  535. mutex_unlock(&tracepoint_module_list_mutex);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL_GPL(register_tracepoint_module_notifier);
  539. /**
  540. * unregister_tracepoint_module_notifier - unregister tracepoint coming/going notifier
  541. * @nb: notifier block
  542. *
  543. * The notifier block callback should expect a "struct tp_module" data
  544. * pointer.
  545. */
  546. int unregister_tracepoint_module_notifier(struct notifier_block *nb)
  547. {
  548. struct tp_module *tp_mod;
  549. int ret;
  550. mutex_lock(&tracepoint_module_list_mutex);
  551. ret = blocking_notifier_chain_unregister(&tracepoint_notify_list, nb);
  552. if (ret)
  553. goto end;
  554. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  555. (void) nb->notifier_call(nb, MODULE_STATE_GOING, tp_mod);
  556. end:
  557. mutex_unlock(&tracepoint_module_list_mutex);
  558. return ret;
  559. }
  560. EXPORT_SYMBOL_GPL(unregister_tracepoint_module_notifier);
  561. /*
  562. * Ensure the tracer unregistered the module's probes before the module
  563. * teardown is performed. Prevents leaks of probe and data pointers.
  564. */
  565. static void tp_module_going_check_quiescent(struct tracepoint *tp, void *priv)
  566. {
  567. WARN_ON_ONCE(tp->funcs);
  568. }
  569. static int tracepoint_module_coming(struct module *mod)
  570. {
  571. struct tp_module *tp_mod;
  572. if (!mod->num_tracepoints)
  573. return 0;
  574. /*
  575. * We skip modules that taint the kernel, especially those with different
  576. * module headers (for forced load), to make sure we don't cause a crash.
  577. * Staging, out-of-tree, unsigned GPL, and test modules are fine.
  578. */
  579. if (trace_module_has_bad_taint(mod))
  580. return 0;
  581. tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
  582. if (!tp_mod)
  583. return -ENOMEM;
  584. tp_mod->mod = mod;
  585. mutex_lock(&tracepoint_module_list_mutex);
  586. list_add_tail(&tp_mod->list, &tracepoint_module_list);
  587. blocking_notifier_call_chain(&tracepoint_notify_list,
  588. MODULE_STATE_COMING, tp_mod);
  589. mutex_unlock(&tracepoint_module_list_mutex);
  590. return 0;
  591. }
  592. static void tracepoint_module_going(struct module *mod)
  593. {
  594. struct tp_module *tp_mod;
  595. if (!mod->num_tracepoints)
  596. return;
  597. mutex_lock(&tracepoint_module_list_mutex);
  598. list_for_each_entry(tp_mod, &tracepoint_module_list, list) {
  599. if (tp_mod->mod == mod) {
  600. blocking_notifier_call_chain(&tracepoint_notify_list,
  601. MODULE_STATE_GOING, tp_mod);
  602. list_del(&tp_mod->list);
  603. kfree(tp_mod);
  604. /*
  605. * Called the going notifier before checking for
  606. * quiescence.
  607. */
  608. for_each_tracepoint_range(mod->tracepoints_ptrs,
  609. mod->tracepoints_ptrs + mod->num_tracepoints,
  610. tp_module_going_check_quiescent, NULL);
  611. break;
  612. }
  613. }
  614. /*
  615. * In the case of modules that were tainted at "coming", we'll simply
  616. * walk through the list without finding it. We cannot use the "tainted"
  617. * flag on "going", in case a module taints the kernel only after being
  618. * loaded.
  619. */
  620. mutex_unlock(&tracepoint_module_list_mutex);
  621. }
  622. static int tracepoint_module_notify(struct notifier_block *self,
  623. unsigned long val, void *data)
  624. {
  625. struct module *mod = data;
  626. int ret = 0;
  627. switch (val) {
  628. case MODULE_STATE_COMING:
  629. ret = tracepoint_module_coming(mod);
  630. break;
  631. case MODULE_STATE_LIVE:
  632. break;
  633. case MODULE_STATE_GOING:
  634. tracepoint_module_going(mod);
  635. break;
  636. case MODULE_STATE_UNFORMED:
  637. break;
  638. }
  639. return notifier_from_errno(ret);
  640. }
  641. static struct notifier_block tracepoint_module_nb = {
  642. .notifier_call = tracepoint_module_notify,
  643. .priority = 0,
  644. };
  645. static __init int init_tracepoints(void)
  646. {
  647. int ret;
  648. ret = register_module_notifier(&tracepoint_module_nb);
  649. if (ret)
  650. pr_warn("Failed to register tracepoint module enter notifier\n");
  651. return ret;
  652. }
  653. __initcall(init_tracepoints);
  654. #endif /* CONFIG_MODULES */
  655. /**
  656. * for_each_kernel_tracepoint - iteration on all kernel tracepoints
  657. * @fct: callback
  658. * @priv: private data
  659. */
  660. void for_each_kernel_tracepoint(void (*fct)(struct tracepoint *tp, void *priv),
  661. void *priv)
  662. {
  663. for_each_tracepoint_range(__start___tracepoints_ptrs,
  664. __stop___tracepoints_ptrs, fct, priv);
  665. }
  666. EXPORT_SYMBOL_GPL(for_each_kernel_tracepoint);
  667. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  668. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  669. static int sys_tracepoint_refcount;
  670. int syscall_regfunc(void)
  671. {
  672. struct task_struct *p, *t;
  673. if (!sys_tracepoint_refcount) {
  674. read_lock(&tasklist_lock);
  675. for_each_process_thread(p, t) {
  676. set_task_syscall_work(t, SYSCALL_TRACEPOINT);
  677. }
  678. read_unlock(&tasklist_lock);
  679. }
  680. sys_tracepoint_refcount++;
  681. return 0;
  682. }
  683. void syscall_unregfunc(void)
  684. {
  685. struct task_struct *p, *t;
  686. sys_tracepoint_refcount--;
  687. if (!sys_tracepoint_refcount) {
  688. read_lock(&tasklist_lock);
  689. for_each_process_thread(p, t) {
  690. clear_task_syscall_work(t, SYSCALL_TRACEPOINT);
  691. }
  692. read_unlock(&tasklist_lock);
  693. }
  694. }
  695. #endif
  696. #ifdef CONFIG_ANDROID_VENDOR_HOOKS
  697. static void *rvh_zalloc_funcs(int count)
  698. {
  699. return kzalloc(sizeof(struct tracepoint_func) * count, GFP_KERNEL);
  700. }
  701. #define ANDROID_RVH_NR_PROBES_MAX 2
  702. static int rvh_func_add(struct tracepoint *tp, struct tracepoint_func *func)
  703. {
  704. int i;
  705. if (!static_key_enabled(&tp->key)) {
  706. /* '+ 1' for the last NULL element */
  707. tp->funcs = rvh_zalloc_funcs(ANDROID_RVH_NR_PROBES_MAX + 1);
  708. if (!tp->funcs)
  709. return ENOMEM;
  710. }
  711. for (i = 0; i < ANDROID_RVH_NR_PROBES_MAX; i++) {
  712. if (!tp->funcs[i].func) {
  713. if (!static_key_enabled(&tp->key))
  714. tp->funcs[i].data = func->data;
  715. WRITE_ONCE(tp->funcs[i].func, func->func);
  716. return 0;
  717. }
  718. }
  719. return -EBUSY;
  720. }
  721. static int android_rvh_add_func(struct tracepoint *tp, struct tracepoint_func *func)
  722. {
  723. int ret;
  724. if (tp->regfunc && !static_key_enabled(&tp->key)) {
  725. ret = tp->regfunc();
  726. if (ret < 0)
  727. return ret;
  728. }
  729. ret = rvh_func_add(tp, func);
  730. if (ret)
  731. return ret;
  732. tracepoint_update_call(tp, tp->funcs);
  733. static_key_enable(&tp->key);
  734. return 0;
  735. }
  736. int android_rvh_probe_register(struct tracepoint *tp, void *probe, void *data)
  737. {
  738. struct tracepoint_func tp_func;
  739. int ret;
  740. /*
  741. * Once the static key has been flipped, the array may be read
  742. * concurrently. Although __traceiter_*() always checks .func first,
  743. * it doesn't enforce read->read dependencies, and we can't strongly
  744. * guarantee it will see the correct .data for the second element
  745. * without adding smp_load_acquire() in the fast path. But this is a
  746. * corner case which is unlikely to be needed by anybody in practice,
  747. * so let's just forbid it and keep the fast path clean.
  748. */
  749. if (WARN_ON(static_key_enabled(&tp->key) && data))
  750. return -EINVAL;
  751. mutex_lock(&tracepoints_mutex);
  752. tp_func.func = probe;
  753. tp_func.data = data;
  754. ret = android_rvh_add_func(tp, &tp_func);
  755. mutex_unlock(&tracepoints_mutex);
  756. return ret;
  757. }
  758. EXPORT_SYMBOL_GPL(android_rvh_probe_register);
  759. #endif