1 #ifndef _BCACHEFS_SIX_H
2 #define _BCACHEFS_SIX_H
4 #include <linux/lockdep.h>
5 #include <linux/osq_lock.h>
6 #include <linux/sched.h>
7 #include <linux/types.h>
11 #define SIX_LOCK_SEPARATE_LOCKFNS
16 * read, intent, write (i.e. shared/intent/exclusive, hence the name)
18 * read and write work as with normal read/write locks - a lock can have
19 * multiple readers, but write excludes reads and other write locks.
21 * Intent does not block read, but it does block other intent locks. The idea is
22 * by taking an intent lock, you can then later upgrade to a write lock without
23 * dropping your read lock and without deadlocking - because no other thread has
24 * the intent lock and thus no other thread could be trying to take the write
28 union six_lock_state {
38 /* for waitlist_bitnr() */
43 unsigned read_lock:26;
44 unsigned intent_lock:3;
47 * seq works much like in seqlocks: it's incremented every time
48 * we lock and unlock for write.
50 * If it's odd write lock is held, even unlocked.
52 * Thus readers can unlock, and then lock again later iff it
53 * hasn't been modified in the meantime.
59 #define SIX_LOCK_MAX_RECURSE ((1 << 3) - 1)
68 union six_lock_state state;
69 struct task_struct *owner;
70 struct optimistic_spin_queue osq;
72 raw_spinlock_t wait_lock;
73 struct list_head wait_list[2];
74 #ifdef CONFIG_DEBUG_LOCK_ALLOC
75 struct lockdep_map dep_map;
79 static __always_inline void __six_lock_init(struct six_lock *lock,
81 struct lock_class_key *key)
83 atomic64_set(&lock->state.counter, 0);
84 raw_spin_lock_init(&lock->wait_lock);
85 INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_read]);
86 INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_intent]);
87 #ifdef CONFIG_DEBUG_LOCK_ALLOC
88 debug_check_no_locks_freed((void *) lock, sizeof(*lock));
89 lockdep_init_map(&lock->dep_map, name, key, 0);
93 #define six_lock_init(lock) \
95 static struct lock_class_key __key; \
97 __six_lock_init((lock), #lock, &__key); \
100 #define __SIX_VAL(field, _v) (((union six_lock_state) { .field = _v }).v)
102 #ifdef SIX_LOCK_SEPARATE_LOCKFNS
104 #define __SIX_LOCK(type) \
105 bool six_trylock_##type(struct six_lock *); \
106 bool six_relock_##type(struct six_lock *, u32); \
107 void six_lock_##type(struct six_lock *); \
108 void six_unlock_##type(struct six_lock *);
115 #define SIX_LOCK_DISPATCH(type, fn, ...) \
117 case SIX_LOCK_read: \
118 return fn##_read(__VA_ARGS__); \
119 case SIX_LOCK_intent: \
120 return fn##_intent(__VA_ARGS__); \
121 case SIX_LOCK_write: \
122 return fn##_write(__VA_ARGS__); \
127 static inline bool six_trylock_type(struct six_lock *lock, enum six_lock_type type)
129 SIX_LOCK_DISPATCH(type, six_trylock, lock);
132 static inline bool six_relock_type(struct six_lock *lock, enum six_lock_type type,
135 SIX_LOCK_DISPATCH(type, six_relock, lock, seq);
138 static inline void six_lock_type(struct six_lock *lock, enum six_lock_type type)
140 SIX_LOCK_DISPATCH(type, six_lock, lock);
143 static inline void six_unlock_type(struct six_lock *lock, enum six_lock_type type)
145 SIX_LOCK_DISPATCH(type, six_unlock, lock);
150 bool six_trylock_type(struct six_lock *, enum six_lock_type);
151 bool six_relock_type(struct six_lock *, enum six_lock_type, unsigned);
152 void six_lock_type(struct six_lock *, enum six_lock_type);
153 void six_unlock_type(struct six_lock *, enum six_lock_type);
155 #define __SIX_LOCK(type) \
156 static __always_inline bool six_trylock_##type(struct six_lock *lock) \
158 return six_trylock_type(lock, SIX_LOCK_##type); \
161 static __always_inline bool six_relock_##type(struct six_lock *lock, u32 seq)\
163 return six_relock_type(lock, SIX_LOCK_##type, seq); \
166 static __always_inline void six_lock_##type(struct six_lock *lock) \
168 six_lock_type(lock, SIX_LOCK_##type); \
171 static __always_inline void six_unlock_##type(struct six_lock *lock) \
173 six_unlock_type(lock, SIX_LOCK_##type); \
183 void six_lock_downgrade(struct six_lock *);
184 bool six_lock_tryupgrade(struct six_lock *);
185 bool six_trylock_convert(struct six_lock *, enum six_lock_type,
188 void six_lock_increment(struct six_lock *, enum six_lock_type);
190 #endif /* _BCACHEFS_SIX_H */