1. 14 2月, 2015 1 次提交
    • T
      kernfs: remove KERNFS_STATIC_NAME · dfeb0750
      Tejun Heo 提交于
      When a new kernfs node is created, KERNFS_STATIC_NAME is used to avoid
      making a separate copy of its name.  It's currently only used for sysfs
      attributes whose filenames are required to stay accessible and unchanged.
      There are rare exceptions where these names are allocated and formatted
      dynamically but for the vast majority of cases they're consts in the
      rodata section.
      
      Now that kernfs is converted to use kstrdup_const() and kfree_const(),
      there's little point in keeping KERNFS_STATIC_NAME around.  Remove it.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Cc: Andrzej Hajda <a.hajda@samsung.com>
      Signed-off-by: NAndrew Morton <akpm@linux-foundation.org>
      Signed-off-by: NLinus Torvalds <torvalds@linux-foundation.org>
      dfeb0750
  2. 08 11月, 2014 3 次提交
    • N
      sysfs/kernfs: make read requests on pre-alloc files use the buffer. · 4ef67a8c
      NeilBrown 提交于
      To match the previous patch which used the pre-alloc buffer for
      writes, this patch causes reads to use the same buffer.
      This is not strictly necessary as the current seq_read() will allocate
      on first read, so user-space can trigger the required pre-alloc.  But
      consistency is valuable.
      
      The read function is somewhat simpler than seq_read() and, for example,
      does not support reading from an offset into the file: reads must be
      at the start of the file.
      
      As seq_read() does not use the prealloc buffer, ->seq_show is
      incompatible with ->prealloc and caused an EINVAL return from open().
      sysfs code which calls into kernfs always chooses the correct function.
      
      As the buffer is shared with writes and other reads, the mutex is
      extended to cover the copy_to_user.
      Signed-off-by: NNeilBrown <neilb@suse.de>
      Reviewed-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      4ef67a8c
    • N
      sysfs/kernfs: allow attributes to request write buffer be pre-allocated. · 2b75869b
      NeilBrown 提交于
      md/raid allows metadata management to be performed in user-space.
      A various times, particularly on device failure, the metadata needs
      to be updated before further writes can be permitted.
      This means that the user-space program which updates metadata much
      not block on writeout, and so must not allocate memory.
      
      mlockall(MCL_CURRENT|MCL_FUTURE) and pre-allocation can avoid all
      memory allocation issues for user-memory, but that does not help
      kernel memory.
      Several kernel objects can be pre-allocated.  e.g. files opened before
      any writes to the array are permitted.
      However some kernel allocation happens in places that cannot be
      pre-allocated.
      In particular, writes to sysfs files (to tell md that it can now
      allow writes to the array) allocate a buffer using GFP_KERNEL.
      
      This patch allows attributes to be marked as "PREALLOC".  In that case
      the maximal buffer is allocated when the file is opened, and then used
      on each write instead of allocating a new buffer.
      
      As the same buffer is now shared for all writes on the same file
      description, the mutex is extended to cover full use of the buffer
      including the copy_from_user().
      
      The new __ATTR_PREALLOC() 'or's a new flag in to the 'mode', which is
      inspected by sysfs_add_file_mode_ns() to determine if the file should be
      marked as requiring prealloc.
      
      Despite the comment, we *do* use ->seq_show together with ->prealloc
      in this patch.  The next patch fixes that.
      Signed-off-by: NNeilBrown  <neilb@suse.de>
      Reviewed-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      2b75869b
    • V
      fs: sysfs: return EGBIG on write if offset is larger than file size · 09368960
      Vladimir Zapolskiy 提交于
      According to the user expectations common utilities like dd or sh
      redirection operator > should work correctly over binary files from
      sysfs. At the moment doing excessive write can not be completed:
      
        write(1, "\0\0\0\0\0\0\0\0", 8)         = 4
        write(1, "\0\0\0\0", 4)                 = 0
        write(1, "\0\0\0\0", 4)                 = 0
        write(1, "\0\0\0\0", 4)                 = 0
        ...
      
      Fix the problem by returning EFBIG described in man 2 write.
      Signed-off-by: NVladimir Zapolskiy <vladimir_zapolskiy@mentor.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      09368960
  3. 20 5月, 2014 1 次提交
    • T
      sysfs: make sure read buffer is zeroed · f5c16f29
      Tejun Heo 提交于
      13c589d5 ("sysfs: use seq_file when reading regular files")
      switched sysfs from custom read implementation to seq_file to enable
      later transition to kernfs.  After the change, the buffer passed to
      ->show() is acquired through seq_get_buf(); unfortunately, this
      introduces a subtle behavior change.  Before the commit, the buffer
      passed to ->show() was always zero as it was allocated using
      get_zeroed_page().  Because seq_file doesn't clear buffers on
      allocation and neither does seq_get_buf(), after the commit, depending
      on the behavior of ->show(), we may end up exposing uninitialized data
      to userland thus possibly altering userland visible behavior and
      leaking information.
      
      Fix it by explicitly clearing the buffer.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Reported-by: NRon <ron@debian.org>
      Fixes: 13c589d5 ("sysfs: use seq_file when reading regular files")
      Cc: stable <stable@vger.kernel.org> # 3.13+
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      f5c16f29
  4. 17 4月, 2014 1 次提交
  5. 26 3月, 2014 1 次提交
  6. 08 2月, 2014 2 次提交
    • T
      sysfs, driver-core: remove unused {sysfs|device}_schedule_callback_owner() · ce8b04aa
      Tejun Heo 提交于
      All device_schedule_callback_owner() users are converted to use
      device_remove_file_self().  Remove now unused
      {sysfs|device}_schedule_callback_owner().
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      ce8b04aa
    • T
      kernfs, sysfs, driver-core: implement kernfs_remove_self() and its wrappers · 6b0afc2a
      Tejun Heo 提交于
      Sometimes it's necessary to implement a node which wants to delete
      nodes including itself.  This isn't straightforward because of kernfs
      active reference.  While a file operation is in progress, an active
      reference is held and kernfs_remove() waits for all such references to
      drain before completing.  For a self-deleting node, this is a deadlock
      as kernfs_remove() ends up waiting for an active reference that itself
      is sitting on top of.
      
      This currently is worked around in the sysfs layer using
      sysfs_schedule_callback() which makes such removals asynchronous.
      While it works, it's rather cumbersome and inherently breaks
      synchronicity of the operation - the file operation which triggered
      the operation may complete before the removal is finished (or even
      started) and the removal may fail asynchronously.  If a removal
      operation is immmediately followed by another operation which expects
      the specific name to be available (e.g. removal followed by rename
      onto the same name), there's no way to make the latter operation
      reliable.
      
      The thing is there's no inherent reason for this to be asynchrnous.
      All that's necessary to do this synchronous is a dedicated operation
      which drops its own active ref and deactivates self.  This patch
      implements kernfs_remove_self() and its wrappers in sysfs and driver
      core.  kernfs_remove_self() is to be called from one of the file
      operations, drops the active ref the task is holding, removes the self
      node, and restores active ref to the dead node so that the ref is
      balanced afterwards.  __kernfs_remove() is updated so that it takes an
      early exit if the target node is already fully removed so that the
      active ref restored by kernfs_remove_self() after removal doesn't
      confuse the deactivation path.
      
      This makes implementing self-deleting nodes very easy.  The normal
      removal path doesn't even need to be changed to use
      kernfs_remove_self() for the self-deleting node.  The method can
      invoke kernfs_remove_self() on itself before proceeding the normal
      removal path.  kernfs_remove() invoked on the node by the normal
      deletion path will simply be ignored.
      
      This will replace sysfs_schedule_callback().  A subtle feature of
      sysfs_schedule_callback() is that it collapses multiple invocations -
      even if multiple removals are triggered, the removal callback is run
      only once.  An equivalent effect can be achieved by testing the return
      value of kernfs_remove_self() - only the one which gets %true return
      value should proceed with actual deletion.  All other instances of
      kernfs_remove_self() will wait till the enclosing kernfs operation
      which invoked the winning instance of kernfs_remove_self() finishes
      and then return %false.  This trivially makes all users of
      kernfs_remove_self() automatically show correct synchronous behavior
      even when there are multiple concurrent operations - all "echo 1 >
      delete" instances will finish only after the whole operation is
      completed by one of the instances.
      
      Note that manipulation of active ref is implemented in separate public
      functions - kernfs_[un]break_active_protection().
      kernfs_remove_self() is the only user at the moment but this will be
      used to cater to more complex cases.
      
      v2: For !CONFIG_SYSFS, dummy version kernfs_remove_self() was missing
          and sysfs_remove_file_self() had incorrect return type.  Fix it.
          Reported by kbuild test bot.
      
      v3: kernfs_[un]break_active_protection() separated out from
          kernfs_remove_self() and exposed as public API.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Cc: Alan Stern <stern@rowland.harvard.edu>
      Cc: kbuild test robot <fengguang.wu@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      6b0afc2a
  7. 14 1月, 2014 2 次提交
  8. 11 1月, 2014 2 次提交
    • T
      sysfs, driver-core: remove unused {sysfs|device}_schedule_callback_owner() · d1ba277e
      Tejun Heo 提交于
      All device_schedule_callback_owner() users are converted to use
      device_remove_file_self().  Remove now unused
      {sysfs|device}_schedule_callback_owner().
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      d1ba277e
    • T
      kernfs, sysfs, driver-core: implement kernfs_remove_self() and its wrappers · 1ae06819
      Tejun Heo 提交于
      Sometimes it's necessary to implement a node which wants to delete
      nodes including itself.  This isn't straightforward because of kernfs
      active reference.  While a file operation is in progress, an active
      reference is held and kernfs_remove() waits for all such references to
      drain before completing.  For a self-deleting node, this is a deadlock
      as kernfs_remove() ends up waiting for an active reference that itself
      is sitting on top of.
      
      This currently is worked around in the sysfs layer using
      sysfs_schedule_callback() which makes such removals asynchronous.
      While it works, it's rather cumbersome and inherently breaks
      synchronicity of the operation - the file operation which triggered
      the operation may complete before the removal is finished (or even
      started) and the removal may fail asynchronously.  If a removal
      operation is immmediately followed by another operation which expects
      the specific name to be available (e.g. removal followed by rename
      onto the same name), there's no way to make the latter operation
      reliable.
      
      The thing is there's no inherent reason for this to be asynchrnous.
      All that's necessary to do this synchronous is a dedicated operation
      which drops its own active ref and deactivates self.  This patch
      implements kernfs_remove_self() and its wrappers in sysfs and driver
      core.  kernfs_remove_self() is to be called from one of the file
      operations, drops the active ref and deactivates using
      __kernfs_deactivate_self(), removes the self node, and restores active
      ref to the dead node using __kernfs_reactivate_self() so that the ref
      is balanced afterwards.  __kernfs_remove() is updated so that it takes
      an early exit if the target node is already fully removed so that the
      active ref restored by kernfs_remove_self() after removal doesn't
      confuse the deactivation path.
      
      This makes implementing self-deleting nodes very easy.  The normal
      removal path doesn't even need to be changed to use
      kernfs_remove_self() for the self-deleting node.  The method can
      invoke kernfs_remove_self() on itself before proceeding the normal
      removal path.  kernfs_remove() invoked on the node by the normal
      deletion path will simply be ignored.
      
      This will replace sysfs_schedule_callback().  A subtle feature of
      sysfs_schedule_callback() is that it collapses multiple invocations -
      even if multiple removals are triggered, the removal callback is run
      only once.  An equivalent effect can be achieved by testing the return
      value of kernfs_remove_self() - only the one which gets %true return
      value should proceed with actual deletion.  All other instances of
      kernfs_remove_self() will wait till the enclosing kernfs operation
      which invoked the winning instance of kernfs_remove_self() finishes
      and then return %false.  This trivially makes all users of
      kernfs_remove_self() automatically show correct synchronous behavior
      even when there are multiple concurrent operations - all "echo 1 >
      delete" instances will finish only after the whole operation is
      completed by one of the instances.
      
      v2: For !CONFIG_SYSFS, dummy version kernfs_remove_self() was missing
          and sysfs_remove_file_self() had incorrect return type.  Fix it.
          Reported by kbuild test bot.
      
      v3: Updated to use __kernfs_{de|re}activate_self().
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Cc: Alan Stern <stern@rowland.harvard.edu>
      Cc: kbuild test robot <fengguang.wu@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      1ae06819
  9. 18 12月, 2013 1 次提交
    • T
      kernfs: mark static names with KERNFS_STATIC_NAME · 2063d608
      Tejun Heo 提交于
      Because sysfs used struct attribute which are supposed to stay
      constant, sysfs didn't copy names when creating regular files.  The
      specified string for name was supposed to stay constant.  Such
      distinction isn't inherent for kernfs.  kernfs_create_file[_ns]()
      should be able to take the same @name as kernfs_create_dir[_ns]()
      
      As there can be huge number of sysfs attributes, we still want to be
      able to use static names for sysfs attributes.  This patch renames
      kernfs_create_file_ns_key() to __kernfs_create_file() and adds
      @name_is_static parameter so that the caller can explicitly indicate
      that @name can be used without copying.  kernfs is updated to use
      KERNFS_STATIC_NAME to distinguish static and copied names.
      
      This patch doesn't introduce any behavior changes.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      2063d608
  10. 12 12月, 2013 4 次提交
    • T
      kernfs: s/sysfs/kernfs/ in constants · df23fc39
      Tejun Heo 提交于
      kernfs has just been separated out from sysfs and we're already in
      full conflict mode.  Nothing can make the situation any worse.  Let's
      take the chance to name things properly.
      
      This patch performs the following renames.
      
      * s/SYSFS_DIR/KERNFS_DIR/
      * s/SYSFS_KOBJ_ATTR/KERNFS_FILE/
      * s/SYSFS_KOBJ_LINK/KERNFS_LINK/
      * s/SYSFS_{TYPE_FLAGS}/KERNFS_{TYPE_FLAGS}/
      * s/SYSFS_FLAG_{FLAG}/KERNFS_{FLAG}/
      * s/sysfs_type()/kernfs_type()/
      * s/SD_DEACTIVATED_BIAS/KN_DEACTIVATED_BIAS/
      
      This patch is strictly rename only and doesn't introduce any
      functional difference.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      df23fc39
    • T
      kernfs: s/sysfs/kernfs/ in various data structures · c525aadd
      Tejun Heo 提交于
      kernfs has just been separated out from sysfs and we're already in
      full conflict mode.  Nothing can make the situation any worse.  Let's
      take the chance to name things properly.
      
      This patch performs the following renames.
      
      * s/sysfs_open_dirent/kernfs_open_node/
      * s/sysfs_open_file/kernfs_open_file/
      * s/sysfs_inode_attrs/kernfs_iattrs/
      * s/sysfs_addrm_cxt/kernfs_addrm_cxt/
      * s/sysfs_super_info/kernfs_super_info/
      * s/sysfs_info()/kernfs_info()/
      * s/sysfs_open_dirent_lock/kernfs_open_node_lock/
      * s/sysfs_open_file_mutex/kernfs_open_file_mutex/
      * s/sysfs_of()/kernfs_of()/
      
      This patch is strictly rename only and doesn't introduce any
      functional difference.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      c525aadd
    • T
      kernfs: drop s_ prefix from kernfs_node members · adc5e8b5
      Tejun Heo 提交于
      kernfs has just been separated out from sysfs and we're already in
      full conflict mode.  Nothing can make the situation any worse.  Let's
      take the chance to name things properly.
      
      s_ prefix for kernfs members is used inconsistently and a misnomer
      now.  It's not like kernfs_node is used widely across the kernel
      making the ability to grep for the members particularly useful.  Let's
      just drop the prefix.
      
      This patch is strictly rename only and doesn't introduce any
      functional difference.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      adc5e8b5
    • T
      kernfs: s/sysfs_dirent/kernfs_node/ and rename its friends accordingly · 324a56e1
      Tejun Heo 提交于
      kernfs has just been separated out from sysfs and we're already in
      full conflict mode.  Nothing can make the situation any worse.  Let's
      take the chance to name things properly.
      
      This patch performs the following renames.
      
      * s/sysfs_elem_dir/kernfs_elem_dir/
      * s/sysfs_elem_symlink/kernfs_elem_symlink/
      * s/sysfs_elem_attr/kernfs_elem_file/
      * s/sysfs_dirent/kernfs_node/
      * s/sd/kn/ in kernfs proper
      * s/parent_sd/parent/
      * s/target_sd/target/
      * s/dir_sd/parent/
      * s/to_sysfs_dirent()/rb_to_kn()/
      * misc renames of local vars when they conflict with the above
      
      Because md, mic and gpio dig into sysfs details, this patch ends up
      modifying them.  All are sysfs_dirent renames and trivial.  While we
      can avoid these by introducing a dummy wrapping struct sysfs_dirent
      around kernfs_node, given the limited usage outside kernfs and sysfs
      proper, I don't think such workaround is called for.
      
      This patch is strictly rename only and doesn't introduce any
      functional difference.
      
      - mic / gpio renames were missing.  Spotted by kbuild test robot.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Cc: Neil Brown <neilb@suse.de>
      Cc: Linus Walleij <linus.walleij@linaro.org>
      Cc: Ashutosh Dixit <ashutosh.dixit@intel.com>
      Cc: kbuild test robot <fengguang.wu@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      324a56e1
  11. 11 12月, 2013 1 次提交
    • T
      sysfs: bail early from kernfs_file_mmap() to avoid spurious lockdep warning · 9b2db6e1
      Tejun Heo 提交于
      This is v3.14 fix for the same issue that a8b14744 ("sysfs: give
      different locking key to regular and bin files") addresses for v3.13.
      Due to the extensive kernfs reorganization in v3.14 branch, the same
      fix couldn't be ported as-is.  The v3.13 fix was ignored while merging
      it into v3.14 branch.
      
      027a485d ("sysfs: use a separate locking class for open files
      depending on mmap") assigned different lockdep key to
      sysfs_open_file->mutex depending on whether the file implements mmap
      or not in an attempt to avoid spurious lockdep warning caused by
      merging of regular and bin file paths.
      
      While this restored some of the original behavior of using different
      locks (at least lockdep is concerned) for the different clases of
      files.  The restoration wasn't full because now the lockdep key
      assignment depends on whether the file has mmap or not instead of
      whether it's a regular file or not.
      
      This means that bin files which don't implement mmap will get assigned
      the same lockdep class as regular files.  This is problematic because
      file_operations for bin files still implements the mmap file operation
      and checking whether the sysfs file actually implements mmap happens
      in the file operation after grabbing @sysfs_open_file->mutex.  We
      still end up adding locking dependency from mmap locking to
      sysfs_open_file->mutex to the regular file mutex which triggers
      spurious circular locking warning.
      
      For v3.13, a8b14744 ("sysfs: give different locking key to regular
      and bin files") fixed it by giving sysfs_open_file->mutex different
      lockdep keys depending on whether the file is regular or bin instead
      of whether mmap exists or not; however, due to the way sysfs is now
      layered behind kernfs, this approach is no longer viable.  kernfs can
      tell whether a sysfs node has mmap implemented or not but can't tell
      whether a bin file from a regular one.
      
      This patch updates kernfs such that kernfs_file_mmap() checks
      SYSFS_FLAG_HAS_MMAP and bail before grabbing sysfs_open_file->mutex so
      that it doesn't add spurious locking dependency from mmap to
      sysfs_open_file->mutex and changes sysfs so that it specifies
      kernfs_ops->mmap iff the sysfs file implements mmap.  Combined, this
      ensures that sysfs_open_file->mutex is grabbed under mmap path iff the
      sysfs file actually implements mmap.  As sysfs_open_file->mutex is
      already given a different lockdep key if mmap is implemented, this
      removes the spurious locking dependency.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Reported-by: NDave Jones <davej@redhat.com>
      Link: http://lkml.kernel.org/g/20131203184324.GA11320@redhat.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      9b2db6e1
  12. 08 12月, 2013 1 次提交
    • T
      sysfs: give different locking key to regular and bin files · a8b14744
      Tejun Heo 提交于
      027a485d ("sysfs: use a separate locking class for open files
      depending on mmap") assigned different lockdep key to
      sysfs_open_file->mutex depending on whether the file implements mmap
      or not in an attempt to avoid spurious lockdep warning caused by
      merging of regular and bin file paths.
      
      While this restored some of the original behavior of using different
      locks (at least lockdep is concerned) for the different clases of
      files.  The restoration wasn't full because now the lockdep key
      assignment depends on whether the file has mmap or not instead of
      whether it's a regular file or not.
      
      This means that bin files which don't implement mmap will get assigned
      the same lockdep class as regular files.  This is problematic because
      file_operations for bin files still implements the mmap file operation
      and checking whether the sysfs file actually implements mmap happens
      in the file operation after grabbing @sysfs_open_file->mutex.  We
      still end up adding locking dependency from mmap locking to
      sysfs_open_file->mutex to the regular file mutex which triggers
      spurious circular locking warning.
      
      Fix it by restoring the original behavior fully by differentiating
      lockdep key by whether the file is regular or bin, instead of the
      existence of mmap.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Reported-by: NDave Jones <davej@redhat.com>
      Link: http://lkml.kernel.org/g/20131203184324.GA11320@redhat.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a8b14744
  13. 30 11月, 2013 16 次提交
    • T
      sysfs, kernfs: move file core code to fs/kernfs/file.c · 414985ae
      Tejun Heo 提交于
      Move core file code to fs/kernfs/file.c.  fs/sysfs/file.c now contains
      sysfs kernfs_ops callbacks, sysfs wrappers around kernfs interfaces,
      and sysfs_schedule_callback().  The respective declarations in
      fs/sysfs/sysfs.h are moved to fs/kernfs/kernfs-internal.h.
      
      This is pure relocation.
      
      v2: Refreshed on top of the v2 of "sysfs, kernfs: prepare read path
          for kernfs".
      
      v3: Refreshed on top of the v3 of "sysfs, kernfs: prepare read path
          for kernfs".
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      414985ae
    • T
      sysfs, kernfs: introduce kernfs[_find_and]_get() and kernfs_put() · ccf73cf3
      Tejun Heo 提交于
      Introduce kernfs interface for finding, getting and putting
      sysfs_dirents.
      
      * sysfs_find_dirent() is renamed to kernfs_find_ns() and lockdep
        assertion for sysfs_mutex is added.
      
      * sysfs_get_dirent_ns() is renamed to kernfs_find_and_get().
      
      * Macro inline dancing around __sysfs_get/put() are removed and
        kernfs_get/put() are made proper functions implemented in
        fs/sysfs/dir.c.
      
      While the conversions are mostly equivalent, there's one difference -
      kernfs_get() doesn't return the input param as its return value.  This
      change is intentional.  While passing through the input increases
      writability in some areas, it is unnecessary and has been shown to
      cause confusion regarding how the last ref is handled.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      ccf73cf3
    • T
      sysfs, kernfs: revamp sysfs_dirent active_ref lockdep annotation · 517e64f5
      Tejun Heo 提交于
      Currently, sysfs_dirent active_ref lockdep annotation uses
      attribute->[s]key as the lockdep key, which forces
      kernfs_create_file_ns() to assume that sysfs_dirent->priv is pointing
      to a struct attribute which may not be true for non-sysfs users.  This
      patch restructures the lockdep annotation such that
      
      * kernfs_ops contains lockdep_key which is used by default for files
        created kernfs_create_file_ns().
      
      * kernfs_create_file_ns_key() is introduced which takes an extra @key
        argument.  The created file will use the specified key for
        active_ref lockdep annotation.  If NULL is specified, lockdep for
        the file is disabled.
      
      * sysfs_add_file_mode_ns() is updated to use
        kernfs_create_file_ns_key() with the appropriate key from the
        attribute or NULL if ignore_lockdep is set.
      
      This makes the lockdep annotation properly contained in kernfs while
      allowing sysfs to cleanly keep its current behavior.  This patch
      doesn't introduce any behavior differences.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      517e64f5
    • T
      sysfs, kernfs: introduce kernfs_notify() · 024f6471
      Tejun Heo 提交于
      Introduce kernfs interface to wake up poll(2) which takes and returns
      sysfs_dirents.
      
      sysfs_notify_dirent() is renamed to kernfs_notify() and sysfs_notify()
      is updated so that it doesn't directly grab sysfs_mutex but acquires
      the target sysfs_dirents using sysfs_get_dirent().
      sysfs_notify_dirent() is reimplemented as a dumb inline wrapper around
      kernfs_notify().
      
      This patch doesn't introduce any behavior changes.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      024f6471
    • T
      sysfs, kernfs: add kernfs_ops->seq_{start|next|stop}() · d19b9846
      Tejun Heo 提交于
      kernfs_ops currently only supports single_open() behavior which is
      pretty restrictive.  Add optional callbacks ->seq_{start|next|stop}()
      which, when implemented, are invoked for seq_file traversal.  This
      allows full seq_file functionality for kernfs users.  This currently
      doesn't have any user and doesn't change any behavior.
      
      v2: Refreshed on top of the updated "sysfs, kernfs: prepare read path
          for kernfs".
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      d19b9846
    • T
      sysfs, kernfs: remove sysfs_add_one() · 2d0cfbec
      Tejun Heo 提交于
      sysfs_add_one() is a wrapper around __sysfs_add_one() which prints out
      duplicate name warning if __sysfs_add_one() fails with -EEXIST.  The
      previous kernfs conversions moved all dup warnings to sysfs interface
      functions and sysfs_add_one() doesn't have any user left.
      
      Remove sysfs_add_one() and update __sysfs_add_one() to take its name.
      
      This patch doesn't make any functional changes.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      2d0cfbec
    • T
      sysfs, kernfs: introduce kernfs_create_file[_ns]() · 496f7394
      Tejun Heo 提交于
      Introduce kernfs interface to create a file which takes and returns
      sysfs_dirents.
      
      The actual file creation part is separated out from
      sysfs_add_file_mode_ns() into kernfs_create_file_ns().  The former now
      only decides the kernfs_ops to use and the file's size and invokes the
      latter.
      
      This patch doesn't introduce behavior changes.
      
      v2: Dummy implementation for !CONFIG_SYSFS updated to return -ENOSYS.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      496f7394
    • T
      sysfs, kernfs: remove SYSFS_KOBJ_BIN_ATTR · a7dc66df
      Tejun Heo 提交于
      After kernfs_ops and sysfs_dirent->s_attr.size addition, the
      distinction between SYSFS_KOBJ_BIN_ATTR and SYSFS_KOBJ_ATTR is only
      necessary while creating files to decide which kernfs_ops to use.
      Afterwards, they behave exactly the same.
      
      This patch removes SYSFS_KOBJ_BIN_ATTR along with sysfs_is_bin().
      sysfs_add_file[_mode_ns]() are updated to take bool @is_bin instead of
      @type.
      
      This patch doesn't introduce any behavior changes.  This completely
      isolates the distinction between the two sysfs file types in the sysfs
      layer proper.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a7dc66df
    • T
      sysfs, kernfs: add sysfs_dirent->s_attr.size · 471bd7b7
      Tejun Heo 提交于
      sysfs sets the size of regular files unconditionally at PAGE_SIZE and
      takes the size of bin files from bin_attribute.  The latter is a
      pretty bad interface which forces bin_attribute users to create a
      separate copy of bin_attribute for each instance of the file -
      e.g. pci resource files.
      
      Add sysfs_dirent->s_attr.size so that the size can be specified
      separately.  This unifies inode init paths of ATTR and BIN_ATTR
      identical and allows for generic size handling for kernfs.
      
      Unfortunately, this grows the size of sysfs_dirent by sizeof(loff_t).
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      471bd7b7
    • T
      sysfs, kernfs: introduce kernfs_ops · f6acf8bb
      Tejun Heo 提交于
      We're in the process of separating out core sysfs functionality into
      kernfs which will deal with sysfs_dirents directly.  This patch
      introduces kernfs_ops which hosts methods kernfs users implement and
      updates fs/sysfs/file.c such that sysfs_kf_*() functions populate
      kernfs_ops and kernfs_file_*() functions call the matching entries
      from kernfs_ops.
      
      kernfs_ops contains the following groups of methods.
      
      * seq_show() - for kernfs files which use seq_file for reads.
      
      * read() - for direct read implementations.  Used iff seq_show() is
        not implemented.
      
      * write() - for writes.
      
      * mmap() - for mmaps.
      
      Notes:
      
      * sysfs_elem_attr->ops is added so that kernfs_ops can be accessed
        from sysfs_dirent.  kernfs_ops() helper is added to verify locking
        and access the field.
      
      * SYSFS_FLAG_HAS_(SEQ_SHOW|MMAP) added.  sd->s_attr->ops is accessible
        only while holding active_ref and there are cases where we want to
        take different actions depending on which ops are implemented.
        These two flags cache whether the two ops are implemented for those.
      
      * kernfs_file_*() no longer test sysfs type but chooses different
        behaviors depending on which methods in kernfs_ops are implemented.
        The conversions are trivial except for the open path.  As
        kernfs_file_open() now decides whether to allow read/write accesses
        depending on the kernfs_ops implemented, the presence of methods in
        kobjs and attribute_bin should be propagated to kernfs_ops.
        sysfs_add_file_mode_ns() is updated so that it propagates presence /
        absence of the callbacks through _empty, _ro, _wo, _rw kernfs_ops.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      f6acf8bb
    • T
      sysfs, kernfs: move sysfs_open_file to include/linux/kernfs.h · dd8a5b03
      Tejun Heo 提交于
      sysfs_open_file will be used as the primary handle for kernfs methods.
      Move its definition from fs/sysfs/file.c to include/linux/kernfs.h and
      mark the public and private fields.
      
      This is pure relocation.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      dd8a5b03
    • T
      sysfs, kernfs: prepare open, release, poll paths for kernfs · c6fb4495
      Tejun Heo 提交于
      We're in the process of separating out core sysfs functionality into
      kernfs which will deal with sysfs_dirents directly.  This patch
      prepares the rest - open, release and poll.  There isn't much to do.
      Just renaming is enough.  As sysfs_file_operations and
      sysfs_bin_operations are identical now, use the same file_operations
      for both - kernfs_file_operations.
      
      This patch doesn't introduce any behavior changes.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      c6fb4495
    • T
      sysfs, kernfs: prepare mmap path for kernfs · fdbffaa4
      Tejun Heo 提交于
      We're in the process of separating out core sysfs functionality into
      kernfs which will deal with sysfs_dirents directly.  This patch
      rearranges mmap path so that the kernfs and sysfs parts are separate.
      
      sysfs_kf_bin_mmap() which handles the interaction with bin_attribute
      mmap method is factored out of sysfs_bin_mmap(), which is renamed to
      kernfs_file_mmap().  All vma ops are renamed accordingly.
      
      sysfs_bin_mmap() is updated such that it can be used for both file
      types.  This will eventually allow using the same file_operations for
      both file types, which is necessary to separate out kernfs.
      
      This patch doesn't introduce any behavior changes.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      fdbffaa4
    • T
      sysfs, kernfs: prepare write path for kernfs · 50b38ca0
      Tejun Heo 提交于
      We're in the process of separating out core sysfs functionality into
      kernfs which will deal with sysfs_dirents directly.  This patch
      rearranges write path so that the kernfs and sysfs parts are separate.
      
      kernfs_file_write() handles all boilerplate work including buffer
      management and locking and invokes sysfs_kf_write() or
      sysfs_kf_bin_write() depending on the file type which deals with the
      interaction with kobj store or bin_attribute write method.
      
      While this patch changes the order of some operations, it shouldn't
      change any visible behavior.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      50b38ca0
    • T
      sysfs, kernfs: prepare read path for kernfs · c2b19daf
      Tejun Heo 提交于
      We're in the process of separating out core sysfs functionality into
      kernfs which will deal with sysfs_dirents directly.  This patch
      rearranges read path so that the kernfs and sysfs parts are separate.
      
      * Regular file read path is refactored such that
        kernfs_seq_start/next/stop/show() handle all the boilerplate work
        including locking and updating event count for poll, while
        sysfs_kf_seq_show() deals with interaction with kobj show method.
      
      * Bin file read path is refactored such that kernfs_file_direct_read()
        handles all the boilerplate work including buffer management and
        locking, while sysfs_kf_bin_read() deals with interaction with
        bin_attribute read method.
      
      kernfs_file_read() is added.  It invokes either the seq_file or direct
      read path depending on the file type.  This will eventually allow
      using the same file_operations for both file types, which is necessary
      to separate out kernfs.
      
      While this patch changes the order of some operations, it shouldn't
      change any visible behavior.
      
      v2: Dropped unnecessary zeroing of @count from sysfs_kf_seq_show().
          Add comments explaining single_open() behavior.  Both suggested by
          Pavel.
      
      v3: seq_stop() is called even after seq_start() failed.
          kernfs_seq_start() updated so that it doesn't unlock
          sysfs_open_file->mutex on failure so that kernfs_seq_stop()
          doesn't try to unlock an already unlocked mutex.  Reported by
          Fengguang.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Cc: Pavel Machek <pavel@ucw.cz>
      Cc: Fengguang Wu <fengguang.wu@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      c2b19daf
    • T
      sysfs, kernfs: replace sysfs_dirent->s_dir.kobj and ->s_attr.[bin_]attr with ->priv · 7c6e2d36
      Tejun Heo 提交于
      A directory sysfs_dirent points to the associated kobj.  A regular or
      bin file points to the associated [bin_]attribute.  This patch
      replaces sysfs_dirent->s_dir.kobj and ->s_attr.[bin_]attr with void *
      ->priv.
      
      This is to prepare for kernfs interface so that sysfs can specify the
      private data in the same way for directories and files.  This lower
      debuggability but not by much - the whole thing was overlaid in a
      union anyway.  If debuggability becomes an issue, we can later add
      ->priv accessors which explicitly check for the sysfs_dirent type and
      performs casting.
      
      This patch doesn't introduce any behavior difference.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      7c6e2d36
  14. 28 11月, 2013 2 次提交
  15. 24 11月, 2013 1 次提交
    • T
      sysfs: use a separate locking class for open files depending on mmap · 027a485d
      Tejun Heo 提交于
      The following two commits implemented mmap support in the regular file
      path and merged bin file support into the regular path.
      
       73d97146 ("sysfs: copy bin mmap support from fs/sysfs/bin.c to fs/sysfs/file.c")
       3124eb16 ("sysfs: merge regular and bin file handling")
      
      After the merge, the following commands trigger a spurious lockdep
      warning.  "test-mmap-read" simply mmaps the file and dumps the
      content.
      
        $ cat /sys/block/sda/trace/act_mask
        $ test-mmap-read /sys/devices/pci0000\:00/0000\:00\:03.0/resource0 4096
      
        ======================================================
        [ INFO: possible circular locking dependency detected ]
        3.12.0-work+ #378 Not tainted
        -------------------------------------------------------
        test-mmap-read/567 is trying to acquire lock:
         (&of->mutex){+.+.+.}, at: [<ffffffff8120a8df>] sysfs_bin_mmap+0x4f/0x120
      
        but task is already holding lock:
         (&mm->mmap_sem){++++++}, at: [<ffffffff8114b399>] vm_mmap_pgoff+0x49/0xa0
      
        which lock already depends on the new lock.
      
        the existing dependency chain (in reverse order) is:
      
        -> #3 (&mm->mmap_sem){++++++}:
        ...
        -> #2 (sr_mutex){+.+.+.}:
        ...
        -> #1 (&bdev->bd_mutex){+.+.+.}:
        ...
        -> #0 (&of->mutex){+.+.+.}:
        ...
      
        other info that might help us debug this:
      
        Chain exists of:
         &of->mutex --> sr_mutex --> &mm->mmap_sem
      
         Possible unsafe locking scenario:
      
      	 CPU0                    CPU1
      	 ----                    ----
          lock(&mm->mmap_sem);
      				 lock(sr_mutex);
      				 lock(&mm->mmap_sem);
          lock(&of->mutex);
      
         *** DEADLOCK ***
      
        1 lock held by test-mmap-read/567:
         #0:  (&mm->mmap_sem){++++++}, at: [<ffffffff8114b399>] vm_mmap_pgoff+0x49/0xa0
      
        stack backtrace:
        CPU: 3 PID: 567 Comm: test-mmap-read Not tainted 3.12.0-work+ #378
        Hardware name: Bochs Bochs, BIOS Bochs 01/01/2011
         ffffffff81ed41a0 ffff880009441bc8 ffffffff81611ad2 ffffffff81eccb80
         ffff880009441c08 ffffffff8160f215 ffff880009441c60 ffff880009c75208
         0000000000000000 ffff880009c751e0 ffff880009c75208 ffff880009c74ac0
        Call Trace:
         [<ffffffff81611ad2>] dump_stack+0x4e/0x7a
         [<ffffffff8160f215>] print_circular_bug+0x2b0/0x2bf
         [<ffffffff8109ca0a>] __lock_acquire+0x1a3a/0x1e60
         [<ffffffff8109d6ba>] lock_acquire+0x9a/0x1d0
         [<ffffffff81615547>] mutex_lock_nested+0x67/0x3f0
         [<ffffffff8120a8df>] sysfs_bin_mmap+0x4f/0x120
         [<ffffffff8115d363>] mmap_region+0x3b3/0x5b0
         [<ffffffff8115d8ae>] do_mmap_pgoff+0x34e/0x3d0
         [<ffffffff8114b3ba>] vm_mmap_pgoff+0x6a/0xa0
         [<ffffffff8115be3e>] SyS_mmap_pgoff+0xbe/0x250
         [<ffffffff81008282>] SyS_mmap+0x22/0x30
         [<ffffffff8161a4d2>] system_call_fastpath+0x16/0x1b
      
      This happens because one file nests sr_mutex, which nests mm->mmap_sem
      under it, under of->mutex while mmap implementation naturally nests
      of->mutex under mm->mmap_sem.  The warning is false positive as
      of->mutex is per open-file and the two paths belong to two different
      files.  This warning didn't trigger before regular and bin file
      supports were merged because only bin file supported mmap and the
      other side of locking happened only on regular files which used
      equivalent but separate locking.
      
      It'd be best if we give separate locking classes per file but we can't
      easily do that.  Let's differentiate on ->mmap() for now.  Later we'll
      add explicit file operations struct and can add per-ops lockdep key
      there.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Reported-by: NDave Jones <davej@redhat.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      027a485d
  16. 02 11月, 2013 1 次提交
    • T
      sysfs: use generic_file_llseek() for sysfs_file_operations · 044e3bc3
      Tejun Heo 提交于
      13c589d5 ("sysfs: use seq_file when reading regular files")
      converted regular sysfs files to use seq_file.  The commit substituted
      generic_file_llseek() with seq_lseek() for llseek implementation.
      
      Before the change, all regular sysfs files were allowed to seek to any
      position in [0, PAGE_SIZE] as the file size is always PAGE_SIZE and
      generic_file_llseek() allows any seeking inside the range under file
      size; however, seq_lseek()'s behavior is different.  It traverses the
      output by repeatedly invoking ->show() until it reaches the target
      offset or traversal indicates EOF.  As seq_files are fully dynamic and
      may not end at all, it doesn't support seeking from the end
      (SEEK_END).
      
      Apparently, there are userland tools which uses SEEK_END to discover
      the buffer size to use and the switch to seq_lseek() disturbs them as
      SEEK_END fails with -EINVAL.
      
      The only benefits of using seq_lseek() instead of
      generic_file_llseek() are
      
      * Early failure.  If traversing to certain file position should fail,
        seq_lseek() will report such failures on lseek(2) instead of the
        following read/write operations.
      
      * EOF detection.  While SEEK_END is not supported, SEEK_SET/CUR +
        large offset can be used to detect eof - eof at the time of the seek
        anyway as the file size may change dynamically.
      
      Both aren't necessary for sysfs or prospect kernfs users.  Revert to
      genefic_file_llseek() and preserve the original behavior.
      Signed-off-by: NTejun Heo <tj@kernel.org>
      Reported-by: NHeiko Carstens <heiko.carstens@de.ibm.com>
      Link: https://lkml.kernel.org/r/20131031114358.GA5551@osirisTested-by: NHeiko Carstens <heiko.carstens@de.ibm.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      044e3bc3