Re: [PATCH v40 09/24] x86/sgx: Add SGX page allocator functions

From: Borislav Petkov
Date: Thu Nov 05 2020 - 10:08:57 EST


On Wed, Nov 04, 2020 at 04:54:15PM +0200, Jarkko Sakkinen wrote:
> The previous patch initialized a simple SGX page allocator. Add functions
> for runtime allocation and free.
>
> This allocator and its algorithms are as simple as it gets. They do a
> linear search across all EPC sections and find the first free page. They
> are not NUMA aware and only hand out individual pages. The SGX hardware
> does not support large pages, so something more complicated like a buddy
> allocator is unwarranted.
>
> The free function (sgx_free_epc_page()) implicitly calls ENCLS[EREMOVE],
> which returns the page to the uninitialized state. This ensures that the
> page is ready for use at the next allocation.
>
> Acked-by: Jethro Beekman <jethro@xxxxxxxxxxxx>
> Reviewed-by: Darren Kenny <darren.kenny@xxxxxxxxxx>
> Co-developed-by: Sean Christopherson <sean.j.christopherson@xxxxxxxxx>
> Signed-off-by: Sean Christopherson <sean.j.christopherson@xxxxxxxxx>
> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@xxxxxxxxxxxxxxx>
> ---
> arch/x86/kernel/cpu/sgx/main.c | 62 ++++++++++++++++++++++++++++++++++
> arch/x86/kernel/cpu/sgx/sgx.h | 3 ++
> 2 files changed, 65 insertions(+)
>
> diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
> index 956055a0eff6..b9ac438a13a4 100644
> --- a/arch/x86/kernel/cpu/sgx/main.c
> +++ b/arch/x86/kernel/cpu/sgx/main.c
> @@ -85,6 +85,68 @@ static bool __init sgx_page_reclaimer_init(void)
> return true;
> }
>
> +static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_section *section)
> +{
> + struct sgx_epc_page *page;
> +
> + if (list_empty(&section->page_list))
> + return NULL;
> +
> + page = list_first_entry(&section->page_list, struct sgx_epc_page, list);
> + list_del_init(&page->list);
> +
> + return page;
> +}
> +
> +/**
> + * __sgx_alloc_epc_page() - Allocate an EPC page
> + *
> + * Iterate through EPC sections and borrow a free EPC page to the caller. When a
> + * page is no longer needed it must be released with sgx_free_epc_page().
> + *
> + * Return:
> + * an EPC page,
> + * -errno on error
> + */
> +struct sgx_epc_page *__sgx_alloc_epc_page(void)
> +{
> + struct sgx_epc_section *section;
> + struct sgx_epc_page *page;
> + int i;
> +
> + for (i = 0; i < sgx_nr_epc_sections; i++) {
> + section = &sgx_epc_sections[i];
> + spin_lock(&section->lock);
> + page = __sgx_alloc_epc_page_from_section(section);
> + spin_unlock(&section->lock);

Something for a future cleanup: you can put that logic into
__sgx_alloc_epc_page_from_section() and simplify this one call site.

But not now - you can do that later or if v41 needs to be sent out...

Thx.

--
Regards/Gruss,
Boris.

https://people.kernel.org/tglx/notes-about-netiquette