archive mirror
 help / color / mirror / Atom feed
To: Jarkko Sakkinen <>
Cc: Ira Weiny <>,
	Sean Christopherson <>,
	Jethro Beekman <>,,
Subject: [PATCH] x86: Remove unnecessary kmap() from sgx_ioc_enclave_init()
Date: Mon,  1 Feb 2021 17:37:25 -0800	[thread overview]
Message-ID: <> (raw)

From: Ira Weiny <>

kmap is inefficient and we are trying to reduce the usage in the kernel.
There is no readily apparent reason why the initp_page page needs to be
allocated and kmap'ed() but sigstruct needs to be page aligned and token
512 byte aligned.

In this case page_address() can be used instead of kmap_local_page() as
a much more efficient way to use the address because the page is
allocated GFP_KERNEL.

Remove the kmap and replace with page_address() to get a kernel address
for the alloc'ed page.

In addition add a comment regarding the alignment requirements as well
as 2 BUILD_BUG_ON's to ensure future changes to sigstruct and token do
not go unnoticed and cause a bug.

Cc: Sean Christopherson <>,
Cc: Jethro Beekman <>,
Signed-off-by: Ira Weiny <>

Changes from v1[1]:
	Use page_address() instead of kcmalloc() to ensure sigstruct is
	page aligned
	Use BUILD_BUG_ON to ensure token and sigstruct don't collide.

 arch/x86/kernel/cpu/sgx/ioctl.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kernel/cpu/sgx/ioctl.c b/arch/x86/kernel/cpu/sgx/ioctl.c
index 90a5caf76939..678b02d67c3c 100644
--- a/arch/x86/kernel/cpu/sgx/ioctl.c
+++ b/arch/x86/kernel/cpu/sgx/ioctl.c
@@ -615,11 +615,18 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
 	if (copy_from_user(&init_arg, arg, sizeof(init_arg)))
 		return -EFAULT;
+	/*
+	 * sigstruct must be on a page boundry and token on a 512 byte boundry
+	 * so use alloc_page/page_address instead of a kmalloc().
+	 */
 	initp_page = alloc_page(GFP_KERNEL);
 	if (!initp_page)
 		return -ENOMEM;
-	sigstruct = kmap(initp_page);
+	sigstruct = page_address(initp_page);
+	BUILD_BUG_ON(sizeof(*sigstruct) > (PAGE_SIZE/2));
 	token = (void *)((unsigned long)sigstruct + PAGE_SIZE / 2);
 	memset(token, 0, SGX_LAUNCH_TOKEN_SIZE);
@@ -645,7 +652,6 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
 	ret = sgx_encl_init(encl, sigstruct, token);
-	kunmap(initp_page);
 	return ret;

             reply	other threads:[~2021-02-02  1:38 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-02  1:37 ira.weiny [this message]
2021-02-02 18:55 ` [PATCH] x86: Remove unnecessary kmap() from sgx_ioc_enclave_init() Dave Hansen
2021-02-02 22:43   ` Jarkko Sakkinen
  -- strict thread matches above, loose matches on Subject: below --
2021-01-29  0:14 ira.weiny
2021-01-29 17:37 ` Sean Christopherson
2021-02-01  8:48   ` Christoph Hellwig
2021-02-02 17:37     ` 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:

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

  git send-email \ \ \ \ \ \ \ \

* 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).