mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will@kernel.org>, Christoph Hellwig <hch@lst.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Marco Elver <elver@google.com>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Nathan Chancellor <nathan@kernel.org>,
	Kees Cook <kees@kernel.org>, Jann Horn <jannh@google.com>,
	linux-kernel@vger.kernel.org,
	Bart Van Assche <bvanassche@acm.org>,
	Ingo Molnar <mingo@redhat.com>, Boqun Feng <boqun.feng@gmail.com>,
	Waiman Long <longman@redhat.com>
Subject: [PATCH RFC 03/33] locking: Introduce <linux/thread_safety.h>
Date: Thu,  6 Feb 2025 09:50:44 -0800	[thread overview]
Message-ID: <20250206175114.1974171-4-bvanassche@acm.org> (raw)
In-Reply-To: <20250206175114.1974171-1-bvanassche@acm.org>

Introduce a new kernel header with the Clang thread-safety attributes.
If:
- a struct that represents a synchronization object is annotated with the
  CAPABILITY() attribute,
- the operations on that synchronization object are annotated with the
  ACQUIRE() and RELEASE() attributes,
- if variables or members that should be guarded by a synchronization
  object are annotated with GUARDED_BY(),

then the Clang compiler verifies the following if -Wthread-safety is
enabled:
- Whether or not locking in a function implementation matches the
  thread-safety attributes in the function declaration. No annotation
  is necessary if a lock call is followed by an unlock call. For other
  patterns, annotation is required.
- Whether or not the requirements of the GUARDED_BY() annotations are
  met.

Some highlights from the Clang thread-safety attribute documentation:
- Alias analysis is not performed on thread-safety attribute arguments.
  Hence the expansion of some local variables in subsequent patches.
- Most thread-safety attributes affect the function interface.
  NO_THREAD_SAFETY_ANALYSIS only affects the function definition.
- If a private struct definition (in a .c file) includes a
  synchronization object, annotations of functions in .h files must
  not refer to the name of the private struct. A possible solution is
  to define a capability with DEFINE_CAPABILITY and to use the name of
  that capability in the thread-safety annotations.

A few notes from me:
- Thread-safety attributes are not included in function pointer types.
  In other words, when passing an annotated function as an argument to
  another function, the thread-safety attributes are discarded.
- Annotating conditional locking functions that return a pointer is not
  yet supported by Clang.

More information is available here:
https://clang.llvm.org/docs/ThreadSafetyAnalysis.html

In case anyone would be interested, the equivalent Qemu header file is
available here:
https://github.com/qemu/qemu/blob/master/include/qemu/clang-tsa.h

Cc: Ingo Molnar <mingo@redhat.com>
Cc: Will Deacon <will@kernel.org>
Cc: Boqun Feng <boqun.feng@gmail.com> (LOCKDEP & RUST)
Cc: Waiman Long <longman@redhat.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 include/linux/thread_safety.h | 141 ++++++++++++++++++++++++++++++++++
 1 file changed, 141 insertions(+)
 create mode 100644 include/linux/thread_safety.h

diff --git a/include/linux/thread_safety.h b/include/linux/thread_safety.h
new file mode 100644
index 000000000000..e23175223a18
--- /dev/null
+++ b/include/linux/thread_safety.h
@@ -0,0 +1,141 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _THREAD_SAFETY_H_
+#define _THREAD_SAFETY_H_
+
+/* See also https://clang.llvm.org/docs/ThreadSafetyAnalysis.html */
+
+/*
+ * Enable thread safety attributes only for clang. The attributes can be safely
+ * ignored when compiling with other compilers.
+ */
+#if defined(__clang__)
+#define THREAD_ANNOTATION_ATTRIBUTE_(...) __attribute__((__VA_ARGS__))
+#else
+#define THREAD_ANNOTATION_ATTRIBUTE_(...)
+#endif
+
+/*
+ * Macro for applying a capability as an attribute to a type definition.
+ * This macro can be used in struct definitions and also in typedefs.
+ * @x must be a string.
+ */
+#define CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE_(capability(x))
+
+/*
+ * Macro for defining a capability name that is not tied to an existing type.
+ * @capability_name is declared as an external variable. Any attempt to
+ * read or modify that external variable will result in a linker error.
+ */
+#define DEFINE_CAPABILITY(capability_name)	      \
+	extern const struct {} CAPABILITY(#capability_name) capability_name
+
+/*
+ * Attribute for structure members that declares that the structure members are
+ * protected by the given capability.
+ */
+#define GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE_(guarded_by(x))
+
+/*
+ * Attribute for pointer structure members that declares that the contents
+ * of these pointers are protected by the given capability.
+ */
+#define PT_GUARDED_BY(x) THREAD_ANNOTATION_ATTRIBUTE_(pt_guarded_by(x))
+
+/*
+ * Attribute for instances of data structures that declares that the given
+ * capabilities must be acquired before the annotated data structure.
+ */
+#define ACQUIRED_BEFORE(...)					\
+	THREAD_ANNOTATION_ATTRIBUTE_(acquired_before(__VA_ARGS__))
+
+/*
+ * Attribute for instances of data structures that declares that the given
+ * capabilities must be acquired after the annotated data structure.
+ */
+#define ACQUIRED_AFTER(...)					\
+	THREAD_ANNOTATION_ATTRIBUTE_(acquired_after(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that the caller must have exclusive access
+ * to the given capabilities.
+ */
+#define REQUIRES(...)							\
+	THREAD_ANNOTATION_ATTRIBUTE_(requires_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that the caller must have shared access
+ * to the given capabilities.
+ */
+#define REQUIRES_SHARED(...)						\
+	THREAD_ANNOTATION_ATTRIBUTE_(requires_shared_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that the function acquires the given
+ * capability.
+ */
+#define ACQUIRE(...)						\
+	THREAD_ANNOTATION_ATTRIBUTE_(acquire_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that the function acquires the given
+ * shared capability.
+ */
+#define ACQUIRE_SHARED(...)						\
+	THREAD_ANNOTATION_ATTRIBUTE_(acquire_shared_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that the function releases the given
+ * capability.
+ */
+#define RELEASE(...)						\
+	THREAD_ANNOTATION_ATTRIBUTE_(release_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that the function releases the given
+ * shared capability.
+ */
+#define RELEASE_SHARED(...)						\
+	THREAD_ANNOTATION_ATTRIBUTE_(release_shared_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that a function only acquires the given
+ * capability (2nd argument) for a given return value (first argument).
+ */
+#define TRY_ACQUIRE(...)						\
+	THREAD_ANNOTATION_ATTRIBUTE_(try_acquire_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that a function only acquires the given
+ * shared capability (2nd argument) for a given return value (first argument).
+ */
+#define TRY_ACQUIRE_SHARED(...)						\
+	THREAD_ANNOTATION_ATTRIBUTE_(try_acquire_shared_capability(__VA_ARGS__))
+
+/*
+ * Function attribute that declares that the caller must not hold the given
+ * capabilities.
+ */
+#define EXCLUDES(...) THREAD_ANNOTATION_ATTRIBUTE_(locks_excluded(__VA_ARGS__))
+
+/*
+ * Tell the compiler that the given capability is held.
+ */
+#define ASSERT_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE_(assert_capability(x))
+
+/*
+ * Tell the compiler that the given shared capability is held.
+ */
+#define ASSERT_SHARED_CAPABILITY(x)				\
+	THREAD_ANNOTATION_ATTRIBUTE_(assert_shared_capability(x))
+
+/*
+ * Function attribute that declares that a function returns a pointer to a
+ * capability.
+ */
+#define RETURN_CAPABILITY(x) THREAD_ANNOTATION_ATTRIBUTE_(lock_returned(x))
+
+/* Function attribute that disables thread-safety analysis. */
+#define NO_THREAD_SAFETY_ANALYSIS				\
+	THREAD_ANNOTATION_ATTRIBUTE_(no_thread_safety_analysis)
+
+#endif /* _THREAD_SAFETY_H_ */

  parent reply	other threads:[~2025-02-06 17:51 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-06 17:50 [PATCH RFC 00/33] Compile-time thread-safety checking Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 01/33] scsi, usb: Rename the RESERVE and RELEASE constants Bart Van Assche
2025-02-07  3:44   ` Christoph Hellwig
2025-02-06 17:50 ` [PATCH RFC 02/33] s390: Comment out the RELEASE constant Bart Van Assche
2025-02-06 17:50 ` Bart Van Assche [this message]
2025-02-07  3:53   ` [PATCH RFC 03/33] locking: Introduce <linux/thread_safety.h> Christoph Hellwig
2025-02-07  8:29     ` Marco Elver
2025-02-07 22:34     ` Bart Van Assche
2025-02-07 23:19       ` Marco Elver
2025-02-07 23:50         ` Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 04/33] include/linux/cleanup.h: Support thread-safety analysis Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 05/33] locking/mutex: Change the atomic_dec_and_mutex_lock() return type Bart Van Assche
2025-02-07  3:47   ` Christoph Hellwig
2025-02-06 17:50 ` [PATCH RFC 06/33] locking/mutex: Annotate struct mutex and mutex functions Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 07/33] driver core: Annotate locking functions in <linux/device.h> Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 08/33] kref: Add thread-safety annotations in <linux/kref.h> Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 09/33] refcount: Add thread-safety annotations in <linux/refcount.h> Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 10/33] treewide: Modify mutex_lock_interruptible() return value checks Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 11/33] PNP: isapnp: Check the isapnp_cfg_begin() return value Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 12/33] scsi: mpi3mr: Fix locking in an error path Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 13/33] scsi: mpt3sas: Fix a locking bug " Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 14/33] ice: Split ice_dcb_rebuild() Bart Van Assche
2025-02-06 22:01   ` Przemek Kitszel
2025-02-06 17:50 ` [PATCH RFC 15/33] ice: Fix a locking bug in an error path Bart Van Assche
2025-02-06 21:35   ` Tony Nguyen
2025-02-06 21:44     ` Bart Van Assche
2025-02-06 21:48       ` Tony Nguyen
2025-02-06 17:50 ` [PATCH RFC 16/33] net/mlx5e: Make the code easier to analyze Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 17/33] Input: synaptics-rmi4 - fix a locking bug in an error path Bart Van Assche
2025-02-06 17:50 ` [PATCH RFC 18/33] misc: nsm: Fix " Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 19/33] drm/amdgpu: Unlock a mutex before destroying it Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 20/33] drm/amdgpu: Fix a locking bug in an error path Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 21/33] drm/amdgpu: Fix locking bugs in error paths Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 22/33] drm: bridge: cdns-mhdp8546: Fix a locking bug in an error path Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 23/33] " Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 24/33] drm: zynqmp_dp: Fix a deadlock in zynqmp_dp_ignore_hpd_set() Bart Van Assche
2025-02-06 19:22   ` Sean Anderson
2025-02-06 20:24     ` Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 25/33] wifi: ath12k: Fix locking in error paths Bart Van Assche
2025-02-06 18:25   ` Jeff Johnson
2025-02-06 22:13     ` Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 26/33] mctp i3c: " Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 27/33] iavf: Fix a locking bug in an error path Bart Van Assche
2025-02-12  2:03   ` Jakub Kicinski
2025-02-06 17:51 ` [PATCH RFC 28/33] wifi: mt76: mt7925: " Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 29/33] hwmon: (it87) Check the it87_lock() return value Bart Van Assche
2025-02-06 22:51   ` Guenter Roeck
2025-02-06 23:34     ` Bart Van Assche
2025-02-06 23:41       ` Guenter Roeck
2025-02-09  5:04         ` Frank Crawford
2025-02-07  3:45     ` Christoph Hellwig
2025-02-06 17:51 ` [PATCH RFC 30/33] drivers/net/ethernet/marvell/octeontx2/nic: Fix locking in an error path Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 31/33] md/raid*: Fix raid*_set_queue_limits() Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 32/33] treewide: Annotate all struct mutex users Bart Van Assche
2025-02-06 17:51 ` [PATCH RFC 33/33] kbuild: clang: Unconditionally enable thread-safety checking Bart Van Assche
2025-02-06 18:20 ` [PATCH RFC 00/33] Compile-time " Marco Elver
2025-02-06 18:34   ` Bart Van Assche
2025-02-07  8:42     ` Peter Zijlstra
2025-02-07  9:05       ` Marco Elver
2025-02-07  9:08         ` Peter Zijlstra
2025-02-07  9:41           ` Marco Elver
2025-02-07 17:46           ` Bart Van Assche
2025-02-07 18:24             ` Marco Elver
2025-02-07 18:35               ` Bart Van Assche
2025-02-07 18:54                 ` Marco Elver
2025-02-07  3:44   ` Christoph Hellwig

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=20250206175114.1974171-4-bvanassche@acm.org \
    --to=bvanassche@acm.org \
    --cc=boqun.feng@gmail.com \
    --cc=elver@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hch@lst.de \
    --cc=jannh@google.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=peterz@infradead.org \
    --cc=will@kernel.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

all inboxes | Powered by JetHome®