marker.c 24 KB

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