tracepoint.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. }
  238. /**
  239. * tracepoint_update_probe_range - Update a probe range
  240. * @begin: beginning of the range
  241. * @end: end of the range
  242. *
  243. * Updates the probe callback corresponding to a range of tracepoints.
  244. */
  245. void tracepoint_update_probe_range(struct tracepoint *begin,
  246. struct tracepoint *end)
  247. {
  248. struct tracepoint *iter;
  249. struct tracepoint_entry *mark_entry;
  250. mutex_lock(&tracepoints_mutex);
  251. for (iter = begin; iter < end; iter++) {
  252. mark_entry = get_tracepoint(iter->name);
  253. if (mark_entry) {
  254. set_tracepoint(&mark_entry, iter,
  255. !!mark_entry->refcount);
  256. } else {
  257. disable_tracepoint(iter);
  258. }
  259. }
  260. mutex_unlock(&tracepoints_mutex);
  261. }
  262. /*
  263. * Update probes, removing the faulty probes.
  264. */
  265. static void tracepoint_update_probes(void)
  266. {
  267. /* Core kernel tracepoints */
  268. tracepoint_update_probe_range(__start___tracepoints,
  269. __stop___tracepoints);
  270. /* tracepoints in modules. */
  271. module_update_tracepoints();
  272. }
  273. static void *tracepoint_add_probe(const char *name, void *probe)
  274. {
  275. struct tracepoint_entry *entry;
  276. void *old;
  277. entry = get_tracepoint(name);
  278. if (!entry) {
  279. entry = add_tracepoint(name);
  280. if (IS_ERR(entry))
  281. return entry;
  282. }
  283. old = tracepoint_entry_add_probe(entry, probe);
  284. if (IS_ERR(old) && !entry->refcount)
  285. remove_tracepoint(entry);
  286. return old;
  287. }
  288. /**
  289. * tracepoint_probe_register - Connect a probe to a tracepoint
  290. * @name: tracepoint name
  291. * @probe: probe handler
  292. *
  293. * Returns 0 if ok, error value on error.
  294. * The probe address must at least be aligned on the architecture pointer size.
  295. */
  296. int tracepoint_probe_register(const char *name, void *probe)
  297. {
  298. void *old;
  299. mutex_lock(&tracepoints_mutex);
  300. old = tracepoint_add_probe(name, probe);
  301. mutex_unlock(&tracepoints_mutex);
  302. if (IS_ERR(old))
  303. return PTR_ERR(old);
  304. tracepoint_update_probes(); /* may update entry */
  305. release_probes(old);
  306. return 0;
  307. }
  308. EXPORT_SYMBOL_GPL(tracepoint_probe_register);
  309. static void *tracepoint_remove_probe(const char *name, void *probe)
  310. {
  311. struct tracepoint_entry *entry;
  312. void *old;
  313. entry = get_tracepoint(name);
  314. if (!entry)
  315. return ERR_PTR(-ENOENT);
  316. old = tracepoint_entry_remove_probe(entry, probe);
  317. if (IS_ERR(old))
  318. return old;
  319. if (!entry->refcount)
  320. remove_tracepoint(entry);
  321. return old;
  322. }
  323. /**
  324. * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
  325. * @name: tracepoint name
  326. * @probe: probe function pointer
  327. *
  328. * We do not need to call a synchronize_sched to make sure the probes have
  329. * finished running before doing a module unload, because the module unload
  330. * itself uses stop_machine(), which insures that every preempt disabled section
  331. * have finished.
  332. */
  333. int tracepoint_probe_unregister(const char *name, void *probe)
  334. {
  335. void *old;
  336. mutex_lock(&tracepoints_mutex);
  337. old = tracepoint_remove_probe(name, probe);
  338. mutex_unlock(&tracepoints_mutex);
  339. if (IS_ERR(old))
  340. return PTR_ERR(old);
  341. tracepoint_update_probes(); /* may update entry */
  342. release_probes(old);
  343. return 0;
  344. }
  345. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister);
  346. static LIST_HEAD(old_probes);
  347. static int need_update;
  348. static void tracepoint_add_old_probes(void *old)
  349. {
  350. need_update = 1;
  351. if (old) {
  352. struct tp_probes *tp_probes = container_of(old,
  353. struct tp_probes, probes[0]);
  354. list_add(&tp_probes->u.list, &old_probes);
  355. }
  356. }
  357. /**
  358. * tracepoint_probe_register_noupdate - register a probe but not connect
  359. * @name: tracepoint name
  360. * @probe: probe handler
  361. *
  362. * caller must call tracepoint_probe_update_all()
  363. */
  364. int tracepoint_probe_register_noupdate(const char *name, void *probe)
  365. {
  366. void *old;
  367. mutex_lock(&tracepoints_mutex);
  368. old = tracepoint_add_probe(name, probe);
  369. if (IS_ERR(old)) {
  370. mutex_unlock(&tracepoints_mutex);
  371. return PTR_ERR(old);
  372. }
  373. tracepoint_add_old_probes(old);
  374. mutex_unlock(&tracepoints_mutex);
  375. return 0;
  376. }
  377. EXPORT_SYMBOL_GPL(tracepoint_probe_register_noupdate);
  378. /**
  379. * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
  380. * @name: tracepoint name
  381. * @probe: probe function pointer
  382. *
  383. * caller must call tracepoint_probe_update_all()
  384. */
  385. int tracepoint_probe_unregister_noupdate(const char *name, void *probe)
  386. {
  387. void *old;
  388. mutex_lock(&tracepoints_mutex);
  389. old = tracepoint_remove_probe(name, probe);
  390. if (IS_ERR(old)) {
  391. mutex_unlock(&tracepoints_mutex);
  392. return PTR_ERR(old);
  393. }
  394. tracepoint_add_old_probes(old);
  395. mutex_unlock(&tracepoints_mutex);
  396. return 0;
  397. }
  398. EXPORT_SYMBOL_GPL(tracepoint_probe_unregister_noupdate);
  399. /**
  400. * tracepoint_probe_update_all - update tracepoints
  401. */
  402. void tracepoint_probe_update_all(void)
  403. {
  404. LIST_HEAD(release_probes);
  405. struct tp_probes *pos, *next;
  406. mutex_lock(&tracepoints_mutex);
  407. if (!need_update) {
  408. mutex_unlock(&tracepoints_mutex);
  409. return;
  410. }
  411. if (!list_empty(&old_probes))
  412. list_replace_init(&old_probes, &release_probes);
  413. need_update = 0;
  414. mutex_unlock(&tracepoints_mutex);
  415. tracepoint_update_probes();
  416. list_for_each_entry_safe(pos, next, &release_probes, u.list) {
  417. list_del(&pos->u.list);
  418. call_rcu_sched(&pos->u.rcu, rcu_free_old_probes);
  419. }
  420. }
  421. EXPORT_SYMBOL_GPL(tracepoint_probe_update_all);
  422. /**
  423. * tracepoint_get_iter_range - Get a next tracepoint iterator given a range.
  424. * @tracepoint: current tracepoints (in), next tracepoint (out)
  425. * @begin: beginning of the range
  426. * @end: end of the range
  427. *
  428. * Returns whether a next tracepoint has been found (1) or not (0).
  429. * Will return the first tracepoint in the range if the input tracepoint is
  430. * NULL.
  431. */
  432. int tracepoint_get_iter_range(struct tracepoint **tracepoint,
  433. struct tracepoint *begin, struct tracepoint *end)
  434. {
  435. if (!*tracepoint && begin != end) {
  436. *tracepoint = begin;
  437. return 1;
  438. }
  439. if (*tracepoint >= begin && *tracepoint < end)
  440. return 1;
  441. return 0;
  442. }
  443. EXPORT_SYMBOL_GPL(tracepoint_get_iter_range);
  444. static void tracepoint_get_iter(struct tracepoint_iter *iter)
  445. {
  446. int found = 0;
  447. /* Core kernel tracepoints */
  448. if (!iter->module) {
  449. found = tracepoint_get_iter_range(&iter->tracepoint,
  450. __start___tracepoints, __stop___tracepoints);
  451. if (found)
  452. goto end;
  453. }
  454. /* tracepoints in modules. */
  455. found = module_get_iter_tracepoints(iter);
  456. end:
  457. if (!found)
  458. tracepoint_iter_reset(iter);
  459. }
  460. void tracepoint_iter_start(struct tracepoint_iter *iter)
  461. {
  462. tracepoint_get_iter(iter);
  463. }
  464. EXPORT_SYMBOL_GPL(tracepoint_iter_start);
  465. void tracepoint_iter_next(struct tracepoint_iter *iter)
  466. {
  467. iter->tracepoint++;
  468. /*
  469. * iter->tracepoint may be invalid because we blindly incremented it.
  470. * Make sure it is valid by marshalling on the tracepoints, getting the
  471. * tracepoints from following modules if necessary.
  472. */
  473. tracepoint_get_iter(iter);
  474. }
  475. EXPORT_SYMBOL_GPL(tracepoint_iter_next);
  476. void tracepoint_iter_stop(struct tracepoint_iter *iter)
  477. {
  478. }
  479. EXPORT_SYMBOL_GPL(tracepoint_iter_stop);
  480. void tracepoint_iter_reset(struct tracepoint_iter *iter)
  481. {
  482. iter->module = NULL;
  483. iter->tracepoint = NULL;
  484. }
  485. EXPORT_SYMBOL_GPL(tracepoint_iter_reset);