Make sure BPF registration and unregistration leave io_uring in the right state. Signed-off-by: Pavel Begunkov --- tools/testing/selftests/io_uring/Makefile | 2 +- .../testing/selftests/io_uring/common-defs.h | 4 + tools/testing/selftests/io_uring/unreg.bpf.c | 25 +++++ tools/testing/selftests/io_uring/unreg.c | 92 +++++++++++++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/io_uring/unreg.bpf.c create mode 100644 tools/testing/selftests/io_uring/unreg.c diff --git a/tools/testing/selftests/io_uring/Makefile b/tools/testing/selftests/io_uring/Makefile index 77df197449ef..37f50acdba37 100644 --- a/tools/testing/selftests/io_uring/Makefile +++ b/tools/testing/selftests/io_uring/Makefile @@ -3,7 +3,7 @@ include ../../../build/Build.include include ../../../scripts/Makefile.arch include ../../../scripts/Makefile.include -TEST_GEN_PROGS := nops_loop overflow +TEST_GEN_PROGS := nops_loop overflow unreg # override lib.mk's default rules OVERRIDE_TARGETS := 1 diff --git a/tools/testing/selftests/io_uring/common-defs.h b/tools/testing/selftests/io_uring/common-defs.h index 948453c90375..9a44e0687436 100644 --- a/tools/testing/selftests/io_uring/common-defs.h +++ b/tools/testing/selftests/io_uring/common-defs.h @@ -24,4 +24,8 @@ struct nops_state { int reqs_left; }; +struct unreg_state { + unsigned times_invoked; +}; + #endif /* IOU_TOOLS_COMMON_DEFS_H */ diff --git a/tools/testing/selftests/io_uring/unreg.bpf.c b/tools/testing/selftests/io_uring/unreg.bpf.c new file mode 100644 index 000000000000..e872915b09dd --- /dev/null +++ b/tools/testing/selftests/io_uring/unreg.bpf.c @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include +#include +#include +#include "vmlinux.h" +#include "common-defs.h" + +char LICENSE[] SEC("license") = "Dual BSD/GPL"; + +SEC("struct_ops.s/unreg_loop_step") +int BPF_PROG(unreg_loop_step, struct io_ring_ctx *ring, + struct iou_loop_params *ls) +{ + struct unreg_state *us; + + us = (void *)bpf_io_uring_get_region(ring, IOU_REGION_MEM, sizeof(*us)); + if (us) + us->times_invoked++; + return IOU_LOOP_STOP; +} + +SEC(".struct_ops.link") +struct io_uring_bpf_ops unreg_ops = { + .loop_step = (void *)unreg_loop_step, +}; diff --git a/tools/testing/selftests/io_uring/unreg.c b/tools/testing/selftests/io_uring/unreg.c new file mode 100644 index 000000000000..43076681999f --- /dev/null +++ b/tools/testing/selftests/io_uring/unreg.c @@ -0,0 +1,92 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Test BPF registration / unregistration works and doesn't leave a dangling + * function pointer. + */ +#include +#include +#include +#include + +#include +#include + +#include "helpers.h" +#include "unreg.bpf.skel.h" + +static struct unreg *load_unreg(struct ring_ctx *ctx) +{ + struct unreg *skel; + int ret; + + skel = unreg__open(); + if (!skel) { + fprintf(stderr, "can't generate skeleton\n"); + exit(1); + } + + skel->struct_ops.unreg_ops->ring_fd = ctx->ring.ring_fd; + + ret = unreg__load(skel); + if (ret) { + fprintf(stderr, "failed to load skeleton\n"); + exit(1); + } + + return skel; +} + +int main() +{ + struct bpf_link *link1, *link2; + struct unreg *skel1, *skel2; + struct unreg_state *us; + struct ring_ctx ctx; + + ring_ctx_create(&ctx, sizeof(struct unreg_state)); + us = ctx.region; + + skel1 = load_unreg(&ctx); + skel2 = load_unreg(&ctx); + + link1 = bpf_map__attach_struct_ops(skel1->maps.unreg_ops); + if (!link1) { + fprintf(stderr, "failed to attach ops\n"); + return 1; + } + + ring_ctx_run(&ctx); + if (us->times_invoked != 1) { + fprintf(stderr, "failed to run BPF\n"); + return 1; + } + + /* remove the program and give the kernel time to actually destroy it */ + bpf_link__destroy(link1); + unreg__destroy(skel1); + sleep(1); + + ring_ctx_run(&ctx); + if (us->times_invoked != 1) { + fprintf(stderr, "Executed removed BPF\n"); + return 1; + } + + /* try to attach another program */ + link2 = bpf_map__attach_struct_ops(skel2->maps.unreg_ops); + if (!link2) { + fprintf(stderr, "failed to reattach ops\n"); + return 1; + } + + ring_ctx_run(&ctx); + if (us->times_invoked != 2) { + fprintf(stderr, "failed to run reattached BPF\n"); + return 1; + } + + bpf_link__destroy(link2); + unreg__destroy(skel2); + ring_ctx_destroy(&ctx); + return 0; +} -- 2.53.0