Skip to content

Commit 64b582c

Browse files
johnpgarryaxboe
authored andcommitted
block: Read max write zeroes once for __blkdev_issue_write_zeroes()
As reported in [0], we may get a hang when formatting a XFS FS on a RAID0 drive. Commit 73a768d ("block: factor out a blk_write_zeroes_limit helper") changed __blkdev_issue_write_zeroes() to read the max write zeroes value in the loop. This is not safe as max write zeroes may change in value. Specifically for the case of [0], the value goes to 0, and we get an infinite loop. Lift the limit reading out of the loop. [0] https://lore.kernel.org/linux-xfs/4d31268f-310b-4220-88a2-e191c3932a82@oracle.com/T/#t Fixes: 73a768d ("block: factor out a blk_write_zeroes_limit helper") Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: John Garry <john.g.garry@oracle.com> Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com> Link: https://lore.kernel.org/r/20240815163228.216051-2-john.g.garry@oracle.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent b313a8c commit 64b582c

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

block/blk-lib.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,20 @@ static sector_t bio_write_zeroes_limit(struct block_device *bdev)
111111
(UINT_MAX >> SECTOR_SHIFT) & ~bs_mask);
112112
}
113113

114+
/*
115+
* There is no reliable way for the SCSI subsystem to determine whether a
116+
* device supports a WRITE SAME operation without actually performing a write
117+
* to media. As a result, write_zeroes is enabled by default and will be
118+
* disabled if a zeroing operation subsequently fails. This means that this
119+
* queue limit is likely to change at runtime.
120+
*/
114121
static void __blkdev_issue_write_zeroes(struct block_device *bdev,
115122
sector_t sector, sector_t nr_sects, gfp_t gfp_mask,
116-
struct bio **biop, unsigned flags)
123+
struct bio **biop, unsigned flags, sector_t limit)
117124
{
125+
118126
while (nr_sects) {
119-
unsigned int len = min_t(sector_t, nr_sects,
120-
bio_write_zeroes_limit(bdev));
127+
unsigned int len = min(nr_sects, limit);
121128
struct bio *bio;
122129

123130
if ((flags & BLKDEV_ZERO_KILLABLE) &&
@@ -141,12 +148,14 @@ static void __blkdev_issue_write_zeroes(struct block_device *bdev,
141148
static int blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector,
142149
sector_t nr_sects, gfp_t gfp, unsigned flags)
143150
{
151+
sector_t limit = bio_write_zeroes_limit(bdev);
144152
struct bio *bio = NULL;
145153
struct blk_plug plug;
146154
int ret = 0;
147155

148156
blk_start_plug(&plug);
149-
__blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp, &bio, flags);
157+
__blkdev_issue_write_zeroes(bdev, sector, nr_sects, gfp, &bio,
158+
flags, limit);
150159
if (bio) {
151160
if ((flags & BLKDEV_ZERO_KILLABLE) &&
152161
fatal_signal_pending(current)) {
@@ -165,7 +174,7 @@ static int blkdev_issue_write_zeroes(struct block_device *bdev, sector_t sector,
165174
* on an I/O error, in which case we'll turn any error into
166175
* "not supported" here.
167176
*/
168-
if (ret && !bdev_write_zeroes_sectors(bdev))
177+
if (ret && !limit)
169178
return -EOPNOTSUPP;
170179
return ret;
171180
}
@@ -265,12 +274,14 @@ int __blkdev_issue_zeroout(struct block_device *bdev, sector_t sector,
265274
sector_t nr_sects, gfp_t gfp_mask, struct bio **biop,
266275
unsigned flags)
267276
{
277+
sector_t limit = bio_write_zeroes_limit(bdev);
278+
268279
if (bdev_read_only(bdev))
269280
return -EPERM;
270281

271-
if (bdev_write_zeroes_sectors(bdev)) {
282+
if (limit) {
272283
__blkdev_issue_write_zeroes(bdev, sector, nr_sects,
273-
gfp_mask, biop, flags);
284+
gfp_mask, biop, flags, limit);
274285
} else {
275286
if (flags & BLKDEV_ZERO_NOFALLBACK)
276287
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)