Skip to contentSkip to conversation

Add throttling detection to sev-guest

PATCH Superseded Dionna Glaze 3 participants 13 messages Oct 25, 2022 – Oct 28, 2022

Subsystems (4)

v4 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add throttling detection to sev-guest

The guest request synchronous API from SEV-SNP VMs to the host's security processor consumes a global resource. For this reason, AMD's docs recommend that the host implements a throttling mechanism. In order for the guest to know it's been throttled and should try its request again, we need some good-faith communication from the host that the request has been throttled.

These patches work with the existing /dev/sev-guest ABI to detect a throttling code.

Changes from v3:
* sev-guest ratelimits itself to one request twice a second.
* Fixed a type signature to use u64 instead of unsigned int
* Set *exitinfo2 unconditionally after the ghcb_hv_call.
Changes from v2:
* Codified the non-firmware-call firmware error code as (u32)-1.
* Changed sev_issue_guest_request unsigned long *fw_err argument to u64 *exitinfo2 to more accurately and type-safely describe the value that it outputs.
* Changed sev_issue_guest_request to always set its exitinfo2 argument to either the non-firmware-call error code, the EXIT_INFO_2 returned from the VMM if the request failed, or 0 on success. This fixes a bug that returned uninitialized kernel stack memory to the user when there is no error.
* Changed the throttle behavior to retry in the driver instead of returning -EAGAIN, due to possible message sequence number reuse on different message contents.

Changes from v1:
* Changed throttle error code to 2

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>

Dionna Glaze (4):
ccp: Name -1 return value as SEV_RET_NO_FW_CALL x86/sev: Change snp_guest_issue_request's fw_err virt/coco/sev-guest: Remove err in handle_guest_request virt/coco/sev-guest: interpret VMM errors from guest request

 arch/x86/include/asm/sev.h              |  4 +-
 arch/x86/kernel/sev.c                   | 10 ++--
 drivers/crypto/ccp/sev-dev.c            |  2 +-
 drivers/virt/coco/sev-guest/sev-guest.c | 61 ++++++++++++++++++++-----
 include/uapi/linux/psp-sev.h            |  7 +++
 include/uapi/linux/sev-guest.h          | 18 +++++++-
 6 files changed, 82 insertions(+), 20 deletions(-)
Signature · 1 line
2.38.0.135.g90850a2211-goog
#1 lore raw

PATCH v4 1/4 ccp: Name -1 return value as SEV_RET_NO_FW_CALL

The PSP can return a "firmware error" code of -1 in circumstances where the PSP is not actually called. To make this protocol unambiguous, we add a constant naming the return value.

From: Peter Gonda <[email protected]>
Cc: Thomas Lendacky <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Joerg Roedel <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Andy Lutomirsky <[email protected]>

---
 drivers/crypto/ccp/sev-dev.c | 2 +-
 include/uapi/linux/psp-sev.h | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +1 -1
index 06fc7156c04f..97eb3544ab36 100644
@@ -444,7 +444,7 @@ static int __sev_platform_init_locked(int *error)
 {
 	struct psp_device *psp = psp_master;
 	struct sev_device *sev;
-	int rc = 0, psp_ret = -1;
+	int rc = 0, psp_ret = SEV_RET_NO_FW_CALL;
 	int (*init_function)(int *error);
 
 	if (!psp || !psp->sev_data)
include/uapi/linux/psp-sev.h +7 -0
index 91b4c63d5cbf..fb61e083d42e 100644
@@ -36,6 +36,13 @@ enum {
  * SEV Firmware status code
  */
 typedef enum {
+	/*
+	 * This error code is not in the SEV spec but is added to convey that
+	 * there was an error that prevented the SEV Firmware from being called.
+	 * This is (u32)-1 since the firmware error code part of EXIT_INFO_2 is
+	 * the lower 32 bits.
+	 */
+	SEV_RET_NO_FW_CALL = 0xffffffff,
 	SEV_RET_SUCCESS = 0,
 	SEV_RET_INVALID_PLATFORM_STATE,
 	SEV_RET_INVALID_GUEST_STATE,
Signature · 1 line
2.38.0.135.g90850a2211-goog
maintainer Reviewed-by on v4 1/4
#2 lore raw

Re: [PATCH v4 1/4] ccp: Name -1 return value as SEV_RET_NO_FW_CALL

Quoted 4 lines · Dionna Glaze

The PSP can return a "firmware error" code of -1 in circumstances where the PSP is not actually called. To make this protocol unambiguous, we add a constant naming the return value.

Quoted 8 lines

From: Peter Gonda <[email protected]>
Cc: Thomas Lendacky <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Joerg Roedel <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Andy Lutomirsky <[email protected]>

Just one minor comment below, otherwise...

Quoted 23 lines
---
  drivers/crypto/ccp/sev-dev.c | 2 +-
  include/uapi/linux/psp-sev.h | 7 +++++++
  2 files changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +1 -1
index 06fc7156c04f..97eb3544ab36 100644
@@ -444,7 +444,7 @@ static int __sev_platform_init_locked(int *error)
  {
  	struct psp_device *psp = psp_master;
  	struct sev_device *sev;
-	int rc = 0, psp_ret = -1;
+	int rc = 0, psp_ret = SEV_RET_NO_FW_CALL;
  	int (*init_function)(int *error);
 
  	if (!psp || !psp->sev_data)
include/uapi/linux/psp-sev.h +0 -0
index 91b4c63d5cbf..fb61e083d42e 100644

include/uapi/linux/psp-sev.h enum { · line 43

Quoted 8 lines
include/uapi/linux/psp-sev.h +5 -0
@@ -36,6 +36,13 @@ enum {
   * SEV Firmware status code
   */
  typedef enum {
+	/*
+	 * This error code is not in the SEV spec but is added to convey that
+	 * there was an error that prevented the SEV Firmware from being called.
+	 * This is (u32)-1 since the firmware error code part of EXIT_INFO_2 is
+	 * the lower 32 bits.

These codes are used for /dev/sev access on the HV side, too, where EXIT_INFO_2 doesn't exist. I would change this to something like:

This is (u32)-1 since the firwmare error code is represented as a 32-bit integer.

Thanks,
Tom

Quoted 5 lines
include/uapi/linux/psp-sev.h +2 -0
+	 */
+	SEV_RET_NO_FW_CALL = 0xffffffff,
  	SEV_RET_SUCCESS = 0,
  	SEV_RET_INVALID_PLATFORM_STATE,
  	SEV_RET_INVALID_GUEST_STATE,
#3 lore raw

PATCH v4 2/4 x86/sev: Change snp_guest_issue_request's fw_err

In reply to Dionna Glaze

The GHCB specification declares that the firmware error value for a guest request will be stored in the lower 32 bits of EXIT_INFO_2. The upper 32 bits are for the VMM's own error code. The fw_err argument is thus a misnomer, and callers will need access to all 64 bits.

The type of unsigned long also causes problems, since sw_exit_info2 is u64 (unsigned long long) vs the argument's previous unsigned long*. The signature change requires the follow-up change to drivers/virt/coco/sev-guest to use the new expected type in order to compile.

The firmware might not even be called, so we bookend the call with the no firmware call error and clearing the error.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>
Fixes d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs")
---
 arch/x86/include/asm/sev.h |  4 ++--
 arch/x86/kernel/sev.c      | 10 ++++++----
 2 files changed, 8 insertions(+), 6 deletions(-)
arch/x86/include/asm/sev.h +2 -2
index ebc271bb6d8e..8ebd78b6a57c 100644
@@ -196,7 +196,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages);
 void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __init __noreturn snp_abort(void);
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err);
+int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, u64 *exitinfo2);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
@@ -217,7 +217,7 @@ 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 int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input,
-					  unsigned long *fw_err)
+					  u64 *exitinfo2)
 {
 	return -ENOTTY;
 }
arch/x86/kernel/sev.c +6 -4
index a428c62330d3..148f17cb07b5 100644
@@ -22,6 +22,7 @@
 #include <linux/efi.h>
 #include <linux/platform_device.h>
 #include <linux/io.h>
+#include <linux/psp-sev.h>
 
 #include <asm/cpu_entry_area.h>
 #include <asm/stacktrace.h>
@@ -2175,7 +2176,7 @@ static int __init init_sev_config(char *str)
 }
 __setup("sev=", init_sev_config);
 
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err)
+int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, u64 *exitinfo2)
 {
 	struct ghcb_state state;
 	struct es_em_ctxt ctxt;
@@ -2186,9 +2187,11 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned
 	if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
 		return -ENODEV;
 
-	if (!fw_err)
+	if (!exitinfo2)
 		return -EINVAL;
 
+	*exitinfo2 = SEV_RET_NO_FW_CALL;
+
 	/*
 	 * __sev_get_ghcb() needs to run with IRQs disabled because it is using
 	 * a per-CPU GHCB.
@@ -2212,14 +2215,13 @@ int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned
 	if (ret)
 		goto e_put;
 
+	*exitinfo2 = ghcb->save.sw_exit_info_2;
 	if (ghcb->save.sw_exit_info_2) {
 		/* Number of expected pages are returned in RBX */
 		if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST &&
 		    ghcb->save.sw_exit_info_2 == SNP_GUEST_REQ_INVALID_LEN)
 			input->data_npages = ghcb_get_rbx(ghcb);
 
-		*fw_err = ghcb->save.sw_exit_info_2;
-
 		ret = -EIO;
 	}
 
Signature · 1 line
2.38.0.135.g90850a2211-goog
#4 lore raw

Re: [PATCH v4 2/4] x86/sev: Change snp_guest_issue_request's fw_err

Quoted 43 lines · Dionna Glaze

The GHCB specification declares that the firmware error value for a guest request will be stored in the lower 32 bits of EXIT_INFO_2. The upper 32 bits are for the VMM's own error code. The fw_err argument is thus a misnomer, and callers will need access to all 64 bits.

The type of unsigned long also causes problems, since sw_exit_info2 is u64 (unsigned long long) vs the argument's previous unsigned long*. The signature change requires the follow-up change to drivers/virt/coco/sev-guest to use the new expected type in order to compile.

The firmware might not even be called, so we bookend the call with the no firmware call error and clearing the error.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>
Fixes d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs")
---
 arch/x86/include/asm/sev.h |  4 ++--
 arch/x86/kernel/sev.c      | 10 ++++++----
 2 files changed, 8 insertions(+), 6 deletions(-)
arch/x86/include/asm/sev.h +1 -1
index ebc271bb6d8e..8ebd78b6a57c 100644
@@ -196,7 +196,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages);
 void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __init __noreturn snp_abort(void);
-int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err);
+int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, u64 *exitinfo2);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }

arch/x86/include/asm/sev.h static inline void snp_set_wakeup_secondary_cpu(void) { } · line 223

Quoted 8 lines
arch/x86/include/asm/sev.h +1 -1
@@ -217,7 +217,7 @@ 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 int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input,
-                                         unsigned long *fw_err)
+                                         u64 *exitinfo2)
 {
        return -ENOTTY;
 }

In another thread Borislav suggested we edit snp_issue_guest_request() to take a parameter struct pointer instead of a long list of args. Should we use the opportunity to do this instead of making this list longer?

#5 lore raw

Re: [PATCH v4 2/4] x86/sev: Change snp_guest_issue_request's fw_err

Quoted 5 lines

In another thread Borislav suggested we edit snp_issue_guest_request() to take a parameter struct pointer instead of a long list of args. Should we use the opportunity to do this instead of making this list longer?

I'm just renaming and changing the type of an argument. If you'd like a struct argument cleanup patch first, I can do that.

Signature · 1 line
-Dionna Glaze, PhD (she/her)
maintainer Reviewed-by on v4 2/4
#6 lore raw

Re: [PATCH v4 2/4] x86/sev: Change snp_guest_issue_request's fw_err

In reply to Dionna Glaze
Quoted 21 lines · Dionna Glaze

The GHCB specification declares that the firmware error value for a guest request will be stored in the lower 32 bits of EXIT_INFO_2. The upper 32 bits are for the VMM's own error code. The fw_err argument is thus a misnomer, and callers will need access to all 64 bits.

The type of unsigned long also causes problems, since sw_exit_info2 is u64 (unsigned long long) vs the argument's previous unsigned long*. The signature change requires the follow-up change to drivers/virt/coco/sev-guest to use the new expected type in order to compile.

The firmware might not even be called, so we bookend the call with the no firmware call error and clearing the error.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>
Quoted 2 lines
Fixes d5af44dde546 ("x86/sev: Provide support for SNP guest request NAEs")
Quoted 5 lines
---
  arch/x86/include/asm/sev.h |  4 ++--
  arch/x86/kernel/sev.c      | 10 ++++++----
  2 files changed, 8 insertions(+), 6 deletions(-)
#7 lore raw

PATCH v4 3/4 virt/coco/sev-guest: Remove err in handle_guest_request

In reply to Dionna Glaze

The err variable may not be set in the call to snp_issue_guest_request, yet it is unconditionally written back to fw_err if fw_err is non-null. This is undefined behavior, and currently returns uninitialized kernel stack memory to user space.

The fw_err argument is better to just pass through to snp_issue_guest_request, so we do that. Since the issue_request's signature has changed fw_err to exitinfo2, we change the argument name here.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>
Fixes fce96cf04430 ("virt: Add SEV-SNP guest driver")
---
 drivers/virt/coco/sev-guest/sev-guest.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +2 -5
index f422f9c58ba7..0508c2f46f6b 100644
@@ -303,9 +303,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8
 
 static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, int msg_ver,
 				u8 type, void *req_buf, size_t req_sz, void *resp_buf,
-				u32 resp_sz, __u64 *fw_err)
+				u32 resp_sz, __u64 *exitinfo2)
 {
-	unsigned long err;
 	u64 seqno;
 	int rc;
 
@@ -322,9 +321,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 		return rc;
 
 	/* Call firmware to process the request */
-	rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
-	if (fw_err)
-		*fw_err = err;
+	rc = snp_issue_guest_request(exit_code, &snp_dev->input, exitinfo2);
 
 	if (rc)
 		return rc;
Signature · 1 line
2.38.0.135.g90850a2211-goog
maintainer Reviewed-by on v4 3/4
#8 lore raw

Re: [PATCH v4 3/4] virt/coco/sev-guest: Remove err in handle_guest_request

Quoted 17 lines · Dionna Glaze

The err variable may not be set in the call to snp_issue_guest_request, yet it is unconditionally written back to fw_err if fw_err is non-null. This is undefined behavior, and currently returns uninitialized kernel stack memory to user space.

The fw_err argument is better to just pass through to snp_issue_guest_request, so we do that. Since the issue_request's signature has changed fw_err to exitinfo2, we change the argument name here.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>
Quoted 2 lines
Fixes fce96cf04430 ("virt: Add SEV-SNP guest driver")
Quoted 30 lines
---
  drivers/virt/coco/sev-guest/sev-guest.c | 7 ++-----
  1 file changed, 2 insertions(+), 5 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +2 -5
index f422f9c58ba7..0508c2f46f6b 100644
@@ -303,9 +303,8 @@ static int enc_payload(struct snp_guest_dev *snp_dev, u64 seqno, int version, u8
 
  static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, int msg_ver,
  				u8 type, void *req_buf, size_t req_sz, void *resp_buf,
-				u32 resp_sz, __u64 *fw_err)
+				u32 resp_sz, __u64 *exitinfo2)
  {
-	unsigned long err;
  	u64 seqno;
  	int rc;
 
@@ -322,9 +321,7 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
  		return rc;
 
  	/* Call firmware to process the request */
-	rc = snp_issue_guest_request(exit_code, &snp_dev->input, &err);
-	if (fw_err)
-		*fw_err = err;
+	rc = snp_issue_guest_request(exit_code, &snp_dev->input, exitinfo2);
 
  	if (rc)
  		return rc;
#9 lore raw

PATCH v4 4/4 virt/coco/sev-guest: interpret VMM errors from guest request

In reply to Dionna Glaze

The GHCB specification states that the upper 32 bits of exitinfo2 are for the VMM's error codes. The sev-guest ABI has already locked in that the fw_err status of the input will be 64 bits, and that BIT_ULL(32) means that the extended guest request's data buffer was too small, so we have to keep that ABI.

We can still interpret the upper 32 bits of exitinfo2 for the user anyway in case the request gets throttled. For safety, since the encryption algorithm in GHCBv2 is AES_GCM, we cannot return to user space without having completed the request with the current sequence number. If we were to return and the guest were to make another request but with different message contents, then that would be IV reuse.

When throttled, the driver will reschedule itself and then try again. The ioctl may block indefinitely, but that has always been the case when deferring these requests to the host.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>
---
 drivers/virt/coco/sev-guest/sev-guest.c | 54 ++++++++++++++++++++++---
 include/uapi/linux/sev-guest.h          | 18 ++++++++-
 2 files changed, 64 insertions(+), 8 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +48 -6
index 0508c2f46f6b..dfde777933b6 100644
@@ -14,6 +14,7 @@
 #include <linux/io.h>
 #include <linux/platform_device.h>
 #include <linux/miscdevice.h>
+#include <linux/ratelimit.h>
 #include <linux/set_memory.h>
 #include <linux/fs.h>
 #include <crypto/aead.h>
@@ -48,12 +49,22 @@ struct snp_guest_dev {
 	struct snp_req_data input;
 	u32 *os_area_msg_seqno;
 	u8 *vmpck;
+
+	struct ratelimit_state rs;
 };
 
 static u32 vmpck_id;
 module_param(vmpck_id, uint, 0444);
 MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");
 
+static int rate_hz = 2;
+module_param(rate_hz, uint, 0444);
+MODULE_PARM_DESC(vmpck_id, "The rate limit frequency to limit requests to.");
+
+static int rate_burst = 1;
+module_param(rate_burst, uint, 0444);
+MODULE_PARM_DESC(rate_burst, "The rate limit burst amount to limit requests to.");
+
 /* Mutex to serialize the shared buffer access and command handling. */
 static DEFINE_MUTEX(snp_cmd_mutex);
 
@@ -305,9 +316,12 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 				u8 type, void *req_buf, size_t req_sz, void *resp_buf,
 				u32 resp_sz, __u64 *exitinfo2)
 {
+	unsigned int vmm_err;
 	u64 seqno;
 	int rc;
 
+	might_resched();
+
 	/* Get message sequence and verify that its a non-zero */
 	seqno = snp_get_msg_seqno(snp_dev);
 	if (!seqno)
@@ -320,9 +334,35 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
 	if (rc)
 		return rc;
 
+retry:
+	/*
+	 * Rate limit commands internally since the host can also throttle, and
+	 * we don't want to create a tight request spin that could end up
+	 * getting this VM throttled more heavily.
+	 */
+	if (!__ratelimit(&snp_dev->rs)) {
+		cond_resched();
+		goto retry;
+	}
 	/* Call firmware to process the request */
 	rc = snp_issue_guest_request(exit_code, &snp_dev->input, exitinfo2);
 
+	vmm_err = *exitinfo2 >> SNP_GUEST_VMM_ERR_SHIFT;
+	/*
+	 * The host may return EBUSY if the request has been throttled.
+	 * We retry in the driver to avoid returning and reusing the message
+	 * sequence number on a different message.
+	 */
+	if (vmm_err == SNP_GUEST_VMM_ERR_BUSY) {
+		cond_resched();
+		goto retry;
+	}
+
+	if (vmm_err && vmm_err != SNP_GUEST_VMM_ERR_INVALID_LEN) {
+		pr_err("sev-guest: host returned unknown error code: %d\n",
+		       vmm_err);
+		return -EINVAL;
+	}
 	if (rc)
 		return rc;
 
@@ -375,7 +415,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io
 
 	rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version,
 				  SNP_MSG_REPORT_REQ, &req, sizeof(req), resp->data,
-				  resp_len, &arg->fw_err);
+				  resp_len, &arg->exitinfo2);
 	if (rc)
 		goto e_free;
 
@@ -415,7 +455,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque
 
 	rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version,
 				  SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len,
-				  &arg->fw_err);
+				  &arg->exitinfo2);
 	if (rc)
 		return rc;
 
@@ -477,10 +517,10 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
 	snp_dev->input.data_npages = npages;
 	ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg->msg_version,
 				   SNP_MSG_REPORT_REQ, &req.data,
-				   sizeof(req.data), resp->data, resp_len, &arg->fw_err);
+				   sizeof(req.data), resp->data, resp_len, &arg->exitinfo2);
 
 	/* If certs length is invalid then copy the returned length */
-	if (arg->fw_err == SNP_GUEST_REQ_INVALID_LEN) {
+	if (arg->vmm_error == SNP_GUEST_VMM_ERR_INVALID_LEN) {
 		req.certs_len = snp_dev->input.data_npages << PAGE_SHIFT;
 
 		if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req)))
@@ -515,7 +555,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
 	if (copy_from_user(&input, argp, sizeof(input)))
 		return -EFAULT;
 
-	input.fw_err = 0xff;
+	input.exitinfo2 = SEV_RET_NO_FW_CALL;
 
 	/* Message version must be non-zero */
 	if (!input.msg_version)
@@ -546,7 +586,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
 
 	mutex_unlock(&snp_cmd_mutex);
 
-	if (input.fw_err && copy_to_user(argp, &input, sizeof(input)))
+	if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input)))
 		return -EFAULT;
 
 	return ret;
@@ -696,6 +736,8 @@ static int __init sev_guest_probe(struct platform_device *pdev)
 	if (ret)
 		goto e_free_cert_data;
 
+	ratelimit_state_init(&snp_dev->rs, rate_hz * HZ, rate_burst);
+
 	dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id);
 	return 0;
 
include/uapi/linux/sev-guest.h +16 -2
index 256aaeff7e65..8e4144aa78c9 100644
@@ -52,8 +52,15 @@ struct snp_guest_request_ioctl {
 	__u64 req_data;
 	__u64 resp_data;
 
-	/* firmware error code on failure (see psp-sev.h) */
-	__u64 fw_err;
+	/* bits[63:32]: VMM error code, bits[31:0] firmware error code (see psp-sev.h) */
+	union {
+		__u64 exitinfo2;
+		__u64 fw_err; /* Name deprecated in favor of others */
+		struct {
+			__u32 fw_error;
+			__u32 vmm_error;
+		};
+	};
 };
 
 struct snp_ext_report_req {
@@ -77,4 +84,11 @@ struct snp_ext_report_req {
 /* Get SNP extended report as defined in the GHCB specification version 2. */
 #define SNP_GET_EXT_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x2, struct snp_guest_request_ioctl)
 
+/* Guest message request EXIT_INFO_2 constants */
+#define SNP_GUEST_FW_ERR_MASK		GENMASK_ULL(31, 0)
+#define SNP_GUEST_VMM_ERR_SHIFT		32
+
+#define SNP_GUEST_VMM_ERR_INVALID_LEN	1
+#define SNP_GUEST_VMM_ERR_BUSY		2
+
 #endif /* __UAPI_LINUX_SEV_GUEST_H_ */
Signature · 1 line
2.38.0.135.g90850a2211-goog
maintainer
#10 lore raw

Re: [PATCH v4 4/4] virt/coco/sev-guest: interpret VMM errors from guest request

Quoted 45 lines · Dionna Glaze

The GHCB specification states that the upper 32 bits of exitinfo2 are for the VMM's error codes. The sev-guest ABI has already locked in that the fw_err status of the input will be 64 bits, and that BIT_ULL(32) means that the extended guest request's data buffer was too small, so we have to keep that ABI.

We can still interpret the upper 32 bits of exitinfo2 for the user anyway in case the request gets throttled. For safety, since the encryption algorithm in GHCBv2 is AES_GCM, we cannot return to user space without having completed the request with the current sequence number. If we were to return and the guest were to make another request but with different message contents, then that would be IV reuse.

When throttled, the driver will reschedule itself and then try again. The ioctl may block indefinitely, but that has always been the case when deferring these requests to the host.

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>
---
  drivers/virt/coco/sev-guest/sev-guest.c | 54 ++++++++++++++++++++++---
  include/uapi/linux/sev-guest.h          | 18 ++++++++-
  2 files changed, 64 insertions(+), 8 deletions(-)
drivers/virt/coco/sev-guest/sev-guest.c +1 -0
index 0508c2f46f6b..dfde777933b6 100644
@@ -14,6 +14,7 @@
  #include <linux/io.h>
  #include <linux/platform_device.h>
  #include <linux/miscdevice.h>
+#include <linux/ratelimit.h>
  #include <linux/set_memory.h>
  #include <linux/fs.h>
  #include <crypto/aead.h>
@@ -48,12 +49,22 @@ struct snp_guest_dev {
  	struct snp_req_data input;
  	u32 *os_area_msg_seqno;

drivers/virt/coco/sev-guest/sev-guest.c struct snp_guest_dev { · line 60

Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +3 -0
  	u8 *vmpck;
+
+	struct ratelimit_state rs;
  };
 
  static u32 vmpck_id;
  module_param(vmpck_id, uint, 0444);
  MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");
 
+static int rate_hz = 2;

s/int/unsigned int/ to match uint below.

drivers/virt/coco/sev-guest/sev-guest.c struct snp_guest_dev { · line 64

Quoted 4 lines
drivers/virt/coco/sev-guest/sev-guest.c +4 -0
+module_param(rate_hz, uint, 0444);
+MODULE_PARM_DESC(vmpck_id, "The rate limit frequency to limit requests to.");
+
+static int rate_burst = 1;

s/int/unsigned int/ to match uint below.

Quoted 7 lines
drivers/virt/coco/sev-guest/sev-guest.c +3 -0
+module_param(rate_burst, uint, 0444);
+MODULE_PARM_DESC(rate_burst, "The rate limit burst amount to limit requests to.");
+
  /* Mutex to serialize the shared buffer access and command handling. */
  static DEFINE_MUTEX(snp_cmd_mutex);
 

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 324

Quoted 9 lines
drivers/virt/coco/sev-guest/sev-guest.c +3 -0
@@ -305,9 +316,12 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
  				u8 type, void *req_buf, size_t req_sz, void *resp_buf,
  				u32 resp_sz, __u64 *exitinfo2)
  {
+	unsigned int vmm_err;
  	u64 seqno;
  	int rc;
 
+	might_resched();
+

Not sure this is needed. This may call cond_resched() right away, so I don't think this is doing what you think it is (as I only see it used in scheduler code and spinlock code). Did you mean to use might_sleep()?

Quoted 4 lines
drivers/virt/coco/sev-guest/sev-guest.c +0 -0
  	/* Get message sequence and verify that its a non-zero */
  	seqno = snp_get_msg_seqno(snp_dev);
  	if (!seqno)

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 343

Quoted 10 lines
drivers/virt/coco/sev-guest/sev-guest.c +7 -0
@@ -320,9 +334,35 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
  	if (rc)
  		return rc;
 
+retry:
+	/*
+	 * Rate limit commands internally since the host can also throttle, and
+	 * we don't want to create a tight request spin that could end up
+	 * getting this VM throttled more heavily.
+	 */
+	if (!__ratelimit(&snp_dev->rs)) {

This doesn't do any sleeping/delaying, it just returns a 0 or 1, so you could still cause a tight spin here. I guess that shouldn't be a problem is nothing else is ready to run. But maybe adding an msleep()/usleep() here based on the rate limit parameters (half the rate limit?) would be good so that the CPU isn't pegged while rating for the ratelimit to be satisified?

I'll let others chime in on that and see if it is appropriate.

Thanks,
Tom

Quoted 117 lines
drivers/virt/coco/sev-guest/sev-guest.c +27 -6
+		cond_resched();
+		goto retry;
+	}
  	/* Call firmware to process the request */
  	rc = snp_issue_guest_request(exit_code, &snp_dev->input, exitinfo2);
 
+	vmm_err = *exitinfo2 >> SNP_GUEST_VMM_ERR_SHIFT;
+	/*
+	 * The host may return EBUSY if the request has been throttled.
+	 * We retry in the driver to avoid returning and reusing the message
+	 * sequence number on a different message.
+	 */
+	if (vmm_err == SNP_GUEST_VMM_ERR_BUSY) {
+		cond_resched();
+		goto retry;
+	}
+
+	if (vmm_err && vmm_err != SNP_GUEST_VMM_ERR_INVALID_LEN) {
+		pr_err("sev-guest: host returned unknown error code: %d\n",
+		       vmm_err);
+		return -EINVAL;
+	}
  	if (rc)
  		return rc;
 
@@ -375,7 +415,7 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io
 
  	rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version,
  				  SNP_MSG_REPORT_REQ, &req, sizeof(req), resp->data,
-				  resp_len, &arg->fw_err);
+				  resp_len, &arg->exitinfo2);
  	if (rc)
  		goto e_free;
 
@@ -415,7 +455,7 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque
 
  	rc = handle_guest_request(snp_dev, SVM_VMGEXIT_GUEST_REQUEST, arg->msg_version,
  				  SNP_MSG_KEY_REQ, &req, sizeof(req), buf, resp_len,
-				  &arg->fw_err);
+				  &arg->exitinfo2);
  	if (rc)
  		return rc;
 
@@ -477,10 +517,10 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
  	snp_dev->input.data_npages = npages;
  	ret = handle_guest_request(snp_dev, SVM_VMGEXIT_EXT_GUEST_REQUEST, arg->msg_version,
  				   SNP_MSG_REPORT_REQ, &req.data,
-				   sizeof(req.data), resp->data, resp_len, &arg->fw_err);
+				   sizeof(req.data), resp->data, resp_len, &arg->exitinfo2);
 
  	/* If certs length is invalid then copy the returned length */
-	if (arg->fw_err == SNP_GUEST_REQ_INVALID_LEN) {
+	if (arg->vmm_error == SNP_GUEST_VMM_ERR_INVALID_LEN) {
  		req.certs_len = snp_dev->input.data_npages << PAGE_SHIFT;
 
  		if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req)))
@@ -515,7 +555,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
  	if (copy_from_user(&input, argp, sizeof(input)))
  		return -EFAULT;
 
-	input.fw_err = 0xff;
+	input.exitinfo2 = SEV_RET_NO_FW_CALL;
 
  	/* Message version must be non-zero */
  	if (!input.msg_version)
@@ -546,7 +586,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
 
  	mutex_unlock(&snp_cmd_mutex);
 
-	if (input.fw_err && copy_to_user(argp, &input, sizeof(input)))
+	if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input)))
  		return -EFAULT;
 
  	return ret;
@@ -696,6 +736,8 @@ static int __init sev_guest_probe(struct platform_device *pdev)
  	if (ret)
  		goto e_free_cert_data;
 
+	ratelimit_state_init(&snp_dev->rs, rate_hz * HZ, rate_burst);
+
  	dev_info(dev, "Initialized SEV guest driver (using vmpck_id %d)\n", vmpck_id);
  	return 0;
 
include/uapi/linux/sev-guest.h +16 -2
index 256aaeff7e65..8e4144aa78c9 100644
@@ -52,8 +52,15 @@ struct snp_guest_request_ioctl {
  	__u64 req_data;
  	__u64 resp_data;
 
-	/* firmware error code on failure (see psp-sev.h) */
-	__u64 fw_err;
+	/* bits[63:32]: VMM error code, bits[31:0] firmware error code (see psp-sev.h) */
+	union {
+		__u64 exitinfo2;
+		__u64 fw_err; /* Name deprecated in favor of others */
+		struct {
+			__u32 fw_error;
+			__u32 vmm_error;
+		};
+	};
  };
 
  struct snp_ext_report_req {
@@ -77,4 +84,11 @@ struct snp_ext_report_req {
  /* Get SNP extended report as defined in the GHCB specification version 2. */
  #define SNP_GET_EXT_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x2, struct snp_guest_request_ioctl)
 
+/* Guest message request EXIT_INFO_2 constants */
+#define SNP_GUEST_FW_ERR_MASK		GENMASK_ULL(31, 0)
+#define SNP_GUEST_VMM_ERR_SHIFT		32
+
+#define SNP_GUEST_VMM_ERR_INVALID_LEN	1
+#define SNP_GUEST_VMM_ERR_BUSY		2
+
  #endif /* __UAPI_LINUX_SEV_GUEST_H_ */
#11 lore raw

Re: [PATCH v4 4/4] virt/coco/sev-guest: interpret VMM errors from guest request

Quoted 8 lines
Quoted 5 lines

static u32 vmpck_id;
module_param(vmpck_id, uint, 0444);
MODULE_PARM_DESC(vmpck_id, "The VMPCK ID to use when communicating with the PSP.");

+static int rate_hz = 2;

s/int/unsigned int/ to match uint below.

I'll change the uint to int, since the types in ratelimit_struct are int.

Quoted 14 lines
Quoted 14 lines
drivers/virt/coco/sev-guest/sev-guest.c +4 -0
+module_param(rate_burst, uint, 0444);
+MODULE_PARM_DESC(rate_burst, "The rate limit burst amount to limit requests to.");
+
  /* Mutex to serialize the shared buffer access and command handling. */
  static DEFINE_MUTEX(snp_cmd_mutex);
@@ -305,9 +316,12 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
                              u8 type, void *req_buf, size_t req_sz, void *resp_buf,
                              u32 resp_sz, __u64 *exitinfo2)
  {
+     unsigned int vmm_err;
      u64 seqno;
      int rc;
 
Quoted 6 lines

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 324

Quoted 2 lines
drivers/virt/coco/sev-guest/sev-guest.c +2 -0
+     might_resched();
+

Not sure this is needed. This may call cond_resched() right away, so I don't think this is doing what you think it is (as I only see it used in scheduler code and spinlock code). Did you mean to use might_sleep()?

Rereading the docs about it, I'll remove it.

Quoted 8 lines
Quoted 8 lines
drivers/virt/coco/sev-guest/sev-guest.c +0 -0
      /* Get message sequence and verify that its a non-zero */
      seqno = snp_get_msg_seqno(snp_dev);
      if (!seqno)
@@ -320,9 +334,35 @@ static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in
      if (rc)
              return rc;
 
Quoted 15 lines

drivers/virt/coco/sev-guest/sev-guest.c static int handle_guest_request(struct snp_guest_dev *snp_dev, u64 exit_code, in · line 343

Quoted 7 lines
drivers/virt/coco/sev-guest/sev-guest.c +7 -0
+retry:
+     /*
+      * Rate limit commands internally since the host can also throttle, and
+      * we don't want to create a tight request spin that could end up
+      * getting this VM throttled more heavily.
+      */
+     if (!__ratelimit(&snp_dev->rs)) {

This doesn't do any sleeping/delaying, it just returns a 0 or 1, so you could still cause a tight spin here. I guess that shouldn't be a problem is nothing else is ready to run. But maybe adding an msleep()/usleep() here based on the rate limit parameters (half the rate limit?) would be good so that the CPU isn't pegged while rating for the ratelimit to be satisified?

It looks like sleep_timeout_interruptible((rate_hz * HZ) / 2) could be the appropriate solution for this.

Quoted 2 lines

I'll let others chime in on that and see if it is appropriate.

Same, I'll wait until later Monday to send out v5.

Quoted 2 lines

Thanks,
Tom

Thanks for the reviews :)

Signature · 1 line
-Dionna Glaze, PhD (she/her)
#12 lore raw
In reply to Dionna Glaze

Pinging y'all in case you haven't seen v4 :)

Quoted 58 lines · Dionna Glaze

The guest request synchronous API from SEV-SNP VMs to the host's security processor consumes a global resource. For this reason, AMD's docs recommend that the host implements a throttling mechanism. In order for the guest to know it's been throttled and should try its request again, we need some good-faith communication from the host that the request has been throttled.

These patches work with the existing /dev/sev-guest ABI to detect a throttling code.

Changes from v3:
* sev-guest ratelimits itself to one request twice a second.
* Fixed a type signature to use u64 instead of unsigned int
* Set *exitinfo2 unconditionally after the ghcb_hv_call.
Changes from v2:
* Codified the non-firmware-call firmware error code as (u32)-1.
* Changed sev_issue_guest_request unsigned long *fw_err argument to u64 *exitinfo2 to more accurately and type-safely describe the value that it outputs.
* Changed sev_issue_guest_request to always set its exitinfo2 argument to either the non-firmware-call error code, the EXIT_INFO_2 returned from the VMM if the request failed, or 0 on success. This fixes a bug that returned uninitialized kernel stack memory to the user when there is no error.
* Changed the throttle behavior to retry in the driver instead of returning -EAGAIN, due to possible message sequence number reuse on different message contents.

Changes from v1:
* Changed throttle error code to 2

Cc Tom Lendacky <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Peter Gonda <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Dave Hansen <[email protected]>

Dionna Glaze (4):
ccp: Name -1 return value as SEV_RET_NO_FW_CALL x86/sev: Change snp_guest_issue_request's fw_err virt/coco/sev-guest: Remove err in handle_guest_request virt/coco/sev-guest: interpret VMM errors from guest request

 arch/x86/include/asm/sev.h              |  4 +-
 arch/x86/kernel/sev.c                   | 10 ++--
 drivers/crypto/ccp/sev-dev.c            |  2 +-
 drivers/virt/coco/sev-guest/sev-guest.c | 61 ++++++++++++++++++++-----
 include/uapi/linux/psp-sev.h            |  7 +++
 include/uapi/linux/sev-guest.h          | 18 +++++++-
 6 files changed, 82 insertions(+), 20 deletions(-)

--
2.38.0.135.g90850a2211-goog

Signature · 1 line
-Dionna Glaze, PhD (she/her)