diff mbox series

[bpf-next] selftests/bpf: skip btf_tag test if btf_tag attribute not supported

Message ID 20210915061036.2577971-1-yhs@fb.com (mailing list archive)
State Accepted
Delegated to: BPF
Headers show
Series [bpf-next] selftests/bpf: skip btf_tag test if btf_tag attribute not supported | expand

Checks

Context Check Description
bpf/vmtest-bpf-next success VM_Test
bpf/vmtest-bpf-next-PR success PR summary
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 10 maintainers not CCed: nathan@kernel.org kpsingh@kernel.org john.fastabend@gmail.com clang-built-linux@googlegroups.com ndesaulniers@google.com linux-kselftest@vger.kernel.org shuah@kernel.org songliubraving@fb.com netdev@vger.kernel.org kafai@fb.com
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 2 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch warning WARNING: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst
netdev/build_allmodconfig_warn success Errors and warnings before: 2 this patch: 0
netdev/header_inline success Link

Commit Message

Yonghong Song Sept. 15, 2021, 6:10 a.m. UTC
Commit c240ba287890 ("selftests/bpf: Add a test with a bpf
program with btf_tag attributes") added btf_tag selftest
to test BTF_KIND_TAG generation from C source code, and to
test kernel validation of generated BTF types.
But if an old clang (clang 13 or earlier) is used, the
following compiler warning may be seen:
  progs/tag.c:23:20: warning: unknown attribute 'btf_tag' ignored
and the test itself is marked OK. The compiler warning is bad
and the test itself shouldn't be marked OK.

This patch added the check for btf_tag attribute support.
If btf_tag is not supported by the clang, the attribute will
not be used in the code and the test will be marked as skipped.
For example, with clang 13:
  ./test_progs -t btf_tag
  #21 btf_tag:SKIP
  Summary: 1/0 PASSED, 1 SKIPPED, 0 FAILED

The selftests/README.rst is updated to clarify when the btf_tag
test may be skipped.

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/README.rst           | 14 ++++++++++++++
 tools/testing/selftests/bpf/prog_tests/btf_tag.c |  6 ++++++
 tools/testing/selftests/bpf/progs/tag.c          | 12 +++++++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)

Comments

Alexei Starovoitov Sept. 15, 2021, 4:02 p.m. UTC | #1
On Tue, Sep 14, 2021 at 11:10 PM Yonghong Song <yhs@fb.com> wrote:
>
> Commit c240ba287890 ("selftests/bpf: Add a test with a bpf
> program with btf_tag attributes") added btf_tag selftest
> to test BTF_KIND_TAG generation from C source code, and to
> test kernel validation of generated BTF types.
> But if an old clang (clang 13 or earlier) is used, the
> following compiler warning may be seen:
>   progs/tag.c:23:20: warning: unknown attribute 'btf_tag' ignored
> and the test itself is marked OK. The compiler warning is bad
> and the test itself shouldn't be marked OK.
>
> This patch added the check for btf_tag attribute support.
> If btf_tag is not supported by the clang, the attribute will
> not be used in the code and the test will be marked as skipped.
> For example, with clang 13:
>   ./test_progs -t btf_tag
>   #21 btf_tag:SKIP
>   Summary: 1/0 PASSED, 1 SKIPPED, 0 FAILED
>
> The selftests/README.rst is updated to clarify when the btf_tag
> test may be skipped.
>
> Signed-off-by: Yonghong Song <yhs@fb.com>

Applied. Thanks for the quick follow up.
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/README.rst b/tools/testing/selftests/bpf/README.rst
index 9b17f2867488..8200c0da2769 100644
--- a/tools/testing/selftests/bpf/README.rst
+++ b/tools/testing/selftests/bpf/README.rst
@@ -201,6 +201,20 @@  Without it, the error from compiling bpf selftests looks like:
 
 __ https://reviews.llvm.org/D93563
 
+btf_tag test and Clang version
+==============================
+
+The btf_tag selftest require LLVM support to recognize the btf_tag attribute.
+It was introduced in `Clang 14`__.
+
+Without it, the btf_tag selftest will be skipped and you will observe:
+
+.. code-block:: console
+
+  #<test_num> btf_tag:SKIP
+
+__ https://reviews.llvm.org/D106614
+
 Clang dependencies for static linking tests
 ===========================================
 
diff --git a/tools/testing/selftests/bpf/prog_tests/btf_tag.c b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
index f939527ede77..91821f42714d 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_tag.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_tag.c
@@ -10,5 +10,11 @@  void test_btf_tag(void)
 	skel = tag__open_and_load();
 	if (!ASSERT_OK_PTR(skel, "btf_tag"))
 		return;
+
+	if (skel->rodata->skip_tests) {
+		printf("%s:SKIP: btf_tag attribute not supported", __func__);
+		test__skip();
+	}
+
 	tag__destroy(skel);
 }
diff --git a/tools/testing/selftests/bpf/progs/tag.c b/tools/testing/selftests/bpf/progs/tag.c
index 17f88c58a6c5..b46b1bfac7da 100644
--- a/tools/testing/selftests/bpf/progs/tag.c
+++ b/tools/testing/selftests/bpf/progs/tag.c
@@ -4,8 +4,19 @@ 
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_tracing.h>
 
+#ifndef __has_attribute
+#define __has_attribute(x) 0
+#endif
+
+#if __has_attribute(btf_tag)
 #define __tag1 __attribute__((btf_tag("tag1")))
 #define __tag2 __attribute__((btf_tag("tag2")))
+volatile const bool skip_tests __tag1 __tag2 = false;
+#else
+#define __tag1
+#define __tag2
+volatile const bool skip_tests = true;
+#endif
 
 struct key_t {
 	int a;
@@ -20,7 +31,6 @@  struct {
 	__type(value, __u64);
 } hashmap1 SEC(".maps");
 
-__u32 total __tag1 __tag2 = 0;
 
 static __noinline int foo(int x __tag1 __tag2) __tag1 __tag2
 {