Skip to contentSkip to conversation

x86/sgx: Add missing xa_destroy() when virtual EPC is destroyed

PATCH Applied Kai Huang 5 participants 9 messages Jun 15, 2021 – Jun 18, 2021

Subsystems (2)

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Kai Huang

author
#0 lore raw

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.

Fixes 540745ddbc70 ("x86/sgx: Introduce virtual EPC for use by KVM guests")
---
 arch/x86/kernel/cpu/sgx/virt.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/virt.c +1 -0
index 6ad165a5c0cc..64511c4a5200 100644
@@ -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
maintainer
#1 lore raw
Quoted 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 missing xa_destroy() to fix.

called

Quoted 23 lines
Fixes 540745ddbc70 ("x86/sgx: Introduce virtual EPC for use by KVM guests")
---
 arch/x86/kernel/cpu/sgx/virt.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/virt.c +1 -0
index 6ad165a5c0cc..64511c4a5200 100644
@@ -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

author
#2 lore raw
Quoted 7 lines · Jarkko Sakkinen
Quoted 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 missing xa_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")
---
 arch/x86/kernel/cpu/sgx/virt.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/virt.c +1 -0
index 6ad165a5c0cc..64511c4a5200 100644
@@ -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

maintainer
#3 lore raw
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".

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
author
#4 lore raw
Quoted 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.

maintainer
#5 lore raw
Quoted 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-netiquette
Acked-by on v1 1/1
#6 lore raw
In reply to Kai Huang
Quoted 3 lines · Kai Huang

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.

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.

author
#7 lore raw
Quoted 9 lines · Dave Hansen
Quoted 3 lines · Kai Huang

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.

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.

Thank you!

merge bot Applied
#8 lore raw
In reply to Kai Huang
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().

Fixes 540745ddbc70 ("x86/sgx: Introduce virtual EPC for use by KVM guests")
---
 arch/x86/kernel/cpu/sgx/virt.c | 1 +
 1 file changed, 1 insertion(+)
arch/x86/kernel/cpu/sgx/virt.c +1 -0
index 6ad165a..64511c4 100644
@@ -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;