From patchwork Wed Nov 13 16:26:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13874000 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 0A21AD462D3 for ; Wed, 13 Nov 2024 16:26:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 710096B00AE; Wed, 13 Nov 2024 11:26:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BFC66B00AF; Wed, 13 Nov 2024 11:26:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 49C156B00B0; Wed, 13 Nov 2024 11:26:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 248FE6B00AE for ; Wed, 13 Nov 2024 11:26:36 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DAE63140BBE for ; Wed, 13 Nov 2024 16:26:35 +0000 (UTC) X-FDA: 82781599488.02.CD3D0A6 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id 57910180006 for ; Wed, 13 Nov 2024 16:25:51 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tzYlZYVW; spf=pass (imf16.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=1731514959; 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=wadKFwZTLfnao1fH9keMi+FTI4wW4tk3hkTRL9IV7a0=; b=8qo6RHgo/C3BDyVYwTr0DNS+qU9jDSFzz1PnrvARpkS97e1+dRn4ITfPkCef1EYtRH74nV iAnRq6g5hQ8QD1E2Kn7NmXyBbiz8l++blaqFjwNMdjyH+VA6oZCGlGLQ+RFmYX4S4HQ9eu z6n6Lj383qHGtzOr3AZJMzLHWDgDuqs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=tzYlZYVW; spf=pass (imf16.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=1731514959; a=rsa-sha256; cv=none; b=PgRbZNHn5wQnQWNDrJU/ydZdiL3ad1r2fIR8BaC1b6o3D1H/mLVZSfuRmOfGoWJ5cndbcK aUrC59H0kKxcrqKtqQ0vV6/WfNI5+eLTZbj9SAN9/6LBByfLNxy2r+l4WlyZiGa5YT/DYq T5lLKN63zixaGsDTunfvgpDwerB0hrc= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 05EC55C598C; Wed, 13 Nov 2024 16:25:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F09AEC4CEDA; Wed, 13 Nov 2024 16:26:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1731515193; bh=nvp2mMbvRyEYpGGB8GVuStSGx5/+sBiAwPQ8jRBowzE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tzYlZYVWfpmFdoSg5mqCA2/vMt8lNAryL/Qeyopf1laH1M7ZHFQ8p0ykW9VOdcJh8 QERHe9jyUv6PPQWlcy+OZQmMaVZUKzvcTAtpSDf8U1phkVpOe2bzKcf+6Qiqk0Btm0 ZjRmiMhGClDT1cBdOeFLeNqM0iHlUF2cs56Wu4r0rTvkZ1vZ+Q3FnemvCgZ7aiIIFK NFwiOxsY/h4+PN42u737de7plor3eb0vrolvMCpzzIuiubuDelDYWAUqvwUPy4V1pP FBL0v4F4mKXSEs1Sc94U7GsviRoyS7F/h8vtH3Js7xQrYCZSBNaaKRlUM/h/Arvj6V shacV75K8YH0Q== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com Subject: [RFC PATCH 4/5] samples/damon: introduce a skeleton of a smaple DAMON module for proactive reclamation Date: Wed, 13 Nov 2024 08:26:23 -0800 Message-Id: <20241113162624.48973-5-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241113162624.48973-1-sj@kernel.org> References: <20241113162624.48973-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 57910180006 X-Stat-Signature: 8a6k7s9t4yucbawqdr9qmgntdtzkcanp X-Rspam-User: X-HE-Tag: 1731515151-977252 X-HE-Meta: U2FsdGVkX18MGlwSndkWcPyICH5Tmxz+yVnbTxhUMzwByd+aXXjMcZw9/oGrpv55EIlvL4XAaMZf8A226J2XZZtPJTLWrjqXsw+gGbdKuam5JUU4uwBXwcmR/S00MmVv/PPg13BAZsTPO86naWvlJEWRBqHcR9rN3kRLJYu0mgN3nXZB9BT+bKhtZ6uVZz+Q7VB9to3FIwM7NpsIUsm1Ay1RS/qIghJpVm6+L/DSmr/XdbhmdkkTGHPmZ7gUrpfz6DpQbjdzANwRwrKVrSY2vkCIBfVp8qogVayzMB0YmebUJMLUYzGXQG4P6FDf9mskNA6D57FCVgEI2OMN/ust8nAdRJ3oGmEdapVkXQhROVQFvU0tAjnAwnxOaBSUM2xEl9vN9kPeghG9ZVqjkFlq9UDRmgEWHC3THLbeGu1jH9L1574LHHoQDHnwCkvt6uoUzjUt0kQAtq6wNE9s+Ms0rQLB59h2GHQ2QjuBV9spwbWYW21ioge6D+wAofk8IMg+tLJEs9arKEinKuSDyiRubmsWTMiDw3xffZk0qHOCSn2EjhhC9UUgN0TSPG9AtEpxqYPGOSbBZf306mtB7vVXmBAkXded44vTddZS+tVIsnjiTW87Oi13KVuwdzrFgEsikJWaEuBfvHNAWKuGGdGMcEbGCZDaNK91jIp40PYO4bB4yBqYD3KVB8TaeMD0mMAqAnF+J27sV3gllV3zPIKM6ef4VVNH8Cu+umm4K+1VE6cqQmwOTRBmXSn4TjwNX01ieiSjFIH+sL+nzU5lOwAb8OlNTymkQ9vyZdBmZiYYwE5GqSkwDck5TsHqz/YE/2H9qw0tlFdfRJm9zcbOjvV3yZz98+fSkGZSqrFp1tqiGAsfJP0Ndp8KaustkmrhmKMniMXD1t9y+Kd3CuMWKu7nHjYeH6xUK/bRzV5Y+GfrElN4pQdC/Zi+rIP47jR2YE0UwypT9ZOQQ762P5kA02Q 1q2smHqf ZPkr1nkInISTA6Cg9Y41Ijw7PyMCLi9tPdZZuYTmgWpukblzYpslLWyQnuMOiTWmxoPjCQiKpyOGUjrrufVsxn4kuAFTIdWu+B1Ss+FhkWfcAl04egHlOW80UphEY3TSppvOlgs6PqN7uAebI6AdycRcdxK4DNNnMmmGyq7B1yPCRUSMOvjbKIpIUT9ae5CWhpMCFkuhZCe1l78bL2BTS9KmUgYr/s+ejXhNsVMkyp5FynNY0V7RahQ5d7EGAkWC19ZI3U9MI29QwpzA= 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: Add a skeleton for a sample DAMON module that can be used for proactively reclaiming cold memory regions of a given process. The skeleton is actually same to working set size estimation sample module, but only the names of the file and symbols are changed. Signed-off-by: SeongJae Park --- samples/Makefile | 1 + samples/damon/Kconfig | 13 +++++ samples/damon/Makefile | 1 + samples/damon/prcl.c | 120 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 135 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..32ccdd91cf55 --- /dev/null +++ b/samples/damon/prcl.c @@ -0,0 +1,120 @@ +// 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); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("SeongJae Park"); +MODULE_DESCRIPTION("DAMON sample module for proactive reclamation");