locking.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. static inline void spin_nested(struct extent_buffer *eb)
  28. {
  29. spin_lock(&eb->lock);
  30. }
  31. /*
  32. * Setting a lock to blocking will drop the spinlock and set the
  33. * flag that forces other procs who want the lock to wait. After
  34. * this you can safely schedule with the lock held.
  35. */
  36. void btrfs_set_lock_blocking(struct extent_buffer *eb)
  37. {
  38. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
  39. set_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags);
  40. spin_unlock(&eb->lock);
  41. }
  42. /* exit with the spin lock released and the bit set */
  43. }
  44. /*
  45. * clearing the blocking flag will take the spinlock again.
  46. * After this you can't safely schedule
  47. */
  48. void btrfs_clear_lock_blocking(struct extent_buffer *eb)
  49. {
  50. if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
  51. spin_nested(eb);
  52. clear_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags);
  53. smp_mb__after_clear_bit();
  54. }
  55. /* exit with the spin lock held */
  56. }
  57. /*
  58. * unfortunately, many of the places that currently set a lock to blocking
  59. * don't end up blocking for every long, and often they don't block
  60. * at all. For a dbench 50 run, if we don't spin one the blocking bit
  61. * at all, the context switch rate can jump up to 400,000/sec or more.
  62. *
  63. * So, we're still stuck with this crummy spin on the blocking bit,
  64. * at least until the most common causes of the short blocks
  65. * can be dealt with.
  66. */
  67. static int btrfs_spin_on_block(struct extent_buffer *eb)
  68. {
  69. int i;
  70. for (i = 0; i < 512; i++) {
  71. cpu_relax();
  72. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  73. return 1;
  74. if (need_resched())
  75. break;
  76. }
  77. return 0;
  78. }
  79. /*
  80. * This is somewhat different from trylock. It will take the
  81. * spinlock but if it finds the lock is set to blocking, it will
  82. * return without the lock held.
  83. *
  84. * returns 1 if it was able to take the lock and zero otherwise
  85. *
  86. * After this call, scheduling is not safe without first calling
  87. * btrfs_set_lock_blocking()
  88. */
  89. int btrfs_try_spin_lock(struct extent_buffer *eb)
  90. {
  91. int i;
  92. spin_nested(eb);
  93. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  94. return 1;
  95. spin_unlock(&eb->lock);
  96. /* spin for a bit on the BLOCKING flag */
  97. for (i = 0; i < 2; i++) {
  98. if (!btrfs_spin_on_block(eb))
  99. break;
  100. spin_nested(eb);
  101. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  102. return 1;
  103. spin_unlock(&eb->lock);
  104. }
  105. return 0;
  106. }
  107. /*
  108. * the autoremove wake function will return 0 if it tried to wake up
  109. * a process that was already awake, which means that process won't
  110. * count as an exclusive wakeup. The waitq code will continue waking
  111. * procs until it finds one that was actually sleeping.
  112. *
  113. * For btrfs, this isn't quite what we want. We want a single proc
  114. * to be notified that the lock is ready for taking. If that proc
  115. * already happen to be awake, great, it will loop around and try for
  116. * the lock.
  117. *
  118. * So, btrfs_wake_function always returns 1, even when the proc that we
  119. * tried to wake up was already awake.
  120. */
  121. static int btrfs_wake_function(wait_queue_t *wait, unsigned mode,
  122. int sync, void *key)
  123. {
  124. autoremove_wake_function(wait, mode, sync, key);
  125. return 1;
  126. }
  127. /*
  128. * returns with the extent buffer spinlocked.
  129. *
  130. * This will spin and/or wait as required to take the lock, and then
  131. * return with the spinlock held.
  132. *
  133. * After this call, scheduling is not safe without first calling
  134. * btrfs_set_lock_blocking()
  135. */
  136. int btrfs_tree_lock(struct extent_buffer *eb)
  137. {
  138. DEFINE_WAIT(wait);
  139. wait.func = btrfs_wake_function;
  140. while(1) {
  141. spin_nested(eb);
  142. /* nobody is blocking, exit with the spinlock held */
  143. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  144. return 0;
  145. /*
  146. * we have the spinlock, but the real owner is blocking.
  147. * wait for them
  148. */
  149. spin_unlock(&eb->lock);
  150. /*
  151. * spin for a bit, and if the blocking flag goes away,
  152. * loop around
  153. */
  154. if (btrfs_spin_on_block(eb))
  155. continue;
  156. prepare_to_wait_exclusive(&eb->lock_wq, &wait,
  157. TASK_UNINTERRUPTIBLE);
  158. if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  159. schedule();
  160. finish_wait(&eb->lock_wq, &wait);
  161. }
  162. return 0;
  163. }
  164. /*
  165. * Very quick trylock, this does not spin or schedule. It returns
  166. * 1 with the spinlock held if it was able to take the lock, or it
  167. * returns zero if it was unable to take the lock.
  168. *
  169. * After this call, scheduling is not safe without first calling
  170. * btrfs_set_lock_blocking()
  171. */
  172. int btrfs_try_tree_lock(struct extent_buffer *eb)
  173. {
  174. if (spin_trylock(&eb->lock)) {
  175. if (test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags)) {
  176. /*
  177. * we've got the spinlock, but the real owner is
  178. * blocking. Drop the spinlock and return failure
  179. */
  180. spin_unlock(&eb->lock);
  181. return 0;
  182. }
  183. return 1;
  184. }
  185. /* someone else has the spinlock giveup */
  186. return 0;
  187. }
  188. int btrfs_tree_unlock(struct extent_buffer *eb)
  189. {
  190. /*
  191. * if we were a blocking owner, we don't have the spinlock held
  192. * just clear the bit and look for waiters
  193. */
  194. if (test_and_clear_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  195. smp_mb__after_clear_bit();
  196. else
  197. spin_unlock(&eb->lock);
  198. if (waitqueue_active(&eb->lock_wq))
  199. wake_up(&eb->lock_wq);
  200. return 0;
  201. }
  202. void btrfs_assert_tree_locked(struct extent_buffer *eb)
  203. {
  204. if (!test_bit(EXTENT_BUFFER_BLOCKING, &eb->bflags))
  205. assert_spin_locked(&eb->lock);
  206. }