tracepoint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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 && !elem->state && active)
  228. elem->regfunc();
  229. else if (elem->unregfunc && elem->state && !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 (!elem->state && active) {
  240. jump_label_enable(&elem->state);
  241. elem->state = active;
  242. } else if (elem->state && !active) {
  243. jump_label_disable(&elem->state);
  244. elem->state = active;
  245. }
  246. }
  247. /*
  248. * Disable a tracepoint and its probe callback.
  249. * Note: only waiting an RCU period after setting elem->call to the empty
  250. * function insures that the original callback is not used anymore. This insured
  251. * by preempt_disable around the call site.
  252. */
  253. static void disable_tracepoint(struct tracepoint *elem)
  254. {
  255. if (elem->unregfunc && elem->state)
  256. elem->unregfunc();
  257. if (elem->state) {
  258. jump_label_disable(&elem->state);
  259. elem->state = 0;
  260. }
  261. rcu_assign_pointer(elem->funcs, NULL);
  262. }
  263. /**
  264. * tracepoint_update_probe_range - Update a probe range
  265. * @begin: beginning of the range
  266. * @end: end of the range
  267. *
  268. * Updates the probe callback corresponding to a range of tracepoints.
  269. */
  270. 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. mutex_lock(&tracepoints_mutex);
  278. for (iter = begin; iter < end; iter++) {
  279. mark_entry = get_tracepoint((*iter)->name);
  280. if (mark_entry) {
  281. set_tracepoint(&mark_entry, *iter,
  282. !!mark_entry->refcount);
  283. } else {
  284. disable_tracepoint(*iter);
  285. }
  286. }
  287. mutex_unlock(&tracepoints_mutex);
  288. }
  289. /*
  290. * Update probes, removing the faulty probes.
  291. */
  292. static void tracepoint_update_probes(void)
  293. {
  294. /* Core kernel tracepoints */
  295. tracepoint_update_probe_range(__start___tracepoints_ptrs,
  296. __stop___tracepoints_ptrs);
  297. /* tracepoints in modules. */
  298. module_update_tracepoints();
  299. }
  300. static struct tracepoint_func *
  301. tracepoint_add_probe(const char *name, void *probe, void *data)
  302. {
  303. struct tracepoint_entry *entry;
  304. struct tracepoint_func *old;
  305. entry = get_tracepoint(name);
  306. if (!entry) {
  307. entry = add_tracepoint(name);
  308. if (IS_ERR(entry))
  309. return (struct tracepoint_func *)entry;
  310. }
  311. old = tracepoint_entry_add_probe(entry, probe, data);
  312. if (IS_ERR(old) && !entry->refcount)
  313. remove_tracepoint(entry);
  314. return old;
  315. }
  316. /**
  317. * tracepoint_probe_register - Connect a probe to a tracepoint
  318. * @name: tracepoint name
  319. * @probe: probe handler
  320. *
  321. * Returns 0 if ok, error value on error.
  322. * The probe address must at least be aligned on the architecture pointer size.
  323. */
  324. int tracepoint_probe_register(const char *name, void *probe, void *data)
  325. {
  326. struct tracepoint_func *old;
  327. mutex_lock(&tracepoints_mutex);
  328. old = tracepoint_add_probe(name, probe, data);
  329. mutex_unlock(&tracepoints_mutex);
  330. if (IS_ERR(old))
  331. return PTR_ERR(old);
  332. tracepoint_update_probes(); /* may update entry */
  333. release_probes(old);
  334. return 0;
  335. }
  336. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  337. static struct tracepoint_func *
  338. tracepoint_remove_probe(const char *name, void *probe, void *data)
  339. {
  340. struct tracepoint_entry *entry;
  341. struct tracepoint_func *old;
  342. entry = get_tracepoint(name);
  343. if (!entry)
  344. return ERR_PTR(-ENOENT);
  345. old = tracepoint_entry_remove_probe(entry, probe, data);
  346. if (IS_ERR(old))
  347. return old;
  348. if (!entry->refcount)
  349. remove_tracepoint(entry);
  350. return old;
  351. }
  352. /**
  353. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  354. * @name: tracepoint name
  355. * @probe: probe function pointer
  356. *
  357. * We do not need to call a synchronize_sched to make sure the probes have
  358. * finished running before doing a module unload, because the module unload
  359. * itself uses stop_machine(), which insures that every preempt disabled section
  360. * have finished.
  361. */
  362. int tracepoint_probe_unregister(const char *name, void *probe, void *data)
  363. {
  364. struct tracepoint_func *old;
  365. mutex_lock(&tracepoints_mutex);
  366. old = tracepoint_remove_probe(name, probe, data);
  367. mutex_unlock(&tracepoints_mutex);
  368. if (IS_ERR(old))
  369. return PTR_ERR(old);
  370. tracepoint_update_probes(); /* may update entry */
  371. release_probes(old);
  372. return 0;
  373. }
  374. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  375. static LIST_HEAD(old_probes);
  376. static int need_update;
  377. static void tracepoint_add_old_probes(void *old)
  378. {
  379. need_update = 1;
  380. if (old) {
  381. struct tp_probes *tp_probes = container_of(old,
  382. struct tp_probes, probes[0]);
  383. list_add(&tp_probes->u.list, &old_probes);
  384. }
  385. }
  386. /**
  387. * tracepoint_probe_register_noupdate - register a probe but not connect
  388. * @name: tracepoint name
  389. * @probe: probe handler
  390. *
  391. * caller must call tracepoint_probe_update_all()
  392. */
  393. int tracepoint_probe_register_noupdate(const char *name, void *probe,
  394. void *data)
  395. {
  396. struct tracepoint_func *old;
  397. mutex_lock(&tracepoints_mutex);
  398. old = tracepoint_add_probe(name, probe, data);
  399. if (IS_ERR(old)) {
  400. mutex_unlock(&tracepoints_mutex);
  401. return PTR_ERR(old);
  402. }
  403. tracepoint_add_old_probes(old);
  404. mutex_unlock(&tracepoints_mutex);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
  408. /**
  409. * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
  410. * @name: tracepoint name
  411. * @probe: probe function pointer
  412. *
  413. * caller must call tracepoint_probe_update_all()
  414. */
  415. int tracepoint_probe_unregister_noupdate(const char *name, void *probe,
  416. void *data)
  417. {
  418. struct tracepoint_func *old;
  419. mutex_lock(&tracepoints_mutex);
  420. old = tracepoint_remove_probe(name, probe, data);
  421. if (IS_ERR(old)) {
  422. mutex_unlock(&tracepoints_mutex);
  423. return PTR_ERR(old);
  424. }
  425. tracepoint_add_old_probes(old);
  426. mutex_unlock(&tracepoints_mutex);
  427. return 0;
  428. }
  429. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
  430. /**
  431. * tracepoint_probe_update_all - update tracepoints
  432. */
  433. void tracepoint_probe_update_all(void)
  434. {
  435. LIST_HEAD(release_probes);
  436. struct tp_probes *pos, *next;
  437. mutex_lock(&tracepoints_mutex);
  438. if (!need_update) {
  439. mutex_unlock(&tracepoints_mutex);
  440. return;
  441. }
  442. if (!list_empty(&old_probes))
  443. list_replace_init(&old_probes, &release_probes);
  444. need_update = 0;
  445. mutex_unlock(&tracepoints_mutex);
  446. tracepoint_update_probes();
  447. list_for_each_entry_safe(pos, next, &release_probes, u.list) {
  448. list_del(&pos->u.list);
  449. call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
  450. }
  451. }
  452. EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
  453. /**
  454. * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
  455. * @tracepoint: current tracepoints (in), next tracepoint (out)
  456. * @begin: beginning of the range
  457. * @end: end of the range
  458. *
  459. * Returns whether a next tracepoint has been found (1) or not (0).
  460. * Will return the first tracepoint in the range if the input tracepoint is
  461. * NULL.
  462. */
  463. int tracepoint_get_iter_range(struct tracepoint * const **tracepoint,
  464. struct tracepoint * const *begin, struct tracepoint * const *end)
  465. {
  466. if (!*tracepoint && begin != end) {
  467. *tracepoint = begin;
  468. return 1;
  469. }
  470. if (*tracepoint >= begin && *tracepoint < end)
  471. return 1;
  472. return 0;
  473. }
  474. EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
  475. static void tracepoint_get_iter(struct tracepoint_iter *iter)
  476. {
  477. int found = 0;
  478. /* Core kernel tracepoints */
  479. if (!iter->module) {
  480. found = tracepoint_get_iter_range(&iter->tracepoint,
  481. __start___tracepoints_ptrs,
  482. __stop___tracepoints_ptrs);
  483. if (found)
  484. goto end;
  485. }
  486. /* tracepoints in modules. */
  487. found = module_get_iter_tracepoints(iter);
  488. end:
  489. if (!found)
  490. tracepoint_iter_reset(iter);
  491. }
  492. void tracepoint_iter_start(struct tracepoint_iter *iter)
  493. {
  494. tracepoint_get_iter(iter);
  495. }
  496. EXPORT_SYMBOL_GPL(tracepoint_iter_start);
  497. void tracepoint_iter_next(struct tracepoint_iter *iter)
  498. {
  499. iter->tracepoint++;
  500. /*
  501. * iter->tracepoint may be invalid because we blindly incremented it.
  502. * Make sure it is valid by marshalling on the tracepoints, getting the
  503. * tracepoints from following modules if necessary.
  504. */
  505. tracepoint_get_iter(iter);
  506. }
  507. EXPORT_SYMBOL_GPL(tracepoint_iter_next);
  508. void tracepoint_iter_stop(struct tracepoint_iter *iter)
  509. {
  510. }
  511. EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
  512. void tracepoint_iter_reset(struct tracepoint_iter *iter)
  513. {
  514. iter->module = NULL;
  515. iter->tracepoint = NULL;
  516. }
  517. EXPORT_SYMBOL_GPL(tracepoint_iter_reset);
  518. #ifdef CONFIG_MODULES
  519. int tracepoint_module_notify(struct notifier_block *self,
  520. unsigned long val, void *data)
  521. {
  522. struct module *mod = data;
  523. switch (val) {
  524. case MODULE_STATE_COMING:
  525. case MODULE_STATE_GOING:
  526. tracepoint_update_probe_range(mod->tracepoints_ptrs,
  527. mod->tracepoints_ptrs + mod->num_tracepoints);
  528. break;
  529. }
  530. return 0;
  531. }
  532. struct notifier_block tracepoint_module_nb = {
  533. .notifier_call = tracepoint_module_notify,
  534. .priority = 0,
  535. };
  536. static int init_tracepoints(void)
  537. {
  538. return register_module_notifier(&tracepoint_module_nb);
  539. }
  540. __initcall(init_tracepoints);
  541. #endif /* CONFIG_MODULES */
  542. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  543. /* NB: reg/unreg are called while guarded with the tracepoints_mutex */
  544. static int sys_tracepoint_refcount;
  545. void syscall_regfunc(void)
  546. {
  547. unsigned long flags;
  548. struct task_struct *g, *t;
  549. if (!sys_tracepoint_refcount) {
  550. read_lock_irqsave(&tasklist_lock, flags);
  551. do_each_thread(g, t) {
  552. /* Skip kernel threads. */
  553. if (t->mm)
  554. set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  555. } while_each_thread(g, t);
  556. read_unlock_irqrestore(&tasklist_lock, flags);
  557. }
  558. sys_tracepoint_refcount++;
  559. }
  560. void syscall_unregfunc(void)
  561. {
  562. unsigned long flags;
  563. struct task_struct *g, *t;
  564. sys_tracepoint_refcount--;
  565. if (!sys_tracepoint_refcount) {
  566. read_lock_irqsave(&tasklist_lock, flags);
  567. do_each_thread(g, t) {
  568. clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
  569. } while_each_thread(g, t);
  570. read_unlock_irqrestore(&tasklist_lock, flags);
  571. }
  572. }
  573. #endif