[cc += Sascha] On Wed, Oct 14, 2020 at 09:25:05PM +0100, Mark Brown wrote: > > On Wed, Oct 14, 2020 at 04:09:12PM +0200, Lukas Wunner wrote: > > > Apparently the problem is that spi_unregister_controller() drops the > > > last ref on the controller, causing it to be freed, and afterwards we > > > access the controller's private data, which is part of the same > > > allocation as struct spi_controller: > > Right, the proposed patch is yet another way to fix the issue - it all > comes back to the fact that you shouldn't be using the driver data after > unregistering if it was allocated as part of allocating the controller. > This framework feature is unfortunately quite error prone. How about holding a ref on the controller as long as the SPI driver is bound to the controller's parent device? See below for a patch, compile-tested only for lack of an SPI-equipped machine. Makes sense or dumb idea? If this approach is deemed to be a case of "midlayer fallacy", we could alternatively do this in a library function which drivers opt-in to. Or, given that the majority of drivers seems to be affected, make it the default and allow drivers to opt-out. -- >8 -- diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 0cab239..5afa275 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2399,6 +2399,11 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr, extern struct class spi_slave_class; /* dummy */ #endif +static void __spi_controller_put(void *ctlr) +{ + spi_controller_put(ctlr); +} + /** * __spi_alloc_controller - allocate an SPI master or slave controller * @dev: the controller, possibly using the platform_bus @@ -2414,6 +2419,7 @@ static ssize_t slave_store(struct device *dev, struct device_attribute *attr, * This call is used only by SPI controller drivers, which are the * only ones directly touching chip registers. It's how they allocate * an spi_controller structure, prior to calling spi_register_controller(). + * The structure is accessible as long as the SPI driver is bound to @dev. * * This must be called from context that can sleep. * @@ -2429,6 +2435,7 @@ struct spi_controller *__spi_alloc_controller(struct device *dev, { struct spi_controller *ctlr; size_t ctlr_size = ALIGN(sizeof(*ctlr), dma_get_cache_alignment()); + int ret; if (!dev) return NULL; @@ -2449,6 +2456,13 @@ struct spi_controller *__spi_alloc_controller(struct device *dev, pm_suspend_ignore_children(&ctlr->dev, true); spi_controller_set_devdata(ctlr, (void *)ctlr + ctlr_size); + spi_controller_get(ctlr); + ret = devm_add_action(dev, __spi_controller_put, ctlr); + if (ret) { + kfree(ctlr); + return NULL; + } + return ctlr; } EXPORT_SYMBOL_GPL(__spi_alloc_controller);