* [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment
[not found] <20190428155755.14267-1-longman@redhat.com>
@ 2019-04-28 15:57 ` Waiman Long
2019-04-28 16:07 ` Waiman Long
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Waiman Long @ 2019-04-28 15:57 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Will Deacon, Thomas Gleixner,
Borislav Petkov, H. Peter Anvin
Cc: linux-kernel, x86, Davidlohr Bueso, Linus Torvalds, Tim Chen,
huang ying, Waiman Long, stable
During my rwsem testing, it was found that after a down_read(), the
reader count may occasionally become 0 or even negative. Consequently,
a writer may steal the lock at that time and execute with the reader
in parallel thus breaking the mutual exclusion guarantee of the write
lock. In other words, both readers and writer can become rwsem owners
simultaneously.
The current reader wakeup code does it in one pass to clear waiter->task
and put them into wake_q before fully incrementing the reader count.
Once waiter->task is cleared, the corresponding reader may see it,
finish the critical section and do unlock to decrement the count before
the count is incremented. This is not a problem if there is only one
reader to wake up as the count has been pre-incremented by 1. It is
a problem if there are more than one readers to be woken up and writer
can steal the lock.
The wakeup is actually done in 2 passes before the v4.9 commit
70800c3c0cc5 ("locking/rwsem: Scan the wait_list for readers only
once"). To fix this problem, the wakeup is now done in two passes
again. In the first pass, we collect the readers and count them. The
reader count is then fully incremented. In the second pass, the
waiter->task is then cleared and they are put into wake_q to be woken
up later.
Fixes: 70800c3c0cc5 ("locking/rwsem: Scan the wait_list for readers only once")
Cc: <stable@vger.kernel.org>
Signed-off-by: Waiman Long <longman@redhat.com>
---
kernel/locking/rwsem-xadd.c | 45 +++++++++++++++++++++++++------------
1 file changed, 31 insertions(+), 14 deletions(-)
diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c
index 6b3ee9948bf1..cab5b1f6b2de 100644
--- a/kernel/locking/rwsem-xadd.c
+++ b/kernel/locking/rwsem-xadd.c
@@ -130,6 +130,7 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,
{
struct rwsem_waiter *waiter, *tmp;
long oldcount, woken = 0, adjustment = 0;
+ struct list_head wlist;
/*
* Take a peek at the queue head waiter such that we can determine
@@ -188,18 +189,44 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,
* of the queue. We know that woken will be at least 1 as we accounted
* for above. Note we increment the 'active part' of the count by the
* number of readers before waking any processes up.
+ *
+ * We have to do wakeup in 2 passes to prevent the possibility that
+ * the reader count may be decremented before it is incremented. It
+ * is because the to-be-woken waiter may not have slept yet. So it
+ * may see waiter->task got cleared, finish its critical section and
+ * do an unlock before the reader count increment.
+ *
+ * 1) Collect the read-waiters in a separate list, count them and
+ * fully increment the reader count in rwsem.
+ * 2) For each waiters in the new list, clear waiter->task and
+ * put them into wake_q to be woken up later.
*/
+ INIT_LIST_HEAD(&wlist);
list_for_each_entry_safe(waiter, tmp, &sem->wait_list, list) {
- struct task_struct *tsk;
-
if (waiter->type == RWSEM_WAITING_FOR_WRITE)
break;
woken++;
- tsk = waiter->task;
+ list_move_tail(&waiter->list, &wlist);
+ }
+
+ adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment;
+ lockevent_cond_inc(rwsem_wake_reader, woken);
+ if (list_empty(&sem->wait_list)) {
+ /* hit end of list above */
+ adjustment -= RWSEM_WAITING_BIAS;
+ }
+
+ if (adjustment)
+ atomic_long_add(adjustment, &sem->count);
+
+ /* 2nd pass */
+ list_for_each_entry(waiter, &wlist, list) {
+ struct task_struct *tsk;
+ tsk = waiter->task;
get_task_struct(tsk);
- list_del(&waiter->list);
+
/*
* Ensure calling get_task_struct() before setting the reader
* waiter to nil such that rwsem_down_read_failed() cannot
@@ -213,16 +240,6 @@ static void __rwsem_mark_wake(struct rw_semaphore *sem,
*/
wake_q_add_safe(wake_q, tsk);
}
-
- adjustment = woken * RWSEM_ACTIVE_READ_BIAS - adjustment;
- lockevent_cond_inc(rwsem_wake_reader, woken);
- if (list_empty(&sem->wait_list)) {
- /* hit end of list above */
- adjustment -= RWSEM_WAITING_BIAS;
- }
-
- if (adjustment)
- atomic_long_add(adjustment, &sem->count);
}
/*
--
2.18.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment
2019-04-28 15:57 ` [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment Waiman Long
@ 2019-04-28 16:07 ` Waiman Long
2019-04-28 17:41 ` Linus Torvalds
[not found] ` <20190428203916.E1AA6206A3@mail.kernel.org>
2 siblings, 0 replies; 6+ messages in thread
From: Waiman Long @ 2019-04-28 16:07 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Will Deacon, Thomas Gleixner,
Borislav Petkov, H. Peter Anvin
Cc: linux-kernel, x86, Davidlohr Bueso, Linus Torvalds, Tim Chen,
huang ying, stable
On 4/28/19 11:57 AM, Waiman Long wrote:
> During my rwsem testing, it was found that after a down_read(), the
> reader count may occasionally become 0 or even negative. Consequently,
> a writer may steal the lock at that time and execute with the reader
> in parallel thus breaking the mutual exclusion guarantee of the write
> lock. In other words, both readers and writer can become rwsem owners
> simultaneously.
I would like to have this particular patch merged in the next merge
window, if possible, so that I can backport it downstream.
Thanks,
Longman
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment
2019-04-28 15:57 ` [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment Waiman Long
2019-04-28 16:07 ` Waiman Long
@ 2019-04-28 17:41 ` Linus Torvalds
2019-04-28 17:59 ` Linus Torvalds
[not found] ` <20190428203916.E1AA6206A3@mail.kernel.org>
2 siblings, 1 reply; 6+ messages in thread
From: Linus Torvalds @ 2019-04-28 17:41 UTC (permalink / raw)
To: Waiman Long
Cc: Peter Zijlstra, Ingo Molnar, Will Deacon, Thomas Gleixner,
Borislav Petkov, H. Peter Anvin, Linux List Kernel Mailing,
the arch/x86 maintainers, Davidlohr Bueso, Tim Chen, huang ying,
stable
On Sun, Apr 28, 2019 at 8:58 AM Waiman Long <longman@redhat.com> wrote:
>
> +
> + /* 2nd pass */
> + list_for_each_entry(waiter, &wlist, list) {
This is not safe, as far as I can tell.
As this loop walks the list, you do that
smp_store_release(&waiter->task, NULL);
and that very action means that the "waiter" is now released, and you
cannot possibly use it.
HOWEVER.
"list_for_each_entry()" will load "waiter->next" to go forward in the list.
So you absolutely *have* to use "list_for_each_entry_safe()" in this
loop, I think. You should treat that "smp_store_release()" as if you
deleted the list entry, because you cannot trust it after you've done
it, because the sleeper may have gone its own merry ways and released
the on-stack 'waiter' allocation.
It's the *first* loop that you could play games with, because you hold
the lock, and the list is stable during that loop. So the *first* loop
could just walk the list, and then do one list splitting operation
instead of doing that "list_move_tail()" thing for each entry.
But as long as you do "list_move_tail()" in the first loop, you'll
obviously have to use list_for_each_entry_safe() there too, since
right now you change that list as you walk it.
I'm just saying that you *could* optimize that first phase to just
walk it and then perhaps split it with list_cut_before() when you find
the first entry that isn't going to be woken up.
Linus
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment
2019-04-28 17:41 ` Linus Torvalds
@ 2019-04-28 17:59 ` Linus Torvalds
2019-04-28 19:40 ` Waiman Long
0 siblings, 1 reply; 6+ messages in thread
From: Linus Torvalds @ 2019-04-28 17:59 UTC (permalink / raw)
To: Waiman Long
Cc: Peter Zijlstra, Ingo Molnar, Will Deacon, Thomas Gleixner,
Borislav Petkov, H. Peter Anvin, Linux List Kernel Mailing,
the arch/x86 maintainers, Davidlohr Bueso, Tim Chen, huang ying,
stable
On Sun, Apr 28, 2019 at 10:41 AM Linus Torvalds
<torvalds@linux-foundation.org> wrote:
>
> It's the *first* loop that you could play games with, because you hold
> the lock, and the list is stable during that loop. So the *first* loop
> could just walk the list, and then do one list splitting operation
> instead of doing that "list_move_tail()" thing for each entry.
.. having looked at that, I would suggest against it.
I _think_ this short and sweet code snippet might just work fine for
the first loop:
list_for_each_entry(waiter, &sem->wait_list, list) {
if (waiter->type == RWSEM_WAITING_FOR_WRITE)
break;
woken++;
}
list_cut_before(&wlist, &sem->wait_list, waiter);
and if it *does* work it would be both smaller and more efficient. But
it looks a bit too subtle to my taste. Somebody would need to go
through that with a fine comb, and double-check that it gets the
"whole list" case right, for example.
So the "phase 1" loop could be perhaps simplified to the above cute things.
But the "phase 2" loop absolutely has to be changed to use
list_for_each_entry_safe().
Linus
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment
2019-04-28 17:59 ` Linus Torvalds
@ 2019-04-28 19:40 ` Waiman Long
0 siblings, 0 replies; 6+ messages in thread
From: Waiman Long @ 2019-04-28 19:40 UTC (permalink / raw)
To: Linus Torvalds
Cc: Peter Zijlstra, Ingo Molnar, Will Deacon, Thomas Gleixner,
Borislav Petkov, H. Peter Anvin, Linux List Kernel Mailing,
the arch/x86 maintainers, Davidlohr Bueso, Tim Chen, huang ying,
stable
On 4/28/19 1:59 PM, Linus Torvalds wrote:
> On Sun, Apr 28, 2019 at 10:41 AM Linus Torvalds
> <torvalds@linux-foundation.org> wrote:
>> It's the *first* loop that you could play games with, because you hold
>> the lock, and the list is stable during that loop. So the *first* loop
>> could just walk the list, and then do one list splitting operation
>> instead of doing that "list_move_tail()" thing for each entry.
> .. having looked at that, I would suggest against it.
>
> I _think_ this short and sweet code snippet might just work fine for
> the first loop:
>
> list_for_each_entry(waiter, &sem->wait_list, list) {
> if (waiter->type == RWSEM_WAITING_FOR_WRITE)
> break;
> woken++;
> }
> list_cut_before(&wlist, &sem->wait_list, waiter);
>
> and if it *does* work it would be both smaller and more efficient. But
> it looks a bit too subtle to my taste. Somebody would need to go
> through that with a fine comb, and double-check that it gets the
> "whole list" case right, for example.
>
> So the "phase 1" loop could be perhaps simplified to the above cute things.
>
> But the "phase 2" loop absolutely has to be changed to use
> list_for_each_entry_safe().
>
> Linus
You are right. Thanks for checking my code. I will make the necessary
change.
Cheers,
Longman
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment
[not found] ` <20190428203916.E1AA6206A3@mail.kernel.org>
@ 2019-04-28 20:47 ` Waiman Long
0 siblings, 0 replies; 6+ messages in thread
From: Waiman Long @ 2019-04-28 20:47 UTC (permalink / raw)
To: Sasha Levin, Peter Zijlstra; +Cc: linux-kernel, x86, stable
On 4/28/19 4:39 PM, Sasha Levin wrote:
> Hi,
>
> [This is an automated email]
>
> This commit has been processed because it contains a "Fixes:" tag,
> fixing commit: 70800c3c0cc5 locking/rwsem: Scan the wait_list for readers only once.
>
> The bot has tested the following trees: v5.0.10, v4.19.37, v4.14.114, v4.9.171.
>
> v5.0.10: Failed to apply! Possible dependencies:
> 412f34a82ccf ("locking/qspinlock_stat: Track the no MCS node available case")
> 46ad0840b158 ("locking/rwsem: Remove arch specific rwsem files")
> 579afe866f52 ("xtensa: use generic spinlock/rwlock implementation")
> a8654596f037 ("locking/rwsem: Enable lock event counting")
> ad53fa10fa9e ("locking/qspinlock_stat: Introduce generic lockevent_*() counting APIs")
> c7580c1e8443 ("locking/rwsem: Move owner setting code from rwsem.c to rwsem.h")
> ce9084ba0d1d ("x86: Make ARCH_USE_MEMREMAP_PROT a generic Kconfig symbol")
> d682b596d993 ("locking/qspinlock: Handle > 4 slowpath nesting levels")
> eecec78f7777 ("locking/rwsem: Relocate rwsem_down_read_failed()")
> fb346fd9fc08 ("locking/lock_events: Make lock_events available for all archs & other locks")
>
> v4.19.37: Failed to apply! Possible dependencies:
> 0fa809ca7f81 ("locking/pvqspinlock: Extend node size when pvqspinlock is configured")
> 1222109a5363 ("locking/qspinlock_stat: Count instances of nested lock slowpaths")
> 412f34a82ccf ("locking/qspinlock_stat: Track the no MCS node available case")
> 46ad0840b158 ("locking/rwsem: Remove arch specific rwsem files")
> 4b486b535c33 ("locking/rwsem: Exit read lock slowpath if queue empty & no writer")
> 925b9cd1b89a ("locking/rwsem: Make owner store task pointer of last owning reader")
> a8654596f037 ("locking/rwsem: Enable lock event counting")
> ad53fa10fa9e ("locking/qspinlock_stat: Introduce generic lockevent_*() counting APIs")
> c7580c1e8443 ("locking/rwsem: Move owner setting code from rwsem.c to rwsem.h")
> ce9084ba0d1d ("x86: Make ARCH_USE_MEMREMAP_PROT a generic Kconfig symbol")
> d682b596d993 ("locking/qspinlock: Handle > 4 slowpath nesting levels")
> eecec78f7777 ("locking/rwsem: Relocate rwsem_down_read_failed()")
> fb346fd9fc08 ("locking/lock_events: Make lock_events available for all archs & other locks")
>
> v4.14.114: Failed to apply! Possible dependencies:
> 0fa809ca7f81 ("locking/pvqspinlock: Extend node size when pvqspinlock is configured")
> 11752adb68a3 ("locking/pvqspinlock: Implement hybrid PV queued/unfair locks")
> 1222109a5363 ("locking/qspinlock_stat: Count instances of nested lock slowpaths")
> 1958b5fc4010 ("x86/boot: Add early boot support when running with SEV active")
> 1cd9c22fee3a ("x86/mm/encrypt: Move page table helpers into separate translation unit")
> 271ca788774a ("arch: enable relative relocations for arm64, power and x86")
> 412f34a82ccf ("locking/qspinlock_stat: Track the no MCS node available case")
> 81d3dc9a349b ("locking/qspinlock: Add stat tracking for pending vs. slowpath")
> 94d49eb30e85 ("x86/mm: Decouple dynamic __PHYSICAL_MASK from AMD SME")
> a8654596f037 ("locking/rwsem: Enable lock event counting")
> ad53fa10fa9e ("locking/qspinlock_stat: Introduce generic lockevent_*() counting APIs")
> ce9084ba0d1d ("x86: Make ARCH_USE_MEMREMAP_PROT a generic Kconfig symbol")
> d682b596d993 ("locking/qspinlock: Handle > 4 slowpath nesting levels")
> d7b417fa08d1 ("x86/mm: Add DMA support for SEV memory encryption")
> d8aa7eea78a1 ("x86/mm: Add Secure Encrypted Virtualization (SEV) support")
> dfaaec9033b8 ("x86: Add support for changing memory encryption attribute in early boot")
> fb346fd9fc08 ("locking/lock_events: Make lock_events available for all archs & other locks")
>
> v4.9.171: Failed to apply! Possible dependencies:
> 1a8b6d76dc5b ("net:add one common config ARCH_WANT_RELAX_ORDER to support relax ordering")
> 271ca788774a ("arch: enable relative relocations for arm64, power and x86")
> 29dee3c03abc ("locking/refcounts: Out-of-line everything")
> 40565b5aedd6 ("sched/cputime, powerpc, s390: Make scaled cputime arch specific")
> 4ad8622dc548 ("powerpc/8xx: Implement hw_breakpoint")
> 51c9c0843993 ("powerpc/kprobes: Implement Optprobes")
> 5b9ff0278598 ("powerpc: Build-time sort the exception table")
> 65c059bcaa73 ("powerpc: Enable support for GCC plugins")
> 9fea59bd7ca5 ("powerpc/mm: Add support for runtime configuration of ASLR limits")
> a7d2475af7ae ("powerpc: Sort the selects under CONFIG_PPC")
> a8654596f037 ("locking/rwsem: Enable lock event counting")
> bd174169c7a1 ("locking/refcount: Add refcount_t API kernel-doc comments")
> ce9084ba0d1d ("x86: Make ARCH_USE_MEMREMAP_PROT a generic Kconfig symbol")
> d557d1b58b35 ("refcount: change EXPORT_SYMBOL markings")
> ebfa50df435e ("powerpc: Add helper to check if offset is within relative branch range")
> f405df5de317 ("refcount_t: Introduce a special purpose refcount type")
> fa769d3f58e6 ("powerpc/32: Enable HW_BREAKPOINT on BOOK3S")
> fb346fd9fc08 ("locking/lock_events: Make lock_events available for all archs & other locks")
> fd25d19f6b8d ("locking/refcount: Create unchecked atomic_t implementation")
>
>
> How should we proceed with this patch?
I will send out a version that will be easier to backport once this
patch lands in the mainline.
Cheers,
Longman
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2019-04-28 20:47 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <20190428155755.14267-1-longman@redhat.com>
2019-04-28 15:57 ` [PATCH-tip v6 01/20] locking/rwsem: Prevent decrement of reader count before increment Waiman Long
2019-04-28 16:07 ` Waiman Long
2019-04-28 17:41 ` Linus Torvalds
2019-04-28 17:59 ` Linus Torvalds
2019-04-28 19:40 ` Waiman Long
[not found] ` <20190428203916.E1AA6206A3@mail.kernel.org>
2019-04-28 20:47 ` Waiman Long
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox