From nobody Sun Dec 14 12:16:20 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1104D3376B7; Thu, 16 Oct 2025 13:13:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620434; cv=none; b=iJxNB7iFID6Ivk1NkbSLnnTrZp6Tk6tqYnluxWc6eh7+wpj6Dr7WPnhPnqsfgHlN5cRKdHz+wfh8tM8epY1pCAn8jkrDXFjU36dn+LnBfPRg02B1+7wLI1cgAf/mG5tu8FKeRutxd07KgqE8NFLNuV7DkePELB5t0Mxb7rP4orA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620434; c=relaxed/simple; bh=Ge0GfgKPm8igvB7m3Nk7r7BIfrVJNerw7/otRhzAzvs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q+QfaTJD6KgPDgMXmFlrN/g5EnsPjngMLjZ1x+uUhjrjfEs7/uUP100Dm//yLv7fuBhc8qC3F6fSx/gxtEq8fWlx9qWes1BN9+R9Avx823aMZJRyUySxhgrZ2z9tIekd6TVRlhMFhr92rqV7u0m58c90BVUEQFR0NR6sqKMRVXo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=KG45htv5; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="KG45htv5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1760620433; x=1792156433; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ge0GfgKPm8igvB7m3Nk7r7BIfrVJNerw7/otRhzAzvs=; b=KG45htv5bgoDmnn0aovrbASy4p7y8FtJ6A/FeMNJwoYhHZdwqnw4wz3g rxt+P+KFon+lgbJZJlrTF0vNhXMqEzKs8B0hWnxlLhi/OY5oLOVvry8X3 zjmvf8exGLEmWINq60K0rW1wtOE73PfydycQIZNn++/RZDFQoPpYb3oru qBOxSA1y6eAdo2IFMm6PiV1uJ1xz+f8WjpkQxuTv11F9G2lqggSiisU3G uxOAjoFKKc57uulxsKICBq7ZAYkr8OZdN96iCfH55LrtYtQ59RGxy9t32 gM7czJ4Ta8fdBFlMzxNxEYMIeRMQZKANQLvfIs02FcqAXAa9JXhLUk9Tj Q==; X-CSE-ConnectionGUID: VGVcTFZNR0yjpq9IoUGbGg== X-CSE-MsgGUID: 88JT6BAIRYiyAY4flYWvqg== X-IronPort-AV: E=McAfee;i="6800,10657,11584"; a="62904725" X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="62904725" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:13:52 -0700 X-CSE-ConnectionGUID: GXd1bLwzTr6eDhA3UqS0EA== X-CSE-MsgGUID: KJwFJNATQBedFOEx/MlIxw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="182015983" Received: from klitkey1-mobl1.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.10]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:13:48 -0700 From: Elena Reshetova To: dave.hansen@intel.com Cc: jarkko@kernel.org, seanjc@google.com, kai.huang@intel.com, mingo@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, asit.k.mallick@intel.com, vincent.r.scarlata@intel.com, chongc@google.com, erdemaktas@google.com, vannapurve@google.com, bondarn@google.com, scott.raynor@intel.com, Elena Reshetova Subject: [PATCH v17 1/5] x86/sgx: Introduce functions to count the sgx_(vepc_)open() Date: Thu, 16 Oct 2025 16:11:04 +0300 Message-ID: <20251016131314.17153-2-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20251016131314.17153-1-elena.reshetova@intel.com> References: <20251016131314.17153-1-elena.reshetova@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Kai Huang Reviewed-by: Jarkko Sakkinen Signed-off-by: Elena Reshetova Tested-by: Nataliia Bondarevska --- 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/dri= ver.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 =3D ~0x3; u32 sgx_misc_reserved_mask; =20 -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 *fi= le) return 0; } =20 +static int sgx_open(struct inode *inode, struct file *file) +{ + int ret; + + ret =3D sgx_inc_usage_count(); + if (ret) + return ret; + + ret =3D __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 =3D 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); =20 kfree(encl); + sgx_dec_usage_count(); } =20 /* 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_attribute= s, } EXPORT_SYMBOL_GPL(sgx_set_attribute); =20 +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 =20 +int sgx_inc_usage_count(void); +void sgx_dec_usage_count(void); + void sgx_update_lepubkeyhash(u64 *lepubkeyhash); =20 #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, stru= ct file *file) xa_destroy(&vepc->page_array); kfree(vepc); =20 + sgx_dec_usage_count(); return 0; } =20 -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; =20 @@ -273,6 +274,23 @@ static int sgx_vepc_open(struct inode *inode, struct f= ile *file) return 0; } =20 +static int sgx_vepc_open(struct inode *inode, struct file *file) +{ + int ret; + + ret =3D sgx_inc_usage_count(); + if (ret) + return ret; + + ret =3D __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) { --=20 2.45.2 From nobody Sun Dec 14 12:16:20 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 53707338F42; Thu, 16 Oct 2025 13:13:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620439; cv=none; b=pIXghB1bepdWgJt+HoGzAz7lfHw0fdeIgQsoFusiCYMPqpNf+1iyLX2XEaO+GV8b1kifuSbCYZVdL+FthKDw8X7N++anSmordMMetdnwr/SLoyqVCByaDA/jPaRTHLY9++L4csobmVx1a6HNx2Z1jUWw/9NoCU/lzQRijyCd7OE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620439; c=relaxed/simple; bh=jRoq7JF8rovb+jVv6uFar0LHx/5OA0LVzpA7utFnOGw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zd/00h7j8YpJASh9lzILVxIegcMKY2Lbtg+yl8jvgXRPw+ljSUPh8KlRMTvIgPmoD6ix3U/MlVeAATU+9TM+Y6Y/NMh4EYDWj9yWBK/UHLXqcBpvhBfDE8WHZSMr1hQ9pwYuMDaU4uBAxLt4kG1IrHh2VF9vgSKVq2FSmUEUQlU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=IJRaVccV; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="IJRaVccV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1760620438; x=1792156438; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=jRoq7JF8rovb+jVv6uFar0LHx/5OA0LVzpA7utFnOGw=; b=IJRaVccVtXc4SemnQApJeqUhMsrY2ATNubxhH5M4yGjRlL3uai7HeF40 vqL0A6Rfhdi+TsRANvMELsq1b4mPlLU2wZ293L8JwS2r3kEEkU269urBM 8Ppb9lmqy22IpYRVGD0unEslS1R2RC+g8FeVD/ybfYWUwVWW2mBXDMrNS 2/99hdhLQzStsu/GBa9bPS7dYTSqhxIsk6EVRH4Uw/b1R+GntiqhiaF/s 37wJn/7vqTLBOW6ErWdFFCYw6c4ZwsqMcEl9kw+OgRreslsyPqGijyMel 1vSjX3dAWhxVSkhkR3Zu5nZdeoEKMfZzSq9ALqGC1+DGXNasb8fyurWv8 Q==; X-CSE-ConnectionGUID: drT0YiksSyajhWsD6Doefw== X-CSE-MsgGUID: EWQpDh/2Q/KwUV91+nIv+g== X-IronPort-AV: E=McAfee;i="6800,10657,11584"; a="62904728" X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="62904728" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:13:58 -0700 X-CSE-ConnectionGUID: Y2fCOYF8QuCuCZ7dA9sd7Q== X-CSE-MsgGUID: OLO6mKHgQ8anth41zjrWqg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="182015994" Received: from klitkey1-mobl1.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.10]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:13:53 -0700 From: Elena Reshetova To: dave.hansen@intel.com Cc: jarkko@kernel.org, seanjc@google.com, kai.huang@intel.com, mingo@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, asit.k.mallick@intel.com, vincent.r.scarlata@intel.com, chongc@google.com, erdemaktas@google.com, vannapurve@google.com, bondarn@google.com, scott.raynor@intel.com, Elena Reshetova , Dave Hansen Subject: [PATCH v17 2/5] x86/cpufeatures: Add X86_FEATURE_SGX_EUPDATESVN feature flag Date: Thu, 16 Oct 2025 16:11:05 +0300 Message-ID: <20251016131314.17153-3-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20251016131314.17153-1-elena.reshetova@intel.com> References: <20251016131314.17153-1-elena.reshetova@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Kai Huang Reviewed-by: Dave Hansen Signed-off-by: Elena Reshetova Tested-by: Nataliia Bondarevska --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/cpuid-deps.c | 1 + arch/x86/kernel/cpu/scattered.c | 1 + 3 files changed, 3 insertions(+) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpuf= eatures.h index 4091a776e37a..76364b6dd93b 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -499,6 +499,7 @@ #define X86_FEATURE_IBPB_EXIT_TO_USER (21*32+14) /* Use IBPB on exit-to-us= erspace, see VMSCAPE bug */ #define X86_FEATURE_ABMC (21*32+15) /* Assignable Bandwidth Monitoring Co= unters */ #define X86_FEATURE_MSR_IMM (21*32+16) /* MSR immediate form instructions= */ +#define X86_FEATURE_SGX_EUPDATESVN (21*32+17) /* Support for ENCLS[EUPDATE= SVN] instruction */ =20 /* * BUG word(s) diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-d= eps.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[] =3D { { 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/scattere= d.c index caa4dc885c21..37850356a9b5 100644 --- a/arch/x86/kernel/cpu/scattered.c +++ b/arch/x86/kernel/cpu/scattered.c @@ -43,6 +43,7 @@ static const struct cpuid_bit cpuid_bits[] =3D { { 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 }, --=20 2.45.2 From nobody Sun Dec 14 12:16:20 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0785C337696; Thu, 16 Oct 2025 13:14:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620445; cv=none; b=W04WOdcrUeqVZ/JmPCpLm4uomvQVkYIiE1NAOWBDRcKRfWh9hliEojLWyF3Oo/NHGueqEPqvFQxF6hbBDeJwntTaMqYlKQHY1jfV2NsqTwVLXY/fmvozo96ZWp+4/vCQopTbtrtG59dW0bwU9VcntdXXwAA8TQne4ewHE/HF18I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620445; c=relaxed/simple; bh=omk+H227w99hQHnvhhAyXagsaFNACP5FrVuwGpjF8XU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xawv7dYOmYGWYvA1vD3yko6OTaRUD5/rYIX99kLd0S2PZ8qbKGjYPjGPQZ7XB//vlbLaymMc63NUetC+DBjfBFRrb85Wr73/2Vl8fszoDVg27zfxwew8MwWp+yUyqYSirWnjl130pvQawHo/xFnL2B+38d5UOE/+lc3WvDSayUQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Qmgt1mwY; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Qmgt1mwY" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1760620444; x=1792156444; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=omk+H227w99hQHnvhhAyXagsaFNACP5FrVuwGpjF8XU=; b=Qmgt1mwYjMaSW0ndPglyZefZz2lVX5hDMdBznp8M5iuwmeRcNpoe+WPB qksPVhl9swWJ5PJ/Tn1FWwg9WiWG9v8x2nVF1707IsDdsTlGIun8vRdEs kTYdJuPqLIx+dN+OK7i3mHySu2dgA8gmDP/rP4paSWCy3UB0C4cvloWZB GUG7bT4o7Hj0kNRhDsBdR813+MH6490oj9oHfU/o5wzZo6lG7l2I4iFp1 QXjxMF/v6IGpsTOTP5AWNjNptM74XJEVFMo643DdWrYc6Q3KniSbYSCtp uhUhPJYoCUO7jpkvR4GDoBpd2u+3IlTKBOl+5VE7eZYiC3Ed5coFwIAeB g==; X-CSE-ConnectionGUID: wCBIrgH/TVOYp6NS8ryumw== X-CSE-MsgGUID: wvksVGIzSYmnJSxS2jBPmQ== X-IronPort-AV: E=McAfee;i="6800,10657,11584"; a="62904735" X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="62904735" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:14:03 -0700 X-CSE-ConnectionGUID: xiRynRS3QfyAJx1oznHoFw== X-CSE-MsgGUID: RilIYjLYQ+aaMhkbxHAnFA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="182016005" Received: from klitkey1-mobl1.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.10]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:13:59 -0700 From: Elena Reshetova To: dave.hansen@intel.com Cc: jarkko@kernel.org, seanjc@google.com, kai.huang@intel.com, mingo@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, asit.k.mallick@intel.com, vincent.r.scarlata@intel.com, chongc@google.com, erdemaktas@google.com, vannapurve@google.com, bondarn@google.com, scott.raynor@intel.com, Elena Reshetova Subject: [PATCH v17 3/5] x86/sgx: Define error codes for use by ENCLS[EUPDATESVN] Date: Thu, 16 Oct 2025 16:11:06 +0300 Message-ID: <20251016131314.17153-4-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20251016131314.17153-1-elena.reshetova@intel.com> References: <20251016131314.17153-1-elena.reshetova@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Jarkko Sakkinen Signed-off-by: Elena Reshetova Tested-by: Nataliia Bondarevska --- 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 =3D 13, SGX_INVALID_EINITTOKEN =3D 16, SGX_PAGE_NOT_MODIFIABLE =3D 20, + SGX_INSUFFICIENT_ENTROPY =3D 29, + SGX_NO_UPDATE =3D 31, SGX_UNMASKED_EVENT =3D 128, }; =20 --=20 2.45.2 From nobody Sun Dec 14 12:16:20 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8959335BC8; Thu, 16 Oct 2025 13:14:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620450; cv=none; b=GQUs6+5bJtWARW3AFiz77hXcnKBTtyKzQXfTWIm87hgjBVpVXlI+hYx4xoeC8KlQNtwXbZduWYjloUKMObJY30uuEho6Flb/r8DSyjHuy41MaqfLC6/sWhvD3FEnrFaw1g99CGdXRt+NdN6lKOT9KmpZ238FhBb2zWihjiFTtaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620450; c=relaxed/simple; bh=XEWcIXPhVodD+hPjudKanXF8EQDvHpVubtWozV1Qmsg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VCvV3T9VIDHmqgR+FYACQWvaRuMcaIjP/ydzoFmhwuJ+vndAsfUN8vbnqbNrvtSOH6bxnaXSUGYvDtWuFCvdmHQrNXmQzI+KTgNwXJCH6WRmckR6ydbSx4bFh1EmlHRfousk4JEGbgKJOi4l9pzUXAHb0lkSMjaq4urlWzi3qYA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=HQ20DW7c; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="HQ20DW7c" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1760620448; x=1792156448; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=XEWcIXPhVodD+hPjudKanXF8EQDvHpVubtWozV1Qmsg=; b=HQ20DW7cp+2I1GB/ylFHRnqs1AQJ8l77GH9v6xoTwoVDWRLEK+QkR7yr +TH12WLxG1/G6HSpHoW3XBJ0eAgiCtDBYiNORzmPA2l89j58XoKvr7L1V pN3433RfxDm9tnzJ/sdIZRBjE7aIB+DiJAecCizFMV5zshl6CUg4tXMZc JJFRjzZSqBUWzudg/F6SRoo3tesvdnKnmAfx0cBZoGKBecrKsyx7zeI24 oKsejhKo6nBCWPHIXfz8HXypAPZ+tilvGC1JlRgMNZNRUn+TM2f8G7etS c4mS5PVCT6XFAS9V9I3aBoFno+5JK0ztzLBHrw1Zizwd5LVFK8nOtME+Q g==; X-CSE-ConnectionGUID: xryI1h4cTTmw80ow+imDqA== X-CSE-MsgGUID: FZ4yMasiToqgGVesUIGIiA== X-IronPort-AV: E=McAfee;i="6800,10657,11584"; a="62904741" X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="62904741" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:14:08 -0700 X-CSE-ConnectionGUID: Sf9Jlh63SPu6FiN6Ldu8zg== X-CSE-MsgGUID: 5rBIqRqKRfSHmmhTYhErBQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="182016017" Received: from klitkey1-mobl1.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.10]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:14:04 -0700 From: Elena Reshetova To: dave.hansen@intel.com Cc: jarkko@kernel.org, seanjc@google.com, kai.huang@intel.com, mingo@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, asit.k.mallick@intel.com, vincent.r.scarlata@intel.com, chongc@google.com, erdemaktas@google.com, vannapurve@google.com, bondarn@google.com, scott.raynor@intel.com, Elena Reshetova Subject: [PATCH v17 4/5] x86/sgx: Implement ENCLS[EUPDATESVN] Date: Thu, 16 Oct 2025 16:11:07 +0300 Message-ID: <20251016131314.17153-5-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20251016131314.17153-1-elena.reshetova@intel.com> References: <20251016131314.17153-1-elena.reshetova@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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: Kai Huang Reviewed-by: Jarkko Sakkinen Signed-off-by: Elena Reshetova Tested-by: Nataliia Bondarevska --- 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) =20 enum sgx_encls_function { - ECREATE =3D 0x00, - EADD =3D 0x01, - EINIT =3D 0x02, - EREMOVE =3D 0x03, - EDGBRD =3D 0x04, - EDGBWR =3D 0x05, - EEXTEND =3D 0x06, - ELDU =3D 0x08, - EBLOCK =3D 0x09, - EPA =3D 0x0A, - EWB =3D 0x0B, - ETRACK =3D 0x0C, - EAUG =3D 0x0D, - EMODPR =3D 0x0E, - EMODT =3D 0x0F, + ECREATE =3D 0x00, + EADD =3D 0x01, + EINIT =3D 0x02, + EREMOVE =3D 0x03, + EDGBRD =3D 0x04, + EDGBWR =3D 0x05, + EEXTEND =3D 0x06, + ELDU =3D 0x08, + EBLOCK =3D 0x09, + EPA =3D 0x0A, + EWB =3D 0x0B, + ETRACK =3D 0x0C, + EAUG =3D 0x0D, + EMODPR =3D 0x0E, + EMODT =3D 0x0F, + EUPDATESVN =3D 0x18, }; =20 /** diff --git a/arch/x86/kernel/cpu/sgx/encls.h b/arch/x86/kernel/cpu/sgx/encl= s.h index 42a088a337c5..74be751199a4 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, v= oid *addr) return __encls_2(EAUG, pginfo, addr); } =20 +/* 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..ffc7b9496218 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "driver.h" #include "encl.h" #include "encls.h" @@ -917,6 +918,80 @@ int sgx_set_attribute(unsigned long *allowed_attribute= s, } EXPORT_SYMBOL_GPL(sgx_set_attribute); =20 +/* 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 =3D 0; i < RDRAND_RETRY_LOOPS; i++) { + ret =3D __eupdatesvn(); + + /* Stop on success or unexpected errors: */ + if (ret !=3D 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; --=20 2.45.2 From nobody Sun Dec 14 12:16:20 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C4E1033A008; Thu, 16 Oct 2025 13:14:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620456; cv=none; b=NV8m0pYwNCpWOdwnSOWSXsstzZ7uQL42jGbWg8V9zEIDOf0V/axRiw/Wv7jmfegYuHjnuay2ePVg4UL/xEsO/i1k5+qCsclsIWrBgPGPyWrCyoR7QTXuLG9azNFI0x+WAdn+S7KrFoZ/zuzguV4nCbVSwYqXip1UNITmZltwIlw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760620456; c=relaxed/simple; bh=0N8/qd5qzLHbFEVNvXz6ej7DAPmf2eK2l9UUMC1Qd9c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ANuxCHNjYM0HFVo1uyuJ3TN3Dkk25YgMBddbskh3o4k+DLgkJKb5+z58g5yywR/gqUR8xBxSqMPl58HG64x83LTN2iJ7Js09Ls1nMcb9niFADIubMZhG+n62k1GRkUL+d59AzcfVI8AWkt2nWArBWHN3yQiGD9Z64VzS1mZz9Fg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Vn7F8YCc; arc=none smtp.client-ip=192.198.163.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Vn7F8YCc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1760620454; x=1792156454; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0N8/qd5qzLHbFEVNvXz6ej7DAPmf2eK2l9UUMC1Qd9c=; b=Vn7F8YCcrw7zgKVrHyJ4DXdAPpl22cmnxqON7RmQg3oJldJ71031w+e7 AwQ6EAImvMR3CSk11e2EAPcKnhzipB7W83/KkFPyT9BiLQgQwwIhpQogr TrUzUqLkjOb7HwEg9kpOygshKGbnIfcyJc4y3lbeks/PRYnmT+oaHjec2 yxMLIL4Fsr/ar1WRW1jPQefinZ67I450+/MwesHRSzh4N+hPj2ty9I2DZ sbhPkvr4fk2xj5A3JZXvrk4Wq1CHWbofJBdeeX4V7zGQZto9m3f/A4TY9 4jZCBxRHiKNB4g/tCGNfrTUffFLaCDgBL87vUdGa7aYZEN5g2rjBdeiGl g==; X-CSE-ConnectionGUID: xcJsGYceS1qYXX3ubSUvYA== X-CSE-MsgGUID: oifVkcWSTfaN0ajvjXHJsw== X-IronPort-AV: E=McAfee;i="6800,10657,11584"; a="62904750" X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="62904750" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:14:14 -0700 X-CSE-ConnectionGUID: /6qmLXwSSuujN6n5tJPkyg== X-CSE-MsgGUID: GPjxXppZQzOxZThp/c/j8Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.19,234,1754982000"; d="scan'208";a="182016030" Received: from klitkey1-mobl1.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.10]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2025 06:14:09 -0700 From: Elena Reshetova To: dave.hansen@intel.com Cc: jarkko@kernel.org, seanjc@google.com, kai.huang@intel.com, mingo@kernel.org, linux-sgx@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, asit.k.mallick@intel.com, vincent.r.scarlata@intel.com, chongc@google.com, erdemaktas@google.com, vannapurve@google.com, bondarn@google.com, scott.raynor@intel.com, Elena Reshetova Subject: [PATCH v17 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves Date: Thu, 16 Oct 2025 16:11:08 +0300 Message-ID: <20251016131314.17153-6-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20251016131314.17153-1-elena.reshetova@intel.com> References: <20251016131314.17153-1-elena.reshetova@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable =3D=3D Background =3D=3D 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 =E2=80=9Cunused=E2=80=9D in the SGX hardware m= etadata (aka.EPCM). This requirement ensures that no compromised enclave can survive the EUPDATESVN procedure and provides an opportunity to generate new cryptographic assets. =3D=3D Solution =3D=3D 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: Kai Huang Reviewed-by: Jarkko Sakkinen Signed-off-by: Elena Reshetova Tested-by: Nataliia Bondarevska --- arch/x86/kernel/cpu/sgx/main.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index ffc7b9496218..3eda7e7942e6 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; =20 @@ -992,14 +992,30 @@ static int __maybe_unused sgx_update_svn(void) return -EIO; } =20 +/* 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 =3D sgx_update_svn(); + if (ret) + return ret; + } + + sgx_usage_count++; + return 0; } =20 void sgx_dec_usage_count(void) { - return; + guard(mutex)(&sgx_svn_lock); + sgx_usage_count--; } =20 static int __init sgx_init(void) --=20 2.45.2