xref: /linux/kernel/rcu/tasks.h (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Task-based RCU implementations.
4  *
5  * Copyright (C) 2020 Paul E. McKenney
6  */
7 
8 #ifdef CONFIG_TASKS_RCU_GENERIC
9 #include "rcu_segcblist.h"
10 
11 ////////////////////////////////////////////////////////////////////////
12 //
13 // Generic data structures.
14 
15 struct rcu_tasks;
16 typedef void (*rcu_tasks_gp_func_t)(struct rcu_tasks *rtp);
17 typedef void (*pregp_func_t)(struct list_head *hop);
18 typedef void (*pertask_func_t)(struct task_struct *t, struct list_head *hop);
19 typedef void (*postscan_func_t)(struct list_head *hop);
20 typedef void (*holdouts_func_t)(struct list_head *hop, bool ndrpt, bool *frptp);
21 typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
22 
23 /**
24  * struct rcu_tasks_percpu - Per-CPU component of definition for a Tasks-RCU-like mechanism.
25  * @cblist: Callback list.
26  * @lock: Lock protecting per-CPU callback list.
27  * @rtp_jiffies: Jiffies counter value for statistics.
28  * @lazy_timer: Timer to unlazify callbacks.
29  * @urgent_gp: Number of additional non-lazy grace periods.
30  * @rtp_n_lock_retries: Rough lock-contention statistic.
31  * @rtp_work: Work queue for invoking callbacks.
32  * @rtp_irq_work: IRQ work queue for deferred wakeups.
33  * @barrier_q_head: RCU callback for barrier operation.
34  * @rtp_blkd_tasks: List of tasks blocked as readers.
35  * @rtp_exit_list: List of tasks in the latter portion of do_exit().
36  * @cpu: CPU number corresponding to this entry.
37  * @index: Index of this CPU in rtpcp_array of the rcu_tasks structure.
38  * @rtpp: Pointer to the rcu_tasks structure.
39  */
40 struct rcu_tasks_percpu {
41 	struct rcu_segcblist cblist;
42 	raw_spinlock_t __private lock;
43 	unsigned long rtp_jiffies;
44 	unsigned long rtp_n_lock_retries;
45 	struct timer_list lazy_timer;
46 	unsigned int urgent_gp;
47 	struct work_struct rtp_work;
48 	struct irq_work rtp_irq_work;
49 	struct rcu_head barrier_q_head;
50 	struct list_head rtp_blkd_tasks;
51 	struct list_head rtp_exit_list;
52 	int cpu;
53 	int index;
54 	struct rcu_tasks *rtpp;
55 };
56 
57 /**
58  * struct rcu_tasks - Definition for a Tasks-RCU-like mechanism.
59  * @cbs_wait: RCU wait allowing a new callback to get kthread's attention.
60  * @cbs_gbl_lock: Lock protecting callback list.
61  * @tasks_gp_mutex: Mutex protecting grace period, needed during mid-boot dead zone.
62  * @gp_func: This flavor's grace-period-wait function.
63  * @gp_state: Grace period's most recent state transition (debugging).
64  * @gp_sleep: Per-grace-period sleep to prevent CPU-bound looping.
65  * @init_fract: Initial backoff sleep interval.
66  * @gp_jiffies: Time of last @gp_state transition.
67  * @gp_start: Most recent grace-period start in jiffies.
68  * @tasks_gp_seq: Number of grace periods completed since boot in upper bits.
69  * @n_ipis: Number of IPIs sent to encourage grace periods to end.
70  * @n_ipis_fails: Number of IPI-send failures.
71  * @kthread_ptr: This flavor's grace-period/callback-invocation kthread.
72  * @lazy_jiffies: Number of jiffies to allow callbacks to be lazy.
73  * @pregp_func: This flavor's pre-grace-period function (optional).
74  * @pertask_func: This flavor's per-task scan function (optional).
75  * @postscan_func: This flavor's post-task scan function (optional).
76  * @holdouts_func: This flavor's holdout-list scan function (optional).
77  * @postgp_func: This flavor's post-grace-period function (optional).
78  * @call_func: This flavor's call_rcu()-equivalent function.
79  * @wait_state: Task state for synchronous grace-period waits (default TASK_UNINTERRUPTIBLE).
80  * @rtpcpu: This flavor's rcu_tasks_percpu structure.
81  * @rtpcp_array: Array of pointers to rcu_tasks_percpu structure of CPUs in cpu_possible_mask.
82  * @percpu_enqueue_shift: Shift down CPU ID this much when enqueuing callbacks.
83  * @percpu_enqueue_lim: Number of per-CPU callback queues in use for enqueuing.
84  * @percpu_dequeue_lim: Number of per-CPU callback queues in use for dequeuing.
85  * @percpu_dequeue_gpseq: RCU grace-period number to propagate enqueue limit to dequeuers.
86  * @barrier_q_mutex: Serialize barrier operations.
87  * @barrier_q_count: Number of queues being waited on.
88  * @barrier_q_completion: Barrier wait/wakeup mechanism.
89  * @barrier_q_seq: Sequence number for barrier operations.
90  * @barrier_q_start: Most recent barrier start in jiffies.
91  * @name: This flavor's textual name.
92  * @kname: This flavor's kthread name.
93  */
94 struct rcu_tasks {
95 	struct rcuwait cbs_wait;
96 	raw_spinlock_t cbs_gbl_lock;
97 	struct mutex tasks_gp_mutex;
98 	int gp_state;
99 	int gp_sleep;
100 	int init_fract;
101 	unsigned long gp_jiffies;
102 	unsigned long gp_start;
103 	unsigned long tasks_gp_seq;
104 	unsigned long n_ipis;
105 	unsigned long n_ipis_fails;
106 	struct task_struct *kthread_ptr;
107 	unsigned long lazy_jiffies;
108 	rcu_tasks_gp_func_t gp_func;
109 	pregp_func_t pregp_func;
110 	pertask_func_t pertask_func;
111 	postscan_func_t postscan_func;
112 	holdouts_func_t holdouts_func;
113 	postgp_func_t postgp_func;
114 	call_rcu_func_t call_func;
115 	unsigned int wait_state;
116 	struct rcu_tasks_percpu __percpu *rtpcpu;
117 	struct rcu_tasks_percpu **rtpcp_array;
118 	int percpu_enqueue_shift;
119 	int percpu_enqueue_lim;
120 	int percpu_dequeue_lim;
121 	unsigned long percpu_dequeue_gpseq;
122 	struct mutex barrier_q_mutex;
123 	atomic_t barrier_q_count;
124 	struct completion barrier_q_completion;
125 	unsigned long barrier_q_seq;
126 	unsigned long barrier_q_start;
127 	char *name;
128 	char *kname;
129 };
130 
131 static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp);
132 
133 #define DEFINE_RCU_TASKS(rt_name, gp, call, n)						\
134 static DEFINE_PER_CPU(struct rcu_tasks_percpu, rt_name ## __percpu) = {			\
135 	.lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name ## __percpu.cbs_pcpu_lock),		\
136 	.rtp_irq_work = IRQ_WORK_INIT_HARD(call_rcu_tasks_iw_wakeup),			\
137 };											\
138 static struct rcu_tasks rt_name =							\
139 {											\
140 	.cbs_wait = __RCUWAIT_INITIALIZER(rt_name.wait),				\
141 	.cbs_gbl_lock = __RAW_SPIN_LOCK_UNLOCKED(rt_name.cbs_gbl_lock),			\
142 	.tasks_gp_mutex = __MUTEX_INITIALIZER(rt_name.tasks_gp_mutex),			\
143 	.gp_func = gp,									\
144 	.call_func = call,								\
145 	.wait_state = TASK_UNINTERRUPTIBLE,						\
146 	.rtpcpu = &rt_name ## __percpu,							\
147 	.lazy_jiffies = DIV_ROUND_UP(HZ, 4),						\
148 	.name = n,									\
149 	.percpu_enqueue_shift = order_base_2(CONFIG_NR_CPUS),				\
150 	.percpu_enqueue_lim = 1,							\
151 	.percpu_dequeue_lim = 1,							\
152 	.barrier_q_mutex = __MUTEX_INITIALIZER(rt_name.barrier_q_mutex),		\
153 	.barrier_q_seq = (0UL - 50UL) << RCU_SEQ_CTR_SHIFT,				\
154 	.kname = #rt_name,								\
155 }
156 
157 #ifdef CONFIG_TASKS_RCU
158 
159 /* Report delay of scan exiting tasklist in rcu_tasks_postscan(). */
160 static void tasks_rcu_exit_srcu_stall(struct timer_list *unused);
161 static DEFINE_TIMER(tasks_rcu_exit_srcu_stall_timer, tasks_rcu_exit_srcu_stall);
162 #endif
163 
164 /* Control stall timeouts.  Disable with <= 0, otherwise jiffies till stall. */
165 #define RCU_TASK_BOOT_STALL_TIMEOUT (HZ * 30)
166 #define RCU_TASK_STALL_TIMEOUT (HZ * 60 * 10)
167 static int rcu_task_stall_timeout __read_mostly = RCU_TASK_STALL_TIMEOUT;
168 module_param(rcu_task_stall_timeout, int, 0644);
169 #define RCU_TASK_STALL_INFO (HZ * 10)
170 static int rcu_task_stall_info __read_mostly = RCU_TASK_STALL_INFO;
171 module_param(rcu_task_stall_info, int, 0644);
172 static int rcu_task_stall_info_mult __read_mostly = 3;
173 module_param(rcu_task_stall_info_mult, int, 0444);
174 
175 static int rcu_task_enqueue_lim __read_mostly = -1;
176 module_param(rcu_task_enqueue_lim, int, 0444);
177 
178 static bool rcu_task_cb_adjust;
179 static int rcu_task_contend_lim __read_mostly = 100;
180 module_param(rcu_task_contend_lim, int, 0444);
181 static int rcu_task_collapse_lim __read_mostly = 10;
182 module_param(rcu_task_collapse_lim, int, 0444);
183 static int rcu_task_lazy_lim __read_mostly = 32;
184 module_param(rcu_task_lazy_lim, int, 0444);
185 
186 static int rcu_task_cpu_ids;
187 
188 /* RCU tasks grace-period state for debugging. */
189 #define RTGS_INIT		 0
190 #define RTGS_WAIT_WAIT_CBS	 1
191 #define RTGS_WAIT_GP		 2
192 #define RTGS_PRE_WAIT_GP	 3
193 #define RTGS_SCAN_TASKLIST	 4
194 #define RTGS_POST_SCAN_TASKLIST	 5
195 #define RTGS_WAIT_SCAN_HOLDOUTS	 6
196 #define RTGS_SCAN_HOLDOUTS	 7
197 #define RTGS_POST_GP		 8
198 #define RTGS_WAIT_READERS	 9
199 #define RTGS_INVOKE_CBS		10
200 #define RTGS_WAIT_CBS		11
201 #ifndef CONFIG_TINY_RCU
202 static const char * const rcu_tasks_gp_state_names[] = {
203 	"RTGS_INIT",
204 	"RTGS_WAIT_WAIT_CBS",
205 	"RTGS_WAIT_GP",
206 	"RTGS_PRE_WAIT_GP",
207 	"RTGS_SCAN_TASKLIST",
208 	"RTGS_POST_SCAN_TASKLIST",
209 	"RTGS_WAIT_SCAN_HOLDOUTS",
210 	"RTGS_SCAN_HOLDOUTS",
211 	"RTGS_POST_GP",
212 	"RTGS_WAIT_READERS",
213 	"RTGS_INVOKE_CBS",
214 	"RTGS_WAIT_CBS",
215 };
216 #endif /* #ifndef CONFIG_TINY_RCU */
217 
218 ////////////////////////////////////////////////////////////////////////
219 //
220 // Generic code.
221 
222 static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp);
223 
224 /* Record grace-period phase and time. */
225 static void set_tasks_gp_state(struct rcu_tasks *rtp, int newstate)
226 {
227 	rtp->gp_state = newstate;
228 	rtp->gp_jiffies = jiffies;
229 }
230 
231 #ifndef CONFIG_TINY_RCU
232 /* Return state name. */
233 static const char *tasks_gp_state_getname(struct rcu_tasks *rtp)
234 {
235 	int i = data_race(rtp->gp_state); // Let KCSAN detect update races
236 	int j = READ_ONCE(i); // Prevent the compiler from reading twice
237 
238 	if (j >= ARRAY_SIZE(rcu_tasks_gp_state_names))
239 		return "???";
240 	return rcu_tasks_gp_state_names[j];
241 }
242 #endif /* #ifndef CONFIG_TINY_RCU */
243 
244 // Initialize per-CPU callback lists for the specified flavor of
245 // Tasks RCU.  Do not enqueue callbacks before this function is invoked.
246 static void cblist_init_generic(struct rcu_tasks *rtp)
247 {
248 	int cpu;
249 	int lim;
250 	int shift;
251 	int maxcpu;
252 	int index = 0;
253 
254 	if (rcu_task_enqueue_lim < 0) {
255 		rcu_task_enqueue_lim = 1;
256 		rcu_task_cb_adjust = true;
257 	} else if (rcu_task_enqueue_lim == 0) {
258 		rcu_task_enqueue_lim = 1;
259 	}
260 	lim = rcu_task_enqueue_lim;
261 
262 	rtp->rtpcp_array = kzalloc_objs(struct rcu_tasks_percpu *,
263 					num_possible_cpus(), GFP_KERNEL);
264 	BUG_ON(!rtp->rtpcp_array);
265 
266 	for_each_possible_cpu(cpu) {
267 		struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
268 
269 		WARN_ON_ONCE(!rtpcp);
270 		if (cpu)
271 			raw_spin_lock_init(&ACCESS_PRIVATE(rtpcp, lock));
272 		if (rcu_segcblist_empty(&rtpcp->cblist))
273 			rcu_segcblist_init(&rtpcp->cblist);
274 		INIT_WORK(&rtpcp->rtp_work, rcu_tasks_invoke_cbs_wq);
275 		rtpcp->cpu = cpu;
276 		rtpcp->rtpp = rtp;
277 		rtpcp->index = index;
278 		rtp->rtpcp_array[index] = rtpcp;
279 		index++;
280 		if (!rtpcp->rtp_blkd_tasks.next)
281 			INIT_LIST_HEAD(&rtpcp->rtp_blkd_tasks);
282 		if (!rtpcp->rtp_exit_list.next)
283 			INIT_LIST_HEAD(&rtpcp->rtp_exit_list);
284 		rtpcp->barrier_q_head.next = &rtpcp->barrier_q_head;
285 		maxcpu = cpu;
286 	}
287 
288 	rcu_task_cpu_ids = maxcpu + 1;
289 	if (lim > rcu_task_cpu_ids)
290 		lim = rcu_task_cpu_ids;
291 	shift = ilog2(rcu_task_cpu_ids / lim);
292 	if (((rcu_task_cpu_ids - 1) >> shift) >= lim)
293 		shift++;
294 	WRITE_ONCE(rtp->percpu_enqueue_shift, shift);
295 	WRITE_ONCE(rtp->percpu_dequeue_lim, lim);
296 	smp_store_release(&rtp->percpu_enqueue_lim, lim);
297 
298 	pr_info("%s: Setting shift to %d and lim to %d rcu_task_cb_adjust=%d rcu_task_cpu_ids=%d.\n",
299 			rtp->name, data_race(rtp->percpu_enqueue_shift), data_race(rtp->percpu_enqueue_lim),
300 			rcu_task_cb_adjust, rcu_task_cpu_ids);
301 }
302 
303 // Compute wakeup time for lazy callback timer.
304 static unsigned long rcu_tasks_lazy_time(struct rcu_tasks *rtp)
305 {
306 	return jiffies + rtp->lazy_jiffies;
307 }
308 
309 // Timer handler that unlazifies lazy callbacks.
310 static void call_rcu_tasks_generic_timer(struct timer_list *tlp)
311 {
312 	unsigned long flags;
313 	bool needwake = false;
314 	struct rcu_tasks *rtp;
315 	struct rcu_tasks_percpu *rtpcp = timer_container_of(rtpcp, tlp,
316 						            lazy_timer);
317 
318 	rtp = rtpcp->rtpp;
319 	raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
320 	if (!rcu_segcblist_empty(&rtpcp->cblist) && rtp->lazy_jiffies) {
321 		if (!rtpcp->urgent_gp)
322 			rtpcp->urgent_gp = 1;
323 		needwake = true;
324 		mod_timer(&rtpcp->lazy_timer, rcu_tasks_lazy_time(rtp));
325 	}
326 	raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
327 	if (needwake)
328 		rcuwait_wake_up(&rtp->cbs_wait);
329 }
330 
331 // IRQ-work handler that does deferred wakeup for call_rcu_tasks_generic().
332 static void call_rcu_tasks_iw_wakeup(struct irq_work *iwp)
333 {
334 	struct rcu_tasks *rtp;
335 	struct rcu_tasks_percpu *rtpcp = container_of(iwp, struct rcu_tasks_percpu, rtp_irq_work);
336 
337 	rtp = rtpcp->rtpp;
338 	rcuwait_wake_up(&rtp->cbs_wait);
339 }
340 
341 // Enqueue a callback for the specified flavor of Tasks RCU.
342 static void call_rcu_tasks_generic(struct rcu_head *rhp, rcu_callback_t func,
343 				   struct rcu_tasks *rtp)
344 {
345 	int chosen_cpu;
346 	unsigned long flags;
347 	bool havekthread = smp_load_acquire(&rtp->kthread_ptr);
348 	int ideal_cpu;
349 	unsigned long j;
350 	bool needadjust = false;
351 	bool needwake;
352 	struct rcu_tasks_percpu *rtpcp;
353 
354 	rhp->next = NULL;
355 	rhp->func = func;
356 	local_irq_save(flags);
357 	rcu_read_lock();
358 	ideal_cpu = smp_processor_id() >> READ_ONCE(rtp->percpu_enqueue_shift);
359 	chosen_cpu = cpumask_next(ideal_cpu - 1, cpu_possible_mask);
360 	WARN_ON_ONCE(chosen_cpu >= rcu_task_cpu_ids);
361 	rtpcp = per_cpu_ptr(rtp->rtpcpu, chosen_cpu);
362 	if (!raw_spin_trylock_rcu_node(rtpcp)) { // irqs already disabled.
363 		raw_spin_lock_rcu_node(rtpcp); // irqs already disabled.
364 		j = jiffies;
365 		if (rtpcp->rtp_jiffies != j) {
366 			rtpcp->rtp_jiffies = j;
367 			rtpcp->rtp_n_lock_retries = 0;
368 		}
369 		if (rcu_task_cb_adjust && ++rtpcp->rtp_n_lock_retries > rcu_task_contend_lim &&
370 		    READ_ONCE(rtp->percpu_enqueue_lim) != rcu_task_cpu_ids)
371 			needadjust = true;  // Defer adjustment to avoid deadlock.
372 	}
373 	// Queuing callbacks before initialization not yet supported.
374 	if (WARN_ON_ONCE(!rcu_segcblist_is_enabled(&rtpcp->cblist)))
375 		rcu_segcblist_init(&rtpcp->cblist);
376 	needwake = (func == wakeme_after_rcu) ||
377 		   (rcu_segcblist_n_cbs(&rtpcp->cblist) == rcu_task_lazy_lim);
378 	if (havekthread && !needwake && !timer_pending(&rtpcp->lazy_timer)) {
379 		if (rtp->lazy_jiffies)
380 			mod_timer(&rtpcp->lazy_timer, rcu_tasks_lazy_time(rtp));
381 		else
382 			needwake = rcu_segcblist_empty(&rtpcp->cblist);
383 	}
384 	if (needwake)
385 		rtpcp->urgent_gp = 3;
386 	rcu_segcblist_enqueue(&rtpcp->cblist, rhp);
387 	raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
388 	if (unlikely(needadjust)) {
389 		raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
390 		if (rtp->percpu_enqueue_lim != rcu_task_cpu_ids) {
391 			WRITE_ONCE(rtp->percpu_enqueue_shift, 0);
392 			WRITE_ONCE(rtp->percpu_dequeue_lim, rcu_task_cpu_ids);
393 			smp_store_release(&rtp->percpu_enqueue_lim, rcu_task_cpu_ids);
394 			pr_info("Switching %s to per-CPU callback queuing.\n", rtp->name);
395 		}
396 		raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
397 	}
398 	rcu_read_unlock();
399 	/* We can't create the thread unless interrupts are enabled. */
400 	if (needwake && READ_ONCE(rtp->kthread_ptr))
401 		irq_work_queue(&rtpcp->rtp_irq_work);
402 }
403 
404 // RCU callback function for rcu_barrier_tasks_generic().
405 static void rcu_barrier_tasks_generic_cb(struct rcu_head *rhp)
406 {
407 	struct rcu_tasks *rtp;
408 	struct rcu_tasks_percpu *rtpcp;
409 
410 	rhp->next = rhp; // Mark the callback as having been invoked.
411 	rtpcp = container_of(rhp, struct rcu_tasks_percpu, barrier_q_head);
412 	rtp = rtpcp->rtpp;
413 	if (atomic_dec_and_test(&rtp->barrier_q_count))
414 		complete(&rtp->barrier_q_completion);
415 }
416 
417 // Wait for all in-flight callbacks for the specified RCU Tasks flavor.
418 // Operates in a manner similar to rcu_barrier().
419 static void __maybe_unused rcu_barrier_tasks_generic(struct rcu_tasks *rtp)
420 {
421 	int cpu;
422 	unsigned long flags;
423 	struct rcu_tasks_percpu *rtpcp;
424 	unsigned long s = rcu_seq_snap(&rtp->barrier_q_seq);
425 
426 	mutex_lock(&rtp->barrier_q_mutex);
427 	if (rcu_seq_done(&rtp->barrier_q_seq, s)) {
428 		smp_mb();
429 		mutex_unlock(&rtp->barrier_q_mutex);
430 		return;
431 	}
432 	rtp->barrier_q_start = jiffies;
433 	rcu_seq_start(&rtp->barrier_q_seq);
434 	init_completion(&rtp->barrier_q_completion);
435 	atomic_set(&rtp->barrier_q_count, 2);
436 	for_each_possible_cpu(cpu) {
437 		if (cpu >= smp_load_acquire(&rtp->percpu_dequeue_lim))
438 			break;
439 		rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
440 		rtpcp->barrier_q_head.func = rcu_barrier_tasks_generic_cb;
441 		raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
442 		if (rcu_segcblist_entrain(&rtpcp->cblist, &rtpcp->barrier_q_head))
443 			atomic_inc(&rtp->barrier_q_count);
444 		raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
445 	}
446 	if (atomic_sub_and_test(2, &rtp->barrier_q_count))
447 		complete(&rtp->barrier_q_completion);
448 	wait_for_completion(&rtp->barrier_q_completion);
449 	rcu_seq_end(&rtp->barrier_q_seq);
450 	mutex_unlock(&rtp->barrier_q_mutex);
451 }
452 
453 // Advance callbacks and indicate whether either a grace period or
454 // callback invocation is needed.
455 static int rcu_tasks_need_gpcb(struct rcu_tasks *rtp)
456 {
457 	int cpu;
458 	int dequeue_limit;
459 	unsigned long flags;
460 	bool gpdone = poll_state_synchronize_rcu(rtp->percpu_dequeue_gpseq);
461 	long n;
462 	long ncbs = 0;
463 	long ncbsnz = 0;
464 	int needgpcb = 0;
465 
466 	dequeue_limit = smp_load_acquire(&rtp->percpu_dequeue_lim);
467 	for (cpu = 0; cpu < dequeue_limit; cpu++) {
468 		if (!cpu_possible(cpu))
469 			continue;
470 		struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
471 
472 		/* Advance and accelerate any new callbacks. */
473 		if (!rcu_segcblist_n_cbs(&rtpcp->cblist))
474 			continue;
475 		raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
476 		// Should we shrink down to a single callback queue?
477 		n = rcu_segcblist_n_cbs(&rtpcp->cblist);
478 		if (n) {
479 			ncbs += n;
480 			if (cpu > 0)
481 				ncbsnz += n;
482 		}
483 		rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
484 		(void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
485 		if (rtpcp->urgent_gp > 0 && rcu_segcblist_pend_cbs(&rtpcp->cblist)) {
486 			if (rtp->lazy_jiffies)
487 				rtpcp->urgent_gp--;
488 			needgpcb |= 0x3;
489 		} else if (rcu_segcblist_empty(&rtpcp->cblist)) {
490 			rtpcp->urgent_gp = 0;
491 		}
492 		if (rcu_segcblist_ready_cbs(&rtpcp->cblist))
493 			needgpcb |= 0x1;
494 		raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
495 	}
496 
497 	// Shrink down to a single callback queue if appropriate.
498 	// This is done in two stages: (1) If there are no more than
499 	// rcu_task_collapse_lim callbacks on CPU 0 and none on any other
500 	// CPU, limit enqueueing to CPU 0.  (2) After an RCU grace period,
501 	// if there has not been an increase in callbacks, limit dequeuing
502 	// to CPU 0.  Note the matching RCU read-side critical section in
503 	// call_rcu_tasks_generic().
504 	if (rcu_task_cb_adjust && ncbs <= rcu_task_collapse_lim) {
505 		raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
506 		if (rtp->percpu_enqueue_lim > 1) {
507 			WRITE_ONCE(rtp->percpu_enqueue_shift, order_base_2(rcu_task_cpu_ids));
508 			smp_store_release(&rtp->percpu_enqueue_lim, 1);
509 			rtp->percpu_dequeue_gpseq = get_state_synchronize_rcu();
510 			gpdone = false;
511 			pr_info("Starting switch %s to CPU-0 callback queuing.\n", rtp->name);
512 		}
513 		raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
514 	}
515 	if (rcu_task_cb_adjust && !ncbsnz && gpdone) {
516 		raw_spin_lock_irqsave(&rtp->cbs_gbl_lock, flags);
517 		if (rtp->percpu_enqueue_lim < rtp->percpu_dequeue_lim) {
518 			WRITE_ONCE(rtp->percpu_dequeue_lim, 1);
519 			pr_info("Completing switch %s to CPU-0 callback queuing.\n", rtp->name);
520 		}
521 		if (rtp->percpu_dequeue_lim == 1) {
522 			for (cpu = rtp->percpu_dequeue_lim; cpu < rcu_task_cpu_ids; cpu++) {
523 				if (!cpu_possible(cpu))
524 					continue;
525 				struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
526 
527 				WARN_ON_ONCE(rcu_segcblist_n_cbs(&rtpcp->cblist));
528 			}
529 		}
530 		raw_spin_unlock_irqrestore(&rtp->cbs_gbl_lock, flags);
531 	}
532 
533 	return needgpcb;
534 }
535 
536 // Advance callbacks and invoke any that are ready.
537 static void rcu_tasks_invoke_cbs(struct rcu_tasks *rtp, struct rcu_tasks_percpu *rtpcp)
538 {
539 	int cpuwq;
540 	unsigned long flags;
541 	int len;
542 	int index;
543 	struct rcu_head *rhp;
544 	struct rcu_cblist rcl = RCU_CBLIST_INITIALIZER(rcl);
545 	struct rcu_tasks_percpu *rtpcp_next;
546 
547 	index = rtpcp->index * 2 + 1;
548 	if (index < num_possible_cpus()) {
549 		rtpcp_next = rtp->rtpcp_array[index];
550 		if (rtpcp_next->cpu < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
551 			cpuwq = rcu_cpu_beenfullyonline(rtpcp_next->cpu) ? rtpcp_next->cpu : WORK_CPU_UNBOUND;
552 			queue_work_on(cpuwq, system_percpu_wq, &rtpcp_next->rtp_work);
553 			index++;
554 			if (index < num_possible_cpus()) {
555 				rtpcp_next = rtp->rtpcp_array[index];
556 				if (rtpcp_next->cpu < smp_load_acquire(&rtp->percpu_dequeue_lim)) {
557 					cpuwq = rcu_cpu_beenfullyonline(rtpcp_next->cpu) ? rtpcp_next->cpu : WORK_CPU_UNBOUND;
558 					queue_work_on(cpuwq, system_percpu_wq, &rtpcp_next->rtp_work);
559 				}
560 			}
561 		}
562 	}
563 
564 	if (rcu_segcblist_empty(&rtpcp->cblist))
565 		return;
566 	raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
567 	rcu_segcblist_advance(&rtpcp->cblist, rcu_seq_current(&rtp->tasks_gp_seq));
568 	rcu_segcblist_extract_done_cbs(&rtpcp->cblist, &rcl);
569 	raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
570 	len = rcl.len;
571 	for (rhp = rcu_cblist_dequeue(&rcl); rhp; rhp = rcu_cblist_dequeue(&rcl)) {
572 		debug_rcu_head_callback(rhp);
573 		local_bh_disable();
574 		rhp->func(rhp);
575 		local_bh_enable();
576 		cond_resched();
577 	}
578 	raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
579 	rcu_segcblist_add_len(&rtpcp->cblist, -len);
580 	(void)rcu_segcblist_accelerate(&rtpcp->cblist, rcu_seq_snap(&rtp->tasks_gp_seq));
581 	raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
582 }
583 
584 // Workqueue flood to advance callbacks and invoke any that are ready.
585 static void rcu_tasks_invoke_cbs_wq(struct work_struct *wp)
586 {
587 	struct rcu_tasks *rtp;
588 	struct rcu_tasks_percpu *rtpcp = container_of(wp, struct rcu_tasks_percpu, rtp_work);
589 
590 	rtp = rtpcp->rtpp;
591 	rcu_tasks_invoke_cbs(rtp, rtpcp);
592 }
593 
594 // Wait for one grace period.
595 static void rcu_tasks_one_gp(struct rcu_tasks *rtp, bool midboot)
596 {
597 	int needgpcb;
598 
599 	mutex_lock(&rtp->tasks_gp_mutex);
600 
601 	// If there were none, wait a bit and start over.
602 	if (unlikely(midboot)) {
603 		needgpcb = 0x2;
604 	} else {
605 		mutex_unlock(&rtp->tasks_gp_mutex);
606 		set_tasks_gp_state(rtp, RTGS_WAIT_CBS);
607 		rcuwait_wait_event(&rtp->cbs_wait,
608 				   (needgpcb = rcu_tasks_need_gpcb(rtp)),
609 				   TASK_IDLE);
610 		mutex_lock(&rtp->tasks_gp_mutex);
611 	}
612 
613 	if (needgpcb & 0x2) {
614 		// Wait for one grace period.
615 		set_tasks_gp_state(rtp, RTGS_WAIT_GP);
616 		rtp->gp_start = jiffies;
617 		rcu_seq_start(&rtp->tasks_gp_seq);
618 		rtp->gp_func(rtp);
619 		rcu_seq_end(&rtp->tasks_gp_seq);
620 	}
621 
622 	// Invoke callbacks.
623 	set_tasks_gp_state(rtp, RTGS_INVOKE_CBS);
624 	rcu_tasks_invoke_cbs(rtp, per_cpu_ptr(rtp->rtpcpu, 0));
625 	mutex_unlock(&rtp->tasks_gp_mutex);
626 }
627 
628 // RCU-tasks kthread that detects grace periods and invokes callbacks.
629 static int __noreturn rcu_tasks_kthread(void *arg)
630 {
631 	int cpu;
632 	struct rcu_tasks *rtp = arg;
633 
634 	for_each_possible_cpu(cpu) {
635 		struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
636 
637 		timer_setup(&rtpcp->lazy_timer, call_rcu_tasks_generic_timer, 0);
638 		rtpcp->urgent_gp = 1;
639 	}
640 
641 	/* Run on housekeeping CPUs by default.  Sysadm can move if desired. */
642 	housekeeping_affine(current, HK_TYPE_RCU);
643 	smp_store_release(&rtp->kthread_ptr, current); // Let GPs start!
644 
645 	/*
646 	 * Each pass through the following loop makes one check for
647 	 * newly arrived callbacks, and, if there are some, waits for
648 	 * one RCU-tasks grace period and then invokes the callbacks.
649 	 * This loop is terminated by the system going down.  ;-)
650 	 */
651 	for (;;) {
652 		// Wait for one grace period and invoke any callbacks
653 		// that are ready.
654 		rcu_tasks_one_gp(rtp, false);
655 
656 		// Paranoid sleep to keep this from entering a tight loop.
657 		schedule_timeout_idle(rtp->gp_sleep);
658 	}
659 }
660 
661 // Wait for a grace period for the specified flavor of Tasks RCU.
662 static void synchronize_rcu_tasks_generic(struct rcu_tasks *rtp)
663 {
664 	/* Complain if the scheduler has not started.  */
665 	if (WARN_ONCE(rcu_scheduler_active == RCU_SCHEDULER_INACTIVE,
666 			 "synchronize_%s() called too soon", rtp->name))
667 		return;
668 
669 	// If the grace-period kthread is running, use it.
670 	if (READ_ONCE(rtp->kthread_ptr)) {
671 		wait_rcu_gp_state(rtp->wait_state, rtp->call_func);
672 		return;
673 	}
674 	rcu_tasks_one_gp(rtp, true);
675 }
676 
677 /* Spawn RCU-tasks grace-period kthread. */
678 static void __init rcu_spawn_tasks_kthread_generic(struct rcu_tasks *rtp)
679 {
680 	struct task_struct *t;
681 
682 	t = kthread_run(rcu_tasks_kthread, rtp, "%s_kthread", rtp->kname);
683 	if (WARN_ONCE(IS_ERR(t), "%s: Could not start %s grace-period kthread, OOM is now expected behavior\n", __func__, rtp->name))
684 		return;
685 	smp_mb(); /* Ensure others see full kthread. */
686 }
687 
688 #ifndef CONFIG_TINY_RCU
689 
690 /*
691  * Print any non-default Tasks RCU settings.
692  */
693 static void __init rcu_tasks_bootup_oddness(void)
694 {
695 #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
696 	int rtsimc;
697 
698 	if (rcu_task_stall_timeout != RCU_TASK_STALL_TIMEOUT)
699 		pr_info("\tTasks-RCU CPU stall warnings timeout set to %d (rcu_task_stall_timeout).\n", rcu_task_stall_timeout);
700 	rtsimc = clamp(rcu_task_stall_info_mult, 1, 10);
701 	if (rtsimc != rcu_task_stall_info_mult) {
702 		pr_info("\tTasks-RCU CPU stall info multiplier clamped to %d (rcu_task_stall_info_mult).\n", rtsimc);
703 		rcu_task_stall_info_mult = rtsimc;
704 	}
705 #endif /* #ifdef CONFIG_TASKS_RCU */
706 #ifdef CONFIG_TASKS_RCU
707 	pr_info("\tTrampoline variant of Tasks RCU enabled.\n");
708 #endif /* #ifdef CONFIG_TASKS_RCU */
709 #ifdef CONFIG_TASKS_RUDE_RCU
710 	pr_info("\tRude variant of Tasks RCU enabled.\n");
711 #endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
712 #ifdef CONFIG_TASKS_TRACE_RCU
713 	pr_info("\tTracing variant of Tasks RCU enabled.\n");
714 #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */
715 }
716 
717 /* Dump out rcutorture-relevant state common to all RCU-tasks flavors. */
718 static void show_rcu_tasks_generic_gp_kthread(struct rcu_tasks *rtp, char *s)
719 {
720 	int cpu;
721 	bool havecbs = false;
722 	bool haveurgent = false;
723 	bool haveurgentcbs = false;
724 
725 	for_each_possible_cpu(cpu) {
726 		struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
727 
728 		if (!data_race(rcu_segcblist_empty(&rtpcp->cblist)))
729 			havecbs = true;
730 		if (data_race(rtpcp->urgent_gp))
731 			haveurgent = true;
732 		if (!data_race(rcu_segcblist_empty(&rtpcp->cblist)) && data_race(rtpcp->urgent_gp))
733 			haveurgentcbs = true;
734 		if (havecbs && haveurgent && haveurgentcbs)
735 			break;
736 	}
737 	pr_info("%s: %s(%d) since %lu g:%lu i:%lu/%lu %c%c%c%c l:%lu %s\n",
738 		rtp->kname,
739 		tasks_gp_state_getname(rtp), data_race(rtp->gp_state),
740 		jiffies - data_race(rtp->gp_jiffies),
741 		data_race(rcu_seq_current(&rtp->tasks_gp_seq)),
742 		data_race(rtp->n_ipis_fails), data_race(rtp->n_ipis),
743 		".k"[!!data_race(rtp->kthread_ptr)],
744 		".C"[havecbs],
745 		".u"[haveurgent],
746 		".U"[haveurgentcbs],
747 		rtp->lazy_jiffies,
748 		s);
749 }
750 
751 /* Dump out more rcutorture-relevant state common to all RCU-tasks flavors. */
752 static void rcu_tasks_torture_stats_print_generic(struct rcu_tasks *rtp, char *tt,
753 						  char *tf, char *tst)
754 {
755 	cpumask_var_t cm;
756 	int cpu;
757 	bool gotcb = false;
758 	unsigned long j = jiffies;
759 
760 	pr_alert("%s%s Tasks%s RCU g%ld gp_start %lu gp_jiffies %lu gp_state %d (%s).\n",
761 		 tt, tf, tst, data_race(rtp->tasks_gp_seq),
762 		 j - data_race(rtp->gp_start), j - data_race(rtp->gp_jiffies),
763 		 data_race(rtp->gp_state), tasks_gp_state_getname(rtp));
764 	pr_alert("\tEnqueue shift %d limit %d Dequeue limit %d gpseq %lu.\n",
765 		 data_race(rtp->percpu_enqueue_shift),
766 		 data_race(rtp->percpu_enqueue_lim),
767 		 data_race(rtp->percpu_dequeue_lim),
768 		 data_race(rtp->percpu_dequeue_gpseq));
769 	(void)zalloc_cpumask_var(&cm, GFP_KERNEL);
770 	pr_alert("\tCallback counts:");
771 	for_each_possible_cpu(cpu) {
772 		long n;
773 		struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rtp->rtpcpu, cpu);
774 
775 		if (cpumask_available(cm) && !rcu_barrier_cb_is_done(&rtpcp->barrier_q_head))
776 			cpumask_set_cpu(cpu, cm);
777 		n = rcu_segcblist_n_cbs(&rtpcp->cblist);
778 		if (!n)
779 			continue;
780 		pr_cont(" %d:%ld", cpu, n);
781 		gotcb = true;
782 	}
783 	if (gotcb)
784 		pr_cont(".\n");
785 	else
786 		pr_cont(" (none).\n");
787 	pr_alert("\tBarrier seq %lu start %lu count %d holdout CPUs ",
788 		 data_race(rtp->barrier_q_seq), j - data_race(rtp->barrier_q_start),
789 		 atomic_read(&rtp->barrier_q_count));
790 	if (cpumask_available(cm) && !cpumask_empty(cm))
791 		pr_cont(" %*pbl.\n", cpumask_pr_args(cm));
792 	else
793 		pr_cont("(none).\n");
794 	free_cpumask_var(cm);
795 }
796 
797 #endif // #ifndef CONFIG_TINY_RCU
798 
799 #if defined(CONFIG_TASKS_RCU)
800 
801 ////////////////////////////////////////////////////////////////////////
802 //
803 // Shared code between task-list-scanning variants of Tasks RCU.
804 
805 /* Wait for one RCU-tasks grace period. */
806 static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
807 {
808 	struct task_struct *g;
809 	int fract;
810 	LIST_HEAD(holdouts);
811 	unsigned long j;
812 	unsigned long lastinfo;
813 	unsigned long lastreport;
814 	bool reported = false;
815 	int rtsi;
816 	struct task_struct *t;
817 
818 	set_tasks_gp_state(rtp, RTGS_PRE_WAIT_GP);
819 	rtp->pregp_func(&holdouts);
820 
821 	/*
822 	 * There were callbacks, so we need to wait for an RCU-tasks
823 	 * grace period.  Start off by scanning the task list for tasks
824 	 * that are not already voluntarily blocked.  Mark these tasks
825 	 * and make a list of them in holdouts.
826 	 */
827 	set_tasks_gp_state(rtp, RTGS_SCAN_TASKLIST);
828 	if (rtp->pertask_func) {
829 		rcu_read_lock();
830 		for_each_process_thread(g, t)
831 			rtp->pertask_func(t, &holdouts);
832 		rcu_read_unlock();
833 	}
834 
835 	set_tasks_gp_state(rtp, RTGS_POST_SCAN_TASKLIST);
836 	rtp->postscan_func(&holdouts);
837 
838 	/*
839 	 * Each pass through the following loop scans the list of holdout
840 	 * tasks, removing any that are no longer holdouts.  When the list
841 	 * is empty, we are done.
842 	 */
843 	lastreport = jiffies;
844 	lastinfo = lastreport;
845 	rtsi = READ_ONCE(rcu_task_stall_info);
846 
847 	// Start off with initial wait and slowly back off to 1 HZ wait.
848 	fract = rtp->init_fract;
849 
850 	while (!list_empty(&holdouts)) {
851 		ktime_t exp;
852 		bool firstreport;
853 		bool needreport;
854 		int rtst;
855 
856 		// Slowly back off waiting for holdouts
857 		set_tasks_gp_state(rtp, RTGS_WAIT_SCAN_HOLDOUTS);
858 		if (!IS_ENABLED(CONFIG_PREEMPT_RT)) {
859 			schedule_timeout_idle(fract);
860 		} else {
861 			exp = jiffies_to_nsecs(fract);
862 			__set_current_state(TASK_IDLE);
863 			schedule_hrtimeout_range(&exp, jiffies_to_nsecs(HZ / 2), HRTIMER_MODE_REL_HARD);
864 		}
865 
866 		if (fract < HZ)
867 			fract++;
868 
869 		rtst = READ_ONCE(rcu_task_stall_timeout);
870 		needreport = rtst > 0 && time_after(jiffies, lastreport + rtst);
871 		if (needreport) {
872 			lastreport = jiffies;
873 			reported = true;
874 		}
875 		firstreport = true;
876 		WARN_ON(signal_pending(current));
877 		set_tasks_gp_state(rtp, RTGS_SCAN_HOLDOUTS);
878 		rtp->holdouts_func(&holdouts, needreport, &firstreport);
879 
880 		// Print pre-stall informational messages if needed.
881 		j = jiffies;
882 		if (rtsi > 0 && !reported && time_after(j, lastinfo + rtsi)) {
883 			lastinfo = j;
884 			rtsi = rtsi * rcu_task_stall_info_mult;
885 			pr_info("%s: %s grace period number %lu (since boot) is %lu jiffies old.\n",
886 				__func__, rtp->kname, rtp->tasks_gp_seq, j - rtp->gp_start);
887 		}
888 	}
889 
890 	set_tasks_gp_state(rtp, RTGS_POST_GP);
891 	rtp->postgp_func(rtp);
892 }
893 
894 #endif /* #if defined(CONFIG_TASKS_RCU) */
895 
896 #ifdef CONFIG_TASKS_RCU
897 
898 ////////////////////////////////////////////////////////////////////////
899 //
900 // Simple variant of RCU whose quiescent states are voluntary context
901 // switch, cond_resched_tasks_rcu_qs(), user-space execution, and idle.
902 // As such, grace periods can take one good long time.  There are no
903 // read-side primitives similar to rcu_read_lock() and rcu_read_unlock()
904 // because this implementation is intended to get the system into a safe
905 // state for some of the manipulations involved in tracing and the like.
906 // Finally, this implementation does not support high call_rcu_tasks()
907 // rates from multiple CPUs.  If this is required, per-CPU callback lists
908 // will be needed.
909 //
910 // The implementation uses rcu_tasks_wait_gp(), which relies on function
911 // pointers in the rcu_tasks structure.  The rcu_spawn_tasks_kthread()
912 // function sets these function pointers up so that rcu_tasks_wait_gp()
913 // invokes these functions in this order:
914 //
915 // rcu_tasks_pregp_step():
916 //	Invokes synchronize_rcu() in order to wait for all in-flight
917 //	t->on_rq and t->nvcsw transitions to complete.	This works because
918 //	all such transitions are carried out with interrupts disabled.
919 // rcu_tasks_pertask(), invoked on every non-idle task:
920 //	For every runnable non-idle task other than the current one, use
921 //	get_task_struct() to pin down that task, snapshot that task's
922 //	number of voluntary context switches, and add that task to the
923 //	holdout list.
924 // rcu_tasks_postscan():
925 //	Gather per-CPU lists of tasks in do_exit() to ensure that all
926 //	tasks that were in the process of exiting (and which thus might
927 //	not know to synchronize with this RCU Tasks grace period) have
928 //	completed exiting.  The synchronize_rcu() in rcu_tasks_postgp()
929 //	will take care of any tasks stuck in the non-preemptible region
930 //	of do_exit() following its call to exit_tasks_rcu_finish().
931 // check_all_holdout_tasks(), repeatedly until holdout list is empty:
932 //	Scans the holdout list, attempting to identify a quiescent state
933 //	for each task on the list.  If there is a quiescent state, the
934 //	corresponding task is removed from the holdout list.
935 // rcu_tasks_postgp():
936 //	Invokes synchronize_rcu() in order to ensure that all prior
937 //	t->on_rq and t->nvcsw transitions are seen by all CPUs and tasks
938 //	to have happened before the end of this RCU Tasks grace period.
939 //	Again, this works because all such transitions are carried out
940 //	with interrupts disabled.
941 //
942 // For each exiting task, the exit_tasks_rcu_start() and
943 // exit_tasks_rcu_finish() functions add and remove, respectively, the
944 // current task to a per-CPU list of tasks that rcu_tasks_postscan() must
945 // wait on.  This is necessary because rcu_tasks_postscan() must wait on
946 // tasks that have already been removed from the global list of tasks.
947 //
948 // Pre-grace-period update-side code is ordered before the grace
949 // via the raw_spin_lock.*rcu_node().  Pre-grace-period read-side code
950 // is ordered before the grace period via synchronize_rcu() call in
951 // rcu_tasks_pregp_step() and by the scheduler's locks and interrupt
952 // disabling.
953 
954 /* Pre-grace-period preparation. */
955 static void rcu_tasks_pregp_step(struct list_head *hop)
956 {
957 	/*
958 	 * Wait for all pre-existing t->on_rq and t->nvcsw transitions
959 	 * to complete.  Invoking synchronize_rcu() suffices because all
960 	 * these transitions occur with interrupts disabled.  Without this
961 	 * synchronize_rcu(), a read-side critical section that started
962 	 * before the grace period might be incorrectly seen as having
963 	 * started after the grace period.
964 	 *
965 	 * This synchronize_rcu() also dispenses with the need for a
966 	 * memory barrier on the first store to t->rcu_tasks_holdout,
967 	 * as it forces the store to happen after the beginning of the
968 	 * grace period.
969 	 */
970 	synchronize_rcu();
971 }
972 
973 /* Check for quiescent states since the pregp's synchronize_rcu() */
974 static bool rcu_tasks_is_holdout(struct task_struct *t)
975 {
976 	int cpu;
977 
978 	/* Has the task been seen voluntarily sleeping? */
979 	if (!READ_ONCE(t->on_rq))
980 		return false;
981 
982 	/*
983 	 * t->on_rq && !t->se.sched_delayed *could* be considered sleeping but
984 	 * since it is a spurious state (it will transition into the
985 	 * traditional blocked state or get woken up without outside
986 	 * dependencies), not considering it such should only affect timing.
987 	 *
988 	 * Be conservative for now and not include it.
989 	 */
990 
991 	/*
992 	 * Idle tasks (or idle injection) within the idle loop are RCU-tasks
993 	 * quiescent states. But CPU boot code performed by the idle task
994 	 * isn't a quiescent state.
995 	 */
996 	if (is_idle_task(t))
997 		return false;
998 
999 	cpu = task_cpu(t);
1000 
1001 	/* Idle tasks on offline CPUs are RCU-tasks quiescent states. */
1002 	if (t == idle_task(cpu) && !rcu_cpu_online(cpu))
1003 		return false;
1004 
1005 	return true;
1006 }
1007 
1008 /* Per-task initial processing. */
1009 static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
1010 {
1011 	if (t != current && rcu_tasks_is_holdout(t)) {
1012 		get_task_struct(t);
1013 		t->rcu_tasks_nvcsw = READ_ONCE(t->nvcsw);
1014 		WRITE_ONCE(t->rcu_tasks_holdout, true);
1015 		list_add(&t->rcu_tasks_holdout_list, hop);
1016 	}
1017 }
1018 
1019 void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
1020 DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
1021 
1022 /* Processing between scanning taskslist and draining the holdout list. */
1023 static void rcu_tasks_postscan(struct list_head *hop)
1024 {
1025 	int cpu;
1026 	int rtsi = READ_ONCE(rcu_task_stall_info);
1027 
1028 	if (!IS_ENABLED(CONFIG_TINY_RCU)) {
1029 		tasks_rcu_exit_srcu_stall_timer.expires = jiffies + rtsi;
1030 		add_timer(&tasks_rcu_exit_srcu_stall_timer);
1031 	}
1032 
1033 	/*
1034 	 * Exiting tasks may escape the tasklist scan. Those are vulnerable
1035 	 * until their final schedule() with TASK_DEAD state. To cope with
1036 	 * this, divide the fragile exit path part in two intersecting
1037 	 * read side critical sections:
1038 	 *
1039 	 * 1) A task_struct list addition before calling exit_notify(),
1040 	 *    which may remove the task from the tasklist, with the
1041 	 *    removal after the final preempt_disable() call in do_exit().
1042 	 *
1043 	 * 2) An _RCU_ read side starting with the final preempt_disable()
1044 	 *    call in do_exit() and ending with the final call to schedule()
1045 	 *    with TASK_DEAD state.
1046 	 *
1047 	 * This handles the part 1). And postgp will handle part 2) with a
1048 	 * call to synchronize_rcu().
1049 	 */
1050 
1051 	for_each_possible_cpu(cpu) {
1052 		unsigned long j = jiffies + 1;
1053 		struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
1054 		struct task_struct *t;
1055 		struct task_struct *t1;
1056 		struct list_head tmp;
1057 
1058 		raw_spin_lock_irq_rcu_node(rtpcp);
1059 		list_for_each_entry_safe(t, t1, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
1060 			if (list_empty(&t->rcu_tasks_holdout_list))
1061 				rcu_tasks_pertask(t, hop);
1062 
1063 			// RT kernels need frequent pauses, otherwise
1064 			// pause at least once per pair of jiffies.
1065 			if (!IS_ENABLED(CONFIG_PREEMPT_RT) && time_before(jiffies, j))
1066 				continue;
1067 
1068 			// Keep our place in the list while pausing.
1069 			// Nothing else traverses this list, so adding a
1070 			// bare list_head is OK.
1071 			list_add(&tmp, &t->rcu_tasks_exit_list);
1072 			raw_spin_unlock_irq_rcu_node(rtpcp);
1073 			cond_resched(); // For CONFIG_PREEMPT=n kernels
1074 			raw_spin_lock_irq_rcu_node(rtpcp);
1075 			t1 = list_entry(tmp.next, struct task_struct, rcu_tasks_exit_list);
1076 			list_del(&tmp);
1077 			j = jiffies + 1;
1078 		}
1079 		raw_spin_unlock_irq_rcu_node(rtpcp);
1080 	}
1081 
1082 	if (!IS_ENABLED(CONFIG_TINY_RCU))
1083 		timer_delete_sync(&tasks_rcu_exit_srcu_stall_timer);
1084 }
1085 
1086 /* See if tasks are still holding out, complain if so. */
1087 static void check_holdout_task(struct task_struct *t,
1088 			       bool needreport, bool *firstreport)
1089 {
1090 	int cpu;
1091 
1092 	if (!READ_ONCE(t->rcu_tasks_holdout) ||
1093 	    t->rcu_tasks_nvcsw != READ_ONCE(t->nvcsw) ||
1094 	    !rcu_tasks_is_holdout(t) ||
1095 	    (IS_ENABLED(CONFIG_NO_HZ_FULL) &&
1096 	     !is_idle_task(t) && READ_ONCE(t->rcu_tasks_idle_cpu) >= 0)) {
1097 		WRITE_ONCE(t->rcu_tasks_holdout, false);
1098 		list_del_init(&t->rcu_tasks_holdout_list);
1099 		put_task_struct(t);
1100 		return;
1101 	}
1102 	rcu_request_urgent_qs_task(t);
1103 	if (!needreport)
1104 		return;
1105 	if (*firstreport) {
1106 		pr_err("INFO: rcu_tasks detected stalls on tasks:\n");
1107 		*firstreport = false;
1108 	}
1109 	cpu = task_cpu(t);
1110 	pr_alert("%p: %c%c nvcsw: %lu/%lu holdout: %d idle_cpu: %d/%d\n",
1111 		 t, ".I"[is_idle_task(t)],
1112 		 "N."[cpu < 0 || !tick_nohz_full_cpu(cpu)],
1113 		 t->rcu_tasks_nvcsw, t->nvcsw, t->rcu_tasks_holdout,
1114 		 data_race(t->rcu_tasks_idle_cpu), cpu);
1115 	sched_show_task(t);
1116 }
1117 
1118 /* Scan the holdout lists for tasks no longer holding out. */
1119 static void check_all_holdout_tasks(struct list_head *hop,
1120 				    bool needreport, bool *firstreport)
1121 {
1122 	struct task_struct *t, *t1;
1123 
1124 	list_for_each_entry_safe(t, t1, hop, rcu_tasks_holdout_list) {
1125 		check_holdout_task(t, needreport, firstreport);
1126 		cond_resched();
1127 	}
1128 }
1129 
1130 /* Finish off the Tasks-RCU grace period. */
1131 static void rcu_tasks_postgp(struct rcu_tasks *rtp)
1132 {
1133 	/*
1134 	 * Because ->on_rq and ->nvcsw are not guaranteed to have a full
1135 	 * memory barriers prior to them in the schedule() path, memory
1136 	 * reordering on other CPUs could cause their RCU-tasks read-side
1137 	 * critical sections to extend past the end of the grace period.
1138 	 * However, because these ->nvcsw updates are carried out with
1139 	 * interrupts disabled, we can use synchronize_rcu() to force the
1140 	 * needed ordering on all such CPUs.
1141 	 *
1142 	 * This synchronize_rcu() also confines all ->rcu_tasks_holdout
1143 	 * accesses to be within the grace period, avoiding the need for
1144 	 * memory barriers for ->rcu_tasks_holdout accesses.
1145 	 *
1146 	 * In addition, this synchronize_rcu() waits for exiting tasks
1147 	 * to complete their final preempt_disable() region of execution,
1148 	 * enforcing the whole region before tasklist removal until
1149 	 * the final schedule() with TASK_DEAD state to be an RCU TASKS
1150 	 * read side critical section.
1151 	 */
1152 	synchronize_rcu();
1153 }
1154 
1155 static void tasks_rcu_exit_srcu_stall(struct timer_list *unused)
1156 {
1157 #ifndef CONFIG_TINY_RCU
1158 	int rtsi;
1159 
1160 	rtsi = READ_ONCE(rcu_task_stall_info);
1161 	pr_info("%s: %s grace period number %lu (since boot) gp_state: %s is %lu jiffies old.\n",
1162 		__func__, rcu_tasks.kname, rcu_tasks.tasks_gp_seq,
1163 		tasks_gp_state_getname(&rcu_tasks), jiffies - rcu_tasks.gp_jiffies);
1164 	pr_info("Please check any exiting tasks stuck between calls to exit_tasks_rcu_start() and exit_tasks_rcu_finish()\n");
1165 	tasks_rcu_exit_srcu_stall_timer.expires = jiffies + rtsi;
1166 	add_timer(&tasks_rcu_exit_srcu_stall_timer);
1167 #endif // #ifndef CONFIG_TINY_RCU
1168 }
1169 
1170 /**
1171  * call_rcu_tasks() - Queue an RCU for invocation task-based grace period
1172  * @rhp: structure to be used for queueing the RCU updates.
1173  * @func: actual callback function to be invoked after the grace period
1174  *
1175  * The callback function will be invoked some time after a full grace
1176  * period elapses, in other words after all currently executing RCU
1177  * read-side critical sections have completed. call_rcu_tasks() assumes
1178  * that the read-side critical sections end at a voluntary context
1179  * switch (not a preemption!), cond_resched_tasks_rcu_qs(), entry into idle,
1180  * or transition to usermode execution.  As such, there are no read-side
1181  * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
1182  * this primitive is intended to determine that all tasks have passed
1183  * through a safe state, not so much for data-structure synchronization.
1184  *
1185  * See the description of call_rcu() for more detailed information on
1186  * memory ordering guarantees.
1187  */
1188 void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func)
1189 {
1190 	call_rcu_tasks_generic(rhp, func, &rcu_tasks);
1191 }
1192 EXPORT_SYMBOL_GPL(call_rcu_tasks);
1193 
1194 /**
1195  * synchronize_rcu_tasks - wait until an rcu-tasks grace period has elapsed.
1196  *
1197  * Control will return to the caller some time after a full rcu-tasks
1198  * grace period has elapsed, in other words after all currently
1199  * executing rcu-tasks read-side critical sections have elapsed.  These
1200  * read-side critical sections are delimited by calls to schedule(),
1201  * cond_resched_tasks_rcu_qs(), idle execution, userspace execution, calls
1202  * to synchronize_rcu_tasks(), and (in theory, anyway) cond_resched().
1203  *
1204  * This is a very specialized primitive, intended only for a few uses in
1205  * tracing and other situations requiring manipulation of function
1206  * preambles and profiling hooks.  The synchronize_rcu_tasks() function
1207  * is not (yet) intended for heavy use from multiple CPUs.
1208  *
1209  * See the description of synchronize_rcu() for more detailed information
1210  * on memory ordering guarantees.
1211  */
1212 void synchronize_rcu_tasks(void)
1213 {
1214 	synchronize_rcu_tasks_generic(&rcu_tasks);
1215 }
1216 EXPORT_SYMBOL_GPL(synchronize_rcu_tasks);
1217 
1218 /**
1219  * rcu_barrier_tasks - Wait for in-flight call_rcu_tasks() callbacks.
1220  *
1221  * Although the current implementation is guaranteed to wait, it is not
1222  * obligated to, for example, if there are no pending callbacks.
1223  */
1224 void rcu_barrier_tasks(void)
1225 {
1226 	rcu_barrier_tasks_generic(&rcu_tasks);
1227 }
1228 EXPORT_SYMBOL_GPL(rcu_barrier_tasks);
1229 
1230 static int rcu_tasks_lazy_ms = -1;
1231 module_param(rcu_tasks_lazy_ms, int, 0444);
1232 
1233 static int __init rcu_spawn_tasks_kthread(void)
1234 {
1235 	rcu_tasks.gp_sleep = HZ / 10;
1236 	rcu_tasks.init_fract = HZ / 10;
1237 	if (rcu_tasks_lazy_ms >= 0)
1238 		rcu_tasks.lazy_jiffies = msecs_to_jiffies(rcu_tasks_lazy_ms);
1239 	rcu_tasks.pregp_func = rcu_tasks_pregp_step;
1240 	rcu_tasks.pertask_func = rcu_tasks_pertask;
1241 	rcu_tasks.postscan_func = rcu_tasks_postscan;
1242 	rcu_tasks.holdouts_func = check_all_holdout_tasks;
1243 	rcu_tasks.postgp_func = rcu_tasks_postgp;
1244 	rcu_tasks.wait_state = TASK_IDLE;
1245 	rcu_spawn_tasks_kthread_generic(&rcu_tasks);
1246 	return 0;
1247 }
1248 
1249 #if !defined(CONFIG_TINY_RCU)
1250 void show_rcu_tasks_classic_gp_kthread(void)
1251 {
1252 	show_rcu_tasks_generic_gp_kthread(&rcu_tasks, "");
1253 }
1254 EXPORT_SYMBOL_GPL(show_rcu_tasks_classic_gp_kthread);
1255 
1256 void rcu_tasks_torture_stats_print(char *tt, char *tf)
1257 {
1258 	rcu_tasks_torture_stats_print_generic(&rcu_tasks, tt, tf, "");
1259 }
1260 EXPORT_SYMBOL_GPL(rcu_tasks_torture_stats_print);
1261 #endif // !defined(CONFIG_TINY_RCU)
1262 
1263 struct task_struct *get_rcu_tasks_gp_kthread(void)
1264 {
1265 	return rcu_tasks.kthread_ptr;
1266 }
1267 EXPORT_SYMBOL_GPL(get_rcu_tasks_gp_kthread);
1268 
1269 void rcu_tasks_get_gp_data(int *flags, unsigned long *gp_seq)
1270 {
1271 	*flags = 0;
1272 	*gp_seq = rcu_seq_current(&rcu_tasks.tasks_gp_seq);
1273 }
1274 EXPORT_SYMBOL_GPL(rcu_tasks_get_gp_data);
1275 
1276 /*
1277  * Protect against tasklist scan blind spot while the task is exiting and
1278  * may be removed from the tasklist.  Do this by adding the task to yet
1279  * another list.
1280  *
1281  * Note that the task will remove itself from this list, so there is no
1282  * need for get_task_struct(), except in the case where rcu_tasks_pertask()
1283  * adds it to the holdout list, in which case rcu_tasks_pertask() supplies
1284  * the needed get_task_struct().
1285  */
1286 void exit_tasks_rcu_start(void)
1287 {
1288 	unsigned long flags;
1289 	struct rcu_tasks_percpu *rtpcp;
1290 	struct task_struct *t = current;
1291 
1292 	WARN_ON_ONCE(!list_empty(&t->rcu_tasks_exit_list));
1293 	preempt_disable();
1294 	rtpcp = this_cpu_ptr(rcu_tasks.rtpcpu);
1295 	t->rcu_tasks_exit_cpu = smp_processor_id();
1296 	raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1297 	WARN_ON_ONCE(!rtpcp->rtp_exit_list.next);
1298 	list_add(&t->rcu_tasks_exit_list, &rtpcp->rtp_exit_list);
1299 	raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1300 	preempt_enable();
1301 }
1302 
1303 /*
1304  * Remove the task from the "yet another list" because do_exit() is now
1305  * non-preemptible, allowing synchronize_rcu() to wait beyond this point.
1306  */
1307 void exit_tasks_rcu_finish(void)
1308 {
1309 	unsigned long flags;
1310 	struct rcu_tasks_percpu *rtpcp;
1311 	struct task_struct *t = current;
1312 
1313 	WARN_ON_ONCE(list_empty(&t->rcu_tasks_exit_list));
1314 	rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, t->rcu_tasks_exit_cpu);
1315 	raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
1316 	list_del_init(&t->rcu_tasks_exit_list);
1317 	raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
1318 }
1319 
1320 #else /* #ifdef CONFIG_TASKS_RCU */
1321 void exit_tasks_rcu_start(void) { }
1322 void exit_tasks_rcu_finish(void) { }
1323 #endif /* #else #ifdef CONFIG_TASKS_RCU */
1324 
1325 #ifdef CONFIG_TASKS_RUDE_RCU
1326 
1327 ////////////////////////////////////////////////////////////////////////
1328 //
1329 // "Rude" variant of Tasks RCU, inspired by Steve Rostedt's
1330 // trick of passing an empty function to schedule_on_each_cpu().
1331 // This approach provides batching of concurrent calls to the synchronous
1332 // synchronize_rcu_tasks_rude() API.  This invokes schedule_on_each_cpu()
1333 // in order to send IPIs far and wide and induces otherwise unnecessary
1334 // context switches on all online CPUs, whether idle or not.
1335 //
1336 // Callback handling is provided by the rcu_tasks_kthread() function.
1337 //
1338 // Ordering is provided by the scheduler's context-switch code.
1339 
1340 // Empty function to allow workqueues to force a context switch.
1341 static void rcu_tasks_be_rude(struct work_struct *work)
1342 {
1343 }
1344 
1345 // Wait for one rude RCU-tasks grace period.
1346 static void rcu_tasks_rude_wait_gp(struct rcu_tasks *rtp)
1347 {
1348 	rtp->n_ipis += cpumask_weight(cpu_online_mask);
1349 	schedule_on_each_cpu(rcu_tasks_be_rude);
1350 }
1351 
1352 static void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func);
1353 DEFINE_RCU_TASKS(rcu_tasks_rude, rcu_tasks_rude_wait_gp, call_rcu_tasks_rude,
1354 		 "RCU Tasks Rude");
1355 
1356 /*
1357  * call_rcu_tasks_rude() - Queue a callback rude task-based grace period
1358  * @rhp: structure to be used for queueing the RCU updates.
1359  * @func: actual callback function to be invoked after the grace period
1360  *
1361  * The callback function will be invoked some time after a full grace
1362  * period elapses, in other words after all currently executing RCU
1363  * read-side critical sections have completed. call_rcu_tasks_rude()
1364  * assumes that the read-side critical sections end at context switch,
1365  * cond_resched_tasks_rcu_qs(), or transition to usermode execution (as
1366  * usermode execution is schedulable). As such, there are no read-side
1367  * primitives analogous to rcu_read_lock() and rcu_read_unlock() because
1368  * this primitive is intended to determine that all tasks have passed
1369  * through a safe state, not so much for data-structure synchronization.
1370  *
1371  * See the description of call_rcu() for more detailed information on
1372  * memory ordering guarantees.
1373  *
1374  * This is no longer exported, and is instead reserved for use by
1375  * synchronize_rcu_tasks_rude().
1376  */
1377 static void call_rcu_tasks_rude(struct rcu_head *rhp, rcu_callback_t func)
1378 {
1379 	call_rcu_tasks_generic(rhp, func, &rcu_tasks_rude);
1380 }
1381 
1382 /**
1383  * synchronize_rcu_tasks_rude - wait for a rude rcu-tasks grace period
1384  *
1385  * Control will return to the caller some time after a rude rcu-tasks
1386  * grace period has elapsed, in other words after all currently
1387  * executing rcu-tasks read-side critical sections have elapsed.  These
1388  * read-side critical sections are delimited by calls to schedule(),
1389  * cond_resched_tasks_rcu_qs(), userspace execution (which is a schedulable
1390  * context), and (in theory, anyway) cond_resched().
1391  *
1392  * This is a very specialized primitive, intended only for a few uses in
1393  * tracing and other situations requiring manipulation of function preambles
1394  * and profiling hooks.  The synchronize_rcu_tasks_rude() function is not
1395  * (yet) intended for heavy use from multiple CPUs.
1396  *
1397  * See the description of synchronize_rcu() for more detailed information
1398  * on memory ordering guarantees.
1399  */
1400 void synchronize_rcu_tasks_rude(void)
1401 {
1402 	if (!IS_ENABLED(CONFIG_ARCH_WANTS_NO_INSTR) || IS_ENABLED(CONFIG_FORCE_TASKS_RUDE_RCU))
1403 		synchronize_rcu_tasks_generic(&rcu_tasks_rude);
1404 }
1405 EXPORT_SYMBOL_GPL(synchronize_rcu_tasks_rude);
1406 
1407 static int __init rcu_spawn_tasks_rude_kthread(void)
1408 {
1409 	rcu_tasks_rude.gp_sleep = HZ / 10;
1410 	rcu_spawn_tasks_kthread_generic(&rcu_tasks_rude);
1411 	return 0;
1412 }
1413 
1414 #if !defined(CONFIG_TINY_RCU)
1415 void show_rcu_tasks_rude_gp_kthread(void)
1416 {
1417 	show_rcu_tasks_generic_gp_kthread(&rcu_tasks_rude, "");
1418 }
1419 EXPORT_SYMBOL_GPL(show_rcu_tasks_rude_gp_kthread);
1420 
1421 void rcu_tasks_rude_torture_stats_print(char *tt, char *tf)
1422 {
1423 	rcu_tasks_torture_stats_print_generic(&rcu_tasks_rude, tt, tf, "");
1424 }
1425 EXPORT_SYMBOL_GPL(rcu_tasks_rude_torture_stats_print);
1426 #endif // !defined(CONFIG_TINY_RCU)
1427 
1428 struct task_struct *get_rcu_tasks_rude_gp_kthread(void)
1429 {
1430 	return rcu_tasks_rude.kthread_ptr;
1431 }
1432 EXPORT_SYMBOL_GPL(get_rcu_tasks_rude_gp_kthread);
1433 
1434 void rcu_tasks_rude_get_gp_data(int *flags, unsigned long *gp_seq)
1435 {
1436 	*flags = 0;
1437 	*gp_seq = rcu_seq_current(&rcu_tasks_rude.tasks_gp_seq);
1438 }
1439 EXPORT_SYMBOL_GPL(rcu_tasks_rude_get_gp_data);
1440 
1441 #endif /* #ifdef CONFIG_TASKS_RUDE_RCU */
1442 
1443 #ifndef CONFIG_TINY_RCU
1444 void show_rcu_tasks_gp_kthreads(void)
1445 {
1446 	show_rcu_tasks_classic_gp_kthread();
1447 	show_rcu_tasks_rude_gp_kthread();
1448 }
1449 #endif /* #ifndef CONFIG_TINY_RCU */
1450 
1451 #ifdef CONFIG_PROVE_RCU
1452 struct rcu_tasks_test_desc {
1453 	struct rcu_head rh;
1454 	const char *name;
1455 	bool notrun;
1456 	unsigned long runstart;
1457 };
1458 
1459 static struct rcu_tasks_test_desc tests[] = {
1460 	{
1461 		.name = "call_rcu_tasks()",
1462 		/* If not defined, the test is skipped. */
1463 		.notrun = IS_ENABLED(CONFIG_TASKS_RCU),
1464 	},
1465 	{
1466 		.name = "call_rcu_tasks_trace()",
1467 		/* If not defined, the test is skipped. */
1468 		.notrun = IS_ENABLED(CONFIG_TASKS_TRACE_RCU)
1469 	}
1470 };
1471 
1472 #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
1473 static void test_rcu_tasks_callback(struct rcu_head *rhp)
1474 {
1475 	struct rcu_tasks_test_desc *rttd =
1476 		container_of(rhp, struct rcu_tasks_test_desc, rh);
1477 
1478 	pr_info("Callback from %s invoked.\n", rttd->name);
1479 
1480 	rttd->notrun = false;
1481 }
1482 #endif // #if defined(CONFIG_TASKS_RCU) || defined(CONFIG_TASKS_TRACE_RCU)
1483 
1484 static void rcu_tasks_initiate_self_tests(void)
1485 {
1486 #ifdef CONFIG_TASKS_RCU
1487 	pr_info("Running RCU Tasks wait API self tests\n");
1488 	tests[0].runstart = jiffies;
1489 	synchronize_rcu_tasks();
1490 	call_rcu_tasks(&tests[0].rh, test_rcu_tasks_callback);
1491 #endif
1492 
1493 #ifdef CONFIG_TASKS_RUDE_RCU
1494 	pr_info("Running RCU Tasks Rude wait API self tests\n");
1495 	synchronize_rcu_tasks_rude();
1496 #endif
1497 
1498 #ifdef CONFIG_TASKS_TRACE_RCU
1499 	pr_info("Running RCU Tasks Trace wait API self tests\n");
1500 	tests[1].runstart = jiffies;
1501 	synchronize_rcu_tasks_trace();
1502 	call_rcu_tasks_trace(&tests[1].rh, test_rcu_tasks_callback);
1503 #endif
1504 }
1505 
1506 /*
1507  * Return:  0 - test passed
1508  *	    1 - test failed, but have not timed out yet
1509  *	   -1 - test failed and timed out
1510  */
1511 static int rcu_tasks_verify_self_tests(void)
1512 {
1513 	int ret = 0;
1514 	int i;
1515 	unsigned long bst = rcu_task_stall_timeout;
1516 
1517 	if (bst <= 0 || bst > RCU_TASK_BOOT_STALL_TIMEOUT)
1518 		bst = RCU_TASK_BOOT_STALL_TIMEOUT;
1519 	for (i = 0; i < ARRAY_SIZE(tests); i++) {
1520 		while (tests[i].notrun) {		// still hanging.
1521 			if (time_after(jiffies, tests[i].runstart + bst)) {
1522 				pr_err("%s has failed boot-time tests.\n", tests[i].name);
1523 				ret = -1;
1524 				break;
1525 			}
1526 			ret = 1;
1527 			break;
1528 		}
1529 	}
1530 	WARN_ON(ret < 0);
1531 
1532 	return ret;
1533 }
1534 
1535 /*
1536  * Repeat the rcu_tasks_verify_self_tests() call once every second until the
1537  * test passes or has timed out.
1538  */
1539 static struct delayed_work rcu_tasks_verify_work;
1540 static void rcu_tasks_verify_work_fn(struct work_struct *work __maybe_unused)
1541 {
1542 	int ret = rcu_tasks_verify_self_tests();
1543 
1544 	if (ret <= 0)
1545 		return;
1546 
1547 	/* Test fails but not timed out yet, reschedule another check */
1548 	schedule_delayed_work(&rcu_tasks_verify_work, HZ);
1549 }
1550 
1551 static int rcu_tasks_verify_schedule_work(void)
1552 {
1553 	INIT_DELAYED_WORK(&rcu_tasks_verify_work, rcu_tasks_verify_work_fn);
1554 	rcu_tasks_verify_work_fn(NULL);
1555 	return 0;
1556 }
1557 late_initcall(rcu_tasks_verify_schedule_work);
1558 #else /* #ifdef CONFIG_PROVE_RCU */
1559 static void rcu_tasks_initiate_self_tests(void) { }
1560 #endif /* #else #ifdef CONFIG_PROVE_RCU */
1561 
1562 void __init tasks_cblist_init_generic(void)
1563 {
1564 	lockdep_assert_irqs_disabled();
1565 	WARN_ON(num_online_cpus() > 1);
1566 
1567 #ifdef CONFIG_TASKS_RCU
1568 	cblist_init_generic(&rcu_tasks);
1569 #endif
1570 
1571 #ifdef CONFIG_TASKS_RUDE_RCU
1572 	cblist_init_generic(&rcu_tasks_rude);
1573 #endif
1574 }
1575 
1576 static int __init rcu_init_tasks_generic(void)
1577 {
1578 #ifdef CONFIG_TASKS_RCU
1579 	rcu_spawn_tasks_kthread();
1580 #endif
1581 
1582 #ifdef CONFIG_TASKS_RUDE_RCU
1583 	rcu_spawn_tasks_rude_kthread();
1584 #endif
1585 
1586 	// Run the self-tests.
1587 	rcu_tasks_initiate_self_tests();
1588 
1589 	return 0;
1590 }
1591 core_initcall(rcu_init_tasks_generic);
1592 
1593 #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
1594 static inline void rcu_tasks_bootup_oddness(void) {}
1595 #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
1596 
1597 #ifdef CONFIG_TASKS_TRACE_RCU
1598 
1599 ////////////////////////////////////////////////////////////////////////
1600 //
1601 // Tracing variant of Tasks RCU.  This variant is designed to be used
1602 // to protect tracing hooks, including those of BPF.  This variant
1603 // is implemented via a straightforward mapping onto SRCU-fast.
1604 
1605 DEFINE_SRCU_FAST(rcu_tasks_trace_srcu_struct);
1606 EXPORT_SYMBOL_GPL(rcu_tasks_trace_srcu_struct);
1607 
1608 #endif /* #else #ifdef CONFIG_TASKS_TRACE_RCU */
1609