Skip to content

Commit a2b992c

Browse files
kuba-moodavem330
authored andcommitted
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 <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8fb49c0 commit a2b992c

File tree

5 files changed

+31
-30
lines changed

5 files changed

+31
-30
lines changed

Documentation/filesystems/debugfs.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,17 @@ byte offsets over a base for the register block.
185185

186186
If you want to dump an u32 array in debugfs, you can create file with::
187187

188+
struct debugfs_u32_array {
189+
u32 *array;
190+
u32 n_elements;
191+
};
192+
188193
void debugfs_create_u32_array(const char *name, umode_t mode,
189194
struct dentry *parent,
190-
u32 *array, u32 elements);
195+
struct debugfs_u32_array *array);
191196

192-
The "array" argument provides data, and the "elements" argument is
193-
the number of elements in the array. Note: Once array is created its
194-
size can not be changed.
197+
The "array" argument wraps a pointer to the array's data and the number
198+
of its elements. Note: Once array is created its size can not be changed.
195199

196200
There is a helper function to create device related seq_file::
197201

fs/debugfs/file.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -918,11 +918,6 @@ struct dentry *debugfs_create_blob(const char *name, umode_t mode,
918918
}
919919
EXPORT_SYMBOL_GPL(debugfs_create_blob);
920920

921-
struct array_data {
922-
void *array;
923-
u32 elements;
924-
};
925-
926921
static size_t u32_format_array(char *buf, size_t bufsize,
927922
u32 *array, int array_size)
928923
{
@@ -943,8 +938,8 @@ static size_t u32_format_array(char *buf, size_t bufsize,
943938

944939
static int u32_array_open(struct inode *inode, struct file *file)
945940
{
946-
struct array_data *data = inode->i_private;
947-
int size, elements = data->elements;
941+
struct debugfs_u32_array *data = inode->i_private;
942+
int size, elements = data->n_elements;
948943
char *buf;
949944

950945
/*
@@ -959,7 +954,7 @@ static int u32_array_open(struct inode *inode, struct file *file)
959954
buf[size] = 0;
960955

961956
file->private_data = buf;
962-
u32_format_array(buf, size, data->array, data->elements);
957+
u32_format_array(buf, size, data->array, data->n_elements);
963958

964959
return nonseekable_open(inode, file);
965960
}
@@ -996,26 +991,18 @@ static const struct file_operations u32_array_fops = {
996991
* @parent: a pointer to the parent dentry for this file. This should be a
997992
* directory dentry if set. If this parameter is %NULL, then the
998993
* file will be created in the root of the debugfs filesystem.
999-
* @array: u32 array that provides data.
1000-
* @elements: total number of elements in the array.
994+
* @array: wrapper struct containing data pointer and size of the array.
1001995
*
1002996
* This function creates a file in debugfs with the given name that exports
1003997
* @array as data. If the @mode variable is so set it can be read from.
1004998
* Writing is not supported. Seek within the file is also not supported.
1005999
* Once array is created its size can not be changed.
10061000
*/
10071001
void debugfs_create_u32_array(const char *name, umode_t mode,
1008-
struct dentry *parent, u32 *array, u32 elements)
1002+
struct dentry *parent,
1003+
struct debugfs_u32_array *array)
10091004
{
1010-
struct array_data *data = kmalloc(sizeof(*data), GFP_KERNEL);
1011-
1012-
if (data == NULL)
1013-
return;
1014-
1015-
data->array = array;
1016-
data->elements = elements;
1017-
1018-
debugfs_create_file_unsafe(name, mode, parent, data, &u32_array_fops);
1005+
debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
10191006
}
10201007
EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
10211008

include/linux/debugfs.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ struct debugfs_regset32 {
3838
struct device *dev; /* Optional device for Runtime PM */
3939
};
4040

41+
struct debugfs_u32_array {
42+
u32 *array;
43+
u32 n_elements;
44+
};
45+
4146
extern struct dentry *arch_debugfs_dir;
4247

4348
#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,
136141
int nregs, void __iomem *base, char *prefix);
137142

138143
void debugfs_create_u32_array(const char *name, umode_t mode,
139-
struct dentry *parent, u32 *array, u32 elements);
144+
struct dentry *parent,
145+
struct debugfs_u32_array *array);
140146

141147
struct dentry *debugfs_create_devm_seqfile(struct device *dev, const char *name,
142148
struct dentry *parent,
@@ -316,8 +322,8 @@ static inline bool debugfs_initialized(void)
316322
}
317323

318324
static inline void debugfs_create_u32_array(const char *name, umode_t mode,
319-
struct dentry *parent, u32 *array,
320-
u32 elements)
325+
struct dentry *parent,
326+
struct debugfs_u32_array *array)
321327
{
322328
}
323329

mm/cma.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef __MM_CMA_H__
33
#define __MM_CMA_H__
44

5+
#include <linux/debugfs.h>
6+
57
struct cma {
68
unsigned long base_pfn;
79
unsigned long count;
@@ -11,6 +13,7 @@ struct cma {
1113
#ifdef CONFIG_CMA_DEBUGFS
1214
struct hlist_head mem_head;
1315
spinlock_t mem_head_lock;
16+
struct debugfs_u32_array dfs_bitmap;
1417
#endif
1518
const char *name;
1619
};

mm/cma_debug.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentry)
164164
{
165165
struct dentry *tmp;
166166
char name[16];
167-
int u32s;
168167

169168
scnprintf(name, sizeof(name), "cma-%s", cma->name);
170169

@@ -180,8 +179,10 @@ static void cma_debugfs_add_one(struct cma *cma, struct dentry *root_dentry)
180179
debugfs_create_file("used", 0444, tmp, cma, &cma_used_fops);
181180
debugfs_create_file("maxchunk", 0444, tmp, cma, &cma_maxchunk_fops);
182181

183-
u32s = DIV_ROUND_UP(cma_bitmap_maxno(cma), BITS_PER_BYTE * sizeof(u32));
184-
debugfs_create_u32_array("bitmap", 0444, tmp, (u32 *)cma->bitmap, u32s);
182+
cma->dfs_bitmap.array = (u32 *)cma->bitmap;
183+
cma->dfs_bitmap.n_elements = DIV_ROUND_UP(cma_bitmap_maxno(cma),
184+
BITS_PER_BYTE * sizeof(u32));
185+
debugfs_create_u32_array("bitmap", 0444, tmp, &cma->dfs_bitmap);
185186
}
186187

187188
static int __init cma_debugfs_init(void)

0 commit comments

Comments
 (0)