staging: erofs: introduce erofs_grab_bio
authorGao Xiang <gaoxiang25@huawei.com>
Tue, 21 Aug 2018 14:49:29 +0000 (22:49 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 27 Aug 2018 17:46:12 +0000 (19:46 +0200)
this patch renames prepare_bio to erofs_grab_bio, and
adds a nofail option in order to retry in the bio allocator
under memory pressure.

Signed-off-by: Gao Xiang <gaoxiang25@huawei.com>
Reviewed-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Chao Yu <yuchao0@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/erofs/data.c
drivers/staging/erofs/internal.h
drivers/staging/erofs/unzip_vle.c

index ac263a180253e5aaf5375a8da9b65f0ad8b06c40..e0c046df66651859eb80da1d2b841da481a80f05 100644 (file)
@@ -60,7 +60,8 @@ repeat:
                struct bio *bio;
                int err;
 
-               bio = prepare_bio(sb, blkaddr, 1, read_endio);
+               bio = erofs_grab_bio(sb, blkaddr, 1, read_endio, true);
+
                err = bio_add_page(bio, page, PAGE_SIZE, 0);
                BUG_ON(err != PAGE_SIZE);
 
@@ -278,7 +279,14 @@ submit_bio_retry:
                if (nblocks > BIO_MAX_PAGES)
                        nblocks = BIO_MAX_PAGES;
 
-               bio = prepare_bio(inode->i_sb, blknr, nblocks, read_endio);
+               bio = erofs_grab_bio(inode->i_sb,
+                       blknr, nblocks, read_endio, false);
+
+               if (IS_ERR(bio)) {
+                       err = PTR_ERR(bio);
+                       bio = NULL;
+                       goto err_out;
+               }
        }
 
        err = bio_add_page(bio, page, PAGE_SIZE, 0);
index 367b39fe46e523ef8da89e083e147cf6f31aa63b..1353b3ff8401d11c423b43fd9b475fd7d467032e 100644 (file)
@@ -420,26 +420,26 @@ struct erofs_map_blocks {
 #define EROFS_GET_BLOCKS_RAW    0x0001
 
 /* data.c */
-static inline struct bio *prepare_bio(
-       struct super_block *sb,
-       erofs_blk_t blkaddr, unsigned nr_pages,
-       bio_end_io_t endio)
+static inline struct bio *
+erofs_grab_bio(struct super_block *sb,
+              erofs_blk_t blkaddr, unsigned int nr_pages,
+              bio_end_io_t endio, bool nofail)
 {
-       gfp_t gfp = GFP_NOIO;
-       struct bio *bio = bio_alloc(gfp, nr_pages);
-
-       if (unlikely(bio == NULL) &&
-               (current->flags & PF_MEMALLOC)) {
-               do {
-                       nr_pages /= 2;
-                       if (unlikely(!nr_pages)) {
-                               bio = bio_alloc(gfp | __GFP_NOFAIL, 1);
-                               BUG_ON(bio == NULL);
-                               break;
+       const gfp_t gfp = GFP_NOIO;
+       struct bio *bio;
+
+       do {
+               if (nr_pages == 1) {
+                       bio = bio_alloc(gfp | (nofail ? __GFP_NOFAIL : 0), 1);
+                       if (unlikely(bio == NULL)) {
+                               DBG_BUGON(nofail);
+                               return ERR_PTR(-ENOMEM);
                        }
-                       bio = bio_alloc(gfp, nr_pages);
-               } while (bio == NULL);
-       }
+                       break;
+               }
+               bio = bio_alloc(gfp, nr_pages);
+               nr_pages /= 2;
+       } while (unlikely(bio == NULL));
 
        bio->bi_end_io = endio;
        bio_set_dev(bio, sb->s_bdev);
index 8721f0a41d157b7ac799eed40bdfea06aa30decd..375c1711bb6ba96cf2bfac77abba202a5514da10 100644 (file)
@@ -1213,8 +1213,8 @@ submit_bio_retry:
                }
 
                if (bio == NULL) {
-                       bio = prepare_bio(sb, first_index + i,
-                               BIO_MAX_PAGES, z_erofs_vle_read_endio);
+                       bio = erofs_grab_bio(sb, first_index + i,
+                               BIO_MAX_PAGES, z_erofs_vle_read_endio, true);
                        bio->bi_private = tagptr_cast_ptr(bi_private);
 
                        ++nr_bios;