From nobody Sat Feb 7 16:25:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E36823236F; Tue, 10 Dec 2024 21:50:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867438; cv=none; b=O+KKHvr+ROH1sCEzs051mR7nLS/Y9tchbEyuNn3JtizkNP6fk1f73WAkRmbKsDs3kHQX02EUqihNoIzYPCfbEtfTXQdNncfJBLZVsvP+Oc5XpE3LdBzEsDp6gUMo2dQU6VuZkMbwGXY6t1eHPRepN76+ffB4gFbEh8sggxVbFXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867438; c=relaxed/simple; bh=jB3IdYIaZt6diKe0C9b6i8dd46f2ua9DxU6E12pOiyg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jQgSCvTmodjOvf99LeWY18zzJYCmJjyFhXX3NdIBomegtBT6BsezMGYgKwkBcNr3oWtmeFOC7EnS5Z6iaM03rnrS4NuK+NoCg40W6hO+FlGtMceTHQan+11LuCuGg5KJWLt/XWRdeTBVIUssdPbCZkH9gHEz3kh7sf7xXmPKD5E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PNReYWYc; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="PNReYWYc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A3A2C4CED6; Tue, 10 Dec 2024 21:50:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733867437; bh=jB3IdYIaZt6diKe0C9b6i8dd46f2ua9DxU6E12pOiyg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PNReYWYc6t6JPmtGOCpuSkxP8a/ufQVYtC4vVsoaWJcdcEW212jts0kBclf3jFszr /2HuzNHpYAKsjcWYBlwteMlCTwfTjIPQ/6eMmgV9mY0VDr9tUnjS2qN1j3s9ZLyUQZ LbGnkrxvhaEEwpVPIoYqJAAW4/PPNpzvCy9qE2dDzxeFJy8eWHOYBjbJZXUx9tM6JK giskQ8rX1qZ9t+Z+NliBXXK4Kj0eEZldVkxlzG99L1q8zQGoUFxgXXWxxLa5N2nXr7 cV+rSMecRabVS0IS9Yz7u/yPNemmi4qLGKSdcQpiolcQk6Lr5rxeqpGEnCPK5m3x/J WDvC2woiHovbA== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH 1/5] samples: add a skeleton of a sample DAMON module for working set size estimation Date: Tue, 10 Dec 2024 13:50:26 -0800 Message-Id: <20241210215030.85675-2-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241210215030.85675-1-sj@kernel.org> References: <20241210215030.85675-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a skeleton for a sample DAMON static module that can be used for estimating working set size of a given process. Note that it is a static module since DAMON is not exporting symbols to loadable modules for now. It exposes two module parameters, namely 'pid' and 'enable'. 'pid' will specify the process that the module will estimate the working set size of. 'enable' will receive whether to start or stop the estimation. Because this is just a skeleton, the parameters do nothing, though. The functionalities will be implemented by following commits. Signed-off-by: SeongJae Park --- MAINTAINERS | 1 + samples/Kconfig | 2 ++ samples/Makefile | 1 + samples/damon/Kconfig | 17 +++++++++++ samples/damon/Makefile | 3 ++ samples/damon/wsse.c | 65 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 samples/damon/Kconfig create mode 100644 samples/damon/Makefile create mode 100644 samples/damon/wsse.c diff --git a/MAINTAINERS b/MAINTAINERS index 1e930c7a58b1..68d825a4c69c 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -6328,6 +6328,7 @@ F: Documentation/mm/damon/ F: include/linux/damon.h F: include/trace/events/damon.h F: mm/damon/ +F: samples/damon/ F: tools/testing/selftests/damon/ =20 DAVICOM FAST ETHERNET (DMFE) NETWORK DRIVER diff --git a/samples/Kconfig b/samples/Kconfig index b288d9991d27..8d5a36f0e5d6 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -293,6 +293,8 @@ config SAMPLE_CGROUP =20 source "samples/rust/Kconfig" =20 +source "samples/damon/Kconfig" + endif # SAMPLES =20 config HAVE_SAMPLE_FTRACE_DIRECT diff --git a/samples/Makefile b/samples/Makefile index b85fa64390c5..726bb5293486 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -39,3 +39,4 @@ obj-$(CONFIG_SAMPLE_KMEMLEAK) +=3D kmemleak/ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) +=3D coresight/ obj-$(CONFIG_SAMPLE_FPROBE) +=3D fprobe/ obj-$(CONFIG_SAMPLES_RUST) +=3D rust/ +obj-$(CONFIG_SAMPLE_DAMON_WSSE) +=3D damon/ diff --git a/samples/damon/Kconfig b/samples/damon/Kconfig new file mode 100644 index 000000000000..b799e01345c8 --- /dev/null +++ b/samples/damon/Kconfig @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: GPL-2.0 + +menu "DAMON Samples" + +config SAMPLE_DAMON_WSSE + bool "DAMON sameple module for working set size estimation" + depends on DAMON && DAMON_VADDR + help + This builds DAMON sample module for working set size estimation. + + The module receives a pid, monitor access to the virtual address + space of the process, estimate working set size of the process, and + repeatedly prints the size on the kernel log. + + If unsure, say N. + +endmenu diff --git a/samples/damon/Makefile b/samples/damon/Makefile new file mode 100644 index 000000000000..ccbe93d40130 --- /dev/null +++ b/samples/damon/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_SAMPLE_DAMON_WSSE) +=3D wsse.o diff --git a/samples/damon/wsse.c b/samples/damon/wsse.c new file mode 100644 index 000000000000..7f2cb76a1a70 --- /dev/null +++ b/samples/damon/wsse.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * working set size estimation: monitor access pattern of given process and + * print estimated working set size (total size of regions that showing so= me + * access). + */ + +#define pr_fmt(fmt) "damon_sample_wsse: " fmt + +#include +#include +#include +#include + +static int target_pid __read_mostly; +module_param(target_pid, int, 0600); + +static int damon_sample_wsse_enable_store( + const char *val, const struct kernel_param *kp); + +static const struct kernel_param_ops enable_param_ops =3D { + .set =3D damon_sample_wsse_enable_store, + .get =3D param_get_bool, +}; + +static bool enable __read_mostly; +module_param_cb(enable, &enable_param_ops, &enable, 0600); +MODULE_PARM_DESC(enable, "Enable or disable DAMON_SAMPLE_WSSE"); + +static int damon_sample_wsse_start(void) +{ + pr_info("start\n"); + return 0; +} + +static void damon_sample_wsse_stop(void) +{ + pr_info("stop\n"); +} + +static int damon_sample_wsse_enable_store( + const char *val, const struct kernel_param *kp) +{ + bool enabled =3D enable; + int err; + + err =3D kstrtobool(val, &enable); + if (err) + return err; + + if (enable =3D=3D enabled) + return 0; + + if (enable) + return damon_sample_wsse_start(); + damon_sample_wsse_stop(); + return 0; +} + +static int __init damon_sample_wsse_init(void) +{ + return 0; +} + +module_init(damon_sample_wsse_init); --=20 2.39.5 From nobody Sat Feb 7 16:25:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E86EB2397B9; Tue, 10 Dec 2024 21:50:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867439; cv=none; b=IKMsY/XO6D0BEefrrjTyZKibHfSoDkTeD09Y2YJksrwCbI4vZsKXYsuICXYxTYiWODO3tS5AEw4qnMkERm53elchZlaHfy8Vfd0sM/jbkEj50zZyIeSf0J6lvPUFFsT/tWSEnxrsQNVpGhWTL2jYZua6UaS1mhmlEZ6FdmsHAKE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867439; c=relaxed/simple; bh=hoi3YSqqOR9ePv9N3p8sOerDMAcJ+RMcuGrppxt+mz8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=beXibOIon9Xur3n6FVmMB2xZ/EznSaUSfFOJpSBmdhPRHI51M+Jwv0zC15ZTTVESyebaNCIpmD7Ov1BzSXz/HxsCba75gBbtkVXUUn3631HrTxJxYkczPbYEHD/GrLH1jEcRvb/EYVsScodi7X9nnFoe/Kju2wZwz+QrhGIJ07I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bl7caDxm; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="bl7caDxm" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 55444C4CED6; Tue, 10 Dec 2024 21:50:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733867438; bh=hoi3YSqqOR9ePv9N3p8sOerDMAcJ+RMcuGrppxt+mz8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bl7caDxmLeYr/o8tpYrjKbJRh6DULZtvH6VaBGKNqJ9E7gVsm/G00XYnAtxCqsxPn i8J260Ruy88dw6OYY3lGAg0E+gPFiX7P5FIql/AG4y/rdPuOCUkqofbaK0livJSP44 lwYtDlRNDcM0D6j4hpOKd0DlDK8F98uiP5Iddddck8qscsnNbCo3RUVg++r9aEOsug THFGw1CoXBGjE/CMQNAwPlr8y0Hxqd2cNI+M2WQhks2hZ6VLsa6Tf2KY/qYE3+rQkO /5P71yilA08C4due9Nrh6c5J3NjAq2SAnay8Iq1do/5xn6/R9cKK0BPbzJcR/kDF2w 0bx+PLksRF9iw== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH 2/5] samples/damon/wsse: start and stop DAMON as the user requests Date: Tue, 10 Dec 2024 13:50:27 -0800 Message-Id: <20241210215030.85675-3-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241210215030.85675-1-sj@kernel.org> References: <20241210215030.85675-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Start running DAMON to monitor accesses of a process that the user specified via 'target_pid' parameter, when 'y' is passed to 'enable' parameter. Stop running DAMON when 'n' is passed to 'enable' parameter. Estimating the working set size from DAMON's monitoring results and reporting it to the user will be implemented by the following commit. Signed-off-by: SeongJae Park --- samples/damon/wsse.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/samples/damon/wsse.c b/samples/damon/wsse.c index 7f2cb76a1a70..2ba0c91baad9 100644 --- a/samples/damon/wsse.c +++ b/samples/damon/wsse.c @@ -27,15 +27,48 @@ static bool enable __read_mostly; module_param_cb(enable, &enable_param_ops, &enable, 0600); MODULE_PARM_DESC(enable, "Enable or disable DAMON_SAMPLE_WSSE"); =20 +static struct damon_ctx *ctx; +static struct pid *target_pidp; + static int damon_sample_wsse_start(void) { + struct damon_target *target; + pr_info("start\n"); - return 0; + + ctx =3D damon_new_ctx(); + if (!ctx) + return -ENOMEM; + if (damon_select_ops(ctx, DAMON_OPS_VADDR)) { + damon_destroy_ctx(ctx); + return -EINVAL; + } + + target =3D damon_new_target(); + if (!target) { + damon_destroy_ctx(ctx); + return -ENOMEM; + } + damon_add_target(ctx, target); + target_pidp =3D find_get_pid(target_pid); + if (!target_pidp) { + damon_destroy_ctx(ctx); + return -EINVAL; + } + target->pid =3D target_pidp; + + return damon_start(&ctx, 1, true); } =20 static void damon_sample_wsse_stop(void) { pr_info("stop\n"); + if (ctx) { + damon_stop(&ctx, 1); + damon_destroy_ctx(ctx); + } + if (target_pidp) + put_pid(target_pidp); } =20 static int damon_sample_wsse_enable_store( --=20 2.39.5 From nobody Sat Feb 7 16:25:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BDB3123A577; Tue, 10 Dec 2024 21:50:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867439; cv=none; b=Mbo2tZlcWP1U8K9rnOqDG/tsKQkEwG9BER9Sha7ixFr0w88KLSNpP7nLLAVUuxUx/g/sDlwWXHbUruEU+Stf9jPSh9K+Er/9Mbgdom2d7tNYyNB+5e9EyZ+tlvDgX/xl8wPXx4NO2iWER9r8mtmNL7CnN/rR+bB4QtUXwF1dCOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867439; c=relaxed/simple; bh=WhKbvDPjbuIBA/Td7/ebEpkfZLrMJgwOSAMpKKqqUME=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FQXyJUoctzUwLiAjZ6mlES225sAIDR4EjuJwaGV3AaGkOxWRWt1AoGZI6nOqkW4FSSS25fvUk8CfIoHu84lvoOtC3H/k4RBgjyRq+vF+ODxMHHTWrhXaKMwOaNsZ7kvjOwxUPCni+7cDaRQ/ak/w0r+RwN1dYH3YMiKObXaInAY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EmMRqjDE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EmMRqjDE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 74FDFC4CEDF; Tue, 10 Dec 2024 21:50:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733867439; bh=WhKbvDPjbuIBA/Td7/ebEpkfZLrMJgwOSAMpKKqqUME=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EmMRqjDEmysTyaTfmY/VxeGmtYNZm7cToJf/5OuUA/BWeBb5Wcre05Ccqb5qdayLZ 7F93iFBLS8q4pebZnd/TYpcySoLYarrtciHg4Hge3+ZALTkxtye8utueT89EXUmQed OjrhDQ4wm84CWa1Ew1Cx3L7FsYZ+QwTVgFGZlpHikj5RITpsTDj4fU0UJp+NEtTUJl N+mOlxI9BbGqSpB+c1dJJmuHPjcdhvfJfe8qcT0Ihx73vIc6wk//JUEaMt7K43Zahf ixcOYKt7CNRTpQwjuhbbl8+XHlgtOna/Kdv2pT36zEQgvfyMmvcBDycQaSbaPc2/ny uc/F7vl7pyvFg== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH 3/5] samples/damon/wsse: implement working set size estimation and logging Date: Tue, 10 Dec 2024 13:50:28 -0800 Message-Id: <20241210215030.85675-4-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241210215030.85675-1-sj@kernel.org> References: <20241210215030.85675-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement the DAMON-based working set size estimation logic. The logic iterates memory regions in DAMON-generated access pattern snapshot for every aggregation interval and get the total sum of the size of any region having one or higher 'nr_accesses' count. That is, it assumes any region having one or higher 'nr_accesses' to be a part of the working set. The estimated value is reported to the user by printing it to the kernel log. Signed-off-by: SeongJae Park --- samples/damon/wsse.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/samples/damon/wsse.c b/samples/damon/wsse.c index 2ba0c91baad9..11be25803274 100644 --- a/samples/damon/wsse.c +++ b/samples/damon/wsse.c @@ -30,6 +30,23 @@ MODULE_PARM_DESC(enable, "Enable or disable DAMON_SAMPLE= _WSSE"); static struct damon_ctx *ctx; static struct pid *target_pidp; =20 +static int damon_sample_wsse_after_aggregate(struct damon_ctx *c) +{ + struct damon_target *t; + + damon_for_each_target(t, c) { + struct damon_region *r; + unsigned long wss =3D 0; + + damon_for_each_region(r, t) { + if (r->nr_accesses > 0) + wss +=3D r->ar.end - r->ar.start; + } + pr_info("wss: %lu\n", wss); + } + return 0; +} + static int damon_sample_wsse_start(void) { struct damon_target *target; @@ -57,6 +74,7 @@ static int damon_sample_wsse_start(void) } target->pid =3D target_pidp; =20 + ctx->callback.after_aggregation =3D damon_sample_wsse_after_aggregate; return damon_start(&ctx, 1, true); } =20 --=20 2.39.5 From nobody Sat Feb 7 16:25:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2AEB123A59F; Tue, 10 Dec 2024 21:50:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867441; cv=none; b=PUG0aSmb8Z104G9MicybpM+z8VKx/9s1cjOkuYzfmcYtMjfKRU914w0tzBX/9ZRw8doACnSS4bU8OgmJX/pRrG/utlf+9RiGYA4cKPKuqMyaIiq+vdrqmYjxs1TS56eeaUNCbYplWqrTPerdkApKEYM1wCb0oBft7A3JQA41erA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867441; c=relaxed/simple; bh=7FEsy6E4p0WwhU+DV5aMiawHvgrRCnoGLK5GZ2jkVJM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lnLAaOS0g2kN+FqtOhkgHeSVu55L5RySWRTCD/PPleuSc9i3+YV2LqzEAhetmCqFkoHT1brQ63XJMpwFQoOvIpatwUg/Gt7D831rgyZV5mHtw/2Xp1rzmLmcMTmB2alwPQdysgMCyVFdFFJobq8rVH1LthOjvkESwxXf6NB/mD8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=D15Zzayf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="D15Zzayf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 865A5C4CED6; Tue, 10 Dec 2024 21:50:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733867440; bh=7FEsy6E4p0WwhU+DV5aMiawHvgrRCnoGLK5GZ2jkVJM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D15ZzayfcegIah0G2/GjTG3mOixo+knKcaGLxl3qTlHsz1ye8cAFdYef2OgA7Rjxy qswkyU7NMOc8lTa3B/lHqiAgrX6D3M7EQufPx/+1d/5nbdYJv4l/GRAPsU7NiC9DOG 65rq7/frDYJQg/agSTFvwoVtW9Yhx2karjpOzP2pqUZmK16YJa4pJisysczCaeNSmH VBoCynKIjHOGkTzVHPEgFJk+xwsaq0+e1QyD/L84EOYQYaxLXXJc/onZ6cRbajfejQ AkfpJgJLBm+4ZIc03oL6MQIeHOXMSgDkvtgAWI2Uodunac0jT/9OhQ02S/0SOdDLIU /mlgSJMIdkKAA== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH 4/5] samples/damon: introduce a skeleton of a smaple DAMON module for proactive reclamation Date: Tue, 10 Dec 2024 13:50:29 -0800 Message-Id: <20241210215030.85675-5-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241210215030.85675-1-sj@kernel.org> References: <20241210215030.85675-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DAMON is not only for monitoring of access patterns, but also for access-aware system operations. For the system operations, DAMON provides a feature called DAMOS (Data Access Monitoring-based Operation Schemes). There is no sample API usage of DAMOS, though. Copy the working set size estimation sample modules with changed names of the module and symbols, to use it as a skeleton for a sample module showing the DAMOS API usage. The following commit will make it proactively reclaim cold memory of the given process, using DAMOS. Signed-off-by: SeongJae Park --- samples/Makefile | 1 + samples/damon/Kconfig | 13 +++++ samples/damon/Makefile | 1 + samples/damon/prcl.c | 116 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 samples/damon/prcl.c diff --git a/samples/Makefile b/samples/Makefile index 726bb5293486..5af6bb8afb07 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -40,3 +40,4 @@ obj-$(CONFIG_SAMPLE_CORESIGHT_SYSCFG) +=3D coresight/ obj-$(CONFIG_SAMPLE_FPROBE) +=3D fprobe/ obj-$(CONFIG_SAMPLES_RUST) +=3D rust/ obj-$(CONFIG_SAMPLE_DAMON_WSSE) +=3D damon/ +obj-$(CONFIG_SAMPLE_DAMON_PRCL) +=3D damon/ diff --git a/samples/damon/Kconfig b/samples/damon/Kconfig index b799e01345c8..63f6dcd71daa 100644 --- a/samples/damon/Kconfig +++ b/samples/damon/Kconfig @@ -14,4 +14,17 @@ config SAMPLE_DAMON_WSSE =20 If unsure, say N. =20 +config SAMPLE_DAMON_PRCL + bool "DAMON sameple module for access-aware proactive reclamation" + depends on DAMON && DAMON_VADDR + help + This builds DAMON sample module for access-aware proactive + reclamation. + + The module receives a pid, monitor access to the virtual address + space of the process, find memory regions that not accessed, and + proactively reclaim the regions. + + If unsure, say N. + endmenu diff --git a/samples/damon/Makefile b/samples/damon/Makefile index ccbe93d40130..7f155143f237 100644 --- a/samples/damon/Makefile +++ b/samples/damon/Makefile @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0 =20 obj-$(CONFIG_SAMPLE_DAMON_WSSE) +=3D wsse.o +obj-$(CONFIG_SAMPLE_DAMON_PRCL) +=3D prcl.o diff --git a/samples/damon/prcl.c b/samples/damon/prcl.c new file mode 100644 index 000000000000..b34b9bfed532 --- /dev/null +++ b/samples/damon/prcl.c @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * proactive reclamation: monitor access pattern of a given process, find + * regiosn that seems not accessed, and proactively page out the regions. + */ + +#define pr_fmt(fmt) "damon_sample_prcl: " fmt + +#include +#include +#include +#include + +static int target_pid __read_mostly; +module_param(target_pid, int, 0600); + +static int damon_sample_prcl_enable_store( + const char *val, const struct kernel_param *kp); + +static const struct kernel_param_ops enable_param_ops =3D { + .set =3D damon_sample_prcl_enable_store, + .get =3D param_get_bool, +}; + +static bool enable __read_mostly; +module_param_cb(enable, &enable_param_ops, &enable, 0600); +MODULE_PARM_DESC(enable, "Enable of disable DAMON_SAMPLE_WSSE"); + +static struct damon_ctx *ctx; +static struct pid *target_pidp; + +static int damon_sample_prcl_after_aggregate(struct damon_ctx *c) +{ + struct damon_target *t; + + damon_for_each_target(t, c) { + struct damon_region *r; + unsigned long wss =3D 0; + + damon_for_each_region(r, t) { + if (r->nr_accesses > 0) + wss +=3D r->ar.end - r->ar.start; + } + pr_info("wss: %lu\n", wss); + } + return 0; +} + +static int damon_sample_prcl_start(void) +{ + struct damon_target *target; + + pr_info("start\n"); + + ctx =3D damon_new_ctx(); + if (!ctx) + return -ENOMEM; + if (damon_select_ops(ctx, DAMON_OPS_VADDR)) { + damon_destroy_ctx(ctx); + return -EINVAL; + } + + target =3D damon_new_target(); + if (!target) { + damon_destroy_ctx(ctx); + return -ENOMEM; + } + damon_add_target(ctx, target); + target_pidp =3D find_get_pid(target_pid); + if (!target_pidp) { + damon_destroy_ctx(ctx); + return -EINVAL; + } + target->pid =3D target_pidp; + + ctx->callback.after_aggregation =3D damon_sample_prcl_after_aggregate; + + return damon_start(&ctx, 1, true); +} + +static void damon_sample_prcl_stop(void) +{ + pr_info("stop\n"); + if (ctx) { + damon_stop(&ctx, 1); + damon_destroy_ctx(ctx); + } + if (target_pidp) + put_pid(target_pidp); +} + +static int damon_sample_prcl_enable_store( + const char *val, const struct kernel_param *kp) +{ + bool enabled =3D enable; + int err; + + err =3D kstrtobool(val, &enable); + if (err) + return err; + + if (enable =3D=3D enabled) + return 0; + + if (enable) + return damon_sample_prcl_start(); + damon_sample_prcl_stop(); + return 0; +} + +static int __init damon_sample_prcl_init(void) +{ + return 0; +} + +module_init(damon_sample_prcl_init); --=20 2.39.5 From nobody Sat Feb 7 16:25:27 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 619D323D426; Tue, 10 Dec 2024 21:50:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867442; cv=none; b=sx0D8E9v5csnKoZ6Ax/cPWbSSuFoYFrG2q3ktTV+Op0xEKcNJICoPjd3tst1KA4Gqq4M5lsO5SCJNKvb6gfcuWnQohDMDD9VZdZXuCbRRWYMNFyoCwIft5Co5+ySH57lUZ1jgPqrbiU0ONZbhHJSSca7ieje1i2lGEwQwKNyXdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733867442; c=relaxed/simple; bh=RRBl+ZZMmihwq3svB0M1vQmUUPQ7A6rMW0M3h5PQx34=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QJhezVM6h4H508KOcsSvS2jizNXVq6hXvxrXRgzAlSDLWSxcuXLodsqRZ4xeRe9aQKA7dayfO2rrmmA0eEcUIen8XrleiFqb4Osjq/ArArYrXgdjWnDUT05RJwjos/Ow24UL+fMzBG9IWqc/Z0QqH2j1QAVUQEpGvHb2IXN0GEo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A3prnbLf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="A3prnbLf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B94CDC4CEE4; Tue, 10 Dec 2024 21:50:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733867441; bh=RRBl+ZZMmihwq3svB0M1vQmUUPQ7A6rMW0M3h5PQx34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A3prnbLfsY6KQzK//CrrzVTSX2P1eXC45Vr/x+G6mRozoJeC3hWO6Wn96Lt/kSN3R kuBCaznCZi/tcBYunW1jZhdpQIhdru1DGjJOeg7RKBqcAIPt4eNnkl33vmahTl6qDn gLLXKiYxnJx+hNAtADXtJUpwO2z92wVn9Joeoh1M5Nk3eRPSPDd3sTZHDv8WS2b8BA NlG8jmbhwla0yQoLpZ+dEsFJKrPfR1fJlWRQFFilwze64VEHfAsBFxICiAqZDKd6O1 df5yX0VKr8vDPUoY+PucGaICMPCBKi5EN5GkHVYlSRrQ9xN7v4CgpJpMHBAXyjvGDv +D0X/+nWEBc6A== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [PATCH 5/5] samples/damon/prcl: implement schemes setup Date: Tue, 10 Dec 2024 13:50:30 -0800 Message-Id: <20241210215030.85675-6-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241210215030.85675-1-sj@kernel.org> References: <20241210215030.85675-1-sj@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement a proactive cold memory regions reclaiming logic of prcl sample module using DAMOS. The logic treats memory regions that not accessed at all for five or more seconds as cold, and reclaim those as soon as found. Signed-off-by: SeongJae Park --- samples/damon/prcl.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/samples/damon/prcl.c b/samples/damon/prcl.c index b34b9bfed532..c3acbdab7a62 100644 --- a/samples/damon/prcl.c +++ b/samples/damon/prcl.c @@ -49,6 +49,7 @@ static int damon_sample_prcl_after_aggregate(struct damon= _ctx *c) static int damon_sample_prcl_start(void) { struct damon_target *target; + struct damos *scheme; =20 pr_info("start\n"); =20 @@ -75,6 +76,25 @@ static int damon_sample_prcl_start(void) =20 ctx->callback.after_aggregation =3D damon_sample_prcl_after_aggregate; =20 + scheme =3D damon_new_scheme( + &(struct damos_access_pattern) { + .min_sz_region =3D PAGE_SIZE, + .max_sz_region =3D ULONG_MAX, + .min_nr_accesses =3D 0, + .max_nr_accesses =3D 0, + .min_age_region =3D 50, + .max_age_region =3D UINT_MAX}, + DAMOS_PAGEOUT, + 0, + &(struct damos_quota){}, + &(struct damos_watermarks){}, + NUMA_NO_NODE); + if (!scheme) { + damon_destroy_ctx(ctx); + return -ENOMEM; + } + damon_set_schemes(ctx, &scheme, 1); + return damon_start(&ctx, 1, true); } =20 --=20 2.39.5