Skip to content

Commit 9751316

Browse files
geertugregkh
authored andcommitted
spi: Fix double IDR allocation with DT aliases
commit 04b2d03 upstream. 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> Cc: Sudip Mukherjee <sudipm.mukherjee@gmail.com> Cc: Kirill Kapranov <kirill.kapranov@compulab.co.il> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ed5e946 commit 9751316

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

drivers/spi/spi.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,8 +2108,17 @@ int spi_register_controller(struct spi_controller *ctlr)
21082108
*/
21092109
if (ctlr->num_chipselect == 0)
21102110
return -EINVAL;
2111-
/* allocate dynamic bus number using Linux idr */
2112-
if ((ctlr->bus_num < 0) && ctlr->dev.of_node) {
2111+
if (ctlr->bus_num >= 0) {
2112+
/* devices with a fixed bus num must check-in with the num */
2113+
mutex_lock(&board_lock);
2114+
id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2115+
ctlr->bus_num + 1, GFP_KERNEL);
2116+
mutex_unlock(&board_lock);
2117+
if (WARN(id < 0, "couldn't get idr"))
2118+
return id == -ENOSPC ? -EBUSY : id;
2119+
ctlr->bus_num = id;
2120+
} else if (ctlr->dev.of_node) {
2121+
/* allocate dynamic bus number using Linux idr */
21132122
id = of_alias_get_id(ctlr->dev.of_node, "spi");
21142123
if (id >= 0) {
21152124
ctlr->bus_num = id;
@@ -2135,15 +2144,6 @@ int spi_register_controller(struct spi_controller *ctlr)
21352144
if (WARN(id < 0, "couldn't get idr"))
21362145
return id;
21372146
ctlr->bus_num = id;
2138-
} else {
2139-
/* devices with a fixed bus num must check-in with the num */
2140-
mutex_lock(&board_lock);
2141-
id = idr_alloc(&spi_master_idr, ctlr, ctlr->bus_num,
2142-
ctlr->bus_num + 1, GFP_KERNEL);
2143-
mutex_unlock(&board_lock);
2144-
if (WARN(id < 0, "couldn't get idr"))
2145-
return id == -ENOSPC ? -EBUSY : id;
2146-
ctlr->bus_num = id;
21472147
}
21482148
INIT_LIST_HEAD(&ctlr->queue);
21492149
spin_lock_init(&ctlr->queue_lock);

0 commit comments

Comments
 (0)