Skip to contentSkip to conversation

sched_ext: Honor SCX_PICK_IDLE_CORE in CPU selection shortcuts

PATCH New Hui Su 1 participant 3 messages Oct 12

Subsystems (5)
lore
Share & tools
lore

Hui Su

author
#0 lore raw

cover sched_ext: Honor SCX_PICK_IDLE_CORE in CPU selection shortcuts

The restricted select_cpu kfunc path and the normal WAKE_SYNC shortcuts can bypass SCX_PICK_IDLE_CORE on SMT systems. The first patch skips the WAKE_SYNC shortcuts when strict-core selection is requested and validates restricted candidates against their idle SMT mask. The second patch covers the restricted path and both WAKE_SYNC shortcuts while retaining the generic smoke test when no SMT pair is available.

This series is based on:

[PATCH v3] sched_ext: idle: Respect task affinity in restricted CPU selection
https://lore.kernel.org/lkml/[email protected]/

Testing:
- x86_64 KVM/QEMU: all three restricted/WAKE_SYNC differential cases reproduced the strict-core violation before the fix and returned -EBUSY after it.
- The sched_ext selftests passed apart from an unrelated dsp_local_on environment failure while opening system Kconfig.
- The strict-core test passed 50 consecutive runs; non-SMT coverage retained the generic smoke test and skipped the SMT-specific cases.

Hui Su (2):
sched_ext: idle: Honor SCX_PICK_IDLE_CORE in CPU selection shortcuts selftests/sched_ext: Cover strict-core CPU selection shortcuts

 kernel/sched/ext/idle.c                       |   6 +-
 .../selftests/sched_ext/allowed_cpus.bpf.c    |  36 +-
 .../selftests/sched_ext/allowed_cpus.c        | 316 +++++++++++++++++-
 3 files changed, 347 insertions(+), 11 deletions(-)

base-commit: 9a2d086eba0f331eb5940d6a91efa7caa39080ad

Signature · 1 line
2.55.0
author
#1 lore raw

PATCH 1/2 sched_ext: idle: Honor SCX_PICK_IDLE_CORE in CPU selection shortcuts

SCX_PICK_IDLE_CORE requires selecting a CPU whose SMT siblings are all idle. The restricted select_cpu kfunc path now derives its candidate from task state, but it does not check that condition before claiming the CPU.

The WAKE_SYNC shortcuts in scx_select_cpu_dfl() can bypass strict-core selection on SMT systems as well. Skip those shortcuts when SCX_PICK_IDLE_CORE is requested, and validate restricted candidates against their idle SMT mask before consuming the idle bit.

Preserve individual-idle selection when the flag is not set and on non-SMT systems.

Fixes 683d2d0faba1 ("sched_ext: idle: Introduce scx_bpf_select_cpu_and()")
---
 kernel/sched/ext/idle.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/sched/ext/idle.c +5 -1
index 28ed9e327085..09e40928aef4 100644
@@ -523,7 +523,7 @@ s32 scx_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
 	/*
 	 * If WAKE_SYNC, try to migrate the wakee to the waker's CPU.
 	 */
-	if (wake_flags & SCX_WAKE_SYNC) {
+	if ((wake_flags & SCX_WAKE_SYNC) && !(sched_smt_active() && (flags & SCX_PICK_IDLE_CORE))) {
 		int waker_node;
 
 		/*
@@ -976,6 +976,10 @@ static s32 select_cpu_restricted(struct task_struct *p, s32 prev_cpu,
 			return -EBUSY;
 	}
 
+	if ((flags & SCX_PICK_IDLE_CORE) && sched_smt_active() &&
+	    !cpumask_test_cpu(cpu, idle_cpumask(scx_cpu_node_if_enabled(cpu))->smt))
+		return -EBUSY;
+
 	if (!scx_idle_test_and_clear_cpu(cpu))
 		return -EBUSY;
 
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH 2/2 selftests/sched_ext: Cover strict-core CPU selection shortcuts

In reply to Hui Su

Exercise scx_bpf_select_cpu_and() with a task whose candidate CPU is individually idle while its SMT sibling is occupied by the test runner. Verify that SCX_PICK_IDLE_CORE rejects the candidate, while the same restricted path without the flag still selects it.

Also cover both WAKE_SYNC shortcuts with SCX_PICK_IDLE_CORE set, including the waker-CPU path. The test uses a separate sleeping target task and private allowed masks so strict-core checks do not depend on task-affinity intersection or mutate the scheduler's configured mask.

Retain the generic user-space smoke test when no SMT pair is available, and skip only the strict-core cases in that configuration.

---
 .../selftests/sched_ext/allowed_cpus.bpf.c    |  36 +-
 .../selftests/sched_ext/allowed_cpus.c        | 316 +++++++++++++++++-
 2 files changed, 342 insertions(+), 10 deletions(-)
tools/testing/selftests/sched_ext/allowed_cpus.bpf.c +34 -2
index 9dd72d0da29b..4e0a798b696e 100644
@@ -148,22 +148,54 @@ void BPF_STRUCT_OPS(allowed_cpus_exit, struct scx_exit_info *ei)
 
 struct task_cpu_arg {
 	pid_t pid;
+	int prev_cpu;
+	int allowed_cpu;
+	__u64 wake_flags;
+	__u64 flags;
+	__u32 check_idle;
 };
 
 SEC("syscall")
 int select_cpu_from_user(struct task_cpu_arg *input)
 {
+	const struct cpumask *idle;
 	struct task_struct *p;
+	struct bpf_cpumask *mask;
 	int cpu;
 
 	p = bpf_task_from_pid(input->pid);
 	if (!p)
 		return -EINVAL;
 
+	mask = bpf_cpumask_create();
+	if (!mask) {
+		bpf_task_release(p);
+		return -ENOMEM;
+	}
+
+	if (input->allowed_cpu < 0)
+		bpf_cpumask_setall(mask);
+	else
+		bpf_cpumask_set_cpu(input->allowed_cpu, mask);
+
 	bpf_rcu_read_lock();
-	cpu = scx_bpf_select_cpu_and(p, bpf_get_smp_processor_id(), 0, p->cpus_ptr, 0);
-	bpf_rcu_read_unlock();
+	if (input->check_idle) {
+		idle = scx_bpf_get_idle_cpumask();
+		if (!bpf_cpumask_test_cpu(input->prev_cpu, idle)) {
+			scx_bpf_put_idle_cpumask(idle);
+			bpf_rcu_read_unlock();
+			bpf_cpumask_release(mask);
+			bpf_task_release(p);
+			return -EAGAIN;
+		}
+		scx_bpf_put_idle_cpumask(idle);
+	}
 
+	cpu = scx_bpf_select_cpu_and(p, input->prev_cpu, input->wake_flags,
+				     cast_mask(mask), input->flags);
+
+	bpf_rcu_read_unlock();
+	bpf_cpumask_release(mask);
 	bpf_task_release(p);
 
 	return cpu;
tools/testing/selftests/sched_ext/allowed_cpus.c +308 -8

354 lines not on this page · raw message

Signature · 1 line
2.55.0