Re: [PATCH RFC] mm/madvise: introduce MADV_POPULATE to prefault/prealloc memory

From: David Hildenbrand
Date: Wed Feb 17 2021 - 12:08:47 EST


On 17.02.21 17:46, Dave Hansen wrote:
On 2/17/21 7:48 AM, David Hildenbrand wrote:
While MADV_DONTNEED and FALLOC_FL_PUNCH_HOLE provide us ways to reliably
discard memory, there is no generic approach to populate ("preallocate")
memory.

Although mmap() supports MAP_POPULATE, it is not applicable to the concept
of sparse memory mappings, where we want to do populate/discard
dynamically and avoid expensive/problematic remappings. In addition,
we never actually report error during the final populate phase - it is
best-effort only.

Seems pretty sane to me.

But, I was surprised that MADV_WILLNEED was no mentioned. It might be
nice to touch on on why MADV_WILLNEED is a bad choice for this
functionality? We could theoretically have it populate anonymous
mappings instead of just swapping in.

I stumbled over it, but it ended up looking like mixing in different semantics.

"Expect access in the near future." and "might be a good idea to read some pages" vs. "Definitely populate/preallocate all memory and definitely fail.".


I guess it's possible that folks are using MADV_WILLNEED on sparse
mappings that they don't want to populate, but it would be nice to get
that in the changelog.

Indeed: prime example is virtio-balloon in QEMU when deflating. Just because we are deflating the balloon doesn't mean that the guest is going to use all memory immediately - and that we want to actually consume memory immediately. ... we call MADV_WILLNEED unconditionally on any memory backing when deflating ...

I'll definitely add that to the changelog - thanks.


I was also a bit bummed to see the broad VM_IO/PFNMAP restriction show
up again. I was just looking at implementing pre-faulting for the new
SGX driver:

I added that because __mm_populate() similarly skips over VM_IO | VM_PFNMAP. So it mimics existing "populate semantics" we have.


https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kernel/cpu/sgx/driver.c

It has a vm_ops->fault handler, but the VMAs are VM_IO. It obviously
don't work with gup, though. Not a deal breaker, and something we could
certainly add to this later.

I assume you would then also want to support MAP_POPULATE, right? Because it ends up using __mm_populate() and would not work.

Thanks!

--
Thanks,

David / dhildenb