blob: 3cff8003f707f2f884d891bf1edf5dea6c88e935 [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;
Paul E. McKenney98059b92017-05-02 06:30:12 -070023}
24
25/*
Paul E. McKenneyeda669a2019-07-01 17:36:53 -070026 * Enqueue an rcu_head structure onto the specified callback list.
Paul E. McKenneyeda669a2019-07-01 17:36:53 -070027 */
28void rcu_cblist_enqueue(struct rcu_cblist *rclp, struct rcu_head *rhp)
29{
30 *rclp->tail = rhp;
31 rclp->tail = &rhp->next;
32 WRITE_ONCE(rclp->len, rclp->len + 1);
33}
34
35/*
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -070036 * Flush the second rcu_cblist structure onto the first one, obliterating
37 * any contents of the first. If rhp is non-NULL, enqueue it as the sole
38 * element of the second rcu_cblist structure, but ensuring that the second
39 * rcu_cblist structure, if initially non-empty, always appears non-empty
40 * throughout the process. If rdp is NULL, the second rcu_cblist structure
41 * is instead initialized to empty.
42 */
43void rcu_cblist_flush_enqueue(struct rcu_cblist *drclp,
44 struct rcu_cblist *srclp,
45 struct rcu_head *rhp)
46{
47 drclp->head = srclp->head;
48 if (drclp->head)
49 drclp->tail = srclp->tail;
50 else
51 drclp->tail = &drclp->head;
52 drclp->len = srclp->len;
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -070053 if (!rhp) {
54 rcu_cblist_init(srclp);
55 } else {
56 rhp->next = NULL;
57 srclp->head = rhp;
58 srclp->tail = &rhp->next;
59 WRITE_ONCE(srclp->len, 1);
Paul E. McKenneyd1b222c2019-07-02 16:03:33 -070060 }
61}
62
63/*
Paul E. McKenney98059b92017-05-02 06:30:12 -070064 * Dequeue the oldest rcu_head structure from the specified callback
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -040065 * list.
Paul E. McKenney98059b92017-05-02 06:30:12 -070066 */
67struct rcu_head *rcu_cblist_dequeue(struct rcu_cblist *rclp)
68{
69 struct rcu_head *rhp;
70
71 rhp = rclp->head;
72 if (!rhp)
73 return NULL;
74 rclp->len--;
75 rclp->head = rhp->next;
76 if (!rclp->head)
77 rclp->tail = &rclp->head;
78 return rhp;
79}
80
Paul E. McKenneyeda669a2019-07-01 17:36:53 -070081/* Set the length of an rcu_segcblist structure. */
kbuild test robot1d24dd42019-08-08 10:32:58 +080082static void rcu_segcblist_set_len(struct rcu_segcblist *rsclp, long v)
Paul E. McKenneyeda669a2019-07-01 17:36:53 -070083{
84#ifdef CONFIG_RCU_NOCB_CPU
85 atomic_long_set(&rsclp->len, v);
86#else
87 WRITE_ONCE(rsclp->len, v);
88#endif
89}
90
91/*
92 * Increase the numeric length of an rcu_segcblist structure by the
93 * specified amount, which can be negative. This can cause the ->len
94 * field to disagree with the actual number of callbacks on the structure.
95 * This increase is fully ordered with respect to the callers accesses
96 * both before and after.
Joel Fernandes (Google)c2e13112020-11-03 09:26:03 -050097 *
98 * So why on earth is a memory barrier required both before and after
99 * the update to the ->len field???
100 *
101 * The reason is that rcu_barrier() locklessly samples each CPU's ->len
102 * field, and if a given CPU's field is zero, avoids IPIing that CPU.
103 * This can of course race with both queuing and invoking of callbacks.
104 * Failing to correctly handle either of these races could result in
105 * rcu_barrier() failing to IPI a CPU that actually had callbacks queued
106 * which rcu_barrier() was obligated to wait on. And if rcu_barrier()
107 * failed to wait on such a callback, unloading certain kernel modules
108 * would result in calls to functions whose code was no longer present in
109 * the kernel, for but one example.
110 *
111 * Therefore, ->len transitions from 1->0 and 0->1 have to be carefully
112 * ordered with respect with both list modifications and the rcu_barrier().
113 *
114 * The queuing case is CASE 1 and the invoking case is CASE 2.
115 *
116 * CASE 1: Suppose that CPU 0 has no callbacks queued, but invokes
117 * call_rcu() just as CPU 1 invokes rcu_barrier(). CPU 0's ->len field
118 * will transition from 0->1, which is one of the transitions that must
119 * be handled carefully. Without the full memory barriers after the ->len
120 * update and at the beginning of rcu_barrier(), the following could happen:
121 *
122 * CPU 0 CPU 1
123 *
124 * call_rcu().
125 * rcu_barrier() sees ->len as 0.
126 * set ->len = 1.
127 * rcu_barrier() does nothing.
128 * module is unloaded.
129 * callback invokes unloaded function!
130 *
131 * With the full barriers, any case where rcu_barrier() sees ->len as 0 will
132 * have unambiguously preceded the return from the racing call_rcu(), which
133 * means that this call_rcu() invocation is OK to not wait on. After all,
134 * you are supposed to make sure that any problematic call_rcu() invocations
135 * happen before the rcu_barrier().
136 *
137 *
138 * CASE 2: Suppose that CPU 0 is invoking its last callback just as
139 * CPU 1 invokes rcu_barrier(). CPU 0's ->len field will transition from
140 * 1->0, which is one of the transitions that must be handled carefully.
141 * Without the full memory barriers before the ->len update and at the
142 * end of rcu_barrier(), the following could happen:
143 *
144 * CPU 0 CPU 1
145 *
146 * start invoking last callback
147 * set ->len = 0 (reordered)
148 * rcu_barrier() sees ->len as 0
149 * rcu_barrier() does nothing.
150 * module is unloaded
151 * callback executing after unloaded!
152 *
153 * With the full barriers, any case where rcu_barrier() sees ->len as 0
154 * will be fully ordered after the completion of the callback function,
155 * so that the module unloading operation is completely safe.
156 *
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700157 */
Joel Fernandes (Google)6bc33582020-11-03 09:25:57 -0500158void rcu_segcblist_add_len(struct rcu_segcblist *rsclp, long v)
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700159{
160#ifdef CONFIG_RCU_NOCB_CPU
Joel Fernandes (Google)c2e13112020-11-03 09:26:03 -0500161 smp_mb__before_atomic(); // Read header comment above.
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700162 atomic_long_add(v, &rsclp->len);
Joel Fernandes (Google)c2e13112020-11-03 09:26:03 -0500163 smp_mb__after_atomic(); // Read header comment above.
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700164#else
Joel Fernandes (Google)c2e13112020-11-03 09:26:03 -0500165 smp_mb(); // Read header comment above.
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700166 WRITE_ONCE(rsclp->len, rsclp->len + v);
Joel Fernandes (Google)c2e13112020-11-03 09:26:03 -0500167 smp_mb(); // Read header comment above.
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700168#endif
169}
170
171/*
172 * Increase the numeric length of an rcu_segcblist structure by one.
173 * This can cause the ->len field to disagree with the actual number of
174 * callbacks on the structure. This increase is fully ordered with respect
175 * to the callers accesses both before and after.
176 */
177void rcu_segcblist_inc_len(struct rcu_segcblist *rsclp)
178{
179 rcu_segcblist_add_len(rsclp, 1);
180}
181
182/*
183 * Exchange the numeric length of the specified rcu_segcblist structure
184 * with the specified value. This can cause the ->len field to disagree
185 * with the actual number of callbacks on the structure. This exchange is
186 * fully ordered with respect to the callers accesses both before and after.
187 */
kbuild test robot1d24dd42019-08-08 10:32:58 +0800188static long rcu_segcblist_xchg_len(struct rcu_segcblist *rsclp, long v)
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700189{
190#ifdef CONFIG_RCU_NOCB_CPU
191 return atomic_long_xchg(&rsclp->len, v);
192#else
193 long ret = rsclp->len;
194
195 smp_mb(); /* Up to the caller! */
196 WRITE_ONCE(rsclp->len, v);
197 smp_mb(); /* Up to the caller! */
198 return ret;
199#endif
200}
201
Paul E. McKenney98059b92017-05-02 06:30:12 -0700202/*
203 * Initialize an rcu_segcblist structure.
204 */
205void rcu_segcblist_init(struct rcu_segcblist *rsclp)
206{
207 int i;
208
209 BUILD_BUG_ON(RCU_NEXT_TAIL + 1 != ARRAY_SIZE(rsclp->gp_seq));
210 BUILD_BUG_ON(ARRAY_SIZE(rsclp->tails) != ARRAY_SIZE(rsclp->gp_seq));
211 rsclp->head = NULL;
212 for (i = 0; i < RCU_CBLIST_NSEGS; i++)
213 rsclp->tails[i] = &rsclp->head;
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700214 rcu_segcblist_set_len(rsclp, 0);
Paul E. McKenney1bb5f9b2019-04-12 12:34:41 -0700215 rsclp->enabled = 1;
Paul E. McKenney98059b92017-05-02 06:30:12 -0700216}
217
218/*
219 * Disable the specified rcu_segcblist structure, so that callbacks can
220 * no longer be posted to it. This structure must be empty.
221 */
222void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
223{
224 WARN_ON_ONCE(!rcu_segcblist_empty(rsclp));
225 WARN_ON_ONCE(rcu_segcblist_n_cbs(rsclp));
Paul E. McKenney1bb5f9b2019-04-12 12:34:41 -0700226 rsclp->enabled = 0;
Paul E. McKenney98059b92017-05-02 06:30:12 -0700227}
228
229/*
Paul E. McKenneyce5215c2019-04-12 15:58:34 -0700230 * Mark the specified rcu_segcblist structure as offloaded. This
231 * structure must be empty.
232 */
233void rcu_segcblist_offload(struct rcu_segcblist *rsclp)
234{
Paul E. McKenneyce5215c2019-04-12 15:58:34 -0700235 rsclp->offloaded = 1;
236}
237
238/*
Paul E. McKenney98059b92017-05-02 06:30:12 -0700239 * Does the specified rcu_segcblist structure contain callbacks that
240 * are ready to be invoked?
241 */
242bool rcu_segcblist_ready_cbs(struct rcu_segcblist *rsclp)
243{
244 return rcu_segcblist_is_enabled(rsclp) &&
Paul E. McKenneybfeebe22020-01-03 16:14:08 -0800245 &rsclp->head != READ_ONCE(rsclp->tails[RCU_DONE_TAIL]);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700246}
247
248/*
249 * Does the specified rcu_segcblist structure contain callbacks that
250 * are still pending, that is, not yet ready to be invoked?
251 */
252bool rcu_segcblist_pend_cbs(struct rcu_segcblist *rsclp)
253{
254 return rcu_segcblist_is_enabled(rsclp) &&
255 !rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL);
256}
257
258/*
Paul E. McKenney98059b92017-05-02 06:30:12 -0700259 * Return a pointer to the first callback in the specified rcu_segcblist
260 * structure. This is useful for diagnostics.
261 */
262struct rcu_head *rcu_segcblist_first_cb(struct rcu_segcblist *rsclp)
263{
264 if (rcu_segcblist_is_enabled(rsclp))
265 return rsclp->head;
266 return NULL;
267}
268
269/*
270 * Return a pointer to the first pending callback in the specified
271 * rcu_segcblist structure. This is useful just after posting a given
272 * callback -- if that callback is the first pending callback, then
273 * you cannot rely on someone else having already started up the required
274 * grace period.
275 */
276struct rcu_head *rcu_segcblist_first_pend_cb(struct rcu_segcblist *rsclp)
277{
278 if (rcu_segcblist_is_enabled(rsclp))
279 return *rsclp->tails[RCU_DONE_TAIL];
280 return NULL;
281}
282
283/*
Paul E. McKenney5d6742b2019-05-15 09:56:40 -0700284 * Return false if there are no CBs awaiting grace periods, otherwise,
285 * return true and store the nearest waited-upon grace period into *lp.
286 */
287bool rcu_segcblist_nextgp(struct rcu_segcblist *rsclp, unsigned long *lp)
288{
289 if (!rcu_segcblist_pend_cbs(rsclp))
290 return false;
291 *lp = rsclp->gp_seq[RCU_WAIT_TAIL];
292 return true;
293}
294
295/*
Paul E. McKenney98059b92017-05-02 06:30:12 -0700296 * Enqueue the specified callback onto the specified rcu_segcblist
297 * structure, updating accounting as needed. Note that the ->len
298 * field may be accessed locklessly, hence the WRITE_ONCE().
299 * The ->len field is used by rcu_barrier() and friends to determine
300 * if it must post a callback on this structure, and it is OK
301 * for rcu_barrier() to sometimes post callbacks needlessly, but
302 * absolutely not OK for it to ever miss posting a callback.
303 */
304void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -0400305 struct rcu_head *rhp)
Paul E. McKenney98059b92017-05-02 06:30:12 -0700306{
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700307 rcu_segcblist_inc_len(rsclp);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700308 smp_mb(); /* Ensure counts are updated before callback is enqueued. */
309 rhp->next = NULL;
Paul E. McKenney76c69272019-05-13 14:36:11 -0700310 WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rhp);
311 WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], &rhp->next);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700312}
313
314/*
315 * Entrain the specified callback onto the specified rcu_segcblist at
316 * the end of the last non-empty segment. If the entire rcu_segcblist
317 * is empty, make no change, but return false.
318 *
319 * This is intended for use by rcu_barrier()-like primitives, -not-
320 * for normal grace-period use. IMPORTANT: The callback you enqueue
321 * will wait for all prior callbacks, NOT necessarily for a grace
322 * period. You have been warned.
323 */
324bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
Joel Fernandes (Google)77a40f92019-08-30 12:36:32 -0400325 struct rcu_head *rhp)
Paul E. McKenney98059b92017-05-02 06:30:12 -0700326{
327 int i;
328
329 if (rcu_segcblist_n_cbs(rsclp) == 0)
330 return false;
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700331 rcu_segcblist_inc_len(rsclp);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700332 smp_mb(); /* Ensure counts are updated before callback is entrained. */
333 rhp->next = NULL;
334 for (i = RCU_NEXT_TAIL; i > RCU_DONE_TAIL; i--)
335 if (rsclp->tails[i] != rsclp->tails[i - 1])
336 break;
Paul E. McKenney76c69272019-05-13 14:36:11 -0700337 WRITE_ONCE(*rsclp->tails[i], rhp);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700338 for (; i <= RCU_NEXT_TAIL; i++)
Paul E. McKenney76c69272019-05-13 14:36:11 -0700339 WRITE_ONCE(rsclp->tails[i], &rhp->next);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700340 return true;
341}
342
343/*
344 * Extract only the counts from the specified rcu_segcblist structure,
345 * and place them in the specified rcu_cblist structure. This function
346 * supports both callback orphaning and invocation, hence the separation
347 * of counts and callbacks. (Callbacks ready for invocation must be
348 * orphaned and adopted separately from pending callbacks, but counts
349 * apply to all callbacks. Locking must be used to make sure that
350 * both orphaned-callbacks lists are consistent.)
351 */
352void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
353 struct rcu_cblist *rclp)
354{
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700355 rclp->len = rcu_segcblist_xchg_len(rsclp, 0);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700356}
357
358/*
359 * Extract only those callbacks ready to be invoked from the specified
360 * rcu_segcblist structure and place them in the specified rcu_cblist
361 * structure.
362 */
363void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
364 struct rcu_cblist *rclp)
365{
366 int i;
367
368 if (!rcu_segcblist_ready_cbs(rsclp))
369 return; /* Nothing to do. */
370 *rclp->tail = rsclp->head;
Paul E. McKenneye6060b42019-05-13 15:57:50 -0700371 WRITE_ONCE(rsclp->head, *rsclp->tails[RCU_DONE_TAIL]);
Paul E. McKenney76c69272019-05-13 14:36:11 -0700372 WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700373 rclp->tail = rsclp->tails[RCU_DONE_TAIL];
374 for (i = RCU_CBLIST_NSEGS - 1; i >= RCU_DONE_TAIL; i--)
375 if (rsclp->tails[i] == rsclp->tails[RCU_DONE_TAIL])
Paul E. McKenney76c69272019-05-13 14:36:11 -0700376 WRITE_ONCE(rsclp->tails[i], &rsclp->head);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700377}
378
379/*
380 * Extract only those callbacks still pending (not yet ready to be
381 * invoked) from the specified rcu_segcblist structure and place them in
382 * the specified rcu_cblist structure. Note that this loses information
383 * about any callbacks that might have been partway done waiting for
384 * their grace period. Too bad! They will have to start over.
385 */
386void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
387 struct rcu_cblist *rclp)
388{
389 int i;
390
391 if (!rcu_segcblist_pend_cbs(rsclp))
392 return; /* Nothing to do. */
393 *rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
394 rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
Paul E. McKenney76c69272019-05-13 14:36:11 -0700395 WRITE_ONCE(*rsclp->tails[RCU_DONE_TAIL], NULL);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700396 for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++)
Paul E. McKenney76c69272019-05-13 14:36:11 -0700397 WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_DONE_TAIL]);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700398}
399
400/*
401 * Insert counts from the specified rcu_cblist structure in the
402 * specified rcu_segcblist structure.
403 */
404void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
405 struct rcu_cblist *rclp)
406{
Paul E. McKenneyeda669a2019-07-01 17:36:53 -0700407 rcu_segcblist_add_len(rsclp, rclp->len);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700408 rclp->len = 0;
409}
410
411/*
412 * Move callbacks from the specified rcu_cblist to the beginning of the
413 * done-callbacks segment of the specified rcu_segcblist.
414 */
415void rcu_segcblist_insert_done_cbs(struct rcu_segcblist *rsclp,
416 struct rcu_cblist *rclp)
417{
418 int i;
419
420 if (!rclp->head)
421 return; /* No callbacks to move. */
422 *rclp->tail = rsclp->head;
Paul E. McKenneye6060b42019-05-13 15:57:50 -0700423 WRITE_ONCE(rsclp->head, rclp->head);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700424 for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
425 if (&rsclp->head == rsclp->tails[i])
Paul E. McKenney76c69272019-05-13 14:36:11 -0700426 WRITE_ONCE(rsclp->tails[i], rclp->tail);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700427 else
428 break;
429 rclp->head = NULL;
430 rclp->tail = &rclp->head;
431}
432
433/*
434 * Move callbacks from the specified rcu_cblist to the end of the
435 * new-callbacks segment of the specified rcu_segcblist.
436 */
437void rcu_segcblist_insert_pend_cbs(struct rcu_segcblist *rsclp,
438 struct rcu_cblist *rclp)
439{
440 if (!rclp->head)
441 return; /* Nothing to do. */
Paul E. McKenney76c69272019-05-13 14:36:11 -0700442 WRITE_ONCE(*rsclp->tails[RCU_NEXT_TAIL], rclp->head);
443 WRITE_ONCE(rsclp->tails[RCU_NEXT_TAIL], rclp->tail);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700444}
445
446/*
447 * Advance the callbacks in the specified rcu_segcblist structure based
448 * on the current value passed in for the grace-period counter.
449 */
450void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
451{
452 int i, j;
453
454 WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
455 if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
456 return;
457
458 /*
459 * Find all callbacks whose ->gp_seq numbers indicate that they
460 * are ready to invoke, and put them into the RCU_DONE_TAIL segment.
461 */
462 for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
463 if (ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
464 break;
Paul E. McKenney76c69272019-05-13 14:36:11 -0700465 WRITE_ONCE(rsclp->tails[RCU_DONE_TAIL], rsclp->tails[i]);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700466 }
467
468 /* If no callbacks moved, nothing more need be done. */
469 if (i == RCU_WAIT_TAIL)
470 return;
471
472 /* Clean up tail pointers that might have been misordered above. */
473 for (j = RCU_WAIT_TAIL; j < i; j++)
Paul E. McKenney76c69272019-05-13 14:36:11 -0700474 WRITE_ONCE(rsclp->tails[j], rsclp->tails[RCU_DONE_TAIL]);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700475
476 /*
477 * Callbacks moved, so clean up the misordered ->tails[] pointers
478 * that now point into the middle of the list of ready-to-invoke
479 * callbacks. The overall effect is to copy down the later pointers
480 * into the gap that was created by the now-ready segments.
481 */
482 for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
483 if (rsclp->tails[j] == rsclp->tails[RCU_NEXT_TAIL])
484 break; /* No more callbacks. */
Paul E. McKenney76c69272019-05-13 14:36:11 -0700485 WRITE_ONCE(rsclp->tails[j], rsclp->tails[i]);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700486 rsclp->gp_seq[j] = rsclp->gp_seq[i];
487 }
488}
489
490/*
491 * "Accelerate" callbacks based on more-accurate grace-period information.
492 * The reason for this is that RCU does not synchronize the beginnings and
493 * ends of grace periods, and that callbacks are posted locally. This in
494 * turn means that the callbacks must be labelled conservatively early
495 * on, as getting exact information would degrade both performance and
496 * scalability. When more accurate grace-period information becomes
497 * available, previously posted callbacks can be "accelerated", marking
498 * them to complete at the end of the earlier grace period.
499 *
500 * This function operates on an rcu_segcblist structure, and also the
501 * grace-period sequence number seq at which new callbacks would become
502 * ready to invoke. Returns true if there are callbacks that won't be
503 * ready to invoke until seq, false otherwise.
504 */
505bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
506{
507 int i;
508
509 WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
510 if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
511 return false;
512
513 /*
514 * Find the segment preceding the oldest segment of callbacks
515 * whose ->gp_seq[] completion is at or after that passed in via
516 * "seq", skipping any empty segments. This oldest segment, along
517 * with any later segments, can be merged in with any newly arrived
518 * callbacks in the RCU_NEXT_TAIL segment, and assigned "seq"
519 * as their ->gp_seq[] grace-period completion sequence number.
520 */
521 for (i = RCU_NEXT_READY_TAIL; i > RCU_DONE_TAIL; i--)
522 if (rsclp->tails[i] != rsclp->tails[i - 1] &&
523 ULONG_CMP_LT(rsclp->gp_seq[i], seq))
524 break;
525
526 /*
527 * If all the segments contain callbacks that correspond to
528 * earlier grace-period sequence numbers than "seq", leave.
529 * Assuming that the rcu_segcblist structure has enough
530 * segments in its arrays, this can only happen if some of
531 * the non-done segments contain callbacks that really are
532 * ready to invoke. This situation will get straightened
533 * out by the next call to rcu_segcblist_advance().
534 *
535 * Also advance to the oldest segment of callbacks whose
536 * ->gp_seq[] completion is at or after that passed in via "seq",
537 * skipping any empty segments.
Joel Fernandes (Google)53922272020-06-18 16:29:49 -0400538 *
539 * Note that segment "i" (and any lower-numbered segments
540 * containing older callbacks) will be unaffected, and their
541 * grace-period numbers remain unchanged. For example, if i ==
542 * WAIT_TAIL, then neither WAIT_TAIL nor DONE_TAIL will be touched.
543 * Instead, the CBs in NEXT_TAIL will be merged with those in
544 * NEXT_READY_TAIL and the grace-period number of NEXT_READY_TAIL
545 * would be updated. NEXT_TAIL would then be empty.
Paul E. McKenney98059b92017-05-02 06:30:12 -0700546 */
Joel Fernandes (Google)53922272020-06-18 16:29:49 -0400547 if (rcu_segcblist_restempty(rsclp, i) || ++i >= RCU_NEXT_TAIL)
Paul E. McKenney98059b92017-05-02 06:30:12 -0700548 return false;
549
550 /*
551 * Merge all later callbacks, including newly arrived callbacks,
552 * into the segment located by the for-loop above. Assign "seq"
553 * as the ->gp_seq[] value in order to correctly handle the case
554 * where there were no pending callbacks in the rcu_segcblist
555 * structure other than in the RCU_NEXT_TAIL segment.
556 */
557 for (; i < RCU_NEXT_TAIL; i++) {
Paul E. McKenney76c69272019-05-13 14:36:11 -0700558 WRITE_ONCE(rsclp->tails[i], rsclp->tails[RCU_NEXT_TAIL]);
Paul E. McKenney98059b92017-05-02 06:30:12 -0700559 rsclp->gp_seq[i] = seq;
560 }
561 return true;
562}
563
564/*
Paul E. McKenneyf2dbe4a2017-06-27 07:44:06 -0700565 * Merge the source rcu_segcblist structure into the destination
566 * rcu_segcblist structure, then initialize the source. Any pending
567 * callbacks from the source get to start over. It is best to
568 * advance and accelerate both the destination and the source
569 * before merging.
570 */
571void rcu_segcblist_merge(struct rcu_segcblist *dst_rsclp,
572 struct rcu_segcblist *src_rsclp)
573{
574 struct rcu_cblist donecbs;
575 struct rcu_cblist pendcbs;
576
577 rcu_cblist_init(&donecbs);
578 rcu_cblist_init(&pendcbs);
579 rcu_segcblist_extract_count(src_rsclp, &donecbs);
580 rcu_segcblist_extract_done_cbs(src_rsclp, &donecbs);
581 rcu_segcblist_extract_pend_cbs(src_rsclp, &pendcbs);
582 rcu_segcblist_insert_count(dst_rsclp, &donecbs);
583 rcu_segcblist_insert_done_cbs(dst_rsclp, &donecbs);
584 rcu_segcblist_insert_pend_cbs(dst_rsclp, &pendcbs);
585 rcu_segcblist_init(src_rsclp);
586}