Skip to content

Commit 50468e4

Browse files
jarkkojshansendc
authored andcommitted
x86/sgx: Add an attribute for the amount of SGX memory in a NUMA node
== Problem == The amount of SGX memory on a system is determined by the BIOS and it varies wildly between systems. It can be as small as dozens of MB's and as large as many GB's on servers. Just like how applications need to know how much regular RAM is available, enclave builders need to know how much SGX memory an enclave can consume. == Solution == Introduce a new sysfs file: /sys/devices/system/node/nodeX/x86/sgx_total_bytes to enumerate the amount of SGX memory available in each NUMA node. This serves the same function for SGX as /proc/meminfo or /sys/devices/system/node/nodeX/meminfo does for normal RAM. 'sgx_total_bytes' is needed today to help drive the SGX selftests. SGX-specific swap code is exercised by creating overcommitted enclaves which are larger than the physical SGX memory on the system. They currently use a CPUID-based approach which can diverge from the actual amount of SGX memory available. 'sgx_total_bytes' ensures that the selftests can work efficiently and do not attempt stupid things like creating a 100,000 MB enclave on a system with 128 MB of SGX memory. == Implementation Details == Introduce CONFIG_HAVE_ARCH_NODE_DEV_GROUP opt-in flag to expose an arch specific attribute group, and add an attribute for the amount of SGX memory in bytes to each NUMA node: == ABI Design Discussion == As opposed to the per-node ABI, a single, global ABI was considered. However, this would prevent enclaves from being able to size themselves so that they fit on a single NUMA node. Essentially, a single value would rule out NUMA optimizations for enclaves. Create a new "x86/" directory inside each "nodeX/" sysfs directory. 'sgx_total_bytes' is expected to be the first of at least a few sgx-specific files to be placed in the new directory. Just scanning /proc/meminfo, these are the no-brainers that we have for RAM, but we need for SGX: MemTotal: xxxx kB // sgx_total_bytes (implemented here) MemFree: yyyy kB // sgx_free_bytes SwapTotal: zzzz kB // sgx_swapped_bytes So, at *least* three. I think we will eventually end up needing something more along the lines of a dozen. A new directory (as opposed to being in the nodeX/ "root") directory avoids cluttering the root with several "sgx_*" files. Place the new file in a new "nodeX/x86/" directory because SGX is highly x86-specific. It is very unlikely that any other architecture (or even non-Intel x86 vendor) will ever implement SGX. Using "sgx/" as opposed to "x86/" was also considered. But, there is a real chance this can get used for other arch-specific purposes. [ dhansen: rewrite changelog ] Signed-off-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Acked-by: Borislav Petkov <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 5c16f7e commit 50468e4

File tree

7 files changed

+39
-0
lines changed

7 files changed

+39
-0
lines changed

Documentation/ABI/stable/sysfs-devices-node

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,9 @@ Contact: Keith Busch <[email protected]>
176176
Description:
177177
The cache write policy: 0 for write-back, 1 for write-through,
178178
other or unknown.
179+
180+
What: /sys/devices/system/node/nodeX/x86/sgx_total_bytes
181+
Date: November 2021
182+
Contact: Jarkko Sakkinen <[email protected]>
183+
Description:
184+
The total amount of SGX physical memory in bytes.

arch/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,10 @@ config ARCH_HAS_PARANOID_L1D_FLUSH
13021302
config DYNAMIC_SIGFRAME
13031303
bool
13041304

1305+
# Select, if arch has a named attribute group bound to NUMA device nodes.
1306+
config HAVE_ARCH_NODE_DEV_GROUP
1307+
bool
1308+
13051309
source "kernel/gcov/Kconfig"
13061310

13071311
source "scripts/gcc-plugins/Kconfig"

arch/x86/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ config X86
269269
select HAVE_ARCH_KCSAN if X86_64
270270
select X86_FEATURE_NAMES if PROC_FS
271271
select PROC_PID_ARCH_STATUS if PROC_FS
272+
select HAVE_ARCH_NODE_DEV_GROUP if X86_SGX
272273
imply IMA_SECURE_AND_OR_TRUSTED_BOOT if EFI
273274

274275
config INSTRUCTION_DECODER

arch/x86/kernel/cpu/sgx/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,11 @@ static bool __init sgx_page_cache_init(void)
825825
INIT_LIST_HEAD(&sgx_numa_nodes[nid].free_page_list);
826826
INIT_LIST_HEAD(&sgx_numa_nodes[nid].sgx_poison_page_list);
827827
node_set(nid, sgx_numa_mask);
828+
sgx_numa_nodes[nid].size = 0;
828829
}
829830

830831
sgx_epc_sections[i].node = &sgx_numa_nodes[nid];
832+
sgx_numa_nodes[nid].size += size;
831833

832834
sgx_nr_epc_sections++;
833835
}
@@ -901,6 +903,24 @@ int sgx_set_attribute(unsigned long *allowed_attributes,
901903
}
902904
EXPORT_SYMBOL_GPL(sgx_set_attribute);
903905

906+
#ifdef CONFIG_NUMA
907+
static ssize_t sgx_total_bytes_show(struct device *dev, struct device_attribute *attr, char *buf)
908+
{
909+
return sysfs_emit(buf, "%lu\n", sgx_numa_nodes[dev->id].size);
910+
}
911+
static DEVICE_ATTR_RO(sgx_total_bytes);
912+
913+
static struct attribute *arch_node_dev_attrs[] = {
914+
&dev_attr_sgx_total_bytes.attr,
915+
NULL,
916+
};
917+
918+
const struct attribute_group arch_node_dev_group = {
919+
.name = "x86",
920+
.attrs = arch_node_dev_attrs,
921+
};
922+
#endif /* CONFIG_NUMA */
923+
904924
static int __init sgx_init(void)
905925
{
906926
int ret;

arch/x86/kernel/cpu/sgx/sgx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ struct sgx_epc_page {
4444
struct sgx_numa_node {
4545
struct list_head free_page_list;
4646
struct list_head sgx_poison_page_list;
47+
unsigned long size;
4748
spinlock_t lock;
4849
};
4950

drivers/base/node.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,9 @@ static const struct attribute_group node_dev_group = {
581581

582582
static const struct attribute_group *node_dev_groups[] = {
583583
&node_dev_group,
584+
#ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP
585+
&arch_node_dev_group,
586+
#endif
584587
NULL
585588
};
586589

include/linux/numa.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ static inline int phys_to_target_node(u64 start)
5858
}
5959
#endif
6060

61+
#ifdef CONFIG_HAVE_ARCH_NODE_DEV_GROUP
62+
extern const struct attribute_group arch_node_dev_group;
63+
#endif
64+
6165
#endif /* _LINUX_NUMA_H */

0 commit comments

Comments
 (0)