From patchwork Thu Apr 20 16:16:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 9690893 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2E37F60383 for ; Thu, 20 Apr 2017 16:16:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 108D22849C for ; Thu, 20 Apr 2017 16:16:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 056CA28497; Thu, 20 Apr 2017 16:16:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from ml01.01.org (ml01.01.org [198.145.21.10]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4F5D92849C for ; Thu, 20 Apr 2017 16:16:26 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 79E1D20D7B8F3; Thu, 20 Apr 2017 09:16:25 -0700 (PDT) X-Original-To: intel-sgx-kernel-dev@lists.01.org Delivered-To: intel-sgx-kernel-dev@lists.01.org Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 122F921954079 for ; Thu, 20 Apr 2017 09:16:24 -0700 (PDT) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga105.fm.intel.com with ESMTP; 20 Apr 2017 09:16:23 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.37,225,1488873600"; d="scan'208";a="251515788" Received: from sjchrist-ts.jf.intel.com ([10.54.74.20]) by fmsmga004.fm.intel.com with ESMTP; 20 Apr 2017 09:16:23 -0700 From: Sean Christopherson To: intel-sgx-kernel-dev@lists.01.org Date: Thu, 20 Apr 2017 09:16:15 -0700 Message-Id: <1492704977-26510-4-git-send-email-sean.j.christopherson@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1492704977-26510-1-git-send-email-sean.j.christopherson@intel.com> References: <1492704977-26510-1-git-send-email-sean.j.christopherson@intel.com> Subject: [intel-sgx-kernel-dev] [PATCH v2 3/4] intel_sgx: combine epc_page and va_page into union X-BeenThere: intel-sgx-kernel-dev@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: "Project: Intel® Software Guard Extensions for Linux*: https://01.org/intel-software-guard-extensions" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: intel-sgx-kernel-dev-bounces@lists.01.org Sender: "intel-sgx-kernel-dev" X-Virus-Scanned: ClamAV using ClamSMTP Combine sgx_encl_page's epc_page and va_page into a union. On-demand VA slot/page allocations guarantees only one of epc_page or va_page will be valid at any given time. Add a new flag, SGX_ENCL_PAGE_EPC_VALID, to track if an encl page has been loaded into the EPC. If SGX_ENCL_PAGE_EPC_VALID is set, then the epc_page member of the epc_page/va_page union is valid, otherwise va_page *may* be valid, e.g. neither is valid if EADD has not been completed. Move the functionality of sgx_evict_page into the success path of EWB, as the EPC page must be freed before updating the encl_page's va_page. Signed-off-by: Sean Christopherson --- drivers/platform/x86/intel_sgx/sgx.h | 9 ++++++--- drivers/platform/x86/intel_sgx/sgx_ioctl.c | 2 ++ drivers/platform/x86/intel_sgx/sgx_page_cache.c | 11 ++++++----- drivers/platform/x86/intel_sgx/sgx_util.c | 11 +++++------ 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/drivers/platform/x86/intel_sgx/sgx.h b/drivers/platform/x86/intel_sgx/sgx.h index 958e37d..bd22554 100644 --- a/drivers/platform/x86/intel_sgx/sgx.h +++ b/drivers/platform/x86/intel_sgx/sgx.h @@ -91,15 +91,18 @@ static inline void sgx_free_va_slot(struct sgx_va_page *page, enum sgx_encl_page_flags { SGX_ENCL_PAGE_TCS = BIT(0), SGX_ENCL_PAGE_RESERVED = BIT(1), + SGX_ENCL_PAGE_EPC_VALID = BIT(2), }; struct sgx_encl_page { unsigned long addr; unsigned int flags; - struct sgx_epc_page *epc_page; - struct list_head load_list; - struct sgx_va_page *va_page; unsigned int va_offset; + union { + struct sgx_epc_page *epc_page; + struct sgx_va_page *va_page; + }; + struct list_head load_list; }; struct sgx_tgid_ctx { diff --git a/drivers/platform/x86/intel_sgx/sgx_ioctl.c b/drivers/platform/x86/intel_sgx/sgx_ioctl.c index af80571..6719e56 100644 --- a/drivers/platform/x86/intel_sgx/sgx_ioctl.c +++ b/drivers/platform/x86/intel_sgx/sgx_ioctl.c @@ -267,6 +267,7 @@ static bool sgx_process_add_page_req(struct sgx_add_page_req *req) goto out; } + encl_page->flags |= SGX_ENCL_PAGE_EPC_VALID; encl_page->epc_page = epc_page; sgx_test_and_clear_young(encl_page, encl); list_add_tail(&encl_page->load_list, &encl->load_list); @@ -558,6 +559,7 @@ static long sgx_ioc_enclave_create(struct file *filep, unsigned int cmd, goto out; } + encl->secs_page.flags |= SGX_ENCL_PAGE_EPC_VALID; encl->secs_page.epc_page = secs_epc; createp->src = (unsigned long)encl->base; diff --git a/drivers/platform/x86/intel_sgx/sgx_page_cache.c b/drivers/platform/x86/intel_sgx/sgx_page_cache.c index 19e6617..f3326e0 100644 --- a/drivers/platform/x86/intel_sgx/sgx_page_cache.c +++ b/drivers/platform/x86/intel_sgx/sgx_page_cache.c @@ -330,10 +330,14 @@ static int __sgx_ewb(struct sgx_encl *encl, sgx_put_page(va); sgx_put_page(epc); - if (ret == SGX_SUCCESS) + if (ret == SGX_SUCCESS) { + encl_page->flags &= ~(SGX_ENCL_PAGE_RESERVED | SGX_ENCL_PAGE_EPC_VALID); + sgx_free_page(encl_page->epc_page, encl); encl_page->va_page = va_page; - else + } + else { sgx_free_va_slot(va_page, encl_page->va_offset); + } out_pcmd: sgx_put_backing(pcmd, true); @@ -368,9 +372,6 @@ static void sgx_evict_page(struct sgx_encl_page *entry, struct sgx_encl *encl) { sgx_ewb(encl, entry); - sgx_free_page(entry->epc_page, encl); - entry->epc_page = NULL; - entry->flags &= ~SGX_ENCL_PAGE_RESERVED; } static void sgx_write_pages(struct sgx_encl *encl, struct list_head *src) diff --git a/drivers/platform/x86/intel_sgx/sgx_util.c b/drivers/platform/x86/intel_sgx/sgx_util.c index 3aacd5a..2ff9f85 100644 --- a/drivers/platform/x86/intel_sgx/sgx_util.c +++ b/drivers/platform/x86/intel_sgx/sgx_util.c @@ -232,6 +232,7 @@ static int sgx_eldu(struct sgx_encl *encl, ret = -EFAULT; } else { sgx_free_va_slot(encl_page->va_page, encl_page->va_offset); + encl_page->flags |= SGX_ENCL_PAGE_EPC_VALID; encl_page->epc_page = epc_page; } @@ -293,7 +294,7 @@ static struct sgx_encl_page *sgx_do_fault(struct vm_area_struct *vma, } /* Legal race condition, page is already faulted. */ - if (entry->epc_page) { + if (entry->flags & SGX_ENCL_PAGE_EPC_VALID) { if (reserve) entry->flags |= SGX_ENCL_PAGE_RESERVED; goto out; @@ -307,7 +308,7 @@ static struct sgx_encl_page *sgx_do_fault(struct vm_area_struct *vma, } /* If SECS is evicted then reload it first */ - if (!encl->secs_page.epc_page) { + if (!(encl->secs_page.flags & SGX_ENCL_PAGE_EPC_VALID)) { secs_epc_page = sgx_alloc_page(SGX_ALLOC_ATOMIC); if (IS_ERR(secs_epc_page)) { rc = PTR_ERR(secs_epc_page); @@ -398,7 +399,7 @@ void sgx_encl_release(struct kref *ref) radix_tree_for_each_slot(slot, &encl->page_tree, &iter, 0) { entry = *slot; - if (entry->epc_page) { + if (entry->flags & SGX_ENCL_PAGE_EPC_VALID) { list_del(&entry->load_list); sgx_free_page(entry->epc_page, encl); } @@ -414,11 +415,9 @@ void sgx_encl_release(struct kref *ref) kfree(va_page); } - if (encl->secs_page.epc_page) + if (encl->secs_page.flags & SGX_ENCL_PAGE_EPC_VALID) sgx_free_page(encl->secs_page.epc_page, encl); - encl->secs_page.epc_page = NULL; - if (encl->tgid_ctx) kref_put(&encl->tgid_ctx->refcount, sgx_tgid_ctx_release);