marker.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Copyright (C) 2007 Mathieu Desnoyers
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/types.h>
  21. #include <linux/jhash.h>
  22. #include <linux/list.h>
  23. #include <linux/rcupdate.h>
  24. #include <linux/marker.h>
  25. #include <linux/err.h>
  26. #include <linux/slab.h>
  27. extern struct marker __start___markers[];
  28. extern struct marker __stop___markers[];
  29. /* Set to 1 to enable marker debug output */
  30. static const int marker_debug;
  31. /*
  32. * markers_mutex nests inside module_mutex. Markers mutex protects the builtin
  33. * and module markers and the hash table.
  34. */
  35. static DEFINE_MUTEX(markers_mutex);
  36. /*
  37. * Marker hash table, containing the active markers.
  38. * Protected by module_mutex.
  39. */
  40. #define MARKER_HASH_BITS 6
  41. #define MARKER_TABLE_SIZE (1 << MARKER_HASH_BITS)
  42. /*
  43. * Note about RCU :
  44. * It is used to make sure every handler has finished using its private data
  45. * between two consecutive operation (add or remove) on a given marker. It is
  46. * also used to delay the free of multiple probes array until a quiescent state
  47. * is reached.
  48. * marker entries modifications are protected by the markers_mutex.
  49. */
  50. struct marker_entry {
  51. struct hlist_node hlist;
  52. char *format;
  53. /* Probe wrapper */
  54. void (*call)(const struct marker *mdata, void *call_private, ...);
  55. struct marker_probe_closure single;
  56. struct marker_probe_closure *multi;
  57. int refcount; /* Number of times armed. 0 if disarmed. */
  58. struct rcu_head rcu;
  59. void *oldptr;
  60. int rcu_pending;
  61. unsigned char ptype:1;
  62. 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. * rcu_read_lock_sched does two things : disabling preemption to make
  98. * sure the teardown of the callbacks can be done correctly when they
  99. * are in modules and they insure RCU read coherency.
  100. */
  101. rcu_read_lock_sched();
  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. * Read mdata->ptype before mdata->multi.
  121. */
  122. smp_rmb();
  123. multi = mdata->multi;
  124. /*
  125. * multi points to an array, therefore accessing the array
  126. * depends on reading multi. However, even in this case,
  127. * we must insure that the pointer is read _before_ the array
  128. * data. Same as rcu_dereference, but we need a full smp_rmb()
  129. * in the fast path, so put the explicit barrier here.
  130. */
  131. smp_read_barrier_depends();
  132. for (i = 0; multi[i].func; i++) {
  133. va_start(args, call_private);
  134. multi[i].func(multi[i].probe_private, call_private,
  135. mdata->format, &args);
  136. va_end(args);
  137. }
  138. }
  139. rcu_read_unlock_sched();
  140. }
  141. EXPORT_SYMBOL_GPL(marker_probe_cb);
  142. /*
  143. * marker_probe_cb Callback that does not prepare the variable argument list.
  144. * @mdata: pointer of type struct marker
  145. * @call_private: caller site private data
  146. * @...: Variable argument list.
  147. *
  148. * Should be connected to markers "MARK_NOARGS".
  149. */
  150. void marker_probe_cb_noarg(const struct marker *mdata, void *call_private, ...)
  151. {
  152. va_list args; /* not initialized */
  153. char ptype;
  154. rcu_read_lock_sched();
  155. ptype = mdata->ptype;
  156. if (likely(!ptype)) {
  157. marker_probe_func *func;
  158. /* Must read the ptype before ptr. They are not data dependant,
  159. * so we put an explicit smp_rmb() here. */
  160. smp_rmb();
  161. func = mdata->single.func;
  162. /* Must read the ptr before private data. They are not data
  163. * dependant, so we put an explicit smp_rmb() here. */
  164. smp_rmb();
  165. func(mdata->single.probe_private, call_private, mdata->format,
  166. &args);
  167. } else {
  168. struct marker_probe_closure *multi;
  169. int i;
  170. /*
  171. * Read mdata->ptype before mdata->multi.
  172. */
  173. smp_rmb();
  174. multi = mdata->multi;
  175. /*
  176. * multi points to an array, therefore accessing the array
  177. * depends on reading multi. However, even in this case,
  178. * we must insure that the pointer is read _before_ the array
  179. * data. Same as rcu_dereference, but we need a full smp_rmb()
  180. * in the fast path, so put the explicit barrier here.
  181. */
  182. smp_read_barrier_depends();
  183. for (i = 0; multi[i].func; i++)
  184. multi[i].func(multi[i].probe_private, call_private,
  185. mdata->format, &args);
  186. }
  187. rcu_read_unlock_sched();
  188. }
  189. EXPORT_SYMBOL_GPL(marker_probe_cb_noarg);
  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->refcount = 0;
  394. e->rcu_pending = 0;
  395. hlist_add_head(&e->hlist, head);
  396. return e;
  397. }
  398. /*
  399. * Remove the marker from the marker hash table. Must be called with mutex_lock
  400. * held.
  401. */
  402. static int remove_marker(const char *name)
  403. {
  404. struct hlist_head *head;
  405. struct hlist_node *node;
  406. struct marker_entry *e;
  407. int found = 0;
  408. size_t len = strlen(name) + 1;
  409. u32 hash = jhash(name, len-1, 0);
  410. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  411. hlist_for_each_entry(e, node, head, hlist) {
  412. if (!strcmp(name, e->name)) {
  413. found = 1;
  414. break;
  415. }
  416. }
  417. if (!found)
  418. return -ENOENT;
  419. if (e->single.func != __mark_empty_function)
  420. return -EBUSY;
  421. hlist_del(&e->hlist);
  422. /* Make sure the call_rcu has been executed */
  423. if (e->rcu_pending)
  424. rcu_barrier_sched();
  425. kfree(e);
  426. return 0;
  427. }
  428. /*
  429. * Set the mark_entry format to the format found in the element.
  430. */
  431. static int marker_set_format(struct marker_entry **entry, const char *format)
  432. {
  433. struct marker_entry *e;
  434. size_t name_len = strlen((*entry)->name) + 1;
  435. size_t format_len = strlen(format) + 1;
  436. e = kmalloc(sizeof(struct marker_entry) + name_len + format_len,
  437. GFP_KERNEL);
  438. if (!e)
  439. return -ENOMEM;
  440. memcpy(&e->name[0], (*entry)->name, name_len);
  441. e->format = &e->name[name_len];
  442. memcpy(e->format, format, format_len);
  443. if (strcmp(e->format, MARK_NOARGS) == 0)
  444. e->call = marker_probe_cb_noarg;
  445. else
  446. e->call = marker_probe_cb;
  447. e->single = (*entry)->single;
  448. e->multi = (*entry)->multi;
  449. e->ptype = (*entry)->ptype;
  450. e->refcount = (*entry)->refcount;
  451. e->rcu_pending = 0;
  452. hlist_add_before(&e->hlist, &(*entry)->hlist);
  453. hlist_del(&(*entry)->hlist);
  454. /* Make sure the call_rcu has been executed */
  455. if ((*entry)->rcu_pending)
  456. rcu_barrier_sched();
  457. kfree(*entry);
  458. *entry = e;
  459. trace_mark(core_marker_format, "name %s format %s",
  460. e->name, e->format);
  461. return 0;
  462. }
  463. /*
  464. * Sets the probe callback corresponding to one marker.
  465. */
  466. static int set_marker(struct marker_entry **entry, struct marker *elem,
  467. int active)
  468. {
  469. int ret;
  470. WARN_ON(strcmp((*entry)->name, elem->name) != 0);
  471. if ((*entry)->format) {
  472. if (strcmp((*entry)->format, elem->format) != 0) {
  473. printk(KERN_NOTICE
  474. "Format mismatch for probe %s "
  475. "(%s), marker (%s)\n",
  476. (*entry)->name,
  477. (*entry)->format,
  478. elem->format);
  479. return -EPERM;
  480. }
  481. } else {
  482. ret = marker_set_format(entry, elem->format);
  483. if (ret)
  484. return ret;
  485. }
  486. /*
  487. * probe_cb setup (statically known) is done here. It is
  488. * asynchronous with the rest of execution, therefore we only
  489. * pass from a "safe" callback (with argument) to an "unsafe"
  490. * callback (does not set arguments).
  491. */
  492. elem->call = (*entry)->call;
  493. /*
  494. * Sanity check :
  495. * We only update the single probe private data when the ptr is
  496. * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
  497. */
  498. WARN_ON(elem->single.func != __mark_empty_function
  499. && elem->single.probe_private
  500. != (*entry)->single.probe_private &&
  501. !elem->ptype);
  502. elem->single.probe_private = (*entry)->single.probe_private;
  503. /*
  504. * Make sure the private data is valid when we update the
  505. * single probe ptr.
  506. */
  507. smp_wmb();
  508. elem->single.func = (*entry)->single.func;
  509. /*
  510. * We also make sure that the new probe callbacks array is consistent
  511. * before setting a pointer to it.
  512. */
  513. rcu_assign_pointer(elem->multi, (*entry)->multi);
  514. /*
  515. * Update the function or multi probe array pointer before setting the
  516. * ptype.
  517. */
  518. smp_wmb();
  519. elem->ptype = (*entry)->ptype;
  520. elem->state = active;
  521. return 0;
  522. }
  523. /*
  524. * Disable a marker and its probe callback.
  525. * Note: only waiting an RCU period after setting elem->call to the empty
  526. * function insures that the original callback is not used anymore. This insured
  527. * by rcu_read_lock_sched around the call site.
  528. */
  529. static void disable_marker(struct marker *elem)
  530. {
  531. /* leave "call" as is. It is known statically. */
  532. elem->state = 0;
  533. elem->single.func = __mark_empty_function;
  534. /* Update the function before setting the ptype */
  535. smp_wmb();
  536. elem->ptype = 0; /* single probe */
  537. /*
  538. * Leave the private data and id there, because removal is racy and
  539. * should be done only after an RCU period. These are never used until
  540. * the next initialization anyway.
  541. */
  542. }
  543. /**
  544. * marker_update_probe_range - Update a probe range
  545. * @begin: beginning of the range
  546. * @end: end of the range
  547. *
  548. * Updates the probe callback corresponding to a range of markers.
  549. */
  550. void marker_update_probe_range(struct marker *begin,
  551. struct marker *end)
  552. {
  553. struct marker *iter;
  554. struct marker_entry *mark_entry;
  555. mutex_lock(&markers_mutex);
  556. for (iter = begin; iter < end; iter++) {
  557. mark_entry = get_marker(iter->name);
  558. if (mark_entry) {
  559. set_marker(&mark_entry, iter,
  560. !!mark_entry->refcount);
  561. /*
  562. * ignore error, continue
  563. */
  564. } else {
  565. disable_marker(iter);
  566. }
  567. }
  568. mutex_unlock(&markers_mutex);
  569. }
  570. /*
  571. * Update probes, removing the faulty probes.
  572. *
  573. * Internal callback only changed before the first probe is connected to it.
  574. * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
  575. * transitions. All other transitions will leave the old private data valid.
  576. * This makes the non-atomicity of the callback/private data updates valid.
  577. *
  578. * "special case" updates :
  579. * 0 -> 1 callback
  580. * 1 -> 0 callback
  581. * 1 -> 2 callbacks
  582. * 2 -> 1 callbacks
  583. * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
  584. * Site effect : marker_set_format may delete the marker entry (creating a
  585. * replacement).
  586. */
  587. static void marker_update_probes(void)
  588. {
  589. /* Core kernel markers */
  590. marker_update_probe_range(__start___markers, __stop___markers);
  591. /* Markers in modules. */
  592. module_update_markers();
  593. }
  594. /**
  595. * marker_probe_register - Connect a probe to a marker
  596. * @name: marker name
  597. * @format: format string
  598. * @probe: probe handler
  599. * @probe_private: probe private data
  600. *
  601. * private data must be a valid allocated memory address, or NULL.
  602. * Returns 0 if ok, error value on error.
  603. * The probe address must at least be aligned on the architecture pointer size.
  604. */
  605. int marker_probe_register(const char *name, const char *format,
  606. marker_probe_func *probe, void *probe_private)
  607. {
  608. struct marker_entry *entry;
  609. int ret = 0;
  610. struct marker_probe_closure *old;
  611. mutex_lock(&markers_mutex);
  612. entry = get_marker(name);
  613. if (!entry) {
  614. entry = add_marker(name, format);
  615. if (IS_ERR(entry))
  616. ret = PTR_ERR(entry);
  617. } else if (format) {
  618. if (!entry->format)
  619. ret = marker_set_format(&entry, format);
  620. else if (strcmp(entry->format, format))
  621. ret = -EPERM;
  622. }
  623. if (ret)
  624. goto end;
  625. /*
  626. * If we detect that a call_rcu is pending for this marker,
  627. * make sure it's executed now.
  628. */
  629. if (entry->rcu_pending)
  630. rcu_barrier_sched();
  631. old = marker_entry_add_probe(entry, probe, probe_private);
  632. if (IS_ERR(old)) {
  633. ret = PTR_ERR(old);
  634. goto end;
  635. }
  636. mutex_unlock(&markers_mutex);
  637. marker_update_probes(); /* may update entry */
  638. mutex_lock(&markers_mutex);
  639. entry = get_marker(name);
  640. WARN_ON(!entry);
  641. if (entry->rcu_pending)
  642. rcu_barrier_sched();
  643. entry->oldptr = old;
  644. entry->rcu_pending = 1;
  645. /* write rcu_pending before calling the RCU callback */
  646. smp_wmb();
  647. call_rcu_sched(&entry->rcu, free_old_closure);
  648. end:
  649. mutex_unlock(&markers_mutex);
  650. return ret;
  651. }
  652. EXPORT_SYMBOL_GPL(marker_probe_register);
  653. /**
  654. * marker_probe_unregister - Disconnect a probe from a marker
  655. * @name: marker name
  656. * @probe: probe function pointer
  657. * @probe_private: probe private data
  658. *
  659. * Returns the private data given to marker_probe_register, or an ERR_PTR().
  660. * We do not need to call a synchronize_sched to make sure the probes have
  661. * finished running before doing a module unload, because the module unload
  662. * itself uses stop_machine(), which insures that every preempt disabled section
  663. * have finished.
  664. */
  665. int marker_probe_unregister(const char *name,
  666. marker_probe_func *probe, void *probe_private)
  667. {
  668. struct marker_entry *entry;
  669. struct marker_probe_closure *old;
  670. int ret = -ENOENT;
  671. mutex_lock(&markers_mutex);
  672. entry = get_marker(name);
  673. if (!entry)
  674. goto end;
  675. if (entry->rcu_pending)
  676. rcu_barrier_sched();
  677. old = marker_entry_remove_probe(entry, probe, probe_private);
  678. mutex_unlock(&markers_mutex);
  679. marker_update_probes(); /* may update entry */
  680. mutex_lock(&markers_mutex);
  681. entry = get_marker(name);
  682. if (!entry)
  683. goto end;
  684. if (entry->rcu_pending)
  685. rcu_barrier_sched();
  686. entry->oldptr = old;
  687. entry->rcu_pending = 1;
  688. /* write rcu_pending before calling the RCU callback */
  689. smp_wmb();
  690. call_rcu_sched(&entry->rcu, free_old_closure);
  691. remove_marker(name); /* Ignore busy error message */
  692. ret = 0;
  693. end:
  694. mutex_unlock(&markers_mutex);
  695. return ret;
  696. }
  697. EXPORT_SYMBOL_GPL(marker_probe_unregister);
  698. static struct marker_entry *
  699. get_marker_from_private_data(marker_probe_func *probe, void *probe_private)
  700. {
  701. struct marker_entry *entry;
  702. unsigned int i;
  703. struct hlist_head *head;
  704. struct hlist_node *node;
  705. for (i = 0; i < MARKER_TABLE_SIZE; i++) {
  706. head = &marker_table[i];
  707. hlist_for_each_entry(entry, node, head, hlist) {
  708. if (!entry->ptype) {
  709. if (entry->single.func == probe
  710. && entry->single.probe_private
  711. == probe_private)
  712. return entry;
  713. } else {
  714. struct marker_probe_closure *closure;
  715. closure = entry->multi;
  716. for (i = 0; closure[i].func; i++) {
  717. if (closure[i].func == probe &&
  718. closure[i].probe_private
  719. == probe_private)
  720. return entry;
  721. }
  722. }
  723. }
  724. }
  725. return NULL;
  726. }
  727. /**
  728. * marker_probe_unregister_private_data - Disconnect a probe from a marker
  729. * @probe: probe function
  730. * @probe_private: probe private data
  731. *
  732. * Unregister a probe by providing the registered private data.
  733. * Only removes the first marker found in hash table.
  734. * Return 0 on success or error value.
  735. * We do not need to call a synchronize_sched to make sure the probes have
  736. * finished running before doing a module unload, because the module unload
  737. * itself uses stop_machine(), which insures that every preempt disabled section
  738. * have finished.
  739. */
  740. int marker_probe_unregister_private_data(marker_probe_func *probe,
  741. void *probe_private)
  742. {
  743. struct marker_entry *entry;
  744. int ret = 0;
  745. struct marker_probe_closure *old;
  746. mutex_lock(&markers_mutex);
  747. entry = get_marker_from_private_data(probe, probe_private);
  748. if (!entry) {
  749. ret = -ENOENT;
  750. goto end;
  751. }
  752. if (entry->rcu_pending)
  753. rcu_barrier_sched();
  754. old = marker_entry_remove_probe(entry, NULL, probe_private);
  755. mutex_unlock(&markers_mutex);
  756. marker_update_probes(); /* may update entry */
  757. mutex_lock(&markers_mutex);
  758. entry = get_marker_from_private_data(probe, probe_private);
  759. WARN_ON(!entry);
  760. if (entry->rcu_pending)
  761. rcu_barrier_sched();
  762. entry->oldptr = old;
  763. entry->rcu_pending = 1;
  764. /* write rcu_pending before calling the RCU callback */
  765. smp_wmb();
  766. call_rcu_sched(&entry->rcu, free_old_closure);
  767. remove_marker(entry->name); /* Ignore busy error message */
  768. end:
  769. mutex_unlock(&markers_mutex);
  770. return ret;
  771. }
  772. EXPORT_SYMBOL_GPL(marker_probe_unregister_private_data);
  773. /**
  774. * marker_get_private_data - Get a marker's probe private data
  775. * @name: marker name
  776. * @probe: probe to match
  777. * @num: get the nth matching probe's private data
  778. *
  779. * Returns the nth private data pointer (starting from 0) matching, or an
  780. * ERR_PTR.
  781. * Returns the private data pointer, or an ERR_PTR.
  782. * The private data pointer should _only_ be dereferenced if the caller is the
  783. * owner of the data, or its content could vanish. This is mostly used to
  784. * confirm that a caller is the owner of a registered probe.
  785. */
  786. void *marker_get_private_data(const char *name, marker_probe_func *probe,
  787. int num)
  788. {
  789. struct hlist_head *head;
  790. struct hlist_node *node;
  791. struct marker_entry *e;
  792. size_t name_len = strlen(name) + 1;
  793. u32 hash = jhash(name, name_len-1, 0);
  794. int i;
  795. head = &marker_table[hash & ((1 << MARKER_HASH_BITS)-1)];
  796. hlist_for_each_entry(e, node, head, hlist) {
  797. if (!strcmp(name, e->name)) {
  798. if (!e->ptype) {
  799. if (num == 0 && e->single.func == probe)
  800. return e->single.probe_private;
  801. else
  802. break;
  803. } else {
  804. struct marker_probe_closure *closure;
  805. int match = 0;
  806. closure = e->multi;
  807. for (i = 0; closure[i].func; i++) {
  808. if (closure[i].func != probe)
  809. continue;
  810. if (match++ == num)
  811. return closure[i].probe_private;
  812. }
  813. }
  814. }
  815. }
  816. return ERR_PTR(-ENOENT);
  817. }
  818. EXPORT_SYMBOL_GPL(marker_get_private_data);