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