tracepoint.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. struct rcu_head rcu;
  57. void *probes[0];
  58. };
  59. static inline void *allocate_probes(int count)
  60. {
  61. struct tp_probes *p = kmalloc(count * sizeof(void *)
  62. + sizeof(struct tp_probes), GFP_KERNEL);
  63. return p == NULL ? NULL : p->probes;
  64. }
  65. static void rcu_free_old_probes(struct rcu_head *head)
  66. {
  67. kfree(container_of(head, struct tp_probes, rcu));
  68. }
  69. static inline void release_probes(void *old)
  70. {
  71. if (old) {
  72. struct tp_probes *tp_probes = container_of(old,
  73. struct tp_probes, probes[0]);
  74. call_rcu(&tp_probes->rcu, rcu_free_old_probes);
  75. }
  76. }
  77. static void debug_print_probes(struct tracepoint_entry *entry)
  78. {
  79. int i;
  80. if (!tracepoint_debug || !entry->funcs)
  81. return;
  82. for (i = 0; entry->funcs[i]; i++)
  83. printk(KERN_DEBUG "Probe %d : %p\n", i, entry->funcs[i]);
  84. }
  85. static void *
  86. tracepoint_entry_add_probe(struct tracepoint_entry *entry, void *probe)
  87. {
  88. int nr_probes = 0;
  89. void **old, **new;
  90. WARN_ON(!probe);
  91. debug_print_probes(entry);
  92. old = entry->funcs;
  93. if (old) {
  94. /* (N -> N+1), (N != 0, 1) probes */
  95. for (nr_probes = 0; old[nr_probes]; nr_probes++)
  96. if (old[nr_probes] == probe)
  97. return ERR_PTR(-EEXIST);
  98. }
  99. /* + 2 : one for new probe, one for NULL func */
  100. new = allocate_probes(nr_probes + 2);
  101. if (new == NULL)
  102. return ERR_PTR(-ENOMEM);
  103. if (old)
  104. memcpy(new, old, nr_probes * sizeof(void *));
  105. new[nr_probes] = probe;
  106. new[nr_probes + 1] = NULL;
  107. entry->refcount = nr_probes + 1;
  108. entry->funcs = new;
  109. debug_print_probes(entry);
  110. return old;
  111. }
  112. static void *
  113. tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe)
  114. {
  115. int nr_probes = 0, nr_del = 0, i;
  116. void **old, **new;
  117. old = entry->funcs;
  118. if (!old)
  119. return ERR_PTR(-ENOENT);
  120. debug_print_probes(entry);
  121. /* (N -> M), (N > 1, M >= 0) probes */
  122. for (nr_probes = 0; old[nr_probes]; nr_probes++) {
  123. if ((!probe || old[nr_probes] == probe))
  124. nr_del++;
  125. }
  126. if (nr_probes - nr_del == 0) {
  127. /* N -> 0, (N > 1) */
  128. entry->funcs = NULL;
  129. entry->refcount = 0;
  130. debug_print_probes(entry);
  131. return old;
  132. } else {
  133. int j = 0;
  134. /* N -> M, (N > 1, M > 0) */
  135. /* + 1 for NULL */
  136. new = allocate_probes(nr_probes - nr_del + 1);
  137. if (new == NULL)
  138. return ERR_PTR(-ENOMEM);
  139. for (i = 0; old[i]; i++)
  140. if ((probe && old[i] != probe))
  141. new[j++] = old[i];
  142. new[nr_probes - nr_del] = NULL;
  143. entry->refcount = nr_probes - nr_del;
  144. entry->funcs = new;
  145. }
  146. debug_print_probes(entry);
  147. return old;
  148. }
  149. /*
  150. * Get tracepoint if the tracepoint is present in the tracepoint hash table.
  151. * Must be called with tracepoints_mutex held.
  152. * Returns NULL if not present.
  153. */
  154. static struct tracepoint_entry *get_tracepoint(const char *name)
  155. {
  156. struct hlist_head *head;
  157. struct hlist_node *node;
  158. struct tracepoint_entry *e;
  159. u32 hash = jhash(name, strlen(name), 0);
  160. head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
  161. hlist_for_each_entry(e, node, head, hlist) {
  162. if (!strcmp(name, e->name))
  163. return e;
  164. }
  165. return NULL;
  166. }
  167. /*
  168. * Add the tracepoint to the tracepoint hash table. Must be called with
  169. * tracepoints_mutex held.
  170. */
  171. static struct tracepoint_entry *add_tracepoint(const char *name)
  172. {
  173. struct hlist_head *head;
  174. struct hlist_node *node;
  175. struct tracepoint_entry *e;
  176. size_t name_len = strlen(name) + 1;
  177. u32 hash = jhash(name, name_len-1, 0);
  178. head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)];
  179. hlist_for_each_entry(e, node, head, hlist) {
  180. if (!strcmp(name, e->name)) {
  181. printk(KERN_NOTICE
  182. "tracepoint %s busy\n", name);
  183. return ERR_PTR(-EEXIST); /* Already there */
  184. }
  185. }
  186. /*
  187. * Using kmalloc here to allocate a variable length element. Could
  188. * cause some memory fragmentation if overused.
  189. */
  190. e = kmalloc(sizeof(struct tracepoint_entry) + name_len, GFP_KERNEL);
  191. if (!e)
  192. return ERR_PTR(-ENOMEM);
  193. memcpy(&e->name[0], name, name_len);
  194. e->funcs = NULL;
  195. e->refcount = 0;
  196. hlist_add_head(&e->hlist, head);
  197. return e;
  198. }
  199. /*
  200. * Remove the tracepoint from the tracepoint hash table. Must be called with
  201. * mutex_lock held.
  202. */
  203. static inline void remove_tracepoint(struct tracepoint_entry *e)
  204. {
  205. hlist_del(&e->hlist);
  206. kfree(e);
  207. }
  208. /*
  209. * Sets the probe callback corresponding to one tracepoint.
  210. */
  211. static void set_tracepoint(struct tracepoint_entry **entry,
  212. struct tracepoint *elem, int active)
  213. {
  214. WARN_ON(strcmp((*entry)->name, elem->name) != 0);
  215. /*
  216. * rcu_assign_pointer has a smp_wmb() which makes sure that the new
  217. * probe callbacks array is consistent before setting a pointer to it.
  218. * This array is referenced by __DO_TRACE from
  219. * include/linux/tracepoints.h. A matching smp_read_barrier_depends()
  220. * is used.
  221. */
  222. rcu_assign_pointer(elem->funcs, (*entry)->funcs);
  223. elem->state = active;
  224. }
  225. /*
  226. * Disable a tracepoint and its probe callback.
  227. * Note: only waiting an RCU period after setting elem->call to the empty
  228. * function insures that the original callback is not used anymore. This insured
  229. * by preempt_disable around the call site.
  230. */
  231. static void disable_tracepoint(struct tracepoint *elem)
  232. {
  233. elem->state = 0;
  234. }
  235. /**
  236. * tracepoint_update_probe_range - Update a probe range
  237. * @begin: beginning of the range
  238. * @end: end of the range
  239. *
  240. * Updates the probe callback corresponding to a range of tracepoints.
  241. */
  242. void tracepoint_update_probe_range(struct tracepoint *begin,
  243. struct tracepoint *end)
  244. {
  245. struct tracepoint *iter;
  246. struct tracepoint_entry *mark_entry;
  247. mutex_lock(&tracepoints_mutex);
  248. for (iter = begin; iter < end; iter++) {
  249. mark_entry = get_tracepoint(iter->name);
  250. if (mark_entry) {
  251. set_tracepoint(&mark_entry, iter,
  252. !!mark_entry->refcount);
  253. } else {
  254. disable_tracepoint(iter);
  255. }
  256. }
  257. mutex_unlock(&tracepoints_mutex);
  258. }
  259. /*
  260. * Update probes, removing the faulty probes.
  261. */
  262. static void tracepoint_update_probes(void)
  263. {
  264. /* Core kernel tracepoints */
  265. tracepoint_update_probe_range(__start___tracepoints,
  266. __stop___tracepoints);
  267. /* tracepoints in modules. */
  268. module_update_tracepoints();
  269. }
  270. /**
  271. * tracepoint_probe_register - Connect a probe to a tracepoint
  272. * @name: tracepoint name
  273. * @probe: probe handler
  274. *
  275. * Returns 0 if ok, error value on error.
  276. * The probe address must at least be aligned on the architecture pointer size.
  277. */
  278. int tracepoint_probe_register(const char *name, void *probe)
  279. {
  280. struct tracepoint_entry *entry;
  281. int ret = 0;
  282. void *old;
  283. mutex_lock(&tracepoints_mutex);
  284. entry = get_tracepoint(name);
  285. if (!entry) {
  286. entry = add_tracepoint(name);
  287. if (IS_ERR(entry)) {
  288. ret = PTR_ERR(entry);
  289. goto end;
  290. }
  291. }
  292. old = tracepoint_entry_add_probe(entry, probe);
  293. if (IS_ERR(old)) {
  294. if (!entry->refcount)
  295. remove_tracepoint(entry);
  296. ret = PTR_ERR(old);
  297. goto end;
  298. }
  299. mutex_unlock(&tracepoints_mutex);
  300. tracepoint_update_probes(); /* may update entry */
  301. release_probes(old);
  302. return 0;
  303. end:
  304. mutex_unlock(&tracepoints_mutex);
  305. return ret;
  306. }
  307. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  308. /**
  309. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  310. * @name: tracepoint name
  311. * @probe: probe function pointer
  312. *
  313. * We do not need to call a synchronize_sched to make sure the probes have
  314. * finished running before doing a module unload, because the module unload
  315. * itself uses stop_machine(), which insures that every preempt disabled section
  316. * have finished.
  317. */
  318. int tracepoint_probe_unregister(const char *name, void *probe)
  319. {
  320. struct tracepoint_entry *entry;
  321. void *old;
  322. int ret = -ENOENT;
  323. mutex_lock(&tracepoints_mutex);
  324. entry = get_tracepoint(name);
  325. if (!entry)
  326. goto end;
  327. old = tracepoint_entry_remove_probe(entry, probe);
  328. if (IS_ERR(old)) {
  329. ret = PTR_ERR(old);
  330. goto end;
  331. }
  332. if (!entry->refcount)
  333. remove_tracepoint(entry);
  334. mutex_unlock(&tracepoints_mutex);
  335. tracepoint_update_probes(); /* may update entry */
  336. release_probes(old);
  337. return 0;
  338. end:
  339. mutex_unlock(&tracepoints_mutex);
  340. return ret;
  341. }
  342. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  343. /**
  344. * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
  345. * @tracepoint: current tracepoints (in), next tracepoint (out)
  346. * @begin: beginning of the range
  347. * @end: end of the range
  348. *
  349. * Returns whether a next tracepoint has been found (1) or not (0).
  350. * Will return the first tracepoint in the range if the input tracepoint is
  351. * NULL.
  352. */
  353. int tracepoint_get_iter_range(struct tracepoint **tracepoint,
  354. struct tracepoint *begin, struct tracepoint *end)
  355. {
  356. if (!*tracepoint && begin != end) {
  357. *tracepoint = begin;
  358. return 1;
  359. }
  360. if (*tracepoint >= begin && *tracepoint < end)
  361. return 1;
  362. return 0;
  363. }
  364. EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
  365. static void tracepoint_get_iter(struct tracepoint_iter *iter)
  366. {
  367. int found = 0;
  368. /* Core kernel tracepoints */
  369. if (!iter->module) {
  370. found = tracepoint_get_iter_range(&iter->tracepoint,
  371. __start___tracepoints, __stop___tracepoints);
  372. if (found)
  373. goto end;
  374. }
  375. /* tracepoints in modules. */
  376. found = module_get_iter_tracepoints(iter);
  377. end:
  378. if (!found)
  379. tracepoint_iter_reset(iter);
  380. }
  381. void tracepoint_iter_start(struct tracepoint_iter *iter)
  382. {
  383. tracepoint_get_iter(iter);
  384. }
  385. EXPORT_SYMBOL_GPL(tracepoint_iter_start);
  386. void tracepoint_iter_next(struct tracepoint_iter *iter)
  387. {
  388. iter->tracepoint++;
  389. /*
  390. * iter->tracepoint may be invalid because we blindly incremented it.
  391. * Make sure it is valid by marshalling on the tracepoints, getting the
  392. * tracepoints from following modules if necessary.
  393. */
  394. tracepoint_get_iter(iter);
  395. }
  396. EXPORT_SYMBOL_GPL(tracepoint_iter_next);
  397. void tracepoint_iter_stop(struct tracepoint_iter *iter)
  398. {
  399. }
  400. EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
  401. void tracepoint_iter_reset(struct tracepoint_iter *iter)
  402. {
  403. iter->module = NULL;
  404. iter->tracepoint = NULL;
  405. }
  406. EXPORT_SYMBOL_GPL(tracepoint_iter_reset);