From patchwork Sun Jan 2 11:21:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 12702188 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 68236C433EF for ; Sun, 2 Jan 2022 11:22:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 763B06B0071; Sun, 2 Jan 2022 06:21:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 713E36B0073; Sun, 2 Jan 2022 06:21:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5DB0C6B0074; Sun, 2 Jan 2022 06:21:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4BEE96B0071 for ; Sun, 2 Jan 2022 06:21:59 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0A6898249980 for ; Sun, 2 Jan 2022 11:21:59 +0000 (UTC) X-FDA: 78985107558.17.554A3FF Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf20.hostedemail.com (Postfix) with ESMTP id D9D751C0006 for ; Sun, 2 Jan 2022 11:21:49 +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 ams.source.kernel.org (Postfix) with ESMTPS id 2B3B0B8076C; Sun, 2 Jan 2022 11:21:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6176AC36AEB; Sun, 2 Jan 2022 11:21:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1641122516; bh=e5c93iVmg9zZH1VDOZ3D2QktHR1jOLFH92mK2h8O/qo=; h=From:To:Cc:Subject:Date:From; b=FrkSk2yK6ynCWcpoytw3s39fVXouH1yIsFWqc+cc/OLaOSajXVTbbAxrps77vZMja JviXvxQ3TXSvC78VQt153CvLtu6AQgIDlu1GN+lKYFFKEp1j0CGKI0dtFNoLWWZB8O t8oDp3QkT2Iiq01aAxnXRfBbpyK5OgcncODWqiN601SeJadQmGOoUNhCjMsnFALHNi KDkyR4XAMbyOvOBPmyXzVbFRy/UJxdUoxTFW82pFlvWZdu+Spc+bPyx5QyiZYsAh5z 8LHW8j3KW6C8y8K13xaTYd8fXkAHrMKaCjReDSP6+ZAuZSr23WBi2BunuycWiXv+ru z76WZnGjVs4rg== From: SeongJae Park To: stable@vger.kernel.org, gregkh@linuxfoundation.org Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, SeongJae Park , Linus Torvalds Subject: [PATCH for-v5.15.x] mm/damon/dbgfs: fix 'struct pid' leaks in 'dbgfs_target_ids_write()' Date: Sun, 2 Jan 2022 11:21:41 +0000 Message-Id: <20220102112141.12281-1-sj@kernel.org> X-Mailer: git-send-email 2.17.1 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D9D751C0006 X-Stat-Signature: 6qpia1oc3fafdki5k4phjdeanfz5cq9c Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=FrkSk2yK; spf=pass (imf20.hostedemail.com: domain of sj@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-HE-Tag: 1641122509-414918 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: commit ebb3f994dd92f8fb4d70c7541091216c1e10cb71 upstream. DAMON debugfs interface increases the reference counts of 'struct pid's for targets from the 'target_ids' file write callback ('dbgfs_target_ids_write()'), but decreases the counts only in DAMON monitoring termination callback ('dbgfs_before_terminate()'). Therefore, when 'target_ids' file is repeatedly written without DAMON monitoring start/termination, the reference count is not decreased and therefore memory for the 'struct pid' cannot be freed. This commit fixes this issue by decreasing the reference counts when 'target_ids' is written. Link: https://lkml.kernel.org/r/20211229124029.23348-1-sj@kernel.org Fixes: 4bc05954d007 ("mm/damon: implement a debugfs-based user space interface") Signed-off-by: SeongJae Park Cc: # 5.15.x Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds --- This is a backport of a DAMON fix that merged in the mainline, for v5.15.x stable series. mm/damon/dbgfs.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c index d3bc110430f9..36624990b577 100644 --- a/mm/damon/dbgfs.c +++ b/mm/damon/dbgfs.c @@ -185,6 +185,7 @@ static ssize_t dbgfs_target_ids_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { struct damon_ctx *ctx = file->private_data; + struct damon_target *t, *next_t; char *kbuf, *nrs; unsigned long *targets; ssize_t nr_targets; @@ -224,6 +225,13 @@ static ssize_t dbgfs_target_ids_write(struct file *file, goto unlock_out; } + /* remove previously set targets */ + damon_for_each_target_safe(t, next_t, ctx) { + if (targetid_is_pid(ctx)) + put_pid((struct pid *)t->id); + damon_destroy_target(t); + } + err = damon_set_targets(ctx, targets, nr_targets); if (err) { if (targetid_is_pid(ctx))