Greg Kroah-Hartman | 3bce94fd | 2017-11-07 16:59:23 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * debugfs is for people to use instead of /proc or /sys. |
Mauro Carvalho Chehab | e1b4fc7 | 2017-05-14 12:04:55 -0300 | [diff] [blame] | 9 | * See Documentation/filesystems/ for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/fs.h> |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/debugfs.h> |
Alessandro Rubini | 03e099f | 2011-11-21 10:01:40 +0100 | [diff] [blame] | 17 | #include <linux/io.h> |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 18 | #include <linux/slab.h> |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 19 | #include <linux/atomic.h> |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 20 | #include <linux/device.h> |
Geert Uytterhoeven | 30332ee | 2020-02-11 19:18:55 +0100 | [diff] [blame] | 21 | #include <linux/pm_runtime.h> |
Al Viro | cfe3944 | 2018-02-01 12:14:57 -0500 | [diff] [blame] | 22 | #include <linux/poll.h> |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 23 | #include <linux/security.h> |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 24 | |
| 25 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 27 | struct poll_table_struct; |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 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 | |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 41 | const struct file_operations debugfs_noop_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | .read = default_read_file, |
| 43 | .write = default_write_file, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 44 | .open = simple_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 45 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 48 | #define F_DENTRY(filp) ((filp)->f_path.dentry) |
| 49 | |
Nicolai Stange | 7c8d469 | 2017-10-31 00:15:47 +0100 | [diff] [blame] | 50 | const struct file_operations *debugfs_real_fops(const struct file *filp) |
Nicolai Stange | 7c8d469 | 2017-10-31 00:15:47 +0100 | [diff] [blame] | 51 | { |
| 52 | struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata; |
Nicolai Stange | 055ab8e | 2017-10-31 00:15:49 +0100 | [diff] [blame] | 53 | |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 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 | |
Nicolai Stange | 7c8d469 | 2017-10-31 00:15:47 +0100 | [diff] [blame] | 63 | return fsd->real_fops; |
| 64 | } |
| 65 | EXPORT_SYMBOL_GPL(debugfs_real_fops); |
| 66 | |
Nicolai Stange | e9117a5 | 2017-10-31 00:15:48 +0100 | [diff] [blame] | 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 | */ |
| 82 | int debugfs_file_get(struct dentry *dentry) |
| 83 | { |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 84 | struct debugfs_fsdata *fsd; |
| 85 | void *d_fsd; |
Nicolai Stange | e9117a5 | 2017-10-31 00:15:48 +0100 | [diff] [blame] | 86 | |
Johannes Berg | 0ed04a1 | 2023-11-24 17:25:24 +0100 | [diff] [blame] | 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 | |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 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); |
Johannes Berg | 159f5bd | 2023-12-21 15:04:45 +0100 | [diff] [blame] | 107 | INIT_LIST_HEAD(&fsd->cancellations); |
| 108 | mutex_init(&fsd->cancellations_mtx); |
| 109 | |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 110 | if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) { |
Johannes Berg | 159f5bd | 2023-12-21 15:04:45 +0100 | [diff] [blame] | 111 | mutex_destroy(&fsd->cancellations_mtx); |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 112 | kfree(fsd); |
| 113 | fsd = READ_ONCE(dentry->d_fsdata); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * In case of a successful cmpxchg() above, this check is |
| 119 | * strictly necessary and must follow it, see the comment in |
| 120 | * __debugfs_remove_file(). |
| 121 | * OTOH, if the cmpxchg() hasn't been executed or wasn't |
| 122 | * successful, this serves the purpose of not starving |
| 123 | * removers. |
| 124 | */ |
Nicolai Stange | e9117a5 | 2017-10-31 00:15:48 +0100 | [diff] [blame] | 125 | if (d_unlinked(dentry)) |
| 126 | return -EIO; |
| 127 | |
| 128 | if (!refcount_inc_not_zero(&fsd->active_users)) |
| 129 | return -EIO; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(debugfs_file_get); |
| 134 | |
| 135 | /** |
| 136 | * debugfs_file_put - mark the end of file data access |
| 137 | * @dentry: the dentry object formerly passed to |
| 138 | * debugfs_file_get(). |
| 139 | * |
| 140 | * Allow any ongoing concurrent call into debugfs_remove() or |
| 141 | * debugfs_remove_recursive() blocked by a former call to |
| 142 | * debugfs_file_get() to proceed and return to its caller. |
| 143 | */ |
| 144 | void debugfs_file_put(struct dentry *dentry) |
| 145 | { |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 146 | struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata); |
Nicolai Stange | e9117a5 | 2017-10-31 00:15:48 +0100 | [diff] [blame] | 147 | |
| 148 | if (refcount_dec_and_test(&fsd->active_users)) |
| 149 | complete(&fsd->active_users_drained); |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(debugfs_file_put); |
| 152 | |
Johannes Berg | 8c88a47 | 2023-11-24 17:25:26 +0100 | [diff] [blame] | 153 | /** |
| 154 | * debugfs_enter_cancellation - enter a debugfs cancellation |
| 155 | * @file: the file being accessed |
| 156 | * @cancellation: the cancellation object, the cancel callback |
| 157 | * inside of it must be initialized |
| 158 | * |
| 159 | * When a debugfs file is removed it needs to wait for all active |
| 160 | * operations to complete. However, the operation itself may need |
| 161 | * to wait for hardware or completion of some asynchronous process |
| 162 | * or similar. As such, it may need to be cancelled to avoid long |
| 163 | * waits or even deadlocks. |
| 164 | * |
| 165 | * This function can be used inside a debugfs handler that may |
| 166 | * need to be cancelled. As soon as this function is called, the |
| 167 | * cancellation's 'cancel' callback may be called, at which point |
| 168 | * the caller should proceed to call debugfs_leave_cancellation() |
| 169 | * and leave the debugfs handler function as soon as possible. |
| 170 | * Note that the 'cancel' callback is only ever called in the |
| 171 | * context of some kind of debugfs_remove(). |
| 172 | * |
| 173 | * This function must be paired with debugfs_leave_cancellation(). |
| 174 | */ |
| 175 | void debugfs_enter_cancellation(struct file *file, |
| 176 | struct debugfs_cancellation *cancellation) |
| 177 | { |
| 178 | struct debugfs_fsdata *fsd; |
| 179 | struct dentry *dentry = F_DENTRY(file); |
| 180 | |
| 181 | INIT_LIST_HEAD(&cancellation->list); |
| 182 | |
| 183 | if (WARN_ON(!d_is_reg(dentry))) |
| 184 | return; |
| 185 | |
| 186 | if (WARN_ON(!cancellation->cancel)) |
| 187 | return; |
| 188 | |
| 189 | fsd = READ_ONCE(dentry->d_fsdata); |
| 190 | if (WARN_ON(!fsd || |
| 191 | ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))) |
| 192 | return; |
| 193 | |
| 194 | mutex_lock(&fsd->cancellations_mtx); |
| 195 | list_add(&cancellation->list, &fsd->cancellations); |
| 196 | mutex_unlock(&fsd->cancellations_mtx); |
| 197 | |
| 198 | /* if we're already removing wake it up to cancel */ |
| 199 | if (d_unlinked(dentry)) |
| 200 | complete(&fsd->active_users_drained); |
| 201 | } |
| 202 | EXPORT_SYMBOL_GPL(debugfs_enter_cancellation); |
| 203 | |
| 204 | /** |
| 205 | * debugfs_leave_cancellation - leave cancellation section |
| 206 | * @file: the file being accessed |
| 207 | * @cancellation: the cancellation previously registered with |
| 208 | * debugfs_enter_cancellation() |
| 209 | * |
| 210 | * See the documentation of debugfs_enter_cancellation(). |
| 211 | */ |
| 212 | void debugfs_leave_cancellation(struct file *file, |
| 213 | struct debugfs_cancellation *cancellation) |
| 214 | { |
| 215 | struct debugfs_fsdata *fsd; |
| 216 | struct dentry *dentry = F_DENTRY(file); |
| 217 | |
| 218 | if (WARN_ON(!d_is_reg(dentry))) |
| 219 | return; |
| 220 | |
| 221 | fsd = READ_ONCE(dentry->d_fsdata); |
| 222 | if (WARN_ON(!fsd || |
| 223 | ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT))) |
| 224 | return; |
| 225 | |
| 226 | mutex_lock(&fsd->cancellations_mtx); |
| 227 | if (!list_empty(&cancellation->list)) |
| 228 | list_del(&cancellation->list); |
| 229 | mutex_unlock(&fsd->cancellations_mtx); |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(debugfs_leave_cancellation); |
| 232 | |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 233 | /* |
| 234 | * Only permit access to world-readable files when the kernel is locked down. |
| 235 | * We also need to exclude any file that has ways to write or alter it as root |
| 236 | * can bypass the permissions check. |
| 237 | */ |
Eric Snowberg | a37f495 | 2019-12-07 11:16:03 -0500 | [diff] [blame] | 238 | static int debugfs_locked_down(struct inode *inode, |
| 239 | struct file *filp, |
| 240 | const struct file_operations *real_fops) |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 241 | { |
Michal Suchanek | 358fcf5 | 2022-01-04 18:05:05 +0100 | [diff] [blame] | 242 | if ((inode->i_mode & 07777 & ~0444) == 0 && |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 243 | !(filp->f_mode & FMODE_WRITE) && |
| 244 | !real_fops->unlocked_ioctl && |
| 245 | !real_fops->compat_ioctl && |
| 246 | !real_fops->mmap) |
Eric Snowberg | a37f495 | 2019-12-07 11:16:03 -0500 | [diff] [blame] | 247 | return 0; |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 248 | |
Eric Snowberg | a37f495 | 2019-12-07 11:16:03 -0500 | [diff] [blame] | 249 | if (security_locked_down(LOCKDOWN_DEBUGFS)) |
| 250 | return -EPERM; |
| 251 | |
| 252 | return 0; |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 255 | static int open_proxy_open(struct inode *inode, struct file *filp) |
| 256 | { |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 257 | struct dentry *dentry = F_DENTRY(filp); |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 258 | const struct file_operations *real_fops = NULL; |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 259 | int r; |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 260 | |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 261 | r = debugfs_file_get(dentry); |
| 262 | if (r) |
| 263 | return r == -EIO ? -ENOENT : r; |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 264 | |
Christian Lamparter | 86f0e06 | 2016-09-17 21:43:01 +0200 | [diff] [blame] | 265 | real_fops = debugfs_real_fops(filp); |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 266 | |
Eric Snowberg | a37f495 | 2019-12-07 11:16:03 -0500 | [diff] [blame] | 267 | r = debugfs_locked_down(inode, filp, real_fops); |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 268 | if (r) |
| 269 | goto out; |
| 270 | |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 271 | if (!fops_get(real_fops)) { |
Vladis Dronov | e3b9fc7 | 2020-08-11 17:01:29 +0200 | [diff] [blame] | 272 | #ifdef CONFIG_MODULES |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 273 | if (real_fops->owner && |
Sven Eckelmann | 112cedc | 2021-08-02 18:24:44 +0200 | [diff] [blame] | 274 | real_fops->owner->state == MODULE_STATE_GOING) { |
| 275 | r = -ENXIO; |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 276 | goto out; |
Sven Eckelmann | 112cedc | 2021-08-02 18:24:44 +0200 | [diff] [blame] | 277 | } |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 278 | #endif |
| 279 | |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 280 | /* Huh? Module did not clean up after itself at exit? */ |
| 281 | WARN(1, "debugfs file owner did not clean up at exit: %pd", |
| 282 | dentry); |
| 283 | r = -ENXIO; |
| 284 | goto out; |
| 285 | } |
| 286 | replace_fops(filp, real_fops); |
| 287 | |
| 288 | if (real_fops->open) |
| 289 | r = real_fops->open(inode, filp); |
| 290 | |
| 291 | out: |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 292 | debugfs_file_put(dentry); |
Nicolai Stange | 9fd4dce | 2016-03-22 14:11:13 +0100 | [diff] [blame] | 293 | return r; |
| 294 | } |
| 295 | |
| 296 | const struct file_operations debugfs_open_proxy_file_operations = { |
| 297 | .open = open_proxy_open, |
| 298 | }; |
| 299 | |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 300 | #define PROTO(args...) args |
| 301 | #define ARGS(args...) args |
| 302 | |
| 303 | #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args) \ |
| 304 | static ret_type full_proxy_ ## name(proto) \ |
| 305 | { \ |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 306 | struct dentry *dentry = F_DENTRY(filp); \ |
Nicolai Stange | 154b9d7 | 2017-10-31 00:15:53 +0100 | [diff] [blame] | 307 | const struct file_operations *real_fops; \ |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 308 | ret_type r; \ |
| 309 | \ |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 310 | r = debugfs_file_get(dentry); \ |
| 311 | if (unlikely(r)) \ |
| 312 | return r; \ |
Nicolai Stange | 154b9d7 | 2017-10-31 00:15:53 +0100 | [diff] [blame] | 313 | real_fops = debugfs_real_fops(filp); \ |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 314 | r = real_fops->name(args); \ |
| 315 | debugfs_file_put(dentry); \ |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 316 | return r; \ |
| 317 | } |
| 318 | |
| 319 | FULL_PROXY_FUNC(llseek, loff_t, filp, |
| 320 | PROTO(struct file *filp, loff_t offset, int whence), |
| 321 | ARGS(filp, offset, whence)); |
| 322 | |
| 323 | FULL_PROXY_FUNC(read, ssize_t, filp, |
| 324 | PROTO(struct file *filp, char __user *buf, size_t size, |
| 325 | loff_t *ppos), |
| 326 | ARGS(filp, buf, size, ppos)); |
| 327 | |
| 328 | FULL_PROXY_FUNC(write, ssize_t, filp, |
| 329 | PROTO(struct file *filp, const char __user *buf, size_t size, |
| 330 | loff_t *ppos), |
| 331 | ARGS(filp, buf, size, ppos)); |
| 332 | |
| 333 | FULL_PROXY_FUNC(unlocked_ioctl, long, filp, |
| 334 | PROTO(struct file *filp, unsigned int cmd, unsigned long arg), |
| 335 | ARGS(filp, cmd, arg)); |
| 336 | |
Al Viro | 076ccb7 | 2017-07-03 01:02:18 -0400 | [diff] [blame] | 337 | static __poll_t full_proxy_poll(struct file *filp, |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 338 | struct poll_table_struct *wait) |
| 339 | { |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 340 | struct dentry *dentry = F_DENTRY(filp); |
Al Viro | e6c8adc | 2017-07-03 22:25:56 -0400 | [diff] [blame] | 341 | __poll_t r = 0; |
Nicolai Stange | 154b9d7 | 2017-10-31 00:15:53 +0100 | [diff] [blame] | 342 | const struct file_operations *real_fops; |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 343 | |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 344 | if (debugfs_file_get(dentry)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 345 | return EPOLLHUP; |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 346 | |
Nicolai Stange | 154b9d7 | 2017-10-31 00:15:53 +0100 | [diff] [blame] | 347 | real_fops = debugfs_real_fops(filp); |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 348 | r = real_fops->poll(filp, wait); |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 349 | debugfs_file_put(dentry); |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 350 | return r; |
| 351 | } |
| 352 | |
| 353 | static int full_proxy_release(struct inode *inode, struct file *filp) |
| 354 | { |
| 355 | const struct dentry *dentry = F_DENTRY(filp); |
Christian Lamparter | 86f0e06 | 2016-09-17 21:43:01 +0200 | [diff] [blame] | 356 | const struct file_operations *real_fops = debugfs_real_fops(filp); |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 357 | const struct file_operations *proxy_fops = filp->f_op; |
| 358 | int r = 0; |
| 359 | |
| 360 | /* |
| 361 | * We must not protect this against removal races here: the |
| 362 | * original releaser should be called unconditionally in order |
| 363 | * not to leak any resources. Releasers must not assume that |
| 364 | * ->i_private is still being meaningful here. |
| 365 | */ |
| 366 | if (real_fops->release) |
| 367 | r = real_fops->release(inode, filp); |
| 368 | |
| 369 | replace_fops(filp, d_inode(dentry)->i_fop); |
Xu Wang | c80a67b | 2020-07-09 05:40:33 +0000 | [diff] [blame] | 370 | kfree(proxy_fops); |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 371 | fops_put(real_fops); |
Eric Engestrom | a1a9e5d | 2016-09-21 10:27:36 +0100 | [diff] [blame] | 372 | return r; |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void __full_proxy_fops_init(struct file_operations *proxy_fops, |
| 376 | const struct file_operations *real_fops) |
| 377 | { |
| 378 | proxy_fops->release = full_proxy_release; |
| 379 | if (real_fops->llseek) |
| 380 | proxy_fops->llseek = full_proxy_llseek; |
| 381 | if (real_fops->read) |
| 382 | proxy_fops->read = full_proxy_read; |
| 383 | if (real_fops->write) |
| 384 | proxy_fops->write = full_proxy_write; |
| 385 | if (real_fops->poll) |
| 386 | proxy_fops->poll = full_proxy_poll; |
| 387 | if (real_fops->unlocked_ioctl) |
| 388 | proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl; |
| 389 | } |
| 390 | |
| 391 | static int full_proxy_open(struct inode *inode, struct file *filp) |
| 392 | { |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 393 | struct dentry *dentry = F_DENTRY(filp); |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 394 | const struct file_operations *real_fops = NULL; |
| 395 | struct file_operations *proxy_fops = NULL; |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 396 | int r; |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 397 | |
Nicolai Stange | 7d39bc5 | 2017-10-31 00:15:54 +0100 | [diff] [blame] | 398 | r = debugfs_file_get(dentry); |
| 399 | if (r) |
| 400 | return r == -EIO ? -ENOENT : r; |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 401 | |
Christian Lamparter | 86f0e06 | 2016-09-17 21:43:01 +0200 | [diff] [blame] | 402 | real_fops = debugfs_real_fops(filp); |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 403 | |
Eric Snowberg | a37f495 | 2019-12-07 11:16:03 -0500 | [diff] [blame] | 404 | r = debugfs_locked_down(inode, filp, real_fops); |
David Howells | 5496197 | 2019-08-19 17:18:02 -0700 | [diff] [blame] | 405 | if (r) |
| 406 | goto out; |
| 407 | |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 408 | if (!fops_get(real_fops)) { |
Vladis Dronov | e3b9fc7 | 2020-08-11 17:01:29 +0200 | [diff] [blame] | 409 | #ifdef CONFIG_MODULES |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 410 | if (real_fops->owner && |
Sven Eckelmann | 112cedc | 2021-08-02 18:24:44 +0200 | [diff] [blame] | 411 | real_fops->owner->state == MODULE_STATE_GOING) { |
| 412 | r = -ENXIO; |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 413 | goto out; |
Sven Eckelmann | 112cedc | 2021-08-02 18:24:44 +0200 | [diff] [blame] | 414 | } |
Taehee Yoo | 275678e | 2020-02-18 04:31:50 +0000 | [diff] [blame] | 415 | #endif |
| 416 | |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 417 | /* Huh? Module did not cleanup after itself at exit? */ |
| 418 | WARN(1, "debugfs file owner did not clean up at exit: %pd", |
| 419 | dentry); |
| 420 | r = -ENXIO; |
| 421 | goto out; |
| 422 | } |
| 423 | |
| 424 | proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL); |
| 425 | if (!proxy_fops) { |
| 426 | r = -ENOMEM; |
| 427 | goto free_proxy; |
| 428 | } |
| 429 | __full_proxy_fops_init(proxy_fops, real_fops); |
| 430 | replace_fops(filp, proxy_fops); |
| 431 | |
| 432 | if (real_fops->open) { |
| 433 | r = real_fops->open(inode, filp); |
Nicolai Stange | b10e3e9 | 2016-05-24 13:08:53 +0200 | [diff] [blame] | 434 | if (r) { |
| 435 | replace_fops(filp, d_inode(dentry)->i_fop); |
| 436 | goto free_proxy; |
| 437 | } else if (filp->f_op != proxy_fops) { |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 438 | /* No protection against file removal anymore. */ |
| 439 | WARN(1, "debugfs file owner replaced proxy fops: %pd", |
| 440 | dentry); |
| 441 | goto free_proxy; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | goto out; |
| 446 | free_proxy: |
| 447 | kfree(proxy_fops); |
| 448 | fops_put(real_fops); |
| 449 | out: |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 450 | debugfs_file_put(dentry); |
Nicolai Stange | 49d200d | 2016-03-22 14:11:14 +0100 | [diff] [blame] | 451 | return r; |
| 452 | } |
| 453 | |
| 454 | const struct file_operations debugfs_full_proxy_file_operations = { |
| 455 | .open = full_proxy_open, |
| 456 | }; |
| 457 | |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 458 | ssize_t debugfs_attr_read(struct file *file, char __user *buf, |
| 459 | size_t len, loff_t *ppos) |
| 460 | { |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 461 | struct dentry *dentry = F_DENTRY(file); |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 462 | ssize_t ret; |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 463 | |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 464 | ret = debugfs_file_get(dentry); |
| 465 | if (unlikely(ret)) |
| 466 | return ret; |
| 467 | ret = simple_attr_read(file, buf, len, ppos); |
| 468 | debugfs_file_put(dentry); |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 469 | return ret; |
| 470 | } |
| 471 | EXPORT_SYMBOL_GPL(debugfs_attr_read); |
| 472 | |
Akinobu Mita | d472cf7 | 2022-09-20 02:24:18 +0900 | [diff] [blame] | 473 | static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf, |
| 474 | size_t len, loff_t *ppos, bool is_signed) |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 475 | { |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 476 | struct dentry *dentry = F_DENTRY(file); |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 477 | ssize_t ret; |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 478 | |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 479 | ret = debugfs_file_get(dentry); |
| 480 | if (unlikely(ret)) |
| 481 | return ret; |
Akinobu Mita | d472cf7 | 2022-09-20 02:24:18 +0900 | [diff] [blame] | 482 | if (is_signed) |
| 483 | ret = simple_attr_write_signed(file, buf, len, ppos); |
| 484 | else |
| 485 | ret = simple_attr_write(file, buf, len, ppos); |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 486 | debugfs_file_put(dentry); |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 487 | return ret; |
| 488 | } |
Akinobu Mita | d472cf7 | 2022-09-20 02:24:18 +0900 | [diff] [blame] | 489 | |
| 490 | ssize_t debugfs_attr_write(struct file *file, const char __user *buf, |
| 491 | size_t len, loff_t *ppos) |
| 492 | { |
| 493 | return debugfs_attr_write_xsigned(file, buf, len, ppos, false); |
| 494 | } |
Nicolai Stange | c646880 | 2016-03-22 14:11:15 +0100 | [diff] [blame] | 495 | EXPORT_SYMBOL_GPL(debugfs_attr_write); |
| 496 | |
Akinobu Mita | d472cf7 | 2022-09-20 02:24:18 +0900 | [diff] [blame] | 497 | ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf, |
| 498 | size_t len, loff_t *ppos) |
| 499 | { |
| 500 | return debugfs_attr_write_xsigned(file, buf, len, ppos, true); |
| 501 | } |
| 502 | EXPORT_SYMBOL_GPL(debugfs_attr_write_signed); |
| 503 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 504 | static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode, |
| 505 | struct dentry *parent, void *value, |
| 506 | const struct file_operations *fops, |
| 507 | const struct file_operations *fops_ro, |
| 508 | const struct file_operations *fops_wo) |
| 509 | { |
| 510 | /* if there are no write bits set, make read only */ |
| 511 | if (!(mode & S_IWUGO)) |
| 512 | return debugfs_create_file_unsafe(name, mode, parent, value, |
| 513 | fops_ro); |
| 514 | /* if there are no read bits set, make write only */ |
| 515 | if (!(mode & S_IRUGO)) |
| 516 | return debugfs_create_file_unsafe(name, mode, parent, value, |
| 517 | fops_wo); |
| 518 | |
| 519 | return debugfs_create_file_unsafe(name, mode, parent, value, fops); |
| 520 | } |
| 521 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 522 | static int debugfs_u8_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 523 | { |
| 524 | *(u8 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 525 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 526 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 527 | static int debugfs_u8_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 528 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 529 | *val = *(u8 *)data; |
| 530 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 531 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 532 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n"); |
| 533 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n"); |
| 534 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
| 536 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 537 | * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | * @name: a pointer to a string containing the name of the file to create. |
| 539 | * @mode: the permission that the file should have |
| 540 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 541 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | * file will be created in the root of the debugfs filesystem. |
| 543 | * @value: a pointer to the variable that the file should read to and write |
| 544 | * from. |
| 545 | * |
| 546 | * This function creates a file in debugfs with the given name that |
| 547 | * contains the value of the variable @value. If the @mode variable is so |
| 548 | * set, it can be read from, and written to. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | */ |
Greg Kroah-Hartman | 9655ac4 | 2019-10-11 15:29:24 +0200 | [diff] [blame] | 550 | void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent, |
| 551 | u8 *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
Greg Kroah-Hartman | 9655ac4 | 2019-10-11 15:29:24 +0200 | [diff] [blame] | 553 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 554 | &fops_u8_ro, &fops_u8_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
| 556 | EXPORT_SYMBOL_GPL(debugfs_create_u8); |
| 557 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 558 | static int debugfs_u16_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 559 | { |
| 560 | *(u16 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 561 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 562 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 563 | static int debugfs_u16_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 564 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 565 | *val = *(u16 *)data; |
| 566 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 567 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 568 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n"); |
| 569 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n"); |
| 570 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n"); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 571 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 573 | * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | * @name: a pointer to a string containing the name of the file to create. |
| 575 | * @mode: the permission that the file should have |
| 576 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 577 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | * file will be created in the root of the debugfs filesystem. |
| 579 | * @value: a pointer to the variable that the file should read to and write |
| 580 | * from. |
| 581 | * |
| 582 | * This function creates a file in debugfs with the given name that |
| 583 | * contains the value of the variable @value. If the @mode variable is so |
| 584 | * set, it can be read from, and written to. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | */ |
Greg Kroah-Hartman | 313f5db | 2019-10-11 15:29:25 +0200 | [diff] [blame] | 586 | void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent, |
| 587 | u16 *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | { |
Greg Kroah-Hartman | 313f5db | 2019-10-11 15:29:25 +0200 | [diff] [blame] | 589 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 590 | &fops_u16_ro, &fops_u16_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | EXPORT_SYMBOL_GPL(debugfs_create_u16); |
| 593 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 594 | static int debugfs_u32_set(void *data, u64 val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 595 | { |
| 596 | *(u32 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 597 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 598 | } |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 599 | static int debugfs_u32_get(void *data, u64 *val) |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 600 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 601 | *val = *(u32 *)data; |
| 602 | return 0; |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 603 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 604 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n"); |
| 605 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n"); |
| 606 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n"); |
Arnd Bergmann | acaefc2 | 2005-05-18 14:40:59 +0200 | [diff] [blame] | 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 609 | * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | * @name: a pointer to a string containing the name of the file to create. |
| 611 | * @mode: the permission that the file should have |
| 612 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 613 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | * file will be created in the root of the debugfs filesystem. |
| 615 | * @value: a pointer to the variable that the file should read to and write |
| 616 | * from. |
| 617 | * |
| 618 | * This function creates a file in debugfs with the given name that |
| 619 | * contains the value of the variable @value. If the @mode variable is so |
| 620 | * set, it can be read from, and written to. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | */ |
Greg Kroah-Hartman | 2b07021 | 2020-04-16 16:54:48 +0200 | [diff] [blame] | 622 | void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent, |
| 623 | u32 *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | { |
Greg Kroah-Hartman | 2b07021 | 2020-04-16 16:54:48 +0200 | [diff] [blame] | 625 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 626 | &fops_u32_ro, &fops_u32_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | EXPORT_SYMBOL_GPL(debugfs_create_u32); |
| 629 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 630 | static int debugfs_u64_set(void *data, u64 val) |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 631 | { |
| 632 | *(u64 *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 633 | return 0; |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 634 | } |
| 635 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 636 | static int debugfs_u64_get(void *data, u64 *val) |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 637 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 638 | *val = *(u64 *)data; |
| 639 | return 0; |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 640 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 641 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n"); |
| 642 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n"); |
| 643 | DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n"); |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 644 | |
| 645 | /** |
| 646 | * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value |
| 647 | * @name: a pointer to a string containing the name of the file to create. |
| 648 | * @mode: the permission that the file should have |
| 649 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 650 | * directory dentry if set. If this parameter is %NULL, then the |
| 651 | * file will be created in the root of the debugfs filesystem. |
| 652 | * @value: a pointer to the variable that the file should read to and write |
| 653 | * from. |
| 654 | * |
| 655 | * This function creates a file in debugfs with the given name that |
| 656 | * contains the value of the variable @value. If the @mode variable is so |
| 657 | * set, it can be read from, and written to. |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 658 | */ |
Greg Kroah-Hartman | ad26221 | 2019-10-11 15:29:26 +0200 | [diff] [blame] | 659 | void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent, |
| 660 | u64 *value) |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 661 | { |
Greg Kroah-Hartman | ad26221 | 2019-10-11 15:29:26 +0200 | [diff] [blame] | 662 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 663 | &fops_u64_ro, &fops_u64_wo); |
Michael Ellerman | 8447891 | 2007-04-17 15:59:36 +1000 | [diff] [blame] | 664 | } |
| 665 | EXPORT_SYMBOL_GPL(debugfs_create_u64); |
| 666 | |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 667 | static int debugfs_ulong_set(void *data, u64 val) |
| 668 | { |
| 669 | *(unsigned long *)data = val; |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static int debugfs_ulong_get(void *data, u64 *val) |
| 674 | { |
| 675 | *val = *(unsigned long *)data; |
| 676 | return 0; |
| 677 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 678 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set, |
| 679 | "%llu\n"); |
| 680 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n"); |
| 681 | DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n"); |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 682 | |
| 683 | /** |
| 684 | * debugfs_create_ulong - create a debugfs file that is used to read and write |
| 685 | * an unsigned long value. |
| 686 | * @name: a pointer to a string containing the name of the file to create. |
| 687 | * @mode: the permission that the file should have |
| 688 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 689 | * directory dentry if set. If this parameter is %NULL, then the |
| 690 | * file will be created in the root of the debugfs filesystem. |
| 691 | * @value: a pointer to the variable that the file should read to and write |
| 692 | * from. |
| 693 | * |
| 694 | * This function creates a file in debugfs with the given name that |
| 695 | * contains the value of the variable @value. If the @mode variable is so |
| 696 | * set, it can be read from, and written to. |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 697 | */ |
Greg Kroah-Hartman | fb05b14 | 2021-05-21 20:43:40 +0200 | [diff] [blame] | 698 | void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent, |
| 699 | unsigned long *value) |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 700 | { |
Greg Kroah-Hartman | fb05b14 | 2021-05-21 20:43:40 +0200 | [diff] [blame] | 701 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong, |
| 702 | &fops_ulong_ro, &fops_ulong_wo); |
Viresh Kumar | c23fe83 | 2015-10-18 22:43:19 +0530 | [diff] [blame] | 703 | } |
| 704 | EXPORT_SYMBOL_GPL(debugfs_create_ulong); |
| 705 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 706 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n"); |
| 707 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n"); |
| 708 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 709 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 710 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set, |
| 711 | "0x%04llx\n"); |
| 712 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n"); |
| 713 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 714 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 715 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set, |
| 716 | "0x%08llx\n"); |
| 717 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n"); |
| 718 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n"); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 719 | |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 720 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set, |
| 721 | "0x%016llx\n"); |
| 722 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n"); |
| 723 | DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n"); |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 724 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 725 | /* |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 726 | * debugfs_create_x{8,16,32,64} - create a debugfs file that is used to read and write an unsigned {8,16,32,64}-bit value |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 727 | * |
| 728 | * These functions are exactly the same as the above functions (but use a hex |
| 729 | * output for the decimal challenged). For details look at the above unsigned |
| 730 | * decimal functions. |
| 731 | */ |
| 732 | |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 733 | /** |
| 734 | * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 735 | * @name: a pointer to a string containing the name of the file to create. |
| 736 | * @mode: the permission that the file should have |
| 737 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 738 | * directory dentry if set. If this parameter is %NULL, then the |
| 739 | * file will be created in the root of the debugfs filesystem. |
| 740 | * @value: a pointer to the variable that the file should read to and write |
| 741 | * from. |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 742 | */ |
Greg Kroah-Hartman | c7c1168 | 2019-10-11 15:29:28 +0200 | [diff] [blame] | 743 | void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent, |
| 744 | u8 *value) |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 745 | { |
Greg Kroah-Hartman | c7c1168 | 2019-10-11 15:29:28 +0200 | [diff] [blame] | 746 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 747 | &fops_x8_ro, &fops_x8_wo); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 748 | } |
| 749 | EXPORT_SYMBOL_GPL(debugfs_create_x8); |
| 750 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 751 | /** |
| 752 | * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value |
| 753 | * @name: a pointer to a string containing the name of the file to create. |
| 754 | * @mode: the permission that the file should have |
| 755 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 756 | * directory dentry if set. If this parameter is %NULL, then the |
| 757 | * file will be created in the root of the debugfs filesystem. |
| 758 | * @value: a pointer to the variable that the file should read to and write |
| 759 | * from. |
| 760 | */ |
Greg Kroah-Hartman | e40d38f | 2019-10-11 15:29:29 +0200 | [diff] [blame] | 761 | void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent, |
| 762 | u16 *value) |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 763 | { |
Greg Kroah-Hartman | e40d38f | 2019-10-11 15:29:29 +0200 | [diff] [blame] | 764 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 765 | &fops_x16_ro, &fops_x16_wo); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 766 | } |
| 767 | EXPORT_SYMBOL_GPL(debugfs_create_x16); |
| 768 | |
Randy Dunlap | e6716b8 | 2007-10-15 17:30:19 -0700 | [diff] [blame] | 769 | /** |
| 770 | * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value |
| 771 | * @name: a pointer to a string containing the name of the file to create. |
| 772 | * @mode: the permission that the file should have |
| 773 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 774 | * directory dentry if set. If this parameter is %NULL, then the |
| 775 | * file will be created in the root of the debugfs filesystem. |
| 776 | * @value: a pointer to the variable that the file should read to and write |
| 777 | * from. |
| 778 | */ |
Greg Kroah-Hartman | f5cb0a7 | 2019-10-11 15:29:30 +0200 | [diff] [blame] | 779 | void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent, |
| 780 | u32 *value) |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 781 | { |
Greg Kroah-Hartman | f5cb0a7 | 2019-10-11 15:29:30 +0200 | [diff] [blame] | 782 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32, |
Stephen Boyd | b97f679 | 2015-10-12 18:09:09 -0700 | [diff] [blame] | 783 | &fops_x32_ro, &fops_x32_wo); |
Robin Getz | 2ebefc5 | 2007-08-02 18:23:50 -0400 | [diff] [blame] | 784 | } |
| 785 | EXPORT_SYMBOL_GPL(debugfs_create_x32); |
| 786 | |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 787 | /** |
| 788 | * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value |
| 789 | * @name: a pointer to a string containing the name of the file to create. |
| 790 | * @mode: the permission that the file should have |
| 791 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 792 | * directory dentry if set. If this parameter is %NULL, then the |
| 793 | * file will be created in the root of the debugfs filesystem. |
| 794 | * @value: a pointer to the variable that the file should read to and write |
| 795 | * from. |
| 796 | */ |
Greg Kroah-Hartman | 0864c40 | 2019-10-11 15:29:31 +0200 | [diff] [blame] | 797 | void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent, |
| 798 | u64 *value) |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 799 | { |
Greg Kroah-Hartman | 0864c40 | 2019-10-11 15:29:31 +0200 | [diff] [blame] | 800 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64, |
Stephen Boyd | 82b7d4f | 2015-10-12 18:09:10 -0700 | [diff] [blame] | 801 | &fops_x64_ro, &fops_x64_wo); |
Huang Ying | 15b0bea | 2010-05-18 14:35:23 +0800 | [diff] [blame] | 802 | } |
| 803 | EXPORT_SYMBOL_GPL(debugfs_create_x64); |
| 804 | |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 805 | |
| 806 | static int debugfs_size_t_set(void *data, u64 val) |
| 807 | { |
| 808 | *(size_t *)data = val; |
| 809 | return 0; |
| 810 | } |
| 811 | static int debugfs_size_t_get(void *data, u64 *val) |
| 812 | { |
| 813 | *val = *(size_t *)data; |
| 814 | return 0; |
| 815 | } |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 816 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set, |
| 817 | "%llu\n"); /* %llu and %zu are more or less the same */ |
| 818 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n"); |
| 819 | DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n"); |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 820 | |
| 821 | /** |
| 822 | * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value |
| 823 | * @name: a pointer to a string containing the name of the file to create. |
| 824 | * @mode: the permission that the file should have |
| 825 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 826 | * directory dentry if set. If this parameter is %NULL, then the |
| 827 | * file will be created in the root of the debugfs filesystem. |
| 828 | * @value: a pointer to the variable that the file should read to and write |
| 829 | * from. |
| 830 | */ |
Greg Kroah-Hartman | 8e58026 | 2019-10-11 15:29:27 +0200 | [diff] [blame] | 831 | void debugfs_create_size_t(const char *name, umode_t mode, |
| 832 | struct dentry *parent, size_t *value) |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 833 | { |
Greg Kroah-Hartman | 8e58026 | 2019-10-11 15:29:27 +0200 | [diff] [blame] | 834 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t, |
| 835 | &fops_size_t_ro, &fops_size_t_wo); |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 836 | } |
| 837 | EXPORT_SYMBOL_GPL(debugfs_create_size_t); |
| 838 | |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 839 | static int debugfs_atomic_t_set(void *data, u64 val) |
| 840 | { |
| 841 | atomic_set((atomic_t *)data, val); |
| 842 | return 0; |
| 843 | } |
| 844 | static int debugfs_atomic_t_get(void *data, u64 *val) |
| 845 | { |
| 846 | *val = atomic_read((atomic_t *)data); |
| 847 | return 0; |
| 848 | } |
Akinobu Mita | d472cf7 | 2022-09-20 02:24:18 +0900 | [diff] [blame] | 849 | DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get, |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 850 | debugfs_atomic_t_set, "%lld\n"); |
Akinobu Mita | d472cf7 | 2022-09-20 02:24:18 +0900 | [diff] [blame] | 851 | DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL, |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 852 | "%lld\n"); |
Akinobu Mita | d472cf7 | 2022-09-20 02:24:18 +0900 | [diff] [blame] | 853 | DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set, |
Nicolai Stange | 4909f16 | 2016-03-22 14:11:17 +0100 | [diff] [blame] | 854 | "%lld\n"); |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 855 | |
| 856 | /** |
| 857 | * debugfs_create_atomic_t - create a debugfs file that is used to read and |
| 858 | * write an atomic_t value |
| 859 | * @name: a pointer to a string containing the name of the file to create. |
| 860 | * @mode: the permission that the file should have |
| 861 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 862 | * directory dentry if set. If this parameter is %NULL, then the |
| 863 | * file will be created in the root of the debugfs filesystem. |
| 864 | * @value: a pointer to the variable that the file should read to and write |
| 865 | * from. |
| 866 | */ |
Greg Kroah-Hartman | 9927c6f | 2019-10-16 06:03:32 -0700 | [diff] [blame] | 867 | void debugfs_create_atomic_t(const char *name, umode_t mode, |
| 868 | struct dentry *parent, atomic_t *value) |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 869 | { |
Greg Kroah-Hartman | 9927c6f | 2019-10-16 06:03:32 -0700 | [diff] [blame] | 870 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t, |
| 871 | &fops_atomic_t_ro, &fops_atomic_t_wo); |
Seth Jennings | 3a76e5e | 2013-06-03 15:33:02 -0500 | [diff] [blame] | 872 | } |
| 873 | EXPORT_SYMBOL_GPL(debugfs_create_atomic_t); |
Inaky Perez-Gonzalez | 5e07878 | 2008-12-20 16:57:39 -0800 | [diff] [blame] | 874 | |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 875 | ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf, |
| 876 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
Rasmus Villemoes | c8a9c28 | 2021-03-26 16:14:11 +0100 | [diff] [blame] | 878 | char buf[2]; |
Nicolai Stange | 4d45f79 | 2016-03-22 14:11:18 +0100 | [diff] [blame] | 879 | bool val; |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 880 | int r; |
| 881 | struct dentry *dentry = F_DENTRY(file); |
Rahul Bedarkar | 88e412e | 2014-06-06 23:12:04 +0530 | [diff] [blame] | 882 | |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 883 | r = debugfs_file_get(dentry); |
| 884 | if (unlikely(r)) |
Nicolai Stange | 4d45f79 | 2016-03-22 14:11:18 +0100 | [diff] [blame] | 885 | return r; |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 886 | val = *(bool *)file->private_data; |
| 887 | debugfs_file_put(dentry); |
Nicolai Stange | 4d45f79 | 2016-03-22 14:11:18 +0100 | [diff] [blame] | 888 | |
| 889 | if (val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | buf[0] = 'Y'; |
| 891 | else |
| 892 | buf[0] = 'N'; |
| 893 | buf[1] = '\n'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | return simple_read_from_buffer(user_buf, count, ppos, buf, 2); |
| 895 | } |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 896 | EXPORT_SYMBOL_GPL(debugfs_read_file_bool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 898 | ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf, |
| 899 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
Jonathan Cameron | 8705b48 | 2011-04-19 12:43:46 +0100 | [diff] [blame] | 901 | bool bv; |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 902 | int r; |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 903 | bool *val = file->private_data; |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 904 | struct dentry *dentry = F_DENTRY(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Andy Shevchenko | 964f836 | 2018-05-03 19:17:52 +0300 | [diff] [blame] | 906 | r = kstrtobool_from_user(user_buf, count, &bv); |
| 907 | if (!r) { |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 908 | r = debugfs_file_get(dentry); |
| 909 | if (unlikely(r)) |
Nicolai Stange | 4d45f79 | 2016-03-22 14:11:18 +0100 | [diff] [blame] | 910 | return r; |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 911 | *val = bv; |
| 912 | debugfs_file_put(dentry); |
Nicolai Stange | 4d45f79 | 2016-03-22 14:11:18 +0100 | [diff] [blame] | 913 | } |
Jonathan Cameron | 8705b48 | 2011-04-19 12:43:46 +0100 | [diff] [blame] | 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | return count; |
| 916 | } |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 917 | EXPORT_SYMBOL_GPL(debugfs_write_file_bool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 919 | static const struct file_operations fops_bool = { |
Richard Fitzgerald | 0642ef6 | 2015-06-23 14:32:54 +0100 | [diff] [blame] | 920 | .read = debugfs_read_file_bool, |
| 921 | .write = debugfs_write_file_bool, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 922 | .open = simple_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 923 | .llseek = default_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | }; |
| 925 | |
Stephen Boyd | 6713e8f | 2015-10-12 18:09:12 -0700 | [diff] [blame] | 926 | static const struct file_operations fops_bool_ro = { |
| 927 | .read = debugfs_read_file_bool, |
| 928 | .open = simple_open, |
| 929 | .llseek = default_llseek, |
| 930 | }; |
| 931 | |
| 932 | static const struct file_operations fops_bool_wo = { |
| 933 | .write = debugfs_write_file_bool, |
| 934 | .open = simple_open, |
| 935 | .llseek = default_llseek, |
| 936 | }; |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /** |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 939 | * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | * @name: a pointer to a string containing the name of the file to create. |
| 941 | * @mode: the permission that the file should have |
| 942 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 943 | * directory dentry if set. If this parameter is %NULL, then the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | * file will be created in the root of the debugfs filesystem. |
| 945 | * @value: a pointer to the variable that the file should read to and write |
| 946 | * from. |
| 947 | * |
| 948 | * This function creates a file in debugfs with the given name that |
| 949 | * contains the value of the variable @value. If the @mode variable is so |
| 950 | * set, it can be read from, and written to. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | */ |
Greg Kroah-Hartman | 393b063 | 2021-05-21 20:45:19 +0200 | [diff] [blame] | 952 | void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent, |
| 953 | bool *value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | { |
Greg Kroah-Hartman | 393b063 | 2021-05-21 20:45:19 +0200 | [diff] [blame] | 955 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool, |
Stephen Boyd | 6713e8f | 2015-10-12 18:09:12 -0700 | [diff] [blame] | 956 | &fops_bool_ro, &fops_bool_wo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | } |
| 958 | EXPORT_SYMBOL_GPL(debugfs_create_bool); |
| 959 | |
Peter Zijlstra | 9af0440 | 2021-03-25 10:53:55 +0100 | [diff] [blame] | 960 | ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf, |
| 961 | size_t count, loff_t *ppos) |
| 962 | { |
| 963 | struct dentry *dentry = F_DENTRY(file); |
| 964 | char *str, *copy = NULL; |
| 965 | int copy_len, len; |
| 966 | ssize_t ret; |
| 967 | |
| 968 | ret = debugfs_file_get(dentry); |
| 969 | if (unlikely(ret)) |
| 970 | return ret; |
| 971 | |
| 972 | str = *(char **)file->private_data; |
| 973 | len = strlen(str) + 1; |
| 974 | copy = kmalloc(len, GFP_KERNEL); |
| 975 | if (!copy) { |
| 976 | debugfs_file_put(dentry); |
| 977 | return -ENOMEM; |
| 978 | } |
| 979 | |
| 980 | copy_len = strscpy(copy, str, len); |
| 981 | debugfs_file_put(dentry); |
| 982 | if (copy_len < 0) { |
| 983 | kfree(copy); |
| 984 | return copy_len; |
| 985 | } |
| 986 | |
| 987 | copy[copy_len] = '\n'; |
| 988 | |
Dietmar Eggemann | f501b6a | 2021-05-27 11:11:05 +0200 | [diff] [blame] | 989 | ret = simple_read_from_buffer(user_buf, count, ppos, copy, len); |
Peter Zijlstra | 9af0440 | 2021-03-25 10:53:55 +0100 | [diff] [blame] | 990 | kfree(copy); |
| 991 | |
| 992 | return ret; |
| 993 | } |
Cristian Marussi | d60b59b | 2023-01-18 12:14:18 +0000 | [diff] [blame] | 994 | EXPORT_SYMBOL_GPL(debugfs_create_str); |
Peter Zijlstra | 9af0440 | 2021-03-25 10:53:55 +0100 | [diff] [blame] | 995 | |
| 996 | static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf, |
| 997 | size_t count, loff_t *ppos) |
| 998 | { |
Mike Tipton | 86b5488 | 2023-08-07 07:29:12 -0700 | [diff] [blame] | 999 | struct dentry *dentry = F_DENTRY(file); |
| 1000 | char *old, *new = NULL; |
| 1001 | int pos = *ppos; |
| 1002 | int r; |
| 1003 | |
| 1004 | r = debugfs_file_get(dentry); |
| 1005 | if (unlikely(r)) |
| 1006 | return r; |
| 1007 | |
| 1008 | old = *(char **)file->private_data; |
| 1009 | |
| 1010 | /* only allow strict concatenation */ |
| 1011 | r = -EINVAL; |
| 1012 | if (pos && pos != strlen(old)) |
| 1013 | goto error; |
| 1014 | |
| 1015 | r = -E2BIG; |
| 1016 | if (pos + count + 1 > PAGE_SIZE) |
| 1017 | goto error; |
| 1018 | |
| 1019 | r = -ENOMEM; |
| 1020 | new = kmalloc(pos + count + 1, GFP_KERNEL); |
| 1021 | if (!new) |
| 1022 | goto error; |
| 1023 | |
| 1024 | if (pos) |
| 1025 | memcpy(new, old, pos); |
| 1026 | |
| 1027 | r = -EFAULT; |
| 1028 | if (copy_from_user(new + pos, user_buf, count)) |
| 1029 | goto error; |
| 1030 | |
| 1031 | new[pos + count] = '\0'; |
| 1032 | strim(new); |
| 1033 | |
Mike Tipton | 7360a48 | 2023-09-22 06:45:12 -0700 | [diff] [blame] | 1034 | rcu_assign_pointer(*(char __rcu **)file->private_data, new); |
Mike Tipton | 86b5488 | 2023-08-07 07:29:12 -0700 | [diff] [blame] | 1035 | synchronize_rcu(); |
| 1036 | kfree(old); |
| 1037 | |
| 1038 | debugfs_file_put(dentry); |
| 1039 | return count; |
| 1040 | |
| 1041 | error: |
| 1042 | kfree(new); |
| 1043 | debugfs_file_put(dentry); |
| 1044 | return r; |
Peter Zijlstra | 9af0440 | 2021-03-25 10:53:55 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | static const struct file_operations fops_str = { |
| 1048 | .read = debugfs_read_file_str, |
| 1049 | .write = debugfs_write_file_str, |
| 1050 | .open = simple_open, |
| 1051 | .llseek = default_llseek, |
| 1052 | }; |
| 1053 | |
| 1054 | static const struct file_operations fops_str_ro = { |
| 1055 | .read = debugfs_read_file_str, |
| 1056 | .open = simple_open, |
| 1057 | .llseek = default_llseek, |
| 1058 | }; |
| 1059 | |
| 1060 | static const struct file_operations fops_str_wo = { |
| 1061 | .write = debugfs_write_file_str, |
| 1062 | .open = simple_open, |
| 1063 | .llseek = default_llseek, |
| 1064 | }; |
| 1065 | |
| 1066 | /** |
| 1067 | * debugfs_create_str - create a debugfs file that is used to read and write a string value |
| 1068 | * @name: a pointer to a string containing the name of the file to create. |
| 1069 | * @mode: the permission that the file should have |
| 1070 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 1071 | * directory dentry if set. If this parameter is %NULL, then the |
| 1072 | * file will be created in the root of the debugfs filesystem. |
| 1073 | * @value: a pointer to the variable that the file should read to and write |
| 1074 | * from. |
| 1075 | * |
| 1076 | * This function creates a file in debugfs with the given name that |
| 1077 | * contains the value of the variable @value. If the @mode variable is so |
| 1078 | * set, it can be read from, and written to. |
Peter Zijlstra | 9af0440 | 2021-03-25 10:53:55 +0100 | [diff] [blame] | 1079 | */ |
| 1080 | void debugfs_create_str(const char *name, umode_t mode, |
| 1081 | struct dentry *parent, char **value) |
| 1082 | { |
| 1083 | debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str, |
| 1084 | &fops_str_ro, &fops_str_wo); |
| 1085 | } |
| 1086 | |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1087 | static ssize_t read_file_blob(struct file *file, char __user *user_buf, |
| 1088 | size_t count, loff_t *ppos) |
| 1089 | { |
| 1090 | struct debugfs_blob_wrapper *blob = file->private_data; |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 1091 | struct dentry *dentry = F_DENTRY(file); |
Nicolai Stange | 83b711cb | 2016-03-22 14:11:19 +0100 | [diff] [blame] | 1092 | ssize_t r; |
Nicolai Stange | 83b711cb | 2016-03-22 14:11:19 +0100 | [diff] [blame] | 1093 | |
Nicolai Stange | 69d29f9 | 2017-10-31 00:15:50 +0100 | [diff] [blame] | 1094 | r = debugfs_file_get(dentry); |
| 1095 | if (unlikely(r)) |
| 1096 | return r; |
| 1097 | r = simple_read_from_buffer(user_buf, count, ppos, blob->data, |
| 1098 | blob->size); |
| 1099 | debugfs_file_put(dentry); |
Nicolai Stange | 83b711cb | 2016-03-22 14:11:19 +0100 | [diff] [blame] | 1100 | return r; |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1101 | } |
| 1102 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1103 | static const struct file_operations fops_blob = { |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1104 | .read = read_file_blob, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 1105 | .open = simple_open, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1106 | .llseek = default_llseek, |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1107 | }; |
| 1108 | |
| 1109 | /** |
Jonathan Corbet | 400ced6 | 2009-05-25 10:15:27 -0600 | [diff] [blame] | 1110 | * debugfs_create_blob - create a debugfs file that is used to read a binary blob |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1111 | * @name: a pointer to a string containing the name of the file to create. |
Wolfram Sang | d616f56 | 2021-05-04 15:13:49 +0200 | [diff] [blame] | 1112 | * @mode: the read permission that the file should have (other permissions are |
| 1113 | * masked out) |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1114 | * @parent: a pointer to the parent dentry for this file. This should be a |
Randy Dunlap | 6468b3a | 2006-07-20 08:16:42 -0700 | [diff] [blame] | 1115 | * directory dentry if set. If this parameter is %NULL, then the |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1116 | * file will be created in the root of the debugfs filesystem. |
| 1117 | * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer |
| 1118 | * to the blob data and the size of the data. |
| 1119 | * |
| 1120 | * This function creates a file in debugfs with the given name that exports |
| 1121 | * @blob->data as a binary blob. If the @mode variable is so set it can be |
| 1122 | * read from. Writing is not supported. |
| 1123 | * |
| 1124 | * This function will return a pointer to a dentry if it succeeds. This |
| 1125 | * pointer must be passed to the debugfs_remove() function when the file is |
| 1126 | * to be removed (no automatic cleanup happens if your module is unloaded, |
Daniel W. S. Almeida | adc92dd | 2019-12-26 22:00:33 -0300 | [diff] [blame] | 1127 | * you are responsible here.) If an error occurs, ERR_PTR(-ERROR) will be |
Ronald Tschalär | 9abb249 | 2019-04-15 01:25:05 -0700 | [diff] [blame] | 1128 | * returned. |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1129 | * |
Daniel W. S. Almeida | adc92dd | 2019-12-26 22:00:33 -0300 | [diff] [blame] | 1130 | * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will |
Ronald Tschalär | 9abb249 | 2019-04-15 01:25:05 -0700 | [diff] [blame] | 1131 | * be returned. |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1132 | */ |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 1133 | struct dentry *debugfs_create_blob(const char *name, umode_t mode, |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1134 | struct dentry *parent, |
| 1135 | struct debugfs_blob_wrapper *blob) |
| 1136 | { |
Wolfram Sang | d616f56 | 2021-05-04 15:13:49 +0200 | [diff] [blame] | 1137 | return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob); |
Michael Ellerman | dd308bc | 2006-03-07 21:41:59 +1100 | [diff] [blame] | 1138 | } |
| 1139 | EXPORT_SYMBOL_GPL(debugfs_create_blob); |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1140 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1141 | static size_t u32_format_array(char *buf, size_t bufsize, |
| 1142 | u32 *array, int array_size) |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1143 | { |
| 1144 | size_t ret = 0; |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1145 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1146 | while (--array_size >= 0) { |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1147 | size_t len; |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1148 | char term = array_size ? ' ' : '\n'; |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1149 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1150 | len = snprintf(buf, bufsize, "%u%c", *array++, term); |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1151 | ret += len; |
| 1152 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1153 | buf += len; |
| 1154 | bufsize -= len; |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1155 | } |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1156 | return ret; |
| 1157 | } |
| 1158 | |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 1159 | static int u32_array_open(struct inode *inode, struct file *file) |
| 1160 | { |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 1161 | struct debugfs_u32_array *data = inode->i_private; |
| 1162 | int size, elements = data->n_elements; |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1163 | char *buf; |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 1164 | |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1165 | /* |
| 1166 | * Max size: |
| 1167 | * - 10 digits + ' '/'\n' = 11 bytes per number |
| 1168 | * - terminating NUL character |
| 1169 | */ |
| 1170 | size = elements*11; |
| 1171 | buf = kmalloc(size+1, GFP_KERNEL); |
| 1172 | if (!buf) |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 1173 | return -ENOMEM; |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1174 | buf[size] = 0; |
| 1175 | |
| 1176 | file->private_data = buf; |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 1177 | u32_format_array(buf, size, data->array, data->n_elements); |
Linus Torvalds | e05e279 | 2012-09-21 11:48:05 -0700 | [diff] [blame] | 1178 | |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 1179 | return nonseekable_open(inode, file); |
| 1180 | } |
| 1181 | |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1182 | static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len, |
| 1183 | loff_t *ppos) |
| 1184 | { |
David Rientjes | 3604885 | 2012-09-21 02:16:29 -0700 | [diff] [blame] | 1185 | size_t size = strlen(file->private_data); |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1186 | |
| 1187 | return simple_read_from_buffer(buf, len, ppos, |
| 1188 | file->private_data, size); |
| 1189 | } |
| 1190 | |
| 1191 | static int u32_array_release(struct inode *inode, struct file *file) |
| 1192 | { |
| 1193 | kfree(file->private_data); |
| 1194 | |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | static const struct file_operations u32_array_fops = { |
| 1199 | .owner = THIS_MODULE, |
| 1200 | .open = u32_array_open, |
| 1201 | .release = u32_array_release, |
| 1202 | .read = u32_array_read, |
| 1203 | .llseek = no_llseek, |
| 1204 | }; |
| 1205 | |
| 1206 | /** |
| 1207 | * debugfs_create_u32_array - create a debugfs file that is used to read u32 |
| 1208 | * array. |
| 1209 | * @name: a pointer to a string containing the name of the file to create. |
| 1210 | * @mode: the permission that the file should have. |
| 1211 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 1212 | * directory dentry if set. If this parameter is %NULL, then the |
| 1213 | * file will be created in the root of the debugfs filesystem. |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 1214 | * @array: wrapper struct containing data pointer and size of the array. |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1215 | * |
| 1216 | * This function creates a file in debugfs with the given name that exports |
| 1217 | * @array as data. If the @mode variable is so set it can be read from. |
| 1218 | * Writing is not supported. Seek within the file is also not supported. |
| 1219 | * Once array is created its size can not be changed. |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1220 | */ |
Greg Kroah-Hartman | c9c2c27 | 2019-04-16 15:46:55 +0200 | [diff] [blame] | 1221 | void debugfs_create_u32_array(const char *name, umode_t mode, |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 1222 | struct dentry *parent, |
| 1223 | struct debugfs_u32_array *array) |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1224 | { |
Jakub Kicinski | a2b992c | 2020-07-09 17:42:44 -0700 | [diff] [blame] | 1225 | debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops); |
Srivatsa Vaddagiri | 9fe2a70 | 2012-03-23 13:36:28 +0530 | [diff] [blame] | 1226 | } |
| 1227 | EXPORT_SYMBOL_GPL(debugfs_create_u32_array); |
| 1228 | |
Heiko Carstens | 3b85e4a | 2011-12-27 15:08:28 +0100 | [diff] [blame] | 1229 | #ifdef CONFIG_HAS_IOMEM |
| 1230 | |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1231 | /* |
| 1232 | * The regset32 stuff is used to print 32-bit registers using the |
| 1233 | * seq_file utilities. We offer printing a register set in an already-opened |
| 1234 | * sequential file or create a debugfs file that only prints a regset32. |
| 1235 | */ |
| 1236 | |
| 1237 | /** |
| 1238 | * debugfs_print_regs32 - use seq_print to describe a set of registers |
| 1239 | * @s: the seq_file structure being used to generate output |
| 1240 | * @regs: an array if struct debugfs_reg32 structures |
Randy Dunlap | b5763ac | 2012-01-21 11:02:42 -0800 | [diff] [blame] | 1241 | * @nregs: the length of the above array |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1242 | * @base: the base address to be used in reading the registers |
| 1243 | * @prefix: a string to be prefixed to every output line |
| 1244 | * |
| 1245 | * This function outputs a text block describing the current values of |
| 1246 | * some 32-bit hardware registers. It is meant to be used within debugfs |
| 1247 | * files based on seq_file that need to show registers, intermixed with other |
| 1248 | * information. The prefix argument may be used to specify a leading string, |
| 1249 | * because some peripherals have several blocks of identical registers, |
| 1250 | * for example configuration of dma channels |
| 1251 | */ |
Joe Perches | 9761536 | 2014-09-29 16:08:26 -0700 | [diff] [blame] | 1252 | void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs, |
| 1253 | int nregs, void __iomem *base, char *prefix) |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1254 | { |
Joe Perches | 9761536 | 2014-09-29 16:08:26 -0700 | [diff] [blame] | 1255 | int i; |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1256 | |
| 1257 | for (i = 0; i < nregs; i++, regs++) { |
| 1258 | if (prefix) |
Joe Perches | 9761536 | 2014-09-29 16:08:26 -0700 | [diff] [blame] | 1259 | seq_printf(s, "%s", prefix); |
| 1260 | seq_printf(s, "%s = 0x%08x\n", regs->name, |
| 1261 | readl(base + regs->offset)); |
| 1262 | if (seq_has_overflowed(s)) |
| 1263 | break; |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1264 | } |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1265 | } |
| 1266 | EXPORT_SYMBOL_GPL(debugfs_print_regs32); |
| 1267 | |
ChenXiaoSong | 19029f3 | 2022-09-23 18:25:54 +0800 | [diff] [blame] | 1268 | static int debugfs_regset32_show(struct seq_file *s, void *data) |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1269 | { |
| 1270 | struct debugfs_regset32 *regset = s->private; |
| 1271 | |
Geert Uytterhoeven | 30332ee | 2020-02-11 19:18:55 +0100 | [diff] [blame] | 1272 | if (regset->dev) |
| 1273 | pm_runtime_get_sync(regset->dev); |
| 1274 | |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1275 | debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, ""); |
Geert Uytterhoeven | 30332ee | 2020-02-11 19:18:55 +0100 | [diff] [blame] | 1276 | |
| 1277 | if (regset->dev) |
| 1278 | pm_runtime_put(regset->dev); |
| 1279 | |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1280 | return 0; |
| 1281 | } |
| 1282 | |
ChenXiaoSong | 19029f3 | 2022-09-23 18:25:54 +0800 | [diff] [blame] | 1283 | DEFINE_SHOW_ATTRIBUTE(debugfs_regset32); |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1284 | |
| 1285 | /** |
| 1286 | * debugfs_create_regset32 - create a debugfs file that returns register values |
| 1287 | * @name: a pointer to a string containing the name of the file to create. |
| 1288 | * @mode: the permission that the file should have |
| 1289 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 1290 | * directory dentry if set. If this parameter is %NULL, then the |
| 1291 | * file will be created in the root of the debugfs filesystem. |
| 1292 | * @regset: a pointer to a struct debugfs_regset32, which contains a pointer |
| 1293 | * to an array of register definitions, the array size and the base |
| 1294 | * address where the register bank is to be found. |
| 1295 | * |
| 1296 | * This function creates a file in debugfs with the given name that reports |
| 1297 | * the names and values of a set of 32-bit registers. If the @mode variable |
| 1298 | * is so set it can be read from. Writing is not supported. |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1299 | */ |
Greg Kroah-Hartman | ae91c92 | 2019-11-22 11:44:53 +0100 | [diff] [blame] | 1300 | void debugfs_create_regset32(const char *name, umode_t mode, |
| 1301 | struct dentry *parent, |
| 1302 | struct debugfs_regset32 *regset) |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1303 | { |
ChenXiaoSong | 19029f3 | 2022-09-23 18:25:54 +0800 | [diff] [blame] | 1304 | debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops); |
Alessandro Rubini | 1a087c6 | 2011-11-18 14:50:21 +0100 | [diff] [blame] | 1305 | } |
| 1306 | EXPORT_SYMBOL_GPL(debugfs_create_regset32); |
Heiko Carstens | 3b85e4a | 2011-12-27 15:08:28 +0100 | [diff] [blame] | 1307 | |
| 1308 | #endif /* CONFIG_HAS_IOMEM */ |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 1309 | |
| 1310 | struct debugfs_devm_entry { |
| 1311 | int (*read)(struct seq_file *seq, void *data); |
| 1312 | struct device *dev; |
| 1313 | }; |
| 1314 | |
| 1315 | static int debugfs_devm_entry_open(struct inode *inode, struct file *f) |
| 1316 | { |
| 1317 | struct debugfs_devm_entry *entry = inode->i_private; |
| 1318 | |
| 1319 | return single_open(f, entry->read, entry->dev); |
| 1320 | } |
| 1321 | |
| 1322 | static const struct file_operations debugfs_devm_entry_ops = { |
| 1323 | .owner = THIS_MODULE, |
| 1324 | .open = debugfs_devm_entry_open, |
| 1325 | .release = single_release, |
| 1326 | .read = seq_read, |
| 1327 | .llseek = seq_lseek |
| 1328 | }; |
| 1329 | |
| 1330 | /** |
| 1331 | * debugfs_create_devm_seqfile - create a debugfs file that is bound to device. |
| 1332 | * |
| 1333 | * @dev: device related to this debugfs file. |
| 1334 | * @name: name of the debugfs file. |
| 1335 | * @parent: a pointer to the parent dentry for this file. This should be a |
| 1336 | * directory dentry if set. If this parameter is %NULL, then the |
| 1337 | * file will be created in the root of the debugfs filesystem. |
| 1338 | * @read_fn: function pointer called to print the seq_file content. |
| 1339 | */ |
Greg Kroah-Hartman | 0d519cb | 2020-10-23 15:10:37 +0200 | [diff] [blame] | 1340 | void debugfs_create_devm_seqfile(struct device *dev, const char *name, |
| 1341 | struct dentry *parent, |
| 1342 | int (*read_fn)(struct seq_file *s, void *data)) |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 1343 | { |
| 1344 | struct debugfs_devm_entry *entry; |
| 1345 | |
| 1346 | if (IS_ERR(parent)) |
Greg Kroah-Hartman | 0d519cb | 2020-10-23 15:10:37 +0200 | [diff] [blame] | 1347 | return; |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 1348 | |
| 1349 | entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL); |
| 1350 | if (!entry) |
Greg Kroah-Hartman | 0d519cb | 2020-10-23 15:10:37 +0200 | [diff] [blame] | 1351 | return; |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 1352 | |
| 1353 | entry->read = read_fn; |
| 1354 | entry->dev = dev; |
| 1355 | |
Greg Kroah-Hartman | 0d519cb | 2020-10-23 15:10:37 +0200 | [diff] [blame] | 1356 | debugfs_create_file(name, S_IRUGO, parent, entry, |
| 1357 | &debugfs_devm_entry_ops); |
Arend van Spriel | 98210b7 | 2014-11-09 11:31:58 +0100 | [diff] [blame] | 1358 | } |
| 1359 | EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile); |