Skip to contentSkip to conversation

mm/damon: introduce perf event based access check

RFC PATCH In review Akinobu Mita 4 participants 16 messages Apr 23 – Apr 29

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover mm/damon: introduce perf event based access check

DAMON currently only provides PTE accessed-bit based access check, this patch series adds a new perf event based access check.

Since perf event-based access checks do not require modifying the PTE accessed-bit for pages representing each damon region, it reduces the overhead of monitoring at a fixed granularity of the page size.

Here is a method and its results for comparing access checks using existing access bits and new perf events.

The time required for the access check will be measured using the following steps.

Start a memcached server with approximately 100GB of memory and run a benchmark client.

```
# Run memcached as a daemon
memcached --daemon --pidfile=/tmp/memcached.pid --port=7777 \
  --memory-limit=200000 --threads=8
# Load keys
memtier_benchmark -s 127.0.0.1 -p 7777 --key-maximum=100000000 \
  --data-size=1000 --threads=8 --protocol=memcache_text \
  --key-pattern=P:P --ratio=1:0 --requests=allkeys
# Run benchmark in the background
memtier_benchmark -s 127.0.0.1 -p 7777 --key-maximum=100000000 \
  --data-size=1000 --threads=8 --protocol=memcache_text \
  --key-pattern=Z:Z --ratio=50:50 --test-time=999999 --pipeline=10 \
  --distinct-client-seed --randomize &> /tmp/memtier.log &
```

Measure the time required for access checks when monitoring memcached at page size granularity.

Using accessed bit, prepare_access_checks takes 7 seconds and check_accesses takes 5 seconds.

```
$ sudo $HOME/damo/damo start --ops vaddr \
	--monitoring_intervals 5s 60s 300s \
	--monitoring_nr_regions_range 1000000000 1000000000 \
	--target_pid $(cat /tmp/memcached.pid)
$ sudo perf ftrace latency -a --hide-empty \
  -T damon_va_prepare_access_checks -- sleep 60
#   DURATION     |      COUNT | GRAPH                                          |
     1 - ...  s  |          4 | ############################################## |

# statistics  (in usec)
  total time:             28624961
    avg time:              7156240
    max time:              7322209
    min time:              6859464
       count:                    4
$ sudo perf ftrace latency -a --hide-empty \
  -T damon_va_check_accesses -- sleep 60
#   DURATION     |      COUNT | GRAPH                                          |
     1 - ...  s  |          3 | ############################################## |

# statistics  (in usec)
  total time:             16007973
    avg time:              5335991
    max time:              5369579
    min time:              5300285
       count:                    3
```

Using perf event, prepare_access_checks takes 0.01 seconds and check_accesses takes 2.6 seconds.

```
$ sudo $HOME/damo/damo stop
$ sudo $HOME/damo/damo start --ops vaddr \
	--perf_event 5000 0 0x4 0x1cd 0x1f 0 \
	--monitoring_intervals 5s 60s 300s \
	--monitoring_nr_regions_range 1000000000 1000000000 \
	--target_pid $(cat /tmp/memcached.pid)
$ sudo perf ftrace latency -a --hide-empty \
  -T kdamond_prepare_perf_event_report -- sleep 60
#   DURATION     |      COUNT | GRAPH                                          |
     4 -    8 ms |          7 | ############################################## |

# statistics  (in usec)
  total time:                75641
    avg time:                10806
    max time:                11174
    min time:                 9945
       count:                    7
$ sudo perf ftrace latency -a --hide-empty \
  -T kdamond_check_perf_event_reported_accesses -- sleep 60
#   DURATION     |      COUNT | GRAPH                                          |
     1 - ...  s  |          7 | ############################################## |

# statistics  (in usec)
  total time:             18101248
    avg time:              2585893
    max time:              2834578
    min time:              2504466
       count:                    7
$ kill $(cat /tmp/memcached.pid)
```

The time required for prepare_access_checks is proportional to the size of the monitoring region when using the accessed bit, but it takes almost no time when using the perf event as it only requires enabling it.

The time required for check_accesses is proportional to the size of the monitoring region, regardless of whether you use the accessed bit or perf-event.

This indicates that monitoring even larger memory ranges at page granularity could take even longer.

However, when using perf-event, there is room in the future to make the time proportional to the number of perf event samples by changing the monitoring region management from an address-order linear list to a tree structure.

It is important to note that with perf events, extra memory such as sampling buffers needs to be allocated, and there is overhead due to perf event interrupts (NMI) during the sampling interval.

Note: damo's --perf_event option

Using these features also requires modifications to damo, but these are not included in this patch series and are currently under development in the following branch:

* https://github.com/mita/damo/tree/damo-perf-for-v3.2.2

The option newly added to damo for perf event-based access check has the following format:

--perf_event <sample freq> <sample phys addr> <type> <config> <config1> <config2>

- sample freq: A higher frequency improves access accuracy, but also increases overhead.
- sample phys addr: specify 0 for vaddr and 1 for paddr.
- The remaining type, config, config1, and config2 settings can be found as follows:

Run perf mem record -vv to obtain the log, and then find the part in the log where perf_event_open was executed successfully.

```
$ sudo perf mem record -vv echo > /tmp/perf-mem-record.log 2>&1
$ cat /tmp/perf-mem-record.log
...
perf_event_attr:
  type                             4 (cpu)
  size                             144
  config                           0x1cd (mem_trans_retired.load_latency_gt_1024)
  { sample_period, sample_freq }   4000
  sample_type                      IP|TID|TIME|ADDR|ID|PERIOD|DATA_SRC|WEIGHT_STRUCT
  read_format                      ID|LOST
  disabled                         1
  inherit                          1
  mmap                             1
  comm                             1
  freq                             1
  enable_on_exec                   1
  task                             1
  precise_ip                       2
  mmap_data                        1
  sample_id_all                    1
  mmap2                            1
  comm_exec                        1
  ksymbol                          1
  bpf_event                        1
  build_id                         1
  { bp_addr, config1 }             0x1f
------------------------------------------------------------
sys_perf_event_open: pid 287986  cpu 0  group_fd -1  flags 0x8 = 5
...
```

The values for type and config are taken from this output. Use the values of config1 and config2 if they are included in the log; otherwise, set them to 0.

Any feedback or advice on the patch set would be greatly appreciated.

* RFC v3
- drop "reintroduce damon_operations->cleanup()" as no longer needed
- drop "allow user to set min size of region" (will be sent separately)
- add tgid to struct damon_access_report and use it instead of tid
- set perf_event_attr.precise_ip to 2
- prepare for the transition to report-based monitoring
- switch to use access_reported in struct damon_region
- switch to use sample/primitives/perf_events sysfs directory

* RFC v2: https://lore.kernel.org/damon/[email protected]/T/
- reintroduce damon_operations->cleanup()
- introduce struct damon_access_report
- use struct damon_access_report instead of introducing struct damon_perf_record
- remove maximum region size setting

* RFC v1: https://lore.kernel.org/damon/[email protected]/T/

* TODO
- Integrate into report-based monitoring
- Explain actual usage examples and test results
- Send the necessary patches to damo
- Set maybe_corrupted when it detects that a useless perf_event has been specified.
- Check if it works in a virtual environment using vPMU

Akinobu Mita (4):
mm/damon/core: add code borrowed from report-based monitoring work mm/damon/core: add common code for perf event based access check mm/damon/vaddr: support perf event based access check mm/damon/paddr: support perf event based access check

 .../ABI/testing/sysfs-kernel-mm-damon         |   8 +
 include/linux/damon.h                         | 102 ++
 mm/damon/core.c                               | 113 ++-
 mm/damon/ops-common.h                         |  59 ++
 mm/damon/paddr.c                              |  41 +
 mm/damon/sysfs.c                              | 872 +++++++++++++++++-
 mm/damon/vaddr.c                              | 506 ++++++++++
 7 files changed, 1697 insertions(+), 4 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

RFC PATCH v3 1/4 mm/damon/core: add code borrowed from report-based monitoring work

The perf event-based monitoring will be implemented on top of the report-based monitoring infrastructure, but since the migration is still in progress, only borrow the following necessary code for now.

* struct damon_access_report
* access_reported field in struct damon_region
* sample/primitives/ sysfs directory

[1] https://lore.kernel.org/linux-mm/[email protected]/T/

---
 include/linux/damon.h |  68 ++++++
 mm/damon/core.c       |   1 +
 mm/damon/sysfs.c      | 499 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 568 insertions(+)
include/linux/damon.h +68 -0
index f2cdb7c3f5e6..9ba26d8a0bce 100644
@@ -85,6 +85,7 @@ struct damon_region {
 	unsigned int age;
 /* private: Internal value for age calculation. */
 	unsigned int last_nr_accesses;
+	bool access_reported;
 };
 
 /**
@@ -112,6 +113,30 @@ struct damon_target {
 	bool obsolete;
 };
 
+/**
+ * struct damon_access_report - Represent single access report information.
+ * @paddr:		Start physical address of the accessed address range.
+ * @vaddr:		Start virtual address of the accessed address range.
+ * @size:		The size of the accessed address range.
+ * @cpu:		The id of the CPU that made the access.
+ * @tid:		The task id of the task that made the access.
+ * @is_write:		Whether the access is write.
+ *
+ * Any DAMON API callers that notified access events can report the information
+ * to DAMON using damon_report_access().  This struct contains the reporting
+ * information.  Refer to damon_report_access() for more details.
+ */
+struct damon_access_report {
+	unsigned long paddr;
+	unsigned long vaddr;
+	unsigned long size;
+	unsigned int cpu;
+	pid_t tid;
+	bool is_write;
+/* private: */
+	unsigned long report_jiffies;	/* when this report is made */
+};
+
 /**
  * enum damos_action - Represents an action of a Data Access Monitoring-based
  * Operation Scheme.
@@ -763,6 +788,49 @@ struct damon_attrs {
 	unsigned long aggr_samples;
 };
 
+/**
+ * enum damon_sample_filter_type - Type of &struct damon_sample_filter.
+ *
+ * @DAMON_FILTER_TYPE_CPUMASK:	Filter by access-generated CPUs.
+ * @DAMON_FILTER_TYPE_THREADS:	Filter by access-generated threads.
+ * @DAMON_FILTER_TYPE_WRITE:	Filter by whether the access was for writing.
+ *
+ * Read &struct damon_sample_control for more details.
+ */
+enum damon_sample_filter_type {
+	DAMON_FILTER_TYPE_CPUMASK,
+	DAMON_FILTER_TYPE_THREADS,
+	DAMON_FILTER_TYPE_WRITE,
+};
+
+/**
+ * struct damon_sample_filter - &struct damon_access_report filter.
+ *
+ * @type:	The type of this filter.
+ * @matching:	Whether it is for condition-matching reports.
+ * @allow:	Whether to include or excludie the @matching reports.
+ * @cpumask:	Access-generated CPUs if @type is DAMON_FILTER_TYPE_CPUMASK.
+ * @tid_arr:	Array of access-generated thread ids, if @type is
+ *		DAMON_FILTER_TYPE_THREADS.
+ * @nr_tids:	Size of @tid_arr, if @type is DAMON_FILTER_TYPE_THREADS.
+ * @list:	List head for siblings.
+ *
+ * Read &struct damon_sample_control for more details.
+ */
+struct damon_sample_filter {
+	enum damon_sample_filter_type type;
+	bool matching;
+	bool allow;
+	union {
+		cpumask_t cpumask;
+		struct {
+			pid_t *tid_arr;
+			int nr_tids;
+		};
+	};
+	struct list_head list;
+};
+
 /**
  * struct damon_ctx - Represents a context for each monitoring.  This is the
  * main interface that allows users to set the attributes and get the results
mm/damon/core.c +1 -0
index 3dbbbfdeff71..a52b2962aa22 100644
@@ -142,6 +142,7 @@ struct damon_region *damon_new_region(unsigned long start, unsigned long end)
 
 	region->age = 0;
 	region->last_nr_accesses = 0;
+	region->access_reported = false;
 
 	return region;
 }
mm/damon/sysfs.c +499 -0

531 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [RFC PATCH v3 1/4] mm/damon/core: add code borrowed from report-based monitoring work

Sashiko AI review found 2 potential issue(s): - [Medium] Stack bloat / stack overflow risk from `cpumask_t` allocation on the stack. - [Medium] Data race and potential torn reads from lockless…

Sashiko AI review found 2 potential issue(s):
- [Medium] Stack bloat / stack overflow risk from cpumask_t allocation on the stack.
- [Medium] Data race and potential torn reads from lockless assignment of cpumask_t. --

commit 9c32a9c6100e762865883bc9bf4b2272ccb6dedb
Author: Akinobu Mita <[email protected]>

mm/damon/core: add code borrowed from report-based monitoring work

This commit borrows code from the report-based monitoring infrastructure to support perf event-based monitoring. It brings in struct damon_access_report, an access_reported field in damon_region, and the sample/primitives sysfs directory structure.

Quoted 4 lines
mm/damon/sysfs.c +0 -0
index eefa959aa30ae..c19556f2af3bb 100644

[ ... ]

mm/damon/sysfs.c static const struct kobj_type damon_sysfs_intervals_ktype = { · line 880

Quoted 6 lines
mm/damon/sysfs.c +6 -0
+static ssize_t cpumask_store(struct kobject *kobj, struct kobj_attribute *attr,
+		const char *buf, size_t count)
+{
+	struct damon_sysfs_sample_filter *filter = container_of(kobj,
+			struct damon_sysfs_sample_filter, kobj);
+	cpumask_t cpumask;

Is it safe to allocate cpumask_t on the stack here?

The size of cpumask_t scales with CONFIG_NR_CPUS. On systems with a large number of CPUs, this could consume a significant amount of the kernel stack and potentially risk a stack overflow. Should this use alloc_cpumask_var() instead to dynamically allocate the temporary mask?

mm/damon/sysfs.c static const struct kobj_type damon_sysfs_intervals_ktype = { · line 885

Quoted 5 lines
mm/damon/sysfs.c +5 -0
+	int err = cpulist_parse(buf, &cpumask);
+
+	if (err)
+		return err;
+	filter->cpumask = cpumask;

Can this lockless assignment cause a data race or torn reads?

Because cpumask_t can be larger than a single word, this assignment may compile into a non-atomic multi-word copy like memcpy(). If a concurrent sysfs read in cpumask_show() or a DAMON commit operation accesses filter->cpumask while this copy is ongoing, could it read a partially updated, torn mask?

Should this be protected by explicit synchronization, such as damon_sysfs_lock?

Quoted 2 lines
mm/damon/sysfs.c +2 -0
+	return count;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

RFC PATCH v3 2/4 mm/damon/core: add common code for perf event based access check

In reply to Akinobu Mita

This patch creates the necessary infrastructure (structure definitions, functions, sysfs interfaces) to support perf event based access checks for both physical and virtual address space monitoring.

Perf events can be added through the following sysfs interface:

What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/nr_perf_events
Description: Writing a number 'N' to this file creates the number of
directories for controlling each perf event named '0' to 'N-1' under the
perf_events/ directory.

What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/type
Description: Writing to and reading from this file sets and gets the
"type" field of struct perf_event_attr for perf event based access
check.

What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/config
Description: Same as above.

What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/config1
Description: Same as above.

What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/config2
Description: Same as above.

What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/sample_freq
Description: Same as above.

What: /sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/sample_phys_addr
Description: Writing to and reading from this file sets and gets the
PERF_SAMPLE_PHYS_ADDR bitfield in the "sample_type" field of struct
perf_event_attr for perf event based access check.

The perf event that can be specified for the perf event based access check must be able to obtain the data source address corresponding to the sample.

One way to find such perf event settings is to run perf mem record -vv and look for a dump of the perf_event_attr structure given to the perf_event_open() system call in the debug messages output.

If at least one perf event is specified, the perf event based access check will be enabled instead of the existing PTE accessed-bit based access check. In other words, if no perf event is specified, the existing PTE accessed-bit based access check will be used.

Add the following common functions required for both physical and virtual address space monitoring:

damon_perf_init()
Each perf event specified by the sysfs interface is initialized with perf_event_create_kernel_counter().

damon_perf_cleanup()
Release each initialized perf event with perf_event_release_kernel().

damon_perf_prepare_access_checks()
Enable each initialized perf event with perf_event_enable().

---
 .../ABI/testing/sysfs-kernel-mm-damon         |   8 +
 include/linux/damon.h                         |  34 ++
 mm/damon/core.c                               |  67 ++++
 mm/damon/ops-common.h                         |  29 ++
 mm/damon/sysfs.c                              | 377 +++++++++++++++++-
 mm/damon/vaddr.c                              | 158 ++++++++
 6 files changed, 669 insertions(+), 4 deletions(-)
Documentation/ABI/testing/sysfs-kernel-mm-damon +8 -0
index 2424237ebb10..aa69ce09789a 100644
@@ -576,3 +576,11 @@ Contact:	SeongJae Park <[email protected]>
 Description:	Reading this file returns the size of the memory in the region
 		that passed DAMON operations layer-handled filters of the
 		scheme in bytes.
+
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/nr_perf_events
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/type
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/config
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/config1
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/config2
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/sample_phys_addr
+What:		/sys/kernel/mm/damon/admin/kdamonds/<K>/contexts/<C>/monitoring_attrs/sample/primitives/perf_events/<P>/sample_freq
include/linux/damon.h +34 -0
index 9ba26d8a0bce..2e7b5939f350 100644
@@ -120,6 +120,7 @@ struct damon_target {
  * @size:		The size of the accessed address range.
  * @cpu:		The id of the CPU that made the access.
  * @tid:		The task id of the task that made the access.
+ * @tgid:		The task group id of the task that made the access.
  * @is_write:		Whether the access is write.
  *
  * Any DAMON API callers that notified access events can report the information
@@ -132,6 +133,7 @@ struct damon_access_report {
 	unsigned long size;
 	unsigned int cpu;
 	pid_t tid;
+	pid_t tgid;
 	bool is_write;
 /* private: */
 	unsigned long report_jiffies;	/* when this report is made */
@@ -831,6 +833,36 @@ struct damon_sample_filter {
 	struct list_head list;
 };
 
+/**
+ * struct damon_perf_event_attr - raw PMU event attr for access check
+ *
+ * @type:		raw PMU event type for access check
+ * @config:		raw PMU event config for access check
+ * @config1:		raw PMU event config1 for access check
+ * @config2:		raw PMU event config2 for access check
+ * @sample_phys_addr:	raw PMU event PERF_SAMPLE_PHYS_ADDR in sample_type for access check
+ * @sample_freq:	raw PMU event sample_freq for access check
+ */
+struct damon_perf_event_attr {
+	u32 type;
+	u64 config;
+	u64 config1;
+	u64 config2;
+	bool sample_phys_addr;
+	u64 sample_freq;
+};
+
+/**
+ * struct damon_perf_event - perf event for access check
+ *
+ * @priv:		Monitoring operations-specific data
+ */
+struct damon_perf_event {
+	struct damon_perf_event_attr attr;
+	void *priv;
+	struct list_head list;
+};
+
 /**
  * struct damon_ctx - Represents a context for each monitoring.  This is the
  * main interface that allows users to set the attributes and get the results
@@ -855,6 +887,7 @@ struct damon_sample_filter {
  * @ops:	Set of monitoring operations for given use cases.
  * @addr_unit:	Scale factor for core to ops address conversion.
  * @min_region_sz:	Minimum region size.
+ * @perf_events:	Head of perf events (&damon_perf_event) list.
  * @adaptive_targets:	Head of monitoring targets (&damon_target) list.
  * @schemes:		Head of schemes (&damos) list.
  */
@@ -909,6 +942,7 @@ struct damon_ctx {
 	unsigned long addr_unit;
 	unsigned long min_region_sz;
 
+	struct list_head perf_events;
 	struct list_head adaptive_targets;
 	struct list_head schemes;
 };
mm/damon/core.c +67 -0

115 lines not on this page · raw message

mm/damon/ops-common.h +29 -0
index 5efa5b5970de..e28c5afab7f0 100644
@@ -23,3 +23,32 @@ bool damos_folio_filter_match(struct damos_filter *filter, struct folio *folio);
 unsigned long damon_migrate_pages(struct list_head *folio_list, int target_nid);
 
 bool damos_ops_has_filter(struct damos *s);
+
+#ifdef CONFIG_PERF_EVENTS
+
+void damon_perf_prepare_access_checks(struct damon_ctx *ctx, struct damon_perf_event *event);
+int damon_perf_init(struct damon_ctx *ctx, struct damon_perf_event *event);
+void damon_perf_cleanup(struct damon_ctx *ctx, struct damon_perf_event *event);
+
+struct damon_perf {
+	struct perf_event * __percpu *event;
+	struct damon_perf_buffer __percpu *buffer;
+};
+
+#else /* CONFIG_PERF_EVENTS */
+
+static inline void damon_perf_prepare_access_checks(struct damon_ctx *ctx,
+		struct damon_perf_event *event)
+{
+}
+
+static inline int damon_perf_init(struct damon_ctx *ctx, struct damon_perf_event *event)
+{
+	return 0;
+}
+
+static inline void damon_perf_cleanup(struct damon_ctx *ctx, struct damon_perf_event *event)
+{
+}
+
+#endif /* CONFIG_PERF_EVENTS */
mm/damon/sysfs.c +373 -4

437 lines not on this page · raw message

mm/damon/vaddr.c +158 -0

170 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [RFC PATCH v3 2/4] mm/damon/core: add common code for perf event based access check

Sashiko AI review found 3 potential issue(s): - [High] Memory leak in `damon_perf_init()` when `perf_event_create_kernel_counter()` fails with `-ENODEV` for all CPUs. - [Medium] Unnecessary…

Sashiko AI review found 3 potential issue(s):
- [High] Memory leak in damon_perf_init() when perf_event_create_kernel_counter() fails with -ENODEV for all CPUs.
- [Medium] Unnecessary PERF_SAMPLE_WEIGHT_STRUCT flag in perf event attributes arbitrarily restricts hardware compatibility.
- [Low] Linker error when CONFIG_DAMON_VADDR=n but CONFIG_PERF_EVENTS=y.
--

commit 97617fb5ae7981867a46207cd14a68c3cbb4813d
Author: Akinobu Mita <[email protected]>

mm/damon/core: add common code for perf event based access check

This commit creates the necessary infrastructure (structure definitions, functions, sysfs interfaces) to support perf event based access checks for both physical and virtual address space monitoring.

Quoted 4 lines
mm/damon/core.c +0 -0
index a52b2962aa22a..220b105482daa 100644

[ ... ]

Quoted 9 lines
mm/damon/core.c +1 -0
@@ -2933,6 +2990,7 @@ static void kdamond_init_ctx(struct damon_ctx *ctx)
 	unsigned long sample_interval = ctx->attrs.sample_interval ?
 		ctx->attrs.sample_interval : 1;
 	struct damos *scheme;
+	struct damon_perf_event *event, *next;
 
 	ctx->passed_sample_intervals = 0;
 	ctx->next_aggregation_sis = ctx->attrs.aggr_interval / sample_interval;

mm/damon/core.c static void kdamond_init_ctx(struct damon_ctx *ctx) · line 3008

Quoted 6 lines
mm/damon/core.c +3 -0
@@ -2945,6 +3003,15 @@ static void kdamond_init_ctx(struct damon_ctx *ctx)
 		damos_set_next_apply_sis(scheme, ctx);
 		damos_set_filters_default_reject(scheme);
 	}
+
+	list_for_each_entry_safe(event, next, &ctx->perf_events, list) {
+		int err = damon_perf_init(ctx, event);

If the kernel is configured with CONFIG_DAMON_VADDR=n and CONFIG_PERF_EVENTS=y, will this cause a linker error? The function damon_perf_init() is defined in mm/damon/vaddr.c, which would not be compiled in that configuration.

Quoted 13 lines
mm/damon/core.c +6 -0
+
+		if (err) {
+			list_del(&event->list);
+			kfree(event);
+		}
+	}
 }
 
 /*
mm/damon/vaddr.c +0 -0
index b069dbc7e3d25..1534372e7637c 100644

[ ... ]

Quoted 5 lines
mm/damon/vaddr.c +1 -0
@@ -936,6 +939,161 @@ static int damon_va_scheme_score(struct damon_ctx *context,
 	return DAMOS_MAX_SCORE;
 }
 
+#ifdef CONFIG_PERF_EVENTS

[ ... ]

Quoted 6 lines
mm/damon/vaddr.c +6 -0
+int damon_perf_init(struct damon_ctx *ctx, struct damon_perf_event *event)
+{
+	struct damon_perf *perf;
+	struct perf_event_attr attr = {
+		.type = PERF_TYPE_RAW,
+		.size = sizeof(attr),

mm/damon/vaddr.c static int damon_va_scheme_score(struct damon_ctx *context, · line 990

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+		.type = event->attr.type,
+		.config = event->attr.config,
+		.config1 = event->attr.config1,
+		.config2 = event->attr.config2,
+		.sample_freq = event->attr.sample_freq,
+		.freq = 1,
+		.sample_type = PERF_SAMPLE_TIME | PERF_SAMPLE_ADDR |
+			PERF_SAMPLE_PERIOD | PERF_SAMPLE_DATA_SRC |
+			(event->attr.sample_phys_addr ? PERF_SAMPLE_PHYS_ADDR : 0) |
+			PERF_SAMPLE_WEIGHT_STRUCT,

Does this configuration need to include PERF_SAMPLE_WEIGHT_STRUCT?

The overflow handler damon_perf_overflow() doesn't appear to read any weight metrics from the sample data. If the underlying hardware PMU does not support extended weight sampling, could this cause perf_event_create_kernel_counter() to return -EINVAL and unnecessarily reject the configuration?

Quoted 35 lines
mm/damon/vaddr.c +35 -0
+		.precise_ip = 2,
+		.pinned = 1,
+		.disabled = 1,
+	};
+	int cpu;
+	int err = -ENOMEM;
+	bool found = false;
+
+	perf = kzalloc_obj(*perf);
+	if (!perf)
+		return -ENOMEM;
+
+	perf->event = alloc_percpu(typeof(*perf->event));
+	if (!perf->event)
+		goto free_percpu;
+
+	perf->buffer = alloc_percpu(typeof(*perf->buffer));
+	if (!perf->buffer)
+		goto free_percpu;
+
+	for_each_possible_cpu(cpu) {
+		struct perf_event *perf_event;
+		struct damon_perf_buffer *buffer = per_cpu_ptr(perf->buffer, cpu);
+
+		perf_event = perf_event_create_kernel_counter(&attr, cpu, NULL,
+				damon_perf_overflow, event);
+		if (IS_ERR(perf_event)) {
+			err = PTR_ERR(perf_event);
+			if (err == -ENODEV)
+				continue;
+			pr_err("perf event create on CPU %d failed with %d\n", cpu, err);
+			goto free_for_each_cpu;
+		}
+		found = true;
+		*per_cpu_ptr(perf->event, cpu) = perf_event;

mm/damon/vaddr.c static int damon_va_scheme_score(struct damon_ctx *context, · line 1035

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+
+		buffer->size = DAMON_PERF_INIT_RECORDS;
+		buffer->reports = kvcalloc_node(buffer->size, sizeof(buffer->reports[0]),
+						GFP_KERNEL, cpu_to_node(cpu));
+		if (!buffer->reports)
+			goto free_for_each_cpu;
+	}
+	event->priv = perf;
+
+	return found ? 0 : -ENODEV;

If perf_event_create_kernel_counter() returns -ENODEV for all possible CPUs, the found flag remains false, and this returns -ENODEV.

Since the caller will receive a non-zero error code and free the event wrapper without calling damon_perf_cleanup(), does this leak the memory allocated for perf, perf->event, and perf->buffer?

Quoted 17 lines
mm/damon/vaddr.c +17 -0
+
+free_for_each_cpu:
+	for_each_possible_cpu(cpu) {
+		struct perf_event *perf_event = per_cpu(*perf->event, cpu);
+		struct damon_perf_buffer *buffer = per_cpu_ptr(perf->buffer, cpu);
+
+		if (perf_event)
+			perf_event_release_kernel(perf_event);
+		kvfree(buffer->reports);
+	}
+free_percpu:
+	free_percpu(perf->event);
+	free_percpu(perf->buffer);
+	kfree(perf);
+
+	return err;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

RFC PATCH v3 3/4 mm/damon/vaddr: support perf event based access check

In reply to Akinobu Mita

This patch adds perf event based access check for virtual address space monitoring.

The perf event that can be specified for perf event-based access check must be able to obtain the data source address corresponding to the sample. In other words, PERF_SAMPLE_DATA_SRC must be able to be specified in perf_event_attr.sample_type

The perf event based access check is performed as follows:

1. During the sampling interval, samples of the perf event are stored in a sampling buffer.

2. After the sampling interval ends, during the access check, a histogram showing the number of accesses to each memory address is generated based on each sample contained in the sampling buffer.

If the sampling buffer is full during a sampling interval, the sampling buffer is expanded to prepare for the next sampling interval.

If you configure the memory addresses corresponding to the perf event samples to be obtained as virtual addresses, the histogram is implemented as a two-dimensional xarray indexed by pid and virtual memory address.

If you configure perf event samples to have their corresponding memory addresses retrieved by physical addresses, the histogram is implemented using an xarray indexed by physical addresses.

3. For each monitoring target region, the number of accesses is updated based on the histogram.

---
 mm/damon/core.c       |  42 ++++-
 mm/damon/ops-common.h |  24 +++
 mm/damon/vaddr.c      | 348 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 412 insertions(+), 2 deletions(-)
mm/damon/core.c +40 -2
index 220b105482da..def72672982a 100644
@@ -3014,6 +3014,40 @@ static void kdamond_init_ctx(struct damon_ctx *ctx)
 	}
 }
 
+static void kdamond_prepare_perf_event_report(struct damon_ctx *ctx)
+{
+	struct damon_perf_event *event;
+
+	list_for_each_entry(event, &ctx->perf_events, list)
+		damon_perf_prepare_access_checks(ctx, event);
+}
+
+static unsigned int kdamond_check_perf_event_reported_accesses(struct damon_ctx *ctx)
+{
+	struct damon_target *t;
+	struct damon_perf_event *event;
+	unsigned int max_nr_accesses = 0;
+
+	if (damon_target_has_pid(ctx)) {
+		list_for_each_entry(event, &ctx->perf_events, list)
+			damon_va_perf_check_accesses(ctx, event);
+	}
+
+	damon_for_each_target(t, ctx) {
+		struct damon_region *r;
+
+		damon_for_each_region(r, t) {
+			if (r->access_reported)
+				r->access_reported = false;
+			else
+				damon_update_region_access_rate(r, false, &ctx->attrs);
+			max_nr_accesses = max(r->nr_accesses, max_nr_accesses);
+		}
+	}
+
+	return max_nr_accesses;
+}
+
 /*
  * The monitoring daemon that runs as a kernel thread
  */
@@ -3057,13 +3091,17 @@ static int kdamond_fn(void *data)
 		if (kdamond_wait_activation(ctx))
 			break;
 
-		if (ctx->ops.prepare_access_checks)
+		if (!list_empty(&ctx->perf_events))
+			kdamond_prepare_perf_event_report(ctx);
+		else if (ctx->ops.prepare_access_checks)
 			ctx->ops.prepare_access_checks(ctx);
 
 		kdamond_usleep(sample_interval);
 		ctx->passed_sample_intervals++;
 
-		if (ctx->ops.check_accesses)
+		if (!list_empty(&ctx->perf_events))
+			max_nr_accesses = kdamond_check_perf_event_reported_accesses(ctx);
+		else if (ctx->ops.check_accesses)
 			max_nr_accesses = ctx->ops.check_accesses(ctx);
 
 		if (time_after_eq(ctx->passed_sample_intervals,
mm/damon/ops-common.h +24 -0
index e28c5afab7f0..da39abe07cfc 100644
@@ -27,14 +27,33 @@ bool damos_ops_has_filter(struct damos *s);
 #ifdef CONFIG_PERF_EVENTS
 
 void damon_perf_prepare_access_checks(struct damon_ctx *ctx, struct damon_perf_event *event);
+void damon_va_perf_check_accesses(struct damon_ctx *ctx, struct damon_perf_event *event);
 int damon_perf_init(struct damon_ctx *ctx, struct damon_perf_event *event);
 void damon_perf_cleanup(struct damon_ctx *ctx, struct damon_perf_event *event);
 
+struct damon_vaddr_histogram {
+	struct xarray targets;
+};
+
+struct damon_paddr_histogram {
+	struct xarray accesses;
+};
+
 struct damon_perf {
 	struct perf_event * __percpu *event;
 	struct damon_perf_buffer __percpu *buffer;
+	union {
+		struct damon_vaddr_histogram vaddr_histogram;
+		struct damon_paddr_histogram paddr_histogram;
+	};
 };
 
+void damon_paddr_histogram_init(struct damon_paddr_histogram *histogram);
+unsigned long damon_paddr_histogram_count(struct damon_paddr_histogram *histogram, u64 paddr);
+void damon_paddr_histogram_destroy(struct damon_paddr_histogram *histogram);
+
+void damon_perf_populate_paddr_histogram(struct damon_ctx *ctx, struct damon_perf_event *event);
+
 #else /* CONFIG_PERF_EVENTS */
 
 static inline void damon_perf_prepare_access_checks(struct damon_ctx *ctx,
@@ -42,6 +61,11 @@ static inline void damon_perf_prepare_access_checks(struct damon_ctx *ctx,
 {
 }
 
+static inline void damon_va_perf_check_accesses(struct damon_ctx *ctx,
+		struct damon_perf_event *event)
+{
+}
+
 static inline int damon_perf_init(struct damon_ctx *ctx, struct damon_perf_event *event)
 {
 	return 0;
mm/damon/vaddr.c +348 -0

366 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw

Re: [RFC PATCH v3 3/4] mm/damon/vaddr: support perf event based access check

Sashiko AI review found 13 potential issue(s): - [Critical] Compilation Failure: Use of undefined API `kmalloc_obj()`. - [Critical] Compilation Failure: Non-existent field `sample_phys_addr` in…

Sashiko AI review found 13 potential issue(s):
- [Critical] Compilation Failure: Use of undefined API kmalloc_obj().
- [Critical] Compilation Failure: Non-existent field sample_phys_addr in perf_event_attr.
- [Critical] Compilation Failure: Typo in the CONFIG_HUGETLB_PAGE fallback macro.
- [High] Deadlock Risk: Recursive acquisition of mmap_read_lock.
- [High] Infinite Loop: Returning ACTION_AGAIN on pte_offset_map_lock() failure.
- [High] Memory Leak: damon_vaddr_histogram_per_target structs are never freed.
- [High] Memory Leak: Dynamically allocated buffer->reports arrays are orphaned.
- [High] Memory Leak: Orphaned perf structs when the kdamond thread is restarted.
- [High] Data Truncation: 64-bit physical addresses are silently truncated when used as xarray indices on 32-bit platforms.
- [High] Logic Error: Unbounded perf events for physical memory targets (DAMON_OPS_PADDR).
- [High] Logic Error: Invalid PFN extraction from non-present PMD migration entries.
- [High] Severe Performance Flaw: Full page table walks per sampling interval.
- [High] Severe Performance Flaw: O(N) iteration by PAGE_SIZE over massive virtual memory regions.
--

commit 04697c66c594dde51627f9cdaf4c7d11b08fe1fe
Author: Akinobu Mita <[email protected]>

mm/damon/vaddr: support perf event based access check

This commit introduces a mechanism to use perf events for virtual and physical address space monitoring in DAMON. It buffers perf samples during the sampling interval and generates a histogram of memory accesses. During the access check phase, this histogram is used to update the access counts for each monitoring target region.

mm/damon/core.c static void kdamond_init_ctx(struct damon_ctx *ctx) · line 3015

Quoted 6 lines
mm/damon/core.c +0 -0
@@ -3014,6 +3014,40 @@ static void kdamond_init_ctx(struct damon_ctx *ctx)
 	}
 }

When the kdamond thread begins, kdamond_init_ctx() unconditionally initializes the perf events. If a user stops and restarts the thread via sysfs, does this leak the previous allocations? Calling damon_perf_init() again would allocate new perf structures, hardware counters, and per-CPU buffers, permanently leaking the previous ones.

Quoted 8 lines
mm/damon/core.c +8 -0
+static void kdamond_prepare_perf_event_report(struct damon_ctx *ctx)
+{
+	struct damon_perf_event *event;
+
+	list_for_each_entry(event, &ctx->perf_events, list)
+		damon_perf_prepare_access_checks(ctx, event);
+}
+

mm/damon/core.c static void kdamond_init_ctx(struct damon_ctx *ctx) · line 3034

Quoted 10 lines
mm/damon/core.c +10 -0
+static unsigned int kdamond_check_perf_event_reported_accesses(struct damon_ctx *ctx)
+{
+	struct damon_target *t;
+	struct damon_perf_event *event;
+	unsigned int max_nr_accesses = 0;
+
+	if (damon_target_has_pid(ctx)) {
+		list_for_each_entry(event, &ctx->perf_events, list)
+			damon_va_perf_check_accesses(ctx, event);
+	}

For physical memory targets (which have no PIDs), the processing code inside this if block is skipped. However, kdamond_prepare_perf_event_report() unconditionally enables perf events. Does this leave perf events permanently enabled and continuously firing for physical memory targets?

[ ... ]

Quoted 3 lines
mm/damon/vaddr.c +0 -0

[ ... ]

mm/damon/vaddr.c struct damon_perf_buffer { · line 968

Quoted 8 lines
mm/damon/vaddr.c +8 -0
+static void damon_vaddr_histogram_add(struct damon_vaddr_histogram *histogram, u32 pid,
+						u64 vaddr)
+{
+	struct damon_vaddr_histogram_per_target *target;
+	unsigned long nr_accesses;
+
+	while (!(target = xa_load(&histogram->targets, pid))) {
+		target = kmalloc_obj(*target);

Can this code compile? There is no kmalloc_obj() macro or function in the Linux kernel. A standard kmalloc() could be used instead:

target = kmalloc(sizeof(*target), GFP_KERNEL);

[ ... ]

mm/damon/vaddr.c struct damon_perf_buffer { · line 1004

Quoted 7 lines
mm/damon/vaddr.c +7 -0
+static void damon_vaddr_histogram_destroy(struct damon_vaddr_histogram *histogram)
+{
+	unsigned long index;
+	struct damon_vaddr_histogram_per_target *target;
+
+	xa_for_each(&histogram->targets, index, target)
+		xa_destroy(&target->accesses);

Does this code leak the damon_vaddr_histogram_per_target structure? The nested accesses xarray is destroyed via xa_destroy(), but the target struct itself is not freed with kfree().

Quoted 2 lines
mm/damon/vaddr.c +2 -0
+	xa_destroy(&histogram->targets);
+}

[ ... ]

Quoted 3 lines
mm/damon/vaddr.c +3 -0
+static void damon_va_perf_check_accesses_by_vaddr(struct damon_ctx *ctx,
+		struct damon_perf_event *event)
+{

[ ... ]

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1192

Quoted 4 lines
mm/damon/vaddr.c +4 -0
+		if ((count == buffer->size - 1) && (buffer->size < DAMON_PERF_MAX_RECORDS)) {
+			void *new_reports = kvcalloc_node(buffer->size * 2,
+					sizeof(buffer->reports[0]), GFP_KERNEL,
+					cpu_to_node(cpu));

Are these dynamically allocated buffer->reports arrays freed during cleanup? When perf monitoring stops, damon_perf_cleanup() frees the top-level per-CPU pointer wrapper but appears to orphan these inner reports arrays.

[ ... ]

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1214

Quoted 11 lines
mm/damon/vaddr.c +11 -0
+	damon_for_each_target(t, ctx) {
+		struct damon_region *r;
+		u32 pid = pid_nr(t->pid);
+
+		damon_for_each_region(r, t) {
+			unsigned long addr;
+
+			if (r->access_reported)
+				continue;
+
+			for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {

Can this loop stall the kernel thread? Since DAMON regions can merge to encompass hundreds of gigabytes, iterating from r->ar.start to r->ar.end by PAGE_SIZE could result in hundreds of millions of iterations per interval. Could this logic iterate over the populated xarray entries instead?

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+				if (damon_vaddr_histogram_count(&perf->vaddr_histogram, pid,
+									addr & PAGE_MASK)) {
+					damon_update_region_access_rate(r, true, &ctx->attrs);
+					r->access_reported = true;
+					break;
+				}
+			}
+		}
+		tidx++;
+	}

[ ... ]

Quoted 3 lines
mm/damon/vaddr.c +3 -0
+static void damon_va_perf_check_accesses_by_paddr(struct damon_ctx *ctx,
+		struct damon_perf_event *event)
+{

[ ... ]

Quoted 9 lines
mm/damon/vaddr.c +9 -0
+	damon_for_each_target(t, ctx) {
+		struct damon_region *r;
+		struct mm_struct *mm = damon_get_mm(t);
+
+		if (!mm)
+			continue;
+
+		mmap_read_lock(mm);
+		damon_for_each_region(r, t) {

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1316

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+			struct damon_paddr_walk walk_private = {
+				.histogram = &perf->paddr_histogram,
+				.accessed = false,
+			};
+
+			if (r->access_reported)
+				continue;
+
+			walk_page_range(mm, r->ar.start, r->ar.end, &damon_paddr_ops,
+					&walk_private);

Can this sequence cause a recursive read lock deadlock? The mmap_read_lock is acquired directly, but damon_paddr_ops is configured with .walk_lock = PGWALK_RDLOCK, which instructs the page walker to also acquire the mmap_read_lock. If a writer is pending, the second read lock will block and self-deadlock.

Is it too expensive to call walk_page_range() across the target's entire mapped virtual memory space on every sampling interval? For large memory footprints, walking millions of page table entries every few milliseconds could create severe CPU overhead and defeat the adaptive sampling design.

Quoted 6 lines
mm/damon/vaddr.c +6 -0
+			if (walk_private.accessed) {
+				damon_update_region_access_rate(r, true, &ctx->attrs);
+				r->access_reported = true;
+			}
+		}
+		mmap_read_unlock(mm);

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1332

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+		mmput(mm);
+		tidx++;
+	}
+
+	damon_paddr_histogram_destroy(&perf->paddr_histogram);
+}
+
+void damon_va_perf_check_accesses(struct damon_ctx *ctx, struct damon_perf_event *event)
+{
+	if (event->attr.sample_phys_addr)

Does struct perf_event_attr contain a sample_phys_addr field? Physical address sampling is typically requested by setting the PERF_SAMPLE_PHYS_ADDR flag in the sample_type bitmask.

Quoted 4 lines
mm/damon/vaddr.c +4 -0
+		return damon_va_perf_check_accesses_by_paddr(ctx, event);
+	else
+		return damon_va_perf_check_accesses_by_vaddr(ctx, event);
+}

[ ... ]

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1349

Quoted 7 lines
mm/damon/vaddr.c +7 -0
+static void damon_paddr_histogram_add(struct damon_paddr_histogram *histogram,
+						u64 paddr)
+{
+	unsigned long nr_accesses;
+
+	nr_accesses = xa_to_value(xa_load(&histogram->accesses, paddr));
+	xa_store(&histogram->accesses, paddr, xa_mk_value(nr_accesses + 1), GFP_KERNEL);

Can this silently truncate physical addresses on 32-bit platforms? The paddr argument is a u64, but it is passed directly as the index to xa_load() and xa_store(), which take an unsigned long. On 32-bit systems with PAE/LPAE, this will drop the upper 32 bits. Could the physical address be shifted by PAGE_SHIFT to store the PFN instead?

Quoted 1 line

+}

[ ... ]

Quoted 4 lines
mm/damon/vaddr.c +4 -0
+static int damon_paddr_pmd_entry(pmd_t *pmd, unsigned long addr,
+		unsigned long next, struct mm_walk *walk)
+{
+	pte_t *pte;

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1375

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+	spinlock_t *ptl;
+	struct damon_paddr_walk *paddr_walk = walk->private;
+
+	ptl = pmd_trans_huge_lock(pmd, walk->vma);
+	if (ptl) {
+		pmd_t pmde = pmdp_get(pmd);
+
+		if (pmd_present(pmde)) {
+			for (; addr < next && !paddr_walk->accessed; addr += PAGE_SIZE) {
+				u64 frame = pmd_pfn(pmde) +

Can pmd_present() evaluate to true for non-present leaf entries like migration entries? Extracting the PFN via pmd_pfn() on a migration entry would treat swap offset metadata as a physical address, resulting in invalid calculations. Does this code need to verify the PMD is a valid THP first?

Quoted 7 lines
mm/damon/vaddr.c +7 -0
+					((addr & ~HPAGE_PMD_MASK) >> PAGE_SHIFT);
+
+				if (damon_paddr_histogram_count(paddr_walk->histogram,
+						PFN_PHYS(frame))) {
+					paddr_walk->accessed = true;
+					break;
+				}

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1392

Quoted 10 lines
mm/damon/vaddr.c +10 -0
+			}
+		}
+		spin_unlock(ptl);
+		return paddr_walk->accessed ? 1 : 0;
+	}
+
+	pte = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
+	if (!pte) {
+		walk->action = ACTION_AGAIN;
+		return 0;

Will setting walk->action = ACTION_AGAIN on a pte_offset_map_lock() failure cause an infinite loop? Migration entries cause this lock to return NULL non-transiently, meaning the page walker will continually retry the exact same PMD without making progress. Returning 0 without ACTION_AGAIN skips it gracefully.

Quoted 1 line
mm/damon/vaddr.c +1 -0
+	}

[ ... ]

Quoted 2 lines
mm/damon/vaddr.c +2 -0
+#ifdef CONFIG_HUGETLB_PAGE
+static int damon_paddr_hugetlb_entry(pte_t *pte, unsigned long hmask,

[ ... ]

mm/damon/vaddr.c void damon_perf_prepare_access_checks(struct damon_ctx *ctx, · line 1439

Quoted 9 lines
mm/damon/vaddr.c +9 -0
+	return paddr_walk->accessed ? 1 : 0;
+}
+#else
+#define damon_perf_hugetlb_entry NULL
+#endif /* CONFIG_HUGETLB_PAGE */
+
+static const struct mm_walk_ops damon_paddr_ops = {
+	.pmd_entry = damon_paddr_pmd_entry,
+	.hugetlb_entry = damon_paddr_hugetlb_entry,

Does this cause a compilation failure when CONFIG_HUGETLB_PAGE is disabled? The fallback macro is defined as damon_perf_hugetlb_entry, but the struct initializes .hugetlb_entry with damon_paddr_hugetlb_entry, leaving it undeclared.

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

RFC PATCH v3 4/4 mm/damon/paddr: support perf event based access check

In reply to Akinobu Mita

This patch adds perf event based access checks for physical address spaces monitoring.

The implementation is very similar to that described in perf event based access check for virtual address space monitoring.

However, for perf events that can be specified with physical address spaces monitoring, the data source address corresponding to the sample must be obtainable as a physical address. In other words, PERF_SAMPLE_DATA_SRC and PERF_SAMPLE_PHYS_ADDR must be specifiable in perf_event_attr.sample_type.

---
 mm/damon/core.c       |  3 +++
 mm/damon/ops-common.h |  6 ++++++
 mm/damon/paddr.c      | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)
mm/damon/core.c +3 -0
index def72672982a..bf9318c13d0b 100644
@@ -3031,6 +3031,9 @@ static unsigned int kdamond_check_perf_event_reported_accesses(struct damon_ctx
 	if (damon_target_has_pid(ctx)) {
 		list_for_each_entry(event, &ctx->perf_events, list)
 			damon_va_perf_check_accesses(ctx, event);
+	} else {
+		list_for_each_entry(event, &ctx->perf_events, list)
+			damon_pa_perf_check_accesses(ctx, event);
 	}
 
 	damon_for_each_target(t, ctx) {
mm/damon/ops-common.h +6 -0
index da39abe07cfc..f6db6bc4d882 100644
@@ -28,6 +28,7 @@ bool damos_ops_has_filter(struct damos *s);
 
 void damon_perf_prepare_access_checks(struct damon_ctx *ctx, struct damon_perf_event *event);
 void damon_va_perf_check_accesses(struct damon_ctx *ctx, struct damon_perf_event *event);
+void damon_pa_perf_check_accesses(struct damon_ctx *ctx, struct damon_perf_event *event);
 int damon_perf_init(struct damon_ctx *ctx, struct damon_perf_event *event);
 void damon_perf_cleanup(struct damon_ctx *ctx, struct damon_perf_event *event);
 
@@ -66,6 +67,11 @@ static inline void damon_va_perf_check_accesses(struct damon_ctx *ctx,
 {
 }
 
+static inline void damon_pa_perf_check_accesses(struct damon_ctx *ctx,
+		struct damon_perf_event *event)
+{
+}
+
 static inline int damon_perf_init(struct damon_ctx *ctx, struct damon_perf_event *event)
 {
 	return 0;
mm/damon/paddr.c +41 -0

47 lines not on this page · raw message

Signature · 1 line
2.43.0
#8 lore raw

Re: [RFC PATCH v3 4/4] mm/damon/paddr: support perf event based access check

Sashiko AI review found 6 potential issue(s): - [High] Severe Performance Regression and Soft Lockups via O(N) Memory Iteration - [High] Address Scale Mismatch Breaking Access Reporting…

Sashiko AI review found 6 potential issue(s):
- [High] Severe Performance Regression and Soft Lockups via O(N) Memory Iteration
- [High] Address Scale Mismatch Breaking Access Reporting (ctx->addr_unit ignored)
- [High] Infinite Loop and Kernel Hang via Integer Overflow on 32-bit
- [High] XArray Index Truncation Corrupting PAE Physical Addresses
- [Medium] Silent Failure due to Missing PERF_SAMPLE_PHYS_ADDR Validation
- [Low] Unused Local Variable tidx
--

commit 0cc4cc53289c7273fbdf7b96674ce9315f78cdfb
Author: Akinobu Mita <[email protected]>

mm/damon/paddr: support perf event based access check

This commit adds perf event based access checks for physical address space monitoring. It implements support similar to the virtual address space monitoring, relying on PERF_SAMPLE_PHYS_ADDR to obtain the data source address.

Quoted 1 line

mm/damon/paddr: support perf event based access check

[ ... ]

Quoted 5 lines

However, for perf events that can be specified with physical address spaces monitoring, the data source address corresponding to the sample must be obtainable as a physical address. In other words, PERF_SAMPLE_DATA_SRC and PERF_SAMPLE_PHYS_ADDR must be specifiable in perf_event_attr.sample_type.

Is there validation in the code to enforce this requirement? If a user or the framework fails to set the sample_phys_addr attribute, the perf subsystem might fail to populate the physical address and default to 0. Could this silently process invalid samples and record all accesses at physical address 0?

[ ... ]

Quoted 4 lines
mm/damon/paddr.c +0 -0
index 5cdcc5037cbc1..97df21281fb13 100644

[ ... ]

mm/damon/paddr.c static int damon_pa_scheme_score(struct damon_ctx *context, · line 372

Quoted 11 lines
mm/damon/paddr.c +7 -0
@@ -363,6 +363,47 @@ static int damon_pa_scheme_score(struct damon_ctx *context,
 	return DAMOS_MAX_SCORE;
 }
 
+#ifdef CONFIG_PERF_EVENTS
+
+void damon_pa_perf_check_accesses(struct damon_ctx *ctx, struct damon_perf_event *event)
+{
+	struct damon_perf *perf = event->priv;
+	struct damon_target *t;
+	unsigned int tidx = 0;

Is tidx needed here? It is incremented at the end of the loop but never read, which might trigger an unused-but-set-variable compiler warning.

Quoted 8 lines
mm/damon/paddr.c +8 -0
+
+	if (!perf)
+		return;
+
+	damon_paddr_histogram_init(&perf->paddr_histogram);
+
+	damon_perf_populate_paddr_histogram(ctx, event);
+

mm/damon/paddr.c static int damon_pa_scheme_score(struct damon_ctx *context, · line 390

Quoted 10 lines
mm/damon/paddr.c +10 -0
+	damon_for_each_target(t, ctx) {
+		struct damon_region *r;
+
+		damon_for_each_region(r, t) {
+			unsigned long addr;
+
+			if (r->access_reported)
+				continue;
+
+			for (addr = r->ar.start; addr < r->ar.end; addr += PAGE_SIZE) {

Does this directly use the core address as a physical byte address?

DAMON scales physical addresses down using ctx->addr_unit, meaning r->ar.start and r->ar.end hold these scaled core addresses. If ctx->addr_unit is greater than 1 (e.g., PAGE_SIZE), r->ar.start is actually a Page Frame Number. Incrementing a PFN by PAGE_SIZE would skip large blocks of memory per iteration. Should the code convert core addresses to physical addresses using damon_pa_phys_addr() first?

Could this increment overflow on 32-bit architectures? If a physical memory region ends near ULONG_MAX, addr += PAGE_SIZE might wrap around to 0. This would keep the addr < r->ar.end condition true indefinitely, causing an unkillable infinite loop.

Additionally, does this loop iterate over every single page in the region? Since DAMON regions can be extremely large, iterating over millions of pages and performing a synchronous XArray lookup for each one without yielding via cond_resched() could heavily saturate the CPU and stall RCU grace periods.

mm/damon/paddr.c static int damon_pa_scheme_score(struct damon_ctx *context, · line 392

Quoted 2 lines
mm/damon/paddr.c +2 -0
+				if (damon_paddr_histogram_count(&perf->paddr_histogram,
+									addr & PAGE_MASK)) {

Does the underlying XArray implementation safely handle 64-bit physical addresses on 32-bit systems?

The perf subsystem records the access address as a 64-bit u64 paddr, but the XArray API uses unsigned long for its indices. On systems using PAE to access more than 4GB of RAM, passing a 64-bit raw byte address to the XArray could truncate the upper 32 bits, causing physical addresses above 4GB to alias with lower addresses. Would using Page Frame Numbers as XArray indices instead of raw byte addresses avoid this?

Quoted 13 lines
mm/damon/paddr.c +13 -0
+					damon_update_region_access_rate(r, true, &ctx->attrs);
+					r->access_reported = true;
+					break;
+				}
+			}
+		}
+		tidx++;
+	}
+
+	damon_paddr_histogram_destroy(&perf->paddr_histogram);
+}
+
+#endif /* CONFIG_PERF_EVENTS */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
maintainer
#9 lore raw
In reply to Akinobu Mita

Hello Akinobu,

Quoted 6 lines · On Thu, 23 Apr 2026 09:42:06 +0900 Akinobu Mita <[email protected]> wrote:

DAMON currently only provides PTE accessed-bit based access check, this patch series adds a new perf event based access check.

Since perf event-based access checks do not require modifying the PTE accessed-bit for pages representing each damon region, it reduces the overhead of monitoring at a fixed granularity of the page size.

As I also commented to the previous version, the high level idea makes sense to me. I think this can be useful.

Also as I commented to the previous version, I understand the page level monitoring overhead reduction is the main purpose of this patch, and it makes sense to me. Nonetheless, I like this mainly because the perf event could provide more detailed information including from which CPU/thread the access is made, and whether the access is for read/write, to my understanding. Please let me know if my understanding is wrong.

Quoted 3 lines

Here is a method and its results for comparing access checks using existing access bits and new perf events.

[...]

Quoted 2 lines

Using accessed bit, prepare_access_checks takes 7 seconds and check_accesses takes 5 seconds.

[...]

Quoted 2 lines

Using perf event, prepare_access_checks takes 0.01 seconds and check_accesses takes 2.6 seconds.

Thank you so much for sharing these great test results with the detailed setup description!

Quoted 7 lines

```

$ sudo $HOME/damo/damo stop
$ sudo $HOME/damo/damo start --ops vaddr \
	--perf_event 5000 0 0x4 0x1cd 0x1f 0 \
	--monitoring_intervals 5s 60s 300s \
	--monitoring_nr_regions_range 1000000000 1000000000 \
	--target_pid $(cat /tmp/memcached.pid)

[...]

Quoted 7 lines

Note: damo's --perf_event option

Using these features also requires modifications to damo, but these are not included in this patch series and are currently under development in the following branch:

* https://github.com/mita/damo/tree/damo-perf-for-v3.2.2

Thank you for sharing this, too!

Quoted 5 lines

The option newly added to damo for perf event-based access check has the following format:

--perf_event <sample freq> <sample phys addr> <type> <config> <config1> <config2>

I think we may need to discuss more for final interface. But I think that could be done later, after the kerenl ABI is fixed.

Quoted 6 lines

- sample freq: A higher frequency improves access accuracy, but also increases overhead.
- sample phys addr: specify 0 for vaddr and 1 for paddr.
- The remaining type, config, config1, and config2 settings can be found as follows:

The values look bit human-unfriendly. I wonder if we could use more human-friendly values, e.g., 'cpu' for type, 'mem_trans_retired.load_latency_gt_1024' for config, etc. Not necessarily we need to fix this right now. Let's keep discussing.

Quoted 11 lines

Run perf mem record -vv to obtain the log, and then find the part in the log where perf_event_open was executed successfully. ```

$ sudo perf mem record -vv echo > /tmp/perf-mem-record.log 2>&1
$ cat /tmp/perf-mem-record.log
...
perf_event_attr:
  type                             4 (cpu)
  size                             144
  config                           0x1cd (mem_trans_retired.load_latency_gt_1024)

[...]

Quoted 6 lines

...
```

The values for type and config are taken from this output. Use the values of config1 and config2 if they are included in the log; otherwise, set them to 0.

The above information will also be very useful for early testers of this patch series including myself. Thank you for sharing.

Quoted 2 lines

Any feedback or advice on the patch set would be greatly appreciated.

Again, I like the high level concept of this patch series. I definitely want this to be merged. Nonetheless, not exactly as it is right now. I think we should spend enough time for the user interface design. I hope the final interface is long-term maintainable and easy to extend.

Quoted 9 lines

* RFC v3
- drop "reintroduce damon_operations->cleanup()" as no longer needed
- drop "allow user to set min size of region" (will be sent separately)
- add tgid to struct damon_access_report and use it instead of tid
- set perf_event_attr.precise_ip to 2
- prepare for the transition to report-based monitoring
- switch to use access_reported in struct damon_region
- switch to use sample/primitives/perf_events sysfs directory

Thank you for doing this revisioning.

For the user interface and also internal interface, I suggested to consider reusing those for page fault events based per-CPUs/threads/reads/writes monitoring DAMON extension patch series in our previous discussion. I think this version is following the right direction much more than the previous version.

But, nowadays I'm thinking about a new user interface for the per-CPUs/threads/reads/writes monitoring that can easily also extended for perf event based monitoring and for not only access monitoring but general and multiple data attributes monitoring.

It is still under the development and I'm planning to share a very early RFC version of that by LSFMMBPF. I will try to add my idea about how this perf event based monitoring could also implemented by reusing the interface on the early RFC version.

Could we wait until the early RFC version is published, review my idea about how this perf event based monitoring could reuse it, and continue discussions about the next steps of this patch series?

[1] https://lore.kernel.org/[email protected]/

Thanks,
SJ

[...]

#10 lore raw

Hello SeongJae,

2026年4月23日(木) 13:34 SeongJae Park <[email protected]>:

Quoted 15 lines

Hello Akinobu,

Quoted 6 lines · On Thu, 23 Apr 2026 09:42:06 +0900 Akinobu Mita <[email protected]> wrote:

DAMON currently only provides PTE accessed-bit based access check, this patch series adds a new perf event based access check.

Since perf event-based access checks do not require modifying the PTE accessed-bit for pages representing each damon region, it reduces the overhead of monitoring at a fixed granularity of the page size.

As I also commented to the previous version, the high level idea makes sense to me. I think this can be useful.

Quoted 6 lines

Also as I commented to the previous version, I understand the page level monitoring overhead reduction is the main purpose of this patch, and it makes sense to me. Nonetheless, I like this mainly because the perf event could provide more detailed information including from which CPU/thread the access is made, and whether the access is for read/write, to my understanding. Please let me know if my understanding is wrong.

That's correct.
Once the integration into your new monitoring infrastructure is complete, those things will become possible.

Quoted 32 lines
Quoted 3 lines

Here is a method and its results for comparing access checks using existing access bits and new perf events.

[...]

Quoted 2 lines

Using accessed bit, prepare_access_checks takes 7 seconds and check_accesses takes 5 seconds.

[...]

Quoted 2 lines

Using perf event, prepare_access_checks takes 0.01 seconds and check_accesses takes 2.6 seconds.

Thank you so much for sharing these great test results with the detailed setup description!

Quoted 7 lines

```

$ sudo $HOME/damo/damo stop
$ sudo $HOME/damo/damo start --ops vaddr \
      --perf_event 5000 0 0x4 0x1cd 0x1f 0 \
      --monitoring_intervals 5s 60s 300s \
      --monitoring_nr_regions_range 1000000000 1000000000 \
      --target_pid $(cat /tmp/memcached.pid)

[...]

Quoted 7 lines

Note: damo's --perf_event option

Using these features also requires modifications to damo, but these are not included in this patch series and are currently under development in the following branch:

* https://github.com/mita/damo/tree/damo-perf-for-v3.2.2

Thank you for sharing this, too!

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

The option newly added to damo for perf event-based access check has the following format:

--perf_event <sample freq> <sample phys addr> <type> <config> <config1> <config2>

I think we may need to discuss more for final interface. But I think that could be done later, after the kerenl ABI is fixed.

My current patch introduces a set of kernel ABIs (.../perf_events/<P>/*), and if I want to control another field in the perf_event_attr, I would have to add a new ABI. This would be a significant maintenance cost.

Instead, I would like to provide a single bin file where the perf_event_attr I want to set is written as binary.

Quoted 11 lines
Quoted 6 lines

- sample freq: A higher frequency improves access accuracy, but also increases overhead.
- sample phys addr: specify 0 for vaddr and 1 for paddr.
- The remaining type, config, config1, and config2 settings can be found as follows:

The values look bit human-unfriendly. I wonder if we could use more human-friendly values, e.g., 'cpu' for type, 'mem_trans_retired.load_latency_gt_1024' for config, etc. Not necessarily we need to fix this right now. Let's keep discussing.

I'd like to do it that way. These processes can be handled better from user space than from within the kernel, so I'd like to achieve this from damo with the help of the perf tool and library.

Quoted 53 lines
Quoted 11 lines

Run perf mem record -vv to obtain the log, and then find the part in the log where perf_event_open was executed successfully. ```

$ sudo perf mem record -vv echo > /tmp/perf-mem-record.log 2>&1
$ cat /tmp/perf-mem-record.log
...
perf_event_attr:
  type                             4 (cpu)
  size                             144
  config                           0x1cd (mem_trans_retired.load_latency_gt_1024)

[...]

Quoted 6 lines

...
```

The values for type and config are taken from this output. Use the values of config1 and config2 if they are included in the log; otherwise, set them to 0.

The above information will also be very useful for early testers of this patch series including myself. Thank you for sharing.

Quoted 2 lines

Any feedback or advice on the patch set would be greatly appreciated.

Again, I like the high level concept of this patch series. I definitely want this to be merged. Nonetheless, not exactly as it is right now. I think we should spend enough time for the user interface design. I hope the final interface is long-term maintainable and easy to extend.

Quoted 9 lines

* RFC v3
- drop "reintroduce damon_operations->cleanup()" as no longer needed
- drop "allow user to set min size of region" (will be sent separately)
- add tgid to struct damon_access_report and use it instead of tid
- set perf_event_attr.precise_ip to 2
- prepare for the transition to report-based monitoring
- switch to use access_reported in struct damon_region
- switch to use sample/primitives/perf_events sysfs directory

Thank you for doing this revisioning.

For the user interface and also internal interface, I suggested to consider reusing those for page fault events based per-CPUs/threads/reads/writes monitoring DAMON extension patch series in our previous discussion. I think this version is following the right direction much more than the previous version.

But, nowadays I'm thinking about a new user interface for the per-CPUs/threads/reads/writes monitoring that can easily also extended for perf event based monitoring and for not only access monitoring but general and multiple data attributes monitoring.

Quoted 8 lines

It is still under the development and I'm planning to share a very early RFC version of that by LSFMMBPF. I will try to add my idea about how this perf event based monitoring could also implemented by reusing the interface on the early RFC version.

Could we wait until the early RFC version is published, review my idea about how this perf event based monitoring could reuse it, and continue discussions about the next steps of this patch series?

That sounds good. I'd definitely like to check it out.

Quoted 7 lines

[1] https://lore.kernel.org/[email protected]/

Thanks,
SJ

[...]

maintainer
#11 lore raw
Quoted 25 lines · On Fri, 24 Apr 2026 12:27:07 +0900 Akinobu Mita <[email protected]> wrote:

Hello SeongJae,

2026年4月23日(木) 13:34 SeongJae Park <[email protected]>:

Quoted 15 lines

Hello Akinobu,

Quoted 6 lines · On Thu, 23 Apr 2026 09:42:06 +0900 Akinobu Mita <[email protected]> wrote:

DAMON currently only provides PTE accessed-bit based access check, this patch series adds a new perf event based access check.

Since perf event-based access checks do not require modifying the PTE accessed-bit for pages representing each damon region, it reduces the overhead of monitoring at a fixed granularity of the page size.

As I also commented to the previous version, the high level idea makes sense to me. I think this can be useful.

Quoted 6 lines

Also as I commented to the previous version, I understand the page level monitoring overhead reduction is the main purpose of this patch, and it makes sense to me. Nonetheless, I like this mainly because the perf event could provide more detailed information including from which CPU/thread the access is made, and whether the access is for read/write, to my understanding. Please let me know if my understanding is wrong.

Quoted 3 lines

That's correct.
Once the integration into your new monitoring infrastructure is complete, those things will become possible.

Thank you for confirming!

Quoted 41 lines
Quoted 33 lines
Quoted 4 lines

Here is a method and its results for comparing access checks using existing access bits and new perf events.

[...]

Quoted 2 lines

Using accessed bit, prepare_access_checks takes 7 seconds and check_accesses takes 5 seconds.

[...]

Quoted 2 lines

Using perf event, prepare_access_checks takes 0.01 seconds and check_accesses takes 2.6 seconds.

Thank you so much for sharing these great test results with the detailed setup description!

Quoted 7 lines

```

$ sudo $HOME/damo/damo stop
$ sudo $HOME/damo/damo start --ops vaddr \
      --perf_event 5000 0 0x4 0x1cd 0x1f 0 \
      --monitoring_intervals 5s 60s 300s \
      --monitoring_nr_regions_range 1000000000 1000000000 \
      --target_pid $(cat /tmp/memcached.pid)

[...]

Quoted 7 lines

Note: damo's --perf_event option

Using these features also requires modifications to damo, but these are not included in this patch series and are currently under development in the following branch:

* https://github.com/mita/damo/tree/damo-perf-for-v3.2.2

Thank you for sharing this, too!

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

The option newly added to damo for perf event-based access check has the following format:

--perf_event <sample freq> <sample phys addr> <type> <config> <config1> <config2>

I think we may need to discuss more for final interface. But I think that could be done later, after the kerenl ABI is fixed.

Quoted 7 lines

My current patch introduces a set of kernel ABIs (.../perf_events/<P>/*), and if I want to control another field in the perf_event_attr, I would have to add a new ABI. This would be a significant maintenance cost.

Instead, I would like to provide a single bin file where the perf_event_attr I want to set is written as binary.

I'd prefer having single file per parameter, as sysfs is designed for. It allows keeping the input format simple and therefore easy to maintain and extend. Of course, we should carefult at adding new parameters, though.

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

- sample freq: A higher frequency improves access accuracy, but also increases overhead.
- sample phys addr: specify 0 for vaddr and 1 for paddr.
- The remaining type, config, config1, and config2 settings can be found as follows:

Quoted 8 lines
Quoted 4 lines

The values look bit human-unfriendly. I wonder if we could use more human-friendly values, e.g., 'cpu' for type, 'mem_trans_retired.load_latency_gt_1024' for config, etc. Not necessarily we need to fix this right now. Let's keep discussing.

I'd like to do it that way. These processes can be handled better from user space than from within the kernel, so I'd like to achieve this from damo with the help of the perf tool and library.

Does existing perf event ABI also take that approach? If so, I think that is fine.

Quoted 59 lines
Quoted 59 lines
Quoted 12 lines

Run perf mem record -vv to obtain the log, and then find the part in the log where perf_event_open was executed successfully. ```

$ sudo perf mem record -vv echo > /tmp/perf-mem-record.log 2>&1
$ cat /tmp/perf-mem-record.log
...
perf_event_attr:
  type                             4 (cpu)
  size                             144
  config                           0x1cd (mem_trans_retired.load_latency_gt_1024)

[...]

Quoted 6 lines

...
```

The values for type and config are taken from this output. Use the values of config1 and config2 if they are included in the log; otherwise, set them to 0.

The above information will also be very useful for early testers of this patch series including myself. Thank you for sharing.

Quoted 2 lines

Any feedback or advice on the patch set would be greatly appreciated.

Again, I like the high level concept of this patch series. I definitely want this to be merged. Nonetheless, not exactly as it is right now. I think we should spend enough time for the user interface design. I hope the final interface is long-term maintainable and easy to extend.

Quoted 9 lines

* RFC v3
- drop "reintroduce damon_operations->cleanup()" as no longer needed
- drop "allow user to set min size of region" (will be sent separately)
- add tgid to struct damon_access_report and use it instead of tid
- set perf_event_attr.precise_ip to 2
- prepare for the transition to report-based monitoring
- switch to use access_reported in struct damon_region
- switch to use sample/primitives/perf_events sysfs directory

Thank you for doing this revisioning.

For the user interface and also internal interface, I suggested to consider reusing those for page fault events based per-CPUs/threads/reads/writes monitoring DAMON extension patch series in our previous discussion. I think this version is following the right direction much more than the previous version.

But, nowadays I'm thinking about a new user interface for the per-CPUs/threads/reads/writes monitoring that can easily also extended for perf event based monitoring and for not only access monitoring but general and multiple data attributes monitoring.

It is still under the development and I'm planning to share a very early RFC version of that by LSFMMBPF. I will try to add my idea about how this perf event based monitoring could also implemented by reusing the interface on the early RFC version.

Quoted 5 lines
Quoted 3 lines

Could we wait until the early RFC version is published, review my idea about how this perf event based monitoring could reuse it, and continue discussions about the next steps of this patch series?

That sounds good. I'd definitely like to check it out.

Thank you for accepting my humble suggestion. And sorry for making you be delayed. Hopefully this interface extension will soon be published as it is one of the topics for DAMON session [1] at LSFMMBPF.

Quoted 2 lines
Quoted 2 lines

[1] https://lore.kernel.org/[email protected]/

[1] https://docs.google.com/spreadsheets/d/1mGEdDrWskp7Ua91jGXzquQGinorcD58DAVXhOiRp2Gg/edit?gid=1852749899#gid=1852749899

Thanks,
SJ

[...]

#12 lore raw

2026年4月25日(土) 8:31 SeongJae Park <[email protected]>:

Quoted 80 lines · On Fri, 24 Apr 2026 12:27:07 +0900 Akinobu Mita <[email protected]> wrote:
Quoted 26 lines

Hello SeongJae,

2026年4月23日(木) 13:34 SeongJae Park <[email protected]>:

Quoted 15 lines

Hello Akinobu,

Quoted 6 lines · On Thu, 23 Apr 2026 09:42:06 +0900 Akinobu Mita <[email protected]> wrote:

DAMON currently only provides PTE accessed-bit based access check, this patch series adds a new perf event based access check.

Since perf event-based access checks do not require modifying the PTE accessed-bit for pages representing each damon region, it reduces the overhead of monitoring at a fixed granularity of the page size.

As I also commented to the previous version, the high level idea makes sense to me. I think this can be useful.

Quoted 6 lines

Also as I commented to the previous version, I understand the page level monitoring overhead reduction is the main purpose of this patch, and it makes sense to me. Nonetheless, I like this mainly because the perf event could provide more detailed information including from which CPU/thread the access is made, and whether the access is for read/write, to my understanding. Please let me know if my understanding is wrong.

Quoted 3 lines

That's correct.
Once the integration into your new monitoring infrastructure is complete, those things will become possible.

Thank you for confirming!

Quoted 46 lines
Quoted 33 lines
Quoted 4 lines

Here is a method and its results for comparing access checks using existing access bits and new perf events.

[...]

Quoted 2 lines

Using accessed bit, prepare_access_checks takes 7 seconds and check_accesses takes 5 seconds.

[...]

Quoted 2 lines

Using perf event, prepare_access_checks takes 0.01 seconds and check_accesses takes 2.6 seconds.

Thank you so much for sharing these great test results with the detailed setup description!

Quoted 7 lines

```

$ sudo $HOME/damo/damo stop
$ sudo $HOME/damo/damo start --ops vaddr \
      --perf_event 5000 0 0x4 0x1cd 0x1f 0 \
      --monitoring_intervals 5s 60s 300s \
      --monitoring_nr_regions_range 1000000000 1000000000 \
      --target_pid $(cat /tmp/memcached.pid)

[...]

Quoted 7 lines

Note: damo's --perf_event option

Using these features also requires modifications to damo, but these are not included in this patch series and are currently under development in the following branch:

* https://github.com/mita/damo/tree/damo-perf-for-v3.2.2

Thank you for sharing this, too!

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

The option newly added to damo for perf event-based access check has the following format:

--perf_event <sample freq> <sample phys addr> <type> <config> <config1> <config2>

I think we may need to discuss more for final interface. But I think that could be done later, after the kerenl ABI is fixed.

My current patch introduces a set of kernel ABIs (.../perf_events/<P>/*), and if I want to control another field in the perf_event_attr, I would have to add a new ABI. This would be a significant maintenance cost.

Quoted 6 lines
Quoted 2 lines

Instead, I would like to provide a single bin file where the perf_event_attr I want to set is written as binary.

I'd prefer having single file per parameter, as sysfs is designed for. It allows keeping the input format simple and therefore easy to maintain and extend. Of course, we should carefult at adding new parameters, though.

Another idea from Namhyung Kim [1] is to fix the events specified by the CPU architecture being executed.

Of course, we would want to control a limited number of parameters, such as sample_freq.

[1] https://lore.kernel.org/damon/aZwLDAxf9eP0lPdD@z2/

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 7 lines

- sample freq: A higher frequency improves access accuracy, but also increases overhead.
- sample phys addr: specify 0 for vaddr and 1 for paddr.
- The remaining type, config, config1, and config2 settings can be found as follows:

The values look bit human-unfriendly. I wonder if we could use more human-friendly values, e.g., 'cpu' for type, 'mem_trans_retired.load_latency_gt_1024' for config, etc. Not necessarily we need to fix this right now. Let's keep discussing.

Quoted 6 lines
Quoted 3 lines

I'd like to do it that way. These processes can be handled better from user space than from within the kernel, so I'd like to achieve this from damo with the help of the perf tool and library.

Does existing perf event ABI also take that approach? If so, I think that is fine.

On Intel CPUs, you can read the mem-loads and/or mem-stores files as follows to find the value that should be set to perf_event_attr:

$ cat /sys/bus/event_source/devices/cpu/events/mem-loads
event=0xcd,umask=0x1,ldlat=3

You can find out which bit field of which member of perf_event_attr should be used to set these values from the corresponding files in the format directory.

$ cat /sys/bus/event_source/devices/cpu/format/{event,umask,ldlat}
config:0-7
config:8-15
config1:0-15

This conversion from symbolic event name to values that should be set in perf_event_attr can be done, for example, as shown below, so I think it should be implemented using perf in some way.

import perf

if __name__ == '__main__':
evlist = perf.parse_events("mem-loads")
for evsel in evlist:
print(f"{evsel}: type={evsel.type} config={evsel.config}")

maintainer
#13 lore raw
Quoted 7 lines · On Sat, 25 Apr 2026 21:33:07 +0900 Akinobu Mita <[email protected]> wrote:

2026年4月25日(土) 8:31 SeongJae Park <[email protected]>:

Quoted 6 lines · On Fri, 24 Apr 2026 12:27:07 +0900 Akinobu Mita <[email protected]> wrote:
Quoted 4 lines

Hello SeongJae,

2026年4月23日(木) 13:34 SeongJae Park <[email protected]>:

[...]

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

My current patch introduces a set of kernel ABIs (.../perf_events/<P>/*), and if I want to control another field in the perf_event_attr, I would have to add a new ABI. This would be a significant maintenance cost.

Instead, I would like to provide a single bin file where the perf_event_attr I want to set is written as binary.

Quoted 6 lines
Quoted 3 lines

I'd prefer having single file per parameter, as sysfs is designed for. It allows keeping the input format simple and therefore easy to maintain and extend. Of course, we should carefult at adding new parameters, though.

Another idea from Namhyung Kim [1] is to fix the events specified by the CPU architecture being executed.

I think I need to study mroe about perf events :)

Quoted 46 lines

Of course, we would want to control a limited number of parameters, such as sample_freq.

[1] https://lore.kernel.org/damon/aZwLDAxf9eP0lPdD@z2/

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 7 lines

- sample freq: A higher frequency improves access accuracy, but also increases overhead.
- sample phys addr: specify 0 for vaddr and 1 for paddr.
- The remaining type, config, config1, and config2 settings can be found as follows:

The values look bit human-unfriendly. I wonder if we could use more human-friendly values, e.g., 'cpu' for type, 'mem_trans_retired.load_latency_gt_1024' for config, etc. Not necessarily we need to fix this right now. Let's keep discussing.

Quoted 6 lines
Quoted 3 lines

I'd like to do it that way. These processes can be handled better from user space than from within the kernel, so I'd like to achieve this from damo with the help of the perf tool and library.

Does existing perf event ABI also take that approach? If so, I think that is fine.

On Intel CPUs, you can read the mem-loads and/or mem-stores files as follows to find the value that should be set to perf_event_attr:

$ cat /sys/bus/event_source/devices/cpu/events/mem-loads
event=0xcd,umask=0x1,ldlat=3

You can find out which bit field of which member of perf_event_attr should be used to set these values from the corresponding files in the format directory.

$ cat /sys/bus/event_source/devices/cpu/format/{event,umask,ldlat}
config:0-7
config:8-15
config1:0-15

This conversion from symbolic event name to values that should be set in perf_event_attr can be done, for example, as shown below, so I think it should be implemented using perf in some way.

Quoted 6 lines

import perf

if __name__ == '__main__':
evlist = perf.parse_events("mem-loads")
for evsel in evlist:
print(f"{evsel}: type={evsel.type} config={evsel.config}")

I definitely need to study mroe about perf event. I will comment after I learn some fundamentals. But I believe this is not a blocker of this work.

Thanks,
SJ

#14 lore raw
In reply to Akinobu Mita

Hello,

Quoted 136 lines · Akinobu Mita

2026年4月25日(土) 8:31 SeongJae Park <[email protected]>:

Quoted 80 lines · On Fri, 24 Apr 2026 12:27:07 +0900 Akinobu Mita <[email protected]> wrote:
Quoted 26 lines

Hello SeongJae,

2026年4月23日(木) 13:34 SeongJae Park <[email protected]>:

Quoted 15 lines

Hello Akinobu,

Quoted 6 lines · On Thu, 23 Apr 2026 09:42:06 +0900 Akinobu Mita <[email protected]> wrote:

DAMON currently only provides PTE accessed-bit based access check, this patch series adds a new perf event based access check.

Since perf event-based access checks do not require modifying the PTE accessed-bit for pages representing each damon region, it reduces the overhead of monitoring at a fixed granularity of the page size.

As I also commented to the previous version, the high level idea makes sense to me. I think this can be useful.

Quoted 6 lines

Also as I commented to the previous version, I understand the page level monitoring overhead reduction is the main purpose of this patch, and it makes sense to me. Nonetheless, I like this mainly because the perf event could provide more detailed information including from which CPU/thread the access is made, and whether the access is for read/write, to my understanding. Please let me know if my understanding is wrong.

Quoted 3 lines

That's correct.
Once the integration into your new monitoring infrastructure is complete, those things will become possible.

Thank you for confirming!

Quoted 46 lines
Quoted 33 lines
Quoted 4 lines

Here is a method and its results for comparing access checks using existing access bits and new perf events.

[...]

Quoted 2 lines

Using accessed bit, prepare_access_checks takes 7 seconds and check_accesses takes 5 seconds.

[...]

Quoted 2 lines

Using perf event, prepare_access_checks takes 0.01 seconds and check_accesses takes 2.6 seconds.

Thank you so much for sharing these great test results with the detailed setup description!

Quoted 7 lines

```

$ sudo $HOME/damo/damo stop
$ sudo $HOME/damo/damo start --ops vaddr \
      --perf_event 5000 0 0x4 0x1cd 0x1f 0 \
      --monitoring_intervals 5s 60s 300s \
      --monitoring_nr_regions_range 1000000000 1000000000 \
      --target_pid $(cat /tmp/memcached.pid)

[...]

Quoted 7 lines

Note: damo's --perf_event option

Using these features also requires modifications to damo, but these are not included in this patch series and are currently under development in the following branch:

* https://github.com/mita/damo/tree/damo-perf-for-v3.2.2

Thank you for sharing this, too!

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

The option newly added to damo for perf event-based access check has the following format:

--perf_event <sample freq> <sample phys addr> <type> <config> <config1> <config2>

I think we may need to discuss more for final interface. But I think that could be done later, after the kerenl ABI is fixed.

My current patch introduces a set of kernel ABIs (.../perf_events/<P>/*), and if I want to control another field in the perf_event_attr, I would have to add a new ABI. This would be a significant maintenance cost.

Quoted 6 lines
Quoted 2 lines

Instead, I would like to provide a single bin file where the perf_event_attr I want to set is written as binary.

I'd prefer having single file per parameter, as sysfs is designed for. It allows keeping the input format simple and therefore easy to maintain and extend. Of course, we should carefult at adding new parameters, though.

Another idea from Namhyung Kim [1] is to fix the events specified by the CPU architecture being executed.

Of course, we would want to control a limited number of parameters, such as sample_freq.

[1] https://lore.kernel.org/damon/aZwLDAxf9eP0lPdD@z2/

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 7 lines

- sample freq: A higher frequency improves access accuracy, but also increases overhead.
- sample phys addr: specify 0 for vaddr and 1 for paddr.
- The remaining type, config, config1, and config2 settings can be found as follows:

The values look bit human-unfriendly. I wonder if we could use more human-friendly values, e.g., 'cpu' for type, 'mem_trans_retired.load_latency_gt_1024' for config, etc. Not necessarily we need to fix this right now. Let's keep discussing.

Quoted 6 lines
Quoted 3 lines

I'd like to do it that way. These processes can be handled better from user space than from within the kernel, so I'd like to achieve this from damo with the help of the perf tool and library.

Does existing perf event ABI also take that approach? If so, I think that is fine.

On Intel CPUs, you can read the mem-loads and/or mem-stores files as follows to find the value that should be set to perf_event_attr:

$ cat /sys/bus/event_source/devices/cpu/events/mem-loads
event=0xcd,umask=0x1,ldlat=3

You can find out which bit field of which member of perf_event_attr should be used to set these values from the corresponding files in the format directory.

$ cat /sys/bus/event_source/devices/cpu/format/{event,umask,ldlat}
config:0-7
config:8-15
config1:0-15

This conversion from symbolic event name to values that should be set in perf_event_attr can be done, for example, as shown below, so I think it should be implemented using perf in some way.

Quoted 6 lines

import perf

if __name__ == '__main__':
evlist = perf.parse_events("mem-loads")
for evsel in evlist:
print(f"{evsel}: type={evsel.type} config={evsel.config}")

It'd be great if we could have something like this. Currently we have libperf (in C) in tools/lib/perf but it doesn't have the parser yet.

Anyway there are vendor-contributed event/metric description in JSON under tools/perf/pmu-events/arch directory. You may extract the event encoding from the JSON and convert it using the sysfs format info.

On Alderlake, the 'mem_trans_retired.load_latency_gt_1024' is defined like below:

tools/perf/pmu-events/arch/x86/alderlake/memory.json +128

    {
        "BriefDescription": "Counts randomly selected loads when the latency from first dispatch to completion is greater than 1024 cycles.",
        "Counter": "1,2,3,4,5,6,7",
        "Data_LA": "1",
        "EventCode": "0xcd",
        "EventName": "MEM_TRANS_RETIRED.LOAD_LATENCY_GT_1024",
        "MSRIndex": "0x3F6",
        "MSRValue": "0x400",
        "PublicDescription": "Counts randomly selected loads when the latency from first dispatch to completion is greater than 1024 cycles.  Reported latency may be longer than just the memory latency.",
        "SampleAfterValue": "53",
        "UMask": "0x1",
        "Unit": "cpu_core"
    },

I think MSRValue is for attr.config1.

Thanks,
Namhyung

maintainer
#15 lore raw
Quoted 4 lines · On Wed, 29 Apr 2026 01:00:50 -0700 Namhyung Kim <[email protected]> wrote:

Hello,

Quoted 1 line · Akinobu Mita

2026年4月25日(土) 8:31 SeongJae Park <[email protected]>:

[...]

Quoted 33 lines
Quoted 6 lines

import perf

if __name__ == '__main__':
evlist = perf.parse_events("mem-loads")
for evsel in evlist:
print(f"{evsel}: type={evsel.type} config={evsel.config}")

It'd be great if we could have something like this. Currently we have libperf (in C) in tools/lib/perf but it doesn't have the parser yet.

Anyway there are vendor-contributed event/metric description in JSON under tools/perf/pmu-events/arch directory. You may extract the event encoding from the JSON and convert it using the sysfs format info.

On Alderlake, the 'mem_trans_retired.load_latency_gt_1024' is defined like below:

tools/perf/pmu-events/arch/x86/alderlake/memory.json +128

    {
        "BriefDescription": "Counts randomly selected loads when the latency from first dispatch to completion is greater than 1024 cycles.",
        "Counter": "1,2,3,4,5,6,7",
        "Data_LA": "1",
        "EventCode": "0xcd",
        "EventName": "MEM_TRANS_RETIRED.LOAD_LATENCY_GT_1024",
        "MSRIndex": "0x3F6",
        "MSRValue": "0x400",
        "PublicDescription": "Counts randomly selected loads when the latency from first dispatch to completion is greater than 1024 cycles.  Reported latency may be longer than just the memory latency.",
        "SampleAfterValue": "53",
        "UMask": "0x1",
        "Unit": "cpu_core"
    },
Quoted 1 line

I think MSRValue is for attr.config1.

Oh great. Thank you for kindly sharing this Namhyung! This makes me feel perf event based DAMON monitoring even more promising!

Thanks,
SJ

[...]