* [PATCH v14 1/5] x86/sgx: Introduce functions to count the sgx_(vepc_)open()
2025-08-14 7:34 [PATCH v14 0/5] Enable automatic SVN updates for SGX enclaves Elena Reshetova
@ 2025-08-14 7:34 ` Elena Reshetova
2025-08-14 7:34 ` [PATCH v14 2/5] x86/cpufeatures: Add X86_FEATURE_SGX_EUPDATESVN feature flag Elena Reshetova
` (3 subsequent siblings)
4 siblings, 0 replies; 16+ messages in thread
From: Elena Reshetova @ 2025-08-14 7:34 UTC (permalink / raw)
To: dave.hansen
Cc: jarkko, seanjc, kai.huang, mingo, linux-sgx, linux-kernel, x86,
asit.k.mallick, vincent.r.scarlata, chongc, erdemaktas,
vannapurve, bondarn, scott.raynor, Elena Reshetova
Currently, when SGX is compromised and the microcode update fix is applied,
the machine needs to be rebooted to invalidate old SGX crypto-assets and
make SGX be in an updated safe state. It's not friendly for the cloud.
To avoid having to reboot, a new ENCLS[EUPDATESVN] is introduced to update
SGX environment at runtime. This process needs to be done when there's no
SGX users to make sure no compromised enclaves can survive from the update
and allow the system to regenerate crypto-assets.
For now there's no counter to track the active SGX users of host enclave
and virtual EPC. Introduce such counter mechanism so that the EUPDATESVN
can be done only when there's no SGX users.
Define placeholder functions sgx_inc/dec_usage_count() that are used to
increment and decrement such a counter. Also, wire the call sites for
these functions. Encapsulate the current sgx_(vepc_)open() to
__sgx_(vepc_)open() to make the new sgx_(vepc_)open() easy to read.
The definition of the counter itself and the actual implementation of
sgx_inc/dec_usage_count() functions come next.
Note: The EUPDATESVN, which may fail, will be done in
sgx_inc_usage_count(). Make it return 'int' to make subsequent patches
which implement EUPDATESVN easier to review. For now it always returns
success.
Suggested-by: Sean Christopherson <seanjc@google.com>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
arch/x86/kernel/cpu/sgx/driver.c | 19 ++++++++++++++++++-
arch/x86/kernel/cpu/sgx/encl.c | 1 +
arch/x86/kernel/cpu/sgx/main.c | 10 ++++++++++
arch/x86/kernel/cpu/sgx/sgx.h | 3 +++
arch/x86/kernel/cpu/sgx/virt.c | 20 +++++++++++++++++++-
5 files changed, 51 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/driver.c
index 7f8d1e11dbee..79d6020dfe9c 100644
--- a/arch/x86/kernel/cpu/sgx/driver.c
+++ b/arch/x86/kernel/cpu/sgx/driver.c
@@ -14,7 +14,7 @@ u64 sgx_attributes_reserved_mask;
u64 sgx_xfrm_reserved_mask = ~0x3;
u32 sgx_misc_reserved_mask;
-static int sgx_open(struct inode *inode, struct file *file)
+static int __sgx_open(struct inode *inode, struct file *file)
{
struct sgx_encl *encl;
int ret;
@@ -41,6 +41,23 @@ static int sgx_open(struct inode *inode, struct file *file)
return 0;
}
+static int sgx_open(struct inode *inode, struct file *file)
+{
+ int ret;
+
+ ret = sgx_inc_usage_count();
+ if (ret)
+ return ret;
+
+ ret = __sgx_open(inode, file);
+ if (ret) {
+ sgx_dec_usage_count();
+ return ret;
+ }
+
+ return 0;
+}
+
static int sgx_release(struct inode *inode, struct file *file)
{
struct sgx_encl *encl = file->private_data;
diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c
index 308dbbae6c6e..cf149b9f4916 100644
--- a/arch/x86/kernel/cpu/sgx/encl.c
+++ b/arch/x86/kernel/cpu/sgx/encl.c
@@ -765,6 +765,7 @@ void sgx_encl_release(struct kref *ref)
WARN_ON_ONCE(encl->secs.epc_page);
kfree(encl);
+ sgx_dec_usage_count();
}
/*
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 2de01b379aa3..3a5cbd1c170e 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -917,6 +917,16 @@ int sgx_set_attribute(unsigned long *allowed_attributes,
}
EXPORT_SYMBOL_GPL(sgx_set_attribute);
+int sgx_inc_usage_count(void)
+{
+ return 0;
+}
+
+void sgx_dec_usage_count(void)
+{
+ return;
+}
+
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 d2dad21259a8..f5940393d9bd 100644
--- a/arch/x86/kernel/cpu/sgx/sgx.h
+++ b/arch/x86/kernel/cpu/sgx/sgx.h
@@ -102,6 +102,9 @@ static inline int __init sgx_vepc_init(void)
}
#endif
+int sgx_inc_usage_count(void);
+void sgx_dec_usage_count(void);
+
void sgx_update_lepubkeyhash(u64 *lepubkeyhash);
#endif /* _X86_SGX_H */
diff --git a/arch/x86/kernel/cpu/sgx/virt.c b/arch/x86/kernel/cpu/sgx/virt.c
index 7aaa3652e31d..b649c0610019 100644
--- a/arch/x86/kernel/cpu/sgx/virt.c
+++ b/arch/x86/kernel/cpu/sgx/virt.c
@@ -255,10 +255,11 @@ static int sgx_vepc_release(struct inode *inode, struct file *file)
xa_destroy(&vepc->page_array);
kfree(vepc);
+ sgx_dec_usage_count();
return 0;
}
-static int sgx_vepc_open(struct inode *inode, struct file *file)
+static int __sgx_vepc_open(struct inode *inode, struct file *file)
{
struct sgx_vepc *vepc;
@@ -273,6 +274,23 @@ static int sgx_vepc_open(struct inode *inode, struct file *file)
return 0;
}
+static int sgx_vepc_open(struct inode *inode, struct file *file)
+{
+ int ret;
+
+ ret = sgx_inc_usage_count();
+ if (ret)
+ return ret;
+
+ ret = __sgx_vepc_open(inode, file);
+ if (ret) {
+ sgx_dec_usage_count();
+ return ret;
+ }
+
+ return 0;
+}
+
static long sgx_vepc_ioctl(struct file *file,
unsigned int cmd, unsigned long arg)
{
--
2.45.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v14 2/5] x86/cpufeatures: Add X86_FEATURE_SGX_EUPDATESVN feature flag
2025-08-14 7:34 [PATCH v14 0/5] Enable automatic SVN updates for SGX enclaves Elena Reshetova
2025-08-14 7:34 ` [PATCH v14 1/5] x86/sgx: Introduce functions to count the sgx_(vepc_)open() Elena Reshetova
@ 2025-08-14 7:34 ` Elena Reshetova
2025-08-14 7:34 ` [PATCH v14 3/5] x86/sgx: Define error codes for use by ENCLS[EUPDATESVN] Elena Reshetova
` (2 subsequent siblings)
4 siblings, 0 replies; 16+ messages in thread
From: Elena Reshetova @ 2025-08-14 7:34 UTC (permalink / raw)
To: dave.hansen
Cc: jarkko, seanjc, kai.huang, mingo, linux-sgx, linux-kernel, x86,
asit.k.mallick, vincent.r.scarlata, chongc, erdemaktas,
vannapurve, bondarn, scott.raynor, Elena Reshetova, Dave Hansen
Add a flag indicating whenever ENCLS[EUPDATESVN] SGX instruction is
supported. This will be used by SGX driver to perform CPU SVN updates.
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Reviewed-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
arch/x86/include/asm/cpufeatures.h | 1 +
arch/x86/kernel/cpu/cpuid-deps.c | 1 +
arch/x86/kernel/cpu/scattered.c | 1 +
tools/arch/x86/include/asm/cpufeatures.h | 1 +
4 files changed, 4 insertions(+)
diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h
index 602957dd2609..830d24ff1ada 100644
--- a/arch/x86/include/asm/cpufeatures.h
+++ b/arch/x86/include/asm/cpufeatures.h
@@ -494,6 +494,7 @@
#define X86_FEATURE_TSA_SQ_NO (21*32+11) /* AMD CPU not vulnerable to TSA-SQ */
#define X86_FEATURE_TSA_L1_NO (21*32+12) /* AMD CPU not vulnerable to TSA-L1 */
#define X86_FEATURE_CLEAR_CPU_BUF_VM (21*32+13) /* Clear CPU buffers using VERW before VMRUN */
+#define X86_FEATURE_SGX_EUPDATESVN (21*32+14) /* Support for ENCLS[EUPDATESVN] instruction */
/*
* BUG word(s)
diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c
index 46efcbd6afa4..3d9f49ad0efd 100644
--- a/arch/x86/kernel/cpu/cpuid-deps.c
+++ b/arch/x86/kernel/cpu/cpuid-deps.c
@@ -79,6 +79,7 @@ static const struct cpuid_dep cpuid_deps[] = {
{ X86_FEATURE_SGX_LC, X86_FEATURE_SGX },
{ X86_FEATURE_SGX1, X86_FEATURE_SGX },
{ X86_FEATURE_SGX2, X86_FEATURE_SGX1 },
+ { X86_FEATURE_SGX_EUPDATESVN, X86_FEATURE_SGX1 },
{ X86_FEATURE_SGX_EDECCSSA, X86_FEATURE_SGX1 },
{ X86_FEATURE_XFD, X86_FEATURE_XSAVES },
{ X86_FEATURE_XFD, X86_FEATURE_XGETBV1 },
diff --git a/arch/x86/kernel/cpu/scattered.c b/arch/x86/kernel/cpu/scattered.c
index b4a1f6732a3a..d13444d11ba0 100644
--- a/arch/x86/kernel/cpu/scattered.c
+++ b/arch/x86/kernel/cpu/scattered.c
@@ -42,6 +42,7 @@ static const struct cpuid_bit cpuid_bits[] = {
{ X86_FEATURE_PER_THREAD_MBA, CPUID_ECX, 0, 0x00000010, 3 },
{ X86_FEATURE_SGX1, CPUID_EAX, 0, 0x00000012, 0 },
{ X86_FEATURE_SGX2, CPUID_EAX, 1, 0x00000012, 0 },
+ { X86_FEATURE_SGX_EUPDATESVN, CPUID_EAX, 10, 0x00000012, 0 },
{ X86_FEATURE_SGX_EDECCSSA, CPUID_EAX, 11, 0x00000012, 0 },
{ X86_FEATURE_HW_PSTATE, CPUID_EDX, 7, 0x80000007, 0 },
{ X86_FEATURE_CPB, CPUID_EDX, 9, 0x80000007, 0 },
diff --git a/tools/arch/x86/include/asm/cpufeatures.h b/tools/arch/x86/include/asm/cpufeatures.h
index ee176236c2be..78c3894c17c1 100644
--- a/tools/arch/x86/include/asm/cpufeatures.h
+++ b/tools/arch/x86/include/asm/cpufeatures.h
@@ -487,6 +487,7 @@
#define X86_FEATURE_PREFER_YMM (21*32+ 8) /* Avoid ZMM registers due to downclocking */
#define X86_FEATURE_APX (21*32+ 9) /* Advanced Performance Extensions */
#define X86_FEATURE_INDIRECT_THUNK_ITS (21*32+10) /* Use thunk for indirect branches in lower half of cacheline */
+#define X86_FEATURE_SGX_EUPDATESVN (21*32+14) /* Support for ENCLS[EUPDATESVN] instruction */
/*
* BUG word(s)
--
2.45.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v14 3/5] x86/sgx: Define error codes for use by ENCLS[EUPDATESVN]
2025-08-14 7:34 [PATCH v14 0/5] Enable automatic SVN updates for SGX enclaves Elena Reshetova
2025-08-14 7:34 ` [PATCH v14 1/5] x86/sgx: Introduce functions to count the sgx_(vepc_)open() Elena Reshetova
2025-08-14 7:34 ` [PATCH v14 2/5] x86/cpufeatures: Add X86_FEATURE_SGX_EUPDATESVN feature flag Elena Reshetova
@ 2025-08-14 7:34 ` Elena Reshetova
2025-08-14 7:34 ` [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN] Elena Reshetova
2025-08-14 7:34 ` [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves Elena Reshetova
4 siblings, 0 replies; 16+ messages in thread
From: Elena Reshetova @ 2025-08-14 7:34 UTC (permalink / raw)
To: dave.hansen
Cc: jarkko, seanjc, kai.huang, mingo, linux-sgx, linux-kernel, x86,
asit.k.mallick, vincent.r.scarlata, chongc, erdemaktas,
vannapurve, bondarn, scott.raynor, Elena Reshetova
Add error codes for ENCLS[EUPDATESVN], then SGX CPUSVN update process can
know the execution state of EUPDATESVN and notify userspace.
EUPDATESVN will be called when no active SGX users is guaranteed. Only add
the error codes that can legally happen. E.g., it could also fail due to
"SGX not ready" when there's SGX users but it wouldn't happen in this
implementation.
Reviewed-by: Kai Huang <kai.huang@intel.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
arch/x86/include/asm/sgx.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h
index 6a0069761508..73348cf4fd78 100644
--- a/arch/x86/include/asm/sgx.h
+++ b/arch/x86/include/asm/sgx.h
@@ -73,6 +73,10 @@ enum sgx_encls_function {
* public key does not match IA32_SGXLEPUBKEYHASH.
* %SGX_PAGE_NOT_MODIFIABLE: The EPC page cannot be modified because it
* is in the PENDING or MODIFIED state.
+ * %SGX_INSUFFICIENT_ENTROPY: Insufficient entropy in RNG.
+ * %SGX_NO_UPDATE: EUPDATESVN could not update the CPUSVN because the
+ * current SVN was not newer than CPUSVN. This is the most
+ * common error code returned by EUPDATESVN.
* %SGX_UNMASKED_EVENT: An unmasked event, e.g. INTR, was received
*/
enum sgx_return_code {
@@ -81,6 +85,8 @@ enum sgx_return_code {
SGX_CHILD_PRESENT = 13,
SGX_INVALID_EINITTOKEN = 16,
SGX_PAGE_NOT_MODIFIABLE = 20,
+ SGX_INSUFFICIENT_ENTROPY = 29,
+ SGX_NO_UPDATE = 31,
SGX_UNMASKED_EVENT = 128,
};
--
2.45.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN]
2025-08-14 7:34 [PATCH v14 0/5] Enable automatic SVN updates for SGX enclaves Elena Reshetova
` (2 preceding siblings ...)
2025-08-14 7:34 ` [PATCH v14 3/5] x86/sgx: Define error codes for use by ENCLS[EUPDATESVN] Elena Reshetova
@ 2025-08-14 7:34 ` Elena Reshetova
2025-08-14 9:35 ` Huang, Kai
2025-08-14 7:34 ` [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves Elena Reshetova
4 siblings, 1 reply; 16+ messages in thread
From: Elena Reshetova @ 2025-08-14 7:34 UTC (permalink / raw)
To: dave.hansen
Cc: jarkko, seanjc, kai.huang, mingo, linux-sgx, linux-kernel, x86,
asit.k.mallick, vincent.r.scarlata, chongc, erdemaktas,
vannapurve, bondarn, scott.raynor, Elena Reshetova
All running enclaves and cryptographic assets (such as internal SGX
encryption keys) are assumed to be compromised whenever an SGX-related
microcode update occurs. To mitigate this assumed compromise the new
supervisor SGX instruction ENCLS[EUPDATESVN] can generate fresh
cryptographic assets.
Before executing EUPDATESVN, all SGX memory must be marked as unused. This
requirement ensures that no potentially compromised enclave survives the
update and allows the system to safely regenerate cryptographic assets.
Add the method to perform ENCLS[EUPDATESVN]. However, until the follow up
patch that wires calling sgx_update_svn() from sgx_inc_usage_count(), this
code is not reachable.
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
arch/x86/include/asm/sgx.h | 31 +++++++-------
arch/x86/kernel/cpu/sgx/encls.h | 5 +++
arch/x86/kernel/cpu/sgx/main.c | 75 +++++++++++++++++++++++++++++++++
3 files changed, 96 insertions(+), 15 deletions(-)
diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h
index 73348cf4fd78..c2c4c0d22ca4 100644
--- a/arch/x86/include/asm/sgx.h
+++ b/arch/x86/include/asm/sgx.h
@@ -28,21 +28,22 @@
#define SGX_CPUID_EPC_MASK GENMASK(3, 0)
enum sgx_encls_function {
- ECREATE = 0x00,
- EADD = 0x01,
- EINIT = 0x02,
- EREMOVE = 0x03,
- EDGBRD = 0x04,
- EDGBWR = 0x05,
- EEXTEND = 0x06,
- ELDU = 0x08,
- EBLOCK = 0x09,
- EPA = 0x0A,
- EWB = 0x0B,
- ETRACK = 0x0C,
- EAUG = 0x0D,
- EMODPR = 0x0E,
- EMODT = 0x0F,
+ ECREATE = 0x00,
+ EADD = 0x01,
+ EINIT = 0x02,
+ EREMOVE = 0x03,
+ EDGBRD = 0x04,
+ EDGBWR = 0x05,
+ EEXTEND = 0x06,
+ ELDU = 0x08,
+ EBLOCK = 0x09,
+ EPA = 0x0A,
+ EWB = 0x0B,
+ ETRACK = 0x0C,
+ EAUG = 0x0D,
+ EMODPR = 0x0E,
+ EMODT = 0x0F,
+ EUPDATESVN = 0x18,
};
/**
diff --git a/arch/x86/kernel/cpu/sgx/encls.h b/arch/x86/kernel/cpu/sgx/encls.h
index 99004b02e2ed..d9160c89a93d 100644
--- a/arch/x86/kernel/cpu/sgx/encls.h
+++ b/arch/x86/kernel/cpu/sgx/encls.h
@@ -233,4 +233,9 @@ static inline int __eaug(struct sgx_pageinfo *pginfo, void *addr)
return __encls_2(EAUG, pginfo, addr);
}
+/* Attempt to update CPUSVN at runtime. */
+static inline int __eupdatesvn(void)
+{
+ return __encls_ret_1(EUPDATESVN, "");
+}
#endif /* _X86_ENCLS_H */
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 3a5cbd1c170e..69ab28641e20 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -16,6 +16,7 @@
#include <linux/vmalloc.h>
#include <asm/msr.h>
#include <asm/sgx.h>
+#include <asm/archrandom.h>
#include "driver.h"
#include "encl.h"
#include "encls.h"
@@ -917,6 +918,80 @@ int sgx_set_attribute(unsigned long *allowed_attributes,
}
EXPORT_SYMBOL_GPL(sgx_set_attribute);
+/* Counter to count the active SGX users */
+static int sgx_usage_count;
+
+/**
+ * sgx_update_svn() - Attempt to call ENCLS[EUPDATESVN].
+ *
+ * This instruction attempts to update CPUSVN to the
+ * currently loaded microcode update SVN and generate new
+ * cryptographic assets.
+ *
+ * Return:
+ * * %0: - Success or not supported
+ * * %-EAGAIN: - Can be safely retried, failure is due to lack of
+ * * entropy in RNG
+ * * %-EIO: - Unexpected error, retries are not advisable
+ */
+static int __maybe_unused sgx_update_svn(void)
+{
+ int ret;
+
+ /*
+ * If EUPDATESVN is not available, it is ok to
+ * silently skip it to comply with legacy behavior.
+ */
+ if (!cpu_feature_enabled(X86_FEATURE_SGX_EUPDATESVN))
+ return 0;
+
+ /*
+ * EPC is guaranteed to be empty when there are no users.
+ * Ensure we are on our first user before proceeding further.
+ */
+ WARN(sgx_usage_count, "Elevated usage count when calling EUPDATESVN\n");
+
+ for (int i = 0; i < RDRAND_RETRY_LOOPS; i++) {
+ ret = __eupdatesvn();
+
+ /* Stop on success or unexpected errors: */
+ if (ret != SGX_INSUFFICIENT_ENTROPY)
+ break;
+ }
+
+ switch (ret) {
+ case 0:
+ /*
+ * SVN successfully updated.
+ * Let users know when the update was successful.
+ */
+ pr_info("SVN updated successfully\n");
+ return 0;
+ case SGX_NO_UPDATE:
+ /*
+ * SVN update failed since the current SVN is
+ * not newer than CPUSVN. This is the most
+ * common case and indicates no harm.
+ */
+ return 0;
+ case SGX_INSUFFICIENT_ENTROPY:
+ /*
+ * SVN update failed due to lack of entropy in DRNG.
+ * Indicate to userspace that it should retry.
+ */
+ return -EAGAIN;
+ default:
+ break;
+ }
+
+ /*
+ * EUPDATESVN was called when EPC is empty, all other error
+ * codes are unexpected.
+ */
+ ENCLS_WARN(ret, "EUPDATESVN");
+ return -EIO;
+}
+
int sgx_inc_usage_count(void)
{
return 0;
--
2.45.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN]
2025-08-14 7:34 ` [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN] Elena Reshetova
@ 2025-08-14 9:35 ` Huang, Kai
2025-08-14 11:48 ` Reshetova, Elena
0 siblings, 1 reply; 16+ messages in thread
From: Huang, Kai @ 2025-08-14 9:35 UTC (permalink / raw)
To: Reshetova, Elena, Hansen, Dave
Cc: seanjc@google.com, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, linux-sgx@vger.kernel.org,
Raynor, Scott
On Thu, 2025-08-14 at 10:34 +0300, Reshetova, Elena wrote:
> All running enclaves and cryptographic assets (such as internal SGX
> encryption keys) are assumed to be compromised whenever an SGX-related
> microcode update occurs. To mitigate this assumed compromise the new
> supervisor SGX instruction ENCLS[EUPDATESVN] can generate fresh
> cryptographic assets.
>
> Before executing EUPDATESVN, all SGX memory must be marked as unused. This
> requirement ensures that no potentially compromised enclave survives the
> update and allows the system to safely regenerate cryptographic assets.
>
> Add the method to perform ENCLS[EUPDATESVN]. However, until the follow up
> patch that wires calling sgx_update_svn() from sgx_inc_usage_count(), this
> code is not reachable.
>
> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
Reviewed-by: Kai Huang <kai.huang@intel.com>
>
> + * Return:
> + * * %0: - Success or not supported
> + * * %-EAGAIN: - Can be safely retried, failure is due to lack of
> + * * entropy in RNG
Nit: if another version is ever needed, I think it would be better to make
the text vertical aligned w/o the leading '-', i.e.,
* %-EAGAIN: - Can be ....
entropy in RNG.
.. instead of
* %-EAGAIN: - Can be ....
entropy in RNG.
^ permalink raw reply [flat|nested] 16+ messages in thread
* RE: [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN]
2025-08-14 9:35 ` Huang, Kai
@ 2025-08-14 11:48 ` Reshetova, Elena
2025-08-14 22:31 ` Huang, Kai
0 siblings, 1 reply; 16+ messages in thread
From: Reshetova, Elena @ 2025-08-14 11:48 UTC (permalink / raw)
To: Huang, Kai, Hansen, Dave
Cc: seanjc@google.com, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, linux-sgx@vger.kernel.org,
Raynor, Scott
> -----Original Message-----
> From: Huang, Kai <kai.huang@intel.com>
> Sent: Thursday, August 14, 2025 12:36 PM
> To: Reshetova, Elena <elena.reshetova@intel.com>; Hansen, Dave
> <dave.hansen@intel.com>
> Cc: seanjc@google.com; mingo@kernel.org; Scarlata, Vincent R
> <vincent.r.scarlata@intel.com>; x86@kernel.org; jarkko@kernel.org;
> Annapurve, Vishal <vannapurve@google.com>; linux-kernel@vger.kernel.org;
> Mallick, Asit K <asit.k.mallick@intel.com>; Aktas, Erdem
> <erdemaktas@google.com>; Cai, Chong <chongc@google.com>; Bondarevska,
> Nataliia <bondarn@google.com>; linux-sgx@vger.kernel.org; Raynor, Scott
> <scott.raynor@intel.com>
> Subject: Re: [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN]
>
> On Thu, 2025-08-14 at 10:34 +0300, Reshetova, Elena wrote:
> > All running enclaves and cryptographic assets (such as internal SGX
> > encryption keys) are assumed to be compromised whenever an SGX-related
> > microcode update occurs. To mitigate this assumed compromise the new
> > supervisor SGX instruction ENCLS[EUPDATESVN] can generate fresh
> > cryptographic assets.
> >
> > Before executing EUPDATESVN, all SGX memory must be marked as unused.
> This
> > requirement ensures that no potentially compromised enclave survives the
> > update and allows the system to safely regenerate cryptographic assets.
> >
> > Add the method to perform ENCLS[EUPDATESVN]. However, until the follow
> up
> > patch that wires calling sgx_update_svn() from sgx_inc_usage_count(), this
> > code is not reachable.
> >
> > Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
> > Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
>
> Reviewed-by: Kai Huang <kai.huang@intel.com>
>
> >
> > + * Return:
> > + * * %0: - Success or not supported
> > + * * %-EAGAIN: - Can be safely retried, failure is due to lack of
> > + * * entropy in RNG
>
> Nit: if another version is ever needed, I think it would be better to make
> the text vertical aligned w/o the leading '-', i.e.,
>
> * %-EAGAIN: - Can be ....
> entropy in RNG.
>
> .. instead of
>
> * %-EAGAIN: - Can be ....
> entropy in RNG.
OK, yes, this can be fixed, indeed.
Thank you very much for your reviews, Kai!
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN]
2025-08-14 11:48 ` Reshetova, Elena
@ 2025-08-14 22:31 ` Huang, Kai
2025-08-15 12:05 ` Reshetova, Elena
0 siblings, 1 reply; 16+ messages in thread
From: Huang, Kai @ 2025-08-14 22:31 UTC (permalink / raw)
To: Reshetova, Elena, Hansen, Dave
Cc: linux-sgx@vger.kernel.org, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, seanjc@google.com,
Raynor, Scott
> >
> > >
> > > + * Return:
> > > + * * %0: - Success or not supported
> > > + * * %-EAGAIN: - Can be safely retried, failure is due to lack of
> > > + * * entropy in RNG
> >
> > Nit: if another version is ever needed, I think it would be better to make
> > the text vertical aligned w/o the leading '-', i.e.,
> >
> > * %-EAGAIN: - Can be ....
> > entropy in RNG.
> >
> > .. instead of
> >
> > * %-EAGAIN: - Can be ....
> > entropy in RNG.
>
> OK, yes, this can be fixed, indeed.
>
I downloaded those patches and checked locally. I found there's an
unnecessary 'tab' between the error codes and the descriptions, making the
whitespace between them unnecessarily too long.
Please see below diff I came up with:
diff --git a/arch/x86/kernel/cpu/sgx/main.c
b/arch/x86/kernel/cpu/sgx/main.c
index cff5c4d22ac2..c6467628da04 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -929,10 +929,10 @@ static int sgx_usage_count;
* cryptographic assets.
*
* Return:
- * * %0: - Success or not supported
- * * %-EAGAIN: - Can be safely retried, failure is due to lack of
- * * entropy in RNG
- * * %-EIO: - Unexpected error, retries are not advisable
+ * * %0: - Success or not supported
+ * * %-EAGAIN: - Can be safely retried, failure is due to lack of
+ * * entropy in RNG
+ * * %-EIO: - Unexpected error, retries are not advisable
*/
static int sgx_update_svn(void)
{
^ permalink raw reply related [flat|nested] 16+ messages in thread
* RE: [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN]
2025-08-14 22:31 ` Huang, Kai
@ 2025-08-15 12:05 ` Reshetova, Elena
0 siblings, 0 replies; 16+ messages in thread
From: Reshetova, Elena @ 2025-08-15 12:05 UTC (permalink / raw)
To: Huang, Kai, Hansen, Dave
Cc: linux-sgx@vger.kernel.org, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, seanjc@google.com,
Raynor, Scott
> -----Original Message-----
> From: Huang, Kai <kai.huang@intel.com>
> Sent: Friday, August 15, 2025 1:31 AM
> To: Reshetova, Elena <elena.reshetova@intel.com>; Hansen, Dave
> <dave.hansen@intel.com>
> Cc: linux-sgx@vger.kernel.org; mingo@kernel.org; Scarlata, Vincent R
> <vincent.r.scarlata@intel.com>; x86@kernel.org; jarkko@kernel.org;
> Annapurve, Vishal <vannapurve@google.com>; linux-kernel@vger.kernel.org;
> Mallick, Asit K <asit.k.mallick@intel.com>; Aktas, Erdem
> <erdemaktas@google.com>; Cai, Chong <chongc@google.com>; Bondarevska,
> Nataliia <bondarn@google.com>; seanjc@google.com; Raynor, Scott
> <scott.raynor@intel.com>
> Subject: Re: [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN]
>
>
> > >
> > > >
> > > > + * Return:
> > > > + * * %0: - Success or not supported
> > > > + * * %-EAGAIN: - Can be safely retried, failure is due to lack of
> > > > + * * entropy in RNG
> > >
> > > Nit: if another version is ever needed, I think it would be better to make
> > > the text vertical aligned w/o the leading '-', i.e.,
> > >
> > > * %-EAGAIN: - Can be ....
> > > entropy in RNG.
> > >
> > > .. instead of
> > >
> > > * %-EAGAIN: - Can be ....
> > > entropy in RNG.
> >
> > OK, yes, this can be fixed, indeed.
> >
>
> I downloaded those patches and checked locally. I found there's an
> unnecessary 'tab' between the error codes and the descriptions, making the
> whitespace between them unnecessarily too long.
>
> Please see below diff I came up with:
>
> diff --git a/arch/x86/kernel/cpu/sgx/main.c
> b/arch/x86/kernel/cpu/sgx/main.c
> index cff5c4d22ac2..c6467628da04 100644
> --- a/arch/x86/kernel/cpu/sgx/main.c
> +++ b/arch/x86/kernel/cpu/sgx/main.c
> @@ -929,10 +929,10 @@ static int sgx_usage_count;
> * cryptographic assets.
> *
> * Return:
> - * * %0: - Success or not supported
> - * * %-EAGAIN: - Can be safely retried, failure is due to lack of
> - * * entropy in RNG
> - * * %-EIO: - Unexpected error, retries are not advisable
> + * * %0: - Success or not supported
> + * * %-EAGAIN: - Can be safely retried, failure is due to lack of
> + * * entropy in RNG
> + * * %-EIO: - Unexpected error, retries are not advisable
> */
> static int sgx_update_svn(void)
> {
Thank you, I will use will rendering!
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
2025-08-14 7:34 [PATCH v14 0/5] Enable automatic SVN updates for SGX enclaves Elena Reshetova
` (3 preceding siblings ...)
2025-08-14 7:34 ` [PATCH v14 4/5] x86/sgx: Implement ENCLS[EUPDATESVN] Elena Reshetova
@ 2025-08-14 7:34 ` Elena Reshetova
2025-08-14 9:36 ` Huang, Kai
2025-08-14 16:50 ` Dave Hansen
4 siblings, 2 replies; 16+ messages in thread
From: Elena Reshetova @ 2025-08-14 7:34 UTC (permalink / raw)
To: dave.hansen
Cc: jarkko, seanjc, kai.huang, mingo, linux-sgx, linux-kernel, x86,
asit.k.mallick, vincent.r.scarlata, chongc, erdemaktas,
vannapurve, bondarn, scott.raynor, Elena Reshetova
== Background ==
ENCLS[EUPDATESVN] is a new SGX instruction [1] which allows enclave
attestation to include information about updated microcode SVN without a
reboot. Before an EUPDATESVN operation can be successful, all SGX memory
(aka. EPC) must be marked as “unused” in the SGX hardware metadata
(aka.EPCM). This requirement ensures that no compromised enclave can
survive the EUPDATESVN procedure and provides an opportunity to generate
new cryptographic assets.
== Solution ==
Attempt to execute ENCLS[EUPDATESVN] every time the first file descriptor
is obtained via sgx_(vepc_)open(). In the most common case the microcode
SVN is already up-to-date, and the operation succeeds without updating SVN.
Note: while in such cases the underlying crypto assets are regenerated, it
does not affect enclaves' visible keys obtained via EGETKEY instruction.
If it fails with any other error code than SGX_INSUFFICIENT_ENTROPY, this
is considered unexpected and the *open() returns an error. This should not
happen in practice.
On contrary, SGX_INSUFFICIENT_ENTROPY might happen due to a pressure on the
system's DRNG (RDSEED) and therefore the *open() can be safely retried to
allow normal enclave operation.
[1] Runtime Microcode Updates with Intel Software Guard Extensions,
https://cdrdv2.intel.com/v1/dl/getContent/648682
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
---
arch/x86/kernel/cpu/sgx/main.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c
index 69ab28641e20..cff5c4d22ac2 100644
--- a/arch/x86/kernel/cpu/sgx/main.c
+++ b/arch/x86/kernel/cpu/sgx/main.c
@@ -934,7 +934,7 @@ static int sgx_usage_count;
* * entropy in RNG
* * %-EIO: - Unexpected error, retries are not advisable
*/
-static int __maybe_unused sgx_update_svn(void)
+static int sgx_update_svn(void)
{
int ret;
@@ -992,14 +992,29 @@ static int __maybe_unused sgx_update_svn(void)
return -EIO;
}
+/* Mutex to ensure no concurrent EPC accesses during EUPDATESVN */
+static DEFINE_MUTEX(sgx_svn_lock);
+
int sgx_inc_usage_count(void)
{
+ int ret;
+
+ guard(mutex)(&sgx_svn_lock);
+
+ if (!sgx_usage_count) {
+ ret = sgx_update_svn();
+ if (ret)
+ return ret;
+ }
+
+ sgx_usage_count++;
+
return 0;
}
void sgx_dec_usage_count(void)
{
- return;
+ sgx_usage_count--;
}
static int __init sgx_init(void)
--
2.45.2
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
2025-08-14 7:34 ` [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves Elena Reshetova
@ 2025-08-14 9:36 ` Huang, Kai
2025-08-14 16:50 ` Dave Hansen
1 sibling, 0 replies; 16+ messages in thread
From: Huang, Kai @ 2025-08-14 9:36 UTC (permalink / raw)
To: Reshetova, Elena, Hansen, Dave
Cc: seanjc@google.com, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, linux-sgx@vger.kernel.org,
Raynor, Scott
On Thu, 2025-08-14 at 10:34 +0300, Reshetova, Elena wrote:
> == Background ==
>
> ENCLS[EUPDATESVN] is a new SGX instruction [1] which allows enclave
> attestation to include information about updated microcode SVN without a
> reboot. Before an EUPDATESVN operation can be successful, all SGX memory
> (aka. EPC) must be marked as “unused” in the SGX hardware metadata
> (aka.EPCM). This requirement ensures that no compromised enclave can
> survive the EUPDATESVN procedure and provides an opportunity to generate
> new cryptographic assets.
>
> == Solution ==
>
> Attempt to execute ENCLS[EUPDATESVN] every time the first file descriptor
> is obtained via sgx_(vepc_)open(). In the most common case the microcode
> SVN is already up-to-date, and the operation succeeds without updating SVN.
>
> Note: while in such cases the underlying crypto assets are regenerated, it
> does not affect enclaves' visible keys obtained via EGETKEY instruction.
>
> If it fails with any other error code than SGX_INSUFFICIENT_ENTROPY, this
> is considered unexpected and the *open() returns an error. This should not
> happen in practice.
>
> On contrary, SGX_INSUFFICIENT_ENTROPY might happen due to a pressure on the
> system's DRNG (RDSEED) and therefore the *open() can be safely retried to
> allow normal enclave operation.
>
> [1] Runtime Microcode Updates with Intel Software Guard Extensions,
> https://cdrdv2.intel.com/v1/dl/getContent/648682
>
> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
> Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
>
Reviewed-by: Kai Huang <kai.huang@intel.com>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
2025-08-14 7:34 ` [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves Elena Reshetova
2025-08-14 9:36 ` Huang, Kai
@ 2025-08-14 16:50 ` Dave Hansen
2025-08-14 21:40 ` Huang, Kai
1 sibling, 1 reply; 16+ messages in thread
From: Dave Hansen @ 2025-08-14 16:50 UTC (permalink / raw)
To: Elena Reshetova
Cc: jarkko, seanjc, kai.huang, mingo, linux-sgx, linux-kernel, x86,
asit.k.mallick, vincent.r.scarlata, chongc, erdemaktas,
vannapurve, bondarn, scott.raynor
On 8/14/25 00:34, Elena Reshetova wrote:
> +/* Mutex to ensure no concurrent EPC accesses during EUPDATESVN */
> +static DEFINE_MUTEX(sgx_svn_lock);
> +
> int sgx_inc_usage_count(void)
> {
> + int ret;
> +
> + guard(mutex)(&sgx_svn_lock);
> +
> + if (!sgx_usage_count) {
> + ret = sgx_update_svn();
> + if (ret)
> + return ret;
> + }
> +
> + sgx_usage_count++;
> +
> return 0;
> }
>
> void sgx_dec_usage_count(void)
> {
> - return;
> + sgx_usage_count--;
> }
How is a plain int-- safe?
Where's the locking?
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
2025-08-14 16:50 ` Dave Hansen
@ 2025-08-14 21:40 ` Huang, Kai
2025-08-15 6:59 ` Reshetova, Elena
0 siblings, 1 reply; 16+ messages in thread
From: Huang, Kai @ 2025-08-14 21:40 UTC (permalink / raw)
To: Reshetova, Elena, Hansen, Dave
Cc: seanjc@google.com, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, linux-sgx@vger.kernel.org,
Raynor, Scott
On Thu, 2025-08-14 at 09:50 -0700, Dave Hansen wrote:
> On 8/14/25 00:34, Elena Reshetova wrote:
> > +/* Mutex to ensure no concurrent EPC accesses during EUPDATESVN */
> > +static DEFINE_MUTEX(sgx_svn_lock);
> > +
> > int sgx_inc_usage_count(void)
> > {
> > + int ret;
> > +
> > + guard(mutex)(&sgx_svn_lock);
> > +
> > + if (!sgx_usage_count) {
> > + ret = sgx_update_svn();
> > + if (ret)
> > + return ret;
> > + }
> > +
> > + sgx_usage_count++;
> > +
> > return 0;
> > }
> >
> > void sgx_dec_usage_count(void)
> > {
> > - return;
> > + sgx_usage_count--;
> > }
>
> How is a plain int-- safe?
>
> Where's the locking?
Sorry I missed this during review too.
^ permalink raw reply [flat|nested] 16+ messages in thread
* RE: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
2025-08-14 21:40 ` Huang, Kai
@ 2025-08-15 6:59 ` Reshetova, Elena
2025-08-18 5:03 ` Huang, Kai
0 siblings, 1 reply; 16+ messages in thread
From: Reshetova, Elena @ 2025-08-15 6:59 UTC (permalink / raw)
To: Huang, Kai, Hansen, Dave
Cc: seanjc@google.com, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, linux-sgx@vger.kernel.org,
Raynor, Scott
> -----Original Message-----
> From: Huang, Kai <kai.huang@intel.com>
> Sent: Friday, August 15, 2025 12:41 AM
> To: Reshetova, Elena <elena.reshetova@intel.com>; Hansen, Dave
> <dave.hansen@intel.com>
> Cc: seanjc@google.com; mingo@kernel.org; Scarlata, Vincent R
> <vincent.r.scarlata@intel.com>; x86@kernel.org; jarkko@kernel.org;
> Annapurve, Vishal <vannapurve@google.com>; linux-kernel@vger.kernel.org;
> Mallick, Asit K <asit.k.mallick@intel.com>; Aktas, Erdem
> <erdemaktas@google.com>; Cai, Chong <chongc@google.com>; Bondarevska,
> Nataliia <bondarn@google.com>; linux-sgx@vger.kernel.org; Raynor, Scott
> <scott.raynor@intel.com>
> Subject: Re: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX
> enclaves
>
> On Thu, 2025-08-14 at 09:50 -0700, Dave Hansen wrote:
> > On 8/14/25 00:34, Elena Reshetova wrote:
> > > +/* Mutex to ensure no concurrent EPC accesses during EUPDATESVN */
> > > +static DEFINE_MUTEX(sgx_svn_lock);
> > > +
> > > int sgx_inc_usage_count(void)
> > > {
> > > + int ret;
> > > +
> > > + guard(mutex)(&sgx_svn_lock);
> > > +
> > > + if (!sgx_usage_count) {
> > > + ret = sgx_update_svn();
> > > + if (ret)
> > > + return ret;
> > > + }
> > > +
> > > + sgx_usage_count++;
> > > +
> > > return 0;
> > > }
> > >
> > > void sgx_dec_usage_count(void)
> > > {
> > > - return;
> > > + sgx_usage_count--;
> > > }
> >
> > How is a plain int-- safe?
> >
> > Where's the locking?
>
> Sorry I missed this during review too.
My line of thinking went that we don't actually
care or act on decrement (it is not a true ref counter)
and therefore, races here are ok. What I forgot is that
we loose basic atomicity also with plain int ((
I would personally like to go back to atomic (this is
what it is exactly for), but I can also just add another
mutex here. Preferences?
Best Regards,
Elena.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
2025-08-15 6:59 ` Reshetova, Elena
@ 2025-08-18 5:03 ` Huang, Kai
2025-08-18 5:41 ` Reshetova, Elena
0 siblings, 1 reply; 16+ messages in thread
From: Huang, Kai @ 2025-08-18 5:03 UTC (permalink / raw)
To: Reshetova, Elena, Hansen, Dave
Cc: linux-sgx@vger.kernel.org, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, seanjc@google.com,
Raynor, Scott
> > > >
> > > > void sgx_dec_usage_count(void)
> > > > {
> > > > - return;
> > > > + sgx_usage_count--;
> > > > }
> > >
> > > How is a plain int-- safe?
> > >
> > > Where's the locking?
> >
> > Sorry I missed this during review too.
>
> My line of thinking went that we don't actually
> care or act on decrement (it is not a true ref counter)
> and therefore, races here are ok. What I forgot is that
> we loose basic atomicity also with plain int ((
>
> I would personally like to go back to atomic (this is
> what it is exactly for), but I can also just add another
> mutex here. Preferences?
You don't need another mutex AFAICT, just use the one you already have.
The problem of the raw 'count--' is it is not multiple threads safe, e.g.,
IIUC, you could effectively lose one or more subtractions here, leading to
counter never reaching to 0.
From the perspective of functionality, to me there's no difference between
mutex vs atomic_t, so either would be fine. But as shown in your v7 [*],
it appears atomic_t version is still slightly more complicated than the
mutex.
So since we are already here, I would say just use the mutex:
void sgx_dec_usage_count(void)
{
guard(mutex)(&sgx_svn_lock);
sgx_usage_count--;
}
Am I missing anything?
[*]
https://lore.kernel.org/linux-sgx/20250711165212.1354943-1-elena.reshetova@intel.com/T/#me3d9ca942330039a59e2dd6e1d14b81c6670f87a
^ permalink raw reply [flat|nested] 16+ messages in thread
* RE: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves
2025-08-18 5:03 ` Huang, Kai
@ 2025-08-18 5:41 ` Reshetova, Elena
0 siblings, 0 replies; 16+ messages in thread
From: Reshetova, Elena @ 2025-08-18 5:41 UTC (permalink / raw)
To: Huang, Kai, Hansen, Dave
Cc: linux-sgx@vger.kernel.org, mingo@kernel.org, Scarlata, Vincent R,
x86@kernel.org, jarkko@kernel.org, Annapurve, Vishal,
linux-kernel@vger.kernel.org, Mallick, Asit K, Aktas, Erdem,
Cai, Chong, Bondarevska, Nataliia, seanjc@google.com,
Raynor, Scott
> -----Original Message-----
> From: Huang, Kai <kai.huang@intel.com>
> Sent: Monday, August 18, 2025 8:04 AM
> To: Reshetova, Elena <elena.reshetova@intel.com>; Hansen, Dave
> <dave.hansen@intel.com>
> Cc: linux-sgx@vger.kernel.org; mingo@kernel.org; Scarlata, Vincent R
> <vincent.r.scarlata@intel.com>; x86@kernel.org; jarkko@kernel.org;
> Annapurve, Vishal <vannapurve@google.com>; linux-kernel@vger.kernel.org;
> Mallick, Asit K <asit.k.mallick@intel.com>; Aktas, Erdem
> <erdemaktas@google.com>; Cai, Chong <chongc@google.com>; Bondarevska,
> Nataliia <bondarn@google.com>; seanjc@google.com; Raynor, Scott
> <scott.raynor@intel.com>
> Subject: Re: [PATCH v14 5/5] x86/sgx: Enable automatic SVN updates for SGX
> enclaves
>
> > > > >
> > > > > void sgx_dec_usage_count(void)
> > > > > {
> > > > > - return;
> > > > > + sgx_usage_count--;
> > > > > }
> > > >
> > > > How is a plain int-- safe?
> > > >
> > > > Where's the locking?
> > >
> > > Sorry I missed this during review too.
> >
> > My line of thinking went that we don't actually
> > care or act on decrement (it is not a true ref counter)
> > and therefore, races here are ok. What I forgot is that
> > we loose basic atomicity also with plain int ((
> >
> > I would personally like to go back to atomic (this is
> > what it is exactly for), but I can also just add another
> > mutex here. Preferences?
>
> You don't need another mutex AFAICT, just use the one you already have.
>
> The problem of the raw 'count--' is it is not multiple threads safe, e.g.,
> IIUC, you could effectively lose one or more subtractions here, leading to
> counter never reaching to 0.
Yes, this is what I call atomicity.
>
> From the perspective of functionality, to me there's no difference between
> mutex vs atomic_t, so either would be fine. But as shown in your v7 [*],
> it appears atomic_t version is still slightly more complicated than the
> mutex.
Yes, but it was because originally we tried to avoid mutex/lock in
fast (non-zero increment part). If we drop this requirement and condition
everything with the mutex (as now), then we can go back to simpler handling
via atomic in inc() and in dec() it would be simple atomic_dec(). Smth like this:
int sgx_inc_usage_count(void)
{
int ret;
guard(mutex)(&sgx_svn_lock);
if (atomic64_read(&sgx_usage_count)==0) {
ret = sgx_update_svn();
if (ret)
return ret;
}
atomic64_inc(&sgx_usage_count);
return 0;
}
void sgx_dec_usage_count(void)
{
atomic64_dec(&sgx_usage_count);
}
> So since we are already here, I would say just use the mutex:
>
> void sgx_dec_usage_count(void)
> {
> guard(mutex)(&sgx_svn_lock);
> sgx_usage_count--;
> }
>
> Am I missing anything?
The mutex just seems such an overkill for plain atomicity requirement here
in dec(). But sure, I can send the next version with mutex.
Best Regards,
Elena.
^ permalink raw reply [flat|nested] 16+ messages in thread