2008-06-26 04:01:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/page-flags.h>
|
2008-07-24 21:51:08 +08:00
|
|
|
#include <asm/bug.h>
|
2008-06-26 04:01:30 +08:00
|
|
|
#include "ctree.h"
|
|
|
|
#include "extent_io.h"
|
|
|
|
#include "locking.h"
|
|
|
|
|
2013-04-26 04:41:01 +08:00
|
|
|
static void btrfs_assert_tree_read_locked(struct extent_buffer *eb);
|
2009-01-06 10:25:51 +08:00
|
|
|
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
/*
|
2011-07-17 03:23:14 +08:00
|
|
|
* if we currently have a spinning reader or writer lock
|
|
|
|
* (indicated by the rw flag) this will bump the count
|
|
|
|
* of blocking holders and drop the spinlock.
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
*/
|
2011-07-17 03:23:14 +08:00
|
|
|
void btrfs_set_lock_blocking_rw(struct extent_buffer *eb, int rw)
|
2008-06-26 04:01:30 +08:00
|
|
|
{
|
2014-06-20 05:16:52 +08:00
|
|
|
/*
|
|
|
|
* no lock is required. The lock owner may change if
|
|
|
|
* we have a read lock, but it won't change to or away
|
|
|
|
* from us. If we have the write lock, we are the owner
|
|
|
|
* and it'll never change.
|
|
|
|
*/
|
|
|
|
if (eb->lock_nested && current->pid == eb->lock_owner)
|
|
|
|
return;
|
2011-07-17 03:23:14 +08:00
|
|
|
if (rw == BTRFS_WRITE_LOCK) {
|
|
|
|
if (atomic_read(&eb->blocking_writers) == 0) {
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_writers) != 1);
|
|
|
|
atomic_dec(&eb->spinning_writers);
|
|
|
|
btrfs_assert_tree_locked(eb);
|
|
|
|
atomic_inc(&eb->blocking_writers);
|
|
|
|
write_unlock(&eb->lock);
|
|
|
|
}
|
|
|
|
} else if (rw == BTRFS_READ_LOCK) {
|
|
|
|
btrfs_assert_tree_read_locked(eb);
|
|
|
|
atomic_inc(&eb->blocking_readers);
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_readers) == 0);
|
|
|
|
atomic_dec(&eb->spinning_readers);
|
|
|
|
read_unlock(&eb->lock);
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
return;
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
}
|
2008-06-26 04:14:04 +08:00
|
|
|
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
/*
|
2011-07-17 03:23:14 +08:00
|
|
|
* if we currently have a blocking lock, take the spinlock
|
|
|
|
* and drop our blocking count
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
*/
|
2011-07-17 03:23:14 +08:00
|
|
|
void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw)
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
{
|
2014-06-20 05:16:52 +08:00
|
|
|
/*
|
|
|
|
* no lock is required. The lock owner may change if
|
|
|
|
* we have a read lock, but it won't change to or away
|
|
|
|
* from us. If we have the write lock, we are the owner
|
|
|
|
* and it'll never change.
|
|
|
|
*/
|
|
|
|
if (eb->lock_nested && current->pid == eb->lock_owner)
|
|
|
|
return;
|
|
|
|
|
2011-07-17 03:23:14 +08:00
|
|
|
if (rw == BTRFS_WRITE_LOCK_BLOCKING) {
|
|
|
|
BUG_ON(atomic_read(&eb->blocking_writers) != 1);
|
|
|
|
write_lock(&eb->lock);
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_writers));
|
|
|
|
atomic_inc(&eb->spinning_writers);
|
2015-02-17 02:41:40 +08:00
|
|
|
/*
|
|
|
|
* atomic_dec_and_test implies a barrier for waitqueue_active
|
|
|
|
*/
|
2012-07-24 03:25:05 +08:00
|
|
|
if (atomic_dec_and_test(&eb->blocking_writers) &&
|
|
|
|
waitqueue_active(&eb->write_lock_wq))
|
2011-07-17 03:23:14 +08:00
|
|
|
wake_up(&eb->write_lock_wq);
|
|
|
|
} else if (rw == BTRFS_READ_LOCK_BLOCKING) {
|
|
|
|
BUG_ON(atomic_read(&eb->blocking_readers) == 0);
|
|
|
|
read_lock(&eb->lock);
|
|
|
|
atomic_inc(&eb->spinning_readers);
|
2015-02-17 02:41:40 +08:00
|
|
|
/*
|
|
|
|
* atomic_dec_and_test implies a barrier for waitqueue_active
|
|
|
|
*/
|
2012-07-24 03:25:05 +08:00
|
|
|
if (atomic_dec_and_test(&eb->blocking_readers) &&
|
|
|
|
waitqueue_active(&eb->read_lock_wq))
|
2011-07-17 03:23:14 +08:00
|
|
|
wake_up(&eb->read_lock_wq);
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
return;
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-07-17 03:23:14 +08:00
|
|
|
* take a spinning read lock. This will wait for any blocking
|
|
|
|
* writers
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
*/
|
2011-07-17 03:23:14 +08:00
|
|
|
void btrfs_tree_read_lock(struct extent_buffer *eb)
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
{
|
2011-07-17 03:23:14 +08:00
|
|
|
again:
|
2014-06-20 05:16:52 +08:00
|
|
|
BUG_ON(!atomic_read(&eb->blocking_writers) &&
|
|
|
|
current->pid == eb->lock_owner);
|
|
|
|
|
2011-09-13 16:55:48 +08:00
|
|
|
read_lock(&eb->lock);
|
|
|
|
if (atomic_read(&eb->blocking_writers) &&
|
|
|
|
current->pid == eb->lock_owner) {
|
|
|
|
/*
|
|
|
|
* This extent is already write-locked by our thread. We allow
|
|
|
|
* an additional read lock to be added because it's for the same
|
|
|
|
* thread. btrfs_find_all_roots() depends on this as it may be
|
|
|
|
* called on a partly (write-)locked tree.
|
|
|
|
*/
|
|
|
|
BUG_ON(eb->lock_nested);
|
|
|
|
eb->lock_nested = 1;
|
|
|
|
read_unlock(&eb->lock);
|
|
|
|
return;
|
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
if (atomic_read(&eb->blocking_writers)) {
|
|
|
|
read_unlock(&eb->lock);
|
2012-12-27 17:01:22 +08:00
|
|
|
wait_event(eb->write_lock_wq,
|
|
|
|
atomic_read(&eb->blocking_writers) == 0);
|
2011-07-17 03:23:14 +08:00
|
|
|
goto again;
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
atomic_inc(&eb->read_locks);
|
|
|
|
atomic_inc(&eb->spinning_readers);
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
}
|
|
|
|
|
2014-11-20 02:25:09 +08:00
|
|
|
/*
|
|
|
|
* take a spinning read lock.
|
|
|
|
* returns 1 if we get the read lock and 0 if we don't
|
|
|
|
* this won't wait for blocking writers
|
|
|
|
*/
|
|
|
|
int btrfs_tree_read_lock_atomic(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
if (atomic_read(&eb->blocking_writers))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
read_lock(&eb->lock);
|
|
|
|
if (atomic_read(&eb->blocking_writers)) {
|
|
|
|
read_unlock(&eb->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
atomic_inc(&eb->read_locks);
|
|
|
|
atomic_inc(&eb->spinning_readers);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
/*
|
2011-07-17 03:23:14 +08:00
|
|
|
* returns 1 if we get the read lock and 0 if we don't
|
|
|
|
* this won't wait for blocking writers
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
*/
|
2011-07-17 03:23:14 +08:00
|
|
|
int btrfs_try_tree_read_lock(struct extent_buffer *eb)
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
{
|
2011-07-17 03:23:14 +08:00
|
|
|
if (atomic_read(&eb->blocking_writers))
|
|
|
|
return 0;
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
|
2014-06-20 05:16:52 +08:00
|
|
|
if (!read_trylock(&eb->lock))
|
|
|
|
return 0;
|
|
|
|
|
2011-07-17 03:23:14 +08:00
|
|
|
if (atomic_read(&eb->blocking_writers)) {
|
|
|
|
read_unlock(&eb->lock);
|
|
|
|
return 0;
|
2009-03-13 23:00:37 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
atomic_inc(&eb->read_locks);
|
|
|
|
atomic_inc(&eb->spinning_readers);
|
|
|
|
return 1;
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-07-17 03:23:14 +08:00
|
|
|
* returns 1 if we get the read lock and 0 if we don't
|
|
|
|
* this won't wait for blocking writers or readers
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
*/
|
2011-07-17 03:23:14 +08:00
|
|
|
int btrfs_try_tree_write_lock(struct extent_buffer *eb)
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
{
|
2011-07-17 03:23:14 +08:00
|
|
|
if (atomic_read(&eb->blocking_writers) ||
|
|
|
|
atomic_read(&eb->blocking_readers))
|
|
|
|
return 0;
|
2014-06-20 05:16:52 +08:00
|
|
|
|
2014-11-20 02:25:09 +08:00
|
|
|
write_lock(&eb->lock);
|
2011-07-17 03:23:14 +08:00
|
|
|
if (atomic_read(&eb->blocking_writers) ||
|
|
|
|
atomic_read(&eb->blocking_readers)) {
|
|
|
|
write_unlock(&eb->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
atomic_inc(&eb->write_locks);
|
|
|
|
atomic_inc(&eb->spinning_writers);
|
2011-09-13 16:55:48 +08:00
|
|
|
eb->lock_owner = current->pid;
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-07-17 03:23:14 +08:00
|
|
|
* drop a spinning read lock
|
|
|
|
*/
|
|
|
|
void btrfs_tree_read_unlock(struct extent_buffer *eb)
|
|
|
|
{
|
2014-06-20 05:16:52 +08:00
|
|
|
/*
|
|
|
|
* if we're nested, we have the write lock. No new locking
|
|
|
|
* is needed as long as we are the lock owner.
|
|
|
|
* The write unlock will do a barrier for us, and the lock_nested
|
|
|
|
* field only matters to the lock owner.
|
|
|
|
*/
|
|
|
|
if (eb->lock_nested && current->pid == eb->lock_owner) {
|
|
|
|
eb->lock_nested = 0;
|
|
|
|
return;
|
2011-09-13 16:55:48 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
btrfs_assert_tree_read_locked(eb);
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_readers) == 0);
|
|
|
|
atomic_dec(&eb->spinning_readers);
|
|
|
|
atomic_dec(&eb->read_locks);
|
|
|
|
read_unlock(&eb->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* drop a blocking read lock
|
|
|
|
*/
|
|
|
|
void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
|
|
|
|
{
|
2014-06-20 05:16:52 +08:00
|
|
|
/*
|
|
|
|
* if we're nested, we have the write lock. No new locking
|
|
|
|
* is needed as long as we are the lock owner.
|
|
|
|
* The write unlock will do a barrier for us, and the lock_nested
|
|
|
|
* field only matters to the lock owner.
|
|
|
|
*/
|
|
|
|
if (eb->lock_nested && current->pid == eb->lock_owner) {
|
|
|
|
eb->lock_nested = 0;
|
|
|
|
return;
|
2011-09-13 16:55:48 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
btrfs_assert_tree_read_locked(eb);
|
|
|
|
WARN_ON(atomic_read(&eb->blocking_readers) == 0);
|
2015-02-17 02:41:40 +08:00
|
|
|
/*
|
|
|
|
* atomic_dec_and_test implies a barrier for waitqueue_active
|
|
|
|
*/
|
2012-07-24 03:25:05 +08:00
|
|
|
if (atomic_dec_and_test(&eb->blocking_readers) &&
|
|
|
|
waitqueue_active(&eb->read_lock_wq))
|
2011-07-17 03:23:14 +08:00
|
|
|
wake_up(&eb->read_lock_wq);
|
|
|
|
atomic_dec(&eb->read_locks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* take a spinning write lock. This will wait for both
|
|
|
|
* blocking readers or writers
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
*/
|
2012-03-01 21:56:26 +08:00
|
|
|
void btrfs_tree_lock(struct extent_buffer *eb)
|
Btrfs: Change btree locking to use explicit blocking points
Most of the btrfs metadata operations can be protected by a spinlock,
but some operations still need to schedule.
So far, btrfs has been using a mutex along with a trylock loop,
most of the time it is able to avoid going for the full mutex, so
the trylock loop is a big performance gain.
This commit is step one for getting rid of the blocking locks entirely.
btrfs_tree_lock takes a spinlock, and the code explicitly switches
to a blocking lock when it starts an operation that can schedule.
We'll be able get rid of the blocking locks in smaller pieces over time.
Tracing allows us to find the most common cause of blocking, so we
can start with the hot spots first.
The basic idea is:
btrfs_tree_lock() returns with the spin lock held
btrfs_set_lock_blocking() sets the EXTENT_BUFFER_BLOCKING bit in
the extent buffer flags, and then drops the spin lock. The buffer is
still considered locked by all of the btrfs code.
If btrfs_tree_lock gets the spinlock but finds the blocking bit set, it drops
the spin lock and waits on a wait queue for the blocking bit to go away.
Much of the code that needs to set the blocking bit finishes without actually
blocking a good percentage of the time. So, an adaptive spin is still
used against the blocking bit to avoid very high context switch rates.
btrfs_clear_lock_blocking() clears the blocking bit and returns
with the spinlock held again.
btrfs_tree_unlock() can be called on either blocking or spinning locks,
it does the right thing based on the blocking bit.
ctree.c has a helper function to set/clear all the locked buffers in a
path as blocking.
Signed-off-by: Chris Mason <chris.mason@oracle.com>
2009-02-04 22:25:08 +08:00
|
|
|
{
|
2015-08-06 22:39:36 +08:00
|
|
|
WARN_ON(eb->lock_owner == current->pid);
|
2011-07-17 03:23:14 +08:00
|
|
|
again:
|
|
|
|
wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
|
|
|
|
wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
|
|
|
|
write_lock(&eb->lock);
|
|
|
|
if (atomic_read(&eb->blocking_readers)) {
|
|
|
|
write_unlock(&eb->lock);
|
|
|
|
wait_event(eb->read_lock_wq,
|
|
|
|
atomic_read(&eb->blocking_readers) == 0);
|
|
|
|
goto again;
|
2008-06-26 04:14:04 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
if (atomic_read(&eb->blocking_writers)) {
|
|
|
|
write_unlock(&eb->lock);
|
|
|
|
wait_event(eb->write_lock_wq,
|
|
|
|
atomic_read(&eb->blocking_writers) == 0);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_writers));
|
|
|
|
atomic_inc(&eb->spinning_writers);
|
|
|
|
atomic_inc(&eb->write_locks);
|
2011-09-13 16:55:48 +08:00
|
|
|
eb->lock_owner = current->pid;
|
2008-06-26 04:01:30 +08:00
|
|
|
}
|
|
|
|
|
2011-07-17 03:23:14 +08:00
|
|
|
/*
|
|
|
|
* drop a spinning or a blocking write lock.
|
|
|
|
*/
|
2012-03-01 21:56:26 +08:00
|
|
|
void btrfs_tree_unlock(struct extent_buffer *eb)
|
2008-06-26 04:01:30 +08:00
|
|
|
{
|
2011-07-17 03:23:14 +08:00
|
|
|
int blockers = atomic_read(&eb->blocking_writers);
|
|
|
|
|
|
|
|
BUG_ON(blockers > 1);
|
|
|
|
|
|
|
|
btrfs_assert_tree_locked(eb);
|
2014-06-20 05:16:52 +08:00
|
|
|
eb->lock_owner = 0;
|
2011-07-17 03:23:14 +08:00
|
|
|
atomic_dec(&eb->write_locks);
|
|
|
|
|
|
|
|
if (blockers) {
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_writers));
|
|
|
|
atomic_dec(&eb->blocking_writers);
|
2015-02-17 02:36:47 +08:00
|
|
|
/*
|
|
|
|
* Make sure counter is updated before we wake up waiters.
|
|
|
|
*/
|
2012-07-24 03:25:05 +08:00
|
|
|
smp_mb();
|
|
|
|
if (waitqueue_active(&eb->write_lock_wq))
|
|
|
|
wake_up(&eb->write_lock_wq);
|
2011-07-17 03:23:14 +08:00
|
|
|
} else {
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_writers) != 1);
|
|
|
|
atomic_dec(&eb->spinning_writers);
|
|
|
|
write_unlock(&eb->lock);
|
|
|
|
}
|
2008-06-26 04:01:30 +08:00
|
|
|
}
|
|
|
|
|
2009-03-09 23:45:38 +08:00
|
|
|
void btrfs_assert_tree_locked(struct extent_buffer *eb)
|
2008-06-26 04:01:30 +08:00
|
|
|
{
|
2011-07-17 03:23:14 +08:00
|
|
|
BUG_ON(!atomic_read(&eb->write_locks));
|
|
|
|
}
|
|
|
|
|
2013-04-26 04:41:01 +08:00
|
|
|
static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
|
2011-07-17 03:23:14 +08:00
|
|
|
{
|
|
|
|
BUG_ON(!atomic_read(&eb->read_locks));
|
2008-06-26 04:01:30 +08:00
|
|
|
}
|