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