From: Kunwu Chan <[email protected]>

Add the backend operations used by AUX trace-buffer PMUs, backend state
to damon_perf_event, and stubs for configurations without AUX support.

Add CONFIG_DAMON_PERF_AUX for the ARM SPE transport and a separate
CONFIG_DAMON_PERF_SPE_KUNIT_TEST option.  The current transport requires
ARM SPE to be built into the kernel; it remains independent of the
optional observability counters and tracepoints.

Co-developed-by: Lian Wang (ProcessMission) <[email protected]>
Signed-off-by: Lian Wang (ProcessMission) <[email protected]>
Signed-off-by: Kunwu Chan <[email protected]>
---
 include/linux/damon.h       |  5 +++
 mm/damon/Kconfig            | 28 +++++++++++++++
 mm/damon/perf/aux_backend.h | 72 +++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+)
 create mode 100644 mm/damon/perf/aux_backend.h

diff --git a/include/linux/damon.h b/include/linux/damon.h
index c191c065b0e4..a27c5f6c459b 100644
--- a/include/linux/damon.h
+++ b/include/linux/damon.h
@@ -8,6 +8,7 @@
 #ifndef _DAMON_H_
 #define _DAMON_H_
 
+#include <linux/cpumask.h>
 #include <linux/math64.h>
 #include <linux/memcontrol.h>
 #include <linux/mutex.h>
@@ -127,6 +128,7 @@ struct damon_target {
 enum damon_report_source {
        DAMON_REPORT_SRC_PERF_OVERFLOW = 0,  /* overflow_handler (IBS, PEBS) */
        DAMON_REPORT_SRC_PAGE_FAULT,         /* damon_report_page_fault() */
+       DAMON_REPORT_SRC_PERF_AUX,           /* AUX trace-buffer backend */
 };
 
 /**
@@ -538,6 +540,7 @@ struct damos_filter {
 struct damon_ctx;
 struct damon_target_lookup;
 struct damos;
+struct damon_perf_backend_ops;
 
 /**
  * struct damos_walk_control - Control damos_walk().
@@ -1056,6 +1059,8 @@ struct damon_perf_event_attr {
 struct damon_perf_event {
        struct damon_perf_event_attr attr;
        void *priv;
+       const struct damon_perf_backend_ops *ops;
+       cpumask_t aux_cpumask;
        struct list_head list;
        struct hlist_node hlist_node;
        bool init_complete;
diff --git a/mm/damon/Kconfig b/mm/damon/Kconfig
index 9fac286df124..e9fb62ec186f 100644
--- a/mm/damon/Kconfig
+++ b/mm/damon/Kconfig
@@ -149,4 +149,32 @@ config DAMON_PERF_OBSERVE
 
          If unsure, say N.
 
+
+config DAMON_PERF_AUX
+       bool "DAMON AUX trace-buffer backend support"
+       depends on DAMON
+       depends on PERF_EVENTS
+       depends on ARM_SPE_PMU=y
+       default n
+       help
+         Enable the functional AUX trace-buffer transport for DAMON perf
+         events.  This provides backend selection and drain scheduling for
+         ARM SPE, which does not deliver samples through an overflow
+         callback.  ARM SPE must be built into the kernel.
+
+         This transport is independent of CONFIG_DAMON_PERF_OBSERVE.
+
+         If unsure, say N.
+
+config DAMON_PERF_SPE_KUNIT_TEST
+       bool "Test the DAMON perf SPE parser" if !KUNIT_ALL_TESTS
+       depends on DAMON_PERF_AUX && KUNIT=y
+       default KUNIT_ALL_TESTS
+       help
+         Test the ARM SPE parser with byte-exact packet streams.  The cases
+         cover valid records, truncation, alignment, error resynchronization
+         and consumed-length accounting.  Results can be exposed through
+         debugfs when CONFIG_KUNIT_DEBUGFS is enabled.
+
+         If unsure, say N.
 endmenu
diff --git a/mm/damon/perf/aux_backend.h b/mm/damon/perf/aux_backend.h
new file mode 100644
index 000000000000..70e6ac6771bc
--- /dev/null
+++ b/mm/damon/perf/aux_backend.h
@@ -0,0 +1,72 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _DAMON_PERF_AUX_BACKEND_H
+#define _DAMON_PERF_AUX_BACKEND_H
+
+#include <linux/bits.h>
+#include <linux/errno.h>
+#include <linux/perf_event.h>
+#include <linux/types.h>
+
+struct damon_ctx;
+struct damon_perf_event;
+
+/**
+ * struct damon_perf_backend_ops - PMU-specific AUX backend operations
+ * @name: Human-readable backend name.
+ * @flags: Bitmask of DAMON_PERF_BACKEND_* flags.
+ * @match_pmu: Return true when this backend claims @perf_event.
+ * @init: Allocate per-event, per-CPU resources.
+ * @cleanup: Release resources initialized for one CPU.
+ * @arm: Prepare the AUX producer before perf_event_enable().
+ * @disarm: Quiesce backend state after perf_event_disable().
+ * @drain: Parse pending AUX data into DAMON access reports.
+ *
+ * All callbacks run in process context.  The caller serializes resource
+ * lifetime against CPU hotplug and invokes @drain only for CPUs recorded
+ * in damon_perf_event::aux_cpumask.
+ */
+struct damon_perf_backend_ops {
+       const char *name;
+       u32 flags;
+       bool (*match_pmu)(struct perf_event *perf_event);
+       int (*init)(struct damon_perf_event *event, int cpu,
+                   struct perf_event *perf_event);
+       void (*cleanup)(struct damon_perf_event *event, int cpu);
+       int (*arm)(struct damon_perf_event *event, int cpu);
+       void (*disarm)(struct damon_perf_event *event, int cpu);
+       unsigned int (*drain)(struct damon_perf_event *event, int cpu);
+};
+
+#define DAMON_PERF_BACKEND_AUX BIT(0)
+
+#ifdef CONFIG_DAMON_PERF_AUX
+void damon_perf_aux_drain(struct damon_ctx *ctx);
+int damon_perf_aux_register_backend(const struct damon_perf_backend_ops *ops);
+const struct damon_perf_backend_ops *
+damon_perf_aux_find_backend(struct perf_event *perf_event);
+void damon_perf_aux_select(struct damon_perf_event *event,
+                          struct perf_event *perf_event);
+#else
+static inline void damon_perf_aux_drain(struct damon_ctx *ctx)
+{
+}
+
+static inline int
+damon_perf_aux_register_backend(const struct damon_perf_backend_ops *ops)
+{
+       return -EOPNOTSUPP;
+}
+
+static inline const struct damon_perf_backend_ops *
+damon_perf_aux_find_backend(struct perf_event *perf_event)
+{
+       return NULL;
+}
+
+static inline void damon_perf_aux_select(struct damon_perf_event *event,
+                                        struct perf_event *perf_event)
+{
+}
+#endif
+
+#endif /* _DAMON_PERF_AUX_BACKEND_H */
-- 
2.43.0


Reply via email to