marker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. * markers_mutex nests inside module_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. */
  242. void marker_update_probe_range(struct marker *begin,
  243. struct marker *end, struct module *probe_module,
  244. int *refcount)
  245. {
  246. struct marker *iter;
  247. struct marker_entry *mark_entry;
  248. mutex_lock(&markers_mutex);
  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. mutex_unlock(&markers_mutex);
  265. }
  266. /*
  267. * Update probes, removing the faulty probes.
  268. * Issues a synchronize_sched() when no reference to the module passed
  269. * as parameter is found in the probes so the probe module can be
  270. * safely unloaded from now on.
  271. */
  272. static void marker_update_probes(struct module *probe_module)
  273. {
  274. int refcount = 0;
  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. }
  285. /**
  286. * marker_probe_register - Connect a probe to a marker
  287. * @name: marker name
  288. * @format: format string
  289. * @probe: probe handler
  290. * @private: probe private data
  291. *
  292. * private data must be a valid allocated memory address, or NULL.
  293. * Returns 0 if ok, error value on error.
  294. */
  295. int marker_probe_register(const char *name, const char *format,
  296. marker_probe_func *probe, void *private)
  297. {
  298. struct marker_entry *entry;
  299. int ret = 0;
  300. mutex_lock(&markers_mutex);
  301. entry = get_marker(name);
  302. if (entry && entry->refcount) {
  303. ret = -EBUSY;
  304. goto end;
  305. }
  306. if (deferred_sync) {
  307. synchronize_sched();
  308. deferred_sync = 0;
  309. }
  310. ret = add_marker(name, format, probe, private);
  311. if (ret)
  312. goto end;
  313. mutex_unlock(&markers_mutex);
  314. marker_update_probes(NULL);
  315. return ret;
  316. end:
  317. mutex_unlock(&markers_mutex);
  318. return ret;
  319. }
  320. EXPORT_SYMBOL_GPL(marker_probe_register);
  321. /**
  322. * marker_probe_unregister - Disconnect a probe from a marker
  323. * @name: marker name
  324. *
  325. * Returns the private data given to marker_probe_register, or an ERR_PTR().
  326. */
  327. void *marker_probe_unregister(const char *name)
  328. {
  329. struct module *probe_module;
  330. struct marker_entry *entry;
  331. void *private;
  332. mutex_lock(&markers_mutex);
  333. entry = get_marker(name);
  334. if (!entry) {
  335. private = ERR_PTR(-ENOENT);
  336. goto end;
  337. }
  338. entry->refcount = 0;
  339. /* In what module is the probe handler ? */
  340. probe_module = __module_text_address((unsigned long)entry->probe);
  341. private = remove_marker(name);
  342. deferred_sync = 1;
  343. mutex_unlock(&markers_mutex);
  344. marker_update_probes(probe_module);
  345. return private;
  346. end:
  347. mutex_unlock(&markers_mutex);
  348. return private;
  349. }
  350. EXPORT_SYMBOL_GPL(marker_probe_unregister);
  351. /**
  352. * marker_probe_unregister_private_data - Disconnect a probe from a marker
  353. * @private: probe private data
  354. *
  355. * Unregister a marker by providing the registered private data.
  356. * Returns the private data given to marker_probe_register, or an ERR_PTR().
  357. */
  358. void *marker_probe_unregister_private_data(void *private)
  359. {
  360. struct module *probe_module;
  361. struct hlist_head *head;
  362. struct hlist_node *node;
  363. struct marker_entry *entry;
  364. int found = 0;
  365. unsigned int i;
  366. mutex_lock(&markers_mutex);
  367. for (i = 0; i < MARKER_TABLE_SIZE; i++) {
  368. head = &marker_table[i];
  369. hlist_for_each_entry(entry, node, head, hlist) {
  370. if (entry->private == private) {
  371. found = 1;
  372. goto iter_end;
  373. }
  374. }
  375. }
  376. iter_end:
  377. if (!found) {
  378. private = ERR_PTR(-ENOENT);
  379. goto end;
  380. }
  381. entry->refcount = 0;
  382. /* In what module is the probe handler ? */
  383. probe_module = __module_text_address((unsigned long)entry->probe);
  384. private = remove_marker(entry->name);
  385. deferred_sync = 1;
  386. mutex_unlock(&markers_mutex);
  387. marker_update_probes(probe_module);
  388. return private;
  389. end:
  390. mutex_unlock(&markers_mutex);
  391. return private;
  392. }
  393. EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
  394. /**
  395. * marker_arm - Arm a marker
  396. * @name: marker name
  397. *
  398. * Activate a marker. It keeps a reference count of the number of
  399. * arming/disarming done.
  400. * Returns 0 if ok, error value on error.
  401. */
  402. int marker_arm(const char *name)
  403. {
  404. struct marker_entry *entry;
  405. int ret = 0;
  406. mutex_lock(&markers_mutex);
  407. entry = get_marker(name);
  408. if (!entry) {
  409. ret = -ENOENT;
  410. goto end;
  411. }
  412. /*
  413. * Only need to update probes when refcount passes from 0 to 1.
  414. */
  415. if (entry->refcount++)
  416. goto end;
  417. end:
  418. mutex_unlock(&markers_mutex);
  419. marker_update_probes(NULL);
  420. return ret;
  421. }
  422. EXPORT_SYMBOL_GPL(marker_arm);
  423. /**
  424. * marker_disarm - Disarm a marker
  425. * @name: marker name
  426. *
  427. * Disarm a marker. It keeps a reference count of the number of arming/disarming
  428. * done.
  429. * Returns 0 if ok, error value on error.
  430. */
  431. int marker_disarm(const char *name)
  432. {
  433. struct marker_entry *entry;
  434. int ret = 0;
  435. mutex_lock(&markers_mutex);
  436. entry = get_marker(name);
  437. if (!entry) {
  438. ret = -ENOENT;
  439. goto end;
  440. }
  441. /*
  442. * Only permit decrement refcount if higher than 0.
  443. * Do probe update only on 1 -> 0 transition.
  444. */
  445. if (entry->refcount) {
  446. if (--entry->refcount)
  447. goto end;
  448. } else {
  449. ret = -EPERM;
  450. goto end;
  451. }
  452. end:
  453. mutex_unlock(&markers_mutex);
  454. marker_update_probes(NULL);
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(marker_disarm);
  458. /**
  459. * marker_get_private_data - Get a marker's probe private data
  460. * @name: marker name
  461. *
  462. * Returns the private data pointer, or an ERR_PTR.
  463. * The private data pointer should _only_ be dereferenced if the caller is the
  464. * owner of the data, or its content could vanish. This is mostly used to
  465. * confirm that a caller is the owner of a registered probe.
  466. */
  467. void *marker_get_private_data(const char *name)
  468. {
  469. struct hlist_head *head;
  470. struct hlist_node *node;
  471. struct marker_entry *e;
  472. size_t name_len = strlen(name) + 1;
  473. u32 hash = jhash(name, name_len-1, 0);
  474. int found = 0;
  475. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  476. hlist_for_each_entry(e, node, head, hlist) {
  477. if (!strcmp(name, e->name)) {
  478. found = 1;
  479. return e->private;
  480. }
  481. }
  482. return ERR_PTR(-ENOENT);
  483. }
  484. EXPORT_SYMBOL_GPL(marker_get_private_data);