Skip to contentSkip to conversation

Introduce DAMON sysfs interface

RFC PATCH New SeongJae Park 2 participants 6 messages Feb 17, 2022

Subsystems (3)
lore
Share & tools
lore

SeongJae Park

author maintainer
#0 lore raw

cover Introduce DAMON sysfs interface

DAMON's debugfs-based user interface served very well, so far. However, it unnecessarily depends on debugfs, while DAMON is not aimed to be used for only debugging. Also, the interface receives multiple values via one file. For example, schemes file receives 18 values. As a result, it is not only hard to be used, but also difficult to be extended. Especially, keeping backward compatibility of user space tools is getting only challenging. It would be better to implement another reliable and flexible interface and deprecate the debugfs interface in long term.

For the reason, this patchset introduces a sysfs-based new user interface of DAMON. The idea of the new interface is, using directory hierarchies and making one file for one value. For a short example, users can do the virtual address monitoring via the interface as below:

    # cd /sys/kernel/mm/damon/admin/
    # echo 1 > kdamonds/nr
    # echo 1 > kdamonds/0/contexts/nr
    # echo vaddr > kdamonds/0/contexts/0/damon_type
    # echo 1 > kdamonds/0/contexts/0/targets/nr
    # echo $(pidof <workload>) > kdamonds/0/contexts/0/targets/0/pid
    # echo on > kdamonds/0/state

Changes that will be made for dropping RFC tag
----------------------------------------------

As the RFC tag means, this is not for merged in the mainline but only for early comments. Therefore, this version is not implementing all the features of DAMON debugfs interface but only basic virtual address space monitoring. The official version of the patchset will provide all the features that DAMON debugfs interface is providing. Also, this RFC patchset lacks formal documentation. It will be a part of the official version.

Future plan of DAMON debugfs interface deprecation
--------------------------------------------------

Once the official version of this patchset is merged, DAMON debugfs interface development will be frozen. That is, we will keep the interface works as is now, but it will not provide any new feature of DAMON. The support will be continued only until next LTS release. After that, we will drop DAMON debugfs interface. Changes to the documentation for explicitly announcing the deprecation plan would be a part of the official version of this patchset.

Main difference between DAMON_DBGFS and DAMON_SYSFS
---------------------------------------------------

DAMON debugfs interface allows multiple monitoring contexts, but it asks users to turn those all on and off at once. It's not a big problem but makes the operation a little bit complex. DAMON_SYSFS allows users to turn on and off monitoring contexts individually.

The Hierarchy
=============

In a glance, the files hierarchy of the sysfs interface is as below.

/sys/kernel/mm/damon/admin
│ kdamonds
│ │ nr
│ │ 0/
│ │ │ state,pid
│ │ │ contexts
│ │ │ │ nr
│ │ │ │ 0/
│ │ │ │ │ damon_type
│ │ │ │ │ monitoring_attrs/
│ │ │ │ │ │ intervals/sample_us,aggr_us,update_us │ │ │ │ │ │ nr_regions/min,max
│ │ │ │ │ targets/
│ │ │ │ │ │ nr
│ │ │ │ │ │ 0/
│ │ │ │ │ │ │ pid
│ │ │ │ │ │ │ regions/
│ │ │ │ │ │ │ │ nr
│ │ │ │ │ │ │ │ 0/
│ │ │ │ │ │ │ │ │ start,end
│ │ │ │ │ │ │ │ ...
│ │ │ │ │ │ ...
│ │ │ │ │ ...
│ │ ...

Root
----

The root of the DAMON sysfs is /sys/kernel/mm/damon/, and it has one directory named 'admin'. The directory contains the interface for privileged user space programs. User space tools or deamons having root permission could use this directory. In a future, sibling directories for non-root user space tools or deamons (we could allow them to do monitong of their own virtual address space) or control of in-kernel DAMON-based deamons could be created.

kdamonds/
---------

The monitoring-related information including request specifications and results are called DAMON context. DAMON executes a set of the contexts with a kernel thread called kdamond (for now, only one context per kdamond is supported), and multiple kdamonds could run in parallel. This directory has files for controlling the kdamonds.

In the beginning, this directory has only one file, 'nr'. Writing a number (N) to the file creates the number of child directories named 0 to N-1. Each directory represents each kdamond.

kdamonds/<N>/
-------------

In each kdamond directory, two files (state and pid) and one directory (contexts) reside.

Reading state returns on if the kdamond is currently running, or off if it is not running. Writing on or off makes the kdamond be in the state. If the state is on, reading pid shows the pid of the kdamond thread.

contexts directory resembles kdamonds. It contains files for controlling the monitoring contexts that this kdamond will execute.

kdamonds/<N>/contexts/
----------------------

In the beginning, this directory has only one file, 'nr'. Writing a number (N) to the file creates the number of child directories named as 0 to N-1. Each directory represents each monitoring context. At the moment, only one context per kdamond is supported, so only 0 or 1 can be written to the file.

contexts/<N>/
-------------

In each context directory, one file (operations) and two directories (monitoring_attrs and targets) reside.

DAMON supports multiple types of monitoring operations, including those for virtual address space and physical address space. You can set and show what type of monitoring operations you want to use with the context by writing one of below keywords to, and reading the file.

- vaddr: Monitor virtual address spaces of specific processes

Files for specifying attributes of the monitoring including required quality and efficiency of the monitoring are in monitoring_attrs directory, while files for specifying to what memory regions the monitoring should be done are in targets directory.

contexts/<N>/monitoring_attrs/
------------------------------

In this directory, you can show two directories, intervals and nr_regions.

Under intervals directory, three files for DAMON's sampling interval (sample_us), aggregation interval (aggr_us) and update interval (update_us) exist. You can set and get the values by writing to and reading from the files.

Under nr_regions directory, two files for the lower-bound and upper-bound of DAMON's monitoring regions (min and max, respectively), which controls the monitoring overhead, reside. You can set and get the values by writing to and rading from the files.

For more details about the intervals and monitoring regions range, please read the Design document[1].

[1] https://docs.kernel.org/vm/damon/design.html

contexts/<N>/targets/
---------------------

In the beginning, this directory has only one file, 'nr'. Writing a number (N) to the file creates the number of child directories named 0 to N-1. Each directory represents each monitoring target.

targets/<N>/
------------

In each target directory, one file (pid) exists.

You can make the context to monitor the virtual address space of a process by writing the pid of the process to the file, and show what process's virtual address space the context is set to monitor by reading the file.

SeongJae Park (4):
mm/damon: Implement a sysfs-based DAMON user interface mm/damon/core: Allow non-exclusive DAMON start/stop mm/damon/sysfs: Link DAMON to 'state' file read/write functions selftests/damon: Add a test for DAMON sysfs interface

 include/linux/damon.h                  |    2 +-
 mm/damon/Kconfig                       |    7 +
 mm/damon/Makefile                      |    1 +
 mm/damon/core.c                        |   22 +-
 mm/damon/dbgfs.c                       |    2 +-
 mm/damon/reclaim.c                     |    2 +-
 mm/damon/sysfs.c                       | 1258 ++++++++++++++++++++++++
 tools/testing/selftests/damon/Makefile |    1 +
 tools/testing/selftests/damon/sysfs.sh |  200 ++++
 9 files changed, 1486 insertions(+), 9 deletions(-)
 create mode 100644 mm/damon/sysfs.c
 create mode 100755 tools/testing/selftests/damon/sysfs.sh
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

RFC PATCH 1/4 mm/damon: Implement a sysfs-based DAMON user interface

DAMON's debugfs-based user interface served very well, so far. However, it unnecessarily depends on debugfs, while DAMON is not aimed to be used for only debugging. Also, the interface receives multiple values via one file. For example, schemes file receives 18 values. As a result, it is not only hard to be used, but also difficult to be extended. Especially, keeping backward compatibility of user space tools is getting only challenging. It would be better to implement another reliable and flexible interface and deprecate the debugfs interface in long term.

For the reason, this commit implements a stub of a part of the sysfs-based new user interface of DAMON. Specifically, this commit implements only the sysfs control part for virtual address space monitoring. Commits for linking with DAMON and support of more features that debugfs is currently providing will follow.

The idea of the new interface is, using directory hierarchies and making one file for one value. For a short example, users can do the virtual address monitoring via the interface as below:

    # cd /sys/kernel/mm/damon/admin/
    # echo 1 > kdamonds/nr
    # echo 1 > kdamonds/0/contexts/nr
    # echo vaddr > kdamonds/0/contexts/0/damon_type
    # echo 1 > kdamonds/0/contexts/0/targets/nr
    # echo $(pidof <workload>) > kdamonds/0/contexts/0/targets/0/pid
    # echo on > kdamonds/0/state

Main difference between DAMON_DBGFS and DAMON_SYSFS
---------------------------------------------------

DAMON debugfs interface allows multiple monitoring contexts, but it asks users to turn those all on and off at once. It's not a big problem but makes the operation a little bit complex. DAMON_SYSFS allows users to turn on and off monitoring contexts individually.

The Hierarchy
-------------

In a glance, the files hierarchy of the sysfs interface will look like below.

/sys/kernel/mm/damon/admin
│ kdamonds
│ │ nr
│ │ 0/
│ │ │ state,pid
│ │ │ contexts
│ │ │ │ nr
│ │ │ │ 0/
│ │ │ │ │ damon_type
│ │ │ │ │ monitoring_attrs/
│ │ │ │ │ │ intervals/sample_us,aggr_us,update_us │ │ │ │ │ │ nr_regions/min,max
│ │ │ │ │ targets/
│ │ │ │ │ │ nr
│ │ │ │ │ │ 0/
│ │ │ │ │ │ │ pid
│ │ │ │ │ │ │ regions/
│ │ │ │ │ │ │ │ nr
│ │ │ │ │ │ │ │ 0/
│ │ │ │ │ │ │ │ │ start,end
│ │ │ │ │ │ │ │ ...
│ │ │ │ │ │ ...
│ │ │ │ │ ...
│ │ ...

Root
----

The root of the DAMON sysfs is /sys/kernel/mm/damon/, and it has one directory named 'admin'. The directory contains the interface for privileged user space programs. User space tools or deamons having root permission could use this directory. In a future, sibling directories for non-root user space tools or deamons (we could allow them to do monitong of their own virtual address space) or control of in-kernel DAMON-based deamons could be created.

kdamonds/
---------

The monitoring-related information including request specifications and results are called DAMON context. DAMON executes a set of the contexts with a kernel thread called kdamond (for now, only one context per kdamond is supported), and multiple kdamonds could run in parallel. This directory has files for controlling the kdamonds.

In the beginning, this directory has only one file, 'nr'. Writing a number (N) to the file creates the number of child directories named 0 to N-1. Each directory represents each kdamond.

kdamonds/<N>/
-------------

In each kdamond directory, two files (state and pid) and one directory (contexts) reside.

Reading state returns on if the kdamond is currently running, or off if it is not running. Writing on or off makes the kdamond be in the state. If the state is on, reading pid shows the pid of the kdamond thread.

contexts directory resembles kdamonds. It contains files for controlling the monitoring contexts that this kdamond will execute.

kdamonds/<N>/contexts/
----------------------

In the beginning, this directory has only one file, 'nr'. Writing a number (N) to the file creates the number of child directories named as 0 to N-1. Each directory represents each monitoring context. At the moment, only one context per kdamond is supported, so only 0 or 1 can be written to the file.

contexts/<N>/
-------------

In each context directory, one file (operations) and two directories (monitoring_attrs and targets) reside.

DAMON supports multiple types of monitoring operations, including those for virtual address space and physical address space. You can set and show what type of monitoring operations you want to use with the context by writing one of below keywords to, and reading the file.

- vaddr: Monitor virtual address spaces of specific processes

Files for specifying attributes of the monitoring including required quality and efficiency of the monitoring are in monitoring_attrs directory, while files for specifying to what memory regions the monitoring should be done are in targets directory.

contexts/<N>/monitoring_attrs/
------------------------------

In this directory, you can show two directories, intervals and nr_regions.

Under intervals directory, three files for DAMON's sampling interval (sample_us), aggregation interval (aggr_us) and update interval (update_us) exist. You can set and get the values by writing to and reading from the files.

Under nr_regions directory, two files for the lower-bound and upper-bound of DAMON's monitoring regions (min and max, respectively), which controls the monitoring overhead, reside. You can set and get the values by writing to and rading from the files.

For more details about the intervals and monitoring regions range, please read the Design document[1].

[1] https://docs.kernel.org/vm/damon/design.html

contexts/<N>/targets/
---------------------

In the beginning, this directory has only one file, 'nr'. Writing a number (N) to the file creates the number of child directories named 0 to N-1. Each directory represents each monitoring target.

targets/<N>/
------------

In each target directory, one file (pid) exists.

You can make the context to monitor the virtual address space of a process by writing the pid of the process to the file, and show what process's virtual address space the context is set to monitor by reading the file.

---
 mm/damon/Kconfig  |    7 +
 mm/damon/Makefile |    1 +
 mm/damon/sysfs.c  | 1100 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1108 insertions(+)
 create mode 100644 mm/damon/sysfs.c
mm/damon/Kconfig +7 -0
index 01bad77ad7ae..9b559c76d6dd 100644
@@ -52,6 +52,13 @@ config DAMON_VADDR_KUNIT_TEST
 
 	  If unsure, say N.
 
+config DAMON_SYSFS
+	bool "DAMON sysfs interface"
+	depends on DAMON && SYSFS
+	help
+	  This builds the sysfs interface for DAMON.  The user space can use
+	  the interface for arbitrary data access monitoring.
+
 config DAMON_DBGFS
 	bool "DAMON debugfs interface"
 	depends on DAMON_VADDR && DAMON_PADDR && DEBUG_FS
mm/damon/Makefile +1 -0
index 03931472991a..d7e316ab9eb7 100644
@@ -4,4 +4,5 @@ obj-$(CONFIG_DAMON)		:= core.o
 obj-$(CONFIG_DAMON_VADDR)	+= ops-common.o vaddr.o
 obj-$(CONFIG_DAMON_PADDR)	+= ops-common.o paddr.o
 obj-$(CONFIG_DAMON_DBGFS)	+= dbgfs.o
+obj-$(CONFIG_DAMON_SYSFS)	+= sysfs.o
 obj-$(CONFIG_DAMON_RECLAIM)	+= reclaim.o
mm/damon/sysfs.c new file +1100 -0

1,100 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [RFC PATCH 1/4] mm/damon: Implement a sysfs-based DAMON user interface

[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]

Hi SeongJae,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on hnaz-mm/master]
[also build test WARNING on shuah-kselftest/next linux/master linus/master v5.17-rc4 next-20220217] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/SeongJae-Park/Introduce-DAMON-sysfs-interface/20220218-002109
base:   https://github.com/hnaz/linux-mm master
config: arc-allyesconfig (https://download.01.org/0day-ci/archive/20220218/202202180207.cMG3IvJT-lkp(a)intel.com/config)
compiler: arceb-elf-gcc (GCC) 11.2.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/f0b638f2a815dfdba8e4164563e21f7744becda5
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review SeongJae-Park/Introduce-DAMON-sysfs-interface/20220218-002109 git checkout f0b638f2a815dfdba8e4164563e21f7744becda5
# save the config file to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=arc SHELL=/bin/bash mm/damon/

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> mm/damon/sysfs.c:898:5: warning: no previous prototype for 'damon_sysfs_nr_running_ctxs' [-Wmissing-prototypes]

898 | int damon_sysfs_nr_running_ctxs(struct damon_sysfs_kdamond **kdamonds,
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +/damon_sysfs_nr_running_ctxs +898 mm/damon/sysfs.c

897

 > 898	int damon_sysfs_nr_running_ctxs(struct damon_sysfs_kdamond **kdamonds,

899 int nr_kdamonds)
900 {
901 int nr_running_ctxs = 0;
902 int i;
903
904 for (i = 0; i < nr_kdamonds; i++) {
905 struct damon_ctx *ctx;
906
907 ctx = kdamonds[i]->damon_ctx;
908 if (!ctx)
909 continue;
910 mutex_lock(&ctx->kdamond_lock);
911 if (ctx->kdamond)
912 nr_running_ctxs++;
913 mutex_unlock(&ctx->kdamond_lock);
914 }
915 return nr_running_ctxs;
916 }
917

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

author maintainer
#3 lore raw

RFC PATCH 2/4 mm/damon/core: Allow non-exclusive DAMON start/stop

In reply to SeongJae Park

To avoid interference between DAMON users, current start/stop functions of DAMON allows only exclusive groups. This makes use of DAMON a little bit restrictive. Meanwhile, admins could somehow aware each DAMON usage and therefore could address the interference on their own. This commit hence allows callers of the functions to specify if the contexts should run in an exclusive mode or not.

---
 include/linux/damon.h |  2 +-
 mm/damon/core.c       | 22 ++++++++++++++++------
 mm/damon/dbgfs.c      |  2 +-
 mm/damon/reclaim.c    |  2 +-
 4 files changed, 19 insertions(+), 9 deletions(-)
include/linux/damon.h +1 -1
index 49c4a11ecf20..f8e99e47d747 100644
@@ -508,7 +508,7 @@ int damon_nr_running_ctxs(void);
 int damon_register_ops(struct damon_operations *ops);
 int damon_select_ops(struct damon_ctx *ctx, enum damon_ops_id id);
 
-int damon_start(struct damon_ctx **ctxs, int nr_ctxs);
+int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive);
 int damon_stop(struct damon_ctx **ctxs, int nr_ctxs);
 
 #endif	/* CONFIG_DAMON */
mm/damon/core.c +16 -6
index 82e0a4620c4f..08c5e4dd2ed9 100644
@@ -24,6 +24,7 @@
 
 static DEFINE_MUTEX(damon_lock);
 static int nr_running_ctxs;
+static bool running_exclusive_ctxs;
 
 static DEFINE_MUTEX(damon_ops_lock);
 static struct damon_operations damon_registered_ops[NR_DAMON_OPS];
@@ -434,22 +435,25 @@ static int __damon_start(struct damon_ctx *ctx)
  * damon_start() - Starts the monitorings for a given group of contexts.
  * @ctxs:	an array of the pointers for contexts to start monitoring
  * @nr_ctxs:	size of @ctxs
+ * @exclusive:	exclusiveness of this contexts group
  *
  * This function starts a group of monitoring threads for a group of monitoring
  * contexts.  One thread per each context is created and run in parallel.  The
- * caller should handle synchronization between the threads by itself.  If a
- * group of threads that created by other 'damon_start()' call is currently
- * running, this function does nothing but returns -EBUSY.
+ * caller should handle synchronization between the threads by itself.  If
+ * @exclusive is true and a group of threads that created by other
+ * 'damon_start()' call is currently running, this function does nothing but
+ * returns -EBUSY.
  *
  * Return: 0 on success, negative error code otherwise.
  */
-int damon_start(struct damon_ctx **ctxs, int nr_ctxs)
+int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive)
 {
 	int i;
 	int err = 0;
 
 	mutex_lock(&damon_lock);
-	if (nr_running_ctxs) {
+	if ((exclusive && nr_running_ctxs) ||
+			(!exclusive && running_exclusive_ctxs)) {
 		mutex_unlock(&damon_lock);
 		return -EBUSY;
 	}
@@ -460,13 +464,15 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs)
 			break;
 		nr_running_ctxs++;
 	}
+	if (exclusive && nr_running_ctxs)
+		running_exclusive_ctxs = true;
 	mutex_unlock(&damon_lock);
 
 	return err;
 }
 
 /*
- * __damon_stop() - Stops monitoring of given context.
+ * __damon_stop() - Stops monitoring of a given context.
  * @ctx:	monitoring context
  *
  * Return: 0 on success, negative error code otherwise.
@@ -506,6 +512,10 @@ int damon_stop(struct damon_ctx **ctxs, int nr_ctxs)
 		if (err)
 			return err;
 	}
+	mutex_lock(&damon_lock);
+	if (!nr_running_ctxs && running_exclusive_ctxs)
+		running_exclusive_ctxs = false;
+	mutex_unlock(&damon_lock);
 
 	return err;
 }
mm/damon/dbgfs.c +1 -1
index 05b574cbcea8..a0dab8b5e45f 100644
@@ -967,7 +967,7 @@ static ssize_t dbgfs_monitor_on_write(struct file *file,
 				return -EINVAL;
 			}
 		}
-		ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
+		ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs, true);
 	} else if (!strncmp(kbuf, "off", count)) {
 		ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
 	} else {
mm/damon/reclaim.c +1 -1
index b53d9c22fad1..e34c4d0c4d93 100644
@@ -330,7 +330,7 @@ static int damon_reclaim_turn(bool on)
 	if (err)
 		goto free_scheme_out;
 
-	err = damon_start(&ctx, 1);
+	err = damon_start(&ctx, 1, true);
 	if (!err) {
 		kdamond_pid = ctx->kdamond->pid;
 		return 0;
Signature · 1 line
2.17.1
author maintainer
#4 lore raw

RFC PATCH 3/4 mm/damon/sysfs: Link DAMON to 'state' file read/write functions

In reply to SeongJae Park

This commit implements 'state' DAMON sysfs interface file read/write functions. In detail, writing 'on' or 'off' to the 'state' file turns DAMON for the context on or off, accordingly. Reading the file shows the state.

---
 mm/damon/sysfs.c | 166 +++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 162 insertions(+), 4 deletions(-)
mm/damon/sysfs.c +162 -4
index 59bdc7268dc6..721ee086265f 100644
@@ -793,18 +793,176 @@ static void damon_sysfs_kdamond_rm_dirs(struct damon_sysfs_kdamond *kdamond)
 	kobject_put(&kdamond->contexts->kobj);
 }
 
+static bool damon_sysfs_ctx_running(struct damon_ctx *ctx)
+{
+	bool running;
+
+	mutex_lock(&ctx->kdamond_lock);
+	running = ctx->kdamond != NULL;
+	mutex_unlock(&ctx->kdamond_lock);
+	return running;
+}
+
 static ssize_t damon_sysfs_kdamond_state_show(struct kobject *kobj,
 		struct kobj_attribute *attr, char *buf)
 {
-	/* TODO: Link with DAMON */
-	return sysfs_emit(buf, "off\n");
+	struct damon_sysfs_kdamond *kdamond = container_of(kobj,
+			struct damon_sysfs_kdamond, kobj);
+	struct damon_ctx *ctx = kdamond->damon_ctx;
+	bool running;
+
+	if (!ctx)
+		running = false;
+	else
+		running = damon_sysfs_ctx_running(ctx);
+
+	return sysfs_emit(buf, "%s\n", running ? "on" : "off");
+}
+
+static void damon_sysfs_destroy_targets(struct damon_ctx *ctx)
+{
+	struct damon_target *t, *next;
+
+	damon_for_each_target_safe(t, next, ctx) {
+		if (ctx->ops.id == DAMON_OPS_VADDR)
+			put_pid(t->pid);
+		damon_destroy_target(t);
+	}
+}
+
+static int damon_sysfs_set_targets(struct damon_ctx *ctx,
+		struct damon_sysfs_context *sysfs_ctx)
+{
+	struct damon_sysfs_targets *targets = sysfs_ctx->targets;
+	int i;
+
+	for (i = 0; i < targets->nr_targets; i++) {
+		struct damon_target *t;
+
+		t = damon_new_target();
+		if (!t) {
+			damon_sysfs_destroy_targets(ctx);
+			return -ENOMEM;
+		}
+		if (ctx->ops.id == DAMON_OPS_VADDR) {
+			t->pid = find_get_pid(targets->targets_arr[i]->pid);
+			if (!t->pid) {
+				damon_sysfs_destroy_targets(ctx);
+				return -EINVAL;
+			}
+		}
+		damon_add_target(ctx, t);
+	}
+	return 0;
+}
+
+static inline bool target_has_pid(const struct damon_ctx *ctx)
+{
+	return ctx->ops.id == DAMON_OPS_VADDR;
+}
+
+static void damon_sysfs_before_terminate(struct damon_ctx *ctx)
+{
+	struct damon_target *t, *next;
+
+	if (!target_has_pid(ctx))
+		return;
+
+	mutex_lock(&ctx->kdamond_lock);
+	damon_for_each_target_safe(t, next, ctx) {
+		put_pid(t->pid);
+		damon_destroy_target(t);
+	}
+	mutex_unlock(&ctx->kdamond_lock);
+}
+
+static struct damon_ctx *damon_sysfs_build_ctx(
+		struct damon_sysfs_context *sys_ctx)
+{
+	struct damon_ctx *ctx = damon_new_ctx();
+	struct damon_sysfs_attrs *sys_attrs = sys_ctx->attrs;
+	struct damon_sysfs_ul_range *sys_nr_regions = sys_attrs->nr_regions;
+	struct damon_sysfs_intervals *sys_intervals = sys_attrs->intervals;
+	int err;
+
+	if (!ctx)
+		return ERR_PTR(-ENOMEM);
+
+	err = damon_select_ops(ctx, sys_ctx->ops_id);
+	if (err)
+		goto out;
+
+	err = damon_set_attrs(ctx, sys_intervals->sample_us,
+			sys_intervals->aggr_us, sys_intervals->update_us,
+			sys_nr_regions->min, sys_nr_regions->max);
+	if (err)
+		goto out;
+	err = damon_sysfs_set_targets(ctx, sys_ctx);
+	if (err)
+		goto out;
+	ctx->callback.before_terminate = damon_sysfs_before_terminate;
+	return ctx;
+
+out:
+	damon_destroy_ctx(ctx);
+	return ERR_PTR(err);
 }
 
 static ssize_t damon_sysfs_kdamond_state_store(struct kobject *kobj,
 		struct kobj_attribute *attr, const char *buf, size_t count)
 {
-	/* TODO: Link with DAMON */
-	return count;
+	struct damon_sysfs_kdamond *kdamond = container_of(kobj,
+			struct damon_sysfs_kdamond, kobj);
+	struct damon_ctx *ctx;
+	ssize_t ret;
+
+	if (!mutex_trylock(&damon_sysfs_lock))
+		return -EBUSY;
+	if (!strncmp(buf, "on\n", count)) {
+		if (kdamond->damon_ctx &&
+				damon_sysfs_ctx_running(kdamond->damon_ctx)) {
+			ret = -EBUSY;
+			goto out;
+		}
+		if (kdamond->contexts->nr != 1) {
+			ret = -EINVAL;
+			goto out;
+		}
+
+		if (kdamond->damon_ctx)
+			damon_destroy_ctx(kdamond->damon_ctx);
+		kdamond->damon_ctx = NULL;
+
+		ctx = damon_sysfs_build_ctx(
+				kdamond->contexts->contexts_arr[0]);
+		if (IS_ERR(ctx)) {
+			ret = PTR_ERR(ctx);
+			goto out;
+		}
+		ret = damon_start(&ctx, 1, false);
+		if (ret) {
+			damon_destroy_ctx(ctx);
+			goto out;
+		}
+		kdamond->damon_ctx = ctx;
+	} else if (!strncmp(buf, "off\n", count)) {
+		if (!kdamond->damon_ctx) {
+			ret = -EINVAL;
+			goto out;
+		}
+		ret = damon_stop(&kdamond->damon_ctx, 1);
+		/*
+		 * kdamond->damon_ctx will be freed in next on, or
+		 * kdamonds_nr_store()
+		 */
+	} else {
+		ret = -EINVAL;
+	}
+out:
+	mutex_unlock(&damon_sysfs_lock);
+	if (!ret)
+		ret = count;
+	return ret;
 }
 
 static ssize_t damon_sysfs_kdamond_pid_show(struct kobject *kobj,
Signature · 1 line
2.17.1
author maintainer
#5 lore raw

RFC PATCH 4/4 selftests/damon: Add a test for DAMON sysfs interface

In reply to SeongJae Park

This commit adds a selftest for DAMON sysfs interface. It tests functionality of 'nr' files and existence of files in each directory of the hierarchy.

---
 tools/testing/selftests/damon/Makefile |   1 +
 tools/testing/selftests/damon/sysfs.sh | 200 +++++++++++++++++++++++++
 2 files changed, 201 insertions(+)
 create mode 100755 tools/testing/selftests/damon/sysfs.sh
tools/testing/selftests/damon/Makefile +1 -0
index 937d36ae9a69..0470c5f3e690 100644
@@ -6,5 +6,6 @@ TEST_GEN_FILES += huge_count_read_write
 TEST_FILES = _chk_dependency.sh _debugfs_common.sh
 TEST_PROGS = debugfs_attrs.sh debugfs_schemes.sh debugfs_target_ids.sh
 TEST_PROGS += debugfs_empty_targets.sh debugfs_huge_count_read_write.sh
+TEST_PROGS += sysfs.sh
 
 include ../lib.mk
tools/testing/selftests/damon/sysfs.sh new file +200 -0

200 lines not on this page · raw message

Signature · 1 line
2.17.1