From patchwork Sun Aug 21 18:08:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 12950035 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 6F5E9C32789 for ; Sun, 21 Aug 2022 18:09:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 714CB6B0075; Sun, 21 Aug 2022 14:09:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C4228E0001; Sun, 21 Aug 2022 14:09:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 58B806B007B; Sun, 21 Aug 2022 14:09:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4B3A66B0075 for ; Sun, 21 Aug 2022 14:09:11 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B335E1A088F for ; Sun, 21 Aug 2022 18:09:10 +0000 (UTC) X-FDA: 79824386460.22.AA4C8DC Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id 30A14C0029 for ; Sun, 21 Aug 2022 18:09:10 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 4426960F36; Sun, 21 Aug 2022 18:09:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 01189C433C1; Sun, 21 Aug 2022 18:09:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1661105348; bh=CFxaHOeeNQD9u+hqUUFiCzrpMlWn7QCApyqs9t5RjH8=; h=From:To:Cc:Subject:Date:From; b=QYLY095YIKc1sgsq1NooWz9CeWSILMnF16lNotNHYEiotFvoxZRmjYgxHazbOnXju VU/vP6TFw93W2UCqHaW3pczWK4eaUxTTaf18AwawiRM449VvvBdnMfKdgnq2plxSru FEhktr6PSGOA2Bo1RIhPCl6NHnzPTVIWIVcm7M1SRTPT3hBdTK9S+QdI2RUoI0XHEA ULdFedLOJbSMqqeEjKc4eEfs8M0Ut1iTIhvKeDFbstW0Lfq4ePL4v5HmvU0cwte4ib w6MXWI8cfbOt0/2d8PI1+qE66l4DuL2/ywENhWZJ5otl2qOD6AjgYJEGnx1S13TOTX XXiUCzmFvi/iw== From: SeongJae Park To: akpm@linux-foundation.org Cc: badari.pulavarty@intel.com, gregkh@linuxfoundation.org, damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, SeongJae Park Subject: [PATCH v3] mm/damon/dbgfs: avoid duplicate context directory creation Date: Sun, 21 Aug 2022 18:08:53 +0000 Message-Id: <20220821180853.2400-1-sj@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661105350; a=rsa-sha256; cv=none; b=TmsS1jLsgqsIV0qCzoB1D4EkKGQv95Zwymt1Y754J3camlF04Hv2vz+Y8sTpe1G06mgmBo Hu+QdfL/XjpbcqtjNwJoldEnXhbY3Z1DDxAPzDLy9xEEnT22uoxiGjGI4DApnsl0LdfZQz AV8skZFwvVt1Oov4F9JvAwSCxk2ay5A= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QYLY095Y; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 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=1661105350; 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=2DVQVZSFvDfWnTI6+y/XPkBx2bfvXydU1XAEbZu/RZA=; b=HJed9RxeCTsPRNproqmlVrP5VhYkfZAtbZ6GO2bqOuC6yNCLyRDQmZe3YNApGocCkhX9Ma nhar+AynnYsmuOti+8i8Wiv6EPfTAy2AURX7cvU4GeFxs3iWfatuFCwIjzKNQnM/H5X1LG TPffOk1oMMjcfNCV3ceN10FDJC6C4lE= Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=QYLY095Y; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org X-Rspam-User: X-Stat-Signature: 5ono18kyxw3soicada4t85znethtjeqr X-Rspamd-Queue-Id: 30A14C0029 X-Rspamd-Server: rspam06 X-HE-Tag: 1661105350-60344 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: From: Badari Pulavarty From: Badari Pulavarty When user tries to create a DAMON context via the DAMON debugfs interface with a name of an already existing context, the context directory creation fails but a new context is created and added in the internal data structure, due to absence of the directory creation success check. As a result, memory could leak and DAMON cannot be turned on. An example test case is as below: # cd /sys/kernel/debug/damon/ # echo "off" > monitor_on # echo paddr > target_ids # echo "abc" > mk_context # echo "abc" > mk_context # echo $$ > abc/target_ids # echo "on" > monitor_on <<< fails Return value of 'debugfs_create_dir()' is expected to be ignored in general, but this is an exceptional case as DAMON feature is depending on the debugfs functionality and it has the potential duplicate name issue. This commit therefore fixes the issue by checking the directory creation failure and immediately return the error in the case. Fixes: 75c1c2b53c78 ("mm/damon/dbgfs: support multiple contexts") Cc: # 5.15.x Signed-off-by: Badari Pulavarty Signed-off-by: SeongJae Park --- Changes from v2 (https://lore.kernel.org/damon/20220819171930.16166-1-sj@kernel.org/) - Simply check the debugfs_create_dir() return value (Andrew Morton) - Put a comment for justifying check of the return value (Greg KH) Changes from v1 (https://lore.kernel.org/damon/DM6PR11MB3978994F75A4104D714437379C679@DM6PR11MB3978.namprd11.prod.outlook.com/) - Manually check duplicate entry instead of checking 'debugfs_create_dir()' return value - Reword commit message and the test case Seems Badari have some email client issue, so I (SJ) am making this third version of the patch based on Badari's final proposal and reposting on behalf of Badari. mm/damon/dbgfs.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c index 51d67c8050dd..3b55a1b219b5 100644 --- a/mm/damon/dbgfs.c +++ b/mm/damon/dbgfs.c @@ -818,6 +818,9 @@ static int dbgfs_mk_context(char *name) return -ENOENT; new_dir = debugfs_create_dir(name, root); + /* Below check is required for a potential duplicated name case */ + if (IS_ERR(new_dir)) + return PTR_ERR(new_dir); dbgfs_dirs[dbgfs_nr_ctxs] = new_dir; new_ctx = dbgfs_new_ctx();