From patchwork Fri Dec 13 21:53:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13907897 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 804F8E77180 for ; Fri, 13 Dec 2024 21:53:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D16786B0098; Fri, 13 Dec 2024 16:53:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C79A06B0099; Fri, 13 Dec 2024 16:53:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7C426B009A; Fri, 13 Dec 2024 16:53:28 -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 7E0EE6B0098 for ; Fri, 13 Dec 2024 16:53:28 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id EDD56140245 for ; Fri, 13 Dec 2024 21:53:27 +0000 (UTC) X-FDA: 82891286856.10.74BF9C9 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 04F91160008 for ; Fri, 13 Dec 2024 21:53:08 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=m35yPDSV; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.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=1734126794; 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=SSI1DshWewQ1A1CtcKtsqO0YpAzbiInyST9+Hy185cs=; b=mOgvxt7+DpYyLnVqFtJvPRmWPoT6aeb3RkAof9eu1JeR3XfinihLms+62chCyu+xHby7/2 M1biLHkDXu7zw5QHXSqKOHyJEDoiHpCgk0RVs4sQCfhO6vTtFhCAR+0JXJ6HlwMDOKvgy2 c0j/qwlTspnqPayHbr1+vIwpXWKh4hY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734126794; a=rsa-sha256; cv=none; b=d3XPVGq/yQdjxIsuJ13OZEE5UUNlJUp6zr7TiXuEGUYlRWuR9U1cz5wLjYHbVeSqhtZBKu aps7MDSvd9Y+nXTOqX7I2ytc2OxADyEWjzcZJpgFnsTJXEOF0DIlJJbt/ifNrOB2NvJdA+ Xxs4SY5mTAKISTconI1NbYoaEC+Q9OE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=m35yPDSV; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf08.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 450F55C6A6E for ; Fri, 13 Dec 2024 21:52:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 33160C4CED6; Fri, 13 Dec 2024 21:53:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126805; bh=7p5Zd1Ss19X+LmH72iki7m+ul6ZR9lC5pFElBjMKsP4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=m35yPDSVC7fTsZNDdPl5J9+AKBe7MTTa/TNmqSlKnSyQeAsxwhux1PhTTRiRk6w+0 Q8XR1pO0ock3xaR8FRjMuL5ncDhohSGrFDooq3d+oF+yDIhqv61a9ppziz8H5muoqx lGmFcYJF5hKAn+eQyLXMnJZrLPkxAc5uhjc1O2Z/YUJJS3QOoqSAuJ/n/5mWGpQ2XU ImXGhlvcZgIIK5Y/C47hEc3oc/ABwmLMUmKTTdaUyrLXX5r/5ihtD1o/TJxfUx7S5C YO93+u7xxESrtllExf5OCN1Fegww4Ydbymh6p6/hlgDwWLgGGKtricJ/F5e81nwhhL 2C5/T7ICBB3Ow== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 8/9] mm/damon/sysfs: use damos_walk() for update_schemes_tried_{bytes,regions} Date: Fri, 13 Dec 2024 13:53:05 -0800 Message-Id: <20241213215306.54778-9-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20241213215306.54778-1-sj@kernel.org> References: <20241213215306.54778-1-sj@kernel.org> MIME-Version: 1.0 X-Stat-Signature: tumrmj3oyjs8nyi5a5ctbkrsuay7b3fe X-Rspamd-Queue-Id: 04F91160008 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1734126788-830461 X-HE-Meta: U2FsdGVkX19alpSQkEK+ksuQ01ZNbID5HVX+mPiTulBl0aGGB38MeC1z515Uu9fchPKMeM/Rbq+FotfTi6fhJmdWx8N0QAGmRPa+HUJsPAl4w7tt649jT3aQM747s9oSInqXNzn31NCDALV5+z+Ar+Kpdn3UWmBP44fv6NiY2Qx9O9A3DGpAUEfUYNY0FfEHjD4Hn3j8EfCW520rO2yL3NcAjhkaOiZ+/HzOgYiGizHvJImFu1tLDeFX5/NCtFN1sD8S1n8Vv83t5XIvwL97qDtA7I3fw2AESEd7Qdle+i3PmGigP1m84FeYhWkpo9bs61gSFFlRvsasOpdYU62LsSIdZg6nURP/lbffzJ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edence: 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 6c8f9eae75af..404c21f45640 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -2188,6 +2188,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 978de4305f2b..dafeec20ac53 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);