xref: /linux/fs/bcachefs/btree_locking.h (revision 3e7819886281e077e82006fe4804b0d6b0f5643b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_LOCKING_H
3 #define _BCACHEFS_BTREE_LOCKING_H
4 
5 /*
6  * Only for internal btree use:
7  *
8  * The btree iterator tracks what locks it wants to take, and what locks it
9  * currently has - here we have wrappers for locking/unlocking btree nodes and
10  * updating the iterator state
11  */
12 
13 #include "btree_iter.h"
14 #include "six.h"
15 
16 void bch2_btree_lock_init(struct btree_bkey_cached_common *, enum six_lock_init_flags);
17 
18 #ifdef CONFIG_LOCKDEP
19 void bch2_assert_btree_nodes_not_locked(void);
20 #else
21 static inline void bch2_assert_btree_nodes_not_locked(void) {}
22 #endif
23 
24 void bch2_trans_unlock_noassert(struct btree_trans *);
25 
26 static inline bool is_btree_node(struct btree_path *path, unsigned l)
27 {
28 	return l < BTREE_MAX_DEPTH && !IS_ERR_OR_NULL(path->l[l].b);
29 }
30 
31 static inline struct btree_transaction_stats *btree_trans_stats(struct btree_trans *trans)
32 {
33 	return trans->fn_idx < ARRAY_SIZE(trans->c->btree_transaction_stats)
34 		? &trans->c->btree_transaction_stats[trans->fn_idx]
35 		: NULL;
36 }
37 
38 /* matches six lock types */
39 enum btree_node_locked_type {
40 	BTREE_NODE_UNLOCKED		= -1,
41 	BTREE_NODE_READ_LOCKED		= SIX_LOCK_read,
42 	BTREE_NODE_INTENT_LOCKED	= SIX_LOCK_intent,
43 	BTREE_NODE_WRITE_LOCKED		= SIX_LOCK_write,
44 };
45 
46 static inline int btree_node_locked_type(struct btree_path *path,
47 					 unsigned level)
48 {
49 	return BTREE_NODE_UNLOCKED + ((path->nodes_locked >> (level << 1)) & 3);
50 }
51 
52 static inline bool btree_node_write_locked(struct btree_path *path, unsigned l)
53 {
54 	return btree_node_locked_type(path, l) == BTREE_NODE_WRITE_LOCKED;
55 }
56 
57 static inline bool btree_node_intent_locked(struct btree_path *path, unsigned l)
58 {
59 	return btree_node_locked_type(path, l) == BTREE_NODE_INTENT_LOCKED;
60 }
61 
62 static inline bool btree_node_read_locked(struct btree_path *path, unsigned l)
63 {
64 	return btree_node_locked_type(path, l) == BTREE_NODE_READ_LOCKED;
65 }
66 
67 static inline bool btree_node_locked(struct btree_path *path, unsigned level)
68 {
69 	return btree_node_locked_type(path, level) != BTREE_NODE_UNLOCKED;
70 }
71 
72 static inline void mark_btree_node_locked_noreset(struct btree_path *path,
73 						  unsigned level,
74 						  enum btree_node_locked_type type)
75 {
76 	/* relying on this to avoid a branch */
77 	BUILD_BUG_ON(SIX_LOCK_read   != 0);
78 	BUILD_BUG_ON(SIX_LOCK_intent != 1);
79 
80 	path->nodes_locked &= ~(3U << (level << 1));
81 	path->nodes_locked |= (type + 1) << (level << 1);
82 }
83 
84 static inline void mark_btree_node_unlocked(struct btree_path *path,
85 					    unsigned level)
86 {
87 	EBUG_ON(btree_node_write_locked(path, level));
88 	mark_btree_node_locked_noreset(path, level, BTREE_NODE_UNLOCKED);
89 }
90 
91 static inline void mark_btree_node_locked(struct btree_trans *trans,
92 					  struct btree_path *path,
93 					  unsigned level,
94 					  enum btree_node_locked_type type)
95 {
96 	mark_btree_node_locked_noreset(path, level, (enum btree_node_locked_type) type);
97 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
98 	path->l[level].lock_taken_time = local_clock();
99 #endif
100 }
101 
102 static inline enum six_lock_type __btree_lock_want(struct btree_path *path, int level)
103 {
104 	return level < path->locks_want
105 		? SIX_LOCK_intent
106 		: SIX_LOCK_read;
107 }
108 
109 static inline enum btree_node_locked_type
110 btree_lock_want(struct btree_path *path, int level)
111 {
112 	if (level < path->level)
113 		return BTREE_NODE_UNLOCKED;
114 	if (level < path->locks_want)
115 		return BTREE_NODE_INTENT_LOCKED;
116 	if (level == path->level)
117 		return BTREE_NODE_READ_LOCKED;
118 	return BTREE_NODE_UNLOCKED;
119 }
120 
121 static void btree_trans_lock_hold_time_update(struct btree_trans *trans,
122 					      struct btree_path *path, unsigned level)
123 {
124 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
125 	__bch2_time_stats_update(&btree_trans_stats(trans)->lock_hold_times,
126 				 path->l[level].lock_taken_time,
127 				 local_clock());
128 #endif
129 }
130 
131 /* unlock: */
132 
133 static inline void btree_node_unlock(struct btree_trans *trans,
134 				     struct btree_path *path, unsigned level)
135 {
136 	int lock_type = btree_node_locked_type(path, level);
137 
138 	EBUG_ON(level >= BTREE_MAX_DEPTH);
139 
140 	if (lock_type != BTREE_NODE_UNLOCKED) {
141 		six_unlock_type(&path->l[level].b->c.lock, lock_type);
142 		btree_trans_lock_hold_time_update(trans, path, level);
143 	}
144 	mark_btree_node_unlocked(path, level);
145 }
146 
147 static inline int btree_path_lowest_level_locked(struct btree_path *path)
148 {
149 	return __ffs(path->nodes_locked) >> 1;
150 }
151 
152 static inline int btree_path_highest_level_locked(struct btree_path *path)
153 {
154 	return __fls(path->nodes_locked) >> 1;
155 }
156 
157 static inline void __bch2_btree_path_unlock(struct btree_trans *trans,
158 					    struct btree_path *path)
159 {
160 	btree_path_set_dirty(path, BTREE_ITER_NEED_RELOCK);
161 
162 	while (path->nodes_locked)
163 		btree_node_unlock(trans, path, btree_path_lowest_level_locked(path));
164 }
165 
166 /*
167  * Updates the saved lock sequence number, so that bch2_btree_node_relock() will
168  * succeed:
169  */
170 static inline void
171 bch2_btree_node_unlock_write_inlined(struct btree_trans *trans, struct btree_path *path,
172 				     struct btree *b)
173 {
174 	struct btree_path *linked;
175 	unsigned i;
176 
177 	EBUG_ON(path->l[b->c.level].b != b);
178 	EBUG_ON(path->l[b->c.level].lock_seq != six_lock_seq(&b->c.lock));
179 	EBUG_ON(btree_node_locked_type(path, b->c.level) != SIX_LOCK_write);
180 
181 	mark_btree_node_locked_noreset(path, b->c.level, BTREE_NODE_INTENT_LOCKED);
182 
183 	trans_for_each_path_with_node(trans, b, linked, i)
184 		linked->l[b->c.level].lock_seq++;
185 
186 	six_unlock_write(&b->c.lock);
187 }
188 
189 void bch2_btree_node_unlock_write(struct btree_trans *,
190 			struct btree_path *, struct btree *);
191 
192 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p);
193 
194 /* lock: */
195 
196 static inline void trans_set_locked(struct btree_trans *trans)
197 {
198 	if (!trans->locked) {
199 		trans->locked = true;
200 		trans->last_unlock_ip = 0;
201 
202 		trans->pf_memalloc_nofs = (current->flags & PF_MEMALLOC_NOFS) != 0;
203 		current->flags |= PF_MEMALLOC_NOFS;
204 	}
205 }
206 
207 static inline void trans_set_unlocked(struct btree_trans *trans)
208 {
209 	if (trans->locked) {
210 		trans->locked = false;
211 		trans->last_unlock_ip = _RET_IP_;
212 
213 		if (!trans->pf_memalloc_nofs)
214 			current->flags &= ~PF_MEMALLOC_NOFS;
215 	}
216 }
217 
218 static inline int __btree_node_lock_nopath(struct btree_trans *trans,
219 					 struct btree_bkey_cached_common *b,
220 					 enum six_lock_type type,
221 					 bool lock_may_not_fail,
222 					 unsigned long ip)
223 {
224 	int ret;
225 
226 	trans->lock_may_not_fail = lock_may_not_fail;
227 	trans->lock_must_abort	= false;
228 	trans->locking		= b;
229 
230 	ret = six_lock_ip_waiter(&b->lock, type, &trans->locking_wait,
231 				 bch2_six_check_for_deadlock, trans, ip);
232 	WRITE_ONCE(trans->locking, NULL);
233 	WRITE_ONCE(trans->locking_wait.start_time, 0);
234 	return ret;
235 }
236 
237 static inline int __must_check
238 btree_node_lock_nopath(struct btree_trans *trans,
239 		       struct btree_bkey_cached_common *b,
240 		       enum six_lock_type type,
241 		       unsigned long ip)
242 {
243 	return __btree_node_lock_nopath(trans, b, type, false, ip);
244 }
245 
246 static inline void btree_node_lock_nopath_nofail(struct btree_trans *trans,
247 					 struct btree_bkey_cached_common *b,
248 					 enum six_lock_type type)
249 {
250 	int ret = __btree_node_lock_nopath(trans, b, type, true, _THIS_IP_);
251 
252 	BUG_ON(ret);
253 }
254 
255 /*
256  * Lock a btree node if we already have it locked on one of our linked
257  * iterators:
258  */
259 static inline bool btree_node_lock_increment(struct btree_trans *trans,
260 					     struct btree_bkey_cached_common *b,
261 					     unsigned level,
262 					     enum btree_node_locked_type want)
263 {
264 	struct btree_path *path;
265 	unsigned i;
266 
267 	trans_for_each_path(trans, path, i)
268 		if (&path->l[level].b->c == b &&
269 		    btree_node_locked_type(path, level) >= want) {
270 			six_lock_increment(&b->lock, (enum six_lock_type) want);
271 			return true;
272 		}
273 
274 	return false;
275 }
276 
277 static inline int btree_node_lock(struct btree_trans *trans,
278 			struct btree_path *path,
279 			struct btree_bkey_cached_common *b,
280 			unsigned level,
281 			enum six_lock_type type,
282 			unsigned long ip)
283 {
284 	int ret = 0;
285 
286 	EBUG_ON(level >= BTREE_MAX_DEPTH);
287 
288 	if (likely(six_trylock_type(&b->lock, type)) ||
289 	    btree_node_lock_increment(trans, b, level, (enum btree_node_locked_type) type) ||
290 	    !(ret = btree_node_lock_nopath(trans, b, type, btree_path_ip_allocated(path)))) {
291 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
292 		path->l[b->level].lock_taken_time = local_clock();
293 #endif
294 	}
295 
296 	return ret;
297 }
298 
299 int __bch2_btree_node_lock_write(struct btree_trans *, struct btree_path *,
300 				 struct btree_bkey_cached_common *b, bool);
301 
302 static inline int __btree_node_lock_write(struct btree_trans *trans,
303 					  struct btree_path *path,
304 					  struct btree_bkey_cached_common *b,
305 					  bool lock_may_not_fail)
306 {
307 	EBUG_ON(&path->l[b->level].b->c != b);
308 	EBUG_ON(path->l[b->level].lock_seq != six_lock_seq(&b->lock));
309 	EBUG_ON(!btree_node_intent_locked(path, b->level));
310 
311 	/*
312 	 * six locks are unfair, and read locks block while a thread wants a
313 	 * write lock: thus, we need to tell the cycle detector we have a write
314 	 * lock _before_ taking the lock:
315 	 */
316 	mark_btree_node_locked_noreset(path, b->level, BTREE_NODE_WRITE_LOCKED);
317 
318 	return likely(six_trylock_write(&b->lock))
319 		? 0
320 		: __bch2_btree_node_lock_write(trans, path, b, lock_may_not_fail);
321 }
322 
323 static inline int __must_check
324 bch2_btree_node_lock_write(struct btree_trans *trans,
325 			   struct btree_path *path,
326 			   struct btree_bkey_cached_common *b)
327 {
328 	return __btree_node_lock_write(trans, path, b, false);
329 }
330 
331 void bch2_btree_node_lock_write_nofail(struct btree_trans *,
332 				       struct btree_path *,
333 				       struct btree_bkey_cached_common *);
334 
335 /* relock: */
336 
337 bool bch2_btree_path_relock_norestart(struct btree_trans *, struct btree_path *);
338 int __bch2_btree_path_relock(struct btree_trans *,
339 			     struct btree_path *, unsigned long);
340 
341 static inline int bch2_btree_path_relock(struct btree_trans *trans,
342 				struct btree_path *path, unsigned long trace_ip)
343 {
344 	return btree_node_locked(path, path->level)
345 		? 0
346 		: __bch2_btree_path_relock(trans, path, trace_ip);
347 }
348 
349 bool __bch2_btree_node_relock(struct btree_trans *, struct btree_path *, unsigned, bool trace);
350 
351 static inline bool bch2_btree_node_relock(struct btree_trans *trans,
352 					  struct btree_path *path, unsigned level)
353 {
354 	EBUG_ON(btree_node_locked(path, level) &&
355 		!btree_node_write_locked(path, level) &&
356 		btree_node_locked_type(path, level) != __btree_lock_want(path, level));
357 
358 	return likely(btree_node_locked(path, level)) ||
359 		(!IS_ERR_OR_NULL(path->l[level].b) &&
360 		 __bch2_btree_node_relock(trans, path, level, true));
361 }
362 
363 static inline bool bch2_btree_node_relock_notrace(struct btree_trans *trans,
364 						  struct btree_path *path, unsigned level)
365 {
366 	EBUG_ON(btree_node_locked(path, level) &&
367 		!btree_node_write_locked(path, level) &&
368 		btree_node_locked_type(path, level) != __btree_lock_want(path, level));
369 
370 	return likely(btree_node_locked(path, level)) ||
371 		(!IS_ERR_OR_NULL(path->l[level].b) &&
372 		 __bch2_btree_node_relock(trans, path, level, false));
373 }
374 
375 /* upgrade */
376 
377 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *,
378 			       struct btree_path *, unsigned,
379 			       struct get_locks_fail *);
380 
381 bool __bch2_btree_path_upgrade(struct btree_trans *,
382 			       struct btree_path *, unsigned,
383 			       struct get_locks_fail *);
384 
385 static inline int bch2_btree_path_upgrade(struct btree_trans *trans,
386 					  struct btree_path *path,
387 					  unsigned new_locks_want)
388 {
389 	struct get_locks_fail f = {};
390 	unsigned old_locks_want = path->locks_want;
391 
392 	new_locks_want = min(new_locks_want, BTREE_MAX_DEPTH);
393 
394 	if (path->locks_want < new_locks_want
395 	    ? __bch2_btree_path_upgrade(trans, path, new_locks_want, &f)
396 	    : path->nodes_locked)
397 		return 0;
398 
399 	trace_and_count(trans->c, trans_restart_upgrade, trans, _THIS_IP_, path,
400 			old_locks_want, new_locks_want, &f);
401 	return btree_trans_restart(trans, BCH_ERR_transaction_restart_upgrade);
402 }
403 
404 /* misc: */
405 
406 static inline void btree_path_set_should_be_locked(struct btree_path *path)
407 {
408 	EBUG_ON(!btree_node_locked(path, path->level));
409 	EBUG_ON(path->uptodate);
410 
411 	path->should_be_locked = true;
412 }
413 
414 static inline void __btree_path_set_level_up(struct btree_trans *trans,
415 				      struct btree_path *path,
416 				      unsigned l)
417 {
418 	btree_node_unlock(trans, path, l);
419 	path->l[l].b = ERR_PTR(-BCH_ERR_no_btree_node_up);
420 }
421 
422 static inline void btree_path_set_level_up(struct btree_trans *trans,
423 				    struct btree_path *path)
424 {
425 	__btree_path_set_level_up(trans, path, path->level++);
426 	btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
427 }
428 
429 /* debug */
430 
431 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *,
432 				struct btree_path *,
433 				struct btree_bkey_cached_common *b,
434 				unsigned);
435 
436 int bch2_check_for_deadlock(struct btree_trans *, struct printbuf *);
437 
438 #ifdef CONFIG_BCACHEFS_DEBUG
439 void bch2_btree_path_verify_locks(struct btree_path *);
440 void bch2_trans_verify_locks(struct btree_trans *);
441 #else
442 static inline void bch2_btree_path_verify_locks(struct btree_path *path) {}
443 static inline void bch2_trans_verify_locks(struct btree_trans *trans) {}
444 #endif
445 
446 #endif /* _BCACHEFS_BTREE_LOCKING_H */
447