Skip to content

Commit dee0774

Browse files
jhovoldbroonie
authored andcommitted
spi: fix statistics allocation
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>
1 parent 8634e05 commit dee0774

1 file changed

Lines changed: 8 additions & 9 deletions

File tree

drivers/spi/spi.c

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

30513051
ctlr = container_of(dev, struct spi_controller, dev);
3052+
3053+
free_percpu(ctlr->pcpu_statistics);
30523054
kfree(ctlr);
30533055
}
30543056

@@ -3192,6 +3194,12 @@ struct spi_controller *__spi_alloc_controller(struct device *dev,
31923194
if (!ctlr)
31933195
return NULL;
31943196

3197+
ctlr->pcpu_statistics = spi_alloc_pcpu_stats(NULL);
3198+
if (!ctlr->pcpu_statistics) {
3199+
kfree(ctlr);
3200+
return NULL;
3201+
}
3202+
31953203
device_initialize(&ctlr->dev);
31963204
INIT_LIST_HEAD(&ctlr->queue);
31973205
spin_lock_init(&ctlr->queue_lock);
@@ -3483,13 +3491,6 @@ int spi_register_controller(struct spi_controller *ctlr)
34833491
if (status)
34843492
goto del_ctrl;
34853493
}
3486-
/* Add statistics */
3487-
ctlr->pcpu_statistics = spi_alloc_pcpu_stats(dev);
3488-
if (!ctlr->pcpu_statistics) {
3489-
dev_err(dev, "Error allocating per-cpu statistics\n");
3490-
status = -ENOMEM;
3491-
goto destroy_queue;
3492-
}
34933494

34943495
mutex_lock(&board_lock);
34953496
list_add_tail(&ctlr->list, &spi_controller_list);
@@ -3502,8 +3503,6 @@ int spi_register_controller(struct spi_controller *ctlr)
35023503
acpi_register_spi_devices(ctlr);
35033504
return status;
35043505

3505-
destroy_queue:
3506-
spi_destroy_queue(ctlr);
35073506
del_ctrl:
35083507
device_del(&ctlr->dev);
35093508
free_bus_id:

0 commit comments

Comments
 (0)