All of lore.kernel.org
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Ian Rogers <irogers@google.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Jiri Olsa <jolsa@kernel.org>, Andrii Nakryiko <andrii@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Hao Luo <haoluo@google.com>, bpf <bpf@vger.kernel.org>,
	Ingo Molnar <mingo@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Andi Kleen <ak@linux.intel.com>, Song Liu <songliubraving@fb.com>,
	Milian Wolff <milian.wolff@kdab.com>,
	linux-perf-users <linux-perf-users@vger.kernel.org>,
	Blake Jones <blakejones@google.com>
Subject: Re: [PATCH 2/6] perf record: Enable off-cpu analysis with BPF
Date: Thu, 2 Jun 2022 15:36:03 -0700	[thread overview]
Message-ID: <Ypk7U1Ce4Muq3l5U@google.com> (raw)
In-Reply-To: <CAM9d7cjT2o3xVUQf402shzirD4K2XoyomN+AL_R2WENKg6pwoQ@mail.gmail.com>

On Tue, May 31, 2022 at 11:01:14PM -0700, Namhyung Kim wrote:
> On Tue, May 31, 2022 at 5:00 PM Ian Rogers <irogers@google.com> wrote:
> >
> > On Wed, May 18, 2022 at 3:47 PM Namhyung Kim <namhyung@kernel.org> wrote:
> [SNIP]
> > > +
> > > +/*
> > > + * Old kernel used to call it task_struct->state and now it's '__state'.
> > > + * Use BPF CO-RE "ignored suffix rule" to deal with it like below:
> > > + *
> > > + * https://nakryiko.com/posts/bpf-core-reference-guide/#handling-incompatible-field-and-type-changes
> > > + */
> > > +static inline int get_task_state(struct task_struct *t)
> > > +{
> > > +       if (bpf_core_field_exists(t->__state))
> > > +               return BPF_CORE_READ(t, __state);
> > > +
> >
> > When building against a pre-5.14 kernel I'm running into a build issue here:
> >
> > tools/perf/util/bpf_skel/off_cpu.bpf.c:96:31: error: no member named '__
> > state' in 'struct task_struct'; did you mean 'state'?
> >        if (bpf_core_field_exists(t->__state))
> >                                     ^~~~~~~
> >                                     state
> >
> > This isn't covered by Andrii's BPF CO-RE reference guide. I have an
> > #iffy workaround below,but this will be brittle if the 5.14+ kernel
> > code is backported. Suggestions welcomed :-)
> 
> Thanks for the fix.  I think we should not guess the field name
> in the current task struct and check both versions separately.
> I'm afraid the version check won't work with some backported
> kernels.  But do we care?


What about this instead?

----8<----
From a621f836f00e11942e5d39a735ec8f7a21962d6a Mon Sep 17 00:00:00 2001
From: Namhyung Kim <namhyung@kernel.org>
Date: Tue, 31 May 2022 14:25:05 -0700
Subject: [PATCH] perf tools: Fix a build failure in off-cpu BPF program on old kernels

Old kernels have task_struct which contains "state" field.  While the
get_task_state() in the BPF code handles that, it assumed the kernel
has the new definition and caused a build error on old kernels.

We should not assume anything and access them carefully.

Reported-by: Ian Rogers <irogers@google.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/bpf_skel/off_cpu.bpf.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/bpf_skel/off_cpu.bpf.c b/tools/perf/util/bpf_skel/off_cpu.bpf.c
index 792ae2847080..cc6d7fd55118 100644
--- a/tools/perf/util/bpf_skel/off_cpu.bpf.c
+++ b/tools/perf/util/bpf_skel/off_cpu.bpf.c
@@ -71,6 +71,11 @@ struct {
 	__uint(max_entries, 1);
 } cgroup_filter SEC(".maps");
 
+/* new kernel task_struct definition */
+struct task_struct___new {
+	long __state;
+} __attribute__((preserve_access_index));
+
 /* old kernel task_struct definition */
 struct task_struct___old {
 	long state;
@@ -93,14 +98,17 @@ const volatile bool uses_cgroup_v1 = false;
  */
 static inline int get_task_state(struct task_struct *t)
 {
-	if (bpf_core_field_exists(t->__state))
-		return BPF_CORE_READ(t, __state);
+	/* recast pointer to capture new type for compiler */
+	struct task_struct___new *t_new = (void *)t;
 
-	/* recast pointer to capture task_struct___old type for compiler */
-	struct task_struct___old *t_old = (void *)t;
+	if (bpf_core_field_exists(t_new->__state)) {
+		return BPF_CORE_READ(t_new, __state);
+	} else {
+		/* recast pointer to capture old type for compiler */
+		struct task_struct___old *t_old = (void *)t;
 
-	/* now use old "state" name of the field */
-	return BPF_CORE_READ(t_old, state);
+		return BPF_CORE_READ(t_old, state);
+	}
 }
 
 static inline __u64 get_cgroup_id(struct task_struct *t)
-- 
2.36.1.255.ge46751e96f-goog


  reply	other threads:[~2022-06-02 22:36 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-18 22:47 [RFC 0/6] perf record: Implement off-cpu profiling with BPF (v3) Namhyung Kim
2022-05-18 22:47 ` [PATCH 1/6] perf report: Do not extend sample type of bpf-output event Namhyung Kim
2022-05-18 22:47 ` [PATCH 2/6] perf record: Enable off-cpu analysis with BPF Namhyung Kim
2022-05-19  3:58   ` Ian Rogers
2022-05-19 21:01     ` Namhyung Kim
2022-06-01  0:00   ` Ian Rogers
2022-06-01  6:01     ` Namhyung Kim
2022-06-02 22:36       ` Namhyung Kim [this message]
2022-05-18 22:47 ` [PATCH 3/6] perf record: Implement basic filtering for off-cpu Namhyung Kim
2022-05-19  4:02   ` Ian Rogers
2022-05-19 21:02     ` Namhyung Kim
2022-05-25 11:27       ` Arnaldo Carvalho de Melo
2022-05-25 11:44         ` Arnaldo Carvalho de Melo
2022-05-25 14:06           ` Arnaldo Carvalho de Melo
2022-05-18 22:47 ` [PATCH 4/6] perf record: Handle argument change in sched_switch Namhyung Kim
2022-05-19  4:04   ` Ian Rogers
2022-05-18 22:47 ` [PATCH 5/6] perf record: Add cgroup support for off-cpu profiling Namhyung Kim
2022-05-19  4:07   ` Ian Rogers
2022-05-18 22:47 ` [PATCH 6/6] perf test: Add a basic offcpu profiling test Namhyung Kim
2022-05-19  4:08   ` Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Ypk7U1Ce4Muq3l5U@google.com \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=andrii@kernel.org \
    --cc=blakejones@google.com \
    --cc=bpf@vger.kernel.org \
    --cc=haoluo@google.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=milian.wolff@kdab.com \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=songliubraving@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.