tracepoint.c 14 KB

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