Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3028860ybv; Sun, 9 Feb 2020 13:27:46 -0800 (PST) X-Google-Smtp-Source: APXvYqwOP5AqA4dmFUxInT+SKCCFQJ1gJcH7grdy2sWw6hGQHdB2U9y2jvu0B8Gu0ZSm2ui+EiFZ X-Received: by 2002:a05:6808:2cd:: with SMTP id a13mr8916643oid.82.1581283666271; Sun, 09 Feb 2020 13:27:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581283666; cv=none; d=google.com; s=arc-20160816; b=XMxwcRkPEzv6p+Gj4pUrHHnQEM/sCSsSXKmCgS/ZerA/XxiNMTjD66SUBF3V2HrDBS 7+z12cq9ejLy3Ht5KCBnOF8pIrG0fhNd85KBFfenFgGudULFT2LjwSTmQ00PL5HLvQPe zX839x+RBgXPx7clcWJWdLXPUMy24zrcwW5y8/qIT1tin1z0zHGMhZ6R5Z2reg71Qron ah/URgnl5xguukWBY2IeTxDbL/Y71zdgJ9s40X32reJ2aEnjxMovAYIfuMV7+xk0o8T8 a6qiX12PUENEQsUSwouXZKlwCZc90uULGv4tr4f8lwXJaS+KNhXBNL+gApGko6M08Nna 14AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=6Ee9gKxhdwdqruSKoc9B9my8wg28fXo83d+JvVXi3Og=; b=ctEiKZb42/JOOpjDLelifa+NLqSvMW9ngu82xGgX3iFkExXMTARgJRgbP/am56Fmeo gsOjjvpiU0ARprQxvTZP6WhpDDrN5ztM0HrV38ZRPL68sb0ovYNYn/in620xxDmuExGq oqlCSN1NaRNYowlK99hE5Z2DgL/6H5Htw7D7WiygFGfVN9bPGArZw7p4ZF8TMBEanFrE 6T9INBLLB606vY47IFaP7ic9zTFZBXKc7eTaHDCm6ElCTfgrITKEbhC/TLuwvKLw+GVd FBcN3x1p0ZR76FoZyAqJKe0BhNTwnJyAybdmO1c68HE/p76HwbNFPunnIbecRwJexkye iz4g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h28si3922155otg.63.2020.02.09.13.27.35; Sun, 09 Feb 2020 13:27:46 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728056AbgBIV1F (ORCPT + 99 others); Sun, 9 Feb 2020 16:27:05 -0500 Received: from mga06.intel.com ([134.134.136.31]:33626 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727416AbgBIV1F (ORCPT ); Sun, 9 Feb 2020 16:27:05 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 Feb 2020 13:27:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,422,1574150400"; d="scan'208";a="405398796" Received: from jradtke-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.22.75]) by orsmga005.jf.intel.com with ESMTP; 09 Feb 2020 13:26:59 -0800 From: Jarkko Sakkinen To: linux-kernel@vger.kernel.org, x86@kernel.org, linux-sgx@vger.kernel.org Cc: akpm@linux-foundation.org, dave.hansen@intel.com, sean.j.christopherson@intel.com, nhorman@redhat.com, npmccallum@redhat.com, haitao.huang@intel.com, andriy.shevchenko@linux.intel.com, tglx@linutronix.de, kai.svahn@intel.com, bp@alien8.de, josh@joshtriplett.org, luto@kernel.org, kai.huang@intel.com, rientjes@google.com, cedric.xing@intel.com, puiterwijk@redhat.com, Jarkko Sakkinen Subject: [PATCH v26 08/22] x86/sgx: Add functions to allocate and free EPC pages Date: Sun, 9 Feb 2020 23:25:55 +0200 Message-Id: <20200209212609.7928-9-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200209212609.7928-1-jarkko.sakkinen@linux.intel.com> References: <20200209212609.7928-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add functions for allocating page from Enclave Page Cache (EPC). A page is allocated by going through the EPC sections and returning the first free page. When a page is freed, it might have a valid state, which means that the callee has assigned it to an enclave, which are protected memory ares used to run code protected from outside access. The page is returned back to the invalid state with ENCLS[EREMOVE] [1]. [1] Intel SDM: 40.3 INTELĀ® SGX SYSTEM LEAF FUNCTION REFERENCE Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/main.c | 60 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 3 ++ 2 files changed, 63 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 38424c1e8341..60d82e7537c8 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -13,6 +13,66 @@ struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; int sgx_nr_epc_sections; +static struct sgx_epc_page *__sgx_try_alloc_page(struct sgx_epc_section *section) +{ + struct sgx_epc_page *page; + + if (list_empty(§ion->page_list)) + return NULL; + + page = list_first_entry(§ion->page_list, struct sgx_epc_page, list); + list_del_init(&page->list); + return page; +} + +/** + * sgx_try_alloc_page() - Allocate an EPC page + * + * Try to grab a page from the free EPC page list. + * + * Return: + * a pointer to a &struct sgx_epc_page instance, + * -errno on error + */ +struct sgx_epc_page *sgx_try_alloc_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(§ion->lock); + page = __sgx_try_alloc_page(section); + spin_unlock(§ion->lock); + + if (page) + return page; + } + + return ERR_PTR(-ENOMEM); +} + +/** + * sgx_free_page() - Free an EPC page + * @page: pointer a previously allocated EPC page + * + * EREMOVE an EPC page and insert it back to the list of free pages. + */ +void sgx_free_page(struct sgx_epc_page *page) +{ + struct sgx_epc_section *section = sgx_epc_section(page); + int ret; + + ret = __eremove(sgx_epc_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 void __init sgx_free_epc_section(struct sgx_epc_section *section) { struct sgx_epc_page *page; diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index aad30980be32..aa85f85412d8 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -67,4 +67,7 @@ extern struct task_struct *ksgxswapd_tsk; bool __init sgx_page_reclaimer_init(void); +struct sgx_epc_page *sgx_try_alloc_page(void); +void sgx_free_page(struct sgx_epc_page *page); + #endif /* _X86_SGX_H */ -- 2.20.1