x86/sgx: Add missing xa_destroy() when virtual EPC is destroyed
PATCH 1/1 x86/sgx: Add missing xa_destroy() when virtual EPC is destroyed
xa_destroy() needs to be called to destroy virtual EPC's page arra before calling kfree() to free the virtual EPC. Currently it is not calaled. Add the missing xa_destroy() to fix.
--- arch/x86/kernel/cpu/sgx/virt.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/virt.c +1 -0
@@ -212,6 +212,7 @@ static int sgx_vepc_release(struct inode *inode, struct file *file)list_splice_tail (&secs_pages, &zombie_secs_pages);mutex_unlock (&zombie_secs_pages_lock); +xa_destroy (&vepc->page_array);kfree (vepc);return 0 ;
Signature · 1 line
2.31.1
Jarkko Sakkinen
maintainerQuoted 1 line · Kai Huang
xa_destroy()needs to be called to destroy virtual EPC's page arra
y
Quoted 2 lines
before calling
kfree()to free the virtual EPC. Currently it is not calaled. Add the missingxa_destroy()to fix.
called
Quoted 23 lines
Fixes 540745ddbc70 ("x86/sgx: Introduce virtual EPC for use by KVM guests")Tested-by Yang Zhong <[email protected]>Signed-off-by Kai Huang <[email protected]>--- arch/x86/kernel/cpu/sgx/virt.c | 1 + 1 file changed, 1 insertion(+)arch/x86/kernel/cpu/sgx/virt.c +1
-0@@ -212,6 +212,7 @@ static int sgx_vepc_release(struct inode *inode, struct file *file)list_splice_tail (&secs_pages, &zombie_secs_pages);mutex_unlock (&zombie_secs_pages_lock); +xa_destroy (&vepc->page_array);kfree (vepc);return 0 ;Signature · 1 line
2.31.1
/Jarkko
Quoted 7 lines · Jarkko Sakkinen
Quoted 1 line · Kai Huang
xa_destroy()needs to be called to destroy virtual EPC's page array
Quoted 2 lines
before calling
kfree()to free the virtual EPC. Currently it is not calaled. Add the missingxa_destroy()to fix.called
Thanks Jarkko. I literally need to find some way to avoid such error in future :)
Quoted 26 lines
Quoted 24 lines
Fixes 540745ddbc70 ("x86/sgx: Introduce virtual EPC for use by KVM guests")Tested-by Yang Zhong <[email protected]>Signed-off-by Kai Huang <[email protected]>--- arch/x86/kernel/cpu/sgx/virt.c | 1 + 1 file changed, 1 insertion(+)arch/x86/kernel/cpu/sgx/virt.c +1
-0@@ -212,6 +212,7 @@ static int sgx_vepc_release(struct inode *inode, struct file *file)list_splice_tail (&secs_pages, &zombie_secs_pages);mutex_unlock (&zombie_secs_pages_lock); +xa_destroy (&vepc->page_array);kfree (vepc);return 0 ;Signature · 1 line
2.31.1/Jarkko
Borislav Petkov
maintainerQuoted 1 line · Kai Huang
Thanks Jarkko. I literally need to find some way to avoid such error in future :)
That way is called "integrate checkpatch.pl into your patch creation workflow".
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 6 lines · Borislav Petkov
Quoted 1 line · Kai Huang
Thanks Jarkko. I literally need to find some way to avoid such error in future :)
That way is called "integrate checkpatch.pl into your patch creation workflow".
Thanks for suggestion. Yes I actually did the checkpatch.pl, but it didn't report typo in commit message. A little bit strange.
Borislav Petkov
maintainerQuoted 2 lines · Kai Huang
Thanks for suggestion. Yes I actually did the checkpatch.pl, but it didn't report typo in commit message. A little bit strange.
Yah, and I know it does catch typos. It seems it does so only sometimes:
$ ./scripts/checkpatch.pl --strict /tmp/kai.01 total: 0 errors, 0 warnings, 0 checks, 7 lines checked
/tmp/kai.01 has no obvious style problems and is ready for submission.
Someday soon we'll have a better way to deal with this.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 3 lines · Kai Huang
xa_destroy()needs to be called to destroy virtual EPC's page arra before callingkfree()to free the virtual EPC. Currently it is not calaled. Add the missingxa_destroy()to fix.
Looks good Kai, thanks for fixing this.
Could you please take a good look through the sgx_release() and the vpec equivalent and see if anything else stands out as possibly being missed?
Also, is this the kind of thing that a simple open/add/close selftest
might have found?
Maybe we should beef up the selftests a bit.
Quoted 9 lines · Dave Hansen
Quoted 3 lines · Kai Huang
xa_destroy()needs to be called to destroy virtual EPC's page arra before callingkfree()to free the virtual EPC. Currently it is not calaled. Add the missingxa_destroy()to fix.Looks good Kai, thanks for fixing this.
Could you please take a good look through the
sgx_release()and the vpec equivalent and see if anything else stands out as possibly being missed?
I looked over. One potential issue is both sgx_encl and sgx_vepc have 'struct mutex lock' embedded, but mutex_destroy() is not called when they are released. However I am not sure whether this is worth fixing, since mutex_destroy() is empty unless CONFIG_DEBUG_MUTEXES is turned on (even with it turned on, mutex_destroy() doesn't do anything like freeing resources so in practice there should have no problem).
Another thing is sgx_encl_release() doesn't explicitly call xa_erase() for each EPC page in encl->page_array when looping it over to free all EPC pages, but I think it is OK since xa_destroy() is called later which will destroy all xarray internal data structures. But I don't know internal implementation of xarray.
Quoted 2 lines
Also, is this the kind of thing that a simple open/add/close selftest
might have found?
It might be useful but I don't think it can detect things like xa_destroy() being missing.
Quoted 4 lines
Maybe we should beef up the selftests a bit.
Acked-by Dave Hansen <[email protected]>
Thank you!
The following commit has been merged into the x86/urgent branch of tip:
The following commit has been merged into the x86/urgent branch of tip:
Commit-ID: 4692bc775d2180a937335ccba0edce557103d44a Gitweb: https://git.kernel.org/tip/4692bc775d2180a937335ccba0edce557103d44a Author: Kai Huang <[email protected]> AuthorDate: Tue, 15 Jun 2021 22:16:39 +12:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Tue, 15 Jun 2021 18:03:45 +02:00
x86/sgx: Add missing xa_destroy() when virtual EPC is destroyed
xa_destroy() needs to be called to destroy a virtual EPC's page array before calling kfree() to free the virtual EPC. Currently it is not called so add the missing xa_destroy().
--- arch/x86/kernel/cpu/sgx/virt.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/virt.c +1 -0
@@ -212,6 +212,7 @@ static int sgx_vepc_release(struct inode *inode, struct file *file)list_splice_tail (&secs_pages, &zombie_secs_pages);mutex_unlock (&zombie_secs_pages_lock); +xa_destroy (&vepc->page_array);kfree (vepc);return 0 ;