Skip to content

Commit 04b2d03

Browse files
geertubroonie
authored andcommitted
spi: Fix double IDR allocation with DT aliases
If the SPI bus number is provided by a DT alias, idr_alloc() is called twice, leading to: WARNING: CPU: 1 PID: 1 at drivers/spi/spi.c:2179 spi_register_controller+0x11c/0x5d8 couldn't get idr Fix this by moving the handling of fixed SPI bus numbers up, before the DT handling code fills in ctlr->bus_num. Fixes: 1a4327f ("spi: fix IDR collision on systems with both fixed and dynamic SPI bus numbers") Signed-off-by: Geert Uytterhoeven <geert+renesas@glider.be> Tested-by: Fabio Estevam <fabio.estevam@nxp.com> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 1a4327f commit 04b2d03

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

drivers/spi/spi.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,8 +2143,17 @@ int spi_register_controller(struct spi_controller *ctlr)
21432143
*/
21442144
if (ctlr->num_chipselect == 0)
21452145
return -EINVAL;
2146-
/* allocate dynamic bus number using Linux idr */
2147-
if ((ctlr->bus_num < 0) && ctlr->dev.of_node) {
2146+
if (ctlr->bus_num >= 0) {
2147+
/* devices with a fixed bus num must check-in with the num */
2148+
mutex_lock(&board_lock);
2149+
id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2150+
ctlr->bus_num + 1, GFP_KERNEL);
2151+
mutex_unlock(&board_lock);
2152+
if (WARN(id < 0, "couldn't get idr"))
2153+
return id == -ENOSPC ? -EBUSY : id;
2154+
ctlr->bus_num = id;
2155+
} else if (ctlr->dev.of_node) {
2156+
/* allocate dynamic bus number using Linux idr */
21482157
id = of_alias_get_id(ctlr->dev.of_node, "spi");
21492158
if (id >= 0) {
21502159
ctlr->bus_num = id;
@@ -2170,15 +2179,6 @@ int spi_register_controller(struct spi_controller *ctlr)
21702179
if (WARN(id < 0, "couldn't get idr"))
21712180
return id;
21722181
ctlr->bus_num = id;
2173-
} else {
2174-
/* devices with a fixed bus num must check-in with the num */
2175-
mutex_lock(&board_lock);
2176-
id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2177-
ctlr->bus_num + 1, GFP_KERNEL);
2178-
mutex_unlock(&board_lock);
2179-
if (WARN(id < 0, "couldn't get idr"))
2180-
return id == -ENOSPC ? -EBUSY : id;
2181-
ctlr->bus_num = id;
21822182
}
21832183
INIT_LIST_HEAD(&ctlr->queue);
21842184
spin_lock_init(&ctlr->queue_lock);

0 commit comments

Comments
 (0)