Skip to contentSkip to conversation

mm/damon/perf: observability framework for hardware-sampled access reports

RFC PATCH New Kunwu Chan 1 participant 8 messages Aug 18

Subsystems (6)
lore
Share & tools
lore

Kunwu Chan

author
#0 lore raw

cover mm/damon/perf: observability framework for hardware-sampled access reports

From: Kunwu Chan <[email protected]>

This series adds a unified observability framework to DAMON's hardware-sampled access report pipeline. When a PMU backend delivers zero callbacks, the framework pinpoints the failing stage — was the event not created? not enabled? did the hardware produce no samples? did the ring drop them? did the drain fail to match? — without a single printk. Every PMU backend (ARM SPE, AMD IBS, Intel PEBS, …) funnels through the same damon_perf_observe_*() API, which fans out to per-CPU pipeline counters, debugfs statistics, and tracepoints.

The framework does not contain any PMU driver. It is pure observability infrastructure that sits alongside the existing DAMON software statistics (scheme stats, kdamond stats) and complements them for the hardware-sampling path.

Why this framework exists
-------------------------
The framework was built to answer a concrete question during ARM SPE
bring-up: what prevents SPE from delivering samples to DAMON? The answer could be anywhere in the pipeline — the event wasn't created, the PMU wasn't enabled, the hardware produced no samples, the ring dropped them, or the drain failed to match — and without per-stage visibility, the only way to find out was to insert printks across the entire pipeline.

The framework instruments every stage with zero-overhead counters (when CONFIG_DAMON_PERF_OBSERVE=n) and optional tracepoints, so the failing stage is revealed at a glance. It immediately diagnosed the ARM SPE gap: Layers 1-3 (event create/bind/enable) passed, but the PMU was not producing data — a PMU-level gap, not a DAMON pipeline bug. This confirmed that SPE required an AUX buffer backend, which does not fit the perf_event_create_kernel_counter() API.

During the subsequent ARM SPE AUX backend development [2] (which builds on the perf AUX kernel-consumer API [3]), the framework was used to verify each pipeline fix. When the AUX backend was complete, the same framework confirmed a fully functional pipeline: Layers 1-7 all pass, real SPE hardware samples flow through the ring, match, and update stages. The before/after results are shown in the Testing section.

We are posting the framework now so that other PMU backends can reuse the same infrastructure instead of re-deriving it.

How to use
----------
Compile with CONFIG_DAMON_PERF_OBSERVE=y. After starting a kdamond with a perf event, the pipeline is visible at a glance:

  # cat /sys/kernel/debug/damon/perf_stats
      --------------  ----------
      Counter          Value
      --------------  ----------
      callback          233
      valid             233
      ...
      enqueue           233
      dequeue           233
      match              12
      update              9

For automated diagnosis, run the selftest:

  # sudo ./damon_perf_obs_test.sh --pmu software --freq 1 --sample-freq 100

For structured diagnostics, enable the tracepoints under /sys/kernel/debug/tracing/events/damon/.

What the series adds
--------------------
Patch 1 defines the three tracepoints (damon_perf_sample with 8 fields including sample_flags, sample_type, and execution context, plus damon_perf_report_missed and damon_perf_drain) with the CONFIG switch, the observe API declarations, no-op stubs, and the Kconfig / Makefile wiring. (Reviewers: tracepoint folks — Steven, Masami, Mathieu.)

Patch 2 implements the observe_*() core: per-CPU monotonic counters, event-lifecycle hooks with lazily-allocated per-event per-CPU state, NMI-safe sample classification with execution-context detection, ring enqueue/dequeue/peak, match/miss/update, and framework init.

Patch 3 adds the debugfs perf_stats interface (debug-only, format unstable).

Patch 4 wires the observe calls into the vaddr overflow handlers and the kdamond drain, integrating the framework into the hot path. Reads ring->tail once with READ_ONCE and reuses the cached value for the peak-occupancy estimate, avoiding a compiler reload and a torn read on the NMI producer side.

Patch 5 adds the automated selftest with snapshot/delta accounting, per-CPU state-column checks, clean-session guard, and a PMU support verdict (FULLY INTEGRATED / PLUMBING-ONLY / UNUSABLE). (Reviewers: kselftest folks — Shuah.)

Patch 6 documents the framework.

Patch 7 adds CONFIG_DAMON_PERF_DEBUG as an optional Kconfig option for verbose pr_debug() output via damon_perf_dbg(), wired into every event-lifecycle hook, the drain summary, and the init paths. Adds a pipeline health check to the selftest that diagnoses which stage is broken when callbacks are zero.

Testing
=======
Tested on Kunpeng 920 (256 CPUs, ARM SPE, kernel 7.1.0-rc5-mm-new-damon+).

KUnit: 35/35 PASSED (damon 29, damon-operations 5, damon-sysfs 1).

The framework is PMU-agnostic. Three configurations demonstrate this, with the ARM SPE case being the framework's core diagnostic value:

**Software PMU** (page-fault sampling, exercises the full pipeline, no hardware required):

  $ sudo ./damon_perf_obs_test.sh --pmu software --freq 1 --sample-freq 100
  SUMMARY: 27 passed, 0 failed, 0 skipped (FULLY INTEGRATED)
  callback=170, valid=170, enqueue=170, dequeue=170, match=6, update=3

**CPU-clock** (address-less PMU, plumbing-only):

  $ sudo ./damon_perf_obs_test.sh --pmu software --config 0 --freq 1 --sample-freq 100
  SUMMARY: 22 passed, 0 failed, 3 skipped (PLUMBING-ONLY)
  callback=1913, valid=0, addr_zero=1913, enqueue=0
  Layers 1-4 pass; Ring/Match/Drain skipped as expected.

**ARM SPE** (hardware trace). This is the scenario that motivated the
framework: without an AUX buffer backend, SPE cannot deliver samples through perf_event_create_kernel_counter(), and the framework pinpoints the failure without a single printk:

  $ sudo ./damon_perf_obs_test.sh --pmu arm_spe_0 --freq 0 --period 256

--- Layer 2-3: Enable & Run (via per-CPU state) ---
[PASS] Event Created (max per-CPU state >= CREATED) [PASS] Event Bound (max per-CPU state >= BOUND) [PASS] Event Enabled (max per-CPU state >= ENABLED) [PASS] Perf event creation (no errors in dmesg delta)

--- Analysis ---
Pipeline diagnosis: PMU not producing data or AUX pipeline broken [FAIL] Sampling — 0 callbacks — PMU is not delivering samples to DAMON [SKIP] Event Running — no callbacks — state cannot advance past ENABLED
Ring: enqueue=0 dequeue=0 overflow=0

--- PMU support verdict ---
UNUSABLE: the PMU never delivered a sample to DAMON

SUMMARY: 19 passed, 1 failed, 5 skipped

Layers 1-3 pass — the per-CPU state advances to ENABLED — so the kernel-side plumbing is intact. The health check confirms state >= ENABLED but enqueue=0, ruling out event-creation and enable bugs. The failure is isolated to Layer 4 (Sampling): the PMU itself is not producing data. This is the expected result for ARM SPE without an AUX buffer backend, and the framework diagnoses it as a PMU-level gap rather than a DAMON pipeline bug.

After the AUX backend is added [2] (which itself depends on the perf AUX kernel-consumer API [3]), the same test script confirms a fully functional pipeline, including real SPE hardware samples:

  $ sudo ./damon_perf_obs_test.sh --pmu arm_spe_0 --freq 0 --period 256
  SUMMARY: 27 passed, 0 failed, 0 skipped (FULLY INTEGRATED)
  callback=47248, valid=47248, enqueue=15161, dequeue=15161,
  overflow=32087, match=255, update=4
  All 7 pipeline stages pass.  The AUX drain path delivers samples in
  process context (context=0), and the ring overflow counter (32087)
  shows the ring is sized for software PMU rates — a real-world
  observation the framework provides without any additional
  instrumentation.

Known limitations
=================
- The debugfs perf_stats format is explicitly unstable and must not be used by scripts. Tracepoints and counters are the stable diagnostic interface.
- The global per-CPU lifecycle state is monotonic and never reset, so a previous RUNNING event can mask a current event's failure. Per-event per-CPU state is available for accurate tracking.
- This series depends on Ravi's hardware-sampled access reports branch [1] for the following facilities:
* Per-CPU SPSC report ring (damon_report_rings, DAMON_REPORT_RING_MASK)
* damon_report_access() and damon_report_page_fault() as the ring producer
* kdamond_check_reported_accesses() as the drain path
* damon_perf_event lifecycle (create/bind/enable/disable/free)
Without [1] this series cannot compile, so [1] must be merged first.

References
==========
Link [1]: https://github.com/damonitor/linux.git

          branch: ravi_hw_sampled_access_reports_rfc_v1
          commit: fd968106e5bf

Link [2]: https://lore.kernel.org/all/[email protected]/
Link [3]: https://lore.kernel.org/all/[email protected]/

Base: fd968106e5bf ("mm/damon: add damos_node_eligible_mem_bp tracepoint")

Kunwu Chan (4):
mm/damon/perf: add observability framework with tracepoints and CONFIG
switch
mm/damon/perf: implement observe API and per-CPU statistics engine mm/damon: integrate observe API into vaddr overflow handlers and core
drain
selftests/damon: add automated layer-by-layer observability test

Lian Wang (3):
mm/damon/perf: add debugfs statistics interface Docs/mm/damon: document the perf observability framework mm/damon/perf: add CONFIG_DAMON_PERF_DEBUG and pipeline health check

 Documentation/admin-guide/mm/damon/index.rst  |   1 +
 .../mm/damon/perf-observability.rst           | 210 +++++++
 include/linux/damon.h                         |  44 ++
 include/trace/events/damon.h                  | 120 ++++
 mm/damon/Kconfig                              |  33 +
 mm/damon/Makefile                             |   1 +
 mm/damon/core.c                               | 129 +++-
 mm/damon/perf/Makefile                        |   5 +
 mm/damon/perf/debugfs.c                       | 145 +++++
 mm/damon/perf/perf.h                          | 235 +++++++
 mm/damon/perf/stats.c                         | 308 ++++++++++
 mm/damon/vaddr.c                              | 104 +++-
 tools/testing/selftests/damon/Makefile        |   1 +
 .../selftests/damon/damon_perf_obs_test.sh    | 578 ++++++++++++++++++
 14 files changed, 1880 insertions(+), 34 deletions(-)
 create mode 100644 Documentation/admin-guide/mm/damon/perf-observability.rst
 create mode 100644 mm/damon/perf/Makefile
 create mode 100644 mm/damon/perf/debugfs.c
 create mode 100644 mm/damon/perf/perf.h
 create mode 100644 mm/damon/perf/stats.c
 create mode 100755 tools/testing/selftests/damon/damon_perf_obs_test.sh
Signature · 1 line
2.43.0
author
#1 lore raw

RFC PATCH 1/7 mm/damon/perf: add observability framework with tracepoints and CONFIG switch

From: Kunwu Chan <[email protected]>

Add the observe framework headers together with the compile-time switch that gates the whole feature: the three DAMON perf tracepoints this series adds (guarded with CONFIG_DAMON_PERF_OBSERVE so they are not registered when the switch is off; damon_perf_ring_overflow is provided by the base series), the observe API declarations with static-inline no-ops for disabled builds, the access-report contract (miss-reason enum, report-source enum, report source field, per-event cpu_state member), and the Kconfig and Makefile wiring.

The sample tracepoint carries what the PMU actually populated (data->sample_flags), what was requested (perf_event->attr. sample_type), and the execution context (process/softirq/hardirq/NMI) in a single line, so PMU support gaps and context expectations (e.g. IBS overflow in NMI, SPE AUX drain in process context) are verifiable at a glance.

Every following commit in this series builds with CONFIG_DAMON_PERF_OBSERVE both enabled and disabled.

---
 include/linux/damon.h        |  44 +++++++
 include/trace/events/damon.h | 120 ++++++++++++++++++
 mm/damon/Kconfig             |  17 +++
 mm/damon/Makefile            |   1 +
 mm/damon/perf/Makefile       |   3 +
 mm/damon/perf/perf.h         | 228 +++++++++++++++++++++++++++++++++++
 6 files changed, 413 insertions(+)
 create mode 100644 mm/damon/perf/Makefile
 create mode 100644 mm/damon/perf/perf.h
include/linux/damon.h +44 -0
index 11f1c1071b9b..c191c065b0e4 100644
@@ -116,6 +116,19 @@ struct damon_target {
 	bool obsolete;
 };
 
+/**
+ * enum damon_report_source - Tells which subsystem produced an access report.
+ *
+ * Ring and matching counters aggregate all sources; this enum lets callers
+ * tag reports so that tracepoints and future per-source breakdowns can
+ * distinguish NMI overflow-handler samples from
+ * page-fault hints.
+ */
+enum damon_report_source {
+	DAMON_REPORT_SRC_PERF_OVERFLOW = 0,  /* overflow_handler (IBS, PEBS) */
+	DAMON_REPORT_SRC_PAGE_FAULT,         /* damon_report_page_fault() */
+};
+
 /**
  * struct damon_access_report - Represent single access report information.
  * @paddr:		Start physical address of the accessed address range.
@@ -125,6 +138,8 @@ struct damon_target {
  * @tid:		The task id of the task that made the access.
  * @tgid:		Thread group id of the task that made the access.
  * @is_write:		Whether the access is write.
+ * @source:		Which subsystem produced this report
+ *			(enum damon_report_source).
  *
  * Any DAMON API callers that notified access events can report the information
  * to DAMON using damon_report_access().  This struct contains the reporting
@@ -138,10 +153,28 @@ struct damon_access_report {
 	pid_t tid;
 	pid_t tgid;
 	bool is_write;
+#ifdef CONFIG_DAMON_PERF_OBSERVE
+	int source;
+#endif /* CONFIG_DAMON_PERF_OBSERVE */
 /* private: */
 	unsigned long report_jiffies;	/* when this report is made */
 };
 
+/*
+ * Reason codes for trace_damon_perf_report_missed.
+ *
+ * DAMON_REPORT_MISS_TGID:       tgid mismatch (pid-based monitoring,
+ *                               missed at drain-loop level before the
+ *                               per-target iteration).
+ * DAMON_REPORT_MISS_NOREGION:   binary search found no containing region.
+ * DAMON_REPORT_MISS_BOUNDARY:   address + size straddles region boundary.
+ */
+enum damon_report_miss_reason {
+	DAMON_REPORT_MISS_TGID = 1,
+	DAMON_REPORT_MISS_NOREGION = 2,
+	DAMON_REPORT_MISS_BOUNDARY = 3,
+};
+
 /**
  * enum damos_action - Represents an action of a Data Access Monitoring-based
  * Operation Scheme.
@@ -1027,6 +1060,17 @@ struct damon_perf_event {
 	struct hlist_node hlist_node;
 	bool init_complete;
 	bool any_cpu_failed;
+#ifdef CONFIG_DAMON_PERF_OBSERVE
+	/*
+	 * Per-CPU lifecycle state (enum damon_perf_event_state).
+	 * Allocated lazily on the first observe_event_created(),
+	 * freed on observe_event_destroyed().  Each event tracks
+	 * its own progression through CREATED->BOUND->ENABLED,
+	 * so destroying one event does not overwrite another"s
+	 * state on the same CPU.
+	 */
+	int __percpu *cpu_state;
+#endif /* CONFIG_DAMON_PERF_OBSERVE */
 	struct damon_ctx *ctx;
 };
 
include/trace/events/damon.h +120 -0

126 lines not on this page · raw message

mm/damon/Kconfig +17 -0
index ad629f0f31d8..9f811510760f 100644
@@ -131,4 +131,21 @@ config DAMON_ACMA
 	  min/max memory for the system and maximum memory pressure stall time
 	  ratio.
 
+config DAMON_PERF_OBSERVE
+	bool "DAMON perf event observability framework"
+	depends on DAMON
+	depends on PERF_EVENTS
+	depends on DEBUG_FS
+	default n
+	help
+	  Enable per-CPU pipeline counters, tracepoints, and a
+	  debug-only debugfs perf_stats file for DAMON
+	  hardware-sampled access reports.  The debugfs format is
+	  unstable and must not be used by scripts; counters and
+	  tracepoints are the diagnostic interface.
+
+	  When disabled, all observe functions are compiled to
+	  static-inline no-ops with zero runtime overhead.
+
+	  If unsure, say N.
 endmenu
mm/damon/Makefile +1 -0
index 22494754f41e..04da39a9f56c 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_DAMON_RECLAIM)	+= modules-common.o reclaim.o
 obj-$(CONFIG_DAMON_LRU_SORT)	+= modules-common.o lru_sort.o
 obj-$(CONFIG_DAMON_STAT)	+= modules-common.o stat.o
 obj-$(CONFIG_DAMON_ACMA)	+= modules-common.o acma.o
+obj-$(CONFIG_DAMON)		+= perf/
mm/damon/perf/Makefile new file +3 -0
new file mode 100644
index 000000000000..cc0d4f1d1d28
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+
+# Observability: per-CPU counters, tracepoints, debugfs perf_stats
mm/damon/perf/perf.h new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#2 lore raw

RFC PATCH 2/7 mm/damon/perf: implement observe API and per-CPU statistics engine

In reply to Kunwu Chan

From: Kunwu Chan <[email protected]>

Implement the observe_*() core: per-CPU monotonic counters, the event-lifecycle hooks with lazily-allocated per-event per-CPU state alongside the global per-CPU stage, NMI-safe sample classification, ring enqueue/dequeue/peak, match/miss/update, and framework init. The global per-CPU stage advances monotonically so a late create/bind/ enable cannot regress a CPU that is already RUNNING.

damon_perf_observe_sample() records the execution context (process/softirq/hardirq/NMI) so overflow callbacks can be verified to fire in the expected context.

---
 mm/damon/perf/Makefile |   2 +
 mm/damon/perf/stats.c  | 295 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 297 insertions(+)
 create mode 100644 mm/damon/perf/stats.c
mm/damon/perf/Makefile +2 -0
index cc0d4f1d1d28..5c46d3da7ef8 100644
@@ -1,3 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 
 # Observability: per-CPU counters, tracepoints, debugfs perf_stats
+obj-$(CONFIG_DAMON_PERF_OBSERVE)	+= damon-perf.o
+damon-perf-objs			:= stats.o
mm/damon/perf/stats.c new file +295 -0

295 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#3 lore raw

RFC PATCH 3/7 mm/damon/perf: add debugfs statistics interface

In reply to Kunwu Chan

From: Lian Wang <[email protected]>

Expose the aggregated per-CPU counters and the global event-stage in a debug-only debugfs perf_stats file. The format is explicitly unstable; tracepoints are the stable diagnostic interface.

---
 mm/damon/perf/Makefile  |   2 +-
 mm/damon/perf/debugfs.c | 142 ++++++++++++++++++++++++++++++++++++++++
 mm/damon/perf/stats.c   |   2 +-
 3 files changed, 144 insertions(+), 2 deletions(-)
 create mode 100644 mm/damon/perf/debugfs.c
mm/damon/perf/Makefile +1 -1
index 5c46d3da7ef8..150cbaa875fa 100644
@@ -2,4 +2,4 @@
 
 # Observability: per-CPU counters, tracepoints, debugfs perf_stats
 obj-$(CONFIG_DAMON_PERF_OBSERVE)	+= damon-perf.o
-damon-perf-objs			:= stats.o
+damon-perf-objs			:= stats.o debugfs.o
mm/damon/perf/debugfs.c new file +142 -0

142 lines not on this page · raw message

mm/damon/perf/stats.c +1 -1
index a869f115bb26..ae5b0037a31d 100644
@@ -291,5 +291,5 @@ void damon_perf_stats_aggregate(struct damon_perf_stats *dst)
 
 int damon_perf_framework_init(void)
 {
-	return 0;
+	return damon_perf_debugfs_init();
 }
Signature · 1 line
2.43.0
author
#4 lore raw

RFC PATCH 4/7 mm/damon: integrate observe API into vaddr overflow handlers and core drain

In reply to Kunwu Chan

From: Kunwu Chan <[email protected]>

Wire the observe_*() calls into the DAMON hot paths: vaddr access-check overflow handlers report into the per-CPU ring, and the kdamond drain matches each report against the target whose tgid owns it. The observe calls are pure side-effect statistics (no-ops under CONFIG_DAMON_PERF_OBSERVE=n), so the switch never changes DAMON matching semantics. The vaddr teardown frees the per-event cpu_state array.

Read ring->tail once with READ_ONCE in damon_report_access() and reuse the cached value for the peak-occupancy estimate, avoiding a torn read and a compiler reload on the producer side.

---
 mm/damon/core.c  | 129 +++++++++++++++++++++++++++++++++++++----------
 mm/damon/vaddr.c | 104 +++++++++++++++++++++++++++++++++++---
 2 files changed, 199 insertions(+), 34 deletions(-)
mm/damon/core.c +102 -27
index 609d627e2b33..377f07122fb0 100644
@@ -21,6 +21,7 @@
 
 /* for damon_get_folio() used by node eligible memory metrics */
 #include "ops-common.h"
+#include "perf/perf.h"
 
 #define CREATE_TRACE_POINTS
 #include <trace/events/damon.h>
@@ -2243,24 +2244,41 @@ void damon_report_access(struct damon_access_report *report)
 	preempt_disable();
 	if (local_inc_return(this_cpu_ptr(&damon_report_ring_busy)) != 1) {
 		/* NMI nested on a process-context producer; drop. */
-		trace_damon_perf_ring_overflow(smp_processor_id());
+#ifdef CONFIG_DAMON_PERF_OBSERVE
+		damon_perf_observe_ring_overflow(smp_processor_id());
+#endif /* CONFIG_DAMON_PERF_OBSERVE */
 		goto out;
 	}
 
 	ring = this_cpu_ptr(&damon_report_rings);
 	head = ring->head;
 	next = (head + 1) & DAMON_REPORT_RING_MASK;
+	{
+		unsigned int tail = READ_ONCE(ring->tail);
 
-	if (next == READ_ONCE(ring->tail)) {
-		trace_damon_perf_ring_overflow(smp_processor_id());
-		goto out;
-	}
+		if (next == tail) {
+#ifdef CONFIG_DAMON_PERF_OBSERVE
+			damon_perf_observe_ring_overflow(smp_processor_id());
+#endif /* CONFIG_DAMON_PERF_OBSERVE */
+			goto out;
+		}
 
-	ring->entries[head] = *report;
-	ring->entries[head].report_jiffies = jiffies;
-	smp_wmb(); /* publish entry before head advance */
-	WRITE_ONCE(ring->head, next);
-	WRITE_ONCE(*this_cpu_ptr(&damon_ring_pending), 1);
+		ring->entries[head] = *report;
+		ring->entries[head].report_jiffies = jiffies;
+		smp_wmb(); /* publish entry before head advance */
+		WRITE_ONCE(ring->head, next);
+		WRITE_ONCE(*this_cpu_ptr(&damon_ring_pending), 1);
+#ifdef CONFIG_DAMON_PERF_OBSERVE
+		damon_perf_observe_ring_enqueue();
+		/*
+		 * Track peak occupancy for health evaluation.
+		 * next is the new head; tail was read before enqueue
+		 * (may be slightly stale — acceptable for a peak estimate).
+		 */
+		damon_perf_observe_ring_peak(
+				(next - tail) & DAMON_REPORT_RING_MASK);
+#endif /* CONFIG_DAMON_PERF_OBSERVE */
+	}
 out:
 	local_dec(this_cpu_ptr(&damon_report_ring_busy));
 	preempt_enable();
@@ -2276,6 +2294,9 @@ void damon_report_page_fault(struct vm_fault *vmf, bool huge_pmd)
 		.tid = current->pid,
 		.tgid = task_tgid_nr(current),
 		.is_write = vmf->flags & FAULT_FLAG_WRITE,
+#ifdef CONFIG_DAMON_PERF_OBSERVE
+		.source = DAMON_REPORT_SRC_PAGE_FAULT,
+#endif /* CONFIG_DAMON_PERF_OBSERVE */
 	};
 
 	if (huge_pmd)
@@ -3917,8 +3938,16 @@ static bool damon_sample_filter_out(struct damon_access_report *report,
 	return !filter->allow;
 }
 
-static void kdamond_apply_access_report(struct damon_access_report *report,
-		struct damon_target *t,
+/*
+ * Try to apply one access report to a target's region snapshot.
+ *
+ * Caller has already resolved tgid (for pid-based monitoring), so this
+ * function only does address-to-region matching.  Miss reasons for
+ * trace_damon_perf_report_missed use enum damon_report_miss_reason.
+ *
+ * Return: true if the report fell inside a known region, false otherwise.
+ */
+static bool kdamond_apply_access_report(struct damon_access_report *report,
 		struct damon_region **regions, unsigned int nr_regions,
 		struct damon_ctx *ctx)
 {
@@ -3926,13 +3955,7 @@ static void kdamond_apply_access_report(struct damon_access_report *report,
 	unsigned long addr;
 	int left, right, mid;
 
-	if (damon_target_has_pid(ctx)) {
-		if (pid_nr(t->pid) != report->tgid)
-			return;
-		addr = report->vaddr;
-	} else {
-		addr = report->paddr;
-	}
+	addr = damon_target_has_pid(ctx) ? report->vaddr : report->paddr;
 
 	/* Binary search the snapshot for the region containing addr. */
 	left = 0;
@@ -3951,17 +3974,27 @@ static void kdamond_apply_access_report(struct damon_access_report *report,
 		}
 	}
 
-	if (!r)
-		return;
+	if (!r) {
+		damon_perf_observe_miss(addr, report->cpu,
+				DAMON_REPORT_MISS_NOREGION);
+		return false;
+	}
 	/* Reject reports straddling a region boundary. */
-	if (addr + report->size > r->ar.end)
-		return;
+	if (addr + report->size > r->ar.end) {
+		damon_perf_observe_miss(addr, report->cpu,
+				DAMON_REPORT_MISS_BOUNDARY);
+		return false;
+	}
 	if (!r->access_reported) {
 		damon_update_region_access_rate(r, true, &ctx->attrs);
 		r->access_reported = true;
+		damon_perf_observe_update(report->cpu);
 	}
+	damon_perf_observe_match(addr, report->cpu);
+	return true;
 }
 
+
 static unsigned int kdamond_apply_zero_access_report(struct damon_ctx *ctx)
 {
 	struct damon_target *t;
@@ -4045,6 +4078,7 @@ static unsigned int kdamond_check_reported_accesses(struct damon_ctx *ctx)
 	struct damon_target_lookup *tbl;
 	unsigned int nr_targets = 0;
 	unsigned int i;
+	unsigned int total_reports = 0, matched_reports = 0;
 
 	tbl = damon_build_target_lookup(ctx, &nr_targets);
 	if (!tbl) {
@@ -4077,6 +4111,10 @@ static unsigned int kdamond_check_reported_accesses(struct damon_ctx *ctx)
 		while (tail != head) {
 			struct damon_access_report *report =
 				&ring->entries[tail];
+			bool applied = false;
+
+			/* Count every entry removed from the ring */
+			damon_perf_observe_ring_dequeue(report->cpu);
 
 			if (time_before(report->report_jiffies,
 					jiffies - usecs_to_jiffies(
@@ -4085,16 +4123,52 @@ static unsigned int kdamond_check_reported_accesses(struct damon_ctx *ctx)
 			if (damon_sample_filter_out(report,
 					&ctx->sample_control))
 				goto next;
-			for (i = 0; i < nr_targets; i++)
-				kdamond_apply_access_report(report,
-						tbl[i].t,
+			/*
+			 * For pid-based monitoring, resolve tgid to the
+			 * single matching target before calling
+			 * kdamond_apply_access_report(), avoiding a
+			 * spurious miss tracepoint for every non-matching
+			 * target.
+			 */
+			if (damon_target_has_pid(ctx)) {
+				for (i = 0; i < nr_targets; i++) {
+					if (pid_nr(tbl[i].t->pid) ==
+							report->tgid) {
+						applied =
+					    kdamond_apply_access_report(
+							report,
+							tbl[i].regions,
+							tbl[i].nr_regions,
+							ctx);
+						break;
+					}
+				}
+				if (!applied && i == nr_targets)
+					damon_perf_observe_miss(
+						report->vaddr,
+						report->cpu,
+						DAMON_REPORT_MISS_TGID);
+			} else {
+				for (i = 0; i < nr_targets; i++)
+					applied |=
+					    kdamond_apply_access_report(
+						report,
 						tbl[i].regions,
 						tbl[i].nr_regions, ctx);
+			}
+			total_reports++;
+			if (applied)
+				matched_reports++;
+
 next:
 			tail = (tail + 1) & DAMON_REPORT_RING_MASK;
 		}
 		WRITE_ONCE(ring->tail, tail);
 	}
+
+	if (total_reports)
+		damon_perf_observe_drain(total_reports, matched_reports);
+
 	/* For nr_accesses_bp, absence of access should also be reported. */
 	return kdamond_apply_zero_access_report(ctx);
 }
@@ -4158,8 +4232,9 @@ static int kdamond_fn(void *data)
 		ctx->passed_sample_intervals++;
 
 		if (!list_empty(&ctx->perf_events) ||
-		    ctx->sample_control.primitives_enabled.page_fault)
+		    ctx->sample_control.primitives_enabled.page_fault) {
 			max_nr_accesses = kdamond_check_reported_accesses(ctx);
+		}
 		else if (ctx->ops.check_accesses)
 			max_nr_accesses = ctx->ops.check_accesses(ctx);
 		if (ctx->ops.apply_probes)
mm/damon/vaddr.c +97 -7

205 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#5 lore raw

RFC PATCH 5/7 selftests/damon: add automated layer-by-layer observability test

In reply to Kunwu Chan

From: Kunwu Chan <[email protected]>

Drive the observe framework end-to-end with a software page-fault PMU positive control (no hardware PMU required): create a kdamond with a perf event via the DAMON sysfs interface, run a memory-pressure workload, capture a bounded trace window, and assert each pipeline layer from per-run snapshot/delta counter deltas (counters are cumulative since boot, so the snapshot is taken before the workload window): callbacks, valid data addresses, ring enqueue/dequeue/ overflow, drain match/update, the four DAMON perf tracepoints, and the per-CPU state column advancing CREATED/BOUND/ENABLED to RUNNING after the first callback.

A clean-session guard refuses to run while a kdamond already exists. Ends with a PMU support verdict (FULLY INTEGRATED / PLUMBING-ONLY / UNUSABLE). Cleanup is ownership safe: it tears down only the kdamond, workload and debugfs mount created by this invocation, and retains the raw evidence directory by default.

---
 tools/testing/selftests/damon/Makefile        |   1 +
 .../selftests/damon/damon_perf_obs_test.sh    | 562 ++++++++++++++++++
 2 files changed, 563 insertions(+)
 create mode 100755 tools/testing/selftests/damon/damon_perf_obs_test.sh
tools/testing/selftests/damon/Makefile +1 -0
index 2180c328a825..1db8fa95ba2d 100644
@@ -23,4 +23,5 @@ TEST_PROGS += sysfs_no_op_commit_break.py
 
 EXTRA_CLEAN = __pycache__
 
+TEST_PROGS += damon_perf_obs_test.sh
 include ../lib.mk
tools/testing/selftests/damon/damon_perf_obs_test.sh new file +562 -0

562 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#6 lore raw

RFC PATCH 6/7 Docs/mm/damon: document the perf observability framework

In reply to Kunwu Chan

From: Lian Wang <[email protected]>

Add documentation for the DAMON perf observability framework, covering the CONFIG_DAMON_PERF_OBSERVE Kconfig option, the debugfs perf_stats interface, the tracepoints, and the per-CPU pipeline counter model. The debugfs format is explicitly marked as unstable and must not be used by scripts.

---
 Documentation/admin-guide/mm/damon/index.rst  |   1 +
 .../mm/damon/perf-observability.rst           | 210 ++++++++++++++++++
 2 files changed, 211 insertions(+)
 create mode 100644 Documentation/admin-guide/mm/damon/perf-observability.rst
Documentation/admin-guide/mm/damon/index.rst +1 -0
index 3ce3164480c7..623a5c312b69 100644
@@ -15,3 +15,4 @@ access monitoring and access-aware system operations.
    reclaim
    lru_sort
    stat
+   perf-observability
Documentation/admin-guide/mm/damon/perf-observability.rst new file +210 -0

210 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#7 lore raw

RFC PATCH 7/7 mm/damon/perf: add CONFIG_DAMON_PERF_DEBUG and pipeline health check

In reply to Kunwu Chan

From: Lian Wang <[email protected]>

Add CONFIG_DAMON_PERF_DEBUG as an optional Kconfig option that enables pr_debug() output for the observability pipeline via the damon_perf_dbg() macro. Default off, zero overhead when disabled.

Add a pipeline health check to the observability selftest that diagnoses which stage is broken when callbacks are zero, using the existing per-CPU state and counter deltas.

---
 mm/damon/Kconfig                               | 16 ++++++++++++++++
 mm/damon/perf/debugfs.c                        |  5 ++++-
 mm/damon/perf/perf.h                           |  7 +++++++
 mm/damon/perf/stats.c                          | 15 ++++++++++++++-
 .../selftests/damon/damon_perf_obs_test.sh     | 18 +++++++++++++++++-
 5 files changed, 58 insertions(+), 3 deletions(-)
mm/damon/Kconfig +16 -0
index 9f811510760f..35ec4d12c5b8 100644
@@ -148,4 +148,20 @@ config DAMON_PERF_OBSERVE
 	  static-inline no-ops with zero runtime overhead.
 
 	  If unsure, say N.
+
+config DAMON_PERF_DEBUG
+	bool "DAMON Perf verbose debugging output"
+	depends on DAMON_PERF_OBSERVE
+	help
+	  Enable verbose per-event and per-drain pr_debug() output
+	  for the DAMON perf observability pipeline.  When enabled,
+	  key lifecycle transitions and sampling events are logged
+	  at KERN_DEBUG level, visible via dynamic_debug or when
+	  DEBUG is defined at compile time.
+
+	  This adds dmesg noise and should only be enabled for
+	  development or troubleshooting.
+
+	  If unsure, say N.
+
 endmenu
mm/damon/perf/debugfs.c +4 -1
index c54dd7644ac3..48f3d23c2dbc 100644
@@ -132,11 +132,14 @@ int damon_perf_debugfs_init(void)
 	damon_debugfs_dir = debugfs_create_dir("damon", NULL);
 	if (damon_debugfs_dir == ERR_PTR(-EEXIST))
 		damon_debugfs_dir = debugfs_lookup("damon", NULL);
-	if (IS_ERR(damon_debugfs_dir))
+	if (IS_ERR(damon_debugfs_dir)) {
+		damon_perf_dbg("debugfs init failed: %ld\n", PTR_ERR(damon_debugfs_dir));
 		return PTR_ERR(damon_debugfs_dir);
+	}
 
 	debugfs_create_file("perf_stats", 0400, damon_debugfs_dir,
 			NULL, &perf_stats_fops);
 
+	damon_perf_dbg("debugfs init ok\n");
 	return 0;
 }
mm/damon/perf/perf.h +7 -0
index 78e23d436336..908c06f2e3db 100644
@@ -18,6 +18,13 @@
 struct perf_event;
 #include <linux/types.h>
 
+#ifdef CONFIG_DAMON_PERF_DEBUG
+#define damon_perf_dbg(fmt, ...) \
+	pr_debug("damon-perf: " fmt, ##__VA_ARGS__)
+#else
+#define damon_perf_dbg(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
+#endif
+
 struct damon_perf_event;
 
 /*
mm/damon/perf/stats.c +14 -1
index ae5b0037a31d..e2c1e5764d41 100644
@@ -67,6 +67,7 @@ void damon_perf_observe_event_created(struct damon_perf_event *event, int cpu)
 			return;
 	}
 	*per_cpu_ptr(event->cpu_state, cpu) = DAMON_PERF_STATE_CREATED;
+	damon_perf_dbg("cpu %d: event created\n", cpu);
 	damon_perf_cpu_state_advance(cpu, DAMON_PERF_STATE_CREATED);
 }
 
@@ -76,6 +77,7 @@ void damon_perf_observe_event_bound(struct damon_perf_event *event,
 	if (event->cpu_state)
 		*per_cpu_ptr(event->cpu_state, cpu) = DAMON_PERF_STATE_BOUND;
 	damon_perf_cpu_state_advance(cpu, DAMON_PERF_STATE_BOUND);
+	damon_perf_dbg("cpu %d: event bound\n", cpu);
 }
 
 void damon_perf_observe_event_enabled(struct damon_perf_event *event,
@@ -84,12 +86,14 @@ void damon_perf_observe_event_enabled(struct damon_perf_event *event,
 	if (event->cpu_state)
 		*per_cpu_ptr(event->cpu_state, cpu) = DAMON_PERF_STATE_ENABLED;
 	damon_perf_cpu_state_advance(cpu, DAMON_PERF_STATE_ENABLED);
+	damon_perf_dbg("cpu %d: event enabled\n", cpu);
 }
 
 void damon_perf_observe_event_disabled(struct damon_perf_event *event,
 		int cpu, int state)
 {
 	/* State unchanged: the event may be re-enabled later. */
+	damon_perf_dbg("cpu %d: event disabled\n", cpu);
 }
 
 void damon_perf_observe_event_destroyed(struct damon_perf_event *event, int cpu)
@@ -104,6 +108,7 @@ void damon_perf_observe_event_destroyed(struct damon_perf_event *event, int cpu)
 	 */
 	if (event->cpu_state)
 		*per_cpu_ptr(event->cpu_state, cpu) = DAMON_PERF_STATE_UNINIT;
+	damon_perf_dbg("cpu %d: event destroyed\n", cpu);
 }
 
 void damon_perf_observe_event_free(struct damon_perf_event *event)
@@ -111,6 +116,7 @@ void damon_perf_observe_event_free(struct damon_perf_event *event)
 	if (event->cpu_state) {
 		free_percpu(event->cpu_state);
 		event->cpu_state = NULL;
+		damon_perf_dbg("event freed\n");
 	}
 }
 
@@ -231,6 +237,7 @@ void damon_perf_observe_drain(unsigned int total, unsigned int matched)
 {
 	if (trace_damon_perf_drain_enabled())
 		trace_damon_perf_drain(total, matched);
+	damon_perf_dbg("drain: total=%u matched=%u\n", total, matched);
 }
 
 /*
@@ -291,5 +298,11 @@ void damon_perf_stats_aggregate(struct damon_perf_stats *dst)
 
 int damon_perf_framework_init(void)
 {
-	return damon_perf_debugfs_init();
+	int ret = damon_perf_debugfs_init();
+
+	if (ret)
+		damon_perf_dbg("framework init failed: %d\n", ret);
+	else
+		damon_perf_dbg("framework init ok\n");
+	return ret;
 }
tools/testing/selftests/damon/damon_perf_obs_test.sh +17 -1
index 4c4074cdd191..cd567c151ae6 100755
@@ -344,7 +344,7 @@ max_cpu_state() {
 	} END { print max+0 }' "$1" 2>/dev/null
 }
 
-CPU_ST_BASE=$(max_cpu_state "$STATS_BASE")
+CPU_ST_BASE=$(max_cpu_state /sys/kernel/debug/damon/perf_stats)
 
 if [[ "$CPU_ST_BASE" -ge 1 ]]; then
 	pass "Event Created (max per-CPU state >= CREATED)"
@@ -431,6 +431,22 @@ VALID=$(delta valid)
 ADDR_ZERO=$(delta addr_zero)
 KERNEL=$(delta kernel)
 ENQUEUE=$(delta enqueue)
+# Pipeline health check: diagnose which stage is broken when
+# callbacks are zero, using the existing per-CPU state and
+# counter deltas.  This is a best-effort diagnostic, not a
+# substitute for detailed per-backend debugging.
+if [[ "$CALLBACK" -eq 0 ]]; then
+	CPU_ST_BASE_VAL=$(max_cpu_state /sys/kernel/debug/damon/perf_stats)
+	if [[ "$CPU_ST_BASE_VAL" -le 1 ]]; then
+		echo "  Pipeline diagnosis: event not created or bound (state=$CPU_ST_BASE_VAL)"
+	elif [[ "$CPU_ST_BASE_VAL" -eq 2 ]]; then
+		echo "  Pipeline diagnosis: event bound but not enabled (state=BOUND)"
+	elif [[ "$ENQUEUE" -eq 0 ]]; then
+		echo "  Pipeline diagnosis: PMU not producing data or AUX pipeline broken"
+	else
+		echo "  Pipeline diagnosis: samples enqueued but none valid"
+	fi
+fi
 DEQUEUE=$(delta dequeue)
 OVERFLOW=$(delta overflow)
 MATCH=$(delta match)
Signature · 1 line
2.43.0