From patchwork Tue Dec 10 21:50:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13902112 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4DDFE77180 for ; Tue, 10 Dec 2024 21:50:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 529B56B027C; Tue, 10 Dec 2024 16:50:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D9178D0015; Tue, 10 Dec 2024 16:50:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A1768D0013; Tue, 10 Dec 2024 16:50:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 14F3A6B027C for ; Tue, 10 Dec 2024 16:50:44 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id BD97E140B46 for ; Tue, 10 Dec 2024 21:50:43 +0000 (UTC) X-FDA: 82880393358.19.A46B629 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf19.hostedemail.com (Postfix) with ESMTP id 5E4EA1A0003 for ; Tue, 10 Dec 2024 21:50:18 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=D15Zzayf; spf=pass (imf19.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733867426; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DuzyJcswBf9jWR7yuAK6RUfkkDL4ys1ZA7CmNccAvPA=; b=nJ6GK+FJQC2xn//uMR+IoTQXagQK8JooE0tsc56I/UzhY9We9yUXzmYmcZea2tmucSttvB EFmfscX2CcqbriQrYNwuY59VWCHQXbVqavR7sQNqsxU7PKJVi+VlEc/e6pfb3WbKdaCFLK EFHhpUj0oe0haX/VpPxZo71jfBzF5zs= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=D15Zzayf; spf=pass (imf19.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733867427; a=rsa-sha256; cv=none; b=6FP1pC3yxuKZdxmvwCaV/KtiGdCXtgcuOZcMN2Ryt0JyjlSO4sdD9XcAOqNYNpngrLPqr6 b0jEY+MxIXRTqSgPi2h3nv2gUBRebYoX9uafAwqrKD+P+VgLuDmJMN75l/SnRTpXQuz8pf yu4GbvmIzx2Mstbfw1z+4RG2jyMsTiA= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 7DDE35C62B9; Tue, 10 Dec 2024 21:49:58 +0000 (UTC) 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> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Stat-Signature: 3dwux7wxcef373s755zi4k9c7896udhd X-Rspamd-Queue-Id: 5E4EA1A0003 X-Rspam-User: X-HE-Tag: 1733867418-18263 X-HE-Meta: U2FsdGVkX18qfYRVmWiFTpbw3ZkEpN7GagmSLrZPsXxi34ek4t30/uhWpaME5CxsiTK39jzDAa6KZn7Qcy+CCU6YtAg9lbnSmXYDlLngPmB6Xz2qo35ySk+gj+yFO7VwN9XQeV3lpFZDzvsX39xCoUms41GUhEIhIuvLNwEmaTNoBnDSwVWIRyLLa67h1m54aZWOX9oHesvA5j41Y964olZzCj8wCczYyiQ4Piwe4U7V5mDuxeWYzIgKn+GgVP3voK7wYeWhmfYydB2LAkQRxpFJz4g3GlsMYxa04x7S+YWMsUpFohFFmTkwdryCreg0sm5xrIfu0jNxoNQK+TejdaVDMC1yyYQtygyL3WPjN96b0tuAhwfg/ZBHN4zDS/js1yIIXB+4koxbE+uWBqXyLV856/2jMEqSxG5O/+4T7rKSQZ/i8FGxX8wzVR2SDJ68h3Yt9zNXxTbATK6QQBdzD6EJgbgrr+mn8T9rkPPUGOCll2XEFzJ33GWAfu2Ruzea2ECqvb9OrFPsygg3Gm1uNHRKXeFrROkKD+PnloedU2MQWxaUZ0WCIN5n2JPqE/KTG0BQKRU9Do4/w8eLZi0RLto1pcU4UalkC8nn1tEkHkfyK3H2KebGhq920UojiYiuWQlo5pTlcpWs7s8NMzTxx2mJ01vPOxDVfkpdoCONNvnjvrcJBur6nrTBLJQ6lnymB6ozB5C0K5kyj9Y3y/yGyMPLdNYY0Yb3RM0h+SBtLfSTSxs4+OIqLEFTl9G4kmLqLGwtaqbL/FRvrvP/6moj7dzJUNZL+SiY0FnvbwPmBZhpe/fnSNy/jv2irXR4uB8DWSzfH9/sVreourSI2YlMXkp5yZncuYMiQv4AGHzRm0Nnsf6IvBBDT9eWbHgte86wbbjQXjhYkYvVQYVO7V9tH8uye+/kKjjAF0544rATQHmxY2Bd9/dD+1ZK9OsR5SiHqaMtWbH2G9NZ8UwHcrn IsQf8dfI xHJ83QEXSPypB8/s+Md7xO4HviVpHF8flIbFFI5zmSll89GSP3NoIDf2tzvXPsSfJ1MKVn4V7jvlCM6TDUtwM52JYQRRQoCOjJfhwtzlAVzVEaONjO4K+aWQFB3G36v+wI2M3+JCFlxtmgOwPehnflEvaQpCPgUV1OFgl7wNB2VCMt5HnktjzDmra6Xtdg3i0amy4uGZlgqsIBGb5M9sl4xP8y6Okd4wgJFiPJEwC3LwnQIk8UrtK2C80vEDsuRpQHc9LaVE+plvOgk8= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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) += coresight/ obj-$(CONFIG_SAMPLE_FPROBE) += fprobe/ obj-$(CONFIG_SAMPLES_RUST) += rust/ obj-$(CONFIG_SAMPLE_DAMON_WSSE) += damon/ +obj-$(CONFIG_SAMPLE_DAMON_PRCL) += 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 If unsure, say N. +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 obj-$(CONFIG_SAMPLE_DAMON_WSSE) += wsse.o +obj-$(CONFIG_SAMPLE_DAMON_PRCL) += 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 = { + .set = damon_sample_prcl_enable_store, + .get = 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 = 0; + + damon_for_each_region(r, t) { + if (r->nr_accesses > 0) + wss += 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 = damon_new_ctx(); + if (!ctx) + return -ENOMEM; + if (damon_select_ops(ctx, DAMON_OPS_VADDR)) { + damon_destroy_ctx(ctx); + return -EINVAL; + } + + target = damon_new_target(); + if (!target) { + damon_destroy_ctx(ctx); + return -ENOMEM; + } + damon_add_target(ctx, target); + target_pidp = find_get_pid(target_pid); + if (!target_pidp) { + damon_destroy_ctx(ctx); + return -EINVAL; + } + target->pid = target_pidp; + + ctx->callback.after_aggregation = 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 = enable; + int err; + + err = kstrtobool(val, &enable); + if (err) + return err; + + if (enable == 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);