Skip to contentSkip to conversation

Guest XenStore page allocation for 11 Dom0less domUs

PATCH In review Henry Wang 7 participants 26 messages May 17, 2024 – May 27, 2024

lore
Share & tools
lore

Henry Wang

author
#0 lore raw

v3 cover Guest XenStore page allocation for 11 Dom0less domUs

Hi all,

This series is trying to fix the reported guest magic region alloc issue for 11 Dom0less domUs, an error message can seen from the init-dom0less application on 1:1 direct-mapped Dom0less DomUs:

```
Allocating magic pages
memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1
Error on alloc magic pages
```

This is because populate_physmap() automatically assumes gfn == mfn for direct mapped domains. This cannot be true for the magic pages that are allocated later for 1:1 Dom0less DomUs from the init-dom0less helper application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.

In [1] I've tried to fix this issue by the domctl approach, and discussions in [2] and [3] indicates that a domctl is not really necessary, as we can simplify the issue to "allocate the Dom0less guest magic regions at the Dom0less DomU build time and pass the region base PFN to init-dom0less application". The later on discussion [4] reached an agreement that we only need to allocate one single page for XenStore, and set the HVM_PARAM_STORE_PFN from hypervisor with some Linux XenStore late init protocol improvements. Therefore, this series tries to fix the issue based on all discussions. The first patch puts a restriction that static shared memory on direct-mapped DomUs should also be direct mapped, as otherwise it will clash [5]. Patch 2 allocates the XenStore page from Xen and set the initial connection status to RECONNECTED. Patch 3 is the update of the init-dom0less application with all of the changes. Patch 4 is the doc change to reflect the changes introduced by this series.

**NOTE**: This series should work with the Linux change [6].

[1] https://lore.kernel.org/xen-devel/[email protected]/
[2] https://lore.kernel.org/xen-devel/[email protected]/
[3] https://lore.kernel.org/xen-devel/alpine.DEB.2.22.394.2404251508470.3940@ubuntu-linux-20-04-desktop/
[4] https://lore.kernel.org/xen-devel/[email protected]/
[5] https://lore.kernel.org/xen-devel/[email protected]/
[6] https://lore.kernel.org/xen-devel/[email protected]/

Henry Wang (4):
xen/arm/static-shmem: Static-shmem should be direct-mapped for
direct-mapped domains
xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor tools/init-dom0less: Avoid hardcoding GUEST_MAGIC_BASE docs/features/dom0less: Update the late XenStore init protocol

 docs/features/dom0less.pandoc         | 12 +++---
 docs/misc/arm/device-tree/booting.txt |  3 ++
 tools/helpers/init-dom0less.c         | 58 +++++++--------------------
 xen/arch/arm/dom0less-build.c         | 44 +++++++++++++++++++-
 xen/arch/arm/static-shmem.c           |  6 +++
 5 files changed, 73 insertions(+), 50 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v3 1/4 xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Currently, users are allowed to map static shared memory in a non-direct-mapped way for direct-mapped domains. This can lead to clashing of guest memory spaces. Also, the current extended region finding logic only removes the host physical addresses of the static shared memory areas for direct-mapped domains, which may be inconsistent with the guest memory map if users map the static shared memory in a non-direct-mapped way. This will lead to incorrect extended region calculation results.

To make things easier, add restriction that static shared memory should also be direct-mapped for direct-mapped domains. Check the host physical address to be matched with guest physical address when parsing the device tree. Document this restriction in the doc.

---
v3:
- New patch.

---
 docs/misc/arm/device-tree/booting.txt | 3 +++
 xen/arch/arm/static-shmem.c           | 6 ++++++
 2 files changed, 9 insertions(+)
docs/misc/arm/device-tree/booting.txt +3 -0
index bbd955e9c2..c994e48391 100644
@@ -591,6 +591,9 @@ communication.
     shared memory region in host physical address space, a size, and a guest
     physical address, as the target address of the mapping.
     e.g. xen,shared-mem = < [host physical address] [guest address] [size] >
+    Note that if a domain is direct-mapped, i.e. the Dom0 and the Dom0less
+    DomUs with `direct-map` device tree property, the static shared memory
+    should also be direct-mapped (host physical address == guest address).
 
     It shall also meet the following criteria:
     1) If the SHM ID matches with an existing region, the address range of the
xen/arch/arm/static-shmem.c +6 -0
index 78881dd1d3..b26fb69874 100644
@@ -235,6 +235,12 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
                    d, psize);
             return -EINVAL;
         }
+        if ( is_domain_direct_mapped(d) && (pbase != gbase) )
+        {
+            printk("%pd: physical address 0x%"PRIpaddr" and guest address 0x%"PRIpaddr" are not 1:1 direct-mapped.\n",
+                   d, pbase, gbase);
+            return -EINVAL;
+        }
 
         for ( i = 0; i < PFN_DOWN(psize); i++ )
             if ( !mfn_valid(mfn_add(maddr_to_mfn(pbase), i)) )
Signature · 1 line
2.34.1
Acked-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Hi Henry,

+CC: Luca

Quoted 11 lines · Henry Wang

Currently, users are allowed to map static shared memory in a non-direct-mapped way for direct-mapped domains. This can lead to clashing of guest memory spaces. Also, the current extended region finding logic only removes the host physical addresses of the static shared memory areas for direct-mapped domains, which may be inconsistent with the guest memory map if users map the static shared memory in a non-direct-mapped way. This will lead to incorrect extended region calculation results.

Quoted 4 lines

To make things easier, add restriction that static shared memory should also be direct-mapped for direct-mapped domains. Check the host physical address to be matched with guest physical address when parsing the device tree. Document this restriction in the doc.

I'm ok with this restriction.

@Luca, do you have any use case preventing us from making this restriction?

This patch clashes with Luca series so depending on which goes first,

Quoted 29 lines

---
v3:
- New patch.

---
 docs/misc/arm/device-tree/booting.txt | 3 +++
 xen/arch/arm/static-shmem.c           | 6 ++++++
 2 files changed, 9 insertions(+)
docs/misc/arm/device-tree/booting.txt +3 -0
index bbd955e9c2..c994e48391 100644
@@ -591,6 +591,9 @@ communication.
     shared memory region in host physical address space, a size, and a guest
     physical address, as the target address of the mapping.
     e.g. xen,shared-mem = < [host physical address] [guest address] [size] >
+    Note that if a domain is direct-mapped, i.e. the Dom0 and the Dom0less
+    DomUs with `direct-map` device tree property, the static shared memory
+    should also be direct-mapped (host physical address == guest address).
 
     It shall also meet the following criteria:
     1) If the SHM ID matches with an existing region, the address range of the
xen/arch/arm/static-shmem.c +0 -0
index 78881dd1d3..b26fb69874 100644

xen/arch/arm/static-shmem.c int __init process_shm(struct domain *d, struct kernel_info *kinfo, · line 240

Quoted 6 lines
xen/arch/arm/static-shmem.c +3 -0
@@ -235,6 +235,12 @@ int __init process_shm(struct domain *d, struct kernel_info *kinfo,
                    d, psize);
             return -EINVAL;
         }
+        if ( is_domain_direct_mapped(d) && (pbase != gbase) )
+        {
+            printk("%pd: physical address 0x%"PRIpaddr" and guest address 0x%"PRIpaddr" are not 1:1 direct-mapped.\n",

NIT: 1:1 and direct-mapped means the same so no need to place them next to each other

Quoted 10 lines
xen/arch/arm/static-shmem.c +3 -1
+                   d, pbase, gbase);
+            return -EINVAL;
+        }
 
         for ( i = 0; i < PFN_DOWN(psize); i++ )
             if ( !mfn_valid(mfn_add(maddr_to_mfn(pbase), i)) )
--

2.34.1

~Michal

#3 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Quoted 20 lines · Michal Orzel

Hi Henry,

+CC: Luca

Quoted 11 lines · Henry Wang

Currently, users are allowed to map static shared memory in a non-direct-mapped way for direct-mapped domains. This can lead to clashing of guest memory spaces. Also, the current extended region finding logic only removes the host physical addresses of the static shared memory areas for direct-mapped domains, which may be inconsistent with the guest memory map if users map the static shared memory in a non-direct-mapped way. This will lead to incorrect extended region calculation results.

Quoted 7 lines
Quoted 4 lines

To make things easier, add restriction that static shared memory should also be direct-mapped for direct-mapped domains. Check the host physical address to be matched with guest physical address when parsing the device tree. Document this restriction in the doc.

I'm ok with this restriction.

@Luca, do you have any use case preventing us from making this restriction?

Hi Michal, Henry,

I think it’s sensible, I don’t think we have any use case for direct-mapped domains using non direct mapped static shared memory.

Cheers,
Luca

author
#4 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

In reply to Michal Orzel

Hi Michal, Luca,

Quoted 9 lines · Michal Orzel

Hi Henry,

+CC: Luca

Quoted 4 lines · Henry Wang

To make things easier, add restriction that static shared memory should also be direct-mapped for direct-mapped domains. Check the host physical address to be matched with guest physical address when parsing the device tree. Document this restriction in the doc.

Quoted 5 lines

I'm ok with this restriction.

@Luca, do you have any use case preventing us from making this restriction?

This patch clashes with Luca series so depending on which goes first,

I agree that there will be some conflicts between the two series. To avoid back and forth, if Luca's series goes in first, would it be ok for you if I place the same check from this patch in handle_shared_mem_bank() like below?

xen/arch/arm/static-shmem.c +0 -0
index 9c3a83042d..2d23fa4917 100644
@@ -219,6 +219,13 @@ static int __init handle_shared_mem_bank(struct domain *d, paddr_t gbase,

     pbase = shm_bank->start;
     psize = shm_bank->size;

+    if ( is_domain_direct_mapped(d) && (pbase != gbase) )
+    {
+        printk("%pd: physical address 0x%"PRIpaddr" and guest address 0x%"PRIpaddr" are not direct-mapped.\n", +               d, pbase, gbase);
+        return -EINVAL;
+    }
+
     printk("%pd: SHMEM map from %s: mphys 0x%"PRIpaddr" -> gphys 0x%"PRIpaddr", size 0x%"PRIpaddr"\n", d, bank_from_heap ? "Xen heap" : "Host", pbase, gbase, psize);

Quoted 1 line

Thanks. I will take the tag if you are ok with above diff (for the case if this series goes in later than Luca's).

Quoted 5 lines

Review

Quoted 4 lines
          }
(quoted diff) +3 -0
+        if ( is_domain_direct_mapped(d) && (pbase != gbase) )
+        {
+            printk("%pd: physical address 0x%"PRIpaddr" and guest address 0x%"PRIpaddr" are not 1:1 direct-mapped.\n",

NIT: 1:1 and direct-mapped means the same so no need to place them next to each other

Ok. I will drop the "1:1" in the next version. Thanks.

Kind regards,
Henry

Quoted 1 line

~Michal

#5 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Hi Henry,

Quoted 44 lines · Henry Wang

Hi Michal, Luca,

Quoted 9 lines · Michal Orzel

Hi Henry,

+CC: Luca

Quoted 4 lines · Henry Wang

To make things easier, add restriction that static shared memory should also be direct-mapped for direct-mapped domains. Check the host physical address to be matched with guest physical address when parsing the device tree. Document this restriction in the doc.

Quoted 5 lines

I'm ok with this restriction.

@Luca, do you have any use case preventing us from making this restriction?

This patch clashes with Luca series so depending on which goes first,

I agree that there will be some conflicts between the two series. To avoid back and forth, if Luca's series goes in first, would it be ok for you if I place the same check from this patch in handle_shared_mem_bank() like below?

xen/arch/arm/static-shmem.c +7 -0
index 9c3a83042d..2d23fa4917 100644
@@ -219,6 +219,13 @@ static int __init handle_shared_mem_bank(struct 
 domain *d, paddr_t gbase,
      pbase = shm_bank->start;
      psize = shm_bank->size;
 
+    if ( is_domain_direct_mapped(d) && (pbase != gbase) )
+    {
+        printk("%pd: physical address 0x%"PRIpaddr" and guest address 
 0x%"PRIpaddr" are not direct-mapped.\n",
+               d, pbase, gbase);
+        return -EINVAL;
+    }
+
      printk("%pd: SHMEM map from %s: mphys 0x%"PRIpaddr" -> gphys 

0x%"PRIpaddr", size 0x%"PRIpaddr"\n",
            d, bank_from_heap ? "Xen heap" : "Host", pbase, gbase, psize);

Quoted 4 lines
Quoted 1 line

Thanks. I will take the tag if you are ok with above diff (for the case if this series goes in later than Luca's).

I would move this check to process_shm() right after "gbase = dt_read_paddr" setting. This would be the most natural placement for such a check.

~Michal

author
#6 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Hi Michal,

Quoted 46 lines · Michal Orzel

Hi Henry,

Quoted 43 lines · Henry Wang

Hi Michal, Luca,

Quoted 9 lines · Michal Orzel

Hi Henry,

+CC: Luca

Quoted 4 lines · Henry Wang

To make things easier, add restriction that static shared memory should also be direct-mapped for direct-mapped domains. Check the host physical address to be matched with guest physical address when parsing the device tree. Document this restriction in the doc.

Quoted 5 lines

I'm ok with this restriction.

@Luca, do you have any use case preventing us from making this restriction?

This patch clashes with Luca series so depending on which goes first,

I agree that there will be some conflicts between the two series. To avoid back and forth, if Luca's series goes in first, would it be ok for you if I place the same check from this patch in handle_shared_mem_bank() like below?

xen/arch/arm/static-shmem.c +7 -0
index 9c3a83042d..2d23fa4917 100644
@@ -219,6 +219,13 @@ static int __init handle_shared_mem_bank(struct
 domain *d, paddr_t gbase,
       pbase = shm_bank->start;
       psize = shm_bank->size;
 
+    if ( is_domain_direct_mapped(d) && (pbase != gbase) )
+    {
+        printk("%pd: physical address 0x%"PRIpaddr" and guest address
 0x%"PRIpaddr" are not direct-mapped.\n",
+               d, pbase, gbase);
+        return -EINVAL;
+    }
+
       printk("%pd: SHMEM map from %s: mphys 0x%"PRIpaddr" -> gphys

0x%"PRIpaddr", size 0x%"PRIpaddr"\n",
            d, bank_from_heap ? "Xen heap" : "Host", pbase, gbase, psize);

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks. I will take the tag if you are ok with above diff (for the case if this series goes in later than Luca's).

I would move this check to process_shm() right after "gbase = dt_read_paddr" setting. This would be the most natural placement for such a check.

That sounds good. Thanks! IIUC we only need to add the check for the pbase != INVALID_PADDR case correct?

Kind regards,
Henry

Quoted 1 line

~Michal

#7 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Quoted 49 lines · Henry Wang

Hi Michal,

Quoted 46 lines · Michal Orzel

Hi Henry,

Quoted 43 lines · Henry Wang

Hi Michal, Luca,

Quoted 9 lines · Michal Orzel

Hi Henry,

+CC: Luca

Quoted 4 lines · Henry Wang

To make things easier, add restriction that static shared memory should also be direct-mapped for direct-mapped domains. Check the host physical address to be matched with guest physical address when parsing the device tree. Document this restriction in the doc.

Quoted 5 lines

I'm ok with this restriction.

@Luca, do you have any use case preventing us from making this restriction?

This patch clashes with Luca series so depending on which goes first,

I agree that there will be some conflicts between the two series. To avoid back and forth, if Luca's series goes in first, would it be ok for you if I place the same check from this patch in handle_shared_mem_bank() like below?

xen/arch/arm/static-shmem.c +7 -0
index 9c3a83042d..2d23fa4917 100644
@@ -219,6 +219,13 @@ static int __init handle_shared_mem_bank(struct
 domain *d, paddr_t gbase,
       pbase = shm_bank->start;
       psize = shm_bank->size;
 
+    if ( is_domain_direct_mapped(d) && (pbase != gbase) )
+    {
+        printk("%pd: physical address 0x%"PRIpaddr" and guest address
 0x%"PRIpaddr" are not direct-mapped.\n",
+               d, pbase, gbase);
+        return -EINVAL;
+    }
+
       printk("%pd: SHMEM map from %s: mphys 0x%"PRIpaddr" -> gphys

0x%"PRIpaddr", size 0x%"PRIpaddr"\n",
            d, bank_from_heap ? "Xen heap" : "Host", pbase, gbase, psize);

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Thanks. I will take the tag if you are ok with above diff (for the case if this series goes in later than Luca's).

I would move this check to process_shm() right after "gbase = dt_read_paddr" setting. This would be the most natural placement for such a check.

That sounds good. Thanks! IIUC we only need to add the check for the pbase != INVALID_PADDR case correct?

Yes, but at the same time I wonder whether we should also return error if a user omits pbase for direct mapped domain.

~Michal

author
#8 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Hi Michal,

Quoted 8 lines · Michal Orzel
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Thanks. I will take the tag if you are ok with above diff (for the case if this series goes in later than Luca's).

I would move this check to process_shm() right after "gbase = dt_read_paddr" setting. This would be the most natural placement for such a check.

That sounds good. Thanks! IIUC we only need to add the check for the pbase != INVALID_PADDR case correct?

Yes, but at the same time I wonder whether we should also return error if a user omits pbase for direct mapped domain.

I think this makes sense. So I will add also a check for the case if users omit pbase in the device tree for the direct mapped domain.

Kind regards,
Henry

Quoted 2 lines

~Michal

#9 lore raw

Re: [PATCH v3 1/4] xen/arm/static-shmem: Static-shmem should be direct-mapped for direct-mapped domains

Quoted 9 lines · Henry Wang

Hi Michal,

Quoted 6 lines · Michal Orzel
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Thanks. I will take the tag if you are ok with above diff (for the case
if this series goes in later than Luca's).

I would move this check to process_shm() right after "gbase = dt_read_paddr" setting.
This would be the most natural placement for such a check.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

That sounds good. Thanks! IIUC we only need to add the check for the pbase != INVALID_PADDR case correct?

Yes, but at the same time I wonder whether we should also return error if a user omits pbase
for direct mapped domain.

I think this makes sense. So I will add also a check for the case if users omit pbase in the device tree for the direct mapped domain.

I fixed the NIT and added the ack, but as Luca's series hasn't been committed yet, I have not made this change. I'll leave it to Julien when he commits both series.

author
#10 lore raw

PATCH v3 2/4 xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

In reply to Henry Wang

There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.

An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:

```
Allocating magic pages
memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1
Error on alloc magic pages
```

The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through populate_physmap(), and populate_physmap() automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.

Since for init-dom0less, the magic page region is only for XenStore. To solve above issue, this commit allocates the XenStore page for Dom0less DomUs at the domain construction time. The PFN will be noted and communicated to the init-dom0less application executed from Dom0. To keep the XenStore late init protocol, set the connection status to XENSTORE_RECONNECT.

---
v3:
- Only allocate XenStore page. (Julien)
- Set HVM_PARAM_STORE_PFN and the XenStore connection status directly from hypervisor. (Stefano)
v2:
- Reword the commit msg to explain what is "magic page" and use generic terminology "hypervisor reserved pages" in commit msg. (Daniel)
- Also move the offset definition of magic pages. (Michal)
- Extract the magic page allocation logic to a function. (Michal)

---
 xen/arch/arm/dom0less-build.c | 44 ++++++++++++++++++++++++++++++++++-
 1 file changed, 43 insertions(+), 1 deletion(-)
xen/arch/arm/dom0less-build.c +43 -1
index 74f053c242..95c4fd1a2d 100644
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 #include <xen/device_tree.h>
+#include <xen/domain_page.h>
 #include <xen/err.h>
 #include <xen/event.h>
 #include <xen/grant_table.h>
@@ -10,6 +11,8 @@
 #include <xen/sizes.h>
 #include <xen/vmap.h>
 
+#include <public/io/xs_wire.h>
+
 #include <asm/arm64/sve.h>
 #include <asm/dom0less-build.h>
 #include <asm/domain_build.h>
@@ -739,6 +742,42 @@ static int __init alloc_xenstore_evtchn(struct domain *d)
     return 0;
 }
 
+#define XENSTORE_PFN_OFFSET 1
+static int __init alloc_xenstore_page(struct domain *d)
+{
+    struct page_info *xenstore_pg;
+    struct xenstore_domain_interface *interface;
+    mfn_t mfn;
+    gfn_t gfn;
+    int rc;
+
+    d->max_pages += 1;
+    xenstore_pg = alloc_domheap_page(d, 0);
+    if ( xenstore_pg == NULL )
+        return -ENOMEM;
+
+    mfn = page_to_mfn(xenstore_pg);
+    if ( !is_domain_direct_mapped(d) )
+        gfn = gaddr_to_gfn(GUEST_MAGIC_BASE +
+                           (XENSTORE_PFN_OFFSET << PAGE_SHIFT));
+    else
+        gfn = gaddr_to_gfn(mfn_to_maddr(mfn));
+
+    rc = guest_physmap_add_page(d, gfn, mfn, 0);
+    if ( rc )
+    {
+        free_domheap_page(xenstore_pg);
+        return rc;
+    }
+
+    d->arch.hvm.params[HVM_PARAM_STORE_PFN] = gfn_x(gfn);
+    interface = (struct xenstore_domain_interface *)map_domain_page(mfn);
+    interface->connection = XENSTORE_RECONNECT;
+    unmap_domain_page(interface);
+
+    return 0;
+}
+
 static int __init construct_domU(struct domain *d,
                                  const struct dt_device_node *node)
 {
@@ -839,7 +878,10 @@ static int __init construct_domU(struct domain *d,
         rc = alloc_xenstore_evtchn(d);
         if ( rc < 0 )
             return rc;
-        d->arch.hvm.params[HVM_PARAM_STORE_PFN] = ~0ULL;
+
+        rc = alloc_xenstore_page(d);
+        if ( rc < 0 )
+            return rc;
     }
 
     return rc;
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach. The documentation of the new logic is in:

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the call last week). Some comments below.

Quoted 24 lines · Henry Wang

There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.

An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:

```
Allocating magic pages
memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1
Error on alloc magic pages
```

The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through populate_physmap(), and populate_physmap() automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.

Quoted 6 lines

Since for init-dom0less, the magic page region is only for XenStore. To solve above issue, this commit allocates the XenStore page for Dom0less DomUs at the domain construction time. The PFN will be noted and communicated to the init-dom0less application executed from Dom0. To keep the XenStore late init protocol, set the connection status to XENSTORE_RECONNECT.

So this commit is allocating the page, but it will not be used by init-dom0less until the next patch. But Linux could use it. So would this break bisection? If so, then I think patch #3 needs to be folded in this patch.

Quoted 42 lines

---
v3:
- Only allocate XenStore page. (Julien)
- Set HVM_PARAM_STORE_PFN and the XenStore connection status directly from hypervisor. (Stefano)
v2:
- Reword the commit msg to explain what is "magic page" and use generic terminology "hypervisor reserved pages" in commit msg. (Daniel)
- Also move the offset definition of magic pages. (Michal)
- Extract the magic page allocation logic to a function. (Michal)

---
  xen/arch/arm/dom0less-build.c | 44 ++++++++++++++++++++++++++++++++++-
  1 file changed, 43 insertions(+), 1 deletion(-)
xen/arch/arm/dom0less-build.c +3 -0
index 74f053c242..95c4fd1a2d 100644
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: GPL-2.0-only */
  #include <xen/device_tree.h>
+#include <xen/domain_page.h>
  #include <xen/err.h>
  #include <xen/event.h>
  #include <xen/grant_table.h>
@@ -10,6 +11,8 @@
  #include <xen/sizes.h>
  #include <xen/vmap.h>
 
+#include <public/io/xs_wire.h>
+
  #include <asm/arm64/sve.h>
  #include <asm/dom0less-build.h>
  #include <asm/domain_build.h>
@@ -739,6 +742,42 @@ static int __init alloc_xenstore_evtchn(struct domain *d)
      return 0;
  }
 

xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 754

Quoted 10 lines
xen/arch/arm/dom0less-build.c +10 -0
+#define XENSTORE_PFN_OFFSET 1
+static int __init alloc_xenstore_page(struct domain *d)
+{
+    struct page_info *xenstore_pg;
+    struct xenstore_domain_interface *interface;
+    mfn_t mfn;
+    gfn_t gfn;
+    int rc;
+
+    d->max_pages += 1;

Sorry I should have spotted it earlier. But you want to check d->max_pages is not overflowing. You can look at acquire_shared_memory_bank() for how to do it.

Also, maybe we want an helper to do it so it is not open-coded in multiple places.

xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 755

Quoted 1 line
xen/arch/arm/dom0less-build.c +1 -0
+    xenstore_pg = alloc_domheap_page(d, 0);

I think we may want to restrict where the page is allocated. For instance, 32-bit domain using short page-tables will not be able to address all the physical memory.

I would consider to try to allocate the page below 32-bit (using MEMF-bits(32). And then fallback to above 32-bit only 64-bit for domain.

Also, just to note that in theory alloc_domheap_page() could return MFN 0. In practice we have excluded MFN 0 because it breaks the page allocator so far.

But I would still prefer if we add a check on the MFN. This will make easier to spot any issue if we ever give MFN 0 to the allocator.

A possible implementation would be to call alloc_domphea_page() a second time and then free the first one (e.g. MFN 0).

Quoted 8 lines
xen/arch/arm/dom0less-build.c +8 -0
+    if ( xenstore_pg == NULL )
+        return -ENOMEM;
+
+    mfn = page_to_mfn(xenstore_pg);
+    if ( !is_domain_direct_mapped(d) )
+        gfn = gaddr_to_gfn(GUEST_MAGIC_BASE +
+                           (XENSTORE_PFN_OFFSET << PAGE_SHIFT));
+    else

xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 774

Quoted 10 lines
xen/arch/arm/dom0less-build.c +10 -0
+        gfn = gaddr_to_gfn(mfn_to_maddr(mfn));
+
+    rc = guest_physmap_add_page(d, gfn, mfn, 0); > +    if ( rc )
+    {
+        free_domheap_page(xenstore_pg);
+        return rc;
+    }
+
+    d->arch.hvm.params[HVM_PARAM_STORE_PFN] = gfn_x(gfn);
+    interface = (struct xenstore_domain_interface *)map_domain_page(mfn);

map_domain_page() will return a void *. So why do you need to an explicit case?

Quoted 11 lines
xen/arch/arm/dom0less-build.c +6 -0
+    interface->connection = XENSTORE_RECONNECT;
+    unmap_domain_page(interface);
+
+    return 0;
+}
+
  static int __init construct_domU(struct domain *d,
                                   const struct dt_device_node *node)
  {
@@ -839,7 +878,10 @@ static int __init construct_domU(struct domain *d,
          rc = alloc_xenstore_evtchn(d);

xen/arch/arm/dom0less-build.c static int __init construct_domU(struct domain *d, · line 887

Quoted 10 lines
xen/arch/arm/dom0less-build.c +4 -1
          if ( rc < 0 )
              return rc;
-        d->arch.hvm.params[HVM_PARAM_STORE_PFN] = ~0ULL;
+
+        rc = alloc_xenstore_page(d);
+        if ( rc < 0 )
+            return rc;
      }
 
      return rc;

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Quoted 8 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach. The documentation of the new logic is in:

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the call last week). Some comments below.

I'm not against this logic, but I'm wondering why it needs to be so complicated.

Can't the domU itself allocate the Xenstore page from its RAM pages, write the PFN into the Xenstore grant tab entry, and then make it public via setting HVM_PARAM_STORE_PFN?

The init-dom0less application could then check HVM_PARAM_STORE_PFN being set and call XS_introduce_domain.

Note that at least C-xenstored does not need the PFN of the Xenstore page, as it is just using GNTTAB_RESERVED_XENSTORE for mapping the page.

Juergen

#13 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Quoted 17 lines · Jürgen Groß
Quoted 8 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach. The documentation of the new logic is in:

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the call last week). Some comments below.

I'm not against this logic, but I'm wondering why it needs to be so complicated.

Can't the domU itself allocate the Xenstore page from its RAM pages, write the PFN into the Xenstore grant tab entry, and then make it public via setting HVM_PARAM_STORE_PFN?

Quoted 6 lines

The init-dom0less application could then check HVM_PARAM_STORE_PFN being set and call XS_introduce_domain.

Note that at least C-xenstored does not need the PFN of the Xenstore page, as it is just using GNTTAB_RESERVED_XENSTORE for mapping the page.

Hmm, seems as if O-xenstored is using the map_foreign_page mechanism with the PFN to map the Xenstore ring page. Maybe this should be changed to use the grant entry, too?

Juergen

#14 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

In reply to Jürgen Groß
Quoted 6 lines · Jürgen Groß
Quoted 5 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach. The documentation of the new logic is in:

Quoted 7 lines
Quoted 4 lines

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the call last week). Some comments below.

I'm not against this logic, but I'm wondering why it needs to be so complicated.

Actually the reason I like it is that in my view, this is the simplest approach. You allocate a domain, you also allocate the xenstore page together with it. Initially the xenstore connection has an "uninitialized" state, as it should be. That's it. At some point, when xenstored is ready, the state changes to CONNECTED.

Quoted 3 lines

Can't the domU itself allocate the Xenstore page from its RAM pages, write the PFN into the Xenstore grant tab entry, and then make it public via setting HVM_PARAM_STORE_PFN?

This is not simpler in my view

Quoted 6 lines

The init-dom0less application could then check HVM_PARAM_STORE_PFN being set and call XS_introduce_domain.

Note that at least C-xenstored does not need the PFN of the Xenstore page, as it is just using GNTTAB_RESERVED_XENSTORE for mapping the page.

#15 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Quoted 22 lines · Stefano Stabellini
Quoted 6 lines · Jürgen Groß
Quoted 5 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach. The documentation of the new logic is in:

Quoted 7 lines
Quoted 4 lines

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the call last week). Some comments below.

I'm not against this logic, but I'm wondering why it needs to be so complicated.

Actually the reason I like it is that in my view, this is the simplest approach. You allocate a domain, you also allocate the xenstore page together with it. Initially the xenstore connection has an "uninitialized" state, as it should be. That's it. At some point, when xenstored is ready, the state changes to CONNECTED.

Quoted 5 lines
Quoted 3 lines

Can't the domU itself allocate the Xenstore page from its RAM pages, write the PFN into the Xenstore grant tab entry, and then make it public via setting HVM_PARAM_STORE_PFN?

This is not simpler in my view

Okay, fine with me. I had the impression that violating the 1:1 mapping of the domain would add complexity, but if you are fine with that I don't mind your approach.

Juergen

#16 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Quoted 29 lines · Jürgen Groß
Quoted 28 lines · Stefano Stabellini
Quoted 7 lines · Jürgen Groß
Quoted 6 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach.
The documentation of the new logic is in:

Quoted 8 lines
Quoted 5 lines

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on
the call last week). Some comments below.

I'm not against this logic, but I'm wondering why it needs to be so complicated.

Actually the reason I like it is that in my view, this is the simplest approach. You allocate a domain, you also allocate the xenstore page together with it. Initially the xenstore connection has an "uninitialized" state, as it should be. That's it. At some point, when xenstored is ready, the state changes to CONNECTED.

Quoted 3 lines

Can't the domU itself allocate the Xenstore page from its RAM pages, write the PFN into the Xenstore grant tab entry, and then make it public via setting HVM_PARAM_STORE_PFN?

Quoted 5 lines
Quoted 1 line

This is not simpler in my view

Okay, fine with me. I had the impression that violating the 1:1 mapping of the domain would add complexity, but if you are fine with that I don't mind your approach.

Yes, that's fine. Thanks!

#17 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

In reply to Julien Grall
Quoted 42 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach. The documentation of the new logic is in:

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the call last week). Some comments below.

Quoted 24 lines · Henry Wang

There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.

An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:

```
Allocating magic pages
memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1
Error on alloc magic pages
```

The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through populate_physmap(), and populate_physmap() automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.

Quoted 6 lines

Since for init-dom0less, the magic page region is only for XenStore. To solve above issue, this commit allocates the XenStore page for Dom0less DomUs at the domain construction time. The PFN will be noted and communicated to the init-dom0less application executed from Dom0. To keep the XenStore late init protocol, set the connection status to XENSTORE_RECONNECT.

Quoted 4 lines

So this commit is allocating the page, but it will not be used by init-dom0less until the next patch. But Linux could use it. So would this break bisection? If so, then I think patch #3 needs to be folded in this patch.

I think that's fine, I'll leave that with you on commit. I'll resend as is addressing the other comments.

Quoted 56 lines
Quoted 41 lines

---
v3:
- Only allocate XenStore page. (Julien)
- Set HVM_PARAM_STORE_PFN and the XenStore connection status directly from hypervisor. (Stefano)
v2:
- Reword the commit msg to explain what is "magic page" and use generic terminology "hypervisor reserved pages" in commit msg. (Daniel)
- Also move the offset definition of magic pages. (Michal)
- Extract the magic page allocation logic to a function. (Michal)

---
  xen/arch/arm/dom0less-build.c | 44 ++++++++++++++++++++++++++++++++++-
  1 file changed, 43 insertions(+), 1 deletion(-)
xen/arch/arm/dom0less-build.c +2 -0
index 74f053c242..95c4fd1a2d 100644
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: GPL-2.0-only */
  #include <xen/device_tree.h>
+#include <xen/domain_page.h>
  #include <xen/err.h>
  #include <xen/event.h>
  #include <xen/grant_table.h>
@@ -10,6 +11,8 @@
  #include <xen/sizes.h>
  #include <xen/vmap.h>
  +#include <public/io/xs_wire.h>
+
  #include <asm/arm64/sve.h>
  #include <asm/dom0less-build.h>
  #include <asm/domain_build.h>
@@ -739,6 +742,42 @@ static int __init alloc_xenstore_evtchn(struct domain
 *d)
      return 0;
  }

xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain · line 754

Quoted 10 lines
xen/arch/arm/dom0less-build.c +9 -0
  +#define XENSTORE_PFN_OFFSET 1
+static int __init alloc_xenstore_page(struct domain *d)
+{
+    struct page_info *xenstore_pg;
+    struct xenstore_domain_interface *interface;
+    mfn_t mfn;
+    gfn_t gfn;
+    int rc;
+
+    d->max_pages += 1;

Sorry I should have spotted it earlier. But you want to check d->max_pages is not overflowing. You can look at acquire_shared_memory_bank() for how to do it.

Quoted 2 lines

Also, maybe we want an helper to do it so it is not open-coded in multiple places.

Makes sense. I open-coded it because I wasn't sure if you preferred a static inline or a macro and where to add the implementation.

Quoted 8 lines

xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 755

Quoted 1 line
xen/arch/arm/dom0less-build.c +1 -0
+    xenstore_pg = alloc_domheap_page(d, 0);

I think we may want to restrict where the page is allocated. For instance, 32-bit domain using short page-tables will not be able to address all the physical memory.

I would consider to try to allocate the page below 32-bit (using MEMF-bits(32). And then fallback to above 32-bit only 64-bit for domain.

done

Quoted 5 lines

Also, just to note that in theory alloc_domheap_page() could return MFN 0. In practice we have excluded MFN 0 because it breaks the page allocator so far.

But I would still prefer if we add a check on the MFN. This will make easier to spot any issue if we ever give MFN 0 to the allocator.

Good idea, but for simplicity I added a check on MFN and if the result is zero return an error. At least we have explicit check.

Quoted 16 lines

A possible implementation would be to call alloc_domphea_page() a second time and then free the first one (e.g. MFN 0).

Quoted 13 lines
xen/arch/arm/dom0less-build.c +13 -0
+    if ( xenstore_pg == NULL )
+        return -ENOMEM;
+
+    mfn = page_to_mfn(xenstore_pg);
+    if ( !is_domain_direct_mapped(d) )
+        gfn = gaddr_to_gfn(GUEST_MAGIC_BASE +
+                           (XENSTORE_PFN_OFFSET << PAGE_SHIFT));
+    else
+        gfn = gaddr_to_gfn(mfn_to_maddr(mfn));
+
+    rc = guest_physmap_add_page(d, gfn, mfn, 0); > +    if ( rc )
+    {
+        free_domheap_page(xenstore_pg);
Quoted 8 lines

xen/arch/arm/dom0less-build.c static int __init alloc_xenstore_evtchn(struct domain *d) · line 774

Quoted 5 lines
xen/arch/arm/dom0less-build.c +5 -0
+        return rc;
+    }
+
+    d->arch.hvm.params[HVM_PARAM_STORE_PFN] = gfn_x(gfn);
+    interface = (struct xenstore_domain_interface *)map_domain_page(mfn);

map_domain_page() will return a void *. So why do you need to an explicit case?

Changed

Quoted 26 lines
Quoted 10 lines
xen/arch/arm/dom0less-build.c +6 -0
+    interface->connection = XENSTORE_RECONNECT;
+    unmap_domain_page(interface);
+
+    return 0;
+}
+
  static int __init construct_domU(struct domain *d,
                                   const struct dt_device_node *node)
  {

xen/arch/arm/dom0less-build.c static int __init construct_domU(struct domain *d, · line 886

Quoted 10 lines
xen/arch/arm/dom0less-build.c +4 -1
@@ -839,7 +878,10 @@ static int __init construct_domU(struct domain *d,
          rc = alloc_xenstore_evtchn(d);
          if ( rc < 0 )
              return rc;
-        d->arch.hvm.params[HVM_PARAM_STORE_PFN] = ~0ULL;
+
+        rc = alloc_xenstore_page(d);
+        if ( rc < 0 )
+            return rc;
      }
        return rc;

Cheers,

Signature · 1 line
Julien Grall
#18 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Hi Stefano,

Quoted 43 lines · Stefano Stabellini
Quoted 42 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach. The documentation of the new logic is in:

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the call last week). Some comments below.

Quoted 30 lines · Henry Wang

There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.

An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:

```
Allocating magic pages
memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1
Error on alloc magic pages
```

The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through populate_physmap(), and populate_physmap() automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.

Since for init-dom0less, the magic page region is only for XenStore. To solve above issue, this commit allocates the XenStore page for Dom0less DomUs at the domain construction time. The PFN will be noted and communicated to the init-dom0less application executed from Dom0. To keep the XenStore late init protocol, set the connection status to XENSTORE_RECONNECT.

Quoted 6 lines
Quoted 4 lines

So this commit is allocating the page, but it will not be used by init-dom0less until the next patch. But Linux could use it. So would this break bisection? If so, then I think patch #3 needs to be folded in this patch.

I think that's fine,

I am not sure what you mean. Are you saying it is ok to break bisection?

Quoted 1 line

I'll leave that with you on commit.

I am sorry but I don't think the folding should be done on commit. It should happen before hand because the commit message will also need to be updated.

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Quoted 48 lines · Julien Grall

Hi Stefano,

Quoted 45 lines · Stefano Stabellini
Quoted 44 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach.
The documentation of the new logic is in:

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the
call last week). Some comments below.

Quoted 30 lines · Henry Wang

There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.

An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:

```
Allocating magic pages
memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1
Error on alloc magic pages
```

The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through populate_physmap(), and populate_physmap() automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.

Since for init-dom0less, the magic page region is only for XenStore. To solve above issue, this commit allocates the XenStore page for Dom0less DomUs at the domain construction time. The PFN will be noted and communicated to the init-dom0less application executed from Dom0. To keep the XenStore late init protocol, set the connection status to XENSTORE_RECONNECT.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

So this commit is allocating the page, but it will not be used by init-dom0less until the next patch. But Linux could use it. So would this break bisection? If so, then I think patch #3 needs to be folded in this patch.

I think that's fine,

I am not sure what you mean. Are you saying it is ok to break bisection?

No, I meant to say that it is fine to merge on commit.

Quoted 4 lines
Quoted 1 line

I'll leave that with you on commit.

I am sorry but I don't think the folding should be done on commit. It should happen before hand because the commit message will also need to be updated.

Understood. I'll send one more version with the patches merged (ideally with an ack?)

#20 lore raw

Re: [PATCH v3 2/4] xen/arm: Alloc XenStore page for Dom0less DomUs from hypervisor

Hi,

Quoted 61 lines · Stefano Stabellini
Quoted 48 lines · Julien Grall

Hi Stefano,

Quoted 45 lines · Stefano Stabellini
Quoted 44 lines · Julien Grall

Hi Henry,

+ Juergen as the Xenstore maintainers. I'd like his opinion on the approach.
The documentation of the new logic is in:

https://lore.kernel.org/xen-devel/[email protected]/

FWIW I am happy in principle with the logic (this is what we discussed on the
call last week). Some comments below.

Quoted 30 lines · Henry Wang

There are use cases (for example using the PV driver) in Dom0less setup that require Dom0less DomUs start immediately with Dom0, but initialize XenStore later after Dom0's successful boot and call to the init-dom0less application.

An error message can seen from the init-dom0less application on 1:1 direct-mapped domains:

```
Allocating magic pages
memory.c:238:d0v0 mfn 0x39000 doesn't belong to d1
Error on alloc magic pages
```

The "magic page" is a terminology used in the toolstack as reserved pages for the VM to have access to virtual platform capabilities. Currently the magic pages for Dom0less DomUs are populated by the init-dom0less app through populate_physmap(), and populate_physmap() automatically assumes gfn == mfn for 1:1 direct mapped domains. This cannot be true for the magic pages that are allocated later from the init-dom0less application executed in Dom0. For domain using statically allocated memory but not 1:1 direct-mapped, similar error "failed to retrieve a reserved page" can be seen as the reserved memory list is empty at that time.

Since for init-dom0less, the magic page region is only for XenStore. To solve above issue, this commit allocates the XenStore page for Dom0less DomUs at the domain construction time. The PFN will be noted and communicated to the init-dom0less application executed from Dom0. To keep the XenStore late init protocol, set the connection status to XENSTORE_RECONNECT.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

So this commit is allocating the page, but it will not be used by init-dom0less until the next patch. But Linux could use it. So would this break bisection? If so, then I think patch #3 needs to be folded in this patch.

I think that's fine,

I am not sure what you mean. Are you saying it is ok to break bisection?

No, I meant to say that it is fine to merge on commit.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I'll leave that with you on commit.

I am sorry but I don't think the folding should be done on commit. It should happen before hand because the commit message will also need to be updated.

Understood. I'll send one more version with the patches merged (ideally with an ack?)

Sorry I don't feel it is right for me to ack this patch with the pending questions from Juergen.

Cheers,

Signature · 1 line
Julien Grall
author
#21 lore raw

PATCH v3 3/4 tools/init-dom0less: Avoid hardcoding GUEST_MAGIC_BASE

In reply to Henry Wang

Currently the GUEST_MAGIC_BASE in the init-dom0less application is hardcoded, which will lead to failures for 1:1 direct-mapped Dom0less DomUs.

Since the guest magic region allocation from init-dom0less is for XenStore, and the XenStore page is now allocated from the hypervisor, instead of hardcoding the guest magic pages region, use xc_hvm_param_get() to get the XenStore page PFN. Rename alloc_xs_page() to get_xs_page() to reflect the changes.

With this change, some existing code is not needed anymore, including:
(1) The definition of the XenStore page offset.
(2) Call to xc_domain_setmaxmem() and xc_clear_domain_page() as we don't need to set the max mem and clear the page anymore.
(3) Foreign mapping of the XenStore page, setting of XenStore interface status and HVM_PARAM_STORE_PFN from init-dom0less, as they are set by the hypervisor.

Take the opportunity to do some coding style improvements when possible.

---
v3:
- Only get the XenStore page.
- Drop the unneeded code.
v2:
- Update HVMOP keys name.

---
 tools/helpers/init-dom0less.c | 58 +++++++++--------------------------
 1 file changed, 14 insertions(+), 44 deletions(-)
tools/helpers/init-dom0less.c +14 -44
index fee93459c4..2b51965fa7 100644
@@ -16,30 +16,18 @@
 
 #include "init-dom-json.h"
 
-#define XENSTORE_PFN_OFFSET 1
 #define STR_MAX_LENGTH 128
 
-static int alloc_xs_page(struct xc_interface_core *xch,
-                         libxl_dominfo *info,
-                         uint64_t *xenstore_pfn)
+static int get_xs_page(struct xc_interface_core *xch, libxl_dominfo *info,
+                       uint64_t *xenstore_pfn)
 {
     int rc;
-    const xen_pfn_t base = GUEST_MAGIC_BASE >> XC_PAGE_SHIFT;
-    xen_pfn_t p2m = (GUEST_MAGIC_BASE >> XC_PAGE_SHIFT) + XENSTORE_PFN_OFFSET;
 
-    rc = xc_domain_setmaxmem(xch, info->domid,
-                             info->max_memkb + (XC_PAGE_SIZE/1024));
-    if (rc < 0)
-        return rc;
-
-    rc = xc_domain_populate_physmap_exact(xch, info->domid, 1, 0, 0, &p2m);
-    if (rc < 0)
-        return rc;
-
-    *xenstore_pfn = base + XENSTORE_PFN_OFFSET;
-    rc = xc_clear_domain_page(xch, info->domid, *xenstore_pfn);
-    if (rc < 0)
-        return rc;
+    rc = xc_hvm_param_get(xch, info->domid, HVM_PARAM_STORE_PFN, xenstore_pfn);
+    if (rc < 0) {
+        printf("Failed to get HVM_PARAM_STORE_PFN\n");
+        return 1;
+    }
 
     return 0;
 }
@@ -100,6 +88,7 @@ static bool do_xs_write_vm(struct xs_handle *xsh, xs_transaction_t t,
  */
 static int create_xenstore(struct xs_handle *xsh,
                            libxl_dominfo *info, libxl_uuid uuid,
+                           uint64_t xenstore_pfn,
                            evtchn_port_t xenstore_port)
 {
     domid_t domid;
@@ -145,8 +134,7 @@ static int create_xenstore(struct xs_handle *xsh,
     rc = snprintf(target_memkb_str, STR_MAX_LENGTH, "%"PRIu64, info->current_memkb);
     if (rc < 0 || rc >= STR_MAX_LENGTH)
         return rc;
-    rc = snprintf(ring_ref_str, STR_MAX_LENGTH, "%lld",
-                  (GUEST_MAGIC_BASE >> XC_PAGE_SHIFT) + XENSTORE_PFN_OFFSET);
+    rc = snprintf(ring_ref_str, STR_MAX_LENGTH, "%"PRIu64, xenstore_pfn);
     if (rc < 0 || rc >= STR_MAX_LENGTH)
         return rc;
     rc = snprintf(xenstore_port_str, STR_MAX_LENGTH, "%u", xenstore_port);
@@ -230,7 +218,6 @@ static int init_domain(struct xs_handle *xsh,
     libxl_uuid uuid;
     uint64_t xenstore_evtchn, xenstore_pfn;
     int rc;
-    struct xenstore_domain_interface *intf;
 
     printf("Init dom0less domain: %u\n", info->domid);
 
@@ -245,20 +232,11 @@ static int init_domain(struct xs_handle *xsh,
     if (!xenstore_evtchn)
         return 0;
 
-    /* Alloc xenstore page */
-    if (alloc_xs_page(xch, info, &xenstore_pfn) != 0) {
-        printf("Error on alloc magic pages\n");
-        return 1;
-    }
-
-    intf = xenforeignmemory_map(xfh, info->domid, PROT_READ | PROT_WRITE, 1,
-                                &xenstore_pfn, NULL);
-    if (!intf) {
-        printf("Error mapping xenstore page\n");
+    /* Get xenstore page */
+    if (get_xs_page(xch, info, &xenstore_pfn) != 0) {
+        printf("Error on getting xenstore page\n");
         return 1;
     }
-    intf->connection = XENSTORE_RECONNECT;
-    xenforeignmemory_unmap(xfh, intf, 1);
 
     rc = xc_dom_gnttab_seed(xch, info->domid, true,
                             (xen_pfn_t)-1, xenstore_pfn, 0, 0);
@@ -272,19 +250,11 @@ static int init_domain(struct xs_handle *xsh,
     if (rc)
         err(1, "gen_stub_json_config");
 
-    /* Now everything is ready: set HVM_PARAM_STORE_PFN */
-    rc = xc_hvm_param_set(xch, info->domid, HVM_PARAM_STORE_PFN,
-                          xenstore_pfn);
-    if (rc < 0)
-        return rc;
-
-    rc = create_xenstore(xsh, info, uuid, xenstore_evtchn);
+    rc = create_xenstore(xsh, info, uuid, xenstore_pfn, xenstore_evtchn);
     if (rc)
         err(1, "writing to xenstore");
 
-    rc = xs_introduce_domain(xsh, info->domid,
-            (GUEST_MAGIC_BASE >> XC_PAGE_SHIFT) + XENSTORE_PFN_OFFSET,
-            xenstore_evtchn);
+    rc = xs_introduce_domain(xsh, info->domid, xenstore_pfn, xenstore_evtchn);
     if (!rc)
         err(1, "xs_introduce_domain");
     return 0;
Signature · 1 line
2.34.1
Reviewed-by on v3 3/4
#22 lore raw

Re: [PATCH v3 3/4] tools/init-dom0less: Avoid hardcoding GUEST_MAGIC_BASE

Quoted 18 lines · Henry Wang

Currently the GUEST_MAGIC_BASE in the init-dom0less application is hardcoded, which will lead to failures for 1:1 direct-mapped Dom0less DomUs.

Since the guest magic region allocation from init-dom0less is for XenStore, and the XenStore page is now allocated from the hypervisor, instead of hardcoding the guest magic pages region, use xc_hvm_param_get() to get the XenStore page PFN. Rename alloc_xs_page() to get_xs_page() to reflect the changes.

With this change, some existing code is not needed anymore, including:
(1) The definition of the XenStore page offset.
(2) Call to xc_domain_setmaxmem() and xc_clear_domain_page() as we don't need to set the max mem and clear the page anymore.
(3) Foreign mapping of the XenStore page, setting of XenStore interface status and HVM_PARAM_STORE_PFN from init-dom0less, as they are set by the hypervisor.

Quoted 4 lines

Take the opportunity to do some coding style improvements when possible.

Thanks,
Jason

Reviewed-by
#23 lore raw

Re: [PATCH v3 3/4] tools/init-dom0less: Avoid hardcoding GUEST_MAGIC_BASE

Quoted 19 lines · Jason Andryuk
Quoted 18 lines · Henry Wang

Currently the GUEST_MAGIC_BASE in the init-dom0less application is hardcoded, which will lead to failures for 1:1 direct-mapped Dom0less DomUs.

Since the guest magic region allocation from init-dom0less is for XenStore, and the XenStore page is now allocated from the hypervisor, instead of hardcoding the guest magic pages region, use xc_hvm_param_get() to get the XenStore page PFN. Rename alloc_xs_page() to get_xs_page() to reflect the changes.

With this change, some existing code is not needed anymore, including:
(1) The definition of the XenStore page offset.
(2) Call to xc_domain_setmaxmem() and xc_clear_domain_page() as we don't need to set the max mem and clear the page anymore.
(3) Foreign mapping of the XenStore page, setting of XenStore interface status and HVM_PARAM_STORE_PFN from init-dom0less, as they are set by the hypervisor.

Quoted 6 lines
Quoted 4 lines

Take the opportunity to do some coding style improvements when possible.

author
#24 lore raw

PATCH v3 4/4 docs/features/dom0less: Update the late XenStore init protocol

In reply to Henry Wang

With the new allocation strategy of Dom0less DomUs XenStore page, update the doc of the late XenStore init protocol accordingly.

---
v3:
- Wording change.
v2:
- New patch.

---
 docs/features/dom0less.pandoc | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
docs/features/dom0less.pandoc +7 -5
index 725afa0558..8b178edee0 100644
@@ -110,9 +110,10 @@ hotplug PV drivers to dom0less guests. E.g. xl network-attach domU.
 The implementation works as follows:
 - Xen allocates the xenstore event channel for each dom0less domU that
   has the "xen,enhanced" property, and sets HVM_PARAM_STORE_EVTCHN
-- Xen does *not* allocate the xenstore page and sets HVM_PARAM_STORE_PFN
-  to ~0ULL (invalid)
-- Dom0less domU kernels check that HVM_PARAM_STORE_PFN is set to invalid
+- Xen allocates the xenstore page and sets HVM_PARAM_STORE_PFN as well
+  as the connection status to XENSTORE_RECONNECT.
+- Dom0less domU kernels check that HVM_PARAM_STORE_PFN is set to
+  ~0ULL (invalid) or the connection status is *not* XENSTORE_CONNECTED.
     - Old kernels will continue without xenstore support (Note: some old
       buggy kernels might crash because they don't check the validity of
       HVM_PARAM_STORE_PFN before using it! Disable "xen,enhanced" in
@@ -121,13 +122,14 @@ The implementation works as follows:
       channel (HVM_PARAM_STORE_EVTCHN) before continuing with the
       initialization
 - Once dom0 is booted, init-dom0less is executed:
-    - it allocates the xenstore shared page and sets HVM_PARAM_STORE_PFN
+    - it gets the xenstore shared page from HVM_PARAM_STORE_PFN
     - it calls xs_introduce_domain
 - Xenstored notices the new domain, initializes interfaces as usual, and
   sends an event channel notification to the domain using the xenstore
   event channel (HVM_PARAM_STORE_EVTCHN)
 - The Linux domU kernel receives the event channel notification, checks
-  HVM_PARAM_STORE_PFN again and continue with the initialization
+  HVM_PARAM_STORE_PFN and the connection status again and continue with
+  the initialization
 
 
 Limitations
Signature · 1 line
2.34.1
Reviewed-by on v3 4/4
#25 lore raw

Re: [PATCH v3 4/4] docs/features/dom0less: Update the late XenStore init protocol

Quoted 4 lines · Henry Wang

With the new allocation strategy of Dom0less DomUs XenStore page, update the doc of the late XenStore init protocol accordingly.

Quoted 48 lines

---
v3:
- Wording change.
v2:
- New patch.

---
 docs/features/dom0less.pandoc | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
docs/features/dom0less.pandoc +7 -5
index 725afa0558..8b178edee0 100644
@@ -110,9 +110,10 @@ hotplug PV drivers to dom0less guests. E.g. xl network-attach domU.
 The implementation works as follows:
 - Xen allocates the xenstore event channel for each dom0less domU that
   has the "xen,enhanced" property, and sets HVM_PARAM_STORE_EVTCHN
-- Xen does *not* allocate the xenstore page and sets HVM_PARAM_STORE_PFN
-  to ~0ULL (invalid)
-- Dom0less domU kernels check that HVM_PARAM_STORE_PFN is set to invalid
+- Xen allocates the xenstore page and sets HVM_PARAM_STORE_PFN as well
+  as the connection status to XENSTORE_RECONNECT.
+- Dom0less domU kernels check that HVM_PARAM_STORE_PFN is set to
+  ~0ULL (invalid) or the connection status is *not* XENSTORE_CONNECTED.
     - Old kernels will continue without xenstore support (Note: some old
       buggy kernels might crash because they don't check the validity of
       HVM_PARAM_STORE_PFN before using it! Disable "xen,enhanced" in
@@ -121,13 +122,14 @@ The implementation works as follows:
       channel (HVM_PARAM_STORE_EVTCHN) before continuing with the
       initialization
 - Once dom0 is booted, init-dom0less is executed:
-    - it allocates the xenstore shared page and sets HVM_PARAM_STORE_PFN
+    - it gets the xenstore shared page from HVM_PARAM_STORE_PFN
     - it calls xs_introduce_domain
 - Xenstored notices the new domain, initializes interfaces as usual, and
   sends an event channel notification to the domain using the xenstore
   event channel (HVM_PARAM_STORE_EVTCHN)
 - The Linux domU kernel receives the event channel notification, checks
-  HVM_PARAM_STORE_PFN again and continue with the initialization
+  HVM_PARAM_STORE_PFN and the connection status again and continue with
+  the initialization
 
 
 Limitations
Signature · 1 line
2.34.1