Skip to contentSkip to conversation

Move alloc/free_vcpu_struct() to common code

PATCH Superseded Oleksii Kurochko 5 participants 20 messages Dec 18, 2025 – Dec 19, 2025 RISC-V

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover Move alloc/free_vcpu_struct() to common code

As it was suggested in [1] it would be better to allocate one page for struct vcpu for all arch-es. To do that it is needed to align Arm code to allocate one page (as there is a case(when CONFIG_NEW_VGIC=y) when Arm64 will require to allocate two pages). As a result, the following patches for Arm have been introduced:
- [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu
- [PATCH v2 2/4] xen/arm: drop MAX_PAGES_PER_VCPU

This patches are dependency for:
- [PATCH v2 3/4] xen: move alloc/free_vcpu_struct() to common code

Also, as a part of this patch series another clean up is done which makes {alloc,free}_domain_struct() static.

[1] https://lore.kernel.org/xen-devel/[email protected]/T/#m275dfcbdccef0461fa9a8acef072403f18091768

---
Changes in v2:
- Introduce new patches for Arm:
- [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu
- [PATCH v2 2/4] xen/arm: drop MAX_PAGES_PER_VCPU
to allocate one page for struct vcpu in common code for all the arch-es.
- Introduce patch to clean up xen/domain.h a little bit:
- [PATCH v2 4/4] xen/common: make {alloc,free}_domain_struct() static
- Address the comments from v1:
- [PATCH v2 3/4] xen: move alloc/free_vcpu_struct() to common code
---

Oleksii Kurochko (4):
xen/arm: optimize the size of struct vcpu xen/arm: drop MAX_PAGES_PER_VCPU
xen: move alloc/free_vcpu_struct() to common code xen/common: make {alloc,free}_domain_struct() static

 xen/arch/arm/domain.c             | 32 --------------
 xen/arch/arm/gic-vgic.c           | 48 ++++++++++-----------
 xen/arch/arm/include/asm/domain.h |  2 +-
 xen/arch/arm/vgic-v3.c            | 34 +++++++--------
 xen/arch/arm/vgic.c               | 72 +++++++++++++++++--------------
 xen/arch/arm/vgic/vgic-init.c     | 10 ++++-
 xen/arch/arm/vgic/vgic-v2.c       |  4 +-
 xen/arch/arm/vgic/vgic.c          | 50 ++++++++++-----------
 xen/arch/ppc/stubs.c              | 10 -----
 xen/arch/riscv/stubs.c            | 10 -----
 xen/arch/x86/domain.c             | 17 +-------
 xen/arch/x86/include/asm/domain.h |  3 ++
 xen/common/domain.c               | 26 ++++++++++-
 xen/include/xen/domain.h          |  8 ----
 14 files changed, 145 insertions(+), 181 deletions(-)
Signature · 1 line
2.52.0
#14 lore raw

PATCH v2 3/4 xen: move alloc/free_vcpu_struct() to common code

alloc_vcpu_struct() and free_vcpu_struct() contain little architecture-specific logic and are suitable for sharing across architectures. Move both helpers to common code.

To support the remaining architectural differences, introduce arch_vcpu_struct_memflags(), allowing architectures to override the memory flags passed to alloc_xenheap_pages(). This is currently needed by x86, which may require MEMF_bits(32) for HVM guests using shadow paging.

The ARM implementation of alloc/free_vcpu_struct() is removed and replaced by the common version. Stub implementations are also dropped from PPC and RISC-V.

Finally, make alloc_vcpu_struct() and free_vcpu_struct() static to common/domain.c, as they are no longer used outside common code.

No functional changes.

---
Changes in v2:
- Rework alloc/free_vcpu_struct() to work with only one page.
- Return back the comment about the restriction inside x86's arch_vcpu_struct_memflags().
- Drop MAX_PAGES_PER_VCPU.

---
 xen/arch/arm/domain.c             | 17 -----------------
 xen/arch/ppc/stubs.c              | 10 ----------
 xen/arch/riscv/stubs.c            | 10 ----------
 xen/arch/x86/domain.c             | 17 ++---------------
 xen/arch/x86/include/asm/domain.h |  3 +++
 xen/common/domain.c               | 20 ++++++++++++++++++++
 xen/include/xen/domain.h          |  4 ----
 7 files changed, 25 insertions(+), 56 deletions(-)
xen/arch/arm/domain.c +0 -17
index e566023340..507df807ed 100644
@@ -473,23 +473,6 @@ void dump_pageframe_info(struct domain *d)
 
 }
 
-struct vcpu *alloc_vcpu_struct(const struct domain *d)
-{
-    struct vcpu *v;
-
-    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
-    v = alloc_xenheap_pages(0, 0);
-    if ( v )
-        clear_page(v);
-
-    return v;
-}
-
-void free_vcpu_struct(struct vcpu *v)
-{
-    free_xenheap_page(v);
-}
-
 int arch_vcpu_create(struct vcpu *v)
 {
     int rc = 0;
xen/arch/ppc/stubs.c +0 -10
index 9953ea1c6c..f7f6e7ed97 100644
@@ -152,11 +152,6 @@ void dump_pageframe_info(struct domain *d)
     BUG_ON("unimplemented");
 }
 
-void free_vcpu_struct(struct vcpu *v)
-{
-    BUG_ON("unimplemented");
-}
-
 int arch_vcpu_create(struct vcpu *v)
 {
     BUG_ON("unimplemented");
@@ -264,11 +259,6 @@ void vcpu_kick(struct vcpu *v)
     BUG_ON("unimplemented");
 }
 
-struct vcpu *alloc_vcpu_struct(const struct domain *d)
-{
-    BUG_ON("unimplemented");
-}
-
 unsigned long
 hypercall_create_continuation(unsigned int op, const char *format, ...)
 {
xen/arch/riscv/stubs.c +0 -10
index fe7d85ee1d..579c4215c8 100644
@@ -126,11 +126,6 @@ void dump_pageframe_info(struct domain *d)
     BUG_ON("unimplemented");
 }
 
-void free_vcpu_struct(struct vcpu *v)
-{
-    BUG_ON("unimplemented");
-}
-
 int arch_vcpu_create(struct vcpu *v)
 {
     BUG_ON("unimplemented");
@@ -238,11 +233,6 @@ void vcpu_kick(struct vcpu *v)
     BUG_ON("unimplemented");
 }
 
-struct vcpu *alloc_vcpu_struct(const struct domain *d)
-{
-    BUG_ON("unimplemented");
-}
-
 unsigned long
 hypercall_create_continuation(unsigned int op, const char *format, ...)
 {
xen/arch/x86/domain.c +2 -15
index 7632d5e2d6..68c7503eda 100644
@@ -493,28 +493,15 @@ unsigned int arch_domain_struct_memflags(void)
     return MEMF_bits(bits);
 }
 
-struct vcpu *alloc_vcpu_struct(const struct domain *d)
+unsigned int arch_vcpu_struct_memflags(const struct domain *d)
 {
-    struct vcpu *v;
     /*
      * This structure contains embedded PAE PDPTEs, used when an HVM guest
      * runs on shadow pagetables outside of 64-bit mode. In this case the CPU
      * may require that the shadow CR3 points below 4GB, and hence the whole
      * structure must satisfy this restriction. Thus we specify MEMF_bits(32).
      */
-    unsigned int memflags =
-        (is_hvm_domain(d) && paging_mode_shadow(d)) ? MEMF_bits(32) : 0;
-
-    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
-    v = alloc_xenheap_pages(0, memflags);
-    if ( v != NULL )
-        clear_page(v);
-    return v;
-}
-
-void free_vcpu_struct(struct vcpu *v)
-{
-    free_xenheap_page(v);
+    return (is_hvm_domain(d) && paging_mode_shadow(d)) ? MEMF_bits(32) : 0;
 }
 
 /* Initialise various registers to their architectural INIT/RESET state. */
xen/arch/x86/include/asm/domain.h +3 -0
index 7e5cbd11a4..576f9202b4 100644
@@ -15,6 +15,9 @@
 unsigned int arch_domain_struct_memflags(void);
 #define arch_domain_struct_memflags arch_domain_struct_memflags
 
+unsigned int arch_vcpu_struct_memflags(const struct domain *d);
+#define arch_vcpu_struct_memflags arch_vcpu_struct_memflags
+
 #define has_32bit_shinfo(d)    ((d)->arch.has_32bit_shinfo)
 
 /*
xen/common/domain.c +20 -0
index 93c71bc766..92fc0684fc 100644
@@ -392,6 +392,26 @@ static int vcpu_teardown(struct vcpu *v)
     return 0;
 }
 
+static struct vcpu *alloc_vcpu_struct(const struct domain *d)
+{
+#ifndef arch_vcpu_struct_memflags
+# define arch_vcpu_struct_memflags(d) 0
+#endif
+    struct vcpu *v;
+
+    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
+    v = alloc_xenheap_pages(0, arch_vcpu_struct_memflags(d));
+    if ( v )
+        clear_page(v);
+
+    return v;
+}
+
+static void free_vcpu_struct(struct vcpu *v)
+{
+    free_xenheap_page(v);
+}
+
 /*
  * Destoy a vcpu once all references to it have been dropped.  Used either
  * from domain_destroy()'s RCU path, or from the vcpu_create() error path
xen/include/xen/domain.h +0 -4
index 8aab05ae93..644f5ac3f2 100644
@@ -70,10 +70,6 @@ void domid_free(domid_t domid);
 struct domain *alloc_domain_struct(void);
 void free_domain_struct(struct domain *d);
 
-/* Allocate/free a VCPU structure. */
-struct vcpu *alloc_vcpu_struct(const struct domain *d);
-void free_vcpu_struct(struct vcpu *v);
-
 /* Allocate/free a PIRQ structure. */
 #ifndef alloc_pirq_struct
 struct pirq *alloc_pirq_struct(struct domain *d);
Signature · 1 line
2.52.0
#10 lore raw

PATCH v2 2/4 xen/arm: drop MAX_PAGES_PER_VCPU

In reply to Oleksii Kurochko

Now that the size of struct vcpu is smaller than PAGE_SIZE, MAX_PAGES_PER_VCPU is no longer needed and is removed.

This change also updates {alloc,free}_vcpu_struct().

---
Changes in v2:
- New patch.

---
 xen/arch/arm/domain.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)
xen/arch/arm/domain.c +5 -20
index 47973f99d9..e566023340 100644
@@ -473,36 +473,21 @@ void dump_pageframe_info(struct domain *d)
 
 }
 
-/*
- * The new VGIC has a bigger per-IRQ structure, so we need more than one
- * page on ARM64. Cowardly increase the limit in this case.
- */
-#if defined(CONFIG_NEW_VGIC) && defined(CONFIG_ARM_64)
-#define MAX_PAGES_PER_VCPU  2
-#else
-#define MAX_PAGES_PER_VCPU  1
-#endif
-
 struct vcpu *alloc_vcpu_struct(const struct domain *d)
 {
     struct vcpu *v;
 
-    BUILD_BUG_ON(sizeof(*v) > MAX_PAGES_PER_VCPU * PAGE_SIZE);
-    v = alloc_xenheap_pages(get_order_from_bytes(sizeof(*v)), 0);
-    if ( v != NULL )
-    {
-        unsigned int i;
-
-        for ( i = 0; i < DIV_ROUND_UP(sizeof(*v), PAGE_SIZE); i++ )
-            clear_page((void *)v + i * PAGE_SIZE);
-    }
+    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
+    v = alloc_xenheap_pages(0, 0);
+    if ( v )
+        clear_page(v);
 
     return v;
 }
 
 void free_vcpu_struct(struct vcpu *v)
 {
-    free_xenheap_pages(v, get_order_from_bytes(sizeof(*v)));
+    free_xenheap_page(v);
 }
 
 int arch_vcpu_create(struct vcpu *v)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/4 xen/arm: optimize the size of struct vcpu

In reply to Oleksii Kurochko

When CONFIG_NEW_VGIC=y and CONFIG_ARM_64=y, the size of struct vcpu exceeds one page, which requires allocating two pages and led to the introduction of MAX_PAGES_PER_VCPU.

To remove the need for MAX_PAGES_PER_VCPU in a follow-up patch, the vgic member of struct arch_vcpu is changed to a pointer to struct vgic_cpu. As a result, the size of struct vcpu for Arm64 is reduced to 2048 bytes, compared to 3840 bytes (without these changes and with CONFIG_ARM_64=y) and 4736 bytes (without these changes and with both CONFIG_ARM_64=y and CONFIG_NEW_VGIC=y).

Since the vgic member is now a pointer, vcpu_vgic_init() and vcpu_vgic_free() are updated to allocate and free the struct vgic_cpu instance dynamically.

---
Changes in v2:
- New patch.

---
 xen/arch/arm/gic-vgic.c           | 48 ++++++++++-----------
 xen/arch/arm/include/asm/domain.h |  2 +-
 xen/arch/arm/vgic-v3.c            | 34 +++++++--------
 xen/arch/arm/vgic.c               | 72 +++++++++++++++++--------------
 xen/arch/arm/vgic/vgic-init.c     | 10 ++++-
 xen/arch/arm/vgic/vgic-v2.c       |  4 +-
 xen/arch/arm/vgic/vgic.c          | 50 ++++++++++-----------
 7 files changed, 116 insertions(+), 104 deletions(-)
xen/arch/arm/gic-vgic.c +24 -24
index ea48c5375a..482b77c986 100644
@@ -42,12 +42,12 @@ static inline void gic_add_to_lr_pending(struct vcpu *v, struct pending_irq *n)
 {
     struct pending_irq *iter;
 
-    ASSERT(spin_is_locked(&v->arch.vgic.lock));
+    ASSERT(spin_is_locked(&v->arch.vgic->lock));
 
     if ( !list_empty(&n->lr_queue) )
         return;
 
-    list_for_each_entry ( iter, &v->arch.vgic.lr_pending, lr_queue )
+    list_for_each_entry ( iter, &v->arch.vgic->lr_pending, lr_queue )
     {
         if ( iter->priority > n->priority )
         {
@@ -55,12 +55,12 @@ static inline void gic_add_to_lr_pending(struct vcpu *v, struct pending_irq *n)
             return;
         }
     }
-    list_add_tail(&n->lr_queue, &v->arch.vgic.lr_pending);
+    list_add_tail(&n->lr_queue, &v->arch.vgic->lr_pending);
 }
 
 void gic_remove_from_lr_pending(struct vcpu *v, struct pending_irq *p)
 {
-    ASSERT(spin_is_locked(&v->arch.vgic.lock));
+    ASSERT(spin_is_locked(&v->arch.vgic->lock));
 
     list_del_init(&p->lr_queue);
 }
@@ -73,7 +73,7 @@ void gic_raise_inflight_irq(struct vcpu *v, unsigned int virtual_irq)
     if ( unlikely(!n) )
         return;
 
-    ASSERT(spin_is_locked(&v->arch.vgic.lock));
+    ASSERT(spin_is_locked(&v->arch.vgic->lock));
 
     /* Don't try to update the LR if the interrupt is disabled */
     if ( !test_bit(GIC_IRQ_GUEST_ENABLED, &n->status) )
@@ -104,7 +104,7 @@ static unsigned int gic_find_unused_lr(struct vcpu *v,
 {
     uint64_t *lr_mask = &this_cpu(lr_mask);
 
-    ASSERT(spin_is_locked(&v->arch.vgic.lock));
+    ASSERT(spin_is_locked(&v->arch.vgic->lock));
 
     if ( unlikely(test_bit(GIC_IRQ_GUEST_PRISTINE_LPI, &p->status)) )
     {
@@ -130,13 +130,13 @@ void gic_raise_guest_irq(struct vcpu *v, unsigned int virtual_irq,
     unsigned int nr_lrs = gic_get_nr_lrs();
     struct pending_irq *p = irq_to_pending(v, virtual_irq);
 
-    ASSERT(spin_is_locked(&v->arch.vgic.lock));
+    ASSERT(spin_is_locked(&v->arch.vgic->lock));
 
     if ( unlikely(!p) )
         /* An unmapped LPI does not need to be raised. */
         return;
 
-    if ( v == current && list_empty(&v->arch.vgic.lr_pending) )
+    if ( v == current && list_empty(&v->arch.vgic->lr_pending) )
     {
         i = gic_find_unused_lr(v, p, 0);
 
@@ -156,7 +156,7 @@ static void gic_update_one_lr(struct vcpu *v, int i)
     int irq;
     struct gic_lr lr_val;
 
-    ASSERT(spin_is_locked(&v->arch.vgic.lock));
+    ASSERT(spin_is_locked(&v->arch.vgic->lock));
     ASSERT(!local_irq_is_enabled());
 
     gic_hw_ops->read_lr(i, &lr_val);
@@ -253,7 +253,7 @@ void vgic_sync_from_lrs(struct vcpu *v)
 
     gic_hw_ops->update_hcr_status(GICH_HCR_UIE, false);
 
-    spin_lock_irqsave(&v->arch.vgic.lock, flags);
+    spin_lock_irqsave(&v->arch.vgic->lock, flags);
 
     while ((i = find_next_bit((const unsigned long *) &this_cpu(lr_mask),
                               nr_lrs, i)) < nr_lrs ) {
@@ -261,7 +261,7 @@ void vgic_sync_from_lrs(struct vcpu *v)
         i++;
     }
 
-    spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+    spin_unlock_irqrestore(&v->arch.vgic->lock, flags);
 }
 
 static void gic_restore_pending_irqs(struct vcpu *v)
@@ -274,13 +274,13 @@ static void gic_restore_pending_irqs(struct vcpu *v)
 
     ASSERT(!local_irq_is_enabled());
 
-    spin_lock(&v->arch.vgic.lock);
+    spin_lock(&v->arch.vgic->lock);
 
-    if ( list_empty(&v->arch.vgic.lr_pending) )
+    if ( list_empty(&v->arch.vgic->lr_pending) )
         goto out;
 
-    inflight_r = &v->arch.vgic.inflight_irqs;
-    list_for_each_entry_safe ( p, t, &v->arch.vgic.lr_pending, lr_queue )
+    inflight_r = &v->arch.vgic->inflight_irqs;
+    list_for_each_entry_safe ( p, t, &v->arch.vgic->lr_pending, lr_queue )
     {
         lr = gic_find_unused_lr(v, p, lr);
         if ( lr >= nr_lrs )
@@ -318,17 +318,17 @@ found:
     }
 
 out:
-    spin_unlock(&v->arch.vgic.lock);
+    spin_unlock(&v->arch.vgic->lock);
 }
 
 void gic_clear_pending_irqs(struct vcpu *v)
 {
     struct pending_irq *p, *t;
 
-    ASSERT(spin_is_locked(&v->arch.vgic.lock));
+    ASSERT(spin_is_locked(&v->arch.vgic->lock));
 
     v->arch.lr_mask = 0;
-    list_for_each_entry_safe ( p, t, &v->arch.vgic.lr_pending, lr_queue )
+    list_for_each_entry_safe ( p, t, &v->arch.vgic->lr_pending, lr_queue )
         gic_remove_from_lr_pending(v, p);
 }
 
@@ -357,14 +357,14 @@ int vgic_vcpu_pending_irq(struct vcpu *v)
     mask_priority = gic_hw_ops->read_vmcr_priority();
     active_priority = find_first_bit(&apr, 32);
 
-    spin_lock_irqsave(&v->arch.vgic.lock, flags);
+    spin_lock_irqsave(&v->arch.vgic->lock, flags);
 
     /* TODO: We order the guest irqs by priority, but we don't change
      * the priority of host irqs. */
 
     /* find the first enabled non-active irq, the queue is already
      * ordered by priority */
-    list_for_each_entry( p, &v->arch.vgic.inflight_irqs, inflight )
+    list_for_each_entry( p, &v->arch.vgic->inflight_irqs, inflight )
     {
         if ( GIC_PRI_TO_GUEST(p->priority) >= mask_priority )
             goto out;
@@ -378,7 +378,7 @@ int vgic_vcpu_pending_irq(struct vcpu *v)
     }
 
 out:
-    spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+    spin_unlock_irqrestore(&v->arch.vgic->lock, flags);
     return rc;
 }
 
@@ -388,7 +388,7 @@ void vgic_sync_to_lrs(void)
 
     gic_restore_pending_irqs(current);
 
-    if ( !list_empty(&current->arch.vgic.lr_pending) && lr_all_full() )
+    if ( !list_empty(&current->arch.vgic->lr_pending) && lr_all_full() )
         gic_hw_ops->update_hcr_status(GICH_HCR_UIE, true);
 }
 
@@ -396,10 +396,10 @@ void gic_dump_vgic_info(struct vcpu *v)
 {
     struct pending_irq *p;
 
-    list_for_each_entry ( p, &v->arch.vgic.inflight_irqs, inflight )
+    list_for_each_entry ( p, &v->arch.vgic->inflight_irqs, inflight )
         printk("Inflight irq=%u lr=%u\n", p->irq, p->lr);
 
-    list_for_each_entry( p, &v->arch.vgic.lr_pending, lr_queue )
+    list_for_each_entry( p, &v->arch.vgic->lr_pending, lr_queue )
         printk("Pending irq=%d\n", p->irq);
 }
 
xen/arch/arm/include/asm/domain.h +1 -1
index 758ad807e4..6cfa793828 100644
@@ -230,7 +230,7 @@ struct arch_vcpu
     union gic_state_data gic;
     uint64_t lr_mask;
 
-    struct vgic_cpu vgic;
+    struct vgic_cpu *vgic;
 
     /* Timer registers  */
     register_t cntkctl;
xen/arch/arm/vgic-v3.c +17 -17
index 77aab5c3c2..a9bb7e8906 100644
@@ -162,10 +162,10 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
             goto read_as_zero_32;
         if ( dabt.size != DABT_WORD ) goto bad_width;
 
-        spin_lock_irqsave(&v->arch.vgic.lock, flags);
-        *r = vreg_reg32_extract(!!(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED),
+        spin_lock_irqsave(&v->arch.vgic->lock, flags);
+        *r = vreg_reg32_extract(!!(v->arch.vgic->flags & VGIC_V3_LPIS_ENABLED),
                                 info);
-        spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+        spin_unlock_irqrestore(&v->arch.vgic->lock, flags);
         return 1;
     }
 
@@ -195,7 +195,7 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
         /* We use the VCPU ID as the redistributor ID in bits[23:8] */
         typer |= v->vcpu_id << GICR_TYPER_PROC_NUM_SHIFT;
 
-        if ( v->arch.vgic.flags & VGIC_V3_RDIST_LAST )
+        if ( v->arch.vgic->flags & VGIC_V3_RDIST_LAST )
             typer |= GICR_TYPER_LAST;
 
         if ( v->domain->arch.vgic.has_its )
@@ -249,7 +249,7 @@ static int __vgic_v3_rdistr_rd_mmio_read(struct vcpu *v, mmio_info_t *info,
             goto read_as_zero_64;
         if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
 
-        val = read_atomic(&v->arch.vgic.rdist_pendbase);
+        val = read_atomic(&v->arch.vgic->rdist_pendbase);
         val &= ~GICR_PENDBASER_PTZ;      /* WO, reads as 0 */
         *r = vreg_reg64_extract(val, info);
         return 1;
@@ -467,7 +467,7 @@ static void vgic_vcpu_enable_lpis(struct vcpu *v)
         smp_mb();
     }
 
-    v->arch.vgic.flags |= VGIC_V3_LPIS_ENABLED;
+    v->arch.vgic->flags |= VGIC_V3_LPIS_ENABLED;
 }
 
 static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
@@ -488,14 +488,14 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
         if ( dabt.size != DABT_WORD ) goto bad_width;
 
         vgic_lock(v);                   /* protects rdists_enabled */
-        spin_lock_irqsave(&v->arch.vgic.lock, flags);
+        spin_lock_irqsave(&v->arch.vgic->lock, flags);
 
         /* LPIs can only be enabled once, but never disabled again. */
         if ( (r & GICR_CTLR_ENABLE_LPIS) &&
-             !(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED) )
+             !(v->arch.vgic->flags & VGIC_V3_LPIS_ENABLED) )
             vgic_vcpu_enable_lpis(v);
 
-        spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+        spin_unlock_irqrestore(&v->arch.vgic->lock, flags);
         vgic_unlock(v);
 
         return 1;
@@ -565,18 +565,18 @@ static int __vgic_v3_rdistr_rd_mmio_write(struct vcpu *v, mmio_info_t *info,
             goto write_ignore_64;
         if ( !vgic_reg64_check_access(dabt) ) goto bad_width;
 
-        spin_lock_irqsave(&v->arch.vgic.lock, flags);
+        spin_lock_irqsave(&v->arch.vgic->lock, flags);
 
         /* Writing PENDBASER with LPIs enabled is UNPREDICTABLE. */
-        if ( !(v->arch.vgic.flags & VGIC_V3_LPIS_ENABLED) )
+        if ( !(v->arch.vgic->flags & VGIC_V3_LPIS_ENABLED) )
         {
-            reg = read_atomic(&v->arch.vgic.rdist_pendbase);
+            reg = read_atomic(&v->arch.vgic->rdist_pendbase);
             vreg_reg64_update(&reg, r, info);
             reg = sanitize_pendbaser(reg);
-            write_atomic(&v->arch.vgic.rdist_pendbase, reg);
+            write_atomic(&v->arch.vgic->rdist_pendbase, reg);
         }
 
-        spin_unlock_irqrestore(&v->arch.vgic.lock, flags);
+        spin_unlock_irqrestore(&v->arch.vgic->lock, flags);
 
         return 1;
     }
@@ -1115,7 +1115,7 @@ static struct vcpu *get_vcpu_from_rdist(struct domain *d,
 
     v = d->vcpu[vcpu_id];
 
-    *offset = gpa - v->arch.vgic.rdist_base;
+    *offset = gpa - v->arch.vgic->rdist_base;
 
     return v;
 }
@@ -1745,7 +1745,7 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
         return -EINVAL;
     }
 
-    v->arch.vgic.rdist_base = rdist_base;
+    v->arch.vgic->rdist_base = rdist_base;
 
     /*
      * If the redistributor is the last one of the
@@ -1756,7 +1756,7 @@ static int vgic_v3_vcpu_init(struct vcpu *v)
     last_cpu = (region->size / GICV3_GICR_SIZE) + region->first_cpu - 1;
 
     if ( v->vcpu_id == last_cpu || (v->vcpu_id == (d->max_vcpus - 1)) )
-        v->arch.vgic.flags |= VGIC_V3_RDIST_LAST;
+        v->arch.vgic->flags |= VGIC_V3_RDIST_LAST;
 
     return 0;
 }
xen/arch/arm/vgic.c +39 -33

214 lines not on this page · raw message

xen/arch/arm/vgic/vgic-init.c +8 -2
index f8d7d3a226..67f297797f 100644
@@ -57,7 +57,7 @@
  */
 static void vgic_vcpu_early_init(struct vcpu *vcpu)
 {
-    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic;
+    struct vgic_cpu *vgic_cpu = vcpu->arch.vgic;
     unsigned int i;
 
     INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
@@ -202,6 +202,10 @@ int vcpu_vgic_init(struct vcpu *v)
 {
     int ret = 0;
 
+    v->arch.vgic = xzalloc(struct vgic_cpu);
+    if ( v->arch.vgic == NULL )
+        return -ENOMEM;
+
     vgic_vcpu_early_init(v);
 
     if ( gic_hw_version() == GIC_V2 )
@@ -241,10 +245,12 @@ void domain_vgic_free(struct domain *d)
 
 int vcpu_vgic_free(struct vcpu *v)
 {
-    struct vgic_cpu *vgic_cpu = &v->arch.vgic;
+    struct vgic_cpu *vgic_cpu = v->arch.vgic;
 
     INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
 
+    xfree(vgic_cpu);
+
     return 0;
 }
 
xen/arch/arm/vgic/vgic-v2.c +2 -2
index 6a558089c5..e64d681dd2 100644
@@ -56,8 +56,8 @@ void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
  */
 void vgic_v2_fold_lr_state(struct vcpu *vcpu)
 {
-    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic;
-    unsigned int used_lrs = vcpu->arch.vgic.used_lrs;
+    struct vgic_cpu *vgic_cpu = vcpu->arch.vgic;
+    unsigned int used_lrs = vcpu->arch.vgic->used_lrs;
     unsigned long flags;
     unsigned int lr;
 
xen/arch/arm/vgic/vgic.c +25 -25

169 lines not on this page · raw message

Signature · 1 line
2.52.0
#17 lore raw

PATCH v2 4/4 xen/common: make {alloc,free}_domain_struct() static

In reply to Oleksii Kurochko

As {alloc,free}_domain_struct() are used only within domain.c, they can be declared static and their declarations removed from xen/domain.h.

---
Changes in v2:
- New patch.

---
 xen/common/domain.c      | 6 ++++--
 xen/include/xen/domain.h | 4 ----
 2 files changed, 4 insertions(+), 6 deletions(-)
xen/common/domain.c +4 -2
index 92fc0684fc..7509dafd6f 100644
@@ -690,6 +690,8 @@ static int domain_teardown(struct domain *d)
     return 0;
 }
 
+static void free_domain_struct(struct domain *d);
+
 /*
  * Destroy a domain once all references to it have been dropped.  Used either
  * from the RCU path, or from the domain_create() error path before the domain
@@ -819,7 +821,7 @@ static int sanitise_domain_config(struct xen_domctl_createdomain *config)
     return arch_sanitise_domain_config(config);
 }
 
-struct domain *alloc_domain_struct(void)
+static struct domain *alloc_domain_struct(void)
 {
 #ifndef arch_domain_struct_memflags
 # define arch_domain_struct_memflags() 0
@@ -835,7 +837,7 @@ struct domain *alloc_domain_struct(void)
     return d;
 }
 
-void free_domain_struct(struct domain *d)
+static void free_domain_struct(struct domain *d)
 {
     free_xenheap_page(d);
 }
xen/include/xen/domain.h +0 -4
index 644f5ac3f2..273717c31b 100644
@@ -66,10 +66,6 @@ void domid_free(domid_t domid);
  * Arch-specifics.
  */
 
-/* Allocate/free a domain structure. */
-struct domain *alloc_domain_struct(void);
-void free_domain_struct(struct domain *d);
-
 /* Allocate/free a PIRQ structure. */
 #ifndef alloc_pirq_struct
 struct pirq *alloc_pirq_struct(struct domain *d);
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

In reply to Oleksii Kurochko
Quoted 7 lines · Oleksii Kurochko
xen/arch/arm/vgic.c +0 -0
index 3ebdf9953f..8b17871b86 100644
@@ -370,29 +370,35 @@ int vcpu_vgic_init(struct vcpu *v)
 {
     int i;

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 379

Quoted 10 lines
xen/arch/arm/vgic.c +6 -2
 
-    v->arch.vgic.private_irqs = xzalloc(struct vgic_irq_rank);
-    if ( v->arch.vgic.private_irqs == NULL )
+    v->arch.vgic = xzalloc(struct vgic_cpu);
+    if ( v->arch.vgic == NULL )
+        return -ENOMEM;
+
+    v->arch.vgic->private_irqs = xzalloc(struct vgic_irq_rank);
+    if ( v->arch.vgic->private_irqs == NULL )
       return -ENOMEM;

This error path needs to free v->arch.vgic.  (If we continue down this route.  See below.)

Quoted 21 lines
xen/arch/arm/vgic.c +6 -6
     /* SGIs/PPIs are always routed to this VCPU */
-    vgic_rank_init(v->arch.vgic.private_irqs, 0, v->vcpu_id);
+    vgic_rank_init(v->arch.vgic->private_irqs, 0, v->vcpu_id);
 
     v->domain->arch.vgic.handler->vcpu_init(v);
 
-    memset(&v->arch.vgic.pending_irqs, 0, sizeof(v->arch.vgic.pending_irqs));
+    memset(&v->arch.vgic->pending_irqs, 0, sizeof(v->arch.vgic->pending_irqs));
     for (i = 0; i < 32; i++)
-        vgic_init_pending_irq(&v->arch.vgic.pending_irqs[i], i);
+        vgic_init_pending_irq(&v->arch.vgic->pending_irqs[i], i);
 
-    INIT_LIST_HEAD(&v->arch.vgic.inflight_irqs);
-    INIT_LIST_HEAD(&v->arch.vgic.lr_pending);
-    spin_lock_init(&v->arch.vgic.lock);
+    INIT_LIST_HEAD(&v->arch.vgic->inflight_irqs);
+    INIT_LIST_HEAD(&v->arch.vgic->lr_pending);
+    spin_lock_init(&v->arch.vgic->lock);
 
     return 0;

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 403

Quoted 10 lines
xen/arch/arm/vgic.c +3 -1
 }
 
 int vcpu_vgic_free(struct vcpu *v)
 {
-    xfree(v->arch.vgic.private_irqs);
+    xfree(v->arch.vgic->private_irqs);
+    xfree(v->arch.vgic);
+
     return 0;
 }

Free functions should be idempotent.  This was buggy before, even moreso now.  It wants to be:

void vcpu_vgic_free(struct vcpu *v)
{
    if ( v->arch.vgic )
    {
        XFREE(v->arch.vgic->private_irqs);
        XFREE(v->arch.vgic);
    }
}

Given the type change, this probably wants splitting out into an earlier patch.

Given the fact that the single caller doesn't even check the return value, you're fixing a MISRA violation by making it void.

Quoted 7 lines
xen/arch/arm/vgic/vgic-init.c +0 -0
index f8d7d3a226..67f297797f 100644
@@ -241,10 +245,12 @@ void domain_vgic_free(struct domain *d)
 
 int vcpu_vgic_free(struct vcpu *v)

xen/arch/arm/vgic/vgic-init.c void domain_vgic_free(struct domain *d) · line 255

Quoted 10 lines
xen/arch/arm/vgic/vgic-init.c +3 -1
 {
-    struct vgic_cpu *vgic_cpu = &v->arch.vgic;
+    struct vgic_cpu *vgic_cpu = v->arch.vgic;
 
     INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
 
+    xfree(vgic_cpu);
+
     return 0;
 }

Not in your part of the change, but this is bogus.  It's not remotely safe to init the list head like that.

The list is either already empty, in which case it's a no-op, or it corrupts the list.  It appears that the list mixes entries from other vCPUs, and from the domain.

I think this is further proof that NEW_VGIC should be deleted wholesale.  It's clearly not in a good state, and I get the impression that a badly timed evtchn sent to a domain in the middle of being cleaned up will cause Xen to trip over the corrupted list.

xen/arch/arm/vgic/vgic-v2.c void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize, · line 60

Quoted 13 lines
xen/arch/arm/vgic/vgic-v2.c +2 -2
index 6a558089c5..e64d681dd2 100644
@@ -56,8 +56,8 @@ void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
  */
 void vgic_v2_fold_lr_state(struct vcpu *vcpu)
 {
-    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic;
-    unsigned int used_lrs = vcpu->arch.vgic.used_lrs;
+    struct vgic_cpu *vgic_cpu = vcpu->arch.vgic;
+    unsigned int used_lrs = vcpu->arch.vgic->used_lrs;

vgic_cpu->used_lrs.

Taking a step back, I think the patch could be much smaller if you only made private_irqs in NEW_VGIC be a separate pointer, matching the "old" VGIC code.  Or does that not save enough space in struct vCPU?

~Andrew

#11 lore raw

Re: [PATCH v2 2/4] xen/arm: drop MAX_PAGES_PER_VCPU

In reply to Oleksii Kurochko
Quoted 23 lines · Oleksii Kurochko
xen/arch/arm/domain.c +0 -11
index 47973f99d9..e566023340 100644
@@ -473,36 +473,21 @@ void dump_pageframe_info(struct domain *d)
 
 }
 
-/*
- * The new VGIC has a bigger per-IRQ structure, so we need more than one
- * page on ARM64. Cowardly increase the limit in this case.
- */
-#if defined(CONFIG_NEW_VGIC) && defined(CONFIG_ARM_64)
-#define MAX_PAGES_PER_VCPU  2
-#else
-#define MAX_PAGES_PER_VCPU  1
-#endif
-
 struct vcpu *alloc_vcpu_struct(const struct domain *d)
 {
     struct vcpu *v;
 
-    BUILD_BUG_ON(sizeof(*v) > MAX_PAGES_PER_VCPU * PAGE_SIZE);

xen/arch/arm/domain.c void dump_pageframe_info(struct domain *d) · line 481

Quoted 10 lines
xen/arch/arm/domain.c +2 -8
-    v = alloc_xenheap_pages(get_order_from_bytes(sizeof(*v)), 0);
-    if ( v != NULL )
-    {
-        unsigned int i;
-
-        for ( i = 0; i < DIV_ROUND_UP(sizeof(*v), PAGE_SIZE); i++ )
-            clear_page((void *)v + i * PAGE_SIZE);
-    }
+    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
+    v = alloc_xenheap_pages(0, 0);

I know this is only interim until the next patch, but alloc_xenheap_page() to match the free function used.

Personally, I'd merge patches 2 and 3 together, because everything you touch in this patch is deleted by the next one.

But, whatever the ARM maintainers prefer.

~Andrew

Acked-by on v2 4/4
#18 lore raw

Re: [PATCH v2 4/4] xen/common: make {alloc,free}_domain_struct() static

In reply to Oleksii Kurochko
Quoted 5 lines · Oleksii Kurochko

As {alloc,free}_domain_struct() are used only within domain.c, they can be declared static and their declarations removed from xen/domain.h.

Quoted 13 lines

---
Changes in v2:
- New patch.

---
 xen/common/domain.c      | 6 ++++--
 xen/include/xen/domain.h | 4 ----
 2 files changed, 4 insertions(+), 6 deletions(-)
xen/common/domain.c +0 -0
index 92fc0684fc..7509dafd6f 100644

xen/common/domain.c static int domain_teardown(struct domain *d) · line 694

Quoted 5 lines
xen/common/domain.c +2 -0
@@ -690,6 +690,8 @@ static int domain_teardown(struct domain *d)
     return 0;
 }
 
+static void free_domain_struct(struct domain *d);
+

Another option would be to move them both up here, and avoid the forward declaration.  It's a bigger patch, but results in domain.c being slightly less complex.

~Andrew

#15 lore raw

Re: [PATCH v2 3/4] xen: move alloc/free_vcpu_struct() to common code

In reply to Oleksii Kurochko
Quoted 26 lines · Oleksii Kurochko

alloc_vcpu_struct() and free_vcpu_struct() contain little architecture-specific logic and are suitable for sharing across architectures. Move both helpers to common code.

To support the remaining architectural differences, introduce arch_vcpu_struct_memflags(), allowing architectures to override the memory flags passed to alloc_xenheap_pages(). This is currently needed by x86, which may require MEMF_bits(32) for HVM guests using shadow paging.

The ARM implementation of alloc/free_vcpu_struct() is removed and replaced by the common version. Stub implementations are also dropped from PPC and RISC-V.

Finally, make alloc_vcpu_struct() and free_vcpu_struct() static to common/domain.c, as they are no longer used outside common code.

No functional changes.

---
Changes in v2:
- Rework alloc/free_vcpu_struct() to work with only one page.
- Return back the comment about the restriction inside x86's arch_vcpu_struct_memflags().
- Drop MAX_PAGES_PER_VCPU.

Quoted 9 lines
---
 xen/arch/arm/domain.c             | 17 -----------------
 xen/arch/ppc/stubs.c              | 10 ----------
 xen/arch/riscv/stubs.c            | 10 ----------
 xen/arch/x86/domain.c             | 17 ++---------------
 xen/arch/x86/include/asm/domain.h |  3 +++
 xen/common/domain.c               | 20 ++++++++++++++++++++
 xen/include/xen/domain.h          |  4 ----
 7 files changed, 25 insertions(+), 56 deletions(-)

Much nicer.

Quoted 40 lines
xen/arch/x86/domain.c +2 -15
index 7632d5e2d6..68c7503eda 100644
@@ -493,28 +493,15 @@ unsigned int arch_domain_struct_memflags(void)
     return MEMF_bits(bits);
 }
 
-struct vcpu *alloc_vcpu_struct(const struct domain *d)
+unsigned int arch_vcpu_struct_memflags(const struct domain *d)
 {
-    struct vcpu *v;
     /*
      * This structure contains embedded PAE PDPTEs, used when an HVM guest
      * runs on shadow pagetables outside of 64-bit mode. In this case the CPU
      * may require that the shadow CR3 points below 4GB, and hence the whole
      * structure must satisfy this restriction. Thus we specify MEMF_bits(32).
      */
-    unsigned int memflags =
-        (is_hvm_domain(d) && paging_mode_shadow(d)) ? MEMF_bits(32) : 0;
-
-    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
-    v = alloc_xenheap_pages(0, memflags);
-    if ( v != NULL )
-        clear_page(v);
-    return v;
-}
-
-void free_vcpu_struct(struct vcpu *v)
-{
-    free_xenheap_page(v);
+    return (is_hvm_domain(d) && paging_mode_shadow(d)) ? MEMF_bits(32) : 0;
 }
 
 /* Initialise various registers to their architectural INIT/RESET state. */
xen/arch/x86/include/asm/domain.h +0 -0
index 7e5cbd11a4..576f9202b4 100644

xen/arch/x86/include/asm/domain.h line 20

Quoted 6 lines
xen/arch/x86/include/asm/domain.h +3 -0
@@ -15,6 +15,9 @@
 unsigned int arch_domain_struct_memflags(void);
 #define arch_domain_struct_memflags arch_domain_struct_memflags
 
+unsigned int arch_vcpu_struct_memflags(const struct domain *d);
+#define arch_vcpu_struct_memflags arch_vcpu_struct_memflags
+

Given the single caller and simplicity, this would be better as a static inline, except it can't yet for header reasons.  So, instead, I'd suggest simply:

/*
 * $COMMENT
 */
#define arch_vcpu_struct_memflags(d) \
(is_hvm_domain(d) && paging_mode_shadow(d) ? MEMF_bits(32) : 0)

This form does double expand 'd'.  The more complex form would be to match
https://xenbits.xen.org/gitweb/?p=xen.git;a=commitdiff;h=c37bcb35f928c81cbeea7c05f86b6779f8a2b8c4
with a local variable.

xen/common/domain.c static int vcpu_teardown(struct vcpu *v) · line 398

Quoted 12 lines
xen/common/domain.c +4 -0
index 93c71bc766..92fc0684fc 100644
@@ -392,6 +392,26 @@ static int vcpu_teardown(struct vcpu *v)
     return 0;
 }
 
+static struct vcpu *alloc_vcpu_struct(const struct domain *d)
+{
+#ifndef arch_vcpu_struct_memflags
+# define arch_vcpu_struct_memflags(d) 0

((void)d, 0)

~Andrew

#16 lore raw

Re: [PATCH v2 3/4] xen: move alloc/free_vcpu_struct() to common code

Quoted 7 lines · Andrew Cooper
Quoted 6 lines · Oleksii Kurochko
xen/common/domain.c +0 -0
@@ -392,6 +392,26 @@ static int vcpu_teardown(struct vcpu *v)
     return 0;
 }
 
Quoted 6 lines

xen/common/domain.c static int vcpu_teardown(struct vcpu *v) · line 398

Quoted 4 lines
xen/common/domain.c +4 -0
+static struct vcpu *alloc_vcpu_struct(const struct domain *d)
+{
+#ifndef arch_vcpu_struct_memflags
+# define arch_vcpu_struct_memflags(d) 0

((void)d, 0)

Nit: ((void)(d), 0)

Jan

#12 lore raw

Re: [PATCH v2 2/4] xen/arm: drop MAX_PAGES_PER_VCPU

In reply to Andrew Cooper
Quoted 38 lines · Andrew Cooper
Quoted 23 lines · Oleksii Kurochko
xen/arch/arm/domain.c +0 -11
index 47973f99d9..e566023340 100644
@@ -473,36 +473,21 @@ void dump_pageframe_info(struct domain *d)
 
 }
 
-/*
- * The new VGIC has a bigger per-IRQ structure, so we need more than one
- * page on ARM64. Cowardly increase the limit in this case.
- */
-#if defined(CONFIG_NEW_VGIC) && defined(CONFIG_ARM_64)
-#define MAX_PAGES_PER_VCPU  2
-#else
-#define MAX_PAGES_PER_VCPU  1
-#endif
-
 struct vcpu *alloc_vcpu_struct(const struct domain *d)
 {
     struct vcpu *v;
 
-    BUILD_BUG_ON(sizeof(*v) > MAX_PAGES_PER_VCPU * PAGE_SIZE);

xen/arch/arm/domain.c void dump_pageframe_info(struct domain *d) · line 481

Quoted 10 lines
xen/arch/arm/domain.c +2 -8
-    v = alloc_xenheap_pages(get_order_from_bytes(sizeof(*v)), 0);
-    if ( v != NULL )
-    {
-        unsigned int i;
-
-        for ( i = 0; i < DIV_ROUND_UP(sizeof(*v), PAGE_SIZE); i++ )
-            clear_page((void *)v + i * PAGE_SIZE);
-    }
+    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
+    v = alloc_xenheap_pages(0, 0);

I know this is only interim until the next patch, but alloc_xenheap_page() to match the free function used.

Quoted 4 lines

Personally, I'd merge patches 2 and 3 together, because everything you touch in this patch is deleted by the next one.

But, whatever the ARM maintainers prefer.

I'm in favor of Andrew's suggestion. There's no point in introducing something in one patch and dropping it in the very next one.

~Michal

#3 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

In reply to Andrew Cooper
Quoted 64 lines · Andrew Cooper
Quoted 7 lines · Oleksii Kurochko
xen/arch/arm/vgic.c +0 -0
index 3ebdf9953f..8b17871b86 100644
@@ -370,29 +370,35 @@ int vcpu_vgic_init(struct vcpu *v)
  {
      int i;

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 379

Quoted 10 lines
xen/arch/arm/vgic.c +6 -2
 
-    v->arch.vgic.private_irqs = xzalloc(struct vgic_irq_rank);
-    if ( v->arch.vgic.private_irqs == NULL )
+    v->arch.vgic = xzalloc(struct vgic_cpu);
+    if ( v->arch.vgic == NULL )
+        return -ENOMEM;
+
+    v->arch.vgic->private_irqs = xzalloc(struct vgic_irq_rank);
+    if ( v->arch.vgic->private_irqs == NULL )
        return -ENOMEM;

This error path needs to free v->arch.vgic.  (If we continue down this route.  See below.)

Quoted 21 lines
xen/arch/arm/vgic.c +6 -6
      /* SGIs/PPIs are always routed to this VCPU */
-    vgic_rank_init(v->arch.vgic.private_irqs, 0, v->vcpu_id);
+    vgic_rank_init(v->arch.vgic->private_irqs, 0, v->vcpu_id);
 
      v->domain->arch.vgic.handler->vcpu_init(v);
 
-    memset(&v->arch.vgic.pending_irqs, 0, sizeof(v->arch.vgic.pending_irqs));
+    memset(&v->arch.vgic->pending_irqs, 0, sizeof(v->arch.vgic->pending_irqs));
      for (i = 0; i < 32; i++)
-        vgic_init_pending_irq(&v->arch.vgic.pending_irqs[i], i);
+        vgic_init_pending_irq(&v->arch.vgic->pending_irqs[i], i);
 
-    INIT_LIST_HEAD(&v->arch.vgic.inflight_irqs);
-    INIT_LIST_HEAD(&v->arch.vgic.lr_pending);
-    spin_lock_init(&v->arch.vgic.lock);
+    INIT_LIST_HEAD(&v->arch.vgic->inflight_irqs);
+    INIT_LIST_HEAD(&v->arch.vgic->lr_pending);
+    spin_lock_init(&v->arch.vgic->lock);
 
      return 0;

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 403

Quoted 10 lines
xen/arch/arm/vgic.c +3 -1
  }
 
  int vcpu_vgic_free(struct vcpu *v)
  {
-    xfree(v->arch.vgic.private_irqs);
+    xfree(v->arch.vgic->private_irqs);
+    xfree(v->arch.vgic);
+
      return 0;
  }

Free functions should be idempotent.  This was buggy before, even moreso now.  It wants to be:

void vcpu_vgic_free(struct vcpu *v)
{
    if ( v->arch.vgic )
    {
        XFREE(v->arch.vgic->private_irqs);
        XFREE(v->arch.vgic);
    }
}

Quoted 5 lines

Given the type change, this probably wants splitting out into an earlier patch.

Given the fact that the single caller doesn't even check the return value, you're fixing a MISRA violation by making it void.

Btw, IIUC, it could be also be something like:
(void) vcpu_vgic_free(...)
and then we won't break any MISRA rule, right?

I will send suggested updates in the separate patch of this patch series.

Quoted 21 lines
Quoted 8 lines
xen/arch/arm/vgic/vgic-init.c +0 -0
index f8d7d3a226..67f297797f 100644
@@ -241,10 +245,12 @@ void domain_vgic_free(struct domain *d)
 
  int vcpu_vgic_free(struct vcpu *v)

xen/arch/arm/vgic/vgic-init.c void domain_vgic_free(struct domain *d) · line 255

Quoted 10 lines
xen/arch/arm/vgic/vgic-init.c +3 -1
  {
-    struct vgic_cpu *vgic_cpu = &v->arch.vgic;
+    struct vgic_cpu *vgic_cpu = v->arch.vgic;
 
      INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
 
+    xfree(vgic_cpu);
+
      return 0;
  }

Not in your part of the change, but this is bogus.  It's not remotely safe to init the list head like that.

Quoted 3 lines

The list is either already empty, in which case it's a no-op, or it corrupts the list.  It appears that the list mixes entries from other vCPUs, and from the domain.

I guess it should be handled by vgic_prune_ap_list() which is called when we are entering hypervisor (enter_hypervisor_from_guest() -> vgic_sync_from_lrs() -> vgic_prune_ap_list()).

Also, I would like not that this code is based on KVM which also has the same INIT_LIST_HEAD():
https://elixir.bootlin.com/linux/v6.18.1/source/arch/arm64/kvm/vgic/vgic-init.c#L467

I won't touch this code in the next patch series except if one of Arm's maintainer will tell me so.

Quoted 21 lines

I think this is further proof that NEW_VGIC should be deleted wholesale.  It's clearly not in a good state, and I get the impression that a badly timed evtchn sent to a domain in the middle of being cleaned up will cause Xen to trip over the corrupted list.

xen/arch/arm/vgic/vgic-v2.c void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize, · line 60

Quoted 13 lines
xen/arch/arm/vgic/vgic-v2.c +2 -2
index 6a558089c5..e64d681dd2 100644
@@ -56,8 +56,8 @@ void vgic_v2_setup_hw(paddr_t dbase, paddr_t cbase, paddr_t csize,
   */
  void vgic_v2_fold_lr_state(struct vcpu *vcpu)
  {
-    struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic;
-    unsigned int used_lrs = vcpu->arch.vgic.used_lrs;
+    struct vgic_cpu *vgic_cpu = vcpu->arch.vgic;
+    unsigned int used_lrs = vcpu->arch.vgic->used_lrs;

vgic_cpu->used_lrs.

Quoted 3 lines

Taking a step back, I think the patch could be much smaller if you only made private_irqs in NEW_VGIC be a separate pointer, matching the "old" VGIC code.  Or does that not save enough space in struct vCPU?

It seems like it would be also fine, the size of struct vcpu will be 2176. And it also basically just require only:
struct vgic_cpu {
- struct vgic_irq private_irqs[VGIC_NR_PRIVATE_IRQS];
+ struct vgic_irq *private_irqs;
(of course with allocation and freeing of private_irqs).

Lets stick to this approach then.

Thanks.

~ Oleksii

#13 lore raw

Re: [PATCH v2 2/4] xen/arm: drop MAX_PAGES_PER_VCPU

In reply to Orzel, Michal
Quoted 39 lines · Michal
Quoted 37 lines · Andrew Cooper
Quoted 23 lines · Oleksii Kurochko
xen/arch/arm/domain.c +0 -11
index 47973f99d9..e566023340 100644
@@ -473,36 +473,21 @@ void dump_pageframe_info(struct domain *d)
 
  }
 
-/*
- * The new VGIC has a bigger per-IRQ structure, so we need more than one
- * page on ARM64. Cowardly increase the limit in this case.
- */
-#if defined(CONFIG_NEW_VGIC) && defined(CONFIG_ARM_64)
-#define MAX_PAGES_PER_VCPU  2
-#else
-#define MAX_PAGES_PER_VCPU  1
-#endif
-
  struct vcpu *alloc_vcpu_struct(const struct domain *d)
  {
      struct vcpu *v;
 
-    BUILD_BUG_ON(sizeof(*v) > MAX_PAGES_PER_VCPU * PAGE_SIZE);

xen/arch/arm/domain.c void dump_pageframe_info(struct domain *d) · line 481

Quoted 10 lines
xen/arch/arm/domain.c +2 -8
-    v = alloc_xenheap_pages(get_order_from_bytes(sizeof(*v)), 0);
-    if ( v != NULL )
-    {
-        unsigned int i;
-
-        for ( i = 0; i < DIV_ROUND_UP(sizeof(*v), PAGE_SIZE); i++ )
-            clear_page((void *)v + i * PAGE_SIZE);
-    }
+    BUILD_BUG_ON(sizeof(*v) > PAGE_SIZE);
+    v = alloc_xenheap_pages(0, 0);

I know this is only interim until the next patch, but alloc_xenheap_page() to match the free function used.

Quoted 6 lines
Quoted 4 lines

Personally, I'd merge patches 2 and 3 together, because everything you touch in this patch is deleted by the next one.

But, whatever the ARM maintainers prefer.

I'm in favor of Andrew's suggestion. There's no point in introducing something in one patch and dropping it in the very next one.

Then I will merge patch 2 and 3.

~ Oleksii

#19 lore raw

Re: [PATCH v2 4/4] xen/common: make {alloc,free}_domain_struct() static

In reply to Andrew Cooper
Quoted 7 lines · Andrew Cooper
Quoted 5 lines · Oleksii Kurochko

As {alloc,free}_domain_struct() are used only within domain.c, they can be declared static and their declarations removed from xen/domain.h.

Thanks.

Quoted 16 lines
Quoted 16 lines

---
Changes in v2:
- New patch.

---
  xen/common/domain.c      | 6 ++++--
  xen/include/xen/domain.h | 4 ----
  2 files changed, 4 insertions(+), 6 deletions(-)
xen/common/domain.c +0 -0
index 92fc0684fc..7509dafd6f 100644
@@ -690,6 +690,8 @@ static int domain_teardown(struct domain *d)
      return 0;
  }
 
Quoted 5 lines

xen/common/domain.c static int domain_teardown(struct domain *d) · line 694

Quoted 2 lines
xen/common/domain.c +2 -0
+static void free_domain_struct(struct domain *d);
+

Another option would be to move them both up here, and avoid the forward declaration.  It's a bigger patch, but results in domain.c being slightly less complex.

Then I will move both|free_domain_struct()| and|alloc_domain_struct()| (to keep them together) to the place where the forward declaration of|free_domain_struct()| is introduced in this patch.

Does that work for you?

~ Oleksii

#5 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 403

Quoted 8 lines
  int vcpu_vgic_free(struct vcpu *v)
  {
xen/arch/arm/vgic.c +3 -1
-    xfree(v->arch.vgic.private_irqs);
+    xfree(v->arch.vgic->private_irqs);
+    xfree(v->arch.vgic);
+
      return 0;
  }

Free functions should be idempotent.  This was buggy before, even moreso now.

Was it really buggy before in terms of idempotent.

It seems like xfree() can handle the case when v->arch.vgic.private_irqs is NULL. Should I still have:
if ( v->arch.vgic->private_irqs )

    XFREE(v->arch.vgic->private_irqs);

?

~ Oleksii

#4 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

In reply to Oleksii Kurochko
Quoted 69 lines · Oleksii Kurochko
Quoted 67 lines · Andrew Cooper
Quoted 7 lines · Oleksii Kurochko
xen/arch/arm/vgic.c +0 -0
index 3ebdf9953f..8b17871b86 100644
@@ -370,29 +370,35 @@ int vcpu_vgic_init(struct vcpu *v)
  {
      int i;

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 379

Quoted 10 lines
xen/arch/arm/vgic.c +6 -2
 
-    v->arch.vgic.private_irqs = xzalloc(struct vgic_irq_rank);
-    if ( v->arch.vgic.private_irqs == NULL )
+    v->arch.vgic = xzalloc(struct vgic_cpu);
+    if ( v->arch.vgic == NULL )
+        return -ENOMEM;
+
+    v->arch.vgic->private_irqs = xzalloc(struct vgic_irq_rank);
+    if ( v->arch.vgic->private_irqs == NULL )
        return -ENOMEM;

This error path needs to free v->arch.vgic.  (If we continue down this route.  See below.)

Quoted 21 lines
xen/arch/arm/vgic.c +6 -6
      /* SGIs/PPIs are always routed to this VCPU */
-    vgic_rank_init(v->arch.vgic.private_irqs, 0, v->vcpu_id);
+    vgic_rank_init(v->arch.vgic->private_irqs, 0, v->vcpu_id);
 
      v->domain->arch.vgic.handler->vcpu_init(v);
 
-    memset(&v->arch.vgic.pending_irqs, 0, sizeof(v->arch.vgic.pending_irqs));
+    memset(&v->arch.vgic->pending_irqs, 0, sizeof(v->arch.vgic->pending_irqs));
      for (i = 0; i < 32; i++)
-        vgic_init_pending_irq(&v->arch.vgic.pending_irqs[i], i);
+        vgic_init_pending_irq(&v->arch.vgic->pending_irqs[i], i);
 
-    INIT_LIST_HEAD(&v->arch.vgic.inflight_irqs);
-    INIT_LIST_HEAD(&v->arch.vgic.lr_pending);
-    spin_lock_init(&v->arch.vgic.lock);
+    INIT_LIST_HEAD(&v->arch.vgic->inflight_irqs);
+    INIT_LIST_HEAD(&v->arch.vgic->lr_pending);
+    spin_lock_init(&v->arch.vgic->lock);
 
      return 0;

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 403

Quoted 10 lines
xen/arch/arm/vgic.c +3 -1
  }
 
  int vcpu_vgic_free(struct vcpu *v)
  {
-    xfree(v->arch.vgic.private_irqs);
+    xfree(v->arch.vgic->private_irqs);
+    xfree(v->arch.vgic);
+
      return 0;
  }

Free functions should be idempotent.  This was buggy before, even moreso now.  It wants to be:

void vcpu_vgic_free(struct vcpu *v)
{
    if ( v->arch.vgic )
    {
        XFREE(v->arch.vgic->private_irqs);
        XFREE(v->arch.vgic);
    }
}

Given the type change, this probably wants splitting out into an earlier patch.

Quoted 6 lines
Quoted 2 lines

Given the fact that the single caller doesn't even check the return value, you're fixing a MISRA violation by making it void.

Btw, IIUC, it could be also be something like:

   (void) vcpu_vgic_free(...)

and then we won't break any MISRA rule, right?

That would address one Misra concern, but there would then still be dead code.

Jan

#6 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

In reply to Oleksii Kurochko
Quoted 13 lines · Oleksii Kurochko
Quoted 10 lines · Andrew Cooper

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 403

Quoted 8 lines
  int vcpu_vgic_free(struct vcpu *v)
  {
xen/arch/arm/vgic.c +3 -1
-    xfree(v->arch.vgic.private_irqs);
+    xfree(v->arch.vgic->private_irqs);
+    xfree(v->arch.vgic);
+
      return 0;
  }

Free functions should be idempotent.  This was buggy before, even moreso now.

Quoted 7 lines

Was it really buggy before in terms of idempotent.

It seems like xfree() can handle the case when v->arch.vgic.private_irqs is NULL. Should I still have:
if ( v->arch.vgic->private_irqs )

     XFREE(v->arch.vgic->private_irqs);

?

No, and iirc Andrew also didn't ask for this.

Jan

#9 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

In reply to Andrew Cooper

Hi Andrew,

Quoted 21 lines · Andrew Cooper
Quoted 7 lines
xen/arch/arm/vgic/vgic-init.c +0 -0
index f8d7d3a226..67f297797f 100644
@@ -241,10 +245,12 @@ void domain_vgic_free(struct domain *d)
 
  int vcpu_vgic_free(struct vcpu *v)

xen/arch/arm/vgic/vgic-init.c void domain_vgic_free(struct domain *d) · line 255

Quoted 10 lines
xen/arch/arm/vgic/vgic-init.c +3 -1
  {
-    struct vgic_cpu *vgic_cpu = &v->arch.vgic;
+    struct vgic_cpu *vgic_cpu = v->arch.vgic;
 
      INIT_LIST_HEAD(&vgic_cpu->ap_list_head);
 
+    xfree(vgic_cpu);
+
      return 0;
  }

Not in your part of the change, but this is bogus.  It's not remotely safe to init the list head like that.

Quoted 8 lines

The list is either already empty, in which case it's a no-op, or it corrupts the list.  It appears that the list mixes entries from other vCPUs, and from the domain.

I think this is further proof that NEW_VGIC should be deleted wholesale.  It's clearly not in a good state, and I get the impression that a badly timed evtchn sent to a domain in the middle of being cleaned up will cause Xen to trip over the corrupted list.

I am probably missing something. What other issues are in the NEW_VGIC?

But note, if we delete the NEW_VGIC then we are back to a situation where the vGIC implementation we have is not even remotely spec compliant (no level support, not pending/active clear/set support) and from previous discussion it was not possible to rework the default vGIC implementation to make it compliant.

I was going to say that the NEW_VGIC is meant to be experimental. But I see, it can selected without EXPERT :(. Thankfully it is marked as not security supported at least in the Kconfig doc.

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

In reply to Jan Beulich
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
Quoted 10 lines · Andrew Cooper

xen/arch/arm/vgic.c int vcpu_vgic_init(struct vcpu *v) · line 403

Quoted 8 lines
   int vcpu_vgic_free(struct vcpu *v)
   {
xen/arch/arm/vgic.c +3 -1
-    xfree(v->arch.vgic.private_irqs);
+    xfree(v->arch.vgic->private_irqs);
+    xfree(v->arch.vgic);
+
       return 0;
   }

Free functions should be idempotent.  This was buggy before, even moreso now.

Quoted 8 lines
Quoted 7 lines

Was it really buggy before in terms of idempotent.

It seems like xfree() can handle the case when v->arch.vgic.private_irqs is NULL. Should I still have:

   if ( v->arch.vgic->private_irqs )
      XFREE(v->arch.vgic->private_irqs);

?

No, and iirc Andrew also didn't ask for this.

Andrew wrote:

```
Free functions should be idempotent.  This was buggy before, even moreso
now.  It wants to be:

void vcpu_vgic_free(struct vcpu *v)
{
     if ( v->arch.vgic )
     {
         XFREE(v->arch.vgic->private_irqs);
         XFREE(v->arch.vgic);
     }
}

Given the type change, this probably wants splitting out into an earlier
patch.

Given the fact that the single caller doesn't even check the return
value, you're fixing a MISRA violation by making it void.
```

It isn't clear what "This was buggy before" means. In my changes it will be an issue if vcpu_vgic_free() would be called twice because without "if ( v->arch.vgic )" we will have NULL pointer dereference XFREE(v->arch.vgic->private_irqs) here. But considering that before it was just:
int vcpu_vgic_free(struct vcpu *v)

  {
	xfree(v->arch.vgic.private_irqs);
	return 0;

}
In that case, it seems that calling|vcpu_vgic_free()| twice would not cause anything serious to happen. Am I misunderstanding what was buggy in the original code, or probably I'm misunderstanding what is idempotent?

~ Oleksii

#8 lore raw

Re: [PATCH v2 1/4] xen/arm: optimize the size of struct vcpu

Quoted 44 lines · Oleksii Kurochko
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Oleksii Kurochko
Quoted 10 lines · Andrew Cooper
Quoted 8 lines

   int vcpu_vgic_free(struct vcpu *v)
   {
-    xfree(v->arch.vgic.private_irqs);
+    xfree(v->arch.vgic->private_irqs);
+    xfree(v->arch.vgic);
+
       return 0;
   }

Free functions should be idempotent.  This was buggy before, even moreso now.

Quoted 8 lines
Quoted 7 lines

Was it really buggy before in terms of idempotent.

It seems like xfree() can handle the case when v->arch.vgic.private_irqs is NULL. Should I still have:
   if ( v->arch.vgic->private_irqs ) XFREE(v->arch.vgic->private_irqs);
?

No, and iirc Andrew also didn't ask for this.

Andrew wrote:

```
Free functions should be idempotent.  This was buggy before, even moreso
now.  It wants to be:

void vcpu_vgic_free(struct vcpu *v)
{
     if ( v->arch.vgic )
     {
         XFREE(v->arch.vgic->private_irqs);
         XFREE(v->arch.vgic);
     }
}

Given the type change, this probably wants splitting out into an earlier
patch.

Given the fact that the single caller doesn't even check the return
value, you're fixing a MISRA violation by making it void.
```
Quoted 13 lines

It isn't clear what "This was buggy before" means. In my changes
it will be an issue if vcpu_vgic_free() would be called twice because without "if ( v->arch.vgic )" we will have NULL pointer dereference XFREE(v->arch.vgic->private_irqs) here. But considering that before it was just:
  int vcpu_vgic_free(struct vcpu *v)
  {
    xfree(v->arch.vgic.private_irqs);
    return 0;
  }
In that case, it seems that calling|vcpu_vgic_free()| twice would not cause anything serious to happen. Am I misunderstanding what was buggy in the original code, or probably I'm misunderstanding what is idempotent?

Oh, it is about that xfree() doesn't set private_irqs pointer to NULL. I thought it is doing that inside xfree().

~ Oleksii