Re: [PATCH] mm,madvise,hugetlb: fix unexpected data loss with MADV_DONTNEED on hugetlbfs

From: Andrew Morton
Date: Fri Oct 21 2022 - 16:48:53 EST


On Fri, 21 Oct 2022 15:45:46 -0400 Rik van Riel <riel@xxxxxxxxxxx> 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.

> 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.

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.

> 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!