tracepoint.c 20 KB

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