From patchwork Tue Dec 5 02:28:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13479311 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 79C81C10DCE for ; Tue, 5 Dec 2023 02:29:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4AFB6B0081; Mon, 4 Dec 2023 21:29:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CF9EE6B0083; Mon, 4 Dec 2023 21:29:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B74106B0087; Mon, 4 Dec 2023 21:29:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A13AD6B0083 for ; Mon, 4 Dec 2023 21:29:11 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7661BC019E for ; Tue, 5 Dec 2023 02:29:11 +0000 (UTC) X-FDA: 81531182502.05.F222AEA Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf03.hostedemail.com (Postfix) with ESMTP id C9AA120002 for ; Tue, 5 Dec 2023 02:29:09 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jngeDOXK; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1701743349; 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=X9ug2oDHbNJiQ6+d1a5HspawVsN24THkddLE+A501iI=; b=uZIyBPjJK9MLEVvvxny9XuVuOaZ4wQtxbK+lZIi2F0hILqWZrTjYeYbguMx1Zsmsm7tKYN L+fI0WR1paTF1AGGHmDrOLaRRrH82EJPbi0dvtxkPhjhkXIqLN3H1bVVZCpiFGckvaZU4g 4MfKtoHpyw44+AZ594B3HLPFi6nf7+8= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=jngeDOXK; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf03.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701743349; a=rsa-sha256; cv=none; b=T++jRAcgOq8D9JGWej2k96kRxJ5jsllBcbtlOwH6Iqm4W0QyaN0yAL+1PHvoHRwokUaI0n joYNemf52wjkOtGJJrWuHzzYAyjnjea5SH8QeEQU7SWPnY5E+pzL183d6+4dQPtOi+Yb/d LtzHTkZVywjMW9dViRwlDP7rUvqevHY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 146AC61477; Tue, 5 Dec 2023 02:29:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4EF36C433C7; Tue, 5 Dec 2023 02:29:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701743348; bh=E477KEEpVfXYBLYCDBbDHRJITkivLWed5UVr6LwW/WM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jngeDOXKER4A8iR1WtRk2Q+VSSXWfsEjUkLKHDo5gl9pr5B546BF+p4Ih9ogHv/gd 3j69ZEx7YNQra9zB8utvELkTvedAI3ffNHroQG1gcpuQah3zv+sddfG/nNHJn9ou6S V0w2KF3S6nz+YbsqEFzG+OzOZhS4KVVv6Kn1osxhYFIOLBAbQcEh2xNbZeBSZMHLjN rmFPkmKu5dKkqh+PyAVPcNDF0TKhEyCuWIYnRSxwIjfg2tI9e/JnNNypV5nyWeHzks t4LyV4p1qOS8rNrGRHVZ64HFNT8LC3wuXLEBuNYV8ZZfKVtkbe2iA31eO8zza3RRqJ mwHIOcSmOtT0Q== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] samples: add proactive reclamation DAMON sample module Date: Tue, 5 Dec 2023 02:28:58 +0000 Message-Id: <20231205022858.1540-4-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231205022858.1540-1-sj@kernel.org> References: <20231205022858.1540-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C9AA120002 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: nps51a6xz7nmzto7zbniuc7wpaxrsm6s X-HE-Tag: 1701743349-767408 X-HE-Meta: U2FsdGVkX19h1H7L0F9Y5G5ZLecFdqv86d8+L3wn4mJnpjzC5EKbA40/oPlYOlw+u8Fmel9hEl0zI3SnIcaw5/D5dXVi5kOaHdfFQgonlFoZdqqN8FdMLsSM6mnvRGH++wV9dvt5q8L9CxNNYAQWoBF19mV4fS7p4ifW1vEfDTzw/C7i6jxn0ZZx0yZyHF225cQHQaMg7BfHPySDhwgeKwNv0aTfikvw+r5vwBnbumIW7Ccj75mPD2GEy/0kpDT7741hZEAPe8sg0aHTLwWtjGTIgCu+qQfINbHyZeveIu8m5v3Bl58E7vqz2NicfAu6ViW/d2AHF4THADkQ8JS4+8UVkXs2OFQVxNKV10rUdh7vtdnVaHY6tQGV9KEhAds5O/cMzBpekvSLMqkJP+Fan234+B0bn4Z7xwKfeOXzDrjbCB2tteeUJ3B5/CmL2LePe8NMzJ1llTw0SQATmRYRTPcm/FtV3HQkQjtGn4KkqzpslJi5a5dfinGQZz0ibLtYtmmeE7R9QB7/1k/UeTaAUXPmSROzNQ8uut3214jHmQ/XrKhMFOTyApQUYKgDOo7zWiaBMone0V7ZOquiji33WeqwgWhXop04Z4I1xUVY+OQz4ErhQ2rSxE//wVFUI53CYOVyfym1F3xvcyix9rpVZ34XYHWP6B9XgnlfRSU9mSNWJSaAv6ix7Jds4m/S7bn6h3UYkovy7GxxSq6lFTagm1agMsGKln2KhUcsuOjJpU61K9vKo82EyYc8mOs4cxhXy23g+EfH2njJzjKs91sW35m7HOrfrYB5AV3ApIFOgK9coLmwBDGo/mbqT17bnmAeMQuzfy1eGx/TijpOBPmJWjHYC7FegVNEUmaEnup1HlgD7R6RRajXMh3Cp2ubJI59TeYDnBbyFq+3iTk0r8cn7zbdarGX/NewTHmhVYmDv04QYizpRJSBlHX58uCoiE/azpq96pmpNgtUMzv9lmv 2Jvy8iMv OCpNBNfqS6qCV1IrYVHQDVlJA+fkzn8nF5sSgA5V53STPk34kBrWBoTxAWG2nAOTwZNghTTytoRnsRCN/oDWcqICrBs2MUd7xOss9UBwCMxh9euf+vVH6lb6xsZHkbFBt5xL/B2GOWIwgnmTlCVDmJgKWoQw59rK1C4FRgI/oVs5ImXlPIg7TZa45zX2z6HiGgRbLiSwS2fUunKo6DmYPeMJ+SvBjw10NLnTTN+fL7U5ydzOyg5q9ilD/CXFIGPmh779ZoyJrUlVYCnlX+VYUivabMcIbGkP8AFvyXMdRXHSDxD8= 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 sample DAMON application kernel module, namely damon_sample_prcl. It is same to the one of the example kernel modules that presented at kernel summit 2021 with 10 minutes live coding[1] but updated for the latest DAMON interface. The sample module receives a pid of a process to monitor the access, monitors the access to the virtual address space of the process, find regions that not accessed and proactively reclaim those using DAMOS. [1] https://linuxplumbersconf.org/event/11/contributions/984/ Signed-off-by: SeongJae Park --- samples/damon/Kconfig | 13 ++++ samples/damon/Makefile | 1 + samples/damon/damon_sample_prcl.c | 102 ++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 samples/damon/damon_sample_prcl.c diff --git a/samples/damon/Kconfig b/samples/damon/Kconfig index f8b9a73717f9..459042ce5958 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 + tristate "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 pid of a process, 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 1ace3ad4aff0..509f6a619dc4 100644 --- a/samples/damon/Makefile +++ b/samples/damon/Makefile @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0 obj-$(CONFIG_SAMPLE_DAMON_WSSE) += damon_sample_wsse.o +obj-$(CONFIG_SAMPLE_DAMON_PRCL) += damon_sample_prcl.o diff --git a/samples/damon/damon_sample_prcl.c b/samples/damon/damon_sample_prcl.c new file mode 100644 index 000000000000..487196f18864 --- /dev/null +++ b/samples/damon/damon_sample_prcl.c @@ -0,0 +1,102 @@ +// 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 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 __init damon_sample_prcl_init(void) +{ + struct damon_target *target; + struct damos *scheme; + + pr_info("Hello\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; + + scheme = damon_new_scheme( + &(struct damos_access_pattern) { + .min_sz_region = PAGE_SIZE, + .max_sz_region = ULONG_MAX, + .min_nr_accesses = 0, + .max_nr_accesses = 0}, + DAMOS_PAGEOUT, + 0, + &(struct damos_quota){}, + &(struct damos_watermarks){}); + if (!scheme) { + damon_destroy_ctx(ctx); + return -ENOMEM; + } + damon_set_schemes(ctx, &scheme, 1); + + return damon_start(&ctx, 1, true); +} + +static void __exit damon_sample_prcl_exit(void) +{ + pr_info("Goodbye\n"); + if (ctx) { + damon_stop(&ctx, 1); + damon_destroy_ctx(ctx); + } + if (target_pidp) + put_pid(target_pidp); +} + +module_init(damon_sample_prcl_init); +module_exit(damon_sample_prcl_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("SeongJae Park"); +MODULE_DESCRIPTION("DAMON sample module for proactive reclamation");