marker.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. #include <linux/slab.h>
  27. extern struct marker __start___markers[];
  28. extern struct marker __stop___markers[];
  29. /* Set to 1 to enable marker debug output */
  30. static const int marker_debug;
  31. /*
  32. * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
  33. * and module markers and the hash table.
  34. */
  35. static DEFINE_MUTEX(markers_mutex);
  36. /*
  37. * Marker hash table, containing the active markers.
  38. * Protected by module_mutex.
  39. */
  40. #define MARKER_HASH_BITS 6
  41. #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
  42. /*
  43. * Note about RCU :
  44. * It is used to make sure every handler has finished using its private data
  45. * between two consecutive operation (add or remove) on a given marker. It is
  46. * also used to delay the free of multiple probes array until a quiescent state
  47. * is reached.
  48. * marker entries modifications are protected by the markers_mutex.
  49. */
  50. struct marker_entry {
  51. struct hlist_node hlist;
  52. char *format;
  53. /* Probe wrapper */
  54. void (*call)(const struct marker *mdata, void *call_private, ...);
  55. struct marker_probe_closure single;
  56. struct marker_probe_closure *multi;
  57. int refcount; /* Number of times armed. 0 if disarmed. */
  58. struct rcu_head rcu;
  59. void *oldptr;
  60. int rcu_pending;
  61. unsigned char ptype:1;
  62. unsigned char format_allocated:1;
  63. char name[0]; /* Contains name'\0'format'\0' */
  64. };
  65. static struct hlist_head marker_table[MARKER_TABLE_SIZE];
  66. /**
  67. * __mark_empty_function - Empty probe callback
  68. * @probe_private: probe private data
  69. * @call_private: call site private data
  70. * @fmt: format string
  71. * @...: variable argument list
  72. *
  73. * Empty callback provided as a probe to the markers. By providing this to a
  74. * disabled marker, we make sure the execution flow is always valid even
  75. * though the function pointer change and the marker enabling are two distinct
  76. * operations that modifies the execution flow of preemptible code.
  77. */
  78. void __mark_empty_function(void *probe_private, void *call_private,
  79. const char *fmt, va_list *args)
  80. {
  81. }
  82. EXPORT_SYMBOL_GPL(__mark_empty_function);
  83. /*
  84. * marker_probe_cb Callback that prepares the variable argument list for probes.
  85. * @mdata: pointer of type struct marker
  86. * @call_private: caller site private data
  87. * @...: Variable argument list.
  88. *
  89. * Since we do not use "typical" pointer based RCU in the 1 argument case, we
  90. * need to put a full smp_rmb() in this branch. This is why we do not use
  91. * rcu_dereference() for the pointer read.
  92. */
  93. void marker_probe_cb(const struct marker *mdata, void *call_private, ...)
  94. {
  95. va_list args;
  96. char ptype;
  97. /*
  98. * rcu_read_lock_sched does two things : disabling preemption to make
  99. * sure the teardown of the callbacks can be done correctly when they
  100. * are in modules and they insure RCU read coherency.
  101. */
  102. rcu_read_lock_sched();
  103. ptype = mdata->ptype;
  104. if (likely(!ptype)) {
  105. marker_probe_func *func;
  106. /* Must read the ptype before ptr. They are not data dependant,
  107. * so we put an explicit smp_rmb() here. */
  108. smp_rmb();
  109. func = mdata->single.func;
  110. /* Must read the ptr before private data. They are not data
  111. * dependant, so we put an explicit smp_rmb() here. */
  112. smp_rmb();
  113. va_start(args, call_private);
  114. func(mdata->single.probe_private, call_private, mdata->format,
  115. &args);
  116. va_end(args);
  117. } else {
  118. struct marker_probe_closure *multi;
  119. int i;
  120. /*
  121. * Read mdata->ptype before mdata->multi.
  122. */
  123. smp_rmb();
  124. multi = mdata->multi;
  125. /*
  126. * multi points to an array, therefore accessing the array
  127. * depends on reading multi. However, even in this case,
  128. * we must insure that the pointer is read _before_ the array
  129. * data. Same as rcu_dereference, but we need a full smp_rmb()
  130. * in the fast path, so put the explicit barrier here.
  131. */
  132. smp_read_barrier_depends();
  133. for (i = 0; multi[i].func; i++) {
  134. va_start(args, call_private);
  135. multi[i].func(multi[i].probe_private, call_private,
  136. mdata->format, &args);
  137. va_end(args);
  138. }
  139. }
  140. rcu_read_unlock_sched();
  141. }
  142. EXPORT_SYMBOL_GPL(marker_probe_cb);
  143. /*
  144. * marker_probe_cb Callback that does not prepare the variable argument list.
  145. * @mdata: pointer of type struct marker
  146. * @call_private: caller site private data
  147. * @...: Variable argument list.
  148. *
  149. * Should be connected to markers "MARK_NOARGS".
  150. */
  151. void marker_probe_cb_noarg(const struct marker *mdata, void *call_private, ...)
  152. {
  153. va_list args; /* not initialized */
  154. char ptype;
  155. rcu_read_lock_sched();
  156. ptype = mdata->ptype;
  157. if (likely(!ptype)) {
  158. marker_probe_func *func;
  159. /* Must read the ptype before ptr. They are not data dependant,
  160. * so we put an explicit smp_rmb() here. */
  161. smp_rmb();
  162. func = mdata->single.func;
  163. /* Must read the ptr before private data. They are not data
  164. * dependant, so we put an explicit smp_rmb() here. */
  165. smp_rmb();
  166. func(mdata->single.probe_private, call_private, mdata->format,
  167. &args);
  168. } else {
  169. struct marker_probe_closure *multi;
  170. int i;
  171. /*
  172. * Read mdata->ptype before mdata->multi.
  173. */
  174. smp_rmb();
  175. multi = mdata->multi;
  176. /*
  177. * multi points to an array, therefore accessing the array
  178. * depends on reading multi. However, even in this case,
  179. * we must insure that the pointer is read _before_ the array
  180. * data. Same as rcu_dereference, but we need a full smp_rmb()
  181. * in the fast path, so put the explicit barrier here.
  182. */
  183. smp_read_barrier_depends();
  184. for (i = 0; multi[i].func; i++)
  185. multi[i].func(multi[i].probe_private, call_private,
  186. mdata->format, &args);
  187. }
  188. rcu_read_unlock_sched();
  189. }
  190. EXPORT_SYMBOL_GPL(marker_probe_cb_noarg);
  191. static void free_old_closure(struct rcu_head *head)
  192. {
  193. struct marker_entry *entry = container_of(head,
  194. struct marker_entry, rcu);
  195. kfree(entry->oldptr);
  196. /* Make sure we free the data before setting the pending flag to 0 */
  197. smp_wmb();
  198. entry->rcu_pending = 0;
  199. }
  200. static void debug_print_probes(struct marker_entry *entry)
  201. {
  202. int i;
  203. if (!marker_debug)
  204. return;
  205. if (!entry->ptype) {
  206. printk(KERN_DEBUG "Single probe : %p %p\n",
  207. entry->single.func,
  208. entry->single.probe_private);
  209. } else {
  210. for (i = 0; entry->multi[i].func; i++)
  211. printk(KERN_DEBUG "Multi probe %d : %p %p\n", i,
  212. entry->multi[i].func,
  213. entry->multi[i].probe_private);
  214. }
  215. }
  216. static struct marker_probe_closure *
  217. marker_entry_add_probe(struct marker_entry *entry,
  218. marker_probe_func *probe, void *probe_private)
  219. {
  220. int nr_probes = 0;
  221. struct marker_probe_closure *old, *new;
  222. WARN_ON(!probe);
  223. debug_print_probes(entry);
  224. old = entry->multi;
  225. if (!entry->ptype) {
  226. if (entry->single.func == probe &&
  227. entry->single.probe_private == probe_private)
  228. return ERR_PTR(-EBUSY);
  229. if (entry->single.func == __mark_empty_function) {
  230. /* 0 -> 1 probes */
  231. entry->single.func = probe;
  232. entry->single.probe_private = probe_private;
  233. entry->refcount = 1;
  234. entry->ptype = 0;
  235. debug_print_probes(entry);
  236. return NULL;
  237. } else {
  238. /* 1 -> 2 probes */
  239. nr_probes = 1;
  240. old = NULL;
  241. }
  242. } else {
  243. /* (N -> N+1), (N != 0, 1) probes */
  244. for (nr_probes = 0; old[nr_probes].func; nr_probes++)
  245. if (old[nr_probes].func == probe
  246. && old[nr_probes].probe_private
  247. == probe_private)
  248. return ERR_PTR(-EBUSY);
  249. }
  250. /* + 2 : one for new probe, one for NULL func */
  251. new = kzalloc((nr_probes + 2) * sizeof(struct marker_probe_closure),
  252. GFP_KERNEL);
  253. if (new == NULL)
  254. return ERR_PTR(-ENOMEM);
  255. if (!old)
  256. new[0] = entry->single;
  257. else
  258. memcpy(new, old,
  259. nr_probes * sizeof(struct marker_probe_closure));
  260. new[nr_probes].func = probe;
  261. new[nr_probes].probe_private = probe_private;
  262. entry->refcount = nr_probes + 1;
  263. entry->multi = new;
  264. entry->ptype = 1;
  265. debug_print_probes(entry);
  266. return old;
  267. }
  268. static struct marker_probe_closure *
  269. marker_entry_remove_probe(struct marker_entry *entry,
  270. marker_probe_func *probe, void *probe_private)
  271. {
  272. int nr_probes = 0, nr_del = 0, i;
  273. struct marker_probe_closure *old, *new;
  274. old = entry->multi;
  275. debug_print_probes(entry);
  276. if (!entry->ptype) {
  277. /* 0 -> N is an error */
  278. WARN_ON(entry->single.func == __mark_empty_function);
  279. /* 1 -> 0 probes */
  280. WARN_ON(probe && entry->single.func != probe);
  281. WARN_ON(entry->single.probe_private != probe_private);
  282. entry->single.func = __mark_empty_function;
  283. entry->refcount = 0;
  284. entry->ptype = 0;
  285. debug_print_probes(entry);
  286. return NULL;
  287. } else {
  288. /* (N -> M), (N > 1, M >= 0) probes */
  289. for (nr_probes = 0; old[nr_probes].func; nr_probes++) {
  290. if ((!probe || old[nr_probes].func == probe)
  291. && old[nr_probes].probe_private
  292. == probe_private)
  293. nr_del++;
  294. }
  295. }
  296. if (nr_probes - nr_del == 0) {
  297. /* N -> 0, (N > 1) */
  298. entry->single.func = __mark_empty_function;
  299. entry->refcount = 0;
  300. entry->ptype = 0;
  301. } else if (nr_probes - nr_del == 1) {
  302. /* N -> 1, (N > 1) */
  303. for (i = 0; old[i].func; i++)
  304. if ((probe && old[i].func != probe) ||
  305. old[i].probe_private != probe_private)
  306. entry->single = old[i];
  307. entry->refcount = 1;
  308. entry->ptype = 0;
  309. } else {
  310. int j = 0;
  311. /* N -> M, (N > 1, M > 1) */
  312. /* + 1 for NULL */
  313. new = kzalloc((nr_probes - nr_del + 1)
  314. * sizeof(struct marker_probe_closure), GFP_KERNEL);
  315. if (new == NULL)
  316. return ERR_PTR(-ENOMEM);
  317. for (i = 0; old[i].func; i++)
  318. if ((probe && old[i].func != probe) ||
  319. old[i].probe_private != probe_private)
  320. new[j++] = old[i];
  321. entry->refcount = nr_probes - nr_del;
  322. entry->ptype = 1;
  323. entry->multi = new;
  324. }
  325. debug_print_probes(entry);
  326. return old;
  327. }
  328. /*
  329. * Get marker if the marker is present in the marker hash table.
  330. * Must be called with markers_mutex held.
  331. * Returns NULL if not present.
  332. */
  333. static struct marker_entry *get_marker(const char *name)
  334. {
  335. struct hlist_head *head;
  336. struct hlist_node *node;
  337. struct marker_entry *e;
  338. u32 hash = jhash(name, strlen(name), 0);
  339. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  340. hlist_for_each_entry(e, node, head, hlist) {
  341. if (!strcmp(name, e->name))
  342. return e;
  343. }
  344. return NULL;
  345. }
  346. /*
  347. * Add the marker to the marker hash table. Must be called with markers_mutex
  348. * held.
  349. */
  350. static struct marker_entry *add_marker(const char *name, const char *format)
  351. {
  352. struct hlist_head *head;
  353. struct hlist_node *node;
  354. struct marker_entry *e;
  355. size_t name_len = strlen(name) + 1;
  356. size_t format_len = 0;
  357. u32 hash = jhash(name, name_len-1, 0);
  358. if (format)
  359. format_len = strlen(format) + 1;
  360. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  361. hlist_for_each_entry(e, node, head, hlist) {
  362. if (!strcmp(name, e->name)) {
  363. printk(KERN_NOTICE
  364. "Marker %s busy\n", name);
  365. return ERR_PTR(-EBUSY); /* Already there */
  366. }
  367. }
  368. /*
  369. * Using kmalloc here to allocate a variable length element. Could
  370. * cause some memory fragmentation if overused.
  371. */
  372. e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
  373. GFP_KERNEL);
  374. if (!e)
  375. return ERR_PTR(-ENOMEM);
  376. memcpy(&e->name[0], name, name_len);
  377. if (format) {
  378. e->format = &e->name[name_len];
  379. memcpy(e->format, format, format_len);
  380. if (strcmp(e->format, MARK_NOARGS) == 0)
  381. e->call = marker_probe_cb_noarg;
  382. else
  383. e->call = marker_probe_cb;
  384. trace_mark(core_marker_format, "name %s format %s",
  385. e->name, e->format);
  386. } else {
  387. e->format = NULL;
  388. e->call = marker_probe_cb;
  389. }
  390. e->single.func = __mark_empty_function;
  391. e->single.probe_private = NULL;
  392. e->multi = NULL;
  393. e->ptype = 0;
  394. e->format_allocated = 0;
  395. e->refcount = 0;
  396. e->rcu_pending = 0;
  397. hlist_add_head(&e->hlist, head);
  398. return e;
  399. }
  400. /*
  401. * Remove the marker from the marker hash table. Must be called with mutex_lock
  402. * held.
  403. */
  404. static int remove_marker(const char *name)
  405. {
  406. struct hlist_head *head;
  407. struct hlist_node *node;
  408. struct marker_entry *e;
  409. int found = 0;
  410. size_t len = strlen(name) + 1;
  411. u32 hash = jhash(name, len-1, 0);
  412. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  413. hlist_for_each_entry(e, node, head, hlist) {
  414. if (!strcmp(name, e->name)) {
  415. found = 1;
  416. break;
  417. }
  418. }
  419. if (!found)
  420. return -ENOENT;
  421. if (e->single.func != __mark_empty_function)
  422. return -EBUSY;
  423. hlist_del(&e->hlist);
  424. if (e->format_allocated)
  425. kfree(e->format);
  426. /* Make sure the call_rcu has been executed */
  427. if (e->rcu_pending)
  428. rcu_barrier_sched();
  429. kfree(e);
  430. return 0;
  431. }
  432. /*
  433. * Set the mark_entry format to the format found in the element.
  434. */
  435. static int marker_set_format(struct marker_entry *entry, const char *format)
  436. {
  437. entry->format = kstrdup(format, GFP_KERNEL);
  438. if (!entry->format)
  439. return -ENOMEM;
  440. entry->format_allocated = 1;
  441. trace_mark(core_marker_format, "name %s format %s",
  442. entry->name, entry->format);
  443. return 0;
  444. }
  445. /*
  446. * Sets the probe callback corresponding to one marker.
  447. */
  448. static int set_marker(struct marker_entry *entry, struct marker *elem,
  449. int active)
  450. {
  451. int ret;
  452. WARN_ON(strcmp(entry->name, elem->name) != 0);
  453. if (entry->format) {
  454. if (strcmp(entry->format, elem->format) != 0) {
  455. printk(KERN_NOTICE
  456. "Format mismatch for probe %s "
  457. "(%s), marker (%s)\n",
  458. entry->name,
  459. entry->format,
  460. elem->format);
  461. return -EPERM;
  462. }
  463. } else {
  464. ret = marker_set_format(entry, elem->format);
  465. if (ret)
  466. return ret;
  467. }
  468. /*
  469. * probe_cb setup (statically known) is done here. It is
  470. * asynchronous with the rest of execution, therefore we only
  471. * pass from a "safe" callback (with argument) to an "unsafe"
  472. * callback (does not set arguments).
  473. */
  474. elem->call = entry->call;
  475. /*
  476. * Sanity check :
  477. * We only update the single probe private data when the ptr is
  478. * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
  479. */
  480. WARN_ON(elem->single.func != __mark_empty_function
  481. && elem->single.probe_private != entry->single.probe_private
  482. && !elem->ptype);
  483. elem->single.probe_private = entry->single.probe_private;
  484. /*
  485. * Make sure the private data is valid when we update the
  486. * single probe ptr.
  487. */
  488. smp_wmb();
  489. elem->single.func = entry->single.func;
  490. /*
  491. * We also make sure that the new probe callbacks array is consistent
  492. * before setting a pointer to it.
  493. */
  494. rcu_assign_pointer(elem->multi, entry->multi);
  495. /*
  496. * Update the function or multi probe array pointer before setting the
  497. * ptype.
  498. */
  499. smp_wmb();
  500. elem->ptype = entry->ptype;
  501. elem->state = active;
  502. return 0;
  503. }
  504. /*
  505. * Disable a marker and its probe callback.
  506. * Note: only waiting an RCU period after setting elem->call to the empty
  507. * function insures that the original callback is not used anymore. This insured
  508. * by rcu_read_lock_sched around the call site.
  509. */
  510. static void disable_marker(struct marker *elem)
  511. {
  512. /* leave "call" as is. It is known statically. */
  513. elem->state = 0;
  514. elem->single.func = __mark_empty_function;
  515. /* Update the function before setting the ptype */
  516. smp_wmb();
  517. elem->ptype = 0; /* single probe */
  518. /*
  519. * Leave the private data and id there, because removal is racy and
  520. * should be done only after an RCU period. These are never used until
  521. * the next initialization anyway.
  522. */
  523. }
  524. /**
  525. * marker_update_probe_range - Update a probe range
  526. * @begin: beginning of the range
  527. * @end: end of the range
  528. *
  529. * Updates the probe callback corresponding to a range of markers.
  530. */
  531. void marker_update_probe_range(struct marker *begin,
  532. struct marker *end)
  533. {
  534. struct marker *iter;
  535. struct marker_entry *mark_entry;
  536. mutex_lock(&markers_mutex);
  537. for (iter = begin; iter < end; iter++) {
  538. mark_entry = get_marker(iter->name);
  539. if (mark_entry) {
  540. set_marker(mark_entry, iter, !!mark_entry->refcount);
  541. /*
  542. * ignore error, continue
  543. */
  544. } else {
  545. disable_marker(iter);
  546. }
  547. }
  548. mutex_unlock(&markers_mutex);
  549. }
  550. /*
  551. * Update probes, removing the faulty probes.
  552. *
  553. * Internal callback only changed before the first probe is connected to it.
  554. * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
  555. * transitions. All other transitions will leave the old private data valid.
  556. * This makes the non-atomicity of the callback/private data updates valid.
  557. *
  558. * "special case" updates :
  559. * 0 -> 1 callback
  560. * 1 -> 0 callback
  561. * 1 -> 2 callbacks
  562. * 2 -> 1 callbacks
  563. * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
  564. * Site effect : marker_set_format may delete the marker entry (creating a
  565. * replacement).
  566. */
  567. static void marker_update_probes(void)
  568. {
  569. /* Core kernel markers */
  570. marker_update_probe_range(__start___markers, __stop___markers);
  571. /* Markers in modules. */
  572. module_update_markers();
  573. }
  574. /**
  575. * marker_probe_register - Connect a probe to a marker
  576. * @name: marker name
  577. * @format: format string
  578. * @probe: probe handler
  579. * @probe_private: probe private data
  580. *
  581. * private data must be a valid allocated memory address, or NULL.
  582. * Returns 0 if ok, error value on error.
  583. * The probe address must at least be aligned on the architecture pointer size.
  584. */
  585. int marker_probe_register(const char *name, const char *format,
  586. marker_probe_func *probe, void *probe_private)
  587. {
  588. struct marker_entry *entry;
  589. int ret = 0;
  590. struct marker_probe_closure *old;
  591. mutex_lock(&markers_mutex);
  592. entry = get_marker(name);
  593. if (!entry) {
  594. entry = add_marker(name, format);
  595. if (IS_ERR(entry))
  596. ret = PTR_ERR(entry);
  597. } else if (format) {
  598. if (!entry->format)
  599. ret = marker_set_format(entry, format);
  600. else if (strcmp(entry->format, format))
  601. ret = -EPERM;
  602. }
  603. if (ret)
  604. goto end;
  605. /*
  606. * If we detect that a call_rcu is pending for this marker,
  607. * make sure it's executed now.
  608. */
  609. if (entry->rcu_pending)
  610. rcu_barrier_sched();
  611. old = marker_entry_add_probe(entry, probe, probe_private);
  612. if (IS_ERR(old)) {
  613. ret = PTR_ERR(old);
  614. goto end;
  615. }
  616. mutex_unlock(&markers_mutex);
  617. marker_update_probes(); /* may update entry */
  618. mutex_lock(&markers_mutex);
  619. entry = get_marker(name);
  620. WARN_ON(!entry);
  621. if (entry->rcu_pending)
  622. rcu_barrier_sched();
  623. entry->oldptr = old;
  624. entry->rcu_pending = 1;
  625. /* write rcu_pending before calling the RCU callback */
  626. smp_wmb();
  627. call_rcu_sched(&entry->rcu, free_old_closure);
  628. end:
  629. mutex_unlock(&markers_mutex);
  630. return ret;
  631. }
  632. EXPORT_SYMBOL_GPL(marker_probe_register);
  633. /**
  634. * marker_probe_unregister - Disconnect a probe from a marker
  635. * @name: marker name
  636. * @probe: probe function pointer
  637. * @probe_private: probe private data
  638. *
  639. * Returns the private data given to marker_probe_register, or an ERR_PTR().
  640. * We do not need to call a synchronize_sched to make sure the probes have
  641. * finished running before doing a module unload, because the module unload
  642. * itself uses stop_machine(), which insures that every preempt disabled section
  643. * have finished.
  644. */
  645. int marker_probe_unregister(const char *name,
  646. marker_probe_func *probe, void *probe_private)
  647. {
  648. struct marker_entry *entry;
  649. struct marker_probe_closure *old;
  650. int ret = -ENOENT;
  651. mutex_lock(&markers_mutex);
  652. entry = get_marker(name);
  653. if (!entry)
  654. goto end;
  655. if (entry->rcu_pending)
  656. rcu_barrier_sched();
  657. old = marker_entry_remove_probe(entry, probe, probe_private);
  658. mutex_unlock(&markers_mutex);
  659. marker_update_probes(); /* may update entry */
  660. mutex_lock(&markers_mutex);
  661. entry = get_marker(name);
  662. if (!entry)
  663. goto end;
  664. if (entry->rcu_pending)
  665. rcu_barrier_sched();
  666. entry->oldptr = old;
  667. entry->rcu_pending = 1;
  668. /* write rcu_pending before calling the RCU callback */
  669. smp_wmb();
  670. call_rcu_sched(&entry->rcu, free_old_closure);
  671. remove_marker(name); /* Ignore busy error message */
  672. ret = 0;
  673. end:
  674. mutex_unlock(&markers_mutex);
  675. return ret;
  676. }
  677. EXPORT_SYMBOL_GPL(marker_probe_unregister);
  678. static struct marker_entry *
  679. get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
  680. {
  681. struct marker_entry *entry;
  682. unsigned int i;
  683. struct hlist_head *head;
  684. struct hlist_node *node;
  685. for (i = 0; i < MARKER_TABLE_SIZE; i++) {
  686. head = &marker_table[i];
  687. hlist_for_each_entry(entry, node, head, hlist) {
  688. if (!entry->ptype) {
  689. if (entry->single.func == probe
  690. && entry->single.probe_private
  691. == probe_private)
  692. return entry;
  693. } else {
  694. struct marker_probe_closure *closure;
  695. closure = entry->multi;
  696. for (i = 0; closure[i].func; i++) {
  697. if (closure[i].func == probe &&
  698. closure[i].probe_private
  699. == probe_private)
  700. return entry;
  701. }
  702. }
  703. }
  704. }
  705. return NULL;
  706. }
  707. /**
  708. * marker_probe_unregister_private_data - Disconnect a probe from a marker
  709. * @probe: probe function
  710. * @probe_private: probe private data
  711. *
  712. * Unregister a probe by providing the registered private data.
  713. * Only removes the first marker found in hash table.
  714. * Return 0 on success or error value.
  715. * We do not need to call a synchronize_sched to make sure the probes have
  716. * finished running before doing a module unload, because the module unload
  717. * itself uses stop_machine(), which insures that every preempt disabled section
  718. * have finished.
  719. */
  720. int marker_probe_unregister_private_data(marker_probe_func *probe,
  721. void *probe_private)
  722. {
  723. struct marker_entry *entry;
  724. int ret = 0;
  725. struct marker_probe_closure *old;
  726. mutex_lock(&markers_mutex);
  727. entry = get_marker_from_private_data(probe, probe_private);
  728. if (!entry) {
  729. ret = -ENOENT;
  730. goto end;
  731. }
  732. if (entry->rcu_pending)
  733. rcu_barrier_sched();
  734. old = marker_entry_remove_probe(entry, NULL, probe_private);
  735. mutex_unlock(&markers_mutex);
  736. marker_update_probes(); /* may update entry */
  737. mutex_lock(&markers_mutex);
  738. entry = get_marker_from_private_data(probe, probe_private);
  739. WARN_ON(!entry);
  740. if (entry->rcu_pending)
  741. rcu_barrier_sched();
  742. entry->oldptr = old;
  743. entry->rcu_pending = 1;
  744. /* write rcu_pending before calling the RCU callback */
  745. smp_wmb();
  746. call_rcu_sched(&entry->rcu, free_old_closure);
  747. remove_marker(entry->name); /* Ignore busy error message */
  748. end:
  749. mutex_unlock(&markers_mutex);
  750. return ret;
  751. }
  752. EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
  753. /**
  754. * marker_get_private_data - Get a marker's probe private data
  755. * @name: marker name
  756. * @probe: probe to match
  757. * @num: get the nth matching probe's private data
  758. *
  759. * Returns the nth private data pointer (starting from 0) matching, or an
  760. * ERR_PTR.
  761. * Returns the private data pointer, or an ERR_PTR.
  762. * The private data pointer should _only_ be dereferenced if the caller is the
  763. * owner of the data, or its content could vanish. This is mostly used to
  764. * confirm that a caller is the owner of a registered probe.
  765. */
  766. void *marker_get_private_data(const char *name, marker_probe_func *probe,
  767. int num)
  768. {
  769. struct hlist_head *head;
  770. struct hlist_node *node;
  771. struct marker_entry *e;
  772. size_t name_len = strlen(name) + 1;
  773. u32 hash = jhash(name, name_len-1, 0);
  774. int i;
  775. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  776. hlist_for_each_entry(e, node, head, hlist) {
  777. if (!strcmp(name, e->name)) {
  778. if (!e->ptype) {
  779. if (num == 0 && e->single.func == probe)
  780. return e->single.probe_private;
  781. else
  782. break;
  783. } else {
  784. struct marker_probe_closure *closure;
  785. int match = 0;
  786. closure = e->multi;
  787. for (i = 0; closure[i].func; i++) {
  788. if (closure[i].func != probe)
  789. continue;
  790. if (match++ == num)
  791. return closure[i].probe_private;
  792. }
  793. }
  794. }
  795. }
  796. return ERR_PTR(-ENOENT);
  797. }
  798. EXPORT_SYMBOL_GPL(marker_get_private_data);