marker.c 25 KB

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