rcu_segcblist.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * RCU segmented callback lists, function definitions
  4. *
  5. * Copyright IBM Corporation, 2017
  6. *
  7. * Authors: Paul E. McKenney <paulmck@linux.ibm.com>
  8. */
  9. #include <linux/cpu.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include "rcu_segcblist.h"
  14. /* Initialize simple callback list. */
  15. void rcu_cblist_init(struct rcu_cblist *rclp)
  16. {
  17. rclp->head = NULL;
  18. rclp->tail = &rclp->head;
  19. rclp->len = 0;
  20. }
  21. /*
  22. * Enqueue an rcu_head structure onto the specified callback list.
  23. */
  24. void rcu_cblist_enqueue(struct rcu_cblist *rclp, struct rcu_head *rhp)
  25. {
  26. *rclp->tail = rhp;
  27. rclp->tail = &rhp->next;
  28. WRITE_ONCE(rclp->len, rclp->len + 1);
  29. }
  30. /*
  31. * Flush the second rcu_cblist structure onto the first one, obliterating
  32. * any contents of the first. If rhp is non-NULL, enqueue it as the sole
  33. * element of the second rcu_cblist structure, but ensuring that the second
  34. * rcu_cblist structure, if initially non-empty, always appears non-empty
  35. * throughout the process. If rdp is NULL, the second rcu_cblist structure
  36. * is instead initialized to empty.
  37. */
  38. void rcu_cblist_flush_enqueue(struct rcu_cblist *drclp,
  39. struct rcu_cblist *srclp,
  40. struct rcu_head *rhp)
  41. {
  42. drclp->head = srclp->head;
  43. if (drclp->head)
  44. drclp->tail = srclp->tail;
  45. else
  46. drclp->tail = &drclp->head;
  47. drclp->len = srclp->len;
  48. if (!rhp) {
  49. rcu_cblist_init(srclp);
  50. } else {
  51. rhp->next = NULL;
  52. srclp->head = rhp;
  53. srclp->tail = &rhp->next;
  54. WRITE_ONCE(srclp->len, 1);
  55. }
  56. }
  57. /*
  58. * Dequeue the oldest rcu_head structure from the specified callback
  59. * list.
  60. */
  61. struct rcu_head *rcu_cblist_dequeue(struct rcu_cblist *rclp)
  62. {
  63. struct rcu_head *rhp;
  64. rhp = rclp->head;
  65. if (!rhp)
  66. return NULL;
  67. rclp->len--;
  68. rclp->head = rhp->next;
  69. if (!rclp->head)
  70. rclp->tail = &rclp->head;
  71. return rhp;
  72. }
  73. /* Set the length of an rcu_segcblist structure. */
  74. static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
  75. {
  76. #ifdef CONFIG_RCU_NOCB_CPU
  77. atomic_long_set(&rsclp->len, v);
  78. #else
  79. WRITE_ONCE(rsclp->len, v);
  80. #endif
  81. }
  82. /* Get the length of a segment of the rcu_segcblist structure. */
  83. long rcu_segcblist_get_seglen(struct rcu_segcblist *rsclp, int seg)
  84. {
  85. return READ_ONCE(rsclp->seglen[seg]);
  86. }
  87. /* Return number of callbacks in segmented callback list by summing seglen. */
  88. long rcu_segcblist_n_segment_cbs(struct rcu_segcblist *rsclp)
  89. {
  90. long len = 0;
  91. int i;
  92. for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
  93. len += rcu_segcblist_get_seglen(rsclp, i);
  94. return len;
  95. }
  96. /* Set the length of a segment of the rcu_segcblist structure. */
  97. static void rcu_segcblist_set_seglen(struct rcu_segcblist *rsclp, int seg, long v)
  98. {
  99. WRITE_ONCE(rsclp->seglen[seg], v);
  100. }
  101. /* Increase the numeric length of a segment by a specified amount. */
  102. static void rcu_segcblist_add_seglen(struct rcu_segcblist *rsclp, int seg, long v)
  103. {
  104. WRITE_ONCE(rsclp->seglen[seg], rsclp->seglen[seg] + v);
  105. }
  106. /* Move from's segment length to to's segment. */
  107. static void rcu_segcblist_move_seglen(struct rcu_segcblist *rsclp, int from, int to)
  108. {
  109. long len;
  110. if (from == to)
  111. return;
  112. len = rcu_segcblist_get_seglen(rsclp, from);
  113. if (!len)
  114. return;
  115. rcu_segcblist_add_seglen(rsclp, to, len);
  116. rcu_segcblist_set_seglen(rsclp, from, 0);
  117. }
  118. /* Increment segment's length. */
  119. static void rcu_segcblist_inc_seglen(struct rcu_segcblist *rsclp, int seg)
  120. {
  121. rcu_segcblist_add_seglen(rsclp, seg, 1);
  122. }
  123. /*
  124. * Increase the numeric length of an rcu_segcblist structure by the
  125. * specified amount, which can be negative. This can cause the ->len
  126. * field to disagree with the actual number of callbacks on the structure.
  127. * This increase is fully ordered with respect to the callers accesses
  128. * both before and after.
  129. *
  130. * So why on earth is a memory barrier required both before and after
  131. * the update to the ->len field???
  132. *
  133. * The reason is that rcu_barrier() locklessly samples each CPU's ->len
  134. * field, and if a given CPU's field is zero, avoids IPIing that CPU.
  135. * This can of course race with both queuing and invoking of callbacks.
  136. * Failing to correctly handle either of these races could result in
  137. * rcu_barrier() failing to IPI a CPU that actually had callbacks queued
  138. * which rcu_barrier() was obligated to wait on. And if rcu_barrier()
  139. * failed to wait on such a callback, unloading certain kernel modules
  140. * would result in calls to functions whose code was no longer present in
  141. * the kernel, for but one example.
  142. *
  143. * Therefore, ->len transitions from 1->0 and 0->1 have to be carefully
  144. * ordered with respect with both list modifications and the rcu_barrier().
  145. *
  146. * The queuing case is CASE 1 and the invoking case is CASE 2.
  147. *
  148. * CASE 1: Suppose that CPU 0 has no callbacks queued, but invokes
  149. * call_rcu() just as CPU 1 invokes rcu_barrier(). CPU 0's ->len field
  150. * will transition from 0->1, which is one of the transitions that must
  151. * be handled carefully. Without the full memory barriers after the ->len
  152. * update and at the beginning of rcu_barrier(), the following could happen:
  153. *
  154. * CPU 0 CPU 1
  155. *
  156. * call_rcu().
  157. * rcu_barrier() sees ->len as 0.
  158. * set ->len = 1.
  159. * rcu_barrier() does nothing.
  160. * module is unloaded.
  161. * callback invokes unloaded function!
  162. *
  163. * With the full barriers, any case where rcu_barrier() sees ->len as 0 will
  164. * have unambiguously preceded the return from the racing call_rcu(), which
  165. * means that this call_rcu() invocation is OK to not wait on. After all,
  166. * you are supposed to make sure that any problematic call_rcu() invocations
  167. * happen before the rcu_barrier().
  168. *
  169. *
  170. * CASE 2: Suppose that CPU 0 is invoking its last callback just as
  171. * CPU 1 invokes rcu_barrier(). CPU 0's ->len field will transition from
  172. * 1->0, which is one of the transitions that must be handled carefully.
  173. * Without the full memory barriers before the ->len update and at the
  174. * end of rcu_barrier(), the following could happen:
  175. *
  176. * CPU 0 CPU 1
  177. *
  178. * start invoking last callback
  179. * set ->len = 0 (reordered)
  180. * rcu_barrier() sees ->len as 0
  181. * rcu_barrier() does nothing.
  182. * module is unloaded
  183. * callback executing after unloaded!
  184. *
  185. * With the full barriers, any case where rcu_barrier() sees ->len as 0
  186. * will be fully ordered after the completion of the callback function,
  187. * so that the module unloading operation is completely safe.
  188. *
  189. */
  190. void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
  191. {
  192. #ifdef CONFIG_RCU_NOCB_CPU
  193. smp_mb__before_atomic(); // Read header comment above.
  194. atomic_long_add(v, &rsclp->len);
  195. smp_mb__after_atomic(); // Read header comment above.
  196. #else
  197. smp_mb(); // Read header comment above.
  198. WRITE_ONCE(rsclp->len, rsclp->len + v);
  199. smp_mb(); // Read header comment above.
  200. #endif
  201. }
  202. /*
  203. * Increase the numeric length of an rcu_segcblist structure by one.
  204. * This can cause the ->len field to disagree with the actual number of
  205. * callbacks on the structure. This increase is fully ordered with respect
  206. * to the callers accesses both before and after.
  207. */
  208. void rcu_segcblist_inc_len(struct rcu_segcblist *rsclp)
  209. {
  210. rcu_segcblist_add_len(rsclp, 1);
  211. }
  212. /*
  213. * Initialize an rcu_segcblist structure.
  214. */
  215. void rcu_segcblist_init(struct rcu_segcblist *rsclp)
  216. {
  217. int i;
  218. BUILD_BUG_ON(RCU_NEXT_TAIL + 1 != ARRAY_SIZE(rsclp->gp_seq));
  219. BUILD_BUG_ON(ARRAY_SIZE(rsclp->tails) != ARRAY_SIZE(rsclp->gp_seq));
  220. rsclp->head = NULL;
  221. for (i = 0; i < RCU_CBLIST_NSEGS; i++) {
  222. rsclp->tails[i] = &rsclp->head;
  223. rcu_segcblist_set_seglen(rsclp, i, 0);
  224. }
  225. rcu_segcblist_set_len(rsclp, 0);
  226. rcu_segcblist_set_flags(rsclp, SEGCBLIST_ENABLED);
  227. }
  228. /*
  229. * Disable the specified rcu_segcblist structure, so that callbacks can
  230. * no longer be posted to it. This structure must be empty.
  231. */
  232. void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
  233. {
  234. WARN_ON_ONCE(!rcu_segcblist_empty(rsclp));
  235. WARN_ON_ONCE(rcu_segcblist_n_cbs(rsclp));
  236. rcu_segcblist_clear_flags(rsclp, SEGCBLIST_ENABLED);
  237. }
  238. /*
  239. * Does the specified rcu_segcblist structure contain callbacks that
  240. * are ready to be invoked?
  241. */
  242. bool rcu_segcblist_ready_cbs(struct rcu_segcblist *rsclp)
  243. {
  244. return rcu_segcblist_is_enabled(rsclp) &&
  245. &rsclp->head != READ_ONCE(rsclp->tails[RCU_DONE_TAIL]);
  246. }
  247. /*
  248. * Does the specified rcu_segcblist structure contain callbacks that
  249. * are still pending, that is, not yet ready to be invoked?
  250. */
  251. bool rcu_segcblist_pend_cbs(struct rcu_segcblist *rsclp)
  252. {
  253. return rcu_segcblist_is_enabled(rsclp) &&
  254. !rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL);
  255. }
  256. /*
  257. * Return a pointer to the first callback in the specified rcu_segcblist
  258. * structure. This is useful for diagnostics.
  259. */
  260. struct rcu_head *rcu_segcblist_first_cb(struct rcu_segcblist *rsclp)
  261. {
  262. if (rcu_segcblist_is_enabled(rsclp))
  263. return rsclp->head;
  264. return NULL;
  265. }
  266. /*
  267. * Return a pointer to the first pending callback in the specified
  268. * rcu_segcblist structure. This is useful just after posting a given
  269. * callback -- if that callback is the first pending callback, then
  270. * you cannot rely on someone else having already started up the required
  271. * grace period.
  272. */
  273. struct rcu_head *rcu_segcblist_first_pend_cb(struct rcu_segcblist *rsclp)
  274. {
  275. if (rcu_segcblist_is_enabled(rsclp))
  276. return *rsclp->tails[RCU_DONE_TAIL];
  277. return NULL;
  278. }
  279. /*
  280. * Return false if there are no CBs awaiting grace periods, otherwise,
  281. * return true and store the nearest waited-upon grace period into *lp.
  282. */
  283. bool rcu_segcblist_nextgp(struct rcu_segcblist *rsclp, unsigned long *lp)
  284. {
  285. if (!rcu_segcblist_pend_cbs(rsclp))
  286. return false;
  287. *lp = rsclp->gp_seq[RCU_WAIT_TAIL];
  288. return true;
  289. }
  290. /*
  291. * Enqueue the specified callback onto the specified rcu_segcblist
  292. * structure, updating accounting as needed. Note that the ->len
  293. * field may be accessed locklessly, hence the WRITE_ONCE().
  294. * The ->len field is used by rcu_barrier() and friends to determine
  295. * if it must post a callback on this structure, and it is OK
  296. * for rcu_barrier() to sometimes post callbacks needlessly, but
  297. * absolutely not OK for it to ever miss posting a callback.
  298. */
  299. void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
  300. struct rcu_head *rhp)
  301. {
  302. rcu_segcblist_inc_len(rsclp);
  303. rcu_segcblist_inc_seglen(rsclp, RCU_NEXT_TAIL);
  304. rhp->next = NULL;
  305. WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
  306. WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], &rhp->next);
  307. }
  308. /*
  309. * Entrain the specified callback onto the specified rcu_segcblist at
  310. * the end of the last non-empty segment. If the entire rcu_segcblist
  311. * is empty, make no change, but return false.
  312. *
  313. * This is intended for use by rcu_barrier()-like primitives, -not-
  314. * for normal grace-period use. IMPORTANT: The callback you enqueue
  315. * will wait for all prior callbacks, NOT necessarily for a grace
  316. * period. You have been warned.
  317. */
  318. bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
  319. struct rcu_head *rhp)
  320. {
  321. int i;
  322. if (rcu_segcblist_n_cbs(rsclp) == 0)
  323. return false;
  324. rcu_segcblist_inc_len(rsclp);
  325. smp_mb(); /* Ensure counts are updated before callback is entrained. */
  326. rhp->next = NULL;
  327. for (i = RCU_NEXT_TAIL; i > RCU_DONE_TAIL; i--)
  328. if (!rcu_segcblist_segempty(rsclp, i))
  329. break;
  330. rcu_segcblist_inc_seglen(rsclp, i);
  331. WRITE_ONCE(*rsclp->tails[i], rhp);
  332. for (; i <= RCU_NEXT_TAIL; i++)
  333. WRITE_ONCE(rsclp->tails[i], &rhp->next);
  334. return true;
  335. }
  336. /*
  337. * Extract only those callbacks ready to be invoked from the specified
  338. * rcu_segcblist structure and place them in the specified rcu_cblist
  339. * structure.
  340. */
  341. void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
  342. struct rcu_cblist *rclp)
  343. {
  344. int i;
  345. if (!rcu_segcblist_ready_cbs(rsclp))
  346. return; /* Nothing to do. */
  347. rclp->len = rcu_segcblist_get_seglen(rsclp, RCU_DONE_TAIL);
  348. *rclp->tail = rsclp->head;
  349. WRITE_ONCE(rsclp->head, *rsclp->tails[RCU_DONE_TAIL]);
  350. WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
  351. rclp->tail = rsclp->tails[RCU_DONE_TAIL];
  352. for (i = RCU_CBLIST_NSEGS - 1; i >= RCU_DONE_TAIL; i--)
  353. if (rsclp->tails[i] == rsclp->tails[RCU_DONE_TAIL])
  354. WRITE_ONCE(rsclp->tails[i], &rsclp->head);
  355. rcu_segcblist_set_seglen(rsclp, RCU_DONE_TAIL, 0);
  356. }
  357. /*
  358. * Extract only those callbacks still pending (not yet ready to be
  359. * invoked) from the specified rcu_segcblist structure and place them in
  360. * the specified rcu_cblist structure. Note that this loses information
  361. * about any callbacks that might have been partway done waiting for
  362. * their grace period. Too bad! They will have to start over.
  363. */
  364. void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
  365. struct rcu_cblist *rclp)
  366. {
  367. int i;
  368. if (!rcu_segcblist_pend_cbs(rsclp))
  369. return; /* Nothing to do. */
  370. rclp->len = 0;
  371. *rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
  372. rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
  373. WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
  374. for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++) {
  375. rclp->len += rcu_segcblist_get_seglen(rsclp, i);
  376. WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
  377. rcu_segcblist_set_seglen(rsclp, i, 0);
  378. }
  379. }
  380. /*
  381. * Insert counts from the specified rcu_cblist structure in the
  382. * specified rcu_segcblist structure.
  383. */
  384. void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
  385. struct rcu_cblist *rclp)
  386. {
  387. rcu_segcblist_add_len(rsclp, rclp->len);
  388. }
  389. /*
  390. * Move callbacks from the specified rcu_cblist to the beginning of the
  391. * done-callbacks segment of the specified rcu_segcblist.
  392. */
  393. void rcu_segcblist_insert_done_cbs(struct rcu_segcblist *rsclp,
  394. struct rcu_cblist *rclp)
  395. {
  396. int i;
  397. if (!rclp->head)
  398. return; /* No callbacks to move. */
  399. rcu_segcblist_add_seglen(rsclp, RCU_DONE_TAIL, rclp->len);
  400. *rclp->tail = rsclp->head;
  401. WRITE_ONCE(rsclp->head, rclp->head);
  402. for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
  403. if (&rsclp->head == rsclp->tails[i])
  404. WRITE_ONCE(rsclp->tails[i], rclp->tail);
  405. else
  406. break;
  407. rclp->head = NULL;
  408. rclp->tail = &rclp->head;
  409. }
  410. /*
  411. * Move callbacks from the specified rcu_cblist to the end of the
  412. * new-callbacks segment of the specified rcu_segcblist.
  413. */
  414. void rcu_segcblist_insert_pend_cbs(struct rcu_segcblist *rsclp,
  415. struct rcu_cblist *rclp)
  416. {
  417. if (!rclp->head)
  418. return; /* Nothing to do. */
  419. rcu_segcblist_add_seglen(rsclp, RCU_NEXT_TAIL, rclp->len);
  420. WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rclp->head);
  421. WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], rclp->tail);
  422. }
  423. /*
  424. * Advance the callbacks in the specified rcu_segcblist structure based
  425. * on the current value passed in for the grace-period counter.
  426. */
  427. void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
  428. {
  429. int i, j;
  430. WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
  431. if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
  432. return;
  433. /*
  434. * Find all callbacks whose ->gp_seq numbers indicate that they
  435. * are ready to invoke, and put them into the RCU_DONE_TAIL segment.
  436. */
  437. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
  438. if (ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
  439. break;
  440. WRITE_ONCE(rsclp->tails[RCU_DONE_TAIL], rsclp->tails[i]);
  441. rcu_segcblist_move_seglen(rsclp, i, RCU_DONE_TAIL);
  442. }
  443. /* If no callbacks moved, nothing more need be done. */
  444. if (i == RCU_WAIT_TAIL)
  445. return;
  446. /* Clean up tail pointers that might have been misordered above. */
  447. for (j = RCU_WAIT_TAIL; j < i; j++)
  448. WRITE_ONCE(rsclp->tails[j], rsclp->tails[RCU_DONE_TAIL]);
  449. /*
  450. * Callbacks moved, so there might be an empty RCU_WAIT_TAIL
  451. * and a non-empty RCU_NEXT_READY_TAIL. If so, copy the
  452. * RCU_NEXT_READY_TAIL segment to fill the RCU_WAIT_TAIL gap
  453. * created by the now-ready-to-invoke segments.
  454. */
  455. for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
  456. if (rsclp->tails[j] == rsclp->tails[RCU_NEXT_TAIL])
  457. break; /* No more callbacks. */
  458. WRITE_ONCE(rsclp->tails[j], rsclp->tails[i]);
  459. rcu_segcblist_move_seglen(rsclp, i, j);
  460. rsclp->gp_seq[j] = rsclp->gp_seq[i];
  461. }
  462. }
  463. /*
  464. * "Accelerate" callbacks based on more-accurate grace-period information.
  465. * The reason for this is that RCU does not synchronize the beginnings and
  466. * ends of grace periods, and that callbacks are posted locally. This in
  467. * turn means that the callbacks must be labelled conservatively early
  468. * on, as getting exact information would degrade both performance and
  469. * scalability. When more accurate grace-period information becomes
  470. * available, previously posted callbacks can be "accelerated", marking
  471. * them to complete at the end of the earlier grace period.
  472. *
  473. * This function operates on an rcu_segcblist structure, and also the
  474. * grace-period sequence number seq at which new callbacks would become
  475. * ready to invoke. Returns true if there are callbacks that won't be
  476. * ready to invoke until seq, false otherwise.
  477. */
  478. bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
  479. {
  480. int i, j;
  481. WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
  482. if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
  483. return false;
  484. /*
  485. * Find the segment preceding the oldest segment of callbacks
  486. * whose ->gp_seq[] completion is at or after that passed in via
  487. * "seq", skipping any empty segments. This oldest segment, along
  488. * with any later segments, can be merged in with any newly arrived
  489. * callbacks in the RCU_NEXT_TAIL segment, and assigned "seq"
  490. * as their ->gp_seq[] grace-period completion sequence number.
  491. */
  492. for (i = RCU_NEXT_READY_TAIL; i > RCU_DONE_TAIL; i--)
  493. if (!rcu_segcblist_segempty(rsclp, i) &&
  494. ULONG_CMP_LT(rsclp->gp_seq[i], seq))
  495. break;
  496. /*
  497. * If all the segments contain callbacks that correspond to
  498. * earlier grace-period sequence numbers than "seq", leave.
  499. * Assuming that the rcu_segcblist structure has enough
  500. * segments in its arrays, this can only happen if some of
  501. * the non-done segments contain callbacks that really are
  502. * ready to invoke. This situation will get straightened
  503. * out by the next call to rcu_segcblist_advance().
  504. *
  505. * Also advance to the oldest segment of callbacks whose
  506. * ->gp_seq[] completion is at or after that passed in via "seq",
  507. * skipping any empty segments.
  508. *
  509. * Note that segment "i" (and any lower-numbered segments
  510. * containing older callbacks) will be unaffected, and their
  511. * grace-period numbers remain unchanged. For example, if i ==
  512. * WAIT_TAIL, then neither WAIT_TAIL nor DONE_TAIL will be touched.
  513. * Instead, the CBs in NEXT_TAIL will be merged with those in
  514. * NEXT_READY_TAIL and the grace-period number of NEXT_READY_TAIL
  515. * would be updated. NEXT_TAIL would then be empty.
  516. */
  517. if (rcu_segcblist_restempty(rsclp, i) || ++i >= RCU_NEXT_TAIL)
  518. return false;
  519. /* Accounting: everything below i is about to get merged into i. */
  520. for (j = i + 1; j <= RCU_NEXT_TAIL; j++)
  521. rcu_segcblist_move_seglen(rsclp, j, i);
  522. /*
  523. * Merge all later callbacks, including newly arrived callbacks,
  524. * into the segment located by the for-loop above. Assign "seq"
  525. * as the ->gp_seq[] value in order to correctly handle the case
  526. * where there were no pending callbacks in the rcu_segcblist
  527. * structure other than in the RCU_NEXT_TAIL segment.
  528. */
  529. for (; i < RCU_NEXT_TAIL; i++) {
  530. WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_NEXT_TAIL]);
  531. rsclp->gp_seq[i] = seq;
  532. }
  533. return true;
  534. }
  535. /*
  536. * Merge the source rcu_segcblist structure into the destination
  537. * rcu_segcblist structure, then initialize the source. Any pending
  538. * callbacks from the source get to start over. It is best to
  539. * advance and accelerate both the destination and the source
  540. * before merging.
  541. */
  542. void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
  543. struct rcu_segcblist *src_rsclp)
  544. {
  545. struct rcu_cblist donecbs;
  546. struct rcu_cblist pendcbs;
  547. lockdep_assert_cpus_held();
  548. rcu_cblist_init(&donecbs);
  549. rcu_cblist_init(&pendcbs);
  550. rcu_segcblist_extract_done_cbs(src_rsclp, &donecbs);
  551. rcu_segcblist_extract_pend_cbs(src_rsclp, &pendcbs);
  552. /*
  553. * No need smp_mb() before setting length to 0, because CPU hotplug
  554. * lock excludes rcu_barrier.
  555. */
  556. rcu_segcblist_set_len(src_rsclp, 0);
  557. rcu_segcblist_insert_count(dst_rsclp, &donecbs);
  558. rcu_segcblist_insert_count(dst_rsclp, &pendcbs);
  559. rcu_segcblist_insert_done_cbs(dst_rsclp, &donecbs);
  560. rcu_segcblist_insert_pend_cbs(dst_rsclp, &pendcbs);
  561. rcu_segcblist_init(src_rsclp);
  562. }