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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 97DEAC001B5 for ; Fri, 2 Sep 2022 16:17:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07D16800D8; Fri, 2 Sep 2022 12:17:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 02CEF8D0014; Fri, 2 Sep 2022 12:17:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E3682800D8; Fri, 2 Sep 2022 12:17:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D421B8D0014 for ; Fri, 2 Sep 2022 12:17:06 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 9FAFD40B0C for ; Fri, 2 Sep 2022 16:17:06 +0000 (UTC) X-FDA: 79867649652.18.BC49113 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 4989514007A for ; Fri, 2 Sep 2022 16:17:06 +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 67264620C4; Fri, 2 Sep 2022 16:17:05 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9350BC433C1; Fri, 2 Sep 2022 16:17:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1662135424; bh=Yl0CvY2cQDQb6yeL6TcfJK7/w0lVfZxtsJZIiQXq0fo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=LmBARlASMMFNtLL56hHrW9qMXLJF2LTMfWDy72+nXemQGz0G7X3gflzIbt4/coFwk M57KPCk2OcuCRqSRXzza5lgPJmFkzXKe1MnJ/KpfFpbSSwlcK8DtXpjNNc7nYQuz/i j5PJHfilgicicwOjdVQnRgtmolPdsnAa0xKYK8MY= Date: Fri, 2 Sep 2022 09:17:03 -0700 From: Andrew Morton To: Greg Kroah-Hartman Cc: SeongJae Park , linux-kernel@vger.kernel.org, damon@lists.linux.dev, linux-mm@kvack.org, stable Subject: Re: [PATCH] mm/damon/dbgfs: fix memory leak when using debugfs_lookup() Message-Id: <20220902091703.dcee7737e7ce8857e3235fa7@linux-foundation.org> In-Reply-To: <20220902125631.128329-1-gregkh@linuxfoundation.org> References: <20220902125631.128329-1-gregkh@linuxfoundation.org> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662135426; a=rsa-sha256; cv=none; b=Fb8Y/tH+OqXzwIZFzkogW+8XvIjp5ORi7QxOCnfGNfE+YRZz6tJbRmiMjlDE+7rxbTC+Sa oslRQ2hPbPY8/yE0xz4ugVY66fvvAkI23ul+7FwWxG7bZXrkpGqBqPHcdJQgoCwFPvfeub 4vKHB6TPctZxBAyKhgQuUikyoQ3HX4g= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=LmBARlAS; spf=pass (imf26.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662135426; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=rbPyC8I8QYW/b7Qg3Kntr+CjwcaziuuHaEyLsvjx6bE=; b=BNffMXhDnddmIh4se8fplDcSPOq3zvUhGtZLq4ce7vI8mIsnMDO+u/kudPUANue5Bc2hts axD7GSb8PtMo3nJbOt0TL3+ML7JDGeyf+2lesbXC/a17zVBycFXFKmlP+3QH0SedfjhXV1 2xPDl7OvxdeW5gWtopFWLnUzmY+k3m0= Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=LmBARlAS; spf=pass (imf26.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none X-Stat-Signature: zdc97pwr4y3gihkcnw36y4dpq87nfd43 X-Rspamd-Queue-Id: 4989514007A X-Rspamd-Server: rspam04 X-Rspam-User: X-HE-Tag: 1662135426-451148 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 Fri, 2 Sep 2022 14:56:31 +0200 Greg Kroah-Hartman wrote: > When calling debugfs_lookup() the result must have dput() called on it, > otherwise the memory will leak over time. Fix this up by properly > calling dput(). > > ... > Fixes: 75c1c2b53c78b, I assume. > --- a/mm/damon/dbgfs.c > +++ b/mm/damon/dbgfs.c > @@ -915,6 +915,7 @@ static int dbgfs_rm_context(char *name) > new_ctxs[j++] = dbgfs_ctxs[i]; > } > > + dput(dir); > kfree(dbgfs_dirs); > kfree(dbgfs_ctxs); > dput() is also needed if either of the kmalloc_array() calls fail? Maybe something like --- a/mm/damon/dbgfs.c~a +++ a/mm/damon/dbgfs.c @@ -884,6 +884,7 @@ static int dbgfs_rm_context(char *name) struct dentry *root, *dir, **new_dirs; struct damon_ctx **new_ctxs; int i, j; + int ret = 0; if (damon_nr_running_ctxs()) return -EBUSY; @@ -899,14 +900,12 @@ static int dbgfs_rm_context(char *name) new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs), GFP_KERNEL); if (!new_dirs) - return -ENOMEM; + goto out_dput; new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs), GFP_KERNEL); - if (!new_ctxs) { - kfree(new_dirs); - return -ENOMEM; - } + if (!new_ctxs) + goto out_new_dirs; for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) { if (dbgfs_dirs[i] == dir) { @@ -925,7 +924,13 @@ static int dbgfs_rm_context(char *name) dbgfs_ctxs = new_ctxs; dbgfs_nr_ctxs--; - return 0; + goto out_dput; + +out_new_dirs: + kfree(new_dirs); +out_dput: + dput(dir); + return ret; } static ssize_t dbgfs_rm_context_write(struct file *file, _