tracepoint.c 12 KB

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