All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Jiri Olsa <olsajiri@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	bpf@vger.kernel.org, Martin KaFai Lau <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
	Quentin Monnet <qmo@kernel.org>, Tao Chen <chen.dylane@linux.dev>,
	STAR Labs SG <info@starlabs.sg>,
	Arnaud Lecomte <contact@arnaud-lcm.com>
Subject: Re: [PATCHv2 bpf-next 09/11] bpf: Remove trace_in argument from __bpf_get_stack
Date: Mon, 3 Aug 2026 09:37:00 +0200	[thread overview]
Message-ID: <anBFHILS3Ac-RmtT@krava> (raw)
In-Reply-To: <CAEf4BzaWoB-RnBqa62X4pzM0PBvn21ZA_Fh3P9pWZ6s5_Hjoeg@mail.gmail.com>

On Fri, Jul 31, 2026 at 08:14:03AM -0700, Andrii Nakryiko wrote:
> On Fri, Jul 31, 2026 at 6:33 AM Jiri Olsa <olsajiri@gmail.com> wrote:
> >
> > On Thu, Jul 30, 2026 at 04:06:14PM -0700, Andrii Nakryiko wrote:
> >
> > SNIP
> >
> > > >  BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
> > > >            void *, buf, u32, size, u64, flags)
> > > >  {
> > > > @@ -947,7 +970,7 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
> > > >         int err = -EINVAL;
> > > >
> > > >         if (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN))
> > > > -               return __bpf_get_stack(regs, NULL, NULL, buf, size, flags, false /* !may_fault */);
> > > > +               return __bpf_get_stack(regs, NULL, buf, size, flags, false /* !may_fault */);
> > > >
> > > >         if (unlikely(flags & ~(BPF_F_SKIP_FIELD_MASK | BPF_F_USER_STACK |
> > > >                                BPF_F_USER_BUILD_ID)))
> > > > @@ -966,7 +989,7 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
> > > >
> > > >         if (kernel) {
> > > >                 trace->nr = nr_kernel;
> > >
> > > this whole count_kernel_ip() logic above, why do we have it? I don't
> > > think we can have combined user and kernel stack trace, so how can we
> > > end up with PERF_CONTEXT_USER "ip" at all? I might be missing
> >
> > the callchain comes from perf event's callchain and that can
> > have both kernel and user part
> 
> in general, but bpf_get_stack and bpf_get_stackid never request both
> kernel and user stack trace in one go and we set add_mark to false, so
> we can't have PERF_CONTEXT_USER, which is why I am asking what's the
> deal with this nr_kernel calculation.

it's the bpf_get_stack_pe helper for perf event and we store the
callchain before we run bpf program in bpf_overflow_handler:

  perf_prepare_sample(data, event, regs); 
   -> perf_sample_save_callchain
      -> perf_callchain
         -> get_perf_callchain(... add_mark=true ..)

  ret = bpf_prog_run(prog, &ctx);
   -> bpf_get_stack_pe
      -> trace = ctx->data->callchain

the perf event setup is independent of bpf_get_stack execution,
so you can have PERF_CONTEXT_USER in the callchain

jirka


> 
> If it's unnecessary, we should just drop that part and not have that
> extra unnecessary complication
> 
> >
> > > something subtle here, but can you please check again if this whole
> > > trace->nr calculation/override is even necessary.
> >
> > as I wrote in the other reply I don't think we need to change
> > trace->nr directly, just pass the needed nr/count as argument
> >
> > jirka
> >
> > >
> > > > -               err = __bpf_get_stack(regs, NULL, trace, buf, size, flags, false /* !may_fault */);
> > > > +               err = __bpf_get_stack_pe(trace, buf, size, flags);
> > > >
> > > >         } else { /* user */
> > > >                 u64 skip = flags & BPF_F_SKIP_FIELD_MASK;
> > > > @@ -974,9 +997,8 @@ BPF_CALL_4(bpf_get_stack_pe, struct bpf_perf_event_data_kern *, ctx,
> > > >                 skip += nr_kernel;
> > > >                 if (skip > BPF_F_SKIP_FIELD_MASK)
> > > >                         goto clear;
> > > > -
> > > >                 flags = (flags & ~BPF_F_SKIP_FIELD_MASK) | skip;
> > > > -               err = __bpf_get_stack(regs, NULL, trace, buf, size, flags, false /* !may_fault */);
> > > > +               err = __bpf_get_stack_pe(trace, buf, size, flags);
> > > >         }
> > > >
> > > >         /* restore nr */
> > > > --
> > > > 2.54.0
> > > >

  reply	other threads:[~2026-08-03  7:37 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-29  8:37 [PATCHv2 bpf-next 00/11] bpf: Disable preemption in stack map code Jiri Olsa
2026-07-29  8:37 ` [PATCHv2 bpf-next 01/11] bpf: Factor stackid_init function from __bpf_get_stackid Jiri Olsa
2026-07-29  8:51   ` sashiko-bot
2026-07-30 12:04     ` Jiri Olsa
2026-07-29  8:37 ` [PATCHv2 bpf-next 02/11] bpf: Factor stackid_fastpath " Jiri Olsa
2026-07-29  8:37 ` [PATCHv2 bpf-next 03/11] bpf: Factor stackid_new_bucket " Jiri Olsa
2026-07-29  8:38 ` [PATCHv2 bpf-next 04/11] bpf: Use stack id functions instead of __bpf_get_stackid Jiri Olsa
2026-07-29  9:42   ` bot+bpf-ci
2026-07-29  8:38 ` [PATCHv2 bpf-next 05/11] bpf: Disable preemption in bpf_get_stackid Jiri Olsa
2026-07-29 10:30   ` Leon Hwang
2026-07-30 11:38     ` Jiri Olsa
2026-07-30 13:43       ` Leon Hwang
2026-07-30 22:52         ` Andrii Nakryiko
2026-07-31 13:33           ` Jiri Olsa
2026-07-29  8:38 ` [PATCHv2 bpf-next 06/11] bpf: Factor callchain_store function from __bpf_get_stack Jiri Olsa
2026-07-29  8:57   ` sashiko-bot
2026-07-30 12:04     ` Jiri Olsa
2026-07-29  8:38 ` [PATCHv2 bpf-next 07/11] bpf: Factor callchain_finalize " Jiri Olsa
2026-07-29  9:41   ` bot+bpf-ci
2026-07-29  8:38 ` [PATCHv2 bpf-next 08/11] bpf: Restore trace->nr value properly in bpf_get_stack_pe Jiri Olsa
2026-07-30 23:01   ` Andrii Nakryiko
2026-07-31 13:33     ` Jiri Olsa
2026-07-29  8:38 ` [PATCHv2 bpf-next 09/11] bpf: Remove trace_in argument from __bpf_get_stack Jiri Olsa
2026-07-29  9:58   ` bot+bpf-ci
2026-07-30 23:06   ` Andrii Nakryiko
2026-07-31 13:33     ` Jiri Olsa
2026-07-31 15:14       ` Andrii Nakryiko
2026-08-03  7:37         ` Jiri Olsa [this message]
2026-08-03 17:45           ` Andrii Nakryiko
2026-07-29  8:38 ` [PATCHv2 bpf-next 10/11] bpf: Disable preemption in __bpf_get_stack Jiri Olsa
2026-07-29  8:38 ` [PATCHv2 bpf-next 11/11] bpf: Clear buf on error in __bpf_get_task_stack Jiri Olsa
2026-07-29  9:57   ` bot+bpf-ci
2026-07-30 12:04     ` Jiri Olsa
2026-07-30 23:11   ` Andrii Nakryiko
2026-07-31 13:33     ` Jiri Olsa
2026-07-31 15:18       ` Andrii Nakryiko

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=anBFHILS3Ac-RmtT@krava \
    --to=olsajiri@gmail.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chen.dylane@linux.dev \
    --cc=contact@arnaud-lcm.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=info@starlabs.sg \
    --cc=martin.lau@linux.dev \
    --cc=qmo@kernel.org \
    --cc=songliubraving@fb.com \
    --cc=yhs@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.