Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Test subprog jit when toggle bpf_jit_harden repeatedly

When bpf_jit_harden is toggled between 0 and 2, subprog jit may fail
due to inconsistent twice read values of bpf_jit_harden during jit. So
add a test to ensure the problem is fixed.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220309123321.2400262-5-houtao1@huawei.com

authored by

Hou Tao and committed by
Alexei Starovoitov
ad13baf4 d2a3b7c5

+68 -9
+68 -9
tools/testing/selftests/bpf/prog_tests/subprogs.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* Copyright (c) 2020 Facebook */ 3 3 #include <test_progs.h> 4 - #include <time.h> 5 4 #include "test_subprogs.skel.h" 6 5 #include "test_subprogs_unused.skel.h" 7 6 8 - static int duration; 7 + struct toggler_ctx { 8 + int fd; 9 + bool stop; 10 + }; 9 11 10 - void test_subprogs(void) 12 + static void *toggle_jit_harden(void *arg) 13 + { 14 + struct toggler_ctx *ctx = arg; 15 + char two = '2'; 16 + char zero = '0'; 17 + 18 + while (!ctx->stop) { 19 + lseek(ctx->fd, SEEK_SET, 0); 20 + write(ctx->fd, &two, sizeof(two)); 21 + lseek(ctx->fd, SEEK_SET, 0); 22 + write(ctx->fd, &zero, sizeof(zero)); 23 + } 24 + 25 + return NULL; 26 + } 27 + 28 + static void test_subprogs_with_jit_harden_toggling(void) 29 + { 30 + struct toggler_ctx ctx; 31 + pthread_t toggler; 32 + int err; 33 + unsigned int i, loop = 10; 34 + 35 + ctx.fd = open("/proc/sys/net/core/bpf_jit_harden", O_RDWR); 36 + if (!ASSERT_GE(ctx.fd, 0, "open bpf_jit_harden")) 37 + return; 38 + 39 + ctx.stop = false; 40 + err = pthread_create(&toggler, NULL, toggle_jit_harden, &ctx); 41 + if (!ASSERT_OK(err, "new toggler")) 42 + goto out; 43 + 44 + /* Make toggler thread to run */ 45 + usleep(1); 46 + 47 + for (i = 0; i < loop; i++) { 48 + struct test_subprogs *skel = test_subprogs__open_and_load(); 49 + 50 + if (!ASSERT_OK_PTR(skel, "skel open")) 51 + break; 52 + test_subprogs__destroy(skel); 53 + } 54 + 55 + ctx.stop = true; 56 + pthread_join(toggler, NULL); 57 + out: 58 + close(ctx.fd); 59 + } 60 + 61 + static void test_subprogs_alone(void) 11 62 { 12 63 struct test_subprogs *skel; 13 64 struct test_subprogs_unused *skel2; 14 65 int err; 15 66 16 67 skel = test_subprogs__open_and_load(); 17 - if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) 68 + if (!ASSERT_OK_PTR(skel, "skel_open")) 18 69 return; 19 70 20 71 err = test_subprogs__attach(skel); 21 - if (CHECK(err, "skel_attach", "failed to attach skeleton: %d\n", err)) 72 + if (!ASSERT_OK(err, "skel attach")) 22 73 goto cleanup; 23 74 24 75 usleep(1); 25 76 26 - CHECK(skel->bss->res1 != 12, "res1", "got %d, exp %d\n", skel->bss->res1, 12); 27 - CHECK(skel->bss->res2 != 17, "res2", "got %d, exp %d\n", skel->bss->res2, 17); 28 - CHECK(skel->bss->res3 != 19, "res3", "got %d, exp %d\n", skel->bss->res3, 19); 29 - CHECK(skel->bss->res4 != 36, "res4", "got %d, exp %d\n", skel->bss->res4, 36); 77 + ASSERT_EQ(skel->bss->res1, 12, "res1"); 78 + ASSERT_EQ(skel->bss->res2, 17, "res2"); 79 + ASSERT_EQ(skel->bss->res3, 19, "res3"); 80 + ASSERT_EQ(skel->bss->res4, 36, "res4"); 30 81 31 82 skel2 = test_subprogs_unused__open_and_load(); 32 83 ASSERT_OK_PTR(skel2, "unused_progs_skel"); ··· 85 34 86 35 cleanup: 87 36 test_subprogs__destroy(skel); 37 + } 38 + 39 + void test_subprogs(void) 40 + { 41 + if (test__start_subtest("subprogs_alone")) 42 + test_subprogs_alone(); 43 + if (test__start_subtest("subprogs_and_jit_harden")) 44 + test_subprogs_with_jit_harden_toggling(); 88 45 }