marker.c 23 KB

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