Forum | Documentation | Website | Blog

Skip to content
Snippets Groups Projects
  1. Oct 19, 2023
  2. Oct 18, 2023
  3. May 15, 2023
  4. Dec 02, 2022
    • Yang Yingliang's avatar
      chardev: fix error handling in cdev_device_add() · 11fa7fef
      Yang Yingliang authored
      While doing fault injection test, I got the following report:
      
      ------------[ cut here ]------------
      kobject: '(null)' (0000000039956980): is not initialized, yet kobject_put() is being called.
      WARNING: CPU: 3 PID: 6306 at kobject_put+0x23d/0x4e0
      CPU: 3 PID: 6306 Comm: 283 Tainted: G        W          6.1.0-rc2-00005-g307c1086d7c9 #1253
      Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.13.0-1ubuntu1.1 04/01/2014
      RIP: 0010:kobject_put+0x23d/0x4e0
      Call Trace:
       <TASK>
       cdev_device_add+0x15e/0x1b0
       __iio_device_register+0x13b4/0x1af0 [industrialio]
       __devm_iio_device_register+0x22/0x90 [industrialio]
       max517_probe+0x3d8/0x6b4 [max517]
       i2c_device_probe+0xa81/0xc00
      
      When device_add() is injected fault and returns error, if dev->devt is not set,
      cdev_add() is not called, cdev_del() is not needed. Fix this by checking dev->devt
      in error path.
      
      Fixes: 233ed09d
      
       ("chardev: add helper function to register char devs with a struct device")
      Signed-off-by: default avatarYang Yingliang <yangyingliang@huawei.com>
      Link: https://lore.kernel.org/r/20221202030237.520280-1-yangyingliang@huawei.com
      
      
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      11fa7fef
  5. Nov 10, 2022
    • Shang XiaoJing's avatar
      chardev: Fix potential memory leak when cdev_add() failed · 4634c973
      Shang XiaoJing authored
      
      Some init function of cdev(like comedi) will call kobject_set_name()
      before cdev_add(), but won't free the cdev.kobj.name or put the ref cnt
      of cdev.kobj when cdev_add() failed. As the result, cdev.kobj.name will
      be leaked.
      
      Free the name of kobject in cdev_add() fail path to prevent memleak. With
      this fix, the callers don't need to care about freeing the name of
      kobject if cdev_add() fails.
      
      unreferenced object 0xffff8881000fa8c0 (size 8):
        comm "modprobe", pid 239, jiffies 4294905173 (age 51.308s)
        hex dump (first 8 bytes):
          63 6f 6d 65 64 69 00 ff                          comedi..
        backtrace:
          [<000000005f9878f7>] __kmalloc_node_track_caller+0x4c/0x1c0
          [<000000000fd70302>] kstrdup+0x3f/0x70
          [<000000009428bc33>] kstrdup_const+0x46/0x60
          [<00000000ed50d9de>] kvasprintf_const+0xdb/0xf0
          [<00000000b2766964>] kobject_set_name_vargs+0x3c/0xe0
          [<00000000f2424ef7>] kobject_set_name+0x62/0x90
          [<000000005d5a125b>] 0xffffffffa0013098
          [<00000000f331e663>] do_one_initcall+0x7a/0x380
          [<00000000aa7bac96>] do_init_module+0x5c/0x230
          [<000000005fd72335>] load_module+0x227d/0x2420
          [<00000000ad550cf1>] __do_sys_finit_module+0xd5/0x140
          [<00000000069a60c5>] do_syscall_64+0x3f/0x90
          [<00000000c5e0d521>] entry_SYSCALL_64_after_hwframe+0x63/0xcd
      
      Suggested-by: default avatarGreg KH <gregkh@linuxfoundation.org>
      Signed-off-by: default avatarShang XiaoJing <shangxiaojing@huawei.com>
      Link: https://lore.kernel.org/r/20221102072659.23671-1-shangxiaojing@huawei.com
      
      
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      4634c973
  6. May 14, 2020
    • Miklos Szeredi's avatar
      vfs: allow unprivileged whiteout creation · a3c751a5
      Miklos Szeredi authored
      
      Whiteouts, unlike real device node should not require privileges to create.
      
      The general concern with device nodes is that opening them can have side
      effects.  The kernel already avoids zero major (see
      Documentation/admin-guide/devices.txt).  To be on the safe side the patch
      explicitly forbids registering a char device with 0/0 number (see
      cdev_add()).
      
      This guarantees that a non-O_PATH open on a whiteout will fail with ENODEV;
      i.e. it won't have any side effect.
      
      Signed-off-by: default avatarMiklos Szeredi <mszeredi@redhat.com>
      a3c751a5
  7. Jan 06, 2020
    • Will Deacon's avatar
      chardev: Avoid potential use-after-free in 'chrdev_open()' · 68faa679
      Will Deacon authored
      
      'chrdev_open()' calls 'cdev_get()' to obtain a reference to the
      'struct cdev *' stashed in the 'i_cdev' field of the target inode
      structure. If the pointer is NULL, then it is initialised lazily by
      looking up the kobject in the 'cdev_map' and so the whole procedure is
      protected by the 'cdev_lock' spinlock to serialise initialisation of
      the shared pointer.
      
      Unfortunately, it is possible for the initialising thread to fail *after*
      installing the new pointer, for example if the subsequent '->open()' call
      on the file fails. In this case, 'cdev_put()' is called, the reference
      count on the kobject is dropped and, if nobody else has taken a reference,
      the release function is called which finally clears 'inode->i_cdev' from
      'cdev_purge()' before potentially freeing the object. The problem here
      is that a racing thread can happily take the 'cdev_lock' and see the
      non-NULL pointer in the inode, which can result in a refcount increment
      from zero and a warning:
      
        |  ------------[ cut here ]------------
        |  refcount_t: addition on 0; use-after-free.
        |  WARNING: CPU: 2 PID: 6385 at lib/refcount.c:25 refcount_warn_saturate+0x6d/0xf0
        |  Modules linked in:
        |  CPU: 2 PID: 6385 Comm: repro Not tainted 5.5.0-rc2+ #22
        |  Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-1 04/01/2014
        |  RIP: 0010:refcount_warn_saturate+0x6d/0xf0
        |  Code: 05 55 9a 15 01 01 e8 9d aa c8 ff 0f 0b c3 80 3d 45 9a 15 01 00 75 ce 48 c7 c7 00 9c 62 b3 c6 08
        |  RSP: 0018:ffffb524c1b9bc70 EFLAGS: 00010282
        |  RAX: 0000000000000000 RBX: ffff9e9da1f71390 RCX: 0000000000000000
        |  RDX: ffff9e9dbbd27618 RSI: ffff9e9dbbd18798 RDI: ffff9e9dbbd18798
        |  RBP: 0000000000000000 R08: 000000000000095f R09: 0000000000000039
        |  R10: 0000000000000000 R11: ffffb524c1b9bb20 R12: ffff9e9da1e8c700
        |  R13: ffffffffb25ee8b0 R14: 0000000000000000 R15: ffff9e9da1e8c700
        |  FS:  00007f3b87d26700(0000) GS:ffff9e9dbbd00000(0000) knlGS:0000000000000000
        |  CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
        |  CR2: 00007fc16909c000 CR3: 000000012df9c000 CR4: 00000000000006e0
        |  DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
        |  DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
        |  Call Trace:
        |   kobject_get+0x5c/0x60
        |   cdev_get+0x2b/0x60
        |   chrdev_open+0x55/0x220
        |   ? cdev_put.part.3+0x20/0x20
        |   do_dentry_open+0x13a/0x390
        |   path_openat+0x2c8/0x1470
        |   do_filp_open+0x93/0x100
        |   ? selinux_file_ioctl+0x17f/0x220
        |   do_sys_open+0x186/0x220
        |   do_syscall_64+0x48/0x150
        |   entry_SYSCALL_64_after_hwframe+0x44/0xa9
        |  RIP: 0033:0x7f3b87efcd0e
        |  Code: 89 54 24 08 e8 a3 f4 ff ff 8b 74 24 0c 48 8b 3c 24 41 89 c0 44 8b 54 24 08 b8 01 01 00 00 89 f4
        |  RSP: 002b:00007f3b87d259f0 EFLAGS: 00000293 ORIG_RAX: 0000000000000101
        |  RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f3b87efcd0e
        |  RDX: 0000000000000000 RSI: 00007f3b87d25a80 RDI: 00000000ffffff9c
        |  RBP: 00007f3b87d25e90 R08: 0000000000000000 R09: 0000000000000000
        |  R10: 0000000000000000 R11: 0000000000000293 R12: 00007ffe188f504e
        |  R13: 00007ffe188f504f R14: 00007f3b87d26700 R15: 0000000000000000
        |  ---[ end trace 24f53ca58db8180a ]---
      
      Since 'cdev_get()' can already fail to obtain a reference, simply move
      it over to use 'kobject_get_unless_zero()' instead of 'kobject_get()',
      which will cause the racing thread to return -ENXIO if the initialising
      thread fails unexpectedly.
      
      Cc: Hillf Danton <hdanton@sina.com>
      Cc: Andrew Morton <akpm@linux-foundation.org>
      Cc: Al Viro <viro@zeniv.linux.org.uk>
      Reported-by: default avatar <syzbot+82defefbbd8527e1c2cb@syzkaller.appspotmail.com>
      Signed-off-by: default avatarWill Deacon <will@kernel.org>
      Cc: stable <stable@vger.kernel.org>
      Link: https://lore.kernel.org/r/20191219120203.32691-1-will@kernel.org
      
      
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      68faa679
  8. May 24, 2019
  9. Apr 02, 2019
  10. Mar 15, 2018
  11. Nov 02, 2017
    • Greg Kroah-Hartman's avatar
      License cleanup: add SPDX GPL-2.0 license identifier to files with no license · b2441318
      Greg Kroah-Hartman authored
      Many source files in the tree are missing licensing information, which
      makes it harder for compliance tools to determine the correct license.
      
      By default all files without license information are under the default
      license of the kernel, which is GPL version 2.
      
      Update the files which contain no license information with the 'GPL-2.0'
      SPDX license identifier.  The SPDX identifier is a legally binding
      shorthand, which can be used instead of the full boiler plate text.
      
      This patch is based on work done by Thomas Gleixner and Kate Stewart and
      Philippe Ombredanne.
      
      How this work was done:
      
      Patches were generated and checked against linux-4.14-rc6 for a subset of
      the use cases:
       - file had no licensing information it it.
       - file was a */uapi/* one with no licensing information in it,
       - file was a */uapi/* one with existing licensing information,
      
      Further patches will be generated in subsequent months to fix up cases
      where non-standard...
      b2441318
  12. Jul 17, 2017
    • Logan Gunthorpe's avatar
      char_dev: order /proc/devices by major number · 8a932f73
      Logan Gunthorpe authored
      
      Presently, the order of the char devices listed in /proc/devices is not
      entirely sequential. If a char device has a major number greater than
      CHRDEV_MAJOR_HASH_SIZE (255), it will be ordered as if its major were
      module 255. For example, 511 appears after 1.
      
      This patch cleans that up and prints each major number in the correct
      order, regardless of where they are stored in the hash table.
      
      In order to do this, we introduce CHRDEV_MAJOR_MAX as an artificial
      limit (chosen to be 511). It will then print all devices in major
      order number from 0 to the maximum.
      
      Signed-off-by: default avatarLogan Gunthorpe <logang@deltatee.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Alan Cox <alan@linux.intel.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Linus Walleij <linus.walleij@linaro.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      8a932f73
    • Logan Gunthorpe's avatar
      char_dev: extend dynamic allocation of majors into a higher range · a5d31a3f
      Logan Gunthorpe authored
      
      We've run into problems with running out of dynamicly assign char
      device majors particullarly on automated test systems with
      all-yes-configs. Roughly 40 dynamic assignments can be made with such
      kernels at this time while space is reserved for only 20.
      
      Currently, the kernel only prints a warning when dynamic allocation
      overflows the reserved region. And when this happens drivers that have
      fixed assignments can randomly fail depending on the order of
      initialization of other drivers. Thus, adding a new char device can cause
      unexpected failures in completely unrelated parts of the kernel.
      
      This patch solves the problem by extending dynamic major number
      allocations down from 511 once the 234-254 region fills up. Fixed
      majors already exist above 255 so the infrastructure to support
      high number majors is already in place. The patch reserves an
      additional 128 major numbers which should hopefully last us a while.
      
      Kernels that don't require more than 20 dynamic majors assigned (which
      is pretty typical) should not be affected by this change.
      
      Signed-off-by: default avatarLogan Gunthorpe <logang@deltatee.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Alan Cox <alan@linux.intel.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Linus Walleij <linus.walleij@linaro.org>
      Link: https://lkml.org/lkml/2017/6/4/107
      
      
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a5d31a3f
  13. Mar 21, 2017
    • Logan Gunthorpe's avatar
      chardev: add helper function to register char devs with a struct device · 233ed09d
      Logan Gunthorpe authored
      Credit for this patch goes is shared with Dan Williams [1]. I've
      taken things one step further to make the helper function more
      useful and clean up calling code.
      
      There's a common pattern in the kernel whereby a struct cdev is placed
      in a structure along side a struct device which manages the life-cycle
      of both. In the naive approach, the reference counting is broken and
      the struct device can free everything before the chardev code
      is entirely released.
      
      Many developers have solved this problem by linking the internal kobjs
      in this fashion:
      
      cdev.kobj.parent = &parent_dev.kobj;
      
      The cdev code explicitly gets and puts a reference to it's kobj parent.
      So this seems like it was intended to be used this way. Dmitrty Torokhov
      first put this in place in 2012 with this commit:
      
      2f0157f1 char_dev: pin parent kobject
      
      and the first instance of the fix was then done in the input subsystem
      in the following commit:
      
      4a215aad Input: fix use-after-free introduced with dynamic minor changes
      
      Subsequently over the years, however, this issue seems to have tripped
      up multiple developers independently. For example, see these commits:
      
      0d5b7dae iio: Prevent race between IIO chardev opening and IIO device
      (by Lars-Peter Clausen in 2013)
      
      ba0ef854 tpm: Fix initialization of the cdev
      (by Jason Gunthorpe in 2015)
      
      5b28dde5 [media] media: fix use-after-free in cdev_put() when app exits
      after driver unbind
      (by Shauh Khan in 2016)
      
      This technique is similarly done in at least 15 places within the kernel
      and probably should have been done so in another, at least, 5 places.
      The kobj line also looks very suspect in that one would not expect
      drivers to have to mess with kobject internals in this way.
      Even highly experienced kernel developers can be surprised by this
      code, as seen in [2].
      
      To help alleviate this situation, and hopefully prevent future
      wasted effort on this problem, this patch introduces a helper function
      to register a char device along with its parent struct device.
      This creates a more regular API for tying a char device to its parent
      without the developer having to set members in the underlying kobject.
      
      This patch introduce cdev_device_add and cdev_device_del which
      replaces a common pattern including setting the kobj parent, calling
      cdev_add and then calling device_add. It also introduces cdev_set_parent
      for the few cases that set the kobject parent without using device_add.
      
      [1] https://lkml.org/lkml/2017/2/13/700
      [2] https://lkml.org/lkml/2017/2/10/370
      
      
      
      Signed-off-by: default avatarLogan Gunthorpe <logang@deltatee.com>
      Signed-off-by: default avatarDan Williams <dan.j.williams@intel.com>
      Reviewed-by: default avatarHans Verkuil <hans.verkuil@cisco.com>
      Reviewed-by: default avatarAlexandre Belloni <alexandre.belloni@free-electrons.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      233ed09d
  14. Aug 24, 2016
  15. Jul 14, 2016
    • Fengguang Wu's avatar
      chardev: add missing line break in pr_warn · 077e2642
      Fengguang Wu authored
      
      To fix super long dmesg error lines like
      
        CHRDEV "dummy_stm.0" major number 224 goes below the dynamic allocation rangeCHRDEV "dummy_stm.1" major number 223 goes below the dynamic allocation rangeswapper: page allocation failure: order:8, mode:0x26040c0(GFP_KERNEL|__GFP_COMP|__GFP_NOTRACK)
      
      After fix, it should look like
      
        CHRDEV "dummy_stm.0" major number 224 goes below the dynamic allocation range
        CHRDEV "dummy_stm.1" major number 223 goes below the dynamic allocation range
        swapper: page allocation failure: order:8, mode:0x26040c0(GFP_KERNEL|__GFP_COMP|__GFP_NOTRACK)
      
      Reported-by: default avatarPhilip Li <philip.li@intel.com>
      Signed-off-by: default avatarFengguang Wu <fengguang.wu@intel.com>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      077e2642
  16. Mar 29, 2016
    • Linus Walleij's avatar
      chrdev: emit a warning when we go below dynamic major range · 49db08c3
      Linus Walleij authored
      Currently a dynamically allocated character device major is taken
      from 254 and downward. This mechanism is used for RTC, IIO and a
      few other subsystems.
      
      The kernel currently has no check prevening these dynamic
      allocations from eating into the assigned numbers at 233 and
      downward.
      
      In a recent test it was reported that so many dynamic device
      majors were used on a test server, that the major number for
      infiniband (231) was stolen. This occurred when allocating a new
      major number for GPIO chips. The error messages from the kernel
      were not helpful. (See: https://lkml.org/lkml/2016/2/14/124
      
      )
      
      This patch adds a defined lower limit of the dynamic major
      allocation region will henceforth emit a warning if we start to
      eat into the assigned numbers. It does not do any semantic
      changes and will not change the kernels behaviour: numbers will
      still continue to be stolen, but we will know from dmesg what
      is going on.
      
      This also updates the Documentation/devices.txt to clearly
      reflect that we are using this range of major numbers for dynamic
      allocation.
      
      Reported-by: default avatarYing Huang <ying.huang@linux.intel.com>
      Cc: Linus Torvalds <torvalds@linux-foundation.org>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Alan Cox <alan@linux.intel.com>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
      Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      49db08c3
  17. Aug 05, 2015
  18. Jan 20, 2015
  19. Dec 10, 2014
  20. Nov 08, 2013
  21. Oct 24, 2013
  22. Oct 22, 2012
  23. Dec 13, 2011
  24. Jan 13, 2011
  25. Dec 17, 2010
  26. Oct 15, 2010
    • Arnd Bergmann's avatar
      llseek: automatically add .llseek fop · 6038f373
      Arnd Bergmann authored
      All file_operations should get a .llseek operation so we can make
      nonseekable_open the default for future file operations without a
      .llseek pointer.
      
      The three cases that we can automatically detect are no_llseek, seq_lseek
      and default_llseek. For cases where we can we can automatically prove that
      the file offset is always ignored, we use noop_llseek, which maintains
      the current behavior of not returning an error from a seek.
      
      New drivers should normally not use noop_llseek but instead use no_llseek
      and call nonseekable_open at open time.  Existing drivers can be converted
      to do the same when the maintainer knows for certain that no user code
      relies on calling seek on the device file.
      
      The generated code is often incorrectly indented and right now contains
      comments that clarify for each added line why a specific variant was
      chosen. In the version that gets submitted upstream, the comments will
      be gone and I will manually fix the indentation, because there does not
      see...
      6038f373
  27. Sep 22, 2010
  28. Aug 06, 2010
    • David Howells's avatar
      Fix init ordering of /dev/console vs callers of modprobe · 31d1d48e
      David Howells authored
      
      Make /dev/console get initialised before any initialisation routine that
      invokes modprobe because if modprobe fails, it's going to want to open
      /dev/console, presumably to write an error message to.
      
      The problem with that is that if the /dev/console driver is not yet
      initialised, the chardev handler will call request_module() to invoke
      modprobe, which will fail, because we never compile /dev/console as a
      module.
      
      This will lead to a modprobe loop, showing the following in the kernel
      log:
      
      	request_module: runaway loop modprobe char-major-5-1
      	request_module: runaway loop modprobe char-major-5-1
      	request_module: runaway loop modprobe char-major-5-1
      	request_module: runaway loop modprobe char-major-5-1
      	request_module: runaway loop modprobe char-major-5-1
      
      This can happen, for example, when the built in md5 module can't find
      the built in cryptomgr module (because the latter fails to initialise).
      The md5 module comes before the call to tty_init(), presumably because
      'crypto' comes before 'drivers' alphabetically.
      
      Fix this by calling tty_init() from chrdev_init().
      
      Signed-off-by: default avatarDavid Howells <dhowells@redhat.com>
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      31d1d48e
  29. Sep 24, 2009
  30. Sep 11, 2009
  31. Aug 10, 2009
  32. Jul 12, 2009
  33. Jun 11, 2009
    • Theodore Ts'o's avatar
      fs: Remove i_cindex from struct inode · 9fd5746f
      Theodore Ts'o authored
      
      The only user of the i_cindex element in the inode structure is used
      is by the firewire drivers.  As part of an attempt to slim down the
      inode structure to save memory --- since a typical Linux system will
      have hundreds of thousands if not millions of inodes cached, a
      reduction in the size inode has high leverage.
      
      The firewire driver does not need i_cindex in any fast path, so it's
      simple enough to calculate when it is needed, instead of wasting space
      in the inode structure.
      
      Signed-off-by: default avatar"Theodore Ts'o" <tytso@mit.edu>
      Cc: krh@redhat.com
      Cc: stefanr@s5r6.in-berlin.de
      Cc: linux-fsdevel@vger.kernel.org
      Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
      9fd5746f
  34. Jan 06, 2009
  35. Oct 23, 2008