Skip to content

Commit 633b061

Browse files
weiny2suryasaimadhu
authored andcommitted
x86/sgx: Remove unnecessary kmap() from sgx_ioc_enclave_init()
kmap() is inefficient and is being replaced by kmap_local_page(), if possible. There is no readily apparent reason why initp_page needs to be allocated and kmap'ed() except that 'sigstruct' needs to be page-aligned and 'token' 512 byte-aligned. Rather than change it to kmap_local_page(), use kmalloc() instead because kmalloc() can give this alignment when allocating PAGE_SIZE bytes. Remove the alloc_page()/kmap() and replace with kmalloc(PAGE_SIZE, ...) to get a page aligned kernel address. In addition, add a comment to document the alignment requirements so that others don't attempt to 'fix' this again. [ bp: Massage commit message. ] Signed-off-by: Ira Weiny <[email protected]> Signed-off-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent f33dece commit 633b061

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

arch/x86/kernel/cpu/sgx/ioctl.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,6 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
604604
{
605605
struct sgx_sigstruct *sigstruct;
606606
struct sgx_enclave_init init_arg;
607-
struct page *initp_page;
608607
void *token;
609608
int ret;
610609

@@ -615,11 +614,15 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
615614
if (copy_from_user(&init_arg, arg, sizeof(init_arg)))
616615
return -EFAULT;
617616

618-
initp_page = alloc_page(GFP_KERNEL);
619-
if (!initp_page)
617+
/*
618+
* 'sigstruct' must be on a page boundary and 'token' on a 512 byte
619+
* boundary. kmalloc() will give this alignment when allocating
620+
* PAGE_SIZE bytes.
621+
*/
622+
sigstruct = kmalloc(PAGE_SIZE, GFP_KERNEL);
623+
if (!sigstruct)
620624
return -ENOMEM;
621625

622-
sigstruct = kmap(initp_page);
623626
token = (void *)((unsigned long)sigstruct + PAGE_SIZE / 2);
624627
memset(token, 0, SGX_LAUNCH_TOKEN_SIZE);
625628

@@ -645,8 +648,7 @@ static long sgx_ioc_enclave_init(struct sgx_encl *encl, void __user *arg)
645648
ret = sgx_encl_init(encl, sigstruct, token);
646649

647650
out:
648-
kunmap(initp_page);
649-
__free_page(initp_page);
651+
kfree(sigstruct);
650652
return ret;
651653
}
652654

0 commit comments

Comments
 (0)