Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp882177ybx; Thu, 31 Oct 2019 02:21:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqx5ELjGDz25/Fuhi2DrK4Rgh6bUCjAQsxKl2IU4g49V7ZaHkr2nCZ/BejPRLB6PCNUE2Wrz X-Received: by 2002:a17:906:c9d9:: with SMTP id hk25mr2843895ejb.74.1572513674305; Thu, 31 Oct 2019 02:21:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572513674; cv=none; d=google.com; s=arc-20160816; b=ZgKnXKUxu4TCph0ef0EIM6QFhuOXS5lmPeltjBRPB/dP5tSURe2y44pTuQkwgsKg8w 2WSCKBDrHPMsHsv90c0xXMzSrSPC4q17Kt84WTPfSg9hlAz4c6BESlZj/wMrurBCiKNC lidvNaCCNirgZL3mzRj2+XunYqVm/vy9gSrIbsn/Dkuj2GN8V9ryIJqRwuZgZj+dCDOl lhp/Fb4WjHyTBBIT2WqoZ29hH3SLU0XQ2k3t84z+5hHR8Qimu6F6cGyVWrM5ffk8tXHs cnxoQf3sFun5GSS3teH7KIikcdNQb05+pChYYtRQdLd3oxuqKy7cSl9W+b3ifkdyPGIj dalg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=FC6+2zQ4eDw1E8S0a2CTaFpOCaXItwDYw3Gtj+VFI5I=; b=hhh1pHpNKyMp4QegWGQuBuiEpkKSqbyTSiOxiy83Wq0WG9u81xHQeREEOmMIRiIIT3 El9Tq/H9nOrQ1o5R+i9nFQCin7gzwXzswkWPHWvhI9r7WDSC90KJwCQW7sbFcs7npjAs 2xi8fCpwiii7o/Ongx63E3Eb2vyBlPyZehT5KhWIWS6Evet9v5LDKUN+oScgtoPNPSWi wTLcNX92/N+Z2A/jQCY3ULDexiTeS2fJhBKC7bV/DrKECAXo7reNbdd6KyEV5GpX9uBd qMoe5T1EmjduoLsXhlEFeLUnKqannd7mi/0SpZKg4ykvN52w2BLYwJ9Iovdw3a2MJNPM 5MQA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h18si3382976edh.352.2019.10.31.02.20.49; Thu, 31 Oct 2019 02:21:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-ext4-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726897AbfJaJUp (ORCPT + 99 others); Thu, 31 Oct 2019 05:20:45 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:5236 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726864AbfJaJUp (ORCPT ); Thu, 31 Oct 2019 05:20:45 -0400 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 1AFA291DC1FC5E986338; Thu, 31 Oct 2019 17:20:42 +0800 (CST) Received: from architecture4.huawei.com (10.140.130.215) by smtp.huawei.com (10.3.19.214) with Microsoft SMTP Server (TLS) id 14.3.439.0; Thu, 31 Oct 2019 17:20:31 +0800 From: Gao Xiang To: Theodore Ts'o , Andreas Dilger CC: , , Gao Xiang , Ritesh Harjani , Chao Yu Subject: [PATCH v2] ext4: bio_alloc with __GFP_DIRECT_RECLAIM never fails Date: Thu, 31 Oct 2019 17:23:15 +0800 Message-ID: <20191031092315.139267-1-gaoxiang25@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191030161244.GB3953@hsiangkao-HP-ZHAN-66-Pro-G1> References: <20191030161244.GB3953@hsiangkao-HP-ZHAN-66-Pro-G1> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.140.130.215] X-CFilter-Loop: Reflected Sender: linux-ext4-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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/20191030035518.65477-1-gaoxiang25@huawei.com [2] https://lore.kernel.org/r/20190830162812.GA10694@infradead.org Cc: Theodore Ts'o Cc: Andreas Dilger Cc: Ritesh Harjani Cc: Chao Yu Signed-off-by: Gao Xiang --- changes since v1: - fix commit message and simplify logic as suggested by Ritesh; - add short messages ahead of bio_alloc suggested by Ted. fs/ext4/page-io.c | 57 ++++++++++++++++++---------------------------- fs/ext4/readpage.c | 6 +++-- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c index 12ceadef32c5..c92504c1b1ca 100644 --- a/fs/ext4/page-io.c +++ b/fs/ext4/page-io.c @@ -358,14 +358,16 @@ void ext4_io_submit_init(struct ext4_io_submit *io, io->io_end = NULL; } -static int io_submit_init_bio(struct ext4_io_submit *io, - struct buffer_head *bh) +static void io_submit_init_bio(struct ext4_io_submit *io, + struct buffer_head *bh) { struct bio *bio; + /* + * bio_alloc will _always_ be able to allocate a bio if + * __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset(). + */ bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); - if (!bio) - return -ENOMEM; bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); bio_set_dev(bio, bh->b_bdev); bio->bi_end_io = ext4_end_bio; @@ -373,13 +375,12 @@ static int io_submit_init_bio(struct ext4_io_submit *io, io->io_bio = bio; io->io_next_block = bh->b_blocknr; wbc_init_bio(io->io_wbc, bio); - return 0; } -static int io_submit_add_bh(struct ext4_io_submit *io, - struct inode *inode, - struct page *page, - struct buffer_head *bh) +static void io_submit_add_bh(struct ext4_io_submit *io, + struct inode *inode, + struct page *page, + struct buffer_head *bh) { int ret; @@ -388,9 +389,7 @@ static int io_submit_add_bh(struct ext4_io_submit *io, ext4_io_submit(io); } if (io->io_bio == NULL) { - ret = io_submit_init_bio(io, bh); - if (ret) - return ret; + io_submit_init_bio(io, bh); io->io_bio->bi_write_hint = inode->i_write_hint; } ret = bio_add_page(io->io_bio, page, bh->b_size, bh_offset(bh)); @@ -398,7 +397,6 @@ static int io_submit_add_bh(struct ext4_io_submit *io, goto submit_and_retry; wbc_account_cgroup_owner(io->io_wbc, page, bh->b_size); io->io_next_block++; - return 0; } int ext4_bio_write_page(struct ext4_io_submit *io, @@ -491,8 +489,14 @@ int ext4_bio_write_page(struct ext4_io_submit *io, gfp_flags |= __GFP_NOFAIL; goto retry_encrypt; } - bounce_page = NULL; - goto out; + + printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret); + redirty_page_for_writepage(wbc, page); + do { + clear_buffer_async_write(bh); + bh = bh->b_this_page; + } while (bh != head); + goto unlock; } } @@ -500,30 +504,13 @@ int ext4_bio_write_page(struct ext4_io_submit *io, do { if (!buffer_async_write(bh)) continue; - ret = io_submit_add_bh(io, inode, bounce_page ?: page, bh); - if (ret) { - /* - * We only get here on ENOMEM. Not much else - * we can do but mark the page as dirty, and - * better luck next time. - */ - break; - } + io_submit_add_bh(io, inode, + bounce_page ? bounce_page : page, bh); nr_submitted++; clear_buffer_dirty(bh); } while ((bh = bh->b_this_page) != head); - /* Error stopped previous loop? Clean up buffers... */ - if (ret) { - out: - fscrypt_free_bounce_page(bounce_page); - printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret); - redirty_page_for_writepage(wbc, page); - do { - clear_buffer_async_write(bh); - bh = bh->b_this_page; - } while (bh != head); - } +unlock: unlock_page(page); /* Nothing submitted - we have to end page writeback */ if (!nr_submitted) diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c index a30b203fa461..fef7755300c3 100644 --- a/fs/ext4/readpage.c +++ b/fs/ext4/readpage.c @@ -360,10 +360,12 @@ int ext4_mpage_readpages(struct address_space *mapping, if (bio == NULL) { struct bio_post_read_ctx *ctx; + /* + * bio_alloc will _always_ be able to allocate a bio if + * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset(). + */ bio = bio_alloc(GFP_KERNEL, min_t(int, nr_pages, BIO_MAX_PAGES)); - if (!bio) - goto set_error_page; ctx = get_bio_post_read_ctx(inode, bio, page->index); if (IS_ERR(ctx)) { bio_put(bio); -- 2.17.1