From patchwork Sun Dec 4 18:40:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 9460097 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 C29ED60231 for ; Sun, 4 Dec 2016 18:41:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B89C62465D for ; Sun, 4 Dec 2016 18:41:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AD91624B91; Sun, 4 Dec 2016 18:41:05 +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 671A32465D for ; Sun, 4 Dec 2016 18:41:05 +0000 (UTC) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 7D79181FC3 for ; Sun, 4 Dec 2016 10:41:05 -0800 (PST) X-Original-To: intel-sgx-kernel-dev@lists.01.org Delivered-To: intel-sgx-kernel-dev@lists.01.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (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 E5F8881FC3 for ; Sun, 4 Dec 2016 10:41:04 -0800 (PST) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga101.fm.intel.com with ESMTP; 04 Dec 2016 10:41:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,743,1477983600"; d="scan'208";a="38852602" Received: from jcortizr-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.2.177]) by fmsmga006.fm.intel.com with ESMTP; 04 Dec 2016 10:41:02 -0800 From: Jarkko Sakkinen To: intel-sgx-kernel-dev@lists.01.org Date: Sun, 4 Dec 2016 20:40:37 +0200 Message-Id: <20161204184044.21031-2-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20161204184044.21031-1-jarkko.sakkinen@linux.intel.com> References: <20161204184044.21031-1-jarkko.sakkinen@linux.intel.com> Subject: [intel-sgx-kernel-dev] [PATCH v6 1/8] intel_sgx: fallback more gracefully from EWB failure X-BeenThere: intel-sgx-kernel-dev@lists.01.org X-Mailman-Version: 2.1.21 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 Fallback from EWB failure by killing the enclave by zeroing TCS PTEs and kicking out threads instead of crashing the driver with BUG_ON(). Signed-off-by: Jarkko Sakkinen --- drivers/platform/x86/intel_sgx_page_cache.c | 57 ++++++++++++++++++----------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/drivers/platform/x86/intel_sgx_page_cache.c b/drivers/platform/x86/intel_sgx_page_cache.c index 8b1cc82..0f63060 100644 --- a/drivers/platform/x86/intel_sgx_page_cache.c +++ b/drivers/platform/x86/intel_sgx_page_cache.c @@ -195,9 +195,9 @@ static void sgx_etrack(struct sgx_epc_page *epc_page) sgx_put_epc_page(epc); } -static int sgx_ewb(struct sgx_encl *encl, - struct sgx_encl_page *encl_page, - struct page *backing) +static int __sgx_ewb(struct sgx_encl *encl, + struct sgx_encl_page *encl_page, + struct page *backing) { struct sgx_page_info pginfo; void *epc; @@ -218,12 +218,31 @@ static int sgx_ewb(struct sgx_encl *encl, sgx_put_epc_page(epc); kunmap_atomic((void *)(unsigned long)pginfo.srcpge); - if (ret != 0 && ret != SGX_NOT_TRACKED) - sgx_err(encl, "EWB returned %d\n", ret); - return ret; } +static bool sgx_ewb(struct sgx_encl *encl, + struct sgx_encl_page *entry, + struct page *backing) +{ + int ret = __sgx_ewb(encl, entry, backing); + + /* Only kick out threads with an IPI if needed. */ + if (ret == SGX_NOT_TRACKED) { + smp_call_function(sgx_ipi_cb, NULL, 1); + ret = __sgx_ewb(encl, entry, backing); + } + + if (ret) { + /* Make enclave inaccessible. */ + sgx_invalidate(encl); + smp_call_function(sgx_ipi_cb, NULL, 1); + return false; + } + + return true; +} + void sgx_free_encl_page(struct sgx_encl_page *entry, struct sgx_encl *encl, unsigned int flags) @@ -239,9 +258,9 @@ static void sgx_write_pages(struct sgx_encl *encl, struct list_head *src) struct sgx_encl_page *tmp; struct page *pages[SGX_NR_SWAP_CLUSTER_MAX + 1]; struct vm_area_struct *evma; + unsigned int free_flags; int cnt = 0; int i = 0; - int ret; if (list_empty(src)) return; @@ -304,20 +323,16 @@ static void sgx_write_pages(struct sgx_encl *encl, struct list_head *src) load_list); list_del(&entry->load_list); + free_flags = 0; + evma = sgx_find_vma(encl, entry->addr); if (evma) { - ret = sgx_ewb(encl, entry, pages[i]); - BUG_ON(ret != 0 && ret != SGX_NOT_TRACKED); - /* Only kick out threads with an IPI if needed. */ - if (ret) { - smp_call_function(sgx_ipi_cb, NULL, 1); - BUG_ON(sgx_ewb(encl, entry, pages[i])); - } + if (sgx_ewb(encl, entry, pages[i])) + free_flags = SGX_FREE_SKIP_EREMOVE; encl->secs_child_cnt--; } - sgx_free_encl_page(entry, encl, - evma ? SGX_FREE_SKIP_EREMOVE : 0); + sgx_free_encl_page(entry, encl, free_flags); sgx_put_backing(pages[i++], evma); } @@ -326,13 +341,13 @@ static void sgx_write_pages(struct sgx_encl *encl, struct list_head *src) (encl->flags & SGX_ENCL_INITIALIZED)) { pages[cnt] = sgx_get_backing(encl, &encl->secs_page); if (!IS_ERR(pages[cnt])) { - ret = sgx_ewb(encl, &encl->secs_page, - pages[cnt]); - BUG_ON(ret); + free_flags = 0; + if (sgx_ewb(encl, &encl->secs_page, pages[cnt])) + free_flags = SGX_FREE_SKIP_EREMOVE; + encl->flags |= SGX_ENCL_SECS_EVICTED; - sgx_free_encl_page(&encl->secs_page, encl, - SGX_FREE_SKIP_EREMOVE); + sgx_free_encl_page(&encl->secs_page, encl, free_flags); sgx_put_backing(pages[cnt], true); } }