From nobody Mon Oct 6 06:31:23 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 7F8142737E9; Thu, 24 Jul 2025 08:03:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344220; cv=none; b=JqBIaX9iMrU/gadshmHDZUnHvpV/EN9Pz/WPVQekm/sxAMc0uhaTubJyV7hYzK2ORYYpQfrrx8mnnyYr4W7jT2Y3TVQewR2NuPb3r71Er1FLcTl2UsTyy0CLHEMZfc2a8YLoYccDsZPKSuaMcz8oPHVsZUgcBjab3O9XvLUwGPA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344220; c=relaxed/simple; bh=wdt6Kwvm2hqddw/1GaZbZ5bzXAIOICs3gTIo1FllAXw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RdDjPzwZ+wwUjg9w/FCBWZN/igxd6ldfsQEqevar6s0AmYnMcrWBxMK5IpoFCwIz1GfLVpFCWcRH/7+R+l5o7ZygChL4P3lvy+8fDh2gVIZd94PiLeNSRHjNzlTqOpBmyTU9zdUfKoWlC3iPdvcW9VM4iSLU1ZoA+YqOv6C6hZE= 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=PcmdQ/5u; arc=none smtp.client-ip=192.198.163.10 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="PcmdQ/5u" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1753344219; x=1784880219; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wdt6Kwvm2hqddw/1GaZbZ5bzXAIOICs3gTIo1FllAXw=; b=PcmdQ/5uL7IjtyZ81ZtUOSq3yKM4aJlmv35hsr0prVzspXWGkB2bbFgu QLs2eRgVm+VABvaVDTRTXcMxhc8cKY9ZrUfndts5YX5Qh/ChkO1EmcWFq yCGQoCI66fBFu/zX7KvonBD1TWn5glyytm6puTbKUrfRFyaeatpltDxmT FUVRAvgteJl7MxAfFdJAcpaTnYkNxUFZTeKl1Y4GvZjf75opHm7/z43S2 mFT3FCIuqIFuO/Dz+nXp8EqvU/+tFsCiVRHX/vfAkDVW7xYlwrOuVkMOJ XT8GTnRghz/5EMUdTEnKYXroIBIcTLVve/AcM+ZldDxaMrSgev4QEdntX w==; X-CSE-ConnectionGUID: MOOsqXJvTk2ReziL9u9ong== X-CSE-MsgGUID: 6tU1AHDMTYy3foYbKgL9mw== X-IronPort-AV: E=McAfee;i="6800,10657,11501"; a="66991920" X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="66991920" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:37 -0700 X-CSE-ConnectionGUID: POF8fgXkQj+6g2jC3NSdDA== X-CSE-MsgGUID: wv07hMlKSj605vW1mddfig== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="160341088" Received: from savramon-mobl1 (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.60]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:32 -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 v9 1/6] x86/sgx: Convert sgx_(vepc_)open to __sgx_(vepc_)open Date: Thu, 24 Jul 2025 11:02:10 +0300 Message-ID: <20250724080313.605676-2-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250724080313.605676-1-elena.reshetova@intel.com> References: <20250724080313.605676-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" In order to introduce the counting of active sgx users on top of clean functions that allocate vepc structures, covert existing sgx_(vepc_)open to __sgx_(vepc_)open. Later patch will introduce the top level wrappers that manage the usage count. No functional change intended in this patch. Signed-off-by: Elena Reshetova --- arch/x86/kernel/cpu/sgx/driver.c | 4 ++-- arch/x86/kernel/cpu/sgx/virt.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/dri= ver.c index 7f8d1e11dbee..9aa48f455c54 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; @@ -126,7 +126,7 @@ static long sgx_compat_ioctl(struct file *filep, unsign= ed int cmd, =20 static const struct file_operations sgx_encl_fops =3D { .owner =3D THIS_MODULE, - .open =3D sgx_open, + .open =3D __sgx_open, .release =3D sgx_release, .unlocked_ioctl =3D sgx_ioctl, #ifdef CONFIG_COMPAT diff --git a/arch/x86/kernel/cpu/sgx/virt.c b/arch/x86/kernel/cpu/sgx/virt.c index 7aaa3652e31d..d8fdf7f39215 100644 --- a/arch/x86/kernel/cpu/sgx/virt.c +++ b/arch/x86/kernel/cpu/sgx/virt.c @@ -258,7 +258,7 @@ static int sgx_vepc_release(struct inode *inode, struct= file *file) 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 @@ -291,7 +291,7 @@ static long sgx_vepc_ioctl(struct file *file, =20 static const struct file_operations sgx_vepc_fops =3D { .owner =3D THIS_MODULE, - .open =3D sgx_vepc_open, + .open =3D __sgx_vepc_open, .unlocked_ioctl =3D sgx_vepc_ioctl, .compat_ioctl =3D sgx_vepc_ioctl, .release =3D sgx_vepc_release, --=20 2.45.2 From nobody Mon Oct 6 06:31:23 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 DAF48274664; Thu, 24 Jul 2025 08:03:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344224; cv=none; b=rV4hbZw4ty8ywYsZVnskoX6us1H3on2V4UZRE+M8IWBfC77H0HdSDwAgqUkY26s8yvZGhkUk7yOnVw0kkfS56hUhkCbRB21sGjz+t0X+IOCwgXwv1rbOYbWl7gOg8DleH6DD0fOEitS2FoDKkDC4O4JrZX0SHBZ7ikP8r7697S8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344224; c=relaxed/simple; bh=nrpTmKiY9LjE5O/dGaCI3ZrBmAYWPMJ4nZKKlcacg20=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GQWdkP2GMN5k+/YvLFKmFSdZOxqRssrflUdrnWXXy1PownYSh9+0c3AsqQ1wH1w3eQdKpbmXCfsEgB/sMBhXkc6tf5WsfiILEu7qi5PyLsUDfdBjweYgiEhcWVv/fsc70yziwrrsw7DDUd9TnDBGStDRlik6eCkdEpTGSMNdHqw= 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=RQZipo9+; arc=none smtp.client-ip=192.198.163.10 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="RQZipo9+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1753344223; x=1784880223; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nrpTmKiY9LjE5O/dGaCI3ZrBmAYWPMJ4nZKKlcacg20=; b=RQZipo9+XnDD592T73YF4GHqXKkjN5Vo1zds/V6EZinfyS/BlHker/0z a0wfB48TRpGUQOtKITS9uXZyuXqzIA13jicMEKMNIwFaKJH2hdCDacB9D dxaECi9B8xkFHIUpq4NEMCO5raj8LtUb5tJwMWTMiUgxwSQK7iBBfNcdk MmlvjZNxRJKomoneJ26XSXhi5Gpob1nms+2l80m9AC94p94+YuU5Ge+oH UUsTeHM8mFeQRUWKxqGYV24TmyLTX/Y1WtN8oLbo1boYM9t/A2NtMm5xz xmceuOAy4kmEcDhmfVclsP/ijNStWB60andgD8Lk1cXbSkOzRRosC86+7 g==; X-CSE-ConnectionGUID: zOOjRczlRJKElUoOa9j+Vw== X-CSE-MsgGUID: jrIaxs2iSNqNcUN/9x40+g== X-IronPort-AV: E=McAfee;i="6800,10657,11501"; a="66991938" X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="66991938" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:42 -0700 X-CSE-ConnectionGUID: 44SY9nM5TdSmtyD5BHdvdA== X-CSE-MsgGUID: zRhE1ct+TfynUdQEm3gW3w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="160341348" Received: from savramon-mobl1 (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.60]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03: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 v9 2/6] x86/sgx: Introduce a counter to count the sgx_(vepc_)open() Date: Thu, 24 Jul 2025 11:02:11 +0300 Message-ID: <20250724080313.605676-3-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250724080313.605676-1-elena.reshetova@intel.com> References: <20250724080313.605676-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 --- arch/x86/kernel/cpu/sgx/driver.c | 19 ++++++++++++++++++- arch/x86/kernel/cpu/sgx/encl.c | 1 + arch/x86/kernel/cpu/sgx/main.c | 13 +++++++++++++ arch/x86/kernel/cpu/sgx/sgx.h | 3 +++ arch/x86/kernel/cpu/sgx/virt.c | 21 ++++++++++++++++++++- 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/driver.c b/arch/x86/kernel/cpu/sgx/dri= ver.c index 9aa48f455c54..79d6020dfe9c 100644 --- a/arch/x86/kernel/cpu/sgx/driver.c +++ b/arch/x86/kernel/cpu/sgx/driver.c @@ -41,6 +41,23 @@ static int __sgx_open(struct inode *inode, struct file *= file) 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; @@ -126,7 +143,7 @@ static long sgx_compat_ioctl(struct file *filep, unsign= ed int cmd, =20 static const struct file_operations sgx_encl_fops =3D { .owner =3D THIS_MODULE, - .open =3D __sgx_open, + .open =3D sgx_open, .release =3D sgx_release, .unlocked_ioctl =3D sgx_ioctl, #ifdef CONFIG_COMPAT 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..ccb81a649d1b 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -917,6 +917,19 @@ 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 __maybe_unused sgx_usage_count; + +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 d8fdf7f39215..d6bff258d761 100644 --- a/arch/x86/kernel/cpu/sgx/virt.c +++ b/arch/x86/kernel/cpu/sgx/virt.c @@ -255,6 +255,7 @@ static int sgx_vepc_release(struct inode *inode, struct= file *file) xa_destroy(&vepc->page_array); kfree(vepc); =20 + sgx_dec_usage_count(); return 0; } =20 @@ -265,6 +266,7 @@ static int __sgx_vepc_open(struct inode *inode, struct = file *file) vepc =3D kzalloc(sizeof(struct sgx_vepc), GFP_KERNEL); if (!vepc) return -ENOMEM; + mutex_init(&vepc->lock); xa_init(&vepc->page_array); =20 @@ -273,6 +275,23 @@ static int __sgx_vepc_open(struct inode *inode, struct= file *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) { @@ -291,7 +310,7 @@ static long sgx_vepc_ioctl(struct file *file, =20 static const struct file_operations sgx_vepc_fops =3D { .owner =3D THIS_MODULE, - .open =3D __sgx_vepc_open, + .open =3D sgx_vepc_open, .unlocked_ioctl =3D sgx_vepc_ioctl, .compat_ioctl =3D sgx_vepc_ioctl, .release =3D sgx_vepc_release, --=20 2.45.2 From nobody Mon Oct 6 06:31:23 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 1F39F2749C3; Thu, 24 Jul 2025 08:03:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344228; cv=none; b=oeV8D9/wDEHnM49ThgHirSDueXf1hTW8ABXoFGg3rNhkS5GUVXbal+tv48L8fd3FkG/q70F3dU/BEYYmcrYOQaPKMuydT2uzccR1V8NrqN7b9DjSbbRBrh1vhXZn9c0SJRDjLof6vn1TD6AkeIeIuNvaizXhVvjzaAMtlslICCI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344228; c=relaxed/simple; bh=p3OfQo+a9uQB6l65p1mAORFqE3su7GRtll6CjNHX3NQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mlrvwF7Jx6D+mmcGS+w0xgjSv3Chu6s1waaloPU8ufDuFTTPuxks24vbreIR+RaXpSk5cIXFCXLVzNOjAZOMvzc9D07BKbyS4rU389+f0+NGD01HUGtyqBAf0cxv/JAmMBkTb/nA29SLmg0xcDYcLOheAorYjxBg/cBR1JaJnqM= 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=h9i6kDz5; arc=none smtp.client-ip=192.198.163.10 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="h9i6kDz5" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1753344227; x=1784880227; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=p3OfQo+a9uQB6l65p1mAORFqE3su7GRtll6CjNHX3NQ=; b=h9i6kDz5C1ATmAUlJFPXjvjliT/vL5/dTU2DDWuVUTin/Gja8h+cb7t+ Ap0qXwufsXBNuYszTl/lAVtLM1ZsEbx9KyOfZ8hcK0oxwKu/nw0XUBqLC Qw/H9VwswbZcnEk1Q0Ur4qIXU/elieY/PesJkoxdcGeq+hlWwy1gvjo98 lSVIWCQpcIyxkph/kgUAvusnZhabXBctOLlTgT5iU/Ma+RUwTW8YTCcmn LpOM0VIlJ4DFm9Wb5xS3aL8UGAyzah2kEBIGX+ZpXlm5wKHN8cnjER4ux FzF5QqNDMdYXWkMor10qHQEBdYGIPgIQv0domlAkFmmQUZjQgCxhghTKZ A==; X-CSE-ConnectionGUID: wc1U3MAlQSKmvexilRjLiA== X-CSE-MsgGUID: lBqlpDrESGymZTZZqxHpuQ== X-IronPort-AV: E=McAfee;i="6800,10657,11501"; a="66991959" X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="66991959" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:47 -0700 X-CSE-ConnectionGUID: +tsMuZF5TlKnaFSn7WiXMw== X-CSE-MsgGUID: ZkVZ0zwIR5KJ241Dzd+UJw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="160341579" Received: from savramon-mobl1 (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.60]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:42 -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 v9 3/6] x86/cpufeatures: Add X86_FEATURE_SGX_EUPDATESVN feature flag Date: Thu, 24 Jul 2025 11:02:12 +0300 Message-ID: <20250724080313.605676-4-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250724080313.605676-1-elena.reshetova@intel.com> References: <20250724080313.605676-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 --- 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 Mon Oct 6 06:31:23 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 7779E274B4D; Thu, 24 Jul 2025 08:03:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344233; cv=none; b=HFsj+HTobEu2RwRm+qqLI1+CtY0zF3v3jHnFagq/7afq20Dj+SgXsIe8+qD7rY9b20nscOb08ohXS2Qy8qSUcsdd3LXO7aLD8yap64bHa6e1QT3ZYL/EtjuHYxd8rmr7NyThTimrTQOhjl4ktB/l6z52K3W5zUMIaR2rcmXcbYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344233; c=relaxed/simple; bh=5tn1s0xpEgieBaW2rEVLCUgMZNIIbtgh5f8AiANQjxI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bRSPfvWvaAXuLfVHf1ZFuEsCpDrBTTX5UN5HeNnUssgqfm4fAODn6j9NoOO2Syjb7ymwJsCZPpvijmd2TBizAAxrV63XselHZjuKP1ThuBvXuIEGshbXSpM8py4AtDX47Gxo86uutypS+o3DWxQvCzzJNgQ/2vNdhuy6aAkzHWA= 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=gveIbIrj; arc=none smtp.client-ip=192.198.163.10 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="gveIbIrj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1753344233; x=1784880233; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5tn1s0xpEgieBaW2rEVLCUgMZNIIbtgh5f8AiANQjxI=; b=gveIbIrjYKVJC0i3BjzZODadcME+aV7TXBvOQ+RPdjZWUqDixgvvP1gB W/0YZd/BIMAP0oj/7OsVxvYKh4adB4IMOm5Ws20rA5+ivJ7OvKill6bPC vH67aNAEdNzX0ht4ZExsbhL7XWLNJNIeqI34IE2+VPzSlMozLo1wusKDY /MWndgpOtUd28yaDNrr+raIQgHnf/BmqE/efDmSPqe2qxVHayTgBoczOO G6/GqRiHGQgz0NMCyogmQo6pSBH7dKLd16D7WNlfjteKVZaY7mrXqt2s5 glbl6WTg9ALkTMxdXQ4Hkm/nrdIRXv2QR3rT+h2ef/u3jUNtnxua4aiFR Q==; X-CSE-ConnectionGUID: LVY/QKJfSvWL9tuNKtWWyw== X-CSE-MsgGUID: bDmfCBi7SHOWibJvtPORQQ== X-IronPort-AV: E=McAfee;i="6800,10657,11501"; a="66991972" X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="66991972" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:53 -0700 X-CSE-ConnectionGUID: v98wCQaPSn+GsYbvI2v6Lw== X-CSE-MsgGUID: hWa08Ve4S1aQS44DO8RiAA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="160341827" Received: from savramon-mobl1 (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.60]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:47 -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 v9 4/6] x86/sgx: Define error codes for use by ENCLS[EUPDATESVN] Date: Thu, 24 Jul 2025 11:02:13 +0300 Message-ID: <20250724080313.605676-5-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250724080313.605676-1-elena.reshetova@intel.com> References: <20250724080313.605676-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 --- 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 Mon Oct 6 06:31:23 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 926CE272816; Thu, 24 Jul 2025 08:03:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344239; cv=none; b=JDR6ersupMxDvEvj4s1IxopNdYR8g/psm7sEwZxmbQ5e0JUarib7u93bruKT8bH33VRwzYM0nEqq2UoErxzuqIpNS0B1IqMNl6P9Ob8nVVDgAgvjfEXuy32X8qwxE1lJBviIP3Q9ekrXiUuuaFPIx/z1OpC+as+XV7h56HqMnaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344239; c=relaxed/simple; bh=5IAldkgSZm7p/aHbwXA4PqHVmVEu3v0XDt3bNysrur0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YgapHBdXguxTR3RdQZPj90/2PORxOZaNR2FpyhcFtorak7T1lUKro1ApGUwAKdxKLyJZQjIHpcfdzN+e+2XNMC1+721F8Pf/x5Wi4400DmpM/gTG/fJLrgrNFCr0UUDeqlcazFFNiUBD1U/1EAXo0ryLeGzn6pnZslfcFHmw80U= 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=HggmGK9x; arc=none smtp.client-ip=192.198.163.10 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="HggmGK9x" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1753344238; x=1784880238; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5IAldkgSZm7p/aHbwXA4PqHVmVEu3v0XDt3bNysrur0=; b=HggmGK9xWdnwAtWoR6x/3XPTpVhMosIvmMjM+7QMfF5wnhEp/Y5paM5o stcSLE8tdxPgL/H+biGwC5WBuqq5zTNdEAjnLZnN1e6pUwSHfFvCG1T2F VkkVfGA7yrGZH3nvACThrA0TkvXmTN8ns+TAyMk9U35bb9rgozgv4cAOu O+QsA0z00gUt3+em+kUDCq1yyiqJrc274v/ooSFeEbcQZM0L6w4TIeTNK U2R83emHFJ4cSeaIsy3mp6gwg2VJ7khipElLxWYhgBTH6hli9JkNPfPgh B8gZU+7+qVNhGXmb1Ptas2Iq2KUQpgX1ejaNDoWBU3OSYQSPaiak++rtH Q==; X-CSE-ConnectionGUID: KdBFfixpT02tbl2d4spJQg== X-CSE-MsgGUID: 0ZW09WleRCKHgKcxMfKzKg== X-IronPort-AV: E=McAfee;i="6800,10657,11501"; a="66991989" X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="66991989" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:58 -0700 X-CSE-ConnectionGUID: 6P+5RBlRRUykWsX46QtnxQ== X-CSE-MsgGUID: beGOXuFmSPCzLNtVeLiDGw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="160342101" Received: from savramon-mobl1 (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.60]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:52 -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 v9 5/6] x86/sgx: Implement ENCLS[EUPDATESVN] Date: Thu, 24 Jul 2025 11:02:14 +0300 Message-ID: <20250724080313.605676-6-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250724080313.605676-1-elena.reshetova@intel.com> References: <20250724080313.605676-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 --- 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 ccb81a649d1b..206bf41d8325 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 __maybe_unused 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) { return 0; --=20 2.45.2 From nobody Mon Oct 6 06:31:23 2025 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (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 DD437275103; Thu, 24 Jul 2025 08:04:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344243; cv=none; b=c6a8CXgB/n6J2ggOlmdMWoCj2qQjnz3SRlalaxMkeyhw6HpTCKtpoUy64U/z/6oxdVj0giSHkBW4GBT4g/6+M++0H+eVrDtE8qvP5mD3XKdi8sWTOHMkEJOJSgsJRS9z93RjGA3JE/FFF5shdf3/hXw6ELuc7OPsjSISf7w+WqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753344243; c=relaxed/simple; bh=hJ78VH6KfiP/kPVA/V86syXh1HmFRiQnUtKrsFN/w9s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=lMbnyg2WtlnKNh8sTHBNdAB+/zy7OB9afdqkjTNsUgrMy8FVMc/dBqYPU1tyK0UCTD/E8k6k0xXfEqAHfIx0Ox3C92yT1N69NJIH36bM4rXBYVOvSPtiTkbSMksCwNu6BORFY86ZGAQXbpAcWhxp9sc+5HPFuAMkWjvPAirK1h0= 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=khMPqsSL; arc=none smtp.client-ip=192.198.163.10 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="khMPqsSL" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1753344242; x=1784880242; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hJ78VH6KfiP/kPVA/V86syXh1HmFRiQnUtKrsFN/w9s=; b=khMPqsSLPcCD++WjGBRiMN+QYzUUtwDAFelR/TUHSxJlaQKI9ECw3OGz QnAmohTUtnbqOBpWKi+inv2nVP7DhWXF4NB/Mv9TDUAL7MBxDjebDshMx KYetdPNx3Og16YUXBMbRS7Xduw2+9LwgaL+FLrhNRCCvA0qDs+4hXJ04s 1NNBVNbhGpqVAQgK6VPpgjKi3nFt7XloHhmAH7LdOmHg4sYf3KkkCIjFT Unpnrp/gX307ppSVst4u6HI7LIkfBY5VYa80eJW8QxTN5SY9t4YILTv/s SRwidJj6knrgnPsDx6qIXuXdTqMZos8huHNr6fU44FgqZ88a6m8rXNXC6 Q==; X-CSE-ConnectionGUID: a7bjVq1uRReqThIW9LiJ6A== X-CSE-MsgGUID: E6pCpRoDRXitxITK2nSvVg== X-IronPort-AV: E=McAfee;i="6800,10657,11501"; a="66992002" X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="66992002" Received: from fmviesa009.fm.intel.com ([10.60.135.149]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:04:02 -0700 X-CSE-ConnectionGUID: gQyNVfTXRTSLHfVeYXAguQ== X-CSE-MsgGUID: t20ShfOwRKCo6p1uKacpxA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.16,336,1744095600"; d="scan'208";a="160342242" Received: from savramon-mobl1 (HELO eresheto-mobl3.ger.corp.intel.com) ([10.245.244.60]) by fmviesa009-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2025 01:03:57 -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 v9 6/6] x86/sgx: Enable automatic SVN updates for SGX enclaves Date: Thu, 24 Jul 2025 11:02:15 +0300 Message-ID: <20250724080313.605676-7-elena.reshetova@intel.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250724080313.605676-1-elena.reshetova@intel.com> References: <20250724080313.605676-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 --- arch/x86/kernel/cpu/sgx/main.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 206bf41d8325..90b7416969e4 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 __maybe_unused 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 __maybe_unused 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,12 +986,17 @@ static int __maybe_unused sgx_update_svn(void) =20 int sgx_inc_usage_count(void) { + guard(mutex)(&sgx_svn_lock); + + if (sgx_usage_count++ =3D=3D 0) + return sgx_update_svn(); + return 0; } =20 void sgx_dec_usage_count(void) { - return; + sgx_usage_count--; } =20 static int __init sgx_init(void) --=20 2.45.2