Skip to contentSkip to conversation

perf/core: add AUX buffer kernel-consumer API

RFC PATCH New Kunwu Chan 2 participants 11 messages Aug 14

Subsystems (4)
lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

cover perf/core: add AUX buffer kernel-consumer API

From: Kunwu Chan <[email protected]>

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
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/5 perf/core: add AUX buffer ownership for kernel events

From: Kunwu Chan <[email protected]>

Add an in-kernel AUX owner reference and setup/release helpers for kernel-created perf events that need an AUX buffer without a userspace mmap. The setup path validates that the event is a kernel event with no parent, rejects non-power-of-two page counts and negative watermark values, allocates the perf buffer and AUX pages, records the kernel owner, and attaches the buffer through the existing mmap-serialized helper. The release path stops AUX writers, frees AUX storage, drops the event->rb reference via ring_buffer_put(), and detaches only buffers owned by this API.

Keep aux_mmap_count dedicated to userspace mappings. perf_aux_output_begin() accepts a writer while either a userspace or kernel owner remains, preserving the existing teardown ordering.

---
 include/linux/perf_event.h  |  11 +++
 kernel/events/core.c        | 139 ++++++++++++++++++++++++++++++++++++
 kernel/events/internal.h    |   1 +
 kernel/events/ring_buffer.c |  11 +--
 4 files changed, 158 insertions(+), 4 deletions(-)
include/linux/perf_event.h +11 -0
index 48d851fbd8ea..4070a725d21f 100644
@@ -1254,6 +1254,17 @@ perf_event_create_kernel_counter(struct perf_event_attr *attr,
 				 perf_overflow_handler_t callback,
 				 void *context);
 
+/*
+ * AUX ring-buffer support for kernel-created events (no user mmap).
+ * perf_event_setup_aux() allocates the buffer the PMU writes into via
+ * perf_aux_output_begin()/perf_aux_output_end(); the paired
+ * perf_event_release_aux() must be called before
+ * perf_event_release_kernel().
+ */
+extern int perf_event_setup_aux(struct perf_event *event, int nr_pages,
+				long watermark);
+extern void perf_event_release_aux(struct perf_event *event);
+
 extern void perf_pmu_migrate_context(struct pmu *pmu,
 				     int src_cpu, int dst_cpu);
 extern int perf_event_read_local(struct perf_event *event, u64 *value,
kernel/events/core.c +139 -0

145 lines not on this page · raw message

kernel/events/internal.h +1 -0
index c03c4f2eea57..7bcb60827514 100644
@@ -48,6 +48,7 @@ struct perf_buffer {
 	int				aux_nr_pages;
 	int				aux_overwrite;
 	refcount_t			aux_mmap_count;
+	refcount_t			aux_kernel_count; /* in-kernel AUX owner */
 	unsigned long			aux_mmap_locked;
 	void				(*free_aux)(void *);
 	refcount_t			aux_refcount;
kernel/events/ring_buffer.c +7 -4
index 9fe92161715e..be57007a1b79 100644
@@ -395,14 +395,17 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
 		goto err;
 
 	/*
-	 * If aux_mmap_count is zero, the aux buffer is in perf_mmap_close(),
-	 * about to get freed, so we leave immediately.
+	 * If no AUX owner remains, the buffer is in perf_mmap_close() or
+	 * perf_event_release_aux(), about to get freed, so we leave
+	 * immediately.  aux_mmap_count tracks user-space mmap owners;
+	 * aux_kernel_count tracks in-kernel owners (perf_event_setup_aux()).
 	 *
-	 * Checking rb::aux_mmap_count and rb::refcount has to be done in
+	 * Checking the AUX owner counts and rb::refcount has to be done in
 	 * the same order, see perf_mmap_close. Otherwise we end up freeing
 	 * aux pages in this path, which is a bug, because in_atomic().
 	 */
-	if (!refcount_read(&rb->aux_mmap_count))
+	if (!refcount_read(&rb->aux_mmap_count) &&
+	    !refcount_read(&rb->aux_kernel_count))
 		goto err;
 
 	if (!refcount_inc_not_zero(&rb->aux_refcount))
Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH 1/5] perf/core: add AUX buffer ownership for kernel events

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Double `ring_buffer_put()` in `perf_event_release_aux()` leads to refcount underflow and…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Double ring_buffer_put() in perf_event_release_aux() leads to refcount underflow and use-after-free of the ring buffer.
- [High] Concurrent detach of event->rb causes a NULL pointer dereference inside perf_pmu_output_stop(). --

--- Patch [1]: [RFC PATCH 1/5] perf/core: add AUX buffer ownership for kernel events --- commit 34b60be57b99a06be2e537e647853e4c5c20c8c2
Author: Kunwu Chan <[email protected]>

perf/core: add AUX buffer ownership for kernel events

This commit adds an in-kernel AUX owner reference and setup/release helpers for kernel-created perf events. It introduces perf_event_setup_aux() and perf_event_release_aux() to handle AUX buffer lifecycle without requiring a userspace mmap.

Quoted 3 lines
kernel/events/core.c +0 -0

[ ... ]

Quoted 12 lines
kernel/events/core.c +12 -0
+void perf_event_release_aux(struct perf_event *event)
+{
+	struct perf_buffer *rb;
+
+	if (!is_kernel_event(event) || event->parent)
+		return;
+
+	rb = ring_buffer_get(event);
+	if (!rb)
+		return;
+
+	/* Do not detach a ring buffer that this API does not own. */

kernel/events/core.c static void perf_mmap_close(struct vm_area_struct *vma) · line 7239

Quoted 10 lines
kernel/events/core.c +10 -0
+	if (!rb_has_aux(rb) || !refcount_read(&rb->aux_kernel_count))
+		goto out_put;
+
+	if (refcount_dec_and_mutex_lock(&rb->aux_kernel_count, &rb->aux_mutex)) {
+		/*
+		 * Stop all AUX events writing to this buffer so the pages
+		 * can be freed; after aux_kernel_count drops to zero they
+		 * won't start any more (see perf_aux_output_begin()).
+		 */
+		perf_pmu_output_stop(event);

[Severity: High]
Is there a race condition here if multiple threads call this concurrently?

If Thread 1 enters this block and calls perf_pmu_output_stop(), it accesses event->rb directly:

static void perf_pmu_output_stop(struct perf_event *event)

{
        ...
        list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {

Meanwhile, Thread 2 could fail the aux_kernel_count decrement, skip the block, and immediately detach the buffer:

        if (event->rb == rb) {
                ring_buffer_attach(event, NULL);

Since ring_buffer_attach(event, NULL) sets event->rb to NULL, could Thread 1 dereference a NULL pointer inside perf_pmu_output_stop()?

Quoted 6 lines
kernel/events/core.c +6 -0
+
+		rb_free_aux(rb);
+		WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
+		mutex_unlock(&rb->aux_mutex);
+	}
+

kernel/events/core.c static void perf_mmap_close(struct vm_area_struct *vma) · line 7255

Quoted 10 lines
kernel/events/core.c +10 -0
+	/*
+	 * Detach the ring buffer from the event.  This runs even if the
+	 * refcount_dec_and_mutex_lock above lost the race to another
+	 * concurrent release caller; the mmap_mutex serialisation and the
+	 * event->rb == rb check ensure only one caller performs the detach.
+	 */
+	mutex_lock(&event->mmap_mutex);
+	if (event->rb == rb) {
+		ring_buffer_attach(event, NULL);
+		ring_buffer_put(rb); /* drop the event->rb reference */

[Severity: High]
Does this result in a double-free?

Looking at ring_buffer_attach(), it already drops the reference to the old buffer when it is replaced:

static void ring_buffer_attach(struct perf_event *event,
struct perf_buffer *rb)

{
        ...
        rcu_assign_pointer(event->rb, rb);

        if (old_rb) {
                ring_buffer_put(old_rb);
        ...
}

If ring_buffer_attach(event, NULL) already calls ring_buffer_put() on the detached buffer, will calling ring_buffer_put(rb) again immediately after cause the refcount to underflow and prematurely free the ring buffer?

Quoted 6 lines
kernel/events/core.c +6 -0
+	}
+	mutex_unlock(&event->mmap_mutex);
+
+out_put:
+	ring_buffer_put(rb); /* the temporary reference from ring_buffer_get() */
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

RFC PATCH 2/5 perf/core: add AUX ring accessors for kernel consumers

In reply to Kunwu Chan

From: Kunwu Chan <[email protected]>

Add kernel-consumer accessors for the published AUX producer head, the consumer tail, and copying a possibly wrapped AUX interval without exposing perf's internal page array.

Use the same memory ordering as mmap consumers: order data reads after the published head with smp_rmb() and order completed data reads before advancing the tail with smp_mb(). Validate cursor distances in the absolute cursor domain before applying the ring mask, so rewound, already-consumed, and future windows are rejected even across unsigned cursor wrap.

Hold an AUX reference while copying so storage cannot disappear under the consumer. Restrict the helpers to buffers owned by the kernel AUX setup API. Document the API in Documentation/userspace-api/perf_ring_buffer.rst (§3.4).

---
 .../userspace-api/perf_ring_buffer.rst        |  56 ++++-
 include/linux/perf_event.h                    |   7 +
 kernel/events/ring_buffer.c                   | 194 ++++++++++++++++++
 3 files changed, 256 insertions(+), 1 deletion(-)
Documentation/userspace-api/perf_ring_buffer.rst +55 -1
index dc71544532ce..8333d2791878 100644
@@ -26,6 +26,7 @@ Perf ring buffer
     3.1  The relationship between AUX and regular ring buffers
     3.2  AUX events
     3.3  Snapshot mode
+    3.4  Kernel-consumer AUX buffer access
 
 
 1. Introduction
@@ -827,4 +828,57 @@ mode.
          |                 AUX Ring buffer 3     | <- aux_head
          +---------------------------------------+
 
-                Figure 9. Snapshot with system wide mode
+                 Figure 9. Snapshot with system wide mode
+
+3.4 Kernel-consumer AUX buffer access
+-------------------------------------
+
+The AUX ring buffer is normally consumed from user space via mmap()
+on the perf event fd.  Some tracing PMUs (e.g. ARM SPE) write trace
+records directly to the AUX buffer without generating a
+perf_event_overflow() callback for each record.  A kernel consumer
+therefore needs to own and drain the AUX buffer itself rather than
+rely on the overflow path.
+
+The perf core provides five exported functions for in-kernel
+consumers that do not have a user-space mmap:
+
+  - ``perf_event_setup_aux(event, nr_pages, watermark)`` — allocate
+    an AUX ring buffer for a kernel-created perf event.  The event
+    must be created by ``perf_event_create_kernel_counter()``; it
+    must not have a parent or an existing ring buffer.  ``nr_pages``
+    must be a power of two and ``watermark`` must be non-negative
+    (0 selects half the buffer).
+
+  - ``perf_event_release_aux(event)`` — tear down the AUX buffer
+    allocated by ``perf_event_setup_aux()``.  Must be called before
+    ``perf_event_release_kernel()``.  Safe to call on an event that
+    never had an AUX buffer (no-op).  A ring buffer not owned by the
+    kernel AUX API is left attached.
+
+  - ``perf_event_aux_head(event)`` — read the published producer
+    head.  Returns 0 if the event has no ring buffer.
+
+  - ``perf_event_aux_tail_set(event, tail)`` — advance the consumer
+    tail.  The new tail must be within the current ``[old_tail,
+    head]`` window; an out-of-range tail is rejected with ``-EINVAL``.
+
+  - ``perf_event_aux_copy(event, from, to, buf)`` — copy a possibly
+    wrapped AUX interval into a linear buffer.  The ``from``/``to``
+    cursors are absolute and must lie within ``[tail, head]``.
+
+The owner reference is tracked by ``aux_kernel_count`` on the
+``perf_buffer``, separate from the userspace ``aux_mmap_count``.
+``perf_aux_output_begin()`` admits a writer while either owner
+count is non-zero, so a kernel consumer and a userspace consumer
+on different events for the same PMU do not interfere.
+
+A userspace mmap and a kernel ``perf_event_setup_aux()`` on the
+*same* event cannot coexist: the second call finds ``event->rb``
+already set and returns ``-EBUSY``.
+
+Memory ordering follows the same protocol as the userspace AUX
+mmap consumer: ``perf_event_aux_head()`` uses ``smp_rmb()`` to pair
+with the producer's data-write barrier before publishing ``aux_head``,
+and ``perf_event_aux_tail_set()`` uses ``smp_mb()`` to order prior
+data reads before advancing ``aux_tail``.
include/linux/perf_event.h +7 -0
index 4070a725d21f..800638f70628 100644
@@ -1265,6 +1265,13 @@ extern int perf_event_setup_aux(struct perf_event *event, int nr_pages,
 				long watermark);
 extern void perf_event_release_aux(struct perf_event *event);
 
+/* AUX ring accessors for kernel consumers (no user-space mmap). */
+extern unsigned long perf_event_aux_head(struct perf_event *event);
+extern int perf_event_aux_tail_set(struct perf_event *event,
+				   unsigned long tail);
+extern long perf_event_aux_copy(struct perf_event *event, unsigned long from,
+				unsigned long to, void *buf);
+
 extern void perf_pmu_migrate_context(struct pmu *pmu,
 				     int src_cpu, int dst_cpu);
 extern int perf_event_read_local(struct perf_event *event, u64 *value,
kernel/events/ring_buffer.c +194 -0

200 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [RFC PATCH 2/5] perf/core: add AUX ring accessors for kernel consumers

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

From: Kunwu Chan <[email protected]>

Add kernel-consumer accessors for the published AUX producer head, the consumer tail, and copying a possibly wrapped AUX interval without exposing perf's internal page array.

Use the same memory ordering as mmap consumers: order data reads after the published head with smp_rmb() and order completed data reads before advancing the tail with smp_mb(). Validate cursor distances in the absolute cursor domain before applying the ring mask, so rewound, already-consumed, and future windows are rejected even across unsigned cursor wrap.

Quoted 8 lines

Hold an AUX reference while copying so storage cannot disappear under the consumer. Restrict the helpers to buffers owned by the kernel AUX setup API. Document the API in Documentation/userspace-api/perf_ring_buffer.rst (§3.4).

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

RFC PATCH 3/5 perf/core: add KUnit tests for AUX kernel-consumer API

In reply to Kunwu Chan

From: Lian Wang <[email protected]>

Add a KUnit test suite (perf_aux_kernel) for the perf_event_setup_aux(), perf_event_release_aux(), perf_event_aux_head(), perf_event_aux_tail_set() and perf_event_aux_copy() functions.

A self-contained dummy AUX PMU is registered in suite_init so that every test is deterministic and needs no hardware. The dummy setup_aux pre-fills all pages with a recognisable pattern (0xAB); aux_test_produce() advances the published head via perf_aux_output_begin/end, and copy tests verify the pattern.

39 test cases cover setup/release lifecycle, writer admission and blocking, head/tail/copy accessor contract validation (including wrap-around, boundary cursors, and data correctness), buffer-full behaviour, negative watermark rejection, independence of two events on the same PMU, user/kernel coexistence simulation, and concurrent release on the same event.

Gated by CONFIG_PERF_AUX_KERNEL_KUNIT_TEST.

---
 kernel/events/Makefile          |    1 +
 kernel/events/aux_kernel_test.c | 1144 +++++++++++++++++++++++++++++++
 lib/Kconfig.debug               |   15 +
 3 files changed, 1160 insertions(+)
 create mode 100644 kernel/events/aux_kernel_test.c
kernel/events/Makefile +1 -0
index 91a62f566743..592604db2c76 100644
@@ -3,4 +3,5 @@ obj-y := core.o ring_buffer.o callchain.o
 
 obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
 obj-$(CONFIG_HW_BREAKPOINT_KUNIT_TEST) += hw_breakpoint_test.o
+obj-$(CONFIG_PERF_AUX_KERNEL_KUNIT_TEST) += aux_kernel_test.o
 obj-$(CONFIG_UPROBES) += uprobes.o
kernel/events/aux_kernel_test.c new file +1144 -0

1,144 lines not on this page · raw message

lib/Kconfig.debug +15 -0
index 1244dcac2294..30f4ab1dd791 100644
@@ -3102,6 +3102,21 @@ config HW_BREAKPOINT_KUNIT_TEST
 
 	  If unsure, say N.
 
+config PERF_AUX_KERNEL_KUNIT_TEST
+	bool "Test perf AUX kernel-consumer API" if !KUNIT_ALL_TESTS
+	depends on PERF_EVENTS=y
+	depends on KUNIT=y
+	default KUNIT_ALL_TESTS
+	help
+	  Tests for the perf AUX kernel-consumer API functions:
+	  perf_event_setup_aux(), perf_event_release_aux(),
+	  perf_event_aux_head(), perf_event_aux_tail_set(), and
+	  perf_event_aux_copy().  Error-path contract tests work
+	  without hardware; happy-path tests require an AUX-capable
+	  PMU (ARM SPE, Intel PT, etc.).
+
+	  If unsure, say N.
+
 source "lib/crypto/tests/Kconfig"
 
 config SIPHASH_KUNIT_TEST
Signature · 1 line
2.43.0
#6 lore raw

Re: [RFC PATCH 3/5] perf/core: add KUnit tests for AUX kernel-consumer API

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [High] KUnit assertions (`KUNIT_ASSERT_*`) and `kunit_skip()` abort tests immediately, leaking resources…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [High] KUnit assertions (KUNIT_ASSERT_*) and kunit_skip() abort tests immediately, leaking resources and causing stack Use-After-Free.
- [High] Race between atomic_dec_and_test() and lockless wait_event() causes a stack Use-After-Free in test_concurrent_release().
- [High] Test cases use non-fatal KUNIT_EXPECT_* macros for critical setup steps, resulting in uninitialized stack usage and NULL pointer dereferences.
- [High] test_concurrent_release triggers a refcount_t underflow kernel WARNING via concurrent perf_event_release_aux() calls.
- [High] Error path in test_concurrent_release calls kthread_stop() on natively exiting threads, leading to Use-After-Free.
- [Medium] Dead code in test_buffer_full_stops_producer masks actual API behavior and fails to enforce expected outcomes.
--

--- Patch [3]: [RFC PATCH 3/5] perf/core: add KUnit tests for AUX kernel-consumer API --- commit 7ac72ec86c4a7abdecf89cc4cddc373879a7f3b2
Author: Lian Wang <[email protected]>

perf/core: add KUnit tests for AUX kernel-consumer API

Adds a KUnit test suite (perf_aux_kernel) for the perf_event_setup_aux() and related functions. Registers a self-contained dummy AUX PMU to run tests deterministically without hardware. Includes 39 test cases covering lifecycle, writers, ring buffer accessors, and concurrency.

Quoted 5 lines
kernel/events/aux_kernel_test.c new file +0 -0
new file mode 100644
index 0000000000000..0b5f787c59799

[ ... ]

Quoted 3 lines
kernel/events/aux_kernel_test.c new file +3 -0
+static void test_copy_full_window(struct kunit *test)
+{
+	struct perf_event *event = aux_test_create_event();

[ ... ]

kernel/events/aux_kernel_test.c line 384

Quoted 8 lines
kernel/events/aux_kernel_test.c new file +8 -0
+	ret = perf_event_setup_aux(event, 1, 0);
+	KUNIT_EXPECT_EQ(test, 0, ret);
+
+	head = aux_test_produce(event, buf_size / 2);
+	KUNIT_EXPECT_GT(test, head, 0);
+
+	buf = kunit_kmalloc(test, head, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);

[Severity: High]
Does this assertion leak the perf_event structure?

KUnit assertions like KUNIT_ASSERT_NOT_ERR_OR_NULL abort the test immediately by throwing a KUnit exception. If this allocation fails after aux_test_create_event() succeeds, the test will abort without calling perf_event_release_kernel(), causing a permanent resource leak.

[ ... ]

Quoted 2 lines
kernel/events/aux_kernel_test.c new file +2 -0
+static void test_buffer_full_stops_producer(struct kunit *test)
+{

[ ... ]

kernel/events/aux_kernel_test.c line 883

Quoted 8 lines
kernel/events/aux_kernel_test.c new file +8 -0
+	head = aux_test_produce(event, buf_size);
+	KUNIT_EXPECT_GT(test, head, 0);
+
+	addr = perf_aux_output_begin(&handle, event);
+	if (addr) {
+		KUNIT_EXPECT_EQ(test, 0, handle.size);
+		perf_aux_output_end(&handle, 0);
+	}

[Severity: Medium]
Does this block fail to verify the expected behavior?

When the buffer is full, perf_aux_output_begin() correctly returns NULL. Because addr is NULL, this conditional block is skipped entirely, turning the core validation into dead code. This allows the test to silently pass even if perf_aux_output_begin() incorrectly returns a non-NULL pointer with an arbitrary size.

Would it be better to explicitly assert KUNIT_EXPECT_NULL(test, addr)?

[ ... ]

Quoted 2 lines
kernel/events/aux_kernel_test.c new file +2 -0
+static void test_user_kernel_coexistence(struct kunit *test)
+{

[ ... ]

kernel/events/aux_kernel_test.c line 1003

Quoted 8 lines
kernel/events/aux_kernel_test.c new file +8 -0
+	/* Event B: start with kernel setup, then simulate userspace */
+	ret = perf_event_setup_aux(ev_b, 1, 0);
+	KUNIT_EXPECT_EQ(test, 0, ret);
+
+	/* Simulate userspace ownership: swap refcounts on B's rb.
+	 * perf_mmap() would set aux_mmap_count=1 for a userspace event.
+	 */
+	refcount_set(&ev_b->rb->aux_mmap_count, 1);

[Severity: High]
Can this result in a NULL pointer dereference?

The KUNIT_EXPECT_EQ macro logs failures but allows execution to continue. If perf_event_setup_aux() fails, ev_b->rb will be NULL. The test then proceeds to evaluate refcount_set(&ev_b->rb->aux_mmap_count, 1), which dereferences it.

Should KUNIT_ASSERT_EQ be used for critical setup steps?

[ ... ]

kernel/events/aux_kernel_test.c line 1009

Quoted 4 lines
kernel/events/aux_kernel_test.c new file +4 -0
+	/* Produce on A — B's head stays 0 */
+	addr = perf_aux_output_begin(&handle_a, ev_a);
+	KUNIT_EXPECT_NOT_NULL(test, addr);
+	perf_aux_output_end(&handle_a, 512);

[Severity: High]
Can this sequence cause a kernel panic?

Similar to the previous setup step, if perf_aux_output_begin() fails and returns NULL, handle_a remains uninitialized since KUNIT_EXPECT_NOT_NULL does not halt the test.

Continuing past the failure passes this uninitialized handle to perf_aux_output_end(), which will dereference garbage data from handle_a.rb.

[ ... ]

Quoted 6 lines
kernel/events/aux_kernel_test.c new file +6 -0
+struct concurrent_release_ctx {
+	struct perf_event *event;
+	atomic_t count;
+	wait_queue_head_t wq;
+};
+

kernel/events/aux_kernel_test.c line 1058

Quoted 10 lines
kernel/events/aux_kernel_test.c new file +10 -0
+static int concurrent_release_thread(void *data)
+{
+	struct concurrent_release_ctx *ctx = data;
+
+	perf_event_release_aux(ctx->event);
+
+	if (atomic_dec_and_test(&ctx->count))
+		wake_up(&ctx->wq);
+	return 0;
+}

[Severity: High]
Can decoupling the atomic decrement and the wake_up() call create a stack use-after-free window?

The main thread waits for worker threads using wait_event(), which evaluates its condition locklessly. If the last worker thread executes atomic_dec_and_test(&ctx->count), setting it to 0, the main thread can observe the 0, break out of wait_event(), and return from the function before the worker executes wake_up(&ctx->wq).

Returning from test_concurrent_release() destroys the ctx struct on the stack, meaning the worker thread will then call wake_up() on a destroyed stack frame.

Could this be avoided by using the struct completion API, which is explicitly designed to prevent this race for on-stack waiters?

[ ... ]

Quoted 6 lines
kernel/events/aux_kernel_test.c new file +6 -0
+static void test_concurrent_release(struct kunit *test)
+{
+	struct perf_event *event = aux_test_create_event();
+	struct concurrent_release_ctx ctx;
+	struct task_struct *threads[2];
+	int ret, i;

[ ... ]

kernel/events/aux_kernel_test.c line 1078

Quoted 3 lines
kernel/events/aux_kernel_test.c new file +3 -0
+	for (i = 0; i < ARRAY_SIZE(threads); i++) {
+		threads[i] = kthread_run(concurrent_release_thread, &ctx,
+					 "aux_crel_%d", i);

[Severity: High]
Does intentionally launching concurrent releases here trigger a refcount_t underflow warning?

In perf_event_release_aux() in core.c, the check !refcount_read(&rb->aux_kernel_count) is performed locklessly. Both worker threads can read aux_kernel_count as 1 and proceed to call refcount_dec_and_mutex_lock().

The first thread successfully decrements the count from 1 to 0. The second thread subsequently calls refcount_dec_and_mutex_lock() on a count of 0, which triggers a REFCOUNT_WARN("underflow; use-after-free.") inside __refcount_dec_and_test().

Is it intended for a KUnit test to trigger kernel warnings on a successful path?

kernel/events/aux_kernel_test.c line 1080

Quoted 2 lines
kernel/events/aux_kernel_test.c new file +2 -0
+		if (IS_ERR_OR_NULL(threads[i])) {
+			kunit_skip(test, "failed to create kthread");

[Severity: High]
Can kunit_skip() cause a stack use-after-free if a thread is already running?

kunit_skip() terminates the test immediately by throwing a KUnit exception, bypassing any subsequent cleanup code. This means the test function returns instantly, destroying the ctx struct allocated on the main thread's stack.

If an earlier thread was successfully spawned and is still running, it will access ctx->event and ctx->count on the destroyed stack frame.

kernel/events/aux_kernel_test.c line 1082

Quoted 2 lines
kernel/events/aux_kernel_test.c new file +2 -0
+			while (--i >= 0)
+				kthread_stop(threads[i]);

[Severity: High]
Does kthread_stop() cause a use-after-free on natively exiting threads?

The concurrent_release_thread function does not check kthread_should_stop(); it simply executes and naturally returns 0. When a thread naturally exits, its task_struct is reaped.

Calling kthread_stop() here on a potentially reaped task_struct will access freed memory inside get_task_struct(). While currently masked as dead code due to kunit_skip() aborting execution above, this cleanup logic appears unsafe.

Quoted 5 lines
kernel/events/aux_kernel_test.c new file +5 -0
+			perf_event_release_aux(event);
+			perf_event_release_kernel(event);
+			return;
+		}
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#7 lore raw

RFC PATCH 4/5 selftests/perf_events: add userspace AUX regression test

In reply to Kunwu Chan

From: Lian Wang <[email protected]>

Add a standalone C program that opens a perf event, mmaps the metadata and data ring, sets up the AUX area via a second mmap, enables tracing, reads aux_head from the mmap page, and tears down the mapping. This verifies the kernel-side aux_kernel_count changes do not break the existing userspace AUX mmap protocol.

When the AUX area mmap fails (e.g. PMU does not support it), the AUX-specific tests are skipped and only a basic software-event test runs.

---
 tools/testing/selftests/perf_events/Makefile  |   1 +
 .../perf_events/aux_kernel_usermode.c         | 546 ++++++++++++++++++
 tools/testing/selftests/perf_events/config    |   2 +
 3 files changed, 549 insertions(+)
 create mode 100644 tools/testing/selftests/perf_events/aux_kernel_usermode.c
tools/testing/selftests/perf_events/Makefile +1 -0
index 2e5d85770dfe..fbafbd43f097 100644
@@ -3,4 +3,5 @@ CFLAGS += -Wl,-no-as-needed -Wall $(KHDR_INCLUDES)
 LDFLAGS += -lpthread
 
 TEST_GEN_PROGS := sigtrap_threads remove_on_exec watermark_signal mmap
+TEST_GEN_PROGS += aux_kernel_usermode
 include ../lib.mk
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +546 -0

546 lines not on this page · raw message

tools/testing/selftests/perf_events/config +2 -0
index ba58ff2203e4..2508f331cfe0 100644
@@ -1 +1,3 @@
 CONFIG_PERF_EVENTS=y
+CONFIG_PERF_AUX_KERNEL_KUNIT_TEST=y
+CONFIG_KUNIT_DEBUGFS=y
Signature · 1 line
2.43.0
#8 lore raw

Re: [RFC PATCH 4/5] selftests/perf_events: add userspace AUX regression test

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Multiple test functions have fundamental logic flaws that cause them to vacuously pass or…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Multiple test functions have fundamental logic flaws that cause them to vacuously pass or unconditionally skip, failing to validate the AUX buffer behavior they are intended to test. --

--- Patch [4]: [RFC PATCH 4/5] selftests/perf_events: add userspace AUX regression test --- commit d0e63b73f16bd8ebf0771524fc82ef084c85ea92
Author: Lian Wang <[email protected]>

selftests/perf_events: add userspace AUX regression test

This commit adds a standalone C program to test the perf event AUX userspace mmap protocol. It opens a perf event, mmaps the metadata/data rings, and sets up the AUX area to ensure kernel-side changes do not break existing behavior.

Quoted 5 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
new file mode 100644
index 0000000000000..c3d2323d08797

[ ... ]

tools/testing/selftests/perf_events/aux_kernel_usermode.c static int test_aux_mmap_independence(int pmu_type) · line 277

Quoted 12 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
@@ -267,23 +267,23 @@ static int test_aux_mmap_independence(int pmu_type)
 	mp1 = mmap(NULL, aux_size + getpagesize(), PROT_READ | PROT_WRITE,
 		   MAP_SHARED, fd1, 0);
 	if (mp1 == MAP_FAILED) {
 		FAIL("AUX independence: first mmap failed (%s)", strerror(errno));
 		close(fd1);
 		return 1;
 	}
 	PASS("AUX independence: first event mmap OK");
 
 	/* Open a second event for the same PMU */
 	fd2 = perf_event_open(&attr, 0, -1, -1, 0);

[Severity: Medium]
Does test_aux_mmap_independence() actually allocate an AUX buffer? This code only mmaps the standard perf metadata/data ring. It appears to omit the setup and second mmap() calls required to actually allocate an AUX buffer for either file descriptor.

[ ... ]

Quoted 6 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
@@ -359,20 +359,20 @@ static int test_mmap_page_fields(int pmu_type)
 	if (mp->compat_version != 0) {
 		FAIL("mmap fields: compat_version=%u (expected 0)",
 		     mp->compat_version);
 		munmap(mp, total_size + getpagesize());
 		close(fd);

tools/testing/selftests/perf_events/aux_kernel_usermode.c static int test_mmap_page_fields(int pmu_type) · line 373

Quoted 10 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
 		return 1;
 	}
 	PASS("mmap fields: compat_version=%u", mp->compat_version);
 
 	if (mp->aux_offset == 0) {
 		printf("  [SKIP] mmap fields: aux_offset is 0 (no AUX area)\n");
 		munmap(mp, total_size + getpagesize());
 		close(fd);
 		return 0;
 	}

[Severity: Medium]
Does this cause test_mmap_page_fields() to unconditionally skip? The aux_offset is a user-space defined field, but this checks it immediately after the primary mmap() when the kernel has zero-initialized the page.

[ ... ]

Quoted 4 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
@@ -411,20 +411,20 @@ static int test_aux_head_monotonic(int pmu_type)
 
 	attr.type = pmu_type;
 	attr.size = sizeof(attr);

tools/testing/selftests/perf_events/aux_kernel_usermode.c static int test_aux_head_monotonic(int pmu_type) · line 423

Quoted 10 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
 	attr.disabled = 1;
 	attr.sample_period = 256;
 	attr.freq = 0;
 
 	fd = perf_event_open(&attr, 0, -1, -1, 0);
 	if (fd < 0) {
 		/* Some PMUs may reject the default config */
 		PASS("AUX head monotonic: skipped (PMU rejected config)");
 		return 0;
 	}

[Severity: Medium]
Should this use a skip or fail mechanism instead? Returning PASS on a failure to open the event could mask regressions and inflate the pass rate.

[ ... ]

Quoted 7 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
@@ -443,20 +443,20 @@ static int test_aux_head_monotonic(int pmu_type)
 	if (aux_base == MAP_FAILED) {
 		printf("  [SKIP] AUX head monotonic: AUX area mmap failed (%s)\n",
 		       strerror(errno));
 		munmap(mp, mmap_size);
 		close(fd);
 		return 0;

tools/testing/selftests/perf_events/aux_kernel_usermode.c static int test_aux_head_monotonic(int pmu_type) · line 458

Quoted 10 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
 	}
 
 	ret = ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
 	if (ret < 0) {
 		PASS("AUX head monotonic: skipped (enable failed)");
 		munmap(aux_base, aux_size);
 		munmap(mp, mmap_size);
 		close(fd);
 		return 0;
 	}

[Severity: Medium]
Similarly, should this report a skip or failure instead of PASS when the event fails to enable?

[ ... ]

Quoted 10 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
@@ -453,20 +453,20 @@ static int test_aux_head_monotonic(int pmu_type)
 	}
 
 	/* Read initial head */
 	head_before = __atomic_load_n(&mp->aux_head, __ATOMIC_RELAXED);
 
 	usleep(100000); /* 100ms */
 
 	head_after = __atomic_load_n(&mp->aux_head, __ATOMIC_RELAXED);
 

tools/testing/selftests/perf_events/aux_kernel_usermode.c static int test_aux_head_monotonic(int pmu_type) · line 471

Quoted 10 lines
tools/testing/selftests/perf_events/aux_kernel_usermode.c new file +0 -0
 	ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
 
 	if (head_after >= head_before) {
 		PASS("AUX head monotonic: before=%lu after=%lu (OK)",
 		     head_before, head_after);
 	} else {
 		/* Wrapping is OK for very long runs */
 		PASS("AUX head monotonic: before=%lu after=%lu (wrapped)",
 		     head_before, head_after);
 	}

[Severity: Medium]
How does this verify that the head was monotonically increasing? Since both branches result in PASS, the test doesn't seem to assert any failure condition or ensure that AUX data was actually generated.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#9 lore raw

RFC PATCH 5/5 selftests/perf_events: add AUX kernel API selftest script

In reply to Kunwu Chan

From: Kunwu Chan <[email protected]>

Add a shell script that runs the perf_aux_kernel KUnit suite through debugfs, builds and runs the userspace regression program, and performs static-analysis checks on the source tree (memory barriers, refcount types, boundary validation, EXPORT_SYMBOL_GPL coverage).

When an AUX-capable PMU is present, the script also runs a live perf record and checks dmesg for errors.

Usage: sudo ./aux_kernel.sh [--pmu arm_spe_0]

---
 tools/testing/selftests/perf_events/Makefile  |   1 +
 .../selftests/perf_events/aux_kernel.sh       | 563 ++++++++++++++++++
 2 files changed, 564 insertions(+)
 create mode 100755 tools/testing/selftests/perf_events/aux_kernel.sh
tools/testing/selftests/perf_events/Makefile +1 -0
index fbafbd43f097..9b82c7ee1238 100644
@@ -4,4 +4,5 @@ LDFLAGS += -lpthread
 
 TEST_GEN_PROGS := sigtrap_threads remove_on_exec watermark_signal mmap
 TEST_GEN_PROGS += aux_kernel_usermode
+TEST_PROGS += aux_kernel.sh
 include ../lib.mk
tools/testing/selftests/perf_events/aux_kernel.sh new file +563 -0

563 lines not on this page · raw message

Signature · 1 line
2.43.0
#10 lore raw

Re: [RFC PATCH 5/5] selftests/perf_events: add AUX kernel API selftest script

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] Selftest unconditionally fails in installed environments by expecting kernel source files to be…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] Selftest unconditionally fails in installed environments by expecting kernel source files to be present.
- [Medium] Userspace regression test is improperly skipped when the selftest is run from an installed location.
- [Medium] Selftest hardcodes gcc for runtime compilation, breaking cross-compilation environments.
- [Medium] Insecure predictable temporary directory name in /tmp allows local arbitrary file overwrite. --

--- Patch [5]: [RFC PATCH 5/5] selftests/perf_events: add AUX kernel API selftest script --- commit 3dec31f6b5ec09fc257a6fee2e10670ad1c14b3c
Author: Kunwu Chan <[email protected]>

selftests/perf_events: add AUX kernel API selftest script

Add a shell script that runs the perf_aux_kernel KUnit suite through debugfs, builds and runs the userspace regression program, and performs static-analysis checks on the source tree.

Quoted 7 lines
tools/testing/selftests/perf_events/aux_kernel.sh new file +1 -0
new file mode 100755
index 0000000000000..431f59ef02e7d
@@ -0,0 +1,563 @@
+#!/bin/bash

[ ... ]

tools/testing/selftests/perf_events/aux_kernel.sh line 38

Quoted 3 lines
tools/testing/selftests/perf_events/aux_kernel.sh new file +3 -0
+RESULTS_DIR="/tmp/perf_aux_kernel_test_$$"
+mkdir -p "$RESULTS_DIR"
+exec > >(tee "$RESULTS_DIR/output.log") 2>&1

[Severity: Medium]
Does this predictable temporary directory name create a local file overwrite vulnerability?

Since this selftest might be executed as root, a local unprivileged user could pre-create a symlink with the target PID pointing to any system file (like /etc/shadow). The tee command would then follow the symlink and overwrite the target file.

[ ... ]

tools/testing/selftests/perf_events/aux_kernel.sh line 99

Quoted 8 lines
tools/testing/selftests/perf_events/aux_kernel.sh new file +8 -0
+SRC_EVENTS="$ROOT/kernel/events"
+for f in core.c ring_buffer.c internal.h; do
+	if [[ -f "$SRC_EVENTS/$f" ]]; then
+		pass "Source file: kernel/events/$f"
+	else
+		fail "Source file: kernel/events/$f missing"
+	fi
+done

[Severity: Medium]
Will this unconditionally fail in installed selftest environments?

When tests are run via make install on target CI systems, the kernel source tree is typically not present. Checking for source files in kernel/events/ and invoking fail() will cause false positive CI failures.

[ ... ]

tools/testing/selftests/perf_events/aux_kernel.sh line 235

Quoted 6 lines
tools/testing/selftests/perf_events/aux_kernel.sh new file +6 -0
+# Build and run the C regression test program
+UMODE_SRC="$SCRIPT_DIR/aux_kernel_usermode.c"
+UMODE_BIN="$SCRIPT_DIR/aux_kernel_usermode"
+
+if [[ -f "$UMODE_SRC" ]]; then
+	if [[ ! -x "$UMODE_BIN" ]] || [[ "$UMODE_SRC" -nt "$UMODE_BIN" ]]; then

[Severity: Medium]
Will this skip the userspace regression test when run from an installed location?

Because source files are not installed by TEST_GEN_PROGS during make install, the source file won't exist on the target system. This conditional causes the script to skip executing the already compiled binary, reducing test coverage in CI pipelines.

tools/testing/selftests/perf_events/aux_kernel.sh line 237

Quoted 2 lines
tools/testing/selftests/perf_events/aux_kernel.sh new file +2 -0
+		echo "  Building aux_kernel_usermode..."
+		gcc -o "$UMODE_BIN" "$UMODE_SRC" -Wall -Wextra -O2 2>&1 | \

[Severity: Medium]
Does hardcoding gcc break cross-compilation environments?

If the binary is missing and the script attempts to compile it dynamically, using gcc ignores the CC and CROSS_COMPILE environment variables set by the kernel build system. This can lead to compilation failures or building for the wrong architecture.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5