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 if ((unsigned long)d_fsd
& DEBUGFS_FSDATA_IS_SHORT_FOPS_BIT
) {
104 fsd
->real_fops
= NULL
;
105 fsd
->short_fops
= (void *)((unsigned long)d_fsd
&
106 ~(DEBUGFS_FSDATA_IS_REAL_FOPS_BIT
|
107 DEBUGFS_FSDATA_IS_SHORT_FOPS_BIT
));
109 fsd
->real_fops
= (void *)((unsigned long)d_fsd
&
110 ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT
);
111 fsd
->short_fops
= NULL
;
113 refcount_set(&fsd
->active_users
, 1);
114 init_completion(&fsd
->active_users_drained
);
115 INIT_LIST_HEAD(&fsd
->cancellations
);
116 mutex_init(&fsd
->cancellations_mtx
);
118 if (cmpxchg(&dentry
->d_fsdata
, d_fsd
, fsd
) != d_fsd
) {
119 mutex_destroy(&fsd
->cancellations_mtx
);
121 fsd
= READ_ONCE(dentry
->d_fsdata
);
126 * In case of a successful cmpxchg() above, this check is
127 * strictly necessary and must follow it, see the comment in
128 * __debugfs_remove_file().
129 * OTOH, if the cmpxchg() hasn't been executed or wasn't
130 * successful, this serves the purpose of not starving
133 if (d_unlinked(dentry
))
136 if (!refcount_inc_not_zero(&fsd
->active_users
))
141 EXPORT_SYMBOL_GPL(debugfs_file_get
);
144 * debugfs_file_put - mark the end of file data access
145 * @dentry: the dentry object formerly passed to
146 * debugfs_file_get().
148 * Allow any ongoing concurrent call into debugfs_remove() or
149 * debugfs_remove_recursive() blocked by a former call to
150 * debugfs_file_get() to proceed and return to its caller.
152 void debugfs_file_put(struct dentry
*dentry
)
154 struct debugfs_fsdata
*fsd
= READ_ONCE(dentry
->d_fsdata
);
156 if (refcount_dec_and_test(&fsd
->active_users
))
157 complete(&fsd
->active_users_drained
);
159 EXPORT_SYMBOL_GPL(debugfs_file_put
);
162 * debugfs_enter_cancellation - enter a debugfs cancellation
163 * @file: the file being accessed
164 * @cancellation: the cancellation object, the cancel callback
165 * inside of it must be initialized
167 * When a debugfs file is removed it needs to wait for all active
168 * operations to complete. However, the operation itself may need
169 * to wait for hardware or completion of some asynchronous process
170 * or similar. As such, it may need to be cancelled to avoid long
171 * waits or even deadlocks.
173 * This function can be used inside a debugfs handler that may
174 * need to be cancelled. As soon as this function is called, the
175 * cancellation's 'cancel' callback may be called, at which point
176 * the caller should proceed to call debugfs_leave_cancellation()
177 * and leave the debugfs handler function as soon as possible.
178 * Note that the 'cancel' callback is only ever called in the
179 * context of some kind of debugfs_remove().
181 * This function must be paired with debugfs_leave_cancellation().
183 void debugfs_enter_cancellation(struct file
*file
,
184 struct debugfs_cancellation
*cancellation
)
186 struct debugfs_fsdata
*fsd
;
187 struct dentry
*dentry
= F_DENTRY(file
);
189 INIT_LIST_HEAD(&cancellation
->list
);
191 if (WARN_ON(!d_is_reg(dentry
)))
194 if (WARN_ON(!cancellation
->cancel
))
197 fsd
= READ_ONCE(dentry
->d_fsdata
);
199 ((unsigned long)fsd
& DEBUGFS_FSDATA_IS_REAL_FOPS_BIT
)))
202 mutex_lock(&fsd
->cancellations_mtx
);
203 list_add(&cancellation
->list
, &fsd
->cancellations
);
204 mutex_unlock(&fsd
->cancellations_mtx
);
206 /* if we're already removing wake it up to cancel */
207 if (d_unlinked(dentry
))
208 complete(&fsd
->active_users_drained
);
210 EXPORT_SYMBOL_GPL(debugfs_enter_cancellation
);
213 * debugfs_leave_cancellation - leave cancellation section
214 * @file: the file being accessed
215 * @cancellation: the cancellation previously registered with
216 * debugfs_enter_cancellation()
218 * See the documentation of debugfs_enter_cancellation().
220 void debugfs_leave_cancellation(struct file
*file
,
221 struct debugfs_cancellation
*cancellation
)
223 struct debugfs_fsdata
*fsd
;
224 struct dentry
*dentry
= F_DENTRY(file
);
226 if (WARN_ON(!d_is_reg(dentry
)))
229 fsd
= READ_ONCE(dentry
->d_fsdata
);
231 ((unsigned long)fsd
& DEBUGFS_FSDATA_IS_REAL_FOPS_BIT
)))
234 mutex_lock(&fsd
->cancellations_mtx
);
235 if (!list_empty(&cancellation
->list
))
236 list_del(&cancellation
->list
);
237 mutex_unlock(&fsd
->cancellations_mtx
);
239 EXPORT_SYMBOL_GPL(debugfs_leave_cancellation
);
242 * Only permit access to world-readable files when the kernel is locked down.
243 * We also need to exclude any file that has ways to write or alter it as root
244 * can bypass the permissions check.
246 static int debugfs_locked_down(struct inode
*inode
,
248 const struct file_operations
*real_fops
)
250 if ((inode
->i_mode
& 07777 & ~0444) == 0 &&
251 !(filp
->f_mode
& FMODE_WRITE
) &&
253 (!real_fops
->unlocked_ioctl
&&
254 !real_fops
->compat_ioctl
&&
258 if (security_locked_down(LOCKDOWN_DEBUGFS
))
264 static int open_proxy_open(struct inode
*inode
, struct file
*filp
)
266 struct dentry
*dentry
= F_DENTRY(filp
);
267 const struct file_operations
*real_fops
= NULL
;
270 r
= debugfs_file_get(dentry
);
272 return r
== -EIO
? -ENOENT
: r
;
274 real_fops
= debugfs_real_fops(filp
);
276 r
= debugfs_locked_down(inode
, filp
, real_fops
);
280 if (!fops_get(real_fops
)) {
281 #ifdef CONFIG_MODULES
282 if (real_fops
->owner
&&
283 real_fops
->owner
->state
== MODULE_STATE_GOING
) {
289 /* Huh? Module did not clean up after itself at exit? */
290 WARN(1, "debugfs file owner did not clean up at exit: %pd",
295 replace_fops(filp
, real_fops
);
298 r
= real_fops
->open(inode
, filp
);
301 debugfs_file_put(dentry
);
305 const struct file_operations debugfs_open_proxy_file_operations
= {
306 .open
= open_proxy_open
,
309 #define PROTO(args...) args
310 #define ARGS(args...) args
312 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
313 static ret_type full_proxy_ ## name(proto) \
315 struct dentry *dentry = F_DENTRY(filp); \
316 const struct file_operations *real_fops; \
319 r = debugfs_file_get(dentry); \
322 real_fops = debugfs_real_fops(filp); \
323 r = real_fops->name(args); \
324 debugfs_file_put(dentry); \
328 #define FULL_PROXY_FUNC_BOTH(name, ret_type, filp, proto, args) \
329 static ret_type full_proxy_ ## name(proto) \
331 struct dentry *dentry = F_DENTRY(filp); \
332 struct debugfs_fsdata *fsd; \
335 r = debugfs_file_get(dentry); \
338 fsd = dentry->d_fsdata; \
339 if (fsd->real_fops) \
340 r = fsd->real_fops->name(args); \
342 r = fsd->short_fops->name(args); \
343 debugfs_file_put(dentry); \
347 FULL_PROXY_FUNC_BOTH(llseek
, loff_t
, filp
,
348 PROTO(struct file
*filp
, loff_t offset
, int whence
),
349 ARGS(filp
, offset
, whence
));
351 FULL_PROXY_FUNC_BOTH(read
, ssize_t
, filp
,
352 PROTO(struct file
*filp
, char __user
*buf
, size_t size
,
354 ARGS(filp
, buf
, size
, ppos
));
356 FULL_PROXY_FUNC_BOTH(write
, ssize_t
, filp
,
357 PROTO(struct file
*filp
, const char __user
*buf
,
358 size_t size
, loff_t
*ppos
),
359 ARGS(filp
, buf
, size
, ppos
));
361 FULL_PROXY_FUNC(unlocked_ioctl
, long, filp
,
362 PROTO(struct file
*filp
, unsigned int cmd
, unsigned long arg
),
363 ARGS(filp
, cmd
, arg
));
365 static __poll_t
full_proxy_poll(struct file
*filp
,
366 struct poll_table_struct
*wait
)
368 struct dentry
*dentry
= F_DENTRY(filp
);
370 const struct file_operations
*real_fops
;
372 if (debugfs_file_get(dentry
))
375 real_fops
= debugfs_real_fops(filp
);
376 r
= real_fops
->poll(filp
, wait
);
377 debugfs_file_put(dentry
);
381 static int full_proxy_release(struct inode
*inode
, struct file
*filp
)
383 const struct dentry
*dentry
= F_DENTRY(filp
);
384 const struct file_operations
*real_fops
= debugfs_real_fops(filp
);
385 const struct file_operations
*proxy_fops
= filp
->f_op
;
389 * We must not protect this against removal races here: the
390 * original releaser should be called unconditionally in order
391 * not to leak any resources. Releasers must not assume that
392 * ->i_private is still being meaningful here.
394 if (real_fops
&& real_fops
->release
)
395 r
= real_fops
->release(inode
, filp
);
397 replace_fops(filp
, d_inode(dentry
)->i_fop
);
403 static void __full_proxy_fops_init(struct file_operations
*proxy_fops
,
404 struct debugfs_fsdata
*fsd
)
406 proxy_fops
->release
= full_proxy_release
;
408 if ((fsd
->real_fops
&& fsd
->real_fops
->llseek
) ||
409 (fsd
->short_fops
&& fsd
->short_fops
->llseek
))
410 proxy_fops
->llseek
= full_proxy_llseek
;
412 if ((fsd
->real_fops
&& fsd
->real_fops
->read
) ||
413 (fsd
->short_fops
&& fsd
->short_fops
->read
))
414 proxy_fops
->read
= full_proxy_read
;
416 if ((fsd
->real_fops
&& fsd
->real_fops
->write
) ||
417 (fsd
->short_fops
&& fsd
->short_fops
->write
))
418 proxy_fops
->write
= full_proxy_write
;
420 if (fsd
->real_fops
&& fsd
->real_fops
->poll
)
421 proxy_fops
->poll
= full_proxy_poll
;
423 if (fsd
->real_fops
&& fsd
->real_fops
->unlocked_ioctl
)
424 proxy_fops
->unlocked_ioctl
= full_proxy_unlocked_ioctl
;
427 static int full_proxy_open(struct inode
*inode
, struct file
*filp
)
429 struct dentry
*dentry
= F_DENTRY(filp
);
430 const struct file_operations
*real_fops
;
431 struct file_operations
*proxy_fops
= NULL
;
432 struct debugfs_fsdata
*fsd
;
435 r
= debugfs_file_get(dentry
);
437 return r
== -EIO
? -ENOENT
: r
;
439 fsd
= dentry
->d_fsdata
;
440 real_fops
= fsd
->real_fops
;
441 r
= debugfs_locked_down(inode
, filp
, real_fops
);
445 if (real_fops
&& !fops_get(real_fops
)) {
446 #ifdef CONFIG_MODULES
447 if (real_fops
->owner
&&
448 real_fops
->owner
->state
== MODULE_STATE_GOING
) {
454 /* Huh? Module did not cleanup after itself at exit? */
455 WARN(1, "debugfs file owner did not clean up at exit: %pd",
461 proxy_fops
= kzalloc(sizeof(*proxy_fops
), GFP_KERNEL
);
466 __full_proxy_fops_init(proxy_fops
, fsd
);
467 replace_fops(filp
, proxy_fops
);
469 if (!real_fops
|| real_fops
->open
) {
471 r
= real_fops
->open(inode
, filp
);
473 r
= simple_open(inode
, filp
);
475 replace_fops(filp
, d_inode(dentry
)->i_fop
);
477 } else if (filp
->f_op
!= proxy_fops
) {
478 /* No protection against file removal anymore. */
479 WARN(1, "debugfs file owner replaced proxy fops: %pd",
490 debugfs_file_put(dentry
);
494 const struct file_operations debugfs_full_proxy_file_operations
= {
495 .open
= full_proxy_open
,
498 ssize_t
debugfs_attr_read(struct file
*file
, char __user
*buf
,
499 size_t len
, loff_t
*ppos
)
501 struct dentry
*dentry
= F_DENTRY(file
);
504 ret
= debugfs_file_get(dentry
);
507 ret
= simple_attr_read(file
, buf
, len
, ppos
);
508 debugfs_file_put(dentry
);
511 EXPORT_SYMBOL_GPL(debugfs_attr_read
);
513 static ssize_t
debugfs_attr_write_xsigned(struct file
*file
, const char __user
*buf
,
514 size_t len
, loff_t
*ppos
, bool is_signed
)
516 struct dentry
*dentry
= F_DENTRY(file
);
519 ret
= debugfs_file_get(dentry
);
523 ret
= simple_attr_write_signed(file
, buf
, len
, ppos
);
525 ret
= simple_attr_write(file
, buf
, len
, ppos
);
526 debugfs_file_put(dentry
);
530 ssize_t
debugfs_attr_write(struct file
*file
, const char __user
*buf
,
531 size_t len
, loff_t
*ppos
)
533 return debugfs_attr_write_xsigned(file
, buf
, len
, ppos
, false);
535 EXPORT_SYMBOL_GPL(debugfs_attr_write
);
537 ssize_t
debugfs_attr_write_signed(struct file
*file
, const char __user
*buf
,
538 size_t len
, loff_t
*ppos
)
540 return debugfs_attr_write_xsigned(file
, buf
, len
, ppos
, true);
542 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed
);
544 static struct dentry
*debugfs_create_mode_unsafe(const char *name
, umode_t mode
,
545 struct dentry
*parent
, void *value
,
546 const struct file_operations
*fops
,
547 const struct file_operations
*fops_ro
,
548 const struct file_operations
*fops_wo
)
550 /* if there are no write bits set, make read only */
551 if (!(mode
& S_IWUGO
))
552 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
554 /* if there are no read bits set, make write only */
555 if (!(mode
& S_IRUGO
))
556 return debugfs_create_file_unsafe(name
, mode
, parent
, value
,
559 return debugfs_create_file_unsafe(name
, mode
, parent
, value
, fops
);
562 static int debugfs_u8_set(void *data
, u64 val
)
567 static int debugfs_u8_get(void *data
, u64
*val
)
572 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8
, debugfs_u8_get
, debugfs_u8_set
, "%llu\n");
573 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro
, debugfs_u8_get
, NULL
, "%llu\n");
574 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo
, NULL
, debugfs_u8_set
, "%llu\n");
577 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
578 * @name: a pointer to a string containing the name of the file to create.
579 * @mode: the permission that the file should have
580 * @parent: a pointer to the parent dentry for this file. This should be a
581 * directory dentry if set. If this parameter is %NULL, then the
582 * file will be created in the root of the debugfs filesystem.
583 * @value: a pointer to the variable that the file should read to and write
586 * This function creates a file in debugfs with the given name that
587 * contains the value of the variable @value. If the @mode variable is so
588 * set, it can be read from, and written to.
590 void debugfs_create_u8(const char *name
, umode_t mode
, struct dentry
*parent
,
593 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u8
,
594 &fops_u8_ro
, &fops_u8_wo
);
596 EXPORT_SYMBOL_GPL(debugfs_create_u8
);
598 static int debugfs_u16_set(void *data
, u64 val
)
603 static int debugfs_u16_get(void *data
, u64
*val
)
608 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16
, debugfs_u16_get
, debugfs_u16_set
, "%llu\n");
609 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro
, debugfs_u16_get
, NULL
, "%llu\n");
610 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo
, NULL
, debugfs_u16_set
, "%llu\n");
613 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
614 * @name: a pointer to a string containing the name of the file to create.
615 * @mode: the permission that the file should have
616 * @parent: a pointer to the parent dentry for this file. This should be a
617 * directory dentry if set. If this parameter is %NULL, then the
618 * file will be created in the root of the debugfs filesystem.
619 * @value: a pointer to the variable that the file should read to and write
622 * This function creates a file in debugfs with the given name that
623 * contains the value of the variable @value. If the @mode variable is so
624 * set, it can be read from, and written to.
626 void debugfs_create_u16(const char *name
, umode_t mode
, struct dentry
*parent
,
629 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u16
,
630 &fops_u16_ro
, &fops_u16_wo
);
632 EXPORT_SYMBOL_GPL(debugfs_create_u16
);
634 static int debugfs_u32_set(void *data
, u64 val
)
639 static int debugfs_u32_get(void *data
, u64
*val
)
644 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32
, debugfs_u32_get
, debugfs_u32_set
, "%llu\n");
645 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro
, debugfs_u32_get
, NULL
, "%llu\n");
646 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo
, NULL
, debugfs_u32_set
, "%llu\n");
649 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
650 * @name: a pointer to a string containing the name of the file to create.
651 * @mode: the permission that the file should have
652 * @parent: a pointer to the parent dentry for this file. This should be a
653 * directory dentry if set. If this parameter is %NULL, then the
654 * file will be created in the root of the debugfs filesystem.
655 * @value: a pointer to the variable that the file should read to and write
658 * This function creates a file in debugfs with the given name that
659 * contains the value of the variable @value. If the @mode variable is so
660 * set, it can be read from, and written to.
662 void debugfs_create_u32(const char *name
, umode_t mode
, struct dentry
*parent
,
665 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u32
,
666 &fops_u32_ro
, &fops_u32_wo
);
668 EXPORT_SYMBOL_GPL(debugfs_create_u32
);
670 static int debugfs_u64_set(void *data
, u64 val
)
676 static int debugfs_u64_get(void *data
, u64
*val
)
681 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64
, debugfs_u64_get
, debugfs_u64_set
, "%llu\n");
682 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro
, debugfs_u64_get
, NULL
, "%llu\n");
683 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo
, NULL
, debugfs_u64_set
, "%llu\n");
686 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
687 * @name: a pointer to a string containing the name of the file to create.
688 * @mode: the permission that the file should have
689 * @parent: a pointer to the parent dentry for this file. This should be a
690 * directory dentry if set. If this parameter is %NULL, then the
691 * file will be created in the root of the debugfs filesystem.
692 * @value: a pointer to the variable that the file should read to and write
695 * This function creates a file in debugfs with the given name that
696 * contains the value of the variable @value. If the @mode variable is so
697 * set, it can be read from, and written to.
699 void debugfs_create_u64(const char *name
, umode_t mode
, struct dentry
*parent
,
702 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_u64
,
703 &fops_u64_ro
, &fops_u64_wo
);
705 EXPORT_SYMBOL_GPL(debugfs_create_u64
);
707 static int debugfs_ulong_set(void *data
, u64 val
)
709 *(unsigned long *)data
= val
;
713 static int debugfs_ulong_get(void *data
, u64
*val
)
715 *val
= *(unsigned long *)data
;
718 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong
, debugfs_ulong_get
, debugfs_ulong_set
,
720 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro
, debugfs_ulong_get
, NULL
, "%llu\n");
721 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo
, NULL
, debugfs_ulong_set
, "%llu\n");
724 * debugfs_create_ulong - create a debugfs file that is used to read and write
725 * an unsigned long value.
726 * @name: a pointer to a string containing the name of the file to create.
727 * @mode: the permission that the file should have
728 * @parent: a pointer to the parent dentry for this file. This should be a
729 * directory dentry if set. If this parameter is %NULL, then the
730 * file will be created in the root of the debugfs filesystem.
731 * @value: a pointer to the variable that the file should read to and write
734 * This function creates a file in debugfs with the given name that
735 * contains the value of the variable @value. If the @mode variable is so
736 * set, it can be read from, and written to.
738 void debugfs_create_ulong(const char *name
, umode_t mode
, struct dentry
*parent
,
739 unsigned long *value
)
741 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_ulong
,
742 &fops_ulong_ro
, &fops_ulong_wo
);
744 EXPORT_SYMBOL_GPL(debugfs_create_ulong
);
746 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8
, debugfs_u8_get
, debugfs_u8_set
, "0x%02llx\n");
747 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro
, debugfs_u8_get
, NULL
, "0x%02llx\n");
748 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo
, NULL
, debugfs_u8_set
, "0x%02llx\n");
750 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16
, debugfs_u16_get
, debugfs_u16_set
,
752 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro
, debugfs_u16_get
, NULL
, "0x%04llx\n");
753 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo
, NULL
, debugfs_u16_set
, "0x%04llx\n");
755 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32
, debugfs_u32_get
, debugfs_u32_set
,
757 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro
, debugfs_u32_get
, NULL
, "0x%08llx\n");
758 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo
, NULL
, debugfs_u32_set
, "0x%08llx\n");
760 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64
, debugfs_u64_get
, debugfs_u64_set
,
762 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro
, debugfs_u64_get
, NULL
, "0x%016llx\n");
763 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo
, NULL
, debugfs_u64_set
, "0x%016llx\n");
766 * 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
768 * These functions are exactly the same as the above functions (but use a hex
769 * output for the decimal challenged). For details look at the above unsigned
774 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
775 * @name: a pointer to a string containing the name of the file to create.
776 * @mode: the permission that the file should have
777 * @parent: a pointer to the parent dentry for this file. This should be a
778 * directory dentry if set. If this parameter is %NULL, then the
779 * file will be created in the root of the debugfs filesystem.
780 * @value: a pointer to the variable that the file should read to and write
783 void debugfs_create_x8(const char *name
, umode_t mode
, struct dentry
*parent
,
786 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x8
,
787 &fops_x8_ro
, &fops_x8_wo
);
789 EXPORT_SYMBOL_GPL(debugfs_create_x8
);
792 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
793 * @name: a pointer to a string containing the name of the file to create.
794 * @mode: the permission that the file should have
795 * @parent: a pointer to the parent dentry for this file. This should be a
796 * directory dentry if set. If this parameter is %NULL, then the
797 * file will be created in the root of the debugfs filesystem.
798 * @value: a pointer to the variable that the file should read to and write
801 void debugfs_create_x16(const char *name
, umode_t mode
, struct dentry
*parent
,
804 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x16
,
805 &fops_x16_ro
, &fops_x16_wo
);
807 EXPORT_SYMBOL_GPL(debugfs_create_x16
);
810 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
811 * @name: a pointer to a string containing the name of the file to create.
812 * @mode: the permission that the file should have
813 * @parent: a pointer to the parent dentry for this file. This should be a
814 * directory dentry if set. If this parameter is %NULL, then the
815 * file will be created in the root of the debugfs filesystem.
816 * @value: a pointer to the variable that the file should read to and write
819 void debugfs_create_x32(const char *name
, umode_t mode
, struct dentry
*parent
,
822 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x32
,
823 &fops_x32_ro
, &fops_x32_wo
);
825 EXPORT_SYMBOL_GPL(debugfs_create_x32
);
828 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
829 * @name: a pointer to a string containing the name of the file to create.
830 * @mode: the permission that the file should have
831 * @parent: a pointer to the parent dentry for this file. This should be a
832 * directory dentry if set. If this parameter is %NULL, then the
833 * file will be created in the root of the debugfs filesystem.
834 * @value: a pointer to the variable that the file should read to and write
837 void debugfs_create_x64(const char *name
, umode_t mode
, struct dentry
*parent
,
840 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_x64
,
841 &fops_x64_ro
, &fops_x64_wo
);
843 EXPORT_SYMBOL_GPL(debugfs_create_x64
);
846 static int debugfs_size_t_set(void *data
, u64 val
)
848 *(size_t *)data
= val
;
851 static int debugfs_size_t_get(void *data
, u64
*val
)
853 *val
= *(size_t *)data
;
856 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t
, debugfs_size_t_get
, debugfs_size_t_set
,
857 "%llu\n"); /* %llu and %zu are more or less the same */
858 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro
, debugfs_size_t_get
, NULL
, "%llu\n");
859 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo
, NULL
, debugfs_size_t_set
, "%llu\n");
862 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
863 * @name: a pointer to a string containing the name of the file to create.
864 * @mode: the permission that the file should have
865 * @parent: a pointer to the parent dentry for this file. This should be a
866 * directory dentry if set. If this parameter is %NULL, then the
867 * file will be created in the root of the debugfs filesystem.
868 * @value: a pointer to the variable that the file should read to and write
871 void debugfs_create_size_t(const char *name
, umode_t mode
,
872 struct dentry
*parent
, size_t *value
)
874 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_size_t
,
875 &fops_size_t_ro
, &fops_size_t_wo
);
877 EXPORT_SYMBOL_GPL(debugfs_create_size_t
);
879 static int debugfs_atomic_t_set(void *data
, u64 val
)
881 atomic_set((atomic_t
*)data
, val
);
884 static int debugfs_atomic_t_get(void *data
, u64
*val
)
886 *val
= atomic_read((atomic_t
*)data
);
889 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t
, debugfs_atomic_t_get
,
890 debugfs_atomic_t_set
, "%lld\n");
891 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro
, debugfs_atomic_t_get
, NULL
,
893 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo
, NULL
, debugfs_atomic_t_set
,
897 * debugfs_create_atomic_t - create a debugfs file that is used to read and
898 * write an atomic_t value
899 * @name: a pointer to a string containing the name of the file to create.
900 * @mode: the permission that the file should have
901 * @parent: a pointer to the parent dentry for this file. This should be a
902 * directory dentry if set. If this parameter is %NULL, then the
903 * file will be created in the root of the debugfs filesystem.
904 * @value: a pointer to the variable that the file should read to and write
907 void debugfs_create_atomic_t(const char *name
, umode_t mode
,
908 struct dentry
*parent
, atomic_t
*value
)
910 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_atomic_t
,
911 &fops_atomic_t_ro
, &fops_atomic_t_wo
);
913 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t
);
915 ssize_t
debugfs_read_file_bool(struct file
*file
, char __user
*user_buf
,
916 size_t count
, loff_t
*ppos
)
921 struct dentry
*dentry
= F_DENTRY(file
);
923 r
= debugfs_file_get(dentry
);
926 val
= *(bool *)file
->private_data
;
927 debugfs_file_put(dentry
);
934 return simple_read_from_buffer(user_buf
, count
, ppos
, buf
, 2);
936 EXPORT_SYMBOL_GPL(debugfs_read_file_bool
);
938 ssize_t
debugfs_write_file_bool(struct file
*file
, const char __user
*user_buf
,
939 size_t count
, loff_t
*ppos
)
943 bool *val
= file
->private_data
;
944 struct dentry
*dentry
= F_DENTRY(file
);
946 r
= kstrtobool_from_user(user_buf
, count
, &bv
);
948 r
= debugfs_file_get(dentry
);
952 debugfs_file_put(dentry
);
957 EXPORT_SYMBOL_GPL(debugfs_write_file_bool
);
959 static const struct file_operations fops_bool
= {
960 .read
= debugfs_read_file_bool
,
961 .write
= debugfs_write_file_bool
,
963 .llseek
= default_llseek
,
966 static const struct file_operations fops_bool_ro
= {
967 .read
= debugfs_read_file_bool
,
969 .llseek
= default_llseek
,
972 static const struct file_operations fops_bool_wo
= {
973 .write
= debugfs_write_file_bool
,
975 .llseek
= default_llseek
,
979 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
980 * @name: a pointer to a string containing the name of the file to create.
981 * @mode: the permission that the file should have
982 * @parent: a pointer to the parent dentry for this file. This should be a
983 * directory dentry if set. If this parameter is %NULL, then the
984 * file will be created in the root of the debugfs filesystem.
985 * @value: a pointer to the variable that the file should read to and write
988 * This function creates a file in debugfs with the given name that
989 * contains the value of the variable @value. If the @mode variable is so
990 * set, it can be read from, and written to.
992 void debugfs_create_bool(const char *name
, umode_t mode
, struct dentry
*parent
,
995 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_bool
,
996 &fops_bool_ro
, &fops_bool_wo
);
998 EXPORT_SYMBOL_GPL(debugfs_create_bool
);
1000 ssize_t
debugfs_read_file_str(struct file
*file
, char __user
*user_buf
,
1001 size_t count
, loff_t
*ppos
)
1003 struct dentry
*dentry
= F_DENTRY(file
);
1004 char *str
, *copy
= NULL
;
1008 ret
= debugfs_file_get(dentry
);
1012 str
= *(char **)file
->private_data
;
1013 len
= strlen(str
) + 1;
1014 copy
= kmalloc(len
, GFP_KERNEL
);
1016 debugfs_file_put(dentry
);
1020 copy_len
= strscpy(copy
, str
, len
);
1021 debugfs_file_put(dentry
);
1027 copy
[copy_len
] = '\n';
1029 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, copy
, len
);
1034 EXPORT_SYMBOL_GPL(debugfs_create_str
);
1036 static ssize_t
debugfs_write_file_str(struct file
*file
, const char __user
*user_buf
,
1037 size_t count
, loff_t
*ppos
)
1039 struct dentry
*dentry
= F_DENTRY(file
);
1040 char *old
, *new = NULL
;
1044 r
= debugfs_file_get(dentry
);
1048 old
= *(char **)file
->private_data
;
1050 /* only allow strict concatenation */
1052 if (pos
&& pos
!= strlen(old
))
1056 if (pos
+ count
+ 1 > PAGE_SIZE
)
1060 new = kmalloc(pos
+ count
+ 1, GFP_KERNEL
);
1065 memcpy(new, old
, pos
);
1068 if (copy_from_user(new + pos
, user_buf
, count
))
1071 new[pos
+ count
] = '\0';
1074 rcu_assign_pointer(*(char __rcu
**)file
->private_data
, new);
1078 debugfs_file_put(dentry
);
1083 debugfs_file_put(dentry
);
1087 static const struct file_operations fops_str
= {
1088 .read
= debugfs_read_file_str
,
1089 .write
= debugfs_write_file_str
,
1090 .open
= simple_open
,
1091 .llseek
= default_llseek
,
1094 static const struct file_operations fops_str_ro
= {
1095 .read
= debugfs_read_file_str
,
1096 .open
= simple_open
,
1097 .llseek
= default_llseek
,
1100 static const struct file_operations fops_str_wo
= {
1101 .write
= debugfs_write_file_str
,
1102 .open
= simple_open
,
1103 .llseek
= default_llseek
,
1107 * debugfs_create_str - create a debugfs file that is used to read and write a string value
1108 * @name: a pointer to a string containing the name of the file to create.
1109 * @mode: the permission that the file should have
1110 * @parent: a pointer to the parent dentry for this file. This should be a
1111 * directory dentry if set. If this parameter is %NULL, then the
1112 * file will be created in the root of the debugfs filesystem.
1113 * @value: a pointer to the variable that the file should read to and write
1116 * This function creates a file in debugfs with the given name that
1117 * contains the value of the variable @value. If the @mode variable is so
1118 * set, it can be read from, and written to.
1120 void debugfs_create_str(const char *name
, umode_t mode
,
1121 struct dentry
*parent
, char **value
)
1123 debugfs_create_mode_unsafe(name
, mode
, parent
, value
, &fops_str
,
1124 &fops_str_ro
, &fops_str_wo
);
1127 static ssize_t
read_file_blob(struct file
*file
, char __user
*user_buf
,
1128 size_t count
, loff_t
*ppos
)
1130 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
1131 struct dentry
*dentry
= F_DENTRY(file
);
1134 r
= debugfs_file_get(dentry
);
1137 r
= simple_read_from_buffer(user_buf
, count
, ppos
, blob
->data
,
1139 debugfs_file_put(dentry
);
1143 static ssize_t
write_file_blob(struct file
*file
, const char __user
*user_buf
,
1144 size_t count
, loff_t
*ppos
)
1146 struct debugfs_blob_wrapper
*blob
= file
->private_data
;
1147 struct dentry
*dentry
= F_DENTRY(file
);
1150 r
= debugfs_file_get(dentry
);
1153 r
= simple_write_to_buffer(blob
->data
, blob
->size
, ppos
, user_buf
,
1156 debugfs_file_put(dentry
);
1160 static const struct file_operations fops_blob
= {
1161 .read
= read_file_blob
,
1162 .write
= write_file_blob
,
1163 .open
= simple_open
,
1164 .llseek
= default_llseek
,
1168 * debugfs_create_blob - create a debugfs file that is used to read and write
1170 * @name: a pointer to a string containing the name of the file to create.
1171 * @mode: the permission that the file should have
1172 * @parent: a pointer to the parent dentry for this file. This should be a
1173 * directory dentry if set. If this parameter is %NULL, then the
1174 * file will be created in the root of the debugfs filesystem.
1175 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
1176 * to the blob data and the size of the data.
1178 * This function creates a file in debugfs with the given name that exports
1179 * @blob->data as a binary blob. If the @mode variable is so set it can be
1180 * read from and written to.
1182 * This function will return a pointer to a dentry if it succeeds. This
1183 * pointer must be passed to the debugfs_remove() function when the file is
1184 * to be removed (no automatic cleanup happens if your module is unloaded,
1185 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
1188 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
1191 struct dentry
*debugfs_create_blob(const char *name
, umode_t mode
,
1192 struct dentry
*parent
,
1193 struct debugfs_blob_wrapper
*blob
)
1195 return debugfs_create_file_unsafe(name
, mode
& 0644, parent
, blob
, &fops_blob
);
1197 EXPORT_SYMBOL_GPL(debugfs_create_blob
);
1199 static size_t u32_format_array(char *buf
, size_t bufsize
,
1200 u32
*array
, int array_size
)
1204 while (--array_size
>= 0) {
1206 char term
= array_size
? ' ' : '\n';
1208 len
= snprintf(buf
, bufsize
, "%u%c", *array
++, term
);
1217 static int u32_array_open(struct inode
*inode
, struct file
*file
)
1219 struct debugfs_u32_array
*data
= inode
->i_private
;
1220 int size
, elements
= data
->n_elements
;
1225 * - 10 digits + ' '/'\n' = 11 bytes per number
1226 * - terminating NUL character
1229 buf
= kmalloc(size
+1, GFP_KERNEL
);
1234 file
->private_data
= buf
;
1235 u32_format_array(buf
, size
, data
->array
, data
->n_elements
);
1237 return nonseekable_open(inode
, file
);
1240 static ssize_t
u32_array_read(struct file
*file
, char __user
*buf
, size_t len
,
1243 size_t size
= strlen(file
->private_data
);
1245 return simple_read_from_buffer(buf
, len
, ppos
,
1246 file
->private_data
, size
);
1249 static int u32_array_release(struct inode
*inode
, struct file
*file
)
1251 kfree(file
->private_data
);
1256 static const struct file_operations u32_array_fops
= {
1257 .owner
= THIS_MODULE
,
1258 .open
= u32_array_open
,
1259 .release
= u32_array_release
,
1260 .read
= u32_array_read
,
1264 * debugfs_create_u32_array - create a debugfs file that is used to read u32
1266 * @name: a pointer to a string containing the name of the file to create.
1267 * @mode: the permission that the file should have.
1268 * @parent: a pointer to the parent dentry for this file. This should be a
1269 * directory dentry if set. If this parameter is %NULL, then the
1270 * file will be created in the root of the debugfs filesystem.
1271 * @array: wrapper struct containing data pointer and size of the array.
1273 * This function creates a file in debugfs with the given name that exports
1274 * @array as data. If the @mode variable is so set it can be read from.
1275 * Writing is not supported. Seek within the file is also not supported.
1276 * Once array is created its size can not be changed.
1278 void debugfs_create_u32_array(const char *name
, umode_t mode
,
1279 struct dentry
*parent
,
1280 struct debugfs_u32_array
*array
)
1282 debugfs_create_file_unsafe(name
, mode
, parent
, array
, &u32_array_fops
);
1284 EXPORT_SYMBOL_GPL(debugfs_create_u32_array
);
1286 #ifdef CONFIG_HAS_IOMEM
1289 * The regset32 stuff is used to print 32-bit registers using the
1290 * seq_file utilities. We offer printing a register set in an already-opened
1291 * sequential file or create a debugfs file that only prints a regset32.
1295 * debugfs_print_regs32 - use seq_print to describe a set of registers
1296 * @s: the seq_file structure being used to generate output
1297 * @regs: an array if struct debugfs_reg32 structures
1298 * @nregs: the length of the above array
1299 * @base: the base address to be used in reading the registers
1300 * @prefix: a string to be prefixed to every output line
1302 * This function outputs a text block describing the current values of
1303 * some 32-bit hardware registers. It is meant to be used within debugfs
1304 * files based on seq_file that need to show registers, intermixed with other
1305 * information. The prefix argument may be used to specify a leading string,
1306 * because some peripherals have several blocks of identical registers,
1307 * for example configuration of dma channels
1309 void debugfs_print_regs32(struct seq_file
*s
, const struct debugfs_reg32
*regs
,
1310 int nregs
, void __iomem
*base
, char *prefix
)
1314 for (i
= 0; i
< nregs
; i
++, regs
++) {
1316 seq_printf(s
, "%s", prefix
);
1317 seq_printf(s
, "%s = 0x%08x\n", regs
->name
,
1318 readl(base
+ regs
->offset
));
1319 if (seq_has_overflowed(s
))
1323 EXPORT_SYMBOL_GPL(debugfs_print_regs32
);
1325 static int debugfs_regset32_show(struct seq_file
*s
, void *data
)
1327 struct debugfs_regset32
*regset
= s
->private;
1330 pm_runtime_get_sync(regset
->dev
);
1332 debugfs_print_regs32(s
, regset
->regs
, regset
->nregs
, regset
->base
, "");
1335 pm_runtime_put(regset
->dev
);
1340 DEFINE_SHOW_ATTRIBUTE(debugfs_regset32
);
1343 * debugfs_create_regset32 - create a debugfs file that returns register values
1344 * @name: a pointer to a string containing the name of the file to create.
1345 * @mode: the permission that the file should have
1346 * @parent: a pointer to the parent dentry for this file. This should be a
1347 * directory dentry if set. If this parameter is %NULL, then the
1348 * file will be created in the root of the debugfs filesystem.
1349 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1350 * to an array of register definitions, the array size and the base
1351 * address where the register bank is to be found.
1353 * This function creates a file in debugfs with the given name that reports
1354 * the names and values of a set of 32-bit registers. If the @mode variable
1355 * is so set it can be read from. Writing is not supported.
1357 void debugfs_create_regset32(const char *name
, umode_t mode
,
1358 struct dentry
*parent
,
1359 struct debugfs_regset32
*regset
)
1361 debugfs_create_file(name
, mode
, parent
, regset
, &debugfs_regset32_fops
);
1363 EXPORT_SYMBOL_GPL(debugfs_create_regset32
);
1365 #endif /* CONFIG_HAS_IOMEM */
1367 struct debugfs_devm_entry
{
1368 int (*read
)(struct seq_file
*seq
, void *data
);
1372 static int debugfs_devm_entry_open(struct inode
*inode
, struct file
*f
)
1374 struct debugfs_devm_entry
*entry
= inode
->i_private
;
1376 return single_open(f
, entry
->read
, entry
->dev
);
1379 static const struct file_operations debugfs_devm_entry_ops
= {
1380 .owner
= THIS_MODULE
,
1381 .open
= debugfs_devm_entry_open
,
1382 .release
= single_release
,
1388 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1390 * @dev: device related to this debugfs file.
1391 * @name: name of the debugfs file.
1392 * @parent: a pointer to the parent dentry for this file. This should be a
1393 * directory dentry if set. If this parameter is %NULL, then the
1394 * file will be created in the root of the debugfs filesystem.
1395 * @read_fn: function pointer called to print the seq_file content.
1397 void debugfs_create_devm_seqfile(struct device
*dev
, const char *name
,
1398 struct dentry
*parent
,
1399 int (*read_fn
)(struct seq_file
*s
, void *data
))
1401 struct debugfs_devm_entry
*entry
;
1406 entry
= devm_kzalloc(dev
, sizeof(*entry
), GFP_KERNEL
);
1410 entry
->read
= read_fn
;
1413 debugfs_create_file(name
, S_IRUGO
, parent
, entry
,
1414 &debugfs_devm_entry_ops
);
1416 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile
);