Skip to contentSkip to conversation

perf test: Skip sigtrap test on old kernels

PATCH Superseded Namhyung Kim 2 participants 3 messages Sep 7, 2022 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v3 1/1 perf test: Skip sigtrap test on old kernels

If it runs on an old kernel, perf_event_open would fail because of the new fields sigtrap and sig_data. Just skipping the test could miss an actual bug in the kernel.

Let's check BTF if it has the perf_event_attr.sigtrap field.

---
* move #include under #ifdef
* return true when BPF is not supported
* update comment

 tools/perf/tests/sigtrap.c | 50 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +49 -1
index e32ece90e164..cdf75eab6a8a 100644
@@ -54,6 +54,48 @@ static struct perf_event_attr make_event_attr(void)
 	return attr;
 }
 
+#ifdef HAVE_BPF_SKEL
+#include <bpf/btf.h>
+
+static bool attr_has_sigtrap(void)
+{
+	bool ret = false;
+	struct btf *btf;
+	const struct btf_type *t;
+	const struct btf_member *m;
+	const char *name;
+	int i, id;
+
+	btf = btf__load_vmlinux_btf();
+	if (btf == NULL) {
+		/* should be an old kernel */
+		return false;
+	}
+
+	id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT);
+	if (id < 0)
+		goto out;
+
+	t = btf__type_by_id(btf, id);
+	for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
+		name = btf__name_by_offset(btf, m->name_off);
+		if (!strcmp(name, "sigtrap")) {
+			ret = true;
+			break;
+		}
+	}
+out:
+	btf__free(btf);
+	return ret;
+}
+#else  /* !HAVE_BPF_SKEL */
+static bool attr_has_sigtrap(void)
+{
+	/* to maintain current behavior */
+	return true;
+}
+#endif  /* HAVE_BPF_SKEL */
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +181,13 @@ static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __m
 
 	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
 	if (fd < 0) {
-		pr_debug("FAILED sys_perf_event_open(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
+		if (attr_has_sigtrap()) {
+			pr_debug("FAILED sys_perf_event_open(): %s\n",
+				 str_error_r(errno, sbuf, sizeof(sbuf)));
+		} else {
+			pr_debug("perf_event_attr doesn't have sigtrap\n");
+			ret = TEST_SKIP;
+		}
 		goto out_restore_sigaction;
 	}
 
Signature · 1 line
2.37.2.789.g6183377224-goog
reviewer
#1 lore raw
Quoted 54 lines · Namhyung Kim

If it runs on an old kernel, perf_event_open would fail because of the new fields sigtrap and sig_data. Just skipping the test could miss an actual bug in the kernel.

Let's check BTF if it has the perf_event_attr.sigtrap field.

---
* move #include under #ifdef
* return true when BPF is not supported
* update comment

 tools/perf/tests/sigtrap.c | 50 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +28 -0
index e32ece90e164..cdf75eab6a8a 100644
@@ -54,6 +54,48 @@ static struct perf_event_attr make_event_attr(void)
        return attr;
 }
 
+#ifdef HAVE_BPF_SKEL
+#include <bpf/btf.h>
+
+static bool attr_has_sigtrap(void)
+{
+       bool ret = false;
+       struct btf *btf;
+       const struct btf_type *t;
+       const struct btf_member *m;
+       const char *name;
+       int i, id;
+
+       btf = btf__load_vmlinux_btf();
+       if (btf == NULL) {
+               /* should be an old kernel */
+               return false;
+       }
+
+       id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT);
+       if (id < 0)
+               goto out;
+
+       t = btf__type_by_id(btf, id);
+       for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
+               name = btf__name_by_offset(btf, m->name_off);
+               if (!strcmp(name, "sigtrap")) {
+                       ret = true;
+                       break;

tools/perf/tests/sigtrap.c static struct perf_event_attr make_event_attr(void) · line 94

Quoted 10 lines
tools/perf/tests/sigtrap.c +10 -0
+               }
+       }
+out:
+       btf__free(btf);
+       return ret;
+}
+#else  /* !HAVE_BPF_SKEL */
+static bool attr_has_sigtrap(void)
+{
+       /* to maintain current behavior */

nit: I don't think this comment will age well and the behavior of the function is a bit counterintuitive. Perhaps:

/* If we don't have libbpf then guess we're on a newer kernel with sigtrap support. This will mean the test will fail on older kernels. */

Quoted 25 lines
tools/perf/tests/sigtrap.c +11 -2
+       return true;
+}
+#endif  /* HAVE_BPF_SKEL */
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +181,13 @@ static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __m
 
        fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
        if (fd < 0) {
-               pr_debug("FAILED sys_perf_event_open(): %s\n", str_error_r(errno, sbuf, sizeof(sbuf)));
+               if (attr_has_sigtrap()) {
+                       pr_debug("FAILED sys_perf_event_open(): %s\n",
+                                str_error_r(errno, sbuf, sizeof(sbuf)));
+               } else {
+                       pr_debug("perf_event_attr doesn't have sigtrap\n");
+                       ret = TEST_SKIP;
+               }
                goto out_restore_sigaction;
        }
 
--

2.37.2.789.g6183377224-goog

author maintainer
#2 lore raw

Hi Ian,

Quoted 70 lines · Ian Rogers
Quoted 54 lines · Namhyung Kim

If it runs on an old kernel, perf_event_open would fail because of the new fields sigtrap and sig_data. Just skipping the test could miss an actual bug in the kernel.

Let's check BTF if it has the perf_event_attr.sigtrap field.

---
* move #include under #ifdef
* return true when BPF is not supported
* update comment

 tools/perf/tests/sigtrap.c | 50 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +28 -0
index e32ece90e164..cdf75eab6a8a 100644
@@ -54,6 +54,48 @@ static struct perf_event_attr make_event_attr(void)
        return attr;
 }
 
+#ifdef HAVE_BPF_SKEL
+#include <bpf/btf.h>
+
+static bool attr_has_sigtrap(void)
+{
+       bool ret = false;
+       struct btf *btf;
+       const struct btf_type *t;
+       const struct btf_member *m;
+       const char *name;
+       int i, id;
+
+       btf = btf__load_vmlinux_btf();
+       if (btf == NULL) {
+               /* should be an old kernel */
+               return false;
+       }
+
+       id = btf__find_by_name_kind(btf, "perf_event_attr", BTF_KIND_STRUCT);
+       if (id < 0)
+               goto out;
+
+       t = btf__type_by_id(btf, id);
+       for (i = 0, m = btf_members(t); i < btf_vlen(t); i++, m++) {
+               name = btf__name_by_offset(btf, m->name_off);
+               if (!strcmp(name, "sigtrap")) {
+                       ret = true;
+                       break;

tools/perf/tests/sigtrap.c static struct perf_event_attr make_event_attr(void) · line 94

Quoted 10 lines
tools/perf/tests/sigtrap.c +10 -0
+               }
+       }
+out:
+       btf__free(btf);
+       return ret;
+}
+#else  /* !HAVE_BPF_SKEL */
+static bool attr_has_sigtrap(void)
+{
+       /* to maintain current behavior */

nit: I don't think this comment will age well and the behavior of the function is a bit counterintuitive. Perhaps:

Quoted 3 lines

/* If we don't have libbpf then guess we're on a newer kernel with sigtrap support. This will mean the test will fail on older kernels. */

Thanks for the clarification, will update!

Namhyung