From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58438196446; Fri, 13 Dec 2024 21:53:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126799; cv=none; b=S9pJj7PJcI+Ex1Rf5lmDc1DU4k59Kf7n/cxrD/M6Yj1Hn3Gm9IsDcl+YG2IoBxhaxWkhMTatzipIhQ1OeuXZM0yeKtSAWUhD7iVvYjrzLasPrk//aIdMOu7YYNVioc9FIOPmbPmlwA+6xHLu1VYePLUJn748tmbL4swZyYsNeK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126799; c=relaxed/simple; bh=LaL2B6Z67E0pzAwlb61QS3yiBPbVveY7ewg0M8543bU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WBcIoCLxjFO808PUeFXHpnYCeoskNNUCdtj6HzG2A7OHYRUMJEiMVNFHQfBddIM1uSmBn+TLLM7eDv3ADPW6//cvEhPLS+m7L+0G2Udhw7sHWwkW1F3w6bmkq+M/fpHezXpbQAoIN45GaVPA8yocX0Cbaa+KXMggk50UGI/djMI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JIHoa8xv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JIHoa8xv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4042C4CED4; Fri, 13 Dec 2024 21:53:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126797; bh=LaL2B6Z67E0pzAwlb61QS3yiBPbVveY7ewg0M8543bU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JIHoa8xvHtBEgKfFgfmzr8JhU8wuYdm22oBq3aXrAJdLT92I+YnPf+q/gazur1Svt H9IJ4O5mRQQJIXElDQ0J31E4A7AQOGeqVPy0Kpz6Gz97Whk1qNbxbeYnFL6YMf0w9J NxZhLEBwNgdQUk/b0Ub7ZMqCbFcqfNH7vZchKvHkSjFdzUCUCuX4hExGnI4n0j6Kx3 YHsIFHkl/x3L5l3dE+8A2jdnvmMTWACMZu46ymdApCGVPC36eS13a+tXgzno5OIgt6 Ff5siVphwaYrP4Mc2Ef1h9jtHp3dfEItjBTppVVBUnBOXXa7UpW7Sfoh7BW6Ep/FCr oYIJWZBC0khMw== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/9] mm/damon/sysfs-schemes: remove unnecessary schemes existence check in damon_sysfs_schemes_clear_regions() Date: Fri, 13 Dec 2024 13:52:58 -0800 Message-Id: <20241213215306.54778-2-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" damon_sysfs_schemes_clear_regions() skips removing the scheme tried region directories only if the matching scheme is still ongoing. It is unnecessary check, since what users want is just removing the entire region directories. Remove the unnecessary check. Signed-off-by: SeongJae Park --- mm/damon/sysfs-common.h | 3 +-- mm/damon/sysfs-schemes.c | 16 +++++----------- mm/damon/sysfs.c | 2 +- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/mm/damon/sysfs-common.h b/mm/damon/sysfs-common.h index 9a18f3c535d3..e79b4a65ff2d 100644 --- a/mm/damon/sysfs-common.h +++ b/mm/damon/sysfs-common.h @@ -56,8 +56,7 @@ bool damos_sysfs_regions_upd_done(void); int damon_sysfs_schemes_update_regions_stop(struct damon_ctx *ctx); =20 int damon_sysfs_schemes_clear_regions( - struct damon_sysfs_schemes *sysfs_schemes, - struct damon_ctx *ctx); + struct damon_sysfs_schemes *sysfs_schemes); =20 int damos_sysfs_set_quota_scores(struct damon_sysfs_schemes *sysfs_schemes, struct damon_ctx *ctx); diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index 25356fe99273..65f6d3339a77 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -2213,20 +2213,14 @@ void damos_sysfs_mark_finished_regions_updates(stru= ct damon_ctx *ctx) =20 /* Called from damon_sysfs_cmd_request_callback under damon_sysfs_lock */ int damon_sysfs_schemes_clear_regions( - struct damon_sysfs_schemes *sysfs_schemes, - struct damon_ctx *ctx) + struct damon_sysfs_schemes *sysfs_schemes) { - struct damos *scheme; - int schemes_idx =3D 0; + int i; =20 - damon_for_each_scheme(scheme, ctx) { + for (i =3D 0; i < sysfs_schemes->nr; i++) { struct damon_sysfs_scheme *sysfs_scheme; =20 - /* user could have removed the scheme sysfs dir */ - if (schemes_idx >=3D sysfs_schemes->nr) - break; - - sysfs_scheme =3D sysfs_schemes->schemes_arr[schemes_idx++]; + sysfs_scheme =3D sysfs_schemes->schemes_arr[i]; damon_sysfs_scheme_regions_rm_dirs( sysfs_scheme->tried_regions); sysfs_scheme->tried_regions->total_bytes =3D 0; @@ -2276,7 +2270,7 @@ int damon_sysfs_schemes_update_regions_start( struct damon_sysfs_schemes *sysfs_schemes, struct damon_ctx *ctx, bool total_bytes_only) { - damon_sysfs_schemes_clear_regions(sysfs_schemes, ctx); + damon_sysfs_schemes_clear_regions(sysfs_schemes); damon_sysfs_schemes_for_damos_callback =3D sysfs_schemes; damos_tried_regions_init_upd_status(sysfs_schemes, ctx); damos_regions_upd_total_bytes_only =3D total_bytes_only; diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 4daac92be30b..71bc2622ab35 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1262,7 +1262,7 @@ static int damon_sysfs_clear_schemes_regions( if (!ctx) return -EINVAL; return damon_sysfs_schemes_clear_regions( - kdamond->contexts->contexts_arr[0]->schemes, ctx); + kdamond->contexts->contexts_arr[0]->schemes); } =20 static inline bool damon_sysfs_kdamond_running( --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6732F1974EA; Fri, 13 Dec 2024 21:53:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126800; cv=none; b=JLkgbuLTsMiSOTb6JvCawDFPpMtPFrW2HOs033iKxXArD7kxGBXh0kk56dIoIZY/lA4lxmceSmaFv+SHzv7k94FvIbP1NVIaqWNEdLPBZSXBqyK2//qPElkNsZpEHbDRnqHx+3W0EDIJ4m91f6GUdPUNYuCrDUsnr4mFaq05dfc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126800; c=relaxed/simple; bh=KeUP+pCvuxkILHBLCBzmbDq6X8tGBEnSsOyYCFO9ER4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UlTqAzYzoWhWSMpvaK5e1gY9pRP8EslwK6YGjsa6G6cv8A/RwGw2IXeK+rr09prkhkyY9z7yqV31cl/Oqu9fPiiRwZglA1mMzQQOPrjJAxqo2Cm8/6U559VeDOvoZWlLQe0E05JeE/zabG5n1cKCkqO+11cAYLXe7Cjx0LyMHfA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nfdfXvct; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nfdfXvct" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C029EC4CED0; Fri, 13 Dec 2024 21:53:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126798; bh=KeUP+pCvuxkILHBLCBzmbDq6X8tGBEnSsOyYCFO9ER4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nfdfXvct2DhdTzFkuAL1Stw3Y9RViraAPNw5F4RuvEghu7rw6op1PLPMfFfxpXCW0 5LJzRigU/GJ/mRfHzc9VQX6l6oHyTTVCjmB9NMg0DaZg8agdG/eD724XV5v3X9BMAi F8qDykQFR2H+N7nxiKbeCVnAwvj4lGlyycCkP1QBWzgUresQuwCq0rRtPS1Q1yTnwm jYmDkEEN8fr5jrWWDF4LZiZ8va00lghmI5bs2sfzaC4+pRXzfHZmrHofsw4b0jbcSx TOvfpyKgFLqU6NqZqvLD6isQxnHwsVeDea5QfsfInrora/kv2V1GBtWIlGzoQE31aU B9FT0mSE6X4Bg== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/9] mm/damon/sysfs: handle clear_schemes_tried_regions from DAMON sysfs context Date: Fri, 13 Dec 2024 13:52:59 -0800 Message-Id: <20241213215306.54778-3-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DAMON sysfs interface handles clear_schemes_tried_regions from DAMON callback context (damon_sysfs_cmd_request_callback()). But no DAMON internal data is accessed for the work. Directly handle it from DAMON sysfs interface context, namely damon_sysfs_handle_cmd(). Signed-off-by: SeongJae Park --- mm/damon/sysfs-schemes.c | 2 +- mm/damon/sysfs.c | 17 +++-------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index 65f6d3339a77..6c8f9eae75af 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -2265,7 +2265,7 @@ static void damos_tried_regions_init_upd_status( } } =20 -/* Called from damon_sysfs_cmd_request_callback under damon_sysfs_lock */ +/* Called while damon_sysfs_lock is hold */ int damon_sysfs_schemes_update_regions_start( struct damon_sysfs_schemes *sysfs_schemes, struct damon_ctx *ctx, bool total_bytes_only) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 71bc2622ab35..8cb940d21fbe 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1254,17 +1254,6 @@ static int damon_sysfs_upd_schemes_regions_stop( return damon_sysfs_schemes_update_regions_stop(ctx); } =20 -static int damon_sysfs_clear_schemes_regions( - struct damon_sysfs_kdamond *kdamond) -{ - struct damon_ctx *ctx =3D kdamond->damon_ctx; - - if (!ctx) - return -EINVAL; - return damon_sysfs_schemes_clear_regions( - kdamond->contexts->contexts_arr[0]->schemes); -} - static inline bool damon_sysfs_kdamond_running( struct damon_sysfs_kdamond *kdamond) { @@ -1417,9 +1406,6 @@ static int damon_sysfs_cmd_request_callback(struct da= mon_ctx *c, bool active, damon_sysfs_schemes_regions_updating =3D false; } break; - case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS: - err =3D damon_sysfs_clear_schemes_regions(kdamond); - break; case DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECTIVE_QUOTAS: err =3D damon_sysfs_upd_schemes_effective_quotas(kdamond); break; @@ -1549,6 +1535,9 @@ static int damon_sysfs_handle_cmd(enum damon_sysfs_cm= d cmd, return damon_sysfs_turn_damon_on(kdamond); case DAMON_SYSFS_CMD_OFF: return damon_sysfs_turn_damon_off(kdamond); + case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS: + return damon_sysfs_schemes_clear_regions( + kdamond->contexts->contexts_arr[0]->schemes); default: break; } --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67385197512; Fri, 13 Dec 2024 21:53:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126800; cv=none; b=QHz6NtsLV0iM8j/So4lB6F1AYQMGY0qu/vSafrL93XjQwSBCJy2MqA6TIRdBlWaQX9W1VKJ0f+WpToQr3hyCXrfFupKAdrJDoh1Yp0tpdVckUYCbLXUpvw9QICWLx4+DZe4MOA9WoRuSJm/t4yOkIIV1onjd0WSyhzSKUIsEtNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126800; c=relaxed/simple; bh=CuegZeDM4FxKE/fdac0FkRb85mikc51KmPkJxxesedI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tJGP21GynPJR68RghVIUeFRCm3bPh4y4qQ+jFtgG4fMTZjycQjuhkFjPpkZKBkA7AxKP9SrMgmU+8J2RkVXy+k9vRgp/o4QLU3thO4UF3kG/5VtdBj+B/hyBvbBrgVEt3T2MzmlgBalv1MP7pXxZmqIrmqKXcF06TNTcsKdfqTI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=X7Hv9Kwf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="X7Hv9Kwf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C62C1C4CED6; Fri, 13 Dec 2024 21:53:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126800; bh=CuegZeDM4FxKE/fdac0FkRb85mikc51KmPkJxxesedI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X7Hv9Kwf5zS9WP/rJRXgSfMY+UFevhNx7vSTcspG6xNCHaSbxxbX2m3bTvmne9I0G BEvAeYFmxKA621fItAOJdn4kpDz0A4qUQUeXuluyldhTqUnDKw7BwOuVF8wFUfs6Yl 3Cl5lVBeiP4uU7Rq9MSLiaMXsTNTIls70NWqvi+EcxgSt7vlJ5IYqBrWStt0jb/5VJ 7xaL2ZIRyCgzR5tL2wNDcrbyHFcGZs9+rQ3CQFLgF2c5x7vl7hljZaNitST7UlmboJ eSj3i3dA2IJNvIsqZYMVBY0Inj6CPul/Wu5Q1UoFb3hKd+ZAPBjyzbsXV+zkcXZlkd gUqLS67FqgmNQ== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 3/9] mm/damon/core: implement damon_call() Date: Fri, 13 Dec 2024 13:53:00 -0800 Message-Id: <20241213215306.54778-4-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a new DAMON core API function, damon_call(). It aims to replace some damon_callback usages that access damon_ctx of ongoing kdamond with additional synchronizations. It receives a function pointer, let the parallel kdamond invokes the function, and returns after the invocation is finished, or canceled due to some races. kdamond invokes the function inside the main loop after sampling is done. If it is deactivated by DAMOS watermarks or already out of the main loop, mark the request as canceled so that damon_call() can wakeup and return. Signed-off-by: SeongJae Park --- include/linux/damon.h | 26 +++++++++++++ mm/damon/core.c | 86 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index 10fc6df52111..529ea578f2d5 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -590,6 +590,27 @@ struct damon_callback { void (*before_terminate)(struct damon_ctx *context); }; =20 +/* + * struct damon_call_control - Control damon_call(). + * + * @fn: Function to be called back. + * @data: Data that will be passed to @fn. + * @return_code: Return code from @fn invocation. + * + * Control damon_call(), which requests specific kdamond to invoke a given + * function. Refer to damon_call() for more details. + */ +struct damon_call_control { + int (*fn)(void *data); + void *data; + int return_code; +/* private: internal use only */ + /* informs if the kdamond finished handling of the request */ + struct completion completion; + /* informs if the kdamond canceled @fn infocation */ + bool canceled; +}; + /** * struct damon_attrs - Monitoring attributes for accuracy/overhead contro= l. * @@ -670,6 +691,9 @@ struct damon_ctx { /* for scheme quotas prioritization */ unsigned long *regions_score_histogram; =20 + struct damon_call_control *call_control; + struct mutex call_control_lock; + /* public: */ struct task_struct *kdamond; struct mutex kdamond_lock; @@ -817,6 +841,8 @@ static inline unsigned int damon_max_nr_accesses(const = struct damon_attrs *attrs int damon_start(struct damon_ctx **ctxs, int nr_ctxs, bool exclusive); int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); =20 +int damon_call(struct damon_ctx *ctx, struct damon_call_control *control); + int damon_set_region_biggest_system_ram_default(struct damon_target *t, unsigned long *start, unsigned long *end); =20 diff --git a/mm/damon/core.c b/mm/damon/core.c index bf04987a91c6..89a679c06e30 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -533,6 +533,7 @@ struct damon_ctx *damon_new_ctx(void) ctx->next_ops_update_sis =3D 0; =20 mutex_init(&ctx->kdamond_lock); + mutex_init(&ctx->call_control_lock); =20 ctx->attrs.min_nr_regions =3D 10; ctx->attrs.max_nr_regions =3D 1000; @@ -1183,6 +1184,54 @@ int damon_stop(struct damon_ctx **ctxs, int nr_ctxs) return err; } =20 +static bool damon_is_running(struct damon_ctx *ctx) +{ + bool running; + + mutex_lock(&ctx->kdamond_lock); + running =3D ctx->kdamond !=3D NULL; + mutex_unlock(&ctx->kdamond_lock); + return running; +} + +/** + * damon_call() - Invoke a given function on DAMON worker thread (kdamond). + * @ctx: DAMON context to call the function for. + * @control: Control variable of the call request. + * + * Ask DAMON worker thread (kdamond) of @ctx to call a function with an + * argument data that respectively passed via &damon_call_control->fn and + * &damon_call_control->data of @control, and wait until the kdamond finis= hes + * handling of the request. + * + * The kdamond executes the function with the argument in the main loop, j= ust + * after a sampling of the iteration is finished. The function can hence + * safely access the internal data of the &struct damon_ctx without additi= onal + * synchronization. The return value of the function will be saved in + * &damon_call_control->return_code. + * + * Return: 0 on success, negative error code otherwise. + */ +int damon_call(struct damon_ctx *ctx, struct damon_call_control *control) +{ + init_completion(&control->completion); + control->canceled =3D false; + + mutex_lock(&ctx->call_control_lock); + if (ctx->call_control) { + mutex_unlock(&ctx->call_control_lock); + return -EBUSY; + } + ctx->call_control =3D control; + mutex_unlock(&ctx->call_control_lock); + if (!damon_is_running(ctx)) + return -EINVAL; + wait_for_completion(&control->completion); + if (control->canceled) + return -ECANCELED; + return 0; +} + /* * Reset the aggregated monitoring results ('nr_accesses' of each region). */ @@ -1970,6 +2019,39 @@ static void kdamond_usleep(unsigned long usecs) usleep_range_idle(usecs, usecs + 1); } =20 +/* + * kdamond_call() - handle damon_call_control. + * @ctx: The &struct damon_ctx of the kdamond. + * @cancel: Whether to cancel the invocation of the function. + * + * If there is a &struct damon_call_control request that registered via + * &damon_call() on @ctx, do or cancel the invocation of the function depe= nding + * on @cancel. @cancel is set when the kdamond is deactivated by DAMOS + * watermarks, or the kdamond is already out of the main loop and therefore + * will be terminated. + */ +static void kdamond_call(struct damon_ctx *ctx, bool cancel) +{ + struct damon_call_control *control; + int ret =3D 0; + + mutex_lock(&ctx->call_control_lock); + control =3D ctx->call_control; + mutex_unlock(&ctx->call_control_lock); + if (!control) + return; + if (cancel) { + control->canceled =3D true; + } else { + ret =3D control->fn(control->data); + control->return_code =3D ret; + } + complete(&control->completion); + mutex_lock(&ctx->call_control_lock); + ctx->call_control =3D NULL; + mutex_unlock(&ctx->call_control_lock); +} + /* Returns negative error code if it's not activated but should return */ static int kdamond_wait_activation(struct damon_ctx *ctx) { @@ -1994,6 +2076,7 @@ static int kdamond_wait_activation(struct damon_ctx *= ctx) if (ctx->callback.after_wmarks_check && ctx->callback.after_wmarks_check(ctx)) break; + kdamond_call(ctx, true); } return -EBUSY; } @@ -2065,6 +2148,7 @@ static int kdamond_fn(void *data) if (ctx->callback.after_sampling && ctx->callback.after_sampling(ctx)) break; + kdamond_call(ctx, false); =20 kdamond_usleep(sample_interval); ctx->passed_sample_intervals++; @@ -2126,6 +2210,8 @@ static int kdamond_fn(void *data) ctx->kdamond =3D NULL; mutex_unlock(&ctx->kdamond_lock); =20 + kdamond_call(ctx, true); + mutex_lock(&damon_lock); nr_running_ctxs--; if (!nr_running_ctxs && running_exclusive_ctxs) --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23F171991C3; Fri, 13 Dec 2024 21:53:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126801; cv=none; b=kEwdB6MUy11J1zgFbFxFah6tIRBNEvCoaKppzs68Hm9rQHW3WLsCpBV7gzEU/j1lBfriFPFfzBuA32lyL2qrzuUj3dI9DPnopmLys0JmD/Ppj0Ot5DWNyUrQCCmSYhKMTA0+ZoH5RuMD7sQxUt+wXnnBMT3xKA3VCppAj50bbcY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126801; c=relaxed/simple; bh=yGKlblsUzxnMA0YCWYFB2DujgbVF+67M4pYSvqkOPCU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KzJhJhhdA5Gb2jgnNzOlazHlKUjfNvTwJ8BrnC8ketXiCBMtZyP/a0y3Is8Y9bMmV23dvxquN7bz+71fQ57xx27DOL+B7gYFeHHUWPeLRL5TBw3P4Z9c1rFkjQvgl2iLNXvsw8mJAgJ3/5h/WfGdTdlKuGde59qJa4f2m+v6Joc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CK3ykhyp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="CK3ykhyp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D330AC4CED0; Fri, 13 Dec 2024 21:53:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126801; bh=yGKlblsUzxnMA0YCWYFB2DujgbVF+67M4pYSvqkOPCU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CK3ykhyp/Jp3ORy0ACMRS8jA23yGFl2KYbBgQQu/8F5MazUNFhMQm200EQUyH+G9d uPIc2wL6UrU9abFd4v0sbGQYhQiy4u0iiUePXYgF4yr2Tb5rIZCdytcj31l0yhgjrz 7Gk5+LUUBlj8sa6LoWDxHUR/2eV+8jp73thX8tlPfaTcMyaEyTh6HEw9sWezw5NNXa 6TQsHIIvVaALG835aoLSEINpEWnKvbgWwaMsO5J+7kqAUEo387KfSxC7ebQ2q3GcAX 2kKStFtgtvuPQeUCT8XacA35Iu9+tW1VPhpAI1HRky4XCFmsRdjtgo3Auq13PXdIam TlQULl+TbYLmw== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 4/9] mm/damon/sysfs: use damon_call() for update_schemes_stats Date: Fri, 13 Dec 2024 13:53:01 -0800 Message-Id: <20241213215306.54778-5-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DAMON sysfs interface uses damon_callback with its own synchronization facility to handle update_schemes_stats kdamond command. But damon_call() can support the use case without the additional synchronizations. Convert the code to use damon_call() instead. Signed-off-by: SeongJae Park --- mm/damon/sysfs.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 8cb940d21fbe..d30d659c794e 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1214,19 +1214,19 @@ static void damon_sysfs_before_terminate(struct dam= on_ctx *ctx) =20 /* * damon_sysfs_upd_schemes_stats() - Update schemes stats sysfs files. - * @kdamond: The kobject wrapper that associated to the kdamond thread. + * @data: The kobject wrapper that associated to the kdamond thread. * * This function reads the schemes stats of specific kdamond and update the * related values for sysfs files. This function should be called from DA= MON - * callbacks while holding ``damon_syfs_lock``, to safely access the DAMON - * contexts-internal data and DAMON sysfs variables. + * worker thread,to safely access the DAMON contexts-internal data. Caller + * should also ensure holding ``damon_syfs_lock``, and ->damon_ctx of @dat= a is + * not NULL but a valid pointer, to safely access DAMON sysfs variables. */ -static int damon_sysfs_upd_schemes_stats(struct damon_sysfs_kdamond *kdamo= nd) +static int damon_sysfs_upd_schemes_stats(void *data) { + struct damon_sysfs_kdamond *kdamond =3D data; struct damon_ctx *ctx =3D kdamond->damon_ctx; =20 - if (!ctx) - return -EINVAL; damon_sysfs_schemes_update_stats( kdamond->contexts->contexts_arr[0]->schemes, ctx); return 0; @@ -1371,9 +1371,6 @@ static int damon_sysfs_cmd_request_callback(struct da= mon_ctx *c, bool active, if (!kdamond || kdamond->damon_ctx !=3D c) goto out; switch (damon_sysfs_cmd_request.cmd) { - case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: - err =3D damon_sysfs_upd_schemes_stats(kdamond); - break; case DAMON_SYSFS_CMD_COMMIT: if (!after_aggregation) goto out; @@ -1511,6 +1508,18 @@ static int damon_sysfs_turn_damon_off(struct damon_s= ysfs_kdamond *kdamond) */ } =20 +static int damon_sysfs_damon_call(int (*fn)(void *data), + struct damon_sysfs_kdamond *kdamond) +{ + struct damon_call_control call_control =3D {}; + + if (!kdamond->damon_ctx) + return -EINVAL; + call_control.fn =3D fn; + call_control.data =3D kdamond; + return damon_call(kdamond->damon_ctx, &call_control); +} + /* * damon_sysfs_handle_cmd() - Handle a command for a specific kdamond. * @cmd: The command to handle. @@ -1529,12 +1538,14 @@ static int damon_sysfs_handle_cmd(enum damon_sysfs_= cmd cmd, { bool need_wait =3D true; =20 - /* Handle commands that doesn't access DAMON context-internal data */ switch (cmd) { case DAMON_SYSFS_CMD_ON: return damon_sysfs_turn_damon_on(kdamond); case DAMON_SYSFS_CMD_OFF: return damon_sysfs_turn_damon_off(kdamond); + case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: + return damon_sysfs_damon_call( + damon_sysfs_upd_schemes_stats, kdamond); case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS: return damon_sysfs_schemes_clear_regions( kdamond->contexts->contexts_arr[0]->schemes); --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3454C1A8F75; Fri, 13 Dec 2024 21:53:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126802; cv=none; b=j3DaDxFILgBfM/8EPnAJAiPuzqrIMe7VLOT2yfC8G2Om2453z55lu/oTeKR+gwDpdAt+0sQByb7iSz8AHwagTE4JsMSthgisBrcvwOY1fmjg1cO9054XO+qp2F9QkkU+lTogbXYecVg8yR0PQRI2i/c+c1CtX8DanZDRkagjV+w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126802; c=relaxed/simple; bh=FCh8a972zYqAWkurp7V4i9/Q5lpzQYSovyP9XVSlO7I=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EbGDQcIe2HoC4O5k1UpVKwwDYLTVOTzhDS2+Ied5FZLM1edxfwi1tDn9jzrC9MWn5A2rw1agXssXExZWPPBHnnzXquYF0qiYeRLS3pKN8tHlywT7kMsqFq5b+ualqqXfX6MtCgOZLy+MwNeBM2gGnS6RbRv5qpBMrNWj1PKsFa0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mESrzqYb; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mESrzqYb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E025BC4CED4; Fri, 13 Dec 2024 21:53:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126802; bh=FCh8a972zYqAWkurp7V4i9/Q5lpzQYSovyP9XVSlO7I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mESrzqYbfUdBHy6ECESgpIB7ns1eso/niqE8lgDzC14iji3R2caZzJIdB121YcKSA 8usUtKuIPKkO/x3wdfnm3dAQkxrswObf5qC8QijOuBtxK/T3uxAAQQ/8qreOYKuh6Z 1NvECbd4WtD5wK8m4kksLRl8GuDTjNzZhMp/TYB2CNMq60Wi7Fg2oAWh18BO6pNiDO IV2UBs9Koal3A7QNNrWVbA3XdCYhxss0yiG5F0OyP8krUg1xLYWB7M92oTuKlJLrkM rer32ycbaMVQDw4eoOUAVBerPN2QKp5fCqaghHGGtPKKgFHAN89WUp2h7CICRGBCUC nCWH2nqhD4d5A== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 5/9] mm/damon/sysfs: use damon_call() for commit_schemes_quota_goals Date: Fri, 13 Dec 2024 13:53:02 -0800 Message-Id: <20241213215306.54778-6-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DAMON sysfs interface uses damon_callback with its own synchronization facility to handle commit_schemes_quota_goals command. But damon_call() can support the use case without the additional synchronizations. Convert the code to use damon_call() instead. Signed-off-by: SeongJae Park --- mm/damon/sysfs.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index d30d659c794e..24070f36fa7c 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1307,9 +1307,9 @@ static int damon_sysfs_commit_input(struct damon_sysf= s_kdamond *kdamond) return err; } =20 -static int damon_sysfs_commit_schemes_quota_goals( - struct damon_sysfs_kdamond *sysfs_kdamond) +static int damon_sysfs_commit_schemes_quota_goals(void *data) { + struct damon_sysfs_kdamond *sysfs_kdamond =3D data; struct damon_ctx *ctx; struct damon_sysfs_context *sysfs_ctx; =20 @@ -1376,9 +1376,6 @@ static int damon_sysfs_cmd_request_callback(struct da= mon_ctx *c, bool active, goto out; err =3D damon_sysfs_commit_input(kdamond); break; - case DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS: - err =3D damon_sysfs_commit_schemes_quota_goals(kdamond); - break; case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES: total_bytes_only =3D true; fallthrough; @@ -1543,6 +1540,10 @@ static int damon_sysfs_handle_cmd(enum damon_sysfs_c= md cmd, return damon_sysfs_turn_damon_on(kdamond); case DAMON_SYSFS_CMD_OFF: return damon_sysfs_turn_damon_off(kdamond); + case DAMON_SYSFS_CMD_COMMIT_SCHEMES_QUOTA_GOALS: + return damon_sysfs_damon_call( + damon_sysfs_commit_schemes_quota_goals, + kdamond); case DAMON_SYSFS_CMD_UPDATE_SCHEMES_STATS: return damon_sysfs_damon_call( damon_sysfs_upd_schemes_stats, kdamond); --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 942F11B140D; Fri, 13 Dec 2024 21:53:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126803; cv=none; b=ro4QPdNzRdR078GRunCuCzUBAgcenH+syPPB+6TZjkUIRKT8by2F2oEVcsmxD10o3DRAnogpW2AZznQxbcMWb4lBDg4Hi5VpTjYqwejdLHw0KlVbZmZnG6KeHi/DQ1VnjiDHeZz4czHPjX7F4jrfOfxbSqmQKIU4CJVUVFI0MuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126803; c=relaxed/simple; bh=rp/tcYNceH/iVxFLl5+nJOs7qKY1obY5L2t2IlLP5LI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=crSWSzC37YyBFMekzL6oS7NzAealNFEHpOA0b0tSRMEJZw/RD5V4e4IhwMmJVUyG+OjSCjqjBCM9NBa/Vf5XHAx0m7rpODYaMcCcviisxh9wUeGDBpVQ1/wA36P+Rm9+/ZE5B4UxXI0t2+9uP8bnXO8kJSwEuNPvwsC+nJPvgSo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d2j387Kv; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="d2j387Kv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01F62C4CED4; Fri, 13 Dec 2024 21:53:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126803; bh=rp/tcYNceH/iVxFLl5+nJOs7qKY1obY5L2t2IlLP5LI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=d2j387KvOWKzzdbwcgD4HvlbKrHfywY4HSrfo8zIbYOUUR5p24WyVaSb+IKwsr7Nq YgVhXSg1ecg2rKGrjlSTmlyXSGSVqSfUIx8KTUW4mS4gKV0Wccm3ZHwpcGEkcKg0XM 9LkPkiDyWf+XOAhZCX1a+TkX1n9fMr2Bl+V9tvvCUASUbUknjmeASY9uRp0IhMoOh6 bU+F5FTaEaiiU/m+U5sNFzdWlJpWT+EeRB0lBNgs8HCBWT1aRNiNVzKy1Y2ac1fwyp 1jrVs5j8fu8KID5KLBbiR6olcszj7m4UCkoLV5A/0+Rqx/jLYblQ469k8dhVSo54p4 UId4w5K1SGvMA== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 6/9] mm/damon/sysfs: use damon_call() for update_schemes_effective_quotas Date: Fri, 13 Dec 2024 13:53:03 -0800 Message-Id: <20241213215306.54778-7-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DAMON sysfs interface uses damon_callback with its own synchronization facility to handle update_schemes_effective_quotas command. But damon_call() can support the use case without the additional synchronizations. Convert the code to use damon_call() instead. Signed-off-by: SeongJae Park --- mm/damon/sysfs.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 24070f36fa7c..978de4305f2b 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1327,20 +1327,18 @@ static int damon_sysfs_commit_schemes_quota_goals(v= oid *data) /* * damon_sysfs_upd_schemes_effective_quotas() - Update schemes effective q= uotas * sysfs files. - * @kdamond: The kobject wrapper that associated to the kdamond thread. + * @data: The kobject wrapper that associated to the kdamond thread. * * This function reads the schemes' effective quotas of specific kdamond a= nd * update the related values for sysfs files. This function should be cal= led * from DAMON callbacks while holding ``damon_syfs_lock``, to safely acces= s the * DAMON contexts-internal data and DAMON sysfs variables. */ -static int damon_sysfs_upd_schemes_effective_quotas( - struct damon_sysfs_kdamond *kdamond) +static int damon_sysfs_upd_schemes_effective_quotas(void *data) { + struct damon_sysfs_kdamond *kdamond =3D data; struct damon_ctx *ctx =3D kdamond->damon_ctx; =20 - if (!ctx) - return -EINVAL; damos_sysfs_update_effective_quotas( kdamond->contexts->contexts_arr[0]->schemes, ctx); return 0; @@ -1400,9 +1398,6 @@ static int damon_sysfs_cmd_request_callback(struct da= mon_ctx *c, bool active, damon_sysfs_schemes_regions_updating =3D false; } break; - case DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECTIVE_QUOTAS: - err =3D damon_sysfs_upd_schemes_effective_quotas(kdamond); - break; default: break; } @@ -1550,6 +1545,10 @@ static int damon_sysfs_handle_cmd(enum damon_sysfs_c= md cmd, case DAMON_SYSFS_CMD_CLEAR_SCHEMES_TRIED_REGIONS: return damon_sysfs_schemes_clear_regions( kdamond->contexts->contexts_arr[0]->schemes); + case DAMON_SYSFS_CMD_UPDATE_SCHEMES_EFFECTIVE_QUOTAS: + return damon_sysfs_damon_call( + damon_sysfs_upd_schemes_effective_quotas, + kdamond); default: break; } --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4F921E883E; Fri, 13 Dec 2024 21:53:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126804; cv=none; b=FPRxabJg34/L0GffY52Q5h2NlcQT152ICdGsrxP5PKE/zhjtwFFnFoQXnQ74DOj+9vaoBgX2kuWi05kYoOHqKGn24Nkj8LECuQEQZm2rVMayCLG6n+wdViLqzaKHhiwKf6z0U3/jC94e/8fGdlwf7RMbsEJvZv3FWuAo7hQWkJE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126804; c=relaxed/simple; bh=iQ/qZJ1QHQV0+h/4WTEQK+3ncwWBGkQjrNEPl3UnO74=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=r8rTDB/jX68ynPheNYOeDbKiZ8EXcCnp8PAlx6T8Na8nCPdadaLTeYBX1R2253fGw9vwcTQw79L4X0jElsT6gzFYBgT9PN0lJ6tFq5kE3/6Uk8UgFvxxitHM9UvWVpLqlMuioLxwHTzEF2XICb6OrcrMSLSxJgJCAoysSsHj4lU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A1ciks1M; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="A1ciks1M" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2357EC4CEDD; Fri, 13 Dec 2024 21:53:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126804; bh=iQ/qZJ1QHQV0+h/4WTEQK+3ncwWBGkQjrNEPl3UnO74=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A1ciks1MAAQLTtKHJqC1uldwlJ6lT8vW43m5x64l0pW73cebSFC34x+nIOL9ptgDV GdMY7EW9phV13W3Fxdr5MWnfiVyZPE6gEsV2K1Qf/p9YDRXhm0eZIxsa5qGfconTMF OAT2o2klemlx3QxZHU7qN37fNj2yOm5rkUpDwx9BDxn00TItH1BF5ayvM8ZcuDc7xm O1va3eT27f0HyOwXNAnm1Lu7F+jPh2uwZOQ9gusC8vGkGleZnLjrBr9T9vdGR6eP4h 6HxcrmKjgaf+rLpT4P9PD/7Of99n+M8ogmTeSi6PpwuwjMIBVO8yFLaxM+rSfgB7jN 1Rd/TxZq3PUDA== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 7/9] mm/damon/core: implement damos_walk() Date: Fri, 13 Dec 2024 13:53:04 -0800 Message-Id: <20241213215306.54778-8-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a new core layer interface, damos_walk(). It aims to replace some damon_callback usages that access DAMOS schemes applied regions of ongoing kdamond with additional synchronizations. It receives a function pointer and asks kdamond to invoke it for any region that it will apply any DAMOS action within one scheme apply interval for every scheme of it. The function further waits until the kdamond finishes the invocations for every scheme, or cancels the request, and returns. The kdamond invokes the function as requested within the main loop. If it is deactivated by DAMOS watermarks or going out of the main loop, it marks the request as canceled, so that damos_walk() can wakeup and return. Signed-off-by: SeongJae Park --- include/linux/damon.h | 33 ++++++++++- mm/damon/core.c | 134 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+), 2 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 529ea578f2d5..acedaab4dccf 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -368,6 +368,31 @@ struct damos_filter { struct list_head list; }; =20 +struct damon_ctx; +struct damos; + +/** + * struct damos_walk_control - Control damos_walk(). + * + * @walk_fn: Function to be called back for each region. + * @data: Data that will be passed to walk functions. + * + * Control damos_walk(), which requests specific kdamond to invoke the giv= en + * function to each region that eligible to apply actions of the kdamond's + * schemes. Refer to damos_walk() for more details. + */ +struct damos_walk_control { + void (*walk_fn)(void *data, struct damon_ctx *ctx, + struct damon_target *t, struct damon_region *r, + struct damos *s); + void *data; +/* private: internal use only */ + /* informs if the kdamond finished handling of the walk request */ + struct completion completion; + /* informs if the walk is canceled. */ + bool canceled; +}; + /** * struct damos_access_pattern - Target access pattern of the given scheme. * @min_sz_region: Minimum size of target regions. @@ -453,6 +478,8 @@ struct damos { * @action */ unsigned long next_apply_sis; + /* informs if ongoing DAMOS walk for this scheme is finished */ + bool walk_completed; /* public: */ struct damos_quota quota; struct damos_watermarks wmarks; @@ -480,8 +507,6 @@ enum damon_ops_id { NR_DAMON_OPS, }; =20 -struct damon_ctx; - /** * struct damon_operations - Monitoring operations for given use cases. * @@ -694,6 +719,9 @@ struct damon_ctx { struct damon_call_control *call_control; struct mutex call_control_lock; =20 + struct damos_walk_control *walk_control; + struct mutex walk_control_lock; + /* public: */ struct task_struct *kdamond; struct mutex kdamond_lock; @@ -842,6 +870,7 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs, b= ool exclusive); int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); =20 int damon_call(struct damon_ctx *ctx, struct damon_call_control *control); +int damos_walk(struct damon_ctx *ctx, struct damos_walk_control *control); =20 int damon_set_region_biggest_system_ram_default(struct damon_target *t, unsigned long *start, unsigned long *end); diff --git a/mm/damon/core.c b/mm/damon/core.c index 89a679c06e30..de923b3a1084 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -534,6 +534,7 @@ struct damon_ctx *damon_new_ctx(void) =20 mutex_init(&ctx->kdamond_lock); mutex_init(&ctx->call_control_lock); + mutex_init(&ctx->walk_control_lock); =20 ctx->attrs.min_nr_regions =3D 10; ctx->attrs.max_nr_regions =3D 1000; @@ -1232,6 +1233,46 @@ int damon_call(struct damon_ctx *ctx, struct damon_c= all_control *control) return 0; } =20 +/** + * damos_walk() - Invoke a given functions while DAMOS walk regions. + * @ctx: DAMON context to call the functions for. + * @control: Control variable of the walk request. + * + * Ask DAMON worker thread (kdamond) of @ctx to call a function for each r= egion + * that the kdamond will apply DAMOS action to, and wait until the kdamond + * finishes handling of the request. + * + * The kdamond executes the given function in the main loop, for each regi= on + * just before it applies any DAMOS actions of @ctx to it. The invocation= is + * made only within one &damos->apply_interval_us since damos_walk() + * invocation, for each scheme. The given callback function can hence saf= ely + * access the internal data of &struct damon_ctx and &struct damon_region = that + * each of the scheme will apply the action for next interval, without + * additional synchronizations against the kdamond. If every scheme of @c= tx + * passed at least one &damos->apply_interval_us, kdamond marks the reques= t as + * completed so that damos_walk() can wakeup and return. + * + * Return: 0 on success, negative error code otherwise. + */ +int damos_walk(struct damon_ctx *ctx, struct damos_walk_control *control) +{ + init_completion(&control->completion); + control->canceled =3D false; + mutex_lock(&ctx->walk_control_lock); + if (ctx->walk_control) { + mutex_unlock(&ctx->walk_control_lock); + return -EBUSY; + } + ctx->walk_control =3D control; + mutex_unlock(&ctx->walk_control_lock); + if (!damon_is_running(ctx)) + return -EINVAL; + wait_for_completion(&control->completion); + if (control->canceled) + return -ECANCELED; + return 0; +} + /* * Reset the aggregated monitoring results ('nr_accesses' of each region). */ @@ -1411,6 +1452,93 @@ static bool damos_filter_out(struct damon_ctx *ctx, = struct damon_target *t, return false; } =20 +/* + * damos_walk_call_walk() - Call &damos_walk_control->walk_fn. + * @ctx: The context of &damon_ctx->walk_control. + * @t: The monitoring target of @r that @s will be applied. + * @r: The region of @t that @s will be applied. + * @s: The scheme of @ctx that will be applied to @r. + * + * This function is called from kdamond whenever it found a region that + * eligible to apply a DAMOS scheme's action. If a DAMOS walk request is + * installed by damos_walk() and its &damos_walk_control->walk_fn has not + * invoked for the region for the last &damos->apply_interval_us interval, + * invoke it. + */ +static void damos_walk_call_walk(struct damon_ctx *ctx, struct damon_targe= t *t, + struct damon_region *r, struct damos *s) +{ + struct damos_walk_control *control; + + mutex_lock(&ctx->walk_control_lock); + control =3D ctx->walk_control; + mutex_unlock(&ctx->walk_control_lock); + if (!control) + return; + control->walk_fn(control->data, ctx, t, r, s); +} + +/* + * damos_walk_complete() - Complete DAMOS walk request if all walks are do= ne. + * @ctx: The context of &damon_ctx->walk_control. + * @s: A scheme of @ctx that all walks are now done. + * + * This function is called when kdamond finished applying the action of a = DAMOS + * scheme to regions that eligible for the given &damos->apply_interval_us= . If + * every scheme of @ctx including @s now finished walking for at least one + * &damos->apply_interval_us, this function makrs the handling of the given + * DAMOS walk request is done, so that damos_walk() can wake up and return. + */ +static void damos_walk_complete(struct damon_ctx *ctx, struct damos *s) +{ + struct damos *siter; + struct damos_walk_control *control; + + mutex_lock(&ctx->walk_control_lock); + control =3D ctx->walk_control; + mutex_unlock(&ctx->walk_control_lock); + if (!control) + return; + + s->walk_completed =3D true; + /* if all schemes completed, signal completion to walker */ + damon_for_each_scheme(siter, ctx) { + if (!siter->walk_completed) + return; + } + complete(&control->completion); + mutex_lock(&ctx->walk_control_lock); + ctx->walk_control =3D NULL; + mutex_unlock(&ctx->walk_control_lock); +} + +/* + * damos_walk_cancel() - Cancel the current DAMOS walk request. + * @ctx: The context of &damon_ctx->walk_control. + * + * This function is called when @ctx is deactivated by DAMOS watermarks, D= AMOS + * walk is requested but there is no DAMOS scheme to walk for, or the kdam= ond + * is already out of the main loop and therefore gonna be terminated, and = hence + * cannot continue the walks. This function therefore marks the walk requ= est + * as canceled, so that damos_walk() can wake up and return. + */ +static void damos_walk_cancel(struct damon_ctx *ctx) +{ + struct damos_walk_control *control; + + mutex_lock(&ctx->walk_control_lock); + control =3D ctx->walk_control; + mutex_unlock(&ctx->walk_control_lock); + + if (!control) + return; + control->canceled =3D true; + complete(&control->completion); + mutex_lock(&ctx->walk_control_lock); + ctx->walk_control =3D NULL; + mutex_unlock(&ctx->walk_control_lock); +} + static void damos_apply_scheme(struct damon_ctx *c, struct damon_target *t, struct damon_region *r, struct damos *s) { @@ -1467,6 +1595,7 @@ static void damos_apply_scheme(struct damon_ctx *c, s= truct damon_target *t, if (damos_filter_out(c, t, r, s)) return; ktime_get_coarse_ts64(&begin); + damos_walk_call_walk(c, t, r, s); if (c->callback.before_damos_apply) err =3D c->callback.before_damos_apply(c, t, r, s); if (!err) { @@ -1745,6 +1874,7 @@ static void kdamond_apply_schemes(struct damon_ctx *c) damon_for_each_scheme(s, c) { if (c->passed_sample_intervals < s->next_apply_sis) continue; + damos_walk_complete(c, s); s->next_apply_sis =3D c->passed_sample_intervals + (s->apply_interval_us ? s->apply_interval_us : c->attrs.aggr_interval) / sample_interval; @@ -2077,6 +2207,7 @@ static int kdamond_wait_activation(struct damon_ctx *= ctx) ctx->callback.after_wmarks_check(ctx)) break; kdamond_call(ctx, true); + damos_walk_cancel(ctx); } return -EBUSY; } @@ -2171,6 +2302,8 @@ static int kdamond_fn(void *data) */ if (!list_empty(&ctx->schemes)) kdamond_apply_schemes(ctx); + else + damos_walk_cancel(ctx); =20 sample_interval =3D ctx->attrs.sample_interval ? ctx->attrs.sample_interval : 1; @@ -2211,6 +2344,7 @@ static int kdamond_fn(void *data) mutex_unlock(&ctx->kdamond_lock); =20 kdamond_call(ctx, true); + damos_walk_cancel(ctx); =20 mutex_lock(&damon_lock); nr_running_ctxs--; --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D9C0F1EE034; Fri, 13 Dec 2024 21:53:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126807; cv=none; b=Q6UtcSp3MhM0vHbnNA7t6Q6DXhSlYjKWeUG2w6k0HUIGJWJtJxINaXdyotrZDAOYlwLeojWrLA1h+VdayaCLoE6mltrv5DuuPz5mHhO1QLkcrjyg0+rwB9wc3W8XK1cPycbRMGjPB8cB0XdRToe7T9BRVg03amcsnCZY+eRpcA8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126807; c=relaxed/simple; bh=7p5Zd1Ss19X+LmH72iki7m+ul6ZR9lC5pFElBjMKsP4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=F16/2CE/hfVit1BIOF6dYVOEGnDQFyexoJkWTrlRIn2y/hC4x/601+ZlGfmAefk/Vyz/FhXlsxm/gFrRurxbVEkj0ody8l2RzGpb7f+Jt1ZoqimAViKnEWXqxNF9+FHhM9c64pny02lo8tRRcD4F/iP0LeqPhWw0vkrq72wNbH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=m35yPDSV; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="m35yPDSV" 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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); =20 int damon_sysfs_schemes_update_regions_stop(struct damon_ctx *ctx); =20 +void damos_sysfs_populate_region_dir(struct damon_sysfs_schemes *sysfs_sch= emes, + 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); =20 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 dam= on_ctx *ctx, return 0; } =20 +/** + * 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_sch= emes, + 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 =3D 0; + + damon_for_each_scheme(scheme, ctx) { + if (scheme =3D=3D s) + break; + schemes_idx++; + } + + /* user could have removed the scheme sysfs dir */ + if (schemes_idx >=3D sysfs_schemes->nr) + return; + + sysfs_regions =3D sysfs_schemes->schemes_arr[schemes_idx]->tried_regions; + sysfs_regions->total_bytes +=3D r->ar.end - r->ar.start; + if (total_bytes_only) + return; + + region =3D 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 *da= ta), return damon_call(kdamond->damon_ctx, &call_control); } =20 +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 d= amon_ctx *ctx, + struct damon_target *t, struct damon_region *r, + struct damos *s) +{ + struct damon_sysfs_schemes_walk_data *walk_data =3D data; + struct damon_sysfs_kdamond *sysfs_kdamond =3D 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 =3D { + .sysfs_kdamond =3D sysfs_kdamond, + .total_bytes_only =3D total_bytes_only, + }; + struct damos_walk_control control =3D { + .walk_fn =3D damon_sysfs_schemes_tried_regions_upd_one, + .data =3D &walk_data, + }; + struct damon_ctx *ctx =3D 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_c= md 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); --=20 2.39.5 From nobody Wed Dec 17 15:54:19 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F2B5194C8B; Fri, 13 Dec 2024 21:53:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126808; cv=none; b=ghceyO2TAp9gFrXo4YLl2zU4oumzVZOcqsCCpfymGgmbgB0gbGwQ8ttg4P3YjfC9RJyxo5Tj7VfAF/HjF5jAhtphWmGT0yisQff+JSa8rQatvKzWaoC7YtLFyr5nPNZ5lEofcs6kUEiD+98gqPRmbe3uI/XZlQAL5LkpS/B1it8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734126808; c=relaxed/simple; bh=ZVUan1fZkxzqrz+Kv9xlvlqW12Ur6Dh+XlW22jd7kFw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HRhVho+zhtUIAJg2/ydD7UdYGmAZ1igxnVko6UJAxLOrCVoin8hvFbJ60kkaRuU+MY/54FNJoe52ofBDIv6f6NwyxGmWvuYzoMt+tBFziwP5paGa5apPd5R0nqJRk9NkbSP3TFlpXeXYvJteJ1j4hrSerNO9CasGsLisWZQRosc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=S0E3zVoC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="S0E3zVoC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52C85C4CEDF; Fri, 13 Dec 2024 21:53:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734126806; bh=ZVUan1fZkxzqrz+Kv9xlvlqW12Ur6Dh+XlW22jd7kFw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S0E3zVoCoJpubrVS278f6wuW045DOZ9ZeBH8yXyzO9kDubGZPmBP6QlG/QHK3Jkpy YlLqC9V72MTkRYgamfMWU7FrrcbWMuTjrHqilyTfswU8DpTX/Lg+6YTPVkz7g3jQKD 0ZWE+qaBQqKPrz8nIhAOY+pMjaW2dDXO4fwdCqfJ2X61tybGzg0pPqCATM1qGj1WjO XUk/u7KVqMGMY1kIeWl9GYh8FB8vTN7UNqEPR99Hw1n70NEBHe8YlqH+aK3UA8zl54 ihQknlRi//zkZIOSk0aO7dehD8ZtbczYdfhpvqSiyQsMOBvb4sJtFvKBqr7WnibqG1 BI4AK7SW7LD6Q== From: SeongJae Park To: Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 9/9] mm/damon/sysfs: remove unused code for schemes tried regions update Date: Fri, 13 Dec 2024 13:53:06 -0800 Message-Id: <20241213215306.54778-10-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" DAMON sysfs interface was using damon_callback with its own complicated synchronization logics to update DAMOS scheme applied regions directories and files. But it is replaced to use damos_walk(), and the additional synchronization logics are no more being used. Remove those. Signed-off-by: SeongJae Park --- mm/damon/sysfs-common.h | 10 -- mm/damon/sysfs-schemes.c | 204 --------------------------------------- mm/damon/sysfs.c | 70 +------------- 3 files changed, 2 insertions(+), 282 deletions(-) diff --git a/mm/damon/sysfs-common.h b/mm/damon/sysfs-common.h index 81f1c845118f..b3f63bc658b7 100644 --- a/mm/damon/sysfs-common.h +++ b/mm/damon/sysfs-common.h @@ -45,16 +45,6 @@ void damon_sysfs_schemes_update_stats( struct damon_sysfs_schemes *sysfs_schemes, struct damon_ctx *ctx); =20 -int damon_sysfs_schemes_update_regions_start( - struct damon_sysfs_schemes *sysfs_schemes, - struct damon_ctx *ctx, bool total_bytes_only); - -void damos_sysfs_mark_finished_regions_updates(struct damon_ctx *ctx); - -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_sch= emes, struct damon_ctx *ctx, struct damon_target *t, struct damon_region *r, struct damos *s, diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index 404c21f45640..5000d81d0d83 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -114,55 +114,11 @@ static const struct kobj_type damon_sysfs_scheme_regi= on_ktype =3D { * scheme regions directory */ =20 -/* - * enum damos_sysfs_regions_upd_status - Represent DAMOS tried regions upd= ate - * status - * @DAMOS_TRIED_REGIONS_UPD_IDLE: Waiting for next request. - * @DAMOS_TRIED_REGIONS_UPD_STARTED: Update started. - * @DAMOS_TRIED_REGIONS_UPD_FINISHED: Update finished. - * - * Each DAMON-based operation scheme (&struct damos) has its own apply - * interval, and we need to expose the scheme tried regions based on only - * single snapshot. For this, we keep the tried regions update status for= each - * scheme. The status becomes 'idle' at the beginning. - * - * Once the tried regions update request is received, the request handling - * start function (damon_sysfs_scheme_update_regions_start()) sets the sta= tus - * of all schemes as 'idle' again, and register ->before_damos_apply() - * callback. - * - * Then, the first followup ->before_damos_apply() callback - * (damon_sysfs_before_damos_apply()) sets the status 'started'. The first - * ->after_sampling() or ->after_aggregation() callback - * (damon_sysfs_cmd_request_callback()) after the call is called only aft= er - * the scheme is completely applied to the given snapshot. Hence the cal= lback - * knows the situation by showing 'started' status, and sets the status as - * 'finished'. Then, damon_sysfs_before_damos_apply() understands the - * situation by showing the 'finished' status and do nothing. - * - * If DAMOS is not applied to any region due to any reasons including the - * access pattern, the watermarks, the quotas, and the filters, - * ->before_damos_apply() will not be called back. Until the situation is - * changed, the update will not be finished. To avoid this, - * damon_sysfs_after_sampling() set the status as 'finished' if more than = two - * apply intervals of the scheme is passed while the state is 'idle'. - * - * Finally, the tried regions request handling finisher function - * (damon_sysfs_schemes_update_regions_stop()) unregisters the callbacks. - */ -enum damos_sysfs_regions_upd_status { - DAMOS_TRIED_REGIONS_UPD_IDLE, - DAMOS_TRIED_REGIONS_UPD_STARTED, - DAMOS_TRIED_REGIONS_UPD_FINISHED, -}; - struct damon_sysfs_scheme_regions { struct kobject kobj; struct list_head regions_list; int nr_regions; unsigned long total_bytes; - enum damos_sysfs_regions_upd_status upd_status; - unsigned long upd_timeout_jiffies; }; =20 static struct damon_sysfs_scheme_regions * @@ -178,7 +134,6 @@ damon_sysfs_scheme_regions_alloc(void) INIT_LIST_HEAD(®ions->regions_list); regions->nr_regions =3D 0; regions->total_bytes =3D 0; - regions->upd_status =3D DAMOS_TRIED_REGIONS_UPD_IDLE; return regions; } =20 @@ -2131,63 +2086,6 @@ void damon_sysfs_schemes_update_stats( } } =20 -/* - * damon_sysfs_schemes that need to update its schemes regions dir. Prote= cted - * by damon_sysfs_lock - */ -static struct damon_sysfs_schemes *damon_sysfs_schemes_for_damos_callback; -static int damon_sysfs_schemes_region_idx; -static bool damos_regions_upd_total_bytes_only; - -/* - * DAMON callback that called before damos apply. While this callback is - * registered, damon_sysfs_lock should be held to ensure the regions - * directories exist. - */ -static int damon_sysfs_before_damos_apply(struct damon_ctx *ctx, - struct damon_target *t, struct damon_region *r, - struct damos *s) -{ - struct damos *scheme; - struct damon_sysfs_scheme_regions *sysfs_regions; - struct damon_sysfs_scheme_region *region; - struct damon_sysfs_schemes *sysfs_schemes =3D - damon_sysfs_schemes_for_damos_callback; - int schemes_idx =3D 0; - - damon_for_each_scheme(scheme, ctx) { - if (scheme =3D=3D s) - break; - schemes_idx++; - } - - /* user could have removed the scheme sysfs dir */ - if (schemes_idx >=3D sysfs_schemes->nr) - return 0; - - sysfs_regions =3D sysfs_schemes->schemes_arr[schemes_idx]->tried_regions; - if (sysfs_regions->upd_status =3D=3D DAMOS_TRIED_REGIONS_UPD_FINISHED) - return 0; - if (sysfs_regions->upd_status =3D=3D DAMOS_TRIED_REGIONS_UPD_IDLE) - sysfs_regions->upd_status =3D DAMOS_TRIED_REGIONS_UPD_STARTED; - sysfs_regions->total_bytes +=3D r->ar.end - r->ar.start; - if (damos_regions_upd_total_bytes_only) - return 0; - - region =3D damon_sysfs_scheme_region_alloc(r); - if (!region) - return 0; - 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", - damon_sysfs_schemes_region_idx++)) { - kobject_put(®ion->kobj); - } - return 0; -} - /** * damos_sysfs_populate_region_dir() - Populate a schemes tried region dir. * @sysfs_schemes: Schemes directory to populate regions directory. @@ -2236,29 +2134,6 @@ void damos_sysfs_populate_region_dir(struct damon_sy= sfs_schemes *sysfs_schemes, } } =20 -/* - * DAMON callback that called after each accesses sampling. While this - * callback is registered, damon_sysfs_lock should be held to ensure the - * regions directories exist. - */ -void damos_sysfs_mark_finished_regions_updates(struct damon_ctx *ctx) -{ - struct damon_sysfs_schemes *sysfs_schemes =3D - damon_sysfs_schemes_for_damos_callback; - struct damon_sysfs_scheme_regions *sysfs_regions; - int i; - - for (i =3D 0; i < sysfs_schemes->nr; i++) { - sysfs_regions =3D sysfs_schemes->schemes_arr[i]->tried_regions; - if (sysfs_regions->upd_status =3D=3D - DAMOS_TRIED_REGIONS_UPD_STARTED || - time_after(jiffies, - sysfs_regions->upd_timeout_jiffies)) - sysfs_regions->upd_status =3D - DAMOS_TRIED_REGIONS_UPD_FINISHED; - } -} - /* Called from damon_sysfs_cmd_request_callback under damon_sysfs_lock */ int damon_sysfs_schemes_clear_regions( struct damon_sysfs_schemes *sysfs_schemes) @@ -2275,82 +2150,3 @@ int damon_sysfs_schemes_clear_regions( } return 0; } - -static struct damos *damos_sysfs_nth_scheme(int n, struct damon_ctx *ctx) -{ - struct damos *scheme; - int i =3D 0; - - damon_for_each_scheme(scheme, ctx) { - if (i =3D=3D n) - return scheme; - i++; - } - return NULL; -} - -static void damos_tried_regions_init_upd_status( - struct damon_sysfs_schemes *sysfs_schemes, - struct damon_ctx *ctx) -{ - int i; - struct damos *scheme; - struct damon_sysfs_scheme_regions *sysfs_regions; - - for (i =3D 0; i < sysfs_schemes->nr; i++) { - sysfs_regions =3D sysfs_schemes->schemes_arr[i]->tried_regions; - scheme =3D damos_sysfs_nth_scheme(i, ctx); - if (!scheme) { - sysfs_regions->upd_status =3D - DAMOS_TRIED_REGIONS_UPD_FINISHED; - continue; - } - sysfs_regions->upd_status =3D DAMOS_TRIED_REGIONS_UPD_IDLE; - sysfs_regions->upd_timeout_jiffies =3D jiffies + - 2 * usecs_to_jiffies(scheme->apply_interval_us ? - scheme->apply_interval_us : - ctx->attrs.aggr_interval); - } -} - -/* Called while damon_sysfs_lock is hold */ -int damon_sysfs_schemes_update_regions_start( - struct damon_sysfs_schemes *sysfs_schemes, - struct damon_ctx *ctx, bool total_bytes_only) -{ - damon_sysfs_schemes_clear_regions(sysfs_schemes); - damon_sysfs_schemes_for_damos_callback =3D sysfs_schemes; - damos_tried_regions_init_upd_status(sysfs_schemes, ctx); - damos_regions_upd_total_bytes_only =3D total_bytes_only; - ctx->callback.before_damos_apply =3D damon_sysfs_before_damos_apply; - return 0; -} - -bool damos_sysfs_regions_upd_done(void) -{ - struct damon_sysfs_schemes *sysfs_schemes =3D - damon_sysfs_schemes_for_damos_callback; - struct damon_sysfs_scheme_regions *sysfs_regions; - int i; - - for (i =3D 0; i < sysfs_schemes->nr; i++) { - sysfs_regions =3D sysfs_schemes->schemes_arr[i]->tried_regions; - if (sysfs_regions->upd_status !=3D - DAMOS_TRIED_REGIONS_UPD_FINISHED) - return false; - } - return true; -} - -/* - * Called from damon_sysfs_cmd_request_callback under damon_sysfs_lock. C= aller - * should unlock damon_sysfs_lock which held before - * damon_sysfs_schemes_update_regions_start() - */ -int damon_sysfs_schemes_update_regions_stop(struct damon_ctx *ctx) -{ - damon_sysfs_schemes_for_damos_callback =3D NULL; - ctx->callback.before_damos_apply =3D NULL; - damon_sysfs_schemes_region_idx =3D 0; - return 0; -} diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index dafeec20ac53..77a9b8200e78 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1181,25 +1181,9 @@ static int damon_sysfs_add_targets(struct damon_ctx = *ctx, return 0; } =20 -static bool damon_sysfs_schemes_regions_updating; - static void damon_sysfs_before_terminate(struct damon_ctx *ctx) { struct damon_target *t, *next; - struct damon_sysfs_kdamond *kdamond; - enum damon_sysfs_cmd cmd; - - /* damon_sysfs_schemes_update_regions_stop() might not yet called */ - kdamond =3D damon_sysfs_cmd_request.kdamond; - cmd =3D damon_sysfs_cmd_request.cmd; - if (kdamond && ctx =3D=3D kdamond->damon_ctx && - (cmd =3D=3D DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS || - cmd =3D=3D DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES) && - damon_sysfs_schemes_regions_updating) { - damon_sysfs_schemes_update_regions_stop(ctx); - damon_sysfs_schemes_regions_updating =3D false; - mutex_unlock(&damon_sysfs_lock); - } =20 if (!damon_target_has_pid(ctx)) return; @@ -1232,28 +1216,6 @@ static int damon_sysfs_upd_schemes_stats(void *data) return 0; } =20 -static int damon_sysfs_upd_schemes_regions_start( - struct damon_sysfs_kdamond *kdamond, bool total_bytes_only) -{ - struct damon_ctx *ctx =3D kdamond->damon_ctx; - - if (!ctx) - return -EINVAL; - return damon_sysfs_schemes_update_regions_start( - kdamond->contexts->contexts_arr[0]->schemes, ctx, - total_bytes_only); -} - -static int damon_sysfs_upd_schemes_regions_stop( - struct damon_sysfs_kdamond *kdamond) -{ - struct damon_ctx *ctx =3D kdamond->damon_ctx; - - if (!ctx) - return -EINVAL; - return damon_sysfs_schemes_update_regions_stop(ctx); -} - static inline bool damon_sysfs_kdamond_running( struct damon_sysfs_kdamond *kdamond) { @@ -1358,12 +1320,10 @@ static int damon_sysfs_cmd_request_callback(struct = damon_ctx *c, bool active, bool after_aggregation) { struct damon_sysfs_kdamond *kdamond; - bool total_bytes_only =3D false; int err =3D 0; =20 /* avoid deadlock due to concurrent state_store('off') */ - if (!damon_sysfs_schemes_regions_updating && - !mutex_trylock(&damon_sysfs_lock)) + if (!mutex_trylock(&damon_sysfs_lock)) return 0; kdamond =3D damon_sysfs_cmd_request.kdamond; if (!kdamond || kdamond->damon_ctx !=3D c) @@ -1374,39 +1334,13 @@ static int damon_sysfs_cmd_request_callback(struct = damon_ctx *c, bool active, goto out; err =3D damon_sysfs_commit_input(kdamond); break; - case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_BYTES: - total_bytes_only =3D true; - fallthrough; - case DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS: - if (!damon_sysfs_schemes_regions_updating) { - err =3D damon_sysfs_upd_schemes_regions_start(kdamond, - total_bytes_only); - if (!err) { - damon_sysfs_schemes_regions_updating =3D true; - goto keep_lock_out; - } - } else { - damos_sysfs_mark_finished_regions_updates(c); - /* - * Continue regions updating if DAMON is till - * active and the update for all schemes is not - * finished. - */ - if (active && !damos_sysfs_regions_upd_done()) - goto keep_lock_out; - err =3D damon_sysfs_upd_schemes_regions_stop(kdamond); - damon_sysfs_schemes_regions_updating =3D false; - } - break; default: break; } /* Mark the request as invalid now. */ damon_sysfs_cmd_request.kdamond =3D NULL; out: - if (!damon_sysfs_schemes_regions_updating) - mutex_unlock(&damon_sysfs_lock); -keep_lock_out: + mutex_unlock(&damon_sysfs_lock); return err; } =20 --=20 2.39.5