* [PATCH] selftests/sched_ext: Fix bpf_link leak on early return in prog_run
@ 2026-07-09 10:03 luoliang
2026-07-09 20:32 ` Andrea Righi
2026-07-09 21:40 ` Tejun Heo
0 siblings, 2 replies; 3+ messages in thread
From: luoliang @ 2026-07-09 10:03 UTC (permalink / raw)
To: Tejun Heo, David Vernet, Andrea Righi, Changwoo Min, Shuah Khan
Cc: sched-ext, linux-kselftest, linux-kernel, Liang Luo
From: Liang Luo <luoliang@kylinos.cn>
In prog_run's run(), the bpf_link is attached early but only destroyed
on the success path. The three SCX_EQ assertions between attach and
destroy expand to a direct 'return SCX_TEST_FAIL', so if any of them
triggers, bpf_link__destroy() is never reached and the BPF scheduler
stays loaded. All subsequent tests then fail to attach because SCX is
not in the DISABLED state.
Convert those assertions to explicit checks that jump to a unified
'out' label which always runs the cleanup, matching the pattern used
in cyclic_kick_wait.c.
Signed-off-by: Liang Luo <luoliang@kylinos.cn>
---
tools/testing/selftests/sched_ext/prog_run.c | 34 +++++++++++++++-----
1 file changed, 26 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/sched_ext/prog_run.c b/tools/testing/selftests/sched_ext/prog_run.c
index 05974820ca69..1129ec2aaddc 100644
--- a/tools/testing/selftests/sched_ext/prog_run.c
+++ b/tools/testing/selftests/sched_ext/prog_run.c
@@ -28,7 +28,8 @@ static enum scx_test_status setup(void **ctx)
static enum scx_test_status run(void *ctx)
{
struct prog_run *skel = ctx;
- struct bpf_link *link;
+ struct bpf_link *link = NULL;
+ enum scx_test_status status = SCX_TEST_PASS;
int prog_fd, err = 0;
prog_fd = bpf_program__fd(skel->progs.prog_run_syscall);
@@ -42,23 +43,40 @@ static enum scx_test_status run(void *ctx)
link = bpf_map__attach_struct_ops(skel->maps.prog_run_ops);
if (!link) {
SCX_ERR("Failed to attach scheduler");
- close(prog_fd);
- return SCX_TEST_FAIL;
+ status = SCX_TEST_FAIL;
+ goto out;
}
err = bpf_prog_test_run_opts(prog_fd, &topts);
- SCX_EQ(err, 0);
+ if (err) {
+ SCX_ERR("BPF_PROG_RUN failed (%d)", err);
+ status = SCX_TEST_FAIL;
+ goto out;
+ }
/* Assumes uei.kind is written last */
while (skel->data->uei.kind == EXIT_KIND(SCX_EXIT_NONE))
sched_yield();
- SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG_BPF));
- SCX_EQ(skel->data->uei.exit_code, 0xdeadbeef);
+ if (skel->data->uei.kind != EXIT_KIND(SCX_EXIT_UNREG_BPF)) {
+ SCX_ERR("Unexpected exit kind: %llu",
+ (unsigned long long)skel->data->uei.kind);
+ status = SCX_TEST_FAIL;
+ goto out;
+ }
+ if (skel->data->uei.exit_code != 0xdeadbeef) {
+ SCX_ERR("Unexpected exit code: %lld",
+ (long long)skel->data->uei.exit_code);
+ status = SCX_TEST_FAIL;
+ goto out;
+ }
+
+out:
close(prog_fd);
- bpf_link__destroy(link);
+ if (link)
+ bpf_link__destroy(link);
- return SCX_TEST_PASS;
+ return status;
}
static void cleanup(void *ctx)
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/sched_ext: Fix bpf_link leak on early return in prog_run
2026-07-09 10:03 [PATCH] selftests/sched_ext: Fix bpf_link leak on early return in prog_run luoliang
@ 2026-07-09 20:32 ` Andrea Righi
2026-07-09 21:40 ` Tejun Heo
1 sibling, 0 replies; 3+ messages in thread
From: Andrea Righi @ 2026-07-09 20:32 UTC (permalink / raw)
To: luoliang
Cc: Tejun Heo, David Vernet, Changwoo Min, Shuah Khan, sched-ext,
linux-kselftest, linux-kernel
Hi Liang,
On Thu, Jul 09, 2026 at 06:03:40PM +0800, luoliang@kylinos.cn wrote:
> From: Liang Luo <luoliang@kylinos.cn>
>
> In prog_run's run(), the bpf_link is attached early but only destroyed
> on the success path. The three SCX_EQ assertions between attach and
> destroy expand to a direct 'return SCX_TEST_FAIL', so if any of them
> triggers, bpf_link__destroy() is never reached and the BPF scheduler
> stays loaded. All subsequent tests then fail to attach because SCX is
> not in the DISABLED state.
>
> Convert those assertions to explicit checks that jump to a unified
> 'out' label which always runs the cleanup, matching the pattern used
> in cyclic_kick_wait.c.
>
> Signed-off-by: Liang Luo <luoliang@kylinos.cn>
We could also add (optionally):
Fixes: a5db7817af78 ("sched_ext: Add selftests")
Other than that, looks good to me.
Reviewed-by: Andrea Righi <arighi@nvidia.com>
Thanks,
-Andrea
> ---
> tools/testing/selftests/sched_ext/prog_run.c | 34 +++++++++++++++-----
> 1 file changed, 26 insertions(+), 8 deletions(-)
>
> diff --git a/tools/testing/selftests/sched_ext/prog_run.c b/tools/testing/selftests/sched_ext/prog_run.c
> index 05974820ca69..1129ec2aaddc 100644
> --- a/tools/testing/selftests/sched_ext/prog_run.c
> +++ b/tools/testing/selftests/sched_ext/prog_run.c
> @@ -28,7 +28,8 @@ static enum scx_test_status setup(void **ctx)
> static enum scx_test_status run(void *ctx)
> {
> struct prog_run *skel = ctx;
> - struct bpf_link *link;
> + struct bpf_link *link = NULL;
> + enum scx_test_status status = SCX_TEST_PASS;
> int prog_fd, err = 0;
>
> prog_fd = bpf_program__fd(skel->progs.prog_run_syscall);
> @@ -42,23 +43,40 @@ static enum scx_test_status run(void *ctx)
> link = bpf_map__attach_struct_ops(skel->maps.prog_run_ops);
> if (!link) {
> SCX_ERR("Failed to attach scheduler");
> - close(prog_fd);
> - return SCX_TEST_FAIL;
> + status = SCX_TEST_FAIL;
> + goto out;
> }
>
> err = bpf_prog_test_run_opts(prog_fd, &topts);
> - SCX_EQ(err, 0);
> + if (err) {
> + SCX_ERR("BPF_PROG_RUN failed (%d)", err);
> + status = SCX_TEST_FAIL;
> + goto out;
> + }
>
> /* Assumes uei.kind is written last */
> while (skel->data->uei.kind == EXIT_KIND(SCX_EXIT_NONE))
> sched_yield();
>
> - SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG_BPF));
> - SCX_EQ(skel->data->uei.exit_code, 0xdeadbeef);
> + if (skel->data->uei.kind != EXIT_KIND(SCX_EXIT_UNREG_BPF)) {
> + SCX_ERR("Unexpected exit kind: %llu",
> + (unsigned long long)skel->data->uei.kind);
> + status = SCX_TEST_FAIL;
> + goto out;
> + }
> + if (skel->data->uei.exit_code != 0xdeadbeef) {
> + SCX_ERR("Unexpected exit code: %lld",
> + (long long)skel->data->uei.exit_code);
> + status = SCX_TEST_FAIL;
> + goto out;
> + }
> +
> +out:
> close(prog_fd);
> - bpf_link__destroy(link);
> + if (link)
> + bpf_link__destroy(link);
>
> - return SCX_TEST_PASS;
> + return status;
> }
>
> static void cleanup(void *ctx)
> --
> 2.43.0
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] selftests/sched_ext: Fix bpf_link leak on early return in prog_run
2026-07-09 10:03 [PATCH] selftests/sched_ext: Fix bpf_link leak on early return in prog_run luoliang
2026-07-09 20:32 ` Andrea Righi
@ 2026-07-09 21:40 ` Tejun Heo
1 sibling, 0 replies; 3+ messages in thread
From: Tejun Heo @ 2026-07-09 21:40 UTC (permalink / raw)
To: Liang Luo
Cc: David Vernet, Andrea Righi, Changwoo Min, Shuah Khan, sched-ext,
linux-kselftest, linux-kernel, Emil Tsalapatis
Applied to sched_ext/for-7.3, with the Fixes tag Andrea suggested:
Fixes: a5db7817af78 ("sched_ext: Add selftests")
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-07-09 21:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-09 10:03 [PATCH] selftests/sched_ext: Fix bpf_link leak on early return in prog_run luoliang
2026-07-09 20:32 ` Andrea Righi
2026-07-09 21:40 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox