Some optimizations related to sgx
v3 cover Some optimizations related to sgx
This is an optimization of a set of sgx-related codes, each of which is independent of the patch. Because the second and third patches have conflicting dependencies, these patches are put together.
---
v3 changes:
* split free_cnt count and spin lock optimization into two patches
v2 changes:
* review suggested changes
Tianjia Zhang (5):
selftests/x86: Simplify the code to get vdso base address in sgx x86/sgx: Optimize the locking range in sgx_sanitize_section() x86/sgx: Optimize the free_cnt count in sgx_epc_section x86/sgx: Allows ioctl PROVISION to execute before CREATE x86/sgx: Remove redundant if conditions in sgx_encl_create
arch/x86/kernel/cpu/sgx/driver.c | 1 + arch/x86/kernel/cpu/sgx/ioctl.c | 9 +++++---- arch/x86/kernel/cpu/sgx/main.c | 13 +++++-------- tools/testing/selftests/sgx/main.c | 24 ++++-------------------- 4 files changed, 15 insertions(+), 32 deletions(-)
Signature · 1 line
2.19.1.3.ge56e4f7
PATCH v3 1/5 selftests/x86: Simplify the code to get vdso base address in sgx
This patch uses the library function getauxval(AT_SYSINFO_EHDR) instead of the custom function vdso_get_base_addr to obtain the base address of vDSO, which will simplify the code implementation.
--- tools/testing/selftests/sgx/main.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-)
tools/testing/selftests/sgx/main.c +4 -20
@@ -15,6 +15,7 @@#include <sys/stat.h> #include <sys/time.h> #include <sys/types.h> +#include <sys/auxv.h> #include "defines.h" #include "main.h" #include "../kselftest.h"
@@ -28,24 +29,6 @@ struct vdso_symtab {Elf64_Word *elf_hashtab; }; -static void *vdso_get_base_addr (char *envp[]) -{ -Elf64_auxv_t *auxv; -int i; - -for (i =0 ; envp[i]; i++) - ; - - auxv = (Elf64_auxv_t *)&envp[i +1 ]; - -for (i =0 ; auxv[i].a_type !=AT_NULL ; i++) { -if (auxv[i].a_type ==AT_SYSINFO_EHDR ) -return (void *)auxv[i].a_un.a_val; - } - -return NULL ; -} -static Elf64_Dyn *vdso_get_dyntab (void *addr) {Elf64_Ehdr *ehdr = addr;
@@ -162,7 +145,7 @@ static int user_handler(long rdi, long rsi, long rdx, long ursp, long r8, long rreturn 0 ; } -int main (int argc,char *argv[],char *envp[]) +int main (int argc,char *argv[]) {struct sgx_enclave_run run;struct vdso_symtab symtab;
@@ -203,7 +186,8 @@ int main(int argc, char *argv[], char *envp[])memset (&run,0 ,sizeof (run)); run.tcs = encl.encl_base; - addr =vdso_get_base_addr (envp); +/* Get vDSO base address */ + addr = (void *)(uintptr_t )getauxval (AT_SYSINFO_EHDR );if (!addr) goto err;
Signature · 1 line
2.19.1.3.ge56e4f7
PATCH v3 2/5 x86/sgx: Optimize the locking range in sgx_sanitize_section()
In reply to Tianjia ZhangThe spin lock of sgx_epc_section only locks the page_list. The EREMOVE operation and init_laundry_list is not necessary in the protection range of the spin lock. This patch reduces the lock range of the spin lock in the function sgx_sanitize_section() and only protects the operation of the page_list.
--- arch/x86/kernel/cpu/sgx/main.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +4 -7
@@ -41,20 +41,17 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)if (kthread_should_stop ())return ; -/* needed for access to ->page_list: */ -spin_lock (§ion->lock); - page =list_first_entry (§ion->init_laundry_list, struct sgx_epc_page, list); ret =__eremove (sgx_get_epc_virt_addr (page)); -if (!ret) +if (!ret) { +spin_lock (§ion->lock);list_move (&page->list, §ion->page_list); -else +spin_unlock (§ion->lock); + }else list_move_tail (&page->list, &dirty); -spin_unlock (§ion->lock); -cond_resched (); }
Signature · 1 line
2.19.1.3.ge56e4f7
Jarkko Sakkinen
maintainerRe: [PATCH v3 2/5] x86/sgx: Optimize the locking range in sgx_sanitize_section()
Quoted 8 lines · Tianjia Zhang
The spin lock of
sgx_epc_sectiononly locks thepage_list. The EREMOVE operation andinit_laundry_listis not necessary in the protection range of the spin lock. This patch reduces the lock range of the spin lock in the functionsgx_sanitize_section()and only protects the operation of thepage_list.Suggested-by Sean Christopherson <[email protected]>Signed-off-by Tianjia Zhang <[email protected]>
I prefer to use the word "optimize" only if there is supporting data to show that the code change has significant value.
/Jarkko
PATCH v3 3/5 x86/sgx: Optimize the free_cnt count in sgx_epc_section
In reply to Tianjia Zhangsection->free_cnt represents the free page in sgx_epc_section, which is assigned once after initialization. In fact, just after the initialization is completed, the pages are in the init_laundry_list list and cannot be allocated. This needs to be recovered by EREMOVE of function sgx_sanitize_section() before it can be used as a page that can be allocated. The sgx_sanitize_section() will be called in the kernel thread ksgxd.
This patch moves the initialization of section->free_cnt from the initialization function sgx_setup_epc_section() to the function sgx_sanitize_section(), and then accumulates the count after the successful execution of EREMOVE. This seems to be more reasonable, free_cnt will also truly reflect the allocatable free pages in EPC.
--- arch/x86/kernel/cpu/sgx/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/main.c +1 -1
@@ -48,6 +48,7 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)if (!ret) {spin_lock (§ion->lock);list_move (&page->list, §ion->page_list); + section->free_cnt++;spin_unlock (§ion->lock); }else list_move_tail (&page->list, &dirty);
@@ -643,7 +644,6 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,list_add_tail (§ion->pages[i].list, §ion->init_laundry_list); } - section->free_cnt = nr_pages;return true; }
Signature · 1 line
2.19.1.3.ge56e4f7
Jarkko Sakkinen
maintainerRe: [PATCH v3 3/5] x86/sgx: Optimize the free_cnt count in sgx_epc_section
I could bet some money that this does not bring any significant performance gain.
Quoted 11 lines · Tianjia Zhang
section->free_cntrepresents the free page insgx_epc_section, which is assigned once after initialization. In fact, just after the initialization is completed, the pages are in theinit_laundry_listlist and cannot be allocated. This needs to be recovered by EREMOVE of functionsgx_sanitize_section()before it can be used as a page that can be allocated. Thesgx_sanitize_section()will be called in the kernel thread ksgxd.This patch moves the initialization of
section->free_cntfrom the initialization functionsgx_setup_epc_section()to the functionsgx_sanitize_section(), and then accumulates the count after the
Use single quotes instead of hyphens.
Quoted 14 lines
successful execution of EREMOVE. This seems to be more reasonable,
free_cntwill also truly reflect the allocatable free pages in EPC.Sined-off-by Tianjia Zhang <[email protected]>Reviewed-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/cpu/sgx/main.c +0
-0
arch/x86/kernel/cpu/sgx/main.c static void sgx_sanitize_section(struct sgx_epc_section *section) · line 52
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0@@ -48,6 +48,7 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)if (!ret) {spin_lock (§ion->lock);list_move (&page->list, §ion->page_list); + section->free_cnt++;spin_unlock (§ion->lock);
Someone can try to allocate a page while sanitize process is in progress.
I think it is better to keep critical sections in the form that when you leave from one, the global state is legit.
Quoted 14 lines
arch/x86/kernel/cpu/sgx/main.c +0
-1}else list_move_tail (&page->list, &dirty);@@ -643,7 +644,6 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,list_add_tail (§ion->pages[i].list, §ion->init_laundry_list); } - section->free_cnt = nr_pages;return true; }Signature · 1 line
2.19.1.3.ge56e4f7
/Jarkko
Re: [PATCH v3 3/5] x86/sgx: Optimize the free_cnt count in sgx_epc_section
Hi,
Sorry for the late reply.
Quoted 3 lines · Jarkko Sakkinen
I could bet some money that this does not bring any significant performance gain.
Yes, this does not bring performance gains. This is not a change for performance, mainly to make the value of free_cnt look more accurate.
Quoted 36 lines · Tianjia Zhang
Quoted 11 lines
section->free_cntrepresents the free page insgx_epc_section, which is assigned once after initialization. In fact, just after the initialization is completed, the pages are in theinit_laundry_listlist and cannot be allocated. This needs to be recovered by EREMOVE of functionsgx_sanitize_section()before it can be used as a page that can be allocated. Thesgx_sanitize_section()will be called in the kernel thread ksgxd.This patch moves the initialization of
section->free_cntfrom the initialization functionsgx_setup_epc_section()to the functionsgx_sanitize_section(), and then accumulates the count after theUse single quotes instead of hyphens.
Quoted 14 lines
Quoted 1 line
successful execution of EREMOVE. This seems to be more reasonable,
free_cntwill also truly reflect the allocatable free pages in EPC.Sined-off-by Tianjia Zhang <[email protected]>Reviewed-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/cpu/sgx/main.c +0
-0arch/x86/kernel/cpu/sgx/main.c static void sgx_sanitize_section(struct sgx_epc_section *section) · line 52
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0@@ -48,6 +48,7 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)if (!ret) {spin_lock (§ion->lock);list_move (&page->list, §ion->page_list); + section->free_cnt++;spin_unlock (§ion->lock);Someone can try to allocate a page while sanitize process is in progress.
Quoted 3 lines
I think it is better to keep critical sections in the form that when you leave from one, the global state is legit.
Do you mean to move the critical section to protect the entire while loop? Of course, this is also possible, sanitize is a process only needed for initialization, and the possibility of conflict is very small.
Best regards,
Tianjia
Jarkko Sakkinen
maintainerRe: [PATCH v3 3/5] x86/sgx: Optimize the free_cnt count in sgx_epc_section
Quoted 52 lines · Tianjia Zhang
Hi,
Sorry for the late reply.
Quoted 3 lines · Jarkko Sakkinen
I could bet some money that this does not bring any significant performance gain.
Yes, this does not bring performance gains. This is not a change for performance, mainly to make the value of
free_cntlook more accurate.Quoted 36 lines · Tianjia Zhang
Quoted 11 lines
section->free_cntrepresents the free page insgx_epc_section, which is assigned once after initialization. In fact, just after the initialization is completed, the pages are in theinit_laundry_listlist and cannot be allocated. This needs to be recovered by EREMOVE of functionsgx_sanitize_section()before it can be used as a page that can be allocated. Thesgx_sanitize_section()will be called in the kernel thread ksgxd.This patch moves the initialization of
section->free_cntfrom the initialization functionsgx_setup_epc_section()to the functionsgx_sanitize_section(), and then accumulates the count after theUse single quotes instead of hyphens.
Quoted 14 lines
Quoted 1 line
successful execution of EREMOVE. This seems to be more reasonable,
free_cntwill also truly reflect the allocatable free pages in EPC.Sined-off-by Tianjia Zhang <[email protected]>Reviewed-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/cpu/sgx/main.c +0
-0arch/x86/kernel/cpu/sgx/main.c static void sgx_sanitize_section(struct sgx_epc_section *section) · line 52
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0@@ -48,6 +48,7 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)if (!ret) {spin_lock (§ion->lock);list_move (&page->list, §ion->page_list); + section->free_cnt++;spin_unlock (§ion->lock);Someone can try to allocate a page while sanitize process is in progress.
Quoted 3 lines
I think it is better to keep critical sections in the form that when you leave from one, the global state is legit.
Quoted 6 lines
Do you mean to move the critical section to protect the entire while loop? Of course, this is also possible, sanitize is a process only needed for initialization, and the possibility of conflict is very small.
Best regards,
Tianjia
The big picture of this change to me, to be frank is that it's completely useless.
Please start with the picture.
/Jarkko
Re: [PATCH v3 3/5] x86/sgx: Optimize the free_cnt count in sgx_epc_section
Quoted 60 lines · Jarkko Sakkinen
Quoted 52 lines · Tianjia Zhang
Hi,
Sorry for the late reply.
Quoted 3 lines · Jarkko Sakkinen
I could bet some money that this does not bring any significant performance gain.
Yes, this does not bring performance gains. This is not a change for performance, mainly to make the value of
free_cntlook more accurate.Quoted 36 lines · Tianjia Zhang
Quoted 11 lines
section->free_cntrepresents the free page insgx_epc_section, which is assigned once after initialization. In fact, just after the initialization is completed, the pages are in theinit_laundry_listlist and cannot be allocated. This needs to be recovered by EREMOVE of functionsgx_sanitize_section()before it can be used as a page that can be allocated. Thesgx_sanitize_section()will be called in the kernel thread ksgxd.This patch moves the initialization of
section->free_cntfrom the initialization functionsgx_setup_epc_section()to the functionsgx_sanitize_section(), and then accumulates the count after theUse single quotes instead of hyphens.
Quoted 14 lines
Quoted 1 line
successful execution of EREMOVE. This seems to be more reasonable,
free_cntwill also truly reflect the allocatable free pages in EPC.Sined-off-by Tianjia Zhang <[email protected]>Reviewed-by Sean Christopherson <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/cpu/sgx/main.c +0
-0arch/x86/kernel/cpu/sgx/main.c static void sgx_sanitize_section(struct sgx_epc_section *section) · line 52
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0@@ -48,6 +48,7 @@ static void sgx_sanitize_section(struct sgx_epc_section *section)if (!ret) {spin_lock (§ion->lock);list_move (&page->list, §ion->page_list); + section->free_cnt++;spin_unlock (§ion->lock);Someone can try to allocate a page while sanitize process is in progress.
Quoted 3 lines
I think it is better to keep critical sections in the form that when you leave from one, the global state is legit.
Quoted 6 lines
Do you mean to move the critical section to protect the entire while loop? Of course, this is also possible, sanitize is a process only needed for initialization, and the possibility of conflict is very small.
Best regards,
Tianjia
Quoted 7 lines
The big picture of this change to me, to be frank is that it's completely useless.
Please start with the picture.
/Jarkko
I carefully considered your suggestion, and I will delete 2/5 and 3/5 in the next version.
Best regards,
Tianjia
PATCH v3 4/5 x86/sgx: Allows ioctl PROVISION to execute before CREATE
In reply to Tianjia ZhangIn the function sgx_create_enclave(), the direct assignment operation of attributes_mask determines that the ioctl PROVISION operation must be executed after the ioctl CREATE operation, which will limit the flexibility of sgx developers.
This patch takes the assignment of attributes_mask from the function sgx_create_enclave() has been moved to the function sgx_open() to avoid this restriction.
--- arch/x86/kernel/cpu/sgx/driver.c | 1 + arch/x86/kernel/cpu/sgx/ioctl.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/cpu/sgx/driver.c +1 -0
@@ -36,6 +36,7 @@ static int sgx_open(struct inode *inode, struct file *file)return ret; } + encl->attributes_mask =SGX_ATTR_DEBUG |SGX_ATTR_MODE64BIT |SGX_ATTR_KSS ; file->private_data = encl;return 0 ;
arch/x86/kernel/cpu/sgx/ioctl.c +0 -1
@@ -109,7 +109,6 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) encl->base = secs->base; encl->size = secs->size; encl->attributes = secs->attributes; - encl->attributes_mask =SGX_ATTR_DEBUG |SGX_ATTR_MODE64BIT |SGX_ATTR_KSS ;/* Set only after completion, as encl->lock has not been taken. */ set_bit (SGX_ENCL_CREATED , &encl->flags);
Signature · 1 line
2.19.1.3.ge56e4f7
Jarkko Sakkinen
maintainerRe: [PATCH v3 4/5] x86/sgx: Allows ioctl PROVISION to execute before CREATE
Quoted 10 lines · Tianjia Zhang
In the function
sgx_create_enclave(), the direct assignment operation ofattributes_maskdetermines that the ioctl PROVISION operation must be executed after the ioctl CREATE operation, which will limit the flexibility of sgx developers.This patch takes the assignment of
attributes_maskfrom the functionsgx_create_enclave()has been moved to the functionsgx_open()to avoid this restriction.Signed-off-by Tianjia Zhang <[email protected]>
The commit message should explicit that the API behaviour changes as the result. Please don't use hyphens in quoting.
/Jarkko
Quoted 33 lines
--- arch/x86/kernel/cpu/sgx/driver.c | 1 + arch/x86/kernel/cpu/sgx/ioctl.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/cpu/sgx/driver.c +1
-0@@ -36,6 +36,7 @@ static int sgx_open(struct inode *inode, struct file *file)return ret; } + encl->attributes_mask =SGX_ATTR_DEBUG |SGX_ATTR_MODE64BIT |SGX_ATTR_KSS ; file->private_data = encl;return 0 ;arch/x86/kernel/cpu/sgx/ioctl.c +0
-1@@ -109,7 +109,6 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) encl->base = secs->base; encl->size = secs->size; encl->attributes = secs->attributes; - encl->attributes_mask =SGX_ATTR_DEBUG |SGX_ATTR_MODE64BIT |SGX_ATTR_KSS ;/* Set only after completion, as encl->lock has not been taken. */ set_bit (SGX_ENCL_CREATED , &encl->flags);Signature · 1 line
2.19.1.3.ge56e4f7
Re: [PATCH v3 4/5] x86/sgx: Allows ioctl PROVISION to execute before CREATE
Quoted 10 lines · Jarkko Sakkinen
Quoted 9 lines · Tianjia Zhang
In the function
sgx_create_enclave(), the direct assignment operation ofattributes_maskdetermines that the ioctl PROVISION operation must be executed after the ioctl CREATE operation, which will limit the flexibility of sgx developers.This patch takes the assignment of
attributes_maskfrom the functionsgx_create_enclave()has been moved to the functionsgx_open()to avoid this restriction.
Quoted 7 lines
Quoted 1 line
Signed-off-by Tianjia Zhang <[email protected]>The commit message should explicit that the API behaviour changes as the result. Please don't use hyphens in quoting.
/Jarkko
Will be improved in the next version.
Best regards,
Tianjia
Quoted 33 lines
Quoted 33 lines
--- arch/x86/kernel/cpu/sgx/driver.c | 1 + arch/x86/kernel/cpu/sgx/ioctl.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-)arch/x86/kernel/cpu/sgx/driver.c +1
-0@@ -36,6 +36,7 @@ static int sgx_open(struct inode *inode, struct file *file)return ret; } + encl->attributes_mask =SGX_ATTR_DEBUG |SGX_ATTR_MODE64BIT |SGX_ATTR_KSS ; file->private_data = encl;return 0 ;arch/x86/kernel/cpu/sgx/ioctl.c +0
-1@@ -109,7 +109,6 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) encl->base = secs->base; encl->size = secs->size; encl->attributes = secs->attributes; - encl->attributes_mask =SGX_ATTR_DEBUG |SGX_ATTR_MODE64BIT |SGX_ATTR_KSS ;/* Set only after completion, as encl->lock has not been taken. */ set_bit (SGX_ENCL_CREATED , &encl->flags);Signature · 1 line
2.19.1.3.ge56e4f7
PATCH v3 5/5 x86/sgx: Remove redundant if conditions in sgx_encl_create
In reply to Tianjia ZhangIn this scenario, there is no case where va_page is NULL, and the error has been checked. The if condition statement here is redundant, so remove the condition detection.
--- arch/x86/kernel/cpu/sgx/ioctl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kernel/cpu/sgx/ioctl.c +5 -3
@@ -66,9 +66,11 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) va_page =sgx_encl_grow (encl);if (IS_ERR (va_page))return PTR_ERR (va_page); -else if (va_page) -list_add (&va_page->list, &encl->va_pages); -/* else the tail page of the VA page list had free slots. */ + +if (WARN_ONCE (!va_page,"non-empty VA page list before ECREATE" )) +return -EIO ; + +list_add (&va_page->list, &encl->va_pages);/* The extra page goes to SECS. */ encl_size = secs->size +PAGE_SIZE ;
Signature · 1 line
2.19.1.3.ge56e4f7
Re: [PATCH v3 5/5] x86/sgx: Remove redundant if conditions in sgx_encl_create
Quoted 14 lines · Tianjia Zhang
In this scenario, there is no case where
va_pageis NULL, and the error has been checked. The if condition statement here is redundant, so remove the condition detection.Signed-off-by Tianjia Zhang <[email protected]>--- arch/x86/kernel/cpu/sgx/ioctl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)arch/x86/kernel/cpu/sgx/ioctl.c +0
-0
arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 71
Quoted 9 lines
arch/x86/kernel/cpu/sgx/ioctl.c +3
-3@@ -66,9 +66,11 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) va_page =sgx_encl_grow (encl);if (IS_ERR (va_page))return PTR_ERR (va_page); -else if (va_page) -list_add (&va_page->list, &encl->va_pages); -/* else the tail page of the VA page list had free slots. */ + +if (WARN_ONCE (!va_page,"non-empty VA page list before ECREATE" )) +return -EIO ;
So you just crashed machines that have panic-on-warn enabled. Don't do that for no reason, just handle the error and move on.
thanks,
greg k-h
Re: [PATCH v3 5/5] x86/sgx: Remove redundant if conditions in sgx_encl_create
Quoted 19 lines · Greg KH
Quoted 18 lines · Tianjia Zhang
In this scenario, there is no case where
va_pageis NULL, and the error has been checked. The if condition statement here is redundant, so remove the condition detection.Signed-off-by Tianjia Zhang <[email protected]>--- arch/x86/kernel/cpu/sgx/ioctl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)arch/x86/kernel/cpu/sgx/ioctl.c +0
-1@@ -66,9 +66,11 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) va_page =sgx_encl_grow (encl);if (IS_ERR (va_page))return PTR_ERR (va_page); -else if (va_page)
Quoted 8 lines
arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 71
Quoted 5 lines
arch/x86/kernel/cpu/sgx/ioctl.c +3
-2-list_add (&va_page->list, &encl->va_pages); -/* else the tail page of the VA page list had free slots. */ + +if (WARN_ONCE (!va_page,"non-empty VA page list before ECREATE" )) +return -EIO ;So you just crashed machines that have panic-on-warn enabled. Don't do that for no reason, just handle the error and move on.
The WARN will only fire if someone introduces a kernel bug. It's one part documentation, two parts helping detect future breakage. Even if the VA page management is significantly reworked, I'm having a hard time envisioning a scenario where adding VA pages before ECREATE would be anything but a kernel bug.
Jarkko Sakkinen
maintainerRe: [PATCH v3 5/5] x86/sgx: Remove redundant if conditions in sgx_encl_create
In reply to Tianjia ZhangQuoted 14 lines · Tianjia Zhang
In this scenario, there is no case where
va_pageis NULL, and the error has been checked. The if condition statement here is redundant, so remove the condition detection.Signed-off-by Tianjia Zhang <[email protected]>--- arch/x86/kernel/cpu/sgx/ioctl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)arch/x86/kernel/cpu/sgx/ioctl.c +0
-0
arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 71
Quoted 6 lines
arch/x86/kernel/cpu/sgx/ioctl.c +0
-3@@ -66,9 +66,11 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) va_page =sgx_encl_grow (encl);if (IS_ERR (va_page))return PTR_ERR (va_page); -else if (va_page) -list_add (&va_page->list, &encl->va_pages); -/* else the tail page of the VA page list had free slots. */
This is fine. The check does not make sense.
arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 71
Quoted 3 lines
arch/x86/kernel/cpu/sgx/ioctl.c +3
-0+ +if (WARN_ONCE (!va_page,"non-empty VA page list before ECREATE" )) +return -EIO ;
No need for this WARN_ONCE().
arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 73
Quoted 2 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-0+ +list_add (&va_page->list, &encl->va_pages);
This is fine.
Quoted 7 lines
arch/x86/kernel/cpu/sgx/ioctl.c +0
-0/* The extra page goes to SECS. */ encl_size = secs->size +PAGE_SIZE ;Signature · 1 line
2.19.1.3.ge56e4f7
/Jarkko
Re: [PATCH v3 5/5] x86/sgx: Remove redundant if conditions in sgx_encl_create
Quoted 36 lines · Jarkko Sakkinen
Quoted 14 lines · Tianjia Zhang
In this scenario, there is no case where
va_pageis NULL, and the error has been checked. The if condition statement here is redundant, so remove the condition detection.Signed-off-by Tianjia Zhang <[email protected]>--- arch/x86/kernel/cpu/sgx/ioctl.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)arch/x86/kernel/cpu/sgx/ioctl.c +0
-0arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 71
Quoted 6 lines
arch/x86/kernel/cpu/sgx/ioctl.c +0
-3@@ -66,9 +66,11 @@ static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) va_page =sgx_encl_grow (encl);if (IS_ERR (va_page))return PTR_ERR (va_page); -else if (va_page) -list_add (&va_page->list, &encl->va_pages); -/* else the tail page of the VA page list had free slots. */ This is fine. The check does not make sense.
arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 71
Quoted 3 lines
arch/x86/kernel/cpu/sgx/ioctl.c +3
-0+ +if (WARN_ONCE (!va_page,"non-empty VA page list before ECREATE" )) +return -EIO ;No need for this
WARN_ONCE().arch/x86/kernel/cpu/sgx/ioctl.c static int sgx_encl_create(struct sgx_encl *encl, struct sgx_secs *secs) · line 73
Quoted 2 lines
arch/x86/kernel/cpu/sgx/ioctl.c +2
-0+ +list_add (&va_page->list, &encl->va_pages);This is fine.
Quoted 1 line
Quoted 9 lines
Quoted 6 lines
/* The extra page goes to SECS. */
encl_size=secs->size+PAGE_SIZE;Signature · 1 line
2.19.1.3.ge56e4f7/Jarkko
Will be improved in the next version.
Thanks,
Tianjia
Jarkko Sakkinen
maintainerQuoted 11 lines · Tianjia Zhang
This is an optimization of a set of sgx-related codes, each of which is independent of the patch. Because the second and third patches have conflicting dependencies, these patches are put together.
---
v3 changes:
* splitfree_cntcount and spin lock optimization into two patchesv2 changes:
* review suggested changes
Quoted 6 lines
Tianjia Zhang (5):
selftests/x86: Simplify the code to get vdso base address in sgx x86/sgx: Optimize the locking range insgx_sanitize_section()x86/sgx: Optimize thefree_cntcount insgx_epc_sectionx86/sgx: Allows ioctl PROVISION to execute before CREATE x86/sgx: Remove redundant if conditions insgx_encl_create
I remember asking about previous patches. I don't recall of getting any responses to anything basically.
/Jarkko