Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6848315rwn; Tue, 13 Sep 2022 09:47:58 -0700 (PDT) X-Google-Smtp-Source: AA6agR5p/3bRPhkR+X+HNvkKic2eAylXFsrYzxWKefQ0e/YOUJFmGv4owSU784apTLVJ7qbXb4iG X-Received: by 2002:a62:1c8f:0:b0:537:2284:bd00 with SMTP id c137-20020a621c8f000000b005372284bd00mr33458872pfc.78.1663087677759; Tue, 13 Sep 2022 09:47:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663087677; cv=none; d=google.com; s=arc-20160816; b=oGG/yv/u1mZI9fyTxwxUKA/bgxfar9jrFGalnck3ktOBgByQ3Gl1snYdM2ejaCUzKH R2g7LBSgzhpcmY8slLsA2Hli74ZOKbIB7EySBBJvO/dpiZgDwtbqeYVR2LlAgyS4ZONe LSx4dmYPy5npNDaviUreFWNu0G0yJHFPk6BrppHNQTvpg0bctKbzQW4cffw0a09KUOaq Pf6685M2rz4NtzsVOo4h6fU0He+kuVyZ1vZqK7tDqn9M0r6fxqZgQAAT1rkFaR3xNOVK fb+O0etAAaT6jakdYCGo9bPkCzwx5SyxdZC+c/M5joGajMg5dAn1fqJCiZyOEojGrAT+ mBqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AwczKZjEChWKdHdQxHhj5rztxUyc/PpMzm5bj25BWLU=; b=PnC0KKoJ87kKMEY/b85IE9jjI3rlBKl7EYbm5e/F2XOC8GRmwVdG2RXS1SCTSOe+xW QGguswMfkD7EZKM4RRjw3DFLU7cDXoFejKorh0Ceu+d6zFnnFQCeo5bFzFZrk85e7+vo tCgw5LRm8eJTE/r3/G3o4cOcL/GYWZnYKy1XxM/gommjRciNX/LYdHOYrYsk4SxT20bY J+pYibnK1J8qAWJAfEBMG6IBDWiVP1cM0BTaZ8adrEQ2RAaBXvCCvLbvB+u+UPgsOF4e PnG8F7MbraFfvlDPnuP35un0aboG17eyEd4QJxnrTEsXSqYIniUPDlSxw80h4KrsIsds tX4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="muZ/cOFD"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v32-20020a634660000000b0043496cbbb81si988860pgk.386.2022.09.13.09.47.45; Tue, 13 Sep 2022 09:47:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="muZ/cOFD"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236211AbiIMQAy (ORCPT + 99 others); Tue, 13 Sep 2022 12:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54942 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235573AbiIMQAI (ORCPT ); Tue, 13 Sep 2022 12:00:08 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5854D956BC; Tue, 13 Sep 2022 07:58:27 -0700 (PDT) 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 914FC614EB; Tue, 13 Sep 2022 14:33:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86939C433D6; Tue, 13 Sep 2022 14:33:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663079593; bh=JBXl8o5PzldYcwYjx7aTRdDFZwXaECqL96GgEg8CeJY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=muZ/cOFDI7NSa9PYHpAgvLvt/eQV1jg5szRSoGMHJXrJXDYPMTwU3gmq6BkRbcgG9 U6QgjoZXr/laKwXy4wHKcyrnowKwHqLY/fzEYoyMcGYAiJPtAf397CP7v0ji0gsd/l tnQ6ER2SN9fhu6Sf8S9DiWPacxFswdV8yX1t7TLE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable , Kuyo Chang Subject: [PATCH 4.19 61/79] debugfs: add debugfs_lookup_and_remove() Date: Tue, 13 Sep 2022 16:07:19 +0200 Message-Id: <20220913140351.839626204@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140348.835121645@linuxfoundation.org> References: <20220913140348.835121645@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Greg Kroah-Hartman commit dec9b2f1e0455a151a7293c367da22ab973f713e upstream. There is a very common pattern of using debugfs_remove(debufs_lookup(..)) which results in a dentry leak of the dentry that was looked up. Instead of having to open-code the correct pattern of calling dput() on the dentry, create debugfs_lookup_and_remove() to handle this pattern automatically and properly without any memory leaks. Cc: stable Reported-by: Kuyo Chang Tested-by: Kuyo Chang Link: https://lore.kernel.org/r/YxIaQ8cSinDR881k@kroah.com Signed-off-by: Greg Kroah-Hartman --- fs/debugfs/inode.c | 22 ++++++++++++++++++++++ include/linux/debugfs.h | 6 ++++++ 2 files changed, 28 insertions(+) --- a/fs/debugfs/inode.c +++ b/fs/debugfs/inode.c @@ -767,6 +767,28 @@ void debugfs_remove_recursive(struct den EXPORT_SYMBOL_GPL(debugfs_remove_recursive); /** + * debugfs_lookup_and_remove - lookup a directory or file and recursively remove it + * @name: a pointer to a string containing the name of the item to look up. + * @parent: a pointer to the parent dentry of the item. + * + * This is the equlivant of doing something like + * debugfs_remove(debugfs_lookup(..)) but with the proper reference counting + * handled for the directory being looked up. + */ +void debugfs_lookup_and_remove(const char *name, struct dentry *parent) +{ + struct dentry *dentry; + + dentry = debugfs_lookup(name, parent); + if (!dentry) + return; + + debugfs_remove(dentry); + dput(dentry); +} +EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove); + +/** * debugfs_rename - rename a file/directory in the debugfs filesystem * @old_dir: a pointer to the parent dentry for the renamed object. This * should be a directory dentry. --- a/include/linux/debugfs.h +++ b/include/linux/debugfs.h @@ -85,6 +85,8 @@ struct dentry *debugfs_create_automount( void debugfs_remove(struct dentry *dentry); void debugfs_remove_recursive(struct dentry *dentry); +void debugfs_lookup_and_remove(const char *name, struct dentry *parent); + const struct file_operations *debugfs_real_fops(const struct file *filp); int debugfs_file_get(struct dentry *dentry); @@ -217,6 +219,10 @@ static inline void debugfs_remove(struct static inline void debugfs_remove_recursive(struct dentry *dentry) { } +static inline void debugfs_lookup_and_remove(const char *name, + struct dentry *parent) +{ } + const struct file_operations *debugfs_real_fops(const struct file *filp); static inline int debugfs_file_get(struct dentry *dentry)