Skip to contentSkip to conversation

xen/x86: Fix memory leak in vcpu_create() error path

PATCH In review Andrew Cooper 2 participants 6 messages Sep 28, 2020 – Dec 21, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/x86: Fix memory leak in vcpu_create() error path

Various paths in vcpu_create() end up calling paging_update_paging_modes(), which eventually allocate a monitor pagetable if one doesn't exist.

However, an error in vcpu_create() results in the vcpu being cleaned up locally, and not put onto the domain's vcpu list. Therefore, the monitor table is not freed by {hap,shadow}_teardown()'s loop. This is caught by assertions later that we've successfully freed the entire hap/shadow memory pool.

The per-vcpu loops in domain teardown logic is conceptually wrong, but exist due to insufficient existing structure in the existing logic.

Break paging_vcpu_teardown() out of paging_teardown(), with mirrored breakouts in the hap/shadow code, and use it from arch_vcpu_create()'s error path. This fixes the memory leak.

The new {hap,shadow}_vcpu_teardown() must be idempotent, and are written to be as tolerable as possible, with the minimum number of safety checks possible. In particular, drop the mfn_valid() check - if junk is in these fields, then Xen is going to explode anyway.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Wei Liu <[email protected]>
CC: George Dunlap <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Michał Leszczyński <[email protected]>

This is a minimal patch which ought to be safe to backport. The whole cleanup infrastructure is a mess.

---
 xen/arch/x86/domain.c           |  1 +
 xen/arch/x86/mm/hap/hap.c       | 39 ++++++++++++++++++-------------
 xen/arch/x86/mm/paging.c        |  8 +++++++
 xen/arch/x86/mm/shadow/common.c | 52 ++++++++++++++++++++++++-----------------
 xen/include/asm-x86/hap.h       |  1 +
 xen/include/asm-x86/paging.h    |  3 ++-
 xen/include/asm-x86/shadow.h    |  3 ++-
 7 files changed, 67 insertions(+), 40 deletions(-)
xen/arch/x86/domain.c +1 -0
index e8e91cf080..b8f5b1f5b4 100644
@@ -603,6 +603,7 @@ int arch_vcpu_create(struct vcpu *v)
     return rc;
 
  fail:
+    paging_vcpu_teardown(v);
     vcpu_destroy_fpu(v);
     xfree(v->arch.msrs);
     v->arch.msrs = NULL;
xen/arch/x86/mm/hap/hap.c +23 -16
index 4eedd1a995..737821a166 100644
@@ -563,30 +563,37 @@ void hap_final_teardown(struct domain *d)
     paging_unlock(d);
 }
 
+void hap_vcpu_teardown(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    mfn_t mfn;
+
+    paging_lock(d);
+
+    if ( !paging_mode_hap(d) || !v->arch.paging.mode )
+        goto out;
+
+    mfn = pagetable_get_mfn(v->arch.hvm.monitor_table);
+    if ( mfn_x(mfn) )
+        hap_destroy_monitor_table(v, mfn);
+    v->arch.hvm.monitor_table = pagetable_null();
+
+ out:
+    paging_unlock(d);
+}
+
 void hap_teardown(struct domain *d, bool *preempted)
 {
     struct vcpu *v;
-    mfn_t mfn;
 
     ASSERT(d->is_dying);
     ASSERT(d != current->domain);
 
-    paging_lock(d); /* Keep various asserts happy */
+    /* TODO - Remove when the teardown path is better structured. */
+    for_each_vcpu ( d, v )
+        hap_vcpu_teardown(v);
 
-    if ( paging_mode_enabled(d) )
-    {
-        /* release the monitor table held by each vcpu */
-        for_each_vcpu ( d, v )
-        {
-            if ( paging_get_hostmode(v) && paging_mode_external(d) )
-            {
-                mfn = pagetable_get_mfn(v->arch.hvm.monitor_table);
-                if ( mfn_valid(mfn) && (mfn_x(mfn) != 0) )
-                    hap_destroy_monitor_table(v, mfn);
-                v->arch.hvm.monitor_table = pagetable_null();
-            }
-        }
-    }
+    paging_lock(d); /* Keep various asserts happy */
 
     if ( d->arch.paging.hap.total_pages != 0 )
     {
xen/arch/x86/mm/paging.c +8 -0
index 695372783d..d5e967fcd5 100644
@@ -794,6 +794,14 @@ long paging_domctl_continuation(XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
 }
 #endif /* CONFIG_PV_SHIM_EXCLUSIVE */
 
+void paging_vcpu_teardown(struct vcpu *v)
+{
+    if ( hap_enabled(v->domain) )
+        hap_vcpu_teardown(v);
+    else
+        shadow_vcpu_teardown(v);
+}
+
 /* Call when destroying a domain */
 int paging_teardown(struct domain *d)
 {
xen/arch/x86/mm/shadow/common.c +30 -22

65 lines not on this page · raw message

xen/include/asm-x86/hap.h +1 -0
index d489df3812..90dece29de 100644
@@ -36,6 +36,7 @@ int   hap_domctl(struct domain *d, struct xen_domctl_shadow_op *sc,
                  XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl);
 int   hap_enable(struct domain *d, u32 mode);
 void  hap_final_teardown(struct domain *d);
+void  hap_vcpu_teardown(struct vcpu *v);
 void  hap_teardown(struct domain *d, bool *preempted);
 void  hap_vcpu_init(struct vcpu *v);
 int   hap_track_dirty_vram(struct domain *d,
xen/include/asm-x86/paging.h +2 -1
index b803efa7b5..6b8ed80c64 100644
@@ -234,7 +234,8 @@ int paging_domctl(struct domain *d, struct xen_domctl_shadow_op *sc,
 /* Helper hypercall for dealing with continuations. */
 long paging_domctl_continuation(XEN_GUEST_HANDLE_PARAM(xen_domctl_t));
 
-/* Call when destroying a domain */
+/* Call when destroying a vcpu/domain */
+void paging_vcpu_teardown(struct vcpu *v);
 int paging_teardown(struct domain *d);
 
 /* Call once all of the references to the domain have gone away */
xen/include/asm-x86/shadow.h +2 -1
index 76e47f257f..29a86ed78e 100644
@@ -74,7 +74,8 @@ int shadow_domctl(struct domain *d,
                   struct xen_domctl_shadow_op *sc,
                   XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl);
 
-/* Call when destroying a domain */
+/* Call when destroying a vcpu/domain */
+void shadow_vcpu_teardown(struct vcpu *v);
 void shadow_teardown(struct domain *d, bool *preempted);
 
 /* Call once all of the references to the domain have gone away */
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Andrew Cooper

Various paths in vcpu_create() end up calling paging_update_paging_modes(), which eventually allocate a monitor pagetable if one doesn't exist.

However, an error in vcpu_create() results in the vcpu being cleaned up locally, and not put onto the domain's vcpu list. Therefore, the monitor table is not freed by {hap,shadow}_teardown()'s loop. This is caught by assertions later that we've successfully freed the entire hap/shadow memory pool.

The per-vcpu loops in domain teardown logic is conceptually wrong, but exist due to insufficient existing structure in the existing logic.

Break paging_vcpu_teardown() out of paging_teardown(), with mirrored breakouts in the hap/shadow code, and use it from arch_vcpu_create()'s error path. This fixes the memory leak.

Quoted 7 lines

The new {hap,shadow}_vcpu_teardown() must be idempotent, and are written to be as tolerable as possible, with the minimum number of safety checks possible. In particular, drop the mfn_valid() check - if junk is in these fields, then Xen is going to explode anyway.

yet I've got a couple of simple questions:

Quoted 5 lines
xen/arch/x86/mm/hap/hap.c +0 -0
@@ -563,30 +563,37 @@ void hap_final_teardown(struct domain *d)
     paging_unlock(d);
 }

xen/arch/x86/mm/hap/hap.c void hap_final_teardown(struct domain *d) · line 574

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +9 -0
 
+void hap_vcpu_teardown(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    mfn_t mfn;
+
+    paging_lock(d);
+
+    if ( !paging_mode_hap(d) || !v->arch.paging.mode )
+        goto out;

Any particular reason you don't use paging_get_hostmode() (as the original code did) here? Any particular reason for the seemingly redundant (and hence somewhat in conflict with the description's "with the minimum number of safety checks possible") paging_mode_hap()?

xen/arch/x86/mm/shadow/common.c int shadow_enable(struct domain *d, u32 mode) · line 2784

Quoted 13 lines
xen/arch/x86/mm/shadow/common.c +7 -0
@@ -2775,6 +2775,32 @@ int shadow_enable(struct domain *d, u32 mode)
     return rv;
 }
 
+void shadow_vcpu_teardown(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+
+    paging_lock(d);
+
+    if ( !paging_mode_shadow(d) || !v->arch.paging.mode )

Same question regarding paging_get_hostmode() here, albeit I see the original code open-coded it in this case.

Jan

#2 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper

Various paths in vcpu_create() end up calling paging_update_paging_modes(), which eventually allocate a monitor pagetable if one doesn't exist.

However, an error in vcpu_create() results in the vcpu being cleaned up locally, and not put onto the domain's vcpu list. Therefore, the monitor table is not freed by {hap,shadow}_teardown()'s loop. This is caught by assertions later that we've successfully freed the entire hap/shadow memory pool.

The per-vcpu loops in domain teardown logic is conceptually wrong, but exist due to insufficient existing structure in the existing logic.

Break paging_vcpu_teardown() out of paging_teardown(), with mirrored breakouts in the hap/shadow code, and use it from arch_vcpu_create()'s error path. This fixes the memory leak.

Quoted 8 lines
Quoted 7 lines

The new {hap,shadow}_vcpu_teardown() must be idempotent, and are written to be as tolerable as possible, with the minimum number of safety checks possible. In particular, drop the mfn_valid() check - if junk is in these fields, then Xen is going to explode anyway.

Thanks.  (Wow it really is a long time since needing to drop everything for security work...)

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/mm/hap/hap.c +0 -0
@@ -563,30 +563,37 @@ void hap_final_teardown(struct domain *d)
     paging_unlock(d);
 }
 
Quoted 14 lines

xen/arch/x86/mm/hap/hap.c void hap_final_teardown(struct domain *d) · line 574

Quoted 9 lines
xen/arch/x86/mm/hap/hap.c +9 -0
+void hap_vcpu_teardown(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    mfn_t mfn;
+
+    paging_lock(d);
+
+    if ( !paging_mode_hap(d) || !v->arch.paging.mode )
+        goto out;

Any particular reason you don't use paging_get_hostmode() (as the original code did) here? Any particular reason for the seemingly redundant (and hence somewhat in conflict with the description's "with the minimum number of safety checks possible") paging_mode_hap()?

Yes to both.  As you spotted, I converted the shadow side first, and made the two consistent.

The paging_mode_{shadow,hap})() is necessary for idempotency.  These functions really might get called before paging is set up, for an early failure in domain_create().

The paging mode has nothing really to do with hostmode/guestmode/etc. It is the only way of expressing the logic where it is clear that the lower pointer dereferences are trivially safe.  (Also, the guestmode predicate isn't going to survive the nested virt work.  It's conceptually broken.)

~Andrew

#3 lore raw
Quoted 23 lines · Andrew Cooper
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Andrew Cooper
xen/arch/x86/mm/hap/hap.c +0 -0
@@ -563,30 +563,37 @@ void hap_final_teardown(struct domain *d)
     paging_unlock(d);
 }
 
Quoted 14 lines

xen/arch/x86/mm/hap/hap.c void hap_final_teardown(struct domain *d) · line 574

Quoted 9 lines
xen/arch/x86/mm/hap/hap.c +9 -0
+void hap_vcpu_teardown(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    mfn_t mfn;
+
+    paging_lock(d);
+
+    if ( !paging_mode_hap(d) || !v->arch.paging.mode )
+        goto out;

Any particular reason you don't use paging_get_hostmode() (as the original code did) here? Any particular reason for the seemingly redundant (and hence somewhat in conflict with the description's "with the minimum number of safety checks possible") paging_mode_hap()?

Quoted 6 lines

Yes to both.  As you spotted, I converted the shadow side first, and made the two consistent.

The paging_mode_{shadow,hap})() is necessary for idempotency.  These functions really might get called before paging is set up, for an early failure in domain_create().

In which case how would v->arch.paging.mode be non-NULL already? They get set in {hap,shadow}_vcpu_init() only.

Quoted 3 lines

The paging mode has nothing really to do with hostmode/guestmode/etc. It is the only way of expressing the logic where it is clear that the lower pointer dereferences are trivially safe.

Well, yes and no - the other uses of course should then also use paging_get_hostmode(), like various of the wrappers in paging.h do. Or else I question why we have paging_get_hostmode() in the first place. There are more examples in shadow code where this gets open-coded when it probably shouldn't be. There haven't been any such cases in HAP code so far ...

Additionally (noticing only now) in the shadow case you may now loop over all vCPU-s in shadow_teardown() just for shadow_vcpu_teardown() to bail right away. Wouldn't it make sense to retain the "if ( shadow_mode_enabled(d) )" there around the loop?

Jan

#4 lore raw
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
xen/arch/x86/mm/hap/hap.c +0 -0
@@ -563,30 +563,37 @@ void hap_final_teardown(struct domain *d)
     paging_unlock(d);
 }

xen/arch/x86/mm/hap/hap.c void hap_final_teardown(struct domain *d) · line 574

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +9 -0
 
+void hap_vcpu_teardown(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    mfn_t mfn;
+
+    paging_lock(d);
+
+    if ( !paging_mode_hap(d) || !v->arch.paging.mode )
+        goto out;

Any particular reason you don't use paging_get_hostmode() (as the original code did) here? Any particular reason for the seemingly redundant (and hence somewhat in conflict with the description's "with the minimum number of safety checks possible") paging_mode_hap()?

Quoted 8 lines
Quoted 6 lines

Yes to both.  As you spotted, I converted the shadow side first, and made the two consistent.

The paging_mode_{shadow,hap})() is necessary for idempotency.  These functions really might get called before paging is set up, for an early failure in domain_create().

In which case how would v->arch.paging.mode be non-NULL already? They get set in {hap,shadow}_vcpu_init() only.

Right, but we also might end up here with an error early in vcpu_create(), where d->arch.paging is set up, but v->arch.paging isn't.

This logic needs to be safe to use at any point of partial initialisation.

(And to be clear, I found I needed both of these based on some artificial error injection testing.)

Quoted 7 lines
Quoted 3 lines

The paging mode has nothing really to do with hostmode/guestmode/etc. It is the only way of expressing the logic where it is clear that the lower pointer dereferences are trivially safe.

Well, yes and no - the other uses of course should then also use paging_get_hostmode(), like various of the wrappers in paging.h do. Or else I question why we have paging_get_hostmode() in the first place.

I'm not convinced it is an appropriate abstraction to have, and I don't expect it to survive the nested virt work.

Quoted 3 lines

There are more examples in shadow code where this
gets open-coded when it probably shouldn't be. There haven't been any such cases in HAP code so far ...

Doesn't matter.  Its use here would obfuscate the code (this is one part of why I think it is a bad abstraction to begin with), and if the implementation ever changed, the function would lose its safety.

Quoted 5 lines

Additionally (noticing only now) in the shadow case you may now loop over all vCPU-s in shadow_teardown() just for shadow_vcpu_teardown() to bail right away. Wouldn't it make sense to retain the "if ( shadow_mode_enabled(d) )" there around the loop?

I'm not entirely convinced that was necessarily safe.  Irrespective, see the TODO.  The foreach_vcpu() is only a stopgap until some cleanup structure changes come along (which I had queued behind this patch anyway).

~Andrew

#5 lore raw
Quoted 66 lines · Andrew Cooper
Quoted 23 lines · Jan Beulich
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Jan Beulich
Quoted 5 lines · Andrew Cooper
xen/arch/x86/mm/hap/hap.c +0 -0
@@ -563,30 +563,37 @@ void hap_final_teardown(struct domain *d)
     paging_unlock(d);
 }

xen/arch/x86/mm/hap/hap.c void hap_final_teardown(struct domain *d) · line 574

Quoted 10 lines
xen/arch/x86/mm/hap/hap.c +9 -0
 
+void hap_vcpu_teardown(struct vcpu *v)
+{
+    struct domain *d = v->domain;
+    mfn_t mfn;
+
+    paging_lock(d);
+
+    if ( !paging_mode_hap(d) || !v->arch.paging.mode )
+        goto out;

Any particular reason you don't use paging_get_hostmode() (as the original code did) here? Any particular reason for the seemingly redundant (and hence somewhat in conflict with the description's "with the minimum number of safety checks possible") paging_mode_hap()?

Quoted 8 lines
Quoted 6 lines

Yes to both.  As you spotted, I converted the shadow side first, and made the two consistent.

The paging_mode_{shadow,hap})() is necessary for idempotency.  These functions really might get called before paging is set up, for an early failure in domain_create().

In which case how would v->arch.paging.mode be non-NULL already? They get set in {hap,shadow}_vcpu_init() only.

Right, but we also might end up here with an error early in vcpu_create(), where d->arch.paging is set up, but v->arch.paging isn't.

This logic needs to be safe to use at any point of partial initialisation.

(And to be clear, I found I needed both of these based on some artificial error injection testing.)

Quoted 7 lines
Quoted 3 lines

The paging mode has nothing really to do with hostmode/guestmode/etc. It is the only way of expressing the logic where it is clear that the lower pointer dereferences are trivially safe.

Well, yes and no - the other uses of course should then also use paging_get_hostmode(), like various of the wrappers in paging.h do. Or else I question why we have paging_get_hostmode() in the first place.

I'm not convinced it is an appropriate abstraction to have, and I don't expect it to survive the nested virt work.

Quoted 3 lines

There are more examples in shadow code where this
gets open-coded when it probably shouldn't be. There haven't been any such cases in HAP code so far ...

Doesn't matter.  Its use here would obfuscate the code (this is one part of why I think it is a bad abstraction to begin with), and if the implementation ever changed, the function would lose its safety.

Quoted 5 lines

Additionally (noticing only now) in the shadow case you may now loop over all vCPU-s in shadow_teardown() just for shadow_vcpu_teardown() to bail right away. Wouldn't it make sense to retain the "if ( shadow_mode_enabled(d) )" there around the loop?

Quoted 3 lines

I'm not entirely convinced that was necessarily safe.  Irrespective, see the TODO.  The foreach_vcpu() is only a stopgap until some cleanup structure changes come along (which I had queued behind this patch anyway).

Well, fair enough (for all of the points). You have my R-b already, and all you need to do (if you haven't already) is re-base the change, as the conflicting one of mine (which was triggered by reviewing yours) has gone in already.

Jan