From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384622; b=GikbuqrC1P5LxgQuUcQDxCORtMY6CgMn4HKxwHDsR1OxSf8KiYpSnC/WwzU6Vi0LFaXl ofWjCU58SpsYSDpRB2Ju3FtJsQpCR7d7mlQQ7CSddlKi3IgnFYmJhB5TYZ60aFatJxmK0 K75Hrz578OWkO1S9KszUlwDei8LRD+wiw1UMtt771u8WIfwqgqm+a2NKsRQyQdBksWocV b/An7Jw/x+wlG2mKqriVQCvDS+wvDjgwqZ0nSLk+fekEotLciGcOyXwD6KNhF7P5HkhRp XrJ7dK4mlB/KbKb75v+qpuKrdP456RYbDjaHOH7NNlxmxL5z54x5E3wExJyKyrvHpFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384622; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=bVXLRLSdR8xbE1eysWLHudQWaCIJbIQp3eXWxiM88J8=; b=4LUUfGr1Cy5BHGTsqvfUm8V2oJApPiu2ll8Tb+mPsuPk1Wl05W0deOqPM9ZIfGcZca7u kyGg9oY+ezZ4uVYMqtCHzpB4uWESCtXz4wxn1c2QHTiIGyI4N+DKzOJvOpZUuOLHUnzKZ C0gOiovj0MmkdrZnGl9HVOujo7snjPLN7mqokrN8/WTUX07I/oAKToQdt6dyEF0SBFfOy /OVJuhQ4kkYWB8wcg/28ipmTJbaCoXF4oYgcbodfKMxwdVK6zw6Txh59LS88+INiBjXcV lCmA/+QIhECNYeNhjdxfUIPJ2Sztwnjv48ewvzPn7r9Snb+mygL6NvN2f3ftRgOo3iQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=JNAYEN7h; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3B3E606A338; Fri, 4 Dec 2026 11:43:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=JNAYEN7h Received: by smtp.kernel.org (Postfix) with ESMTPSA id 28F231F00934; Fri, 4 Dec 2026 11:43:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384621; bh=bVXLRLSdR8xbE1eysWLHudQWaCIJbIQp3eXWxiM88J8=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=JNAYEN7hdDHFTB1TVi+xBfjg1k1EXYKhPxFaft6Ypij/Gl/lIbQyCWIqKw+gegYev 4NGCbQeoaU7xVJiN0KxC45zMIFHBk+sioS+U/oLHcOlmuphrlDjvJobkWjDHdl2p2J WaWPBKsEf+noue+LD3DK/1I7ixRroPClm1cB5Kqs= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Mark Korenberg , Ming Lei , Jens Axboe , Sasha Levin Subject: [PATCH 6.6 0225/1167] ublk: publish a new device by its id only once it is set up Date: Fri, 4 Dec 2026 18:54:12 +0900 Message-ID: <20261204100707.931948420@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mark Korenberg [ Upstream commit e5c463e5074a940c644d54d8fc29aba06709aab0 ] ADD_DEV stores the device in ublk_index_idr when it allocates the id, and control commands find it there as soon as ublk_add_chdev() has initialized its struct device, before the char device is added. If dev_set_name() or cdev_device_add() then fails, ublk_add_chdev() drops the initial reference to free the device. A DEL_DEV that found the device meanwhile waits for ublk_ctl_mutex, then calls ublk_remove(): cdev_device_del() of a device that was never added, and a second put of the initial reference, which frees the device under the callers still holding references. Reserve the id with a NULL entry, which idr_find() and the idr iterators skip, and store the device once ublk_add_chdev() succeeded. Fixes: 71f28f3136af ("ublk_drv: add io_uring based userspace block driver") Signed-off-by: Mark Korenberg Reviewed-by: Ming Lei Link: https://patch.msgid.link/20261105-ublk-del-gendisk-hang-v1-1-913a91a4314d@gmail.com Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- drivers/block/ublk_drv.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c index 547492b56067a..2c31ab722361d 100644 --- a/drivers/block/ublk_drv.c +++ b/drivers/block/ublk_drv.c @@ -2152,14 +2152,18 @@ static int ublk_alloc_dev_number(struct ublk_device *ub, int idx) int i = idx; int err; + /* + * Only reserve the id: the device is published by + * ublk_publish_dev_number() once it is fully set up. + */ spin_lock(&ublk_idr_lock); /* allocate id, if @id >= 0, we're requesting that specific id */ if (i >= 0) { - err = idr_alloc(&ublk_index_idr, ub, i, i + 1, GFP_NOWAIT); + err = idr_alloc(&ublk_index_idr, NULL, i, i + 1, GFP_NOWAIT); if (err == -ENOSPC) err = -EEXIST; } else { - err = idr_alloc(&ublk_index_idr, ub, 0, UBLK_MAX_UBLKS, + err = idr_alloc(&ublk_index_idr, NULL, 0, UBLK_MAX_UBLKS, GFP_NOWAIT); } spin_unlock(&ublk_idr_lock); @@ -2170,6 +2174,19 @@ static int ublk_alloc_dev_number(struct ublk_device *ub, int idx) return err; } +/* + * Make the device visible to control commands by its id. Before its char + * device is added, DEL_DEV could find it and remove a char device that was + * never added, and drop the reference that ublk_add_chdev() drops on + * failure. + */ +static void ublk_publish_dev_number(struct ublk_device *ub) +{ + spin_lock(&ublk_idr_lock); + WARN_ON_ONCE(idr_replace(&ublk_index_idr, ub, ub->ub_number) != NULL); + spin_unlock(&ublk_idr_lock); +} + static void ublk_free_dev_number(struct ublk_device *ub) { spin_lock(&ublk_idr_lock); @@ -2542,6 +2559,8 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) * ublk_add_chdev() will cleanup everything if it fails. */ ret = ublk_add_chdev(ub); + if (!ret) + ublk_publish_dev_number(ub); goto out_unlock; out_free_tag_set: -- 2.53.0