Received: by 2002:ac0:98c7:0:0:0:0:0 with SMTP id g7-v6csp2692168imd; Fri, 2 Nov 2018 16:18:26 -0700 (PDT) X-Google-Smtp-Source: AJdET5fpIz5Lti5CDvZLEIXxW/wZVNIK9sE5V9f2H6uIO505a9IaDS0iTnxjKab9TiMMme13qXgB X-Received: by 2002:a63:8043:: with SMTP id j64mr12739165pgd.405.1541200706371; Fri, 02 Nov 2018 16:18:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1541200706; cv=none; d=google.com; s=arc-20160816; b=EgNmGpHjccsjhXzoPo2eeU0G8NTQ3+h05MqrxJuB7jhqjlMWmco+tw4Yb30pD9MB6r 1wDZxxLcaDobIW84q7wDsgAA3jCuQnKu4EZNz22WqXGJqogkfCqo9ocp5MLNzhg+BD58 X2wBj4WHn1sJS/QnNIqudQ/Xs4owH8oIdH+GF9LadI9UM5m94fjE5IO9vu/9GYAb/UGW wqHGoIU5tWfterxiAdk5FrJfzOeiZnfqF4wtTvjh4XqVjk/bqrIxstk1JQuhfLQVov2N hBNo/28LYDXFJ+pAJxqofppx4FeP0hcGH9R6l/c6xtKWsHaLSMzoorILDqmPexrXBEPe H4BQ== 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=pWTDaIAy6WF6Tc1MK5yJMbC8OZmcmMFst6dAiEpwKZ8=; b=rBSfbFzvGsXsFpfvltlnJTmTnQMb7kjKdjQAOpAUX4TaD1Aym9kOIa6vs2QJN15X3f RGCqfbDsegKViYLxFUuSjodd6MwZONbASBpQ70J3drADmGPqAiXrlBKfah0LsFEcv77K Bkoq9nn2GyeMtdhdCYSkuSGmmVXzq26btdHUMFQsM3RW0ROq00ZauNi9F5oJ77JKRLrL IcsAzoLIiuLNfZHq1063cMeJ3Nf2KS1If6rGzSGi5r5NzbFN9wsOQP0gVlXy3h82oai4 S3am6B1ZNGAqoN+nfgRyY8UcjNz8XrdBMWNloUBH3ynlpjo2qiEN+tHXBZe4zexxW4Gd 1FPA== 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 s1-v6si24952525pfb.148.2018.11.02.16.18.11; Fri, 02 Nov 2018 16:18:26 -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 S1728745AbeKCI0o (ORCPT + 99 others); Sat, 3 Nov 2018 04:26:44 -0400 Received: from mga02.intel.com ([134.134.136.20]:11781 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726705AbeKCI0n (ORCPT ); Sat, 3 Nov 2018 04:26:43 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Nov 2018 16:17:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,457,1534834800"; d="scan'208";a="270988132" Received: from btyborox-mobl.ger.corp.intel.com (HELO localhost) ([10.249.254.138]) by orsmga005.jf.intel.com with ESMTP; 02 Nov 2018 16:17:26 -0700 From: Jarkko Sakkinen To: x86@kernel.org, platform-driver-x86@vger.kernel.org, linux-sgx@vger.kernel.org Cc: 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, mark.shanahan@intel.com, andriy.shevchenko@linux.intel.com, Jarkko Sakkinen , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Suresh Siddha , linux-kernel@vger.kernel.org (open list:X86 ARCHITECTURE (32-BIT AND 64-BIT)) Subject: [PATCH v15 17/23] x86/sgx: Add functions to allocate and free EPC pages Date: Sat, 3 Nov 2018 01:11:16 +0200 Message-Id: <20181102231320.29164-18-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181102231320.29164-1-jarkko.sakkinen@linux.intel.com> References: <20181102231320.29164-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. Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson Signed-off-by: Jarkko Sakkinen --- arch/x86/include/asm/sgx.h | 4 ++ arch/x86/kernel/cpu/intel_sgx.c | 77 +++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h index aa51165eb3a8..4e39bcfad018 100644 --- a/arch/x86/include/asm/sgx.h +++ b/arch/x86/include/asm/sgx.h @@ -309,4 +309,8 @@ static inline int __emodt(struct sgx_secinfo *secinfo, void __iomem *addr) return __encls_ret_2(SGX_EMODT, secinfo, addr); } +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 /* _ASM_X86_SGX_H */ diff --git a/arch/x86/kernel/cpu/intel_sgx.c b/arch/x86/kernel/cpu/intel_sgx.c index b86aa4111592..b06709a9ea65 100644 --- a/arch/x86/kernel/cpu/intel_sgx.c +++ b/arch/x86/kernel/cpu/intel_sgx.c @@ -15,6 +15,83 @@ EXPORT_SYMBOL_GPL(sgx_epc_sections); static int sgx_nr_epc_sections; +/** + * 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); + if (section->free_cnt) { + page = section->pages[section->free_cnt - 1]; + section->free_cnt--; + } + 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); + section->pages[section->free_cnt++] = 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) { int i; -- 2.19.1