1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/pagemap.h>
8 #include <linux/spinlock.h>
9 #include <linux/page-flags.h>
13 #include "extent_io.h"
17 * Extent buffer locking
18 * =====================
20 * We use a rw_semaphore for tree locking, and the semantics are exactly the
23 * - reader/writer exclusion
24 * - writer/writer exclusion
25 * - reader/reader sharing
26 * - try-lock semantics for readers and writers
28 * Additionally we need one level nesting recursion, see below. The rwsem
29 * implementation does opportunistic spinning which reduces number of times the
30 * locking task needs to sleep.
36 * A write operation on a tree might indirectly start a look up on the same
37 * tree. This can happen when btrfs_cow_block locks the tree and needs to
38 * lookup free extents.
42 * alloc_tree_block_no_bg_flush
43 * btrfs_alloc_tree_block
44 * btrfs_reserve_extent
46 * load_free_space_cache
48 * btrfs_lookup_file_extent
54 * __btrfs_tree_read_lock - lock extent buffer for read
55 * @eb: the eb to be locked
56 * @nest: the nesting level to be used for lockdep
57 * @recurse: if this lock is able to be recursed
59 * This takes the read lock on the extent buffer, using the specified nesting
60 * level for lockdep purposes.
62 * If you specify recurse = true, then we will allow this to be taken if we
63 * currently own the lock already. This should only be used in specific
64 * usecases, and the subsequent unlock will not change the state of the lock.
66 void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest,
71 if (trace_btrfs_tree_read_lock_enabled())
72 start_ns = ktime_get_ns();
74 if (unlikely(recurse)) {
75 /* First see if we can grab the lock outright */
76 if (down_read_trylock(&eb->lock))
80 * Ok still doesn't necessarily mean we are already holding the
81 * lock, check the owner.
83 if (eb->lock_owner != current->pid) {
84 down_read_nested(&eb->lock, nest);
89 * Ok we have actually recursed, but we should only be recursing
90 * once, so blow up if we're already recursed, otherwise set
91 * ->lock_recursed and carry on.
93 BUG_ON(eb->lock_recursed);
94 eb->lock_recursed = true;
97 down_read_nested(&eb->lock, nest);
99 eb->lock_owner = current->pid;
100 trace_btrfs_tree_read_lock(eb, start_ns);
103 void btrfs_tree_read_lock(struct extent_buffer *eb)
105 __btrfs_tree_read_lock(eb, BTRFS_NESTING_NORMAL, false);
111 * Retrun 1 if the rwlock has been taken, 0 otherwise
113 int btrfs_try_tree_read_lock(struct extent_buffer *eb)
115 if (down_read_trylock(&eb->lock)) {
116 eb->lock_owner = current->pid;
117 trace_btrfs_try_tree_read_lock(eb);
124 * Try-lock for write.
126 * Retrun 1 if the rwlock has been taken, 0 otherwise
128 int btrfs_try_tree_write_lock(struct extent_buffer *eb)
130 if (down_write_trylock(&eb->lock)) {
131 eb->lock_owner = current->pid;
132 trace_btrfs_try_tree_write_lock(eb);
139 * Release read lock. If the read lock was recursed then the lock stays in the
140 * original state that it was before it was recursively locked.
142 void btrfs_tree_read_unlock(struct extent_buffer *eb)
144 trace_btrfs_tree_read_unlock(eb);
146 * if we're nested, we have the write lock. No new locking
147 * is needed as long as we are the lock owner.
148 * The write unlock will do a barrier for us, and the lock_recursed
149 * field only matters to the lock owner.
151 if (eb->lock_recursed && current->pid == eb->lock_owner) {
152 eb->lock_recursed = false;
160 * __btrfs_tree_lock - lock eb for write
161 * @eb: the eb to lock
162 * @nest: the nesting to use for the lock
164 * Returns with the eb->lock write locked.
166 void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest)
167 __acquires(&eb->lock)
171 if (trace_btrfs_tree_lock_enabled())
172 start_ns = ktime_get_ns();
174 down_write_nested(&eb->lock, nest);
175 eb->lock_owner = current->pid;
176 trace_btrfs_tree_lock(eb, start_ns);
179 void btrfs_tree_lock(struct extent_buffer *eb)
181 __btrfs_tree_lock(eb, BTRFS_NESTING_NORMAL);
185 * Release the write lock.
187 void btrfs_tree_unlock(struct extent_buffer *eb)
189 trace_btrfs_tree_unlock(eb);
195 * This releases any locks held in the path starting at level and going all the
196 * way up to the root.
198 * btrfs_search_slot will keep the lock held on higher nodes in a few corner
199 * cases, such as COW of the block at slot zero in the node. This ignores
200 * those rules, and it should only be called when there are no more updates to
201 * be done higher up in the tree.
203 void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
207 if (path->keep_locks)
210 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
215 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
221 * Loop around taking references on and locking the root node of the tree until
222 * we end up with a lock on the root node.
224 * Return: root extent buffer with write lock held
226 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
228 struct extent_buffer *eb;
231 eb = btrfs_root_node(root);
233 if (eb == root->node)
235 btrfs_tree_unlock(eb);
236 free_extent_buffer(eb);
242 * Loop around taking references on and locking the root node of the tree until
243 * we end up with a lock on the root node.
245 * Return: root extent buffer with read lock held
247 struct extent_buffer *__btrfs_read_lock_root_node(struct btrfs_root *root,
250 struct extent_buffer *eb;
253 eb = btrfs_root_node(root);
254 __btrfs_tree_read_lock(eb, BTRFS_NESTING_NORMAL, recurse);
255 if (eb == root->node)
257 btrfs_tree_read_unlock(eb);
258 free_extent_buffer(eb);
267 * DREW stands for double-reader-writer-exclusion lock. It's used in situation
268 * where you want to provide A-B exclusion but not AA or BB.
270 * Currently implementation gives more priority to reader. If a reader and a
271 * writer both race to acquire their respective sides of the lock the writer
272 * would yield its lock as soon as it detects a concurrent reader. Additionally
273 * if there are pending readers no new writers would be allowed to come in and
277 int btrfs_drew_lock_init(struct btrfs_drew_lock *lock)
281 ret = percpu_counter_init(&lock->writers, 0, GFP_KERNEL);
285 atomic_set(&lock->readers, 0);
286 init_waitqueue_head(&lock->pending_readers);
287 init_waitqueue_head(&lock->pending_writers);
292 void btrfs_drew_lock_destroy(struct btrfs_drew_lock *lock)
294 percpu_counter_destroy(&lock->writers);
297 /* Return true if acquisition is successful, false otherwise */
298 bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock)
300 if (atomic_read(&lock->readers))
303 percpu_counter_inc(&lock->writers);
305 /* Ensure writers count is updated before we check for pending readers */
307 if (atomic_read(&lock->readers)) {
308 btrfs_drew_write_unlock(lock);
315 void btrfs_drew_write_lock(struct btrfs_drew_lock *lock)
318 if (btrfs_drew_try_write_lock(lock))
320 wait_event(lock->pending_writers, !atomic_read(&lock->readers));
324 void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock)
326 percpu_counter_dec(&lock->writers);
327 cond_wake_up(&lock->pending_readers);
330 void btrfs_drew_read_lock(struct btrfs_drew_lock *lock)
332 atomic_inc(&lock->readers);
335 * Ensure the pending reader count is perceieved BEFORE this reader
336 * goes to sleep in case of active writers. This guarantees new writers
337 * won't be allowed and that the current reader will be woken up when
338 * the last active writer finishes its jobs.
340 smp_mb__after_atomic();
342 wait_event(lock->pending_readers,
343 percpu_counter_sum(&lock->writers) == 0);
346 void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock)
349 * atomic_dec_and_test implies a full barrier, so woken up writers
350 * are guaranteed to see the decrement
352 if (atomic_dec_and_test(&lock->readers))
353 wake_up(&lock->pending_writers);