[PATCH v2 02/58] driver core: retain the owner of device release callbacks

Greg Kroah-Hartman gregkh at linuxfoundation.org
Tue Nov 10 10:50:49 PST 2026


On Tue, Nov 10, 2026 at 12:44:05AM -0700, James Hilliard wrote:
> Device references can outlive the driver or subsystem operation that
> unregistered the device. A modular release callback needs its code to
> remain loaded until it returns. Dropping its last module reference inside
> the callback permits concurrent unload before the function epilogue has
> finished.
> 
> Add an optional release_owner to struct device. device_initialize() takes
> the reference before the device can be published. The built-in device
> core caches the owner before invoking the release callback and drops the
> reference after the callback returns, allowing the callback to free the
> device itself. This also covers initialized devices that were never added
> and device_add() failures.

Please no, we have tried this in the past and it's not good.  Devices
are data, which is different from code.

> MTD will use this to keep its release callback available after retained
> device references outlive driver unbind.

This isn't a good idea, dig through the archives and see what went wrong
before please.

As a hint, look at networking, no module reference counts anywhere!
That's the proper way to do it.

thanks,

greg k-h



More information about the linux-mtd mailing list