1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_SCHED_TOPOLOGY_H
3 #define _LINUX_SCHED_TOPOLOGY_H
5 #include <linux/topology.h>
7 #include <linux/sched/idle.h>
10 * Increase resolution of cpu_capacity calculations
12 #define SCHED_CAPACITY_SHIFT SCHED_FIXEDPOINT_SHIFT
13 #define SCHED_CAPACITY_SCALE (1L << SCHED_CAPACITY_SHIFT)
16 * sched-domains (multiprocessor balancing) declarations:
20 #define SD_LOAD_BALANCE 0x0001 /* Do load balancing on this domain. */
21 #define SD_BALANCE_NEWIDLE 0x0002 /* Balance when about to become idle */
22 #define SD_BALANCE_EXEC 0x0004 /* Balance on exec */
23 #define SD_BALANCE_FORK 0x0008 /* Balance on fork, clone */
24 #define SD_BALANCE_WAKE 0x0010 /* Balance on wakeup */
25 #define SD_WAKE_AFFINE 0x0020 /* Wake task to waking CPU */
26 #define SD_ASYM_CPUCAPACITY 0x0040 /* Domain members have different CPU capacities */
27 #define SD_SHARE_CPUCAPACITY 0x0080 /* Domain members share CPU capacity */
28 #define SD_SHARE_POWERDOMAIN 0x0100 /* Domain members share power domain */
29 #define SD_SHARE_PKG_RESOURCES 0x0200 /* Domain members share CPU pkg resources */
30 #define SD_SERIALIZE 0x0400 /* Only a single load balancing instance */
31 #define SD_ASYM_PACKING 0x0800 /* Place busy groups earlier in the domain */
32 #define SD_PREFER_SIBLING 0x1000 /* Prefer to place tasks in a sibling domain */
33 #define SD_OVERLAP 0x2000 /* sched_domains of this level overlap */
34 #define SD_NUMA 0x4000 /* cross-node balancing */
36 #ifdef CONFIG_SCHED_SMT
37 static inline int cpu_smt_flags(void)
39 return SD_SHARE_CPUCAPACITY | SD_SHARE_PKG_RESOURCES;
43 #ifdef CONFIG_SCHED_MC
44 static inline int cpu_core_flags(void)
46 return SD_SHARE_PKG_RESOURCES;
51 static inline int cpu_numa_flags(void)
57 extern int arch_asym_cpu_priority(int cpu);
59 struct sched_domain_attr {
60 int relax_domain_level;
63 #define SD_ATTR_INIT (struct sched_domain_attr) { \
64 .relax_domain_level = -1, \
67 extern int sched_domain_level_max;
71 struct sched_domain_shared {
73 atomic_t nr_busy_cpus;
78 /* These fields must be setup */
79 struct sched_domain __rcu *parent; /* top domain must be null terminated */
80 struct sched_domain __rcu *child; /* bottom domain must be null terminated */
81 struct sched_group *groups; /* the balancing groups of the domain */
82 unsigned long min_interval; /* Minimum balance interval ms */
83 unsigned long max_interval; /* Maximum balance interval ms */
84 unsigned int busy_factor; /* less balancing by factor if busy */
85 unsigned int imbalance_pct; /* No balance until over watermark */
86 unsigned int cache_nice_tries; /* Leave cache hot tasks for # tries */
88 int nohz_idle; /* NOHZ IDLE status */
89 int flags; /* See SD_* */
93 unsigned long last_balance; /* init to jiffies. units in jiffies */
94 unsigned int balance_interval; /* initialise to 1. units in ms. */
95 unsigned int nr_balance_failed; /* initialise to 0 */
97 /* idle_balance() stats */
98 u64 max_newidle_lb_cost;
99 unsigned long next_decay_max_lb_cost;
101 u64 avg_scan_cost; /* select_idle_sibling */
103 #ifdef CONFIG_SCHEDSTATS
104 /* load_balance() stats */
105 unsigned int lb_count[CPU_MAX_IDLE_TYPES];
106 unsigned int lb_failed[CPU_MAX_IDLE_TYPES];
107 unsigned int lb_balanced[CPU_MAX_IDLE_TYPES];
108 unsigned int lb_imbalance[CPU_MAX_IDLE_TYPES];
109 unsigned int lb_gained[CPU_MAX_IDLE_TYPES];
110 unsigned int lb_hot_gained[CPU_MAX_IDLE_TYPES];
111 unsigned int lb_nobusyg[CPU_MAX_IDLE_TYPES];
112 unsigned int lb_nobusyq[CPU_MAX_IDLE_TYPES];
114 /* Active load balancing */
115 unsigned int alb_count;
116 unsigned int alb_failed;
117 unsigned int alb_pushed;
119 /* SD_BALANCE_EXEC stats */
120 unsigned int sbe_count;
121 unsigned int sbe_balanced;
122 unsigned int sbe_pushed;
124 /* SD_BALANCE_FORK stats */
125 unsigned int sbf_count;
126 unsigned int sbf_balanced;
127 unsigned int sbf_pushed;
129 /* try_to_wake_up() stats */
130 unsigned int ttwu_wake_remote;
131 unsigned int ttwu_move_affine;
132 unsigned int ttwu_move_balance;
134 #ifdef CONFIG_SCHED_DEBUG
138 void *private; /* used during construction */
139 struct rcu_head rcu; /* used during destruction */
141 struct sched_domain_shared *shared;
143 unsigned int span_weight;
145 * Span of all CPUs in this domain.
147 * NOTE: this field is variable length. (Allocated dynamically
148 * by attaching extra space to the end of the structure,
149 * depending on how many CPUs the kernel has booted up with)
151 unsigned long span[0];
154 static inline struct cpumask *sched_domain_span(struct sched_domain *sd)
156 return to_cpumask(sd->span);
159 extern void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
160 struct sched_domain_attr *dattr_new);
162 /* Allocate an array of sched domains, for partition_sched_domains(). */
163 cpumask_var_t *alloc_sched_domains(unsigned int ndoms);
164 void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms);
166 bool cpus_share_cache(int this_cpu, int that_cpu);
168 typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
169 typedef int (*sched_domain_flags_f)(void);
171 #define SDTL_OVERLAP 0x01
174 struct sched_domain *__percpu *sd;
175 struct sched_domain_shared *__percpu *sds;
176 struct sched_group *__percpu *sg;
177 struct sched_group_capacity *__percpu *sgc;
180 struct sched_domain_topology_level {
181 sched_domain_mask_f mask;
182 sched_domain_flags_f sd_flags;
186 #ifdef CONFIG_SCHED_DEBUG
191 extern void set_sched_topology(struct sched_domain_topology_level *tl);
193 #ifdef CONFIG_SCHED_DEBUG
194 # define SD_INIT_NAME(type) .name = #type
196 # define SD_INIT_NAME(type)
199 #else /* CONFIG_SMP */
201 struct sched_domain_attr;
204 partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
205 struct sched_domain_attr *dattr_new)
209 static inline bool cpus_share_cache(int this_cpu, int that_cpu)
214 #endif /* !CONFIG_SMP */
216 #ifndef arch_scale_cpu_capacity
217 static __always_inline
218 unsigned long arch_scale_cpu_capacity(int cpu)
220 return SCHED_CAPACITY_SCALE;
224 static inline int task_node(const struct task_struct *p)
226 return cpu_to_node(task_cpu(p));
229 #endif /* _LINUX_SCHED_TOPOLOGY_H */