Commit a2b992c8 authored by Jakub Kicinski's avatar Jakub Kicinski Committed by David S. Miller

debugfs: make sure we can remove u32_array files cleanly

debugfs_create_u32_array() allocates a small structure to wrap
the data and size information about the array. If users ever
try to remove the file this leads to a leak since nothing ever
frees this wrapper.

That said there are no upstream users of debugfs_create_u32_array()
that'd remove a u32 array file (we only have one u32 array user in
CMA), so there is no real bug here.

Make callers pass a wrapper they allocated. This way the lifetime
management of the wrapper is on the caller, and we can avoid the
potential leak in debugfs.

CC: Chucheng Luo <luochucheng@vivo.com>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 8fb49c01
...@@ -185,13 +185,17 @@ byte offsets over a base for the register block. ...@@ -185,13 +185,17 @@ byte offsets over a base for the register block.
If you want to dump an u32 array in debugfs, you can create file with:: If you want to dump an u32 array in debugfs, you can create file with::
struct debugfs_u32_array {
u32 *array;
u32 n_elements;
};
void debugfs_create_u32_array(const char *name, umode_t mode, void debugfs_create_u32_array(const char *name, umode_t mode,
struct dentry *parent, struct dentry *parent,
u32 *array, u32 elements); struct debugfs_u32_array *array);
The "array" argument provides data, and the "elements" argument is The "array" argument wraps a pointer to the array's data and the number
the number of elements in the array. Note: Once array is created its of its elements. Note: Once array is created its size can not be changed.
size can not be changed.
There is a helper function to create device related seq_file:: There is a helper function to create device related seq_file::
......
...@@ -918,11 +918,6 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode, ...@@ -918,11 +918,6 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode,
} }
EXPORT_SYMBOL_GPL(debugfs_create_blob); EXPORT_SYMBOL_GPL(debugfs_create_blob);
struct array_data {
void *array;
u32 elements;
};
static size_t u32_format_array(char *buf, size_t bufsize, static size_t u32_format_array(char *buf, size_t bufsize,
u32 *array, int array_size) u32 *array, int array_size)
{ {
...@@ -943,8 +938,8 @@ static size_t u32_format_array(char *buf, size_t bufsize, ...@@ -943,8 +938,8 @@ static size_t u32_format_array(char *buf, size_t bufsize,
static int u32_array_open(struct inode *inode, struct file *file) static int u32_array_open(struct inode *inode, struct file *file)
{ {
struct array_data *data = inode->i_private; struct debugfs_u32_array *data = inode->i_private;
int size, elements = data->elements; int size, elements = data->n_elements;
char *buf; char *buf;
/* /*
...@@ -959,7 +954,7 @@ static int u32_array_open(struct inode *inode, struct file *file) ...@@ -959,7 +954,7 @@ static int u32_array_open(struct inode *inode, struct file *file)
buf[size] = 0; buf[size] = 0;
file->private_data = buf; file->private_data = buf;
u32_format_array(buf, size, data->array, data->elements); u32_format_array(buf, size, data->array, data->n_elements);
return nonseekable_open(inode, file); return nonseekable_open(inode, file);
} }
...@@ -996,8 +991,7 @@ static const struct file_operations u32_array_fops = { ...@@ -996,8 +991,7 @@ static const struct file_operations u32_array_fops = {
* @parent: a pointer to the parent dentry for this file. This should be a * @parent: a pointer to the parent dentry for this file. This should be a
* directory dentry if set. If this parameter is %NULL, then the * directory dentry if set. If this parameter is %NULL, then the
* file will be created in the root of the debugfs filesystem. * file will be created in the root of the debugfs filesystem.
* @array: u32 array that provides data. * @array: wrapper struct containing data pointer and size of the array.
* @elements: total number of elements in the array.
* *
* This function creates a file in debugfs with the given name that exports * This function creates a file in debugfs with the given name that exports
* @array as data. If the @mode variable is so set it can be read from. * @array as data. If the @mode variable is so set it can be read from.
...@@ -1005,17 +999,10 @@ static const struct file_operations u32_array_fops = { ...@@ -1005,17 +999,10 @@ static const struct file_operations u32_array_fops = {
* Once array is created its size can not be changed. * Once array is created its size can not be changed.
*/ */
void debugfs_create_u32_array(const char *name, umode_t mode, void debugfs_create_u32_array(const char *name, umode_t mode,
struct dentry *parent, u32 *array, u32 elements) struct dentry *parent,
struct debugfs_u32_array *array)
{ {
struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL); debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
if (data == NULL)
return;
data->array = array;
data->elements = elements;
debugfs_create_file_unsafe(name, mode, parent, data, &u32_array_fops);
} }
EXPORT_SYMBOL_GPL(debugfs_create_u32_array); EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
......
...@@ -38,6 +38,11 @@ struct debugfs_regset32 { ...@@ -38,6 +38,11 @@ struct debugfs_regset32 {
struct device *dev; /* Optional device for Runtime PM */ struct device *dev; /* Optional device for Runtime PM */
}; };
struct debugfs_u32_array {
u32 *array;
u32 n_elements;
};
extern struct dentry *arch_debugfs_dir; extern struct dentry *arch_debugfs_dir;
#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \ #define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
...@@ -136,7 +141,8 @@ void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, ...@@ -136,7 +141,8 @@ void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
int nregs, void __iomem *base, char *prefix); int nregs, void __iomem *base, char *prefix);
void debugfs_create_u32_array(const char *name, umode_t mode, void debugfs_create_u32_array(const char *name, umode_t mode,
struct dentry *parent, u32 *array, u32 elements); struct dentry *parent,
struct debugfs_u32_array *array);
struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name, struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
struct dentry *parent, struct dentry *parent,
...@@ -316,8 +322,8 @@ static inline bool debugfs_initialized(void) ...@@ -316,8 +322,8 @@ static inline bool debugfs_initialized(void)
} }
static inline void debugfs_create_u32_array(const char *name, umode_t mode, static inline void debugfs_create_u32_array(const char *name, umode_t mode,
struct dentry *parent, u32 *array, struct dentry *parent,
u32 elements) struct debugfs_u32_array *array)
{ {
} }
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#ifndef __MM_CMA_H__ #ifndef __MM_CMA_H__
#define __MM_CMA_H__ #define __MM_CMA_H__
#include <linux/debugfs.h>
struct cma { struct cma {
unsigned long base_pfn; unsigned long base_pfn;
unsigned long count; unsigned long count;
...@@ -11,6 +13,7 @@ struct cma { ...@@ -11,6 +13,7 @@ struct cma {
#ifdef CONFIG_CMA_DEBUGFS #ifdef CONFIG_CMA_DEBUGFS
struct hlist_head mem_head; struct hlist_head mem_head;
spinlock_t mem_head_lock; spinlock_t mem_head_lock;
struct debugfs_u32_array dfs_bitmap;
#endif #endif
const char *name; const char *name;
}; };
......
...@@ -164,7 +164,6 @@ static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentry) ...@@ -164,7 +164,6 @@ static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentry)
{ {
struct dentry *tmp; struct dentry *tmp;
char name[16]; char name[16];
int u32s;
scnprintf(name, sizeof(name), "cma-%s", cma->name); scnprintf(name, sizeof(name), "cma-%s", cma->name);
...@@ -180,8 +179,10 @@ static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentry) ...@@ -180,8 +179,10 @@ static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentry)
debugfs_create_file("used", 0444, tmp, cma, &cma_used_fops); debugfs_create_file("used", 0444, tmp, cma, &cma_used_fops);
debugfs_create_file("maxchunk", 0444, tmp, cma, &cma_maxchunk_fops); debugfs_create_file("maxchunk", 0444, tmp, cma, &cma_maxchunk_fops);
u32s = DIV_ROUND_UP(cma_bitmap_maxno(cma), BITS_PER_BYTE * sizeof(u32)); cma->dfs_bitmap.array = (u32 *)cma->bitmap;
debugfs_create_u32_array("bitmap", 0444, tmp, (u32 *)cma->bitmap, u32s); cma->dfs_bitmap.n_elements = DIV_ROUND_UP(cma_bitmap_maxno(cma),
BITS_PER_BYTE * sizeof(u32));
debugfs_create_u32_array("bitmap", 0444, tmp, &cma->dfs_bitmap);
} }
static int __init cma_debugfs_init(void) static int __init cma_debugfs_init(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment