linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RESEND 6.5.y] mm/damon/sysfs: check DAMOS regions update progress from before_terminate()
       [not found] <2023102716-prudishly-reggae-1b29@gregkh>
@ 2023-10-28 21:39 ` SeongJae Park
  2023-10-31 11:16   ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: SeongJae Park @ 2023-10-28 21:39 UTC (permalink / raw)
  To: stable; +Cc: SeongJae Park, Andrew Morton, damon, linux-mm, linux-kernel

DAMON_SYSFS can receive DAMOS tried regions update request while kdamond
is already out of the main loop and before_terminate callback
(damon_sysfs_before_terminate() in this case) is not yet called.  And
damon_sysfs_handle_cmd() can further be finished before the callback is
invoked.  Then, damon_sysfs_before_terminate() unlocks damon_sysfs_lock,
which is not locked by anyone.  This happens because the callback function
assumes damon_sysfs_cmd_request_callback() should be called before it.
Check if the assumption was true before doing the unlock, to avoid this
problem.

Link: https://lkml.kernel.org/r/20231007200432.3110-1-sj@kernel.org
Fixes: f1d13cacabe1 ("mm/damon/sysfs: implement DAMOS tried regions update command")
Signed-off-by: SeongJae Park <sj@kernel.org>
Cc: <stable@vger.kernel.org>	[6.2.x]
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
(cherry picked from commit 76b7069bcc89dec33f03eb08abee165d0306b754)
---

I sent this patch yesterday, but seems not archived in lore.kernel.org.
Sending again Cc-ing more mailing lists, to ensure it is sent and others can
find it, to avoid any duplicated works.

 mm/damon/sysfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c
index 33e1d5c9cb54..df165820c605 100644
--- a/mm/damon/sysfs.c
+++ b/mm/damon/sysfs.c
@@ -1202,6 +1202,8 @@ static int damon_sysfs_set_targets(struct damon_ctx *ctx,
 	return 0;
 }
 
+static bool damon_sysfs_schemes_regions_updating;
+
 static void damon_sysfs_before_terminate(struct damon_ctx *ctx)
 {
 	struct damon_target *t, *next;
@@ -1209,10 +1211,12 @@ static void damon_sysfs_before_terminate(struct damon_ctx *ctx)
 
 	/* damon_sysfs_schemes_update_regions_stop() might not yet called */
 	kdamond = damon_sysfs_cmd_request.kdamond;
-	if (kdamond && damon_sysfs_cmd_request.cmd ==
+	if (kdamond && (damon_sysfs_cmd_request.cmd ==
 			DAMON_SYSFS_CMD_UPDATE_SCHEMES_TRIED_REGIONS &&
+			damon_sysfs_schemes_regions_updating) &&
 			ctx == kdamond->damon_ctx) {
 		damon_sysfs_schemes_update_regions_stop(ctx);
+		damon_sysfs_schemes_regions_updating = false;
 		mutex_unlock(&damon_sysfs_lock);
 	}
 
@@ -1331,7 +1335,6 @@ static int damon_sysfs_commit_input(struct damon_sysfs_kdamond *kdamond)
 static int damon_sysfs_cmd_request_callback(struct damon_ctx *c)
 {
 	struct damon_sysfs_kdamond *kdamond;
-	static bool damon_sysfs_schemes_regions_updating;
 	int err = 0;
 
 	/* avoid deadlock due to concurrent state_store('off') */
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH RESEND 6.5.y] mm/damon/sysfs: check DAMOS regions update progress from before_terminate()
  2023-10-28 21:39 ` [PATCH RESEND 6.5.y] mm/damon/sysfs: check DAMOS regions update progress from before_terminate() SeongJae Park
@ 2023-10-31 11:16   ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2023-10-31 11:16 UTC (permalink / raw)
  To: SeongJae Park; +Cc: stable, Andrew Morton, damon, linux-mm, linux-kernel

On Sat, Oct 28, 2023 at 09:39:36PM +0000, SeongJae Park wrote:
> DAMON_SYSFS can receive DAMOS tried regions update request while kdamond
> is already out of the main loop and before_terminate callback
> (damon_sysfs_before_terminate() in this case) is not yet called.  And
> damon_sysfs_handle_cmd() can further be finished before the callback is
> invoked.  Then, damon_sysfs_before_terminate() unlocks damon_sysfs_lock,
> which is not locked by anyone.  This happens because the callback function
> assumes damon_sysfs_cmd_request_callback() should be called before it.
> Check if the assumption was true before doing the unlock, to avoid this
> problem.
> 
> Link: https://lkml.kernel.org/r/20231007200432.3110-1-sj@kernel.org
> Fixes: f1d13cacabe1 ("mm/damon/sysfs: implement DAMOS tried regions update command")
> Signed-off-by: SeongJae Park <sj@kernel.org>
> Cc: <stable@vger.kernel.org>	[6.2.x]
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
> (cherry picked from commit 76b7069bcc89dec33f03eb08abee165d0306b754)
> ---
> 
> I sent this patch yesterday, but seems not archived in lore.kernel.org.
> Sending again Cc-ing more mailing lists, to ensure it is sent and others can
> find it, to avoid any duplicated works.

Now queued up, thanks.

greg k-h


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-10-31 11:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <2023102716-prudishly-reggae-1b29@gregkh>
2023-10-28 21:39 ` [PATCH RESEND 6.5.y] mm/damon/sysfs: check DAMOS regions update progress from before_terminate() SeongJae Park
2023-10-31 11:16   ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).