qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org
Subject: [PULL 47/51] graph-lock: TSA annotations for lock/unlock functions
Date: Wed, 14 Dec 2022 14:44:49 +0100	[thread overview]
Message-ID: <20221214134453.31665-48-kwolf@redhat.com> (raw)
In-Reply-To: <20221214134453.31665-1-kwolf@redhat.com>

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Message-Id: <20221207131838.239125-15-kwolf@redhat.com>
Reviewed-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 include/block/graph-lock.h | 80 +++++++++++++++++++++++++++++++++-----
 block/graph-lock.c         |  3 ++
 2 files changed, 73 insertions(+), 10 deletions(-)

diff --git a/include/block/graph-lock.h b/include/block/graph-lock.h
index 85e8a53b73..50b7e7b1b6 100644
--- a/include/block/graph-lock.h
+++ b/include/block/graph-lock.h
@@ -21,6 +21,7 @@
 #define GRAPH_LOCK_H
 
 #include "qemu/osdep.h"
+#include "qemu/clang-tsa.h"
 
 #include "qemu/coroutine.h"
 
@@ -57,6 +58,35 @@
  */
 typedef struct BdrvGraphRWlock BdrvGraphRWlock;
 
+/* Dummy lock object to use for Thread Safety Analysis (TSA) */
+typedef struct TSA_CAPABILITY("graph-lock") BdrvGraphLock {
+} BdrvGraphLock;
+
+extern BdrvGraphLock graph_lock;
+
+/*
+ * clang doesn't check consistency in locking annotations between forward
+ * declarations and the function definition. Having the annotation on the
+ * definition, but not the declaration in a header file, may give the reader
+ * a false sense of security because the condition actually remains unchecked
+ * for callers in other source files.
+ *
+ * Therefore, as a convention, for public functions, GRAPH_RDLOCK and
+ * GRAPH_WRLOCK annotations should be present only in the header file.
+ */
+#define GRAPH_WRLOCK TSA_REQUIRES(graph_lock)
+#define GRAPH_RDLOCK TSA_REQUIRES_SHARED(graph_lock)
+
+/*
+ * TSA annotations are not part of function types, so checks are defeated when
+ * using a function pointer. As a workaround, annotate function pointers with
+ * this macro that will require that the lock is at least taken while reading
+ * the pointer. In most cases this is equivalent to actually protecting the
+ * function call.
+ */
+#define GRAPH_RDLOCK_PTR TSA_GUARDED_BY(graph_lock)
+#define GRAPH_WRLOCK_PTR TSA_GUARDED_BY(graph_lock)
+
 /*
  * register_aiocontext:
  * Add AioContext @ctx to the list of AioContext.
@@ -85,14 +115,14 @@ void unregister_aiocontext(AioContext *ctx);
  * This function polls. Callers must not hold the lock of any AioContext other
  * than the current one.
  */
-void bdrv_graph_wrlock(void);
+void bdrv_graph_wrlock(void) TSA_ACQUIRE(graph_lock) TSA_NO_TSA;
 
 /*
  * bdrv_graph_wrunlock:
  * Write finished, reset global has_writer to 0 and restart
  * all readers that are waiting.
  */
-void bdrv_graph_wrunlock(void);
+void bdrv_graph_wrunlock(void) TSA_RELEASE(graph_lock) TSA_NO_TSA;
 
 /*
  * bdrv_graph_co_rdlock:
@@ -116,7 +146,8 @@ void bdrv_graph_wrunlock(void);
  * loop) to take it and wait that the coroutine ends, so that
  * we always signal that a reader is running.
  */
-void coroutine_fn bdrv_graph_co_rdlock(void);
+void coroutine_fn TSA_ACQUIRE_SHARED(graph_lock) TSA_NO_TSA
+bdrv_graph_co_rdlock(void);
 
 /*
  * bdrv_graph_rdunlock:
@@ -124,7 +155,8 @@ void coroutine_fn bdrv_graph_co_rdlock(void);
  * If the writer is waiting for reads to finish (has_writer == 1), signal
  * the writer that we are done via aio_wait_kick() to let it continue.
  */
-void coroutine_fn bdrv_graph_co_rdunlock(void);
+void coroutine_fn TSA_RELEASE_SHARED(graph_lock) TSA_NO_TSA
+bdrv_graph_co_rdunlock(void);
 
 /*
  * bdrv_graph_rd{un}lock_main_loop:
@@ -132,8 +164,11 @@ void coroutine_fn bdrv_graph_co_rdunlock(void);
  * in the main loop. It is just asserting that we are not
  * in a coroutine and in GLOBAL_STATE_CODE.
  */
-void bdrv_graph_rdlock_main_loop(void);
-void bdrv_graph_rdunlock_main_loop(void);
+void TSA_ACQUIRE_SHARED(graph_lock) TSA_NO_TSA
+bdrv_graph_rdlock_main_loop(void);
+
+void TSA_RELEASE_SHARED(graph_lock) TSA_NO_TSA
+bdrv_graph_rdunlock_main_loop(void);
 
 /*
  * assert_bdrv_graph_readable:
@@ -150,6 +185,17 @@ void assert_bdrv_graph_readable(void);
  */
 void assert_bdrv_graph_writable(void);
 
+/*
+ * Calling this function tells TSA that we know that the lock is effectively
+ * taken even though we cannot prove it (yet) with GRAPH_RDLOCK. This can be
+ * useful in intermediate stages of a conversion to using the GRAPH_RDLOCK
+ * macro.
+ */
+static inline void TSA_ASSERT_SHARED(graph_lock) TSA_NO_TSA
+assume_graph_lock(void)
+{
+}
+
 typedef struct GraphLockable { } GraphLockable;
 
 /*
@@ -159,13 +205,21 @@ typedef struct GraphLockable { } GraphLockable;
  */
 #define GML_OBJ_() (&(GraphLockable) { })
 
-static inline GraphLockable *graph_lockable_auto_lock(GraphLockable *x)
+/*
+ * This is not marked as TSA_ACQUIRE() because TSA doesn't understand the
+ * cleanup attribute and would therefore complain that the graph is never
+ * unlocked. TSA_ASSERT() makes sure that the following calls know that we
+ * hold the lock while unlocking is left unchecked.
+ */
+static inline GraphLockable * TSA_ASSERT(graph_lock) TSA_NO_TSA
+graph_lockable_auto_lock(GraphLockable *x)
 {
     bdrv_graph_co_rdlock();
     return x;
 }
 
-static inline void graph_lockable_auto_unlock(GraphLockable *x)
+static inline void TSA_NO_TSA
+graph_lockable_auto_unlock(GraphLockable *x)
 {
     bdrv_graph_co_rdunlock();
 }
@@ -195,14 +249,20 @@ typedef struct GraphLockableMainloop { } GraphLockableMainloop;
  */
 #define GMLML_OBJ_() (&(GraphLockableMainloop) { })
 
-static inline GraphLockableMainloop *
+/*
+ * This is not marked as TSA_ACQUIRE() because TSA doesn't understand the
+ * cleanup attribute and would therefore complain that the graph is never
+ * unlocked. TSA_ASSERT() makes sure that the following calls know that we
+ * hold the lock while unlocking is left unchecked.
+ */
+static inline GraphLockableMainloop * TSA_ASSERT(graph_lock) TSA_NO_TSA
 graph_lockable_auto_lock_mainloop(GraphLockableMainloop *x)
 {
     bdrv_graph_rdlock_main_loop();
     return x;
 }
 
-static inline void
+static inline void TSA_NO_TSA
 graph_lockable_auto_unlock_mainloop(GraphLockableMainloop *x)
 {
     bdrv_graph_rdunlock_main_loop();
diff --git a/block/graph-lock.c b/block/graph-lock.c
index c4d9d2c274..454c31e691 100644
--- a/block/graph-lock.c
+++ b/block/graph-lock.c
@@ -24,6 +24,9 @@
 #include "block/block.h"
 #include "block/block_int.h"
 
+/* Dummy lock object to use for Thread Safety Analysis (TSA) */
+BdrvGraphLock graph_lock;
+
 /* Protects the list of aiocontext and orphaned_reader_count */
 static QemuMutex aio_context_list_lock;
 
-- 
2.38.1



  parent reply	other threads:[~2022-12-14 13:57 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-14 13:44 [PULL 00/51] Block layer patches Kevin Wolf
2022-12-14 13:44 ` [PULL 01/51] block: Inline bdrv_detach_child() Kevin Wolf
2022-12-14 13:44 ` [PULL 02/51] block: drop bdrv_remove_filter_or_cow_child Kevin Wolf
2022-12-14 13:44 ` [PULL 03/51] block: bdrv_refresh_perms(): allow external tran Kevin Wolf
2022-12-14 13:44 ` [PULL 04/51] block: refactor bdrv_list_refresh_perms to allow any list of nodes Kevin Wolf
2022-12-14 13:44 ` [PULL 05/51] qed: Don't yield in bdrv_qed_co_drain_begin() Kevin Wolf
2022-12-14 13:44 ` [PULL 06/51] test-bdrv-drain: Don't yield in .bdrv_co_drained_begin/end() Kevin Wolf
2022-12-14 13:44 ` [PULL 07/51] block: Revert .bdrv_drained_begin/end to non-coroutine_fn Kevin Wolf
2022-12-14 13:44 ` [PULL 08/51] block: Remove drained_end_counter Kevin Wolf
2022-12-14 13:44 ` [PULL 09/51] block: Inline bdrv_drain_invoke() Kevin Wolf
2022-12-14 13:44 ` [PULL 10/51] block: Fix locking for bdrv_reopen_queue_child() Kevin Wolf
2022-12-14 13:44 ` [PULL 11/51] block: Drain individual nodes during reopen Kevin Wolf
2022-12-14 13:44 ` [PULL 12/51] block: Don't use subtree drains in bdrv_drop_intermediate() Kevin Wolf
2022-12-14 13:44 ` [PULL 13/51] stream: Replace subtree drain with a single node drain Kevin Wolf
2022-12-14 13:44 ` [PULL 14/51] block: Remove subtree drains Kevin Wolf
2022-12-14 13:44 ` [PULL 15/51] block: Call drain callbacks only once Kevin Wolf
2022-12-14 13:44 ` [PULL 16/51] block: Remove ignore_bds_parents parameter from drain_begin/end Kevin Wolf
2022-12-14 13:44 ` [PULL 17/51] block: Drop out of coroutine in bdrv_do_drained_begin_quiesce() Kevin Wolf
2022-12-14 13:44 ` [PULL 18/51] block: Don't poll in bdrv_replace_child_noperm() Kevin Wolf
2022-12-14 13:44 ` [PULL 19/51] block: Remove poll parameter from bdrv_parent_drained_begin_single() Kevin Wolf
2022-12-14 13:44 ` [PULL 20/51] block-io: introduce coroutine_fn duplicates for bdrv_common_block_status_above callers Kevin Wolf
2022-12-14 13:44 ` [PULL 21/51] block-copy: add coroutine_fn annotations Kevin Wolf
2022-12-14 13:44 ` [PULL 22/51] nbd/server.c: " Kevin Wolf
2022-12-14 13:44 ` [PULL 23/51] block-backend: replace bdrv_*_above with blk_*_above Kevin Wolf
2022-12-14 13:44 ` [PULL 24/51] block/vmdk: add coroutine_fn annotations Kevin Wolf
2022-12-14 13:44 ` [PULL 25/51] block: avoid duplicating filename string in bdrv_create Kevin Wolf
2022-12-14 13:44 ` [PULL 26/51] block: distinguish between bdrv_create running in coroutine and not Kevin Wolf
2022-12-14 13:44 ` [PULL 27/51] block: bdrv_create_file is a coroutine_fn Kevin Wolf
2022-12-14 13:44 ` [PULL 28/51] block: rename generated_co_wrapper in co_wrapper_mixed Kevin Wolf
2022-12-14 13:44 ` [PULL 29/51] block-coroutine-wrapper.py: introduce co_wrapper Kevin Wolf
2022-12-14 13:44 ` [PULL 30/51] block-coroutine-wrapper.py: support functions without bs arg Kevin Wolf
2022-12-14 13:44 ` [PULL 31/51] block-coroutine-wrapper.py: support also basic return types Kevin Wolf
2022-12-14 13:44 ` [PULL 32/51] block: convert bdrv_create to co_wrapper Kevin Wolf
2022-12-14 13:44 ` [PULL 33/51] block/dirty-bitmap: convert coroutine-only functions " Kevin Wolf
2022-12-14 13:44 ` [PULL 34/51] block: Factor out bdrv_drain_all_begin_nopoll() Kevin Wolf
2022-12-14 13:44 ` [PULL 35/51] graph-lock: Introduce a lock to protect block graph operations Kevin Wolf
2022-12-14 13:44 ` [PULL 36/51] graph-lock: Implement guard macros Kevin Wolf
2022-12-14 13:44 ` [PULL 37/51] async: Register/unregister aiocontext in graph lock list Kevin Wolf
2022-12-14 13:44 ` [PULL 38/51] Import clang-tsa.h Kevin Wolf
2022-12-14 13:44 ` [PULL 39/51] clang-tsa: Add TSA_ASSERT() macro Kevin Wolf
2022-12-14 13:44 ` [PULL 40/51] clang-tsa: Add macros for shared locks Kevin Wolf
2022-12-14 13:44 ` [PULL 41/51] configure: Enable -Wthread-safety if present Kevin Wolf
2022-12-14 13:44 ` [PULL 42/51] test-bdrv-drain: Fix incorrrect drain assumptions Kevin Wolf
2022-12-14 13:44 ` [PULL 43/51] block: Fix locking in external_snapshot_prepare() Kevin Wolf
2022-12-14 13:44 ` [PULL 44/51] block: wrlock in bdrv_replace_child_noperm Kevin Wolf
2022-12-14 13:44 ` [PULL 45/51] block: remove unnecessary assert_bdrv_graph_writable() Kevin Wolf
2022-12-14 13:44 ` [PULL 46/51] block: assert that graph read and writes are performed correctly Kevin Wolf
2022-12-14 13:44 ` Kevin Wolf [this message]
2022-12-14 13:44 ` [PULL 48/51] Mark assert_bdrv_graph_readable/writable() GRAPH_RD/WRLOCK Kevin Wolf
2022-12-14 13:44 ` [PULL 49/51] block-coroutine-wrapper.py: introduce annotations that take the graph rdlock Kevin Wolf
2022-12-14 13:44 ` [PULL 50/51] block: use co_wrapper_mixed_bdrv_rdlock in functions taking the rdlock Kevin Wolf
2022-12-14 13:44 ` [PULL 51/51] block: GRAPH_RDLOCK for functions only called by co_wrappers Kevin Wolf
2022-12-14 22:35 ` [PULL 00/51] Block layer patches Peter Maydell
2022-12-15  9:44   ` Kevin Wolf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221214134453.31665-48-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).