btree_locking.h 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _BCACHEFS_BTREE_LOCKING_H
#define _BCACHEFS_BTREE_LOCKING_H

/*
 * Only for internal btree use:
 *
 * The btree iterator tracks what locks it wants to take, and what locks it
 * currently has - here we have wrappers for locking/unlocking btree nodes and
 * updating the iterator state
 */

#include "btree_iter.h"
#include "six.h"

16 17
extern struct lock_class_key bch2_btree_node_lock_key;

18 19 20 21 22
static inline bool is_btree_node(struct btree_path *path, unsigned l)
{
	return l < BTREE_MAX_DEPTH && !IS_ERR_OR_NULL(path->l[l].b);
}

23 24 25 26 27 28 29
static inline struct btree_transaction_stats *btree_trans_stats(struct btree_trans *trans)
{
	return trans->fn_idx < ARRAY_SIZE(trans->c->btree_transaction_stats)
		? &trans->c->btree_transaction_stats[trans->fn_idx]
		: NULL;
}

30 31 32 33 34
/* matches six lock types */
enum btree_node_locked_type {
	BTREE_NODE_UNLOCKED		= -1,
	BTREE_NODE_READ_LOCKED		= SIX_LOCK_read,
	BTREE_NODE_INTENT_LOCKED	= SIX_LOCK_intent,
35
	BTREE_NODE_WRITE_LOCKED		= SIX_LOCK_write,
36 37
};

Kent Overstreet's avatar
Kent Overstreet committed
38
static inline int btree_node_locked_type(struct btree_path *path,
39 40
					 unsigned level)
{
41
	return BTREE_NODE_UNLOCKED + ((path->nodes_locked >> (level << 1)) & 3);
42 43
}

44 45 46 47 48 49
static inline bool btree_node_write_locked(struct btree_path *path, unsigned l)
{
	return btree_node_locked_type(path, l) == BTREE_NODE_WRITE_LOCKED;
}

static inline bool btree_node_intent_locked(struct btree_path *path, unsigned l)
50
{
51
	return btree_node_locked_type(path, l) == BTREE_NODE_INTENT_LOCKED;
52 53
}

54
static inline bool btree_node_read_locked(struct btree_path *path, unsigned l)
55
{
56
	return btree_node_locked_type(path, l) == BTREE_NODE_READ_LOCKED;
57 58
}

Kent Overstreet's avatar
Kent Overstreet committed
59
static inline bool btree_node_locked(struct btree_path *path, unsigned level)
60
{
61
	return btree_node_locked_type(path, level) != BTREE_NODE_UNLOCKED;
62 63
}

64
static inline void mark_btree_node_locked_noreset(struct btree_path *path,
65 66
						  unsigned level,
						  enum btree_node_locked_type type)
67 68 69 70 71
{
	/* relying on this to avoid a branch */
	BUILD_BUG_ON(SIX_LOCK_read   != 0);
	BUILD_BUG_ON(SIX_LOCK_intent != 1);

72 73
	path->nodes_locked &= ~(3U << (level << 1));
	path->nodes_locked |= (type + 1) << (level << 1);
74 75 76 77 78
}

static inline void mark_btree_node_unlocked(struct btree_path *path,
					    unsigned level)
{
79
	EBUG_ON(btree_node_write_locked(path, level));
80
	mark_btree_node_locked_noreset(path, level, BTREE_NODE_UNLOCKED);
81 82
}

83 84 85 86 87
static inline void mark_btree_node_locked(struct btree_trans *trans,
					  struct btree_path *path,
					  unsigned level,
					  enum six_lock_type type)
{
88
	mark_btree_node_locked_noreset(path, level, type);
89 90 91 92 93
#ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
	path->l[level].lock_taken_time = ktime_get_ns();
#endif
}

Kent Overstreet's avatar
Kent Overstreet committed
94
static inline enum six_lock_type __btree_lock_want(struct btree_path *path, int level)
95
{
Kent Overstreet's avatar
Kent Overstreet committed
96
	return level < path->locks_want
97 98 99 100 101
		? SIX_LOCK_intent
		: SIX_LOCK_read;
}

static inline enum btree_node_locked_type
Kent Overstreet's avatar
Kent Overstreet committed
102
btree_lock_want(struct btree_path *path, int level)
103
{
Kent Overstreet's avatar
Kent Overstreet committed
104
	if (level < path->level)
105
		return BTREE_NODE_UNLOCKED;
Kent Overstreet's avatar
Kent Overstreet committed
106
	if (level < path->locks_want)
107
		return BTREE_NODE_INTENT_LOCKED;
Kent Overstreet's avatar
Kent Overstreet committed
108
	if (level == path->level)
109 110 111 112
		return BTREE_NODE_READ_LOCKED;
	return BTREE_NODE_UNLOCKED;
}

113 114 115 116 117 118 119 120 121 122 123 124 125
static void btree_trans_lock_hold_time_update(struct btree_trans *trans,
					      struct btree_path *path, unsigned level)
{
#ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
	struct btree_transaction_stats *s = btree_trans_stats(trans);

	if (s)
		__bch2_time_stats_update(&s->lock_hold_times,
					 path->l[level].lock_taken_time,
					 ktime_get_ns());
#endif
}

126 127
/* unlock: */

128 129
static inline void btree_node_unlock(struct btree_trans *trans,
				     struct btree_path *path, unsigned level)
130
{
Kent Overstreet's avatar
Kent Overstreet committed
131
	int lock_type = btree_node_locked_type(path, level);
132 133 134

	EBUG_ON(level >= BTREE_MAX_DEPTH);

135
	if (lock_type != BTREE_NODE_UNLOCKED) {
Kent Overstreet's avatar
Kent Overstreet committed
136
		six_unlock_type(&path->l[level].b->c.lock, lock_type);
137
		btree_trans_lock_hold_time_update(trans, path, level);
138
	}
Kent Overstreet's avatar
Kent Overstreet committed
139
	mark_btree_node_unlocked(path, level);
140 141
}

142 143
static inline int btree_path_lowest_level_locked(struct btree_path *path)
{
144
	return __ffs(path->nodes_locked) >> 1;
145 146 147 148
}

static inline int btree_path_highest_level_locked(struct btree_path *path)
{
149
	return __fls(path->nodes_locked) >> 1;
150 151
}

152 153
static inline void __bch2_btree_path_unlock(struct btree_trans *trans,
					    struct btree_path *path)
154
{
Kent Overstreet's avatar
Kent Overstreet committed
155
	btree_path_set_dirty(path, BTREE_ITER_NEED_RELOCK);
156

Kent Overstreet's avatar
Kent Overstreet committed
157
	while (path->nodes_locked)
158
		btree_node_unlock(trans, path, btree_path_lowest_level_locked(path));
159 160
}

161 162 163 164 165 166 167
/*
 * Updates the saved lock sequence number, so that bch2_btree_node_relock() will
 * succeed:
 */
static inline void
bch2_btree_node_unlock_write_inlined(struct btree_trans *trans, struct btree_path *path,
				     struct btree *b)
168
{
169 170 171 172
	struct btree_path *linked;

	EBUG_ON(path->l[b->c.level].b != b);
	EBUG_ON(path->l[b->c.level].lock_seq + 1 != b->c.lock.state.seq);
173 174 175
	EBUG_ON(btree_node_locked_type(path, b->c.level) != SIX_LOCK_write);

	mark_btree_node_locked_noreset(path, b->c.level, SIX_LOCK_intent);
176 177 178 179 180

	trans_for_each_path_with_node(trans, b, linked)
		linked->l[b->c.level].lock_seq += 2;

	six_unlock_write(&b->c.lock);
181 182
}

183 184 185
void bch2_btree_node_unlock_write(struct btree_trans *,
			struct btree_path *, struct btree *);

186 187
int bch2_six_check_for_deadlock(struct six_lock *lock, void *p);

188 189
/* lock: */

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
static inline int __btree_node_lock_nopath(struct btree_trans *trans,
					 struct btree_bkey_cached_common *b,
					 enum six_lock_type type,
					 bool lock_may_not_fail)
{
	int ret;

	trans->lock_may_not_fail = lock_may_not_fail;
	trans->locking		= b;
	trans->lock_must_abort	= false;

	ret = six_lock_type_waiter(&b->lock, type, &trans->locking_wait,
				   bch2_six_check_for_deadlock, trans);
	WRITE_ONCE(trans->locking, NULL);
	WRITE_ONCE(trans->locking_wait.start_time, 0);
	return ret;
}

208 209 210 211 212
static inline int __must_check
btree_node_lock_nopath(struct btree_trans *trans,
		       struct btree_bkey_cached_common *b,
		       enum six_lock_type type)
{
213
	return __btree_node_lock_nopath(trans, b, type, false);
214 215 216 217 218 219
}

static inline void btree_node_lock_nopath_nofail(struct btree_trans *trans,
					 struct btree_bkey_cached_common *b,
					 enum six_lock_type type)
{
220
	int ret = __btree_node_lock_nopath(trans, b, type, true);
221 222 223 224

	BUG_ON(ret);
}

225
static inline int btree_node_lock_type(struct btree_trans *trans,
226
				       struct btree_path *path,
227
				       struct btree_bkey_cached_common *b,
228 229 230
				       struct bpos pos, unsigned level,
				       enum six_lock_type type,
				       six_lock_should_sleep_fn should_sleep_fn, void *p)
231
{
232
	if (six_trylock_type(&b->lock, type))
233
		return 0;
234

235 236 237 238
	trans->locking_path_idx = path->idx;
	trans->locking_pos	= pos;
	trans->locking_btree_id	= path->btree_id;
	trans->locking_level	= level;
239
	trans->lock_may_not_fail = false;
240
	trans->locking		= b;
241 242
	return six_lock_type_waiter(&b->lock, type, &trans->locking_wait,
				    bch2_six_check_for_deadlock, trans);
243 244
}

245 246 247 248
/*
 * Lock a btree node if we already have it locked on one of our linked
 * iterators:
 */
249
static inline bool btree_node_lock_increment(struct btree_trans *trans,
250 251
					     struct btree_bkey_cached_common *b,
					     unsigned level,
252 253
					     enum btree_node_locked_type want)
{
Kent Overstreet's avatar
Kent Overstreet committed
254
	struct btree_path *path;
255

Kent Overstreet's avatar
Kent Overstreet committed
256
	trans_for_each_path(trans, path)
257
		if (&path->l[level].b->c == b &&
Kent Overstreet's avatar
Kent Overstreet committed
258
		    btree_node_locked_type(path, level) >= want) {
259
			six_lock_increment(&b->lock, want);
260 261 262 263 264 265
			return true;
		}

	return false;
}

266
int __bch2_btree_node_lock(struct btree_trans *, struct btree_path *,
267 268
			   struct btree_bkey_cached_common *,
			   struct bpos, unsigned,
269 270 271
			   enum six_lock_type,
			   six_lock_should_sleep_fn, void *,
			   unsigned long);
272

273
static inline int btree_node_lock(struct btree_trans *trans,
Kent Overstreet's avatar
Kent Overstreet committed
274
			struct btree_path *path,
275 276
			struct btree_bkey_cached_common *b,
			struct bpos pos, unsigned level,
277
			enum six_lock_type type,
278 279
			six_lock_should_sleep_fn should_sleep_fn, void *p,
			unsigned long ip)
280
{
281 282
	int ret = 0;

283
	EBUG_ON(level >= BTREE_MAX_DEPTH);
Kent Overstreet's avatar
Kent Overstreet committed
284
	EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
285

286
	if (likely(six_trylock_type(&b->lock, type)) ||
287 288 289
	    btree_node_lock_increment(trans, b, level, type) ||
	    !(ret = __bch2_btree_node_lock(trans, path, b, pos, level, type,
					   should_sleep_fn, p, ip))) {
290
#ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
291
		path->l[b->level].lock_taken_time = ktime_get_ns();
292 293
#endif
	}
294 295

	return ret;
296 297
}

298
int __bch2_btree_node_lock_write(struct btree_trans *, struct btree_bkey_cached_common *, bool);
299

300 301 302 303
static inline int __btree_node_lock_write(struct btree_trans *trans,
					  struct btree_path *path,
					  struct btree_bkey_cached_common *b,
					  bool lock_may_not_fail)
304
{
305 306
	int ret;

307 308 309
	EBUG_ON(&path->l[b->level].b->c != b);
	EBUG_ON(path->l[b->level].lock_seq != b->lock.state.seq);
	EBUG_ON(!btree_node_intent_locked(path, b->level));
310

311 312 313 314 315
	/*
	 * six locks are unfair, and read locks block while a thread wants a
	 * write lock: thus, we need to tell the cycle detector we have a write
	 * lock _before_ taking the lock:
	 */
316
	mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_write);
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	ret = likely(six_trylock_write(&b->lock))
		? 0
		: __bch2_btree_node_lock_write(trans, b, lock_may_not_fail);
	if (ret)
		mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_intent);

	return ret;
}

static inline void bch2_btree_node_lock_write_nofail(struct btree_trans *trans,
					      struct btree_path *path,
					      struct btree_bkey_cached_common *b)
{
	int ret = __btree_node_lock_write(trans, path, b, true);
	BUG_ON(ret);
333 334
}

335 336 337
static inline int __must_check
bch2_btree_node_lock_write(struct btree_trans *trans,
			   struct btree_path *path,
338
			   struct btree_bkey_cached_common *b)
339
{
340
	return __btree_node_lock_write(trans, path, b, false);
341 342
}

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
/* relock: */

bool bch2_btree_path_relock_norestart(struct btree_trans *,
				      struct btree_path *, unsigned long);
int __bch2_btree_path_relock(struct btree_trans *,
			     struct btree_path *, unsigned long);

static inline int bch2_btree_path_relock(struct btree_trans *trans,
				struct btree_path *path, unsigned long trace_ip)
{
	return btree_node_locked(path, path->level)
		? 0
		: __bch2_btree_path_relock(trans, path, trace_ip);
}

bool __bch2_btree_node_relock(struct btree_trans *, struct btree_path *, unsigned);

static inline bool bch2_btree_node_relock(struct btree_trans *trans,
					  struct btree_path *path, unsigned level)
{
	EBUG_ON(btree_node_locked(path, level) &&
364 365
		!btree_node_write_locked(path, level) &&
		btree_node_locked_type(path, level) != __btree_lock_want(path, level));
366 367

	return likely(btree_node_locked(path, level)) ||
368 369
		(!IS_ERR_OR_NULL(path->l[level].b) &&
		 __bch2_btree_node_relock(trans, path, level));
370 371 372 373
}

/* upgrade */

374 375 376 377 378
bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *,
			       struct btree_path *, unsigned);
bool __bch2_btree_path_upgrade(struct btree_trans *,
			       struct btree_path *, unsigned);

379 380 381
static inline int bch2_btree_path_upgrade(struct btree_trans *trans,
					  struct btree_path *path,
					  unsigned new_locks_want)
382
{
383 384
	unsigned old_locks_want = path->locks_want;

385 386
	new_locks_want = min(new_locks_want, BTREE_MAX_DEPTH);

387 388 389 390 391 392 393 394
	if (path->locks_want < new_locks_want
	    ? __bch2_btree_path_upgrade(trans, path, new_locks_want)
	    : path->uptodate == BTREE_ITER_UPTODATE)
		return 0;

	trace_and_count(trans->c, trans_restart_upgrade, trans, _THIS_IP_, path,
			old_locks_want, new_locks_want);
	return btree_trans_restart(trans, BCH_ERR_transaction_restart_upgrade);
395 396
}

397 398
/* misc: */

399 400 401 402 403 404 405 406
static inline void btree_path_set_should_be_locked(struct btree_path *path)
{
	EBUG_ON(!btree_node_locked(path, path->level));
	EBUG_ON(path->uptodate);

	path->should_be_locked = true;
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static inline void __btree_path_set_level_up(struct btree_trans *trans,
				      struct btree_path *path,
				      unsigned l)
{
	btree_node_unlock(trans, path, l);
	path->l[l].b = ERR_PTR(-BCH_ERR_no_btree_node_up);
}

static inline void btree_path_set_level_up(struct btree_trans *trans,
				    struct btree_path *path)
{
	__btree_path_set_level_up(trans, path, path->level++);
	btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
}

422 423
/* debug */

424
struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *,
425 426 427
				struct btree_path *,
				struct btree_bkey_cached_common *b,
				unsigned);
428

429
int bch2_check_for_deadlock(struct btree_trans *, struct printbuf *);
430 431 432 433 434 435 436 437 438

#ifdef CONFIG_BCACHEFS_DEBUG
void bch2_btree_path_verify_locks(struct btree_path *);
void bch2_trans_verify_locks(struct btree_trans *);
#else
static inline void bch2_btree_path_verify_locks(struct btree_path *path) {}
static inline void bch2_trans_verify_locks(struct btree_trans *trans) {}
#endif

439
#endif /* _BCACHEFS_BTREE_LOCKING_H */