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