public inbox for linux-toolchains@vger.kernel.org
 help / color / mirror / Atom feed
* Thread Safety Analysis and the Linux kernel
@ 2025-03-05 11:47 Marco Elver
  2025-03-05 23:54 ` Puchert, Aaron
  2025-05-05 13:44 ` Marco Elver
  0 siblings, 2 replies; 29+ messages in thread
From: Marco Elver @ 2025-03-05 11:47 UTC (permalink / raw)
  To: aaron.puchert, Aaron Ballman
  Cc: linux-toolchains, Peter Zijlstra, llvm, Bart Van Assche,
	Paul E. McKenney, Boqun Feng

Hi Aaron ^ 2,

[+Cc a bunch of folks that were involved in discussions.]

After sending v2 of the -Wthread-safety / Capability Analysis patches
for the Linux kernel [1], a number of concrete improvements to
-Wthread-safety will be required sooner or later. It is unclear if
they are blockers to the Linux kernel's adoption because the feature
is currently designed as "opt-in per subsystem", but it's clear the
current state of things is not ideal.

[1] https://lore.kernel.org/all/20250304092417.2873893-1-elver@google.com/

Peter Zijlstra's feedback:
https://lore.kernel.org/all/20250305112041.GA16878@noisy.programming.kicks-ass.net/
-- much of which led to the below requests. Peter also managed to
crash Clang, but that's probably unrelated to -Wthread-safety
directly: https://github.com/llvm/llvm-project/issues/129873

More straightforward requests:

1. Re-entrant acquires: rcu_read_lock(), preempt_disable(), etc. are
all re-entrant locks. My proposal is to introduce an attribute that
can be added to "ACQUIRE(..)" annotated functions to indicate they are
re-entrant. Release-count needs to then match acquire-count to fully
release a capability.

2. Basic alias analysis, e.g. when storing a pointer to a lock in a
function-local variable. [Complaint in:
https://lore.kernel.org/all/569186c5-8663-43df-a01c-d543f57ce5ca@kernel.org/
-- quote: "Fix the analyzer instead."]

3. Ability to refer to locks in returned reference/pointer. For example:
    struct foo *ret_lock_struct(void) ACQUIRE(return->somelock);
    struct foo *try_ret_lock_struct(void) TRY_ACQUIRE(1,
return->somelock); // locked if non-NULL
I expect this also requires basic alias analysis to work so that
assigning the returned pointer to a function-local variable and then
later use in an unlock function works as expected.

More complex requests:

4. The ability to deal with conditional locking with return values
that are not just true/non-zero and false/zero. A concrete case here
is that a lot of functions return error codes, and if the error code
is < 0, no lock is taken. If the error code is >= 0, the lock is
taken. [ Source:
https://lore.kernel.org/all/569186c5-8663-43df-a01c-d543f57ce5ca@kernel.org/
]

5. Better control-flow handling. Basic understanding of conditional
locking, which is explicitly ruled out in:
https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#conditional-locks
- however, if there's some way to even get basic support, would vastly
improve things for the kernel.

Some of these are more complex than others, and any hints how we might
go about this are appreciated. I'm happy to try and implement some of
them, but if you find that you already know exactly how you'd like an
implementation to look like, rough drafts that we can take over and
polish would be very very helpful, too!

In general, the Linux kernel has some of the most complex
synchronization patterns with numerous synchronization primitives.
Getting this to work for a good chunk of the more complex
synchronization code in the kernel will be quite the achievement if we
get there. :-)

Thanks,
-- Marco

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

end of thread, other threads:[~2025-09-18 11:10 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-05 11:47 Thread Safety Analysis and the Linux kernel Marco Elver
2025-03-05 23:54 ` Puchert, Aaron
2025-03-06  9:47   ` Peter Zijlstra
2025-03-06 16:18     ` Bart Van Assche
2025-03-07  8:07       ` Peter Zijlstra
2025-03-07 21:50       ` Puchert, Aaron
2025-03-07 21:46     ` Puchert, Aaron
2025-03-06 10:08   ` Peter Zijlstra
2025-03-06 22:18     ` Puchert, Aaron
2025-03-07  7:59       ` Peter Zijlstra
2025-03-07 14:13         ` Peter Zijlstra
2025-03-06 10:37   ` Peter Zijlstra
2025-03-06 23:14     ` Puchert, Aaron
2025-03-07  8:52       ` Peter Zijlstra
2025-03-07 12:52         ` Peter Zijlstra
2025-03-07 14:22           ` Greg Kroah-Hartman
2025-03-07 14:35             ` Peter Zijlstra
2025-03-08  6:06               ` Greg Kroah-Hartman
2025-03-07 23:03         ` Puchert, Aaron
2025-03-06 17:11   ` Paul E. McKenney
2025-03-06 23:24     ` Puchert, Aaron
2025-03-06 23:44       ` Paul E. McKenney
2025-03-07 17:59         ` Puchert, Aaron
2025-03-07 18:24           ` Paul E. McKenney
2025-03-07 12:00   ` Marco Elver
2025-05-05 13:44 ` Marco Elver
2025-06-05 12:44   ` Marco Elver
2025-09-18 10:37     ` Marco Elver
2025-09-18 11:10       ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox