xref: /linux/kernel/sched/core.c (revision c1e822754cc7f28b98c6897d62e8b47b4001e422)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  kernel/sched/core.c
4  *
5  *  Core kernel CPU scheduler code
6  *
7  *  Copyright (C) 1991-2002  Linus Torvalds
8  *  Copyright (C) 1998-2024  Ingo Molnar, Red Hat
9  */
10 #include <linux/highmem.h>
11 #include <linux/hrtimer_api.h>
12 #include <linux/ktime_api.h>
13 #include <linux/sched/signal.h>
14 #include <linux/syscalls_api.h>
15 #include <linux/debug_locks.h>
16 #include <linux/prefetch.h>
17 #include <linux/capability.h>
18 #include <linux/pgtable_api.h>
19 #include <linux/wait_bit.h>
20 #include <linux/jiffies.h>
21 #include <linux/spinlock_api.h>
22 #include <linux/cpumask_api.h>
23 #include <linux/lockdep_api.h>
24 #include <linux/hardirq.h>
25 #include <linux/softirq.h>
26 #include <linux/refcount_api.h>
27 #include <linux/topology.h>
28 #include <linux/sched/clock.h>
29 #include <linux/sched/cond_resched.h>
30 #include <linux/sched/cputime.h>
31 #include <linux/sched/debug.h>
32 #include <linux/sched/hotplug.h>
33 #include <linux/sched/init.h>
34 #include <linux/sched/isolation.h>
35 #include <linux/sched/loadavg.h>
36 #include <linux/sched/mm.h>
37 #include <linux/sched/nohz.h>
38 #include <linux/sched/rseq_api.h>
39 #include <linux/sched/rt.h>
40 
41 #include <linux/blkdev.h>
42 #include <linux/context_tracking.h>
43 #include <linux/cpuset.h>
44 #include <linux/delayacct.h>
45 #include <linux/init_task.h>
46 #include <linux/interrupt.h>
47 #include <linux/ioprio.h>
48 #include <linux/kallsyms.h>
49 #include <linux/kcov.h>
50 #include <linux/kprobes.h>
51 #include <linux/llist_api.h>
52 #include <linux/mmu_context.h>
53 #include <linux/mmzone.h>
54 #include <linux/mutex_api.h>
55 #include <linux/nmi.h>
56 #include <linux/nospec.h>
57 #include <linux/perf_event_api.h>
58 #include <linux/profile.h>
59 #include <linux/psi.h>
60 #include <linux/rcuwait_api.h>
61 #include <linux/rseq.h>
62 #include <linux/sched/wake_q.h>
63 #include <linux/scs.h>
64 #include <linux/slab.h>
65 #include <linux/syscalls.h>
66 #include <linux/vtime.h>
67 #include <linux/wait_api.h>
68 #include <linux/workqueue_api.h>
69 
70 #ifdef CONFIG_PREEMPT_DYNAMIC
71 # ifdef CONFIG_GENERIC_ENTRY
72 #  include <linux/entry-common.h>
73 # endif
74 #endif
75 
76 #include <uapi/linux/sched/types.h>
77 
78 #include <asm/irq_regs.h>
79 #include <asm/switch_to.h>
80 #include <asm/tlb.h>
81 
82 #define CREATE_TRACE_POINTS
83 #include <linux/sched/rseq_api.h>
84 #include <trace/events/sched.h>
85 #include <trace/events/ipi.h>
86 #undef CREATE_TRACE_POINTS
87 
88 #include "sched.h"
89 #include "stats.h"
90 
91 #include "autogroup.h"
92 #include "pelt.h"
93 #include "smp.h"
94 #include "stats.h"
95 
96 #include "../workqueue_internal.h"
97 #include "../../io_uring/io-wq.h"
98 #include "../smpboot.h"
99 
100 EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpu);
101 EXPORT_TRACEPOINT_SYMBOL_GPL(ipi_send_cpumask);
102 
103 /*
104  * Export tracepoints that act as a bare tracehook (ie: have no trace event
105  * associated with them) to allow external modules to probe them.
106  */
107 EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_cfs_tp);
108 EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_rt_tp);
109 EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_dl_tp);
110 EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_irq_tp);
111 EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_se_tp);
112 EXPORT_TRACEPOINT_SYMBOL_GPL(pelt_hw_tp);
113 EXPORT_TRACEPOINT_SYMBOL_GPL(sched_cpu_capacity_tp);
114 EXPORT_TRACEPOINT_SYMBOL_GPL(sched_overutilized_tp);
115 EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_cfs_tp);
116 EXPORT_TRACEPOINT_SYMBOL_GPL(sched_util_est_se_tp);
117 EXPORT_TRACEPOINT_SYMBOL_GPL(sched_update_nr_running_tp);
118 EXPORT_TRACEPOINT_SYMBOL_GPL(sched_compute_energy_tp);
119 
120 DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
121 
122 #ifdef CONFIG_SCHED_DEBUG
123 /*
124  * Debugging: various feature bits
125  *
126  * If SCHED_DEBUG is disabled, each compilation unit has its own copy of
127  * sysctl_sched_features, defined in sched.h, to allow constants propagation
128  * at compile time and compiler optimization based on features default.
129  */
130 #define SCHED_FEAT(name, enabled)	\
131 	(1UL << __SCHED_FEAT_##name) * enabled |
132 const_debug unsigned int sysctl_sched_features =
133 #include "features.h"
134 	0;
135 #undef SCHED_FEAT
136 
137 /*
138  * Print a warning if need_resched is set for the given duration (if
139  * LATENCY_WARN is enabled).
140  *
141  * If sysctl_resched_latency_warn_once is set, only one warning will be shown
142  * per boot.
143  */
144 __read_mostly int sysctl_resched_latency_warn_ms = 100;
145 __read_mostly int sysctl_resched_latency_warn_once = 1;
146 #endif /* CONFIG_SCHED_DEBUG */
147 
148 /*
149  * Number of tasks to iterate in a single balance run.
150  * Limited because this is done with IRQs disabled.
151  */
152 const_debug unsigned int sysctl_sched_nr_migrate = SCHED_NR_MIGRATE_BREAK;
153 
154 __read_mostly int scheduler_running;
155 
156 #ifdef CONFIG_SCHED_CORE
157 
158 DEFINE_STATIC_KEY_FALSE(__sched_core_enabled);
159 
160 /* kernel prio, less is more */
161 static inline int __task_prio(const struct task_struct *p)
162 {
163 	if (p->sched_class == &stop_sched_class) /* trumps deadline */
164 		return -2;
165 
166 	if (p->dl_server)
167 		return -1; /* deadline */
168 
169 	if (rt_or_dl_prio(p->prio))
170 		return p->prio; /* [-1, 99] */
171 
172 	if (p->sched_class == &idle_sched_class)
173 		return MAX_RT_PRIO + NICE_WIDTH; /* 140 */
174 
175 	if (task_on_scx(p))
176 		return MAX_RT_PRIO + MAX_NICE + 1; /* 120, squash ext */
177 
178 	return MAX_RT_PRIO + MAX_NICE; /* 119, squash fair */
179 }
180 
181 /*
182  * l(a,b)
183  * le(a,b) := !l(b,a)
184  * g(a,b)  := l(b,a)
185  * ge(a,b) := !l(a,b)
186  */
187 
188 /* real prio, less is less */
189 static inline bool prio_less(const struct task_struct *a,
190 			     const struct task_struct *b, bool in_fi)
191 {
192 
193 	int pa = __task_prio(a), pb = __task_prio(b);
194 
195 	if (-pa < -pb)
196 		return true;
197 
198 	if (-pb < -pa)
199 		return false;
200 
201 	if (pa == -1) { /* dl_prio() doesn't work because of stop_class above */
202 		const struct sched_dl_entity *a_dl, *b_dl;
203 
204 		a_dl = &a->dl;
205 		/*
206 		 * Since,'a' and 'b' can be CFS tasks served by DL server,
207 		 * __task_prio() can return -1 (for DL) even for those. In that
208 		 * case, get to the dl_server's DL entity.
209 		 */
210 		if (a->dl_server)
211 			a_dl = a->dl_server;
212 
213 		b_dl = &b->dl;
214 		if (b->dl_server)
215 			b_dl = b->dl_server;
216 
217 		return !dl_time_before(a_dl->deadline, b_dl->deadline);
218 	}
219 
220 	if (pa == MAX_RT_PRIO + MAX_NICE)	/* fair */
221 		return cfs_prio_less(a, b, in_fi);
222 
223 #ifdef CONFIG_SCHED_CLASS_EXT
224 	if (pa == MAX_RT_PRIO + MAX_NICE + 1)	/* ext */
225 		return scx_prio_less(a, b, in_fi);
226 #endif
227 
228 	return false;
229 }
230 
231 static inline bool __sched_core_less(const struct task_struct *a,
232 				     const struct task_struct *b)
233 {
234 	if (a->core_cookie < b->core_cookie)
235 		return true;
236 
237 	if (a->core_cookie > b->core_cookie)
238 		return false;
239 
240 	/* flip prio, so high prio is leftmost */
241 	if (prio_less(b, a, !!task_rq(a)->core->core_forceidle_count))
242 		return true;
243 
244 	return false;
245 }
246 
247 #define __node_2_sc(node) rb_entry((node), struct task_struct, core_node)
248 
249 static inline bool rb_sched_core_less(struct rb_node *a, const struct rb_node *b)
250 {
251 	return __sched_core_less(__node_2_sc(a), __node_2_sc(b));
252 }
253 
254 static inline int rb_sched_core_cmp(const void *key, const struct rb_node *node)
255 {
256 	const struct task_struct *p = __node_2_sc(node);
257 	unsigned long cookie = (unsigned long)key;
258 
259 	if (cookie < p->core_cookie)
260 		return -1;
261 
262 	if (cookie > p->core_cookie)
263 		return 1;
264 
265 	return 0;
266 }
267 
268 void sched_core_enqueue(struct rq *rq, struct task_struct *p)
269 {
270 	if (p->se.sched_delayed)
271 		return;
272 
273 	rq->core->core_task_seq++;
274 
275 	if (!p->core_cookie)
276 		return;
277 
278 	rb_add(&p->core_node, &rq->core_tree, rb_sched_core_less);
279 }
280 
281 void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags)
282 {
283 	if (p->se.sched_delayed)
284 		return;
285 
286 	rq->core->core_task_seq++;
287 
288 	if (sched_core_enqueued(p)) {
289 		rb_erase(&p->core_node, &rq->core_tree);
290 		RB_CLEAR_NODE(&p->core_node);
291 	}
292 
293 	/*
294 	 * Migrating the last task off the cpu, with the cpu in forced idle
295 	 * state. Reschedule to create an accounting edge for forced idle,
296 	 * and re-examine whether the core is still in forced idle state.
297 	 */
298 	if (!(flags & DEQUEUE_SAVE) && rq->nr_running == 1 &&
299 	    rq->core->core_forceidle_count && rq->curr == rq->idle)
300 		resched_curr(rq);
301 }
302 
303 static int sched_task_is_throttled(struct task_struct *p, int cpu)
304 {
305 	if (p->sched_class->task_is_throttled)
306 		return p->sched_class->task_is_throttled(p, cpu);
307 
308 	return 0;
309 }
310 
311 static struct task_struct *sched_core_next(struct task_struct *p, unsigned long cookie)
312 {
313 	struct rb_node *node = &p->core_node;
314 	int cpu = task_cpu(p);
315 
316 	do {
317 		node = rb_next(node);
318 		if (!node)
319 			return NULL;
320 
321 		p = __node_2_sc(node);
322 		if (p->core_cookie != cookie)
323 			return NULL;
324 
325 	} while (sched_task_is_throttled(p, cpu));
326 
327 	return p;
328 }
329 
330 /*
331  * Find left-most (aka, highest priority) and unthrottled task matching @cookie.
332  * If no suitable task is found, NULL will be returned.
333  */
334 static struct task_struct *sched_core_find(struct rq *rq, unsigned long cookie)
335 {
336 	struct task_struct *p;
337 	struct rb_node *node;
338 
339 	node = rb_find_first((void *)cookie, &rq->core_tree, rb_sched_core_cmp);
340 	if (!node)
341 		return NULL;
342 
343 	p = __node_2_sc(node);
344 	if (!sched_task_is_throttled(p, rq->cpu))
345 		return p;
346 
347 	return sched_core_next(p, cookie);
348 }
349 
350 /*
351  * Magic required such that:
352  *
353  *	raw_spin_rq_lock(rq);
354  *	...
355  *	raw_spin_rq_unlock(rq);
356  *
357  * ends up locking and unlocking the _same_ lock, and all CPUs
358  * always agree on what rq has what lock.
359  *
360  * XXX entirely possible to selectively enable cores, don't bother for now.
361  */
362 
363 static DEFINE_MUTEX(sched_core_mutex);
364 static atomic_t sched_core_count;
365 static struct cpumask sched_core_mask;
366 
367 static void sched_core_lock(int cpu, unsigned long *flags)
368 {
369 	const struct cpumask *smt_mask = cpu_smt_mask(cpu);
370 	int t, i = 0;
371 
372 	local_irq_save(*flags);
373 	for_each_cpu(t, smt_mask)
374 		raw_spin_lock_nested(&cpu_rq(t)->__lock, i++);
375 }
376 
377 static void sched_core_unlock(int cpu, unsigned long *flags)
378 {
379 	const struct cpumask *smt_mask = cpu_smt_mask(cpu);
380 	int t;
381 
382 	for_each_cpu(t, smt_mask)
383 		raw_spin_unlock(&cpu_rq(t)->__lock);
384 	local_irq_restore(*flags);
385 }
386 
387 static void __sched_core_flip(bool enabled)
388 {
389 	unsigned long flags;
390 	int cpu, t;
391 
392 	cpus_read_lock();
393 
394 	/*
395 	 * Toggle the online cores, one by one.
396 	 */
397 	cpumask_copy(&sched_core_mask, cpu_online_mask);
398 	for_each_cpu(cpu, &sched_core_mask) {
399 		const struct cpumask *smt_mask = cpu_smt_mask(cpu);
400 
401 		sched_core_lock(cpu, &flags);
402 
403 		for_each_cpu(t, smt_mask)
404 			cpu_rq(t)->core_enabled = enabled;
405 
406 		cpu_rq(cpu)->core->core_forceidle_start = 0;
407 
408 		sched_core_unlock(cpu, &flags);
409 
410 		cpumask_andnot(&sched_core_mask, &sched_core_mask, smt_mask);
411 	}
412 
413 	/*
414 	 * Toggle the offline CPUs.
415 	 */
416 	for_each_cpu_andnot(cpu, cpu_possible_mask, cpu_online_mask)
417 		cpu_rq(cpu)->core_enabled = enabled;
418 
419 	cpus_read_unlock();
420 }
421 
422 static void sched_core_assert_empty(void)
423 {
424 	int cpu;
425 
426 	for_each_possible_cpu(cpu)
427 		WARN_ON_ONCE(!RB_EMPTY_ROOT(&cpu_rq(cpu)->core_tree));
428 }
429 
430 static void __sched_core_enable(void)
431 {
432 	static_branch_enable(&__sched_core_enabled);
433 	/*
434 	 * Ensure all previous instances of raw_spin_rq_*lock() have finished
435 	 * and future ones will observe !sched_core_disabled().
436 	 */
437 	synchronize_rcu();
438 	__sched_core_flip(true);
439 	sched_core_assert_empty();
440 }
441 
442 static void __sched_core_disable(void)
443 {
444 	sched_core_assert_empty();
445 	__sched_core_flip(false);
446 	static_branch_disable(&__sched_core_enabled);
447 }
448 
449 void sched_core_get(void)
450 {
451 	if (atomic_inc_not_zero(&sched_core_count))
452 		return;
453 
454 	mutex_lock(&sched_core_mutex);
455 	if (!atomic_read(&sched_core_count))
456 		__sched_core_enable();
457 
458 	smp_mb__before_atomic();
459 	atomic_inc(&sched_core_count);
460 	mutex_unlock(&sched_core_mutex);
461 }
462 
463 static void __sched_core_put(struct work_struct *work)
464 {
465 	if (atomic_dec_and_mutex_lock(&sched_core_count, &sched_core_mutex)) {
466 		__sched_core_disable();
467 		mutex_unlock(&sched_core_mutex);
468 	}
469 }
470 
471 void sched_core_put(void)
472 {
473 	static DECLARE_WORK(_work, __sched_core_put);
474 
475 	/*
476 	 * "There can be only one"
477 	 *
478 	 * Either this is the last one, or we don't actually need to do any
479 	 * 'work'. If it is the last *again*, we rely on
480 	 * WORK_STRUCT_PENDING_BIT.
481 	 */
482 	if (!atomic_add_unless(&sched_core_count, -1, 1))
483 		schedule_work(&_work);
484 }
485 
486 #else /* !CONFIG_SCHED_CORE */
487 
488 static inline void sched_core_enqueue(struct rq *rq, struct task_struct *p) { }
489 static inline void
490 sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags) { }
491 
492 #endif /* CONFIG_SCHED_CORE */
493 
494 /*
495  * Serialization rules:
496  *
497  * Lock order:
498  *
499  *   p->pi_lock
500  *     rq->lock
501  *       hrtimer_cpu_base->lock (hrtimer_start() for bandwidth controls)
502  *
503  *  rq1->lock
504  *    rq2->lock  where: rq1 < rq2
505  *
506  * Regular state:
507  *
508  * Normal scheduling state is serialized by rq->lock. __schedule() takes the
509  * local CPU's rq->lock, it optionally removes the task from the runqueue and
510  * always looks at the local rq data structures to find the most eligible task
511  * to run next.
512  *
513  * Task enqueue is also under rq->lock, possibly taken from another CPU.
514  * Wakeups from another LLC domain might use an IPI to transfer the enqueue to
515  * the local CPU to avoid bouncing the runqueue state around [ see
516  * ttwu_queue_wakelist() ]
517  *
518  * Task wakeup, specifically wakeups that involve migration, are horribly
519  * complicated to avoid having to take two rq->locks.
520  *
521  * Special state:
522  *
523  * System-calls and anything external will use task_rq_lock() which acquires
524  * both p->pi_lock and rq->lock. As a consequence the state they change is
525  * stable while holding either lock:
526  *
527  *  - sched_setaffinity()/
528  *    set_cpus_allowed_ptr():	p->cpus_ptr, p->nr_cpus_allowed
529  *  - set_user_nice():		p->se.load, p->*prio
530  *  - __sched_setscheduler():	p->sched_class, p->policy, p->*prio,
531  *				p->se.load, p->rt_priority,
532  *				p->dl.dl_{runtime, deadline, period, flags, bw, density}
533  *  - sched_setnuma():		p->numa_preferred_nid
534  *  - sched_move_task():	p->sched_task_group
535  *  - uclamp_update_active()	p->uclamp*
536  *
537  * p->state <- TASK_*:
538  *
539  *   is changed locklessly using set_current_state(), __set_current_state() or
540  *   set_special_state(), see their respective comments, or by
541  *   try_to_wake_up(). This latter uses p->pi_lock to serialize against
542  *   concurrent self.
543  *
544  * p->on_rq <- { 0, 1 = TASK_ON_RQ_QUEUED, 2 = TASK_ON_RQ_MIGRATING }:
545  *
546  *   is set by activate_task() and cleared by deactivate_task(), under
547  *   rq->lock. Non-zero indicates the task is runnable, the special
548  *   ON_RQ_MIGRATING state is used for migration without holding both
549  *   rq->locks. It indicates task_cpu() is not stable, see task_rq_lock().
550  *
551  *   Additionally it is possible to be ->on_rq but still be considered not
552  *   runnable when p->se.sched_delayed is true. These tasks are on the runqueue
553  *   but will be dequeued as soon as they get picked again. See the
554  *   task_is_runnable() helper.
555  *
556  * p->on_cpu <- { 0, 1 }:
557  *
558  *   is set by prepare_task() and cleared by finish_task() such that it will be
559  *   set before p is scheduled-in and cleared after p is scheduled-out, both
560  *   under rq->lock. Non-zero indicates the task is running on its CPU.
561  *
562  *   [ The astute reader will observe that it is possible for two tasks on one
563  *     CPU to have ->on_cpu = 1 at the same time. ]
564  *
565  * task_cpu(p): is changed by set_task_cpu(), the rules are:
566  *
567  *  - Don't call set_task_cpu() on a blocked task:
568  *
569  *    We don't care what CPU we're not running on, this simplifies hotplug,
570  *    the CPU assignment of blocked tasks isn't required to be valid.
571  *
572  *  - for try_to_wake_up(), called under p->pi_lock:
573  *
574  *    This allows try_to_wake_up() to only take one rq->lock, see its comment.
575  *
576  *  - for migration called under rq->lock:
577  *    [ see task_on_rq_migrating() in task_rq_lock() ]
578  *
579  *    o move_queued_task()
580  *    o detach_task()
581  *
582  *  - for migration called under double_rq_lock():
583  *
584  *    o __migrate_swap_task()
585  *    o push_rt_task() / pull_rt_task()
586  *    o push_dl_task() / pull_dl_task()
587  *    o dl_task_offline_migration()
588  *
589  */
590 
591 void raw_spin_rq_lock_nested(struct rq *rq, int subclass)
592 {
593 	raw_spinlock_t *lock;
594 
595 	/* Matches synchronize_rcu() in __sched_core_enable() */
596 	preempt_disable();
597 	if (sched_core_disabled()) {
598 		raw_spin_lock_nested(&rq->__lock, subclass);
599 		/* preempt_count *MUST* be > 1 */
600 		preempt_enable_no_resched();
601 		return;
602 	}
603 
604 	for (;;) {
605 		lock = __rq_lockp(rq);
606 		raw_spin_lock_nested(lock, subclass);
607 		if (likely(lock == __rq_lockp(rq))) {
608 			/* preempt_count *MUST* be > 1 */
609 			preempt_enable_no_resched();
610 			return;
611 		}
612 		raw_spin_unlock(lock);
613 	}
614 }
615 
616 bool raw_spin_rq_trylock(struct rq *rq)
617 {
618 	raw_spinlock_t *lock;
619 	bool ret;
620 
621 	/* Matches synchronize_rcu() in __sched_core_enable() */
622 	preempt_disable();
623 	if (sched_core_disabled()) {
624 		ret = raw_spin_trylock(&rq->__lock);
625 		preempt_enable();
626 		return ret;
627 	}
628 
629 	for (;;) {
630 		lock = __rq_lockp(rq);
631 		ret = raw_spin_trylock(lock);
632 		if (!ret || (likely(lock == __rq_lockp(rq)))) {
633 			preempt_enable();
634 			return ret;
635 		}
636 		raw_spin_unlock(lock);
637 	}
638 }
639 
640 void raw_spin_rq_unlock(struct rq *rq)
641 {
642 	raw_spin_unlock(rq_lockp(rq));
643 }
644 
645 #ifdef CONFIG_SMP
646 /*
647  * double_rq_lock - safely lock two runqueues
648  */
649 void double_rq_lock(struct rq *rq1, struct rq *rq2)
650 {
651 	lockdep_assert_irqs_disabled();
652 
653 	if (rq_order_less(rq2, rq1))
654 		swap(rq1, rq2);
655 
656 	raw_spin_rq_lock(rq1);
657 	if (__rq_lockp(rq1) != __rq_lockp(rq2))
658 		raw_spin_rq_lock_nested(rq2, SINGLE_DEPTH_NESTING);
659 
660 	double_rq_clock_clear_update(rq1, rq2);
661 }
662 #endif
663 
664 /*
665  * __task_rq_lock - lock the rq @p resides on.
666  */
667 struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
668 	__acquires(rq->lock)
669 {
670 	struct rq *rq;
671 
672 	lockdep_assert_held(&p->pi_lock);
673 
674 	for (;;) {
675 		rq = task_rq(p);
676 		raw_spin_rq_lock(rq);
677 		if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
678 			rq_pin_lock(rq, rf);
679 			return rq;
680 		}
681 		raw_spin_rq_unlock(rq);
682 
683 		while (unlikely(task_on_rq_migrating(p)))
684 			cpu_relax();
685 	}
686 }
687 
688 /*
689  * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
690  */
691 struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
692 	__acquires(p->pi_lock)
693 	__acquires(rq->lock)
694 {
695 	struct rq *rq;
696 
697 	for (;;) {
698 		raw_spin_lock_irqsave(&p->pi_lock, rf->flags);
699 		rq = task_rq(p);
700 		raw_spin_rq_lock(rq);
701 		/*
702 		 *	move_queued_task()		task_rq_lock()
703 		 *
704 		 *	ACQUIRE (rq->lock)
705 		 *	[S] ->on_rq = MIGRATING		[L] rq = task_rq()
706 		 *	WMB (__set_task_cpu())		ACQUIRE (rq->lock);
707 		 *	[S] ->cpu = new_cpu		[L] task_rq()
708 		 *					[L] ->on_rq
709 		 *	RELEASE (rq->lock)
710 		 *
711 		 * If we observe the old CPU in task_rq_lock(), the acquire of
712 		 * the old rq->lock will fully serialize against the stores.
713 		 *
714 		 * If we observe the new CPU in task_rq_lock(), the address
715 		 * dependency headed by '[L] rq = task_rq()' and the acquire
716 		 * will pair with the WMB to ensure we then also see migrating.
717 		 */
718 		if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
719 			rq_pin_lock(rq, rf);
720 			return rq;
721 		}
722 		raw_spin_rq_unlock(rq);
723 		raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
724 
725 		while (unlikely(task_on_rq_migrating(p)))
726 			cpu_relax();
727 	}
728 }
729 
730 /*
731  * RQ-clock updating methods:
732  */
733 
734 static void update_rq_clock_task(struct rq *rq, s64 delta)
735 {
736 /*
737  * In theory, the compile should just see 0 here, and optimize out the call
738  * to sched_rt_avg_update. But I don't trust it...
739  */
740 	s64 __maybe_unused steal = 0, irq_delta = 0;
741 
742 #ifdef CONFIG_IRQ_TIME_ACCOUNTING
743 	irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
744 
745 	/*
746 	 * Since irq_time is only updated on {soft,}irq_exit, we might run into
747 	 * this case when a previous update_rq_clock() happened inside a
748 	 * {soft,}IRQ region.
749 	 *
750 	 * When this happens, we stop ->clock_task and only update the
751 	 * prev_irq_time stamp to account for the part that fit, so that a next
752 	 * update will consume the rest. This ensures ->clock_task is
753 	 * monotonic.
754 	 *
755 	 * It does however cause some slight miss-attribution of {soft,}IRQ
756 	 * time, a more accurate solution would be to update the irq_time using
757 	 * the current rq->clock timestamp, except that would require using
758 	 * atomic ops.
759 	 */
760 	if (irq_delta > delta)
761 		irq_delta = delta;
762 
763 	rq->prev_irq_time += irq_delta;
764 	delta -= irq_delta;
765 	delayacct_irq(rq->curr, irq_delta);
766 #endif
767 #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
768 	if (static_key_false((&paravirt_steal_rq_enabled))) {
769 		steal = paravirt_steal_clock(cpu_of(rq));
770 		steal -= rq->prev_steal_time_rq;
771 
772 		if (unlikely(steal > delta))
773 			steal = delta;
774 
775 		rq->prev_steal_time_rq += steal;
776 		delta -= steal;
777 	}
778 #endif
779 
780 	rq->clock_task += delta;
781 
782 #ifdef CONFIG_HAVE_SCHED_AVG_IRQ
783 	if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
784 		update_irq_load_avg(rq, irq_delta + steal);
785 #endif
786 	update_rq_clock_pelt(rq, delta);
787 }
788 
789 void update_rq_clock(struct rq *rq)
790 {
791 	s64 delta;
792 
793 	lockdep_assert_rq_held(rq);
794 
795 	if (rq->clock_update_flags & RQCF_ACT_SKIP)
796 		return;
797 
798 #ifdef CONFIG_SCHED_DEBUG
799 	if (sched_feat(WARN_DOUBLE_CLOCK))
800 		SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED);
801 	rq->clock_update_flags |= RQCF_UPDATED;
802 #endif
803 
804 	delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
805 	if (delta < 0)
806 		return;
807 	rq->clock += delta;
808 	update_rq_clock_task(rq, delta);
809 }
810 
811 #ifdef CONFIG_SCHED_HRTICK
812 /*
813  * Use HR-timers to deliver accurate preemption points.
814  */
815 
816 static void hrtick_clear(struct rq *rq)
817 {
818 	if (hrtimer_active(&rq->hrtick_timer))
819 		hrtimer_cancel(&rq->hrtick_timer);
820 }
821 
822 /*
823  * High-resolution timer tick.
824  * Runs from hardirq context with interrupts disabled.
825  */
826 static enum hrtimer_restart hrtick(struct hrtimer *timer)
827 {
828 	struct rq *rq = container_of(timer, struct rq, hrtick_timer);
829 	struct rq_flags rf;
830 
831 	WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
832 
833 	rq_lock(rq, &rf);
834 	update_rq_clock(rq);
835 	rq->curr->sched_class->task_tick(rq, rq->curr, 1);
836 	rq_unlock(rq, &rf);
837 
838 	return HRTIMER_NORESTART;
839 }
840 
841 #ifdef CONFIG_SMP
842 
843 static void __hrtick_restart(struct rq *rq)
844 {
845 	struct hrtimer *timer = &rq->hrtick_timer;
846 	ktime_t time = rq->hrtick_time;
847 
848 	hrtimer_start(timer, time, HRTIMER_MODE_ABS_PINNED_HARD);
849 }
850 
851 /*
852  * called from hardirq (IPI) context
853  */
854 static void __hrtick_start(void *arg)
855 {
856 	struct rq *rq = arg;
857 	struct rq_flags rf;
858 
859 	rq_lock(rq, &rf);
860 	__hrtick_restart(rq);
861 	rq_unlock(rq, &rf);
862 }
863 
864 /*
865  * Called to set the hrtick timer state.
866  *
867  * called with rq->lock held and IRQs disabled
868  */
869 void hrtick_start(struct rq *rq, u64 delay)
870 {
871 	struct hrtimer *timer = &rq->hrtick_timer;
872 	s64 delta;
873 
874 	/*
875 	 * Don't schedule slices shorter than 10000ns, that just
876 	 * doesn't make sense and can cause timer DoS.
877 	 */
878 	delta = max_t(s64, delay, 10000LL);
879 	rq->hrtick_time = ktime_add_ns(timer->base->get_time(), delta);
880 
881 	if (rq == this_rq())
882 		__hrtick_restart(rq);
883 	else
884 		smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
885 }
886 
887 #else
888 /*
889  * Called to set the hrtick timer state.
890  *
891  * called with rq->lock held and IRQs disabled
892  */
893 void hrtick_start(struct rq *rq, u64 delay)
894 {
895 	/*
896 	 * Don't schedule slices shorter than 10000ns, that just
897 	 * doesn't make sense. Rely on vruntime for fairness.
898 	 */
899 	delay = max_t(u64, delay, 10000LL);
900 	hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
901 		      HRTIMER_MODE_REL_PINNED_HARD);
902 }
903 
904 #endif /* CONFIG_SMP */
905 
906 static void hrtick_rq_init(struct rq *rq)
907 {
908 #ifdef CONFIG_SMP
909 	INIT_CSD(&rq->hrtick_csd, __hrtick_start, rq);
910 #endif
911 	hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
912 	rq->hrtick_timer.function = hrtick;
913 }
914 #else	/* CONFIG_SCHED_HRTICK */
915 static inline void hrtick_clear(struct rq *rq)
916 {
917 }
918 
919 static inline void hrtick_rq_init(struct rq *rq)
920 {
921 }
922 #endif	/* CONFIG_SCHED_HRTICK */
923 
924 /*
925  * try_cmpxchg based fetch_or() macro so it works for different integer types:
926  */
927 #define fetch_or(ptr, mask)						\
928 	({								\
929 		typeof(ptr) _ptr = (ptr);				\
930 		typeof(mask) _mask = (mask);				\
931 		typeof(*_ptr) _val = *_ptr;				\
932 									\
933 		do {							\
934 		} while (!try_cmpxchg(_ptr, &_val, _val | _mask));	\
935 	_val;								\
936 })
937 
938 #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
939 /*
940  * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
941  * this avoids any races wrt polling state changes and thereby avoids
942  * spurious IPIs.
943  */
944 static inline bool set_nr_and_not_polling(struct task_struct *p)
945 {
946 	struct thread_info *ti = task_thread_info(p);
947 	return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
948 }
949 
950 /*
951  * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
952  *
953  * If this returns true, then the idle task promises to call
954  * sched_ttwu_pending() and reschedule soon.
955  */
956 static bool set_nr_if_polling(struct task_struct *p)
957 {
958 	struct thread_info *ti = task_thread_info(p);
959 	typeof(ti->flags) val = READ_ONCE(ti->flags);
960 
961 	do {
962 		if (!(val & _TIF_POLLING_NRFLAG))
963 			return false;
964 		if (val & _TIF_NEED_RESCHED)
965 			return true;
966 	} while (!try_cmpxchg(&ti->flags, &val, val | _TIF_NEED_RESCHED));
967 
968 	return true;
969 }
970 
971 #else
972 static inline bool set_nr_and_not_polling(struct task_struct *p)
973 {
974 	set_tsk_need_resched(p);
975 	return true;
976 }
977 
978 #ifdef CONFIG_SMP
979 static inline bool set_nr_if_polling(struct task_struct *p)
980 {
981 	return false;
982 }
983 #endif
984 #endif
985 
986 static bool __wake_q_add(struct wake_q_head *head, struct task_struct *task)
987 {
988 	struct wake_q_node *node = &task->wake_q;
989 
990 	/*
991 	 * Atomically grab the task, if ->wake_q is !nil already it means
992 	 * it's already queued (either by us or someone else) and will get the
993 	 * wakeup due to that.
994 	 *
995 	 * In order to ensure that a pending wakeup will observe our pending
996 	 * state, even in the failed case, an explicit smp_mb() must be used.
997 	 */
998 	smp_mb__before_atomic();
999 	if (unlikely(cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL)))
1000 		return false;
1001 
1002 	/*
1003 	 * The head is context local, there can be no concurrency.
1004 	 */
1005 	*head->lastp = node;
1006 	head->lastp = &node->next;
1007 	return true;
1008 }
1009 
1010 /**
1011  * wake_q_add() - queue a wakeup for 'later' waking.
1012  * @head: the wake_q_head to add @task to
1013  * @task: the task to queue for 'later' wakeup
1014  *
1015  * Queue a task for later wakeup, most likely by the wake_up_q() call in the
1016  * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
1017  * instantly.
1018  *
1019  * This function must be used as-if it were wake_up_process(); IOW the task
1020  * must be ready to be woken at this location.
1021  */
1022 void wake_q_add(struct wake_q_head *head, struct task_struct *task)
1023 {
1024 	if (__wake_q_add(head, task))
1025 		get_task_struct(task);
1026 }
1027 
1028 /**
1029  * wake_q_add_safe() - safely queue a wakeup for 'later' waking.
1030  * @head: the wake_q_head to add @task to
1031  * @task: the task to queue for 'later' wakeup
1032  *
1033  * Queue a task for later wakeup, most likely by the wake_up_q() call in the
1034  * same context, _HOWEVER_ this is not guaranteed, the wakeup can come
1035  * instantly.
1036  *
1037  * This function must be used as-if it were wake_up_process(); IOW the task
1038  * must be ready to be woken at this location.
1039  *
1040  * This function is essentially a task-safe equivalent to wake_q_add(). Callers
1041  * that already hold reference to @task can call the 'safe' version and trust
1042  * wake_q to do the right thing depending whether or not the @task is already
1043  * queued for wakeup.
1044  */
1045 void wake_q_add_safe(struct wake_q_head *head, struct task_struct *task)
1046 {
1047 	if (!__wake_q_add(head, task))
1048 		put_task_struct(task);
1049 }
1050 
1051 void wake_up_q(struct wake_q_head *head)
1052 {
1053 	struct wake_q_node *node = head->first;
1054 
1055 	while (node != WAKE_Q_TAIL) {
1056 		struct task_struct *task;
1057 
1058 		task = container_of(node, struct task_struct, wake_q);
1059 		/* Task can safely be re-inserted now: */
1060 		node = node->next;
1061 		task->wake_q.next = NULL;
1062 
1063 		/*
1064 		 * wake_up_process() executes a full barrier, which pairs with
1065 		 * the queueing in wake_q_add() so as not to miss wakeups.
1066 		 */
1067 		wake_up_process(task);
1068 		put_task_struct(task);
1069 	}
1070 }
1071 
1072 /*
1073  * resched_curr - mark rq's current task 'to be rescheduled now'.
1074  *
1075  * On UP this means the setting of the need_resched flag, on SMP it
1076  * might also involve a cross-CPU call to trigger the scheduler on
1077  * the target CPU.
1078  */
1079 void resched_curr(struct rq *rq)
1080 {
1081 	struct task_struct *curr = rq->curr;
1082 	int cpu;
1083 
1084 	lockdep_assert_rq_held(rq);
1085 
1086 	if (test_tsk_need_resched(curr))
1087 		return;
1088 
1089 	cpu = cpu_of(rq);
1090 
1091 	if (cpu == smp_processor_id()) {
1092 		set_tsk_need_resched(curr);
1093 		set_preempt_need_resched();
1094 		return;
1095 	}
1096 
1097 	if (set_nr_and_not_polling(curr))
1098 		smp_send_reschedule(cpu);
1099 	else
1100 		trace_sched_wake_idle_without_ipi(cpu);
1101 }
1102 
1103 void resched_cpu(int cpu)
1104 {
1105 	struct rq *rq = cpu_rq(cpu);
1106 	unsigned long flags;
1107 
1108 	raw_spin_rq_lock_irqsave(rq, flags);
1109 	if (cpu_online(cpu) || cpu == smp_processor_id())
1110 		resched_curr(rq);
1111 	raw_spin_rq_unlock_irqrestore(rq, flags);
1112 }
1113 
1114 #ifdef CONFIG_SMP
1115 #ifdef CONFIG_NO_HZ_COMMON
1116 /*
1117  * In the semi idle case, use the nearest busy CPU for migrating timers
1118  * from an idle CPU.  This is good for power-savings.
1119  *
1120  * We don't do similar optimization for completely idle system, as
1121  * selecting an idle CPU will add more delays to the timers than intended
1122  * (as that CPU's timer base may not be up to date wrt jiffies etc).
1123  */
1124 int get_nohz_timer_target(void)
1125 {
1126 	int i, cpu = smp_processor_id(), default_cpu = -1;
1127 	struct sched_domain *sd;
1128 	const struct cpumask *hk_mask;
1129 
1130 	if (housekeeping_cpu(cpu, HK_TYPE_TIMER)) {
1131 		if (!idle_cpu(cpu))
1132 			return cpu;
1133 		default_cpu = cpu;
1134 	}
1135 
1136 	hk_mask = housekeeping_cpumask(HK_TYPE_TIMER);
1137 
1138 	guard(rcu)();
1139 
1140 	for_each_domain(cpu, sd) {
1141 		for_each_cpu_and(i, sched_domain_span(sd), hk_mask) {
1142 			if (cpu == i)
1143 				continue;
1144 
1145 			if (!idle_cpu(i))
1146 				return i;
1147 		}
1148 	}
1149 
1150 	if (default_cpu == -1)
1151 		default_cpu = housekeeping_any_cpu(HK_TYPE_TIMER);
1152 
1153 	return default_cpu;
1154 }
1155 
1156 /*
1157  * When add_timer_on() enqueues a timer into the timer wheel of an
1158  * idle CPU then this timer might expire before the next timer event
1159  * which is scheduled to wake up that CPU. In case of a completely
1160  * idle system the next event might even be infinite time into the
1161  * future. wake_up_idle_cpu() ensures that the CPU is woken up and
1162  * leaves the inner idle loop so the newly added timer is taken into
1163  * account when the CPU goes back to idle and evaluates the timer
1164  * wheel for the next timer event.
1165  */
1166 static void wake_up_idle_cpu(int cpu)
1167 {
1168 	struct rq *rq = cpu_rq(cpu);
1169 
1170 	if (cpu == smp_processor_id())
1171 		return;
1172 
1173 	/*
1174 	 * Set TIF_NEED_RESCHED and send an IPI if in the non-polling
1175 	 * part of the idle loop. This forces an exit from the idle loop
1176 	 * and a round trip to schedule(). Now this could be optimized
1177 	 * because a simple new idle loop iteration is enough to
1178 	 * re-evaluate the next tick. Provided some re-ordering of tick
1179 	 * nohz functions that would need to follow TIF_NR_POLLING
1180 	 * clearing:
1181 	 *
1182 	 * - On most architectures, a simple fetch_or on ti::flags with a
1183 	 *   "0" value would be enough to know if an IPI needs to be sent.
1184 	 *
1185 	 * - x86 needs to perform a last need_resched() check between
1186 	 *   monitor and mwait which doesn't take timers into account.
1187 	 *   There a dedicated TIF_TIMER flag would be required to
1188 	 *   fetch_or here and be checked along with TIF_NEED_RESCHED
1189 	 *   before mwait().
1190 	 *
1191 	 * However, remote timer enqueue is not such a frequent event
1192 	 * and testing of the above solutions didn't appear to report
1193 	 * much benefits.
1194 	 */
1195 	if (set_nr_and_not_polling(rq->idle))
1196 		smp_send_reschedule(cpu);
1197 	else
1198 		trace_sched_wake_idle_without_ipi(cpu);
1199 }
1200 
1201 static bool wake_up_full_nohz_cpu(int cpu)
1202 {
1203 	/*
1204 	 * We just need the target to call irq_exit() and re-evaluate
1205 	 * the next tick. The nohz full kick at least implies that.
1206 	 * If needed we can still optimize that later with an
1207 	 * empty IRQ.
1208 	 */
1209 	if (cpu_is_offline(cpu))
1210 		return true;  /* Don't try to wake offline CPUs. */
1211 	if (tick_nohz_full_cpu(cpu)) {
1212 		if (cpu != smp_processor_id() ||
1213 		    tick_nohz_tick_stopped())
1214 			tick_nohz_full_kick_cpu(cpu);
1215 		return true;
1216 	}
1217 
1218 	return false;
1219 }
1220 
1221 /*
1222  * Wake up the specified CPU.  If the CPU is going offline, it is the
1223  * caller's responsibility to deal with the lost wakeup, for example,
1224  * by hooking into the CPU_DEAD notifier like timers and hrtimers do.
1225  */
1226 void wake_up_nohz_cpu(int cpu)
1227 {
1228 	if (!wake_up_full_nohz_cpu(cpu))
1229 		wake_up_idle_cpu(cpu);
1230 }
1231 
1232 static void nohz_csd_func(void *info)
1233 {
1234 	struct rq *rq = info;
1235 	int cpu = cpu_of(rq);
1236 	unsigned int flags;
1237 
1238 	/*
1239 	 * Release the rq::nohz_csd.
1240 	 */
1241 	flags = atomic_fetch_andnot(NOHZ_KICK_MASK | NOHZ_NEWILB_KICK, nohz_flags(cpu));
1242 	WARN_ON(!(flags & NOHZ_KICK_MASK));
1243 
1244 	rq->idle_balance = idle_cpu(cpu);
1245 	if (rq->idle_balance && !need_resched()) {
1246 		rq->nohz_idle_balance = flags;
1247 		raise_softirq_irqoff(SCHED_SOFTIRQ);
1248 	}
1249 }
1250 
1251 #endif /* CONFIG_NO_HZ_COMMON */
1252 
1253 #ifdef CONFIG_NO_HZ_FULL
1254 static inline bool __need_bw_check(struct rq *rq, struct task_struct *p)
1255 {
1256 	if (rq->nr_running != 1)
1257 		return false;
1258 
1259 	if (p->sched_class != &fair_sched_class)
1260 		return false;
1261 
1262 	if (!task_on_rq_queued(p))
1263 		return false;
1264 
1265 	return true;
1266 }
1267 
1268 bool sched_can_stop_tick(struct rq *rq)
1269 {
1270 	int fifo_nr_running;
1271 
1272 	/* Deadline tasks, even if single, need the tick */
1273 	if (rq->dl.dl_nr_running)
1274 		return false;
1275 
1276 	/*
1277 	 * If there are more than one RR tasks, we need the tick to affect the
1278 	 * actual RR behaviour.
1279 	 */
1280 	if (rq->rt.rr_nr_running) {
1281 		if (rq->rt.rr_nr_running == 1)
1282 			return true;
1283 		else
1284 			return false;
1285 	}
1286 
1287 	/*
1288 	 * If there's no RR tasks, but FIFO tasks, we can skip the tick, no
1289 	 * forced preemption between FIFO tasks.
1290 	 */
1291 	fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running;
1292 	if (fifo_nr_running)
1293 		return true;
1294 
1295 	/*
1296 	 * If there are no DL,RR/FIFO tasks, there must only be CFS or SCX tasks
1297 	 * left. For CFS, if there's more than one we need the tick for
1298 	 * involuntary preemption. For SCX, ask.
1299 	 */
1300 	if (scx_enabled() && !scx_can_stop_tick(rq))
1301 		return false;
1302 
1303 	if (rq->cfs.nr_running > 1)
1304 		return false;
1305 
1306 	/*
1307 	 * If there is one task and it has CFS runtime bandwidth constraints
1308 	 * and it's on the cpu now we don't want to stop the tick.
1309 	 * This check prevents clearing the bit if a newly enqueued task here is
1310 	 * dequeued by migrating while the constrained task continues to run.
1311 	 * E.g. going from 2->1 without going through pick_next_task().
1312 	 */
1313 	if (__need_bw_check(rq, rq->curr)) {
1314 		if (cfs_task_bw_constrained(rq->curr))
1315 			return false;
1316 	}
1317 
1318 	return true;
1319 }
1320 #endif /* CONFIG_NO_HZ_FULL */
1321 #endif /* CONFIG_SMP */
1322 
1323 #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
1324 			(defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
1325 /*
1326  * Iterate task_group tree rooted at *from, calling @down when first entering a
1327  * node and @up when leaving it for the final time.
1328  *
1329  * Caller must hold rcu_lock or sufficient equivalent.
1330  */
1331 int walk_tg_tree_from(struct task_group *from,
1332 			     tg_visitor down, tg_visitor up, void *data)
1333 {
1334 	struct task_group *parent, *child;
1335 	int ret;
1336 
1337 	parent = from;
1338 
1339 down:
1340 	ret = (*down)(parent, data);
1341 	if (ret)
1342 		goto out;
1343 	list_for_each_entry_rcu(child, &parent->children, siblings) {
1344 		parent = child;
1345 		goto down;
1346 
1347 up:
1348 		continue;
1349 	}
1350 	ret = (*up)(parent, data);
1351 	if (ret || parent == from)
1352 		goto out;
1353 
1354 	child = parent;
1355 	parent = parent->parent;
1356 	if (parent)
1357 		goto up;
1358 out:
1359 	return ret;
1360 }
1361 
1362 int tg_nop(struct task_group *tg, void *data)
1363 {
1364 	return 0;
1365 }
1366 #endif
1367 
1368 void set_load_weight(struct task_struct *p, bool update_load)
1369 {
1370 	int prio = p->static_prio - MAX_RT_PRIO;
1371 	struct load_weight lw;
1372 
1373 	if (task_has_idle_policy(p)) {
1374 		lw.weight = scale_load(WEIGHT_IDLEPRIO);
1375 		lw.inv_weight = WMULT_IDLEPRIO;
1376 	} else {
1377 		lw.weight = scale_load(sched_prio_to_weight[prio]);
1378 		lw.inv_weight = sched_prio_to_wmult[prio];
1379 	}
1380 
1381 	/*
1382 	 * SCHED_OTHER tasks have to update their load when changing their
1383 	 * weight
1384 	 */
1385 	if (update_load && p->sched_class->reweight_task)
1386 		p->sched_class->reweight_task(task_rq(p), p, &lw);
1387 	else
1388 		p->se.load = lw;
1389 }
1390 
1391 #ifdef CONFIG_UCLAMP_TASK
1392 /*
1393  * Serializes updates of utilization clamp values
1394  *
1395  * The (slow-path) user-space triggers utilization clamp value updates which
1396  * can require updates on (fast-path) scheduler's data structures used to
1397  * support enqueue/dequeue operations.
1398  * While the per-CPU rq lock protects fast-path update operations, user-space
1399  * requests are serialized using a mutex to reduce the risk of conflicting
1400  * updates or API abuses.
1401  */
1402 static DEFINE_MUTEX(uclamp_mutex);
1403 
1404 /* Max allowed minimum utilization */
1405 static unsigned int __maybe_unused sysctl_sched_uclamp_util_min = SCHED_CAPACITY_SCALE;
1406 
1407 /* Max allowed maximum utilization */
1408 static unsigned int __maybe_unused sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE;
1409 
1410 /*
1411  * By default RT tasks run at the maximum performance point/capacity of the
1412  * system. Uclamp enforces this by always setting UCLAMP_MIN of RT tasks to
1413  * SCHED_CAPACITY_SCALE.
1414  *
1415  * This knob allows admins to change the default behavior when uclamp is being
1416  * used. In battery powered devices, particularly, running at the maximum
1417  * capacity and frequency will increase energy consumption and shorten the
1418  * battery life.
1419  *
1420  * This knob only affects RT tasks that their uclamp_se->user_defined == false.
1421  *
1422  * This knob will not override the system default sched_util_clamp_min defined
1423  * above.
1424  */
1425 unsigned int sysctl_sched_uclamp_util_min_rt_default = SCHED_CAPACITY_SCALE;
1426 
1427 /* All clamps are required to be less or equal than these values */
1428 static struct uclamp_se uclamp_default[UCLAMP_CNT];
1429 
1430 /*
1431  * This static key is used to reduce the uclamp overhead in the fast path. It
1432  * primarily disables the call to uclamp_rq_{inc, dec}() in
1433  * enqueue/dequeue_task().
1434  *
1435  * This allows users to continue to enable uclamp in their kernel config with
1436  * minimum uclamp overhead in the fast path.
1437  *
1438  * As soon as userspace modifies any of the uclamp knobs, the static key is
1439  * enabled, since we have an actual users that make use of uclamp
1440  * functionality.
1441  *
1442  * The knobs that would enable this static key are:
1443  *
1444  *   * A task modifying its uclamp value with sched_setattr().
1445  *   * An admin modifying the sysctl_sched_uclamp_{min, max} via procfs.
1446  *   * An admin modifying the cgroup cpu.uclamp.{min, max}
1447  */
1448 DEFINE_STATIC_KEY_FALSE(sched_uclamp_used);
1449 
1450 static inline unsigned int
1451 uclamp_idle_value(struct rq *rq, enum uclamp_id clamp_id,
1452 		  unsigned int clamp_value)
1453 {
1454 	/*
1455 	 * Avoid blocked utilization pushing up the frequency when we go
1456 	 * idle (which drops the max-clamp) by retaining the last known
1457 	 * max-clamp.
1458 	 */
1459 	if (clamp_id == UCLAMP_MAX) {
1460 		rq->uclamp_flags |= UCLAMP_FLAG_IDLE;
1461 		return clamp_value;
1462 	}
1463 
1464 	return uclamp_none(UCLAMP_MIN);
1465 }
1466 
1467 static inline void uclamp_idle_reset(struct rq *rq, enum uclamp_id clamp_id,
1468 				     unsigned int clamp_value)
1469 {
1470 	/* Reset max-clamp retention only on idle exit */
1471 	if (!(rq->uclamp_flags & UCLAMP_FLAG_IDLE))
1472 		return;
1473 
1474 	uclamp_rq_set(rq, clamp_id, clamp_value);
1475 }
1476 
1477 static inline
1478 unsigned int uclamp_rq_max_value(struct rq *rq, enum uclamp_id clamp_id,
1479 				   unsigned int clamp_value)
1480 {
1481 	struct uclamp_bucket *bucket = rq->uclamp[clamp_id].bucket;
1482 	int bucket_id = UCLAMP_BUCKETS - 1;
1483 
1484 	/*
1485 	 * Since both min and max clamps are max aggregated, find the
1486 	 * top most bucket with tasks in.
1487 	 */
1488 	for ( ; bucket_id >= 0; bucket_id--) {
1489 		if (!bucket[bucket_id].tasks)
1490 			continue;
1491 		return bucket[bucket_id].value;
1492 	}
1493 
1494 	/* No tasks -- default clamp values */
1495 	return uclamp_idle_value(rq, clamp_id, clamp_value);
1496 }
1497 
1498 static void __uclamp_update_util_min_rt_default(struct task_struct *p)
1499 {
1500 	unsigned int default_util_min;
1501 	struct uclamp_se *uc_se;
1502 
1503 	lockdep_assert_held(&p->pi_lock);
1504 
1505 	uc_se = &p->uclamp_req[UCLAMP_MIN];
1506 
1507 	/* Only sync if user didn't override the default */
1508 	if (uc_se->user_defined)
1509 		return;
1510 
1511 	default_util_min = sysctl_sched_uclamp_util_min_rt_default;
1512 	uclamp_se_set(uc_se, default_util_min, false);
1513 }
1514 
1515 static void uclamp_update_util_min_rt_default(struct task_struct *p)
1516 {
1517 	if (!rt_task(p))
1518 		return;
1519 
1520 	/* Protect updates to p->uclamp_* */
1521 	guard(task_rq_lock)(p);
1522 	__uclamp_update_util_min_rt_default(p);
1523 }
1524 
1525 static inline struct uclamp_se
1526 uclamp_tg_restrict(struct task_struct *p, enum uclamp_id clamp_id)
1527 {
1528 	/* Copy by value as we could modify it */
1529 	struct uclamp_se uc_req = p->uclamp_req[clamp_id];
1530 #ifdef CONFIG_UCLAMP_TASK_GROUP
1531 	unsigned int tg_min, tg_max, value;
1532 
1533 	/*
1534 	 * Tasks in autogroups or root task group will be
1535 	 * restricted by system defaults.
1536 	 */
1537 	if (task_group_is_autogroup(task_group(p)))
1538 		return uc_req;
1539 	if (task_group(p) == &root_task_group)
1540 		return uc_req;
1541 
1542 	tg_min = task_group(p)->uclamp[UCLAMP_MIN].value;
1543 	tg_max = task_group(p)->uclamp[UCLAMP_MAX].value;
1544 	value = uc_req.value;
1545 	value = clamp(value, tg_min, tg_max);
1546 	uclamp_se_set(&uc_req, value, false);
1547 #endif
1548 
1549 	return uc_req;
1550 }
1551 
1552 /*
1553  * The effective clamp bucket index of a task depends on, by increasing
1554  * priority:
1555  * - the task specific clamp value, when explicitly requested from userspace
1556  * - the task group effective clamp value, for tasks not either in the root
1557  *   group or in an autogroup
1558  * - the system default clamp value, defined by the sysadmin
1559  */
1560 static inline struct uclamp_se
1561 uclamp_eff_get(struct task_struct *p, enum uclamp_id clamp_id)
1562 {
1563 	struct uclamp_se uc_req = uclamp_tg_restrict(p, clamp_id);
1564 	struct uclamp_se uc_max = uclamp_default[clamp_id];
1565 
1566 	/* System default restrictions always apply */
1567 	if (unlikely(uc_req.value > uc_max.value))
1568 		return uc_max;
1569 
1570 	return uc_req;
1571 }
1572 
1573 unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
1574 {
1575 	struct uclamp_se uc_eff;
1576 
1577 	/* Task currently refcounted: use back-annotated (effective) value */
1578 	if (p->uclamp[clamp_id].active)
1579 		return (unsigned long)p->uclamp[clamp_id].value;
1580 
1581 	uc_eff = uclamp_eff_get(p, clamp_id);
1582 
1583 	return (unsigned long)uc_eff.value;
1584 }
1585 
1586 /*
1587  * When a task is enqueued on a rq, the clamp bucket currently defined by the
1588  * task's uclamp::bucket_id is refcounted on that rq. This also immediately
1589  * updates the rq's clamp value if required.
1590  *
1591  * Tasks can have a task-specific value requested from user-space, track
1592  * within each bucket the maximum value for tasks refcounted in it.
1593  * This "local max aggregation" allows to track the exact "requested" value
1594  * for each bucket when all its RUNNABLE tasks require the same clamp.
1595  */
1596 static inline void uclamp_rq_inc_id(struct rq *rq, struct task_struct *p,
1597 				    enum uclamp_id clamp_id)
1598 {
1599 	struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
1600 	struct uclamp_se *uc_se = &p->uclamp[clamp_id];
1601 	struct uclamp_bucket *bucket;
1602 
1603 	lockdep_assert_rq_held(rq);
1604 
1605 	/* Update task effective clamp */
1606 	p->uclamp[clamp_id] = uclamp_eff_get(p, clamp_id);
1607 
1608 	bucket = &uc_rq->bucket[uc_se->bucket_id];
1609 	bucket->tasks++;
1610 	uc_se->active = true;
1611 
1612 	uclamp_idle_reset(rq, clamp_id, uc_se->value);
1613 
1614 	/*
1615 	 * Local max aggregation: rq buckets always track the max
1616 	 * "requested" clamp value of its RUNNABLE tasks.
1617 	 */
1618 	if (bucket->tasks == 1 || uc_se->value > bucket->value)
1619 		bucket->value = uc_se->value;
1620 
1621 	if (uc_se->value > uclamp_rq_get(rq, clamp_id))
1622 		uclamp_rq_set(rq, clamp_id, uc_se->value);
1623 }
1624 
1625 /*
1626  * When a task is dequeued from a rq, the clamp bucket refcounted by the task
1627  * is released. If this is the last task reference counting the rq's max
1628  * active clamp value, then the rq's clamp value is updated.
1629  *
1630  * Both refcounted tasks and rq's cached clamp values are expected to be
1631  * always valid. If it's detected they are not, as defensive programming,
1632  * enforce the expected state and warn.
1633  */
1634 static inline void uclamp_rq_dec_id(struct rq *rq, struct task_struct *p,
1635 				    enum uclamp_id clamp_id)
1636 {
1637 	struct uclamp_rq *uc_rq = &rq->uclamp[clamp_id];
1638 	struct uclamp_se *uc_se = &p->uclamp[clamp_id];
1639 	struct uclamp_bucket *bucket;
1640 	unsigned int bkt_clamp;
1641 	unsigned int rq_clamp;
1642 
1643 	lockdep_assert_rq_held(rq);
1644 
1645 	/*
1646 	 * If sched_uclamp_used was enabled after task @p was enqueued,
1647 	 * we could end up with unbalanced call to uclamp_rq_dec_id().
1648 	 *
1649 	 * In this case the uc_se->active flag should be false since no uclamp
1650 	 * accounting was performed at enqueue time and we can just return
1651 	 * here.
1652 	 *
1653 	 * Need to be careful of the following enqueue/dequeue ordering
1654 	 * problem too
1655 	 *
1656 	 *	enqueue(taskA)
1657 	 *	// sched_uclamp_used gets enabled
1658 	 *	enqueue(taskB)
1659 	 *	dequeue(taskA)
1660 	 *	// Must not decrement bucket->tasks here
1661 	 *	dequeue(taskB)
1662 	 *
1663 	 * where we could end up with stale data in uc_se and
1664 	 * bucket[uc_se->bucket_id].
1665 	 *
1666 	 * The following check here eliminates the possibility of such race.
1667 	 */
1668 	if (unlikely(!uc_se->active))
1669 		return;
1670 
1671 	bucket = &uc_rq->bucket[uc_se->bucket_id];
1672 
1673 	SCHED_WARN_ON(!bucket->tasks);
1674 	if (likely(bucket->tasks))
1675 		bucket->tasks--;
1676 
1677 	uc_se->active = false;
1678 
1679 	/*
1680 	 * Keep "local max aggregation" simple and accept to (possibly)
1681 	 * overboost some RUNNABLE tasks in the same bucket.
1682 	 * The rq clamp bucket value is reset to its base value whenever
1683 	 * there are no more RUNNABLE tasks refcounting it.
1684 	 */
1685 	if (likely(bucket->tasks))
1686 		return;
1687 
1688 	rq_clamp = uclamp_rq_get(rq, clamp_id);
1689 	/*
1690 	 * Defensive programming: this should never happen. If it happens,
1691 	 * e.g. due to future modification, warn and fix up the expected value.
1692 	 */
1693 	SCHED_WARN_ON(bucket->value > rq_clamp);
1694 	if (bucket->value >= rq_clamp) {
1695 		bkt_clamp = uclamp_rq_max_value(rq, clamp_id, uc_se->value);
1696 		uclamp_rq_set(rq, clamp_id, bkt_clamp);
1697 	}
1698 }
1699 
1700 static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p)
1701 {
1702 	enum uclamp_id clamp_id;
1703 
1704 	/*
1705 	 * Avoid any overhead until uclamp is actually used by the userspace.
1706 	 *
1707 	 * The condition is constructed such that a NOP is generated when
1708 	 * sched_uclamp_used is disabled.
1709 	 */
1710 	if (!static_branch_unlikely(&sched_uclamp_used))
1711 		return;
1712 
1713 	if (unlikely(!p->sched_class->uclamp_enabled))
1714 		return;
1715 
1716 	if (p->se.sched_delayed)
1717 		return;
1718 
1719 	for_each_clamp_id(clamp_id)
1720 		uclamp_rq_inc_id(rq, p, clamp_id);
1721 
1722 	/* Reset clamp idle holding when there is one RUNNABLE task */
1723 	if (rq->uclamp_flags & UCLAMP_FLAG_IDLE)
1724 		rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
1725 }
1726 
1727 static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p)
1728 {
1729 	enum uclamp_id clamp_id;
1730 
1731 	/*
1732 	 * Avoid any overhead until uclamp is actually used by the userspace.
1733 	 *
1734 	 * The condition is constructed such that a NOP is generated when
1735 	 * sched_uclamp_used is disabled.
1736 	 */
1737 	if (!static_branch_unlikely(&sched_uclamp_used))
1738 		return;
1739 
1740 	if (unlikely(!p->sched_class->uclamp_enabled))
1741 		return;
1742 
1743 	if (p->se.sched_delayed)
1744 		return;
1745 
1746 	for_each_clamp_id(clamp_id)
1747 		uclamp_rq_dec_id(rq, p, clamp_id);
1748 }
1749 
1750 static inline void uclamp_rq_reinc_id(struct rq *rq, struct task_struct *p,
1751 				      enum uclamp_id clamp_id)
1752 {
1753 	if (!p->uclamp[clamp_id].active)
1754 		return;
1755 
1756 	uclamp_rq_dec_id(rq, p, clamp_id);
1757 	uclamp_rq_inc_id(rq, p, clamp_id);
1758 
1759 	/*
1760 	 * Make sure to clear the idle flag if we've transiently reached 0
1761 	 * active tasks on rq.
1762 	 */
1763 	if (clamp_id == UCLAMP_MAX && (rq->uclamp_flags & UCLAMP_FLAG_IDLE))
1764 		rq->uclamp_flags &= ~UCLAMP_FLAG_IDLE;
1765 }
1766 
1767 static inline void
1768 uclamp_update_active(struct task_struct *p)
1769 {
1770 	enum uclamp_id clamp_id;
1771 	struct rq_flags rf;
1772 	struct rq *rq;
1773 
1774 	/*
1775 	 * Lock the task and the rq where the task is (or was) queued.
1776 	 *
1777 	 * We might lock the (previous) rq of a !RUNNABLE task, but that's the
1778 	 * price to pay to safely serialize util_{min,max} updates with
1779 	 * enqueues, dequeues and migration operations.
1780 	 * This is the same locking schema used by __set_cpus_allowed_ptr().
1781 	 */
1782 	rq = task_rq_lock(p, &rf);
1783 
1784 	/*
1785 	 * Setting the clamp bucket is serialized by task_rq_lock().
1786 	 * If the task is not yet RUNNABLE and its task_struct is not
1787 	 * affecting a valid clamp bucket, the next time it's enqueued,
1788 	 * it will already see the updated clamp bucket value.
1789 	 */
1790 	for_each_clamp_id(clamp_id)
1791 		uclamp_rq_reinc_id(rq, p, clamp_id);
1792 
1793 	task_rq_unlock(rq, p, &rf);
1794 }
1795 
1796 #ifdef CONFIG_UCLAMP_TASK_GROUP
1797 static inline void
1798 uclamp_update_active_tasks(struct cgroup_subsys_state *css)
1799 {
1800 	struct css_task_iter it;
1801 	struct task_struct *p;
1802 
1803 	css_task_iter_start(css, 0, &it);
1804 	while ((p = css_task_iter_next(&it)))
1805 		uclamp_update_active(p);
1806 	css_task_iter_end(&it);
1807 }
1808 
1809 static void cpu_util_update_eff(struct cgroup_subsys_state *css);
1810 #endif
1811 
1812 #ifdef CONFIG_SYSCTL
1813 #ifdef CONFIG_UCLAMP_TASK_GROUP
1814 static void uclamp_update_root_tg(void)
1815 {
1816 	struct task_group *tg = &root_task_group;
1817 
1818 	uclamp_se_set(&tg->uclamp_req[UCLAMP_MIN],
1819 		      sysctl_sched_uclamp_util_min, false);
1820 	uclamp_se_set(&tg->uclamp_req[UCLAMP_MAX],
1821 		      sysctl_sched_uclamp_util_max, false);
1822 
1823 	guard(rcu)();
1824 	cpu_util_update_eff(&root_task_group.css);
1825 }
1826 #else
1827 static void uclamp_update_root_tg(void) { }
1828 #endif
1829 
1830 static void uclamp_sync_util_min_rt_default(void)
1831 {
1832 	struct task_struct *g, *p;
1833 
1834 	/*
1835 	 * copy_process()			sysctl_uclamp
1836 	 *					  uclamp_min_rt = X;
1837 	 *   write_lock(&tasklist_lock)		  read_lock(&tasklist_lock)
1838 	 *   // link thread			  smp_mb__after_spinlock()
1839 	 *   write_unlock(&tasklist_lock)	  read_unlock(&tasklist_lock);
1840 	 *   sched_post_fork()			  for_each_process_thread()
1841 	 *     __uclamp_sync_rt()		    __uclamp_sync_rt()
1842 	 *
1843 	 * Ensures that either sched_post_fork() will observe the new
1844 	 * uclamp_min_rt or for_each_process_thread() will observe the new
1845 	 * task.
1846 	 */
1847 	read_lock(&tasklist_lock);
1848 	smp_mb__after_spinlock();
1849 	read_unlock(&tasklist_lock);
1850 
1851 	guard(rcu)();
1852 	for_each_process_thread(g, p)
1853 		uclamp_update_util_min_rt_default(p);
1854 }
1855 
1856 static int sysctl_sched_uclamp_handler(const struct ctl_table *table, int write,
1857 				void *buffer, size_t *lenp, loff_t *ppos)
1858 {
1859 	bool update_root_tg = false;
1860 	int old_min, old_max, old_min_rt;
1861 	int result;
1862 
1863 	guard(mutex)(&uclamp_mutex);
1864 
1865 	old_min = sysctl_sched_uclamp_util_min;
1866 	old_max = sysctl_sched_uclamp_util_max;
1867 	old_min_rt = sysctl_sched_uclamp_util_min_rt_default;
1868 
1869 	result = proc_dointvec(table, write, buffer, lenp, ppos);
1870 	if (result)
1871 		goto undo;
1872 	if (!write)
1873 		return 0;
1874 
1875 	if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max ||
1876 	    sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE	||
1877 	    sysctl_sched_uclamp_util_min_rt_default > SCHED_CAPACITY_SCALE) {
1878 
1879 		result = -EINVAL;
1880 		goto undo;
1881 	}
1882 
1883 	if (old_min != sysctl_sched_uclamp_util_min) {
1884 		uclamp_se_set(&uclamp_default[UCLAMP_MIN],
1885 			      sysctl_sched_uclamp_util_min, false);
1886 		update_root_tg = true;
1887 	}
1888 	if (old_max != sysctl_sched_uclamp_util_max) {
1889 		uclamp_se_set(&uclamp_default[UCLAMP_MAX],
1890 			      sysctl_sched_uclamp_util_max, false);
1891 		update_root_tg = true;
1892 	}
1893 
1894 	if (update_root_tg) {
1895 		static_branch_enable(&sched_uclamp_used);
1896 		uclamp_update_root_tg();
1897 	}
1898 
1899 	if (old_min_rt != sysctl_sched_uclamp_util_min_rt_default) {
1900 		static_branch_enable(&sched_uclamp_used);
1901 		uclamp_sync_util_min_rt_default();
1902 	}
1903 
1904 	/*
1905 	 * We update all RUNNABLE tasks only when task groups are in use.
1906 	 * Otherwise, keep it simple and do just a lazy update at each next
1907 	 * task enqueue time.
1908 	 */
1909 	return 0;
1910 
1911 undo:
1912 	sysctl_sched_uclamp_util_min = old_min;
1913 	sysctl_sched_uclamp_util_max = old_max;
1914 	sysctl_sched_uclamp_util_min_rt_default = old_min_rt;
1915 	return result;
1916 }
1917 #endif
1918 
1919 static void uclamp_fork(struct task_struct *p)
1920 {
1921 	enum uclamp_id clamp_id;
1922 
1923 	/*
1924 	 * We don't need to hold task_rq_lock() when updating p->uclamp_* here
1925 	 * as the task is still at its early fork stages.
1926 	 */
1927 	for_each_clamp_id(clamp_id)
1928 		p->uclamp[clamp_id].active = false;
1929 
1930 	if (likely(!p->sched_reset_on_fork))
1931 		return;
1932 
1933 	for_each_clamp_id(clamp_id) {
1934 		uclamp_se_set(&p->uclamp_req[clamp_id],
1935 			      uclamp_none(clamp_id), false);
1936 	}
1937 }
1938 
1939 static void uclamp_post_fork(struct task_struct *p)
1940 {
1941 	uclamp_update_util_min_rt_default(p);
1942 }
1943 
1944 static void __init init_uclamp_rq(struct rq *rq)
1945 {
1946 	enum uclamp_id clamp_id;
1947 	struct uclamp_rq *uc_rq = rq->uclamp;
1948 
1949 	for_each_clamp_id(clamp_id) {
1950 		uc_rq[clamp_id] = (struct uclamp_rq) {
1951 			.value = uclamp_none(clamp_id)
1952 		};
1953 	}
1954 
1955 	rq->uclamp_flags = UCLAMP_FLAG_IDLE;
1956 }
1957 
1958 static void __init init_uclamp(void)
1959 {
1960 	struct uclamp_se uc_max = {};
1961 	enum uclamp_id clamp_id;
1962 	int cpu;
1963 
1964 	for_each_possible_cpu(cpu)
1965 		init_uclamp_rq(cpu_rq(cpu));
1966 
1967 	for_each_clamp_id(clamp_id) {
1968 		uclamp_se_set(&init_task.uclamp_req[clamp_id],
1969 			      uclamp_none(clamp_id), false);
1970 	}
1971 
1972 	/* System defaults allow max clamp values for both indexes */
1973 	uclamp_se_set(&uc_max, uclamp_none(UCLAMP_MAX), false);
1974 	for_each_clamp_id(clamp_id) {
1975 		uclamp_default[clamp_id] = uc_max;
1976 #ifdef CONFIG_UCLAMP_TASK_GROUP
1977 		root_task_group.uclamp_req[clamp_id] = uc_max;
1978 		root_task_group.uclamp[clamp_id] = uc_max;
1979 #endif
1980 	}
1981 }
1982 
1983 #else /* !CONFIG_UCLAMP_TASK */
1984 static inline void uclamp_rq_inc(struct rq *rq, struct task_struct *p) { }
1985 static inline void uclamp_rq_dec(struct rq *rq, struct task_struct *p) { }
1986 static inline void uclamp_fork(struct task_struct *p) { }
1987 static inline void uclamp_post_fork(struct task_struct *p) { }
1988 static inline void init_uclamp(void) { }
1989 #endif /* CONFIG_UCLAMP_TASK */
1990 
1991 bool sched_task_on_rq(struct task_struct *p)
1992 {
1993 	return task_on_rq_queued(p);
1994 }
1995 
1996 unsigned long get_wchan(struct task_struct *p)
1997 {
1998 	unsigned long ip = 0;
1999 	unsigned int state;
2000 
2001 	if (!p || p == current)
2002 		return 0;
2003 
2004 	/* Only get wchan if task is blocked and we can keep it that way. */
2005 	raw_spin_lock_irq(&p->pi_lock);
2006 	state = READ_ONCE(p->__state);
2007 	smp_rmb(); /* see try_to_wake_up() */
2008 	if (state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq)
2009 		ip = __get_wchan(p);
2010 	raw_spin_unlock_irq(&p->pi_lock);
2011 
2012 	return ip;
2013 }
2014 
2015 void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
2016 {
2017 	if (!(flags & ENQUEUE_NOCLOCK))
2018 		update_rq_clock(rq);
2019 
2020 	p->sched_class->enqueue_task(rq, p, flags);
2021 	/*
2022 	 * Must be after ->enqueue_task() because ENQUEUE_DELAYED can clear
2023 	 * ->sched_delayed.
2024 	 */
2025 	uclamp_rq_inc(rq, p);
2026 
2027 	if (!(flags & ENQUEUE_RESTORE)) {
2028 		sched_info_enqueue(rq, p);
2029 		psi_enqueue(p, flags & ENQUEUE_MIGRATED);
2030 	}
2031 
2032 	if (sched_core_enabled(rq))
2033 		sched_core_enqueue(rq, p);
2034 }
2035 
2036 /*
2037  * Must only return false when DEQUEUE_SLEEP.
2038  */
2039 inline bool dequeue_task(struct rq *rq, struct task_struct *p, int flags)
2040 {
2041 	if (sched_core_enabled(rq))
2042 		sched_core_dequeue(rq, p, flags);
2043 
2044 	if (!(flags & DEQUEUE_NOCLOCK))
2045 		update_rq_clock(rq);
2046 
2047 	if (!(flags & DEQUEUE_SAVE)) {
2048 		sched_info_dequeue(rq, p);
2049 		psi_dequeue(p, !(flags & DEQUEUE_SLEEP));
2050 	}
2051 
2052 	/*
2053 	 * Must be before ->dequeue_task() because ->dequeue_task() can 'fail'
2054 	 * and mark the task ->sched_delayed.
2055 	 */
2056 	uclamp_rq_dec(rq, p);
2057 	return p->sched_class->dequeue_task(rq, p, flags);
2058 }
2059 
2060 void activate_task(struct rq *rq, struct task_struct *p, int flags)
2061 {
2062 	if (task_on_rq_migrating(p))
2063 		flags |= ENQUEUE_MIGRATED;
2064 	if (flags & ENQUEUE_MIGRATED)
2065 		sched_mm_cid_migrate_to(rq, p);
2066 
2067 	enqueue_task(rq, p, flags);
2068 
2069 	WRITE_ONCE(p->on_rq, TASK_ON_RQ_QUEUED);
2070 	ASSERT_EXCLUSIVE_WRITER(p->on_rq);
2071 }
2072 
2073 void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
2074 {
2075 	SCHED_WARN_ON(flags & DEQUEUE_SLEEP);
2076 
2077 	WRITE_ONCE(p->on_rq, TASK_ON_RQ_MIGRATING);
2078 	ASSERT_EXCLUSIVE_WRITER(p->on_rq);
2079 
2080 	/*
2081 	 * Code explicitly relies on TASK_ON_RQ_MIGRATING begin set *before*
2082 	 * dequeue_task() and cleared *after* enqueue_task().
2083 	 */
2084 
2085 	dequeue_task(rq, p, flags);
2086 }
2087 
2088 static void block_task(struct rq *rq, struct task_struct *p, int flags)
2089 {
2090 	if (dequeue_task(rq, p, DEQUEUE_SLEEP | flags))
2091 		__block_task(rq, p);
2092 }
2093 
2094 /**
2095  * task_curr - is this task currently executing on a CPU?
2096  * @p: the task in question.
2097  *
2098  * Return: 1 if the task is currently executing. 0 otherwise.
2099  */
2100 inline int task_curr(const struct task_struct *p)
2101 {
2102 	return cpu_curr(task_cpu(p)) == p;
2103 }
2104 
2105 /*
2106  * ->switching_to() is called with the pi_lock and rq_lock held and must not
2107  * mess with locking.
2108  */
2109 void check_class_changing(struct rq *rq, struct task_struct *p,
2110 			  const struct sched_class *prev_class)
2111 {
2112 	if (prev_class != p->sched_class && p->sched_class->switching_to)
2113 		p->sched_class->switching_to(rq, p);
2114 }
2115 
2116 /*
2117  * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
2118  * use the balance_callback list if you want balancing.
2119  *
2120  * this means any call to check_class_changed() must be followed by a call to
2121  * balance_callback().
2122  */
2123 void check_class_changed(struct rq *rq, struct task_struct *p,
2124 			 const struct sched_class *prev_class,
2125 			 int oldprio)
2126 {
2127 	if (prev_class != p->sched_class) {
2128 		if (prev_class->switched_from)
2129 			prev_class->switched_from(rq, p);
2130 
2131 		p->sched_class->switched_to(rq, p);
2132 	} else if (oldprio != p->prio || dl_task(p))
2133 		p->sched_class->prio_changed(rq, p, oldprio);
2134 }
2135 
2136 void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags)
2137 {
2138 	if (p->sched_class == rq->curr->sched_class)
2139 		rq->curr->sched_class->wakeup_preempt(rq, p, flags);
2140 	else if (sched_class_above(p->sched_class, rq->curr->sched_class))
2141 		resched_curr(rq);
2142 
2143 	/*
2144 	 * A queue event has occurred, and we're going to schedule.  In
2145 	 * this case, we can save a useless back to back clock update.
2146 	 */
2147 	if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
2148 		rq_clock_skip_update(rq);
2149 }
2150 
2151 static __always_inline
2152 int __task_state_match(struct task_struct *p, unsigned int state)
2153 {
2154 	if (READ_ONCE(p->__state) & state)
2155 		return 1;
2156 
2157 	if (READ_ONCE(p->saved_state) & state)
2158 		return -1;
2159 
2160 	return 0;
2161 }
2162 
2163 static __always_inline
2164 int task_state_match(struct task_struct *p, unsigned int state)
2165 {
2166 	/*
2167 	 * Serialize against current_save_and_set_rtlock_wait_state(),
2168 	 * current_restore_rtlock_saved_state(), and __refrigerator().
2169 	 */
2170 	guard(raw_spinlock_irq)(&p->pi_lock);
2171 	return __task_state_match(p, state);
2172 }
2173 
2174 /*
2175  * wait_task_inactive - wait for a thread to unschedule.
2176  *
2177  * Wait for the thread to block in any of the states set in @match_state.
2178  * If it changes, i.e. @p might have woken up, then return zero.  When we
2179  * succeed in waiting for @p to be off its CPU, we return a positive number
2180  * (its total switch count).  If a second call a short while later returns the
2181  * same number, the caller can be sure that @p has remained unscheduled the
2182  * whole time.
2183  *
2184  * The caller must ensure that the task *will* unschedule sometime soon,
2185  * else this function might spin for a *long* time. This function can't
2186  * be called with interrupts off, or it may introduce deadlock with
2187  * smp_call_function() if an IPI is sent by the same process we are
2188  * waiting to become inactive.
2189  */
2190 unsigned long wait_task_inactive(struct task_struct *p, unsigned int match_state)
2191 {
2192 	int running, queued, match;
2193 	struct rq_flags rf;
2194 	unsigned long ncsw;
2195 	struct rq *rq;
2196 
2197 	for (;;) {
2198 		/*
2199 		 * We do the initial early heuristics without holding
2200 		 * any task-queue locks at all. We'll only try to get
2201 		 * the runqueue lock when things look like they will
2202 		 * work out!
2203 		 */
2204 		rq = task_rq(p);
2205 
2206 		/*
2207 		 * If the task is actively running on another CPU
2208 		 * still, just relax and busy-wait without holding
2209 		 * any locks.
2210 		 *
2211 		 * NOTE! Since we don't hold any locks, it's not
2212 		 * even sure that "rq" stays as the right runqueue!
2213 		 * But we don't care, since "task_on_cpu()" will
2214 		 * return false if the runqueue has changed and p
2215 		 * is actually now running somewhere else!
2216 		 */
2217 		while (task_on_cpu(rq, p)) {
2218 			if (!task_state_match(p, match_state))
2219 				return 0;
2220 			cpu_relax();
2221 		}
2222 
2223 		/*
2224 		 * Ok, time to look more closely! We need the rq
2225 		 * lock now, to be *sure*. If we're wrong, we'll
2226 		 * just go back and repeat.
2227 		 */
2228 		rq = task_rq_lock(p, &rf);
2229 		trace_sched_wait_task(p);
2230 		running = task_on_cpu(rq, p);
2231 		queued = task_on_rq_queued(p);
2232 		ncsw = 0;
2233 		if ((match = __task_state_match(p, match_state))) {
2234 			/*
2235 			 * When matching on p->saved_state, consider this task
2236 			 * still queued so it will wait.
2237 			 */
2238 			if (match < 0)
2239 				queued = 1;
2240 			ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
2241 		}
2242 		task_rq_unlock(rq, p, &rf);
2243 
2244 		/*
2245 		 * If it changed from the expected state, bail out now.
2246 		 */
2247 		if (unlikely(!ncsw))
2248 			break;
2249 
2250 		/*
2251 		 * Was it really running after all now that we
2252 		 * checked with the proper locks actually held?
2253 		 *
2254 		 * Oops. Go back and try again..
2255 		 */
2256 		if (unlikely(running)) {
2257 			cpu_relax();
2258 			continue;
2259 		}
2260 
2261 		/*
2262 		 * It's not enough that it's not actively running,
2263 		 * it must be off the runqueue _entirely_, and not
2264 		 * preempted!
2265 		 *
2266 		 * So if it was still runnable (but just not actively
2267 		 * running right now), it's preempted, and we should
2268 		 * yield - it could be a while.
2269 		 */
2270 		if (unlikely(queued)) {
2271 			ktime_t to = NSEC_PER_SEC / HZ;
2272 
2273 			set_current_state(TASK_UNINTERRUPTIBLE);
2274 			schedule_hrtimeout(&to, HRTIMER_MODE_REL_HARD);
2275 			continue;
2276 		}
2277 
2278 		/*
2279 		 * Ahh, all good. It wasn't running, and it wasn't
2280 		 * runnable, which means that it will never become
2281 		 * running in the future either. We're all done!
2282 		 */
2283 		break;
2284 	}
2285 
2286 	return ncsw;
2287 }
2288 
2289 #ifdef CONFIG_SMP
2290 
2291 static void
2292 __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx);
2293 
2294 static void migrate_disable_switch(struct rq *rq, struct task_struct *p)
2295 {
2296 	struct affinity_context ac = {
2297 		.new_mask  = cpumask_of(rq->cpu),
2298 		.flags     = SCA_MIGRATE_DISABLE,
2299 	};
2300 
2301 	if (likely(!p->migration_disabled))
2302 		return;
2303 
2304 	if (p->cpus_ptr != &p->cpus_mask)
2305 		return;
2306 
2307 	/*
2308 	 * Violates locking rules! See comment in __do_set_cpus_allowed().
2309 	 */
2310 	__do_set_cpus_allowed(p, &ac);
2311 }
2312 
2313 void migrate_disable(void)
2314 {
2315 	struct task_struct *p = current;
2316 
2317 	if (p->migration_disabled) {
2318 #ifdef CONFIG_DEBUG_PREEMPT
2319 		/*
2320 		 *Warn about overflow half-way through the range.
2321 		 */
2322 		WARN_ON_ONCE((s16)p->migration_disabled < 0);
2323 #endif
2324 		p->migration_disabled++;
2325 		return;
2326 	}
2327 
2328 	guard(preempt)();
2329 	this_rq()->nr_pinned++;
2330 	p->migration_disabled = 1;
2331 }
2332 EXPORT_SYMBOL_GPL(migrate_disable);
2333 
2334 void migrate_enable(void)
2335 {
2336 	struct task_struct *p = current;
2337 	struct affinity_context ac = {
2338 		.new_mask  = &p->cpus_mask,
2339 		.flags     = SCA_MIGRATE_ENABLE,
2340 	};
2341 
2342 #ifdef CONFIG_DEBUG_PREEMPT
2343 	/*
2344 	 * Check both overflow from migrate_disable() and superfluous
2345 	 * migrate_enable().
2346 	 */
2347 	if (WARN_ON_ONCE((s16)p->migration_disabled <= 0))
2348 		return;
2349 #endif
2350 
2351 	if (p->migration_disabled > 1) {
2352 		p->migration_disabled--;
2353 		return;
2354 	}
2355 
2356 	/*
2357 	 * Ensure stop_task runs either before or after this, and that
2358 	 * __set_cpus_allowed_ptr(SCA_MIGRATE_ENABLE) doesn't schedule().
2359 	 */
2360 	guard(preempt)();
2361 	if (p->cpus_ptr != &p->cpus_mask)
2362 		__set_cpus_allowed_ptr(p, &ac);
2363 	/*
2364 	 * Mustn't clear migration_disabled() until cpus_ptr points back at the
2365 	 * regular cpus_mask, otherwise things that race (eg.
2366 	 * select_fallback_rq) get confused.
2367 	 */
2368 	barrier();
2369 	p->migration_disabled = 0;
2370 	this_rq()->nr_pinned--;
2371 }
2372 EXPORT_SYMBOL_GPL(migrate_enable);
2373 
2374 static inline bool rq_has_pinned_tasks(struct rq *rq)
2375 {
2376 	return rq->nr_pinned;
2377 }
2378 
2379 /*
2380  * Per-CPU kthreads are allowed to run on !active && online CPUs, see
2381  * __set_cpus_allowed_ptr() and select_fallback_rq().
2382  */
2383 static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
2384 {
2385 	/* When not in the task's cpumask, no point in looking further. */
2386 	if (!task_allowed_on_cpu(p, cpu))
2387 		return false;
2388 
2389 	/* migrate_disabled() must be allowed to finish. */
2390 	if (is_migration_disabled(p))
2391 		return cpu_online(cpu);
2392 
2393 	/* Non kernel threads are not allowed during either online or offline. */
2394 	if (!(p->flags & PF_KTHREAD))
2395 		return cpu_active(cpu);
2396 
2397 	/* KTHREAD_IS_PER_CPU is always allowed. */
2398 	if (kthread_is_per_cpu(p))
2399 		return cpu_online(cpu);
2400 
2401 	/* Regular kernel threads don't get to stay during offline. */
2402 	if (cpu_dying(cpu))
2403 		return false;
2404 
2405 	/* But are allowed during online. */
2406 	return cpu_online(cpu);
2407 }
2408 
2409 /*
2410  * This is how migration works:
2411  *
2412  * 1) we invoke migration_cpu_stop() on the target CPU using
2413  *    stop_one_cpu().
2414  * 2) stopper starts to run (implicitly forcing the migrated thread
2415  *    off the CPU)
2416  * 3) it checks whether the migrated task is still in the wrong runqueue.
2417  * 4) if it's in the wrong runqueue then the migration thread removes
2418  *    it and puts it into the right queue.
2419  * 5) stopper completes and stop_one_cpu() returns and the migration
2420  *    is done.
2421  */
2422 
2423 /*
2424  * move_queued_task - move a queued task to new rq.
2425  *
2426  * Returns (locked) new rq. Old rq's lock is released.
2427  */
2428 static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
2429 				   struct task_struct *p, int new_cpu)
2430 {
2431 	lockdep_assert_rq_held(rq);
2432 
2433 	deactivate_task(rq, p, DEQUEUE_NOCLOCK);
2434 	set_task_cpu(p, new_cpu);
2435 	rq_unlock(rq, rf);
2436 
2437 	rq = cpu_rq(new_cpu);
2438 
2439 	rq_lock(rq, rf);
2440 	WARN_ON_ONCE(task_cpu(p) != new_cpu);
2441 	activate_task(rq, p, 0);
2442 	wakeup_preempt(rq, p, 0);
2443 
2444 	return rq;
2445 }
2446 
2447 struct migration_arg {
2448 	struct task_struct		*task;
2449 	int				dest_cpu;
2450 	struct set_affinity_pending	*pending;
2451 };
2452 
2453 /*
2454  * @refs: number of wait_for_completion()
2455  * @stop_pending: is @stop_work in use
2456  */
2457 struct set_affinity_pending {
2458 	refcount_t		refs;
2459 	unsigned int		stop_pending;
2460 	struct completion	done;
2461 	struct cpu_stop_work	stop_work;
2462 	struct migration_arg	arg;
2463 };
2464 
2465 /*
2466  * Move (not current) task off this CPU, onto the destination CPU. We're doing
2467  * this because either it can't run here any more (set_cpus_allowed()
2468  * away from this CPU, or CPU going down), or because we're
2469  * attempting to rebalance this task on exec (sched_exec).
2470  *
2471  * So we race with normal scheduler movements, but that's OK, as long
2472  * as the task is no longer on this CPU.
2473  */
2474 static struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
2475 				 struct task_struct *p, int dest_cpu)
2476 {
2477 	/* Affinity changed (again). */
2478 	if (!is_cpu_allowed(p, dest_cpu))
2479 		return rq;
2480 
2481 	rq = move_queued_task(rq, rf, p, dest_cpu);
2482 
2483 	return rq;
2484 }
2485 
2486 /*
2487  * migration_cpu_stop - this will be executed by a high-prio stopper thread
2488  * and performs thread migration by bumping thread off CPU then
2489  * 'pushing' onto another runqueue.
2490  */
2491 static int migration_cpu_stop(void *data)
2492 {
2493 	struct migration_arg *arg = data;
2494 	struct set_affinity_pending *pending = arg->pending;
2495 	struct task_struct *p = arg->task;
2496 	struct rq *rq = this_rq();
2497 	bool complete = false;
2498 	struct rq_flags rf;
2499 
2500 	/*
2501 	 * The original target CPU might have gone down and we might
2502 	 * be on another CPU but it doesn't matter.
2503 	 */
2504 	local_irq_save(rf.flags);
2505 	/*
2506 	 * We need to explicitly wake pending tasks before running
2507 	 * __migrate_task() such that we will not miss enforcing cpus_ptr
2508 	 * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
2509 	 */
2510 	flush_smp_call_function_queue();
2511 
2512 	raw_spin_lock(&p->pi_lock);
2513 	rq_lock(rq, &rf);
2514 
2515 	/*
2516 	 * If we were passed a pending, then ->stop_pending was set, thus
2517 	 * p->migration_pending must have remained stable.
2518 	 */
2519 	WARN_ON_ONCE(pending && pending != p->migration_pending);
2520 
2521 	/*
2522 	 * If task_rq(p) != rq, it cannot be migrated here, because we're
2523 	 * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
2524 	 * we're holding p->pi_lock.
2525 	 */
2526 	if (task_rq(p) == rq) {
2527 		if (is_migration_disabled(p))
2528 			goto out;
2529 
2530 		if (pending) {
2531 			p->migration_pending = NULL;
2532 			complete = true;
2533 
2534 			if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask))
2535 				goto out;
2536 		}
2537 
2538 		if (task_on_rq_queued(p)) {
2539 			update_rq_clock(rq);
2540 			rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
2541 		} else {
2542 			p->wake_cpu = arg->dest_cpu;
2543 		}
2544 
2545 		/*
2546 		 * XXX __migrate_task() can fail, at which point we might end
2547 		 * up running on a dodgy CPU, AFAICT this can only happen
2548 		 * during CPU hotplug, at which point we'll get pushed out
2549 		 * anyway, so it's probably not a big deal.
2550 		 */
2551 
2552 	} else if (pending) {
2553 		/*
2554 		 * This happens when we get migrated between migrate_enable()'s
2555 		 * preempt_enable() and scheduling the stopper task. At that
2556 		 * point we're a regular task again and not current anymore.
2557 		 *
2558 		 * A !PREEMPT kernel has a giant hole here, which makes it far
2559 		 * more likely.
2560 		 */
2561 
2562 		/*
2563 		 * The task moved before the stopper got to run. We're holding
2564 		 * ->pi_lock, so the allowed mask is stable - if it got
2565 		 * somewhere allowed, we're done.
2566 		 */
2567 		if (cpumask_test_cpu(task_cpu(p), p->cpus_ptr)) {
2568 			p->migration_pending = NULL;
2569 			complete = true;
2570 			goto out;
2571 		}
2572 
2573 		/*
2574 		 * When migrate_enable() hits a rq mis-match we can't reliably
2575 		 * determine is_migration_disabled() and so have to chase after
2576 		 * it.
2577 		 */
2578 		WARN_ON_ONCE(!pending->stop_pending);
2579 		preempt_disable();
2580 		task_rq_unlock(rq, p, &rf);
2581 		stop_one_cpu_nowait(task_cpu(p), migration_cpu_stop,
2582 				    &pending->arg, &pending->stop_work);
2583 		preempt_enable();
2584 		return 0;
2585 	}
2586 out:
2587 	if (pending)
2588 		pending->stop_pending = false;
2589 	task_rq_unlock(rq, p, &rf);
2590 
2591 	if (complete)
2592 		complete_all(&pending->done);
2593 
2594 	return 0;
2595 }
2596 
2597 int push_cpu_stop(void *arg)
2598 {
2599 	struct rq *lowest_rq = NULL, *rq = this_rq();
2600 	struct task_struct *p = arg;
2601 
2602 	raw_spin_lock_irq(&p->pi_lock);
2603 	raw_spin_rq_lock(rq);
2604 
2605 	if (task_rq(p) != rq)
2606 		goto out_unlock;
2607 
2608 	if (is_migration_disabled(p)) {
2609 		p->migration_flags |= MDF_PUSH;
2610 		goto out_unlock;
2611 	}
2612 
2613 	p->migration_flags &= ~MDF_PUSH;
2614 
2615 	if (p->sched_class->find_lock_rq)
2616 		lowest_rq = p->sched_class->find_lock_rq(p, rq);
2617 
2618 	if (!lowest_rq)
2619 		goto out_unlock;
2620 
2621 	// XXX validate p is still the highest prio task
2622 	if (task_rq(p) == rq) {
2623 		deactivate_task(rq, p, 0);
2624 		set_task_cpu(p, lowest_rq->cpu);
2625 		activate_task(lowest_rq, p, 0);
2626 		resched_curr(lowest_rq);
2627 	}
2628 
2629 	double_unlock_balance(rq, lowest_rq);
2630 
2631 out_unlock:
2632 	rq->push_busy = false;
2633 	raw_spin_rq_unlock(rq);
2634 	raw_spin_unlock_irq(&p->pi_lock);
2635 
2636 	put_task_struct(p);
2637 	return 0;
2638 }
2639 
2640 /*
2641  * sched_class::set_cpus_allowed must do the below, but is not required to
2642  * actually call this function.
2643  */
2644 void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx)
2645 {
2646 	if (ctx->flags & (SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) {
2647 		p->cpus_ptr = ctx->new_mask;
2648 		return;
2649 	}
2650 
2651 	cpumask_copy(&p->cpus_mask, ctx->new_mask);
2652 	p->nr_cpus_allowed = cpumask_weight(ctx->new_mask);
2653 
2654 	/*
2655 	 * Swap in a new user_cpus_ptr if SCA_USER flag set
2656 	 */
2657 	if (ctx->flags & SCA_USER)
2658 		swap(p->user_cpus_ptr, ctx->user_mask);
2659 }
2660 
2661 static void
2662 __do_set_cpus_allowed(struct task_struct *p, struct affinity_context *ctx)
2663 {
2664 	struct rq *rq = task_rq(p);
2665 	bool queued, running;
2666 
2667 	/*
2668 	 * This here violates the locking rules for affinity, since we're only
2669 	 * supposed to change these variables while holding both rq->lock and
2670 	 * p->pi_lock.
2671 	 *
2672 	 * HOWEVER, it magically works, because ttwu() is the only code that
2673 	 * accesses these variables under p->pi_lock and only does so after
2674 	 * smp_cond_load_acquire(&p->on_cpu, !VAL), and we're in __schedule()
2675 	 * before finish_task().
2676 	 *
2677 	 * XXX do further audits, this smells like something putrid.
2678 	 */
2679 	if (ctx->flags & SCA_MIGRATE_DISABLE)
2680 		SCHED_WARN_ON(!p->on_cpu);
2681 	else
2682 		lockdep_assert_held(&p->pi_lock);
2683 
2684 	queued = task_on_rq_queued(p);
2685 	running = task_current(rq, p);
2686 
2687 	if (queued) {
2688 		/*
2689 		 * Because __kthread_bind() calls this on blocked tasks without
2690 		 * holding rq->lock.
2691 		 */
2692 		lockdep_assert_rq_held(rq);
2693 		dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
2694 	}
2695 	if (running)
2696 		put_prev_task(rq, p);
2697 
2698 	p->sched_class->set_cpus_allowed(p, ctx);
2699 
2700 	if (queued)
2701 		enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
2702 	if (running)
2703 		set_next_task(rq, p);
2704 }
2705 
2706 /*
2707  * Used for kthread_bind() and select_fallback_rq(), in both cases the user
2708  * affinity (if any) should be destroyed too.
2709  */
2710 void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
2711 {
2712 	struct affinity_context ac = {
2713 		.new_mask  = new_mask,
2714 		.user_mask = NULL,
2715 		.flags     = SCA_USER,	/* clear the user requested mask */
2716 	};
2717 	union cpumask_rcuhead {
2718 		cpumask_t cpumask;
2719 		struct rcu_head rcu;
2720 	};
2721 
2722 	__do_set_cpus_allowed(p, &ac);
2723 
2724 	/*
2725 	 * Because this is called with p->pi_lock held, it is not possible
2726 	 * to use kfree() here (when PREEMPT_RT=y), therefore punt to using
2727 	 * kfree_rcu().
2728 	 */
2729 	kfree_rcu((union cpumask_rcuhead *)ac.user_mask, rcu);
2730 }
2731 
2732 int dup_user_cpus_ptr(struct task_struct *dst, struct task_struct *src,
2733 		      int node)
2734 {
2735 	cpumask_t *user_mask;
2736 	unsigned long flags;
2737 
2738 	/*
2739 	 * Always clear dst->user_cpus_ptr first as their user_cpus_ptr's
2740 	 * may differ by now due to racing.
2741 	 */
2742 	dst->user_cpus_ptr = NULL;
2743 
2744 	/*
2745 	 * This check is racy and losing the race is a valid situation.
2746 	 * It is not worth the extra overhead of taking the pi_lock on
2747 	 * every fork/clone.
2748 	 */
2749 	if (data_race(!src->user_cpus_ptr))
2750 		return 0;
2751 
2752 	user_mask = alloc_user_cpus_ptr(node);
2753 	if (!user_mask)
2754 		return -ENOMEM;
2755 
2756 	/*
2757 	 * Use pi_lock to protect content of user_cpus_ptr
2758 	 *
2759 	 * Though unlikely, user_cpus_ptr can be reset to NULL by a concurrent
2760 	 * do_set_cpus_allowed().
2761 	 */
2762 	raw_spin_lock_irqsave(&src->pi_lock, flags);
2763 	if (src->user_cpus_ptr) {
2764 		swap(dst->user_cpus_ptr, user_mask);
2765 		cpumask_copy(dst->user_cpus_ptr, src->user_cpus_ptr);
2766 	}
2767 	raw_spin_unlock_irqrestore(&src->pi_lock, flags);
2768 
2769 	if (unlikely(user_mask))
2770 		kfree(user_mask);
2771 
2772 	return 0;
2773 }
2774 
2775 static inline struct cpumask *clear_user_cpus_ptr(struct task_struct *p)
2776 {
2777 	struct cpumask *user_mask = NULL;
2778 
2779 	swap(p->user_cpus_ptr, user_mask);
2780 
2781 	return user_mask;
2782 }
2783 
2784 void release_user_cpus_ptr(struct task_struct *p)
2785 {
2786 	kfree(clear_user_cpus_ptr(p));
2787 }
2788 
2789 /*
2790  * This function is wildly self concurrent; here be dragons.
2791  *
2792  *
2793  * When given a valid mask, __set_cpus_allowed_ptr() must block until the
2794  * designated task is enqueued on an allowed CPU. If that task is currently
2795  * running, we have to kick it out using the CPU stopper.
2796  *
2797  * Migrate-Disable comes along and tramples all over our nice sandcastle.
2798  * Consider:
2799  *
2800  *     Initial conditions: P0->cpus_mask = [0, 1]
2801  *
2802  *     P0@CPU0                  P1
2803  *
2804  *     migrate_disable();
2805  *     <preempted>
2806  *                              set_cpus_allowed_ptr(P0, [1]);
2807  *
2808  * P1 *cannot* return from this set_cpus_allowed_ptr() call until P0 executes
2809  * its outermost migrate_enable() (i.e. it exits its Migrate-Disable region).
2810  * This means we need the following scheme:
2811  *
2812  *     P0@CPU0                  P1
2813  *
2814  *     migrate_disable();
2815  *     <preempted>
2816  *                              set_cpus_allowed_ptr(P0, [1]);
2817  *                                <blocks>
2818  *     <resumes>
2819  *     migrate_enable();
2820  *       __set_cpus_allowed_ptr();
2821  *       <wakes local stopper>
2822  *                         `--> <woken on migration completion>
2823  *
2824  * Now the fun stuff: there may be several P1-like tasks, i.e. multiple
2825  * concurrent set_cpus_allowed_ptr(P0, [*]) calls. CPU affinity changes of any
2826  * task p are serialized by p->pi_lock, which we can leverage: the one that
2827  * should come into effect at the end of the Migrate-Disable region is the last
2828  * one. This means we only need to track a single cpumask (i.e. p->cpus_mask),
2829  * but we still need to properly signal those waiting tasks at the appropriate
2830  * moment.
2831  *
2832  * This is implemented using struct set_affinity_pending. The first
2833  * __set_cpus_allowed_ptr() caller within a given Migrate-Disable region will
2834  * setup an instance of that struct and install it on the targeted task_struct.
2835  * Any and all further callers will reuse that instance. Those then wait for
2836  * a completion signaled at the tail of the CPU stopper callback (1), triggered
2837  * on the end of the Migrate-Disable region (i.e. outermost migrate_enable()).
2838  *
2839  *
2840  * (1) In the cases covered above. There is one more where the completion is
2841  * signaled within affine_move_task() itself: when a subsequent affinity request
2842  * occurs after the stopper bailed out due to the targeted task still being
2843  * Migrate-Disable. Consider:
2844  *
2845  *     Initial conditions: P0->cpus_mask = [0, 1]
2846  *
2847  *     CPU0		  P1				P2
2848  *     <P0>
2849  *       migrate_disable();
2850  *       <preempted>
2851  *                        set_cpus_allowed_ptr(P0, [1]);
2852  *                          <blocks>
2853  *     <migration/0>
2854  *       migration_cpu_stop()
2855  *         is_migration_disabled()
2856  *           <bails>
2857  *                                                       set_cpus_allowed_ptr(P0, [0, 1]);
2858  *                                                         <signal completion>
2859  *                          <awakes>
2860  *
2861  * Note that the above is safe vs a concurrent migrate_enable(), as any
2862  * pending affinity completion is preceded by an uninstallation of
2863  * p->migration_pending done with p->pi_lock held.
2864  */
2865 static int affine_move_task(struct rq *rq, struct task_struct *p, struct rq_flags *rf,
2866 			    int dest_cpu, unsigned int flags)
2867 	__releases(rq->lock)
2868 	__releases(p->pi_lock)
2869 {
2870 	struct set_affinity_pending my_pending = { }, *pending = NULL;
2871 	bool stop_pending, complete = false;
2872 
2873 	/* Can the task run on the task's current CPU? If so, we're done */
2874 	if (cpumask_test_cpu(task_cpu(p), &p->cpus_mask)) {
2875 		struct task_struct *push_task = NULL;
2876 
2877 		if ((flags & SCA_MIGRATE_ENABLE) &&
2878 		    (p->migration_flags & MDF_PUSH) && !rq->push_busy) {
2879 			rq->push_busy = true;
2880 			push_task = get_task_struct(p);
2881 		}
2882 
2883 		/*
2884 		 * If there are pending waiters, but no pending stop_work,
2885 		 * then complete now.
2886 		 */
2887 		pending = p->migration_pending;
2888 		if (pending && !pending->stop_pending) {
2889 			p->migration_pending = NULL;
2890 			complete = true;
2891 		}
2892 
2893 		preempt_disable();
2894 		task_rq_unlock(rq, p, rf);
2895 		if (push_task) {
2896 			stop_one_cpu_nowait(rq->cpu, push_cpu_stop,
2897 					    p, &rq->push_work);
2898 		}
2899 		preempt_enable();
2900 
2901 		if (complete)
2902 			complete_all(&pending->done);
2903 
2904 		return 0;
2905 	}
2906 
2907 	if (!(flags & SCA_MIGRATE_ENABLE)) {
2908 		/* serialized by p->pi_lock */
2909 		if (!p->migration_pending) {
2910 			/* Install the request */
2911 			refcount_set(&my_pending.refs, 1);
2912 			init_completion(&my_pending.done);
2913 			my_pending.arg = (struct migration_arg) {
2914 				.task = p,
2915 				.dest_cpu = dest_cpu,
2916 				.pending = &my_pending,
2917 			};
2918 
2919 			p->migration_pending = &my_pending;
2920 		} else {
2921 			pending = p->migration_pending;
2922 			refcount_inc(&pending->refs);
2923 			/*
2924 			 * Affinity has changed, but we've already installed a
2925 			 * pending. migration_cpu_stop() *must* see this, else
2926 			 * we risk a completion of the pending despite having a
2927 			 * task on a disallowed CPU.
2928 			 *
2929 			 * Serialized by p->pi_lock, so this is safe.
2930 			 */
2931 			pending->arg.dest_cpu = dest_cpu;
2932 		}
2933 	}
2934 	pending = p->migration_pending;
2935 	/*
2936 	 * - !MIGRATE_ENABLE:
2937 	 *   we'll have installed a pending if there wasn't one already.
2938 	 *
2939 	 * - MIGRATE_ENABLE:
2940 	 *   we're here because the current CPU isn't matching anymore,
2941 	 *   the only way that can happen is because of a concurrent
2942 	 *   set_cpus_allowed_ptr() call, which should then still be
2943 	 *   pending completion.
2944 	 *
2945 	 * Either way, we really should have a @pending here.
2946 	 */
2947 	if (WARN_ON_ONCE(!pending)) {
2948 		task_rq_unlock(rq, p, rf);
2949 		return -EINVAL;
2950 	}
2951 
2952 	if (task_on_cpu(rq, p) || READ_ONCE(p->__state) == TASK_WAKING) {
2953 		/*
2954 		 * MIGRATE_ENABLE gets here because 'p == current', but for
2955 		 * anything else we cannot do is_migration_disabled(), punt
2956 		 * and have the stopper function handle it all race-free.
2957 		 */
2958 		stop_pending = pending->stop_pending;
2959 		if (!stop_pending)
2960 			pending->stop_pending = true;
2961 
2962 		if (flags & SCA_MIGRATE_ENABLE)
2963 			p->migration_flags &= ~MDF_PUSH;
2964 
2965 		preempt_disable();
2966 		task_rq_unlock(rq, p, rf);
2967 		if (!stop_pending) {
2968 			stop_one_cpu_nowait(cpu_of(rq), migration_cpu_stop,
2969 					    &pending->arg, &pending->stop_work);
2970 		}
2971 		preempt_enable();
2972 
2973 		if (flags & SCA_MIGRATE_ENABLE)
2974 			return 0;
2975 	} else {
2976 
2977 		if (!is_migration_disabled(p)) {
2978 			if (task_on_rq_queued(p))
2979 				rq = move_queued_task(rq, rf, p, dest_cpu);
2980 
2981 			if (!pending->stop_pending) {
2982 				p->migration_pending = NULL;
2983 				complete = true;
2984 			}
2985 		}
2986 		task_rq_unlock(rq, p, rf);
2987 
2988 		if (complete)
2989 			complete_all(&pending->done);
2990 	}
2991 
2992 	wait_for_completion(&pending->done);
2993 
2994 	if (refcount_dec_and_test(&pending->refs))
2995 		wake_up_var(&pending->refs); /* No UaF, just an address */
2996 
2997 	/*
2998 	 * Block the original owner of &pending until all subsequent callers
2999 	 * have seen the completion and decremented the refcount
3000 	 */
3001 	wait_var_event(&my_pending.refs, !refcount_read(&my_pending.refs));
3002 
3003 	/* ARGH */
3004 	WARN_ON_ONCE(my_pending.stop_pending);
3005 
3006 	return 0;
3007 }
3008 
3009 /*
3010  * Called with both p->pi_lock and rq->lock held; drops both before returning.
3011  */
3012 static int __set_cpus_allowed_ptr_locked(struct task_struct *p,
3013 					 struct affinity_context *ctx,
3014 					 struct rq *rq,
3015 					 struct rq_flags *rf)
3016 	__releases(rq->lock)
3017 	__releases(p->pi_lock)
3018 {
3019 	const struct cpumask *cpu_allowed_mask = task_cpu_possible_mask(p);
3020 	const struct cpumask *cpu_valid_mask = cpu_active_mask;
3021 	bool kthread = p->flags & PF_KTHREAD;
3022 	unsigned int dest_cpu;
3023 	int ret = 0;
3024 
3025 	update_rq_clock(rq);
3026 
3027 	if (kthread || is_migration_disabled(p)) {
3028 		/*
3029 		 * Kernel threads are allowed on online && !active CPUs,
3030 		 * however, during cpu-hot-unplug, even these might get pushed
3031 		 * away if not KTHREAD_IS_PER_CPU.
3032 		 *
3033 		 * Specifically, migration_disabled() tasks must not fail the
3034 		 * cpumask_any_and_distribute() pick below, esp. so on
3035 		 * SCA_MIGRATE_ENABLE, otherwise we'll not call
3036 		 * set_cpus_allowed_common() and actually reset p->cpus_ptr.
3037 		 */
3038 		cpu_valid_mask = cpu_online_mask;
3039 	}
3040 
3041 	if (!kthread && !cpumask_subset(ctx->new_mask, cpu_allowed_mask)) {
3042 		ret = -EINVAL;
3043 		goto out;
3044 	}
3045 
3046 	/*
3047 	 * Must re-check here, to close a race against __kthread_bind(),
3048 	 * sched_setaffinity() is not guaranteed to observe the flag.
3049 	 */
3050 	if ((ctx->flags & SCA_CHECK) && (p->flags & PF_NO_SETAFFINITY)) {
3051 		ret = -EINVAL;
3052 		goto out;
3053 	}
3054 
3055 	if (!(ctx->flags & SCA_MIGRATE_ENABLE)) {
3056 		if (cpumask_equal(&p->cpus_mask, ctx->new_mask)) {
3057 			if (ctx->flags & SCA_USER)
3058 				swap(p->user_cpus_ptr, ctx->user_mask);
3059 			goto out;
3060 		}
3061 
3062 		if (WARN_ON_ONCE(p == current &&
3063 				 is_migration_disabled(p) &&
3064 				 !cpumask_test_cpu(task_cpu(p), ctx->new_mask))) {
3065 			ret = -EBUSY;
3066 			goto out;
3067 		}
3068 	}
3069 
3070 	/*
3071 	 * Picking a ~random cpu helps in cases where we are changing affinity
3072 	 * for groups of tasks (ie. cpuset), so that load balancing is not
3073 	 * immediately required to distribute the tasks within their new mask.
3074 	 */
3075 	dest_cpu = cpumask_any_and_distribute(cpu_valid_mask, ctx->new_mask);
3076 	if (dest_cpu >= nr_cpu_ids) {
3077 		ret = -EINVAL;
3078 		goto out;
3079 	}
3080 
3081 	__do_set_cpus_allowed(p, ctx);
3082 
3083 	return affine_move_task(rq, p, rf, dest_cpu, ctx->flags);
3084 
3085 out:
3086 	task_rq_unlock(rq, p, rf);
3087 
3088 	return ret;
3089 }
3090 
3091 /*
3092  * Change a given task's CPU affinity. Migrate the thread to a
3093  * proper CPU and schedule it away if the CPU it's executing on
3094  * is removed from the allowed bitmask.
3095  *
3096  * NOTE: the caller must have a valid reference to the task, the
3097  * task must not exit() & deallocate itself prematurely. The
3098  * call is not atomic; no spinlocks may be held.
3099  */
3100 int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx)
3101 {
3102 	struct rq_flags rf;
3103 	struct rq *rq;
3104 
3105 	rq = task_rq_lock(p, &rf);
3106 	/*
3107 	 * Masking should be skipped if SCA_USER or any of the SCA_MIGRATE_*
3108 	 * flags are set.
3109 	 */
3110 	if (p->user_cpus_ptr &&
3111 	    !(ctx->flags & (SCA_USER | SCA_MIGRATE_ENABLE | SCA_MIGRATE_DISABLE)) &&
3112 	    cpumask_and(rq->scratch_mask, ctx->new_mask, p->user_cpus_ptr))
3113 		ctx->new_mask = rq->scratch_mask;
3114 
3115 	return __set_cpus_allowed_ptr_locked(p, ctx, rq, &rf);
3116 }
3117 
3118 int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
3119 {
3120 	struct affinity_context ac = {
3121 		.new_mask  = new_mask,
3122 		.flags     = 0,
3123 	};
3124 
3125 	return __set_cpus_allowed_ptr(p, &ac);
3126 }
3127 EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
3128 
3129 /*
3130  * Change a given task's CPU affinity to the intersection of its current
3131  * affinity mask and @subset_mask, writing the resulting mask to @new_mask.
3132  * If user_cpus_ptr is defined, use it as the basis for restricting CPU
3133  * affinity or use cpu_online_mask instead.
3134  *
3135  * If the resulting mask is empty, leave the affinity unchanged and return
3136  * -EINVAL.
3137  */
3138 static int restrict_cpus_allowed_ptr(struct task_struct *p,
3139 				     struct cpumask *new_mask,
3140 				     const struct cpumask *subset_mask)
3141 {
3142 	struct affinity_context ac = {
3143 		.new_mask  = new_mask,
3144 		.flags     = 0,
3145 	};
3146 	struct rq_flags rf;
3147 	struct rq *rq;
3148 	int err;
3149 
3150 	rq = task_rq_lock(p, &rf);
3151 
3152 	/*
3153 	 * Forcefully restricting the affinity of a deadline task is
3154 	 * likely to cause problems, so fail and noisily override the
3155 	 * mask entirely.
3156 	 */
3157 	if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
3158 		err = -EPERM;
3159 		goto err_unlock;
3160 	}
3161 
3162 	if (!cpumask_and(new_mask, task_user_cpus(p), subset_mask)) {
3163 		err = -EINVAL;
3164 		goto err_unlock;
3165 	}
3166 
3167 	return __set_cpus_allowed_ptr_locked(p, &ac, rq, &rf);
3168 
3169 err_unlock:
3170 	task_rq_unlock(rq, p, &rf);
3171 	return err;
3172 }
3173 
3174 /*
3175  * Restrict the CPU affinity of task @p so that it is a subset of
3176  * task_cpu_possible_mask() and point @p->user_cpus_ptr to a copy of the
3177  * old affinity mask. If the resulting mask is empty, we warn and walk
3178  * up the cpuset hierarchy until we find a suitable mask.
3179  */
3180 void force_compatible_cpus_allowed_ptr(struct task_struct *p)
3181 {
3182 	cpumask_var_t new_mask;
3183 	const struct cpumask *override_mask = task_cpu_possible_mask(p);
3184 
3185 	alloc_cpumask_var(&new_mask, GFP_KERNEL);
3186 
3187 	/*
3188 	 * __migrate_task() can fail silently in the face of concurrent
3189 	 * offlining of the chosen destination CPU, so take the hotplug
3190 	 * lock to ensure that the migration succeeds.
3191 	 */
3192 	cpus_read_lock();
3193 	if (!cpumask_available(new_mask))
3194 		goto out_set_mask;
3195 
3196 	if (!restrict_cpus_allowed_ptr(p, new_mask, override_mask))
3197 		goto out_free_mask;
3198 
3199 	/*
3200 	 * We failed to find a valid subset of the affinity mask for the
3201 	 * task, so override it based on its cpuset hierarchy.
3202 	 */
3203 	cpuset_cpus_allowed(p, new_mask);
3204 	override_mask = new_mask;
3205 
3206 out_set_mask:
3207 	if (printk_ratelimit()) {
3208 		printk_deferred("Overriding affinity for process %d (%s) to CPUs %*pbl\n",
3209 				task_pid_nr(p), p->comm,
3210 				cpumask_pr_args(override_mask));
3211 	}
3212 
3213 	WARN_ON(set_cpus_allowed_ptr(p, override_mask));
3214 out_free_mask:
3215 	cpus_read_unlock();
3216 	free_cpumask_var(new_mask);
3217 }
3218 
3219 /*
3220  * Restore the affinity of a task @p which was previously restricted by a
3221  * call to force_compatible_cpus_allowed_ptr().
3222  *
3223  * It is the caller's responsibility to serialise this with any calls to
3224  * force_compatible_cpus_allowed_ptr(@p).
3225  */
3226 void relax_compatible_cpus_allowed_ptr(struct task_struct *p)
3227 {
3228 	struct affinity_context ac = {
3229 		.new_mask  = task_user_cpus(p),
3230 		.flags     = 0,
3231 	};
3232 	int ret;
3233 
3234 	/*
3235 	 * Try to restore the old affinity mask with __sched_setaffinity().
3236 	 * Cpuset masking will be done there too.
3237 	 */
3238 	ret = __sched_setaffinity(p, &ac);
3239 	WARN_ON_ONCE(ret);
3240 }
3241 
3242 void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
3243 {
3244 #ifdef CONFIG_SCHED_DEBUG
3245 	unsigned int state = READ_ONCE(p->__state);
3246 
3247 	/*
3248 	 * We should never call set_task_cpu() on a blocked task,
3249 	 * ttwu() will sort out the placement.
3250 	 */
3251 	WARN_ON_ONCE(state != TASK_RUNNING && state != TASK_WAKING && !p->on_rq);
3252 
3253 	/*
3254 	 * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
3255 	 * because schedstat_wait_{start,end} rebase migrating task's wait_start
3256 	 * time relying on p->on_rq.
3257 	 */
3258 	WARN_ON_ONCE(state == TASK_RUNNING &&
3259 		     p->sched_class == &fair_sched_class &&
3260 		     (p->on_rq && !task_on_rq_migrating(p)));
3261 
3262 #ifdef CONFIG_LOCKDEP
3263 	/*
3264 	 * The caller should hold either p->pi_lock or rq->lock, when changing
3265 	 * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
3266 	 *
3267 	 * sched_move_task() holds both and thus holding either pins the cgroup,
3268 	 * see task_group().
3269 	 *
3270 	 * Furthermore, all task_rq users should acquire both locks, see
3271 	 * task_rq_lock().
3272 	 */
3273 	WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
3274 				      lockdep_is_held(__rq_lockp(task_rq(p)))));
3275 #endif
3276 	/*
3277 	 * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
3278 	 */
3279 	WARN_ON_ONCE(!cpu_online(new_cpu));
3280 
3281 	WARN_ON_ONCE(is_migration_disabled(p));
3282 #endif
3283 
3284 	trace_sched_migrate_task(p, new_cpu);
3285 
3286 	if (task_cpu(p) != new_cpu) {
3287 		if (p->sched_class->migrate_task_rq)
3288 			p->sched_class->migrate_task_rq(p, new_cpu);
3289 		p->se.nr_migrations++;
3290 		rseq_migrate(p);
3291 		sched_mm_cid_migrate_from(p);
3292 		perf_event_task_migrate(p);
3293 	}
3294 
3295 	__set_task_cpu(p, new_cpu);
3296 }
3297 
3298 #ifdef CONFIG_NUMA_BALANCING
3299 static void __migrate_swap_task(struct task_struct *p, int cpu)
3300 {
3301 	if (task_on_rq_queued(p)) {
3302 		struct rq *src_rq, *dst_rq;
3303 		struct rq_flags srf, drf;
3304 
3305 		src_rq = task_rq(p);
3306 		dst_rq = cpu_rq(cpu);
3307 
3308 		rq_pin_lock(src_rq, &srf);
3309 		rq_pin_lock(dst_rq, &drf);
3310 
3311 		deactivate_task(src_rq, p, 0);
3312 		set_task_cpu(p, cpu);
3313 		activate_task(dst_rq, p, 0);
3314 		wakeup_preempt(dst_rq, p, 0);
3315 
3316 		rq_unpin_lock(dst_rq, &drf);
3317 		rq_unpin_lock(src_rq, &srf);
3318 
3319 	} else {
3320 		/*
3321 		 * Task isn't running anymore; make it appear like we migrated
3322 		 * it before it went to sleep. This means on wakeup we make the
3323 		 * previous CPU our target instead of where it really is.
3324 		 */
3325 		p->wake_cpu = cpu;
3326 	}
3327 }
3328 
3329 struct migration_swap_arg {
3330 	struct task_struct *src_task, *dst_task;
3331 	int src_cpu, dst_cpu;
3332 };
3333 
3334 static int migrate_swap_stop(void *data)
3335 {
3336 	struct migration_swap_arg *arg = data;
3337 	struct rq *src_rq, *dst_rq;
3338 
3339 	if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
3340 		return -EAGAIN;
3341 
3342 	src_rq = cpu_rq(arg->src_cpu);
3343 	dst_rq = cpu_rq(arg->dst_cpu);
3344 
3345 	guard(double_raw_spinlock)(&arg->src_task->pi_lock, &arg->dst_task->pi_lock);
3346 	guard(double_rq_lock)(src_rq, dst_rq);
3347 
3348 	if (task_cpu(arg->dst_task) != arg->dst_cpu)
3349 		return -EAGAIN;
3350 
3351 	if (task_cpu(arg->src_task) != arg->src_cpu)
3352 		return -EAGAIN;
3353 
3354 	if (!cpumask_test_cpu(arg->dst_cpu, arg->src_task->cpus_ptr))
3355 		return -EAGAIN;
3356 
3357 	if (!cpumask_test_cpu(arg->src_cpu, arg->dst_task->cpus_ptr))
3358 		return -EAGAIN;
3359 
3360 	__migrate_swap_task(arg->src_task, arg->dst_cpu);
3361 	__migrate_swap_task(arg->dst_task, arg->src_cpu);
3362 
3363 	return 0;
3364 }
3365 
3366 /*
3367  * Cross migrate two tasks
3368  */
3369 int migrate_swap(struct task_struct *cur, struct task_struct *p,
3370 		int target_cpu, int curr_cpu)
3371 {
3372 	struct migration_swap_arg arg;
3373 	int ret = -EINVAL;
3374 
3375 	arg = (struct migration_swap_arg){
3376 		.src_task = cur,
3377 		.src_cpu = curr_cpu,
3378 		.dst_task = p,
3379 		.dst_cpu = target_cpu,
3380 	};
3381 
3382 	if (arg.src_cpu == arg.dst_cpu)
3383 		goto out;
3384 
3385 	/*
3386 	 * These three tests are all lockless; this is OK since all of them
3387 	 * will be re-checked with proper locks held further down the line.
3388 	 */
3389 	if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
3390 		goto out;
3391 
3392 	if (!cpumask_test_cpu(arg.dst_cpu, arg.src_task->cpus_ptr))
3393 		goto out;
3394 
3395 	if (!cpumask_test_cpu(arg.src_cpu, arg.dst_task->cpus_ptr))
3396 		goto out;
3397 
3398 	trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
3399 	ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
3400 
3401 out:
3402 	return ret;
3403 }
3404 #endif /* CONFIG_NUMA_BALANCING */
3405 
3406 /***
3407  * kick_process - kick a running thread to enter/exit the kernel
3408  * @p: the to-be-kicked thread
3409  *
3410  * Cause a process which is running on another CPU to enter
3411  * kernel-mode, without any delay. (to get signals handled.)
3412  *
3413  * NOTE: this function doesn't have to take the runqueue lock,
3414  * because all it wants to ensure is that the remote task enters
3415  * the kernel. If the IPI races and the task has been migrated
3416  * to another CPU then no harm is done and the purpose has been
3417  * achieved as well.
3418  */
3419 void kick_process(struct task_struct *p)
3420 {
3421 	guard(preempt)();
3422 	int cpu = task_cpu(p);
3423 
3424 	if ((cpu != smp_processor_id()) && task_curr(p))
3425 		smp_send_reschedule(cpu);
3426 }
3427 EXPORT_SYMBOL_GPL(kick_process);
3428 
3429 /*
3430  * ->cpus_ptr is protected by both rq->lock and p->pi_lock
3431  *
3432  * A few notes on cpu_active vs cpu_online:
3433  *
3434  *  - cpu_active must be a subset of cpu_online
3435  *
3436  *  - on CPU-up we allow per-CPU kthreads on the online && !active CPU,
3437  *    see __set_cpus_allowed_ptr(). At this point the newly online
3438  *    CPU isn't yet part of the sched domains, and balancing will not
3439  *    see it.
3440  *
3441  *  - on CPU-down we clear cpu_active() to mask the sched domains and
3442  *    avoid the load balancer to place new tasks on the to be removed
3443  *    CPU. Existing tasks will remain running there and will be taken
3444  *    off.
3445  *
3446  * This means that fallback selection must not select !active CPUs.
3447  * And can assume that any active CPU must be online. Conversely
3448  * select_task_rq() below may allow selection of !active CPUs in order
3449  * to satisfy the above rules.
3450  */
3451 static int select_fallback_rq(int cpu, struct task_struct *p)
3452 {
3453 	int nid = cpu_to_node(cpu);
3454 	const struct cpumask *nodemask = NULL;
3455 	enum { cpuset, possible, fail } state = cpuset;
3456 	int dest_cpu;
3457 
3458 	/*
3459 	 * If the node that the CPU is on has been offlined, cpu_to_node()
3460 	 * will return -1. There is no CPU on the node, and we should
3461 	 * select the CPU on the other node.
3462 	 */
3463 	if (nid != -1) {
3464 		nodemask = cpumask_of_node(nid);
3465 
3466 		/* Look for allowed, online CPU in same node. */
3467 		for_each_cpu(dest_cpu, nodemask) {
3468 			if (is_cpu_allowed(p, dest_cpu))
3469 				return dest_cpu;
3470 		}
3471 	}
3472 
3473 	for (;;) {
3474 		/* Any allowed, online CPU? */
3475 		for_each_cpu(dest_cpu, p->cpus_ptr) {
3476 			if (!is_cpu_allowed(p, dest_cpu))
3477 				continue;
3478 
3479 			goto out;
3480 		}
3481 
3482 		/* No more Mr. Nice Guy. */
3483 		switch (state) {
3484 		case cpuset:
3485 			if (cpuset_cpus_allowed_fallback(p)) {
3486 				state = possible;
3487 				break;
3488 			}
3489 			fallthrough;
3490 		case possible:
3491 			/*
3492 			 * XXX When called from select_task_rq() we only
3493 			 * hold p->pi_lock and again violate locking order.
3494 			 *
3495 			 * More yuck to audit.
3496 			 */
3497 			do_set_cpus_allowed(p, task_cpu_possible_mask(p));
3498 			state = fail;
3499 			break;
3500 		case fail:
3501 			BUG();
3502 			break;
3503 		}
3504 	}
3505 
3506 out:
3507 	if (state != cpuset) {
3508 		/*
3509 		 * Don't tell them about moving exiting tasks or
3510 		 * kernel threads (both mm NULL), since they never
3511 		 * leave kernel.
3512 		 */
3513 		if (p->mm && printk_ratelimit()) {
3514 			printk_deferred("process %d (%s) no longer affine to cpu%d\n",
3515 					task_pid_nr(p), p->comm, cpu);
3516 		}
3517 	}
3518 
3519 	return dest_cpu;
3520 }
3521 
3522 /*
3523  * The caller (fork, wakeup) owns p->pi_lock, ->cpus_ptr is stable.
3524  */
3525 static inline
3526 int select_task_rq(struct task_struct *p, int cpu, int *wake_flags)
3527 {
3528 	lockdep_assert_held(&p->pi_lock);
3529 
3530 	if (p->nr_cpus_allowed > 1 && !is_migration_disabled(p)) {
3531 		cpu = p->sched_class->select_task_rq(p, cpu, *wake_flags);
3532 		*wake_flags |= WF_RQ_SELECTED;
3533 	} else {
3534 		cpu = cpumask_any(p->cpus_ptr);
3535 	}
3536 
3537 	/*
3538 	 * In order not to call set_task_cpu() on a blocking task we need
3539 	 * to rely on ttwu() to place the task on a valid ->cpus_ptr
3540 	 * CPU.
3541 	 *
3542 	 * Since this is common to all placement strategies, this lives here.
3543 	 *
3544 	 * [ this allows ->select_task() to simply return task_cpu(p) and
3545 	 *   not worry about this generic constraint ]
3546 	 */
3547 	if (unlikely(!is_cpu_allowed(p, cpu)))
3548 		cpu = select_fallback_rq(task_cpu(p), p);
3549 
3550 	return cpu;
3551 }
3552 
3553 void sched_set_stop_task(int cpu, struct task_struct *stop)
3554 {
3555 	static struct lock_class_key stop_pi_lock;
3556 	struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
3557 	struct task_struct *old_stop = cpu_rq(cpu)->stop;
3558 
3559 	if (stop) {
3560 		/*
3561 		 * Make it appear like a SCHED_FIFO task, its something
3562 		 * userspace knows about and won't get confused about.
3563 		 *
3564 		 * Also, it will make PI more or less work without too
3565 		 * much confusion -- but then, stop work should not
3566 		 * rely on PI working anyway.
3567 		 */
3568 		sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
3569 
3570 		stop->sched_class = &stop_sched_class;
3571 
3572 		/*
3573 		 * The PI code calls rt_mutex_setprio() with ->pi_lock held to
3574 		 * adjust the effective priority of a task. As a result,
3575 		 * rt_mutex_setprio() can trigger (RT) balancing operations,
3576 		 * which can then trigger wakeups of the stop thread to push
3577 		 * around the current task.
3578 		 *
3579 		 * The stop task itself will never be part of the PI-chain, it
3580 		 * never blocks, therefore that ->pi_lock recursion is safe.
3581 		 * Tell lockdep about this by placing the stop->pi_lock in its
3582 		 * own class.
3583 		 */
3584 		lockdep_set_class(&stop->pi_lock, &stop_pi_lock);
3585 	}
3586 
3587 	cpu_rq(cpu)->stop = stop;
3588 
3589 	if (old_stop) {
3590 		/*
3591 		 * Reset it back to a normal scheduling class so that
3592 		 * it can die in pieces.
3593 		 */
3594 		old_stop->sched_class = &rt_sched_class;
3595 	}
3596 }
3597 
3598 #else /* CONFIG_SMP */
3599 
3600 static inline void migrate_disable_switch(struct rq *rq, struct task_struct *p) { }
3601 
3602 static inline bool rq_has_pinned_tasks(struct rq *rq)
3603 {
3604 	return false;
3605 }
3606 
3607 #endif /* !CONFIG_SMP */
3608 
3609 static void
3610 ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
3611 {
3612 	struct rq *rq;
3613 
3614 	if (!schedstat_enabled())
3615 		return;
3616 
3617 	rq = this_rq();
3618 
3619 #ifdef CONFIG_SMP
3620 	if (cpu == rq->cpu) {
3621 		__schedstat_inc(rq->ttwu_local);
3622 		__schedstat_inc(p->stats.nr_wakeups_local);
3623 	} else {
3624 		struct sched_domain *sd;
3625 
3626 		__schedstat_inc(p->stats.nr_wakeups_remote);
3627 
3628 		guard(rcu)();
3629 		for_each_domain(rq->cpu, sd) {
3630 			if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
3631 				__schedstat_inc(sd->ttwu_wake_remote);
3632 				break;
3633 			}
3634 		}
3635 	}
3636 
3637 	if (wake_flags & WF_MIGRATED)
3638 		__schedstat_inc(p->stats.nr_wakeups_migrate);
3639 #endif /* CONFIG_SMP */
3640 
3641 	__schedstat_inc(rq->ttwu_count);
3642 	__schedstat_inc(p->stats.nr_wakeups);
3643 
3644 	if (wake_flags & WF_SYNC)
3645 		__schedstat_inc(p->stats.nr_wakeups_sync);
3646 }
3647 
3648 /*
3649  * Mark the task runnable.
3650  */
3651 static inline void ttwu_do_wakeup(struct task_struct *p)
3652 {
3653 	WRITE_ONCE(p->__state, TASK_RUNNING);
3654 	trace_sched_wakeup(p);
3655 }
3656 
3657 static void
3658 ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
3659 		 struct rq_flags *rf)
3660 {
3661 	int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK;
3662 
3663 	lockdep_assert_rq_held(rq);
3664 
3665 	if (p->sched_contributes_to_load)
3666 		rq->nr_uninterruptible--;
3667 
3668 #ifdef CONFIG_SMP
3669 	if (wake_flags & WF_RQ_SELECTED)
3670 		en_flags |= ENQUEUE_RQ_SELECTED;
3671 	if (wake_flags & WF_MIGRATED)
3672 		en_flags |= ENQUEUE_MIGRATED;
3673 	else
3674 #endif
3675 	if (p->in_iowait) {
3676 		delayacct_blkio_end(p);
3677 		atomic_dec(&task_rq(p)->nr_iowait);
3678 	}
3679 
3680 	activate_task(rq, p, en_flags);
3681 	wakeup_preempt(rq, p, wake_flags);
3682 
3683 	ttwu_do_wakeup(p);
3684 
3685 #ifdef CONFIG_SMP
3686 	if (p->sched_class->task_woken) {
3687 		/*
3688 		 * Our task @p is fully woken up and running; so it's safe to
3689 		 * drop the rq->lock, hereafter rq is only used for statistics.
3690 		 */
3691 		rq_unpin_lock(rq, rf);
3692 		p->sched_class->task_woken(rq, p);
3693 		rq_repin_lock(rq, rf);
3694 	}
3695 
3696 	if (rq->idle_stamp) {
3697 		u64 delta = rq_clock(rq) - rq->idle_stamp;
3698 		u64 max = 2*rq->max_idle_balance_cost;
3699 
3700 		update_avg(&rq->avg_idle, delta);
3701 
3702 		if (rq->avg_idle > max)
3703 			rq->avg_idle = max;
3704 
3705 		rq->idle_stamp = 0;
3706 	}
3707 #endif
3708 }
3709 
3710 /*
3711  * Consider @p being inside a wait loop:
3712  *
3713  *   for (;;) {
3714  *      set_current_state(TASK_UNINTERRUPTIBLE);
3715  *
3716  *      if (CONDITION)
3717  *         break;
3718  *
3719  *      schedule();
3720  *   }
3721  *   __set_current_state(TASK_RUNNING);
3722  *
3723  * between set_current_state() and schedule(). In this case @p is still
3724  * runnable, so all that needs doing is change p->state back to TASK_RUNNING in
3725  * an atomic manner.
3726  *
3727  * By taking task_rq(p)->lock we serialize against schedule(), if @p->on_rq
3728  * then schedule() must still happen and p->state can be changed to
3729  * TASK_RUNNING. Otherwise we lost the race, schedule() has happened, and we
3730  * need to do a full wakeup with enqueue.
3731  *
3732  * Returns: %true when the wakeup is done,
3733  *          %false otherwise.
3734  */
3735 static int ttwu_runnable(struct task_struct *p, int wake_flags)
3736 {
3737 	struct rq_flags rf;
3738 	struct rq *rq;
3739 	int ret = 0;
3740 
3741 	rq = __task_rq_lock(p, &rf);
3742 	if (task_on_rq_queued(p)) {
3743 		update_rq_clock(rq);
3744 		if (p->se.sched_delayed)
3745 			enqueue_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_DELAYED);
3746 		if (!task_on_cpu(rq, p)) {
3747 			/*
3748 			 * When on_rq && !on_cpu the task is preempted, see if
3749 			 * it should preempt the task that is current now.
3750 			 */
3751 			wakeup_preempt(rq, p, wake_flags);
3752 		}
3753 		ttwu_do_wakeup(p);
3754 		ret = 1;
3755 	}
3756 	__task_rq_unlock(rq, &rf);
3757 
3758 	return ret;
3759 }
3760 
3761 #ifdef CONFIG_SMP
3762 void sched_ttwu_pending(void *arg)
3763 {
3764 	struct llist_node *llist = arg;
3765 	struct rq *rq = this_rq();
3766 	struct task_struct *p, *t;
3767 	struct rq_flags rf;
3768 
3769 	if (!llist)
3770 		return;
3771 
3772 	rq_lock_irqsave(rq, &rf);
3773 	update_rq_clock(rq);
3774 
3775 	llist_for_each_entry_safe(p, t, llist, wake_entry.llist) {
3776 		if (WARN_ON_ONCE(p->on_cpu))
3777 			smp_cond_load_acquire(&p->on_cpu, !VAL);
3778 
3779 		if (WARN_ON_ONCE(task_cpu(p) != cpu_of(rq)))
3780 			set_task_cpu(p, cpu_of(rq));
3781 
3782 		ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf);
3783 	}
3784 
3785 	/*
3786 	 * Must be after enqueueing at least once task such that
3787 	 * idle_cpu() does not observe a false-negative -- if it does,
3788 	 * it is possible for select_idle_siblings() to stack a number
3789 	 * of tasks on this CPU during that window.
3790 	 *
3791 	 * It is OK to clear ttwu_pending when another task pending.
3792 	 * We will receive IPI after local IRQ enabled and then enqueue it.
3793 	 * Since now nr_running > 0, idle_cpu() will always get correct result.
3794 	 */
3795 	WRITE_ONCE(rq->ttwu_pending, 0);
3796 	rq_unlock_irqrestore(rq, &rf);
3797 }
3798 
3799 /*
3800  * Prepare the scene for sending an IPI for a remote smp_call
3801  *
3802  * Returns true if the caller can proceed with sending the IPI.
3803  * Returns false otherwise.
3804  */
3805 bool call_function_single_prep_ipi(int cpu)
3806 {
3807 	if (set_nr_if_polling(cpu_rq(cpu)->idle)) {
3808 		trace_sched_wake_idle_without_ipi(cpu);
3809 		return false;
3810 	}
3811 
3812 	return true;
3813 }
3814 
3815 /*
3816  * Queue a task on the target CPUs wake_list and wake the CPU via IPI if
3817  * necessary. The wakee CPU on receipt of the IPI will queue the task
3818  * via sched_ttwu_wakeup() for activation so the wakee incurs the cost
3819  * of the wakeup instead of the waker.
3820  */
3821 static void __ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
3822 {
3823 	struct rq *rq = cpu_rq(cpu);
3824 
3825 	p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
3826 
3827 	WRITE_ONCE(rq->ttwu_pending, 1);
3828 	__smp_call_single_queue(cpu, &p->wake_entry.llist);
3829 }
3830 
3831 void wake_up_if_idle(int cpu)
3832 {
3833 	struct rq *rq = cpu_rq(cpu);
3834 
3835 	guard(rcu)();
3836 	if (is_idle_task(rcu_dereference(rq->curr))) {
3837 		guard(rq_lock_irqsave)(rq);
3838 		if (is_idle_task(rq->curr))
3839 			resched_curr(rq);
3840 	}
3841 }
3842 
3843 bool cpus_equal_capacity(int this_cpu, int that_cpu)
3844 {
3845 	if (!sched_asym_cpucap_active())
3846 		return true;
3847 
3848 	if (this_cpu == that_cpu)
3849 		return true;
3850 
3851 	return arch_scale_cpu_capacity(this_cpu) == arch_scale_cpu_capacity(that_cpu);
3852 }
3853 
3854 bool cpus_share_cache(int this_cpu, int that_cpu)
3855 {
3856 	if (this_cpu == that_cpu)
3857 		return true;
3858 
3859 	return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
3860 }
3861 
3862 /*
3863  * Whether CPUs are share cache resources, which means LLC on non-cluster
3864  * machines and LLC tag or L2 on machines with clusters.
3865  */
3866 bool cpus_share_resources(int this_cpu, int that_cpu)
3867 {
3868 	if (this_cpu == that_cpu)
3869 		return true;
3870 
3871 	return per_cpu(sd_share_id, this_cpu) == per_cpu(sd_share_id, that_cpu);
3872 }
3873 
3874 static inline bool ttwu_queue_cond(struct task_struct *p, int cpu)
3875 {
3876 	/*
3877 	 * The BPF scheduler may depend on select_task_rq() being invoked during
3878 	 * wakeups. In addition, @p may end up executing on a different CPU
3879 	 * regardless of what happens in the wakeup path making the ttwu_queue
3880 	 * optimization less meaningful. Skip if on SCX.
3881 	 */
3882 	if (task_on_scx(p))
3883 		return false;
3884 
3885 	/*
3886 	 * Do not complicate things with the async wake_list while the CPU is
3887 	 * in hotplug state.
3888 	 */
3889 	if (!cpu_active(cpu))
3890 		return false;
3891 
3892 	/* Ensure the task will still be allowed to run on the CPU. */
3893 	if (!cpumask_test_cpu(cpu, p->cpus_ptr))
3894 		return false;
3895 
3896 	/*
3897 	 * If the CPU does not share cache, then queue the task on the
3898 	 * remote rqs wakelist to avoid accessing remote data.
3899 	 */
3900 	if (!cpus_share_cache(smp_processor_id(), cpu))
3901 		return true;
3902 
3903 	if (cpu == smp_processor_id())
3904 		return false;
3905 
3906 	/*
3907 	 * If the wakee cpu is idle, or the task is descheduling and the
3908 	 * only running task on the CPU, then use the wakelist to offload
3909 	 * the task activation to the idle (or soon-to-be-idle) CPU as
3910 	 * the current CPU is likely busy. nr_running is checked to
3911 	 * avoid unnecessary task stacking.
3912 	 *
3913 	 * Note that we can only get here with (wakee) p->on_rq=0,
3914 	 * p->on_cpu can be whatever, we've done the dequeue, so
3915 	 * the wakee has been accounted out of ->nr_running.
3916 	 */
3917 	if (!cpu_rq(cpu)->nr_running)
3918 		return true;
3919 
3920 	return false;
3921 }
3922 
3923 static bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
3924 {
3925 	if (sched_feat(TTWU_QUEUE) && ttwu_queue_cond(p, cpu)) {
3926 		sched_clock_cpu(cpu); /* Sync clocks across CPUs */
3927 		__ttwu_queue_wakelist(p, cpu, wake_flags);
3928 		return true;
3929 	}
3930 
3931 	return false;
3932 }
3933 
3934 #else /* !CONFIG_SMP */
3935 
3936 static inline bool ttwu_queue_wakelist(struct task_struct *p, int cpu, int wake_flags)
3937 {
3938 	return false;
3939 }
3940 
3941 #endif /* CONFIG_SMP */
3942 
3943 static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
3944 {
3945 	struct rq *rq = cpu_rq(cpu);
3946 	struct rq_flags rf;
3947 
3948 	if (ttwu_queue_wakelist(p, cpu, wake_flags))
3949 		return;
3950 
3951 	rq_lock(rq, &rf);
3952 	update_rq_clock(rq);
3953 	ttwu_do_activate(rq, p, wake_flags, &rf);
3954 	rq_unlock(rq, &rf);
3955 }
3956 
3957 /*
3958  * Invoked from try_to_wake_up() to check whether the task can be woken up.
3959  *
3960  * The caller holds p::pi_lock if p != current or has preemption
3961  * disabled when p == current.
3962  *
3963  * The rules of saved_state:
3964  *
3965  *   The related locking code always holds p::pi_lock when updating
3966  *   p::saved_state, which means the code is fully serialized in both cases.
3967  *
3968  *   For PREEMPT_RT, the lock wait and lock wakeups happen via TASK_RTLOCK_WAIT.
3969  *   No other bits set. This allows to distinguish all wakeup scenarios.
3970  *
3971  *   For FREEZER, the wakeup happens via TASK_FROZEN. No other bits set. This
3972  *   allows us to prevent early wakeup of tasks before they can be run on
3973  *   asymmetric ISA architectures (eg ARMv9).
3974  */
3975 static __always_inline
3976 bool ttwu_state_match(struct task_struct *p, unsigned int state, int *success)
3977 {
3978 	int match;
3979 
3980 	if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)) {
3981 		WARN_ON_ONCE((state & TASK_RTLOCK_WAIT) &&
3982 			     state != TASK_RTLOCK_WAIT);
3983 	}
3984 
3985 	*success = !!(match = __task_state_match(p, state));
3986 
3987 	/*
3988 	 * Saved state preserves the task state across blocking on
3989 	 * an RT lock or TASK_FREEZABLE tasks.  If the state matches,
3990 	 * set p::saved_state to TASK_RUNNING, but do not wake the task
3991 	 * because it waits for a lock wakeup or __thaw_task(). Also
3992 	 * indicate success because from the regular waker's point of
3993 	 * view this has succeeded.
3994 	 *
3995 	 * After acquiring the lock the task will restore p::__state
3996 	 * from p::saved_state which ensures that the regular
3997 	 * wakeup is not lost. The restore will also set
3998 	 * p::saved_state to TASK_RUNNING so any further tests will
3999 	 * not result in false positives vs. @success
4000 	 */
4001 	if (match < 0)
4002 		p->saved_state = TASK_RUNNING;
4003 
4004 	return match > 0;
4005 }
4006 
4007 /*
4008  * Notes on Program-Order guarantees on SMP systems.
4009  *
4010  *  MIGRATION
4011  *
4012  * The basic program-order guarantee on SMP systems is that when a task [t]
4013  * migrates, all its activity on its old CPU [c0] happens-before any subsequent
4014  * execution on its new CPU [c1].
4015  *
4016  * For migration (of runnable tasks) this is provided by the following means:
4017  *
4018  *  A) UNLOCK of the rq(c0)->lock scheduling out task t
4019  *  B) migration for t is required to synchronize *both* rq(c0)->lock and
4020  *     rq(c1)->lock (if not at the same time, then in that order).
4021  *  C) LOCK of the rq(c1)->lock scheduling in task
4022  *
4023  * Release/acquire chaining guarantees that B happens after A and C after B.
4024  * Note: the CPU doing B need not be c0 or c1
4025  *
4026  * Example:
4027  *
4028  *   CPU0            CPU1            CPU2
4029  *
4030  *   LOCK rq(0)->lock
4031  *   sched-out X
4032  *   sched-in Y
4033  *   UNLOCK rq(0)->lock
4034  *
4035  *                                   LOCK rq(0)->lock // orders against CPU0
4036  *                                   dequeue X
4037  *                                   UNLOCK rq(0)->lock
4038  *
4039  *                                   LOCK rq(1)->lock
4040  *                                   enqueue X
4041  *                                   UNLOCK rq(1)->lock
4042  *
4043  *                   LOCK rq(1)->lock // orders against CPU2
4044  *                   sched-out Z
4045  *                   sched-in X
4046  *                   UNLOCK rq(1)->lock
4047  *
4048  *
4049  *  BLOCKING -- aka. SLEEP + WAKEUP
4050  *
4051  * For blocking we (obviously) need to provide the same guarantee as for
4052  * migration. However the means are completely different as there is no lock
4053  * chain to provide order. Instead we do:
4054  *
4055  *   1) smp_store_release(X->on_cpu, 0)   -- finish_task()
4056  *   2) smp_cond_load_acquire(!X->on_cpu) -- try_to_wake_up()
4057  *
4058  * Example:
4059  *
4060  *   CPU0 (schedule)  CPU1 (try_to_wake_up) CPU2 (schedule)
4061  *
4062  *   LOCK rq(0)->lock LOCK X->pi_lock
4063  *   dequeue X
4064  *   sched-out X
4065  *   smp_store_release(X->on_cpu, 0);
4066  *
4067  *                    smp_cond_load_acquire(&X->on_cpu, !VAL);
4068  *                    X->state = WAKING
4069  *                    set_task_cpu(X,2)
4070  *
4071  *                    LOCK rq(2)->lock
4072  *                    enqueue X
4073  *                    X->state = RUNNING
4074  *                    UNLOCK rq(2)->lock
4075  *
4076  *                                          LOCK rq(2)->lock // orders against CPU1
4077  *                                          sched-out Z
4078  *                                          sched-in X
4079  *                                          UNLOCK rq(2)->lock
4080  *
4081  *                    UNLOCK X->pi_lock
4082  *   UNLOCK rq(0)->lock
4083  *
4084  *
4085  * However, for wakeups there is a second guarantee we must provide, namely we
4086  * must ensure that CONDITION=1 done by the caller can not be reordered with
4087  * accesses to the task state; see try_to_wake_up() and set_current_state().
4088  */
4089 
4090 /**
4091  * try_to_wake_up - wake up a thread
4092  * @p: the thread to be awakened
4093  * @state: the mask of task states that can be woken
4094  * @wake_flags: wake modifier flags (WF_*)
4095  *
4096  * Conceptually does:
4097  *
4098  *   If (@state & @p->state) @p->state = TASK_RUNNING.
4099  *
4100  * If the task was not queued/runnable, also place it back on a runqueue.
4101  *
4102  * This function is atomic against schedule() which would dequeue the task.
4103  *
4104  * It issues a full memory barrier before accessing @p->state, see the comment
4105  * with set_current_state().
4106  *
4107  * Uses p->pi_lock to serialize against concurrent wake-ups.
4108  *
4109  * Relies on p->pi_lock stabilizing:
4110  *  - p->sched_class
4111  *  - p->cpus_ptr
4112  *  - p->sched_task_group
4113  * in order to do migration, see its use of select_task_rq()/set_task_cpu().
4114  *
4115  * Tries really hard to only take one task_rq(p)->lock for performance.
4116  * Takes rq->lock in:
4117  *  - ttwu_runnable()    -- old rq, unavoidable, see comment there;
4118  *  - ttwu_queue()       -- new rq, for enqueue of the task;
4119  *  - psi_ttwu_dequeue() -- much sadness :-( accounting will kill us.
4120  *
4121  * As a consequence we race really badly with just about everything. See the
4122  * many memory barriers and their comments for details.
4123  *
4124  * Return: %true if @p->state changes (an actual wakeup was done),
4125  *	   %false otherwise.
4126  */
4127 int try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
4128 {
4129 	guard(preempt)();
4130 	int cpu, success = 0;
4131 
4132 	wake_flags |= WF_TTWU;
4133 
4134 	if (p == current) {
4135 		/*
4136 		 * We're waking current, this means 'p->on_rq' and 'task_cpu(p)
4137 		 * == smp_processor_id()'. Together this means we can special
4138 		 * case the whole 'p->on_rq && ttwu_runnable()' case below
4139 		 * without taking any locks.
4140 		 *
4141 		 * Specifically, given current runs ttwu() we must be before
4142 		 * schedule()'s block_task(), as such this must not observe
4143 		 * sched_delayed.
4144 		 *
4145 		 * In particular:
4146 		 *  - we rely on Program-Order guarantees for all the ordering,
4147 		 *  - we're serialized against set_special_state() by virtue of
4148 		 *    it disabling IRQs (this allows not taking ->pi_lock).
4149 		 */
4150 		SCHED_WARN_ON(p->se.sched_delayed);
4151 		if (!ttwu_state_match(p, state, &success))
4152 			goto out;
4153 
4154 		trace_sched_waking(p);
4155 		ttwu_do_wakeup(p);
4156 		goto out;
4157 	}
4158 
4159 	/*
4160 	 * If we are going to wake up a thread waiting for CONDITION we
4161 	 * need to ensure that CONDITION=1 done by the caller can not be
4162 	 * reordered with p->state check below. This pairs with smp_store_mb()
4163 	 * in set_current_state() that the waiting thread does.
4164 	 */
4165 	scoped_guard (raw_spinlock_irqsave, &p->pi_lock) {
4166 		smp_mb__after_spinlock();
4167 		if (!ttwu_state_match(p, state, &success))
4168 			break;
4169 
4170 		trace_sched_waking(p);
4171 
4172 		/*
4173 		 * Ensure we load p->on_rq _after_ p->state, otherwise it would
4174 		 * be possible to, falsely, observe p->on_rq == 0 and get stuck
4175 		 * in smp_cond_load_acquire() below.
4176 		 *
4177 		 * sched_ttwu_pending()			try_to_wake_up()
4178 		 *   STORE p->on_rq = 1			  LOAD p->state
4179 		 *   UNLOCK rq->lock
4180 		 *
4181 		 * __schedule() (switch to task 'p')
4182 		 *   LOCK rq->lock			  smp_rmb();
4183 		 *   smp_mb__after_spinlock();
4184 		 *   UNLOCK rq->lock
4185 		 *
4186 		 * [task p]
4187 		 *   STORE p->state = UNINTERRUPTIBLE	  LOAD p->on_rq
4188 		 *
4189 		 * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
4190 		 * __schedule().  See the comment for smp_mb__after_spinlock().
4191 		 *
4192 		 * A similar smp_rmb() lives in __task_needs_rq_lock().
4193 		 */
4194 		smp_rmb();
4195 		if (READ_ONCE(p->on_rq) && ttwu_runnable(p, wake_flags))
4196 			break;
4197 
4198 #ifdef CONFIG_SMP
4199 		/*
4200 		 * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
4201 		 * possible to, falsely, observe p->on_cpu == 0.
4202 		 *
4203 		 * One must be running (->on_cpu == 1) in order to remove oneself
4204 		 * from the runqueue.
4205 		 *
4206 		 * __schedule() (switch to task 'p')	try_to_wake_up()
4207 		 *   STORE p->on_cpu = 1		  LOAD p->on_rq
4208 		 *   UNLOCK rq->lock
4209 		 *
4210 		 * __schedule() (put 'p' to sleep)
4211 		 *   LOCK rq->lock			  smp_rmb();
4212 		 *   smp_mb__after_spinlock();
4213 		 *   STORE p->on_rq = 0			  LOAD p->on_cpu
4214 		 *
4215 		 * Pairs with the LOCK+smp_mb__after_spinlock() on rq->lock in
4216 		 * __schedule().  See the comment for smp_mb__after_spinlock().
4217 		 *
4218 		 * Form a control-dep-acquire with p->on_rq == 0 above, to ensure
4219 		 * schedule()'s deactivate_task() has 'happened' and p will no longer
4220 		 * care about it's own p->state. See the comment in __schedule().
4221 		 */
4222 		smp_acquire__after_ctrl_dep();
4223 
4224 		/*
4225 		 * We're doing the wakeup (@success == 1), they did a dequeue (p->on_rq
4226 		 * == 0), which means we need to do an enqueue, change p->state to
4227 		 * TASK_WAKING such that we can unlock p->pi_lock before doing the
4228 		 * enqueue, such as ttwu_queue_wakelist().
4229 		 */
4230 		WRITE_ONCE(p->__state, TASK_WAKING);
4231 
4232 		/*
4233 		 * If the owning (remote) CPU is still in the middle of schedule() with
4234 		 * this task as prev, considering queueing p on the remote CPUs wake_list
4235 		 * which potentially sends an IPI instead of spinning on p->on_cpu to
4236 		 * let the waker make forward progress. This is safe because IRQs are
4237 		 * disabled and the IPI will deliver after on_cpu is cleared.
4238 		 *
4239 		 * Ensure we load task_cpu(p) after p->on_cpu:
4240 		 *
4241 		 * set_task_cpu(p, cpu);
4242 		 *   STORE p->cpu = @cpu
4243 		 * __schedule() (switch to task 'p')
4244 		 *   LOCK rq->lock
4245 		 *   smp_mb__after_spin_lock()		smp_cond_load_acquire(&p->on_cpu)
4246 		 *   STORE p->on_cpu = 1		LOAD p->cpu
4247 		 *
4248 		 * to ensure we observe the correct CPU on which the task is currently
4249 		 * scheduling.
4250 		 */
4251 		if (smp_load_acquire(&p->on_cpu) &&
4252 		    ttwu_queue_wakelist(p, task_cpu(p), wake_flags))
4253 			break;
4254 
4255 		/*
4256 		 * If the owning (remote) CPU is still in the middle of schedule() with
4257 		 * this task as prev, wait until it's done referencing the task.
4258 		 *
4259 		 * Pairs with the smp_store_release() in finish_task().
4260 		 *
4261 		 * This ensures that tasks getting woken will be fully ordered against
4262 		 * their previous state and preserve Program Order.
4263 		 */
4264 		smp_cond_load_acquire(&p->on_cpu, !VAL);
4265 
4266 		cpu = select_task_rq(p, p->wake_cpu, &wake_flags);
4267 		if (task_cpu(p) != cpu) {
4268 			if (p->in_iowait) {
4269 				delayacct_blkio_end(p);
4270 				atomic_dec(&task_rq(p)->nr_iowait);
4271 			}
4272 
4273 			wake_flags |= WF_MIGRATED;
4274 			psi_ttwu_dequeue(p);
4275 			set_task_cpu(p, cpu);
4276 		}
4277 #else
4278 		cpu = task_cpu(p);
4279 #endif /* CONFIG_SMP */
4280 
4281 		ttwu_queue(p, cpu, wake_flags);
4282 	}
4283 out:
4284 	if (success)
4285 		ttwu_stat(p, task_cpu(p), wake_flags);
4286 
4287 	return success;
4288 }
4289 
4290 static bool __task_needs_rq_lock(struct task_struct *p)
4291 {
4292 	unsigned int state = READ_ONCE(p->__state);
4293 
4294 	/*
4295 	 * Since pi->lock blocks try_to_wake_up(), we don't need rq->lock when
4296 	 * the task is blocked. Make sure to check @state since ttwu() can drop
4297 	 * locks at the end, see ttwu_queue_wakelist().
4298 	 */
4299 	if (state == TASK_RUNNING || state == TASK_WAKING)
4300 		return true;
4301 
4302 	/*
4303 	 * Ensure we load p->on_rq after p->__state, otherwise it would be
4304 	 * possible to, falsely, observe p->on_rq == 0.
4305 	 *
4306 	 * See try_to_wake_up() for a longer comment.
4307 	 */
4308 	smp_rmb();
4309 	if (p->on_rq)
4310 		return true;
4311 
4312 #ifdef CONFIG_SMP
4313 	/*
4314 	 * Ensure the task has finished __schedule() and will not be referenced
4315 	 * anymore. Again, see try_to_wake_up() for a longer comment.
4316 	 */
4317 	smp_rmb();
4318 	smp_cond_load_acquire(&p->on_cpu, !VAL);
4319 #endif
4320 
4321 	return false;
4322 }
4323 
4324 /**
4325  * task_call_func - Invoke a function on task in fixed state
4326  * @p: Process for which the function is to be invoked, can be @current.
4327  * @func: Function to invoke.
4328  * @arg: Argument to function.
4329  *
4330  * Fix the task in it's current state by avoiding wakeups and or rq operations
4331  * and call @func(@arg) on it.  This function can use task_is_runnable() and
4332  * task_curr() to work out what the state is, if required.  Given that @func
4333  * can be invoked with a runqueue lock held, it had better be quite
4334  * lightweight.
4335  *
4336  * Returns:
4337  *   Whatever @func returns
4338  */
4339 int task_call_func(struct task_struct *p, task_call_f func, void *arg)
4340 {
4341 	struct rq *rq = NULL;
4342 	struct rq_flags rf;
4343 	int ret;
4344 
4345 	raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
4346 
4347 	if (__task_needs_rq_lock(p))
4348 		rq = __task_rq_lock(p, &rf);
4349 
4350 	/*
4351 	 * At this point the task is pinned; either:
4352 	 *  - blocked and we're holding off wakeups	 (pi->lock)
4353 	 *  - woken, and we're holding off enqueue	 (rq->lock)
4354 	 *  - queued, and we're holding off schedule	 (rq->lock)
4355 	 *  - running, and we're holding off de-schedule (rq->lock)
4356 	 *
4357 	 * The called function (@func) can use: task_curr(), p->on_rq and
4358 	 * p->__state to differentiate between these states.
4359 	 */
4360 	ret = func(p, arg);
4361 
4362 	if (rq)
4363 		rq_unlock(rq, &rf);
4364 
4365 	raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags);
4366 	return ret;
4367 }
4368 
4369 /**
4370  * cpu_curr_snapshot - Return a snapshot of the currently running task
4371  * @cpu: The CPU on which to snapshot the task.
4372  *
4373  * Returns the task_struct pointer of the task "currently" running on
4374  * the specified CPU.
4375  *
4376  * If the specified CPU was offline, the return value is whatever it
4377  * is, perhaps a pointer to the task_struct structure of that CPU's idle
4378  * task, but there is no guarantee.  Callers wishing a useful return
4379  * value must take some action to ensure that the specified CPU remains
4380  * online throughout.
4381  *
4382  * This function executes full memory barriers before and after fetching
4383  * the pointer, which permits the caller to confine this function's fetch
4384  * with respect to the caller's accesses to other shared variables.
4385  */
4386 struct task_struct *cpu_curr_snapshot(int cpu)
4387 {
4388 	struct rq *rq = cpu_rq(cpu);
4389 	struct task_struct *t;
4390 	struct rq_flags rf;
4391 
4392 	rq_lock_irqsave(rq, &rf);
4393 	smp_mb__after_spinlock(); /* Pairing determined by caller's synchronization design. */
4394 	t = rcu_dereference(cpu_curr(cpu));
4395 	rq_unlock_irqrestore(rq, &rf);
4396 	smp_mb(); /* Pairing determined by caller's synchronization design. */
4397 
4398 	return t;
4399 }
4400 
4401 /**
4402  * wake_up_process - Wake up a specific process
4403  * @p: The process to be woken up.
4404  *
4405  * Attempt to wake up the nominated process and move it to the set of runnable
4406  * processes.
4407  *
4408  * Return: 1 if the process was woken up, 0 if it was already running.
4409  *
4410  * This function executes a full memory barrier before accessing the task state.
4411  */
4412 int wake_up_process(struct task_struct *p)
4413 {
4414 	return try_to_wake_up(p, TASK_NORMAL, 0);
4415 }
4416 EXPORT_SYMBOL(wake_up_process);
4417 
4418 int wake_up_state(struct task_struct *p, unsigned int state)
4419 {
4420 	return try_to_wake_up(p, state, 0);
4421 }
4422 
4423 /*
4424  * Perform scheduler related setup for a newly forked process p.
4425  * p is forked by current.
4426  *
4427  * __sched_fork() is basic setup used by init_idle() too:
4428  */
4429 static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
4430 {
4431 	p->on_rq			= 0;
4432 
4433 	p->se.on_rq			= 0;
4434 	p->se.exec_start		= 0;
4435 	p->se.sum_exec_runtime		= 0;
4436 	p->se.prev_sum_exec_runtime	= 0;
4437 	p->se.nr_migrations		= 0;
4438 	p->se.vruntime			= 0;
4439 	p->se.vlag			= 0;
4440 	INIT_LIST_HEAD(&p->se.group_node);
4441 
4442 	/* A delayed task cannot be in clone(). */
4443 	SCHED_WARN_ON(p->se.sched_delayed);
4444 
4445 #ifdef CONFIG_FAIR_GROUP_SCHED
4446 	p->se.cfs_rq			= NULL;
4447 #endif
4448 
4449 #ifdef CONFIG_SCHEDSTATS
4450 	/* Even if schedstat is disabled, there should not be garbage */
4451 	memset(&p->stats, 0, sizeof(p->stats));
4452 #endif
4453 
4454 	init_dl_entity(&p->dl);
4455 
4456 	INIT_LIST_HEAD(&p->rt.run_list);
4457 	p->rt.timeout		= 0;
4458 	p->rt.time_slice	= sched_rr_timeslice;
4459 	p->rt.on_rq		= 0;
4460 	p->rt.on_list		= 0;
4461 
4462 #ifdef CONFIG_SCHED_CLASS_EXT
4463 	init_scx_entity(&p->scx);
4464 #endif
4465 
4466 #ifdef CONFIG_PREEMPT_NOTIFIERS
4467 	INIT_HLIST_HEAD(&p->preempt_notifiers);
4468 #endif
4469 
4470 #ifdef CONFIG_COMPACTION
4471 	p->capture_control = NULL;
4472 #endif
4473 	init_numa_balancing(clone_flags, p);
4474 #ifdef CONFIG_SMP
4475 	p->wake_entry.u_flags = CSD_TYPE_TTWU;
4476 	p->migration_pending = NULL;
4477 #endif
4478 	init_sched_mm_cid(p);
4479 }
4480 
4481 DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
4482 
4483 #ifdef CONFIG_NUMA_BALANCING
4484 
4485 int sysctl_numa_balancing_mode;
4486 
4487 static void __set_numabalancing_state(bool enabled)
4488 {
4489 	if (enabled)
4490 		static_branch_enable(&sched_numa_balancing);
4491 	else
4492 		static_branch_disable(&sched_numa_balancing);
4493 }
4494 
4495 void set_numabalancing_state(bool enabled)
4496 {
4497 	if (enabled)
4498 		sysctl_numa_balancing_mode = NUMA_BALANCING_NORMAL;
4499 	else
4500 		sysctl_numa_balancing_mode = NUMA_BALANCING_DISABLED;
4501 	__set_numabalancing_state(enabled);
4502 }
4503 
4504 #ifdef CONFIG_PROC_SYSCTL
4505 static void reset_memory_tiering(void)
4506 {
4507 	struct pglist_data *pgdat;
4508 
4509 	for_each_online_pgdat(pgdat) {
4510 		pgdat->nbp_threshold = 0;
4511 		pgdat->nbp_th_nr_cand = node_page_state(pgdat, PGPROMOTE_CANDIDATE);
4512 		pgdat->nbp_th_start = jiffies_to_msecs(jiffies);
4513 	}
4514 }
4515 
4516 static int sysctl_numa_balancing(const struct ctl_table *table, int write,
4517 			  void *buffer, size_t *lenp, loff_t *ppos)
4518 {
4519 	struct ctl_table t;
4520 	int err;
4521 	int state = sysctl_numa_balancing_mode;
4522 
4523 	if (write && !capable(CAP_SYS_ADMIN))
4524 		return -EPERM;
4525 
4526 	t = *table;
4527 	t.data = &state;
4528 	err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
4529 	if (err < 0)
4530 		return err;
4531 	if (write) {
4532 		if (!(sysctl_numa_balancing_mode & NUMA_BALANCING_MEMORY_TIERING) &&
4533 		    (state & NUMA_BALANCING_MEMORY_TIERING))
4534 			reset_memory_tiering();
4535 		sysctl_numa_balancing_mode = state;
4536 		__set_numabalancing_state(state);
4537 	}
4538 	return err;
4539 }
4540 #endif
4541 #endif
4542 
4543 #ifdef CONFIG_SCHEDSTATS
4544 
4545 DEFINE_STATIC_KEY_FALSE(sched_schedstats);
4546 
4547 static void set_schedstats(bool enabled)
4548 {
4549 	if (enabled)
4550 		static_branch_enable(&sched_schedstats);
4551 	else
4552 		static_branch_disable(&sched_schedstats);
4553 }
4554 
4555 void force_schedstat_enabled(void)
4556 {
4557 	if (!schedstat_enabled()) {
4558 		pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n");
4559 		static_branch_enable(&sched_schedstats);
4560 	}
4561 }
4562 
4563 static int __init setup_schedstats(char *str)
4564 {
4565 	int ret = 0;
4566 	if (!str)
4567 		goto out;
4568 
4569 	if (!strcmp(str, "enable")) {
4570 		set_schedstats(true);
4571 		ret = 1;
4572 	} else if (!strcmp(str, "disable")) {
4573 		set_schedstats(false);
4574 		ret = 1;
4575 	}
4576 out:
4577 	if (!ret)
4578 		pr_warn("Unable to parse schedstats=\n");
4579 
4580 	return ret;
4581 }
4582 __setup("schedstats=", setup_schedstats);
4583 
4584 #ifdef CONFIG_PROC_SYSCTL
4585 static int sysctl_schedstats(const struct ctl_table *table, int write, void *buffer,
4586 		size_t *lenp, loff_t *ppos)
4587 {
4588 	struct ctl_table t;
4589 	int err;
4590 	int state = static_branch_likely(&sched_schedstats);
4591 
4592 	if (write && !capable(CAP_SYS_ADMIN))
4593 		return -EPERM;
4594 
4595 	t = *table;
4596 	t.data = &state;
4597 	err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
4598 	if (err < 0)
4599 		return err;
4600 	if (write)
4601 		set_schedstats(state);
4602 	return err;
4603 }
4604 #endif /* CONFIG_PROC_SYSCTL */
4605 #endif /* CONFIG_SCHEDSTATS */
4606 
4607 #ifdef CONFIG_SYSCTL
4608 static struct ctl_table sched_core_sysctls[] = {
4609 #ifdef CONFIG_SCHEDSTATS
4610 	{
4611 		.procname       = "sched_schedstats",
4612 		.data           = NULL,
4613 		.maxlen         = sizeof(unsigned int),
4614 		.mode           = 0644,
4615 		.proc_handler   = sysctl_schedstats,
4616 		.extra1         = SYSCTL_ZERO,
4617 		.extra2         = SYSCTL_ONE,
4618 	},
4619 #endif /* CONFIG_SCHEDSTATS */
4620 #ifdef CONFIG_UCLAMP_TASK
4621 	{
4622 		.procname       = "sched_util_clamp_min",
4623 		.data           = &sysctl_sched_uclamp_util_min,
4624 		.maxlen         = sizeof(unsigned int),
4625 		.mode           = 0644,
4626 		.proc_handler   = sysctl_sched_uclamp_handler,
4627 	},
4628 	{
4629 		.procname       = "sched_util_clamp_max",
4630 		.data           = &sysctl_sched_uclamp_util_max,
4631 		.maxlen         = sizeof(unsigned int),
4632 		.mode           = 0644,
4633 		.proc_handler   = sysctl_sched_uclamp_handler,
4634 	},
4635 	{
4636 		.procname       = "sched_util_clamp_min_rt_default",
4637 		.data           = &sysctl_sched_uclamp_util_min_rt_default,
4638 		.maxlen         = sizeof(unsigned int),
4639 		.mode           = 0644,
4640 		.proc_handler   = sysctl_sched_uclamp_handler,
4641 	},
4642 #endif /* CONFIG_UCLAMP_TASK */
4643 #ifdef CONFIG_NUMA_BALANCING
4644 	{
4645 		.procname	= "numa_balancing",
4646 		.data		= NULL, /* filled in by handler */
4647 		.maxlen		= sizeof(unsigned int),
4648 		.mode		= 0644,
4649 		.proc_handler	= sysctl_numa_balancing,
4650 		.extra1		= SYSCTL_ZERO,
4651 		.extra2		= SYSCTL_FOUR,
4652 	},
4653 #endif /* CONFIG_NUMA_BALANCING */
4654 };
4655 static int __init sched_core_sysctl_init(void)
4656 {
4657 	register_sysctl_init("kernel", sched_core_sysctls);
4658 	return 0;
4659 }
4660 late_initcall(sched_core_sysctl_init);
4661 #endif /* CONFIG_SYSCTL */
4662 
4663 /*
4664  * fork()/clone()-time setup:
4665  */
4666 int sched_fork(unsigned long clone_flags, struct task_struct *p)
4667 {
4668 	__sched_fork(clone_flags, p);
4669 	/*
4670 	 * We mark the process as NEW here. This guarantees that
4671 	 * nobody will actually run it, and a signal or other external
4672 	 * event cannot wake it up and insert it on the runqueue either.
4673 	 */
4674 	p->__state = TASK_NEW;
4675 
4676 	/*
4677 	 * Make sure we do not leak PI boosting priority to the child.
4678 	 */
4679 	p->prio = current->normal_prio;
4680 
4681 	uclamp_fork(p);
4682 
4683 	/*
4684 	 * Revert to default priority/policy on fork if requested.
4685 	 */
4686 	if (unlikely(p->sched_reset_on_fork)) {
4687 		if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
4688 			p->policy = SCHED_NORMAL;
4689 			p->static_prio = NICE_TO_PRIO(0);
4690 			p->rt_priority = 0;
4691 		} else if (PRIO_TO_NICE(p->static_prio) < 0)
4692 			p->static_prio = NICE_TO_PRIO(0);
4693 
4694 		p->prio = p->normal_prio = p->static_prio;
4695 		set_load_weight(p, false);
4696 		p->se.custom_slice = 0;
4697 		p->se.slice = sysctl_sched_base_slice;
4698 
4699 		/*
4700 		 * We don't need the reset flag anymore after the fork. It has
4701 		 * fulfilled its duty:
4702 		 */
4703 		p->sched_reset_on_fork = 0;
4704 	}
4705 
4706 	if (dl_prio(p->prio))
4707 		return -EAGAIN;
4708 
4709 	scx_pre_fork(p);
4710 
4711 	if (rt_prio(p->prio)) {
4712 		p->sched_class = &rt_sched_class;
4713 #ifdef CONFIG_SCHED_CLASS_EXT
4714 	} else if (task_should_scx(p)) {
4715 		p->sched_class = &ext_sched_class;
4716 #endif
4717 	} else {
4718 		p->sched_class = &fair_sched_class;
4719 	}
4720 
4721 	init_entity_runnable_average(&p->se);
4722 
4723 
4724 #ifdef CONFIG_SCHED_INFO
4725 	if (likely(sched_info_on()))
4726 		memset(&p->sched_info, 0, sizeof(p->sched_info));
4727 #endif
4728 #if defined(CONFIG_SMP)
4729 	p->on_cpu = 0;
4730 #endif
4731 	init_task_preempt_count(p);
4732 #ifdef CONFIG_SMP
4733 	plist_node_init(&p->pushable_tasks, MAX_PRIO);
4734 	RB_CLEAR_NODE(&p->pushable_dl_tasks);
4735 #endif
4736 	return 0;
4737 }
4738 
4739 int sched_cgroup_fork(struct task_struct *p, struct kernel_clone_args *kargs)
4740 {
4741 	unsigned long flags;
4742 
4743 	/*
4744 	 * Because we're not yet on the pid-hash, p->pi_lock isn't strictly
4745 	 * required yet, but lockdep gets upset if rules are violated.
4746 	 */
4747 	raw_spin_lock_irqsave(&p->pi_lock, flags);
4748 #ifdef CONFIG_CGROUP_SCHED
4749 	if (1) {
4750 		struct task_group *tg;
4751 		tg = container_of(kargs->cset->subsys[cpu_cgrp_id],
4752 				  struct task_group, css);
4753 		tg = autogroup_task_group(p, tg);
4754 		p->sched_task_group = tg;
4755 	}
4756 #endif
4757 	rseq_migrate(p);
4758 	/*
4759 	 * We're setting the CPU for the first time, we don't migrate,
4760 	 * so use __set_task_cpu().
4761 	 */
4762 	__set_task_cpu(p, smp_processor_id());
4763 	if (p->sched_class->task_fork)
4764 		p->sched_class->task_fork(p);
4765 	raw_spin_unlock_irqrestore(&p->pi_lock, flags);
4766 
4767 	return scx_fork(p);
4768 }
4769 
4770 void sched_cancel_fork(struct task_struct *p)
4771 {
4772 	scx_cancel_fork(p);
4773 }
4774 
4775 void sched_post_fork(struct task_struct *p)
4776 {
4777 	uclamp_post_fork(p);
4778 	scx_post_fork(p);
4779 }
4780 
4781 unsigned long to_ratio(u64 period, u64 runtime)
4782 {
4783 	if (runtime == RUNTIME_INF)
4784 		return BW_UNIT;
4785 
4786 	/*
4787 	 * Doing this here saves a lot of checks in all
4788 	 * the calling paths, and returning zero seems
4789 	 * safe for them anyway.
4790 	 */
4791 	if (period == 0)
4792 		return 0;
4793 
4794 	return div64_u64(runtime << BW_SHIFT, period);
4795 }
4796 
4797 /*
4798  * wake_up_new_task - wake up a newly created task for the first time.
4799  *
4800  * This function will do some initial scheduler statistics housekeeping
4801  * that must be done for every newly created context, then puts the task
4802  * on the runqueue and wakes it.
4803  */
4804 void wake_up_new_task(struct task_struct *p)
4805 {
4806 	struct rq_flags rf;
4807 	struct rq *rq;
4808 	int wake_flags = WF_FORK;
4809 
4810 	raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
4811 	WRITE_ONCE(p->__state, TASK_RUNNING);
4812 #ifdef CONFIG_SMP
4813 	/*
4814 	 * Fork balancing, do it here and not earlier because:
4815 	 *  - cpus_ptr can change in the fork path
4816 	 *  - any previously selected CPU might disappear through hotplug
4817 	 *
4818 	 * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq,
4819 	 * as we're not fully set-up yet.
4820 	 */
4821 	p->recent_used_cpu = task_cpu(p);
4822 	rseq_migrate(p);
4823 	__set_task_cpu(p, select_task_rq(p, task_cpu(p), &wake_flags));
4824 #endif
4825 	rq = __task_rq_lock(p, &rf);
4826 	update_rq_clock(rq);
4827 	post_init_entity_util_avg(p);
4828 
4829 	activate_task(rq, p, ENQUEUE_NOCLOCK | ENQUEUE_INITIAL);
4830 	trace_sched_wakeup_new(p);
4831 	wakeup_preempt(rq, p, wake_flags);
4832 #ifdef CONFIG_SMP
4833 	if (p->sched_class->task_woken) {
4834 		/*
4835 		 * Nothing relies on rq->lock after this, so it's fine to
4836 		 * drop it.
4837 		 */
4838 		rq_unpin_lock(rq, &rf);
4839 		p->sched_class->task_woken(rq, p);
4840 		rq_repin_lock(rq, &rf);
4841 	}
4842 #endif
4843 	task_rq_unlock(rq, p, &rf);
4844 }
4845 
4846 #ifdef CONFIG_PREEMPT_NOTIFIERS
4847 
4848 static DEFINE_STATIC_KEY_FALSE(preempt_notifier_key);
4849 
4850 void preempt_notifier_inc(void)
4851 {
4852 	static_branch_inc(&preempt_notifier_key);
4853 }
4854 EXPORT_SYMBOL_GPL(preempt_notifier_inc);
4855 
4856 void preempt_notifier_dec(void)
4857 {
4858 	static_branch_dec(&preempt_notifier_key);
4859 }
4860 EXPORT_SYMBOL_GPL(preempt_notifier_dec);
4861 
4862 /**
4863  * preempt_notifier_register - tell me when current is being preempted & rescheduled
4864  * @notifier: notifier struct to register
4865  */
4866 void preempt_notifier_register(struct preempt_notifier *notifier)
4867 {
4868 	if (!static_branch_unlikely(&preempt_notifier_key))
4869 		WARN(1, "registering preempt_notifier while notifiers disabled\n");
4870 
4871 	hlist_add_head(&notifier->link, &current->preempt_notifiers);
4872 }
4873 EXPORT_SYMBOL_GPL(preempt_notifier_register);
4874 
4875 /**
4876  * preempt_notifier_unregister - no longer interested in preemption notifications
4877  * @notifier: notifier struct to unregister
4878  *
4879  * This is *not* safe to call from within a preemption notifier.
4880  */
4881 void preempt_notifier_unregister(struct preempt_notifier *notifier)
4882 {
4883 	hlist_del(&notifier->link);
4884 }
4885 EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
4886 
4887 static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
4888 {
4889 	struct preempt_notifier *notifier;
4890 
4891 	hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
4892 		notifier->ops->sched_in(notifier, raw_smp_processor_id());
4893 }
4894 
4895 static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
4896 {
4897 	if (static_branch_unlikely(&preempt_notifier_key))
4898 		__fire_sched_in_preempt_notifiers(curr);
4899 }
4900 
4901 static void
4902 __fire_sched_out_preempt_notifiers(struct task_struct *curr,
4903 				   struct task_struct *next)
4904 {
4905 	struct preempt_notifier *notifier;
4906 
4907 	hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
4908 		notifier->ops->sched_out(notifier, next);
4909 }
4910 
4911 static __always_inline void
4912 fire_sched_out_preempt_notifiers(struct task_struct *curr,
4913 				 struct task_struct *next)
4914 {
4915 	if (static_branch_unlikely(&preempt_notifier_key))
4916 		__fire_sched_out_preempt_notifiers(curr, next);
4917 }
4918 
4919 #else /* !CONFIG_PREEMPT_NOTIFIERS */
4920 
4921 static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
4922 {
4923 }
4924 
4925 static inline void
4926 fire_sched_out_preempt_notifiers(struct task_struct *curr,
4927 				 struct task_struct *next)
4928 {
4929 }
4930 
4931 #endif /* CONFIG_PREEMPT_NOTIFIERS */
4932 
4933 static inline void prepare_task(struct task_struct *next)
4934 {
4935 #ifdef CONFIG_SMP
4936 	/*
4937 	 * Claim the task as running, we do this before switching to it
4938 	 * such that any running task will have this set.
4939 	 *
4940 	 * See the smp_load_acquire(&p->on_cpu) case in ttwu() and
4941 	 * its ordering comment.
4942 	 */
4943 	WRITE_ONCE(next->on_cpu, 1);
4944 #endif
4945 }
4946 
4947 static inline void finish_task(struct task_struct *prev)
4948 {
4949 #ifdef CONFIG_SMP
4950 	/*
4951 	 * This must be the very last reference to @prev from this CPU. After
4952 	 * p->on_cpu is cleared, the task can be moved to a different CPU. We
4953 	 * must ensure this doesn't happen until the switch is completely
4954 	 * finished.
4955 	 *
4956 	 * In particular, the load of prev->state in finish_task_switch() must
4957 	 * happen before this.
4958 	 *
4959 	 * Pairs with the smp_cond_load_acquire() in try_to_wake_up().
4960 	 */
4961 	smp_store_release(&prev->on_cpu, 0);
4962 #endif
4963 }
4964 
4965 #ifdef CONFIG_SMP
4966 
4967 static void do_balance_callbacks(struct rq *rq, struct balance_callback *head)
4968 {
4969 	void (*func)(struct rq *rq);
4970 	struct balance_callback *next;
4971 
4972 	lockdep_assert_rq_held(rq);
4973 
4974 	while (head) {
4975 		func = (void (*)(struct rq *))head->func;
4976 		next = head->next;
4977 		head->next = NULL;
4978 		head = next;
4979 
4980 		func(rq);
4981 	}
4982 }
4983 
4984 static void balance_push(struct rq *rq);
4985 
4986 /*
4987  * balance_push_callback is a right abuse of the callback interface and plays
4988  * by significantly different rules.
4989  *
4990  * Where the normal balance_callback's purpose is to be ran in the same context
4991  * that queued it (only later, when it's safe to drop rq->lock again),
4992  * balance_push_callback is specifically targeted at __schedule().
4993  *
4994  * This abuse is tolerated because it places all the unlikely/odd cases behind
4995  * a single test, namely: rq->balance_callback == NULL.
4996  */
4997 struct balance_callback balance_push_callback = {
4998 	.next = NULL,
4999 	.func = balance_push,
5000 };
5001 
5002 static inline struct balance_callback *
5003 __splice_balance_callbacks(struct rq *rq, bool split)
5004 {
5005 	struct balance_callback *head = rq->balance_callback;
5006 
5007 	if (likely(!head))
5008 		return NULL;
5009 
5010 	lockdep_assert_rq_held(rq);
5011 	/*
5012 	 * Must not take balance_push_callback off the list when
5013 	 * splice_balance_callbacks() and balance_callbacks() are not
5014 	 * in the same rq->lock section.
5015 	 *
5016 	 * In that case it would be possible for __schedule() to interleave
5017 	 * and observe the list empty.
5018 	 */
5019 	if (split && head == &balance_push_callback)
5020 		head = NULL;
5021 	else
5022 		rq->balance_callback = NULL;
5023 
5024 	return head;
5025 }
5026 
5027 struct balance_callback *splice_balance_callbacks(struct rq *rq)
5028 {
5029 	return __splice_balance_callbacks(rq, true);
5030 }
5031 
5032 static void __balance_callbacks(struct rq *rq)
5033 {
5034 	do_balance_callbacks(rq, __splice_balance_callbacks(rq, false));
5035 }
5036 
5037 void balance_callbacks(struct rq *rq, struct balance_callback *head)
5038 {
5039 	unsigned long flags;
5040 
5041 	if (unlikely(head)) {
5042 		raw_spin_rq_lock_irqsave(rq, flags);
5043 		do_balance_callbacks(rq, head);
5044 		raw_spin_rq_unlock_irqrestore(rq, flags);
5045 	}
5046 }
5047 
5048 #else
5049 
5050 static inline void __balance_callbacks(struct rq *rq)
5051 {
5052 }
5053 
5054 #endif
5055 
5056 static inline void
5057 prepare_lock_switch(struct rq *rq, struct task_struct *next, struct rq_flags *rf)
5058 {
5059 	/*
5060 	 * Since the runqueue lock will be released by the next
5061 	 * task (which is an invalid locking op but in the case
5062 	 * of the scheduler it's an obvious special-case), so we
5063 	 * do an early lockdep release here:
5064 	 */
5065 	rq_unpin_lock(rq, rf);
5066 	spin_release(&__rq_lockp(rq)->dep_map, _THIS_IP_);
5067 #ifdef CONFIG_DEBUG_SPINLOCK
5068 	/* this is a valid case when another task releases the spinlock */
5069 	rq_lockp(rq)->owner = next;
5070 #endif
5071 }
5072 
5073 static inline void finish_lock_switch(struct rq *rq)
5074 {
5075 	/*
5076 	 * If we are tracking spinlock dependencies then we have to
5077 	 * fix up the runqueue lock - which gets 'carried over' from
5078 	 * prev into current:
5079 	 */
5080 	spin_acquire(&__rq_lockp(rq)->dep_map, 0, 0, _THIS_IP_);
5081 	__balance_callbacks(rq);
5082 	raw_spin_rq_unlock_irq(rq);
5083 }
5084 
5085 /*
5086  * NOP if the arch has not defined these:
5087  */
5088 
5089 #ifndef prepare_arch_switch
5090 # define prepare_arch_switch(next)	do { } while (0)
5091 #endif
5092 
5093 #ifndef finish_arch_post_lock_switch
5094 # define finish_arch_post_lock_switch()	do { } while (0)
5095 #endif
5096 
5097 static inline void kmap_local_sched_out(void)
5098 {
5099 #ifdef CONFIG_KMAP_LOCAL
5100 	if (unlikely(current->kmap_ctrl.idx))
5101 		__kmap_local_sched_out();
5102 #endif
5103 }
5104 
5105 static inline void kmap_local_sched_in(void)
5106 {
5107 #ifdef CONFIG_KMAP_LOCAL
5108 	if (unlikely(current->kmap_ctrl.idx))
5109 		__kmap_local_sched_in();
5110 #endif
5111 }
5112 
5113 /**
5114  * prepare_task_switch - prepare to switch tasks
5115  * @rq: the runqueue preparing to switch
5116  * @prev: the current task that is being switched out
5117  * @next: the task we are going to switch to.
5118  *
5119  * This is called with the rq lock held and interrupts off. It must
5120  * be paired with a subsequent finish_task_switch after the context
5121  * switch.
5122  *
5123  * prepare_task_switch sets up locking and calls architecture specific
5124  * hooks.
5125  */
5126 static inline void
5127 prepare_task_switch(struct rq *rq, struct task_struct *prev,
5128 		    struct task_struct *next)
5129 {
5130 	kcov_prepare_switch(prev);
5131 	sched_info_switch(rq, prev, next);
5132 	perf_event_task_sched_out(prev, next);
5133 	rseq_preempt(prev);
5134 	fire_sched_out_preempt_notifiers(prev, next);
5135 	kmap_local_sched_out();
5136 	prepare_task(next);
5137 	prepare_arch_switch(next);
5138 }
5139 
5140 /**
5141  * finish_task_switch - clean up after a task-switch
5142  * @prev: the thread we just switched away from.
5143  *
5144  * finish_task_switch must be called after the context switch, paired
5145  * with a prepare_task_switch call before the context switch.
5146  * finish_task_switch will reconcile locking set up by prepare_task_switch,
5147  * and do any other architecture-specific cleanup actions.
5148  *
5149  * Note that we may have delayed dropping an mm in context_switch(). If
5150  * so, we finish that here outside of the runqueue lock. (Doing it
5151  * with the lock held can cause deadlocks; see schedule() for
5152  * details.)
5153  *
5154  * The context switch have flipped the stack from under us and restored the
5155  * local variables which were saved when this task called schedule() in the
5156  * past. 'prev == current' is still correct but we need to recalculate this_rq
5157  * because prev may have moved to another CPU.
5158  */
5159 static struct rq *finish_task_switch(struct task_struct *prev)
5160 	__releases(rq->lock)
5161 {
5162 	struct rq *rq = this_rq();
5163 	struct mm_struct *mm = rq->prev_mm;
5164 	unsigned int prev_state;
5165 
5166 	/*
5167 	 * The previous task will have left us with a preempt_count of 2
5168 	 * because it left us after:
5169 	 *
5170 	 *	schedule()
5171 	 *	  preempt_disable();			// 1
5172 	 *	  __schedule()
5173 	 *	    raw_spin_lock_irq(&rq->lock)	// 2
5174 	 *
5175 	 * Also, see FORK_PREEMPT_COUNT.
5176 	 */
5177 	if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
5178 		      "corrupted preempt_count: %s/%d/0x%x\n",
5179 		      current->comm, current->pid, preempt_count()))
5180 		preempt_count_set(FORK_PREEMPT_COUNT);
5181 
5182 	rq->prev_mm = NULL;
5183 
5184 	/*
5185 	 * A task struct has one reference for the use as "current".
5186 	 * If a task dies, then it sets TASK_DEAD in tsk->state and calls
5187 	 * schedule one last time. The schedule call will never return, and
5188 	 * the scheduled task must drop that reference.
5189 	 *
5190 	 * We must observe prev->state before clearing prev->on_cpu (in
5191 	 * finish_task), otherwise a concurrent wakeup can get prev
5192 	 * running on another CPU and we could rave with its RUNNING -> DEAD
5193 	 * transition, resulting in a double drop.
5194 	 */
5195 	prev_state = READ_ONCE(prev->__state);
5196 	vtime_task_switch(prev);
5197 	perf_event_task_sched_in(prev, current);
5198 	finish_task(prev);
5199 	tick_nohz_task_switch();
5200 	finish_lock_switch(rq);
5201 	finish_arch_post_lock_switch();
5202 	kcov_finish_switch(current);
5203 	/*
5204 	 * kmap_local_sched_out() is invoked with rq::lock held and
5205 	 * interrupts disabled. There is no requirement for that, but the
5206 	 * sched out code does not have an interrupt enabled section.
5207 	 * Restoring the maps on sched in does not require interrupts being
5208 	 * disabled either.
5209 	 */
5210 	kmap_local_sched_in();
5211 
5212 	fire_sched_in_preempt_notifiers(current);
5213 	/*
5214 	 * When switching through a kernel thread, the loop in
5215 	 * membarrier_{private,global}_expedited() may have observed that
5216 	 * kernel thread and not issued an IPI. It is therefore possible to
5217 	 * schedule between user->kernel->user threads without passing though
5218 	 * switch_mm(). Membarrier requires a barrier after storing to
5219 	 * rq->curr, before returning to userspace, so provide them here:
5220 	 *
5221 	 * - a full memory barrier for {PRIVATE,GLOBAL}_EXPEDITED, implicitly
5222 	 *   provided by mmdrop_lazy_tlb(),
5223 	 * - a sync_core for SYNC_CORE.
5224 	 */
5225 	if (mm) {
5226 		membarrier_mm_sync_core_before_usermode(mm);
5227 		mmdrop_lazy_tlb_sched(mm);
5228 	}
5229 
5230 	if (unlikely(prev_state == TASK_DEAD)) {
5231 		if (prev->sched_class->task_dead)
5232 			prev->sched_class->task_dead(prev);
5233 
5234 		/* Task is done with its stack. */
5235 		put_task_stack(prev);
5236 
5237 		put_task_struct_rcu_user(prev);
5238 	}
5239 
5240 	return rq;
5241 }
5242 
5243 /**
5244  * schedule_tail - first thing a freshly forked thread must call.
5245  * @prev: the thread we just switched away from.
5246  */
5247 asmlinkage __visible void schedule_tail(struct task_struct *prev)
5248 	__releases(rq->lock)
5249 {
5250 	/*
5251 	 * New tasks start with FORK_PREEMPT_COUNT, see there and
5252 	 * finish_task_switch() for details.
5253 	 *
5254 	 * finish_task_switch() will drop rq->lock() and lower preempt_count
5255 	 * and the preempt_enable() will end up enabling preemption (on
5256 	 * PREEMPT_COUNT kernels).
5257 	 */
5258 
5259 	finish_task_switch(prev);
5260 	preempt_enable();
5261 
5262 	if (current->set_child_tid)
5263 		put_user(task_pid_vnr(current), current->set_child_tid);
5264 
5265 	calculate_sigpending();
5266 }
5267 
5268 /*
5269  * context_switch - switch to the new MM and the new thread's register state.
5270  */
5271 static __always_inline struct rq *
5272 context_switch(struct rq *rq, struct task_struct *prev,
5273 	       struct task_struct *next, struct rq_flags *rf)
5274 {
5275 	prepare_task_switch(rq, prev, next);
5276 
5277 	/*
5278 	 * For paravirt, this is coupled with an exit in switch_to to
5279 	 * combine the page table reload and the switch backend into
5280 	 * one hypercall.
5281 	 */
5282 	arch_start_context_switch(prev);
5283 
5284 	/*
5285 	 * kernel -> kernel   lazy + transfer active
5286 	 *   user -> kernel   lazy + mmgrab_lazy_tlb() active
5287 	 *
5288 	 * kernel ->   user   switch + mmdrop_lazy_tlb() active
5289 	 *   user ->   user   switch
5290 	 *
5291 	 * switch_mm_cid() needs to be updated if the barriers provided
5292 	 * by context_switch() are modified.
5293 	 */
5294 	if (!next->mm) {                                // to kernel
5295 		enter_lazy_tlb(prev->active_mm, next);
5296 
5297 		next->active_mm = prev->active_mm;
5298 		if (prev->mm)                           // from user
5299 			mmgrab_lazy_tlb(prev->active_mm);
5300 		else
5301 			prev->active_mm = NULL;
5302 	} else {                                        // to user
5303 		membarrier_switch_mm(rq, prev->active_mm, next->mm);
5304 		/*
5305 		 * sys_membarrier() requires an smp_mb() between setting
5306 		 * rq->curr / membarrier_switch_mm() and returning to userspace.
5307 		 *
5308 		 * The below provides this either through switch_mm(), or in
5309 		 * case 'prev->active_mm == next->mm' through
5310 		 * finish_task_switch()'s mmdrop().
5311 		 */
5312 		switch_mm_irqs_off(prev->active_mm, next->mm, next);
5313 		lru_gen_use_mm(next->mm);
5314 
5315 		if (!prev->mm) {                        // from kernel
5316 			/* will mmdrop_lazy_tlb() in finish_task_switch(). */
5317 			rq->prev_mm = prev->active_mm;
5318 			prev->active_mm = NULL;
5319 		}
5320 	}
5321 
5322 	/* switch_mm_cid() requires the memory barriers above. */
5323 	switch_mm_cid(rq, prev, next);
5324 
5325 	prepare_lock_switch(rq, next, rf);
5326 
5327 	/* Here we just switch the register state and the stack. */
5328 	switch_to(prev, next, prev);
5329 	barrier();
5330 
5331 	return finish_task_switch(prev);
5332 }
5333 
5334 /*
5335  * nr_running and nr_context_switches:
5336  *
5337  * externally visible scheduler statistics: current number of runnable
5338  * threads, total number of context switches performed since bootup.
5339  */
5340 unsigned int nr_running(void)
5341 {
5342 	unsigned int i, sum = 0;
5343 
5344 	for_each_online_cpu(i)
5345 		sum += cpu_rq(i)->nr_running;
5346 
5347 	return sum;
5348 }
5349 
5350 /*
5351  * Check if only the current task is running on the CPU.
5352  *
5353  * Caution: this function does not check that the caller has disabled
5354  * preemption, thus the result might have a time-of-check-to-time-of-use
5355  * race.  The caller is responsible to use it correctly, for example:
5356  *
5357  * - from a non-preemptible section (of course)
5358  *
5359  * - from a thread that is bound to a single CPU
5360  *
5361  * - in a loop with very short iterations (e.g. a polling loop)
5362  */
5363 bool single_task_running(void)
5364 {
5365 	return raw_rq()->nr_running == 1;
5366 }
5367 EXPORT_SYMBOL(single_task_running);
5368 
5369 unsigned long long nr_context_switches_cpu(int cpu)
5370 {
5371 	return cpu_rq(cpu)->nr_switches;
5372 }
5373 
5374 unsigned long long nr_context_switches(void)
5375 {
5376 	int i;
5377 	unsigned long long sum = 0;
5378 
5379 	for_each_possible_cpu(i)
5380 		sum += cpu_rq(i)->nr_switches;
5381 
5382 	return sum;
5383 }
5384 
5385 /*
5386  * Consumers of these two interfaces, like for example the cpuidle menu
5387  * governor, are using nonsensical data. Preferring shallow idle state selection
5388  * for a CPU that has IO-wait which might not even end up running the task when
5389  * it does become runnable.
5390  */
5391 
5392 unsigned int nr_iowait_cpu(int cpu)
5393 {
5394 	return atomic_read(&cpu_rq(cpu)->nr_iowait);
5395 }
5396 
5397 /*
5398  * IO-wait accounting, and how it's mostly bollocks (on SMP).
5399  *
5400  * The idea behind IO-wait account is to account the idle time that we could
5401  * have spend running if it were not for IO. That is, if we were to improve the
5402  * storage performance, we'd have a proportional reduction in IO-wait time.
5403  *
5404  * This all works nicely on UP, where, when a task blocks on IO, we account
5405  * idle time as IO-wait, because if the storage were faster, it could've been
5406  * running and we'd not be idle.
5407  *
5408  * This has been extended to SMP, by doing the same for each CPU. This however
5409  * is broken.
5410  *
5411  * Imagine for instance the case where two tasks block on one CPU, only the one
5412  * CPU will have IO-wait accounted, while the other has regular idle. Even
5413  * though, if the storage were faster, both could've ran at the same time,
5414  * utilising both CPUs.
5415  *
5416  * This means, that when looking globally, the current IO-wait accounting on
5417  * SMP is a lower bound, by reason of under accounting.
5418  *
5419  * Worse, since the numbers are provided per CPU, they are sometimes
5420  * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly
5421  * associated with any one particular CPU, it can wake to another CPU than it
5422  * blocked on. This means the per CPU IO-wait number is meaningless.
5423  *
5424  * Task CPU affinities can make all that even more 'interesting'.
5425  */
5426 
5427 unsigned int nr_iowait(void)
5428 {
5429 	unsigned int i, sum = 0;
5430 
5431 	for_each_possible_cpu(i)
5432 		sum += nr_iowait_cpu(i);
5433 
5434 	return sum;
5435 }
5436 
5437 #ifdef CONFIG_SMP
5438 
5439 /*
5440  * sched_exec - execve() is a valuable balancing opportunity, because at
5441  * this point the task has the smallest effective memory and cache footprint.
5442  */
5443 void sched_exec(void)
5444 {
5445 	struct task_struct *p = current;
5446 	struct migration_arg arg;
5447 	int dest_cpu;
5448 
5449 	scoped_guard (raw_spinlock_irqsave, &p->pi_lock) {
5450 		dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), WF_EXEC);
5451 		if (dest_cpu == smp_processor_id())
5452 			return;
5453 
5454 		if (unlikely(!cpu_active(dest_cpu)))
5455 			return;
5456 
5457 		arg = (struct migration_arg){ p, dest_cpu };
5458 	}
5459 	stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
5460 }
5461 
5462 #endif
5463 
5464 DEFINE_PER_CPU(struct kernel_stat, kstat);
5465 DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
5466 
5467 EXPORT_PER_CPU_SYMBOL(kstat);
5468 EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
5469 
5470 /*
5471  * The function fair_sched_class.update_curr accesses the struct curr
5472  * and its field curr->exec_start; when called from task_sched_runtime(),
5473  * we observe a high rate of cache misses in practice.
5474  * Prefetching this data results in improved performance.
5475  */
5476 static inline void prefetch_curr_exec_start(struct task_struct *p)
5477 {
5478 #ifdef CONFIG_FAIR_GROUP_SCHED
5479 	struct sched_entity *curr = p->se.cfs_rq->curr;
5480 #else
5481 	struct sched_entity *curr = task_rq(p)->cfs.curr;
5482 #endif
5483 	prefetch(curr);
5484 	prefetch(&curr->exec_start);
5485 }
5486 
5487 /*
5488  * Return accounted runtime for the task.
5489  * In case the task is currently running, return the runtime plus current's
5490  * pending runtime that have not been accounted yet.
5491  */
5492 unsigned long long task_sched_runtime(struct task_struct *p)
5493 {
5494 	struct rq_flags rf;
5495 	struct rq *rq;
5496 	u64 ns;
5497 
5498 #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
5499 	/*
5500 	 * 64-bit doesn't need locks to atomically read a 64-bit value.
5501 	 * So we have a optimization chance when the task's delta_exec is 0.
5502 	 * Reading ->on_cpu is racy, but this is OK.
5503 	 *
5504 	 * If we race with it leaving CPU, we'll take a lock. So we're correct.
5505 	 * If we race with it entering CPU, unaccounted time is 0. This is
5506 	 * indistinguishable from the read occurring a few cycles earlier.
5507 	 * If we see ->on_cpu without ->on_rq, the task is leaving, and has
5508 	 * been accounted, so we're correct here as well.
5509 	 */
5510 	if (!p->on_cpu || !task_on_rq_queued(p))
5511 		return p->se.sum_exec_runtime;
5512 #endif
5513 
5514 	rq = task_rq_lock(p, &rf);
5515 	/*
5516 	 * Must be ->curr _and_ ->on_rq.  If dequeued, we would
5517 	 * project cycles that may never be accounted to this
5518 	 * thread, breaking clock_gettime().
5519 	 */
5520 	if (task_current(rq, p) && task_on_rq_queued(p)) {
5521 		prefetch_curr_exec_start(p);
5522 		update_rq_clock(rq);
5523 		p->sched_class->update_curr(rq);
5524 	}
5525 	ns = p->se.sum_exec_runtime;
5526 	task_rq_unlock(rq, p, &rf);
5527 
5528 	return ns;
5529 }
5530 
5531 #ifdef CONFIG_SCHED_DEBUG
5532 static u64 cpu_resched_latency(struct rq *rq)
5533 {
5534 	int latency_warn_ms = READ_ONCE(sysctl_resched_latency_warn_ms);
5535 	u64 resched_latency, now = rq_clock(rq);
5536 	static bool warned_once;
5537 
5538 	if (sysctl_resched_latency_warn_once && warned_once)
5539 		return 0;
5540 
5541 	if (!need_resched() || !latency_warn_ms)
5542 		return 0;
5543 
5544 	if (system_state == SYSTEM_BOOTING)
5545 		return 0;
5546 
5547 	if (!rq->last_seen_need_resched_ns) {
5548 		rq->last_seen_need_resched_ns = now;
5549 		rq->ticks_without_resched = 0;
5550 		return 0;
5551 	}
5552 
5553 	rq->ticks_without_resched++;
5554 	resched_latency = now - rq->last_seen_need_resched_ns;
5555 	if (resched_latency <= latency_warn_ms * NSEC_PER_MSEC)
5556 		return 0;
5557 
5558 	warned_once = true;
5559 
5560 	return resched_latency;
5561 }
5562 
5563 static int __init setup_resched_latency_warn_ms(char *str)
5564 {
5565 	long val;
5566 
5567 	if ((kstrtol(str, 0, &val))) {
5568 		pr_warn("Unable to set resched_latency_warn_ms\n");
5569 		return 1;
5570 	}
5571 
5572 	sysctl_resched_latency_warn_ms = val;
5573 	return 1;
5574 }
5575 __setup("resched_latency_warn_ms=", setup_resched_latency_warn_ms);
5576 #else
5577 static inline u64 cpu_resched_latency(struct rq *rq) { return 0; }
5578 #endif /* CONFIG_SCHED_DEBUG */
5579 
5580 /*
5581  * This function gets called by the timer code, with HZ frequency.
5582  * We call it with interrupts disabled.
5583  */
5584 void sched_tick(void)
5585 {
5586 	int cpu = smp_processor_id();
5587 	struct rq *rq = cpu_rq(cpu);
5588 	struct task_struct *curr;
5589 	struct rq_flags rf;
5590 	unsigned long hw_pressure;
5591 	u64 resched_latency;
5592 
5593 	if (housekeeping_cpu(cpu, HK_TYPE_TICK))
5594 		arch_scale_freq_tick();
5595 
5596 	sched_clock_tick();
5597 
5598 	rq_lock(rq, &rf);
5599 
5600 	curr = rq->curr;
5601 	psi_account_irqtime(rq, curr, NULL);
5602 
5603 	update_rq_clock(rq);
5604 	hw_pressure = arch_scale_hw_pressure(cpu_of(rq));
5605 	update_hw_load_avg(rq_clock_task(rq), rq, hw_pressure);
5606 	curr->sched_class->task_tick(rq, curr, 0);
5607 	if (sched_feat(LATENCY_WARN))
5608 		resched_latency = cpu_resched_latency(rq);
5609 	calc_global_load_tick(rq);
5610 	sched_core_tick(rq);
5611 	task_tick_mm_cid(rq, curr);
5612 	scx_tick(rq);
5613 
5614 	rq_unlock(rq, &rf);
5615 
5616 	if (sched_feat(LATENCY_WARN) && resched_latency)
5617 		resched_latency_warn(cpu, resched_latency);
5618 
5619 	perf_event_task_tick();
5620 
5621 	if (curr->flags & PF_WQ_WORKER)
5622 		wq_worker_tick(curr);
5623 
5624 #ifdef CONFIG_SMP
5625 	if (!scx_switched_all()) {
5626 		rq->idle_balance = idle_cpu(cpu);
5627 		sched_balance_trigger(rq);
5628 	}
5629 #endif
5630 }
5631 
5632 #ifdef CONFIG_NO_HZ_FULL
5633 
5634 struct tick_work {
5635 	int			cpu;
5636 	atomic_t		state;
5637 	struct delayed_work	work;
5638 };
5639 /* Values for ->state, see diagram below. */
5640 #define TICK_SCHED_REMOTE_OFFLINE	0
5641 #define TICK_SCHED_REMOTE_OFFLINING	1
5642 #define TICK_SCHED_REMOTE_RUNNING	2
5643 
5644 /*
5645  * State diagram for ->state:
5646  *
5647  *
5648  *          TICK_SCHED_REMOTE_OFFLINE
5649  *                    |   ^
5650  *                    |   |
5651  *                    |   | sched_tick_remote()
5652  *                    |   |
5653  *                    |   |
5654  *                    +--TICK_SCHED_REMOTE_OFFLINING
5655  *                    |   ^
5656  *                    |   |
5657  * sched_tick_start() |   | sched_tick_stop()
5658  *                    |   |
5659  *                    V   |
5660  *          TICK_SCHED_REMOTE_RUNNING
5661  *
5662  *
5663  * Other transitions get WARN_ON_ONCE(), except that sched_tick_remote()
5664  * and sched_tick_start() are happy to leave the state in RUNNING.
5665  */
5666 
5667 static struct tick_work __percpu *tick_work_cpu;
5668 
5669 static void sched_tick_remote(struct work_struct *work)
5670 {
5671 	struct delayed_work *dwork = to_delayed_work(work);
5672 	struct tick_work *twork = container_of(dwork, struct tick_work, work);
5673 	int cpu = twork->cpu;
5674 	struct rq *rq = cpu_rq(cpu);
5675 	int os;
5676 
5677 	/*
5678 	 * Handle the tick only if it appears the remote CPU is running in full
5679 	 * dynticks mode. The check is racy by nature, but missing a tick or
5680 	 * having one too much is no big deal because the scheduler tick updates
5681 	 * statistics and checks timeslices in a time-independent way, regardless
5682 	 * of when exactly it is running.
5683 	 */
5684 	if (tick_nohz_tick_stopped_cpu(cpu)) {
5685 		guard(rq_lock_irq)(rq);
5686 		struct task_struct *curr = rq->curr;
5687 
5688 		if (cpu_online(cpu)) {
5689 			update_rq_clock(rq);
5690 
5691 			if (!is_idle_task(curr)) {
5692 				/*
5693 				 * Make sure the next tick runs within a
5694 				 * reasonable amount of time.
5695 				 */
5696 				u64 delta = rq_clock_task(rq) - curr->se.exec_start;
5697 				WARN_ON_ONCE(delta > (u64)NSEC_PER_SEC * 3);
5698 			}
5699 			curr->sched_class->task_tick(rq, curr, 0);
5700 
5701 			calc_load_nohz_remote(rq);
5702 		}
5703 	}
5704 
5705 	/*
5706 	 * Run the remote tick once per second (1Hz). This arbitrary
5707 	 * frequency is large enough to avoid overload but short enough
5708 	 * to keep scheduler internal stats reasonably up to date.  But
5709 	 * first update state to reflect hotplug activity if required.
5710 	 */
5711 	os = atomic_fetch_add_unless(&twork->state, -1, TICK_SCHED_REMOTE_RUNNING);
5712 	WARN_ON_ONCE(os == TICK_SCHED_REMOTE_OFFLINE);
5713 	if (os == TICK_SCHED_REMOTE_RUNNING)
5714 		queue_delayed_work(system_unbound_wq, dwork, HZ);
5715 }
5716 
5717 static void sched_tick_start(int cpu)
5718 {
5719 	int os;
5720 	struct tick_work *twork;
5721 
5722 	if (housekeeping_cpu(cpu, HK_TYPE_TICK))
5723 		return;
5724 
5725 	WARN_ON_ONCE(!tick_work_cpu);
5726 
5727 	twork = per_cpu_ptr(tick_work_cpu, cpu);
5728 	os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_RUNNING);
5729 	WARN_ON_ONCE(os == TICK_SCHED_REMOTE_RUNNING);
5730 	if (os == TICK_SCHED_REMOTE_OFFLINE) {
5731 		twork->cpu = cpu;
5732 		INIT_DELAYED_WORK(&twork->work, sched_tick_remote);
5733 		queue_delayed_work(system_unbound_wq, &twork->work, HZ);
5734 	}
5735 }
5736 
5737 #ifdef CONFIG_HOTPLUG_CPU
5738 static void sched_tick_stop(int cpu)
5739 {
5740 	struct tick_work *twork;
5741 	int os;
5742 
5743 	if (housekeeping_cpu(cpu, HK_TYPE_TICK))
5744 		return;
5745 
5746 	WARN_ON_ONCE(!tick_work_cpu);
5747 
5748 	twork = per_cpu_ptr(tick_work_cpu, cpu);
5749 	/* There cannot be competing actions, but don't rely on stop-machine. */
5750 	os = atomic_xchg(&twork->state, TICK_SCHED_REMOTE_OFFLINING);
5751 	WARN_ON_ONCE(os != TICK_SCHED_REMOTE_RUNNING);
5752 	/* Don't cancel, as this would mess up the state machine. */
5753 }
5754 #endif /* CONFIG_HOTPLUG_CPU */
5755 
5756 int __init sched_tick_offload_init(void)
5757 {
5758 	tick_work_cpu = alloc_percpu(struct tick_work);
5759 	BUG_ON(!tick_work_cpu);
5760 	return 0;
5761 }
5762 
5763 #else /* !CONFIG_NO_HZ_FULL */
5764 static inline void sched_tick_start(int cpu) { }
5765 static inline void sched_tick_stop(int cpu) { }
5766 #endif
5767 
5768 #if defined(CONFIG_PREEMPTION) && (defined(CONFIG_DEBUG_PREEMPT) || \
5769 				defined(CONFIG_TRACE_PREEMPT_TOGGLE))
5770 /*
5771  * If the value passed in is equal to the current preempt count
5772  * then we just disabled preemption. Start timing the latency.
5773  */
5774 static inline void preempt_latency_start(int val)
5775 {
5776 	if (preempt_count() == val) {
5777 		unsigned long ip = get_lock_parent_ip();
5778 #ifdef CONFIG_DEBUG_PREEMPT
5779 		current->preempt_disable_ip = ip;
5780 #endif
5781 		trace_preempt_off(CALLER_ADDR0, ip);
5782 	}
5783 }
5784 
5785 void preempt_count_add(int val)
5786 {
5787 #ifdef CONFIG_DEBUG_PREEMPT
5788 	/*
5789 	 * Underflow?
5790 	 */
5791 	if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
5792 		return;
5793 #endif
5794 	__preempt_count_add(val);
5795 #ifdef CONFIG_DEBUG_PREEMPT
5796 	/*
5797 	 * Spinlock count overflowing soon?
5798 	 */
5799 	DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
5800 				PREEMPT_MASK - 10);
5801 #endif
5802 	preempt_latency_start(val);
5803 }
5804 EXPORT_SYMBOL(preempt_count_add);
5805 NOKPROBE_SYMBOL(preempt_count_add);
5806 
5807 /*
5808  * If the value passed in equals to the current preempt count
5809  * then we just enabled preemption. Stop timing the latency.
5810  */
5811 static inline void preempt_latency_stop(int val)
5812 {
5813 	if (preempt_count() == val)
5814 		trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
5815 }
5816 
5817 void preempt_count_sub(int val)
5818 {
5819 #ifdef CONFIG_DEBUG_PREEMPT
5820 	/*
5821 	 * Underflow?
5822 	 */
5823 	if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
5824 		return;
5825 	/*
5826 	 * Is the spinlock portion underflowing?
5827 	 */
5828 	if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
5829 			!(preempt_count() & PREEMPT_MASK)))
5830 		return;
5831 #endif
5832 
5833 	preempt_latency_stop(val);
5834 	__preempt_count_sub(val);
5835 }
5836 EXPORT_SYMBOL(preempt_count_sub);
5837 NOKPROBE_SYMBOL(preempt_count_sub);
5838 
5839 #else
5840 static inline void preempt_latency_start(int val) { }
5841 static inline void preempt_latency_stop(int val) { }
5842 #endif
5843 
5844 static inline unsigned long get_preempt_disable_ip(struct task_struct *p)
5845 {
5846 #ifdef CONFIG_DEBUG_PREEMPT
5847 	return p->preempt_disable_ip;
5848 #else
5849 	return 0;
5850 #endif
5851 }
5852 
5853 /*
5854  * Print scheduling while atomic bug:
5855  */
5856 static noinline void __schedule_bug(struct task_struct *prev)
5857 {
5858 	/* Save this before calling printk(), since that will clobber it */
5859 	unsigned long preempt_disable_ip = get_preempt_disable_ip(current);
5860 
5861 	if (oops_in_progress)
5862 		return;
5863 
5864 	printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
5865 		prev->comm, prev->pid, preempt_count());
5866 
5867 	debug_show_held_locks(prev);
5868 	print_modules();
5869 	if (irqs_disabled())
5870 		print_irqtrace_events(prev);
5871 	if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)) {
5872 		pr_err("Preemption disabled at:");
5873 		print_ip_sym(KERN_ERR, preempt_disable_ip);
5874 	}
5875 	check_panic_on_warn("scheduling while atomic");
5876 
5877 	dump_stack();
5878 	add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
5879 }
5880 
5881 /*
5882  * Various schedule()-time debugging checks and statistics:
5883  */
5884 static inline void schedule_debug(struct task_struct *prev, bool preempt)
5885 {
5886 #ifdef CONFIG_SCHED_STACK_END_CHECK
5887 	if (task_stack_end_corrupted(prev))
5888 		panic("corrupted stack end detected inside scheduler\n");
5889 
5890 	if (task_scs_end_corrupted(prev))
5891 		panic("corrupted shadow stack detected inside scheduler\n");
5892 #endif
5893 
5894 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
5895 	if (!preempt && READ_ONCE(prev->__state) && prev->non_block_count) {
5896 		printk(KERN_ERR "BUG: scheduling in a non-blocking section: %s/%d/%i\n",
5897 			prev->comm, prev->pid, prev->non_block_count);
5898 		dump_stack();
5899 		add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
5900 	}
5901 #endif
5902 
5903 	if (unlikely(in_atomic_preempt_off())) {
5904 		__schedule_bug(prev);
5905 		preempt_count_set(PREEMPT_DISABLED);
5906 	}
5907 	rcu_sleep_check();
5908 	SCHED_WARN_ON(ct_state() == CT_STATE_USER);
5909 
5910 	profile_hit(SCHED_PROFILING, __builtin_return_address(0));
5911 
5912 	schedstat_inc(this_rq()->sched_count);
5913 }
5914 
5915 static void prev_balance(struct rq *rq, struct task_struct *prev,
5916 			 struct rq_flags *rf)
5917 {
5918 	const struct sched_class *start_class = prev->sched_class;
5919 	const struct sched_class *class;
5920 
5921 #ifdef CONFIG_SCHED_CLASS_EXT
5922 	/*
5923 	 * SCX requires a balance() call before every pick_next_task() including
5924 	 * when waking up from SCHED_IDLE. If @start_class is below SCX, start
5925 	 * from SCX instead.
5926 	 */
5927 	if (scx_enabled() && sched_class_above(&ext_sched_class, start_class))
5928 		start_class = &ext_sched_class;
5929 #endif
5930 
5931 	/*
5932 	 * We must do the balancing pass before put_prev_task(), such
5933 	 * that when we release the rq->lock the task is in the same
5934 	 * state as before we took rq->lock.
5935 	 *
5936 	 * We can terminate the balance pass as soon as we know there is
5937 	 * a runnable task of @class priority or higher.
5938 	 */
5939 	for_active_class_range(class, start_class, &idle_sched_class) {
5940 		if (class->balance && class->balance(rq, prev, rf))
5941 			break;
5942 	}
5943 }
5944 
5945 /*
5946  * Pick up the highest-prio task:
5947  */
5948 static inline struct task_struct *
5949 __pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
5950 {
5951 	const struct sched_class *class;
5952 	struct task_struct *p;
5953 
5954 	rq->dl_server = NULL;
5955 
5956 	if (scx_enabled())
5957 		goto restart;
5958 
5959 	/*
5960 	 * Optimization: we know that if all tasks are in the fair class we can
5961 	 * call that function directly, but only if the @prev task wasn't of a
5962 	 * higher scheduling class, because otherwise those lose the
5963 	 * opportunity to pull in more work from other CPUs.
5964 	 */
5965 	if (likely(!sched_class_above(prev->sched_class, &fair_sched_class) &&
5966 		   rq->nr_running == rq->cfs.h_nr_running)) {
5967 
5968 		p = pick_next_task_fair(rq, prev, rf);
5969 		if (unlikely(p == RETRY_TASK))
5970 			goto restart;
5971 
5972 		/* Assume the next prioritized class is idle_sched_class */
5973 		if (!p) {
5974 			p = pick_task_idle(rq);
5975 			put_prev_set_next_task(rq, prev, p);
5976 		}
5977 
5978 		return p;
5979 	}
5980 
5981 restart:
5982 	prev_balance(rq, prev, rf);
5983 
5984 	for_each_active_class(class) {
5985 		if (class->pick_next_task) {
5986 			p = class->pick_next_task(rq, prev);
5987 			if (p)
5988 				return p;
5989 		} else {
5990 			p = class->pick_task(rq);
5991 			if (p) {
5992 				put_prev_set_next_task(rq, prev, p);
5993 				return p;
5994 			}
5995 		}
5996 	}
5997 
5998 	BUG(); /* The idle class should always have a runnable task. */
5999 }
6000 
6001 #ifdef CONFIG_SCHED_CORE
6002 static inline bool is_task_rq_idle(struct task_struct *t)
6003 {
6004 	return (task_rq(t)->idle == t);
6005 }
6006 
6007 static inline bool cookie_equals(struct task_struct *a, unsigned long cookie)
6008 {
6009 	return is_task_rq_idle(a) || (a->core_cookie == cookie);
6010 }
6011 
6012 static inline bool cookie_match(struct task_struct *a, struct task_struct *b)
6013 {
6014 	if (is_task_rq_idle(a) || is_task_rq_idle(b))
6015 		return true;
6016 
6017 	return a->core_cookie == b->core_cookie;
6018 }
6019 
6020 static inline struct task_struct *pick_task(struct rq *rq)
6021 {
6022 	const struct sched_class *class;
6023 	struct task_struct *p;
6024 
6025 	rq->dl_server = NULL;
6026 
6027 	for_each_active_class(class) {
6028 		p = class->pick_task(rq);
6029 		if (p)
6030 			return p;
6031 	}
6032 
6033 	BUG(); /* The idle class should always have a runnable task. */
6034 }
6035 
6036 extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
6037 
6038 static void queue_core_balance(struct rq *rq);
6039 
6040 static struct task_struct *
6041 pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
6042 {
6043 	struct task_struct *next, *p, *max = NULL;
6044 	const struct cpumask *smt_mask;
6045 	bool fi_before = false;
6046 	bool core_clock_updated = (rq == rq->core);
6047 	unsigned long cookie;
6048 	int i, cpu, occ = 0;
6049 	struct rq *rq_i;
6050 	bool need_sync;
6051 
6052 	if (!sched_core_enabled(rq))
6053 		return __pick_next_task(rq, prev, rf);
6054 
6055 	cpu = cpu_of(rq);
6056 
6057 	/* Stopper task is switching into idle, no need core-wide selection. */
6058 	if (cpu_is_offline(cpu)) {
6059 		/*
6060 		 * Reset core_pick so that we don't enter the fastpath when
6061 		 * coming online. core_pick would already be migrated to
6062 		 * another cpu during offline.
6063 		 */
6064 		rq->core_pick = NULL;
6065 		rq->core_dl_server = NULL;
6066 		return __pick_next_task(rq, prev, rf);
6067 	}
6068 
6069 	/*
6070 	 * If there were no {en,de}queues since we picked (IOW, the task
6071 	 * pointers are all still valid), and we haven't scheduled the last
6072 	 * pick yet, do so now.
6073 	 *
6074 	 * rq->core_pick can be NULL if no selection was made for a CPU because
6075 	 * it was either offline or went offline during a sibling's core-wide
6076 	 * selection. In this case, do a core-wide selection.
6077 	 */
6078 	if (rq->core->core_pick_seq == rq->core->core_task_seq &&
6079 	    rq->core->core_pick_seq != rq->core_sched_seq &&
6080 	    rq->core_pick) {
6081 		WRITE_ONCE(rq->core_sched_seq, rq->core->core_pick_seq);
6082 
6083 		next = rq->core_pick;
6084 		rq->dl_server = rq->core_dl_server;
6085 		rq->core_pick = NULL;
6086 		rq->core_dl_server = NULL;
6087 		goto out_set_next;
6088 	}
6089 
6090 	prev_balance(rq, prev, rf);
6091 
6092 	smt_mask = cpu_smt_mask(cpu);
6093 	need_sync = !!rq->core->core_cookie;
6094 
6095 	/* reset state */
6096 	rq->core->core_cookie = 0UL;
6097 	if (rq->core->core_forceidle_count) {
6098 		if (!core_clock_updated) {
6099 			update_rq_clock(rq->core);
6100 			core_clock_updated = true;
6101 		}
6102 		sched_core_account_forceidle(rq);
6103 		/* reset after accounting force idle */
6104 		rq->core->core_forceidle_start = 0;
6105 		rq->core->core_forceidle_count = 0;
6106 		rq->core->core_forceidle_occupation = 0;
6107 		need_sync = true;
6108 		fi_before = true;
6109 	}
6110 
6111 	/*
6112 	 * core->core_task_seq, core->core_pick_seq, rq->core_sched_seq
6113 	 *
6114 	 * @task_seq guards the task state ({en,de}queues)
6115 	 * @pick_seq is the @task_seq we did a selection on
6116 	 * @sched_seq is the @pick_seq we scheduled
6117 	 *
6118 	 * However, preemptions can cause multiple picks on the same task set.
6119 	 * 'Fix' this by also increasing @task_seq for every pick.
6120 	 */
6121 	rq->core->core_task_seq++;
6122 
6123 	/*
6124 	 * Optimize for common case where this CPU has no cookies
6125 	 * and there are no cookied tasks running on siblings.
6126 	 */
6127 	if (!need_sync) {
6128 		next = pick_task(rq);
6129 		if (!next->core_cookie) {
6130 			rq->core_pick = NULL;
6131 			rq->core_dl_server = NULL;
6132 			/*
6133 			 * For robustness, update the min_vruntime_fi for
6134 			 * unconstrained picks as well.
6135 			 */
6136 			WARN_ON_ONCE(fi_before);
6137 			task_vruntime_update(rq, next, false);
6138 			goto out_set_next;
6139 		}
6140 	}
6141 
6142 	/*
6143 	 * For each thread: do the regular task pick and find the max prio task
6144 	 * amongst them.
6145 	 *
6146 	 * Tie-break prio towards the current CPU
6147 	 */
6148 	for_each_cpu_wrap(i, smt_mask, cpu) {
6149 		rq_i = cpu_rq(i);
6150 
6151 		/*
6152 		 * Current cpu always has its clock updated on entrance to
6153 		 * pick_next_task(). If the current cpu is not the core,
6154 		 * the core may also have been updated above.
6155 		 */
6156 		if (i != cpu && (rq_i != rq->core || !core_clock_updated))
6157 			update_rq_clock(rq_i);
6158 
6159 		rq_i->core_pick = p = pick_task(rq_i);
6160 		rq_i->core_dl_server = rq_i->dl_server;
6161 
6162 		if (!max || prio_less(max, p, fi_before))
6163 			max = p;
6164 	}
6165 
6166 	cookie = rq->core->core_cookie = max->core_cookie;
6167 
6168 	/*
6169 	 * For each thread: try and find a runnable task that matches @max or
6170 	 * force idle.
6171 	 */
6172 	for_each_cpu(i, smt_mask) {
6173 		rq_i = cpu_rq(i);
6174 		p = rq_i->core_pick;
6175 
6176 		if (!cookie_equals(p, cookie)) {
6177 			p = NULL;
6178 			if (cookie)
6179 				p = sched_core_find(rq_i, cookie);
6180 			if (!p)
6181 				p = idle_sched_class.pick_task(rq_i);
6182 		}
6183 
6184 		rq_i->core_pick = p;
6185 		rq_i->core_dl_server = NULL;
6186 
6187 		if (p == rq_i->idle) {
6188 			if (rq_i->nr_running) {
6189 				rq->core->core_forceidle_count++;
6190 				if (!fi_before)
6191 					rq->core->core_forceidle_seq++;
6192 			}
6193 		} else {
6194 			occ++;
6195 		}
6196 	}
6197 
6198 	if (schedstat_enabled() && rq->core->core_forceidle_count) {
6199 		rq->core->core_forceidle_start = rq_clock(rq->core);
6200 		rq->core->core_forceidle_occupation = occ;
6201 	}
6202 
6203 	rq->core->core_pick_seq = rq->core->core_task_seq;
6204 	next = rq->core_pick;
6205 	rq->core_sched_seq = rq->core->core_pick_seq;
6206 
6207 	/* Something should have been selected for current CPU */
6208 	WARN_ON_ONCE(!next);
6209 
6210 	/*
6211 	 * Reschedule siblings
6212 	 *
6213 	 * NOTE: L1TF -- at this point we're no longer running the old task and
6214 	 * sending an IPI (below) ensures the sibling will no longer be running
6215 	 * their task. This ensures there is no inter-sibling overlap between
6216 	 * non-matching user state.
6217 	 */
6218 	for_each_cpu(i, smt_mask) {
6219 		rq_i = cpu_rq(i);
6220 
6221 		/*
6222 		 * An online sibling might have gone offline before a task
6223 		 * could be picked for it, or it might be offline but later
6224 		 * happen to come online, but its too late and nothing was
6225 		 * picked for it.  That's Ok - it will pick tasks for itself,
6226 		 * so ignore it.
6227 		 */
6228 		if (!rq_i->core_pick)
6229 			continue;
6230 
6231 		/*
6232 		 * Update for new !FI->FI transitions, or if continuing to be in !FI:
6233 		 * fi_before     fi      update?
6234 		 *  0            0       1
6235 		 *  0            1       1
6236 		 *  1            0       1
6237 		 *  1            1       0
6238 		 */
6239 		if (!(fi_before && rq->core->core_forceidle_count))
6240 			task_vruntime_update(rq_i, rq_i->core_pick, !!rq->core->core_forceidle_count);
6241 
6242 		rq_i->core_pick->core_occupation = occ;
6243 
6244 		if (i == cpu) {
6245 			rq_i->core_pick = NULL;
6246 			rq_i->core_dl_server = NULL;
6247 			continue;
6248 		}
6249 
6250 		/* Did we break L1TF mitigation requirements? */
6251 		WARN_ON_ONCE(!cookie_match(next, rq_i->core_pick));
6252 
6253 		if (rq_i->curr == rq_i->core_pick) {
6254 			rq_i->core_pick = NULL;
6255 			rq_i->core_dl_server = NULL;
6256 			continue;
6257 		}
6258 
6259 		resched_curr(rq_i);
6260 	}
6261 
6262 out_set_next:
6263 	put_prev_set_next_task(rq, prev, next);
6264 	if (rq->core->core_forceidle_count && next == rq->idle)
6265 		queue_core_balance(rq);
6266 
6267 	return next;
6268 }
6269 
6270 static bool try_steal_cookie(int this, int that)
6271 {
6272 	struct rq *dst = cpu_rq(this), *src = cpu_rq(that);
6273 	struct task_struct *p;
6274 	unsigned long cookie;
6275 	bool success = false;
6276 
6277 	guard(irq)();
6278 	guard(double_rq_lock)(dst, src);
6279 
6280 	cookie = dst->core->core_cookie;
6281 	if (!cookie)
6282 		return false;
6283 
6284 	if (dst->curr != dst->idle)
6285 		return false;
6286 
6287 	p = sched_core_find(src, cookie);
6288 	if (!p)
6289 		return false;
6290 
6291 	do {
6292 		if (p == src->core_pick || p == src->curr)
6293 			goto next;
6294 
6295 		if (!is_cpu_allowed(p, this))
6296 			goto next;
6297 
6298 		if (p->core_occupation > dst->idle->core_occupation)
6299 			goto next;
6300 		/*
6301 		 * sched_core_find() and sched_core_next() will ensure
6302 		 * that task @p is not throttled now, we also need to
6303 		 * check whether the runqueue of the destination CPU is
6304 		 * being throttled.
6305 		 */
6306 		if (sched_task_is_throttled(p, this))
6307 			goto next;
6308 
6309 		deactivate_task(src, p, 0);
6310 		set_task_cpu(p, this);
6311 		activate_task(dst, p, 0);
6312 
6313 		resched_curr(dst);
6314 
6315 		success = true;
6316 		break;
6317 
6318 next:
6319 		p = sched_core_next(p, cookie);
6320 	} while (p);
6321 
6322 	return success;
6323 }
6324 
6325 static bool steal_cookie_task(int cpu, struct sched_domain *sd)
6326 {
6327 	int i;
6328 
6329 	for_each_cpu_wrap(i, sched_domain_span(sd), cpu + 1) {
6330 		if (i == cpu)
6331 			continue;
6332 
6333 		if (need_resched())
6334 			break;
6335 
6336 		if (try_steal_cookie(cpu, i))
6337 			return true;
6338 	}
6339 
6340 	return false;
6341 }
6342 
6343 static void sched_core_balance(struct rq *rq)
6344 {
6345 	struct sched_domain *sd;
6346 	int cpu = cpu_of(rq);
6347 
6348 	guard(preempt)();
6349 	guard(rcu)();
6350 
6351 	raw_spin_rq_unlock_irq(rq);
6352 	for_each_domain(cpu, sd) {
6353 		if (need_resched())
6354 			break;
6355 
6356 		if (steal_cookie_task(cpu, sd))
6357 			break;
6358 	}
6359 	raw_spin_rq_lock_irq(rq);
6360 }
6361 
6362 static DEFINE_PER_CPU(struct balance_callback, core_balance_head);
6363 
6364 static void queue_core_balance(struct rq *rq)
6365 {
6366 	if (!sched_core_enabled(rq))
6367 		return;
6368 
6369 	if (!rq->core->core_cookie)
6370 		return;
6371 
6372 	if (!rq->nr_running) /* not forced idle */
6373 		return;
6374 
6375 	queue_balance_callback(rq, &per_cpu(core_balance_head, rq->cpu), sched_core_balance);
6376 }
6377 
6378 DEFINE_LOCK_GUARD_1(core_lock, int,
6379 		    sched_core_lock(*_T->lock, &_T->flags),
6380 		    sched_core_unlock(*_T->lock, &_T->flags),
6381 		    unsigned long flags)
6382 
6383 static void sched_core_cpu_starting(unsigned int cpu)
6384 {
6385 	const struct cpumask *smt_mask = cpu_smt_mask(cpu);
6386 	struct rq *rq = cpu_rq(cpu), *core_rq = NULL;
6387 	int t;
6388 
6389 	guard(core_lock)(&cpu);
6390 
6391 	WARN_ON_ONCE(rq->core != rq);
6392 
6393 	/* if we're the first, we'll be our own leader */
6394 	if (cpumask_weight(smt_mask) == 1)
6395 		return;
6396 
6397 	/* find the leader */
6398 	for_each_cpu(t, smt_mask) {
6399 		if (t == cpu)
6400 			continue;
6401 		rq = cpu_rq(t);
6402 		if (rq->core == rq) {
6403 			core_rq = rq;
6404 			break;
6405 		}
6406 	}
6407 
6408 	if (WARN_ON_ONCE(!core_rq)) /* whoopsie */
6409 		return;
6410 
6411 	/* install and validate core_rq */
6412 	for_each_cpu(t, smt_mask) {
6413 		rq = cpu_rq(t);
6414 
6415 		if (t == cpu)
6416 			rq->core = core_rq;
6417 
6418 		WARN_ON_ONCE(rq->core != core_rq);
6419 	}
6420 }
6421 
6422 static void sched_core_cpu_deactivate(unsigned int cpu)
6423 {
6424 	const struct cpumask *smt_mask = cpu_smt_mask(cpu);
6425 	struct rq *rq = cpu_rq(cpu), *core_rq = NULL;
6426 	int t;
6427 
6428 	guard(core_lock)(&cpu);
6429 
6430 	/* if we're the last man standing, nothing to do */
6431 	if (cpumask_weight(smt_mask) == 1) {
6432 		WARN_ON_ONCE(rq->core != rq);
6433 		return;
6434 	}
6435 
6436 	/* if we're not the leader, nothing to do */
6437 	if (rq->core != rq)
6438 		return;
6439 
6440 	/* find a new leader */
6441 	for_each_cpu(t, smt_mask) {
6442 		if (t == cpu)
6443 			continue;
6444 		core_rq = cpu_rq(t);
6445 		break;
6446 	}
6447 
6448 	if (WARN_ON_ONCE(!core_rq)) /* impossible */
6449 		return;
6450 
6451 	/* copy the shared state to the new leader */
6452 	core_rq->core_task_seq             = rq->core_task_seq;
6453 	core_rq->core_pick_seq             = rq->core_pick_seq;
6454 	core_rq->core_cookie               = rq->core_cookie;
6455 	core_rq->core_forceidle_count      = rq->core_forceidle_count;
6456 	core_rq->core_forceidle_seq        = rq->core_forceidle_seq;
6457 	core_rq->core_forceidle_occupation = rq->core_forceidle_occupation;
6458 
6459 	/*
6460 	 * Accounting edge for forced idle is handled in pick_next_task().
6461 	 * Don't need another one here, since the hotplug thread shouldn't
6462 	 * have a cookie.
6463 	 */
6464 	core_rq->core_forceidle_start = 0;
6465 
6466 	/* install new leader */
6467 	for_each_cpu(t, smt_mask) {
6468 		rq = cpu_rq(t);
6469 		rq->core = core_rq;
6470 	}
6471 }
6472 
6473 static inline void sched_core_cpu_dying(unsigned int cpu)
6474 {
6475 	struct rq *rq = cpu_rq(cpu);
6476 
6477 	if (rq->core != rq)
6478 		rq->core = rq;
6479 }
6480 
6481 #else /* !CONFIG_SCHED_CORE */
6482 
6483 static inline void sched_core_cpu_starting(unsigned int cpu) {}
6484 static inline void sched_core_cpu_deactivate(unsigned int cpu) {}
6485 static inline void sched_core_cpu_dying(unsigned int cpu) {}
6486 
6487 static struct task_struct *
6488 pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
6489 {
6490 	return __pick_next_task(rq, prev, rf);
6491 }
6492 
6493 #endif /* CONFIG_SCHED_CORE */
6494 
6495 /*
6496  * Constants for the sched_mode argument of __schedule().
6497  *
6498  * The mode argument allows RT enabled kernels to differentiate a
6499  * preemption from blocking on an 'sleeping' spin/rwlock.
6500  */
6501 #define SM_IDLE			(-1)
6502 #define SM_NONE			0
6503 #define SM_PREEMPT		1
6504 #define SM_RTLOCK_WAIT		2
6505 
6506 /*
6507  * __schedule() is the main scheduler function.
6508  *
6509  * The main means of driving the scheduler and thus entering this function are:
6510  *
6511  *   1. Explicit blocking: mutex, semaphore, waitqueue, etc.
6512  *
6513  *   2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
6514  *      paths. For example, see arch/x86/entry_64.S.
6515  *
6516  *      To drive preemption between tasks, the scheduler sets the flag in timer
6517  *      interrupt handler sched_tick().
6518  *
6519  *   3. Wakeups don't really cause entry into schedule(). They add a
6520  *      task to the run-queue and that's it.
6521  *
6522  *      Now, if the new task added to the run-queue preempts the current
6523  *      task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
6524  *      called on the nearest possible occasion:
6525  *
6526  *       - If the kernel is preemptible (CONFIG_PREEMPTION=y):
6527  *
6528  *         - in syscall or exception context, at the next outmost
6529  *           preempt_enable(). (this might be as soon as the wake_up()'s
6530  *           spin_unlock()!)
6531  *
6532  *         - in IRQ context, return from interrupt-handler to
6533  *           preemptible context
6534  *
6535  *       - If the kernel is not preemptible (CONFIG_PREEMPTION is not set)
6536  *         then at the next:
6537  *
6538  *          - cond_resched() call
6539  *          - explicit schedule() call
6540  *          - return from syscall or exception to user-space
6541  *          - return from interrupt-handler to user-space
6542  *
6543  * WARNING: must be called with preemption disabled!
6544  */
6545 static void __sched notrace __schedule(int sched_mode)
6546 {
6547 	struct task_struct *prev, *next;
6548 	/*
6549 	 * On PREEMPT_RT kernel, SM_RTLOCK_WAIT is noted
6550 	 * as a preemption by schedule_debug() and RCU.
6551 	 */
6552 	bool preempt = sched_mode > SM_NONE;
6553 	bool block = false;
6554 	unsigned long *switch_count;
6555 	unsigned long prev_state;
6556 	struct rq_flags rf;
6557 	struct rq *rq;
6558 	int cpu;
6559 
6560 	cpu = smp_processor_id();
6561 	rq = cpu_rq(cpu);
6562 	prev = rq->curr;
6563 
6564 	schedule_debug(prev, preempt);
6565 
6566 	if (sched_feat(HRTICK) || sched_feat(HRTICK_DL))
6567 		hrtick_clear(rq);
6568 
6569 	local_irq_disable();
6570 	rcu_note_context_switch(preempt);
6571 
6572 	/*
6573 	 * Make sure that signal_pending_state()->signal_pending() below
6574 	 * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
6575 	 * done by the caller to avoid the race with signal_wake_up():
6576 	 *
6577 	 * __set_current_state(@state)		signal_wake_up()
6578 	 * schedule()				  set_tsk_thread_flag(p, TIF_SIGPENDING)
6579 	 *					  wake_up_state(p, state)
6580 	 *   LOCK rq->lock			    LOCK p->pi_state
6581 	 *   smp_mb__after_spinlock()		    smp_mb__after_spinlock()
6582 	 *     if (signal_pending_state())	    if (p->state & @state)
6583 	 *
6584 	 * Also, the membarrier system call requires a full memory barrier
6585 	 * after coming from user-space, before storing to rq->curr; this
6586 	 * barrier matches a full barrier in the proximity of the membarrier
6587 	 * system call exit.
6588 	 */
6589 	rq_lock(rq, &rf);
6590 	smp_mb__after_spinlock();
6591 
6592 	/* Promote REQ to ACT */
6593 	rq->clock_update_flags <<= 1;
6594 	update_rq_clock(rq);
6595 	rq->clock_update_flags = RQCF_UPDATED;
6596 
6597 	switch_count = &prev->nivcsw;
6598 
6599 	/* Task state changes only considers SM_PREEMPT as preemption */
6600 	preempt = sched_mode == SM_PREEMPT;
6601 
6602 	/*
6603 	 * We must load prev->state once (task_struct::state is volatile), such
6604 	 * that we form a control dependency vs deactivate_task() below.
6605 	 */
6606 	prev_state = READ_ONCE(prev->__state);
6607 	if (sched_mode == SM_IDLE) {
6608 		/* SCX must consult the BPF scheduler to tell if rq is empty */
6609 		if (!rq->nr_running && !scx_enabled()) {
6610 			next = prev;
6611 			goto picked;
6612 		}
6613 	} else if (!preempt && prev_state) {
6614 		if (signal_pending_state(prev_state, prev)) {
6615 			WRITE_ONCE(prev->__state, TASK_RUNNING);
6616 		} else {
6617 			int flags = DEQUEUE_NOCLOCK;
6618 
6619 			prev->sched_contributes_to_load =
6620 				(prev_state & TASK_UNINTERRUPTIBLE) &&
6621 				!(prev_state & TASK_NOLOAD) &&
6622 				!(prev_state & TASK_FROZEN);
6623 
6624 			if (unlikely(is_special_task_state(prev_state)))
6625 				flags |= DEQUEUE_SPECIAL;
6626 
6627 			/*
6628 			 * __schedule()			ttwu()
6629 			 *   prev_state = prev->state;    if (p->on_rq && ...)
6630 			 *   if (prev_state)		    goto out;
6631 			 *     p->on_rq = 0;		  smp_acquire__after_ctrl_dep();
6632 			 *				  p->state = TASK_WAKING
6633 			 *
6634 			 * Where __schedule() and ttwu() have matching control dependencies.
6635 			 *
6636 			 * After this, schedule() must not care about p->state any more.
6637 			 */
6638 			block_task(rq, prev, flags);
6639 			block = true;
6640 		}
6641 		switch_count = &prev->nvcsw;
6642 	}
6643 
6644 	next = pick_next_task(rq, prev, &rf);
6645 picked:
6646 	clear_tsk_need_resched(prev);
6647 	clear_preempt_need_resched();
6648 #ifdef CONFIG_SCHED_DEBUG
6649 	rq->last_seen_need_resched_ns = 0;
6650 #endif
6651 
6652 	if (likely(prev != next)) {
6653 		rq->nr_switches++;
6654 		/*
6655 		 * RCU users of rcu_dereference(rq->curr) may not see
6656 		 * changes to task_struct made by pick_next_task().
6657 		 */
6658 		RCU_INIT_POINTER(rq->curr, next);
6659 		/*
6660 		 * The membarrier system call requires each architecture
6661 		 * to have a full memory barrier after updating
6662 		 * rq->curr, before returning to user-space.
6663 		 *
6664 		 * Here are the schemes providing that barrier on the
6665 		 * various architectures:
6666 		 * - mm ? switch_mm() : mmdrop() for x86, s390, sparc, PowerPC,
6667 		 *   RISC-V.  switch_mm() relies on membarrier_arch_switch_mm()
6668 		 *   on PowerPC and on RISC-V.
6669 		 * - finish_lock_switch() for weakly-ordered
6670 		 *   architectures where spin_unlock is a full barrier,
6671 		 * - switch_to() for arm64 (weakly-ordered, spin_unlock
6672 		 *   is a RELEASE barrier),
6673 		 *
6674 		 * The barrier matches a full barrier in the proximity of
6675 		 * the membarrier system call entry.
6676 		 *
6677 		 * On RISC-V, this barrier pairing is also needed for the
6678 		 * SYNC_CORE command when switching between processes, cf.
6679 		 * the inline comments in membarrier_arch_switch_mm().
6680 		 */
6681 		++*switch_count;
6682 
6683 		migrate_disable_switch(rq, prev);
6684 		psi_account_irqtime(rq, prev, next);
6685 		psi_sched_switch(prev, next, block);
6686 
6687 		trace_sched_switch(preempt, prev, next, prev_state);
6688 
6689 		/* Also unlocks the rq: */
6690 		rq = context_switch(rq, prev, next, &rf);
6691 	} else {
6692 		rq_unpin_lock(rq, &rf);
6693 		__balance_callbacks(rq);
6694 		raw_spin_rq_unlock_irq(rq);
6695 	}
6696 }
6697 
6698 void __noreturn do_task_dead(void)
6699 {
6700 	/* Causes final put_task_struct in finish_task_switch(): */
6701 	set_special_state(TASK_DEAD);
6702 
6703 	/* Tell freezer to ignore us: */
6704 	current->flags |= PF_NOFREEZE;
6705 
6706 	__schedule(SM_NONE);
6707 	BUG();
6708 
6709 	/* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */
6710 	for (;;)
6711 		cpu_relax();
6712 }
6713 
6714 static inline void sched_submit_work(struct task_struct *tsk)
6715 {
6716 	static DEFINE_WAIT_OVERRIDE_MAP(sched_map, LD_WAIT_CONFIG);
6717 	unsigned int task_flags;
6718 
6719 	/*
6720 	 * Establish LD_WAIT_CONFIG context to ensure none of the code called
6721 	 * will use a blocking primitive -- which would lead to recursion.
6722 	 */
6723 	lock_map_acquire_try(&sched_map);
6724 
6725 	task_flags = tsk->flags;
6726 	/*
6727 	 * If a worker goes to sleep, notify and ask workqueue whether it
6728 	 * wants to wake up a task to maintain concurrency.
6729 	 */
6730 	if (task_flags & PF_WQ_WORKER)
6731 		wq_worker_sleeping(tsk);
6732 	else if (task_flags & PF_IO_WORKER)
6733 		io_wq_worker_sleeping(tsk);
6734 
6735 	/*
6736 	 * spinlock and rwlock must not flush block requests.  This will
6737 	 * deadlock if the callback attempts to acquire a lock which is
6738 	 * already acquired.
6739 	 */
6740 	SCHED_WARN_ON(current->__state & TASK_RTLOCK_WAIT);
6741 
6742 	/*
6743 	 * If we are going to sleep and we have plugged IO queued,
6744 	 * make sure to submit it to avoid deadlocks.
6745 	 */
6746 	blk_flush_plug(tsk->plug, true);
6747 
6748 	lock_map_release(&sched_map);
6749 }
6750 
6751 static void sched_update_worker(struct task_struct *tsk)
6752 {
6753 	if (tsk->flags & (PF_WQ_WORKER | PF_IO_WORKER | PF_BLOCK_TS)) {
6754 		if (tsk->flags & PF_BLOCK_TS)
6755 			blk_plug_invalidate_ts(tsk);
6756 		if (tsk->flags & PF_WQ_WORKER)
6757 			wq_worker_running(tsk);
6758 		else if (tsk->flags & PF_IO_WORKER)
6759 			io_wq_worker_running(tsk);
6760 	}
6761 }
6762 
6763 static __always_inline void __schedule_loop(int sched_mode)
6764 {
6765 	do {
6766 		preempt_disable();
6767 		__schedule(sched_mode);
6768 		sched_preempt_enable_no_resched();
6769 	} while (need_resched());
6770 }
6771 
6772 asmlinkage __visible void __sched schedule(void)
6773 {
6774 	struct task_struct *tsk = current;
6775 
6776 #ifdef CONFIG_RT_MUTEXES
6777 	lockdep_assert(!tsk->sched_rt_mutex);
6778 #endif
6779 
6780 	if (!task_is_running(tsk))
6781 		sched_submit_work(tsk);
6782 	__schedule_loop(SM_NONE);
6783 	sched_update_worker(tsk);
6784 }
6785 EXPORT_SYMBOL(schedule);
6786 
6787 /*
6788  * synchronize_rcu_tasks() makes sure that no task is stuck in preempted
6789  * state (have scheduled out non-voluntarily) by making sure that all
6790  * tasks have either left the run queue or have gone into user space.
6791  * As idle tasks do not do either, they must not ever be preempted
6792  * (schedule out non-voluntarily).
6793  *
6794  * schedule_idle() is similar to schedule_preempt_disable() except that it
6795  * never enables preemption because it does not call sched_submit_work().
6796  */
6797 void __sched schedule_idle(void)
6798 {
6799 	/*
6800 	 * As this skips calling sched_submit_work(), which the idle task does
6801 	 * regardless because that function is a NOP when the task is in a
6802 	 * TASK_RUNNING state, make sure this isn't used someplace that the
6803 	 * current task can be in any other state. Note, idle is always in the
6804 	 * TASK_RUNNING state.
6805 	 */
6806 	WARN_ON_ONCE(current->__state);
6807 	do {
6808 		__schedule(SM_IDLE);
6809 	} while (need_resched());
6810 }
6811 
6812 #if defined(CONFIG_CONTEXT_TRACKING_USER) && !defined(CONFIG_HAVE_CONTEXT_TRACKING_USER_OFFSTACK)
6813 asmlinkage __visible void __sched schedule_user(void)
6814 {
6815 	/*
6816 	 * If we come here after a random call to set_need_resched(),
6817 	 * or we have been woken up remotely but the IPI has not yet arrived,
6818 	 * we haven't yet exited the RCU idle mode. Do it here manually until
6819 	 * we find a better solution.
6820 	 *
6821 	 * NB: There are buggy callers of this function.  Ideally we
6822 	 * should warn if prev_state != CT_STATE_USER, but that will trigger
6823 	 * too frequently to make sense yet.
6824 	 */
6825 	enum ctx_state prev_state = exception_enter();
6826 	schedule();
6827 	exception_exit(prev_state);
6828 }
6829 #endif
6830 
6831 /**
6832  * schedule_preempt_disabled - called with preemption disabled
6833  *
6834  * Returns with preemption disabled. Note: preempt_count must be 1
6835  */
6836 void __sched schedule_preempt_disabled(void)
6837 {
6838 	sched_preempt_enable_no_resched();
6839 	schedule();
6840 	preempt_disable();
6841 }
6842 
6843 #ifdef CONFIG_PREEMPT_RT
6844 void __sched notrace schedule_rtlock(void)
6845 {
6846 	__schedule_loop(SM_RTLOCK_WAIT);
6847 }
6848 NOKPROBE_SYMBOL(schedule_rtlock);
6849 #endif
6850 
6851 static void __sched notrace preempt_schedule_common(void)
6852 {
6853 	do {
6854 		/*
6855 		 * Because the function tracer can trace preempt_count_sub()
6856 		 * and it also uses preempt_enable/disable_notrace(), if
6857 		 * NEED_RESCHED is set, the preempt_enable_notrace() called
6858 		 * by the function tracer will call this function again and
6859 		 * cause infinite recursion.
6860 		 *
6861 		 * Preemption must be disabled here before the function
6862 		 * tracer can trace. Break up preempt_disable() into two
6863 		 * calls. One to disable preemption without fear of being
6864 		 * traced. The other to still record the preemption latency,
6865 		 * which can also be traced by the function tracer.
6866 		 */
6867 		preempt_disable_notrace();
6868 		preempt_latency_start(1);
6869 		__schedule(SM_PREEMPT);
6870 		preempt_latency_stop(1);
6871 		preempt_enable_no_resched_notrace();
6872 
6873 		/*
6874 		 * Check again in case we missed a preemption opportunity
6875 		 * between schedule and now.
6876 		 */
6877 	} while (need_resched());
6878 }
6879 
6880 #ifdef CONFIG_PREEMPTION
6881 /*
6882  * This is the entry point to schedule() from in-kernel preemption
6883  * off of preempt_enable.
6884  */
6885 asmlinkage __visible void __sched notrace preempt_schedule(void)
6886 {
6887 	/*
6888 	 * If there is a non-zero preempt_count or interrupts are disabled,
6889 	 * we do not want to preempt the current task. Just return..
6890 	 */
6891 	if (likely(!preemptible()))
6892 		return;
6893 	preempt_schedule_common();
6894 }
6895 NOKPROBE_SYMBOL(preempt_schedule);
6896 EXPORT_SYMBOL(preempt_schedule);
6897 
6898 #ifdef CONFIG_PREEMPT_DYNAMIC
6899 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
6900 #ifndef preempt_schedule_dynamic_enabled
6901 #define preempt_schedule_dynamic_enabled	preempt_schedule
6902 #define preempt_schedule_dynamic_disabled	NULL
6903 #endif
6904 DEFINE_STATIC_CALL(preempt_schedule, preempt_schedule_dynamic_enabled);
6905 EXPORT_STATIC_CALL_TRAMP(preempt_schedule);
6906 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
6907 static DEFINE_STATIC_KEY_TRUE(sk_dynamic_preempt_schedule);
6908 void __sched notrace dynamic_preempt_schedule(void)
6909 {
6910 	if (!static_branch_unlikely(&sk_dynamic_preempt_schedule))
6911 		return;
6912 	preempt_schedule();
6913 }
6914 NOKPROBE_SYMBOL(dynamic_preempt_schedule);
6915 EXPORT_SYMBOL(dynamic_preempt_schedule);
6916 #endif
6917 #endif
6918 
6919 /**
6920  * preempt_schedule_notrace - preempt_schedule called by tracing
6921  *
6922  * The tracing infrastructure uses preempt_enable_notrace to prevent
6923  * recursion and tracing preempt enabling caused by the tracing
6924  * infrastructure itself. But as tracing can happen in areas coming
6925  * from userspace or just about to enter userspace, a preempt enable
6926  * can occur before user_exit() is called. This will cause the scheduler
6927  * to be called when the system is still in usermode.
6928  *
6929  * To prevent this, the preempt_enable_notrace will use this function
6930  * instead of preempt_schedule() to exit user context if needed before
6931  * calling the scheduler.
6932  */
6933 asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
6934 {
6935 	enum ctx_state prev_ctx;
6936 
6937 	if (likely(!preemptible()))
6938 		return;
6939 
6940 	do {
6941 		/*
6942 		 * Because the function tracer can trace preempt_count_sub()
6943 		 * and it also uses preempt_enable/disable_notrace(), if
6944 		 * NEED_RESCHED is set, the preempt_enable_notrace() called
6945 		 * by the function tracer will call this function again and
6946 		 * cause infinite recursion.
6947 		 *
6948 		 * Preemption must be disabled here before the function
6949 		 * tracer can trace. Break up preempt_disable() into two
6950 		 * calls. One to disable preemption without fear of being
6951 		 * traced. The other to still record the preemption latency,
6952 		 * which can also be traced by the function tracer.
6953 		 */
6954 		preempt_disable_notrace();
6955 		preempt_latency_start(1);
6956 		/*
6957 		 * Needs preempt disabled in case user_exit() is traced
6958 		 * and the tracer calls preempt_enable_notrace() causing
6959 		 * an infinite recursion.
6960 		 */
6961 		prev_ctx = exception_enter();
6962 		__schedule(SM_PREEMPT);
6963 		exception_exit(prev_ctx);
6964 
6965 		preempt_latency_stop(1);
6966 		preempt_enable_no_resched_notrace();
6967 	} while (need_resched());
6968 }
6969 EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
6970 
6971 #ifdef CONFIG_PREEMPT_DYNAMIC
6972 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
6973 #ifndef preempt_schedule_notrace_dynamic_enabled
6974 #define preempt_schedule_notrace_dynamic_enabled	preempt_schedule_notrace
6975 #define preempt_schedule_notrace_dynamic_disabled	NULL
6976 #endif
6977 DEFINE_STATIC_CALL(preempt_schedule_notrace, preempt_schedule_notrace_dynamic_enabled);
6978 EXPORT_STATIC_CALL_TRAMP(preempt_schedule_notrace);
6979 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
6980 static DEFINE_STATIC_KEY_TRUE(sk_dynamic_preempt_schedule_notrace);
6981 void __sched notrace dynamic_preempt_schedule_notrace(void)
6982 {
6983 	if (!static_branch_unlikely(&sk_dynamic_preempt_schedule_notrace))
6984 		return;
6985 	preempt_schedule_notrace();
6986 }
6987 NOKPROBE_SYMBOL(dynamic_preempt_schedule_notrace);
6988 EXPORT_SYMBOL(dynamic_preempt_schedule_notrace);
6989 #endif
6990 #endif
6991 
6992 #endif /* CONFIG_PREEMPTION */
6993 
6994 /*
6995  * This is the entry point to schedule() from kernel preemption
6996  * off of IRQ context.
6997  * Note, that this is called and return with IRQs disabled. This will
6998  * protect us against recursive calling from IRQ contexts.
6999  */
7000 asmlinkage __visible void __sched preempt_schedule_irq(void)
7001 {
7002 	enum ctx_state prev_state;
7003 
7004 	/* Catch callers which need to be fixed */
7005 	BUG_ON(preempt_count() || !irqs_disabled());
7006 
7007 	prev_state = exception_enter();
7008 
7009 	do {
7010 		preempt_disable();
7011 		local_irq_enable();
7012 		__schedule(SM_PREEMPT);
7013 		local_irq_disable();
7014 		sched_preempt_enable_no_resched();
7015 	} while (need_resched());
7016 
7017 	exception_exit(prev_state);
7018 }
7019 
7020 int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
7021 			  void *key)
7022 {
7023 	WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~(WF_SYNC|WF_CURRENT_CPU));
7024 	return try_to_wake_up(curr->private, mode, wake_flags);
7025 }
7026 EXPORT_SYMBOL(default_wake_function);
7027 
7028 const struct sched_class *__setscheduler_class(struct task_struct *p, int prio)
7029 {
7030 	if (dl_prio(prio))
7031 		return &dl_sched_class;
7032 
7033 	if (rt_prio(prio))
7034 		return &rt_sched_class;
7035 
7036 #ifdef CONFIG_SCHED_CLASS_EXT
7037 	if (task_should_scx(p))
7038 		return &ext_sched_class;
7039 #endif
7040 
7041 	return &fair_sched_class;
7042 }
7043 
7044 #ifdef CONFIG_RT_MUTEXES
7045 
7046 /*
7047  * Would be more useful with typeof()/auto_type but they don't mix with
7048  * bit-fields. Since it's a local thing, use int. Keep the generic sounding
7049  * name such that if someone were to implement this function we get to compare
7050  * notes.
7051  */
7052 #define fetch_and_set(x, v) ({ int _x = (x); (x) = (v); _x; })
7053 
7054 void rt_mutex_pre_schedule(void)
7055 {
7056 	lockdep_assert(!fetch_and_set(current->sched_rt_mutex, 1));
7057 	sched_submit_work(current);
7058 }
7059 
7060 void rt_mutex_schedule(void)
7061 {
7062 	lockdep_assert(current->sched_rt_mutex);
7063 	__schedule_loop(SM_NONE);
7064 }
7065 
7066 void rt_mutex_post_schedule(void)
7067 {
7068 	sched_update_worker(current);
7069 	lockdep_assert(fetch_and_set(current->sched_rt_mutex, 0));
7070 }
7071 
7072 /*
7073  * rt_mutex_setprio - set the current priority of a task
7074  * @p: task to boost
7075  * @pi_task: donor task
7076  *
7077  * This function changes the 'effective' priority of a task. It does
7078  * not touch ->normal_prio like __setscheduler().
7079  *
7080  * Used by the rt_mutex code to implement priority inheritance
7081  * logic. Call site only calls if the priority of the task changed.
7082  */
7083 void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
7084 {
7085 	int prio, oldprio, queued, running, queue_flag =
7086 		DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
7087 	const struct sched_class *prev_class, *next_class;
7088 	struct rq_flags rf;
7089 	struct rq *rq;
7090 
7091 	/* XXX used to be waiter->prio, not waiter->task->prio */
7092 	prio = __rt_effective_prio(pi_task, p->normal_prio);
7093 
7094 	/*
7095 	 * If nothing changed; bail early.
7096 	 */
7097 	if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio))
7098 		return;
7099 
7100 	rq = __task_rq_lock(p, &rf);
7101 	update_rq_clock(rq);
7102 	/*
7103 	 * Set under pi_lock && rq->lock, such that the value can be used under
7104 	 * either lock.
7105 	 *
7106 	 * Note that there is loads of tricky to make this pointer cache work
7107 	 * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to
7108 	 * ensure a task is de-boosted (pi_task is set to NULL) before the
7109 	 * task is allowed to run again (and can exit). This ensures the pointer
7110 	 * points to a blocked task -- which guarantees the task is present.
7111 	 */
7112 	p->pi_top_task = pi_task;
7113 
7114 	/*
7115 	 * For FIFO/RR we only need to set prio, if that matches we're done.
7116 	 */
7117 	if (prio == p->prio && !dl_prio(prio))
7118 		goto out_unlock;
7119 
7120 	/*
7121 	 * Idle task boosting is a no-no in general. There is one
7122 	 * exception, when PREEMPT_RT and NOHZ is active:
7123 	 *
7124 	 * The idle task calls get_next_timer_interrupt() and holds
7125 	 * the timer wheel base->lock on the CPU and another CPU wants
7126 	 * to access the timer (probably to cancel it). We can safely
7127 	 * ignore the boosting request, as the idle CPU runs this code
7128 	 * with interrupts disabled and will complete the lock
7129 	 * protected section without being interrupted. So there is no
7130 	 * real need to boost.
7131 	 */
7132 	if (unlikely(p == rq->idle)) {
7133 		WARN_ON(p != rq->curr);
7134 		WARN_ON(p->pi_blocked_on);
7135 		goto out_unlock;
7136 	}
7137 
7138 	trace_sched_pi_setprio(p, pi_task);
7139 	oldprio = p->prio;
7140 
7141 	if (oldprio == prio)
7142 		queue_flag &= ~DEQUEUE_MOVE;
7143 
7144 	prev_class = p->sched_class;
7145 	next_class = __setscheduler_class(p, prio);
7146 
7147 	if (prev_class != next_class && p->se.sched_delayed)
7148 		dequeue_task(rq, p, DEQUEUE_SLEEP | DEQUEUE_DELAYED | DEQUEUE_NOCLOCK);
7149 
7150 	queued = task_on_rq_queued(p);
7151 	running = task_current(rq, p);
7152 	if (queued)
7153 		dequeue_task(rq, p, queue_flag);
7154 	if (running)
7155 		put_prev_task(rq, p);
7156 
7157 	/*
7158 	 * Boosting condition are:
7159 	 * 1. -rt task is running and holds mutex A
7160 	 *      --> -dl task blocks on mutex A
7161 	 *
7162 	 * 2. -dl task is running and holds mutex A
7163 	 *      --> -dl task blocks on mutex A and could preempt the
7164 	 *          running task
7165 	 */
7166 	if (dl_prio(prio)) {
7167 		if (!dl_prio(p->normal_prio) ||
7168 		    (pi_task && dl_prio(pi_task->prio) &&
7169 		     dl_entity_preempt(&pi_task->dl, &p->dl))) {
7170 			p->dl.pi_se = pi_task->dl.pi_se;
7171 			queue_flag |= ENQUEUE_REPLENISH;
7172 		} else {
7173 			p->dl.pi_se = &p->dl;
7174 		}
7175 	} else if (rt_prio(prio)) {
7176 		if (dl_prio(oldprio))
7177 			p->dl.pi_se = &p->dl;
7178 		if (oldprio < prio)
7179 			queue_flag |= ENQUEUE_HEAD;
7180 	} else {
7181 		if (dl_prio(oldprio))
7182 			p->dl.pi_se = &p->dl;
7183 		if (rt_prio(oldprio))
7184 			p->rt.timeout = 0;
7185 	}
7186 
7187 	p->sched_class = next_class;
7188 	p->prio = prio;
7189 
7190 	check_class_changing(rq, p, prev_class);
7191 
7192 	if (queued)
7193 		enqueue_task(rq, p, queue_flag);
7194 	if (running)
7195 		set_next_task(rq, p);
7196 
7197 	check_class_changed(rq, p, prev_class, oldprio);
7198 out_unlock:
7199 	/* Avoid rq from going away on us: */
7200 	preempt_disable();
7201 
7202 	rq_unpin_lock(rq, &rf);
7203 	__balance_callbacks(rq);
7204 	raw_spin_rq_unlock(rq);
7205 
7206 	preempt_enable();
7207 }
7208 #endif
7209 
7210 #if !defined(CONFIG_PREEMPTION) || defined(CONFIG_PREEMPT_DYNAMIC)
7211 int __sched __cond_resched(void)
7212 {
7213 	if (should_resched(0)) {
7214 		preempt_schedule_common();
7215 		return 1;
7216 	}
7217 	/*
7218 	 * In preemptible kernels, ->rcu_read_lock_nesting tells the tick
7219 	 * whether the current CPU is in an RCU read-side critical section,
7220 	 * so the tick can report quiescent states even for CPUs looping
7221 	 * in kernel context.  In contrast, in non-preemptible kernels,
7222 	 * RCU readers leave no in-memory hints, which means that CPU-bound
7223 	 * processes executing in kernel context might never report an
7224 	 * RCU quiescent state.  Therefore, the following code causes
7225 	 * cond_resched() to report a quiescent state, but only when RCU
7226 	 * is in urgent need of one.
7227 	 */
7228 #ifndef CONFIG_PREEMPT_RCU
7229 	rcu_all_qs();
7230 #endif
7231 	return 0;
7232 }
7233 EXPORT_SYMBOL(__cond_resched);
7234 #endif
7235 
7236 #ifdef CONFIG_PREEMPT_DYNAMIC
7237 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
7238 #define cond_resched_dynamic_enabled	__cond_resched
7239 #define cond_resched_dynamic_disabled	((void *)&__static_call_return0)
7240 DEFINE_STATIC_CALL_RET0(cond_resched, __cond_resched);
7241 EXPORT_STATIC_CALL_TRAMP(cond_resched);
7242 
7243 #define might_resched_dynamic_enabled	__cond_resched
7244 #define might_resched_dynamic_disabled	((void *)&__static_call_return0)
7245 DEFINE_STATIC_CALL_RET0(might_resched, __cond_resched);
7246 EXPORT_STATIC_CALL_TRAMP(might_resched);
7247 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
7248 static DEFINE_STATIC_KEY_FALSE(sk_dynamic_cond_resched);
7249 int __sched dynamic_cond_resched(void)
7250 {
7251 	klp_sched_try_switch();
7252 	if (!static_branch_unlikely(&sk_dynamic_cond_resched))
7253 		return 0;
7254 	return __cond_resched();
7255 }
7256 EXPORT_SYMBOL(dynamic_cond_resched);
7257 
7258 static DEFINE_STATIC_KEY_FALSE(sk_dynamic_might_resched);
7259 int __sched dynamic_might_resched(void)
7260 {
7261 	if (!static_branch_unlikely(&sk_dynamic_might_resched))
7262 		return 0;
7263 	return __cond_resched();
7264 }
7265 EXPORT_SYMBOL(dynamic_might_resched);
7266 #endif
7267 #endif
7268 
7269 /*
7270  * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
7271  * call schedule, and on return reacquire the lock.
7272  *
7273  * This works OK both with and without CONFIG_PREEMPTION. We do strange low-level
7274  * operations here to prevent schedule() from being called twice (once via
7275  * spin_unlock(), once by hand).
7276  */
7277 int __cond_resched_lock(spinlock_t *lock)
7278 {
7279 	int resched = should_resched(PREEMPT_LOCK_OFFSET);
7280 	int ret = 0;
7281 
7282 	lockdep_assert_held(lock);
7283 
7284 	if (spin_needbreak(lock) || resched) {
7285 		spin_unlock(lock);
7286 		if (!_cond_resched())
7287 			cpu_relax();
7288 		ret = 1;
7289 		spin_lock(lock);
7290 	}
7291 	return ret;
7292 }
7293 EXPORT_SYMBOL(__cond_resched_lock);
7294 
7295 int __cond_resched_rwlock_read(rwlock_t *lock)
7296 {
7297 	int resched = should_resched(PREEMPT_LOCK_OFFSET);
7298 	int ret = 0;
7299 
7300 	lockdep_assert_held_read(lock);
7301 
7302 	if (rwlock_needbreak(lock) || resched) {
7303 		read_unlock(lock);
7304 		if (!_cond_resched())
7305 			cpu_relax();
7306 		ret = 1;
7307 		read_lock(lock);
7308 	}
7309 	return ret;
7310 }
7311 EXPORT_SYMBOL(__cond_resched_rwlock_read);
7312 
7313 int __cond_resched_rwlock_write(rwlock_t *lock)
7314 {
7315 	int resched = should_resched(PREEMPT_LOCK_OFFSET);
7316 	int ret = 0;
7317 
7318 	lockdep_assert_held_write(lock);
7319 
7320 	if (rwlock_needbreak(lock) || resched) {
7321 		write_unlock(lock);
7322 		if (!_cond_resched())
7323 			cpu_relax();
7324 		ret = 1;
7325 		write_lock(lock);
7326 	}
7327 	return ret;
7328 }
7329 EXPORT_SYMBOL(__cond_resched_rwlock_write);
7330 
7331 #ifdef CONFIG_PREEMPT_DYNAMIC
7332 
7333 #ifdef CONFIG_GENERIC_ENTRY
7334 #include <linux/entry-common.h>
7335 #endif
7336 
7337 /*
7338  * SC:cond_resched
7339  * SC:might_resched
7340  * SC:preempt_schedule
7341  * SC:preempt_schedule_notrace
7342  * SC:irqentry_exit_cond_resched
7343  *
7344  *
7345  * NONE:
7346  *   cond_resched               <- __cond_resched
7347  *   might_resched              <- RET0
7348  *   preempt_schedule           <- NOP
7349  *   preempt_schedule_notrace   <- NOP
7350  *   irqentry_exit_cond_resched <- NOP
7351  *
7352  * VOLUNTARY:
7353  *   cond_resched               <- __cond_resched
7354  *   might_resched              <- __cond_resched
7355  *   preempt_schedule           <- NOP
7356  *   preempt_schedule_notrace   <- NOP
7357  *   irqentry_exit_cond_resched <- NOP
7358  *
7359  * FULL:
7360  *   cond_resched               <- RET0
7361  *   might_resched              <- RET0
7362  *   preempt_schedule           <- preempt_schedule
7363  *   preempt_schedule_notrace   <- preempt_schedule_notrace
7364  *   irqentry_exit_cond_resched <- irqentry_exit_cond_resched
7365  */
7366 
7367 enum {
7368 	preempt_dynamic_undefined = -1,
7369 	preempt_dynamic_none,
7370 	preempt_dynamic_voluntary,
7371 	preempt_dynamic_full,
7372 };
7373 
7374 int preempt_dynamic_mode = preempt_dynamic_undefined;
7375 
7376 int sched_dynamic_mode(const char *str)
7377 {
7378 	if (!strcmp(str, "none"))
7379 		return preempt_dynamic_none;
7380 
7381 	if (!strcmp(str, "voluntary"))
7382 		return preempt_dynamic_voluntary;
7383 
7384 	if (!strcmp(str, "full"))
7385 		return preempt_dynamic_full;
7386 
7387 	return -EINVAL;
7388 }
7389 
7390 #if defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
7391 #define preempt_dynamic_enable(f)	static_call_update(f, f##_dynamic_enabled)
7392 #define preempt_dynamic_disable(f)	static_call_update(f, f##_dynamic_disabled)
7393 #elif defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
7394 #define preempt_dynamic_enable(f)	static_key_enable(&sk_dynamic_##f.key)
7395 #define preempt_dynamic_disable(f)	static_key_disable(&sk_dynamic_##f.key)
7396 #else
7397 #error "Unsupported PREEMPT_DYNAMIC mechanism"
7398 #endif
7399 
7400 static DEFINE_MUTEX(sched_dynamic_mutex);
7401 static bool klp_override;
7402 
7403 static void __sched_dynamic_update(int mode)
7404 {
7405 	/*
7406 	 * Avoid {NONE,VOLUNTARY} -> FULL transitions from ever ending up in
7407 	 * the ZERO state, which is invalid.
7408 	 */
7409 	if (!klp_override)
7410 		preempt_dynamic_enable(cond_resched);
7411 	preempt_dynamic_enable(might_resched);
7412 	preempt_dynamic_enable(preempt_schedule);
7413 	preempt_dynamic_enable(preempt_schedule_notrace);
7414 	preempt_dynamic_enable(irqentry_exit_cond_resched);
7415 
7416 	switch (mode) {
7417 	case preempt_dynamic_none:
7418 		if (!klp_override)
7419 			preempt_dynamic_enable(cond_resched);
7420 		preempt_dynamic_disable(might_resched);
7421 		preempt_dynamic_disable(preempt_schedule);
7422 		preempt_dynamic_disable(preempt_schedule_notrace);
7423 		preempt_dynamic_disable(irqentry_exit_cond_resched);
7424 		if (mode != preempt_dynamic_mode)
7425 			pr_info("Dynamic Preempt: none\n");
7426 		break;
7427 
7428 	case preempt_dynamic_voluntary:
7429 		if (!klp_override)
7430 			preempt_dynamic_enable(cond_resched);
7431 		preempt_dynamic_enable(might_resched);
7432 		preempt_dynamic_disable(preempt_schedule);
7433 		preempt_dynamic_disable(preempt_schedule_notrace);
7434 		preempt_dynamic_disable(irqentry_exit_cond_resched);
7435 		if (mode != preempt_dynamic_mode)
7436 			pr_info("Dynamic Preempt: voluntary\n");
7437 		break;
7438 
7439 	case preempt_dynamic_full:
7440 		if (!klp_override)
7441 			preempt_dynamic_disable(cond_resched);
7442 		preempt_dynamic_disable(might_resched);
7443 		preempt_dynamic_enable(preempt_schedule);
7444 		preempt_dynamic_enable(preempt_schedule_notrace);
7445 		preempt_dynamic_enable(irqentry_exit_cond_resched);
7446 		if (mode != preempt_dynamic_mode)
7447 			pr_info("Dynamic Preempt: full\n");
7448 		break;
7449 	}
7450 
7451 	preempt_dynamic_mode = mode;
7452 }
7453 
7454 void sched_dynamic_update(int mode)
7455 {
7456 	mutex_lock(&sched_dynamic_mutex);
7457 	__sched_dynamic_update(mode);
7458 	mutex_unlock(&sched_dynamic_mutex);
7459 }
7460 
7461 #ifdef CONFIG_HAVE_PREEMPT_DYNAMIC_CALL
7462 
7463 static int klp_cond_resched(void)
7464 {
7465 	__klp_sched_try_switch();
7466 	return __cond_resched();
7467 }
7468 
7469 void sched_dynamic_klp_enable(void)
7470 {
7471 	mutex_lock(&sched_dynamic_mutex);
7472 
7473 	klp_override = true;
7474 	static_call_update(cond_resched, klp_cond_resched);
7475 
7476 	mutex_unlock(&sched_dynamic_mutex);
7477 }
7478 
7479 void sched_dynamic_klp_disable(void)
7480 {
7481 	mutex_lock(&sched_dynamic_mutex);
7482 
7483 	klp_override = false;
7484 	__sched_dynamic_update(preempt_dynamic_mode);
7485 
7486 	mutex_unlock(&sched_dynamic_mutex);
7487 }
7488 
7489 #endif /* CONFIG_HAVE_PREEMPT_DYNAMIC_CALL */
7490 
7491 static int __init setup_preempt_mode(char *str)
7492 {
7493 	int mode = sched_dynamic_mode(str);
7494 	if (mode < 0) {
7495 		pr_warn("Dynamic Preempt: unsupported mode: %s\n", str);
7496 		return 0;
7497 	}
7498 
7499 	sched_dynamic_update(mode);
7500 	return 1;
7501 }
7502 __setup("preempt=", setup_preempt_mode);
7503 
7504 static void __init preempt_dynamic_init(void)
7505 {
7506 	if (preempt_dynamic_mode == preempt_dynamic_undefined) {
7507 		if (IS_ENABLED(CONFIG_PREEMPT_NONE)) {
7508 			sched_dynamic_update(preempt_dynamic_none);
7509 		} else if (IS_ENABLED(CONFIG_PREEMPT_VOLUNTARY)) {
7510 			sched_dynamic_update(preempt_dynamic_voluntary);
7511 		} else {
7512 			/* Default static call setting, nothing to do */
7513 			WARN_ON_ONCE(!IS_ENABLED(CONFIG_PREEMPT));
7514 			preempt_dynamic_mode = preempt_dynamic_full;
7515 			pr_info("Dynamic Preempt: full\n");
7516 		}
7517 	}
7518 }
7519 
7520 #define PREEMPT_MODEL_ACCESSOR(mode) \
7521 	bool preempt_model_##mode(void)						 \
7522 	{									 \
7523 		WARN_ON_ONCE(preempt_dynamic_mode == preempt_dynamic_undefined); \
7524 		return preempt_dynamic_mode == preempt_dynamic_##mode;		 \
7525 	}									 \
7526 	EXPORT_SYMBOL_GPL(preempt_model_##mode)
7527 
7528 PREEMPT_MODEL_ACCESSOR(none);
7529 PREEMPT_MODEL_ACCESSOR(voluntary);
7530 PREEMPT_MODEL_ACCESSOR(full);
7531 
7532 #else /* !CONFIG_PREEMPT_DYNAMIC: */
7533 
7534 static inline void preempt_dynamic_init(void) { }
7535 
7536 #endif /* CONFIG_PREEMPT_DYNAMIC */
7537 
7538 int io_schedule_prepare(void)
7539 {
7540 	int old_iowait = current->in_iowait;
7541 
7542 	current->in_iowait = 1;
7543 	blk_flush_plug(current->plug, true);
7544 	return old_iowait;
7545 }
7546 
7547 void io_schedule_finish(int token)
7548 {
7549 	current->in_iowait = token;
7550 }
7551 
7552 /*
7553  * This task is about to go to sleep on IO. Increment rq->nr_iowait so
7554  * that process accounting knows that this is a task in IO wait state.
7555  */
7556 long __sched io_schedule_timeout(long timeout)
7557 {
7558 	int token;
7559 	long ret;
7560 
7561 	token = io_schedule_prepare();
7562 	ret = schedule_timeout(timeout);
7563 	io_schedule_finish(token);
7564 
7565 	return ret;
7566 }
7567 EXPORT_SYMBOL(io_schedule_timeout);
7568 
7569 void __sched io_schedule(void)
7570 {
7571 	int token;
7572 
7573 	token = io_schedule_prepare();
7574 	schedule();
7575 	io_schedule_finish(token);
7576 }
7577 EXPORT_SYMBOL(io_schedule);
7578 
7579 void sched_show_task(struct task_struct *p)
7580 {
7581 	unsigned long free;
7582 	int ppid;
7583 
7584 	if (!try_get_task_stack(p))
7585 		return;
7586 
7587 	pr_info("task:%-15.15s state:%c", p->comm, task_state_to_char(p));
7588 
7589 	if (task_is_running(p))
7590 		pr_cont("  running task    ");
7591 	free = stack_not_used(p);
7592 	ppid = 0;
7593 	rcu_read_lock();
7594 	if (pid_alive(p))
7595 		ppid = task_pid_nr(rcu_dereference(p->real_parent));
7596 	rcu_read_unlock();
7597 	pr_cont(" stack:%-5lu pid:%-5d tgid:%-5d ppid:%-6d flags:0x%08lx\n",
7598 		free, task_pid_nr(p), task_tgid_nr(p),
7599 		ppid, read_task_thread_flags(p));
7600 
7601 	print_worker_info(KERN_INFO, p);
7602 	print_stop_info(KERN_INFO, p);
7603 	print_scx_info(KERN_INFO, p);
7604 	show_stack(p, NULL, KERN_INFO);
7605 	put_task_stack(p);
7606 }
7607 EXPORT_SYMBOL_GPL(sched_show_task);
7608 
7609 static inline bool
7610 state_filter_match(unsigned long state_filter, struct task_struct *p)
7611 {
7612 	unsigned int state = READ_ONCE(p->__state);
7613 
7614 	/* no filter, everything matches */
7615 	if (!state_filter)
7616 		return true;
7617 
7618 	/* filter, but doesn't match */
7619 	if (!(state & state_filter))
7620 		return false;
7621 
7622 	/*
7623 	 * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows
7624 	 * TASK_KILLABLE).
7625 	 */
7626 	if (state_filter == TASK_UNINTERRUPTIBLE && (state & TASK_NOLOAD))
7627 		return false;
7628 
7629 	return true;
7630 }
7631 
7632 
7633 void show_state_filter(unsigned int state_filter)
7634 {
7635 	struct task_struct *g, *p;
7636 
7637 	rcu_read_lock();
7638 	for_each_process_thread(g, p) {
7639 		/*
7640 		 * reset the NMI-timeout, listing all files on a slow
7641 		 * console might take a lot of time:
7642 		 * Also, reset softlockup watchdogs on all CPUs, because
7643 		 * another CPU might be blocked waiting for us to process
7644 		 * an IPI.
7645 		 */
7646 		touch_nmi_watchdog();
7647 		touch_all_softlockup_watchdogs();
7648 		if (state_filter_match(state_filter, p))
7649 			sched_show_task(p);
7650 	}
7651 
7652 #ifdef CONFIG_SCHED_DEBUG
7653 	if (!state_filter)
7654 		sysrq_sched_debug_show();
7655 #endif
7656 	rcu_read_unlock();
7657 	/*
7658 	 * Only show locks if all tasks are dumped:
7659 	 */
7660 	if (!state_filter)
7661 		debug_show_all_locks();
7662 }
7663 
7664 /**
7665  * init_idle - set up an idle thread for a given CPU
7666  * @idle: task in question
7667  * @cpu: CPU the idle task belongs to
7668  *
7669  * NOTE: this function does not set the idle thread's NEED_RESCHED
7670  * flag, to make booting more robust.
7671  */
7672 void __init init_idle(struct task_struct *idle, int cpu)
7673 {
7674 #ifdef CONFIG_SMP
7675 	struct affinity_context ac = (struct affinity_context) {
7676 		.new_mask  = cpumask_of(cpu),
7677 		.flags     = 0,
7678 	};
7679 #endif
7680 	struct rq *rq = cpu_rq(cpu);
7681 	unsigned long flags;
7682 
7683 	__sched_fork(0, idle);
7684 
7685 	raw_spin_lock_irqsave(&idle->pi_lock, flags);
7686 	raw_spin_rq_lock(rq);
7687 
7688 	idle->__state = TASK_RUNNING;
7689 	idle->se.exec_start = sched_clock();
7690 	/*
7691 	 * PF_KTHREAD should already be set at this point; regardless, make it
7692 	 * look like a proper per-CPU kthread.
7693 	 */
7694 	idle->flags |= PF_KTHREAD | PF_NO_SETAFFINITY;
7695 	kthread_set_per_cpu(idle, cpu);
7696 
7697 #ifdef CONFIG_SMP
7698 	/*
7699 	 * It's possible that init_idle() gets called multiple times on a task,
7700 	 * in that case do_set_cpus_allowed() will not do the right thing.
7701 	 *
7702 	 * And since this is boot we can forgo the serialization.
7703 	 */
7704 	set_cpus_allowed_common(idle, &ac);
7705 #endif
7706 	/*
7707 	 * We're having a chicken and egg problem, even though we are
7708 	 * holding rq->lock, the CPU isn't yet set to this CPU so the
7709 	 * lockdep check in task_group() will fail.
7710 	 *
7711 	 * Similar case to sched_fork(). / Alternatively we could
7712 	 * use task_rq_lock() here and obtain the other rq->lock.
7713 	 *
7714 	 * Silence PROVE_RCU
7715 	 */
7716 	rcu_read_lock();
7717 	__set_task_cpu(idle, cpu);
7718 	rcu_read_unlock();
7719 
7720 	rq->idle = idle;
7721 	rcu_assign_pointer(rq->curr, idle);
7722 	idle->on_rq = TASK_ON_RQ_QUEUED;
7723 #ifdef CONFIG_SMP
7724 	idle->on_cpu = 1;
7725 #endif
7726 	raw_spin_rq_unlock(rq);
7727 	raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
7728 
7729 	/* Set the preempt count _outside_ the spinlocks! */
7730 	init_idle_preempt_count(idle, cpu);
7731 
7732 	/*
7733 	 * The idle tasks have their own, simple scheduling class:
7734 	 */
7735 	idle->sched_class = &idle_sched_class;
7736 	ftrace_graph_init_idle_task(idle, cpu);
7737 	vtime_init_idle(idle, cpu);
7738 #ifdef CONFIG_SMP
7739 	sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
7740 #endif
7741 }
7742 
7743 #ifdef CONFIG_SMP
7744 
7745 int cpuset_cpumask_can_shrink(const struct cpumask *cur,
7746 			      const struct cpumask *trial)
7747 {
7748 	int ret = 1;
7749 
7750 	if (cpumask_empty(cur))
7751 		return ret;
7752 
7753 	ret = dl_cpuset_cpumask_can_shrink(cur, trial);
7754 
7755 	return ret;
7756 }
7757 
7758 int task_can_attach(struct task_struct *p)
7759 {
7760 	int ret = 0;
7761 
7762 	/*
7763 	 * Kthreads which disallow setaffinity shouldn't be moved
7764 	 * to a new cpuset; we don't want to change their CPU
7765 	 * affinity and isolating such threads by their set of
7766 	 * allowed nodes is unnecessary.  Thus, cpusets are not
7767 	 * applicable for such threads.  This prevents checking for
7768 	 * success of set_cpus_allowed_ptr() on all attached tasks
7769 	 * before cpus_mask may be changed.
7770 	 */
7771 	if (p->flags & PF_NO_SETAFFINITY)
7772 		ret = -EINVAL;
7773 
7774 	return ret;
7775 }
7776 
7777 bool sched_smp_initialized __read_mostly;
7778 
7779 #ifdef CONFIG_NUMA_BALANCING
7780 /* Migrate current task p to target_cpu */
7781 int migrate_task_to(struct task_struct *p, int target_cpu)
7782 {
7783 	struct migration_arg arg = { p, target_cpu };
7784 	int curr_cpu = task_cpu(p);
7785 
7786 	if (curr_cpu == target_cpu)
7787 		return 0;
7788 
7789 	if (!cpumask_test_cpu(target_cpu, p->cpus_ptr))
7790 		return -EINVAL;
7791 
7792 	/* TODO: This is not properly updating schedstats */
7793 
7794 	trace_sched_move_numa(p, curr_cpu, target_cpu);
7795 	return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
7796 }
7797 
7798 /*
7799  * Requeue a task on a given node and accurately track the number of NUMA
7800  * tasks on the runqueues
7801  */
7802 void sched_setnuma(struct task_struct *p, int nid)
7803 {
7804 	bool queued, running;
7805 	struct rq_flags rf;
7806 	struct rq *rq;
7807 
7808 	rq = task_rq_lock(p, &rf);
7809 	queued = task_on_rq_queued(p);
7810 	running = task_current(rq, p);
7811 
7812 	if (queued)
7813 		dequeue_task(rq, p, DEQUEUE_SAVE);
7814 	if (running)
7815 		put_prev_task(rq, p);
7816 
7817 	p->numa_preferred_nid = nid;
7818 
7819 	if (queued)
7820 		enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
7821 	if (running)
7822 		set_next_task(rq, p);
7823 	task_rq_unlock(rq, p, &rf);
7824 }
7825 #endif /* CONFIG_NUMA_BALANCING */
7826 
7827 #ifdef CONFIG_HOTPLUG_CPU
7828 /*
7829  * Ensure that the idle task is using init_mm right before its CPU goes
7830  * offline.
7831  */
7832 void idle_task_exit(void)
7833 {
7834 	struct mm_struct *mm = current->active_mm;
7835 
7836 	BUG_ON(cpu_online(smp_processor_id()));
7837 	BUG_ON(current != this_rq()->idle);
7838 
7839 	if (mm != &init_mm) {
7840 		switch_mm(mm, &init_mm, current);
7841 		finish_arch_post_lock_switch();
7842 	}
7843 
7844 	/* finish_cpu(), as ran on the BP, will clean up the active_mm state */
7845 }
7846 
7847 static int __balance_push_cpu_stop(void *arg)
7848 {
7849 	struct task_struct *p = arg;
7850 	struct rq *rq = this_rq();
7851 	struct rq_flags rf;
7852 	int cpu;
7853 
7854 	raw_spin_lock_irq(&p->pi_lock);
7855 	rq_lock(rq, &rf);
7856 
7857 	update_rq_clock(rq);
7858 
7859 	if (task_rq(p) == rq && task_on_rq_queued(p)) {
7860 		cpu = select_fallback_rq(rq->cpu, p);
7861 		rq = __migrate_task(rq, &rf, p, cpu);
7862 	}
7863 
7864 	rq_unlock(rq, &rf);
7865 	raw_spin_unlock_irq(&p->pi_lock);
7866 
7867 	put_task_struct(p);
7868 
7869 	return 0;
7870 }
7871 
7872 static DEFINE_PER_CPU(struct cpu_stop_work, push_work);
7873 
7874 /*
7875  * Ensure we only run per-cpu kthreads once the CPU goes !active.
7876  *
7877  * This is enabled below SCHED_AP_ACTIVE; when !cpu_active(), but only
7878  * effective when the hotplug motion is down.
7879  */
7880 static void balance_push(struct rq *rq)
7881 {
7882 	struct task_struct *push_task = rq->curr;
7883 
7884 	lockdep_assert_rq_held(rq);
7885 
7886 	/*
7887 	 * Ensure the thing is persistent until balance_push_set(.on = false);
7888 	 */
7889 	rq->balance_callback = &balance_push_callback;
7890 
7891 	/*
7892 	 * Only active while going offline and when invoked on the outgoing
7893 	 * CPU.
7894 	 */
7895 	if (!cpu_dying(rq->cpu) || rq != this_rq())
7896 		return;
7897 
7898 	/*
7899 	 * Both the cpu-hotplug and stop task are in this case and are
7900 	 * required to complete the hotplug process.
7901 	 */
7902 	if (kthread_is_per_cpu(push_task) ||
7903 	    is_migration_disabled(push_task)) {
7904 
7905 		/*
7906 		 * If this is the idle task on the outgoing CPU try to wake
7907 		 * up the hotplug control thread which might wait for the
7908 		 * last task to vanish. The rcuwait_active() check is
7909 		 * accurate here because the waiter is pinned on this CPU
7910 		 * and can't obviously be running in parallel.
7911 		 *
7912 		 * On RT kernels this also has to check whether there are
7913 		 * pinned and scheduled out tasks on the runqueue. They
7914 		 * need to leave the migrate disabled section first.
7915 		 */
7916 		if (!rq->nr_running && !rq_has_pinned_tasks(rq) &&
7917 		    rcuwait_active(&rq->hotplug_wait)) {
7918 			raw_spin_rq_unlock(rq);
7919 			rcuwait_wake_up(&rq->hotplug_wait);
7920 			raw_spin_rq_lock(rq);
7921 		}
7922 		return;
7923 	}
7924 
7925 	get_task_struct(push_task);
7926 	/*
7927 	 * Temporarily drop rq->lock such that we can wake-up the stop task.
7928 	 * Both preemption and IRQs are still disabled.
7929 	 */
7930 	preempt_disable();
7931 	raw_spin_rq_unlock(rq);
7932 	stop_one_cpu_nowait(rq->cpu, __balance_push_cpu_stop, push_task,
7933 			    this_cpu_ptr(&push_work));
7934 	preempt_enable();
7935 	/*
7936 	 * At this point need_resched() is true and we'll take the loop in
7937 	 * schedule(). The next pick is obviously going to be the stop task
7938 	 * which kthread_is_per_cpu() and will push this task away.
7939 	 */
7940 	raw_spin_rq_lock(rq);
7941 }
7942 
7943 static void balance_push_set(int cpu, bool on)
7944 {
7945 	struct rq *rq = cpu_rq(cpu);
7946 	struct rq_flags rf;
7947 
7948 	rq_lock_irqsave(rq, &rf);
7949 	if (on) {
7950 		WARN_ON_ONCE(rq->balance_callback);
7951 		rq->balance_callback = &balance_push_callback;
7952 	} else if (rq->balance_callback == &balance_push_callback) {
7953 		rq->balance_callback = NULL;
7954 	}
7955 	rq_unlock_irqrestore(rq, &rf);
7956 }
7957 
7958 /*
7959  * Invoked from a CPUs hotplug control thread after the CPU has been marked
7960  * inactive. All tasks which are not per CPU kernel threads are either
7961  * pushed off this CPU now via balance_push() or placed on a different CPU
7962  * during wakeup. Wait until the CPU is quiescent.
7963  */
7964 static void balance_hotplug_wait(void)
7965 {
7966 	struct rq *rq = this_rq();
7967 
7968 	rcuwait_wait_event(&rq->hotplug_wait,
7969 			   rq->nr_running == 1 && !rq_has_pinned_tasks(rq),
7970 			   TASK_UNINTERRUPTIBLE);
7971 }
7972 
7973 #else
7974 
7975 static inline void balance_push(struct rq *rq)
7976 {
7977 }
7978 
7979 static inline void balance_push_set(int cpu, bool on)
7980 {
7981 }
7982 
7983 static inline void balance_hotplug_wait(void)
7984 {
7985 }
7986 
7987 #endif /* CONFIG_HOTPLUG_CPU */
7988 
7989 void set_rq_online(struct rq *rq)
7990 {
7991 	if (!rq->online) {
7992 		const struct sched_class *class;
7993 
7994 		cpumask_set_cpu(rq->cpu, rq->rd->online);
7995 		rq->online = 1;
7996 
7997 		for_each_class(class) {
7998 			if (class->rq_online)
7999 				class->rq_online(rq);
8000 		}
8001 	}
8002 }
8003 
8004 void set_rq_offline(struct rq *rq)
8005 {
8006 	if (rq->online) {
8007 		const struct sched_class *class;
8008 
8009 		update_rq_clock(rq);
8010 		for_each_class(class) {
8011 			if (class->rq_offline)
8012 				class->rq_offline(rq);
8013 		}
8014 
8015 		cpumask_clear_cpu(rq->cpu, rq->rd->online);
8016 		rq->online = 0;
8017 	}
8018 }
8019 
8020 static inline void sched_set_rq_online(struct rq *rq, int cpu)
8021 {
8022 	struct rq_flags rf;
8023 
8024 	rq_lock_irqsave(rq, &rf);
8025 	if (rq->rd) {
8026 		BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
8027 		set_rq_online(rq);
8028 	}
8029 	rq_unlock_irqrestore(rq, &rf);
8030 }
8031 
8032 static inline void sched_set_rq_offline(struct rq *rq, int cpu)
8033 {
8034 	struct rq_flags rf;
8035 
8036 	rq_lock_irqsave(rq, &rf);
8037 	if (rq->rd) {
8038 		BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
8039 		set_rq_offline(rq);
8040 	}
8041 	rq_unlock_irqrestore(rq, &rf);
8042 }
8043 
8044 /*
8045  * used to mark begin/end of suspend/resume:
8046  */
8047 static int num_cpus_frozen;
8048 
8049 /*
8050  * Update cpusets according to cpu_active mask.  If cpusets are
8051  * disabled, cpuset_update_active_cpus() becomes a simple wrapper
8052  * around partition_sched_domains().
8053  *
8054  * If we come here as part of a suspend/resume, don't touch cpusets because we
8055  * want to restore it back to its original state upon resume anyway.
8056  */
8057 static void cpuset_cpu_active(void)
8058 {
8059 	if (cpuhp_tasks_frozen) {
8060 		/*
8061 		 * num_cpus_frozen tracks how many CPUs are involved in suspend
8062 		 * resume sequence. As long as this is not the last online
8063 		 * operation in the resume sequence, just build a single sched
8064 		 * domain, ignoring cpusets.
8065 		 */
8066 		partition_sched_domains(1, NULL, NULL);
8067 		if (--num_cpus_frozen)
8068 			return;
8069 		/*
8070 		 * This is the last CPU online operation. So fall through and
8071 		 * restore the original sched domains by considering the
8072 		 * cpuset configurations.
8073 		 */
8074 		cpuset_force_rebuild();
8075 	}
8076 	cpuset_update_active_cpus();
8077 }
8078 
8079 static int cpuset_cpu_inactive(unsigned int cpu)
8080 {
8081 	if (!cpuhp_tasks_frozen) {
8082 		int ret = dl_bw_check_overflow(cpu);
8083 
8084 		if (ret)
8085 			return ret;
8086 		cpuset_update_active_cpus();
8087 	} else {
8088 		num_cpus_frozen++;
8089 		partition_sched_domains(1, NULL, NULL);
8090 	}
8091 	return 0;
8092 }
8093 
8094 static inline void sched_smt_present_inc(int cpu)
8095 {
8096 #ifdef CONFIG_SCHED_SMT
8097 	if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
8098 		static_branch_inc_cpuslocked(&sched_smt_present);
8099 #endif
8100 }
8101 
8102 static inline void sched_smt_present_dec(int cpu)
8103 {
8104 #ifdef CONFIG_SCHED_SMT
8105 	if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
8106 		static_branch_dec_cpuslocked(&sched_smt_present);
8107 #endif
8108 }
8109 
8110 int sched_cpu_activate(unsigned int cpu)
8111 {
8112 	struct rq *rq = cpu_rq(cpu);
8113 
8114 	/*
8115 	 * Clear the balance_push callback and prepare to schedule
8116 	 * regular tasks.
8117 	 */
8118 	balance_push_set(cpu, false);
8119 
8120 	/*
8121 	 * When going up, increment the number of cores with SMT present.
8122 	 */
8123 	sched_smt_present_inc(cpu);
8124 	set_cpu_active(cpu, true);
8125 
8126 	if (sched_smp_initialized) {
8127 		sched_update_numa(cpu, true);
8128 		sched_domains_numa_masks_set(cpu);
8129 		cpuset_cpu_active();
8130 	}
8131 
8132 	scx_rq_activate(rq);
8133 
8134 	/*
8135 	 * Put the rq online, if not already. This happens:
8136 	 *
8137 	 * 1) In the early boot process, because we build the real domains
8138 	 *    after all CPUs have been brought up.
8139 	 *
8140 	 * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
8141 	 *    domains.
8142 	 */
8143 	sched_set_rq_online(rq, cpu);
8144 
8145 	return 0;
8146 }
8147 
8148 int sched_cpu_deactivate(unsigned int cpu)
8149 {
8150 	struct rq *rq = cpu_rq(cpu);
8151 	int ret;
8152 
8153 	/*
8154 	 * Remove CPU from nohz.idle_cpus_mask to prevent participating in
8155 	 * load balancing when not active
8156 	 */
8157 	nohz_balance_exit_idle(rq);
8158 
8159 	set_cpu_active(cpu, false);
8160 
8161 	/*
8162 	 * From this point forward, this CPU will refuse to run any task that
8163 	 * is not: migrate_disable() or KTHREAD_IS_PER_CPU, and will actively
8164 	 * push those tasks away until this gets cleared, see
8165 	 * sched_cpu_dying().
8166 	 */
8167 	balance_push_set(cpu, true);
8168 
8169 	/*
8170 	 * We've cleared cpu_active_mask / set balance_push, wait for all
8171 	 * preempt-disabled and RCU users of this state to go away such that
8172 	 * all new such users will observe it.
8173 	 *
8174 	 * Specifically, we rely on ttwu to no longer target this CPU, see
8175 	 * ttwu_queue_cond() and is_cpu_allowed().
8176 	 *
8177 	 * Do sync before park smpboot threads to take care the RCU boost case.
8178 	 */
8179 	synchronize_rcu();
8180 
8181 	sched_set_rq_offline(rq, cpu);
8182 
8183 	scx_rq_deactivate(rq);
8184 
8185 	/*
8186 	 * When going down, decrement the number of cores with SMT present.
8187 	 */
8188 	sched_smt_present_dec(cpu);
8189 
8190 #ifdef CONFIG_SCHED_SMT
8191 	sched_core_cpu_deactivate(cpu);
8192 #endif
8193 
8194 	if (!sched_smp_initialized)
8195 		return 0;
8196 
8197 	sched_update_numa(cpu, false);
8198 	ret = cpuset_cpu_inactive(cpu);
8199 	if (ret) {
8200 		sched_smt_present_inc(cpu);
8201 		sched_set_rq_online(rq, cpu);
8202 		balance_push_set(cpu, false);
8203 		set_cpu_active(cpu, true);
8204 		sched_update_numa(cpu, true);
8205 		return ret;
8206 	}
8207 	sched_domains_numa_masks_clear(cpu);
8208 	return 0;
8209 }
8210 
8211 static void sched_rq_cpu_starting(unsigned int cpu)
8212 {
8213 	struct rq *rq = cpu_rq(cpu);
8214 
8215 	rq->calc_load_update = calc_load_update;
8216 	update_max_interval();
8217 }
8218 
8219 int sched_cpu_starting(unsigned int cpu)
8220 {
8221 	sched_core_cpu_starting(cpu);
8222 	sched_rq_cpu_starting(cpu);
8223 	sched_tick_start(cpu);
8224 	return 0;
8225 }
8226 
8227 #ifdef CONFIG_HOTPLUG_CPU
8228 
8229 /*
8230  * Invoked immediately before the stopper thread is invoked to bring the
8231  * CPU down completely. At this point all per CPU kthreads except the
8232  * hotplug thread (current) and the stopper thread (inactive) have been
8233  * either parked or have been unbound from the outgoing CPU. Ensure that
8234  * any of those which might be on the way out are gone.
8235  *
8236  * If after this point a bound task is being woken on this CPU then the
8237  * responsible hotplug callback has failed to do it's job.
8238  * sched_cpu_dying() will catch it with the appropriate fireworks.
8239  */
8240 int sched_cpu_wait_empty(unsigned int cpu)
8241 {
8242 	balance_hotplug_wait();
8243 	return 0;
8244 }
8245 
8246 /*
8247  * Since this CPU is going 'away' for a while, fold any nr_active delta we
8248  * might have. Called from the CPU stopper task after ensuring that the
8249  * stopper is the last running task on the CPU, so nr_active count is
8250  * stable. We need to take the tear-down thread which is calling this into
8251  * account, so we hand in adjust = 1 to the load calculation.
8252  *
8253  * Also see the comment "Global load-average calculations".
8254  */
8255 static void calc_load_migrate(struct rq *rq)
8256 {
8257 	long delta = calc_load_fold_active(rq, 1);
8258 
8259 	if (delta)
8260 		atomic_long_add(delta, &calc_load_tasks);
8261 }
8262 
8263 static void dump_rq_tasks(struct rq *rq, const char *loglvl)
8264 {
8265 	struct task_struct *g, *p;
8266 	int cpu = cpu_of(rq);
8267 
8268 	lockdep_assert_rq_held(rq);
8269 
8270 	printk("%sCPU%d enqueued tasks (%u total):\n", loglvl, cpu, rq->nr_running);
8271 	for_each_process_thread(g, p) {
8272 		if (task_cpu(p) != cpu)
8273 			continue;
8274 
8275 		if (!task_on_rq_queued(p))
8276 			continue;
8277 
8278 		printk("%s\tpid: %d, name: %s\n", loglvl, p->pid, p->comm);
8279 	}
8280 }
8281 
8282 int sched_cpu_dying(unsigned int cpu)
8283 {
8284 	struct rq *rq = cpu_rq(cpu);
8285 	struct rq_flags rf;
8286 
8287 	/* Handle pending wakeups and then migrate everything off */
8288 	sched_tick_stop(cpu);
8289 
8290 	rq_lock_irqsave(rq, &rf);
8291 	if (rq->nr_running != 1 || rq_has_pinned_tasks(rq)) {
8292 		WARN(true, "Dying CPU not properly vacated!");
8293 		dump_rq_tasks(rq, KERN_WARNING);
8294 	}
8295 	rq_unlock_irqrestore(rq, &rf);
8296 
8297 	calc_load_migrate(rq);
8298 	update_max_interval();
8299 	hrtick_clear(rq);
8300 	sched_core_cpu_dying(cpu);
8301 	return 0;
8302 }
8303 #endif
8304 
8305 void __init sched_init_smp(void)
8306 {
8307 	sched_init_numa(NUMA_NO_NODE);
8308 
8309 	/*
8310 	 * There's no userspace yet to cause hotplug operations; hence all the
8311 	 * CPU masks are stable and all blatant races in the below code cannot
8312 	 * happen.
8313 	 */
8314 	mutex_lock(&sched_domains_mutex);
8315 	sched_init_domains(cpu_active_mask);
8316 	mutex_unlock(&sched_domains_mutex);
8317 
8318 	/* Move init over to a non-isolated CPU */
8319 	if (set_cpus_allowed_ptr(current, housekeeping_cpumask(HK_TYPE_DOMAIN)) < 0)
8320 		BUG();
8321 	current->flags &= ~PF_NO_SETAFFINITY;
8322 	sched_init_granularity();
8323 
8324 	init_sched_rt_class();
8325 	init_sched_dl_class();
8326 
8327 	sched_smp_initialized = true;
8328 }
8329 
8330 static int __init migration_init(void)
8331 {
8332 	sched_cpu_starting(smp_processor_id());
8333 	return 0;
8334 }
8335 early_initcall(migration_init);
8336 
8337 #else
8338 void __init sched_init_smp(void)
8339 {
8340 	sched_init_granularity();
8341 }
8342 #endif /* CONFIG_SMP */
8343 
8344 int in_sched_functions(unsigned long addr)
8345 {
8346 	return in_lock_functions(addr) ||
8347 		(addr >= (unsigned long)__sched_text_start
8348 		&& addr < (unsigned long)__sched_text_end);
8349 }
8350 
8351 #ifdef CONFIG_CGROUP_SCHED
8352 /*
8353  * Default task group.
8354  * Every task in system belongs to this group at bootup.
8355  */
8356 struct task_group root_task_group;
8357 LIST_HEAD(task_groups);
8358 
8359 /* Cacheline aligned slab cache for task_group */
8360 static struct kmem_cache *task_group_cache __ro_after_init;
8361 #endif
8362 
8363 void __init sched_init(void)
8364 {
8365 	unsigned long ptr = 0;
8366 	int i;
8367 
8368 	/* Make sure the linker didn't screw up */
8369 #ifdef CONFIG_SMP
8370 	BUG_ON(!sched_class_above(&stop_sched_class, &dl_sched_class));
8371 #endif
8372 	BUG_ON(!sched_class_above(&dl_sched_class, &rt_sched_class));
8373 	BUG_ON(!sched_class_above(&rt_sched_class, &fair_sched_class));
8374 	BUG_ON(!sched_class_above(&fair_sched_class, &idle_sched_class));
8375 #ifdef CONFIG_SCHED_CLASS_EXT
8376 	BUG_ON(!sched_class_above(&fair_sched_class, &ext_sched_class));
8377 	BUG_ON(!sched_class_above(&ext_sched_class, &idle_sched_class));
8378 #endif
8379 
8380 	wait_bit_init();
8381 
8382 #ifdef CONFIG_FAIR_GROUP_SCHED
8383 	ptr += 2 * nr_cpu_ids * sizeof(void **);
8384 #endif
8385 #ifdef CONFIG_RT_GROUP_SCHED
8386 	ptr += 2 * nr_cpu_ids * sizeof(void **);
8387 #endif
8388 	if (ptr) {
8389 		ptr = (unsigned long)kzalloc(ptr, GFP_NOWAIT);
8390 
8391 #ifdef CONFIG_FAIR_GROUP_SCHED
8392 		root_task_group.se = (struct sched_entity **)ptr;
8393 		ptr += nr_cpu_ids * sizeof(void **);
8394 
8395 		root_task_group.cfs_rq = (struct cfs_rq **)ptr;
8396 		ptr += nr_cpu_ids * sizeof(void **);
8397 
8398 		root_task_group.shares = ROOT_TASK_GROUP_LOAD;
8399 		init_cfs_bandwidth(&root_task_group.cfs_bandwidth, NULL);
8400 #endif /* CONFIG_FAIR_GROUP_SCHED */
8401 #ifdef CONFIG_EXT_GROUP_SCHED
8402 		root_task_group.scx_weight = CGROUP_WEIGHT_DFL;
8403 #endif /* CONFIG_EXT_GROUP_SCHED */
8404 #ifdef CONFIG_RT_GROUP_SCHED
8405 		root_task_group.rt_se = (struct sched_rt_entity **)ptr;
8406 		ptr += nr_cpu_ids * sizeof(void **);
8407 
8408 		root_task_group.rt_rq = (struct rt_rq **)ptr;
8409 		ptr += nr_cpu_ids * sizeof(void **);
8410 
8411 #endif /* CONFIG_RT_GROUP_SCHED */
8412 	}
8413 
8414 #ifdef CONFIG_SMP
8415 	init_defrootdomain();
8416 #endif
8417 
8418 #ifdef CONFIG_RT_GROUP_SCHED
8419 	init_rt_bandwidth(&root_task_group.rt_bandwidth,
8420 			global_rt_period(), global_rt_runtime());
8421 #endif /* CONFIG_RT_GROUP_SCHED */
8422 
8423 #ifdef CONFIG_CGROUP_SCHED
8424 	task_group_cache = KMEM_CACHE(task_group, 0);
8425 
8426 	list_add(&root_task_group.list, &task_groups);
8427 	INIT_LIST_HEAD(&root_task_group.children);
8428 	INIT_LIST_HEAD(&root_task_group.siblings);
8429 	autogroup_init(&init_task);
8430 #endif /* CONFIG_CGROUP_SCHED */
8431 
8432 	for_each_possible_cpu(i) {
8433 		struct rq *rq;
8434 
8435 		rq = cpu_rq(i);
8436 		raw_spin_lock_init(&rq->__lock);
8437 		rq->nr_running = 0;
8438 		rq->calc_load_active = 0;
8439 		rq->calc_load_update = jiffies + LOAD_FREQ;
8440 		init_cfs_rq(&rq->cfs);
8441 		init_rt_rq(&rq->rt);
8442 		init_dl_rq(&rq->dl);
8443 #ifdef CONFIG_FAIR_GROUP_SCHED
8444 		INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
8445 		rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
8446 		/*
8447 		 * How much CPU bandwidth does root_task_group get?
8448 		 *
8449 		 * In case of task-groups formed through the cgroup filesystem, it
8450 		 * gets 100% of the CPU resources in the system. This overall
8451 		 * system CPU resource is divided among the tasks of
8452 		 * root_task_group and its child task-groups in a fair manner,
8453 		 * based on each entity's (task or task-group's) weight
8454 		 * (se->load.weight).
8455 		 *
8456 		 * In other words, if root_task_group has 10 tasks of weight
8457 		 * 1024) and two child groups A0 and A1 (of weight 1024 each),
8458 		 * then A0's share of the CPU resource is:
8459 		 *
8460 		 *	A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
8461 		 *
8462 		 * We achieve this by letting root_task_group's tasks sit
8463 		 * directly in rq->cfs (i.e root_task_group->se[] = NULL).
8464 		 */
8465 		init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
8466 #endif /* CONFIG_FAIR_GROUP_SCHED */
8467 
8468 #ifdef CONFIG_RT_GROUP_SCHED
8469 		/*
8470 		 * This is required for init cpu because rt.c:__enable_runtime()
8471 		 * starts working after scheduler_running, which is not the case
8472 		 * yet.
8473 		 */
8474 		rq->rt.rt_runtime = global_rt_runtime();
8475 		init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
8476 #endif
8477 #ifdef CONFIG_SMP
8478 		rq->sd = NULL;
8479 		rq->rd = NULL;
8480 		rq->cpu_capacity = SCHED_CAPACITY_SCALE;
8481 		rq->balance_callback = &balance_push_callback;
8482 		rq->active_balance = 0;
8483 		rq->next_balance = jiffies;
8484 		rq->push_cpu = 0;
8485 		rq->cpu = i;
8486 		rq->online = 0;
8487 		rq->idle_stamp = 0;
8488 		rq->avg_idle = 2*sysctl_sched_migration_cost;
8489 		rq->max_idle_balance_cost = sysctl_sched_migration_cost;
8490 
8491 		INIT_LIST_HEAD(&rq->cfs_tasks);
8492 
8493 		rq_attach_root(rq, &def_root_domain);
8494 #ifdef CONFIG_NO_HZ_COMMON
8495 		rq->last_blocked_load_update_tick = jiffies;
8496 		atomic_set(&rq->nohz_flags, 0);
8497 
8498 		INIT_CSD(&rq->nohz_csd, nohz_csd_func, rq);
8499 #endif
8500 #ifdef CONFIG_HOTPLUG_CPU
8501 		rcuwait_init(&rq->hotplug_wait);
8502 #endif
8503 #endif /* CONFIG_SMP */
8504 		hrtick_rq_init(rq);
8505 		atomic_set(&rq->nr_iowait, 0);
8506 		fair_server_init(rq);
8507 
8508 #ifdef CONFIG_SCHED_CORE
8509 		rq->core = rq;
8510 		rq->core_pick = NULL;
8511 		rq->core_dl_server = NULL;
8512 		rq->core_enabled = 0;
8513 		rq->core_tree = RB_ROOT;
8514 		rq->core_forceidle_count = 0;
8515 		rq->core_forceidle_occupation = 0;
8516 		rq->core_forceidle_start = 0;
8517 
8518 		rq->core_cookie = 0UL;
8519 #endif
8520 		zalloc_cpumask_var_node(&rq->scratch_mask, GFP_KERNEL, cpu_to_node(i));
8521 	}
8522 
8523 	set_load_weight(&init_task, false);
8524 	init_task.se.slice = sysctl_sched_base_slice,
8525 
8526 	/*
8527 	 * The boot idle thread does lazy MMU switching as well:
8528 	 */
8529 	mmgrab_lazy_tlb(&init_mm);
8530 	enter_lazy_tlb(&init_mm, current);
8531 
8532 	/*
8533 	 * The idle task doesn't need the kthread struct to function, but it
8534 	 * is dressed up as a per-CPU kthread and thus needs to play the part
8535 	 * if we want to avoid special-casing it in code that deals with per-CPU
8536 	 * kthreads.
8537 	 */
8538 	WARN_ON(!set_kthread_struct(current));
8539 
8540 	/*
8541 	 * Make us the idle thread. Technically, schedule() should not be
8542 	 * called from this thread, however somewhere below it might be,
8543 	 * but because we are the idle thread, we just pick up running again
8544 	 * when this runqueue becomes "idle".
8545 	 */
8546 	init_idle(current, smp_processor_id());
8547 
8548 	calc_load_update = jiffies + LOAD_FREQ;
8549 
8550 #ifdef CONFIG_SMP
8551 	idle_thread_set_boot_cpu();
8552 	balance_push_set(smp_processor_id(), false);
8553 #endif
8554 	init_sched_fair_class();
8555 	init_sched_ext_class();
8556 
8557 	psi_init();
8558 
8559 	init_uclamp();
8560 
8561 	preempt_dynamic_init();
8562 
8563 	scheduler_running = 1;
8564 }
8565 
8566 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
8567 
8568 void __might_sleep(const char *file, int line)
8569 {
8570 	unsigned int state = get_current_state();
8571 	/*
8572 	 * Blocking primitives will set (and therefore destroy) current->state,
8573 	 * since we will exit with TASK_RUNNING make sure we enter with it,
8574 	 * otherwise we will destroy state.
8575 	 */
8576 	WARN_ONCE(state != TASK_RUNNING && current->task_state_change,
8577 			"do not call blocking ops when !TASK_RUNNING; "
8578 			"state=%x set at [<%p>] %pS\n", state,
8579 			(void *)current->task_state_change,
8580 			(void *)current->task_state_change);
8581 
8582 	__might_resched(file, line, 0);
8583 }
8584 EXPORT_SYMBOL(__might_sleep);
8585 
8586 static void print_preempt_disable_ip(int preempt_offset, unsigned long ip)
8587 {
8588 	if (!IS_ENABLED(CONFIG_DEBUG_PREEMPT))
8589 		return;
8590 
8591 	if (preempt_count() == preempt_offset)
8592 		return;
8593 
8594 	pr_err("Preemption disabled at:");
8595 	print_ip_sym(KERN_ERR, ip);
8596 }
8597 
8598 static inline bool resched_offsets_ok(unsigned int offsets)
8599 {
8600 	unsigned int nested = preempt_count();
8601 
8602 	nested += rcu_preempt_depth() << MIGHT_RESCHED_RCU_SHIFT;
8603 
8604 	return nested == offsets;
8605 }
8606 
8607 void __might_resched(const char *file, int line, unsigned int offsets)
8608 {
8609 	/* Ratelimiting timestamp: */
8610 	static unsigned long prev_jiffy;
8611 
8612 	unsigned long preempt_disable_ip;
8613 
8614 	/* WARN_ON_ONCE() by default, no rate limit required: */
8615 	rcu_sleep_check();
8616 
8617 	if ((resched_offsets_ok(offsets) && !irqs_disabled() &&
8618 	     !is_idle_task(current) && !current->non_block_count) ||
8619 	    system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
8620 	    oops_in_progress)
8621 		return;
8622 
8623 	if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8624 		return;
8625 	prev_jiffy = jiffies;
8626 
8627 	/* Save this before calling printk(), since that will clobber it: */
8628 	preempt_disable_ip = get_preempt_disable_ip(current);
8629 
8630 	pr_err("BUG: sleeping function called from invalid context at %s:%d\n",
8631 	       file, line);
8632 	pr_err("in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n",
8633 	       in_atomic(), irqs_disabled(), current->non_block_count,
8634 	       current->pid, current->comm);
8635 	pr_err("preempt_count: %x, expected: %x\n", preempt_count(),
8636 	       offsets & MIGHT_RESCHED_PREEMPT_MASK);
8637 
8638 	if (IS_ENABLED(CONFIG_PREEMPT_RCU)) {
8639 		pr_err("RCU nest depth: %d, expected: %u\n",
8640 		       rcu_preempt_depth(), offsets >> MIGHT_RESCHED_RCU_SHIFT);
8641 	}
8642 
8643 	if (task_stack_end_corrupted(current))
8644 		pr_emerg("Thread overran stack, or stack corrupted\n");
8645 
8646 	debug_show_held_locks(current);
8647 	if (irqs_disabled())
8648 		print_irqtrace_events(current);
8649 
8650 	print_preempt_disable_ip(offsets & MIGHT_RESCHED_PREEMPT_MASK,
8651 				 preempt_disable_ip);
8652 
8653 	dump_stack();
8654 	add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
8655 }
8656 EXPORT_SYMBOL(__might_resched);
8657 
8658 void __cant_sleep(const char *file, int line, int preempt_offset)
8659 {
8660 	static unsigned long prev_jiffy;
8661 
8662 	if (irqs_disabled())
8663 		return;
8664 
8665 	if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
8666 		return;
8667 
8668 	if (preempt_count() > preempt_offset)
8669 		return;
8670 
8671 	if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8672 		return;
8673 	prev_jiffy = jiffies;
8674 
8675 	printk(KERN_ERR "BUG: assuming atomic context at %s:%d\n", file, line);
8676 	printk(KERN_ERR "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
8677 			in_atomic(), irqs_disabled(),
8678 			current->pid, current->comm);
8679 
8680 	debug_show_held_locks(current);
8681 	dump_stack();
8682 	add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
8683 }
8684 EXPORT_SYMBOL_GPL(__cant_sleep);
8685 
8686 #ifdef CONFIG_SMP
8687 void __cant_migrate(const char *file, int line)
8688 {
8689 	static unsigned long prev_jiffy;
8690 
8691 	if (irqs_disabled())
8692 		return;
8693 
8694 	if (is_migration_disabled(current))
8695 		return;
8696 
8697 	if (!IS_ENABLED(CONFIG_PREEMPT_COUNT))
8698 		return;
8699 
8700 	if (preempt_count() > 0)
8701 		return;
8702 
8703 	if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
8704 		return;
8705 	prev_jiffy = jiffies;
8706 
8707 	pr_err("BUG: assuming non migratable context at %s:%d\n", file, line);
8708 	pr_err("in_atomic(): %d, irqs_disabled(): %d, migration_disabled() %u pid: %d, name: %s\n",
8709 	       in_atomic(), irqs_disabled(), is_migration_disabled(current),
8710 	       current->pid, current->comm);
8711 
8712 	debug_show_held_locks(current);
8713 	dump_stack();
8714 	add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
8715 }
8716 EXPORT_SYMBOL_GPL(__cant_migrate);
8717 #endif
8718 #endif
8719 
8720 #ifdef CONFIG_MAGIC_SYSRQ
8721 void normalize_rt_tasks(void)
8722 {
8723 	struct task_struct *g, *p;
8724 	struct sched_attr attr = {
8725 		.sched_policy = SCHED_NORMAL,
8726 	};
8727 
8728 	read_lock(&tasklist_lock);
8729 	for_each_process_thread(g, p) {
8730 		/*
8731 		 * Only normalize user tasks:
8732 		 */
8733 		if (p->flags & PF_KTHREAD)
8734 			continue;
8735 
8736 		p->se.exec_start = 0;
8737 		schedstat_set(p->stats.wait_start,  0);
8738 		schedstat_set(p->stats.sleep_start, 0);
8739 		schedstat_set(p->stats.block_start, 0);
8740 
8741 		if (!rt_or_dl_task(p)) {
8742 			/*
8743 			 * Renice negative nice level userspace
8744 			 * tasks back to 0:
8745 			 */
8746 			if (task_nice(p) < 0)
8747 				set_user_nice(p, 0);
8748 			continue;
8749 		}
8750 
8751 		__sched_setscheduler(p, &attr, false, false);
8752 	}
8753 	read_unlock(&tasklist_lock);
8754 }
8755 
8756 #endif /* CONFIG_MAGIC_SYSRQ */
8757 
8758 #if defined(CONFIG_KGDB_KDB)
8759 /*
8760  * These functions are only useful for KDB.
8761  *
8762  * They can only be called when the whole system has been
8763  * stopped - every CPU needs to be quiescent, and no scheduling
8764  * activity can take place. Using them for anything else would
8765  * be a serious bug, and as a result, they aren't even visible
8766  * under any other configuration.
8767  */
8768 
8769 /**
8770  * curr_task - return the current task for a given CPU.
8771  * @cpu: the processor in question.
8772  *
8773  * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
8774  *
8775  * Return: The current task for @cpu.
8776  */
8777 struct task_struct *curr_task(int cpu)
8778 {
8779 	return cpu_curr(cpu);
8780 }
8781 
8782 #endif /* defined(CONFIG_KGDB_KDB) */
8783 
8784 #ifdef CONFIG_CGROUP_SCHED
8785 /* task_group_lock serializes the addition/removal of task groups */
8786 static DEFINE_SPINLOCK(task_group_lock);
8787 
8788 static inline void alloc_uclamp_sched_group(struct task_group *tg,
8789 					    struct task_group *parent)
8790 {
8791 #ifdef CONFIG_UCLAMP_TASK_GROUP
8792 	enum uclamp_id clamp_id;
8793 
8794 	for_each_clamp_id(clamp_id) {
8795 		uclamp_se_set(&tg->uclamp_req[clamp_id],
8796 			      uclamp_none(clamp_id), false);
8797 		tg->uclamp[clamp_id] = parent->uclamp[clamp_id];
8798 	}
8799 #endif
8800 }
8801 
8802 static void sched_free_group(struct task_group *tg)
8803 {
8804 	free_fair_sched_group(tg);
8805 	free_rt_sched_group(tg);
8806 	autogroup_free(tg);
8807 	kmem_cache_free(task_group_cache, tg);
8808 }
8809 
8810 static void sched_free_group_rcu(struct rcu_head *rcu)
8811 {
8812 	sched_free_group(container_of(rcu, struct task_group, rcu));
8813 }
8814 
8815 static void sched_unregister_group(struct task_group *tg)
8816 {
8817 	unregister_fair_sched_group(tg);
8818 	unregister_rt_sched_group(tg);
8819 	/*
8820 	 * We have to wait for yet another RCU grace period to expire, as
8821 	 * print_cfs_stats() might run concurrently.
8822 	 */
8823 	call_rcu(&tg->rcu, sched_free_group_rcu);
8824 }
8825 
8826 /* allocate runqueue etc for a new task group */
8827 struct task_group *sched_create_group(struct task_group *parent)
8828 {
8829 	struct task_group *tg;
8830 
8831 	tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO);
8832 	if (!tg)
8833 		return ERR_PTR(-ENOMEM);
8834 
8835 	if (!alloc_fair_sched_group(tg, parent))
8836 		goto err;
8837 
8838 	if (!alloc_rt_sched_group(tg, parent))
8839 		goto err;
8840 
8841 	scx_group_set_weight(tg, CGROUP_WEIGHT_DFL);
8842 	alloc_uclamp_sched_group(tg, parent);
8843 
8844 	return tg;
8845 
8846 err:
8847 	sched_free_group(tg);
8848 	return ERR_PTR(-ENOMEM);
8849 }
8850 
8851 void sched_online_group(struct task_group *tg, struct task_group *parent)
8852 {
8853 	unsigned long flags;
8854 
8855 	spin_lock_irqsave(&task_group_lock, flags);
8856 	list_add_rcu(&tg->list, &task_groups);
8857 
8858 	/* Root should already exist: */
8859 	WARN_ON(!parent);
8860 
8861 	tg->parent = parent;
8862 	INIT_LIST_HEAD(&tg->children);
8863 	list_add_rcu(&tg->siblings, &parent->children);
8864 	spin_unlock_irqrestore(&task_group_lock, flags);
8865 
8866 	online_fair_sched_group(tg);
8867 }
8868 
8869 /* RCU callback to free various structures associated with a task group */
8870 static void sched_unregister_group_rcu(struct rcu_head *rhp)
8871 {
8872 	/* Now it should be safe to free those cfs_rqs: */
8873 	sched_unregister_group(container_of(rhp, struct task_group, rcu));
8874 }
8875 
8876 void sched_destroy_group(struct task_group *tg)
8877 {
8878 	/* Wait for possible concurrent references to cfs_rqs complete: */
8879 	call_rcu(&tg->rcu, sched_unregister_group_rcu);
8880 }
8881 
8882 void sched_release_group(struct task_group *tg)
8883 {
8884 	unsigned long flags;
8885 
8886 	/*
8887 	 * Unlink first, to avoid walk_tg_tree_from() from finding us (via
8888 	 * sched_cfs_period_timer()).
8889 	 *
8890 	 * For this to be effective, we have to wait for all pending users of
8891 	 * this task group to leave their RCU critical section to ensure no new
8892 	 * user will see our dying task group any more. Specifically ensure
8893 	 * that tg_unthrottle_up() won't add decayed cfs_rq's to it.
8894 	 *
8895 	 * We therefore defer calling unregister_fair_sched_group() to
8896 	 * sched_unregister_group() which is guarantied to get called only after the
8897 	 * current RCU grace period has expired.
8898 	 */
8899 	spin_lock_irqsave(&task_group_lock, flags);
8900 	list_del_rcu(&tg->list);
8901 	list_del_rcu(&tg->siblings);
8902 	spin_unlock_irqrestore(&task_group_lock, flags);
8903 }
8904 
8905 static struct task_group *sched_get_task_group(struct task_struct *tsk)
8906 {
8907 	struct task_group *tg;
8908 
8909 	/*
8910 	 * All callers are synchronized by task_rq_lock(); we do not use RCU
8911 	 * which is pointless here. Thus, we pass "true" to task_css_check()
8912 	 * to prevent lockdep warnings.
8913 	 */
8914 	tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
8915 			  struct task_group, css);
8916 	tg = autogroup_task_group(tsk, tg);
8917 
8918 	return tg;
8919 }
8920 
8921 static void sched_change_group(struct task_struct *tsk, struct task_group *group)
8922 {
8923 	tsk->sched_task_group = group;
8924 
8925 #ifdef CONFIG_FAIR_GROUP_SCHED
8926 	if (tsk->sched_class->task_change_group)
8927 		tsk->sched_class->task_change_group(tsk);
8928 	else
8929 #endif
8930 		set_task_rq(tsk, task_cpu(tsk));
8931 }
8932 
8933 /*
8934  * Change task's runqueue when it moves between groups.
8935  *
8936  * The caller of this function should have put the task in its new group by
8937  * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect
8938  * its new group.
8939  */
8940 void sched_move_task(struct task_struct *tsk)
8941 {
8942 	int queued, running, queue_flags =
8943 		DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
8944 	struct task_group *group;
8945 	struct rq *rq;
8946 
8947 	CLASS(task_rq_lock, rq_guard)(tsk);
8948 	rq = rq_guard.rq;
8949 
8950 	/*
8951 	 * Esp. with SCHED_AUTOGROUP enabled it is possible to get superfluous
8952 	 * group changes.
8953 	 */
8954 	group = sched_get_task_group(tsk);
8955 	if (group == tsk->sched_task_group)
8956 		return;
8957 
8958 	update_rq_clock(rq);
8959 
8960 	running = task_current(rq, tsk);
8961 	queued = task_on_rq_queued(tsk);
8962 
8963 	if (queued)
8964 		dequeue_task(rq, tsk, queue_flags);
8965 	if (running)
8966 		put_prev_task(rq, tsk);
8967 
8968 	sched_change_group(tsk, group);
8969 	scx_move_task(tsk);
8970 
8971 	if (queued)
8972 		enqueue_task(rq, tsk, queue_flags);
8973 	if (running) {
8974 		set_next_task(rq, tsk);
8975 		/*
8976 		 * After changing group, the running task may have joined a
8977 		 * throttled one but it's still the running task. Trigger a
8978 		 * resched to make sure that task can still run.
8979 		 */
8980 		resched_curr(rq);
8981 	}
8982 }
8983 
8984 static struct cgroup_subsys_state *
8985 cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
8986 {
8987 	struct task_group *parent = css_tg(parent_css);
8988 	struct task_group *tg;
8989 
8990 	if (!parent) {
8991 		/* This is early initialization for the top cgroup */
8992 		return &root_task_group.css;
8993 	}
8994 
8995 	tg = sched_create_group(parent);
8996 	if (IS_ERR(tg))
8997 		return ERR_PTR(-ENOMEM);
8998 
8999 	return &tg->css;
9000 }
9001 
9002 /* Expose task group only after completing cgroup initialization */
9003 static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
9004 {
9005 	struct task_group *tg = css_tg(css);
9006 	struct task_group *parent = css_tg(css->parent);
9007 	int ret;
9008 
9009 	ret = scx_tg_online(tg);
9010 	if (ret)
9011 		return ret;
9012 
9013 	if (parent)
9014 		sched_online_group(tg, parent);
9015 
9016 #ifdef CONFIG_UCLAMP_TASK_GROUP
9017 	/* Propagate the effective uclamp value for the new group */
9018 	guard(mutex)(&uclamp_mutex);
9019 	guard(rcu)();
9020 	cpu_util_update_eff(css);
9021 #endif
9022 
9023 	return 0;
9024 }
9025 
9026 static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
9027 {
9028 	struct task_group *tg = css_tg(css);
9029 
9030 	scx_tg_offline(tg);
9031 }
9032 
9033 static void cpu_cgroup_css_released(struct cgroup_subsys_state *css)
9034 {
9035 	struct task_group *tg = css_tg(css);
9036 
9037 	sched_release_group(tg);
9038 }
9039 
9040 static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
9041 {
9042 	struct task_group *tg = css_tg(css);
9043 
9044 	/*
9045 	 * Relies on the RCU grace period between css_released() and this.
9046 	 */
9047 	sched_unregister_group(tg);
9048 }
9049 
9050 static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
9051 {
9052 #ifdef CONFIG_RT_GROUP_SCHED
9053 	struct task_struct *task;
9054 	struct cgroup_subsys_state *css;
9055 
9056 	cgroup_taskset_for_each(task, css, tset) {
9057 		if (!sched_rt_can_attach(css_tg(css), task))
9058 			return -EINVAL;
9059 	}
9060 #endif
9061 	return scx_cgroup_can_attach(tset);
9062 }
9063 
9064 static void cpu_cgroup_attach(struct cgroup_taskset *tset)
9065 {
9066 	struct task_struct *task;
9067 	struct cgroup_subsys_state *css;
9068 
9069 	cgroup_taskset_for_each(task, css, tset)
9070 		sched_move_task(task);
9071 
9072 	scx_cgroup_finish_attach();
9073 }
9074 
9075 static void cpu_cgroup_cancel_attach(struct cgroup_taskset *tset)
9076 {
9077 	scx_cgroup_cancel_attach(tset);
9078 }
9079 
9080 #ifdef CONFIG_UCLAMP_TASK_GROUP
9081 static void cpu_util_update_eff(struct cgroup_subsys_state *css)
9082 {
9083 	struct cgroup_subsys_state *top_css = css;
9084 	struct uclamp_se *uc_parent = NULL;
9085 	struct uclamp_se *uc_se = NULL;
9086 	unsigned int eff[UCLAMP_CNT];
9087 	enum uclamp_id clamp_id;
9088 	unsigned int clamps;
9089 
9090 	lockdep_assert_held(&uclamp_mutex);
9091 	SCHED_WARN_ON(!rcu_read_lock_held());
9092 
9093 	css_for_each_descendant_pre(css, top_css) {
9094 		uc_parent = css_tg(css)->parent
9095 			? css_tg(css)->parent->uclamp : NULL;
9096 
9097 		for_each_clamp_id(clamp_id) {
9098 			/* Assume effective clamps matches requested clamps */
9099 			eff[clamp_id] = css_tg(css)->uclamp_req[clamp_id].value;
9100 			/* Cap effective clamps with parent's effective clamps */
9101 			if (uc_parent &&
9102 			    eff[clamp_id] > uc_parent[clamp_id].value) {
9103 				eff[clamp_id] = uc_parent[clamp_id].value;
9104 			}
9105 		}
9106 		/* Ensure protection is always capped by limit */
9107 		eff[UCLAMP_MIN] = min(eff[UCLAMP_MIN], eff[UCLAMP_MAX]);
9108 
9109 		/* Propagate most restrictive effective clamps */
9110 		clamps = 0x0;
9111 		uc_se = css_tg(css)->uclamp;
9112 		for_each_clamp_id(clamp_id) {
9113 			if (eff[clamp_id] == uc_se[clamp_id].value)
9114 				continue;
9115 			uc_se[clamp_id].value = eff[clamp_id];
9116 			uc_se[clamp_id].bucket_id = uclamp_bucket_id(eff[clamp_id]);
9117 			clamps |= (0x1 << clamp_id);
9118 		}
9119 		if (!clamps) {
9120 			css = css_rightmost_descendant(css);
9121 			continue;
9122 		}
9123 
9124 		/* Immediately update descendants RUNNABLE tasks */
9125 		uclamp_update_active_tasks(css);
9126 	}
9127 }
9128 
9129 /*
9130  * Integer 10^N with a given N exponent by casting to integer the literal "1eN"
9131  * C expression. Since there is no way to convert a macro argument (N) into a
9132  * character constant, use two levels of macros.
9133  */
9134 #define _POW10(exp) ((unsigned int)1e##exp)
9135 #define POW10(exp) _POW10(exp)
9136 
9137 struct uclamp_request {
9138 #define UCLAMP_PERCENT_SHIFT	2
9139 #define UCLAMP_PERCENT_SCALE	(100 * POW10(UCLAMP_PERCENT_SHIFT))
9140 	s64 percent;
9141 	u64 util;
9142 	int ret;
9143 };
9144 
9145 static inline struct uclamp_request
9146 capacity_from_percent(char *buf)
9147 {
9148 	struct uclamp_request req = {
9149 		.percent = UCLAMP_PERCENT_SCALE,
9150 		.util = SCHED_CAPACITY_SCALE,
9151 		.ret = 0,
9152 	};
9153 
9154 	buf = strim(buf);
9155 	if (strcmp(buf, "max")) {
9156 		req.ret = cgroup_parse_float(buf, UCLAMP_PERCENT_SHIFT,
9157 					     &req.percent);
9158 		if (req.ret)
9159 			return req;
9160 		if ((u64)req.percent > UCLAMP_PERCENT_SCALE) {
9161 			req.ret = -ERANGE;
9162 			return req;
9163 		}
9164 
9165 		req.util = req.percent << SCHED_CAPACITY_SHIFT;
9166 		req.util = DIV_ROUND_CLOSEST_ULL(req.util, UCLAMP_PERCENT_SCALE);
9167 	}
9168 
9169 	return req;
9170 }
9171 
9172 static ssize_t cpu_uclamp_write(struct kernfs_open_file *of, char *buf,
9173 				size_t nbytes, loff_t off,
9174 				enum uclamp_id clamp_id)
9175 {
9176 	struct uclamp_request req;
9177 	struct task_group *tg;
9178 
9179 	req = capacity_from_percent(buf);
9180 	if (req.ret)
9181 		return req.ret;
9182 
9183 	static_branch_enable(&sched_uclamp_used);
9184 
9185 	guard(mutex)(&uclamp_mutex);
9186 	guard(rcu)();
9187 
9188 	tg = css_tg(of_css(of));
9189 	if (tg->uclamp_req[clamp_id].value != req.util)
9190 		uclamp_se_set(&tg->uclamp_req[clamp_id], req.util, false);
9191 
9192 	/*
9193 	 * Because of not recoverable conversion rounding we keep track of the
9194 	 * exact requested value
9195 	 */
9196 	tg->uclamp_pct[clamp_id] = req.percent;
9197 
9198 	/* Update effective clamps to track the most restrictive value */
9199 	cpu_util_update_eff(of_css(of));
9200 
9201 	return nbytes;
9202 }
9203 
9204 static ssize_t cpu_uclamp_min_write(struct kernfs_open_file *of,
9205 				    char *buf, size_t nbytes,
9206 				    loff_t off)
9207 {
9208 	return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MIN);
9209 }
9210 
9211 static ssize_t cpu_uclamp_max_write(struct kernfs_open_file *of,
9212 				    char *buf, size_t nbytes,
9213 				    loff_t off)
9214 {
9215 	return cpu_uclamp_write(of, buf, nbytes, off, UCLAMP_MAX);
9216 }
9217 
9218 static inline void cpu_uclamp_print(struct seq_file *sf,
9219 				    enum uclamp_id clamp_id)
9220 {
9221 	struct task_group *tg;
9222 	u64 util_clamp;
9223 	u64 percent;
9224 	u32 rem;
9225 
9226 	scoped_guard (rcu) {
9227 		tg = css_tg(seq_css(sf));
9228 		util_clamp = tg->uclamp_req[clamp_id].value;
9229 	}
9230 
9231 	if (util_clamp == SCHED_CAPACITY_SCALE) {
9232 		seq_puts(sf, "max\n");
9233 		return;
9234 	}
9235 
9236 	percent = tg->uclamp_pct[clamp_id];
9237 	percent = div_u64_rem(percent, POW10(UCLAMP_PERCENT_SHIFT), &rem);
9238 	seq_printf(sf, "%llu.%0*u\n", percent, UCLAMP_PERCENT_SHIFT, rem);
9239 }
9240 
9241 static int cpu_uclamp_min_show(struct seq_file *sf, void *v)
9242 {
9243 	cpu_uclamp_print(sf, UCLAMP_MIN);
9244 	return 0;
9245 }
9246 
9247 static int cpu_uclamp_max_show(struct seq_file *sf, void *v)
9248 {
9249 	cpu_uclamp_print(sf, UCLAMP_MAX);
9250 	return 0;
9251 }
9252 #endif /* CONFIG_UCLAMP_TASK_GROUP */
9253 
9254 #ifdef CONFIG_GROUP_SCHED_WEIGHT
9255 static unsigned long tg_weight(struct task_group *tg)
9256 {
9257 #ifdef CONFIG_FAIR_GROUP_SCHED
9258 	return scale_load_down(tg->shares);
9259 #else
9260 	return sched_weight_from_cgroup(tg->scx_weight);
9261 #endif
9262 }
9263 
9264 static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
9265 				struct cftype *cftype, u64 shareval)
9266 {
9267 	int ret;
9268 
9269 	if (shareval > scale_load_down(ULONG_MAX))
9270 		shareval = MAX_SHARES;
9271 	ret = sched_group_set_shares(css_tg(css), scale_load(shareval));
9272 	if (!ret)
9273 		scx_group_set_weight(css_tg(css),
9274 				     sched_weight_to_cgroup(shareval));
9275 	return ret;
9276 }
9277 
9278 static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
9279 			       struct cftype *cft)
9280 {
9281 	return tg_weight(css_tg(css));
9282 }
9283 #endif /* CONFIG_GROUP_SCHED_WEIGHT */
9284 
9285 #ifdef CONFIG_CFS_BANDWIDTH
9286 static DEFINE_MUTEX(cfs_constraints_mutex);
9287 
9288 const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
9289 static const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
9290 /* More than 203 days if BW_SHIFT equals 20. */
9291 static const u64 max_cfs_runtime = MAX_BW * NSEC_PER_USEC;
9292 
9293 static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
9294 
9295 static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota,
9296 				u64 burst)
9297 {
9298 	int i, ret = 0, runtime_enabled, runtime_was_enabled;
9299 	struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
9300 
9301 	if (tg == &root_task_group)
9302 		return -EINVAL;
9303 
9304 	/*
9305 	 * Ensure we have at some amount of bandwidth every period.  This is
9306 	 * to prevent reaching a state of large arrears when throttled via
9307 	 * entity_tick() resulting in prolonged exit starvation.
9308 	 */
9309 	if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
9310 		return -EINVAL;
9311 
9312 	/*
9313 	 * Likewise, bound things on the other side by preventing insane quota
9314 	 * periods.  This also allows us to normalize in computing quota
9315 	 * feasibility.
9316 	 */
9317 	if (period > max_cfs_quota_period)
9318 		return -EINVAL;
9319 
9320 	/*
9321 	 * Bound quota to defend quota against overflow during bandwidth shift.
9322 	 */
9323 	if (quota != RUNTIME_INF && quota > max_cfs_runtime)
9324 		return -EINVAL;
9325 
9326 	if (quota != RUNTIME_INF && (burst > quota ||
9327 				     burst + quota > max_cfs_runtime))
9328 		return -EINVAL;
9329 
9330 	/*
9331 	 * Prevent race between setting of cfs_rq->runtime_enabled and
9332 	 * unthrottle_offline_cfs_rqs().
9333 	 */
9334 	guard(cpus_read_lock)();
9335 	guard(mutex)(&cfs_constraints_mutex);
9336 
9337 	ret = __cfs_schedulable(tg, period, quota);
9338 	if (ret)
9339 		return ret;
9340 
9341 	runtime_enabled = quota != RUNTIME_INF;
9342 	runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
9343 	/*
9344 	 * If we need to toggle cfs_bandwidth_used, off->on must occur
9345 	 * before making related changes, and on->off must occur afterwards
9346 	 */
9347 	if (runtime_enabled && !runtime_was_enabled)
9348 		cfs_bandwidth_usage_inc();
9349 
9350 	scoped_guard (raw_spinlock_irq, &cfs_b->lock) {
9351 		cfs_b->period = ns_to_ktime(period);
9352 		cfs_b->quota = quota;
9353 		cfs_b->burst = burst;
9354 
9355 		__refill_cfs_bandwidth_runtime(cfs_b);
9356 
9357 		/*
9358 		 * Restart the period timer (if active) to handle new
9359 		 * period expiry:
9360 		 */
9361 		if (runtime_enabled)
9362 			start_cfs_bandwidth(cfs_b);
9363 	}
9364 
9365 	for_each_online_cpu(i) {
9366 		struct cfs_rq *cfs_rq = tg->cfs_rq[i];
9367 		struct rq *rq = cfs_rq->rq;
9368 
9369 		guard(rq_lock_irq)(rq);
9370 		cfs_rq->runtime_enabled = runtime_enabled;
9371 		cfs_rq->runtime_remaining = 0;
9372 
9373 		if (cfs_rq->throttled)
9374 			unthrottle_cfs_rq(cfs_rq);
9375 	}
9376 
9377 	if (runtime_was_enabled && !runtime_enabled)
9378 		cfs_bandwidth_usage_dec();
9379 
9380 	return 0;
9381 }
9382 
9383 static int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
9384 {
9385 	u64 quota, period, burst;
9386 
9387 	period = ktime_to_ns(tg->cfs_bandwidth.period);
9388 	burst = tg->cfs_bandwidth.burst;
9389 	if (cfs_quota_us < 0)
9390 		quota = RUNTIME_INF;
9391 	else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
9392 		quota = (u64)cfs_quota_us * NSEC_PER_USEC;
9393 	else
9394 		return -EINVAL;
9395 
9396 	return tg_set_cfs_bandwidth(tg, period, quota, burst);
9397 }
9398 
9399 static long tg_get_cfs_quota(struct task_group *tg)
9400 {
9401 	u64 quota_us;
9402 
9403 	if (tg->cfs_bandwidth.quota == RUNTIME_INF)
9404 		return -1;
9405 
9406 	quota_us = tg->cfs_bandwidth.quota;
9407 	do_div(quota_us, NSEC_PER_USEC);
9408 
9409 	return quota_us;
9410 }
9411 
9412 static int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
9413 {
9414 	u64 quota, period, burst;
9415 
9416 	if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC)
9417 		return -EINVAL;
9418 
9419 	period = (u64)cfs_period_us * NSEC_PER_USEC;
9420 	quota = tg->cfs_bandwidth.quota;
9421 	burst = tg->cfs_bandwidth.burst;
9422 
9423 	return tg_set_cfs_bandwidth(tg, period, quota, burst);
9424 }
9425 
9426 static long tg_get_cfs_period(struct task_group *tg)
9427 {
9428 	u64 cfs_period_us;
9429 
9430 	cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
9431 	do_div(cfs_period_us, NSEC_PER_USEC);
9432 
9433 	return cfs_period_us;
9434 }
9435 
9436 static int tg_set_cfs_burst(struct task_group *tg, long cfs_burst_us)
9437 {
9438 	u64 quota, period, burst;
9439 
9440 	if ((u64)cfs_burst_us > U64_MAX / NSEC_PER_USEC)
9441 		return -EINVAL;
9442 
9443 	burst = (u64)cfs_burst_us * NSEC_PER_USEC;
9444 	period = ktime_to_ns(tg->cfs_bandwidth.period);
9445 	quota = tg->cfs_bandwidth.quota;
9446 
9447 	return tg_set_cfs_bandwidth(tg, period, quota, burst);
9448 }
9449 
9450 static long tg_get_cfs_burst(struct task_group *tg)
9451 {
9452 	u64 burst_us;
9453 
9454 	burst_us = tg->cfs_bandwidth.burst;
9455 	do_div(burst_us, NSEC_PER_USEC);
9456 
9457 	return burst_us;
9458 }
9459 
9460 static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
9461 				  struct cftype *cft)
9462 {
9463 	return tg_get_cfs_quota(css_tg(css));
9464 }
9465 
9466 static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
9467 				   struct cftype *cftype, s64 cfs_quota_us)
9468 {
9469 	return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
9470 }
9471 
9472 static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
9473 				   struct cftype *cft)
9474 {
9475 	return tg_get_cfs_period(css_tg(css));
9476 }
9477 
9478 static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
9479 				    struct cftype *cftype, u64 cfs_period_us)
9480 {
9481 	return tg_set_cfs_period(css_tg(css), cfs_period_us);
9482 }
9483 
9484 static u64 cpu_cfs_burst_read_u64(struct cgroup_subsys_state *css,
9485 				  struct cftype *cft)
9486 {
9487 	return tg_get_cfs_burst(css_tg(css));
9488 }
9489 
9490 static int cpu_cfs_burst_write_u64(struct cgroup_subsys_state *css,
9491 				   struct cftype *cftype, u64 cfs_burst_us)
9492 {
9493 	return tg_set_cfs_burst(css_tg(css), cfs_burst_us);
9494 }
9495 
9496 struct cfs_schedulable_data {
9497 	struct task_group *tg;
9498 	u64 period, quota;
9499 };
9500 
9501 /*
9502  * normalize group quota/period to be quota/max_period
9503  * note: units are usecs
9504  */
9505 static u64 normalize_cfs_quota(struct task_group *tg,
9506 			       struct cfs_schedulable_data *d)
9507 {
9508 	u64 quota, period;
9509 
9510 	if (tg == d->tg) {
9511 		period = d->period;
9512 		quota = d->quota;
9513 	} else {
9514 		period = tg_get_cfs_period(tg);
9515 		quota = tg_get_cfs_quota(tg);
9516 	}
9517 
9518 	/* note: these should typically be equivalent */
9519 	if (quota == RUNTIME_INF || quota == -1)
9520 		return RUNTIME_INF;
9521 
9522 	return to_ratio(period, quota);
9523 }
9524 
9525 static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
9526 {
9527 	struct cfs_schedulable_data *d = data;
9528 	struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
9529 	s64 quota = 0, parent_quota = -1;
9530 
9531 	if (!tg->parent) {
9532 		quota = RUNTIME_INF;
9533 	} else {
9534 		struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
9535 
9536 		quota = normalize_cfs_quota(tg, d);
9537 		parent_quota = parent_b->hierarchical_quota;
9538 
9539 		/*
9540 		 * Ensure max(child_quota) <= parent_quota.  On cgroup2,
9541 		 * always take the non-RUNTIME_INF min.  On cgroup1, only
9542 		 * inherit when no limit is set. In both cases this is used
9543 		 * by the scheduler to determine if a given CFS task has a
9544 		 * bandwidth constraint at some higher level.
9545 		 */
9546 		if (cgroup_subsys_on_dfl(cpu_cgrp_subsys)) {
9547 			if (quota == RUNTIME_INF)
9548 				quota = parent_quota;
9549 			else if (parent_quota != RUNTIME_INF)
9550 				quota = min(quota, parent_quota);
9551 		} else {
9552 			if (quota == RUNTIME_INF)
9553 				quota = parent_quota;
9554 			else if (parent_quota != RUNTIME_INF && quota > parent_quota)
9555 				return -EINVAL;
9556 		}
9557 	}
9558 	cfs_b->hierarchical_quota = quota;
9559 
9560 	return 0;
9561 }
9562 
9563 static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
9564 {
9565 	struct cfs_schedulable_data data = {
9566 		.tg = tg,
9567 		.period = period,
9568 		.quota = quota,
9569 	};
9570 
9571 	if (quota != RUNTIME_INF) {
9572 		do_div(data.period, NSEC_PER_USEC);
9573 		do_div(data.quota, NSEC_PER_USEC);
9574 	}
9575 
9576 	guard(rcu)();
9577 	return walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
9578 }
9579 
9580 static int cpu_cfs_stat_show(struct seq_file *sf, void *v)
9581 {
9582 	struct task_group *tg = css_tg(seq_css(sf));
9583 	struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
9584 
9585 	seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
9586 	seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
9587 	seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
9588 
9589 	if (schedstat_enabled() && tg != &root_task_group) {
9590 		struct sched_statistics *stats;
9591 		u64 ws = 0;
9592 		int i;
9593 
9594 		for_each_possible_cpu(i) {
9595 			stats = __schedstats_from_se(tg->se[i]);
9596 			ws += schedstat_val(stats->wait_sum);
9597 		}
9598 
9599 		seq_printf(sf, "wait_sum %llu\n", ws);
9600 	}
9601 
9602 	seq_printf(sf, "nr_bursts %d\n", cfs_b->nr_burst);
9603 	seq_printf(sf, "burst_time %llu\n", cfs_b->burst_time);
9604 
9605 	return 0;
9606 }
9607 
9608 static u64 throttled_time_self(struct task_group *tg)
9609 {
9610 	int i;
9611 	u64 total = 0;
9612 
9613 	for_each_possible_cpu(i) {
9614 		total += READ_ONCE(tg->cfs_rq[i]->throttled_clock_self_time);
9615 	}
9616 
9617 	return total;
9618 }
9619 
9620 static int cpu_cfs_local_stat_show(struct seq_file *sf, void *v)
9621 {
9622 	struct task_group *tg = css_tg(seq_css(sf));
9623 
9624 	seq_printf(sf, "throttled_time %llu\n", throttled_time_self(tg));
9625 
9626 	return 0;
9627 }
9628 #endif /* CONFIG_CFS_BANDWIDTH */
9629 
9630 #ifdef CONFIG_RT_GROUP_SCHED
9631 static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
9632 				struct cftype *cft, s64 val)
9633 {
9634 	return sched_group_set_rt_runtime(css_tg(css), val);
9635 }
9636 
9637 static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
9638 			       struct cftype *cft)
9639 {
9640 	return sched_group_rt_runtime(css_tg(css));
9641 }
9642 
9643 static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
9644 				    struct cftype *cftype, u64 rt_period_us)
9645 {
9646 	return sched_group_set_rt_period(css_tg(css), rt_period_us);
9647 }
9648 
9649 static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
9650 				   struct cftype *cft)
9651 {
9652 	return sched_group_rt_period(css_tg(css));
9653 }
9654 #endif /* CONFIG_RT_GROUP_SCHED */
9655 
9656 #ifdef CONFIG_GROUP_SCHED_WEIGHT
9657 static s64 cpu_idle_read_s64(struct cgroup_subsys_state *css,
9658 			       struct cftype *cft)
9659 {
9660 	return css_tg(css)->idle;
9661 }
9662 
9663 static int cpu_idle_write_s64(struct cgroup_subsys_state *css,
9664 				struct cftype *cft, s64 idle)
9665 {
9666 	int ret;
9667 
9668 	ret = sched_group_set_idle(css_tg(css), idle);
9669 	if (!ret)
9670 		scx_group_set_idle(css_tg(css), idle);
9671 	return ret;
9672 }
9673 #endif
9674 
9675 static struct cftype cpu_legacy_files[] = {
9676 #ifdef CONFIG_GROUP_SCHED_WEIGHT
9677 	{
9678 		.name = "shares",
9679 		.read_u64 = cpu_shares_read_u64,
9680 		.write_u64 = cpu_shares_write_u64,
9681 	},
9682 	{
9683 		.name = "idle",
9684 		.read_s64 = cpu_idle_read_s64,
9685 		.write_s64 = cpu_idle_write_s64,
9686 	},
9687 #endif
9688 #ifdef CONFIG_CFS_BANDWIDTH
9689 	{
9690 		.name = "cfs_quota_us",
9691 		.read_s64 = cpu_cfs_quota_read_s64,
9692 		.write_s64 = cpu_cfs_quota_write_s64,
9693 	},
9694 	{
9695 		.name = "cfs_period_us",
9696 		.read_u64 = cpu_cfs_period_read_u64,
9697 		.write_u64 = cpu_cfs_period_write_u64,
9698 	},
9699 	{
9700 		.name = "cfs_burst_us",
9701 		.read_u64 = cpu_cfs_burst_read_u64,
9702 		.write_u64 = cpu_cfs_burst_write_u64,
9703 	},
9704 	{
9705 		.name = "stat",
9706 		.seq_show = cpu_cfs_stat_show,
9707 	},
9708 	{
9709 		.name = "stat.local",
9710 		.seq_show = cpu_cfs_local_stat_show,
9711 	},
9712 #endif
9713 #ifdef CONFIG_RT_GROUP_SCHED
9714 	{
9715 		.name = "rt_runtime_us",
9716 		.read_s64 = cpu_rt_runtime_read,
9717 		.write_s64 = cpu_rt_runtime_write,
9718 	},
9719 	{
9720 		.name = "rt_period_us",
9721 		.read_u64 = cpu_rt_period_read_uint,
9722 		.write_u64 = cpu_rt_period_write_uint,
9723 	},
9724 #endif
9725 #ifdef CONFIG_UCLAMP_TASK_GROUP
9726 	{
9727 		.name = "uclamp.min",
9728 		.flags = CFTYPE_NOT_ON_ROOT,
9729 		.seq_show = cpu_uclamp_min_show,
9730 		.write = cpu_uclamp_min_write,
9731 	},
9732 	{
9733 		.name = "uclamp.max",
9734 		.flags = CFTYPE_NOT_ON_ROOT,
9735 		.seq_show = cpu_uclamp_max_show,
9736 		.write = cpu_uclamp_max_write,
9737 	},
9738 #endif
9739 	{ }	/* Terminate */
9740 };
9741 
9742 static int cpu_extra_stat_show(struct seq_file *sf,
9743 			       struct cgroup_subsys_state *css)
9744 {
9745 #ifdef CONFIG_CFS_BANDWIDTH
9746 	{
9747 		struct task_group *tg = css_tg(css);
9748 		struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
9749 		u64 throttled_usec, burst_usec;
9750 
9751 		throttled_usec = cfs_b->throttled_time;
9752 		do_div(throttled_usec, NSEC_PER_USEC);
9753 		burst_usec = cfs_b->burst_time;
9754 		do_div(burst_usec, NSEC_PER_USEC);
9755 
9756 		seq_printf(sf, "nr_periods %d\n"
9757 			   "nr_throttled %d\n"
9758 			   "throttled_usec %llu\n"
9759 			   "nr_bursts %d\n"
9760 			   "burst_usec %llu\n",
9761 			   cfs_b->nr_periods, cfs_b->nr_throttled,
9762 			   throttled_usec, cfs_b->nr_burst, burst_usec);
9763 	}
9764 #endif
9765 	return 0;
9766 }
9767 
9768 static int cpu_local_stat_show(struct seq_file *sf,
9769 			       struct cgroup_subsys_state *css)
9770 {
9771 #ifdef CONFIG_CFS_BANDWIDTH
9772 	{
9773 		struct task_group *tg = css_tg(css);
9774 		u64 throttled_self_usec;
9775 
9776 		throttled_self_usec = throttled_time_self(tg);
9777 		do_div(throttled_self_usec, NSEC_PER_USEC);
9778 
9779 		seq_printf(sf, "throttled_usec %llu\n",
9780 			   throttled_self_usec);
9781 	}
9782 #endif
9783 	return 0;
9784 }
9785 
9786 #ifdef CONFIG_GROUP_SCHED_WEIGHT
9787 
9788 static u64 cpu_weight_read_u64(struct cgroup_subsys_state *css,
9789 			       struct cftype *cft)
9790 {
9791 	return sched_weight_to_cgroup(tg_weight(css_tg(css)));
9792 }
9793 
9794 static int cpu_weight_write_u64(struct cgroup_subsys_state *css,
9795 				struct cftype *cft, u64 cgrp_weight)
9796 {
9797 	unsigned long weight;
9798 	int ret;
9799 
9800 	if (cgrp_weight < CGROUP_WEIGHT_MIN || cgrp_weight > CGROUP_WEIGHT_MAX)
9801 		return -ERANGE;
9802 
9803 	weight = sched_weight_from_cgroup(cgrp_weight);
9804 
9805 	ret = sched_group_set_shares(css_tg(css), scale_load(weight));
9806 	if (!ret)
9807 		scx_group_set_weight(css_tg(css), cgrp_weight);
9808 	return ret;
9809 }
9810 
9811 static s64 cpu_weight_nice_read_s64(struct cgroup_subsys_state *css,
9812 				    struct cftype *cft)
9813 {
9814 	unsigned long weight = tg_weight(css_tg(css));
9815 	int last_delta = INT_MAX;
9816 	int prio, delta;
9817 
9818 	/* find the closest nice value to the current weight */
9819 	for (prio = 0; prio < ARRAY_SIZE(sched_prio_to_weight); prio++) {
9820 		delta = abs(sched_prio_to_weight[prio] - weight);
9821 		if (delta >= last_delta)
9822 			break;
9823 		last_delta = delta;
9824 	}
9825 
9826 	return PRIO_TO_NICE(prio - 1 + MAX_RT_PRIO);
9827 }
9828 
9829 static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css,
9830 				     struct cftype *cft, s64 nice)
9831 {
9832 	unsigned long weight;
9833 	int idx, ret;
9834 
9835 	if (nice < MIN_NICE || nice > MAX_NICE)
9836 		return -ERANGE;
9837 
9838 	idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO;
9839 	idx = array_index_nospec(idx, 40);
9840 	weight = sched_prio_to_weight[idx];
9841 
9842 	ret = sched_group_set_shares(css_tg(css), scale_load(weight));
9843 	if (!ret)
9844 		scx_group_set_weight(css_tg(css),
9845 				     sched_weight_to_cgroup(weight));
9846 	return ret;
9847 }
9848 #endif /* CONFIG_GROUP_SCHED_WEIGHT */
9849 
9850 static void __maybe_unused cpu_period_quota_print(struct seq_file *sf,
9851 						  long period, long quota)
9852 {
9853 	if (quota < 0)
9854 		seq_puts(sf, "max");
9855 	else
9856 		seq_printf(sf, "%ld", quota);
9857 
9858 	seq_printf(sf, " %ld\n", period);
9859 }
9860 
9861 /* caller should put the current value in *@periodp before calling */
9862 static int __maybe_unused cpu_period_quota_parse(char *buf,
9863 						 u64 *periodp, u64 *quotap)
9864 {
9865 	char tok[21];	/* U64_MAX */
9866 
9867 	if (sscanf(buf, "%20s %llu", tok, periodp) < 1)
9868 		return -EINVAL;
9869 
9870 	*periodp *= NSEC_PER_USEC;
9871 
9872 	if (sscanf(tok, "%llu", quotap))
9873 		*quotap *= NSEC_PER_USEC;
9874 	else if (!strcmp(tok, "max"))
9875 		*quotap = RUNTIME_INF;
9876 	else
9877 		return -EINVAL;
9878 
9879 	return 0;
9880 }
9881 
9882 #ifdef CONFIG_CFS_BANDWIDTH
9883 static int cpu_max_show(struct seq_file *sf, void *v)
9884 {
9885 	struct task_group *tg = css_tg(seq_css(sf));
9886 
9887 	cpu_period_quota_print(sf, tg_get_cfs_period(tg), tg_get_cfs_quota(tg));
9888 	return 0;
9889 }
9890 
9891 static ssize_t cpu_max_write(struct kernfs_open_file *of,
9892 			     char *buf, size_t nbytes, loff_t off)
9893 {
9894 	struct task_group *tg = css_tg(of_css(of));
9895 	u64 period = tg_get_cfs_period(tg);
9896 	u64 burst = tg->cfs_bandwidth.burst;
9897 	u64 quota;
9898 	int ret;
9899 
9900 	ret = cpu_period_quota_parse(buf, &period, &quota);
9901 	if (!ret)
9902 		ret = tg_set_cfs_bandwidth(tg, period, quota, burst);
9903 	return ret ?: nbytes;
9904 }
9905 #endif
9906 
9907 static struct cftype cpu_files[] = {
9908 #ifdef CONFIG_GROUP_SCHED_WEIGHT
9909 	{
9910 		.name = "weight",
9911 		.flags = CFTYPE_NOT_ON_ROOT,
9912 		.read_u64 = cpu_weight_read_u64,
9913 		.write_u64 = cpu_weight_write_u64,
9914 	},
9915 	{
9916 		.name = "weight.nice",
9917 		.flags = CFTYPE_NOT_ON_ROOT,
9918 		.read_s64 = cpu_weight_nice_read_s64,
9919 		.write_s64 = cpu_weight_nice_write_s64,
9920 	},
9921 	{
9922 		.name = "idle",
9923 		.flags = CFTYPE_NOT_ON_ROOT,
9924 		.read_s64 = cpu_idle_read_s64,
9925 		.write_s64 = cpu_idle_write_s64,
9926 	},
9927 #endif
9928 #ifdef CONFIG_CFS_BANDWIDTH
9929 	{
9930 		.name = "max",
9931 		.flags = CFTYPE_NOT_ON_ROOT,
9932 		.seq_show = cpu_max_show,
9933 		.write = cpu_max_write,
9934 	},
9935 	{
9936 		.name = "max.burst",
9937 		.flags = CFTYPE_NOT_ON_ROOT,
9938 		.read_u64 = cpu_cfs_burst_read_u64,
9939 		.write_u64 = cpu_cfs_burst_write_u64,
9940 	},
9941 #endif
9942 #ifdef CONFIG_UCLAMP_TASK_GROUP
9943 	{
9944 		.name = "uclamp.min",
9945 		.flags = CFTYPE_NOT_ON_ROOT,
9946 		.seq_show = cpu_uclamp_min_show,
9947 		.write = cpu_uclamp_min_write,
9948 	},
9949 	{
9950 		.name = "uclamp.max",
9951 		.flags = CFTYPE_NOT_ON_ROOT,
9952 		.seq_show = cpu_uclamp_max_show,
9953 		.write = cpu_uclamp_max_write,
9954 	},
9955 #endif
9956 	{ }	/* terminate */
9957 };
9958 
9959 struct cgroup_subsys cpu_cgrp_subsys = {
9960 	.css_alloc	= cpu_cgroup_css_alloc,
9961 	.css_online	= cpu_cgroup_css_online,
9962 	.css_offline	= cpu_cgroup_css_offline,
9963 	.css_released	= cpu_cgroup_css_released,
9964 	.css_free	= cpu_cgroup_css_free,
9965 	.css_extra_stat_show = cpu_extra_stat_show,
9966 	.css_local_stat_show = cpu_local_stat_show,
9967 	.can_attach	= cpu_cgroup_can_attach,
9968 	.attach		= cpu_cgroup_attach,
9969 	.cancel_attach	= cpu_cgroup_cancel_attach,
9970 	.legacy_cftypes	= cpu_legacy_files,
9971 	.dfl_cftypes	= cpu_files,
9972 	.early_init	= true,
9973 	.threaded	= true,
9974 };
9975 
9976 #endif	/* CONFIG_CGROUP_SCHED */
9977 
9978 void dump_cpu_task(int cpu)
9979 {
9980 	if (in_hardirq() && cpu == smp_processor_id()) {
9981 		struct pt_regs *regs;
9982 
9983 		regs = get_irq_regs();
9984 		if (regs) {
9985 			show_regs(regs);
9986 			return;
9987 		}
9988 	}
9989 
9990 	if (trigger_single_cpu_backtrace(cpu))
9991 		return;
9992 
9993 	pr_info("Task dump for CPU %d:\n", cpu);
9994 	sched_show_task(cpu_curr(cpu));
9995 }
9996 
9997 /*
9998  * Nice levels are multiplicative, with a gentle 10% change for every
9999  * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
10000  * nice 1, it will get ~10% less CPU time than another CPU-bound task
10001  * that remained on nice 0.
10002  *
10003  * The "10% effect" is relative and cumulative: from _any_ nice level,
10004  * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
10005  * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
10006  * If a task goes up by ~10% and another task goes down by ~10% then
10007  * the relative distance between them is ~25%.)
10008  */
10009 const int sched_prio_to_weight[40] = {
10010  /* -20 */     88761,     71755,     56483,     46273,     36291,
10011  /* -15 */     29154,     23254,     18705,     14949,     11916,
10012  /* -10 */      9548,      7620,      6100,      4904,      3906,
10013  /*  -5 */      3121,      2501,      1991,      1586,      1277,
10014  /*   0 */      1024,       820,       655,       526,       423,
10015  /*   5 */       335,       272,       215,       172,       137,
10016  /*  10 */       110,        87,        70,        56,        45,
10017  /*  15 */        36,        29,        23,        18,        15,
10018 };
10019 
10020 /*
10021  * Inverse (2^32/x) values of the sched_prio_to_weight[] array, pre-calculated.
10022  *
10023  * In cases where the weight does not change often, we can use the
10024  * pre-calculated inverse to speed up arithmetics by turning divisions
10025  * into multiplications:
10026  */
10027 const u32 sched_prio_to_wmult[40] = {
10028  /* -20 */     48388,     59856,     76040,     92818,    118348,
10029  /* -15 */    147320,    184698,    229616,    287308,    360437,
10030  /* -10 */    449829,    563644,    704093,    875809,   1099582,
10031  /*  -5 */   1376151,   1717300,   2157191,   2708050,   3363326,
10032  /*   0 */   4194304,   5237765,   6557202,   8165337,  10153587,
10033  /*   5 */  12820798,  15790321,  19976592,  24970740,  31350126,
10034  /*  10 */  39045157,  49367440,  61356676,  76695844,  95443717,
10035  /*  15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
10036 };
10037 
10038 void call_trace_sched_update_nr_running(struct rq *rq, int count)
10039 {
10040         trace_sched_update_nr_running_tp(rq, count);
10041 }
10042 
10043 #ifdef CONFIG_SCHED_MM_CID
10044 
10045 /*
10046  * @cid_lock: Guarantee forward-progress of cid allocation.
10047  *
10048  * Concurrency ID allocation within a bitmap is mostly lock-free. The cid_lock
10049  * is only used when contention is detected by the lock-free allocation so
10050  * forward progress can be guaranteed.
10051  */
10052 DEFINE_RAW_SPINLOCK(cid_lock);
10053 
10054 /*
10055  * @use_cid_lock: Select cid allocation behavior: lock-free vs spinlock.
10056  *
10057  * When @use_cid_lock is 0, the cid allocation is lock-free. When contention is
10058  * detected, it is set to 1 to ensure that all newly coming allocations are
10059  * serialized by @cid_lock until the allocation which detected contention
10060  * completes and sets @use_cid_lock back to 0. This guarantees forward progress
10061  * of a cid allocation.
10062  */
10063 int use_cid_lock;
10064 
10065 /*
10066  * mm_cid remote-clear implements a lock-free algorithm to clear per-mm/cpu cid
10067  * concurrently with respect to the execution of the source runqueue context
10068  * switch.
10069  *
10070  * There is one basic properties we want to guarantee here:
10071  *
10072  * (1) Remote-clear should _never_ mark a per-cpu cid UNSET when it is actively
10073  * used by a task. That would lead to concurrent allocation of the cid and
10074  * userspace corruption.
10075  *
10076  * Provide this guarantee by introducing a Dekker memory ordering to guarantee
10077  * that a pair of loads observe at least one of a pair of stores, which can be
10078  * shown as:
10079  *
10080  *      X = Y = 0
10081  *
10082  *      w[X]=1          w[Y]=1
10083  *      MB              MB
10084  *      r[Y]=y          r[X]=x
10085  *
10086  * Which guarantees that x==0 && y==0 is impossible. But rather than using
10087  * values 0 and 1, this algorithm cares about specific state transitions of the
10088  * runqueue current task (as updated by the scheduler context switch), and the
10089  * per-mm/cpu cid value.
10090  *
10091  * Let's introduce task (Y) which has task->mm == mm and task (N) which has
10092  * task->mm != mm for the rest of the discussion. There are two scheduler state
10093  * transitions on context switch we care about:
10094  *
10095  * (TSA) Store to rq->curr with transition from (N) to (Y)
10096  *
10097  * (TSB) Store to rq->curr with transition from (Y) to (N)
10098  *
10099  * On the remote-clear side, there is one transition we care about:
10100  *
10101  * (TMA) cmpxchg to *pcpu_cid to set the LAZY flag
10102  *
10103  * There is also a transition to UNSET state which can be performed from all
10104  * sides (scheduler, remote-clear). It is always performed with a cmpxchg which
10105  * guarantees that only a single thread will succeed:
10106  *
10107  * (TMB) cmpxchg to *pcpu_cid to mark UNSET
10108  *
10109  * Just to be clear, what we do _not_ want to happen is a transition to UNSET
10110  * when a thread is actively using the cid (property (1)).
10111  *
10112  * Let's looks at the relevant combinations of TSA/TSB, and TMA transitions.
10113  *
10114  * Scenario A) (TSA)+(TMA) (from next task perspective)
10115  *
10116  * CPU0                                      CPU1
10117  *
10118  * Context switch CS-1                       Remote-clear
10119  *   - store to rq->curr: (N)->(Y) (TSA)     - cmpxchg to *pcpu_id to LAZY (TMA)
10120  *                                             (implied barrier after cmpxchg)
10121  *   - switch_mm_cid()
10122  *     - memory barrier (see switch_mm_cid()
10123  *       comment explaining how this barrier
10124  *       is combined with other scheduler
10125  *       barriers)
10126  *     - mm_cid_get (next)
10127  *       - READ_ONCE(*pcpu_cid)              - rcu_dereference(src_rq->curr)
10128  *
10129  * This Dekker ensures that either task (Y) is observed by the
10130  * rcu_dereference() or the LAZY flag is observed by READ_ONCE(), or both are
10131  * observed.
10132  *
10133  * If task (Y) store is observed by rcu_dereference(), it means that there is
10134  * still an active task on the cpu. Remote-clear will therefore not transition
10135  * to UNSET, which fulfills property (1).
10136  *
10137  * If task (Y) is not observed, but the lazy flag is observed by READ_ONCE(),
10138  * it will move its state to UNSET, which clears the percpu cid perhaps
10139  * uselessly (which is not an issue for correctness). Because task (Y) is not
10140  * observed, CPU1 can move ahead to set the state to UNSET. Because moving
10141  * state to UNSET is done with a cmpxchg expecting that the old state has the
10142  * LAZY flag set, only one thread will successfully UNSET.
10143  *
10144  * If both states (LAZY flag and task (Y)) are observed, the thread on CPU0
10145  * will observe the LAZY flag and transition to UNSET (perhaps uselessly), and
10146  * CPU1 will observe task (Y) and do nothing more, which is fine.
10147  *
10148  * What we are effectively preventing with this Dekker is a scenario where
10149  * neither LAZY flag nor store (Y) are observed, which would fail property (1)
10150  * because this would UNSET a cid which is actively used.
10151  */
10152 
10153 void sched_mm_cid_migrate_from(struct task_struct *t)
10154 {
10155 	t->migrate_from_cpu = task_cpu(t);
10156 }
10157 
10158 static
10159 int __sched_mm_cid_migrate_from_fetch_cid(struct rq *src_rq,
10160 					  struct task_struct *t,
10161 					  struct mm_cid *src_pcpu_cid)
10162 {
10163 	struct mm_struct *mm = t->mm;
10164 	struct task_struct *src_task;
10165 	int src_cid, last_mm_cid;
10166 
10167 	if (!mm)
10168 		return -1;
10169 
10170 	last_mm_cid = t->last_mm_cid;
10171 	/*
10172 	 * If the migrated task has no last cid, or if the current
10173 	 * task on src rq uses the cid, it means the source cid does not need
10174 	 * to be moved to the destination cpu.
10175 	 */
10176 	if (last_mm_cid == -1)
10177 		return -1;
10178 	src_cid = READ_ONCE(src_pcpu_cid->cid);
10179 	if (!mm_cid_is_valid(src_cid) || last_mm_cid != src_cid)
10180 		return -1;
10181 
10182 	/*
10183 	 * If we observe an active task using the mm on this rq, it means we
10184 	 * are not the last task to be migrated from this cpu for this mm, so
10185 	 * there is no need to move src_cid to the destination cpu.
10186 	 */
10187 	guard(rcu)();
10188 	src_task = rcu_dereference(src_rq->curr);
10189 	if (READ_ONCE(src_task->mm_cid_active) && src_task->mm == mm) {
10190 		t->last_mm_cid = -1;
10191 		return -1;
10192 	}
10193 
10194 	return src_cid;
10195 }
10196 
10197 static
10198 int __sched_mm_cid_migrate_from_try_steal_cid(struct rq *src_rq,
10199 					      struct task_struct *t,
10200 					      struct mm_cid *src_pcpu_cid,
10201 					      int src_cid)
10202 {
10203 	struct task_struct *src_task;
10204 	struct mm_struct *mm = t->mm;
10205 	int lazy_cid;
10206 
10207 	if (src_cid == -1)
10208 		return -1;
10209 
10210 	/*
10211 	 * Attempt to clear the source cpu cid to move it to the destination
10212 	 * cpu.
10213 	 */
10214 	lazy_cid = mm_cid_set_lazy_put(src_cid);
10215 	if (!try_cmpxchg(&src_pcpu_cid->cid, &src_cid, lazy_cid))
10216 		return -1;
10217 
10218 	/*
10219 	 * The implicit barrier after cmpxchg per-mm/cpu cid before loading
10220 	 * rq->curr->mm matches the scheduler barrier in context_switch()
10221 	 * between store to rq->curr and load of prev and next task's
10222 	 * per-mm/cpu cid.
10223 	 *
10224 	 * The implicit barrier after cmpxchg per-mm/cpu cid before loading
10225 	 * rq->curr->mm_cid_active matches the barrier in
10226 	 * sched_mm_cid_exit_signals(), sched_mm_cid_before_execve(), and
10227 	 * sched_mm_cid_after_execve() between store to t->mm_cid_active and
10228 	 * load of per-mm/cpu cid.
10229 	 */
10230 
10231 	/*
10232 	 * If we observe an active task using the mm on this rq after setting
10233 	 * the lazy-put flag, this task will be responsible for transitioning
10234 	 * from lazy-put flag set to MM_CID_UNSET.
10235 	 */
10236 	scoped_guard (rcu) {
10237 		src_task = rcu_dereference(src_rq->curr);
10238 		if (READ_ONCE(src_task->mm_cid_active) && src_task->mm == mm) {
10239 			/*
10240 			 * We observed an active task for this mm, there is therefore
10241 			 * no point in moving this cid to the destination cpu.
10242 			 */
10243 			t->last_mm_cid = -1;
10244 			return -1;
10245 		}
10246 	}
10247 
10248 	/*
10249 	 * The src_cid is unused, so it can be unset.
10250 	 */
10251 	if (!try_cmpxchg(&src_pcpu_cid->cid, &lazy_cid, MM_CID_UNSET))
10252 		return -1;
10253 	return src_cid;
10254 }
10255 
10256 /*
10257  * Migration to dst cpu. Called with dst_rq lock held.
10258  * Interrupts are disabled, which keeps the window of cid ownership without the
10259  * source rq lock held small.
10260  */
10261 void sched_mm_cid_migrate_to(struct rq *dst_rq, struct task_struct *t)
10262 {
10263 	struct mm_cid *src_pcpu_cid, *dst_pcpu_cid;
10264 	struct mm_struct *mm = t->mm;
10265 	int src_cid, dst_cid, src_cpu;
10266 	struct rq *src_rq;
10267 
10268 	lockdep_assert_rq_held(dst_rq);
10269 
10270 	if (!mm)
10271 		return;
10272 	src_cpu = t->migrate_from_cpu;
10273 	if (src_cpu == -1) {
10274 		t->last_mm_cid = -1;
10275 		return;
10276 	}
10277 	/*
10278 	 * Move the src cid if the dst cid is unset. This keeps id
10279 	 * allocation closest to 0 in cases where few threads migrate around
10280 	 * many CPUs.
10281 	 *
10282 	 * If destination cid is already set, we may have to just clear
10283 	 * the src cid to ensure compactness in frequent migrations
10284 	 * scenarios.
10285 	 *
10286 	 * It is not useful to clear the src cid when the number of threads is
10287 	 * greater or equal to the number of allowed CPUs, because user-space
10288 	 * can expect that the number of allowed cids can reach the number of
10289 	 * allowed CPUs.
10290 	 */
10291 	dst_pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu_of(dst_rq));
10292 	dst_cid = READ_ONCE(dst_pcpu_cid->cid);
10293 	if (!mm_cid_is_unset(dst_cid) &&
10294 	    atomic_read(&mm->mm_users) >= t->nr_cpus_allowed)
10295 		return;
10296 	src_pcpu_cid = per_cpu_ptr(mm->pcpu_cid, src_cpu);
10297 	src_rq = cpu_rq(src_cpu);
10298 	src_cid = __sched_mm_cid_migrate_from_fetch_cid(src_rq, t, src_pcpu_cid);
10299 	if (src_cid == -1)
10300 		return;
10301 	src_cid = __sched_mm_cid_migrate_from_try_steal_cid(src_rq, t, src_pcpu_cid,
10302 							    src_cid);
10303 	if (src_cid == -1)
10304 		return;
10305 	if (!mm_cid_is_unset(dst_cid)) {
10306 		__mm_cid_put(mm, src_cid);
10307 		return;
10308 	}
10309 	/* Move src_cid to dst cpu. */
10310 	mm_cid_snapshot_time(dst_rq, mm);
10311 	WRITE_ONCE(dst_pcpu_cid->cid, src_cid);
10312 }
10313 
10314 static void sched_mm_cid_remote_clear(struct mm_struct *mm, struct mm_cid *pcpu_cid,
10315 				      int cpu)
10316 {
10317 	struct rq *rq = cpu_rq(cpu);
10318 	struct task_struct *t;
10319 	int cid, lazy_cid;
10320 
10321 	cid = READ_ONCE(pcpu_cid->cid);
10322 	if (!mm_cid_is_valid(cid))
10323 		return;
10324 
10325 	/*
10326 	 * Clear the cpu cid if it is set to keep cid allocation compact.  If
10327 	 * there happens to be other tasks left on the source cpu using this
10328 	 * mm, the next task using this mm will reallocate its cid on context
10329 	 * switch.
10330 	 */
10331 	lazy_cid = mm_cid_set_lazy_put(cid);
10332 	if (!try_cmpxchg(&pcpu_cid->cid, &cid, lazy_cid))
10333 		return;
10334 
10335 	/*
10336 	 * The implicit barrier after cmpxchg per-mm/cpu cid before loading
10337 	 * rq->curr->mm matches the scheduler barrier in context_switch()
10338 	 * between store to rq->curr and load of prev and next task's
10339 	 * per-mm/cpu cid.
10340 	 *
10341 	 * The implicit barrier after cmpxchg per-mm/cpu cid before loading
10342 	 * rq->curr->mm_cid_active matches the barrier in
10343 	 * sched_mm_cid_exit_signals(), sched_mm_cid_before_execve(), and
10344 	 * sched_mm_cid_after_execve() between store to t->mm_cid_active and
10345 	 * load of per-mm/cpu cid.
10346 	 */
10347 
10348 	/*
10349 	 * If we observe an active task using the mm on this rq after setting
10350 	 * the lazy-put flag, that task will be responsible for transitioning
10351 	 * from lazy-put flag set to MM_CID_UNSET.
10352 	 */
10353 	scoped_guard (rcu) {
10354 		t = rcu_dereference(rq->curr);
10355 		if (READ_ONCE(t->mm_cid_active) && t->mm == mm)
10356 			return;
10357 	}
10358 
10359 	/*
10360 	 * The cid is unused, so it can be unset.
10361 	 * Disable interrupts to keep the window of cid ownership without rq
10362 	 * lock small.
10363 	 */
10364 	scoped_guard (irqsave) {
10365 		if (try_cmpxchg(&pcpu_cid->cid, &lazy_cid, MM_CID_UNSET))
10366 			__mm_cid_put(mm, cid);
10367 	}
10368 }
10369 
10370 static void sched_mm_cid_remote_clear_old(struct mm_struct *mm, int cpu)
10371 {
10372 	struct rq *rq = cpu_rq(cpu);
10373 	struct mm_cid *pcpu_cid;
10374 	struct task_struct *curr;
10375 	u64 rq_clock;
10376 
10377 	/*
10378 	 * rq->clock load is racy on 32-bit but one spurious clear once in a
10379 	 * while is irrelevant.
10380 	 */
10381 	rq_clock = READ_ONCE(rq->clock);
10382 	pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu);
10383 
10384 	/*
10385 	 * In order to take care of infrequently scheduled tasks, bump the time
10386 	 * snapshot associated with this cid if an active task using the mm is
10387 	 * observed on this rq.
10388 	 */
10389 	scoped_guard (rcu) {
10390 		curr = rcu_dereference(rq->curr);
10391 		if (READ_ONCE(curr->mm_cid_active) && curr->mm == mm) {
10392 			WRITE_ONCE(pcpu_cid->time, rq_clock);
10393 			return;
10394 		}
10395 	}
10396 
10397 	if (rq_clock < pcpu_cid->time + SCHED_MM_CID_PERIOD_NS)
10398 		return;
10399 	sched_mm_cid_remote_clear(mm, pcpu_cid, cpu);
10400 }
10401 
10402 static void sched_mm_cid_remote_clear_weight(struct mm_struct *mm, int cpu,
10403 					     int weight)
10404 {
10405 	struct mm_cid *pcpu_cid;
10406 	int cid;
10407 
10408 	pcpu_cid = per_cpu_ptr(mm->pcpu_cid, cpu);
10409 	cid = READ_ONCE(pcpu_cid->cid);
10410 	if (!mm_cid_is_valid(cid) || cid < weight)
10411 		return;
10412 	sched_mm_cid_remote_clear(mm, pcpu_cid, cpu);
10413 }
10414 
10415 static void task_mm_cid_work(struct callback_head *work)
10416 {
10417 	unsigned long now = jiffies, old_scan, next_scan;
10418 	struct task_struct *t = current;
10419 	struct cpumask *cidmask;
10420 	struct mm_struct *mm;
10421 	int weight, cpu;
10422 
10423 	SCHED_WARN_ON(t != container_of(work, struct task_struct, cid_work));
10424 
10425 	work->next = work;	/* Prevent double-add */
10426 	if (t->flags & PF_EXITING)
10427 		return;
10428 	mm = t->mm;
10429 	if (!mm)
10430 		return;
10431 	old_scan = READ_ONCE(mm->mm_cid_next_scan);
10432 	next_scan = now + msecs_to_jiffies(MM_CID_SCAN_DELAY);
10433 	if (!old_scan) {
10434 		unsigned long res;
10435 
10436 		res = cmpxchg(&mm->mm_cid_next_scan, old_scan, next_scan);
10437 		if (res != old_scan)
10438 			old_scan = res;
10439 		else
10440 			old_scan = next_scan;
10441 	}
10442 	if (time_before(now, old_scan))
10443 		return;
10444 	if (!try_cmpxchg(&mm->mm_cid_next_scan, &old_scan, next_scan))
10445 		return;
10446 	cidmask = mm_cidmask(mm);
10447 	/* Clear cids that were not recently used. */
10448 	for_each_possible_cpu(cpu)
10449 		sched_mm_cid_remote_clear_old(mm, cpu);
10450 	weight = cpumask_weight(cidmask);
10451 	/*
10452 	 * Clear cids that are greater or equal to the cidmask weight to
10453 	 * recompact it.
10454 	 */
10455 	for_each_possible_cpu(cpu)
10456 		sched_mm_cid_remote_clear_weight(mm, cpu, weight);
10457 }
10458 
10459 void init_sched_mm_cid(struct task_struct *t)
10460 {
10461 	struct mm_struct *mm = t->mm;
10462 	int mm_users = 0;
10463 
10464 	if (mm) {
10465 		mm_users = atomic_read(&mm->mm_users);
10466 		if (mm_users == 1)
10467 			mm->mm_cid_next_scan = jiffies + msecs_to_jiffies(MM_CID_SCAN_DELAY);
10468 	}
10469 	t->cid_work.next = &t->cid_work;	/* Protect against double add */
10470 	init_task_work(&t->cid_work, task_mm_cid_work);
10471 }
10472 
10473 void task_tick_mm_cid(struct rq *rq, struct task_struct *curr)
10474 {
10475 	struct callback_head *work = &curr->cid_work;
10476 	unsigned long now = jiffies;
10477 
10478 	if (!curr->mm || (curr->flags & (PF_EXITING | PF_KTHREAD)) ||
10479 	    work->next != work)
10480 		return;
10481 	if (time_before(now, READ_ONCE(curr->mm->mm_cid_next_scan)))
10482 		return;
10483 
10484 	/* No page allocation under rq lock */
10485 	task_work_add(curr, work, TWA_RESUME | TWAF_NO_ALLOC);
10486 }
10487 
10488 void sched_mm_cid_exit_signals(struct task_struct *t)
10489 {
10490 	struct mm_struct *mm = t->mm;
10491 	struct rq *rq;
10492 
10493 	if (!mm)
10494 		return;
10495 
10496 	preempt_disable();
10497 	rq = this_rq();
10498 	guard(rq_lock_irqsave)(rq);
10499 	preempt_enable_no_resched();	/* holding spinlock */
10500 	WRITE_ONCE(t->mm_cid_active, 0);
10501 	/*
10502 	 * Store t->mm_cid_active before loading per-mm/cpu cid.
10503 	 * Matches barrier in sched_mm_cid_remote_clear_old().
10504 	 */
10505 	smp_mb();
10506 	mm_cid_put(mm);
10507 	t->last_mm_cid = t->mm_cid = -1;
10508 }
10509 
10510 void sched_mm_cid_before_execve(struct task_struct *t)
10511 {
10512 	struct mm_struct *mm = t->mm;
10513 	struct rq *rq;
10514 
10515 	if (!mm)
10516 		return;
10517 
10518 	preempt_disable();
10519 	rq = this_rq();
10520 	guard(rq_lock_irqsave)(rq);
10521 	preempt_enable_no_resched();	/* holding spinlock */
10522 	WRITE_ONCE(t->mm_cid_active, 0);
10523 	/*
10524 	 * Store t->mm_cid_active before loading per-mm/cpu cid.
10525 	 * Matches barrier in sched_mm_cid_remote_clear_old().
10526 	 */
10527 	smp_mb();
10528 	mm_cid_put(mm);
10529 	t->last_mm_cid = t->mm_cid = -1;
10530 }
10531 
10532 void sched_mm_cid_after_execve(struct task_struct *t)
10533 {
10534 	struct mm_struct *mm = t->mm;
10535 	struct rq *rq;
10536 
10537 	if (!mm)
10538 		return;
10539 
10540 	preempt_disable();
10541 	rq = this_rq();
10542 	scoped_guard (rq_lock_irqsave, rq) {
10543 		preempt_enable_no_resched();	/* holding spinlock */
10544 		WRITE_ONCE(t->mm_cid_active, 1);
10545 		/*
10546 		 * Store t->mm_cid_active before loading per-mm/cpu cid.
10547 		 * Matches barrier in sched_mm_cid_remote_clear_old().
10548 		 */
10549 		smp_mb();
10550 		t->last_mm_cid = t->mm_cid = mm_cid_get(rq, mm);
10551 	}
10552 	rseq_set_notify_resume(t);
10553 }
10554 
10555 void sched_mm_cid_fork(struct task_struct *t)
10556 {
10557 	WARN_ON_ONCE(!t->mm || t->mm_cid != -1);
10558 	t->mm_cid_active = 1;
10559 }
10560 #endif
10561 
10562 #ifdef CONFIG_SCHED_CLASS_EXT
10563 void sched_deq_and_put_task(struct task_struct *p, int queue_flags,
10564 			    struct sched_enq_and_set_ctx *ctx)
10565 {
10566 	struct rq *rq = task_rq(p);
10567 
10568 	lockdep_assert_rq_held(rq);
10569 
10570 	*ctx = (struct sched_enq_and_set_ctx){
10571 		.p = p,
10572 		.queue_flags = queue_flags,
10573 		.queued = task_on_rq_queued(p),
10574 		.running = task_current(rq, p),
10575 	};
10576 
10577 	update_rq_clock(rq);
10578 	if (ctx->queued)
10579 		dequeue_task(rq, p, queue_flags | DEQUEUE_NOCLOCK);
10580 	if (ctx->running)
10581 		put_prev_task(rq, p);
10582 }
10583 
10584 void sched_enq_and_set_task(struct sched_enq_and_set_ctx *ctx)
10585 {
10586 	struct rq *rq = task_rq(ctx->p);
10587 
10588 	lockdep_assert_rq_held(rq);
10589 
10590 	if (ctx->queued)
10591 		enqueue_task(rq, ctx->p, ctx->queue_flags | ENQUEUE_NOCLOCK);
10592 	if (ctx->running)
10593 		set_next_task(rq, ctx->p);
10594 }
10595 #endif	/* CONFIG_SCHED_CLASS_EXT */
10596