* [Patch] perf_event: fix a race condition in perf_remove_from_context()
@ 2014-08-28 23:27 Cong Wang
2014-09-01 8:38 ` Peter Zijlstra
0 siblings, 1 reply; 3+ messages in thread
From: Cong Wang @ 2014-08-28 23:27 UTC (permalink / raw)
To: linux-kernel
Cc: stable, Peter Zijlstra, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo, Cong Wang, Cong Wang
From: Cong Wang <cwang@twopensource.com>
We saw a kernel soft lockup in perf_remove_from_context(),
it looks like the `perf` process, when exiting, could not go
out of the retry loop. Meanwhile, the target process was forking
a child. So either the target process should execute the smp
function call to deactive the event (if it was running) or it should
do a context switch which deactives the event.
It seems we optimize out a context switch in perf_event_context_sched_out(),
and what's more important, we still test an obsolete task pointer when
retrying, so no one actually would deactive that event in this situation.
Fix it directly by reloading the task pointer in perf_remove_from_context().
This should fix the above soft lockup.
Cc: stable@vger.kernel.org
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Signed-off-by: Cong Wang <cwang@twopensource.com>
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com>
---
diff --git a/kernel/events/core.c b/kernel/events/core.c
index f9c1ed0..c4141a0 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -1524,6 +1524,11 @@ retry:
*/
if (ctx->is_active) {
raw_spin_unlock_irq(&ctx->lock);
+ /*
+ * Reload the task pointer, it might have been changed by
+ * a concurrent perf_event_context_sched_out() without switching
+ */
+ task = ctx->task;
goto retry;
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Patch] perf_event: fix a race condition in perf_remove_from_context()
2014-08-28 23:27 [Patch] perf_event: fix a race condition in perf_remove_from_context() Cong Wang
@ 2014-09-01 8:38 ` Peter Zijlstra
2014-09-02 22:15 ` Cong Wang
0 siblings, 1 reply; 3+ messages in thread
From: Peter Zijlstra @ 2014-09-01 8:38 UTC (permalink / raw)
To: Cong Wang
Cc: linux-kernel, stable, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo, Cong Wang
On Thu, Aug 28, 2014 at 04:27:35PM -0700, Cong Wang wrote:
> From: Cong Wang <cwang@twopensource.com>
>
> We saw a kernel soft lockup in perf_remove_from_context(),
> it looks like the `perf` process, when exiting, could not go
> out of the retry loop. Meanwhile, the target process was forking
> a child. So either the target process should execute the smp
> function call to deactive the event (if it was running) or it should
> do a context switch which deactives the event.
>
> It seems we optimize out a context switch in perf_event_context_sched_out(),
> and what's more important, we still test an obsolete task pointer when
> retrying, so no one actually would deactive that event in this situation.
> Fix it directly by reloading the task pointer in perf_remove_from_context().
> This should fix the above soft lockup.
> ---
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index f9c1ed0..c4141a0 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -1524,6 +1524,11 @@ retry:
Please use either:
.gitconfig:
[diff "default"]
xfuncname = "^[[:alpha:]$_].*[^:]$"
.quiltrc:
QUILT_DIFF_OPTS="-F ^[[:alpha:]\$_].*[^:]\$"
> */
> if (ctx->is_active) {
> raw_spin_unlock_irq(&ctx->lock);
> + /*
> + * Reload the task pointer, it might have been changed by
> + * a concurrent perf_event_context_sched_out() without switching
> + */
> + task = ctx->task;
> goto retry;
> }
You forgot to check if that same error happened in other places (it
does), please fix all of them.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Patch] perf_event: fix a race condition in perf_remove_from_context()
2014-09-01 8:38 ` Peter Zijlstra
@ 2014-09-02 22:15 ` Cong Wang
0 siblings, 0 replies; 3+ messages in thread
From: Cong Wang @ 2014-09-02 22:15 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Cong Wang, linux-kernel, stable, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo
On Mon, Sep 1, 2014 at 1:38 AM, Peter Zijlstra <peterz@infradead.org> wrote:
> On Thu, Aug 28, 2014 at 04:27:35PM -0700, Cong Wang wrote:
>> From: Cong Wang <cwang@twopensource.com>
>>
>> We saw a kernel soft lockup in perf_remove_from_context(),
>> it looks like the `perf` process, when exiting, could not go
>> out of the retry loop. Meanwhile, the target process was forking
>> a child. So either the target process should execute the smp
>> function call to deactive the event (if it was running) or it should
>> do a context switch which deactives the event.
>>
>> It seems we optimize out a context switch in perf_event_context_sched_out(),
>> and what's more important, we still test an obsolete task pointer when
>> retrying, so no one actually would deactive that event in this situation.
>> Fix it directly by reloading the task pointer in perf_remove_from_context().
>> This should fix the above soft lockup.
>
>
>
>> ---
>> diff --git a/kernel/events/core.c b/kernel/events/core.c
>> index f9c1ed0..c4141a0 100644
>> --- a/kernel/events/core.c
>> +++ b/kernel/events/core.c
>> @@ -1524,6 +1524,11 @@ retry:
>
> Please use either:
>
> .gitconfig:
>
> [diff "default"]
> xfuncname = "^[[:alpha:]$_].*[^:]$"
>
> .quiltrc:
>
> QUILT_DIFF_OPTS="-F ^[[:alpha:]\$_].*[^:]\$"
>
OK, I didn't know this before.
>> */
>> if (ctx->is_active) {
>> raw_spin_unlock_irq(&ctx->lock);
>> + /*
>> + * Reload the task pointer, it might have been changed by
>> + * a concurrent perf_event_context_sched_out() without switching
>> + */
>> + task = ctx->task;
>> goto retry;
>> }
>
> You forgot to check if that same error happened in other places (it
> does), please fix all of them.
I think you mean perf_install_in_context()? I only saw the soft lockup in
perf_remove_from_context() so far, but I can fix other places if you want.
Thanks!
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-09-02 22:15 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-28 23:27 [Patch] perf_event: fix a race condition in perf_remove_from_context() Cong Wang
2014-09-01 8:38 ` Peter Zijlstra
2014-09-02 22:15 ` Cong Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome