Skip to contentSkip to conversation

x86: Remove unnecessary kmap() from sgx_ioc_enclave_init()

PATCH Superseded [email protected] 3 participants 3 messages Feb 2, 2021

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: Remove unnecessary kmap() from sgx_ioc_enclave_init()

From: Ira Weiny <[email protected]>

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 <[email protected]>,
Cc Jethro Beekman <[email protected]>,

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

[1] https://lore.kernel.org/lkml/[email protected]/

---
 arch/x86/kernel/cpu/sgx/ioctl.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +8 -2
index 90a5caf76939..678b02d67c3c 100644
@@ -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));
+	BUILD_BUG_ON(SGX_LAUNCH_TOKEN_SIZE > (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);
 
 out:
-	kunmap(initp_page);
 	__free_page(initp_page);
 	return ret;
 }
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#1 lore raw
Quoted 4 lines · On 2/1/21 5:37 PM, [email protected] wrote:

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.

Hi Ira,

It's a *relatively* recent guaranteed, but:

https://www.kernel.org/doc/Documentation/core-api/memory-allocation.rst

says:

Quoted 3 lines

The address of a chunk allocated with kmalloc is aligned to at least ARCH_KMALLOC_MINALIGN bytes. For sizes which are a power of two, the alignment is also guaranteed to be at least the respective size.

So, if you allocate a page with kmalloc(), you get an aligned page. Yay!

maintainer
#2 lore raw
Quoted 12 lines · Dave Hansen
Quoted 4 lines · On 2/1/21 5:37 PM, [email protected] wrote:

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.

Hi Ira,

It's a *relatively* recent guaranteed, but:

https://www.kernel.org/doc/Documentation/core-api/memory-allocation.rst

Quoted 7 lines

says:

Quoted 3 lines

The address of a chunk allocated with kmalloc is aligned to at least ARCH_KMALLOC_MINALIGN bytes. For sizes which are a power of two, the alignment is also guaranteed to be at least the respective size.

So, if you allocate a page with kmalloc(), you get an aligned page. Yay!

And this what we do sgx_ioc_enclave_create() anyway, as I stated in my earlier response. Better to use the same pattern everywhere consitently when it makes sense.

/Jarkko