[v3,05/14] virt: sev-guest: Add vmpck_id to snp_guest_dev struct

Message ID 20230722111909.15166-6-nikunj@amd.com
State New
Headers
Series Add Secure TSC support for SNP guests |

Commit Message

Nikunj A. Dadhania July 22, 2023, 11:19 a.m. UTC
  Drop vmpck and os_area_msg_seqno pointers so that secret page layout
does not need to be exposed to the sev-guest driver after the rework.
Instead, add helper APIs to access vmpck and os_area_msg_seqno when
needed.

Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
---
 drivers/virt/coco/sev-guest/sev-guest.c | 84 +++++++++++++------------
 1 file changed, 43 insertions(+), 41 deletions(-)
  

Comments

Tom Lendacky Aug. 1, 2023, 4:30 p.m. UTC | #1
On 7/22/23 06:19, Nikunj A Dadhania wrote:
> Drop vmpck and os_area_msg_seqno pointers so that secret page layout
> does not need to be exposed to the sev-guest driver after the rework.
> Instead, add helper APIs to access vmpck and os_area_msg_seqno when
> needed.
> 
> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
> ---
>   drivers/virt/coco/sev-guest/sev-guest.c | 84 +++++++++++++------------
>   1 file changed, 43 insertions(+), 41 deletions(-)
> 
> diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
> index d4241048b397..8ad43e007d3b 100644
> --- a/drivers/virt/coco/sev-guest/sev-guest.c
> +++ b/drivers/virt/coco/sev-guest/sev-guest.c
> @@ -50,8 +50,7 @@ struct snp_guest_dev {
>   
>   	struct snp_secrets_page_layout *layout;
>   	struct snp_req_data input;
> -	u32 *os_area_msg_seqno;
> -	u8 *vmpck;
> +	unsigned int vmpck_id;
>   };
>   
>   static u32 vmpck_id;
> @@ -67,12 +66,23 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)
>   	return 0;
>   }
>   
> -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
> +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
> +{
> +	return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
> +}
> +
> +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev)
> +{
> +	return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id;
> +}
> +
> +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev)

I noticed this name change from is_vmpck_empty() to snp_is_vmpck_empty(). 
Is that in prep for moving, too? Is so, maybe call that out in the commit 
message.

>   {
>   	char zero_key[VMPCK_KEY_LEN] = {0};
> +	u8 *key = snp_get_vmpck(snp_dev);
>   
> -	if (snp_dev->vmpck)
> -		return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN);
> +	if (key)
> +		return !memcmp(key, zero_key, VMPCK_KEY_LEN);

I believe key can't be NULL, so this check isn't required.

Thanks,
Tom

>   
>   	return true;
>   }
  
Nikunj A. Dadhania Aug. 2, 2023, 4:12 a.m. UTC | #2
On 8/1/2023 10:00 PM, Tom Lendacky wrote:
> On 7/22/23 06:19, Nikunj A Dadhania wrote:
>> Drop vmpck and os_area_msg_seqno pointers so that secret page layout
>> does not need to be exposed to the sev-guest driver after the rework.
>> Instead, add helper APIs to access vmpck and os_area_msg_seqno when
>> needed.
>>
>> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
>> ---
>>   drivers/virt/coco/sev-guest/sev-guest.c | 84 +++++++++++++------------
>>   1 file changed, 43 insertions(+), 41 deletions(-)
>>
>> diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
>> index d4241048b397..8ad43e007d3b 100644
>> --- a/drivers/virt/coco/sev-guest/sev-guest.c
>> +++ b/drivers/virt/coco/sev-guest/sev-guest.c
>> @@ -50,8 +50,7 @@ struct snp_guest_dev {
>>         struct snp_secrets_page_layout *layout;
>>       struct snp_req_data input;
>> -    u32 *os_area_msg_seqno;
>> -    u8 *vmpck;
>> +    unsigned int vmpck_id;
>>   };
>>     static u32 vmpck_id;
>> @@ -67,12 +66,23 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)
>>       return 0;
>>   }
>>   -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
>> +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
>> +{
>> +    return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
>> +}
>> +
>> +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev)
>> +{
>> +    return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id;
>> +}
>> +
>> +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev)
> 
> I noticed this name change from is_vmpck_empty() to snp_is_vmpck_empty(). Is that in prep for moving, too? Is so, maybe call that out in the commit message.

Yes, will add to the  commit.

> 
>>   {
>>       char zero_key[VMPCK_KEY_LEN] = {0};
>> +    u8 *key = snp_get_vmpck(snp_dev);
>>   -    if (snp_dev->vmpck)
>> -        return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN);
>> +    if (key)
>> +        return !memcmp(key, zero_key, VMPCK_KEY_LEN);
> 
> I believe key can't be NULL, so this check isn't required.

Sure, will update.

Regards
Nikunj
  
Tom Lendacky Aug. 2, 2023, 4:55 p.m. UTC | #3
On 8/1/23 23:12, Nikunj A. Dadhania wrote:
> On 8/1/2023 10:00 PM, Tom Lendacky wrote:
>> On 7/22/23 06:19, Nikunj A Dadhania wrote:
>>> Drop vmpck and os_area_msg_seqno pointers so that secret page layout
>>> does not need to be exposed to the sev-guest driver after the rework.
>>> Instead, add helper APIs to access vmpck and os_area_msg_seqno when
>>> needed.
>>>
>>> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
>>> ---
>>>    drivers/virt/coco/sev-guest/sev-guest.c | 84 +++++++++++++------------
>>>    1 file changed, 43 insertions(+), 41 deletions(-)
>>>
>>> diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
>>> index d4241048b397..8ad43e007d3b 100644
>>> --- a/drivers/virt/coco/sev-guest/sev-guest.c
>>> +++ b/drivers/virt/coco/sev-guest/sev-guest.c
>>> @@ -50,8 +50,7 @@ struct snp_guest_dev {
>>>          struct snp_secrets_page_layout *layout;
>>>        struct snp_req_data input;
>>> -    u32 *os_area_msg_seqno;
>>> -    u8 *vmpck;
>>> +    unsigned int vmpck_id;
>>>    };
>>>      static u32 vmpck_id;
>>> @@ -67,12 +66,23 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)
>>>        return 0;
>>>    }
>>>    -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
>>> +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
>>> +{
>>> +    return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
>>> +}
>>> +
>>> +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev)
>>> +{
>>> +    return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id;
>>> +}
>>> +
>>> +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev)
>>
>> I noticed this name change from is_vmpck_empty() to snp_is_vmpck_empty(). Is that in prep for moving, too? Is so, maybe call that out in the commit message.
> 
> Yes, will add to the  commit.
> 
>>
>>>    {
>>>        char zero_key[VMPCK_KEY_LEN] = {0};
>>> +    u8 *key = snp_get_vmpck(snp_dev);
>>>    -    if (snp_dev->vmpck)
>>> -        return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN);
>>> +    if (key)
>>> +        return !memcmp(key, zero_key, VMPCK_KEY_LEN);
>>
>> I believe key can't be NULL, so this check isn't required.
> 
> Sure, will update.

Ah, although I noticed that when the various functions are moved over to 
the other file, the key return value can be NULL, so probably not worth 
changing here.

Thanks,
Tom

> 
> Regards
> Nikunj
>
  
Nikunj A. Dadhania Aug. 3, 2023, 5:02 a.m. UTC | #4
On 8/2/2023 10:25 PM, Tom Lendacky wrote:
> On 8/1/23 23:12, Nikunj A. Dadhania wrote:
>> On 8/1/2023 10:00 PM, Tom Lendacky wrote:
>>> On 7/22/23 06:19, Nikunj A Dadhania wrote:
>>>> Drop vmpck and os_area_msg_seqno pointers so that secret page layout
>>>> does not need to be exposed to the sev-guest driver after the rework.
>>>> Instead, add helper APIs to access vmpck and os_area_msg_seqno when
>>>> needed.
>>>>
>>>> Signed-off-by: Nikunj A Dadhania <nikunj@amd.com>
>>>> ---
>>>>    drivers/virt/coco/sev-guest/sev-guest.c | 84 +++++++++++++------------
>>>>    1 file changed, 43 insertions(+), 41 deletions(-)
>>>>
>>>> diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
>>>> index d4241048b397..8ad43e007d3b 100644
>>>> --- a/drivers/virt/coco/sev-guest/sev-guest.c
>>>> +++ b/drivers/virt/coco/sev-guest/sev-guest.c
>>>> @@ -50,8 +50,7 @@ struct snp_guest_dev {
>>>>          struct snp_secrets_page_layout *layout;
>>>>        struct snp_req_data input;
>>>> -    u32 *os_area_msg_seqno;
>>>> -    u8 *vmpck;
>>>> +    unsigned int vmpck_id;
>>>>    };
>>>>      static u32 vmpck_id;
>>>> @@ -67,12 +66,23 @@ static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)
>>>>        return 0;
>>>>    }
>>>>    -static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
>>>> +static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
>>>> +{
>>>> +    return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
>>>> +}
>>>> +
>>>> +static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev)
>>>> +{
>>>> +    return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id;
>>>> +}
>>>> +
>>>> +static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev)
>>>
>>> I noticed this name change from is_vmpck_empty() to snp_is_vmpck_empty(). Is that in prep for moving, too? Is so, maybe call that out in the commit message.
>>
>> Yes, will add to the  commit.
>>
>>>
>>>>    {
>>>>        char zero_key[VMPCK_KEY_LEN] = {0};
>>>> +    u8 *key = snp_get_vmpck(snp_dev);
>>>>    -    if (snp_dev->vmpck)
>>>> -        return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN);
>>>> +    if (key)
>>>> +        return !memcmp(key, zero_key, VMPCK_KEY_LEN);
>>>
>>> I believe key can't be NULL, so this check isn't required.
>>
>> Sure, will update.
> 
> Ah, although I noticed that when the various functions are moved over to the other file, the key return value can be NULL, so probably not worth changing here.

I have removed the check in this patch, but the check is retained in the later patch where key can be NULL.

Regards
Nikunj
  

Patch

diff --git a/drivers/virt/coco/sev-guest/sev-guest.c b/drivers/virt/coco/sev-guest/sev-guest.c
index d4241048b397..8ad43e007d3b 100644
--- a/drivers/virt/coco/sev-guest/sev-guest.c
+++ b/drivers/virt/coco/sev-guest/sev-guest.c
@@ -50,8 +50,7 @@  struct snp_guest_dev {
 
 	struct snp_secrets_page_layout *layout;
 	struct snp_req_data input;
-	u32 *os_area_msg_seqno;
-	u8 *vmpck;
+	unsigned int vmpck_id;
 };
 
 static u32 vmpck_id;
@@ -67,12 +66,23 @@  static inline unsigned int get_ctx_authsize(struct snp_guest_dev *snp_dev)
 	return 0;
 }
 
-static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
+static inline u8 *snp_get_vmpck(struct snp_guest_dev *snp_dev)
+{
+	return snp_dev->layout->vmpck0 + snp_dev->vmpck_id * VMPCK_KEY_LEN;
+}
+
+static inline u32 *snp_get_os_area_msg_seqno(struct snp_guest_dev *snp_dev)
+{
+	return &snp_dev->layout->os_area.msg_seqno_0 + snp_dev->vmpck_id;
+}
+
+static bool snp_is_vmpck_empty(struct snp_guest_dev *snp_dev)
 {
 	char zero_key[VMPCK_KEY_LEN] = {0};
+	u8 *key = snp_get_vmpck(snp_dev);
 
-	if (snp_dev->vmpck)
-		return !memcmp(snp_dev->vmpck, zero_key, VMPCK_KEY_LEN);
+	if (key)
+		return !memcmp(key, zero_key, VMPCK_KEY_LEN);
 
 	return true;
 }
@@ -96,20 +106,22 @@  static bool is_vmpck_empty(struct snp_guest_dev *snp_dev)
  */
 static void snp_disable_vmpck(struct snp_guest_dev *snp_dev)
 {
+	u8 *key = snp_get_vmpck(snp_dev);
+
 	dev_alert(snp_dev->dev, "Disabling vmpck_id %d to prevent IV reuse.\n",
-		  vmpck_id);
-	memzero_explicit(snp_dev->vmpck, VMPCK_KEY_LEN);
-	snp_dev->vmpck = NULL;
+		  snp_dev->vmpck_id);
+	memzero_explicit(key, VMPCK_KEY_LEN);
 }
 
 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_dev->cmd_mutex);
 
 	/* Read the current message sequence counter from secrets pages */
-	count = *snp_dev->os_area_msg_seqno;
+	count = *os_area_msg_seqno;
 
 	return count + 1;
 }
@@ -137,11 +149,13 @@  static u64 snp_get_msg_seqno(struct snp_guest_dev *snp_dev)
 
 static void snp_inc_msg_seqno(struct snp_guest_dev *snp_dev)
 {
+	u32 *os_area_msg_seqno = snp_get_os_area_msg_seqno(snp_dev);
+
 	/*
 	 * The counter is also incremented by the PSP, so increment it by 2
 	 * and save in secrets page.
 	 */
-	*snp_dev->os_area_msg_seqno += 2;
+	*os_area_msg_seqno += 2;
 }
 
 static inline struct snp_guest_dev *to_snp_dev(struct file *file)
@@ -151,15 +165,22 @@  static inline struct snp_guest_dev *to_snp_dev(struct file *file)
 	return container_of(dev, struct snp_guest_dev, misc);
 }
 
-static struct aesgcm_ctx *snp_init_crypto(u8 *key, size_t keylen)
+static struct aesgcm_ctx *snp_init_crypto(struct snp_guest_dev *snp_dev)
 {
 	struct aesgcm_ctx *ctx;
+	u8 *key;
+
+	if (snp_is_vmpck_empty(snp_dev)) {
+		pr_err("SNP: vmpck id %d is null\n", snp_dev->vmpck_id);
+		return NULL;
+	}
 
 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
 	if (!ctx)
 		return NULL;
 
-	if (aesgcm_expandkey(ctx, key, keylen, AUTHTAG_LEN)) {
+	key = snp_get_vmpck(snp_dev);
+	if (aesgcm_expandkey(ctx, key, VMPCK_KEY_LEN, AUTHTAG_LEN)) {
 		pr_err("SNP: crypto init failed\n");
 		kfree(ctx);
 		return NULL;
@@ -606,7 +627,7 @@  static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long
 	mutex_lock(&snp_dev->cmd_mutex);
 
 	/* Check if the VMPCK is not empty */
-	if (is_vmpck_empty(snp_dev)) {
+	if (snp_is_vmpck_empty(snp_dev)) {
 		dev_err_ratelimited(snp_dev->dev, "VMPCK is disabled\n");
 		mutex_unlock(&snp_dev->cmd_mutex);
 		return -ENOTTY;
@@ -676,32 +697,14 @@  static const struct file_operations snp_guest_fops = {
 	.unlocked_ioctl = snp_guest_ioctl,
 };
 
-static u8 *get_vmpck(int id, struct snp_secrets_page_layout *layout, u32 **seqno)
+bool snp_assign_vmpck(struct snp_guest_dev *dev, int vmpck_id)
 {
-	u8 *key = NULL;
+	if (WARN_ON(vmpck_id > 3))
+		return false;
 
-	switch (id) {
-	case 0:
-		*seqno = &layout->os_area.msg_seqno_0;
-		key = layout->vmpck0;
-		break;
-	case 1:
-		*seqno = &layout->os_area.msg_seqno_1;
-		key = layout->vmpck1;
-		break;
-	case 2:
-		*seqno = &layout->os_area.msg_seqno_2;
-		key = layout->vmpck2;
-		break;
-	case 3:
-		*seqno = &layout->os_area.msg_seqno_3;
-		key = layout->vmpck3;
-		break;
-	default:
-		break;
-	}
+	dev->vmpck_id = vmpck_id;
 
-	return key;
+	return true;
 }
 
 static int __init sev_guest_probe(struct platform_device *pdev)
@@ -733,14 +736,14 @@  static int __init sev_guest_probe(struct platform_device *pdev)
 		goto e_unmap;
 
 	ret = -EINVAL;
-	snp_dev->vmpck = get_vmpck(vmpck_id, layout, &snp_dev->os_area_msg_seqno);
-	if (!snp_dev->vmpck) {
+	snp_dev->layout = layout;
+	if (!snp_assign_vmpck(snp_dev, vmpck_id)) {
 		dev_err(dev, "invalid vmpck id %d\n", vmpck_id);
 		goto e_unmap;
 	}
 
 	/* Verify that VMPCK is not zero. */
-	if (is_vmpck_empty(snp_dev)) {
+	if (snp_is_vmpck_empty(snp_dev)) {
 		dev_err(dev, "vmpck id %d is null\n", vmpck_id);
 		goto e_unmap;
 	}
@@ -748,7 +751,6 @@  static int __init sev_guest_probe(struct platform_device *pdev)
 	mutex_init(&snp_dev->cmd_mutex);
 	platform_set_drvdata(pdev, snp_dev);
 	snp_dev->dev = dev;
-	snp_dev->layout = layout;
 
 	/* Allocate the shared page used for the request and response message. */
 	snp_dev->request = alloc_shared_pages(dev, sizeof(struct snp_guest_msg));
@@ -764,7 +766,7 @@  static int __init sev_guest_probe(struct platform_device *pdev)
 		goto e_free_response;
 
 	ret = -EIO;
-	snp_dev->ctx = snp_init_crypto(snp_dev->vmpck, VMPCK_KEY_LEN);
+	snp_dev->ctx = snp_init_crypto(snp_dev);
 	if (!snp_dev->ctx)
 		goto e_free_cert_data;