From patchwork Wed Jan 8 09:25:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13930392 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 262511DFE0F; Wed, 8 Jan 2025 09:25:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.137.202.133 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736328344; cv=none; b=C00UAx1FedZ7hZN1qBsl/Z6OztitW98JvKnqIxBSKr6sWUJ7/PE90/tM+4Qz3RQ8CXiRNDYn49bqIelI//e3r7ybO/C57l99re95P18fEOz/OLQ5I4eyYMq2625vEF+/D3OZndYQlT7T9FnZ9+Cc4UDfDPzAFq5yQy68OQm+Lfk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736328344; c=relaxed/simple; bh=B+HJqIXUYgLitEBhLZA48RqLjtE3YbyMxOAycfcBaxk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ht0Ij3NVl+d5N9GhmqI0ht3rTOz1NA9FZdwv2kPRaOxGqf6aXt9TLA/hVe4ihm2Ii6cCTMD8QQhJxU1p95E/M3dC1DRVrVXL5vQ8Lm7a+NbCzbcw5YHOScB+Qcb4A87tCsURbGNEg3ZDBt7cOFU9xk1VPMZPa4Ej4ubjvMDIXYA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de; spf=none smtp.mailfrom=bombadil.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=KZcY6PoO; arc=none smtp.client-ip=198.137.202.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=bombadil.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="KZcY6PoO" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=uJvbhGLt97FsIbhsJnskeRWyPNiZsDPKmhL4bk2h2rs=; b=KZcY6PoONs9ZwxuP902O2oQcaL qQaZrfsGX22AeaTWMG+eQbVsFEXtiXG6lrvjSDEXBm92iHstiM3h8BuXcZV1bEKo+SlddhA6UsHXc 3o8ilp8VOx+ye+Frn+HWduqRI28DsIIn6tNmrQakbwTdVEdrCsKv+Tj87An9TdJwc4tpBI+M+KT1c 7M3LwC8Jf6H31H/+97RyMfKmizeAKc4o305O3gNFPDrUNnhsFX0Ga8dKQcYYEkaV/y0INdgbx1C1W wOAixA+TPGFQ3N44cG1NqivpBEnrVanA46o8GSfUCceTL5ZSfNrNsAmAvon/pgYS49Ufbs+8WRDlB 5G572+jg==; Received: from 2a02-8389-2341-5b80-e44b-b36a-6403-8f06.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:e44b:b36a:6403:8f06] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tVSJp-00000007lax-21ig; Wed, 08 Jan 2025 09:25:42 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Damien Le Moal , Ming Lei , Nilay Shroff , linux-block@vger.kernel.org, linux-nvme@lists.infradead.org, nbd@other.debian.org, linux-scsi@vger.kernel.org, usb-storage@lists.one-eyed-alien.net Subject: [PATCH 05/10] block: fix queue freeze vs limits lock order in sysfs store methods Date: Wed, 8 Jan 2025 10:25:02 +0100 Message-ID: <20250108092520.1325324-6-hch@lst.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250108092520.1325324-1-hch@lst.de> References: <20250108092520.1325324-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html queue_attr_store() always freezes a device queue before calling the attribute store operation. For attributes that control queue limits, the store operation will also lock the queue limits with a call to queue_limits_start_update(). However, some drivers (e.g. SCSI sd) may need to issue commands to a device to obtain limit values from the hardware with the queue limits locked. This creates a potential ABBA deadlock situation if a user attempts to modify a limit (thus freezing the device queue) while the device driver starts a revalidation of the device queue limits. Avoid such deadlock by not freezing the queue before calling the ->store_limit() method in struct queue_sysfs_entry and instead use the queue_limits_commit_update_frozen helper to freeze the queue after taking the limits lock. (commit log adapted from a similar patch from Damien Le Moal) Fixes: ff956a3be95b ("block: use queue_limits_commit_update in queue_discard_max_store") Fixes: 0327ca9d53bf ("block: use queue_limits_commit_update in queue_max_sectors_store") Signed-off-by: Christoph Hellwig Reviewed-by: Nilay Shroff Reviewed-by: Damien Le Moal Reviewed-by: Ming Lei --- block/blk-sysfs.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c index f36356cbde0b..2de405cb5f10 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -691,22 +691,24 @@ queue_attr_store(struct kobject *kobj, struct attribute *attr, if (entry->load_module) entry->load_module(disk, page, length); - mutex_lock(&q->sysfs_lock); - blk_mq_freeze_queue(q); if (entry->store_limit) { struct queue_limits lim = queue_limits_start_update(q); res = entry->store_limit(disk, page, length, &lim); if (res < 0) { queue_limits_cancel_update(q); - } else { - res = queue_limits_commit_update(q, &lim); - if (!res) - res = length; + return res; } - } else { - res = entry->store(disk, page, length); + + res = queue_limits_commit_update_frozen(q, &lim); + if (res) + return res; + return length; } + + mutex_lock(&q->sysfs_lock); + blk_mq_freeze_queue(q); + res = entry->store(disk, page, length); blk_mq_unfreeze_queue(q); mutex_unlock(&q->sysfs_lock); return res;