linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: linux-sgx@vger.kernel.org
Cc: haitao.huang@intel.com, dan.j.williams@intel.com,
	dave.hansen@linux.intel.com, bp@alien8.de, tglx@linutronix.de,
	linux-kernel@vger.kernel.org, x86@kernel.org,
	Jarkko Sakkinen <jarkko@kernel.org>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>
Subject: [PATCH 1/3] x86/sgx: Move struct sgx_va_page creation to sgx_alloc_va_page()
Date: Thu, 25 Feb 2021 00:20:47 +0200	[thread overview]
Message-ID: <20210224222049.240754-2-jarkko@kernel.org> (raw)
In-Reply-To: <20210224222049.240754-1-jarkko@kernel.org>

The VA page creation is illogically split in-between sgx_encl_grow() and
sgx_alloc_va_page(). Move it fully to sgx_alloc_va_page().

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
 arch/x86/kernel/cpu/sgx/encl.c  | 36 +++++++++++++++++++++++----------
 arch/x86/kernel/cpu/sgx/encl.h  |  2 +-
 arch/x86/kernel/cpu/sgx/ioctl.c | 20 ++++--------------
 3 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
index 7449ef33f081..42eb879c167a 100644
--- a/arch/x86/kernel/cpu/sgx/encl.c
+++ b/arch/x86/kernel/cpu/sgx/encl.c
@@ -671,26 +671,40 @@ int sgx_encl_test_and_clear_young(struct mm_struct *mm,
  * Allocate a free EPC page and convert it to a Version Array (VA) page.
  *
  * Return:
- *   a VA page,
- *   -errno otherwise
+ * - A VA page:		On success.
+ * - ERR_PTR(-errno):	Otherwise.
  */
-struct sgx_epc_page *sgx_alloc_va_page(void)
+struct sgx_va_page *sgx_alloc_va_page(void)
 {
-	struct sgx_epc_page *epc_page;
+	struct sgx_va_page *va_page;
 	int ret;
 
-	epc_page = sgx_alloc_epc_page(NULL, true);
-	if (IS_ERR(epc_page))
-		return ERR_CAST(epc_page);
+	va_page = kzalloc(sizeof(*va_page), GFP_KERNEL);
+	if (!va_page)
+		return ERR_PTR(-ENOMEM);
 
-	ret = __epa(sgx_get_epc_virt_addr(epc_page));
+	va_page->epc_page = sgx_alloc_epc_page(NULL, true);
+	if (IS_ERR(va_page->epc_page)) {
+		ret = PTR_ERR(va_page->epc_page);
+		goto err_va_page;
+	}
+
+	ret = __epa(sgx_get_epc_virt_addr(va_page->epc_page));
 	if (ret) {
 		WARN_ONCE(1, "EPA returned %d (0x%x)", ret, ret);
-		sgx_free_epc_page(epc_page);
-		return ERR_PTR(-EFAULT);
+		ret = -EFAULT;
+		goto err_epc_page;
 	}
 
-	return epc_page;
+	return va_page;
+
+err_epc_page:
+	sgx_free_epc_page(va_page->epc_page);
+
+err_va_page:
+	kfree(va_page);
+
+	return ERR_PTR(ret);
 }
 
 /**
diff --git a/arch/x86/kernel/cpu/sgx/encl.h b/arch/x86/kernel/cpu/sgx/encl.h
index d8d30ccbef4c..315e87fcc142 100644
--- a/arch/x86/kernel/cpu/sgx/encl.h
+++ b/arch/x86/kernel/cpu/sgx/encl.h
@@ -111,7 +111,7 @@ void sgx_encl_put_backing(struct sgx_backing *backing, bool do_write);
 int sgx_encl_test_and_clear_young(struct mm_struct *mm,
 				  struct sgx_encl_page *page);
 
-struct sgx_epc_page *sgx_alloc_va_page(void);
+struct sgx_va_page *sgx_alloc_va_page(void);
 unsigned int sgx_alloc_va_slot(struct sgx_va_page *va_page);
 void sgx_free_va_slot(struct sgx_va_page *va_page, unsigned int offset);
 bool sgx_va_page_full(struct sgx_va_page *va_page);
diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index 90a5caf76939..db6e2c6ad42d 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -19,26 +19,14 @@
 static struct sgx_va_page *sgx_encl_grow(struct sgx_encl *encl)
 {
 	struct sgx_va_page *va_page = NULL;
-	void *err;
 
-	BUILD_BUG_ON(SGX_VA_SLOT_COUNT !=
-		(SGX_ENCL_PAGE_VA_OFFSET_MASK >> 3) + 1);
+	BUILD_BUG_ON(SGX_VA_SLOT_COUNT != (SGX_ENCL_PAGE_VA_OFFSET_MASK >> 3) + 1);
 
-	if (!(encl->page_cnt % SGX_VA_SLOT_COUNT)) {
-		va_page = kzalloc(sizeof(*va_page), GFP_KERNEL);
-		if (!va_page)
-			return ERR_PTR(-ENOMEM);
+	if (!(encl->page_cnt % SGX_VA_SLOT_COUNT))
+		va_page = sgx_alloc_va_page();
 
-		va_page->epc_page = sgx_alloc_va_page();
-		if (IS_ERR(va_page->epc_page)) {
-			err = ERR_CAST(va_page->epc_page);
-			kfree(va_page);
-			return err;
-		}
-
-		WARN_ON_ONCE(encl->page_cnt % SGX_VA_SLOT_COUNT);
-	}
 	encl->page_cnt++;
+
 	return va_page;
 }
 
-- 
2.30.1


  reply	other threads:[~2021-02-24 22:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-24 22:20 [PATCH 0/3] Introduce version array structure: sgx_va Jarkko Sakkinen
2021-02-24 22:20 ` Jarkko Sakkinen [this message]
2021-02-24 22:20 ` [PATCH 2/3] x86/sgx: Add a version array (VA) structure Jarkko Sakkinen
2021-02-24 22:20 ` [PATCH 3/3] x86/sgx: Use sgx_va for the enclave's version array Jarkko Sakkinen
2021-02-24 23:48 ` [PATCH 0/3] Introduce version array structure: sgx_va Dave Hansen
2021-02-25  1:01   ` Jarkko Sakkinen
2021-02-25  7:34 ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210224222049.240754-2-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=bp@alien8.de \
    --cc=dan.j.williams@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=haitao.huang@intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).