From nobody Tue Oct 7 01:56:33 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 641972E1734; Tue, 15 Jul 2025 12:41:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583293; cv=none; b=OF1Ra5dkG6azTYeqtwT3WHHFNC8ntycgkoIqKCZUXO6tAQcDyaY8tGJSiUzy9ac6byHbqUXo8qksGWMWJPKBz0jwflaMYFmaY7ThvxTxolgWJbITOJL6vVMviPThrd09cRRVNHyeltMMoTr766X2OmAhNGr+Y3oDV37qAE4TW2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583293; c=relaxed/simple; bh=nuaXZrkc4KSZxzqg0wh6FUNxF/aVlCe5hXHpamEieyk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i+h4q99PUVTNxk79ddqUptnHmmvSS4p+Q62SRh/HlGbTTI1eq7PfElDccASeXMTMdPYXoerrHVl1EfkRB781tGoZCQrRWTGKfnk8YR8XOKy6phnJ51/c5PL8Gu1+PswSNNx9jH3SLVbAQcOAyrmVRtk++fiXcbFnpWpLIadFtlQ= 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=P+5hjcCI; arc=none smtp.client-ip=192.198.163.9 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="P+5hjcCI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1752583291; x=1784119291; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nuaXZrkc4KSZxzqg0wh6FUNxF/aVlCe5hXHpamEieyk=; b=P+5hjcCIQsK5QSYSOWRnnBfUg0HyCNIG3fxuOWRViiqyzVG8/Yj6tU4W u2hbiqc2HCJQCjiMWqkwFulVhE+9PYqk8f0Os4nNAnfAjExSNn2E7s9Yv VrsO6b432e+R1e+/T8fMTUob7YXMSJ2y/2d9PicVcFFHhbY4HSxS2AfWg Hyf4mndudG27DbIgG660f8HeJ6I4GbpzbeAK7WyjCWg/CLdMX1IUoARBi D4g3UFsi6PyAjeveio95huluamGloI7i3bU/W3H6F6NQWbfkJq9RD9HXH I/T0z9vDKvSObr83cy8Ti201d+FyiX/MapofkyNOlPvrg2W4RKU4JAGVS w==; X-CSE-ConnectionGUID: CfdrEnRpQYOIcRbV/WFSKA== X-CSE-MsgGUID: 1QpVVdnPTr+DV4LnwryoiA== X-IronPort-AV: E=McAfee;i="6800,10657,11493"; a="65498176" X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="65498176" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:31 -0700 X-CSE-ConnectionGUID: +ADNgoZYRwe6LmPzOXob5Q== X-CSE-MsgGUID: w8Bsf1HDTBCbyISJOSq2Qg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="157018299" Received: from jkrzyszt-mobl2.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.245.22]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:26 -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 v8 1/5] x86/sgx: Introduce a counter to count the sgx_(vepc_)open() Date: Tue, 15 Jul 2025 15:40:18 +0300 Message-ID: <20250715124109.1711717-2-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250715124109.1711717-1-elena.reshetova@intel.com> References: <20250715124109.1711717-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 SGX does not have a global counter to count the active users from userspace or hypervisor. Implement such a counter, sgx_usage_count. It will be used by the driver when attempting to call EUPDATESVN SGX instruction. Note: the sgx_inc_usage_count prototype is defined to return int for the cleanliness of the follow-up patches. When the EUPDATESVN SGX instruction will be enabled in the follow-up patch, the sgx_inc_usage_count will start to return int. Suggested-by: Sean Christopherson Signed-off-by: Elena Reshetova Acked-by: Kai Huang --- arch/x86/kernel/cpu/sgx/driver.c | 22 ++++++++++++++++------ arch/x86/kernel/cpu/sgx/encl.c | 1 + arch/x86/kernel/cpu/sgx/main.c | 14 ++++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 3 +++ arch/x86/kernel/cpu/sgx/virt.c | 16 ++++++++++++++-- 5 files changed, 48 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/dri= ver.c index 7f8d1e11dbee..a2994a74bdff 100644 --- a/arch/x86/kernel/cpu/sgx/driver.c +++ b/arch/x86/kernel/cpu/sgx/driver.c @@ -19,9 +19,15 @@ static int sgx_open(struct inode *inode, struct file *fi= le) struct sgx_encl *encl; int ret; =20 + ret =3D sgx_inc_usage_count(); + if (ret) + return ret; + encl =3D kzalloc(sizeof(*encl), GFP_KERNEL); - if (!encl) - return -ENOMEM; + if (!encl) { + ret =3D -ENOMEM; + goto err_usage_count; + } =20 kref_init(&encl->refcount); xa_init(&encl->page_array); @@ -31,14 +37,18 @@ static int sgx_open(struct inode *inode, struct file *f= ile) spin_lock_init(&encl->mm_lock); =20 ret =3D init_srcu_struct(&encl->srcu); - if (ret) { - kfree(encl); - return ret; - } + if (ret) + goto err_encl; =20 file->private_data =3D encl; =20 return 0; + +err_encl: + kfree(encl); +err_usage_count: + sgx_dec_usage_count(); + return ret; } =20 static int sgx_release(struct inode *inode, struct file *file) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index 279148e72459..3b54889ae4a4 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..0e75090f93c9 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -917,6 +917,20 @@ 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; + +int sgx_inc_usage_count(void) +{ + sgx_usage_count++; + return 0; +} + +void sgx_dec_usage_count(void) +{ + sgx_usage_count--; +} + 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..6ce908ed51c9 100644 --- a/arch/x86/kernel/cpu/sgx/virt.c +++ b/arch/x86/kernel/cpu/sgx/virt.c @@ -255,22 +255,34 @@ 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) { struct sgx_vepc *vepc; + int ret; + + ret =3D sgx_inc_usage_count(); + if (ret) + return ret; =20 vepc =3D kzalloc(sizeof(struct sgx_vepc), GFP_KERNEL); - if (!vepc) - return -ENOMEM; + if (!vepc) { + ret =3D -ENOMEM; + goto err_usage_count; + } mutex_init(&vepc->lock); xa_init(&vepc->page_array); =20 file->private_data =3D vepc; =20 return 0; + +err_usage_count: + sgx_dec_usage_count(); + return ret; } =20 static long sgx_vepc_ioctl(struct file *file, --=20 2.45.2 From nobody Tue Oct 7 01:56:33 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 62B872E1753; Tue, 15 Jul 2025 12:41:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583298; cv=none; b=GOIHsvnQD/9acrXC2tfGOUjcElv1MHxKTh5wUgBwtkCa+dFa+UJ+DPeDZaKra+dy6oNcOLI4hScycqAJ0QIpHKtmJsEjCb9kWGm9CQq+cauFTXyurbYKB2jUVfBxYLoeFqkAwUNhkD+p7B1+6yn/xBjSD/y+GkEQ3rXfIqTcBJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583298; c=relaxed/simple; bh=p3OfQo+a9uQB6l65p1mAORFqE3su7GRtll6CjNHX3NQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pFPyccp+PbOQqhOjuswIh4Szhrcwp1GqGKQW6Bw02+rE3jGfeXXeUfZIErbX05nixXUCMbyg6RDiy+tCs/GGEQBidV8EACc1pGGZWBL8MO9sEs9AfSFqjwZD2P68dkJHnSUfTE3fWcOUXrT+R+ekm4Yr/9xtf14Hbdh0E2+j89Q= 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=Zb2B6LFu; arc=none smtp.client-ip=192.198.163.9 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="Zb2B6LFu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1752583297; x=1784119297; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=p3OfQo+a9uQB6l65p1mAORFqE3su7GRtll6CjNHX3NQ=; b=Zb2B6LFuDxOxzmb2DlIeyzEvLDD2qJiwp7Y3H7wi52FHxw0Ufr7vPyDu AQ20wsivYG9selBMmYyU6U1TOhn/9Ski0XB7+f9ltnaAMsfNziTBRtyha d1OdKipRqhoYg70QRq6QQ5YWIli37rocWl82yXDT8vR0z0U42OKnFpALx rcJtkQ9T+jIW0LRinb8ySBO/XdB82Gx4YAsnUd2BfVIwoWDnVIdqJ6tAW adGAHM7pGwLwI4/CEcCSaNTBvcXTN+sxkXWkfP3bMXu22Q9AU+QwdmkT0 zOe8+k1cp5gaCn0s0LR+QweHriervvUaKU8VF74YA7vre3pIq9Wnu420E Q==; X-CSE-ConnectionGUID: Z/j221xgQkO9GgFQGV0ReQ== X-CSE-MsgGUID: xHmxU5AoTgCs1MRVLgmQ5w== X-IronPort-AV: E=McAfee;i="6800,10657,11493"; a="65498198" X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="65498198" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:37 -0700 X-CSE-ConnectionGUID: YcsNDj4eQu66d7IfQXkpFA== X-CSE-MsgGUID: RkTa7hSQTPefI3vguD48gg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="157018351" Received: from jkrzyszt-mobl2.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.245.22]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:31 -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 v8 2/5] x86/cpufeatures: Add X86_FEATURE_SGX_EUPDATESVN feature flag Date: Tue, 15 Jul 2025 15:40:19 +0300 Message-ID: <20250715124109.1711717-3-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250715124109.1711717-1-elena.reshetova@intel.com> References: <20250715124109.1711717-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. Signed-off-by: Elena Reshetova Acked-by: Kai Huang --- 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/cpuf= eatures.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[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 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[] =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 }, diff --git a/tools/arch/x86/include/asm/cpufeatures.h b/tools/arch/x86/incl= ude/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 d= ownclocking */ #define X86_FEATURE_APX (21*32+ 9) /* Advanced Performance Extensions */ #define X86_FEATURE_INDIRECT_THUNK_ITS (21*32+10) /* Use thunk for indirec= t branches in lower half of cacheline */ +#define X86_FEATURE_SGX_EUPDATESVN (21*32+14) /* Support for ENCLS[EUPDATE= SVN] instruction */ =20 /* * BUG word(s) --=20 2.45.2 From nobody Tue Oct 7 01:56:33 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 1843C2E041E; Tue, 15 Jul 2025 12:41:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583304; cv=none; b=jch4mmTihXpYj/ydelCWfwtDmJw4cRdyGVTS7XE6DQrc2ehSoZwP+BRQXF1EEmEpd7r4jbHt6vmup9AtC1aT9V2GrTWQySGF/+U9g3zqPBAoSUz7wFJz9qFBg0kMAGztPk4fgJnvQY7onncDz8uHbITySeFgk202FkhszRuduh0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583304; c=relaxed/simple; bh=5tn1s0xpEgieBaW2rEVLCUgMZNIIbtgh5f8AiANQjxI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PtgCp8YqE+nuE/oKZW88VzkydoP9dBInYsA/pQhNs1Zjr5BCrac96sPR8KoykA57QHjRkMPhHdQ1gVxyLdLTik3VzzmFDM3yscbbtnmDxUTTkLLcpnTsIMXjFQuPnmAwbAuIp/cGAWjzladoUDwWCLcnbNiUlBRuIDaLhtFeRj8= 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=OoGdIUuz; arc=none smtp.client-ip=192.198.163.9 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="OoGdIUuz" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1752583303; x=1784119303; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5tn1s0xpEgieBaW2rEVLCUgMZNIIbtgh5f8AiANQjxI=; b=OoGdIUuzFYK/wml0SpO9pONWqH966/LBSNUzsrXTp0K8ZDj3BHpBUv7G 6RIwslbOvoryXgk1ZHIjjsrWdkduw0OqX4H6p3RIW9lVbW+FJBb6ysL9H BP3r7usHCPFry4XA7PWXVDqHdy47L9PHfEovKY4IlFpmN7+b0TK+B3ZIt g164Gpby11YKYDfrgiV7eRjltnV0HkhoJ9Vn9rbqVVFSXS1qwpW6SS7uJ m0nVSXV74LMemnmAUfVdxgzXbz1E4X0RBcdB67OXlK0pWVN18AYVYTLt7 Z5R4ANWijmKRDYYnI76Q/CbOGRRNNxKMqHxB9WQZA0maHyGaitUcarogG A==; X-CSE-ConnectionGUID: dwcD9MRARNGjYWnAO8H2GA== X-CSE-MsgGUID: V4GHLuDMSWy9SdnOm6i4eg== X-IronPort-AV: E=McAfee;i="6800,10657,11493"; a="65498216" X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="65498216" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:43 -0700 X-CSE-ConnectionGUID: 0Fg16o6ITTKeIomAbOJZew== X-CSE-MsgGUID: 2wWQ6AJQSj6XzMjkk2UlIQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="157018379" Received: from jkrzyszt-mobl2.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.245.22]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:37 -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 v8 3/5] x86/sgx: Define error codes for use by ENCLS[EUPDATESVN] Date: Tue, 15 Jul 2025 15:40:20 +0300 Message-ID: <20250715124109.1711717-4-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250715124109.1711717-1-elena.reshetova@intel.com> References: <20250715124109.1711717-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. Signed-off-by: Elena Reshetova Acked-by: Kai Huang --- arch/x86/include/asm/sgx.h | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/sgx.h b/arch/x86/include/asm/sgx.h index 6a0069761508..1abf1461fab6 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 /** @@ -73,6 +74,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 was successful, but CPUSVN was not + * updated because current SVN was not newer than + * CPUSVN. * %SGX_UNMASKED_EVENT: An unmasked event, e.g. INTR, was received */ enum sgx_return_code { @@ -81,6 +86,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 Tue Oct 7 01:56:33 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 771DC2E173B; Tue, 15 Jul 2025 12:41:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583311; cv=none; b=lFIHAWb4vDsd6uyQdDIuxu87tlKuvMeYo74OpDIrik7bumUOSHDRbkY2OPEyJz9uMQ3AcxOCMgalJtoAKW+s1emE9YeVW5PsnWSCfECDpTy0WjKBD6fmp9Opj/33DjsvoJ5J1rk3Mg5SIjmWX9WQOhZausmd/vtiQrGqyfQkHks= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583311; c=relaxed/simple; bh=50l1WrZjc94LvangMr63Bs8tspnYXTz20qT2I6OQfG0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QrnPdWppqKc2YmigYo0Pvujkfv00XPLOAaRw93Ny4MWqAlcl9fx6p6KeyRksn3OijDZsmD0uBz9ADTWbqWqpPhjTxGIZV/jFpjnrnnscwwNskjxNIh2plYS+CYKk8Df900MA635uGAVnhJIDa+v0Rvqli/CpvpWHYNZkZBE3haE= 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=KJRWYgdH; arc=none smtp.client-ip=192.198.163.9 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="KJRWYgdH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1752583309; x=1784119309; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=50l1WrZjc94LvangMr63Bs8tspnYXTz20qT2I6OQfG0=; b=KJRWYgdHyLDg02l3CbSImb1QxoWAtwLnO52ouenfiE9Fv2BWvO0VswqI OEp0pfdiwHUgxxg/LRWIV5FAQeZelAlQ8L5YrEnIMYn3PfDin1pYnDvTJ Tgg4bNkHpSHHeQ2bi/9PVws0vGckTQSaQXzDM7d/sL6hJbAuWRiBGgVSF mAsPEL0tEchKRwIlxCUrF1kIRJBNDNvCmusdzcszOHeGrqaVg/CQ5yUOg qLweWmLuexahtl007WDdjYvBis55miiyXJ1OtKiqR5Jm+7hHRXxTPZO4y 4EcVkW4fmhNgpf1ujP8z652THu4L/9nOKo2FVeSnjCWRyiC/OpwUWf1zH A==; X-CSE-ConnectionGUID: DU6JzREMQeeQUoLiWHbmcA== X-CSE-MsgGUID: N7IMyf5FSIitNH8GO3Ft9w== X-IronPort-AV: E=McAfee;i="6800,10657,11493"; a="65498231" X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="65498231" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:49 -0700 X-CSE-ConnectionGUID: Voaqqp7dS76YeSd3yWtKsQ== X-CSE-MsgGUID: zBt1ojT9RsagUl66IBDXTQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="157018433" Received: from jkrzyszt-mobl2.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.245.22]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:43 -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 v8 4/5] x86/sgx: Implement ENCLS[EUPDATESVN] Date: Tue, 15 Jul 2025 15:40:21 +0300 Message-ID: <20250715124109.1711717-5-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250715124109.1711717-1-elena.reshetova@intel.com> References: <20250715124109.1711717-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]. Signed-off-by: Elena Reshetova Acked-by: Kai Huang --- arch/x86/kernel/cpu/sgx/encls.h | 5 +++ arch/x86/kernel/cpu/sgx/main.c | 61 +++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/encls.h b/arch/x86/kernel/cpu/sgx/encl= s.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, 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 0e75090f93c9..c97bba16e4fd 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" @@ -920,6 +921,66 @@ EXPORT_SYMBOL_GPL(sgx_set_attribute); /* Counter to count the active SGX users */ static int sgx_usage_count; =20 +/** + * 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. Must be called when EPC is empty. + * Most of the time, there will be no update and that's OK. + * If the failure is due to SGX_INSUFFICIENT_ENTROPY, the + * operation can be safely retried. In other failure cases, + * the retry should not be attempted. + * + * 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; + + 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; + } + + /* + * SVN successfully updated. + * Let users know when the update was successful. + */ + if (!ret) + pr_info("SVN updated successfully\n"); + + if (!ret || ret =3D=3D SGX_NO_UPDATE) + return 0; + + /* + * SVN update failed due to lack of entropy in DRNG. + * Indicate to userspace that it should retry. + */ + if (ret =3D=3D SGX_INSUFFICIENT_ENTROPY) + return -EAGAIN; + + /* + * 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) { sgx_usage_count++; --=20 2.45.2 From nobody Tue Oct 7 01:56:33 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.9]) (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 13DCD2E040D; Tue, 15 Jul 2025 12:41:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583316; cv=none; b=E7PwTSGSdbFRQSLGmOhFJWy3YcO+U06YzeW9BIND54u3kD/GV2eICXIkYZYisd6rmcase+5k7agddREVpsMdezZAV8ifHYkxGfTxAIZH2iMEhUG28PHcIUlWDs69V8pzHanSr4mDs7hUqT3rLRUpEEJEBI192akWnMy886nuf0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752583316; c=relaxed/simple; bh=kTPbS5WV4fcTLPZZqpeiPTQ0D+DrIU5oiRwpDe/RW10=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=r/CWyHZC6wM7w8gsJQ3Eb9BFjkpL7rL4TPX4ggJd++VYAya8fet6NHENdz6t4hYYYd1yui2DhMpuqN5z8uOsOaQ/AaIMOBS18p9hyroOaLLNZINQN6tRCEvEAMZ0ROQNMPY6Embc2Pl2lCQODvLdbcZ6nB2V/zecpZwMwwOPLmQ= 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=clmBtK4W; arc=none smtp.client-ip=192.198.163.9 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="clmBtK4W" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1752583315; x=1784119315; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kTPbS5WV4fcTLPZZqpeiPTQ0D+DrIU5oiRwpDe/RW10=; b=clmBtK4WXYj6QTp6vwKsRhCJSseA9u+a7ohNL44OmiM49+MMA8x+L4H3 X6D1KXVI2fnGpQfVQ/+u8ZPpc0CdkzozVzOE5S0EZBMB/fJM8Gw3PCzDI RTue0ian4u5i5CoNIX3nMm54YcL41ZH6BY2ZmxiuV0iTLSzCIKTZefGY2 UtSlBTTFS6NLQ6XCj8cDH/Ti5o3QSKGF2dsQPc0kfnRVYH04t5dSXO+Gu RVGOOndC3mbBLgBlMeiO+wXSvETdCXfXyMU90w/wQijtr7kaoOTIdq+sP 9KwKUHoUqfwvlEJXdq+qUIO3HlZegfObCSJ1oYbtqsO/2BRohW6lMZ3wb Q==; X-CSE-ConnectionGUID: DxiPeDfnRP2Tcyumi58EqA== X-CSE-MsgGUID: ye4c70JBRlyh1xvR6ABqpg== X-IronPort-AV: E=McAfee;i="6800,10657,11493"; a="65498242" X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="65498242" Received: from orviesa009.jf.intel.com ([10.64.159.149]) by fmvoesa103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:55 -0700 X-CSE-ConnectionGUID: deMFgW0RQECMKjVHMJQ3fw== X-CSE-MsgGUID: 9HH43WNdRSWE0el2hd1lNw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,313,1744095600"; d="scan'208";a="157018463" Received: from jkrzyszt-mobl2.ger.corp.intel.com (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.245.22]) by orviesa009-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Jul 2025 05:41:49 -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 v8 5/5] x86/sgx: Enable automatic SVN updates for SGX enclaves Date: Tue, 15 Jul 2025 15:40:22 +0300 Message-ID: <20250715124109.1711717-6-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250715124109.1711717-1-elena.reshetova@intel.com> References: <20250715124109.1711717-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 Patch Contents =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. 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 Signed-off-by: Elena Reshetova Acked-by: Kai Huang --- arch/x86/kernel/cpu/sgx/main.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index c97bba16e4fd..58cc342bfdbd 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -921,6 +921,9 @@ EXPORT_SYMBOL_GPL(sgx_set_attribute); /* Counter to count the active SGX users */ static int sgx_usage_count; =20 +/* Mutex to ensure no concurrent EPC accesses during EUPDATESVN */ +static DEFINE_MUTEX(sgx_svn_lock); + /** * sgx_update_svn() - Attempt to call ENCLS[EUPDATESVN]. * This instruction attempts to update CPUSVN to the @@ -937,7 +940,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 @@ -983,7 +986,11 @@ static int __maybe_unused sgx_update_svn(void) =20 int sgx_inc_usage_count(void) { - sgx_usage_count++; + guard(mutex)(&sgx_svn_lock); + + if (sgx_usage_count++ =3D=3D 0) + return sgx_update_svn(); + return 0; } =20 --=20 2.45.2