Skip to content

Commit cd89fb0

Browse files
rikvanrieltorvalds
authored andcommitted
mm,thp,shmem: make khugepaged obey tmpfs mount flags
Currently if thp enabled=[madvise], mounting a tmpfs filesystem with huge=always and mmapping files from that tmpfs does not result in khugepaged collapsing those mappings, despite the mount flag indicating that it should. Fix that by breaking up the blocks of tests in hugepage_vma_check a little bit, and testing things in the correct order. Link: https://lkml.kernel.org/r/20201124194925.623931-4-riel@surriel.com Fixes: c223102 ("mm: thp: register mm for khugepaged when merging vma for shmem") Signed-off-by: Rik van Riel <riel@surriel.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Matthew Wilcox (Oracle) <willy@infradead.org> Cc: Mel Gorman <mgorman@suse.de> Cc: Michal Hocko <mhocko@suse.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Xu Yu <xuyu@linux.alibaba.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 78cc8cd commit cd89fb0

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

include/linux/khugepaged.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define _LINUX_KHUGEPAGED_H
44

55
#include <linux/sched/coredump.h> /* MMF_VM_HUGEPAGE */
6+
#include <linux/shmem_fs.h>
67

78

89
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
@@ -57,6 +58,7 @@ static inline int khugepaged_enter(struct vm_area_struct *vma,
5758
{
5859
if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags))
5960
if ((khugepaged_always() ||
61+
(shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) ||
6062
(khugepaged_req_madv() && (vm_flags & VM_HUGEPAGE))) &&
6163
!(vm_flags & VM_NOHUGEPAGE) &&
6264
!test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))

mm/khugepaged.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,18 +442,28 @@ 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-
if ((!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
446-
(vm_flags & VM_NOHUGEPAGE) ||
445+
/* Explicitly disabled through madvise. */
446+
if ((vm_flags & VM_NOHUGEPAGE) ||
447447
test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
448448
return false;
449449

450-
if (shmem_file(vma->vm_file) ||
451-
(IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) &&
452-
vma->vm_file &&
453-
(vm_flags & VM_DENYWRITE))) {
450+
/* Enabled via shmem mount options or sysfs settings. */
451+
if (shmem_file(vma->vm_file) && shmem_huge_enabled(vma)) {
454452
return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
455453
HPAGE_PMD_NR);
456454
}
455+
456+
/* THP settings require madvise. */
457+
if (!(vm_flags & VM_HUGEPAGE) && !khugepaged_always())
458+
return false;
459+
460+
/* Read-only file mappings need to be aligned for THP to work. */
461+
if (IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && vma->vm_file &&
462+
(vm_flags & VM_DENYWRITE)) {
463+
return IS_ALIGNED((vma->vm_start >> PAGE_SHIFT) - vma->vm_pgoff,
464+
HPAGE_PMD_NR);
465+
}
466+
457467
if (!vma->anon_vma || vma->vm_ops)
458468
return false;
459469
if (vma_is_temporary_stack(vma))

0 commit comments

Comments
 (0)