From patchwork Thu Sep 12 19:47:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143699 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 56B7D14E5 for ; Thu, 12 Sep 2019 19:47:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3FCB92081B for ; Thu, 12 Sep 2019 19:47:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726984AbfILTrf (ORCPT ); Thu, 12 Sep 2019 15:47:35 -0400 Received: from mga18.intel.com ([134.134.136.126]:22341 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbfILTrf (ORCPT ); Thu, 12 Sep 2019 15:47:35 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:47:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336682868" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:47:31 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 01/11] selftest/x86/sgx: Remove encl_piggy.h Date: Thu, 12 Sep 2019 20:47:10 +0100 Message-Id: <20190912194720.7107-2-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org When encl.bin and encl.ss where detached from the test_sgx ELF binary this file should have been removed. Take care of that. Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- tools/testing/selftests/x86/sgx/encl_piggy.h | 14 -------------- tools/testing/selftests/x86/sgx/main.c | 1 - 2 files changed, 15 deletions(-) delete mode 100644 tools/testing/selftests/x86/sgx/encl_piggy.h diff --git a/tools/testing/selftests/x86/sgx/encl_piggy.h b/tools/testing/selftests/x86/sgx/encl_piggy.h deleted file mode 100644 index ee8224f8cc8d..000000000000 --- a/tools/testing/selftests/x86/sgx/encl_piggy.h +++ /dev/null @@ -1,14 +0,0 @@ -/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ -/* - * Copyright(c) 2016-18 Intel Corporation. - */ - -#ifndef ENCL_PIGGY_H -#define ENCL_PIGGY_H - -extern unsigned char encl_bin[]; -extern unsigned char encl_bin_end[]; -extern unsigned char encl_ss[]; -extern unsigned char encl_ss_end[]; - -#endif /* ENCL_PIGGY_H */ diff --git a/tools/testing/selftests/x86/sgx/main.c b/tools/testing/selftests/x86/sgx/main.c index 2160bcd0ccd9..f78ff458b0dd 100644 --- a/tools/testing/selftests/x86/sgx/main.c +++ b/tools/testing/selftests/x86/sgx/main.c @@ -15,7 +15,6 @@ #include #include #include -#include "encl_piggy.h" #include "defines.h" #include "../../../../../arch/x86/kernel/cpu/sgx/arch.h" #include "../../../../../arch/x86/include/uapi/asm/sgx.h" From patchwork Thu Sep 12 19:47:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143701 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CEF99924 for ; Thu, 12 Sep 2019 19:47:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B798220856 for ; Thu, 12 Sep 2019 19:47:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727076AbfILTrm (ORCPT ); Thu, 12 Sep 2019 15:47:42 -0400 Received: from mga07.intel.com ([134.134.136.100]:55553 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbfILTrm (ORCPT ); Thu, 12 Sep 2019 15:47:42 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:47:41 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336682904" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:47:37 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 02/11] x86/sgx: Clean up internal includes Date: Thu, 12 Sep 2019 20:47:11 +0100 Message-Id: <20190912194720.7107-3-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org Include arch.h from sgx.h and include sgx.h from other headers. This makes a sane include order. Any new header should only need to include sgx.h. The .c files must just include the modules that they use. Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/driver.c | 2 ++ arch/x86/kernel/cpu/sgx/driver.h | 3 --- arch/x86/kernel/cpu/sgx/encl.c | 2 -- arch/x86/kernel/cpu/sgx/encl.h | 1 + arch/x86/kernel/cpu/sgx/encls.h | 2 +- arch/x86/kernel/cpu/sgx/ioctl.c | 2 ++ arch/x86/kernel/cpu/sgx/main.c | 3 +-- arch/x86/kernel/cpu/sgx/reclaim.c | 3 ++- arch/x86/kernel/cpu/sgx/sgx.h | 1 + 9 files changed, 10 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c index a1da479ffd3a..1ceeb742c1da 100644 --- a/arch/x86/kernel/cpu/sgx/driver.c +++ b/arch/x86/kernel/cpu/sgx/driver.c @@ -9,6 +9,8 @@ #include #include #include "driver.h" +#include "encl.h" +#include "encls.h" MODULE_DESCRIPTION("Intel SGX Enclave Driver"); MODULE_AUTHOR("Jarkko Sakkinen "); diff --git a/arch/x86/kernel/cpu/sgx/driver.h b/arch/x86/kernel/cpu/sgx/driver.h index 1e35933cf8a4..2f13886522a8 100644 --- a/arch/x86/kernel/cpu/sgx/driver.h +++ b/arch/x86/kernel/cpu/sgx/driver.h @@ -10,9 +10,6 @@ #include #include #include -#include "arch.h" -#include "encl.h" -#include "encls.h" #include "sgx.h" #define SGX_DRV_NR_DEVICES 2 diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index 9bf49e30a568..1c1fbc95be33 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -7,10 +7,8 @@ #include #include #include -#include "arch.h" #include "encl.h" #include "encls.h" -#include "sgx.h" static int __sgx_encl_eldu(struct sgx_encl_page *encl_page, struct sgx_epc_page *epc_page, diff --git a/arch/x86/kernel/cpu/sgx/encl.h b/arch/x86/kernel/cpu/sgx/encl.h index c7608964d69e..95e5713a50ad 100644 --- a/arch/x86/kernel/cpu/sgx/encl.h +++ b/arch/x86/kernel/cpu/sgx/encl.h @@ -15,6 +15,7 @@ #include #include #include +#include "sgx.h" /** * enum sgx_encl_page_desc - defines bits for an enclave page's descriptor diff --git a/arch/x86/kernel/cpu/sgx/encls.h b/arch/x86/kernel/cpu/sgx/encls.h index aea3b9d09936..6631afbf2f64 100644 --- a/arch/x86/kernel/cpu/sgx/encls.h +++ b/arch/x86/kernel/cpu/sgx/encls.h @@ -8,7 +8,7 @@ #include #include #include -#include "arch.h" +#include "sgx.h" /** * ENCLS_FAULT_FLAG - flag signifying an ENCLS return code is a trapnr diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c index e2205a433b87..e57dda38513b 100644 --- a/arch/x86/kernel/cpu/sgx/ioctl.c +++ b/arch/x86/kernel/cpu/sgx/ioctl.c @@ -13,6 +13,8 @@ #include #include #include "driver.h" +#include "encl.h" +#include "encls.h" static struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl) { diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index e4f751651a65..c58ab5f28669 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -9,9 +9,8 @@ #include #include #include "driver.h" -#include "arch.h" +#include "encl.h" #include "encls.h" -#include "sgx.h" struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; int sgx_nr_epc_sections; diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index bd8ac11a4278..d82ce1eaa60e 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -10,7 +10,8 @@ #include #include #include "driver.h" -#include "sgx.h" +#include "encl.h" +#include "encls.h" struct task_struct *ksgxswapd_tsk; DECLARE_WAIT_QUEUE_HEAD(ksgxswapd_waitq); diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h index f0ff7bd3d18e..bc6a644af2b5 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -9,6 +9,7 @@ #include #include #include +#include "arch.h" struct sgx_epc_page { unsigned long desc; From patchwork Thu Sep 12 19:47:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143703 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5557E14E5 for ; Thu, 12 Sep 2019 19:47:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3459320856 for ; Thu, 12 Sep 2019 19:47:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726540AbfILTrt (ORCPT ); Thu, 12 Sep 2019 15:47:49 -0400 Received: from mga06.intel.com ([134.134.136.31]:19814 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbfILTrs (ORCPT ); Thu, 12 Sep 2019 15:47:48 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:47:48 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336682930" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:47:44 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 03/11] x86/sgx: Write backing storage only if EWB is successful Date: Thu, 12 Sep 2019 20:47:12 +0100 Message-Id: <20190912194720.7107-4-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org Do not call set_page_dirty() unless the EWB operation is successful. Probably nothing bad will happen by doing this but it is still an unnecessary action. Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/reclaim.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index d82ce1eaa60e..213406756443 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -266,9 +266,12 @@ static int __sgx_encl_ewb(struct sgx_encl *encl, struct sgx_epc_page *epc_page, kunmap_atomic((void *)(unsigned long)(pginfo.metadata - pcmd_offset)); kunmap_atomic((void *)(unsigned long)pginfo.contents); - set_page_dirty(pcmd); + if (!ret) { + set_page_dirty(pcmd); + set_page_dirty(backing); + } + put_page(pcmd); - set_page_dirty(backing); err_pcmd: put_page(backing); From patchwork Thu Sep 12 19:47:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143705 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1A737924 for ; Thu, 12 Sep 2019 19:47:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ED3BB20856 for ; Thu, 12 Sep 2019 19:47:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726595AbfILTr4 (ORCPT ); Thu, 12 Sep 2019 15:47:56 -0400 Received: from mga03.intel.com ([134.134.136.65]:43463 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726496AbfILTr4 (ORCPT ); Thu, 12 Sep 2019 15:47:56 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:47:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336682961" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:47:52 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 04/11] x86/sgx: Rename 'j' as 'cnt' in sgx_reclaim_pages() Date: Thu, 12 Sep 2019 20:47:13 +0100 Message-Id: <20190912194720.7107-5-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org It is better to have more descriptive name than 'j' for the associated local variable in sgx_reclaim_pages() as it does not represent just a trivial loop index like 'i' does. Thus, rename it as 'cnt'. Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/reclaim.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index 213406756443..cfda38a9b05c 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -403,12 +403,13 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) void sgx_reclaim_pages(void) { struct sgx_epc_page *chunk[SGX_NR_TO_SCAN + 1]; - struct sgx_epc_page *epc_page; struct sgx_epc_section *section; - int i, j; + struct sgx_epc_page *epc_page; + int cnt = 0; + int i; spin_lock(&sgx_active_page_list_lock); - for (i = 0, j = 0; i < SGX_NR_TO_SCAN; i++) { + for (i = 0; i < SGX_NR_TO_SCAN; i++) { if (list_empty(&sgx_active_page_list)) break; @@ -417,7 +418,7 @@ void sgx_reclaim_pages(void) list_del_init(&epc_page->list); if (sgx_reclaimer_get(epc_page)) - chunk[j++] = epc_page; + chunk[cnt++] = epc_page; else /* The owner is freeing the page. No need to add the * page back to the list of reclaimable pages. @@ -426,7 +427,7 @@ void sgx_reclaim_pages(void) } spin_unlock(&sgx_active_page_list_lock); - for (i = 0; i < j; i++) { + for (i = 0; i < cnt; i++) { epc_page = chunk[i]; if (sgx_reclaimer_evict(epc_page)) continue; @@ -440,13 +441,13 @@ void sgx_reclaim_pages(void) chunk[i] = NULL; } - for (i = 0; i < j; i++) { + for (i = 0; i < cnt; i++) { epc_page = chunk[i]; if (epc_page) sgx_reclaimer_block(epc_page); } - for (i = 0; i < j; i++) { + for (i = 0; i < cnt; i++) { epc_page = chunk[i]; if (epc_page) { sgx_reclaimer_write(epc_page); From patchwork Thu Sep 12 19:47:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143707 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C8A3A924 for ; Thu, 12 Sep 2019 19:48:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B074D20856 for ; Thu, 12 Sep 2019 19:48:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726496AbfILTsE (ORCPT ); Thu, 12 Sep 2019 15:48:04 -0400 Received: from mga14.intel.com ([192.55.52.115]:31837 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726867AbfILTsE (ORCPT ); Thu, 12 Sep 2019 15:48:04 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:48:04 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336683009" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:47:59 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 05/11] x86/sgx: Turn encls_failed() as inline function Date: Thu, 12 Sep 2019 20:47:14 +0100 Message-Id: <20190912194720.7107-6-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org Turn encls_failed() as an inline function. As far as the tracing goes we issue warning consistently in the call sites, which is enough to catch the potential issues. Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/Makefile | 2 +- arch/x86/kernel/cpu/sgx/encls.c | 24 ------------------------ arch/x86/kernel/cpu/sgx/encls.h | 18 +++++++++++++++++- 3 files changed, 18 insertions(+), 26 deletions(-) delete mode 100644 arch/x86/kernel/cpu/sgx/encls.c diff --git a/arch/x86/kernel/cpu/sgx/Makefile b/arch/x86/kernel/cpu/sgx/Makefile index 379e9c52848e..cfd29c42264b 100644 --- a/arch/x86/kernel/cpu/sgx/Makefile +++ b/arch/x86/kernel/cpu/sgx/Makefile @@ -1,5 +1,5 @@ # core -obj-y += encl.o encls.o main.o reclaim.o +obj-y += encl.o main.o reclaim.o # driver obj-y += driver.o ioctl.o diff --git a/arch/x86/kernel/cpu/sgx/encls.c b/arch/x86/kernel/cpu/sgx/encls.c deleted file mode 100644 index 1b492c15a2b8..000000000000 --- a/arch/x86/kernel/cpu/sgx/encls.c +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) -// Copyright(c) 2016-19 Intel Corporation. - -#include -#include -#include "encls.h" -#include "sgx.h" - -/** - * encls_failed() - Check if an ENCLS leaf function failed - * @ret: the return value of an ENCLS leaf function call - * - * Check if an ENCLS leaf function failed. This is a condition where the leaf - * function causes a fault that is not caused by an EPCM conflict. - * - * Return: true if there was a fault other than an EPCM conflict - */ -bool encls_failed(int ret) -{ - int epcm_trapnr = boot_cpu_has(X86_FEATURE_SGX2) ? - X86_TRAP_PF : X86_TRAP_GP; - - return encls_faulted(ret) && ENCLS_TRAPNR(ret) != epcm_trapnr; -} diff --git a/arch/x86/kernel/cpu/sgx/encls.h b/arch/x86/kernel/cpu/sgx/encls.h index 6631afbf2f64..b7e6462e58b8 100644 --- a/arch/x86/kernel/cpu/sgx/encls.h +++ b/arch/x86/kernel/cpu/sgx/encls.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "sgx.h" /** @@ -66,7 +67,22 @@ static inline bool encls_returned_code(int ret) return !encls_faulted(ret) && ret; } -bool encls_failed(int ret); +/** + * encls_failed() - Check if an ENCLS leaf function failed + * @ret: the return value of an ENCLS leaf function call + * + * Check if an ENCLS leaf function failed. This is a condition where the leaf + * function causes a fault that is not caused by an EPCM conflict. + * + * Return: true if there was a fault other than an EPCM conflict + */ +static inline bool encls_failed(int ret) +{ + int epcm_trapnr = boot_cpu_has(X86_FEATURE_SGX2) ? + X86_TRAP_PF : X86_TRAP_GP; + + return encls_faulted(ret) && ENCLS_TRAPNR(ret) != epcm_trapnr; +} /** * __encls_ret_N - encode an ENCLS leaf that returns an error code in EAX From patchwork Thu Sep 12 19:47:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143709 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8970A924 for ; Thu, 12 Sep 2019 19:48:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 68A2520856 for ; Thu, 12 Sep 2019 19:48:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726867AbfILTsL (ORCPT ); Thu, 12 Sep 2019 15:48:11 -0400 Received: from mga03.intel.com ([134.134.136.65]:43472 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbfILTsK (ORCPT ); Thu, 12 Sep 2019 15:48:10 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:48:10 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336683033" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:48:06 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 06/11] x86/sgx: Move sgx_einit() to encls.c Date: Thu, 12 Sep 2019 20:47:15 +0100 Message-Id: <20190912194720.7107-7-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org Move sgx_einit() to encls.c as it is essentially a global wrapper for EINIT somewhat independent of the code using it. It does not have any binding with the code in main.c. Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/Makefile | 2 +- arch/x86/kernel/cpu/sgx/encls.c | 56 ++++++++++++++++++++++++++++++++ arch/x86/kernel/cpu/sgx/encls.h | 3 ++ arch/x86/kernel/cpu/sgx/main.c | 50 ---------------------------- arch/x86/kernel/cpu/sgx/sgx.h | 2 -- 5 files changed, 60 insertions(+), 53 deletions(-) create mode 100644 arch/x86/kernel/cpu/sgx/encls.c diff --git a/arch/x86/kernel/cpu/sgx/Makefile b/arch/x86/kernel/cpu/sgx/Makefile index cfd29c42264b..379e9c52848e 100644 --- a/arch/x86/kernel/cpu/sgx/Makefile +++ b/arch/x86/kernel/cpu/sgx/Makefile @@ -1,5 +1,5 @@ # core -obj-y += encl.o main.o reclaim.o +obj-y += encl.o encls.o main.o reclaim.o # driver obj-y += driver.o ioctl.o diff --git a/arch/x86/kernel/cpu/sgx/encls.c b/arch/x86/kernel/cpu/sgx/encls.c new file mode 100644 index 000000000000..cda09cf8b927 --- /dev/null +++ b/arch/x86/kernel/cpu/sgx/encls.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) +// Copyright(c) 2016-19 Intel Corporation. + +#include +#include +#include "encls.h" + +/* A per-cpu cache for the last known values of IA32_SGXLEPUBKEYHASHx MSRs. */ +static DEFINE_PER_CPU(u64 [4], sgx_lepubkeyhash_cache); + +static void sgx_update_lepubkeyhash_msrs(u64 *lepubkeyhash, bool enforce) +{ + u64 *cache; + int i; + + cache = per_cpu(sgx_lepubkeyhash_cache, smp_processor_id()); + for (i = 0; i < 4; i++) { + if (enforce || (lepubkeyhash[i] != cache[i])) { + wrmsrl(MSR_IA32_SGXLEPUBKEYHASH0 + i, lepubkeyhash[i]); + cache[i] = lepubkeyhash[i]; + } + } +} + +/** + * sgx_einit() - Initialize an enclave + * @sigstruct: a pointer a SIGSTRUCT + * @token: a pointer an EINITTOKEN (optional) + * @secs: a pointer a SECS + * @lepubkeyhash: the desired value for IA32_SGXLEPUBKEYHASHx MSRs + * + * Execute ENCLS[EINIT], writing the IA32_SGXLEPUBKEYHASHx MSRs according + * to @lepubkeyhash (if possible and necessary). + * + * Return: + * 0 on success, + * -errno or SGX error on failure + */ +int sgx_einit(struct sgx_sigstruct *sigstruct, struct sgx_einittoken *token, + struct sgx_epc_page *secs, u64 *lepubkeyhash) +{ + int ret; + + if (!boot_cpu_has(X86_FEATURE_SGX_LC)) + return __einit(sigstruct, token, sgx_epc_addr(secs)); + + preempt_disable(); + sgx_update_lepubkeyhash_msrs(lepubkeyhash, false); + ret = __einit(sigstruct, token, sgx_epc_addr(secs)); + if (ret == SGX_INVALID_EINITTOKEN) { + sgx_update_lepubkeyhash_msrs(lepubkeyhash, true); + ret = __einit(sigstruct, token, sgx_epc_addr(secs)); + } + preempt_enable(); + return ret; +} diff --git a/arch/x86/kernel/cpu/sgx/encls.h b/arch/x86/kernel/cpu/sgx/encls.h index b7e6462e58b8..e3713337c187 100644 --- a/arch/x86/kernel/cpu/sgx/encls.h +++ b/arch/x86/kernel/cpu/sgx/encls.h @@ -257,4 +257,7 @@ static inline int __emodt(struct sgx_secinfo *secinfo, void *addr) return __encls_ret_2(SGX_EMODT, secinfo, addr); } +int sgx_einit(struct sgx_sigstruct *sigstruct, struct sgx_einittoken *token, + struct sgx_epc_page *secs, u64 *lepubkeyhash); + #endif /* _X86_ENCLS_H */ diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index c58ab5f28669..4c03e5f33414 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -15,9 +15,6 @@ struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS]; int sgx_nr_epc_sections; -/* A per-cpu cache for the last known values of IA32_SGXLEPUBKEYHASHx MSRs. */ -static DEFINE_PER_CPU(u64 [4], sgx_lepubkeyhash_cache); - static struct sgx_epc_page *sgx_section_try_take_page( struct sgx_epc_section *section) { @@ -162,53 +159,6 @@ void sgx_free_page(struct sgx_epc_page *page) WARN(ret > 0, "sgx: EREMOVE returned %d (0x%x)", ret, ret); } -static void sgx_update_lepubkeyhash_msrs(u64 *lepubkeyhash, bool enforce) -{ - u64 *cache; - int i; - - cache = per_cpu(sgx_lepubkeyhash_cache, smp_processor_id()); - for (i = 0; i < 4; i++) { - if (enforce || (lepubkeyhash[i] != cache[i])) { - wrmsrl(MSR_IA32_SGXLEPUBKEYHASH0 + i, lepubkeyhash[i]); - cache[i] = lepubkeyhash[i]; - } - } -} - -/** - * sgx_einit - initialize an enclave - * @sigstruct: a pointer a SIGSTRUCT - * @token: a pointer an EINITTOKEN (optional) - * @secs: a pointer a SECS - * @lepubkeyhash: the desired value for IA32_SGXLEPUBKEYHASHx MSRs - * - * Execute ENCLS[EINIT], writing the IA32_SGXLEPUBKEYHASHx MSRs according - * to @lepubkeyhash (if possible and necessary). - * - * Return: - * 0 on success, - * -errno or SGX error on failure - */ -int sgx_einit(struct sgx_sigstruct *sigstruct, struct sgx_einittoken *token, - struct sgx_epc_page *secs, u64 *lepubkeyhash) -{ - int ret; - - if (!boot_cpu_has(X86_FEATURE_SGX_LC)) - return __einit(sigstruct, token, sgx_epc_addr(secs)); - - preempt_disable(); - sgx_update_lepubkeyhash_msrs(lepubkeyhash, false); - ret = __einit(sigstruct, token, sgx_epc_addr(secs)); - if (ret == SGX_INVALID_EINITTOKEN) { - sgx_update_lepubkeyhash_msrs(lepubkeyhash, true); - ret = __einit(sigstruct, token, sgx_epc_addr(secs)); - } - preempt_enable(); - return ret; -} - 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 bc6a644af2b5..9b08690262b5 100644 --- a/arch/x86/kernel/cpu/sgx/sgx.h +++ b/arch/x86/kernel/cpu/sgx/sgx.h @@ -85,7 +85,5 @@ void sgx_reclaim_pages(void); 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); -int sgx_einit(struct sgx_sigstruct *sigstruct, struct sgx_einittoken *token, - struct sgx_epc_page *secs, u64 *lepubkeyhash); #endif /* _X86_SGX_H */ From patchwork Thu Sep 12 19:47:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143711 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B1461924 for ; Thu, 12 Sep 2019 19:48:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9AC3C20856 for ; Thu, 12 Sep 2019 19:48:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726794AbfILTsS (ORCPT ); Thu, 12 Sep 2019 15:48:18 -0400 Received: from mga18.intel.com ([134.134.136.126]:22375 "EHLO mga18.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726314AbfILTsS (ORCPT ); Thu, 12 Sep 2019 15:48:18 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:48:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336683046" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:48:13 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 07/11] x86/sgx: Remove pages in sgx_reclaimer_write() Date: Thu, 12 Sep 2019 20:47:16 +0100 Message-Id: <20190912194720.7107-8-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org The overall flow is somewhat simpler if sgx_reclaimer_write() takes care of freeing and removing pages and sgx_encl_ewb() focuses only on doing ENCLS[EWB]. Move sgx_free_page() and __eremove() from sgx_encl_ewb() to sgx_reclaimer_write(). Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/reclaim.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index cfda38a9b05c..353888256b2b 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -357,16 +357,7 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, unsigned int pt) encl_page->desc |= va_offset; encl_page->va_page = va_page; - } else if (pt != SGX_SECINFO_SECS) { - ret = __eremove(sgx_epc_addr(epc_page)); - WARN(ret, "EREMOVE returned %d\n", ret); } - - /* The reclaimer is not aware of SECS pages. */ - if (pt == SGX_SECINFO_SECS) - sgx_free_page(epc_page); - - encl_page->epc_page = NULL; } static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) @@ -374,6 +365,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) struct sgx_encl_page *encl_page = epc_page->owner; struct sgx_encl *encl = encl_page->encl; unsigned int pt; + int ret; if (encl_page->desc & SGX_ENCL_PAGE_TCS) pt = SGX_SECINFO_TCS; @@ -383,13 +375,22 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) mutex_lock(&encl->lock); sgx_encl_ewb(epc_page, pt); + if (atomic_read(&encl->flags) & SGX_ENCL_DEAD) { + ret = __eremove(sgx_epc_addr(epc_page)); + WARN(ret, "EREMOVE returned %d\n", ret); + } + encl_page->epc_page = NULL; encl->secs_child_cnt--; if (!encl->secs_child_cnt && (atomic_read(&encl->flags) & - (SGX_ENCL_DEAD | SGX_ENCL_INITIALIZED))) + (SGX_ENCL_DEAD | SGX_ENCL_INITIALIZED))) { sgx_encl_ewb(encl->secs.epc_page, SGX_SECINFO_SECS); + sgx_free_page(encl->secs.epc_page); + + encl->secs.epc_page = NULL; + } mutex_unlock(&encl->lock); } From patchwork Thu Sep 12 19:47:17 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143713 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8F21B1599 for ; Thu, 12 Sep 2019 19:48:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7823B2081B for ; Thu, 12 Sep 2019 19:48:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726951AbfILTsZ (ORCPT ); Thu, 12 Sep 2019 15:48:25 -0400 Received: from mga09.intel.com ([134.134.136.24]:47142 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726873AbfILTsZ (ORCPT ); Thu, 12 Sep 2019 15:48:25 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:48:24 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336683057" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:48:20 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 08/11] x86/sgx: Calculate page index in sgx_reclaimer_write() Date: Thu, 12 Sep 2019 20:47:17 +0100 Message-Id: <20190912194720.7107-9-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org Move page index calculation for backing storage to sgx_reclaimer_write() as it somewhat simplifies the flow and makes it also easier to control as the high level write logic is consolidated to a single function. Cc: Sean Christopherson Cc: Shay Katz-zamir Cc: Serge Ayoun Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/reclaim.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index 353888256b2b..872c68bf04dd 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -222,26 +222,15 @@ static void sgx_reclaimer_block(struct sgx_epc_page *epc_page) static int __sgx_encl_ewb(struct sgx_encl *encl, struct sgx_epc_page *epc_page, struct sgx_va_page *va_page, unsigned int va_offset, - unsigned int pt) + unsigned int page_index) { - struct sgx_encl_page *encl_page = epc_page->owner; struct sgx_pageinfo pginfo; unsigned long pcmd_offset; struct page *backing; - pgoff_t page_index; pgoff_t pcmd_index; struct page *pcmd; int ret; - if (pt != SGX_SECINFO_SECS && pt != SGX_SECINFO_TCS && - pt != SGX_SECINFO_REG) - return -EINVAL; - - if (pt == SGX_SECINFO_SECS) - page_index = PFN_DOWN(encl->size); - else - page_index = SGX_ENCL_PAGE_INDEX(encl_page); - pcmd_index = sgx_pcmd_index(encl, page_index); pcmd_offset = sgx_pcmd_offset(page_index); @@ -308,7 +297,8 @@ static const cpumask_t *sgx_encl_ewb_cpumask(struct sgx_encl *encl) return cpumask; } -static void sgx_encl_ewb(struct sgx_epc_page *epc_page, unsigned int pt) +static void sgx_encl_ewb(struct sgx_epc_page *epc_page, + unsigned int page_index) { struct sgx_encl_page *encl_page = epc_page->owner; struct sgx_encl *encl = encl_page->encl; @@ -325,7 +315,8 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, unsigned int pt) if (sgx_va_page_full(va_page)) list_move_tail(&va_page->list, &encl->va_pages); - ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset, pt); + ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset, + page_index); if (ret == SGX_NOT_TRACKED) { ret = __etrack(sgx_epc_addr(encl->secs.epc_page)); if (ret) { @@ -335,7 +326,7 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, unsigned int pt) } ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset, - pt); + page_index); if (ret == SGX_NOT_TRACKED) { /* * Slow path, send IPIs to kick cpus out of the @@ -347,7 +338,7 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, unsigned int pt) on_each_cpu_mask(sgx_encl_ewb_cpumask(encl), sgx_ipi_cb, NULL, 1); ret = __sgx_encl_ewb(encl, epc_page, va_page, - va_offset, pt); + va_offset, page_index); } } @@ -374,7 +365,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) mutex_lock(&encl->lock); - sgx_encl_ewb(epc_page, pt); + sgx_encl_ewb(epc_page, SGX_ENCL_PAGE_INDEX(encl_page)); if (atomic_read(&encl->flags) & SGX_ENCL_DEAD) { ret = __eremove(sgx_epc_addr(epc_page)); WARN(ret, "EREMOVE returned %d\n", ret); @@ -386,7 +377,7 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) if (!encl->secs_child_cnt && (atomic_read(&encl->flags) & (SGX_ENCL_DEAD | SGX_ENCL_INITIALIZED))) { - sgx_encl_ewb(encl->secs.epc_page, SGX_SECINFO_SECS); + sgx_encl_ewb(encl->secs.epc_page, PFN_DOWN(encl->size)); sgx_free_page(encl->secs.epc_page); encl->secs.epc_page = NULL; From patchwork Thu Sep 12 19:47:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143715 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 45920924 for ; Thu, 12 Sep 2019 19:48:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2EAFB2081B for ; Thu, 12 Sep 2019 19:48:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726736AbfILTsd (ORCPT ); Thu, 12 Sep 2019 15:48:33 -0400 Received: from mga05.intel.com ([192.55.52.43]:28974 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726968AbfILTsc (ORCPT ); Thu, 12 Sep 2019 15:48:32 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:48:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336683083" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:48:27 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 09/11] x86/sgx: Move SGX_ENCL_DEAD check to sgx_reclaimer_write() Date: Thu, 12 Sep 2019 20:47:18 +0100 Message-Id: <20190912194720.7107-10-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org Do enclave state checks only in sgx_reclaimer_write(). Checking the enclave state is not part of the sgx_encl_ewb() flow. The check is done differently for SECS and for addressable pages. Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/reclaim.c | 69 +++++++++++++++---------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index 872c68bf04dd..f96f4c70f4a6 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -308,47 +308,45 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, encl_page->desc &= ~SGX_ENCL_PAGE_RECLAIMED; - if (!(atomic_read(&encl->flags) & SGX_ENCL_DEAD)) { - va_page = list_first_entry(&encl->va_pages, struct sgx_va_page, - list); - va_offset = sgx_alloc_va_slot(va_page); - if (sgx_va_page_full(va_page)) - list_move_tail(&va_page->list, &encl->va_pages); + va_page = list_first_entry(&encl->va_pages, struct sgx_va_page, + list); + va_offset = sgx_alloc_va_slot(va_page); + if (sgx_va_page_full(va_page)) + list_move_tail(&va_page->list, &encl->va_pages); + + ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset, + page_index); + if (ret == SGX_NOT_TRACKED) { + ret = __etrack(sgx_epc_addr(encl->secs.epc_page)); + if (ret) { + if (encls_failed(ret) || + encls_returned_code(ret)) + ENCLS_WARN(ret, "ETRACK"); + } ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset, page_index); if (ret == SGX_NOT_TRACKED) { - ret = __etrack(sgx_epc_addr(encl->secs.epc_page)); - if (ret) { - if (encls_failed(ret) || - encls_returned_code(ret)) - ENCLS_WARN(ret, "ETRACK"); - } - - ret = __sgx_encl_ewb(encl, epc_page, va_page, va_offset, - page_index); - if (ret == SGX_NOT_TRACKED) { - /* - * Slow path, send IPIs to kick cpus out of the - * enclave. Note, it's imperative that the cpu - * mask is generated *after* ETRACK, else we'll - * miss cpus that entered the enclave between - * generating the mask and incrementing epoch. - */ - on_each_cpu_mask(sgx_encl_ewb_cpumask(encl), - sgx_ipi_cb, NULL, 1); - ret = __sgx_encl_ewb(encl, epc_page, va_page, - va_offset, page_index); - } + /* + * Slow path, send IPIs to kick cpus out of the + * enclave. Note, it's imperative that the cpu + * mask is generated *after* ETRACK, else we'll + * miss cpus that entered the enclave between + * generating the mask and incrementing epoch. + */ + on_each_cpu_mask(sgx_encl_ewb_cpumask(encl), + sgx_ipi_cb, NULL, 1); + ret = __sgx_encl_ewb(encl, epc_page, va_page, + va_offset, page_index); } + } - if (ret) - if (encls_failed(ret) || encls_returned_code(ret)) - ENCLS_WARN(ret, "EWB"); + if (ret) + if (encls_failed(ret) || encls_returned_code(ret)) + ENCLS_WARN(ret, "EWB"); - encl_page->desc |= va_offset; - encl_page->va_page = va_page; - } + encl_page->desc |= va_offset; + encl_page->va_page = va_page; } static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) @@ -365,10 +363,11 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) mutex_lock(&encl->lock); - sgx_encl_ewb(epc_page, SGX_ENCL_PAGE_INDEX(encl_page)); if (atomic_read(&encl->flags) & SGX_ENCL_DEAD) { ret = __eremove(sgx_epc_addr(epc_page)); WARN(ret, "EREMOVE returned %d\n", ret); + } else { + sgx_encl_ewb(epc_page, SGX_ENCL_PAGE_INDEX(encl_page)); } encl_page->epc_page = NULL; From patchwork Thu Sep 12 19:47:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143717 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8AC9D112B for ; Thu, 12 Sep 2019 19:48:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 693B720856 for ; Thu, 12 Sep 2019 19:48:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726968AbfILTsk (ORCPT ); Thu, 12 Sep 2019 15:48:40 -0400 Received: from mga04.intel.com ([192.55.52.120]:11395 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726851AbfILTsk (ORCPT ); Thu, 12 Sep 2019 15:48:40 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:48:39 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336683099" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:48:35 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 10/11] x86/sgx: Free VA slot when the EWB flow fails Date: Thu, 12 Sep 2019 20:47:19 +0100 Message-Id: <20190912194720.7107-11-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org Call sgx_free_va_slot() when the EWB flow fails. Otherwise, they will leak in the failure case. Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/reclaim.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index f96f4c70f4a6..fb41141a28a7 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -341,12 +341,15 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, } } - if (ret) + if (ret) { if (encls_failed(ret) || encls_returned_code(ret)) ENCLS_WARN(ret, "EWB"); - encl_page->desc |= va_offset; - encl_page->va_page = va_page; + sgx_free_va_slot(va_page, va_offset); + } else { + encl_page->desc |= va_offset; + encl_page->va_page = va_page; + } } static void sgx_reclaimer_write(struct sgx_epc_page *epc_page) From patchwork Thu Sep 12 19:47:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11143719 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CA871924 for ; Thu, 12 Sep 2019 19:48:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B3C7120856 for ; Thu, 12 Sep 2019 19:48:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726981AbfILTsq (ORCPT ); Thu, 12 Sep 2019 15:48:46 -0400 Received: from mga11.intel.com ([192.55.52.93]:21360 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726851AbfILTsq (ORCPT ); Thu, 12 Sep 2019 15:48:46 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 12 Sep 2019 12:48:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,489,1559545200"; d="scan'208";a="336683105" Received: from dscaswel-mobl1.ger.corp.intel.com (HELO localhost) ([10.252.53.44]) by orsmga004.jf.intel.com with ESMTP; 12 Sep 2019 12:48:41 -0700 From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: sean.j.christopherson@intel.com, serge.ayoun@intel.com, shay.katz-zamir@intel.com, Jarkko Sakkinen Subject: [PATCH RESEND 11/11] x86/sgx: Call sgx_encl_destroy() when the EWB flow fails Date: Thu, 12 Sep 2019 20:47:20 +0100 Message-Id: <20190912194720.7107-12-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> References: <20190912194720.7107-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Sender: linux-sgx-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org There is not much we can do if the EWB flow fails. It can fail if the binding of the backing storage fails or if the enclave is running inside a VM and the host is suspended. Signed-off-by: Jarkko Sakkinen --- arch/x86/kernel/cpu/sgx/reclaim.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kernel/cpu/sgx/reclaim.c b/arch/x86/kernel/cpu/sgx/reclaim.c index fb41141a28a7..ba1260a373ce 100644 --- a/arch/x86/kernel/cpu/sgx/reclaim.c +++ b/arch/x86/kernel/cpu/sgx/reclaim.c @@ -345,7 +345,7 @@ static void sgx_encl_ewb(struct sgx_epc_page *epc_page, if (encls_failed(ret) || encls_returned_code(ret)) ENCLS_WARN(ret, "EWB"); - sgx_free_va_slot(va_page, va_offset); + sgx_encl_destroy(encl); } else { encl_page->desc |= va_offset; encl_page->va_page = va_page;