1 // SPDX-License-Identifier: GPL-2.0
2 /*
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 *
8 * debugfs is for people to use instead of /proc or /sys.
9 * See Documentation/filesystems/ for more details.
10 */
11
12 #include <linux/module.h>
13 #include <linux/fs.h>
14 #include <linux/seq_file.h>
15 #include <linux/pagemap.h>
16 #include <linux/debugfs.h>
17 #include <linux/io.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>
24
25 #include "internal.h"
26
27 struct poll_table_struct;
28
default_read_file(struct file * file,char __user * buf,size_t count,loff_t * ppos)29 static ssize_t default_read_file(struct file *file, char __user *buf,
30 size_t count, loff_t *ppos)
31 {
32 return 0;
33 }
34
default_write_file(struct file * file,const char __user * buf,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)
37 {
38 return count;
39 }
40
41 const struct file_operations debugfs_noop_file_operations = {
42 .read = default_read_file,
43 .write = default_write_file,
44 .open = simple_open,
45 .llseek = noop_llseek,
46 };
47
48 #define F_DENTRY(filp) ((filp)->f_path.dentry)
49
debugfs_real_fops(const struct file * filp)50 const struct file_operations *debugfs_real_fops(const struct file *filp)
51 {
52 struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
53
54 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
63 return fsd->real_fops;
64 }
65 EXPORT_SYMBOL_GPL(debugfs_real_fops);
66
67 /**
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 */
debugfs_file_get(struct dentry * dentry)82 int debugfs_file_get(struct dentry *dentry)
83 {
84 struct debugfs_fsdata *fsd;
85 void *d_fsd;
86
87 /*
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
95 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 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));
108 } else {
109 fsd->real_fops = (void *)((unsigned long)d_fsd &
110 ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
111 fsd->short_fops = NULL;
112 }
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);
117
118 if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
119 mutex_destroy(&fsd->cancellations_mtx);
120 kfree(fsd);
121 fsd = READ_ONCE(dentry->d_fsdata);
122 }
123 }
124
125 /*
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
131 * removers.
132 */
133 if (d_unlinked(dentry))
134 return -EIO;
135
136 if (!refcount_inc_not_zero(&fsd->active_users))
137 return -EIO;
138
139 return 0;
140 }
141 EXPORT_SYMBOL_GPL(debugfs_file_get);
142
143 /**
144 * debugfs_file_put - mark the end of file data access
145 * @dentry: the dentry object formerly passed to
146 * debugfs_file_get().
147 *
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.
151 */
debugfs_file_put(struct dentry * dentry)152 void debugfs_file_put(struct dentry *dentry)
153 {
154 struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
155
156 if (refcount_dec_and_test(&fsd->active_users))
157 complete(&fsd->active_users_drained);
158 }
159 EXPORT_SYMBOL_GPL(debugfs_file_put);
160
161 /**
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
166 *
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.
172 *
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().
180 *
181 * This function must be paired with debugfs_leave_cancellation().
182 */
debugfs_enter_cancellation(struct file * file,struct debugfs_cancellation * cancellation)183 void debugfs_enter_cancellation(struct file *file,
184 struct debugfs_cancellation *cancellation)
185 {
186 struct debugfs_fsdata *fsd;
187 struct dentry *dentry = F_DENTRY(file);
188
189 INIT_LIST_HEAD(&cancellation->list);
190
191 if (WARN_ON(!d_is_reg(dentry)))
192 return;
193
194 if (WARN_ON(!cancellation->cancel))
195 return;
196
197 fsd = READ_ONCE(dentry->d_fsdata);
198 if (WARN_ON(!fsd ||
199 ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
200 return;
201
202 mutex_lock(&fsd->cancellations_mtx);
203 list_add(&cancellation->list, &fsd->cancellations);
204 mutex_unlock(&fsd->cancellations_mtx);
205
206 /* if we're already removing wake it up to cancel */
207 if (d_unlinked(dentry))
208 complete(&fsd->active_users_drained);
209 }
210 EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
211
212 /**
213 * debugfs_leave_cancellation - leave cancellation section
214 * @file: the file being accessed
215 * @cancellation: the cancellation previously registered with
216 * debugfs_enter_cancellation()
217 *
218 * See the documentation of debugfs_enter_cancellation().
219 */
debugfs_leave_cancellation(struct file * file,struct debugfs_cancellation * cancellation)220 void debugfs_leave_cancellation(struct file *file,
221 struct debugfs_cancellation *cancellation)
222 {
223 struct debugfs_fsdata *fsd;
224 struct dentry *dentry = F_DENTRY(file);
225
226 if (WARN_ON(!d_is_reg(dentry)))
227 return;
228
229 fsd = READ_ONCE(dentry->d_fsdata);
230 if (WARN_ON(!fsd ||
231 ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
232 return;
233
234 mutex_lock(&fsd->cancellations_mtx);
235 if (!list_empty(&cancellation->list))
236 list_del(&cancellation->list);
237 mutex_unlock(&fsd->cancellations_mtx);
238 }
239 EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
240
241 /*
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.
245 */
debugfs_locked_down(struct inode * inode,struct file * filp,const struct file_operations * real_fops)246 static int debugfs_locked_down(struct inode *inode,
247 struct file *filp,
248 const struct file_operations *real_fops)
249 {
250 if ((inode->i_mode & 07777 & ~0444) == 0 &&
251 !(filp->f_mode & FMODE_WRITE) &&
252 (!real_fops ||
253 (!real_fops->unlocked_ioctl &&
254 !real_fops->compat_ioctl &&
255 !real_fops->mmap)))
256 return 0;
257
258 if (security_locked_down(LOCKDOWN_DEBUGFS))
259 return -EPERM;
260
261 return 0;
262 }
263
open_proxy_open(struct inode * inode,struct file * filp)264 static int open_proxy_open(struct inode *inode, struct file *filp)
265 {
266 struct dentry *dentry = F_DENTRY(filp);
267 const struct file_operations *real_fops = NULL;
268 int r;
269
270 r = debugfs_file_get(dentry);
271 if (r)
272 return r == -EIO ? -ENOENT : r;
273
274 real_fops = debugfs_real_fops(filp);
275
276 r = debugfs_locked_down(inode, filp, real_fops);
277 if (r)
278 goto out;
279
280 if (!fops_get(real_fops)) {
281 #ifdef CONFIG_MODULES
282 if (real_fops->owner &&
283 real_fops->owner->state == MODULE_STATE_GOING) {
284 r = -ENXIO;
285 goto out;
286 }
287 #endif
288
289 /* Huh? Module did not clean up after itself at exit? */
290 WARN(1, "debugfs file owner did not clean up at exit: %pd",
291 dentry);
292 r = -ENXIO;
293 goto out;
294 }
295 replace_fops(filp, real_fops);
296
297 if (real_fops->open)
298 r = real_fops->open(inode, filp);
299
300 out:
301 debugfs_file_put(dentry);
302 return r;
303 }
304
305 const struct file_operations debugfs_open_proxy_file_operations = {
306 .open = open_proxy_open,
307 };
308
309 #define PROTO(args...) args
310 #define ARGS(args...) args
311
312 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \
313 static ret_type full_proxy_ ## name(proto) \
314 { \
315 struct dentry *dentry = F_DENTRY(filp); \
316 const struct file_operations *real_fops; \
317 ret_type r; \
318 \
319 r = debugfs_file_get(dentry); \
320 if (unlikely(r)) \
321 return r; \
322 real_fops = debugfs_real_fops(filp); \
323 r = real_fops->name(args); \
324 debugfs_file_put(dentry); \
325 return r; \
326 }
327
328 #define FULL_PROXY_FUNC_BOTH(name, ret_type, filp, proto, args) \
329 static ret_type full_proxy_ ## name(proto) \
330 { \
331 struct dentry *dentry = F_DENTRY(filp); \
332 struct debugfs_fsdata *fsd; \
333 ret_type r; \
334 \
335 r = debugfs_file_get(dentry); \
336 if (unlikely(r)) \
337 return r; \
338 fsd = dentry->d_fsdata; \
339 if (fsd->real_fops) \
340 r = fsd->real_fops->name(args); \
341 else \
342 r = fsd->short_fops->name(args); \
343 debugfs_file_put(dentry); \
344 return r; \
345 }
346
347 FULL_PROXY_FUNC_BOTH(llseek, loff_t, filp,
348 PROTO(struct file *filp, loff_t offset, int whence),
349 ARGS(filp, offset, whence));
350
351 FULL_PROXY_FUNC_BOTH(read, ssize_t, filp,
352 PROTO(struct file *filp, char __user *buf, size_t size,
353 loff_t *ppos),
354 ARGS(filp, buf, size, ppos));
355
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));
360
361 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
362 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
363 ARGS(filp, cmd, arg));
364
full_proxy_poll(struct file * filp,struct poll_table_struct * wait)365 static __poll_t full_proxy_poll(struct file *filp,
366 struct poll_table_struct *wait)
367 {
368 struct dentry *dentry = F_DENTRY(filp);
369 __poll_t r = 0;
370 const struct file_operations *real_fops;
371
372 if (debugfs_file_get(dentry))
373 return EPOLLHUP;
374
375 real_fops = debugfs_real_fops(filp);
376 r = real_fops->poll(filp, wait);
377 debugfs_file_put(dentry);
378 return r;
379 }
380
full_proxy_release(struct inode * inode,struct file * filp)381 static int full_proxy_release(struct inode *inode, struct file *filp)
382 {
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;
386 int r = 0;
387
388 /*
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.
393 */
394 if (real_fops && real_fops->release)
395 r = real_fops->release(inode, filp);
396
397 replace_fops(filp, d_inode(dentry)->i_fop);
398 kfree(proxy_fops);
399 fops_put(real_fops);
400 return r;
401 }
402
__full_proxy_fops_init(struct file_operations * proxy_fops,struct debugfs_fsdata * fsd)403 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
404 struct debugfs_fsdata *fsd)
405 {
406 proxy_fops->release = full_proxy_release;
407
408 if ((fsd->real_fops && fsd->real_fops->llseek) ||
409 (fsd->short_fops && fsd->short_fops->llseek))
410 proxy_fops->llseek = full_proxy_llseek;
411
412 if ((fsd->real_fops && fsd->real_fops->read) ||
413 (fsd->short_fops && fsd->short_fops->read))
414 proxy_fops->read = full_proxy_read;
415
416 if ((fsd->real_fops && fsd->real_fops->write) ||
417 (fsd->short_fops && fsd->short_fops->write))
418 proxy_fops->write = full_proxy_write;
419
420 if (fsd->real_fops && fsd->real_fops->poll)
421 proxy_fops->poll = full_proxy_poll;
422
423 if (fsd->real_fops && fsd->real_fops->unlocked_ioctl)
424 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
425 }
426
full_proxy_open(struct inode * inode,struct file * filp)427 static int full_proxy_open(struct inode *inode, struct file *filp)
428 {
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;
433 int r;
434
435 r = debugfs_file_get(dentry);
436 if (r)
437 return r == -EIO ? -ENOENT : r;
438
439 fsd = dentry->d_fsdata;
440 real_fops = fsd->real_fops;
441 r = debugfs_locked_down(inode, filp, real_fops);
442 if (r)
443 goto out;
444
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) {
449 r = -ENXIO;
450 goto out;
451 }
452 #endif
453
454 /* Huh? Module did not cleanup after itself at exit? */
455 WARN(1, "debugfs file owner did not clean up at exit: %pd",
456 dentry);
457 r = -ENXIO;
458 goto out;
459 }
460
461 proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
462 if (!proxy_fops) {
463 r = -ENOMEM;
464 goto free_proxy;
465 }
466 __full_proxy_fops_init(proxy_fops, fsd);
467 replace_fops(filp, proxy_fops);
468
469 if (!real_fops || real_fops->open) {
470 if (real_fops)
471 r = real_fops->open(inode, filp);
472 else
473 r = simple_open(inode, filp);
474 if (r) {
475 replace_fops(filp, d_inode(dentry)->i_fop);
476 goto free_proxy;
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",
480 dentry);
481 goto free_proxy;
482 }
483 }
484
485 goto out;
486 free_proxy:
487 kfree(proxy_fops);
488 fops_put(real_fops);
489 out:
490 debugfs_file_put(dentry);
491 return r;
492 }
493
494 const struct file_operations debugfs_full_proxy_file_operations = {
495 .open = full_proxy_open,
496 };
497
debugfs_attr_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)498 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
499 size_t len, loff_t *ppos)
500 {
501 struct dentry *dentry = F_DENTRY(file);
502 ssize_t ret;
503
504 ret = debugfs_file_get(dentry);
505 if (unlikely(ret))
506 return ret;
507 ret = simple_attr_read(file, buf, len, ppos);
508 debugfs_file_put(dentry);
509 return ret;
510 }
511 EXPORT_SYMBOL_GPL(debugfs_attr_read);
512
debugfs_attr_write_xsigned(struct file * file,const char __user * buf,size_t len,loff_t * ppos,bool is_signed)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)
515 {
516 struct dentry *dentry = F_DENTRY(file);
517 ssize_t ret;
518
519 ret = debugfs_file_get(dentry);
520 if (unlikely(ret))
521 return ret;
522 if (is_signed)
523 ret = simple_attr_write_signed(file, buf, len, ppos);
524 else
525 ret = simple_attr_write(file, buf, len, ppos);
526 debugfs_file_put(dentry);
527 return ret;
528 }
529
debugfs_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)530 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
531 size_t len, loff_t *ppos)
532 {
533 return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
534 }
535 EXPORT_SYMBOL_GPL(debugfs_attr_write);
536
debugfs_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)537 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
538 size_t len, loff_t *ppos)
539 {
540 return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
541 }
542 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
543
debugfs_create_mode_unsafe(const char * name,umode_t mode,struct dentry * parent,void * value,const struct file_operations * fops,const struct file_operations * fops_ro,const struct file_operations * fops_wo)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)
549 {
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,
553 fops_ro);
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,
557 fops_wo);
558
559 return debugfs_create_file_unsafe(name, mode, parent, value, fops);
560 }
561
debugfs_u8_set(void * data,u64 val)562 static int debugfs_u8_set(void *data, u64 val)
563 {
564 *(u8 *)data = val;
565 return 0;
566 }
debugfs_u8_get(void * data,u64 * val)567 static int debugfs_u8_get(void *data, u64 *val)
568 {
569 *val = *(u8 *)data;
570 return 0;
571 }
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");
575
576 /**
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
584 * from.
585 *
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.
589 */
debugfs_create_u8(const char * name,umode_t mode,struct dentry * parent,u8 * value)590 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
591 u8 *value)
592 {
593 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
594 &fops_u8_ro, &fops_u8_wo);
595 }
596 EXPORT_SYMBOL_GPL(debugfs_create_u8);
597
debugfs_u16_set(void * data,u64 val)598 static int debugfs_u16_set(void *data, u64 val)
599 {
600 *(u16 *)data = val;
601 return 0;
602 }
debugfs_u16_get(void * data,u64 * val)603 static int debugfs_u16_get(void *data, u64 *val)
604 {
605 *val = *(u16 *)data;
606 return 0;
607 }
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");
611
612 /**
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
620 * from.
621 *
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.
625 */
debugfs_create_u16(const char * name,umode_t mode,struct dentry * parent,u16 * value)626 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
627 u16 *value)
628 {
629 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
630 &fops_u16_ro, &fops_u16_wo);
631 }
632 EXPORT_SYMBOL_GPL(debugfs_create_u16);
633
debugfs_u32_set(void * data,u64 val)634 static int debugfs_u32_set(void *data, u64 val)
635 {
636 *(u32 *)data = val;
637 return 0;
638 }
debugfs_u32_get(void * data,u64 * val)639 static int debugfs_u32_get(void *data, u64 *val)
640 {
641 *val = *(u32 *)data;
642 return 0;
643 }
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");
647
648 /**
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
656 * from.
657 *
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.
661 */
debugfs_create_u32(const char * name,umode_t mode,struct dentry * parent,u32 * value)662 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
663 u32 *value)
664 {
665 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
666 &fops_u32_ro, &fops_u32_wo);
667 }
668 EXPORT_SYMBOL_GPL(debugfs_create_u32);
669
debugfs_u64_set(void * data,u64 val)670 static int debugfs_u64_set(void *data, u64 val)
671 {
672 *(u64 *)data = val;
673 return 0;
674 }
675
debugfs_u64_get(void * data,u64 * val)676 static int debugfs_u64_get(void *data, u64 *val)
677 {
678 *val = *(u64 *)data;
679 return 0;
680 }
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");
684
685 /**
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
693 * from.
694 *
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.
698 */
debugfs_create_u64(const char * name,umode_t mode,struct dentry * parent,u64 * value)699 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
700 u64 *value)
701 {
702 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
703 &fops_u64_ro, &fops_u64_wo);
704 }
705 EXPORT_SYMBOL_GPL(debugfs_create_u64);
706
debugfs_ulong_set(void * data,u64 val)707 static int debugfs_ulong_set(void *data, u64 val)
708 {
709 *(unsigned long *)data = val;
710 return 0;
711 }
712
debugfs_ulong_get(void * data,u64 * val)713 static int debugfs_ulong_get(void *data, u64 *val)
714 {
715 *val = *(unsigned long *)data;
716 return 0;
717 }
718 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
719 "%llu\n");
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");
722
723 /**
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
732 * from.
733 *
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.
737 */
debugfs_create_ulong(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)738 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
739 unsigned long *value)
740 {
741 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
742 &fops_ulong_ro, &fops_ulong_wo);
743 }
744 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
745
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");
749
750 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
751 "0x%04llx\n");
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");
754
755 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
756 "0x%08llx\n");
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");
759
760 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
761 "0x%016llx\n");
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");
764
765 /*
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
767 *
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
770 * decimal functions.
771 */
772
773 /**
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
781 * from.
782 */
debugfs_create_x8(const char * name,umode_t mode,struct dentry * parent,u8 * value)783 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
784 u8 *value)
785 {
786 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
787 &fops_x8_ro, &fops_x8_wo);
788 }
789 EXPORT_SYMBOL_GPL(debugfs_create_x8);
790
791 /**
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
799 * from.
800 */
debugfs_create_x16(const char * name,umode_t mode,struct dentry * parent,u16 * value)801 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
802 u16 *value)
803 {
804 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
805 &fops_x16_ro, &fops_x16_wo);
806 }
807 EXPORT_SYMBOL_GPL(debugfs_create_x16);
808
809 /**
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
817 * from.
818 */
debugfs_create_x32(const char * name,umode_t mode,struct dentry * parent,u32 * value)819 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
820 u32 *value)
821 {
822 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
823 &fops_x32_ro, &fops_x32_wo);
824 }
825 EXPORT_SYMBOL_GPL(debugfs_create_x32);
826
827 /**
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
835 * from.
836 */
debugfs_create_x64(const char * name,umode_t mode,struct dentry * parent,u64 * value)837 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
838 u64 *value)
839 {
840 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
841 &fops_x64_ro, &fops_x64_wo);
842 }
843 EXPORT_SYMBOL_GPL(debugfs_create_x64);
844
845
debugfs_size_t_set(void * data,u64 val)846 static int debugfs_size_t_set(void *data, u64 val)
847 {
848 *(size_t *)data = val;
849 return 0;
850 }
debugfs_size_t_get(void * data,u64 * val)851 static int debugfs_size_t_get(void *data, u64 *val)
852 {
853 *val = *(size_t *)data;
854 return 0;
855 }
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");
860
861 /**
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
869 * from.
870 */
debugfs_create_size_t(const char * name,umode_t mode,struct dentry * parent,size_t * value)871 void debugfs_create_size_t(const char *name, umode_t mode,
872 struct dentry *parent, size_t *value)
873 {
874 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
875 &fops_size_t_ro, &fops_size_t_wo);
876 }
877 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
878
debugfs_atomic_t_set(void * data,u64 val)879 static int debugfs_atomic_t_set(void *data, u64 val)
880 {
881 atomic_set((atomic_t *)data, val);
882 return 0;
883 }
debugfs_atomic_t_get(void * data,u64 * val)884 static int debugfs_atomic_t_get(void *data, u64 *val)
885 {
886 *val = atomic_read((atomic_t *)data);
887 return 0;
888 }
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,
892 "%lld\n");
893 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
894 "%lld\n");
895
896 /**
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
905 * from.
906 */
debugfs_create_atomic_t(const char * name,umode_t mode,struct dentry * parent,atomic_t * value)907 void debugfs_create_atomic_t(const char *name, umode_t mode,
908 struct dentry *parent, atomic_t *value)
909 {
910 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
911 &fops_atomic_t_ro, &fops_atomic_t_wo);
912 }
913 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
914
debugfs_read_file_bool(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)915 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
916 size_t count, loff_t *ppos)
917 {
918 char buf[2];
919 bool val;
920 int r;
921 struct dentry *dentry = F_DENTRY(file);
922
923 r = debugfs_file_get(dentry);
924 if (unlikely(r))
925 return r;
926 val = *(bool *)file->private_data;
927 debugfs_file_put(dentry);
928
929 if (val)
930 buf[0] = 'Y';
931 else
932 buf[0] = 'N';
933 buf[1] = '\n';
934 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
935 }
936 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
937
debugfs_write_file_bool(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)938 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
939 size_t count, loff_t *ppos)
940 {
941 bool bv;
942 int r;
943 bool *val = file->private_data;
944 struct dentry *dentry = F_DENTRY(file);
945
946 r = kstrtobool_from_user(user_buf, count, &bv);
947 if (!r) {
948 r = debugfs_file_get(dentry);
949 if (unlikely(r))
950 return r;
951 *val = bv;
952 debugfs_file_put(dentry);
953 }
954
955 return count;
956 }
957 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
958
959 static const struct file_operations fops_bool = {
960 .read = debugfs_read_file_bool,
961 .write = debugfs_write_file_bool,
962 .open = simple_open,
963 .llseek = default_llseek,
964 };
965
966 static const struct file_operations fops_bool_ro = {
967 .read = debugfs_read_file_bool,
968 .open = simple_open,
969 .llseek = default_llseek,
970 };
971
972 static const struct file_operations fops_bool_wo = {
973 .write = debugfs_write_file_bool,
974 .open = simple_open,
975 .llseek = default_llseek,
976 };
977
978 /**
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
986 * from.
987 *
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.
991 */
debugfs_create_bool(const char * name,umode_t mode,struct dentry * parent,bool * value)992 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
993 bool *value)
994 {
995 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
996 &fops_bool_ro, &fops_bool_wo);
997 }
998 EXPORT_SYMBOL_GPL(debugfs_create_bool);
999
debugfs_read_file_str(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1000 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
1001 size_t count, loff_t *ppos)
1002 {
1003 struct dentry *dentry = F_DENTRY(file);
1004 char *str, *copy = NULL;
1005 int copy_len, len;
1006 ssize_t ret;
1007
1008 ret = debugfs_file_get(dentry);
1009 if (unlikely(ret))
1010 return ret;
1011
1012 str = *(char **)file->private_data;
1013 len = strlen(str) + 1;
1014 copy = kmalloc(len, GFP_KERNEL);
1015 if (!copy) {
1016 debugfs_file_put(dentry);
1017 return -ENOMEM;
1018 }
1019
1020 copy_len = strscpy(copy, str, len);
1021 debugfs_file_put(dentry);
1022 if (copy_len < 0) {
1023 kfree(copy);
1024 return copy_len;
1025 }
1026
1027 copy[copy_len] = '\n';
1028
1029 ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
1030 kfree(copy);
1031
1032 return ret;
1033 }
1034 EXPORT_SYMBOL_GPL(debugfs_create_str);
1035
debugfs_write_file_str(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)1036 static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
1037 size_t count, loff_t *ppos)
1038 {
1039 struct dentry *dentry = F_DENTRY(file);
1040 char *old, *new = NULL;
1041 int pos = *ppos;
1042 int r;
1043
1044 r = debugfs_file_get(dentry);
1045 if (unlikely(r))
1046 return r;
1047
1048 old = *(char **)file->private_data;
1049
1050 /* only allow strict concatenation */
1051 r = -EINVAL;
1052 if (pos && pos != strlen(old))
1053 goto error;
1054
1055 r = -E2BIG;
1056 if (pos + count + 1 > PAGE_SIZE)
1057 goto error;
1058
1059 r = -ENOMEM;
1060 new = kmalloc(pos + count + 1, GFP_KERNEL);
1061 if (!new)
1062 goto error;
1063
1064 if (pos)
1065 memcpy(new, old, pos);
1066
1067 r = -EFAULT;
1068 if (copy_from_user(new + pos, user_buf, count))
1069 goto error;
1070
1071 new[pos + count] = '\0';
1072 strim(new);
1073
1074 rcu_assign_pointer(*(char __rcu **)file->private_data, new);
1075 synchronize_rcu();
1076 kfree(old);
1077
1078 debugfs_file_put(dentry);
1079 return count;
1080
1081 error:
1082 kfree(new);
1083 debugfs_file_put(dentry);
1084 return r;
1085 }
1086
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,
1092 };
1093
1094 static const struct file_operations fops_str_ro = {
1095 .read = debugfs_read_file_str,
1096 .open = simple_open,
1097 .llseek = default_llseek,
1098 };
1099
1100 static const struct file_operations fops_str_wo = {
1101 .write = debugfs_write_file_str,
1102 .open = simple_open,
1103 .llseek = default_llseek,
1104 };
1105
1106 /**
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
1114 * from.
1115 *
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.
1119 */
debugfs_create_str(const char * name,umode_t mode,struct dentry * parent,char ** value)1120 void debugfs_create_str(const char *name, umode_t mode,
1121 struct dentry *parent, char **value)
1122 {
1123 debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
1124 &fops_str_ro, &fops_str_wo);
1125 }
1126
read_file_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1127 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
1128 size_t count, loff_t *ppos)
1129 {
1130 struct debugfs_blob_wrapper *blob = file->private_data;
1131 struct dentry *dentry = F_DENTRY(file);
1132 ssize_t r;
1133
1134 r = debugfs_file_get(dentry);
1135 if (unlikely(r))
1136 return r;
1137 r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
1138 blob->size);
1139 debugfs_file_put(dentry);
1140 return r;
1141 }
1142
write_file_blob(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)1143 static ssize_t write_file_blob(struct file *file, const char __user *user_buf,
1144 size_t count, loff_t *ppos)
1145 {
1146 struct debugfs_blob_wrapper *blob = file->private_data;
1147 struct dentry *dentry = F_DENTRY(file);
1148 ssize_t r;
1149
1150 r = debugfs_file_get(dentry);
1151 if (unlikely(r))
1152 return r;
1153 r = simple_write_to_buffer(blob->data, blob->size, ppos, user_buf,
1154 count);
1155
1156 debugfs_file_put(dentry);
1157 return r;
1158 }
1159
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,
1165 };
1166
1167 /**
1168 * debugfs_create_blob - create a debugfs file that is used to read and write
1169 * a binary blob
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.
1177 *
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.
1181 *
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
1186 * returned.
1187 *
1188 * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
1189 * be returned.
1190 */
debugfs_create_blob(const char * name,umode_t mode,struct dentry * parent,struct debugfs_blob_wrapper * blob)1191 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1192 struct dentry *parent,
1193 struct debugfs_blob_wrapper *blob)
1194 {
1195 return debugfs_create_file_unsafe(name, mode & 0644, parent, blob, &fops_blob);
1196 }
1197 EXPORT_SYMBOL_GPL(debugfs_create_blob);
1198
u32_format_array(char * buf,size_t bufsize,u32 * array,int array_size)1199 static size_t u32_format_array(char *buf, size_t bufsize,
1200 u32 *array, int array_size)
1201 {
1202 size_t ret = 0;
1203
1204 while (--array_size >= 0) {
1205 size_t len;
1206 char term = array_size ? ' ' : '\n';
1207
1208 len = snprintf(buf, bufsize, "%u%c", *array++, term);
1209 ret += len;
1210
1211 buf += len;
1212 bufsize -= len;
1213 }
1214 return ret;
1215 }
1216
u32_array_open(struct inode * inode,struct file * file)1217 static int u32_array_open(struct inode *inode, struct file *file)
1218 {
1219 struct debugfs_u32_array *data = inode->i_private;
1220 int size, elements = data->n_elements;
1221 char *buf;
1222
1223 /*
1224 * Max size:
1225 * - 10 digits + ' '/'\n' = 11 bytes per number
1226 * - terminating NUL character
1227 */
1228 size = elements*11;
1229 buf = kmalloc(size+1, GFP_KERNEL);
1230 if (!buf)
1231 return -ENOMEM;
1232 buf[size] = 0;
1233
1234 file->private_data = buf;
1235 u32_format_array(buf, size, data->array, data->n_elements);
1236
1237 return nonseekable_open(inode, file);
1238 }
1239
u32_array_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)1240 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
1241 loff_t *ppos)
1242 {
1243 size_t size = strlen(file->private_data);
1244
1245 return simple_read_from_buffer(buf, len, ppos,
1246 file->private_data, size);
1247 }
1248
u32_array_release(struct inode * inode,struct file * file)1249 static int u32_array_release(struct inode *inode, struct file *file)
1250 {
1251 kfree(file->private_data);
1252
1253 return 0;
1254 }
1255
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,
1261 };
1262
1263 /**
1264 * debugfs_create_u32_array - create a debugfs file that is used to read u32
1265 * array.
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.
1272 *
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.
1277 */
debugfs_create_u32_array(const char * name,umode_t mode,struct dentry * parent,struct debugfs_u32_array * array)1278 void debugfs_create_u32_array(const char *name, umode_t mode,
1279 struct dentry *parent,
1280 struct debugfs_u32_array *array)
1281 {
1282 debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
1283 }
1284 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1285
1286 #ifdef CONFIG_HAS_IOMEM
1287
1288 /*
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.
1292 */
1293
1294 /**
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
1301 *
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
1308 */
debugfs_print_regs32(struct seq_file * s,const struct debugfs_reg32 * regs,int nregs,void __iomem * base,char * prefix)1309 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1310 int nregs, void __iomem *base, char *prefix)
1311 {
1312 int i;
1313
1314 for (i = 0; i < nregs; i++, regs++) {
1315 if (prefix)
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))
1320 break;
1321 }
1322 }
1323 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1324
debugfs_regset32_show(struct seq_file * s,void * data)1325 static int debugfs_regset32_show(struct seq_file *s, void *data)
1326 {
1327 struct debugfs_regset32 *regset = s->private;
1328
1329 if (regset->dev)
1330 pm_runtime_get_sync(regset->dev);
1331
1332 debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1333
1334 if (regset->dev)
1335 pm_runtime_put(regset->dev);
1336
1337 return 0;
1338 }
1339
1340 DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
1341
1342 /**
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.
1352 *
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.
1356 */
debugfs_create_regset32(const char * name,umode_t mode,struct dentry * parent,struct debugfs_regset32 * regset)1357 void debugfs_create_regset32(const char *name, umode_t mode,
1358 struct dentry *parent,
1359 struct debugfs_regset32 *regset)
1360 {
1361 debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
1362 }
1363 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1364
1365 #endif /* CONFIG_HAS_IOMEM */
1366
1367 struct debugfs_devm_entry {
1368 int (*read)(struct seq_file *seq, void *data);
1369 struct device *dev;
1370 };
1371
debugfs_devm_entry_open(struct inode * inode,struct file * f)1372 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1373 {
1374 struct debugfs_devm_entry *entry = inode->i_private;
1375
1376 return single_open(f, entry->read, entry->dev);
1377 }
1378
1379 static const struct file_operations debugfs_devm_entry_ops = {
1380 .owner = THIS_MODULE,
1381 .open = debugfs_devm_entry_open,
1382 .release = single_release,
1383 .read = seq_read,
1384 .llseek = seq_lseek
1385 };
1386
1387 /**
1388 * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1389 *
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.
1396 */
debugfs_create_devm_seqfile(struct device * dev,const char * name,struct dentry * parent,int (* read_fn)(struct seq_file * s,void * data))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))
1400 {
1401 struct debugfs_devm_entry *entry;
1402
1403 if (IS_ERR(parent))
1404 return;
1405
1406 entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1407 if (!entry)
1408 return;
1409
1410 entry->read = read_fn;
1411 entry->dev = dev;
1412
1413 debugfs_create_file(name, S_IRUGO, parent, entry,
1414 &debugfs_devm_entry_ops);
1415 }
1416 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1417