locking.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/page-flags.h>
  9. #include <asm/bug.h>
  10. #include <trace/events/btrfs.h>
  11. #include "misc.h"
  12. #include "ctree.h"
  13. #include "extent_io.h"
  14. #include "locking.h"
  15. /*
  16. * Lockdep class keys for extent_buffer->lock's in this root. For a given
  17. * eb, the lockdep key is determined by the btrfs_root it belongs to and
  18. * the level the eb occupies in the tree.
  19. *
  20. * Different roots are used for different purposes and may nest inside each
  21. * other and they require separate keysets. As lockdep keys should be
  22. * static, assign keysets according to the purpose of the root as indicated
  23. * by btrfs_root->root_key.objectid. This ensures that all special purpose
  24. * roots have separate keysets.
  25. *
  26. * Lock-nesting across peer nodes is always done with the immediate parent
  27. * node locked thus preventing deadlock. As lockdep doesn't know this, use
  28. * subclass to avoid triggering lockdep warning in such cases.
  29. *
  30. * The key is set by the readpage_end_io_hook after the buffer has passed
  31. * csum validation but before the pages are unlocked. It is also set by
  32. * btrfs_init_new_buffer on freshly allocated blocks.
  33. *
  34. * We also add a check to make sure the highest level of the tree is the
  35. * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
  36. * needs update as well.
  37. */
  38. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  39. #if BTRFS_MAX_LEVEL != 8
  40. #error
  41. #endif
  42. #define DEFINE_LEVEL(stem, level) \
  43. .names[level] = "btrfs-" stem "-0" #level,
  44. #define DEFINE_NAME(stem) \
  45. DEFINE_LEVEL(stem, 0) \
  46. DEFINE_LEVEL(stem, 1) \
  47. DEFINE_LEVEL(stem, 2) \
  48. DEFINE_LEVEL(stem, 3) \
  49. DEFINE_LEVEL(stem, 4) \
  50. DEFINE_LEVEL(stem, 5) \
  51. DEFINE_LEVEL(stem, 6) \
  52. DEFINE_LEVEL(stem, 7)
  53. static struct btrfs_lockdep_keyset {
  54. u64 id; /* root objectid */
  55. /* Longest entry: btrfs-block-group-00 */
  56. char names[BTRFS_MAX_LEVEL][24];
  57. struct lock_class_key keys[BTRFS_MAX_LEVEL];
  58. } btrfs_lockdep_keysets[] = {
  59. { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") },
  60. { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") },
  61. { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") },
  62. { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") },
  63. { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") },
  64. { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") },
  65. { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") },
  66. { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") },
  67. { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") },
  68. { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") },
  69. { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") },
  70. { .id = BTRFS_BLOCK_GROUP_TREE_OBJECTID, DEFINE_NAME("block-group") },
  71. { .id = BTRFS_RAID_STRIPE_TREE_OBJECTID, DEFINE_NAME("raid-stripe") },
  72. { .id = 0, DEFINE_NAME("tree") },
  73. };
  74. #undef DEFINE_LEVEL
  75. #undef DEFINE_NAME
  76. void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, int level)
  77. {
  78. struct btrfs_lockdep_keyset *ks;
  79. ASSERT(level < ARRAY_SIZE(ks->keys));
  80. /* Find the matching keyset, id 0 is the default entry */
  81. for (ks = btrfs_lockdep_keysets; ks->id; ks++)
  82. if (ks->id == objectid)
  83. break;
  84. lockdep_set_class_and_name(&eb->lock, &ks->keys[level], ks->names[level]);
  85. }
  86. void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buffer *eb)
  87. {
  88. if (test_bit(BTRFS_ROOT_RESET_LOCKDEP_CLASS, &root->state))
  89. btrfs_set_buffer_lockdep_class(btrfs_root_id(root),
  90. eb, btrfs_header_level(eb));
  91. }
  92. #endif
  93. #ifdef CONFIG_BTRFS_DEBUG
  94. static void btrfs_set_eb_lock_owner(struct extent_buffer *eb, pid_t owner)
  95. {
  96. eb->lock_owner = owner;
  97. }
  98. #else
  99. static void btrfs_set_eb_lock_owner(struct extent_buffer *eb, pid_t owner) { }
  100. #endif
  101. /*
  102. * Extent buffer locking
  103. * =====================
  104. *
  105. * We use a rw_semaphore for tree locking, and the semantics are exactly the
  106. * same:
  107. *
  108. * - reader/writer exclusion
  109. * - writer/writer exclusion
  110. * - reader/reader sharing
  111. * - try-lock semantics for readers and writers
  112. *
  113. * The rwsem implementation does opportunistic spinning which reduces number of
  114. * times the locking task needs to sleep.
  115. */
  116. /*
  117. * btrfs_tree_read_lock_nested - lock extent buffer for read
  118. * @eb: the eb to be locked
  119. * @nest: the nesting level to be used for lockdep
  120. *
  121. * This takes the read lock on the extent buffer, using the specified nesting
  122. * level for lockdep purposes.
  123. */
  124. void btrfs_tree_read_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest)
  125. {
  126. u64 start_ns = 0;
  127. if (trace_btrfs_tree_read_lock_enabled())
  128. start_ns = ktime_get_ns();
  129. down_read_nested(&eb->lock, nest);
  130. trace_btrfs_tree_read_lock(eb, start_ns);
  131. }
  132. /*
  133. * Try-lock for read.
  134. *
  135. * Return 1 if the rwlock has been taken, 0 otherwise
  136. */
  137. int btrfs_try_tree_read_lock(struct extent_buffer *eb)
  138. {
  139. if (down_read_trylock(&eb->lock)) {
  140. trace_btrfs_try_tree_read_lock(eb);
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Try-lock for write.
  147. *
  148. * Return 1 if the rwlock has been taken, 0 otherwise
  149. */
  150. int btrfs_try_tree_write_lock(struct extent_buffer *eb)
  151. {
  152. if (down_write_trylock(&eb->lock)) {
  153. btrfs_set_eb_lock_owner(eb, current->pid);
  154. trace_btrfs_try_tree_write_lock(eb);
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. /*
  160. * Release read lock.
  161. */
  162. void btrfs_tree_read_unlock(struct extent_buffer *eb)
  163. {
  164. trace_btrfs_tree_read_unlock(eb);
  165. up_read(&eb->lock);
  166. }
  167. /*
  168. * Lock eb for write.
  169. *
  170. * @eb: the eb to lock
  171. * @nest: the nesting to use for the lock
  172. *
  173. * Returns with the eb->lock write locked.
  174. */
  175. void btrfs_tree_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest)
  176. __acquires(&eb->lock)
  177. {
  178. u64 start_ns = 0;
  179. if (trace_btrfs_tree_lock_enabled())
  180. start_ns = ktime_get_ns();
  181. down_write_nested(&eb->lock, nest);
  182. btrfs_set_eb_lock_owner(eb, current->pid);
  183. trace_btrfs_tree_lock(eb, start_ns);
  184. }
  185. /*
  186. * Release the write lock.
  187. */
  188. void btrfs_tree_unlock(struct extent_buffer *eb)
  189. {
  190. trace_btrfs_tree_unlock(eb);
  191. btrfs_set_eb_lock_owner(eb, 0);
  192. up_write(&eb->lock);
  193. }
  194. /*
  195. * This releases any locks held in the path starting at level and going all the
  196. * way up to the root.
  197. *
  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.
  202. */
  203. void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
  204. {
  205. int i;
  206. if (path->keep_locks)
  207. return;
  208. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  209. if (!path->nodes[i])
  210. continue;
  211. if (!path->locks[i])
  212. continue;
  213. btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
  214. path->locks[i] = 0;
  215. }
  216. }
  217. /*
  218. * Loop around taking references on and locking the root node of the tree until
  219. * we end up with a lock on the root node.
  220. *
  221. * Return: root extent buffer with write lock held
  222. */
  223. struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
  224. {
  225. struct extent_buffer *eb;
  226. while (1) {
  227. eb = btrfs_root_node(root);
  228. btrfs_maybe_reset_lockdep_class(root, eb);
  229. btrfs_tree_lock(eb);
  230. if (eb == root->node)
  231. break;
  232. btrfs_tree_unlock(eb);
  233. free_extent_buffer(eb);
  234. }
  235. return eb;
  236. }
  237. /*
  238. * Loop around taking references on and locking the root node of the tree until
  239. * we end up with a lock on the root node.
  240. *
  241. * Return: root extent buffer with read lock held
  242. */
  243. struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
  244. {
  245. struct extent_buffer *eb;
  246. while (1) {
  247. eb = btrfs_root_node(root);
  248. btrfs_maybe_reset_lockdep_class(root, eb);
  249. btrfs_tree_read_lock(eb);
  250. if (eb == root->node)
  251. break;
  252. btrfs_tree_read_unlock(eb);
  253. free_extent_buffer(eb);
  254. }
  255. return eb;
  256. }
  257. /*
  258. * Loop around taking references on and locking the root node of the tree in
  259. * nowait mode until we end up with a lock on the root node or returning to
  260. * avoid blocking.
  261. *
  262. * Return: root extent buffer with read lock held or -EAGAIN.
  263. */
  264. struct extent_buffer *btrfs_try_read_lock_root_node(struct btrfs_root *root)
  265. {
  266. struct extent_buffer *eb;
  267. while (1) {
  268. eb = btrfs_root_node(root);
  269. if (!btrfs_try_tree_read_lock(eb)) {
  270. free_extent_buffer(eb);
  271. return ERR_PTR(-EAGAIN);
  272. }
  273. if (eb == root->node)
  274. break;
  275. btrfs_tree_read_unlock(eb);
  276. free_extent_buffer(eb);
  277. }
  278. return eb;
  279. }
  280. /*
  281. * DREW locks
  282. * ==========
  283. *
  284. * DREW stands for double-reader-writer-exclusion lock. It's used in situation
  285. * where you want to provide A-B exclusion but not AA or BB.
  286. *
  287. * Currently implementation gives more priority to reader. If a reader and a
  288. * writer both race to acquire their respective sides of the lock the writer
  289. * would yield its lock as soon as it detects a concurrent reader. Additionally
  290. * if there are pending readers no new writers would be allowed to come in and
  291. * acquire the lock.
  292. */
  293. void btrfs_drew_lock_init(struct btrfs_drew_lock *lock)
  294. {
  295. atomic_set(&lock->readers, 0);
  296. atomic_set(&lock->writers, 0);
  297. init_waitqueue_head(&lock->pending_readers);
  298. init_waitqueue_head(&lock->pending_writers);
  299. }
  300. /* Return true if acquisition is successful, false otherwise */
  301. bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock)
  302. {
  303. if (atomic_read(&lock->readers))
  304. return false;
  305. atomic_inc(&lock->writers);
  306. /* Ensure writers count is updated before we check for pending readers */
  307. smp_mb__after_atomic();
  308. if (atomic_read(&lock->readers)) {
  309. btrfs_drew_write_unlock(lock);
  310. return false;
  311. }
  312. return true;
  313. }
  314. void btrfs_drew_write_lock(struct btrfs_drew_lock *lock)
  315. {
  316. while (true) {
  317. if (btrfs_drew_try_write_lock(lock))
  318. return;
  319. wait_event(lock->pending_writers, !atomic_read(&lock->readers));
  320. }
  321. }
  322. void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock)
  323. {
  324. /*
  325. * atomic_dec_and_test() implies a full barrier, so woken up readers are
  326. * guaranteed to see the decrement.
  327. */
  328. if (atomic_dec_and_test(&lock->writers))
  329. wake_up(&lock->pending_readers);
  330. }
  331. void btrfs_drew_read_lock(struct btrfs_drew_lock *lock)
  332. {
  333. atomic_inc(&lock->readers);
  334. /*
  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.
  339. */
  340. smp_mb__after_atomic();
  341. wait_event(lock->pending_readers, atomic_read(&lock->writers) == 0);
  342. }
  343. void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock)
  344. {
  345. /*
  346. * atomic_dec_and_test implies a full barrier, so woken up writers
  347. * are guaranteed to see the decrement
  348. */
  349. if (atomic_dec_and_test(&lock->readers))
  350. wake_up(&lock->pending_writers);
  351. }