linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] perf test: Skip sigtrap test on old kernels
@ 2022-09-07  7:12 Namhyung Kim
  2022-09-07 15:47 ` Ian Rogers
  0 siblings, 1 reply; 5+ messages in thread
From: Namhyung Kim @ 2022-09-07  7:12 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, LKML, Ian Rogers, linux-perf-users,
	Marco Elver, Song Liu

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.

Cc: Marco Elver <elver@google.com>
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
* 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(-)

diff --git a/tools/perf/tests/sigtrap.c b/tools/perf/tests/sigtrap.c
index e32ece90e164..cdf75eab6a8a 100644
--- a/tools/perf/tests/sigtrap.c
+++ b/tools/perf/tests/sigtrap.c
@@ -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;
 	}
 
-- 
2.37.2.789.g6183377224-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] perf test: Skip sigtrap test on old kernels
  2022-09-07  7:12 [PATCH v3] perf test: Skip sigtrap test on old kernels Namhyung Kim
@ 2022-09-07 15:47 ` Ian Rogers
  2022-09-07 17:51   ` Namhyung Kim
  0 siblings, 1 reply; 5+ messages in thread
From: Ian Rogers @ 2022-09-07 15:47 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Jiri Olsa, Ingo Molnar, Peter Zijlstra,
	LKML, linux-perf-users, Marco Elver, Song Liu

On Wed, Sep 7, 2022 at 12:12 AM Namhyung Kim <namhyung@kernel.org> wrote:
>
> 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.
>
> Cc: Marco Elver <elver@google.com>
> Acked-by: Song Liu <song@kernel.org>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
> * 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(-)
>
> diff --git a/tools/perf/tests/sigtrap.c b/tools/perf/tests/sigtrap.c
> index e32ece90e164..cdf75eab6a8a 100644
> --- a/tools/perf/tests/sigtrap.c
> +++ b/tools/perf/tests/sigtrap.c
> @@ -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 */

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.
*/

> +       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
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] perf test: Skip sigtrap test on old kernels
  2022-09-07 15:47 ` Ian Rogers
@ 2022-09-07 17:51   ` Namhyung Kim
  0 siblings, 0 replies; 5+ messages in thread
From: Namhyung Kim @ 2022-09-07 17:51 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Arnaldo Carvalho de Melo, Jiri Olsa, Ingo Molnar, Peter Zijlstra,
	LKML, linux-perf-users, Marco Elver, Song Liu

Hi Ian,

On Wed, Sep 7, 2022 at 8:48 AM Ian Rogers <irogers@google.com> wrote:
>
> On Wed, Sep 7, 2022 at 12:12 AM Namhyung Kim <namhyung@kernel.org> wrote:
> >
> > 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.
> >
> > Cc: Marco Elver <elver@google.com>
> > Acked-by: Song Liu <song@kernel.org>
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> > ---
> > * 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(-)
> >
> > diff --git a/tools/perf/tests/sigtrap.c b/tools/perf/tests/sigtrap.c
> > index e32ece90e164..cdf75eab6a8a 100644
> > --- a/tools/perf/tests/sigtrap.c
> > +++ b/tools/perf/tests/sigtrap.c
> > @@ -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 */
>
> 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.
> */

Thanks for the clarification, will update!

Namhyung

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v3] perf test: Skip sigtrap test on old kernels
  2022-09-08 23:01 Namhyung Kim
@ 2022-09-09 13:23 ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 5+ messages in thread
From: Arnaldo Carvalho de Melo @ 2022-09-09 13:23 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Jiri Olsa, Ingo Molnar, Peter Zijlstra, LKML, Ian Rogers,
	Adrian Hunter, linux-perf-users, Marco Elver, Song Liu

Em Thu, Sep 08, 2022 at 04:01:50PM -0700, Namhyung Kim escreveu:
> 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 (when we have libbpf) if it has the sigtrap field in the
> perf_event_attr.  Otherwise, we can check it with a minimal event config.

Thanks, applied.

- Arnaldo

 
> Cc: Marco Elver <elver@google.com>
> Acked-by: Song Liu <song@kernel.org>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> ---
>  tools/perf/tests/sigtrap.c | 65 +++++++++++++++++++++++++++++++++++++-
>  1 file changed, 64 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/tests/sigtrap.c b/tools/perf/tests/sigtrap.c
> index e32ece90e164..1de7478ec189 100644
> --- a/tools/perf/tests/sigtrap.c
> +++ b/tools/perf/tests/sigtrap.c
> @@ -54,6 +54,63 @@ 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)
> +{
> +	struct perf_event_attr attr = {
> +		.type		= PERF_TYPE_SOFTWARE,
> +		.config		= PERF_COUNT_SW_DUMMY,
> +		.size		= sizeof(attr),
> +		.remove_on_exec = 1, /* Required by sigtrap. */
> +		.sigtrap	= 1, /* Request synchronous SIGTRAP on event. */
> +	};
> +	int fd;
> +	bool ret = false;
> +
> +	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
> +	if (fd >= 0) {
> +		ret = true;
> +		close(fd);
> +	}
> +
> +	return ret;
> +}
> +#endif  /* HAVE_BPF_SKEL */
> +
>  static void
>  sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
>  {
> @@ -139,7 +196,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

-- 

- Arnaldo

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v3] perf test: Skip sigtrap test on old kernels
@ 2022-09-08 23:01 Namhyung Kim
  2022-09-09 13:23 ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 5+ messages in thread
From: Namhyung Kim @ 2022-09-08 23:01 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Jiri Olsa
  Cc: Ingo Molnar, Peter Zijlstra, LKML, Ian Rogers, Adrian Hunter,
	linux-perf-users, Marco Elver, Song Liu

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 (when we have libbpf) if it has the sigtrap field in the
perf_event_attr.  Otherwise, we can check it with a minimal event config.

Cc: Marco Elver <elver@google.com>
Acked-by: Song Liu <song@kernel.org>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/sigtrap.c | 65 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 64 insertions(+), 1 deletion(-)

diff --git a/tools/perf/tests/sigtrap.c b/tools/perf/tests/sigtrap.c
index e32ece90e164..1de7478ec189 100644
--- a/tools/perf/tests/sigtrap.c
+++ b/tools/perf/tests/sigtrap.c
@@ -54,6 +54,63 @@ 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)
+{
+	struct perf_event_attr attr = {
+		.type		= PERF_TYPE_SOFTWARE,
+		.config		= PERF_COUNT_SW_DUMMY,
+		.size		= sizeof(attr),
+		.remove_on_exec = 1, /* Required by sigtrap. */
+		.sigtrap	= 1, /* Request synchronous SIGTRAP on event. */
+	};
+	int fd;
+	bool ret = false;
+
+	fd = sys_perf_event_open(&attr, 0, -1, -1, perf_event_open_cloexec_flag());
+	if (fd >= 0) {
+		ret = true;
+		close(fd);
+	}
+
+	return ret;
+}
+#endif  /* HAVE_BPF_SKEL */
+
 static void
 sigtrap_handler(int signum __maybe_unused, siginfo_t *info, void *ucontext __maybe_unused)
 {
@@ -139,7 +196,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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-09 13:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-07  7:12 [PATCH v3] perf test: Skip sigtrap test on old kernels Namhyung Kim
2022-09-07 15:47 ` Ian Rogers
2022-09-07 17:51   ` Namhyung Kim
2022-09-08 23:01 Namhyung Kim
2022-09-09 13:23 ` Arnaldo Carvalho de Melo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).