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