mbox series

[PATCHSET,0/8] perf/core: Prepare sample data for BPF (v3)

Message ID 20230118060559.615653-1-namhyung@kernel.org (mailing list archive)
Headers show
Series perf/core: Prepare sample data for BPF (v3) | expand

Message

Namhyung Kim Jan. 18, 2023, 6:05 a.m. UTC
Hello,

The perf_prepare_sample() is to fill the perf sample data and update the
header info before sending it to the ring buffer.  But we want to use it
for BPF overflow handler so that it can access the sample data to filter
relevant ones.

Changes in v3)
 * update data->sample_flags in __perf_event_header__init_id()  (PeterZ)
 * add Acked-by's from Jiri and Song

Changes in v2)
 * the layout change is merged
 * reduce branches using __cond_set  (PeterZ)
 * add helpers to set dynamic sample data  (PeterZ)
 * introduce perf_prepare_header()  (PeterZ)
 * call perf_prepare_sample() before bpf_overflow_handler unconditionally

This means the perf_prepare_handler() can be called more than once.  To
avoid duplicate work, use the data->sample_flags and save the data size.

I also added a few of helpers to set those information accordingly.
But it looks some fields like REGS_USER, STACK_USER and AUX are saved in
the perf_prepare_sample() so I didn't add the helpers for them.

After than we can just check the filtered_sample_type flags begin zero
to determine if it has more work.  In that case, it needs to update the
data->type since it's possible to miss when PMU driver sets all required
sample flags before calling perf_prepare_sample().

The code is also available at 'perf/prepare-sample-v3' branch in

  git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Thanks,
Namhyung

Cc: Song Liu <song@kernel.org>
Cc: bpf@vger.kernel.org

Namhyung Kim (8):
  perf/core: Save the dynamic parts of sample data size
  perf/core: Add perf_sample_save_callchain() helper
  perf/core: Add perf_sample_save_raw_data() helper
  perf/core: Add perf_sample_save_brstack() helper
  perf/core: Set data->sample_flags in perf_prepare_sample()
  perf/core: Do not pass header for sample id init
  perf/core: Introduce perf_prepare_header()
  perf/core: Call perf_prepare_sample() before running BPF

 arch/powerpc/perf/core-book3s.c    |   3 +-
 arch/s390/kernel/perf_cpum_cf.c    |   4 +-
 arch/s390/kernel/perf_cpum_sf.c    |   3 +-
 arch/s390/kernel/perf_pai_crypto.c |   4 +-
 arch/s390/kernel/perf_pai_ext.c    |   4 +-
 arch/x86/events/amd/core.c         |   6 +-
 arch/x86/events/amd/ibs.c          |   9 +-
 arch/x86/events/intel/core.c       |   6 +-
 arch/x86/events/intel/ds.c         |  24 ++--
 include/linux/perf_event.h         | 133 +++++++++++++-----
 kernel/events/core.c               | 207 ++++++++++++++++-------------
 kernel/trace/bpf_trace.c           |   6 +-
 12 files changed, 236 insertions(+), 173 deletions(-)


base-commit: 9fcad995c6c52cc9791f7ee9f1386a5684055f9c

Comments

Peter Zijlstra Jan. 18, 2023, 10:50 a.m. UTC | #1
On Tue, Jan 17, 2023 at 10:05:51PM -0800, Namhyung Kim wrote:
> Namhyung Kim (8):
>   perf/core: Save the dynamic parts of sample data size
>   perf/core: Add perf_sample_save_callchain() helper
>   perf/core: Add perf_sample_save_raw_data() helper
>   perf/core: Add perf_sample_save_brstack() helper
>   perf/core: Set data->sample_flags in perf_prepare_sample()
>   perf/core: Do not pass header for sample id init
>   perf/core: Introduce perf_prepare_header()
>   perf/core: Call perf_prepare_sample() before running BPF

Thanks!,

Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Alexei Starovoitov Jan. 18, 2023, 7:42 p.m. UTC | #2
On Wed, Jan 18, 2023 at 11:50:37AM +0100, Peter Zijlstra wrote:
> On Tue, Jan 17, 2023 at 10:05:51PM -0800, Namhyung Kim wrote:
> > Namhyung Kim (8):
> >   perf/core: Save the dynamic parts of sample data size
> >   perf/core: Add perf_sample_save_callchain() helper
> >   perf/core: Add perf_sample_save_raw_data() helper
> >   perf/core: Add perf_sample_save_brstack() helper
> >   perf/core: Set data->sample_flags in perf_prepare_sample()
> >   perf/core: Do not pass header for sample id init
> >   perf/core: Introduce perf_prepare_header()
> >   perf/core: Call perf_prepare_sample() before running BPF
> 
> Thanks!,
> 
> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>

Thanks Peter.
The patches look good to me as well.
How do you want to route them bpf-next or tip ?

Namhyung,
I'd also like to see a follow up patch with a selftest/bpf for this.

BPF CI didn't have a chance to really test the set, since
there is a build issue on s390 due to llvm upstream repo.
We're aware and working on that.
Namhyung Kim Jan. 18, 2023, 10:08 p.m. UTC | #3
Hi Alexei,

On Wed, Jan 18, 2023 at 11:43 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Wed, Jan 18, 2023 at 11:50:37AM +0100, Peter Zijlstra wrote:
> > On Tue, Jan 17, 2023 at 10:05:51PM -0800, Namhyung Kim wrote:
> > > Namhyung Kim (8):
> > >   perf/core: Save the dynamic parts of sample data size
> > >   perf/core: Add perf_sample_save_callchain() helper
> > >   perf/core: Add perf_sample_save_raw_data() helper
> > >   perf/core: Add perf_sample_save_brstack() helper
> > >   perf/core: Set data->sample_flags in perf_prepare_sample()
> > >   perf/core: Do not pass header for sample id init
> > >   perf/core: Introduce perf_prepare_header()
> > >   perf/core: Call perf_prepare_sample() before running BPF
> >
> > Thanks!,
> >
> > Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
>
> Thanks Peter.
> The patches look good to me as well.
> How do you want to route them bpf-next or tip ?
>
> Namhyung,
> I'd also like to see a follow up patch with a selftest/bpf for this.
>
> BPF CI didn't have a chance to really test the set, since
> there is a build issue on s390 due to llvm upstream repo.
> We're aware and working on that.

Yep, I'll add a test case as soon as it gets the change.

Thanks,
Namhyung