[RFC PATCH 1/3] mm: enable MADV_DONTNEED for hugetlb mappings

From: Mike Kravetz
Date: Thu Jan 13 2022 - 13:03:46 EST


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.

The only thing preventing MADV_DONTNEED (and MADV_FREE) 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_valid_vma()
that will allow hugetlb mappings.

Signed-off-by: Mike Kravetz <mike.kravetz@xxxxxxxxxx>
---
mm/madvise.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/mm/madvise.c b/mm/madvise.c
index 8c927202bbe6..fc8992f4ae40 100644
--- a/mm/madvise.c
+++ b/mm/madvise.c
@@ -766,6 +766,11 @@ static long madvise_dontneed_single_vma(struct vm_area_struct *vma,
return 0;
}

+static bool madvise_dontneed_valid_vma(struct vm_area_struct *vma)
+{
+ return (vma->vm_flags & VM_HUGETLB) || can_madv_lru_vma(vma);
+}
+
static long madvise_dontneed_free(struct vm_area_struct *vma,
struct vm_area_struct **prev,
unsigned long start, unsigned long end,
@@ -774,7 +779,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,
struct mm_struct *mm = vma->vm_mm;

*prev = vma;
- if (!can_madv_lru_vma(vma))
+ if (!madvise_dontneed_valid_vma(vma))
return -EINVAL;

if (!userfaultfd_remove(vma, start, end)) {
@@ -796,7 +801,7 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,
*/
return -ENOMEM;
}
- if (!can_madv_lru_vma(vma))
+ if (!madvise_dontneed_valid_vma(vma))
return -EINVAL;
if (end > vma->vm_end) {
/*
--
2.34.1