Skip to contentSkip to conversation

domain: add barrier in vcpu_create()

PATCH In review Jan Beulich 3 participants 6 messages Nov 26, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 domain: add barrier in vcpu_create()

The lock-less list updating isn't safe against racing for_each_vcpu(), unless done (by hardware) in exactly the order written.

Fixes 3037c5a2cb82 ("arm: domain")

---
The Fixes: tag is pretty arbitrary; the issue was becoming non-latent when Arm support was added. (Strictly speaking IA-64 and PPC would have been affected too afaict, just that now that doesn't matter anymore [or, for PPC, not yet, seeing that its support is being re-built from scratch].)

I'm not quite happy about prev_id being plain int, but changing it to unsigned (with suitable other adjustments) actually makes gcc15 generate worse code on x86.

xen/common/domain.c +1 -0
@@ -464,6 +464,7 @@ struct vcpu *vcpu_create(struct domain *
             prev_id--;
         BUG_ON(prev_id < 0);
         v->next_in_list = d->vcpu[prev_id]->next_in_list;
+        smp_wmb();
         d->vcpu[prev_id]->next_in_list = v;
     }
 
#1 lore raw
Quoted 19 lines · Jan Beulich

The lock-less list updating isn't safe against racing for_each_vcpu(), unless done (by hardware) in exactly the order written.

Fixes 3037c5a2cb82 ("arm: domain")

---
The Fixes: tag is pretty arbitrary; the issue was becoming non-latent when Arm support was added. (Strictly speaking IA-64 and PPC would have been affected too afaict, just that now that doesn't matter anymore [or, for PPC, not yet, seeing that its support is being re-built from scratch].)

I'm not quite happy about prev_id being plain int, but changing it to unsigned (with suitable other adjustments) actually makes gcc15 generate worse code on x86.

xen/common/domain.c struct vcpu *vcpu_create(struct domain * · line 470

Quoted 7 lines
xen/common/domain.c +1 -0
@@ -464,6 +464,7 @@ struct vcpu *vcpu_create(struct domain *
              prev_id--;
          BUG_ON(prev_id < 0);
          v->next_in_list = d->vcpu[prev_id]->next_in_list;
+        smp_wmb();
          d->vcpu[prev_id]->next_in_list = v;
      }
 

It should be noted that this is an issue only in case for_each_vcpu() is running against the idle domain during cpu hotplug.

All other domain get the vcpus populated form vcpu 0 upwards, so v->next_in_list will always be NULL for a new vcpu of a "normal" domain.

That said I believe the fix is fine and should be done, but there is no latent issue right now, as I believe cpu hotplug is supported for x86 today.

Juergen

#2 lore raw
Quoted 31 lines · Juergen Gross
Quoted 19 lines · Jan Beulich

The lock-less list updating isn't safe against racing for_each_vcpu(), unless done (by hardware) in exactly the order written.

Fixes 3037c5a2cb82 ("arm: domain")

---
The Fixes: tag is pretty arbitrary; the issue was becoming non-latent when Arm support was added. (Strictly speaking IA-64 and PPC would have been affected too afaict, just that now that doesn't matter anymore [or, for PPC, not yet, seeing that its support is being re-built from scratch].)

I'm not quite happy about prev_id being plain int, but changing it to unsigned (with suitable other adjustments) actually makes gcc15 generate worse code on x86.

xen/common/domain.c struct vcpu *vcpu_create(struct domain * · line 470

Quoted 7 lines
xen/common/domain.c +1 -0
@@ -464,6 +464,7 @@ struct vcpu *vcpu_create(struct domain *
              prev_id--;
          BUG_ON(prev_id < 0);
          v->next_in_list = d->vcpu[prev_id]->next_in_list;
+        smp_wmb();
          d->vcpu[prev_id]->next_in_list = v;
      }
 

It should be noted that this is an issue only in case for_each_vcpu() is running against the idle domain during cpu hotplug.

Quoted 2 lines

All other domain get the vcpus populated form vcpu 0 upwards, so v->next_in_list will always be NULL for a new vcpu of a "normal" domain.

Hmm, right. And I don't think we ever do for_each_vcpu() on the idle domain.

Jan

#3 lore raw
In reply to Jan Beulich
Quoted 18 lines · Jan Beulich

The lock-less list updating isn't safe against racing for_each_vcpu(), unless done (by hardware) in exactly the order written.

Fixes 3037c5a2cb82 ("arm: domain")

---
The Fixes: tag is pretty arbitrary; the issue was becoming non-latent when Arm support was added. (Strictly speaking IA-64 and PPC would have been affected too afaict, just that now that doesn't matter anymore [or, for PPC, not yet, seeing that its support is being re-built from scratch].)

I'm not quite happy about prev_id being plain int, but changing it to unsigned (with suitable other adjustments) actually makes gcc15 generate worse code on x86.

xen/common/domain.c struct vcpu *vcpu_create(struct domain * · line 470

Quoted 7 lines
xen/common/domain.c +1 -0
@@ -464,6 +464,7 @@ struct vcpu *vcpu_create(struct domain *
             prev_id--;
         BUG_ON(prev_id < 0);
         v->next_in_list = d->vcpu[prev_id]->next_in_list;
+        smp_wmb();
         d->vcpu[prev_id]->next_in_list = v;
     }
 

Where's the matching smp_rmb()?  There needs to be one for this smp_wmb() to be correct.

It's rather rhetorical, because clearly the smp_rmb() needs to be in for_each_vcpu() given your commit message, but we obviously don't want to do that.

This list can only be changed once during a VM's lifecycle, and even then it only gets appended to.  i.e. this particular piece of logic to splice a vCPU into the middle of a single linked list can be simplified to the second assignment, as the first is always copying NULL.

~Andrew

#4 lore raw
Quoted 30 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich

The lock-less list updating isn't safe against racing for_each_vcpu(), unless done (by hardware) in exactly the order written.

Fixes 3037c5a2cb82 ("arm: domain")

---
The Fixes: tag is pretty arbitrary; the issue was becoming non-latent when Arm support was added. (Strictly speaking IA-64 and PPC would have been affected too afaict, just that now that doesn't matter anymore [or, for PPC, not yet, seeing that its support is being re-built from scratch].)

I'm not quite happy about prev_id being plain int, but changing it to unsigned (with suitable other adjustments) actually makes gcc15 generate worse code on x86.

xen/common/domain.c struct vcpu *vcpu_create(struct domain * · line 470

Quoted 7 lines
xen/common/domain.c +1 -0
@@ -464,6 +464,7 @@ struct vcpu *vcpu_create(struct domain *
             prev_id--;
         BUG_ON(prev_id < 0);
         v->next_in_list = d->vcpu[prev_id]->next_in_list;
+        smp_wmb();
         d->vcpu[prev_id]->next_in_list = v;
     }
 

Where's the matching smp_rmb()?  There needs to be one for this smp_wmb() to be correct.

Quoted 3 lines

It's rather rhetorical, because clearly the smp_rmb() needs to be in for_each_vcpu() given your commit message, but we obviously don't want to do that.

It's not rhetorical at all, I think. It's not needed there because of the data dependency due to reading the loop iteration variable from the link fields. I.e. there are no two reads there which would need ordering against one another, and hence there's simply no-where to place a read barrier.

Quoted 4 lines

This list can only be changed once during a VM's lifecycle, and even then it only gets appended to.  i.e. this particular piece of logic to splice a vCPU into the middle of a single linked list can be simplified to the second assignment, as the first is always copying NULL.

Except for the idle domain, as Jürgen also mentioned.

Jan

#5 lore raw
In reply to Andrew Cooper
Quoted 30 lines · Andrew Cooper
Quoted 18 lines · Jan Beulich

The lock-less list updating isn't safe against racing for_each_vcpu(), unless done (by hardware) in exactly the order written.

Fixes 3037c5a2cb82 ("arm: domain")

---
The Fixes: tag is pretty arbitrary; the issue was becoming non-latent when Arm support was added. (Strictly speaking IA-64 and PPC would have been affected too afaict, just that now that doesn't matter anymore [or, for PPC, not yet, seeing that its support is being re-built from scratch].)

I'm not quite happy about prev_id being plain int, but changing it to unsigned (with suitable other adjustments) actually makes gcc15 generate worse code on x86.

xen/common/domain.c struct vcpu *vcpu_create(struct domain * · line 470

Quoted 7 lines
xen/common/domain.c +1 -0
@@ -464,6 +464,7 @@ struct vcpu *vcpu_create(struct domain *
              prev_id--;
          BUG_ON(prev_id < 0);
          v->next_in_list = d->vcpu[prev_id]->next_in_list;
+        smp_wmb();
          d->vcpu[prev_id]->next_in_list = v;
      }
 

Where's the matching smp_rmb()?  There needs to be one for this smp_wmb() to be correct.

Quoted 8 lines

It's rather rhetorical, because clearly the smp_rmb() needs to be in for_each_vcpu() given your commit message, but we obviously don't want to do that.

This list can only be changed once during a VM's lifecycle, and even then it only gets appended to.  i.e. this particular piece of logic to splice a vCPU into the middle of a single linked list can be simplified to the second assignment, as the first is always copying NULL.

This is not true for cpu hotplug: the associated idle vcpu will be inserted at the appropriate place in the list.

The question is whether we call for_each_vcpu() for the idle domain at all.

Juergen