[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2] xen: rework error handling in vcpu_create


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Stewart Hildebrand <stewart.hildebrand@xxxxxxx>
  • Date: Fri, 1 Aug 2025 16:24:16 -0400
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nuuNhzl+CLRgolTrlm0kKbEa3pU9+4fwb6K6xxKAvbo=; b=MFvpX9Qqa2n5udP2/WW7Ioc35EgpmJpUN2ktCbHulmh2iZ5Jc6lDVjN55pb/2AbUmlzf0xZ+vC2ujcXypG1jCmqBzdBzvP0MBqZOL9yYuXnffGDsbTEaGc/eOwsb0w+X0UUfOFpQgR5CXK3eBt7ANcHUQyK3/HoaB4sQi21AIRWzLDGzJdNaBYLjEVngdCZoj5gAytRM9NlVSyux9/BaMWGlxLhcWGW/yokwk6fUWrLBS8j7/MJPfItOoARv9BbhsaKZxyYLnj3chYkR6sPKUTa0rvmG2gcoXXUUt0HGXapRiiGfiztqG30vW+d6d1Mh1FOxLCSzPvKUGJRQCDeVuw==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=vG0Ps1IRCqOZ1Hp4ytJx0np1v4O8IXHTR5yfEIftIGcHO4Lio3Za1Rb/B4UoP07fUoyX4XRe/dT7Fau59RLctAE/pAvMQYwR50mODVr46UCV4sB4UW42DMnpEzYVW4inQxmZg9bzPISy+ttYs5WhhX6OR2Ph+GhHwe4MVZKLUllJOTyHYcSFGInp24hEIciTi6kH1doTxWPgkMfE06ogy04nkfEHA4ygua1nG11hcviCw9wxsaz3eMsp8fJM0/NK1ZlE57aaDCfI1XfRqxJRACJu1kzMcu8Mi65W3sUW0mE9vkKccoas90hfYm1BuOKdsyltT+whyQ0K2BH46LdQ6w==
  • Cc: Stewart Hildebrand <stewart.hildebrand@xxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, "Julien Grall" <julien@xxxxxxx>, Roger Pau Monné <roger.pau@xxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, "Dario Faggioli" <dfaggioli@xxxxxxxx>, Juergen Gross <jgross@xxxxxxxx>, "George Dunlap" <gwd@xxxxxxxxxxxxxx>
  • Delivery-date: Fri, 01 Aug 2025 20:24:56 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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.

Move sched_destroy_vcpu and destroy_waitqueue_vcpu to vcpu_teardown.
Make vcpu_teardown idempotent: deal with NULL unit.

Fix vcpu_runstate_get (called during XEN_SYSCTL_getdomaininfolist post
vcpu_teardown) when v->sched_unit is NULL.

Fixes: 217dd79ee292 ("xen/domain: Add vmtrace_size domain creation parameter")
Signed-off-by: Stewart Hildebrand <stewart.hildebrand@xxxxxxx>
---
v1->v2:
* move cleanup functions to vcpu_teardown
* renamed, was ("xen: fix memory leak on error in vcpu_create")
---
 xen/common/domain.c     | 14 ++++++--------
 xen/common/sched/core.c |  5 ++++-
 2 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/xen/common/domain.c b/xen/common/domain.c
index 5241a1629eeb..9c65c2974ea3 100644
--- a/xen/common/domain.c
+++ b/xen/common/domain.c
@@ -388,6 +388,8 @@ static int vmtrace_alloc_buffer(struct vcpu *v)
 static int vcpu_teardown(struct vcpu *v)
 {
     vmtrace_free_buffer(v);
+    sched_destroy_vcpu(v);
+    destroy_waitqueue_vcpu(v);
 
     return 0;
 }
@@ -448,13 +450,13 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int 
vcpu_id)
     }
 
     if ( sched_init_vcpu(v) != 0 )
-        goto fail_wq;
+        goto fail;
 
     if ( vmtrace_alloc_buffer(v) != 0 )
-        goto fail_wq;
+        goto fail;
 
     if ( arch_vcpu_create(v) != 0 )
-        goto fail_sched;
+        goto fail;
 
     d->vcpu[vcpu_id] = v;
     if ( vcpu_id != 0 )
@@ -472,11 +474,7 @@ struct vcpu *vcpu_create(struct domain *d, unsigned int 
vcpu_id)
 
     return v;
 
- fail_sched:
-    sched_destroy_vcpu(v);
- fail_wq:
-    destroy_waitqueue_vcpu(v);
-
+ fail:
     /* Must not hit a continuation in this context. */
     if ( vcpu_teardown(v) )
         ASSERT_UNREACHABLE();
diff --git a/xen/common/sched/core.c b/xen/common/sched/core.c
index 2ab4313517c3..fb7c99b05360 100644
--- a/xen/common/sched/core.c
+++ b/xen/common/sched/core.c
@@ -321,7 +321,7 @@ void vcpu_runstate_get(const struct vcpu *v,
      */
     unit = is_idle_vcpu(v) ? get_sched_res(v->processor)->sched_unit_idle
                            : v->sched_unit;
-    lock = likely(v == current) ? NULL : unit_schedule_lock_irq(unit);
+    lock = likely(v == current || !unit) ? NULL : unit_schedule_lock_irq(unit);
     memcpy(runstate, &v->runstate, sizeof(*runstate));
     delta = NOW() - runstate->state_entry_time;
     if ( delta > 0 )
@@ -839,6 +839,9 @@ void sched_destroy_vcpu(struct vcpu *v)
 {
     struct sched_unit *unit = v->sched_unit;
 
+    if ( !unit )
+        return;
+
     kill_timer(&v->periodic_timer);
     kill_timer(&v->singleshot_timer);
     kill_timer(&v->poll_timer);

base-commit: f6c990ac3cddc2d1965a7ab09324d821b05e4b6c
-- 
2.50.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.