tracepoint.c 14 KB

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