1 // SPDX-License-Identifier: GPL-2.0
3 * file.c - part of debugfs, a tiny little debug file system
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2004 IBM Inc.
8 * debugfs is for people to use instead of /proc or /sys.
9 * See Documentation/filesystems/ for more details.
12 #include <linux/module.h>
14 #include <linux/seq_file.h>
15 #include <linux/pagemap.h>
16 #include <linux/debugfs.h>
18 #include <linux/slab.h>
19 #include <linux/atomic.h>
20 #include <linux/device.h>
21 #include <linux/poll.h>
22 #include <linux/security.h>
26 struct poll_table_struct
;
28 static ssize_t
default_read_file(struct file
*file
, char __user
*buf
,
29 size_t count
, loff_t
*ppos
)
34 static ssize_t
default_write_file(struct file
*file
, const char __user
*buf
,
35 size_t count
, loff_t
*ppos
)
40 const struct file_operations debugfs_noop_file_operations
= {
41 .read
= default_read_file
,
42 .write
= default_write_file
,
44 .llseek
= noop_llseek
,
47 #define F_DENTRY(filp) ((filp)->f_path.dentry)
49 const struct file_operations
*debugfs_real_fops(const struct file
*filp
)
51 struct debugfs_fsdata
*fsd
= F_DENTRY(filp
)->d_fsdata
;
53 if ((unsigned long)fsd
& DEBUGFS_FSDATA_IS_REAL_FOPS_BIT
) {
55 * Urgh, we've been called w/o a protecting
62 return fsd
->real_fops
;
64 EXPORT_SYMBOL_GPL(debugfs_real_fops
);
67 * debugfs_file_get - mark the beginning of file data access
68 * @dentry: the dentry object whose data is being accessed.
70 * Up to a matching call to debugfs_file_put(), any successive call
71 * into the file removing functions debugfs_remove() and
72 * debugfs_remove_recursive() will block. Since associated private
73 * file data may only get freed after a successful return of any of
74 * the removal functions, you may safely access it after a successful
75 * call to debugfs_file_get() without worrying about lifetime issues.
77 * If -%EIO is returned, the file has already been removed and thus,
78 * it is not safe to access any of its data. If, on the other hand,
79 * it is allowed to access the file data, zero is returned.
81 int debugfs_file_get(struct dentry
*dentry
)
83 struct debugfs_fsdata
*fsd
;
86 d_fsd
= READ_ONCE(dentry
->d_fsdata
);
87 if (!((unsigned long)d_fsd
& DEBUGFS_FSDATA_IS_REAL_FOPS_BIT
)) {
90 fsd
= kmalloc(sizeof(*fsd
), GFP_KERNEL
);
94 fsd
->real_fops
= (void *)((unsigned long)d_fsd
&
95 ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT
);
96 refcount_set(&fsd
->active_users
, 1);
97 init_completion(&fsd
->active_users_drained
);
98 if (cmpxchg(&dentry
->d_fsdata
, d_fsd
, fsd
) != d_fsd
) {
100 fsd
= READ_ONCE(dentry
->d_fsdata
);
105 * In case of a successful cmpxchg() above, this check is
106 * strictly necessary and must follow it, see the comment in
107 * __debugfs_remove_file().
108 * OTOH, if the cmpxchg() hasn't been executed or wasn't
109 * successful, this serves the purpose of not starving
112 if (d_unlinked(dentry
))
115 if (!refcount_inc_not_zero(&fsd
->active_users
))
120 EXPORT_SYMBOL_GPL(debugfs_file_get
);
123 * debugfs_file_put - mark the end of file data access
124 * @dentry: the dentry object formerly passed to
125 * debugfs_file_get().
127 * Allow any ongoing concurrent call into debugfs_remove() or
128 * debugfs_remove_recursive() blocked by a former call to
129 * debugfs_file_get() to proceed and return to its caller.
131 void debugfs_file_put(struct dentry
*dentry
)
133 struct debugfs_fsdata
*fsd
= READ_ONCE(dentry
->d_fsdata
);
135 if (refcount_dec_and_test(&fsd
->active_users
))
136 complete(&fsd
->active_users_drained
);
138 EXPORT_SYMBOL_GPL(debugfs_file_put
);
141 * Only permit access to world-readable files when the kernel is locked down.
142 * We also need to exclude any file that has ways to write or alter it as root
143 * can bypass the permissions check.
145 static int debugfs_locked_down(struct inode
*inode
,
147 const struct file_operations
*real_fops
)
149 if ((inode
->i_mode
& 07777) == 0444 &&
150 !(filp
->f_mode
& FMODE_WRITE
) &&
151 !real_fops
->unlocked_ioctl
&&
152 !real_fops
->compat_ioctl
&&
156 if (security_locked_down(LOCKDOWN_DEBUGFS
))
162 static int open_proxy_open(struct inode
*inode
, struct file
*filp
)
164 struct dentry
*dentry
= F_DENTRY(filp
);
165 const struct file_operations
*real_fops
= NULL
;
168 r
= debugfs_file_get(dentry
);
170 return r
== -EIO
? -ENOENT
: r
;
172 real_fops
= debugfs_real_fops(filp
);
174 r
= debugfs_locked_down(inode
, filp
, real_fops
);
178 if (!fops_get(real_fops
)) {
180 if (real_fops
->owner
&&
181 real_fops
->owner
->state
== MODULE_STATE_GOING
)
185 /* Huh? Module did not clean up after itself at exit? */
186 WARN(1, "debugfs file owner did not clean up at exit: %pd",
191 replace_fops(filp
, real_fops
);
194 r
= real_fops
->open(inode
, filp
);
197 debugfs_file_put(dentry
);
201 const struct file_operations debugfs_open_proxy_file_operations
= {
202 .open
= open_proxy_open
,
205 #define PROTO(args...) args
206 #define ARGS(args...) args
208 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
209 static ret_type full_proxy_ ## name(proto) \
211 struct dentry *dentry = F_DENTRY(filp); \
212 const struct file_operations *real_fops; \
215 r = debugfs_file_get(dentry); \
218 real_fops = debugfs_real_fops(filp); \
219 r = real_fops->name(args); \
220 debugfs_file_put(dentry); \
224 FULL_PROXY_FUNC(llseek
, loff_t
, filp
,
225 PROTO(struct file
*filp
, loff_t offset
, int whence
),
226 ARGS(filp
, offset
, whence
));
228 FULL_PROXY_FUNC(read
, ssize_t
, filp
,
229 PROTO(struct file
*filp
, char __user
*buf
, size_t size
,
231 ARGS(filp
, buf
, size
, ppos
));
233 FULL_PROXY_FUNC(write
, ssize_t
, filp
,
234 PROTO(struct file
*filp
, const char __user
*buf
, size_t size
,
236 ARGS(filp
, buf
, size
, ppos
));
238 FULL_PROXY_FUNC(unlocked_ioctl
, long, filp
,
239 PROTO(struct file
*filp
, unsigned int cmd
, unsigned long arg
),
240 ARGS(filp
, cmd
, arg
));
242 static __poll_t
full_proxy_poll(struct file
*filp
,
243 struct poll_table_struct
*wait
)
245 struct dentry
*dentry
= F_DENTRY(filp
);
247 const struct file_operations
*real_fops
;
249 if (debugfs_file_get(dentry
))
252 real_fops
= debugfs_real_fops(filp
);
253 r
= real_fops
->poll(filp
, wait
);
254 debugfs_file_put(dentry
);
258 static int full_proxy_release(struct inode
*inode
, struct file
*filp
)
260 const struct dentry
*dentry
= F_DENTRY(filp
);
261 const struct file_operations
*real_fops
= debugfs_real_fops(filp
);
262 const struct file_operations
*proxy_fops
= filp
->f_op
;
266 * We must not protect this against removal races here: the
267 * original releaser should be called unconditionally in order
268 * not to leak any resources. Releasers must not assume that
269 * ->i_private is still being meaningful here.
271 if (real_fops
->release
)
272 r
= real_fops
->release(inode
, filp
);
274 replace_fops(filp
, d_inode(dentry
)->i_fop
);
275 kfree((void *)proxy_fops
);
280 static void __full_proxy_fops_init(struct file_operations
*proxy_fops
,
281 const struct file_operations
*real_fops
)
283 proxy_fops
->release
= full_proxy_release
;
284 if (real_fops
->llseek
)
285 proxy_fops
->llseek
= full_proxy_llseek
;
287 proxy_fops
->read
= full_proxy_read
;
288 if (real_fops
->write
)
289 proxy_fops
->write
= full_proxy_write
;
291 proxy_fops
->poll
= full_proxy_poll
;
292 if (real_fops
->unlocked_ioctl
)
293 proxy_fops
->unlocked_ioctl
= full_proxy_unlocked_ioctl
;
296 static int full_proxy_open(struct inode
*inode
, struct file
*filp
)
298 struct dentry
*dentry
= F_DENTRY(filp
);
299 const struct file_operations
*real_fops
= NULL
;
300 struct file_operations
*proxy_fops
= NULL
;
303 r
= debugfs_file_get(dentry
);
305 return r
== -EIO
? -ENOENT
: r
;
307 real_fops
= debugfs_real_fops(filp
);
309 r
= debugfs_locked_down(inode
, filp
, real_fops
);
313 if (!fops_get(real_fops
)) {
315 if (real_fops
->owner
&&
316 real_fops
->owner
->state
== MODULE_STATE_GOING
)
320 /* Huh? Module did not cleanup after itself at exit? */
321 WARN(1, "debugfs file owner did not clean up at exit: %pd",
327 proxy_fops
= kzalloc(sizeof(*proxy_fops
), GFP_KERNEL
);
332 __full_proxy_fops_init(proxy_fops
, real_fops
);
333 replace_fops(filp
, proxy_fops
);
335 if (real_fops
->open
) {
336 r
= real_fops
->open(inode
, filp
);
338 replace_fops(filp
, d_inode(dentry
)->i_fop
);
340 } else if (filp
->f_op
!= proxy_fops
) {
341 /* No protection against file removal anymore. */
342 WARN(1, "debugfs file owner replaced proxy fops: %pd",
353 debugfs_file_put(dentry
);
357 const struct file_operations debugfs_full_proxy_file_operations
= {
358 .open
= full_proxy_open
,
361 ssize_t
debugfs_attr_read(struct file
*file
, char __user
*buf
,
362 size_t len
, loff_t
*ppos
)
364 struct dentry
*dentry
= F_DENTRY(file
);
367 ret
= debugfs_file_get(dentry
);
370 ret
= simple_attr_read(file
, buf
, len
, ppos
);
371 debugfs_file_put(dentry
);
374 EXPORT_SYMBOL_GPL(debugfs_attr_read
);
376 ssize_t
debugfs_attr_write(struct file
*file
, const char __user
*buf
,
377 size_t len
, loff_t
*ppos
)
379 struct dentry
*dentry
= F_DENTRY(file
);
382 ret
= debugfs_file_get(dentry
);
385 ret
= simple_attr_write(file
, buf
, len
, ppos
);
386 debugfs_file_put(dentry
);
389 EXPORT_SYMBOL_GPL(debugfs_attr_write
);
391 static struct dentry
*debugfs_create_mode_unsafe(const char *name
, umode_t mode
,
392 struct dentry
*parent
, void *value
,
393 const struct file_operations
*fops
,
394 const struct file_operations
*fops_ro
,
395 const struct file_operations
*fops_wo
)
397 /* if there are no write bits set, make read only */
398 if (!(mode
& S_IWUGO
))
399 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
401 /* if there are no read bits set, make write only */
402 if (!(mode
& S_IRUGO
))
403 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
406 return debugfs_create_file_unsafe(name
, mode
, parent
, value
, fops
);
409 static int debugfs_u8_set(void *data
, u64 val
)
414 static int debugfs_u8_get(void *data
, u64
*val
)
419 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8
, debugfs_u8_get
, debugfs_u8_set
, "%llu\n");
420 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro
, debugfs_u8_get
, NULL
, "%llu\n");
421 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo
, NULL
, debugfs_u8_set
, "%llu\n");
424 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
425 * @name: a pointer to a string containing the name of the file to create.
426 * @mode: the permission that the file should have
427 * @parent: a pointer to the parent dentry for this file. This should be a
428 * directory dentry if set. If this parameter is %NULL, then the
429 * file will be created in the root of the debugfs filesystem.
430 * @value: a pointer to the variable that the file should read to and write
433 * This function creates a file in debugfs with the given name that
434 * contains the value of the variable @value. If the @mode variable is so
435 * set, it can be read from, and written to.
437 void debugfs_create_u8(const char *name
, umode_t mode
, struct dentry
*parent
,
440 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u8
,
441 &fops_u8_ro
, &fops_u8_wo
);
443 EXPORT_SYMBOL_GPL(debugfs_create_u8
);
445 static int debugfs_u16_set(void *data
, u64 val
)
450 static int debugfs_u16_get(void *data
, u64
*val
)
455 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16
, debugfs_u16_get
, debugfs_u16_set
, "%llu\n");
456 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro
, debugfs_u16_get
, NULL
, "%llu\n");
457 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo
, NULL
, debugfs_u16_set
, "%llu\n");
460 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
461 * @name: a pointer to a string containing the name of the file to create.
462 * @mode: the permission that the file should have
463 * @parent: a pointer to the parent dentry for this file. This should be a
464 * directory dentry if set. If this parameter is %NULL, then the
465 * file will be created in the root of the debugfs filesystem.
466 * @value: a pointer to the variable that the file should read to and write
469 * This function creates a file in debugfs with the given name that
470 * contains the value of the variable @value. If the @mode variable is so
471 * set, it can be read from, and written to.
473 void debugfs_create_u16(const char *name
, umode_t mode
, struct dentry
*parent
,
476 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u16
,
477 &fops_u16_ro
, &fops_u16_wo
);
479 EXPORT_SYMBOL_GPL(debugfs_create_u16
);
481 static int debugfs_u32_set(void *data
, u64 val
)
486 static int debugfs_u32_get(void *data
, u64
*val
)
491 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32
, debugfs_u32_get
, debugfs_u32_set
, "%llu\n");
492 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro
, debugfs_u32_get
, NULL
, "%llu\n");
493 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo
, NULL
, debugfs_u32_set
, "%llu\n");
496 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
497 * @name: a pointer to a string containing the name of the file to create.
498 * @mode: the permission that the file should have
499 * @parent: a pointer to the parent dentry for this file. This should be a
500 * directory dentry if set. If this parameter is %NULL, then the
501 * file will be created in the root of the debugfs filesystem.
502 * @value: a pointer to the variable that the file should read to and write
505 * This function creates a file in debugfs with the given name that
506 * contains the value of the variable @value. If the @mode variable is so
507 * set, it can be read from, and written to.
509 * This function will return a pointer to a dentry if it succeeds. This
510 * pointer must be passed to the debugfs_remove() function when the file is
511 * to be removed (no automatic cleanup happens if your module is unloaded,
512 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
515 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
518 struct dentry
*debugfs_create_u32(const char *name
, umode_t mode
,
519 struct dentry
*parent
, u32
*value
)
521 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u32
,
522 &fops_u32_ro
, &fops_u32_wo
);
524 EXPORT_SYMBOL_GPL(debugfs_create_u32
);
526 static int debugfs_u64_set(void *data
, u64 val
)
532 static int debugfs_u64_get(void *data
, u64
*val
)
537 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64
, debugfs_u64_get
, debugfs_u64_set
, "%llu\n");
538 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro
, debugfs_u64_get
, NULL
, "%llu\n");
539 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo
, NULL
, debugfs_u64_set
, "%llu\n");
542 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
543 * @name: a pointer to a string containing the name of the file to create.
544 * @mode: the permission that the file should have
545 * @parent: a pointer to the parent dentry for this file. This should be a
546 * directory dentry if set. If this parameter is %NULL, then the
547 * file will be created in the root of the debugfs filesystem.
548 * @value: a pointer to the variable that the file should read to and write
551 * This function creates a file in debugfs with the given name that
552 * contains the value of the variable @value. If the @mode variable is so
553 * set, it can be read from, and written to.
555 void debugfs_create_u64(const char *name
, umode_t mode
, struct dentry
*parent
,
558 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u64
,
559 &fops_u64_ro
, &fops_u64_wo
);
561 EXPORT_SYMBOL_GPL(debugfs_create_u64
);
563 static int debugfs_ulong_set(void *data
, u64 val
)
565 *(unsigned long *)data
= val
;
569 static int debugfs_ulong_get(void *data
, u64
*val
)
571 *val
= *(unsigned long *)data
;
574 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong
, debugfs_ulong_get
, debugfs_ulong_set
,
576 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro
, debugfs_ulong_get
, NULL
, "%llu\n");
577 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo
, NULL
, debugfs_ulong_set
, "%llu\n");
580 * debugfs_create_ulong - create a debugfs file that is used to read and write
581 * an unsigned long value.
582 * @name: a pointer to a string containing the name of the file to create.
583 * @mode: the permission that the file should have
584 * @parent: a pointer to the parent dentry for this file. This should be a
585 * directory dentry if set. If this parameter is %NULL, then the
586 * file will be created in the root of the debugfs filesystem.
587 * @value: a pointer to the variable that the file should read to and write
590 * This function creates a file in debugfs with the given name that
591 * contains the value of the variable @value. If the @mode variable is so
592 * set, it can be read from, and written to.
594 * This function will return a pointer to a dentry if it succeeds. This
595 * pointer must be passed to the debugfs_remove() function when the file is
596 * to be removed (no automatic cleanup happens if your module is unloaded,
597 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
600 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
603 struct dentry
*debugfs_create_ulong(const char *name
, umode_t mode
,
604 struct dentry
*parent
, unsigned long *value
)
606 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
,
607 &fops_ulong
, &fops_ulong_ro
,
610 EXPORT_SYMBOL_GPL(debugfs_create_ulong
);
612 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8
, debugfs_u8_get
, debugfs_u8_set
, "0x%02llx\n");
613 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro
, debugfs_u8_get
, NULL
, "0x%02llx\n");
614 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo
, NULL
, debugfs_u8_set
, "0x%02llx\n");
616 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16
, debugfs_u16_get
, debugfs_u16_set
,
618 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro
, debugfs_u16_get
, NULL
, "0x%04llx\n");
619 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo
, NULL
, debugfs_u16_set
, "0x%04llx\n");
621 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32
, debugfs_u32_get
, debugfs_u32_set
,
623 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro
, debugfs_u32_get
, NULL
, "0x%08llx\n");
624 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo
, NULL
, debugfs_u32_set
, "0x%08llx\n");
626 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64
, debugfs_u64_get
, debugfs_u64_set
,
628 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro
, debugfs_u64_get
, NULL
, "0x%016llx\n");
629 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo
, NULL
, debugfs_u64_set
, "0x%016llx\n");
632 * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value
634 * These functions are exactly the same as the above functions (but use a hex
635 * output for the decimal challenged). For details look at the above unsigned
640 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
641 * @name: a pointer to a string containing the name of the file to create.
642 * @mode: the permission that the file should have
643 * @parent: a pointer to the parent dentry for this file. This should be a
644 * directory dentry if set. If this parameter is %NULL, then the
645 * file will be created in the root of the debugfs filesystem.
646 * @value: a pointer to the variable that the file should read to and write
649 void debugfs_create_x8(const char *name
, umode_t mode
, struct dentry
*parent
,
652 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x8
,
653 &fops_x8_ro
, &fops_x8_wo
);
655 EXPORT_SYMBOL_GPL(debugfs_create_x8
);
658 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
659 * @name: a pointer to a string containing the name of the file to create.
660 * @mode: the permission that the file should have
661 * @parent: a pointer to the parent dentry for this file. This should be a
662 * directory dentry if set. If this parameter is %NULL, then the
663 * file will be created in the root of the debugfs filesystem.
664 * @value: a pointer to the variable that the file should read to and write
667 void debugfs_create_x16(const char *name
, umode_t mode
, struct dentry
*parent
,
670 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x16
,
671 &fops_x16_ro
, &fops_x16_wo
);
673 EXPORT_SYMBOL_GPL(debugfs_create_x16
);
676 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
677 * @name: a pointer to a string containing the name of the file to create.
678 * @mode: the permission that the file should have
679 * @parent: a pointer to the parent dentry for this file. This should be a
680 * directory dentry if set. If this parameter is %NULL, then the
681 * file will be created in the root of the debugfs filesystem.
682 * @value: a pointer to the variable that the file should read to and write
685 void debugfs_create_x32(const char *name
, umode_t mode
, struct dentry
*parent
,
688 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x32
,
689 &fops_x32_ro
, &fops_x32_wo
);
691 EXPORT_SYMBOL_GPL(debugfs_create_x32
);
694 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
695 * @name: a pointer to a string containing the name of the file to create.
696 * @mode: the permission that the file should have
697 * @parent: a pointer to the parent dentry for this file. This should be a
698 * directory dentry if set. If this parameter is %NULL, then the
699 * file will be created in the root of the debugfs filesystem.
700 * @value: a pointer to the variable that the file should read to and write
703 void debugfs_create_x64(const char *name
, umode_t mode
, struct dentry
*parent
,
706 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x64
,
707 &fops_x64_ro
, &fops_x64_wo
);
709 EXPORT_SYMBOL_GPL(debugfs_create_x64
);
712 static int debugfs_size_t_set(void *data
, u64 val
)
714 *(size_t *)data
= val
;
717 static int debugfs_size_t_get(void *data
, u64
*val
)
719 *val
= *(size_t *)data
;
722 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t
, debugfs_size_t_get
, debugfs_size_t_set
,
723 "%llu\n"); /* %llu and %zu are more or less the same */
724 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro
, debugfs_size_t_get
, NULL
, "%llu\n");
725 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo
, NULL
, debugfs_size_t_set
, "%llu\n");
728 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
729 * @name: a pointer to a string containing the name of the file to create.
730 * @mode: the permission that the file should have
731 * @parent: a pointer to the parent dentry for this file. This should be a
732 * directory dentry if set. If this parameter is %NULL, then the
733 * file will be created in the root of the debugfs filesystem.
734 * @value: a pointer to the variable that the file should read to and write
737 void debugfs_create_size_t(const char *name
, umode_t mode
,
738 struct dentry
*parent
, size_t *value
)
740 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_size_t
,
741 &fops_size_t_ro
, &fops_size_t_wo
);
743 EXPORT_SYMBOL_GPL(debugfs_create_size_t
);
745 static int debugfs_atomic_t_set(void *data
, u64 val
)
747 atomic_set((atomic_t
*)data
, val
);
750 static int debugfs_atomic_t_get(void *data
, u64
*val
)
752 *val
= atomic_read((atomic_t
*)data
);
755 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t
, debugfs_atomic_t_get
,
756 debugfs_atomic_t_set
, "%lld\n");
757 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro
, debugfs_atomic_t_get
, NULL
,
759 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo
, NULL
, debugfs_atomic_t_set
,
763 * debugfs_create_atomic_t - create a debugfs file that is used to read and
764 * write an atomic_t value
765 * @name: a pointer to a string containing the name of the file to create.
766 * @mode: the permission that the file should have
767 * @parent: a pointer to the parent dentry for this file. This should be a
768 * directory dentry if set. If this parameter is %NULL, then the
769 * file will be created in the root of the debugfs filesystem.
770 * @value: a pointer to the variable that the file should read to and write
773 void debugfs_create_atomic_t(const char *name
, umode_t mode
,
774 struct dentry
*parent
, atomic_t
*value
)
776 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_atomic_t
,
777 &fops_atomic_t_ro
, &fops_atomic_t_wo
);
779 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t
);
781 ssize_t
debugfs_read_file_bool(struct file
*file
, char __user
*user_buf
,
782 size_t count
, loff_t
*ppos
)
787 struct dentry
*dentry
= F_DENTRY(file
);
789 r
= debugfs_file_get(dentry
);
792 val
= *(bool *)file
->private_data
;
793 debugfs_file_put(dentry
);
801 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
803 EXPORT_SYMBOL_GPL(debugfs_read_file_bool
);
805 ssize_t
debugfs_write_file_bool(struct file
*file
, const char __user
*user_buf
,
806 size_t count
, loff_t
*ppos
)
810 bool *val
= file
->private_data
;
811 struct dentry
*dentry
= F_DENTRY(file
);
813 r
= kstrtobool_from_user(user_buf
, count
, &bv
);
815 r
= debugfs_file_get(dentry
);
819 debugfs_file_put(dentry
);
824 EXPORT_SYMBOL_GPL(debugfs_write_file_bool
);
826 static const struct file_operations fops_bool
= {
827 .read
= debugfs_read_file_bool
,
828 .write
= debugfs_write_file_bool
,
830 .llseek
= default_llseek
,
833 static const struct file_operations fops_bool_ro
= {
834 .read
= debugfs_read_file_bool
,
836 .llseek
= default_llseek
,
839 static const struct file_operations fops_bool_wo
= {
840 .write
= debugfs_write_file_bool
,
842 .llseek
= default_llseek
,
846 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
847 * @name: a pointer to a string containing the name of the file to create.
848 * @mode: the permission that the file should have
849 * @parent: a pointer to the parent dentry for this file. This should be a
850 * directory dentry if set. If this parameter is %NULL, then the
851 * file will be created in the root of the debugfs filesystem.
852 * @value: a pointer to the variable that the file should read to and write
855 * This function creates a file in debugfs with the given name that
856 * contains the value of the variable @value. If the @mode variable is so
857 * set, it can be read from, and written to.
859 * This function will return a pointer to a dentry if it succeeds. This
860 * pointer must be passed to the debugfs_remove() function when the file is
861 * to be removed (no automatic cleanup happens if your module is unloaded,
862 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
865 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
868 struct dentry
*debugfs_create_bool(const char *name
, umode_t mode
,
869 struct dentry
*parent
, bool *value
)
871 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_bool
,
872 &fops_bool_ro
, &fops_bool_wo
);
874 EXPORT_SYMBOL_GPL(debugfs_create_bool
);
876 static ssize_t
read_file_blob(struct file
*file
, char __user
*user_buf
,
877 size_t count
, loff_t
*ppos
)
879 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
880 struct dentry
*dentry
= F_DENTRY(file
);
883 r
= debugfs_file_get(dentry
);
886 r
= simple_read_from_buffer(user_buf
, count
, ppos
, blob
->data
,
888 debugfs_file_put(dentry
);
892 static const struct file_operations fops_blob
= {
893 .read
= read_file_blob
,
895 .llseek
= default_llseek
,
899 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
900 * @name: a pointer to a string containing the name of the file to create.
901 * @mode: the permission that the file should have
902 * @parent: a pointer to the parent dentry for this file. This should be a
903 * directory dentry if set. If this parameter is %NULL, then the
904 * file will be created in the root of the debugfs filesystem.
905 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
906 * to the blob data and the size of the data.
908 * This function creates a file in debugfs with the given name that exports
909 * @blob->data as a binary blob. If the @mode variable is so set it can be
910 * read from. Writing is not supported.
912 * This function will return a pointer to a dentry if it succeeds. This
913 * pointer must be passed to the debugfs_remove() function when the file is
914 * to be removed (no automatic cleanup happens if your module is unloaded,
915 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
918 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
921 struct dentry
*debugfs_create_blob(const char *name
, umode_t mode
,
922 struct dentry
*parent
,
923 struct debugfs_blob_wrapper
*blob
)
925 return debugfs_create_file_unsafe(name
, mode
, parent
, blob
, &fops_blob
);
927 EXPORT_SYMBOL_GPL(debugfs_create_blob
);
934 static size_t u32_format_array(char *buf
, size_t bufsize
,
935 u32
*array
, int array_size
)
939 while (--array_size
>= 0) {
941 char term
= array_size
? ' ' : '\n';
943 len
= snprintf(buf
, bufsize
, "%u%c", *array
++, term
);
952 static int u32_array_open(struct inode
*inode
, struct file
*file
)
954 struct array_data
*data
= inode
->i_private
;
955 int size
, elements
= data
->elements
;
960 * - 10 digits + ' '/'\n' = 11 bytes per number
961 * - terminating NUL character
964 buf
= kmalloc(size
+1, GFP_KERNEL
);
969 file
->private_data
= buf
;
970 u32_format_array(buf
, size
, data
->array
, data
->elements
);
972 return nonseekable_open(inode
, file
);
975 static ssize_t
u32_array_read(struct file
*file
, char __user
*buf
, size_t len
,
978 size_t size
= strlen(file
->private_data
);
980 return simple_read_from_buffer(buf
, len
, ppos
,
981 file
->private_data
, size
);
984 static int u32_array_release(struct inode
*inode
, struct file
*file
)
986 kfree(file
->private_data
);
991 static const struct file_operations u32_array_fops
= {
992 .owner
= THIS_MODULE
,
993 .open
= u32_array_open
,
994 .release
= u32_array_release
,
995 .read
= u32_array_read
,
1000 * debugfs_create_u32_array - create a debugfs file that is used to read u32
1002 * @name: a pointer to a string containing the name of the file to create.
1003 * @mode: the permission that the file should have.
1004 * @parent: a pointer to the parent dentry for this file. This should be a
1005 * directory dentry if set. If this parameter is %NULL, then the
1006 * file will be created in the root of the debugfs filesystem.
1007 * @array: u32 array that provides data.
1008 * @elements: total number of elements in the array.
1010 * This function creates a file in debugfs with the given name that exports
1011 * @array as data. If the @mode variable is so set it can be read from.
1012 * Writing is not supported. Seek within the file is also not supported.
1013 * Once array is created its size can not be changed.
1015 void debugfs_create_u32_array(const char *name
, umode_t mode
,
1016 struct dentry
*parent
, u32
*array
, u32 elements
)
1018 struct array_data
*data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
1023 data
->array
= array
;
1024 data
->elements
= elements
;
1026 debugfs_create_file_unsafe(name
, mode
, parent
, data
, &u32_array_fops
);
1028 EXPORT_SYMBOL_GPL(debugfs_create_u32_array
);
1030 #ifdef CONFIG_HAS_IOMEM
1033 * The regset32 stuff is used to print 32-bit registers using the
1034 * seq_file utilities. We offer printing a register set in an already-opened
1035 * sequential file or create a debugfs file that only prints a regset32.
1039 * debugfs_print_regs32 - use seq_print to describe a set of registers
1040 * @s: the seq_file structure being used to generate output
1041 * @regs: an array if struct debugfs_reg32 structures
1042 * @nregs: the length of the above array
1043 * @base: the base address to be used in reading the registers
1044 * @prefix: a string to be prefixed to every output line
1046 * This function outputs a text block describing the current values of
1047 * some 32-bit hardware registers. It is meant to be used within debugfs
1048 * files based on seq_file that need to show registers, intermixed with other
1049 * information. The prefix argument may be used to specify a leading string,
1050 * because some peripherals have several blocks of identical registers,
1051 * for example configuration of dma channels
1053 void debugfs_print_regs32(struct seq_file
*s
, const struct debugfs_reg32
*regs
,
1054 int nregs
, void __iomem
*base
, char *prefix
)
1058 for (i
= 0; i
< nregs
; i
++, regs
++) {
1060 seq_printf(s
, "%s", prefix
);
1061 seq_printf(s
, "%s = 0x%08x\n", regs
->name
,
1062 readl(base
+ regs
->offset
));
1063 if (seq_has_overflowed(s
))
1067 EXPORT_SYMBOL_GPL(debugfs_print_regs32
);
1069 static int debugfs_show_regset32(struct seq_file
*s
, void *data
)
1071 struct debugfs_regset32
*regset
= s
->private;
1073 debugfs_print_regs32(s
, regset
->regs
, regset
->nregs
, regset
->base
, "");
1077 static int debugfs_open_regset32(struct inode
*inode
, struct file
*file
)
1079 return single_open(file
, debugfs_show_regset32
, inode
->i_private
);
1082 static const struct file_operations fops_regset32
= {
1083 .open
= debugfs_open_regset32
,
1085 .llseek
= seq_lseek
,
1086 .release
= single_release
,
1090 * debugfs_create_regset32 - create a debugfs file that returns register values
1091 * @name: a pointer to a string containing the name of the file to create.
1092 * @mode: the permission that the file should have
1093 * @parent: a pointer to the parent dentry for this file. This should be a
1094 * directory dentry if set. If this parameter is %NULL, then the
1095 * file will be created in the root of the debugfs filesystem.
1096 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1097 * to an array of register definitions, the array size and the base
1098 * address where the register bank is to be found.
1100 * This function creates a file in debugfs with the given name that reports
1101 * the names and values of a set of 32-bit registers. If the @mode variable
1102 * is so set it can be read from. Writing is not supported.
1104 void debugfs_create_regset32(const char *name
, umode_t mode
,
1105 struct dentry
*parent
,
1106 struct debugfs_regset32
*regset
)
1108 debugfs_create_file(name
, mode
, parent
, regset
, &fops_regset32
);
1110 EXPORT_SYMBOL_GPL(debugfs_create_regset32
);
1112 #endif /* CONFIG_HAS_IOMEM */
1114 struct debugfs_devm_entry
{
1115 int (*read
)(struct seq_file
*seq
, void *data
);
1119 static int debugfs_devm_entry_open(struct inode
*inode
, struct file
*f
)
1121 struct debugfs_devm_entry
*entry
= inode
->i_private
;
1123 return single_open(f
, entry
->read
, entry
->dev
);
1126 static const struct file_operations debugfs_devm_entry_ops
= {
1127 .owner
= THIS_MODULE
,
1128 .open
= debugfs_devm_entry_open
,
1129 .release
= single_release
,
1135 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1137 * @dev: device related to this debugfs file.
1138 * @name: name of the debugfs file.
1139 * @parent: a pointer to the parent dentry for this file. This should be a
1140 * directory dentry if set. If this parameter is %NULL, then the
1141 * file will be created in the root of the debugfs filesystem.
1142 * @read_fn: function pointer called to print the seq_file content.
1144 struct dentry
*debugfs_create_devm_seqfile(struct device
*dev
, const char *name
,
1145 struct dentry
*parent
,
1146 int (*read_fn
)(struct seq_file
*s
,
1149 struct debugfs_devm_entry
*entry
;
1152 return ERR_PTR(-ENOENT
);
1154 entry
= devm_kzalloc(dev
, sizeof(*entry
), GFP_KERNEL
);
1156 return ERR_PTR(-ENOMEM
);
1158 entry
->read
= read_fn
;
1161 return debugfs_create_file(name
, S_IRUGO
, parent
, entry
,
1162 &debugfs_devm_entry_ops
);
1164 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile
);