Skip to contentSkip to conversation

xen/timers: Fix memory leak with cpu unplug/plug

PATCH In review Andrew Cooper 3 participants 9 messages Apr 8, 2019

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/timers: Fix memory leak with cpu unplug/plug

timer_softirq_action() realloc's itself a larger timer heap whenever necessary, which includes bootstrapping from the empty dummy_heap. Nothing ever freed this allocation.

CPU plug and unplug has the side effect of zeroing the percpu data area, which clears ts->heap. This in turn causes new timers to be put on the list rather than the heap, and for timer_softirq_action() to bootstrap itself again.

This in practice leaks ts->heap every time a CPU is unplugged and replugged.

Implement free_percpu_timers() which includes freeing ts->heap when appropriate, and update the notifier callback with the recent cpu parking logic and free-avoidance across suspend.

---
CC: George Dunlap <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>

v2:
* Rebase over Juergen's free-avoidance series.

This texturally depends on "xen/timers: Document and improve the representation of the timer heap metadata" which was necessary to understand the problem well enough to fix it, but isn't backporting over this change isn't too complicated (should the cleanup patch not want to be backported).

---
 xen/common/timer.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
xen/common/timer.c +26 -1
index 98f2c48..f265a36 100644
@@ -615,6 +615,22 @@ static void migrate_timers_from_cpu(unsigned int old_cpu)
  */
 static struct timer *dummy_heap[1];
 
+static void free_percpu_timers(unsigned int cpu)
+{
+    struct timers *ts = &per_cpu(timers, cpu);
+
+    migrate_timers_from_cpu(cpu);
+
+    ASSERT(heap_metadata(ts->heap)->size == 0);
+    if ( heap_metadata(ts->heap)->limit )
+    {
+        xfree(ts->heap);
+        ts->heap = dummy_heap;
+    }
+    else
+        ASSERT(ts->heap == dummy_heap);
+}
+
 static int cpu_callback(
     struct notifier_block *nfb, unsigned long action, void *hcpu)
 {
@@ -628,10 +644,19 @@ static int cpu_callback(
         spin_lock_init(&ts->lock);
         ts->heap = dummy_heap;
         break;
+
     case CPU_UP_CANCELED:
     case CPU_DEAD:
-        migrate_timers_from_cpu(cpu);
+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )
+            free_percpu_timers(cpu);
         break;
+
+    case CPU_REMOVE:
+        if ( park_offline_cpus )
+            free_percpu_timers(cpu);
+        break;
+
     default:
         break;
     }
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line · On 08.04.19 at 11:39, <[email protected]> wrote:

timer_softirq_action() realloc's itself a larger timer heap whenever necessary, which includes bootstrapping from the empty dummy_heap. Nothing ever freed this allocation.

CPU plug and unplug has the side effect of zeroing the percpu data area, which clears ts->heap. This in turn causes new timers to be put on the list rather than the heap, and for timer_softirq_action() to bootstrap itself again.

Quoted 7 lines

This in practice leaks ts->heap every time a CPU is unplugged and replugged.

Implement free_percpu_timers() which includes freeing ts->heap when appropriate, and update the notifier callback with the recent cpu parking logic and free-avoidance across suspend.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
In reply to Andrew Cooper

Hi,

xen/common/timer.c static int cpu_callback( · line 651

Quoted 2 lines · Andrew Cooper
xen/common/timer.c +2 -0
+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )

This patch breaks compilation on arm32/arm64 because park_offline_cpus is not defined:

timer.c: In function 'cpu_callback':
timer.c:651:15: error: 'park_offline_cpus' undeclared (first use in this function)

         if ( !park_offline_cpus && system_state != SYS_STATE_suspend )
               ^~~~~~~~~~~~~~~~~

What is the purpose of park_offline_cpus?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw
Quoted 9 lines · Julien Grall

Hi,

Quoted 2 lines · Andrew Cooper

+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )

This patch breaks compilation on arm32/arm64 because park_offline_cpus is not defined:

Quoted 7 lines

timer.c: In function 'cpu_callback':
timer.c:651:15: error: 'park_offline_cpus' undeclared (first use in this function)
         if ( !park_offline_cpus && system_state != SYS_STATE_suspend ) ^~~~~~~~~~~~~~~~~

What is the purpose of park_offline_cpus?

Sorry.  I should have waited for a full build test first.

park_offline_cpus is a workaround for Intel's MCE behaviour, where the system will shut down rather than deliver an #MC if machine checking isn't configured on all CPUs.

As a result, we have to start all CPUs, even beyond maxcpus= and set up machine check handling, and never ever free their stacks, even if we'd prefer the CPUs to be offline.

Are you happy with a

#define park_offline_cpus false

in ARM?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Hi,

Quoted 20 lines · Andrew Cooper
Quoted 9 lines · Julien Grall

Hi,

Quoted 2 lines · Andrew Cooper

+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )

This patch breaks compilation on arm32/arm64 because park_offline_cpus is not defined:

Quoted 7 lines

timer.c: In function 'cpu_callback':
timer.c:651:15: error: 'park_offline_cpus' undeclared (first use in this function)
         if ( !park_offline_cpus && system_state != SYS_STATE_suspend ) ^~~~~~~~~~~~~~~~~

What is the purpose of park_offline_cpus?

Sorry.  I should have waited for a full build test first.

Quoted 7 lines

park_offline_cpus is a workaround for Intel's MCE behaviour, where the system will shut down rather than deliver an #MC if machine checking isn't configured on all CPUs.

As a result, we have to start all CPUs, even beyond maxcpus= and set up machine check handling, and never ever free their stacks, even if we'd prefer the CPUs to be offline.

I am a bit confused, why this is necessary now for the timer and not in other places of the common code?

Quoted 5 lines

Are you happy with a

#define park_offline_cpus false >
in ARM?

The name is fairly confusing if you don't know the background.

But I have to admit that even with your explanation above, I still don't understand why you need to check park_offline_cpus in the timers.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw
Quoted 39 lines · Julien Grall

Hi,

Quoted 22 lines · Andrew Cooper
Quoted 10 lines · Julien Grall

Hi,

Quoted 3 lines · Andrew Cooper

+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )

This patch breaks compilation on arm32/arm64 because park_offline_cpus is not defined:

Quoted 8 lines

timer.c: In function 'cpu_callback':
timer.c:651:15: error: 'park_offline_cpus' undeclared (first use in this function)
          if ( !park_offline_cpus && system_state != SYS_STATE_suspend )
                ^~~~~~~~~~~~~~~~~

What is the purpose of park_offline_cpus?

Sorry.  I should have waited for a full build test first.

Quoted 7 lines

park_offline_cpus is a workaround for Intel's MCE behaviour, where the system will shut down rather than deliver an #MC if machine checking isn't configured on all CPUs.

As a result, we have to start all CPUs, even beyond maxcpus= and set up machine check handling, and never ever free their stacks, even if we'd prefer the CPUs to be offline.

I am a bit confused, why this is necessary now for the timer and not in other places of the common code?

Quoted 3 lines

Are you happy with a

Quoted 7 lines
Quoted 2 lines

#define park_offline_cpus false >
in ARM?

The name is fairly confusing if you don't know the background.

But I have to admit that even with your explanation above, I still don't understand why you need to check park_offline_cpus in the timers.

It is all to do with how/when we free per-cpu data.

Technically speaking (with the memory leak fixed) the old arrangement ought to function correctly, but the new arrangement is more efficient.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

Hi Andrew,

Quoted 45 lines · Andrew Cooper
Quoted 44 lines · Julien Grall

Hi,

Quoted 22 lines · Andrew Cooper
Quoted 10 lines · Julien Grall

Hi,

Quoted 3 lines · Andrew Cooper

+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )

This patch breaks compilation on arm32/arm64 because park_offline_cpus is not defined:

Quoted 8 lines

timer.c: In function 'cpu_callback':
timer.c:651:15: error: 'park_offline_cpus' undeclared (first use in this function)
          if ( !park_offline_cpus && system_state !=
SYS_STATE_suspend )
                ^~~~~~~~~~~~~~~~~

What is the purpose of park_offline_cpus?

Sorry.  I should have waited for a full build test first.

Quoted 7 lines

park_offline_cpus is a workaround for Intel's MCE behaviour, where the system will shut down rather than deliver an #MC if machine checking isn't configured on all CPUs.

As a result, we have to start all CPUs, even beyond maxcpus= and set up machine check handling, and never ever free their stacks, even if we'd prefer the CPUs to be offline.

I am a bit confused, why this is necessary now for the timer and not in other places of the common code?

Quoted 5 lines

Are you happy with a

#define park_offline_cpus false >
in ARM?

The name is fairly confusing if you don't know the background.

Quoted 7 lines
Quoted 2 lines

But I have to admit that even with your explanation above, I still don't understand why you need to check park_offline_cpus in the timers.

It is all to do with how/when we free per-cpu data.

Technically speaking (with the memory leak fixed) the old arrangement ought to function correctly, but the new arrangement is more efficient.

Where would the free happen in the "less efficient" way?

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw
Quoted 50 lines · Julien Grall

Hi Andrew,

Quoted 47 lines · Andrew Cooper
Quoted 46 lines · Julien Grall

Hi,

Quoted 23 lines · Andrew Cooper
Quoted 11 lines · Julien Grall

Hi,

Quoted 3 lines · Andrew Cooper

+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )

This patch breaks compilation on arm32/arm64 because park_offline_cpus
is not defined:

Quoted 8 lines

timer.c: In function 'cpu_callback':
timer.c:651:15: error: 'park_offline_cpus' undeclared (first use in this function)
           if ( !park_offline_cpus && system_state != SYS_STATE_suspend )
                 ^~~~~~~~~~~~~~~~~

What is the purpose of park_offline_cpus?

Sorry.  I should have waited for a full build test first.

Quoted 8 lines

park_offline_cpus is a workaround for Intel's MCE behaviour, where the system will shut down rather than deliver an #MC if machine checking isn't configured on all CPUs.

As a result, we have to start all CPUs, even beyond maxcpus= and set up
machine check handling, and never ever free their stacks, even if we'd prefer the CPUs to be offline.

I am a bit confused, why this is necessary now for the timer and not in other places of the common code?

Quoted 5 lines

Are you happy with a

#define park_offline_cpus false >
in ARM?

The name is fairly confusing if you don't know the background.

Quoted 8 lines
Quoted 7 lines
Quoted 2 lines

But I have to admit that even with your explanation above, I still don't understand why you need to check park_offline_cpus in the timers.

It is all to do with how/when we free per-cpu data.

Technically speaking (with the memory leak fixed) the old arrangement ought to function correctly, but the new arrangement is more efficient.

Where would the free happen in the "less efficient" way?

I don't quite understand the question.

https://lists.xenproject.org/archives/html/xen-devel/2019-03/msg02252.html
is the v1 patch, but that has already been rejected for not using the up-to-date notifier layout.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Hi,

Quoted 54 lines · Andrew Cooper
Quoted 53 lines · Julien Grall

Hi Andrew,

Quoted 50 lines · Andrew Cooper
Quoted 48 lines · Julien Grall

Hi,

Quoted 23 lines · Andrew Cooper
Quoted 11 lines · Julien Grall

Hi,

Quoted 3 lines · Andrew Cooper

+    case CPU_RESUME_FAILED:
+        if ( !park_offline_cpus && system_state != SYS_STATE_suspend )

This patch breaks compilation on arm32/arm64 because park_offline_cpus
is not defined:

Quoted 8 lines

timer.c: In function 'cpu_callback':
timer.c:651:15: error: 'park_offline_cpus' undeclared (first use in this function)
           if ( !park_offline_cpus && system_state !=
SYS_STATE_suspend )
                 ^~~~~~~~~~~~~~~~~

What is the purpose of park_offline_cpus?

Sorry.  I should have waited for a full build test first.

Quoted 8 lines

park_offline_cpus is a workaround for Intel's MCE behaviour, where the system will shut down rather than deliver an #MC if machine checking isn't configured on all CPUs.

As a result, we have to start all CPUs, even beyond maxcpus= and set up
machine check handling, and never ever free their stacks, even if we'd prefer the CPUs to be offline.

I am a bit confused, why this is necessary now for the timer and not in other places of the common code?

Quoted 5 lines

Are you happy with a

#define park_offline_cpus false >
in ARM?

The name is fairly confusing if you don't know the background.

But I have to admit that even with your explanation above, I still don't understand why you need to check park_offline_cpus in the timers.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

It is all to do with how/when we free per-cpu data.

Technically speaking (with the memory leak fixed) the old arrangement ought to function correctly, but the new arrangement is more efficient.

Where would the free happen in the "less efficient" way?

I don't quite understand the question.

You mention that this new arrangement is "more efficient". So I was asking what was the previous solution.

Quoted 4 lines

https://lists.xenproject.org/archives/html/xen-devel/2019-03/msg02252.html
is the v1 patch, but that has already been rejected for not using the up-to-date notifier layout.

At the expense of introducing an undocumented x86-ism in common code. I would have nacked such patch if I had time too (only hour between post and commit). For common code, could I request a bit more time to allow other of the maintainers give an opinion if they have?

I don't have a better name for park_offline_cpus, but we at least need more documentation for someone to understand the purpose of it without looking at x86. Then, I would be able to understand what is the correct fix for Arm.

For time being, I would just revert this patch.

Cheers,

Signature · 6 lines
Julien Grall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel