marker.c 24 KB

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