Skip to contentSkip to conversation

Add Secure TSC support for SNP guests

PATCH Applied Nikunj A Dadhania 5 participants 60 messages Jan 6, 2025 – Jan 28, 2025

Subsystems (5)
x86 KVM KVM x86 x86 MM +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v16 cover Add Secure TSC support for SNP guests

This patchset is also available at:

https://github.com/AMDESE/linux-kvm/tree/sectsc-guest-latest

and is based on tip/master

Overview
--------

Secure TSC allows guests to securely use RDTSC/RDTSCP instructions as the parameters being used cannot be changed by hypervisor once the guest is launched. More details in the AMD64 APM Vol 2, Section "Secure TSC".

In order to enable secure TSC, SEV-SNP guests need to send a TSC_INFO guest message before the APs are booted. Details from the TSC_INFO response will then be used to program the VMSA before the APs are brought up. See "SEV Secure Nested Paging Firmware ABI Specification" document (currently at
https://www.amd.com/system/files/TechDocs/56860.pdf) section "TSC Info"

SEV-guest driver has the implementation for guest and AMD Security Processor communication. As the TSC_INFO needs to be initialized during early boot before APs are started, move the guest messaging code from sev-guest driver to sev/core.c and provide well defined APIs to the sev-guest driver.

Patches:
01-02: Prepatch
03-04: Patches moving SNP guest messaging code from SEV guest driver to
SEV common code
05-10: SecureTSC enablement patches
11-12: Generic TSC improvements
15: SecureTSC enablement.

Testing SecureTSC
-----------------

SecureTSC hypervisor patches based on top of SEV-SNP Guest MEMFD series:
https://github.com/AMDESE/linux-kvm/tree/sectsc-host-latest

QEMU changes:
https://github.com/nikunjad/qemu/tree/snp-securetsc-latest

QEMU commandline SEV-SNP with SecureTSC:

  qemu-system-x86_64 -cpu EPYC-Milan-v2 -smp 4 \
    -object memory-backend-memfd,id=ram1,size=1G,share=true,prealloc=false,reserve=false \
    -object sev-snp-guest,id=sev0,cbitpos=51,reduced-phys-bits=1,secure-tsc=on \
    -machine q35,confidential-guest-support=sev0,memory-backend=ram1 \
    ...

Changelog:
----------

v16:
* Rebased on latest tip/master
* prepatch: Remove is_vmpck_empty() and use memchr_inv() instead (Boris)
* prepatch: Uses GFP_KERNEL for snp_init_crypto() (Boris)
* Use memset during cleanup and fix memory leak (Boris/Francesco)
* Use tsp_resp with extra allocation and get rid of buf variable (Francesco)
* Drop RoBs and TBs, comment/commit message update
* Add switch case for #VC Secure TSC MSRs handling (Boris)
* Move CC_ATTR_GUEST_SNP_SECURE_TSC before host defines to group guest defines(Tom)
* Cache the TSC frequency in khz to avoid reading MSR and multiply every time (Tom)
* Drop the patch preventing FW_BUG for not running at P0 frequency (Boris)
* Limit clock rating upgrade to virtualized environments as part of CONFIG_PARAVIRT (Boris)
* Squash patch 9 and 12 to logically merge changes that switches to TSC from kvm-clock (Boris)

v15: https://lore.kernel.org/kvm/[email protected]/
* Rebased on latest tip/master
* Update commits/comments (Boris)
* Add snp_msg_free() to free allocated buffers (Boris)
* Dynamically allocate buffers for sending TSC_INFO (Boris)
* Fix the build issue at patch#1 (Boris)
* Carve out tsc handling in __vc_handle_msr_tsc() (Boris)

Nikunj A Dadhania (13):
virt: sev-guest: Remove is_vmpck_empty() helper
virt: sev-guest: Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL x86/sev: Carve out and export SNP guest messaging init routines x86/sev: Relocate SNP guest messaging routines to common code x86/sev: Add Secure TSC support for SNP guests x86/sev: Change TSC MSR behavior for Secure TSC enabled guests x86/sev: Prevent GUEST_TSC_FREQ MSR interception for Secure TSC
enabled guests
x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled
guests
x86/sev: Mark Secure TSC as reliable clocksource x86/tsc: Switch Secure TSC guests away from kvm-clock x86/tsc: Upgrade TSC clocksource rating for guests x86/tsc: Switch to native sched clock
x86/sev: Allow Secure TSC feature for SNP guests

 arch/x86/include/asm/msr-index.h        |   1 +
 arch/x86/include/asm/sev-common.h       |   1 +
 arch/x86/include/asm/sev.h              |  48 +-
 arch/x86/include/asm/svm.h              |   6 +-
 include/linux/cc_platform.h             |   8 +
 arch/x86/boot/compressed/sev.c          |   3 +-
 arch/x86/coco/core.c                    |   4 +
 arch/x86/coco/sev/core.c                | 652 +++++++++++++++++++++++-
 arch/x86/coco/sev/shared.c              |  10 +
 arch/x86/kernel/kvmclock.c              |  11 +
 arch/x86/kernel/tsc.c                   |  43 ++
 arch/x86/mm/mem_encrypt.c               |   2 +
 arch/x86/mm/mem_encrypt_amd.c           |   4 +
 drivers/virt/coco/sev-guest/sev-guest.c | 485 +-----------------
 arch/x86/Kconfig                        |   1 +
 drivers/virt/coco/sev-guest/Kconfig     |   1 -
 16 files changed, 791 insertions(+), 489 deletions(-)

base-commit: af2c8596bd2e455ae350ba1585bc938ee85aa38d

Signature · 1 line
2.34.1
#1 lore raw

PATCH v16 1/13 virt: sev-guest: Remove is_vmpck_empty() helper

Remove the is_vmpck_empty() helper function, which uses a local array allocation to check if the VMPCK is empty. Replace it with memchr_inv() to directly determine if the VMPCK is empty without additional memory allocation.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +2 -12
index b699771be029..62328d0b2cb6 100644
@@ -63,16 +63,6 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.
 /* Mutex to serialize the shared buffer access and command handling. */
 static DEFINE_MUTEX(snp_cmd_mutex);
 
-static bool is_vmpck_empty(struct snp_msg_desc *mdesc)
-{
-	char zero_key[VMPCK_KEY_LEN] = {0};
-
-	if (mdesc->vmpck)
-		return !memcmp(mdesc->vmpck, zero_key, VMPCK_KEY_LEN);
-
-	return true;
-}
-
 /*
  * If an error is received from the host or AMD Secure Processor (ASP) there
  * are two options. Either retry the exact same encrypted request or discontinue
@@ -335,7 +325,7 @@ static int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_r
 	guard(mutex)(&snp_cmd_mutex);
 
 	/* Check if the VMPCK is not empty */
-	if (is_vmpck_empty(mdesc)) {
+	if (!mdesc->vmpck || !memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
 		pr_err_ratelimited("VMPCK is disabled\n");
 		return -ENOTTY;
 	}
@@ -1024,7 +1014,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	}
 
 	/* Verify that VMPCK is not zero. */
-	if (is_vmpck_empty(mdesc)) {
+	if (!memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
 		dev_err(dev, "Empty VMPCK%d communication key\n", vmpck_id);
 		goto e_unmap;
 	}
Signature · 1 line
2.34.1
Reviewed-by on v16 1/13
#2 lore raw

Re: [PATCH v16 01/13] virt: sev-guest: Remove is_vmpck_empty() helper

Quoted 18 lines · Nikunj A Dadhania

Remove the is_vmpck_empty() helper function, which uses a local array allocation to check if the VMPCK is empty. Replace it with memchr_inv() to directly determine if the VMPCK is empty without additional memory allocation.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +0 -0
index b699771be029..62328d0b2cb6 100644
@@ -63,16 +63,6 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.
 /* Mutex to serialize the shared buffer access and command handling. */
 static DEFINE_MUTEX(snp_cmd_mutex);

drivers/virt/coco/sev-guest/sev-guest.c MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP. · line 74

Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +0 -9
 
-static bool is_vmpck_empty(struct snp_msg_desc *mdesc)
-{
-	char zero_key[VMPCK_KEY_LEN] = {0};
-
-	if (mdesc->vmpck)
-		return !memcmp(mdesc->vmpck, zero_key, VMPCK_KEY_LEN);
-
-	return true;
-}

I still like the helper, but just using memchr_inv() inside it instead, e.g.:

return !mdesc->vmpck || !memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN);

But either way works for me.

Quoted 22 lines
drivers/virt/coco/sev-guest/sev-guest.c +2 -3
-
 /*
  * If an error is received from the host or AMD Secure Processor (ASP) there
  * are two options. Either retry the exact same encrypted request or discontinue
@@ -335,7 +325,7 @@ static int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_r
 	guard(mutex)(&snp_cmd_mutex);
 
 	/* Check if the VMPCK is not empty */
-	if (is_vmpck_empty(mdesc)) {
+	if (!mdesc->vmpck || !memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
 		pr_err_ratelimited("VMPCK is disabled\n");
 		return -ENOTTY;
 	}
@@ -1024,7 +1014,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	}
 
 	/* Verify that VMPCK is not zero. */
-	if (is_vmpck_empty(mdesc)) {
+	if (!memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
 		dev_err(dev, "Empty VMPCK%d communication key\n", vmpck_id);
 		goto e_unmap;
 	}
#3 lore raw

[tip: x86/sev] virt: sev-guest: Remove is_vmpck_empty() helper

In reply to Nikunj A Dadhania
The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     8234177d2027e52126e40472fe5807f4e94b19a3
Gitweb:        https://git.kernel.org/tip/8234177d2027e52126e40472fe5807f4e94b19a3
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:21 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 08:57:19 +01:00

virt: sev-guest: Remove is_vmpck_empty() helper

Remove is_vmpck_empty() which uses a local array allocation to check if the VMPCK is empty and replace it with memchr_inv() to directly determine if the VMPCK is empty without additional memory allocation.

[ bp: Massage commit message. ]

---
 drivers/virt/coco/sev-guest/sev-guest.c | 14 ++------------
 1 file changed, 2 insertions(+), 12 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +2 -12
index b699771..62328d0 100644
@@ -63,16 +63,6 @@ MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.
 /* Mutex to serialize the shared buffer access and command handling. */
 static DEFINE_MUTEX(snp_cmd_mutex);
 
-static bool is_vmpck_empty(struct snp_msg_desc *mdesc)
-{
-	char zero_key[VMPCK_KEY_LEN] = {0};
-
-	if (mdesc->vmpck)
-		return !memcmp(mdesc->vmpck, zero_key, VMPCK_KEY_LEN);
-
-	return true;
-}
-
 /*
  * If an error is received from the host or AMD Secure Processor (ASP) there
  * are two options. Either retry the exact same encrypted request or discontinue
@@ -335,7 +325,7 @@ static int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_r
 	guard(mutex)(&snp_cmd_mutex);
 
 	/* Check if the VMPCK is not empty */
-	if (is_vmpck_empty(mdesc)) {
+	if (!mdesc->vmpck || !memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
 		pr_err_ratelimited("VMPCK is disabled\n");
 		return -ENOTTY;
 	}
@@ -1024,7 +1014,7 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	}
 
 	/* Verify that VMPCK is not zero. */
-	if (is_vmpck_empty(mdesc)) {
+	if (!memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
 		dev_err(dev, "Empty VMPCK%d communication key\n", vmpck_id);
 		goto e_unmap;
 	}
#4 lore raw

PATCH v16 2/13 virt: sev-guest: Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL

In reply to Nikunj A Dadhania

Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL in the sev-guest driver code. GFP_KERNEL_ACCOUNT is typically used for accounting untrusted userspace allocations. After auditing the sev-guest code, the following changes are necessary:

* snp_init_crypto(): Use GFP_KERNEL as this is a trusted device probe path.

Retain GFP_KERNEL_ACCOUNT in the following cases for robustness and specific path requirements:

* alloc_shared_pages(): Although all allocations are limited, retain GFP_KERNEL_ACCOUNT for future robustness.

* get_report() and get_ext_report(): These functions are on the unlocked ioctl path and should continue using GFP_KERNEL_ACCOUNT.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
index 62328d0b2cb6..250ce92d816b 100644
@@ -141,7 +141,7 @@ static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
 {
 	struct aesgcm_ctx *ctx;
 
-	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return NULL;
 
Signature · 1 line
2.34.1
Reviewed-by on v16 2/13
#5 lore raw

Re: [PATCH v16 02/13] virt: sev-guest: Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL

Quoted 11 lines · Nikunj A Dadhania

Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL in the sev-guest driver code. GFP_KERNEL_ACCOUNT is typically used for accounting untrusted userspace allocations. After auditing the sev-guest code, the following changes are necessary:

* snp_init_crypto(): Use GFP_KERNEL as this is a trusted device probe path.

Retain GFP_KERNEL_ACCOUNT in the following cases for robustness and specific path requirements:

Quoted 8 lines

* alloc_shared_pages(): Although all allocations are limited, retain GFP_KERNEL_ACCOUNT for future robustness.

* get_report() and get_ext_report(): These functions are on the unlocked ioctl path and should continue using GFP_KERNEL_ACCOUNT.

Quoted 17 lines
---
 drivers/virt/coco/sev-guest/sev-guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
index 62328d0b2cb6..250ce92d816b 100644
@@ -141,7 +141,7 @@ static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
 {
 	struct aesgcm_ctx *ctx;
 
-	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return NULL;
 
#6 lore raw

[tip: x86/sev] virt: sev-guest: Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL

In reply to Nikunj A Dadhania
The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     864884a0c29cc610a859b5210158112fd8675fe1
Gitweb:        https://git.kernel.org/tip/864884a0c29cc610a859b5210158112fd8675fe1
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:22 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 10:26:20 +01:00

virt: sev-guest: Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL

Replace GFP_KERNEL_ACCOUNT with GFP_KERNEL in the sev-guest driver code. GFP_KERNEL_ACCOUNT is typically used for accounting untrusted userspace allocations. After auditing the sev-guest code, the following changes are necessary:

* snp_init_crypto(): Use GFP_KERNEL as this is a trusted device probe path.

Retain GFP_KERNEL_ACCOUNT in the following cases for robustness and specific path requirements:

* alloc_shared_pages(): Although all allocations are limited, retain GFP_KERNEL_ACCOUNT for future robustness.

* get_report() and get_ext_report(): These functions are on the unlocked ioctl path and should continue using GFP_KERNEL_ACCOUNT.

---
 drivers/virt/coco/sev-guest/sev-guest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/virt/coco/sev-guest/sev-guest.c +1 -1
index 62328d0..250ce92 100644
@@ -141,7 +141,7 @@ static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
 {
 	struct aesgcm_ctx *ctx;
 
-	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
+	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
 	if (!ctx)
 		return NULL;
 
#7 lore raw

PATCH v16 3/13 x86/sev: Carve out and export SNP guest messaging init routines

In reply to Nikunj A Dadhania

Currently, the sev-guest driver is the only user of SNP guest messaging. All routines for initializing SNP guest messaging are implemented within the sev-guest driver and are not available during early boot. In preparation for adding Secure TSC guest support, carve out APIs to allocate and initialize the guest messaging descriptor context and make it part of coco/sev/core.c. As there is no user of sev_guest_platform_data anymore, remove the structure.

---
 arch/x86/include/asm/sev.h              |  13 +-
 arch/x86/coco/sev/core.c                | 183 ++++++++++++++++++++++-
 drivers/virt/coco/sev-guest/sev-guest.c | 185 +++---------------------
 arch/x86/Kconfig                        |   1 +
 drivers/virt/coco/sev-guest/Kconfig     |   1 -
 5 files changed, 208 insertions(+), 175 deletions(-)
arch/x86/include/asm/sev.h +9 -4
index 91f08af31078..db08d0ac90be 100644
@@ -14,6 +14,7 @@
 #include <asm/insn.h>
 #include <asm/sev-common.h>
 #include <asm/coco.h>
+#include <asm/set_memory.h>
 
 #define GHCB_PROTOCOL_MIN	1ULL
 #define GHCB_PROTOCOL_MAX	2ULL
@@ -170,10 +171,6 @@ struct snp_guest_msg {
 	u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
 } __packed;
 
-struct sev_guest_platform_data {
-	u64 secrets_gpa;
-};
-
 struct snp_guest_req {
 	void *req_buf;
 	size_t req_sz;
@@ -253,6 +250,7 @@ struct snp_msg_desc {
 
 	u32 *os_area_msg_seqno;
 	u8 *vmpck;
+	int vmpck_id;
 };
 
 /*
@@ -458,6 +456,10 @@ void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot);
 void snp_kexec_finish(void);
 void snp_kexec_begin(void);
 
+int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id);
+struct snp_msg_desc *snp_msg_alloc(void);
+void snp_msg_free(struct snp_msg_desc *mdesc);
+
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
 #define snp_vmpl 0
@@ -498,6 +500,9 @@ static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; }
 static inline void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot) { }
 static inline void snp_kexec_finish(void) { }
 static inline void snp_kexec_begin(void) { }
+static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; }
+static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
+static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/coco/sev/core.c +177 -6

201 lines not on this page · raw message

drivers/virt/coco/sev-guest/sev-guest.c +21 -164

276 lines not on this page · raw message

arch/x86/Kconfig +1 -0
index e4e27d44dc2b..ba2ac635c2fc 100644
@@ -1559,6 +1559,7 @@ config AMD_MEM_ENCRYPT
 	select ARCH_HAS_CC_PLATFORM
 	select X86_MEM_ENCRYPT
 	select UNACCEPTED_MEMORY
+	select CRYPTO_LIB_AESGCM
 	help
 	  Say yes to enable support for the encryption of system memory.
 	  This requires an AMD processor that supports Secure Memory
drivers/virt/coco/sev-guest/Kconfig +0 -1
index 0b772bd921d8..a6405ab6c2c3 100644
@@ -2,7 +2,6 @@ config SEV_GUEST
 	tristate "AMD SEV Guest driver"
 	default m
 	depends on AMD_MEM_ENCRYPT
-	select CRYPTO_LIB_AESGCM
 	select TSM_REPORTS
 	help
 	  SEV-SNP firmware provides the guest a mechanism to communicate with
Signature · 1 line
2.34.1
#8 lore raw

[tip: x86/sev] x86/sev: Carve out and export SNP guest messaging init routines

The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     c5529418d05079384af4dbbb6f6156344c2ffce2
Gitweb:        https://git.kernel.org/tip/c5529418d05079384af4dbbb6f6156344c2ffce2
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:23 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 10:33:20 +01:00

x86/sev: Carve out and export SNP guest messaging init routines

Currently, the sev-guest driver is the only user of SNP guest messaging. All routines for initializing SNP guest messaging are implemented within the sev-guest driver and are not available during early boot.

In preparation for adding Secure TSC guest support, carve out APIs to allocate and initialize the guest messaging descriptor context and make it part of coco/sev/core.c. As there is no user of sev_guest_platform_data anymore, remove the structure.

---
 arch/x86/Kconfig                        |   1 +-
 arch/x86/coco/sev/core.c                | 183 ++++++++++++++++++++++-
 arch/x86/include/asm/sev.h              |  13 +-
 drivers/virt/coco/sev-guest/Kconfig     |   1 +-
 drivers/virt/coco/sev-guest/sev-guest.c | 185 ++---------------------
 5 files changed, 208 insertions(+), 175 deletions(-)
arch/x86/Kconfig +1 -0
index 9d7bd0a..0f7e3ac 100644
@@ -1559,6 +1559,7 @@ config AMD_MEM_ENCRYPT
 	select ARCH_HAS_CC_PLATFORM
 	select X86_MEM_ENCRYPT
 	select UNACCEPTED_MEMORY
+	select CRYPTO_LIB_AESGCM
 	help
 	  Say yes to enable support for the encryption of system memory.
 	  This requires an AMD processor that supports Secure Memory
arch/x86/coco/sev/core.c +177 -6

201 lines not on this page · raw message

arch/x86/include/asm/sev.h +9 -4
index 91f08af..db08d0a 100644
@@ -14,6 +14,7 @@
 #include <asm/insn.h>
 #include <asm/sev-common.h>
 #include <asm/coco.h>
+#include <asm/set_memory.h>
 
 #define GHCB_PROTOCOL_MIN	1ULL
 #define GHCB_PROTOCOL_MAX	2ULL
@@ -170,10 +171,6 @@ struct snp_guest_msg {
 	u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
 } __packed;
 
-struct sev_guest_platform_data {
-	u64 secrets_gpa;
-};
-
 struct snp_guest_req {
 	void *req_buf;
 	size_t req_sz;
@@ -253,6 +250,7 @@ struct snp_msg_desc {
 
 	u32 *os_area_msg_seqno;
 	u8 *vmpck;
+	int vmpck_id;
 };
 
 /*
@@ -458,6 +456,10 @@ void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot);
 void snp_kexec_finish(void);
 void snp_kexec_begin(void);
 
+int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id);
+struct snp_msg_desc *snp_msg_alloc(void);
+void snp_msg_free(struct snp_msg_desc *mdesc);
+
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
 #define snp_vmpl 0
@@ -498,6 +500,9 @@ static inline int prepare_pte_enc(struct pte_enc_desc *d) { return 0; }
 static inline void set_pte_enc_mask(pte_t *kpte, unsigned long pfn, pgprot_t new_prot) { }
 static inline void snp_kexec_finish(void) { }
 static inline void snp_kexec_begin(void) { }
+static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; }
+static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
+static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
drivers/virt/coco/sev-guest/Kconfig +0 -1
index 0b772bd..a6405ab 100644
@@ -2,7 +2,6 @@ config SEV_GUEST
 	tristate "AMD SEV Guest driver"
 	default m
 	depends on AMD_MEM_ENCRYPT
-	select CRYPTO_LIB_AESGCM
 	select TSM_REPORTS
 	help
 	  SEV-SNP firmware provides the guest a mechanism to communicate with
drivers/virt/coco/sev-guest/sev-guest.c +21 -164

276 lines not on this page · raw message

#9 lore raw

PATCH v16 4/13 x86/sev: Relocate SNP guest messaging routines to common code

In reply to Nikunj A Dadhania

At present, the SEV guest driver exclusively handles SNP guest messaging. All routines for sending guest messages are embedded within the guest driver. To support Secure TSC, SEV-SNP guests must communicate with the AMD Security Processor during early boot. However, these guest messaging functions are not accessible during early boot since they are currently part of the guest driver.

Hence, relocate the core SNP guest messaging functions to SEV common code and provide an API for sending SNP guest messages.

No functional change, but just an export symbol added for snp_send_guest_request() and dropped the export symbol on snp_issue_guest_request() and made it static.

---
 arch/x86/include/asm/sev.h              |  14 +-
 arch/x86/coco/sev/core.c                | 294 +++++++++++++++++++++++-
 drivers/virt/coco/sev-guest/sev-guest.c | 292 -----------------------
 3 files changed, 298 insertions(+), 302 deletions(-)
arch/x86/include/asm/sev.h +7 -7
index db08d0ac90be..0937ac7a96db 100644
@@ -125,6 +125,9 @@ struct snp_req_data {
 #define AAD_LEN			48
 #define MSG_HDR_VER		1
 
+#define SNP_REQ_MAX_RETRY_DURATION      (60*HZ)
+#define SNP_REQ_RETRY_DELAY             (2*HZ)
+
 /* See SNP spec SNP_GUEST_REQUEST section for the structure */
 enum msg_type {
 	SNP_MSG_TYPE_INVALID = 0,
@@ -443,8 +446,6 @@ void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __noreturn snp_abort(void);
 void snp_dmi_setup(void);
-int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
-			    struct snp_guest_request_ioctl *rio);
 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input);
 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
 u64 snp_get_unsupported_features(u64 status);
@@ -459,6 +460,8 @@ void snp_kexec_begin(void);
 int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id);
 struct snp_msg_desc *snp_msg_alloc(void);
 void snp_msg_free(struct snp_msg_desc *mdesc);
+int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
+			   struct snp_guest_request_ioctl *rio);
 
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
@@ -482,11 +485,6 @@ static inline void snp_set_wakeup_secondary_cpu(void) { }
 static inline bool snp_init(struct boot_params *bp) { return false; }
 static inline void snp_abort(void) { }
 static inline void snp_dmi_setup(void) { }
-static inline int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
-					  struct snp_guest_request_ioctl *rio)
-{
-	return -ENOTTY;
-}
 static inline int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input)
 {
 	return -ENOTTY;
@@ -503,6 +501,8 @@ static inline void snp_kexec_begin(void) { }
 static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; }
 static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
 static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
+static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
+					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/coco/sev/core.c +291 -3

309 lines not on this page · raw message

drivers/virt/coco/sev-guest/sev-guest.c +0 -292

310 lines not on this page · raw message

Signature · 1 line
2.34.1
#10 lore raw

[tip: x86/sev] x86/sev: Relocate SNP guest messaging routines to common code

The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     1e0b23b5d2d18b2bd2c66d8214072d700a8c350d
Gitweb:        https://git.kernel.org/tip/1e0b23b5d2d18b2bd2c66d8214072d700a8c350d
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:24 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 11:16:46 +01:00

x86/sev: Relocate SNP guest messaging routines to common code

At present, the SEV guest driver exclusively handles SNP guest messaging. All routines for sending guest messages are embedded within it.

To support Secure TSC, SEV-SNP guests must communicate with the AMD Security Processor during early boot. However, these guest messaging functions are not accessible during early boot since they are currently part of the guest driver.

Hence, relocate the core SNP guest messaging functions to SEV common code and provide an API for sending SNP guest messages.

No functional change, but just an export symbol added for snp_send_guest_request() and dropped the export symbol on snp_issue_guest_request() and made it static.

---
 arch/x86/coco/sev/core.c                | 294 ++++++++++++++++++++++-
 arch/x86/include/asm/sev.h              |  14 +-
 drivers/virt/coco/sev-guest/sev-guest.c | 292 +-----------------------
 3 files changed, 298 insertions(+), 302 deletions(-)
arch/x86/coco/sev/core.c +291 -3
index 30ce563..ad3a288 100644
@@ -2509,8 +2509,8 @@ int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call,
 }
 EXPORT_SYMBOL_GPL(snp_issue_svsm_attest_req);
 
-int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
-			    struct snp_guest_request_ioctl *rio)
+static int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
+				   struct snp_guest_request_ioctl *rio)
 {
 	struct ghcb_state state;
 	struct es_em_ctxt ctxt;
@@ -2572,7 +2572,6 @@ e_restore_irq:
 
 	return ret;
 }
-EXPORT_SYMBOL_GPL(snp_issue_guest_request);
 
 static struct platform_device sev_guest_device = {
 	.name		= "sev-guest",
@@ -2838,3 +2837,292 @@ void snp_msg_free(struct snp_msg_desc *mdesc)
 	kfree(mdesc);
 }
 EXPORT_SYMBOL_GPL(snp_msg_free);
+
+/* Mutex to serialize the shared buffer access and command handling. */
+static DEFINE_MUTEX(snp_cmd_mutex);
+
+/*
+ * If an error is received from the host or AMD Secure Processor (ASP) there
+ * are two options. Either retry the exact same encrypted request or discontinue
+ * using the VMPCK.
+ *
+ * This is because in the current encryption scheme GHCB v2 uses AES-GCM to
+ * encrypt the requests. The IV for this scheme is the sequence number. GCM
+ * cannot tolerate IV reuse.
+ *
+ * The ASP FW v1.51 only increments the sequence numbers on a successful
+ * guest<->ASP back and forth and only accepts messages at its exact sequence
+ * number.
+ *
+ * So if the sequence number were to be reused the encryption scheme is
+ * vulnerable. If the sequence number were incremented for a fresh IV the ASP
+ * will reject the request.
+ */
+static void snp_disable_vmpck(struct snp_msg_desc *mdesc)
+{
+	pr_alert("Disabling VMPCK%d communication key to prevent IV reuse.\n",
+		  mdesc->vmpck_id);
+	memzero_explicit(mdesc->vmpck, VMPCK_KEY_LEN);
+	mdesc->vmpck = NULL;
+}
+
+static inline u64 __snp_get_msg_seqno(struct snp_msg_desc *mdesc)
+{
+	u64 count;
+
+	lockdep_assert_held(&snp_cmd_mutex);
+
+	/* Read the current message sequence counter from secrets pages */
+	count = *mdesc->os_area_msg_seqno;
+
+	return count + 1;
+}
+
+/* Return a non-zero on success */
+static u64 snp_get_msg_seqno(struct snp_msg_desc *mdesc)
+{
+	u64 count = __snp_get_msg_seqno(mdesc);
+
+	/*
+	 * The message sequence counter for the SNP guest request is a  64-bit
+	 * value but the version 2 of GHCB specification defines a 32-bit storage
+	 * for it. If the counter exceeds the 32-bit value then return zero.
+	 * The caller should check the return value, but if the caller happens to
+	 * not check the value and use it, then the firmware treats zero as an
+	 * invalid number and will fail the  message request.
+	 */
+	if (count >= UINT_MAX) {
+		pr_err("request message sequence counter overflow\n");
+		return 0;
+	}
+
+	return count;
+}
+
+static void snp_inc_msg_seqno(struct snp_msg_desc *mdesc)
+{
+	/*
+	 * The counter is also incremented by the PSP, so increment it by 2
+	 * and save in secrets page.
+	 */
+	*mdesc->os_area_msg_seqno += 2;
+}
+
+static int verify_and_dec_payload(struct snp_msg_desc *mdesc, struct snp_guest_req *req)
+{
+	struct snp_guest_msg *resp_msg = &mdesc->secret_response;
+	struct snp_guest_msg *req_msg = &mdesc->secret_request;
+	struct snp_guest_msg_hdr *req_msg_hdr = &req_msg->hdr;
+	struct snp_guest_msg_hdr *resp_msg_hdr = &resp_msg->hdr;
+	struct aesgcm_ctx *ctx = mdesc->ctx;
+	u8 iv[GCM_AES_IV_SIZE] = {};
+
+	pr_debug("response [seqno %lld type %d version %d sz %d]\n",
+		 resp_msg_hdr->msg_seqno, resp_msg_hdr->msg_type, resp_msg_hdr->msg_version,
+		 resp_msg_hdr->msg_sz);
+
+	/* Copy response from shared memory to encrypted memory. */
+	memcpy(resp_msg, mdesc->response, sizeof(*resp_msg));
+
+	/* Verify that the sequence counter is incremented by 1 */
+	if (unlikely(resp_msg_hdr->msg_seqno != (req_msg_hdr->msg_seqno + 1)))
+		return -EBADMSG;
+
+	/* Verify response message type and version number. */
+	if (resp_msg_hdr->msg_type != (req_msg_hdr->msg_type + 1) ||
+	    resp_msg_hdr->msg_version != req_msg_hdr->msg_version)
+		return -EBADMSG;
+
+	/*
+	 * If the message size is greater than our buffer length then return
+	 * an error.
+	 */
+	if (unlikely((resp_msg_hdr->msg_sz + ctx->authsize) > req->resp_sz))
+		return -EBADMSG;
+
+	/* Decrypt the payload */
+	memcpy(iv, &resp_msg_hdr->msg_seqno, min(sizeof(iv), sizeof(resp_msg_hdr->msg_seqno)));
+	if (!aesgcm_decrypt(ctx, req->resp_buf, resp_msg->payload, resp_msg_hdr->msg_sz,
+			    &resp_msg_hdr->algo, AAD_LEN, iv, resp_msg_hdr->authtag))
+		return -EBADMSG;
+
+	return 0;
+}
+
+static int enc_payload(struct snp_msg_desc *mdesc, u64 seqno, struct snp_guest_req *req)
+{
+	struct snp_guest_msg *msg = &mdesc->secret_request;
+	struct snp_guest_msg_hdr *hdr = &msg->hdr;
+	struct aesgcm_ctx *ctx = mdesc->ctx;
+	u8 iv[GCM_AES_IV_SIZE] = {};
+
+	memset(msg, 0, sizeof(*msg));
+
+	hdr->algo = SNP_AEAD_AES_256_GCM;
+	hdr->hdr_version = MSG_HDR_VER;
+	hdr->hdr_sz = sizeof(*hdr);
+	hdr->msg_type = req->msg_type;
+	hdr->msg_version = req->msg_version;
+	hdr->msg_seqno = seqno;
+	hdr->msg_vmpck = req->vmpck_id;
+	hdr->msg_sz = req->req_sz;
+
+	/* Verify the sequence number is non-zero */
+	if (!hdr->msg_seqno)
+		return -ENOSR;
+
+	pr_debug("request [seqno %lld type %d version %d sz %d]\n",
+		 hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz);
+
+	if (WARN_ON((req->req_sz + ctx->authsize) > sizeof(msg->payload)))
+		return -EBADMSG;
+
+	memcpy(iv, &hdr->msg_seqno, min(sizeof(iv), sizeof(hdr->msg_seqno)));
+	aesgcm_encrypt(ctx, msg->payload, req->req_buf, req->req_sz, &hdr->algo,
+		       AAD_LEN, iv, hdr->authtag);
+
+	return 0;
+}
+
+static int __handle_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
+				  struct snp_guest_request_ioctl *rio)
+{
+	unsigned long req_start = jiffies;
+	unsigned int override_npages = 0;
+	u64 override_err = 0;
+	int rc;
+
+retry_request:
+	/*
+	 * Call firmware to process the request. In this function the encrypted
+	 * message enters shared memory with the host. So after this call the
+	 * sequence number must be incremented or the VMPCK must be deleted to
+	 * prevent reuse of the IV.
+	 */
+	rc = snp_issue_guest_request(req, &mdesc->input, rio);
+	switch (rc) {
+	case -ENOSPC:
+		/*
+		 * If the extended guest request fails due to having too
+		 * small of a certificate data buffer, retry the same
+		 * guest request without the extended data request in
+		 * order to increment the sequence number and thus avoid
+		 * IV reuse.
+		 */
+		override_npages = mdesc->input.data_npages;
+		req->exit_code	= SVM_VMGEXIT_GUEST_REQUEST;
+
+		/*
+		 * Override the error to inform callers the given extended
+		 * request buffer size was too small and give the caller the
+		 * required buffer size.
+		 */
+		override_err = SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN);
+
+		/*
+		 * If this call to the firmware succeeds, the sequence number can
+		 * be incremented allowing for continued use of the VMPCK. If
+		 * there is an error reflected in the return value, this value
+		 * is checked further down and the result will be the deletion
+		 * of the VMPCK and the error code being propagated back to the
+		 * user as an ioctl() return code.
+		 */
+		goto retry_request;
+
+	/*
+	 * The host may return SNP_GUEST_VMM_ERR_BUSY if the request has been
+	 * throttled. Retry in the driver to avoid returning and reusing the
+	 * message sequence number on a different message.
+	 */
+	case -EAGAIN:
+		if (jiffies - req_start > SNP_REQ_MAX_RETRY_DURATION) {
+			rc = -ETIMEDOUT;
+			break;
+		}
+		schedule_timeout_killable(SNP_REQ_RETRY_DELAY);
+		goto retry_request;
+	}
+
+	/*
+	 * Increment the message sequence number. There is no harm in doing
+	 * this now because decryption uses the value stored in the response
+	 * structure and any failure will wipe the VMPCK, preventing further
+	 * use anyway.
+	 */
+	snp_inc_msg_seqno(mdesc);
+
+	if (override_err) {
+		rio->exitinfo2 = override_err;
+
+		/*
+		 * If an extended guest request was issued and the supplied certificate
+		 * buffer was not large enough, a standard guest request was issued to
+		 * prevent IV reuse. If the standard request was successful, return -EIO
+		 * back to the caller as would have originally been returned.
+		 */
+		if (!rc && override_err == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN))
+			rc = -EIO;
+	}
+
+	if (override_npages)
+		mdesc->input.data_npages = override_npages;
+
+	return rc;
+}
+
+int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
+			   struct snp_guest_request_ioctl *rio)
+{
+	u64 seqno;
+	int rc;
+
+	guard(mutex)(&snp_cmd_mutex);
+
+	/* Check if the VMPCK is not empty */
+	if (!mdesc->vmpck || !memchr_inv(mdesc->vmpck, 0, VMPCK_KEY_LEN)) {
+		pr_err_ratelimited("VMPCK is disabled\n");
+		return -ENOTTY;
+	}
+
+	/* Get message sequence and verify that its a non-zero */
+	seqno = snp_get_msg_seqno(mdesc);
+	if (!seqno)
+		return -EIO;
+
+	/* Clear shared memory's response for the host to populate. */
+	memset(mdesc->response, 0, sizeof(struct snp_guest_msg));
+
+	/* Encrypt the userspace provided payload in mdesc->secret_request. */
+	rc = enc_payload(mdesc, seqno, req);
+	if (rc)
+		return rc;
+
+	/*
+	 * Write the fully encrypted request to the shared unencrypted
+	 * request page.
+	 */
+	memcpy(mdesc->request, &mdesc->secret_request, sizeof(mdesc->secret_request));
+
+	rc = __handle_guest_request(mdesc, req, rio);
+	if (rc) {
+		if (rc == -EIO &&
+		    rio->exitinfo2 == SNP_GUEST_VMM_ERR(SNP_GUEST_VMM_ERR_INVALID_LEN))
+			return rc;
+
+		pr_alert("Detected error from ASP request. rc: %d, exitinfo2: 0x%llx\n",
+			 rc, rio->exitinfo2);
+
+		snp_disable_vmpck(mdesc);
+		return rc;
+	}
+
+	rc = verify_and_dec_payload(mdesc, req);
+	if (rc) {
+		pr_alert("Detected unexpected decode failure from ASP. rc: %d\n", rc);
+		snp_disable_vmpck(mdesc);
+		return rc;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(snp_send_guest_request);
arch/x86/include/asm/sev.h +7 -7
index db08d0a..0937ac7 100644
@@ -125,6 +125,9 @@ struct snp_req_data {
 #define AAD_LEN			48
 #define MSG_HDR_VER		1
 
+#define SNP_REQ_MAX_RETRY_DURATION      (60*HZ)
+#define SNP_REQ_RETRY_DELAY             (2*HZ)
+
 /* See SNP spec SNP_GUEST_REQUEST section for the structure */
 enum msg_type {
 	SNP_MSG_TYPE_INVALID = 0,
@@ -443,8 +446,6 @@ void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __noreturn snp_abort(void);
 void snp_dmi_setup(void);
-int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
-			    struct snp_guest_request_ioctl *rio);
 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input);
 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
 u64 snp_get_unsupported_features(u64 status);
@@ -459,6 +460,8 @@ void snp_kexec_begin(void);
 int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id);
 struct snp_msg_desc *snp_msg_alloc(void);
 void snp_msg_free(struct snp_msg_desc *mdesc);
+int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
+			   struct snp_guest_request_ioctl *rio);
 
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
@@ -482,11 +485,6 @@ static inline void snp_set_wakeup_secondary_cpu(void) { }
 static inline bool snp_init(struct boot_params *bp) { return false; }
 static inline void snp_abort(void) { }
 static inline void snp_dmi_setup(void) { }
-static inline int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
-					  struct snp_guest_request_ioctl *rio)
-{
-	return -ENOTTY;
-}
 static inline int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input)
 {
 	return -ENOTTY;
@@ -503,6 +501,8 @@ static inline void snp_kexec_begin(void) { }
 static inline int snp_msg_init(struct snp_msg_desc *mdesc, int vmpck_id) { return -1; }
 static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
 static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
+static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
+					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
drivers/virt/coco/sev-guest/sev-guest.c +0 -292

310 lines not on this page · raw message

#11 lore raw

PATCH v16 5/13 x86/sev: Add Secure TSC support for SNP guests

In reply to Nikunj A Dadhania

Add support for Secure TSC in SNP-enabled guests. Secure TSC allows guests to securely use RDTSC/RDTSCP instructions, ensuring that the parameters used cannot be altered by the hypervisor once the guest is launched.

Secure TSC-enabled guests need to query TSC information from the AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, with the hypervisor acting merely as a conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with AEAD (AES-256 GCM).

---
 arch/x86/include/asm/sev-common.h |   1 +
 arch/x86/include/asm/sev.h        |  21 ++++++
 arch/x86/include/asm/svm.h        |   6 +-
 include/linux/cc_platform.h       |   8 +++
 arch/x86/coco/core.c              |   4 ++
 arch/x86/coco/sev/core.c          | 107 ++++++++++++++++++++++++++++++
 arch/x86/mm/mem_encrypt.c         |   2 +
 7 files changed, 147 insertions(+), 2 deletions(-)
arch/x86/include/asm/sev-common.h +1 -0
index 50f5666938c0..6ef92432a5ce 100644
@@ -206,6 +206,7 @@ struct snp_psc_desc {
 #define GHCB_TERM_NO_SVSM		7	/* SVSM is not advertised in the secrets page */
 #define GHCB_TERM_SVSM_VMPL0		8	/* SVSM is present but has set VMPL to 0 */
 #define GHCB_TERM_SVSM_CAA		9	/* SVSM is present but CAA is not page aligned */
+#define GHCB_TERM_SECURE_TSC		10	/* Secure TSC initialization failed */
 
 #define GHCB_RESP_CODE(v)		((v) & GHCB_MSR_INFO_MASK)
 
arch/x86/include/asm/sev.h +21 -0
index 0937ac7a96db..bdcdaac4df1c 100644
@@ -146,6 +146,9 @@ enum msg_type {
 	SNP_MSG_VMRK_REQ,
 	SNP_MSG_VMRK_RSP,
 
+	SNP_MSG_TSC_INFO_REQ = 17,
+	SNP_MSG_TSC_INFO_RSP,
+
 	SNP_MSG_TYPE_MAX
 };
 
@@ -174,6 +177,21 @@ struct snp_guest_msg {
 	u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
 } __packed;
 
+#define SNP_TSC_INFO_REQ_SZ	128
+
+struct snp_tsc_info_req {
+	u8 rsvd[SNP_TSC_INFO_REQ_SZ];
+} __packed;
+
+struct snp_tsc_info_resp {
+	u32 status;
+	u32 rsvd1;
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u32 tsc_factor;
+	u8 rsvd2[100];
+} __packed;
+
 struct snp_guest_req {
 	void *req_buf;
 	size_t req_sz;
@@ -463,6 +481,8 @@ void snp_msg_free(struct snp_msg_desc *mdesc);
 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 			   struct snp_guest_request_ioctl *rio);
 
+void __init snp_secure_tsc_prepare(void);
+
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
 #define snp_vmpl 0
@@ -503,6 +523,7 @@ static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
 static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
 static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
+static inline void __init snp_secure_tsc_prepare(void) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/include/asm/svm.h +4 -2
index 2b59b9951c90..92e18798f197 100644
@@ -417,7 +417,9 @@ struct sev_es_save_area {
 	u8 reserved_0x298[80];
 	u32 pkru;
 	u32 tsc_aux;
-	u8 reserved_0x2f0[24];
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u8 reserved_0x300[8];
 	u64 rcx;
 	u64 rdx;
 	u64 rbx;
@@ -564,7 +566,7 @@ static inline void __unused_size_checks(void)
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x1c0);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x248);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x298);
-	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x2f0);
+	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x300);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0);
include/linux/cc_platform.h +8 -0
index caa4b4430634..0bf7d33a1048 100644
@@ -81,6 +81,14 @@ enum cc_attr {
 	 */
 	CC_ATTR_GUEST_SEV_SNP,
 
+	/**
+	 * @CC_ATTR_GUEST_SNP_SECURE_TSC: SNP Secure TSC is active.
+	 *
+	 * The platform/OS is running as a guest/virtual machine and actively
+	 * using AMD SEV-SNP Secure TSC feature.
+	 */
+	CC_ATTR_GUEST_SNP_SECURE_TSC,
+
 	/**
 	 * @CC_ATTR_HOST_SEV_SNP: AMD SNP enabled on the host.
 	 *
arch/x86/coco/core.c +4 -0
index 0f81f70aca82..715c2c09582f 100644
@@ -97,6 +97,10 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 	case CC_ATTR_GUEST_SEV_SNP:
 		return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
 
+	case CC_ATTR_GUEST_SNP_SECURE_TSC:
+		return (sev_status & MSR_AMD64_SEV_SNP_ENABLED) &&
+			(sev_status & MSR_AMD64_SNP_SECURE_TSC);
+
 	case CC_ATTR_HOST_SEV_SNP:
 		return cc_flags.host_sev_snp;
 
arch/x86/coco/sev/core.c +107 -0

122 lines not on this page · raw message

arch/x86/mm/mem_encrypt.c +2 -0
index 0a120d85d7bb..95bae74fdab2 100644
@@ -94,6 +94,8 @@ void __init mem_encrypt_init(void)
 	/* Call into SWIOTLB to update the SWIOTLB DMA buffers */
 	swiotlb_update_mem_attributes();
 
+	snp_secure_tsc_prepare();
+
 	print_mem_encrypt_feature_info();
 }
 
Signature · 1 line
2.34.1
maintainer
#12 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

arch/x86/coco/core.c static bool noinstr amd_cc_platform_has(enum cc_attr attr) · line 101

Quoted 10 lines · Nikunj A Dadhania
arch/x86/coco/core.c +2 -0
index 0f81f70aca82..715c2c09582f 100644
@@ -97,6 +97,10 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 	case CC_ATTR_GUEST_SEV_SNP:
 		return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
 
+	case CC_ATTR_GUEST_SNP_SECURE_TSC:
+		return (sev_status & MSR_AMD64_SEV_SNP_ENABLED) &&

This is new here?

Quoted 2 lines
arch/x86/coco/core.c +2 -0
+			(sev_status & MSR_AMD64_SNP_SECURE_TSC);
+
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#13 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 9 lines · Borislav Petkov
Quoted 8 lines · Nikunj A Dadhania
arch/x86/coco/core.c +0 -0
index 0f81f70aca82..715c2c09582f 100644
@@ -97,6 +97,10 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 	case CC_ATTR_GUEST_SEV_SNP:
 		return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
 
Quoted 4 lines

arch/x86/coco/core.c static bool noinstr amd_cc_platform_has(enum cc_attr attr) · line 101

Quoted 2 lines
arch/x86/coco/core.c +2 -0
+	case CC_ATTR_GUEST_SNP_SECURE_TSC:
+		return (sev_status & MSR_AMD64_SEV_SNP_ENABLED) &&

This is new here?

Yes, this was suggested by Tom here [1]

Quoted 3 lines

arch/x86/coco/core.c static bool noinstr amd_cc_platform_has(enum cc_attr attr) · line 103

Quoted 2 lines
arch/x86/coco/core.c +2 -0
+			(sev_status & MSR_AMD64_SNP_SECURE_TSC);
+

Regards
Nikunj

1. https://lore.kernel.org/all/[email protected]

maintainer
#14 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 6 lines · Nikunj A. Dadhania
Quoted 4 lines

arch/x86/coco/core.c static bool noinstr amd_cc_platform_has(enum cc_attr attr) · line 101

Quoted 2 lines
arch/x86/coco/core.c +2 -0
+	case CC_ATTR_GUEST_SNP_SECURE_TSC:
+		return (sev_status & MSR_AMD64_SEV_SNP_ENABLED) &&

This is new here?

Yes, this was suggested by Tom here [1]

Either of you care to explain why this is needed?

Quoted 1 line
Quoted 1 line

arch/x86/coco/core.c static bool noinstr amd_cc_platform_has(enum cc_attr attr) · line 102

Quoted 1 line
arch/x86/coco/core.c +1 -0
+			(sev_status & MSR_AMD64_SNP_SECURE_TSC);

I would strongly assume that whatever sets MSR_AMD64_SNP_SECURE_TSC will have checked/set MSR_AMD64_SEV_SNP_ENABLED already.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#15 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 6 lines · Borislav Petkov
Quoted 5 lines · Nikunj A. Dadhania
Quoted 4 lines

arch/x86/coco/core.c static bool noinstr amd_cc_platform_has(enum cc_attr attr) · line 101

Quoted 2 lines
arch/x86/coco/core.c +2 -0
+	case CC_ATTR_GUEST_SNP_SECURE_TSC:
+		return (sev_status & MSR_AMD64_SEV_SNP_ENABLED) &&

This is new here?

Quoted 8 lines
Quoted 1 line

Yes, this was suggested by Tom here [1]

Either of you care to explain why this is needed?

Quoted 1 line
Quoted 1 line

arch/x86/coco/core.c static bool noinstr amd_cc_platform_has(enum cc_attr attr) · line 102

Quoted 1 line
arch/x86/coco/core.c +1 -0
+			(sev_status & MSR_AMD64_SNP_SECURE_TSC);

I would strongly assume that whatever sets MSR_AMD64_SNP_SECURE_TSC will have checked/set MSR_AMD64_SEV_SNP_ENABLED already.

Yes, but from a readability point of view this makes it perfectly clear that Secure TSC is only for SNP guests.

It's not on a fast path, so I don't think it hurts anything by having the extra check. But if you prefer the previous check, I'm ok with that.

Thanks,
Tom

Quoted 1 line
maintainer
#16 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 2 lines · Tom Lendacky

Yes, but from a readability point of view this makes it perfectly clear that Secure TSC is only for SNP guests.

That would mean that we need to check SNP with every SNP-specific feature which would be just silly. And all SNP feature bits have "SNP" in the same so...

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#17 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 8 lines · Borislav Petkov
Quoted 2 lines · Tom Lendacky

Yes, but from a readability point of view this makes it perfectly clear that Secure TSC is only for SNP guests.

That would mean that we need to check SNP with every SNP-specific feature which would be just silly. And all SNP feature bits have "SNP" in the same so...

Right, here is the updated diff:

arch/x86/coco/core.c +1 -2
index 715c2c09582f..d6647953590b 100644
@@ -98,8 +98,7 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 		return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
 
 	case CC_ATTR_GUEST_SNP_SECURE_TSC:
-		return (sev_status & MSR_AMD64_SEV_SNP_ENABLED) &&
-			(sev_status & MSR_AMD64_SNP_SECURE_TSC);
+		return sev_status & MSR_AMD64_SNP_SECURE_TSC;
 
 	case CC_ATTR_HOST_SEV_SNP:
 		return cc_flags.host_sev_snp;
arch/x86/coco/sev/core.c +2 -1
index 00a0ac3baab7..763cfeb65b2f 100644
@@ -3218,7 +3218,8 @@ static int __init snp_get_tsc_info(void)
 
 void __init snp_secure_tsc_prepare(void)
 {
-	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
+	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) ||
+	    !cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
 		return;
 
 	if (snp_get_tsc_info()) {
maintainer
#18 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

arch/x86/coco/sev/core.c static int __init snp_get_tsc_info(void) · line 3222

Quoted 11 lines · Nikunj A. Dadhania
arch/x86/coco/sev/core.c +2 -1
index 00a0ac3baab7..763cfeb65b2f 100644
@@ -3218,7 +3218,8 @@ static int __init snp_get_tsc_info(void)
 
 void __init snp_secure_tsc_prepare(void)
 {
-	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
+	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) ||
+	    !cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))

So how is moving the CC_ATTR_GUEST_SEV_SNP check here make any sense?

I simply zapped the MSR_AMD64_SEV_SNP_ENABLED check above locally.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#19 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 7 lines · Borislav Petkov
Quoted 6 lines · Nikunj A. Dadhania
arch/x86/coco/sev/core.c +0 -0
index 00a0ac3baab7..763cfeb65b2f 100644
@@ -3218,7 +3218,8 @@ static int __init snp_get_tsc_info(void)
 
Quoted 7 lines

arch/x86/coco/sev/core.c static int __init snp_get_tsc_info(void) · line 3222

Quoted 5 lines
arch/x86/coco/sev/core.c +2 -1
 void __init snp_secure_tsc_prepare(void)
 {
-	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
+	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) ||
+	    !cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))

So how is moving the CC_ATTR_GUEST_SEV_SNP check here make any sense?

In the comment that you gave here[1], I understood that this check has to be pushed to snp_secure_tsc_prepare().

arch/x86/mm/mem_encrypt.c +1 -3
index 996ca27f0b72..95bae74fdab2 100644
@@ -94,9 +94,7 @@ void __init mem_encrypt_init(void)
 	/* Call into SWIOTLB to update the SWIOTLB DMA buffers */
 	swiotlb_update_mem_attributes();
 
-	/* Initialize SNP Secure TSC */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
-		snp_secure_tsc_prepare();
+	snp_secure_tsc_prepare();
 
 	print_mem_encrypt_feature_info();
 }
Quoted 1 line

I simply zapped the MSR_AMD64_SEV_SNP_ENABLED check above locally.

For SEV and SEV-ES this SecureTSC bit should not be set, I think we should be fine without MSR_AMD64_SEV_SNP_ENABLED check.

Regards
Nikunj

1. https://lore.kernel.org/kvm/20241111103434.GAZzHduouKi4LBwbg8@fat_crate.local/

maintainer
#20 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 10 lines · Nikunj A. Dadhania
Quoted 7 lines

Review

Quoted 5 lines

void __init snp_secure_tsc_prepare(void)
{

(quoted diff) +2 -1
-	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
+	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP) ||
+	    !cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))

So how is moving the CC_ATTR_GUEST_SEV_SNP check here make any sense?

In the comment that you gave here[1], I understood that this check has to be pushed to snp_secure_tsc_prepare().

The check should be pushed there but you should not check CC_ATTR_GUEST_SEV_SNP *and* CC_ATTR_GUEST_SNP_SECURE_TSC if all you wanna do is check if you're running a STSC guest. That was the whole point.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Reviewed-by on v16 5/13
#21 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

In reply to Nikunj A Dadhania
Quoted 11 lines · Nikunj A Dadhania

Add support for Secure TSC in SNP-enabled guests. Secure TSC allows guests to securely use RDTSC/RDTSCP instructions, ensuring that the parameters used cannot be altered by the hypervisor once the guest is launched.

Secure TSC-enabled guests need to query TSC information from the AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, with the hypervisor acting merely as a conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with AEAD (AES-256 GCM).

Although if you have to re-spin, it might make sense to use the __free() support to reduce the number of gotos and labels in snp_get_tsc_info(). You could also use kfree_sensitive() to automatically do the memzero_explicit(), too.

Quoted 275 lines
---
 arch/x86/include/asm/sev-common.h |   1 +
 arch/x86/include/asm/sev.h        |  21 ++++++
 arch/x86/include/asm/svm.h        |   6 +-
 include/linux/cc_platform.h       |   8 +++
 arch/x86/coco/core.c              |   4 ++
 arch/x86/coco/sev/core.c          | 107 ++++++++++++++++++++++++++++++
 arch/x86/mm/mem_encrypt.c         |   2 +
 7 files changed, 147 insertions(+), 2 deletions(-)
arch/x86/include/asm/sev-common.h +1 -0
index 50f5666938c0..6ef92432a5ce 100644
@@ -206,6 +206,7 @@ struct snp_psc_desc {
 #define GHCB_TERM_NO_SVSM		7	/* SVSM is not advertised in the secrets page */
 #define GHCB_TERM_SVSM_VMPL0		8	/* SVSM is present but has set VMPL to 0 */
 #define GHCB_TERM_SVSM_CAA		9	/* SVSM is present but CAA is not page aligned */
+#define GHCB_TERM_SECURE_TSC		10	/* Secure TSC initialization failed */
 
 #define GHCB_RESP_CODE(v)		((v) & GHCB_MSR_INFO_MASK)
 
arch/x86/include/asm/sev.h +21 -0
index 0937ac7a96db..bdcdaac4df1c 100644
@@ -146,6 +146,9 @@ enum msg_type {
 	SNP_MSG_VMRK_REQ,
 	SNP_MSG_VMRK_RSP,
 
+	SNP_MSG_TSC_INFO_REQ = 17,
+	SNP_MSG_TSC_INFO_RSP,
+
 	SNP_MSG_TYPE_MAX
 };
 
@@ -174,6 +177,21 @@ struct snp_guest_msg {
 	u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
 } __packed;
 
+#define SNP_TSC_INFO_REQ_SZ	128
+
+struct snp_tsc_info_req {
+	u8 rsvd[SNP_TSC_INFO_REQ_SZ];
+} __packed;
+
+struct snp_tsc_info_resp {
+	u32 status;
+	u32 rsvd1;
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u32 tsc_factor;
+	u8 rsvd2[100];
+} __packed;
+
 struct snp_guest_req {
 	void *req_buf;
 	size_t req_sz;
@@ -463,6 +481,8 @@ void snp_msg_free(struct snp_msg_desc *mdesc);
 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 			   struct snp_guest_request_ioctl *rio);
 
+void __init snp_secure_tsc_prepare(void);
+
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
 #define snp_vmpl 0
@@ -503,6 +523,7 @@ static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
 static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
 static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
+static inline void __init snp_secure_tsc_prepare(void) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/include/asm/svm.h +4 -2
index 2b59b9951c90..92e18798f197 100644
@@ -417,7 +417,9 @@ struct sev_es_save_area {
 	u8 reserved_0x298[80];
 	u32 pkru;
 	u32 tsc_aux;
-	u8 reserved_0x2f0[24];
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u8 reserved_0x300[8];
 	u64 rcx;
 	u64 rdx;
 	u64 rbx;
@@ -564,7 +566,7 @@ static inline void __unused_size_checks(void)
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x1c0);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x248);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x298);
-	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x2f0);
+	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x300);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0);
include/linux/cc_platform.h +8 -0
index caa4b4430634..0bf7d33a1048 100644
@@ -81,6 +81,14 @@ enum cc_attr {
 	 */
 	CC_ATTR_GUEST_SEV_SNP,
 
+	/**
+	 * @CC_ATTR_GUEST_SNP_SECURE_TSC: SNP Secure TSC is active.
+	 *
+	 * The platform/OS is running as a guest/virtual machine and actively
+	 * using AMD SEV-SNP Secure TSC feature.
+	 */
+	CC_ATTR_GUEST_SNP_SECURE_TSC,
+
 	/**
 	 * @CC_ATTR_HOST_SEV_SNP: AMD SNP enabled on the host.
 	 *
arch/x86/coco/core.c +4 -0
index 0f81f70aca82..715c2c09582f 100644
@@ -97,6 +97,10 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 	case CC_ATTR_GUEST_SEV_SNP:
 		return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
 
+	case CC_ATTR_GUEST_SNP_SECURE_TSC:
+		return (sev_status & MSR_AMD64_SEV_SNP_ENABLED) &&
+			(sev_status & MSR_AMD64_SNP_SECURE_TSC);
+
 	case CC_ATTR_HOST_SEV_SNP:
 		return cc_flags.host_sev_snp;
 
arch/x86/coco/sev/core.c +107 -0
index feb145df6bf7..00a0ac3baab7 100644
@@ -96,6 +96,14 @@ static u64 sev_hv_features __ro_after_init;
 /* Secrets page physical address from the CC blob */
 static u64 secrets_pa __ro_after_init;
 
+/*
+ * For Secure TSC guests, the BSP fetches TSC_INFO using SNP guest messaging and
+ * initializes snp_tsc_scale and snp_tsc_offset. These values are replicated
+ * across the APs VMSA fields (TSC_SCALE and TSC_OFFSET).
+ */
+static u64 snp_tsc_scale __ro_after_init;
+static u64 snp_tsc_offset __ro_after_init;
+
 /* #VC handler runtime per-CPU data */
 struct sev_es_runtime_data {
 	struct ghcb ghcb_page;
@@ -1272,6 +1280,12 @@ static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
 	vmsa->vmpl		= snp_vmpl;
 	vmsa->sev_features	= sev_status >> 2;
 
+	/* Populate AP's TSC scale/offset to get accurate TSC values. */
+	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
+		vmsa->tsc_scale = snp_tsc_scale;
+		vmsa->tsc_offset = snp_tsc_offset;
+	}
+
 	/* Switch the page over to a VMSA page now that it is initialized */
 	ret = snp_set_vmsa(vmsa, caa, apic_id, true);
 	if (ret) {
@@ -3121,3 +3135,96 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
 	return 0;
 }
 EXPORT_SYMBOL_GPL(snp_send_guest_request);
+
+static int __init snp_get_tsc_info(void)
+{
+	struct snp_guest_request_ioctl *rio;
+	struct snp_tsc_info_resp *tsc_resp;
+	struct snp_tsc_info_req *tsc_req;
+	struct snp_msg_desc *mdesc;
+	struct snp_guest_req *req;
+	int rc = -ENOMEM;
+
+	tsc_req = kzalloc(sizeof(*tsc_req), GFP_KERNEL);
+	if (!tsc_req)
+		return rc;
+
+	/*
+	 * The intermediate response buffer is used while decrypting the
+	 * response payload. Make sure that it has enough space to cover
+	 * the authtag.
+	 */
+	tsc_resp = kzalloc(sizeof(*tsc_resp) + AUTHTAG_LEN, GFP_KERNEL);
+	if (!tsc_resp)
+		goto e_free_tsc_req;
+
+	req = kzalloc(sizeof(*req), GFP_KERNEL);
+	if (!req)
+		goto e_free_tsc_resp;
+
+	rio = kzalloc(sizeof(*rio), GFP_KERNEL);
+	if (!rio)
+		goto e_free_req;
+
+	mdesc = snp_msg_alloc();
+	if (IS_ERR_OR_NULL(mdesc))
+		goto e_free_rio;
+
+	rc = snp_msg_init(mdesc, snp_vmpl);
+	if (rc)
+		goto e_free_mdesc;
+
+	req->msg_version = MSG_HDR_VER;
+	req->msg_type = SNP_MSG_TSC_INFO_REQ;
+	req->vmpck_id = snp_vmpl;
+	req->req_buf = tsc_req;
+	req->req_sz = sizeof(*tsc_req);
+	req->resp_buf = (void *)tsc_resp;
+	req->resp_sz = sizeof(*tsc_resp) + AUTHTAG_LEN;
+	req->exit_code = SVM_VMGEXIT_GUEST_REQUEST;
+
+	rc = snp_send_guest_request(mdesc, req, rio);
+	if (rc)
+		goto e_request;
+
+	pr_debug("%s: response status 0x%x scale 0x%llx offset 0x%llx factor 0x%x\n",
+		 __func__, tsc_resp->status, tsc_resp->tsc_scale, tsc_resp->tsc_offset,
+		 tsc_resp->tsc_factor);
+
+	if (!tsc_resp->status) {
+		snp_tsc_scale = tsc_resp->tsc_scale;
+		snp_tsc_offset = tsc_resp->tsc_offset;
+	} else {
+		pr_err("Failed to get TSC info, response status 0x%x\n", tsc_resp->status);
+		rc = -EIO;
+	}
+
+e_request:
+	/* The response buffer contains sensitive data, explicitly clear it. */
+	memzero_explicit(tsc_resp, sizeof(*tsc_resp) + AUTHTAG_LEN);
+e_free_mdesc:
+	snp_msg_free(mdesc);
+e_free_rio:
+	kfree(rio);
+e_free_req:
+	kfree(req);
+ e_free_tsc_resp:
+	kfree(tsc_resp);
+e_free_tsc_req:
+	kfree(tsc_req);
+
+	return rc;
+}
+
+void __init snp_secure_tsc_prepare(void)
+{
+	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
+		return;
+
+	if (snp_get_tsc_info()) {
+		pr_alert("Unable to retrieve Secure TSC info from ASP\n");
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_SECURE_TSC);
+	}
+
+	pr_debug("SecureTSC enabled");
+}
arch/x86/mm/mem_encrypt.c +2 -0
index 0a120d85d7bb..95bae74fdab2 100644
@@ -94,6 +94,8 @@ void __init mem_encrypt_init(void)
 	/* Call into SWIOTLB to update the SWIOTLB DMA buffers */
 	swiotlb_update_mem_attributes();
 
+	snp_secure_tsc_prepare();
+
 	print_mem_encrypt_feature_info();
 }
 
maintainer
#22 lore raw

Re: [PATCH v16 05/13] x86/sev: Add Secure TSC support for SNP guests

Quoted 4 lines · Tom Lendacky

Although if you have to re-spin, it might make sense to use the __free() support to reduce the number of gotos and labels in snp_get_tsc_info(). You could also use kfree_sensitive() to automatically do the memzero_explicit(), too.

Let's keep that for future cleanups, pls, when there's time.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#23 lore raw

[tip: x86/sev] x86/sev: Add Secure TSC support for SNP guests

In reply to Nikunj A Dadhania
The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     85b60ca9ad2c94661acf86a0c11278246cc5ea86
Gitweb:        https://git.kernel.org/tip/85b60ca9ad2c94661acf86a0c11278246cc5ea86
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:25 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 20:27:23 +01:00

x86/sev: Add Secure TSC support for SNP guests

Add support for Secure TSC in SNP-enabled guests. Secure TSC allows guests to securely use RDTSC/RDTSCP instructions, ensuring that the parameters used cannot be altered by the hypervisor once the guest is launched.

Secure TSC-enabled guests need to query TSC information from the AMD Security Processor. This communication channel is encrypted between the AMD Security Processor and the guest, with the hypervisor acting merely as a conduit to deliver the guest messages to the AMD Security Processor. Each message is protected with AEAD (AES-256 GCM).

[ bp: Zap a stray newline over amd_cc_platform_has() while at it,
simplify CC_ATTR_GUEST_SNP_SECURE_TSC check ]

---
 arch/x86/coco/core.c              |   4 +-
 arch/x86/coco/sev/core.c          | 107 +++++++++++++++++++++++++++++-
 arch/x86/include/asm/sev-common.h |   1 +-
 arch/x86/include/asm/sev.h        |  21 ++++++-
 arch/x86/include/asm/svm.h        |   6 +-
 arch/x86/mm/mem_encrypt.c         |   2 +-
 include/linux/cc_platform.h       |   8 ++-
 7 files changed, 146 insertions(+), 3 deletions(-)
arch/x86/coco/core.c +3 -1
index 0f81f70..9a0ddda 100644
@@ -65,7 +65,6 @@ static __maybe_unused __always_inline bool amd_cc_platform_vtom(enum cc_attr att
  * up under SME the trampoline area cannot be encrypted, whereas under SEV
  * the trampoline area must be encrypted.
  */
-
 static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 {
 #ifdef CONFIG_AMD_MEM_ENCRYPT
@@ -97,6 +96,9 @@ static bool noinstr amd_cc_platform_has(enum cc_attr attr)
 	case CC_ATTR_GUEST_SEV_SNP:
 		return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
 
+	case CC_ATTR_GUEST_SNP_SECURE_TSC:
+		return sev_status & MSR_AMD64_SNP_SECURE_TSC;
+
 	case CC_ATTR_HOST_SEV_SNP:
 		return cc_flags.host_sev_snp;
 
arch/x86/coco/sev/core.c +107 -0

122 lines not on this page · raw message

arch/x86/include/asm/sev-common.h +1 -0
index 50f5666..6ef9243 100644
@@ -206,6 +206,7 @@ struct snp_psc_desc {
 #define GHCB_TERM_NO_SVSM		7	/* SVSM is not advertised in the secrets page */
 #define GHCB_TERM_SVSM_VMPL0		8	/* SVSM is present but has set VMPL to 0 */
 #define GHCB_TERM_SVSM_CAA		9	/* SVSM is present but CAA is not page aligned */
+#define GHCB_TERM_SECURE_TSC		10	/* Secure TSC initialization failed */
 
 #define GHCB_RESP_CODE(v)		((v) & GHCB_MSR_INFO_MASK)
 
arch/x86/include/asm/sev.h +21 -0
index 0937ac7..bdcdaac 100644
@@ -146,6 +146,9 @@ enum msg_type {
 	SNP_MSG_VMRK_REQ,
 	SNP_MSG_VMRK_RSP,
 
+	SNP_MSG_TSC_INFO_REQ = 17,
+	SNP_MSG_TSC_INFO_RSP,
+
 	SNP_MSG_TYPE_MAX
 };
 
@@ -174,6 +177,21 @@ struct snp_guest_msg {
 	u8 payload[PAGE_SIZE - sizeof(struct snp_guest_msg_hdr)];
 } __packed;
 
+#define SNP_TSC_INFO_REQ_SZ	128
+
+struct snp_tsc_info_req {
+	u8 rsvd[SNP_TSC_INFO_REQ_SZ];
+} __packed;
+
+struct snp_tsc_info_resp {
+	u32 status;
+	u32 rsvd1;
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u32 tsc_factor;
+	u8 rsvd2[100];
+} __packed;
+
 struct snp_guest_req {
 	void *req_buf;
 	size_t req_sz;
@@ -463,6 +481,8 @@ void snp_msg_free(struct snp_msg_desc *mdesc);
 int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 			   struct snp_guest_request_ioctl *rio);
 
+void __init snp_secure_tsc_prepare(void);
+
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
 #define snp_vmpl 0
@@ -503,6 +523,7 @@ static inline struct snp_msg_desc *snp_msg_alloc(void) { return NULL; }
 static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
 static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
+static inline void __init snp_secure_tsc_prepare(void) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/include/asm/svm.h +4 -2
index 2b59b99..92e1879 100644
@@ -417,7 +417,9 @@ struct sev_es_save_area {
 	u8 reserved_0x298[80];
 	u32 pkru;
 	u32 tsc_aux;
-	u8 reserved_0x2f0[24];
+	u64 tsc_scale;
+	u64 tsc_offset;
+	u8 reserved_0x300[8];
 	u64 rcx;
 	u64 rdx;
 	u64 rbx;
@@ -564,7 +566,7 @@ static inline void __unused_size_checks(void)
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x1c0);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x248);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x298);
-	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x2f0);
+	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x300);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x320);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x380);
 	BUILD_BUG_RESERVED_OFFSET(sev_es_save_area, 0x3f0);
arch/x86/mm/mem_encrypt.c +2 -0
index 0a120d8..95bae74 100644
@@ -94,6 +94,8 @@ void __init mem_encrypt_init(void)
 	/* Call into SWIOTLB to update the SWIOTLB DMA buffers */
 	swiotlb_update_mem_attributes();
 
+	snp_secure_tsc_prepare();
+
 	print_mem_encrypt_feature_info();
 }
 
include/linux/cc_platform.h +8 -0
index caa4b44..0bf7d33 100644
@@ -82,6 +82,14 @@ enum cc_attr {
 	CC_ATTR_GUEST_SEV_SNP,
 
 	/**
+	 * @CC_ATTR_GUEST_SNP_SECURE_TSC: SNP Secure TSC is active.
+	 *
+	 * The platform/OS is running as a guest/virtual machine and actively
+	 * using AMD SEV-SNP Secure TSC feature.
+	 */
+	CC_ATTR_GUEST_SNP_SECURE_TSC,
+
+	/**
 	 * @CC_ATTR_HOST_SEV_SNP: AMD SNP enabled on the host.
 	 *
 	 * The host kernel is running with the necessary features
#24 lore raw

PATCH v16 6/13 x86/sev: Change TSC MSR behavior for Secure TSC enabled guests

In reply to Nikunj A Dadhania

Secure TSC enabled guests should not write to the MSR_IA32_TSC(10H) register as the subsequent TSC value reads are undefined. For AMD platform, MSR_IA32_TSC is intercepted by the hypervisor. MSR_IA32_TSC read/write accesses should not exit to the hypervisor for such guests.

Accesses to MSR_IA32_TSC needs special handling in the #VC handler for the guests with Secure TSC enabled. Writes to MSR_IA32_TSC should be ignored and flagged once with a warning, and reads of MSR_IA32_TSC should return the result of the RDTSC instruction.

---
 arch/x86/coco/sev/core.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
arch/x86/coco/sev/core.c +38 -1
index 00a0ac3baab7..f49d3e97e170 100644
@@ -1428,6 +1428,34 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
 	return ES_OK;
 }
 
+/*
+ * TSC related accesses should not exit to the hypervisor when a guest is
+ * executing with Secure TSC enabled, so special handling is required for
+ * accesses of MSR_IA32_TSC.
+ */
+static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool write)
+{
+	u64 tsc;
+
+	/*
+	 * Writes: Writing to MSR_IA32_TSC can cause subsequent reads of the TSC
+	 *         to return undefined values, so ignore all writes.
+	 *
+	 * Reads: Reads of MSR_IA32_TSC should return the current TSC value, use
+	 *        the value returned by rdtsc_ordered().
+	 */
+	if (write) {
+		WARN_ONCE(1, "TSC MSR writes are verboten!\n");
+		return ES_OK;
+	}
+
+	tsc = rdtsc_ordered();
+	regs->ax = lower_32_bits(tsc);
+	regs->dx = upper_32_bits(tsc);
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct pt_regs *regs = ctxt->regs;
@@ -1437,8 +1465,17 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	/* Is it a WRMSR? */
 	write = ctxt->insn.opcode.bytes[1] == 0x30;
 
-	if (regs->cx == MSR_SVSM_CAA)
+	switch (regs->cx) {
+	case MSR_SVSM_CAA:
 		return __vc_handle_msr_caa(regs, write);
+	case MSR_IA32_TSC:
+		if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+			return __vc_handle_secure_tsc_msrs(regs, write);
+		else
+			break;
+	default:
+		break;
+	}
 
 	ghcb_set_rcx(ghcb, regs->cx);
 	if (write) {
Signature · 1 line
2.34.1
Reviewed-by on v16 6/13
#25 lore raw

Re: [PATCH v16 06/13] x86/sev: Change TSC MSR behavior for Secure TSC enabled guests

Quoted 11 lines · Nikunj A Dadhania

Secure TSC enabled guests should not write to the MSR_IA32_TSC(10H) register as the subsequent TSC value reads are undefined. For AMD platform, MSR_IA32_TSC is intercepted by the hypervisor. MSR_IA32_TSC read/write accesses should not exit to the hypervisor for such guests.

Accesses to MSR_IA32_TSC needs special handling in the #VC handler for the guests with Secure TSC enabled. Writes to MSR_IA32_TSC should be ignored and flagged once with a warning, and reads of MSR_IA32_TSC should return the result of the RDTSC instruction.

Quoted 62 lines
---
 arch/x86/coco/sev/core.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
arch/x86/coco/sev/core.c +38 -1
index 00a0ac3baab7..f49d3e97e170 100644
@@ -1428,6 +1428,34 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
 	return ES_OK;
 }
 
+/*
+ * TSC related accesses should not exit to the hypervisor when a guest is
+ * executing with Secure TSC enabled, so special handling is required for
+ * accesses of MSR_IA32_TSC.
+ */
+static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool write)
+{
+	u64 tsc;
+
+	/*
+	 * Writes: Writing to MSR_IA32_TSC can cause subsequent reads of the TSC
+	 *         to return undefined values, so ignore all writes.
+	 *
+	 * Reads: Reads of MSR_IA32_TSC should return the current TSC value, use
+	 *        the value returned by rdtsc_ordered().
+	 */
+	if (write) {
+		WARN_ONCE(1, "TSC MSR writes are verboten!\n");
+		return ES_OK;
+	}
+
+	tsc = rdtsc_ordered();
+	regs->ax = lower_32_bits(tsc);
+	regs->dx = upper_32_bits(tsc);
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct pt_regs *regs = ctxt->regs;
@@ -1437,8 +1465,17 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	/* Is it a WRMSR? */
 	write = ctxt->insn.opcode.bytes[1] == 0x30;
 
-	if (regs->cx == MSR_SVSM_CAA)
+	switch (regs->cx) {
+	case MSR_SVSM_CAA:
 		return __vc_handle_msr_caa(regs, write);
+	case MSR_IA32_TSC:
+		if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+			return __vc_handle_secure_tsc_msrs(regs, write);
+		else
+			break;
+	default:
+		break;
+	}
 
 	ghcb_set_rcx(ghcb, regs->cx);
 	if (write) {
#26 lore raw

[tip: x86/sev] x86/sev: Change TSC MSR behavior for Secure TSC enabled guests

In reply to Nikunj A Dadhania
The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     0f0502b8865c0a4c402e73aeb0fb406acc19d0d2
Gitweb:        https://git.kernel.org/tip/0f0502b8865c0a4c402e73aeb0fb406acc19d0d2
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:26 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 21:26:06 +01:00

x86/sev: Change TSC MSR behavior for Secure TSC enabled guests

Secure TSC enabled guests should not write to the MSR_IA32_TSC (0x10) register as the subsequent TSC value reads are undefined. On AMD, MSR_IA32_TSC is intercepted by the hypervisor by default. MSR_IA32_TSC read/write accesses should not exit to the hypervisor for such guests.

Accesses to MSR_IA32_TSC need special handling in the #VC handler for the guests with Secure TSC enabled. Writes to MSR_IA32_TSC should be ignored and flagged once with a warning, and reads of MSR_IA32_TSC should return the result of the RDTSC instruction.

[ bp: Massage commit message. ]

---
 arch/x86/coco/sev/core.c | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)
arch/x86/coco/sev/core.c +38 -1
index 7458805..cd5b9b7 100644
@@ -1433,6 +1433,34 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
 	return ES_OK;
 }
 
+/*
+ * TSC related accesses should not exit to the hypervisor when a guest is
+ * executing with Secure TSC enabled, so special handling is required for
+ * accesses of MSR_IA32_TSC.
+ */
+static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool write)
+{
+	u64 tsc;
+
+	/*
+	 * Writes: Writing to MSR_IA32_TSC can cause subsequent reads of the TSC
+	 *         to return undefined values, so ignore all writes.
+	 *
+	 * Reads: Reads of MSR_IA32_TSC should return the current TSC value, use
+	 *        the value returned by rdtsc_ordered().
+	 */
+	if (write) {
+		WARN_ONCE(1, "TSC MSR writes are verboten!\n");
+		return ES_OK;
+	}
+
+	tsc = rdtsc_ordered();
+	regs->ax = lower_32_bits(tsc);
+	regs->dx = upper_32_bits(tsc);
+
+	return ES_OK;
+}
+
 static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 {
 	struct pt_regs *regs = ctxt->regs;
@@ -1442,8 +1470,17 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	/* Is it a WRMSR? */
 	write = ctxt->insn.opcode.bytes[1] == 0x30;
 
-	if (regs->cx == MSR_SVSM_CAA)
+	switch (regs->cx) {
+	case MSR_SVSM_CAA:
 		return __vc_handle_msr_caa(regs, write);
+	case MSR_IA32_TSC:
+		if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+			return __vc_handle_secure_tsc_msrs(regs, write);
+		else
+			break;
+	default:
+		break;
+	}
 
 	ghcb_set_rcx(ghcb, regs->cx);
 	if (write) {
#27 lore raw

PATCH v16 7/13 x86/sev: Prevent GUEST_TSC_FREQ MSR interception for Secure TSC enabled guests

In reply to Nikunj A Dadhania

The hypervisor should not be intercepting GUEST_TSC_FREQ MSR(0xcOO10134) when Secure TSC is enabled. A #VC exception will be generated if the GUEST_TSC_FREQ MSR is being intercepted. If this should occur and Secure TSC is enabled, terminate guest execution.

---
 arch/x86/include/asm/msr-index.h |  1 +
 arch/x86/coco/sev/core.c         | 10 +++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
arch/x86/include/asm/msr-index.h +1 -0
index 3f3e2bc99162..9a71880eec07 100644
@@ -608,6 +608,7 @@
 #define MSR_AMD_PERF_CTL		0xc0010062
 #define MSR_AMD_PERF_STATUS		0xc0010063
 #define MSR_AMD_PSTATE_DEF_BASE		0xc0010064
+#define MSR_AMD64_GUEST_TSC_FREQ	0xc0010134
 #define MSR_AMD64_OSVW_ID_LENGTH	0xc0010140
 #define MSR_AMD64_OSVW_STATUS		0xc0010141
 #define MSR_AMD_PPIN_CTL		0xc00102f0
arch/x86/coco/sev/core.c +9 -1
index f49d3e97e170..dbf4531c6271 100644
@@ -1431,12 +1431,19 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
 /*
  * TSC related accesses should not exit to the hypervisor when a guest is
  * executing with Secure TSC enabled, so special handling is required for
- * accesses of MSR_IA32_TSC.
+ * accesses of MSR_IA32_TSC and MSR_AMD64_GUEST_TSC_FREQ.
  */
 static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool write)
 {
 	u64 tsc;
 
+	/*
+	 * GUEST_TSC_FREQ should not be intercepted when Secure TSC is enabled.
+	 * Terminate the SNP guest when the interception is enabled.
+	 */
+	if (regs->cx == MSR_AMD64_GUEST_TSC_FREQ)
+		return ES_VMM_ERROR;
+
 	/*
 	 * Writes: Writing to MSR_IA32_TSC can cause subsequent reads of the TSC
 	 *         to return undefined values, so ignore all writes.
@@ -1469,6 +1476,7 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	case MSR_SVSM_CAA:
 		return __vc_handle_msr_caa(regs, write);
 	case MSR_IA32_TSC:
+	case MSR_AMD64_GUEST_TSC_FREQ:
 		if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
 			return __vc_handle_secure_tsc_msrs(regs, write);
 		else
Signature · 1 line
2.34.1
#28 lore raw

[tip: x86/sev] x86/sev: Prevent GUEST_TSC_FREQ MSR interception for Secure TSC enabled guests

The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     38cc6495cdec18a448b9e1de45fedce4118833a2
Gitweb:        https://git.kernel.org/tip/38cc6495cdec18a448b9e1de45fedce4118833a2
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:27 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 21:26:19 +01:00

x86/sev: Prevent GUEST_TSC_FREQ MSR interception for Secure TSC enabled guests

The hypervisor should not be intercepting GUEST_TSC_FREQ MSR(0xcOO10134) when Secure TSC is enabled. A #VC exception will be generated otherwise. If this should occur and Secure TSC is enabled, terminate guest execution.

---
 arch/x86/coco/sev/core.c         | 10 +++++++++-
 arch/x86/include/asm/msr-index.h |  1 +
 2 files changed, 10 insertions(+), 1 deletion(-)
arch/x86/coco/sev/core.c +9 -1
index cd5b9b7..106bded 100644
@@ -1436,13 +1436,20 @@ static enum es_result __vc_handle_msr_caa(struct pt_regs *regs, bool write)
 /*
  * TSC related accesses should not exit to the hypervisor when a guest is
  * executing with Secure TSC enabled, so special handling is required for
- * accesses of MSR_IA32_TSC.
+ * accesses of MSR_IA32_TSC and MSR_AMD64_GUEST_TSC_FREQ.
  */
 static enum es_result __vc_handle_secure_tsc_msrs(struct pt_regs *regs, bool write)
 {
 	u64 tsc;
 
 	/*
+	 * GUEST_TSC_FREQ should not be intercepted when Secure TSC is enabled.
+	 * Terminate the SNP guest when the interception is enabled.
+	 */
+	if (regs->cx == MSR_AMD64_GUEST_TSC_FREQ)
+		return ES_VMM_ERROR;
+
+	/*
 	 * Writes: Writing to MSR_IA32_TSC can cause subsequent reads of the TSC
 	 *         to return undefined values, so ignore all writes.
 	 *
@@ -1474,6 +1481,7 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 	case MSR_SVSM_CAA:
 		return __vc_handle_msr_caa(regs, write);
 	case MSR_IA32_TSC:
+	case MSR_AMD64_GUEST_TSC_FREQ:
 		if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
 			return __vc_handle_secure_tsc_msrs(regs, write);
 		else
arch/x86/include/asm/msr-index.h +1 -0
index 3f3e2bc..9a71880 100644
@@ -608,6 +608,7 @@
 #define MSR_AMD_PERF_CTL		0xc0010062
 #define MSR_AMD_PERF_STATUS		0xc0010063
 #define MSR_AMD_PSTATE_DEF_BASE		0xc0010064
+#define MSR_AMD64_GUEST_TSC_FREQ	0xc0010134
 #define MSR_AMD64_OSVW_ID_LENGTH	0xc0010140
 #define MSR_AMD64_OSVW_STATUS		0xc0010141
 #define MSR_AMD_PPIN_CTL		0xc00102f0
#29 lore raw

PATCH v16 8/13 x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests

In reply to Nikunj A Dadhania

The hypervisor should not be intercepting RDTSC/RDTSCP when Secure TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP instructions are being intercepted. If this should occur and Secure TSC is enabled, guest execution should be terminated as the guest cannot rely on the TSC value provided by the hypervisor.

---
 arch/x86/coco/sev/shared.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/coco/sev/shared.c +10 -0
index 96023bd978cc..2e4122f8aa6b 100644
@@ -1141,6 +1141,16 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
 	bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
 	enum es_result ret;
 
+	/*
+	 * The hypervisor should not be intercepting RDTSC/RDTSCP when Secure
+	 * TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP
+	 * instructions are being intercepted. If this should occur and Secure
+	 * TSC is enabled, guest execution should be terminated as the guest
+	 * cannot rely on the TSC value provided by the hypervisor.
+	 */
+	if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+		return ES_VMM_ERROR;
+
 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
 	if (ret != ES_OK)
 		return ret;
Signature · 1 line
2.34.1
#30 lore raw

[tip: x86/sev] x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests

The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     eef679a4b52e35be3b4a982a7f42bcc16054ec62
Gitweb:        https://git.kernel.org/tip/eef679a4b52e35be3b4a982a7f42bcc16054ec62
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:28 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 21:26:20 +01:00

x86/sev: Prevent RDTSC/RDTSCP interception for Secure TSC enabled guests

The hypervisor should not be intercepting RDTSC/RDTSCP when Secure TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP instructions are being intercepted. If this should occur and Secure TSC is enabled, guest execution should be terminated as the guest cannot rely on the TSC value provided by the hypervisor.

---
 arch/x86/coco/sev/shared.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
arch/x86/coco/sev/shared.c +10 -0
index 71de531..4386f37 100644
@@ -1140,6 +1140,16 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
 	bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
 	enum es_result ret;
 
+	/*
+	 * The hypervisor should not be intercepting RDTSC/RDTSCP when Secure
+	 * TSC is enabled. A #VC exception will be generated if the RDTSC/RDTSCP
+	 * instructions are being intercepted. If this should occur and Secure
+	 * TSC is enabled, guest execution should be terminated as the guest
+	 * cannot rely on the TSC value provided by the hypervisor.
+	 */
+	if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+		return ES_VMM_ERROR;
+
 	ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
 	if (ret != ES_OK)
 		return ret;
#31 lore raw

PATCH v16 9/13 x86/sev: Mark Secure TSC as reliable clocksource

In reply to Nikunj A Dadhania

In SNP guest environment with Secure TSC enabled, unlike other clock sources (such as HPET, ACPI timer, APIC, etc), the RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. Even when the host CPU's TSC is tampered with, the Secure TSC enabled guest keeps on ticking forward. Hence, mark Secure TSC as the only reliable clock source, bypassing unstable calibration.

---
 arch/x86/mm/mem_encrypt_amd.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/mm/mem_encrypt_amd.c +4 -0
index 774f9677458f..fa0bc52ef707 100644
@@ -541,6 +541,10 @@ void __init sme_early_init(void)
 	 * kernel mapped.
 	 */
 	snp_update_svsm_ca();
+
+	/* Mark the TSC as reliable when Secure TSC is enabled */
+	if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 }
 
 void __init mem_encrypt_free_decrypted_mem(void)
Signature · 1 line
2.34.1
#32 lore raw

[tip: x86/sev] x86/sev: Mark the TSC in a secure TSC guest as reliable

The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     0a2a98f691f2c57db5bb321e68787cb1de29c7dd
Gitweb:        https://git.kernel.org/tip/0a2a98f691f2c57db5bb321e68787cb1de29c7dd
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:29 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Tue, 07 Jan 2025 21:26:20 +01:00

x86/sev: Mark the TSC in a secure TSC guest as reliable

In SNP guest environment with Secure TSC enabled, unlike other clock sources (such as HPET, ACPI timer, APIC, etc), the RDTSC instruction is handled without causing a VM exit, resulting in minimal overhead and jitters. Even when the host CPU's TSC is tampered with, the Secure TSC enabled guest keeps on ticking forward. Hence, mark Secure TSC as the only reliable clock source, bypassing unstable calibration.

[ bp: Massage. ]

---
 arch/x86/mm/mem_encrypt_amd.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/mm/mem_encrypt_amd.c +3 -0
index 774f967..b56c5c0 100644
@@ -541,6 +541,9 @@ void __init sme_early_init(void)
 	 * kernel mapped.
 	 */
 	snp_update_svsm_ca();
+
+	if (sev_status & MSR_AMD64_SNP_SECURE_TSC)
+		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 }
 
 void __init mem_encrypt_free_decrypted_mem(void)
#33 lore raw

PATCH v16 10/13 x86/tsc: Switch Secure TSC guests away from kvm-clock

In reply to Nikunj A Dadhania

Although the kernel switches over to a stable TSC clocksource instead of kvm-clock, TSC frequency calibration still relies on the kvm-clock based frequency calibration. This is due to kvmclock_init() unconditionally updating the x86_platform's CPU and TSC callbacks.

For Secure TSC enabled guests, use the GUEST_TSC_FREQ MSR to discover the TSC frequency instead of relying on kvm-clock based frequency calibration. Override both CPU and TSC frequency calibration callbacks with securetsc_get_tsc_khz(). Since the difference between CPU base and TSC frequency does not apply in this case, the same callback is being used.

Additionally, warn users when kvm-clock is selected as the clocksource for Secure TSC enabled guests. Users can change the clocksource to kvm-clock using the sysfs interface while running on a Secure TSC enabled guest. Switching to the hypervisor-controlled kvm-clock can lead to potential security issues.

Taint the kernel and issue a warning to the user when the clocksource switches to kvm-clock, ensuring they are aware of the change and its implications.

---
 arch/x86/include/asm/sev.h |  2 ++
 arch/x86/coco/sev/core.c   | 21 +++++++++++++++++++++
 arch/x86/kernel/kvmclock.c | 11 +++++++++++
 arch/x86/kernel/tsc.c      |  4 ++++
 4 files changed, 38 insertions(+)
arch/x86/include/asm/sev.h +2 -0
index bdcdaac4df1c..5d9685f92e5c 100644
@@ -482,6 +482,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
 			   struct snp_guest_request_ioctl *rio);
 
 void __init snp_secure_tsc_prepare(void);
+void __init snp_secure_tsc_init(void);
 
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
@@ -524,6 +525,7 @@ static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
 static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
 static inline void __init snp_secure_tsc_prepare(void) { }
+static inline void __init snp_secure_tsc_init(void) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/coco/sev/core.c +21 -0
index dbf4531c6271..9c971637e56b 100644
@@ -103,6 +103,7 @@ static u64 secrets_pa __ro_after_init;
  */
 static u64 snp_tsc_scale __ro_after_init;
 static u64 snp_tsc_offset __ro_after_init;
+static u64 snp_tsc_freq_khz __ro_after_init;
 
 /* #VC handler runtime per-CPU data */
 struct sev_es_runtime_data {
@@ -3273,3 +3274,23 @@ void __init snp_secure_tsc_prepare(void)
 
 	pr_debug("SecureTSC enabled");
 }
+
+static unsigned long securetsc_get_tsc_khz(void)
+{
+	return snp_tsc_freq_khz;
+}
+
+void __init snp_secure_tsc_init(void)
+{
+	unsigned long long tsc_freq_mhz;
+
+	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
+		return;
+
+	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
+	rdmsrl(MSR_AMD64_GUEST_TSC_FREQ, tsc_freq_mhz);
+	snp_tsc_freq_khz = (unsigned long)(tsc_freq_mhz * 1000);
+
+	x86_platform.calibrate_cpu = securetsc_get_tsc_khz;
+	x86_platform.calibrate_tsc = securetsc_get_tsc_khz;
+}
arch/x86/kernel/kvmclock.c +11 -0
index 5b2c15214a6b..960260a8d884 100644
@@ -21,6 +21,7 @@
 #include <asm/hypervisor.h>
 #include <asm/x86_init.h>
 #include <asm/kvmclock.h>
+#include <asm/sev.h>
 
 static int kvmclock __initdata = 1;
 static int kvmclock_vsyscall __initdata = 1;
@@ -150,6 +151,16 @@ bool kvm_check_and_clear_guest_paused(void)
 
 static int kvm_cs_enable(struct clocksource *cs)
 {
+	/*
+	 * TSC clocksource should be used for a guest with Secure TSC enabled,
+	 * taint the kernel and warn when the user changes the clocksource to
+	 * kvm-clock.
+	 */
+	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
+		add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
+		WARN_ONCE(1, "For Secure TSC guest, changing the clocksource is not allowed!\n");
+	}
+
 	vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);
 	return 0;
 }
arch/x86/kernel/tsc.c +4 -0
index a85594644e13..34dec0b72ea8 100644
@@ -31,6 +31,7 @@
 #include <asm/i8259.h>
 #include <asm/topology.h>
 #include <asm/uv/uv.h>
+#include <asm/sev.h>
 
 unsigned int __read_mostly cpu_khz;	/* TSC clocks / usec, not used here */
 EXPORT_SYMBOL(cpu_khz);
@@ -1514,6 +1515,9 @@ void __init tsc_early_init(void)
 	/* Don't change UV TSC multi-chassis synchronization */
 	if (is_early_uv_system())
 		return;
+
+	snp_secure_tsc_init();
+
 	if (!determine_cpu_tsc_frequencies(true))
 		return;
 	tsc_enable_sched_clock();
Signature · 1 line
2.34.1
maintainer
#34 lore raw

Re: [PATCH v16 10/13] x86/tsc: Switch Secure TSC guests away from kvm-clock

Review

Quoted 10 lines · Nikunj A Dadhania

static int kvm_cs_enable(struct clocksource *cs)
{

(quoted diff) +8 -0
+	/*
+	 * TSC clocksource should be used for a guest with Secure TSC enabled,
+	 * taint the kernel and warn when the user changes the clocksource to
+	 * kvm-clock.
+	 */
+	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
+		add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
+		WARN_ONCE(1, "For Secure TSC guest, changing the clocksource is not allowed!\n");

So this thing is trying to state that changing the clocksource is not allowed but it still allows it. Why not simply do this:

arch/x86/kernel/kvmclock.c +3 -7
index 960260a8d884..d8fef3a65a35 100644
@@ -151,14 +151,10 @@ bool kvm_check_and_clear_guest_paused(void)
 
 static int kvm_cs_enable(struct clocksource *cs)
 {
-	/*
-	 * TSC clocksource should be used for a guest with Secure TSC enabled,
-	 * taint the kernel and warn when the user changes the clocksource to
-	 * kvm-clock.
-	 */
+	/* Only the TSC should be used in a Secure TSC guest. */
 	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
-		add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
-		WARN_ONCE(1, "For Secure TSC guest, changing the clocksource is not allowed!\n");
+		WARN_ONCE(1, "Secure TSC guest, changing the clocksource is not allowed!\n");
+		return 1;
 	}
 
 	vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);

?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#35 lore raw

Re: [PATCH v16 10/13] x86/tsc: Switch Secure TSC guests away from kvm-clock

Quoted 38 lines · Borislav Petkov

Review

Quoted 10 lines · Nikunj A Dadhania

static int kvm_cs_enable(struct clocksource *cs)
{

(quoted diff) +8 -0
+	/*
+	 * TSC clocksource should be used for a guest with Secure TSC enabled,
+	 * taint the kernel and warn when the user changes the clocksource to
+	 * kvm-clock.
+	 */
+	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
+		add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
+		WARN_ONCE(1, "For Secure TSC guest, changing the clocksource is not allowed!\n");

So this thing is trying to state that changing the clocksource is not allowed but it still allows it. Why not simply do this:

arch/x86/kernel/kvmclock.c +3 -7
index 960260a8d884..d8fef3a65a35 100644
@@ -151,14 +151,10 @@ bool kvm_check_and_clear_guest_paused(void)
 
 static int kvm_cs_enable(struct clocksource *cs)
 {
-	/*
-	 * TSC clocksource should be used for a guest with Secure TSC enabled,
-	 * taint the kernel and warn when the user changes the clocksource to
-	 * kvm-clock.
-	 */
+	/* Only the TSC should be used in a Secure TSC guest. */
 	if (cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
-		add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
-		WARN_ONCE(1, "For Secure TSC guest, changing the clocksource is not allowed!\n");
+		WARN_ONCE(1, "Secure TSC guest, changing the clocksource is not allowed!\n");
+		return 1;
 	}
 
 	vclocks_set_used(VDSO_CLOCKMODE_PVCLOCK);
Quoted 1 line

?

Works as expected:

$ echo 'kvm-clock' > /sys/devices/system/clocksource/clocksource0/current_clocksource
[   30.333603] ------------[ cut here ]------------
[   30.334802] Secure TSC guest, changing the clocksource is not allowed!
[   30.336460] WARNING: CPU: 0 PID: 19 at arch/x86/kernel/kvmclock.c:156 kvm_cs_enable+0x57/0x70

Regards
Nikunj

#36 lore raw

[tip: x86/sev] x86/tsc: Init the TSC for Secure TSC guests

In reply to Nikunj A Dadhania
The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     73bbf3b0fbba9aa27fef07a1fbd837661a863f03
Gitweb:        https://git.kernel.org/tip/73bbf3b0fbba9aa27fef07a1fbd837661a863f03
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:30 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Wed, 08 Jan 2025 21:26:19 +01:00

x86/tsc: Init the TSC for Secure TSC guests

Use the GUEST_TSC_FREQ MSR to discover the TSC frequency instead of relying on kvm-clock based frequency calibration. Override both CPU and TSC frequency calibration callbacks with securetsc_get_tsc_khz(). Since the difference between CPU base and TSC frequency does not apply in this case, the same callback is being used.

[ bp: Carve out from

    https://lore.kernel.org/r/[email protected] ]
---
 arch/x86/coco/sev/core.c   | 21 +++++++++++++++++++++
 arch/x86/include/asm/sev.h |  2 ++
 arch/x86/kernel/tsc.c      |  4 ++++
 3 files changed, 27 insertions(+)
arch/x86/coco/sev/core.c +21 -0
index 106bded..65d676c 100644
@@ -103,6 +103,7 @@ static u64 secrets_pa __ro_after_init;
  */
 static u64 snp_tsc_scale __ro_after_init;
 static u64 snp_tsc_offset __ro_after_init;
+static u64 snp_tsc_freq_khz __ro_after_init;
 
 /* #VC handler runtime per-CPU data */
 struct sev_es_runtime_data {
@@ -3278,3 +3279,23 @@ void __init snp_secure_tsc_prepare(void)
 
 	pr_debug("SecureTSC enabled");
 }
+
+static unsigned long securetsc_get_tsc_khz(void)
+{
+	return snp_tsc_freq_khz;
+}
+
+void __init snp_secure_tsc_init(void)
+{
+	unsigned long long tsc_freq_mhz;
+
+	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC))
+		return;
+
+	setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
+	rdmsrl(MSR_AMD64_GUEST_TSC_FREQ, tsc_freq_mhz);
+	snp_tsc_freq_khz = (unsigned long)(tsc_freq_mhz * 1000);
+
+	x86_platform.calibrate_cpu = securetsc_get_tsc_khz;
+	x86_platform.calibrate_tsc = securetsc_get_tsc_khz;
+}
arch/x86/include/asm/sev.h +2 -0
index bdcdaac..5d9685f 100644
@@ -482,6 +482,7 @@ int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req
 			   struct snp_guest_request_ioctl *rio);
 
 void __init snp_secure_tsc_prepare(void);
+void __init snp_secure_tsc_init(void);
 
 #else	/* !CONFIG_AMD_MEM_ENCRYPT */
 
@@ -524,6 +525,7 @@ static inline void snp_msg_free(struct snp_msg_desc *mdesc) { }
 static inline int snp_send_guest_request(struct snp_msg_desc *mdesc, struct snp_guest_req *req,
 					 struct snp_guest_request_ioctl *rio) { return -ENODEV; }
 static inline void __init snp_secure_tsc_prepare(void) { }
+static inline void __init snp_secure_tsc_init(void) { }
 
 #endif	/* CONFIG_AMD_MEM_ENCRYPT */
 
arch/x86/kernel/tsc.c +4 -0
index 67aeaba..0864b31 100644
@@ -30,6 +30,7 @@
 #include <asm/i8259.h>
 #include <asm/topology.h>
 #include <asm/uv/uv.h>
+#include <asm/sev.h>
 
 unsigned int __read_mostly cpu_khz;	/* TSC clocks / usec, not used here */
 EXPORT_SYMBOL(cpu_khz);
@@ -1515,6 +1516,9 @@ void __init tsc_early_init(void)
 	/* Don't change UV TSC multi-chassis synchronization */
 	if (is_early_uv_system())
 		return;
+
+	snp_secure_tsc_init();
+
 	if (!determine_cpu_tsc_frequencies(true))
 		return;
 	tsc_enable_sched_clock();
#37 lore raw

PATCH v16 11/13 x86/tsc: Upgrade TSC clocksource rating for guests

In reply to Nikunj A Dadhania

Hypervisor platform setup (x86_hyper_init::init_platform) routines register their own PV clock sources (KVM, HyperV, and Xen) at different clock ratings, resulting in PV clocksource being selected even when a stable TSC clocksource is available. Upgrade the clock rating of the TSC early and regular clocksource to prefer TSC over PV clock sources when TSC is invariant, non-stop, and stable

Cc Alexey Makhalov <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Boris Ostrovsky <[email protected]>
---
 arch/x86/kernel/tsc.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/x86/kernel/tsc.c +21 -0
index 34dec0b72ea8..88d8bfceea04 100644
@@ -274,10 +274,29 @@ bool using_native_sched_clock(void)
 {
 	return static_call_query(pv_sched_clock) == native_sched_clock;
 }
+
+/*
+ * Upgrade the clock rating for TSC early and regular clocksource when the
+ * underlying platform provides non-stop, invariant, and stable TSC. TSC
+ * early/regular clocksource will be preferred over other PV clock sources.
+ */
+static void __init upgrade_clock_rating(struct clocksource *tsc_early,
+					struct clocksource *tsc)
+{
+	if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR) &&
+	    cpu_feature_enabled(X86_FEATURE_CONSTANT_TSC) &&
+	    cpu_feature_enabled(X86_FEATURE_NONSTOP_TSC) &&
+	    !tsc_unstable) {
+		tsc_early->rating = 449;
+		tsc->rating = 450;
+	}
+}
 #else
 u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock")));
 
 bool using_native_sched_clock(void) { return true; }
+
+static void __init upgrade_clock_rating(struct clocksource *tsc_early, struct clocksource *tsc) { }
 #endif
 
 notrace u64 sched_clock(void)
@@ -1564,6 +1583,8 @@ void __init tsc_init(void)
 	if (tsc_clocksource_reliable || no_tsc_watchdog)
 		tsc_disable_clocksource_watchdog();
 
+	upgrade_clock_rating(&clocksource_tsc_early, &clocksource_tsc);
+
 	clocksource_register_khz(&clocksource_tsc_early, tsc_khz);
 	detect_art();
 }
Signature · 1 line
2.34.1
maintainer
#38 lore raw

Re: [PATCH v16 11/13] x86/tsc: Upgrade TSC clocksource rating for guests

Quoted 7 lines · Nikunj A Dadhania

Hypervisor platform setup (x86_hyper_init::init_platform) routines register their own PV clock sources (KVM, HyperV, and Xen) at different clock ratings, resulting in PV clocksource being selected even when a stable TSC clocksource is available. Upgrade the clock rating of the TSC early and regular clocksource to prefer TSC over PV clock sources when TSC is invariant, non-stop, and stable

Quoted 8 lines
Cc Alexey Makhalov <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Boris Ostrovsky <[email protected]>
---
 arch/x86/kernel/tsc.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

This needs to make it perfectly clear that it is about virt and not in general:

arch/x86/kernel/tsc.c +9 -7
index e98b7e585c1c..3741d097d925 100644
@@ -276,14 +276,16 @@ bool using_native_sched_clock(void)
 
 /*
  * Upgrade the clock rating for TSC early and regular clocksource when the
- * underlying platform provides non-stop, invariant, and stable TSC. TSC
+ * underlying guest is provided a non-stop, invariant, and stable TSC. TSC
  * early/regular clocksource will be preferred over other PV clock sources.
  */
-static void __init upgrade_clock_rating(struct clocksource *tsc_early,
-					struct clocksource *tsc)
+static void __init virt_upgrade_clock_rating(struct clocksource *tsc_early,
+					     struct clocksource *tsc)
 {
-	if (cpu_feature_enabled(X86_FEATURE_HYPERVISOR) &&
-	    cpu_feature_enabled(X86_FEATURE_CONSTANT_TSC) &&
+	if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
+		return;
+
+	if (cpu_feature_enabled(X86_FEATURE_CONSTANT_TSC) &&
 	    cpu_feature_enabled(X86_FEATURE_NONSTOP_TSC) &&
 	    !tsc_unstable) {
 		tsc_early->rating = 449;
@@ -295,7 +297,7 @@ u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock")));
 
 bool using_native_sched_clock(void) { return true; }
 
-static void __init upgrade_clock_rating(struct clocksource *tsc_early, struct clocksource *tsc) { }
+static void __init virt_upgrade_clock_rating(struct clocksource *tsc_early, struct clocksource *tsc) { }
 #endif
 
 notrace u64 sched_clock(void)
@@ -1584,7 +1586,7 @@ void __init tsc_init(void)
 	if (tsc_clocksource_reliable || no_tsc_watchdog)
 		tsc_disable_clocksource_watchdog();
 
-	upgrade_clock_rating(&clocksource_tsc_early, &clocksource_tsc);
+	virt_upgrade_clock_rating(&clocksource_tsc_early, &clocksource_tsc);
 
 	clocksource_register_khz(&clocksource_tsc_early, tsc_khz);
 	detect_art();
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#39 lore raw

PATCH v16 12/13 x86/tsc: Switch to native sched clock

In reply to Nikunj A Dadhania

Although the kernel switches over to stable TSC clocksource instead of PV clocksource, the scheduler still keeps on using PV clocks as the sched clock source. This is because KVM, Xen and VMWare, switch the paravirt sched clock handler in their init routines. HyperV is the only PV clock source that checks if the platform provides an invariant TSC and does not switch to PV sched clock.

When switching back to stable TSC, restore the scheduler clock to native_sched_clock().

As the clock selection happens in the stop machine context, schedule delayed work to update the static_call()

Cc Alexey Makhalov <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Boris Ostrovsky <[email protected]>
---
 arch/x86/kernel/tsc.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
arch/x86/kernel/tsc.c +18 -0
index 88d8bfceea04..fe7a0b1b7cfd 100644
@@ -291,12 +291,26 @@ static void __init upgrade_clock_rating(struct clocksource *tsc_early,
 		tsc->rating = 450;
 	}
 }
+
+static void enable_native_sc_work(struct work_struct *work)
+{
+	pr_info("Using native sched clock\n");
+	paravirt_set_sched_clock(native_sched_clock);
+}
+static DECLARE_DELAYED_WORK(enable_native_sc, enable_native_sc_work);
+
+static void enable_native_sched_clock(void)
+{
+	if (!using_native_sched_clock())
+		schedule_delayed_work(&enable_native_sc, 0);
+}
 #else
 u64 sched_clock_noinstr(void) __attribute__((alias("native_sched_clock")));
 
 bool using_native_sched_clock(void) { return true; }
 
 static void __init upgrade_clock_rating(struct clocksource *tsc_early, struct clocksource *tsc) { }
+static void enable_native_sched_clock(void) { }
 #endif
 
 notrace u64 sched_clock(void)
@@ -1176,6 +1190,10 @@ static void tsc_cs_tick_stable(struct clocksource *cs)
 static int tsc_cs_enable(struct clocksource *cs)
 {
 	vclocks_set_used(VDSO_CLOCKMODE_TSC);
+
+	/* Restore native_sched_clock() when switching to TSC */
+	enable_native_sched_clock();
+
 	return 0;
 }
 
Signature · 1 line
2.34.1
maintainer
#40 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 6 lines · Nikunj A Dadhania

Although the kernel switches over to stable TSC clocksource instead of PV clocksource, the scheduler still keeps on using PV clocks as the sched clock source. This is because KVM, Xen and VMWare, switch the paravirt sched clock handler in their init routines. HyperV is the only PV clock source that checks if the platform provides an invariant TSC and does not switch to PV sched clock.

So this below looks like some desperate hackery. Are you doing this because kvm_sched_clock_init() does

	paravirt_set_sched_clock(kvm_sched_clock_read);

?

If so, why don't you simply prevent that on STSC guests?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#41 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 8 lines · Borislav Petkov
Quoted 6 lines · Nikunj A Dadhania

Although the kernel switches over to stable TSC clocksource instead of PV clocksource, the scheduler still keeps on using PV clocks as the sched clock source. This is because KVM, Xen and VMWare, switch the paravirt sched clock handler in their init routines. HyperV is the only PV clock source that checks if the platform provides an invariant TSC and does not switch to PV sched clock.

Quoted 6 lines

So this below looks like some desperate hackery. Are you doing this because kvm_sched_clock_init() does

	paravirt_set_sched_clock(kvm_sched_clock_read);

?

All guests(including STSC) running on KVM or other hypervisors like Xen/VMWare uses the regular TSC when the guest is booted with TscInvariant bit set. But it doesn't switch the sched clock to use TSC which it should have, pointed here[1] by Sean:

No, I'm saying that the guest should prefer the raw TSC over kvmclock if the TSC is stable, irrespective of SNP or TDX. This is effectively already done for the timekeeping base (see commit 7539b174aef4 ("x86: kvmguest: use TSC clocksource if invariant TSC is exposed")), but the scheduler still uses kvmclock thanks to the kvm_sched_clock_init() code.

Quoted 1 line

If so, why don't you simply prevent that on STSC guests?

This is for all guests running on different hypervisor with stable TSC. And I did try to limit this to KVM here [2], here is Sean's comment to that implementation:

Quoted 4 lines

Although the kernel switches over to stable TSC clocksource instead of kvmclock, the scheduler still keeps on using kvmclock as the sched clock. This is due to kvm_sched_clock_init() updating the pv_sched_clock() unconditionally.

All PV clocks are affected by this, no? This seems like something that should be handled in common code, which is the point I was trying to make in v11.

Regards
Nikunj

1. https://lore.kernel.org/kvm/[email protected]/
2. https://lore.kernel.org/kvm/[email protected]/

maintainer
#42 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 4 lines · Nikunj A. Dadhania

All guests(including STSC) running on KVM or other hypervisors like Xen/VMWare uses the regular TSC when the guest is booted with TscInvariant bit set. But it doesn't switch the sched clock to use TSC which it should have, pointed here[1] by Sean:

Well, that paravirt_set_sched_clock() thing is there for a reason and all the different guest types which call it perhaps have their reasons.

Now, if you think it would make sense to use the TSC in every guest type if the TSC is this and that, then I'd need to see a patch or even a patchset which explains why it is ok to do so on every guest type and then tested on every guest type and then each patch would convert a single guest type and properly explain why.

And your patch doesn't have that at all. I know Sean thinks it is a good idea and perhaps it is but without proper justification and acks from the other guest type maintainers, I simply won't take such a patch(-set).

And even if it is good, you need to solve it another way - not with this delayed-work hackery.

IOW, this switching the paravirt clock crap to use TSC when the TSC is this and that and the HV tells you so, should be a wholly separate patchset with reviews and acks and the usual shebang.

If you want to take care only of STSC now, I'd take a patch which is known good and tested properly. And that should happen very soon because the merge window is closing in. Otherwise, there's always another merge window and rushing things doesn't do any good anyway, as I keep preaching...

Your call.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#43 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 4 lines · Borislav Petkov

On Wed, Jan 08, 2025 at 10:50:13AM +0530, Nikunj A. Dadhania wrote:
And your patch doesn't have that at all. I know Sean thinks it is a good idea and perhaps it is but without proper justification and acks from the other guest type maintainers, I simply won't take such a patch(-set).

I haven't heard anything from other guest type maintainers yet, and they are on CC from v12 onward.

Quoted 6 lines

And even if it is good, you need to solve it another way - not with this delayed-work hackery.

IOW, this switching the paravirt clock crap to use TSC when the TSC is this and that and the HV tells you so, should be a wholly separate patchset with reviews and acks and the usual shebang.

I agree, we should handle this as a separate patch series.

Quoted 3 lines

If you want to take care only of STSC now, I'd take a patch which is known good and tested properly. And that should happen very soon because the merge window is closing in.

In that case, let me limit this only to STSC for now, i will send updated patch.

Regards,
Nikunj

#44 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 6 lines · Nikunj A. Dadhania
Quoted 4 lines

If you want to take care only of STSC now, I'd take a patch which is known good and tested properly. And that should happen very soon because the merge window is closing in.

In that case, let me limit this only to STSC for now, i will send updated patch.

From: Nikunj A Dadhania <[email protected]>
Date: Wed, 8 Jan 2025 14:18:04 +0530
Subject: [PATCH] x86/kvmclock: Prefer TSC as the scheduler clock for Secure
TSC guests

Although the kernel switches over to a stable TSC clocksource instead of kvmclock, the scheduler still keeps on using kvmclock as the sched clock. This is due to kvm_sched_clock_init() updating the pv_sched_clock() unconditionally. Do not override the PV sched clock when Secure TSC is enabled.

---
 arch/x86/kernel/kvmclock.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/kernel/kvmclock.c +4 -2
index d8fef3a65a35..82c4743a5e7a 100644
@@ -324,8 +324,10 @@ void __init kvmclock_init(void)
 	if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
 		pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
 
-	flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
-	kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
+	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
+		flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+		kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
+	}
 
 	x86_platform.calibrate_tsc = kvm_get_tsc_khz;
 	x86_platform.calibrate_cpu = kvm_get_tsc_khz;
Signature · 1 line
2.34.1
#45 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 30 lines · Nikunj A. Dadhania
Quoted 6 lines · Nikunj A. Dadhania
Quoted 4 lines

If you want to take care only of STSC now, I'd take a patch which is known good and tested properly. And that should happen very soon because the merge window is closing in.

In that case, let me limit this only to STSC for now, i will send updated patch.

From: Nikunj A Dadhania <[email protected]>
Date: Wed, 8 Jan 2025 14:18:04 +0530
Subject: [PATCH] x86/kvmclock: Prefer TSC as the scheduler clock for Secure
TSC guests

Although the kernel switches over to a stable TSC clocksource instead of kvmclock, the scheduler still keeps on using kvmclock as the sched clock. This is due to kvm_sched_clock_init() updating the pv_sched_clock() unconditionally. Do not override the PV sched clock when Secure TSC is enabled.

---
 arch/x86/kernel/kvmclock.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
arch/x86/kernel/kvmclock.c +0 -0
index d8fef3a65a35..82c4743a5e7a 100644

arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 330

Quoted 9 lines
arch/x86/kernel/kvmclock.c +4 -2
@@ -324,8 +324,10 @@ void __init kvmclock_init(void)
 	if (kvm_para_has_feature(KVM_FEATURE_CLOCKSOURCE_STABLE_BIT))
 		pvclock_set_flags(PVCLOCK_TSC_STABLE_BIT);
 
-	flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
-	kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
+	if (!cc_platform_has(CC_ATTR_GUEST_SNP_SECURE_TSC)) {
+		flags = pvclock_read_flags(&hv_clock_boot[0].pvti);
+		kvm_sched_clock_init(flags & PVCLOCK_TSC_STABLE_BIT);
+	}

This still misses my point. Ditto for the "x86/tsc: Switch Secure TSC guests away from kvm-clock".

I object to singling out kvmclock. It's weird and misleading, because handling only kvmclock suggests that other PV clocks are somehow trusted/ok, when in reality the only reason kvmclock is getting singled out is (presumably) because it's what Nikunj and the other folks enabling KVM SNP test on.

What I care most about is having a sane, consistent policy throughout the kernel. E.g. so that a user/reader walks away with an understanding PV clocks are a theoretical host attack vector and so should be avoided when Secure TSC is available.

Ideally, if the TSC is the preferred clocksource, then the scheduler will use the TSC and not a PV clock irrespective of STSC. But I 100% agree with Boris that it needs buy-in from other maintainers (including Paolo), because it's entirely possible (likely, even) that there's an angle to scheduling I'm not considering.

maintainer
#46 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 1 line · Sean Christopherson

This still misses my point.

Oh I got it, no worries.

Quoted 6 lines

Ditto for the "x86/tsc: Switch Secure TSC guests away from kvm-clock".

I object to singling out kvmclock. It's weird and misleading, because handling only kvmclock suggests that other PV clocks are somehow trusted/ok, when in reality the only reason kvmclock is getting singled out is (presumably) because it's what Nikunj and the other folks enabling KVM SNP test on.

Presumably.

Quoted 4 lines

What I care most about is having a sane, consistent policy throughout the kernel. E.g. so that a user/reader walks away with an understanding PV clocks are a theoretical host attack vector and so should be avoided when Secure TSC is available.

Yap, agreed.

Quoted 4 lines

Ideally, if the TSC is the preferred clocksource, then the scheduler will use the TSC and not a PV clock irrespective of STSC. But I 100% agree with Boris that it needs buy-in from other maintainers (including Paolo), because it's entirely possible (likely, even) that there's an angle to scheduling I'm not considering.

That's exactly why I wanted to have this taken care of only for the STSC side of things now and temporarily. So that we can finally land those STSC patches
- they've been pending for waaay too long.

And then ask Nikunj nicely to clean up this whole pv clock gunk, potentially kill some of those old clocksources which probably don't matter anymore.

But your call how/when you wanna do this.

If you want the cleanup first, I'll take only a subset of the STSC set so that I can unload some of that set upstream.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#47 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 6 lines · Borislav Petkov
Quoted 4 lines · Sean Christopherson

Ideally, if the TSC is the preferred clocksource, then the scheduler will use the TSC and not a PV clock irrespective of STSC. But I 100% agree with Boris that it needs buy-in from other maintainers (including Paolo), because it's entirely possible (likely, even) that there's an angle to scheduling I'm not considering.

Quoted 8 lines

That's exactly why I wanted to have this taken care of only for the STSC side of things now and temporarily. So that we can finally land those STSC patches
- they've been pending for waaay too long.

And then ask Nikunj nicely to clean up this whole pv clock gunk, potentially kill some of those old clocksources which probably don't matter anymore.

But your call how/when you wanna do this.

I'm okay starting with just TDX and SNP guests, but I don't want to special case SNP's Secure TSC anywhere in kvmclock or common TSC/sched code.

For TDX guests, the TSC is _always_ "secure". So similar to singling out kvmclock, handling SNP's STSC but not the TDX case again leaves the kernel in an inconsistent state. Which is why I originally suggested[*] fixing the sched_clock mess in a generically; doing so would avoid the need to special case SNP or TDX in code that doesn't/shouldn't care about SNP or TDX.

[*] https://lore.kernel.org/all/[email protected]

Quoted 2 lines

If you want the cleanup first, I'll take only a subset of the STSC set so that I can unload some of that set upstream.

My vote is to apply through "x86/sev: Mark Secure TSC as reliable clocksource", and then split "x86/tsc: Switch Secure TSC guests away from kvm-clock" to grab only the snp_secure_tsc_init() related changes (which is how that patch should be constructed no matter what; adding support for MSR_AMD64_GUEST_TSC_FREQ has nothing to do with kvmclock).

And then figure out how to wrangle clocksource and sched_clock in a way that is sane and consistent.

maintainer
#48 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 4 lines · Sean Christopherson

I'm okay starting with just TDX and SNP guests, but I don't want to special case SNP's Secure TSC anywhere in kvmclock or common TSC/sched code.

For TDX guests, the TSC is _always_ "secure".

Ah, good to know. I was wondering what the situation in TDX land is wrt TSC...

Quoted 5 lines

So similar to singling out kvmclock,
handling SNP's STSC but not the TDX case again leaves the kernel in an inconsistent state. Which is why I originally suggested[*] fixing the sched_clock mess in a generically; doing so would avoid the need to special case SNP or TDX in code that doesn't/shouldn't care about SNP or TDX.

Right.

Quoted 8 lines

My vote is to apply through "x86/sev: Mark Secure TSC as reliable clocksource", and then split "x86/tsc: Switch Secure TSC guests away from kvm-clock" to grab only the snp_secure_tsc_init() related changes (which is how that patch should be constructed no matter what; adding support for MSR_AMD64_GUEST_TSC_FREQ has nothing to do with kvmclock).

And then figure out how to wrangle clocksource and sched_clock in a way that is sane and consistent.

Makes sense to me, I'll carve out the bits.

Nikunj, I'd appreciate it if you gathered whatever bits are left wrt kvmclock and take care of things as Sean suggests above.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#49 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson
Quoted 1 line · Borislav Petkov

On Wed, Jan 08, 2025 at 06:00:59AM -0800, Sean Christopherson wrote:

For TDX guests, the TSC is _always_ "secure". So similar to singling out kvmclock,
handling SNP's STSC but not the TDX case again leaves the kernel in an inconsistent
state. Which is why I originally suggested[*] fixing the sched_clock mess in a
generically;
doing so would avoid the need to special case SNP or TDX in code> that doesn't/shouldn't care about SNP or TDX.

That is what I have attempted in this patch[1] where irrespective of SNP/TDX, whenever TSC is picked up as a clock source, sched-clock will start using TSC instead of any PV sched clock. This does not have any special case for STSC/SNP/TDX.

Quoted 1 line

[*] https://lore.kernel.org/all/[email protected]

Regards
Nikunj

1. https://lore.kernel.org/kvm/[email protected]/

#50 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 10 lines · Nikunj A. Dadhania
Quoted 8 lines · Sean Christopherson
Quoted 1 line · Borislav Petkov

On Wed, Jan 08, 2025 at 06:00:59AM -0800, Sean Christopherson wrote:

For TDX guests, the TSC is _always_ "secure". So similar to singling out kvmclock,
handling SNP's STSC but not the TDX case again leaves the kernel in an inconsistent
state. Which is why I originally suggested[*] fixing the sched_clock mess in a
generically;
doing so would avoid the need to special case SNP or TDX in code> that doesn't/shouldn't care about SNP or TDX.

Quoted 3 lines

That is what I have attempted in this patch[1] where irrespective of SNP/TDX, whenever TSC is picked up as a clock source, sched-clock will start using TSC instead of any PV sched clock. This does not have any special case for STSC/SNP/TDX.

*sigh*

This is a complete and utter trainwreck.

Paolo had an idea to handle this without a deferred callback by having kvmclock detect if kvmclock is selected as the clocksource, OR if the TSC is unreliable, i.e. if the kernel may switch to kvmclock due to the TSC being marked unreliable.

Unfortunately, that idea doesn't work because the ordering is all wrong. The *early* TSC initialization in setup_arch() happens after kvmclock_init() (via init_hypervisor_platform())

	init_hypervisor_platform();

	tsc_early_init();

and even if we mucked with that, __clocksource_select() very deliberately doesn't change the clocksource until the kernel is "finished" booting, in order to avoid thrashing the clocksource.

static struct clocksource *clocksource_find_best(bool oneshot, bool skipcur)

  {
	struct clocksource *cs;

	if (!finished_booting || list_empty(&clocksource_list)) <===
		return NULL;

	...

}

/*
* clocksource_done_booting - Called near the end of core bootup *
* Hack to avoid lots of clocksource churn at boot time.
* We use fs_initcall because we want this to start before
* device_initcall but after subsys_initcall.
*/
static int __init clocksource_done_booting(void)

  {
	mutex_lock(&clocksource_mutex);
	curr_clocksource = clocksource_default_clock();
	finished_booting = 1;
	/*
	 * Run the watchdog first to eliminate unstable clock sources
	 */
	__clocksource_watchdog_kthread();
	clocksource_select();
	mutex_unlock(&clocksource_mutex);
	return 0;

}
fs_initcall(clocksource_done_booting);

I fiddled with a variety of ideas to try and let kvmclock tell sched_clock that it prefers the TSC, e.g. if TSC_RELIABLE is set, but after seeing this comment in native_sched_clock():

/*
* Fall back to jiffies if there's no TSC available:
* ( But note that we still use it if the TSC is marked
* unstable. We do this because unlike Time Of Day,
* the scheduler clock tolerates small errors and it's
* very important for it to be as fast as the platform
* can achieve it. )
*/

My strong vote is prefer TSC over kvmclock for sched_clock if the TSC is constant, nonstop, and not marked stable via command line. I.e. use the same criteria as tweaking the clocksource rating. As above, sched_clock is more tolerant of slop than clocksource, so it's a bit ridiculous to care whether the TSC or kvmclock (or something else entirely) is used for the clocksource.

If we wanted to go with a more conservative approach, e.g. to minimize the risk of breaking existing setups, we could also condition the change on the TSC being reliable and having a known frequency. I.e. require SNP's Secure TSC, or require the hypervisor to enumerate the TSC frequency via CPUID. I don't see a ton of value in that approach though, and long-term it would lead to some truly weird code due to holding sched_clock to a higher standard than clocksource.

But wait, there's more! Because TDX doesn't override .calibrate_tsc() or .calibrate_cpu(), even though TDX provides a trusted TSC *and* enumerates the frequency of the TSC, unless I'm missing something, tsc_early_init() will compute the TSC frequency using the information provided by KVM, i.e. the untrusted host.

The "obvious" solution is to leave the calibration functions as-is if the TSC has a known, reliable frequency, but even _that_ is riddled with problems, because as-is, the kernel sets TSC_KNOWN_FREQ and TSC_RELIABLE in tsc_early_init(), which runs *after* init_hypervisor_platform(). SNP Secure TSC fudges around this by overiding the calibration routines, but that's a bit gross and easy to fix if we also fix TDX. And fixing TDX by running native_calibrate_tsc() would give the same love to setups where the hypervisor provides CPUID 0x15 and/or 0x16.

All in all, I'm thinking something like this (across multiple patches):

---
 arch/x86/include/asm/tsc.h |  1 +
 arch/x86/kernel/kvmclock.c | 55 ++++++++++++++++++++++++++------------
 arch/x86/kernel/setup.c    |  7 +++++
 arch/x86/kernel/tsc.c      | 32 +++++++++++++++++-----
 4 files changed, 72 insertions(+), 23 deletions(-)
arch/x86/include/asm/tsc.h +1 -0
index 94408a784c8e..e13d6c3f2298 100644
@@ -28,6 +28,7 @@ static inline cycles_t get_cycles(void)
 }
 #define get_cycles get_cycles
 
+extern void tsc_early_detect(void);
 extern void tsc_early_init(void);
 extern void tsc_init(void);
 extern void mark_tsc_unstable(char *reason);
arch/x86/kernel/kvmclock.c +38 -17

71 lines not on this page · raw message

arch/x86/kernel/setup.c +7 -0
index f1fea506e20f..2b6800426349 100644
@@ -907,6 +907,13 @@ void __init setup_arch(char **cmdline_p)
 	reserve_ibft_region();
 	x86_init.resources.dmi_setup();
 
+	/*
+	 * Detect, but do not fully initialize, TSC info before initializing
+	 * the hypervisor platform, so that hypervisor code can make informed
+	 * decisions about using a paravirt clock vs. TSC.
+	 */
+	tsc_early_detect();
+
 	/*
 	 * VMware detection requires dmi to be available, so this
 	 * needs to be done after dmi_setup(), for the boot CPU.
arch/x86/kernel/tsc.c +26 -6
index 0864b314c26a..9baffb425386 100644
@@ -663,7 +663,12 @@ unsigned long native_calibrate_tsc(void)
 	unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
 	unsigned int crystal_khz;
 
-	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
+	/*
+	 * Ignore the vendor when running as a VM, if the hypervisor provides
+	 * garbage CPUID information then the vendor is also suspect.
+	 */
+	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
+	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		return 0;
 
 	if (boot_cpu_data.cpuid_level < 0x15)
@@ -713,10 +718,13 @@ unsigned long native_calibrate_tsc(void)
 		return 0;
 
 	/*
-	 * For Atom SoCs TSC is the only reliable clocksource.
-	 * Mark TSC reliable so no watchdog on it.
+	 * For Atom SoCs TSC is the only reliable clocksource.  Similarly, in a
+	 * VM, any watchdog is going to be less reliable than the TSC as the
+	 * watchdog source will be emulated in software.  In both cases, mark
+	 * the TSC reliable so that no watchdog runs on it.
 	 */
-	if (boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
+	if (boot_cpu_has(X86_FEATURE_HYPERVISOR) ||
+	    boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 
 #ifdef CONFIG_X86_LOCAL_APIC
@@ -1509,6 +1517,20 @@ static void __init tsc_enable_sched_clock(void)
 	static_branch_enable(&__use_tsc);
 }
 
+void __init tsc_early_detect(void)
+{
+	if (!boot_cpu_has(X86_FEATURE_TSC))
+		return;
+
+	snp_secure_tsc_init();
+
+	/*
+	 * Run through native TSC calibration to set TSC_KNOWN_FREQ and/or
+	 * TSC_RELIABLE as appropriate.
+	 */
+	native_calibrate_tsc();
+}
+
 void __init tsc_early_init(void)
 {
 	if (!boot_cpu_has(X86_FEATURE_TSC))
@@ -1517,8 +1539,6 @@ void __init tsc_early_init(void)
 	if (is_early_uv_system())
 		return;
 
-	snp_secure_tsc_init();
-
 	if (!determine_cpu_tsc_frequencies(true))
 		return;
 	tsc_enable_sched_clock();

base-commit: 0563ee35ae2c9cfb0c6a7b2c0ddf7d9372bb8a98

maintainer
#51 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 2 lines · Sean Christopherson

My strong vote is prefer TSC over kvmclock for sched_clock if the TSC is constant, nonstop, and not marked stable via command line.

So how do we deal with the case here where a malicious HV could set those bits and still tweak the TSC?

IOW, I think Secure TSC and TDX should be the only ones who trust the TSC when in a guest.

If anything, trusting the TSC in a normal guest should at least issue a warning saying that we do use the TSC but there's no 100% guarantee that it is trustworthy...

Quoted 4 lines

But wait, there's more! Because TDX doesn't override .calibrate_tsc() or .calibrate_cpu(), even though TDX provides a trusted TSC *and* enumerates the frequency of the TSC, unless I'm missing something, tsc_early_init() will compute the TSC frequency using the information provided by KVM, i.e. the untrusted host.

Yeah, I guess we don't want that. Or at least we should warn about it.

arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 332

Quoted 13 lines
arch/x86/kernel/kvmclock.c +13 -0
+	/*
+	 * If the TSC counts at a constant frequency across P/T states, counts
+	 * in deep C-states, and the TSC hasn't been marked unstable, prefer
+	 * the TSC over kvmclock for sched_clock and drop kvmclock's rating so
+	 * that TSC is chosen as the clocksource.  Note, the TSC unstable check
+	 * exists purely to honor the TSC being marked unstable via command
+	 * line, any runtime detection of an unstable will happen after this.
+	 */
+	if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
+	    boot_cpu_has(X86_FEATURE_NONSTOP_TSC) &&
+	    !check_tsc_unstable()) {
+		kvm_clock.rating = 299;
+		pr_warn("kvm-clock: Using native sched_clock\n");

The warn is in the right direction but probably should say TSC still cannot be trusted 100%.

Quoted 25 lines
arch/x86/kernel/tsc.c +7 -3
index 0864b314c26a..9baffb425386 100644
@@ -663,7 +663,12 @@ unsigned long native_calibrate_tsc(void)
 	unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
 	unsigned int crystal_khz;
 
-	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
+	/*
+	 * Ignore the vendor when running as a VM, if the hypervisor provides
+	 * garbage CPUID information then the vendor is also suspect.
+	 */
+	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
+	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		return 0;
 
 	if (boot_cpu_data.cpuid_level < 0x15)
@@ -713,10 +718,13 @@ unsigned long native_calibrate_tsc(void)
 		return 0;
 
 	/*
-	 * For Atom SoCs TSC is the only reliable clocksource.
-	 * Mark TSC reliable so no watchdog on it.
+	 * For Atom SoCs TSC is the only reliable clocksource.  Similarly, in a

arch/x86/kernel/tsc.c unsigned long native_calibrate_tsc(void) · line 730

Quoted 10 lines
arch/x86/kernel/tsc.c +5 -1
+	 * VM, any watchdog is going to be less reliable than the TSC as the
+	 * watchdog source will be emulated in software.  In both cases, mark
+	 * the TSC reliable so that no watchdog runs on it.
 	 */
-	if (boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
+	if (boot_cpu_has(X86_FEATURE_HYPERVISOR) ||
+	    boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 
 #ifdef CONFIG_X86_LOCAL_APIC

It looks all wrong if a function called native_* sprinkles a bunch of "am I a guest" checks. I guess we should split it into VM and native variants.

But yeah, the general direction is ok once we agree on what we do when exactly.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#52 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Sean Christopherson

My strong vote is prefer TSC over kvmclock for sched_clock if the TSC is constant, nonstop, and not marked stable via command line.

So how do we deal with the case here where a malicious HV could set those bits and still tweak the TSC?

You don't. If the hypervisor is malicious, it's game over for non-CoCo guests. The clocksource being untrusted is completely unintersting because the host has full access to VM state.

It's probably game over for SEV and SEV-ES too, e.g. thanks to remapping attacks, lack of robust attestation, and a variety of other avenues a truly malicious hypervisor can exploit to attack the guest.

It's only with SNP and TDX that the clocksource becomes at all interesting.

Quoted 6 lines

IOW, I think Secure TSC and TDX should be the only ones who trust the TSC when in a guest.

If anything, trusting the TSC in a normal guest should at least issue a warning saying that we do use the TSC but there's no 100% guarantee that it is trustworthy...

Why? For non-TDX/SecureTSC guests, *all* clocksources are host controlled.

Quoted 6 lines
Quoted 4 lines

But wait, there's more! Because TDX doesn't override .calibrate_tsc() or .calibrate_cpu(), even though TDX provides a trusted TSC *and* enumerates the frequency of the TSC, unless I'm missing something, tsc_early_init() will compute the TSC frequency using the information provided by KVM, i.e. the untrusted host.

Yeah, I guess we don't want that. Or at least we should warn about it.

CPUID 0x15 (and 0x16?) is guaranteed to be available under TDX, and Secure TSC would ideally assert that the kernel doesn't switch to some other calibration method too. Not sure where to hook into that though, without bleeding TDX and SNP details everywhere.

Quoted 8 lines
Quoted 8 lines
arch/x86/kernel/kvmclock.c +8 -0
+	/*
+	 * If the TSC counts at a constant frequency across P/T states, counts
+	 * in deep C-states, and the TSC hasn't been marked unstable, prefer
+	 * the TSC over kvmclock for sched_clock and drop kvmclock's rating so
+	 * that TSC is chosen as the clocksource.  Note, the TSC unstable check
+	 * exists purely to honor the TSC being marked unstable via command
+	 * line, any runtime detection of an unstable will happen after this.
+	 */
Quoted 8 lines

arch/x86/kernel/kvmclock.c void __init kvmclock_init(void) · line 332

Quoted 5 lines
arch/x86/kernel/kvmclock.c +5 -0
+	if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
+	    boot_cpu_has(X86_FEATURE_NONSTOP_TSC) &&
+	    !check_tsc_unstable()) {
+		kvm_clock.rating = 299;
+		pr_warn("kvm-clock: Using native sched_clock\n");

The warn is in the right direction but probably should say TSC still cannot be trusted 100%.

Heh, I didn't mean to include the printks, they were purely for my own debugging.

Quoted 34 lines
Quoted 34 lines
arch/x86/kernel/tsc.c +12 -4
index 0864b314c26a..9baffb425386 100644
@@ -663,7 +663,12 @@ unsigned long native_calibrate_tsc(void)
 	unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
 	unsigned int crystal_khz;
 
-	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
+	/*
+	 * Ignore the vendor when running as a VM, if the hypervisor provides
+	 * garbage CPUID information then the vendor is also suspect.
+	 */
+	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
+	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		return 0;
 
 	if (boot_cpu_data.cpuid_level < 0x15)
@@ -713,10 +718,13 @@ unsigned long native_calibrate_tsc(void)
 		return 0;
 
 	/*
-	 * For Atom SoCs TSC is the only reliable clocksource.
-	 * Mark TSC reliable so no watchdog on it.
+	 * For Atom SoCs TSC is the only reliable clocksource.  Similarly, in a
+	 * VM, any watchdog is going to be less reliable than the TSC as the
+	 * watchdog source will be emulated in software.  In both cases, mark
+	 * the TSC reliable so that no watchdog runs on it.
 	 */
-	if (boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
+	if (boot_cpu_has(X86_FEATURE_HYPERVISOR) ||
+	    boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 
Quoted 4 lines

arch/x86/kernel/tsc.c unsigned long native_calibrate_tsc(void) · line 730

Quoted 1 line
arch/x86/kernel/tsc.c +0 -0
 #ifdef CONFIG_X86_LOCAL_APIC

It looks all wrong if a function called native_* sprinkles a bunch of "am I a guest" checks. I guess we should split it into VM and native variants.

I agree the naming is weird, but outside of the vendor checks, the VM code is identical to the "native" code, so I don't know that it's worth splitting into multiple functions.

What if we simply rename it to calibrate_tsc_from_cpuid()?

Quoted 2 lines

But yeah, the general direction is ok once we agree on what we do when exactly.

maintainer
#53 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 1 line · Sean Christopherson

It's only with SNP and TDX that the clocksource becomes at all interesting.

So basically you're saying, let's just go ahead and trust the TSC when the HV sets a bunch of CPUID bits.

But we really really trust it when the guest type is SNP+STSC or TDX since there the HV is out of the picture and the only one who can flub it there is the OEM.

Quoted 4 lines

CPUID 0x15 (and 0x16?) is guaranteed to be available under TDX, and Secure TSC would ideally assert that the kernel doesn't switch to some other calibration method too. Not sure where to hook into that though, without bleeding TDX and SNP details everywhere.

We could use the platform calibrate* function pointers and assign TDX- or SNP-specific ones and perhaps even define new such function ptrs. That's what the platform stuff is for... needs staring, ofc.

Quoted 5 lines

I agree the naming is weird, but outside of the vendor checks, the VM code is identical to the "native" code, so I don't know that it's worth splitting into multiple functions.

What if we simply rename it to calibrate_tsc_from_cpuid()?

This is all wrong layering with all those different guest types having their own ->calibrate_tsc:

arch/x86/kernel/cpu/acrn.c:32: x86_platform.calibrate_tsc = acrn_get_tsc_khz;
arch/x86/kernel/cpu/mshyperv.c:424: x86_platform.calibrate_tsc = hv_get_tsc_khz;
arch/x86/kernel/cpu/vmware.c:419: x86_platform.calibrate_tsc = vmware_get_tsc_khz;
arch/x86/kernel/jailhouse.c:213: x86_platform.calibrate_tsc = jailhouse_get_tsc;
arch/x86/kernel/kvmclock.c:323: x86_platform.calibrate_tsc = kvm_get_tsc_khz;
arch/x86/kernel/tsc.c:944: tsc_khz = x86_platform.calibrate_tsc();
arch/x86/kernel/tsc.c:1458: tsc_khz = x86_platform.calibrate_tsc();
arch/x86/kernel/x86_init.c:148: .calibrate_tsc = native_calibrate_tsc,
arch/x86/xen/time.c:569: x86_platform.calibrate_tsc = xen_tsc_khz;

What you want sounds like a redesign to me considering how you want to keep the KVM guest code and baremetal pretty close... Hmmm, needs staring...

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#54 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 5 lines · Borislav Petkov
Quoted 1 line · Sean Christopherson

It's only with SNP and TDX that the clocksource becomes at all interesting.

So basically you're saying, let's just go ahead and trust the TSC when the HV sets a bunch of CPUID bits.

Sort of. It's not a trust thing though. The Xen, KVM, and VMware PV clocks are all based on TSC, i.e. we already "trust" the hypervisor to not muck with TSC.

The purpose of such PV clocks is to account for things in software, that aren't handled in hardware. E.g. to provide a constant counter on hardware without a constant TSC.

The proposal here, and what kvmclock already does for clocksource, is to use the raw TSC when the hypervisor sets bits that effectively say that the massaging of TSC done by the PV clock isn't needed.

Quoted 3 lines

But we really really trust it when the guest type is SNP+STSC or TDX since there the HV is out of the picture and the only one who can flub it there is the OEM.

Yep. This one _is_ about trust. Specifically, we trust the raw TSC more than any clock that is provided by the HV.

Quoted 28 lines
Quoted 4 lines

CPUID 0x15 (and 0x16?) is guaranteed to be available under TDX, and Secure TSC would ideally assert that the kernel doesn't switch to some other calibration method too. Not sure where to hook into that though, without bleeding TDX and SNP details everywhere.

We could use the platform calibrate* function pointers and assign TDX- or SNP-specific ones and perhaps even define new such function ptrs. That's what the platform stuff is for... needs staring, ofc.

Quoted 5 lines

I agree the naming is weird, but outside of the vendor checks, the VM code is identical to the "native" code, so I don't know that it's worth splitting into multiple functions.

What if we simply rename it to calibrate_tsc_from_cpuid()?

This is all wrong layering with all those different guest types having their own ->calibrate_tsc:

arch/x86/kernel/cpu/acrn.c:32: x86_platform.calibrate_tsc = acrn_get_tsc_khz;
arch/x86/kernel/cpu/mshyperv.c:424: x86_platform.calibrate_tsc = hv_get_tsc_khz;
arch/x86/kernel/cpu/vmware.c:419: x86_platform.calibrate_tsc = vmware_get_tsc_khz;
arch/x86/kernel/jailhouse.c:213: x86_platform.calibrate_tsc = jailhouse_get_tsc;
arch/x86/kernel/kvmclock.c:323: x86_platform.calibrate_tsc = kvm_get_tsc_khz;
arch/x86/kernel/tsc.c:944: tsc_khz = x86_platform.calibrate_tsc();
arch/x86/kernel/tsc.c:1458: tsc_khz = x86_platform.calibrate_tsc();
arch/x86/kernel/x86_init.c:148: .calibrate_tsc = native_calibrate_tsc,
arch/x86/xen/time.c:569: x86_platform.calibrate_tsc = xen_tsc_khz;

Quoted 2 lines

What you want sounds like a redesign to me considering how you want to keep the KVM guest code and baremetal pretty close... Hmmm, needs staring...

It's not KVM guest code though. The CPUID stuff is Intel's architecturally defined behavior. There are oodles and oodles of features that are transparently emulated by the hypervisor according to hardware specifications. Generally speaking, the kernel treats those as "native", e.g. native_wrmsrl(), native_cpuid(), etc.

What I am proposing is that, for TDX especially, instead of relying on the hypervisor to use a paravirtual channel for communicating the TSC frequency, we rely on the hardware-defined way of getting the frequency, because CPUID is emulated by the trusted entity, i.e. the OEM.

Hmm, though I suppose I'm arguing against myself in that case. If the hypervisor provides the frequency and there are no trust issues, why would we care if the kernel gets the frequency via CPUID or the PV channel. It's really only TDX that matters. And we could handle TDX by overriding .calibrate_tsc() in tsc_init(), same as Secure TSC.

That said, I do think it makes sense to either override the vendor and F/M/S checks native_calibrate_tsc(). Or even better drop the initial vendor check entirely, because both Intel and AMD have a rich history of implementing each other's CPUID leaves. I.e. I see no reason to ignore CPUID 0x15 just because the CPU isn't Intel.

As for the Goldmost F/M/S check, that one is a virtualization specific thing. The argument is that when running as a guest, any non-TSC clocksource is going to be emulated by the hypervisor, and therefore is going to be less reliable than TSC. I.e. putting a watchdog on TSC does more harm than good, because what ends up happening is the TSC gets marked unreliable because the *watchdog* is unreliable.

maintainer
#55 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

Quoted 10 lines · Sean Christopherson

The proposal here, and what kvmclock already does for clocksource, is to use the raw TSC when the hypervisor sets bits that effectively say that the massaging of TSC done by the PV clock isn't needed.

Quoted 3 lines

But we really really trust it when the guest type is SNP+STSC or TDX since there the HV is out of the picture and the only one who can flub it there is the OEM.

Yep. This one _is_ about trust. Specifically, we trust the raw TSC more than any clock that is provided by the HV.

Ok, makes sense.

Quoted 5 lines

It's not KVM guest code though. The CPUID stuff is Intel's architecturally defined behavior. There are oodles and oodles of features that are transparently emulated by the hypervisor according to hardware specifications. Generally speaking, the kernel treats those as "native", e.g. native_wrmsrl(), native_cpuid(), etc.

Uuuh, this is calling for confusion.

native_* has always been stuff the kernel calls when it runs, well, natively, on baremetal. And not some PV or whatever-enlightened ops.

Now, if you want to emulate those transparently that's fine but this is what native means in arch/x86/. Unless I've missed some memo but I doubt it. And I asked around :)

Quoted 4 lines

What I am proposing is that, for TDX especially, instead of relying on the hypervisor to use a paravirtual channel for communicating the TSC frequency, we rely on the hardware-defined way of getting the frequency, because CPUID is emulated by the trusted entity, i.e. the OEM.

I wouldn't trust the OEM with a single bit but that's a different story. :-P

Quoted 5 lines

Hmm, though I suppose I'm arguing against myself in that case. If the hypervisor provides the frequency and there are no trust issues, why would we care if the kernel gets the frequency via CPUID or the PV channel. It's really only TDX that matters. And we could handle TDX by overriding .calibrate_tsc() in tsc_init(), same as Secure TSC.

I guess it all boils down to the trust level you want to have with the TSC. I don't know whether there's even a HV which tries to lie to the guest with the TSC and I guess we'll find out eventually but for now we could treat the two things similarly. The two things being:

- TDX and STSC SNP guests - full TSC trust
- HV with the required CPUID bits set - almost full, we assume HV is benevolent. Could change if we notice something.

Quoted 3 lines

That said, I do think it makes sense to either override the vendor and F/M/S checks native_calibrate_tsc(). Or even better drop the initial vendor check entirely,

I have no clue why that thing is even there:

aa297292d708 ("x86/tsc: Enumerate SKL cpu_khz and tsc_khz via CPUID")

I guess back then it meant that only Intel sports those new CPUID leafs but those things do change.

Quoted 10 lines

because both Intel and AMD have a rich history of implementing each other's CPUID leaves. I.e. I see no reason to ignore CPUID 0x15 just because the CPU isn't Intel.

As for the Goldmost F/M/S check, that one is a virtualization specific thing. The argument is that when running as a guest, any non-TSC clocksource is going to be emulated by the hypervisor, and therefore is going to be less reliable than TSC. I.e. putting a watchdog on TSC does more harm than good, because what ends up happening is the TSC gets marked unreliable because the *watchdog* is unreliable.

So I think the best thing to do is to carve out the meat of that function which is valid for both baremetal and virtualized and then have separate helpers which call the common thing. So that you don't have to test on *everything* when having to change it in the future for whatever reason and so that both camps can be relatively free when implementing their idiosyncrasies.

And then it should fit in the current scheme with platform function ptrs.

I know you want to use native_calibrate_tsc() but then you have to sprinkle "am I a guest" checks and this reminds me of the "if XEN" fiasco back then. Also, a really nasty lying HV won't simply set X86_FEATURE_HYPERVISOR...

So I'd prefer if we fit a guest which runs on a relatively honest HV :) into the current scheme as the kernel running simply on yet another platform, even at the price of some small code duplication.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#56 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

In reply to Sean Christopherson
Quoted 18 lines · Sean Christopherson

My strong vote is prefer TSC over kvmclock for sched_clock if the TSC is constant, nonstop, and not marked stable via command line. I.e. use the same criteria as tweaking the clocksource rating. As above, sched_clock is more tolerant of slop than clocksource, so it's a bit ridiculous to care whether the TSC or kvmclock (or something else entirely) is used for the clocksource.

If we wanted to go with a more conservative approach, e.g. to minimize the risk of breaking existing setups, we could also condition the change on the TSC being reliable and having a known frequency. I.e. require SNP's Secure TSC, or require the hypervisor to enumerate the TSC frequency via CPUID. I don't see a ton of value in that approach though, and long-term it would lead to some truly weird code due to holding sched_clock to a higher standard than clocksource.

But wait, there's more! Because TDX doesn't override .calibrate_tsc() or .calibrate_cpu(), even though TDX provides a trusted TSC *and* enumerates the frequency of the TSC, unless I'm missing something, tsc_early_init() will compute the TSC frequency using the information provided by KVM, i.e. the untrusted host.

Quoted 7 lines

The "obvious" solution is to leave the calibration functions as-is if the TSC has a known, reliable frequency, but even _that_ is riddled with problems, because as-is, the kernel sets TSC_KNOWN_FREQ and TSC_RELIABLE in tsc_early_init(), which runs *after* init_hypervisor_platform(). SNP Secure TSC fudges around this by overiding the calibration routines, but that's a bit gross and easy to fix if we also fix TDX. And fixing TDX by running native_calibrate_tsc() would give the same love to setups where the hypervisor provides CPUID 0x15 and/or 0x16.

One change that I wasn't sure was about non-Intel guests using CPUID 0x15H/0x16H, that you have already answered in the subsequent emails. At present, AMD platform does not implement either of them and will bail out from the below check:

        /* CPUID 15H TSC/Crystal ratio, plus optionally Crystal Hz */
        cpuid(CPUID_LEAF_TSC, &eax_denominator, &ebx_numerator, &ecx_hz, &edx);

        if (ebx_numerator == 0 || eax_denominator == 0)
                return 0;
Quoted 1 line

All in all, I'm thinking something like this (across multiple patches):

Tested on AMD Milan and changes are working as intended:

For SecureTSC guests with TSC_INVARIANT set:
* Raw TSC is used as clocksource and sched-clock
* Calibration is done using GUEST_TSC_FREQ MSR

For non-SecureTSC guests with TSC_INVARIANT set:
* Raw TSC is used as clocksource and sched-clock
* Calibration is done using kvm-clock

For non-SecureTSC guests without TSC_INVARIANT:
* kvm-clock(based on TSC) is used as clocksource and sched-clock
* Calibration is done using kvm-clock

Quoted 8 lines
arch/x86/kernel/tsc.c +0 -0
index 0864b314c26a..9baffb425386 100644
@@ -663,7 +663,12 @@ unsigned long native_calibrate_tsc(void)
 	unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
 	unsigned int crystal_khz;
 

arch/x86/kernel/tsc.c unsigned long native_calibrate_tsc(void) · line 674

Quoted 10 lines
arch/x86/kernel/tsc.c +6 -1
-	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
+	/*
+	 * Ignore the vendor when running as a VM, if the hypervisor provides
+	 * garbage CPUID information then the vendor is also suspect.
+	 */
+	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
+	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		return 0;
 
 	if (boot_cpu_data.cpuid_level < 0x15)

Regards
Nikunj

#57 lore raw

Re: [PATCH v16 12/13] x86/tsc: Switch to native sched clock

In reply to Sean Christopherson
Quoted 24 lines · Sean Christopherson
arch/x86/kernel/tsc.c +6 -2
index 0864b314c26a..9baffb425386 100644
@@ -663,7 +663,12 @@ unsigned long native_calibrate_tsc(void)
 	unsigned int eax_denominator, ebx_numerator, ecx_hz, edx;
 	unsigned int crystal_khz;
 
-	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
+	/*
+	 * Ignore the vendor when running as a VM, if the hypervisor provides
+	 * garbage CPUID information then the vendor is also suspect.
+	 */
+	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
+	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		return 0;
 
 	if (boot_cpu_data.cpuid_level < 0x15)
@@ -713,10 +718,13 @@ unsigned long native_calibrate_tsc(void)
 		return 0;
 
 	/*
-	 * For Atom SoCs TSC is the only reliable clocksource.

arch/x86/kernel/tsc.c unsigned long native_calibrate_tsc(void) · line 728

Quoted 10 lines
arch/x86/kernel/tsc.c +6 -2
-	 * Mark TSC reliable so no watchdog on it.
+	 * For Atom SoCs TSC is the only reliable clocksource.  Similarly, in a
+	 * VM, any watchdog is going to be less reliable than the TSC as the
+	 * watchdog source will be emulated in software.  In both cases, mark
+	 * the TSC reliable so that no watchdog runs on it.
 	 */
-	if (boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
+	if (boot_cpu_has(X86_FEATURE_HYPERVISOR) ||
+	    boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);

One more point here is for AMD guests, TSC will not be marked reliable as per the above change, it will only be effective for CPUs supporting CPUID 15H/16H. Although, the watchdog should be stopped for AMD guests as well.

Will it make sense to move this before cpuid_level check ?

arch/x86/kernel/tsc.c +11 -6
index e7abcc4d02c3..2769d1598c0d 100644
@@ -672,6 +672,14 @@ unsigned long native_calibrate_tsc(void)
 	    !boot_cpu_has(X86_FEATURE_HYPERVISOR))
 		return 0;
 
+	/*
+	 * In a VM, any watchdog is going to be less reliable than the TSC as
+	 * the watchdog source will be emulated in software. Mark the TSC
+	 * reliable so that no watchdog runs on it.
+	 */
+	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
+		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
+
 	if (boot_cpu_data.cpuid_level < CPUID_LEAF_TSC)
 		return 0;
 
@@ -719,13 +727,10 @@ unsigned long native_calibrate_tsc(void)
 		return 0;
 
 	/*
-	 * For Atom SoCs TSC is the only reliable clocksource.  Similarly, in a
-	 * VM, any watchdog is going to be less reliable than the TSC as the
-	 * watchdog source will be emulated in software.  In both cases, mark
-	 * the TSC reliable so that no watchdog runs on it.
+	 * For Atom SoCs TSC is the only reliable clocksource.
+	 * Mark TSC reliable so no watchdog on it.
 	 */
-	if (boot_cpu_has(X86_FEATURE_HYPERVISOR) ||
-	    boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
+	if (boot_cpu_data.x86_vfm == INTEL_ATOM_GOLDMONT)
 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
 
 #ifdef CONFIG_X86_LOCAL_APIC

Regards
Nikunj

#58 lore raw

PATCH v16 13/13 x86/sev: Allow Secure TSC feature for SNP guests

In reply to Nikunj A Dadhania

Now that all the required plumbing is done for enabling SNP Secure TSC feature, add Secure TSC to SNP features present list.

---
 arch/x86/boot/compressed/sev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/sev.c +2 -1
index cd44e120fe53..bb55934c1cee 100644
@@ -401,7 +401,8 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
  * by the guest kernel. As and when a new feature is implemented in the
  * guest kernel, a corresponding bit should be added to the mask.
  */
-#define SNP_FEATURES_PRESENT	MSR_AMD64_SNP_DEBUG_SWAP
+#define SNP_FEATURES_PRESENT	(MSR_AMD64_SNP_DEBUG_SWAP |	\
+				 MSR_AMD64_SNP_SECURE_TSC)
 
 u64 snp_get_unsupported_features(u64 status)
 {
Signature · 1 line
2.34.1
#59 lore raw

[tip: x86/sev] x86/sev: Add the Secure TSC feature for SNP guests

The following commit has been merged into the x86/sev branch of tip:

The following commit has been merged into the x86/sev branch of tip:

Commit-ID:     0563ee35ae2c9cfb0c6a7b2c0ddf7d9372bb8a98
Gitweb:        https://git.kernel.org/tip/0563ee35ae2c9cfb0c6a7b2c0ddf7d9372bb8a98
Author:        Nikunj A Dadhania <[email protected]>
AuthorDate:    Mon, 06 Jan 2025 18:16:33 +05:30
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Thu, 09 Jan 2025 10:21:56 +01:00

x86/sev: Add the Secure TSC feature for SNP guests

Now that all the required plumbing is done for enabling Secure TSC, add it to the SNP features present list.

---
 arch/x86/boot/compressed/sev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/boot/compressed/sev.c +2 -1
index cd44e12..bb55934 100644
@@ -401,7 +401,8 @@ finish:
  * by the guest kernel. As and when a new feature is implemented in the
  * guest kernel, a corresponding bit should be added to the mask.
  */
-#define SNP_FEATURES_PRESENT	MSR_AMD64_SNP_DEBUG_SWAP
+#define SNP_FEATURES_PRESENT	(MSR_AMD64_SNP_DEBUG_SWAP |	\
+				 MSR_AMD64_SNP_SECURE_TSC)
 
 u64 snp_get_unsupported_features(u64 status)
 {