Skip to content

Commit e6be37b

Browse files
MiaoheLintorvalds
authored andcommitted
mm/huge_memory.c: add missing read-only THP checking in transparent_hugepage_enabled()
Since commit 99cb0db ("mm,thp: add read-only THP support for (non-shmem) FS"), read-only THP file mapping is supported. But it forgot to add checking for it in transparent_hugepage_enabled(). To fix it, we add checking for read-only THP file mapping and also introduce helper transhuge_vma_enabled() to check whether thp is enabled for specified vma to reduce duplicated code. We rename transparent_hugepage_enabled to transparent_hugepage_active to make the code easier to follow as suggested by David Hildenbrand. [[email protected]: define transhuge_vma_enabled next to transhuge_vma_suitable] Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 99cb0db ("mm,thp: add read-only THP support for (non-shmem) FS") Signed-off-by: Miaohe Lin <[email protected]> Reviewed-by: Yang Shi <[email protected]> Cc: Alexey Dobriyan <[email protected]> Cc: "Aneesh Kumar K . V" <[email protected]> Cc: Anshuman Khandual <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Kirill A. Shutemov <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Minchan Kim <[email protected]> Cc: Ralph Campbell <[email protected]> Cc: Rik van Riel <[email protected]> Cc: Song Liu <[email protected]> Cc: William Kucharski <[email protected]> Cc: Zi Yan <[email protected]> Cc: Mike Kravetz <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent dfe5c51 commit e6be37b

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

fs/proc/task_mmu.c

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

834834
seq_printf(m, "THPeligible: %d\n",
835-
transparent_hugepage_enabled(vma));
835+
transparent_hugepage_active(vma));
836836

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

include/linux/huge_mm.h

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,34 @@ extern struct kobj_attribute shmem_enabled_attr;
115115

116116
extern unsigned long transparent_hugepage_flags;
117117

118+
static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
119+
unsigned long haddr)
120+
{
121+
/* Don't have to check pgoff for anonymous vma */
122+
if (!vma_is_anonymous(vma)) {
123+
if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
124+
HPAGE_PMD_NR))
125+
return false;
126+
}
127+
128+
if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
129+
return false;
130+
return true;
131+
}
132+
133+
static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
134+
unsigned long vm_flags)
135+
{
136+
/* Explicitly disabled through madvise. */
137+
if ((vm_flags & VM_NOHUGEPAGE) ||
138+
test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
139+
return false;
140+
return true;
141+
}
142+
118143
/*
119144
* to be used on vmas which are known to support THP.
120-
* Use transparent_hugepage_enabled otherwise
145+
* Use transparent_hugepage_active otherwise
121146
*/
122147
static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
123148
{
@@ -128,15 +153,12 @@ static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
128153
if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_NEVER_DAX))
129154
return false;
130155

131-
if (vma->vm_flags & VM_NOHUGEPAGE)
156+
if (!transhuge_vma_enabled(vma, vma->vm_flags))
132157
return false;
133158

134159
if (vma_is_temporary_stack(vma))
135160
return false;
136161

137-
if (test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
138-
return false;
139-
140162
if (transparent_hugepage_flags & (1 << TRANSPARENT_HUGEPAGE_FLAG))
141163
return true;
142164

@@ -150,22 +172,7 @@ static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
150172
return false;
151173
}
152174

153-
bool transparent_hugepage_enabled(struct vm_area_struct *vma);
154-
155-
static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
156-
unsigned long haddr)
157-
{
158-
/* Don't have to check pgoff for anonymous vma */
159-
if (!vma_is_anonymous(vma)) {
160-
if (!IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
161-
HPAGE_PMD_NR))
162-
return false;
163-
}
164-
165-
if (haddr < vma->vm_start || haddr + HPAGE_PMD_SIZE > vma->vm_end)
166-
return false;
167-
return true;
168-
}
175+
bool transparent_hugepage_active(struct vm_area_struct *vma);
169176

170177
#define transparent_hugepage_use_zero_page() \
171178
(transparent_hugepage_flags & \
@@ -352,7 +359,7 @@ static inline bool __transparent_hugepage_enabled(struct vm_area_struct *vma)
352359
return false;
353360
}
354361

355-
static inline bool transparent_hugepage_enabled(struct vm_area_struct *vma)
362+
static inline bool transparent_hugepage_active(struct vm_area_struct *vma)
356363
{
357364
return false;
358365
}
@@ -363,6 +370,12 @@ static inline bool transhuge_vma_suitable(struct vm_area_struct *vma,
363370
return false;
364371
}
365372

373+
static inline bool transhuge_vma_enabled(struct vm_area_struct *vma,
374+
unsigned long vm_flags)
375+
{
376+
return false;
377+
}
378+
366379
static inline void prep_transhuge_page(struct page *page) {}
367380

368381
static inline bool is_transparent_hugepage(struct page *page)

mm/huge_memory.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ static atomic_t huge_zero_refcount;
6464
struct page *huge_zero_page __read_mostly;
6565
unsigned long huge_zero_pfn __read_mostly = ~0UL;
6666

67-
bool transparent_hugepage_enabled(struct vm_area_struct *vma)
67+
static inline bool file_thp_enabled(struct vm_area_struct *vma)
68+
{
69+
return transhuge_vma_enabled(vma, vma->vm_flags) && vma->vm_file &&
70+
!inode_is_open_for_write(vma->vm_file->f_inode) &&
71+
(vma->vm_flags & VM_EXEC);
72+
}
73+
74+
bool transparent_hugepage_active(struct vm_area_struct *vma)
6875
{
6976
/* The addr is used to check if the vma size fits */
7077
unsigned long addr = (vma->vm_end & HPAGE_PMD_MASK) - HPAGE_PMD_SIZE;
@@ -75,6 +82,8 @@ bool transparent_hugepage_enabled(struct vm_area_struct *vma)
7582
return __transparent_hugepage_enabled(vma);
7683
if (vma_is_shmem(vma))
7784
return shmem_huge_enabled(vma);
85+
if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS))
86+
return file_thp_enabled(vma);
7887

7988
return false;
8089
}

mm/khugepaged.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,7 @@ static inline int khugepaged_test_exit(struct mm_struct *mm)
442442
static bool hugepage_vma_check(struct vm_area_struct *vma,
443443
unsigned long vm_flags)
444444
{
445-
/* Explicitly disabled through madvise. */
446-
if ((vm_flags & VM_NOHUGEPAGE) ||
447-
test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
445+
if (!transhuge_vma_enabled(vma, vm_flags))
448446
return false;
449447

450448
/* Enabled via shmem mount options or sysfs settings. */

mm/shmem.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4040,8 +4040,7 @@ bool shmem_huge_enabled(struct vm_area_struct *vma)
40404040
loff_t i_size;
40414041
pgoff_t off;
40424042

4043-
if ((vma->vm_flags & VM_NOHUGEPAGE) ||
4044-
test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
4043+
if (!transhuge_vma_enabled(vma, vma->vm_flags))
40454044
return false;
40464045
if (shmem_huge == SHMEM_HUGE_FORCE)
40474046
return true;

0 commit comments

Comments
 (0)