blob: 6d7c1a49581f7ab10d3249d4c161c483274f9ab4 [file] [log] [blame]
Greg Kroah-Hartman3bce94fd2017-11-07 16:59:23 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * file.c - part of debugfs, a tiny little debug file system
4 *
5 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
6 * Copyright (C) 2004 IBM Inc.
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * debugfs is for people to use instead of /proc or /sys.
Mauro Carvalho Chehabe1b4fc72017-05-14 12:04:55 -03009 * See Documentation/filesystems/ for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/fs.h>
Alessandro Rubini1a087c62011-11-18 14:50:21 +010014#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/pagemap.h>
16#include <linux/debugfs.h>
Alessandro Rubini03e099f2011-11-21 10:01:40 +010017#include <linux/io.h>
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +053018#include <linux/slab.h>
Seth Jennings3a76e5e2013-06-03 15:33:02 -050019#include <linux/atomic.h>
Arend van Spriel98210b72014-11-09 11:31:58 +010020#include <linux/device.h>
Geert Uytterhoeven30332ee2020-02-11 19:18:55 +010021#include <linux/pm_runtime.h>
Al Virocfe39442018-02-01 12:14:57 -050022#include <linux/poll.h>
David Howells54961972019-08-19 17:18:02 -070023#include <linux/security.h>
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010024
25#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Nicolai Stange49d200d2016-03-22 14:11:14 +010027struct poll_table_struct;
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static ssize_t default_read_file(struct file *file, char __user *buf,
30 size_t count, loff_t *ppos)
31{
32 return 0;
33}
34
35static ssize_t default_write_file(struct file *file, const char __user *buf,
36 size_t count, loff_t *ppos)
37{
38 return count;
39}
40
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010041const struct file_operations debugfs_noop_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .read = default_read_file,
43 .write = default_write_file,
Stephen Boyd234e3402012-04-05 14:25:11 -070044 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +020045 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Nicolai Stange9fd4dce2016-03-22 14:11:13 +010048#define F_DENTRY(filp) ((filp)->f_path.dentry)
49
Nicolai Stange7c8d4692017-10-31 00:15:47 +010050const struct file_operations *debugfs_real_fops(const struct file *filp)
Nicolai Stange7c8d4692017-10-31 00:15:47 +010051{
52 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
Nicolai Stange055ab8e2017-10-31 00:15:49 +010053
Nicolai Stange7d39bc52017-10-31 00:15:54 +010054 if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
55 /*
56 * Urgh, we've been called w/o a protecting
57 * debugfs_file_get().
58 */
59 WARN_ON(1);
60 return NULL;
61 }
62
Nicolai Stange7c8d4692017-10-31 00:15:47 +010063 return fsd->real_fops;
64}
65EXPORT_SYMBOL_GPL(debugfs_real_fops);
66
Nicolai Stangee9117a52017-10-31 00:15:48 +010067/**
68 * debugfs_file_get - mark the beginning of file data access
69 * @dentry: the dentry object whose data is being accessed.
70 *
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.
77 *
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.
81 */
82int debugfs_file_get(struct dentry *dentry)
83{
Nicolai Stange7d39bc52017-10-31 00:15:54 +010084 struct debugfs_fsdata *fsd;
85 void *d_fsd;
Nicolai Stangee9117a52017-10-31 00:15:48 +010086
Johannes Berg0ed04a12023-11-24 17:25:24 +010087 /*
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
90 * them know about it.
91 */
92 if (WARN_ON(!d_is_reg(dentry)))
93 return -EINVAL;
94
Nicolai Stange7d39bc52017-10-31 00:15:54 +010095 d_fsd = READ_ONCE(dentry->d_fsdata);
96 if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
97 fsd = d_fsd;
98 } else {
99 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
100 if (!fsd)
101 return -ENOMEM;
102
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);
Johannes Berg159f5bd2023-12-21 15:04:45 +0100107 INIT_LIST_HEAD(&fsd->cancellations);
108 mutex_init(&fsd->cancellations_mtx);
109
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100110 if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
Johannes Berg159f5bd2023-12-21 15:04:45 +0100111 mutex_destroy(&fsd->cancellations_mtx);
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100112 kfree(fsd);
113 fsd = READ_ONCE(dentry->d_fsdata);
114 }
115 }
116
117 /*
118 * In case of a successful cmpxchg() above, this check is
119 * strictly necessary and must follow it, see the comment in
120 * __debugfs_remove_file().
121 * OTOH, if the cmpxchg() hasn't been executed or wasn't
122 * successful, this serves the purpose of not starving
123 * removers.
124 */
Nicolai Stangee9117a52017-10-31 00:15:48 +0100125 if (d_unlinked(dentry))
126 return -EIO;
127
128 if (!refcount_inc_not_zero(&fsd->active_users))
129 return -EIO;
130
131 return 0;
132}
133EXPORT_SYMBOL_GPL(debugfs_file_get);
134
135/**
136 * debugfs_file_put - mark the end of file data access
137 * @dentry: the dentry object formerly passed to
138 * debugfs_file_get().
139 *
140 * Allow any ongoing concurrent call into debugfs_remove() or
141 * debugfs_remove_recursive() blocked by a former call to
142 * debugfs_file_get() to proceed and return to its caller.
143 */
144void debugfs_file_put(struct dentry *dentry)
145{
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100146 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
Nicolai Stangee9117a52017-10-31 00:15:48 +0100147
148 if (refcount_dec_and_test(&fsd->active_users))
149 complete(&fsd->active_users_drained);
150}
151EXPORT_SYMBOL_GPL(debugfs_file_put);
152
Johannes Berg8c88a472023-11-24 17:25:26 +0100153/**
154 * debugfs_enter_cancellation - enter a debugfs cancellation
155 * @file: the file being accessed
156 * @cancellation: the cancellation object, the cancel callback
157 * inside of it must be initialized
158 *
159 * When a debugfs file is removed it needs to wait for all active
160 * operations to complete. However, the operation itself may need
161 * to wait for hardware or completion of some asynchronous process
162 * or similar. As such, it may need to be cancelled to avoid long
163 * waits or even deadlocks.
164 *
165 * This function can be used inside a debugfs handler that may
166 * need to be cancelled. As soon as this function is called, the
167 * cancellation's 'cancel' callback may be called, at which point
168 * the caller should proceed to call debugfs_leave_cancellation()
169 * and leave the debugfs handler function as soon as possible.
170 * Note that the 'cancel' callback is only ever called in the
171 * context of some kind of debugfs_remove().
172 *
173 * This function must be paired with debugfs_leave_cancellation().
174 */
175void debugfs_enter_cancellation(struct file *file,
176 struct debugfs_cancellation *cancellation)
177{
178 struct debugfs_fsdata *fsd;
179 struct dentry *dentry = F_DENTRY(file);
180
181 INIT_LIST_HEAD(&cancellation->list);
182
183 if (WARN_ON(!d_is_reg(dentry)))
184 return;
185
186 if (WARN_ON(!cancellation->cancel))
187 return;
188
189 fsd = READ_ONCE(dentry->d_fsdata);
190 if (WARN_ON(!fsd ||
191 ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
192 return;
193
194 mutex_lock(&fsd->cancellations_mtx);
195 list_add(&cancellation->list, &fsd->cancellations);
196 mutex_unlock(&fsd->cancellations_mtx);
197
198 /* if we're already removing wake it up to cancel */
199 if (d_unlinked(dentry))
200 complete(&fsd->active_users_drained);
201}
202EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
203
204/**
205 * debugfs_leave_cancellation - leave cancellation section
206 * @file: the file being accessed
207 * @cancellation: the cancellation previously registered with
208 * debugfs_enter_cancellation()
209 *
210 * See the documentation of debugfs_enter_cancellation().
211 */
212void debugfs_leave_cancellation(struct file *file,
213 struct debugfs_cancellation *cancellation)
214{
215 struct debugfs_fsdata *fsd;
216 struct dentry *dentry = F_DENTRY(file);
217
218 if (WARN_ON(!d_is_reg(dentry)))
219 return;
220
221 fsd = READ_ONCE(dentry->d_fsdata);
222 if (WARN_ON(!fsd ||
223 ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
224 return;
225
226 mutex_lock(&fsd->cancellations_mtx);
227 if (!list_empty(&cancellation->list))
228 list_del(&cancellation->list);
229 mutex_unlock(&fsd->cancellations_mtx);
230}
231EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
232
David Howells54961972019-08-19 17:18:02 -0700233/*
234 * Only permit access to world-readable files when the kernel is locked down.
235 * We also need to exclude any file that has ways to write or alter it as root
236 * can bypass the permissions check.
237 */
Eric Snowberga37f4952019-12-07 11:16:03 -0500238static int debugfs_locked_down(struct inode *inode,
239 struct file *filp,
240 const struct file_operations *real_fops)
David Howells54961972019-08-19 17:18:02 -0700241{
Michal Suchanek358fcf52022-01-04 18:05:05 +0100242 if ((inode->i_mode & 07777 & ~0444) == 0 &&
David Howells54961972019-08-19 17:18:02 -0700243 !(filp->f_mode & FMODE_WRITE) &&
244 !real_fops->unlocked_ioctl &&
245 !real_fops->compat_ioctl &&
246 !real_fops->mmap)
Eric Snowberga37f4952019-12-07 11:16:03 -0500247 return 0;
David Howells54961972019-08-19 17:18:02 -0700248
Eric Snowberga37f4952019-12-07 11:16:03 -0500249 if (security_locked_down(LOCKDOWN_DEBUGFS))
250 return -EPERM;
251
252 return 0;
David Howells54961972019-08-19 17:18:02 -0700253}
254
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100255static int open_proxy_open(struct inode *inode, struct file *filp)
256{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100257 struct dentry *dentry = F_DENTRY(filp);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100258 const struct file_operations *real_fops = NULL;
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100259 int r;
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100260
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100261 r = debugfs_file_get(dentry);
262 if (r)
263 return r == -EIO ? -ENOENT : r;
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100264
Christian Lamparter86f0e062016-09-17 21:43:01 +0200265 real_fops = debugfs_real_fops(filp);
David Howells54961972019-08-19 17:18:02 -0700266
Eric Snowberga37f4952019-12-07 11:16:03 -0500267 r = debugfs_locked_down(inode, filp, real_fops);
David Howells54961972019-08-19 17:18:02 -0700268 if (r)
269 goto out;
270
Taehee Yoo275678e2020-02-18 04:31:50 +0000271 if (!fops_get(real_fops)) {
Vladis Dronove3b9fc72020-08-11 17:01:29 +0200272#ifdef CONFIG_MODULES
Taehee Yoo275678e2020-02-18 04:31:50 +0000273 if (real_fops->owner &&
Sven Eckelmann112cedc2021-08-02 18:24:44 +0200274 real_fops->owner->state == MODULE_STATE_GOING) {
275 r = -ENXIO;
Taehee Yoo275678e2020-02-18 04:31:50 +0000276 goto out;
Sven Eckelmann112cedc2021-08-02 18:24:44 +0200277 }
Taehee Yoo275678e2020-02-18 04:31:50 +0000278#endif
279
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100280 /* Huh? Module did not clean up after itself at exit? */
281 WARN(1, "debugfs file owner did not clean up at exit: %pd",
282 dentry);
283 r = -ENXIO;
284 goto out;
285 }
286 replace_fops(filp, real_fops);
287
288 if (real_fops->open)
289 r = real_fops->open(inode, filp);
290
291out:
Nicolai Stange69d29f92017-10-31 00:15:50 +0100292 debugfs_file_put(dentry);
Nicolai Stange9fd4dce2016-03-22 14:11:13 +0100293 return r;
294}
295
296const struct file_operations debugfs_open_proxy_file_operations = {
297 .open = open_proxy_open,
298};
299
Nicolai Stange49d200d2016-03-22 14:11:14 +0100300#define PROTO(args...) args
301#define ARGS(args...) args
302
303#define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
304static ret_type full_proxy_ ## name(proto) \
305{ \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100306 struct dentry *dentry = F_DENTRY(filp); \
Nicolai Stange154b9d72017-10-31 00:15:53 +0100307 const struct file_operations *real_fops; \
Nicolai Stange49d200d2016-03-22 14:11:14 +0100308 ret_type r; \
309 \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100310 r = debugfs_file_get(dentry); \
311 if (unlikely(r)) \
312 return r; \
Nicolai Stange154b9d72017-10-31 00:15:53 +0100313 real_fops = debugfs_real_fops(filp); \
Nicolai Stange69d29f92017-10-31 00:15:50 +0100314 r = real_fops->name(args); \
315 debugfs_file_put(dentry); \
Nicolai Stange49d200d2016-03-22 14:11:14 +0100316 return r; \
317}
318
319FULL_PROXY_FUNC(llseek, loff_t, filp,
320 PROTO(struct file *filp, loff_t offset, int whence),
321 ARGS(filp, offset, whence));
322
323FULL_PROXY_FUNC(read, ssize_t, filp,
324 PROTO(struct file *filp, char __user *buf, size_t size,
325 loff_t *ppos),
326 ARGS(filp, buf, size, ppos));
327
328FULL_PROXY_FUNC(write, ssize_t, filp,
329 PROTO(struct file *filp, const char __user *buf, size_t size,
330 loff_t *ppos),
331 ARGS(filp, buf, size, ppos));
332
333FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
334 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
335 ARGS(filp, cmd, arg));
336
Al Viro076ccb72017-07-03 01:02:18 -0400337static __poll_t full_proxy_poll(struct file *filp,
Nicolai Stange49d200d2016-03-22 14:11:14 +0100338 struct poll_table_struct *wait)
339{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100340 struct dentry *dentry = F_DENTRY(filp);
Al Viroe6c8adc2017-07-03 22:25:56 -0400341 __poll_t r = 0;
Nicolai Stange154b9d72017-10-31 00:15:53 +0100342 const struct file_operations *real_fops;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100343
Nicolai Stange69d29f92017-10-31 00:15:50 +0100344 if (debugfs_file_get(dentry))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800345 return EPOLLHUP;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100346
Nicolai Stange154b9d72017-10-31 00:15:53 +0100347 real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100348 r = real_fops->poll(filp, wait);
Nicolai Stange69d29f92017-10-31 00:15:50 +0100349 debugfs_file_put(dentry);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100350 return r;
351}
352
353static int full_proxy_release(struct inode *inode, struct file *filp)
354{
355 const struct dentry *dentry = F_DENTRY(filp);
Christian Lamparter86f0e062016-09-17 21:43:01 +0200356 const struct file_operations *real_fops = debugfs_real_fops(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100357 const struct file_operations *proxy_fops = filp->f_op;
358 int r = 0;
359
360 /*
361 * We must not protect this against removal races here: the
362 * original releaser should be called unconditionally in order
363 * not to leak any resources. Releasers must not assume that
364 * ->i_private is still being meaningful here.
365 */
366 if (real_fops->release)
367 r = real_fops->release(inode, filp);
368
369 replace_fops(filp, d_inode(dentry)->i_fop);
Xu Wangc80a67b2020-07-09 05:40:33 +0000370 kfree(proxy_fops);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100371 fops_put(real_fops);
Eric Engestroma1a9e5d2016-09-21 10:27:36 +0100372 return r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100373}
374
375static void __full_proxy_fops_init(struct file_operations *proxy_fops,
376 const struct file_operations *real_fops)
377{
378 proxy_fops->release = full_proxy_release;
379 if (real_fops->llseek)
380 proxy_fops->llseek = full_proxy_llseek;
381 if (real_fops->read)
382 proxy_fops->read = full_proxy_read;
383 if (real_fops->write)
384 proxy_fops->write = full_proxy_write;
385 if (real_fops->poll)
386 proxy_fops->poll = full_proxy_poll;
387 if (real_fops->unlocked_ioctl)
388 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
389}
390
391static int full_proxy_open(struct inode *inode, struct file *filp)
392{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100393 struct dentry *dentry = F_DENTRY(filp);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100394 const struct file_operations *real_fops = NULL;
395 struct file_operations *proxy_fops = NULL;
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100396 int r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100397
Nicolai Stange7d39bc52017-10-31 00:15:54 +0100398 r = debugfs_file_get(dentry);
399 if (r)
400 return r == -EIO ? -ENOENT : r;
Nicolai Stange49d200d2016-03-22 14:11:14 +0100401
Christian Lamparter86f0e062016-09-17 21:43:01 +0200402 real_fops = debugfs_real_fops(filp);
David Howells54961972019-08-19 17:18:02 -0700403
Eric Snowberga37f4952019-12-07 11:16:03 -0500404 r = debugfs_locked_down(inode, filp, real_fops);
David Howells54961972019-08-19 17:18:02 -0700405 if (r)
406 goto out;
407
Taehee Yoo275678e2020-02-18 04:31:50 +0000408 if (!fops_get(real_fops)) {
Vladis Dronove3b9fc72020-08-11 17:01:29 +0200409#ifdef CONFIG_MODULES
Taehee Yoo275678e2020-02-18 04:31:50 +0000410 if (real_fops->owner &&
Sven Eckelmann112cedc2021-08-02 18:24:44 +0200411 real_fops->owner->state == MODULE_STATE_GOING) {
412 r = -ENXIO;
Taehee Yoo275678e2020-02-18 04:31:50 +0000413 goto out;
Sven Eckelmann112cedc2021-08-02 18:24:44 +0200414 }
Taehee Yoo275678e2020-02-18 04:31:50 +0000415#endif
416
Nicolai Stange49d200d2016-03-22 14:11:14 +0100417 /* Huh? Module did not cleanup after itself at exit? */
418 WARN(1, "debugfs file owner did not clean up at exit: %pd",
419 dentry);
420 r = -ENXIO;
421 goto out;
422 }
423
424 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
425 if (!proxy_fops) {
426 r = -ENOMEM;
427 goto free_proxy;
428 }
429 __full_proxy_fops_init(proxy_fops, real_fops);
430 replace_fops(filp, proxy_fops);
431
432 if (real_fops->open) {
433 r = real_fops->open(inode, filp);
Nicolai Stangeb10e3e92016-05-24 13:08:53 +0200434 if (r) {
435 replace_fops(filp, d_inode(dentry)->i_fop);
436 goto free_proxy;
437 } else if (filp->f_op != proxy_fops) {
Nicolai Stange49d200d2016-03-22 14:11:14 +0100438 /* No protection against file removal anymore. */
439 WARN(1, "debugfs file owner replaced proxy fops: %pd",
440 dentry);
441 goto free_proxy;
442 }
443 }
444
445 goto out;
446free_proxy:
447 kfree(proxy_fops);
448 fops_put(real_fops);
449out:
Nicolai Stange69d29f92017-10-31 00:15:50 +0100450 debugfs_file_put(dentry);
Nicolai Stange49d200d2016-03-22 14:11:14 +0100451 return r;
452}
453
454const struct file_operations debugfs_full_proxy_file_operations = {
455 .open = full_proxy_open,
456};
457
Nicolai Stangec6468802016-03-22 14:11:15 +0100458ssize_t debugfs_attr_read(struct file *file, char __user *buf,
459 size_t len, loff_t *ppos)
460{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100461 struct dentry *dentry = F_DENTRY(file);
Nicolai Stangec6468802016-03-22 14:11:15 +0100462 ssize_t ret;
Nicolai Stangec6468802016-03-22 14:11:15 +0100463
Nicolai Stange69d29f92017-10-31 00:15:50 +0100464 ret = debugfs_file_get(dentry);
465 if (unlikely(ret))
466 return ret;
467 ret = simple_attr_read(file, buf, len, ppos);
468 debugfs_file_put(dentry);
Nicolai Stangec6468802016-03-22 14:11:15 +0100469 return ret;
470}
471EXPORT_SYMBOL_GPL(debugfs_attr_read);
472
Akinobu Mitad472cf72022-09-20 02:24:18 +0900473static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
474 size_t len, loff_t *ppos, bool is_signed)
Nicolai Stangec6468802016-03-22 14:11:15 +0100475{
Nicolai Stange69d29f92017-10-31 00:15:50 +0100476 struct dentry *dentry = F_DENTRY(file);
Nicolai Stangec6468802016-03-22 14:11:15 +0100477 ssize_t ret;
Nicolai Stangec6468802016-03-22 14:11:15 +0100478
Nicolai Stange69d29f92017-10-31 00:15:50 +0100479 ret = debugfs_file_get(dentry);
480 if (unlikely(ret))
481 return ret;
Akinobu Mitad472cf72022-09-20 02:24:18 +0900482 if (is_signed)
483 ret = simple_attr_write_signed(file, buf, len, ppos);
484 else
485 ret = simple_attr_write(file, buf, len, ppos);
Nicolai Stange69d29f92017-10-31 00:15:50 +0100486 debugfs_file_put(dentry);
Nicolai Stangec6468802016-03-22 14:11:15 +0100487 return ret;
488}
Akinobu Mitad472cf72022-09-20 02:24:18 +0900489
490ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
491 size_t len, loff_t *ppos)
492{
493 return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
494}
Nicolai Stangec6468802016-03-22 14:11:15 +0100495EXPORT_SYMBOL_GPL(debugfs_attr_write);
496
Akinobu Mitad472cf72022-09-20 02:24:18 +0900497ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
498 size_t len, loff_t *ppos)
499{
500 return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
501}
502EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
503
Nicolai Stange4909f162016-03-22 14:11:17 +0100504static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
505 struct dentry *parent, void *value,
506 const struct file_operations *fops,
507 const struct file_operations *fops_ro,
508 const struct file_operations *fops_wo)
509{
510 /* if there are no write bits set, make read only */
511 if (!(mode & S_IWUGO))
512 return debugfs_create_file_unsafe(name, mode, parent, value,
513 fops_ro);
514 /* if there are no read bits set, make write only */
515 if (!(mode & S_IRUGO))
516 return debugfs_create_file_unsafe(name, mode, parent, value,
517 fops_wo);
518
519 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
520}
521
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800522static int debugfs_u8_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200523{
524 *(u8 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800525 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200526}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800527static int debugfs_u8_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200528{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800529 *val = *(u8 *)data;
530 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200531}
Nicolai Stange4909f162016-03-22 14:11:17 +0100532DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
533DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
534DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700537 * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * @name: a pointer to a string containing the name of the file to create.
539 * @mode: the permission that the file should have
540 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700541 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * file will be created in the root of the debugfs filesystem.
543 * @value: a pointer to the variable that the file should read to and write
544 * from.
545 *
546 * This function creates a file in debugfs with the given name that
547 * contains the value of the variable @value. If the @mode variable is so
548 * set, it can be read from, and written to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200550void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
551 u8 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Greg Kroah-Hartman9655ac42019-10-11 15:29:24 +0200553 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700554 &fops_u8_ro, &fops_u8_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556EXPORT_SYMBOL_GPL(debugfs_create_u8);
557
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800558static int debugfs_u16_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200559{
560 *(u16 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800561 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200562}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800563static int debugfs_u16_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200564{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800565 *val = *(u16 *)data;
566 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200567}
Nicolai Stange4909f162016-03-22 14:11:17 +0100568DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
569DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
570DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700573 * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * @name: a pointer to a string containing the name of the file to create.
575 * @mode: the permission that the file should have
576 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700577 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * file will be created in the root of the debugfs filesystem.
579 * @value: a pointer to the variable that the file should read to and write
580 * from.
581 *
582 * This function creates a file in debugfs with the given name that
583 * contains the value of the variable @value. If the @mode variable is so
584 * set, it can be read from, and written to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200586void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
587 u16 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Greg Kroah-Hartman313f5db2019-10-11 15:29:25 +0200589 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700590 &fops_u16_ro, &fops_u16_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592EXPORT_SYMBOL_GPL(debugfs_create_u16);
593
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800594static int debugfs_u32_set(void *data, u64 val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200595{
596 *(u32 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800597 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200598}
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800599static int debugfs_u32_get(void *data, u64 *val)
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200600{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800601 *val = *(u32 *)data;
602 return 0;
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200603}
Nicolai Stange4909f162016-03-22 14:11:17 +0100604DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
605DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
606DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
Arnd Bergmannacaefc22005-05-18 14:40:59 +0200607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700609 * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 * @name: a pointer to a string containing the name of the file to create.
611 * @mode: the permission that the file should have
612 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700613 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * file will be created in the root of the debugfs filesystem.
615 * @value: a pointer to the variable that the file should read to and write
616 * from.
617 *
618 * This function creates a file in debugfs with the given name that
619 * contains the value of the variable @value. If the @mode variable is so
620 * set, it can be read from, and written to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Greg Kroah-Hartman2b070212020-04-16 16:54:48 +0200622void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
623 u32 *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Greg Kroah-Hartman2b070212020-04-16 16:54:48 +0200625 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700626 &fops_u32_ro, &fops_u32_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628EXPORT_SYMBOL_GPL(debugfs_create_u32);
629
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800630static int debugfs_u64_set(void *data, u64 val)
Michael Ellerman84478912007-04-17 15:59:36 +1000631{
632 *(u64 *)data = val;
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800633 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000634}
635
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800636static int debugfs_u64_get(void *data, u64 *val)
Michael Ellerman84478912007-04-17 15:59:36 +1000637{
Christoph Hellwig8b88b092008-02-08 04:20:26 -0800638 *val = *(u64 *)data;
639 return 0;
Michael Ellerman84478912007-04-17 15:59:36 +1000640}
Nicolai Stange4909f162016-03-22 14:11:17 +0100641DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
642DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
643DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
Michael Ellerman84478912007-04-17 15:59:36 +1000644
645/**
646 * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
647 * @name: a pointer to a string containing the name of the file to create.
648 * @mode: the permission that the file should have
649 * @parent: a pointer to the parent dentry for this file. This should be a
650 * directory dentry if set. If this parameter is %NULL, then the
651 * file will be created in the root of the debugfs filesystem.
652 * @value: a pointer to the variable that the file should read to and write
653 * from.
654 *
655 * This function creates a file in debugfs with the given name that
656 * contains the value of the variable @value. If the @mode variable is so
657 * set, it can be read from, and written to.
Michael Ellerman84478912007-04-17 15:59:36 +1000658 */
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200659void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
660 u64 *value)
Michael Ellerman84478912007-04-17 15:59:36 +1000661{
Greg Kroah-Hartmanad262212019-10-11 15:29:26 +0200662 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
Stephen Boydb97f6792015-10-12 18:09:09 -0700663 &fops_u64_ro, &fops_u64_wo);
Michael Ellerman84478912007-04-17 15:59:36 +1000664}
665EXPORT_SYMBOL_GPL(debugfs_create_u64);
666
Viresh Kumarc23fe832015-10-18 22:43:19 +0530667static int debugfs_ulong_set(void *data, u64 val)
668{
669 *(unsigned long *)data = val;
670 return 0;
671}
672
673static int debugfs_ulong_get(void *data, u64 *val)
674{
675 *val = *(unsigned long *)data;
676 return 0;
677}
Nicolai Stange4909f162016-03-22 14:11:17 +0100678DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
679 "%llu\n");
680DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
681DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
Viresh Kumarc23fe832015-10-18 22:43:19 +0530682
683/**
684 * debugfs_create_ulong - create a debugfs file that is used to read and write
685 * an unsigned long value.
686 * @name: a pointer to a string containing the name of the file to create.
687 * @mode: the permission that the file should have
688 * @parent: a pointer to the parent dentry for this file. This should be a
689 * directory dentry if set. If this parameter is %NULL, then the
690 * file will be created in the root of the debugfs filesystem.
691 * @value: a pointer to the variable that the file should read to and write
692 * from.
693 *
694 * This function creates a file in debugfs with the given name that
695 * contains the value of the variable @value. If the @mode variable is so
696 * set, it can be read from, and written to.
Viresh Kumarc23fe832015-10-18 22:43:19 +0530697 */
Greg Kroah-Hartmanfb05b142021-05-21 20:43:40 +0200698void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
699 unsigned long *value)
Viresh Kumarc23fe832015-10-18 22:43:19 +0530700{
Greg Kroah-Hartmanfb05b142021-05-21 20:43:40 +0200701 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
702 &fops_ulong_ro, &fops_ulong_wo);
Viresh Kumarc23fe832015-10-18 22:43:19 +0530703}
704EXPORT_SYMBOL_GPL(debugfs_create_ulong);
705
Nicolai Stange4909f162016-03-22 14:11:17 +0100706DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
707DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
708DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400709
Nicolai Stange4909f162016-03-22 14:11:17 +0100710DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
711 "0x%04llx\n");
712DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
713DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400714
Nicolai Stange4909f162016-03-22 14:11:17 +0100715DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
716 "0x%08llx\n");
717DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
718DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
Robin Getz2ebefc52007-08-02 18:23:50 -0400719
Nicolai Stange4909f162016-03-22 14:11:17 +0100720DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
721 "0x%016llx\n");
722DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
723DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
Huang Ying15b0bea2010-05-18 14:35:23 +0800724
Randy Dunlape6716b82007-10-15 17:30:19 -0700725/*
Huang Ying15b0bea2010-05-18 14:35:23 +0800726 * 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
Randy Dunlape6716b82007-10-15 17:30:19 -0700727 *
728 * These functions are exactly the same as the above functions (but use a hex
729 * output for the decimal challenged). For details look at the above unsigned
730 * decimal functions.
731 */
732
Robin Getz2ebefc52007-08-02 18:23:50 -0400733/**
734 * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
Randy Dunlape6716b82007-10-15 17:30:19 -0700735 * @name: a pointer to a string containing the name of the file to create.
736 * @mode: the permission that the file should have
737 * @parent: a pointer to the parent dentry for this file. This should be a
738 * directory dentry if set. If this parameter is %NULL, then the
739 * file will be created in the root of the debugfs filesystem.
740 * @value: a pointer to the variable that the file should read to and write
741 * from.
Robin Getz2ebefc52007-08-02 18:23:50 -0400742 */
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200743void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
744 u8 *value)
Robin Getz2ebefc52007-08-02 18:23:50 -0400745{
Greg Kroah-Hartmanc7c11682019-10-11 15:29:28 +0200746 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
Stephen Boydb97f6792015-10-12 18:09:09 -0700747 &fops_x8_ro, &fops_x8_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400748}
749EXPORT_SYMBOL_GPL(debugfs_create_x8);
750
Randy Dunlape6716b82007-10-15 17:30:19 -0700751/**
752 * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
753 * @name: a pointer to a string containing the name of the file to create.
754 * @mode: the permission that the file should have
755 * @parent: a pointer to the parent dentry for this file. This should be a
756 * directory dentry if set. If this parameter is %NULL, then the
757 * file will be created in the root of the debugfs filesystem.
758 * @value: a pointer to the variable that the file should read to and write
759 * from.
760 */
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200761void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
762 u16 *value)
Robin Getz2ebefc52007-08-02 18:23:50 -0400763{
Greg Kroah-Hartmane40d38f2019-10-11 15:29:29 +0200764 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
Stephen Boydb97f6792015-10-12 18:09:09 -0700765 &fops_x16_ro, &fops_x16_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400766}
767EXPORT_SYMBOL_GPL(debugfs_create_x16);
768
Randy Dunlape6716b82007-10-15 17:30:19 -0700769/**
770 * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
771 * @name: a pointer to a string containing the name of the file to create.
772 * @mode: the permission that the file should have
773 * @parent: a pointer to the parent dentry for this file. This should be a
774 * directory dentry if set. If this parameter is %NULL, then the
775 * file will be created in the root of the debugfs filesystem.
776 * @value: a pointer to the variable that the file should read to and write
777 * from.
778 */
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200779void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
780 u32 *value)
Robin Getz2ebefc52007-08-02 18:23:50 -0400781{
Greg Kroah-Hartmanf5cb0a72019-10-11 15:29:30 +0200782 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
Stephen Boydb97f6792015-10-12 18:09:09 -0700783 &fops_x32_ro, &fops_x32_wo);
Robin Getz2ebefc52007-08-02 18:23:50 -0400784}
785EXPORT_SYMBOL_GPL(debugfs_create_x32);
786
Huang Ying15b0bea2010-05-18 14:35:23 +0800787/**
788 * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
789 * @name: a pointer to a string containing the name of the file to create.
790 * @mode: the permission that the file should have
791 * @parent: a pointer to the parent dentry for this file. This should be a
792 * directory dentry if set. If this parameter is %NULL, then the
793 * file will be created in the root of the debugfs filesystem.
794 * @value: a pointer to the variable that the file should read to and write
795 * from.
796 */
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200797void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
798 u64 *value)
Huang Ying15b0bea2010-05-18 14:35:23 +0800799{
Greg Kroah-Hartman0864c402019-10-11 15:29:31 +0200800 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
Stephen Boyd82b7d4f2015-10-12 18:09:10 -0700801 &fops_x64_ro, &fops_x64_wo);
Huang Ying15b0bea2010-05-18 14:35:23 +0800802}
803EXPORT_SYMBOL_GPL(debugfs_create_x64);
804
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800805
806static int debugfs_size_t_set(void *data, u64 val)
807{
808 *(size_t *)data = val;
809 return 0;
810}
811static int debugfs_size_t_get(void *data, u64 *val)
812{
813 *val = *(size_t *)data;
814 return 0;
815}
Nicolai Stange4909f162016-03-22 14:11:17 +0100816DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
817 "%llu\n"); /* %llu and %zu are more or less the same */
818DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
819DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800820
821/**
822 * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
823 * @name: a pointer to a string containing the name of the file to create.
824 * @mode: the permission that the file should have
825 * @parent: a pointer to the parent dentry for this file. This should be a
826 * directory dentry if set. If this parameter is %NULL, then the
827 * file will be created in the root of the debugfs filesystem.
828 * @value: a pointer to the variable that the file should read to and write
829 * from.
830 */
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200831void debugfs_create_size_t(const char *name, umode_t mode,
832 struct dentry *parent, size_t *value)
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800833{
Greg Kroah-Hartman8e580262019-10-11 15:29:27 +0200834 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
835 &fops_size_t_ro, &fops_size_t_wo);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800836}
837EXPORT_SYMBOL_GPL(debugfs_create_size_t);
838
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500839static int debugfs_atomic_t_set(void *data, u64 val)
840{
841 atomic_set((atomic_t *)data, val);
842 return 0;
843}
844static int debugfs_atomic_t_get(void *data, u64 *val)
845{
846 *val = atomic_read((atomic_t *)data);
847 return 0;
848}
Akinobu Mitad472cf72022-09-20 02:24:18 +0900849DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500850 debugfs_atomic_t_set, "%lld\n");
Akinobu Mitad472cf72022-09-20 02:24:18 +0900851DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
Nicolai Stange4909f162016-03-22 14:11:17 +0100852 "%lld\n");
Akinobu Mitad472cf72022-09-20 02:24:18 +0900853DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
Nicolai Stange4909f162016-03-22 14:11:17 +0100854 "%lld\n");
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500855
856/**
857 * debugfs_create_atomic_t - create a debugfs file that is used to read and
858 * write an atomic_t value
859 * @name: a pointer to a string containing the name of the file to create.
860 * @mode: the permission that the file should have
861 * @parent: a pointer to the parent dentry for this file. This should be a
862 * directory dentry if set. If this parameter is %NULL, then the
863 * file will be created in the root of the debugfs filesystem.
864 * @value: a pointer to the variable that the file should read to and write
865 * from.
866 */
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700867void debugfs_create_atomic_t(const char *name, umode_t mode,
868 struct dentry *parent, atomic_t *value)
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500869{
Greg Kroah-Hartman9927c6f2019-10-16 06:03:32 -0700870 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
871 &fops_atomic_t_ro, &fops_atomic_t_wo);
Seth Jennings3a76e5e2013-06-03 15:33:02 -0500872}
873EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
Inaky Perez-Gonzalez5e078782008-12-20 16:57:39 -0800874
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100875ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
876 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Rasmus Villemoesc8a9c282021-03-26 16:14:11 +0100878 char buf[2];
Nicolai Stange4d45f792016-03-22 14:11:18 +0100879 bool val;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100880 int r;
881 struct dentry *dentry = F_DENTRY(file);
Rahul Bedarkar88e412e2014-06-06 23:12:04 +0530882
Nicolai Stange69d29f92017-10-31 00:15:50 +0100883 r = debugfs_file_get(dentry);
884 if (unlikely(r))
Nicolai Stange4d45f792016-03-22 14:11:18 +0100885 return r;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100886 val = *(bool *)file->private_data;
887 debugfs_file_put(dentry);
Nicolai Stange4d45f792016-03-22 14:11:18 +0100888
889 if (val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 buf[0] = 'Y';
891 else
892 buf[0] = 'N';
893 buf[1] = '\n';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
895}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100896EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100898ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
899 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Jonathan Cameron8705b482011-04-19 12:43:46 +0100901 bool bv;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100902 int r;
Viresh Kumar621a5f72015-09-26 15:04:07 -0700903 bool *val = file->private_data;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100904 struct dentry *dentry = F_DENTRY(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Andy Shevchenko964f8362018-05-03 19:17:52 +0300906 r = kstrtobool_from_user(user_buf, count, &bv);
907 if (!r) {
Nicolai Stange69d29f92017-10-31 00:15:50 +0100908 r = debugfs_file_get(dentry);
909 if (unlikely(r))
Nicolai Stange4d45f792016-03-22 14:11:18 +0100910 return r;
Nicolai Stange69d29f92017-10-31 00:15:50 +0100911 *val = bv;
912 debugfs_file_put(dentry);
Nicolai Stange4d45f792016-03-22 14:11:18 +0100913 }
Jonathan Cameron8705b482011-04-19 12:43:46 +0100914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return count;
916}
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100917EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800919static const struct file_operations fops_bool = {
Richard Fitzgerald0642ef62015-06-23 14:32:54 +0100920 .read = debugfs_read_file_bool,
921 .write = debugfs_write_file_bool,
Stephen Boyd234e3402012-04-05 14:25:11 -0700922 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200923 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924};
925
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700926static const struct file_operations fops_bool_ro = {
927 .read = debugfs_read_file_bool,
928 .open = simple_open,
929 .llseek = default_llseek,
930};
931
932static const struct file_operations fops_bool_wo = {
933 .write = debugfs_write_file_bool,
934 .open = simple_open,
935 .llseek = default_llseek,
936};
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/**
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700939 * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * @name: a pointer to a string containing the name of the file to create.
941 * @mode: the permission that the file should have
942 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -0700943 * directory dentry if set. If this parameter is %NULL, then the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * file will be created in the root of the debugfs filesystem.
945 * @value: a pointer to the variable that the file should read to and write
946 * from.
947 *
948 * This function creates a file in debugfs with the given name that
949 * contains the value of the variable @value. If the @mode variable is so
950 * set, it can be read from, and written to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Greg Kroah-Hartman393b0632021-05-21 20:45:19 +0200952void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
953 bool *value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Greg Kroah-Hartman393b0632021-05-21 20:45:19 +0200955 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
Stephen Boyd6713e8f2015-10-12 18:09:12 -0700956 &fops_bool_ro, &fops_bool_wo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958EXPORT_SYMBOL_GPL(debugfs_create_bool);
959
Peter Zijlstra9af04402021-03-25 10:53:55 +0100960ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
961 size_t count, loff_t *ppos)
962{
963 struct dentry *dentry = F_DENTRY(file);
964 char *str, *copy = NULL;
965 int copy_len, len;
966 ssize_t ret;
967
968 ret = debugfs_file_get(dentry);
969 if (unlikely(ret))
970 return ret;
971
972 str = *(char **)file->private_data;
973 len = strlen(str) + 1;
974 copy = kmalloc(len, GFP_KERNEL);
975 if (!copy) {
976 debugfs_file_put(dentry);
977 return -ENOMEM;
978 }
979
980 copy_len = strscpy(copy, str, len);
981 debugfs_file_put(dentry);
982 if (copy_len < 0) {
983 kfree(copy);
984 return copy_len;
985 }
986
987 copy[copy_len] = '\n';
988
Dietmar Eggemannf501b6a2021-05-27 11:11:05 +0200989 ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
Peter Zijlstra9af04402021-03-25 10:53:55 +0100990 kfree(copy);
991
992 return ret;
993}
Cristian Marussid60b59b2023-01-18 12:14:18 +0000994EXPORT_SYMBOL_GPL(debugfs_create_str);
Peter Zijlstra9af04402021-03-25 10:53:55 +0100995
996static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
997 size_t count, loff_t *ppos)
998{
Mike Tipton86b54882023-08-07 07:29:12 -0700999 struct dentry *dentry = F_DENTRY(file);
1000 char *old, *new = NULL;
1001 int pos = *ppos;
1002 int r;
1003
1004 r = debugfs_file_get(dentry);
1005 if (unlikely(r))
1006 return r;
1007
1008 old = *(char **)file->private_data;
1009
1010 /* only allow strict concatenation */
1011 r = -EINVAL;
1012 if (pos && pos != strlen(old))
1013 goto error;
1014
1015 r = -E2BIG;
1016 if (pos + count + 1 > PAGE_SIZE)
1017 goto error;
1018
1019 r = -ENOMEM;
1020 new = kmalloc(pos + count + 1, GFP_KERNEL);
1021 if (!new)
1022 goto error;
1023
1024 if (pos)
1025 memcpy(new, old, pos);
1026
1027 r = -EFAULT;
1028 if (copy_from_user(new + pos, user_buf, count))
1029 goto error;
1030
1031 new[pos + count] = '\0';
1032 strim(new);
1033
Mike Tipton7360a482023-09-22 06:45:12 -07001034 rcu_assign_pointer(*(char __rcu **)file->private_data, new);
Mike Tipton86b54882023-08-07 07:29:12 -07001035 synchronize_rcu();
1036 kfree(old);
1037
1038 debugfs_file_put(dentry);
1039 return count;
1040
1041error:
1042 kfree(new);
1043 debugfs_file_put(dentry);
1044 return r;
Peter Zijlstra9af04402021-03-25 10:53:55 +01001045}
1046
1047static const struct file_operations fops_str = {
1048 .read = debugfs_read_file_str,
1049 .write = debugfs_write_file_str,
1050 .open = simple_open,
1051 .llseek = default_llseek,
1052};
1053
1054static const struct file_operations fops_str_ro = {
1055 .read = debugfs_read_file_str,
1056 .open = simple_open,
1057 .llseek = default_llseek,
1058};
1059
1060static const struct file_operations fops_str_wo = {
1061 .write = debugfs_write_file_str,
1062 .open = simple_open,
1063 .llseek = default_llseek,
1064};
1065
1066/**
1067 * debugfs_create_str - create a debugfs file that is used to read and write a string value
1068 * @name: a pointer to a string containing the name of the file to create.
1069 * @mode: the permission that the file should have
1070 * @parent: a pointer to the parent dentry for this file. This should be a
1071 * directory dentry if set. If this parameter is %NULL, then the
1072 * file will be created in the root of the debugfs filesystem.
1073 * @value: a pointer to the variable that the file should read to and write
1074 * from.
1075 *
1076 * This function creates a file in debugfs with the given name that
1077 * contains the value of the variable @value. If the @mode variable is so
1078 * set, it can be read from, and written to.
Peter Zijlstra9af04402021-03-25 10:53:55 +01001079 */
1080void debugfs_create_str(const char *name, umode_t mode,
1081 struct dentry *parent, char **value)
1082{
1083 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
1084 &fops_str_ro, &fops_str_wo);
1085}
1086
Michael Ellermandd308bc2006-03-07 21:41:59 +11001087static ssize_t read_file_blob(struct file *file, char __user *user_buf,
1088 size_t count, loff_t *ppos)
1089{
1090 struct debugfs_blob_wrapper *blob = file->private_data;
Nicolai Stange69d29f92017-10-31 00:15:50 +01001091 struct dentry *dentry = F_DENTRY(file);
Nicolai Stange83b711cb2016-03-22 14:11:19 +01001092 ssize_t r;
Nicolai Stange83b711cb2016-03-22 14:11:19 +01001093
Nicolai Stange69d29f92017-10-31 00:15:50 +01001094 r = debugfs_file_get(dentry);
1095 if (unlikely(r))
1096 return r;
1097 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
1098 blob->size);
1099 debugfs_file_put(dentry);
Nicolai Stange83b711cb2016-03-22 14:11:19 +01001100 return r;
Michael Ellermandd308bc2006-03-07 21:41:59 +11001101}
1102
Arjan van de Ven00977a52007-02-12 00:55:34 -08001103static const struct file_operations fops_blob = {
Michael Ellermandd308bc2006-03-07 21:41:59 +11001104 .read = read_file_blob,
Stephen Boyd234e3402012-04-05 14:25:11 -07001105 .open = simple_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001106 .llseek = default_llseek,
Michael Ellermandd308bc2006-03-07 21:41:59 +11001107};
1108
1109/**
Jonathan Corbet400ced62009-05-25 10:15:27 -06001110 * debugfs_create_blob - create a debugfs file that is used to read a binary blob
Michael Ellermandd308bc2006-03-07 21:41:59 +11001111 * @name: a pointer to a string containing the name of the file to create.
Wolfram Sangd616f562021-05-04 15:13:49 +02001112 * @mode: the read permission that the file should have (other permissions are
1113 * masked out)
Michael Ellermandd308bc2006-03-07 21:41:59 +11001114 * @parent: a pointer to the parent dentry for this file. This should be a
Randy Dunlap6468b3a2006-07-20 08:16:42 -07001115 * directory dentry if set. If this parameter is %NULL, then the
Michael Ellermandd308bc2006-03-07 21:41:59 +11001116 * file will be created in the root of the debugfs filesystem.
1117 * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
1118 * to the blob data and the size of the data.
1119 *
1120 * This function creates a file in debugfs with the given name that exports
1121 * @blob->data as a binary blob. If the @mode variable is so set it can be
1122 * read from. Writing is not supported.
1123 *
1124 * This function will return a pointer to a dentry if it succeeds. This
1125 * pointer must be passed to the debugfs_remove() function when the file is
1126 * to be removed (no automatic cleanup happens if your module is unloaded,
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -03001127 * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be
Ronald Tschalär9abb2492019-04-15 01:25:05 -07001128 * returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +11001129 *
Daniel W. S. Almeidaadc92dd2019-12-26 22:00:33 -03001130 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
Ronald Tschalär9abb2492019-04-15 01:25:05 -07001131 * be returned.
Michael Ellermandd308bc2006-03-07 21:41:59 +11001132 */
Al Virof4ae40a62011-07-24 04:33:43 -04001133struct dentry *debugfs_create_blob(const char *name, umode_t mode,
Michael Ellermandd308bc2006-03-07 21:41:59 +11001134 struct dentry *parent,
1135 struct debugfs_blob_wrapper *blob)
1136{
Wolfram Sangd616f562021-05-04 15:13:49 +02001137 return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
Michael Ellermandd308bc2006-03-07 21:41:59 +11001138}
1139EXPORT_SYMBOL_GPL(debugfs_create_blob);
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001140
Linus Torvaldse05e2792012-09-21 11:48:05 -07001141static size_t u32_format_array(char *buf, size_t bufsize,
1142 u32 *array, int array_size)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301143{
1144 size_t ret = 0;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301145
Linus Torvaldse05e2792012-09-21 11:48:05 -07001146 while (--array_size >= 0) {
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301147 size_t len;
Linus Torvaldse05e2792012-09-21 11:48:05 -07001148 char term = array_size ? ' ' : '\n';
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301149
Linus Torvaldse05e2792012-09-21 11:48:05 -07001150 len = snprintf(buf, bufsize, "%u%c", *array++, term);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301151 ret += len;
1152
Linus Torvaldse05e2792012-09-21 11:48:05 -07001153 buf += len;
1154 bufsize -= len;
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301155 }
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301156 return ret;
1157}
1158
David Rientjes36048852012-09-21 02:16:29 -07001159static int u32_array_open(struct inode *inode, struct file *file)
1160{
Jakub Kicinskia2b992c2020-07-09 17:42:44 -07001161 struct debugfs_u32_array *data = inode->i_private;
1162 int size, elements = data->n_elements;
Linus Torvaldse05e2792012-09-21 11:48:05 -07001163 char *buf;
David Rientjes36048852012-09-21 02:16:29 -07001164
Linus Torvaldse05e2792012-09-21 11:48:05 -07001165 /*
1166 * Max size:
1167 * - 10 digits + ' '/'\n' = 11 bytes per number
1168 * - terminating NUL character
1169 */
1170 size = elements*11;
1171 buf = kmalloc(size+1, GFP_KERNEL);
1172 if (!buf)
David Rientjes36048852012-09-21 02:16:29 -07001173 return -ENOMEM;
Linus Torvaldse05e2792012-09-21 11:48:05 -07001174 buf[size] = 0;
1175
1176 file->private_data = buf;
Jakub Kicinskia2b992c2020-07-09 17:42:44 -07001177 u32_format_array(buf, size, data->array, data->n_elements);
Linus Torvaldse05e2792012-09-21 11:48:05 -07001178
David Rientjes36048852012-09-21 02:16:29 -07001179 return nonseekable_open(inode, file);
1180}
1181
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301182static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
1183 loff_t *ppos)
1184{
David Rientjes36048852012-09-21 02:16:29 -07001185 size_t size = strlen(file->private_data);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301186
1187 return simple_read_from_buffer(buf, len, ppos,
1188 file->private_data, size);
1189}
1190
1191static int u32_array_release(struct inode *inode, struct file *file)
1192{
1193 kfree(file->private_data);
1194
1195 return 0;
1196}
1197
1198static const struct file_operations u32_array_fops = {
1199 .owner = THIS_MODULE,
1200 .open = u32_array_open,
1201 .release = u32_array_release,
1202 .read = u32_array_read,
1203 .llseek = no_llseek,
1204};
1205
1206/**
1207 * debugfs_create_u32_array - create a debugfs file that is used to read u32
1208 * array.
1209 * @name: a pointer to a string containing the name of the file to create.
1210 * @mode: the permission that the file should have.
1211 * @parent: a pointer to the parent dentry for this file. This should be a
1212 * directory dentry if set. If this parameter is %NULL, then the
1213 * file will be created in the root of the debugfs filesystem.
Jakub Kicinskia2b992c2020-07-09 17:42:44 -07001214 * @array: wrapper struct containing data pointer and size of the array.
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301215 *
1216 * This function creates a file in debugfs with the given name that exports
1217 * @array as data. If the @mode variable is so set it can be read from.
1218 * Writing is not supported. Seek within the file is also not supported.
1219 * Once array is created its size can not be changed.
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301220 */
Greg Kroah-Hartmanc9c2c272019-04-16 15:46:55 +02001221void debugfs_create_u32_array(const char *name, umode_t mode,
Jakub Kicinskia2b992c2020-07-09 17:42:44 -07001222 struct dentry *parent,
1223 struct debugfs_u32_array *array)
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301224{
Jakub Kicinskia2b992c2020-07-09 17:42:44 -07001225 debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +05301226}
1227EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1228
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001229#ifdef CONFIG_HAS_IOMEM
1230
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001231/*
1232 * The regset32 stuff is used to print 32-bit registers using the
1233 * seq_file utilities. We offer printing a register set in an already-opened
1234 * sequential file or create a debugfs file that only prints a regset32.
1235 */
1236
1237/**
1238 * debugfs_print_regs32 - use seq_print to describe a set of registers
1239 * @s: the seq_file structure being used to generate output
1240 * @regs: an array if struct debugfs_reg32 structures
Randy Dunlapb5763ac2012-01-21 11:02:42 -08001241 * @nregs: the length of the above array
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001242 * @base: the base address to be used in reading the registers
1243 * @prefix: a string to be prefixed to every output line
1244 *
1245 * This function outputs a text block describing the current values of
1246 * some 32-bit hardware registers. It is meant to be used within debugfs
1247 * files based on seq_file that need to show registers, intermixed with other
1248 * information. The prefix argument may be used to specify a leading string,
1249 * because some peripherals have several blocks of identical registers,
1250 * for example configuration of dma channels
1251 */
Joe Perches97615362014-09-29 16:08:26 -07001252void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1253 int nregs, void __iomem *base, char *prefix)
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001254{
Joe Perches97615362014-09-29 16:08:26 -07001255 int i;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001256
1257 for (i = 0; i < nregs; i++, regs++) {
1258 if (prefix)
Joe Perches97615362014-09-29 16:08:26 -07001259 seq_printf(s, "%s", prefix);
1260 seq_printf(s, "%s = 0x%08x\n", regs->name,
1261 readl(base + regs->offset));
1262 if (seq_has_overflowed(s))
1263 break;
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001264 }
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001265}
1266EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1267
ChenXiaoSong19029f32022-09-23 18:25:54 +08001268static int debugfs_regset32_show(struct seq_file *s, void *data)
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001269{
1270 struct debugfs_regset32 *regset = s->private;
1271
Geert Uytterhoeven30332ee2020-02-11 19:18:55 +01001272 if (regset->dev)
1273 pm_runtime_get_sync(regset->dev);
1274
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001275 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
Geert Uytterhoeven30332ee2020-02-11 19:18:55 +01001276
1277 if (regset->dev)
1278 pm_runtime_put(regset->dev);
1279
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001280 return 0;
1281}
1282
ChenXiaoSong19029f32022-09-23 18:25:54 +08001283DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001284
1285/**
1286 * debugfs_create_regset32 - create a debugfs file that returns register values
1287 * @name: a pointer to a string containing the name of the file to create.
1288 * @mode: the permission that the file should have
1289 * @parent: a pointer to the parent dentry for this file. This should be a
1290 * directory dentry if set. If this parameter is %NULL, then the
1291 * file will be created in the root of the debugfs filesystem.
1292 * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1293 * to an array of register definitions, the array size and the base
1294 * address where the register bank is to be found.
1295 *
1296 * This function creates a file in debugfs with the given name that reports
1297 * the names and values of a set of 32-bit registers. If the @mode variable
1298 * is so set it can be read from. Writing is not supported.
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001299 */
Greg Kroah-Hartmanae91c922019-11-22 11:44:53 +01001300void debugfs_create_regset32(const char *name, umode_t mode,
1301 struct dentry *parent,
1302 struct debugfs_regset32 *regset)
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001303{
ChenXiaoSong19029f32022-09-23 18:25:54 +08001304 debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
Alessandro Rubini1a087c62011-11-18 14:50:21 +01001305}
1306EXPORT_SYMBOL_GPL(debugfs_create_regset32);
Heiko Carstens3b85e4a2011-12-27 15:08:28 +01001307
1308#endif /* CONFIG_HAS_IOMEM */
Arend van Spriel98210b72014-11-09 11:31:58 +01001309
1310struct debugfs_devm_entry {
1311 int (*read)(struct seq_file *seq, void *data);
1312 struct device *dev;
1313};
1314
1315static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1316{
1317 struct debugfs_devm_entry *entry = inode->i_private;
1318
1319 return single_open(f, entry->read, entry->dev);
1320}
1321
1322static const struct file_operations debugfs_devm_entry_ops = {
1323 .owner = THIS_MODULE,
1324 .open = debugfs_devm_entry_open,
1325 .release = single_release,
1326 .read = seq_read,
1327 .llseek = seq_lseek
1328};
1329
1330/**
1331 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1332 *
1333 * @dev: device related to this debugfs file.
1334 * @name: name of the debugfs file.
1335 * @parent: a pointer to the parent dentry for this file. This should be a
1336 * directory dentry if set. If this parameter is %NULL, then the
1337 * file will be created in the root of the debugfs filesystem.
1338 * @read_fn: function pointer called to print the seq_file content.
1339 */
Greg Kroah-Hartman0d519cb2020-10-23 15:10:37 +02001340void debugfs_create_devm_seqfile(struct device *dev, const char *name,
1341 struct dentry *parent,
1342 int (*read_fn)(struct seq_file *s, void *data))
Arend van Spriel98210b72014-11-09 11:31:58 +01001343{
1344 struct debugfs_devm_entry *entry;
1345
1346 if (IS_ERR(parent))
Greg Kroah-Hartman0d519cb2020-10-23 15:10:37 +02001347 return;
Arend van Spriel98210b72014-11-09 11:31:58 +01001348
1349 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1350 if (!entry)
Greg Kroah-Hartman0d519cb2020-10-23 15:10:37 +02001351 return;
Arend van Spriel98210b72014-11-09 11:31:58 +01001352
1353 entry->read = read_fn;
1354 entry->dev = dev;
1355
Greg Kroah-Hartman0d519cb2020-10-23 15:10:37 +02001356 debugfs_create_file(name, S_IRUGO, parent, entry,
1357 &debugfs_devm_entry_ops);
Arend van Spriel98210b72014-11-09 11:31:58 +01001358}
1359EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);