From patchwork Tue Jan 7 06:30:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13928290 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 12F591E22FC; Tue, 7 Jan 2025 06:31:40 +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=1736231528; cv=none; b=imKE5oVXuVDfbeWO97uCOAlefJDtR5dokhb8xjN2D9uumKpEn7POBsh2+Re8ULPhY+8ShGepkTSzQBdYzwQid2sstlblCYjr+W/h+hGxrKRlcWARI6AeRg4tD45lRfCIHPCH0uMOlEc+PlqIBUtYuUBZ7sW5sCBoRBTvV3sZDyw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736231528; c=relaxed/simple; bh=6iweoRHlESJ0sTgIMUcp2F8+NXtYgYp6q0Pbchf1arI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Twa98TaZuY5FRdVetCWeQQIhBgcrNwvpMeq8NG2KFiWk0YxmDP5Xwcz7+u3bM52O4/EtLHtAEOomwLI3nPQKw/c+cn3uaEQxhTCSCx4N80eLwfxyOAIxbyWiz+srYThu9fgx6/hC6Bjj6TQ0M0UNYTKQ1ULtwe+zH8SqfUYySlg= 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=DA4OpRwO; 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="DA4OpRwO" 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=pMQiFqzMKsYUd5COwdvMVoaFbslLj2mcHOdXT5wS4Qc=; b=DA4OpRwOb4Ipunn9iFMI8Nk4x4 dsbkp5Wwce5oHTPOjPADYjlVDzKaY6InDz1MDWdeEAgsvoZu9UjYy3MRHdjsTbbq3TaWUfu+AkrYb 1hYqn/DaS+/Ya9t8Up9qyk4CqaiCeWSvkkTpiA78ApJFaRPJ8sEdDP5dJc5XyDs/8tWnZuGVfn9M6 oPLI6q5SHrtTK+WIK0ESkc+J1mq6uCxl/4P3lTDoIlkZ5WoI4bDUREngyi7+8Ovt/wa13pEAsj1ZX 7Ro2koUx/r553KwA12ebgY+q33DshdHLJpoOZmQ4sM2bUkgMuDyoFLAqo348A7S8yo2ICV9yzPbMX uJIKgIzg==; Received: from 2a02-8389-2341-5b80-d467-d75d-35bf-0eb6.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:d467:d75d:35bf:eb6] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.98 #2 (Red Hat Linux)) id 1tV37q-00000003dvo-0Nlj; Tue, 07 Jan 2025 06:31:38 +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 5/8] block: fix queue freeze vs limits lock order in sysfs store methods Date: Tue, 7 Jan 2025 07:30:37 +0100 Message-ID: <20250107063120.1011593-6-hch@lst.de> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250107063120.1011593-1-hch@lst.de> References: <20250107063120.1011593-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 --- 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 8d69315e986d..3bac27fcd635 100644 --- a/block/blk-sysfs.c +++ b/block/blk-sysfs.c @@ -687,22 +687,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;