2 * RCU segmented callback lists, function definitions
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.
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.
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.
18 * Copyright IBM Corporation, 2017
20 * Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/interrupt.h>
27 #include "rcu_segcblist.h"
29 /* Initialize simple callback list. */
30 void rcu_cblist_init(struct rcu_cblist *rclp)
33 rclp->tail = &rclp->head;
39 * Dequeue the oldest rcu_head structure from the specified callback
40 * list. This function assumes that the callback is non-lazy, but
41 * the caller can later invoke rcu_cblist_dequeued_lazy() if it
42 * finds otherwise (and if it cares about laziness). This allows
43 * different users to have different ways of determining laziness.
45 struct rcu_head *rcu_cblist_dequeue(struct rcu_cblist *rclp)
53 rclp->head = rhp->next;
55 rclp->tail = &rclp->head;
60 * Initialize an rcu_segcblist structure.
62 void rcu_segcblist_init(struct rcu_segcblist *rsclp)
66 BUILD_BUG_ON(RCU_NEXT_TAIL + 1 != ARRAY_SIZE(rsclp->gp_seq));
67 BUILD_BUG_ON(ARRAY_SIZE(rsclp->tails) != ARRAY_SIZE(rsclp->gp_seq));
69 for (i = 0; i < RCU_CBLIST_NSEGS; i++)
70 rsclp->tails[i] = &rsclp->head;
76 * Disable the specified rcu_segcblist structure, so that callbacks can
77 * no longer be posted to it. This structure must be empty.
79 void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
81 WARN_ON_ONCE(!rcu_segcblist_empty(rsclp));
82 WARN_ON_ONCE(rcu_segcblist_n_cbs(rsclp));
83 WARN_ON_ONCE(rcu_segcblist_n_lazy_cbs(rsclp));
84 rsclp->tails[RCU_NEXT_TAIL] = NULL;
88 * Does the specified rcu_segcblist structure contain callbacks that
89 * are ready to be invoked?
91 bool rcu_segcblist_ready_cbs(struct rcu_segcblist *rsclp)
93 return rcu_segcblist_is_enabled(rsclp) &&
94 &rsclp->head != rsclp->tails[RCU_DONE_TAIL];
98 * Does the specified rcu_segcblist structure contain callbacks that
99 * are still pending, that is, not yet ready to be invoked?
101 bool rcu_segcblist_pend_cbs(struct rcu_segcblist *rsclp)
103 return rcu_segcblist_is_enabled(rsclp) &&
104 !rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL);
108 * Return a pointer to the first callback in the specified rcu_segcblist
109 * structure. This is useful for diagnostics.
111 struct rcu_head *rcu_segcblist_first_cb(struct rcu_segcblist *rsclp)
113 if (rcu_segcblist_is_enabled(rsclp))
119 * Return a pointer to the first pending callback in the specified
120 * rcu_segcblist structure. This is useful just after posting a given
121 * callback -- if that callback is the first pending callback, then
122 * you cannot rely on someone else having already started up the required
125 struct rcu_head *rcu_segcblist_first_pend_cb(struct rcu_segcblist *rsclp)
127 if (rcu_segcblist_is_enabled(rsclp))
128 return *rsclp->tails[RCU_DONE_TAIL];
133 * Enqueue the specified callback onto the specified rcu_segcblist
134 * structure, updating accounting as needed. Note that the ->len
135 * field may be accessed locklessly, hence the WRITE_ONCE().
136 * The ->len field is used by rcu_barrier() and friends to determine
137 * if it must post a callback on this structure, and it is OK
138 * for rcu_barrier() to sometimes post callbacks needlessly, but
139 * absolutely not OK for it to ever miss posting a callback.
141 void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
142 struct rcu_head *rhp, bool lazy)
144 WRITE_ONCE(rsclp->len, rsclp->len + 1); /* ->len sampled locklessly. */
147 smp_mb(); /* Ensure counts are updated before callback is enqueued. */
149 *rsclp->tails[RCU_NEXT_TAIL] = rhp;
150 rsclp->tails[RCU_NEXT_TAIL] = &rhp->next;
154 * Entrain the specified callback onto the specified rcu_segcblist at
155 * the end of the last non-empty segment. If the entire rcu_segcblist
156 * is empty, make no change, but return false.
158 * This is intended for use by rcu_barrier()-like primitives, -not-
159 * for normal grace-period use. IMPORTANT: The callback you enqueue
160 * will wait for all prior callbacks, NOT necessarily for a grace
161 * period. You have been warned.
163 bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
164 struct rcu_head *rhp, bool lazy)
168 if (rcu_segcblist_n_cbs(rsclp) == 0)
170 WRITE_ONCE(rsclp->len, rsclp->len + 1);
173 smp_mb(); /* Ensure counts are updated before callback is entrained. */
175 for (i = RCU_NEXT_TAIL; i > RCU_DONE_TAIL; i--)
176 if (rsclp->tails[i] != rsclp->tails[i - 1])
178 *rsclp->tails[i] = rhp;
179 for (; i <= RCU_NEXT_TAIL; i++)
180 rsclp->tails[i] = &rhp->next;
185 * Extract only the counts from the specified rcu_segcblist structure,
186 * and place them in the specified rcu_cblist structure. This function
187 * supports both callback orphaning and invocation, hence the separation
188 * of counts and callbacks. (Callbacks ready for invocation must be
189 * orphaned and adopted separately from pending callbacks, but counts
190 * apply to all callbacks. Locking must be used to make sure that
191 * both orphaned-callbacks lists are consistent.)
193 void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
194 struct rcu_cblist *rclp)
196 rclp->len_lazy += rsclp->len_lazy;
197 rclp->len += rsclp->len;
199 WRITE_ONCE(rsclp->len, 0); /* ->len sampled locklessly. */
203 * Extract only those callbacks ready to be invoked from the specified
204 * rcu_segcblist structure and place them in the specified rcu_cblist
207 void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
208 struct rcu_cblist *rclp)
212 if (!rcu_segcblist_ready_cbs(rsclp))
213 return; /* Nothing to do. */
214 *rclp->tail = rsclp->head;
215 rsclp->head = *rsclp->tails[RCU_DONE_TAIL];
216 *rsclp->tails[RCU_DONE_TAIL] = NULL;
217 rclp->tail = rsclp->tails[RCU_DONE_TAIL];
218 for (i = RCU_CBLIST_NSEGS - 1; i >= RCU_DONE_TAIL; i--)
219 if (rsclp->tails[i] == rsclp->tails[RCU_DONE_TAIL])
220 rsclp->tails[i] = &rsclp->head;
224 * Extract only those callbacks still pending (not yet ready to be
225 * invoked) from the specified rcu_segcblist structure and place them in
226 * the specified rcu_cblist structure. Note that this loses information
227 * about any callbacks that might have been partway done waiting for
228 * their grace period. Too bad! They will have to start over.
230 void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
231 struct rcu_cblist *rclp)
235 if (!rcu_segcblist_pend_cbs(rsclp))
236 return; /* Nothing to do. */
237 *rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
238 rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
239 *rsclp->tails[RCU_DONE_TAIL] = NULL;
240 for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++)
241 rsclp->tails[i] = rsclp->tails[RCU_DONE_TAIL];
245 * Insert counts from the specified rcu_cblist structure in the
246 * specified rcu_segcblist structure.
248 void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
249 struct rcu_cblist *rclp)
251 rsclp->len_lazy += rclp->len_lazy;
252 /* ->len sampled locklessly. */
253 WRITE_ONCE(rsclp->len, rsclp->len + rclp->len);
259 * Move callbacks from the specified rcu_cblist to the beginning of the
260 * done-callbacks segment of the specified rcu_segcblist.
262 void rcu_segcblist_insert_done_cbs(struct rcu_segcblist *rsclp,
263 struct rcu_cblist *rclp)
268 return; /* No callbacks to move. */
269 *rclp->tail = rsclp->head;
270 rsclp->head = rclp->head;
271 for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
272 if (&rsclp->head == rsclp->tails[i])
273 rsclp->tails[i] = rclp->tail;
277 rclp->tail = &rclp->head;
281 * Move callbacks from the specified rcu_cblist to the end of the
282 * new-callbacks segment of the specified rcu_segcblist.
284 void rcu_segcblist_insert_pend_cbs(struct rcu_segcblist *rsclp,
285 struct rcu_cblist *rclp)
288 return; /* Nothing to do. */
289 *rsclp->tails[RCU_NEXT_TAIL] = rclp->head;
290 rsclp->tails[RCU_NEXT_TAIL] = rclp->tail;
292 rclp->tail = &rclp->head;
296 * Advance the callbacks in the specified rcu_segcblist structure based
297 * on the current value passed in for the grace-period counter.
299 void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
303 WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
304 if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
308 * Find all callbacks whose ->gp_seq numbers indicate that they
309 * are ready to invoke, and put them into the RCU_DONE_TAIL segment.
311 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
312 if (ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
314 rsclp->tails[RCU_DONE_TAIL] = rsclp->tails[i];
317 /* If no callbacks moved, nothing more need be done. */
318 if (i == RCU_WAIT_TAIL)
321 /* Clean up tail pointers that might have been misordered above. */
322 for (j = RCU_WAIT_TAIL; j < i; j++)
323 rsclp->tails[j] = rsclp->tails[RCU_DONE_TAIL];
326 * Callbacks moved, so clean up the misordered ->tails[] pointers
327 * that now point into the middle of the list of ready-to-invoke
328 * callbacks. The overall effect is to copy down the later pointers
329 * into the gap that was created by the now-ready segments.
331 for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
332 if (rsclp->tails[j] == rsclp->tails[RCU_NEXT_TAIL])
333 break; /* No more callbacks. */
334 rsclp->tails[j] = rsclp->tails[i];
335 rsclp->gp_seq[j] = rsclp->gp_seq[i];
340 * "Accelerate" callbacks based on more-accurate grace-period information.
341 * The reason for this is that RCU does not synchronize the beginnings and
342 * ends of grace periods, and that callbacks are posted locally. This in
343 * turn means that the callbacks must be labelled conservatively early
344 * on, as getting exact information would degrade both performance and
345 * scalability. When more accurate grace-period information becomes
346 * available, previously posted callbacks can be "accelerated", marking
347 * them to complete at the end of the earlier grace period.
349 * This function operates on an rcu_segcblist structure, and also the
350 * grace-period sequence number seq at which new callbacks would become
351 * ready to invoke. Returns true if there are callbacks that won't be
352 * ready to invoke until seq, false otherwise.
354 bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
358 WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
359 if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
363 * Find the segment preceding the oldest segment of callbacks
364 * whose ->gp_seq[] completion is at or after that passed in via
365 * "seq", skipping any empty segments. This oldest segment, along
366 * with any later segments, can be merged in with any newly arrived
367 * callbacks in the RCU_NEXT_TAIL segment, and assigned "seq"
368 * as their ->gp_seq[] grace-period completion sequence number.
370 for (i = RCU_NEXT_READY_TAIL; i > RCU_DONE_TAIL; i--)
371 if (rsclp->tails[i] != rsclp->tails[i - 1] &&
372 ULONG_CMP_LT(rsclp->gp_seq[i], seq))
376 * If all the segments contain callbacks that correspond to
377 * earlier grace-period sequence numbers than "seq", leave.
378 * Assuming that the rcu_segcblist structure has enough
379 * segments in its arrays, this can only happen if some of
380 * the non-done segments contain callbacks that really are
381 * ready to invoke. This situation will get straightened
382 * out by the next call to rcu_segcblist_advance().
384 * Also advance to the oldest segment of callbacks whose
385 * ->gp_seq[] completion is at or after that passed in via "seq",
386 * skipping any empty segments.
388 if (++i >= RCU_NEXT_TAIL)
392 * Merge all later callbacks, including newly arrived callbacks,
393 * into the segment located by the for-loop above. Assign "seq"
394 * as the ->gp_seq[] value in order to correctly handle the case
395 * where there were no pending callbacks in the rcu_segcblist
396 * structure other than in the RCU_NEXT_TAIL segment.
398 for (; i < RCU_NEXT_TAIL; i++) {
399 rsclp->tails[i] = rsclp->tails[RCU_NEXT_TAIL];
400 rsclp->gp_seq[i] = seq;
406 * Scan the specified rcu_segcblist structure for callbacks that need
407 * a grace period later than the one specified by "seq". We don't look
408 * at the RCU_DONE_TAIL or RCU_NEXT_TAIL segments because they don't
409 * have a grace-period sequence number.
411 bool rcu_segcblist_future_gp_needed(struct rcu_segcblist *rsclp,
416 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
417 if (rsclp->tails[i - 1] != rsclp->tails[i] &&
418 ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
424 * Merge the source rcu_segcblist structure into the destination
425 * rcu_segcblist structure, then initialize the source. Any pending
426 * callbacks from the source get to start over. It is best to
427 * advance and accelerate both the destination and the source
430 void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
431 struct rcu_segcblist *src_rsclp)
433 struct rcu_cblist donecbs;
434 struct rcu_cblist pendcbs;
436 rcu_cblist_init(&donecbs);
437 rcu_cblist_init(&pendcbs);
438 rcu_segcblist_extract_count(src_rsclp, &donecbs);
439 rcu_segcblist_extract_done_cbs(src_rsclp, &donecbs);
440 rcu_segcblist_extract_pend_cbs(src_rsclp, &pendcbs);
441 rcu_segcblist_insert_count(dst_rsclp, &donecbs);
442 rcu_segcblist_insert_done_cbs(dst_rsclp, &donecbs);
443 rcu_segcblist_insert_pend_cbs(dst_rsclp, &pendcbs);
444 rcu_segcblist_init(src_rsclp);