Skip to contentSkip to conversation

sched_ext: Unify idle CPU selection kfuncs

PATCH Applied Andrea Righi 2 participants 4 messages May 21, 2025

Subsystems (3)
lore
Share & tools
lore

Andrea Righi

author reviewer
#0 lore raw

PATCH 1/1 sched_ext: Unify idle CPU selection kfuncs

As discussed in [1], consolidate scx_bpf_select_cpu_dfl() and scx_bpf_select_cpu_and(), allowing them to be called from the same contexts and update the corresponding kselftest to reflect this change.

[1] https://lore.kernel.org/all/aCl1nq0hUJ7IdtC5@gpd3/

Andrea Righi (2):
sched_ext: idle: Consolidate default idle CPU selection kfuncs selftests/sched_ext: Update test enq_select_cpu_fails

 kernel/sched/ext_idle.c                            | 156 ++++++++++-----------
 tools/testing/selftests/sched_ext/Makefile         |   2 +-
 .../selftests/sched_ext/enq_select_cpu.bpf.c       |  74 ++++++++++
 tools/testing/selftests/sched_ext/enq_select_cpu.c |  88 ++++++++++++
 .../selftests/sched_ext/enq_select_cpu_fails.bpf.c |  43 ------
 .../selftests/sched_ext/enq_select_cpu_fails.c     |  61 --------
 6 files changed, 234 insertions(+), 190 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu.c
 delete mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu_fails.bpf.c
 delete mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu_fails.c
author reviewer
#1 lore raw

PATCH 1/2 sched_ext: idle: Consolidate default idle CPU selection kfuncs

There is no reason to restrict scx_bpf_select_cpu_dfl() invocations to ops.select_cpu() while allowing scx_bpf_select_cpu_and() to be used from multiple contexts, as both provide equivalent functionality, with the latter simply accepting an additional "allowed" cpumask.

Therefore, unify the two APIs, enabling both kfuncs to be used from ops.select_cpu(), ops.enqueue(), and unlocked contexts (e.g., via BPF test_run).

This allows schedulers to implement a consistent idle CPU selection policy and helps reduce code duplication.

---
 kernel/sched/ext_idle.c | 156 ++++++++++++++++++----------------------
 1 file changed, 71 insertions(+), 85 deletions(-)
kernel/sched/ext_idle.c +71 -85
index 37279a09900ca..ae30de383913d 100644
@@ -854,6 +854,68 @@ static bool check_builtin_idle_enabled(void)
 	return false;
 }
 
+s32 select_cpu_from_kfunc(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
+			  const struct cpumask *allowed, u64 flags)
+{
+	struct rq *rq;
+	struct rq_flags rf;
+	s32 cpu;
+
+	if (!kf_cpu_valid(prev_cpu, NULL))
+		return -EINVAL;
+
+	if (!check_builtin_idle_enabled())
+		return -EBUSY;
+
+	/*
+	 * If called from an unlocked context, acquire the task's rq lock,
+	 * so that we can safely access p->cpus_ptr and p->nr_cpus_allowed.
+	 *
+	 * Otherwise, allow to use this kfunc only from ops.select_cpu()
+	 * and ops.select_enqueue().
+	 */
+	if (scx_kf_allowed_if_unlocked()) {
+		rq = task_rq_lock(p, &rf);
+	} else {
+		if (!scx_kf_allowed(SCX_KF_SELECT_CPU | SCX_KF_ENQUEUE))
+			return -EPERM;
+		rq = scx_locked_rq();
+	}
+
+	/*
+	 * Validate locking correctness to access p->cpus_ptr and
+	 * p->nr_cpus_allowed: if we're holding an rq lock, we're safe;
+	 * otherwise, assert that p->pi_lock is held.
+	 */
+	if (!rq)
+		lockdep_assert_held(&p->pi_lock);
+
+#ifdef CONFIG_SMP
+	/*
+	 * This may also be called from ops.enqueue(), so we need to handle
+	 * per-CPU tasks as well. For these tasks, we can skip all idle CPU
+	 * selection optimizations and simply check whether the previously
+	 * used CPU is idle and within the allowed cpumask.
+	 */
+	if (p->nr_cpus_allowed == 1) {
+		if (cpumask_test_cpu(prev_cpu, allowed ?: p->cpus_ptr) &&
+		    scx_idle_test_and_clear_cpu(prev_cpu))
+			cpu = prev_cpu;
+		else
+			cpu = -EBUSY;
+	} else {
+		cpu = scx_select_cpu_dfl(p, prev_cpu, wake_flags,
+					 allowed ?: p->cpus_ptr, flags);
+	}
+#else
+	cpu = -EBUSY;
+#endif
+	if (scx_kf_allowed_if_unlocked())
+		task_rq_unlock(rq, p, &rf);
+
+	return cpu;
+}
+
 /**
  * scx_bpf_cpu_node - Return the NUMA node the given @cpu belongs to, or
  *		      trigger an error if @cpu is invalid
@@ -878,9 +940,10 @@ __bpf_kfunc int scx_bpf_cpu_node(s32 cpu)
  * @wake_flags: %SCX_WAKE_* flags
  * @is_idle: out parameter indicating whether the returned CPU is idle
  *
- * Can only be called from ops.select_cpu() if the built-in CPU selection is
- * enabled - ops.update_idle() is missing or %SCX_OPS_KEEP_BUILTIN_IDLE is set.
- * @p, @prev_cpu and @wake_flags match ops.select_cpu().
+ * Can be called from ops.select_cpu(), ops.enqueue(), or from an unlocked
+ * context such as a BPF test_run() call, as long as built-in CPU selection
+ * is enabled: ops.update_idle() is missing or %SCX_OPS_KEEP_BUILTIN_IDLE
+ * is set.
  *
  * Returns the picked CPU with *@is_idle indicating whether the picked CPU is
  * currently idle and thus a good candidate for direct dispatching.
@@ -888,28 +951,15 @@ __bpf_kfunc int scx_bpf_cpu_node(s32 cpu)
 __bpf_kfunc s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
 				       u64 wake_flags, bool *is_idle)
 {
-#ifdef CONFIG_SMP
 	s32 cpu;
-#endif
-	if (!kf_cpu_valid(prev_cpu, NULL))
-		goto prev_cpu;
-
-	if (!check_builtin_idle_enabled())
-		goto prev_cpu;
-
-	if (!scx_kf_allowed(SCX_KF_SELECT_CPU))
-		goto prev_cpu;
 
-#ifdef CONFIG_SMP
-	cpu = scx_select_cpu_dfl(p, prev_cpu, wake_flags, NULL, 0);
+	cpu = select_cpu_from_kfunc(p, prev_cpu, wake_flags, NULL, 0);
 	if (cpu >= 0) {
 		*is_idle = true;
 		return cpu;
 	}
-#endif
-
-prev_cpu:
 	*is_idle = false;
+
 	return prev_cpu;
 }
 
@@ -936,62 +986,7 @@ __bpf_kfunc s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu,
 __bpf_kfunc s32 scx_bpf_select_cpu_and(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 				       const struct cpumask *cpus_allowed, u64 flags)
 {
-	struct rq *rq;
-	struct rq_flags rf;
-	s32 cpu;
-
-	if (!kf_cpu_valid(prev_cpu, NULL))
-		return -EINVAL;
-
-	if (!check_builtin_idle_enabled())
-		return -EBUSY;
-
-	/*
-	 * If called from an unlocked context, acquire the task's rq lock,
-	 * so that we can safely access p->cpus_ptr and p->nr_cpus_allowed.
-	 *
-	 * Otherwise, allow to use this kfunc only from ops.select_cpu()
-	 * and ops.select_enqueue().
-	 */
-	if (scx_kf_allowed_if_unlocked()) {
-		rq = task_rq_lock(p, &rf);
-	} else {
-		if (!scx_kf_allowed(SCX_KF_SELECT_CPU | SCX_KF_ENQUEUE))
-			return -EPERM;
-		rq = scx_locked_rq();
-	}
-
-	/*
-	 * Validate locking correctness to access p->cpus_ptr and
-	 * p->nr_cpus_allowed: if we're holding an rq lock, we're safe;
-	 * otherwise, assert that p->pi_lock is held.
-	 */
-	if (!rq)
-		lockdep_assert_held(&p->pi_lock);
-
-#ifdef CONFIG_SMP
-	/*
-	 * This may also be called from ops.enqueue(), so we need to handle
-	 * per-CPU tasks as well. For these tasks, we can skip all idle CPU
-	 * selection optimizations and simply check whether the previously
-	 * used CPU is idle and within the allowed cpumask.
-	 */
-	if (p->nr_cpus_allowed == 1) {
-		if (cpumask_test_cpu(prev_cpu, cpus_allowed) &&
-		    scx_idle_test_and_clear_cpu(prev_cpu))
-			cpu = prev_cpu;
-		else
-			cpu = -EBUSY;
-	} else {
-		cpu = scx_select_cpu_dfl(p, prev_cpu, wake_flags, cpus_allowed, flags);
-	}
-#else
-	cpu = -EBUSY;
-#endif
-	if (scx_kf_allowed_if_unlocked())
-		task_rq_unlock(rq, p, &rf);
-
-	return cpu;
+	return select_cpu_from_kfunc(p, prev_cpu, wake_flags, cpus_allowed, flags);
 }
 
 /**
@@ -1294,6 +1289,7 @@ BTF_ID_FLAGS(func, scx_bpf_pick_idle_cpu, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu_node, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_pick_any_cpu, KF_RCU)
 BTF_ID_FLAGS(func, scx_bpf_select_cpu_and, KF_RCU)
+BTF_ID_FLAGS(func, scx_bpf_select_cpu_dfl, KF_RCU)
 BTF_KFUNCS_END(scx_kfunc_ids_idle)
 
 static const struct btf_kfunc_id_set scx_kfunc_set_idle = {
@@ -1301,21 +1297,11 @@ static const struct btf_kfunc_id_set scx_kfunc_set_idle = {
 	.set			= &scx_kfunc_ids_idle,
 };
 
-BTF_KFUNCS_START(scx_kfunc_ids_select_cpu)
-BTF_ID_FLAGS(func, scx_bpf_select_cpu_dfl, KF_RCU)
-BTF_KFUNCS_END(scx_kfunc_ids_select_cpu)
-
-static const struct btf_kfunc_id_set scx_kfunc_set_select_cpu = {
-	.owner			= THIS_MODULE,
-	.set			= &scx_kfunc_ids_select_cpu,
-};
-
 int scx_idle_init(void)
 {
 	int ret;
 
-	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &scx_kfunc_set_select_cpu) ||
-	      register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &scx_kfunc_set_idle) ||
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_STRUCT_OPS, &scx_kfunc_set_idle) ||
 	      register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &scx_kfunc_set_idle) ||
 	      register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL, &scx_kfunc_set_idle);
 
Signature · 1 line
2.49.0
author reviewer
#2 lore raw

PATCH 2/2 selftests/sched_ext: Update test enq_select_cpu_fails

In reply to Andrea Righi

With commit 08699d20467b6 ("sched_ext: idle: Consolidate default idle CPU selection kfuncs") allowing scx_bpf_select_cpu_dfl() to be invoked from multiple contexts, update the test to validate that the kfunc behaves correctly when used from ops.enqueue() and via BPF test_run.

Additionally, rename the test to enq_select_cpu, dropping "fails" from the name, as the logic has now been inverted.

---
 tools/testing/selftests/sched_ext/Makefile    |  2 +-
 .../selftests/sched_ext/enq_select_cpu.bpf.c  | 74 ++++++++++++++++
 .../selftests/sched_ext/enq_select_cpu.c      | 88 +++++++++++++++++++
 .../sched_ext/enq_select_cpu_fails.bpf.c      | 43 ---------
 .../sched_ext/enq_select_cpu_fails.c          | 61 -------------
 5 files changed, 163 insertions(+), 105 deletions(-)
 create mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu.bpf.c
 create mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu.c
 delete mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu_fails.bpf.c
 delete mode 100644 tools/testing/selftests/sched_ext/enq_select_cpu_fails.c
tools/testing/selftests/sched_ext/Makefile +1 -1
index e9d5bc575f806..9d9d6b4c38b01 100644
@@ -162,10 +162,10 @@ all_test_bpfprogs := $(foreach prog,$(wildcard *.bpf.c),$(INCLUDE_DIR)/$(patsubs
 auto-test-targets :=			\
 	create_dsq			\
 	enq_last_no_enq_fails		\
-	enq_select_cpu_fails		\
 	ddsp_bogus_dsq_fail		\
 	ddsp_vtimelocal_fail		\
 	dsp_local_on			\
+	enq_select_cpu			\
 	exit				\
 	hotplug				\
 	init_enable_count		\
tools/testing/selftests/sched_ext/enq_select_cpu.bpf.c new file +74 -0

74 lines not on this page · raw message

tools/testing/selftests/sched_ext/enq_select_cpu.c new file +88 -0

88 lines not on this page · raw message

tools/testing/selftests/sched_ext/enq_select_cpu_fails.bpf.c deleted +0 -43

43 lines not on this page · raw message

tools/testing/selftests/sched_ext/enq_select_cpu_fails.c deleted +0 -61

61 lines not on this page · raw message

Signature · 1 line
2.49.0
reviewer Applied
#3 lore raw
In reply to Andrea Righi
Quoted 9 lines · Andrea Righi

As discussed in [1], consolidate scx_bpf_select_cpu_dfl() and scx_bpf_select_cpu_and(), allowing them to be called from the same contexts and update the corresponding kselftest to reflect this change.

[1] https://lore.kernel.org/all/aCl1nq0hUJ7IdtC5@gpd3/

Andrea Righi (2):
sched_ext: idle: Consolidate default idle CPU selection kfuncs selftests/sched_ext: Update test enq_select_cpu_fails

Applied to sched_ext/for-6.16.

Thanks.

Signature · 1 line
tejun