Received: by 2002:a4a:301c:0:0:0:0:0 with SMTP id q28-v6csp904732oof; Tue, 25 Sep 2018 06:14:28 -0700 (PDT) X-Google-Smtp-Source: ACcGV61bJz9lOytU0nf9rwcLCELXUvl9C3kXV80VWKrxObHRKh8yxAMJpVriKSOggMs8VPEiGVPG X-Received: by 2002:a63:e206:: with SMTP id q6-v6mr1009097pgh.223.1537881268870; Tue, 25 Sep 2018 06:14:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537881268; cv=none; d=google.com; s=arc-20160816; b=N89wupOQ5uKWtukGFUgWgq0HUV3i6JR2VDq+XpV0IzE69hh1UEg+VeGguOHKsLdw5O Oe/i/qgwiKLHO539124UfghCxLltqHZIZV9ZeOoKNreBM/xaVzyH7ZevLkIqXLJFWYVr z/MXx8ivhS1WaPqmbSchV+UOqnCWd4aoSpr2xGt/PB6niKbTBO+Sc2a1riYxAV2gUiZl xps/SfKm43CTO4aKnYMUHWqQAbB5z1LdVWoI+e7ERY6uR+pA2GjCJD/SAhnWx9rymP1a ueZrXRL0ezkbm171TVZwSV2xH8B4G4W0pA3fdPWn3Yrv1IHi6IU/OX/SL9qghVpXLThk DQPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=DkShB+N0dIkIfg6ZzJuiMenL9gn7zxrmyQMSV7+x7bg=; b=C3PMr21/UiehrZpCY0p1Ste9WzDsnZDqu91RC5Ye+tpphK9PH46S9L/hCIJrMQB96H 2Toq/rgmSZGcGmhtAVnEijH1bLRYv9fsWJauZUIgGYj1xbHPbHILziELSshqHjxM1sGC LGQqOs672fEnBQ1qV1GuNVnfBAOWL/vdFyuCnK7qfB5wNopjPFYgEB9xQ2iHcrewOISl UgCCHSJLYRUv6zKU905L5ODwggGvCZbgAPiFqDeEoy3AJwnMYlKNkGMizd1Xa8RfvGcA V+IMKUupIiTtJUFj1b7J3/vGlSKaOTJbQN8/FWRvaaRGvZ8W99drt3QIBaY4TOFUqiF2 UQtA== 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 n15-v6si2429904pgc.143.2018.09.25.06.14.12; Tue, 25 Sep 2018 06:14:28 -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 S1729343AbeIYTVA (ORCPT + 99 others); Tue, 25 Sep 2018 15:21:00 -0400 Received: from mga11.intel.com ([192.55.52.93]:22699 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727165AbeIYTVA (ORCPT ); Tue, 25 Sep 2018 15:21:00 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2018 06:13:29 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.54,302,1534834800"; d="scan'208";a="93546957" Received: from thomasvo-mobl2.ger.corp.intel.com (HELO localhost) ([10.252.53.212]) by orsmga001.jf.intel.com with ESMTP; 25 Sep 2018 06:11:31 -0700 From: Jarkko Sakkinen To: x86@kernel.org, platform-driver-x86@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, linux-sgx@vger.kernel.org, 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 v14 13/19] x86/sgx: Enclave Page Cache (EPC) memory manager Date: Tue, 25 Sep 2018 16:06:50 +0300 Message-Id: <20180925130845.9962-14-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180925130845.9962-1-jarkko.sakkinen@linux.intel.com> References: <20180925130845.9962-1-jarkko.sakkinen@linux.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a Enclave Page Cache (EPC) memory manager that can be used to allocate and free EPC pages. Signed-off-by: Jarkko Sakkinen Co-developed-by: Sean Christopherson Signed-off-by: Sean Christopherson --- arch/x86/include/asm/sgx.h | 7 ++- arch/x86/kernel/cpu/intel_sgx.c | 86 +++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h index 468e609147cd..6ea5a6dbd36d 100644 --- a/arch/x86/include/asm/sgx.h +++ b/arch/x86/include/asm/sgx.h @@ -14,6 +14,7 @@ struct sgx_epc_page { unsigned long desc; + void *owner; struct list_head list; }; @@ -65,7 +66,7 @@ static inline void __iomem *sgx_epc_addr(struct sgx_epc_page *page) return section->va + (page->desc & PAGE_MASK) - section->pa; } -/** +/* * ENCLS_FAULT_FLAG - flag signifying an ENCLS return code is a trapnr * * ENCLS has its own (positive value) error codes and also generates @@ -309,4 +310,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 *owner, bool reclaim); +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 b24d6287442d..ef30d6730859 100644 --- a/arch/x86/kernel/cpu/intel_sgx.c +++ b/arch/x86/kernel/cpu/intel_sgx.c @@ -19,6 +19,92 @@ EXPORT_SYMBOL_GPL(sgx_epc_sections); static int sgx_nr_epc_sections; +/** + * sgx_alloc_page - Allocate an EPC page + * @owner: the owner of the EPC page + * @reclaim: wait and reclaim pages up until we get one + * + * Try to grab a page from the free EPC page list. If there is a free page + * available, it is returned to the caller. The @reclaim parameter hints + * the EPC memory manager to swap pages when required. + * + * Return: + * a pointer to a &struct sgx_epc_page instace, + * -errno on error + */ +struct sgx_epc_page *sgx_alloc_page(void *owner, bool reclaim) +{ + 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) { + page->owner = owner; + return page; + } + } + + return NULL; +} +EXPORT_SYMBOL_GPL(sgx_alloc_page); + +/** + * __sgx_free_page - Free an EPC page + * @page: pointer a previously allocated EPC page + * + * Invaldate an EPC page and insert it back to the list of free pages. + * + * Return: + * 0 on success + * -EBUSY if the page cannot be removed from the active list + * 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. + * If the page is reclaimable, deletes it from the active page list. + * WARN on any failure. 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: cannot free page, reclaim in-progress"); + 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.17.1