[PATCH net] net: mvpp2: debugfs: fix problem with previous memory leak fix

From: Greg Kroah-Hartman
Date: Wed Sep 21 2022 - 07:44:56 EST


In commit fe2c9c61f668 ("net: mvpp2: debugfs: fix memory leak when using
debugfs_lookup()"), if the module is unloaded, the directory will still
be present if the module is loaded again and creating the directory will
fail, causing the creation of additional child debugfs entries for the
individual devices to fail.

As this module never cleaned up the root directory it created, even when
loaded, and unloading/loading a module is not a normal operation, none
of would normally happen.

To clean all of this up, use a tiny reference counted structure to hold
the root debugfs directory for the driver, and then clean it up when the
last user of it is removed from the system. This should resolve the
previously reported problems, and the original memory leak that
fe2c9c61f668 ("net: mvpp2: debugfs: fix memory leak when using
debugfs_lookup()") was attempting to fix.

Reported-by: Russell King <linux@xxxxxxxxxxxxxxx>
Cc: Marcin Wojtas <mw@xxxxxxxxxxxx>
Cc: "David S. Miller" <davem@xxxxxxxxxxxxx>
Cc: Eric Dumazet <edumazet@xxxxxxxxxx>
Cc: Jakub Kicinski <kuba@xxxxxxxxxx>
Cc: Paolo Abeni <pabeni@xxxxxxxxxx>
Cc: netdev@xxxxxxxxxxxxxxx
Cc: stable <stable@xxxxxxxxxx>
Fixes: fe2c9c61f668 ("net: mvpp2: debugfs: fix memory leak when using debugfs_lookup()")
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
Note, test-built only, I do not have access to this hardware so please
review it for any foolish mistakes I might have again made.

.../ethernet/marvell/mvpp2/mvpp2_debugfs.c | 32 ++++++++++++++++---
1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/marvell/mvpp2/mvpp2_debugfs.c b/drivers/net/ethernet/marvell/mvpp2/mvpp2_debugfs.c
index 0eec05d905eb..16c303048f25 100644
--- a/drivers/net/ethernet/marvell/mvpp2/mvpp2_debugfs.c
+++ b/drivers/net/ethernet/marvell/mvpp2/mvpp2_debugfs.c
@@ -691,23 +691,47 @@ static int mvpp2_dbgfs_port_init(struct dentry *parent,
return 0;
}

+static struct mvpp2_debug_dir {
+ struct dentry *dir;
+ struct kref kref;
+} *mvpp2_root;
+
+static void mvpp2_release(struct kref *kref)
+{
+ struct mvpp2_debug_dir *mvpp2_root = container_of(kref, struct mvpp2_debug_dir, kref);
+
+ debugfs_remove(mvpp2_root->dir);
+ kfree(mvpp2_root);
+}
+
void mvpp2_dbgfs_cleanup(struct mvpp2 *priv)
{
debugfs_remove_recursive(priv->dbgfs_dir);

kfree(priv->dbgfs_entries);
+
+ if (mvpp2_root)
+ kref_put(&mvpp2_root->kref, mvpp2_release);
}

void mvpp2_dbgfs_init(struct mvpp2 *priv, const char *name)
{
- static struct dentry *mvpp2_root;
struct dentry *mvpp2_dir;
int ret, i;

- if (!mvpp2_root)
- mvpp2_root = debugfs_create_dir(MVPP2_DRIVER_NAME, NULL);
+ if (!mvpp2_root) {
+ mvpp2_root = kzalloc(sizeof(mvpp2_root), GFP_KERNEL);
+ if (!mvpp2_root) {
+ mvpp2_root = NULL;
+ return;
+ }
+ mvpp2_root->dir = debugfs_create_dir(MVPP2_DRIVER_NAME, NULL);
+ kref_init(&mvpp2_root->kref);
+ } else {
+ kref_get(&mvpp2_root->kref);
+ }

- mvpp2_dir = debugfs_create_dir(name, mvpp2_root);
+ mvpp2_dir = debugfs_create_dir(name, mvpp2_root->dir);

priv->dbgfs_dir = mvpp2_dir;
priv->dbgfs_entries = kzalloc(sizeof(*priv->dbgfs_entries), GFP_KERNEL);
--
2.37.3