Re: [PATCHv9 02/14] mm: Add support for unaccepted memory
From: Vlastimil Babka
Date: Mon Apr 03 2023 - 05:27:00 EST
On 3/30/23 13:49, Kirill A. Shutemov wrote:
> UEFI Specification version 2.9 introduces the concept of memory
> acceptance. Some Virtual Machine platforms, such as Intel TDX or AMD
> SEV-SNP, require memory to be accepted before it can be used by the
> guest. Accepting happens via a protocol specific to the Virtual Machine
> platform.
>
> There are several ways kernel can deal with unaccepted memory:
>
> 1. Accept all the memory during the boot. It is easy to implement and
> it doesn't have runtime cost once the system is booted. The downside
> is very long boot time.
>
> Accept can be parallelized to multiple CPUs to keep it manageable
> (i.e. via DEFERRED_STRUCT_PAGE_INIT), but it tends to saturate
> memory bandwidth and does not scale beyond the point.
>
> 2. Accept a block of memory on the first use. It requires more
> infrastructure and changes in page allocator to make it work, but
> it provides good boot time.
>
> On-demand memory accept means latency spikes every time kernel steps
> onto a new memory block. The spikes will go away once workload data
> set size gets stabilized or all memory gets accepted.
>
> 3. Accept all memory in background. Introduce a thread (or multiple)
> that gets memory accepted proactively. It will minimize time the
> system experience latency spikes on memory allocation while keeping
> low boot time.
>
> This approach cannot function on its own. It is an extension of #2:
> background memory acceptance requires functional scheduler, but the
> page allocator may need to tap into unaccepted memory before that.
>
> The downside of the approach is that these threads also steal CPU
> cycles and memory bandwidth from the user's workload and may hurt
> user experience.
>
> The patch implements #1 and #2 for now. #2 is the default. Some
> workloads may want to use #1 with accept_memory=eager in kernel
> command line. #3 can be implemented later based on user's demands.
>
> Support of unaccepted memory requires a few changes in core-mm code:
>
> - memblock has to accept memory on allocation;
>
> - page allocator has to accept memory on the first allocation of the
> page;
>
> Memblock change is trivial.
>
> The page allocator is modified to accept pages. New memory gets accepted
> before putting pages on free lists. It is done lazily: only accept new
> pages when we run out of already accepted memory. The memory gets
> accepted until the high watermark is reached.
Great.
> Architecture has to provide two helpers if it wants to support
> unaccepted memory:
>
> - accept_memory() makes a range of physical addresses accepted.
>
> - range_contains_unaccepted_memory() checks anything within the range
> of physical addresses requires acceptance.
>
> Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
> Acked-by: Mike Rapoport <rppt@xxxxxxxxxxxxx> # memblock
Reviewed-by: Vlastimil Babka <vbabka@xxxxxxx>
Just a small suggestion below:
> +
> +static bool try_to_accept_memory(struct zone *zone, unsigned int order)
> +{
> + long to_accept;
> + int ret = false;
> +
> + if (!static_branch_unlikely(&zones_with_unaccepted_pages))
> + return false;
This potentially (depends on what compiler decides) means we'll call this
function just to skip the static branch. OTOH forcing it as inline would be
wasteful too. So I'd split that away and make the callers do that static
branch check inline. Just as deferred_pages_enabled() is used.
> + /* How much to accept to get to high watermark? */
> + to_accept = high_wmark_pages(zone) -
> + (zone_page_state(zone, NR_FREE_PAGES) -
> + __zone_watermark_unusable_free(zone, order, 0));
> +
> + /* Accept at least one page */
> + do {
> + if (!try_to_accept_memory_one(zone))
> + break;
> + ret = true;
> + to_accept -= MAX_ORDER_NR_PAGES;
> + } while (to_accept > 0);
> +
> + return ret;
> +}