Skip to contentSkip to conversation

perf/core: fix a possible deadlock scenario

PATCH Superseded Cong Wang 3 participants 12 messages Jul 16, 2018 – Jul 25, 2018

Subsystems (2)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 perf/core: fix a possible deadlock scenario

hrtimer_cancel() busy-waits for the hrtimer callback to stop, pretty much like del_timer_sync(). This creates a possible deadlock scenario where we hold a spinlock before calling hrtimer_cancel() while in trying to acquire the same spinlock in the callback.

This kind of deadlock is already known and is catchable by lockdep, like for del_timer_sync(), we can add lockdep annotations. However, it is still missing for hrtimer_cancel(). (I have a WIP patch to make it complete for hrtimer_cancel() but it breaks booting.)

And there is such a deadlock scenario in kernel/events/core.c too, well actually, it is a simpler version: the hrtimer callback waits for itself to finish on the same CPU! It sounds stupid but it is not obvious at all, it hides very deeply in the perf event code:

cpu_clock_event_init():
perf_swevent_init_hrtimer():

    hwc->hrtimer.function = perf_swevent_hrtimer;

perf_swevent_hrtimer():
__perf_event_overflow():

    __perf_event_account_interrupt():
      perf_adjust_period():
        pmu->stop():
        cpu_clock_event_stop():
          perf_swevent_cancel():
            hrtimer_cancel()

Getting stuck in a timer doesn't sound very scary, however, in this case, its consequences are a disaster:

perf_event_overflow() which calls __perf_event_overflow() is called in NMI handler too, so it is racy with hrtimer callback as disabling IRQ can't possibly disable NMI. This means this hrtimer callback once interrupted by an NMI handler could deadlock within NMI!

As a further consequence, other IRQ handling is blocked too, notably the IPI handler, especially when smp_call_function_*() waits for their callbacks synchronously. This is why we saw so many soft lockup's in smp_call_function_single() given how widely they are used in kernel. Ironically, perf event code uses synchronous smp_call_function_single() heavily too.

The fix is not easy. To minimize the impact, ideally we should just avoid busy waiting when it is called within the hrtimer callback on the same CPU, there is no reason to wait for itself to finish anyway. Probably it doesn't even need to cancel itself either since it will restart by pmu->start() later. There are two possible fixes here:

1. Modify hrtimer API to detect if a hrtimer callback is running on the same CPU now. This does not look pretty though.

2. Passing some information from perf_swevent_hrtimer() down to perf_swevent_cancel().

So I pick the latter approach, it is simple and straightforward.

Note, currently perf_swevent_hrtimer() still races with perf_event_overflow() in NMI on the same CPU anyway, given there is no lock around and probably locking does not even help. But it is nothing new, and the race itself is not bad either, at most we have some inconsistent updates on the event sample period.

Fixes abd50713944c ("perf: Reimplement frequency driven sampling")
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 include/linux/perf_event.h |  3 +++
 kernel/events/core.c       | 43 +++++++++++++++++++++++++++----------------
 2 files changed, 30 insertions(+), 16 deletions(-)
include/linux/perf_event.h +3 -0
index 1fa12887ec02..aab39b8aa720 100644
@@ -310,6 +310,9 @@ struct pmu {
 #define PERF_EF_START	0x01		/* start the counter when adding    */
 #define PERF_EF_RELOAD	0x02		/* reload the counter when starting */
 #define PERF_EF_UPDATE	0x04		/* update the counter when stopping */
+#define PERF_EF_NO_WAIT	0x08		/* do not wait when stopping, for
+					 * example, waiting for a timer
+					 */
 
 	/*
 	 * Adds/Removes a counter to/from the PMU, can be done inside a
kernel/events/core.c +27 -16

133 lines not on this page · raw message

Signature · 1 line
2.14.4
#1 lore raw
Quoted 21 lines · Cong Wang

hrtimer_cancel() busy-waits for the hrtimer callback to stop, pretty much like del_timer_sync(). This creates a possible deadlock scenario where we hold a spinlock before calling hrtimer_cancel() while in trying to acquire the same spinlock in the callback.

This kind of deadlock is already known and is catchable by lockdep, like for del_timer_sync(), we can add lockdep annotations. However, it is still missing for hrtimer_cancel(). (I have a WIP patch to make it complete for hrtimer_cancel() but it breaks booting.)

And there is such a deadlock scenario in kernel/events/core.c too, well actually, it is a simpler version: the hrtimer callback waits for itself to finish on the same CPU! It sounds stupid but it is not obvious at all, it hides very deeply in the perf event code:

cpu_clock_event_init():
perf_swevent_init_hrtimer():

    hwc->hrtimer.function = perf_swevent_hrtimer;

perf_swevent_hrtimer():
__perf_event_overflow():

Quoted 8 lines
    __perf_event_account_interrupt():
      perf_adjust_period():
        pmu->stop():
        cpu_clock_event_stop():
          perf_swevent_cancel():
            hrtimer_cancel()

Getting stuck in a timer doesn't sound very scary, however, in this

sound scary enough for me ;-) were you able to hit it?

Quoted 6 lines

case, its consequences are a disaster:

perf_event_overflow() which calls __perf_event_overflow() is called in NMI handler too, so it is racy with hrtimer callback as disabling IRQ can't possibly disable NMI. This means this hrtimer callback once interrupted by an NMI handler could deadlock within NMI!

hum, the swevent pmu does not triger NMI, so that timer will never be touched in NMI context

jirka

author
#2 lore raw
Quoted 30 lines · Jiri Olsa
Quoted 28 lines · Cong Wang

hrtimer_cancel() busy-waits for the hrtimer callback to stop, pretty much like del_timer_sync(). This creates a possible deadlock scenario where we hold a spinlock before calling hrtimer_cancel() while in trying to acquire the same spinlock in the callback.

This kind of deadlock is already known and is catchable by lockdep, like for del_timer_sync(), we can add lockdep annotations. However, it is still missing for hrtimer_cancel(). (I have a WIP patch to make it complete for hrtimer_cancel() but it breaks booting.)

And there is such a deadlock scenario in kernel/events/core.c too, well actually, it is a simpler version: the hrtimer callback waits for itself to finish on the same CPU! It sounds stupid but it is not obvious at all, it hides very deeply in the perf event code:

cpu_clock_event_init():
perf_swevent_init_hrtimer():

    hwc->hrtimer.function = perf_swevent_hrtimer;

perf_swevent_hrtimer():
__perf_event_overflow():

    __perf_event_account_interrupt():
      perf_adjust_period():
        pmu->stop():
        cpu_clock_event_stop():
          perf_swevent_cancel():
            hrtimer_cancel()
Quoted 3 lines
Quoted 1 line

Getting stuck in a timer doesn't sound very scary, however, in this

sound scary enough for me ;-) were you able to hit it?

Yeah, we have seen hundreds of soft lockup's in smp function call which is probably caused by this bug. It dates back to 3.10 kernel, seriously. :) Unfortunately we don't have a reproducer, so we can't verify if this patch really fixes those soft lockup's until we deploy the fix to probably thousands of machines in data center.

Quoted 10 lines
Quoted 7 lines

case, its consequences are a disaster:

perf_event_overflow() which calls __perf_event_overflow() is called in NMI handler too, so it is racy with hrtimer callback as disabling IRQ can't possibly disable NMI. This means this hrtimer callback once interrupted by an NMI handler could deadlock within NMI!

hum, the swevent pmu does not triger NMI, so that timer will never be touched in NMI context

Good to know! So I worry too much here.

But still, given hrtimer interrupt is called with IRQ disabled, getting stuck in a hrtimer callback could also block IPI handler, therefore causing soft lockups.

Let me know if you want me to adjust the changelog for this.

Thanks!

#3 lore raw

On Wed, Jul 18, 2018 at 01:21:11PM -0700, Cong Wang wrote:

SNIP

Quoted 10 lines
Quoted 2 lines

hum, the swevent pmu does not triger NMI, so that timer will never be touched in NMI context

Good to know! So I worry too much here.

But still, given hrtimer interrupt is called with IRQ disabled, getting stuck in a hrtimer callback could also block IPI handler, therefore causing soft lockups.

Let me know if you want me to adjust the changelog for this.

yep, it's confusing, I think it should be removed from changelog

jirka

author
#4 lore raw

PATCH v2 1/1 perf/core: fix a possible deadlock scenario

hrtimer_cancel() busy-waits for the hrtimer callback to stop, pretty much like del_timer_sync(). This creates a possible deadlock scenario where we hold a spinlock before calling hrtimer_cancel() while in trying to acquire the same spinlock in the callback.

This kind of deadlock is already known and is catchable by lockdep, like for del_timer_sync(), we can add lockdep annotations. However, it is still missing for hrtimer_cancel(). (I have a WIP patch to make it complete for hrtimer_cancel() but it breaks booting.)

And there is such a deadlock scenario in kernel/events/core.c too, well actually, it is a simpler version: the hrtimer callback waits for itself to finish on the same CPU! It sounds stupid but it is not obvious at all, it hides very deeply in the perf event code:

cpu_clock_event_init():
perf_swevent_init_hrtimer():

    hwc->hrtimer.function = perf_swevent_hrtimer;

perf_swevent_hrtimer():
__perf_event_overflow():

    __perf_event_account_interrupt():
      perf_adjust_period():
        pmu->stop():
        cpu_clock_event_stop():
          perf_swevent_cancel():
            hrtimer_cancel()

Getting stuck in an hrtimer is a disaster:

Interrupts are disabled for hrtimer_interrupt(), this means other interrupt handling is blocked too, notably the IPI handler, especially when smp_call_function_*() waits for their callbacks synchronously. This is probably why we saw hundreds of soft lockup's inside smp_call_function_single(), given how widely they are used in kernel. Ironically, perf event code uses synchronous smp_call_function_single() heavily too.

The fix is not easy. To minimize the impact, ideally we should just avoid busy waiting when it is called within the hrtimer callback on the same CPU, there is no reason to wait for itself to finish anyway. Probably it doesn't even need to cancel itself either since it will restart by pmu->start() later. There are two possible fixes here:

1. Modify hrtimer API to detect if a hrtimer callback is running on the same CPU now. It does not look pretty to adjust it only for this special case though.

2. Passing some information from perf_swevent_hrtimer() down to perf_swevent_cancel().

So I pick the latter approach, it is nicer and straightforward.

Note, Jiri pointed out that the swevent pmu does not triger NMI and this timer will never be touched in NMI context, so we don't race with NMI handler here.

Fixes abd50713944c ("perf: Reimplement frequency driven sampling")
Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
---
 include/linux/perf_event.h |  3 +++
 kernel/events/core.c       | 43 +++++++++++++++++++++++++++----------------
 2 files changed, 30 insertions(+), 16 deletions(-)
include/linux/perf_event.h +3 -0
index 1fa12887ec02..aab39b8aa720 100644
@@ -310,6 +310,9 @@ struct pmu {
 #define PERF_EF_START	0x01		/* start the counter when adding    */
 #define PERF_EF_RELOAD	0x02		/* reload the counter when starting */
 #define PERF_EF_UPDATE	0x04		/* update the counter when stopping */
+#define PERF_EF_NO_WAIT	0x08		/* do not wait when stopping, for
+					 * example, waiting for a timer
+					 */
 
 	/*
 	 * Adds/Removes a counter to/from the PMU, can be done inside a
kernel/events/core.c +27 -16

133 lines not on this page · raw message

Signature · 1 line
2.14.4
maintainer
#5 lore raw
Quoted 4 lines · Cong Wang

hrtimer_cancel() busy-waits for the hrtimer callback to stop, pretty much like del_timer_sync(). This creates a possible deadlock scenario where we hold a spinlock before calling hrtimer_cancel() while in trying to acquire the same spinlock in the callback.

Has this actually been observed?

Quoted 4 lines

cpu_clock_event_init():
perf_swevent_init_hrtimer():

    hwc->hrtimer.function = perf_swevent_hrtimer;
Quoted 8 lines

perf_swevent_hrtimer():
__perf_event_overflow():

    __perf_event_account_interrupt():
      perf_adjust_period():
        pmu->stop():
        cpu_clock_event_stop():
          perf_swevent_cancel():
            hrtimer_cancel()

Please explain how a hrtimer event ever gets to perf_adjust_period(). Last I checked perf_swevent_init_hrtimer() results in attr.freq=0.

Quoted 1 line

Getting stuck in an hrtimer is a disaster:

You'll get NMI watchdog splats. Getting stuck in NMI context is far more 'interesting :-)

include/linux/perf_event.h struct pmu { · line 315

Quoted 3 lines
include/linux/perf_event.h +3 -0
+#define PERF_EF_NO_WAIT	0x08		/* do not wait when stopping, for
+					 * example, waiting for a timer
+					 */

That's a broken comment style.

author
#6 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 4 lines · Cong Wang

hrtimer_cancel() busy-waits for the hrtimer callback to stop, pretty much like del_timer_sync(). This creates a possible deadlock scenario where we hold a spinlock before calling hrtimer_cancel() while in trying to acquire the same spinlock in the callback.

Has this actually been observed?

Without lockdep annotation, it is not easy to observe.

Quoted 14 lines
Quoted 5 lines

cpu_clock_event_init():
perf_swevent_init_hrtimer():

    hwc->hrtimer.function = perf_swevent_hrtimer;
Quoted 8 lines

perf_swevent_hrtimer():
__perf_event_overflow():

    __perf_event_account_interrupt():
      perf_adjust_period():
        pmu->stop():
        cpu_clock_event_stop():
          perf_swevent_cancel():
            hrtimer_cancel()
Quoted 2 lines

Please explain how a hrtimer event ever gets to perf_adjust_period(). Last I checked perf_swevent_init_hrtimer() results in attr.freq=0.

Good point.

I thought attr.freq is specified by user-space, but seems perf_swevent_init_hrtimer() clears it purposely and will not change after initialization, interesting...

Quoted 5 lines
Quoted 2 lines

Getting stuck in an hrtimer is a disaster:

You'll get NMI watchdog splats. Getting stuck in NMI context is far more 'interesting :-)

Yes, I did see a stack trace in perf_swevent_hrtimer() which led me here. But I have to admit among those hundreds of soft lockup's, I only saw one showing swevent hrtimer backtrace.

Previously I thought this is because of NMI handler race, but Jiri pointed out the race doesn't exist.

Quoted 6 lines

include/linux/perf_event.h struct pmu { · line 315

Quoted 4 lines
include/linux/perf_event.h +3 -0
+#define PERF_EF_NO_WAIT      0x08            /* do not wait when stopping, for
+                                      * example, waiting for a timer
+                                      */

That's a broken comment style.

It is picked by checkpatch.pl, not me, I chose a different one and got a complain. :)

Thanks!

author
#7 lore raw

Hi, Peter, Andi

While reviewing the deadlock, I find out it looks like we could have the following infinite recursion too:

perf_event_account_interrupt()
__perf_event_account_interrupt()
perf_adjust_period()
event->pmu->stop
x86_pmu_stop()
x86_pmu.disable()
intel_pmu_disable_event()
intel_pmu_pebs_disable()
intel_pmu_drain_pebs_buffer()
intel_pmu_drain_pebs_nhm()
<repeat....>

This time is pure hardware events, attr.freq must be non-zero.

And, we could enter this infinite recursion in NMI handler too:

intel_pmu_handle_irq()
perf_event_overflow()
__perf_event_overflow()
__perf_event_account_interrupt()
....

Or this is impossible too?

Thanks!

author
#8 lore raw
Quoted 6 lines · Cong Wang

Hi, Peter, Andi

While reviewing the deadlock, I find out it looks like we could have the following infinite recursion too:

Quoted 6 lines

perf_event_account_interrupt()
__perf_event_account_interrupt()
perf_adjust_period()
event->pmu->stop
x86_pmu_stop()
x86_pmu.disable()

Hmm, x86_pmu_stop() calls __test_and_clear_bit(), so we should not call x86_pmu.disable() twice here.

Quoted 19 lines

intel_pmu_disable_event()
intel_pmu_pebs_disable()
intel_pmu_drain_pebs_buffer()
intel_pmu_drain_pebs_nhm()
<repeat....>

This time is pure hardware events, attr.freq must be non-zero.

And, we could enter this infinite recursion in NMI handler too:

intel_pmu_handle_irq()
perf_event_overflow()
__perf_event_overflow()
__perf_event_account_interrupt()
....

Or this is impossible too?

Thanks!

maintainer
#9 lore raw
Quoted 14 lines · Cong Wang
Quoted 6 lines · Cong Wang

Hi, Peter, Andi

While reviewing the deadlock, I find out it looks like we could have the following infinite recursion too:

Quoted 6 lines

perf_event_account_interrupt()
__perf_event_account_interrupt()
perf_adjust_period()
event->pmu->stop
x86_pmu_stop()
x86_pmu.disable()

Quoted 2 lines

Hmm, x86_pmu_stop() calls __test_and_clear_bit(), so we should not call x86_pmu.disable() twice here.

Right, but since we set HES_UPTODATE after calling x86_perf_event_update() that can in fact recurse.

Now, I don't think that'll be fatal, but it might be good to test that.

If you pick these patches:

https://lkml.kernel.org/r/[email protected]

use force_early_printk (and actually configure a serial early_printk) and put a printk() in x86_pmu_stop() and then run the perf_fuzzer or something to try and reproduce.

But paranoia suggets moving that HES_UPTODATE thing one line up.

Quoted 10 lines
Quoted 10 lines

intel_pmu_disable_event()
intel_pmu_pebs_disable()
intel_pmu_drain_pebs_buffer()
intel_pmu_drain_pebs_nhm()
<repeat....>

This time is pure hardware events, attr.freq must be non-zero.

And, we could enter this infinite recursion in NMI handler too:

Quoted 7 lines
Quoted 7 lines

intel_pmu_handle_irq()
perf_event_overflow()
__perf_event_overflow()
__perf_event_account_interrupt()
....

Or this is impossible too?

I'm not sure I see this second one.. can you be a little more specific?

author
#10 lore raw
Quoted 16 lines · Peter Zijlstra
Quoted 14 lines · Cong Wang
Quoted 12 lines · Cong Wang

Hi, Peter, Andi

While reviewing the deadlock, I find out it looks like we could have the following infinite recursion too:

perf_event_account_interrupt()
__perf_event_account_interrupt()
perf_adjust_period()
event->pmu->stop
x86_pmu_stop()
x86_pmu.disable()

Quoted 5 lines
Quoted 2 lines

Hmm, x86_pmu_stop() calls __test_and_clear_bit(), so we should not call x86_pmu.disable() twice here.

Right, but since we set HES_UPTODATE after calling x86_perf_event_update() that can in fact recurse.

I don't see how HES_UPTODATE flag or x86_perf_event_update() could affect the path on this call chain.

Quoted 10 lines

Now, I don't think that'll be fatal, but it might be good to test that.

If you pick these patches:

https://lkml.kernel.org/r/[email protected]

use force_early_printk (and actually configure a serial early_printk) and put a printk() in x86_pmu_stop() and then run the perf_fuzzer or something to try and reproduce.

Is this patchset to make printk() working in NMI context?
But printk() is already used in NMI context, see perf_event_print_debug() which is called in intel_pmu_handle_irq().

Quoted 19 lines

But paranoia suggets moving that HES_UPTODATE thing one line up.

Quoted 16 lines
Quoted 16 lines

intel_pmu_disable_event()
intel_pmu_pebs_disable()
intel_pmu_drain_pebs_buffer()
intel_pmu_drain_pebs_nhm()
<repeat....>

This time is pure hardware events, attr.freq must be non-zero.

And, we could enter this infinite recursion in NMI handler too:

intel_pmu_handle_irq()
perf_event_overflow()
__perf_event_overflow()
__perf_event_account_interrupt()
....

Quoted 3 lines
Quoted 1 line
Quoted 1 line

Or this is impossible too?

I'm not sure I see this second one.. can you be a little more specific?

In fact, it is this:

intel_pmu_handle_irq()
x86_pmu.drain_pebs()
intel_pmu_drain_pebs_nhm()
perf_event_account_interrupt()
__perf_event_account_interrupt()
perf_adjust_period()
event->pmu->stop()
x86_pmu_stop()
x86_pmu.disable()
intel_pmu_disable_event()
intel_pmu_pebs_disable()
intel_pmu_drain_pebs_buffer()
<repeat....>

Thanks!

maintainer
#11 lore raw
In reply to Cong Wang
Quoted 8 lines · Cong Wang
Quoted 5 lines

include/linux/perf_event.h struct pmu { · line 315

Quoted 3 lines
include/linux/perf_event.h +3 -0
+#define PERF_EF_NO_WAIT      0x08            /* do not wait when stopping, for
+                                      * example, waiting for a timer
+                                      */

That's a broken comment style.

It is picked by checkpatch.pl, not me, I chose a different one and got a complain. :)

Ignore checkpatch when it's wrong ;-)