xref: /linux/kernel/rcu/tree_plugin.h (revision 12d5fc96ebbb3b948861805b32a4c3a96105ccd9)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Read-Copy Update mechanism for mutual exclusion (tree-based version)
4  * Internal non-public definitions that provide either classic
5  * or preemptible semantics.
6  *
7  * Copyright Red Hat, 2009
8  * Copyright IBM Corporation, 2009
9  *
10  * Author: Ingo Molnar <mingo@elte.hu>
11  *	   Paul E. McKenney <paulmck@linux.ibm.com>
12  */
13 
14 static bool rcu_rdp_is_offloaded(struct rcu_data *rdp)
15 {
16 	/*
17 	 * In order to read the offloaded state of an rdp in a safe
18 	 * and stable way and prevent from its value to be changed
19 	 * under us, we must either hold the barrier mutex, the cpu
20 	 * hotplug lock (read or write) or the nocb lock. Local
21 	 * non-preemptible reads are also safe. NOCB kthreads and
22 	 * timers have their own means of synchronization against the
23 	 * offloaded state updaters.
24 	 */
25 	RCU_NOCB_LOCKDEP_WARN(
26 		!(lockdep_is_held(&rcu_state.barrier_mutex) ||
27 		  (IS_ENABLED(CONFIG_HOTPLUG_CPU) && lockdep_is_cpus_held()) ||
28 		  lockdep_is_held(&rdp->nocb_lock) ||
29 		  lockdep_is_held(&rcu_state.nocb_mutex) ||
30 		  ((!(IS_ENABLED(CONFIG_PREEMPT_COUNT) && preemptible()) || softirq_count()) &&
31 		   rdp == this_cpu_ptr(&rcu_data)) ||
32 		  rcu_current_is_nocb_kthread(rdp)),
33 		"Unsafe read of RCU_NOCB offloaded state"
34 	);
35 
36 	return rcu_segcblist_is_offloaded(&rdp->cblist);
37 }
38 
39 /*
40  * Check the RCU kernel configuration parameters and print informative
41  * messages about anything out of the ordinary.
42  */
43 static void __init rcu_bootup_announce_oddness(void)
44 {
45 	if (IS_ENABLED(CONFIG_RCU_TRACE))
46 		pr_info("\tRCU event tracing is enabled.\n");
47 	if ((IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 64) ||
48 	    (!IS_ENABLED(CONFIG_64BIT) && RCU_FANOUT != 32))
49 		pr_info("\tCONFIG_RCU_FANOUT set to non-default value of %d.\n",
50 			RCU_FANOUT);
51 	if (rcu_fanout_exact)
52 		pr_info("\tHierarchical RCU autobalancing is disabled.\n");
53 	if (IS_ENABLED(CONFIG_PROVE_RCU))
54 		pr_info("\tRCU lockdep checking is enabled.\n");
55 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD))
56 		pr_info("\tRCU strict (and thus non-scalable) grace periods are enabled.\n");
57 	if (RCU_NUM_LVLS >= 4)
58 		pr_info("\tFour(or more)-level hierarchy is enabled.\n");
59 	if (RCU_FANOUT_LEAF != 16)
60 		pr_info("\tBuild-time adjustment of leaf fanout to %d.\n",
61 			RCU_FANOUT_LEAF);
62 	if (rcu_fanout_leaf != RCU_FANOUT_LEAF)
63 		pr_info("\tBoot-time adjustment of leaf fanout to %d.\n",
64 			rcu_fanout_leaf);
65 	if (nr_cpu_ids != NR_CPUS)
66 		pr_info("\tRCU restricting CPUs from NR_CPUS=%d to nr_cpu_ids=%u.\n", NR_CPUS, nr_cpu_ids);
67 #ifdef CONFIG_RCU_BOOST
68 	pr_info("\tRCU priority boosting: priority %d delay %d ms.\n",
69 		kthread_prio, CONFIG_RCU_BOOST_DELAY);
70 #endif
71 	if (blimit != DEFAULT_RCU_BLIMIT)
72 		pr_info("\tBoot-time adjustment of callback invocation limit to %ld.\n", blimit);
73 	if (qhimark != DEFAULT_RCU_QHIMARK)
74 		pr_info("\tBoot-time adjustment of callback high-water mark to %ld.\n", qhimark);
75 	if (qlowmark != DEFAULT_RCU_QLOMARK)
76 		pr_info("\tBoot-time adjustment of callback low-water mark to %ld.\n", qlowmark);
77 	if (qovld != DEFAULT_RCU_QOVLD)
78 		pr_info("\tBoot-time adjustment of callback overload level to %ld.\n", qovld);
79 	if (jiffies_till_first_fqs != ULONG_MAX)
80 		pr_info("\tBoot-time adjustment of first FQS scan delay to %ld jiffies.\n", jiffies_till_first_fqs);
81 	if (jiffies_till_next_fqs != ULONG_MAX)
82 		pr_info("\tBoot-time adjustment of subsequent FQS scan delay to %ld jiffies.\n", jiffies_till_next_fqs);
83 	if (jiffies_till_sched_qs != ULONG_MAX)
84 		pr_info("\tBoot-time adjustment of scheduler-enlistment delay to %ld jiffies.\n", jiffies_till_sched_qs);
85 	if (rcu_kick_kthreads)
86 		pr_info("\tKick kthreads if too-long grace period.\n");
87 	if (IS_ENABLED(CONFIG_DEBUG_OBJECTS_RCU_HEAD))
88 		pr_info("\tRCU callback double-/use-after-free debug is enabled.\n");
89 	if (gp_preinit_delay)
90 		pr_info("\tRCU debug GP pre-init slowdown %d jiffies.\n", gp_preinit_delay);
91 	if (gp_init_delay)
92 		pr_info("\tRCU debug GP init slowdown %d jiffies.\n", gp_init_delay);
93 	if (gp_cleanup_delay)
94 		pr_info("\tRCU debug GP cleanup slowdown %d jiffies.\n", gp_cleanup_delay);
95 	if (nohz_full_patience_delay < 0) {
96 		pr_info("\tRCU NOCB CPU patience negative (%d), resetting to zero.\n", nohz_full_patience_delay);
97 		nohz_full_patience_delay = 0;
98 	} else if (nohz_full_patience_delay > 5 * MSEC_PER_SEC) {
99 		pr_info("\tRCU NOCB CPU patience too large (%d), resetting to %ld.\n", nohz_full_patience_delay, 5 * MSEC_PER_SEC);
100 		nohz_full_patience_delay = 5 * MSEC_PER_SEC;
101 	} else if (nohz_full_patience_delay) {
102 		pr_info("\tRCU NOCB CPU patience set to %d milliseconds.\n", nohz_full_patience_delay);
103 	}
104 	nohz_full_patience_delay_jiffies = msecs_to_jiffies(nohz_full_patience_delay);
105 	if (!use_softirq)
106 		pr_info("\tRCU_SOFTIRQ processing moved to rcuc kthreads.\n");
107 	if (IS_ENABLED(CONFIG_RCU_EQS_DEBUG))
108 		pr_info("\tRCU debug extended QS entry/exit.\n");
109 	rcupdate_announce_bootup_oddness();
110 }
111 
112 #ifdef CONFIG_PREEMPT_RCU
113 
114 static void rcu_report_exp_rnp(struct rcu_node *rnp, bool wake);
115 static void rcu_read_unlock_special(struct task_struct *t);
116 
117 /*
118  * Tell them what RCU they are running.
119  */
120 static void __init rcu_bootup_announce(void)
121 {
122 	pr_info("Preemptible hierarchical RCU implementation.\n");
123 	rcu_bootup_announce_oddness();
124 }
125 
126 /* Flags for rcu_preempt_ctxt_queue() decision table. */
127 #define RCU_GP_TASKS	0x8
128 #define RCU_EXP_TASKS	0x4
129 #define RCU_GP_BLKD	0x2
130 #define RCU_EXP_BLKD	0x1
131 
132 /*
133  * Queues a task preempted within an RCU-preempt read-side critical
134  * section into the appropriate location within the ->blkd_tasks list,
135  * depending on the states of any ongoing normal and expedited grace
136  * periods.  The ->gp_tasks pointer indicates which element the normal
137  * grace period is waiting on (NULL if none), and the ->exp_tasks pointer
138  * indicates which element the expedited grace period is waiting on (again,
139  * NULL if none).  If a grace period is waiting on a given element in the
140  * ->blkd_tasks list, it also waits on all subsequent elements.  Thus,
141  * adding a task to the tail of the list blocks any grace period that is
142  * already waiting on one of the elements.  In contrast, adding a task
143  * to the head of the list won't block any grace period that is already
144  * waiting on one of the elements.
145  *
146  * This queuing is imprecise, and can sometimes make an ongoing grace
147  * period wait for a task that is not strictly speaking blocking it.
148  * Given the choice, we needlessly block a normal grace period rather than
149  * blocking an expedited grace period.
150  *
151  * Note that an endless sequence of expedited grace periods still cannot
152  * indefinitely postpone a normal grace period.  Eventually, all of the
153  * fixed number of preempted tasks blocking the normal grace period that are
154  * not also blocking the expedited grace period will resume and complete
155  * their RCU read-side critical sections.  At that point, the ->gp_tasks
156  * pointer will equal the ->exp_tasks pointer, at which point the end of
157  * the corresponding expedited grace period will also be the end of the
158  * normal grace period.
159  */
160 static void rcu_preempt_ctxt_queue(struct rcu_node *rnp, struct rcu_data *rdp)
161 	__releases(rnp->lock) /* But leaves rrupts disabled. */
162 {
163 	int blkd_state = (rnp->gp_tasks ? RCU_GP_TASKS : 0) +
164 			 (rnp->exp_tasks ? RCU_EXP_TASKS : 0) +
165 			 (rnp->qsmask & rdp->grpmask ? RCU_GP_BLKD : 0) +
166 			 (rnp->expmask & rdp->grpmask ? RCU_EXP_BLKD : 0);
167 	struct task_struct *t = current;
168 
169 	raw_lockdep_assert_held_rcu_node(rnp);
170 	WARN_ON_ONCE(rdp->mynode != rnp);
171 	WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
172 	/* RCU better not be waiting on newly onlined CPUs! */
173 	WARN_ON_ONCE(rnp->qsmaskinitnext & ~rnp->qsmaskinit & rnp->qsmask &
174 		     rdp->grpmask);
175 
176 	/*
177 	 * Decide where to queue the newly blocked task.  In theory,
178 	 * this could be an if-statement.  In practice, when I tried
179 	 * that, it was quite messy.
180 	 */
181 	switch (blkd_state) {
182 	case 0:
183 	case                RCU_EXP_TASKS:
184 	case                RCU_EXP_TASKS | RCU_GP_BLKD:
185 	case RCU_GP_TASKS:
186 	case RCU_GP_TASKS | RCU_EXP_TASKS:
187 
188 		/*
189 		 * Blocking neither GP, or first task blocking the normal
190 		 * GP but not blocking the already-waiting expedited GP.
191 		 * Queue at the head of the list to avoid unnecessarily
192 		 * blocking the already-waiting GPs.
193 		 */
194 		list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
195 		break;
196 
197 	case                                              RCU_EXP_BLKD:
198 	case                                RCU_GP_BLKD:
199 	case                                RCU_GP_BLKD | RCU_EXP_BLKD:
200 	case RCU_GP_TASKS |                               RCU_EXP_BLKD:
201 	case RCU_GP_TASKS |                 RCU_GP_BLKD | RCU_EXP_BLKD:
202 	case RCU_GP_TASKS | RCU_EXP_TASKS | RCU_GP_BLKD | RCU_EXP_BLKD:
203 
204 		/*
205 		 * First task arriving that blocks either GP, or first task
206 		 * arriving that blocks the expedited GP (with the normal
207 		 * GP already waiting), or a task arriving that blocks
208 		 * both GPs with both GPs already waiting.  Queue at the
209 		 * tail of the list to avoid any GP waiting on any of the
210 		 * already queued tasks that are not blocking it.
211 		 */
212 		list_add_tail(&t->rcu_node_entry, &rnp->blkd_tasks);
213 		break;
214 
215 	case                RCU_EXP_TASKS |               RCU_EXP_BLKD:
216 	case                RCU_EXP_TASKS | RCU_GP_BLKD | RCU_EXP_BLKD:
217 	case RCU_GP_TASKS | RCU_EXP_TASKS |               RCU_EXP_BLKD:
218 
219 		/*
220 		 * Second or subsequent task blocking the expedited GP.
221 		 * The task either does not block the normal GP, or is the
222 		 * first task blocking the normal GP.  Queue just after
223 		 * the first task blocking the expedited GP.
224 		 */
225 		list_add(&t->rcu_node_entry, rnp->exp_tasks);
226 		break;
227 
228 	case RCU_GP_TASKS |                 RCU_GP_BLKD:
229 	case RCU_GP_TASKS | RCU_EXP_TASKS | RCU_GP_BLKD:
230 
231 		/*
232 		 * Second or subsequent task blocking the normal GP.
233 		 * The task does not block the expedited GP. Queue just
234 		 * after the first task blocking the normal GP.
235 		 */
236 		list_add(&t->rcu_node_entry, rnp->gp_tasks);
237 		break;
238 
239 	default:
240 
241 		/* Yet another exercise in excessive paranoia. */
242 		WARN_ON_ONCE(1);
243 		break;
244 	}
245 
246 	/*
247 	 * We have now queued the task.  If it was the first one to
248 	 * block either grace period, update the ->gp_tasks and/or
249 	 * ->exp_tasks pointers, respectively, to reference the newly
250 	 * blocked tasks.
251 	 */
252 	if (!rnp->gp_tasks && (blkd_state & RCU_GP_BLKD)) {
253 		WRITE_ONCE(rnp->gp_tasks, &t->rcu_node_entry);
254 		WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq);
255 	}
256 	if (!rnp->exp_tasks && (blkd_state & RCU_EXP_BLKD))
257 		WRITE_ONCE(rnp->exp_tasks, &t->rcu_node_entry);
258 	WARN_ON_ONCE(!(blkd_state & RCU_GP_BLKD) !=
259 		     !(rnp->qsmask & rdp->grpmask));
260 	WARN_ON_ONCE(!(blkd_state & RCU_EXP_BLKD) !=
261 		     !(rnp->expmask & rdp->grpmask));
262 	raw_spin_unlock_rcu_node(rnp); /* interrupts remain disabled. */
263 
264 	/*
265 	 * Report the quiescent state for the expedited GP.  This expedited
266 	 * GP should not be able to end until we report, so there should be
267 	 * no need to check for a subsequent expedited GP.  (Though we are
268 	 * still in a quiescent state in any case.)
269 	 *
270 	 * Interrupts are disabled, so ->cpu_no_qs.b.exp cannot change.
271 	 */
272 	if (blkd_state & RCU_EXP_BLKD && rdp->cpu_no_qs.b.exp)
273 		rcu_report_exp_rdp(rdp);
274 	else
275 		WARN_ON_ONCE(rdp->cpu_no_qs.b.exp);
276 	ASSERT_EXCLUSIVE_WRITER_SCOPED(rdp->cpu_no_qs.b.exp);
277 }
278 
279 /*
280  * Record a preemptible-RCU quiescent state for the specified CPU.
281  * Note that this does not necessarily mean that the task currently running
282  * on the CPU is in a quiescent state:  Instead, it means that the current
283  * grace period need not wait on any RCU read-side critical section that
284  * starts later on this CPU.  It also means that if the current task is
285  * in an RCU read-side critical section, it has already added itself to
286  * some leaf rcu_node structure's ->blkd_tasks list.  In addition to the
287  * current task, there might be any number of other tasks blocked while
288  * in an RCU read-side critical section.
289  *
290  * Unlike non-preemptible-RCU, quiescent state reports for expedited
291  * grace periods are handled separately via deferred quiescent states
292  * and context switch events.
293  *
294  * Callers to this function must disable preemption.
295  */
296 static void rcu_qs(void)
297 {
298 	RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!\n");
299 	if (this_cpu_read(rcu_data.cpu_no_qs.b.norm)) {
300 		trace_rcu_grace_period(TPS("rcu_preempt"),
301 				       __this_cpu_read(rcu_data.gp_seq),
302 				       TPS("cpuqs"));
303 		this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
304 		barrier(); /* Coordinate with rcu_flavor_sched_clock_irq(). */
305 		WRITE_ONCE(current->rcu_read_unlock_special.b.need_qs, false);
306 	}
307 }
308 
309 /*
310  * We have entered the scheduler, and the current task might soon be
311  * context-switched away from.  If this task is in an RCU read-side
312  * critical section, we will no longer be able to rely on the CPU to
313  * record that fact, so we enqueue the task on the blkd_tasks list.
314  * The task will dequeue itself when it exits the outermost enclosing
315  * RCU read-side critical section.  Therefore, the current grace period
316  * cannot be permitted to complete until the blkd_tasks list entries
317  * predating the current grace period drain, in other words, until
318  * rnp->gp_tasks becomes NULL.
319  *
320  * Caller must disable interrupts.
321  */
322 void rcu_note_context_switch(bool preempt)
323 {
324 	struct task_struct *t = current;
325 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
326 	struct rcu_node *rnp;
327 
328 	trace_rcu_utilization(TPS("Start context switch"));
329 	lockdep_assert_irqs_disabled();
330 	WARN_ONCE(!preempt && rcu_preempt_depth() > 0, "Voluntary context switch within RCU read-side critical section!");
331 	if (rcu_preempt_depth() > 0 &&
332 	    !t->rcu_read_unlock_special.b.blocked) {
333 
334 		/* Possibly blocking in an RCU read-side critical section. */
335 		rnp = rdp->mynode;
336 		raw_spin_lock_rcu_node(rnp);
337 		t->rcu_read_unlock_special.b.blocked = true;
338 		t->rcu_blocked_node = rnp;
339 
340 		/*
341 		 * Verify the CPU's sanity, trace the preemption, and
342 		 * then queue the task as required based on the states
343 		 * of any ongoing and expedited grace periods.
344 		 */
345 		WARN_ON_ONCE(!rcu_rdp_cpu_online(rdp));
346 		WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
347 		trace_rcu_preempt_task(rcu_state.name,
348 				       t->pid,
349 				       (rnp->qsmask & rdp->grpmask)
350 				       ? rnp->gp_seq
351 				       : rcu_seq_snap(&rnp->gp_seq));
352 		rcu_preempt_ctxt_queue(rnp, rdp);
353 	} else {
354 		rcu_preempt_deferred_qs(t);
355 	}
356 
357 	/*
358 	 * Either we were not in an RCU read-side critical section to
359 	 * begin with, or we have now recorded that critical section
360 	 * globally.  Either way, we can now note a quiescent state
361 	 * for this CPU.  Again, if we were in an RCU read-side critical
362 	 * section, and if that critical section was blocking the current
363 	 * grace period, then the fact that the task has been enqueued
364 	 * means that we continue to block the current grace period.
365 	 */
366 	rcu_qs();
367 	if (rdp->cpu_no_qs.b.exp)
368 		rcu_report_exp_rdp(rdp);
369 	rcu_tasks_qs(current, preempt);
370 	trace_rcu_utilization(TPS("End context switch"));
371 }
372 EXPORT_SYMBOL_GPL(rcu_note_context_switch);
373 
374 /*
375  * Check for preempted RCU readers blocking the current grace period
376  * for the specified rcu_node structure.  If the caller needs a reliable
377  * answer, it must hold the rcu_node's ->lock.
378  */
379 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
380 {
381 	return READ_ONCE(rnp->gp_tasks) != NULL;
382 }
383 
384 /* limit value for ->rcu_read_lock_nesting. */
385 #define RCU_NEST_PMAX (INT_MAX / 2)
386 
387 static void rcu_preempt_read_enter(void)
388 {
389 	WRITE_ONCE(current->rcu_read_lock_nesting, READ_ONCE(current->rcu_read_lock_nesting) + 1);
390 }
391 
392 static int rcu_preempt_read_exit(void)
393 {
394 	int ret = READ_ONCE(current->rcu_read_lock_nesting) - 1;
395 
396 	WRITE_ONCE(current->rcu_read_lock_nesting, ret);
397 	return ret;
398 }
399 
400 static void rcu_preempt_depth_set(int val)
401 {
402 	WRITE_ONCE(current->rcu_read_lock_nesting, val);
403 }
404 
405 /*
406  * Preemptible RCU implementation for rcu_read_lock().
407  * Just increment ->rcu_read_lock_nesting, shared state will be updated
408  * if we block.
409  */
410 void __rcu_read_lock(void)
411 {
412 	rcu_preempt_read_enter();
413 	if (IS_ENABLED(CONFIG_PROVE_LOCKING))
414 		WARN_ON_ONCE(rcu_preempt_depth() > RCU_NEST_PMAX);
415 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) && rcu_state.gp_kthread)
416 		WRITE_ONCE(current->rcu_read_unlock_special.b.need_qs, true);
417 	barrier();  /* critical section after entry code. */
418 }
419 EXPORT_SYMBOL_GPL(__rcu_read_lock);
420 
421 /*
422  * Preemptible RCU implementation for rcu_read_unlock().
423  * Decrement ->rcu_read_lock_nesting.  If the result is zero (outermost
424  * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
425  * invoke rcu_read_unlock_special() to clean up after a context switch
426  * in an RCU read-side critical section and other special cases.
427  */
428 void __rcu_read_unlock(void)
429 {
430 	struct task_struct *t = current;
431 
432 	barrier();  // critical section before exit code.
433 	if (rcu_preempt_read_exit() == 0) {
434 		barrier();  // critical-section exit before .s check.
435 		if (unlikely(READ_ONCE(t->rcu_read_unlock_special.s)))
436 			rcu_read_unlock_special(t);
437 	}
438 	if (IS_ENABLED(CONFIG_PROVE_LOCKING)) {
439 		int rrln = rcu_preempt_depth();
440 
441 		WARN_ON_ONCE(rrln < 0 || rrln > RCU_NEST_PMAX);
442 	}
443 }
444 EXPORT_SYMBOL_GPL(__rcu_read_unlock);
445 
446 /*
447  * Advance a ->blkd_tasks-list pointer to the next entry, instead
448  * returning NULL if at the end of the list.
449  */
450 static struct list_head *rcu_next_node_entry(struct task_struct *t,
451 					     struct rcu_node *rnp)
452 {
453 	struct list_head *np;
454 
455 	np = t->rcu_node_entry.next;
456 	if (np == &rnp->blkd_tasks)
457 		np = NULL;
458 	return np;
459 }
460 
461 /*
462  * Return true if the specified rcu_node structure has tasks that were
463  * preempted within an RCU read-side critical section.
464  */
465 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
466 {
467 	return !list_empty(&rnp->blkd_tasks);
468 }
469 
470 /*
471  * Report deferred quiescent states.  The deferral time can
472  * be quite short, for example, in the case of the call from
473  * rcu_read_unlock_special().
474  */
475 static notrace void
476 rcu_preempt_deferred_qs_irqrestore(struct task_struct *t, unsigned long flags)
477 {
478 	bool empty_exp;
479 	bool empty_norm;
480 	bool empty_exp_now;
481 	struct list_head *np;
482 	bool drop_boost_mutex = false;
483 	struct rcu_data *rdp;
484 	struct rcu_node *rnp;
485 	union rcu_special special;
486 
487 	rdp = this_cpu_ptr(&rcu_data);
488 	if (rdp->defer_qs_pending == DEFER_QS_PENDING)
489 		rcu_defer_qs_clear(rdp);
490 
491 	/*
492 	 * If RCU core is waiting for this CPU to exit its critical section,
493 	 * report the fact that it has exited.  Because irqs are disabled,
494 	 * t->rcu_read_unlock_special cannot change.
495 	 */
496 	special = t->rcu_read_unlock_special;
497 	if (!special.s && !rdp->cpu_no_qs.b.exp) {
498 		local_irq_restore(flags);
499 		return;
500 	}
501 	t->rcu_read_unlock_special.s = 0;
502 	if (special.b.need_qs) {
503 		if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)) {
504 			rdp->cpu_no_qs.b.norm = false;
505 			rcu_report_qs_rdp(rdp);
506 			udelay(rcu_unlock_delay);
507 		} else {
508 			rcu_qs();
509 		}
510 	}
511 
512 	/*
513 	 * Respond to a request by an expedited grace period for a
514 	 * quiescent state from this CPU.  Note that requests from
515 	 * tasks are handled when removing the task from the
516 	 * blocked-tasks list below.
517 	 */
518 	if (rdp->cpu_no_qs.b.exp)
519 		rcu_report_exp_rdp(rdp);
520 
521 	/* Clean up if blocked during RCU read-side critical section. */
522 	if (special.b.blocked) {
523 
524 		/*
525 		 * Remove this task from the list it blocked on.  The task
526 		 * now remains queued on the rcu_node corresponding to the
527 		 * CPU it first blocked on, so there is no longer any need
528 		 * to loop.  Retain a WARN_ON_ONCE() out of sheer paranoia.
529 		 */
530 		rnp = t->rcu_blocked_node;
531 		raw_spin_lock_rcu_node(rnp); /* irqs already disabled. */
532 		WARN_ON_ONCE(rnp != t->rcu_blocked_node);
533 		WARN_ON_ONCE(!rcu_is_leaf_node(rnp));
534 		empty_norm = !rcu_preempt_blocked_readers_cgp(rnp);
535 		WARN_ON_ONCE(rnp->completedqs == rnp->gp_seq &&
536 			     (!empty_norm || rnp->qsmask));
537 		empty_exp = sync_rcu_exp_done(rnp);
538 		np = rcu_next_node_entry(t, rnp);
539 		list_del_init(&t->rcu_node_entry);
540 		t->rcu_blocked_node = NULL;
541 		trace_rcu_unlock_preempted_task(TPS("rcu_preempt"),
542 						rnp->gp_seq, t->pid);
543 		if (&t->rcu_node_entry == rnp->gp_tasks)
544 			WRITE_ONCE(rnp->gp_tasks, np);
545 		if (&t->rcu_node_entry == rnp->exp_tasks)
546 			WRITE_ONCE(rnp->exp_tasks, np);
547 		if (IS_ENABLED(CONFIG_RCU_BOOST)) {
548 			/* Snapshot ->boost_mtx ownership w/rnp->lock held. */
549 			drop_boost_mutex = rt_mutex_owner(&rnp->boost_mtx.rtmutex) == t;
550 			if (&t->rcu_node_entry == rnp->boost_tasks)
551 				WRITE_ONCE(rnp->boost_tasks, np);
552 		}
553 
554 		/*
555 		 * If this was the last task on the current list, and if
556 		 * we aren't waiting on any CPUs, report the quiescent state.
557 		 * Note that rcu_report_unblock_qs_rnp() releases rnp->lock,
558 		 * so we must take a snapshot of the expedited state.
559 		 */
560 		empty_exp_now = sync_rcu_exp_done(rnp);
561 		if (!empty_norm && !rcu_preempt_blocked_readers_cgp(rnp)) {
562 			trace_rcu_quiescent_state_report(TPS("preempt_rcu"),
563 							 rnp->gp_seq,
564 							 0, rnp->qsmask,
565 							 rnp->level,
566 							 rnp->grplo,
567 							 rnp->grphi,
568 							 !!rnp->gp_tasks);
569 			rcu_report_unblock_qs_rnp(rnp, flags);
570 		} else {
571 			raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
572 		}
573 
574 		/*
575 		 * If this was the last task on the expedited lists,
576 		 * then we need to report up the rcu_node hierarchy.
577 		 */
578 		if (!empty_exp && empty_exp_now)
579 			rcu_report_exp_rnp(rnp, true);
580 
581 		/* Unboost if we were boosted. */
582 		if (IS_ENABLED(CONFIG_RCU_BOOST) && drop_boost_mutex)
583 			rt_mutex_futex_unlock(&rnp->boost_mtx.rtmutex);
584 	} else {
585 		local_irq_restore(flags);
586 	}
587 }
588 
589 /*
590  * Is a deferred quiescent-state pending, and are we also not in
591  * an RCU read-side critical section?  It is the caller's responsibility
592  * to ensure it is otherwise safe to report any deferred quiescent
593  * states.  The reason for this is that it is safe to report a
594  * quiescent state during context switch even though preemption
595  * is disabled.  This function cannot be expected to understand these
596  * nuances, so the caller must handle them.
597  */
598 static notrace bool rcu_preempt_need_deferred_qs(struct task_struct *t)
599 {
600 	return (this_cpu_read(rcu_data.cpu_no_qs.b.exp) ||
601 		READ_ONCE(t->rcu_read_unlock_special.s)) &&
602 	       rcu_preempt_depth() == 0;
603 }
604 
605 /*
606  * Report a deferred quiescent state if needed and safe to do so.
607  * As with rcu_preempt_need_deferred_qs(), "safe" involves only
608  * not being in an RCU read-side critical section.  The caller must
609  * evaluate safety in terms of interrupt, softirq, and preemption
610  * disabling.
611  */
612 notrace void rcu_preempt_deferred_qs(struct task_struct *t)
613 {
614 	unsigned long flags;
615 	struct rcu_data *rdp;
616 
617 	if (!rcu_preempt_need_deferred_qs(t)) {
618 		/*
619 		 * If we got here from a softirq/irq_work that fired while
620 		 * rcu_preempt_depth() > 0, the deferred-QS mechanism has been
621 		 * consumed without doing any work: rcu_preempt_need_deferred_qs()
622 		 * just returned false because the task is still in a reader, so
623 		 * the actual QS report has to wait for the next
624 		 * rcu_read_unlock().
625 		 *
626 		 * Clear ->defer_qs_pending here so the next outer
627 		 * rcu_read_unlock_special() can re-arm a fresh mechanism (in
628 		 * particular the irq_work path, which the local_irq_enable()
629 		 * recovery boundary cannot itself reschedule from).
630 		 *
631 		 * Recursion safety: rcu_preempt_depth() > 0 means we are inside
632 		 * an outer reader, so any inner rcu_read_unlock() reached via
633 		 * tracing (bpf programs attached to trace points) brings
634 		 * nesting to outer (> 0), never to 0, so no recursive
635 		 * raise_softirq_irqoff()/irq_work_queue_on() can be triggered
636 		 * by this clear.
637 		 */
638 		if (rcu_preempt_depth() > 0) {
639 			rdp = this_cpu_ptr(&rcu_data);
640 			rcu_defer_qs_clear(rdp);
641 		}
642 		return;
643 	}
644 	local_irq_save(flags);
645 	rcu_preempt_deferred_qs_irqrestore(t, flags);
646 }
647 
648 /*
649  * Minimal handler to give the scheduler a chance to re-evaluate.
650  */
651 static void rcu_preempt_deferred_qs_handler(struct irq_work *iwp)
652 {
653 	struct rcu_data *rdp;
654 
655 	lockdep_assert_irqs_disabled();
656 	rdp = container_of(iwp, struct rcu_data, defer_qs_iw);
657 
658 	/*
659 	 * If the IRQ work handler happens to run in the middle of RCU read-side
660 	 * critical section, it could be ineffective in getting the scheduler's
661 	 * attention to report a deferred quiescent state (the whole point of the
662 	 * IRQ work). For this reason, requeue the IRQ work.
663 	 *
664 	 * Basically, we want to avoid following situation:
665 	 * 1. rcu_read_unlock() queues IRQ work (state -> DEFER_QS_PENDING)
666 	 * 2. CPU enters new rcu_read_lock()
667 	 * 3. IRQ work runs but cannot report QS due to rcu_preempt_depth() > 0
668 	 * 4. rcu_read_unlock() does not re-queue work (state still PENDING)
669 	 * 5. Deferred QS reporting does not happen.
670 	 */
671 	if (rcu_preempt_depth() > 0)
672 		rcu_defer_qs_clear(rdp);
673 }
674 
675 /*
676  * Check if expedited grace period processing during unlock is needed.
677  *
678  * This function determines whether expedited handling is required based on:
679  * 1. Task blocking an expedited grace period (based on a heuristic, could be
680  *    false-positive, see below.)
681  * 2. CPU participating in an expedited grace period
682  * 3. Strict grace period mode requiring expedited handling
683  * 4. RCU priority deboosting needs when interrupts were disabled
684  *
685  * @t: The task being checked
686  * @rdp: The per-CPU RCU data
687  * @rnp: The RCU node for this CPU
688  * @irqs_were_disabled: Whether interrupts were disabled before rcu_read_unlock()
689  *
690  * Returns true if expedited processing of the rcu_read_unlock() is needed.
691  */
692 static bool rcu_unlock_needs_exp_handling(struct task_struct *t,
693 				      struct rcu_data *rdp,
694 				      struct rcu_node *rnp,
695 				      bool irqs_were_disabled)
696 {
697 	/*
698 	 * Check if this task is blocking an expedited grace period. If the
699 	 * task was preempted within an RCU read-side critical section and is
700 	 * on the expedited grace period blockers list (exp_tasks), we need
701 	 * expedited handling to unblock the expedited GP. This is not an exact
702 	 * check because 't' might not be on the exp_tasks list at all - its
703 	 * just a fast heuristic that can be false-positive sometimes.
704 	 */
705 	if (t->rcu_blocked_node && READ_ONCE(t->rcu_blocked_node->exp_tasks))
706 		return true;
707 
708 	/*
709 	 * Check if this CPU is participating in an expedited grace period.
710 	 * The expmask bitmap tracks which CPUs need to check in for the
711 	 * current expedited GP. If our CPU's bit is set, we need expedited
712 	 * handling to help complete the expedited GP.
713 	 */
714 	if (rdp->grpmask & READ_ONCE(rnp->expmask))
715 		return true;
716 
717 	/*
718 	 * In CONFIG_RCU_STRICT_GRACE_PERIOD=y kernels, all grace periods
719 	 * are treated as short for testing purposes even if that means
720 	 * disturbing the system more. Check if either:
721 	 * - This CPU has not yet reported a quiescent state, or
722 	 * - This task was preempted within an RCU critical section
723 	 * In either case, require expedited handling for strict GP mode.
724 	 */
725 	if (IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD) &&
726 	    ((rdp->grpmask & READ_ONCE(rnp->qsmask)) || t->rcu_blocked_node))
727 		return true;
728 
729 	/*
730 	 * RCU priority boosting case: If a task is subject to RCU priority
731 	 * boosting and exits an RCU read-side critical section with interrupts
732 	 * disabled, we need expedited handling to ensure timely deboosting.
733 	 * Without this, a low-priority task could incorrectly run at high
734 	 * real-time priority for an extended period degrading real-time
735 	 * responsiveness. This applies to all CONFIG_RCU_BOOST=y kernels,
736 	 * not just to PREEMPT_RT.
737 	 */
738 	if (IS_ENABLED(CONFIG_RCU_BOOST) && irqs_were_disabled && t->rcu_blocked_node)
739 		return true;
740 
741 	return false;
742 }
743 
744 /*
745  * Handle special cases during rcu_read_unlock(), such as needing to
746  * notify RCU core processing or task having blocked during the RCU
747  * read-side critical section.
748  */
749 static void rcu_read_unlock_special(struct task_struct *t)
750 {
751 	unsigned long flags;
752 	bool irqs_were_disabled;
753 	bool preempt_bh_were_disabled =
754 			!!(preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK));
755 
756 	/* NMI handlers cannot block and cannot safely manipulate state. */
757 	if (in_nmi())
758 		return;
759 
760 	local_irq_save(flags);
761 	irqs_were_disabled = irqs_disabled_flags(flags);
762 	if (preempt_bh_were_disabled || irqs_were_disabled) {
763 		bool needs_exp; // Expedited handling needed.
764 		struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
765 		struct rcu_node *rnp = rdp->mynode;
766 
767 		needs_exp = rcu_unlock_needs_exp_handling(t, rdp, rnp, irqs_were_disabled);
768 
769 		// Need to defer quiescent state until everything is enabled.
770 		if (use_softirq && (in_hardirq() || (needs_exp && !irqs_were_disabled))) {
771 			// Using softirq, safe to awaken, and either the
772 			// wakeup is free or there is either an expedited
773 			// GP in flight or a potential need to deboost.
774 			if (rdp->defer_qs_pending != DEFER_QS_PENDING) {
775 				rdp->defer_qs_pending = DEFER_QS_PENDING;
776 				raise_softirq_irqoff(RCU_SOFTIRQ);
777 			}
778 		} else {
779 			// Enabling BH or preempt does reschedule, so...
780 			// Also if no expediting and no possible deboosting,
781 			// slow is OK.  Plus nohz_full CPUs eventually get
782 			// tick enabled.
783 			set_need_resched_current();
784 			if (IS_ENABLED(CONFIG_IRQ_WORK) && irqs_were_disabled &&
785 			    needs_exp && rdp->defer_qs_pending != DEFER_QS_PENDING &&
786 			    cpu_online(rdp->cpu)) {
787 				// Get scheduler to re-evaluate and call hooks.
788 				// If !IRQ_WORK, FQS scan will eventually IPI.
789 				rdp->defer_qs_pending = DEFER_QS_PENDING;
790 				irq_work_queue_on(&rdp->defer_qs_iw, rdp->cpu);
791 			}
792 		}
793 		local_irq_restore(flags);
794 		return;
795 	}
796 	rcu_preempt_deferred_qs_irqrestore(t, flags);
797 }
798 
799 /*
800  * Check that the list of blocked tasks for the newly completed grace
801  * period is in fact empty.  It is a serious bug to complete a grace
802  * period that still has RCU readers blocked!  This function must be
803  * invoked -before- updating this rnp's ->gp_seq.
804  *
805  * Also, if there are blocked tasks on the list, they automatically
806  * block the newly created grace period, so set up ->gp_tasks accordingly.
807  */
808 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
809 {
810 	struct task_struct *t;
811 
812 	RCU_LOCKDEP_WARN(preemptible(), "rcu_preempt_check_blocked_tasks() invoked with preemption enabled!!!\n");
813 	raw_lockdep_assert_held_rcu_node(rnp);
814 	if (WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp)))
815 		dump_blkd_tasks(rnp, 10);
816 	if (rcu_preempt_has_tasks(rnp) &&
817 	    (rnp->qsmaskinit || rnp->wait_blkd_tasks)) {
818 		WRITE_ONCE(rnp->gp_tasks, rnp->blkd_tasks.next);
819 		t = container_of(rnp->gp_tasks, struct task_struct,
820 				 rcu_node_entry);
821 		trace_rcu_unlock_preempted_task(TPS("rcu_preempt-GPS"),
822 						rnp->gp_seq, t->pid);
823 	}
824 	WARN_ON_ONCE(rnp->qsmask);
825 }
826 
827 /*
828  * Check for a quiescent state from the current CPU, including voluntary
829  * context switches for Tasks RCU.  When a task blocks, the task is
830  * recorded in the corresponding CPU's rcu_node structure, which is checked
831  * elsewhere, hence this function need only check for quiescent states
832  * related to the current CPU, not to those related to tasks.
833  */
834 static void rcu_flavor_sched_clock_irq(int user)
835 {
836 	struct task_struct *t = current;
837 
838 	lockdep_assert_irqs_disabled();
839 	if (rcu_preempt_depth() > 0 ||
840 	    (preempt_count() & (PREEMPT_MASK | SOFTIRQ_MASK))) {
841 		/* No QS, force context switch if deferred. */
842 		if (rcu_preempt_need_deferred_qs(t))
843 			set_need_resched_current();
844 	} else if (rcu_preempt_need_deferred_qs(t)) {
845 		rcu_preempt_deferred_qs(t); /* Report deferred QS. */
846 		return;
847 	} else if (!WARN_ON_ONCE(rcu_preempt_depth())) {
848 		rcu_qs(); /* Report immediate QS. */
849 		return;
850 	}
851 
852 	/* If GP is oldish, ask for help from rcu_read_unlock_special(). */
853 	if (rcu_preempt_depth() > 0 &&
854 	    __this_cpu_read(rcu_data.core_needs_qs) &&
855 	    __this_cpu_read(rcu_data.cpu_no_qs.b.norm) &&
856 	    !t->rcu_read_unlock_special.b.need_qs &&
857 	    time_after(jiffies, rcu_state.gp_start + HZ))
858 		t->rcu_read_unlock_special.b.need_qs = true;
859 }
860 
861 /*
862  * Check for a task exiting while in a preemptible-RCU read-side
863  * critical section, clean up if so.  No need to issue warnings, as
864  * debug_check_no_locks_held() already does this if lockdep is enabled.
865  * Besides, if this function does anything other than just immediately
866  * return, there was a bug of some sort.  Spewing warnings from this
867  * function is like as not to simply obscure important prior warnings.
868  */
869 void exit_rcu(void)
870 {
871 	struct task_struct *t = current;
872 
873 	if (unlikely(!list_empty(&current->rcu_node_entry))) {
874 		rcu_preempt_depth_set(1);
875 		barrier();
876 		WRITE_ONCE(t->rcu_read_unlock_special.b.blocked, true);
877 	} else if (unlikely(rcu_preempt_depth())) {
878 		rcu_preempt_depth_set(1);
879 	} else {
880 		return;
881 	}
882 	__rcu_read_unlock();
883 	rcu_preempt_deferred_qs(current);
884 }
885 
886 /*
887  * Dump the blocked-tasks state, but limit the list dump to the
888  * specified number of elements.
889  */
890 static void
891 dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
892 {
893 	int cpu;
894 	int i;
895 	struct list_head *lhp;
896 	struct rcu_data *rdp;
897 	struct rcu_node *rnp1;
898 
899 	raw_lockdep_assert_held_rcu_node(rnp);
900 	pr_info("%s: grp: %d-%d level: %d ->gp_seq %ld ->completedqs %ld\n",
901 		__func__, rnp->grplo, rnp->grphi, rnp->level,
902 		(long)READ_ONCE(rnp->gp_seq), (long)rnp->completedqs);
903 	for (rnp1 = rnp; rnp1; rnp1 = rnp1->parent)
904 		pr_info("%s: %d:%d ->qsmask %#lx ->qsmaskinit %#lx ->qsmaskinitnext %#lx\n",
905 			__func__, rnp1->grplo, rnp1->grphi, rnp1->qsmask, rnp1->qsmaskinit, rnp1->qsmaskinitnext);
906 	pr_info("%s: ->gp_tasks %p ->boost_tasks %p ->exp_tasks %p\n",
907 		__func__, READ_ONCE(rnp->gp_tasks), data_race(rnp->boost_tasks),
908 		READ_ONCE(rnp->exp_tasks));
909 	pr_info("%s: ->blkd_tasks", __func__);
910 	i = 0;
911 	list_for_each(lhp, &rnp->blkd_tasks) {
912 		pr_cont(" %p", lhp);
913 		if (++i >= ncheck)
914 			break;
915 	}
916 	pr_cont("\n");
917 	for (cpu = rnp->grplo; cpu <= rnp->grphi; cpu++) {
918 		rdp = per_cpu_ptr(&rcu_data, cpu);
919 		pr_info("\t%d: %c online: %ld(%d) offline: %ld(%d)\n",
920 			cpu, ".o"[rcu_rdp_cpu_online(rdp)],
921 			(long)rdp->rcu_onl_gp_seq, rdp->rcu_onl_gp_state,
922 			(long)rdp->rcu_ofl_gp_seq, rdp->rcu_ofl_gp_state);
923 	}
924 }
925 
926 static void rcu_preempt_deferred_qs_init(struct rcu_data *rdp)
927 {
928 	rdp->defer_qs_iw = IRQ_WORK_INIT_HARD(rcu_preempt_deferred_qs_handler);
929 }
930 #else /* #ifdef CONFIG_PREEMPT_RCU */
931 
932 /*
933  * If strict grace periods are enabled, and if the calling
934  * __rcu_read_unlock() marks the beginning of a quiescent state, immediately
935  * report that quiescent state and, if requested, spin for a bit.
936  */
937 void rcu_read_unlock_strict(void)
938 {
939 	struct rcu_data *rdp;
940 
941 	if (irqs_disabled() || in_atomic_preempt_off() || !rcu_state.gp_kthread)
942 		return;
943 
944 	/*
945 	 * rcu_report_qs_rdp() can only be invoked with a stable rdp and
946 	 * from the local CPU.
947 	 *
948 	 * The in_atomic_preempt_off() check ensures that we come here holding
949 	 * the last preempt_count (which will get dropped once we return to
950 	 * __rcu_read_unlock()).
951 	 */
952 	rdp = this_cpu_ptr(&rcu_data);
953 	WRITE_ONCE(rdp->cpu_no_qs.b.norm, false);
954 	rcu_report_qs_rdp(rdp);
955 	udelay(rcu_unlock_delay);
956 }
957 EXPORT_SYMBOL_GPL(rcu_read_unlock_strict);
958 
959 /*
960  * Tell them what RCU they are running.
961  */
962 static void __init rcu_bootup_announce(void)
963 {
964 	pr_info("Hierarchical RCU implementation.\n");
965 	rcu_bootup_announce_oddness();
966 }
967 
968 /*
969  * Note a quiescent state for PREEMPTION=n.  Because we do not need to know
970  * how many quiescent states passed, just if there was at least one since
971  * the start of the grace period, this just sets a flag.  The caller must
972  * have disabled preemption.
973  */
974 static void rcu_qs(void)
975 {
976 	RCU_LOCKDEP_WARN(preemptible(), "rcu_qs() invoked with preemption enabled!!!");
977 	if (!this_cpu_read(rcu_data.cpu_no_qs.s))
978 		return;
979 	trace_rcu_grace_period(TPS("rcu_sched"),
980 			       __this_cpu_read(rcu_data.gp_seq), TPS("cpuqs"));
981 	this_cpu_write(rcu_data.cpu_no_qs.b.norm, false);
982 	if (this_cpu_read(rcu_data.cpu_no_qs.b.exp))
983 		rcu_report_exp_rdp(this_cpu_ptr(&rcu_data));
984 }
985 
986 /*
987  * Register an urgently needed quiescent state.  If there is an
988  * emergency, invoke rcu_momentary_eqs() to do a heavy-weight
989  * dyntick-idle quiescent state visible to other CPUs, which will in
990  * some cases serve for expedited as well as normal grace periods.
991  * Either way, register a lightweight quiescent state.
992  */
993 void rcu_all_qs(void)
994 {
995 	unsigned long flags;
996 
997 	if (!READ_ONCE(*raw_cpu_ptr(&rcu_data.rcu_urgent_qs)))
998 		return;
999 	preempt_disable();  // For CONFIG_PREEMPT_COUNT=y kernels
1000 	/* Load rcu_urgent_qs before other flags. */
1001 	if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs))) {
1002 		preempt_enable();
1003 		return;
1004 	}
1005 	WRITE_ONCE(*this_cpu_ptr(&rcu_data.rcu_urgent_qs), false);
1006 	if (unlikely(READ_ONCE(*this_cpu_ptr(&rcu_data.rcu_need_heavy_qs)))) {
1007 		local_irq_save(flags);
1008 		rcu_momentary_eqs();
1009 		local_irq_restore(flags);
1010 	}
1011 	rcu_qs();
1012 	preempt_enable();
1013 }
1014 EXPORT_SYMBOL_GPL(rcu_all_qs);
1015 
1016 /*
1017  * Note a PREEMPTION=n context switch. The caller must have disabled interrupts.
1018  */
1019 void rcu_note_context_switch(bool preempt)
1020 {
1021 	trace_rcu_utilization(TPS("Start context switch"));
1022 	rcu_qs();
1023 	/* Load rcu_urgent_qs before other flags. */
1024 	if (!smp_load_acquire(this_cpu_ptr(&rcu_data.rcu_urgent_qs)))
1025 		goto out;
1026 	WRITE_ONCE(*this_cpu_ptr(&rcu_data.rcu_urgent_qs), false);
1027 	if (unlikely(READ_ONCE(*this_cpu_ptr(&rcu_data.rcu_need_heavy_qs))))
1028 		rcu_momentary_eqs();
1029 out:
1030 	rcu_tasks_qs(current, preempt);
1031 	trace_rcu_utilization(TPS("End context switch"));
1032 }
1033 EXPORT_SYMBOL_GPL(rcu_note_context_switch);
1034 
1035 /*
1036  * Because preemptible RCU does not exist, there are never any preempted
1037  * RCU readers.
1038  */
1039 static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
1040 {
1041 	return 0;
1042 }
1043 
1044 /*
1045  * Because there is no preemptible RCU, there can be no readers blocked.
1046  */
1047 static bool rcu_preempt_has_tasks(struct rcu_node *rnp)
1048 {
1049 	return false;
1050 }
1051 
1052 /*
1053  * Because there is no preemptible RCU, there can be no deferred quiescent
1054  * states.
1055  */
1056 static notrace bool rcu_preempt_need_deferred_qs(struct task_struct *t)
1057 {
1058 	return false;
1059 }
1060 
1061 // Except that we do need to respond to a request by an expedited
1062 // grace period for a quiescent state from this CPU.  Note that in
1063 // non-preemptible kernels, there can be no context switches within RCU
1064 // read-side critical sections, which in turn means that the leaf rcu_node
1065 // structure's blocked-tasks list is always empty.  is therefore no need to
1066 // actually check it.  Instead, a quiescent state from this CPU suffices,
1067 // and this function is only called from such a quiescent state.
1068 notrace void rcu_preempt_deferred_qs(struct task_struct *t)
1069 {
1070 	struct rcu_data *rdp = this_cpu_ptr(&rcu_data);
1071 
1072 	if (READ_ONCE(rdp->cpu_no_qs.b.exp))
1073 		rcu_report_exp_rdp(rdp);
1074 }
1075 
1076 /*
1077  * Because there is no preemptible RCU, there can be no readers blocked,
1078  * so there is no need to check for blocked tasks.  So check only for
1079  * bogus qsmask values.
1080  */
1081 static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
1082 {
1083 	WARN_ON_ONCE(rnp->qsmask);
1084 }
1085 
1086 /*
1087  * Check to see if this CPU is in a non-context-switch quiescent state,
1088  * namely user mode and idle loop.
1089  */
1090 static void rcu_flavor_sched_clock_irq(int user)
1091 {
1092 	if (user || rcu_is_cpu_rrupt_from_idle() ||
1093 	     (IS_ENABLED(CONFIG_PREEMPT_COUNT) &&
1094 	      (preempt_count() == HARDIRQ_OFFSET))) {
1095 
1096 		/*
1097 		 * Get here if this CPU took its interrupt from user
1098 		 * mode, from the idle loop without this being a nested
1099 		 * interrupt, or while not holding the task preempt count
1100 		 * (with PREEMPT_COUNT=y). In this case, the CPU is in a
1101 		 * quiescent state, so note it.
1102 		 *
1103 		 * No memory barrier is required here because rcu_qs()
1104 		 * references only CPU-local variables that other CPUs
1105 		 * neither access nor modify, at least not while the
1106 		 * corresponding CPU is online.
1107 		 */
1108 		rcu_qs();
1109 	}
1110 }
1111 
1112 /*
1113  * Because preemptible RCU does not exist, tasks cannot possibly exit
1114  * while in preemptible RCU read-side critical sections.
1115  */
1116 void exit_rcu(void)
1117 {
1118 }
1119 
1120 /*
1121  * Dump the guaranteed-empty blocked-tasks state.  Trust but verify.
1122  */
1123 static void
1124 dump_blkd_tasks(struct rcu_node *rnp, int ncheck)
1125 {
1126 	WARN_ON_ONCE(!list_empty(&rnp->blkd_tasks));
1127 }
1128 
1129 static void rcu_preempt_deferred_qs_init(struct rcu_data *rdp) { }
1130 
1131 #endif /* #else #ifdef CONFIG_PREEMPT_RCU */
1132 
1133 /*
1134  * If boosting, set rcuc kthreads to realtime priority.
1135  */
1136 static void rcu_cpu_kthread_setup(unsigned int cpu)
1137 {
1138 	struct rcu_data *rdp = per_cpu_ptr(&rcu_data, cpu);
1139 #ifdef CONFIG_RCU_BOOST
1140 	struct sched_param sp;
1141 
1142 	sp.sched_priority = kthread_prio;
1143 	sched_setscheduler_nocheck(current, SCHED_FIFO, &sp);
1144 #endif /* #ifdef CONFIG_RCU_BOOST */
1145 
1146 	WRITE_ONCE(rdp->rcuc_activity, jiffies);
1147 }
1148 
1149 static bool rcu_is_callbacks_nocb_kthread(struct rcu_data *rdp)
1150 {
1151 #ifdef CONFIG_RCU_NOCB_CPU
1152 	return rdp->nocb_cb_kthread == current;
1153 #else
1154 	return false;
1155 #endif
1156 }
1157 
1158 /*
1159  * Is the current CPU running the RCU-callbacks kthread?
1160  * Caller must have preemption disabled.
1161  */
1162 static bool rcu_is_callbacks_kthread(struct rcu_data *rdp)
1163 {
1164 	return rdp->rcu_cpu_kthread_task == current ||
1165 			rcu_is_callbacks_nocb_kthread(rdp);
1166 }
1167 
1168 #ifdef CONFIG_RCU_BOOST
1169 
1170 /*
1171  * Carry out RCU priority boosting on the task indicated by ->exp_tasks
1172  * or ->boost_tasks, advancing the pointer to the next task in the
1173  * ->blkd_tasks list.
1174  *
1175  * Note that irqs must be enabled: boosting the task can block.
1176  * Returns 1 if there are more tasks needing to be boosted.
1177  */
1178 static int rcu_boost(struct rcu_node *rnp)
1179 {
1180 	unsigned long flags;
1181 	struct task_struct *t;
1182 	struct list_head *tb;
1183 
1184 	if (READ_ONCE(rnp->exp_tasks) == NULL &&
1185 	    READ_ONCE(rnp->boost_tasks) == NULL)
1186 		return 0;  /* Nothing left to boost. */
1187 
1188 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
1189 
1190 	/*
1191 	 * Recheck under the lock: all tasks in need of boosting
1192 	 * might exit their RCU read-side critical sections on their own.
1193 	 */
1194 	if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
1195 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1196 		return 0;
1197 	}
1198 
1199 	/*
1200 	 * Preferentially boost tasks blocking expedited grace periods.
1201 	 * This cannot starve the normal grace periods because a second
1202 	 * expedited grace period must boost all blocked tasks, including
1203 	 * those blocking the pre-existing normal grace period.
1204 	 */
1205 	if (rnp->exp_tasks != NULL)
1206 		tb = rnp->exp_tasks;
1207 	else
1208 		tb = rnp->boost_tasks;
1209 
1210 	/*
1211 	 * We boost task t by manufacturing an rt_mutex that appears to
1212 	 * be held by task t.  We leave a pointer to that rt_mutex where
1213 	 * task t can find it, and task t will release the mutex when it
1214 	 * exits its outermost RCU read-side critical section.  Then
1215 	 * simply acquiring this artificial rt_mutex will boost task
1216 	 * t's priority.  (Thanks to tglx for suggesting this approach!)
1217 	 *
1218 	 * Note that task t must acquire rnp->lock to remove itself from
1219 	 * the ->blkd_tasks list, which it will do from exit() if from
1220 	 * nowhere else.  We therefore are guaranteed that task t will
1221 	 * stay around at least until we drop rnp->lock.  Note that
1222 	 * rnp->lock also resolves races between our priority boosting
1223 	 * and task t's exiting its outermost RCU read-side critical
1224 	 * section.
1225 	 */
1226 	t = container_of(tb, struct task_struct, rcu_node_entry);
1227 	rt_mutex_init_proxy_locked(&rnp->boost_mtx.rtmutex, t);
1228 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1229 	/* Lock only for side effect: boosts task t's priority. */
1230 	rt_mutex_lock(&rnp->boost_mtx);
1231 	rt_mutex_unlock(&rnp->boost_mtx);  /* Then keep lockdep happy. */
1232 	rnp->n_boosts++;
1233 
1234 	return READ_ONCE(rnp->exp_tasks) != NULL ||
1235 	       READ_ONCE(rnp->boost_tasks) != NULL;
1236 }
1237 
1238 /*
1239  * Priority-boosting kthread, one per leaf rcu_node.
1240  */
1241 static int rcu_boost_kthread(void *arg)
1242 {
1243 	struct rcu_node *rnp = (struct rcu_node *)arg;
1244 	int spincnt = 0;
1245 	int more2boost;
1246 
1247 	trace_rcu_utilization(TPS("Start boost kthread@init"));
1248 	for (;;) {
1249 		WRITE_ONCE(rnp->boost_kthread_status, RCU_KTHREAD_WAITING);
1250 		trace_rcu_utilization(TPS("End boost kthread@rcu_wait"));
1251 		rcu_wait(READ_ONCE(rnp->boost_tasks) ||
1252 			 READ_ONCE(rnp->exp_tasks));
1253 		trace_rcu_utilization(TPS("Start boost kthread@rcu_wait"));
1254 		WRITE_ONCE(rnp->boost_kthread_status, RCU_KTHREAD_RUNNING);
1255 		more2boost = rcu_boost(rnp);
1256 		if (more2boost)
1257 			spincnt++;
1258 		else
1259 			spincnt = 0;
1260 		if (spincnt > 10) {
1261 			WRITE_ONCE(rnp->boost_kthread_status, RCU_KTHREAD_YIELDING);
1262 			trace_rcu_utilization(TPS("End boost kthread@rcu_yield"));
1263 			schedule_timeout_idle(2);
1264 			trace_rcu_utilization(TPS("Start boost kthread@rcu_yield"));
1265 			spincnt = 0;
1266 		}
1267 	}
1268 	/* NOTREACHED */
1269 	trace_rcu_utilization(TPS("End boost kthread@notreached"));
1270 	return 0;
1271 }
1272 
1273 /*
1274  * Check to see if it is time to start boosting RCU readers that are
1275  * blocking the current grace period, and, if so, tell the per-rcu_node
1276  * kthread to start boosting them.  If there is an expedited grace
1277  * period in progress, it is always time to boost.
1278  *
1279  * The caller must hold rnp->lock, which this function releases.
1280  * The ->boost_kthread_task is immortal, so we don't need to worry
1281  * about it going away.
1282  */
1283 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1284 	__releases(rnp->lock)
1285 {
1286 	raw_lockdep_assert_held_rcu_node(rnp);
1287 	if (!rnp->boost_kthread_task ||
1288 	    (!rcu_preempt_blocked_readers_cgp(rnp) && !rnp->exp_tasks)) {
1289 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1290 		return;
1291 	}
1292 	if (rnp->exp_tasks != NULL ||
1293 	    (rnp->gp_tasks != NULL &&
1294 	     rnp->boost_tasks == NULL &&
1295 	     rnp->qsmask == 0 &&
1296 	     (!time_after(rnp->boost_time, jiffies) || rcu_state.cbovld ||
1297 	      IS_ENABLED(CONFIG_RCU_STRICT_GRACE_PERIOD)))) {
1298 		if (rnp->exp_tasks == NULL)
1299 			WRITE_ONCE(rnp->boost_tasks, rnp->gp_tasks);
1300 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1301 		rcu_wake_cond(rnp->boost_kthread_task,
1302 			      READ_ONCE(rnp->boost_kthread_status));
1303 	} else {
1304 		raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1305 	}
1306 }
1307 
1308 #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
1309 
1310 /*
1311  * Do priority-boost accounting for the start of a new grace period.
1312  */
1313 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1314 {
1315 	rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
1316 }
1317 
1318 /*
1319  * Create an RCU-boost kthread for the specified node if one does not
1320  * already exist.  We only create this kthread for preemptible RCU.
1321  */
1322 static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
1323 {
1324 	unsigned long flags;
1325 	int rnp_index = rnp - rcu_get_root();
1326 	struct sched_param sp;
1327 	struct task_struct *t;
1328 
1329 	if (rnp->boost_kthread_task)
1330 		return;
1331 
1332 	t = kthread_create(rcu_boost_kthread, (void *)rnp,
1333 			   "rcub/%d", rnp_index);
1334 	if (WARN_ON_ONCE(IS_ERR(t)))
1335 		return;
1336 
1337 	raw_spin_lock_irqsave_rcu_node(rnp, flags);
1338 	rnp->boost_kthread_task = t;
1339 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1340 
1341 	sp.sched_priority = kthread_prio;
1342 	sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
1343 	rcu_thread_affine_rnp(t, rnp);
1344 	wake_up_process(t); /* get to TASK_INTERRUPTIBLE quickly. */
1345 }
1346 
1347 #ifdef CONFIG_RCU_TORTURE_TEST
1348 
1349 /*
1350  * Is the current task RCU priority boosted?  This is used by
1351  * rcutorture to check that tasks are always deboosted once then exit
1352  * an RCU read-side critical section, no matter how many overlapping
1353  * segments of rcu_read_lock(), preempt_disable(), local_bh_disable(),
1354  * or local_irq_disable() made up that reader.
1355  *
1356  * The lockless accesses in rt_mutex_owner(&rnp->boost_mtx.rtmutex)
1357  * are safe because tasks release ->boost_mtx when they own it, they
1358  * cannot be boosted unless current->rcu_blocked_node is non-NULL,
1359  * current->rcu_blocked_node is modified only by the current task,
1360  * rt_mutex_owner() uses READ_ONCE() on the ->owner field, and the owner
1361  * switching among other tasks cannot force an equality comparison.
1362  */
1363 bool rcu_is_task_rcu_boosted(void)
1364 {
1365 	bool ret;
1366 	struct rcu_node *rnp;
1367 	struct task_struct *t = current;
1368 
1369 	preempt_disable(); // Stabilize ->rcu_blocked_node
1370 	rnp = t->rcu_blocked_node;
1371 	if (!rnp)
1372 		ret = false;
1373 	else
1374 		ret = (rt_mutex_owner(&rnp->boost_mtx.rtmutex) == t);
1375 	preempt_enable();
1376 	return ret;
1377 }
1378 EXPORT_SYMBOL_GPL(rcu_is_task_rcu_boosted);
1379 
1380 #endif // #ifdef CONFIG_RCU_TORTURE_TEST
1381 
1382 #else /* #ifdef CONFIG_RCU_BOOST */
1383 
1384 static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
1385 	__releases(rnp->lock)
1386 {
1387 	raw_spin_unlock_irqrestore_rcu_node(rnp, flags);
1388 }
1389 
1390 static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
1391 {
1392 }
1393 
1394 static void rcu_spawn_one_boost_kthread(struct rcu_node *rnp)
1395 {
1396 }
1397 
1398 #endif /* #else #ifdef CONFIG_RCU_BOOST */
1399 
1400 /*
1401  * Is this CPU a NO_HZ_FULL CPU that should ignore RCU so that the
1402  * grace-period kthread will do force_quiescent_state() processing?
1403  * The idea is to avoid waking up RCU core processing on such a
1404  * CPU unless the grace period has extended for too long.
1405  *
1406  * This code relies on the fact that all NO_HZ_FULL CPUs are also
1407  * RCU_NOCB_CPU CPUs.
1408  */
1409 static bool rcu_nohz_full_cpu(void)
1410 {
1411 #ifdef CONFIG_NO_HZ_FULL
1412 	if (tick_nohz_full_cpu(smp_processor_id()) &&
1413 	    (!rcu_gp_in_progress() ||
1414 	     time_before(jiffies, READ_ONCE(rcu_state.gp_start) + HZ)))
1415 		return true;
1416 #endif /* #ifdef CONFIG_NO_HZ_FULL */
1417 	return false;
1418 }
1419 
1420 /*
1421  * Bind the RCU grace-period kthreads to the housekeeping CPU.
1422  */
1423 static void rcu_bind_gp_kthread(void)
1424 {
1425 	if (!tick_nohz_full_enabled())
1426 		return;
1427 	housekeeping_affine(current, HK_TYPE_RCU);
1428 }
1429