From patchwork Fri Dec 8 17:50:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13485731 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 597A8C10F05 for ; Fri, 8 Dec 2023 17:50:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 990536B0088; Fri, 8 Dec 2023 12:50:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9402E6B0092; Fri, 8 Dec 2023 12:50:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82EC56B0093; Fri, 8 Dec 2023 12:50:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6C9E56B0088 for ; Fri, 8 Dec 2023 12:50:31 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 486771202A3 for ; Fri, 8 Dec 2023 17:50:31 +0000 (UTC) X-FDA: 81544390662.01.84CC4EF Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf22.hostedemail.com (Postfix) with ESMTP id 44B0BC001B for ; Fri, 8 Dec 2023 17:50:28 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UlSeOLw2; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702057829; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=I9lOd7mXeJ9d5DsCm6Y7KXH5NNhoFNpvoZ5766sIW/k=; b=7zhPtNyJWRp/ILhchWFMkf+u9jopALEti/kQowiuqWrLUuw3hjpnNoPadiISIuxl437AEK zWNFTWEOTF3x2DCx+y+6vBCzzBrSWQqy4kOxWKU5/5KFwQcdZY8HgLFPM2QhSmKfGgNN/O 1hz5fjMa1CtUaVfIZp1FK62wGplqXEY= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=UlSeOLw2; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of sj@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702057829; a=rsa-sha256; cv=none; b=kawPv1w/NN2f9hXxSzcOvR1l/il85IBtLZ0wVcpYFuoqybjdz1+6cp3RLw9RvD8ye2gIU9 IMOWJ+v3hutAv1QINNRz9r06bDDvkIpTbymYTSE7FDhrtpkw6EsLKFOL8Ich+MbEGcsUjW jqZW4nT3L+NMPK/NvrAthcUTPLfO62I= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 7C58CCE226B; Fri, 8 Dec 2023 17:50:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1DAA8C433C7; Fri, 8 Dec 2023 17:50:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1702057824; bh=cE24g6fBBFjzqibu8azeRT7zJ49z9KZ0TlE0lX4ftds=; h=From:To:Cc:Subject:Date:From; b=UlSeOLw24RssSEoLnv7u5w3h+UtuX6mWYBL9Sh+sfakwDsCwpeUev4OhHzla9QVIF bmw0uUM0zMIhikMsVXI4flrGhT7nrWczvLfnLb1tmAkKqRyW14rS7PD6Z5Ry7jdCsq T31MPJj9y5Fb8f1Mr6tYtVu+EIl+OHlkmWh8HltLCarJhdvkHS0bABlmDzQUyLNme5 doKc+VWZ76L7RKDPwaz0dnscdzGMwkMtCW0hA9iauFmt54bTg+k2cPE6iXNd0Kdj7X PPJOahPakyNBrLKKgly1pY+FieuiY/zJxO5qc0fKD5sIqAG7IlfdYpioRXermCrnOS 0tBPI3pUhujig== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , Changbin Du , Jakub Acs , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH] mm/damon/core: make damon_start() waits until kdamond_fn() starts Date: Fri, 8 Dec 2023 17:50:18 +0000 Message-Id: <20231208175018.63880-1-sj@kernel.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 44B0BC001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: yhiydd9m56xhgnf3gfhfdzbytuekgthb X-HE-Tag: 1702057828-301984 X-HE-Meta: U2FsdGVkX19oe2mnI12H9OiTXdTOTEugDecFARsicJULbsySV2RkabZjZ+MdatDj7fKklpfWUrfFjwmGNG/z6wH5oRd1JfARNPrJmW7Z3aDduybletbGUkxg9wuZ1Hket9a6FUhfElxmMN2MqEVNNgfnsv+Y0ki64i8JtEq3Yv5q83GKjOGCVvym+7MiSP9hHlxsKB3R8aEKwn0JcQkYRnWuqlmHeX1ut4kM9/fOpsH/giPtMmfLczWw3DaOIgyOETI+NROBCRzlGSBvCv8JIJdq/O8T1c22aep3sFVoPm3/Sdxlkll1A+eSyYddqAqAI8GUUg0boIyqHLdz0lJ0+MNcgw/H//4NSwen9wGuBIEhDRawyZv3pj6bepFWI1xLjuS3TLkOPBKPYqAmB3mRiuJ9CTsj4VnwGrcR/DEXV+mKpkM8Gty1F773dGz3n+SZQ9oOufOBfW/5SBent6bxbFGGWJ5jsIX6MxsXWUumnp15c2AM6uX0NXZKPWa0hBjuo82lWRp+8LM6FDmzGH0HkJT3+kj/E/z4nY3jCmsCVBhqntYwEwYngg7pIYJdmNes85xfFjRKlg/k8WMAbCQru0UT4fCtVRSkjxA0yM08sSjKXgRY2Ul3V3UJjK9BtaNfboVQi3cB0OH8CxYGK8Rs10/oQ7CCARnY4jxVNZ1Gx+6g/Au/9MdfuVUCSeNcFyOBGVt/j32iF6oVeHhm0iVA5MRsHGw7ZegW5xH8gGx5JstT9TosDnqggXi3BHhS0pYFWerrz7yJPseo4vITwT9WD9VHdyk7PEyo4XwmQSSme4OBdxmr3eh9hasuOKA/4Un6TTWwmalX4L8mpCgRaCF74J+nUGVYB8R/kilGVntuu/mQxGhNpVuW4WavFto07sCBlZeUYi8yZasnQrD5y4VDlE+SotaIljFBN04jQrz5VyNXLvnBv9Bp54EhVO/b5D+WncZ9HZOx8H4gzDYuQ3l LnO3GTVQ gc5Yn8ST1CmD5Vw+jSgNJKlXMv8YZ119VO3VhxznT62DEjPBtJOw46AA918Dycf/h3N4kOuQSxOS1+C4tjH2Tk2Roo1r1PBHB82tUT3DdxDBFgDXkWHX5MKANirQvOMfBv+6qt55Bzwjll9umiiIjuGTxrEmEiNqI3ptuVyi9QLc1No43Nl1J4JHkxBlRkBUOJ/m8eCdJuzp6LBepVV8R/S0PbLNlhFOn9GrVOWBZK9l7gtlCkzX+Q7Dcz768YBjSVIgJQe9wTlzhkoC3+K7AJDwhmx/0wDMoCPMq 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: List-Subscribe: List-Unsubscribe: The cleanup tasks of kdamond threads including reset of corresponding DAMON context's ->kdamond field and decrease of global nr_running_ctxs counter is supposed to be executed by kdamond_fn(). However, commit 0f91d13366a4 ("mm/damon: simplify stop mechanism") made neither damon_start() nor damon_stop() ensure the corresponding kdamond has started the execution of kdamond_fn(). As a result, the cleanup can be skipped if damon_stop() is called fast enough after the previous damon_start(). Especially the skipped reset of ->kdamond could cause a use-after-free. Fix it by waiting for start of kdamond_fn() execution from damon_start(). Fixes: 0f91d13366a4 ("mm/damon: simplify stop mechanism") Reported-by: Jakub Acs Cc: # 5.15.x Signed-off-by: SeongJae Park --- Note that the report has not publicly made, so this patch doesn't have a Closes: tag. include/linux/damon.h | 2 ++ mm/damon/core.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/linux/damon.h b/include/linux/damon.h index aa34ab433bc5..12510d8c51c6 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -579,6 +579,8 @@ struct damon_ctx { * update */ unsigned long next_ops_update_sis; + /* for waiting until the execution of the kdamond_fn is started */ + struct completion kdamond_started; /* public: */ struct task_struct *kdamond; diff --git a/mm/damon/core.c b/mm/damon/core.c index f91715a58dc7..2c0cc65d041e 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -445,6 +445,8 @@ struct damon_ctx *damon_new_ctx(void) if (!ctx) return NULL; + init_completion(&ctx->kdamond_started); + ctx->attrs.sample_interval = 5 * 1000; ctx->attrs.aggr_interval = 100 * 1000; ctx->attrs.ops_update_interval = 60 * 1000 * 1000; @@ -668,11 +670,14 @@ static int __damon_start(struct damon_ctx *ctx) mutex_lock(&ctx->kdamond_lock); if (!ctx->kdamond) { err = 0; + reinit_completion(&ctx->kdamond_started); ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d", nr_running_ctxs); if (IS_ERR(ctx->kdamond)) { err = PTR_ERR(ctx->kdamond); ctx->kdamond = NULL; + } else { + wait_for_completion(&ctx->kdamond_started); } } mutex_unlock(&ctx->kdamond_lock); @@ -1483,6 +1488,7 @@ static int kdamond_fn(void *data) pr_debug("kdamond (%d) starts\n", current->pid); + complete(&ctx->kdamond_started); kdamond_init_intervals_sis(ctx); if (ctx->ops.init)