Skip to content

Commit dec9b2f

Browse files
committed
debugfs: add debugfs_lookup_and_remove()
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 <[email protected]> Reported-by: Kuyo Chang <[email protected]> Tested-by: Kuyo Chang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5666a27 commit dec9b2f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

fs/debugfs/inode.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,28 @@ void debugfs_remove(struct dentry *dentry)
744744
}
745745
EXPORT_SYMBOL_GPL(debugfs_remove);
746746

747+
/**
748+
* debugfs_lookup_and_remove - lookup a directory or file and recursively remove it
749+
* @name: a pointer to a string containing the name of the item to look up.
750+
* @parent: a pointer to the parent dentry of the item.
751+
*
752+
* This is the equlivant of doing something like
753+
* debugfs_remove(debugfs_lookup(..)) but with the proper reference counting
754+
* handled for the directory being looked up.
755+
*/
756+
void debugfs_lookup_and_remove(const char *name, struct dentry *parent)
757+
{
758+
struct dentry *dentry;
759+
760+
dentry = debugfs_lookup(name, parent);
761+
if (!dentry)
762+
return;
763+
764+
debugfs_remove(dentry);
765+
dput(dentry);
766+
}
767+
EXPORT_SYMBOL_GPL(debugfs_lookup_and_remove);
768+
747769
/**
748770
* debugfs_rename - rename a file/directory in the debugfs filesystem
749771
* @old_dir: a pointer to the parent dentry for the renamed object. This

include/linux/debugfs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ struct dentry *debugfs_create_automount(const char *name,
9191
void debugfs_remove(struct dentry *dentry);
9292
#define debugfs_remove_recursive debugfs_remove
9393

94+
void debugfs_lookup_and_remove(const char *name, struct dentry *parent);
95+
9496
const struct file_operations *debugfs_real_fops(const struct file *filp);
9597

9698
int debugfs_file_get(struct dentry *dentry);
@@ -225,6 +227,10 @@ static inline void debugfs_remove(struct dentry *dentry)
225227
static inline void debugfs_remove_recursive(struct dentry *dentry)
226228
{ }
227229

230+
static inline void debugfs_lookup_and_remove(const char *name,
231+
struct dentry *parent)
232+
{ }
233+
228234
const struct file_operations *debugfs_real_fops(const struct file *filp);
229235

230236
static inline int debugfs_file_get(struct dentry *dentry)

0 commit comments

Comments
 (0)