1. 28 8月, 2019 2 次提交
  2. 01 8月, 2019 3 次提交
    • S
      driver core: Add sync_state driver/bus callback · 8f8184d6
      Saravana Kannan 提交于
      This sync_state driver/bus callback is called once all the consumers
      of a supplier have probed successfully.
      
      This allows the supplier device's driver/bus to sync the supplier
      device's state to the software state with the guarantee that all the
      consumers are actively managing the resources provided by the supplier
      device.
      
      To maintain backwards compatibility and ease transition from existing
      frameworks and resource cleanup schemes, late_initcall_sync is the
      earliest when the sync_state callback might be called.
      
      There is no upper bound on the time by which the sync_state callback
      has to be called. This is because if a consumer device never probes,
      the supplier has to maintain its resources in the state left by the
      bootloader. For example, if the bootloader leaves the display
      backlight at a fixed voltage and the backlight driver is never probed,
      you don't want the backlight to ever be turned off after boot up.
      
      Also, when multiple devices are added after kernel init, some
      suppliers could be added before their consumer devices get added. In
      these instances, the supplier devices could get their sync_state
      callback called right after they probe because the consumers devices
      haven't had a chance to create device links to the suppliers.
      
      To handle this correctly, this change also provides APIs to
      pause/resume sync state callbacks so that when multiple devices are
      added, their sync_state callback evaluation can be postponed to happen
      after all of them are added.
      
      kbuild test robot reported missing documentation for device.state_synced
      Reported-by: Nkbuild test robot <lkp@intel.com>
      Signed-off-by: NSaravana Kannan <saravanak@google.com>
      Link: https://lore.kernel.org/r/20190731221721.187713-5-saravanak@google.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      8f8184d6
    • S
      driver core: Add edit_links() callback for drivers · 134b23ee
      Saravana Kannan 提交于
      The driver core/bus adding supplier-consumer dependencies by default
      enables functional dependencies to be tracked correctly even when the
      consumer devices haven't had their drivers registered or loaded (if they
      are modules).
      
      However, when the bus incorrectly adds dependencies that it shouldn't
      have added, the devices might never probe.
      
      For example, if device-C is a consumer of device-S and they have
      phandles to each other in DT, the following could happen:
      
      1.  Device-S get added first.
      2.  The bus add_links() callback will (incorrectly) try to link it as
          a consumer of device-C.
      3.  Since device-C isn't present, device-S will be put in
          "waiting-for-supplier" list.
      4.  Device-C gets added next.
      5.  All devices in "waiting-for-supplier" list are retried for linking.
      6.  Device-S gets linked as consumer to Device-C.
      7.  The bus add_links() callback will (correctly) try to link it as
          a consumer of device-S.
      8.  This isn't allowed because it would create a cyclic device links.
      
      Neither devices will get probed since the supplier is marked as
      dependent on the consumer. And the consumer will never probe because the
      consumer can't get resources from the supplier.
      
      Without this patch, things stay in this broken state. However, with this
      patch, the execution will continue like this:
      
      9.  Device-C's driver is loaded.
      10. Device-C's driver removes Device-S as a consumer of Device-C.
      11. Device-C's driver adds Device-C as a consumer of Device-S.
      12. Device-S probes.
      14. Device-C probes.
      
      kbuild test robot reported missing documentation for device.has_edit_links
      Reported-by: Nkbuild test robot <lkp@intel.com>
      Signed-off-by: NSaravana Kannan <saravanak@google.com>
      Link: https://lore.kernel.org/r/20190731221721.187713-3-saravanak@google.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      134b23ee
    • S
      driver core: Add support for linking devices during device addition · 5302dd7d
      Saravana Kannan 提交于
      When devices are added, the bus might want to create device links to track
      functional dependencies between supplier and consumer devices. This
      tracking of supplier-consumer relationship allows optimizing device probe
      order and tracking whether all consumers of a supplier are active. The
      add_links bus callback is added to support this.
      
      However, when consumer devices are added, they might not have a supplier
      device to link to despite needing mandatory resources/functionality from
      one or more suppliers. A waiting_for_suppliers list is created to track
      such consumers and retry linking them when new devices get added.
      Signed-off-by: NSaravana Kannan <saravanak@google.com>
      Link: https://lore.kernel.org/r/20190731221721.187713-2-saravanak@google.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      5302dd7d
  3. 31 7月, 2019 1 次提交
    • M
      driver core: Fix use-after-free and double free on glue directory · ac43432c
      Muchun Song 提交于
      There is a race condition between removing glue directory and adding a new
      device under the glue dir. It can be reproduced in following test:
      
      CPU1:                                         CPU2:
      
      device_add()
        get_device_parent()
          class_dir_create_and_add()
            kobject_add_internal()
              create_dir()    // create glue_dir
      
                                                    device_add()
                                                      get_device_parent()
                                                        kobject_get() // get glue_dir
      
      device_del()
        cleanup_glue_dir()
          kobject_del(glue_dir)
      
                                                      kobject_add()
                                                        kobject_add_internal()
                                                          create_dir() // in glue_dir
                                                            sysfs_create_dir_ns()
                                                              kernfs_create_dir_ns(sd)
      
            sysfs_remove_dir() // glue_dir->sd=NULL
            sysfs_put()        // free glue_dir->sd
      
                                                                // sd is freed
                                                                kernfs_new_node(sd)
                                                                  kernfs_get(glue_dir)
                                                                  kernfs_add_one()
                                                                  kernfs_put()
      
      Before CPU1 remove last child device under glue dir, if CPU2 add a new
      device under glue dir, the glue_dir kobject reference count will be
      increase to 2 via kobject_get() in get_device_parent(). And CPU2 has
      been called kernfs_create_dir_ns(), but not call kernfs_new_node().
      Meanwhile, CPU1 call sysfs_remove_dir() and sysfs_put(). This result in
      glue_dir->sd is freed and it's reference count will be 0. Then CPU2 call
      kernfs_get(glue_dir) will trigger a warning in kernfs_get() and increase
      it's reference count to 1. Because glue_dir->sd is freed by CPU1, the next
      call kernfs_add_one() by CPU2 will fail(This is also use-after-free)
      and call kernfs_put() to decrease reference count. Because the reference
      count is decremented to 0, it will also call kmem_cache_free() to free
      the glue_dir->sd again. This will result in double free.
      
      In order to avoid this happening, we also should make sure that kernfs_node
      for glue_dir is released in CPU1 only when refcount for glue_dir kobj is
      1 to fix this race.
      
      The following calltrace is captured in kernel 4.14 with the following patch
      applied:
      
      commit 726e4109 ("drivers: core: Remove glue dirs from sysfs earlier")
      
      --------------------------------------------------------------------------
      [    3.633703] WARNING: CPU: 4 PID: 513 at .../fs/kernfs/dir.c:494
                      Here is WARN_ON(!atomic_read(&kn->count) in kernfs_get().
      ....
      [    3.633986] Call trace:
      [    3.633991]  kernfs_create_dir_ns+0xa8/0xb0
      [    3.633994]  sysfs_create_dir_ns+0x54/0xe8
      [    3.634001]  kobject_add_internal+0x22c/0x3f0
      [    3.634005]  kobject_add+0xe4/0x118
      [    3.634011]  device_add+0x200/0x870
      [    3.634017]  _request_firmware+0x958/0xc38
      [    3.634020]  request_firmware_into_buf+0x4c/0x70
      ....
      [    3.634064] kernel BUG at .../mm/slub.c:294!
                      Here is BUG_ON(object == fp) in set_freepointer().
      ....
      [    3.634346] Call trace:
      [    3.634351]  kmem_cache_free+0x504/0x6b8
      [    3.634355]  kernfs_put+0x14c/0x1d8
      [    3.634359]  kernfs_create_dir_ns+0x88/0xb0
      [    3.634362]  sysfs_create_dir_ns+0x54/0xe8
      [    3.634366]  kobject_add_internal+0x22c/0x3f0
      [    3.634370]  kobject_add+0xe4/0x118
      [    3.634374]  device_add+0x200/0x870
      [    3.634378]  _request_firmware+0x958/0xc38
      [    3.634381]  request_firmware_into_buf+0x4c/0x70
      --------------------------------------------------------------------------
      
      Fixes: 726e4109 ("drivers: core: Remove glue dirs from sysfs earlier")
      Signed-off-by: NMuchun Song <smuchun@gmail.com>
      Reviewed-by: NMukesh Ojha <mojha@codeaurora.org>
      Signed-off-by: NPrateek Sood <prsood@codeaurora.org>
      Link: https://lore.kernel.org/r/20190727032122.24639-1-smuchun@gmail.comSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      ac43432c
  4. 30 7月, 2019 6 次提交
  5. 25 7月, 2019 1 次提交
    • R
      driver core: Remove device link creation limitation · 515db266
      Rafael J. Wysocki 提交于
      If device_link_add() is called for a consumer/supplier pair with an
      existing device link between them and the existing link's type is
      not in agreement with the flags passed to that function by its
      caller, NULL will be returned.  That is seriously inconvenient,
      because it forces the callers of device_link_add() to worry about
      what others may or may not do even if that is not relevant to them
      for any other reasons.
      
      It turns out, however, that this limitation can be made go away
      relatively easily.
      
      The underlying observation is that if DL_FLAG_STATELESS has been
      passed to device_link_add() in flags for the given consumer/supplier
      pair at least once, calling either device_link_del() or
      device_link_remove() to release the link returned by it should work,
      but there are no other requirements associated with that flag.  In
      turn, if at least one of the callers of device_link_add() for the
      given consumer/supplier pair has not passed DL_FLAG_STATELESS to it
      in flags, the driver core should track the status of the link and act
      on it as appropriate (ie. the link should be treated as "managed").
      This means that DL_FLAG_STATELESS needs to be set for managed device
      links and it should be valid to call device_link_del() or
      device_link_remove() to drop references to them in certain
      sutiations.
      
      To allow that to happen, introduce a new (internal) device link flag
      called DL_FLAG_MANAGED and make device_link_add() set it automatically
      whenever DL_FLAG_STATELESS is not passed to it.  Also make it take
      additional references to existing device links that were previously
      stateless (that is, with DL_FLAG_STATELESS set and DL_FLAG_MANAGED
      unset) and will need to be managed going forward and initialize
      their status (which has been DL_STATE_NONE so far).
      
      Accordingly, when a managed device link is dropped automatically
      by the driver core, make it clear DL_FLAG_MANAGED, reset the link's
      status back to DL_STATE_NONE and drop the reference to it associated
      with DL_FLAG_MANAGED instead of just deleting it right away (to
      allow it to stay around in case it still needs to be released
      explicitly by someone).
      
      With that, since setting DL_FLAG_STATELESS doesn't mean that the
      device link in question is not managed any more, replace all of the
      status-tracking checks against DL_FLAG_STATELESS with analogous
      checks against DL_FLAG_MANAGED and update the documentation to
      reflect these changes.
      
      While at it, make device_link_add() reject flags that it does not
      recognize, including DL_FLAG_MANAGED.
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Reviewed-by: NSaravana Kannan <saravanak@google.com>
      Tested-by: NMarek Szyprowski <m.szyprowski@samsung.com>
      Review-by: NSaravana Kannan <saravanak@google.com>
      Link: https://lore.kernel.org/r/2305283.AStDPdUUnE@kreacherSigned-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      515db266
  6. 19 7月, 2019 2 次提交
    • D
      driver-core, libnvdimm: Let device subsystems add local lockdep coverage · 87a30e1f
      Dan Williams 提交于
      For good reason, the standard device_lock() is marked
      lockdep_set_novalidate_class() because there is simply no sane way to
      describe the myriad ways the device_lock() ordered with other locks.
      However, that leaves subsystems that know their own local device_lock()
      ordering rules to find lock ordering mistakes manually. Instead,
      introduce an optional / additional lockdep-enabled lock that a subsystem
      can acquire in all the same paths that the device_lock() is acquired.
      
      A conversion of the NFIT driver and NVDIMM subsystem to a
      lockdep-validate device_lock() scheme is included. The
      debug_nvdimm_lock() implementation implements the correct lock-class and
      stacking order for the libnvdimm device topology hierarchy.
      
      Yes, this is a hack, but hopefully it is a useful hack for other
      subsystems device_lock() debug sessions. Quoting Greg:
      
          "Yeah, it feels a bit hacky but it's really up to a subsystem to mess up
           using it as much as anything else, so user beware :)
      
           I don't object to it if it makes things easier for you to debug."
      
      Cc: Ingo Molnar <mingo@redhat.com>
      Cc: Ira Weiny <ira.weiny@intel.com>
      Cc: Will Deacon <will.deacon@arm.com>
      Cc: Dave Jiang <dave.jiang@intel.com>
      Cc: Keith Busch <keith.busch@intel.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Vishal Verma <vishal.l.verma@intel.com>
      Cc: "Rafael J. Wysocki" <rjw@rjwysocki.net>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Signed-off-by: NDan Williams <dan.j.williams@intel.com>
      Acked-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      Reviewed-by: NIra Weiny <ira.weiny@intel.com>
      Link: https://lore.kernel.org/r/156341210661.292348.7014034644265455704.stgit@dwillia2-desk3.amr.corp.intel.com
      87a30e1f
    • D
      drivers/base: Introduce kill_device() · 00289cd8
      Dan Williams 提交于
      The libnvdimm subsystem arranges for devices to be destroyed as a result
      of a sysfs operation. Since device_unregister() cannot be called from
      an actively running sysfs attribute of the same device libnvdimm
      arranges for device_unregister() to be performed in an out-of-line async
      context.
      
      The driver core maintains a 'dead' state for coordinating its own racing
      async registration / de-registration requests. Rather than add local
      'dead' state tracking infrastructure to libnvdimm device objects, export
      the existing state tracking via a new kill_device() helper.
      
      The kill_device() helper simply marks the device as dead, i.e. that it
      is on its way to device_del(), or returns that the device was already
      dead. This can be used in advance of calling device_unregister() for
      subsystems like libnvdimm that might need to handle multiple user
      threads racing to delete a device.
      
      This refactoring does not change any behavior, but it is a pre-requisite
      for follow-on fixes and therefore marked for -stable.
      
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: "Rafael J. Wysocki" <rafael@kernel.org>
      Fixes: 4d88a97a ("libnvdimm, nvdimm: dimm driver and base libnvdimm device-driver...")
      Cc: <stable@vger.kernel.org>
      Tested-by: NJane Chu <jane.chu@oracle.com>
      Reviewed-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      Link: https://lore.kernel.org/r/156341207332.292348.14959761496009347574.stgit@dwillia2-desk3.amr.corp.intel.comSigned-off-by: NDan Williams <dan.j.williams@intel.com>
      00289cd8
  7. 24 6月, 2019 1 次提交
    • S
      drivers: Add generic helper to match by of_node · 65b66682
      Suzuki K Poulose 提交于
      Add a helper to match device by the of_node. This will be later used
      to provide wrappers to the device iterators for {bus/class/driver}_find_device().
      Convert other users to reuse this new helper.
      
      Cc: Alan Tull <atull@kernel.org>
      Cc: Andrew Lunn <andrew@lunn.ch>
      Cc: Daniel Vetter <daniel@ffwll.ch>
      Cc: David Airlie <airlied@linux.ie>
      Cc: "David S. Miller" <davem@davemloft.net>
      Cc: devicetree@vger.kernel.org
      Cc: dri-devel@lists.freedesktop.org
      Cc: Florian Fainelli <f.fainelli@gmail.com>
      Cc: Frank Rowand <frowand.list@gmail.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Heiner Kallweit <hkallweit1@gmail.com>
      Cc: Jiri Slaby <jslaby@suse.com>
      Cc: Jonathan Hunter <jonathanh@nvidia.com>
      Cc: Lee Jones <lee.jones@linaro.org>
      Cc: Liam Girdwood <lgirdwood@gmail.com>
      Cc: linux-fpga@vger.kernel.org
      Cc: linux-i2c@vger.kernel.org
      Cc: linux-spi@vger.kernel.org
      Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>
      Cc: Mark Brown <broonie@kernel.org>
      Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
      Cc: Maxime Ripard <maxime.ripard@bootlin.com>
      Cc: Moritz Fischer <mdf@kernel.org>
      Cc: Peter Rosin <peda@axentia.se>
      Cc: Rob Herring <robh+dt@kernel.org>
      Cc: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
      Cc: Thierry Reding <thierry.reding@gmail.com>
      Cc: Thor Thayer <thor.thayer@linux.intel.com>
      Cc: Wolfram Sang <wsa@the-dreams.de>
      Cc: "Rafael J. Wysocki" <rafael@kernel.org>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Ulf Hansson <ulf.hansson@linaro.org>
      Cc: Joe Perches <joe@perches.com>
      Signed-off-by: NSuzuki K Poulose <suzuki.poulose@arm.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      65b66682
  8. 03 6月, 2019 1 次提交
  9. 26 4月, 2019 1 次提交
  10. 20 2月, 2019 1 次提交
    • R
      driver core: Fix PM-runtime for links added during consumer probe · 36003d4c
      Rafael J. Wysocki 提交于
      Commit 4c06c4e6 ("driver core: Fix possible supplier PM-usage
      counter imbalance") introduced a regression that causes suppliers
      to be suspended prematurely for device links added during consumer
      driver probe if the initial PM-runtime status of the consumer is
      "suspended" and the consumer is resumed after adding the link and
      before pm_runtime_put_suppliers() is called.  In that case,
      pm_runtime_put_suppliers() will drop the rpm_active refcount for
      the link by one and (since rpm_active is equal to two after the
      preceding consumer resume) the supplier's PM-runtime usage counter
      will be decremented, which may cause the supplier to suspend even
      though the consumer's PM-runtime status is "active".
      
      For this reason, partially revert commit 4c06c4e6 as the problem
      it tried to fix needs to be addressed somewhat differently, and
      change pm_runtime_get_suppliers() and pm_runtime_put_suppliers() so
      that the latter only drops rpm_active references acquired by the
      former.  [This requires adding a new field to struct device_link,
      but I coulnd't find a cleaner way to address the issue that would
      work in all cases.]
      
      This causes pm_runtime_put_suppliers() to effectively ignore device
      links added during consumer probe, so device_link_add() doesn't need
      to worry about ensuring that suppliers will remain active after
      pm_runtime_put_suppliers() for links created with DL_FLAG_RPM_ACTIVE
      set and it only needs to bump up rpm_active by one for those links,
      so pm_runtime_active_link() is not necessary any more.
      
      Fixes: 4c06c4e6 ("driver core: Fix possible supplier PM-usage counter imbalance")
      Reported-by: NJon Hunter <jonathanh@nvidia.com>
      Tested-by: NJon Hunter <jonathanh@nvidia.com>
      Tested-by: NUlf Hansson <ulf.hansson@linaro.org>
      Reviewed-by: NUlf Hansson <ulf.hansson@linaro.org>
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Tested-by: NThierry Reding <treding@nvidia.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      36003d4c
  11. 13 2月, 2019 1 次提交
    • R
      driver core: Fix possible supplier PM-usage counter imbalance · 4c06c4e6
      Rafael J. Wysocki 提交于
      If a stateless device link to a certain supplier with
      DL_FLAG_PM_RUNTIME set in the flags is added and then removed by the
      consumer driver's probe callback, the supplier's PM-runtime usage
      counter will be nonzero after that which effectively causes the
      supplier to remain "always on" going forward.
      
      Namely, device_link_add() called to add the link invokes
      device_link_rpm_prepare() which notices that the consumer driver is
      probing, so it increments the supplier's PM-runtime usage counter
      with the assumption that the link will stay around until
      pm_runtime_put_suppliers() is called by driver_probe_device(),
      but if the link goes away before that point, the supplier's
      PM-runtime usage counter will remain nonzero.
      
      To prevent that from happening, first rework pm_runtime_get_suppliers()
      and pm_runtime_put_suppliers() to use the rpm_active refounts of device
      links and make the latter only drop rpm_active and the supplier's
      PM-runtime usage counter for each link by one, unless rpm_active is
      one already for it.  Next, modify device_link_add() to bump up the
      new link's rpm_active refcount and the suppliers PM-runtime usage
      counter by two, to prevent pm_runtime_put_suppliers(), if it is
      called subsequently, from suspending the supplier prematurely (in
      case its PM-runtime usage counter goes down to 0 in there).
      
      Due to the way rpm_put_suppliers() works, this change does not
      affect runtime suspend of the consumer ends of new device links (or,
      generally, device links for which DL_FLAG_PM_RUNTIME has just been
      set).
      
      Fixes: e2f3cd83 ("driver core: Fix handling of runtime PM flags in device_link_add()")
      Reported-by: NUlf Hansson <ulf.hansson@linaro.org>
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Reviewed-by: NUlf Hansson <ulf.hansson@linaro.org>
      Tested-by: NUlf Hansson <ulf.hansson@linaro.org>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      4c06c4e6
  12. 01 2月, 2019 8 次提交
    • R
      driver core: Add device link flag DL_FLAG_AUTOPROBE_CONSUMER · e7dd4010
      Rafael J. Wysocki 提交于
      Add a new device link flag, DL_FLAG_AUTOPROBE_CONSUMER, to request the
      driver core to probe for a consumer driver automatically after binding
      a driver to the supplier device on a persistent managed device link.
      
      As unbinding the supplier driver on a managed device link causes the
      consumer driver to be detached from its device automatically, this
      flag provides a complementary mechanism which is needed to address
      some "composite device" use cases.
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      e7dd4010
    • R
      driver core: Make driver core own stateful device links · 72175d4e
      Rafael J. Wysocki 提交于
      Even though stateful device links are managed by the driver core in
      principle, their creators are allowed and sometimes even expected
      to drop references to them via device_link_del() or
      device_link_remove(), but that doesn't really play well with the
      "persistent" link concept.
      
      If "persistent" managed device links are created from driver
      probe callbacks, device_link_add() called to do that will take a
      new reference on the link each time the callback runs and those
      references will never be dropped, which kind of isn't nice.
      
      This issues arises because of the link reference counting carried
      out by device_link_add() for existing links, but that is only done to
      avoid deleting device links that may still be necessary, which
      shouldn't be a concern for managed (stateful) links.  These device
      links are managed by the driver core and whoever creates one of them
      will need it at least as long as until the consumer driver is detached
      from its device and deleting it may be left to the driver core just
      fine.
      
      For this reason, rework device_link_add() to apply the reference
      counting to stateless links only and make device_link_del() and
      device_link_remove() drop references to stateless links only too.
      After this change, if called to add a stateful device link for
      a consumer-supplier pair for which a stateful device link is
      present already, device_link_add() will return the existing link
      without incrementing its reference counter.  Accordingly,
      device_link_del() and device_link_remove() will WARN() and do
      nothing when called to drop a reference to a stateful link.  Thus,
      effectively, all stateful device links will be owned by the driver
      core.
      
      In addition, clean up the handling of the link management flags,
      DL_FLAG_AUTOREMOVE_CONSUMER and DL_FLAG_AUTOREMOVE_SUPPLIER, so that
      (a) they are never set at the same time and (b) if device_link_add()
      is called for a consumer-supplier pair with an existing stateful link
      between them, the flags of that link will be combined with the flags
      passed to device_link_add() to ensure that the life time of the link
      is sufficient for all of the callers of device_link_add() for the
      same consumer-supplier pair.
      
      Update the device_link_add() kerneldoc comment to reflect the
      above changes.
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      72175d4e
    • R
      driver core: Do not call rpm_put_suppliers() in pm_runtime_drop_link() · a1fdbfbb
      Rafael J. Wysocki 提交于
      Calling rpm_put_suppliers() from pm_runtime_drop_link() is excessive
      as it affects all suppliers of the consumer device and not just the
      one pointed to by the device link being dropped.  Worst case it may
      cause the consumer device to stop working unexpectedly.  Moreover, in
      principle it is racy with respect to runtime PM of the consumer
      device.
      
      To avoid these problems drop runtime PM references on the particular
      supplier pointed to by the link in question only and do that after
      the link has been dropped from the consumer device's list of links to
      suppliers, which is in device_link_free().
      
      Fixes: a0504aec ("PM / runtime: Drop usage count for suppliers at device link removal")
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      a1fdbfbb
    • R
      driver core: Fix adding device links to probing suppliers · 15cfb094
      Rafael J. Wysocki 提交于
      Currently, it is not valid to add a device link from a consumer
      driver ->probe callback to a supplier that is still probing too, but
      generally this is a valid use case.  For example, if the consumer has
      just acquired a resource that can only be available if the supplier
      is functional, adding a device link to that supplier right away
      should be safe (and even desirable arguably), but device_link_add()
      doesn't handle that case correctly and the initial state of the link
      created by it is wrong then.
      
      To address this problem, change the initial state of device links
      added between a probing supplier and a probing consumer to
      DL_STATE_CONSUMER_PROBE and update device_links_driver_bound() to
      skip such links on the supplier side.
      
      With this change, if the supplier probe completes first,
      device_links_driver_bound() called for it will skip the link state
      update and when it is called for the consumer, the link state will
      be updated to "active".  In turn, if the consumer probe completes
      first, device_links_driver_bound() called for it will change the
      state of the link to "active" and when it is called for the
      supplier, the link status update will be skipped.
      
      However, in principle the supplier or consumer probe may still fail
      after the link has been added, so modify device_links_no_driver() to
      change device links in the "active" or "consumer probe" state to
      "dormant" on the supplier side and update __device_links_no_driver()
      to change the link state to "available" only if it is "consumer
      probe" or "active".
      
      Then, if the supplier probe fails first, the leftover link to the
      probing consumer will become "dormant" and device_links_no_driver()
      called for the consumer (when its probe fails) will clean it up.
      In turn, if the consumer probe fails first, it will either drop the
      link, or change its state to "available" and, in the latter case,
      when device_links_no_driver() is called for the supplier, it will
      update the link state to "dormant".  [If the supplier probe fails,
      but the consumer probe succeeds, which should not happen as long as
      the consumer driver is correct, the link still will be around, but
      it will be "dormant" until the supplier is probed again.]
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      15cfb094
    • R
      driver core: Fix handling of runtime PM flags in device_link_add() · e2f3cd83
      Rafael J. Wysocki 提交于
      After commit ead18c23 ("driver core: Introduce device links
      reference counting"), if there is a link between the given supplier
      and the given consumer already, device_link_add() will refcount it
      and return it unconditionally without updating its flags.  It is
      possible, however, that the second (or any subsequent) caller of
      device_link_add() for the same consumer-supplier pair will pass
      DL_FLAG_PM_RUNTIME, possibly along with DL_FLAG_RPM_ACTIVE, in flags
      to it and the existing link may not behave as expected then.
      
      First, if DL_FLAG_PM_RUNTIME is not set in the existing link's flags
      at all, it needs to be set like during the original initialization of
      the link.
      
      Second, if DL_FLAG_RPM_ACTIVE is passed to device_link_add() in flags
      (in addition to DL_FLAG_PM_RUNTIME), the existing link should to be
      updated to reflect the "active" runtime PM configuration of the
      consumer-supplier pair and extra care must be taken here to avoid
      possible destructive races with runtime PM of the consumer.
      
      To that end, redefine the rpm_active field in struct device_link
      as a refcount, initialize it to 1 and make rpm_resume() (for the
      consumer) and device_link_add() increment it whenever they acquire
      a runtime PM reference on the supplier device.  Accordingly, make
      rpm_suspend() (for the consumer) and pm_runtime_clean_up_links()
      decrement it and drop runtime PM references to the supplier
      device in a loop until rpm_active becones 1 again.
      
      Fixes: ead18c23 ("driver core: Introduce device links reference counting")
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      e2f3cd83
    • R
      driver core: Do not resume suppliers under device_links_write_lock() · 5db25c9e
      Rafael J. Wysocki 提交于
      It is incorrect to call pm_runtime_get_sync() under
      device_links_write_lock(), because it may end up trying to take
      device_links_read_lock() while resuming the target device and that
      will deadlock in the non-SRCU case, so avoid that by resuming the
      supplier device in device_link_add() before calling
      device_links_write_lock().
      
      Fixes: 21d5c57b ("PM / runtime: Use device links")
      Fixes: baa8809f ("PM / runtime: Optimize the use of device links")
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      5db25c9e
    • R
      driver core: Avoid careless re-use of existing device links · f265df55
      Rafael J. Wysocki 提交于
      After commit ead18c23 ("driver core: Introduce device links
      reference counting"), if there is a link between the given supplier
      and the given consumer already, device_link_add() will refcount it
      and return it unconditionally.  However, if the flags passed to
      it on the second (or any subsequent) attempt to create a device
      link between the same consumer-supplier pair are not compatible with
      the existing link's flags, that is incorrect.
      
      First off, if the existing link is stateless and the next caller of
      device_link_add() for the same consumer-supplier pair wants a
      stateful one, or the other way around, the existing link cannot be
      returned, because it will not match the expected behavior, so make
      device_link_add() dump the stack and return NULL in that case.
      
      Moreover, if the DL_FLAG_AUTOREMOVE_CONSUMER flag is passed to
      device_link_add(), its caller will expect its reference to the link
      to be dropped automatically on consumer driver removal, which will
      not happen if that flag is not set in the link's flags (and
      analogously for DL_FLAG_AUTOREMOVE_SUPPLIER).  For this reason, make
      device_link_add() update the existing link's flags accordingly
      before returning it to the caller.
      
      Fixes: ead18c23 ("driver core: Introduce device links reference counting")
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      f265df55
    • R
      driver core: Fix DL_FLAG_AUTOREMOVE_SUPPLIER device link flag handling · c8d50986
      Rafael J. Wysocki 提交于
      Change the list walk in device_links_driver_cleanup() to a safe one
      to avoid use-after-free when dropping a link from the list during the
      walk.
      
      Also, while at it, fix device_link_add() to refuse to create
      stateless device links with DL_FLAG_AUTOREMOVE_SUPPLIER set, which is
      an invalid combination (setting that flag means that the driver core
      should manage the link, so it cannot be stateless), and extend the
      kerneldoc comment of device_link_add() to cover the
      DL_FLAG_AUTOREMOVE_SUPPLIER flag properly too.
      
      Fixes: 1689cac5 ("driver core: Add flag to autoremove device link on supplier unbind")
      Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      c8d50986
  13. 31 1月, 2019 1 次提交
    • A
      driver core: Establish order of operations for device_add and device_del via bitflag · 3451a495
      Alexander Duyck 提交于
      Add an additional bit flag to the device_private struct named "dead".
      
      This additional flag provides a guarantee that when a device_del is
      executed on a given interface an async worker will not attempt to attach
      the driver following the earlier device_del call. Previously this
      guarantee was not present and could result in the device_del call
      attempting to remove a driver from an interface only to have the async
      worker attempt to probe the driver later when it finally completes the
      asynchronous probe call.
      
      One additional change added was that I pulled the check for dev->driver
      out of the __device_attach_driver call and instead placed it in the
      __device_attach_async_helper call. This was motivated by the fact that the
      only other caller of this, __device_attach, had already taken the
      device_lock() and checked for dev->driver. Instead of testing for this
      twice in this path it makes more sense to just consolidate the dev->dead
      and dev->driver checks together into one set of checks.
      Reviewed-by: NDan Williams <dan.j.williams@intel.com>
      Reviewed-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Signed-off-by: NAlexander Duyck <alexander.h.duyck@linux.intel.com>
      Signed-off-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      3451a495
  14. 22 1月, 2019 2 次提交
  15. 18 1月, 2019 1 次提交
  16. 06 12月, 2018 2 次提交
  17. 27 11月, 2018 4 次提交
  18. 21 7月, 2018 2 次提交