linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL
@ 2020-10-21  3:11 Xianting Tian
  2020-10-23 16:40 ` Tianxianting
  2020-10-25 19:09 ` Andrew Morton
  0 siblings, 2 replies; 4+ messages in thread
From: Xianting Tian @ 2020-10-21  3:11 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, Xianting Tian

bio_alloc with __GFP_DIRECT_RECLAIM(which is included in GFP_NOIO,
GFP_KERNEL) never fails, as stated in the comments of bio_alloc_bioset.

So we can remove multiple unneeded null checks of bio_alloc and simplify
the code.

We have done it in fs/ext4/readpage.c, fs/ext4/page-io.c, fs/direct-io.c,
and so forth.

Signed-off-by: Xianting Tian <tian.xianting@h3c.com>
---
 mm/page_io.c | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/mm/page_io.c b/mm/page_io.c
index e485a6e8a..9215bb356 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -30,18 +30,20 @@ static struct bio *get_swap_bio(gfp_t gfp_flags,
 				struct page *page, bio_end_io_t end_io)
 {
 	struct bio *bio;
+	struct block_device *bdev;
 
+	/*
+	 * 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_flags, 1);
-	if (bio) {
-		struct block_device *bdev;
+	bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
+	bio_set_dev(bio, bdev);
+	bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
+	bio->bi_end_io = end_io;
 
-		bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
-		bio_set_dev(bio, bdev);
-		bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
-		bio->bi_end_io = end_io;
+	bio_add_page(bio, page, thp_size(page), 0);
 
-		bio_add_page(bio, page, thp_size(page), 0);
-	}
 	return bio;
 }
 
@@ -351,19 +353,13 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
 
 	ret = 0;
 	bio = get_swap_bio(GFP_NOIO, page, end_write_func);
-	if (bio == NULL) {
-		set_page_dirty(page);
-		unlock_page(page);
-		ret = -ENOMEM;
-		goto out;
-	}
 	bio->bi_opf = REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc);
 	bio_associate_blkg_from_page(bio, page);
 	count_swpout_vm_event(page);
 	set_page_writeback(page);
 	unlock_page(page);
 	submit_bio(bio);
-out:
+
 	return ret;
 }
 
@@ -416,11 +412,6 @@ int swap_readpage(struct page *page, bool synchronous)
 
 	ret = 0;
 	bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
-	if (bio == NULL) {
-		unlock_page(page);
-		ret = -ENOMEM;
-		goto out;
-	}
 	disk = bio->bi_disk;
 	/*
 	 * Keep this task valid during swap readpage because the oom killer may
-- 
2.17.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* RE: [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL
  2020-10-21  3:11 [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL Xianting Tian
@ 2020-10-23 16:40 ` Tianxianting
  2020-10-25 19:09 ` Andrew Morton
  1 sibling, 0 replies; 4+ messages in thread
From: Tianxianting @ 2020-10-23 16:40 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel

Hi Andrew
Could I get your comments for this patch?  Thanks in advance.

-----Original Message-----
From: tianxianting (RD) 
Sent: Wednesday, October 21, 2020 11:11 AM
To: akpm@linux-foundation.org
Cc: linux-mm@kvack.org; linux-kernel@vger.kernel.org; tianxianting (RD) <tian.xianting@h3c.com>
Subject: [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL

bio_alloc with __GFP_DIRECT_RECLAIM(which is included in GFP_NOIO,
GFP_KERNEL) never fails, as stated in the comments of bio_alloc_bioset.

So we can remove multiple unneeded null checks of bio_alloc and simplify the code.

We have done it in fs/ext4/readpage.c, fs/ext4/page-io.c, fs/direct-io.c, and so forth.

Signed-off-by: Xianting Tian <tian.xianting@h3c.com>
---
 mm/page_io.c | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/mm/page_io.c b/mm/page_io.c index e485a6e8a..9215bb356 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -30,18 +30,20 @@ static struct bio *get_swap_bio(gfp_t gfp_flags,
 				struct page *page, bio_end_io_t end_io)  {
 	struct bio *bio;
+	struct block_device *bdev;
 
+	/*
+	 * 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_flags, 1);
-	if (bio) {
-		struct block_device *bdev;
+	bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
+	bio_set_dev(bio, bdev);
+	bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
+	bio->bi_end_io = end_io;
 
-		bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
-		bio_set_dev(bio, bdev);
-		bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
-		bio->bi_end_io = end_io;
+	bio_add_page(bio, page, thp_size(page), 0);
 
-		bio_add_page(bio, page, thp_size(page), 0);
-	}
 	return bio;
 }
 
@@ -351,19 +353,13 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
 
 	ret = 0;
 	bio = get_swap_bio(GFP_NOIO, page, end_write_func);
-	if (bio == NULL) {
-		set_page_dirty(page);
-		unlock_page(page);
-		ret = -ENOMEM;
-		goto out;
-	}
 	bio->bi_opf = REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc);
 	bio_associate_blkg_from_page(bio, page);
 	count_swpout_vm_event(page);
 	set_page_writeback(page);
 	unlock_page(page);
 	submit_bio(bio);
-out:
+
 	return ret;
 }
 
@@ -416,11 +412,6 @@ int swap_readpage(struct page *page, bool synchronous)
 
 	ret = 0;
 	bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
-	if (bio == NULL) {
-		unlock_page(page);
-		ret = -ENOMEM;
-		goto out;
-	}
 	disk = bio->bi_disk;
 	/*
 	 * Keep this task valid during swap readpage because the oom killer may
--
2.17.1



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL
  2020-10-21  3:11 [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL Xianting Tian
  2020-10-23 16:40 ` Tianxianting
@ 2020-10-25 19:09 ` Andrew Morton
  2020-10-25 19:24   ` Jens Axboe
  1 sibling, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2020-10-25 19:09 UTC (permalink / raw)
  To: Xianting Tian; +Cc: linux-mm, linux-kernel, Jens Axboe

On Wed, 21 Oct 2020 11:11:28 +0800 Xianting Tian <tian.xianting@h3c.com> wrote:

> bio_alloc with __GFP_DIRECT_RECLAIM(which is included in GFP_NOIO,
> GFP_KERNEL) never fails, as stated in the comments of bio_alloc_bioset.
> 
> So we can remove multiple unneeded null checks of bio_alloc and simplify
> the code.
> 
> We have done it in fs/ext4/readpage.c, fs/ext4/page-io.c, fs/direct-io.c,
> and so forth.
> 

(cc Jens)

> --- a/mm/page_io.c
> +++ b/mm/page_io.c
> @@ -30,18 +30,20 @@ static struct bio *get_swap_bio(gfp_t gfp_flags,
>  				struct page *page, bio_end_io_t end_io)
>  {
>  	struct bio *bio;
> +	struct block_device *bdev;
>  
> +	/*
> +	 * 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_flags, 1);
> -	if (bio) {
> -		struct block_device *bdev;
> +	bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
> +	bio_set_dev(bio, bdev);
> +	bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
> +	bio->bi_end_io = end_io;
>  
> -		bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
> -		bio_set_dev(bio, bdev);
> -		bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
> -		bio->bi_end_io = end_io;
> +	bio_add_page(bio, page, thp_size(page), 0);
>  
> -		bio_add_page(bio, page, thp_size(page), 0);
> -	}
>  	return bio;
>  }
>  
> @@ -351,19 +353,13 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
>  
>  	ret = 0;
>  	bio = get_swap_bio(GFP_NOIO, page, end_write_func);
> -	if (bio == NULL) {
> -		set_page_dirty(page);
> -		unlock_page(page);
> -		ret = -ENOMEM;
> -		goto out;
> -	}
>  	bio->bi_opf = REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc);
>  	bio_associate_blkg_from_page(bio, page);
>  	count_swpout_vm_event(page);
>  	set_page_writeback(page);
>  	unlock_page(page);
>  	submit_bio(bio);
> -out:
> +
>  	return ret;
>  }
>  
> @@ -416,11 +412,6 @@ int swap_readpage(struct page *page, bool synchronous)
>  
>  	ret = 0;
>  	bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
> -	if (bio == NULL) {
> -		unlock_page(page);
> -		ret = -ENOMEM;
> -		goto out;
> -	}
>  	disk = bio->bi_disk;
>  	/*
>  	 * Keep this task valid during swap readpage because the oom killer may

I'm reluctant to remove these checks - yours is a fairly subtle
discovery and things might change in the future.  Intentionally or
otherwise!


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL
  2020-10-25 19:09 ` Andrew Morton
@ 2020-10-25 19:24   ` Jens Axboe
  0 siblings, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2020-10-25 19:24 UTC (permalink / raw)
  To: Andrew Morton, Xianting Tian; +Cc: linux-mm, linux-kernel

On 10/25/20 1:09 PM, Andrew Morton wrote:
> On Wed, 21 Oct 2020 11:11:28 +0800 Xianting Tian <tian.xianting@h3c.com> wrote:
> 
>> bio_alloc with __GFP_DIRECT_RECLAIM(which is included in GFP_NOIO,
>> GFP_KERNEL) never fails, as stated in the comments of bio_alloc_bioset.
>>
>> So we can remove multiple unneeded null checks of bio_alloc and simplify
>> the code.
>>
>> We have done it in fs/ext4/readpage.c, fs/ext4/page-io.c, fs/direct-io.c,
>> and so forth.
>>
> 
> (cc Jens)
> 
>> --- a/mm/page_io.c
>> +++ b/mm/page_io.c
>> @@ -30,18 +30,20 @@ static struct bio *get_swap_bio(gfp_t gfp_flags,
>>  				struct page *page, bio_end_io_t end_io)
>>  {
>>  	struct bio *bio;
>> +	struct block_device *bdev;
>>  
>> +	/*
>> +	 * 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_flags, 1);
>> -	if (bio) {
>> -		struct block_device *bdev;
>> +	bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
>> +	bio_set_dev(bio, bdev);
>> +	bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
>> +	bio->bi_end_io = end_io;
>>  
>> -		bio->bi_iter.bi_sector = map_swap_page(page, &bdev);
>> -		bio_set_dev(bio, bdev);
>> -		bio->bi_iter.bi_sector <<= PAGE_SHIFT - 9;
>> -		bio->bi_end_io = end_io;
>> +	bio_add_page(bio, page, thp_size(page), 0);
>>  
>> -		bio_add_page(bio, page, thp_size(page), 0);
>> -	}
>>  	return bio;
>>  }
>>  
>> @@ -351,19 +353,13 @@ int __swap_writepage(struct page *page, struct writeback_control *wbc,
>>  
>>  	ret = 0;
>>  	bio = get_swap_bio(GFP_NOIO, page, end_write_func);
>> -	if (bio == NULL) {
>> -		set_page_dirty(page);
>> -		unlock_page(page);
>> -		ret = -ENOMEM;
>> -		goto out;
>> -	}
>>  	bio->bi_opf = REQ_OP_WRITE | REQ_SWAP | wbc_to_write_flags(wbc);
>>  	bio_associate_blkg_from_page(bio, page);
>>  	count_swpout_vm_event(page);
>>  	set_page_writeback(page);
>>  	unlock_page(page);
>>  	submit_bio(bio);
>> -out:
>> +
>>  	return ret;
>>  }
>>  
>> @@ -416,11 +412,6 @@ int swap_readpage(struct page *page, bool synchronous)
>>  
>>  	ret = 0;
>>  	bio = get_swap_bio(GFP_KERNEL, page, end_swap_bio_read);
>> -	if (bio == NULL) {
>> -		unlock_page(page);
>> -		ret = -ENOMEM;
>> -		goto out;
>> -	}
>>  	disk = bio->bi_disk;
>>  	/*
>>  	 * Keep this task valid during swap readpage because the oom killer may
> 
> I'm reluctant to remove these checks - yours is a fairly subtle
> discovery and things might change in the future.  Intentionally or
> otherwise!

On the block/bio side, it all boils down to the mempool backing of the
bio allocations. If __GFP_DIRECT_RECLAIM is set for that, then we'll
always wait on allocations for a bio. That is intentional, by design, to
guarantee forward progress. It used to be __GFP_WAIT based, but I guess
that changed at some point...

-- 
Jens Axboe



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-10-25 19:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-21  3:11 [PATCH] mm: bio_alloc never fails when set GFP_NOIO, GFP_KERNEL Xianting Tian
2020-10-23 16:40 ` Tianxianting
2020-10-25 19:09 ` Andrew Morton
2020-10-25 19:24   ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).