All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] LockGuards: replace manual lock()/unlock() calls to WITH_QEMU_LOCK_GUARD()
@ 2022-11-04 23:08 nyoro.gachu
  2022-11-06 13:48 ` Stefan Hajnoczi
  0 siblings, 1 reply; 2+ messages in thread
From: nyoro.gachu @ 2022-11-04 23:08 UTC (permalink / raw)
  To: qemu-devel; +Cc: stefanha, Samker, M N Gachu

From: Samker <samkergachu@gmail.com>

LockGuards are preferred to manual lock()/unlock() calls. Lock guards
help prevent common bugs when locks are not released in error code
paths. This patch
replaces calls to manual lock()/unlock() with the much preferred
WITH_QEMU_LOCK_GUARD()

Signed-off-by: M N Gachu <nyoro.gachu@gmail.com>
---
 softmmu/physmem.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/softmmu/physmem.c b/softmmu/physmem.c
index d9578ccfd4..fb00596777 100644
--- a/softmmu/physmem.c
+++ b/softmmu/physmem.c
@@ -24,6 +24,7 @@
 #include "qemu/cutils.h"
 #include "qemu/cacheflush.h"
 #include "qemu/madvise.h"
+#include "qemu/lockable.h"
 
 #ifdef CONFIG_TCG
 #include "hw/core/tcg-cpu-ops.h"
@@ -3114,13 +3115,12 @@ void cpu_register_map_client(QEMUBH *bh)
 {
     MapClient *client = g_malloc(sizeof(*client));
 
-    qemu_mutex_lock(&map_client_list_lock);
+    WITH_QEMU_LOCK_GUARD(&map_client_list_lock);
     client->bh = bh;
     QLIST_INSERT_HEAD(&map_client_list, client, link);
     if (!qatomic_read(&bounce.in_use)) {
         cpu_notify_map_clients_locked();
     }
-    qemu_mutex_unlock(&map_client_list_lock);
 }
 
 void cpu_exec_init_all(void)
@@ -3143,7 +3143,7 @@ void cpu_unregister_map_client(QEMUBH *bh)
 {
     MapClient *client;
 
-    qemu_mutex_lock(&map_client_list_lock);
+    WITH_QEMU_LOCK_GUARD(&map_client_list_lock);
     QLIST_FOREACH(client, &map_client_list, link) {
         if (client->bh == bh) {
             cpu_unregister_map_client_do(client);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] LockGuards: replace manual lock()/unlock() calls to WITH_QEMU_LOCK_GUARD()
  2022-11-04 23:08 [PATCH] LockGuards: replace manual lock()/unlock() calls to WITH_QEMU_LOCK_GUARD() nyoro.gachu
@ 2022-11-06 13:48 ` Stefan Hajnoczi
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Hajnoczi @ 2022-11-06 13:48 UTC (permalink / raw)
  To: nyoro.gachu; +Cc: qemu-devel, stefanha, Samker

On Fri, 4 Nov 2022 at 21:04, <nyoro.gachu@gmail.com> wrote:
>
> From: Samker <samkergachu@gmail.com>
...
> Signed-off-by: M N Gachu <nyoro.gachu@gmail.com>

The author and Signed-off-by name/email are different. Do you want to
use a single name/email?

> ---
>  softmmu/physmem.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/softmmu/physmem.c b/softmmu/physmem.c
> index d9578ccfd4..fb00596777 100644
> --- a/softmmu/physmem.c
> +++ b/softmmu/physmem.c
> @@ -24,6 +24,7 @@
>  #include "qemu/cutils.h"
>  #include "qemu/cacheflush.h"
>  #include "qemu/madvise.h"
> +#include "qemu/lockable.h"
>
>  #ifdef CONFIG_TCG
>  #include "hw/core/tcg-cpu-ops.h"
> @@ -3114,13 +3115,12 @@ void cpu_register_map_client(QEMUBH *bh)
>  {
>      MapClient *client = g_malloc(sizeof(*client));
>
> -    qemu_mutex_lock(&map_client_list_lock);
> +    WITH_QEMU_LOCK_GUARD(&map_client_list_lock);

There is a bug here: the lock won't be held after this line because
WITH_QEMU_LOCK_GUARD() is block scoped. It requires curly braces:

  WITH_QEMU_LOCK_GUARD(&lock) {
      ...protected code...
  }
  ...unprotected code...

Use QEMU_LOCK_GUARD(&lock); when don't want block scope. It holds the
lock for the remainder of the function.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-06 13:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-04 23:08 [PATCH] LockGuards: replace manual lock()/unlock() calls to WITH_QEMU_LOCK_GUARD() nyoro.gachu
2022-11-06 13:48 ` Stefan Hajnoczi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.