Skip to contentSkip to conversation

xen/arm: Fail domain construction if a secondary vCPU cannot be created

PATCH Superseded Michal Orzel 4 participants 9 messages Jul 8

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/arm: Fail domain construction if a secondary vCPU cannot be created

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

When the guest probes the redistributor of a vCPU that was never created, get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
 xen/arch/arm/domain_build.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +1 -1
index 550617f152bb..b46574fd32aa 100644
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
         if ( vcpu_create(d, i) == NULL )
         {
             printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;
         }
 
         if ( is_64bit_domain(d) )
Signature · 1 line
2.43.0
#1 lore raw

Hi MIchal,

Nice catch. Few questions.

Quoted 7 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

When the guest probes the redistributor of a vCPU that was never created,

Shouldn't the guest check how many vCPUs were created and probe the ones that were created ?

Quoted 8 lines

get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

I see your reasoning.

Alternatively it can be a design choice. Xen does not commit to create the max_vcpus that was requested.

If Xen is unable to create any vCPU, it should abort domain creation.

If Xen creates lesser number of vCPUs than requested by max_vcpus, it can just print a warning and carry on.

In that case it should be the guest's responsibility to check the number of CPUs that it has.

- Ayan

Quoted 20 lines
Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
  xen/arch/arm/domain_build.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +1 -1
index 550617f152bb..b46574fd32aa 100644
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
          if ( vcpu_create(d, i) == NULL )
          {
              printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;
          }
 
          if ( is_64bit_domain(d) )
#2 lore raw
Quoted 11 lines · Ayan Kumar

Hi MIchal,

Nice catch. Few questions.

Quoted 6 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

Quoted 3 lines
Quoted 1 line

When the guest probes the redistributor of a vCPU that was never created,

Shouldn't the guest check how many vCPUs were created and probe the ones that were created ?

See below about DTB.

Quoted 9 lines
Quoted 8 lines

get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

Quoted 4 lines

I see your reasoning.

Alternatively it can be a design choice. Xen does not commit to create the max_vcpus that was requested.

Everything can be a design choice but this one wouldn't be wise, would it? All in all, we (Arm maintainers) aim at following the contract to fail as soon as possible if the user request cannot be satisfied.

Quoted 5 lines

If Xen is unable to create any vCPU, it should abort domain creation.

If Xen creates lesser number of vCPUs than requested by max_vcpus, it can just print a warning and carry on.

No. Xen creates domain DTB before creating vCPUs, so Xen advertises something that is not true. Here, Xen would create a DTB with e.g. 2 vCPUs while only 1 was created.

~Michal

Quoted 26 lines

In that case it should be the guest's responsibility to check the number of CPUs that it has.

- Ayan

Quoted 20 lines
Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
  xen/arch/arm/domain_build.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +1 -1
index 550617f152bb..b46574fd32aa 100644
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
          if ( vcpu_create(d, i) == NULL )
          {
              printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;
          }
 
          if ( is_64bit_domain(d) )
#3 lore raw
Quoted 26 lines · Michal
Quoted 11 lines · Ayan Kumar

Hi MIchal,

Nice catch. Few questions.

Quoted 6 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

Quoted 3 lines
Quoted 1 line

When the guest probes the redistributor of a vCPU that was never created,

Shouldn't the guest check how many vCPUs were created and probe the ones that were created ?

See below about DTB.

Quoted 8 lines
Quoted 8 lines

get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

Quoted 7 lines
Quoted 4 lines

I see your reasoning.

Alternatively it can be a design choice. Xen does not commit to create the max_vcpus that was requested.

Everything can be a design choice but this one wouldn't be wise, would it? All in all, we (Arm maintainers) aim at following the contract to fail as soon as possible if the user request cannot be satisfied.

Can we remove the redundant DTB node so that the contract is maintained ? IMHO , stopping a domain creation seems severe if the other vCPUs were created.

Quoted 8 lines
Quoted 5 lines

If Xen is unable to create any vCPU, it should abort domain creation.

If Xen creates lesser number of vCPUs than requested by max_vcpus, it can just print a warning and carry on.

No. Xen creates domain DTB before creating vCPUs, so Xen advertises something that is not true. Here, Xen would create a DTB with e.g. 2 vCPUs while only 1 was created.

I agree that this is a problem.

- Ayan

Quoted 27 lines

~Michal

Quoted 24 lines

In that case it should be the guest's responsibility to check the number of CPUs that it has.

- Ayan

Quoted 19 lines
Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
   xen/arch/arm/domain_build.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +1 -1
index 550617f152bb..b46574fd32aa 100644
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
           if ( vcpu_create(d, i) == NULL )
           {
               printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;
           }
 
           if ( is_64bit_domain(d) )
#4 lore raw
Quoted 30 lines · Ayan Kumar
Quoted 28 lines · Michal
Quoted 11 lines · Ayan Kumar

Hi MIchal,

Nice catch. Few questions.

Quoted 6 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

Quoted 3 lines
Quoted 1 line

When the guest probes the redistributor of a vCPU that was never created,

Shouldn't the guest check how many vCPUs were created and probe the ones that were created ?

See below about DTB.

Quoted 10 lines
Quoted 8 lines

get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

I see your reasoning.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Alternatively it can be a design choice. Xen does not commit to create the max_vcpus that was requested.

Everything can be a design choice but this one wouldn't be wise, would it? All in all, we (Arm maintainers) aim at following the contract to fail as soon as possible if the user request cannot be satisfied.

Can we remove the redundant DTB node so that the contract is maintained ? IMHO , stopping a domain creation seems severe if the other vCPUs were created.

No, we can't. First of all, the contract is to fail on unsatisifed user requests. User requested e.g. 5 vCPUS, we created only 2 - we should bail out. Please see all the dom0/dom0less code on Arm. Next, DTB generation and vCPU creation happens *before* starting domains, so this is definitely not something severe.

~Michal

Quoted 40 lines
Quoted 8 lines
Quoted 5 lines

If Xen is unable to create any vCPU, it should abort domain creation.

If Xen creates lesser number of vCPUs than requested by max_vcpus, it can just print a warning and carry on.

No. Xen creates domain DTB before creating vCPUs, so Xen advertises something that is not true. Here, Xen would create a DTB with e.g. 2 vCPUs while only 1 was created.

I agree that this is a problem.

- Ayan

Quoted 27 lines

~Michal

Quoted 24 lines

In that case it should be the guest's responsibility to check the number of CPUs that it has.

- Ayan

Quoted 19 lines
Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
   xen/arch/arm/domain_build.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +1 -1
index 550617f152bb..b46574fd32aa 100644
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
           if ( vcpu_create(d, i) == NULL )
           {
               printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;
           }
 
           if ( is_64bit_domain(d) )
#5 lore raw
In reply to Michal Orzel

Hi Michal,

Quoted 27 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

When the guest probes the redistributor of a vCPU that was never created, get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
  xen/arch/arm/domain_build.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +0 -0
index 550617f152bb..b46574fd32aa 100644

xen/arch/arm/domain_build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 1850

Quoted 5 lines
xen/arch/arm/domain_build.c +1 -1
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
          if ( vcpu_create(d, i) == NULL )
          {
              printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;

I would say returning "-ENOMEM" is more actual here, because vcpu_create() fails in most cases due to unable to allocate memory.

xen/arch/arm/domain_build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 1853

Quoted 3 lines
xen/arch/arm/domain_build.c +0 -0
          }
 
          if ( is_64bit_domain(d) )

BR, Dmytro.

#6 lore raw
Quoted 25 lines · On 08-Jul-26 11:18, Dmytro Prokopchuk1 wrote:

Hi Michal,

Quoted 22 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

When the guest probes the redistributor of a vCPU that was never created, get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
  xen/arch/arm/domain_build.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 13 lines

xen/arch/arm/domain_build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 1850

Quoted 10 lines
xen/arch/arm/domain_build.c +1 -1
index 550617f152bb..b46574fd32aa 100644
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
          if ( vcpu_create(d, i) == NULL )
          {
              printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;

I would say returning "-ENOMEM" is more actual here, because vcpu_create() fails in most cases due to unable to allocate memory.

I think you're right. Not all, but most of them yes. Will change on commit.

~Michal

#7 lore raw
In reply to Michal Orzel
Quoted 5 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

I'd suggest phrasing this as "partially constructed".  "holes" isn't really what's going on, given the linear nature of allocation.

Because of the object visibility, and because constructing vCPUs isn't atomic, all code needs to cope with d->vcpu[] having no, some or all of d->max_vcpus constructed.

Quoted 4 lines

When the guest probes the redistributor of a vCPU that was never created, get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

That's unsafe, especially as vcpu_id is calculated from an MMIO access.

xen/arch/arm/vgic-v3.c +0 -0
index c1c4d6f71ea8..c01cc596d593 100644
@@ -1111,10 +1111,10 @@ static struct vcpu *get_vcpu_from_rdist(struct

domain *d,
     unsigned int vcpu_id;

     vcpu_id = region->first_cpu + ((gpa - region->base) / GICV3_GICR_SIZE); -    if ( unlikely(vcpu_id >= d->max_vcpus) )
-        return NULL;

-    v = d->vcpu[vcpu_id];
+    v = domain_vcpu(d, vcpu_id);
+    if ( !v )
+        return NULL;

     *offset = gpa - v->arch.vgic.rdist_base;

Do you want me to submit this separately?

Quoted 18 lines

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
 xen/arch/arm/domain_build.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +0 -0
index 550617f152bb..b46574fd32aa 100644

xen/arch/arm/domain_build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 1853

Quoted 8 lines
xen/arch/arm/domain_build.c +1 -1
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
         if ( vcpu_create(d, i) == NULL )
         {
             printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;
         }
 
         if ( is_64bit_domain(d) )

On x86, we explicitly tolerate a failure to build all of dom0's CPUs, if at least one did get constructed.  This is to increase the chances that the server can boot and at least let an admin in to look at things.

However, I can see why such a behaviour is not wanted in a "single pre-packaged system" as used by automotive.

~Andrew

#8 lore raw
Quoted 41 lines · Andrew Cooper
Quoted 5 lines · Michal Orzel

construct_domain() creates the secondary vCPUs in a loop, but on a vcpu_create() failure it only prints a message and breaks out of the loop returning success. As a result the domain can be constructed with fewer vCPUs than d->max_vcpus, leaving NULL holes in d->vcpu[] below max_vcpus.

I'd suggest phrasing this as "partially constructed".  "holes" isn't really what's going on, given the linear nature of allocation.

Because of the object visibility, and because constructing vCPUs isn't atomic, all code needs to cope with d->vcpu[] having no, some or all of d->max_vcpus constructed.

Quoted 4 lines

When the guest probes the redistributor of a vCPU that was never created, get_vcpu_from_rdist() only checks vcpu_id against d->max_vcpus and then dereferences the NULL d->vcpu[vcpu_id], resulting in a data abort.

That's unsafe, especially as vcpu_id is calculated from an MMIO access.

xen/arch/arm/vgic-v3.c +0 -0
index c1c4d6f71ea8..c01cc596d593 100644
@@ -1111,10 +1111,10 @@ static struct vcpu *get_vcpu_from_rdist(struct
 domain *d,
      unsigned int vcpu_id;


     vcpu_id = region->first_cpu + ((gpa - region->base) / GICV3_GICR_SIZE); -    if ( unlikely(vcpu_id >= d->max_vcpus) )
-        return NULL;

-    v = d->vcpu[vcpu_id];
+    v = domain_vcpu(d, vcpu_id);
+    if ( !v )
+        return NULL;

     *offset = gpa - v->arch.vgic.rdist_base;

Quoted 1 line

Do you want me to submit this separately?

If you have time, yes. Otherwise I can do that too tomorrow.

Quoted 27 lines
Quoted 27 lines

Return an error instead of breaking out of the loop. Both callers (construct_domU() and construct_hwdom()) already propagate a negative return value and fail domain construction, which is the correct
behaviour: a domain that cannot provide the requested number of vCPUs should not be brought up.

Fixes 6b0e8e43348a ("xen/arm: allocate secondaries dom0 vcpus")
---
 xen/arch/arm/domain_build.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/domain_build.c +1 -1
index 550617f152bb..b46574fd32aa 100644
@@ -1847,7 +1847,7 @@ int __init construct_domain(struct domain *d, struct kernel_info *kinfo)
         if ( vcpu_create(d, i) == NULL )
         {
             printk("Failed to allocate d%dv%d\n", d->domain_id, i);
-            break;
+            return -EINVAL;
         }
 
Quoted 8 lines

xen/arch/arm/domain_build.c int __init construct_domain(struct domain *d, struct kernel_info *kinfo) · line 1853

Quoted 1 line
xen/arch/arm/domain_build.c +0 -0
         if ( is_64bit_domain(d) )

On x86, we explicitly tolerate a failure to build all of dom0's CPUs, if at least one did get constructed.  This is to increase the chances that the server can boot and at least let an admin in to look at things.

However, I can see why such a behaviour is not wanted in a "single pre-packaged system" as used by automotive.

Yes, that is a known difference in behavior between x86 and Arm. We decided to bail out asap.

~Michal