Skip to content

Commit ee7fe84

Browse files
eddyz87anakryiko
authored andcommitted
selftests/bpf: __arch_* macro to limit test cases to specific archs
Add annotations __arch_x86_64, __arch_arm64, __arch_riscv64 to specify on which architecture the test case should be tested. Several __arch_* annotations could be specified at once. When test case is not run on current arch it is marked as skipped. For example, the following would be tested only on arm64 and riscv64: SEC("raw_tp") __arch_arm64 __arch_riscv64 __xlated("1: *(u64 *)(r10 - 16) = r1") __xlated("2: call") __xlated("3: r1 = *(u64 *)(r10 - 16);") __success __naked void canary_arm64_riscv64(void) { asm volatile ( "r1 = 1;" "*(u64 *)(r10 - 16) = r1;" "call %[bpf_get_smp_processor_id];" "r1 = *(u64 *)(r10 - 16);" "exit;" : : __imm(bpf_get_smp_processor_id) : __clobber_all); } On x86 it would be skipped: torvalds#467/2 verifier_nocsr/canary_arm64_riscv64:SKIP Acked-by: Andrii Nakryiko <[email protected]> Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent 9c9f733 commit ee7fe84

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

tools/testing/selftests/bpf/progs/bpf_misc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
* __auxiliary Annotated program is not a separate test, but used as auxiliary
6464
* for some other test cases and should always be loaded.
6565
* __auxiliary_unpriv Same, but load program in unprivileged mode.
66+
*
67+
* __arch_* Specify on which architecture the test case should be tested.
68+
* Several __arch_* annotations could be specified at once.
69+
* When test case is not run on current arch it is marked as skipped.
6670
*/
6771
#define __msg(msg) __attribute__((btf_decl_tag("comment:test_expect_msg=" msg)))
6872
#define __regex(regex) __attribute__((btf_decl_tag("comment:test_expect_regex=" regex)))
@@ -82,6 +86,10 @@
8286
#define __auxiliary __attribute__((btf_decl_tag("comment:test_auxiliary")))
8387
#define __auxiliary_unpriv __attribute__((btf_decl_tag("comment:test_auxiliary_unpriv")))
8488
#define __btf_path(path) __attribute__((btf_decl_tag("comment:test_btf_path=" path)))
89+
#define __arch(arch) __attribute__((btf_decl_tag("comment:test_arch=" arch)))
90+
#define __arch_x86_64 __arch("X86_64")
91+
#define __arch_arm64 __arch("ARM64")
92+
#define __arch_riscv64 __arch("RISCV64")
8593

8694
/* Convenience macro for use with 'asm volatile' blocks */
8795
#define __naked __attribute__((naked))

tools/testing/selftests/bpf/test_loader.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#define TEST_TAG_AUXILIARY "comment:test_auxiliary"
3535
#define TEST_TAG_AUXILIARY_UNPRIV "comment:test_auxiliary_unpriv"
3636
#define TEST_BTF_PATH "comment:test_btf_path="
37+
#define TEST_TAG_ARCH "comment:test_arch="
3738

3839
/* Warning: duplicated in bpf_misc.h */
3940
#define POINTER_VALUE 0xcafe4all
@@ -80,6 +81,7 @@ struct test_spec {
8081
int log_level;
8182
int prog_flags;
8283
int mode_mask;
84+
int arch_mask;
8385
bool auxiliary;
8486
bool valid;
8587
};
@@ -213,6 +215,12 @@ static void update_flags(int *flags, int flag, bool clear)
213215
*flags |= flag;
214216
}
215217

218+
enum arch {
219+
ARCH_X86_64 = 0x1,
220+
ARCH_ARM64 = 0x2,
221+
ARCH_RISCV64 = 0x4,
222+
};
223+
216224
/* Uses btf_decl_tag attributes to describe the expected test
217225
* behavior, see bpf_misc.h for detailed description of each attribute
218226
* and attribute combinations.
@@ -226,6 +234,7 @@ static int parse_test_spec(struct test_loader *tester,
226234
bool has_unpriv_result = false;
227235
bool has_unpriv_retval = false;
228236
int func_id, i, err = 0;
237+
u32 arch_mask = 0;
229238
struct btf *btf;
230239

231240
memset(spec, 0, sizeof(*spec));
@@ -364,11 +373,26 @@ static int parse_test_spec(struct test_loader *tester,
364373
goto cleanup;
365374
update_flags(&spec->prog_flags, flags, clear);
366375
}
376+
} else if (str_has_pfx(s, TEST_TAG_ARCH)) {
377+
val = s + sizeof(TEST_TAG_ARCH) - 1;
378+
if (strcmp(val, "X86_64") == 0) {
379+
arch_mask |= ARCH_X86_64;
380+
} else if (strcmp(val, "ARM64") == 0) {
381+
arch_mask |= ARCH_ARM64;
382+
} else if (strcmp(val, "RISCV64") == 0) {
383+
arch_mask |= ARCH_RISCV64;
384+
} else {
385+
PRINT_FAIL("bad arch spec: '%s'", val);
386+
err = -EINVAL;
387+
goto cleanup;
388+
}
367389
} else if (str_has_pfx(s, TEST_BTF_PATH)) {
368390
spec->btf_custom_path = s + sizeof(TEST_BTF_PATH) - 1;
369391
}
370392
}
371393

394+
spec->arch_mask = arch_mask;
395+
372396
if (spec->mode_mask == 0)
373397
spec->mode_mask = PRIV;
374398

@@ -677,6 +701,20 @@ static int get_xlated_program_text(int prog_fd, char *text, size_t text_sz)
677701
return err;
678702
}
679703

704+
static bool run_on_current_arch(int arch_mask)
705+
{
706+
if (arch_mask == 0)
707+
return true;
708+
#if defined(__x86_64__)
709+
return arch_mask & ARCH_X86_64;
710+
#elif defined(__aarch64__)
711+
return arch_mask & ARCH_ARM64;
712+
#elif defined(__riscv) && __riscv_xlen == 64
713+
return arch_mask & ARCH_RISCV64;
714+
#endif
715+
return false;
716+
}
717+
680718
/* this function is forced noinline and has short generic name to look better
681719
* in test_progs output (in case of a failure)
682720
*/
@@ -701,6 +739,11 @@ void run_subtest(struct test_loader *tester,
701739
if (!test__start_subtest(subspec->name))
702740
return;
703741

742+
if (!run_on_current_arch(spec->arch_mask)) {
743+
test__skip();
744+
return;
745+
}
746+
704747
if (unpriv) {
705748
if (!can_execute_unpriv(tester, spec)) {
706749
test__skip();

0 commit comments

Comments
 (0)