Re: [PATCH RFC v2] mm: Add f_ops->populate()
From: Jarkko Sakkinen
Date: Mon Mar 07 2022 - 06:32:20 EST
On Sun, Mar 06, 2022 at 03:41:54PM -0800, Dave Hansen wrote:
> On 3/6/22 15:24, Andrew Morton wrote:
> > On Sun, 6 Mar 2022 05:26:55 +0200 Jarkko Sakkinen <jarkko@xxxxxxxxxx> wrote:
> >
> >> Sometimes you might want to use MAP_POPULATE to ask a device driver to
> >> initialize the device memory in some specific manner. SGX driver can use
> >> this to request more memory by issuing ENCLS[EAUG] x86 opcode for each
> >> page in the address range.
> > Why is this useful? Please fully describe the benefit to kernel users.
> > Convince us that the benefit justifies the code churn, maintenance
> > cost and larger kernel footprint.
>
> In short: page faults stink. The core kernel has lots of ways of
> avoiding page faults like madvise(MADV_WILLNEED) or mmap(MAP_POPULATE).
> But, those only work on normal RAM that the core mm manages.
>
> SGX is weird. SGX memory is managed outside the core mm. It doesn't
> have a 'struct page' and get_user_pages() doesn't work on it. Its VMAs
> are marked with VM_IO. So, none of the existing methods for avoiding
> page faults work on SGX memory.
>
> This essentially helps extend existing "normal RAM" kernel ABIs to work
> for avoiding faults for SGX too. SGX users want to enjoy all of the
> benefits of a delayed allocation policy (better resource use,
> overcommit, NUMA affinity) but without the cost of millions of faults.
>
> That said, this isn't how I would have implemented it. I probably would
> have hooked in to populate_vma_page_range() or its callers.
The exact implementation path is not driver in this. I'm open for
better options. The point of these patches is more to show an issue
rather than solution, and they do carry RFC because of that.
Hooking into populate_vma_page_range() does sound like a better idea,
because then it would be nicely embedded into __mm_populate() and
other functionality that calls that function.
But e.g. in __mm_populate() anything with (VM_IO | VM_PFNMAP) gets
filtered out and never reach that function.
I don't know unorthodox that'd be but could we perhaps have a VM
flag for SGX?
BR, Jarkko