From patchwork Mon Dec 16 08:02:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 13909341 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 3776F1FFC56 for ; Mon, 16 Dec 2024 08:02:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734336158; cv=none; b=j1/hFNvvPJqQZSciTVKl3k7JelLs0q2rPEhT9BRlQaCphvifMhOAIMDuti4gkYjy1vWJ8JBlNgri0/NMnnu+VO9PxKfkN4aT1bD+kxu3noeJ3ZEIREd6dHVjPodloV/yCeFy/MugjMkFSsvy+q+fci+5wRkUWfK+rKA7YYRuWX8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734336158; c=relaxed/simple; bh=kQ5hYPWxOXYZPwXBb4EAMXQtjCs6eOmMXeRq9pnEsEE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L3x5EBUREPPpcpNZfEH/lmvqObLpBP/JpmYKBMJPgRWW+1EjYTy9Z2VJ+v99psv5QMSy55WpGKPZy7OH9VMMTg7T5mRvMgbgScKJ/baIhvm3UeT9BC2ClmN2a9dDM4n3GPUOVaIFf5/kAXnCHQ1IJtd4Ah9AgUEh7zA3M54/RyQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=gdurvRVe; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="gdurvRVe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734336155; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O8sXC6LXjUiv5a6g1mpO360gKJtsqaNC6Rbu/MiAQOM=; b=gdurvRVe7WDOjd4ppaav7LGScW4AFXWmevwDDaW5HzYV3NFlPXfPm4qF77vtUE5sPjffpe qzqu3W1AgHN/qTpaJudODdLbt6vDt6+u3MsrEAh+D+jWY+q6wfudPSKNlqMp8CbTrq0Kg2 +oh9ji5bAx+aL04HQZqLam6I1ypQk8k= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-594-bDYAs3GpPz6hsOCnbz3dBA-1; Mon, 16 Dec 2024 03:02:31 -0500 X-MC-Unique: bDYAs3GpPz6hsOCnbz3dBA-1 X-Mimecast-MFC-AGG-ID: bDYAs3GpPz6hsOCnbz3dBA Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5268A1955F2D; Mon, 16 Dec 2024 08:02:30 +0000 (UTC) Received: from localhost (unknown [10.72.116.154]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EC02A1956052; Mon, 16 Dec 2024 08:02:28 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Christoph Hellwig , Ming Lei Subject: [PATCH 1/2] block: avoid to hold q->limits_lock across APIs for atomic update queue limits Date: Mon, 16 Dec 2024 16:02:03 +0800 Message-ID: <20241216080206.2850773-2-ming.lei@redhat.com> In-Reply-To: <20241216080206.2850773-1-ming.lei@redhat.com> References: <20241216080206.2850773-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 Commit d690cb8ae14b ("block: add an API to atomically update queue limits") adds APIs for updating queue limits atomically. And q->limits_lock is grabbed in queue_limits_start_update(), and released in queue_limits_commit_update(). This way is very fragile and easy to introduce deadlock[1][2]. More importantly, queue_limits_start_update() returns one local copy of q->limits, then the API user overwrites the local copy, and commit the copy by queue_limits_commit_update() finally. So holding q->limits_lock protects nothing for the local copy, and not see any real help by preventing new update & commit from happening, cause what matters is that we do validation & commit atomically. Changes the API to not hold q->limits_lock across atomic queue limits update APIs for fixing deadlock & making it easy to use. [1] https://lore.kernel.org/linux-block/Z1A8fai9_fQFhs1s@hovoldconsulting.com/ [2] https://lore.kernel.org/linux-scsi/ZxG38G9BuFdBpBHZ@fedora/ Fixes: d690cb8ae14b ("block: add an API to atomically update queue limits") Signed-off-by: Ming Lei --- block/blk-settings.c | 2 +- include/linux/blkdev.h | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/block/blk-settings.c b/block/blk-settings.c index 8f09e33f41f6..b737428c6084 100644 --- a/block/blk-settings.c +++ b/block/blk-settings.c @@ -422,6 +422,7 @@ int queue_limits_commit_update(struct request_queue *q, { int error; + mutex_lock(&q->limits_lock); error = blk_validate_limits(lim); if (error) goto out_unlock; @@ -456,7 +457,6 @@ EXPORT_SYMBOL_GPL(queue_limits_commit_update); */ int queue_limits_set(struct request_queue *q, struct queue_limits *lim) { - mutex_lock(&q->limits_lock); return queue_limits_commit_update(q, lim); } EXPORT_SYMBOL_GPL(queue_limits_set); diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 378d3a1a22fc..6cc20ca79adc 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -944,8 +944,13 @@ static inline unsigned int blk_boundary_sectors_left(sector_t offset, static inline struct queue_limits queue_limits_start_update(struct request_queue *q) { + struct queue_limits lim; + mutex_lock(&q->limits_lock); - return q->limits; + lim = q->limits; + mutex_unlock(&q->limits_lock); + + return lim; } int queue_limits_commit_update(struct request_queue *q, struct queue_limits *lim); @@ -962,7 +967,6 @@ int blk_validate_limits(struct queue_limits *lim); */ static inline void queue_limits_cancel_update(struct request_queue *q) { - mutex_unlock(&q->limits_lock); } /* From patchwork Mon Dec 16 08:02:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ming Lei X-Patchwork-Id: 13909342 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 B069D1FFC56 for ; Mon, 16 Dec 2024 08:02:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734336162; cv=none; b=eiOtMGcb7OPvmItiH/3nok5v2Xo/+dKK15iVup+tpPAm58rjLEmcE9CYq/8oY1VRBeSdYwuPLMV5jKBhmjh974ok5inzN2Kp+DILRnepLuV7ZWzi6szeDYqOWtg1gCiOawZLfp7N4tJofTelyqf+ebweTyM3pWSkOOUUsRNkmcc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734336162; c=relaxed/simple; bh=Ns1dvaTpOUy4lMjd72u4kDq+U9HX24L9vEP2LEi5fEE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GtZDU1w3jk7iaImxM4U0kM46GW/2/bvt1ej13Fs1ZFZ6JKclo7/zkRgN9MELk1TLgbkQAuw/uVvui8WTLFVO1ckQYevrWrtizVf0bLaYD9QXhpkBkuE1EUZ/+HPUh5e/cPetMBhoj3D+dItFXIDJDdR1mmr9qNMM5Lce7OvGmZ8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=KGmsPtgu; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KGmsPtgu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734336159; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Ntla/0oqB5bIDpBNzrGgEmevgULQgqiy9pQ9dL9GkfU=; b=KGmsPtguCkdmLQ3E/CZl7dOcgOLs+YFPNseYEmWyZJ1UZ2jNBv9GOvmhCjyTs+khT+PdOG WfA2PtOnKGXbSvcM2GDDKd+EC2wQfcjOowl+h35p1fZQXpGIfowNqPf527dNNRCTc53SrV zfltFuslyxeDu9FY0eqO+yA5lu07KBU= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-642-VodR2cUqNSu-EhF-Qua2Iw-1; Mon, 16 Dec 2024 03:02:36 -0500 X-MC-Unique: VodR2cUqNSu-EhF-Qua2Iw-1 X-Mimecast-MFC-AGG-ID: VodR2cUqNSu-EhF-Qua2Iw Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E1F5E19560BA; Mon, 16 Dec 2024 08:02:34 +0000 (UTC) Received: from localhost (unknown [10.72.116.154]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B9F2C19560A3; Mon, 16 Dec 2024 08:02:32 +0000 (UTC) From: Ming Lei To: Jens Axboe , linux-block@vger.kernel.org Cc: Christoph Hellwig , Ming Lei Subject: [PATCH 2/2] block: remove queue_limits_cancel_update() Date: Mon, 16 Dec 2024 16:02:04 +0800 Message-ID: <20241216080206.2850773-3-ming.lei@redhat.com> In-Reply-To: <20241216080206.2850773-1-ming.lei@redhat.com> References: <20241216080206.2850773-1-ming.lei@redhat.com> Precedence: bulk X-Mailing-List: linux-block@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Now queue_limits_cancel_update() becomes nop, so remove it. Signed-off-by: Ming Lei --- drivers/md/md.c | 1 - drivers/md/raid0.c | 4 +--- drivers/md/raid1.c | 4 +--- drivers/md/raid10.c | 4 +--- drivers/scsi/scsi_scan.c | 1 - include/linux/blkdev.h | 12 ------------ 6 files changed, 3 insertions(+), 23 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index aebe12b0ee27..4a3e109dfa11 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -5788,7 +5788,6 @@ int mddev_stack_new_rdev(struct mddev *mddev, struct md_rdev *rdev) if (!queue_limits_stack_integrity_bdev(&lim, rdev->bdev)) { pr_err("%s: incompatible integrity profile for %pg\n", mdname(mddev), rdev->bdev); - queue_limits_cancel_update(mddev->gendisk->queue); return -ENXIO; } diff --git a/drivers/md/raid0.c b/drivers/md/raid0.c index 7049ec7fb8eb..e8802309ed60 100644 --- a/drivers/md/raid0.c +++ b/drivers/md/raid0.c @@ -386,10 +386,8 @@ static int raid0_set_limits(struct mddev *mddev) lim.io_opt = lim.io_min * mddev->raid_disks; lim.features |= BLK_FEAT_ATOMIC_WRITES_STACKED; err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY); - if (err) { - queue_limits_cancel_update(mddev->gendisk->queue); + if (err) return err; - } return queue_limits_set(mddev->gendisk->queue, &lim); } diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c index 519c56f0ee3d..c6e53cc57440 100644 --- a/drivers/md/raid1.c +++ b/drivers/md/raid1.c @@ -3241,10 +3241,8 @@ static int raid1_set_limits(struct mddev *mddev) lim.max_write_zeroes_sectors = 0; lim.features |= BLK_FEAT_ATOMIC_WRITES_STACKED; err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY); - if (err) { - queue_limits_cancel_update(mddev->gendisk->queue); + if (err) return err; - } return queue_limits_set(mddev->gendisk->queue, &lim); } diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c index 7d7a8a2524dc..6acc96be77aa 100644 --- a/drivers/md/raid10.c +++ b/drivers/md/raid10.c @@ -4042,10 +4042,8 @@ static int raid10_set_queue_limits(struct mddev *mddev) lim.io_opt = lim.io_min * raid10_nr_stripes(conf); lim.features |= BLK_FEAT_ATOMIC_WRITES_STACKED; err = mddev_stack_rdev_limits(mddev, &lim, MDDEV_STACK_INTEGRITY); - if (err) { - queue_limits_cancel_update(mddev->gendisk->queue); + if (err) return err; - } return queue_limits_set(mddev->gendisk->queue, &lim); } diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index 042329b74c6e..3e3f64cec9ee 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -1079,7 +1079,6 @@ static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, else if (hostt->slave_configure) ret = hostt->slave_configure(sdev); if (ret) { - queue_limits_cancel_update(sdev->request_queue); /* * If the LLDD reports device not present, don't clutter the * console with failure messages. diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 6cc20ca79adc..b2542d3dcc23 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -957,18 +957,6 @@ int queue_limits_commit_update(struct request_queue *q, int queue_limits_set(struct request_queue *q, struct queue_limits *lim); int blk_validate_limits(struct queue_limits *lim); -/** - * queue_limits_cancel_update - cancel an atomic update of queue limits - * @q: queue to update - * - * This functions cancels an atomic update of the queue limits started by - * queue_limits_start_update() and should be used when an error occurs after - * starting update. - */ -static inline void queue_limits_cancel_update(struct request_queue *q) -{ -} - /* * These helpers are for drivers that have sloppy feature negotiation and might * have to disable DISCARD, WRITE_ZEROES or SECURE_DISCARD from the I/O