[PATCH 1/9] tpm: Changed a qemu_mutex_lock to QEMU_LOCK_GUARD

Mahmoud Mandour posted 9 patches 4 years, 11 months ago
Maintainers: Markus Armbruster <armbru@redhat.com>, Eric Blake <eblake@redhat.com>, Kevin Wolf <kwolf@redhat.com>, Juan Quintela <quintela@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Christian Schoenebeck <qemu_oss@crudebyte.com>, Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>, Max Reitz <mreitz@redhat.com>, Stefan Berger <stefanb@linux.vnet.ibm.com>, "Marc-André Lureau" <marcandre.lureau@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, Greg Kurz <groug@kaod.org>, Eric Auger <eric.auger@redhat.com>, "Daniel P. Berrangé" <berrange@redhat.com>, "Dr. David Alan Gilbert" <dgilbert@redhat.com>
[PATCH 1/9] tpm: Changed a qemu_mutex_lock to QEMU_LOCK_GUARD
Posted by Mahmoud Mandour 4 years, 11 months ago
Removed a qemu_mutex_lock() and its respective qemu_mutex_unlock()
and used QEMU_LOCK_GUARD instead. This simplifies the code by
eliminiating gotos and removing the qemu_mutex_unlock() calls.

Signed-off-by: Mahmoud Mandour <ma.mandourr@gmail.com>
---
 backends/tpm/tpm_emulator.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/backends/tpm/tpm_emulator.c b/backends/tpm/tpm_emulator.c
index a012adc193..a3c041e402 100644
--- a/backends/tpm/tpm_emulator.c
+++ b/backends/tpm/tpm_emulator.c
@@ -126,7 +126,7 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
     uint8_t *buf = NULL;
     int ret = -1;
 
-    qemu_mutex_lock(&tpm->mutex);
+    QEMU_LOCK_GUARD(&tpm->mutex);
 
     buf = g_alloca(n);
     memcpy(buf, &cmd_no, sizeof(cmd_no));
@@ -134,20 +134,18 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
 
     n = qemu_chr_fe_write_all(dev, buf, n);
     if (n <= 0) {
-        goto end;
+        return ret;
     }
 
     if (msg_len_out != 0) {
         n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
         if (n <= 0) {
-            goto end;
+            return ret;
         }
     }
 
     ret = 0;
 
-end:
-    qemu_mutex_unlock(&tpm->mutex);
     return ret;
 }
 
-- 
2.25.1


Re: [PATCH 1/9] tpm: Changed a qemu_mutex_lock to QEMU_LOCK_GUARD
Posted by Stefan Berger 4 years, 10 months ago
On 3/10/21 10:15 PM, Mahmoud Mandour wrote:
> Removed a qemu_mutex_lock() and its respective qemu_mutex_unlock()
> and used QEMU_LOCK_GUARD instead. This simplifies the code by
> eliminiating gotos and removing the qemu_mutex_unlock() calls.
>
> Signed-off-by: Mahmoud Mandour <ma.mandourr@gmail.com>
> ---
>   backends/tpm/tpm_emulator.c | 8 +++-----
>   1 file changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/backends/tpm/tpm_emulator.c b/backends/tpm/tpm_emulator.c
> index a012adc193..a3c041e402 100644
> --- a/backends/tpm/tpm_emulator.c
> +++ b/backends/tpm/tpm_emulator.c
> @@ -126,7 +126,7 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
>       uint8_t *buf = NULL;
>       int ret = -1;


ret is not needed anymore


>
> -    qemu_mutex_lock(&tpm->mutex);
> +    QEMU_LOCK_GUARD(&tpm->mutex);
>
>       buf = g_alloca(n);
>       memcpy(buf, &cmd_no, sizeof(cmd_no));
> @@ -134,20 +134,18 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
>
>       n = qemu_chr_fe_write_all(dev, buf, n);
>       if (n <= 0) {
> -        goto end;
> +        return ret;
>       }
>
>       if (msg_len_out != 0) {
>           n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
>           if (n <= 0) {
> -            goto end;
> +            return ret;
>           }
>       }
>
>       ret = 0;
>
> -end:
> -    qemu_mutex_unlock(&tpm->mutex);
>       return ret;


return 0;



>   }
>

Re: [PATCH 1/9] tpm: Changed a qemu_mutex_lock to QEMU_LOCK_GUARD
Posted by Marc-André Lureau 4 years, 11 months ago
On Thu, Mar 11, 2021 at 1:26 PM Mahmoud Mandour <ma.mandourr@gmail.com>
wrote:

> Removed a qemu_mutex_lock() and its respective qemu_mutex_unlock()
> and used QEMU_LOCK_GUARD instead. This simplifies the code by
> eliminiating gotos and removing the qemu_mutex_unlock() calls.
>
> Signed-off-by: Mahmoud Mandour <ma.mandourr@gmail.com>
>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

---
>  backends/tpm/tpm_emulator.c | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/backends/tpm/tpm_emulator.c b/backends/tpm/tpm_emulator.c
> index a012adc193..a3c041e402 100644
> --- a/backends/tpm/tpm_emulator.c
> +++ b/backends/tpm/tpm_emulator.c
> @@ -126,7 +126,7 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm,
> unsigned long cmd, void *msg,
>      uint8_t *buf = NULL;
>      int ret = -1;
>
> -    qemu_mutex_lock(&tpm->mutex);
> +    QEMU_LOCK_GUARD(&tpm->mutex);
>
>      buf = g_alloca(n);
>      memcpy(buf, &cmd_no, sizeof(cmd_no));
> @@ -134,20 +134,18 @@ static int tpm_emulator_ctrlcmd(TPMEmulator *tpm,
> unsigned long cmd, void *msg,
>
>      n = qemu_chr_fe_write_all(dev, buf, n);
>      if (n <= 0) {
> -        goto end;
> +        return ret;
>      }
>
>      if (msg_len_out != 0) {
>          n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
>          if (n <= 0) {
> -            goto end;
> +            return ret;
>          }
>      }
>
>      ret = 0;
>
> -end:
> -    qemu_mutex_unlock(&tpm->mutex);
>      return ret;
>  }
>
> --
> 2.25.1
>
>
>

-- 
Marc-André Lureau