Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp720714img; Wed, 20 Mar 2019 09:26:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqwmj/05v1YominQTvdk8IW2ss5P326DkoNmcyWvnXQdJqJfEtIEBGtG8UIEpzNWoARaGmDH X-Received: by 2002:a62:69c3:: with SMTP id e186mr8284348pfc.169.1553099209648; Wed, 20 Mar 2019 09:26:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553099209; cv=none; d=google.com; s=arc-20160816; b=V+Ri1SGB1ReCPbkN3cuznUu5S3Agnyv/KmjJw12UE0E5sz96DkyNeaKinUjOhCttxO GSKKqURpSTdx9I0D3Ys+AFFqRfGe8jOn0XL6ZDRDNtlM5P/xgkfSm0okYnBGnul2xkHl ZAHGDl2rYVELnVrWjQmbZGQx/JKmkeqtAv5X4skaaxEIChzk13NJkMGSqoPEmN25eyk5 O1VtizX7GQtCs3pCh5JxgDOdW8K325o/1KTvJ0W25cr8LR58z7T0JpxTJWJXPmnLXQVs T2XeFL8SyS4O9kni9Cs12swvQB+1LkwtvQSkk0UbnH1k7uIRTMHB/yZBvQSRieQjMDqQ Sx8Q== 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=O89VBu4qT7ZNhVVlLIvqgVGauIMjE2RPoy5JeyuIWOA=; b=N9bj+fEn3xlT3GhcBFinVEqBNtPr66cb5olZDLS37HjfsURX1KK6MOtG+I62t7E4nx +et1g4jYAXYBibIramHAl2YiYRvSv3ZpZsRLJ313ovjJ39h6eqwTM3qdHoS3rMF5oKL5 MWMHge+VgbiTKZxSFRKUl54pkntGAS9qOEZiTpXI+qn4l7J3W9Tpoukr+Cdrrsoiifry Pcmpdpj1Skhwrnbw1gW2lkjtF2DmPlSEY9MTgutCIFezVRYtKL1we8SJYje++yjzmWDQ +/iaRBXYqn/DUF4IWqegTHiC9iFYy85Z/EelFdpyrdLtWIVI4X8I+ooxJZs500zwrhGz eaag== 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 y7si1874072pfe.248.2019.03.20.09.26.34; Wed, 20 Mar 2019 09:26:49 -0700 (PDT) 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 S1727723AbfCTQYN (ORCPT + 99 others); Wed, 20 Mar 2019 12:24:13 -0400 Received: from mga01.intel.com ([192.55.52.88]:41432 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726860AbfCTQYN (ORCPT ); Wed, 20 Mar 2019 12:24:13 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Mar 2019 09:24:12 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,249,1549958400"; d="scan'208";a="135715279" Received: from sorenthe-mobl1.ger.corp.intel.com (HELO localhost) ([10.249.254.203]) by orsmga003.jf.intel.com with ESMTP; 20 Mar 2019 09:24:00 -0700 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, serge.ayoun@intel.com, shay.katz-zamir@intel.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, Jarkko Sakkinen Subject: [PATCH v19,RESEND 14/27] x86/sgx: Add functions to allocate and free EPC pages Date: Wed, 20 Mar 2019 18:21:06 +0200 Message-Id: <20190320162119.4469-15-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190320162119.4469-1-jarkko.sakkinen@linux.intel.com> References: <20190320162119.4469-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At this time there is no support for reclaiming pages prior to the owner explicitly freeing the page. As for freeing pages, because freeing a page is expected to succeed in the vast majority of cases and because most call sites will not be equipped to handle failure, provide a variant for freeing a page that warns on failure, e.g. due to ENCLS[EREMOVE] failing. Signed-off-by: Jarkko Sakkinen Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/kernel/cpu/sgx/main.c | 89 ++++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 4 ++ 2 files changed, 93 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 18ce4acdd7ef..f88c8d86c2a8 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -9,6 +9,7 @@ #include #include #include "arch.h" +#include "encls.h" #include "sgx.h" struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; @@ -23,6 +24,94 @@ static void sgx_section_put_page(struct sgx_epc_section *section, section->free_cnt++; } +static struct sgx_epc_page *sgx_section_get_page( + struct sgx_epc_section *section) +{ + struct sgx_epc_page *page; + + if (!section->free_cnt) + return NULL; + + page = list_first_entry(§ion->page_list, + struct sgx_epc_page, list); + list_del_init(&page->list); + section->free_cnt--; + return page; +} + +/** + * sgx_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_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_section_get_page(section); + spin_unlock(§ion->lock); + + if (page) + return page; + } + + return ERR_PTR(-ENOMEM); +} +EXPORT_SYMBOL_GPL(sgx_alloc_page); + +/** + * __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. + * + * Return: + * 0 on success + * SGX error code if EREMOVE fails + */ +int __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 (ret) + return ret; + + spin_lock(§ion->lock); + sgx_section_put_page(section, page); + spin_unlock(§ion->lock); + + return 0; +} +EXPORT_SYMBOL_GPL(__sgx_free_page); + +/** + * sgx_free_page - Free an EPC page and WARN on failure + * @page: pointer to a previously allocated EPC page + * + * EREMOVE an EPC page and insert it back to the list of free pages, and WARN + * if EREMOVE fails. For use when the call site cannot (or chooses not to) + * handle failure, i.e. the page is leaked on failure. + */ +void sgx_free_page(struct sgx_epc_page *page) +{ + int ret; + + ret = __sgx_free_page(page); + WARN(ret > 0, "sgx: EREMOVE returned %d (0x%x)", ret, ret); +} +EXPORT_SYMBOL_GPL(sgx_free_page); + static __init void 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 228e3dae360d..86b4c74b6089 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -59,4 +59,8 @@ static inline void *sgx_epc_addr(struct sgx_epc_page *page) return section->va + (page->desc & PAGE_MASK) - section->pa; } +struct sgx_epc_page *sgx_alloc_page(void); +int __sgx_free_page(struct sgx_epc_page *page); +void sgx_free_page(struct sgx_epc_page *page); + #endif /* _X86_SGX_H */ -- 2.19.1