Some cleanups for huge_memory
Subsystems (2)
v3 cover Some cleanups for huge_memory
Hi all,
This series contains cleanups to rework some function logics to make it more readable, use helper function and so on. More details can be found in the respective changelogs. Thanks!
v2->v3:
use ALIGN/ALIGN_DOWN too against HPAGE_PMD_SIZE per Peter.
v1->v2:
rename try_to_split_huge_pmd_address and move up comments.
Miaohe Lin (6):
mm/huge_memory.c: rework the function vma_adjust_trans_huge() mm/huge_memory.c: make get_huge_zero_page() return bool mm/huge_memory.c: rework the function do_huge_pmd_numa_page() slightly mm/huge_memory.c: remove redundant PageCompound() check mm/huge_memory.c: remove unused macro
TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
mm/huge_memory.c: use helper function migration_entry_to_page()
include/linux/huge_mm.h | 3 -- mm/huge_memory.c | 69 ++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 41 deletions(-)
Signature · 1 line
2.19.1
PATCH v3 1/6 mm/huge_memory.c: rework the function vma_adjust_trans_huge()
The current implementation of vma_adjust_trans_huge() contains some duplicated codes. Add helper function to get rid of these codes to make it more succinct.
--- mm/huge_memory.c | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-)
mm/huge_memory.c +19 -25
@@ -2301,44 +2301,38 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address,__split_huge_pmd (vma, pmd, address, freeze, page); } +static inline void split_huge_pmd_if_needed (struct vm_area_struct *vma,unsigned long address) +{ +/* +* If the new address isn't hpage aligned and it could previously +* contain an hugepage: check if we need to split an huge pmd. +*/ +if (!IS_ALIGNED (address,HPAGE_PMD_SIZE ) && +range_in_vma (vma,ALIGN_DOWN (address,HPAGE_PMD_SIZE ), +ALIGN (address,HPAGE_PMD_SIZE ))) +split_huge_pmd_address (vma, address, false,NULL ); +} +void vma_adjust_trans_huge (struct vm_area_struct *vma,unsigned long start,unsigned long end,long adjust_next) { -/* -* If the new start address isn't hpage aligned and it could -* previously contain an hugepage: check if we need to split -* an huge pmd. -*/ -if (start & ~HPAGE_PMD_MASK && - (start &HPAGE_PMD_MASK ) >= vma->vm_start && - (start &HPAGE_PMD_MASK ) +HPAGE_PMD_SIZE <= vma->vm_end) -split_huge_pmd_address (vma, start, false,NULL ); +/* Check if we need to split start first. */ +split_huge_pmd_if_needed (vma, start); -/* -* If the new end address isn't hpage aligned and it could -* previously contain an hugepage: check if we need to split -* an huge pmd. -*/ -if (end & ~HPAGE_PMD_MASK && - (end &HPAGE_PMD_MASK ) >= vma->vm_start && - (end &HPAGE_PMD_MASK ) +HPAGE_PMD_SIZE <= vma->vm_end) -split_huge_pmd_address (vma, end, false,NULL ); +/* Check if we need to split end next. */ +split_huge_pmd_if_needed (vma, end);/* -* If we're also updating the vma->vm_next->vm_start, if the new -* vm_next->vm_start isn't hpage aligned and it could previously -* contain an hugepage: check if we need to split an huge pmd. +* If we're also updating the vma->vm_next->vm_start, +* check if we need to split it. */ if (adjust_next >0 ) {struct vm_area_struct *next = vma->vm_next;unsigned long nstart = next->vm_start; nstart += adjust_next; -if (nstart & ~HPAGE_PMD_MASK && - (nstart &HPAGE_PMD_MASK ) >= next->vm_start && - (nstart &HPAGE_PMD_MASK ) +HPAGE_PMD_SIZE <= next->vm_end) -split_huge_pmd_address (next, nstart, false,NULL ); +split_huge_pmd_if_needed (next, nstart); } }
Signature · 1 line
2.19.1
PATCH v3 2/6 mm/huge_memory.c: make get_huge_zero_page() return bool
In reply to Miaohe LinIt's guaranteed that huge_zero_page will not be NULL if huge_zero_refcount is increased successfully. When READ_ONCE(huge_zero_page) is returned, there must be a huge_zero_page and it can be replaced with returning 'true' when we do not care about the value of huge_zero_page. We can thus make it return bool to save READ_ONCE cpu cycles as the return value is just used to check if huge_zero_page exists.
--- mm/huge_memory.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/huge_memory.c +4 -4
@@ -77,18 +77,18 @@ bool transparent_hugepage_enabled(struct vm_area_struct *vma)return false; } -static struct page *get_huge_zero_page (void ) +static bool get_huge_zero_page (void ) {struct page *zero_page; retry:if (likely (atomic_inc_not_zero (&huge_zero_refcount))) -return READ_ONCE (huge_zero_page); +return true; zero_page =alloc_pages ((GFP_TRANSHUGE | __GFP_ZERO) & ~__GFP_MOVABLE,HPAGE_PMD_ORDER );if (!zero_page) {count_vm_event (THP_ZERO_PAGE_ALLOC_FAILED ); -return NULL ; +return false; }count_vm_event (THP_ZERO_PAGE_ALLOC );preempt_disable ();
@@ -101,7 +101,7 @@ static struct page *get_huge_zero_page(void)/* We take additional reference here. It will be put back by shrinker */ atomic_set (&huge_zero_refcount,2 );preempt_enable (); -return READ_ONCE (huge_zero_page); +return true; }static void put_huge_zero_page (void )
Signature · 1 line
2.19.1
Zi Yan
reviewerRe: [PATCH v3 2/6] mm/huge_memory.c: make get_huge_zero_page() return bool
Quoted 7 lines · Miaohe Lin
It's guaranteed that
huge_zero_pagewill not be NULL ifhuge_zero_refcountis increased successfully. WhenREAD_ONCE(huge_zero_page)is returned, there must be ahuge_zero_pageand it can be replaced with returning 'true' when we do not care about the value ofhuge_zero_page. We can thus make it return bool to saveREAD_ONCEcpu cycles as the return value is just used to check ifhuge_zero_pageexists.
Quoted 5 lines
Signed-off-by Miaohe Lin <[email protected]>--- mm/huge_memory.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
LGTM. Reviewed-by: Zi Yan <[email protected]>
—
Best Regards,
Yan Zi
PATCH v3 3/6 mm/huge_memory.c: rework the function do_huge_pmd_numa_page() slightly
In reply to Miaohe LinThe current code that checks if migrating misplaced transhuge page is needed is pretty hard to follow. Rework it and add a comment to make its logic more clear and improve readability.
--- mm/huge_memory.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
mm/huge_memory.c +5 -6
@@ -1462,12 +1462,6 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd)*/ page_locked =trylock_page (page); target_nid =mpol_misplaced (page, vma, haddr); -if (target_nid ==NUMA_NO_NODE ) { -/* If the page was locked, there are no parallel migrations */ -if (page_locked) - goto clear_pmdnuma; - } -/* Migration could have started since the pmd_trans_migrating check */ if (!page_locked) { page_nid =NUMA_NO_NODE ;
@@ -1476,6 +1470,11 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf, pmd_t pmd)spin_unlock (vmf->ptl);put_and_wait_on_page_locked (page,TASK_UNINTERRUPTIBLE ); goto out; + }else if (target_nid ==NUMA_NO_NODE ) { +/* There are no parallel migrations and page is in the right +* node. Clear the numa hinting info in this pmd. +*/ + goto clear_pmdnuma; }/*
Signature · 1 line
2.19.1
Zi Yan
reviewerRe: [PATCH v3 3/6] mm/huge_memory.c: rework the function do_huge_pmd_numa_page() slightly
Quoted 9 lines · Miaohe Lin
The current code that checks if migrating misplaced transhuge page is needed is pretty hard to follow. Rework it and add a comment to make its logic more clear and improve readability.
Signed-off-by Miaohe Lin <[email protected]>--- mm/huge_memory.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
LGTM. Reviewed-by: Zi Yan <[email protected]>
—
Best Regards,
Yan Zi
PATCH v3 4/6 mm/huge_memory.c: remove redundant PageCompound() check
In reply to Miaohe LinThe !PageCompound() check limits the page must be head or tail while !PageHead() further limits it to page head only. So !PageHead() check is equivalent here.
--- mm/huge_memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/huge_memory.c +1 -1
@@ -1291,7 +1291,7 @@ vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf, pmd_t orig_pmd) } page =pmd_page (orig_pmd); -VM_BUG_ON_PAGE (!PageCompound (page) || !PageHead (page), page); +VM_BUG_ON_PAGE (!PageHead (page), page);/* Lock page for reuse_swap_page() */ if (!trylock_page (page)) {
Signature · 1 line
2.19.1
PATCH v3 5/6 mm/huge_memory.c: remove unused macro TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
In reply to Miaohe LinThe commit 4958e4d86ecb ("mm: thp: remove debug_cow switch") forgot to remove TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG macro. Remove it here.
--- include/linux/huge_mm.h | 3 --- 1 file changed, 3 deletions(-)
include/linux/huge_mm.h +0 -3
@@ -87,9 +87,6 @@ enum transparent_hugepage_flag {TRANSPARENT_HUGEPAGE_DEFRAG_REQ_MADV_FLAG ,TRANSPARENT_HUGEPAGE_DEFRAG_KHUGEPAGED_FLAG , TRANSPARENT_HUGEPAGE_USE_ZERO_PAGE_FLAG, -#ifdef CONFIG_DEBUG_VM - TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG, -#endif };struct kobject ;
Signature · 1 line
2.19.1
Zi Yan
reviewerRe: [PATCH v3 5/6] mm/huge_memory.c: remove unused macro TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
Quoted 7 lines · Miaohe Lin
The commit
4958e4d86ecb("mm: thp: removedebug_cowswitch") forgot to removeTRANSPARENT_HUGEPAGE_DEBUG_COW_FLAGmacro. Remove it here.Signed-off-by Miaohe Lin <[email protected]>--- include/linux/huge_mm.h | 3 --- 1 file changed, 3 deletions(-)
LGTM. Zi Yan <[email protected]>
—
Best Regards,
Yan Zi
PATCH v3 6/6 mm/huge_memory.c: use helper function migration_entry_to_page()
In reply to Miaohe LinIt's more recommended to use helper function migration_entry_to_page() to get the page via migration entry. We can also enjoy the PageLocked() check there.
--- mm/huge_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/huge_memory.c +2 -2
@@ -1693,7 +1693,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,VM_BUG_ON (!is_pmd_migration_entry (orig_pmd)); entry =pmd_to_swp_entry (orig_pmd); - page =pfn_to_page (swp_offset (entry)); + page =migration_entry_to_page (entry); flush_needed =0 ; }else WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" );
@@ -2101,7 +2101,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,swp_entry_t entry; entry =pmd_to_swp_entry (old_pmd); - page =pfn_to_page (swp_offset (entry)); + page =migration_entry_to_page (entry); write =is_write_migration_entry (entry); young = false; soft_dirty =pmd_swp_soft_dirty (old_pmd);
Signature · 1 line
2.19.1
Quoted 11 lines · Miaohe Lin
Hi all,
This series contains cleanups to rework some function logics to make it more readable, use helper function and so on. More details can be found in the respective changelogs. Thanks!
v2->v3:
use ALIGN/ALIGN_DOWN too againstHPAGE_PMD_SIZEper Peter.
v1->v2:
renametry_to_split_huge_pmd_addressand move up comments.
Quoted 8 lines
Miaohe Lin (6):
mm/huge_memory.c: rework the functionvma_adjust_trans_huge()mm/huge_memory.c: makeget_huge_zero_page()return boolmm/huge_memory.c: rework the functiondo_huge_pmd_numa_page()slightlymm/huge_memory.c: remove redundantPageCompound()checkmm/huge_memory.c: remove unused macro
TRANSPARENT_HUGEPAGE_DEBUG_COW_FLAG
mm/huge_memory.c: use helper functionmigration_entry_to_page()
Signature · 1 line
Peter Xu