1 // SPDX-License-Identifier: GPL-2.0
 
   3  *  file.c - part of debugfs, a tiny little debug file system
 
   5  *  Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
 
   6  *  Copyright (C) 2004 IBM Inc.
 
   8  *  debugfs is for people to use instead of /proc or /sys.
 
   9  *  See Documentation/filesystems/ for more details.
 
  12 #include <linux/module.h>
 
  14 #include <linux/seq_file.h>
 
  15 #include <linux/pagemap.h>
 
  16 #include <linux/debugfs.h>
 
  18 #include <linux/slab.h>
 
  19 #include <linux/atomic.h>
 
  20 #include <linux/device.h>
 
  21 #include <linux/pm_runtime.h>
 
  22 #include <linux/poll.h>
 
  23 #include <linux/security.h>
 
  27 struct poll_table_struct;
 
  29 static ssize_t default_read_file(struct file *file, char __user *buf,
 
  30                                  size_t count, loff_t *ppos)
 
  35 static ssize_t default_write_file(struct file *file, const char __user *buf,
 
  36                                    size_t count, loff_t *ppos)
 
  41 const struct file_operations debugfs_noop_file_operations = {
 
  42         .read =         default_read_file,
 
  43         .write =        default_write_file,
 
  45         .llseek =       noop_llseek,
 
  48 #define F_DENTRY(filp) ((filp)->f_path.dentry)
 
  50 const struct file_operations *debugfs_real_fops(const struct file *filp)
 
  52         struct debugfs_fsdata *fsd = F_DENTRY(filp)->d_fsdata;
 
  54         if ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT) {
 
  56                  * Urgh, we've been called w/o a protecting
 
  63         return fsd->real_fops;
 
  65 EXPORT_SYMBOL_GPL(debugfs_real_fops);
 
  68  * debugfs_file_get - mark the beginning of file data access
 
  69  * @dentry: the dentry object whose data is being accessed.
 
  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.
 
  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.
 
  82 int debugfs_file_get(struct dentry *dentry)
 
  84         struct debugfs_fsdata *fsd;
 
  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
 
  92         if (WARN_ON(!d_is_reg(dentry)))
 
  95         d_fsd = READ_ONCE(dentry->d_fsdata);
 
  96         if (!((unsigned long)d_fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)) {
 
  99                 fsd = kmalloc(sizeof(*fsd), GFP_KERNEL);
 
 103                 fsd->real_fops = (void *)((unsigned long)d_fsd &
 
 104                                         ~DEBUGFS_FSDATA_IS_REAL_FOPS_BIT);
 
 105                 refcount_set(&fsd->active_users, 1);
 
 106                 init_completion(&fsd->active_users_drained);
 
 107                 if (cmpxchg(&dentry->d_fsdata, d_fsd, fsd) != d_fsd) {
 
 109                         fsd = READ_ONCE(dentry->d_fsdata);
 
 111                 INIT_LIST_HEAD(&fsd->cancellations);
 
 112                 mutex_init(&fsd->cancellations_mtx);
 
 116          * In case of a successful cmpxchg() above, this check is
 
 117          * strictly necessary and must follow it, see the comment in
 
 118          * __debugfs_remove_file().
 
 119          * OTOH, if the cmpxchg() hasn't been executed or wasn't
 
 120          * successful, this serves the purpose of not starving
 
 123         if (d_unlinked(dentry))
 
 126         if (!refcount_inc_not_zero(&fsd->active_users))
 
 131 EXPORT_SYMBOL_GPL(debugfs_file_get);
 
 134  * debugfs_file_put - mark the end of file data access
 
 135  * @dentry: the dentry object formerly passed to
 
 136  *          debugfs_file_get().
 
 138  * Allow any ongoing concurrent call into debugfs_remove() or
 
 139  * debugfs_remove_recursive() blocked by a former call to
 
 140  * debugfs_file_get() to proceed and return to its caller.
 
 142 void debugfs_file_put(struct dentry *dentry)
 
 144         struct debugfs_fsdata *fsd = READ_ONCE(dentry->d_fsdata);
 
 146         if (refcount_dec_and_test(&fsd->active_users))
 
 147                 complete(&fsd->active_users_drained);
 
 149 EXPORT_SYMBOL_GPL(debugfs_file_put);
 
 152  * debugfs_enter_cancellation - enter a debugfs cancellation
 
 153  * @file: the file being accessed
 
 154  * @cancellation: the cancellation object, the cancel callback
 
 155  *      inside of it must be initialized
 
 157  * When a debugfs file is removed it needs to wait for all active
 
 158  * operations to complete. However, the operation itself may need
 
 159  * to wait for hardware or completion of some asynchronous process
 
 160  * or similar. As such, it may need to be cancelled to avoid long
 
 161  * waits or even deadlocks.
 
 163  * This function can be used inside a debugfs handler that may
 
 164  * need to be cancelled. As soon as this function is called, the
 
 165  * cancellation's 'cancel' callback may be called, at which point
 
 166  * the caller should proceed to call debugfs_leave_cancellation()
 
 167  * and leave the debugfs handler function as soon as possible.
 
 168  * Note that the 'cancel' callback is only ever called in the
 
 169  * context of some kind of debugfs_remove().
 
 171  * This function must be paired with debugfs_leave_cancellation().
 
 173 void debugfs_enter_cancellation(struct file *file,
 
 174                                 struct debugfs_cancellation *cancellation)
 
 176         struct debugfs_fsdata *fsd;
 
 177         struct dentry *dentry = F_DENTRY(file);
 
 179         INIT_LIST_HEAD(&cancellation->list);
 
 181         if (WARN_ON(!d_is_reg(dentry)))
 
 184         if (WARN_ON(!cancellation->cancel))
 
 187         fsd = READ_ONCE(dentry->d_fsdata);
 
 189                     ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
 
 192         mutex_lock(&fsd->cancellations_mtx);
 
 193         list_add(&cancellation->list, &fsd->cancellations);
 
 194         mutex_unlock(&fsd->cancellations_mtx);
 
 196         /* if we're already removing wake it up to cancel */
 
 197         if (d_unlinked(dentry))
 
 198                 complete(&fsd->active_users_drained);
 
 200 EXPORT_SYMBOL_GPL(debugfs_enter_cancellation);
 
 203  * debugfs_leave_cancellation - leave cancellation section
 
 204  * @file: the file being accessed
 
 205  * @cancellation: the cancellation previously registered with
 
 206  *      debugfs_enter_cancellation()
 
 208  * See the documentation of debugfs_enter_cancellation().
 
 210 void debugfs_leave_cancellation(struct file *file,
 
 211                                 struct debugfs_cancellation *cancellation)
 
 213         struct debugfs_fsdata *fsd;
 
 214         struct dentry *dentry = F_DENTRY(file);
 
 216         if (WARN_ON(!d_is_reg(dentry)))
 
 219         fsd = READ_ONCE(dentry->d_fsdata);
 
 221                     ((unsigned long)fsd & DEBUGFS_FSDATA_IS_REAL_FOPS_BIT)))
 
 224         mutex_lock(&fsd->cancellations_mtx);
 
 225         if (!list_empty(&cancellation->list))
 
 226                 list_del(&cancellation->list);
 
 227         mutex_unlock(&fsd->cancellations_mtx);
 
 229 EXPORT_SYMBOL_GPL(debugfs_leave_cancellation);
 
 232  * Only permit access to world-readable files when the kernel is locked down.
 
 233  * We also need to exclude any file that has ways to write or alter it as root
 
 234  * can bypass the permissions check.
 
 236 static int debugfs_locked_down(struct inode *inode,
 
 238                                const struct file_operations *real_fops)
 
 240         if ((inode->i_mode & 07777 & ~0444) == 0 &&
 
 241             !(filp->f_mode & FMODE_WRITE) &&
 
 242             !real_fops->unlocked_ioctl &&
 
 243             !real_fops->compat_ioctl &&
 
 247         if (security_locked_down(LOCKDOWN_DEBUGFS))
 
 253 static int open_proxy_open(struct inode *inode, struct file *filp)
 
 255         struct dentry *dentry = F_DENTRY(filp);
 
 256         const struct file_operations *real_fops = NULL;
 
 259         r = debugfs_file_get(dentry);
 
 261                 return r == -EIO ? -ENOENT : r;
 
 263         real_fops = debugfs_real_fops(filp);
 
 265         r = debugfs_locked_down(inode, filp, real_fops);
 
 269         if (!fops_get(real_fops)) {
 
 270 #ifdef CONFIG_MODULES
 
 271                 if (real_fops->owner &&
 
 272                     real_fops->owner->state == MODULE_STATE_GOING) {
 
 278                 /* Huh? Module did not clean up after itself at exit? */
 
 279                 WARN(1, "debugfs file owner did not clean up at exit: %pd",
 
 284         replace_fops(filp, real_fops);
 
 287                 r = real_fops->open(inode, filp);
 
 290         debugfs_file_put(dentry);
 
 294 const struct file_operations debugfs_open_proxy_file_operations = {
 
 295         .open = open_proxy_open,
 
 298 #define PROTO(args...) args
 
 299 #define ARGS(args...) args
 
 301 #define FULL_PROXY_FUNC(name, ret_type, filp, proto, args)              \
 
 302 static ret_type full_proxy_ ## name(proto)                              \
 
 304         struct dentry *dentry = F_DENTRY(filp);                 \
 
 305         const struct file_operations *real_fops;                        \
 
 308         r = debugfs_file_get(dentry);                                   \
 
 311         real_fops = debugfs_real_fops(filp);                            \
 
 312         r = real_fops->name(args);                                      \
 
 313         debugfs_file_put(dentry);                                       \
 
 317 FULL_PROXY_FUNC(llseek, loff_t, filp,
 
 318                 PROTO(struct file *filp, loff_t offset, int whence),
 
 319                 ARGS(filp, offset, whence));
 
 321 FULL_PROXY_FUNC(read, ssize_t, filp,
 
 322                 PROTO(struct file *filp, char __user *buf, size_t size,
 
 324                 ARGS(filp, buf, size, ppos));
 
 326 FULL_PROXY_FUNC(write, ssize_t, filp,
 
 327                 PROTO(struct file *filp, const char __user *buf, size_t size,
 
 329                 ARGS(filp, buf, size, ppos));
 
 331 FULL_PROXY_FUNC(unlocked_ioctl, long, filp,
 
 332                 PROTO(struct file *filp, unsigned int cmd, unsigned long arg),
 
 333                 ARGS(filp, cmd, arg));
 
 335 static __poll_t full_proxy_poll(struct file *filp,
 
 336                                 struct poll_table_struct *wait)
 
 338         struct dentry *dentry = F_DENTRY(filp);
 
 340         const struct file_operations *real_fops;
 
 342         if (debugfs_file_get(dentry))
 
 345         real_fops = debugfs_real_fops(filp);
 
 346         r = real_fops->poll(filp, wait);
 
 347         debugfs_file_put(dentry);
 
 351 static int full_proxy_release(struct inode *inode, struct file *filp)
 
 353         const struct dentry *dentry = F_DENTRY(filp);
 
 354         const struct file_operations *real_fops = debugfs_real_fops(filp);
 
 355         const struct file_operations *proxy_fops = filp->f_op;
 
 359          * We must not protect this against removal races here: the
 
 360          * original releaser should be called unconditionally in order
 
 361          * not to leak any resources. Releasers must not assume that
 
 362          * ->i_private is still being meaningful here.
 
 364         if (real_fops->release)
 
 365                 r = real_fops->release(inode, filp);
 
 367         replace_fops(filp, d_inode(dentry)->i_fop);
 
 373 static void __full_proxy_fops_init(struct file_operations *proxy_fops,
 
 374                                 const struct file_operations *real_fops)
 
 376         proxy_fops->release = full_proxy_release;
 
 377         if (real_fops->llseek)
 
 378                 proxy_fops->llseek = full_proxy_llseek;
 
 380                 proxy_fops->read = full_proxy_read;
 
 381         if (real_fops->write)
 
 382                 proxy_fops->write = full_proxy_write;
 
 384                 proxy_fops->poll = full_proxy_poll;
 
 385         if (real_fops->unlocked_ioctl)
 
 386                 proxy_fops->unlocked_ioctl = full_proxy_unlocked_ioctl;
 
 389 static int full_proxy_open(struct inode *inode, struct file *filp)
 
 391         struct dentry *dentry = F_DENTRY(filp);
 
 392         const struct file_operations *real_fops = NULL;
 
 393         struct file_operations *proxy_fops = NULL;
 
 396         r = debugfs_file_get(dentry);
 
 398                 return r == -EIO ? -ENOENT : r;
 
 400         real_fops = debugfs_real_fops(filp);
 
 402         r = debugfs_locked_down(inode, filp, real_fops);
 
 406         if (!fops_get(real_fops)) {
 
 407 #ifdef CONFIG_MODULES
 
 408                 if (real_fops->owner &&
 
 409                     real_fops->owner->state == MODULE_STATE_GOING) {
 
 415                 /* Huh? Module did not cleanup after itself at exit? */
 
 416                 WARN(1, "debugfs file owner did not clean up at exit: %pd",
 
 422         proxy_fops = kzalloc(sizeof(*proxy_fops), GFP_KERNEL);
 
 427         __full_proxy_fops_init(proxy_fops, real_fops);
 
 428         replace_fops(filp, proxy_fops);
 
 430         if (real_fops->open) {
 
 431                 r = real_fops->open(inode, filp);
 
 433                         replace_fops(filp, d_inode(dentry)->i_fop);
 
 435                 } else if (filp->f_op != proxy_fops) {
 
 436                         /* No protection against file removal anymore. */
 
 437                         WARN(1, "debugfs file owner replaced proxy fops: %pd",
 
 448         debugfs_file_put(dentry);
 
 452 const struct file_operations debugfs_full_proxy_file_operations = {
 
 453         .open = full_proxy_open,
 
 456 ssize_t debugfs_attr_read(struct file *file, char __user *buf,
 
 457                         size_t len, loff_t *ppos)
 
 459         struct dentry *dentry = F_DENTRY(file);
 
 462         ret = debugfs_file_get(dentry);
 
 465         ret = simple_attr_read(file, buf, len, ppos);
 
 466         debugfs_file_put(dentry);
 
 469 EXPORT_SYMBOL_GPL(debugfs_attr_read);
 
 471 static ssize_t debugfs_attr_write_xsigned(struct file *file, const char __user *buf,
 
 472                          size_t len, loff_t *ppos, bool is_signed)
 
 474         struct dentry *dentry = F_DENTRY(file);
 
 477         ret = debugfs_file_get(dentry);
 
 481                 ret = simple_attr_write_signed(file, buf, len, ppos);
 
 483                 ret = simple_attr_write(file, buf, len, ppos);
 
 484         debugfs_file_put(dentry);
 
 488 ssize_t debugfs_attr_write(struct file *file, const char __user *buf,
 
 489                          size_t len, loff_t *ppos)
 
 491         return debugfs_attr_write_xsigned(file, buf, len, ppos, false);
 
 493 EXPORT_SYMBOL_GPL(debugfs_attr_write);
 
 495 ssize_t debugfs_attr_write_signed(struct file *file, const char __user *buf,
 
 496                          size_t len, loff_t *ppos)
 
 498         return debugfs_attr_write_xsigned(file, buf, len, ppos, true);
 
 500 EXPORT_SYMBOL_GPL(debugfs_attr_write_signed);
 
 502 static struct dentry *debugfs_create_mode_unsafe(const char *name, umode_t mode,
 
 503                                         struct dentry *parent, void *value,
 
 504                                         const struct file_operations *fops,
 
 505                                         const struct file_operations *fops_ro,
 
 506                                         const struct file_operations *fops_wo)
 
 508         /* if there are no write bits set, make read only */
 
 509         if (!(mode & S_IWUGO))
 
 510                 return debugfs_create_file_unsafe(name, mode, parent, value,
 
 512         /* if there are no read bits set, make write only */
 
 513         if (!(mode & S_IRUGO))
 
 514                 return debugfs_create_file_unsafe(name, mode, parent, value,
 
 517         return debugfs_create_file_unsafe(name, mode, parent, value, fops);
 
 520 static int debugfs_u8_set(void *data, u64 val)
 
 525 static int debugfs_u8_get(void *data, u64 *val)
 
 530 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8, debugfs_u8_get, debugfs_u8_set, "%llu\n");
 
 531 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_ro, debugfs_u8_get, NULL, "%llu\n");
 
 532 DEFINE_DEBUGFS_ATTRIBUTE(fops_u8_wo, NULL, debugfs_u8_set, "%llu\n");
 
 535  * debugfs_create_u8 - create a debugfs file that is used to read and write an unsigned 8-bit value
 
 536  * @name: a pointer to a string containing the name of the file to create.
 
 537  * @mode: the permission that the file should have
 
 538  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 539  *          directory dentry if set.  If this parameter is %NULL, then the
 
 540  *          file will be created in the root of the debugfs filesystem.
 
 541  * @value: a pointer to the variable that the file should read to and write
 
 544  * This function creates a file in debugfs with the given name that
 
 545  * contains the value of the variable @value.  If the @mode variable is so
 
 546  * set, it can be read from, and written to.
 
 548 void debugfs_create_u8(const char *name, umode_t mode, struct dentry *parent,
 
 551         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u8,
 
 552                                    &fops_u8_ro, &fops_u8_wo);
 
 554 EXPORT_SYMBOL_GPL(debugfs_create_u8);
 
 556 static int debugfs_u16_set(void *data, u64 val)
 
 561 static int debugfs_u16_get(void *data, u64 *val)
 
 566 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16, debugfs_u16_get, debugfs_u16_set, "%llu\n");
 
 567 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_ro, debugfs_u16_get, NULL, "%llu\n");
 
 568 DEFINE_DEBUGFS_ATTRIBUTE(fops_u16_wo, NULL, debugfs_u16_set, "%llu\n");
 
 571  * debugfs_create_u16 - create a debugfs file that is used to read and write an unsigned 16-bit value
 
 572  * @name: a pointer to a string containing the name of the file to create.
 
 573  * @mode: the permission that the file should have
 
 574  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 575  *          directory dentry if set.  If this parameter is %NULL, then the
 
 576  *          file will be created in the root of the debugfs filesystem.
 
 577  * @value: a pointer to the variable that the file should read to and write
 
 580  * This function creates a file in debugfs with the given name that
 
 581  * contains the value of the variable @value.  If the @mode variable is so
 
 582  * set, it can be read from, and written to.
 
 584 void debugfs_create_u16(const char *name, umode_t mode, struct dentry *parent,
 
 587         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u16,
 
 588                                    &fops_u16_ro, &fops_u16_wo);
 
 590 EXPORT_SYMBOL_GPL(debugfs_create_u16);
 
 592 static int debugfs_u32_set(void *data, u64 val)
 
 597 static int debugfs_u32_get(void *data, u64 *val)
 
 602 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32, debugfs_u32_get, debugfs_u32_set, "%llu\n");
 
 603 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_ro, debugfs_u32_get, NULL, "%llu\n");
 
 604 DEFINE_DEBUGFS_ATTRIBUTE(fops_u32_wo, NULL, debugfs_u32_set, "%llu\n");
 
 607  * debugfs_create_u32 - create a debugfs file that is used to read and write an unsigned 32-bit value
 
 608  * @name: a pointer to a string containing the name of the file to create.
 
 609  * @mode: the permission that the file should have
 
 610  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 611  *          directory dentry if set.  If this parameter is %NULL, then the
 
 612  *          file will be created in the root of the debugfs filesystem.
 
 613  * @value: a pointer to the variable that the file should read to and write
 
 616  * This function creates a file in debugfs with the given name that
 
 617  * contains the value of the variable @value.  If the @mode variable is so
 
 618  * set, it can be read from, and written to.
 
 620 void debugfs_create_u32(const char *name, umode_t mode, struct dentry *parent,
 
 623         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u32,
 
 624                                    &fops_u32_ro, &fops_u32_wo);
 
 626 EXPORT_SYMBOL_GPL(debugfs_create_u32);
 
 628 static int debugfs_u64_set(void *data, u64 val)
 
 634 static int debugfs_u64_get(void *data, u64 *val)
 
 639 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64, debugfs_u64_get, debugfs_u64_set, "%llu\n");
 
 640 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_ro, debugfs_u64_get, NULL, "%llu\n");
 
 641 DEFINE_DEBUGFS_ATTRIBUTE(fops_u64_wo, NULL, debugfs_u64_set, "%llu\n");
 
 644  * debugfs_create_u64 - create a debugfs file that is used to read and write an unsigned 64-bit value
 
 645  * @name: a pointer to a string containing the name of the file to create.
 
 646  * @mode: the permission that the file should have
 
 647  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 648  *          directory dentry if set.  If this parameter is %NULL, then the
 
 649  *          file will be created in the root of the debugfs filesystem.
 
 650  * @value: a pointer to the variable that the file should read to and write
 
 653  * This function creates a file in debugfs with the given name that
 
 654  * contains the value of the variable @value.  If the @mode variable is so
 
 655  * set, it can be read from, and written to.
 
 657 void debugfs_create_u64(const char *name, umode_t mode, struct dentry *parent,
 
 660         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_u64,
 
 661                                    &fops_u64_ro, &fops_u64_wo);
 
 663 EXPORT_SYMBOL_GPL(debugfs_create_u64);
 
 665 static int debugfs_ulong_set(void *data, u64 val)
 
 667         *(unsigned long *)data = val;
 
 671 static int debugfs_ulong_get(void *data, u64 *val)
 
 673         *val = *(unsigned long *)data;
 
 676 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong, debugfs_ulong_get, debugfs_ulong_set,
 
 678 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_ro, debugfs_ulong_get, NULL, "%llu\n");
 
 679 DEFINE_DEBUGFS_ATTRIBUTE(fops_ulong_wo, NULL, debugfs_ulong_set, "%llu\n");
 
 682  * debugfs_create_ulong - create a debugfs file that is used to read and write
 
 683  * an unsigned long value.
 
 684  * @name: a pointer to a string containing the name of the file to create.
 
 685  * @mode: the permission that the file should have
 
 686  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 687  *          directory dentry if set.  If this parameter is %NULL, then the
 
 688  *          file will be created in the root of the debugfs filesystem.
 
 689  * @value: a pointer to the variable that the file should read to and write
 
 692  * This function creates a file in debugfs with the given name that
 
 693  * contains the value of the variable @value.  If the @mode variable is so
 
 694  * set, it can be read from, and written to.
 
 696 void debugfs_create_ulong(const char *name, umode_t mode, struct dentry *parent,
 
 697                           unsigned long *value)
 
 699         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_ulong,
 
 700                                    &fops_ulong_ro, &fops_ulong_wo);
 
 702 EXPORT_SYMBOL_GPL(debugfs_create_ulong);
 
 704 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8, debugfs_u8_get, debugfs_u8_set, "0x%02llx\n");
 
 705 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_ro, debugfs_u8_get, NULL, "0x%02llx\n");
 
 706 DEFINE_DEBUGFS_ATTRIBUTE(fops_x8_wo, NULL, debugfs_u8_set, "0x%02llx\n");
 
 708 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16, debugfs_u16_get, debugfs_u16_set,
 
 710 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_ro, debugfs_u16_get, NULL, "0x%04llx\n");
 
 711 DEFINE_DEBUGFS_ATTRIBUTE(fops_x16_wo, NULL, debugfs_u16_set, "0x%04llx\n");
 
 713 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, debugfs_u32_get, debugfs_u32_set,
 
 715 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_ro, debugfs_u32_get, NULL, "0x%08llx\n");
 
 716 DEFINE_DEBUGFS_ATTRIBUTE(fops_x32_wo, NULL, debugfs_u32_set, "0x%08llx\n");
 
 718 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64, debugfs_u64_get, debugfs_u64_set,
 
 720 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_ro, debugfs_u64_get, NULL, "0x%016llx\n");
 
 721 DEFINE_DEBUGFS_ATTRIBUTE(fops_x64_wo, NULL, debugfs_u64_set, "0x%016llx\n");
 
 724  * 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
 
 726  * These functions are exactly the same as the above functions (but use a hex
 
 727  * output for the decimal challenged). For details look at the above unsigned
 
 732  * debugfs_create_x8 - create a debugfs file that is used to read and write an unsigned 8-bit value
 
 733  * @name: a pointer to a string containing the name of the file to create.
 
 734  * @mode: the permission that the file should have
 
 735  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 736  *          directory dentry if set.  If this parameter is %NULL, then the
 
 737  *          file will be created in the root of the debugfs filesystem.
 
 738  * @value: a pointer to the variable that the file should read to and write
 
 741 void debugfs_create_x8(const char *name, umode_t mode, struct dentry *parent,
 
 744         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x8,
 
 745                                    &fops_x8_ro, &fops_x8_wo);
 
 747 EXPORT_SYMBOL_GPL(debugfs_create_x8);
 
 750  * debugfs_create_x16 - create a debugfs file that is used to read and write an unsigned 16-bit value
 
 751  * @name: a pointer to a string containing the name of the file to create.
 
 752  * @mode: the permission that the file should have
 
 753  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 754  *          directory dentry if set.  If this parameter is %NULL, then the
 
 755  *          file will be created in the root of the debugfs filesystem.
 
 756  * @value: a pointer to the variable that the file should read to and write
 
 759 void debugfs_create_x16(const char *name, umode_t mode, struct dentry *parent,
 
 762         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x16,
 
 763                                    &fops_x16_ro, &fops_x16_wo);
 
 765 EXPORT_SYMBOL_GPL(debugfs_create_x16);
 
 768  * debugfs_create_x32 - create a debugfs file that is used to read and write an unsigned 32-bit value
 
 769  * @name: a pointer to a string containing the name of the file to create.
 
 770  * @mode: the permission that the file should have
 
 771  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 772  *          directory dentry if set.  If this parameter is %NULL, then the
 
 773  *          file will be created in the root of the debugfs filesystem.
 
 774  * @value: a pointer to the variable that the file should read to and write
 
 777 void debugfs_create_x32(const char *name, umode_t mode, struct dentry *parent,
 
 780         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x32,
 
 781                                    &fops_x32_ro, &fops_x32_wo);
 
 783 EXPORT_SYMBOL_GPL(debugfs_create_x32);
 
 786  * debugfs_create_x64 - create a debugfs file that is used to read and write an unsigned 64-bit value
 
 787  * @name: a pointer to a string containing the name of the file to create.
 
 788  * @mode: the permission that the file should have
 
 789  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 790  *          directory dentry if set.  If this parameter is %NULL, then the
 
 791  *          file will be created in the root of the debugfs filesystem.
 
 792  * @value: a pointer to the variable that the file should read to and write
 
 795 void debugfs_create_x64(const char *name, umode_t mode, struct dentry *parent,
 
 798         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_x64,
 
 799                                    &fops_x64_ro, &fops_x64_wo);
 
 801 EXPORT_SYMBOL_GPL(debugfs_create_x64);
 
 804 static int debugfs_size_t_set(void *data, u64 val)
 
 806         *(size_t *)data = val;
 
 809 static int debugfs_size_t_get(void *data, u64 *val)
 
 811         *val = *(size_t *)data;
 
 814 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t, debugfs_size_t_get, debugfs_size_t_set,
 
 815                         "%llu\n"); /* %llu and %zu are more or less the same */
 
 816 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_ro, debugfs_size_t_get, NULL, "%llu\n");
 
 817 DEFINE_DEBUGFS_ATTRIBUTE(fops_size_t_wo, NULL, debugfs_size_t_set, "%llu\n");
 
 820  * debugfs_create_size_t - create a debugfs file that is used to read and write an size_t value
 
 821  * @name: a pointer to a string containing the name of the file to create.
 
 822  * @mode: the permission that the file should have
 
 823  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 824  *          directory dentry if set.  If this parameter is %NULL, then the
 
 825  *          file will be created in the root of the debugfs filesystem.
 
 826  * @value: a pointer to the variable that the file should read to and write
 
 829 void debugfs_create_size_t(const char *name, umode_t mode,
 
 830                            struct dentry *parent, size_t *value)
 
 832         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_size_t,
 
 833                                    &fops_size_t_ro, &fops_size_t_wo);
 
 835 EXPORT_SYMBOL_GPL(debugfs_create_size_t);
 
 837 static int debugfs_atomic_t_set(void *data, u64 val)
 
 839         atomic_set((atomic_t *)data, val);
 
 842 static int debugfs_atomic_t_get(void *data, u64 *val)
 
 844         *val = atomic_read((atomic_t *)data);
 
 847 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t, debugfs_atomic_t_get,
 
 848                         debugfs_atomic_t_set, "%lld\n");
 
 849 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_ro, debugfs_atomic_t_get, NULL,
 
 851 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(fops_atomic_t_wo, NULL, debugfs_atomic_t_set,
 
 855  * debugfs_create_atomic_t - create a debugfs file that is used to read and
 
 856  * write an atomic_t value
 
 857  * @name: a pointer to a string containing the name of the file to create.
 
 858  * @mode: the permission that the file should have
 
 859  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 860  *          directory dentry if set.  If this parameter is %NULL, then the
 
 861  *          file will be created in the root of the debugfs filesystem.
 
 862  * @value: a pointer to the variable that the file should read to and write
 
 865 void debugfs_create_atomic_t(const char *name, umode_t mode,
 
 866                              struct dentry *parent, atomic_t *value)
 
 868         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_atomic_t,
 
 869                                    &fops_atomic_t_ro, &fops_atomic_t_wo);
 
 871 EXPORT_SYMBOL_GPL(debugfs_create_atomic_t);
 
 873 ssize_t debugfs_read_file_bool(struct file *file, char __user *user_buf,
 
 874                                size_t count, loff_t *ppos)
 
 879         struct dentry *dentry = F_DENTRY(file);
 
 881         r = debugfs_file_get(dentry);
 
 884         val = *(bool *)file->private_data;
 
 885         debugfs_file_put(dentry);
 
 892         return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
 
 894 EXPORT_SYMBOL_GPL(debugfs_read_file_bool);
 
 896 ssize_t debugfs_write_file_bool(struct file *file, const char __user *user_buf,
 
 897                                 size_t count, loff_t *ppos)
 
 901         bool *val = file->private_data;
 
 902         struct dentry *dentry = F_DENTRY(file);
 
 904         r = kstrtobool_from_user(user_buf, count, &bv);
 
 906                 r = debugfs_file_get(dentry);
 
 910                 debugfs_file_put(dentry);
 
 915 EXPORT_SYMBOL_GPL(debugfs_write_file_bool);
 
 917 static const struct file_operations fops_bool = {
 
 918         .read =         debugfs_read_file_bool,
 
 919         .write =        debugfs_write_file_bool,
 
 921         .llseek =       default_llseek,
 
 924 static const struct file_operations fops_bool_ro = {
 
 925         .read =         debugfs_read_file_bool,
 
 927         .llseek =       default_llseek,
 
 930 static const struct file_operations fops_bool_wo = {
 
 931         .write =        debugfs_write_file_bool,
 
 933         .llseek =       default_llseek,
 
 937  * debugfs_create_bool - create a debugfs file that is used to read and write a boolean value
 
 938  * @name: a pointer to a string containing the name of the file to create.
 
 939  * @mode: the permission that the file should have
 
 940  * @parent: a pointer to the parent dentry for this file.  This should be a
 
 941  *          directory dentry if set.  If this parameter is %NULL, then the
 
 942  *          file will be created in the root of the debugfs filesystem.
 
 943  * @value: a pointer to the variable that the file should read to and write
 
 946  * This function creates a file in debugfs with the given name that
 
 947  * contains the value of the variable @value.  If the @mode variable is so
 
 948  * set, it can be read from, and written to.
 
 950 void debugfs_create_bool(const char *name, umode_t mode, struct dentry *parent,
 
 953         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_bool,
 
 954                                    &fops_bool_ro, &fops_bool_wo);
 
 956 EXPORT_SYMBOL_GPL(debugfs_create_bool);
 
 958 ssize_t debugfs_read_file_str(struct file *file, char __user *user_buf,
 
 959                               size_t count, loff_t *ppos)
 
 961         struct dentry *dentry = F_DENTRY(file);
 
 962         char *str, *copy = NULL;
 
 966         ret = debugfs_file_get(dentry);
 
 970         str = *(char **)file->private_data;
 
 971         len = strlen(str) + 1;
 
 972         copy = kmalloc(len, GFP_KERNEL);
 
 974                 debugfs_file_put(dentry);
 
 978         copy_len = strscpy(copy, str, len);
 
 979         debugfs_file_put(dentry);
 
 985         copy[copy_len] = '\n';
 
 987         ret = simple_read_from_buffer(user_buf, count, ppos, copy, len);
 
 992 EXPORT_SYMBOL_GPL(debugfs_create_str);
 
 994 static ssize_t debugfs_write_file_str(struct file *file, const char __user *user_buf,
 
 995                                       size_t count, loff_t *ppos)
 
 997         struct dentry *dentry = F_DENTRY(file);
 
 998         char *old, *new = NULL;
 
1002         r = debugfs_file_get(dentry);
 
1006         old = *(char **)file->private_data;
 
1008         /* only allow strict concatenation */
 
1010         if (pos && pos != strlen(old))
 
1014         if (pos + count + 1 > PAGE_SIZE)
 
1018         new = kmalloc(pos + count + 1, GFP_KERNEL);
 
1023                 memcpy(new, old, pos);
 
1026         if (copy_from_user(new + pos, user_buf, count))
 
1029         new[pos + count] = '\0';
 
1032         rcu_assign_pointer(*(char __rcu **)file->private_data, new);
 
1036         debugfs_file_put(dentry);
 
1041         debugfs_file_put(dentry);
 
1045 static const struct file_operations fops_str = {
 
1046         .read =         debugfs_read_file_str,
 
1047         .write =        debugfs_write_file_str,
 
1048         .open =         simple_open,
 
1049         .llseek =       default_llseek,
 
1052 static const struct file_operations fops_str_ro = {
 
1053         .read =         debugfs_read_file_str,
 
1054         .open =         simple_open,
 
1055         .llseek =       default_llseek,
 
1058 static const struct file_operations fops_str_wo = {
 
1059         .write =        debugfs_write_file_str,
 
1060         .open =         simple_open,
 
1061         .llseek =       default_llseek,
 
1065  * debugfs_create_str - create a debugfs file that is used to read and write a string value
 
1066  * @name: a pointer to a string containing the name of the file to create.
 
1067  * @mode: the permission that the file should have
 
1068  * @parent: a pointer to the parent dentry for this file.  This should be a
 
1069  *          directory dentry if set.  If this parameter is %NULL, then the
 
1070  *          file will be created in the root of the debugfs filesystem.
 
1071  * @value: a pointer to the variable that the file should read to and write
 
1074  * This function creates a file in debugfs with the given name that
 
1075  * contains the value of the variable @value.  If the @mode variable is so
 
1076  * set, it can be read from, and written to.
 
1078 void debugfs_create_str(const char *name, umode_t mode,
 
1079                         struct dentry *parent, char **value)
 
1081         debugfs_create_mode_unsafe(name, mode, parent, value, &fops_str,
 
1082                                    &fops_str_ro, &fops_str_wo);
 
1085 static ssize_t read_file_blob(struct file *file, char __user *user_buf,
 
1086                               size_t count, loff_t *ppos)
 
1088         struct debugfs_blob_wrapper *blob = file->private_data;
 
1089         struct dentry *dentry = F_DENTRY(file);
 
1092         r = debugfs_file_get(dentry);
 
1095         r = simple_read_from_buffer(user_buf, count, ppos, blob->data,
 
1097         debugfs_file_put(dentry);
 
1101 static const struct file_operations fops_blob = {
 
1102         .read =         read_file_blob,
 
1103         .open =         simple_open,
 
1104         .llseek =       default_llseek,
 
1108  * debugfs_create_blob - create a debugfs file that is used to read a binary blob
 
1109  * @name: a pointer to a string containing the name of the file to create.
 
1110  * @mode: the read permission that the file should have (other permissions are
 
1112  * @parent: a pointer to the parent dentry for this file.  This should be a
 
1113  *          directory dentry if set.  If this parameter is %NULL, then the
 
1114  *          file will be created in the root of the debugfs filesystem.
 
1115  * @blob: a pointer to a struct debugfs_blob_wrapper which contains a pointer
 
1116  *        to the blob data and the size of the data.
 
1118  * This function creates a file in debugfs with the given name that exports
 
1119  * @blob->data as a binary blob. If the @mode variable is so set it can be
 
1120  * read from. Writing is not supported.
 
1122  * This function will return a pointer to a dentry if it succeeds.  This
 
1123  * pointer must be passed to the debugfs_remove() function when the file is
 
1124  * to be removed (no automatic cleanup happens if your module is unloaded,
 
1125  * you are responsible here.)  If an error occurs, ERR_PTR(-ERROR) will be
 
1128  * If debugfs is not enabled in the kernel, the value ERR_PTR(-ENODEV) will
 
1131 struct dentry *debugfs_create_blob(const char *name, umode_t mode,
 
1132                                    struct dentry *parent,
 
1133                                    struct debugfs_blob_wrapper *blob)
 
1135         return debugfs_create_file_unsafe(name, mode & 0444, parent, blob, &fops_blob);
 
1137 EXPORT_SYMBOL_GPL(debugfs_create_blob);
 
1139 static size_t u32_format_array(char *buf, size_t bufsize,
 
1140                                u32 *array, int array_size)
 
1144         while (--array_size >= 0) {
 
1146                 char term = array_size ? ' ' : '\n';
 
1148                 len = snprintf(buf, bufsize, "%u%c", *array++, term);
 
1157 static int u32_array_open(struct inode *inode, struct file *file)
 
1159         struct debugfs_u32_array *data = inode->i_private;
 
1160         int size, elements = data->n_elements;
 
1165          *  - 10 digits + ' '/'\n' = 11 bytes per number
 
1166          *  - terminating NUL character
 
1169         buf = kmalloc(size+1, GFP_KERNEL);
 
1174         file->private_data = buf;
 
1175         u32_format_array(buf, size, data->array, data->n_elements);
 
1177         return nonseekable_open(inode, file);
 
1180 static ssize_t u32_array_read(struct file *file, char __user *buf, size_t len,
 
1183         size_t size = strlen(file->private_data);
 
1185         return simple_read_from_buffer(buf, len, ppos,
 
1186                                         file->private_data, size);
 
1189 static int u32_array_release(struct inode *inode, struct file *file)
 
1191         kfree(file->private_data);
 
1196 static const struct file_operations u32_array_fops = {
 
1197         .owner   = THIS_MODULE,
 
1198         .open    = u32_array_open,
 
1199         .release = u32_array_release,
 
1200         .read    = u32_array_read,
 
1201         .llseek  = no_llseek,
 
1205  * debugfs_create_u32_array - create a debugfs file that is used to read u32
 
1207  * @name: a pointer to a string containing the name of the file to create.
 
1208  * @mode: the permission that the file should have.
 
1209  * @parent: a pointer to the parent dentry for this file.  This should be a
 
1210  *          directory dentry if set.  If this parameter is %NULL, then the
 
1211  *          file will be created in the root of the debugfs filesystem.
 
1212  * @array: wrapper struct containing data pointer and size of the array.
 
1214  * This function creates a file in debugfs with the given name that exports
 
1215  * @array as data. If the @mode variable is so set it can be read from.
 
1216  * Writing is not supported. Seek within the file is also not supported.
 
1217  * Once array is created its size can not be changed.
 
1219 void debugfs_create_u32_array(const char *name, umode_t mode,
 
1220                               struct dentry *parent,
 
1221                               struct debugfs_u32_array *array)
 
1223         debugfs_create_file_unsafe(name, mode, parent, array, &u32_array_fops);
 
1225 EXPORT_SYMBOL_GPL(debugfs_create_u32_array);
 
1227 #ifdef CONFIG_HAS_IOMEM
 
1230  * The regset32 stuff is used to print 32-bit registers using the
 
1231  * seq_file utilities. We offer printing a register set in an already-opened
 
1232  * sequential file or create a debugfs file that only prints a regset32.
 
1236  * debugfs_print_regs32 - use seq_print to describe a set of registers
 
1237  * @s: the seq_file structure being used to generate output
 
1238  * @regs: an array if struct debugfs_reg32 structures
 
1239  * @nregs: the length of the above array
 
1240  * @base: the base address to be used in reading the registers
 
1241  * @prefix: a string to be prefixed to every output line
 
1243  * This function outputs a text block describing the current values of
 
1244  * some 32-bit hardware registers. It is meant to be used within debugfs
 
1245  * files based on seq_file that need to show registers, intermixed with other
 
1246  * information. The prefix argument may be used to specify a leading string,
 
1247  * because some peripherals have several blocks of identical registers,
 
1248  * for example configuration of dma channels
 
1250 void debugfs_print_regs32(struct seq_file *s, const struct debugfs_reg32 *regs,
 
1251                           int nregs, void __iomem *base, char *prefix)
 
1255         for (i = 0; i < nregs; i++, regs++) {
 
1257                         seq_printf(s, "%s", prefix);
 
1258                 seq_printf(s, "%s = 0x%08x\n", regs->name,
 
1259                            readl(base + regs->offset));
 
1260                 if (seq_has_overflowed(s))
 
1264 EXPORT_SYMBOL_GPL(debugfs_print_regs32);
 
1266 static int debugfs_regset32_show(struct seq_file *s, void *data)
 
1268         struct debugfs_regset32 *regset = s->private;
 
1271                 pm_runtime_get_sync(regset->dev);
 
1273         debugfs_print_regs32(s, regset->regs, regset->nregs, regset->base, "");
 
1276                 pm_runtime_put(regset->dev);
 
1281 DEFINE_SHOW_ATTRIBUTE(debugfs_regset32);
 
1284  * debugfs_create_regset32 - create a debugfs file that returns register values
 
1285  * @name: a pointer to a string containing the name of the file to create.
 
1286  * @mode: the permission that the file should have
 
1287  * @parent: a pointer to the parent dentry for this file.  This should be a
 
1288  *          directory dentry if set.  If this parameter is %NULL, then the
 
1289  *          file will be created in the root of the debugfs filesystem.
 
1290  * @regset: a pointer to a struct debugfs_regset32, which contains a pointer
 
1291  *          to an array of register definitions, the array size and the base
 
1292  *          address where the register bank is to be found.
 
1294  * This function creates a file in debugfs with the given name that reports
 
1295  * the names and values of a set of 32-bit registers. If the @mode variable
 
1296  * is so set it can be read from. Writing is not supported.
 
1298 void debugfs_create_regset32(const char *name, umode_t mode,
 
1299                              struct dentry *parent,
 
1300                              struct debugfs_regset32 *regset)
 
1302         debugfs_create_file(name, mode, parent, regset, &debugfs_regset32_fops);
 
1304 EXPORT_SYMBOL_GPL(debugfs_create_regset32);
 
1306 #endif /* CONFIG_HAS_IOMEM */
 
1308 struct debugfs_devm_entry {
 
1309         int (*read)(struct seq_file *seq, void *data);
 
1313 static int debugfs_devm_entry_open(struct inode *inode, struct file *f)
 
1315         struct debugfs_devm_entry *entry = inode->i_private;
 
1317         return single_open(f, entry->read, entry->dev);
 
1320 static const struct file_operations debugfs_devm_entry_ops = {
 
1321         .owner = THIS_MODULE,
 
1322         .open = debugfs_devm_entry_open,
 
1323         .release = single_release,
 
1329  * debugfs_create_devm_seqfile - create a debugfs file that is bound to device.
 
1331  * @dev: device related to this debugfs file.
 
1332  * @name: name of the debugfs file.
 
1333  * @parent: a pointer to the parent dentry for this file.  This should be a
 
1334  *      directory dentry if set.  If this parameter is %NULL, then the
 
1335  *      file will be created in the root of the debugfs filesystem.
 
1336  * @read_fn: function pointer called to print the seq_file content.
 
1338 void debugfs_create_devm_seqfile(struct device *dev, const char *name,
 
1339                                  struct dentry *parent,
 
1340                                  int (*read_fn)(struct seq_file *s, void *data))
 
1342         struct debugfs_devm_entry *entry;
 
1347         entry = devm_kzalloc(dev, sizeof(*entry), GFP_KERNEL);
 
1351         entry->read = read_fn;
 
1354         debugfs_create_file(name, S_IRUGO, parent, entry,
 
1355                             &debugfs_devm_entry_ops);
 
1357 EXPORT_SYMBOL_GPL(debugfs_create_devm_seqfile);