tracepoint.c 16 KB

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