diff mbox series

[v4,12/23] target/i386/sev: Use g_autofree in sev_launch_get_measure()

Message ID 20211007161716.453984-13-philmd@redhat.com (mailing list archive)
State New, archived
Headers show
Series target/i386/sev: Housekeeping SEV + measured Linux SEV guest | expand

Commit Message

Philippe Mathieu-Daudé Oct. 7, 2021, 4:17 p.m. UTC
Use g_autofree to remove a pair of g_free/goto.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 target/i386/sev.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

Comments

Dov Murik Oct. 7, 2021, 4:28 p.m. UTC | #1
On 07/10/2021 19:17, Philippe Mathieu-Daudé wrote:
> Use g_autofree to remove a pair of g_free/goto.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>  target/i386/sev.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/target/i386/sev.c b/target/i386/sev.c
> index 3a30ba6d94a..5cbbcf0bb93 100644
> --- a/target/i386/sev.c
> +++ b/target/i386/sev.c
> @@ -685,8 +685,8 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>  {
>      SevGuestState *sev = sev_guest;
>      int ret, error;
> -    guchar *data;
> -    struct kvm_sev_launch_measure *measurement;
> +    g_autofree guchar *data = NULL;
> +    g_autofree struct kvm_sev_launch_measure *measurement = NULL;

Sorry for joining so late, but why do we allocate struct
kvm_sev_launch_measure on the heap (even before this patch)? It's only
12 bytes. Might be simpler to have a local (stack) variable and not care
about memory management.

-Dov


>  
>      if (!sev_check_state(sev, SEV_STATE_LAUNCH_UPDATE)) {
>          return;
> @@ -708,7 +708,7 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>      if (!measurement->len) {
>          error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
>                       __func__, ret, error, fw_error_to_str(errno));
> -        goto free_measurement;
> +        return;
>      }
>  
>      data = g_new0(guchar, measurement->len);
> @@ -720,7 +720,7 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>      if (ret) {
>          error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
>                       __func__, ret, error, fw_error_to_str(errno));
> -        goto free_data;
> +        return;
>      }
>  
>      sev_set_guest_state(sev, SEV_STATE_LAUNCH_SECRET);
> @@ -728,11 +728,6 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>      /* encode the measurement value and emit the event */
>      sev->measurement = g_base64_encode(data, measurement->len);
>      trace_kvm_sev_launch_measurement(sev->measurement);
> -
> -free_data:
> -    g_free(data);
> -free_measurement:
> -    g_free(measurement);
>  }
>  
>  char *
>
Dov Murik Oct. 12, 2021, 6:15 a.m. UTC | #2
On 07/10/2021 19:28, Dov Murik wrote:
> 
> 
> On 07/10/2021 19:17, Philippe Mathieu-Daudé wrote:
>> Use g_autofree to remove a pair of g_free/goto.
>>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
>> ---
>>  target/i386/sev.c | 13 ++++---------
>>  1 file changed, 4 insertions(+), 9 deletions(-)
>>
>> diff --git a/target/i386/sev.c b/target/i386/sev.c
>> index 3a30ba6d94a..5cbbcf0bb93 100644
>> --- a/target/i386/sev.c
>> +++ b/target/i386/sev.c
>> @@ -685,8 +685,8 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>>  {
>>      SevGuestState *sev = sev_guest;
>>      int ret, error;
>> -    guchar *data;
>> -    struct kvm_sev_launch_measure *measurement;
>> +    g_autofree guchar *data = NULL;
>> +    g_autofree struct kvm_sev_launch_measure *measurement = NULL;
> 
> Sorry for joining so late, but why do we allocate struct
> kvm_sev_launch_measure on the heap (even before this patch)? It's only
> 12 bytes. Might be simpler to have a local (stack) variable and not care
> about memory management.
> 

I sent another series [1] with this fix (and a similar fix in
launch_start); it can be added as part of this housekeeping series.

[1] https://lore.kernel.org/qemu-devel/20211011173026.2454294-1-dovmurik@linux.ibm.com/

-Dov

> -Dov
> 
> 
>>  
>>      if (!sev_check_state(sev, SEV_STATE_LAUNCH_UPDATE)) {
>>          return;
>> @@ -708,7 +708,7 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>>      if (!measurement->len) {
>>          error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
>>                       __func__, ret, error, fw_error_to_str(errno));
>> -        goto free_measurement;
>> +        return;
>>      }
>>  
>>      data = g_new0(guchar, measurement->len);
>> @@ -720,7 +720,7 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>>      if (ret) {
>>          error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
>>                       __func__, ret, error, fw_error_to_str(errno));
>> -        goto free_data;
>> +        return;
>>      }
>>  
>>      sev_set_guest_state(sev, SEV_STATE_LAUNCH_SECRET);
>> @@ -728,11 +728,6 @@ sev_launch_get_measure(Notifier *notifier, void *unused)
>>      /* encode the measurement value and emit the event */
>>      sev->measurement = g_base64_encode(data, measurement->len);
>>      trace_kvm_sev_launch_measurement(sev->measurement);
>> -
>> -free_data:
>> -    g_free(data);
>> -free_measurement:
>> -    g_free(measurement);
>>  }
>>  
>>  char *
>>
diff mbox series

Patch

diff --git a/target/i386/sev.c b/target/i386/sev.c
index 3a30ba6d94a..5cbbcf0bb93 100644
--- a/target/i386/sev.c
+++ b/target/i386/sev.c
@@ -685,8 +685,8 @@  sev_launch_get_measure(Notifier *notifier, void *unused)
 {
     SevGuestState *sev = sev_guest;
     int ret, error;
-    guchar *data;
-    struct kvm_sev_launch_measure *measurement;
+    g_autofree guchar *data = NULL;
+    g_autofree struct kvm_sev_launch_measure *measurement = NULL;
 
     if (!sev_check_state(sev, SEV_STATE_LAUNCH_UPDATE)) {
         return;
@@ -708,7 +708,7 @@  sev_launch_get_measure(Notifier *notifier, void *unused)
     if (!measurement->len) {
         error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
                      __func__, ret, error, fw_error_to_str(errno));
-        goto free_measurement;
+        return;
     }
 
     data = g_new0(guchar, measurement->len);
@@ -720,7 +720,7 @@  sev_launch_get_measure(Notifier *notifier, void *unused)
     if (ret) {
         error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'",
                      __func__, ret, error, fw_error_to_str(errno));
-        goto free_data;
+        return;
     }
 
     sev_set_guest_state(sev, SEV_STATE_LAUNCH_SECRET);
@@ -728,11 +728,6 @@  sev_launch_get_measure(Notifier *notifier, void *unused)
     /* encode the measurement value and emit the event */
     sev->measurement = g_base64_encode(data, measurement->len);
     trace_kvm_sev_launch_measurement(sev->measurement);
-
-free_data:
-    g_free(data);
-free_measurement:
-    g_free(measurement);
 }
 
 char *