selftests/sched_ext: Validate select_cpu_and mask constraints
Subsystems (2)
PATCH 1/1 selftests/sched_ext: Validate select_cpu_and mask constraints
The syscall test checks whether BPF_PROG_TEST_RUN succeeds, but only prints the CPU selection result returned by the BPF program.
Exercise empty and affinity-disjoint custom masks and require -EBUSY. Also exercise a legal singleton mask, allowing its CPU to be busy. Interpret retval as signed and reject unexpected errors or selection outside the custom mask or the task's affinity.
Choose CPUs from the runner's affinity, restore it after the disjoint case, and skip only that case when fewer than two CPUs are available.
The test passes in a two-CPU VM with full and single-CPU affinity. Forcing selection of CPU 0 against an empty mask makes it fail with -ERANGE; the restored test passes again.
--- .../selftests/sched_ext/allowed_cpus.bpf.c | 24 +++++- .../selftests/sched_ext/allowed_cpus.c | 77 ++++++++++++++++--- 2 files changed, 86 insertions(+), 15 deletions(-)
tools/testing/selftests/sched_ext/allowed_cpus.bpf.c +21 -3
@@ -147,23 +147,41 @@ void BPF_STRUCT_OPS(allowed_cpus_exit, struct scx_exit_info *ei) }struct task_cpu_arg { -pid_t pid; +u64 pid; +s64 custom_cpu; }; SEC("syscall")int select_cpu_from_user (struct task_cpu_arg *input) {struct task_struct *p; -int cpu; +struct bpf_cpumask *mask; +s32 cpu; p =bpf_task_from_pid (input->pid);if (!p)return -EINVAL ; + mask =bpf_cpumask_create (); +if (!mask) { +bpf_task_release (p); +return -ENOMEM ; + } + +/* A negative custom_cpu leaves the custom mask empty. */ +if (input->custom_cpu >=0 ) +bpf_cpumask_set_cpu (input->custom_cpu, mask); +bpf_rcu_read_lock (); - cpu =scx_bpf_select_cpu_and (p,bpf_get_smp_processor_id (),0 , p->cpus_ptr,0 ); + cpu =scx_bpf_select_cpu_and (p,bpf_get_smp_processor_id (),0 , +cast_mask (mask),0 ); +if (cpu >=0 && + (!bpf_cpumask_test_cpu (cpu,cast_mask (mask)) || + !bpf_cpumask_test_cpu (cpu, p->cpus_ptr))) + cpu = -ERANGE ;bpf_rcu_read_unlock (); +bpf_cpumask_release (mask);bpf_task_release (p);return cpu;
tools/testing/selftests/sched_ext/allowed_cpus.c +65 -12
117 lines not on this page · raw message
Signature · 1 line
2.55.0
Tejun Heo
reviewerHello, Tianyi.
tools/testing/selftests/sched_ext/allowed_cpus.bpf.c void BPF_STRUCT_OPS(allowed_cpus_exit, struct scx_exit_info *ei) · line 181
Quoted 4 lines · Tianyi Chen
tools/testing/selftests/sched_ext/allowed_cpus.bpf.c +4
-0+if (cpu >=0 && + (!bpf_cpumask_test_cpu (cpu,cast_mask (mask)) || + !bpf_cpumask_test_cpu (cpu, p->cpus_ptr))) + cpu = -ERANGE ;
The syscall runs with migration disabled. With CONFIG_PREEMPT_RCU, preemption between selection and this check can make migrate_disable_switch() narrow p->cpus_ptr to the running CPU, rejecting a valid selection on another CPU. Please check the requested affinity instead.
tools/testing/selftests/sched_ext/allowed_cpus.c static int test_select_cpu_from_user(const struct allowed_cpus *skel) · line 69
Quoted 2 lines
tools/testing/selftests/sched_ext/allowed_cpus.c +2
-0+SCX_FAIL_IF (sched_getaffinity (0 ,sizeof (original), &original), +"Failed to get affinity (%d)" , errno);
Please use dynamically allocated masks. The fixed cpu_set_t makes sched_getaffinity() fail with EINVAL when nr_cpu_ids exceeds 1024, even if the task only uses low-numbered CPUs.
Thanks.
Signature · 1 line
tejun