tracepoint.c 15 KB

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