migrate_pages(): batch TLB flushing
Subsystems (3)
cover migrate_pages(): batch TLB flushing
From: "Huang, Ying" <[email protected]>
Now, migrate_pages() migrate folios one by one, like the fake code as follows,
for each folio
unmap
flush TLB
copy
restore map
If multiple folios are passed to migrate_pages(), there are opportunities to batch the TLB flushing and copying. That is, we can change the code to something as follows,
for each folio
unmap
for each folio
flush TLB
for each folio
copy
for each folio
restore map
The total number of TLB flushing IPI can be reduced considerably. And we may use some hardware accelerator such as DSA to accelerate the folio copying.
So in this patch, we refactor the migrate_pages() implementation and implement the TLB flushing batching. Base on this, hardware accelerated folio copying can be implemented.
If too many folios are passed to migrate_pages(), in the naive batched implementation, we may unmap too many folios at the same time. The possibility for a task to wait for the migrated folios to be mapped again increases. So the latency may be hurt. To deal with this issue, the max number of folios be unmapped in batch is restricted to no more than HPAGE_PMD_NR in the unit of page. That is, the influence is at the same level of THP migration.
We use the following test to measure the performance impact of the patchset,
On a 2-socket Intel server,
- Run pmbench memory accessing benchmark
- Run migratepages to migrate pages of pmbench between node 0 and node 1 back and forth.
With the patch, the TLB flushing IPI reduces 99.1% during the test and the number of pages migrated successfully per second increases 291.7%.
This patchset is based on mm-unstable.
Changes:
v3:
- Rebased on v6.2-rc4
- Fixed a bug about non-LRU folio migration. Thanks Mike!
- Fixed some comments. Thanks Baolin!
- Collected reviewed-by.
v2:
- Rebased on v6.2-rc3
- Fixed type force cast warning. Thanks Kees!
- Added more comments and cleaned up the code. Thanks Andrew, Zi, Alistair, Dan!
- Collected reviewed-by.
from rfc to v1:
- Rebased on v6.2-rc1
- Fix the deadlock issue caused by locking multiple pages synchronously per Alistair's comments. Thanks!
- Fix the autonumabench panic per Rao's comments and fix. Thanks!
- Other minor fixes per comments. Thanks!
Best Regards,
Huang, Ying
PATCH 1/9 migrate_pages: organize stats with struct migrate_pages_stats
Define struct migrate_pages_stats to organize the various statistics in migrate_pages(). This makes it easier to collect and consume the statistics in multiple functions. This will be needed in the following patches in the series.
--- mm/migrate.c | 60 +++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 26 deletions(-)
mm/migrate.c +34 -26
@@ -1396,6 +1396,16 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_freturn rc; } +struct migrate_pages_stats { +int nr_succeeded;/* Normal and large folios migrated successfully, in +units of base pages */ +int nr_failed_pages;/* Normal and large folios failed to be migrated, in +units of base pages. Untried folios aren't counted */ +int nr_thp_succeeded;/* THP migrated successfully */ +int nr_thp_failed;/* THP failed to be migrated */ +int nr_thp_split;/* THP split before migrating */ +}; +/* * migrate_pages - migrate the folios specified in a list, to the free folios * supplied as the target for the page migration
@@ -1430,13 +1440,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,int large_retry =1 ;int thp_retry =1 ;int nr_failed =0 ; -int nr_failed_pages =0 ;int nr_retry_pages =0 ; -int nr_succeeded =0 ; -int nr_thp_succeeded =0 ;int nr_large_failed =0 ; -int nr_thp_failed =0 ; -int nr_thp_split =0 ;int pass =0 ;bool is_large = false;bool is_thp = false;
@@ -1446,9 +1451,11 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,LIST_HEAD (split_folios);bool nosplit = (reason ==MR_NUMA_MISPLACED );bool no_split_folio_counting = false; +struct migrate_pages_stats stats;trace_mm_migrate_pages_start (mode, reason); +memset (&stats,0 ,sizeof (stats)); split_folio_migration:for (pass =0 ; pass <10 && (retry || large_retry); pass++) { retry =0 ;
@@ -1502,9 +1509,9 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,/* Large folio migration is unsupported */ if (is_large) { nr_large_failed++; - nr_thp_failed += is_thp; + stats.nr_thp_failed += is_thp;if (!try_split_folio (folio, &split_folios)) { - nr_thp_split += is_thp; + stats.nr_thp_split += is_thp;break ; }/* Hugetlb migration is unsupported */
@@ -1512,7 +1519,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_failed++; } - nr_failed_pages += nr_pages; + stats.nr_failed_pages += nr_pages;list_move_tail (&folio->lru, &ret_folios);break ;case -ENOMEM :
@@ -1522,13 +1529,13 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,*/ if (is_large) { nr_large_failed++; - nr_thp_failed += is_thp; + stats.nr_thp_failed += is_thp;/* Large folio NUMA faulting doesn't split to retry. */ if (!nosplit) {int ret =try_split_folio (folio, &split_folios);if (!ret) { - nr_thp_split += is_thp; + stats.nr_thp_split += is_thp;break ; }else if (reason ==MR_LONGTERM_PIN && ret == -EAGAIN ) {
@@ -1546,7 +1553,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_failed++; } - nr_failed_pages += nr_pages + nr_retry_pages; + stats.nr_failed_pages += nr_pages + nr_retry_pages;/* * There might be some split folios of fail-to-migrate large * folios left in split_folios list. Move them back to migration
@@ -1556,7 +1563,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,list_splice_init (&split_folios, from);/* nr_failed isn't updated for not used */ nr_large_failed += large_retry; - nr_thp_failed += thp_retry; + stats.nr_thp_failed += thp_retry; goto out;case -EAGAIN :if (is_large) {
@@ -1568,8 +1575,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_retry_pages += nr_pages;break ;case MIGRATEPAGE_SUCCESS : - nr_succeeded += nr_pages; - nr_thp_succeeded += is_thp; + stats.nr_succeeded += nr_pages; + stats.nr_thp_succeeded += is_thp;break ;default :/*
@@ -1580,20 +1587,20 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,*/ if (is_large) { nr_large_failed++; - nr_thp_failed += is_thp; + stats.nr_thp_failed += is_thp; }else if (!no_split_folio_counting) { nr_failed++; } - nr_failed_pages += nr_pages; + stats.nr_failed_pages += nr_pages;break ; } } } nr_failed += retry; nr_large_failed += large_retry; - nr_thp_failed += thp_retry; - nr_failed_pages += nr_retry_pages; + stats.nr_thp_failed += thp_retry; + stats.nr_failed_pages += nr_retry_pages;/* * Try to migrate split folios of fail-to-migrate large folios, no * nr_failed counting in this round, since all split folios of a
@@ -1626,16 +1633,17 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,if (list_empty (from)) rc =0 ; -count_vm_events (PGMIGRATE_SUCCESS , nr_succeeded); -count_vm_events (PGMIGRATE_FAIL , nr_failed_pages); -count_vm_events (THP_MIGRATION_SUCCESS , nr_thp_succeeded); -count_vm_events (THP_MIGRATION_FAIL , nr_thp_failed); -count_vm_events (THP_MIGRATION_SPLIT , nr_thp_split); -trace_mm_migrate_pages (nr_succeeded, nr_failed_pages, nr_thp_succeeded, - nr_thp_failed, nr_thp_split, mode, reason); +count_vm_events (PGMIGRATE_SUCCESS , stats.nr_succeeded); +count_vm_events (PGMIGRATE_FAIL , stats.nr_failed_pages); +count_vm_events (THP_MIGRATION_SUCCESS , stats.nr_thp_succeeded); +count_vm_events (THP_MIGRATION_FAIL , stats.nr_thp_failed); +count_vm_events (THP_MIGRATION_SPLIT , stats.nr_thp_split); +trace_mm_migrate_pages (stats.nr_succeeded, stats.nr_failed_pages, + stats.nr_thp_succeeded, stats.nr_thp_failed, + stats.nr_thp_split, mode, reason);if (ret_succeeded) - *ret_succeeded = nr_succeeded; + *ret_succeeded = stats.nr_succeeded;return rc; }
Signature · 1 line
2.35.1
PATCH 2/9 migrate_pages: separate hugetlb folios migration
In reply to Huang YingThis is a preparation patch to batch the folio unmapping and moving for the non-hugetlb folios. Based on that we can batch the TLB shootdown during the folio migration and make it possible to use some hardware accelerator for the folio copying.
In this patch the hugetlb folios and non-hugetlb folios migration is separated in migrate_pages() to make it easy to change the non-hugetlb folios migration implementation.
--- mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-)
mm/migrate.c +119 -22
@@ -1396,6 +1396,8 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_freturn rc; } +#define NR_MAX_MIGRATE_PAGES_RETRY 10 +struct migrate_pages_stats {int nr_succeeded;/* Normal and large folios migrated successfully, in units of base pages */
@@ -1406,6 +1408,95 @@ struct migrate_pages_stats {int nr_thp_split;/* THP split before migrating */ }; +/* +* Returns the number of hugetlb folios that were not migrated, or an error code +* after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable +* any more because the list has become empty or no retryable hugetlb folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. +*/ +static int migrate_hugetlbs (struct list_head *from,new_page_t get_new_page, +free_page_t put_new_page,unsigned long private, +enum migrate_mode mode,int reason, +struct migrate_pages_stats *stats, +struct list_head *ret_folios) +{ +int retry =1 ; +int nr_failed =0 ; +int nr_retry_pages =0 ; +int pass =0 ; +struct folio *folio, *folio2; +int rc, nr_pages; + +for (pass =0 ; pass <NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) { + retry =0 ; + nr_retry_pages =0 ; + +list_for_each_entry_safe (folio, folio2, from, lru) { +if (!folio_test_hugetlb (folio)) +continue ; + + nr_pages =folio_nr_pages (folio); + +cond_resched (); + + rc =unmap_and_move_huge_page (get_new_page, + put_new_page, private, + &folio->page, pass >2 , mode, + reason, ret_folios); +/* +* The rules are: +* Success: hugetlb folio will be put back +* -EAGAIN: stay on the from list +* -ENOMEM: stay on the from list +* -ENOSYS: stay on the from list +* Other errno: put on ret_folios list +*/ +switch (rc) { +case -ENOSYS : +/* Hugetlb migration is unsupported */ + nr_failed++; + stats->nr_failed_pages += nr_pages; +list_move_tail (&folio->lru, ret_folios); +break ; +case -ENOMEM : +/* +* When memory is low, don't bother to try to migrate +* other folios, just exit. +*/ + stats->nr_failed_pages += nr_pages + nr_retry_pages; +return -ENOMEM ; +case -EAGAIN : + retry++; + nr_retry_pages += nr_pages; +break ; +case MIGRATEPAGE_SUCCESS : + stats->nr_succeeded += nr_pages; +break ; +default : +/* +* Permanent failure (-EBUSY, etc.): +* unlike -EAGAIN case, the failed folio is +* removed from migration folio list and not +* retried in the next outer loop. +*/ + nr_failed++; + stats->nr_failed_pages += nr_pages; +break ; + } + } + } +/* +* nr_failed is number of hugetlb folios failed to be migrated. After +* NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb +* folios as failed. +*/ + nr_failed += retry; + stats->nr_failed_pages += nr_retry_pages; + +return nr_failed; +} +/* * migrate_pages - migrate the folios specified in a list, to the free folios * supplied as the target for the page migration
@@ -1422,10 +1513,10 @@ struct migrate_pages_stats {* @ret_succeeded: Set to the number of folios migrated successfully if * the caller passes a non-NULL pointer. * -* The function returns after 10 attempts or if no folios are movable any more -* because the list has become empty or no retryable folios exist any more. -* It is caller's responsibility to call putback_movable_pages() to return folios -* to the LRU or free list only if ret != 0. +* The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios +* are movable any more because the list has become empty or no retryable folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. * * Returns the number of {normal folio, large folio, hugetlb} that were not * migrated, or an error code. The number of large folio splits will be
@@ -1439,7 +1530,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,int retry =1 ;int large_retry =1 ;int thp_retry =1 ; -int nr_failed =0 ; +int nr_failed;int nr_retry_pages =0 ;int nr_large_failed =0 ;int pass =0 ;
@@ -1456,38 +1547,45 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,trace_mm_migrate_pages_start (mode, reason);memset (&stats,0 ,sizeof (stats)); + rc =migrate_hugetlbs (from, get_new_page, put_new_page, private, mode, reason, + &stats, &ret_folios); +if (rc <0 ) + goto out; + nr_failed = rc; + split_folio_migration: -for (pass =0 ; pass <10 && (retry || large_retry); pass++) { +for (pass =0 ; + pass <NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); + pass++) { retry =0 ; large_retry =0 ; thp_retry =0 ; nr_retry_pages =0 ;list_for_each_entry_safe (folio, folio2, from, lru) { +/* Retried hugetlb folios will be kept in list */ +if (folio_test_hugetlb (folio)) { +list_move_tail (&folio->lru, &ret_folios); +continue ; + } +/* * Large folio statistics is based on the source large * folio. Capture required information that might get * lost during migration. */ - is_large =folio_test_large (folio) && !folio_test_hugetlb (folio); + is_large =folio_test_large (folio); is_thp = is_large &&folio_test_pmd_mappable (folio); nr_pages =folio_nr_pages (folio); +cond_resched (); -if (folio_test_hugetlb (folio)) - rc =unmap_and_move_huge_page (get_new_page, - put_new_page, private, - &folio->page, pass >2 , mode, - reason, - &ret_folios); -else - rc =unmap_and_move (get_new_page, put_new_page, - private, folio, pass >2 , mode, - reason, &ret_folios); + rc =unmap_and_move (get_new_page, put_new_page, + private, folio, pass >2 , mode, + reason, &ret_folios);/* * The rules are: -* Success: non hugetlb folio will be freed, hugetlb -* folio will be put back +* Success: folio will be freed * -EAGAIN: stay on the from list * -ENOMEM: stay on the from list * -ENOSYS: stay on the from list
@@ -1514,7 +1612,6 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, stats.nr_thp_split += is_thp;break ; } -/* Hugetlb migration is unsupported */ }else if (!no_split_folio_counting) { nr_failed++; }
@@ -1608,8 +1705,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,*/ if (!list_empty (&split_folios)) {/* -* Move non-migrated folios (after 10 retries) to ret_folios -* to avoid migrating them again. +* Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY +* retries) to ret_folios to avoid migrating them again. */ list_splice_init (from, &ret_folios);list_splice_init (&split_folios, from);
Signature · 1 line
2.35.1
Re: [PATCH -v3 2/9] migrate_pages: separate hugetlb folios migration
Quoted 157 lines · Huang Ying
This is a preparation patch to batch the folio unmapping and moving for the non-hugetlb folios. Based on that we can batch the TLB shootdown during the folio migration and make it possible to use some hardware accelerator for the folio copying.
In this patch the hugetlb folios and non-hugetlb folios migration is separated in
migrate_pages()to make it easy to change the non-hugetlb folios migration implementation.Signed-off-by "Huang, Ying" <[email protected]>Reviewed-by Baolin Wang <[email protected]>Cc Zi Yan <[email protected]>Cc Yang Shi <[email protected]>Cc Oscar Salvador <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Bharata B Rao <[email protected]>Cc Alistair Popple <[email protected]>Cc haoxin <[email protected]>Cc Minchan Kim <[email protected]>--- mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-)mm/migrate.c +96
-5@@ -1396,6 +1396,8 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_freturn rc; } +#define NR_MAX_MIGRATE_PAGES_RETRY 10 +struct migrate_pages_stats {int nr_succeeded;/* Normal and large folios migrated successfully, in units of base pages */ @@ -1406,6 +1408,95 @@ struct migrate_pages_stats {int nr_thp_split;/* THP split before migrating */ }; +/* +* Returns the number of hugetlb folios that were not migrated, or an error code +* after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable +* any more because the list has become empty or no retryable hugetlb folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. +*/ +static int migrate_hugetlbs (struct list_head *from,new_page_t get_new_page, +free_page_t put_new_page,unsigned long private, +enum migrate_mode mode,int reason, +struct migrate_pages_stats *stats, +struct list_head *ret_folios) +{ +int retry =1 ; +int nr_failed =0 ; +int nr_retry_pages =0 ; +int pass =0 ; +struct folio *folio, *folio2; +int rc, nr_pages; + +for (pass =0 ; pass <NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) { + retry =0 ; + nr_retry_pages =0 ; + +list_for_each_entry_safe (folio, folio2, from, lru) { +if (!folio_test_hugetlb (folio)) +continue ; + + nr_pages =folio_nr_pages (folio); + +cond_resched (); + + rc =unmap_and_move_huge_page (get_new_page, + put_new_page, private, + &folio->page, pass >2 , mode, + reason, ret_folios); +/* +* The rules are: +* Success: hugetlb folio will be put back +* -EAGAIN: stay on the from list +* -ENOMEM: stay on the from list +* -ENOSYS: stay on the from list +* Other errno: put on ret_folios list +*/ +switch (rc) { +case -ENOSYS : +/* Hugetlb migration is unsupported */ + nr_failed++; + stats->nr_failed_pages += nr_pages; +list_move_tail (&folio->lru, ret_folios); +break ; +case -ENOMEM : +/* +* When memory is low, don't bother to try to migrate +* other folios, just exit. +*/ + stats->nr_failed_pages += nr_pages + nr_retry_pages; +return -ENOMEM ; +case -EAGAIN : + retry++; + nr_retry_pages += nr_pages; +break ; +case MIGRATEPAGE_SUCCESS : + stats->nr_succeeded += nr_pages; +break ; +default : +/* +* Permanent failure (-EBUSY, etc.): +* unlike -EAGAIN case, the failed folio is +* removed from migration folio list and not +* retried in the next outer loop. +*/ + nr_failed++; + stats->nr_failed_pages += nr_pages; +break ; + } + } + } +/* +* nr_failed is number of hugetlb folios failed to be migrated. After +* NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb +* folios as failed. +*/ + nr_failed += retry; + stats->nr_failed_pages += nr_retry_pages; + +return nr_failed; +} +/* * migrate_pages - migrate the folios specified in a list, to the free folios * supplied as the target for the page migration @@ -1422,10 +1513,10 @@ struct migrate_pages_stats {* @ret_succeeded: Set to the number of folios migrated successfully if * the caller passes a non-NULL pointer. * -* The function returns after 10 attempts or if no folios are movable any more -* because the list has become empty or no retryable folios exist any more. -* It is caller's responsibility to call putback_movable_pages() to return folios -* to the LRU or free list only if ret != 0. +* The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios +* are movable any more because the list has become empty or no retryable folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. * * Returns the number of {normal folio, large folio, hugetlb} that were not * migrated, or an error code. The number of large folio splits will be @@ -1439,7 +1530,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,int retry =1 ;int large_retry =1 ;int thp_retry =1 ; -int nr_failed =0 ; +int nr_failed;int nr_retry_pages =0 ;int nr_large_failed =0 ;int pass =0 ;
mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1553
Quoted 7 lines
mm/migrate.c +4
-0@@ -1456,38 +1547,45 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,trace_mm_migrate_pages_start (mode, reason);memset (&stats,0 ,sizeof (stats)); + rc =migrate_hugetlbs (from, get_new_page, put_new_page, private, mode, reason, + &stats, &ret_folios); +if (rc <0 ) + goto out;
How about continue migrate small page for -ENOMEM case? maybe there are still free small pages.
Quoted 71 lines
mm/migrate.c +19
-17+ nr_failed = rc; + split_folio_migration: -for (pass =0 ; pass <10 && (retry || large_retry); pass++) { +for (pass =0 ; + pass <NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); + pass++) { retry =0 ; large_retry =0 ; thp_retry =0 ; nr_retry_pages =0 ;list_for_each_entry_safe (folio, folio2, from, lru) { +/* Retried hugetlb folios will be kept in list */ +if (folio_test_hugetlb (folio)) { +list_move_tail (&folio->lru, &ret_folios); +continue ; + } +/* * Large folio statistics is based on the source large * folio. Capture required information that might get * lost during migration. */ - is_large =folio_test_large (folio) && !folio_test_hugetlb (folio); + is_large =folio_test_large (folio); is_thp = is_large &&folio_test_pmd_mappable (folio); nr_pages =folio_nr_pages (folio); +cond_resched (); -if (folio_test_hugetlb (folio)) - rc =unmap_and_move_huge_page (get_new_page, - put_new_page, private, - &folio->page, pass >2 , mode, - reason, - &ret_folios); -else - rc =unmap_and_move (get_new_page, put_new_page, - private, folio, pass >2 , mode, - reason, &ret_folios); + rc =unmap_and_move (get_new_page, put_new_page, + private, folio, pass >2 , mode, + reason, &ret_folios);/* * The rules are: -* Success: non hugetlb folio will be freed, hugetlb -* folio will be put back +* Success: folio will be freed * -EAGAIN: stay on the from list * -ENOMEM: stay on the from list * -ENOSYS: stay on the from list @@ -1514,7 +1612,6 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, stats.nr_thp_split += is_thp;break ; } -/* Hugetlb migration is unsupported */ }else if (!no_split_folio_counting) { nr_failed++; }@@ -1608,8 +1705,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,*/ if (!list_empty (&split_folios)) {/* -* Move non-migrated folios (after 10 retries) to ret_folios -* to avoid migrating them again. +* Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY +* retries) to ret_folios to avoid migrating them again. */ list_splice_init (from, &ret_folios);list_splice_init (&split_folios, from);
Re: [PATCH -v3 2/9] migrate_pages: separate hugetlb folios migration
Quoted 157 lines · Chen Wandun
Quoted 156 lines · Huang Ying
This is a preparation patch to batch the folio unmapping and moving for the non-hugetlb folios. Based on that we can batch the TLB shootdown during the folio migration and make it possible to use some hardware accelerator for the folio copying.
In this patch the hugetlb folios and non-hugetlb folios migration is separated in
migrate_pages()to make it easy to change the non-hugetlb folios migration implementation.Signed-off-by "Huang, Ying" <[email protected]>Reviewed-by Baolin Wang <[email protected]>Cc Zi Yan <[email protected]>Cc Yang Shi <[email protected]>Cc Oscar Salvador <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Bharata B Rao <[email protected]>Cc Alistair Popple <[email protected]>Cc haoxin <[email protected]>Cc Minchan Kim <[email protected]>--- mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-)mm/migrate.c +94
-5@@ -1396,6 +1396,8 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_freturn rc; } +#define NR_MAX_MIGRATE_PAGES_RETRY 10 +struct migrate_pages_stats {int nr_succeeded;/* Normal and large folios migrated successfully, in units of base pages */ @@ -1406,6 +1408,95 @@ struct migrate_pages_stats {int nr_thp_split;/* THP split before migrating */ }; +/* +* Returns the number of hugetlb folios that were not migrated, or an error code +* after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable +* any more because the list has become empty or no retryable hugetlb folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. +*/ +static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page, +free_page_t put_new_page,unsigned long private, +enum migrate_mode mode,int reason, +struct migrate_pages_stats *stats, +struct list_head *ret_folios) +{ +int retry =1 ; +int nr_failed =0 ; +int nr_retry_pages =0 ; +int pass =0 ; +struct folio *folio, *folio2; +int rc, nr_pages; + +for (pass =0 ; pass <NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) { + retry =0 ; + nr_retry_pages =0 ; + +list_for_each_entry_safe (folio, folio2, from, lru) { +if (!folio_test_hugetlb (folio)) +continue ; + + nr_pages =folio_nr_pages (folio); + +cond_resched (); + + rc =unmap_and_move_huge_page (get_new_page, + put_new_page, private, + &folio->page, pass >2 , mode, + reason, ret_folios); +/* +* The rules are: +* Success: hugetlb folio will be put back +* -EAGAIN: stay on the from list +* -ENOMEM: stay on the from list +* -ENOSYS: stay on the from list +* Other errno: put on ret_folios list +*/ +switch (rc) { +case -ENOSYS : +/* Hugetlb migration is unsupported */ + nr_failed++; + stats->nr_failed_pages += nr_pages; +list_move_tail (&folio->lru, ret_folios); +break ; +case -ENOMEM : +/* +* When memory is low, don't bother to try to migrate +* other folios, just exit. +*/ + stats->nr_failed_pages += nr_pages + nr_retry_pages; +return -ENOMEM ; +case -EAGAIN : + retry++; + nr_retry_pages += nr_pages; +break ; +case MIGRATEPAGE_SUCCESS : + stats->nr_succeeded += nr_pages; +break ; +default : +/* +* Permanent failure (-EBUSY, etc.): +* unlike -EAGAIN case, the failed folio is +* removed from migration folio list and not +* retried in the next outer loop. +*/ + nr_failed++; + stats->nr_failed_pages += nr_pages; +break ; + } + } + } +/* +* nr_failed is number of hugetlb folios failed to be migrated. After +* NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb +* folios as failed. +*/ + nr_failed += retry; + stats->nr_failed_pages += nr_retry_pages; + +return nr_failed; +} +/* * migrate_pages - migrate the folios specified in a list, to the free folios * supplied as the target for the page migration @@ -1422,10 +1513,10 @@ struct migrate_pages_stats {* @ret_succeeded: Set to the number of folios migrated successfully if * the caller passes a non-NULL pointer. * -* The function returns after 10 attempts or if no folios are movable any more -* because the list has become empty or no retryable folios exist any more. -* It is caller's responsibility to call putback_movable_pages() to return folios -* to the LRU or free list only if ret != 0. +* The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios +* are movable any more because the list has become empty or no retryable folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. * * Returns the number of {normal folio, large folio, hugetlb} that were not * migrated, or an error code. The number of large folio splits will be @@ -1439,7 +1530,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,int retry =1 ;int large_retry =1 ;int thp_retry =1 ; -int nr_failed =0 ; +int nr_failed;int nr_retry_pages =0 ;int nr_large_failed =0 ;int pass =0 ;@@ -1456,38 +1547,45 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,trace_mm_migrate_pages_start (mode, reason);
Quoted 8 lines
mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1553
Quoted 5 lines
mm/migrate.c +4
-0memset (&stats,0 ,sizeof (stats)); + rc =migrate_hugetlbs (from, get_new_page, put_new_page, private, mode, reason, + &stats, &ret_folios); +if (rc <0 ) + goto out;How about continue migrate small page for
-ENOMEMcase? maybe there are still
free small pages.
Sounds reasonable to me. How about do that on top of this series? Do you have interest to do that?
Best Regards,
Huang, Ying
Re: [PATCH -v3 2/9] migrate_pages: separate hugetlb folios migration
Quoted 167 lines · Ying
Quoted 157 lines · Chen Wandun
Quoted 156 lines · Huang Ying
This is a preparation patch to batch the folio unmapping and moving for the non-hugetlb folios. Based on that we can batch the TLB shootdown during the folio migration and make it possible to use some hardware accelerator for the folio copying.
In this patch the hugetlb folios and non-hugetlb folios migration is separated in
migrate_pages()to make it easy to change the non-hugetlb folios migration implementation.Signed-off-by "Huang, Ying" <[email protected]>Reviewed-by Baolin Wang <[email protected]>Cc Zi Yan <[email protected]>Cc Yang Shi <[email protected]>Cc Oscar Salvador <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Bharata B Rao <[email protected]>Cc Alistair Popple <[email protected]>Cc haoxin <[email protected]>Cc Minchan Kim <[email protected]>--- mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 22 deletions(-)mm/migrate.c +94
-5@@ -1396,6 +1396,8 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_freturn rc; } +#define NR_MAX_MIGRATE_PAGES_RETRY 10 +struct migrate_pages_stats {int nr_succeeded;/* Normal and large folios migrated successfully, in units of base pages */ @@ -1406,6 +1408,95 @@ struct migrate_pages_stats {int nr_thp_split;/* THP split before migrating */ }; +/* +* Returns the number of hugetlb folios that were not migrated, or an error code +* after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no hugetlb folios are movable +* any more because the list has become empty or no retryable hugetlb folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. +*/ +static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page, +free_page_t put_new_page,unsigned long private, +enum migrate_mode mode,int reason, +struct migrate_pages_stats *stats, +struct list_head *ret_folios) +{ +int retry =1 ; +int nr_failed =0 ; +int nr_retry_pages =0 ; +int pass =0 ; +struct folio *folio, *folio2; +int rc, nr_pages; + +for (pass =0 ; pass <NR_MAX_MIGRATE_PAGES_RETRY && retry; pass++) { + retry =0 ; + nr_retry_pages =0 ; + +list_for_each_entry_safe (folio, folio2, from, lru) { +if (!folio_test_hugetlb (folio)) +continue ; + + nr_pages =folio_nr_pages (folio); + +cond_resched (); + + rc =unmap_and_move_huge_page (get_new_page, + put_new_page, private, + &folio->page, pass >2 , mode, + reason, ret_folios); +/* +* The rules are: +* Success: hugetlb folio will be put back +* -EAGAIN: stay on the from list +* -ENOMEM: stay on the from list +* -ENOSYS: stay on the from list +* Other errno: put on ret_folios list +*/ +switch (rc) { +case -ENOSYS : +/* Hugetlb migration is unsupported */ + nr_failed++; + stats->nr_failed_pages += nr_pages; +list_move_tail (&folio->lru, ret_folios); +break ; +case -ENOMEM : +/* +* When memory is low, don't bother to try to migrate +* other folios, just exit. +*/ + stats->nr_failed_pages += nr_pages + nr_retry_pages; +return -ENOMEM ; +case -EAGAIN : + retry++; + nr_retry_pages += nr_pages; +break ; +case MIGRATEPAGE_SUCCESS : + stats->nr_succeeded += nr_pages; +break ; +default : +/* +* Permanent failure (-EBUSY, etc.): +* unlike -EAGAIN case, the failed folio is +* removed from migration folio list and not +* retried in the next outer loop. +*/ + nr_failed++; + stats->nr_failed_pages += nr_pages; +break ; + } + } + } +/* +* nr_failed is number of hugetlb folios failed to be migrated. After +* NR_MAX_MIGRATE_PAGES_RETRY attempts, give up and count retried hugetlb +* folios as failed. +*/ + nr_failed += retry; + stats->nr_failed_pages += nr_retry_pages; + +return nr_failed; +} +/* * migrate_pages - migrate the folios specified in a list, to the free folios * supplied as the target for the page migration @@ -1422,10 +1513,10 @@ struct migrate_pages_stats {* @ret_succeeded: Set to the number of folios migrated successfully if * the caller passes a non-NULL pointer. * -* The function returns after 10 attempts or if no folios are movable any more -* because the list has become empty or no retryable folios exist any more. -* It is caller's responsibility to call putback_movable_pages() to return folios -* to the LRU or free list only if ret != 0. +* The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios +* are movable any more because the list has become empty or no retryable folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. * * Returns the number of {normal folio, large folio, hugetlb} that were not * migrated, or an error code. The number of large folio splits will be @@ -1439,7 +1530,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,int retry =1 ;int large_retry =1 ;int thp_retry =1 ; -int nr_failed =0 ; +int nr_failed;int nr_retry_pages =0 ;int nr_large_failed =0 ;int pass =0 ;@@ -1456,38 +1547,45 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,trace_mm_migrate_pages_start (mode, reason);Quoted 8 lines
mm/migrate.c int migrate_pages(struct list_head *from, new_page_t get_new_page, · line 1553
Quoted 5 lines
mm/migrate.c +4
-0memset (&stats,0 ,sizeof (stats)); + rc =migrate_hugetlbs (from, get_new_page, put_new_page, private, mode, reason, + &stats, &ret_folios); +if (rc <0 ) + goto out;How about continue migrate small page for
-ENOMEMcase? maybe there are still
free small pages.
Quoted 2 lines
Sounds reasonable to me. How about do that on top of this series? Do you have interest to do that?
Happy to do that, thanks for you reply.
Best Regards,
Wandun
Quoted 5 lines
Best Regards,
Huang, Ying.
PATCH 3/9 migrate_pages: restrict number of pages to migrate in batch
In reply to Huang YingThis is a preparation patch to batch the folio unmapping and moving for non-hugetlb folios.
If we had batched the folio unmapping, all folios to be migrated would be unmapped before copying the contents and flags of the folios. If the folios that were passed to migrate_pages() were too many in unit of pages, the execution of the processes would be stopped for too long time, thus too long latency. For example, migrate_pages() syscall will call migrate_pages() with all folios of a process. To avoid this possible issue, in this patch, we restrict the number of pages to be migrated to be no more than HPAGE_PMD_NR. That is, the influence is at the same level of THP migration.
--- mm/migrate.c | 174 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 106 insertions(+), 68 deletions(-)
mm/migrate.c +106 -68
@@ -1396,6 +1396,11 @@ static inline int try_split_folio(struct folio *folio, struct list_head *split_freturn rc; } +#ifdef CONFIG_TRANSPARENT_HUGEPAGE +#define NR_MAX_BATCHED_MIGRATION HPAGE_PMD_NR +#else +#define NR_MAX_BATCHED_MIGRATION 512 +#endif #define NR_MAX_MIGRATE_PAGES_RETRY 10struct migrate_pages_stats {
@@ -1497,40 +1502,15 @@ static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page,return nr_failed; } -/* -* migrate_pages - migrate the folios specified in a list, to the free folios -* supplied as the target for the page migration -* -* @from: The list of folios to be migrated. -* @get_new_page: The function used to allocate free folios to be used -* as the target of the folio migration. -* @put_new_page: The function used to free target folios if migration -* fails, or NULL if no special handling is necessary. -* @private: Private data to be passed on to get_new_page() -* @mode: The migration mode that specifies the constraints for -* folio migration, if any. -* @reason: The reason for folio migration. -* @ret_succeeded: Set to the number of folios migrated successfully if -* the caller passes a non-NULL pointer. -* -* The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios -* are movable any more because the list has become empty or no retryable folios -* exist any more. It is caller's responsibility to call putback_movable_pages() -* only if ret != 0. -* -* Returns the number of {normal folio, large folio, hugetlb} that were not -* migrated, or an error code. The number of large folio splits will be -* considered as the number of non-migrated large folio, no matter how many -* split folios of the large folio are migrated successfully. -*/ -int migrate_pages (struct list_head *from,new_page_t get_new_page, +static int migrate_pages_batch (struct list_head *from,new_page_t get_new_page,free_page_t put_new_page,unsigned long private, -enum migrate_mode mode,int reason,unsigned int *ret_succeeded) +enum migrate_mode mode,int reason,struct list_head *ret_folios, +struct migrate_pages_stats *stats) {int retry =1 ;int large_retry =1 ;int thp_retry =1 ; -int nr_failed; +int nr_failed =0 ;int nr_retry_pages =0 ;int nr_large_failed =0 ;int pass =0 ;
@@ -1538,20 +1518,9 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,bool is_thp = false;struct folio *folio, *folio2;int rc, nr_pages; -LIST_HEAD (ret_folios);LIST_HEAD (split_folios);bool nosplit = (reason ==MR_NUMA_MISPLACED );bool no_split_folio_counting = false; -struct migrate_pages_stats stats; - -trace_mm_migrate_pages_start (mode, reason); - -memset (&stats,0 ,sizeof (stats)); - rc =migrate_hugetlbs (from, get_new_page, put_new_page, private, mode, reason, - &stats, &ret_folios); -if (rc <0 ) - goto out; - nr_failed = rc; split_folio_migration: for (pass = 0;
@@ -1563,12 +1532,6 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_retry_pages =0 ;list_for_each_entry_safe (folio, folio2, from, lru) { -/* Retried hugetlb folios will be kept in list */ -if (folio_test_hugetlb (folio)) { -list_move_tail (&folio->lru, &ret_folios); -continue ; - } -/* * Large folio statistics is based on the source large * folio. Capture required information that might get
@@ -1582,15 +1545,14 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, rc =unmap_and_move (get_new_page, put_new_page, private, folio, pass >2 , mode, - reason, &ret_folios); + reason, ret_folios);/* * The rules are: * Success: folio will be freed * -EAGAIN: stay on the from list * -ENOMEM: stay on the from list * -ENOSYS: stay on the from list -* Other errno: put on ret_folios list then splice to -* from list +* Other errno: put on ret_folios list */ switch (rc) {/*
@@ -1607,17 +1569,17 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,/* Large folio migration is unsupported */ if (is_large) { nr_large_failed++; - stats.nr_thp_failed += is_thp; + stats->nr_thp_failed += is_thp;if (!try_split_folio (folio, &split_folios)) { - stats.nr_thp_split += is_thp; + stats->nr_thp_split += is_thp;break ; } }else if (!no_split_folio_counting) { nr_failed++; } - stats.nr_failed_pages += nr_pages; -list_move_tail (&folio->lru, &ret_folios); + stats->nr_failed_pages += nr_pages; +list_move_tail (&folio->lru, ret_folios);break ;case -ENOMEM :/*
@@ -1626,13 +1588,13 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,*/ if (is_large) { nr_large_failed++; - stats.nr_thp_failed += is_thp; + stats->nr_thp_failed += is_thp;/* Large folio NUMA faulting doesn't split to retry. */ if (!nosplit) {int ret =try_split_folio (folio, &split_folios);if (!ret) { - stats.nr_thp_split += is_thp; + stats->nr_thp_split += is_thp;break ; }else if (reason ==MR_LONGTERM_PIN && ret == -EAGAIN ) {
@@ -1650,17 +1612,17 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_failed++; } - stats.nr_failed_pages += nr_pages + nr_retry_pages; + stats->nr_failed_pages += nr_pages + nr_retry_pages;/* * There might be some split folios of fail-to-migrate large -* folios left in split_folios list. Move them back to migration +* folios left in split_folios list. Move them to ret_folios * list so that they could be put back to the right list by * the caller otherwise the folio refcnt will be leaked. */ -list_splice_init (&split_folios, from); +list_splice_init (&split_folios, ret_folios);/* nr_failed isn't updated for not used */ nr_large_failed += large_retry; - stats.nr_thp_failed += thp_retry; + stats->nr_thp_failed += thp_retry; goto out;case -EAGAIN :if (is_large) {
@@ -1672,8 +1634,8 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, nr_retry_pages += nr_pages;break ;case MIGRATEPAGE_SUCCESS : - stats.nr_succeeded += nr_pages; - stats.nr_thp_succeeded += is_thp; + stats->nr_succeeded += nr_pages; + stats->nr_thp_succeeded += is_thp;break ;default :/*
@@ -1684,20 +1646,20 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,*/ if (is_large) { nr_large_failed++; - stats.nr_thp_failed += is_thp; + stats->nr_thp_failed += is_thp; }else if (!no_split_folio_counting) { nr_failed++; } - stats.nr_failed_pages += nr_pages; + stats->nr_failed_pages += nr_pages;break ; } } } nr_failed += retry; nr_large_failed += large_retry; - stats.nr_thp_failed += thp_retry; - stats.nr_failed_pages += nr_retry_pages; + stats->nr_thp_failed += thp_retry; + stats->nr_failed_pages += nr_retry_pages;/* * Try to migrate split folios of fail-to-migrate large folios, no * nr_failed counting in this round, since all split folios of a
@@ -1708,7 +1670,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,* Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY * retries) to ret_folios to avoid migrating them again. */ -list_splice_init (from, &ret_folios); +list_splice_init (from, ret_folios);list_splice_init (&split_folios, from); no_split_folio_counting = true; retry =1 ;
@@ -1716,6 +1678,82 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page, } rc = nr_failed + nr_large_failed; +out: +return rc; +} + +/* +* migrate_pages - migrate the folios specified in a list, to the free folios +* supplied as the target for the page migration +* +* @from: The list of folios to be migrated. +* @get_new_page: The function used to allocate free folios to be used +* as the target of the folio migration. +* @put_new_page: The function used to free target folios if migration +* fails, or NULL if no special handling is necessary. +* @private: Private data to be passed on to get_new_page() +* @mode: The migration mode that specifies the constraints for +* folio migration, if any. +* @reason: The reason for folio migration. +* @ret_succeeded: Set to the number of folios migrated successfully if +* the caller passes a non-NULL pointer. +* +* The function returns after NR_MAX_MIGRATE_PAGES_RETRY attempts or if no folios +* are movable any more because the list has become empty or no retryable folios +* exist any more. It is caller's responsibility to call putback_movable_pages() +* only if ret != 0. +* +* Returns the number of {normal folio, large folio, hugetlb} that were not +* migrated, or an error code. The number of large folio splits will be +* considered as the number of non-migrated large folio, no matter how many +* split folios of the large folio are migrated successfully. +*/ +int migrate_pages (struct list_head *from,new_page_t get_new_page, +free_page_t put_new_page,unsigned long private, +enum migrate_mode mode,int reason,unsigned int *ret_succeeded) +{ +int rc, rc_gather; +int nr_pages; +struct folio *folio, *folio2; +LIST_HEAD (folios); +LIST_HEAD (ret_folios); +struct migrate_pages_stats stats; + +trace_mm_migrate_pages_start (mode, reason); + +memset (&stats,0 ,sizeof (stats)); + + rc_gather =migrate_hugetlbs (from, get_new_page, put_new_page, private, + mode, reason, &stats, &ret_folios); +if (rc_gather <0 ) + goto out; +again: + nr_pages =0 ; +list_for_each_entry_safe (folio, folio2, from, lru) { +/* Retried hugetlb folios will be kept in list */ +if (folio_test_hugetlb (folio)) { +list_move_tail (&folio->lru, &ret_folios); +continue ; + } + + nr_pages +=folio_nr_pages (folio); +if (nr_pages >NR_MAX_BATCHED_MIGRATION ) +break ; + } +if (nr_pages >NR_MAX_BATCHED_MIGRATION ) +list_cut_before (&folios, from, &folio->lru); +else +list_splice_init (from, &folios); + rc =migrate_pages_batch (&folios, get_new_page, put_new_page, private, + mode, reason, &ret_folios, &stats); +list_splice_tail_init (&folios, &ret_folios); +if (rc <0 ) { + rc_gather = rc; + goto out; + } + rc_gather += rc; +if (!list_empty (from)) + goto again; out:/* * Put the permanent failure folio back to migration list, they
@@ -1728,7 +1766,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,* are migrated successfully. */ if (list_empty (from)) - rc =0 ; + rc_gather =0 ;count_vm_events (PGMIGRATE_SUCCESS , stats.nr_succeeded);count_vm_events (PGMIGRATE_FAIL , stats.nr_failed_pages);
@@ -1742,7 +1780,7 @@ int migrate_pages(struct list_head *from, new_page_t get_new_page,if (ret_succeeded) *ret_succeeded = stats.nr_succeeded; -return rc; +return rc_gather; }struct page *alloc_migration_target (struct page *page,unsigned long private)
Signature · 1 line
2.35.1
PATCH 4/9 migrate_pages: split unmap_and_move() to _unmap() and _move()
In reply to Huang YingThis is a preparation patch to batch the folio unmapping and moving.
In this patch, unmap_and_move() is split to migrate_folio_unmap() and migrate_folio_move(). So, we can batch _unmap() and _move() in different loops later. To pass some information between unmap and move, the original unused dst->mapping and dst->private are used.
--- include/linux/migrate.h | 1 + mm/migrate.c | 170 ++++++++++++++++++++++++++++++---------- 2 files changed, 130 insertions(+), 41 deletions(-)
include/linux/migrate.h +1 -0
@@ -18,6 +18,7 @@ struct migration_target_control;* - zero on page migration success; */ #define MIGRATEPAGE_SUCCESS 0 +#define MIGRATEPAGE_UNMAP 1/** * struct movable_operations - Driver page migration
mm/migrate.c +129 -41
246 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 5/9 migrate_pages: batch _unmap and _move
In reply to Huang YingIn this patch the _unmap and _move stage of the folio migration is batched. That for, previously, it is,
for each folio
_unmap()
_move()Now, it is,
for each folio
_unmap()
for each folio
_move()Based on this, we can batch the TLB flushing and use some hardware accelerator to copy folios between batched _unmap and batched _move stages.
--- mm/migrate.c | 207 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 183 insertions(+), 24 deletions(-)
mm/migrate.c +183 -24
@@ -1033,6 +1033,33 @@ static void __migrate_folio_extract(struct folio *dst, dst->private =NULL ; } +/* Restore the source folio to the original state upon failure */ +static void migrate_folio_undo_src (struct folio *src, +int page_was_mapped, +struct anon_vma *anon_vma, +struct list_head *ret) +{ +if (page_was_mapped) +remove_migration_ptes (src, src, false); +/* Drop an anon_vma reference if we took one */ +if (anon_vma) +put_anon_vma (anon_vma); +folio_unlock (src); +list_move_tail (&src->lru, ret); +} + +/* Restore the destination folio to the original state upon failure */ +static void migrate_folio_undo_dst (struct folio *dst, +free_page_t put_new_page, +unsigned long private) +{ +folio_unlock (dst); +if (put_new_page) +put_new_page (&dst->page, private); +else +folio_put (dst); +} +/* Cleanup src folio upon migration success */ static void migrate_folio_done (struct folio *src,enum migrate_reason reason)
@@ -1052,7 +1079,7 @@ static void migrate_folio_done(struct folio *src, }static int __migrate_folio_unmap (struct folio *src,struct folio *dst, -int force,enum migrate_mode mode) +int force,bool force_lock,enum migrate_mode mode) {int rc = -EAGAIN ;int page_was_mapped =0 ;
@@ -1079,6 +1106,17 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,if (current->flags &PF_MEMALLOC ) goto out; +/* +* We have locked some folios, to avoid deadlock, we cannot +* lock the folio synchronously. Go out to process (and +* unlock) all the locked folios. Then we can lock the folio +* synchronously. +*/ +if (!force_lock) { + rc = -EDEADLOCK ; + goto out; + } +folio_lock (src); }
@@ -1191,6 +1229,10 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,__migrate_folio_extract (dst, &page_was_mapped, &anon_vma); rc =move_to_new_folio (dst, src, mode); + +if (rc != -EAGAIN ) +list_del (&dst->lru); +if (unlikely (!is_lru)) goto out_unlock_both;
@@ -1209,6 +1251,11 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,lru_add_drain (); } +if (rc == -EAGAIN ) { +__migrate_folio_record (dst, page_was_mapped, anon_vma); +return rc; + } +if (page_was_mapped)remove_migration_ptes (src, rc ==MIGRATEPAGE_SUCCESS ? dst : src, false);
@@ -1233,7 +1280,7 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,/* Obtain the lock on page, remove all ptes. */ static int migrate_folio_unmap (new_page_t get_new_page,free_page_t put_new_page,unsigned long private,struct folio *src, -struct folio **dstp,int force, +struct folio **dstp,int force,bool force_lock,enum migrate_mode mode,enum migrate_reason reason,struct list_head *ret) {
@@ -1261,7 +1308,7 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page *dstp = dst; dst->private =NULL ; - rc =__migrate_folio_unmap (src, dst, force, mode); + rc =__migrate_folio_unmap (src, dst, force, force_lock, mode);if (rc ==MIGRATEPAGE_UNMAP )return rc;
@@ -1270,7 +1317,7 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page* references and be restored. */ /* restore the folio to right list. */ -if (rc != -EAGAIN ) +if (rc != -EAGAIN && rc != -EDEADLOCK )list_move_tail (&src->lru, ret);if (put_new_page)
@@ -1309,9 +1356,8 @@ static int migrate_folio_move(free_page_t put_new_page, unsigned long private,*/ if (rc ==MIGRATEPAGE_SUCCESS ) {migrate_folio_done (src, reason); - }else { -if (rc != -EAGAIN ) -list_add_tail (&src->lru, ret); + }else if (rc != -EAGAIN ) { +list_add_tail (&src->lru, ret);if (put_new_page)put_new_page (&dst->page, private);
@@ -1591,7 +1637,7 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, enum migrate_mode mode, int reason, struct list_head *ret_folios, struct migrate_pages_stats *stats) { - int retry = 1; + int retry;int large_retry =1 ;int thp_retry =1 ;int nr_failed =0 ;
@@ -1600,13 +1646,19 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,int pass =0 ;bool is_large = false;bool is_thp = false; -struct folio *folio, *folio2, *dst =NULL ; -int rc, nr_pages; +struct folio *folio, *folio2, *dst =NULL , *dst2; +int rc, rc_saved, nr_pages;LIST_HEAD (split_folios); +LIST_HEAD (unmap_folios); +LIST_HEAD (dst_folios);bool nosplit = (reason ==MR_NUMA_MISPLACED );bool no_split_folio_counting = false; +bool force_lock; -split_folio_migration: +retry: + rc_saved =0 ; + force_lock = true; + retry =1 ;for (pass =0 ; pass <NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); pass++) {
@@ -1628,16 +1680,15 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,cond_resched (); rc =migrate_folio_unmap (get_new_page, put_new_page, private, - folio, &dst, pass >2 , mode, - reason, ret_folios); -if (rc ==MIGRATEPAGE_UNMAP ) - rc =migrate_folio_move (put_new_page, private, - folio, dst, mode, - reason, ret_folios); + folio, &dst, pass >2 , force_lock, + mode, reason, ret_folios);/* * The rules are: * Success: folio will be freed +* Unmap: folio will be put on unmap_folios list, +* dst folio put on dst_folios list * -EAGAIN: stay on the from list +* -EDEADLOCK: stay on the from list * -ENOMEM: stay on the from list * -ENOSYS: stay on the from list * Other errno: put on ret_folios list
@@ -1672,7 +1723,7 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,case -ENOMEM :/* * When memory is low, don't bother to try to migrate -* other folios, just exit. +* other folios, move unmapped folios, then exit. */ if (is_large) { nr_large_failed++;
@@ -1711,7 +1762,19 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,/* nr_failed isn't updated for not used */ nr_large_failed += large_retry; stats->nr_thp_failed += thp_retry; - goto out; + rc_saved = rc; +if (list_empty (&unmap_folios)) + goto out; +else + goto move; +case -EDEADLOCK : +/* +* The folio cannot be locked for potential deadlock. +* Go move (and unlock) all locked folios. Then we can +* try again. +*/ + rc_saved = rc; + goto move;case -EAGAIN :if (is_large) { large_retry++;
@@ -1725,6 +1788,15 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, stats->nr_succeeded += nr_pages; stats->nr_thp_succeeded += is_thp;break ; +case MIGRATEPAGE_UNMAP : +/* +* We have locked some folios, don't force lock +* to avoid deadlock. +*/ + force_lock = false; +list_move_tail (&folio->lru, &unmap_folios); +list_add_tail (&dst->lru, &dst_folios); +break ;default :/* * Permanent failure (-EBUSY, etc.):
@@ -1748,12 +1820,95 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, nr_large_failed += large_retry; stats->nr_thp_failed += thp_retry; stats->nr_failed_pages += nr_retry_pages; +move: + retry =1 ; +for (pass =0 ; + pass <NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry); + pass++) { + retry =0 ; + large_retry =0 ; + thp_retry =0 ; + nr_retry_pages =0 ; + + dst = list_first_entry(&dst_folios, struct folio, lru); + dst2 =list_next_entry (dst, lru); +list_for_each_entry_safe (folio, folio2, &unmap_folios, lru) { + is_large =folio_test_large (folio); + is_thp = is_large &&folio_test_pmd_mappable (folio); + nr_pages =folio_nr_pages (folio); + +cond_resched (); + + rc =migrate_folio_move (put_new_page, private, + folio, dst, mode, + reason, ret_folios); +/* +* The rules are: +* Success: folio will be freed +* -EAGAIN: stay on the unmap_folios list +* Other errno: put on ret_folios list +*/ +switch (rc) { +case -EAGAIN : +if (is_large) { + large_retry++; + thp_retry += is_thp; + }else if (!no_split_folio_counting) { + retry++; + } + nr_retry_pages += nr_pages; +break ; +case MIGRATEPAGE_SUCCESS : + stats->nr_succeeded += nr_pages; + stats->nr_thp_succeeded += is_thp; +break ; +default : +if (is_large) { + nr_large_failed++; + stats->nr_thp_failed += is_thp; + }else if (!no_split_folio_counting) { + nr_failed++; + } + + stats->nr_failed_pages += nr_pages; +break ; + } + dst = dst2; + dst2 =list_next_entry (dst, lru); + } + } + nr_failed += retry; + nr_large_failed += large_retry; + stats->nr_thp_failed += thp_retry; + stats->nr_failed_pages += nr_retry_pages; + +if (rc_saved) + rc = rc_saved; +else + rc = nr_failed + nr_large_failed; +out: +/* Cleanup remaining folios */ + dst = list_first_entry(&dst_folios, struct folio, lru); + dst2 =list_next_entry (dst, lru); +list_for_each_entry_safe (folio, folio2, &unmap_folios, lru) { +int page_was_mapped =0 ; +struct anon_vma *anon_vma =NULL ; + +__migrate_folio_extract (dst, &page_was_mapped, &anon_vma); +migrate_folio_undo_src (folio, page_was_mapped, anon_vma, + ret_folios); +list_del (&dst->lru); +migrate_folio_undo_dst (dst, put_new_page, private); + dst = dst2; + dst2 =list_next_entry (dst, lru); + } +/* * Try to migrate split folios of fail-to-migrate large folios, no * nr_failed counting in this round, since all split folios of a * large folio is counted as 1 failure in the first round. */ -if (!list_empty (&split_folios)) { +if (rc >=0 && !list_empty (&split_folios)) {/* * Move non-migrated folios (after NR_MAX_MIGRATE_PAGES_RETRY * retries) to ret_folios to avoid migrating them again.
@@ -1761,12 +1916,16 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,list_splice_init (from, ret_folios);list_splice_init (&split_folios, from); no_split_folio_counting = true; - retry =1 ; - goto split_folio_migration; + goto retry; } - rc = nr_failed + nr_large_failed; -out: +/* +* We have unlocked all locked folios, so we can force lock now, let's +* try again. +*/ +if (rc == -EDEADLOCK ) + goto retry; +return rc; }
Signature · 1 line
2.35.1
PATCH 6/9 migrate_pages: move migrate_folio_unmap()
In reply to Huang YingJust move the position of the functions. There's no any functionality change. This is to make it easier to review the next patch via putting code near its position in the next patch.
--- mm/migrate.c | 102 +++++++++++++++++++++++++-------------------------- 1 file changed, 51 insertions(+), 51 deletions(-)
mm/migrate.c +51 -51
@@ -1218,6 +1218,57 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,return rc; } +/* Obtain the lock on page, remove all ptes. */ +static int migrate_folio_unmap (new_page_t get_new_page,free_page_t put_new_page, +unsigned long private,struct folio *src, +struct folio **dstp,int force,bool force_lock, +enum migrate_mode mode,enum migrate_reason reason, +struct list_head *ret) +{ +struct folio *dst; +int rc =MIGRATEPAGE_UNMAP ; +struct page *newpage =NULL ; + +if (!thp_migration_supported () &&folio_test_transhuge (src)) +return -ENOSYS ; + +if (folio_ref_count (src) ==1 ) { +/* Folio was freed from under us. So we are done. */ +folio_clear_active (src); +folio_clear_unevictable (src); +/* free_pages_prepare() will clear PG_isolated. */ +list_del (&src->lru); +migrate_folio_done (src, reason); +return MIGRATEPAGE_SUCCESS ; + } + + newpage =get_new_page (&src->page, private); +if (!newpage) +return -ENOMEM ; + dst =page_folio (newpage); + *dstp = dst; + + dst->private =NULL ; + rc =__migrate_folio_unmap (src, dst, force, force_lock, mode); +if (rc ==MIGRATEPAGE_UNMAP ) +return rc; + +/* +* A page that has not been migrated will have kept its +* references and be restored. +*/ +/* restore the folio to right list. */ +if (rc != -EAGAIN && rc != -EDEADLOCK ) +list_move_tail (&src->lru, ret); + +if (put_new_page) +put_new_page (&dst->page, private); +else +folio_put (dst); + +return rc; +} +static int __migrate_folio_move (struct folio *src,struct folio *dst,enum migrate_mode mode) {
@@ -1277,57 +1328,6 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,return rc; } -/* Obtain the lock on page, remove all ptes. */ -static int migrate_folio_unmap (new_page_t get_new_page,free_page_t put_new_page, -unsigned long private,struct folio *src, -struct folio **dstp,int force,bool force_lock, -enum migrate_mode mode,enum migrate_reason reason, -struct list_head *ret) -{ -struct folio *dst; -int rc =MIGRATEPAGE_UNMAP ; -struct page *newpage =NULL ; - -if (!thp_migration_supported () &&folio_test_transhuge (src)) -return -ENOSYS ; - -if (folio_ref_count (src) ==1 ) { -/* Folio was freed from under us. So we are done. */ -folio_clear_active (src); -folio_clear_unevictable (src); -/* free_pages_prepare() will clear PG_isolated. */ -list_del (&src->lru); -migrate_folio_done (src, reason); -return MIGRATEPAGE_SUCCESS ; - } - - newpage =get_new_page (&src->page, private); -if (!newpage) -return -ENOMEM ; - dst =page_folio (newpage); - *dstp = dst; - - dst->private =NULL ; - rc =__migrate_folio_unmap (src, dst, force, force_lock, mode); -if (rc ==MIGRATEPAGE_UNMAP ) -return rc; - -/* -* A page that has not been migrated will have kept its -* references and be restored. -*/ -/* restore the folio to right list. */ -if (rc != -EAGAIN && rc != -EDEADLOCK ) -list_move_tail (&src->lru, ret); - -if (put_new_page) -put_new_page (&dst->page, private); -else -folio_put (dst); - -return rc; -} -/* Migrate the folio to the newly allocated folio in dst. */ static int migrate_folio_move(free_page_t put_new_page, unsigned long private, struct folio *src, struct folio *dst,
Signature · 1 line
2.35.1
PATCH 7/9 migrate_pages: share more code between _unmap and _move
In reply to Huang YingThis is a code cleanup patch to reduce the duplicated code between the _unmap and _move stages of migrate_pages(). No functionality change is expected.
--- mm/migrate.c | 205 +++++++++++++++++++++------------------------------ 1 file changed, 82 insertions(+), 123 deletions(-)
mm/migrate.c +82 -123
@@ -1037,6 +1037,7 @@ static void __migrate_folio_extract(struct folio *dst,static void migrate_folio_undo_src (struct folio *src,int page_was_mapped,struct anon_vma *anon_vma, +bool locked,struct list_head *ret) {if (page_was_mapped)
@@ -1044,16 +1045,20 @@ static void migrate_folio_undo_src(struct folio *src,/* Drop an anon_vma reference if we took one */ if (anon_vma)put_anon_vma (anon_vma); -folio_unlock (src); -list_move_tail (&src->lru, ret); +if (locked) +folio_unlock (src); +if (ret) +list_move_tail (&src->lru, ret); }/* Restore the destination folio to the original state upon failure */ static void migrate_folio_undo_dst (struct folio *dst, +bool locked,free_page_t put_new_page,unsigned long private) { -folio_unlock (dst); +if (locked) +folio_unlock (dst);if (put_new_page)put_new_page (&dst->page, private);else
@@ -1078,13 +1083,42 @@ static void migrate_folio_done(struct folio *src,folio_put (src); } -static int __migrate_folio_unmap (struct folio *src,struct folio *dst, -int force,bool force_lock,enum migrate_mode mode) +/* Obtain the lock on page, remove all ptes. */ +static int migrate_folio_unmap (new_page_t get_new_page,free_page_t put_new_page, +unsigned long private,struct folio *src, +struct folio **dstp,int force,bool force_lock, +enum migrate_mode mode,enum migrate_reason reason, +struct list_head *ret) { +struct folio *dst;int rc = -EAGAIN ; +struct page *newpage =NULL ;int page_was_mapped =0 ;struct anon_vma *anon_vma =NULL ;bool is_lru = !__PageMovable (&src->page); +bool locked = false; +bool dst_locked = false; + +if (!thp_migration_supported () &&folio_test_transhuge (src)) +return -ENOSYS ; + +if (folio_ref_count (src) ==1 ) { +/* Folio was freed from under us. So we are done. */ +folio_clear_active (src); +folio_clear_unevictable (src); +/* free_pages_prepare() will clear PG_isolated. */ +list_del (&src->lru); +migrate_folio_done (src, reason); +return MIGRATEPAGE_SUCCESS ; + } + + newpage =get_new_page (&src->page, private); +if (!newpage) +return -ENOMEM ; + dst =page_folio (newpage); + *dstp = dst; + + dst->private =NULL ;if (!folio_trylock (src)) {if (!force || mode ==MIGRATE_ASYNC )
@@ -1119,6 +1153,7 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,folio_lock (src); } + locked = true;if (folio_test_writeback (src)) {/*
@@ -1133,10 +1168,10 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,break ;default : rc = -EBUSY ; - goto out_unlock; + goto out; }if (!force) - goto out_unlock; + goto out;folio_wait_writeback (src); }
@@ -1166,7 +1201,8 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,* This is much like races on refcount of oldpage: just don't BUG(). */ if (unlikely (!folio_trylock (dst))) - goto out_unlock; + goto out; + dst_locked = true;if (unlikely (!is_lru)) {__migrate_folio_record (dst, page_was_mapped, anon_vma);
@@ -1188,7 +1224,7 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,if (!src->mapping) {if (folio_test_private (src)) {try_to_free_buffers (src); - goto out_unlock_both; + goto out; } }else if (folio_mapped (src)) {/* Establish migration ptes */
@@ -1203,74 +1239,26 @@ static int __migrate_folio_unmap(struct folio *src, struct folio *dst,return MIGRATEPAGE_UNMAP ; } -if (page_was_mapped) -remove_migration_ptes (src, src, false); - -out_unlock_both: -folio_unlock (dst); -out_unlock: -/* Drop an anon_vma reference if we took one */ -if (anon_vma) -put_anon_vma (anon_vma); -folio_unlock (src); out: - -return rc; -} - -/* Obtain the lock on page, remove all ptes. */ -static int migrate_folio_unmap (new_page_t get_new_page,free_page_t put_new_page, -unsigned long private,struct folio *src, -struct folio **dstp,int force,bool force_lock, -enum migrate_mode mode,enum migrate_reason reason, -struct list_head *ret) -{ -struct folio *dst; -int rc =MIGRATEPAGE_UNMAP ; -struct page *newpage =NULL ; - -if (!thp_migration_supported () &&folio_test_transhuge (src)) -return -ENOSYS ; - -if (folio_ref_count (src) ==1 ) { -/* Folio was freed from under us. So we are done. */ -folio_clear_active (src); -folio_clear_unevictable (src); -/* free_pages_prepare() will clear PG_isolated. */ -list_del (&src->lru); -migrate_folio_done (src, reason); -return MIGRATEPAGE_SUCCESS ; - } - - newpage =get_new_page (&src->page, private); -if (!newpage) -return -ENOMEM ; - dst =page_folio (newpage); - *dstp = dst; - - dst->private =NULL ; - rc =__migrate_folio_unmap (src, dst, force, force_lock, mode); -if (rc ==MIGRATEPAGE_UNMAP ) -return rc; -/* * A page that has not been migrated will have kept its * references and be restored. */ /* restore the folio to right list. */ -if (rc != -EAGAIN && rc != -EDEADLOCK ) -list_move_tail (&src->lru, ret); +if (rc == -EAGAIN || rc == -EDEADLOCK ) + ret =NULL ; -if (put_new_page) -put_new_page (&dst->page, private); -else -folio_put (dst); +migrate_folio_undo_src (src, page_was_mapped, anon_vma, locked, ret); +migrate_folio_undo_dst (dst, dst_locked, put_new_page, private);return rc; } -static int __migrate_folio_move (struct folio *src,struct folio *dst, -enum migrate_mode mode) +/* Migrate the folio to the newly allocated folio in dst. */ +static int migrate_folio_move (free_page_t put_new_page,unsigned long private, +struct folio *src,struct folio *dst, +enum migrate_mode mode,enum migrate_reason reason, +struct list_head *ret) {int rc;int page_was_mapped =0 ;
@@ -1280,9 +1268,10 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,__migrate_folio_extract (dst, &page_was_mapped, &anon_vma); rc =move_to_new_folio (dst, src, mode); +if (rc) + goto out; -if (rc != -EAGAIN ) -list_del (&dst->lru); +list_del (&dst->lru);if (unlikely (!is_lru)) goto out_unlock_both;
@@ -1296,75 +1285,45 @@ static int __migrate_folio_move(struct folio *src, struct folio *dst,* unsuccessful, and other cases when a page has been temporarily * isolated from the unevictable LRU: but this case is the easiest. */ -if (rc ==MIGRATEPAGE_SUCCESS ) { -folio_add_lru (dst); -if (page_was_mapped) -lru_add_drain (); - } - -if (rc == -EAGAIN ) { -__migrate_folio_record (dst, page_was_mapped, anon_vma); -return rc; - } +folio_add_lru (dst); +if (page_was_mapped) +lru_add_drain ();if (page_was_mapped) -remove_migration_ptes (src, - rc ==MIGRATEPAGE_SUCCESS ? dst : src, false); +remove_migration_ptes (src, dst, false); out_unlock_both:folio_unlock (dst); -/* Drop an anon_vma reference if we took one */ -if (anon_vma) -put_anon_vma (anon_vma); -folio_unlock (src); +set_page_owner_migrate_reason (&dst->page, reason);/* * If migration is successful, decrease refcount of dst, * which will not free the page because new page owner increased * refcounter. */ -if (rc ==MIGRATEPAGE_SUCCESS ) -folio_put (dst); - -return rc; -} - -/* Migrate the folio to the newly allocated folio in dst. */ -static int migrate_folio_move (free_page_t put_new_page,unsigned long private, -struct folio *src,struct folio *dst, -enum migrate_mode mode,enum migrate_reason reason, -struct list_head *ret) -{ -int rc; - - rc =__migrate_folio_move (src, dst, mode); -if (rc ==MIGRATEPAGE_SUCCESS ) -set_page_owner_migrate_reason (&dst->page, reason); - -if (rc != -EAGAIN ) { -/* -* A folio that has been migrated has all references -* removed and will be freed. A folio that has not been -* migrated will have kept its references and be restored. -*/ -list_del (&src->lru); - } +folio_put (dst);/* -* If migration is successful, releases reference grabbed during -* isolation. Otherwise, restore the folio to right list unless -* we want to retry. +* A page that has been migrated has all references removed +* and will be freed. */ -if (rc ==MIGRATEPAGE_SUCCESS ) { -migrate_folio_done (src, reason); - }else if (rc != -EAGAIN ) { -list_add_tail (&src->lru, ret); +list_del (&src->lru); +/* Drop an anon_vma reference if we took one */ +if (anon_vma) +put_anon_vma (anon_vma); +folio_unlock (src); +migrate_folio_done (src, reason); -if (put_new_page) -put_new_page (&dst->page, private); -else -folio_put (dst); +return rc; +out: +if (rc == -EAGAIN ) { +__migrate_folio_record (dst, page_was_mapped, anon_vma); +return rc; } +migrate_folio_undo_src (src, page_was_mapped, anon_vma, true, ret); +list_del (&dst->lru); +migrate_folio_undo_dst (dst, true, put_new_page, private); +return rc; }
@@ -1896,9 +1855,9 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,__migrate_folio_extract (dst, &page_was_mapped, &anon_vma);migrate_folio_undo_src (folio, page_was_mapped, anon_vma, - ret_folios); + true, ret_folios);list_del (&dst->lru); -migrate_folio_undo_dst (dst, put_new_page, private); +migrate_folio_undo_dst (dst, true, put_new_page, private); dst = dst2; dst2 =list_next_entry (dst, lru); }
Signature · 1 line
2.35.1
PATCH 8/9 migrate_pages: batch flushing TLB
In reply to Huang YingThe TLB flushing will cost quite some CPU cycles during the folio migration in some situations. For example, when migrate a folio of a process with multiple active threads that run on multiple CPUs. After batching the _unmap and _move in migrate_pages(), the TLB flushing can be batched easily with the existing TLB flush batching mechanism. This patch implements that.
We use the following test case to test the patch.
On a 2-socket Intel server,
- Run pmbench memory accessing benchmark
- Run migratepages to migrate pages of pmbench between node 0 and node 1 back and forth.
With the patch, the TLB flushing IPI reduces 99.1% during the test and the number of pages migrated successfully per second increases 291.7%.
NOTE: TLB flushing is batched only for normal folios, not for THP folios. Because the overhead of TLB flushing for THP folios is much lower than that for normal folios (about 1/512 on x86 platform).
--- mm/migrate.c | 4 +++- mm/rmap.c | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-)
mm/migrate.c +3 -1
@@ -1230,7 +1230,7 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_page/* Establish migration ptes */ VM_BUG_ON_FOLIO (folio_test_anon (src) && !folio_test_ksm (src) && !anon_vma, src); -try_to_migrate (src,0 ); +try_to_migrate (src,TTU_BATCH_FLUSH ); page_was_mapped =1 ; }
@@ -1780,6 +1780,8 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page, stats->nr_thp_failed += thp_retry; stats->nr_failed_pages += nr_retry_pages; move: +try_to_unmap_flush (); + retry =1 ; for (pass = 0; pass < NR_MAX_MIGRATE_PAGES_RETRY && (retry || large_retry);
mm/rmap.c +17 -3
@@ -1976,7 +1976,21 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, }else {flush_cache_page (vma, address,pte_pfn (*pvmw.pte));/* Nuke the page table entry. */ - pteval =ptep_clear_flush (vma, address, pvmw.pte); +if (should_defer_flush (mm, flags)) { +/* +* We clear the PTE but do not flush so potentially +* a remote CPU could still be writing to the folio. +* If the entry was previously clean then the +* architecture must guarantee that a clear->dirty +* transition on a cached TLB entry is written through +* and traps if the PTE is unmapped. +*/ + pteval =ptep_get_and_clear (mm, address, pvmw.pte); + +set_tlb_ubc_flush_pending (mm,pte_dirty (pteval)); + }else { + pteval =ptep_clear_flush (vma, address, pvmw.pte); + } }/* Set the dirty flag on the folio now the pte is gone. */
@@ -2148,10 +2162,10 @@ void try_to_migrate(struct folio *folio, enum ttu_flags flags)/* * Migration always ignores mlock and only supports TTU_RMAP_LOCKED and -* TTU_SPLIT_HUGE_PMD and TTU_SYNC flags. +* TTU_SPLIT_HUGE_PMD, TTU_SYNC, and TTU_BATCH_FLUSH flags. */ if (WARN_ON_ONCE (flags & ~(TTU_RMAP_LOCKED |TTU_SPLIT_HUGE_PMD | -TTU_SYNC ))) +TTU_SYNC |TTU_BATCH_FLUSH )))return ; if (folio_is_zone_device(folio) &&
Signature · 1 line
2.35.1
PATCH 9/9 migrate_pages: move THP/hugetlb migration support check to simplify code
In reply to Huang YingThis is a code cleanup patch, no functionality change is expected. After the change, the line number reduces especially in the long migrate_pages_batch().
--- mm/migrate.c | 83 +++++++++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 47 deletions(-)
mm/migrate.c +36 -47
@@ -1099,9 +1099,6 @@ static int migrate_folio_unmap(new_page_t get_new_page, free_page_t put_new_pagebool locked = false;bool dst_locked = false; -if (!thp_migration_supported () &&folio_test_transhuge (src)) -return -ENOSYS ; -if (folio_ref_count (src) ==1 ) {/* Folio was freed from under us. So we are done. */ folio_clear_active (src);
@@ -1358,16 +1355,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,struct anon_vma *anon_vma =NULL ;struct address_space *mapping =NULL ; -/* -* Migratability of hugepages depends on architectures and their size. -* This check is necessary because some callers of hugepage migration -* like soft offline and memory hotremove don't walk through page -* tables or check whether the hugepage is pmd-based or not before -* kicking migration. -*/ -if (!hugepage_migration_supported (page_hstate (hpage))) -return -ENOSYS ; -if (folio_ref_count (src) ==1 ) {/* page was freed from under us. So we are done. */ putback_active_hugepage (hpage);
@@ -1534,6 +1521,20 @@ static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page,cond_resched (); +/* +* Migratability of hugepages depends on architectures and +* their size. This check is necessary because some callers +* of hugepage migration like soft offline and memory +* hotremove don't walk through page tables or check whether +* the hugepage is pmd-based or not before kicking migration. +*/ +if (!hugepage_migration_supported (folio_hstate (folio))) { + nr_failed++; + stats->nr_failed_pages += nr_pages; +list_move_tail (&folio->lru, ret_folios); +continue ; + } + rc = unmap_and_move_huge_page(get_new_page, put_new_page, private, &folio->page, pass > 2, mode,
@@ -1543,16 +1544,9 @@ static int migrate_hugetlbs(struct list_head *from, new_page_t get_new_page,* Success: hugetlb folio will be put back * -EAGAIN: stay on the from list * -ENOMEM: stay on the from list -* -ENOSYS: stay on the from list * Other errno: put on ret_folios list */ switch (rc) { -case -ENOSYS : -/* Hugetlb migration is unsupported */ - nr_failed++; - stats->nr_failed_pages += nr_pages; -list_move_tail (&folio->lru, ret_folios); -break ;case -ENOMEM :/* * When memory is low, don't bother to try to migrate
@@ -1638,6 +1632,28 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,cond_resched (); +/* +* Large folio migration might be unsupported or +* the allocation might be failed so we should retry +* on the same folio with the large folio split +* to normal folios. +* +* Split folios are put in split_folios, and +* we will migrate them after the rest of the +* list is processed. +*/ +if (!thp_migration_supported () && is_thp) { + nr_large_failed++; + stats->nr_thp_failed++; +if (!try_split_folio (folio, &split_folios)) { + stats->nr_thp_split++; +continue ; + } + stats->nr_failed_pages += nr_pages; +list_move_tail (&folio->lru, ret_folios); +continue ; + } + rc =migrate_folio_unmap (get_new_page, put_new_page, private, folio, &dst, pass >2 , force_lock, mode, reason, ret_folios);
@@ -1649,36 +1665,9 @@ static int migrate_pages_batch(struct list_head *from, new_page_t get_new_page,* -EAGAIN: stay on the from list * -EDEADLOCK: stay on the from list * -ENOMEM: stay on the from list -* -ENOSYS: stay on the from list * Other errno: put on ret_folios list */ switch (rc) { -/* -* Large folio migration might be unsupported or -* the allocation could've failed so we should retry -* on the same folio with the large folio split -* to normal folios. -* -* Split folios are put in split_folios, and -* we will migrate them after the rest of the -* list is processed. -*/ -case -ENOSYS : -/* Large folio migration is unsupported */ -if (is_large) { - nr_large_failed++; - stats->nr_thp_failed += is_thp; -if (!try_split_folio (folio, &split_folios)) { - stats->nr_thp_split += is_thp; -break ; - } - }else if (!no_split_folio_counting) { - nr_failed++; - } - - stats->nr_failed_pages += nr_pages; -list_move_tail (&folio->lru, ret_folios); -break ;case -ENOMEM :/* * When memory is low, don't bother to try to migrate
Signature · 1 line
2.35.1