[PATCH v4 1/3] x86/sgx: Report SGX memory in /sys/devices/system/node/node*/meminfo

From: Jarkko Sakkinen
Date: Thu Sep 09 2021 - 20:44:54 EST


The amount of SGX memory on the system is determined by the BIOS and it
varies wildly between systems. It can be from dozens of MB's on desktops
or VM's, up to many GB's on servers. Just like for regular memory, it is
sometimes useful to know the amount of usable SGX memory in the system.

Add SGX_MemTotal field to /sys/devices/system/node/node*/meminfo,
showing the total SGX memory in each NUMA node. The total memory for
each NUMA node is calculated by adding the sizes of contained EPC
sections together.

Introduce arch_node_read_meminfo(), which can optionally be rewritten by
the arch code, and rewrite it for x86 so it prints SGX_MemTotal.

Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
---
v4:
* A new patch.
arch/x86/kernel/cpu/sgx/main.c | 14 ++++++++++++++
arch/x86/kernel/cpu/sgx/sgx.h | 6 ++++++
drivers/base/node.c | 10 +++++++++-
3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 63d3de02bbcc..4c6da5f4a9d4 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -717,6 +717,7 @@ static bool __init sgx_page_cache_init(void)
}

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

sgx_nr_epc_sections++;
}
@@ -790,6 +791,19 @@ int sgx_set_attribute(unsigned long *allowed_attributes,
}
EXPORT_SYMBOL_GPL(sgx_set_attribute);

+ssize_t arch_node_read_meminfo(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int len)
+{
+ struct sgx_numa_node *node = &sgx_numa_nodes[dev->id];
+
+ len += sysfs_emit_at(buf, len,
+ "Node %d SGX_MemTotal: %8lu kB\n",
+ dev->id, node->size);
+
+ return len;
+}
+
static int __init sgx_init(void)
{
int ret;
diff --git a/arch/x86/kernel/cpu/sgx/sgx.h b/arch/x86/kernel/cpu/sgx/sgx.h
index 4628acec0009..74713b98a859 100644
--- a/arch/x86/kernel/cpu/sgx/sgx.h
+++ b/arch/x86/kernel/cpu/sgx/sgx.h
@@ -39,6 +39,7 @@ struct sgx_epc_page {
*/
struct sgx_numa_node {
struct list_head free_page_list;
+ unsigned long size;
spinlock_t lock;
};

@@ -95,4 +96,9 @@ static inline int __init sgx_vepc_init(void)

void sgx_update_lepubkeyhash(u64 *lepubkeyhash);

+extern ssize_t arch_node_read_meminfo(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int len);
+
+
#endif /* _X86_SGX_H */
diff --git a/drivers/base/node.c b/drivers/base/node.c
index 4a4ae868ad9f..91eaa2e2ce33 100644
--- a/drivers/base/node.c
+++ b/drivers/base/node.c
@@ -361,6 +361,13 @@ static void node_init_caches(unsigned int nid) { }
static void node_remove_caches(struct node *node) { }
#endif

+ssize_t __weak arch_node_read_meminfo(struct device *dev,
+ struct device_attribute *attr,
+ char *buf, int len)
+{
+ return len;
+}
+
#define K(x) ((x) << (PAGE_SHIFT - 10))
static ssize_t node_read_meminfo(struct device *dev,
struct device_attribute *attr, char *buf)
@@ -473,7 +480,8 @@ static ssize_t node_read_meminfo(struct device *dev,
#endif
);
len += hugetlb_report_node_meminfo(buf, len, nid);
- return len;
+
+ return arch_node_read_meminfo(dev, attr, buf, len);
}

#undef K
--
2.25.1