mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/4] software node: fix lookup of swnode in release path
@ 2026-11-06  4:38 Peng Fan (OSS)
  2026-11-06  4:38 ` [PATCH v2 1/4] software node: do not resurrect swnode being released Peng Fan (OSS)
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Peng Fan (OSS) @ 2026-11-06  4:38 UTC (permalink / raw)
  To: Andy Shevchenko, Daniel Scally, Heikki Krogerus, Sakari Ailus,
	Bartosz Golaszewski, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, Johan Hovold
  Cc: Rafael J. Wysocki, linux-acpi, driver-core, linux-kernel, imx, Peng Fan

This series fixes a software node lifetime bug where a lookup can find an
swnode whose kobject refcount has already reached zero.

The kset list keeps an swnode linked until kobject_cleanup() calls
kobj_kset_leave(), which happens after the last kobject_put() has already
dropped the refcount to zero. A lookup that does not check whether the
object is still referenceable can therefore call kobject_get() on a dead
object, triggering:

  refcount_t: addition on 0; use-after-free
  refcount_t: saturated; leaking memory
  refcount_t: underflow; use-after-free

The problem is easily reproduced on i.MX95 with dwc3-imx when
dwc3_core_probe() returns -EPROBE_DEFER. dwc3_imx_probe() removes its
static software node from the device and the deferred probe retry
immediately adds the same node again. With
CONFIG_DEBUG_KOBJECT_RELEASE the old swnode stays on the kset list for
some time after its refcount reaches zero, so the retry reliably finds
it.

The first patch fixes the pointer-based kset lookup by using
kobject_get_unless_zero() and skipping entries that are already in their
release path. It also makes registration return the newly created swnode
reference so that device_add_software_node() does not have to look the
node up again after registering it. Reference-owning callers get a
reference from the lookup, while software_node_fwnode() keeps its
borrowed-pointer semantics by dropping the temporary lookup reference.

The remaining patches harden related weak lookups for the same class of
problem. The series is organized as follows:

  1. Fix the pointer-based kset lookup and avoid the second lookup after
     registration.
  2. Harden software_node_find_by_name() against dying entries.
  3. Check the result of software_node_fwnode() in the graph
     remote-endpoint path.
  4. Skip dying children when walking the parent's children list.

Immediate re-registration of named software nodes, or parent/child groups,
can still fail with `-EEXIST` while the previous release is pending.
This series targets unnamed static nodes such as `dwc3_imx_swnode`.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
Changes in v2:
- patch 1:
  - Added `swnode_get_unless_zero()` and used it in the pointer-based lookup.
  - Corrected the commit message: `software_node_unregister()` uses the
    borrowed-pointer variant, not the reference-returning variant.
  - No other change to the core reference model.
- patch 2:
  - No longer dereferences `swnode->node` before taking a reference.
  - Matches using the swnode kobject name and parent link.
  - Added a `named` bit to `struct swnode`, set from `node->name`.
  - Takes the reference only after a match is found.
  - Corrected the Fixes tag:
- patch 3: new
- patch 4:
  - Replaced the borrowed `software_node_fwnode()` plus later
  `software_node_get()` sequence.
  - Uses `software_node_to_swnode_get()` directly and returns
  `&remote->fwnode`, retaining the acquired reference.
- Link to v1: https://patch.msgid.link/20261104-swnode-race-v1-v1-0-9406a67e25be@nxp.com

---
Peng Fan (4):
      software node: do not resurrect swnode being released
      software node: use kobject_get_unless_zero() in software_node_find_by_name()
      software node: pin referenced swnodes while resolving them
      software node: pin remote endpoint swnode

 drivers/base/swnode.c | 209 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 148 insertions(+), 61 deletions(-)
---
base-commit: 2637bdec0535a2044cd830eba9a79363651d9043
change-id: 20261104-swnode-race-v1-b9f2dcc954c8

Best regards,
--  
Peng Fan <peng.fan@nxp.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-06  8:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-06  4:38 [PATCH v2 0/4] software node: fix lookup of swnode in release path Peng Fan (OSS)
2026-11-06  4:38 ` [PATCH v2 1/4] software node: do not resurrect swnode being released Peng Fan (OSS)
2026-11-06  8:57   ` Peng Fan
2026-11-06  4:38 ` [PATCH v2 2/4] software node: use kobject_get_unless_zero() in software_node_find_by_name() Peng Fan (OSS)
2026-11-06  4:38 ` [PATCH v2 3/4] software node: pin referenced swnodes while resolving them Peng Fan (OSS)
2026-11-06  4:38 ` [PATCH v2 4/4] software node: pin remote endpoint swnode Peng Fan (OSS)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®