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/pm_runtime.h>
22 #include <linux/poll.h>
23 #include <linux/security.h>
27 struct poll_table_struct;
29 static ssize_t default_read_file(struct file *file, char __user *buf,
30 size_t count, loff_t *ppos)
35 static ssize_t default_write_file(struct file *file, const char __user *buf,
36 size_t count, loff_t *ppos)
41 const struct file_operations debugfs_noop_file_operations = {
42 .read = default_read_file,
43 .write = default_write_file,
45 .llseek = noop_llseek,
48 #define F_DENTRY(filp) ((filp)->f_path.dentry)
50 const void *debugfs_get_aux(const struct file *file)
52 return DEBUGFS_I(file_inode(file))->aux;
54 EXPORT_SYMBOL_GPL(debugfs_get_aux);
56 const struct file_operations *debugfs_real_fops(const struct file *filp)
58 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
62 * Urgh, we've been called w/o a protecting
69 return fsd->real_fops;
71 EXPORT_SYMBOL_GPL(debugfs_real_fops);
79 static int __debugfs_file_get(struct dentry *dentry, enum dbgfs_get_mode mode)
81 struct debugfs_fsdata *fsd;
85 * This could only happen if some debugfs user erroneously calls
86 * debugfs_file_get() on a dentry that isn't even a file, let
89 if (WARN_ON(!d_is_reg(dentry)))
92 d_fsd = READ_ONCE(dentry->d_fsdata);
96 struct inode *inode = dentry->d_inode;
97 unsigned int methods = 0;
99 if (WARN_ON(mode == DBGFS_GET_ALREADY))
102 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
106 if (mode == DBGFS_GET_SHORT) {
107 const struct debugfs_short_fops *ops;
108 ops = fsd->short_fops = DEBUGFS_I(inode)->short_fops;
110 methods |= HAS_LSEEK;
114 methods |= HAS_WRITE;
115 fsd->real_fops = NULL;
117 const struct file_operations *ops;
118 ops = fsd->real_fops = DEBUGFS_I(inode)->real_fops;
120 methods |= HAS_LSEEK;
124 methods |= HAS_WRITE;
125 if (ops->unlocked_ioctl)
126 methods |= HAS_IOCTL;
129 fsd->short_fops = NULL;
131 fsd->methods = methods;
132 refcount_set(&fsd->active_users, 1);
133 init_completion(&fsd->active_users_drained);
134 INIT_LIST_HEAD(&fsd->cancellations);
135 mutex_init(&fsd->cancellations_mtx);
137 d_fsd = cmpxchg(&dentry->d_fsdata, NULL, fsd);
139 mutex_destroy(&fsd->cancellations_mtx);
146 * In case of a successful cmpxchg() above, this check is
147 * strictly necessary and must follow it, see the comment in
148 * __debugfs_remove_file().
149 * OTOH, if the cmpxchg() hasn't been executed or wasn't
150 * successful, this serves the purpose of not starving
153 if (d_unlinked(dentry))
156 if (!refcount_inc_not_zero(&fsd->active_users))
163 * debugfs_file_get - mark the beginning of file data access
164 * @dentry: the dentry object whose data is being accessed.
166 * Up to a matching call to debugfs_file_put(), any successive call
167 * into the file removing functions debugfs_remove() and
168 * debugfs_remove_recursive() will block. Since associated private
169 * file data may only get freed after a successful return of any of
170 * the removal functions, you may safely access it after a successful
171 * call to debugfs_file_get() without worrying about lifetime issues.
173 * If -%EIO is returned, the file has already been removed and thus,
174 * it is not safe to access any of its data. If, on the other hand,
175 * it is allowed to access the file data, zero is returned.
177 int debugfs_file_get(struct dentry *dentry)
179 return __debugfs_file_get(dentry, DBGFS_GET_ALREADY);
181 EXPORT_SYMBOL_GPL(debugfs_file_get);
184 * debugfs_file_put - mark the end of file data access
185 * @dentry: the dentry object formerly passed to
186 * debugfs_file_get().
188 * Allow any ongoing concurrent call into debugfs_remove() or
189 * debugfs_remove_recursive() blocked by a former call to
190 * debugfs_file_get() to proceed and return to its caller.
192 void debugfs_file_put(struct dentry *dentry)
194 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
196 if (refcount_dec_and_test(&fsd->active_users))
197 complete(&fsd->active_users_drained);
199 EXPORT_SYMBOL_GPL(debugfs_file_put);
202 * debugfs_enter_cancellation - enter a debugfs cancellation
203 * @file: the file being accessed
204 * @cancellation: the cancellation object, the cancel callback
205 * inside of it must be initialized
207 * When a debugfs file is removed it needs to wait for all active
208 * operations to complete. However, the operation itself may need
209 * to wait for hardware or completion of some asynchronous process
210 * or similar. As such, it may need to be cancelled to avoid long
211 * waits or even deadlocks.
213 * This function can be used inside a debugfs handler that may
214 * need to be cancelled. As soon as this function is called, the
215 * cancellation's 'cancel' callback may be called, at which point
216 * the caller should proceed to call debugfs_leave_cancellation()
217 * and leave the debugfs handler function as soon as possible.
218 * Note that the 'cancel' callback is only ever called in the
219 * context of some kind of debugfs_remove().
221 * This function must be paired with debugfs_leave_cancellation().
223 void debugfs_enter_cancellation(struct file *file,
224 struct debugfs_cancellation *cancellation)
226 struct debugfs_fsdata *fsd;
227 struct dentry *dentry = F_DENTRY(file);
229 INIT_LIST_HEAD(&cancellation->list);
231 if (WARN_ON(!d_is_reg(dentry)))
234 if (WARN_ON(!cancellation->cancel))
237 fsd = READ_ONCE(dentry->d_fsdata);
241 mutex_lock(&fsd->cancellations_mtx);
242 list_add(&cancellation->list, &fsd->cancellations);
243 mutex_unlock(&fsd->cancellations_mtx);
245 /* if we're already removing wake it up to cancel */
246 if (d_unlinked(dentry))
247 complete(&fsd->active_users_drained);
249 EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
252 * debugfs_leave_cancellation - leave cancellation section
253 * @file: the file being accessed
254 * @cancellation: the cancellation previously registered with
255 * debugfs_enter_cancellation()
257 * See the documentation of debugfs_enter_cancellation().
259 void debugfs_leave_cancellation(struct file *file,
260 struct debugfs_cancellation *cancellation)
262 struct debugfs_fsdata *fsd;
263 struct dentry *dentry = F_DENTRY(file);
265 if (WARN_ON(!d_is_reg(dentry)))
268 fsd = READ_ONCE(dentry->d_fsdata);
272 mutex_lock(&fsd->cancellations_mtx);
273 if (!list_empty(&cancellation->list))
274 list_del(&cancellation->list);
275 mutex_unlock(&fsd->cancellations_mtx);
277 EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
280 * Only permit access to world-readable files when the kernel is locked down.
281 * We also need to exclude any file that has ways to write or alter it as root
282 * can bypass the permissions check.
284 static int debugfs_locked_down(struct inode *inode,
286 const struct file_operations *real_fops)
288 if ((inode->i_mode & 07777 & ~0444) == 0 &&
289 !(filp->f_mode & FMODE_WRITE) &&
291 (!real_fops->unlocked_ioctl &&
292 !real_fops->compat_ioctl &&
296 if (security_locked_down(LOCKDOWN_DEBUGFS))
302 static int open_proxy_open(struct inode *inode, struct file *filp)
304 struct dentry *dentry = F_DENTRY(filp);
305 const struct file_operations *real_fops = NULL;
308 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
310 return r == -EIO ? -ENOENT : r;
312 real_fops = debugfs_real_fops(filp);
314 r = debugfs_locked_down(inode, filp, real_fops);
318 if (!fops_get(real_fops)) {
319 #ifdef CONFIG_MODULES
320 if (real_fops->owner &&
321 real_fops->owner->state == MODULE_STATE_GOING) {
327 /* Huh? Module did not clean up after itself at exit? */
328 WARN(1, "debugfs file owner did not clean up at exit: %pd",
333 replace_fops(filp, real_fops);
336 r = real_fops->open(inode, filp);
339 debugfs_file_put(dentry);
343 const struct file_operations debugfs_open_proxy_file_operations = {
344 .open = open_proxy_open,
347 #define PROTO(args...) args
348 #define ARGS(args...) args
350 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args, bit, ret) \
351 static ret_type full_proxy_ ## name(proto) \
353 struct dentry *dentry = F_DENTRY(filp); \
354 struct debugfs_fsdata *fsd = dentry->d_fsdata; \
355 const struct file_operations *real_fops; \
358 if (!(fsd->methods & bit)) \
360 r = debugfs_file_get(dentry); \
363 real_fops = debugfs_real_fops(filp); \
364 r = real_fops->name(args); \
365 debugfs_file_put(dentry); \
369 #define FULL_PROXY_FUNC_BOTH(name, ret_type, filp, proto, args, bit, ret) \
370 static ret_type full_proxy_ ## name(proto) \
372 struct dentry *dentry = F_DENTRY(filp); \
373 struct debugfs_fsdata *fsd = dentry->d_fsdata; \
376 if (!(fsd->methods & bit)) \
378 r = debugfs_file_get(dentry); \
381 if (fsd->real_fops) \
382 r = fsd->real_fops->name(args); \
384 r = fsd->short_fops->name(args); \
385 debugfs_file_put(dentry); \
389 FULL_PROXY_FUNC_BOTH(llseek, loff_t, filp,
390 PROTO(struct file *filp, loff_t offset, int whence),
391 ARGS(filp, offset, whence), HAS_LSEEK, -ESPIPE);
393 FULL_PROXY_FUNC_BOTH(read, ssize_t, filp,
394 PROTO(struct file *filp, char __user *buf, size_t size,
396 ARGS(filp, buf, size, ppos), HAS_READ, -EINVAL);
398 FULL_PROXY_FUNC_BOTH(write, ssize_t, filp,
399 PROTO(struct file *filp, const char __user *buf,
400 size_t size, loff_t *ppos),
401 ARGS(filp, buf, size, ppos), HAS_WRITE, -EINVAL);
403 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
404 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
405 ARGS(filp, cmd, arg), HAS_IOCTL, -ENOTTY);
407 static __poll_t full_proxy_poll(struct file *filp,
408 struct poll_table_struct *wait)
410 struct dentry *dentry = F_DENTRY(filp);
411 struct debugfs_fsdata *fsd = dentry->d_fsdata;
413 const struct file_operations *real_fops;
415 if (!(fsd->methods & HAS_POLL))
416 return DEFAULT_POLLMASK;
417 if (debugfs_file_get(dentry))
420 real_fops = debugfs_real_fops(filp);
421 r = real_fops->poll(filp, wait);
422 debugfs_file_put(dentry);
426 static int full_proxy_release(struct inode *inode, struct file *filp)
428 const struct file_operations *real_fops = debugfs_real_fops(filp);
432 * We must not protect this against removal races here: the
433 * original releaser should be called unconditionally in order
434 * not to leak any resources. Releasers must not assume that
435 * ->i_private is still being meaningful here.
437 if (real_fops->release)
438 r = real_fops->release(inode, filp);
444 static int full_proxy_open_regular(struct inode *inode, struct file *filp)
446 struct dentry *dentry = F_DENTRY(filp);
447 const struct file_operations *real_fops;
448 struct debugfs_fsdata *fsd;
451 r = __debugfs_file_get(dentry, DBGFS_GET_REGULAR);
453 return r == -EIO ? -ENOENT : r;
455 fsd = dentry->d_fsdata;
456 real_fops = fsd->real_fops;
457 r = debugfs_locked_down(inode, filp, real_fops);
461 if (!fops_get(real_fops)) {
462 #ifdef CONFIG_MODULES
463 if (real_fops->owner &&
464 real_fops->owner->state == MODULE_STATE_GOING) {
470 /* Huh? Module did not cleanup after itself at exit? */
471 WARN(1, "debugfs file owner did not clean up at exit: %pd",
477 if (real_fops->open) {
478 r = real_fops->open(inode, filp);
481 } else if (filp->f_op != &debugfs_full_proxy_file_operations) {
482 /* No protection against file removal anymore. */
483 WARN(1, "debugfs file owner replaced proxy fops: %pd",
489 debugfs_file_put(dentry);
493 const struct file_operations debugfs_full_proxy_file_operations = {
494 .open = full_proxy_open_regular,
495 .release = full_proxy_release,
496 .llseek = full_proxy_llseek,
497 .read = full_proxy_read,
498 .write = full_proxy_write,
499 .poll = full_proxy_poll,
500 .unlocked_ioctl = full_proxy_unlocked_ioctl
503 static int full_proxy_open_short(struct inode *inode, struct file *filp)
505 struct dentry *dentry = F_DENTRY(filp);
508 r = __debugfs_file_get(dentry, DBGFS_GET_SHORT);
510 return r == -EIO ? -ENOENT : r;
511 r = debugfs_locked_down(inode, filp, NULL);
513 r = simple_open(inode, filp);
514 debugfs_file_put(dentry);
518 const struct file_operations debugfs_full_short_proxy_file_operations = {
519 .open = full_proxy_open_short,
520 .llseek = full_proxy_llseek,
521 .read = full_proxy_read,
522 .write = full_proxy_write,
525 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
526 size_t len, loff_t *ppos)
528 struct dentry *dentry = F_DENTRY(file);
531 ret = debugfs_file_get(dentry);
534 ret = simple_attr_read(file, buf, len, ppos);
535 debugfs_file_put(dentry);
538 EXPORT_SYMBOL_GPL(debugfs_attr_read);
540 static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
541 size_t len, loff_t *ppos, bool is_signed)
543 struct dentry *dentry = F_DENTRY(file);
546 ret = debugfs_file_get(dentry);
550 ret = simple_attr_write_signed(file, buf, len, ppos);
552 ret = simple_attr_write(file, buf, len, ppos);
553 debugfs_file_put(dentry);
557 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
558 size_t len, loff_t *ppos)
560 return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
562 EXPORT_SYMBOL_GPL(debugfs_attr_write);
564 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
565 size_t len, loff_t *ppos)
567 return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
569 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
571 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
572 struct dentry *parent, void *value,
573 const struct file_operations *fops,
574 const struct file_operations *fops_ro,
575 const struct file_operations *fops_wo)
577 /* if there are no write bits set, make read only */
578 if (!(mode & S_IWUGO))
579 return debugfs_create_file_unsafe(name, mode, parent, value,
581 /* if there are no read bits set, make write only */
582 if (!(mode & S_IRUGO))
583 return debugfs_create_file_unsafe(name, mode, parent, value,
586 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
589 static int debugfs_u8_set(void *data, u64 val)
594 static int debugfs_u8_get(void *data, u64 *val)
599 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
600 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
601 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
604 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
605 * @name: a pointer to a string containing the name of the file to create.
606 * @mode: the permission that the file should have
607 * @parent: a pointer to the parent dentry for this file. This should be a
608 * directory dentry if set. If this parameter is %NULL, then the
609 * file will be created in the root of the debugfs filesystem.
610 * @value: a pointer to the variable that the file should read to and write
613 * This function creates a file in debugfs with the given name that
614 * contains the value of the variable @value. If the @mode variable is so
615 * set, it can be read from, and written to.
617 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
620 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
621 &fops_u8_ro, &fops_u8_wo);
623 EXPORT_SYMBOL_GPL(debugfs_create_u8);
625 static int debugfs_u16_set(void *data, u64 val)
630 static int debugfs_u16_get(void *data, u64 *val)
635 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
636 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
637 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
640 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-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 * This function creates a file in debugfs with the given name that
650 * contains the value of the variable @value. If the @mode variable is so
651 * set, it can be read from, and written to.
653 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
656 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
657 &fops_u16_ro, &fops_u16_wo);
659 EXPORT_SYMBOL_GPL(debugfs_create_u16);
661 static int debugfs_u32_set(void *data, u64 val)
666 static int debugfs_u32_get(void *data, u64 *val)
671 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
672 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
673 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
676 * debugfs_create_u32 - 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 * This function creates a file in debugfs with the given name that
686 * contains the value of the variable @value. If the @mode variable is so
687 * set, it can be read from, and written to.
689 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
692 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
693 &fops_u32_ro, &fops_u32_wo);
695 EXPORT_SYMBOL_GPL(debugfs_create_u32);
697 static int debugfs_u64_set(void *data, u64 val)
703 static int debugfs_u64_get(void *data, u64 *val)
708 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
709 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
710 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
713 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
714 * @name: a pointer to a string containing the name of the file to create.
715 * @mode: the permission that the file should have
716 * @parent: a pointer to the parent dentry for this file. This should be a
717 * directory dentry if set. If this parameter is %NULL, then the
718 * file will be created in the root of the debugfs filesystem.
719 * @value: a pointer to the variable that the file should read to and write
722 * This function creates a file in debugfs with the given name that
723 * contains the value of the variable @value. If the @mode variable is so
724 * set, it can be read from, and written to.
726 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
729 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
730 &fops_u64_ro, &fops_u64_wo);
732 EXPORT_SYMBOL_GPL(debugfs_create_u64);
734 static int debugfs_ulong_set(void *data, u64 val)
736 *(unsigned long *)data = val;
740 static int debugfs_ulong_get(void *data, u64 *val)
742 *val = *(unsigned long *)data;
745 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
747 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
748 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
751 * debugfs_create_ulong - create a debugfs file that is used to read and write
752 * an unsigned long value.
753 * @name: a pointer to a string containing the name of the file to create.
754 * @mode: the permission that the file should have
755 * @parent: a pointer to the parent dentry for this file. This should be a
756 * directory dentry if set. If this parameter is %NULL, then the
757 * file will be created in the root of the debugfs filesystem.
758 * @value: a pointer to the variable that the file should read to and write
761 * This function creates a file in debugfs with the given name that
762 * contains the value of the variable @value. If the @mode variable is so
763 * set, it can be read from, and written to.
765 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
766 unsigned long *value)
768 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
769 &fops_ulong_ro, &fops_ulong_wo);
771 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
773 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
774 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
775 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
777 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
779 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
780 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
782 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
784 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
785 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
787 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
789 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
790 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
793 * 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
795 * These functions are exactly the same as the above functions (but use a hex
796 * output for the decimal challenged). For details look at the above unsigned
801 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
802 * @name: a pointer to a string containing the name of the file to create.
803 * @mode: the permission that the file should have
804 * @parent: a pointer to the parent dentry for this file. This should be a
805 * directory dentry if set. If this parameter is %NULL, then the
806 * file will be created in the root of the debugfs filesystem.
807 * @value: a pointer to the variable that the file should read to and write
810 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
813 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
814 &fops_x8_ro, &fops_x8_wo);
816 EXPORT_SYMBOL_GPL(debugfs_create_x8);
819 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
820 * @name: a pointer to a string containing the name of the file to create.
821 * @mode: the permission that the file should have
822 * @parent: a pointer to the parent dentry for this file. This should be a
823 * directory dentry if set. If this parameter is %NULL, then the
824 * file will be created in the root of the debugfs filesystem.
825 * @value: a pointer to the variable that the file should read to and write
828 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
831 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
832 &fops_x16_ro, &fops_x16_wo);
834 EXPORT_SYMBOL_GPL(debugfs_create_x16);
837 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
838 * @name: a pointer to a string containing the name of the file to create.
839 * @mode: the permission that the file should have
840 * @parent: a pointer to the parent dentry for this file. This should be a
841 * directory dentry if set. If this parameter is %NULL, then the
842 * file will be created in the root of the debugfs filesystem.
843 * @value: a pointer to the variable that the file should read to and write
846 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
849 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
850 &fops_x32_ro, &fops_x32_wo);
852 EXPORT_SYMBOL_GPL(debugfs_create_x32);
855 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
856 * @name: a pointer to a string containing the name of the file to create.
857 * @mode: the permission that the file should have
858 * @parent: a pointer to the parent dentry for this file. This should be a
859 * directory dentry if set. If this parameter is %NULL, then the
860 * file will be created in the root of the debugfs filesystem.
861 * @value: a pointer to the variable that the file should read to and write
864 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
867 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
868 &fops_x64_ro, &fops_x64_wo);
870 EXPORT_SYMBOL_GPL(debugfs_create_x64);
873 static int debugfs_size_t_set(void *data, u64 val)
875 *(size_t *)data = val;
878 static int debugfs_size_t_get(void *data, u64 *val)
880 *val = *(size_t *)data;
883 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
884 "%llu\n"); /* %llu and %zu are more or less the same */
885 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
886 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
889 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
890 * @name: a pointer to a string containing the name of the file to create.
891 * @mode: the permission that the file should have
892 * @parent: a pointer to the parent dentry for this file. This should be a
893 * directory dentry if set. If this parameter is %NULL, then the
894 * file will be created in the root of the debugfs filesystem.
895 * @value: a pointer to the variable that the file should read to and write
898 void debugfs_create_size_t(const char *name, umode_t mode,
899 struct dentry *parent, size_t *value)
901 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
902 &fops_size_t_ro, &fops_size_t_wo);
904 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
906 static int debugfs_atomic_t_set(void *data, u64 val)
908 atomic_set((atomic_t *)data, val);
911 static int debugfs_atomic_t_get(void *data, u64 *val)
913 *val = atomic_read((atomic_t *)data);
916 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
917 debugfs_atomic_t_set, "%lld\n");
918 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
920 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
924 * debugfs_create_atomic_t - create a debugfs file that is used to read and
925 * write an atomic_t value
926 * @name: a pointer to a string containing the name of the file to create.
927 * @mode: the permission that the file should have
928 * @parent: a pointer to the parent dentry for this file. This should be a
929 * directory dentry if set. If this parameter is %NULL, then the
930 * file will be created in the root of the debugfs filesystem.
931 * @value: a pointer to the variable that the file should read to and write
934 void debugfs_create_atomic_t(const char *name, umode_t mode,
935 struct dentry *parent, atomic_t *value)
937 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
938 &fops_atomic_t_ro, &fops_atomic_t_wo);
940 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
942 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
943 size_t count, loff_t *ppos)
948 struct dentry *dentry = F_DENTRY(file);
950 r = debugfs_file_get(dentry);
953 val = *(bool *)file->private_data;
954 debugfs_file_put(dentry);
961 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
963 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
965 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
966 size_t count, loff_t *ppos)
970 bool *val = file->private_data;
971 struct dentry *dentry = F_DENTRY(file);
973 r = kstrtobool_from_user(user_buf, count, &bv);
975 r = debugfs_file_get(dentry);
979 debugfs_file_put(dentry);
984 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
986 static const struct file_operations fops_bool = {
987 .read = debugfs_read_file_bool,
988 .write = debugfs_write_file_bool,
990 .llseek = default_llseek,
993 static const struct file_operations fops_bool_ro = {
994 .read = debugfs_read_file_bool,
996 .llseek = default_llseek,
999 static const struct file_operations fops_bool_wo = {
1000 .write = debugfs_write_file_bool,
1001 .open = simple_open,
1002 .llseek = default_llseek,
1006 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
1007 * @name: a pointer to a string containing the name of the file to create.
1008 * @mode: the permission that the file should have
1009 * @parent: a pointer to the parent dentry for this file. This should be a
1010 * directory dentry if set. If this parameter is %NULL, then the
1011 * file will be created in the root of the debugfs filesystem.
1012 * @value: a pointer to the variable that the file should read to and write
1015 * This function creates a file in debugfs with the given name that
1016 * contains the value of the variable @value. If the @mode variable is so
1017 * set, it can be read from, and written to.
1019 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
1022 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
1023 &fops_bool_ro, &fops_bool_wo);
1025 EXPORT_SYMBOL_GPL(debugfs_create_bool);
1027 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
1028 size_t count, loff_t *ppos)
1030 struct dentry *dentry = F_DENTRY(file);
1031 char *str, *copy = NULL;
1035 ret = debugfs_file_get(dentry);
1039 str = *(char **)file->private_data;
1040 len = strlen(str) + 1;
1041 copy = kmalloc(len, GFP_KERNEL);
1043 debugfs_file_put(dentry);
1047 copy_len = strscpy(copy, str, len);
1048 debugfs_file_put(dentry);
1054 copy[copy_len] = '\n';
1056 ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
1061 EXPORT_SYMBOL_GPL(debugfs_create_str);
1063 static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
1064 size_t count, loff_t *ppos)
1066 struct dentry *dentry = F_DENTRY(file);
1067 char *old, *new = NULL;
1071 r = debugfs_file_get(dentry);
1075 old = *(char **)file->private_data;
1077 /* only allow strict concatenation */
1079 if (pos && pos != strlen(old))
1083 if (pos + count + 1 > PAGE_SIZE)
1087 new = kmalloc(pos + count + 1, GFP_KERNEL);
1092 memcpy(new, old, pos);
1095 if (copy_from_user(new + pos, user_buf, count))
1098 new[pos + count] = '\0';
1101 rcu_assign_pointer(*(char __rcu **)file->private_data, new);
1105 debugfs_file_put(dentry);
1110 debugfs_file_put(dentry);
1114 static const struct file_operations fops_str = {
1115 .read = debugfs_read_file_str,
1116 .write = debugfs_write_file_str,
1117 .open = simple_open,
1118 .llseek = default_llseek,
1121 static const struct file_operations fops_str_ro = {
1122 .read = debugfs_read_file_str,
1123 .open = simple_open,
1124 .llseek = default_llseek,
1127 static const struct file_operations fops_str_wo = {
1128 .write = debugfs_write_file_str,
1129 .open = simple_open,
1130 .llseek = default_llseek,
1134 * debugfs_create_str - create a debugfs file that is used to read and write a string value
1135 * @name: a pointer to a string containing the name of the file to create.
1136 * @mode: the permission that the file should have
1137 * @parent: a pointer to the parent dentry for this file. This should be a
1138 * directory dentry if set. If this parameter is %NULL, then the
1139 * file will be created in the root of the debugfs filesystem.
1140 * @value: a pointer to the variable that the file should read to and write
1143 * This function creates a file in debugfs with the given name that
1144 * contains the value of the variable @value. If the @mode variable is so
1145 * set, it can be read from, and written to.
1147 void debugfs_create_str(const char *name, umode_t mode,
1148 struct dentry *parent, char **value)
1150 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
1151 &fops_str_ro, &fops_str_wo);
1154 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
1155 size_t count, loff_t *ppos)
1157 struct debugfs_blob_wrapper *blob = file->private_data;
1158 struct dentry *dentry = F_DENTRY(file);
1161 r = debugfs_file_get(dentry);
1164 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
1166 debugfs_file_put(dentry);
1170 static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
1171 size_t count, loff_t *ppos)
1173 struct debugfs_blob_wrapper *blob = file->private_data;
1174 struct dentry *dentry = F_DENTRY(file);
1177 r = debugfs_file_get(dentry);
1180 r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
1183 debugfs_file_put(dentry);
1187 static const struct file_operations fops_blob = {
1188 .read = read_file_blob,
1189 .write = write_file_blob,
1190 .open = simple_open,
1191 .llseek = default_llseek,
1195 * debugfs_create_blob - create a debugfs file that is used to read and write
1197 * @name: a pointer to a string containing the name of the file to create.
1198 * @mode: the permission that the file should have
1199 * @parent: a pointer to the parent dentry for this file. This should be a
1200 * directory dentry if set. If this parameter is %NULL, then the
1201 * file will be created in the root of the debugfs filesystem.
1202 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
1203 * to the blob data and the size of the data.
1205 * This function creates a file in debugfs with the given name that exports
1206 * @blob->data as a binary blob. If the @mode variable is so set it can be
1207 * read from and written to.
1209 * This function will return a pointer to a dentry if it succeeds. This
1210 * pointer must be passed to the debugfs_remove() function when the file is
1211 * to be removed (no automatic cleanup happens if your module is unloaded,
1212 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
1215 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
1218 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1219 struct dentry *parent,
1220 struct debugfs_blob_wrapper *blob)
1222 return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
1224 EXPORT_SYMBOL_GPL(debugfs_create_blob);
1226 static size_t u32_format_array(char *buf, size_t bufsize,
1227 u32 *array, int array_size)
1231 while (--array_size >= 0) {
1233 char term = array_size ? ' ' : '\n';
1235 len = snprintf(buf, bufsize, "%u%c", *array++, term);
1244 static int u32_array_open(struct inode *inode, struct file *file)
1246 struct debugfs_u32_array *data = inode->i_private;
1247 int size, elements = data->n_elements;
1252 * - 10 digits + ' '/'\n' = 11 bytes per number
1253 * - terminating NUL character
1256 buf = kmalloc(size+1, GFP_KERNEL);
1261 file->private_data = buf;
1262 u32_format_array(buf, size, data->array, data->n_elements);
1264 return nonseekable_open(inode, file);
1267 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
1270 size_t size = strlen(file->private_data);
1272 return simple_read_from_buffer(buf, len, ppos,
1273 file->private_data, size);
1276 static int u32_array_release(struct inode *inode, struct file *file)
1278 kfree(file->private_data);
1283 static const struct file_operations u32_array_fops = {
1284 .owner = THIS_MODULE,
1285 .open = u32_array_open,
1286 .release = u32_array_release,
1287 .read = u32_array_read,
1291 * debugfs_create_u32_array - create a debugfs file that is used to read u32
1293 * @name: a pointer to a string containing the name of the file to create.
1294 * @mode: the permission that the file should have.
1295 * @parent: a pointer to the parent dentry for this file. This should be a
1296 * directory dentry if set. If this parameter is %NULL, then the
1297 * file will be created in the root of the debugfs filesystem.
1298 * @array: wrapper struct containing data pointer and size of the array.
1300 * This function creates a file in debugfs with the given name that exports
1301 * @array as data. If the @mode variable is so set it can be read from.
1302 * Writing is not supported. Seek within the file is also not supported.
1303 * Once array is created its size can not be changed.
1305 void debugfs_create_u32_array(const char *name, umode_t mode,
1306 struct dentry *parent,
1307 struct debugfs_u32_array *array)
1309 debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
1311 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1313 #ifdef CONFIG_HAS_IOMEM
1316 * The regset32 stuff is used to print 32-bit registers using the
1317 * seq_file utilities. We offer printing a register set in an already-opened
1318 * sequential file or create a debugfs file that only prints a regset32.
1322 * debugfs_print_regs32 - use seq_print to describe a set of registers
1323 * @s: the seq_file structure being used to generate output
1324 * @regs: an array if struct debugfs_reg32 structures
1325 * @nregs: the length of the above array
1326 * @base: the base address to be used in reading the registers
1327 * @prefix: a string to be prefixed to every output line
1329 * This function outputs a text block describing the current values of
1330 * some 32-bit hardware registers. It is meant to be used within debugfs
1331 * files based on seq_file that need to show registers, intermixed with other
1332 * information. The prefix argument may be used to specify a leading string,
1333 * because some peripherals have several blocks of identical registers,
1334 * for example configuration of dma channels
1336 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1337 int nregs, void __iomem *base, char *prefix)
1341 for (i = 0; i < nregs; i++, regs++) {
1343 seq_printf(s, "%s", prefix);
1344 seq_printf(s, "%s = 0x%08x\n", regs->name,
1345 readl(base + regs->offset));
1346 if (seq_has_overflowed(s))
1350 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1352 static int debugfs_regset32_show(struct seq_file *s, void *data)
1354 struct debugfs_regset32 *regset = s->private;
1357 pm_runtime_get_sync(regset->dev);
1359 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1362 pm_runtime_put(regset->dev);
1367 DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
1370 * debugfs_create_regset32 - create a debugfs file that returns register values
1371 * @name: a pointer to a string containing the name of the file to create.
1372 * @mode: the permission that the file should have
1373 * @parent: a pointer to the parent dentry for this file. This should be a
1374 * directory dentry if set. If this parameter is %NULL, then the
1375 * file will be created in the root of the debugfs filesystem.
1376 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1377 * to an array of register definitions, the array size and the base
1378 * address where the register bank is to be found.
1380 * This function creates a file in debugfs with the given name that reports
1381 * the names and values of a set of 32-bit registers. If the @mode variable
1382 * is so set it can be read from. Writing is not supported.
1384 void debugfs_create_regset32(const char *name, umode_t mode,
1385 struct dentry *parent,
1386 struct debugfs_regset32 *regset)
1388 debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
1390 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1392 #endif /* CONFIG_HAS_IOMEM */
1394 struct debugfs_devm_entry {
1395 int (*read)(struct seq_file *seq, void *data);
1399 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1401 struct debugfs_devm_entry *entry = inode->i_private;
1403 return single_open(f, entry->read, entry->dev);
1406 static const struct file_operations debugfs_devm_entry_ops = {
1407 .owner = THIS_MODULE,
1408 .open = debugfs_devm_entry_open,
1409 .release = single_release,
1415 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1417 * @dev: device related to this debugfs file.
1418 * @name: name of the debugfs file.
1419 * @parent: a pointer to the parent dentry for this file. This should be a
1420 * directory dentry if set. If this parameter is %NULL, then the
1421 * file will be created in the root of the debugfs filesystem.
1422 * @read_fn: function pointer called to print the seq_file content.
1424 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
1425 struct dentry *parent,
1426 int (*read_fn)(struct seq_file *s, void *data))
1428 struct debugfs_devm_entry *entry;
1433 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1437 entry->read = read_fn;
1440 debugfs_create_file(name, S_IRUGO, parent, entry,
1441 &debugfs_devm_entry_ops);
1443 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);