From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF9B7C433EF for ; Wed, 27 Oct 2021 10:52:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6019E60240 for ; Wed, 27 Oct 2021 10:52:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6019E60240 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 8AB35940008; Wed, 27 Oct 2021 06:52:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 85B6E940007; Wed, 27 Oct 2021 06:52:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77179940008; Wed, 27 Oct 2021 06:52:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0101.hostedemail.com [216.40.44.101]) by kanga.kvack.org (Postfix) with ESMTP id 6A2BE940007 for ; Wed, 27 Oct 2021 06:52:04 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 1D057181C1940 for ; Wed, 27 Oct 2021 10:52:04 +0000 (UTC) X-FDA: 78741902568.22.D4DA11C Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf26.hostedemail.com (Postfix) with ESMTP id 858E520019F1 for ; Wed, 27 Oct 2021 10:52:04 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 417866103B; Wed, 27 Oct 2021 10:52:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635331922; bh=b42wC/g9RlnpUJ/IcMnZ8nC7VV3irkCSL78veVUQExE=; h=From:To:Cc:Subject:Date:In-Reply-To:From; b=F1cFINxCe48pXXHFsjtgrsogkZAywUVld1dZXo9ofTlIJ1iAjVrGHSbH3TcbN9f5U vVMBlkwoW/qXeqcOsYZRdr8jEYEo+/P8yY1CYkWZPiSaahuu+xEhiGWYXvJPzck2Y1 TWv/OXYFAVDWCPXjBUt/qkbM6YODUKwy/aJRLVO3Kb0+j3YlLaczSzb/3vdT0EMd3w SQ0hPClEONY9ByKr5RMGpx9y4rwZcvhVwILkrn1hQMtf3Navu3Uwfvru9T1ub1nsw/ XPm5jxtn68D/93h9Qm2jaYg7HqJwD9sz7bgReCwTFnK83Nq7li9iBb9motpP/N8eep wDpB0H4P6vANA== From: SeongJae Park To: Changbin Du Cc: SeongJae Park , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/damon: simplify stop mechanism Date: Wed, 27 Oct 2021 10:51:58 +0000 Message-Id: <20211027105158.6932-1-sj@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20211027061338.xdqdyjaghrs63ncp@mail.google.com> X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 858E520019F1 X-Stat-Signature: oqtqjd76ek4tnzgcdemrgcfn7ekyjcdr Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=F1cFINxC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of sj@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sj@kernel.org X-HE-Tag: 1635331924-275715 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: On Wed, 27 Oct 2021 14:13:38 +0800 Changbin Du wrote: > On Tue, Oct 26, 2021 at 06:42:03PM +0000, SeongJae Park wrote: > > Hello Changbin, > > > > On Tue, 26 Oct 2021 23:30:33 +0800 Changbin Du wrote: > > > > > An kernel thread can exit gracefully with kthread_stop(). So we don't need a > > > new flag 'kdamond_stop'. And to make sure the task struct is not freed when > > > accessing it, get task struct on start and put it on stop. > > > > We previously considered using kthread_stop() here. However, we resulted in > > current code because kdamond can be self-terminated when all target processes > > are invalid[1]. > > > > Seems this patch is also not fully prepared for the self-termination case. I > > left some comments below. > > > > [1] https://lore.kernel.org/linux-mm/20210624102623.24563-1-sjpark@amazon.de/ > > > > > > > > And since the return value of 'before_terminate' callback is never used, > > > we make it have no return value. > > > > This looks nice to me. Could you please send this again as a separate patch? > > > Sure, I'll do it later. > > > > > > > Signed-off-by: Changbin Du > > > --- > > > include/linux/damon.h | 3 +-- > > > mm/damon/core.c | 59 +++++++++++++------------------------------ > > > mm/damon/dbgfs.c | 5 ++-- > > > 3 files changed, 20 insertions(+), 47 deletions(-) > > > > > > diff --git a/include/linux/damon.h b/include/linux/damon.h > > > index a14b3cc54cab..041966786270 100644 > > > --- a/include/linux/damon.h > > > +++ b/include/linux/damon.h > > [...] > > > --- a/mm/damon/core.c > > > +++ b/mm/damon/core.c > > [...] > > > @@ -1069,7 +1048,7 @@ static int kdamond_fn(void *data) > > > sz_limit); > > > if (ctx->callback.after_aggregation && > > > ctx->callback.after_aggregation(ctx)) > > > - set_kdamond_stop(ctx); > > > + done = true; > > > kdamond_apply_schemes(ctx); > > > kdamond_reset_aggregated(ctx); > > > kdamond_split_regions(ctx); > > > @@ -1088,16 +1067,12 @@ static int kdamond_fn(void *data) > > > damon_destroy_region(r, t); > > > } > > > > > > - if (ctx->callback.before_terminate && > > > - ctx->callback.before_terminate(ctx)) > > > - set_kdamond_stop(ctx); > > > + if (ctx->callback.before_terminate) > > > + ctx->callback.before_terminate(ctx); > > > if (ctx->primitive.cleanup) > > > ctx->primitive.cleanup(ctx); > > > > > > pr_debug("kdamond (%d) finishes\n", current->pid); > > > - mutex_lock(&ctx->kdamond_lock); > > > - ctx->kdamond = NULL; > > > - mutex_unlock(&ctx->kdamond_lock); > > > > When kdamond is self-terminating, ctx->kdamond will not be nullfified. As a > > result, this patch can introduce some errors like below: > > > > # cd /sys/kernel/debug/damon > > # sleep 60 & > > [1] 1926 > > # echo $(pidof sleep) > target_ids > > # echo on > monitor_on > > # cat monitor_on > > on > > # # after 60 seconds, sleep finishes and kdamond is self-terminated > > # cat monitor_on > > off > > # echo 42 > target_ids > > bash: echo: write error: Device or resource busy > > > > If we simply restore the nullification here with the mutex locking, we would > > result in a deadlock because __damon_stop() calls kthread_stop() while holding > > ctx->kdamond_lock. > > > > Also, the reference count of ctx->kdamond, which increased by __damon_start(), > > would not be decreased in the case. > > > > If so, I suppose below change should work correctly (still set ctx->kdamond to > NULL at the end of kdamond). > > static int __damon_stop(struct damon_ctx *ctx) > { > + struct task_struct *tsk; > + > mutex_lock(&ctx->kdamond_lock); > - if (ctx->kdamond) { > - ctx->kdamond_stop = true; > + tsk = ctx->kdamond; > + if (tsk) { > + get_task_struct(tsk); > mutex_unlock(&ctx->kdamond_lock); > - while (damon_kdamond_running(ctx)) > - usleep_range(ctx->sample_interval, > - ctx->sample_interval * 2); > + kthread_stop(tsk); > + put_task_struct(tsk); > return 0; > } > mutex_unlock(&ctx->kdamond_lock); Agreed, this seems to make sense. Could you please send a complete patch? Thanks, SJ > > > > If I'm missing something, please let me know. > > > > > > Thanks, > > SJ > > > > [...] > > -- > Cheers, > Changbin Du