Skip to contentSkip to conversation

netdevsim: take bus device refcount before registering device

PATCH Superseded Adriano Cordova 2 participants 2 messages Aug 31 – Sep 2

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netdevsim: take bus device refcount before registering device

nsim_bus_dev_release() drops nsim_bus_devs when a device is freed, but the matching increment was done in new_device_store() only after nsim_bus_dev_new() returned successfully. When device_register() failed inside nsim_bus_dev_new(), put_device() invoked the release callback and decremented nsim_bus_devs without the increment, driving the refcount to zero. A later successful device creation then incremented from zero and triggered:

refcount_t: addition on 0; use-after-free.

Increment nsim_bus_devs in nsim_bus_dev_new() before device_register() so the release callback's decrement is balanced on both the success and error paths.

Reported-by [email protected]
Fixes 6aff7cbfe7bfa ("netdevsim: Block until all devices are released")
---
 drivers/net/netdevsim/bus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/netdevsim/bus.c +1 -1
index 5c55c308487b..426fb438b922 100644
@@ -194,7 +194,6 @@ new_device_store(const struct bus_type *bus, const char *buf, size_t count)
 		goto err;
 	}
 
-	refcount_inc(&nsim_bus_devs);
 	/* Allow using nsim_bus_dev */
 	smp_store_release(&nsim_bus_dev->init, true);
 
@@ -465,6 +464,7 @@ nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queu
 	/* Disallow using nsim_bus_dev */
 	smp_store_release(&nsim_bus_dev->init, false);
 
+	refcount_inc(&nsim_bus_devs);
 	err = device_register(&nsim_bus_dev->dev);
 	if (err)
 		goto err_nsim_bus_dev_id_free;
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 8 lines · On Mon, 31 Aug 2026 14:39:52 -0400 Adriano Cordova wrote:

nsim_bus_dev_release() drops nsim_bus_devs when a device is freed, but the matching increment was done in new_device_store() only after nsim_bus_dev_new() returned successfully. When device_register() failed inside nsim_bus_dev_new(), put_device() invoked the release callback and decremented nsim_bus_devs without the increment, driving the refcount to zero. A later successful device creation then incremented from zero and triggered:

Quoted 5 lines

refcount_t: addition on 0; use-after-free.

Increment nsim_bus_devs in nsim_bus_dev_new() before device_register() so the release callback's decrement is balanced on both the success and error paths.

This is still racy, please squash this into v2:

@@ -520,8 +520,6 @@ void nsim_bus_exit(void)

        /* Disallow using resources */
        smp_store_release(&nsim_bus_enable, false);

- if (refcount_dec_and_test(&nsim_bus_devs))
- complete(&nsim_bus_devs_released);

        mutex_lock(&nsim_bus_dev_list_lock);
        list_for_each_entry_safe(nsim_bus_dev, tmp, &nsim_bus_dev_list, list) {
@@ -530,6 +528,9 @@ void nsim_bus_exit(void)
        }
        mutex_unlock(&nsim_bus_dev_list_lock);

+ if (refcount_dec_and_test(&nsim_bus_devs))
+ complete(&nsim_bus_devs_released);
+

        wait_for_completion(&nsim_bus_devs_released);
 
        driver_unregister(&nsim_driver);
Signature · 1 line
pw-bot: cr