mm, compaction: Skip all non-migratable pages during scan
Subsystems (2)
PATCH v2 1/1 mm, compaction: Skip all non-migratable pages during scan
Pages pinned in memory through extra refcounts can not be migrated. Currently as isolate_migratepages_block() scans pages for compaction, it skips any pinned anonymous pages. All non-migratable pages should be skipped and not just the anonymous pinned pages. This patch adds a check for extra refcounts on a page to determine if the page can be migrated. This was seen as a real issue on a customer workload where a large number of pages were pinned by vfio on the host and any attempts to allocate hugepages resulted in significant amount of cpu time spent in either direct compaction or in kcompatd scanning vfio pinned pages over and over again that can not be migrated.
---
v2:
- Update comments in the code (Suggested by Andrew)
- Use PagePrivate() instead of page_has_private() (Suggested by Matthew)
- Pass mapping to page_has_extrarefs() (Suggested by Matthew)
- Use page_ref_count() (Suggested by Matthew)
- Rename is_pinned_page() to reflect its function more accurately (Suggested by Matthew)
mm/compaction.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-)
mm/compaction.c +32 -4
@@ -764,6 +764,34 @@ static bool too_many_isolated(pg_data_t *pgdat)return too_many; } +/* +* Check if this base page should be skipped from isolation because +* it has extra refcounts that will prevent it from being migrated. +* This function is called for regular pages only, and not +* for THP or hugetlbfs pages. This code is inspired by similar code +* in migrate_vma_check_page(), can_split_folio() and +* folio_migrate_mapping() +*/ +static inline bool page_has_extrarefs (struct page *page, +struct address_space *mapping) +{ +unsigned long extra_refs; + +/* anonymous page can have extra ref from swap cache */ +if (mapping) + extra_refs =1 +PagePrivate (page); +else + extra_refs =PageSwapCache (page) ?1 :0 ; + +/* +* This is an admittedly racy check but good enough to determine +* if a page is pinned and can not be migrated +*/ +if ((page_ref_count (page) - extra_refs) >page_mapcount (page)) +return true; +return false; +} +/** * isolate_migratepages_block() - isolate all migrate-able pages within * a single pageblock
@@ -992,12 +1020,12 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, goto isolate_fail;/* -* Migration will fail if an anonymous page is pinned in memory, -* so avoid taking lru_lock and isolating it unnecessarily in an -* admittedly racy check. +* Migration will fail if a page has extra refcounts +* preventing it from migrating, so avoid taking +* lru_lock and isolating it unnecessarily */ mapping =page_mapping (page); -if (!mapping && (page_count (page) -1 ) >total_mapcount (page)) +if (page_has_extrarefs (page, mapping)) goto isolate_fail_put;/*
Signature · 1 line
2.37.2
Hi, Khalid,
Cced Mel.
Quoted 10 lines · Khalid Aziz
Pages pinned in memory through extra refcounts can not be migrated. Currently as
isolate_migratepages_block()scans pages for compaction, it skips any pinned anonymous pages. All non-migratable pages should be skipped and not just the anonymous pinned pages. This patch adds a check for extra refcounts on a page to determine if the page can be migrated. This was seen as a real issue on a customer workload where a large number of pages were pinned by vfio on the host and any attempts to allocate hugepages resulted in significant amount of cpu time spent in either direct compaction or in kcompatd scanning vfio pinned pages over and over again that can
s/kcompatd/kcompactd/
Quoted 1 line
not be migrated.
With the patch below, the cycles for kcompactd disappeared?
Quoted 23 lines
Signed-off-by Khalid Aziz <[email protected]>Suggested-by Steve Sistare <[email protected]>Cc Khalid Aziz <[email protected]>---
v2:
- Update comments in the code (Suggested by Andrew)
- UsePagePrivate()instead ofpage_has_private()(Suggested by Matthew)
- Pass mapping topage_has_extrarefs()(Suggested by Matthew)
- Usepage_ref_count()(Suggested by Matthew)
- Renameis_pinned_page()to reflect its function more accurately (Suggested by Matthew)mm/compaction.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-)mm/compaction.c +0
-0@@ -764,6 +764,34 @@ static bool too_many_isolated(pg_data_t *pgdat)return too_many; }
mm/compaction.c static bool too_many_isolated(pg_data_t *pgdat) · line 775
Quoted 10 lines
mm/compaction.c +9
-0+/* +* Check if this base page should be skipped from isolation because +* it has extra refcounts that will prevent it from being migrated. +* This function is called for regular pages only, and not +* for THP or hugetlbfs pages. This code is inspired by similar code +* in migrate_vma_check_page(), can_split_folio() and +* folio_migrate_mapping() +*/ +static inline bool page_has_extrarefs(struct page *page,
Better to be named as page_has_extra_refs()?
mm/compaction.c static bool too_many_isolated(pg_data_t *pgdat) · line 784
Quoted 9 lines
mm/compaction.c +9
-0+ struct address_space *mapping) +{ +unsigned long extra_refs; + +/* anonymous page can have extra ref from swap cache */ +if (mapping) + extra_refs =1 +PagePrivate (page); +else + extra_refs =PageSwapCache (page) ?1 :0 ;
IIUC, mapping != NULL if PageSwapCache(page) is true. Please check the implementation of page_mapping().
And even if mapping == NULL, the extra_refs should be 1, because we have elevated the page refcount in isolate_migratepages_block() before checking whether the page is pinned. IIUC, this is the original behavior. Or, we can add "- 1" in the following checking.
Quoted 20 lines
mm/compaction.c +10
-3+ +/* +* This is an admittedly racy check but good enough to determine +* if a page is pinned and can not be migrated +*/ +if ((page_ref_count (page) - extra_refs) >page_mapcount (page)) +return true; +return false; +} +/** * isolate_migratepages_block() - isolate all migrate-able pages within * a single pageblock @@ -992,12 +1020,12 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, goto isolate_fail;/* -* Migration will fail if an anonymous page is pinned in memory, -* so avoid taking lru_lock and isolating it unnecessarily in an -* admittedly racy check.
mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1031
Quoted 10 lines
mm/compaction.c +4
-1+* Migration will fail if a page has extra refcounts +* preventing it from migrating, so avoid taking +* lru_lock and isolating it unnecessarily */ mapping =page_mapping (page); -if (!mapping && (page_count (page) -1 ) >total_mapcount (page)) +if (page_has_extrarefs (page, mapping)) goto isolate_fail_put;/*
Best Regards,
Huang, Ying
Quoted 17 lines · Ying
Hi, Khalid,
Cced Mel.
Quoted 10 lines · Khalid Aziz
Pages pinned in memory through extra refcounts can not be migrated. Currently as
isolate_migratepages_block()scans pages for compaction, it skips any pinned anonymous pages. All non-migratable pages should be skipped and not just the anonymous pinned pages. This patch adds a check for extra refcounts on a page to determine if the page can be migrated. This was seen as a real issue on a customer workload where a large number of pages were pinned by vfio on the host and any attempts to allocate hugepages resulted in significant amount of cpu time spent in either direct compaction or in kcompatd scanning vfio pinned pages over and over again that can
Quoted 1 line
s/kcompatd/kcompactd/
Thanks for the review. I will add the missing "c".
Quoted 4 lines
Quoted 2 lines
not be migrated.
With the patch below, the cycles for kcompactd disappeared?
Yes, the test showed cpu time accumulated by kcompatcd during test run dropped significantly.
Quoted 25 lines
Quoted 25 lines
Signed-off-by Khalid Aziz <[email protected]>Suggested-by Steve Sistare <[email protected]>Cc Khalid Aziz <[email protected]>---
v2:
- Update comments in the code (Suggested by Andrew)
- UsePagePrivate()instead ofpage_has_private()(Suggested by Matthew)
- Pass mapping topage_has_extrarefs()(Suggested by Matthew)
- Usepage_ref_count()(Suggested by Matthew)
- Renameis_pinned_page()to reflect its function more accurately (Suggested by Matthew)mm/compaction.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-)mm/compaction.c +0
-0@@ -764,6 +764,34 @@ static bool too_many_isolated(pg_data_t *pgdat)return too_many; }
Quoted 11 lines
mm/compaction.c static bool too_many_isolated(pg_data_t *pgdat) · line 775
Quoted 9 lines
mm/compaction.c +9
-0+/* +* Check if this base page should be skipped from isolation because +* it has extra refcounts that will prevent it from being migrated. +* This function is called for regular pages only, and not +* for THP or hugetlbfs pages. This code is inspired by similar code +* in migrate_vma_check_page(), can_split_folio() and +* folio_migrate_mapping() +*/ +static inline bool page_has_extrarefs(struct page *page,Better to be named as
page_has_extra_refs()?
Sure, I can do that.
Quoted 13 lines
mm/compaction.c static bool too_many_isolated(pg_data_t *pgdat) · line 784
Quoted 10 lines
mm/compaction.c +9
-0+ struct address_space *mapping) +{ +unsigned long extra_refs; + +/* anonymous page can have extra ref from swap cache */ +if (mapping) + extra_refs =1 +PagePrivate (page); +else + extra_refs =PageSwapCache (page) ?1 :0 ;IIUC, mapping != NULL if PageSwapCache(page) is true. Please check the implementation of
page_mapping().
which should work out for refcount since it would go to if part of conditional which will add a 1 for swap cache reference, but this code could be written better. I will work on it.
Quoted 6 lines
And even if mapping == NULL, the
extra_refsshould be 1, because we have elevated the page refcount inisolate_migratepages_block()before checking whether the page is pinned. IIUC, this is the original behavior. Or, we can add "- 1" in the following checking.
You are right. There is a get_page_unless_zero() earlier in the code. I will compensate for that.
Quoted 29 lines
Quoted 29 lines
mm/compaction.c +14
-4+ +/* +* This is an admittedly racy check but good enough to determine +* if a page is pinned and can not be migrated +*/ +if ((page_ref_count (page) - extra_refs) >page_mapcount (page)) +return true; +return false; +} +/** * isolate_migratepages_block() - isolate all migrate-able pages within * a single pageblock @@ -992,12 +1020,12 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, goto isolate_fail;/* -* Migration will fail if an anonymous page is pinned in memory, -* so avoid taking lru_lock and isolating it unnecessarily in an -* admittedly racy check. +* Migration will fail if a page has extra refcounts +* preventing it from migrating, so avoid taking +* lru_lock and isolating it unnecessarily */ mapping =page_mapping (page); -if (!mapping && (page_count (page) -1 ) >total_mapcount (page)) +if (page_has_extrarefs (page, mapping)) goto isolate_fail_put;
Quoted 4 lines
mm/compaction.c isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, · line 1031
Quoted 1 line
mm/compaction.c +0
-0/* Best Regards,
Huang, Ying
Thanks,
Khalid