From: Christoph Hellwig <hch@lst.de>
To: Jens Axboe <axboe@kernel.dk>,
Chandan Babu R <chandanbabu@kernel.org>,
Keith Busch <kbusch@kernel.org>
Cc: linux-block@vger.kernel.org, linux-nvme@lists.infradead.org,
linux-xfs@vger.kernel.org
Subject: [PATCH 03/10] block: add a blk_next_discard_bio helper
Date: Thu, 7 Mar 2024 08:11:50 -0700 [thread overview]
Message-ID: <20240307151157.466013-4-hch@lst.de> (raw)
In-Reply-To: <20240307151157.466013-1-hch@lst.de>
This factors the guts of __blkdev_issue_discard into a helper that can
be used by callers avoid the fatal_signal_pending check and to generally
simplify the bio handling.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
block/blk-lib.c | 41 ++++++++++++++++++++++++-----------------
include/linux/blkdev.h | 2 ++
2 files changed, 26 insertions(+), 17 deletions(-)
diff --git a/block/blk-lib.c b/block/blk-lib.c
index 50923508a32466..4f2d52210b129c 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -55,28 +55,35 @@ static void await_bio_chain(struct bio *bio)
blk_wait_io(&done);
}
+bool blk_next_discard_bio(struct block_device *bdev, struct bio **biop,
+ sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask)
+{
+ sector_t bio_sects = min(*nr_sects, bio_discard_limit(bdev, *sector));
+
+ if (!bio_sects)
+ return false;
+
+ *biop = blk_next_bio(*biop, bdev, 0, REQ_OP_DISCARD, gfp_mask);
+ (*biop)->bi_iter.bi_sector = *sector;
+ (*biop)->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 true;
+}
+EXPORT_SYMBOL_GPL(blk_next_discard_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 = *biop;
- while (nr_sects) {
- sector_t req_sects =
- min(nr_sects, bio_discard_limit(bdev, sector));
-
- 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();
+ while (blk_next_discard_bio(bdev, &bio, §or, &nr_sects, gfp_mask)) {
if (fatal_signal_pending(current)) {
await_bio_chain(bio);
return -EINTR;
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index a9a0bd6cac4aff..b87cd889008291 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1059,6 +1059,8 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
sector_t nr_sects, gfp_t gfp_mask);
int __blkdev_issue_discard(struct block_device *bdev, sector_t sector,
sector_t nr_sects, gfp_t gfp_mask, struct bio **biop);
+bool blk_next_discard_bio(struct block_device *bdev, struct bio **biop,
+ sector_t *sector, sector_t *nr_sects, gfp_t gfp_mask);
int blkdev_issue_secure_erase(struct block_device *bdev, sector_t sector,
sector_t nr_sects, gfp_t gfp);
--
2.39.2
next prev parent reply other threads:[~2024-03-07 15:12 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-07 15:11 RFC: untangle and fix __blkdev_issue_discard Christoph Hellwig
2024-03-07 15:11 ` [PATCH 01/10] block: remove the discard_granularity check in __blkdev_issue_discard Christoph Hellwig
2024-03-07 15:11 ` [PATCH 02/10] block: move discard checks into the ioctl handler Christoph Hellwig
2024-03-07 21:33 ` Dave Chinner
2024-03-08 15:22 ` Christoph Hellwig
2024-03-08 21:16 ` Dave Chinner
2024-03-07 15:11 ` Christoph Hellwig [this message]
2024-03-07 15:11 ` [PATCH 04/10] xfs: switch to using blk_next_discard_bio directly Christoph Hellwig
2024-03-07 15:11 ` [PATCH 05/10] f2fs: " Christoph Hellwig
2024-03-07 15:11 ` [PATCH 06/10] ext4: " Christoph Hellwig
2024-03-07 16:13 ` Keith Busch
2024-03-08 15:21 ` Christoph Hellwig
2024-03-07 15:11 ` [PATCH 07/10] nvmet: " Christoph Hellwig
2024-03-07 15:11 ` [PATCH 08/10] md: " Christoph Hellwig
2024-03-07 15:11 ` [PATCH 09/10] dm-thin: " Christoph Hellwig
2024-03-07 15:11 ` [PATCH 10/10] block: remove __blkdev_issue_discard Christoph Hellwig
2024-03-07 21:05 ` RFC: untangle and fix __blkdev_issue_discard Keith Busch
2024-03-08 15:21 ` Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240307151157.466013-4-hch@lst.de \
--to=hch@lst.de \
--cc=axboe@kernel.dk \
--cc=chandanbabu@kernel.org \
--cc=kbusch@kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=linux-xfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox