marker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright (C) 2007 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/marker.h>
  25. #include <linux/err.h>
  26. extern struct marker __start___markers[];
  27. extern struct marker __stop___markers[];
  28. /*
  29. * module_mutex nests inside markers_mutex. Markers mutex protects the builtin
  30. * and module markers, the hash table and deferred_sync.
  31. */
  32. static DEFINE_MUTEX(markers_mutex);
  33. /*
  34. * Marker deferred synchronization.
  35. * Upon marker probe_unregister, we delay call to synchronize_sched() to
  36. * accelerate mass unregistration (only when there is no more reference to a
  37. * given module do we call synchronize_sched()). However, we need to make sure
  38. * every critical region has ended before we re-arm a marker that has been
  39. * unregistered and then registered back with a different probe data.
  40. */
  41. static int deferred_sync;
  42. /*
  43. * Marker hash table, containing the active markers.
  44. * Protected by module_mutex.
  45. */
  46. #define MARKER_HASH_BITS 6
  47. #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
  48. struct marker_entry {
  49. struct hlist_node hlist;
  50. char *format;
  51. marker_probe_func *probe;
  52. void *private;
  53. int refcount; /* Number of times armed. 0 if disarmed. */
  54. char name[0]; /* Contains name'\0'format'\0' */
  55. };
  56. static struct hlist_head marker_table[MARKER_TABLE_SIZE];
  57. /**
  58. * __mark_empty_function - Empty probe callback
  59. * @mdata: pointer of type const struct marker
  60. * @fmt: format string
  61. * @...: variable argument list
  62. *
  63. * Empty callback provided as a probe to the markers. By providing this to a
  64. * disabled marker, we make sure the execution flow is always valid even
  65. * though the function pointer change and the marker enabling are two distinct
  66. * operations that modifies the execution flow of preemptible code.
  67. */
  68. void __mark_empty_function(const struct marker *mdata, void *private,
  69. const char *fmt, ...)
  70. {
  71. }
  72. EXPORT_SYMBOL_GPL(__mark_empty_function);
  73. /*
  74. * Get marker if the marker is present in the marker hash table.
  75. * Must be called with markers_mutex held.
  76. * Returns NULL if not present.
  77. */
  78. static struct marker_entry *get_marker(const char *name)
  79. {
  80. struct hlist_head *head;
  81. struct hlist_node *node;
  82. struct marker_entry *e;
  83. u32 hash = jhash(name, strlen(name), 0);
  84. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  85. hlist_for_each_entry(e, node, head, hlist) {
  86. if (!strcmp(name, e->name))
  87. return e;
  88. }
  89. return NULL;
  90. }
  91. /*
  92. * Add the marker to the marker hash table. Must be called with markers_mutex
  93. * held.
  94. */
  95. static int add_marker(const char *name, const char *format,
  96. marker_probe_func *probe, void *private)
  97. {
  98. struct hlist_head *head;
  99. struct hlist_node *node;
  100. struct marker_entry *e;
  101. size_t name_len = strlen(name) + 1;
  102. size_t format_len = 0;
  103. u32 hash = jhash(name, name_len-1, 0);
  104. if (format)
  105. format_len = strlen(format) + 1;
  106. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  107. hlist_for_each_entry(e, node, head, hlist) {
  108. if (!strcmp(name, e->name)) {
  109. printk(KERN_NOTICE
  110. "Marker %s busy, probe %p already installed\n",
  111. name, e->probe);
  112. return -EBUSY; /* Already there */
  113. }
  114. }
  115. /*
  116. * Using kmalloc here to allocate a variable length element. Could
  117. * cause some memory fragmentation if overused.
  118. */
  119. e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
  120. GFP_KERNEL);
  121. if (!e)
  122. return -ENOMEM;
  123. memcpy(&e->name[0], name, name_len);
  124. if (format) {
  125. e->format = &e->name[name_len];
  126. memcpy(e->format, format, format_len);
  127. trace_mark(core_marker_format, "name %s format %s",
  128. e->name, e->format);
  129. } else
  130. e->format = NULL;
  131. e->probe = probe;
  132. e->private = private;
  133. e->refcount = 0;
  134. hlist_add_head(&e->hlist, head);
  135. return 0;
  136. }
  137. /*
  138. * Remove the marker from the marker hash table. Must be called with mutex_lock
  139. * held.
  140. */
  141. static void *remove_marker(const char *name)
  142. {
  143. struct hlist_head *head;
  144. struct hlist_node *node;
  145. struct marker_entry *e;
  146. int found = 0;
  147. size_t len = strlen(name) + 1;
  148. void *private = NULL;
  149. u32 hash = jhash(name, len-1, 0);
  150. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  151. hlist_for_each_entry(e, node, head, hlist) {
  152. if (!strcmp(name, e->name)) {
  153. found = 1;
  154. break;
  155. }
  156. }
  157. if (found) {
  158. private = e->private;
  159. hlist_del(&e->hlist);
  160. kfree(e);
  161. }
  162. return private;
  163. }
  164. /*
  165. * Set the mark_entry format to the format found in the element.
  166. */
  167. static int marker_set_format(struct marker_entry **entry, const char *format)
  168. {
  169. struct marker_entry *e;
  170. size_t name_len = strlen((*entry)->name) + 1;
  171. size_t format_len = strlen(format) + 1;
  172. e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
  173. GFP_KERNEL);
  174. if (!e)
  175. return -ENOMEM;
  176. memcpy(&e->name[0], (*entry)->name, name_len);
  177. e->format = &e->name[name_len];
  178. memcpy(e->format, format, format_len);
  179. e->probe = (*entry)->probe;
  180. e->private = (*entry)->private;
  181. e->refcount = (*entry)->refcount;
  182. hlist_add_before(&e->hlist, &(*entry)->hlist);
  183. hlist_del(&(*entry)->hlist);
  184. kfree(*entry);
  185. *entry = e;
  186. trace_mark(core_marker_format, "name %s format %s",
  187. e->name, e->format);
  188. return 0;
  189. }
  190. /*
  191. * Sets the probe callback corresponding to one marker.
  192. */
  193. static int set_marker(struct marker_entry **entry, struct marker *elem)
  194. {
  195. int ret;
  196. WARN_ON(strcmp((*entry)->name, elem->name) != 0);
  197. if ((*entry)->format) {
  198. if (strcmp((*entry)->format, elem->format) != 0) {
  199. printk(KERN_NOTICE
  200. "Format mismatch for probe %s "
  201. "(%s), marker (%s)\n",
  202. (*entry)->name,
  203. (*entry)->format,
  204. elem->format);
  205. return -EPERM;
  206. }
  207. } else {
  208. ret = marker_set_format(entry, elem->format);
  209. if (ret)
  210. return ret;
  211. }
  212. elem->call = (*entry)->probe;
  213. elem->private = (*entry)->private;
  214. elem->state = 1;
  215. return 0;
  216. }
  217. /*
  218. * Disable a marker and its probe callback.
  219. * Note: only after a synchronize_sched() issued after setting elem->call to the
  220. * empty function insures that the original callback is not used anymore. This
  221. * insured by preemption disabling around the call site.
  222. */
  223. static void disable_marker(struct marker *elem)
  224. {
  225. elem->state = 0;
  226. elem->call = __mark_empty_function;
  227. /*
  228. * Leave the private data and id there, because removal is racy and
  229. * should be done only after a synchronize_sched(). These are never used
  230. * until the next initialization anyway.
  231. */
  232. }
  233. /**
  234. * marker_update_probe_range - Update a probe range
  235. * @begin: beginning of the range
  236. * @end: end of the range
  237. * @probe_module: module address of the probe being updated
  238. * @refcount: number of references left to the given probe_module (out)
  239. *
  240. * Updates the probe callback corresponding to a range of markers.
  241. * Must be called with markers_mutex held.
  242. */
  243. void marker_update_probe_range(struct marker *begin,
  244. struct marker *end, struct module *probe_module,
  245. int *refcount)
  246. {
  247. struct marker *iter;
  248. struct marker_entry *mark_entry;
  249. for (iter = begin; iter < end; iter++) {
  250. mark_entry = get_marker(iter->name);
  251. if (mark_entry && mark_entry->refcount) {
  252. set_marker(&mark_entry, iter);
  253. /*
  254. * ignore error, continue
  255. */
  256. if (probe_module)
  257. if (probe_module ==
  258. __module_text_address((unsigned long)mark_entry->probe))
  259. (*refcount)++;
  260. } else {
  261. disable_marker(iter);
  262. }
  263. }
  264. }
  265. /*
  266. * Update probes, removing the faulty probes.
  267. * Issues a synchronize_sched() when no reference to the module passed
  268. * as parameter is found in the probes so the probe module can be
  269. * safely unloaded from now on.
  270. */
  271. static void marker_update_probes(struct module *probe_module)
  272. {
  273. int refcount = 0;
  274. mutex_lock(&markers_mutex);
  275. /* Core kernel markers */
  276. marker_update_probe_range(__start___markers,
  277. __stop___markers, probe_module, &refcount);
  278. /* Markers in modules. */
  279. module_update_markers(probe_module, &refcount);
  280. if (probe_module && refcount == 0) {
  281. synchronize_sched();
  282. deferred_sync = 0;
  283. }
  284. mutex_unlock(&markers_mutex);
  285. }
  286. /**
  287. * marker_probe_register - Connect a probe to a marker
  288. * @name: marker name
  289. * @format: format string
  290. * @probe: probe handler
  291. * @private: probe private data
  292. *
  293. * private data must be a valid allocated memory address, or NULL.
  294. * Returns 0 if ok, error value on error.
  295. */
  296. int marker_probe_register(const char *name, const char *format,
  297. marker_probe_func *probe, void *private)
  298. {
  299. struct marker_entry *entry;
  300. int ret = 0, need_update = 0;
  301. mutex_lock(&markers_mutex);
  302. entry = get_marker(name);
  303. if (entry && entry->refcount) {
  304. ret = -EBUSY;
  305. goto end;
  306. }
  307. if (deferred_sync) {
  308. synchronize_sched();
  309. deferred_sync = 0;
  310. }
  311. ret = add_marker(name, format, probe, private);
  312. if (ret)
  313. goto end;
  314. need_update = 1;
  315. end:
  316. mutex_unlock(&markers_mutex);
  317. if (need_update)
  318. marker_update_probes(NULL);
  319. return ret;
  320. }
  321. EXPORT_SYMBOL_GPL(marker_probe_register);
  322. /**
  323. * marker_probe_unregister - Disconnect a probe from a marker
  324. * @name: marker name
  325. *
  326. * Returns the private data given to marker_probe_register, or an ERR_PTR().
  327. */
  328. void *marker_probe_unregister(const char *name)
  329. {
  330. struct module *probe_module;
  331. struct marker_entry *entry;
  332. void *private;
  333. int need_update = 0;
  334. mutex_lock(&markers_mutex);
  335. entry = get_marker(name);
  336. if (!entry) {
  337. private = ERR_PTR(-ENOENT);
  338. goto end;
  339. }
  340. entry->refcount = 0;
  341. /* In what module is the probe handler ? */
  342. probe_module = __module_text_address((unsigned long)entry->probe);
  343. private = remove_marker(name);
  344. deferred_sync = 1;
  345. need_update = 1;
  346. end:
  347. mutex_unlock(&markers_mutex);
  348. if (need_update)
  349. marker_update_probes(probe_module);
  350. return private;
  351. }
  352. EXPORT_SYMBOL_GPL(marker_probe_unregister);
  353. /**
  354. * marker_probe_unregister_private_data - Disconnect a probe from a marker
  355. * @private: probe private data
  356. *
  357. * Unregister a marker by providing the registered private data.
  358. * Returns the private data given to marker_probe_register, or an ERR_PTR().
  359. */
  360. void *marker_probe_unregister_private_data(void *private)
  361. {
  362. struct module *probe_module;
  363. struct hlist_head *head;
  364. struct hlist_node *node;
  365. struct marker_entry *entry;
  366. int found = 0;
  367. unsigned int i;
  368. int need_update = 0;
  369. mutex_lock(&markers_mutex);
  370. for (i = 0; i < MARKER_TABLE_SIZE; i++) {
  371. head = &marker_table[i];
  372. hlist_for_each_entry(entry, node, head, hlist) {
  373. if (entry->private == private) {
  374. found = 1;
  375. goto iter_end;
  376. }
  377. }
  378. }
  379. iter_end:
  380. if (!found) {
  381. private = ERR_PTR(-ENOENT);
  382. goto end;
  383. }
  384. entry->refcount = 0;
  385. /* In what module is the probe handler ? */
  386. probe_module = __module_text_address((unsigned long)entry->probe);
  387. private = remove_marker(entry->name);
  388. deferred_sync = 1;
  389. need_update = 1;
  390. end:
  391. mutex_unlock(&markers_mutex);
  392. if (need_update)
  393. marker_update_probes(probe_module);
  394. return private;
  395. }
  396. EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
  397. /**
  398. * marker_arm - Arm a marker
  399. * @name: marker name
  400. *
  401. * Activate a marker. It keeps a reference count of the number of
  402. * arming/disarming done.
  403. * Returns 0 if ok, error value on error.
  404. */
  405. int marker_arm(const char *name)
  406. {
  407. struct marker_entry *entry;
  408. int ret = 0, need_update = 0;
  409. mutex_lock(&markers_mutex);
  410. entry = get_marker(name);
  411. if (!entry) {
  412. ret = -ENOENT;
  413. goto end;
  414. }
  415. /*
  416. * Only need to update probes when refcount passes from 0 to 1.
  417. */
  418. if (entry->refcount++)
  419. goto end;
  420. need_update = 1;
  421. end:
  422. mutex_unlock(&markers_mutex);
  423. if (need_update)
  424. marker_update_probes(NULL);
  425. return ret;
  426. }
  427. EXPORT_SYMBOL_GPL(marker_arm);
  428. /**
  429. * marker_disarm - Disarm a marker
  430. * @name: marker name
  431. *
  432. * Disarm a marker. It keeps a reference count of the number of arming/disarming
  433. * done.
  434. * Returns 0 if ok, error value on error.
  435. */
  436. int marker_disarm(const char *name)
  437. {
  438. struct marker_entry *entry;
  439. int ret = 0, need_update = 0;
  440. mutex_lock(&markers_mutex);
  441. entry = get_marker(name);
  442. if (!entry) {
  443. ret = -ENOENT;
  444. goto end;
  445. }
  446. /*
  447. * Only permit decrement refcount if higher than 0.
  448. * Do probe update only on 1 -> 0 transition.
  449. */
  450. if (entry->refcount) {
  451. if (--entry->refcount)
  452. goto end;
  453. } else {
  454. ret = -EPERM;
  455. goto end;
  456. }
  457. need_update = 1;
  458. end:
  459. mutex_unlock(&markers_mutex);
  460. if (need_update)
  461. marker_update_probes(NULL);
  462. return ret;
  463. }
  464. EXPORT_SYMBOL_GPL(marker_disarm);
  465. /**
  466. * marker_get_private_data - Get a marker's probe private data
  467. * @name: marker name
  468. *
  469. * Returns the private data pointer, or an ERR_PTR.
  470. * The private data pointer should _only_ be dereferenced if the caller is the
  471. * owner of the data, or its content could vanish. This is mostly used to
  472. * confirm that a caller is the owner of a registered probe.
  473. */
  474. void *marker_get_private_data(const char *name)
  475. {
  476. struct hlist_head *head;
  477. struct hlist_node *node;
  478. struct marker_entry *e;
  479. size_t name_len = strlen(name) + 1;
  480. u32 hash = jhash(name, name_len-1, 0);
  481. int found = 0;
  482. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  483. hlist_for_each_entry(e, node, head, hlist) {
  484. if (!strcmp(name, e->name)) {
  485. found = 1;
  486. return e->private;
  487. }
  488. }
  489. return ERR_PTR(-ENOENT);
  490. }
  491. EXPORT_SYMBOL_GPL(marker_get_private_data);