Skip to content

Commit e764295

Browse files
arighihtejun
authored andcommitted
selftests/sched_ext: Add test for scx_bpf_select_cpu_and() via test_run
Update the allowed_cpus selftest to include a check to validate the behavior of scx_bpf_select_cpu_and() when invoked via a BPF test_run call. Signed-off-by: Andrea Righi <arighi@nvidia.com> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 4ac760b commit e764295

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tools/testing/selftests/sched_ext/allowed_cpus.bpf.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,29 @@ void BPF_STRUCT_OPS(allowed_cpus_exit, struct scx_exit_info *ei)
111111
UEI_RECORD(uei, ei);
112112
}
113113

114+
struct task_cpu_arg {
115+
pid_t pid;
116+
};
117+
118+
SEC("syscall")
119+
int select_cpu_from_user(struct task_cpu_arg *input)
120+
{
121+
struct task_struct *p;
122+
int cpu;
123+
124+
p = bpf_task_from_pid(input->pid);
125+
if (!p)
126+
return -EINVAL;
127+
128+
bpf_rcu_read_lock();
129+
cpu = scx_bpf_select_cpu_and(p, bpf_get_smp_processor_id(), 0, p->cpus_ptr, 0);
130+
bpf_rcu_read_unlock();
131+
132+
bpf_task_release(p);
133+
134+
return cpu;
135+
}
136+
114137
SEC(".struct_ops.link")
115138
struct sched_ext_ops allowed_cpus_ops = {
116139
.select_cpu = (void *)allowed_cpus_select_cpu,

tools/testing/selftests/sched_ext/allowed_cpus.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ static enum scx_test_status setup(void **ctx)
2323
return SCX_TEST_PASS;
2424
}
2525

26+
static int test_select_cpu_from_user(const struct allowed_cpus *skel)
27+
{
28+
int fd, ret;
29+
__u64 args[1];
30+
31+
LIBBPF_OPTS(bpf_test_run_opts, attr,
32+
.ctx_in = args,
33+
.ctx_size_in = sizeof(args),
34+
);
35+
36+
args[0] = getpid();
37+
fd = bpf_program__fd(skel->progs.select_cpu_from_user);
38+
if (fd < 0)
39+
return fd;
40+
41+
ret = bpf_prog_test_run_opts(fd, &attr);
42+
if (ret < 0)
43+
return ret;
44+
45+
fprintf(stderr, "%s: CPU %d\n", __func__, attr.retval);
46+
47+
return 0;
48+
}
49+
2650
static enum scx_test_status run(void *ctx)
2751
{
2852
struct allowed_cpus *skel = ctx;
@@ -31,6 +55,9 @@ static enum scx_test_status run(void *ctx)
3155
link = bpf_map__attach_struct_ops(skel->maps.allowed_cpus_ops);
3256
SCX_FAIL_IF(!link, "Failed to attach scheduler");
3357

58+
/* Pick an idle CPU from user-space */
59+
SCX_FAIL_IF(test_select_cpu_from_user(skel), "Failed to pick idle CPU");
60+
3461
/* Just sleeping is fine, plenty of scheduling events happening */
3562
sleep(1);
3663

0 commit comments

Comments
 (0)