Skip to content

Commit 0aaaf89

Browse files
arighihtejun
authored andcommitted
sched_ext: idle: Introduce SCX_OPS_BUILTIN_IDLE_PER_NODE
Add the new scheduler flag SCX_OPS_BUILTIN_IDLE_PER_NODE, which allows BPF schedulers to select between using a global flat idle cpumask or multiple per-node cpumasks. This only introduces the flag and the mechanism to enable/disable this feature without affecting any scheduling behavior. Cc: Yury Norov [NVIDIA] <yury.norov@gmail.com> Signed-off-by: Andrea Righi <arighi@nvidia.com> Reviewed-by: Yury Norov [NVIDIA] <yury.norov@gmail.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent d73249f commit 0aaaf89

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

kernel/sched/ext.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ enum scx_ops_flags {
154154
*/
155155
SCX_OPS_ALLOW_QUEUED_WAKEUP = 1LLU << 5,
156156

157+
/*
158+
* If set, enable per-node idle cpumasks. If clear, use a single global
159+
* flat idle cpumask.
160+
*/
161+
SCX_OPS_BUILTIN_IDLE_PER_NODE = 1LLU << 6,
162+
157163
/*
158164
* CPU cgroup support flags
159165
*/
@@ -165,6 +171,7 @@ enum scx_ops_flags {
165171
SCX_OPS_ENQ_MIGRATION_DISABLED |
166172
SCX_OPS_ALLOW_QUEUED_WAKEUP |
167173
SCX_OPS_SWITCH_PARTIAL |
174+
SCX_OPS_BUILTIN_IDLE_PER_NODE |
168175
SCX_OPS_HAS_CGROUP_WEIGHT,
169176
};
170177

@@ -3427,7 +3434,7 @@ static void handle_hotplug(struct rq *rq, bool online)
34273434
atomic_long_inc(&scx_hotplug_seq);
34283435

34293436
if (scx_enabled())
3430-
scx_idle_update_selcpu_topology();
3437+
scx_idle_update_selcpu_topology(&scx_ops);
34313438

34323439
if (online && SCX_HAS_OP(cpu_online))
34333440
SCX_CALL_OP(SCX_KF_UNLOCKED, cpu_online, cpu);
@@ -5228,6 +5235,16 @@ static int validate_ops(const struct sched_ext_ops *ops)
52285235
return -EINVAL;
52295236
}
52305237

5238+
/*
5239+
* SCX_OPS_BUILTIN_IDLE_PER_NODE requires built-in CPU idle
5240+
* selection policy to be enabled.
5241+
*/
5242+
if ((ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE) &&
5243+
(ops->update_idle && !(ops->flags & SCX_OPS_KEEP_BUILTIN_IDLE))) {
5244+
scx_ops_error("SCX_OPS_BUILTIN_IDLE_PER_NODE requires CPU idle selection enabled");
5245+
return -EINVAL;
5246+
}
5247+
52315248
return 0;
52325249
}
52335250

@@ -5352,7 +5369,7 @@ static int scx_ops_enable(struct sched_ext_ops *ops, struct bpf_link *link)
53525369
static_branch_enable_cpuslocked(&scx_has_op[i]);
53535370

53545371
check_hotplug_seq(ops);
5355-
scx_idle_update_selcpu_topology();
5372+
scx_idle_update_selcpu_topology(ops);
53565373

53575374
cpus_read_unlock();
53585375

kernel/sched/ext_idle.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
/* Enable/disable built-in idle CPU selection policy */
1515
static DEFINE_STATIC_KEY_FALSE(scx_builtin_idle_enabled);
1616

17+
/* Enable/disable per-node idle cpumasks */
18+
static DEFINE_STATIC_KEY_FALSE(scx_builtin_idle_per_node);
19+
1720
#ifdef CONFIG_SMP
1821
#ifdef CONFIG_CPUMASK_OFFSTACK
1922
#define CL_ALIGNED_IF_ONSTACK
@@ -204,7 +207,7 @@ static bool llc_numa_mismatch(void)
204207
* CPU belongs to a single LLC domain, and that each LLC domain is entirely
205208
* contained within a single NUMA node.
206209
*/
207-
void scx_idle_update_selcpu_topology(void)
210+
void scx_idle_update_selcpu_topology(struct sched_ext_ops *ops)
208211
{
209212
bool enable_llc = false, enable_numa = false;
210213
unsigned int nr_cpus;
@@ -237,13 +240,19 @@ void scx_idle_update_selcpu_topology(void)
237240
* If all CPUs belong to the same NUMA node and the same LLC domain,
238241
* enabling both NUMA and LLC optimizations is unnecessary, as checking
239242
* for an idle CPU in the same domain twice is redundant.
243+
*
244+
* If SCX_OPS_BUILTIN_IDLE_PER_NODE is enabled ignore the NUMA
245+
* optimization, as we would naturally select idle CPUs within
246+
* specific NUMA nodes querying the corresponding per-node cpumask.
240247
*/
241-
nr_cpus = numa_weight(cpu);
242-
if (nr_cpus > 0) {
243-
if (nr_cpus < num_online_cpus() && llc_numa_mismatch())
244-
enable_numa = true;
245-
pr_debug("sched_ext: NUMA=%*pb weight=%u\n",
246-
cpumask_pr_args(numa_span(cpu)), numa_weight(cpu));
248+
if (!(ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE)) {
249+
nr_cpus = numa_weight(cpu);
250+
if (nr_cpus > 0) {
251+
if (nr_cpus < num_online_cpus() && llc_numa_mismatch())
252+
enable_numa = true;
253+
pr_debug("sched_ext: NUMA=%*pb weight=%u\n",
254+
cpumask_pr_args(numa_span(cpu)), nr_cpus);
255+
}
247256
}
248257
rcu_read_unlock();
249258

@@ -530,6 +539,11 @@ void scx_idle_enable(struct sched_ext_ops *ops)
530539
}
531540
static_branch_enable(&scx_builtin_idle_enabled);
532541

542+
if (ops->flags & SCX_OPS_BUILTIN_IDLE_PER_NODE)
543+
static_branch_enable(&scx_builtin_idle_per_node);
544+
else
545+
static_branch_disable(&scx_builtin_idle_per_node);
546+
533547
#ifdef CONFIG_SMP
534548
/*
535549
* Consider all online cpus idle. Should converge to the actual state
@@ -543,6 +557,7 @@ void scx_idle_enable(struct sched_ext_ops *ops)
543557
void scx_idle_disable(void)
544558
{
545559
static_branch_disable(&scx_builtin_idle_enabled);
560+
static_branch_disable(&scx_builtin_idle_per_node);
546561
}
547562

548563
/********************************************************************************

kernel/sched/ext_idle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
struct sched_ext_ops;
1414

1515
#ifdef CONFIG_SMP
16-
void scx_idle_update_selcpu_topology(void);
16+
void scx_idle_update_selcpu_topology(struct sched_ext_ops *ops);
1717
void scx_idle_init_masks(void);
1818
bool scx_idle_test_and_clear_cpu(int cpu);
1919
s32 scx_pick_idle_cpu(const struct cpumask *cpus_allowed, u64 flags);
2020
#else /* !CONFIG_SMP */
21-
static inline void scx_idle_update_selcpu_topology(void) {}
21+
static inline void scx_idle_update_selcpu_topology(struct sched_ext_ops *ops) {}
2222
static inline void scx_idle_init_masks(void) {}
2323
static inline bool scx_idle_test_and_clear_cpu(int cpu) { return false; }
2424
static inline s32 scx_pick_idle_cpu(const struct cpumask *cpus_allowed, u64 flags)

tools/sched_ext/include/scx/compat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ static inline bool __COMPAT_struct_has_field(const char *type, const char *field
109109
#define SCX_OPS_SWITCH_PARTIAL \
110110
__COMPAT_ENUM_OR_ZERO("scx_ops_flags", "SCX_OPS_SWITCH_PARTIAL")
111111

112+
#define SCX_OPS_BUILTIN_IDLE_PER_NODE \
113+
__COMPAT_ENUM_OR_ZERO("scx_ops_flags", "SCX_OPS_BUILTIN_IDLE_PER_NODE")
114+
112115
static inline long scx_hotplug_seq(void)
113116
{
114117
int fd;

0 commit comments

Comments
 (0)