]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/six.h
Update bcachefs sources to e82e656279 bcachefs: Cleanups for building in userspace
[bcachefs-tools-debian] / libbcachefs / six.h
1 #ifndef _BCACHEFS_SIX_H
2 #define _BCACHEFS_SIX_H
3
4 #include <linux/lockdep.h>
5 #include <linux/osq_lock.h>
6 #include <linux/sched.h>
7 #include <linux/types.h>
8
9 #include "util.h"
10
11 /*
12  * LOCK STATES:
13  *
14  * read, intent, write (i.e. shared/intent/exclusive, hence the name)
15  *
16  * read and write work as with normal read/write locks - a lock can have
17  * multiple readers, but write excludes reads and other write locks.
18  *
19  * Intent does not block read, but it does block other intent locks. The idea is
20  * by taking an intent lock, you can then later upgrade to a write lock without
21  * dropping your read lock and without deadlocking - because no other thread has
22  * the intent lock and thus no other thread could be trying to take the write
23  * lock.
24  */
25
26 union six_lock_state {
27         struct {
28                 atomic64_t      counter;
29         };
30
31         struct {
32                 u64             v;
33         };
34
35         struct {
36                 /* for waitlist_bitnr() */
37                 unsigned long   l;
38         };
39
40         struct {
41                 unsigned        read_lock:26;
42                 unsigned        intent_lock:3;
43                 unsigned        waiters:3;
44                 /*
45                  * seq works much like in seqlocks: it's incremented every time
46                  * we lock and unlock for write.
47                  *
48                  * If it's odd write lock is held, even unlocked.
49                  *
50                  * Thus readers can unlock, and then lock again later iff it
51                  * hasn't been modified in the meantime.
52                  */
53                 u32             seq;
54         };
55 };
56
57 #define SIX_LOCK_MAX_RECURSE    ((1 << 3) - 1)
58
59 enum six_lock_type {
60         SIX_LOCK_read,
61         SIX_LOCK_intent,
62         SIX_LOCK_write,
63 };
64
65 struct six_lock {
66         union six_lock_state    state;
67         struct task_struct      *owner;
68         struct optimistic_spin_queue osq;
69
70         raw_spinlock_t          wait_lock;
71         struct list_head        wait_list[3];
72 #ifdef CONFIG_DEBUG_LOCK_ALLOC
73         struct lockdep_map      dep_map;
74 #endif
75 };
76
77 static __always_inline void __six_lock_init(struct six_lock *lock,
78                                             const char *name,
79                                             struct lock_class_key *key)
80 {
81         atomic64_set(&lock->state.counter, 0);
82         raw_spin_lock_init(&lock->wait_lock);
83         INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_read]);
84         INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_intent]);
85         INIT_LIST_HEAD(&lock->wait_list[SIX_LOCK_write]);
86 #ifdef CONFIG_DEBUG_LOCK_ALLOC
87         debug_check_no_locks_freed((void *) lock, sizeof(*lock));
88         lockdep_init_map(&lock->dep_map, name, key, 0);
89 #endif
90 }
91
92 #define six_lock_init(lock)                                             \
93 do {                                                                    \
94         static struct lock_class_key __key;                             \
95                                                                         \
96         __six_lock_init((lock), #lock, &__key);                         \
97 } while (0)
98
99 bool six_trylock_type(struct six_lock *, enum six_lock_type);
100 bool six_relock_type(struct six_lock *, enum six_lock_type, unsigned);
101 void six_lock_type(struct six_lock *, enum six_lock_type);
102 void six_unlock_type(struct six_lock *, enum six_lock_type);
103 bool six_trylock_convert(struct six_lock *, enum six_lock_type,
104                          enum six_lock_type);
105 void six_lock_increment(struct six_lock *, enum six_lock_type);
106 void six_lock_downgrade(struct six_lock *);
107
108 #define __SIX_VAL(field, _v)    (((union six_lock_state) { .field = _v }).v)
109
110 #define __SIX_LOCK(type)                                                \
111 static __always_inline bool six_trylock_##type(struct six_lock *lock)   \
112 {                                                                       \
113         return six_trylock_type(lock, SIX_LOCK_##type);                 \
114 }                                                                       \
115                                                                         \
116 static __always_inline bool six_relock_##type(struct six_lock *lock, u32 seq)\
117 {                                                                       \
118         return six_relock_type(lock, SIX_LOCK_##type, seq);             \
119 }                                                                       \
120                                                                         \
121 static __always_inline void six_lock_##type(struct six_lock *lock)      \
122 {                                                                       \
123         six_lock_type(lock, SIX_LOCK_##type);                           \
124 }                                                                       \
125                                                                         \
126 static __always_inline void six_unlock_##type(struct six_lock *lock)    \
127 {                                                                       \
128         six_unlock_type(lock, SIX_LOCK_##type);                         \
129 }
130
131 __SIX_LOCK(read)
132 __SIX_LOCK(intent)
133 __SIX_LOCK(write)
134
135 #endif /* _BCACHEFS_SIX_H */