tracepoint.c 20 KB

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