rcu_segcblist.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * RCU segmented callback lists, function definitions
  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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright IBM Corporation, 2017
  19. *
  20. * Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  21. */
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/interrupt.h>
  25. #include "rcu_segcblist.h"
  26. /* Initialize simple callback list. */
  27. void rcu_cblist_init(struct rcu_cblist *rclp)
  28. {
  29. rclp->head = NULL;
  30. rclp->tail = &rclp->head;
  31. rclp->len = 0;
  32. rclp->len_lazy = 0;
  33. }
  34. /*
  35. * Dequeue the oldest rcu_head structure from the specified callback
  36. * list. This function assumes that the callback is non-lazy, but
  37. * the caller can later invoke rcu_cblist_dequeued_lazy() if it
  38. * finds otherwise (and if it cares about laziness). This allows
  39. * different users to have different ways of determining laziness.
  40. */
  41. struct rcu_head *rcu_cblist_dequeue(struct rcu_cblist *rclp)
  42. {
  43. struct rcu_head *rhp;
  44. rhp = rclp->head;
  45. if (!rhp)
  46. return NULL;
  47. rclp->len--;
  48. rclp->head = rhp->next;
  49. if (!rclp->head)
  50. rclp->tail = &rclp->head;
  51. return rhp;
  52. }
  53. /*
  54. * Initialize an rcu_segcblist structure.
  55. */
  56. void rcu_segcblist_init(struct rcu_segcblist *rsclp)
  57. {
  58. int i;
  59. BUILD_BUG_ON(RCU_NEXT_TAIL + 1 != ARRAY_SIZE(rsclp->gp_seq));
  60. BUILD_BUG_ON(ARRAY_SIZE(rsclp->tails) != ARRAY_SIZE(rsclp->gp_seq));
  61. rsclp->head = NULL;
  62. for (i = 0; i < RCU_CBLIST_NSEGS; i++)
  63. rsclp->tails[i] = &rsclp->head;
  64. rsclp->len = 0;
  65. rsclp->len_lazy = 0;
  66. }
  67. /*
  68. * Disable the specified rcu_segcblist structure, so that callbacks can
  69. * no longer be posted to it. This structure must be empty.
  70. */
  71. void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
  72. {
  73. WARN_ON_ONCE(!rcu_segcblist_empty(rsclp));
  74. WARN_ON_ONCE(rcu_segcblist_n_cbs(rsclp));
  75. WARN_ON_ONCE(rcu_segcblist_n_lazy_cbs(rsclp));
  76. rsclp->tails[RCU_NEXT_TAIL] = NULL;
  77. }
  78. /*
  79. * Does the specified rcu_segcblist structure contain callbacks that
  80. * are ready to be invoked?
  81. */
  82. bool rcu_segcblist_ready_cbs(struct rcu_segcblist *rsclp)
  83. {
  84. return rcu_segcblist_is_enabled(rsclp) &&
  85. &rsclp->head != rsclp->tails[RCU_DONE_TAIL];
  86. }
  87. /*
  88. * Does the specified rcu_segcblist structure contain callbacks that
  89. * are still pending, that is, not yet ready to be invoked?
  90. */
  91. bool rcu_segcblist_pend_cbs(struct rcu_segcblist *rsclp)
  92. {
  93. return rcu_segcblist_is_enabled(rsclp) &&
  94. !rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL);
  95. }
  96. /*
  97. * Return a pointer to the first callback in the specified rcu_segcblist
  98. * structure. This is useful for diagnostics.
  99. */
  100. struct rcu_head *rcu_segcblist_first_cb(struct rcu_segcblist *rsclp)
  101. {
  102. if (rcu_segcblist_is_enabled(rsclp))
  103. return rsclp->head;
  104. return NULL;
  105. }
  106. /*
  107. * Return a pointer to the first pending callback in the specified
  108. * rcu_segcblist structure. This is useful just after posting a given
  109. * callback -- if that callback is the first pending callback, then
  110. * you cannot rely on someone else having already started up the required
  111. * grace period.
  112. */
  113. struct rcu_head *rcu_segcblist_first_pend_cb(struct rcu_segcblist *rsclp)
  114. {
  115. if (rcu_segcblist_is_enabled(rsclp))
  116. return *rsclp->tails[RCU_DONE_TAIL];
  117. return NULL;
  118. }
  119. /*
  120. * Enqueue the specified callback onto the specified rcu_segcblist
  121. * structure, updating accounting as needed. Note that the ->len
  122. * field may be accessed locklessly, hence the WRITE_ONCE().
  123. * The ->len field is used by rcu_barrier() and friends to determine
  124. * if it must post a callback on this structure, and it is OK
  125. * for rcu_barrier() to sometimes post callbacks needlessly, but
  126. * absolutely not OK for it to ever miss posting a callback.
  127. */
  128. void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
  129. struct rcu_head *rhp, bool lazy)
  130. {
  131. WRITE_ONCE(rsclp->len, rsclp->len + 1); /* ->len sampled locklessly. */
  132. if (lazy)
  133. rsclp->len_lazy++;
  134. smp_mb(); /* Ensure counts are updated before callback is enqueued. */
  135. rhp->next = NULL;
  136. *rsclp->tails[RCU_NEXT_TAIL] = rhp;
  137. rsclp->tails[RCU_NEXT_TAIL] = &rhp->next;
  138. }
  139. /*
  140. * Entrain the specified callback onto the specified rcu_segcblist at
  141. * the end of the last non-empty segment. If the entire rcu_segcblist
  142. * is empty, make no change, but return false.
  143. *
  144. * This is intended for use by rcu_barrier()-like primitives, -not-
  145. * for normal grace-period use. IMPORTANT: The callback you enqueue
  146. * will wait for all prior callbacks, NOT necessarily for a grace
  147. * period. You have been warned.
  148. */
  149. bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
  150. struct rcu_head *rhp, bool lazy)
  151. {
  152. int i;
  153. if (rcu_segcblist_n_cbs(rsclp) == 0)
  154. return false;
  155. WRITE_ONCE(rsclp->len, rsclp->len + 1);
  156. if (lazy)
  157. rsclp->len_lazy++;
  158. smp_mb(); /* Ensure counts are updated before callback is entrained. */
  159. rhp->next = NULL;
  160. for (i = RCU_NEXT_TAIL; i > RCU_DONE_TAIL; i--)
  161. if (rsclp->tails[i] != rsclp->tails[i - 1])
  162. break;
  163. *rsclp->tails[i] = rhp;
  164. for (; i <= RCU_NEXT_TAIL; i++)
  165. rsclp->tails[i] = &rhp->next;
  166. return true;
  167. }
  168. /*
  169. * Extract only the counts from the specified rcu_segcblist structure,
  170. * and place them in the specified rcu_cblist structure. This function
  171. * supports both callback orphaning and invocation, hence the separation
  172. * of counts and callbacks. (Callbacks ready for invocation must be
  173. * orphaned and adopted separately from pending callbacks, but counts
  174. * apply to all callbacks. Locking must be used to make sure that
  175. * both orphaned-callbacks lists are consistent.)
  176. */
  177. void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
  178. struct rcu_cblist *rclp)
  179. {
  180. rclp->len_lazy += rsclp->len_lazy;
  181. rclp->len += rsclp->len;
  182. rsclp->len_lazy = 0;
  183. WRITE_ONCE(rsclp->len, 0); /* ->len sampled locklessly. */
  184. }
  185. /*
  186. * Extract only those callbacks ready to be invoked from the specified
  187. * rcu_segcblist structure and place them in the specified rcu_cblist
  188. * structure.
  189. */
  190. void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
  191. struct rcu_cblist *rclp)
  192. {
  193. int i;
  194. if (!rcu_segcblist_ready_cbs(rsclp))
  195. return; /* Nothing to do. */
  196. *rclp->tail = rsclp->head;
  197. rsclp->head = *rsclp->tails[RCU_DONE_TAIL];
  198. *rsclp->tails[RCU_DONE_TAIL] = NULL;
  199. rclp->tail = rsclp->tails[RCU_DONE_TAIL];
  200. for (i = RCU_CBLIST_NSEGS - 1; i >= RCU_DONE_TAIL; i--)
  201. if (rsclp->tails[i] == rsclp->tails[RCU_DONE_TAIL])
  202. rsclp->tails[i] = &rsclp->head;
  203. }
  204. /*
  205. * Extract only those callbacks still pending (not yet ready to be
  206. * invoked) from the specified rcu_segcblist structure and place them in
  207. * the specified rcu_cblist structure. Note that this loses information
  208. * about any callbacks that might have been partway done waiting for
  209. * their grace period. Too bad! They will have to start over.
  210. */
  211. void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
  212. struct rcu_cblist *rclp)
  213. {
  214. int i;
  215. if (!rcu_segcblist_pend_cbs(rsclp))
  216. return; /* Nothing to do. */
  217. *rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
  218. rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
  219. *rsclp->tails[RCU_DONE_TAIL] = NULL;
  220. for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++)
  221. rsclp->tails[i] = rsclp->tails[RCU_DONE_TAIL];
  222. }
  223. /*
  224. * Insert counts from the specified rcu_cblist structure in the
  225. * specified rcu_segcblist structure.
  226. */
  227. void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
  228. struct rcu_cblist *rclp)
  229. {
  230. rsclp->len_lazy += rclp->len_lazy;
  231. /* ->len sampled locklessly. */
  232. WRITE_ONCE(rsclp->len, rsclp->len + rclp->len);
  233. rclp->len_lazy = 0;
  234. rclp->len = 0;
  235. }
  236. /*
  237. * Move callbacks from the specified rcu_cblist to the beginning of the
  238. * done-callbacks segment of the specified rcu_segcblist.
  239. */
  240. void rcu_segcblist_insert_done_cbs(struct rcu_segcblist *rsclp,
  241. struct rcu_cblist *rclp)
  242. {
  243. int i;
  244. if (!rclp->head)
  245. return; /* No callbacks to move. */
  246. *rclp->tail = rsclp->head;
  247. rsclp->head = rclp->head;
  248. for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
  249. if (&rsclp->head == rsclp->tails[i])
  250. rsclp->tails[i] = rclp->tail;
  251. else
  252. break;
  253. rclp->head = NULL;
  254. rclp->tail = &rclp->head;
  255. }
  256. /*
  257. * Move callbacks from the specified rcu_cblist to the end of the
  258. * new-callbacks segment of the specified rcu_segcblist.
  259. */
  260. void rcu_segcblist_insert_pend_cbs(struct rcu_segcblist *rsclp,
  261. struct rcu_cblist *rclp)
  262. {
  263. if (!rclp->head)
  264. return; /* Nothing to do. */
  265. *rsclp->tails[RCU_NEXT_TAIL] = rclp->head;
  266. rsclp->tails[RCU_NEXT_TAIL] = rclp->tail;
  267. rclp->head = NULL;
  268. rclp->tail = &rclp->head;
  269. }
  270. /*
  271. * Advance the callbacks in the specified rcu_segcblist structure based
  272. * on the current value passed in for the grace-period counter.
  273. */
  274. void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
  275. {
  276. int i, j;
  277. WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
  278. if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
  279. return;
  280. /*
  281. * Find all callbacks whose ->gp_seq numbers indicate that they
  282. * are ready to invoke, and put them into the RCU_DONE_TAIL segment.
  283. */
  284. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
  285. if (ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
  286. break;
  287. rsclp->tails[RCU_DONE_TAIL] = rsclp->tails[i];
  288. }
  289. /* If no callbacks moved, nothing more need be done. */
  290. if (i == RCU_WAIT_TAIL)
  291. return;
  292. /* Clean up tail pointers that might have been misordered above. */
  293. for (j = RCU_WAIT_TAIL; j < i; j++)
  294. rsclp->tails[j] = rsclp->tails[RCU_DONE_TAIL];
  295. /*
  296. * Callbacks moved, so clean up the misordered ->tails[] pointers
  297. * that now point into the middle of the list of ready-to-invoke
  298. * callbacks. The overall effect is to copy down the later pointers
  299. * into the gap that was created by the now-ready segments.
  300. */
  301. for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
  302. if (rsclp->tails[j] == rsclp->tails[RCU_NEXT_TAIL])
  303. break; /* No more callbacks. */
  304. rsclp->tails[j] = rsclp->tails[i];
  305. rsclp->gp_seq[j] = rsclp->gp_seq[i];
  306. }
  307. }
  308. /*
  309. * "Accelerate" callbacks based on more-accurate grace-period information.
  310. * The reason for this is that RCU does not synchronize the beginnings and
  311. * ends of grace periods, and that callbacks are posted locally. This in
  312. * turn means that the callbacks must be labelled conservatively early
  313. * on, as getting exact information would degrade both performance and
  314. * scalability. When more accurate grace-period information becomes
  315. * available, previously posted callbacks can be "accelerated", marking
  316. * them to complete at the end of the earlier grace period.
  317. *
  318. * This function operates on an rcu_segcblist structure, and also the
  319. * grace-period sequence number seq at which new callbacks would become
  320. * ready to invoke. Returns true if there are callbacks that won't be
  321. * ready to invoke until seq, false otherwise.
  322. */
  323. bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
  324. {
  325. int i;
  326. WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
  327. if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
  328. return false;
  329. /*
  330. * Find the segment preceding the oldest segment of callbacks
  331. * whose ->gp_seq[] completion is at or after that passed in via
  332. * "seq", skipping any empty segments. This oldest segment, along
  333. * with any later segments, can be merged in with any newly arrived
  334. * callbacks in the RCU_NEXT_TAIL segment, and assigned "seq"
  335. * as their ->gp_seq[] grace-period completion sequence number.
  336. */
  337. for (i = RCU_NEXT_READY_TAIL; i > RCU_DONE_TAIL; i--)
  338. if (rsclp->tails[i] != rsclp->tails[i - 1] &&
  339. ULONG_CMP_LT(rsclp->gp_seq[i], seq))
  340. break;
  341. /*
  342. * If all the segments contain callbacks that correspond to
  343. * earlier grace-period sequence numbers than "seq", leave.
  344. * Assuming that the rcu_segcblist structure has enough
  345. * segments in its arrays, this can only happen if some of
  346. * the non-done segments contain callbacks that really are
  347. * ready to invoke. This situation will get straightened
  348. * out by the next call to rcu_segcblist_advance().
  349. *
  350. * Also advance to the oldest segment of callbacks whose
  351. * ->gp_seq[] completion is at or after that passed in via "seq",
  352. * skipping any empty segments.
  353. */
  354. if (++i >= RCU_NEXT_TAIL)
  355. return false;
  356. /*
  357. * Merge all later callbacks, including newly arrived callbacks,
  358. * into the segment located by the for-loop above. Assign "seq"
  359. * as the ->gp_seq[] value in order to correctly handle the case
  360. * where there were no pending callbacks in the rcu_segcblist
  361. * structure other than in the RCU_NEXT_TAIL segment.
  362. */
  363. for (; i < RCU_NEXT_TAIL; i++) {
  364. rsclp->tails[i] = rsclp->tails[RCU_NEXT_TAIL];
  365. rsclp->gp_seq[i] = seq;
  366. }
  367. return true;
  368. }
  369. /*
  370. * Scan the specified rcu_segcblist structure for callbacks that need
  371. * a grace period later than the one specified by "seq". We don't look
  372. * at the RCU_DONE_TAIL or RCU_NEXT_TAIL segments because they don't
  373. * have a grace-period sequence number.
  374. */
  375. bool rcu_segcblist_future_gp_needed(struct rcu_segcblist *rsclp,
  376. unsigned long seq)
  377. {
  378. int i;
  379. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
  380. if (rsclp->tails[i - 1] != rsclp->tails[i] &&
  381. ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
  382. return true;
  383. return false;
  384. }
  385. /*
  386. * Merge the source rcu_segcblist structure into the destination
  387. * rcu_segcblist structure, then initialize the source. Any pending
  388. * callbacks from the source get to start over. It is best to
  389. * advance and accelerate both the destination and the source
  390. * before merging.
  391. */
  392. void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
  393. struct rcu_segcblist *src_rsclp)
  394. {
  395. struct rcu_cblist donecbs;
  396. struct rcu_cblist pendcbs;
  397. rcu_cblist_init(&donecbs);
  398. rcu_cblist_init(&pendcbs);
  399. rcu_segcblist_extract_count(src_rsclp, &donecbs);
  400. rcu_segcblist_extract_done_cbs(src_rsclp, &donecbs);
  401. rcu_segcblist_extract_pend_cbs(src_rsclp, &pendcbs);
  402. rcu_segcblist_insert_count(dst_rsclp, &donecbs);
  403. rcu_segcblist_insert_done_cbs(dst_rsclp, &donecbs);
  404. rcu_segcblist_insert_pend_cbs(dst_rsclp, &pendcbs);
  405. rcu_segcblist_init(src_rsclp);
  406. }