Skip to contentSkip to conversation

x86/domain: adjust limitation on shared_info allocation below 4G

PATCH Superseded Roger Pau Monne 3 participants 18 messages Feb 4 – Feb 5

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/domain: adjust limitation on shared_info allocation below 4G

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")
---
 xen/arch/x86/domain.c    |  7 ++++---
 xen/arch/x86/pv/domain.c | 20 ++++++++++++++++++++
 xen/common/domain.c      |  2 +-
 xen/include/xen/domain.h |  2 ++
 4 files changed, 27 insertions(+), 4 deletions(-)
xen/arch/x86/domain.c +4 -3
index edb76366b596..3e701f2146c9 100644
@@ -882,10 +882,11 @@ int arch_domain_create(struct domain *d,
         goto fail;
 
     /*
-     * The shared_info machine address must fit in a 32-bit field within a
-     * 32-bit guest's start_info structure. Hence we specify MEMF_bits(32).
+     * For 32bit PV guests the shared_info machine address must fit in a 32-bit
+     * field within the guest's start_info structure.  We might need to free
+     * and allocate later if the guest turns out to be a 32bit PV one.
      */
-    if ( (d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32))) == NULL )
+    if ( (d->shared_info = alloc_xenheap_page()) == NULL )
         goto fail;
 
     clear_page(d->shared_info);
xen/arch/x86/pv/domain.c +20 -0
index 01499582d2d6..8ced3d70a52f 100644
@@ -247,6 +247,26 @@ int switch_compat(struct domain *d)
     d->arch.has_32bit_shinfo = 1;
     d->arch.pv.is_32bit = true;
 
+    /* Check whether the shared_info page needs to be moved below 4G. */
+    if ( virt_to_maddr(d->shared_info) >> 32 )
+    {
+        shared_info_t *prev = d->shared_info;
+
+        d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32));
+        if ( !d->shared_info )
+        {
+            d->shared_info = prev;
+            rc = -ENOMEM;
+            goto undo_and_fail;
+        }
+        put_page(virt_to_page(prev));
+        clear_page(d->shared_info);
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);
+    }
+
     for_each_vcpu( d, v )
     {
         if ( (rc = setup_compat_arg_xlat(v)) ||
xen/common/domain.c +1 -1
index 376351b528c9..11fed22455b9 100644
@@ -305,7 +305,7 @@ static void vcpu_check_shutdown(struct vcpu *v)
     spin_unlock(&d->shutdown_lock);
 }
 
-static void vcpu_info_reset(struct vcpu *v)
+void vcpu_info_reset(struct vcpu *v)
 {
     struct domain *d = v->domain;
 
xen/include/xen/domain.h +2 -0
index 273717c31b3f..52cdf012c491 100644
@@ -83,6 +83,8 @@ void cf_check free_pirq_struct(void *ptr);
 int  arch_vcpu_create(struct vcpu *v);
 void arch_vcpu_destroy(struct vcpu *v);
 
+void vcpu_info_reset(struct vcpu *v);
+
 int map_guest_area(struct vcpu *v, paddr_t gaddr, unsigned int size,
                    struct guest_area *area,
                    void (*populate)(void *dst, struct vcpu *v));
Signature · 1 line
2.51.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")

The tag is here because there is the (largely theoretical?) possibility for a system to have no memory at all left below 4Gb, in which case creation of a PV64 or non-shadow HVM guest would needlessly fail?

Quoted 1 line

with two nits:

Quoted 12 lines
xen/arch/x86/pv/domain.c +6 -0
@@ -247,6 +247,26 @@ int switch_compat(struct domain *d)
     d->arch.has_32bit_shinfo = 1;
     d->arch.pv.is_32bit = true;
 
+    /* Check whether the shared_info page needs to be moved below 4G. */
+    if ( virt_to_maddr(d->shared_info) >> 32 )
+    {
+        shared_info_t *prev = d->shared_info;
+
+        d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32));

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 265

Quoted 10 lines
xen/arch/x86/pv/domain.c +10 -0
+        if ( !d->shared_info )
+        {
+            d->shared_info = prev;
+            rc = -ENOMEM;
+            goto undo_and_fail;
+        }
+        put_page(virt_to_page(prev));
+        clear_page(d->shared_info);
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */

"references to ... page are dropped" reads as if talk was of page references. vcpu_info_reset() writes out pointers only, though. May I suggest

        /* Ensure all pointers to the old shared_info page are replaced. */

?

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 266

Quoted 1 line
xen/arch/x86/pv/domain.c +1 -0
+        for_each_vcpu( d, v )

A common oddity, where my personal take is that only one of two forms are possible (without ./CODING_STYLE saying anything explicit): Either such a for_each_... is deemed a (kind-of) keyword (like "for"), then it wants to be

for_each_vcpu ( d, v )

or it is deemed not to be one, then it wants to be

        for_each_vcpu(d, v)

Jan

#2 lore raw
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Quoted 5 lines
Quoted 1 line
Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")

The tag is here because there is the (largely theoretical?) possibility for a system to have no memory at all left below 4Gb, in which case creation of a PV64 or non-shadow HVM guest would needlessly fail?

It's kid of an issue we discovered when using strict domain NUMA node placement. At that point the toolstack would exhaust all memory on node 0 and by doing that inadvertently consume all memory below 4G.

Quoted 31 lines
Quoted 1 line

with two nits:

Quoted 12 lines
xen/arch/x86/pv/domain.c +6 -0
@@ -247,6 +247,26 @@ int switch_compat(struct domain *d)
     d->arch.has_32bit_shinfo = 1;
     d->arch.pv.is_32bit = true;
 
+    /* Check whether the shared_info page needs to be moved below 4G. */
+    if ( virt_to_maddr(d->shared_info) >> 32 )
+    {
+        shared_info_t *prev = d->shared_info;
+
+        d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32));

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 265

Quoted 10 lines
xen/arch/x86/pv/domain.c +10 -0
+        if ( !d->shared_info )
+        {
+            d->shared_info = prev;
+            rc = -ENOMEM;
+            goto undo_and_fail;
+        }
+        put_page(virt_to_page(prev));
+        clear_page(d->shared_info);
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */

"references to ... page are dropped" reads as if talk was of page references. vcpu_info_reset() writes out pointers only, though. May I suggest

Quoted 1 line
        /* Ensure all pointers to the old shared_info page are replaced. */

Fine with me.

Quoted 9 lines

?

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 266

Quoted 1 line
xen/arch/x86/pv/domain.c +1 -0
+        for_each_vcpu( d, v )

A common oddity, where my personal take is that only one of two forms are possible (without ./CODING_STYLE saying anything explicit): Either such a for_each_... is deemed a (kind-of) keyword (like "for"), then it wants to be

for_each_vcpu ( d, v )

Oh right, I copied it from the instance below and didn't notice the missing spaces.

Thanks, Roger.

#3 lore raw
Quoted 12 lines · Roger Pau Monné
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")
Quoted 7 lines
Quoted 3 lines

The tag is here because there is the (largely theoretical?) possibility for a system to have no memory at all left below 4Gb, in which case creation of a PV64 or non-shadow HVM guest would needlessly fail?

It's kid of an issue we discovered when using strict domain NUMA node placement. At that point the toolstack would exhaust all memory on node 0 and by doing that inadvertently consume all memory below 4G.

Right, and hence also my "memory: arrange to conserve on DMA reservation", where I'm still fighting with myself as to what to do with the comments you gave there.

Jan

#4 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")

The tag is here because there is the (largely theoretical?) possibility for a system to have no memory at all left below 4Gb, in which case creation of a PV64 or non-shadow HVM guest would needlessly fail?

Quoted 7 lines
Quoted 3 lines

It's kid of an issue we discovered when using strict domain NUMA node placement. At that point the toolstack would exhaust all memory on node 0 and by doing that inadvertently consume all memory below 4G.

Right, and hence also my "memory: arrange to conserve on DMA reservation", where I'm still fighting with myself as to what to do with the comments you gave there.

Better fighting with yourself rather than fighting with me I guess ;).

That change would be controversial with what we currently do on XenServer, because we don't (yet) special case the memory below 4G to not account for it in the per node free amount of memory.

What would happen when you append the MEMF_no_dma flag as proposed in your commit, but the caller is also passing MEMF_exact_node with target node 0? AFAICT the allocation would still refuse to use the low 4G pool.

Also, your commit should also be expanded to avoid staking claims that would drain the DMA pool, as then populate_physmap() won't be able to allocate from there? It would be weird for the toolstack to have successfully made a claim, and then populate_physmap() returning -ENOMEM because (part of) the claim has been made against the DMA pool, which populate_physmap() would explicitly forbidden to allocate from.

Thanks, Roger.

#5 lore raw
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")

The tag is here because there is the (largely theoretical?) possibility for a system to have no memory at all left below 4Gb, in which case creation of a PV64 or non-shadow HVM guest would needlessly fail?

Quoted 7 lines
Quoted 3 lines

It's kid of an issue we discovered when using strict domain NUMA node placement. At that point the toolstack would exhaust all memory on node 0 and by doing that inadvertently consume all memory below 4G.

Right, and hence also my "memory: arrange to conserve on DMA reservation", where I'm still fighting with myself as to what to do with the comments you gave there.

Better fighting with yourself rather than fighting with me I guess ;).

Quoted 8 lines

That change would be controversial with what we currently do on XenServer, because we don't (yet) special case the memory below 4G to not account for it in the per node free amount of memory.

What would happen when you append the MEMF_no_dma flag as proposed in your commit, but the caller is also passing MEMF_exact_node with target node 0? AFAICT the allocation would still refuse to use the low 4G pool.

Yes, DMA-ability is intended to take higher priority than exact-node requests. Another node would then need choosing by the toolstack.

Quoted 3 lines

Also, your commit should also be expanded to avoid staking claims that would drain the DMA pool, as then populate_physmap() won't be able to allocate from there?

Except that upstream claims aren't node-specific, yet, so could be fulfilled my taking memory from other nodes? Aiui the problem would only occur if that DAM-able memory was the only memory left in the system.

Jan

Quoted 7 lines

It would be weird for the toolstack to have
successfully made a claim, and then populate_physmap() returning -ENOMEM because (part of) the claim has been made against the DMA pool, which populate_physmap() would explicitly forbidden to allocate from.

Thanks, Roger.

#6 lore raw
Quoted 41 lines · Jan Beulich
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")

The tag is here because there is the (largely theoretical?) possibility for a system to have no memory at all left below 4Gb, in which case creation of a PV64 or non-shadow HVM guest would needlessly fail?

Quoted 7 lines
Quoted 3 lines

It's kid of an issue we discovered when using strict domain NUMA node placement. At that point the toolstack would exhaust all memory on node 0 and by doing that inadvertently consume all memory below 4G.

Right, and hence also my "memory: arrange to conserve on DMA reservation", where I'm still fighting with myself as to what to do with the comments you gave there.

Better fighting with yourself rather than fighting with me I guess ;).

Quoted 8 lines

That change would be controversial with what we currently do on XenServer, because we don't (yet) special case the memory below 4G to not account for it in the per node free amount of memory.

What would happen when you append the MEMF_no_dma flag as proposed in your commit, but the caller is also passing MEMF_exact_node with target node 0? AFAICT the allocation would still refuse to use the low 4G pool.

Yes, DMA-ability is intended to take higher priority than exact-node requests. Another node would then need choosing by the toolstack.

Quoted 6 lines
Quoted 3 lines

Also, your commit should also be expanded to avoid staking claims that would drain the DMA pool, as then populate_physmap() won't be able to allocate from there?

Except that upstream claims aren't node-specific, yet, so could be fulfilled my taking memory from other nodes?

That's likely to change at some point, but yes, they are not node specific yet.

Quoted 3 lines

Aiui the problem would
only occur if that DAM-able memory was the only memory left in the system.

Indeed, in that scenario toolstack will be allowed to make claims that cover that DMA memory, yet populate physmap won't be able to consume those claims.

I think there are two item that need to be done for us to append MEMF_no_dma to populate physmap allocations:

* DMA memory is not reachable by claims.
* DMA memory must be reported to the toolstack, so it can account for it separately from free memory.

Last point could also be solved by subtracting the DMA memory from the free_pages value returned to the toolstack.

Thanks, Roger.

#7 lore raw
Quoted 52 lines · Roger Pau Monné
Quoted 41 lines · Jan Beulich
Quoted 28 lines · Roger Pau Monné
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monné
Quoted 14 lines · Jan Beulich
Quoted 9 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")

The tag is here because there is the (largely theoretical?) possibility for a system to have no memory at all left below 4Gb, in which case creation of a PV64 or non-shadow HVM guest would needlessly fail?

Quoted 7 lines
Quoted 3 lines

It's kid of an issue we discovered when using strict domain NUMA node placement. At that point the toolstack would exhaust all memory on node 0 and by doing that inadvertently consume all memory below 4G.

Right, and hence also my "memory: arrange to conserve on DMA reservation", where I'm still fighting with myself as to what to do with the comments you gave there.

Better fighting with yourself rather than fighting with me I guess ;).

Quoted 8 lines

That change would be controversial with what we currently do on XenServer, because we don't (yet) special case the memory below 4G to not account for it in the per node free amount of memory.

What would happen when you append the MEMF_no_dma flag as proposed in your commit, but the caller is also passing MEMF_exact_node with target node 0? AFAICT the allocation would still refuse to use the low 4G pool.

Yes, DMA-ability is intended to take higher priority than exact-node requests. Another node would then need choosing by the toolstack.

Quoted 6 lines
Quoted 3 lines

Also, your commit should also be expanded to avoid staking claims that would drain the DMA pool, as then populate_physmap() won't be able to allocate from there?

Except that upstream claims aren't node-specific, yet, so could be fulfilled my taking memory from other nodes?

That's likely to change at some point, but yes, they are not node specific yet.

Quoted 7 lines
Quoted 3 lines

Aiui the problem would
only occur if that DAM-able memory was the only memory left in the system.

Indeed, in that scenario toolstack will be allowed to make claims that cover that DMA memory, yet populate physmap won't be able to consume those claims.

It would be (following said patch of mine), but only in order-0 chunks. Which would make ...

Quoted 9 lines

I think there are two item that need to be done for us to append MEMF_no_dma to populate physmap allocations:

* DMA memory is not reachable by claims.
* DMA memory must be reported to the toolstack, so it can account for it separately from free memory.

Last point could also be solved by subtracting the DMA memory from the free_pages value returned to the toolstack.

... any of this more difficult. We don't want to completely prevent its use, we only want to (heuristically) limit it.

Jan

#8 lore raw
In reply to Roger Pau Monne
Quoted 3 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

All shared info?  HVM does use it, but doesn't see the MFN.

Quoted 22 lines

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")
---
 xen/arch/x86/domain.c    |  7 ++++---
 xen/arch/x86/pv/domain.c | 20 ++++++++++++++++++++
 xen/common/domain.c      |  2 +-
 xen/include/xen/domain.h |  2 ++
 4 files changed, 27 insertions(+), 4 deletions(-)
xen/arch/x86/domain.c +0 -0
index edb76366b596..3e701f2146c9 100644
@@ -882,10 +882,11 @@ int arch_domain_create(struct domain *d,
         goto fail;
 
     /*

xen/arch/x86/domain.c int arch_domain_create(struct domain *d, · line 891

Quoted 10 lines
xen/arch/x86/domain.c +4 -3
-     * The shared_info machine address must fit in a 32-bit field within a
-     * 32-bit guest's start_info structure. Hence we specify MEMF_bits(32).
+     * For 32bit PV guests the shared_info machine address must fit in a 32-bit
+     * field within the guest's start_info structure.  We might need to free
+     * and allocate later if the guest turns out to be a 32bit PV one.
      */
-    if ( (d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32))) == NULL )
+    if ( (d->shared_info = alloc_xenheap_page()) == NULL )
         goto fail;
 

The comment is now out of place when the source is read naturally.  I'd suggest dropping the comment entirely, and ...

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 250

Quoted 10 lines
xen/arch/x86/domain.c +0 -0
     clear_page(d->shared_info);
xen/arch/x86/pv/domain.c +1 -0
index 01499582d2d6..8ced3d70a52f 100644
@@ -247,6 +247,26 @@ int switch_compat(struct domain *d)
     d->arch.has_32bit_shinfo = 1;
     d->arch.pv.is_32bit = true;
 
+    /* Check whether the shared_info page needs to be moved below 4G. */

... extending this one talking about the 32bit field.

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 263

Quoted 13 lines
xen/arch/x86/pv/domain.c +13 -0
+    if ( virt_to_maddr(d->shared_info) >> 32 )
+    {
+        shared_info_t *prev = d->shared_info;
+
+        d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32));
+        if ( !d->shared_info )
+        {
+            d->shared_info = prev;
+            rc = -ENOMEM;
+            goto undo_and_fail;
+        }
+        put_page(virt_to_page(prev));
+        clear_page(d->shared_info);

I think copy_page() would be more appropriate.  That way there are fewer implicit ordering dependencies.

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 267

Quoted 4 lines
xen/arch/x86/pv/domain.c +4 -0
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);

switch_compat() can only occur on a domain with no memory.  How can we have outstanding references?

~Andrew

#9 lore raw
Quoted 6 lines · Andrew Cooper
Quoted 3 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

All shared info?  HVM does use it, but doesn't see the MFN.

I think it's really start_info that is meant here, as that's where the restriction comes from.

Quoted 14 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 263

Quoted 13 lines
xen/arch/x86/pv/domain.c +13 -0
+    if ( virt_to_maddr(d->shared_info) >> 32 )
+    {
+        shared_info_t *prev = d->shared_info;
+
+        d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32));
+        if ( !d->shared_info )
+        {
+            d->shared_info = prev;
+            rc = -ENOMEM;
+            goto undo_and_fail;
+        }
+        put_page(virt_to_page(prev));
+        clear_page(d->shared_info);
Quoted 2 lines

I think copy_page() would be more appropriate.  That way there are fewer implicit ordering dependencies.

I'd strongly recommend against copy_page() here. If there was any data in there that would need copying, it would need to be done field-wise, using the compat xlat machinery. The layouts are different. It may be prudent to assert that the original page is still completely zeroed.

Jan

#10 lore raw
Quoted 13 lines · Jan Beulich
Quoted 13 lines
Quoted 13 lines
xen/arch/x86/pv/domain.c +13 -0
+    if ( virt_to_maddr(d->shared_info) >> 32 )
+    {
+        shared_info_t *prev = d->shared_info;
+
+        d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32));
+        if ( !d->shared_info )
+        {
+            d->shared_info = prev;
+            rc = -ENOMEM;
+            goto undo_and_fail;
+        }
+        put_page(virt_to_page(prev));
+        clear_page(d->shared_info);
Quoted 6 lines
Quoted 2 lines

I think copy_page() would be more appropriate.  That way there are fewer implicit ordering dependencies.

I'd strongly recommend against copy_page() here. If there was any data in there that would need copying, it would need to be done field-wise, using the compat xlat machinery. The layouts are different. It may be prudent to assert that the original page is still completely zeroed.

Oh, yes.  The layouts will be different.  Fine.

~Andrew

#11 lore raw
In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper
Quoted 3 lines · Roger Pau Monne

The limitation of shared_info being allocated below 4G to fit in the start_info field only applies to 32bit PV guests. On 64bit PV guests the start_info field is 64bits wide. HVM guests don't use start_info at all.

All shared info?  HVM does use it, but doesn't see the MFN.

HVM guest use shared_info, but not start_info. The issue is not with shared_info itself, but the size of the field used to store shared_info in start_info.

HVM doesn't use start_info, and hence doesn't care about the position of shared_info in that regard.

Quoted 45 lines
Quoted 22 lines

Drop the restriction in arch_domain_create() and instead free and re-allocate the page from memory below 4G if needed in switch_compat(), when the guest is set to run in 32bit PV mode.

Fixes 3cadc0469d5c ("x86_64: shared_info must be allocated below 4GB as it is advertised to 32-bit guests via a 32-bit machine address field in start_info.")
---
 xen/arch/x86/domain.c    |  7 ++++---
 xen/arch/x86/pv/domain.c | 20 ++++++++++++++++++++
 xen/common/domain.c      |  2 +-
 xen/include/xen/domain.h |  2 ++
 4 files changed, 27 insertions(+), 4 deletions(-)
xen/arch/x86/domain.c +0 -0
index edb76366b596..3e701f2146c9 100644
@@ -882,10 +882,11 @@ int arch_domain_create(struct domain *d,
         goto fail;
 
     /*

xen/arch/x86/domain.c int arch_domain_create(struct domain *d, · line 891

Quoted 10 lines
xen/arch/x86/domain.c +4 -3
-     * The shared_info machine address must fit in a 32-bit field within a
-     * 32-bit guest's start_info structure. Hence we specify MEMF_bits(32).
+     * For 32bit PV guests the shared_info machine address must fit in a 32-bit
+     * field within the guest's start_info structure.  We might need to free
+     * and allocate later if the guest turns out to be a 32bit PV one.
      */
-    if ( (d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32))) == NULL )
+    if ( (d->shared_info = alloc_xenheap_page()) == NULL )
         goto fail;
 

The comment is now out of place when the source is read naturally.  I'd suggest dropping the comment entirely, and ...

Quoted 9 lines
xen/arch/x86/domain.c +0 -0
     clear_page(d->shared_info);
xen/arch/x86/pv/domain.c +0 -0
index 01499582d2d6..8ced3d70a52f 100644
@@ -247,6 +247,26 @@ int switch_compat(struct domain *d)
     d->arch.has_32bit_shinfo = 1;
     d->arch.pv.is_32bit = true;
 
Quoted 3 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 250

Quoted 1 line
xen/arch/x86/pv/domain.c +1 -0
+    /* Check whether the shared_info page needs to be moved below 4G. */

... extending this one talking about the 32bit field.

Hm, yes, I've considered doing that. Unless Jan objects I will move the comment then, seeing as you also think it's best.

Quoted 14 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 263

Quoted 13 lines
xen/arch/x86/pv/domain.c +13 -0
+    if ( virt_to_maddr(d->shared_info) >> 32 )
+    {
+        shared_info_t *prev = d->shared_info;
+
+        d->shared_info = alloc_xenheap_pages(0, MEMF_bits(32));
+        if ( !d->shared_info )
+        {
+            d->shared_info = prev;
+            rc = -ENOMEM;
+            goto undo_and_fail;
+        }
+        put_page(virt_to_page(prev));
+        clear_page(d->shared_info);
Quoted 2 lines

I think copy_page() would be more appropriate.  That way there are fewer implicit ordering dependencies.

Hm, I had a copy_page() initially, but I don't think it's appropriate because (most of?) the fields will need translation. I don't think translation is feasible, but I could at least call update_domain_wallclock_time() which is what hvm_latch_shinfo_size() does when changing bitness.

Quoted 7 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 267

Quoted 4 lines
xen/arch/x86/pv/domain.c +4 -0
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);

switch_compat() can only occur on a domain with no memory.  How can we have outstanding references?

As Jan pointed out, it's not references, but stashed pointers to the previous shared_info page. I've used the wrong wording here.

Thanks, Roger.

#12 lore raw
Quoted 8 lines · Roger Pau Monné
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Roger Pau Monne
xen/arch/x86/pv/domain.c +0 -0
@@ -247,6 +247,26 @@ int switch_compat(struct domain *d)
     d->arch.has_32bit_shinfo = 1;
     d->arch.pv.is_32bit = true;
 
Quoted 6 lines
Quoted 3 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 250

Quoted 1 line
xen/arch/x86/pv/domain.c +1 -0
+    /* Check whether the shared_info page needs to be moved below 4G. */

... extending this one talking about the 32bit field.

Hm, yes, I've considered doing that. Unless Jan objects I will move the comment then, seeing as you also think it's best.

Definitely no objection from me. My R-b stands with that movement.

Jan

#13 lore raw
In reply to Roger Pau Monné
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 267

Quoted 4 lines
xen/arch/x86/pv/domain.c +4 -0
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);

switch_compat() can only occur on a domain with no memory.  How can we have outstanding references?

As Jan pointed out, it's not references, but stashed pointers to the previous shared_info page. I've used the wrong wording here.

Yes, I saw that thread, but my question still stands.

How can there be any this early in the domain's lifecycle?

~Andrew

#14 lore raw
Quoted 5 lines · Andrew Cooper
Quoted 4 lines · Roger Pau Monné
Quoted 4 lines
Quoted 4 lines
xen/arch/x86/pv/domain.c +4 -0
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

switch_compat() can only occur on a domain with no memory.  How can we have outstanding references?

As Jan pointed out, it's not references, but stashed pointers to the previous shared_info page. I've used the wrong wording here.

Yes, I saw that thread, but my question still stands.

How can there be any this early in the domain's lifecycle?

Can't (aren't) vCPU-s added ahead of adding memory?

Jan

#15 lore raw
Quoted 8 lines · Jan Beulich
Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Roger Pau Monné
Quoted 6 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 267

Quoted 4 lines
xen/arch/x86/pv/domain.c +4 -0
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);

switch_compat() can only occur on a domain with no memory.  How can we have outstanding references?

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

As Jan pointed out, it's not references, but stashed pointers to the previous shared_info page. I've used the wrong wording here.

Yes, I saw that thread, but my question still stands.

How can there be any this early in the domain's lifecycle?

Can't (aren't) vCPU-s added ahead of adding memory?

At least on x86 when using xl/libxl the call to XEN_DOMCTL_set_address_size happens after the call to XEN_DOMCTL_max_vcpus, and the later calls vcpu_create() which sets the pointer into the shared_info page for legacy (< 32) vCPUs.

Even if we could invert those two calls, it's impossible to know what other toolstacks might do. I think we need to keep the vcpu_info_reset() call.

Thanks, Roger.

#16 lore raw
Quoted 11 lines · Roger Pau Monné
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines

xen/arch/x86/pv/domain.c int switch_compat(struct domain *d) · line 267

Quoted 4 lines
xen/arch/x86/pv/domain.c +4 -0
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);

switch_compat() can only occur on a domain with no memory.  How can we have outstanding references?

As Jan pointed out, it's not references, but stashed pointers to the previous shared_info page. I've used the wrong wording here.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

Yes, I saw that thread, but my question still stands.

How can there be any this early in the domain's lifecycle?

Can't (aren't) vCPU-s added ahead of adding memory?

At least on x86 when using xl/libxl the call to XEN_DOMCTL_set_address_size happens after the call to XEN_DOMCTL_max_vcpus, and the later calls vcpu_create() which sets the pointer into the shared_info page for legacy (< 32) vCPUs.

Geez, that disaster of an interface still has spike traps we're falling foul of.

Please extend the comment to note the first 32 vcpu compatibility case.

But, combined with the format change (clear vs copy page), doesn't it mean there's an existing bug here?

Even without moving the shared_info page, aren't the cached pointers made wrong by switch_compat() ?

This is one area where we've got no XTF testing at all, and I probably ought to see about fixing that.

~Andrew

#17 lore raw
Quoted 24 lines · Andrew Cooper
Quoted 11 lines · Roger Pau Monné
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Andrew Cooper
Quoted 8 lines · Roger Pau Monné
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+        share_xen_page_with_guest(virt_to_page(d->shared_info), d, SHARE_rw);
+        /* Ensure all references to the old shared_info page are dropped. */
+        for_each_vcpu( d, v )
+            vcpu_info_reset(v);

switch_compat() can only occur on a domain with no memory.  How can we have outstanding references?

As Jan pointed out, it's not references, but stashed pointers to the previous shared_info page. I've used the wrong wording here.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

Yes, I saw that thread, but my question still stands.

How can there be any this early in the domain's lifecycle?

Can't (aren't) vCPU-s added ahead of adding memory?

At least on x86 when using xl/libxl the call to XEN_DOMCTL_set_address_size happens after the call to XEN_DOMCTL_max_vcpus, and the later calls vcpu_create() which sets the pointer into the shared_info page for legacy (< 32) vCPUs.

Geez, that disaster of an interface still has spike traps we're falling foul of.

Quoted 7 lines

Please extend the comment to note the first 32 vcpu compatibility case.

But, combined with the format change (clear vs copy page), doesn't it mean there's an existing bug here?

Even without moving the shared_info page, aren't the cached pointers made wrong by switch_compat() ?

No, they are not wrong because the vcpu_info array is at the start of shared_info, and it has the same size (64bytes) on both 32 and 64bits. Hence the offsets into the different vcpu_info array elements are the same regardless of bitness.

Quoted 2 lines

This is one area where we've got no XTF testing at all, and I probably ought to see about fixing that.

Yeah, it's not great.

Thanks, Roger.