Skip to content

Commit 9fec516

Browse files
yang-shiakpm00
authored andcommitted
mm: thp: kill transparent_hugepage_active()
The transparent_hugepage_active() was introduced to show THP eligibility bit in smaps in proc, smaps is the only user. But it actually does the similar check as hugepage_vma_check() which is used by khugepaged. We definitely don't have to maintain two similar checks, so kill transparent_hugepage_active(). This patch also fixed the wrong behavior for VM_NO_KHUGEPAGED vmas. Also move hugepage_vma_check() to huge_memory.c and huge_mm.h since it is not only for khugepaged anymore. [[email protected]: check vma->vm_mm, per Zach] [[email protected]: add comment to vdso check] 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 f707fa4 commit 9fec516

File tree

5 files changed

+59
-62
lines changed

5 files changed

+59
-62
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-
transparent_hugepage_active(vma));
866+
hugepage_vma_check(vma, vma->vm_flags, true));
867867

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

include/linux/huge_mm.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ static inline bool file_thp_enabled(struct vm_area_struct *vma)
202202
!inode_is_open_for_write(inode) && S_ISREG(inode->i_mode);
203203
}
204204

205-
bool transparent_hugepage_active(struct vm_area_struct *vma);
205+
bool hugepage_vma_check(struct vm_area_struct *vma,
206+
unsigned long vm_flags,
207+
bool smaps);
206208

207209
#define transparent_hugepage_use_zero_page() \
208210
(transparent_hugepage_flags & \
@@ -351,11 +353,6 @@ static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
351353
return false;
352354
}
353355

354-
static inline bool transparent_hugepage_active(struct vm_area_struct *vma)
355-
{
356-
return false;
357-
}
358-
359356
static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
360357
unsigned long addr)
361358
{
@@ -368,6 +365,13 @@ static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
368365
return false;
369366
}
370367

368+
static inline bool hugepage_vma_check(struct vm_area_struct *vma,
369+
unsigned long vm_flags,
370+
bool smaps)
371+
{
372+
return false;
373+
}
374+
371375
static inline void prep_transhuge_page(struct page *page) {}
372376

373377
#define transparent_hugepage_flags 0UL

include/linux/khugepaged.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ extern struct attribute_group khugepaged_attr_group;
1010
extern int khugepaged_init(void);
1111
extern void khugepaged_destroy(void);
1212
extern int start_stop_khugepaged(void);
13-
extern bool hugepage_vma_check(struct vm_area_struct *vma,
14-
unsigned long vm_flags);
1513
extern void __khugepaged_enter(struct mm_struct *mm);
1614
extern void __khugepaged_exit(struct mm_struct *mm);
1715
extern void khugepaged_enter_vma(struct vm_area_struct *vma,

mm/huge_memory.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,56 @@ static atomic_t huge_zero_refcount;
6969
struct page *huge_zero_page __read_mostly;
7070
unsigned long huge_zero_pfn __read_mostly = ~0UL;
7171

72-
bool transparent_hugepage_active(struct vm_area_struct *vma)
72+
bool hugepage_vma_check(struct vm_area_struct *vma,
73+
unsigned long vm_flags,
74+
bool smaps)
7375
{
74-
/* The addr is used to check if the vma size fits */
75-
unsigned long addr = (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE;
76+
if (!vma->vm_mm) /* vdso */
77+
return false;
78+
79+
if (!transhuge_vma_enabled(vma, vm_flags))
80+
return false;
7681

77-
if (!transhuge_vma_suitable(vma, addr))
82+
if (vm_flags & VM_NO_KHUGEPAGED)
7883
return false;
79-
if (vma_is_anonymous(vma))
80-
return __transparent_hugepage_enabled(vma);
81-
if (vma_is_shmem(vma))
84+
85+
/* Don't run khugepaged against DAX vma */
86+
if (vma_is_dax(vma))
87+
return false;
88+
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)))
91+
return false;
92+
93+
/* Enabled via shmem mount options or sysfs settings. */
94+
if (shmem_file(vma->vm_file))
8295
return shmem_huge_enabled(vma);
83-
if (transhuge_vma_enabled(vma, vma->vm_flags) && file_thp_enabled(vma))
96+
97+
if (!khugepaged_enabled())
98+
return false;
99+
100+
/* THP settings require madvise. */
101+
if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
102+
return false;
103+
104+
/* Only regular file is valid */
105+
if (file_thp_enabled(vma))
84106
return true;
85107

86-
return false;
108+
if (!vma_is_anonymous(vma))
109+
return false;
110+
111+
if (vma_is_temporary_stack(vma))
112+
return false;
113+
114+
/*
115+
* THPeligible bit of smaps should show 1 for proper VMAs even
116+
* though anon_vma is not initialized yet.
117+
*/
118+
if (!vma->anon_vma)
119+
return smaps;
120+
121+
return true;
87122
}
88123

89124
static bool get_huge_zero_page(void)

mm/khugepaged.c

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -430,46 +430,6 @@ static inline int khugepaged_test_exit(struct mm_struct *mm)
430430
return atomic_read(&mm->mm_users) == 0;
431431
}
432432

433-
bool hugepage_vma_check(struct vm_area_struct *vma,
434-
unsigned long vm_flags)
435-
{
436-
if (!transhuge_vma_enabled(vma, vm_flags))
437-
return false;
438-
439-
if (vm_flags & VM_NO_KHUGEPAGED)
440-
return false;
441-
442-
/* Don't run khugepaged against DAX vma */
443-
if (vma_is_dax(vma))
444-
return false;
445-
446-
/* Check alignment for file vma and size for both file and anon vma */
447-
if (!transhuge_vma_suitable(vma, (vma->vm_end - HPAGE_PMD_SIZE)))
448-
return false;
449-
450-
/* Enabled via shmem mount options or sysfs settings. */
451-
if (shmem_file(vma->vm_file))
452-
return shmem_huge_enabled(vma);
453-
454-
if (!khugepaged_enabled())
455-
return false;
456-
457-
/* THP settings require madvise. */
458-
if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
459-
return false;
460-
461-
/* Only regular file is valid */
462-
if (file_thp_enabled(vma))
463-
return true;
464-
465-
if (!vma->anon_vma || !vma_is_anonymous(vma))
466-
return false;
467-
if (vma_is_temporary_stack(vma))
468-
return false;
469-
470-
return true;
471-
}
472-
473433
void __khugepaged_enter(struct mm_struct *mm)
474434
{
475435
struct mm_slot *mm_slot;
@@ -506,7 +466,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma,
506466
{
507467
if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) &&
508468
khugepaged_enabled()) {
509-
if (hugepage_vma_check(vma, vm_flags))
469+
if (hugepage_vma_check(vma, vm_flags, false))
510470
__khugepaged_enter(vma->vm_mm);
511471
}
512472
}
@@ -956,7 +916,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
956916

957917
if (!transhuge_vma_suitable(vma, address))
958918
return SCAN_ADDRESS_RANGE;
959-
if (!hugepage_vma_check(vma, vma->vm_flags))
919+
if (!hugepage_vma_check(vma, vma->vm_flags, false))
960920
return SCAN_VMA_CHECK;
961921
/*
962922
* Anon VMA expected, the address may be unmapped then
@@ -1441,7 +1401,7 @@ void collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr)
14411401
* the valid THP. Add extra VM_HUGEPAGE so hugepage_vma_check()
14421402
* will not fail the vma for missing VM_HUGEPAGE
14431403
*/
1444-
if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE))
1404+
if (!hugepage_vma_check(vma, vma->vm_flags | VM_HUGEPAGE, false))
14451405
return;
14461406

14471407
/* Keep pmd pgtable for uffd-wp; see comment in retract_page_tables() */
@@ -2131,7 +2091,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
21312091
progress++;
21322092
break;
21332093
}
2134-
if (!hugepage_vma_check(vma, vma->vm_flags)) {
2094+
if (!hugepage_vma_check(vma, vma->vm_flags, false)) {
21352095
skip:
21362096
progress++;
21372097
continue;

0 commit comments

Comments
 (0)