x86/sgx: Avoid returning NULL in __sgx_alloc_epc_page()
PATCH 1/1 x86/sgx: Avoid returning NULL in __sgx_alloc_epc_page()
Below kernel bug happened when running simple SGX application when EPC is under pressure. The root cause is with commit 5b8719504e3a ("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()"), __sgx_alloc_epc_page() returns NULL when there's no free EPC page can be allocated, while old behavior was it always returned ERR_PTR(-ENOMEM) in such case.
Fix by directly returning the page if __sgx_alloc_epc_page_from_node() allocates a valid page in fallback to non-local allocation, and always returning ERR_PTR(-ENOMEM) if no EPC page can be allocated.
[ 253.474764] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 253.500101] #PF: supervisor write access in kernel mode [ 253.525462] #PF: error_code(0x0002) - not-present page ... [ 254.102041] Call Trace: [ 254.126699] sgx_ioc_enclave_add_pages+0x241/0x770 [ 254.151305] sgx_ioctl+0x194/0x4b0 [ 254.174976] ? handle_mm_fault+0xd0/0x260 [ 254.198470] ? do_user_addr_fault+0x1ef/0x570 [ 254.221827] __x64_sys_ioctl+0x91/0xc0 [ 254.244546] do_syscall_64+0x38/0x90 [ 254.266728] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 254.289232] RIP: 0033:0x7fdc4cf4031b ... [ 254.711480] CR2: 0000000000000008 [ 254.735494] ---[ end trace 970dce6d4cdf7f64 ]--- [ 254.759915] RIP: 0010:sgx_alloc_epc_page+0x46/0x152
...
--- arch/x86/kernel/cpu/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +2 -2
@@ -508,10 +508,10 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void) page =__sgx_alloc_epc_page_from_node (nid);if (page) -break ; +return page; } -return page; +return ERR_PTR (-ENOMEM ); }/**
Signature · 1 line
2.30.2
Quoted 28 lines · Kai Huang
Below kernel bug happened when running simple SGX application when EPC is under pressure. The root cause is with commit
5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme tosgx_alloc_epc_page()"),__sgx_alloc_epc_page()returns NULL when there's no free EPC page can be allocated, while old behavior was it always returnedERR_PTR(-ENOMEM)in such case.Fix by directly returning the page if
__sgx_alloc_epc_page_from_node()allocates a valid page in fallback to non-local allocation, and always returningERR_PTR(-ENOMEM)if no EPC page can be allocated.[ 253.474764] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 253.500101] #PF: supervisor write access in kernel mode [ 253.525462] #PF: error_code(0x0002) - not-present page ... [ 254.102041] Call Trace: [ 254.126699] sgx_ioc_enclave_add_pages+0x241/0x770 [ 254.151305] sgx_ioctl+0x194/0x4b0 [ 254.174976] ? handle_mm_fault+0xd0/0x260 [ 254.198470] ? do_user_addr_fault+0x1ef/0x570 [ 254.221827] __x64_sys_ioctl+0x91/0xc0 [ 254.244546] do_syscall_64+0x38/0x90 [ 254.266728] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 254.289232] RIP: 0033:0x7fdc4cf4031b ... [ 254.711480] CR2: 0000000000000008 [ 254.735494] ---[ end trace 970dce6d4cdf7f64 ]--- [ 254.759915] RIP: 0010:sgx_alloc_epc_page+0x46/0x152
Quoted 4 lines
...
Fixes 5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()")Signed-off-by Kai Huang <[email protected]>
Quoted 25 lines
--- arch/x86/kernel/cpu/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)arch/x86/kernel/cpu/sgx/main.c +2
-2@@ -508,10 +508,10 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void) page =__sgx_alloc_epc_page_from_node (nid);if (page) -break ; +return page; } -return page; +return ERR_PTR (-ENOMEM ); }/** Signature · 1 line
2.30.2
/Jarkko
Quoted 28 lines · Kai Huang
Below kernel bug happened when running simple SGX application when EPC is under pressure. The root cause is with commit
5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme tosgx_alloc_epc_page()"),__sgx_alloc_epc_page()returns NULL when there's no free EPC page can be allocated, while old behavior was it always returnedERR_PTR(-ENOMEM)in such case.Fix by directly returning the page if
__sgx_alloc_epc_page_from_node()allocates a valid page in fallback to non-local allocation, and always returningERR_PTR(-ENOMEM)if no EPC page can be allocated.[ 253.474764] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 253.500101] #PF: supervisor write access in kernel mode [ 253.525462] #PF: error_code(0x0002) - not-present page ... [ 254.102041] Call Trace: [ 254.126699] sgx_ioc_enclave_add_pages+0x241/0x770 [ 254.151305] sgx_ioctl+0x194/0x4b0 [ 254.174976] ? handle_mm_fault+0xd0/0x260 [ 254.198470] ? do_user_addr_fault+0x1ef/0x570 [ 254.221827] __x64_sys_ioctl+0x91/0xc0 [ 254.244546] do_syscall_64+0x38/0x90 [ 254.266728] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 254.289232] RIP: 0033:0x7fdc4cf4031b ... [ 254.711480] CR2: 0000000000000008 [ 254.735494] ---[ end trace 970dce6d4cdf7f64 ]--- [ 254.759915] RIP: 0010:sgx_alloc_epc_page+0x46/0x152
Quoted 7 lines
...
Fixes 5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()")Signed-off-by Kai Huang <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
I was on the verge whether to merge that into the original patch since it is the top patch on the branch or create a new one but opted for former because this way it won't break bisection and people won't have to pay attention whether there's a fix patch to the NUMA patch too, in case they wanna backport and whatnot.
Here's the new version:
Thx.
---
From 323950a8a98b492ac2fa168e8e4c0becfb4554dd Mon Sep 17 00:00:00 2001
From: Jarkko Sakkinen <[email protected]>
Date: Thu, 18 Mar 2021 01:53:31 +0200
Subject: [PATCH] x86/sgx: Add a basic NUMA allocation scheme to
sgx_alloc_epc_page()
Background
==========
SGX enclave memory is enumerated by the processor in contiguous physical ranges called Enclave Page Cache (EPC) sections. Currently, there is a free list per section, but allocations simply target the lowest-numbered sections. This is functional, but has no NUMA awareness.
Fortunately, EPC sections are covered by entries in the ACPI SRAT table. These entries allow each EPC section to be associated with a NUMA node, just like normal RAM.
Solution
========
Implement a NUMA-aware enclave page allocator. Mirror the buddy allocator and maintain a list of enclave pages for each NUMA node. Attempt to allocate enclave memory first from local nodes, then fall back to other nodes.
Note that the fallback is not as sophisticated as the buddy allocator and is itself not aware of NUMA distances. When a node's free list is empty, it searches for the next-highest node with enclave pages (and will wrap if necessary). This could be improved in the future.
Other
=====
NUMA_KEEP_MEMINFO dependency is required for phys_to_target_node().
[ Kai Huang: Do not return NULL from __sgx_alloc_epc_page() because
callers do not expect that and that leads to a NULL ptr deref. ]
--- arch/x86/Kconfig | 1 + arch/x86/kernel/cpu/sgx/main.c | 119 +++++++++++++++++++++------------ arch/x86/kernel/cpu/sgx/sgx.h | 16 +++-- 3 files changed, 88 insertions(+), 48 deletions(-)
arch/x86/Kconfig +1 -0
@@ -1931,6 +1931,7 @@ config X86_SGX depends on CRYPTO_SHA256=y select SRCU select MMU_NOTIFIER + select NUMA_KEEP_MEMINFO if NUMA help Intel(R) Software Guard eXtensions (SGX) is a set of CPU instructions that can be used by applications to set aside private regions of code
arch/x86/kernel/cpu/sgx/main.c +77 -42
210 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/sgx.h +10 -6
@@ -29,22 +29,26 @@ struct sgx_epc_page {struct list_head list; }; +/* +* Contains the tracking data for NUMA nodes having EPC pages. Most importantly, +* the free page list local to the node is stored here. +*/ +struct sgx_numa_node { +struct list_head free_page_list; +spinlock_t lock; +}; +/* * The firmware can define multiple chunks of EPC to the different areas of the * physical memory e.g. for memory areas of the each node. This structure is * used to store EPC pages for one EPC section and virtual memory area where * the pages have been mapped. -* -* 'lock' must be held before accessing 'page_list' or 'free_cnt'. */ struct sgx_epc_section {unsigned long phys_addr;void *virt_addr;struct sgx_epc_page *pages; - -spinlock_t lock; -struct list_head page_list; -unsigned long free_cnt; +struct sgx_numa_node *node; };extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS ];
Signature · 7 lines
2.29.2
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 37 lines · On Fri, 19 Mar 2021 09:45:23 +0100 Borislav Petkov wrote:
Quoted 28 lines · Kai Huang
Below kernel bug happened when running simple SGX application when EPC is under pressure. The root cause is with commit
5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme tosgx_alloc_epc_page()"),__sgx_alloc_epc_page()returns NULL when there's no free EPC page can be allocated, while old behavior was it always returnedERR_PTR(-ENOMEM)in such case.Fix by directly returning the page if
__sgx_alloc_epc_page_from_node()allocates a valid page in fallback to non-local allocation, and always returningERR_PTR(-ENOMEM)if no EPC page can be allocated.[ 253.474764] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 253.500101] #PF: supervisor write access in kernel mode [ 253.525462] #PF: error_code(0x0002) - not-present page ... [ 254.102041] Call Trace: [ 254.126699] sgx_ioc_enclave_add_pages+0x241/0x770 [ 254.151305] sgx_ioctl+0x194/0x4b0 [ 254.174976] ? handle_mm_fault+0xd0/0x260 [ 254.198470] ? do_user_addr_fault+0x1ef/0x570 [ 254.221827] __x64_sys_ioctl+0x91/0xc0 [ 254.244546] do_syscall_64+0x38/0x90 [ 254.266728] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 254.289232] RIP: 0033:0x7fdc4cf4031b ... [ 254.711480] CR2: 0000000000000008 [ 254.735494] ---[ end trace 970dce6d4cdf7f64 ]--- [ 254.759915] RIP: 0010:sgx_alloc_epc_page+0x46/0x152Quoted 7 lines
...
Fixes 5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()")Signed-off-by Kai Huang <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 5 lines
I was on the verge whether to merge that into the original patch since it is the top patch on the branch or create a new one but opted for former because this way it won't break bisection and people won't have to pay attention whether there's a fix patch to the NUMA patch too, in case they wanna backport and whatnot.
Sure.
[...]
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_reclaimer_init(void) · line 509
Quoted 12 lines
arch/x86/kernel/cpu/sgx/main.c +7
-1+ +/* Fall back to the non-local NUMA nodes: */ +while (true) { + nid =next_node_in (nid, sgx_numa_mask); +if (nid == nid_of_current) +break ; - page =__sgx_alloc_epc_page_from_section (section); + page =__sgx_alloc_epc_page_from_node (nid);if (page)return page; }
It seems "return ERR_PTR(-ENOMEM)" is missing at the bottom of this function?
Quoted 55 lines · On Fri, 19 Mar 2021 22:01:41 +1300 Kai Huang wrote:
Quoted 37 lines · On Fri, 19 Mar 2021 09:45:23 +0100 Borislav Petkov wrote:
Quoted 28 lines · Kai Huang
Below kernel bug happened when running simple SGX application when EPC is under pressure. The root cause is with commit
5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme tosgx_alloc_epc_page()"),__sgx_alloc_epc_page()returns NULL when there's no free EPC page can be allocated, while old behavior was it always returnedERR_PTR(-ENOMEM)in such case.Fix by directly returning the page if
__sgx_alloc_epc_page_from_node()allocates a valid page in fallback to non-local allocation, and always returningERR_PTR(-ENOMEM)if no EPC page can be allocated.[ 253.474764] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 253.500101] #PF: supervisor write access in kernel mode [ 253.525462] #PF: error_code(0x0002) - not-present page ... [ 254.102041] Call Trace: [ 254.126699] sgx_ioc_enclave_add_pages+0x241/0x770 [ 254.151305] sgx_ioctl+0x194/0x4b0 [ 254.174976] ? handle_mm_fault+0xd0/0x260 [ 254.198470] ? do_user_addr_fault+0x1ef/0x570 [ 254.221827] __x64_sys_ioctl+0x91/0xc0 [ 254.244546] do_syscall_64+0x38/0x90 [ 254.266728] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 254.289232] RIP: 0033:0x7fdc4cf4031b ... [ 254.711480] CR2: 0000000000000008 [ 254.735494] ---[ end trace 970dce6d4cdf7f64 ]--- [ 254.759915] RIP: 0010:sgx_alloc_epc_page+0x46/0x152Quoted 7 lines
...
Fixes 5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()")Signed-off-by Kai Huang <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 5 lines
I was on the verge whether to merge that into the original patch since it is the top patch on the branch or create a new one but opted for former because this way it won't break bisection and people won't have to pay attention whether there's a fix patch to the NUMA patch too, in case they wanna backport and whatnot.
Sure.
[...]
Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c +6
-0+ +/* Fall back to the non-local NUMA nodes: */ +while (true) { + nid =next_node_in (nid, sgx_numa_mask); +if (nid == nid_of_current) +break ;
Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_reclaimer_init(void) · line 509
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +1
-1- page =__sgx_alloc_epc_page_from_section (section); + page =__sgx_alloc_epc_page_from_node (nid);if (page)return page; }It seems "return
ERR_PTR(-ENOMEM)" is missing at the bottom of this function?
Oh my fault. This line is not shown in patch probably due to it is not changed by this patch. Please ignore this.
Jarkko Sakkinen
maintainerQuoted 43 lines · Borislav Petkov
Quoted 28 lines · Kai Huang
Below kernel bug happened when running simple SGX application when EPC is under pressure. The root cause is with commit
5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme tosgx_alloc_epc_page()"),__sgx_alloc_epc_page()returns NULL when there's no free EPC page can be allocated, while old behavior was it always returnedERR_PTR(-ENOMEM)in such case.Fix by directly returning the page if
__sgx_alloc_epc_page_from_node()allocates a valid page in fallback to non-local allocation, and always returningERR_PTR(-ENOMEM)if no EPC page can be allocated.[ 253.474764] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 253.500101] #PF: supervisor write access in kernel mode [ 253.525462] #PF: error_code(0x0002) - not-present page ... [ 254.102041] Call Trace: [ 254.126699] sgx_ioc_enclave_add_pages+0x241/0x770 [ 254.151305] sgx_ioctl+0x194/0x4b0 [ 254.174976] ? handle_mm_fault+0xd0/0x260 [ 254.198470] ? do_user_addr_fault+0x1ef/0x570 [ 254.221827] __x64_sys_ioctl+0x91/0xc0 [ 254.244546] do_syscall_64+0x38/0x90 [ 254.266728] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 254.289232] RIP: 0033:0x7fdc4cf4031b ... [ 254.711480] CR2: 0000000000000008 [ 254.735494] ---[ end trace 970dce6d4cdf7f64 ]--- [ 254.759915] RIP: 0010:sgx_alloc_epc_page+0x46/0x152Quoted 7 lines
...
Fixes 5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()")Signed-off-by Kai Huang <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)I was on the verge whether to merge that into the original patch since it is the top patch on the branch or create a new one but opted for former because this way it won't break bisection and people won't have to pay attention whether there's a fix patch to the NUMA patch too, in case they wanna backport and whatnot.
Quoted 4 lines
Here's the new version:
Thx.
Yeah, I'd also prefer to keep Dave's and Kai's patches as separate entitied.
Just in case, this was the Dave's fix:
https://patchwork.kernel.org/project/intel-sgx/patch/[email protected]/
/Jarkko
Jarkko Sakkinen
maintainerQuoted 55 lines · Kai Huang
Quoted 37 lines · On Fri, 19 Mar 2021 09:45:23 +0100 Borislav Petkov wrote:
Quoted 28 lines · Kai Huang
Below kernel bug happened when running simple SGX application when EPC is under pressure. The root cause is with commit
5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme tosgx_alloc_epc_page()"),__sgx_alloc_epc_page()returns NULL when there's no free EPC page can be allocated, while old behavior was it always returnedERR_PTR(-ENOMEM)in such case.Fix by directly returning the page if
__sgx_alloc_epc_page_from_node()allocates a valid page in fallback to non-local allocation, and always returningERR_PTR(-ENOMEM)if no EPC page can be allocated.[ 253.474764] BUG: kernel NULL pointer dereference, address: 0000000000000008 [ 253.500101] #PF: supervisor write access in kernel mode [ 253.525462] #PF: error_code(0x0002) - not-present page ... [ 254.102041] Call Trace: [ 254.126699] sgx_ioc_enclave_add_pages+0x241/0x770 [ 254.151305] sgx_ioctl+0x194/0x4b0 [ 254.174976] ? handle_mm_fault+0xd0/0x260 [ 254.198470] ? do_user_addr_fault+0x1ef/0x570 [ 254.221827] __x64_sys_ioctl+0x91/0xc0 [ 254.244546] do_syscall_64+0x38/0x90 [ 254.266728] entry_SYSCALL_64_after_hwframe+0x44/0xae [ 254.289232] RIP: 0033:0x7fdc4cf4031b ... [ 254.711480] CR2: 0000000000000008 [ 254.735494] ---[ end trace 970dce6d4cdf7f64 ]--- [ 254.759915] RIP: 0010:sgx_alloc_epc_page+0x46/0x152Quoted 7 lines
...
Fixes 5b8719504e3a("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()")Signed-off-by Kai Huang <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)Quoted 5 lines
I was on the verge whether to merge that into the original patch since it is the top patch on the branch or create a new one but opted for former because this way it won't break bisection and people won't have to pay attention whether there's a fix patch to the NUMA patch too, in case they wanna backport and whatnot.
Sure.
[...]
Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c +6
-0+ +/* Fall back to the non-local NUMA nodes: */ +while (true) { + nid =next_node_in (nid, sgx_numa_mask); +if (nid == nid_of_current) +break ;
Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_reclaimer_init(void) · line 509
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +1
-1- page =__sgx_alloc_epc_page_from_section (section); + page =__sgx_alloc_epc_page_from_node (nid);if (page)return page; }It seems "return
ERR_PTR(-ENOMEM)" is missing at the bottom of this function?
I understood Boris' comment that the fixes would not be squashed.
/Jarkko
Borislav Petkov
maintainerQuoted 5 lines · Jarkko Sakkinen
Quoted 5 lines
Quoted 5 lines
I was on the verge whether to merge that into the original patch since it is the top patch on the branch or create a new one but opted for former because this way it won't break bisection and people won't have to pay attention whether there's a fix patch to the NUMA patch too, in case they wanna backport and whatnot.
...
Quoted 1 line
I understood Boris' comment that the fixes would not be squashed.
Please read it again and let me know what about it is not clear. It is at the beginning of this mail.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteJarkko Sakkinen
maintainerQuoted 7 lines · Borislav Petkov
Quoted 5 lines · Jarkko Sakkinen
Quoted 5 lines
Quoted 5 lines
I was on the verge whether to merge that into the original patch since it is the top patch on the branch or create a new one but opted for former because this way it won't break bisection and people won't have to pay attention whether there's a fix patch to the NUMA patch too, in case they wanna backport and whatnot.
Quoted 6 lines
...
Quoted 1 line
I understood Boris' comment that the fixes would not be squashed.
Please read it again and let me know what about it is not clear. It is at the beginning of this mail.
I did misread it for the first time.
So let's sanity: you *are* going to squash the patches together because that way it's factors easier to backport the whole thing?
Is this the correct understanding?
Fair point, I can cope with that.
Quoted 6 lines
Signature · 4 lines
Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
/Jarkko
Borislav Petkov
maintainerQuoted 6 lines · Jarkko Sakkinen
I did misread it for the first time.
So let's sanity: you *are* going to squash the patches together because that way it's factors easier to backport the whole thing?
Is this the correct understanding?
I squashed Kai's fix because I don't want to break people's bisection if they land between your patch and his fix. They're already troubled enough chasing an issue, don't want to have them get a NULL ptr in sgx land.
Now, looking at dhansen's fix: what can happen if nid is uninitialized? AFAICT, we'll end up in
static inline int __next_node(int n, const nodemask_t *srcp)
{
return min_t(int,MAX_NUMNODES,find_next_bit(srcp->bits, MAX_NUMNODES, n+1));
}with n uninitialized and depending on its value it'll either return MAX_NUMNODES so we'll try to allocate on the first node or try to allocate on some other node.
Now, if you think that that is still problematic enough for enclave creation, then I'll fold his patch too.
So yes, the main reason is usability and not breaking bisection.
So, what would you prefer?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteQuoted 23 lines · Borislav Petkov
Quoted 6 lines · Jarkko Sakkinen
I did misread it for the first time.
So let's sanity: you *are* going to squash the patches together because that way it's factors easier to backport the whole thing?
Is this the correct understanding?
I squashed Kai's fix because I don't want to break people's bisection if they land between your patch and his fix. They're already troubled enough chasing an issue, don't want to have them get a NULL ptr in sgx land.
Now, looking at dhansen's fix: what can happen if nid is uninitialized? AFAICT, we'll end up in
static inline int
__next_node(int n, const nodemask_t *srcp){ return min_t(int,MAX_NUMNODES,find_next_bit(srcp->bits, MAX_NUMNODES, n+1)); }with n uninitialized and depending on its value it'll either return
MAX_NUMNODESso we'll try to allocate on the first node or try to allocate on some other node.
Quoted 6 lines
Now, if you think that that is still problematic enough for enclave creation, then I'll fold his patch too.
So yes, the main reason is usability and not breaking bisection.
So, what would you prefer?
It's probably best to squash my patch in. The uninitialized value could *theoretically* cause the search to start at the wrong node and then end before every node has been visited. That could cause premature allocation failures.
But, I seriously doubt anyone will notice either way.
Borislav Petkov
maintainerQuoted 6 lines · Dave Hansen
It's probably best to squash my patch in. The uninitialized value could *theoretically* cause the search to start at the wrong node and then end before every node has been visited. That could cause premature allocation failures.
But, I seriously doubt anyone will notice either way.
Yeah, I think too that we should be on the safe side, just in case. Lemme redo the branch with the new changes and test.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette[tip: x86/sgx] x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()
In reply to Kai HuangThe following commit has been merged into the x86/sgx branch of tip:
The following commit has been merged into the x86/sgx branch of tip:
Commit-ID: 901ddbb9ecf5425183ea0c09d10c2fd7868dce54 Gitweb: https://git.kernel.org/tip/901ddbb9ecf5425183ea0c09d10c2fd7868dce54 Author: Jarkko Sakkinen <[email protected]> AuthorDate: Thu, 18 Mar 2021 01:53:31 +02:00 Committer: Borislav Petkov <[email protected]> CommitterDate: Fri, 19 Mar 2021 19:16:51 +01:00
x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()
Background
==========
SGX enclave memory is enumerated by the processor in contiguous physical ranges called Enclave Page Cache (EPC) sections. Currently, there is a free list per section, but allocations simply target the lowest-numbered sections. This is functional, but has no NUMA awareness.
Fortunately, EPC sections are covered by entries in the ACPI SRAT table. These entries allow each EPC section to be associated with a NUMA node, just like normal RAM.
Solution
========
Implement a NUMA-aware enclave page allocator. Mirror the buddy allocator and maintain a list of enclave pages for each NUMA node. Attempt to allocate enclave memory first from local nodes, then fall back to other nodes.
Note that the fallback is not as sophisticated as the buddy allocator and is itself not aware of NUMA distances. When a node's free list is empty, it searches for the next-highest node with enclave pages (and will wrap if necessary). This could be improved in the future.
Other
=====
NUMA_KEEP_MEMINFO dependency is required for phys_to_target_node().
[ Kai Huang: Do not return NULL from __sgx_alloc_epc_page() because
callers do not expect that and that leads to a NULL ptr deref. ]
[ dhansen: Fix an uninitialized 'nid' variable in
__sgx_alloc_epc_page() as
to avoid any potential allocations from the wrong NUMA node or even premature allocation failures. ]
--- arch/x86/Kconfig | 1 +- arch/x86/kernel/cpu/sgx/main.c | 119 ++++++++++++++++++++------------ arch/x86/kernel/cpu/sgx/sgx.h | 16 ++-- 3 files changed, 88 insertions(+), 48 deletions(-)
arch/x86/Kconfig +1 -0
@@ -1931,6 +1931,7 @@ config X86_SGX depends on CRYPTO_SHA256=y select SRCU select MMU_NOTIFIER + select NUMA_KEEP_MEMINFO if NUMA help Intel(R) Software Guard eXtensions (SGX) is a set of CPU instructions that can be used by applications to set aside private regions of code
arch/x86/kernel/cpu/sgx/main.c +77 -42
210 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/sgx.h +10 -6
@@ -30,21 +30,25 @@ struct sgx_epc_page { };/* +* Contains the tracking data for NUMA nodes having EPC pages. Most importantly, +* the free page list local to the node is stored here. +*/ +struct sgx_numa_node { +struct list_head free_page_list; +spinlock_t lock; +}; + +/* * The firmware can define multiple chunks of EPC to the different areas of the * physical memory e.g. for memory areas of the each node. This structure is * used to store EPC pages for one EPC section and virtual memory area where * the pages have been mapped. -* -* 'lock' must be held before accessing 'page_list' or 'free_cnt'. */ struct sgx_epc_section {unsigned long phys_addr;void *virt_addr;struct sgx_epc_page *pages; - -spinlock_t lock; -struct list_head page_list; -unsigned long free_cnt; +struct sgx_numa_node *node; };extern struct sgx_epc_section sgx_epc_sections[SGX_MAX_EPC_SECTIONS ];