From patchwork Wed Oct 27 13:05:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Changbin Du X-Patchwork-Id: 12587289 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 55099C433F5 for ; Wed, 27 Oct 2021 13:05:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EB35C60F39 for ; Wed, 27 Oct 2021 13:05:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org EB35C60F39 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 72AE0940008; Wed, 27 Oct 2021 09:05:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6BC41940007; Wed, 27 Oct 2021 09:05:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 57ADF940008; Wed, 27 Oct 2021 09:05:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0155.hostedemail.com [216.40.44.155]) by kanga.kvack.org (Postfix) with ESMTP id 49208940007 for ; Wed, 27 Oct 2021 09:05:26 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id EFC0B2A491 for ; Wed, 27 Oct 2021 13:05:25 +0000 (UTC) X-FDA: 78742238610.28.268E540 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by imf18.hostedemail.com (Postfix) with ESMTP id A72AF40020A1 for ; Wed, 27 Oct 2021 13:05:25 +0000 (UTC) Received: by mail-wr1-f51.google.com with SMTP id v17so4026957wrv.9 for ; Wed, 27 Oct 2021 06:05:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OiCX8D31pPbHnzvf2oaw3Vd7y+KKNl/DTKEL0iqAa3o=; b=ENSl3WDByEE48jaQ6uANo5ZFCux4iaCaQEe+bJ/MLf93uVEov/ydZgcutP1xBeh1sy TsA4EEeFe5jNr12X4/BDoXpnSCVbxKabPm/gdQweQ6vej1jAKjyWH/GcW2FKaC2olRZG xNgfI/hkxkTjrA06pIOe5CH4pD+CI0xTZyO23GDXy52u/YCtx9auw4AvcIjQ687azct9 wj11kDuKxtcHteZxxsz656mZRWs9GJs4GOWDQ+OMPxmlwTrXZDxi+Zd8Mjp/Rqo9KFVH 1jIy/RtbHneEoJ3g7OwK2fjMtqaqM+xuUNfNPsYnW/Ui8LsJr9EcGisq2Y6EfxvhQPua 9IDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=OiCX8D31pPbHnzvf2oaw3Vd7y+KKNl/DTKEL0iqAa3o=; b=o+ElF3sL3+w1YNcHE4OJTnsSUaSrVxsMiui3CIj7MOHED3owNXWFnsd2NnfiQceyiB Rlj2Aw4ps8oEmCznIWD80GNFnFNxu4axMc04vajTOy7ttiSMFa1PZozJlETDAWJngt7V Hp9xFJM5eFCoLrB5otW8oQTa6dA3NoV9Og2Ju2Do6iAkyMOn0ZdHrkHbydsdR0hH/fRj kNXAXB5dScJdzd1hFM/oUUb7LOUIW7tGxQxxyCtJvhFbFAGgwIi4o4KaGPA8+WNnsXgX ecW+dtMSW2O8SKsuCfF0+g4Z9mUg98xHtOhLFtWY2EhGCWRXbAbGLJqPKoigqeBiff/1 /dIw== X-Gm-Message-State: AOAM530VGVV3n32eTyTmXQmDHv6Kq35ESqvASamBFMxsAX7xo/lKLwYo 5b6JXP2IEYb7UCCduKC82A4= X-Google-Smtp-Source: ABdhPJwrexgWGIaXV2EAye0Q7dfKIR5P9x2Nvmh1cjyBrWQzH9s9xZ6VrRQN2KpWoEZrETKwnb8e3g== X-Received: by 2002:adf:ed89:: with SMTP id c9mr42032701wro.268.1635339924403; Wed, 27 Oct 2021 06:05:24 -0700 (PDT) Received: from WRT-WX9.. ([95.179.207.30]) by smtp.gmail.com with ESMTPSA id v191sm3449104wme.36.2021.10.27.06.05.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Oct 2021 06:05:23 -0700 (PDT) From: Changbin Du To: Andrew Morton Cc: SeongJae Park , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Changbin Du Subject: [PATCH v2] mm/damon: simplify stop mechanism Date: Wed, 27 Oct 2021 21:05:17 +0800 Message-Id: <20211027130517.4404-1-changbin.du@gmail.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Stat-Signature: 91qkce4wckgdrxa88eyu37qstwq787s3 X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: A72AF40020A1 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=ENSl3WDB; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf18.hostedemail.com: domain of changbin.du@gmail.com designates 209.85.221.51 as permitted sender) smtp.mailfrom=changbin.du@gmail.com X-HE-Tag: 1635339925-318010 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: 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 reference of it before termination. Signed-off-by: Changbin Du Reviewed-by: SeongJae Park --- include/linux/damon.h | 1 - mm/damon/core.c | 51 +++++++++++++------------------------------ 2 files changed, 15 insertions(+), 37 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index a14b3cc54cab..50c6eb0dee1f 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -381,7 +381,6 @@ struct damon_ctx { /* public: */ struct task_struct *kdamond; - bool kdamond_stop; struct mutex kdamond_lock; struct damon_primitive primitive; diff --git a/mm/damon/core.c b/mm/damon/core.c index 46a6afea3030..f37c17b53814 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -390,17 +390,6 @@ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) return sz; } -static bool damon_kdamond_running(struct damon_ctx *ctx) -{ - bool running; - - mutex_lock(&ctx->kdamond_lock); - running = ctx->kdamond != NULL; - mutex_unlock(&ctx->kdamond_lock); - - return running; -} - static int kdamond_fn(void *data); /* @@ -418,7 +407,6 @@ static int __damon_start(struct damon_ctx *ctx) mutex_lock(&ctx->kdamond_lock); if (!ctx->kdamond) { err = 0; - ctx->kdamond_stop = false; ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d", nr_running_ctxs); if (IS_ERR(ctx->kdamond)) { @@ -474,13 +462,15 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs) */ 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); @@ -925,12 +915,8 @@ static bool kdamond_need_update_primitive(struct damon_ctx *ctx) static bool kdamond_need_stop(struct damon_ctx *ctx) { struct damon_target *t; - bool stop; - mutex_lock(&ctx->kdamond_lock); - stop = ctx->kdamond_stop; - mutex_unlock(&ctx->kdamond_lock); - if (stop) + if (kthread_should_stop()) return true; if (!ctx->primitive.target_valid) @@ -1021,13 +1007,6 @@ static int kdamond_wait_activation(struct damon_ctx *ctx) return -EBUSY; } -static void set_kdamond_stop(struct damon_ctx *ctx) -{ - mutex_lock(&ctx->kdamond_lock); - ctx->kdamond_stop = true; - mutex_unlock(&ctx->kdamond_lock); -} - /* * The monitoring daemon that runs as a kernel thread */ @@ -1038,17 +1017,18 @@ static int kdamond_fn(void *data) struct damon_region *r, *next; unsigned int max_nr_accesses = 0; unsigned long sz_limit = 0; + bool done = false; pr_debug("kdamond (%d) starts\n", current->pid); if (ctx->primitive.init) ctx->primitive.init(ctx); if (ctx->callback.before_start && ctx->callback.before_start(ctx)) - set_kdamond_stop(ctx); + done = true; sz_limit = damon_region_sz_limit(ctx); - while (!kdamond_need_stop(ctx)) { + while (!kdamond_need_stop(ctx) && !done) { if (kdamond_wait_activation(ctx)) continue; @@ -1056,7 +1036,7 @@ static int kdamond_fn(void *data) ctx->primitive.prepare_access_checks(ctx); if (ctx->callback.after_sampling && ctx->callback.after_sampling(ctx)) - set_kdamond_stop(ctx); + done = true; usleep_range(ctx->sample_interval, ctx->sample_interval + 1); @@ -1069,7 +1049,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,9 +1068,8 @@ 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);