Skip to contentSkip to conversation

perf test: Skip sigtrap test on old kernels

PATCH Superseded Namhyung Kim 4 participants 7 messages Sep 7, 2022 – Sep 8, 2022

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH v2 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.

---
 tools/perf/tests/sigtrap.c | 46 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +45 -1
index e32ece90e164..32f08ce0f2b0 100644
@@ -16,6 +16,8 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 
+#include <bpf/btf.h>
+
 #include "cloexec.h"
 #include "debug.h"
 #include "event.h"
@@ -54,6 +56,42 @@ static struct perf_event_attr make_event_attr(void)
 	return attr;
 }
 
+static bool attr_has_sigtrap(void)
+{
+	bool ret = false;
+
+#ifdef HAVE_BPF_SKEL
+
+	struct btf *btf;
+	const struct btf_type *t;
+	const struct btf_member *m;
+	const char *name;
+	int i, id;
+
+	/* just assume it doesn't have the field */
+	btf = btf__load_vmlinux_btf();
+	if (btf == NULL)
+		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);
+#endif
+
+	return ret;
+}
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +177,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 Acked-by on v2 1/1
#1 lore raw
Quoted 19 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.

---
 tools/perf/tests/sigtrap.c | 46 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +0 -0
index e32ece90e164..32f08ce0f2b0 100644

tools/perf/tests/sigtrap.c line 20

Quoted 5 lines
tools/perf/tests/sigtrap.c +2 -0
@@ -16,6 +16,8 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 
+#include <bpf/btf.h>
+

Do we need "#ifdef HAVE_BPF_SKEL" for the include part?

Other than this, looks good to me.

Quoted 64 lines
tools/perf/tests/sigtrap.c +43 -2
 #include "cloexec.h"
 #include "debug.h"
 #include "event.h"
@@ -54,6 +56,42 @@ static struct perf_event_attr make_event_attr(void)
        return attr;
 }
 
+static bool attr_has_sigtrap(void)
+{
+       bool ret = false;
+
+#ifdef HAVE_BPF_SKEL
+
+       struct btf *btf;
+       const struct btf_type *t;
+       const struct btf_member *m;
+       const char *name;
+       int i, id;
+
+       /* just assume it doesn't have the field */
+       btf = btf__load_vmlinux_btf();
+       if (btf == NULL)
+               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);
+#endif
+
+       return ret;
+}
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +177,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 Song,

Quoted 24 lines · Song Liu
Quoted 22 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.

---
 tools/perf/tests/sigtrap.c | 46 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +0 -0
index e32ece90e164..32f08ce0f2b0 100644
@@ -16,6 +16,8 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 
Quoted 4 lines

tools/perf/tests/sigtrap.c line 20

Quoted 2 lines
tools/perf/tests/sigtrap.c +2 -0
+#include <bpf/btf.h>
+

Do we need "#ifdef HAVE_BPF_SKEL" for the include part?

Right, it'd be better to move it under the #ifdef. Will change.

Quoted 4 lines

Other than this, looks good to me.

Thanks for the review!
Namhyung

#3 lore raw
In reply to Namhyung Kim
Quoted 52 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.

---
 tools/perf/tests/sigtrap.c | 46 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +24 -0
index e32ece90e164..32f08ce0f2b0 100644
@@ -16,6 +16,8 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 
+#include <bpf/btf.h>
+
 #include "cloexec.h"
 #include "debug.h"
 #include "event.h"
@@ -54,6 +56,42 @@ static struct perf_event_attr make_event_attr(void)
 	return attr;
 }
 
+static bool attr_has_sigtrap(void)
+{
+	bool ret = false;
+
+#ifdef HAVE_BPF_SKEL
+
+	struct btf *btf;
+	const struct btf_type *t;
+	const struct btf_member *m;
+	const char *name;
+	int i, id;
+
+	/* just assume it doesn't have the field */
+	btf = btf__load_vmlinux_btf();
+	if (btf == NULL)
+		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);

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

Quoted 10 lines
tools/perf/tests/sigtrap.c +10 -0
+	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);
+#endif

would it be easier to call perf_event_open for simple event with sigtrap set (and perhaps remove_on_exec) ? perf_copy_attr checks on reserved fields

jirka

Quoted 25 lines
tools/perf/tests/sigtrap.c +11 -1
+
+	return ret;
+}
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +177,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
author maintainer
#4 lore raw
Quoted 60 lines · Jiri Olsa
Quoted 58 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.

---
 tools/perf/tests/sigtrap.c | 46 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +30 -0
index e32ece90e164..32f08ce0f2b0 100644
@@ -16,6 +16,8 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 
+#include <bpf/btf.h>
+
 #include "cloexec.h"
 #include "debug.h"
 #include "event.h"
@@ -54,6 +56,42 @@ static struct perf_event_attr make_event_attr(void)
      return attr;
 }
 
+static bool attr_has_sigtrap(void)
+{
+     bool ret = false;
+
+#ifdef HAVE_BPF_SKEL
+
+     struct btf *btf;
+     const struct btf_type *t;
+     const struct btf_member *m;
+     const char *name;
+     int i, id;
+
+     /* just assume it doesn't have the field */
+     btf = btf__load_vmlinux_btf();
+     if (btf == NULL)
+             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;
+             }
Quoted 8 lines

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

Quoted 4 lines
tools/perf/tests/sigtrap.c +4 -0
+     }
+out:
+     btf__free(btf);
+#endif

would it be easier to call perf_event_open for simple event with sigtrap set (and perhaps remove_on_exec) ? perf_copy_attr checks on reserved fields

Hmm.. right. we could do that too. But it might still fail if there's a bug in the path handling in sigtrap even for the simple case. I'm not sure if it's a realistic concern though. :)

Thanks,
Namhyung

Quoted 27 lines
Quoted 27 lines
tools/perf/tests/sigtrap.c +11 -2
+
+     return ret;
+}
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +177,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
#5 lore raw
Quoted 71 lines · Namhyung Kim
Quoted 60 lines · Jiri Olsa
Quoted 58 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.

---
 tools/perf/tests/sigtrap.c | 46 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +30 -0
index e32ece90e164..32f08ce0f2b0 100644
@@ -16,6 +16,8 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 
+#include <bpf/btf.h>
+
 #include "cloexec.h"
 #include "debug.h"
 #include "event.h"
@@ -54,6 +56,42 @@ static struct perf_event_attr make_event_attr(void)
      return attr;
 }
 
+static bool attr_has_sigtrap(void)
+{
+     bool ret = false;
+
+#ifdef HAVE_BPF_SKEL
+
+     struct btf *btf;
+     const struct btf_type *t;
+     const struct btf_member *m;
+     const char *name;
+     int i, id;
+
+     /* just assume it doesn't have the field */
+     btf = btf__load_vmlinux_btf();
+     if (btf == NULL)
+             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;
+             }
Quoted 8 lines

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

Quoted 4 lines
tools/perf/tests/sigtrap.c +4 -0
+     }
+out:
+     btf__free(btf);
+#endif

would it be easier to call perf_event_open for simple event with sigtrap set (and perhaps remove_on_exec) ? perf_copy_attr checks on reserved fields

Quoted 3 lines

Hmm.. right. we could do that too. But it might still fail if there's a bug in the path handling in sigtrap even for the simple case. I'm not sure if it's a realistic concern though. :)

I think we can do that when libbpf is not available.

Thanks,
Namhyung

#6 lore raw
In reply to Namhyung Kim

Em Tue, Sep 06, 2022 at 10:04:07PM -0700, Namhyung Kim escreveu:

Quoted 34 lines

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.

---
 tools/perf/tests/sigtrap.c | 46 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
tools/perf/tests/sigtrap.c +6 -0
index e32ece90e164..32f08ce0f2b0 100644
@@ -16,6 +16,8 @@
 #include <sys/syscall.h>
 #include <unistd.h>
 
+#include <bpf/btf.h>
+
 #include "cloexec.h"
 #include "debug.h"
 #include "event.h"
@@ -54,6 +56,42 @@ static struct perf_event_attr make_event_attr(void)
 	return attr;
 }
 
+static bool attr_has_sigtrap(void)
+{
+	bool ret = false;
+

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

Quoted 10 lines
tools/perf/tests/sigtrap.c +10 -0
+#ifdef HAVE_BPF_SKEL
+
+	struct btf *btf;
+	const struct btf_type *t;
+	const struct btf_member *m;
+	const char *name;
+	int i, id;
+
+	/* just assume it doesn't have the field */
+	btf = btf__load_vmlinux_btf();

Cool, at some point we'll probably move this to some other global place, doing the lazy loading and keeping it in place as probably we'll use it more often :-)

Waiting for v2.

Thanks for working on this.

- Arnaldo

Quoted 42 lines
tools/perf/tests/sigtrap.c +29 -1
+	if (btf == NULL)
+		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);
+#endif
+
+	return ret;
+}
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +177,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
Signature · 1 line
- Arnaldo