locking: SIX locks (shared/intent/exclusive)
PATCH v2 1/1 locking: SIX locks (shared/intent/exclusive)
New lock for bcachefs, like read/write locks but with a third state, intent.
Intent locks conflict with each other, but not with read locks; taking a write lock requires first holding an intent lock.
------------------
The previously posted version was not up to date with the version in the bcachefs tree at time of posting and was only the first patch in the series - whoops. The changelog below will note what changes should have been included with previous posting and what is new work since previous posting - that starts at "six_lock_readers_add()", and "kill six_lock_pcpu_(alloc|free)".
The previously mentioned percpu-reader mode lost wakeup _may_ be resolved - I've had this patch series running in my CI all weekend with an extra patch to enable percpu readear mode for all locks, and I haven't seen it; no tests have been timing out. However, it was always very specific about under what conditions it would trigger and was very sporadic, so I'm not making any definitive claims yet.
Full version history at:
https://evilpiepirate.org/git/bcachefs.git/log/?h=six_locks
Changes since last version
- Improve six_lock_count - now includes write count, also correctly counts intent_lock_recurse
- Delete six_lock_pcpu_free_rcu()
completely broken interface, it's not possible to safely add/remove rcu readers to a lock that's in use
- Simplify wait lists
This switches to a single list of waiters, instead of separate lists for read and intent, and switches write locks to also use the wait lists instead of being handled differently.
Also, removal from the wait list is now done by the process waiting on the lock, not the process doing the wakeup. This is needed for the new deadlock cycle detector - we need tasks to stay on the waitlist until they've successfully acquired the lock.
- six_lock_waiter()
This allows passing in the wait list entry, which makes it possible for an upper layer to implement cycle detection for deadlock avoidance.
- Add start_time to six_lock_waiter
This adds a timestamp to six_lock_waiter, and also guarantees that timestamps on any given waitlist are monotonically increasing: this allows the graph traversal algorithm to use the timestamp as a loop cursor.
- Enable lockdep
Previously, bcachefs was using lockdep_novalidate_class because it wasn't possible for lockdep to check the ordering of its btree node six locks w.r.t. each other. However, with the either the lockdep_set_no_check_recursion() patch or nest_lock we can now enable lockdep checking.
- Fix a lost wakeup
There was a lost wakeup between a read unlock in percpu mode and a write lock. The unlock path unlocks, then executes a barrier, then checks for waiters; correspondingly, the lock side should set the wait bit and execute a barrier, then attempt to take the lock.
- Wakeup now takes lock on behalf of waiter
This brings back an important optimization to avoid touching the wait lists an extra time, while preserving the property that a thread is on a lock waitlist iff it is waiting - it is never removed from the waitlist until it has the lock. This guarantee is needed for the cycle detector in bcachefs.
- Expose tracepoint IP
This adds _ip variations of the various lock functions that allow an IP to be passed in, which is used by lockstat.
- Improved optimistic spinning
This adds a threshold for the maximum spin time, similar to the rwsem code, and a flag to the lock itself indicating when we've spun too long so other threads also refrain from spinning.
- Lock contended tracepoints
This adds calls to trace_contention_begin() and trace_contention_end().
- Simplify six_lock_counts()
replace open coding with call to pcpu_read_count()
NEW WORK SINCE LAST POSTING STARTS HERE:
----------------------------------------
- six_lock_readers_add()
This moves a helper out of the bcachefs code that shouldn't have been there, since it touches six lock internals.
- Kill six_lock_pcpu_(alloc|free)
six_lock_pcpu_alloc() is an unsafe interface: it's not safe to allocate or free the percpu reader count on an existing lock that's in use, the only safe time to allocate percpu readers is when the lock is first being initialized.
This patch adds a flags parameter to six_lock_init(), and instead of six_lock_pcpu_free() we now expose six_lock_exit(), which does the same thing but is less likely to be misused.
- Centralize setting of waiting bit
Originally, the waiting bit was always set by trylock() on failure:
however, it's now set by __six_lock_type_slowpath(), with wait_lock held - which is the more correct place to do it.
That made setting the waiting bit in trylock redundant, so this patch deletes that.
- Simplify dispatch
Originally, we used inlining/flattening to cause the compiler to generate different versions of lock/trylock/relock/unlock for each lock type - read, intent, and write. This made the individual functions smaller and let the compiler eliminate table lookups:
however, as the code has gotten more complicated these optimizations have gotten less worthwhile, and all the tricky inlining and dispatching made the code less readable.
Text size: 11015 bytes -> 7467 bytes, and benchmarks show no loss of performance.
- Kill six_lock_state union
As suggested by Linus, this drops the six_lock_state union in favor of raw bitmasks.
On the one hand, bitfields give more type-level structure to the code. However, a significant amount of the code was working with six_lock_state as a u64/atomic64_t, and the conversions from the bitfields to the u64 were deemed a bit too out-there.
More significantly, because bitfield order is poorly defined (#ifdef __LITTLE_ENDIAN_BITFIELD can be used, but is gross), incrementing the sequence number would overflow into the rest of the bitfield if the compiler didn't put the sequence number at the high end of the word.
The new code is a bit saner when we're on an architecture without real atomic64_t support - all accesses to lock->state now go through atomic64_*() operations.
On architectures with real atomic64_t support, we additionally use atomic bit ops for setting/clearing individual bits.
Text size: 7467 bytes -> 4649 bytes - compilers still suck at bitfields.
- Documentation, renaming
- Expanded and revamped overview documentation in six.h, giving an overview of all features
- docbook-comments for all external interfaces
- Rename some functions for simplicity, i.e.
six_lock_ip_type() -> six_lock_ip()
- Improve spurious wakeup handling in pcpu reader mode
Minor refactoring for consistency, significantly expand code comments
- Split out seq, use atomic_t and kill dependency on atomic64_t
The lock sequence number doesn't actually need to be part of the main lock state if we stop using it to track whether a write lock is held.
We already have a bit in the rest of the lock state for "trying to acquire a write lock", so that writers are unfairly preferred over readers. This changes the meaning of that bit to "write lock being acquired or held", and incrementing the sequence number is now a separate, non atomic operation that happens just prior to a write unlock.
This actually simplifies the implementation of relock(); it's now just "check sequence number; trylock(), recheck sequence number".
And since the "write lock held" state now works like a counter not a sequence number - like read and intent - this gets us some other small cleanups/simplifications.
--- include/linux/six.h | 388 +++++++++++++++++ kernel/Kconfig.locks | 3 + kernel/locking/Makefile | 1 + kernel/locking/six.c | 929 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 1321 insertions(+) create mode 100644 include/linux/six.h create mode 100644 kernel/locking/six.c
include/linux/six.h new file +388 -0
@@ -0,0 +1,388 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_SIX_H +#define _LINUX_SIX_H + +/** +* DOC: SIX locks overview +* +* Shared/intent/exclusive locks: sleepable read/write locks, like rw semaphores +* but with an additional state: read/shared, intent, exclusive/write +* +* The purpose of the intent state is to allow for greater concurrency on tree +* structures without deadlocking. In general, a read can't be upgraded to a +* write lock without deadlocking, so an operation that updates multiple nodes +* will have to take write locks for the full duration of the operation. +* +* But by adding an intent state, which is exclusive with other intent locks but +* not with readers, we can take intent locks at thte start of the operation, +* and then take write locks only for the actual update to each individual +* nodes, without deadlocking. +* +* Example usage: +* six_lock_read(&foo->lock); +* six_unlock_read(&foo->lock); +* +* An intent lock must be held before taking a write lock: +* six_lock_intent(&foo->lock); +* six_lock_write(&foo->lock); +* six_unlock_write(&foo->lock); +* six_unlock_intent(&foo->lock); +* +* Other operations: +* six_trylock_read() +* six_trylock_intent() +* six_trylock_write() +* +* six_lock_downgrade() convert from intent to read +* six_lock_tryupgrade() attempt to convert from read to intent, may fail +* +* There are also interfaces that take the lock type as an enum: +* +* six_lock_type(&foo->lock, SIX_LOCK_read); +* six_trylock_convert(&foo->lock, SIX_LOCK_read, SIX_LOCK_intent) +* six_lock_type(&foo->lock, SIX_LOCK_write); +* six_unlock_type(&foo->lock, SIX_LOCK_write); +* six_unlock_type(&foo->lock, SIX_LOCK_intent); +* +* Lock sequence numbers - unlock(), relock(): +* +* Locks embed sequences numbers, which are incremented on write lock/unlock. +* This allows locks to be dropped and the retaken iff the state they protect +* hasn't changed; this makes it much easier to avoid holding locks while e.g. +* doing IO or allocating memory. +* +* Example usage: +* six_lock_read(&foo->lock); +* u32 seq = six_lock_seq(&foo->lock); +* six_unlock_read(&foo->lock); +* +* some_operation_that_may_block(); +* +* if (six_relock_read(&foo->lock, seq)) { ... } +* +* If the relock operation succeeds, it is as if the lock was never unlocked. +* +* Reentrancy: +* +* Six locks are not by themselves reentrent, but have counters for both the +* read and intent states that can be used to provide reentrency by an upper +* layer that tracks held locks. If a lock is known to already be held in the +* read or intent state, six_lock_increment() can be used to bump the "lock +* held in this state" counter, increasing the number of unlock calls that +* will be required to fully unlock it. +* +* Example usage: +* six_lock_read(&foo->lock); +* six_lock_increment(&foo->lock, SIX_LOCK_read); +* six_unlock_read(&foo->lock); +* six_unlock_read(&foo->lock); +* foo->lock is now fully unlocked. +* +* Since the intent state supercedes read, it's legal to increment the read +* counter when holding an intent lock, but not the reverse. +* +* A lock may only be held once for write: six_lock_increment(.., SIX_LOCK_write) +* is not legal. +* +* should_sleep_fn: +* +* There is a six_lock() variant that takes a function pointer that is called +* immediately prior to schedule() when blocking, and may return an error to +* abort. +* +* One possible use for this feature is when objects being locked are part of +* a cache and may reused, and lock ordering is based on a property of the +* object that will change when the object is reused - i.e. logical key order. +* +* If looking up an object in the cache may race with object reuse, and lock +* ordering is required to prevent deadlock, object reuse may change the +* correct lock order for that object and cause a deadlock. should_sleep_fn +* can be used to check if the object is still the object we want and avoid +* this deadlock. +* +* Wait list entry interface: +* +* There is a six_lock() variant, six_lock_waiter(), that takes a pointer to a +* wait list entry. By embedding six_lock_waiter into another object, and by +* traversing lock waitlists, it is then possible for an upper layer to +* implement full cycle detection for deadlock avoidance. +* +* should_sleep_fn should be used for invoking the cycle detector, walking the +* graph of held locks to check for a deadlock. The upper layer must track +* held locks for each thread, and each thread's held locks must be reachable +* from its six_lock_waiter object. +* +* six_lock_waiter() will add the wait object to the waitlist re-trying taking +* the lock, and before calling should_sleep_fn, and the wait object will not +* be removed from the waitlist until either the lock has been successfully +* acquired, or we aborted because should_sleep_fn returned an error. +* +* Also, six_lock_waiter contains a timestamp, and waiters on a waitlist will +* have timestamps in strictly ascending order - this is so the timestamp can +* be used as a cursor for lock graph traverse. +*/ + +#include <linux/lockdep.h> +#include <linux/osq_lock.h> +#include <linux/sched.h> +#include <linux/types.h> + +enum six_lock_type { + SIX_LOCK_read, + SIX_LOCK_intent, + SIX_LOCK_write, +}; + +struct six_lock { +atomic_t state; +u32 seq; +unsigned intent_lock_recurse; +struct task_struct *owner; +unsigned __percpu *readers; +struct optimistic_spin_queue osq; +raw_spinlock_t wait_lock; +struct list_head wait_list; +#ifdef CONFIG_DEBUG_LOCK_ALLOC +struct lockdep_map dep_map; +#endif +}; + +struct six_lock_waiter { +struct list_head list; +struct task_struct *task; +enum six_lock_type lock_want; +bool lock_acquired; +u64 start_time; +}; + +typedef int (*six_lock_should_sleep_fn )(struct six_lock *lock,void *); + +void six_lock_exit (struct six_lock *lock); + +enum six_lock_init_flags { +SIX_LOCK_INIT_PCPU =1U <<0 , +}; + +void __six_lock_init (struct six_lock *lock,const char *name, +struct lock_class_key *key,enum six_lock_init_flags flags); + +/** +* six_lock_init - initialize a six lock +* @lock: lock to initialize +* @flags: optional flags, i.e. SIX_LOCK_INIT_PCPU +*/ +#define six_lock_init (lock, flags) \ +do { \ + static struct lock_class_key __key; \ + \ + __six_lock_init((lock), #lock, &__key, flags); \ +} while (0) + +/** +* six_lock_seq - obtain current lock sequence number +* @lock: six_lock to obtain sequence number for +* +* @lock should be held for read or intent, and not write +* +* By saving the lock sequence number, we can unlock @lock and then (typically +* after some blocking operation) attempt to relock it: the relock will succeed +* if the sequence number hasn't changed, meaning no write locks have been taken +* and state corresponding to what @lock protects is still valid. +*/ +static inline u32 six_lock_seq (const struct six_lock *lock) +{ +return lock->seq; +} + +bool six_trylock_ip (struct six_lock *lock,enum six_lock_type type,unsigned long ip); + +/** +* six_trylock_type - attempt to take a six lock without blocking +* @lock: lock to take +* @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write +* +* Return: true on success, false on failure. +*/ +static inline bool six_trylock_type (struct six_lock *lock,enum six_lock_type type) +{ +return six_trylock_ip (lock, type, _THIS_IP_); +} + +int six_lock_ip_waiter (struct six_lock *lock,enum six_lock_type type, +struct six_lock_waiter *wait, +six_lock_should_sleep_fn should_sleep_fn,void *p, +unsigned long ip); + +/** +* six_lock_waiter - take a lock, with full waitlist interface +* @lock: lock to take +* @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write +* @wait: pointer to wait object, which will be added to lock's waitlist +* @should_sleep_fn: callback run after adding to waitlist, immediately prior +* to scheduling +* @p: passed through to @should_sleep_fn +* +* This is a convenience wrapper around six_lock_ip_waiter(), see that function +* for full documentation. +* +* Return: 0 on success, or the return code from @should_sleep_fn on failure. +*/ +static inline int six_lock_waiter (struct six_lock *lock,enum six_lock_type type, +struct six_lock_waiter *wait, +six_lock_should_sleep_fn should_sleep_fn,void *p) +{ +return six_lock_ip_waiter (lock, type, wait, should_sleep_fn, p, _THIS_IP_); +} + +/** +* six_lock_ip - take a six lock lock +* @lock: lock to take +* @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write +* @should_sleep_fn: callback run after adding to waitlist, immediately prior +* to scheduling +* @p: passed through to @should_sleep_fn +* @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_ +* +* Return: 0 on success, or the return code from @should_sleep_fn on failure. +*/ +static inline int six_lock_ip (struct six_lock *lock,enum six_lock_type type, +six_lock_should_sleep_fn should_sleep_fn,void *p, +unsigned long ip) +{ +struct six_lock_waiter wait; + +return six_lock_ip_waiter (lock, type, &wait, should_sleep_fn, p, ip); +} + +/** +* six_lock_type - take a six lock lock +* @lock: lock to take +* @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write +* @should_sleep_fn: callback run after adding to waitlist, immediately prior +* to scheduling +* @p: passed through to @should_sleep_fn +* +* Return: 0 on success, or the return code from @should_sleep_fn on failure. +*/ +static inline int six_lock_type (struct six_lock *lock,enum six_lock_type type, +six_lock_should_sleep_fn should_sleep_fn,void *p) +{ +struct six_lock_waiter wait; + +return six_lock_ip_waiter (lock, type, &wait, should_sleep_fn, p, _THIS_IP_); +} + +bool six_relock_ip (struct six_lock *lock,enum six_lock_type type, +unsigned seq,unsigned long ip); + +/** +* six_relock_type - attempt to re-take a lock that was held previously +* @lock: lock to take +* @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write +* @seq: lock sequence number obtained from six_lock_seq() while lock was +* held previously +* +* Return: true on success, false on failure. +*/ +static inline bool six_relock_type (struct six_lock *lock,enum six_lock_type type, +unsigned seq) +{ +return six_relock_ip (lock, type, seq, _THIS_IP_); +} + +void six_unlock_ip (struct six_lock *lock,enum six_lock_type type,unsigned long ip); + +/** +* six_unlock_type - drop a six lock +* @lock: lock to unlock +* @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write +* +* When a lock is held multiple times (because six_lock_incement()) was used), +* this decrements the 'lock held' counter by one. +* +* For example: +* six_lock_read(&foo->lock); read count 1 +* six_lock_increment(&foo->lock, SIX_LOCK_read); read count 2 +* six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 1 +* six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 0 +*/ +static inline void six_unlock_type (struct six_lock *lock,enum six_lock_type type) +{ +six_unlock_ip (lock, type, _THIS_IP_); +} + +#define __SIX_LOCK (type) \ +static inline bool six_trylock_ip_##type(struct six_lock *lock, unsigned long ip)\ +{ \ + return six_trylock_ip(lock, SIX_LOCK_##type, ip); \ +} \ + \ +static inline bool six_trylock_##type(struct six_lock *lock) \ +{ \ + return six_trylock_ip(lock, SIX_LOCK_##type, _THIS_IP_); \ +} \ + \ +static inline int six_lock_ip_waiter_##type(struct six_lock *lock, \ + struct six_lock_waiter *wait, \ + six_lock_should_sleep_fn should_sleep_fn, void *p,\ + unsigned long ip) \ +{ \ + return six_lock_ip_waiter(lock, SIX_LOCK_##type, wait, should_sleep_fn, p, ip);\ +} \ + \ +static inline int six_lock_ip_##type(struct six_lock *lock, \ + six_lock_should_sleep_fn should_sleep_fn, void *p, \ + unsigned long ip) \ +{ \ + return six_lock_ip(lock, SIX_LOCK_##type, should_sleep_fn, p, ip);\ +} \ + \ +static inline bool six_relock_ip_##type(struct six_lock *lock, u32 seq, unsigned long ip)\ +{ \ + return six_relock_ip(lock, SIX_LOCK_##type, seq, ip); \ +} \ + \ +static inline bool six_relock_##type(struct six_lock *lock, u32 seq) \ +{ \ + return six_relock_ip(lock, SIX_LOCK_##type, seq, _THIS_IP_); \ +} \ + \ +static inline int six_lock_##type(struct six_lock *lock, \ + six_lock_should_sleep_fn fn, void *p)\ +{ \ + return six_lock_ip_##type(lock, fn, p, _THIS_IP_); \ +} \ + \ +static inline void six_unlock_ip_##type(struct six_lock *lock, unsigned long ip) \ +{ \ + six_unlock_ip(lock, SIX_LOCK_##type, ip); \ +} \ + \ +static inline void six_unlock_##type(struct six_lock *lock) \ +{ \ + six_unlock_ip(lock, SIX_LOCK_##type, _THIS_IP_); \ +} + +__SIX_LOCK(read) +__SIX_LOCK(intent) +__SIX_LOCK(write) +#undef __SIX_LOCK + +void six_lock_downgrade (struct six_lock *); +bool six_lock_tryupgrade (struct six_lock *); +bool six_trylock_convert (struct six_lock *,enum six_lock_type , +enum six_lock_type ); + +void six_lock_increment (struct six_lock *,enum six_lock_type ); + +void six_lock_wakeup_all (struct six_lock *); + +struct six_lock_count { +unsigned n[3 ]; +}; + +struct six_lock_count six_lock_counts (struct six_lock *); +void six_lock_readers_add (struct six_lock *,int ); + +#endif /* _LINUX_SIX_H */
kernel/Kconfig.locks +3 -0
@@ -259,3 +259,6 @@ config ARCH_HAS_MMIOWB config MMIOWB def_bool y if ARCH_HAS_MMIOWB depends on SMP + +config SIXLOCKS + bool
kernel/locking/Makefile +1 -0
@@ -32,3 +32,4 @@ obj-$(CONFIG_QUEUED_RWLOCKS) += qrwlock.o obj-$(CONFIG_LOCK_TORTURE_TEST) += locktorture.o obj-$(CONFIG_WW_MUTEX_SELFTEST) += test-ww_mutex.o obj-$(CONFIG_LOCK_EVENT_COUNTS) += lock_events.o +obj-$(CONFIG_SIXLOCKS) += six.o
kernel/locking/six.c new file +929 -0
929 lines not on this page · raw message
Signature · 1 line
2.40.1
kernel/locking/six.c line 95
Quoted 10 lines · Kent Overstreet
kernel/locking/six.c new file +8
-0+static inline unsigned u32_mask_to_ulong_bitnr (u32 mask) +{ +union ulong_u32 { +u32 v32; +ulong vlong; + } v = { .v32 = mask }; + +return ilog2 (v.vlong);
No, this is still wrong.
The above is actively undefined - the high bits of 'vlong' can contain random garbage. And you can't even fix it anyway, because even if you add a second 32-bit word and zero it, on big-endian architectures you'll get a result that is bigger than 32, and then when you do this:L
kernel/locking/six.c line 103
Quoted 6 lines
kernel/locking/six.c new file +6
-0+static inline void six_set_bitmask (struct six_lock *lock,u32 mask) +{ +unsigned bitnr =u32_mask_to_ulong_bitnr (mask); + +if (!test_bit (bitnr, (unsigned long *) &lock->state)) +set_bit (bitnr, (unsigned long *) &lock->state);
you're back to basically just undefined behaviour.
You *cannot* do "set_bit()" on a u32. It's that simple. Stop trying to do it with these wrappers that happen to work on x86 but are fundamentally broken.
We don't do locking by playing games like this. It's wrong.
You clearly don't even want the return value, so you're actually much better off just using an atomic_t and "atomic_or()", I suspect.
But these broken games with casting pointers to invalid types MUST END.
Locking is subtle enough without doing clearly bogus things that depend on byte order and word size, and that aren't defined for the type you want to use.
Linus
Quoted 15 lines · Linus Torvalds
kernel/locking/six.c line 95
Quoted 10 lines · Kent Overstreet
kernel/locking/six.c new file +8
-0+static inline unsigned u32_mask_to_ulong_bitnr (u32 mask) +{ +union ulong_u32 { +u32 v32; +ulong vlong; + } v = { .v32 = mask }; + +return ilog2 (v.vlong);No, this is still wrong.
Quoted 5 lines
The above is actively undefined - the high bits of 'vlong' can contain random garbage. And you can't even fix it anyway, because even if you add a second 32-bit word and zero it, on big-endian architectures you'll get a result that is bigger than 32, and then when you do this:L
Uh, I think you're wrong on this one - structs with designated initializers have unspecified fields initialized to 0, and I would expect the same to hold for unions, and the language in the C standard isn't completely explicit but it appears to apply to both.
And checking the generated assembly for a six_set_nospin() that calls a six_set_bitmask() with the test_bit() taken out, for simplicity
00000000000002c0 <six_set_nospin>:
2c0: e8 00 00 00 00 call 2c5 <six_set_nospin+0x5> 2c5: 55 push %rbp 2c6: 48 89 e5 mov %rsp,%rbp 2c9: f0 80 4f 03 80 lock orb $0x80,0x3(%rdi) 2ce: 5d pop %rbp 2cf: c3 ret
meaning the compiler properly constant-propagated and didn't read uninitialized memory. And I did the same test in userspace too, in the unlikely event -ftrivial-auto-var-init=zero was affecting things.
Quoted 9 lines
kernel/locking/six.c line 103
Quoted 6 lines
kernel/locking/six.c new file +6
-0+static inline void six_set_bitmask (struct six_lock *lock,u32 mask) +{ +unsigned bitnr =u32_mask_to_ulong_bitnr (mask); + +if (!test_bit (bitnr, (unsigned long *) &lock->state)) +set_bit (bitnr, (unsigned long *) &lock->state);you're back to basically just undefined behaviour.
Quoted 3 lines
You *cannot* do "
set_bit()" on a u32. It's that simple. Stop trying to do it with these wrappers that happen to work on x86 but are fundamentally broken.
Because of aliasing issues? I thought it had been declared that the kernel would never do strict aliasing.
Quoted 6 lines
We don't do locking by playing games like this. It's wrong.
You clearly don't even want the return value, so you're actually much better off just using an
atomic_tand "atomic_or()", I suspect.But these broken games with casting pointers to invalid types MUST END.
...but, atomic_or() is clearly the right way to do this :)
Quoted 3 lines · Kent Overstreet
Uh, I think you're wrong on this one - structs with designated initializers have unspecified fields initialized to 0
That's definitely true for unspecified fields.
But there *are* no fields. Only padding.
That said, gcc does seem to always initialize the whole thing. We even rely on it when it comes to structures, but I'm not sure it's strictly standardized - and I'm particularly not sure about unions.
But there are counter-examples, like this:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104607
so maybe even structures aren't always initialized fully?
Anyway, I *really* don't want locking to have something that isn't obviously true. And it's not obviously true what the union behavior is.
Quoted 3 lines
I would
expect the same to hold for unions, and the language in the C standard isn't completely explicit but it appears to apply to both.
Only the named member (or, if no name, first) of a union is initialized, not all members. The rest is padding that may or may not be initialized depending on how you read the standard.
Quoted 2 lines
And checking the generated assembly for a
six_set_nospin()that calls asix_set_bitmask()with thetest_bit()taken out, for simplicity
Random compiler issue, *and* you're testing on a little-endian platform that wouldn't show the issue anyway.
Quoted 2 lines
meaning the compiler properly constant-propagated and didn't read uninitialized memory.
Again, that only works because it's little-endian, so the low bits of "vlong" are the same as the low bits of "v32".
Which is not true on BE.
On BE, it *might* work, because who knows what the compiler does for the padding bytes, but again, it's broken.
It also doesn't even *matter*, because the initialization is only *one* of the multiple problems here:
Quoted 6 lines
Quoted 3 lines
You *cannot* do "
set_bit()" on a u32. It's that simple. Stop trying to do it with these wrappers that happen to work on x86 but are fundamentally broken.Because of aliasing issues? I thought it had been declared that the kernel would never do strict aliasing.
No, Because of *alignment* issues.
An u32 is 4-byte aligned. But "set_bit()" requires "long" alignmnent,.
Again, that doesn't happen to matter on x86, at least when the "set_bit()" ends up being done as an "orb".
But it will basically not work on other acrchitectures.
You really can't just randomly cast pointers in locking code and think it makes things ok just because the compiler doesn't complain.
There's a *reason* that "set_bit()" takes a "long *" value, not a "void *" value. It's literally part of the semantics of the thing, and a cast does not make things magically ok.
So STOP DOING THOSE UNION TRICKS.
They aren't the "cure trick" you seem to think they are.
They are only broken garbage.
Linus
Quoted 14 lines · Linus Torvalds
Quoted 3 lines · Kent Overstreet
Uh, I think you're wrong on this one - structs with designated initializers have unspecified fields initialized to 0
That's definitely true for unspecified fields.
But there *are* no fields. Only padding.
That said, gcc does seem to always initialize the whole thing. We even rely on it when it comes to structures, but I'm not sure it's strictly standardized - and I'm particularly not sure about unions.
Quoted 5 lines
But there are counter-examples, like this:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104607so maybe even structures aren't always initialized fully?
Yeah, I have to concede - if we can't rely on struct padding being initialized we can't rely on union padding either.
Quoted 3 lines
No, Because of *alignment* issues.
An u32 is 4-byte aligned. But "
set_bit()" requires "long" alignmnent,.
Ah - right...
On Mon, May 22, 2023 at 01:13:14PM -0400, Kent Overstreet wrote:
[...]
Quoted 6 lines
+/*
+ * bits 0-26 reader count
+ * bits 26-27write_locking(a thread is trying to get a write lock,
+ * but does not have one yet)
+ * bits 27-28 held for intent
+ * bits 28-29 nospin - optimistic spinning has timed out
Still reading to understand the design (and try to find where are the first five locks ;-)), but this comment seems to be wrong, because..
kernel/locking/six.c line 45
Quoted 13 lines
+ * bits 29-30 has read waiters
+ * bits 30-31 has intent waiters
+ * bits 31-32 has write waiters
+ */kernel/locking/six.c new file +9
-0+ +#define SIX_LOCK_HELD_read_OFFSET 0 +#define SIX_LOCK_HELD_read ~(~0U << 26) +#define SIX_LOCK_HELD_intent (1U << 26) +#define SIX_LOCK_HELD_write (1U << 27) +#define SIX_LOCK_WAITING_read (1U << (28 + SIX_LOCK_read)) +#define SIX_LOCK_WAITING_intent (1U << (28 + SIX_LOCK_intent)) +#define SIX_LOCK_WAITING_write (1U << (28 + SIX_LOCK_write)) +#define SIX_LOCK_NOSPIN (1U << 31)
^ NOSPIN is the 31st bit.
Regards,
Boqun
Quoted 7 lines · Boqun Feng
Quoted 7 lines
+ * bits 29-30 has read waiters
+ * bits 30-31 has intent waiters
+ * bits 31-32 has write waiters
+ */kernel/locking/six.c new file +3
-0+ +#define SIX_LOCK_HELD_read_OFFSET 0 +#define SIX_LOCK_HELD_read ~(~0U << 26)
Quoted 8 lines
kernel/locking/six.c line 45
Quoted 6 lines
kernel/locking/six.c new file +6
-0+#define SIX_LOCK_HELD_intent (1U << 26) +#define SIX_LOCK_HELD_write (1U << 27) +#define SIX_LOCK_WAITING_read (1U << (28 + SIX_LOCK_read)) +#define SIX_LOCK_WAITING_intent (1U << (28 + SIX_LOCK_intent)) +#define SIX_LOCK_WAITING_write (1U << (28 + SIX_LOCK_write)) +#define SIX_LOCK_NOSPIN (1U << 31)^ NOSPIN is the 31st bit.
Thanks - I think I'll just delete that comment, it's out of date and the state bits have simplified a bit :)