marker.c 24 KB

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