Skip to contentSkip to conversation

mm,madvise,hugetlb: fix unexpected data loss with MADV_DONTNEED on hugetlbfs

PATCH Superseded Rik van Riel 3 participants 5 messages Oct 21, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm,madvise,hugetlb: fix unexpected data loss with MADV_DONTNEED on hugetlbfs

A common use case for hugetlbfs is for the application to create memory pools backed by huge pages, which then get handed over to some malloc library (eg. jemalloc) for further management.

That malloc library may be doing MADV_DONTNEED calls on memory that is no longer needed, expecting those calls to happen on PAGE_SIZE boundaries.

However, currently the MADV_DONTNEED code rounds up any such requests to HPAGE_PMD_SIZE boundaries. This leads to undesired outcomes when jemalloc expects a 4kB MADV_DONTNEED, but 2MB of memory get zeroed out, instead.

Use of pre-built shared libraries means that user code does not always know the page size of every memory arena in use.

Avoid unexpected data loss with MADV_DONTNEED by rounding up only to PAGE_SIZE (in do_madvise), and rounding down to huge page granularity.

That way programs will only get as much memory zeroed out as they requested.

While we're here, refactor madvise_dontneed_free_valid_vma a little so mlocked hugetlb VMAs need MADV_DONTNEED_LOCKED.

Cc Mike Kravetz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Fixes 90e7e7f5ef3f ("mm: enable MADV_DONTNEED for hugetlb mappings")
---
 mm/madvise.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
mm/madvise.c +19 -7
index 2baa93ca2310..a60e8e23c323 100644
@@ -799,21 +799,29 @@ static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma,
 					    unsigned long *end,
 					    int behavior)
 {
-	if (!is_vm_hugetlb_page(vma)) {
-		unsigned int forbidden = VM_PFNMAP;
+	unsigned int forbidden = VM_PFNMAP;
 
-		if (behavior != MADV_DONTNEED_LOCKED)
-			forbidden |= VM_LOCKED;
+	if (behavior != MADV_DONTNEED_LOCKED)
+		forbidden |= VM_LOCKED;
 
-		return !(vma->vm_flags & forbidden);
-	}
+	if (vma->vm_flags & forbidden)
+		return false;
+
+	if (!is_vm_hugetlb_page(vma))
+		return true;
 
 	if (behavior != MADV_DONTNEED && behavior != MADV_DONTNEED_LOCKED)
 		return false;
 	if (start & ~huge_page_mask(hstate_vma(vma)))
 		return false;
 
-	*end = ALIGN(*end, huge_page_size(hstate_vma(vma)));
+	/*
+	 * Madvise callers expect the length to be rounded up to the page
+	 * size, but they may not know the page size for this VMA is larger
+	 * than PAGE_SIZE! Round down huge pages to avoid unexpected data loss.
+	 */
+	*end = ALIGN_DOWN(*end, huge_page_size(hstate_vma(vma)));
+
 	return true;
 }
 
@@ -828,6 +836,10 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,
 	if (!madvise_dontneed_free_valid_vma(vma, start, &end, behavior))
 		return -EINVAL;
 
+	/* A small MADV_DONTNEED on a huge page gets rounded down to zero. */
+	if (start == end)
+		return 0;
+
 	if (!userfaultfd_remove(vma, start, end)) {
 		*prev = NULL; /* mmap_lock has been dropped, prev is stale */
 
Signature · 1 line
2.37.2
#1 lore raw
Quoted 8 lines · Rik van Riel

A common use case for hugetlbfs is for the application to create memory pools backed by huge pages, which then get handed over to some malloc library (eg. jemalloc) for further management.

That malloc library may be doing MADV_DONTNEED calls on memory that is no longer needed, expecting those calls to happen on PAGE_SIZE boundaries.

Thanks Rik. I tend to agree with this direction as it is 'breaking' current code. David and I discussed this in this thread,
https://lore.kernel.org/linux-mm/[email protected]/

One thing to note is that there was not any documentation saying madvise would happen on page boundaries. The system call takes a length and rounds up to page size. However, the man page explicitly said it operates on a byte range. Certainly mm people and others know we only operate on pages. But, that is not what was documented.

When the change was made to add hugetlb support, the decision was made to round up the range to hugetlb page boundaries in hugetlb vmas. This was to be consistent with how madvise operated on base pages. At the same time, madvise documentation was updated say it operates on page boundaries as well as the behavior for hugetlb mappings. If moving forward with this change we will need to update the man page.

Signature · 1 line
Mike Kravetz
Quoted 82 lines

However, currently the MADV_DONTNEED code rounds up any such requests to HPAGE_PMD_SIZE boundaries. This leads to undesired outcomes when jemalloc expects a 4kB MADV_DONTNEED, but 2MB of memory get zeroed out, instead.

Use of pre-built shared libraries means that user code does not always know the page size of every memory arena in use.

Avoid unexpected data loss with MADV_DONTNEED by rounding up only to PAGE_SIZE (in do_madvise), and rounding down to huge page granularity.

That way programs will only get as much memory zeroed out as they requested.

While we're here, refactor madvise_dontneed_free_valid_vma a little so mlocked hugetlb VMAs need MADV_DONTNEED_LOCKED.

Cc Mike Kravetz <[email protected]>
Cc Andrew Morton <[email protected]>
Cc David Hildenbrand <[email protected]>
Fixes 90e7e7f5ef3f ("mm: enable MADV_DONTNEED for hugetlb mappings")
---
 mm/madvise.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
mm/madvise.c +19 -7
index 2baa93ca2310..a60e8e23c323 100644
@@ -799,21 +799,29 @@ static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma,
 					    unsigned long *end,
 					    int behavior)
 {
-	if (!is_vm_hugetlb_page(vma)) {
-		unsigned int forbidden = VM_PFNMAP;
+	unsigned int forbidden = VM_PFNMAP;
 
-		if (behavior != MADV_DONTNEED_LOCKED)
-			forbidden |= VM_LOCKED;
+	if (behavior != MADV_DONTNEED_LOCKED)
+		forbidden |= VM_LOCKED;
 
-		return !(vma->vm_flags & forbidden);
-	}
+	if (vma->vm_flags & forbidden)
+		return false;
+
+	if (!is_vm_hugetlb_page(vma))
+		return true;
 
 	if (behavior != MADV_DONTNEED && behavior != MADV_DONTNEED_LOCKED)
 		return false;
 	if (start & ~huge_page_mask(hstate_vma(vma)))
 		return false;
 
-	*end = ALIGN(*end, huge_page_size(hstate_vma(vma)));
+	/*
+	 * Madvise callers expect the length to be rounded up to the page
+	 * size, but they may not know the page size for this VMA is larger
+	 * than PAGE_SIZE! Round down huge pages to avoid unexpected data loss.
+	 */
+	*end = ALIGN_DOWN(*end, huge_page_size(hstate_vma(vma)));
+
 	return true;
 }
 
@@ -828,6 +836,10 @@ static long madvise_dontneed_free(struct vm_area_struct *vma,
 	if (!madvise_dontneed_free_valid_vma(vma, start, &end, behavior))
 		return -EINVAL;
 
+	/* A small MADV_DONTNEED on a huge page gets rounded down to zero. */
+	if (start == end)
+		return 0;
+
 	if (!userfaultfd_remove(vma, start, end)) {
 		*prev = NULL; /* mmap_lock has been dropped, prev is stale */
 
Signature · 1 line
2.37.2
#2 lore raw
Quoted 20 lines · Mike Kravetz
Quoted 8 lines · Rik van Riel

A common use case for hugetlbfs is for the application to create memory pools backed by huge pages, which then get handed over to some malloc library (eg. jemalloc) for further management.

That malloc library may be doing MADV_DONTNEED calls on memory that is no longer needed, expecting those calls to happen on PAGE_SIZE boundaries.

Thanks Rik.  I tend to agree with this direction as it is 'breaking' current code.  David and I discussed this in this thread,
https://lore.kernel.org/linux-mm/[email protected]/

One thing to note is that there was not any documentation saying madvise would happen on page boundaries.  The system call takes a length and rounds up to page size.  However, the man page explicitly said it operates on a byte range.  Certainly mm people and others know we only operate on pages.  But, that is not what was documented.

Quoted 8 lines

When the change was made to add hugetlb support, the decision was made
to round up the range to hugetlb page boundaries in hugetlb vmas. This
was to be consistent with how madvise operated on base pages.  At the same time, madvise documentation was updated say it operates on page boundaries as well as the behavior for hugetlb mappings.  If moving forward with this change we will need to update the man page.

I'll send in a patch for the man page after the patch gets merged. I'll change the text to clarify that the system may round up the specified length to PAGE_SIZE granularity, which is a quantity programs can get through (IIRC) getconf.

Andrew, I split out the bit of the patch for stable.

Signature · 1 line
All Rights Reversed.
#3 lore raw
Quoted 30 lines · Rik van Riel
Quoted 20 lines · Mike Kravetz
Quoted 8 lines · Rik van Riel

A common use case for hugetlbfs is for the application to create memory pools backed by huge pages, which then get handed over to some malloc library (eg. jemalloc) for further management.

That malloc library may be doing MADV_DONTNEED calls on memory that is no longer needed, expecting those calls to happen on PAGE_SIZE boundaries.

Thanks Rik.  I tend to agree with this direction as it is 'breaking' current code.  David and I discussed this in this thread,
https://lore.kernel.org/linux-mm/[email protected]/

One thing to note is that there was not any documentation saying madvise would happen on page boundaries.  The system call takes a length and rounds up to page size.  However, the man page explicitly said it operates on a byte range.  Certainly mm people and others know we only operate on pages.  But, that is not what was documented.

Quoted 8 lines

When the change was made to add hugetlb support, the decision was made
to round up the range to hugetlb page boundaries in hugetlb vmas. This
was to be consistent with how madvise operated on base pages.  At the same time, madvise documentation was updated say it operates on page boundaries as well as the behavior for hugetlb mappings.  If moving forward with this change we will need to update the man page.

Quoted 4 lines

I'll send in a patch for the man page after the patch gets merged. I'll change the text to clarify that the system may round up the specified length to PAGE_SIZE granularity, which is a quantity programs can get through (IIRC) getconf.

Ok, the man page now says madvise only operates on page granularity. Perhaps we can add info about using sysconf(PAGE_SIZE) if that will help. What will really need to be changed is the description of rounding hugetlb mappings up to the next huge page size boundary. Just need to describe the hugetlb behavior. Unfortunately, we may need to define/describe the arithmetic:
ALIGN_DOWN(PAGE_ALIGN(length))
i.e. Will align down except in the case length is within PAGE_SIZE
of hugetlb page size.

Signature · 1 line
Mike Kravetz
maintainer
#4 lore raw
In reply to Rik van Riel
Quoted 10 lines · On Fri, 21 Oct 2022 15:45:46 -0400 Rik van Riel <[email protected]> wrote:

A common use case for hugetlbfs is for the application to create memory pools backed by huge pages, which then get handed over to some malloc library (eg. jemalloc) for further management.

That malloc library may be doing MADV_DONTNEED calls on memory that is no longer needed, expecting those calls to happen on PAGE_SIZE boundaries.

However, currently the MADV_DONTNEED code rounds up any such requests to HPAGE_PMD_SIZE boundaries.

Well that's obnoxious.

Quoted 7 lines

This leads to undesired
outcomes when jemalloc expects a 4kB MADV_DONTNEED, but 2MB of memory get zeroed out, instead.

Use of pre-built shared libraries means that user code does not always know the page size of every memory arena in use.

Quoted 6 lines

Avoid unexpected data loss with MADV_DONTNEED by rounding up only to PAGE_SIZE (in do_madvise), and rounding down to huge page granularity.

That way programs will only get as much memory zeroed out as they requested.

If we merge this, we're inviting people to develop and test code on the 6.2 kernel only to ship it and then find that it misbehaves on 6.1 and earlier.

So I think we should backport this.

Quoted 2 lines

While we're here, refactor madvise_dontneed_free_valid_vma a little so mlocked hugetlb VMAs need MADV_DONTNEED_LOCKED.

And if we do backport it, "while we're here" changes are unwelcome!