tracepoint.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * Copyright (C) 2008 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.h>
  28. #include <linux/static_key.h>
  29. extern struct tracepoint * const __start___tracepoints_ptrs[];
  30. extern struct tracepoint * const __stop___tracepoints_ptrs[];
  31. /* Set to 1 to enable tracepoint debug output */
  32. static const int tracepoint_debug;
  33. /*
  34. * Tracepoints mutex protects the builtin and module tracepoints and the hash
  35. * table, as well as the local module list.
  36. */
  37. static DEFINE_MUTEX(tracepoints_mutex);
  38. #ifdef CONFIG_MODULES
  39. /* Local list of struct module */
  40. static LIST_HEAD(tracepoint_module_list);
  41. #endif /* CONFIG_MODULES */
  42. /*
  43. * Tracepoint hash table, containing the active tracepoints.
  44. * Protected by tracepoints_mutex.
  45. */
  46. #define TRACEPOINT_HASH_BITS 6
  47. #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
  48. static struct hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE];
  49. /*
  50. * Note about RCU :
  51. * It is used to delay the free of multiple probes array until a quiescent
  52. * state is reached.
  53. * Tracepoint entries modifications are protected by the tracepoints_mutex.
  54. */
  55. struct tracepoint_entry {
  56. struct hlist_node hlist;
  57. struct tracepoint_func *funcs;
  58. int refcount; /* Number of times armed. 0 if disarmed. */
  59. char name[0];
  60. };
  61. struct tp_probes {
  62. union {
  63. struct rcu_head rcu;
  64. struct list_head list;
  65. } u;
  66. struct tracepoint_func probes[0];
  67. };
  68. static inline void *allocate_probes(int count)
  69. {
  70. struct tp_probes *p = kmalloc(count * sizeof(struct tracepoint_func)
  71. + sizeof(struct tp_probes), GFP_KERNEL);
  72. return p == NULL ? NULL : p->probes;
  73. }
  74. static void rcu_free_old_probes(struct rcu_head *head)
  75. {
  76. kfree(container_of(head, struct tp_probes, u.rcu));
  77. }
  78. static inline void release_probes(struct tracepoint_func *old)
  79. {
  80. if (old) {
  81. struct tp_probes *tp_probes = container_of(old,
  82. struct tp_probes, probes[0]);
  83. call_rcu_sched(&tp_probes->u.rcu, rcu_free_old_probes);
  84. }
  85. }
  86. static void debug_print_probes(struct tracepoint_entry *entry)
  87. {
  88. int i;
  89. if (!tracepoint_debug || !entry->funcs)
  90. return;
  91. for (i = 0; entry->funcs[i].func; i++)
  92. printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i].func);
  93. }
  94. static struct tracepoint_func *
  95. tracepoint_entry_add_probe(struct tracepoint_entry *entry,
  96. void *probe, void *data)
  97. {
  98. int nr_probes = 0;
  99. struct tracepoint_func *old, *new;
  100. WARN_ON(!probe);
  101. debug_print_probes(entry);
  102. old = entry->funcs;
  103. if (old) {
  104. /* (N -> N+1), (N != 0, 1) probes */
  105. for (nr_probes = 0; old[nr_probes].func; nr_probes++)
  106. if (old[nr_probes].func == probe &&
  107. old[nr_probes].data == data)
  108. return ERR_PTR(-EEXIST);
  109. }
  110. /* + 2 : one for new probe, one for NULL func */
  111. new = allocate_probes(nr_probes + 2);
  112. if (new == NULL)
  113. return ERR_PTR(-ENOMEM);
  114. if (old)
  115. memcpy(new, old, nr_probes * sizeof(struct tracepoint_func));
  116. new[nr_probes].func = probe;
  117. new[nr_probes].data = data;
  118. new[nr_probes + 1].func = NULL;
  119. entry->refcount = nr_probes + 1;
  120. entry->funcs = new;
  121. debug_print_probes(entry);
  122. return old;
  123. }
  124. static void *
  125. tracepoint_entry_remove_probe(struct tracepoint_entry *entry,
  126. void *probe, void *data)
  127. {
  128. int nr_probes = 0, nr_del = 0, i;
  129. struct tracepoint_func *old, *new;
  130. old = entry->funcs;
  131. if (!old)
  132. return ERR_PTR(-ENOENT);
  133. debug_print_probes(entry);
  134. /* (N -> M), (N > 1, M >= 0) probes */
  135. for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
  136. if (!probe ||
  137. (old[nr_probes].func == probe &&
  138. old[nr_probes].data == data))
  139. nr_del++;
  140. }
  141. if (nr_probes - nr_del == 0) {
  142. /* N -> 0, (N > 1) */
  143. entry->funcs = NULL;
  144. entry->refcount = 0;
  145. debug_print_probes(entry);
  146. return old;
  147. } else {
  148. int j = 0;
  149. /* N -> M, (N > 1, M > 0) */
  150. /* + 1 for NULL */
  151. new = allocate_probes(nr_probes - nr_del + 1);
  152. if (new == NULL)
  153. return ERR_PTR(-ENOMEM);
  154. for (i = 0; old[i].func; i++)
  155. if (probe &&
  156. (old[i].func != probe || old[i].data != data))
  157. new[j++] = old[i];
  158. new[nr_probes - nr_del].func = NULL;
  159. entry->refcount = nr_probes - nr_del;
  160. entry->funcs = new;
  161. }
  162. debug_print_probes(entry);
  163. return old;
  164. }
  165. /*
  166. * Get tracepoint if the tracepoint is present in the tracepoint hash table.
  167. * Must be called with tracepoints_mutex held.
  168. * Returns NULL if not present.
  169. */
  170. static struct tracepoint_entry *get_tracepoint(const char *name)
  171. {
  172. struct hlist_head *head;
  173. struct hlist_node *node;
  174. struct tracepoint_entry *e;
  175. u32 hash = jhash(name, strlen(name), 0);
  176. head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
  177. hlist_for_each_entry(e, node, head, hlist) {
  178. if (!strcmp(name, e->name))
  179. return e;
  180. }
  181. return NULL;
  182. }
  183. /*
  184. * Add the tracepoint to the tracepoint hash table. Must be called with
  185. * tracepoints_mutex held.
  186. */
  187. static struct tracepoint_entry *add_tracepoint(const char *name)
  188. {
  189. struct hlist_head *head;
  190. struct hlist_node *node;
  191. struct tracepoint_entry *e;
  192. size_t name_len = strlen(name) + 1;
  193. u32 hash = jhash(name, name_len-1, 0);
  194. head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
  195. hlist_for_each_entry(e, node, head, hlist) {
  196. if (!strcmp(name, e->name)) {
  197. printk(KERN_NOTICE
  198. "tracepoint %s busy\n", name);
  199. return ERR_PTR(-EEXIST); /* Already there */
  200. }
  201. }
  202. /*
  203. * Using kmalloc here to allocate a variable length element. Could
  204. * cause some memory fragmentation if overused.
  205. */
  206. e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
  207. if (!e)
  208. return ERR_PTR(-ENOMEM);
  209. memcpy(&e->name[0], name, name_len);
  210. e->funcs = NULL;
  211. e->refcount = 0;
  212. hlist_add_head(&e->hlist, head);
  213. return e;
  214. }
  215. /*
  216. * Remove the tracepoint from the tracepoint hash table. Must be called with
  217. * mutex_lock held.
  218. */
  219. static inline void remove_tracepoint(struct tracepoint_entry *e)
  220. {
  221. hlist_del(&e->hlist);
  222. kfree(e);
  223. }
  224. /*
  225. * Sets the probe callback corresponding to one tracepoint.
  226. */
  227. static void set_tracepoint(struct tracepoint_entry **entry,
  228. struct tracepoint *elem, int active)
  229. {
  230. WARN_ON(strcmp((*entry)->name, elem->name) != 0);
  231. if (elem->regfunc && !static_key_enabled(&elem->key) && active)
  232. elem->regfunc();
  233. else if (elem->unregfunc && static_key_enabled(&elem->key) && !active)
  234. elem->unregfunc();
  235. /*
  236. * rcu_assign_pointer has a smp_wmb() which makes sure that the new
  237. * probe callbacks array is consistent before setting a pointer to it.
  238. * This array is referenced by __DO_TRACE from
  239. * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
  240. * is used.
  241. */
  242. rcu_assign_pointer(elem->funcs, (*entry)->funcs);
  243. if (active && !static_key_enabled(&elem->key))
  244. static_key_slow_inc(&elem->key);
  245. else if (!active && static_key_enabled(&elem->key))
  246. static_key_slow_dec(&elem->key);
  247. }
  248. /*
  249. * Disable a tracepoint and its probe callback.
  250. * Note: only waiting an RCU period after setting elem->call to the empty
  251. * function insures that the original callback is not used anymore. This insured
  252. * by preempt_disable around the call site.
  253. */
  254. static void disable_tracepoint(struct tracepoint *elem)
  255. {
  256. if (elem->unregfunc && static_key_enabled(&elem->key))
  257. elem->unregfunc();
  258. if (static_key_enabled(&elem->key))
  259. static_key_slow_dec(&elem->key);
  260. rcu_assign_pointer(elem->funcs, NULL);
  261. }
  262. /**
  263. * tracepoint_update_probe_range - Update a probe range
  264. * @begin: beginning of the range
  265. * @end: end of the range
  266. *
  267. * Updates the probe callback corresponding to a range of tracepoints.
  268. * Called with tracepoints_mutex held.
  269. */
  270. static void tracepoint_update_probe_range(struct tracepoint * const *begin,
  271. struct tracepoint * const *end)
  272. {
  273. struct tracepoint * const *iter;
  274. struct tracepoint_entry *mark_entry;
  275. if (!begin)
  276. return;
  277. for (iter = begin; iter < end; iter++) {
  278. mark_entry = get_tracepoint((*iter)->name);
  279. if (mark_entry) {
  280. set_tracepoint(&mark_entry, *iter,
  281. !!mark_entry->refcount);
  282. } else {
  283. disable_tracepoint(*iter);
  284. }
  285. }
  286. }
  287. #ifdef CONFIG_MODULES
  288. void module_update_tracepoints(void)
  289. {
  290. struct tp_module *tp_mod;
  291. list_for_each_entry(tp_mod, &tracepoint_module_list, list)
  292. tracepoint_update_probe_range(tp_mod->tracepoints_ptrs,
  293. tp_mod->tracepoints_ptrs + tp_mod->num_tracepoints);
  294. }
  295. #else /* CONFIG_MODULES */
  296. void module_update_tracepoints(void)
  297. {
  298. }
  299. #endif /* CONFIG_MODULES */
  300. /*
  301. * Update probes, removing the faulty probes.
  302. * Called with tracepoints_mutex held.
  303. */
  304. static void tracepoint_update_probes(void)
  305. {
  306. /* Core kernel tracepoints */
  307. tracepoint_update_probe_range(__start___tracepoints_ptrs,
  308. __stop___tracepoints_ptrs);
  309. /* tracepoints in modules. */
  310. module_update_tracepoints();
  311. }
  312. static struct tracepoint_func *
  313. tracepoint_add_probe(const char *name, void *probe, void *data)
  314. {
  315. struct tracepoint_entry *entry;
  316. struct tracepoint_func *old;
  317. entry = get_tracepoint(name);
  318. if (!entry) {
  319. entry = add_tracepoint(name);
  320. if (IS_ERR(entry))
  321. return (struct tracepoint_func *)entry;
  322. }
  323. old = tracepoint_entry_add_probe(entry, probe, data);
  324. if (IS_ERR(old) && !entry->refcount)
  325. remove_tracepoint(entry);
  326. return old;
  327. }
  328. /**
  329. * tracepoint_probe_register - Connect a probe to a tracepoint
  330. * @name: tracepoint name
  331. * @probe: probe handler
  332. *
  333. * Returns 0 if ok, error value on error.
  334. * The probe address must at least be aligned on the architecture pointer size.
  335. */
  336. int tracepoint_probe_register(const char *name, void *probe, void *data)
  337. {
  338. struct tracepoint_func *old;
  339. mutex_lock(&tracepoints_mutex);
  340. old = tracepoint_add_probe(name, probe, data);
  341. if (IS_ERR(old)) {
  342. mutex_unlock(&tracepoints_mutex);
  343. return PTR_ERR(old);
  344. }
  345. tracepoint_update_probes(); /* may update entry */
  346. mutex_unlock(&tracepoints_mutex);
  347. release_probes(old);
  348. return 0;
  349. }
  350. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  351. static struct tracepoint_func *
  352. tracepoint_remove_probe(const char *name, void *probe, void *data)
  353. {
  354. struct tracepoint_entry *entry;
  355. struct tracepoint_func *old;
  356. entry = get_tracepoint(name);
  357. if (!entry)
  358. return ERR_PTR(-ENOENT);
  359. old = tracepoint_entry_remove_probe(entry, probe, data);
  360. if (IS_ERR(old))
  361. return old;
  362. if (!entry->refcount)
  363. remove_tracepoint(entry);
  364. return old;
  365. }
  366. /**
  367. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  368. * @name: tracepoint name
  369. * @probe: probe function pointer
  370. *
  371. * We do not need to call a synchronize_sched to make sure the probes have
  372. * finished running before doing a module unload, because the module unload
  373. * itself uses stop_machine(), which insures that every preempt disabled section
  374. * have finished.
  375. */
  376. int tracepoint_probe_unregister(const char *name, void *probe, void *data)
  377. {
  378. struct tracepoint_func *old;
  379. mutex_lock(&tracepoints_mutex);
  380. old = tracepoint_remove_probe(name, probe, data);
  381. if (IS_ERR(old)) {
  382. mutex_unlock(&tracepoints_mutex);
  383. return PTR_ERR(old);
  384. }
  385. tracepoint_update_probes(); /* may update entry */
  386. mutex_unlock(&tracepoints_mutex);
  387. release_probes(old);
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  391. static LIST_HEAD(old_probes);
  392. static int need_update;
  393. static void tracepoint_add_old_probes(void *old)
  394. {
  395. need_update = 1;
  396. if (old) {
  397. struct tp_probes *tp_probes = container_of(old,
  398. struct tp_probes, probes[0]);
  399. list_add(&tp_probes->u.list, &old_probes);
  400. }
  401. }
  402. /**
  403. * tracepoint_probe_register_noupdate - register a probe but not connect
  404. * @name: tracepoint name
  405. * @probe: probe handler
  406. *
  407. * caller must call tracepoint_probe_update_all()
  408. */
  409. int tracepoint_probe_register_noupdate(const char *name, void *probe,
  410. void *data)
  411. {
  412. struct tracepoint_func *old;
  413. mutex_lock(&tracepoints_mutex);
  414. old = tracepoint_add_probe(name, probe, data);
  415. if (IS_ERR(old)) {
  416. mutex_unlock(&tracepoints_mutex);
  417. return PTR_ERR(old);
  418. }
  419. tracepoint_add_old_probes(old);
  420. mutex_unlock(&tracepoints_mutex);
  421. return 0;
  422. }
  423. EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
  424. /**
  425. * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
  426. * @name: tracepoint name
  427. * @probe: probe function pointer
  428. *
  429. * caller must call tracepoint_probe_update_all()
  430. */
  431. int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
  432. void *data)
  433. {
  434. struct tracepoint_func *old;
  435. mutex_lock(&tracepoints_mutex);
  436. old = tracepoint_remove_probe(name, probe, data);
  437. if (IS_ERR(old)) {
  438. mutex_unlock(&tracepoints_mutex);
  439. return PTR_ERR(old);
  440. }
  441. tracepoint_add_old_probes(old);
  442. mutex_unlock(&tracepoints_mutex);
  443. return 0;
  444. }
  445. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
  446. /**
  447. * tracepoint_probe_update_all - update tracepoints
  448. */
  449. void tracepoint_probe_update_all(void)
  450. {
  451. LIST_HEAD(release_probes);
  452. struct tp_probes *pos, *next;
  453. mutex_lock(&tracepoints_mutex);
  454. if (!need_update) {
  455. mutex_unlock(&tracepoints_mutex);
  456. return;
  457. }
  458. if (!list_empty(&old_probes))
  459. list_replace_init(&old_probes, &release_probes);
  460. need_update = 0;
  461. tracepoint_update_probes();
  462. mutex_unlock(&tracepoints_mutex);
  463. list_for_each_entry_safe(pos, next, &release_probes, u.list) {
  464. list_del(&pos->u.list);
  465. call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
  466. }
  467. }
  468. EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
  469. /**
  470. * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
  471. * @tracepoint: current tracepoints (in), next tracepoint (out)
  472. * @begin: beginning of the range
  473. * @end: end of the range
  474. *
  475. * Returns whether a next tracepoint has been found (1) or not (0).
  476. * Will return the first tracepoint in the range if the input tracepoint is
  477. * NULL.
  478. */
  479. static int tracepoint_get_iter_range(struct tracepoint * const **tracepoint,
  480. struct tracepoint * const *begin, struct tracepoint * const *end)
  481. {
  482. if (!*tracepoint && begin != end) {
  483. *tracepoint = begin;
  484. return 1;
  485. }
  486. if (*tracepoint >= begin && *tracepoint < end)
  487. return 1;
  488. return 0;
  489. }
  490. #ifdef CONFIG_MODULES
  491. static void tracepoint_get_iter(struct tracepoint_iter *iter)
  492. {
  493. int found = 0;
  494. struct tp_module *iter_mod;
  495. /* Core kernel tracepoints */
  496. if (!iter->module) {
  497. found = tracepoint_get_iter_range(&iter->tracepoint,
  498. __start___tracepoints_ptrs,
  499. __stop___tracepoints_ptrs);
  500. if (found)
  501. goto end;
  502. }
  503. /* Tracepoints in modules */
  504. mutex_lock(&tracepoints_mutex);
  505. list_for_each_entry(iter_mod, &tracepoint_module_list, list) {
  506. /*
  507. * Sorted module list
  508. */
  509. if (iter_mod < iter->module)
  510. continue;
  511. else if (iter_mod > iter->module)
  512. iter->tracepoint = NULL;
  513. found = tracepoint_get_iter_range(&iter->tracepoint,
  514. iter_mod->tracepoints_ptrs,
  515. iter_mod->tracepoints_ptrs
  516. + iter_mod->num_tracepoints);
  517. if (found) {
  518. iter->module = iter_mod;
  519. break;
  520. }
  521. }
  522. mutex_unlock(&tracepoints_mutex);
  523. end:
  524. if (!found)
  525. tracepoint_iter_reset(iter);
  526. }
  527. #else /* CONFIG_MODULES */
  528. static void tracepoint_get_iter(struct tracepoint_iter *iter)
  529. {
  530. int found = 0;
  531. /* Core kernel tracepoints */
  532. found = tracepoint_get_iter_range(&iter->tracepoint,
  533. __start___tracepoints_ptrs,
  534. __stop___tracepoints_ptrs);
  535. if (!found)
  536. tracepoint_iter_reset(iter);
  537. }
  538. #endif /* CONFIG_MODULES */
  539. void tracepoint_iter_start(struct tracepoint_iter *iter)
  540. {
  541. tracepoint_get_iter(iter);
  542. }
  543. EXPORT_SYMBOL_GPL(tracepoint_iter_start);
  544. void tracepoint_iter_next(struct tracepoint_iter *iter)
  545. {
  546. iter->tracepoint++;
  547. /*
  548. * iter->tracepoint may be invalid because we blindly incremented it.
  549. * Make sure it is valid by marshalling on the tracepoints, getting the
  550. * tracepoints from following modules if necessary.
  551. */
  552. tracepoint_get_iter(iter);
  553. }
  554. EXPORT_SYMBOL_GPL(tracepoint_iter_next);
  555. void tracepoint_iter_stop(struct tracepoint_iter *iter)
  556. {
  557. }
  558. EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
  559. void tracepoint_iter_reset(struct tracepoint_iter *iter)
  560. {
  561. #ifdef CONFIG_MODULES
  562. iter->module = NULL;
  563. #endif /* CONFIG_MODULES */
  564. iter->tracepoint = NULL;
  565. }
  566. EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
  567. #ifdef CONFIG_MODULES
  568. bool trace_module_has_bad_taint(struct module *mod)
  569. {
  570. return mod->taints & ~((1 << TAINT_OOT_MODULE) | (1 << TAINT_CRAP));
  571. }
  572. static int tracepoint_module_coming(struct module *mod)
  573. {
  574. struct tp_module *tp_mod, *iter;
  575. int ret = 0;
  576. if (!mod->num_tracepoints)
  577. return 0;
  578. /*
  579. * We skip modules that taint the kernel, especially those with different
  580. * module headers (for forced load), to make sure we don't cause a crash.
  581. * Staging and out-of-tree GPL modules are fine.
  582. */
  583. if (trace_module_has_bad_taint(mod))
  584. return 0;
  585. mutex_lock(&tracepoints_mutex);
  586. tp_mod = kmalloc(sizeof(struct tp_module), GFP_KERNEL);
  587. if (!tp_mod) {
  588. ret = -ENOMEM;
  589. goto end;
  590. }
  591. tp_mod->num_tracepoints = mod->num_tracepoints;
  592. tp_mod->tracepoints_ptrs = mod->tracepoints_ptrs;
  593. /*
  594. * tracepoint_module_list is kept sorted by struct module pointer
  595. * address for iteration on tracepoints from a seq_file that can release
  596. * the mutex between calls.
  597. */
  598. list_for_each_entry_reverse(iter, &tracepoint_module_list, list) {
  599. BUG_ON(iter == tp_mod); /* Should never be in the list twice */
  600. if (iter < tp_mod) {
  601. /* We belong to the location right after iter. */
  602. list_add(&tp_mod->list, &iter->list);
  603. goto module_added;
  604. }
  605. }
  606. /* We belong to the beginning of the list */
  607. list_add(&tp_mod->list, &tracepoint_module_list);
  608. module_added:
  609. tracepoint_update_probe_range(mod->tracepoints_ptrs,
  610. mod->tracepoints_ptrs + mod->num_tracepoints);
  611. end:
  612. mutex_unlock(&tracepoints_mutex);
  613. return ret;
  614. }
  615. static int tracepoint_module_going(struct module *mod)
  616. {
  617. struct tp_module *pos;
  618. if (!mod->num_tracepoints)
  619. return 0;
  620. mutex_lock(&tracepoints_mutex);
  621. tracepoint_update_probe_range(mod->tracepoints_ptrs,
  622. mod->tracepoints_ptrs + mod->num_tracepoints);
  623. list_for_each_entry(pos, &tracepoint_module_list, list) {
  624. if (pos->tracepoints_ptrs == mod->tracepoints_ptrs) {
  625. list_del(&pos->list);
  626. kfree(pos);
  627. break;
  628. }
  629. }
  630. /*
  631. * In the case of modules that were tainted at "coming", we'll simply
  632. * walk through the list without finding it. We cannot use the "tainted"
  633. * flag on "going", in case a module taints the kernel only after being
  634. * loaded.
  635. */
  636. mutex_unlock(&tracepoints_mutex);
  637. return 0;
  638. }
  639. int tracepoint_module_notify(struct notifier_block *self,
  640. unsigned long val, void *data)
  641. {
  642. struct module *mod = data;
  643. int ret = 0;
  644. switch (val) {
  645. case MODULE_STATE_COMING:
  646. ret = tracepoint_module_coming(mod);
  647. break;
  648. case MODULE_STATE_LIVE:
  649. break;
  650. case MODULE_STATE_GOING:
  651. ret = tracepoint_module_going(mod);
  652. break;
  653. }
  654. return ret;
  655. }
  656. struct notifier_block tracepoint_module_nb = {
  657. .notifier_call = tracepoint_module_notify,
  658. .priority = 0,
  659. };
  660. static int init_tracepoints(void)
  661. {
  662. return register_module_notifier(&tracepoint_module_nb);
  663. }
  664. __initcall(init_tracepoints);
  665. #endif /* CONFIG_MODULES */
  666. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  667. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  668. static int sys_tracepoint_refcount;
  669. void syscall_regfunc(void)
  670. {
  671. unsigned long flags;
  672. struct task_struct *g, *t;
  673. if (!sys_tracepoint_refcount) {
  674. read_lock_irqsave(&tasklist_lock, flags);
  675. do_each_thread(g, t) {
  676. /* Skip kernel threads. */
  677. if (t->mm)
  678. set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  679. } while_each_thread(g, t);
  680. read_unlock_irqrestore(&tasklist_lock, flags);
  681. }
  682. sys_tracepoint_refcount++;
  683. }
  684. void syscall_unregfunc(void)
  685. {
  686. unsigned long flags;
  687. struct task_struct *g, *t;
  688. sys_tracepoint_refcount--;
  689. if (!sys_tracepoint_refcount) {
  690. read_lock_irqsave(&tasklist_lock, flags);
  691. do_each_thread(g, t) {
  692. clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  693. } while_each_thread(g, t);
  694. read_unlock_irqrestore(&tasklist_lock, flags);
  695. }
  696. }
  697. #endif