Skip to contentSkip to conversation

rwsem: add rwsem_is_contended

PATCH In review Josef Bacik 7 participants 18 messages Aug 30, 2013 – Sep 17, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rwsem: add rwsem_is_contended

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

---
I've cc'ed people who seemed like they may be in charge/familiar with this code, hopefully I got the right people.

 include/linux/rwsem.h |    1 +
 lib/rwsem.c           |   17 +++++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
include/linux/rwsem.h +1 -0
index 0616ffe..61a8802 100644
@@ -35,6 +35,7 @@ extern struct rw_semaphore *rwsem_down_read_failed(struct rw_semaphore *sem);
 extern struct rw_semaphore *rwsem_down_write_failed(struct rw_semaphore *sem);
 extern struct rw_semaphore *rwsem_wake(struct rw_semaphore *);
 extern struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem);
+extern int rwsem_is_contended(struct rw_semaphore *sem);
 
 /* Include the arch specific part */
 #include <asm/rwsem.h>
lib/rwsem.c +17 -0
index 19c5fa9..20858cd 100644
@@ -287,6 +287,23 @@ struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem)
 	return sem;
 }
 
+/*
+ * check to see if the rwsem we're holding has anybody waiting to acquire it.
+ */
+int rwsem_is_contended(struct rw_semaphore *sem)
+{
+	int ret = 0;
+	unsigned long flags;
+
+	if (!raw_spin_trylock_irqsave(&sem->wait_lock, flags))
+		return 1;
+	if (!list_empty(&sem->wait_list))
+		ret = 1;
+	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
+	return ret;
+}
+
+EXPORT_SYMBOL(rwsem_is_contended);
 EXPORT_SYMBOL(rwsem_down_read_failed);
 EXPORT_SYMBOL(rwsem_down_write_failed);
 EXPORT_SYMBOL(rwsem_wake);
Signature · 1 line
1.7.7.6
maintainer
#1 lore raw
Quoted 17 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue.

lib/rwsem.c +7 -0
+/*
+ * check to see if the rwsem we're holding has anybody waiting to acquire it.
+ */
+int rwsem_is_contended(struct rw_semaphore *sem)
+{
+	int ret = 0;
+	unsigned long flags;

lib/rwsem.c struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) · line 306

Quoted 10 lines
lib/rwsem.c +10 -0
+
+	if (!raw_spin_trylock_irqsave(&sem->wait_lock, flags))
+		return 1;
+	if (!list_empty(&sem->wait_list))
+		ret = 1;
+	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
+	return ret;
+}
+
+EXPORT_SYMBOL(rwsem_is_contended);

Modeled after spin_is_contended(), so no problem with that. One thing I was wondering about is if it wants to be called rwsem_is_write_contended() or similar, since it explicitly only tests for pending writers.

#2 lore raw
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue.

lib/rwsem.c +14 -0
+/*
+ * check to see if the rwsem we're holding has anybody waiting to acquire it.
+ */
+int rwsem_is_contended(struct rw_semaphore *sem)
+{
+	int ret = 0;
+	unsigned long flags;
+
+	if (!raw_spin_trylock_irqsave(&sem->wait_lock, flags))
+		return 1;
+	if (!list_empty(&sem->wait_list))
+		ret = 1;
+	raw_spin_unlock_irqrestore(&sem->wait_lock, flags);
+	return ret;
Quoted 9 lines

lib/rwsem.c struct rw_semaphore *rwsem_downgrade_wake(struct rw_semaphore *sem) · line 306

Quoted 3 lines
lib/rwsem.c +3 -0
+}
+
+EXPORT_SYMBOL(rwsem_is_contended);

Modeled after spin_is_contended(), so no problem with that. One thing I was wondering about is if it wants to be called rwsem_is_write_contended() or similar, since it explicitly only tests for pending writers.

Well it checks all pending waiters, the waiters list isn't split between readers waiters and write waiters, so people holding the write lock could call this to see if readers are waiting on the lock. Thanks,

Josef

#3 lore raw
In reply to Josef Bacik

Hi Josef,

Quoted 11 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

FYI, I once tried to introduce something like this before, but my use case was pretty weak so it was not accepted at the time. I don't think there were any objections to the API itself though, and I think it's potentially a good idea if you use case justifies it.

Two comments:

- Note that there are two rwsem implementations - if you are going to add functionality to rwsem.h you want to add the same functionality in rwsem-spinlock.h as well.

- I would prefer if you could avoid taking the wait_lock in your rwsem.h implementation. In your use case (read lock is known to be held), checking for sem->count < 0 would be sufficient to indicate a writer is queued (or getting onto the queue). In the general case, some architectures have the various values set up so that RWSEM_WAITING_BIAS != RWSEM_ACTIVE_WRITE_BIAS - for these architectures at least, you can check for waiters by looking if the lowest bit of RWSEM_WAITING_BIAS is set in sem->count.

Signature · 2 lines
Michel "Walken" Lespinasse
A program is never fully debugged until the last user dies.
#4 lore raw
Quoted 13 lines · Michel Lespinasse

Hi Josef,

Quoted 10 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

Quoted 6 lines
Quoted 1 line

FYI, I once tried to introduce something like this before, but my use case was pretty weak so it was not accepted at the time. I don't think there were any objections to the API itself though, and I think it's potentially a good idea if you use case justifies it.

Exactly, I'm concerned about the use case: readers can't starve writers. Of course, lots of existing readers can temporarily prevent a writer from acquiring, but those readers would already have the lock. Any new readers wouldn't be able to prevent a waiting writer from obtaining the lock.

Josef,
Could you be more explicit, maybe with some detailed numbers about the condition you report?

I say that because a subtle bug that could mistakenly wait a reader existed in the rwsem implementation until relatively recently. Is there some other lurking problem?

Quoted 6 lines

Two comments:

- Note that there are two rwsem implementations - if you are going to add functionality to rwsem.h you want to add the same functionality in rwsem-spinlock.h as well.

Quoted 8 lines

- I would prefer if you could avoid taking the wait_lock in your rwsem.h implementation. In your use case (read lock is known to be held), checking for sem->count < 0 would be sufficient to indicate a writer is queued (or getting onto the queue). In the general case, some architectures have the various values set up so that RWSEM_WAITING_BIAS != RWSEM_ACTIVE_WRITE_BIAS - for these architectures at least, you can check for waiters by looking if the lowest bit of RWSEM_WAITING_BIAS is set in sem->count.

Michel,

I'm glad you point out a much better approach --- but why are we considering open-coding down_read_trylock()/down_write_trylock?

Regards,
Peter Hurley

#5 lore raw
Quoted 21 lines · Peter Hurley
Quoted 13 lines · Michel Lespinasse

Hi Josef,

Quoted 10 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

Quoted 6 lines
Quoted 1 line

FYI, I once tried to introduce something like this before, but my use case was pretty weak so it was not accepted at the time. I don't think there were any objections to the API itself though, and I think it's potentially a good idea if you use case justifies it.

Quoted 9 lines

Exactly, I'm concerned about the use case: readers can't starve writers. Of course, lots of existing readers can temporarily prevent a writer from acquiring, but those readers would already have the lock. Any new readers wouldn't be able to prevent a waiting writer from obtaining the lock.

Josef,
Could you be more explicit, maybe with some detailed numbers about the condition you report?

Sure, this came from a community member

http://article.gmane.org/gmane.comp.file-systems.btrfs/28081

With the old approach we could block between 1-2 seconds waiting for this rwsem, and with the new approach where we allow many more of these caching threads we were staving out the writer for 80 seconds.

So what happens is these threads will scan our extent tree to put together the free space cache, and they'll hold this lock while they are doing the scanning. The only way they will drop this lock is if we hit need_resched(), but because these threads are going to do quite a bit of IO I imagine we're not ever being flagged with need_resched() because we schedule while waiting for IO. So these threads will hold onto this lock for bloody ever without giving it up so the committer can take the write lock. His patch to "fix" the problem was to have an atomic that let us know somebody was waiting for a write lock and then we'd drop the reader lock and schedule.

So really we're just using a rwsem in a really mean way for writers. I'm open to other suggestions but I think this probably the cleanest way. Thanks,

Josef

#6 lore raw
Quoted 40 lines · Josef Bacik
Quoted 21 lines · Peter Hurley
Quoted 13 lines · Michel Lespinasse

Hi Josef,

Quoted 10 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

Quoted 6 lines
Quoted 1 line

FYI, I once tried to introduce something like this before, but my use case was pretty weak so it was not accepted at the time. I don't think there were any objections to the API itself though, and I think it's potentially a good idea if you use case justifies it.

Quoted 9 lines

Exactly, I'm concerned about the use case: readers can't starve writers. Of course, lots of existing readers can temporarily prevent a writer from acquiring, but those readers would already have the lock. Any new readers wouldn't be able to prevent a waiting writer from obtaining the lock.

Josef,
Could you be more explicit, maybe with some detailed numbers about the condition you report?

Sure, this came from a community member

http://article.gmane.org/gmane.comp.file-systems.btrfs/28081

With the old approach we could block between 1-2 seconds waiting for this rwsem, and with the new approach where we allow many more of these caching threads we were staving out the writer for 80 seconds.

Quoted 9 lines

So what happens is these threads will scan our extent tree to put together the free space cache, and they'll hold this lock while they are doing the scanning. The only way they will drop this lock is if we hit need_resched(), but because these threads are going to do quite a bit of IO I imagine we're not ever being flagged with need_resched() because we schedule while waiting for IO. So these threads will hold onto this lock for bloody ever without giving it up so the committer can take the write lock. His patch to "fix" the problem was to have an atomic that let us know somebody was waiting for a write lock and then we'd drop the reader lock and schedule.

Thanks for the additional clarification.

Quoted 2 lines

So really we're just using a rwsem in a really mean way for writers. I'm open to other suggestions but I think this probably the cleanest way.

Is there substantial saved state at the point where the caching thread is checking need_resched() that precludes dropping and reacquiring the extent_commit_sem (or before find_next_key())? Not that it's a cleaner solution; just want to understand better the situation.

Regards,
Peter Hurley

#7 lore raw
Quoted 53 lines · Peter Hurley
Quoted 40 lines · Josef Bacik
Quoted 21 lines · Peter Hurley
Quoted 13 lines · Michel Lespinasse

Hi Josef,

Quoted 10 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

Quoted 6 lines
Quoted 1 line

FYI, I once tried to introduce something like this before, but my use case was pretty weak so it was not accepted at the time. I don't think there were any objections to the API itself though, and I think it's potentially a good idea if you use case justifies it.

Quoted 9 lines

Exactly, I'm concerned about the use case: readers can't starve writers. Of course, lots of existing readers can temporarily prevent a writer from acquiring, but those readers would already have the lock. Any new readers wouldn't be able to prevent a waiting writer from obtaining the lock.

Josef,
Could you be more explicit, maybe with some detailed numbers about the condition you report?

Sure, this came from a community member

http://article.gmane.org/gmane.comp.file-systems.btrfs/28081

With the old approach we could block between 1-2 seconds waiting for this rwsem, and with the new approach where we allow many more of these caching threads we were staving out the writer for 80 seconds.

Quoted 9 lines

So what happens is these threads will scan our extent tree to put together the free space cache, and they'll hold this lock while they are doing the scanning. The only way they will drop this lock is if we hit need_resched(), but because these threads are going to do quite a bit of IO I imagine we're not ever being flagged with need_resched() because we schedule while waiting for IO. So these threads will hold onto this lock for bloody ever without giving it up so the committer can take the write lock. His patch to "fix" the problem was to have an atomic that let us know somebody was waiting for a write lock and then we'd drop the reader lock and schedule.

Thanks for the additional clarification.

Quoted 8 lines
Quoted 2 lines

So really we're just using a rwsem in a really mean way for writers. I'm open to other suggestions but I think this probably the cleanest way.

Is there substantial saved state at the point where the caching thread is checking need_resched() that precludes dropping and reacquiring the extent_commit_sem (or before find_next_key())? Not that it's a cleaner solution; just want to understand better the situation.

Yes I had thought of just dropping our locks everytime we had to do find_next_key() but that isn't going to work. We do have to save state but that's not the hard part, it's the fact that we could race with the committing transaction and lose space. So what would happen is something like this

caching_thread:
save last cached offset
drop locks
find_next_key
get a ref on the current commit root search down to the next leaf
re-take locks
process leaf

transaction committer:
acquire locks
swap commit root
write transaction
unpin all extents up to the last saved cached offset

So if the caching thread grabs a ref on the commit root before the transaction committer swaps out the commit root we are dealing with too old of a tree. So say the leaf we're going to process next has data that was free'ed (and therefore would have been unpinned) during that transaction, because it is after our last cached offset we don't unpin it because we know that the caching thread is going to find the leaf where that extent is not there and add free space for that extent. However we got the leaf from two transactions ago which will still show that extent in use, so we won't add free space for it, which will cause us to leak the extent. We need to make sure we are always consistently on the previous extent root which is why we hold this lock.

I hope that makes sense. Thanks,

Josef

#8 lore raw
In reply to Michel Lespinasse
Quoted 20 lines · Michel Lespinasse

Hi Josef,

Quoted 11 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

FYI, I once tried to introduce something like this before, but my use case was pretty weak so it was not accepted at the time. I don't think there were any objections to the API itself though, and I think it's potentially a good idea if you use case justifies it.

Quoted 6 lines

Two comments:

- Note that there are two rwsem implementations - if you are going to add functionality to rwsem.h you want to add the same functionality in rwsem-spinlock.h as well.

Sure thing.

Quoted 8 lines

- I would prefer if you could avoid taking the wait_lock in your rwsem.h implementation. In your use case (read lock is known to be held), checking for sem->count < 0 would be sufficient to indicate a writer is queued (or getting onto the queue). In the general case, some architectures have the various values set up so that RWSEM_WAITING_BIAS != RWSEM_ACTIVE_WRITE_BIAS - for these architectures at least, you can check for waiters by looking if the lowest bit of RWSEM_WAITING_BIAS is set in sem->count.

Question about this one, I can't just do

if (sem->count < 0)

since each arch has their own atomic way of looking at count, so I'd have to add something to do just a normal read of count for each arch and call that wouldn't I? If that's what you want me to do then I'm fine with that (though I'll need a really thorough review), just want to double check before I make a bunch of extra work for myself. Thanks,

Josef

#9 lore raw
Quoted 39 lines · Josef Bacik
Quoted 20 lines · Michel Lespinasse

Hi Josef,

Quoted 11 lines · Josef Bacik

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

FYI, I once tried to introduce something like this before, but my use case was pretty weak so it was not accepted at the time. I don't think there were any objections to the API itself though, and I think it's potentially a good idea if you use case justifies it.

Quoted 6 lines

Two comments:

- Note that there are two rwsem implementations - if you are going to add functionality to rwsem.h you want to add the same functionality in rwsem-spinlock.h as well.

Sure thing.

Quoted 8 lines

- I would prefer if you could avoid taking the wait_lock in your rwsem.h implementation. In your use case (read lock is known to be held), checking for sem->count < 0 would be sufficient to indicate a writer is queued (or getting onto the queue). In the general case, some architectures have the various values set up so that RWSEM_WAITING_BIAS != RWSEM_ACTIVE_WRITE_BIAS - for these architectures at least, you can check for waiters by looking if the lowest bit of RWSEM_WAITING_BIAS is set in sem->count.

Quoted 7 lines

Question about this one, I can't just do

if (sem->count < 0)

since each arch has their own atomic way of looking at count, so I'd have to add something to do just a normal read of count for each arch and call that wouldn't I?

Reading sem->count is atomic.

For that matter, in your particular use case (which is more heuristic), you could perform the list_empty() check without acquiring the wait_lock.

Regards,
Peter Hurley

maintainer
#10 lore raw
In reply to Josef Bacik
Quoted 10 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

Quoted 5 lines

I've cc'ed people who seemed like they may be in charge/familiar with this code, hopefully I got the right people.

 include/linux/rwsem.h |    1 +
 lib/rwsem.c           |   17 +++++++++++++++++

This will break CONFIG_RWSEM_GENERIC_SPINLOCK=n?

#11 lore raw
Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

Quoted 9 lines

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

So the problem is not that its normal lock starvation, it's more our particular use case that is causing the starvation. We can have lots of people holding readers and simply never give them up for long periods of time, which is why we need this is_contended helper so we know to drop things and let the committer through. Thanks,

Josef

#12 lore raw
Quoted 24 lines · Josef Bacik
Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

Quoted 9 lines

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

Quoted 5 lines

So the problem is not that its normal lock starvation, it's more our particular use case that is causing the starvation. We can have lots of people holding readers and simply never give them up for long periods of time, which is why we need this is_contended helper so we know to drop things and let the committer through. Thanks,

You could easily achieve the same thing by putting an "is_contending" flag in parallel with the rwsem and testing that:

DECLARE_RWSEM(foo);
atomic_t is_contended = ATOMIC_INIT(0);
.
.
.

   /* writing context */
   atomic_inc(&is_contended);
   down_write(&foo);
   do_writing_action();
   up_write(&foo);
   atomic_dec(&is_contended);


   /* reading context */
   down_read(&foo);
   while (!atomic_read(&is_contended))
      do_reading_actions();
   up_read(&foo);

David Daney

#13 lore raw
Quoted 25 lines · David Daney
Quoted 24 lines · Josef Bacik
Quoted 22 lines · Andrew Morton
Quoted 10 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

Quoted 7 lines
Quoted 5 lines

So the problem is not that its normal lock starvation, it's more our particular use case that is causing the starvation. We can have lots of people holding readers and simply never give them up for long periods of time, which is why we need this is_contended helper so we know to drop things and let the committer through. Thanks,

You could easily achieve the same thing by putting an "is_contending" flag in parallel with the rwsem and testing that:

Which adds a bunch more bus-locked operations to contended over, when a unlocked if (list_empty()) is sufficient.

Regards,
Peter Hurley

#14 lore raw
Quoted 45 lines · Peter Hurley
Quoted 44 lines · David Daney
Quoted 42 lines · Josef Bacik
Quoted 22 lines · Andrew Morton
Quoted 9 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a
read lock on this rwsem while they scan the extent tree, and if need_resched()
they will drop the lock and schedule. The transaction commit needs to take a
write lock for this rwsem for a very short period to switch out the commit
roots. If there are a lot of threads doing this caching operation

Quoted 9 lines

we can starve
out the committers which slows everybody out. To address this we want to add
this functionality to see if our rwsem has anybody waiting to take a write lock
so we can drop it and schedule for a bit to allow the commit to continue.
Thanks,

Quoted 9 lines

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

So the problem is not that its normal lock starvation, it's more our particular
use case that is causing the starvation. We can have lots of people holding
readers and simply never give them up for long periods of time, which is why we
need this is_contended helper so we know to drop things and let the committer
through. Thanks,

Quoted 4 lines
Quoted 2 lines

You could easily achieve the same thing by putting an "is_contending" flag in parallel with the rwsem and testing that:

Which adds a bunch more bus-locked operations to contended over

Would that be a problem in this particular case? Has it been measured?

Quoted 2 lines

, when
a unlocked if (list_empty()) is sufficient.

I don't object to adding rwsem_is_contended() *if* it is required. I was just pointing out that there may be other options.

The patch adds a bunch of new semantics to rwsem. There is a trade off between increased complexity of core code, and generalizing subsystem specific optimizations that may not be globally useful.

Is it worth it in this case? I do not know.

<insert quote relating to occam's razor>

David Daney

#15 lore raw
Quoted 56 lines · David Daney
Quoted 45 lines · Peter Hurley
Quoted 44 lines · David Daney
Quoted 42 lines · Josef Bacik
Quoted 22 lines · Andrew Morton
Quoted 9 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a
read lock on this rwsem while they scan the extent tree, and if need_resched()
they will drop the lock and schedule. The transaction commit needs to take a
write lock for this rwsem for a very short period to switch out the commit
roots. If there are a lot of threads doing this caching operation

Quoted 9 lines

we can starve
out the committers which slows everybody out. To address this we want to add
this functionality to see if our rwsem has anybody waiting to take a write lock
so we can drop it and schedule for a bit to allow the commit to continue.
Thanks,

Quoted 9 lines

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

So the problem is not that its normal lock starvation, it's more our particular
use case that is causing the starvation. We can have lots of people holding
readers and simply never give them up for long periods of time, which is why we
need this is_contended helper so we know to drop things and let the committer
through. Thanks,

Quoted 4 lines
Quoted 2 lines

You could easily achieve the same thing by putting an "is_contending" flag in parallel with the rwsem and testing that:

Which adds a bunch more bus-locked operations to contended over

Would that be a problem in this particular case? Has it been measured?

Quoted 2 lines

, when
a unlocked if (list_empty()) is sufficient.

Quoted 9 lines

I don't object to adding rwsem_is_contended() *if* it is required. I was just pointing out that there may be other options.

The patch adds a bunch of new semantics to rwsem. There is a trade off between increased complexity of core code, and generalizing subsystem specific optimizations that may not be globally useful.

Is it worth it in this case? I do not know.

So what you suggested is actually what we did in order to prove that this was what the problem was. I'm ok with continuing to do that, I just figured adding something like rwsem_is_contended() would be nice in case anybody else runs into the issue in the future, plus it would save me an atomic_t in an already large structure. Thanks,

Josef

#16 lore raw
Quoted 64 lines · Josef Bacik
Quoted 63 lines · David Daney
Quoted 45 lines · Peter Hurley
Quoted 44 lines · David Daney
Quoted 42 lines · Josef Bacik
Quoted 22 lines · Andrew Morton
Quoted 9 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a
read lock on this rwsem while they scan the extent tree, and if need_resched()
they will drop the lock and schedule. The transaction commit needs to take a
write lock for this rwsem for a very short period to switch out the commit
roots. If there are a lot of threads doing this caching operation

Quoted 9 lines

we can starve
out the committers which slows everybody out. To address this we want to add
this functionality to see if our rwsem has anybody waiting to take a write lock
so we can drop it and schedule for a bit to allow the commit to continue.
Thanks,

Quoted 9 lines

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

So the problem is not that its normal lock starvation, it's more our particular
use case that is causing the starvation. We can have lots of people holding
readers and simply never give them up for long periods of time, which is why we
need this is_contended helper so we know to drop things and let the committer
through. Thanks,

Quoted 4 lines
Quoted 2 lines

You could easily achieve the same thing by putting an "is_contending" flag in parallel with the rwsem and testing that:

Which adds a bunch more bus-locked operations to contended over

Would that be a problem in this particular case? Has it been measured?

Quoted 2 lines

, when
a unlocked if (list_empty()) is sufficient.

I don't object to adding rwsem_is_contended() *if* it is required. I was just pointing out that there may be other options.

The patch adds a bunch of new semantics to rwsem. There is a trade off between increased complexity of core code, and generalizing subsystem specific optimizations that may not be globally useful.

Quoted 8 lines
Quoted 2 lines

Is it worth it in this case? I do not know.

So what you suggested is actually what we did in order to prove that this was what the problem was. I'm ok with continuing to do that, I just figured adding something like rwsem_is_contended() would be nice in case anybody else runs into the issue in the future, plus it would save me an atomic_t in an already large structure.

I saw the original patch you linked to earlier in the discussion, and I agree that for your use case adding a contention test is cleaner and clearer than other options.

That said, I think this extension is only useful for readers: writers should be getting their business done and releasing the sem.

Also, I think the comment above the function should be clearer that the lock must already be held by the caller; IOW, this is not a trylock replacement.

Regards,
Peter Hurley

#17 lore raw
In reply to Andrew Morton
Quoted 13 lines · Andrew Morton
Quoted 10 lines · On Fri, 30 Aug 2013 10:14:01 -0400 Josef Bacik <[email protected]> wrote:

Btrfs uses an rwsem to control access to its extent tree. Threads will hold a read lock on this rwsem while they scan the extent tree, and if need_resched() they will drop the lock and schedule. The transaction commit needs to take a write lock for this rwsem for a very short period to switch out the commit roots. If there are a lot of threads doing this caching operation we can starve out the committers which slows everybody out. To address this we want to add this functionality to see if our rwsem has anybody waiting to take a write lock so we can drop it and schedule for a bit to allow the commit to continue. Thanks,

Quoted 8 lines

This sounds rather nasty and hacky. Rather then working around a locking shortcoming in a caller it would be better to fix/enhance the core locking code. What would such a change need to do?

Presently rwsem waiters are fifo-queued, are they not? So the commit thread will eventually get that lock. Apparently that's not working adequately for you but I don't fully understand what it is about these dynamics which is causing observable problems.

It would be nice to see the whole solution, together with the btrfs patch.

The problem I have is that this new primitive is only superficially like spin_is_contended(): in the spinlock case dropping the lock will guarantee some sort of progress, because another CPU will almost certainly pick up the lock if we cpu_relax().

In the rwsem case there's no such guarantee of progress, especially if a read-lock is dropped. So I'd like to see how it's implemented in practice.

Thanks,

Ingo