Skip to content

Commit 90e7e7f

Browse files
mjkravetztorvalds
authored andcommitted
mm: enable MADV_DONTNEED for hugetlb mappings
Patch series "Add hugetlb MADV_DONTNEED support", v3. Userfaultfd selftests for hugetlb does not perform UFFD_EVENT_REMAP testing. However, mremap support was recently added in commit 550a7d6 ("mm, hugepages: add mremap() support for hugepage backed vma"). While attempting to enable mremap support in the test, it was discovered that the mremap test indirectly depends on MADV_DONTNEED. madvise does not allow MADV_DONTNEED for hugetlb mappings. However, that is primarily due to the check in can_madv_lru_vma(). By simply removing the check and adding huge page alignment, MADV_DONTNEED can be made to work for hugetlb mappings. Do note that there is no compelling use case for adding this support. This was discussed in the RFC [1]. However, adding support makes sense as it is fairly trivial and brings hugetlb functionality more in line with 'normal' memory. After enabling support, add selftest for MADV_DONTNEED as well as MADV_REMOVE. Then update userfaultfd selftest. If new functionality is accepted, then madvise man page will be updated to indicate hugetlb is supported. It will also be updated to clarify what happens to the passed length argument. This patch (of 3): MADV_DONTNEED is currently disabled for hugetlb mappings. This certainly makes sense in shared file mappings as the pagecache maintains a reference to the page and it will never be freed. However, it could be useful to unmap and free pages in private mappings. In addition, userfaultfd minor fault users may be able to simplify code by using MADV_DONTNEED. The primary thing preventing MADV_DONTNEED from working on hugetlb mappings is a check in can_madv_lru_vma(). To allow support for hugetlb mappings create and use a new routine madvise_dontneed_free_valid_vma() that allows hugetlb mappings in this specific case. For normal mappings, madvise requires the start address be PAGE aligned and rounds up length to the next multiple of PAGE_SIZE. Do similarly for hugetlb mappings: require start address be huge page size aligned and round up length to the next multiple of huge page size. Use the new madvise_dontneed_free_valid_vma routine to check alignment and round up length/end. zap_page_range requires this alignment for hugetlb vmas otherwise we will hit BUGs. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Mike Kravetz <[email protected]> Cc: Naoya Horiguchi <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Axel Rasmussen <[email protected]> Cc: Mina Almasry <[email protected]> Cc: Michal Hocko <[email protected]> Cc: Peter Xu <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Mike Rapoport <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent c32caa2 commit 90e7e7f

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

mm/madvise.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,14 @@ static void madvise_cold_page_range(struct mmu_gather *tlb,
502502
tlb_end_vma(tlb, vma);
503503
}
504504

505+
static inline bool can_madv_lru_non_huge_vma(struct vm_area_struct *vma)
506+
{
507+
return !(vma->vm_flags & (VM_LOCKED|VM_PFNMAP));
508+
}
509+
505510
static inline bool can_madv_lru_vma(struct vm_area_struct *vma)
506511
{
507-
return !(vma->vm_flags & (VM_LOCKED|VM_HUGETLB|VM_PFNMAP));
512+
return can_madv_lru_non_huge_vma(vma) && !is_vm_hugetlb_page(vma);
508513
}
509514

510515
static long madvise_cold(struct vm_area_struct *vma,
@@ -777,6 +782,23 @@ static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
777782
return 0;
778783
}
779784

785+
static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma,
786+
unsigned long start,
787+
unsigned long *end,
788+
int behavior)
789+
{
790+
if (!is_vm_hugetlb_page(vma))
791+
return can_madv_lru_non_huge_vma(vma);
792+
793+
if (behavior != MADV_DONTNEED)
794+
return false;
795+
if (start & ~huge_page_mask(hstate_vma(vma)))
796+
return false;
797+
798+
*end = ALIGN(*end, huge_page_size(hstate_vma(vma)));
799+
return true;
800+
}
801+
780802
static long madvise_dontneed_free(struct vm_area_struct *vma,
781803
struct vm_area_struct **prev,
782804
unsigned long start, unsigned long end,
@@ -785,7 +807,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,
785807
struct mm_struct *mm = vma->vm_mm;
786808

787809
*prev = vma;
788-
if (!can_madv_lru_vma(vma))
810+
if (!madvise_dontneed_free_valid_vma(vma, start, &end, behavior))
789811
return -EINVAL;
790812

791813
if (!userfaultfd_remove(vma, start, end)) {
@@ -807,7 +829,12 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,
807829
*/
808830
return -ENOMEM;
809831
}
810-
if (!can_madv_lru_vma(vma))
832+
/*
833+
* Potential end adjustment for hugetlb vma is OK as
834+
* the check below keeps end within vma.
835+
*/
836+
if (!madvise_dontneed_free_valid_vma(vma, start, &end,
837+
behavior))
811838
return -EINVAL;
812839
if (end > vma->vm_end) {
813840
/*

0 commit comments

Comments
 (0)