Skip to content
/ linux Public

Commit 378b295

Browse files
jhovoldgregkh
authored andcommitted
spi: fix statistics allocation
commit dee0774 upstream. The controller per-cpu statistics is not allocated until after the controller has been registered with driver core, which leaves a window where accessing the sysfs attributes can trigger a NULL-pointer dereference. Fix this by moving the statistics allocation to controller allocation while tying its lifetime to that of the controller (rather than using implicit devres). Fixes: 6598b91 ("spi: spi.c: Convert statistics to per-cpu u64_stats_t") Cc: stable@vger.kernel.org # 6.0 Cc: David Jander <david@protonic.nl> Signed-off-by: Johan Hovold <johan@kernel.org> Link: https://patch.msgid.link/20260312151817.32100-3-johan@kernel.org Signed-off-by: Mark Brown <broonie@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 80f3e8c commit 378b295

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

drivers/spi/spi.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,8 @@ static void spi_controller_release(struct device *dev)
29142914
struct spi_controller *ctlr;
29152915

29162916
ctlr = container_of(dev, struct spi_controller, dev);
2917+
2918+
free_percpu(ctlr->pcpu_statistics);
29172919
kfree(ctlr);
29182920
}
29192921

@@ -3057,6 +3059,12 @@ struct spi_controller *__spi_alloc_controller(struct device *dev,
30573059
if (!ctlr)
30583060
return NULL;
30593061

3062+
ctlr->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
3063+
if (!ctlr->pcpu_statistics) {
3064+
kfree(ctlr);
3065+
return NULL;
3066+
}
3067+
30603068
device_initialize(&ctlr->dev);
30613069
INIT_LIST_HEAD(&ctlr->queue);
30623070
spin_lock_init(&ctlr->queue_lock);
@@ -3347,13 +3355,6 @@ int spi_register_controller(struct spi_controller *ctlr)
33473355
if (status)
33483356
goto del_ctrl;
33493357
}
3350-
/* Add statistics */
3351-
ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev);
3352-
if (!ctlr->pcpu_statistics) {
3353-
dev_err(dev, "Error allocating per-cpu statistics\n");
3354-
status = -ENOMEM;
3355-
goto destroy_queue;
3356-
}
33573358

33583359
mutex_lock(&board_lock);
33593360
list_add_tail(&ctlr->list, &spi_controller_list);
@@ -3366,8 +3367,6 @@ int spi_register_controller(struct spi_controller *ctlr)
33663367
acpi_register_spi_devices(ctlr);
33673368
return status;
33683369

3369-
destroy_queue:
3370-
spi_destroy_queue(ctlr);
33713370
del_ctrl:
33723371
device_del(&ctlr->dev);
33733372
free_bus_id:

0 commit comments

Comments
 (0)