]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Introduce __load_if_JITed annotation for tests
authorJiayuan Chen <mrpre@163.com>
Fri, 14 Feb 2025 09:18:22 +0000 (17:18 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 15 Feb 2025 03:55:15 +0000 (19:55 -0800)
In some cases, the verification logic under the interpreter and JIT
differs, such as may_goto, and the test program behaves differently under
different runtime modes, requiring separate verification logic for each
result.

Introduce __load_if_JITed and __load_if_no_JITed annotation for tests.

Signed-off-by: Jiayuan Chen <mrpre@163.com>
Link: https://lore.kernel.org/r/20250214091823.46042-3-mrpre@163.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/bpf_misc.h
tools/testing/selftests/bpf/test_loader.c

index 02c9f7964e563b2a6c508fec9d9edf7f8fbc746c..34f555da546fb01a9f7a3a18c0c8cf87d4082b09 100644 (file)
 #define __arch_arm64           __arch("ARM64")
 #define __arch_riscv64         __arch("RISCV64")
 #define __caps_unpriv(caps)    __attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))
+#define __load_if_JITed()      __attribute__((btf_decl_tag("comment:load_mode=jited")))
+#define __load_if_no_JITed()   __attribute__((btf_decl_tag("comment:load_mode=no_jited")))
 
 /* Define common capabilities tested using __caps_unpriv */
 #define CAP_NET_ADMIN          12
index 53b06647cf57dbafdc8bb63c5f00b420ef84e358..4d23a9c463ee2747549564125f5d3e1aa0d56ff0 100644 (file)
@@ -37,6 +37,7 @@
 #define TEST_TAG_JITED_PFX "comment:test_jited="
 #define TEST_TAG_JITED_PFX_UNPRIV "comment:test_jited_unpriv="
 #define TEST_TAG_CAPS_UNPRIV "comment:test_caps_unpriv="
+#define TEST_TAG_LOAD_MODE_PFX "comment:load_mode="
 
 /* Warning: duplicated in bpf_misc.h */
 #define POINTER_VALUE  0xcafe4all
@@ -55,6 +56,11 @@ enum mode {
        UNPRIV = 2
 };
 
+enum load_mode {
+       JITED           = 1 << 0,
+       NO_JITED        = 1 << 1,
+};
+
 struct expect_msg {
        const char *substr; /* substring match */
        regex_t regex;
@@ -87,6 +93,7 @@ struct test_spec {
        int prog_flags;
        int mode_mask;
        int arch_mask;
+       int load_mask;
        bool auxiliary;
        bool valid;
 };
@@ -406,6 +413,7 @@ static int parse_test_spec(struct test_loader *tester,
        bool collect_jit = false;
        int func_id, i, err = 0;
        u32 arch_mask = 0;
+       u32 load_mask = 0;
        struct btf *btf;
        enum arch arch;
 
@@ -580,10 +588,22 @@ static int parse_test_spec(struct test_loader *tester,
                        if (err)
                                goto cleanup;
                        spec->mode_mask |= UNPRIV;
+               } else if (str_has_pfx(s, TEST_TAG_LOAD_MODE_PFX)) {
+                       val = s + sizeof(TEST_TAG_LOAD_MODE_PFX) - 1;
+                       if (strcmp(val, "jited") == 0) {
+                               load_mask = JITED;
+                       } else if (strcmp(val, "no_jited") == 0) {
+                               load_mask = NO_JITED;
+                       } else {
+                               PRINT_FAIL("bad load spec: '%s'", val);
+                               err = -EINVAL;
+                               goto cleanup;
+                       }
                }
        }
 
        spec->arch_mask = arch_mask ?: -1;
+       spec->load_mask = load_mask ?: (JITED | NO_JITED);
 
        if (spec->mode_mask == 0)
                spec->mode_mask = PRIV;
@@ -928,6 +948,7 @@ void run_subtest(struct test_loader *tester,
                 bool unpriv)
 {
        struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv;
+       int current_runtime = is_jit_enabled() ? JITED : NO_JITED;
        struct bpf_program *tprog = NULL, *tprog_iter;
        struct bpf_link *link, *links[32] = {};
        struct test_spec *spec_iter;
@@ -946,6 +967,11 @@ void run_subtest(struct test_loader *tester,
                return;
        }
 
+       if ((current_runtime & spec->load_mask) == 0) {
+               test__skip();
+               return;
+       }
+
        if (unpriv) {
                if (!can_execute_unpriv(tester, spec)) {
                        test__skip();