]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
selftests/sched_ext: Update test enq_select_cpu_fails
authorAndrea Righi <arighi@nvidia.com>
Wed, 21 May 2025 07:34:07 +0000 (09:34 +0200)
committerTejun Heo <tj@kernel.org>
Wed, 21 May 2025 17:35:58 +0000 (07:35 -1000)
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.

Signed-off-by: Andrea Righi <arighi@nvidia.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
tools/testing/selftests/sched_ext/Makefile
tools/testing/selftests/sched_ext/enq_select_cpu.bpf.c [new file with mode: 0644]
tools/testing/selftests/sched_ext/enq_select_cpu.c [new file with mode: 0644]
tools/testing/selftests/sched_ext/enq_select_cpu_fails.bpf.c [deleted file]
tools/testing/selftests/sched_ext/enq_select_cpu_fails.c [deleted file]

index e9d5bc575f806c4d355c97b1097c44d401e22615..9d9d6b4c38b015cc5c2b051150823bd347ab015a 100644 (file)
@@ -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               \
diff --git a/tools/testing/selftests/sched_ext/enq_select_cpu.bpf.c b/tools/testing/selftests/sched_ext/enq_select_cpu.bpf.c
new file mode 100644 (file)
index 0000000..ee2c9b8
--- /dev/null
@@ -0,0 +1,74 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
+ * Copyright (c) 2023 David Vernet <dvernet@meta.com>
+ * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
+ */
+
+#include <scx/common.bpf.h>
+
+char _license[] SEC("license") = "GPL";
+
+UEI_DEFINE(uei);
+
+s32 BPF_STRUCT_OPS(enq_select_cpu_select_cpu, struct task_struct *p,
+                  s32 prev_cpu, u64 wake_flags)
+{
+       /* Bounce all tasks to ops.enqueue() */
+       return prev_cpu;
+}
+
+void BPF_STRUCT_OPS(enq_select_cpu_enqueue, struct task_struct *p,
+                   u64 enq_flags)
+{
+       s32 cpu, prev_cpu = scx_bpf_task_cpu(p);
+       bool found = false;
+
+       cpu = scx_bpf_select_cpu_dfl(p, prev_cpu, 0, &found);
+       if (found) {
+               scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL_ON | cpu, SCX_SLICE_DFL, enq_flags);
+               return;
+       }
+
+       scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
+}
+
+void BPF_STRUCT_OPS(enq_select_cpu_exit, struct scx_exit_info *ei)
+{
+       UEI_RECORD(uei, ei);
+}
+
+struct task_cpu_arg {
+       pid_t pid;
+};
+
+SEC("syscall")
+int select_cpu_from_user(struct task_cpu_arg *input)
+{
+       struct task_struct *p;
+       bool found = false;
+       s32 cpu;
+
+       p = bpf_task_from_pid(input->pid);
+       if (!p)
+               return -EINVAL;
+
+       bpf_rcu_read_lock();
+       cpu = scx_bpf_select_cpu_dfl(p, bpf_get_smp_processor_id(), 0, &found);
+       if (!found)
+               cpu = -EBUSY;
+       bpf_rcu_read_unlock();
+
+       bpf_task_release(p);
+
+       return cpu;
+}
+
+SEC(".struct_ops.link")
+struct sched_ext_ops enq_select_cpu_ops = {
+       .select_cpu             = (void *)enq_select_cpu_select_cpu,
+       .enqueue                = (void *)enq_select_cpu_enqueue,
+       .exit                   = (void *)enq_select_cpu_exit,
+       .name                   = "enq_select_cpu",
+       .timeout_ms             = 1000U,
+};
diff --git a/tools/testing/selftests/sched_ext/enq_select_cpu.c b/tools/testing/selftests/sched_ext/enq_select_cpu.c
new file mode 100644 (file)
index 0000000..340c6f8
--- /dev/null
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
+ * Copyright (c) 2023 David Vernet <dvernet@meta.com>
+ * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
+ */
+#include <bpf/bpf.h>
+#include <scx/common.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include "enq_select_cpu.bpf.skel.h"
+#include "scx_test.h"
+
+static enum scx_test_status setup(void **ctx)
+{
+       struct enq_select_cpu *skel;
+
+       skel = enq_select_cpu__open();
+       SCX_FAIL_IF(!skel, "Failed to open");
+       SCX_ENUM_INIT(skel);
+       SCX_FAIL_IF(enq_select_cpu__load(skel), "Failed to load skel");
+
+       *ctx = skel;
+
+       return SCX_TEST_PASS;
+}
+
+static int test_select_cpu_from_user(const struct enq_select_cpu *skel)
+{
+       int fd, ret;
+       __u64 args[1];
+
+       LIBBPF_OPTS(bpf_test_run_opts, attr,
+               .ctx_in = args,
+               .ctx_size_in = sizeof(args),
+       );
+
+       args[0] = getpid();
+       fd = bpf_program__fd(skel->progs.select_cpu_from_user);
+       if (fd < 0)
+               return fd;
+
+       ret = bpf_prog_test_run_opts(fd, &attr);
+       if (ret < 0)
+               return ret;
+
+       fprintf(stderr, "%s: CPU %d\n", __func__, attr.retval);
+
+       return 0;
+}
+
+static enum scx_test_status run(void *ctx)
+{
+       struct enq_select_cpu *skel = ctx;
+       struct bpf_link *link;
+
+       link = bpf_map__attach_struct_ops(skel->maps.enq_select_cpu_ops);
+       if (!link) {
+               SCX_ERR("Failed to attach scheduler");
+               return SCX_TEST_FAIL;
+       }
+
+       /* Pick an idle CPU from user-space */
+       SCX_FAIL_IF(test_select_cpu_from_user(skel), "Failed to pick idle CPU");
+
+       sleep(1);
+
+       SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_NONE));
+       bpf_link__destroy(link);
+
+       return SCX_TEST_PASS;
+}
+
+static void cleanup(void *ctx)
+{
+       struct enq_select_cpu *skel = ctx;
+
+       enq_select_cpu__destroy(skel);
+}
+
+struct scx_test enq_select_cpu = {
+       .name = "enq_select_cpu",
+       .description = "Verify scx_bpf_select_cpu_dfl() from multiple contexts",
+       .setup = setup,
+       .run = run,
+       .cleanup = cleanup,
+};
+REGISTER_SCX_TEST(&enq_select_cpu)
diff --git a/tools/testing/selftests/sched_ext/enq_select_cpu_fails.bpf.c b/tools/testing/selftests/sched_ext/enq_select_cpu_fails.bpf.c
deleted file mode 100644 (file)
index a7cf868..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
- * Copyright (c) 2023 David Vernet <dvernet@meta.com>
- * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
- */
-
-#include <scx/common.bpf.h>
-
-char _license[] SEC("license") = "GPL";
-
-/* Manually specify the signature until the kfunc is added to the scx repo. */
-s32 scx_bpf_select_cpu_dfl(struct task_struct *p, s32 prev_cpu, u64 wake_flags,
-                          bool *found) __ksym;
-
-s32 BPF_STRUCT_OPS(enq_select_cpu_fails_select_cpu, struct task_struct *p,
-                  s32 prev_cpu, u64 wake_flags)
-{
-       return prev_cpu;
-}
-
-void BPF_STRUCT_OPS(enq_select_cpu_fails_enqueue, struct task_struct *p,
-                   u64 enq_flags)
-{
-       /*
-        * Need to initialize the variable or the verifier will fail to load.
-        * Improving these semantics is actively being worked on.
-        */
-       bool found = false;
-
-       /* Can only call from ops.select_cpu() */
-       scx_bpf_select_cpu_dfl(p, 0, 0, &found);
-
-       scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
-}
-
-SEC(".struct_ops.link")
-struct sched_ext_ops enq_select_cpu_fails_ops = {
-       .select_cpu             = (void *) enq_select_cpu_fails_select_cpu,
-       .enqueue                = (void *) enq_select_cpu_fails_enqueue,
-       .name                   = "enq_select_cpu_fails",
-       .timeout_ms             = 1000U,
-};
diff --git a/tools/testing/selftests/sched_ext/enq_select_cpu_fails.c b/tools/testing/selftests/sched_ext/enq_select_cpu_fails.c
deleted file mode 100644 (file)
index a80e3a3..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
- * Copyright (c) 2023 David Vernet <dvernet@meta.com>
- * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
- */
-#include <bpf/bpf.h>
-#include <scx/common.h>
-#include <sys/wait.h>
-#include <unistd.h>
-#include "enq_select_cpu_fails.bpf.skel.h"
-#include "scx_test.h"
-
-static enum scx_test_status setup(void **ctx)
-{
-       struct enq_select_cpu_fails *skel;
-
-       skel = enq_select_cpu_fails__open();
-       SCX_FAIL_IF(!skel, "Failed to open");
-       SCX_ENUM_INIT(skel);
-       SCX_FAIL_IF(enq_select_cpu_fails__load(skel), "Failed to load skel");
-
-       *ctx = skel;
-
-       return SCX_TEST_PASS;
-}
-
-static enum scx_test_status run(void *ctx)
-{
-       struct enq_select_cpu_fails *skel = ctx;
-       struct bpf_link *link;
-
-       link = bpf_map__attach_struct_ops(skel->maps.enq_select_cpu_fails_ops);
-       if (!link) {
-               SCX_ERR("Failed to attach scheduler");
-               return SCX_TEST_FAIL;
-       }
-
-       sleep(1);
-
-       bpf_link__destroy(link);
-
-       return SCX_TEST_PASS;
-}
-
-static void cleanup(void *ctx)
-{
-       struct enq_select_cpu_fails *skel = ctx;
-
-       enq_select_cpu_fails__destroy(skel);
-}
-
-struct scx_test enq_select_cpu_fails = {
-       .name = "enq_select_cpu_fails",
-       .description = "Verify we fail to call scx_bpf_select_cpu_dfl() "
-                      "from ops.enqueue()",
-       .setup = setup,
-       .run = run,
-       .cleanup = cleanup,
-};
-REGISTER_SCX_TEST(&enq_select_cpu_fails)