locking.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2008 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/sched.h>
  19. #include <linux/gfp.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/page-flags.h>
  23. #include <asm/bug.h>
  24. #include "ctree.h"
  25. #include "extent_io.h"
  26. #include "locking.h"
  27. /*
  28. * btrfs_header_level() isn't free, so don't call it when lockdep isn't
  29. * on
  30. */
  31. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  32. static inline void spin_nested(struct extent_buffer *eb)
  33. {
  34. spin_lock_nested(&eb->lock, BTRFS_MAX_LEVEL - btrfs_header_level(eb));
  35. }
  36. #else
  37. static inline void spin_nested(struct extent_buffer *eb)
  38. {
  39. spin_lock(&eb->lock);
  40. }
  41. #endif
  42. /*
  43. * Setting a lock to blocking will drop the spinlock and set the
  44. * flag that forces other procs who want the lock to wait. After
  45. * this you can safely schedule with the lock held.
  46. */
  47. void btrfs_set_lock_blocking(struct extent_buffer *eb)
  48. {
  49. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
  50. set_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags);
  51. spin_unlock(&eb->lock);
  52. }
  53. /* exit with the spin lock released and the bit set */
  54. }
  55. /*
  56. * clearing the blocking flag will take the spinlock again.
  57. * After this you can't safely schedule
  58. */
  59. void btrfs_clear_lock_blocking(struct extent_buffer *eb)
  60. {
  61. if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
  62. spin_nested(eb);
  63. clear_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags);
  64. smp_mb__after_clear_bit();
  65. }
  66. /* exit with the spin lock held */
  67. }
  68. /*
  69. * unfortunately, many of the places that currently set a lock to blocking
  70. * don't end up blocking for every long, and often they don't block
  71. * at all. For a dbench 50 run, if we don't spin one the blocking bit
  72. * at all, the context switch rate can jump up to 400,000/sec or more.
  73. *
  74. * So, we're still stuck with this crummy spin on the blocking bit,
  75. * at least until the most common causes of the short blocks
  76. * can be dealt with.
  77. */
  78. static int btrfs_spin_on_block(struct extent_buffer *eb)
  79. {
  80. int i;
  81. for (i = 0; i < 512; i++) {
  82. cpu_relax();
  83. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  84. return 1;
  85. if (need_resched())
  86. break;
  87. }
  88. return 0;
  89. }
  90. /*
  91. * This is somewhat different from trylock. It will take the
  92. * spinlock but if it finds the lock is set to blocking, it will
  93. * return without the lock held.
  94. *
  95. * returns 1 if it was able to take the lock and zero otherwise
  96. *
  97. * After this call, scheduling is not safe without first calling
  98. * btrfs_set_lock_blocking()
  99. */
  100. int btrfs_try_spin_lock(struct extent_buffer *eb)
  101. {
  102. int i;
  103. spin_nested(eb);
  104. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  105. return 1;
  106. spin_unlock(&eb->lock);
  107. /* spin for a bit on the BLOCKING flag */
  108. for (i = 0; i < 2; i++) {
  109. if (!btrfs_spin_on_block(eb))
  110. break;
  111. spin_nested(eb);
  112. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  113. return 1;
  114. spin_unlock(&eb->lock);
  115. }
  116. return 0;
  117. }
  118. /*
  119. * the autoremove wake function will return 0 if it tried to wake up
  120. * a process that was already awake, which means that process won't
  121. * count as an exclusive wakeup. The waitq code will continue waking
  122. * procs until it finds one that was actually sleeping.
  123. *
  124. * For btrfs, this isn't quite what we want. We want a single proc
  125. * to be notified that the lock is ready for taking. If that proc
  126. * already happen to be awake, great, it will loop around and try for
  127. * the lock.
  128. *
  129. * So, btrfs_wake_function always returns 1, even when the proc that we
  130. * tried to wake up was already awake.
  131. */
  132. static int btrfs_wake_function(wait_queue_t *wait, unsigned mode,
  133. int sync, void *key)
  134. {
  135. autoremove_wake_function(wait, mode, sync, key);
  136. return 1;
  137. }
  138. /*
  139. * returns with the extent buffer spinlocked.
  140. *
  141. * This will spin and/or wait as required to take the lock, and then
  142. * return with the spinlock held.
  143. *
  144. * After this call, scheduling is not safe without first calling
  145. * btrfs_set_lock_blocking()
  146. */
  147. int btrfs_tree_lock(struct extent_buffer *eb)
  148. {
  149. DEFINE_WAIT(wait);
  150. wait.func = btrfs_wake_function;
  151. while(1) {
  152. spin_nested(eb);
  153. /* nobody is blocking, exit with the spinlock held */
  154. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  155. return 0;
  156. /*
  157. * we have the spinlock, but the real owner is blocking.
  158. * wait for them
  159. */
  160. spin_unlock(&eb->lock);
  161. /*
  162. * spin for a bit, and if the blocking flag goes away,
  163. * loop around
  164. */
  165. if (btrfs_spin_on_block(eb))
  166. continue;
  167. prepare_to_wait_exclusive(&eb->lock_wq, &wait,
  168. TASK_UNINTERRUPTIBLE);
  169. if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  170. schedule();
  171. finish_wait(&eb->lock_wq, &wait);
  172. }
  173. return 0;
  174. }
  175. /*
  176. * Very quick trylock, this does not spin or schedule. It returns
  177. * 1 with the spinlock held if it was able to take the lock, or it
  178. * returns zero if it was unable to take the lock.
  179. *
  180. * After this call, scheduling is not safe without first calling
  181. * btrfs_set_lock_blocking()
  182. */
  183. int btrfs_try_tree_lock(struct extent_buffer *eb)
  184. {
  185. if (spin_trylock(&eb->lock)) {
  186. if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
  187. /*
  188. * we've got the spinlock, but the real owner is
  189. * blocking. Drop the spinlock and return failure
  190. */
  191. spin_unlock(&eb->lock);
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. /* someone else has the spinlock giveup */
  197. return 0;
  198. }
  199. int btrfs_tree_unlock(struct extent_buffer *eb)
  200. {
  201. /*
  202. * if we were a blocking owner, we don't have the spinlock held
  203. * just clear the bit and look for waiters
  204. */
  205. if (test_and_clear_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  206. smp_mb__after_clear_bit();
  207. else
  208. spin_unlock(&eb->lock);
  209. if (waitqueue_active(&eb->lock_wq))
  210. wake_up(&eb->lock_wq);
  211. return 0;
  212. }
  213. int btrfs_tree_locked(struct extent_buffer *eb)
  214. {
  215. return test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags) ||
  216. spin_is_locked(&eb->lock);
  217. }