debugfs: add debugfs_lookup_and_remove()
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 <stable@kernel.org> Reported-by: Kuyo Chang <kuyo.chang@mediatek.com> Tested-by: Kuyo Chang <kuyo.chang@mediatek.com> Link: https://lore.kernel.org/r/YxIaQ8cSinDR881k@kroah.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
parent
0d895d2bb1
commit
9fc8c5fa42
@ -742,6 +742,28 @@ void debugfs_remove(struct dentry *dentry)
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(debugfs_remove);
|
EXPORT_SYMBOL_GPL(debugfs_remove);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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_remove_recursive - recursively removes a directory
|
* debugfs_remove_recursive - recursively removes a directory
|
||||||
* @dentry: a pointer to a the dentry of the directory to be removed. If this
|
* @dentry: a pointer to a the dentry of the directory to be removed. If this
|
||||||
|
@ -85,6 +85,8 @@ struct dentry *debugfs_create_automount(const char *name,
|
|||||||
void debugfs_remove(struct dentry *dentry);
|
void debugfs_remove(struct dentry *dentry);
|
||||||
void debugfs_remove_recursive(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);
|
const struct file_operations *debugfs_real_fops(const struct file *filp);
|
||||||
|
|
||||||
int debugfs_file_get(struct dentry *dentry);
|
int debugfs_file_get(struct dentry *dentry);
|
||||||
@ -216,6 +218,10 @@ static inline void debugfs_remove(struct dentry *dentry)
|
|||||||
static inline void debugfs_remove_recursive(struct dentry *dentry)
|
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);
|
const struct file_operations *debugfs_real_fops(const struct file *filp);
|
||||||
|
|
||||||
static inline int debugfs_file_get(struct dentry *dentry)
|
static inline int debugfs_file_get(struct dentry *dentry)
|
||||||
|
Loading…
Reference in New Issue
Block a user