tracepoint.c 14 KB

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