linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jarkko Sakkinen <jarkko@kernel.org>
To: Jarkko Sakkinen <jarkko@kernel.org>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
	Andy Lutomirski <luto@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>
Cc: linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 2/3] x86/sgx: Report SGX memory in /proc/meminfo
Date: Fri, 10 Sep 2021 03:17:25 +0300	[thread overview]
Message-ID: <20210910001726.811497-2-jarkko@kernel.org> (raw)
In-Reply-To: <20210910001726.811497-1-jarkko@kernel.org>

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 /proc/meminfo, which shows the total amount of
usable SGX memory in the system.  E.g. with 32 MB reserved for SGX from
BIOS, the printout would be:

SGX_MemTotal:      22528 kB

Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
v4:
* Calculate sgx_mem_total by adding up the sizes of the NUMA node
  EPC's, only once all of them have been set up correctly.
* Move documentation to a separate patch, as it clearly is a
  separate issue.
v2:
* Move ifdef fix for sgx_set_attribute() to a separate patch.
---
 arch/x86/include/asm/sgx.h     |  2 ++
 arch/x86/kernel/cpu/sgx/main.c | 10 ++++++++--
 arch/x86/mm/pat/set_memory.c   |  5 +++++
 3 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h
index 05f3e21f01a7..65124b05c145 100644
--- a/arch/x86/include/asm/sgx.h
+++ b/arch/x86/include/asm/sgx.h
@@ -365,6 +365,8 @@ struct sgx_sigstruct {
  * comment!
  */
 
+extern unsigned long sgx_mem_total;
+
 #ifdef CONFIG_X86_SGX_KVM
 int sgx_virt_ecreate(struct sgx_pageinfo *pageinfo, void __user *secs,
 		     int *trapnr);
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 4c6da5f4a9d4..d8f5769e4004 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -28,7 +28,10 @@ static DECLARE_WAIT_QUEUE_HEAD(ksgxd_waitq);
 static LIST_HEAD(sgx_active_page_list);
 static DEFINE_SPINLOCK(sgx_reclaimer_lock);
 
-/* The free page list lock protected variables prepend the lock. */
+/* Total size of the Enclave Page Cache (EPC) in the system. */
+unsigned long sgx_mem_total;
+
+/* The number of free EPC pages in all nodes. */
 static unsigned long sgx_nr_free_pages;
 
 /* Nodes with one or more EPC sections. */
@@ -727,6 +730,9 @@ static bool __init sgx_page_cache_init(void)
 		return false;
 	}
 
+	for (i = 0; i < num_possible_nodes(); i++)
+		sgx_mem_total += sgx_numa_nodes[nid].size;
+
 	return true;
 }
 
@@ -799,7 +805,7 @@ ssize_t arch_node_read_meminfo(struct device *dev,
 
 	len += sysfs_emit_at(buf, len,
 			     "Node %d SGX_MemTotal:   %8lu kB\n",
-			     dev->id, node->size);
+			     dev->id, node->size / 1024);
 
 	return len;
 }
diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c
index ad8a5c586a35..b55ea89df801 100644
--- a/arch/x86/mm/pat/set_memory.c
+++ b/arch/x86/mm/pat/set_memory.c
@@ -29,6 +29,7 @@
 #include <asm/proto.h>
 #include <asm/memtype.h>
 #include <asm/set_memory.h>
+#include <asm/sgx.h>
 
 #include "../mm_internal.h"
 
@@ -116,6 +117,10 @@ void arch_report_meminfo(struct seq_file *m)
 	if (direct_gbpages)
 		seq_printf(m, "DirectMap1G:    %8lu kB\n",
 			direct_pages_count[PG_LEVEL_1G] << 20);
+
+#if defined(CONFIG_X86_SGX) || defined(CONFIG_X86_SGX_KVM)
+	seq_printf(m, "SGX_MemTotal:   %8lu kB\n", sgx_mem_total / 1024);
+#endif
 }
 #else
 static inline void split_page_count(int level) { }
-- 
2.25.1


  reply	other threads:[~2021-09-10  0:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-10  0:17 [PATCH v4 1/3] x86/sgx: Report SGX memory in /sys/devices/system/node/node*/meminfo Jarkko Sakkinen
2021-09-10  0:17 ` Jarkko Sakkinen [this message]
2021-09-10  0:20   ` [PATCH v4 2/3] x86/sgx: Report SGX memory in /proc/meminfo Jarkko Sakkinen
2021-09-10  0:17 ` [PATCH v4 3/3] x86/sgx: Document SGX_MemTotal to Documentation/x86/meminfo.rst Jarkko Sakkinen
2021-09-10  6:51 ` [PATCH v4 1/3] x86/sgx: Report SGX memory in /sys/devices/system/node/node*/meminfo Greg Kroah-Hartman
2021-09-10 13:17   ` Jarkko Sakkinen
2021-09-10 14:05     ` Greg Kroah-Hartman
2021-09-10 15:02       ` Jarkko Sakkinen
2021-09-10 15:11         ` Greg Kroah-Hartman
2021-09-12 19:37           ` Jarkko Sakkinen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210910001726.811497-2-jarkko@kernel.org \
    --to=jarkko@kernel.org \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).