xref: /linux/tools/testing/selftests/sched_ext/prog_run.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1*a5db7817SDavid Vernet /* SPDX-License-Identifier: GPL-2.0 */
2*a5db7817SDavid Vernet /*
3*a5db7817SDavid Vernet  * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
4*a5db7817SDavid Vernet  * Copyright (c) 2024 David Vernet <dvernet@meta.com>
5*a5db7817SDavid Vernet  */
6*a5db7817SDavid Vernet #include <bpf/bpf.h>
7*a5db7817SDavid Vernet #include <sched.h>
8*a5db7817SDavid Vernet #include <scx/common.h>
9*a5db7817SDavid Vernet #include <sys/wait.h>
10*a5db7817SDavid Vernet #include <unistd.h>
11*a5db7817SDavid Vernet #include "prog_run.bpf.skel.h"
12*a5db7817SDavid Vernet #include "scx_test.h"
13*a5db7817SDavid Vernet 
setup(void ** ctx)14*a5db7817SDavid Vernet static enum scx_test_status setup(void **ctx)
15*a5db7817SDavid Vernet {
16*a5db7817SDavid Vernet 	struct prog_run *skel;
17*a5db7817SDavid Vernet 
18*a5db7817SDavid Vernet 	skel = prog_run__open_and_load();
19*a5db7817SDavid Vernet 	if (!skel) {
20*a5db7817SDavid Vernet 		SCX_ERR("Failed to open and load skel");
21*a5db7817SDavid Vernet 		return SCX_TEST_FAIL;
22*a5db7817SDavid Vernet 	}
23*a5db7817SDavid Vernet 	*ctx = skel;
24*a5db7817SDavid Vernet 
25*a5db7817SDavid Vernet 	return SCX_TEST_PASS;
26*a5db7817SDavid Vernet }
27*a5db7817SDavid Vernet 
run(void * ctx)28*a5db7817SDavid Vernet static enum scx_test_status run(void *ctx)
29*a5db7817SDavid Vernet {
30*a5db7817SDavid Vernet 	struct prog_run *skel = ctx;
31*a5db7817SDavid Vernet 	struct bpf_link *link;
32*a5db7817SDavid Vernet 	int prog_fd, err = 0;
33*a5db7817SDavid Vernet 
34*a5db7817SDavid Vernet 	prog_fd = bpf_program__fd(skel->progs.prog_run_syscall);
35*a5db7817SDavid Vernet 	if (prog_fd < 0) {
36*a5db7817SDavid Vernet 		SCX_ERR("Failed to get BPF_PROG_RUN prog");
37*a5db7817SDavid Vernet 		return SCX_TEST_FAIL;
38*a5db7817SDavid Vernet 	}
39*a5db7817SDavid Vernet 
40*a5db7817SDavid Vernet 	LIBBPF_OPTS(bpf_test_run_opts, topts);
41*a5db7817SDavid Vernet 
42*a5db7817SDavid Vernet 	link = bpf_map__attach_struct_ops(skel->maps.prog_run_ops);
43*a5db7817SDavid Vernet 	if (!link) {
44*a5db7817SDavid Vernet 		SCX_ERR("Failed to attach scheduler");
45*a5db7817SDavid Vernet 		close(prog_fd);
46*a5db7817SDavid Vernet 		return SCX_TEST_FAIL;
47*a5db7817SDavid Vernet 	}
48*a5db7817SDavid Vernet 
49*a5db7817SDavid Vernet 	err = bpf_prog_test_run_opts(prog_fd, &topts);
50*a5db7817SDavid Vernet 	SCX_EQ(err, 0);
51*a5db7817SDavid Vernet 
52*a5db7817SDavid Vernet 	/* Assumes uei.kind is written last */
53*a5db7817SDavid Vernet 	while (skel->data->uei.kind == EXIT_KIND(SCX_EXIT_NONE))
54*a5db7817SDavid Vernet 		sched_yield();
55*a5db7817SDavid Vernet 
56*a5db7817SDavid Vernet 	SCX_EQ(skel->data->uei.kind, EXIT_KIND(SCX_EXIT_UNREG_BPF));
57*a5db7817SDavid Vernet 	SCX_EQ(skel->data->uei.exit_code, 0xdeadbeef);
58*a5db7817SDavid Vernet 	close(prog_fd);
59*a5db7817SDavid Vernet 	bpf_link__destroy(link);
60*a5db7817SDavid Vernet 
61*a5db7817SDavid Vernet 	return SCX_TEST_PASS;
62*a5db7817SDavid Vernet }
63*a5db7817SDavid Vernet 
cleanup(void * ctx)64*a5db7817SDavid Vernet static void cleanup(void *ctx)
65*a5db7817SDavid Vernet {
66*a5db7817SDavid Vernet 	struct prog_run *skel = ctx;
67*a5db7817SDavid Vernet 
68*a5db7817SDavid Vernet 	prog_run__destroy(skel);
69*a5db7817SDavid Vernet }
70*a5db7817SDavid Vernet 
71*a5db7817SDavid Vernet struct scx_test prog_run = {
72*a5db7817SDavid Vernet 	.name = "prog_run",
73*a5db7817SDavid Vernet 	.description = "Verify we can call into a scheduler with BPF_PROG_RUN, and invoke kfuncs",
74*a5db7817SDavid Vernet 	.setup = setup,
75*a5db7817SDavid Vernet 	.run = run,
76*a5db7817SDavid Vernet 	.cleanup = cleanup,
77*a5db7817SDavid Vernet };
78*a5db7817SDavid Vernet REGISTER_SCX_TEST(&prog_run)
79