Skip to contentSkip to conversation

driver core: use READ_ONCE() for dev->driver in dev_has_sync_state()

PATCH Applied Danilo Krummrich 5 participants 9 messages Apr 18 – May 4 Driver core

lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/1 driver core: use READ_ONCE() for dev->driver in dev_has_sync_state()

dev_has_sync_state() reads dev->driver twice without holding device_lock() -- once for the NULL check and once to dereference ->sync_state. Some callers only hold device_links_write_lock, which doesn't prevent a concurrent unbind from clearing dev->driver via device_unbind_cleanup().

Fix it by reading dev->driver exactly once with READ_ONCE(), pairing with the WRITE_ONCE() in device_set_driver().

Fixes ac338acf514e ("driver core: Add dev_has_sync_state()")
---
 include/linux/device.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/device.h +4 -1
index ac972e7bead4..4c1c9cb8570a 100644
@@ -1018,9 +1018,12 @@ static inline void device_lock_assert(struct device *dev)
 
 static inline bool dev_has_sync_state(struct device *dev)
 {
+	struct device_driver *drv;
+
 	if (!dev)
 		return false;
-	if (dev->driver && dev->driver->sync_state)
+	drv = READ_ONCE(dev->driver);
+	if (drv && drv->sync_state)
 		return true;
 	if (dev->bus && dev->bus->sync_state)
 		return true;

base-commit: 5b484311507b5d403c1f7a45f6aa3778549e268b

Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Danilo Krummrich

dev_has_sync_state() reads dev->driver twice without holding device_lock() -- once for the NULL check and once to dereference ->sync_state. Some callers only hold device_links_write_lock, which doesn't prevent a concurrent unbind from clearing dev->driver via device_unbind_cleanup().

Quoted 6 lines

Fix it by reading dev->driver exactly once with READ_ONCE(), pairing with the WRITE_ONCE() in device_set_driver().

Fixes ac338acf514e ("driver core: Add dev_has_sync_state()")

-Saravana

Quoted 27 lines
---
 include/linux/device.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/device.h +4 -1
index ac972e7bead4..4c1c9cb8570a 100644
@@ -1018,9 +1018,12 @@ static inline void device_lock_assert(struct device *dev)
 
 static inline bool dev_has_sync_state(struct device *dev)
 {
+       struct device_driver *drv;
+
        if (!dev)
                return false;
-       if (dev->driver && dev->driver->sync_state)
+       drv = READ_ONCE(dev->driver);
+       if (drv && drv->sync_state)
                return true;
        if (dev->bus && dev->bus->sync_state)
                return true;

base-commit: 5b484311507b5d403c1f7a45f6aa3778549e268b --
2.53.0

maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Danilo Krummrich
Quoted 35 lines · Danilo Krummrich

dev_has_sync_state() reads dev->driver twice without holding device_lock() -- once for the NULL check and once to dereference ->sync_state. Some callers only hold device_links_write_lock, which doesn't prevent a concurrent unbind from clearing dev->driver via device_unbind_cleanup().

Fix it by reading dev->driver exactly once with READ_ONCE(), pairing with the WRITE_ONCE() in device_set_driver().

Fixes ac338acf514e ("driver core: Add dev_has_sync_state()")
---
 include/linux/device.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/device.h +4 -1
index ac972e7bead4..4c1c9cb8570a 100644
@@ -1018,9 +1018,12 @@ static inline void device_lock_assert(struct device *dev)
 
 static inline bool dev_has_sync_state(struct device *dev)
 {
+	struct device_driver *drv;
+
 	if (!dev)
 		return false;
-	if (dev->driver && dev->driver->sync_state)
+	drv = READ_ONCE(dev->driver);
+	if (drv && drv->sync_state)
 		return true;
 	if (dev->bus && dev->bus->sync_state)
 		return true;
Quoted 5 lines

base-commit: 5b484311507b5d403c1f7a45f6aa3778549e268b

Signature · 1 line
2.53.0
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Danilo Krummrich
Quoted 7 lines · Danilo Krummrich

dev_has_sync_state() reads dev->driver twice without holding device_lock() -- once for the NULL check and once to dereference ->sync_state. Some callers only hold device_links_write_lock, which doesn't prevent a concurrent unbind from clearing dev->driver via device_unbind_cleanup().

Quoted 6 lines

Fix it by reading dev->driver exactly once with READ_ONCE(), pairing with the WRITE_ONCE() in device_set_driver().

Fixes ac338acf514e ("driver core: Add dev_has_sync_state()")
Quoted 13 lines
---
 include/linux/device.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/device.h +0 -0
index ac972e7bead4..4c1c9cb8570a 100644
@@ -1018,9 +1018,12 @@ static inline void device_lock_assert(struct device *dev)
 
 static inline bool dev_has_sync_state(struct device *dev)
 {

include/linux/device.h static inline void device_lock_assert(struct device *dev) · line 1029

Quoted 10 lines
include/linux/device.h +4 -1
+       struct device_driver *drv;
+
        if (!dev)
                return false;
-       if (dev->driver && dev->driver->sync_state)
+       drv = READ_ONCE(dev->driver);
+       if (drv && drv->sync_state)
                return true;
        if (dev->bus && dev->bus->sync_state)
                return true;

On a somewhat related note, I'm wondering if this function can be moved to drivers/base/base.h?

It doesn't look like having it defined in device.h is particularly useful.

#4 lore raw
Quoted 45 lines · Rafael J. Wysocki
Quoted 7 lines · Danilo Krummrich

dev_has_sync_state() reads dev->driver twice without holding device_lock() -- once for the NULL check and once to dereference ->sync_state. Some callers only hold device_links_write_lock, which doesn't prevent a concurrent unbind from clearing dev->driver via device_unbind_cleanup().

Quoted 6 lines

Fix it by reading dev->driver exactly once with READ_ONCE(), pairing with the WRITE_ONCE() in device_set_driver().

Fixes ac338acf514e ("driver core: Add dev_has_sync_state()")
Quoted 13 lines
---
 include/linux/device.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/linux/device.h +0 -0
index ac972e7bead4..4c1c9cb8570a 100644
@@ -1018,9 +1018,12 @@ static inline void device_lock_assert(struct device *dev)
 
 static inline bool dev_has_sync_state(struct device *dev)
 {

include/linux/device.h static inline void device_lock_assert(struct device *dev) · line 1029

Quoted 10 lines
include/linux/device.h +4 -1
+       struct device_driver *drv;
+
        if (!dev)
                return false;
-       if (dev->driver && dev->driver->sync_state)
+       drv = READ_ONCE(dev->driver);
+       if (drv && drv->sync_state)
                return true;
        if (dev->bus && dev->bus->sync_state)
                return true;

On a somewhat related note, I'm wondering if this function can be moved to drivers/base/base.h?

Quoted 1 line

It doesn't look like having it defined in device.h is particularly useful.

Apologize for the delay. Genpd needs the function [1] when it tries to assign a common ->sync_state() callback for its providers.

Kind regards
Uffe

[1]
https://lore.kernel.org/all/[email protected]/

#6 lore raw
Quoted 7 lines · Danilo Krummrich
Quoted 2 lines · Ulf Hansson

Apologize for the delay. Genpd needs the function [1] when it tries to assign a common ->sync_state() callback for its providers.

Can you add a revert of [1] to your patch series, so it gets (re-)exported with a user?

Yes!

I was trying to avoid dependent changes to the driver core to funnel my series entirely through my pmdomain tree. Anyway, perhaps you can host an immutable branch for me to pull in, in this case.

Let's see, I will suggest something for the merge path in the cover letter when I post my new version.

Kind regards
Uffe

Quoted 2 lines

[1] https://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git/commit/?h=driver-core-next&id=9db268212e0d7c7e3c4aef3494e55afbc1695b1f

author maintainer
#7 lore raw
Quoted 6 lines · Ulf Hansson
Quoted 5 lines · Danilo Krummrich
Quoted 2 lines · Ulf Hansson

Apologize for the delay. Genpd needs the function [1] when it tries to assign a common ->sync_state() callback for its providers.

Quoted 8 lines
Quoted 2 lines

Can you add a revert of [1] to your patch series, so it gets (re-)exported with a user?

Yes!

I was trying to avoid dependent changes to the driver core to funnel my series entirely through my pmdomain tree. Anyway, perhaps you can host an immutable branch for me to pull in, in this case.

I think you can just make the revert the first patch in your series and when it is ready I can pick up only the revert in the driver-core tree; everything else can go through your pmdomain tree.

I.e. you don't need the revert in your pmdomain tree as it should still have dev_has_sync_state() exported in device.h.

author maintainer Applied
#8 lore raw
In reply to Danilo Krummrich
Quoted 1 line · Danilo Krummrich

[PATCH] driver core: use READ_ONCE() for dev->driver in dev_has_sync_state()

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/1] driver core: use READ_ONCE() for dev->driver in dev_has_sync_state()

      commit: e9506871a8ea

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.