xref: /openbmc/linux/fs/debugfs/file.c (revision c455b17a)
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 		fsd->real_fops = (void *)((unsigned long)d_fsd &
104 					~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
105 		refcount_set(&fsd->active_users, 1);
106 		init_completion(&fsd->active_users_drained);
107 		if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
108 			kfree(fsd);
109 			fsd = READ_ONCE(dentry->d_fsdata);
110 		}
111 	}
112 
113 	/*
114 	 * In case of a successful cmpxchg() above, this check is
115 	 * strictly necessary and must follow it, see the comment in
116 	 * __debugfs_remove_file().
117 	 * OTOH, if the cmpxchg() hasn't been executed or wasn't
118 	 * successful, this serves the purpose of not starving
119 	 * removers.
120 	 */
121 	if (d_unlinked(dentry))
122 		return -EIO;
123 
124 	if (!refcount_inc_not_zero(&fsd->active_users))
125 		return -EIO;
126 
127 	return 0;
128 }
129 EXPORT_SYMBOL_GPL(debugfs_file_get);
130 
131 /**
132  * debugfs_file_put - mark the end of file data access
133  * @dentry: the dentry object formerly passed to
134  *          debugfs_file_get().
135  *
136  * Allow any ongoing concurrent call into debugfs_remove() or
137  * debugfs_remove_recursive() blocked by a former call to
138  * debugfs_file_get() to proceed and return to its caller.
139  */
debugfs_file_put(struct dentry * dentry)140 void debugfs_file_put(struct dentry *dentry)
141 {
142 	struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
143 
144 	if (refcount_dec_and_test(&fsd->active_users))
145 		complete(&fsd->active_users_drained);
146 }
147 EXPORT_SYMBOL_GPL(debugfs_file_put);
148 
149 /*
150  * Only permit access to world-readable files when the kernel is locked down.
151  * We also need to exclude any file that has ways to write or alter it as root
152  * can bypass the permissions check.
153  */
debugfs_locked_down(struct inode * inode,struct file * filp,const struct file_operations * real_fops)154 static int debugfs_locked_down(struct inode *inode,
155 			       struct file *filp,
156 			       const struct file_operations *real_fops)
157 {
158 	if ((inode->i_mode & 07777 & ~0444) == 0 &&
159 	    !(filp->f_mode & FMODE_WRITE) &&
160 	    !real_fops->unlocked_ioctl &&
161 	    !real_fops->compat_ioctl &&
162 	    !real_fops->mmap)
163 		return 0;
164 
165 	if (security_locked_down(LOCKDOWN_DEBUGFS))
166 		return -EPERM;
167 
168 	return 0;
169 }
170 
open_proxy_open(struct inode * inode,struct file * filp)171 static int open_proxy_open(struct inode *inode, struct file *filp)
172 {
173 	struct dentry *dentry = F_DENTRY(filp);
174 	const struct file_operations *real_fops = NULL;
175 	int r;
176 
177 	r = debugfs_file_get(dentry);
178 	if (r)
179 		return r == -EIO ? -ENOENT : r;
180 
181 	real_fops = debugfs_real_fops(filp);
182 
183 	r = debugfs_locked_down(inode, filp, real_fops);
184 	if (r)
185 		goto out;
186 
187 	if (!fops_get(real_fops)) {
188 #ifdef CONFIG_MODULES
189 		if (real_fops->owner &&
190 		    real_fops->owner->state == MODULE_STATE_GOING) {
191 			r = -ENXIO;
192 			goto out;
193 		}
194 #endif
195 
196 		/* Huh? Module did not clean up after itself at exit? */
197 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
198 			dentry);
199 		r = -ENXIO;
200 		goto out;
201 	}
202 	replace_fops(filp, real_fops);
203 
204 	if (real_fops->open)
205 		r = real_fops->open(inode, filp);
206 
207 out:
208 	debugfs_file_put(dentry);
209 	return r;
210 }
211 
212 const struct file_operations debugfs_open_proxy_file_operations = {
213 	.open = open_proxy_open,
214 };
215 
216 #define PROTO(args...) args
217 #define ARGS(args...) args
218 
219 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)		\
220 static ret_type full_proxy_ ## name(proto)				\
221 {									\
222 	struct dentry *dentry = F_DENTRY(filp);			\
223 	const struct file_operations *real_fops;			\
224 	ret_type r;							\
225 									\
226 	r = debugfs_file_get(dentry);					\
227 	if (unlikely(r))						\
228 		return r;						\
229 	real_fops = debugfs_real_fops(filp);				\
230 	r = real_fops->name(args);					\
231 	debugfs_file_put(dentry);					\
232 	return r;							\
233 }
234 
235 FULL_PROXY_FUNC(llseek, loff_t, filp,
236 		PROTO(struct file *filp, loff_t offset, int whence),
237 		ARGS(filp, offset, whence));
238 
239 FULL_PROXY_FUNC(read, ssize_t, filp,
240 		PROTO(struct file *filp, char __user *buf, size_t size,
241 			loff_t *ppos),
242 		ARGS(filp, buf, size, ppos));
243 
244 FULL_PROXY_FUNC(write, ssize_t, filp,
245 		PROTO(struct file *filp, const char __user *buf, size_t size,
246 			loff_t *ppos),
247 		ARGS(filp, buf, size, ppos));
248 
249 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
250 		PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
251 		ARGS(filp, cmd, arg));
252 
full_proxy_poll(struct file * filp,struct poll_table_struct * wait)253 static __poll_t full_proxy_poll(struct file *filp,
254 				struct poll_table_struct *wait)
255 {
256 	struct dentry *dentry = F_DENTRY(filp);
257 	__poll_t r = 0;
258 	const struct file_operations *real_fops;
259 
260 	if (debugfs_file_get(dentry))
261 		return EPOLLHUP;
262 
263 	real_fops = debugfs_real_fops(filp);
264 	r = real_fops->poll(filp, wait);
265 	debugfs_file_put(dentry);
266 	return r;
267 }
268 
full_proxy_release(struct inode * inode,struct file * filp)269 static int full_proxy_release(struct inode *inode, struct file *filp)
270 {
271 	const struct dentry *dentry = F_DENTRY(filp);
272 	const struct file_operations *real_fops = debugfs_real_fops(filp);
273 	const struct file_operations *proxy_fops = filp->f_op;
274 	int r = 0;
275 
276 	/*
277 	 * We must not protect this against removal races here: the
278 	 * original releaser should be called unconditionally in order
279 	 * not to leak any resources. Releasers must not assume that
280 	 * ->i_private is still being meaningful here.
281 	 */
282 	if (real_fops->release)
283 		r = real_fops->release(inode, filp);
284 
285 	replace_fops(filp, d_inode(dentry)->i_fop);
286 	kfree(proxy_fops);
287 	fops_put(real_fops);
288 	return r;
289 }
290 
__full_proxy_fops_init(struct file_operations * proxy_fops,const struct file_operations * real_fops)291 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
292 				const struct file_operations *real_fops)
293 {
294 	proxy_fops->release = full_proxy_release;
295 	if (real_fops->llseek)
296 		proxy_fops->llseek = full_proxy_llseek;
297 	if (real_fops->read)
298 		proxy_fops->read = full_proxy_read;
299 	if (real_fops->write)
300 		proxy_fops->write = full_proxy_write;
301 	if (real_fops->poll)
302 		proxy_fops->poll = full_proxy_poll;
303 	if (real_fops->unlocked_ioctl)
304 		proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
305 }
306 
full_proxy_open(struct inode * inode,struct file * filp)307 static int full_proxy_open(struct inode *inode, struct file *filp)
308 {
309 	struct dentry *dentry = F_DENTRY(filp);
310 	const struct file_operations *real_fops = NULL;
311 	struct file_operations *proxy_fops = NULL;
312 	int r;
313 
314 	r = debugfs_file_get(dentry);
315 	if (r)
316 		return r == -EIO ? -ENOENT : r;
317 
318 	real_fops = debugfs_real_fops(filp);
319 
320 	r = debugfs_locked_down(inode, filp, real_fops);
321 	if (r)
322 		goto out;
323 
324 	if (!fops_get(real_fops)) {
325 #ifdef CONFIG_MODULES
326 		if (real_fops->owner &&
327 		    real_fops->owner->state == MODULE_STATE_GOING) {
328 			r = -ENXIO;
329 			goto out;
330 		}
331 #endif
332 
333 		/* Huh? Module did not cleanup after itself at exit? */
334 		WARN(1, "debugfs file owner did not clean up at exit: %pd",
335 			dentry);
336 		r = -ENXIO;
337 		goto out;
338 	}
339 
340 	proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
341 	if (!proxy_fops) {
342 		r = -ENOMEM;
343 		goto free_proxy;
344 	}
345 	__full_proxy_fops_init(proxy_fops, real_fops);
346 	replace_fops(filp, proxy_fops);
347 
348 	if (real_fops->open) {
349 		r = real_fops->open(inode, filp);
350 		if (r) {
351 			replace_fops(filp, d_inode(dentry)->i_fop);
352 			goto free_proxy;
353 		} else if (filp->f_op != proxy_fops) {
354 			/* No protection against file removal anymore. */
355 			WARN(1, "debugfs file owner replaced proxy fops: %pd",
356 				dentry);
357 			goto free_proxy;
358 		}
359 	}
360 
361 	goto out;
362 free_proxy:
363 	kfree(proxy_fops);
364 	fops_put(real_fops);
365 out:
366 	debugfs_file_put(dentry);
367 	return r;
368 }
369 
370 const struct file_operations debugfs_full_proxy_file_operations = {
371 	.open = full_proxy_open,
372 };
373 
debugfs_attr_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)374 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
375 			size_t len, loff_t *ppos)
376 {
377 	struct dentry *dentry = F_DENTRY(file);
378 	ssize_t ret;
379 
380 	ret = debugfs_file_get(dentry);
381 	if (unlikely(ret))
382 		return ret;
383 	ret = simple_attr_read(file, buf, len, ppos);
384 	debugfs_file_put(dentry);
385 	return ret;
386 }
387 EXPORT_SYMBOL_GPL(debugfs_attr_read);
388 
debugfs_attr_write_xsigned(struct file * file,const char __user * buf,size_t len,loff_t * ppos,bool is_signed)389 static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
390 			 size_t len, loff_t *ppos, bool is_signed)
391 {
392 	struct dentry *dentry = F_DENTRY(file);
393 	ssize_t ret;
394 
395 	ret = debugfs_file_get(dentry);
396 	if (unlikely(ret))
397 		return ret;
398 	if (is_signed)
399 		ret = simple_attr_write_signed(file, buf, len, ppos);
400 	else
401 		ret = simple_attr_write(file, buf, len, ppos);
402 	debugfs_file_put(dentry);
403 	return ret;
404 }
405 
debugfs_attr_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)406 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
407 			 size_t len, loff_t *ppos)
408 {
409 	return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
410 }
411 EXPORT_SYMBOL_GPL(debugfs_attr_write);
412 
debugfs_attr_write_signed(struct file * file,const char __user * buf,size_t len,loff_t * ppos)413 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
414 			 size_t len, loff_t *ppos)
415 {
416 	return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
417 }
418 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
419 
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)420 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
421 					struct dentry *parent, void *value,
422 					const struct file_operations *fops,
423 					const struct file_operations *fops_ro,
424 					const struct file_operations *fops_wo)
425 {
426 	/* if there are no write bits set, make read only */
427 	if (!(mode & S_IWUGO))
428 		return debugfs_create_file_unsafe(name, mode, parent, value,
429 						fops_ro);
430 	/* if there are no read bits set, make write only */
431 	if (!(mode & S_IRUGO))
432 		return debugfs_create_file_unsafe(name, mode, parent, value,
433 						fops_wo);
434 
435 	return debugfs_create_file_unsafe(name, mode, parent, value, fops);
436 }
437 
debugfs_u8_set(void * data,u64 val)438 static int debugfs_u8_set(void *data, u64 val)
439 {
440 	*(u8 *)data = val;
441 	return 0;
442 }
debugfs_u8_get(void * data,u64 * val)443 static int debugfs_u8_get(void *data, u64 *val)
444 {
445 	*val = *(u8 *)data;
446 	return 0;
447 }
448 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
449 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
450 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
451 
452 /**
453  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
454  * @name: a pointer to a string containing the name of the file to create.
455  * @mode: the permission that the file should have
456  * @parent: a pointer to the parent dentry for this file.  This should be a
457  *          directory dentry if set.  If this parameter is %NULL, then the
458  *          file will be created in the root of the debugfs filesystem.
459  * @value: a pointer to the variable that the file should read to and write
460  *         from.
461  *
462  * This function creates a file in debugfs with the given name that
463  * contains the value of the variable @value.  If the @mode variable is so
464  * set, it can be read from, and written to.
465  */
debugfs_create_u8(const char * name,umode_t mode,struct dentry * parent,u8 * value)466 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
467 		       u8 *value)
468 {
469 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
470 				   &fops_u8_ro, &fops_u8_wo);
471 }
472 EXPORT_SYMBOL_GPL(debugfs_create_u8);
473 
debugfs_u16_set(void * data,u64 val)474 static int debugfs_u16_set(void *data, u64 val)
475 {
476 	*(u16 *)data = val;
477 	return 0;
478 }
debugfs_u16_get(void * data,u64 * val)479 static int debugfs_u16_get(void *data, u64 *val)
480 {
481 	*val = *(u16 *)data;
482 	return 0;
483 }
484 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
485 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
486 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
487 
488 /**
489  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
490  * @name: a pointer to a string containing the name of the file to create.
491  * @mode: the permission that the file should have
492  * @parent: a pointer to the parent dentry for this file.  This should be a
493  *          directory dentry if set.  If this parameter is %NULL, then the
494  *          file will be created in the root of the debugfs filesystem.
495  * @value: a pointer to the variable that the file should read to and write
496  *         from.
497  *
498  * This function creates a file in debugfs with the given name that
499  * contains the value of the variable @value.  If the @mode variable is so
500  * set, it can be read from, and written to.
501  */
debugfs_create_u16(const char * name,umode_t mode,struct dentry * parent,u16 * value)502 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
503 			u16 *value)
504 {
505 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
506 				   &fops_u16_ro, &fops_u16_wo);
507 }
508 EXPORT_SYMBOL_GPL(debugfs_create_u16);
509 
debugfs_u32_set(void * data,u64 val)510 static int debugfs_u32_set(void *data, u64 val)
511 {
512 	*(u32 *)data = val;
513 	return 0;
514 }
debugfs_u32_get(void * data,u64 * val)515 static int debugfs_u32_get(void *data, u64 *val)
516 {
517 	*val = *(u32 *)data;
518 	return 0;
519 }
520 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
521 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
522 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
523 
524 /**
525  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
526  * @name: a pointer to a string containing the name of the file to create.
527  * @mode: the permission that the file should have
528  * @parent: a pointer to the parent dentry for this file.  This should be a
529  *          directory dentry if set.  If this parameter is %NULL, then the
530  *          file will be created in the root of the debugfs filesystem.
531  * @value: a pointer to the variable that the file should read to and write
532  *         from.
533  *
534  * This function creates a file in debugfs with the given name that
535  * contains the value of the variable @value.  If the @mode variable is so
536  * set, it can be read from, and written to.
537  */
debugfs_create_u32(const char * name,umode_t mode,struct dentry * parent,u32 * value)538 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
539 			u32 *value)
540 {
541 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
542 				   &fops_u32_ro, &fops_u32_wo);
543 }
544 EXPORT_SYMBOL_GPL(debugfs_create_u32);
545 
debugfs_u64_set(void * data,u64 val)546 static int debugfs_u64_set(void *data, u64 val)
547 {
548 	*(u64 *)data = val;
549 	return 0;
550 }
551 
debugfs_u64_get(void * data,u64 * val)552 static int debugfs_u64_get(void *data, u64 *val)
553 {
554 	*val = *(u64 *)data;
555 	return 0;
556 }
557 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
558 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
559 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
560 
561 /**
562  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
563  * @name: a pointer to a string containing the name of the file to create.
564  * @mode: the permission that the file should have
565  * @parent: a pointer to the parent dentry for this file.  This should be a
566  *          directory dentry if set.  If this parameter is %NULL, then the
567  *          file will be created in the root of the debugfs filesystem.
568  * @value: a pointer to the variable that the file should read to and write
569  *         from.
570  *
571  * This function creates a file in debugfs with the given name that
572  * contains the value of the variable @value.  If the @mode variable is so
573  * set, it can be read from, and written to.
574  */
debugfs_create_u64(const char * name,umode_t mode,struct dentry * parent,u64 * value)575 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
576 			u64 *value)
577 {
578 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
579 				   &fops_u64_ro, &fops_u64_wo);
580 }
581 EXPORT_SYMBOL_GPL(debugfs_create_u64);
582 
debugfs_ulong_set(void * data,u64 val)583 static int debugfs_ulong_set(void *data, u64 val)
584 {
585 	*(unsigned long *)data = val;
586 	return 0;
587 }
588 
debugfs_ulong_get(void * data,u64 * val)589 static int debugfs_ulong_get(void *data, u64 *val)
590 {
591 	*val = *(unsigned long *)data;
592 	return 0;
593 }
594 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
595 			"%llu\n");
596 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
597 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
598 
599 /**
600  * debugfs_create_ulong - create a debugfs file that is used to read and write
601  * an unsigned long value.
602  * @name: a pointer to a string containing the name of the file to create.
603  * @mode: the permission that the file should have
604  * @parent: a pointer to the parent dentry for this file.  This should be a
605  *          directory dentry if set.  If this parameter is %NULL, then the
606  *          file will be created in the root of the debugfs filesystem.
607  * @value: a pointer to the variable that the file should read to and write
608  *         from.
609  *
610  * This function creates a file in debugfs with the given name that
611  * contains the value of the variable @value.  If the @mode variable is so
612  * set, it can be read from, and written to.
613  */
debugfs_create_ulong(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)614 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
615 			  unsigned long *value)
616 {
617 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
618 				   &fops_ulong_ro, &fops_ulong_wo);
619 }
620 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
621 
622 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
623 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
624 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
625 
626 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
627 			"0x%04llx\n");
628 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
629 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
630 
631 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
632 			"0x%08llx\n");
633 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
634 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
635 
636 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
637 			"0x%016llx\n");
638 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
639 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
640 
641 /*
642  * 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
643  *
644  * These functions are exactly the same as the above functions (but use a hex
645  * output for the decimal challenged). For details look at the above unsigned
646  * decimal functions.
647  */
648 
649 /**
650  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
651  * @name: a pointer to a string containing the name of the file to create.
652  * @mode: the permission that the file should have
653  * @parent: a pointer to the parent dentry for this file.  This should be a
654  *          directory dentry if set.  If this parameter is %NULL, then the
655  *          file will be created in the root of the debugfs filesystem.
656  * @value: a pointer to the variable that the file should read to and write
657  *         from.
658  */
debugfs_create_x8(const char * name,umode_t mode,struct dentry * parent,u8 * value)659 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
660 		       u8 *value)
661 {
662 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
663 				   &fops_x8_ro, &fops_x8_wo);
664 }
665 EXPORT_SYMBOL_GPL(debugfs_create_x8);
666 
667 /**
668  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
669  * @name: a pointer to a string containing the name of the file to create.
670  * @mode: the permission that the file should have
671  * @parent: a pointer to the parent dentry for this file.  This should be a
672  *          directory dentry if set.  If this parameter is %NULL, then the
673  *          file will be created in the root of the debugfs filesystem.
674  * @value: a pointer to the variable that the file should read to and write
675  *         from.
676  */
debugfs_create_x16(const char * name,umode_t mode,struct dentry * parent,u16 * value)677 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
678 			u16 *value)
679 {
680 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
681 				   &fops_x16_ro, &fops_x16_wo);
682 }
683 EXPORT_SYMBOL_GPL(debugfs_create_x16);
684 
685 /**
686  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-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  */
debugfs_create_x32(const char * name,umode_t mode,struct dentry * parent,u32 * value)695 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
696 			u32 *value)
697 {
698 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
699 				   &fops_x32_ro, &fops_x32_wo);
700 }
701 EXPORT_SYMBOL_GPL(debugfs_create_x32);
702 
703 /**
704  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
705  * @name: a pointer to a string containing the name of the file to create.
706  * @mode: the permission that the file should have
707  * @parent: a pointer to the parent dentry for this file.  This should be a
708  *          directory dentry if set.  If this parameter is %NULL, then the
709  *          file will be created in the root of the debugfs filesystem.
710  * @value: a pointer to the variable that the file should read to and write
711  *         from.
712  */
debugfs_create_x64(const char * name,umode_t mode,struct dentry * parent,u64 * value)713 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
714 			u64 *value)
715 {
716 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
717 				   &fops_x64_ro, &fops_x64_wo);
718 }
719 EXPORT_SYMBOL_GPL(debugfs_create_x64);
720 
721 
debugfs_size_t_set(void * data,u64 val)722 static int debugfs_size_t_set(void *data, u64 val)
723 {
724 	*(size_t *)data = val;
725 	return 0;
726 }
debugfs_size_t_get(void * data,u64 * val)727 static int debugfs_size_t_get(void *data, u64 *val)
728 {
729 	*val = *(size_t *)data;
730 	return 0;
731 }
732 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
733 			"%llu\n"); /* %llu and %zu are more or less the same */
734 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
735 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
736 
737 /**
738  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
739  * @name: a pointer to a string containing the name of the file to create.
740  * @mode: the permission that the file should have
741  * @parent: a pointer to the parent dentry for this file.  This should be a
742  *          directory dentry if set.  If this parameter is %NULL, then the
743  *          file will be created in the root of the debugfs filesystem.
744  * @value: a pointer to the variable that the file should read to and write
745  *         from.
746  */
debugfs_create_size_t(const char * name,umode_t mode,struct dentry * parent,size_t * value)747 void debugfs_create_size_t(const char *name, umode_t mode,
748 			   struct dentry *parent, size_t *value)
749 {
750 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
751 				   &fops_size_t_ro, &fops_size_t_wo);
752 }
753 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
754 
debugfs_atomic_t_set(void * data,u64 val)755 static int debugfs_atomic_t_set(void *data, u64 val)
756 {
757 	atomic_set((atomic_t *)data, val);
758 	return 0;
759 }
debugfs_atomic_t_get(void * data,u64 * val)760 static int debugfs_atomic_t_get(void *data, u64 *val)
761 {
762 	*val = atomic_read((atomic_t *)data);
763 	return 0;
764 }
765 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
766 			debugfs_atomic_t_set, "%lld\n");
767 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
768 			"%lld\n");
769 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
770 			"%lld\n");
771 
772 /**
773  * debugfs_create_atomic_t - create a debugfs file that is used to read and
774  * write an atomic_t 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_atomic_t(const char * name,umode_t mode,struct dentry * parent,atomic_t * value)783 void debugfs_create_atomic_t(const char *name, umode_t mode,
784 			     struct dentry *parent, atomic_t *value)
785 {
786 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
787 				   &fops_atomic_t_ro, &fops_atomic_t_wo);
788 }
789 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
790 
debugfs_read_file_bool(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)791 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
792 			       size_t count, loff_t *ppos)
793 {
794 	char buf[2];
795 	bool val;
796 	int r;
797 	struct dentry *dentry = F_DENTRY(file);
798 
799 	r = debugfs_file_get(dentry);
800 	if (unlikely(r))
801 		return r;
802 	val = *(bool *)file->private_data;
803 	debugfs_file_put(dentry);
804 
805 	if (val)
806 		buf[0] = 'Y';
807 	else
808 		buf[0] = 'N';
809 	buf[1] = '\n';
810 	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
811 }
812 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
813 
debugfs_write_file_bool(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)814 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
815 				size_t count, loff_t *ppos)
816 {
817 	bool bv;
818 	int r;
819 	bool *val = file->private_data;
820 	struct dentry *dentry = F_DENTRY(file);
821 
822 	r = kstrtobool_from_user(user_buf, count, &bv);
823 	if (!r) {
824 		r = debugfs_file_get(dentry);
825 		if (unlikely(r))
826 			return r;
827 		*val = bv;
828 		debugfs_file_put(dentry);
829 	}
830 
831 	return count;
832 }
833 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
834 
835 static const struct file_operations fops_bool = {
836 	.read =		debugfs_read_file_bool,
837 	.write =	debugfs_write_file_bool,
838 	.open =		simple_open,
839 	.llseek =	default_llseek,
840 };
841 
842 static const struct file_operations fops_bool_ro = {
843 	.read =		debugfs_read_file_bool,
844 	.open =		simple_open,
845 	.llseek =	default_llseek,
846 };
847 
848 static const struct file_operations fops_bool_wo = {
849 	.write =	debugfs_write_file_bool,
850 	.open =		simple_open,
851 	.llseek =	default_llseek,
852 };
853 
854 /**
855  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
856  * @name: a pointer to a string containing the name of the file to create.
857  * @mode: the permission that the file should have
858  * @parent: a pointer to the parent dentry for this file.  This should be a
859  *          directory dentry if set.  If this parameter is %NULL, then the
860  *          file will be created in the root of the debugfs filesystem.
861  * @value: a pointer to the variable that the file should read to and write
862  *         from.
863  *
864  * This function creates a file in debugfs with the given name that
865  * contains the value of the variable @value.  If the @mode variable is so
866  * set, it can be read from, and written to.
867  */
debugfs_create_bool(const char * name,umode_t mode,struct dentry * parent,bool * value)868 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
869 			 bool *value)
870 {
871 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
872 				   &fops_bool_ro, &fops_bool_wo);
873 }
874 EXPORT_SYMBOL_GPL(debugfs_create_bool);
875 
debugfs_read_file_str(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)876 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
877 			      size_t count, loff_t *ppos)
878 {
879 	struct dentry *dentry = F_DENTRY(file);
880 	char *str, *copy = NULL;
881 	int copy_len, len;
882 	ssize_t ret;
883 
884 	ret = debugfs_file_get(dentry);
885 	if (unlikely(ret))
886 		return ret;
887 
888 	str = *(char **)file->private_data;
889 	len = strlen(str) + 1;
890 	copy = kmalloc(len, GFP_KERNEL);
891 	if (!copy) {
892 		debugfs_file_put(dentry);
893 		return -ENOMEM;
894 	}
895 
896 	copy_len = strscpy(copy, str, len);
897 	debugfs_file_put(dentry);
898 	if (copy_len < 0) {
899 		kfree(copy);
900 		return copy_len;
901 	}
902 
903 	copy[copy_len] = '\n';
904 
905 	ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
906 	kfree(copy);
907 
908 	return ret;
909 }
910 EXPORT_SYMBOL_GPL(debugfs_create_str);
911 
debugfs_write_file_str(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)912 static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
913 				      size_t count, loff_t *ppos)
914 {
915 	struct dentry *dentry = F_DENTRY(file);
916 	char *old, *new = NULL;
917 	int pos = *ppos;
918 	int r;
919 
920 	r = debugfs_file_get(dentry);
921 	if (unlikely(r))
922 		return r;
923 
924 	old = *(char **)file->private_data;
925 
926 	/* only allow strict concatenation */
927 	r = -EINVAL;
928 	if (pos && pos != strlen(old))
929 		goto error;
930 
931 	r = -E2BIG;
932 	if (pos + count + 1 > PAGE_SIZE)
933 		goto error;
934 
935 	r = -ENOMEM;
936 	new = kmalloc(pos + count + 1, GFP_KERNEL);
937 	if (!new)
938 		goto error;
939 
940 	if (pos)
941 		memcpy(new, old, pos);
942 
943 	r = -EFAULT;
944 	if (copy_from_user(new + pos, user_buf, count))
945 		goto error;
946 
947 	new[pos + count] = '\0';
948 	strim(new);
949 
950 	rcu_assign_pointer(*(char __rcu **)file->private_data, new);
951 	synchronize_rcu();
952 	kfree(old);
953 
954 	debugfs_file_put(dentry);
955 	return count;
956 
957 error:
958 	kfree(new);
959 	debugfs_file_put(dentry);
960 	return r;
961 }
962 
963 static const struct file_operations fops_str = {
964 	.read =		debugfs_read_file_str,
965 	.write =	debugfs_write_file_str,
966 	.open =		simple_open,
967 	.llseek =	default_llseek,
968 };
969 
970 static const struct file_operations fops_str_ro = {
971 	.read =		debugfs_read_file_str,
972 	.open =		simple_open,
973 	.llseek =	default_llseek,
974 };
975 
976 static const struct file_operations fops_str_wo = {
977 	.write =	debugfs_write_file_str,
978 	.open =		simple_open,
979 	.llseek =	default_llseek,
980 };
981 
982 /**
983  * debugfs_create_str - create a debugfs file that is used to read and write a string value
984  * @name: a pointer to a string containing the name of the file to create.
985  * @mode: the permission that the file should have
986  * @parent: a pointer to the parent dentry for this file.  This should be a
987  *          directory dentry if set.  If this parameter is %NULL, then the
988  *          file will be created in the root of the debugfs filesystem.
989  * @value: a pointer to the variable that the file should read to and write
990  *         from.
991  *
992  * This function creates a file in debugfs with the given name that
993  * contains the value of the variable @value.  If the @mode variable is so
994  * set, it can be read from, and written to.
995  */
debugfs_create_str(const char * name,umode_t mode,struct dentry * parent,char ** value)996 void debugfs_create_str(const char *name, umode_t mode,
997 			struct dentry *parent, char **value)
998 {
999 	debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
1000 				   &fops_str_ro, &fops_str_wo);
1001 }
1002 
read_file_blob(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)1003 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
1004 			      size_t count, loff_t *ppos)
1005 {
1006 	struct debugfs_blob_wrapper *blob = file->private_data;
1007 	struct dentry *dentry = F_DENTRY(file);
1008 	ssize_t r;
1009 
1010 	r = debugfs_file_get(dentry);
1011 	if (unlikely(r))
1012 		return r;
1013 	r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
1014 				blob->size);
1015 	debugfs_file_put(dentry);
1016 	return r;
1017 }
1018 
1019 static const struct file_operations fops_blob = {
1020 	.read =		read_file_blob,
1021 	.open =		simple_open,
1022 	.llseek =	default_llseek,
1023 };
1024 
1025 /**
1026  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
1027  * @name: a pointer to a string containing the name of the file to create.
1028  * @mode: the read permission that the file should have (other permissions are
1029  *	  masked out)
1030  * @parent: a pointer to the parent dentry for this file.  This should be a
1031  *          directory dentry if set.  If this parameter is %NULL, then the
1032  *          file will be created in the root of the debugfs filesystem.
1033  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
1034  *        to the blob data and the size of the data.
1035  *
1036  * This function creates a file in debugfs with the given name that exports
1037  * @blob->data as a binary blob. If the @mode variable is so set it can be
1038  * read from. Writing is not supported.
1039  *
1040  * This function will return a pointer to a dentry if it succeeds.  This
1041  * pointer must be passed to the debugfs_remove() function when the file is
1042  * to be removed (no automatic cleanup happens if your module is unloaded,
1043  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
1044  * returned.
1045  *
1046  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
1047  * be returned.
1048  */
debugfs_create_blob(const char * name,umode_t mode,struct dentry * parent,struct debugfs_blob_wrapper * blob)1049 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
1050 				   struct dentry *parent,
1051 				   struct debugfs_blob_wrapper *blob)
1052 {
1053 	return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
1054 }
1055 EXPORT_SYMBOL_GPL(debugfs_create_blob);
1056 
u32_format_array(char * buf,size_t bufsize,u32 * array,int array_size)1057 static size_t u32_format_array(char *buf, size_t bufsize,
1058 			       u32 *array, int array_size)
1059 {
1060 	size_t ret = 0;
1061 
1062 	while (--array_size >= 0) {
1063 		size_t len;
1064 		char term = array_size ? ' ' : '\n';
1065 
1066 		len = snprintf(buf, bufsize, "%u%c", *array++, term);
1067 		ret += len;
1068 
1069 		buf += len;
1070 		bufsize -= len;
1071 	}
1072 	return ret;
1073 }
1074 
u32_array_open(struct inode * inode,struct file * file)1075 static int u32_array_open(struct inode *inode, struct file *file)
1076 {
1077 	struct debugfs_u32_array *data = inode->i_private;
1078 	int size, elements = data->n_elements;
1079 	char *buf;
1080 
1081 	/*
1082 	 * Max size:
1083 	 *  - 10 digits + ' '/'\n' = 11 bytes per number
1084 	 *  - terminating NUL character
1085 	 */
1086 	size = elements*11;
1087 	buf = kmalloc(size+1, GFP_KERNEL);
1088 	if (!buf)
1089 		return -ENOMEM;
1090 	buf[size] = 0;
1091 
1092 	file->private_data = buf;
1093 	u32_format_array(buf, size, data->array, data->n_elements);
1094 
1095 	return nonseekable_open(inode, file);
1096 }
1097 
u32_array_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)1098 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
1099 			      loff_t *ppos)
1100 {
1101 	size_t size = strlen(file->private_data);
1102 
1103 	return simple_read_from_buffer(buf, len, ppos,
1104 					file->private_data, size);
1105 }
1106 
u32_array_release(struct inode * inode,struct file * file)1107 static int u32_array_release(struct inode *inode, struct file *file)
1108 {
1109 	kfree(file->private_data);
1110 
1111 	return 0;
1112 }
1113 
1114 static const struct file_operations u32_array_fops = {
1115 	.owner	 = THIS_MODULE,
1116 	.open	 = u32_array_open,
1117 	.release = u32_array_release,
1118 	.read	 = u32_array_read,
1119 	.llseek  = no_llseek,
1120 };
1121 
1122 /**
1123  * debugfs_create_u32_array - create a debugfs file that is used to read u32
1124  * array.
1125  * @name: a pointer to a string containing the name of the file to create.
1126  * @mode: the permission that the file should have.
1127  * @parent: a pointer to the parent dentry for this file.  This should be a
1128  *          directory dentry if set.  If this parameter is %NULL, then the
1129  *          file will be created in the root of the debugfs filesystem.
1130  * @array: wrapper struct containing data pointer and size of the array.
1131  *
1132  * This function creates a file in debugfs with the given name that exports
1133  * @array as data. If the @mode variable is so set it can be read from.
1134  * Writing is not supported. Seek within the file is also not supported.
1135  * Once array is created its size can not be changed.
1136  */
debugfs_create_u32_array(const char * name,umode_t mode,struct dentry * parent,struct debugfs_u32_array * array)1137 void debugfs_create_u32_array(const char *name, umode_t mode,
1138 			      struct dentry *parent,
1139 			      struct debugfs_u32_array *array)
1140 {
1141 	debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
1142 }
1143 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
1144 
1145 #ifdef CONFIG_HAS_IOMEM
1146 
1147 /*
1148  * The regset32 stuff is used to print 32-bit registers using the
1149  * seq_file utilities. We offer printing a register set in an already-opened
1150  * sequential file or create a debugfs file that only prints a regset32.
1151  */
1152 
1153 /**
1154  * debugfs_print_regs32 - use seq_print to describe a set of registers
1155  * @s: the seq_file structure being used to generate output
1156  * @regs: an array if struct debugfs_reg32 structures
1157  * @nregs: the length of the above array
1158  * @base: the base address to be used in reading the registers
1159  * @prefix: a string to be prefixed to every output line
1160  *
1161  * This function outputs a text block describing the current values of
1162  * some 32-bit hardware registers. It is meant to be used within debugfs
1163  * files based on seq_file that need to show registers, intermixed with other
1164  * information. The prefix argument may be used to specify a leading string,
1165  * because some peripherals have several blocks of identical registers,
1166  * for example configuration of dma channels
1167  */
debugfs_print_regs32(struct seq_file * s,const struct debugfs_reg32 * regs,int nregs,void __iomem * base,char * prefix)1168 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
1169 			  int nregs, void __iomem *base, char *prefix)
1170 {
1171 	int i;
1172 
1173 	for (i = 0; i < nregs; i++, regs++) {
1174 		if (prefix)
1175 			seq_printf(s, "%s", prefix);
1176 		seq_printf(s, "%s = 0x%08x\n", regs->name,
1177 			   readl(base + regs->offset));
1178 		if (seq_has_overflowed(s))
1179 			break;
1180 	}
1181 }
1182 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
1183 
debugfs_regset32_show(struct seq_file * s,void * data)1184 static int debugfs_regset32_show(struct seq_file *s, void *data)
1185 {
1186 	struct debugfs_regset32 *regset = s->private;
1187 
1188 	if (regset->dev)
1189 		pm_runtime_get_sync(regset->dev);
1190 
1191 	debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
1192 
1193 	if (regset->dev)
1194 		pm_runtime_put(regset->dev);
1195 
1196 	return 0;
1197 }
1198 
1199 DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
1200 
1201 /**
1202  * debugfs_create_regset32 - create a debugfs file that returns register values
1203  * @name: a pointer to a string containing the name of the file to create.
1204  * @mode: the permission that the file should have
1205  * @parent: a pointer to the parent dentry for this file.  This should be a
1206  *          directory dentry if set.  If this parameter is %NULL, then the
1207  *          file will be created in the root of the debugfs filesystem.
1208  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
1209  *          to an array of register definitions, the array size and the base
1210  *          address where the register bank is to be found.
1211  *
1212  * This function creates a file in debugfs with the given name that reports
1213  * the names and values of a set of 32-bit registers. If the @mode variable
1214  * is so set it can be read from. Writing is not supported.
1215  */
debugfs_create_regset32(const char * name,umode_t mode,struct dentry * parent,struct debugfs_regset32 * regset)1216 void debugfs_create_regset32(const char *name, umode_t mode,
1217 			     struct dentry *parent,
1218 			     struct debugfs_regset32 *regset)
1219 {
1220 	debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
1221 }
1222 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
1223 
1224 #endif /* CONFIG_HAS_IOMEM */
1225 
1226 struct debugfs_devm_entry {
1227 	int (*read)(struct seq_file *seq, void *data);
1228 	struct device *dev;
1229 };
1230 
debugfs_devm_entry_open(struct inode * inode,struct file * f)1231 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
1232 {
1233 	struct debugfs_devm_entry *entry = inode->i_private;
1234 
1235 	return single_open(f, entry->read, entry->dev);
1236 }
1237 
1238 static const struct file_operations debugfs_devm_entry_ops = {
1239 	.owner = THIS_MODULE,
1240 	.open = debugfs_devm_entry_open,
1241 	.release = single_release,
1242 	.read = seq_read,
1243 	.llseek = seq_lseek
1244 };
1245 
1246 /**
1247  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
1248  *
1249  * @dev: device related to this debugfs file.
1250  * @name: name of the debugfs file.
1251  * @parent: a pointer to the parent dentry for this file.  This should be a
1252  *	directory dentry if set.  If this parameter is %NULL, then the
1253  *	file will be created in the root of the debugfs filesystem.
1254  * @read_fn: function pointer called to print the seq_file content.
1255  */
debugfs_create_devm_seqfile(struct device * dev,const char * name,struct dentry * parent,int (* read_fn)(struct seq_file * s,void * data))1256 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
1257 				 struct dentry *parent,
1258 				 int (*read_fn)(struct seq_file *s, void *data))
1259 {
1260 	struct debugfs_devm_entry *entry;
1261 
1262 	if (IS_ERR(parent))
1263 		return;
1264 
1265 	entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
1266 	if (!entry)
1267 		return;
1268 
1269 	entry->read = read_fn;
1270 	entry->dev = dev;
1271 
1272 	debugfs_create_file(name, S_IRUGO, parent, entry,
1273 			    &debugfs_devm_entry_ops);
1274 }
1275 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);
1276