Skip to contentSkip to conversation

sched_ext: Move built-in idle CPU selection policy to a separate file

PATCH Superseded Andrea Righi 2 participants 3 messages Jan 23, 2025 – Jan 25, 2025 Scheduler

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_ext: Move built-in idle CPU selection policy to a separate file

As ext.c is becoming quite large, move the idle CPU selection policy to separate files (ext_idle.c / ext_idle.h) for better code readability.

Moreover, group together all the idle CPU selection kfunc's to the same btf_kfunc_id_set block.

No functional changes, this is purely code reorganization.

---
 MAINTAINERS                 |   2 +
 kernel/sched/build_policy.c |   1 +
 kernel/sched/ext.c          | 753 +-----------------------------------
 kernel/sched/ext_idle.c     | 722 ++++++++++++++++++++++++++++++++++
 kernel/sched/ext_idle.h     |  45 +++
 5 files changed, 787 insertions(+), 736 deletions(-)
 create mode 100644 kernel/sched/ext_idle.c
 create mode 100644 kernel/sched/ext_idle.h
MAINTAINERS +2 -0
index 023df277737d..cd3d5b139a11 100644
@@ -20935,6 +20935,8 @@ T:	git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git
 F:	include/linux/sched/ext.h
 F:	kernel/sched/ext.h
 F:	kernel/sched/ext.c
+F:	kernel/sched/ext_idle.c
+F:	kernel/sched/ext_idle.h
 F:	tools/sched_ext/
 F:	tools/testing/selftests/sched_ext
 
kernel/sched/build_policy.c +1 -0
index fae1f5c921eb..72d97aa8b726 100644
@@ -61,6 +61,7 @@
 
 #ifdef CONFIG_SCHED_CLASS_EXT
 # include "ext.c"
+# include "ext_idle.c"
 #endif
 
 #include "syscalls.c"
kernel/sched/ext.c +17 -736

832 lines not on this page · raw message

kernel/sched/ext_idle.c new file +722 -0

722 lines not on this page · raw message

kernel/sched/ext_idle.h new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.48.1
#1 lore raw

Hello,

On Thu, Jan 23, 2025 at 11:02:02PM +0100, Andrea Righi wrote:
...

MAINTAINERS T: git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git · line 20939

Quoted 10 lines
MAINTAINERS +2 -0
index 023df277737d..cd3d5b139a11 100644
@@ -20935,6 +20935,8 @@ T:	git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git
 F:	include/linux/sched/ext.h
 F:	kernel/sched/ext.h
 F:	kernel/sched/ext.c
+F:	kernel/sched/ext_idle.c
+F:	kernel/sched/ext_idle.h

Maybe kernel/sched/ext*?

Quoted 4 lines
kernel/sched/ext.c +0 -0
index 564f250e7689..a24d48cebfb7 100644

...

Quoted 5 lines
kernel/sched/ext.c +1 -0
@@ -896,6 +893,17 @@ static struct static_key_false scx_has_op[SCX_OPI_END] =
 static atomic_t scx_exit_kind = ATOMIC_INIT(SCX_EXIT_DONE);
 static struct scx_exit_info *scx_exit_info;
 
+#define scx_ops_error_kind(err, fmt, args...)					\

kernel/sched/ext.c static struct static_key_false scx_has_op[SCX_OPI_END] = · line 906

Quoted 10 lines
kernel/sched/ext.c +10 -0
+	scx_ops_exit_kind((err), 0, fmt, ##args)
+
+#define scx_ops_exit(code, fmt, args...)					\
+	scx_ops_exit_kind(SCX_EXIT_UNREG_KERN, (code), fmt, ##args)
+
+#define scx_ops_error(fmt, args...)						\
+	scx_ops_error_kind(SCX_EXIT_ERROR, fmt, ##args)
+
+#define SCX_HAS_OP(op)	static_branch_likely(&scx_has_op[SCX_OP_IDX(op)])
+

This chunk is no longer necessary, right?

...

kernel/sched/ext.c BTF_ID_FLAGS(func, scx_bpf_nr_cpu_ids) · line 7758

Quoted 10 lines
kernel/sched/ext.c +0 -6
@@ -7750,12 +7037,6 @@ BTF_ID_FLAGS(func, scx_bpf_nr_cpu_ids)
 BTF_ID_FLAGS(func, scx_bpf_get_possible_cpumask, KF_ACQUIRE)
 BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE)
 BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE)
-BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask, KF_ACQUIRE)
-BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask, KF_ACQUIRE)
-BTF_ID_FLAGS(func, scx_bpf_put_idle_cpumask, KF_RELEASE)
-BTF_ID_FLAGS(func, scx_bpf_test_and_clear_cpu_idle)
-BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu, KF_RCU)
-BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu, KF_RCU)

So, these were in ids_any and could be called from any BPF progs.

Quoted 5 lines
kernel/sched/ext_idle.c new file +0 -0
new file mode 100644
index 000000000000..ca99fc58af91

...

kernel/sched/ext_idle.c line 717

Quoted 9 lines
kernel/sched/ext_idle.c new file +9 -0
+BTF_KFUNCS_START(scx_kfunc_ids_select_cpu)
+BTF_ID_FLAGS(func, scx_bpf_select_cpu_dfl, KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask, KF_ACQUIRE)
+BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask, KF_ACQUIRE)
+BTF_ID_FLAGS(func, scx_bpf_put_idle_cpumask, KF_RELEASE)
+BTF_ID_FLAGS(func, scx_bpf_test_and_clear_cpu_idle)
+BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu, KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu, KF_RCU)
+BTF_KFUNCS_END(scx_kfunc_ids_select_cpu)

But they get moved into ids_select_cpu and can only be called from struct_ops. Also, we currently don't use kfunc id sets directly to decide which kfunc may be called from which ops but that may change in the future too. Just create a separate ids and register directly from an init function?

...

Quoted 5 lines
kernel/sched/ext_idle.h new file +0 -0
new file mode 100644
index 000000000000..c1385af1ceeb

...

kernel/sched/ext_idle.h line 25

Quoted 12 lines
kernel/sched/ext_idle.h new file +12 -0
+#ifdef CONFIG_SMP
+static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_llc);
+static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_numa);
+
+static void update_selcpu_topology(void);
+static void reset_idle_masks(void);
+static void init_idle_masks(void);
+static bool test_and_clear_cpu_idle(int cpu);
+
+static s32 scx_pick_idle_cpu(const struct cpumask *cpus_allowed, u64 flags);
+static s32 scx_select_cpu_dfl(struct task_struct *p,
+			      s32 prev_cpu, u64 wake_flags, bool *found);

The way scheudler code is built is weird but let's keep it as close to usual h/c splits - make them extern prototypes and prefix them with scx_. Also, maybe name them to explicitly show that they deal with bulitin idle handling?

Thanks.

Signature · 1 line
tejun
#2 lore raw
Quoted 4 lines · Tejun Heo

Hello,

On Thu, Jan 23, 2025 at 11:02:02PM +0100, Andrea Righi wrote:
...

Quoted 12 lines

MAINTAINERS T: git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git · line 20939

Quoted 10 lines
MAINTAINERS +2 -0
index 023df277737d..cd3d5b139a11 100644
@@ -20935,6 +20935,8 @@ T:	git://git.kernel.org/pub/scm/linux/kernel/git/tj/sched_ext.git
 F:	include/linux/sched/ext.h
 F:	kernel/sched/ext.h
 F:	kernel/sched/ext.c
+F:	kernel/sched/ext_idle.c
+F:	kernel/sched/ext_idle.h

Maybe kernel/sched/ext*?

Ok.

Quoted 10 lines
Quoted 5 lines
kernel/sched/ext.c +0 -0
index 564f250e7689..a24d48cebfb7 100644

...

Quoted 4 lines
kernel/sched/ext.c +0 -0
@@ -896,6 +893,17 @@ static struct static_key_false scx_has_op[SCX_OPI_END] =
 static atomic_t scx_exit_kind = ATOMIC_INIT(SCX_EXIT_DONE);
 static struct scx_exit_info *scx_exit_info;
 
Quoted 13 lines

kernel/sched/ext.c static struct static_key_false scx_has_op[SCX_OPI_END] = · line 906

Quoted 11 lines
kernel/sched/ext.c +11 -0
+#define scx_ops_error_kind(err, fmt, args...)					\
+	scx_ops_exit_kind((err), 0, fmt, ##args)
+
+#define scx_ops_exit(code, fmt, args...)					\
+	scx_ops_exit_kind(SCX_EXIT_UNREG_KERN, (code), fmt, ##args)
+
+#define scx_ops_error(fmt, args...)						\
+	scx_ops_error_kind(SCX_EXIT_ERROR, fmt, ##args)
+
+#define SCX_HAS_OP(op)	static_branch_likely(&scx_has_op[SCX_OP_IDX(op)])
+

This chunk is no longer necessary, right?

Correct, no need to move this chunk.

Quoted 21 lines

...

kernel/sched/ext.c BTF_ID_FLAGS(func, scx_bpf_nr_cpu_ids) · line 7758

Quoted 10 lines
kernel/sched/ext.c +0 -6
@@ -7750,12 +7037,6 @@ BTF_ID_FLAGS(func, scx_bpf_nr_cpu_ids)
 BTF_ID_FLAGS(func, scx_bpf_get_possible_cpumask, KF_ACQUIRE)
 BTF_ID_FLAGS(func, scx_bpf_get_online_cpumask, KF_ACQUIRE)
 BTF_ID_FLAGS(func, scx_bpf_put_cpumask, KF_RELEASE)
-BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask, KF_ACQUIRE)
-BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask, KF_ACQUIRE)
-BTF_ID_FLAGS(func, scx_bpf_put_idle_cpumask, KF_RELEASE)
-BTF_ID_FLAGS(func, scx_bpf_test_and_clear_cpu_idle)
-BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu, KF_RCU)
-BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu, KF_RCU)

So, these were in ids_any and could be called from any BPF progs.

Quoted 5 lines
kernel/sched/ext_idle.c new file +0 -0
new file mode 100644
index 000000000000..ca99fc58af91

...

Quoted 14 lines

kernel/sched/ext_idle.c line 717

Quoted 9 lines
kernel/sched/ext_idle.c new file +9 -0
+BTF_KFUNCS_START(scx_kfunc_ids_select_cpu)
+BTF_ID_FLAGS(func, scx_bpf_select_cpu_dfl, KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_get_idle_cpumask, KF_ACQUIRE)
+BTF_ID_FLAGS(func, scx_bpf_get_idle_smtmask, KF_ACQUIRE)
+BTF_ID_FLAGS(func, scx_bpf_put_idle_cpumask, KF_RELEASE)
+BTF_ID_FLAGS(func, scx_bpf_test_and_clear_cpu_idle)
+BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu, KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu, KF_RCU)
+BTF_KFUNCS_END(scx_kfunc_ids_select_cpu)

But they get moved into ids_select_cpu and can only be called from struct_ops. Also, we currently don't use kfunc id sets directly to decide which kfunc may be called from which ops but that may change in the future too. Just create a separate ids and register directly from an init function?

Oh I was missing this aspect. Yeah, moving them to a separate block and registering it separately is probably a cleaner approach and we can have all the idle-related kfuncs grouped together (without breaking anything). Will do this.

Quoted 17 lines

...

Quoted 5 lines
kernel/sched/ext_idle.h new file +0 -0
new file mode 100644
index 000000000000..c1385af1ceeb

...

Quoted 9 lines
kernel/sched/ext_idle.h new file +9 -0
+#ifdef CONFIG_SMP
+static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_llc);
+static DEFINE_STATIC_KEY_FALSE(scx_selcpu_topo_numa);
+
+static void update_selcpu_topology(void);
+static void reset_idle_masks(void);
+static void init_idle_masks(void);
+static bool test_and_clear_cpu_idle(int cpu);
+
Quoted 8 lines

kernel/sched/ext_idle.h line 25

Quoted 3 lines
kernel/sched/ext_idle.h new file +3 -0
+static s32 scx_pick_idle_cpu(const struct cpumask *cpus_allowed, u64 flags);
+static s32 scx_select_cpu_dfl(struct task_struct *p,
+			      s32 prev_cpu, u64 wake_flags, bool *found);

The way scheudler code is built is weird but let's keep it as close to usual h/c splits - make them extern prototypes and prefix them with scx_. Also, maybe name them to explicitly show that they deal with bulitin idle handling?

Ok, I'll apply all these changes and send a new patch.

Thanks for looking at this!
-Andrea