[PATCH v4 1/8] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem

Subsystems: documentation, intel sgx, the rest, x86 architecture (32-bit and 64-bit)

STALE1862d REVIEWED: 4 (4M)

1 review trailer (1 from subsystem maintainers).

4 messages, 2 authors, 2021-08-16 · open the first message on its own page

[PATCH v4 1/8] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem

From: Jarkko Sakkinen <jarkko@kernel.org>
Date: 2021-08-09 09:31:39

Just like normal memory, SGX memory can be overcommitted.  SGX has its
own reclaim mechanism which kicks in when physical SGX memory (Enclave
Page Cache / EPC) is exhausted.  That reclaim mechanism is relatively
rarely exercised and needs selftests to poke at it.

The amount of EPC on the system is determined by the BIOS and it varies
wildly between systems.  It can be dozens of MB on desktops, or many GB
on servers.

To run in a reasonable amount of time, the selftest needs to know how
much EPC there is in the system.

Introduce a new debugfs file to export that information.

Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---

v3:
* Describe the units of sgx_total_mem in Dcumentation/x86/sgx.rst.
* Rewrite of the commit message (suggested by Dave):
  https://lore.kernel.org/linux-sgx/5d3614af-2393-6744-9d85-7001241ca76e@intel.com/

v2:
* sgx_nr_all_pages -> sgx_total_mem

 Documentation/x86/sgx.rst      |  6 ++++++
 arch/x86/kernel/cpu/sgx/main.c | 10 +++++++++-
 2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/Documentation/x86/sgx.rst b/Documentation/x86/sgx.rst
index dd0ac96ff9ef..f11bfb331b93 100644
--- a/Documentation/x86/sgx.rst
+++ b/Documentation/x86/sgx.rst
@@ -250,3 +250,9 @@ user wants to deploy SGX applications both on the host and in guests
 on the same machine, the user should reserve enough EPC (by taking out
 total virtual EPC size of all SGX VMs from the physical EPC size) for
 host SGX applications so they can run with acceptable performance.
+
+Debugging
+=========
+
+*/sys/kernel/debug/x86/sgx_total_mem* contains an integer describing
+the total SGX reserved memory in bytes, available in the system.
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 63d3de02bbcc..b65da19a53ee 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 /*  Copyright(c) 2016-20 Intel Corporation. */
 
+#include <linux/debugfs.h>
 #include <linux/file.h>
 #include <linux/freezer.h>
 #include <linux/highmem.h>
@@ -28,7 +29,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 EPC memory available in bytes. */
+static unsigned long sgx_total_mem;
+
+/* The number of free EPC pages in all nodes. */
 static unsigned long sgx_nr_free_pages;
 
 /* Nodes with one or more EPC sections. */
@@ -656,6 +660,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
 		list_add_tail(&section->pages[i].list, &sgx_dirty_page_list);
 	}
 
+	sgx_total_mem += nr_pages * PAGE_SIZE;
+
 	return true;
 }
 
@@ -823,6 +829,8 @@ static int __init sgx_init(void)
 	if (sgx_vepc_init() && ret)
 		goto err_provision;
 
+	debugfs_create_ulong("sgx_total_mem", 0444, arch_debugfs_dir, &sgx_total_mem);
+
 	return 0;
 
 err_provision:
-- 
2.32.0

Re: [PATCH v4 1/8] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem

From: Shuah Khan <skhan@linuxfoundation.org>
Date: 2021-08-13 17:33:24

On 8/9/21 3:31 AM, Jarkko Sakkinen wrote:
quoted hunk
Just like normal memory, SGX memory can be overcommitted.  SGX has its
own reclaim mechanism which kicks in when physical SGX memory (Enclave
Page Cache / EPC) is exhausted.  That reclaim mechanism is relatively
rarely exercised and needs selftests to poke at it.

The amount of EPC on the system is determined by the BIOS and it varies
wildly between systems.  It can be dozens of MB on desktops, or many GB
on servers.

To run in a reasonable amount of time, the selftest needs to know how
much EPC there is in the system.

Introduce a new debugfs file to export that information.

Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---

v3:
* Describe the units of sgx_total_mem in Dcumentation/x86/sgx.rst.
* Rewrite of the commit message (suggested by Dave):
   https://lore.kernel.org/linux-sgx/5d3614af-2393-6744-9d85-7001241ca76e@intel.com/

v2:
* sgx_nr_all_pages -> sgx_total_mem

  Documentation/x86/sgx.rst      |  6 ++++++
  arch/x86/kernel/cpu/sgx/main.c | 10 +++++++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/Documentation/x86/sgx.rst b/Documentation/x86/sgx.rst
index dd0ac96ff9ef..f11bfb331b93 100644
--- a/Documentation/x86/sgx.rst
+++ b/Documentation/x86/sgx.rst
@@ -250,3 +250,9 @@ user wants to deploy SGX applications both on the host and in guests
  on the same machine, the user should reserve enough EPC (by taking out
  total virtual EPC size of all SGX VMs from the physical EPC size) for
  host SGX applications so they can run with acceptable performance.
+
+Debugging
+=========
+
+*/sys/kernel/debug/x86/sgx_total_mem* contains an integer describing
+the total SGX reserved memory in bytes, available in the system.
Why not add the details you have in the commit log to the document?
Also add more details on how to debug/test.
quoted hunk
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 63d3de02bbcc..b65da19a53ee 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0
  /*  Copyright(c) 2016-20 Intel Corporation. */
  
+#include <linux/debugfs.h>
  #include <linux/file.h>
  #include <linux/freezer.h>
  #include <linux/highmem.h>
@@ -28,7 +29,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 EPC memory available in bytes. */
+static unsigned long sgx_total_mem;
+
+/* The number of free EPC pages in all nodes. */
  static unsigned long sgx_nr_free_pages;
  
  /* Nodes with one or more EPC sections. */
@@ -656,6 +660,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
  		list_add_tail(&section->pages[i].list, &sgx_dirty_page_list);
  	}
  
+	sgx_total_mem += nr_pages * PAGE_SIZE;
+
  	return true;
  }
  
@@ -823,6 +829,8 @@ static int __init sgx_init(void)
  	if (sgx_vepc_init() && ret)
  		goto err_provision;
  
+	debugfs_create_ulong("sgx_total_mem", 0444, arch_debugfs_dir, &sgx_total_mem);
+
  	return 0;
  
  err_provision:
thanks,
-- Shuah

Re: [PATCH v4 1/8] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem

From: Shuah Khan <skhan@linuxfoundation.org>
Date: 2021-08-13 18:53:48

On 8/9/21 3:31 AM, Jarkko Sakkinen wrote:
quoted hunk
Just like normal memory, SGX memory can be overcommitted.  SGX has its
own reclaim mechanism which kicks in when physical SGX memory (Enclave
Page Cache / EPC) is exhausted.  That reclaim mechanism is relatively
rarely exercised and needs selftests to poke at it.

The amount of EPC on the system is determined by the BIOS and it varies
wildly between systems.  It can be dozens of MB on desktops, or many GB
on servers.

To run in a reasonable amount of time, the selftest needs to know how
much EPC there is in the system.

Introduce a new debugfs file to export that information.

Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---

v3:
* Describe the units of sgx_total_mem in Dcumentation/x86/sgx.rst.
* Rewrite of the commit message (suggested by Dave):
   https://lore.kernel.org/linux-sgx/5d3614af-2393-6744-9d85-7001241ca76e@intel.com/

v2:
* sgx_nr_all_pages -> sgx_total_mem

  Documentation/x86/sgx.rst      |  6 ++++++
  arch/x86/kernel/cpu/sgx/main.c | 10 +++++++++-
  2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/Documentation/x86/sgx.rst b/Documentation/x86/sgx.rst
index dd0ac96ff9ef..f11bfb331b93 100644
--- a/Documentation/x86/sgx.rst
+++ b/Documentation/x86/sgx.rst
@@ -250,3 +250,9 @@ user wants to deploy SGX applications both on the host and in guests
  on the same machine, the user should reserve enough EPC (by taking out
  total virtual EPC size of all SGX VMs from the physical EPC size) for
  host SGX applications so they can run with acceptable performance.
+
+Debugging
+=========
+
+*/sys/kernel/debug/x86/sgx_total_mem* contains an integer describing
+the total SGX reserved memory in bytes, available in the system.
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 63d3de02bbcc..b65da19a53ee 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -1,6 +1,7 @@
  // SPDX-License-Identifier: GPL-2.0
  /*  Copyright(c) 2016-20 Intel Corporation. */
  
+#include <linux/debugfs.h>
  #include <linux/file.h>
  #include <linux/freezer.h>
  #include <linux/highmem.h>
@@ -28,7 +29,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 EPC memory available in bytes. */
+static unsigned long sgx_total_mem;
+
+/* The number of free EPC pages in all nodes. */
  static unsigned long sgx_nr_free_pages;
  
  /* Nodes with one or more EPC sections. */
@@ -656,6 +660,8 @@ static bool __init sgx_setup_epc_section(u64 phys_addr, u64 size,
  		list_add_tail(&section->pages[i].list, &sgx_dirty_page_list);
  	}
  
+	sgx_total_mem += nr_pages * PAGE_SIZE;
+
  	return true;
  }
  
@@ -823,6 +829,8 @@ static int __init sgx_init(void)
  	if (sgx_vepc_init() && ret)
  		goto err_provision;
  
+	debugfs_create_ulong("sgx_total_mem", 0444, arch_debugfs_dir, &sgx_total_mem);
dbugfs init dependency would be met I assume by this time?
+
  	return 0;
  
  err_provision:

Re: [PATCH v4 1/8] x86/sgx: Add /sys/kernel/debug/x86/sgx_total_mem

From: Jarkko Sakkinen <jarkko@kernel.org>
Date: 2021-08-16 21:30:50

On Fri, 2021-08-13 at 11:33 -0600, Shuah Khan wrote:
Why not add the details you have in the commit log to the document?
Also add more details on how to debug/test.
Agreed, we can re-use it, thanks for the remark. I'll extend it, 
but it remains to be seen where exactly it will be documented.

I've started to manage this patch as a separate cycle:

https://lore.kernel.org/linux-sgx/20210812224645.90280-1-jarkko@kernel.org/T/#u

I'll CC you the 1st non-RFC version, and hold on with the patch set
under discussion up until we get the kernel change first to the
mainline.

Should happen within couple of days as I think we got quite well
sorted out with Dave last week, what to do next.

/Jarkko
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help