Skip to content

Commit 50f8b92

Browse files
liu-song-6torvalds
authored andcommitted
mm: thp: pass correct vm_flags to hugepage_vma_check()
khugepaged_enter_vma_merge() passes a stale vma->vm_flags to hugepage_vma_check(). The argument vm_flags contains the latest value. Therefore, it is necessary to pass this vm_flags into hugepage_vma_check(). With this bug, madvise(MADV_HUGEPAGE) for mmap files in shmem fails to put memory in huge pages. Here is an example of failed madvise(): /* mount /dev/shm with huge=advise: * mount -o remount,huge=advise /dev/shm */ /* create file /dev/shm/huge */ #define HUGE_FILE "/dev/shm/huge" fd = open(HUGE_FILE, O_RDONLY); ptr = mmap(NULL, FILE_SIZE, PROT_READ, MAP_PRIVATE, fd, 0); ret = madvise(ptr, FILE_SIZE, MADV_HUGEPAGE); madvise() will return 0, but this memory region is never put in huge page (check from /proc/meminfo: ShmemHugePages). Link: http://lkml.kernel.org/r/20180629181752.792831-1-songliubraving@fb.com Fixes: 02b75dc8160d ("mm: thp: register mm for khugepaged when merging vma for shmem") Signed-off-by: Song Liu <songliubraving@fb.com> Reviewed-by: Rik van Riel <riel@surriel.com> Reviewed-by: Yang Shi <yang.shi@linux.alibaba.com> Cc: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a718e28 commit 50f8b92

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

‎mm/khugepaged.c‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,11 @@ static inline int khugepaged_test_exit(struct mm_struct *mm)
397397
return atomic_read(&mm->mm_users) == 0;
398398
}
399399

400-
static bool hugepage_vma_check(struct vm_area_struct *vma)
400+
static bool hugepage_vma_check(struct vm_area_struct *vma,
401+
unsigned long vm_flags)
401402
{
402-
if ((!(vma->vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
403-
(vma->vm_flags & VM_NOHUGEPAGE) ||
403+
if ((!(vm_flags & VM_HUGEPAGE) && !khugepaged_always()) ||
404+
(vm_flags & VM_NOHUGEPAGE) ||
404405
test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
405406
return false;
406407
if (shmem_file(vma->vm_file)) {
@@ -413,7 +414,7 @@ static bool hugepage_vma_check(struct vm_area_struct *vma)
413414
return false;
414415
if (is_vma_temporary_stack(vma))
415416
return false;
416-
return !(vma->vm_flags & VM_NO_KHUGEPAGED);
417+
return !(vm_flags & VM_NO_KHUGEPAGED);
417418
}
418419

419420
int __khugepaged_enter(struct mm_struct *mm)
@@ -458,7 +459,7 @@ int khugepaged_enter_vma_merge(struct vm_area_struct *vma,
458459
* khugepaged does not yet work on non-shmem files or special
459460
* mappings. And file-private shmem THP is not supported.
460461
*/
461-
if (!hugepage_vma_check(vma))
462+
if (!hugepage_vma_check(vma, vm_flags))
462463
return 0;
463464

464465
hstart = (vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK;
@@ -861,7 +862,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm, unsigned long address,
861862
hend = vma->vm_end & HPAGE_PMD_MASK;
862863
if (address < hstart || address + HPAGE_PMD_SIZE > hend)
863864
return SCAN_ADDRESS_RANGE;
864-
if (!hugepage_vma_check(vma))
865+
if (!hugepage_vma_check(vma, vma->vm_flags))
865866
return SCAN_VMA_CHECK;
866867
return 0;
867868
}
@@ -1695,7 +1696,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages,
16951696
progress++;
16961697
break;
16971698
}
1698-
if (!hugepage_vma_check(vma)) {
1699+
if (!hugepage_vma_check(vma, vma->vm_flags)) {
16991700
skip:
17001701
progress++;
17011702
continue;

0 commit comments

Comments
 (0)