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 struct file_operations *debugfs_real_fops(const struct file *filp)
52 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
54 if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
56 * Urgh, we've been called w/o a protecting
63 return fsd->real_fops;
65 EXPORT_SYMBOL_GPL(debugfs_real_fops);
68 * debugfs_file_get - mark the beginning of file data access
69 * @dentry: the dentry object whose data is being accessed.
71 * Up to a matching call to debugfs_file_put(), any successive call
72 * into the file removing functions debugfs_remove() and
73 * debugfs_remove_recursive() will block. Since associated private
74 * file data may only get freed after a successful return of any of
75 * the removal functions, you may safely access it after a successful
76 * call to debugfs_file_get() without worrying about lifetime issues.
78 * If -%EIO is returned, the file has already been removed and thus,
79 * it is not safe to access any of its data. If, on the other hand,
80 * it is allowed to access the file data, zero is returned.
82 int debugfs_file_get(struct dentry *dentry)
84 struct debugfs_fsdata *fsd;
88 * This could only happen if some debugfs user erroneously calls
89 * debugfs_file_get() on a dentry that isn't even a file, let
92 if (WARN_ON(!d_is_reg(dentry)))
95 d_fsd = READ_ONCE(dentry->d_fsdata);
96 if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
99 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
103 fsd->real_fops = (void *)((unsigned long)d_fsd &
104 ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
105 refcount_set(&fsd->active_users, 1);
106 init_completion(&fsd->active_users_drained);
107 if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
109 fsd = READ_ONCE(dentry->d_fsdata);
114 * In case of a successful cmpxchg() above, this check is
115 * strictly necessary and must follow it, see the comment in
116 * __debugfs_remove_file().
117 * OTOH, if the cmpxchg() hasn't been executed or wasn't
118 * successful, this serves the purpose of not starving
121 if (d_unlinked(dentry))
124 if (!refcount_inc_not_zero(&fsd->active_users))
129 EXPORT_SYMBOL_GPL(debugfs_file_get);
132 * debugfs_file_put - mark the end of file data access
133 * @dentry: the dentry object formerly passed to
134 * debugfs_file_get().
136 * Allow any ongoing concurrent call into debugfs_remove() or
137 * debugfs_remove_recursive() blocked by a former call to
138 * debugfs_file_get() to proceed and return to its caller.
140 void debugfs_file_put(struct dentry *dentry)
142 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
144 if (refcount_dec_and_test(&fsd->active_users))
145 complete(&fsd->active_users_drained);
147 EXPORT_SYMBOL_GPL(debugfs_file_put);
150 * Only permit access to world-readable files when the kernel is locked down.
151 * We also need to exclude any file that has ways to write or alter it as root
152 * can bypass the permissions check.
154 static int debugfs_locked_down(struct inode *inode,
156 const struct file_operations *real_fops)
158 if ((inode->i_mode & 07777 & ~0444) == 0 &&
159 !(filp->f_mode & FMODE_WRITE) &&
160 !real_fops->unlocked_ioctl &&
161 !real_fops->compat_ioctl &&
165 if (security_locked_down(LOCKDOWN_DEBUGFS))
171 static int open_proxy_open(struct inode *inode, struct file *filp)
173 struct dentry *dentry = F_DENTRY(filp);
174 const struct file_operations *real_fops = NULL;
177 r = debugfs_file_get(dentry);
179 return r == -EIO ? -ENOENT : r;
181 real_fops = debugfs_real_fops(filp);
183 r = debugfs_locked_down(inode, filp, real_fops);
187 if (!fops_get(real_fops)) {
188 #ifdef CONFIG_MODULES
189 if (real_fops->owner &&
190 real_fops->owner->state == MODULE_STATE_GOING) {
196 /* Huh? Module did not clean up after itself at exit? */
197 WARN(1, "debugfs file owner did not clean up at exit: %pd",
202 replace_fops(filp, real_fops);
205 r = real_fops->open(inode, filp);
208 debugfs_file_put(dentry);
212 const struct file_operations debugfs_open_proxy_file_operations = {
213 .open = open_proxy_open,
216 #define PROTO(args...) args
217 #define ARGS(args...) args
219 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
220 static ret_type full_proxy_ ## name(proto) \
222 struct dentry *dentry = F_DENTRY(filp); \
223 const struct file_operations *real_fops; \
226 r = debugfs_file_get(dentry); \
229 real_fops = debugfs_real_fops(filp); \
230 r = real_fops->name(args); \
231 debugfs_file_put(dentry); \
235 FULL_PROXY_FUNC(llseek, loff_t, filp,
236 PROTO(struct file *filp, loff_t offset, int whence),
237 ARGS(filp, offset, whence));
239 FULL_PROXY_FUNC(read, ssize_t, filp,
240 PROTO(struct file *filp, char __user *buf, size_t size,
242 ARGS(filp, buf, size, ppos));
244 FULL_PROXY_FUNC(write, ssize_t, filp,
245 PROTO(struct file *filp, const char __user *buf, size_t size,
247 ARGS(filp, buf, size, ppos));
249 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
250 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
251 ARGS(filp, cmd, arg));
253 static __poll_t full_proxy_poll(struct file *filp,
254 struct poll_table_struct *wait)
256 struct dentry *dentry = F_DENTRY(filp);
258 const struct file_operations *real_fops;
260 if (debugfs_file_get(dentry))
263 real_fops = debugfs_real_fops(filp);
264 r = real_fops->poll(filp, wait);
265 debugfs_file_put(dentry);
269 static int full_proxy_release(struct inode *inode, struct file *filp)
271 const struct dentry *dentry = F_DENTRY(filp);
272 const struct file_operations *real_fops = debugfs_real_fops(filp);
273 const struct file_operations *proxy_fops = filp->f_op;
277 * We must not protect this against removal races here: the
278 * original releaser should be called unconditionally in order
279 * not to leak any resources. Releasers must not assume that
280 * ->i_private is still being meaningful here.
282 if (real_fops->release)
283 r = real_fops->release(inode, filp);
285 replace_fops(filp, d_inode(dentry)->i_fop);
291 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
292 const struct file_operations *real_fops)
294 proxy_fops->release = full_proxy_release;
295 if (real_fops->llseek)
296 proxy_fops->llseek = full_proxy_llseek;
298 proxy_fops->read = full_proxy_read;
299 if (real_fops->write)
300 proxy_fops->write = full_proxy_write;
302 proxy_fops->poll = full_proxy_poll;
303 if (real_fops->unlocked_ioctl)
304 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
307 static int full_proxy_open(struct inode *inode, struct file *filp)
309 struct dentry *dentry = F_DENTRY(filp);
310 const struct file_operations *real_fops = NULL;
311 struct file_operations *proxy_fops = NULL;
314 r = debugfs_file_get(dentry);
316 return r == -EIO ? -ENOENT : r;
318 real_fops = debugfs_real_fops(filp);
320 r = debugfs_locked_down(inode, filp, real_fops);
324 if (!fops_get(real_fops)) {
325 #ifdef CONFIG_MODULES
326 if (real_fops->owner &&
327 real_fops->owner->state == MODULE_STATE_GOING) {
333 /* Huh? Module did not cleanup after itself at exit? */
334 WARN(1, "debugfs file owner did not clean up at exit: %pd",
340 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
345 __full_proxy_fops_init(proxy_fops, real_fops);
346 replace_fops(filp, proxy_fops);
348 if (real_fops->open) {
349 r = real_fops->open(inode, filp);
351 replace_fops(filp, d_inode(dentry)->i_fop);
353 } else if (filp->f_op != proxy_fops) {
354 /* No protection against file removal anymore. */
355 WARN(1, "debugfs file owner replaced proxy fops: %pd",
366 debugfs_file_put(dentry);
370 const struct file_operations debugfs_full_proxy_file_operations = {
371 .open = full_proxy_open,
374 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
375 size_t len, loff_t *ppos)
377 struct dentry *dentry = F_DENTRY(file);
380 ret = debugfs_file_get(dentry);
383 ret = simple_attr_read(file, buf, len, ppos);
384 debugfs_file_put(dentry);
387 EXPORT_SYMBOL_GPL(debugfs_attr_read);
389 static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
390 size_t len, loff_t *ppos, bool is_signed)
392 struct dentry *dentry = F_DENTRY(file);
395 ret = debugfs_file_get(dentry);
399 ret = simple_attr_write_signed(file, buf, len, ppos);
401 ret = simple_attr_write(file, buf, len, ppos);
402 debugfs_file_put(dentry);
406 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
407 size_t len, loff_t *ppos)
409 return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
411 EXPORT_SYMBOL_GPL(debugfs_attr_write);
413 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
414 size_t len, loff_t *ppos)
416 return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
418 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
420 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
421 struct dentry *parent, void *value,
422 const struct file_operations *fops,
423 const struct file_operations *fops_ro,
424 const struct file_operations *fops_wo)
426 /* if there are no write bits set, make read only */
427 if (!(mode & S_IWUGO))
428 return debugfs_create_file_unsafe(name, mode, parent, value,
430 /* if there are no read bits set, make write only */
431 if (!(mode & S_IRUGO))
432 return debugfs_create_file_unsafe(name, mode, parent, value,
435 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
438 static int debugfs_u8_set(void *data, u64 val)
443 static int debugfs_u8_get(void *data, u64 *val)
448 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
449 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
450 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
453 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
454 * @name: a pointer to a string containing the name of the file to create.
455 * @mode: the permission that the file should have
456 * @parent: a pointer to the parent dentry for this file. This should be a
457 * directory dentry if set. If this parameter is %NULL, then the
458 * file will be created in the root of the debugfs filesystem.
459 * @value: a pointer to the variable that the file should read to and write
462 * This function creates a file in debugfs with the given name that
463 * contains the value of the variable @value. If the @mode variable is so
464 * set, it can be read from, and written to.
466 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
469 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
470 &fops_u8_ro, &fops_u8_wo);
472 EXPORT_SYMBOL_GPL(debugfs_create_u8);
474 static int debugfs_u16_set(void *data, u64 val)
479 static int debugfs_u16_get(void *data, u64 *val)
484 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
485 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
486 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
489 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
490 * @name: a pointer to a string containing the name of the file to create.
491 * @mode: the permission that the file should have
492 * @parent: a pointer to the parent dentry for this file. This should be a
493 * directory dentry if set. If this parameter is %NULL, then the
494 * file will be created in the root of the debugfs filesystem.
495 * @value: a pointer to the variable that the file should read to and write
498 * This function creates a file in debugfs with the given name that
499 * contains the value of the variable @value. If the @mode variable is so
500 * set, it can be read from, and written to.
502 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
505 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
506 &fops_u16_ro, &fops_u16_wo);
508 EXPORT_SYMBOL_GPL(debugfs_create_u16);
510 static int debugfs_u32_set(void *data, u64 val)
515 static int debugfs_u32_get(void *data, u64 *val)
520 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
521 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
522 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
525 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
526 * @name: a pointer to a string containing the name of the file to create.
527 * @mode: the permission that the file should have
528 * @parent: a pointer to the parent dentry for this file. This should be a
529 * directory dentry if set. If this parameter is %NULL, then the
530 * file will be created in the root of the debugfs filesystem.
531 * @value: a pointer to the variable that the file should read to and write
534 * This function creates a file in debugfs with the given name that
535 * contains the value of the variable @value. If the @mode variable is so
536 * set, it can be read from, and written to.
538 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
541 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
542 &fops_u32_ro, &fops_u32_wo);
544 EXPORT_SYMBOL_GPL(debugfs_create_u32);
546 static int debugfs_u64_set(void *data, u64 val)
552 static int debugfs_u64_get(void *data, u64 *val)
557 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
558 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
559 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
562 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
563 * @name: a pointer to a string containing the name of the file to create.
564 * @mode: the permission that the file should have
565 * @parent: a pointer to the parent dentry for this file. This should be a
566 * directory dentry if set. If this parameter is %NULL, then the
567 * file will be created in the root of the debugfs filesystem.
568 * @value: a pointer to the variable that the file should read to and write
571 * This function creates a file in debugfs with the given name that
572 * contains the value of the variable @value. If the @mode variable is so
573 * set, it can be read from, and written to.
575 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
578 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
579 &fops_u64_ro, &fops_u64_wo);
581 EXPORT_SYMBOL_GPL(debugfs_create_u64);
583 static int debugfs_ulong_set(void *data, u64 val)
585 *(unsigned long *)data = val;
589 static int debugfs_ulong_get(void *data, u64 *val)
591 *val = *(unsigned long *)data;
594 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
596 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
597 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
600 * debugfs_create_ulong - create a debugfs file that is used to read and write
601 * an unsigned long value.
602 * @name: a pointer to a string containing the name of the file to create.
603 * @mode: the permission that the file should have
604 * @parent: a pointer to the parent dentry for this file. This should be a
605 * directory dentry if set. If this parameter is %NULL, then the
606 * file will be created in the root of the debugfs filesystem.
607 * @value: a pointer to the variable that the file should read to and write
610 * This function creates a file in debugfs with the given name that
611 * contains the value of the variable @value. If the @mode variable is so
612 * set, it can be read from, and written to.
614 * This function will return a pointer to a dentry if it succeeds. This
615 * pointer must be passed to the debugfs_remove() function when the file is
616 * to be removed (no automatic cleanup happens if your module is unloaded,
617 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
620 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
623 struct dentry *debugfs_create_ulong(const char *name, umode_t mode,
624 struct dentry *parent, unsigned long *value)
626 return debugfs_create_mode_unsafe(name, mode, parent, value,
627 &fops_ulong, &fops_ulong_ro,
630 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
632 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
633 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
634 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
636 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
638 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
639 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
641 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
643 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
644 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
646 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
648 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
649 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
652 * 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
654 * These functions are exactly the same as the above functions (but use a hex
655 * output for the decimal challenged). For details look at the above unsigned
660 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
661 * @name: a pointer to a string containing the name of the file to create.
662 * @mode: the permission that the file should have
663 * @parent: a pointer to the parent dentry for this file. This should be a
664 * directory dentry if set. If this parameter is %NULL, then the
665 * file will be created in the root of the debugfs filesystem.
666 * @value: a pointer to the variable that the file should read to and write
669 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
672 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
673 &fops_x8_ro, &fops_x8_wo);
675 EXPORT_SYMBOL_GPL(debugfs_create_x8);
678 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
679 * @name: a pointer to a string containing the name of the file to create.
680 * @mode: the permission that the file should have
681 * @parent: a pointer to the parent dentry for this file. This should be a
682 * directory dentry if set. If this parameter is %NULL, then the
683 * file will be created in the root of the debugfs filesystem.
684 * @value: a pointer to the variable that the file should read to and write
687 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
690 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
691 &fops_x16_ro, &fops_x16_wo);
693 EXPORT_SYMBOL_GPL(debugfs_create_x16);
696 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
697 * @name: a pointer to a string containing the name of the file to create.
698 * @mode: the permission that the file should have
699 * @parent: a pointer to the parent dentry for this file. This should be a
700 * directory dentry if set. If this parameter is %NULL, then the
701 * file will be created in the root of the debugfs filesystem.
702 * @value: a pointer to the variable that the file should read to and write
705 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
708 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
709 &fops_x32_ro, &fops_x32_wo);
711 EXPORT_SYMBOL_GPL(debugfs_create_x32);
714 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
715 * @name: a pointer to a string containing the name of the file to create.
716 * @mode: the permission that the file should have
717 * @parent: a pointer to the parent dentry for this file. This should be a
718 * directory dentry if set. If this parameter is %NULL, then the
719 * file will be created in the root of the debugfs filesystem.
720 * @value: a pointer to the variable that the file should read to and write
723 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
726 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
727 &fops_x64_ro, &fops_x64_wo);
729 EXPORT_SYMBOL_GPL(debugfs_create_x64);
732 static int debugfs_size_t_set(void *data, u64 val)
734 *(size_t *)data = val;
737 static int debugfs_size_t_get(void *data, u64 *val)
739 *val = *(size_t *)data;
742 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
743 "%llu\n"); /* %llu and %zu are more or less the same */
744 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
745 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
748 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
749 * @name: a pointer to a string containing the name of the file to create.
750 * @mode: the permission that the file should have
751 * @parent: a pointer to the parent dentry for this file. This should be a
752 * directory dentry if set. If this parameter is %NULL, then the
753 * file will be created in the root of the debugfs filesystem.
754 * @value: a pointer to the variable that the file should read to and write
757 void debugfs_create_size_t(const char *name, umode_t mode,
758 struct dentry *parent, size_t *value)
760 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
761 &fops_size_t_ro, &fops_size_t_wo);
763 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
765 static int debugfs_atomic_t_set(void *data, u64 val)
767 atomic_set((atomic_t *)data, val);
770 static int debugfs_atomic_t_get(void *data, u64 *val)
772 *val = atomic_read((atomic_t *)data);
775 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
776 debugfs_atomic_t_set, "%lld\n");
777 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
779 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
783 * debugfs_create_atomic_t - create a debugfs file that is used to read and
784 * write an atomic_t value
785 * @name: a pointer to a string containing the name of the file to create.
786 * @mode: the permission that the file should have
787 * @parent: a pointer to the parent dentry for this file. This should be a
788 * directory dentry if set. If this parameter is %NULL, then the
789 * file will be created in the root of the debugfs filesystem.
790 * @value: a pointer to the variable that the file should read to and write
793 void debugfs_create_atomic_t(const char *name, umode_t mode,
794 struct dentry *parent, atomic_t *value)
796 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
797 &fops_atomic_t_ro, &fops_atomic_t_wo);
799 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
801 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
802 size_t count, loff_t *ppos)
807 struct dentry *dentry = F_DENTRY(file);
809 r = debugfs_file_get(dentry);
812 val = *(bool *)file->private_data;
813 debugfs_file_put(dentry);
821 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
823 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
825 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
826 size_t count, loff_t *ppos)
830 bool *val = file->private_data;
831 struct dentry *dentry = F_DENTRY(file);
833 r = kstrtobool_from_user(user_buf, count, &bv);
835 r = debugfs_file_get(dentry);
839 debugfs_file_put(dentry);
844 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
846 static const struct file_operations fops_bool = {
847 .read = debugfs_read_file_bool,
848 .write = debugfs_write_file_bool,
850 .llseek = default_llseek,
853 static const struct file_operations fops_bool_ro = {
854 .read = debugfs_read_file_bool,
856 .llseek = default_llseek,
859 static const struct file_operations fops_bool_wo = {
860 .write = debugfs_write_file_bool,
862 .llseek = default_llseek,
866 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
867 * @name: a pointer to a string containing the name of the file to create.
868 * @mode: the permission that the file should have
869 * @parent: a pointer to the parent dentry for this file. This should be a
870 * directory dentry if set. If this parameter is %NULL, then the
871 * file will be created in the root of the debugfs filesystem.
872 * @value: a pointer to the variable that the file should read to and write
875 * This function creates a file in debugfs with the given name that
876 * contains the value of the variable @value. If the @mode variable is so
877 * set, it can be read from, and written to.
879 * This function will return a pointer to a dentry if it succeeds. This
880 * pointer must be passed to the debugfs_remove() function when the file is
881 * to be removed (no automatic cleanup happens if your module is unloaded,
882 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
885 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
888 struct dentry *debugfs_create_bool(const char *name, umode_t mode,
889 struct dentry *parent, bool *value)
891 return debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
892 &fops_bool_ro, &fops_bool_wo);
894 EXPORT_SYMBOL_GPL(debugfs_create_bool);
896 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
897 size_t count, loff_t *ppos)
899 struct debugfs_blob_wrapper *blob = file->private_data;
900 struct dentry *dentry = F_DENTRY(file);
903 r = debugfs_file_get(dentry);
906 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
908 debugfs_file_put(dentry);
912 static const struct file_operations fops_blob = {
913 .read = read_file_blob,
915 .llseek = default_llseek,
919 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
920 * @name: a pointer to a string containing the name of the file to create.
921 * @mode: the permission that the file should have
922 * @parent: a pointer to the parent dentry for this file. This should be a
923 * directory dentry if set. If this parameter is %NULL, then the
924 * file will be created in the root of the debugfs filesystem.
925 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
926 * to the blob data and the size of the data.
928 * This function creates a file in debugfs with the given name that exports
929 * @blob->data as a binary blob. If the @mode variable is so set it can be
930 * read from. Writing is not supported.
932 * This function will return a pointer to a dentry if it succeeds. This
933 * pointer must be passed to the debugfs_remove() function when the file is
934 * to be removed (no automatic cleanup happens if your module is unloaded,
935 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
938 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
941 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
942 struct dentry *parent,
943 struct debugfs_blob_wrapper *blob)
945 return debugfs_create_file_unsafe(name, mode, parent, blob, &fops_blob);
947 EXPORT_SYMBOL_GPL(debugfs_create_blob);
949 static size_t u32_format_array(char *buf, size_t bufsize,
950 u32 *array, int array_size)
954 while (--array_size >= 0) {
956 char term = array_size ? ' ' : '\n';
958 len = snprintf(buf, bufsize, "%u%c", *array++, term);
967 static int u32_array_open(struct inode *inode, struct file *file)
969 struct debugfs_u32_array *data = inode->i_private;
970 int size, elements = data->n_elements;
975 * - 10 digits + ' '/'\n' = 11 bytes per number
976 * - terminating NUL character
979 buf = kmalloc(size+1, GFP_KERNEL);
984 file->private_data = buf;
985 u32_format_array(buf, size, data->array, data->n_elements);
987 return nonseekable_open(inode, file);
990 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
993 size_t size = strlen(file->private_data);
995 return simple_read_from_buffer(buf, len, ppos,
996 file->private_data, size);
999 static int u32_array_release(struct inode *inode, struct file *file)
1001 kfree(file->private_data);
1006 static const struct file_operations u32_array_fops = {
1007 .owner = THIS_MODULE,
1008 .open = u32_array_open,
1009 .release = u32_array_release,
1010 .read = u32_array_read,
1011 .llseek = no_llseek,
1015 * debugfs_create_u32_array - create a debugfs file that is used to read u32
1017 * @name: a pointer to a string containing the name of the file to create.
1018 * @mode: the permission that the file should have.
1019 * @parent: a pointer to the parent dentry for this file. This should be a
1020 * directory dentry if set. If this parameter is %NULL, then the
1021 * file will be created in the root of the debugfs filesystem.
1022 * @array: wrapper struct containing data pointer and size of the array.
1024 * This function creates a file in debugfs with the given name that exports
1025 * @array as data. If the @mode variable is so set it can be read from.
1026 * Writing is not supported. Seek within the file is also not supported.
1027 * Once array is created its size can not be changed.
1029 void debugfs_create_u32_array(const char *name, umode_t mode,
1030 struct dentry *parent,
1031 struct debugfs_u32_array *array)
1033 debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
1035 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1037 #ifdef CONFIG_HAS_IOMEM
1040 * The regset32 stuff is used to print 32-bit registers using the
1041 * seq_file utilities. We offer printing a register set in an already-opened
1042 * sequential file or create a debugfs file that only prints a regset32.
1046 * debugfs_print_regs32 - use seq_print to describe a set of registers
1047 * @s: the seq_file structure being used to generate output
1048 * @regs: an array if struct debugfs_reg32 structures
1049 * @nregs: the length of the above array
1050 * @base: the base address to be used in reading the registers
1051 * @prefix: a string to be prefixed to every output line
1053 * This function outputs a text block describing the current values of
1054 * some 32-bit hardware registers. It is meant to be used within debugfs
1055 * files based on seq_file that need to show registers, intermixed with other
1056 * information. The prefix argument may be used to specify a leading string,
1057 * because some peripherals have several blocks of identical registers,
1058 * for example configuration of dma channels
1060 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1061 int nregs, void __iomem *base, char *prefix)
1065 for (i = 0; i < nregs; i++, regs++) {
1067 seq_printf(s, "%s", prefix);
1068 seq_printf(s, "%s = 0x%08x\n", regs->name,
1069 readl(base + regs->offset));
1070 if (seq_has_overflowed(s))
1074 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1076 static int debugfs_show_regset32(struct seq_file *s, void *data)
1078 struct debugfs_regset32 *regset = s->private;
1081 pm_runtime_get_sync(regset->dev);
1083 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1086 pm_runtime_put(regset->dev);
1091 static int debugfs_open_regset32(struct inode *inode, struct file *file)
1093 return single_open(file, debugfs_show_regset32, inode->i_private);
1096 static const struct file_operations fops_regset32 = {
1097 .open = debugfs_open_regset32,
1099 .llseek = seq_lseek,
1100 .release = single_release,
1104 * debugfs_create_regset32 - create a debugfs file that returns register values
1105 * @name: a pointer to a string containing the name of the file to create.
1106 * @mode: the permission that the file should have
1107 * @parent: a pointer to the parent dentry for this file. This should be a
1108 * directory dentry if set. If this parameter is %NULL, then the
1109 * file will be created in the root of the debugfs filesystem.
1110 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1111 * to an array of register definitions, the array size and the base
1112 * address where the register bank is to be found.
1114 * This function creates a file in debugfs with the given name that reports
1115 * the names and values of a set of 32-bit registers. If the @mode variable
1116 * is so set it can be read from. Writing is not supported.
1118 void debugfs_create_regset32(const char *name, umode_t mode,
1119 struct dentry *parent,
1120 struct debugfs_regset32 *regset)
1122 debugfs_create_file(name, mode, parent, regset, &fops_regset32);
1124 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1126 #endif /* CONFIG_HAS_IOMEM */
1128 struct debugfs_devm_entry {
1129 int (*read)(struct seq_file *seq, void *data);
1133 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1135 struct debugfs_devm_entry *entry = inode->i_private;
1137 return single_open(f, entry->read, entry->dev);
1140 static const struct file_operations debugfs_devm_entry_ops = {
1141 .owner = THIS_MODULE,
1142 .open = debugfs_devm_entry_open,
1143 .release = single_release,
1149 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1151 * @dev: device related to this debugfs file.
1152 * @name: name of the debugfs file.
1153 * @parent: a pointer to the parent dentry for this file. This should be a
1154 * directory dentry if set. If this parameter is %NULL, then the
1155 * file will be created in the root of the debugfs filesystem.
1156 * @read_fn: function pointer called to print the seq_file content.
1158 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
1159 struct dentry *parent,
1160 int (*read_fn)(struct seq_file *s, void *data))
1162 struct debugfs_devm_entry *entry;
1167 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1171 entry->read = read_fn;
1174 debugfs_create_file(name, S_IRUGO, parent, entry,
1175 &debugfs_devm_entry_ops);
1177 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);