SGX NUMA fix
v2 cover SGX NUMA fix
Hi,
A soft lockup issue was found during some SGX testing and it turned out to be a problem in SGX code related to NUMA, please see patch1/2 for details.
While Dave reviewed v1 of patch1/2, he suggested dumping an info when a node has both CPUs and memory but no EPC section, so that's patch2/2.
Comments are welcome, thanks.
v2:
- Enhance changelog for patch1/2 according to Kai, Dave and Jarkko's suggestions;
- Fix Reported-by tag, it should be Gerardo. Sorry for the mistake.
- Collect review tags.
- Add patch2/2.
Aaron Lu (2):
x86/sgx: Fix deadlock in SGX NUMA node search x86/sgx: Log information when a node lacks an EPC section
arch/x86/kernel/cpu/sgx/main.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-)
Signature · 1 line
2.45.2
PATCH v2 1/2 x86/sgx: Fix deadlock in SGX NUMA node search
When the current node doesn't have an EPC section configured by firmware and all other EPC sections are used up, CPU can get stuck inside the while loop that looks for an available EPC page from remote nodes indefinitely, leading to a soft lockup. Note how nid_of_current will never be equal to nid in that while loop because nid_of_current is not set in sgx_numa_mask.
Also worth mentioning is that it's perfectly fine for the firmware not to setup an EPC section on a node. While setting up an EPC section on each node can enhance performance, it is not a requirement for functionality.
Rework the loop to start and end on *a* node that has SGX memory. This avoids the deadlock looking for the current SGX-lacking node to show up in the loop when it never will.
--- arch/x86/kernel/cpu/sgx/main.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)
arch/x86/kernel/cpu/sgx/main.c +14 -13
@@ -475,24 +475,25 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void) {struct sgx_epc_page *page;int nid_of_current =numa_node_id (); -int nid = nid_of_current; +int nid_start, nid; -if (node_isset (nid_of_current, sgx_numa_mask)) { - page =__sgx_alloc_epc_page_from_node (nid_of_current); -if (page) -return page; - } - -/* Fall back to the non-local NUMA nodes: */ -while (true) { - nid =next_node_in (nid, sgx_numa_mask); -if (nid == nid_of_current) -break ; +/* +* Try local node first. If it doesn't have an EPC section, +* fall back to the non-local NUMA nodes. +*/ +if (node_isset (nid_of_current, sgx_numa_mask)) + nid_start = nid_of_current; +else + nid_start =next_node_in (nid_of_current, sgx_numa_mask); + nid = nid_start; +do { page =__sgx_alloc_epc_page_from_node (nid);if (page)return page; - } + + nid =next_node_in (nid, sgx_numa_mask); + }while (nid != nid_start);return ERR_PTR (-ENOMEM ); }
Signature · 1 line
2.45.2
Re: [PATCH v2 1/2] x86/sgx: Fix deadlock in SGX NUMA node search
Quoted 59 lines · Aaron Lu
When the current node doesn't have an EPC section configured by firmware and all other EPC sections are used up, CPU can get stuck inside the while loop that looks for an available EPC page from remote nodes indefinitely, leading to a soft lockup. Note how
nid_of_currentwill never be equal to nid in that while loop becausenid_of_currentis not set insgx_numa_mask.Also worth mentioning is that it's perfectly fine for the firmware not to setup an EPC section on a node. While setting up an EPC section on each node can enhance performance, it is not a requirement for functionality.
Rework the loop to start and end on *a* node that has SGX memory. This avoids the deadlock looking for the current SGX-lacking node to show up in the loop when it never will.
Fixes 901ddbb9ecf5 ("x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()")Reported-by "Molina Sabido, Gerardo" <[email protected]>Tested-by Zhimin Luo <[email protected]>Reviewed-by Kai Huang <[email protected]>Acked-by Dave Hansen <[email protected]>Signed-off-by Aaron Lu <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-)arch/x86/kernel/cpu/sgx/main.c +11
-12@@ -475,24 +475,25 @@ struct sgx_epc_page *__sgx_alloc_epc_page(void) {struct sgx_epc_page *page;int nid_of_current =numa_node_id (); -int nid = nid_of_current; +int nid_start, nid; -if (node_isset (nid_of_current, sgx_numa_mask)) { - page =__sgx_alloc_epc_page_from_node (nid_of_current); -if (page) -return page; - } - -/* Fall back to the non-local NUMA nodes: */ -while (true) { - nid =next_node_in (nid, sgx_numa_mask); -if (nid == nid_of_current) -break ; +/* +* Try local node first. If it doesn't have an EPC section, +* fall back to the non-local NUMA nodes. +*/ +if (node_isset (nid_of_current, sgx_numa_mask)) + nid_start = nid_of_current; +else + nid_start =next_node_in (nid_of_current, sgx_numa_mask); + nid = nid_start; + do {
arch/x86/kernel/cpu/sgx/main.c struct sgx_epc_page *__sgx_alloc_epc_page(void) · line 499
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c +3
-1page =__sgx_alloc_epc_page_from_node (nid);if (page)return page; - } + + nid =next_node_in (nid, sgx_numa_mask); + }while (nid != nid_start);return ERR_PTR (-ENOMEM ); }
Looks good to me:
BR, Jarkko
PATCH v2 2/2 x86/sgx: Log information when a node lacks an EPC section
In reply to Aaron LuFor optimized performance, firmware typically distributes EPC sections evenly across different NUMA nodes. However, there are scenarios where a node may have both CPUs and memory but no EPC section configured. For example, in an 8-socket system with a Sub-Numa-Cluster=2 setup, there are a total of 16 nodes. Given that the maximum number of supported EPC sections is 8, it is simply not feasible to assign one EPC section to each node. This configuration is not incorrect - SGX will still operate correctly; it is just not optimized from a NUMA standpoint.
For this reason, log a message when a node with both CPUs and memory lacks an EPC section. This will provide users with a hint as to why they might be experiencing less-than-ideal performance when running SGX enclaves.
--- arch/x86/kernel/cpu/sgx/main.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kernel/cpu/sgx/main.c +7 -0
@@ -848,6 +848,13 @@ static bool __init sgx_page_cache_init(void)return false; } +for_each_online_node (nid) { +if (!node_isset (nid, sgx_numa_mask) && +node_state (nid,N_MEMORY ) &&node_state (nid,N_CPU )) +pr_info ("node%d has both CPUs and memory but doesn't have an EPC section\n" , + nid); + } +return true; }
Signature · 1 line
2.45.2
Re: [PATCH v2 2/2] x86/sgx: Log information when a node lacks an EPC section
Quoted 25 lines · Aaron Lu
For optimized performance, firmware typically distributes EPC sections evenly across different NUMA nodes. However, there are scenarios where a node may have both CPUs and memory but no EPC section configured. For example, in an 8-socket system with a Sub-Numa-Cluster=2 setup, there are a total of 16 nodes. Given that the maximum number of supported EPC sections is 8, it is simply not feasible to assign one EPC section to each node. This configuration is not incorrect - SGX will still operate correctly; it is just not optimized from a NUMA standpoint.
For this reason, log a message when a node with both CPUs and memory lacks an EPC section. This will provide users with a hint as to why they might be experiencing less-than-ideal performance when running SGX enclaves.
Suggested-by Dave Hansen <[email protected]>Signed-off-by Aaron Lu <[email protected]>--- arch/x86/kernel/cpu/sgx/main.c | 7 +++++++ 1 file changed, 7 insertions(+)arch/x86/kernel/cpu/sgx/main.c +0
-0
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 855
Quoted 8 lines
arch/x86/kernel/cpu/sgx/main.c +5
-0@@ -848,6 +848,13 @@ static bool __init sgx_page_cache_init(void)return false; } +for_each_online_node (nid) { +if (!node_isset (nid, sgx_numa_mask) && +node_state (nid,N_MEMORY ) &&node_state (nid,N_CPU )) +pr_info ("node%d has both CPUs and memory but doesn't have an EPC section\n" , + nid);
Is this enough, or is there anything that would need to be done automatically if this happens? With a tracepoint you could react to such even but I'm totally fine with this.
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 860
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+ } +return true; }
BR, Jarkko
Re: [PATCH v2 2/2] x86/sgx: Log information when a node lacks an EPC section
Quoted 8 lines · Jarkko Sakkinen
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 855
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c +5
-0+for_each_online_node (nid) { +if (!node_isset (nid, sgx_numa_mask) && +node_state (nid,N_MEMORY ) &&node_state (nid,N_CPU )) +pr_info ("node%d has both CPUs and memory but doesn't have an EPC section\n" , + nid);Is this enough, or is there anything that would need to be done automatically if this happens? With a tracepoint you could react to such even but I'm totally fine with this.
There's always the theoretical chance that there are nodes being hotplugged or that this is all running in a VM that has some whacky topology.
But this simple pr_info() provides good coverage for the common cases and shouldn't be too much of a burden for the weridos.
Re: [PATCH v2 2/2] x86/sgx: Log information when a node lacks an EPC section
In reply to Aaron LuQuoted 9 lines · Aaron Lu
For optimized performance, firmware typically distributes EPC sections evenly across different NUMA nodes. However, there are scenarios where a node may have both CPUs and memory but no EPC section configured. For example, in an 8-socket system with a Sub-Numa-Cluster=2 setup, there are a total of 16 nodes. Given that the maximum number of supported EPC sections is 8, it is simply not feasible to assign one EPC section to each node. This configuration is not incorrect - SGX will still operate correctly; it is just not optimized from a NUMA standpoint.
Quoted 7 lines
For this reason, log a message when a node with both CPUs and memory lacks an EPC section. This will provide users with a hint as to why they might be experiencing less-than-ideal performance when running SGX enclaves.
Suggested-by Dave Hansen <[email protected]>Signed-off-by Aaron Lu <[email protected]>