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 real_fops
= fops_get(real_fops
);
180 /* Huh? Module did not clean up after itself at exit? */
181 WARN(1, "debugfs file owner did not clean up at exit: %pd",
186 replace_fops(filp
, real_fops
);
189 r
= real_fops
->open(inode
, filp
);
192 debugfs_file_put(dentry
);
196 const struct file_operations debugfs_open_proxy_file_operations
= {
197 .open
= open_proxy_open
,
200 #define PROTO(args...) args
201 #define ARGS(args...) args
203 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
204 static ret_type full_proxy_ ## name(proto) \
206 struct dentry *dentry = F_DENTRY(filp); \
207 const struct file_operations *real_fops; \
210 r = debugfs_file_get(dentry); \
213 real_fops = debugfs_real_fops(filp); \
214 r = real_fops->name(args); \
215 debugfs_file_put(dentry); \
219 FULL_PROXY_FUNC(llseek
, loff_t
, filp
,
220 PROTO(struct file
*filp
, loff_t offset
, int whence
),
221 ARGS(filp
, offset
, whence
));
223 FULL_PROXY_FUNC(read
, ssize_t
, filp
,
224 PROTO(struct file
*filp
, char __user
*buf
, size_t size
,
226 ARGS(filp
, buf
, size
, ppos
));
228 FULL_PROXY_FUNC(write
, ssize_t
, filp
,
229 PROTO(struct file
*filp
, const char __user
*buf
, size_t size
,
231 ARGS(filp
, buf
, size
, ppos
));
233 FULL_PROXY_FUNC(unlocked_ioctl
, long, filp
,
234 PROTO(struct file
*filp
, unsigned int cmd
, unsigned long arg
),
235 ARGS(filp
, cmd
, arg
));
237 static __poll_t
full_proxy_poll(struct file
*filp
,
238 struct poll_table_struct
*wait
)
240 struct dentry
*dentry
= F_DENTRY(filp
);
242 const struct file_operations
*real_fops
;
244 if (debugfs_file_get(dentry
))
247 real_fops
= debugfs_real_fops(filp
);
248 r
= real_fops
->poll(filp
, wait
);
249 debugfs_file_put(dentry
);
253 static int full_proxy_release(struct inode
*inode
, struct file
*filp
)
255 const struct dentry
*dentry
= F_DENTRY(filp
);
256 const struct file_operations
*real_fops
= debugfs_real_fops(filp
);
257 const struct file_operations
*proxy_fops
= filp
->f_op
;
261 * We must not protect this against removal races here: the
262 * original releaser should be called unconditionally in order
263 * not to leak any resources. Releasers must not assume that
264 * ->i_private is still being meaningful here.
266 if (real_fops
->release
)
267 r
= real_fops
->release(inode
, filp
);
269 replace_fops(filp
, d_inode(dentry
)->i_fop
);
270 kfree((void *)proxy_fops
);
275 static void __full_proxy_fops_init(struct file_operations
*proxy_fops
,
276 const struct file_operations
*real_fops
)
278 proxy_fops
->release
= full_proxy_release
;
279 if (real_fops
->llseek
)
280 proxy_fops
->llseek
= full_proxy_llseek
;
282 proxy_fops
->read
= full_proxy_read
;
283 if (real_fops
->write
)
284 proxy_fops
->write
= full_proxy_write
;
286 proxy_fops
->poll
= full_proxy_poll
;
287 if (real_fops
->unlocked_ioctl
)
288 proxy_fops
->unlocked_ioctl
= full_proxy_unlocked_ioctl
;
291 static int full_proxy_open(struct inode
*inode
, struct file
*filp
)
293 struct dentry
*dentry
= F_DENTRY(filp
);
294 const struct file_operations
*real_fops
= NULL
;
295 struct file_operations
*proxy_fops
= NULL
;
298 r
= debugfs_file_get(dentry
);
300 return r
== -EIO
? -ENOENT
: r
;
302 real_fops
= debugfs_real_fops(filp
);
304 r
= debugfs_locked_down(inode
, filp
, real_fops
);
308 real_fops
= fops_get(real_fops
);
310 /* Huh? Module did not cleanup after itself at exit? */
311 WARN(1, "debugfs file owner did not clean up at exit: %pd",
317 proxy_fops
= kzalloc(sizeof(*proxy_fops
), GFP_KERNEL
);
322 __full_proxy_fops_init(proxy_fops
, real_fops
);
323 replace_fops(filp
, proxy_fops
);
325 if (real_fops
->open
) {
326 r
= real_fops
->open(inode
, filp
);
328 replace_fops(filp
, d_inode(dentry
)->i_fop
);
330 } else if (filp
->f_op
!= proxy_fops
) {
331 /* No protection against file removal anymore. */
332 WARN(1, "debugfs file owner replaced proxy fops: %pd",
343 debugfs_file_put(dentry
);
347 const struct file_operations debugfs_full_proxy_file_operations
= {
348 .open
= full_proxy_open
,
351 ssize_t
debugfs_attr_read(struct file
*file
, char __user
*buf
,
352 size_t len
, loff_t
*ppos
)
354 struct dentry
*dentry
= F_DENTRY(file
);
357 ret
= debugfs_file_get(dentry
);
360 ret
= simple_attr_read(file
, buf
, len
, ppos
);
361 debugfs_file_put(dentry
);
364 EXPORT_SYMBOL_GPL(debugfs_attr_read
);
366 ssize_t
debugfs_attr_write(struct file
*file
, const char __user
*buf
,
367 size_t len
, loff_t
*ppos
)
369 struct dentry
*dentry
= F_DENTRY(file
);
372 ret
= debugfs_file_get(dentry
);
375 ret
= simple_attr_write(file
, buf
, len
, ppos
);
376 debugfs_file_put(dentry
);
379 EXPORT_SYMBOL_GPL(debugfs_attr_write
);
381 static struct dentry
*debugfs_create_mode_unsafe(const char *name
, umode_t mode
,
382 struct dentry
*parent
, void *value
,
383 const struct file_operations
*fops
,
384 const struct file_operations
*fops_ro
,
385 const struct file_operations
*fops_wo
)
387 /* if there are no write bits set, make read only */
388 if (!(mode
& S_IWUGO
))
389 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
391 /* if there are no read bits set, make write only */
392 if (!(mode
& S_IRUGO
))
393 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
396 return debugfs_create_file_unsafe(name
, mode
, parent
, value
, fops
);
399 static int debugfs_u8_set(void *data
, u64 val
)
404 static int debugfs_u8_get(void *data
, u64
*val
)
409 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8
, debugfs_u8_get
, debugfs_u8_set
, "%llu\n");
410 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro
, debugfs_u8_get
, NULL
, "%llu\n");
411 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo
, NULL
, debugfs_u8_set
, "%llu\n");
414 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
415 * @name: a pointer to a string containing the name of the file to create.
416 * @mode: the permission that the file should have
417 * @parent: a pointer to the parent dentry for this file. This should be a
418 * directory dentry if set. If this parameter is %NULL, then the
419 * file will be created in the root of the debugfs filesystem.
420 * @value: a pointer to the variable that the file should read to and write
423 * This function creates a file in debugfs with the given name that
424 * contains the value of the variable @value. If the @mode variable is so
425 * set, it can be read from, and written to.
427 void debugfs_create_u8(const char *name
, umode_t mode
, struct dentry
*parent
,
430 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u8
,
431 &fops_u8_ro
, &fops_u8_wo
);
433 EXPORT_SYMBOL_GPL(debugfs_create_u8
);
435 static int debugfs_u16_set(void *data
, u64 val
)
440 static int debugfs_u16_get(void *data
, u64
*val
)
445 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16
, debugfs_u16_get
, debugfs_u16_set
, "%llu\n");
446 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro
, debugfs_u16_get
, NULL
, "%llu\n");
447 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo
, NULL
, debugfs_u16_set
, "%llu\n");
450 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
451 * @name: a pointer to a string containing the name of the file to create.
452 * @mode: the permission that the file should have
453 * @parent: a pointer to the parent dentry for this file. This should be a
454 * directory dentry if set. If this parameter is %NULL, then the
455 * file will be created in the root of the debugfs filesystem.
456 * @value: a pointer to the variable that the file should read to and write
459 * This function creates a file in debugfs with the given name that
460 * contains the value of the variable @value. If the @mode variable is so
461 * set, it can be read from, and written to.
463 void debugfs_create_u16(const char *name
, umode_t mode
, struct dentry
*parent
,
466 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u16
,
467 &fops_u16_ro
, &fops_u16_wo
);
469 EXPORT_SYMBOL_GPL(debugfs_create_u16
);
471 static int debugfs_u32_set(void *data
, u64 val
)
476 static int debugfs_u32_get(void *data
, u64
*val
)
481 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32
, debugfs_u32_get
, debugfs_u32_set
, "%llu\n");
482 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro
, debugfs_u32_get
, NULL
, "%llu\n");
483 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo
, NULL
, debugfs_u32_set
, "%llu\n");
486 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
487 * @name: a pointer to a string containing the name of the file to create.
488 * @mode: the permission that the file should have
489 * @parent: a pointer to the parent dentry for this file. This should be a
490 * directory dentry if set. If this parameter is %NULL, then the
491 * file will be created in the root of the debugfs filesystem.
492 * @value: a pointer to the variable that the file should read to and write
495 * This function creates a file in debugfs with the given name that
496 * contains the value of the variable @value. If the @mode variable is so
497 * set, it can be read from, and written to.
499 * This function will return a pointer to a dentry if it succeeds. This
500 * pointer must be passed to the debugfs_remove() function when the file is
501 * to be removed (no automatic cleanup happens if your module is unloaded,
502 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
505 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
508 struct dentry
*debugfs_create_u32(const char *name
, umode_t mode
,
509 struct dentry
*parent
, u32
*value
)
511 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u32
,
512 &fops_u32_ro
, &fops_u32_wo
);
514 EXPORT_SYMBOL_GPL(debugfs_create_u32
);
516 static int debugfs_u64_set(void *data
, u64 val
)
522 static int debugfs_u64_get(void *data
, u64
*val
)
527 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64
, debugfs_u64_get
, debugfs_u64_set
, "%llu\n");
528 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro
, debugfs_u64_get
, NULL
, "%llu\n");
529 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo
, NULL
, debugfs_u64_set
, "%llu\n");
532 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
533 * @name: a pointer to a string containing the name of the file to create.
534 * @mode: the permission that the file should have
535 * @parent: a pointer to the parent dentry for this file. This should be a
536 * directory dentry if set. If this parameter is %NULL, then the
537 * file will be created in the root of the debugfs filesystem.
538 * @value: a pointer to the variable that the file should read to and write
541 * This function creates a file in debugfs with the given name that
542 * contains the value of the variable @value. If the @mode variable is so
543 * set, it can be read from, and written to.
545 void debugfs_create_u64(const char *name
, umode_t mode
, struct dentry
*parent
,
548 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u64
,
549 &fops_u64_ro
, &fops_u64_wo
);
551 EXPORT_SYMBOL_GPL(debugfs_create_u64
);
553 static int debugfs_ulong_set(void *data
, u64 val
)
555 *(unsigned long *)data
= val
;
559 static int debugfs_ulong_get(void *data
, u64
*val
)
561 *val
= *(unsigned long *)data
;
564 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong
, debugfs_ulong_get
, debugfs_ulong_set
,
566 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro
, debugfs_ulong_get
, NULL
, "%llu\n");
567 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo
, NULL
, debugfs_ulong_set
, "%llu\n");
570 * debugfs_create_ulong - create a debugfs file that is used to read and write
571 * an unsigned long value.
572 * @name: a pointer to a string containing the name of the file to create.
573 * @mode: the permission that the file should have
574 * @parent: a pointer to the parent dentry for this file. This should be a
575 * directory dentry if set. If this parameter is %NULL, then the
576 * file will be created in the root of the debugfs filesystem.
577 * @value: a pointer to the variable that the file should read to and write
580 * This function creates a file in debugfs with the given name that
581 * contains the value of the variable @value. If the @mode variable is so
582 * set, it can be read from, and written to.
584 * This function will return a pointer to a dentry if it succeeds. This
585 * pointer must be passed to the debugfs_remove() function when the file is
586 * to be removed (no automatic cleanup happens if your module is unloaded,
587 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
590 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
593 struct dentry
*debugfs_create_ulong(const char *name
, umode_t mode
,
594 struct dentry
*parent
, unsigned long *value
)
596 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
,
597 &fops_ulong
, &fops_ulong_ro
,
600 EXPORT_SYMBOL_GPL(debugfs_create_ulong
);
602 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8
, debugfs_u8_get
, debugfs_u8_set
, "0x%02llx\n");
603 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro
, debugfs_u8_get
, NULL
, "0x%02llx\n");
604 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo
, NULL
, debugfs_u8_set
, "0x%02llx\n");
606 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16
, debugfs_u16_get
, debugfs_u16_set
,
608 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro
, debugfs_u16_get
, NULL
, "0x%04llx\n");
609 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo
, NULL
, debugfs_u16_set
, "0x%04llx\n");
611 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32
, debugfs_u32_get
, debugfs_u32_set
,
613 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro
, debugfs_u32_get
, NULL
, "0x%08llx\n");
614 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo
, NULL
, debugfs_u32_set
, "0x%08llx\n");
616 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64
, debugfs_u64_get
, debugfs_u64_set
,
618 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro
, debugfs_u64_get
, NULL
, "0x%016llx\n");
619 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo
, NULL
, debugfs_u64_set
, "0x%016llx\n");
622 * 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
624 * These functions are exactly the same as the above functions (but use a hex
625 * output for the decimal challenged). For details look at the above unsigned
630 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
631 * @name: a pointer to a string containing the name of the file to create.
632 * @mode: the permission that the file should have
633 * @parent: a pointer to the parent dentry for this file. This should be a
634 * directory dentry if set. If this parameter is %NULL, then the
635 * file will be created in the root of the debugfs filesystem.
636 * @value: a pointer to the variable that the file should read to and write
639 void debugfs_create_x8(const char *name
, umode_t mode
, struct dentry
*parent
,
642 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x8
,
643 &fops_x8_ro
, &fops_x8_wo
);
645 EXPORT_SYMBOL_GPL(debugfs_create_x8
);
648 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
649 * @name: a pointer to a string containing the name of the file to create.
650 * @mode: the permission that the file should have
651 * @parent: a pointer to the parent dentry for this file. This should be a
652 * directory dentry if set. If this parameter is %NULL, then the
653 * file will be created in the root of the debugfs filesystem.
654 * @value: a pointer to the variable that the file should read to and write
657 void debugfs_create_x16(const char *name
, umode_t mode
, struct dentry
*parent
,
660 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x16
,
661 &fops_x16_ro
, &fops_x16_wo
);
663 EXPORT_SYMBOL_GPL(debugfs_create_x16
);
666 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
667 * @name: a pointer to a string containing the name of the file to create.
668 * @mode: the permission that the file should have
669 * @parent: a pointer to the parent dentry for this file. This should be a
670 * directory dentry if set. If this parameter is %NULL, then the
671 * file will be created in the root of the debugfs filesystem.
672 * @value: a pointer to the variable that the file should read to and write
675 void debugfs_create_x32(const char *name
, umode_t mode
, struct dentry
*parent
,
678 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x32
,
679 &fops_x32_ro
, &fops_x32_wo
);
681 EXPORT_SYMBOL_GPL(debugfs_create_x32
);
684 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
685 * @name: a pointer to a string containing the name of the file to create.
686 * @mode: the permission that the file should have
687 * @parent: a pointer to the parent dentry for this file. This should be a
688 * directory dentry if set. If this parameter is %NULL, then the
689 * file will be created in the root of the debugfs filesystem.
690 * @value: a pointer to the variable that the file should read to and write
693 void debugfs_create_x64(const char *name
, umode_t mode
, struct dentry
*parent
,
696 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x64
,
697 &fops_x64_ro
, &fops_x64_wo
);
699 EXPORT_SYMBOL_GPL(debugfs_create_x64
);
702 static int debugfs_size_t_set(void *data
, u64 val
)
704 *(size_t *)data
= val
;
707 static int debugfs_size_t_get(void *data
, u64
*val
)
709 *val
= *(size_t *)data
;
712 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t
, debugfs_size_t_get
, debugfs_size_t_set
,
713 "%llu\n"); /* %llu and %zu are more or less the same */
714 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro
, debugfs_size_t_get
, NULL
, "%llu\n");
715 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo
, NULL
, debugfs_size_t_set
, "%llu\n");
718 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
719 * @name: a pointer to a string containing the name of the file to create.
720 * @mode: the permission that the file should have
721 * @parent: a pointer to the parent dentry for this file. This should be a
722 * directory dentry if set. If this parameter is %NULL, then the
723 * file will be created in the root of the debugfs filesystem.
724 * @value: a pointer to the variable that the file should read to and write
727 void debugfs_create_size_t(const char *name
, umode_t mode
,
728 struct dentry
*parent
, size_t *value
)
730 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_size_t
,
731 &fops_size_t_ro
, &fops_size_t_wo
);
733 EXPORT_SYMBOL_GPL(debugfs_create_size_t
);
735 static int debugfs_atomic_t_set(void *data
, u64 val
)
737 atomic_set((atomic_t
*)data
, val
);
740 static int debugfs_atomic_t_get(void *data
, u64
*val
)
742 *val
= atomic_read((atomic_t
*)data
);
745 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t
, debugfs_atomic_t_get
,
746 debugfs_atomic_t_set
, "%lld\n");
747 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_ro
, debugfs_atomic_t_get
, NULL
,
749 DEFINE_DEBUGFS_ATTRIBUTE(fops_atomic_t_wo
, NULL
, debugfs_atomic_t_set
,
753 * debugfs_create_atomic_t - create a debugfs file that is used to read and
754 * write an atomic_t value
755 * @name: a pointer to a string containing the name of the file to create.
756 * @mode: the permission that the file should have
757 * @parent: a pointer to the parent dentry for this file. This should be a
758 * directory dentry if set. If this parameter is %NULL, then the
759 * file will be created in the root of the debugfs filesystem.
760 * @value: a pointer to the variable that the file should read to and write
763 void debugfs_create_atomic_t(const char *name
, umode_t mode
,
764 struct dentry
*parent
, atomic_t
*value
)
766 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_atomic_t
,
767 &fops_atomic_t_ro
, &fops_atomic_t_wo
);
769 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t
);
771 ssize_t
debugfs_read_file_bool(struct file
*file
, char __user
*user_buf
,
772 size_t count
, loff_t
*ppos
)
777 struct dentry
*dentry
= F_DENTRY(file
);
779 r
= debugfs_file_get(dentry
);
782 val
= *(bool *)file
->private_data
;
783 debugfs_file_put(dentry
);
791 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
793 EXPORT_SYMBOL_GPL(debugfs_read_file_bool
);
795 ssize_t
debugfs_write_file_bool(struct file
*file
, const char __user
*user_buf
,
796 size_t count
, loff_t
*ppos
)
800 bool *val
= file
->private_data
;
801 struct dentry
*dentry
= F_DENTRY(file
);
803 r
= kstrtobool_from_user(user_buf
, count
, &bv
);
805 r
= debugfs_file_get(dentry
);
809 debugfs_file_put(dentry
);
814 EXPORT_SYMBOL_GPL(debugfs_write_file_bool
);
816 static const struct file_operations fops_bool
= {
817 .read
= debugfs_read_file_bool
,
818 .write
= debugfs_write_file_bool
,
820 .llseek
= default_llseek
,
823 static const struct file_operations fops_bool_ro
= {
824 .read
= debugfs_read_file_bool
,
826 .llseek
= default_llseek
,
829 static const struct file_operations fops_bool_wo
= {
830 .write
= debugfs_write_file_bool
,
832 .llseek
= default_llseek
,
836 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
837 * @name: a pointer to a string containing the name of the file to create.
838 * @mode: the permission that the file should have
839 * @parent: a pointer to the parent dentry for this file. This should be a
840 * directory dentry if set. If this parameter is %NULL, then the
841 * file will be created in the root of the debugfs filesystem.
842 * @value: a pointer to the variable that the file should read to and write
845 * This function creates a file in debugfs with the given name that
846 * contains the value of the variable @value. If the @mode variable is so
847 * set, it can be read from, and written to.
849 * This function will return a pointer to a dentry if it succeeds. This
850 * pointer must be passed to the debugfs_remove() function when the file is
851 * to be removed (no automatic cleanup happens if your module is unloaded,
852 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
855 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
858 struct dentry
*debugfs_create_bool(const char *name
, umode_t mode
,
859 struct dentry
*parent
, bool *value
)
861 return debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_bool
,
862 &fops_bool_ro
, &fops_bool_wo
);
864 EXPORT_SYMBOL_GPL(debugfs_create_bool
);
866 static ssize_t
read_file_blob(struct file
*file
, char __user
*user_buf
,
867 size_t count
, loff_t
*ppos
)
869 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
870 struct dentry
*dentry
= F_DENTRY(file
);
873 r
= debugfs_file_get(dentry
);
876 r
= simple_read_from_buffer(user_buf
, count
, ppos
, blob
->data
,
878 debugfs_file_put(dentry
);
882 static const struct file_operations fops_blob
= {
883 .read
= read_file_blob
,
885 .llseek
= default_llseek
,
889 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
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 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
896 * to the blob data and the size of the data.
898 * This function creates a file in debugfs with the given name that exports
899 * @blob->data as a binary blob. If the @mode variable is so set it can be
900 * read from. Writing is not supported.
902 * This function will return a pointer to a dentry if it succeeds. This
903 * pointer must be passed to the debugfs_remove() function when the file is
904 * to be removed (no automatic cleanup happens if your module is unloaded,
905 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
908 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
911 struct dentry
*debugfs_create_blob(const char *name
, umode_t mode
,
912 struct dentry
*parent
,
913 struct debugfs_blob_wrapper
*blob
)
915 return debugfs_create_file_unsafe(name
, mode
, parent
, blob
, &fops_blob
);
917 EXPORT_SYMBOL_GPL(debugfs_create_blob
);
924 static size_t u32_format_array(char *buf
, size_t bufsize
,
925 u32
*array
, int array_size
)
929 while (--array_size
>= 0) {
931 char term
= array_size
? ' ' : '\n';
933 len
= snprintf(buf
, bufsize
, "%u%c", *array
++, term
);
942 static int u32_array_open(struct inode
*inode
, struct file
*file
)
944 struct array_data
*data
= inode
->i_private
;
945 int size
, elements
= data
->elements
;
950 * - 10 digits + ' '/'\n' = 11 bytes per number
951 * - terminating NUL character
954 buf
= kmalloc(size
+1, GFP_KERNEL
);
959 file
->private_data
= buf
;
960 u32_format_array(buf
, size
, data
->array
, data
->elements
);
962 return nonseekable_open(inode
, file
);
965 static ssize_t
u32_array_read(struct file
*file
, char __user
*buf
, size_t len
,
968 size_t size
= strlen(file
->private_data
);
970 return simple_read_from_buffer(buf
, len
, ppos
,
971 file
->private_data
, size
);
974 static int u32_array_release(struct inode
*inode
, struct file
*file
)
976 kfree(file
->private_data
);
981 static const struct file_operations u32_array_fops
= {
982 .owner
= THIS_MODULE
,
983 .open
= u32_array_open
,
984 .release
= u32_array_release
,
985 .read
= u32_array_read
,
990 * debugfs_create_u32_array - create a debugfs file that is used to read u32
992 * @name: a pointer to a string containing the name of the file to create.
993 * @mode: the permission that the file should have.
994 * @parent: a pointer to the parent dentry for this file. This should be a
995 * directory dentry if set. If this parameter is %NULL, then the
996 * file will be created in the root of the debugfs filesystem.
997 * @array: u32 array that provides data.
998 * @elements: total number of elements in the array.
1000 * This function creates a file in debugfs with the given name that exports
1001 * @array as data. If the @mode variable is so set it can be read from.
1002 * Writing is not supported. Seek within the file is also not supported.
1003 * Once array is created its size can not be changed.
1005 void debugfs_create_u32_array(const char *name
, umode_t mode
,
1006 struct dentry
*parent
, u32
*array
, u32 elements
)
1008 struct array_data
*data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
1013 data
->array
= array
;
1014 data
->elements
= elements
;
1016 debugfs_create_file_unsafe(name
, mode
, parent
, data
, &u32_array_fops
);
1018 EXPORT_SYMBOL_GPL(debugfs_create_u32_array
);
1020 #ifdef CONFIG_HAS_IOMEM
1023 * The regset32 stuff is used to print 32-bit registers using the
1024 * seq_file utilities. We offer printing a register set in an already-opened
1025 * sequential file or create a debugfs file that only prints a regset32.
1029 * debugfs_print_regs32 - use seq_print to describe a set of registers
1030 * @s: the seq_file structure being used to generate output
1031 * @regs: an array if struct debugfs_reg32 structures
1032 * @nregs: the length of the above array
1033 * @base: the base address to be used in reading the registers
1034 * @prefix: a string to be prefixed to every output line
1036 * This function outputs a text block describing the current values of
1037 * some 32-bit hardware registers. It is meant to be used within debugfs
1038 * files based on seq_file that need to show registers, intermixed with other
1039 * information. The prefix argument may be used to specify a leading string,
1040 * because some peripherals have several blocks of identical registers,
1041 * for example configuration of dma channels
1043 void debugfs_print_regs32(struct seq_file
*s
, const struct debugfs_reg32
*regs
,
1044 int nregs
, void __iomem
*base
, char *prefix
)
1048 for (i
= 0; i
< nregs
; i
++, regs
++) {
1050 seq_printf(s
, "%s", prefix
);
1051 seq_printf(s
, "%s = 0x%08x\n", regs
->name
,
1052 readl(base
+ regs
->offset
));
1053 if (seq_has_overflowed(s
))
1057 EXPORT_SYMBOL_GPL(debugfs_print_regs32
);
1059 static int debugfs_show_regset32(struct seq_file
*s
, void *data
)
1061 struct debugfs_regset32
*regset
= s
->private;
1063 debugfs_print_regs32(s
, regset
->regs
, regset
->nregs
, regset
->base
, "");
1067 static int debugfs_open_regset32(struct inode
*inode
, struct file
*file
)
1069 return single_open(file
, debugfs_show_regset32
, inode
->i_private
);
1072 static const struct file_operations fops_regset32
= {
1073 .open
= debugfs_open_regset32
,
1075 .llseek
= seq_lseek
,
1076 .release
= single_release
,
1080 * debugfs_create_regset32 - create a debugfs file that returns register values
1081 * @name: a pointer to a string containing the name of the file to create.
1082 * @mode: the permission that the file should have
1083 * @parent: a pointer to the parent dentry for this file. This should be a
1084 * directory dentry if set. If this parameter is %NULL, then the
1085 * file will be created in the root of the debugfs filesystem.
1086 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1087 * to an array of register definitions, the array size and the base
1088 * address where the register bank is to be found.
1090 * This function creates a file in debugfs with the given name that reports
1091 * the names and values of a set of 32-bit registers. If the @mode variable
1092 * is so set it can be read from. Writing is not supported.
1094 * This function will return a pointer to a dentry if it succeeds. This
1095 * pointer must be passed to the debugfs_remove() function when the file is
1096 * to be removed (no automatic cleanup happens if your module is unloaded,
1097 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
1100 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
1103 struct dentry
*debugfs_create_regset32(const char *name
, umode_t mode
,
1104 struct dentry
*parent
,
1105 struct debugfs_regset32
*regset
)
1107 return debugfs_create_file(name
, mode
, parent
, regset
, &fops_regset32
);
1109 EXPORT_SYMBOL_GPL(debugfs_create_regset32
);
1111 #endif /* CONFIG_HAS_IOMEM */
1113 struct debugfs_devm_entry
{
1114 int (*read
)(struct seq_file
*seq
, void *data
);
1118 static int debugfs_devm_entry_open(struct inode
*inode
, struct file
*f
)
1120 struct debugfs_devm_entry
*entry
= inode
->i_private
;
1122 return single_open(f
, entry
->read
, entry
->dev
);
1125 static const struct file_operations debugfs_devm_entry_ops
= {
1126 .owner
= THIS_MODULE
,
1127 .open
= debugfs_devm_entry_open
,
1128 .release
= single_release
,
1134 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1136 * @dev: device related to this debugfs file.
1137 * @name: name of the debugfs file.
1138 * @parent: a pointer to the parent dentry for this file. This should be a
1139 * directory dentry if set. If this parameter is %NULL, then the
1140 * file will be created in the root of the debugfs filesystem.
1141 * @read_fn: function pointer called to print the seq_file content.
1143 struct dentry
*debugfs_create_devm_seqfile(struct device
*dev
, const char *name
,
1144 struct dentry
*parent
,
1145 int (*read_fn
)(struct seq_file
*s
,
1148 struct debugfs_devm_entry
*entry
;
1151 return ERR_PTR(-ENOENT
);
1153 entry
= devm_kzalloc(dev
, sizeof(*entry
), GFP_KERNEL
);
1155 return ERR_PTR(-ENOMEM
);
1157 entry
->read
= read_fn
;
1160 return debugfs_create_file(name
, S_IRUGO
, parent
, entry
,
1161 &debugfs_devm_entry_ops
);
1163 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile
);