From patchwork Mon Jul 1 16:51:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718411 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 EE2F616DC39; Mon, 1 Jul 2024 16:52:29 +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=1719852752; cv=none; b=kRvhTCzE30b+JGkjB20ybvvGcFavLKVMC6nR+Jt8JUwiTytjtVM4/n0B/Vq5m3qo/x2zVTW4y8zNDSb1o96AFTHkvvwznDky+m7WXgzfBdSskSwmtzDX0Kty/YO9MDHtGD8hv6/nMJvxwfJ0hV3I9OHN89VNk8uyDMV5IWHMKLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852752; c=relaxed/simple; bh=Z1Z+c7oX0SXs1iAkR0IHpp7OVbRRdK9czuSKENBknOY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A6SoVOXy5l4fyRUsE/qZyQX4ruG6S9u7s8C84VVbcf56nP2i+RwKYpH8gMNOUEmozzS+gzlk9qmahntd8CMPSJd2QBXzT4QhUQMiKCjz1iUVUYCm0/2oEd1+4E1LkIPv/TxuDRDl0Ev2OeDz1Q7DXiR8WQbNteCmyY3o7R+ehUI= 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=tJrGTkEU; 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="tJrGTkEU" 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=NVVaxtBxinuCwMKgz8pxtUN+Le0Sbdm4BC3SfXv8GYA=; b=tJrGTkEUv9bPIWdi66FnNiyPtD xcpCjQblXfGi2zsYnlUFn/pvxzjiBURa1VUjeNB/oOS1JZKQAOeIv8ymumKKcJxHYKXddItCS1mrb 0ABvkRH1a/pDcgGGFTDgSxJtCPVFfpyOpKrpzOBrYiak8nm21wp+oJ2CKx2Ja65PIQ4tMHyd1HHoM qk9JIk4960OBtahU55B68PMuiXjIOGvzqOFWLZ7z/ZIsKT3K/V0xwXxTP3SJaoZlRTbM1c9PD8Qrb qgNHFVO3BH37WCbcRtvk1qdlWpJuvDG52CuqCTayZEucw1zHQalhMXSYY/HnrHxHBS1p6d0W5C5P9 wABRqtSQ==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGR-00000004BYA-3XIu; Mon, 01 Jul 2024 16:52:28 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 01/10] block: move secure erase checks into the ioctl handler Date: Mon, 1 Jul 2024 18:51:11 +0200 Message-ID: <20240701165219.1571322-2-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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. Secure erase is a bit different from that in that a lot of checking is done in blkdev_issue_secure_erase, 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 Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 7 ------- block/ioctl.c | 8 +++++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 442da9dad04213..4aabfc4a7eaa20 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -299,13 +299,6 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, max_sectors = UINT_MAX >> SECTOR_SHIFT; max_sectors &= ~bs_mask; - if (max_sectors == 0) - return -EOPNOTSUPP; - if ((sector | nr_sects) & bs_mask) - return -EINVAL; - if (bdev_read_only(bdev)) - return -EPERM; - blk_start_plug(&plug); while (nr_sects) { unsigned int len = min_t(sector_t, nr_sects, max_sectors); diff --git a/block/ioctl.c b/block/ioctl.c index d570e16958961e..f53121edb9a15f 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -163,6 +163,7 @@ static int blk_ioctl_discard(struct block_device *bdev, blk_mode_t mode, static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode, void __user *argp) { + unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; uint64_t start, len; uint64_t range[2]; int err; @@ -171,12 +172,17 @@ static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode, return -EBADF; if (!bdev_max_secure_erase_sectors(bdev)) return -EOPNOTSUPP; + if (bdev_read_only(bdev)) + return -EPERM; if (copy_from_user(range, argp, sizeof(range))) return -EFAULT; start = range[0]; len = range[1]; - if ((start & 511) || (len & 511)) + + if (!len) + return -EINVAL; + if ((start | len) & bs_mask) return -EINVAL; if (start + len > bdev_nr_bytes(bdev)) return -EINVAL; From patchwork Mon Jul 1 16:51:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718412 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 9D61816DC28; Mon, 1 Jul 2024 16:52:33 +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=1719852755; cv=none; b=bAMWNukrIcEqP4t8hwei1DeZTWfiD+Pru6g6SatGbHZcM00QX4I9gD5eBpua4jmbAHherB7etrO+oMI/QovjLmfuS7gx5KOM6ci+CHv7YZKHyqHFyl4/0sit9WqvaHbJie/q2h2en1LP+S8xMH6LSglZtCOipqdJEabJq7z0sok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852755; c=relaxed/simple; bh=VCx3oj3J1gwuO8Ki+SrjUqw3M0UviFUVVT2vZ0j7I2A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lf/pKReGeX+kO+ZBYrxpvpDqN5+qsuR93Htm6te8TjmspjYne85upd1MYNmhA2vjq3N8s7EYqn+RSqkCN4n3SDx9U16i9Q2kXmbCjF/SsZHJXYH0lQJuJc3HxUwpFTT6v8l8RvvvOWdZPxoR4aWGodrUROZwtjSwuCk1iEYUz5k= 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=lroJqfFj; 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="lroJqfFj" 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=Q9sAMTxUEzTcvcgUTbBquYG+NeVvB6IffN/QzKQPocs=; b=lroJqfFjKDjvg1tX+YakPJXKmt 6guRRbdfGA7qFGmZZvcgl6VYe9tOzrZa+IARKFL/MsDC67yNDz1EYCHFYroSwPkZAvLWgdnAqa4Tk WSrx2PoR4xqSQYFCCifv6k/zYDFD9sSwu7WsAfc6VsIZ/2ppBVKLiXko3YTs1fKlrUx3DSMFCHqAI kbViVSRuBKkxFrlfFrWyWNKTGmDd9Y5Kg0QtXvrrUa6sWODMDBH8hdHsYesMF7NF9rCY6ahv+snlF mf82Wsg1i7sXG8cy4zZhBTQaqPuLlDo9SRsg/oS3fRZfJjYgIMW5DFr/VfCbAu8e61Tw0OmFTXOLb bZDwYlxw==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGV-00000004Bam-3wSu; Mon, 01 Jul 2024 16:52:32 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 02/10] block: factor out a bio_secure_erase_limit helper Date: Mon, 1 Jul 2024 18:51:12 +0200 Message-ID: <20240701165219.1571322-3-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 Add a helper to size the maximum secure erase bio without exceeding the queue limit or overlowing the bi_size member. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 4aabfc4a7eaa20..ff12c3d2de5aae 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -285,20 +285,22 @@ int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, } EXPORT_SYMBOL(blkdev_issue_zeroout); +static sector_t bio_secure_erase_limit(struct block_device *bdev) +{ + sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; + + return min(bdev_max_secure_erase_sectors(bdev), + (UINT_MAX >> SECTOR_SHIFT) & ~bs_mask); +} + int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp) { - sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; - unsigned int max_sectors = bdev_max_secure_erase_sectors(bdev); + unsigned int max_sectors = bio_secure_erase_limit(bdev); struct bio *bio = NULL; struct blk_plug plug; int ret = 0; - /* make sure that "len << SECTOR_SHIFT" doesn't overflow */ - if (max_sectors > UINT_MAX >> SECTOR_SHIFT) - max_sectors = UINT_MAX >> SECTOR_SHIFT; - max_sectors &= ~bs_mask; - blk_start_plug(&plug); while (nr_sects) { unsigned int len = min_t(sector_t, nr_sects, max_sectors); From patchwork Mon Jul 1 16:51:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718413 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 1458C16DEA5; Mon, 1 Jul 2024 16:52: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=1719852759; cv=none; b=PJIIXMwOuZBT+ESwKWeoF7A7Q66xPw8hxLa0ruRimeNg6kWMrCwq0MkpzszPris1OdSyO4S2ogMd2tW3DB/l0VKZUvUuMNdspG8gQpCyQBeqZcS/BTkhK/XtJzhYedbEJJZdkEatOCh2YrHONMCN3noq1icPBs5KNKOuBOqcS0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852759; c=relaxed/simple; bh=G5L5Plq19X5GAfeTFNcX9uW9PU0zoLG5mzqe5KDW/+c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cIcRAC9+Yl0eKiYVE8jWRaQdnNxOc/ad46+nq1cpFbECkcGuNI7lfhdVjsZHmdiz/ZPCe5DVy1EkJBPChzc1evnHn9P/ujVrq1tXMsX8KmCsP9mUs0H0UsvKUOAaJnW0PMk742Aj0do9rOv+OWnw9fE92VLPFOorMiBAucnuhn4= 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=34MTjUIP; 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="34MTjUIP" 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=DwD7MjTvJx+hlHOHPGd5dcyVJZrseYQmaHXTz3yhQ8Y=; b=34MTjUIPfC/B8bUTCiroNNKR+9 yir0icGQTNEiZnH+dfKQO58vG0s4AH3KUiWkMWtkfwXOA1X7XmnYxzRnGo3hj9IBz3EMBg2rcf+dj SIP2yT4eThKCMjqHmQCyPASi/7RtLzLQZNs5wbFlmhpxqYtuhdvjb86O5rbWCSnmlB+5aB7yuQnuf vfnAoi1rNai4sUKWS7yOI+aghKOHoGtg7nJrETtTKOTYiti8C6+qYKMqY1lcBmr8O+DZy0DeGmaoZ 5DEFrr9K6gWiApsVt2/UMlKv1wr4H+Cla2zEamyD9Nn9OeZ9t+hvQTzIaZc6QNKpobd9JEJvIyEHQ x8Y0wlUQ==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGZ-00000004Bcj-33T6; Mon, 01 Jul 2024 16:52:36 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 03/10] block: add a blk_alloc_secure_erase_bio helper Date: Mon, 1 Jul 2024 18:51:13 +0200 Message-ID: <20240701165219.1571322-4-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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_secure_erase that chews off as much as possible from a secure_erase range and allocates a bio for it. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 36 +++++++++++++++++++++++------------- include/linux/bio.h | 2 ++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index ff12c3d2de5aae..cf4f0ee6b14503 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -293,26 +293,36 @@ static sector_t bio_secure_erase_limit(struct block_device *bdev) (UINT_MAX >> SECTOR_SHIFT) & ~bs_mask); } +struct bio *blk_alloc_secure_erase_bio(struct block_device *bdev, + sector_t *sector, sector_t *nr_sects, gfp_t gfp) +{ + sector_t bio_sects = min(*nr_sects, bio_secure_erase_limit(bdev)); + struct bio *bio; + + if (!bio_sects) + return NULL; + bio = bio_alloc(bdev, 0, REQ_OP_SECURE_ERASE, gfp); + 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; + cond_resched(); + return bio; +} + int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp) { - unsigned int max_sectors = bio_secure_erase_limit(bdev); - struct bio *bio = NULL; + struct bio *bio = NULL, *next; struct blk_plug plug; int ret = 0; blk_start_plug(&plug); - while (nr_sects) { - unsigned int len = min_t(sector_t, nr_sects, max_sectors); - - bio = blk_next_bio(bio, bdev, 0, REQ_OP_SECURE_ERASE, gfp); - bio->bi_iter.bi_sector = sector; - bio->bi_iter.bi_size = len << SECTOR_SHIFT; - - sector += len; - nr_sects -= len; - cond_resched(); - } + while ((next = blk_alloc_secure_erase_bio(bdev, §or, &nr_sects, + gfp))) + bio = bio_chain_and_submit(bio, next); if (bio) { ret = submit_bio_wait(bio); bio_put(bio); diff --git a/include/linux/bio.h b/include/linux/bio.h index d5379548d684e1..e1da5fe49baca1 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -835,5 +835,7 @@ 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); +struct bio *blk_alloc_secure_erase_bio(struct block_device *bdev, + sector_t *sector, sector_t *nr_sects, gfp_t gfp); #endif /* __LINUX_BIO_H */ From patchwork Mon Jul 1 16:51:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718414 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 3039B16DC35; Mon, 1 Jul 2024 16:52:41 +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=1719852762; cv=none; b=mEmPMlHzHAWJ4iXwzEJDP3XK56eX0vBFIDmU6fec3Ref9N3Tf4JF7TcPoI5SZHf8DLQtYvwTIFsoAaqItZQPBtgYlELGvtfQXbcZHIS7LOoB9Slb6ElKzF6TYij7bY7YD03ugn6IZruWTlqJ/44v9aHUUL7kFWEZ0lfolpG0HWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852762; c=relaxed/simple; bh=VEMRQwfeIWs70rHujqfrqoaMQ9Uh6Mfmgvpl96QOpwM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QK6FtPek+anfOVHxYQGwRQA51ibCz2ZRSpOlO0n9/x67AU38IZSme79zsLzSbyIegoS3ruI0zDzS7aoshkHEi6aXhljvTbuCvypIg4XKV6Hs25u2xsod6Z5+4ixhBbcVwadoTLk3jXLIV/H69H5HXoTPuGknMbLqefgqgjCBpDc= 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=3zbsQa4i; 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="3zbsQa4i" 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=Oqdc6V8xwAdvyBJbY3RHzAse+UGgP4u6Afcst4sdYMk=; b=3zbsQa4iYn/8FdgbtQAMm9GHjz /NqRlhiaxbEOMvyQOxPRJyhjZWOK81jVMi5yS/NGVCQBL5bi2vIK1TC1ba7mfkAZYGbfgaU0dV1Vy y95UDYEu0CrJMEckAwUXGp0c2EO/TjLGxGNphVFJIItIUMZZU9vGjm7yQh2VBXRkX73K9u9e8k9hV ooAYPwwWvYD+9la9ClcSV1kJGVNp4qmVF2pWusl4h2OY6BFXu4WJMmcd+vw3QZ6lVpDABotTft5ja GdvVI1MgC/51iCOSuni4G4WfwNlh4xa5G//R0z6xzVFtMHnP6yTksijHHA+QMEfqnx7/lP5TOW4Zt V0b6pxbg==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGd-00000004Bdw-2AQA; Mon, 01 Jul 2024 16:52:40 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 04/10] blk-lib: check for kill signal in ioctl BLKSECDISCARD Date: Mon, 1 Jul 2024 18:51:14 +0200 Message-ID: <20240701165219.1571322-5-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 Secure Erase 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_secure_erase in the BLKSECDISCARD 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 | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/block/ioctl.c b/block/ioctl.c index f53121edb9a15f..45668a21cdb374 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -164,6 +164,9 @@ static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode, void __user *argp) { unsigned int bs_mask = bdev_logical_block_size(bdev) - 1; + struct bio *prev = NULL, *bio; + sector_t sector, nr_sects; + struct blk_plug plug; uint64_t start, len; uint64_t range[2]; int err; @@ -187,11 +190,36 @@ static int blk_ioctl_secure_erase(struct block_device *bdev, blk_mode_t mode, if (start + len > bdev_nr_bytes(bdev)) return -EINVAL; + sector = start >> SECTOR_SHIFT; + nr_sects = len >> SECTOR_SHIFT; + filemap_invalidate_lock(bdev->bd_mapping); err = truncate_bdev_range(bdev, mode, start, start + len - 1); - if (!err) - err = blkdev_issue_secure_erase(bdev, start >> 9, len >> 9, - GFP_KERNEL); + if (err) + goto out_unlock; + + 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_secure_erase_bio(bdev, §or, &nr_sects, + GFP_KERNEL); + if (!bio) + break; + prev = bio_chain_and_submit(prev, bio); + } + if (prev) { + err = submit_bio_wait(prev); + bio_put(prev); + } + +out_unplug: + blk_finish_plug(&plug); +out_unlock: filemap_invalidate_unlock(bdev->bd_mapping); return err; } From patchwork Mon Jul 1 16:51:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718415 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 50D8516D4F4; Mon, 1 Jul 2024 16:52: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=1719852766; cv=none; b=h2TjbfpJUgy/7X1yqs95PUoasUFgPHrYCxhjyJl1mzbmqOJlQMx//hpV97qs+HNoVG/XF9pYjcpCiY1QqONKmi2YgzICap6kPPEQ5j4hbv0CmFOAPNA48Dv0d3A0f1wFtOOtZXnLQSyae553PJkZBF5ULe/r1Uk1oVadwXNUk/I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852766; c=relaxed/simple; bh=bkBqvU8+udzDY3YWr9bvQ3s+zzZXwra3PaEaBxKtj2k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lghOv3QssqZ/KLj4DBoWEPCtiMF92IZrIaXYJjXY1Pg9uYyQhQha75FwKkUB4QCbC05ukadpMYEgklyoq2zH5OJ2W4hcJtL3dMwtDCGJgMVYa6Ry1s8OSCgysJv+Pi2KzfmMGwGif+TlTyTEemFwHiyHqFM3uhLvz3M8Do46hC0= 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=CfPrCIkd; 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="CfPrCIkd" 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=PF/pH6GqS8l9M6N2BcoMrMWTHNBHwOz4qGTXXLsturA=; b=CfPrCIkdMKmx7SlbwEi8FaDRfc Cvrq2It5W1Vv/d3nw4XoJCmR51wq0K3AMzS3V7uilHuU20fFnydJJZJNRpjwZ3Ngn+jQfg3kMiu1l 6TdB1RkiMROvxBGoX6YP5PNq/jLndh/wICFKNw+5HgT1U3Ljgs5yOM0471rajytsAPmgaoaacR/Li EMxbR88bn6hySGuiAB0uIhItMZd78eiacGI3pBNgD+IlSvQgPfS/JG4+XWrcqky3qwyVMYhcKB21W DBVR8lu131AgKbhSnT/QAmiLOjuzsxhwp48RocKdOCe2wemOubuUDtcgxJVyre/SuvdA1eTEGuaa6 F8el2uOw==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGh-00000004BfP-1HbS; Mon, 01 Jul 2024 16:52:43 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 05/10] block: factor out a blk_write_zeroes_limit helper Date: Mon, 1 Jul 2024 18:51:15 +0200 Message-ID: <20240701165219.1571322-6-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 Contrary to the comment in __blkdev_issue_write_zeroes, nothing here checks for a potential bi_size overflow. Add a helper mirroring the secure erase code for the check. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index cf4f0ee6b14503..f338709aef0c30 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -103,24 +103,28 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector, } EXPORT_SYMBOL(blkdev_issue_discard); +static sector_t bio_write_zeroes_limit(struct block_device *bdev) +{ + sector_t bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; + + return min(bdev_write_zeroes_sectors(bdev), + (UINT_MAX >> SECTOR_SHIFT) & ~bs_mask); +} + static int __blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, unsigned flags) { struct bio *bio = *biop; - unsigned int max_sectors; if (bdev_read_only(bdev)) return -EPERM; - - /* Ensure that max_sectors doesn't overflow bi_size */ - max_sectors = bdev_write_zeroes_sectors(bdev); - - if (max_sectors == 0) + if (!bdev_write_zeroes_sectors(bdev)) return -EOPNOTSUPP; while (nr_sects) { - unsigned int len = min_t(sector_t, nr_sects, max_sectors); + unsigned int len = min_t(sector_t, nr_sects, + bio_write_zeroes_limit(bdev)); bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask); bio->bi_iter.bi_sector = sector; From patchwork Mon Jul 1 16:51:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718416 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 B0D1016DC35; Mon, 1 Jul 2024 16:52:48 +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=1719852770; cv=none; b=hIpA3VSKINa9p/iOn+F9DkIKWMMgKi1IkvWx4cPFAVgFWEqfSFav1dMrRZsfCkTa8oH255GvQees+F9vkCE3+ZF+JfdQ5vTG99CbTHxyLT3vYFUEWzC3w7orGa2lyR/w1Op7l1cWk+4CI7VBxVPSfMbyDhnxfizX8G2F299QEdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852770; c=relaxed/simple; bh=ipWjdzhaQE8AssR1q9AdvcIZYcDG1LAVcn0PJv0gqvY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dt9Fq67J/4WeuxJPBzx2ZO3jGoSRI+RfqBV1chp3V2qFQ1E3LFgk+U239TpARpXLIIlkBQwIj6KKyk5DCf+NnDxUONGyU8Kw9u4jGBRKtChjmqJ9pjifKbOXn3icTEWpL+GTfsOZqsjS90vEZyhRveJ/DmQwy1h0dyBJ7UVoGmQ= 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=pQAoqqsF; 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="pQAoqqsF" 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=PIu8KyjAUoNEI4L8PTKltbTBRaScqHgTEFpyfpYBJ08=; b=pQAoqqsFGlb0P9W6doctrDUG1e Ddjf2OhKa03bTFYSkQL2UVLiv+tM8F3OauRhHrDS32bF0d6AAhQIKoroCh+55C4yBcsWAOF6YSRKA AB0zYQJbRBPzZTpkqYKIE/FkaZ2/xWGjLUPap8dyYL22x2X0iGVsgOBjW98rfDQxabqcs35aEkDWv 7cwjZ7GI4MD6Fw0fQSo2phEvQC9rC5LSrQPNsp1Yf8P8YoE20GrY/TH2ywy5ABuXm39h+mAIU1icP +5yqVIz38Azy7wYU8ZZuIbphgoERdgVE5muKtpXOFP3lHxsF5LFwZ5qqcjT0Ns6ZKHFqs6p4vukFg BCfTVIUw==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGl-00000004BhX-0OxI; Mon, 01 Jul 2024 16:52:47 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 06/10] block: remove the LBA alignment check in __blkdev_issue_zeroout Date: Mon, 1 Jul 2024 18:51:16 +0200 Message-ID: <20240701165219.1571322-7-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 __blkdev_issue_zeroout is a purely kernel internal API and thus can rely on the block layer sector alignment checks. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index f338709aef0c30..7aa7937c34201d 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -209,11 +209,6 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, unsigned flags) { int ret; - sector_t bs_mask; - - bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; - if ((sector | nr_sects) & bs_mask) - return -EINVAL; ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask, biop, flags); From patchwork Mon Jul 1 16:51:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718417 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 C115716EB50; Mon, 1 Jul 2024 16:52:52 +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=1719852774; cv=none; b=hJm5emx/S9Ktte0Q1cq4+DmlaS8o8ebU6LrWP5xfvQeG/zw7/oK+enTtiAQ+ZME4kasUegDhVR9rSgbJPqU3bppnIHJpET5x8K1GJWQr7Wrm2CXyEom+5PpLDXBHPcarXiA2S8/cnWdbeqXBvP2E22yew8rMxVlfAcXFqMljkNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852774; c=relaxed/simple; bh=haVnCrXI7l5VqKshprgf/Vvng8QVSo7+nHtSNKu31Fg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EmWDQxWoBmfog14seZ0uj6Mg+t5pJOmvtf6bMOgPs/4sO8QME7MM6QDQkwHFA9p77OkTnRPgFTwXSXJBU1K/3fd3RSRabxotHx8qHExY6+oGoppFt93by1pAgLwyFWkRi1ADr0RoGr7CjTkkkuCVXyZxQ6aB6AgzNoOLE1JKwv0= 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=WYuQf/YP; 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="WYuQf/YP" 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=E+iu7V6wdisaIDGgCfPAf3w+L8HZpEVxSAbA2UeloJU=; b=WYuQf/YPkaJk/GtSc4dJw304sL 2iSJppd8O2Zt0Q99bj8t1BiubBaKHXo/RA0h2opL8rvZeIEfnTB+nTip/xFR3TrcpluRD7Ak/TJmW qj0YUFtcqkBp4Oq4JKD0WDxbnm2ceIZmZjBmP6X+wI59Jtb7Gd3q4tc1w/cebGuGbCI3nS/Q+ALjh u5DqI2NUV+YkL4y9+BC4KR31iJQon/uHe3yIW1e3sJQsxed5BwLI2i/UIqf/ypcKswe5OiRT9l9UY 5CCijurGc9CTtvRBpNAmA4vsYXTX8qLNKErm/SB1ElsxDVCuT7hPKu1tsaDbK1X6lfRLMr/vjsUE9 esDIDi2g==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGo-00000004Bk0-48lJ; Mon, 01 Jul 2024 16:52:51 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 07/10] block: move read-only and supported checks into (__)blkdev_issue_zeroout Date: Mon, 1 Jul 2024 18:51:17 +0200 Message-ID: <20240701165219.1571322-8-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 Move these checks out of the lower level helpers and into the higher level ones to prepare for refactoring. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 51 ++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 7aa7937c34201d..10325e91560c40 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -111,17 +111,12 @@ static sector_t bio_write_zeroes_limit(struct block_device *bdev) (UINT_MAX >> SECTOR_SHIFT) & ~bs_mask); } -static int __blkdev_issue_write_zeroes(struct block_device *bdev, +static void __blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, unsigned flags) { struct bio *bio = *biop; - if (bdev_read_only(bdev)) - return -EPERM; - if (!bdev_write_zeroes_sectors(bdev)) - return -EOPNOTSUPP; - while (nr_sects) { unsigned int len = min_t(sector_t, nr_sects, bio_write_zeroes_limit(bdev)); @@ -138,7 +133,6 @@ static int __blkdev_issue_write_zeroes(struct block_device *bdev, } *biop = bio; - return 0; } /* @@ -154,7 +148,7 @@ static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects) return min(pages, (sector_t)BIO_MAX_VECS); } -static int __blkdev_issue_zero_pages(struct block_device *bdev, +static void __blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop) { @@ -162,9 +156,6 @@ static int __blkdev_issue_zero_pages(struct block_device *bdev, int bi_size = 0; unsigned int sz; - if (bdev_read_only(bdev)) - return -EPERM; - while (nr_sects != 0) { bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects), REQ_OP_WRITE, gfp_mask); @@ -182,7 +173,6 @@ static int __blkdev_issue_zero_pages(struct block_device *bdev, } *biop = bio; - return 0; } /** @@ -208,15 +198,19 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, unsigned flags) { - int ret; - - ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask, - biop, flags); - if (ret != -EOPNOTSUPP || (flags & BLKDEV_ZERO_NOFALLBACK)) - return ret; + if (bdev_read_only(bdev)) + return -EPERM; - return __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, - biop); + if (bdev_write_zeroes_sectors(bdev)) { + __blkdev_issue_write_zeroes(bdev, sector, nr_sects, + gfp_mask, biop, flags); + } else { + if (flags & BLKDEV_ZERO_NOFALLBACK) + return -EOPNOTSUPP; + __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, + biop); + } + return 0; } EXPORT_SYMBOL(__blkdev_issue_zeroout); @@ -245,21 +239,22 @@ int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; if ((sector | nr_sects) & bs_mask) return -EINVAL; + if (bdev_read_only(bdev)) + return -EPERM; + if ((flags & BLKDEV_ZERO_NOFALLBACK) && !try_write_zeroes) + return -EOPNOTSUPP; retry: bio = NULL; blk_start_plug(&plug); if (try_write_zeroes) { - ret = __blkdev_issue_write_zeroes(bdev, sector, nr_sects, - gfp_mask, &bio, flags); - } else if (!(flags & BLKDEV_ZERO_NOFALLBACK)) { - ret = __blkdev_issue_zero_pages(bdev, sector, nr_sects, - gfp_mask, &bio); + __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask, + &bio, flags); } else { - /* No zeroing offload support */ - ret = -EOPNOTSUPP; + __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, + &bio); } - if (ret == 0 && bio) { + if (bio) { ret = submit_bio_wait(bio); bio_put(bio); } From patchwork Mon Jul 1 16:51:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718418 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 68B0916DEA5; Mon, 1 Jul 2024 16:52:56 +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=1719852777; cv=none; b=M9mc7lY4en+2u0+k93odV4x7P3RqRF9A4JeCCuzc4kNY6ox0hzaR90lKmn6xSAYzPYurIBK+VKrW/eFrTn6H3xZVxkWAqnR+wcgh53FJQ4KVP6UhjKpXAFsvYXdVxDK05IYDvZYPcQcjjVy7i6MFMb5hn4ccqJw1Psb4vJFIkOw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852777; c=relaxed/simple; bh=dwSj8Gc+cBz9Gc10BRpSUyz98fbrdOxzE8JGD8FVWJs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O6z8DovwZR+PoHzNlI3n45kfJko4sVJ1DWgjtwPmhoKqlKof9BaaloU1qEt7U6aTTVOdAtfTsoWbgNvV7czjfsXI/rjEilsNAU1DEKzCfdndWfu9ZmOzNU+ZUIdduGoSePKW+twUvG80lgCQJ+fjsFAk5mql2I7hihhBxsk8Z9s= 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=VfPIhM0u; 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="VfPIhM0u" 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=e2syZyegMy+6QLFCmnnKgLtiBOVxozulluPqAHe5sQU=; b=VfPIhM0u71/aY0HFd81MOSF0y8 ngTUpYFSLErZ6CVMtdIKpEu64c/sjUeBC4LKvCaIKop3DGxMjNcIw4Yx6Ah+pz8B1xbBTsfhWgwER zUx29Z16Daz84Lv90vdUxcHHQPbMGiMRkBDKQ26+RWSLNBkwkX0KPPh65kZ6Rqv0G8ZFvaB+voA75 k3fFcIE+L6fNgQBcUN2m60HsASgHykupdWILQSw0Njtvrk3nOv6r42qIFZKvzcw1PmKvJXXd/J1O0 rHKp2qwDXLU+7EFF4A6SH3eI8sjzht3vCMC1BBMgm7vvfGFBoY0FrJXJ+hDl5xY4E2AAl9gTj45C4 6B7eqqWg==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGs-00000004BmI-2YtV; Mon, 01 Jul 2024 16:52:55 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 08/10] block: refacto blkdev_issue_zeroout Date: Mon, 1 Jul 2024 18:51:18 +0200 Message-ID: <20240701165219.1571322-9-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 Split out two well-defined helpers for hardware supported Write Zeroes and manually writing zeroes using the Write command. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 94 +++++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 10325e91560c40..9585178a51a60c 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -135,6 +135,32 @@ static void __blkdev_issue_write_zeroes(struct block_device *bdev, *biop = bio; } +static int blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector, + sector_t nr_sects, gfp_t gfp, unsigned flags) +{ + struct bio *bio = NULL; + struct blk_plug plug; + int ret = 0; + + blk_start_plug(&plug); + __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp, &bio, flags); + if (bio) { + ret = submit_bio_wait(bio); + bio_put(bio); + } + blk_finish_plug(&plug); + + /* + * For some devices there is no non-destructive way to verify whether + * WRITE ZEROES is actually supported. These will clear the capability + * on an I/O error, in which case we'll turn any error into + * "not supported" here. + */ + if (ret && !bdev_write_zeroes_sectors(bdev)) + return -EOPNOTSUPP; + return ret; +} + /* * Convert a number of 512B sectors to a number of pages. * The result is limited to a number of pages that can fit into a BIO. @@ -175,6 +201,27 @@ static void __blkdev_issue_zero_pages(struct block_device *bdev, *biop = bio; } +static int blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector, + sector_t nr_sects, gfp_t gfp, unsigned flags) +{ + struct bio *bio = NULL; + struct blk_plug plug; + int ret = 0; + + if (flags & BLKDEV_ZERO_NOFALLBACK) + return -EOPNOTSUPP; + + blk_start_plug(&plug); + __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp, &bio); + if (bio) { + ret = submit_bio_wait(bio); + bio_put(bio); + } + blk_finish_plug(&plug); + + return ret; +} + /** * __blkdev_issue_zeroout - generate number of zero filed write bios * @bdev: blockdev to issue @@ -230,52 +277,21 @@ EXPORT_SYMBOL(__blkdev_issue_zeroout); int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, unsigned flags) { - int ret = 0; - sector_t bs_mask; - struct bio *bio; - struct blk_plug plug; - bool try_write_zeroes = !!bdev_write_zeroes_sectors(bdev); + int ret; - bs_mask = (bdev_logical_block_size(bdev) >> 9) - 1; - if ((sector | nr_sects) & bs_mask) + if ((sector | nr_sects) & ((bdev_logical_block_size(bdev) >> 9) - 1)) return -EINVAL; if (bdev_read_only(bdev)) return -EPERM; - if ((flags & BLKDEV_ZERO_NOFALLBACK) && !try_write_zeroes) - return -EOPNOTSUPP; -retry: - bio = NULL; - blk_start_plug(&plug); - if (try_write_zeroes) { - __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask, - &bio, flags); - } else { - __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, - &bio); - } - if (bio) { - ret = submit_bio_wait(bio); - bio_put(bio); - } - blk_finish_plug(&plug); - if (ret && try_write_zeroes) { - if (!(flags & BLKDEV_ZERO_NOFALLBACK)) { - try_write_zeroes = false; - goto retry; - } - if (!bdev_write_zeroes_sectors(bdev)) { - /* - * Zeroing offload support was indicated, but the - * device reported ILLEGAL REQUEST (for some devices - * there is no non-destructive way to verify whether - * WRITE ZEROES is actually supported). - */ - ret = -EOPNOTSUPP; - } + if (bdev_write_zeroes_sectors(bdev)) { + ret = blkdev_issue_write_zeroes(bdev, sector, nr_sects, + gfp_mask, flags); + if (!ret) + return ret; } - return ret; + return blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, flags); } EXPORT_SYMBOL(blkdev_issue_zeroout); From patchwork Mon Jul 1 16:51:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718419 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 93A4B15E5DC; Mon, 1 Jul 2024 16:52:59 +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=1719852780; cv=none; b=ivUi1sb6jtBcftlcqrlv5wpToJvgudjzQNMYY0BEfPGaPeRJRcheq+I8bbpWKDSyiLH531Cv5Kk6hPILZ4kjuIr3oH5tGNBDWXWMf3irZPJPlUxTNhdpYxRZZ8kkMGlhReonsEqqtRtpRFnASsUFUVmsbPkbqjWPl3j13oFM1BY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852780; c=relaxed/simple; bh=47dIZbwgMC3t2R5R5EDJKwmDnPgVrNlLoqy5xt3jicg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k/tVXUdAmyzlP+/axeehmyiKjCq+duqLPZ+WV06l8bnEvWJqCiG0USVCQLJZT2x2mEKQTFsVNwDiCTZm3SQCs0pp4K42/19IO2VoBzPXqa6SvqzIxMUeWcWZmJM06tEPto0gRIa4jD4SqAwJzv+LElhT8Kj7W903f/MNOVL+KUI= 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=hxvJcNmV; 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="hxvJcNmV" 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=htpY4Iv5A+ZWvUcYE7jcvpatTqys0xWVtBJ1A5kLVR0=; b=hxvJcNmVhtO+HPxvvvabRgxEhg jVJ78cJgEtcUQQeWoVnXOe6F2ZnCK91eLq4aMOPUc8xJUfH9yw6mLdJaVGjRuAxg6+3nsEhCQpJwN 467JfI+nKGkxt6LRLDXRcaVj8jBsU10z4j4puMvcwy/1ExEqhSAMQLMTb881XUJsiprtHoX1j9fNc 3kmbXOEV0Cj6i5Lqd7XQqIGVxfHRZxrOaWQVEtNqddSBNdEy5WKOJXPoLz919/zRqMYRwtHYOwHFg oITgw5NLEYpaW6QXdw8CQC1C9GnxJsp1aI0uEHvdTjWdKD7dIS4zTfkDmQOeZBVBypObBB9Rg35qn AMYqzt5g==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGw-00000004Boj-0DaI; Mon, 01 Jul 2024 16:52:58 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 09/10] block: limit the Write Zeroes to manually writing zeroes fallback Date: Mon, 1 Jul 2024 18:51:19 +0200 Message-ID: <20240701165219.1571322-10-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 Only fall back from hardware Write Zeroes failures when blkdev_issue_write_zeroes returns -EOPNOTSUPP; Note that blkdev_issue_write_zeroes turns any failure into -EOPNOTSUPP when the write zeroes queue limit has been cleared to 0, so this still catches all I/O errors where the driver detected missing support for the hardware acceleration. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 9585178a51a60c..95a22e7b1d3b85 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -287,7 +287,7 @@ int blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, if (bdev_write_zeroes_sectors(bdev)) { ret = blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp_mask, flags); - if (!ret) + if (ret != -EOPNOTSUPP) return ret; } From patchwork Mon Jul 1 16:51:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13718420 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 F35B315E5DC; Mon, 1 Jul 2024 16:53:03 +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=1719852785; cv=none; b=tplQi4zZK6uVRNLdLb+L4CsYEsRGi4KSxKoI6kL9dFyZHQqTUq2n/6ton2nWehNQbflOlQweeI67w1fUGYZvj+lzUz9RuOT3X9MSG6wqQoMOENqovh9idbWAp7JEjrher1sLoXz/O1ifV7SrIXPB2MhHhdsY0Nzl7abJc67R1ws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719852785; c=relaxed/simple; bh=OsCtO5bX5Zh7jx6XhKJoZDIlc/TsSDiOuwMVUSQJ11A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J5BRrcIbz1KpTA/Al/Ofsd0AwXG2Q0+sGVGkC7ctGlDUCiavRfMGmHi3VDyTtIoOnMCPxFtAbecD9cxeicvyW7AeLzipEwPjvkpgLQ0UuSA6avv72FSBHiHOyNlibeDCiNhTQ6i4mqf4dYNgu1xaZONJqA5/Ujv9Iq2u8QGDmyY= 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=WszWtJzm; 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="WszWtJzm" 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=W3UEqxM+W8WMDFLwQ70hzXAIh72AVtbpUh2WCLbswBM=; b=WszWtJzmd939XviNuIChjlFDqX hwKZsEv64eV1dzTFGxcZAhpVenbgBIvnj9khD4pSHwRC3JipXQ16Ijtswqm7Wkryx0E1FTAr7SphQ 9Ma0PhDDvRQma8oUVFLrcff7+3v9jsmnHvM46S6qMzTO8ZxJP3MXK1tFbur1t4BGeyv3c7BPdi75+ RRA9Un8GRsPP2EYVQBxWfSc1PAFn17NfLQXNobKx+eSBdNEXRwnsCfeX/hkFD8p3W+5MGNy3pdjZG ROKrjmlLiaDEri7qc/tU0DfM8i5DjjyY2Js5RDYxnsJNSaV0HmTFiTSaQlXbPzIX/7UblOQ0/ixjM gMfY3MmQ==; Received: from 2a02-8389-2341-5b80-6bfa-ffbf-d1d1-ec94.cable.dynamic.v6.surfer.at ([2a02:8389:2341:5b80:6bfa:ffbf:d1d1:ec94] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1sOKGz-00000004Bq6-2fTK; Mon, 01 Jul 2024 16:53:02 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Keith Busch , Conrad Meyer , Ulf Hansson , linux-mmc@vger.kernel.org, linux-block@vger.kernel.org Subject: [PATCH 10/10] blk-lib: check for kill signal in ioctl BLKZEROOUT Date: Mon, 1 Jul 2024 18:51:20 +0200 Message-ID: <20240701165219.1571322-11-hch@lst.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240701165219.1571322-1-hch@lst.de> References: <20240701165219.1571322-1-hch@lst.de> Precedence: bulk X-Mailing-List: linux-mmc@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 Zeroout 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. Add a new BLKDEV_ZERO_KILLABLE flag for blkdev_issue_zeroout that checks 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. Signed-off-by: Christoph Hellwig --- block/blk-lib.c | 66 +++++++++++++++++++++++++++--------------- block/ioctl.c | 2 +- include/linux/blkdev.h | 1 + 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/block/blk-lib.c b/block/blk-lib.c index 95a22e7b1d3b85..729a2669681773 100644 --- a/block/blk-lib.c +++ b/block/blk-lib.c @@ -115,24 +115,27 @@ static void __blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop, unsigned flags) { - struct bio *bio = *biop; - while (nr_sects) { unsigned int len = min_t(sector_t, nr_sects, bio_write_zeroes_limit(bdev)); + struct bio *bio; + + if ((flags & BLKDEV_ZERO_KILLABLE) && + fatal_signal_pending(current)) + break; - bio = blk_next_bio(bio, bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask); + bio = bio_alloc(bdev, 0, REQ_OP_WRITE_ZEROES, gfp_mask); bio->bi_iter.bi_sector = sector; if (flags & BLKDEV_ZERO_NOUNMAP) bio->bi_opf |= REQ_NOUNMAP; bio->bi_iter.bi_size = len << SECTOR_SHIFT; + *biop = bio_chain_and_submit(*biop, bio); + nr_sects -= len; sector += len; cond_resched(); } - - *biop = bio; } static int blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector, @@ -145,6 +148,12 @@ static int blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector, blk_start_plug(&plug); __blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp, &bio, flags); if (bio) { + if ((flags & BLKDEV_ZERO_KILLABLE) && + fatal_signal_pending(current)) { + bio_await_chain(bio); + blk_finish_plug(&plug); + return -EINTR; + } ret = submit_bio_wait(bio); bio_put(bio); } @@ -176,29 +185,34 @@ static unsigned int __blkdev_sectors_to_bio_pages(sector_t nr_sects) static void __blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, - struct bio **biop) + struct bio **biop, unsigned int flags) { - struct bio *bio = *biop; - int bi_size = 0; - unsigned int sz; + while (nr_sects) { + unsigned int nr_vecs = __blkdev_sectors_to_bio_pages(nr_sects); + struct bio *bio; - while (nr_sects != 0) { - bio = blk_next_bio(bio, bdev, __blkdev_sectors_to_bio_pages(nr_sects), - REQ_OP_WRITE, gfp_mask); + bio = bio_alloc(bdev, nr_vecs, REQ_OP_WRITE, gfp_mask); bio->bi_iter.bi_sector = sector; - while (nr_sects != 0) { - sz = min((sector_t) PAGE_SIZE, nr_sects << 9); - bi_size = bio_add_page(bio, ZERO_PAGE(0), sz, 0); - nr_sects -= bi_size >> 9; - sector += bi_size >> 9; - if (bi_size < sz) + if ((flags & BLKDEV_ZERO_KILLABLE) && + fatal_signal_pending(current)) + break; + + do { + unsigned int len, added; + + len = min_t(sector_t, + PAGE_SIZE, nr_sects << SECTOR_SHIFT); + added = bio_add_page(bio, ZERO_PAGE(0), len, 0); + if (added < len) break; - } + nr_sects -= added >> SECTOR_SHIFT; + sector += added >> SECTOR_SHIFT; + } while (nr_sects); + + *biop = bio_chain_and_submit(*biop, bio); cond_resched(); } - - *biop = bio; } static int blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector, @@ -212,8 +226,14 @@ static int blkdev_issue_zero_pages(struct block_device *bdev, sector_t sector, return -EOPNOTSUPP; blk_start_plug(&plug); - __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp, &bio); + __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp, &bio, flags); if (bio) { + if ((flags & BLKDEV_ZERO_KILLABLE) && + fatal_signal_pending(current)) { + bio_await_chain(bio); + blk_finish_plug(&plug); + return -EINTR; + } ret = submit_bio_wait(bio); bio_put(bio); } @@ -255,7 +275,7 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, if (flags & BLKDEV_ZERO_NOFALLBACK) return -EOPNOTSUPP; __blkdev_issue_zero_pages(bdev, sector, nr_sects, gfp_mask, - biop); + biop, flags); } return 0; } diff --git a/block/ioctl.c b/block/ioctl.c index 45668a21cdb374..83899d06a9d30d 100644 --- a/block/ioctl.c +++ b/block/ioctl.c @@ -258,7 +258,7 @@ static int blk_ioctl_zeroout(struct block_device *bdev, blk_mode_t mode, goto fail; err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL, - BLKDEV_ZERO_NOUNMAP); + BLKDEV_ZERO_NOUNMAP | BLKDEV_ZERO_KILLABLE); fail: filemap_invalidate_unlock(bdev->bd_mapping); diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index 4d0d4b83bc740f..538bb9b9b6cb32 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1100,6 +1100,7 @@ int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector, #define BLKDEV_ZERO_NOUNMAP (1 << 0) /* do not free blocks */ #define BLKDEV_ZERO_NOFALLBACK (1 << 1) /* don't write explicit zeroes */ +#define BLKDEV_ZERO_KILLABLE (1 << 2) /* interruptible by fatal signals */ extern int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector, sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,