Re: [PATCH] i3c: master: Skip IDR cleanup if bus ID allocation fails

From: Frank Li

Date: Tue Nov 17 2026 - 12:45:13 EST


On Mon, Nov 16, 2026 at 11:53:17AM +0800, Zhang Yuwei wrote:
> Since commit 9d4f219807d5 ("i3c: fix refcount inconsistency in
> i3c_master_register"), an i3c_bus_init() failure is handled by dropping
> the initialized device reference. This invokes i3c_masterdev_release(),
> which unconditionally calls i3c_bus_cleanup().
>
> However, i3c_bus_init() only assigns the bus ID after idr_alloc()
> succeeds. If the allocation fails, a zero-initialized controller retains
> a bus ID of zero, and i3c_bus_cleanup() removes ID zero from the IDR.
> If another bus owns that ID, its mapping is incorrectly removed, allowing
> the ID to be reused while that bus is still registered.
>
> Initialize the bus ID to -1 before attempting allocation and skip IDR
> cleanup when the ID is negative. Keep the put_device() error path so that
> the device reference and firmware node reference are released normally.
>
> Fixes: 9d4f219807d5 ("i3c: fix refcount inconsistency in i3c_master_register")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Zhang Yuwei <zhangyuwei20@xxxxxxxxxx>
> ---

Reviewed-by: Frank Li <Frank.Li@xxxxxxx>

> drivers/i3c/master.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
> index 9928c5dffb5b..39363c097682 100644
> --- a/drivers/i3c/master.c
> +++ b/drivers/i3c/master.c
> @@ -523,6 +523,9 @@ static void i3c_bus_init_addrslots(struct i3c_bus *bus)
>
> static void i3c_bus_cleanup(struct i3c_bus *i3cbus)
> {
> + if (i3cbus->id < 0)
> + return;
> +
> mutex_lock(&i3c_core_lock);
> idr_remove(&i3c_bus_idr, i3cbus->id);
> mutex_unlock(&i3c_core_lock);
> @@ -532,6 +535,7 @@ static int i3c_bus_init(struct i3c_bus *i3cbus, struct fwnode_handle *fwnode)
> {
> int ret, start, end, id = -1;
>
> + i3cbus->id = -1;
> init_rwsem(&i3cbus->lock);
> INIT_LIST_HEAD(&i3cbus->devs.i2c);
> INIT_LIST_HEAD(&i3cbus->devs.i3c);
> --
> 2.22.0
>