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 23C77C433F5 for ; Wed, 27 Oct 2021 06:13:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A3B57610A0 for ; Wed, 27 Oct 2021 06:13:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org A3B57610A0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 10CE5940008; Wed, 27 Oct 2021 02:13:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0BD33940007; Wed, 27 Oct 2021 02:13:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7959940008; Wed, 27 Oct 2021 02:13:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0144.hostedemail.com [216.40.44.144]) by kanga.kvack.org (Postfix) with ESMTP id D4845940007 for ; Wed, 27 Oct 2021 02:13:46 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 89736348DB for ; Wed, 27 Oct 2021 06:13:46 +0000 (UTC) X-FDA: 78741201252.16.08A3F60 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by imf18.hostedemail.com (Postfix) with ESMTP id 2D4354002092 for ; Wed, 27 Oct 2021 06:13:46 +0000 (UTC) Received: by mail-wr1-f41.google.com with SMTP id k7so2085638wrd.13 for ; Tue, 26 Oct 2021 23:13:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=pibU8nLu7Th8DT1Zw3kLRTLSsjhZSIisT/Igppcs57Q=; b=phRWH/s3Bn6j4dE1ATH6pqYHd12lU25N0SzYP0sI7HNW4+lINtz6J/Sy0l7hwYh8c7 mhdKsbEhJ1rlPNjNKAmIAFxs7yOLyos8BtD/9h5BV2vN7rZK/8GtdENTRzGtGbvtmauj kkaMJC7lzHC9vyJgdedf2ukhbcJv8073dmPs8keCODOOzM0SX9Az59G1vDO+jbHvZ4X8 Nrl0n+QWItWKZ4a8XfKQMk36hBWSPER6VrJDoGLfZF6HGV01x4MZFVa66Sx7/DQpO2fw oWthBTUTjCzqFA5i3YVlbNEA270cui7M2d+fVYKoptsTciAqazejGQ2SGTX9SLW6LG90 Bi1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=pibU8nLu7Th8DT1Zw3kLRTLSsjhZSIisT/Igppcs57Q=; b=jF7ljTI7P2GIpYEafB/ui9l634qRWOXw07xjEj9k81hcLHJCkXweA3IdVujebHDLOo +1LGk0MQY/lzyrCbQKstLmAEvlITXoEoJiMpZuh+apwoUGcXxNXLWKNuRWTutWYGpOMk zs2iwZGPrlNrCa2Q7xVWNTUY9jRYI55MpFJDywhkMVRgSA95CmZgLTdJN9WMerRePIze dO9xcgt49BSuP+bDMr5C+Ql++ACIS/7QGKvBY/UKQTksujprVf36UMvxrau4/Wz8ccID uOxI9ZZtJwN1IsmMv6RYDrlHO1OFtmca8z8rrnjUzaq7KXKyvn+1EImgrc2881cC9UR2 Hlww== X-Gm-Message-State: AOAM531NkE1sOtY1ASR2hg9gjcM64+2IO4RXIF1yW9dPgIfVaTqDtvWs SjE42cU71D1qLP34bWR5Mec= X-Google-Smtp-Source: ABdhPJy+4Z7/lnrZTLEj6B/wxxqr+4WP7aDfa2X0Cu9Q4kQzUFKNuChYG+2NQrkcD/q4LP22pYAqxw== X-Received: by 2002:adf:fa8f:: with SMTP id h15mr38226022wrr.323.1635315224833; Tue, 26 Oct 2021 23:13:44 -0700 (PDT) Received: from mail.google.com ([95.179.207.30]) by smtp.gmail.com with ESMTPSA id y23sm2545459wmi.43.2021.10.26.23.13.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Oct 2021 23:13:44 -0700 (PDT) Date: Wed, 27 Oct 2021 14:13:38 +0800 From: Changbin Du To: SeongJae Park Cc: Changbin Du , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/damon: simplify stop mechanism Message-ID: <20211027061338.xdqdyjaghrs63ncp@mail.google.com> References: <20211026153033.11140-1-changbin.du@gmail.com> <20211026184203.1541-1-sj@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211026184203.1541-1-sj@kernel.org> X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 2D4354002092 X-Stat-Signature: 1u5ubeeqouxnpep3spa449ypbnukmya4 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b="phRWH/s3"; spf=pass (imf18.hostedemail.com: domain of changbin.du@gmail.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=changbin.du@gmail.com; dmarc=pass (policy=none) header.from=gmail.com X-HE-Tag: 1635315226-957276 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 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); > If I'm missing something, please let me know. > > > Thanks, > SJ > > [...] -- Cheers, Changbin Du