x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()
Jarkko Sakkinen
maintainerPATCH 1/1 x86/sgx: Add a basic NUMA allocation scheme to sgx_alloc_epc_page()
Background
==========
EPC section is covered by one or more SRAT entries that are associated with one and only one PXM (NUMA node). The motivation behind this patch is to provide basic elements of building allocation scheme based on this premise.
It does not try to fully address NUMA. For instance, it does not provide integration to the mempolicy API, but neither does introduce any bottlenecks to address this later on. Memory allocation is a complex topic, and thus it's better to start with baby steps.
Solution
========
Use phys_to_target_node() to associate each NUMA node with the EPC sections contained within its range.
In sgx_alloc_epc_page(), first try to allocate from the NUMA node, where the CPU is executing. If that fails, fallback to the legacy allocation.
--- arch/x86/Kconfig | 1 + arch/x86/kernel/cpu/sgx/main.c | 52 +++++++++++++++++++++++++++++++++- arch/x86/kernel/cpu/sgx/sgx.h | 1 + 3 files changed, 53 insertions(+), 1 deletion(-)
arch/x86/Kconfig +1 -0
@@ -1941,6 +1941,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 +51 -1
90 lines not on this page · raw message
arch/x86/kernel/cpu/sgx/sgx.h +1 -0
@@ -45,6 +45,7 @@ struct sgx_epc_section {spinlock_t lock;struct list_head page_list;unsigned long free_cnt; +struct list_head section_list;/* * Pages which need EREMOVE run on them before they can be
Signature · 1 line
2.30.1
arch/x86/kernel/cpu/sgx/main.c static int sgx_nr_epc_sections; · line 22
Quoted 2 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+/* Nodes with one or more EPC sections. */ +static nodemask_t sgx_numa_mask;
I'd also add that this is for optimization only.
arch/x86/kernel/cpu/sgx/main.c static int sgx_nr_epc_sections; · line 25
Quoted 2 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+/* Array of lists of EPC sections for each NUMA node. */ +struct list_head *sgx_numa_nodes;
I'd much prefer:
/*
* Array with one list_head for each possible NUMA node. Each
* list contains all the sgx_epc_section's which are on that
* node.
*/
Otherwise, it's hard to imagine what this structure looks like.
Quoted 32 lines
/*
* These variables are part of the state of the reclaimer, and must be accessed
* withsgx_reclaimer_lockacquired.arch/x86/kernel/cpu/sgx/main.c +20
-0@@ -473,6 +479,26 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_secreturn page; } +static struct sgx_epc_page *__sgx_alloc_epc_page_from_node (int nid) +{ +struct sgx_epc_section *section; +struct sgx_epc_page *page; + +if (WARN_ON_ONCE (nid <0 || nid >=MAX_NUMNODES )) +return NULL ; + +if (!node_isset (nid, sgx_numa_mask)) +return NULL ; + +list_for_each_entry (section, &sgx_numa_nodes[nid], section_list) { + page =__sgx_alloc_epc_page_from_section (section); +if (page) +return page; + } + +return NULL ; +} +/** * __sgx_alloc_epc_page() - Allocate an EPC page * @@ -485,13 +511,17 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec*/
arch/x86/kernel/cpu/sgx/main.c static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec · line 521
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c +4
-0struct sgx_epc_page *__sgx_alloc_epc_page (void ) { +int current_nid =numa_node_id ();struct sgx_epc_section *section;struct sgx_epc_page *page;int i; + page =__sgx_alloc_epc_page_from_node (current_nid); +if (page) +return page;
Comments, please.
/* Try to allocate EPC from the current node, first: */
then:
/* Search all EPC sections, ignoring locality: */
arch/x86/kernel/cpu/sgx/main.c static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec · line 527
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c +0
-1for (i =0 ; i < sgx_nr_epc_sections; i++) { section = &sgx_epc_sections[i]; - page =__sgx_alloc_epc_page_from_section (section);if (page)return page;
This still has the problem that it exerts too much pressure on the low-numbered sgx_epc_sections[]. If a node's sections are full, it always tries to go after sgx_epc_sections[0].
It can be in another patch, but I think the *minimal* thing we can do here for a NUMA allocator is to try to at least balance the allocations.
Instead of having a for-each-section loop, I'd make it for-each-node -> for-each-section. Something like:
for (i = 0; i < num_possible_nodes(); i++) {
node = (numa_node_id() + i) % num_possible_nodes()
if (!node_isset(nid, sgx_numa_mask))
continue;
list_for_each_entry(section, &sgx_numa_nodes[nid],
section_list) {
__sgx_alloc_epc_page_from_section(section)
}
}Then you have a single loop instead of a "try local then a fall back".
Also, that "node++" thing might be able to use next_online_node().
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 702
Quoted 9 lines
arch/x86/kernel/cpu/sgx/main.c +3
-0@@ -665,8 +695,12 @@ static bool __init sgx_page_cache_init(void) {u32 eax, ebx, ecx, edx, type;u64 pa, size; +int nid;int i; +nodes_clear (sgx_numa_mask); + sgx_numa_nodes =kmalloc_array (MAX_NUMNODES ,sizeof (*sgx_numa_nodes),GFP_KERNEL );
MAX_NUMNODES will always be the largest compile-time constant. That's 4k, IIRC. num_possible_nodes() might be as small as 1 if NUMA is off.
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 732
Quoted 13 lines
arch/x86/kernel/cpu/sgx/main.c +6
-0for (i =0 ; i <ARRAY_SIZE (sgx_epc_sections); i++) {cpuid_count (SGX_CPUID , i +SGX_CPUID_EPC , &eax, &ebx, &ecx, &edx);@@ -690,6 +724,22 @@ static bool __init sgx_page_cache_init(void) } sgx_nr_epc_sections++; + + nid =numa_map_to_online_node (phys_to_target_node (pa)); + +if (nid ==NUMA_NO_NODE ) { +pr_err (FW_BUG "unable to map EPC section %d to online node.\n" , nid); + nid =0 ;
Could we dump out the physical address there? I think that's even more informative than a section number.
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 735
Quoted 3 lines
arch/x86/kernel/cpu/sgx/main.c +3
-0+ }else if (WARN_ON_ONCE (nid <0 || nid >=MAX_NUMNODES )) { + nid =0 ; + }
I'm not sure we really need to check for these. If we're worried about the firmware returning these, I'd expect numa_map_to_online_node() to sanity check them for us.
Quoted 14 lines
arch/x86/kernel/cpu/sgx/main.c +6
-0+if (!node_isset (nid, sgx_numa_mask)) { +INIT_LIST_HEAD (&sgx_numa_nodes[nid]); +node_set (nid, sgx_numa_mask); + } + +list_add_tail (&sgx_epc_sections[i].section_list, &sgx_numa_nodes[nid]); }if (!sgx_nr_epc_sections) {arch/x86/kernel/cpu/sgx/sgx.h +0
-0
arch/x86/kernel/cpu/sgx/sgx.h struct sgx_epc_section { · line 48
Quoted 4 lines
arch/x86/kernel/cpu/sgx/sgx.h +1
-0@@ -45,6 +45,7 @@ struct sgx_epc_section {spinlock_t lock;struct list_head page_list;unsigned long free_cnt; +struct list_head section_list;
Maybe name this numa_section_list.
Quoted 14 lines · Dave Hansen
Instead of having a for-each-section loop, I'd make it for-each-node -> for-each-section. Something like:
for (i = 0; i < num_possible_nodes(); i++) { node = (numa_node_id() + i) % num_possible_nodes() if (!node_isset(nid, sgx_numa_mask)) continue; list_for_each_entry(section, &sgx_numa_nodes[nid], section_list) { __sgx_alloc_epc_page_from_section(section) } }
OK, here's an almost completely fleshed-out loop:
page = NULL;
node = numa_node_id();
start_node = node;
while (1) {
list_for_each_entry(section, &sgx_numa_nodes[nid],
section_list) {
page = __sgx_alloc_epc(section);
if (page)
break;
}
if (page)
break;
/*
* EPC allocation failed on 'node'. Fall
* back with round-robin to other nodes with
* EPC:
*/
node = next_node_in(node, sgx_numa_mask);
/* Give up if allocation wraps back to the start: */
if (node == start_node)
break;
}This will:
1. Always start close to the CPU that started the allocation
2. Always spread the allocations out among nodes evenly, never concentrating allocations on node 0, for instance. (This could also be node_random() and get a similar effect, but this probably has slightly better default NUMA behavior).
3. Efficiently look among all nodes because of 'sgx_numa_mask'
4. Have no special case for the first allocation. All allocations will be satisfied from this unified loop.
5. Compile down to no loop on CONFIG_NUMA=y systems.
6. Be guaranteed to make forward progress even if preempted and numa_node_id() changes in the loop.
BTW, I think the name of __sgx_alloc_epc_page_from_section() can be shortened down. It's passed a section and returns a page, so both of those could be removed from the name.
Jarkko Sakkinen
maintainerQuoted 56 lines · Dave Hansen
Quoted 14 lines · Dave Hansen
Instead of having a for-each-section loop, I'd make it for-each-node -> for-each-section. Something like:
for (i = 0; i < num_possible_nodes(); i++) { node = (numa_node_id() + i) % num_possible_nodes() if (!node_isset(nid, sgx_numa_mask)) continue; list_for_each_entry(section, &sgx_numa_nodes[nid], section_list) { __sgx_alloc_epc_page_from_section(section) } }OK, here's an almost completely fleshed-out loop:
page = NULL; node = numa_node_id(); start_node = node; while (1) { list_for_each_entry(section, &sgx_numa_nodes[nid],(quoted diff) +0
-0section_list) { page = __sgx_alloc_epc(section);if (page) break; } if (page) break; /* * EPC allocation failed on 'node'. Fall * back with round-robin to other nodes with * EPC: */ node = next_node_in(node, sgx_numa_mask); /* Give up if allocation wraps back to the start: */ if (node == start_node) break; }This will:
1. Always start close to the CPU that started the allocation
2. Always spread the allocations out among nodes evenly, never concentrating allocations on node 0, for instance. (This could also benode_random()and get a similar effect, but this probably has slightly better default NUMA behavior).
3. Efficiently look among all nodes because of 'sgx_numa_mask'
4. Have no special case for the first allocation. All allocations will be satisfied from this unified loop.
5. Compile down to no loop onCONFIG_NUMA=ysystems.
6. Be guaranteed to make forward progress even if preempted andnuma_node_id()changes in the loop.
Quoted 3 lines
BTW, I think the name of
__sgx_alloc_epc_page_from_section()can be shortened down. It's passed a section and returns a page, so both of those could be removed from the name.
I would start with what I have with minimal changes, and then continue with a patch that completely wipes the section list.
Then fallback can just take a page from a flat FIFO of EPC pages.
/Jarkko
Jarkko Sakkinen
maintainerQuoted 10 lines · Dave Hansen
arch/x86/kernel/cpu/sgx/main.c static int sgx_nr_epc_sections; · line 22
Quoted 2 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+/* Nodes with one or more EPC sections. */ +static nodemask_t sgx_numa_mask;I'd also add that this is for optimization only.
arch/x86/kernel/cpu/sgx/main.c static int sgx_nr_epc_sections; · line 25
Quoted 2 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+/* Array of lists of EPC sections for each NUMA node. */ +struct list_head *sgx_numa_nodes;I'd much prefer:
Quoted 7 lines
/*
* Array with onelist_headfor each possible NUMA node. Each
* list contains all the sgx_epc_section's which are on that
* node.
*/Otherwise, it's hard to imagine what this structure looks like.
OK.
Quoted 58 lines
Quoted 32 lines
/*
* These variables are part of the state of the reclaimer, and must be accessed
* withsgx_reclaimer_lockacquired.arch/x86/kernel/cpu/sgx/main.c +20
-0@@ -473,6 +479,26 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_secreturn page; } +static struct sgx_epc_page *__sgx_alloc_epc_page_from_node (int nid) +{ +struct sgx_epc_section *section; +struct sgx_epc_page *page; + +if (WARN_ON_ONCE (nid <0 || nid >=MAX_NUMNODES )) +return NULL ; + +if (!node_isset (nid, sgx_numa_mask)) +return NULL ; + +list_for_each_entry (section, &sgx_numa_nodes[nid], section_list) { + page =__sgx_alloc_epc_page_from_section (section); +if (page) +return page; + } + +return NULL ; +} +/** * __sgx_alloc_epc_page() - Allocate an EPC page * @@ -485,13 +511,17 @@ static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec*/ arch/x86/kernel/cpu/sgx/main.c static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec · line 521
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c +4
-0struct sgx_epc_page *__sgx_alloc_epc_page (void ) { +int current_nid =numa_node_id ();struct sgx_epc_section *section;struct sgx_epc_page *page;int i; + page =__sgx_alloc_epc_page_from_node (current_nid); +if (page) +return page;Comments, please.
/* Try to allocate EPC from the current node, first: */then:
/* Search all EPC sections, ignoring locality: */arch/x86/kernel/cpu/sgx/main.c static struct sgx_epc_page *__sgx_alloc_epc_page_from_section(struct sgx_epc_sec · line 527
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c +0
-1for (i =0 ; i < sgx_nr_epc_sections; i++) { section = &sgx_epc_sections[i]; - page =__sgx_alloc_epc_page_from_section (section);if (page)return page;
Quoted 3 lines
This still has the problem that it exerts too much pressure on the low-numbered sgx_epc_sections[]. If a node's sections are full, it always tries to go after sgx_epc_sections[0].
I have a better idea. See below.
Quoted 29 lines
It can be in another patch, but I think the *minimal* thing we can do here for a NUMA allocator is to try to at least balance the allocations.
Instead of having a for-each-section loop, I'd make it for-each-node -> for-each-section. Something like:
for (i = 0; i < num_possible_nodes(); i++) { node = (numa_node_id() + i) % num_possible_nodes() if (!node_isset(nid, sgx_numa_mask)) continue; list_for_each_entry(section, &sgx_numa_nodes[nid], section_list) { __sgx_alloc_epc_page_from_section(section) } }Then you have a single loop instead of a "try local then a fall back".
Also, that "node++" thing might be able to use
next_online_node().Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c +1
-0@@ -665,8 +695,12 @@ static bool __init sgx_page_cache_init(void) {u32 eax, ebx, ecx, edx, type;u64 pa, size; +int nid;int i;
Quoted 5 lines
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 702
Quoted 2 lines
arch/x86/kernel/cpu/sgx/main.c +2
-0+nodes_clear (sgx_numa_mask); + sgx_numa_nodes =kmalloc_array (MAX_NUMNODES ,sizeof (*sgx_numa_nodes),GFP_KERNEL );
MAX_NUMNODESwill always be the largest compile-time constant. That's 4k, IIRC.num_possible_nodes()might be as small as 1 if NUMA is off.
Right.
Quoted 6 lines
Quoted 6 lines
arch/x86/kernel/cpu/sgx/main.c +0
-0for (i =0 ; i <ARRAY_SIZE (sgx_epc_sections); i++) {cpuid_count (SGX_CPUID , i +SGX_CPUID_EPC , &eax, &ebx, &ecx, &edx);@@ -690,6 +724,22 @@ static bool __init sgx_page_cache_init(void) }
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 732
Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c +6
-0sgx_nr_epc_sections++; + + nid =numa_map_to_online_node (phys_to_target_node (pa)); + +if (nid ==NUMA_NO_NODE ) { +pr_err (FW_BUG "unable to map EPC section %d to online node.\n" , nid); + nid =0 ;Could we dump out the physical address there? I think that's even more informative than a section number.
Yes.
Quoted 7 lines
arch/x86/kernel/cpu/sgx/main.c static bool __init sgx_page_cache_init(void) · line 735
Quoted 3 lines
arch/x86/kernel/cpu/sgx/main.c +3
-0+ }else if (WARN_ON_ONCE (nid <0 || nid >=MAX_NUMNODES )) { + nid =0 ; + }I'm not sure we really need to check for these. If we're worried about the firmware returning these, I'd expect
numa_map_to_online_node()to sanity check them for us.
Yes, let's remove it.
Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/cpu/sgx/main.c +6
-0+if (!node_isset (nid, sgx_numa_mask)) { +INIT_LIST_HEAD (&sgx_numa_nodes[nid]); +node_set (nid, sgx_numa_mask); + } + +list_add_tail (&sgx_epc_sections[i].section_list, &sgx_numa_nodes[nid]); }
Quoted 12 lines
arch/x86/kernel/cpu/sgx/sgx.h struct sgx_epc_section { · line 48
Quoted 10 lines
arch/x86/kernel/cpu/sgx/main.c +0
-0if (!sgx_nr_epc_sections) {arch/x86/kernel/cpu/sgx/sgx.h +1
-0@@ -45,6 +45,7 @@ struct sgx_epc_section {spinlock_t lock;struct list_head page_list;unsigned long free_cnt; +struct list_head section_list;Maybe name this
numa_section_list.
Instead, let's just:
1. Have a global sgx_free_epc_list and remove sgx_epc_section. Pages from this are allocated from this in LIFO fashion.
2. Instead add struct list_head node_list and use that for node associated pages.
3. Replace 'int section' with 'int node'.
This will remove one layer of abstraction and provide better fallback scheme.
E.g. allocate:
1. Check node_list of current node.
2. As a fallback check sgx_free_epc_list.
3. list_del() for two lists.
/Jarkko
Quoted 7 lines · Jarkko Sakkinen
Instead, let's just:
1. Have a global
sgx_free_epc_listand removesgx_epc_section. Pages from this are allocated from this in LIFO fashion.
2. Instead add structlist_headnode_listand use that for node associated pages.
3. Replace 'int section' with 'int node'.
I was thinking of something similar.
I'm fine with this approach.
Jarkko Sakkinen
maintainerQuoted 9 lines · Dave Hansen
Quoted 7 lines · Jarkko Sakkinen
Instead, let's just:
1. Have a global
sgx_free_epc_listand removesgx_epc_section. Pages from this are allocated from this in LIFO fashion.
2. Instead add structlist_headnode_listand use that for node associated pages.
3. Replace 'int section' with 'int node'.
Quoted 3 lines
I was thinking of something similar.
I'm fine with this approach.
Here's my two step plan.
1. Let's ack this with the cosmetic changes. It's good first baby step to take.
2. I'll do another patch that wipes sgx_epc_section.
Both are functionally self-contained patch that improve. That's why I don't see point in iterating them as a two patch patch set. One patch at a time is less overhead.
/Jarkko
This doesn't look like it addresses all of the suggestions that I made two days ago. Is that coming in v3?
Jarkko Sakkinen
maintainerQuoted 2 lines · Dave Hansen
This doesn't look like it addresses all of the suggestions that I made two days ago. Is that coming in v3?
You mean v2 does not address?
/Jarkko