Skip to content

Commit 5500221

Browse files
Gao Xiangtytso
authored andcommitted
ext4: bio_alloc with __GFP_DIRECT_RECLAIM never fails
Similar to [1] [2], bio_alloc with __GFP_DIRECT_RECLAIM flags guarantees bio allocation under some given restrictions, as stated in block/bio.c and fs/direct-io.c So here it's ok to not check for NULL value from bio_alloc(). [1] https://lore.kernel.org/r/[email protected] [2] https://lore.kernel.org/r/[email protected] Cc: Theodore Ts'o <[email protected]> Cc: Andreas Dilger <[email protected]> Cc: Ritesh Harjani <[email protected]> Reviewed-by: Chao Yu <[email protected]> Signed-off-by: Gao Xiang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Theodore Ts'o <[email protected]>
1 parent ebc11f7 commit 5500221

File tree

2 files changed

+26
-37
lines changed

2 files changed

+26
-37
lines changed

fs/ext4/page-io.c

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -394,28 +394,29 @@ void ext4_io_submit_init(struct ext4_io_submit *io,
394394
io->io_end = NULL;
395395
}
396396

397-
static int io_submit_init_bio(struct ext4_io_submit *io,
398-
struct buffer_head *bh)
397+
static void io_submit_init_bio(struct ext4_io_submit *io,
398+
struct buffer_head *bh)
399399
{
400400
struct bio *bio;
401401

402+
/*
403+
* bio_alloc will _always_ be able to allocate a bio if
404+
* __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset().
405+
*/
402406
bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
403-
if (!bio)
404-
return -ENOMEM;
405407
bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
406408
bio_set_dev(bio, bh->b_bdev);
407409
bio->bi_end_io = ext4_end_bio;
408410
bio->bi_private = ext4_get_io_end(io->io_end);
409411
io->io_bio = bio;
410412
io->io_next_block = bh->b_blocknr;
411413
wbc_init_bio(io->io_wbc, bio);
412-
return 0;
413414
}
414415

415-
static int io_submit_add_bh(struct ext4_io_submit *io,
416-
struct inode *inode,
417-
struct page *page,
418-
struct buffer_head *bh)
416+
static void io_submit_add_bh(struct ext4_io_submit *io,
417+
struct inode *inode,
418+
struct page *page,
419+
struct buffer_head *bh)
419420
{
420421
int ret;
421422

@@ -424,17 +425,14 @@ static int io_submit_add_bh(struct ext4_io_submit *io,
424425
ext4_io_submit(io);
425426
}
426427
if (io->io_bio == NULL) {
427-
ret = io_submit_init_bio(io, bh);
428-
if (ret)
429-
return ret;
428+
io_submit_init_bio(io, bh);
430429
io->io_bio->bi_write_hint = inode->i_write_hint;
431430
}
432431
ret = bio_add_page(io->io_bio, page, bh->b_size, bh_offset(bh));
433432
if (ret != bh->b_size)
434433
goto submit_and_retry;
435434
wbc_account_cgroup_owner(io->io_wbc, page, bh->b_size);
436435
io->io_next_block++;
437-
return 0;
438436
}
439437

440438
int ext4_bio_write_page(struct ext4_io_submit *io,
@@ -527,39 +525,28 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
527525
gfp_flags |= __GFP_NOFAIL;
528526
goto retry_encrypt;
529527
}
530-
bounce_page = NULL;
531-
goto out;
528+
529+
printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
530+
redirty_page_for_writepage(wbc, page);
531+
do {
532+
clear_buffer_async_write(bh);
533+
bh = bh->b_this_page;
534+
} while (bh != head);
535+
goto unlock;
532536
}
533537
}
534538

535539
/* Now submit buffers to write */
536540
do {
537541
if (!buffer_async_write(bh))
538542
continue;
539-
ret = io_submit_add_bh(io, inode, bounce_page ?: page, bh);
540-
if (ret) {
541-
/*
542-
* We only get here on ENOMEM. Not much else
543-
* we can do but mark the page as dirty, and
544-
* better luck next time.
545-
*/
546-
break;
547-
}
543+
io_submit_add_bh(io, inode,
544+
bounce_page ? bounce_page : page, bh);
548545
nr_submitted++;
549546
clear_buffer_dirty(bh);
550547
} while ((bh = bh->b_this_page) != head);
551548

552-
/* Error stopped previous loop? Clean up buffers... */
553-
if (ret) {
554-
out:
555-
fscrypt_free_bounce_page(bounce_page);
556-
printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
557-
redirty_page_for_writepage(wbc, page);
558-
do {
559-
clear_buffer_async_write(bh);
560-
bh = bh->b_this_page;
561-
} while (bh != head);
562-
}
549+
unlock:
563550
unlock_page(page);
564551
/* Nothing submitted - we have to end page writeback */
565552
if (!nr_submitted)

fs/ext4/readpage.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,12 @@ int ext4_mpage_readpages(struct address_space *mapping,
360360
if (bio == NULL) {
361361
struct bio_post_read_ctx *ctx;
362362

363+
/*
364+
* bio_alloc will _always_ be able to allocate a bio if
365+
* __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset().
366+
*/
363367
bio = bio_alloc(GFP_KERNEL,
364368
min_t(int, nr_pages, BIO_MAX_PAGES));
365-
if (!bio)
366-
goto set_error_page;
367369
ctx = get_bio_post_read_ctx(inode, bio, page->index);
368370
if (IS_ERR(ctx)) {
369371
bio_put(bio);

0 commit comments

Comments
 (0)