File Index
Files and directories
Sign in
FILE

sched.h

File preview with syntax highlighting.

linux-6.19.14/kernel/sched/sched.h C
   1/* SPDX-License-Identifier: GPL-2.0 */
   2/*
   3 * Scheduler internal types and methods:
   4 */
   5#ifndef _KERNEL_SCHED_SCHED_H
   6#define _KERNEL_SCHED_SCHED_H
   7
   8#include <linux/prandom.h>
   9#include <linux/sched/affinity.h>
  10#include <linux/sched/autogroup.h>
  11#include <linux/sched/cpufreq.h>
  12#include <linux/sched/deadline.h>
  13#include <linux/sched.h>
  14#include <linux/sched/loadavg.h>
  15#include <linux/sched/mm.h>
  16#include <linux/sched/rseq_api.h>
  17#include <linux/sched/signal.h>
  18#include <linux/sched/smt.h>
  19#include <linux/sched/stat.h>
  20#include <linux/sched/sysctl.h>
  21#include <linux/sched/task_flags.h>
  22#include <linux/sched/task.h>
  23#include <linux/sched/topology.h>
  24#include <linux/atomic.h>
  25#include <linux/bitmap.h>
  26#include <linux/bug.h>
  27#include <linux/capability.h>
  28#include <linux/cgroup_api.h>
  29#include <linux/cgroup.h>
  30#include <linux/context_tracking.h>
  31#include <linux/cpufreq.h>
  32#include <linux/cpumask_api.h>
  33#include <linux/ctype.h>
  34#include <linux/file.h>
  35#include <linux/fs_api.h>
  36#include <linux/hrtimer_api.h>
  37#include <linux/interrupt.h>
  38#include <linux/irq_work.h>
  39#include <linux/jiffies.h>
  40#include <linux/kref_api.h>
  41#include <linux/kthread.h>
  42#include <linux/ktime_api.h>
  43#include <linux/lockdep_api.h>
  44#include <linux/lockdep.h>
  45#include <linux/minmax.h>
  46#include <linux/mm.h>
  47#include <linux/module.h>
  48#include <linux/mutex_api.h>
  49#include <linux/plist.h>
  50#include <linux/poll.h>
  51#include <linux/proc_fs.h>
  52#include <linux/profile.h>
  53#include <linux/psi.h>
  54#include <linux/rcupdate.h>
  55#include <linux/seq_file.h>
  56#include <linux/seqlock.h>
  57#include <linux/softirq.h>
  58#include <linux/spinlock_api.h>
  59#include <linux/static_key.h>
  60#include <linux/stop_machine.h>
  61#include <linux/syscalls_api.h>
  62#include <linux/syscalls.h>
  63#include <linux/tick.h>
  64#include <linux/topology.h>
  65#include <linux/types.h>
  66#include <linux/u64_stats_sync_api.h>
  67#include <linux/uaccess.h>
  68#include <linux/wait_api.h>
  69#include <linux/wait_bit.h>
  70#include <linux/workqueue_api.h>
  71#include <linux/delayacct.h>
  72#include <linux/mmu_context.h>
  73
  74#include <trace/events/power.h>
  75#include <trace/events/sched.h>
  76
  77#include "../workqueue_internal.h"
  78
  79struct rq;
  80struct cfs_rq;
  81struct rt_rq;
  82struct sched_group;
  83struct cpuidle_state;
  84
  85#ifdef CONFIG_PARAVIRT
  86# include <asm/paravirt.h>
  87# include <asm/paravirt_api_clock.h>
  88#endif
  89
  90#include <asm/barrier.h>
  91
  92#include "cpupri.h"
  93#include "cpudeadline.h"
  94
  95/* task_struct::on_rq states: */
  96#define TASK_ON_RQ_QUEUED	1
  97#define TASK_ON_RQ_MIGRATING	2
  98
  99extern __read_mostly int scheduler_running;
 100
 101extern unsigned long calc_load_update;
 102extern atomic_long_t calc_load_tasks;
 103
 104extern void calc_global_load_tick(struct rq *this_rq);
 105extern long calc_load_fold_active(struct rq *this_rq, long adjust);
 106
 107extern void call_trace_sched_update_nr_running(struct rq *rq, int count);
 108
 109extern int sysctl_sched_rt_period;
 110extern int sysctl_sched_rt_runtime;
 111extern int sched_rr_timeslice;
 112
 113/*
 114 * Asymmetric CPU capacity bits
 115 */
 116struct asym_cap_data {
 117	struct list_head link;
 118	struct rcu_head rcu;
 119	unsigned long capacity;
 120	unsigned long cpus[];
 121};
 122
 123extern struct list_head asym_cap_list;
 124
 125#define cpu_capacity_span(asym_data) to_cpumask((asym_data)->cpus)
 126
 127/*
 128 * Helpers for converting nanosecond timing to jiffy resolution
 129 */
 130#define NS_TO_JIFFIES(time)	((unsigned long)(time) / (NSEC_PER_SEC/HZ))
 131
 132/*
 133 * Increase resolution of nice-level calculations for 64-bit architectures.
 134 * The extra resolution improves shares distribution and load balancing of
 135 * low-weight task groups (eg. nice +19 on an autogroup), deeper task-group
 136 * hierarchies, especially on larger systems. This is not a user-visible change
 137 * and does not change the user-interface for setting shares/weights.
 138 *
 139 * We increase resolution only if we have enough bits to allow this increased
 140 * resolution (i.e. 64-bit). The costs for increasing resolution when 32-bit
 141 * are pretty high and the returns do not justify the increased costs.
 142 *
 143 * Really only required when CONFIG_FAIR_GROUP_SCHED=y is also set, but to
 144 * increase coverage and consistency always enable it on 64-bit platforms.
 145 */
 146#ifdef CONFIG_64BIT
 147# define NICE_0_LOAD_SHIFT	(SCHED_FIXEDPOINT_SHIFT + SCHED_FIXEDPOINT_SHIFT)
 148# define scale_load(w)		((w) << SCHED_FIXEDPOINT_SHIFT)
 149# define scale_load_down(w)					\
 150({								\
 151	unsigned long __w = (w);				\
 152								\
 153	if (__w)						\
 154		__w = max(2UL, __w >> SCHED_FIXEDPOINT_SHIFT);	\
 155	__w;							\
 156})
 157#else
 158# define NICE_0_LOAD_SHIFT	(SCHED_FIXEDPOINT_SHIFT)
 159# define scale_load(w)		(w)
 160# define scale_load_down(w)	(w)
 161#endif
 162
 163/*
 164 * Task weight (visible to users) and its load (invisible to users) have
 165 * independent resolution, but they should be well calibrated. We use
 166 * scale_load() and scale_load_down(w) to convert between them. The
 167 * following must be true:
 168 *
 169 *  scale_load(sched_prio_to_weight[NICE_TO_PRIO(0)-MAX_RT_PRIO]) == NICE_0_LOAD
 170 *
 171 */
 172#define NICE_0_LOAD		(1L << NICE_0_LOAD_SHIFT)
 173
 174/*
 175 * Single value that decides SCHED_DEADLINE internal math precision.
 176 * 10 -> just above 1us
 177 * 9  -> just above 0.5us
 178 */
 179#define DL_SCALE		10
 180
 181/*
 182 * Single value that denotes runtime == period, ie unlimited time.
 183 */
 184#define RUNTIME_INF		((u64)~0ULL)
 185
 186static inline int idle_policy(int policy)
 187{
 188	return policy == SCHED_IDLE;
 189}
 190
 191static inline int normal_policy(int policy)
 192{
 193#ifdef CONFIG_SCHED_CLASS_EXT
 194	if (policy == SCHED_EXT)
 195		return true;
 196#endif
 197	return policy == SCHED_NORMAL;
 198}
 199
 200static inline int fair_policy(int policy)
 201{
 202	return normal_policy(policy) || policy == SCHED_BATCH;
 203}
 204
 205static inline int rt_policy(int policy)
 206{
 207	return policy == SCHED_FIFO || policy == SCHED_RR;
 208}
 209
 210static inline int dl_policy(int policy)
 211{
 212	return policy == SCHED_DEADLINE;
 213}
 214
 215static inline bool valid_policy(int policy)
 216{
 217	return idle_policy(policy) || fair_policy(policy) ||
 218		rt_policy(policy) || dl_policy(policy);
 219}
 220
 221static inline int task_has_idle_policy(struct task_struct *p)
 222{
 223	return idle_policy(p->policy);
 224}
 225
 226static inline int task_has_rt_policy(struct task_struct *p)
 227{
 228	return rt_policy(p->policy);
 229}
 230
 231static inline int task_has_dl_policy(struct task_struct *p)
 232{
 233	return dl_policy(p->policy);
 234}
 235
 236#define cap_scale(v, s)		((v)*(s) >> SCHED_CAPACITY_SHIFT)
 237
 238static inline void update_avg(u64 *avg, u64 sample)
 239{
 240	s64 diff = sample - *avg;
 241
 242	*avg += diff / 8;
 243}
 244
 245/*
 246 * Shifting a value by an exponent greater *or equal* to the size of said value
 247 * is UB; cap at size-1.
 248 */
 249#define shr_bound(val, shift)							\
 250	(val >> min_t(typeof(shift), shift, BITS_PER_TYPE(typeof(val)) - 1))
 251
 252/*
 253 * cgroup weight knobs should use the common MIN, DFL and MAX values which are
 254 * 1, 100 and 10000 respectively. While it loses a bit of range on both ends, it
 255 * maps pretty well onto the shares value used by scheduler and the round-trip
 256 * conversions preserve the original value over the entire range.
 257 */
 258static inline unsigned long sched_weight_from_cgroup(unsigned long cgrp_weight)
 259{
 260	return DIV_ROUND_CLOSEST_ULL(cgrp_weight * 1024, CGROUP_WEIGHT_DFL);
 261}
 262
 263static inline unsigned long sched_weight_to_cgroup(unsigned long weight)
 264{
 265	return clamp_t(unsigned long,
 266		       DIV_ROUND_CLOSEST_ULL(weight * CGROUP_WEIGHT_DFL, 1024),
 267		       CGROUP_WEIGHT_MIN, CGROUP_WEIGHT_MAX);
 268}
 269
 270/*
 271 * !! For sched_setattr_nocheck() (kernel) only !!
 272 *
 273 * This is actually gross. :(
 274 *
 275 * It is used to make schedutil kworker(s) higher priority than SCHED_DEADLINE
 276 * tasks, but still be able to sleep. We need this on platforms that cannot
 277 * atomically change clock frequency. Remove once fast switching will be
 278 * available on such platforms.
 279 *
 280 * SUGOV stands for SchedUtil GOVernor.
 281 */
 282#define SCHED_FLAG_SUGOV	0x10000000
 283
 284#define SCHED_DL_FLAGS		(SCHED_FLAG_RECLAIM | SCHED_FLAG_DL_OVERRUN | SCHED_FLAG_SUGOV)
 285
 286static inline bool dl_entity_is_special(const struct sched_dl_entity *dl_se)
 287{
 288#ifdef CONFIG_CPU_FREQ_GOV_SCHEDUTIL
 289	return unlikely(dl_se->flags & SCHED_FLAG_SUGOV);
 290#else
 291	return false;
 292#endif
 293}
 294
 295/*
 296 * Tells if entity @a should preempt entity @b.
 297 */
 298static inline bool dl_entity_preempt(const struct sched_dl_entity *a,
 299				     const struct sched_dl_entity *b)
 300{
 301	return dl_entity_is_special(a) ||
 302	       dl_time_before(a->deadline, b->deadline);
 303}
 304
 305/*
 306 * This is the priority-queue data structure of the RT scheduling class:
 307 */
 308struct rt_prio_array {
 309	DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
 310	struct list_head queue[MAX_RT_PRIO];
 311};
 312
 313struct rt_bandwidth {
 314	/* nests inside the rq lock: */
 315	raw_spinlock_t		rt_runtime_lock;
 316	ktime_t			rt_period;
 317	u64			rt_runtime;
 318	struct hrtimer		rt_period_timer;
 319	unsigned int		rt_period_active;
 320};
 321
 322static inline int dl_bandwidth_enabled(void)
 323{
 324	return sysctl_sched_rt_runtime >= 0;
 325}
 326
 327/*
 328 * To keep the bandwidth of -deadline tasks under control
 329 * we need some place where:
 330 *  - store the maximum -deadline bandwidth of each cpu;
 331 *  - cache the fraction of bandwidth that is currently allocated in
 332 *    each root domain;
 333 *
 334 * This is all done in the data structure below. It is similar to the
 335 * one used for RT-throttling (rt_bandwidth), with the main difference
 336 * that, since here we are only interested in admission control, we
 337 * do not decrease any runtime while the group "executes", neither we
 338 * need a timer to replenish it.
 339 *
 340 * With respect to SMP, bandwidth is given on a per root domain basis,
 341 * meaning that:
 342 *  - bw (< 100%) is the deadline bandwidth of each CPU;
 343 *  - total_bw is the currently allocated bandwidth in each root domain;
 344 */
 345struct dl_bw {
 346	raw_spinlock_t		lock;
 347	u64			bw;
 348	u64			total_bw;
 349};
 350
 351extern void init_dl_bw(struct dl_bw *dl_b);
 352extern int  sched_dl_global_validate(void);
 353extern void sched_dl_do_global(void);
 354extern int  sched_dl_overflow(struct task_struct *p, int policy, const struct sched_attr *attr);
 355extern void __setparam_dl(struct task_struct *p, const struct sched_attr *attr);
 356extern void __getparam_dl(struct task_struct *p, struct sched_attr *attr);
 357extern bool __checkparam_dl(const struct sched_attr *attr);
 358extern bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr);
 359extern int  dl_cpuset_cpumask_can_shrink(const struct cpumask *cur, const struct cpumask *trial);
 360extern int  dl_bw_deactivate(int cpu);
 361extern s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec);
 362/*
 363 * SCHED_DEADLINE supports servers (nested scheduling) with the following
 364 * interface:
 365 *
 366 *   dl_se::rq -- runqueue we belong to.
 367 *
 368 *   dl_se::server_pick() -- nested pick_next_task(); we yield the period if this
 369 *                           returns NULL.
 370 *
 371 *   dl_server_update() -- called from update_curr_common(), propagates runtime
 372 *                         to the server.
 373 *
 374 *   dl_server_start() -- start the server when it has tasks; it will stop
 375 *			  automatically when there are no more tasks, per
 376 *			  dl_se::server_pick() returning NULL.
 377 *
 378 *   dl_server_stop() -- (force) stop the server; use when updating
 379 *                       parameters.
 380 *
 381 *   dl_server_init() -- initializes the server.
 382 *
 383 * When started the dl_server will (per dl_defer) schedule a timer for its
 384 * zero-laxity point -- that is, unlike regular EDF tasks which run ASAP, a
 385 * server will run at the very end of its period.
 386 *
 387 * This is done such that any runtime from the target class can be accounted
 388 * against the server -- through dl_server_update() above -- such that when it
 389 * becomes time to run, it might already be out of runtime and get deferred
 390 * until the next period. In this case dl_server_timer() will alternate
 391 * between defer and replenish but never actually enqueue the server.
 392 *
 393 * Only when the target class does not manage to exhaust the server's runtime
 394 * (there's actualy starvation in the given period), will the dl_server get on
 395 * the runqueue. Once queued it will pick tasks from the target class and run
 396 * them until either its runtime is exhaused, at which point its back to
 397 * dl_server_timer, or until there are no more tasks to run, at which point
 398 * the dl_server stops itself.
 399 *
 400 * By stopping at this point the dl_server retains bandwidth, which, if a new
 401 * task wakes up imminently (starting the server again), can be used --
 402 * subject to CBS wakeup rules -- without having to wait for the next period.
 403 *
 404 * Additionally, because of the dl_defer behaviour the start/stop behaviour is
 405 * naturally thottled to once per period, avoiding high context switch
 406 * workloads from spamming the hrtimer program/cancel paths.
 407 */
 408extern void dl_server_update_idle(struct sched_dl_entity *dl_se, s64 delta_exec);
 409extern void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec);
 410extern void dl_server_start(struct sched_dl_entity *dl_se);
 411extern void dl_server_stop(struct sched_dl_entity *dl_se);
 412extern void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
 413		    dl_server_pick_f pick_task);
 414extern void sched_init_dl_servers(void);
 415
 416extern void fair_server_init(struct rq *rq);
 417extern void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq);
 418extern int dl_server_apply_params(struct sched_dl_entity *dl_se,
 419		    u64 runtime, u64 period, bool init);
 420
 421static inline bool dl_server_active(struct sched_dl_entity *dl_se)
 422{
 423	return dl_se->dl_server_active;
 424}
 425
 426#ifdef CONFIG_CGROUP_SCHED
 427
 428extern struct list_head task_groups;
 429
 430#ifdef CONFIG_GROUP_SCHED_BANDWIDTH
 431extern const u64 max_bw_quota_period_us;
 432
 433/*
 434 * default period for group bandwidth.
 435 * default: 0.1s, units: microseconds
 436 */
 437static inline u64 default_bw_period_us(void)
 438{
 439	return 100000ULL;
 440}
 441#endif /* CONFIG_GROUP_SCHED_BANDWIDTH */
 442
 443struct cfs_bandwidth {
 444#ifdef CONFIG_CFS_BANDWIDTH
 445	raw_spinlock_t		lock;
 446	ktime_t			period;
 447	u64			quota;
 448	u64			runtime;
 449	u64			burst;
 450	u64			runtime_snap;
 451	s64			hierarchical_quota;
 452
 453	u8			idle;
 454	u8			period_active;
 455	u8			slack_started;
 456	struct hrtimer		period_timer;
 457	struct hrtimer		slack_timer;
 458	struct list_head	throttled_cfs_rq;
 459
 460	/* Statistics: */
 461	int			nr_periods;
 462	int			nr_throttled;
 463	int			nr_burst;
 464	u64			throttled_time;
 465	u64			burst_time;
 466#endif /* CONFIG_CFS_BANDWIDTH */
 467};
 468
 469/* Task group related information */
 470struct task_group {
 471	struct cgroup_subsys_state css;
 472
 473#ifdef CONFIG_GROUP_SCHED_WEIGHT
 474	/* A positive value indicates that this is a SCHED_IDLE group. */
 475	int			idle;
 476#endif
 477
 478#ifdef CONFIG_FAIR_GROUP_SCHED
 479	/* schedulable entities of this group on each CPU */
 480	struct sched_entity	**se;
 481	/* runqueue "owned" by this group on each CPU */
 482	struct cfs_rq		**cfs_rq;
 483	unsigned long		shares;
 484	/*
 485	 * load_avg can be heavily contended at clock tick time, so put
 486	 * it in its own cache-line separated from the fields above which
 487	 * will also be accessed at each tick.
 488	 */
 489	atomic_long_t		load_avg ____cacheline_aligned;
 490#endif /* CONFIG_FAIR_GROUP_SCHED */
 491
 492#ifdef CONFIG_RT_GROUP_SCHED
 493	struct sched_rt_entity	**rt_se;
 494	struct rt_rq		**rt_rq;
 495
 496	struct rt_bandwidth	rt_bandwidth;
 497#endif
 498
 499	struct scx_task_group	scx;
 500
 501	struct rcu_head		rcu;
 502	struct list_head	list;
 503
 504	struct task_group	*parent;
 505	struct list_head	siblings;
 506	struct list_head	children;
 507
 508#ifdef CONFIG_SCHED_AUTOGROUP
 509	struct autogroup	*autogroup;
 510#endif
 511
 512	struct cfs_bandwidth	cfs_bandwidth;
 513
 514#ifdef CONFIG_UCLAMP_TASK_GROUP
 515	/* The two decimal precision [%] value requested from user-space */
 516	unsigned int		uclamp_pct[UCLAMP_CNT];
 517	/* Clamp values requested for a task group */
 518	struct uclamp_se	uclamp_req[UCLAMP_CNT];
 519	/* Effective clamp values used for a task group */
 520	struct uclamp_se	uclamp[UCLAMP_CNT];
 521#endif
 522
 523};
 524
 525#ifdef CONFIG_GROUP_SCHED_WEIGHT
 526#define ROOT_TASK_GROUP_LOAD	NICE_0_LOAD
 527
 528/*
 529 * A weight of 0 or 1 can cause arithmetics problems.
 530 * A weight of a cfs_rq is the sum of weights of which entities
 531 * are queued on this cfs_rq, so a weight of a entity should not be
 532 * too large, so as the shares value of a task group.
 533 * (The default weight is 1024 - so there's no practical
 534 *  limitation from this.)
 535 */
 536#define MIN_SHARES		(1UL <<  1)
 537#define MAX_SHARES		(1UL << 18)
 538#endif
 539
 540typedef int (*tg_visitor)(struct task_group *, void *);
 541
 542extern int walk_tg_tree_from(struct task_group *from,
 543			     tg_visitor down, tg_visitor up, void *data);
 544
 545/*
 546 * Iterate the full tree, calling @down when first entering a node and @up when
 547 * leaving it for the final time.
 548 *
 549 * Caller must hold rcu_lock or sufficient equivalent.
 550 */
 551static inline int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
 552{
 553	return walk_tg_tree_from(&root_task_group, down, up, data);
 554}
 555
 556static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
 557{
 558	return css ? container_of(css, struct task_group, css) : NULL;
 559}
 560
 561extern int tg_nop(struct task_group *tg, void *data);
 562
 563#ifdef CONFIG_FAIR_GROUP_SCHED
 564extern void free_fair_sched_group(struct task_group *tg);
 565extern int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent);
 566extern void online_fair_sched_group(struct task_group *tg);
 567extern void unregister_fair_sched_group(struct task_group *tg);
 568#else /* !CONFIG_FAIR_GROUP_SCHED: */
 569static inline void free_fair_sched_group(struct task_group *tg) { }
 570static inline int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
 571{
 572       return 1;
 573}
 574static inline void online_fair_sched_group(struct task_group *tg) { }
 575static inline void unregister_fair_sched_group(struct task_group *tg) { }
 576#endif /* !CONFIG_FAIR_GROUP_SCHED */
 577
 578extern void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
 579			struct sched_entity *se, int cpu,
 580			struct sched_entity *parent);
 581extern void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b, struct cfs_bandwidth *parent);
 582
 583extern void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b);
 584extern void start_cfs_bandwidth(struct cfs_bandwidth *cfs_b);
 585extern void unthrottle_cfs_rq(struct cfs_rq *cfs_rq);
 586extern bool cfs_task_bw_constrained(struct task_struct *p);
 587
 588extern void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
 589		struct sched_rt_entity *rt_se, int cpu,
 590		struct sched_rt_entity *parent);
 591extern int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us);
 592extern int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us);
 593extern long sched_group_rt_runtime(struct task_group *tg);
 594extern long sched_group_rt_period(struct task_group *tg);
 595extern int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk);
 596
 597extern struct task_group *sched_create_group(struct task_group *parent);
 598extern void sched_online_group(struct task_group *tg,
 599			       struct task_group *parent);
 600extern void sched_destroy_group(struct task_group *tg);
 601extern void sched_release_group(struct task_group *tg);
 602
 603extern void sched_move_task(struct task_struct *tsk, bool for_autogroup);
 604
 605#ifdef CONFIG_FAIR_GROUP_SCHED
 606extern int sched_group_set_shares(struct task_group *tg, unsigned long shares);
 607
 608extern int sched_group_set_idle(struct task_group *tg, long idle);
 609
 610extern void set_task_rq_fair(struct sched_entity *se,
 611			     struct cfs_rq *prev, struct cfs_rq *next);
 612#else /* !CONFIG_FAIR_GROUP_SCHED: */
 613static inline int sched_group_set_shares(struct task_group *tg, unsigned long shares) { return 0; }
 614static inline int sched_group_set_idle(struct task_group *tg, long idle) { return 0; }
 615#endif /* !CONFIG_FAIR_GROUP_SCHED */
 616
 617#else /* !CONFIG_CGROUP_SCHED: */
 618
 619struct cfs_bandwidth { };
 620
 621static inline bool cfs_task_bw_constrained(struct task_struct *p) { return false; }
 622
 623#endif /* !CONFIG_CGROUP_SCHED */
 624
 625extern void unregister_rt_sched_group(struct task_group *tg);
 626extern void free_rt_sched_group(struct task_group *tg);
 627extern int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent);
 628
 629/*
 630 * u64_u32_load/u64_u32_store
 631 *
 632 * Use a copy of a u64 value to protect against data race. This is only
 633 * applicable for 32-bits architectures.
 634 */
 635#ifdef CONFIG_64BIT
 636# define u64_u32_load_copy(var, copy)		var
 637# define u64_u32_store_copy(var, copy, val)	(var = val)
 638#else
 639# define u64_u32_load_copy(var, copy)					\
 640({									\
 641	u64 __val, __val_copy;						\
 642	do {								\
 643		__val_copy = copy;					\
 644		/*							\
 645		 * paired with u64_u32_store_copy(), ordering access	\
 646		 * to var and copy.					\
 647		 */							\
 648		smp_rmb();						\
 649		__val = var;						\
 650	} while (__val != __val_copy);					\
 651	__val;								\
 652})
 653# define u64_u32_store_copy(var, copy, val)				\
 654do {									\
 655	typeof(val) __val = (val);					\
 656	var = __val;							\
 657	/*								\
 658	 * paired with u64_u32_load_copy(), ordering access to var and	\
 659	 * copy.							\
 660	 */								\
 661	smp_wmb();							\
 662	copy = __val;							\
 663} while (0)
 664#endif
 665# define u64_u32_load(var)		u64_u32_load_copy(var, var##_copy)
 666# define u64_u32_store(var, val)	u64_u32_store_copy(var, var##_copy, val)
 667
 668struct balance_callback {
 669	struct balance_callback *next;
 670	void (*func)(struct rq *rq);
 671};
 672
 673/* CFS-related fields in a runqueue */
 674struct cfs_rq {
 675	struct load_weight	load;
 676	unsigned int		nr_queued;
 677	unsigned int		h_nr_queued;       /* SCHED_{NORMAL,BATCH,IDLE} */
 678	unsigned int		h_nr_runnable;     /* SCHED_{NORMAL,BATCH,IDLE} */
 679	unsigned int		h_nr_idle; /* SCHED_IDLE */
 680
 681	s64			sum_w_vruntime;
 682	u64			sum_weight;
 683
 684	u64			zero_vruntime;
 685#ifdef CONFIG_SCHED_CORE
 686	unsigned int		forceidle_seq;
 687	u64			zero_vruntime_fi;
 688#endif
 689
 690	struct rb_root_cached	tasks_timeline;
 691
 692	/*
 693	 * 'curr' points to currently running entity on this cfs_rq.
 694	 * It is set to NULL otherwise (i.e when none are currently running).
 695	 */
 696	struct sched_entity	*curr;
 697	struct sched_entity	*next;
 698
 699	/*
 700	 * CFS load tracking
 701	 */
 702	struct sched_avg	avg;
 703#ifndef CONFIG_64BIT
 704	u64			last_update_time_copy;
 705#endif
 706	struct {
 707		raw_spinlock_t	lock ____cacheline_aligned;
 708		int		nr;
 709		unsigned long	load_avg;
 710		unsigned long	util_avg;
 711		unsigned long	runnable_avg;
 712	} removed;
 713
 714#ifdef CONFIG_FAIR_GROUP_SCHED
 715	u64			last_update_tg_load_avg;
 716	unsigned long		tg_load_avg_contrib;
 717	long			propagate;
 718	long			prop_runnable_sum;
 719
 720	/*
 721	 *   h_load = weight * f(tg)
 722	 *
 723	 * Where f(tg) is the recursive weight fraction assigned to
 724	 * this group.
 725	 */
 726	unsigned long		h_load;
 727	u64			last_h_load_update;
 728	struct sched_entity	*h_load_next;
 729#endif /* CONFIG_FAIR_GROUP_SCHED */
 730
 731#ifdef CONFIG_FAIR_GROUP_SCHED
 732	struct rq		*rq;	/* CPU runqueue to which this cfs_rq is attached */
 733
 734	/*
 735	 * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
 736	 * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
 737	 * (like users, containers etc.)
 738	 *
 739	 * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a CPU.
 740	 * This list is used during load balance.
 741	 */
 742	int			on_list;
 743	struct list_head	leaf_cfs_rq_list;
 744	struct task_group	*tg;	/* group that "owns" this runqueue */
 745
 746	/* Locally cached copy of our task_group's idle value */
 747	int			idle;
 748
 749#ifdef CONFIG_CFS_BANDWIDTH
 750	int			runtime_enabled;
 751	s64			runtime_remaining;
 752
 753	u64			throttled_pelt_idle;
 754#ifndef CONFIG_64BIT
 755	u64                     throttled_pelt_idle_copy;
 756#endif
 757	u64			throttled_clock;
 758	u64			throttled_clock_pelt;
 759	u64			throttled_clock_pelt_time;
 760	u64			throttled_clock_self;
 761	u64			throttled_clock_self_time;
 762	bool			throttled:1;
 763	bool			pelt_clock_throttled:1;
 764	int			throttle_count;
 765	struct list_head	throttled_list;
 766	struct list_head	throttled_csd_list;
 767	struct list_head        throttled_limbo_list;
 768#endif /* CONFIG_CFS_BANDWIDTH */
 769#endif /* CONFIG_FAIR_GROUP_SCHED */
 770};
 771
 772#ifdef CONFIG_SCHED_CLASS_EXT
 773/* scx_rq->flags, protected by the rq lock */
 774enum scx_rq_flags {
 775	/*
 776	 * A hotplugged CPU starts scheduling before rq_online_scx(). Track
 777	 * ops.cpu_on/offline() state so that ops.enqueue/dispatch() are called
 778	 * only while the BPF scheduler considers the CPU to be online.
 779	 */
 780	SCX_RQ_ONLINE		= 1 << 0,
 781	SCX_RQ_CAN_STOP_TICK	= 1 << 1,
 782	SCX_RQ_BAL_KEEP		= 1 << 3, /* balance decided to keep current */
 783	SCX_RQ_BYPASSING	= 1 << 4,
 784	SCX_RQ_CLK_VALID	= 1 << 5, /* RQ clock is fresh and valid */
 785	SCX_RQ_BAL_CB_PENDING	= 1 << 6, /* must queue a cb after dispatching */
 786
 787	SCX_RQ_IN_WAKEUP	= 1 << 16,
 788	SCX_RQ_IN_BALANCE	= 1 << 17,
 789};
 790
 791struct scx_rq {
 792	struct scx_dispatch_q	local_dsq;
 793	struct list_head	runnable_list;		/* runnable tasks on this rq */
 794	struct list_head	ddsp_deferred_locals;	/* deferred ddsps from enq */
 795	unsigned long		ops_qseq;
 796	u64			extra_enq_flags;	/* see move_task_to_local_dsq() */
 797	u32			nr_running;
 798	u32			cpuperf_target;		/* [0, SCHED_CAPACITY_SCALE] */
 799	bool			cpu_released;
 800	u32			flags;
 801	u64			clock;			/* current per-rq clock -- see scx_bpf_now() */
 802	cpumask_var_t		cpus_to_kick;
 803	cpumask_var_t		cpus_to_kick_if_idle;
 804	cpumask_var_t		cpus_to_preempt;
 805	cpumask_var_t		cpus_to_wait;
 806	cpumask_var_t		cpus_to_sync;
 807	bool			kick_sync_pending;
 808	unsigned long		kick_sync;
 809	local_t			reenq_local_deferred;
 810	struct balance_callback	deferred_bal_cb;
 811	struct balance_callback	kick_sync_bal_cb;
 812	struct irq_work		deferred_irq_work;
 813	struct irq_work		kick_cpus_irq_work;
 814	struct scx_dispatch_q	bypass_dsq;
 815};
 816#endif /* CONFIG_SCHED_CLASS_EXT */
 817
 818static inline int rt_bandwidth_enabled(void)
 819{
 820	return sysctl_sched_rt_runtime >= 0;
 821}
 822
 823/* RT IPI pull logic requires IRQ_WORK */
 824#if defined(CONFIG_IRQ_WORK) && defined(CONFIG_SMP)
 825# define HAVE_RT_PUSH_IPI
 826#endif
 827
 828/* Real-Time classes' related field in a runqueue: */
 829struct rt_rq {
 830	struct rt_prio_array	active;
 831	unsigned int		rt_nr_running;
 832	unsigned int		rr_nr_running;
 833	struct {
 834		int		curr; /* highest queued rt task prio */
 835		int		next; /* next highest */
 836	} highest_prio;
 837	bool			overloaded;
 838	struct plist_head	pushable_tasks;
 839
 840	int			rt_queued;
 841
 842#ifdef CONFIG_RT_GROUP_SCHED
 843	int			rt_throttled;
 844	u64			rt_time; /* consumed RT time, goes up in update_curr_rt */
 845	u64			rt_runtime; /* allotted RT time, "slice" from rt_bandwidth, RT sharing/balancing */
 846	/* Nests inside the rq lock: */
 847	raw_spinlock_t		rt_runtime_lock;
 848
 849	unsigned int		rt_nr_boosted;
 850
 851	struct rq		*rq; /* this is always top-level rq, cache? */
 852#endif
 853#ifdef CONFIG_CGROUP_SCHED
 854	struct task_group	*tg; /* this tg has "this" rt_rq on given CPU for runnable entities */
 855#endif
 856};
 857
 858static inline bool rt_rq_is_runnable(struct rt_rq *rt_rq)
 859{
 860	return rt_rq->rt_queued && rt_rq->rt_nr_running;
 861}
 862
 863/* Deadline class' related fields in a runqueue */
 864struct dl_rq {
 865	/* runqueue is an rbtree, ordered by deadline */
 866	struct rb_root_cached	root;
 867
 868	unsigned int		dl_nr_running;
 869
 870	/*
 871	 * Deadline values of the currently executing and the
 872	 * earliest ready task on this rq. Caching these facilitates
 873	 * the decision whether or not a ready but not running task
 874	 * should migrate somewhere else.
 875	 */
 876	struct {
 877		u64		curr;
 878		u64		next;
 879	} earliest_dl;
 880
 881	bool			overloaded;
 882
 883	/*
 884	 * Tasks on this rq that can be pushed away. They are kept in
 885	 * an rb-tree, ordered by tasks' deadlines, with caching
 886	 * of the leftmost (earliest deadline) element.
 887	 */
 888	struct rb_root_cached	pushable_dl_tasks_root;
 889
 890	/*
 891	 * "Active utilization" for this runqueue: increased when a
 892	 * task wakes up (becomes TASK_RUNNING) and decreased when a
 893	 * task blocks
 894	 */
 895	u64			running_bw;
 896
 897	/*
 898	 * Utilization of the tasks "assigned" to this runqueue (including
 899	 * the tasks that are in runqueue and the tasks that executed on this
 900	 * CPU and blocked). Increased when a task moves to this runqueue, and
 901	 * decreased when the task moves away (migrates, changes scheduling
 902	 * policy, or terminates).
 903	 * This is needed to compute the "inactive utilization" for the
 904	 * runqueue (inactive utilization = this_bw - running_bw).
 905	 */
 906	u64			this_bw;
 907	u64			extra_bw;
 908
 909	/*
 910	 * Maximum available bandwidth for reclaiming by SCHED_FLAG_RECLAIM
 911	 * tasks of this rq. Used in calculation of reclaimable bandwidth(GRUB).
 912	 */
 913	u64			max_bw;
 914
 915	/*
 916	 * Inverse of the fraction of CPU utilization that can be reclaimed
 917	 * by the GRUB algorithm.
 918	 */
 919	u64			bw_ratio;
 920};
 921
 922#ifdef CONFIG_FAIR_GROUP_SCHED
 923
 924/* An entity is a task if it doesn't "own" a runqueue */
 925#define entity_is_task(se)	(!se->my_q)
 926
 927static inline void se_update_runnable(struct sched_entity *se)
 928{
 929	if (!entity_is_task(se))
 930		se->runnable_weight = se->my_q->h_nr_runnable;
 931}
 932
 933static inline long se_runnable(struct sched_entity *se)
 934{
 935	if (se->sched_delayed)
 936		return false;
 937
 938	if (entity_is_task(se))
 939		return !!se->on_rq;
 940	else
 941		return se->runnable_weight;
 942}
 943
 944#else /* !CONFIG_FAIR_GROUP_SCHED: */
 945
 946#define entity_is_task(se)	1
 947
 948static inline void se_update_runnable(struct sched_entity *se) { }
 949
 950static inline long se_runnable(struct sched_entity *se)
 951{
 952	if (se->sched_delayed)
 953		return false;
 954
 955	return !!se->on_rq;
 956}
 957
 958#endif /* !CONFIG_FAIR_GROUP_SCHED */
 959
 960/*
 961 * XXX we want to get rid of these helpers and use the full load resolution.
 962 */
 963static inline long se_weight(struct sched_entity *se)
 964{
 965	return scale_load_down(se->load.weight);
 966}
 967
 968
 969static inline bool sched_asym_prefer(int a, int b)
 970{
 971	return arch_asym_cpu_priority(a) > arch_asym_cpu_priority(b);
 972}
 973
 974struct perf_domain {
 975	struct em_perf_domain *em_pd;
 976	struct perf_domain *next;
 977	struct rcu_head rcu;
 978};
 979
 980/*
 981 * We add the notion of a root-domain which will be used to define per-domain
 982 * variables. Each exclusive cpuset essentially defines an island domain by
 983 * fully partitioning the member CPUs from any other cpuset. Whenever a new
 984 * exclusive cpuset is created, we also create and attach a new root-domain
 985 * object.
 986 *
 987 */
 988struct root_domain {
 989	atomic_t		refcount;
 990	atomic_t		rto_count;
 991	struct rcu_head		rcu;
 992	cpumask_var_t		span;
 993	cpumask_var_t		online;
 994
 995	/*
 996	 * Indicate pullable load on at least one CPU, e.g:
 997	 * - More than one runnable task
 998	 * - Running task is misfit
 999	 */
1000	bool			overloaded;
1001
1002	/* Indicate one or more CPUs over-utilized (tipping point) */
1003	bool			overutilized;
1004
1005	/*
1006	 * The bit corresponding to a CPU gets set here if such CPU has more
1007	 * than one runnable -deadline task (as it is below for RT tasks).
1008	 */
1009	cpumask_var_t		dlo_mask;
1010	atomic_t		dlo_count;
1011	struct dl_bw		dl_bw;
1012	struct cpudl		cpudl;
1013
1014	/*
1015	 * Indicate whether a root_domain's dl_bw has been checked or
1016	 * updated. It's monotonously increasing value.
1017	 *
1018	 * Also, some corner cases, like 'wrap around' is dangerous, but given
1019	 * that u64 is 'big enough'. So that shouldn't be a concern.
1020	 */
1021	u64 visit_cookie;
1022
1023#ifdef HAVE_RT_PUSH_IPI
1024	/*
1025	 * For IPI pull requests, loop across the rto_mask.
1026	 */
1027	struct irq_work		rto_push_work;
1028	raw_spinlock_t		rto_lock;
1029	/* These are only updated and read within rto_lock */
1030	int			rto_loop;
1031	int			rto_cpu;
1032	/* These atomics are updated outside of a lock */
1033	atomic_t		rto_loop_next;
1034	atomic_t		rto_loop_start;
1035#endif /* HAVE_RT_PUSH_IPI */
1036	/*
1037	 * The "RT overload" flag: it gets set if a CPU has more than
1038	 * one runnable RT task.
1039	 */
1040	cpumask_var_t		rto_mask;
1041	struct cpupri		cpupri;
1042
1043	/*
1044	 * NULL-terminated list of performance domains intersecting with the
1045	 * CPUs of the rd. Protected by RCU.
1046	 */
1047	struct perf_domain __rcu *pd;
1048};
1049
1050extern void init_defrootdomain(void);
1051extern int sched_init_domains(const struct cpumask *cpu_map);
1052extern void rq_attach_root(struct rq *rq, struct root_domain *rd);
1053extern void sched_get_rd(struct root_domain *rd);
1054extern void sched_put_rd(struct root_domain *rd);
1055
1056static inline int get_rd_overloaded(struct root_domain *rd)
1057{
1058	return READ_ONCE(rd->overloaded);
1059}
1060
1061static inline void set_rd_overloaded(struct root_domain *rd, int status)
1062{
1063	if (get_rd_overloaded(rd) != status)
1064		WRITE_ONCE(rd->overloaded, status);
1065}
1066
1067#ifdef HAVE_RT_PUSH_IPI
1068extern void rto_push_irq_work_func(struct irq_work *work);
1069#endif
1070
1071#ifdef CONFIG_UCLAMP_TASK
1072/*
1073 * struct uclamp_bucket - Utilization clamp bucket
1074 * @value: utilization clamp value for tasks on this clamp bucket
1075 * @tasks: number of RUNNABLE tasks on this clamp bucket
1076 *
1077 * Keep track of how many tasks are RUNNABLE for a given utilization
1078 * clamp value.
1079 */
1080struct uclamp_bucket {
1081	unsigned long value : bits_per(SCHED_CAPACITY_SCALE);
1082	unsigned long tasks : BITS_PER_LONG - bits_per(SCHED_CAPACITY_SCALE);
1083};
1084
1085/*
1086 * struct uclamp_rq - rq's utilization clamp
1087 * @value: currently active clamp values for a rq
1088 * @bucket: utilization clamp buckets affecting a rq
1089 *
1090 * Keep track of RUNNABLE tasks on a rq to aggregate their clamp values.
1091 * A clamp value is affecting a rq when there is at least one task RUNNABLE
1092 * (or actually running) with that value.
1093 *
1094 * There are up to UCLAMP_CNT possible different clamp values, currently there
1095 * are only two: minimum utilization and maximum utilization.
1096 *
1097 * All utilization clamping values are MAX aggregated, since:
1098 * - for util_min: we want to run the CPU at least at the max of the minimum
1099 *   utilization required by its currently RUNNABLE tasks.
1100 * - for util_max: we want to allow the CPU to run up to the max of the
1101 *   maximum utilization allowed by its currently RUNNABLE tasks.
1102 *
1103 * Since on each system we expect only a limited number of different
1104 * utilization clamp values (UCLAMP_BUCKETS), use a simple array to track
1105 * the metrics required to compute all the per-rq utilization clamp values.
1106 */
1107struct uclamp_rq {
1108	unsigned int value;
1109	struct uclamp_bucket bucket[UCLAMP_BUCKETS];
1110};
1111
1112DECLARE_STATIC_KEY_FALSE(sched_uclamp_used);
1113#endif /* CONFIG_UCLAMP_TASK */
1114
1115/*
1116 * This is the main, per-CPU runqueue data structure.
1117 *
1118 * Locking rule: those places that want to lock multiple runqueues
1119 * (such as the load balancing or the thread migration code), lock
1120 * acquire operations must be ordered by ascending &runqueue.
1121 */
1122struct rq {
1123	/* runqueue lock: */
1124	raw_spinlock_t		__lock;
1125
1126	/* Per class runqueue modification mask; bits in class order. */
1127	unsigned int		queue_mask;
1128	unsigned int		nr_running;
1129#ifdef CONFIG_NUMA_BALANCING
1130	unsigned int		nr_numa_running;
1131	unsigned int		nr_preferred_running;
1132	unsigned int		numa_migrate_on;
1133#endif
1134#ifdef CONFIG_NO_HZ_COMMON
1135	unsigned long		last_blocked_load_update_tick;
1136	unsigned int		has_blocked_load;
1137	call_single_data_t	nohz_csd;
1138	unsigned int		nohz_tick_stopped;
1139	atomic_t		nohz_flags;
1140#endif /* CONFIG_NO_HZ_COMMON */
1141
1142	unsigned int		ttwu_pending;
1143	u64			nr_switches;
1144
1145#ifdef CONFIG_UCLAMP_TASK
1146	/* Utilization clamp values based on CPU's RUNNABLE tasks */
1147	struct uclamp_rq	uclamp[UCLAMP_CNT] ____cacheline_aligned;
1148	unsigned int		uclamp_flags;
1149#define UCLAMP_FLAG_IDLE 0x01
1150#endif
1151
1152	struct cfs_rq		cfs;
1153	struct rt_rq		rt;
1154	struct dl_rq		dl;
1155#ifdef CONFIG_SCHED_CLASS_EXT
1156	struct scx_rq		scx;
1157#endif
1158
1159	struct sched_dl_entity	fair_server;
1160
1161#ifdef CONFIG_FAIR_GROUP_SCHED
1162	/* list of leaf cfs_rq on this CPU: */
1163	struct list_head	leaf_cfs_rq_list;
1164	struct list_head	*tmp_alone_branch;
1165#endif /* CONFIG_FAIR_GROUP_SCHED */
1166
1167	/*
1168	 * This is part of a global counter where only the total sum
1169	 * over all CPUs matters. A task can increase this counter on
1170	 * one CPU and if it got migrated afterwards it may decrease
1171	 * it on another CPU. Always updated under the runqueue lock:
1172	 */
1173	unsigned long		nr_uninterruptible;
1174
1175#ifdef CONFIG_SCHED_PROXY_EXEC
1176	struct task_struct __rcu	*donor;  /* Scheduling context */
1177	struct task_struct __rcu	*curr;   /* Execution context */
1178#else
1179	union {
1180		struct task_struct __rcu *donor; /* Scheduler context */
1181		struct task_struct __rcu *curr;  /* Execution context */
1182	};
1183#endif
1184	struct sched_dl_entity	*dl_server;
1185	struct task_struct	*idle;
1186	struct task_struct	*stop;
1187	unsigned long		next_balance;
1188	struct mm_struct	*prev_mm;
1189
1190	unsigned int		clock_update_flags;
1191	u64			clock;
1192	/* Ensure that all clocks are in the same cache line */
1193	u64			clock_task ____cacheline_aligned;
1194	u64			clock_pelt;
1195	unsigned long		lost_idle_time;
1196	u64			clock_pelt_idle;
1197	u64			clock_idle;
1198#ifndef CONFIG_64BIT
1199	u64			clock_pelt_idle_copy;
1200	u64			clock_idle_copy;
1201#endif
1202
1203	atomic_t		nr_iowait;
1204
1205	u64 last_seen_need_resched_ns;
1206	int ticks_without_resched;
1207
1208#ifdef CONFIG_MEMBARRIER
1209	int membarrier_state;
1210#endif
1211
1212	struct root_domain		*rd;
1213	struct sched_domain __rcu	*sd;
1214
1215	unsigned long		cpu_capacity;
1216
1217	struct balance_callback *balance_callback;
1218
1219	unsigned char		nohz_idle_balance;
1220	unsigned char		idle_balance;
1221
1222	unsigned long		misfit_task_load;
1223
1224	/* For active balancing */
1225	int			active_balance;
1226	int			push_cpu;
1227	struct cpu_stop_work	active_balance_work;
1228
1229	/* CPU of this runqueue: */
1230	int			cpu;
1231	int			online;
1232
1233	struct list_head cfs_tasks;
1234
1235	struct sched_avg	avg_rt;
1236	struct sched_avg	avg_dl;
1237#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
1238	struct sched_avg	avg_irq;
1239#endif
1240#ifdef CONFIG_SCHED_HW_PRESSURE
1241	struct sched_avg	avg_hw;
1242#endif
1243	u64			idle_stamp;
1244	u64			avg_idle;
1245
1246	/* This is used to determine avg_idle's max value */
1247	u64			max_idle_balance_cost;
1248
1249#ifdef CONFIG_HOTPLUG_CPU
1250	struct rcuwait		hotplug_wait;
1251#endif
1252
1253#ifdef CONFIG_IRQ_TIME_ACCOUNTING
1254	u64			prev_irq_time;
1255	u64			psi_irq_time;
1256#endif
1257#ifdef CONFIG_PARAVIRT
1258	u64			prev_steal_time;
1259#endif
1260#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
1261	u64			prev_steal_time_rq;
1262#endif
1263
1264	/* calc_load related fields */
1265	unsigned long		calc_load_update;
1266	long			calc_load_active;
1267
1268#ifdef CONFIG_SCHED_HRTICK
1269	call_single_data_t	hrtick_csd;
1270	struct hrtimer		hrtick_timer;
1271	ktime_t			hrtick_time;
1272#endif
1273
1274#ifdef CONFIG_SCHEDSTATS
1275	/* latency stats */
1276	struct sched_info	rq_sched_info;
1277	unsigned long long	rq_cpu_time;
1278
1279	/* sys_sched_yield() stats */
1280	unsigned int		yld_count;
1281
1282	/* schedule() stats */
1283	unsigned int		sched_count;
1284	unsigned int		sched_goidle;
1285
1286	/* try_to_wake_up() stats */
1287	unsigned int		ttwu_count;
1288	unsigned int		ttwu_local;
1289#endif
1290
1291#ifdef CONFIG_CPU_IDLE
1292	/* Must be inspected within a RCU lock section */
1293	struct cpuidle_state	*idle_state;
1294#endif
1295
1296	unsigned int		nr_pinned;
1297	unsigned int		push_busy;
1298	struct cpu_stop_work	push_work;
1299
1300#ifdef CONFIG_SCHED_CORE
1301	/* per rq */
1302	struct rq		*core;
1303	struct task_struct	*core_pick;
1304	struct sched_dl_entity	*core_dl_server;
1305	unsigned int		core_enabled;
1306	unsigned int		core_sched_seq;
1307	struct rb_root		core_tree;
1308
1309	/* shared state -- careful with sched_core_cpu_deactivate() */
1310	unsigned int		core_task_seq;
1311	unsigned int		core_pick_seq;
1312	unsigned long		core_cookie;
1313	unsigned int		core_forceidle_count;
1314	unsigned int		core_forceidle_seq;
1315	unsigned int		core_forceidle_occupation;
1316	u64			core_forceidle_start;
1317#endif /* CONFIG_SCHED_CORE */
1318
1319	/* Scratch cpumask to be temporarily used under rq_lock */
1320	cpumask_var_t		scratch_mask;
1321
1322#ifdef CONFIG_CFS_BANDWIDTH
1323	call_single_data_t	cfsb_csd;
1324	struct list_head	cfsb_csd_list;
1325#endif
1326};
1327
1328#ifdef CONFIG_FAIR_GROUP_SCHED
1329
1330/* CPU runqueue to which this cfs_rq is attached */
1331static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1332{
1333	return cfs_rq->rq;
1334}
1335
1336#else /* !CONFIG_FAIR_GROUP_SCHED: */
1337
1338static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
1339{
1340	return container_of(cfs_rq, struct rq, cfs);
1341}
1342#endif /* !CONFIG_FAIR_GROUP_SCHED */
1343
1344static inline int cpu_of(struct rq *rq)
1345{
1346	return rq->cpu;
1347}
1348
1349#define MDF_PUSH		0x01
1350
1351static inline bool is_migration_disabled(struct task_struct *p)
1352{
1353	return p->migration_disabled;
1354}
1355
1356DECLARE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
1357DECLARE_PER_CPU(struct rnd_state, sched_rnd_state);
1358
1359static inline u32 sched_rng(void)
1360{
1361	return prandom_u32_state(this_cpu_ptr(&sched_rnd_state));
1362}
1363
1364#define cpu_rq(cpu)		(&per_cpu(runqueues, (cpu)))
1365#define this_rq()		this_cpu_ptr(&runqueues)
1366#define task_rq(p)		cpu_rq(task_cpu(p))
1367#define cpu_curr(cpu)		(cpu_rq(cpu)->curr)
1368#define raw_rq()		raw_cpu_ptr(&runqueues)
1369
1370static inline bool idle_rq(struct rq *rq)
1371{
1372	return rq->curr == rq->idle && !rq->nr_running && !rq->ttwu_pending;
1373}
1374
1375/**
1376 * available_idle_cpu - is a given CPU idle for enqueuing work.
1377 * @cpu: the CPU in question.
1378 *
1379 * Return: 1 if the CPU is currently idle. 0 otherwise.
1380 */
1381static inline bool available_idle_cpu(int cpu)
1382{
1383	if (!idle_rq(cpu_rq(cpu)))
1384		return 0;
1385
1386	if (vcpu_is_preempted(cpu))
1387		return 0;
1388
1389	return 1;
1390}
1391
1392#ifdef CONFIG_SCHED_PROXY_EXEC
1393static inline void rq_set_donor(struct rq *rq, struct task_struct *t)
1394{
1395	rcu_assign_pointer(rq->donor, t);
1396}
1397#else
1398static inline void rq_set_donor(struct rq *rq, struct task_struct *t)
1399{
1400	/* Do nothing */
1401}
1402#endif
1403
1404#ifdef CONFIG_SCHED_CORE
1405static inline struct cpumask *sched_group_span(struct sched_group *sg);
1406
1407DECLARE_STATIC_KEY_FALSE(__sched_core_enabled);
1408
1409static inline bool sched_core_enabled(struct rq *rq)
1410{
1411	return static_branch_unlikely(&__sched_core_enabled) && rq->core_enabled;
1412}
1413
1414static inline bool sched_core_disabled(void)
1415{
1416	return !static_branch_unlikely(&__sched_core_enabled);
1417}
1418
1419/*
1420 * Be careful with this function; not for general use. The return value isn't
1421 * stable unless you actually hold a relevant rq->__lock.
1422 */
1423static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1424{
1425	if (sched_core_enabled(rq))
1426		return &rq->core->__lock;
1427
1428	return &rq->__lock;
1429}
1430
1431static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1432{
1433	if (rq->core_enabled)
1434		return &rq->core->__lock;
1435
1436	return &rq->__lock;
1437}
1438
1439extern bool
1440cfs_prio_less(const struct task_struct *a, const struct task_struct *b, bool fi);
1441
1442extern void task_vruntime_update(struct rq *rq, struct task_struct *p, bool in_fi);
1443
1444/*
1445 * Helpers to check if the CPU's core cookie matches with the task's cookie
1446 * when core scheduling is enabled.
1447 * A special case is that the task's cookie always matches with CPU's core
1448 * cookie if the CPU is in an idle core.
1449 */
1450static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1451{
1452	/* Ignore cookie match if core scheduler is not enabled on the CPU. */
1453	if (!sched_core_enabled(rq))
1454		return true;
1455
1456	return rq->core->core_cookie == p->core_cookie;
1457}
1458
1459static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1460{
1461	bool idle_core = true;
1462	int cpu;
1463
1464	/* Ignore cookie match if core scheduler is not enabled on the CPU. */
1465	if (!sched_core_enabled(rq))
1466		return true;
1467
1468	if (rq->core->core_cookie == p->core_cookie)
1469		return true;
1470
1471	for_each_cpu(cpu, cpu_smt_mask(cpu_of(rq))) {
1472		if (!available_idle_cpu(cpu)) {
1473			idle_core = false;
1474			break;
1475		}
1476	}
1477
1478	/*
1479	 * A CPU in an idle core is always the best choice for tasks with
1480	 * cookies.
1481	 */
1482	return idle_core;
1483}
1484
1485static inline bool sched_group_cookie_match(struct rq *rq,
1486					    struct task_struct *p,
1487					    struct sched_group *group)
1488{
1489	int cpu;
1490
1491	/* Ignore cookie match if core scheduler is not enabled on the CPU. */
1492	if (!sched_core_enabled(rq))
1493		return true;
1494
1495	for_each_cpu_and(cpu, sched_group_span(group), p->cpus_ptr) {
1496		if (sched_core_cookie_match(cpu_rq(cpu), p))
1497			return true;
1498	}
1499	return false;
1500}
1501
1502static inline bool sched_core_enqueued(struct task_struct *p)
1503{
1504	return !RB_EMPTY_NODE(&p->core_node);
1505}
1506
1507extern void sched_core_enqueue(struct rq *rq, struct task_struct *p);
1508extern void sched_core_dequeue(struct rq *rq, struct task_struct *p, int flags);
1509
1510extern void sched_core_get(void);
1511extern void sched_core_put(void);
1512
1513#else /* !CONFIG_SCHED_CORE: */
1514
1515static inline bool sched_core_enabled(struct rq *rq)
1516{
1517	return false;
1518}
1519
1520static inline bool sched_core_disabled(void)
1521{
1522	return true;
1523}
1524
1525static inline raw_spinlock_t *rq_lockp(struct rq *rq)
1526{
1527	return &rq->__lock;
1528}
1529
1530static inline raw_spinlock_t *__rq_lockp(struct rq *rq)
1531{
1532	return &rq->__lock;
1533}
1534
1535static inline bool sched_cpu_cookie_match(struct rq *rq, struct task_struct *p)
1536{
1537	return true;
1538}
1539
1540static inline bool sched_core_cookie_match(struct rq *rq, struct task_struct *p)
1541{
1542	return true;
1543}
1544
1545static inline bool sched_group_cookie_match(struct rq *rq,
1546					    struct task_struct *p,
1547					    struct sched_group *group)
1548{
1549	return true;
1550}
1551
1552#endif /* !CONFIG_SCHED_CORE */
1553
1554#ifdef CONFIG_RT_GROUP_SCHED
1555# ifdef CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED
1556DECLARE_STATIC_KEY_FALSE(rt_group_sched);
1557static inline bool rt_group_sched_enabled(void)
1558{
1559	return static_branch_unlikely(&rt_group_sched);
1560}
1561# else /* !CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED: */
1562DECLARE_STATIC_KEY_TRUE(rt_group_sched);
1563static inline bool rt_group_sched_enabled(void)
1564{
1565	return static_branch_likely(&rt_group_sched);
1566}
1567# endif /* !CONFIG_RT_GROUP_SCHED_DEFAULT_DISABLED */
1568#else /* !CONFIG_RT_GROUP_SCHED: */
1569# define rt_group_sched_enabled()	false
1570#endif /* !CONFIG_RT_GROUP_SCHED */
1571
1572static inline void lockdep_assert_rq_held(struct rq *rq)
1573{
1574	lockdep_assert_held(__rq_lockp(rq));
1575}
1576
1577extern void raw_spin_rq_lock_nested(struct rq *rq, int subclass);
1578extern bool raw_spin_rq_trylock(struct rq *rq);
1579extern void raw_spin_rq_unlock(struct rq *rq);
1580
1581static inline void raw_spin_rq_lock(struct rq *rq)
1582{
1583	raw_spin_rq_lock_nested(rq, 0);
1584}
1585
1586static inline void raw_spin_rq_lock_irq(struct rq *rq)
1587{
1588	local_irq_disable();
1589	raw_spin_rq_lock(rq);
1590}
1591
1592static inline void raw_spin_rq_unlock_irq(struct rq *rq)
1593{
1594	raw_spin_rq_unlock(rq);
1595	local_irq_enable();
1596}
1597
1598static inline unsigned long _raw_spin_rq_lock_irqsave(struct rq *rq)
1599{
1600	unsigned long flags;
1601
1602	local_irq_save(flags);
1603	raw_spin_rq_lock(rq);
1604
1605	return flags;
1606}
1607
1608static inline void raw_spin_rq_unlock_irqrestore(struct rq *rq, unsigned long flags)
1609{
1610	raw_spin_rq_unlock(rq);
1611	local_irq_restore(flags);
1612}
1613
1614#define raw_spin_rq_lock_irqsave(rq, flags)	\
1615do {						\
1616	flags = _raw_spin_rq_lock_irqsave(rq);	\
1617} while (0)
1618
1619#ifdef CONFIG_SCHED_SMT
1620extern void __update_idle_core(struct rq *rq);
1621
1622static inline void update_idle_core(struct rq *rq)
1623{
1624	if (static_branch_unlikely(&sched_smt_present))
1625		__update_idle_core(rq);
1626}
1627
1628#else /* !CONFIG_SCHED_SMT: */
1629static inline void update_idle_core(struct rq *rq) { }
1630#endif /* !CONFIG_SCHED_SMT */
1631
1632#ifdef CONFIG_FAIR_GROUP_SCHED
1633
1634static inline struct task_struct *task_of(struct sched_entity *se)
1635{
1636	WARN_ON_ONCE(!entity_is_task(se));
1637	return container_of(se, struct task_struct, se);
1638}
1639
1640static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
1641{
1642	return p->se.cfs_rq;
1643}
1644
1645/* runqueue on which this entity is (to be) queued */
1646static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1647{
1648	return se->cfs_rq;
1649}
1650
1651/* runqueue "owned" by this group */
1652static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1653{
1654	return grp->my_q;
1655}
1656
1657#else /* !CONFIG_FAIR_GROUP_SCHED: */
1658
1659#define task_of(_se)		container_of(_se, struct task_struct, se)
1660
1661static inline struct cfs_rq *task_cfs_rq(const struct task_struct *p)
1662{
1663	return &task_rq(p)->cfs;
1664}
1665
1666static inline struct cfs_rq *cfs_rq_of(const struct sched_entity *se)
1667{
1668	const struct task_struct *p = task_of(se);
1669	struct rq *rq = task_rq(p);
1670
1671	return &rq->cfs;
1672}
1673
1674/* runqueue "owned" by this group */
1675static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
1676{
1677	return NULL;
1678}
1679
1680#endif /* !CONFIG_FAIR_GROUP_SCHED */
1681
1682extern void update_rq_clock(struct rq *rq);
1683
1684/*
1685 * rq::clock_update_flags bits
1686 *
1687 * %RQCF_REQ_SKIP - will request skipping of clock update on the next
1688 *  call to __schedule(). This is an optimisation to avoid
1689 *  neighbouring rq clock updates.
1690 *
1691 * %RQCF_ACT_SKIP - is set from inside of __schedule() when skipping is
1692 *  in effect and calls to update_rq_clock() are being ignored.
1693 *
1694 * %RQCF_UPDATED - is a debug flag that indicates whether a call has been
1695 *  made to update_rq_clock() since the last time rq::lock was pinned.
1696 *
1697 * If inside of __schedule(), clock_update_flags will have been
1698 * shifted left (a left shift is a cheap operation for the fast path
1699 * to promote %RQCF_REQ_SKIP to %RQCF_ACT_SKIP), so you must use,
1700 *
1701 *	if (rq-clock_update_flags >= RQCF_UPDATED)
1702 *
1703 * to check if %RQCF_UPDATED is set. It'll never be shifted more than
1704 * one position though, because the next rq_unpin_lock() will shift it
1705 * back.
1706 */
1707#define RQCF_REQ_SKIP		0x01
1708#define RQCF_ACT_SKIP		0x02
1709#define RQCF_UPDATED		0x04
1710
1711static inline void assert_clock_updated(struct rq *rq)
1712{
1713	/*
1714	 * The only reason for not seeing a clock update since the
1715	 * last rq_pin_lock() is if we're currently skipping updates.
1716	 */
1717	WARN_ON_ONCE(rq->clock_update_flags < RQCF_ACT_SKIP);
1718}
1719
1720static inline u64 rq_clock(struct rq *rq)
1721{
1722	lockdep_assert_rq_held(rq);
1723	assert_clock_updated(rq);
1724
1725	return rq->clock;
1726}
1727
1728static inline u64 rq_clock_task(struct rq *rq)
1729{
1730	lockdep_assert_rq_held(rq);
1731	assert_clock_updated(rq);
1732
1733	return rq->clock_task;
1734}
1735
1736static inline void rq_clock_skip_update(struct rq *rq)
1737{
1738	lockdep_assert_rq_held(rq);
1739	rq->clock_update_flags |= RQCF_REQ_SKIP;
1740}
1741
1742/*
1743 * See rt task throttling, which is the only time a skip
1744 * request is canceled.
1745 */
1746static inline void rq_clock_cancel_skipupdate(struct rq *rq)
1747{
1748	lockdep_assert_rq_held(rq);
1749	rq->clock_update_flags &= ~RQCF_REQ_SKIP;
1750}
1751
1752/*
1753 * During cpu offlining and rq wide unthrottling, we can trigger
1754 * an update_rq_clock() for several cfs and rt runqueues (Typically
1755 * when using list_for_each_entry_*)
1756 * rq_clock_start_loop_update() can be called after updating the clock
1757 * once and before iterating over the list to prevent multiple update.
1758 * After the iterative traversal, we need to call rq_clock_stop_loop_update()
1759 * to clear RQCF_ACT_SKIP of rq->clock_update_flags.
1760 */
1761static inline void rq_clock_start_loop_update(struct rq *rq)
1762{
1763	lockdep_assert_rq_held(rq);
1764	WARN_ON_ONCE(rq->clock_update_flags & RQCF_ACT_SKIP);
1765	rq->clock_update_flags |= RQCF_ACT_SKIP;
1766}
1767
1768static inline void rq_clock_stop_loop_update(struct rq *rq)
1769{
1770	lockdep_assert_rq_held(rq);
1771	rq->clock_update_flags &= ~RQCF_ACT_SKIP;
1772}
1773
1774struct rq_flags {
1775	unsigned long flags;
1776	struct pin_cookie cookie;
1777	/*
1778	 * A copy of (rq::clock_update_flags & RQCF_UPDATED) for the
1779	 * current pin context is stashed here in case it needs to be
1780	 * restored in rq_repin_lock().
1781	 */
1782	unsigned int clock_update_flags;
1783};
1784
1785extern struct balance_callback balance_push_callback;
1786
1787#ifdef CONFIG_SCHED_CLASS_EXT
1788extern const struct sched_class ext_sched_class;
1789
1790DECLARE_STATIC_KEY_FALSE(__scx_enabled);	/* SCX BPF scheduler loaded */
1791DECLARE_STATIC_KEY_FALSE(__scx_switched_all);	/* all fair class tasks on SCX */
1792
1793#define scx_enabled()		static_branch_unlikely(&__scx_enabled)
1794#define scx_switched_all()	static_branch_unlikely(&__scx_switched_all)
1795
1796static inline void scx_rq_clock_update(struct rq *rq, u64 clock)
1797{
1798	if (!scx_enabled())
1799		return;
1800	WRITE_ONCE(rq->scx.clock, clock);
1801	smp_store_release(&rq->scx.flags, rq->scx.flags | SCX_RQ_CLK_VALID);
1802}
1803
1804static inline void scx_rq_clock_invalidate(struct rq *rq)
1805{
1806	if (!scx_enabled())
1807		return;
1808	WRITE_ONCE(rq->scx.flags, rq->scx.flags & ~SCX_RQ_CLK_VALID);
1809}
1810
1811#else /* !CONFIG_SCHED_CLASS_EXT: */
1812#define scx_enabled()		false
1813#define scx_switched_all()	false
1814
1815static inline void scx_rq_clock_update(struct rq *rq, u64 clock) {}
1816static inline void scx_rq_clock_invalidate(struct rq *rq) {}
1817#endif /* !CONFIG_SCHED_CLASS_EXT */
1818
1819/*
1820 * Lockdep annotation that avoids accidental unlocks; it's like a
1821 * sticky/continuous lockdep_assert_held().
1822 *
1823 * This avoids code that has access to 'struct rq *rq' (basically everything in
1824 * the scheduler) from accidentally unlocking the rq if they do not also have a
1825 * copy of the (on-stack) 'struct rq_flags rf'.
1826 *
1827 * Also see Documentation/locking/lockdep-design.rst.
1828 */
1829static inline void rq_pin_lock(struct rq *rq, struct rq_flags *rf)
1830{
1831	rf->cookie = lockdep_pin_lock(__rq_lockp(rq));
1832
1833	rq->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
1834	rf->clock_update_flags = 0;
1835	WARN_ON_ONCE(rq->balance_callback && rq->balance_callback != &balance_push_callback);
1836}
1837
1838static inline void rq_unpin_lock(struct rq *rq, struct rq_flags *rf)
1839{
1840	if (rq->clock_update_flags > RQCF_ACT_SKIP)
1841		rf->clock_update_flags = RQCF_UPDATED;
1842
1843	scx_rq_clock_invalidate(rq);
1844	lockdep_unpin_lock(__rq_lockp(rq), rf->cookie);
1845}
1846
1847static inline void rq_repin_lock(struct rq *rq, struct rq_flags *rf)
1848{
1849	lockdep_repin_lock(__rq_lockp(rq), rf->cookie);
1850
1851	/*
1852	 * Restore the value we stashed in @rf for this pin context.
1853	 */
1854	rq->clock_update_flags |= rf->clock_update_flags;
1855}
1856
1857extern
1858struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1859	__acquires(rq->lock);
1860
1861extern
1862struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
1863	__acquires(p->pi_lock)
1864	__acquires(rq->lock);
1865
1866static inline void
1867__task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1868	__releases(rq->lock)
1869{
1870	rq_unpin_lock(rq, rf);
1871	raw_spin_rq_unlock(rq);
1872}
1873
1874static inline void
1875task_rq_unlock(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1876	__releases(rq->lock)
1877	__releases(p->pi_lock)
1878{
1879	__task_rq_unlock(rq, p, rf);
1880	raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
1881}
1882
1883DEFINE_LOCK_GUARD_1(task_rq_lock, struct task_struct,
1884		    _T->rq = task_rq_lock(_T->lock, &_T->rf),
1885		    task_rq_unlock(_T->rq, _T->lock, &_T->rf),
1886		    struct rq *rq; struct rq_flags rf)
1887
1888DEFINE_LOCK_GUARD_1(__task_rq_lock, struct task_struct,
1889		    _T->rq = __task_rq_lock(_T->lock, &_T->rf),
1890		    __task_rq_unlock(_T->rq, _T->lock, &_T->rf),
1891		    struct rq *rq; struct rq_flags rf)
1892
1893static inline void rq_lock_irqsave(struct rq *rq, struct rq_flags *rf)
1894	__acquires(rq->lock)
1895{
1896	raw_spin_rq_lock_irqsave(rq, rf->flags);
1897	rq_pin_lock(rq, rf);
1898}
1899
1900static inline void rq_lock_irq(struct rq *rq, struct rq_flags *rf)
1901	__acquires(rq->lock)
1902{
1903	raw_spin_rq_lock_irq(rq);
1904	rq_pin_lock(rq, rf);
1905}
1906
1907static inline void rq_lock(struct rq *rq, struct rq_flags *rf)
1908	__acquires(rq->lock)
1909{
1910	raw_spin_rq_lock(rq);
1911	rq_pin_lock(rq, rf);
1912}
1913
1914static inline void rq_unlock_irqrestore(struct rq *rq, struct rq_flags *rf)
1915	__releases(rq->lock)
1916{
1917	rq_unpin_lock(rq, rf);
1918	raw_spin_rq_unlock_irqrestore(rq, rf->flags);
1919}
1920
1921static inline void rq_unlock_irq(struct rq *rq, struct rq_flags *rf)
1922	__releases(rq->lock)
1923{
1924	rq_unpin_lock(rq, rf);
1925	raw_spin_rq_unlock_irq(rq);
1926}
1927
1928static inline void rq_unlock(struct rq *rq, struct rq_flags *rf)
1929	__releases(rq->lock)
1930{
1931	rq_unpin_lock(rq, rf);
1932	raw_spin_rq_unlock(rq);
1933}
1934
1935DEFINE_LOCK_GUARD_1(rq_lock, struct rq,
1936		    rq_lock(_T->lock, &_T->rf),
1937		    rq_unlock(_T->lock, &_T->rf),
1938		    struct rq_flags rf)
1939
1940DEFINE_LOCK_GUARD_1(rq_lock_irq, struct rq,
1941		    rq_lock_irq(_T->lock, &_T->rf),
1942		    rq_unlock_irq(_T->lock, &_T->rf),
1943		    struct rq_flags rf)
1944
1945DEFINE_LOCK_GUARD_1(rq_lock_irqsave, struct rq,
1946		    rq_lock_irqsave(_T->lock, &_T->rf),
1947		    rq_unlock_irqrestore(_T->lock, &_T->rf),
1948		    struct rq_flags rf)
1949
1950static inline struct rq *this_rq_lock_irq(struct rq_flags *rf)
1951	__acquires(rq->lock)
1952{
1953	struct rq *rq;
1954
1955	local_irq_disable();
1956	rq = this_rq();
1957	rq_lock(rq, rf);
1958
1959	return rq;
1960}
1961
1962#ifdef CONFIG_NUMA
1963
1964enum numa_topology_type {
1965	NUMA_DIRECT,
1966	NUMA_GLUELESS_MESH,
1967	NUMA_BACKPLANE,
1968};
1969
1970extern enum numa_topology_type sched_numa_topology_type;
1971extern int sched_max_numa_distance;
1972extern bool find_numa_distance(int distance);
1973extern void sched_init_numa(int offline_node);
1974extern void sched_update_numa(int cpu, bool online);
1975extern void sched_domains_numa_masks_set(unsigned int cpu);
1976extern void sched_domains_numa_masks_clear(unsigned int cpu);
1977extern int sched_numa_find_closest(const struct cpumask *cpus, int cpu);
1978
1979#else /* !CONFIG_NUMA: */
1980
1981static inline void sched_init_numa(int offline_node) { }
1982static inline void sched_update_numa(int cpu, bool online) { }
1983static inline void sched_domains_numa_masks_set(unsigned int cpu) { }
1984static inline void sched_domains_numa_masks_clear(unsigned int cpu) { }
1985
1986static inline int sched_numa_find_closest(const struct cpumask *cpus, int cpu)
1987{
1988	return nr_cpu_ids;
1989}
1990
1991#endif /* !CONFIG_NUMA */
1992
1993#ifdef CONFIG_NUMA_BALANCING
1994
1995/* The regions in numa_faults array from task_struct */
1996enum numa_faults_stats {
1997	NUMA_MEM = 0,
1998	NUMA_CPU,
1999	NUMA_MEMBUF,
2000	NUMA_CPUBUF
2001};
2002
2003extern void sched_setnuma(struct task_struct *p, int node);
2004extern int migrate_task_to(struct task_struct *p, int cpu);
2005extern int migrate_swap(struct task_struct *p, struct task_struct *t,
2006			int cpu, int scpu);
2007extern void init_numa_balancing(u64 clone_flags, struct task_struct *p);
2008
2009#else /* !CONFIG_NUMA_BALANCING: */
2010
2011static inline void
2012init_numa_balancing(u64 clone_flags, struct task_struct *p)
2013{
2014}
2015
2016#endif /* !CONFIG_NUMA_BALANCING */
2017
2018static inline void
2019queue_balance_callback(struct rq *rq,
2020		       struct balance_callback *head,
2021		       void (*func)(struct rq *rq))
2022{
2023	lockdep_assert_rq_held(rq);
2024
2025	/*
2026	 * Don't (re)queue an already queued item; nor queue anything when
2027	 * balance_push() is active, see the comment with
2028	 * balance_push_callback.
2029	 */
2030	if (unlikely(head->next || rq->balance_callback == &balance_push_callback))
2031		return;
2032
2033	head->func = func;
2034	head->next = rq->balance_callback;
2035	rq->balance_callback = head;
2036}
2037
2038#define rcu_dereference_check_sched_domain(p) \
2039	rcu_dereference_check((p), lockdep_is_held(&sched_domains_mutex))
2040
2041/*
2042 * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
2043 * See destroy_sched_domains: call_rcu for details.
2044 *
2045 * The domain tree of any CPU may only be accessed from within
2046 * preempt-disabled sections.
2047 */
2048#define for_each_domain(cpu, __sd) \
2049	for (__sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd); \
2050			__sd; __sd = __sd->parent)
2051
2052/* A mask of all the SD flags that have the SDF_SHARED_CHILD metaflag */
2053#define SD_FLAG(name, mflags) (name * !!((mflags) & SDF_SHARED_CHILD)) |
2054static const unsigned int SD_SHARED_CHILD_MASK =
2055#include <linux/sched/sd_flags.h>
20560;
2057#undef SD_FLAG
2058
2059/**
2060 * highest_flag_domain - Return highest sched_domain containing flag.
2061 * @cpu:	The CPU whose highest level of sched domain is to
2062 *		be returned.
2063 * @flag:	The flag to check for the highest sched_domain
2064 *		for the given CPU.
2065 *
2066 * Returns the highest sched_domain of a CPU which contains @flag. If @flag has
2067 * the SDF_SHARED_CHILD metaflag, all the children domains also have @flag.
2068 */
2069static inline struct sched_domain *highest_flag_domain(int cpu, int flag)
2070{
2071	struct sched_domain *sd, *hsd = NULL;
2072
2073	for_each_domain(cpu, sd) {
2074		if (sd->flags & flag) {
2075			hsd = sd;
2076			continue;
2077		}
2078
2079		/*
2080		 * Stop the search if @flag is known to be shared at lower
2081		 * levels. It will not be found further up.
2082		 */
2083		if (flag & SD_SHARED_CHILD_MASK)
2084			break;
2085	}
2086
2087	return hsd;
2088}
2089
2090static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
2091{
2092	struct sched_domain *sd;
2093
2094	for_each_domain(cpu, sd) {
2095		if (sd->flags & flag)
2096			break;
2097	}
2098
2099	return sd;
2100}
2101
2102DECLARE_PER_CPU(struct sched_domain __rcu *, sd_llc);
2103DECLARE_PER_CPU(int, sd_llc_size);
2104DECLARE_PER_CPU(int, sd_llc_id);
2105DECLARE_PER_CPU(int, sd_share_id);
2106DECLARE_PER_CPU(struct sched_domain_shared __rcu *, sd_llc_shared);
2107DECLARE_PER_CPU(struct sched_domain __rcu *, sd_numa);
2108DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_packing);
2109DECLARE_PER_CPU(struct sched_domain __rcu *, sd_asym_cpucapacity);
2110
2111extern struct static_key_false sched_asym_cpucapacity;
2112extern struct static_key_false sched_cluster_active;
2113
2114static __always_inline bool sched_asym_cpucap_active(void)
2115{
2116	return static_branch_unlikely(&sched_asym_cpucapacity);
2117}
2118
2119struct sched_group_capacity {
2120	atomic_t		ref;
2121	/*
2122	 * CPU capacity of this group, SCHED_CAPACITY_SCALE being max capacity
2123	 * for a single CPU.
2124	 */
2125	unsigned long		capacity;
2126	unsigned long		min_capacity;		/* Min per-CPU capacity in group */
2127	unsigned long		max_capacity;		/* Max per-CPU capacity in group */
2128	unsigned long		next_update;
2129	int			imbalance;		/* XXX unrelated to capacity but shared group state */
2130
2131	int			id;
2132
2133	unsigned long		cpumask[];		/* Balance mask */
2134};
2135
2136struct sched_group {
2137	struct sched_group	*next;			/* Must be a circular list */
2138	atomic_t		ref;
2139
2140	unsigned int		group_weight;
2141	unsigned int		cores;
2142	struct sched_group_capacity *sgc;
2143	int			asym_prefer_cpu;	/* CPU of highest priority in group */
2144	int			flags;
2145
2146	/*
2147	 * The CPUs this group covers.
2148	 *
2149	 * NOTE: this field is variable length. (Allocated dynamically
2150	 * by attaching extra space to the end of the structure,
2151	 * depending on how many CPUs the kernel has booted up with)
2152	 */
2153	unsigned long		cpumask[];
2154};
2155
2156static inline struct cpumask *sched_group_span(struct sched_group *sg)
2157{
2158	return to_cpumask(sg->cpumask);
2159}
2160
2161/*
2162 * See build_balance_mask().
2163 */
2164static inline struct cpumask *group_balance_mask(struct sched_group *sg)
2165{
2166	return to_cpumask(sg->sgc->cpumask);
2167}
2168
2169extern int group_balance_cpu(struct sched_group *sg);
2170
2171extern void update_sched_domain_debugfs(void);
2172extern void dirty_sched_domain_sysctl(int cpu);
2173
2174extern int sched_update_scaling(void);
2175
2176static inline const struct cpumask *task_user_cpus(struct task_struct *p)
2177{
2178	if (!p->user_cpus_ptr)
2179		return cpu_possible_mask; /* &init_task.cpus_mask */
2180	return p->user_cpus_ptr;
2181}
2182
2183#ifdef CONFIG_CGROUP_SCHED
2184
2185/*
2186 * Return the group to which this tasks belongs.
2187 *
2188 * We cannot use task_css() and friends because the cgroup subsystem
2189 * changes that value before the cgroup_subsys::attach() method is called,
2190 * therefore we cannot pin it and might observe the wrong value.
2191 *
2192 * The same is true for autogroup's p->signal->autogroup->tg, the autogroup
2193 * core changes this before calling sched_move_task().
2194 *
2195 * Instead we use a 'copy' which is updated from sched_move_task() while
2196 * holding both task_struct::pi_lock and rq::lock.
2197 */
2198static inline struct task_group *task_group(struct task_struct *p)
2199{
2200	return p->sched_task_group;
2201}
2202
2203/* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
2204static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
2205{
2206#if defined(CONFIG_FAIR_GROUP_SCHED) || defined(CONFIG_RT_GROUP_SCHED)
2207	struct task_group *tg = task_group(p);
2208#endif
2209
2210#ifdef CONFIG_FAIR_GROUP_SCHED
2211	set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]);
2212	p->se.cfs_rq = tg->cfs_rq[cpu];
2213	p->se.parent = tg->se[cpu];
2214	p->se.depth = tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0;
2215#endif
2216
2217#ifdef CONFIG_RT_GROUP_SCHED
2218	/*
2219	 * p->rt.rt_rq is NULL initially and it is easier to assign
2220	 * root_task_group's rt_rq than switching in rt_rq_of_se()
2221	 * Clobbers tg(!)
2222	 */
2223	if (!rt_group_sched_enabled())
2224		tg = &root_task_group;
2225	p->rt.rt_rq  = tg->rt_rq[cpu];
2226	p->rt.parent = tg->rt_se[cpu];
2227#endif /* CONFIG_RT_GROUP_SCHED */
2228}
2229
2230#else /* !CONFIG_CGROUP_SCHED: */
2231
2232static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
2233
2234static inline struct task_group *task_group(struct task_struct *p)
2235{
2236	return NULL;
2237}
2238
2239#endif /* !CONFIG_CGROUP_SCHED */
2240
2241static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
2242{
2243	set_task_rq(p, cpu);
2244#ifdef CONFIG_SMP
2245	/*
2246	 * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
2247	 * successfully executed on another CPU. We must ensure that updates of
2248	 * per-task data have been completed by this moment.
2249	 */
2250	smp_wmb();
2251	WRITE_ONCE(task_thread_info(p)->cpu, cpu);
2252	p->wake_cpu = cpu;
2253	rseq_sched_set_ids_changed(p);
2254#endif /* CONFIG_SMP */
2255}
2256
2257/*
2258 * Tunables:
2259 */
2260
2261#define SCHED_FEAT(name, enabled)	\
2262	__SCHED_FEAT_##name ,
2263
2264enum {
2265#include "features.h"
2266	__SCHED_FEAT_NR,
2267};
2268
2269#undef SCHED_FEAT
2270
2271/*
2272 * To support run-time toggling of sched features, all the translation units
2273 * (but core.c) reference the sysctl_sched_features defined in core.c.
2274 */
2275extern __read_mostly unsigned int sysctl_sched_features;
2276
2277#ifdef CONFIG_JUMP_LABEL
2278
2279#define SCHED_FEAT(name, enabled)					\
2280static __always_inline bool static_branch_##name(struct static_key *key) \
2281{									\
2282	return static_key_##enabled(key);				\
2283}
2284
2285#include "features.h"
2286#undef SCHED_FEAT
2287
2288extern struct static_key sched_feat_keys[__SCHED_FEAT_NR];
2289#define sched_feat(x) (static_branch_##x(&sched_feat_keys[__SCHED_FEAT_##x]))
2290
2291#else /* !CONFIG_JUMP_LABEL: */
2292
2293#define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
2294
2295#endif /* !CONFIG_JUMP_LABEL */
2296
2297extern struct static_key_false sched_numa_balancing;
2298extern struct static_key_false sched_schedstats;
2299
2300static inline u64 global_rt_period(void)
2301{
2302	return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
2303}
2304
2305static inline u64 global_rt_runtime(void)
2306{
2307	if (sysctl_sched_rt_runtime < 0)
2308		return RUNTIME_INF;
2309
2310	return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
2311}
2312
2313/*
2314 * Is p the current execution context?
2315 */
2316static inline int task_current(struct rq *rq, struct task_struct *p)
2317{
2318	return rq->curr == p;
2319}
2320
2321/*
2322 * Is p the current scheduling context?
2323 *
2324 * Note that it might be the current execution context at the same time if
2325 * rq->curr == rq->donor == p.
2326 */
2327static inline int task_current_donor(struct rq *rq, struct task_struct *p)
2328{
2329	return rq->donor == p;
2330}
2331
2332static inline bool task_is_blocked(struct task_struct *p)
2333{
2334	if (!sched_proxy_exec())
2335		return false;
2336
2337	return !!p->blocked_on;
2338}
2339
2340static inline int task_on_cpu(struct rq *rq, struct task_struct *p)
2341{
2342	return p->on_cpu;
2343}
2344
2345static inline int task_on_rq_queued(struct task_struct *p)
2346{
2347	return READ_ONCE(p->on_rq) == TASK_ON_RQ_QUEUED;
2348}
2349
2350static inline int task_on_rq_migrating(struct task_struct *p)
2351{
2352	return READ_ONCE(p->on_rq) == TASK_ON_RQ_MIGRATING;
2353}
2354
2355/* Wake flags. The first three directly map to some SD flag value */
2356#define WF_EXEC			0x02 /* Wakeup after exec; maps to SD_BALANCE_EXEC */
2357#define WF_FORK			0x04 /* Wakeup after fork; maps to SD_BALANCE_FORK */
2358#define WF_TTWU			0x08 /* Wakeup;            maps to SD_BALANCE_WAKE */
2359
2360#define WF_SYNC			0x10 /* Waker goes to sleep after wakeup */
2361#define WF_MIGRATED		0x20 /* Internal use, task got migrated */
2362#define WF_CURRENT_CPU		0x40 /* Prefer to move the wakee to the current CPU. */
2363#define WF_RQ_SELECTED		0x80 /* ->select_task_rq() was called */
2364
2365static_assert(WF_EXEC == SD_BALANCE_EXEC);
2366static_assert(WF_FORK == SD_BALANCE_FORK);
2367static_assert(WF_TTWU == SD_BALANCE_WAKE);
2368
2369/*
2370 * To aid in avoiding the subversion of "niceness" due to uneven distribution
2371 * of tasks with abnormal "nice" values across CPUs the contribution that
2372 * each task makes to its run queue's load is weighted according to its
2373 * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
2374 * scaled version of the new time slice allocation that they receive on time
2375 * slice expiry etc.
2376 */
2377
2378#define WEIGHT_IDLEPRIO		3
2379#define WMULT_IDLEPRIO		1431655765
2380
2381extern const int		sched_prio_to_weight[40];
2382extern const u32		sched_prio_to_wmult[40];
2383
2384/*
2385 * {de,en}queue flags:
2386 *
2387 * SLEEP/WAKEUP - task is no-longer/just-became runnable
2388 *
2389 * SAVE/RESTORE - an otherwise spurious dequeue/enqueue, done to ensure tasks
2390 *                are in a known state which allows modification. Such pairs
2391 *                should preserve as much state as possible.
2392 *
2393 * MOVE - paired with SAVE/RESTORE, explicitly does not preserve the location
2394 *        in the runqueue. IOW the priority is allowed to change. Callers
2395 *        must expect to deal with balance callbacks.
2396 *
2397 * NOCLOCK - skip the update_rq_clock() (avoids double updates)
2398 *
2399 * MIGRATION - p->on_rq == TASK_ON_RQ_MIGRATING (used for DEADLINE)
2400 *
2401 * DELAYED - de/re-queue a sched_delayed task
2402 *
2403 * CLASS - going to update p->sched_class; makes sched_change call the
2404 *         various switch methods.
2405 *
2406 * ENQUEUE_HEAD      - place at front of runqueue (tail if not specified)
2407 * ENQUEUE_REPLENISH - CBS (replenish runtime and postpone deadline)
2408 * ENQUEUE_MIGRATED  - the task was migrated during wakeup
2409 * ENQUEUE_RQ_SELECTED - ->select_task_rq() was called
2410 *
2411 * XXX SAVE/RESTORE in combination with CLASS doesn't really make sense, but
2412 * SCHED_DEADLINE seems to rely on this for now.
2413 */
2414
2415#define DEQUEUE_SLEEP		0x0001 /* Matches ENQUEUE_WAKEUP */
2416#define DEQUEUE_SAVE		0x0002 /* Matches ENQUEUE_RESTORE */
2417#define DEQUEUE_MOVE		0x0004 /* Matches ENQUEUE_MOVE */
2418#define DEQUEUE_NOCLOCK		0x0008 /* Matches ENQUEUE_NOCLOCK */
2419
2420#define DEQUEUE_MIGRATING	0x0010 /* Matches ENQUEUE_MIGRATING */
2421#define DEQUEUE_DELAYED		0x0020 /* Matches ENQUEUE_DELAYED */
2422#define DEQUEUE_CLASS		0x0040 /* Matches ENQUEUE_CLASS */
2423
2424#define DEQUEUE_SPECIAL		0x00010000
2425#define DEQUEUE_THROTTLE	0x00020000
2426
2427#define ENQUEUE_WAKEUP		0x0001
2428#define ENQUEUE_RESTORE		0x0002
2429#define ENQUEUE_MOVE		0x0004
2430#define ENQUEUE_NOCLOCK		0x0008
2431
2432#define ENQUEUE_MIGRATING	0x0010
2433#define ENQUEUE_DELAYED		0x0020
2434#define ENQUEUE_CLASS		0x0040
2435
2436#define ENQUEUE_HEAD		0x00010000
2437#define ENQUEUE_REPLENISH	0x00020000
2438#define ENQUEUE_MIGRATED	0x00040000
2439#define ENQUEUE_INITIAL		0x00080000
2440#define ENQUEUE_RQ_SELECTED	0x00100000
2441
2442#define RETRY_TASK		((void *)-1UL)
2443
2444struct affinity_context {
2445	const struct cpumask	*new_mask;
2446	struct cpumask		*user_mask;
2447	unsigned int		flags;
2448};
2449
2450extern s64 update_curr_common(struct rq *rq);
2451
2452struct sched_class {
2453
2454#ifdef CONFIG_UCLAMP_TASK
2455	int uclamp_enabled;
2456#endif
2457	/*
2458	 * idle:  0
2459	 * ext:   1
2460	 * fair:  2
2461	 * rt:    4
2462	 * dl:    8
2463	 * stop: 16
2464	 */
2465	unsigned int queue_mask;
2466
2467	/*
2468	 * move_queued_task/activate_task/enqueue_task: rq->lock
2469	 * ttwu_do_activate/activate_task/enqueue_task: rq->lock
2470	 * wake_up_new_task/activate_task/enqueue_task: task_rq_lock
2471	 * ttwu_runnable/enqueue_task: task_rq_lock
2472	 * proxy_task_current: rq->lock
2473	 * sched_change_end
2474	 */
2475	void (*enqueue_task) (struct rq *rq, struct task_struct *p, int flags);
2476	/*
2477	 * move_queued_task/deactivate_task/dequeue_task: rq->lock
2478	 * __schedule/block_task/dequeue_task: rq->lock
2479	 * proxy_task_current: rq->lock
2480	 * wait_task_inactive: task_rq_lock
2481	 * sched_change_begin
2482	 */
2483	bool (*dequeue_task) (struct rq *rq, struct task_struct *p, int flags);
2484
2485	/*
2486	 * do_sched_yield: rq->lock
2487	 */
2488	void (*yield_task)   (struct rq *rq);
2489	/*
2490	 * yield_to: rq->lock (double)
2491	 */
2492	bool (*yield_to_task)(struct rq *rq, struct task_struct *p);
2493
2494	/*
2495	 * move_queued_task: rq->lock
2496	 * __migrate_swap_task: rq->lock
2497	 * ttwu_do_activate: rq->lock
2498	 * ttwu_runnable: task_rq_lock
2499	 * wake_up_new_task: task_rq_lock
2500	 */
2501	void (*wakeup_preempt)(struct rq *rq, struct task_struct *p, int flags);
2502
2503	/*
2504	 * schedule/pick_next_task/prev_balance: rq->lock
2505	 */
2506	int (*balance)(struct rq *rq, struct task_struct *prev, struct rq_flags *rf);
2507
2508	/*
2509	 * schedule/pick_next_task: rq->lock
2510	 */
2511	struct task_struct *(*pick_task)(struct rq *rq, struct rq_flags *rf);
2512	/*
2513	 * Optional! When implemented pick_next_task() should be equivalent to:
2514	 *
2515	 *   next = pick_task();
2516	 *   if (next) {
2517	 *       put_prev_task(prev);
2518	 *       set_next_task_first(next);
2519	 *   }
2520	 */
2521	struct task_struct *(*pick_next_task)(struct rq *rq, struct task_struct *prev,
2522					      struct rq_flags *rf);
2523
2524	/*
2525	 * sched_change:
2526	 * __schedule: rq->lock
2527	 */
2528	void (*put_prev_task)(struct rq *rq, struct task_struct *p, struct task_struct *next);
2529	void (*set_next_task)(struct rq *rq, struct task_struct *p, bool first);
2530
2531	/*
2532	 * select_task_rq: p->pi_lock
2533	 * sched_exec: p->pi_lock
2534	 */
2535	int  (*select_task_rq)(struct task_struct *p, int task_cpu, int flags);
2536
2537	/*
2538	 * set_task_cpu: p->pi_lock || rq->lock (ttwu like)
2539	 */
2540	void (*migrate_task_rq)(struct task_struct *p, int new_cpu);
2541
2542	/*
2543	 * ttwu_do_activate: rq->lock
2544	 * wake_up_new_task: task_rq_lock
2545	 */
2546	void (*task_woken)(struct rq *this_rq, struct task_struct *task);
2547
2548	/*
2549	 * do_set_cpus_allowed: task_rq_lock + sched_change
2550	 */
2551	void (*set_cpus_allowed)(struct task_struct *p, struct affinity_context *ctx);
2552
2553	/*
2554	 * sched_set_rq_{on,off}line: rq->lock
2555	 */
2556	void (*rq_online)(struct rq *rq);
2557	void (*rq_offline)(struct rq *rq);
2558
2559	/*
2560	 * push_cpu_stop: p->pi_lock && rq->lock
2561	 */
2562	struct rq *(*find_lock_rq)(struct task_struct *p, struct rq *rq);
2563
2564	/*
2565	 * hrtick: rq->lock
2566	 * sched_tick: rq->lock
2567	 * sched_tick_remote: rq->lock
2568	 */
2569	void (*task_tick)(struct rq *rq, struct task_struct *p, int queued);
2570	/*
2571	 * sched_cgroup_fork: p->pi_lock
2572	 */
2573	void (*task_fork)(struct task_struct *p);
2574	/*
2575	 * finish_task_switch: no locks
2576	 */
2577	void (*task_dead)(struct task_struct *p);
2578
2579	/*
2580	 * sched_change
2581	 */
2582	void (*switching_from)(struct rq *this_rq, struct task_struct *task);
2583	void (*switched_from) (struct rq *this_rq, struct task_struct *task);
2584	void (*switching_to)  (struct rq *this_rq, struct task_struct *task);
2585	void (*switched_to)   (struct rq *this_rq, struct task_struct *task);
2586	u64  (*get_prio)     (struct rq *this_rq, struct task_struct *task);
2587	void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
2588			      u64 oldprio);
2589
2590	/*
2591	 * set_load_weight: task_rq_lock + sched_change
2592	 * __setscheduler_parms: task_rq_lock + sched_change
2593	 */
2594	void (*reweight_task)(struct rq *this_rq, struct task_struct *task,
2595			      const struct load_weight *lw);
2596
2597	/*
2598	 * sched_rr_get_interval: task_rq_lock
2599	 */
2600	unsigned int (*get_rr_interval)(struct rq *rq,
2601					struct task_struct *task);
2602
2603	/*
2604	 * task_sched_runtime: task_rq_lock
2605	 */
2606	void (*update_curr)(struct rq *rq);
2607
2608#ifdef CONFIG_FAIR_GROUP_SCHED
2609	/*
2610	 * sched_change_group: task_rq_lock + sched_change
2611	 */
2612	void (*task_change_group)(struct task_struct *p);
2613#endif
2614
2615#ifdef CONFIG_SCHED_CORE
2616	/*
2617	 * pick_next_task: rq->lock
2618	 * try_steal_cookie: rq->lock (double)
2619	 */
2620	int (*task_is_throttled)(struct task_struct *p, int cpu);
2621#endif
2622};
2623
2624/*
2625 * Does not nest; only used around sched_class::pick_task() rq-lock-breaks.
2626 */
2627static inline void rq_modified_clear(struct rq *rq)
2628{
2629	rq->queue_mask = 0;
2630}
2631
2632static inline bool rq_modified_above(struct rq *rq, const struct sched_class * class)
2633{
2634	unsigned int mask = class->queue_mask;
2635	return rq->queue_mask & ~((mask << 1) - 1);
2636}
2637
2638static inline void put_prev_task(struct rq *rq, struct task_struct *prev)
2639{
2640	WARN_ON_ONCE(rq->donor != prev);
2641	prev->sched_class->put_prev_task(rq, prev, NULL);
2642}
2643
2644static inline void set_next_task(struct rq *rq, struct task_struct *next)
2645{
2646	next->sched_class->set_next_task(rq, next, false);
2647}
2648
2649static inline void
2650__put_prev_set_next_dl_server(struct rq *rq,
2651			      struct task_struct *prev,
2652			      struct task_struct *next)
2653{
2654	prev->dl_server = NULL;
2655	next->dl_server = rq->dl_server;
2656	rq->dl_server = NULL;
2657}
2658
2659static inline void put_prev_set_next_task(struct rq *rq,
2660					  struct task_struct *prev,
2661					  struct task_struct *next)
2662{
2663	WARN_ON_ONCE(rq->donor != prev);
2664
2665	__put_prev_set_next_dl_server(rq, prev, next);
2666
2667	if (next == prev)
2668		return;
2669
2670	prev->sched_class->put_prev_task(rq, prev, next);
2671	next->sched_class->set_next_task(rq, next, true);
2672}
2673
2674/*
2675 * Helper to define a sched_class instance; each one is placed in a separate
2676 * section which is ordered by the linker script:
2677 *
2678 *   include/asm-generic/vmlinux.lds.h
2679 *
2680 * *CAREFUL* they are laid out in *REVERSE* order!!!
2681 *
2682 * Also enforce alignment on the instance, not the type, to guarantee layout.
2683 */
2684#define DEFINE_SCHED_CLASS(name) \
2685const struct sched_class name##_sched_class \
2686	__aligned(__alignof__(struct sched_class)) \
2687	__section("__" #name "_sched_class")
2688
2689/* Defined in include/asm-generic/vmlinux.lds.h */
2690extern struct sched_class __sched_class_highest[];
2691extern struct sched_class __sched_class_lowest[];
2692
2693extern const struct sched_class stop_sched_class;
2694extern const struct sched_class dl_sched_class;
2695extern const struct sched_class rt_sched_class;
2696extern const struct sched_class fair_sched_class;
2697extern const struct sched_class idle_sched_class;
2698
2699/*
2700 * Iterate only active classes. SCX can take over all fair tasks or be
2701 * completely disabled. If the former, skip fair. If the latter, skip SCX.
2702 */
2703static inline const struct sched_class *next_active_class(const struct sched_class *class)
2704{
2705	class++;
2706#ifdef CONFIG_SCHED_CLASS_EXT
2707	if (scx_switched_all() && class == &fair_sched_class)
2708		class++;
2709	if (!scx_enabled() && class == &ext_sched_class)
2710		class++;
2711#endif
2712	return class;
2713}
2714
2715#define for_class_range(class, _from, _to) \
2716	for (class = (_from); class < (_to); class++)
2717
2718#define for_each_class(class) \
2719	for_class_range(class, __sched_class_highest, __sched_class_lowest)
2720
2721#define for_active_class_range(class, _from, _to)				\
2722	for (class = (_from); class != (_to); class = next_active_class(class))
2723
2724#define for_each_active_class(class)						\
2725	for_active_class_range(class, __sched_class_highest, __sched_class_lowest)
2726
2727#define sched_class_above(_a, _b)	((_a) < (_b))
2728
2729static inline bool sched_stop_runnable(struct rq *rq)
2730{
2731	return rq->stop && task_on_rq_queued(rq->stop);
2732}
2733
2734static inline bool sched_dl_runnable(struct rq *rq)
2735{
2736	return rq->dl.dl_nr_running > 0;
2737}
2738
2739static inline bool sched_rt_runnable(struct rq *rq)
2740{
2741	return rq->rt.rt_queued > 0;
2742}
2743
2744static inline bool sched_fair_runnable(struct rq *rq)
2745{
2746	return rq->cfs.nr_queued > 0;
2747}
2748
2749extern struct task_struct *pick_next_task_fair(struct rq *rq, struct task_struct *prev,
2750					       struct rq_flags *rf);
2751extern struct task_struct *pick_task_idle(struct rq *rq, struct rq_flags *rf);
2752
2753#define SCA_CHECK		0x01
2754#define SCA_MIGRATE_DISABLE	0x02
2755#define SCA_MIGRATE_ENABLE	0x04
2756#define SCA_USER		0x08
2757
2758extern void update_group_capacity(struct sched_domain *sd, int cpu);
2759
2760extern void sched_balance_trigger(struct rq *rq);
2761
2762extern int __set_cpus_allowed_ptr(struct task_struct *p, struct affinity_context *ctx);
2763extern void set_cpus_allowed_common(struct task_struct *p, struct affinity_context *ctx);
2764
2765static inline bool task_allowed_on_cpu(struct task_struct *p, int cpu)
2766{
2767	/* When not in the task's cpumask, no point in looking further. */
2768	if (!cpumask_test_cpu(cpu, p->cpus_ptr))
2769		return false;
2770
2771	/* Can @cpu run a user thread? */
2772	if (!(p->flags & PF_KTHREAD) && !task_cpu_possible(cpu, p))
2773		return false;
2774
2775	return true;
2776}
2777
2778static inline cpumask_t *alloc_user_cpus_ptr(int node)
2779{
2780	/*
2781	 * See set_cpus_allowed_force() above for the rcu_head usage.
2782	 */
2783	int size = max_t(int, cpumask_size(), sizeof(struct rcu_head));
2784
2785	return kmalloc_node(size, GFP_KERNEL, node);
2786}
2787
2788static inline struct task_struct *get_push_task(struct rq *rq)
2789{
2790	struct task_struct *p = rq->donor;
2791
2792	lockdep_assert_rq_held(rq);
2793
2794	if (rq->push_busy)
2795		return NULL;
2796
2797	if (p->nr_cpus_allowed == 1)
2798		return NULL;
2799
2800	if (p->migration_disabled)
2801		return NULL;
2802
2803	rq->push_busy = true;
2804	return get_task_struct(p);
2805}
2806
2807extern int push_cpu_stop(void *arg);
2808
2809#ifdef CONFIG_CPU_IDLE
2810
2811static inline void idle_set_state(struct rq *rq,
2812				  struct cpuidle_state *idle_state)
2813{
2814	rq->idle_state = idle_state;
2815}
2816
2817static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2818{
2819	WARN_ON_ONCE(!rcu_read_lock_held());
2820
2821	return rq->idle_state;
2822}
2823
2824#else /* !CONFIG_CPU_IDLE: */
2825
2826static inline void idle_set_state(struct rq *rq,
2827				  struct cpuidle_state *idle_state)
2828{
2829}
2830
2831static inline struct cpuidle_state *idle_get_state(struct rq *rq)
2832{
2833	return NULL;
2834}
2835
2836#endif /* !CONFIG_CPU_IDLE */
2837
2838extern void schedule_idle(void);
2839asmlinkage void schedule_user(void);
2840
2841extern void sysrq_sched_debug_show(void);
2842extern void sched_init_granularity(void);
2843extern void update_max_interval(void);
2844
2845extern void init_sched_dl_class(void);
2846extern void init_sched_rt_class(void);
2847extern void init_sched_fair_class(void);
2848
2849extern void resched_curr(struct rq *rq);
2850extern void resched_curr_lazy(struct rq *rq);
2851extern void resched_cpu(int cpu);
2852
2853extern void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime);
2854extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
2855
2856extern void init_dl_entity(struct sched_dl_entity *dl_se);
2857
2858extern void init_cfs_throttle_work(struct task_struct *p);
2859
2860#define BW_SHIFT		20
2861#define BW_UNIT			(1 << BW_SHIFT)
2862#define RATIO_SHIFT		8
2863#define MAX_BW_BITS		(64 - BW_SHIFT)
2864#define MAX_BW			((1ULL << MAX_BW_BITS) - 1)
2865
2866extern unsigned long to_ratio(u64 period, u64 runtime);
2867
2868extern void init_entity_runnable_average(struct sched_entity *se);
2869extern void post_init_entity_util_avg(struct task_struct *p);
2870
2871#ifdef CONFIG_NO_HZ_FULL
2872extern bool sched_can_stop_tick(struct rq *rq);
2873extern int __init sched_tick_offload_init(void);
2874
2875/*
2876 * Tick may be needed by tasks in the runqueue depending on their policy and
2877 * requirements. If tick is needed, lets send the target an IPI to kick it out of
2878 * nohz mode if necessary.
2879 */
2880static inline void sched_update_tick_dependency(struct rq *rq)
2881{
2882	int cpu = cpu_of(rq);
2883
2884	if (!tick_nohz_full_cpu(cpu))
2885		return;
2886
2887	if (sched_can_stop_tick(rq))
2888		tick_nohz_dep_clear_cpu(cpu, TICK_DEP_BIT_SCHED);
2889	else
2890		tick_nohz_dep_set_cpu(cpu, TICK_DEP_BIT_SCHED);
2891}
2892#else /* !CONFIG_NO_HZ_FULL: */
2893static inline int sched_tick_offload_init(void) { return 0; }
2894static inline void sched_update_tick_dependency(struct rq *rq) { }
2895#endif /* !CONFIG_NO_HZ_FULL */
2896
2897static inline void add_nr_running(struct rq *rq, unsigned count)
2898{
2899	unsigned prev_nr = rq->nr_running;
2900
2901	rq->nr_running = prev_nr + count;
2902	if (trace_sched_update_nr_running_tp_enabled()) {
2903		call_trace_sched_update_nr_running(rq, count);
2904	}
2905
2906	if (prev_nr < 2 && rq->nr_running >= 2)
2907		set_rd_overloaded(rq->rd, 1);
2908
2909	sched_update_tick_dependency(rq);
2910}
2911
2912static inline void sub_nr_running(struct rq *rq, unsigned count)
2913{
2914	rq->nr_running -= count;
2915	if (trace_sched_update_nr_running_tp_enabled()) {
2916		call_trace_sched_update_nr_running(rq, -count);
2917	}
2918
2919	/* Check if we still need preemption */
2920	sched_update_tick_dependency(rq);
2921}
2922
2923static inline void __block_task(struct rq *rq, struct task_struct *p)
2924{
2925	if (p->sched_contributes_to_load)
2926		rq->nr_uninterruptible++;
2927
2928	if (p->in_iowait) {
2929		atomic_inc(&rq->nr_iowait);
2930		delayacct_blkio_start();
2931	}
2932
2933	ASSERT_EXCLUSIVE_WRITER(p->on_rq);
2934
2935	/*
2936	 * The moment this write goes through, ttwu() can swoop in and migrate
2937	 * this task, rendering our rq->__lock ineffective.
2938	 *
2939	 * __schedule()				try_to_wake_up()
2940	 *   LOCK rq->__lock			  LOCK p->pi_lock
2941	 *   pick_next_task()
2942	 *     pick_next_task_fair()
2943	 *       pick_next_entity()
2944	 *         dequeue_entities()
2945	 *           __block_task()
2946	 *             RELEASE p->on_rq = 0	  if (p->on_rq && ...)
2947	 *					    break;
2948	 *
2949	 *					  ACQUIRE (after ctrl-dep)
2950	 *
2951	 *					  cpu = select_task_rq();
2952	 *					  set_task_cpu(p, cpu);
2953	 *					  ttwu_queue()
2954	 *					    ttwu_do_activate()
2955	 *					      LOCK rq->__lock
2956	 *					      activate_task()
2957	 *					        STORE p->on_rq = 1
2958	 *   UNLOCK rq->__lock
2959	 *
2960	 * Callers must ensure to not reference @p after this -- we no longer
2961	 * own it.
2962	 */
2963	smp_store_release(&p->on_rq, 0);
2964}
2965
2966extern void activate_task(struct rq *rq, struct task_struct *p, int flags);
2967extern void deactivate_task(struct rq *rq, struct task_struct *p, int flags);
2968
2969extern void wakeup_preempt(struct rq *rq, struct task_struct *p, int flags);
2970
2971#ifdef CONFIG_PREEMPT_RT
2972# define SCHED_NR_MIGRATE_BREAK 8
2973#else
2974# define SCHED_NR_MIGRATE_BREAK 32
2975#endif
2976
2977extern __read_mostly unsigned int sysctl_sched_nr_migrate;
2978extern __read_mostly unsigned int sysctl_sched_migration_cost;
2979
2980extern unsigned int sysctl_sched_base_slice;
2981
2982extern int sysctl_resched_latency_warn_ms;
2983extern int sysctl_resched_latency_warn_once;
2984
2985extern unsigned int sysctl_sched_tunable_scaling;
2986
2987extern unsigned int sysctl_numa_balancing_scan_delay;
2988extern unsigned int sysctl_numa_balancing_scan_period_min;
2989extern unsigned int sysctl_numa_balancing_scan_period_max;
2990extern unsigned int sysctl_numa_balancing_scan_size;
2991extern unsigned int sysctl_numa_balancing_hot_threshold;
2992
2993#ifdef CONFIG_SCHED_HRTICK
2994
2995/*
2996 * Use hrtick when:
2997 *  - enabled by features
2998 *  - hrtimer is actually high res
2999 */
3000static inline int hrtick_enabled(struct rq *rq)
3001{
3002	if (!cpu_active(cpu_of(rq)))
3003		return 0;
3004	return hrtimer_is_hres_active(&rq->hrtick_timer);
3005}
3006
3007static inline int hrtick_enabled_fair(struct rq *rq)
3008{
3009	if (!sched_feat(HRTICK))
3010		return 0;
3011	return hrtick_enabled(rq);
3012}
3013
3014static inline int hrtick_enabled_dl(struct rq *rq)
3015{
3016	if (!sched_feat(HRTICK_DL))
3017		return 0;
3018	return hrtick_enabled(rq);
3019}
3020
3021extern void hrtick_start(struct rq *rq, u64 delay);
3022
3023#else /* !CONFIG_SCHED_HRTICK: */
3024
3025static inline int hrtick_enabled_fair(struct rq *rq)
3026{
3027	return 0;
3028}
3029
3030static inline int hrtick_enabled_dl(struct rq *rq)
3031{
3032	return 0;
3033}
3034
3035static inline int hrtick_enabled(struct rq *rq)
3036{
3037	return 0;
3038}
3039
3040#endif /* !CONFIG_SCHED_HRTICK */
3041
3042#ifndef arch_scale_freq_tick
3043static __always_inline void arch_scale_freq_tick(void) { }
3044#endif
3045
3046#ifndef arch_scale_freq_capacity
3047/**
3048 * arch_scale_freq_capacity - get the frequency scale factor of a given CPU.
3049 * @cpu: the CPU in question.
3050 *
3051 * Return: the frequency scale factor normalized against SCHED_CAPACITY_SCALE, i.e.
3052 *
3053 *     f_curr
3054 *     ------ * SCHED_CAPACITY_SCALE
3055 *     f_max
3056 */
3057static __always_inline
3058unsigned long arch_scale_freq_capacity(int cpu)
3059{
3060	return SCHED_CAPACITY_SCALE;
3061}
3062#endif
3063
3064/*
3065 * In double_lock_balance()/double_rq_lock(), we use raw_spin_rq_lock() to
3066 * acquire rq lock instead of rq_lock(). So at the end of these two functions
3067 * we need to call double_rq_clock_clear_update() to clear RQCF_UPDATED of
3068 * rq->clock_update_flags to avoid the WARN_DOUBLE_CLOCK warning.
3069 */
3070static inline void double_rq_clock_clear_update(struct rq *rq1, struct rq *rq2)
3071{
3072	rq1->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
3073	rq2->clock_update_flags &= (RQCF_REQ_SKIP|RQCF_ACT_SKIP);
3074}
3075
3076#define DEFINE_LOCK_GUARD_2(name, type, _lock, _unlock, ...)				\
3077__DEFINE_UNLOCK_GUARD(name, type, _unlock, type *lock2; __VA_ARGS__)			\
3078static inline class_##name##_t class_##name##_constructor(type *lock, type *lock2)	\
3079{ class_##name##_t _t = { .lock = lock, .lock2 = lock2 }, *_T = &_t;			\
3080  _lock; return _t; }
3081
3082static inline bool rq_order_less(struct rq *rq1, struct rq *rq2)
3083{
3084#ifdef CONFIG_SCHED_CORE
3085	/*
3086	 * In order to not have {0,2},{1,3} turn into into an AB-BA,
3087	 * order by core-id first and cpu-id second.
3088	 *
3089	 * Notably:
3090	 *
3091	 *	double_rq_lock(0,3); will take core-0, core-1 lock
3092	 *	double_rq_lock(1,2); will take core-1, core-0 lock
3093	 *
3094	 * when only cpu-id is considered.
3095	 */
3096	if (rq1->core->cpu < rq2->core->cpu)
3097		return true;
3098	if (rq1->core->cpu > rq2->core->cpu)
3099		return false;
3100
3101	/*
3102	 * __sched_core_flip() relies on SMT having cpu-id lock order.
3103	 */
3104#endif /* CONFIG_SCHED_CORE */
3105	return rq1->cpu < rq2->cpu;
3106}
3107
3108extern void double_rq_lock(struct rq *rq1, struct rq *rq2);
3109
3110#ifdef CONFIG_PREEMPTION
3111
3112/*
3113 * fair double_lock_balance: Safely acquires both rq->locks in a fair
3114 * way at the expense of forcing extra atomic operations in all
3115 * invocations.  This assures that the double_lock is acquired using the
3116 * same underlying policy as the spinlock_t on this architecture, which
3117 * reduces latency compared to the unfair variant below.  However, it
3118 * also adds more overhead and therefore may reduce throughput.
3119 */
3120static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
3121	__releases(this_rq->lock)
3122	__acquires(busiest->lock)
3123	__acquires(this_rq->lock)
3124{
3125	raw_spin_rq_unlock(this_rq);
3126	double_rq_lock(this_rq, busiest);
3127
3128	return 1;
3129}
3130
3131#else /* !CONFIG_PREEMPTION: */
3132/*
3133 * Unfair double_lock_balance: Optimizes throughput at the expense of
3134 * latency by eliminating extra atomic operations when the locks are
3135 * already in proper order on entry.  This favors lower CPU-ids and will
3136 * grant the double lock to lower CPUs over higher ids under contention,
3137 * regardless of entry order into the function.
3138 */
3139static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
3140	__releases(this_rq->lock)
3141	__acquires(busiest->lock)
3142	__acquires(this_rq->lock)
3143{
3144	if (__rq_lockp(this_rq) == __rq_lockp(busiest) ||
3145	    likely(raw_spin_rq_trylock(busiest))) {
3146		double_rq_clock_clear_update(this_rq, busiest);
3147		return 0;
3148	}
3149
3150	if (rq_order_less(this_rq, busiest)) {
3151		raw_spin_rq_lock_nested(busiest, SINGLE_DEPTH_NESTING);
3152		double_rq_clock_clear_update(this_rq, busiest);
3153		return 0;
3154	}
3155
3156	raw_spin_rq_unlock(this_rq);
3157	double_rq_lock(this_rq, busiest);
3158
3159	return 1;
3160}
3161
3162#endif /* !CONFIG_PREEMPTION */
3163
3164/*
3165 * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
3166 */
3167static inline int double_lock_balance(struct rq *this_rq, struct rq *busiest)
3168{
3169	lockdep_assert_irqs_disabled();
3170
3171	return _double_lock_balance(this_rq, busiest);
3172}
3173
3174static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
3175	__releases(busiest->lock)
3176{
3177	if (__rq_lockp(this_rq) != __rq_lockp(busiest))
3178		raw_spin_rq_unlock(busiest);
3179	lock_set_subclass(&__rq_lockp(this_rq)->dep_map, 0, _RET_IP_);
3180}
3181
3182static inline void double_lock(spinlock_t *l1, spinlock_t *l2)
3183{
3184	if (l1 > l2)
3185		swap(l1, l2);
3186
3187	spin_lock(l1);
3188	spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3189}
3190
3191static inline void double_lock_irq(spinlock_t *l1, spinlock_t *l2)
3192{
3193	if (l1 > l2)
3194		swap(l1, l2);
3195
3196	spin_lock_irq(l1);
3197	spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3198}
3199
3200static inline void double_raw_lock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3201{
3202	if (l1 > l2)
3203		swap(l1, l2);
3204
3205	raw_spin_lock(l1);
3206	raw_spin_lock_nested(l2, SINGLE_DEPTH_NESTING);
3207}
3208
3209static inline void double_raw_unlock(raw_spinlock_t *l1, raw_spinlock_t *l2)
3210{
3211	raw_spin_unlock(l1);
3212	raw_spin_unlock(l2);
3213}
3214
3215DEFINE_LOCK_GUARD_2(double_raw_spinlock, raw_spinlock_t,
3216		    double_raw_lock(_T->lock, _T->lock2),
3217		    double_raw_unlock(_T->lock, _T->lock2))
3218
3219/*
3220 * double_rq_unlock - safely unlock two runqueues
3221 *
3222 * Note this does not restore interrupts like task_rq_unlock,
3223 * you need to do so manually after calling.
3224 */
3225static inline void double_rq_unlock(struct rq *rq1, struct rq *rq2)
3226	__releases(rq1->lock)
3227	__releases(rq2->lock)
3228{
3229	if (__rq_lockp(rq1) != __rq_lockp(rq2))
3230		raw_spin_rq_unlock(rq2);
3231	else
3232		__release(rq2->lock);
3233	raw_spin_rq_unlock(rq1);
3234}
3235
3236extern void set_rq_online (struct rq *rq);
3237extern void set_rq_offline(struct rq *rq);
3238
3239extern bool sched_smp_initialized;
3240
3241DEFINE_LOCK_GUARD_2(double_rq_lock, struct rq,
3242		    double_rq_lock(_T->lock, _T->lock2),
3243		    double_rq_unlock(_T->lock, _T->lock2))
3244
3245extern struct sched_entity *__pick_root_entity(struct cfs_rq *cfs_rq);
3246extern struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq);
3247extern struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq);
3248
3249extern bool sched_debug_verbose;
3250
3251extern void print_cfs_stats(struct seq_file *m, int cpu);
3252extern void print_rt_stats(struct seq_file *m, int cpu);
3253extern void print_dl_stats(struct seq_file *m, int cpu);
3254extern void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq);
3255extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq);
3256extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
3257
3258extern void resched_latency_warn(int cpu, u64 latency);
3259
3260#ifdef CONFIG_NUMA_BALANCING
3261extern void show_numa_stats(struct task_struct *p, struct seq_file *m);
3262extern void
3263print_numa_stats(struct seq_file *m, int node, unsigned long tsf,
3264		 unsigned long tpf, unsigned long gsf, unsigned long gpf);
3265#endif /* CONFIG_NUMA_BALANCING */
3266
3267extern void init_cfs_rq(struct cfs_rq *cfs_rq);
3268extern void init_rt_rq(struct rt_rq *rt_rq);
3269extern void init_dl_rq(struct dl_rq *dl_rq);
3270
3271extern void cfs_bandwidth_usage_inc(void);
3272extern void cfs_bandwidth_usage_dec(void);
3273
3274#ifdef CONFIG_NO_HZ_COMMON
3275
3276#define NOHZ_BALANCE_KICK_BIT	0
3277#define NOHZ_STATS_KICK_BIT	1
3278#define NOHZ_NEWILB_KICK_BIT	2
3279#define NOHZ_NEXT_KICK_BIT	3
3280
3281/* Run sched_balance_domains() */
3282#define NOHZ_BALANCE_KICK	BIT(NOHZ_BALANCE_KICK_BIT)
3283/* Update blocked load */
3284#define NOHZ_STATS_KICK		BIT(NOHZ_STATS_KICK_BIT)
3285/* Update blocked load when entering idle */
3286#define NOHZ_NEWILB_KICK	BIT(NOHZ_NEWILB_KICK_BIT)
3287/* Update nohz.next_balance */
3288#define NOHZ_NEXT_KICK		BIT(NOHZ_NEXT_KICK_BIT)
3289
3290#define NOHZ_KICK_MASK		(NOHZ_BALANCE_KICK | NOHZ_STATS_KICK | NOHZ_NEXT_KICK)
3291
3292#define nohz_flags(cpu)		(&cpu_rq(cpu)->nohz_flags)
3293
3294extern void nohz_balance_exit_idle(struct rq *rq);
3295#else /* !CONFIG_NO_HZ_COMMON: */
3296static inline void nohz_balance_exit_idle(struct rq *rq) { }
3297#endif /* !CONFIG_NO_HZ_COMMON */
3298
3299#ifdef CONFIG_NO_HZ_COMMON
3300extern void nohz_run_idle_balance(int cpu);
3301#else
3302static inline void nohz_run_idle_balance(int cpu) { }
3303#endif
3304
3305#include "stats.h"
3306
3307#if defined(CONFIG_SCHED_CORE) && defined(CONFIG_SCHEDSTATS)
3308
3309extern void __sched_core_account_forceidle(struct rq *rq);
3310
3311static inline void sched_core_account_forceidle(struct rq *rq)
3312{
3313	if (schedstat_enabled())
3314		__sched_core_account_forceidle(rq);
3315}
3316
3317extern void __sched_core_tick(struct rq *rq);
3318
3319static inline void sched_core_tick(struct rq *rq)
3320{
3321	if (sched_core_enabled(rq) && schedstat_enabled())
3322		__sched_core_tick(rq);
3323}
3324
3325#else /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS): */
3326
3327static inline void sched_core_account_forceidle(struct rq *rq) { }
3328
3329static inline void sched_core_tick(struct rq *rq) { }
3330
3331#endif /* !(CONFIG_SCHED_CORE && CONFIG_SCHEDSTATS) */
3332
3333#ifdef CONFIG_IRQ_TIME_ACCOUNTING
3334
3335struct irqtime {
3336	u64			total;
3337	u64			tick_delta;
3338	u64			irq_start_time;
3339	struct u64_stats_sync	sync;
3340};
3341
3342DECLARE_PER_CPU(struct irqtime, cpu_irqtime);
3343extern int sched_clock_irqtime;
3344
3345static inline int irqtime_enabled(void)
3346{
3347	return sched_clock_irqtime;
3348}
3349
3350/*
3351 * Returns the irqtime minus the softirq time computed by ksoftirqd.
3352 * Otherwise ksoftirqd's sum_exec_runtime is subtracted its own runtime
3353 * and never move forward.
3354 */
3355static inline u64 irq_time_read(int cpu)
3356{
3357	struct irqtime *irqtime = &per_cpu(cpu_irqtime, cpu);
3358	unsigned int seq;
3359	u64 total;
3360
3361	do {
3362		seq = __u64_stats_fetch_begin(&irqtime->sync);
3363		total = irqtime->total;
3364	} while (__u64_stats_fetch_retry(&irqtime->sync, seq));
3365
3366	return total;
3367}
3368
3369#else /* !CONFIG_IRQ_TIME_ACCOUNTING: */
3370
3371static inline int irqtime_enabled(void)
3372{
3373	return 0;
3374}
3375
3376#endif /* !CONFIG_IRQ_TIME_ACCOUNTING */
3377
3378#ifdef CONFIG_CPU_FREQ
3379
3380DECLARE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
3381
3382/**
3383 * cpufreq_update_util - Take a note about CPU utilization changes.
3384 * @rq: Runqueue to carry out the update for.
3385 * @flags: Update reason flags.
3386 *
3387 * This function is called by the scheduler on the CPU whose utilization is
3388 * being updated.
3389 *
3390 * It can only be called from RCU-sched read-side critical sections.
3391 *
3392 * The way cpufreq is currently arranged requires it to evaluate the CPU
3393 * performance state (frequency/voltage) on a regular basis to prevent it from
3394 * being stuck in a completely inadequate performance level for too long.
3395 * That is not guaranteed to happen if the updates are only triggered from CFS
3396 * and DL, though, because they may not be coming in if only RT tasks are
3397 * active all the time (or there are RT tasks only).
3398 *
3399 * As a workaround for that issue, this function is called periodically by the
3400 * RT sched class to trigger extra cpufreq updates to prevent it from stalling,
3401 * but that really is a band-aid.  Going forward it should be replaced with
3402 * solutions targeted more specifically at RT tasks.
3403 */
3404static inline void cpufreq_update_util(struct rq *rq, unsigned int flags)
3405{
3406	struct update_util_data *data;
3407
3408	data = rcu_dereference_sched(*per_cpu_ptr(&cpufreq_update_util_data,
3409						  cpu_of(rq)));
3410	if (data)
3411		data->func(data, rq_clock(rq), flags);
3412}
3413#else /* !CONFIG_CPU_FREQ: */
3414static inline void cpufreq_update_util(struct rq *rq, unsigned int flags) { }
3415#endif /* !CONFIG_CPU_FREQ */
3416
3417#ifdef arch_scale_freq_capacity
3418# ifndef arch_scale_freq_invariant
3419#  define arch_scale_freq_invariant()	true
3420# endif
3421#else
3422# define arch_scale_freq_invariant()	false
3423#endif
3424
3425unsigned long effective_cpu_util(int cpu, unsigned long util_cfs,
3426				 unsigned long *min,
3427				 unsigned long *max);
3428
3429unsigned long sugov_effective_cpu_perf(int cpu, unsigned long actual,
3430				 unsigned long min,
3431				 unsigned long max);
3432
3433
3434/*
3435 * Verify the fitness of task @p to run on @cpu taking into account the
3436 * CPU original capacity and the runtime/deadline ratio of the task.
3437 *
3438 * The function will return true if the original capacity of @cpu is
3439 * greater than or equal to task's deadline density right shifted by
3440 * (BW_SHIFT - SCHED_CAPACITY_SHIFT) and false otherwise.
3441 */
3442static inline bool dl_task_fits_capacity(struct task_struct *p, int cpu)
3443{
3444	unsigned long cap = arch_scale_cpu_capacity(cpu);
3445
3446	return cap >= p->dl.dl_density >> (BW_SHIFT - SCHED_CAPACITY_SHIFT);
3447}
3448
3449static inline unsigned long cpu_bw_dl(struct rq *rq)
3450{
3451	return (rq->dl.running_bw * SCHED_CAPACITY_SCALE) >> BW_SHIFT;
3452}
3453
3454static inline unsigned long cpu_util_dl(struct rq *rq)
3455{
3456	return READ_ONCE(rq->avg_dl.util_avg);
3457}
3458
3459
3460extern unsigned long cpu_util_cfs(int cpu);
3461extern unsigned long cpu_util_cfs_boost(int cpu);
3462
3463static inline unsigned long cpu_util_rt(struct rq *rq)
3464{
3465	return READ_ONCE(rq->avg_rt.util_avg);
3466}
3467
3468#ifdef CONFIG_UCLAMP_TASK
3469
3470unsigned long uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id);
3471
3472/*
3473 * When uclamp is compiled in, the aggregation at rq level is 'turned off'
3474 * by default in the fast path and only gets turned on once userspace performs
3475 * an operation that requires it.
3476 *
3477 * Returns true if userspace opted-in to use uclamp and aggregation at rq level
3478 * hence is active.
3479 */
3480static inline bool uclamp_is_used(void)
3481{
3482	return static_branch_likely(&sched_uclamp_used);
3483}
3484
3485/*
3486 * Enabling static branches would get the cpus_read_lock(),
3487 * check whether uclamp_is_used before enable it to avoid always
3488 * calling cpus_read_lock(). Because we never disable this
3489 * static key once enable it.
3490 */
3491static inline void sched_uclamp_enable(void)
3492{
3493	if (!uclamp_is_used())
3494		static_branch_enable(&sched_uclamp_used);
3495}
3496
3497static inline unsigned long uclamp_rq_get(struct rq *rq,
3498					  enum uclamp_id clamp_id)
3499{
3500	return READ_ONCE(rq->uclamp[clamp_id].value);
3501}
3502
3503static inline void uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id,
3504				 unsigned int value)
3505{
3506	WRITE_ONCE(rq->uclamp[clamp_id].value, value);
3507}
3508
3509static inline bool uclamp_rq_is_idle(struct rq *rq)
3510{
3511	return rq->uclamp_flags & UCLAMP_FLAG_IDLE;
3512}
3513
3514/* Is the rq being capped/throttled by uclamp_max? */
3515static inline bool uclamp_rq_is_capped(struct rq *rq)
3516{
3517	unsigned long rq_util;
3518	unsigned long max_util;
3519
3520	if (!uclamp_is_used())
3521		return false;
3522
3523	rq_util = cpu_util_cfs(cpu_of(rq)) + cpu_util_rt(rq);
3524	max_util = READ_ONCE(rq->uclamp[UCLAMP_MAX].value);
3525
3526	return max_util != SCHED_CAPACITY_SCALE && rq_util >= max_util;
3527}
3528
3529#define for_each_clamp_id(clamp_id) \
3530	for ((clamp_id) = 0; (clamp_id) < UCLAMP_CNT; (clamp_id)++)
3531
3532extern unsigned int sysctl_sched_uclamp_util_min_rt_default;
3533
3534
3535static inline unsigned int uclamp_none(enum uclamp_id clamp_id)
3536{
3537	if (clamp_id == UCLAMP_MIN)
3538		return 0;
3539	return SCHED_CAPACITY_SCALE;
3540}
3541
3542/* Integer rounded range for each bucket */
3543#define UCLAMP_BUCKET_DELTA DIV_ROUND_CLOSEST(SCHED_CAPACITY_SCALE, UCLAMP_BUCKETS)
3544
3545static inline unsigned int uclamp_bucket_id(unsigned int clamp_value)
3546{
3547	return min_t(unsigned int, clamp_value / UCLAMP_BUCKET_DELTA, UCLAMP_BUCKETS - 1);
3548}
3549
3550static inline void
3551uclamp_se_set(struct uclamp_se *uc_se, unsigned int value, bool user_defined)
3552{
3553	uc_se->value = value;
3554	uc_se->bucket_id = uclamp_bucket_id(value);
3555	uc_se->user_defined = user_defined;
3556}
3557
3558#else /* !CONFIG_UCLAMP_TASK: */
3559
3560static inline unsigned long
3561uclamp_eff_value(struct task_struct *p, enum uclamp_id clamp_id)
3562{
3563	if (clamp_id == UCLAMP_MIN)
3564		return 0;
3565
3566	return SCHED_CAPACITY_SCALE;
3567}
3568
3569static inline bool uclamp_rq_is_capped(struct rq *rq) { return false; }
3570
3571static inline bool uclamp_is_used(void)
3572{
3573	return false;
3574}
3575
3576static inline void sched_uclamp_enable(void) {}
3577
3578static inline unsigned long
3579uclamp_rq_get(struct rq *rq, enum uclamp_id clamp_id)
3580{
3581	if (clamp_id == UCLAMP_MIN)
3582		return 0;
3583
3584	return SCHED_CAPACITY_SCALE;
3585}
3586
3587static inline void
3588uclamp_rq_set(struct rq *rq, enum uclamp_id clamp_id, unsigned int value)
3589{
3590}
3591
3592static inline bool uclamp_rq_is_idle(struct rq *rq)
3593{
3594	return false;
3595}
3596
3597#endif /* !CONFIG_UCLAMP_TASK */
3598
3599#ifdef CONFIG_HAVE_SCHED_AVG_IRQ
3600
3601static inline unsigned long cpu_util_irq(struct rq *rq)
3602{
3603	return READ_ONCE(rq->avg_irq.util_avg);
3604}
3605
3606static inline
3607unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3608{
3609	util *= (max - irq);
3610	util /= max;
3611
3612	return util;
3613
3614}
3615
3616#else /* !CONFIG_HAVE_SCHED_AVG_IRQ: */
3617
3618static inline unsigned long cpu_util_irq(struct rq *rq)
3619{
3620	return 0;
3621}
3622
3623static inline
3624unsigned long scale_irq_capacity(unsigned long util, unsigned long irq, unsigned long max)
3625{
3626	return util;
3627}
3628
3629#endif /* !CONFIG_HAVE_SCHED_AVG_IRQ */
3630
3631extern void __setparam_fair(struct task_struct *p, const struct sched_attr *attr);
3632
3633#if defined(CONFIG_ENERGY_MODEL) && defined(CONFIG_CPU_FREQ_GOV_SCHEDUTIL)
3634
3635#define perf_domain_span(pd) (to_cpumask(((pd)->em_pd->cpus)))
3636
3637DECLARE_STATIC_KEY_FALSE(sched_energy_present);
3638
3639static inline bool sched_energy_enabled(void)
3640{
3641	return static_branch_unlikely(&sched_energy_present);
3642}
3643
3644#else /* !(CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL): */
3645
3646#define perf_domain_span(pd) NULL
3647
3648static inline bool sched_energy_enabled(void) { return false; }
3649
3650#endif /* !(CONFIG_ENERGY_MODEL && CONFIG_CPU_FREQ_GOV_SCHEDUTIL) */
3651
3652#ifdef CONFIG_MEMBARRIER
3653
3654/*
3655 * The scheduler provides memory barriers required by membarrier between:
3656 * - prior user-space memory accesses and store to rq->membarrier_state,
3657 * - store to rq->membarrier_state and following user-space memory accesses.
3658 * In the same way it provides those guarantees around store to rq->curr.
3659 */
3660static inline void membarrier_switch_mm(struct rq *rq,
3661					struct mm_struct *prev_mm,
3662					struct mm_struct *next_mm)
3663{
3664	int membarrier_state;
3665
3666	if (prev_mm == next_mm)
3667		return;
3668
3669	membarrier_state = atomic_read(&next_mm->membarrier_state);
3670	if (READ_ONCE(rq->membarrier_state) == membarrier_state)
3671		return;
3672
3673	WRITE_ONCE(rq->membarrier_state, membarrier_state);
3674}
3675
3676#else /* !CONFIG_MEMBARRIER: */
3677
3678static inline void membarrier_switch_mm(struct rq *rq,
3679					struct mm_struct *prev_mm,
3680					struct mm_struct *next_mm)
3681{
3682}
3683
3684#endif /* !CONFIG_MEMBARRIER */
3685
3686static inline bool is_per_cpu_kthread(struct task_struct *p)
3687{
3688	if (!(p->flags & PF_KTHREAD))
3689		return false;
3690
3691	if (p->nr_cpus_allowed != 1)
3692		return false;
3693
3694	return true;
3695}
3696
3697extern void swake_up_all_locked(struct swait_queue_head *q);
3698extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait);
3699
3700extern int try_to_wake_up(struct task_struct *tsk, unsigned int state, int wake_flags);
3701
3702#ifdef CONFIG_PREEMPT_DYNAMIC
3703extern int preempt_dynamic_mode;
3704extern int sched_dynamic_mode(const char *str);
3705extern void sched_dynamic_update(int mode);
3706#endif
3707extern const char *preempt_modes[];
3708
3709#ifdef CONFIG_SCHED_MM_CID
3710
3711static __always_inline bool cid_on_cpu(unsigned int cid)
3712{
3713	return cid & MM_CID_ONCPU;
3714}
3715
3716static __always_inline bool cid_in_transit(unsigned int cid)
3717{
3718	return cid & MM_CID_TRANSIT;
3719}
3720
3721static __always_inline unsigned int cpu_cid_to_cid(unsigned int cid)
3722{
3723	return cid & ~MM_CID_ONCPU;
3724}
3725
3726static __always_inline unsigned int cid_to_cpu_cid(unsigned int cid)
3727{
3728	return cid | MM_CID_ONCPU;
3729}
3730
3731static __always_inline unsigned int cid_to_transit_cid(unsigned int cid)
3732{
3733	return cid | MM_CID_TRANSIT;
3734}
3735
3736static __always_inline unsigned int cid_from_transit_cid(unsigned int cid)
3737{
3738	return cid & ~MM_CID_TRANSIT;
3739}
3740
3741static __always_inline bool cid_on_task(unsigned int cid)
3742{
3743	/* True if none of the MM_CID_ONCPU, MM_CID_TRANSIT, MM_CID_UNSET bits is set */
3744	return cid < MM_CID_TRANSIT;
3745}
3746
3747static __always_inline void mm_drop_cid(struct mm_struct *mm, unsigned int cid)
3748{
3749	clear_bit(cid, mm_cidmask(mm));
3750}
3751
3752static __always_inline void mm_unset_cid_on_task(struct task_struct *t)
3753{
3754	unsigned int cid = t->mm_cid.cid;
3755
3756	t->mm_cid.cid = MM_CID_UNSET;
3757	if (cid_on_task(cid))
3758		mm_drop_cid(t->mm, cid);
3759}
3760
3761static __always_inline void mm_drop_cid_on_cpu(struct mm_struct *mm, struct mm_cid_pcpu *pcp)
3762{
3763	/* Clear the ONCPU bit, but do not set UNSET in the per CPU storage */
3764	if (cid_on_cpu(pcp->cid)) {
3765		pcp->cid = cpu_cid_to_cid(pcp->cid);
3766		mm_drop_cid(mm, pcp->cid);
3767	}
3768}
3769
3770static inline unsigned int __mm_get_cid(struct mm_struct *mm, unsigned int max_cids)
3771{
3772	unsigned int cid = find_first_zero_bit(mm_cidmask(mm), max_cids);
3773
3774	if (cid >= max_cids)
3775		return MM_CID_UNSET;
3776	if (test_and_set_bit(cid, mm_cidmask(mm)))
3777		return MM_CID_UNSET;
3778	return cid;
3779}
3780
3781static inline unsigned int mm_get_cid(struct mm_struct *mm)
3782{
3783	unsigned int cid = __mm_get_cid(mm, READ_ONCE(mm->mm_cid.max_cids));
3784
3785	while (cid == MM_CID_UNSET) {
3786		cpu_relax();
3787		cid = __mm_get_cid(mm, num_possible_cpus());
3788	}
3789	return cid;
3790}
3791
3792static inline unsigned int mm_cid_converge(struct mm_struct *mm, unsigned int orig_cid,
3793					   unsigned int max_cids)
3794{
3795	unsigned int new_cid, cid = cpu_cid_to_cid(orig_cid);
3796
3797	/* Is it in the optimal CID space? */
3798	if (likely(cid < max_cids))
3799		return orig_cid;
3800
3801	/* Try to find one in the optimal space. Otherwise keep the provided. */
3802	new_cid = __mm_get_cid(mm, max_cids);
3803	if (new_cid != MM_CID_UNSET) {
3804		mm_drop_cid(mm, cid);
3805		/* Preserve the ONCPU mode of the original CID */
3806		return new_cid | (orig_cid & MM_CID_ONCPU);
3807	}
3808	return orig_cid;
3809}
3810
3811static __always_inline void mm_cid_update_task_cid(struct task_struct *t, unsigned int cid)
3812{
3813	if (t->mm_cid.cid != cid) {
3814		t->mm_cid.cid = cid;
3815		rseq_sched_set_ids_changed(t);
3816	}
3817}
3818
3819static __always_inline void mm_cid_update_pcpu_cid(struct mm_struct *mm, unsigned int cid)
3820{
3821	__this_cpu_write(mm->mm_cid.pcpu->cid, cid);
3822}
3823
3824static __always_inline void mm_cid_from_cpu(struct task_struct *t, unsigned int cpu_cid,
3825					    unsigned int mode)
3826{
3827	unsigned int max_cids, tcid = t->mm_cid.cid;
3828	struct mm_struct *mm = t->mm;
3829
3830	max_cids = READ_ONCE(mm->mm_cid.max_cids);
3831	/* Optimize for the common case where both have the ONCPU bit set */
3832	if (likely(cid_on_cpu(cpu_cid & tcid))) {
3833		if (likely(cpu_cid_to_cid(cpu_cid) < max_cids)) {
3834			mm_cid_update_task_cid(t, cpu_cid);
3835			return;
3836		}
3837		/* Try to converge into the optimal CID space */
3838		cpu_cid = mm_cid_converge(mm, cpu_cid, max_cids);
3839	} else {
3840		/* Hand over or drop the task owned CID */
3841		if (cid_on_task(tcid)) {
3842			if (cid_on_cpu(cpu_cid))
3843				mm_unset_cid_on_task(t);
3844			else
3845				cpu_cid = cid_to_cpu_cid(tcid);
3846		}
3847		/* Still nothing, allocate a new one */
3848		if (!cid_on_cpu(cpu_cid))
3849			cpu_cid = cid_to_cpu_cid(mm_get_cid(mm));
3850
3851		/* Handle the transition mode flag if required */
3852		if (mode & MM_CID_TRANSIT)
3853			cpu_cid = cpu_cid_to_cid(cpu_cid) | MM_CID_TRANSIT;
3854	}
3855	mm_cid_update_pcpu_cid(mm, cpu_cid);
3856	mm_cid_update_task_cid(t, cpu_cid);
3857}
3858
3859static __always_inline void mm_cid_from_task(struct task_struct *t, unsigned int cpu_cid,
3860					     unsigned int mode)
3861{
3862	unsigned int max_cids, tcid = t->mm_cid.cid;
3863	struct mm_struct *mm = t->mm;
3864
3865	max_cids = READ_ONCE(mm->mm_cid.max_cids);
3866	/* Optimize for the common case, where both have the ONCPU bit clear */
3867	if (likely(cid_on_task(tcid | cpu_cid))) {
3868		if (likely(tcid < max_cids)) {
3869			mm_cid_update_pcpu_cid(mm, tcid);
3870			return;
3871		}
3872		/* Try to converge into the optimal CID space */
3873		tcid = mm_cid_converge(mm, tcid, max_cids);
3874	} else {
3875		/* Hand over or drop the CPU owned CID */
3876		if (cid_on_cpu(cpu_cid)) {
3877			if (cid_on_task(tcid))
3878				mm_drop_cid_on_cpu(mm, this_cpu_ptr(mm->mm_cid.pcpu));
3879			else
3880				tcid = cpu_cid_to_cid(cpu_cid);
3881		}
3882		/* Still nothing, allocate a new one */
3883		if (!cid_on_task(tcid))
3884			tcid = mm_get_cid(mm);
3885		/* Set the transition mode flag if required */
3886		tcid |= mode & MM_CID_TRANSIT;
3887	}
3888	mm_cid_update_pcpu_cid(mm, tcid);
3889	mm_cid_update_task_cid(t, tcid);
3890}
3891
3892static __always_inline void mm_cid_schedin(struct task_struct *next)
3893{
3894	struct mm_struct *mm = next->mm;
3895	unsigned int cpu_cid, mode;
3896
3897	if (!next->mm_cid.active)
3898		return;
3899
3900	cpu_cid = __this_cpu_read(mm->mm_cid.pcpu->cid);
3901	mode = READ_ONCE(mm->mm_cid.mode);
3902	if (likely(!cid_on_cpu(mode)))
3903		mm_cid_from_task(next, cpu_cid, mode);
3904	else
3905		mm_cid_from_cpu(next, cpu_cid, mode);
3906}
3907
3908static __always_inline void mm_cid_schedout(struct task_struct *prev)
3909{
3910	struct mm_struct *mm = prev->mm;
3911	unsigned int mode, cid;
3912
3913	/* During mode transitions CIDs are temporary and need to be dropped */
3914	if (likely(!cid_in_transit(prev->mm_cid.cid)))
3915		return;
3916
3917	mode = READ_ONCE(mm->mm_cid.mode);
3918	cid = cid_from_transit_cid(prev->mm_cid.cid);
3919
3920	/*
3921	 * If transition mode is done, transfer ownership when the CID is
3922	 * within the convergence range to optimize the next schedule in.
3923	 */
3924	if (!cid_in_transit(mode) && cid < READ_ONCE(mm->mm_cid.max_cids)) {
3925		if (cid_on_cpu(mode))
3926			cid = cid_to_cpu_cid(cid);
3927
3928		/* Update both so that the next schedule in goes into the fast path */
3929		mm_cid_update_pcpu_cid(mm, cid);
3930		prev->mm_cid.cid = cid;
3931	} else {
3932		mm_drop_cid(mm, cid);
3933		prev->mm_cid.cid = MM_CID_UNSET;
3934	}
3935}
3936
3937static inline void mm_cid_switch_to(struct task_struct *prev, struct task_struct *next)
3938{
3939	mm_cid_schedout(prev);
3940	mm_cid_schedin(next);
3941}
3942
3943#else /* !CONFIG_SCHED_MM_CID: */
3944static inline void mm_cid_switch_to(struct task_struct *prev, struct task_struct *next) { }
3945#endif /* !CONFIG_SCHED_MM_CID */
3946
3947extern u64 avg_vruntime(struct cfs_rq *cfs_rq);
3948extern int entity_eligible(struct cfs_rq *cfs_rq, struct sched_entity *se);
3949static inline
3950void move_queued_task_locked(struct rq *src_rq, struct rq *dst_rq, struct task_struct *task)
3951{
3952	lockdep_assert_rq_held(src_rq);
3953	lockdep_assert_rq_held(dst_rq);
3954
3955	deactivate_task(src_rq, task, 0);
3956	set_task_cpu(task, dst_rq->cpu);
3957	activate_task(dst_rq, task, 0);
3958}
3959
3960static inline
3961bool task_is_pushable(struct rq *rq, struct task_struct *p, int cpu)
3962{
3963	if (!task_on_cpu(rq, p) &&
3964	    cpumask_test_cpu(cpu, &p->cpus_mask))
3965		return true;
3966
3967	return false;
3968}
3969
3970#ifdef CONFIG_RT_MUTEXES
3971
3972static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
3973{
3974	if (pi_task)
3975		prio = min(prio, pi_task->prio);
3976
3977	return prio;
3978}
3979
3980static inline int rt_effective_prio(struct task_struct *p, int prio)
3981{
3982	struct task_struct *pi_task = rt_mutex_get_top_task(p);
3983
3984	return __rt_effective_prio(pi_task, prio);
3985}
3986
3987#else /* !CONFIG_RT_MUTEXES: */
3988
3989static inline int rt_effective_prio(struct task_struct *p, int prio)
3990{
3991	return prio;
3992}
3993
3994#endif /* !CONFIG_RT_MUTEXES */
3995
3996extern int __sched_setscheduler(struct task_struct *p, const struct sched_attr *attr, bool user, bool pi);
3997extern int __sched_setaffinity(struct task_struct *p, struct affinity_context *ctx);
3998extern const struct sched_class *__setscheduler_class(int policy, int prio);
3999extern void set_load_weight(struct task_struct *p, bool update_load);
4000extern void enqueue_task(struct rq *rq, struct task_struct *p, int flags);
4001extern bool dequeue_task(struct rq *rq, struct task_struct *p, int flags);
4002
4003extern struct balance_callback *splice_balance_callbacks(struct rq *rq);
4004
4005extern void __balance_callbacks(struct rq *rq, struct rq_flags *rf);
4006extern void balance_callbacks(struct rq *rq, struct balance_callback *head);
4007
4008/*
4009 * The 'sched_change' pattern is the safe, easy and slow way of changing a
4010 * task's scheduling properties. It dequeues a task, such that the scheduler
4011 * is fully unaware of it; at which point its properties can be modified;
4012 * after which it is enqueued again.
4013 *
4014 * Typically this must be called while holding task_rq_lock, since most/all
4015 * properties are serialized under those locks. There is currently one
4016 * exception to this rule in sched/ext which only holds rq->lock.
4017 */
4018
4019/*
4020 * This structure is a temporary, used to preserve/convey the queueing state
4021 * of the task between sched_change_begin() and sched_change_end(). Ensuring
4022 * the task's queueing state is idempotent across the operation.
4023 */
4024struct sched_change_ctx {
4025	u64			prio;
4026	struct task_struct	*p;
4027	int			flags;
4028	bool			queued;
4029	bool			running;
4030};
4031
4032struct sched_change_ctx *sched_change_begin(struct task_struct *p, unsigned int flags);
4033void sched_change_end(struct sched_change_ctx *ctx);
4034
4035DEFINE_CLASS(sched_change, struct sched_change_ctx *,
4036	     sched_change_end(_T),
4037	     sched_change_begin(p, flags),
4038	     struct task_struct *p, unsigned int flags)
4039
4040DEFINE_CLASS_IS_UNCONDITIONAL(sched_change)
4041
4042#include "ext.h"
4043
4044#endif /* _KERNEL_SCHED_SCHED_H */
~/ Read-only file preview.