Re: [PATCH v7 06/16] virt: sev-guest: Move SNP Guest command mutex

From: Tom Lendacky
Date: Fri Jan 26 2024 - 17:13:02 EST


On 12/20/23 09:13, Nikunj A Dadhania wrote:
SNP command mutex is used to serialize the shared buffer access, command
handling and message sequence number races. Move the SNP guest command
mutex out of the sev guest driver and provide accessors to sev-guest
driver. Remove multiple lockdep check in sev-guest driver, next patch adds
a single lockdep check in snp_send_guest_request().

Signed-off-by: Nikunj A Dadhania <nikunj@xxxxxxx>
---
arch/x86/include/asm/sev-guest.h | 3 +++
arch/x86/kernel/sev.c | 21 +++++++++++++++++++++
drivers/virt/coco/sev-guest/sev-guest.c | 23 +++++++----------------
3 files changed, 31 insertions(+), 16 deletions(-)

diff --git a/arch/x86/include/asm/sev-guest.h b/arch/x86/include/asm/sev-guest.h
index 27cc15ad6131..2f3cceb88396 100644
--- a/arch/x86/include/asm/sev-guest.h
+++ b/arch/x86/include/asm/sev-guest.h
@@ -81,4 +81,7 @@ struct snp_guest_req {
int snp_issue_guest_request(struct snp_guest_req *req, struct snp_req_data *input,
struct snp_guest_request_ioctl *rio);
+void snp_guest_cmd_lock(void);
+void snp_guest_cmd_unlock(void);
+
#endif /* __VIRT_SEVGUEST_H__ */
diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
index 6aa0bdf8a7a0..191193924b22 100644
--- a/arch/x86/kernel/sev.c
+++ b/arch/x86/kernel/sev.c
@@ -941,6 +941,21 @@ static void snp_cleanup_vmsa(struct sev_es_save_area *vmsa)
free_page((unsigned long)vmsa);
}
+/* SNP Guest command mutex to serialize the shared buffer access and command handling. */
+static struct mutex snp_guest_cmd_mutex;

You should probably use:

static DEFINE_MUTEX(snp_guest_cmd_mutex);

That way you can avoid the initialization in snp_init_platform_device().

With that:

Reviewed-by: Tom Lendacky <thomas.lendacky@xxxxxxx>

+
+void snp_guest_cmd_lock(void)
+{
+ mutex_lock(&snp_guest_cmd_mutex);
+}
+EXPORT_SYMBOL_GPL(snp_guest_cmd_lock);
+
+void snp_guest_cmd_unlock(void)
+{
+ mutex_unlock(&snp_guest_cmd_mutex);
+}
+EXPORT_SYMBOL_GPL(snp_guest_cmd_unlock);
+
static int wakeup_cpu_via_vmgexit(u32 apic_id, unsigned long start_ip)
{
struct sev_es_save_area *cur_vmsa, *vmsa;
@@ -2240,6 +2255,12 @@ static int __init snp_init_platform_device(void)
return -ENODEV;
}
+ /*
+ * Initialize snp command mutex that is used to serialize the shared
+ * buffer access and use of the vmpck and message sequence number
+ */
+ mutex_init(&snp_guest_cmd_mutex);
+
data.secrets_gpa = secrets_pa;
if (platform_device_add_data(&sev_guest_device, &data, sizeof(data)))
return -ENODEV;
diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
index 9c0ff69a16da..bd30a9ff82c1 100644
--- a/drivers/virt/coco/sev-guest/sev-guest.c
+++ b/drivers/virt/coco/sev-guest/sev-guest.c
@@ -63,9 +63,6 @@ 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.");
-/* Mutex to serialize the shared buffer access and command handling. */
-static DEFINE_MUTEX(snp_cmd_mutex);
-
static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
{
return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
@@ -115,8 +112,6 @@ static inline u64 __snp_get_msg_seqno(struct snp_guest_dev *snp_dev)
u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev);
u64 count;
- lockdep_assert_held(&snp_cmd_mutex);
-
/* Read the current message sequence counter from secrets pages */
count = *os_area_msg_seqno;
@@ -409,8 +404,6 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_guest_request_io
struct snp_report_resp *resp;
int rc, resp_len;
- lockdep_assert_held(&snp_cmd_mutex);
-
if (!arg->req_data || !arg->resp_data)
return -EINVAL;
@@ -457,8 +450,6 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_guest_reque
/* Response data is 64 bytes and max authsize for GCM is 16 bytes. */
u8 buf[64 + 16];
- lockdep_assert_held(&snp_cmd_mutex);
-
if (!arg->req_data || !arg->resp_data)
return -EINVAL;
@@ -507,8 +498,6 @@ static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_guest_reques
sockptr_t certs_address;
int ret, resp_len;
- lockdep_assert_held(&snp_cmd_mutex);
-
if (sockptr_is_null(io->req_data) || sockptr_is_null(io->resp_data))
return -EINVAL;
@@ -604,12 +593,12 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
if (!input.msg_version)
return -EINVAL;
- mutex_lock(&snp_cmd_mutex);
+ snp_guest_cmd_lock();
/* Check if the VMPCK is not empty */
if (snp_is_vmpck_empty(snp_dev)) {
dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n");
- mutex_unlock(&snp_cmd_mutex);
+ snp_guest_cmd_unlock();
return -ENOTTY;
}
@@ -634,7 +623,7 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
break;
}
- mutex_unlock(&snp_cmd_mutex);
+ snp_guest_cmd_unlock();
if (input.exitinfo2 && copy_to_user(argp, &input, sizeof(input)))
return -EFAULT;
@@ -724,14 +713,14 @@ static int sev_report_new(struct tsm_report *report, void *data)
if (!buf)
return -ENOMEM;
- guard(mutex)(&snp_cmd_mutex);
-
/* Check if the VMPCK is not empty */
if (snp_is_vmpck_empty(snp_dev)) {
dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n");
return -ENOTTY;
}
+ snp_guest_cmd_lock();
+
cert_table = buf + report_size;
struct snp_ext_report_req ext_req = {
.data = { .vmpl = desc->privlevel },
@@ -752,6 +741,8 @@ static int sev_report_new(struct tsm_report *report, void *data)
};
ret = get_ext_report(snp_dev, &input, &io);
+ snp_guest_cmd_unlock();
+
if (ret)
return ret;