Skip to content

Commit c646880

Browse files
nicstangegregkh
authored andcommitted
debugfs: add support for self-protecting attribute file fops
In order to protect them against file removal issues, debugfs_create_file() creates a lifetime managing proxy around each struct file_operations handed in. In cases where this struct file_operations is able to manage file lifetime by itself already, the proxy created by debugfs is a waste of resources. The most common class of struct file_operations given to debugfs are those defined by means of the DEFINE_SIMPLE_ATTRIBUTE() macro. Introduce a DEFINE_DEBUGFS_ATTRIBUTE() macro to allow any struct file_operations of this class to be easily made file lifetime aware and thus, to be operated unproxied. Specifically, introduce debugfs_attr_read() and debugfs_attr_write() which wrap simple_attr_read() and simple_attr_write() under the protection of a debugfs_use_file_start()/debugfs_use_file_finish() pair. Make DEFINE_DEBUGFS_ATTRIBUTE() set the defined struct file_operations' ->read() and ->write() members to these wrappers. Export debugfs_create_file_unsafe() in order to allow debugfs users to create their files in non-proxying operation mode. Signed-off-by: Nicolai Stange <nicstange@gmail.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 49d200d commit c646880

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎fs/debugfs/file.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,34 @@ const struct file_operations debugfs_full_proxy_file_operations = {
284284
.open = full_proxy_open,
285285
};
286286

287+
ssize_t debugfs_attr_read(struct file *file, char __user *buf,
288+
size_t len, loff_t *ppos)
289+
{
290+
ssize_t ret;
291+
int srcu_idx;
292+
293+
ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
294+
if (likely(!ret))
295+
ret = simple_attr_read(file, buf, len, ppos);
296+
debugfs_use_file_finish(srcu_idx);
297+
return ret;
298+
}
299+
EXPORT_SYMBOL_GPL(debugfs_attr_read);
300+
301+
ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
302+
size_t len, loff_t *ppos)
303+
{
304+
ssize_t ret;
305+
int srcu_idx;
306+
307+
ret = debugfs_use_file_start(F_DENTRY(file), &srcu_idx);
308+
if (likely(!ret))
309+
ret = simple_attr_write(file, buf, len, ppos);
310+
debugfs_use_file_finish(srcu_idx);
311+
return ret;
312+
}
313+
EXPORT_SYMBOL_GPL(debugfs_attr_write);
314+
287315
static struct dentry *debugfs_create_mode(const char *name, umode_t mode,
288316
struct dentry *parent, void *value,
289317
const struct file_operations *fops,

‎fs/debugfs/inode.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,33 @@ struct dentry *debugfs_create_file(const char *name, umode_t mode,
369369
}
370370
EXPORT_SYMBOL_GPL(debugfs_create_file);
371371

372+
/**
373+
* debugfs_create_file_unsafe - create a file in the debugfs filesystem
374+
* @name: a pointer to a string containing the name of the file to create.
375+
* @mode: the permission that the file should have.
376+
* @parent: a pointer to the parent dentry for this file. This should be a
377+
* directory dentry if set. If this parameter is NULL, then the
378+
* file will be created in the root of the debugfs filesystem.
379+
* @data: a pointer to something that the caller will want to get to later
380+
* on. The inode.i_private pointer will point to this value on
381+
* the open() call.
382+
* @fops: a pointer to a struct file_operations that should be used for
383+
* this file.
384+
*
385+
* debugfs_create_file_unsafe() is completely analogous to
386+
* debugfs_create_file(), the only difference being that the fops
387+
* handed it will not get protected against file removals by the
388+
* debugfs core.
389+
*
390+
* It is your responsibility to protect your struct file_operation
391+
* methods against file removals by means of debugfs_use_file_start()
392+
* and debugfs_use_file_finish(). ->open() is still protected by
393+
* debugfs though.
394+
*
395+
* Any struct file_operations defined by means of
396+
* DEFINE_DEBUGFS_ATTRIBUTE() is protected against file removals and
397+
* thus, may be used here.
398+
*/
372399
struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
373400
struct dentry *parent, void *data,
374401
const struct file_operations *fops)
@@ -379,6 +406,7 @@ struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
379406
&debugfs_noop_file_operations,
380407
fops);
381408
}
409+
EXPORT_SYMBOL_GPL(debugfs_create_file_unsafe);
382410

383411
/**
384412
* debugfs_create_file_size - create a file in the debugfs filesystem

‎include/linux/debugfs.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ extern struct srcu_struct debugfs_srcu;
5050
struct dentry *debugfs_create_file(const char *name, umode_t mode,
5151
struct dentry *parent, void *data,
5252
const struct file_operations *fops);
53+
struct dentry *debugfs_create_file_unsafe(const char *name, umode_t mode,
54+
struct dentry *parent, void *data,
55+
const struct file_operations *fops);
5356

5457
struct dentry *debugfs_create_file_size(const char *name, umode_t mode,
5558
struct dentry *parent, void *data,
@@ -74,6 +77,26 @@ int debugfs_use_file_start(const struct dentry *dentry, int *srcu_idx)
7477

7578
void debugfs_use_file_finish(int srcu_idx) __releases(&debugfs_srcu);
7679

80+
ssize_t debugfs_attr_read(struct file *file, char __user *buf,
81+
size_t len, loff_t *ppos);
82+
ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
83+
size_t len, loff_t *ppos);
84+
85+
#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
86+
static int __fops ## _open(struct inode *inode, struct file *file) \
87+
{ \
88+
__simple_attr_check_format(__fmt, 0ull); \
89+
return simple_attr_open(inode, file, __get, __set, __fmt); \
90+
} \
91+
static const struct file_operations __fops = { \
92+
.owner = THIS_MODULE, \
93+
.open = __fops ## _open, \
94+
.release = simple_attr_release, \
95+
.read = debugfs_attr_read, \
96+
.write = debugfs_attr_write, \
97+
.llseek = generic_file_llseek, \
98+
}
99+
77100
struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
78101
struct dentry *new_dir, const char *new_name);
79102

@@ -193,6 +216,9 @@ static inline void debugfs_use_file_finish(int srcu_idx)
193216
__releases(&debugfs_srcu)
194217
{ }
195218

219+
#define DEFINE_DEBUGFS_ATTRIBUTE(__fops, __get, __set, __fmt) \
220+
static const struct file_operations __fops = { 0 }
221+
196222
static inline struct dentry *debugfs_rename(struct dentry *old_dir, struct dentry *old_dentry,
197223
struct dentry *new_dir, char *new_name)
198224
{

0 commit comments

Comments
 (0)