Skip to contentSkip to conversation

xen: fix memory leak on error in vcpu_create

PATCH In review Stewart Hildebrand 3 participants 7 messages Jul 28, 2025 – Jul 29, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: fix memory leak on error in vcpu_create

In vcpu_create after scheduler data is allocated, if vmtrace_alloc_buffer fails, it will jump to the wrong cleanup label resulting in a memory leak. Correct the label.

Fixes 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")
---
 xen/common/domain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/domain.c +1 -1
index 3c65cca5b0ff..b1e2709d3d82 100644
@@ -451,7 +451,7 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int vcpu_id)
         goto fail_wq;
 
     if ( vmtrace_alloc_buffer(v) != 0 )
-        goto fail_wq;
+        goto fail_sched;
 
     if ( arch_vcpu_create(v) != 0 )
         goto fail_sched;

base-commit: a845b50c12f3ec3a14255f5eb3062d0c9801a356

Signature · 1 line
2.50.1
#1 lore raw
Quoted 6 lines · Stewart Hildebrand

In vcpu_create after scheduler data is allocated, if vmtrace_alloc_buffer fails, it will jump to the wrong cleanup label resulting in a memory leak. Correct the label.

Fixes 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")

Urgh, sorry for breaking this.  Ultimately it comes from having two different error handling schemes.

This patch is probably ok to start with (and to backport), but a better fix would be to handle sched and wq in vcpu_teardown().  That way we get a single failure path that does the correct thing irrespective.

An unrelated observation, but there's a waitqueue vcpu allocated in the common path, but I was under the impression that only x86 had any need for wqv (and I still need to get around to fixing introspection so we can drop wait.c entirely).

~Andrew

#2 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 6 lines · Stewart Hildebrand

In vcpu_create after scheduler data is allocated, if vmtrace_alloc_buffer fails, it will jump to the wrong cleanup label resulting in a memory leak. Correct the label.

Fixes 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")

Urgh, sorry for breaking this.  Ultimately it comes from having two different error handling schemes.

Quoted 8 lines

This patch is probably ok to start with (and to backport), but a better fix would be to handle sched and wq in vcpu_teardown().  That way we get a single failure path that does the correct thing irrespective.

An unrelated observation, but there's a waitqueue vcpu allocated in the common path, but I was under the impression that only x86 had any need for wqv (and I still need to get around to fixing introspection so we can drop wait.c entirely).

P.S. we allocate full wqv for idle CPUs, and they definitely do not need it on any architecture.  Looks like there's some low hanging fruit here too.

~Andrew

#3 lore raw
Quoted 15 lines · Andrew Cooper
Quoted 14 lines · Andrew Cooper
Quoted 6 lines · Stewart Hildebrand

In vcpu_create after scheduler data is allocated, if vmtrace_alloc_buffer fails, it will jump to the wrong cleanup label resulting in a memory leak. Correct the label.

Fixes 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")

Urgh, sorry for breaking this.  Ultimately it comes from having two different error handling schemes.

This patch is probably ok to start with (and to backport), but a better fix would be to handle sched and wq in vcpu_teardown().  That way we get a single failure path that does the correct thing irrespective.

Quoted 7 lines
Quoted 4 lines

An unrelated observation, but there's a waitqueue vcpu allocated in the common path, but I was under the impression that only x86 had any need for wqv (and I still need to get around to fixing introspection so we can drop wait.c entirely).

P.S. we allocate full wqv for idle CPUs, and they definitely do not need it on any architecture.  Looks like there's some low hanging fruit here too.

Hmm, the only init_waitqueue_vcpu() that I see is in an "else" to an "if ( is_idle_domain(d) )".

Jan

#4 lore raw
In reply to Andrew Cooper
Quoted 8 lines · Andrew Cooper
Quoted 6 lines · Stewart Hildebrand

In vcpu_create after scheduler data is allocated, if vmtrace_alloc_buffer fails, it will jump to the wrong cleanup label resulting in a memory leak. Correct the label.

Fixes 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")
Quoted 6 lines

Urgh, sorry for breaking this.  Ultimately it comes from having two different error handling schemes.

This patch is probably ok to start with (and to backport), but a better fix would be to handle sched and wq in vcpu_teardown().  That way we get a single failure path that does the correct thing irrespective.

I agree, and that variant would apparently be as easily backportable. Stewart, are you up for going that route?

Jan

Quoted 6 lines

An unrelated observation, but there's a waitqueue vcpu allocated in the common path, but I was under the impression that only x86 had any need for wqv (and I still need to get around to fixing introspection so we can drop wait.c entirely).

~Andrew

#5 lore raw
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Andrew Cooper
Quoted 6 lines · Stewart Hildebrand

In vcpu_create after scheduler data is allocated, if vmtrace_alloc_buffer fails, it will jump to the wrong cleanup label resulting in a memory leak. Correct the label.

Fixes 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")

Urgh, sorry for breaking this.  Ultimately it comes from having two different error handling schemes.

Quoted 6 lines
Quoted 3 lines

This patch is probably ok to start with (and to backport), but a better fix would be to handle sched and wq in vcpu_teardown().  That way we get a single failure path that does the correct thing irrespective.

I agree, and that variant would apparently be as easily backportable. Stewart, are you up for going that route?

Yep, I'll give it a try

Quoted 10 lines

Jan

Quoted 6 lines

An unrelated observation, but there's a waitqueue vcpu allocated in the common path, but I was under the impression that only x86 had any need for wqv (and I still need to get around to fixing introspection so we can drop wait.c entirely).

~Andrew

#6 lore raw
Quoted 12 lines · Stewart Hildebrand
Quoted 11 lines · Jan Beulich
Quoted 10 lines · Andrew Cooper
Quoted 6 lines · Stewart Hildebrand

In vcpu_create after scheduler data is allocated, if vmtrace_alloc_buffer fails, it will jump to the wrong cleanup label resulting in a memory leak. Correct the label.

Fixes 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")

Urgh, sorry for breaking this.  Ultimately it comes from having two different error handling schemes.

Quoted 6 lines
Quoted 5 lines
Quoted 3 lines

This patch is probably ok to start with (and to backport), but a better fix would be to handle sched and wq in vcpu_teardown().  That way we get a single failure path that does the correct thing irrespective.

I agree, and that variant would apparently be as easily backportable. Stewart, are you up for going that route?

Yep, I'll give it a try

Just as a heads up before you start, the key is to ensure that anything called in *_teardown() is idempotent.

~Andrew