From: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: "linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
Bruce Chen <bruce.chen@unisoc.com>,
Cixi Geng <cixi.geng1@unisoc.com>, Cixi Geng <gengcixi@gmail.com>,
Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] USB: dwc3: fix memory leak with using debugfs_lookup()
Date: Thu, 2 Feb 2023 20:53:05 +0000 [thread overview]
Message-ID: <20230202205259.4n4skr7dvmelystt@synopsys.com> (raw)
In-Reply-To: <20230202152820.2409908-1-gregkh@linuxfoundation.org>
On Thu, Feb 02, 2023, Greg Kroah-Hartman wrote:
> 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.
>
> Note, the root dentry for the debugfs directory for the device needs to
> be saved so we don't have to keep looking it up, which required a bit
> more refactoring to properly create and remove it when needed.
>
> Reported-by: Bruce Chen <bruce.chen@unisoc.com>
> Reported-by: Cixi Geng <cixi.geng1@unisoc.com>
> Tested-by: Cixi Geng <gengcixi@gmail.com>
> Cc: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
> Cc: linux-usb@vger.kernel.org
> Cc: linux-kernel@vger.kernel.org
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
> drivers/usb/dwc3/core.h | 2 ++
> drivers/usb/dwc3/debug.h | 3 +++
> drivers/usb/dwc3/debugfs.c | 19 ++++++++-----------
> drivers/usb/dwc3/gadget.c | 4 +---
> 4 files changed, 14 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
> index 8f9959ba9fd4..582ebd9cf9c2 100644
> --- a/drivers/usb/dwc3/core.h
> +++ b/drivers/usb/dwc3/core.h
> @@ -1117,6 +1117,7 @@ struct dwc3_scratchpad_array {
> * address.
> * @num_ep_resized: carries the current number endpoints which have had its tx
> * fifo resized.
> + * @debug_root: root debugfs directory for this device to put its files in.
> */
> struct dwc3 {
> struct work_struct drd_work;
> @@ -1332,6 +1333,7 @@ struct dwc3 {
> int max_cfg_eps;
> int last_fifo_depth;
> int num_ep_resized;
> + struct dentry *debug_root;
> };
>
> #define INCRX_BURST_MODE 0
> diff --git a/drivers/usb/dwc3/debug.h b/drivers/usb/dwc3/debug.h
> index 48b44b88dc25..8bb2c9e3b9ac 100644
> --- a/drivers/usb/dwc3/debug.h
> +++ b/drivers/usb/dwc3/debug.h
> @@ -414,11 +414,14 @@ static inline const char *dwc3_gadget_generic_cmd_status_string(int status)
>
> #ifdef CONFIG_DEBUG_FS
> extern void dwc3_debugfs_create_endpoint_dir(struct dwc3_ep *dep);
> +extern void dwc3_debugfs_remove_endpoint_dir(struct dwc3_ep *dep);
> extern void dwc3_debugfs_init(struct dwc3 *d);
> extern void dwc3_debugfs_exit(struct dwc3 *d);
> #else
> static inline void dwc3_debugfs_create_endpoint_dir(struct dwc3_ep *dep)
> { }
> +static inline void dwc3_debugfs_remove_endpoint_dir(struct dwc3_ep *dep)
> +{ }
> static inline void dwc3_debugfs_init(struct dwc3 *d)
> { }
> static inline void dwc3_debugfs_exit(struct dwc3 *d)
> diff --git a/drivers/usb/dwc3/debugfs.c b/drivers/usb/dwc3/debugfs.c
> index f2b7675c7f62..850df0e6bcab 100644
> --- a/drivers/usb/dwc3/debugfs.c
> +++ b/drivers/usb/dwc3/debugfs.c
> @@ -873,27 +873,23 @@ static const struct dwc3_ep_file_map dwc3_ep_file_map[] = {
> { "GDBGEPINFO", &dwc3_ep_info_register_fops, },
> };
>
> -static void dwc3_debugfs_create_endpoint_files(struct dwc3_ep *dep,
> - struct dentry *parent)
> +void dwc3_debugfs_create_endpoint_dir(struct dwc3_ep *dep)
> {
> + struct dentry *dir;
> int i;
>
> + dir = debugfs_create_dir(dep->name, dep->dwc->debug_root);
> for (i = 0; i < ARRAY_SIZE(dwc3_ep_file_map); i++) {
> const struct file_operations *fops = dwc3_ep_file_map[i].fops;
> const char *name = dwc3_ep_file_map[i].name;
>
> - debugfs_create_file(name, 0444, parent, dep, fops);
> + debugfs_create_file(name, 0444, dir, dep, fops);
> }
> }
>
> -void dwc3_debugfs_create_endpoint_dir(struct dwc3_ep *dep)
> +void dwc3_debugfs_remove_endpoint_dir(struct dwc3_ep *dep)
> {
> - struct dentry *dir;
> - struct dentry *root;
> -
> - root = debugfs_lookup(dev_name(dep->dwc->dev), usb_debug_root);
> - dir = debugfs_create_dir(dep->name, root);
> - dwc3_debugfs_create_endpoint_files(dep, dir);
> + debugfs_lookup_and_remove(dep->name, dep->dwc->debug_root);
> }
>
> void dwc3_debugfs_init(struct dwc3 *dwc)
> @@ -911,6 +907,7 @@ void dwc3_debugfs_init(struct dwc3 *dwc)
> dwc->regset->base = dwc->regs - DWC3_GLOBALS_REGS_START;
>
> root = debugfs_create_dir(dev_name(dwc->dev), usb_debug_root);
> + dwc->debug_root = root;
> debugfs_create_regset32("regdump", 0444, root, dwc->regset);
> debugfs_create_file("lsp_dump", 0644, root, dwc, &dwc3_lsp_fops);
>
> @@ -929,6 +926,6 @@ void dwc3_debugfs_init(struct dwc3 *dwc)
>
> void dwc3_debugfs_exit(struct dwc3 *dwc)
> {
> - debugfs_remove(debugfs_lookup(dev_name(dwc->dev), usb_debug_root));
> + debugfs_lookup_and_remove(dev_name(dwc->dev), usb_debug_root);
> kfree(dwc->regset);
> }
> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> index 89dcfac01235..3c63fa97a680 100644
> --- a/drivers/usb/dwc3/gadget.c
> +++ b/drivers/usb/dwc3/gadget.c
> @@ -3194,9 +3194,7 @@ static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
> list_del(&dep->endpoint.ep_list);
> }
>
> - debugfs_remove_recursive(debugfs_lookup(dep->name,
> - debugfs_lookup(dev_name(dep->dwc->dev),
> - usb_debug_root)));
> + dwc3_debugfs_remove_endpoint_dir(dep);
> kfree(dep);
> }
> }
> --
> 2.39.1
>
Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
Thanks,
Thinh
prev parent reply other threads:[~2023-02-02 20:53 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-02 15:28 Greg Kroah-Hartman
2023-02-02 20:53 ` Thinh Nguyen [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230202205259.4n4skr7dvmelystt@synopsys.com \
--to=thinh.nguyen@synopsys.com \
--cc=bruce.chen@unisoc.com \
--cc=cixi.geng1@unisoc.com \
--cc=gengcixi@gmail.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®