tracepoint.c 15 KB

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