Skip to content

Commit 7da4e2c

Browse files
yang-shiakpm00
authored andcommitted
mm: thp: kill __transhuge_page_enabled()
The page fault path checks THP eligibility with __transhuge_page_enabled() which does the similar thing as hugepage_vma_check(), so use hugepage_vma_check() instead. However page fault allows DAX and !anon_vma cases, so added a new flag, in_pf, to hugepage_vma_check() to make page fault work correctly. The in_pf flag is also used to skip shmem and file THP for page fault since shmem handles THP in its own shmem_fault() and file THP allocation on fault is not supported yet. Also remove hugepage_vma_enabled() since hugepage_vma_check() is the only caller now, it is not necessary to have a helper function. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Yang Shi <[email protected]> Reviewed-by: Zach O'Keefe <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Miaohe Lin <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 9fec516 commit 7da4e2c

File tree

5 files changed

+52
-73
lines changed

5 files changed

+52
-73
lines changed

fs/proc/task_mmu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ static int show_smap(struct seq_file *m, void *v)
863863
__show_smap(m, &mss, false);
864864

865865
seq_printf(m, "THPeligible: %d\n",
866-
hugepage_vma_check(vma, vma->vm_flags, true));
866+
hugepage_vma_check(vma, vma->vm_flags, true, false));
867867

868868
if (arch_pkeys_enabled())
869869
seq_printf(m, "ProtectionKey: %8u\n", vma_pkey(vma));

include/linux/huge_mm.h

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -146,48 +146,6 @@ static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
146146
return true;
147147
}
148148

149-
static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
150-
unsigned long vm_flags)
151-
{
152-
/* Explicitly disabled through madvise. */
153-
if ((vm_flags & VM_NOHUGEPAGE) ||
154-
test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
155-
return false;
156-
return true;
157-
}
158-
159-
/*
160-
* to be used on vmas which are known to support THP.
161-
* Use transparent_hugepage_active otherwise
162-
*/
163-
static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
164-
{
165-
166-
/*
167-
* If the hardware/firmware marked hugepage support disabled.
168-
*/
169-
if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX))
170-
return false;
171-
172-
if (!transhuge_vma_enabled(vma, vma->vm_flags))
173-
return false;
174-
175-
if (vma_is_temporary_stack(vma))
176-
return false;
177-
178-
if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
179-
return true;
180-
181-
if (vma_is_dax(vma))
182-
return true;
183-
184-
if (transparent_hugepage_flags &
185-
(1 << TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG))
186-
return !!(vma->vm_flags & VM_HUGEPAGE);
187-
188-
return false;
189-
}
190-
191149
static inline bool file_thp_enabled(struct vm_area_struct *vma)
192150
{
193151
struct inode *inode;
@@ -204,7 +162,7 @@ static inline bool file_thp_enabled(struct vm_area_struct *vma)
204162

205163
bool hugepage_vma_check(struct vm_area_struct *vma,
206164
unsigned long vm_flags,
207-
bool smaps);
165+
bool smaps, bool in_pf);
208166

209167
#define transparent_hugepage_use_zero_page() \
210168
(transparent_hugepage_flags & \
@@ -348,26 +306,15 @@ static inline bool folio_test_pmd_mappable(struct folio *folio)
348306
return false;
349307
}
350308

351-
static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
352-
{
353-
return false;
354-
}
355-
356309
static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
357310
unsigned long addr)
358311
{
359312
return false;
360313
}
361314

362-
static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
363-
unsigned long vm_flags)
364-
{
365-
return false;
366-
}
367-
368315
static inline bool hugepage_vma_check(struct vm_area_struct *vma,
369316
unsigned long vm_flags,
370-
bool smaps)
317+
bool smaps, bool in_pf)
371318
{
372319
return false;
373320
}

mm/huge_memory.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,53 @@ unsigned long huge_zero_pfn __read_mostly = ~0UL;
7171

7272
bool hugepage_vma_check(struct vm_area_struct *vma,
7373
unsigned long vm_flags,
74-
bool smaps)
74+
bool smaps, bool in_pf)
7575
{
7676
if (!vma->vm_mm) /* vdso */
7777
return false;
7878

79-
if (!transhuge_vma_enabled(vma, vm_flags))
79+
/*
80+
* Explicitly disabled through madvise or prctl, or some
81+
* architectures may disable THP for some mappings, for
82+
* example, s390 kvm.
83+
* */
84+
if ((vm_flags & VM_NOHUGEPAGE) ||
85+
test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
8086
return false;
81-
82-
if (vm_flags & VM_NO_KHUGEPAGED)
87+
/*
88+
* If the hardware/firmware marked hugepage support disabled.
89+
*/
90+
if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX))
8391
return false;
8492

85-
/* Don't run khugepaged against DAX vma */
93+
/* khugepaged doesn't collapse DAX vma, but page fault is fine. */
8694
if (vma_is_dax(vma))
95+
return in_pf;
96+
97+
/*
98+
* Special VMA and hugetlb VMA.
99+
* Must be checked after dax since some dax mappings may have
100+
* VM_MIXEDMAP set.
101+
*/
102+
if (vm_flags & VM_NO_KHUGEPAGED)
87103
return false;
88104

89-
/* Check alignment for file vma and size for both file and anon vma */
90-
if (!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE)))
105+
/*
106+
* Check alignment for file vma and size for both file and anon vma.
107+
*
108+
* Skip the check for page fault. Huge fault does the check in fault
109+
* handlers. And this check is not suitable for huge PUD fault.
110+
*/
111+
if (!in_pf &&
112+
!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE)))
91113
return false;
92114

93-
/* Enabled via shmem mount options or sysfs settings. */
94-
if (shmem_file(vma->vm_file))
115+
/*
116+
* Enabled via shmem mount options or sysfs settings.
117+
* Must be done before hugepage flags check since shmem has its
118+
* own flags.
119+
*/
120+
if (!in_pf && shmem_file(vma->vm_file))
95121
return shmem_huge_enabled(vma);
96122

97123
if (!khugepaged_enabled())
@@ -102,7 +128,7 @@ bool hugepage_vma_check(struct vm_area_struct *vma,
102128
return false;
103129

104130
/* Only regular file is valid */
105-
if (file_thp_enabled(vma))
131+
if (!in_pf && file_thp_enabled(vma))
106132
return true;
107133

108134
if (!vma_is_anonymous(vma))
@@ -114,9 +140,12 @@ bool hugepage_vma_check(struct vm_area_struct *vma,
114140
/*
115141
* THPeligible bit of smaps should show 1 for proper VMAs even
116142
* though anon_vma is not initialized yet.
143+
*
144+
* Allow page fault since anon_vma may be not initialized until
145+
* the first page fault.
117146
*/
118147
if (!vma->anon_vma)
119-
return smaps;
148+
return (smaps || in_pf);
120149

121150
return true;
122151
}

mm/khugepaged.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma,
466466
{
467467
if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) &&
468468
khugepaged_enabled()) {
469-
if (hugepage_vma_check(vma, vm_flags, false))
469+
if (hugepage_vma_check(vma, vm_flags, false, false))
470470
__khugepaged_enter(vma->vm_mm);
471471
}
472472
}
@@ -916,7 +916,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
916916

917917
if (!transhuge_vma_suitable(vma, address))
918918
return SCAN_ADDRESS_RANGE;
919-
if (!hugepage_vma_check(vma, vma->vm_flags, false))
919+
if (!hugepage_vma_check(vma, vma->vm_flags, false, false))
920920
return SCAN_VMA_CHECK;
921921
/*
922922
* Anon VMA expected, the address may be unmapped then
@@ -1401,7 +1401,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
14011401
* the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check()
14021402
* will not fail the vma for missing VM_HUGEPAGE
14031403
*/
1404-
if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE, false))
1404+
if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE, false, false))
14051405
return;
14061406

14071407
/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
@@ -2091,7 +2091,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
20912091
progress++;
20922092
break;
20932093
}
2094-
if (!hugepage_vma_check(vma, vma->vm_flags, false)) {
2094+
if (!hugepage_vma_check(vma, vma->vm_flags, false, false)) {
20952095
skip:
20962096
progress++;
20972097
continue;

mm/memory.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4970,6 +4970,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
49704970
.gfp_mask = __get_fault_gfp_mask(vma),
49714971
};
49724972
struct mm_struct *mm = vma->vm_mm;
4973+
unsigned long vm_flags = vma->vm_flags;
49734974
pgd_t *pgd;
49744975
p4d_t *p4d;
49754976
vm_fault_t ret;
@@ -4983,7 +4984,8 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
49834984
if (!vmf.pud)
49844985
return VM_FAULT_OOM;
49854986
retry_pud:
4986-
if (pud_none(*vmf.pud) && __transparent_hugepage_enabled(vma)) {
4987+
if (pud_none(*vmf.pud) &&
4988+
hugepage_vma_check(vma, vm_flags, false, true)) {
49874989
ret = create_huge_pud(&vmf);
49884990
if (!(ret & VM_FAULT_FALLBACK))
49894991
return ret;
@@ -5016,7 +5018,8 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,
50165018
if (pud_trans_unstable(vmf.pud))
50175019
goto retry_pud;
50185020

5019-
if (pmd_none(*vmf.pmd) && __transparent_hugepage_enabled(vma)) {
5021+
if (pmd_none(*vmf.pmd) &&
5022+
hugepage_vma_check(vma, vm_flags, false, true)) {
50205023
ret = create_huge_pmd(&vmf);
50215024
if (!(ret & VM_FAULT_FALLBACK))
50225025
return ret;

0 commit comments

Comments
 (0)