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