tracepoint.c 12 KB

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