tracepoint.c 16 KB

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