From patchwork Mon May 6 04:20:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13654886 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 B198343ABC for ; Mon, 6 May 2024 04:20:32 +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=1714969234; cv=none; b=k/f/sG8X4c6rpo3clCIlyDcawEtNwall0nMfVDy+H790WskVRxI2Y+ztpfoX+77kJOYjNgk/IYn3EiStda4Lqn4NIXa2pG9xVixRlJPa/6xg9WVmB20fXCLKHT4zp6Oriv5aVRDPOxd8lJhQBw4IcJc4HYpiraRtfo66+siU0Qs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714969234; c=relaxed/simple; bh=N6P/wm3g0Yu0jltiR1vPEFbEyZaGCar5+1H0lFL0rGc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i1/JliCXUu+j9HLTLePwIABjNPMbA+4ua0OGHox9/4UlEdzVxLtREv/xzZk+pBaRhZamnaE2w6XAhS+LTEkhVEbXVs+9J/iP0cYtCKLzinqUNzlDA/jE6C5tvbqENF2gvt6ond/H+ts1+2UmRktULNS5LLQWsco8vnsPrd1Lf2U= 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=a263W0XM; 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="a263W0XM" 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=D3pqLzp++9XFe0fVDALJJy2muOH3yk6YApG4Yk4oZio=; b=a263W0XM1itMweZB1pF/S0dgz3 18zEfXSlRc00uqn8q6VruU0jVwW4h4Ca/T5T5BQht1t2JIals9wnCOPXY8lXs1yRIVW/csY9AIyik Bzj4rGXqQn5AlGO41V/fHg1EapVjgGX1DYHIM5Ed4ZGWxJ+O1npfx9mlN4vufePFYkDU7vLbF7C1q N/Z6k6+ZVclKI81O+FKo9P4qp51mXTHD32eXWLtCqLtUzjEPYm4/klCLGJsQ98xPpbIbPb8nMnB7y Zy2lYrLyUfdJJe2qu4SwMu3plsf+GrvKw7n6kn4inHuDd1OBNlbxxhGNV37RZH02HSNfXKfGrVz8k gA6hRWRg==; Received: from [2001:4bb8:188:7ba8:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s3pq3-000000060KE-3rgc; Mon, 06 May 2024 04:20:32 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , linux-block@vger.kernel.org Subject: [PATCH 1/6] block: remove the discard_granularity check in __blkdev_issue_discard Date: Mon, 6 May 2024 06:20:22 +0200 Message-Id: <20240506042027.2289826-2-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240506042027.2289826-1-hch@lst.de> References: <20240506042027.2289826-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 We now set a default granularity in the queue limits API, so don't bother with this extra check. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index a6954eafb8c8af..7ec3e170e7f629 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -46,13 +46,6 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, if (!bdev_max_discard_sectors(bdev)) return -EOPNOTSUPP; - /* In case the discard granularity isn't set by buggy device driver */ - if (WARN_ON_ONCE(!bdev_discard_granularity(bdev))) { - pr_err_ratelimited("%pg: Error: discard_granularity is 0.\n", - bdev); - return -EOPNOTSUPP; - } - bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; if ((sector | nr_sects) & bs_mask) return -EINVAL; From patchwork Mon May 6 04:20:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13654887 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 5F48443ABC for ; Mon, 6 May 2024 04:20:35 +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=1714969236; cv=none; b=NEHuoVF5WutxQXZ3QCZqlXcZFCw5zg1BYMMR36IvBOVUlE2lMRMqoKdENQBVZZa290u6t+HpgjCaXxd1eoldq+i4l3Xjv6ZJDtSyU2KwjknxLwtiwBJ4w9ZpShyZq6Ic2lQQVD6I2O/Zmm0nfn+kFpHSTaJfsJN1T6K9LCMYpPM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714969236; c=relaxed/simple; bh=E0DYUDG4l9aEK60M12y3MAsyF1oy8ZxKY/5Si+eyptU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pr2mbsjoysNCQX71Vqm9nhMMmVOjpYBZgg9MFvockUi82GakXuRZzFf8zEKnjYMEGvA7mCriryPa8bMRnpAzgt63cNv9E0bcYrQbAZqttraJRC0275IDZRIdeMt4MQw1mg4kyeY8ZMxTnK7kEJo28VXV2MyhgZlpM16lBuZBG9M= 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=dcumZn/2; 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="dcumZn/2" 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=R9xSF1MXch23RfbVp2uK3KxJeMtGVod4GHpDLdUAsLI=; b=dcumZn/2q0+oS7omkOnNRuH3vq ZzQU7rmXIawHnDFYu98HL9taGzk40PH1D0HNEEQ3EEdlVEKBpbMjepPD8JPYyi6nSejdt6Mn32eLS /k50IkuAw/Yy2Zf4scRM2WlGkf6wPx5eeGiVsq8u/OU77ViXVWQWluq3l1VEuK3f3P0Ho7hWr4UrF 0QgVjpqkVALLyik1djY+n9/+Hq3/W6BrS+9tTN30wfitswz+Ju1qObUGYkh6dfPPXu7Lu921VX/JN Y1OdOWtwfCKCgzE+uPSwow9v7medFZn83u8M5+ClIHAlMhb64HGmAPa1Mq439Vigk+MwTS0BFaA9s BEVue0uw==; Received: from [2001:4bb8:188:7ba8:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s3pq6-000000060KQ-1bTG; Mon, 06 May 2024 04:20:34 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , linux-block@vger.kernel.org Subject: [PATCH 2/6] block: move discard checks into the ioctl handler Date: Mon, 6 May 2024 06:20:23 +0200 Message-Id: <20240506042027.2289826-3-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240506042027.2289826-1-hch@lst.de> References: <20240506042027.2289826-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 Most bio operations get basic sanity checking in submit_bio and anything more complicated than that is done in the callers. Discards are a bit different from that in that a lot of checking is done in __blkdev_issue_discard, and the specific errnos for that are returned to userspace. Move the checks that require specific errnos to the ioctl handler instead, and just leave the basic sanity checking in submit_bio for the other handlers. This introduces two changes in behavior: 1) the logical block size alignment check of the start and len is lost for non-ioctl callers. This matches what is done for other operations including reads and writes. We should probably verify this for all bios, but for now make discards match the normal flow. 2) for non-ioctl callers all errors are reported on I/O completion now instead of synchronously. Callers in general mostly ignore or log errors so this will actually simplify the code once cleaned up Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 13 ------------- block/ioctl.c | 7 +++++-- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 7ec3e170e7f629..6e54ef140bab12 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -39,19 +39,6 @@ int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop) { struct bio *bio = *biop; - sector_t bs_mask; - - if (bdev_read_only(bdev)) - return -EPERM; - if (!bdev_max_discard_sectors(bdev)) - return -EOPNOTSUPP; - - bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; - if ((sector | nr_sects) & bs_mask) - return -EINVAL; - - if (!nr_sects) - return -EINVAL; while (nr_sects) { sector_t req_sects = diff --git a/block/ioctl.c b/block/ioctl.c index 0c76137adcaaa5..03bcdf2783b508 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -95,6 +95,7 @@ static int compat_blkpg_ioctl(struct block_device *bdev, static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, unsigned long arg) { + unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; uint64_t range[2]; uint64_t start, len; struct inode *inode = bdev->bd_inode; @@ -105,6 +106,8 @@ static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, if (!bdev_max_discard_sectors(bdev)) return -EOPNOTSUPP; + if (bdev_read_only(bdev)) + return -EPERM; if (copy_from_user(range, (void __user *)arg, sizeof(range))) return -EFAULT; @@ -112,9 +115,9 @@ static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, start = range[0]; len = range[1]; - if (start & 511) + if (!len) return -EINVAL; - if (len & 511) + if ((start | len) & bs_mask) return -EINVAL; if (start + len > bdev_nr_bytes(bdev)) From patchwork Mon May 6 04:20:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13654888 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 E204243ABC for ; Mon, 6 May 2024 04:20:37 +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=1714969240; cv=none; b=FB4FkjBj+W+zFsdeCaoP3VxTURDjfPPCsVMXBWp/YOfRRiZDnGFooUWolbjpzSXOlBVSlFbQB8Ev5vVJ2h3+w7S9ewU0KaWI54Da1Ht5M7bCLzyoRjE927KFxA1LEquzG2bUTYwAYA2xsMSFUTapTXwbJfV6ut8fqqB9TGCUR70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714969240; c=relaxed/simple; bh=+XOmUvqvbkxshVMwbWkiNmY89oiwHlp0WmLP0Ol5ZMg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qboBCS7eLiuwxvSqHoUS0xlL25mNyEcg6BrNZ2X1CYViR4Zy65bLf9U01VJPkjqIF1JTXt2q7IcymPoyWL/twttA0m0Bh8NJ9wXlNb6/fjXaxsyVIWGLduVl0EppeQXDjdIimR7Lq+aGUJA9qXvqIcHkGrRIy29W0IewVkYtSbc= 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=TCZyNO/e; 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="TCZyNO/e" 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=r7j69I3xEuicF0tvAgL59fBhWIatWNlkiAB+VJk1FPw=; b=TCZyNO/e7Ovxw7e50+soXa8oa9 JueuDlGABmRkGRT6tM5/K8v22KDBpvu2hXqC/Kb7JaORYff6/rfF5k1x9c42V5esomdp7rkDKpVEc Xd8TIG6dbSHmoRjdbCs9FS005/MPUdUKeK/Cw3PX65hZRqlOtoiaETLw0bD6TLyGAo/g0VlE2TPaQ 3dv4uHG1Ufai6KY3lH0MlpL1BlakdSH4kKI9/Vm+GOH7rftarneQq+5tBSDY7rWjFe+8AHFq0+eRC YDjIRdsyF4MuRKzXD9td7xwGviFs/plprWVaY5o3YI/gYMvvIgI2EOmD7zGMl54eqU4dp+uQ7TvLy 42BN4wog==; Received: from [2001:4bb8:188:7ba8:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s3pq8-000000060L6-3uOM; Mon, 06 May 2024 04:20:37 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , linux-block@vger.kernel.org Subject: [PATCH 3/6] block: add a bio_chain_and_submit helper Date: Mon, 6 May 2024 06:20:24 +0200 Message-Id: <20240506042027.2289826-4-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240506042027.2289826-1-hch@lst.de> References: <20240506042027.2289826-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 This is basically blk_next_bio just with the bio allocation moved to the caller to allow for more flexible bio handling in the caller. Signed-off-by: Christoph Hellwig --- block/bio.c | 27 +++++++++++++++++++-------- include/linux/bio.h | 1 + 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/block/bio.c b/block/bio.c index 38baedb39c6f2e..d82ef4fd545cb2 100644 --- a/block/bio.c +++ b/block/bio.c @@ -345,18 +345,29 @@ void bio_chain(struct bio *bio, struct bio *parent) } EXPORT_SYMBOL(bio_chain); -struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev, - unsigned int nr_pages, blk_opf_t opf, gfp_t gfp) +/** + * bio_chain_and_submit - submit a bio after chaining it to another one + * @prev: bio to chain and submit + * @new: bio to chain to + * + * If @prev is non-NULL, chain it to @new and submit it. + * + * Return: @new. + */ +struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new) { - struct bio *new = bio_alloc(bdev, nr_pages, opf, gfp); - - if (bio) { - bio_chain(bio, new); - submit_bio(bio); + if (prev) { + bio_chain(prev, new); + submit_bio(prev); } - return new; } + +struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev, + unsigned int nr_pages, blk_opf_t opf, gfp_t gfp) +{ + return bio_chain_and_submit(bio, bio_alloc(bdev, nr_pages, opf, gfp)); +} EXPORT_SYMBOL_GPL(blk_next_bio); static void bio_alloc_rescue(struct work_struct *work) diff --git a/include/linux/bio.h b/include/linux/bio.h index 9b8a369f44bc6b..283a0dcbd1de61 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -831,5 +831,6 @@ static inline void bio_clear_polled(struct bio *bio) struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev, unsigned int nr_pages, blk_opf_t opf, gfp_t gfp); +struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new); #endif /* __LINUX_BIO_H */ From patchwork Mon May 6 04:20:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13654889 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 29A0C4438F for ; Mon, 6 May 2024 04:20: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=1714969241; cv=none; b=FwKSeG9v9Ctge61ZNgZy434PO0rtKb5z4IGC4tQF0dqzDFSLs47CsVGaANArcF+60bBxkB9K0uZyfF0iwpxYj76ErSodgNf0P56jm6V6ZjLvcOPYBwCtarrsXEo+xX/ZHauTwBJ+V20z6OVOOyHCujXEoINIaswthx6Im2dxggE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714969241; c=relaxed/simple; bh=lZ2aYOiOeiS/dZxOf3HMKTIfQMlYbmtUo0dTbY4rvWw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=M7qjOjGYxo07UziT34zk1HR6fbL46noz3x3ExfvOAcreR+XwgfbYrAbaWu1MRgaLWDiIcvUrf9qoq1Jh21MxwwgHnzx57hAMrjfEdK2WU/btMxgHXi4svV6/Ps9RTvNU2tvoD00UOazsoR5TCG2e2rxOaEJ0FDgRmpOrrkcaxyE= 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=4BpneM7p; 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="4BpneM7p" 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=fMnFSEDCUvnQjVZZmZCJLOtQ7q7a0ZyHj9ZXK9KdIFM=; b=4BpneM7pV/+lgUF+uJgy60OBFb 6KEHG7eS8MDUi+cTCQYLHKXk0BmsauyTqPQUNjiPlJk/yDVUPqPXUwENknf1RCsh6fWYAeKqk23wu gX+5ROCjJ5UxqDsq+rA6QV+fX1e49ALJ9Ns81JafiWVvwu7xMdHKNOh1JOWY64VFuIlFbQmNye4ek wsUN2CcqaAP6yAUGQjh5CAVUraIUogE4BY8jjvW++1cPg234Xdm8I86r/iRQGrHGueiqZ4DppW7/C Jf4hg5VLNvI3/qEPGJe+2lrnDWZSNhZOaIVjZNvzD1TWTzGhUMrQBSDJHvz3nJdaZ9Aja22dmLDks duwA6P1g==; Received: from [2001:4bb8:188:7ba8:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s3pqB-000000060Ll-1lLu; Mon, 06 May 2024 04:20:39 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , linux-block@vger.kernel.org Subject: [PATCH 4/6] block: add a blk_alloc_discard_bio helper Date: Mon, 6 May 2024 06:20:25 +0200 Message-Id: <20240506042027.2289826-5-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240506042027.2289826-1-hch@lst.de> References: <20240506042027.2289826-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 Factor out a helper from __blkdev_issue_discard that chews off as much as possible from a discard range and allocates a bio for it. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 50 ++++++++++++++++++++++++++------------------- include/linux/bio.h | 3 +++ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 6e54ef140bab12..442da9dad04213 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -35,31 +35,39 @@ static sector_t bio_discard_limit(struct block_device *bdev, sector_t sector) return round_down(UINT_MAX, discard_granularity) >> SECTOR_SHIFT; } -int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, - sector_t nr_sects, gfp_t gfp_mask, struct bio **biop) +struct bio *blk_alloc_discard_bio(struct block_device *bdev, + sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask) { - struct bio *bio = *biop; + sector_t bio_sects = min(*nr_sects, bio_discard_limit(bdev, *sector)); + struct bio *bio; - while (nr_sects) { - sector_t req_sects = - min(nr_sects, bio_discard_limit(bdev, sector)); + if (!bio_sects) + return NULL; - bio = blk_next_bio(bio, bdev, 0, REQ_OP_DISCARD, gfp_mask); - bio->bi_iter.bi_sector = sector; - bio->bi_iter.bi_size = req_sects << 9; - sector += req_sects; - nr_sects -= req_sects; - - /* - * We can loop for a long time in here, if someone does - * full device discards (like mkfs). Be nice and allow - * us to schedule out to avoid softlocking if preempt - * is disabled. - */ - cond_resched(); - } + bio = bio_alloc(bdev, 0, REQ_OP_DISCARD, gfp_mask); + if (!bio) + return NULL; + bio->bi_iter.bi_sector = *sector; + bio->bi_iter.bi_size = bio_sects << SECTOR_SHIFT; + *sector += bio_sects; + *nr_sects -= bio_sects; + /* + * We can loop for a long time in here if someone does full device + * discards (like mkfs). Be nice and allow us to schedule out to avoid + * softlocking if preempt is disabled. + */ + cond_resched(); + return bio; +} - *biop = bio; +int __blkdev_issue_discard(struct block_device *bdev, sector_t sector, + sector_t nr_sects, gfp_t gfp_mask, struct bio **biop) +{ + struct bio *bio; + + while ((bio = blk_alloc_discard_bio(bdev, §or, &nr_sects, + gfp_mask))) + *biop = bio_chain_and_submit(*biop, bio); return 0; } EXPORT_SYMBOL(__blkdev_issue_discard); diff --git a/include/linux/bio.h b/include/linux/bio.h index 283a0dcbd1de61..d5379548d684e1 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -833,4 +833,7 @@ struct bio *blk_next_bio(struct bio *bio, struct block_device *bdev, unsigned int nr_pages, blk_opf_t opf, gfp_t gfp); struct bio *bio_chain_and_submit(struct bio *prev, struct bio *new); +struct bio *blk_alloc_discard_bio(struct block_device *bdev, + sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask); + #endif /* __LINUX_BIO_H */ From patchwork Mon May 6 04:20:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13654890 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 C37D64503B for ; Mon, 6 May 2024 04:20:42 +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=1714969244; cv=none; b=fhThIEAe1Wz8HX3f4GaC67/PM9oXEk2VscbsbzRwdcy2N3c1xewJb+oc/v8BunEF6+Wjd1jZfn/tZMhLCKikOdtG0b3nrFRg3LiAF5Fo/QXdmQV3eOjNta4aSJeeB5PqHuLXeBfucyt7QI/Xz3jYeQ9UaCOZu5ZnHHTtdS8SyGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714969244; c=relaxed/simple; bh=QhqhFXk85lPTTZsdhSj7N4YrSdpQ4Fu8FYAmOTsD/hQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=deijMCxVQ/9P9M6PDfs4zZNIvoijW9VWygqG2jbxK5fVGDfvC/ogzXIuvetwVXmABbWhJkUJW0gelbr8460GCdQWmPzpAppazH1QCI4QrmX93Sk4SOb66sKhsSpt0uGRR58FptnpUrt5U0vPSpzeJ0ce3C/i8K8BadArRrOXiDY= 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=JPgOBDRG; 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="JPgOBDRG" 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=tV22VCmL8WFPyjffeM+IMToxy9rDQt+mlWu4HhQ2xus=; b=JPgOBDRGUS3veosPRjyUUDRQsJ QHtAM1JjGN51rUytbYhj0O1K5mO56/qiX+V5jtgwaxkIr+LMzQoKEx5T0Sspl1S4rRTTWk/1UXPty 5RruUbvyq8JjweVT+8GgfS3B0+rSWxt4UuhoJgPBkl8zODUvOUT8iN1gLB7Oxseuo8b2rtTMoZo22 yryU9foOzfa64tIbwaXztt4ESHZhkq1gGWXX0ZlerZeZCJCidIu+xgaAU3FSPLq3z80yBBLm2p1L/ I1kCopR4eQcv/VhjFXOt0ilKWsT0Klxa90kjKI3ROg3eOkbxqU0tglBn3j0hq4R+qrhvrMI8tfRJd 8bkJrw1w==; Received: from [2001:4bb8:188:7ba8:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s3pqD-000000060Me-3zYu; Mon, 06 May 2024 04:20:42 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , linux-block@vger.kernel.org Subject: [PATCH 5/6] block: add a bio_await_chain helper Date: Mon, 6 May 2024 06:20:26 +0200 Message-Id: <20240506042027.2289826-6-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240506042027.2289826-1-hch@lst.de> References: <20240506042027.2289826-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 From: Keith Busch Add a helper to wait for an entire chain of bios to complete. Signed-off-by: Keith Busch [hch: split from a larger patch, moved and changed the name now that it is non-static] Signed-off-by: Christoph Hellwig --- block/bio.c | 20 ++++++++++++++++++++ block/blk.h | 1 + 2 files changed, 21 insertions(+) diff --git a/block/bio.c b/block/bio.c index d82ef4fd545cb2..dce12a0efdead2 100644 --- a/block/bio.c +++ b/block/bio.c @@ -1395,6 +1395,26 @@ int submit_bio_wait(struct bio *bio) } EXPORT_SYMBOL(submit_bio_wait); +static void bio_wait_end_io(struct bio *bio) +{ + complete(bio->bi_private); + bio_put(bio); +} + +/* + * bio_await_chain - ends @bio and waits for every chained bio to complete + */ +void bio_await_chain(struct bio *bio) +{ + DECLARE_COMPLETION_ONSTACK_MAP(done, + bio->bi_bdev->bd_disk->lockdep_map); + + bio->bi_private = &done; + bio->bi_end_io = bio_wait_end_io; + bio_endio(bio); + blk_wait_io(&done); +} + void __bio_advance(struct bio *bio, unsigned bytes) { if (bio_integrity(bio)) diff --git a/block/blk.h b/block/blk.h index ee4f782d149662..d5107e65355e27 100644 --- a/block/blk.h +++ b/block/blk.h @@ -38,6 +38,7 @@ void __blk_mq_unfreeze_queue(struct request_queue *q, bool force_atomic); void blk_queue_start_drain(struct request_queue *q); int __bio_queue_enter(struct request_queue *q, struct bio *bio); void submit_bio_noacct_nocheck(struct bio *bio); +void bio_await_chain(struct bio *bio); static inline bool blk_try_enter_queue(struct request_queue *q, bool pm) { From patchwork Mon May 6 04:20:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13654891 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 CC6DA43AD4 for ; Mon, 6 May 2024 04:20: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=1714969247; cv=none; b=ks0cJA8KKLyuTwQF2AqK+lgIrLg/YPGyPbG2iybpYLZw5GqKzphD5hcUYGnl3vdOsckAM958qPj/5TTLQjKG7A6MIRIxVDdn45EJda6fV97LFTWwsFNke+OwCV56X5h0st9tbcLd4yoKksE1C+UNm690HHIxPcQys1IlZJwnBRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714969247; c=relaxed/simple; bh=/2tBtxZzSDxn5DgC3zUtJp9JJrlJvFs0jB1tGkV3u+A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a8HNEt1ByV7mChJ9GRvNzOQ7NXg6fqqsfVFrH4CAMMbpSpwrj6tfXnPXTJHvgpaJ0w61sVcSNKNGXW88YwFYcw/ClfflpcixciThs5gfMbAkpfeHxLvbHYG8EY0uruYY1ucFV53IdjVgNafyIaoNjV6KNNHaAjE2A24mFSJkq9w= 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=fWadFoyo; 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="fWadFoyo" 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=ICzvPLnKCjqbvrzst2txWHdjNu7MZMnaZDVGNIQ6QRU=; b=fWadFoyohaHY1Ew0WRdGfwxYkl uTlznNb0AM6aFtg0Z/Ef8FvziF+tO5n/lEcSwPtSfNgVLNKjJ0QY80YkexKLjPRpfHjVM1z/Dz8zi 8VznWMQBtpAe0hp7jLYWlBgvEuKwwu0RZn/+AkxpK42bfyGipuc87pJziyt0hhq8C4d0y7ghXTnZB CKvz/BoFIhIvL4oW6IQjH/nKZZigz9Bm2s44wSKyRm2IIOlBa56ohE6Fhu/edC021tDWlsxYlf4d4 wrYPx6kLbGXSsbj/Kz/1KtpxtQvmyLn1pKP6L41krlpVd5JAITSP9tvYfQH6tF/kVfqQPaGshggck +T94HDSA==; Received: from [2001:4bb8:188:7ba8:c70:4a89:bc61:3] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s3pqG-000000060Nd-21HP; Mon, 06 May 2024 04:20:45 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , linux-block@vger.kernel.org Subject: [PATCH 6/6] blk-lib: check for kill signal in ioctl BLKDISCARD Date: Mon, 6 May 2024 06:20:27 +0200 Message-Id: <20240506042027.2289826-7-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240506042027.2289826-1-hch@lst.de> References: <20240506042027.2289826-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 Discards can access a significant capacity and take longer than the user expected. A user may change their mind about wanting to run that command and attempt to kill the process and do something else with their device. But since the task is uninterruptable, they have to wait for it to finish, which could be many hours. Open code blkdev_issue_discard in the BLKDISCARD ioctl handler and check for a fatal signal at each iteration so the user doesn't have to wait for their regretted operation to complete naturally. Heavily based on an earlier patch from Keith Busch. Reported-by: Conrad Meyer Signed-off-by: Christoph Hellwig --- block/ioctl.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index 03bcdf2783b508..003d134779db56 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -96,9 +96,11 @@ static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, unsigned long arg) { unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; - uint64_t range[2]; - uint64_t start, len; struct inode *inode = bdev->bd_inode; + uint64_t range[2], start, len; + struct bio *prev = NULL, *bio; + sector_t sector, nr_sects; + struct blk_plug plug; int err; if (!(mode & BLK_OPEN_WRITE)) @@ -127,7 +129,32 @@ static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, err = truncate_bdev_range(bdev, mode, start, start + len - 1); if (err) goto fail; - err = blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL); + + sector = start >> SECTOR_SHIFT; + nr_sects = len >> SECTOR_SHIFT; + + blk_start_plug(&plug); + while (1) { + if (fatal_signal_pending(current)) { + if (prev) + bio_await_chain(prev); + err = -EINTR; + goto out_unplug; + } + bio = blk_alloc_discard_bio(bdev, §or, &nr_sects, + GFP_KERNEL); + if (!bio) + break; + prev = bio_chain_and_submit(prev, bio); + } + if (prev) { + err = submit_bio_wait(prev); + if (err == -EOPNOTSUPP) + err = 0; + bio_put(prev); + } +out_unplug: + blk_finish_plug(&plug); fail: filemap_invalidate_unlock(inode->i_mapping); return err;