linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: SeongJae Park <sj@kernel.org>
Cc: SeongJae Park <sj@kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	damon@lists.linux.dev, linux-kernel@vger.kernel.org,
	linux-mm@kvack.org
Subject: [RFC PATCH v3 20/37] mm/damon/sysfs: implement sample filter directory
Date: Sun,  7 Dec 2025 22:29:24 -0800	[thread overview]
Message-ID: <20251208062943.68824-21-sj@kernel.org> (raw)
In-Reply-To: <20251208062943.68824-1-sj@kernel.org>

Implement sysfs directory for setting individual DAMON sample filters.

Signed-off-by: SeongJae Park <sj@kernel.org>
---
 mm/damon/sysfs.c | 122 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 122 insertions(+)

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 4208fed2b8df..ba0c76c1300c 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -749,12 +749,46 @@ static const struct kobj_type damon_sysfs_intervals_ktype = {
 	.default_groups = damon_sysfs_intervals_groups,
 };
 
+/*
+ * access check report filter directory
+ */
+
+struct damon_sysfs_sample_filter {
+	struct kobject kobj;
+};
+
+static struct damon_sysfs_sample_filter *damon_sysfs_sample_filter_alloc(void)
+{
+	return kzalloc(sizeof(struct damon_sysfs_sample_filter), GFP_KERNEL);
+}
+
+static void damon_sysfs_sample_filter_release(struct kobject *kobj)
+{
+	struct damon_sysfs_sample_filter *filter = container_of(kobj,
+			struct damon_sysfs_sample_filter, kobj);
+
+	kfree(filter);
+}
+
+static struct attribute *damon_sysfs_sample_filter_attrs[] = {
+	NULL,
+};
+ATTRIBUTE_GROUPS(damon_sysfs_sample_filter);
+
+static const struct kobj_type damon_sysfs_sample_filter_ktype = {
+	.release = damon_sysfs_sample_filter_release,
+	.sysfs_ops = &kobj_sysfs_ops,
+	.default_groups = damon_sysfs_sample_filter_groups,
+};
+
 /*
  * access check report filters directory
  */
 
 struct damon_sysfs_sample_filters {
 	struct kobject kobj;
+	struct damon_sysfs_sample_filter **filters_arr;
+	int nr;
 };
 
 static struct damon_sysfs_sample_filters *
@@ -763,12 +797,99 @@ damon_sysfs_sample_filters_alloc(void)
 	return kzalloc(sizeof(struct damon_sysfs_sample_filters), GFP_KERNEL);
 }
 
+static void damon_sysfs_sample_filters_rm_dirs(
+		struct damon_sysfs_sample_filters *filters)
+{
+	struct damon_sysfs_sample_filter **filters_arr = filters->filters_arr;
+	int i;
+
+	for (i = 0; i < filters->nr; i++)
+		kobject_put(&filters_arr[i]->kobj);
+	filters->nr = 0;
+	kfree(filters_arr);
+	filters->filters_arr = NULL;
+}
+
+static int damon_sysfs_sample_filters_add_dirs(
+		struct damon_sysfs_sample_filters *filters, int nr_filters)
+{
+	struct damon_sysfs_sample_filter **filters_arr, *filter;
+	int err, i;
+
+	damon_sysfs_sample_filters_rm_dirs(filters);
+	if (!nr_filters)
+		return 0;
+
+	filters_arr = kmalloc_array(nr_filters, sizeof(*filters_arr),
+			GFP_KERNEL | __GFP_NOWARN);
+	if (!filters_arr)
+		return -ENOMEM;
+	filters->filters_arr = filters_arr;
+
+	for (i = 0; i < nr_filters; i++) {
+		filter = damon_sysfs_sample_filter_alloc();
+		if (!filter) {
+			damon_sysfs_sample_filters_rm_dirs(filters);
+			return -ENOMEM;
+		}
+
+		err = kobject_init_and_add(&filter->kobj,
+				&damon_sysfs_sample_filter_ktype,
+				&filters->kobj, "%d", i);
+		if (err) {
+			kobject_put(&filter->kobj);
+			damon_sysfs_sample_filters_rm_dirs(filters);
+			return err;
+		}
+
+		filters_arr[i] = filter;
+		filters->nr++;
+	}
+	return 0;
+}
+
+static ssize_t nr_filters_show(struct kobject *kobj,
+		struct kobj_attribute *attr, char *buf)
+{
+	struct damon_sysfs_sample_filters *filters = container_of(kobj,
+			struct damon_sysfs_sample_filters, kobj);
+
+	return sysfs_emit(buf, "%d\n", filters->nr);
+}
+
+static ssize_t nr_filters_store(struct kobject *kobj,
+		struct kobj_attribute *attr, const char *buf, size_t count)
+{
+	struct damon_sysfs_sample_filters *filters;
+	int nr, err = kstrtoint(buf, 0, &nr);
+
+	if (err)
+		return err;
+	if (nr < 0)
+		return -EINVAL;
+
+	filters = container_of(kobj, struct damon_sysfs_sample_filters, kobj);
+
+	if (!mutex_trylock(&damon_sysfs_lock))
+		return -EBUSY;
+	err = damon_sysfs_sample_filters_add_dirs(filters, nr);
+	mutex_unlock(&damon_sysfs_lock);
+	if (err)
+		return err;
+
+	return count;
+}
+
 static void damon_sysfs_sample_filters_release(struct kobject *kobj)
 {
 	kfree(container_of(kobj, struct damon_sysfs_sample_filters, kobj));
 }
 
+static struct kobj_attribute damon_sysfs_sample_filters_nr_attr =
+		__ATTR_RW_MODE(nr_filters, 0600);
+
 static struct attribute *damon_sysfs_sample_filters_attrs[] = {
+	&damon_sysfs_sample_filters_nr_attr.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(damon_sysfs_sample_filters);
@@ -940,6 +1061,7 @@ static void damon_sysfs_sample_rm_dirs(
 	if (sample->primitives)
 		kobject_put(&sample->primitives->kobj);
 	if (sample->filters) {
+		damon_sysfs_sample_filters_rm_dirs(sample->filters);
 		kobject_put(&sample->filters->kobj);
 	}
 }
-- 
2.47.3


  parent reply	other threads:[~2025-12-08  6:30 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-08  6:29 [RFC PATCH v3 00/37] mm/damon: introduce per-CPUs/threads/write/read monitoring SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 01/37] mm/damon/core: implement damon_report_access() SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 02/37] mm/damon: define struct damon_sample_control SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 03/37] mm/damon/core: commit damon_sample_control SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 04/37] mm/damon/core: implement damon_report_page_fault() SeongJae Park
2025-12-12 12:46   ` JaeJoon Jung
2025-12-12 22:47     ` SeongJae Park
2025-12-13  0:31       ` JaeJoon Jung
2025-12-13  0:56         ` SeongJae Park
2025-12-13  1:37           ` JaeJoon Jung
2025-12-08  6:29 ` [RFC PATCH v3 05/37] mm/{mprotect,memory}: (no upstream-aimed hack) implement MM_CP_DAMON SeongJae Park
2025-12-08 11:19   ` David Hildenbrand (Red Hat)
2025-12-09  4:56     ` SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 06/37] mm/damon/paddr: support page fault access check primitive SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 07/37] mm/damon/core: apply access reports to high level snapshot SeongJae Park
2025-12-12 13:20   ` JaeJoon Jung
2025-12-12 23:11     ` SeongJae Park
2025-12-13  1:10       ` JaeJoon Jung
2025-12-13  3:21         ` SeongJae Park
2025-12-13  4:09           ` JaeJoon Jung
2025-12-13  5:53             ` SeongJae Park
2025-12-13  9:17               ` SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 08/37] mm/damon/sysfs: implement monitoring_attrs/sample/ dir SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 09/37] mm/damon/sysfs: implement sample/primitives/ dir SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 10/37] mm/damon/sysfs: connect primitives directory with core SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 11/37] Docs/mm/damon/design: document page fault sampling primitive SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 12/37] Docs/admin-guide/mm/damon/usage: document sample primitives dir SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 13/37] mm/damon: extend damon_access_report for origin CPU reporting SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 14/37] mm/damon/core: report access origin cpu of page faults SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 15/37] mm/damon: implement sample filter data structure for cpus-only monitoring SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 16/37] mm/damon/core: implement damon_sample_filter manipulations SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 17/37] mm/damon/core: commit damon_sample_filters SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 18/37] mm/damon/core: apply sample filter to access reports SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 19/37] mm/damon/sysfs: implement sample/filters/ directory SeongJae Park
2025-12-08  6:29 ` SeongJae Park [this message]
2025-12-08  6:29 ` [RFC PATCH v3 21/37] mm/damon/sysfs: implement type, matching, allow files under sample filter dir SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 22/37] mm/damon/sysfs: implement cpumask file " SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 23/37] mm/damon/sysfs: connect sample filters with core layer SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 24/37] Docs/mm/damon/design: document sample filters SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 25/37] Docs/admin-guide/mm/damon/usage: document sample filters dir SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 26/37] mm/damon: extend damon_access_report for access-origin thread info SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 27/37] mm/damon/core: report access-generated thread id of the fault event SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 28/37] mm/damon: extend damon_sample_filter for threads SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 29/37] mm/damon/core: support threads type sample filter SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 30/37] mm/damon/sysfs: support thread based access sample filtering SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 31/37] Docs/mm/damon/design: document threads type sample filter SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 32/37] Docs/admin-guide/mm/damon/usage: document tids_arr file SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 33/37] mm/damon: support reporting write access SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 34/37] mm/damon/core: report whether the page fault was for writing SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 35/37] mm/damon/core: support write access sample filter SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 36/37] mm/damon/sysfs: support write-type " SeongJae Park
2025-12-08  6:29 ` [RFC PATCH v3 37/37] Docs/mm/damon/design: document write access sample filter type SeongJae Park

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251208062943.68824-21-sj@kernel.org \
    --to=sj@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=damon@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).