From patchwork Thu Feb 2 14:11:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 51943 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp262650wrn; Thu, 2 Feb 2023 06:15:31 -0800 (PST) X-Google-Smtp-Source: AK7set+OSCTNQWmfTfj296IiotdSGGDjyQQbNevMo1HqdnJX4cVgleq6nyv6XBA983uVRrU66OmT X-Received: by 2002:a17:906:3bc3:b0:87f:89f2:c012 with SMTP id v3-20020a1709063bc300b0087f89f2c012mr6469797ejf.24.1675347331812; Thu, 02 Feb 2023 06:15:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675347331; cv=none; d=google.com; s=arc-20160816; b=RoLwcgYFJrTOv7yUunxdfcJwqUrecjzLQW8h0hF3dxSwxV2jUWaJJywIWBILa6Fr6N 3hrN+Zhr9m/m0MG8qaizPG3oVavIF+FcLZOJcxUEWglAp6CUXVlVB+MUC+2pIyQLFknM 5dcWZnpdbx3Bp2dE5luypggSfJl1orKf9rXk0aoXwWzoLVHO+MbLSLbqzSxxOhgTF1dz 73S/gwiMKuMrcMKWpnE8yTHu143/AZCMpMh/wxLn7qgkk2XNGZzhBcWWl8FROXbbwK0C FP7t7XOD6NdeNi0NV2audLzxsRru2ms+oYIVoT4JgJgbAwiqS7kE608+ue/s3hDUJodx oEEA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=Bb9u7NBaetVyPdMt+kCNJ3JzculXbC9P1n+FdqVmosY=; b=VxSrYBytJamlTop4WTcwLFBD40GHErqc0SwcfL78IBa+lV1G599fBiegVJkcBPqm2L sDxUoj761mk0/xlxL4ZKnoLBhyWh/rA+j66LGuyAFnIPJEkQin+c+2yUSJTXzjlfGRCC u478HT28PP6+6PzcqAOflUybT5x/0beyQF7+IZFXBMTVVJ4GACk1kKshGjEgeDVN8uH8 mF4zOP2Q4/wkvadCg+c/33wPiEgHBu1DCt7eY+CFBT19RXtk8d1FzE+6RJNvCtL4+tm0 8IxlOf27QANyI2Uj53CYxLiP64PYksIMoQWwGfZZo8pPBXhGqkIlGB7NKwalPI5gv1Wg zefA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SXvYC5vW; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 20-20020a170906019400b0088b79ec31edsi13461463ejb.572.2023.02.02.06.15.08; Thu, 02 Feb 2023 06:15:31 -0800 (PST) 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=SXvYC5vW; 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 S232135AbjBBOLv (ORCPT + 99 others); Thu, 2 Feb 2023 09:11:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231695AbjBBOLo (ORCPT ); Thu, 2 Feb 2023 09:11:44 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F187B8FB7E for ; Thu, 2 Feb 2023 06:11:42 -0800 (PST) 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 7502B61B63 for ; Thu, 2 Feb 2023 14:11:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D28FC433EF; Thu, 2 Feb 2023 14:11:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1675347101; bh=975b4kPA+NLq0YmeXLwxMZvqYcj3aaM9QWDIE/DupWk=; h=From:To:Cc:Subject:Date:From; b=SXvYC5vW2/L1O1Nmo3J3G2R2uHvLHPxvr+dq4MsIg5JINzOumTQvcanJNMoRNI6kc au35FOqbILogiDGy/cksnHKxUE41nkziisq/3G3XgOEexLL9+9hsIB6/PZOf6oNyQT f/ygnynDxAqGmFnATDmvaxW/V98Q7bHemdVr3LLc= From: Greg Kroah-Hartman To: linux-staging@lists.linux.dev Cc: Greg Kroah-Hartman , Paulo Miguel Almeida , Dan Carpenter , Sidong Yang , Liu Shixin , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , linux-kernel@vger.kernel.org Subject: [PATCH] staging: pi433: fix memory leak with using debugfs_lookup() Date: Thu, 2 Feb 2023 15:11:38 +0100 Message-Id: <20230202141138.2291946-1-gregkh@linuxfoundation.org> X-Mailer: git-send-email 2.39.1 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2772; i=gregkh@linuxfoundation.org; h=from:subject; bh=975b4kPA+NLq0YmeXLwxMZvqYcj3aaM9QWDIE/DupWk=; b=owGbwMvMwCRo6H6F97bub03G02pJDMm3j8zytqh9Wbmqb4ewgPrVYxwNlXwHpz9ft/HQw8lLV/aL L1X42BHLwiDIxCArpsjyZRvP0f0VhxS9DG1Pw8xhZQIZwsDFKQATMQ9gWHBL+vyp15oGrUJyT9rm5l 1gfMhg/Jphnl3WP23VaZxSEhErxFp5J53ucV9xHwA= X-Developer-Key: i=gregkh@linuxfoundation.org; a=openpgp; fpr=F4B60CC5BF78C2214A313DCB3147D40DDB2DFB29 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756729003801382150?= X-GMAIL-MSGID: =?utf-8?q?1756729003801382150?= When calling debugfs_lookup() the result must have dput() called on it, otherwise the memory will leak over time. To make things simpler, just call debugfs_lookup_and_remove() instead which handles all of the logic at once. This requires saving off the root directory dentry to make creation of individual device subdirectories easier. Cc: Paulo Miguel Almeida Cc: Dan Carpenter Cc: Sidong Yang Cc: Liu Shixin Cc: "Uwe Kleine-König" Cc: linux-staging@lists.linux.dev Cc: linux-kernel@vger.kernel.org Signed-off-by: Greg Kroah-Hartman --- drivers/staging/pi433/pi433_if.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/staging/pi433/pi433_if.c b/drivers/staging/pi433/pi433_if.c index d4e06a3929f3..b59f6a4cb611 100644 --- a/drivers/staging/pi433/pi433_if.c +++ b/drivers/staging/pi433/pi433_if.c @@ -55,6 +55,7 @@ static dev_t pi433_dev; static DEFINE_IDR(pi433_idr); static DEFINE_MUTEX(minor_lock); /* Protect idr accesses */ +static struct dentry *root_dir; /* debugfs root directory for the driver */ static struct class *pi433_class; /* mainly for udev to create /dev/pi433 */ @@ -1306,8 +1307,7 @@ static int pi433_probe(struct spi_device *spi) /* spi setup */ spi_set_drvdata(spi, device); - entry = debugfs_create_dir(dev_name(device->dev), - debugfs_lookup(KBUILD_MODNAME, NULL)); + entry = debugfs_create_dir(dev_name(device->dev), root_dir); debugfs_create_file("regs", 0400, entry, device, &pi433_debugfs_regs_fops); return 0; @@ -1333,9 +1333,8 @@ static int pi433_probe(struct spi_device *spi) static void pi433_remove(struct spi_device *spi) { struct pi433_device *device = spi_get_drvdata(spi); - struct dentry *mod_entry = debugfs_lookup(KBUILD_MODNAME, NULL); - debugfs_remove(debugfs_lookup(dev_name(device->dev), mod_entry)); + debugfs_lookup_and_remove(dev_name(device->dev), root_dir); /* free GPIOs */ free_gpio(device); @@ -1408,7 +1407,7 @@ static int __init pi433_init(void) return PTR_ERR(pi433_class); } - debugfs_create_dir(KBUILD_MODNAME, NULL); + root_dir = debugfs_create_dir(KBUILD_MODNAME, NULL); status = spi_register_driver(&pi433_spi_driver); if (status < 0) { @@ -1427,7 +1426,7 @@ static void __exit pi433_exit(void) spi_unregister_driver(&pi433_spi_driver); class_destroy(pi433_class); unregister_chrdev(MAJOR(pi433_dev), pi433_spi_driver.driver.name); - debugfs_remove_recursive(debugfs_lookup(KBUILD_MODNAME, NULL)); + debugfs_remove(root_dir); } module_exit(pi433_exit);