From patchwork Fri Jan 3 17:43:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13925767 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 86103E77188 for ; Fri, 3 Jan 2025 17:44:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EE0F16B009C; Fri, 3 Jan 2025 12:44:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E8FA06B009D; Fri, 3 Jan 2025 12:44:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0B1A6B009E; Fri, 3 Jan 2025 12:44:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A1D406B009C for ; Fri, 3 Jan 2025 12:44:23 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4B0B38095F for ; Fri, 3 Jan 2025 17:44:23 +0000 (UTC) X-FDA: 82966863252.26.F671475 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf07.hostedemail.com (Postfix) with ESMTP id C5E0040015 for ; Fri, 3 Jan 2025 17:42:45 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hJjyRE9+; spf=pass (imf07.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 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=1735926213; a=rsa-sha256; cv=none; b=xIwKXtI4OzVj4vxgosIskJEwMHT3zQTGm3Klyipyc5nSYPfDy4CMeZAg2tzDYUEZAzQbpf DPA6/xk3KfmOAOajEMihKVsirZDshBMzuPQb0n3GRjqr7ksBgJmTujqTs+vZoTxRczB1hC tQfyVSwK6s+NhykwChaIg/MCvOGFvU0= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=hJjyRE9+; spf=pass (imf07.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 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=1735926213; 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=n2QQrOJXhF/D7O++uXiKjCaUfKEUp/yhSHDO13AikpM=; b=0DU7jyXCbDQWYZh4t1APkd+HT9B7CDuky0+kC9ebXzzOULolQhtC5/SyY8Qz0qCGByReWF 10W6rsw9oVU2j6wht1QNfvubEKZvbyF4iV5NCpR7mopBmkdcw4ijpgP3nxZ4V96IiS85v+ FrTmtOuq0A5SmUFhAcL1yG+QpescM6M= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id E04D7A41042; Fri, 3 Jan 2025 17:42:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF26CC4CECE; Fri, 3 Jan 2025 17:44:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1735926260; bh=0AG/RC/M0bhm6WeM0GMTuPdy7IYbXvGsIfaoLaPfpCY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hJjyRE9+8H54mkpZ6BRFxdU14eB4Phkr6Ho40LTYktzdahdxWg1oftRRevZOtCu3f Qautu2v6d+NS6ozj/xOddO7xD9bEMTrAt8NOqv708mPGoOac9PiK/Ea/y65nPfx3N8 pfeeQwuJ2H1hcdhXBTwc9Gk5Y9WhPnJG3kD6gWIS4at0P4vocjSA+cxHXSjux2L7QD GTueVv+Wtt+5A93Q9NV099U3xDMSsEjSG3AhE6u7Ls8T/ZfYClEjqRZwdQ+oxFjYFf 1Hzu2poCoxe5n0guUXVGrEDKeZ2+Jf1BgsCFNb0hmRi3E43/QubKc5Fa6M9OgWrVd8 mNQPMKt0hD9tw== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 09/10] mm/damon/sysfs: use damos_walk() for update_schemes_tried_{bytes,regions} Date: Fri, 3 Jan 2025 09:43:59 -0800 Message-Id: <20250103174400.54890-10-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250103174400.54890-1-sj@kernel.org> References: <20250103174400.54890-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: C5E0040015 X-Stat-Signature: t6w53dheict6dum8at5uzurcier8i5a9 X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1735926165-522534 X-HE-Meta: U2FsdGVkX18k+xCBBLPc3sNY0ixqrb7ALb0nTJEo5Z2rkqjOwMZYx8zYTYHK7OZauyerrM3D7zFY8ldaXL7C0Iu1cTiBxTiSXBOf13pgOWxFGTLKehjq+ncFyskXLulADlWP26SAbIj/psT+1y+LU75Cw63Ec3FSiOF+mMcBI61Qsz3mX+e+VtCBGjLfRdc2vCjFQZn0qKf0SxP131vdXgIGDc+39gmRugVzLo0dS/H9BqzIXH/fZOrSzwB7U4fpfBNIEHRLF/0vgOhHLK4eTNj/SZw19mizFhE/65dNYPOTFBcXv99MrIi5eZ693DeYIP9IL78Taeue9Q2HzHO20ecrYfcbS/CS1L60MlDWs5j/db8gdcHqhSyZofSM7pn+lRc8isedJ3abCWiC7OH9eW7d0F/fHzQrCEAssD9Emfo35sSSPDr7F7DgynfVWlUfEgMC42Z+q0T4Z5jWpRao64z1w3cnQngc9xERiBRdT1aj0k2gwzJSwzrPo7QZcR5CruoIq1Q3ihr651yFCz9HJUI5mSAHxXPPzQ1eYUBUZ8+DoMyhS46NU/v7Qvv4url21O+uxzsYLMn47clExNRQgQVJm+F08ic2fP+eueFxdyPtDeK5/T+yDoJ63Dcqw6nT+VAi5jHBo7m0W/Cugo9/G377+Zu1Ci/yPBfXuCoFwXTzRPwttipsqfT0tm9UsfV+CkMwg99/+f0Kw+FdaA6OjBlb+cSwky3zwoaPxk6MhZdDzWNbc+++XgxPSdssDRB/g1jU8HOO1UUBgbdxxJyeMXItaDDjpsZIEsHGgdm3beXQOzaCfJO2BYo35QvH//2hIJm+TuUV0xSuaR0QPu1WM0naLyOzYbEDpkzNntVyp0x5sVJaPcH9ZcQaqXUhoqEE2DaFSuniboT5gpK/QteVk5/zjvmW2NsKplQZPR1AK4RSTDL6/s4Xx/85blZUfrqhy/XD2j6y82U0otLXKaa 90ij3vpl sbJF6/FP1i8QyNZebNmJs+MHEslqZ6rhNoLJhdqidQxJy02RG484AcD4TWy5HDGbE9LrIvzr2L7+ygT9PEw965Vd6Wuo19i3QDcne8UC2Njs3cKW7qfFKGdzVzfwbr73uRhlArX1CiLHLDqTLPH6oMA2KfQvdmbVn6a4ISAtb7ZkJDJyw3f68NkuMIXlbOTGjKudQaLc3Db8xLpa6EzuWvs7+Nh9U0EJp6lhK08iIYp/+rkjsr3SLCBsmGU092cOXJyVIaWuFs/1rg437UnNRElOm2A== 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 sysfs interface uses damon_callback with its own complicated synchronization facility to handle update_schemes_tried_bytes and update_schemes_tried_regions commands. But damos_walk() can support the use case without the additional synchronizations. Convert the code to use damos_walk() instead. Signed-off-by: SeongJae Park --- mm/damon/sysfs-common.h | 5 +++++ mm/damon/sysfs-schemes.c | 48 ++++++++++++++++++++++++++++++++++++++++ mm/damon/sysfs.c | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) diff --git a/mm/damon/sysfs-common.h b/mm/damon/sysfs-common.h index e79b4a65ff2d..81f1c845118f 100644 --- a/mm/damon/sysfs-common.h +++ b/mm/damon/sysfs-common.h @@ -55,6 +55,11 @@ bool damos_sysfs_regions_upd_done(void); int damon_sysfs_schemes_update_regions_stop(struct damon_ctx *ctx); +void damos_sysfs_populate_region_dir(struct damon_sysfs_schemes *sysfs_schemes, + struct damon_ctx *ctx, struct damon_target *t, + struct damon_region *r, struct damos *s, + bool total_bytes_only); + int damon_sysfs_schemes_clear_regions( struct damon_sysfs_schemes *sysfs_schemes); diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index c57ab47686ff..1d8ad637051a 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -2183,6 +2183,54 @@ static int damon_sysfs_before_damos_apply(struct damon_ctx *ctx, return 0; } +/** + * damos_sysfs_populate_region_dir() - Populate a schemes tried region dir. + * @sysfs_schemes: Schemes directory to populate regions directory. + * @ctx: Corresponding DAMON context. + * @t: DAMON target of @r. + * @r: DAMON region to populate the directory for. + * @s: Corresponding scheme. + * @total_bytes_only: Whether the request is for bytes update only. + * + * Called from DAMOS walk callback while holding damon_sysfs_lock. + */ +void damos_sysfs_populate_region_dir(struct damon_sysfs_schemes *sysfs_schemes, + struct damon_ctx *ctx, struct damon_target *t, + struct damon_region *r, struct damos *s, bool total_bytes_only) +{ + struct damos *scheme; + struct damon_sysfs_scheme_regions *sysfs_regions; + struct damon_sysfs_scheme_region *region; + int schemes_idx = 0; + + damon_for_each_scheme(scheme, ctx) { + if (scheme == s) + break; + schemes_idx++; + } + + /* user could have removed the scheme sysfs dir */ + if (schemes_idx >= sysfs_schemes->nr) + return; + + sysfs_regions = sysfs_schemes->schemes_arr[schemes_idx]->tried_regions; + sysfs_regions->total_bytes += r->ar.end - r->ar.start; + if (total_bytes_only) + return; + + region = damon_sysfs_scheme_region_alloc(r); + if (!region) + return; + list_add_tail(®ion->list, &sysfs_regions->regions_list); + sysfs_regions->nr_regions++; + if (kobject_init_and_add(®ion->kobj, + &damon_sysfs_scheme_region_ktype, + &sysfs_regions->kobj, "%d", + sysfs_regions->nr_regions++)) { + kobject_put(®ion->kobj); + } +} + /* * DAMON callback that called after each accesses sampling. While this * callback is registered, damon_sysfs_lock should be held to ensure the diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 917e6aca3f58..4ba44a314f2e 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1512,6 +1512,45 @@ static int damon_sysfs_damon_call(int (*fn)(void *data), return damon_call(kdamond->damon_ctx, &call_control); } +struct damon_sysfs_schemes_walk_data { + struct damon_sysfs_kdamond *sysfs_kdamond; + bool total_bytes_only; +}; + +/* populate the region directory */ +static void damon_sysfs_schemes_tried_regions_upd_one(void *data, struct damon_ctx *ctx, + struct damon_target *t, struct damon_region *r, + struct damos *s) +{ + struct damon_sysfs_schemes_walk_data *walk_data = data; + struct damon_sysfs_kdamond *sysfs_kdamond = walk_data->sysfs_kdamond; + + damos_sysfs_populate_region_dir( + sysfs_kdamond->contexts->contexts_arr[0]->schemes, + ctx, t, r, s, walk_data->total_bytes_only); +} + +static int damon_sysfs_update_schemes_tried_regions( + struct damon_sysfs_kdamond *sysfs_kdamond, bool total_bytes_only) +{ + struct damon_sysfs_schemes_walk_data walk_data = { + .sysfs_kdamond = sysfs_kdamond, + .total_bytes_only = total_bytes_only, + }; + struct damos_walk_control control = { + .walk_fn = damon_sysfs_schemes_tried_regions_upd_one, + .data = &walk_data, + }; + struct damon_ctx *ctx = sysfs_kdamond->damon_ctx; + + if (!ctx) + return -EINVAL; + + damon_sysfs_schemes_clear_regions( + sysfs_kdamond->contexts->contexts_arr[0]->schemes); + return damos_walk(ctx, &control); +} + /* * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond. * @cmd: The command to handle. @@ -1542,6 +1581,10 @@ static int damon_sysfs_handle_cmd(enum damon_sysfs_cmd cmd, case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: return damon_sysfs_damon_call( damon_sysfs_upd_schemes_stats, kdamond); + case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES: + return damon_sysfs_update_schemes_tried_regions(kdamond, true); + case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS: + return damon_sysfs_update_schemes_tried_regions(kdamond, false); case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS: return damon_sysfs_schemes_clear_regions( kdamond->contexts->contexts_arr[0]->schemes);