From patchwork Fri Jan 10 05:47:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 856497 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 D084D206F1F; Fri, 10 Jan 2025 05:47:45 +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=1736488067; cv=none; b=RjbDK+KAr1t0csruROz5JBHeHWG7e+Rn1HdCnSv/+PQEwZ+p35+PXUnQCN8btPf+LDQjhZiFjqKVTrDndH2HHIuoi1+AKaeSNnMcusTKA5SOTOtmPPF3BEGxHmk33gRlFsKdJH45IqEpzljr4zvXSCtphYTvkOE6+S6Y62y4JjI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736488067; c=relaxed/simple; bh=NgFFGT/OrYg/unk6xUfPSRRdHHDSc0/qHGm0WYYT2oo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BBYc0fupti5+Kxc9ydr4s4KvpQ5vWCyBJcRffnwPcbu19B3sC4b4GWEVJFk7Ihgj6oTn16kFelk6o9QmGiPLouROCJ6NUf5V0lVf4FHVqi5kjzcsPNnTajmKDZYsMN1m67oYYnXUodK6xWbsJ0l8V+Zmf0SA3m/zzTWIVbOc0jU= 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=L6Wh1url; 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="L6Wh1url" 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=XOwKKQcvaeqm5/EcreVGcBeygYwpt1R0Y193htmengg=; b=L6Wh1urlSCBdVsH+PI0WAGTBTX FqQRAofm/qMlGp3DqSY6jNi1rI3/UPhiR4R3TWLXT/QvFFUfkq8dyHsjVyit9gDaB2zpKqtV1PG0T F10D4AR8OZz9Mb0ciThMMlXVphQlPLmfOOXyrxjb0fV2wiUAW7nxTcfG0eHqZo+3besSj0dVNTXF4 leUJwo9FlevxkIC5pv7NydIJ5YA2s4ADIuRc7s7bFoKgq2Hw+glwkOiy0bxpASf4fWAI7Cvmta4BD iFuCSkoRH8Fr1AYgDCkQxya2IOpJrA38c23PXpCOf4V+vmZC9ljpR8do38FCC+fC57reIu6BZR0Zb cS4BCA2w==; Received: from 2a02-8389-2341-5b80-76c3-a3dc-14f6-94e8.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:76c3:a3dc:14f6:94e8] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tW7rz-0000000E4xM-49cZ; Fri, 10 Jan 2025 05:47:44 +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, "Martin K . Petersen" , Johannes Thumshirn Subject: [PATCH 06/11] block: fix queue freeze vs limits lock order in sysfs store methods Date: Fri, 10 Jan 2025 06:47:14 +0100 Message-ID: <20250110054726.1499538-7-hch@lst.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250110054726.1499538-1-hch@lst.de> References: <20250110054726.1499538-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-scsi@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. This also removes taking the sysfs lock for the store_limit method as it doesn't protect anything here, but creates even more nesting. Hopefully it will go away from the actual sysfs methods entirely soon. (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: Ming Lei Reviewed-by: Damien Le Moal Reviewed-by: Martin K. Petersen Reviewed-by: Nilay Shroff Reviewed-by: Johannes Thumshirn --- 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 d2aa2177e4ba..e828be777206 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -694,22 +694,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;