rcu_segcblist.c 14 KB

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