mm, swap: Fix THP swap out
Subsystems (2)
PATCH 1/1 mm, swap: Fix THP swap out
From: Huang Ying <[email protected]>
0-Day test system reported some OOM regressions for several THP (Transparent Huge Page) swap test cases. These regressions are bisected to 6861428921b5 ("block: always define BIO_MAX_PAGES as 256"). In the commit, BIO_MAX_PAGES is set to 256 even when THP swap is enabled. So the bio_alloc(gfp_flags, 512) in get_swap_bio() may fail when swapping out THP. That causes the OOM.
As in the patch description of 6861428921b5 ("block: always define BIO_MAX_PAGES as 256"), THP swap should use multi-page bvec to write THP to swap space. So the issue is fixed via doing that in get_swap_bio().
BTW: I remember I have checked the THP swap code when 6861428921b5 ("block: always define BIO_MAX_PAGES as 256") was merged, and thought the THP swap code needn't to be changed. But apparently, I was wrong. I should have done this at that time.
--- mm/page_io.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
mm/page_io.c +2 -5
@@ -29,10 +29,9 @@static struct bio *get_swap_bio (gfp_t gfp_flags,struct page *page,bio_end_io_t end_io) { -int i, nr =hpage_nr_pages (page);struct bio *bio; - bio =bio_alloc (gfp_flags, nr); + bio =bio_alloc (gfp_flags,1 );if (bio) {struct block_device *bdev;
@@ -41,9 +40,7 @@ static struct bio *get_swap_bio(gfp_t gfp_flags, bio->bi_iter.bi_sector <<=PAGE_SHIFT -9 ; bio->bi_end_io = end_io; -for (i =0 ; i < nr; i++) -bio_add_page (bio, page + i,PAGE_SIZE ,0 ); -VM_BUG_ON (bio->bi_iter.bi_size !=PAGE_SIZE * nr); +__bio_add_page (bio, page,PAGE_SIZE *hpage_nr_pages (page),0 ); }return bio; }
Signature · 1 line
2.20.1
Hi Huang Ying,
Quoted 49 lines · Ying
From: Huang Ying <[email protected]>
0-Day test system reported some OOM regressions for several THP (Transparent Huge Page) swap test cases. These regressions are bisected to
6861428921b5("block: always defineBIO_MAX_PAGESas 256"). In the commit,BIO_MAX_PAGESis set to 256 even when THP swap is enabled. So thebio_alloc(gfp_flags, 512)inget_swap_bio()may fail when swapping out THP. That causes the OOM.As in the patch description of
6861428921b5("block: always defineBIO_MAX_PAGESas 256"), THP swap should use multi-page bvec to write THP to swap space. So the issue is fixed via doing that inget_swap_bio().BTW: I remember I have checked the THP swap code when
6861428921b5("block: always defineBIO_MAX_PAGESas 256") was merged, and thought the THP swap code needn't to be changed. But apparently, I was wrong. I should have done this at that time.Fixes 6861428921b5 ("block: always define BIO_MAX_PAGES as 256")Signed-off-by "Huang, Ying" <[email protected]>Cc Ming Lei <[email protected]>Cc Michal Hocko <[email protected]>Cc Johannes Weiner <[email protected]>Cc Hugh Dickins <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>Cc Daniel Jordan <[email protected]>--- mm/page_io.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)mm/page_io.c +1
-2@@ -29,10 +29,9 @@static struct bio *get_swap_bio (gfp_t gfp_flags,struct page *page,bio_end_io_t end_io) { -int i, nr =hpage_nr_pages (page);struct bio *bio; - bio =bio_alloc (gfp_flags, nr); + bio =bio_alloc (gfp_flags,1 );if (bio) {struct block_device *bdev;
mm/page_io.c static struct bio *get_swap_bio(gfp_t gfp_flags, · line 45
Quoted 5 lines
mm/page_io.c +0
-2@@ -41,9 +40,7 @@ static struct bio *get_swap_bio(gfp_t gfp_flags, bio->bi_iter.bi_sector <<=PAGE_SHIFT -9 ; bio->bi_end_io = end_io; -for (i =0 ; i < nr; i++) -bio_add_page (bio, page + i,PAGE_SIZE ,0 );
bio_add_page() supposes to work, just wondering why it doesn't recently.
Could you share me one test case for reproducing it?
mm/page_io.c static struct bio *get_swap_bio(gfp_t gfp_flags, · line 45
Quoted 4 lines
mm/page_io.c +1
-1-VM_BUG_ON (bio->bi_iter.bi_size !=PAGE_SIZE * nr); +__bio_add_page (bio, page,PAGE_SIZE *hpage_nr_pages (page),0 ); }return bio;
Actually the above code can be simplified as:
mm/page_io.c +2 -3
@@ -29,7 +29,7 @@static struct bio *get_swap_bio (gfp_t gfp_flags,struct page *page,bio_end_io_t end_io) { -int i, nr =hpage_nr_pages (page); +int nr =hpage_nr_pages (page);struct bio *bio; bio =bio_alloc (gfp_flags, nr);
@@ -41,8 +41,7 @@ static struct bio *get_swap_bio(gfp_t gfp_flags, bio->bi_iter.bi_sector <<=PAGE_SHIFT -9 ; bio->bi_end_io = end_io; -for (i =0 ; i < nr; i++) -bio_add_page (bio, page + i,PAGE_SIZE ,0 ); +bio_add_page (bio, page,PAGE_SIZE * nr,0 );VM_BUG_ON (bio->bi_iter.bi_size !=PAGE_SIZE * nr); }return bio;
Thanks,
Ming
Quoted 51 lines · Ming Lei
Hi Huang Ying,
Quoted 48 lines · Ying
From: Huang Ying <[email protected]>
0-Day test system reported some OOM regressions for several THP (Transparent Huge Page) swap test cases. These regressions are bisected to
6861428921b5("block: always defineBIO_MAX_PAGESas 256"). In the commit,BIO_MAX_PAGESis set to 256 even when THP swap is enabled. So thebio_alloc(gfp_flags, 512)inget_swap_bio()may fail when swapping out THP. That causes the OOM.As in the patch description of
6861428921b5("block: always defineBIO_MAX_PAGESas 256"), THP swap should use multi-page bvec to write THP to swap space. So the issue is fixed via doing that inget_swap_bio().BTW: I remember I have checked the THP swap code when
6861428921b5("block: always defineBIO_MAX_PAGESas 256") was merged, and thought the THP swap code needn't to be changed. But apparently, I was wrong. I should have done this at that time.Fixes 6861428921b5 ("block: always define BIO_MAX_PAGES as 256")Signed-off-by "Huang, Ying" <[email protected]>Cc Ming Lei <[email protected]>Cc Michal Hocko <[email protected]>Cc Johannes Weiner <[email protected]>Cc Hugh Dickins <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>Cc Daniel Jordan <[email protected]>--- mm/page_io.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)mm/page_io.c +1
-2@@ -29,10 +29,9 @@static struct bio *get_swap_bio (gfp_t gfp_flags,struct page *page,bio_end_io_t end_io) { -int i, nr =hpage_nr_pages (page);struct bio *bio; - bio =bio_alloc (gfp_flags, nr); + bio =bio_alloc (gfp_flags,1 );if (bio) {struct block_device *bdev;
Quoted 8 lines
mm/page_io.c static struct bio *get_swap_bio(gfp_t gfp_flags, · line 45
Quoted 6 lines
mm/page_io.c +0
-2@@ -41,9 +40,7 @@ static struct bio *get_swap_bio(gfp_t gfp_flags, bio->bi_iter.bi_sector <<=PAGE_SHIFT -9 ; bio->bi_end_io = end_io; -for (i =0 ; i < nr; i++) -bio_add_page (bio, page + i,PAGE_SIZE ,0 );
bio_add_page()supposes to work, just wondering why it doesn't recently.
Yes. Just checked and bio_add_page() works too. I should have used that. The problem isn't bio_add_page(), but bio_alloc(), because nr == 512 > 256, mempool cannot be used during swapout, so swapout will fail.
Best Regards,
Huang, Ying
Quoted 36 lines
Could you share me one test case for reproducing it?
mm/page_io.c static struct bio *get_swap_bio(gfp_t gfp_flags, · line 47
Quoted 4 lines
mm/page_io.c +1
-1-VM_BUG_ON (bio->bi_iter.bi_size !=PAGE_SIZE * nr); +__bio_add_page (bio, page,PAGE_SIZE *hpage_nr_pages (page),0 ); }return bio;Actually the above code can be simplified as:
mm/page_io.c +2
-3@@ -29,7 +29,7 @@static struct bio *get_swap_bio (gfp_t gfp_flags,struct page *page,bio_end_io_t end_io) { -int i, nr =hpage_nr_pages (page); +int nr =hpage_nr_pages (page);struct bio *bio; bio =bio_alloc (gfp_flags, nr);@@ -41,8 +41,7 @@ static struct bio *get_swap_bio(gfp_t gfp_flags, bio->bi_iter.bi_sector <<=PAGE_SHIFT -9 ; bio->bi_end_io = end_io; -for (i =0 ; i < nr; i++) -bio_add_page (bio, page + i,PAGE_SIZE ,0 ); +bio_add_page (bio, page,PAGE_SIZE * nr,0 );VM_BUG_ON (bio->bi_iter.bi_size !=PAGE_SIZE * nr); }return bio;Thanks,
Ming
Quoted 57 lines · Ying
Quoted 55 lines · Ming Lei
Hi Huang Ying,
Quoted 52 lines · Ying
From: Huang Ying <[email protected]>
0-Day test system reported some OOM regressions for several THP (Transparent Huge Page) swap test cases. These regressions are bisected to
6861428921b5("block: always defineBIO_MAX_PAGESas 256"). In the commit,BIO_MAX_PAGESis set to 256 even when THP swap is enabled. So thebio_alloc(gfp_flags, 512)inget_swap_bio()may fail when swapping out THP. That causes the OOM.As in the patch description of
6861428921b5("block: always defineBIO_MAX_PAGESas 256"), THP swap should use multi-page bvec to write THP to swap space. So the issue is fixed via doing that inget_swap_bio().BTW: I remember I have checked the THP swap code when
6861428921b5("block: always defineBIO_MAX_PAGESas 256") was merged, and thought the THP swap code needn't to be changed. But apparently, I was wrong. I should have done this at that time.Fixes 6861428921b5 ("block: always define BIO_MAX_PAGES as 256")Signed-off-by "Huang, Ying" <[email protected]>Cc Ming Lei <[email protected]>Cc Michal Hocko <[email protected]>Cc Johannes Weiner <[email protected]>Cc Hugh Dickins <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>Cc Daniel Jordan <[email protected]>--- mm/page_io.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)mm/page_io.c +1
-2@@ -29,10 +29,9 @@static struct bio *get_swap_bio (gfp_t gfp_flags,struct page *page,bio_end_io_t end_io) { -int i, nr =hpage_nr_pages (page);struct bio *bio; - bio =bio_alloc (gfp_flags, nr); + bio =bio_alloc (gfp_flags,1 );if (bio) {struct block_device *bdev;@@ -41,9 +40,7 @@ static struct bio *get_swap_bio(gfp_t gfp_flags, bio->bi_iter.bi_sector <<=PAGE_SHIFT -9 ; bio->bi_end_io = end_io;
Quoted 8 lines
Quoted 4 lines
mm/page_io.c static struct bio *get_swap_bio(gfp_t gfp_flags, · line 45
Quoted 2 lines
mm/page_io.c +0
-2-for (i =0 ; i < nr; i++) -bio_add_page (bio, page + i,PAGE_SIZE ,0 );
bio_add_page()supposes to work, just wondering why it doesn't recently.Yes. Just checked and
bio_add_page()works too. I should have used that. The problem isn'tbio_add_page(), butbio_alloc(), because nr == 512 > 256, mempool cannot be used during swapout, so swapout will fail.
Then we can pass 1 to bio_alloc(), together with single bio_add_page() for making the code more readable.
thanks,
Ming
Quoted 63 lines · Ming Lei
Quoted 62 lines · Ying
Quoted 59 lines · Ming Lei
Hi Huang Ying,
Quoted 49 lines · Ying
From: Huang Ying <[email protected]>
0-Day test system reported some OOM regressions for several THP (Transparent Huge Page) swap test cases. These regressions are bisected to
6861428921b5("block: always defineBIO_MAX_PAGESas 256"). In the commit,BIO_MAX_PAGESis set to 256 even when THP swap is enabled. So thebio_alloc(gfp_flags, 512)inget_swap_bio()may fail when swapping out THP. That causes the OOM.As in the patch description of
6861428921b5("block: always defineBIO_MAX_PAGESas 256"), THP swap should use multi-page bvec to write THP to swap space. So the issue is fixed via doing that inget_swap_bio().BTW: I remember I have checked the THP swap code when
6861428921b5("block: always defineBIO_MAX_PAGESas 256") was merged, and thought the THP swap code needn't to be changed. But apparently, I was wrong. I should have done this at that time.Fixes 6861428921b5 ("block: always define BIO_MAX_PAGES as 256")Signed-off-by "Huang, Ying" <[email protected]>Cc Ming Lei <[email protected]>Cc Michal Hocko <[email protected]>Cc Johannes Weiner <[email protected]>Cc Hugh Dickins <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>Cc Daniel Jordan <[email protected]>--- mm/page_io.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)mm/page_io.c +1
-2@@ -29,10 +29,9 @@static struct bio *get_swap_bio (gfp_t gfp_flags,struct page *page,bio_end_io_t end_io) { -int i, nr =hpage_nr_pages (page);struct bio *bio; - bio =bio_alloc (gfp_flags, nr); + bio =bio_alloc (gfp_flags,1 );if (bio) {struct block_device *bdev;mm/page_io.c static struct bio *get_swap_bio(gfp_t gfp_flags, · line 45
Quoted 5 lines
mm/page_io.c +0
-2@@ -41,9 +40,7 @@ static struct bio *get_swap_bio(gfp_t gfp_flags, bio->bi_iter.bi_sector <<=PAGE_SHIFT -9 ; bio->bi_end_io = end_io; -for (i =0 ; i < nr; i++) -bio_add_page (bio, page + i,PAGE_SIZE ,0 );
bio_add_page()supposes to work, just wondering why it doesn't recently.
Quoted 7 lines
Quoted 3 lines
Yes. Just checked and
bio_add_page()works too. I should have used that. The problem isn'tbio_add_page(), butbio_alloc(), because nr == 512 > 256, mempool cannot be used during swapout, so swapout will fail.Then we can pass 1 to
bio_alloc(), together with singlebio_add_page()for making the code more readable.
Yes. Will send out v2 to replace __bio_add_page() with bio_add_page().
Best Regards,
Huang, Ying