Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp486291pxb; Wed, 18 Nov 2020 09:22:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJza/DAQZ7SJNvnPBTWDCvIrIrx+AFa7Ld9odmzZusfZPEri/6zPl3GhbWIugBJROOnbSy6I X-Received: by 2002:a17:906:814:: with SMTP id e20mr24524323ejd.514.1605720166277; Wed, 18 Nov 2020 09:22:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605720166; cv=none; d=google.com; s=arc-20160816; b=AkzYXM7iznEBwXOLIQOsSj2+xJCkrE3IJnl2nTs/kSCjBGnS+cZ6hPLWQR8yP8i1wT fdTd9fyx68WWM0Uxg8DZFLAX4CdqXs3vYPLRrxm/3L8G2LXtL+SH3MhWqBJadxPiWyjN dJGZeOEZxESGVttcLOAAo7vF75kd9e/awfZIZso7TtnGrbJuGHX38iaFys9zzAFRgdUs GCzVyuOwQFeHsEPC2W++KSiKdNUgv6YfnGZilDA6fqqKdwoOg9fgdk7MIgaAuyqkGs3v 2IKKLZ55rh4MeCeF732PylZFJfyfvBmEbHLBMd8G7aCQncTHjzDSxu1u8t6D83FU6zU2 ktEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=fpPZbt+b2KrElde1TvONusAOm5dwkze+rhasNAyZJU4=; b=gv/23Z/xdxn1rEduC9Notjq7V4DMnbA29IQDz3j1REYFv7vyjKaY7R8JU9T/0ZJvm3 G+RwmzjZY63+0Orxeka9iFfZ0IzZFTE2Mp6cg6yOgNcrgo4TTqn+JznDzwnvIJe5XvSV tASc8AFu8qjOLEIqTL27kklxzP/LzC6DD/KYOpaF1ZjokgkR6TWC24GXVpW3iYVF4JuO LbiSaX1uaA9tlLtzbDtijbGiLoqUWCLNTwq5TppTUPehNrZAG8ZFcUUKYeDaBO3eWre0 bu9Ew2ga8x8CGo6N8npe8i0yg633kw0jnuOJHQ8jsfYDa95kaVcdPRJqc2m4pOHbEanE dezQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Ajb3RTnq; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Ldx6gzh4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i20si9534930eja.694.2020.11.18.09.22.23; Wed, 18 Nov 2020 09:22:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Ajb3RTnq; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=Ldx6gzh4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728294AbgKRRTD (ORCPT + 99 others); Wed, 18 Nov 2020 12:19:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37042 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728144AbgKRRS1 (ORCPT ); Wed, 18 Nov 2020 12:18:27 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93943C0613D4; Wed, 18 Nov 2020 09:18:27 -0800 (PST) Date: Wed, 18 Nov 2020 17:18:25 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1605719906; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fpPZbt+b2KrElde1TvONusAOm5dwkze+rhasNAyZJU4=; b=Ajb3RTnqVHGZvX0KtVozMUXcVVRkxAk8bvgZ9OFEfZaQ1yRUfkjvX4+VUxdtDZwGt7LasL iQAjTh+d1/OXU2MP51gC3sT4/Av+5iQCSAwy6comaQdmtkXOgHZOE0CDHw/p2YutoKgvMI YKhTaxv0WLGWVWgkpSgflMpzGUHLXpxr3wtnB8mS8mUNVJUgR0fK9OSSVvmEUBvYvimNJo dLAL2o49n3xxe8avTU/IAqs9JA9viuVA+4Y+GRsg42MCaSnW85uBpkCwLtQ02Hmj5bKJri t2iL3eukKtZm7iYIEsNaK4nTXjL4tFhyqowqVlFAnJBVIZelXYHNRx6OxykcAw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1605719906; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fpPZbt+b2KrElde1TvONusAOm5dwkze+rhasNAyZJU4=; b=Ldx6gzh45ss5cfrX7UzTWmTzX5g7soBRV6VepZUbbV5ai08RXsLyKBCH7yI4vK08FULOEa AxOAJrFG9+Boa1Aw== From: "tip-bot2 for Jarkko Sakkinen" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/sgx] x86/sgx: Add SGX page allocator functions Cc: Sean Christopherson , Jarkko Sakkinen , Borislav Petkov , Jethro Beekman , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20201112220135.165028-10-jarkko@kernel.org> References: <20201112220135.165028-10-jarkko@kernel.org> MIME-Version: 1.0 Message-ID: <160571990531.11244.14914038894848073741.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/sgx branch of tip: Commit-ID: d2285493bef310b66b56dfe4eb75c1e2f431ea5c Gitweb: https://git.kernel.org/tip/d2285493bef310b66b56dfe4eb75c1e2f431ea5c Author: Jarkko Sakkinen AuthorDate: Fri, 13 Nov 2020 00:01:20 +02:00 Committer: Borislav Petkov CommitterDate: Tue, 17 Nov 2020 14:36:13 +01:00 x86/sgx: Add SGX page allocator functions 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. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Jarkko Sakkinen Signed-off-by: Borislav Petkov Acked-by: Jethro Beekman Link: https://lkml.kernel.org/r/20201112220135.165028-10-jarkko@kernel.org --- arch/x86/kernel/cpu/sgx/main.c | 65 +++++++++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/sgx.h | 3 ++- 2 files changed, 68 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 187a237..2e53afc 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -85,6 +85,71 @@ 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; + + spin_lock(§ion->lock); + + if (list_empty(§ion->page_list)) { + spin_unlock(§ion->lock); + return NULL; + } + + page = list_first_entry(§ion->page_list, struct sgx_epc_page, list); + list_del_init(&page->list); + + spin_unlock(§ion->lock); + 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]; + + page = __sgx_alloc_epc_page_from_section(section); + if (page) + return page; + } + + return ERR_PTR(-ENOMEM); +} + +/** + * sgx_free_epc_page() - Free an EPC page + * @page: an EPC page + * + * Call EREMOVE for an EPC page and insert it back to the list of free pages. + */ +void sgx_free_epc_page(struct sgx_epc_page *page) +{ + struct sgx_epc_section *section = &sgx_epc_sections[page->section]; + int ret; + + ret = __eremove(sgx_get_epc_virt_addr(page)); + if (WARN_ONCE(ret, "EREMOVE returned %d (0x%x)", ret, ret)) + return; + + spin_lock(§ion->lock); + list_add_tail(&page->list, §ion->page_list); + spin_unlock(§ion->lock); +} + static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size, unsigned long index, struct sgx_epc_section *section) diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index 02afa84..bd9dcb1 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -57,4 +57,7 @@ static inline void *sgx_get_epc_virt_addr(struct sgx_epc_page *page) return section->virt_addr + index * PAGE_SIZE; } +struct sgx_epc_page *__sgx_alloc_epc_page(void); +void sgx_free_epc_page(struct sgx_epc_page *page); + #endif /* _X86_SGX_H */