2018-04-04 01:23:33 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-06-26 04:01:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Oracle. All rights reserved.
|
|
|
|
*/
|
2018-04-04 01:23:33 +08:00
|
|
|
|
2008-06-26 04:01:30 +08:00
|
|
|
#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"
|
|
|
|
|
2018-08-24 20:45:20 +08:00
|
|
|
#ifdef CONFIG_BTRFS_DEBUG
|
|
|
|
static void btrfs_assert_spinning_writers_get(struct extent_buffer *eb)
|
|
|
|
{
|
2019-05-02 22:51:53 +08:00
|
|
|
WARN_ON(eb->spinning_writers);
|
|
|
|
eb->spinning_writers++;
|
2018-08-24 20:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void btrfs_assert_spinning_writers_put(struct extent_buffer *eb)
|
|
|
|
{
|
2019-05-02 22:51:53 +08:00
|
|
|
WARN_ON(eb->spinning_writers != 1);
|
|
|
|
eb->spinning_writers--;
|
2018-08-24 20:45:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void btrfs_assert_no_spinning_writers(struct extent_buffer *eb)
|
|
|
|
{
|
2019-05-02 22:51:53 +08:00
|
|
|
WARN_ON(eb->spinning_writers);
|
2018-08-24 20:45:20 +08:00
|
|
|
}
|
|
|
|
|
2018-08-24 21:53:42 +08:00
|
|
|
static void btrfs_assert_spinning_readers_get(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
atomic_inc(&eb->spinning_readers);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btrfs_assert_spinning_readers_put(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
WARN_ON(atomic_read(&eb->spinning_readers) == 0);
|
|
|
|
atomic_dec(&eb->spinning_readers);
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:13:41 +08:00
|
|
|
static void btrfs_assert_tree_read_locks_get(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
atomic_inc(&eb->read_locks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btrfs_assert_tree_read_locks_put(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
atomic_dec(&eb->read_locks);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
|
|
|
|
{
|
|
|
|
BUG_ON(!atomic_read(&eb->read_locks));
|
|
|
|
}
|
|
|
|
|
2018-08-24 22:20:02 +08:00
|
|
|
static void btrfs_assert_tree_write_locks_get(struct extent_buffer *eb)
|
|
|
|
{
|
2019-05-02 22:53:47 +08:00
|
|
|
eb->write_locks++;
|
2018-08-24 22:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void btrfs_assert_tree_write_locks_put(struct extent_buffer *eb)
|
|
|
|
{
|
2019-05-02 22:53:47 +08:00
|
|
|
eb->write_locks--;
|
2018-08-24 22:20:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void btrfs_assert_tree_locked(struct extent_buffer *eb)
|
|
|
|
{
|
2019-05-02 22:53:47 +08:00
|
|
|
BUG_ON(!eb->write_locks);
|
2018-08-24 22:20:02 +08:00
|
|
|
}
|
|
|
|
|
2018-08-24 20:45:20 +08:00
|
|
|
#else
|
|
|
|
static void btrfs_assert_spinning_writers_get(struct extent_buffer *eb) { }
|
|
|
|
static void btrfs_assert_spinning_writers_put(struct extent_buffer *eb) { }
|
|
|
|
static void btrfs_assert_no_spinning_writers(struct extent_buffer *eb) { }
|
2018-08-24 21:53:42 +08:00
|
|
|
static void btrfs_assert_spinning_readers_put(struct extent_buffer *eb) { }
|
|
|
|
static void btrfs_assert_spinning_readers_get(struct extent_buffer *eb) { }
|
2018-08-24 22:13:41 +08:00
|
|
|
static void btrfs_assert_tree_read_locked(struct extent_buffer *eb) { }
|
|
|
|
static void btrfs_assert_tree_read_locks_get(struct extent_buffer *eb) { }
|
|
|
|
static void btrfs_assert_tree_read_locks_put(struct extent_buffer *eb) { }
|
2018-08-24 22:20:02 +08:00
|
|
|
void btrfs_assert_tree_locked(struct extent_buffer *eb) { }
|
|
|
|
static void btrfs_assert_tree_write_locks_get(struct extent_buffer *eb) { }
|
|
|
|
static void btrfs_assert_tree_write_locks_put(struct extent_buffer *eb) { }
|
2018-08-24 20:45:20 +08:00
|
|
|
#endif
|
|
|
|
|
2018-04-04 07:43:05 +08:00
|
|
|
void btrfs_set_lock_blocking_read(struct extent_buffer *eb)
|
|
|
|
{
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_set_lock_blocking_read(eb);
|
2018-04-04 07:43:05 +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;
|
|
|
|
btrfs_assert_tree_read_locked(eb);
|
|
|
|
atomic_inc(&eb->blocking_readers);
|
2018-08-24 21:57:38 +08:00
|
|
|
btrfs_assert_spinning_readers_put(eb);
|
2018-04-04 07:43:05 +08:00
|
|
|
read_unlock(&eb->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void btrfs_set_lock_blocking_write(struct extent_buffer *eb)
|
2008-06-26 04:01:30 +08:00
|
|
|
{
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_set_lock_blocking_write(eb);
|
2014-06-20 05:16:52 +08:00
|
|
|
/*
|
2018-04-04 07:43:05 +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.
|
2014-06-20 05:16:52 +08:00
|
|
|
*/
|
|
|
|
if (eb->lock_nested && current->pid == eb->lock_owner)
|
|
|
|
return;
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers == 0) {
|
2018-08-24 20:56:28 +08:00
|
|
|
btrfs_assert_spinning_writers_put(eb);
|
2018-04-04 07:43:05 +08:00
|
|
|
btrfs_assert_tree_locked(eb);
|
2019-05-02 22:47:23 +08:00
|
|
|
eb->blocking_writers++;
|
2018-04-04 07:43:05 +08:00
|
|
|
write_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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
* 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
|
|
|
{
|
2019-04-15 21:15:24 +08:00
|
|
|
u64 start_ns = 0;
|
|
|
|
|
|
|
|
if (trace_btrfs_tree_read_lock_enabled())
|
|
|
|
start_ns = ktime_get_ns();
|
2011-07-17 03:23:14 +08:00
|
|
|
again:
|
2011-09-13 16:55:48 +08:00
|
|
|
read_lock(&eb->lock);
|
2019-05-02 22:47:23 +08:00
|
|
|
BUG_ON(eb->blocking_writers == 0 &&
|
|
|
|
current->pid == eb->lock_owner);
|
|
|
|
if (eb->blocking_writers && current->pid == eb->lock_owner) {
|
2011-09-13 16:55:48 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
2018-08-24 22:31:17 +08:00
|
|
|
eb->lock_nested = true;
|
2011-09-13 16:55:48 +08:00
|
|
|
read_unlock(&eb->lock);
|
2019-04-15 21:15:24 +08:00
|
|
|
trace_btrfs_tree_read_lock(eb, start_ns);
|
2011-09-13 16:55:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers) {
|
2011-07-17 03:23:14 +08:00
|
|
|
read_unlock(&eb->lock);
|
2012-12-27 17:01:22 +08:00
|
|
|
wait_event(eb->write_lock_wq,
|
2019-05-02 22:47:23 +08:00
|
|
|
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
|
|
|
}
|
2018-08-24 22:15:51 +08:00
|
|
|
btrfs_assert_tree_read_locks_get(eb);
|
2018-08-24 21:57:38 +08:00
|
|
|
btrfs_assert_spinning_readers_get(eb);
|
2019-04-15 21:15:24 +08:00
|
|
|
trace_btrfs_tree_read_lock(eb, start_ns);
|
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)
|
|
|
|
{
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers)
|
2014-11-20 02:25:09 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
read_lock(&eb->lock);
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers) {
|
2014-11-20 02:25:09 +08:00
|
|
|
read_unlock(&eb->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-24 22:15:51 +08:00
|
|
|
btrfs_assert_tree_read_locks_get(eb);
|
2018-08-24 21:57:38 +08:00
|
|
|
btrfs_assert_spinning_readers_get(eb);
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_tree_read_lock_atomic(eb);
|
2014-11-20 02:25:09 +08:00
|
|
|
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
|
|
|
{
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers)
|
2011-07-17 03:23:14 +08:00
|
|
|
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;
|
|
|
|
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers) {
|
2011-07-17 03:23:14 +08:00
|
|
|
read_unlock(&eb->lock);
|
|
|
|
return 0;
|
2009-03-13 23:00:37 +08:00
|
|
|
}
|
2018-08-24 22:15:51 +08:00
|
|
|
btrfs_assert_tree_read_locks_get(eb);
|
2018-08-24 21:57:38 +08:00
|
|
|
btrfs_assert_spinning_readers_get(eb);
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_try_tree_read_lock(eb);
|
2011-07-17 03:23:14 +08:00
|
|
|
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
|
|
|
{
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers || atomic_read(&eb->blocking_readers))
|
2011-07-17 03:23:14 +08:00
|
|
|
return 0;
|
2014-06-20 05:16:52 +08:00
|
|
|
|
2014-11-20 02:25:09 +08:00
|
|
|
write_lock(&eb->lock);
|
2019-05-02 22:47:23 +08:00
|
|
|
if (eb->blocking_writers || atomic_read(&eb->blocking_readers)) {
|
2011-07-17 03:23:14 +08:00
|
|
|
write_unlock(&eb->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-24 22:24:26 +08:00
|
|
|
btrfs_assert_tree_write_locks_get(eb);
|
2018-08-24 20:56:28 +08:00
|
|
|
btrfs_assert_spinning_writers_get(eb);
|
2011-09-13 16:55:48 +08:00
|
|
|
eb->lock_owner = current->pid;
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_try_tree_write_lock(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
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-07-17 03:23:14 +08:00
|
|
|
* drop a spinning read lock
|
|
|
|
*/
|
|
|
|
void btrfs_tree_read_unlock(struct extent_buffer *eb)
|
|
|
|
{
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_tree_read_unlock(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) {
|
2018-08-24 22:31:17 +08:00
|
|
|
eb->lock_nested = false;
|
2014-06-20 05:16:52 +08:00
|
|
|
return;
|
2011-09-13 16:55:48 +08:00
|
|
|
}
|
2011-07-17 03:23:14 +08:00
|
|
|
btrfs_assert_tree_read_locked(eb);
|
2018-08-24 21:57:38 +08:00
|
|
|
btrfs_assert_spinning_readers_put(eb);
|
2018-08-24 22:15:51 +08:00
|
|
|
btrfs_assert_tree_read_locks_put(eb);
|
2011-07-17 03:23:14 +08:00
|
|
|
read_unlock(&eb->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* drop a blocking read lock
|
|
|
|
*/
|
|
|
|
void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
|
|
|
|
{
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_tree_read_unlock_blocking(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) {
|
2018-08-24 22:31:17 +08:00
|
|
|
eb->lock_nested = false;
|
2014-06-20 05:16:52 +08:00
|
|
|
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);
|
2018-02-26 23:15:17 +08:00
|
|
|
/* atomic_dec_and_test implies a barrier */
|
|
|
|
if (atomic_dec_and_test(&eb->blocking_readers))
|
|
|
|
cond_wake_up_nomb(&eb->read_lock_wq);
|
2018-08-24 22:15:51 +08:00
|
|
|
btrfs_assert_tree_read_locks_put(eb);
|
2011-07-17 03:23:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
{
|
2019-04-15 21:15:24 +08:00
|
|
|
u64 start_ns = 0;
|
|
|
|
|
|
|
|
if (trace_btrfs_tree_lock_enabled())
|
|
|
|
start_ns = ktime_get_ns();
|
|
|
|
|
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);
|
2019-05-02 22:47:23 +08:00
|
|
|
wait_event(eb->write_lock_wq, eb->blocking_writers == 0);
|
2011-07-17 03:23:14 +08:00
|
|
|
write_lock(&eb->lock);
|
2019-05-02 22:47:23 +08:00
|
|
|
if (atomic_read(&eb->blocking_readers) || eb->blocking_writers) {
|
2011-07-17 03:23:14 +08:00
|
|
|
write_unlock(&eb->lock);
|
|
|
|
goto again;
|
|
|
|
}
|
2018-08-24 20:56:28 +08:00
|
|
|
btrfs_assert_spinning_writers_get(eb);
|
2018-08-24 22:24:26 +08:00
|
|
|
btrfs_assert_tree_write_locks_get(eb);
|
2011-09-13 16:55:48 +08:00
|
|
|
eb->lock_owner = current->pid;
|
2019-04-15 21:15:24 +08:00
|
|
|
trace_btrfs_tree_lock(eb, start_ns);
|
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
|
|
|
{
|
2019-05-02 22:47:23 +08:00
|
|
|
int blockers = eb->blocking_writers;
|
2011-07-17 03:23:14 +08:00
|
|
|
|
|
|
|
BUG_ON(blockers > 1);
|
|
|
|
|
|
|
|
btrfs_assert_tree_locked(eb);
|
2019-04-15 21:15:25 +08:00
|
|
|
trace_btrfs_tree_unlock(eb);
|
2014-06-20 05:16:52 +08:00
|
|
|
eb->lock_owner = 0;
|
2018-08-24 22:24:26 +08:00
|
|
|
btrfs_assert_tree_write_locks_put(eb);
|
2011-07-17 03:23:14 +08:00
|
|
|
|
|
|
|
if (blockers) {
|
2018-08-24 20:56:28 +08:00
|
|
|
btrfs_assert_no_spinning_writers(eb);
|
2019-05-02 22:47:23 +08:00
|
|
|
eb->blocking_writers--;
|
2019-07-25 16:27:29 +08:00
|
|
|
/*
|
|
|
|
* We need to order modifying blocking_writers above with
|
|
|
|
* actually waking up the sleepers to ensure they see the
|
|
|
|
* updated value of blocking_writers
|
|
|
|
*/
|
|
|
|
cond_wake_up(&eb->write_lock_wq);
|
2011-07-17 03:23:14 +08:00
|
|
|
} else {
|
2018-08-24 20:56:28 +08:00
|
|
|
btrfs_assert_spinning_writers_put(eb);
|
2011-07-17 03:23:14 +08:00
|
|
|
write_unlock(&eb->lock);
|
|
|
|
}
|
2008-06-26 04:01:30 +08:00
|
|
|
}
|