Linux Documentation
 help / color / mirror / Atom feed
From: Kunwu Chan <kunwu.chan@gmail.com>
To: corbet@lwn.net, skhan@linuxfoundation.org, peterz@infradead.org,
	mingo@redhat.com, acme@kernel.org, namhyung@kernel.org,
	mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
	jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com,
	james.clark@linaro.org, kunwu.chan@gmail.com,
	lianux.mm@gmail.com
Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-perf-users@vger.kernel.org,
	linux-kselftest@vger.kernel.org, sj@kernel.org
Subject: [RFC PATCH 0/5] perf/core: add AUX buffer kernel-consumer API
Date: Fri, 14 Aug 2026 22:49:16 +0800	[thread overview]
Message-ID: <20260814144927.489172-1-kunwu.chan@linux.dev> (raw)

From: Kunwu Chan <kunwu.chan@gmail.com>

ARM SPE and other tracing PMUs write records directly to perf AUX
buffers.  Unlike ordinary sampling PMUs, they do not necessarily
generate a perf_event_overflow() callback for each record.  A kernel
consumer therefore needs to own and drain an AUX buffer rather than
rely on the overflow path.

Userspace obtains AUX storage through perf_event_open() and mmap().
Kernel events created by perf_event_create_kernel_counter() have no
public interface for allocating that storage: rb_alloc_aux() is
reached through perf_mmap().

This series adds a small perf-core API for that use case.  Patches 1-2
add an in-kernel AUX owner, a setup/release pair, and producer-head,
consumer-tail, and copy helpers without exposing perf's internal page
array.  Patches 3-5 add KUnit tests, a userspace regression test, and
a selftest orchestration script.  The first expected consumer is a
DAMON ARM SPE backend, but this series is independent of DAMON and
does not add consumer-specific policy to perf.

The API follows the existing non-overwrite AUX protocol:

- setup validates the event, rejects non-power-of-two page counts and
  negative watermark, allocates the ring buffer and AUX pages, and
  attaches through perf's mmap-serialized lifecycle;
- release stops writers, drops the event->rb reference, and detaches
  only a buffer owned by this API;
- head reads use smp_rmb() to pair with the producer's data-write
  barrier;
- tail updates use smp_rmb() before and smp_mb() after to order data
  reads before releasing space;
- copy validates the requested modular cursor interval against
  [tail, head] before applying the ring mask and holds an AUX
  reference while copying.

Testing
=======

Based on commit 917d558b151c ("perf/x86: Optimize ACR handling in
match_prev_assignment()") in the tip perf/core branch.

Built and tested on arm64 (Kunpeng 920, ARM SPE present).  Each commit
compiles independently.  Full Image build succeeds with
CONFIG_PERF_AUX_KERNEL_KUNIT_TEST=y.

KUnit (39 cases, dummy AUX PMU, zero hardware dependency):
  pass:39 fail:0 skip:0

  The dummy PMU registered in suite_init provides ->setup_aux/->free_aux
  callbacks; setup_aux pre-fills pages with 0xAB so copy tests verify
  data correctness byte-by-byte.  Cases cover:

  - setup/release lifecycle (12): non-power-of-2 rejection, pow2
    acceptance, double-setup -EBUSY, parent event rejection, negative
    watermark, explicit watermark, writer admission/blocking, no-op
    release, setup/release roundtrip, multi-cycle, double release
  - accessor contract (20): head initial/advances/no-rb, copy full
    window/wrap/zero-len/data-correctness, copy rejects
    rewound/future/oversized/null/already-consumed/no-rb, tail_set
    valid/future/within-window/behind-ring/noop/consume-all/no-rb
  - multi-cycle produce->copy->tail_set (1)
  - perf_aux_output_end size=0, buffer-full stops producer (2)
  - two-event independence, release isolation (2)
  - user/kernel coexistence (1): simulated aux_mmap_count=1 event
    and aux_kernel_count=1 event on the same PMU, verify mutual
    non-interference and release isolation
  - concurrent release (1): two kthreads concurrently call
    perf_event_release_aux() on the same event, verifying the
    refcount_dec_and_mutex_lock serialisation and detach-block
    event->rb guard correctly handle the race

Selftest script (9 sections):
  94 passed, 0 failed, 2 skipped -- Overall: PASS

  Sections: build-time config, KUnit orchestration, userspace
  regression, hardware perf record + dmesg, DAMON integration,
  memory ordering static analysis, boundary validation, concurrency
  safety, user/kernel AUX isolation.

Userspace C program:
  9 PASS, 0 FAIL, 2 SKIP

  The program follows the standard dual-mmap AUX protocol: mmap the
  metadata/data ring at offset 0, set aux_offset/aux_size in the
  metadata page, then mmap the AUX area at aux_offset.  When the AUX
  area mmap fails (e.g. PMU does not support it), the test is SKIP
  rather than FAIL.  ARM SPE single-event limitation is also SKIP.

Kunwu Chan (3):
  perf/core: add AUX buffer ownership for kernel events
  perf/core: add AUX ring accessors for kernel consumers
  selftests/perf_events: add AUX kernel API selftest script

Lian Wang (2):
  perf/core: add KUnit tests for AUX kernel-consumer API
  selftests/perf_events: add userspace AUX regression test

 .../userspace-api/perf_ring_buffer.rst        |   56 +-
 include/linux/perf_event.h                    |   18 +
 kernel/events/Makefile                        |    1 +
 kernel/events/aux_kernel_test.c               | 1144 +++++++++++++++++
 kernel/events/core.c                          |  139 ++
 kernel/events/internal.h                      |    1 +
 kernel/events/ring_buffer.c                   |  205 ++-
 lib/Kconfig.debug                             |   15 +
 tools/testing/selftests/perf_events/Makefile  |    2 +
 .../selftests/perf_events/aux_kernel.sh       |  563 ++++++++
 .../perf_events/aux_kernel_usermode.c         |  546 ++++++++
 tools/testing/selftests/perf_events/config    |    2 +
 12 files changed, 2687 insertions(+), 5 deletions(-)
 create mode 100644 kernel/events/aux_kernel_test.c
 create mode 100755 tools/testing/selftests/perf_events/aux_kernel.sh
 create mode 100644 tools/testing/selftests/perf_events/aux_kernel_usermode.c

-- 
2.43.0


             reply	other threads:[~2026-08-14 14:49 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-14 14:49 Kunwu Chan [this message]
2026-08-14 14:49 ` [RFC PATCH 1/5] perf/core: add AUX buffer ownership for kernel events Kunwu Chan
2026-08-14 14:49 ` [RFC PATCH 2/5] perf/core: add AUX ring accessors for kernel consumers Kunwu Chan
2026-08-14 14:49 ` [RFC PATCH 3/5] perf/core: add KUnit tests for AUX kernel-consumer API Kunwu Chan
2026-08-14 14:49 ` [RFC PATCH 4/5] selftests/perf_events: add userspace AUX regression test Kunwu Chan
2026-08-14 14:49 ` [RFC PATCH 5/5] selftests/perf_events: add AUX kernel API selftest script Kunwu Chan

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=20260814144927.489172-1-kunwu.chan@linux.dev \
    --to=kunwu.chan@gmail.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=corbet@lwn.net \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=lianux.mm@gmail.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sj@kernel.org \
    --cc=skhan@linuxfoundation.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox