Skip to contentSkip to conversation

new CONFIG_HAS_PIRQ and extra_guest_irqs adjustment

PATCH Superseded Jan Beulich 3 participants 17 messages May 3, 2023 – Jul 26, 2023

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover new CONFIG_HAS_PIRQ and extra_guest_irqs adjustment

The 1st patch (new in v2) has the effect of the 2nd one no longer affecting Arm.

1: restrict concept of pIRQ to x86
2: cmdline: document and enforce "extra_guest_irqs" upper bounds

Jan

#1 lore raw

PATCH v2 1/2 restrict concept of pIRQ to x86

... by way of a new arch-selectable Kconfig control.

Note that some smaller pieces of code are left without #ifdef, to keep things better readable. Hence items like ECS_PIRQ, nr_static_irqs, or domain_pirq_to_irq() remain uniformly.

---
I'm not really certain about XEN_DOMCTL_irq_permission: With pIRQ-s not used, the prior pIRQ -> IRQ translation cannot have succeeded on Arm, so quite possibly the entire domctl is unused there? Yet then how is access to particular device IRQs being granted/revoked?
---
v2: New.

docs/misc/xen-command-line.pandoc +1 -1
@@ -1120,7 +1120,7 @@ introduced with the Nehalem architecture
       intended as an emergency option for people who first chose fast, then
       change their minds to secure, and wish not to reboot.**
 
-### extra_guest_irqs
+### extra_guest_irqs (x86)
 > `= [<domU number>][,<dom0 number>]`
 
 > Default: `32,<variable>`
xen/arch/arm/include/asm/irq.h +0 -1
@@ -52,7 +52,6 @@ struct arch_irq_desc {
 
 extern const unsigned int nr_irqs;
 #define nr_static_irqs NR_IRQS
-#define arch_hwdom_irqs(domid) NR_IRQS
 
 struct irq_desc;
 struct irqaction;
xen/arch/x86/Kconfig +1 -0
@@ -25,6 +25,7 @@ config X86
 	select HAS_PCI
 	select HAS_PCI_MSI
 	select HAS_PDX
+	select HAS_PIRQ
 	select HAS_SCHED_GRANULARITY
 	select HAS_UBSAN
 	select HAS_VPCI if HVM
xen/common/Kconfig +3 -0
@@ -56,6 +56,9 @@ config HAS_KEXEC
 config HAS_PDX
 	bool
 
+config HAS_PIRQ
+	bool
+
 config HAS_PMAP
 	bool
 
xen/common/domain.c +14 -0
@@ -350,6 +350,8 @@ static int late_hwdom_init(struct domain
 #endif
 }
 
+#ifdef CONFIG_HAS_PIRQ
+
 static unsigned int __read_mostly extra_hwdom_irqs;
 static unsigned int __read_mostly extra_domU_irqs = 32;
 
@@ -364,6 +366,8 @@ static int __init cf_check parse_extra_g
 }
 custom_param("extra_guest_irqs", parse_extra_guest_irqs);
 
+#endif /* CONFIG_HAS_PIRQ */
+
 /*
  * Release resources held by a domain.  There may or may not be live
  * references to the domain, and it may or may not be fully constructed.
@@ -653,6 +657,7 @@ struct domain *domain_create(domid_t dom
     if ( is_system_domain(d) && !is_idle_domain(d) )
         return d;
 
+#ifdef CONFIG_HAS_PIRQ
     if ( !is_idle_domain(d) )
     {
         if ( !is_hardware_domain(d) )
@@ -664,6 +669,7 @@ struct domain *domain_create(domid_t dom
 
         radix_tree_init(&d->pirq_tree);
     }
+#endif
 
     if ( (err = arch_domain_create(d, config, flags)) != 0 )
         goto fail;
@@ -755,7 +761,9 @@ struct domain *domain_create(domid_t dom
     {
         evtchn_destroy(d);
         evtchn_destroy_final(d);
+#ifdef CONFIG_HAS_PIRQ
         radix_tree_destroy(&d->pirq_tree, free_pirq_struct);
+#endif
     }
     if ( init_status & INIT_watchdog )
         watchdog_domain_destroy(d);
@@ -1151,7 +1159,9 @@ static void cf_check complete_domain_des
 
     evtchn_destroy_final(d);
 
+#ifdef CONFIG_HAS_PIRQ
     radix_tree_destroy(&d->pirq_tree, free_pirq_struct);
+#endif
 
     xfree(d->vcpu);
 
@@ -1864,6 +1874,8 @@ long do_vm_assist(unsigned int cmd, unsi
 }
 #endif
 
+#ifdef CONFIG_HAS_PIRQ
+
 struct pirq *pirq_get_info(struct domain *d, int pirq)
 {
     struct pirq *info = pirq_info(d, pirq);
@@ -1893,6 +1905,8 @@ void cf_check free_pirq_struct(void *ptr
     call_rcu(&pirq->rcu_head, _free_pirq_struct);
 }
 
+#endif /* CONFIG_HAS_PIRQ */
+
 struct migrate_info {
     long (*func)(void *data);
     void *data;
xen/common/domctl.c +2 -0
@@ -683,11 +683,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
         unsigned int pirq = op->u.irq_permission.pirq, irq;
         int allow = op->u.irq_permission.allow_access;
 
+#ifdef CONFIG_HAS_PIRQ
         if ( pirq >= current->domain->nr_pirqs )
         {
             ret = -EINVAL;
             break;
         }
+#endif
         irq = pirq_access_permitted(current->domain, pirq);
         if ( !irq || xsm_irq_permission(XSM_HOOK, d, irq, allow) )
             ret = -EPERM;
xen/common/event_channel.c +12 -2
@@ -555,6 +555,7 @@ static int evtchn_bind_ipi(evtchn_bind_i
     return rc;
 }
 
+#ifdef CONFIG_HAS_PIRQ
 
 static void link_pirq_port(int port, struct evtchn *chn, struct vcpu *v)
 {
@@ -580,9 +581,11 @@ static void unlink_pirq_port(struct evtc
             chn->u.pirq.prev_port;
 }
 
+#endif /* CONFIG_HAS_PIRQ */
 
 static int evtchn_bind_pirq(evtchn_bind_pirq_t *bind)
 {
+#ifdef CONFIG_HAS_PIRQ
     struct evtchn *chn;
     struct domain *d = current->domain;
     struct vcpu   *v = d->vcpu[0];
@@ -639,6 +642,9 @@ static int evtchn_bind_pirq(evtchn_bind_
     write_unlock(&d->event_lock);
 
     return rc;
+#else /* !CONFIG_HAS_PIRQ */
+    return -EOPNOTSUPP;
+#endif
 }
 
 
@@ -671,6 +677,7 @@ int evtchn_close(struct domain *d1, int
     case ECS_UNBOUND:
         break;
 
+#ifdef CONFIG_HAS_PIRQ
     case ECS_PIRQ: {
         struct pirq *pirq = pirq_info(d1, chn1->u.pirq.irq);
 
@@ -680,14 +687,13 @@ int evtchn_close(struct domain *d1, int
                 pirq_guest_unbind(d1, pirq);
             pirq->evtchn = 0;
             pirq_cleanup_check(pirq, d1);
-#ifdef CONFIG_X86
             if ( is_hvm_domain(d1) && domain_pirq_to_irq(d1, pirq->pirq) > 0 )
                 unmap_domain_pirq_emuirq(d1, pirq->pirq);
-#endif
         }
         unlink_pirq_port(chn1, d1->vcpu[chn1->notify_vcpu_id]);
         break;
     }
+#endif
 
     case ECS_VIRQ: {
         struct vcpu *v;
@@ -1097,6 +1103,8 @@ int evtchn_bind_vcpu(evtchn_port_t port,
     case ECS_INTERDOMAIN:
         chn->notify_vcpu_id = v->vcpu_id;
         break;
+
+#ifdef CONFIG_HAS_PIRQ
     case ECS_PIRQ:
         if ( chn->notify_vcpu_id == v->vcpu_id )
             break;
@@ -1106,6 +1114,8 @@ int evtchn_bind_vcpu(evtchn_port_t port,
                           cpumask_of(v->processor));
         link_pirq_port(port, chn, v);
         break;
+#endif
+
     default:
         rc = -EINVAL;
         break;
xen/include/xen/sched.h +2 -0
@@ -438,12 +438,14 @@ struct domain
 
     struct grant_table *grant_table;
 
+#ifdef CONFIG_HAS_PIRQ
     /*
      * Interrupt to event-channel mappings and other per-guest-pirq data.
      * Protected by the domain's event-channel spinlock.
      */
     struct radix_tree_root pirq_tree;
     unsigned int     nr_pirqs;
+#endif
 
     unsigned int     options;         /* copy of createdomain flags */
 
#2 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Quoted 6 lines · Jan Beulich
xen/common/domctl.c +0 -0
@@ -683,11 +683,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
         unsigned int pirq = op->u.irq_permission.pirq, irq;
         int allow = op->u.irq_permission.allow_access;
 

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xe · line 695

Quoted 10 lines
xen/common/domctl.c +2 -0
+#ifdef CONFIG_HAS_PIRQ
         if ( pirq >= current->domain->nr_pirqs )
         {
             ret = -EINVAL;
             break;
         }
+#endif
         irq = pirq_access_permitted(current->domain, pirq);
         if ( !irq || xsm_irq_permission(XSM_HOOK, d, irq, allow) )
             ret = -EPERM;

It has occurred to me that in place of adding #ifdef here, we could simply drop that if(), resulting in out-of-range value to also hit the -EPERM path. Would that (a) be acceptable and (b) maybe be preferred for being less code and (more importantly) less #ifdef-ary?

Jan

#3 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

In reply to Jan Beulich
Quoted 4 lines · Jan Beulich
xen/include/xen/sched.h +0 -0
@@ -438,12 +438,14 @@ struct domain
 

xen/include/xen/sched.h struct domain · line 448

Quoted 10 lines
xen/include/xen/sched.h +2 -0
     struct grant_table *grant_table;
 
+#ifdef CONFIG_HAS_PIRQ
     /*
      * Interrupt to event-channel mappings and other per-guest-pirq data.
      * Protected by the domain's event-channel spinlock.
      */
     struct radix_tree_root pirq_tree;
     unsigned int     nr_pirqs;
+#endif

Won't it be cleaner to just move this into arch_domain and avoid a bunch of the ifdefary? As the initialization of the fields would be moved to arch_domain_create() also.

Maybe we would want to introduce some kind of arch-specific event channel handler so that the bind PIRQ hypercall could be handled there?

Thanks, Roger.

#4 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
xen/include/xen/sched.h +0 -0
@@ -438,12 +438,14 @@ struct domain
 
     struct grant_table *grant_table;
 
Quoted 12 lines

xen/include/xen/sched.h struct domain · line 448

Quoted 8 lines
xen/include/xen/sched.h +2 -0
+#ifdef CONFIG_HAS_PIRQ
     /*
      * Interrupt to event-channel mappings and other per-guest-pirq data.
      * Protected by the domain's event-channel spinlock.
      */
     struct radix_tree_root pirq_tree;
     unsigned int     nr_pirqs;
+#endif

Won't it be cleaner to just move this into arch_domain and avoid a bunch of the ifdefary? As the initialization of the fields would be moved to arch_domain_create() also.

That's hard to decide without knowing what e.g. RISC-V is going to want. Taking (past) IA-64 into consideration - that would likely have wanted to select this new HAS_PIRQ, and hence keeping these pieces where they are imo makes sense. I did actually consider that alternative, albeit just briefly. If that ...

Quoted 3 lines

Maybe we would want to introduce some kind of arch-specific event channel handler so that the bind PIRQ hypercall could be handled there?

... and hence this was the route to take, I suppose I would simply drop this patch and revert the 2nd one to what it was before (merely addressing the review comment on Arm's arch_hwdom_irqs()). That's simply more intrusive a change than I'm willing to make right here.

Jan

#5 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Quoted 21 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
xen/include/xen/sched.h +0 -0
@@ -438,12 +438,14 @@ struct domain
 
     struct grant_table *grant_table;
 
Quoted 12 lines

xen/include/xen/sched.h struct domain · line 448

Quoted 8 lines
xen/include/xen/sched.h +2 -0
+#ifdef CONFIG_HAS_PIRQ
     /*
      * Interrupt to event-channel mappings and other per-guest-pirq data.
      * Protected by the domain's event-channel spinlock.
      */
     struct radix_tree_root pirq_tree;
     unsigned int     nr_pirqs;
+#endif

Won't it be cleaner to just move this into arch_domain and avoid a bunch of the ifdefary? As the initialization of the fields would be moved to arch_domain_create() also.

Quoted 4 lines

That's hard to decide without knowing what e.g. RISC-V is going to want. Taking (past) IA-64 into consideration - that would likely have wanted to select this new HAS_PIRQ, and hence keeping these pieces where they are imo makes sense.

I'm kind of confused, what does Arm do here? AFAICT the pirq_tree is used by both PV and HVM guests in order to store the native interrupt -> guest interrupt translation, doesn't Arm also need something similar?

Quoted 11 lines

I did actually consider that
alternative, albeit just briefly. If that ...

Quoted 3 lines

Maybe we would want to introduce some kind of arch-specific event channel handler so that the bind PIRQ hypercall could be handled there?

... and hence this was the route to take, I suppose I would simply drop this patch and revert the 2nd one to what it was before (merely addressing the review comment on Arm's arch_hwdom_irqs()). That's simply more intrusive a change than I'm willing to make right here.

It's hard to tell whether other arches will need that, I think I need to better understand why Arm doesn't before making any judgment myself.

I do think it would be cleaner if the field was moved, but that would require us being quite sure it won't be needed by other arches.

Thanks, Roger.

#6 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Quoted 27 lines · Roger Pau Monné
Quoted 21 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
xen/include/xen/sched.h +0 -0
@@ -438,12 +438,14 @@ struct domain
 
     struct grant_table *grant_table;
 
Quoted 12 lines

xen/include/xen/sched.h struct domain · line 448

Quoted 8 lines
xen/include/xen/sched.h +2 -0
+#ifdef CONFIG_HAS_PIRQ
     /*
      * Interrupt to event-channel mappings and other per-guest-pirq data.
      * Protected by the domain's event-channel spinlock.
      */
     struct radix_tree_root pirq_tree;
     unsigned int     nr_pirqs;
+#endif

Won't it be cleaner to just move this into arch_domain and avoid a bunch of the ifdefary? As the initialization of the fields would be moved to arch_domain_create() also.

Quoted 4 lines

That's hard to decide without knowing what e.g. RISC-V is going to want. Taking (past) IA-64 into consideration - that would likely have wanted to select this new HAS_PIRQ, and hence keeping these pieces where they are imo makes sense.

Quoted 4 lines

I'm kind of confused, what does Arm do here? AFAICT the pirq_tree is used by both PV and HVM guests in order to store the native interrupt -> guest interrupt translation, doesn't Arm also need something similar?

According to [1] they don't, hence the (new in v2) change here. Aiui they simply map IRQ to pIRQ 1:1.

Jan

[1] https://lists.xen.org/archives/html/xen-devel/2023-05/msg00258.html

#7 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Hi,

Quoted 28 lines · Jan Beulich
Quoted 27 lines · Roger Pau Monné
Quoted 25 lines · Jan Beulich
Quoted 7 lines · Roger Pau Monné
Quoted 6 lines · Jan Beulich
xen/include/xen/sched.h +0 -0
@@ -438,12 +438,14 @@ struct domain
 
      struct grant_table *grant_table;
 
Quoted 12 lines

xen/include/xen/sched.h struct domain · line 448

Quoted 8 lines
xen/include/xen/sched.h +2 -0
+#ifdef CONFIG_HAS_PIRQ
      /*
       * Interrupt to event-channel mappings and other per-guest-pirq data.
       * Protected by the domain's event-channel spinlock.
       */
      struct radix_tree_root pirq_tree;
      unsigned int     nr_pirqs;
+#endif

Won't it be cleaner to just move this into arch_domain and avoid a bunch of the ifdefary? As the initialization of the fields would be moved to arch_domain_create() also.

That's hard to decide without knowing what e.g. RISC-V is going to want. Taking (past) IA-64 into consideration - that would likely have wanted to select this new HAS_PIRQ, and hence keeping these pieces where they are imo makes sense.

Quoted 7 lines
Quoted 4 lines

I'm kind of confused, what does Arm do here? AFAICT the pirq_tree is used by both PV and HVM guests in order to store the native interrupt -> guest interrupt translation, doesn't Arm also need something similar?

According to [1] they don't, hence the (new in v2) change here. Aiui they simply map IRQ to pIRQ 1:1.

The vGIC is able to cope with vIRQ != pIRQ. But so far, we only allow a domain to map 1:1.

For that we are storing the pIRQ to vIRQ mapping in the IRQ desc and have a pointer to the desc in the vIRQ information.

So no need to a PIRQ tree on the Arm side.

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

In reply to Jan Beulich

Hi Jan,

Quoted 70 lines · Jan Beulich

... by way of a new arch-selectable Kconfig control.

Note that some smaller pieces of code are left without #ifdef, to keep things better readable. Hence items like ECS_PIRQ, nr_static_irqs, or domain_pirq_to_irq() remain uniformly.

---
I'm not really certain about XEN_DOMCTL_irq_permission: With pIRQ-s not used, the prior pIRQ -> IRQ translation cannot have succeeded on Arm, so quite possibly the entire domctl is unused there? Yet then how is access to particular device IRQs being granted/revoked?
---
v2: New.

docs/misc/xen-command-line.pandoc +1 -1
@@ -1120,7 +1120,7 @@ introduced with the Nehalem architecture
        intended as an emergency option for people who first chose fast, then
        change their minds to secure, and wish not to reboot.**
 
-### extra_guest_irqs
+### extra_guest_irqs (x86)
  > `= [<domU number>][,<dom0 number>]`
 
  > Default: `32,<variable>`
xen/arch/arm/include/asm/irq.h +0 -1
@@ -52,7 +52,6 @@ struct arch_irq_desc {
 
  extern const unsigned int nr_irqs;
  #define nr_static_irqs NR_IRQS
-#define arch_hwdom_irqs(domid) NR_IRQS
 
  struct irq_desc;
  struct irqaction;
xen/arch/x86/Kconfig +1 -0
@@ -25,6 +25,7 @@ config X86
  	select HAS_PCI
  	select HAS_PCI_MSI
  	select HAS_PDX
+	select HAS_PIRQ
  	select HAS_SCHED_GRANULARITY
  	select HAS_UBSAN
  	select HAS_VPCI if HVM
xen/common/Kconfig +3 -0
@@ -56,6 +56,9 @@ config HAS_KEXEC
  config HAS_PDX
  	bool
 
+config HAS_PIRQ
+	bool
+
  config HAS_PMAP
  	bool
 
xen/common/domain.c +2 -0
@@ -350,6 +350,8 @@ static int late_hwdom_init(struct domain
  #endif
  }
 
+#ifdef CONFIG_HAS_PIRQ
+
  static unsigned int __read_mostly extra_hwdom_irqs;
  static unsigned int __read_mostly extra_domU_irqs = 32;
 

xen/common/domain.c static int __init cf_check parse_extra_g · line 369

Quoted 4 lines
xen/common/domain.c +1 -0
@@ -364,6 +366,8 @@ static int __init cf_check parse_extra_g
  }
  custom_param("extra_guest_irqs", parse_extra_guest_irqs);
 
+#endif /* CONFIG_HAS_PIRQ */

NIT: I would suggest create a file pirq.c and move anything PIRQ specific there. This should reduce the number of #ifdef in the code.

Quoted 41 lines
xen/common/domain.c +7 -0
+
  /*
   * Release resources held by a domain.  There may or may not be live
   * references to the domain, and it may or may not be fully constructed.
@@ -653,6 +657,7 @@ struct domain *domain_create(domid_t dom
      if ( is_system_domain(d) && !is_idle_domain(d) )
          return d;
 
+#ifdef CONFIG_HAS_PIRQ
      if ( !is_idle_domain(d) )
      {
          if ( !is_hardware_domain(d) )
@@ -664,6 +669,7 @@ struct domain *domain_create(domid_t dom
 
          radix_tree_init(&d->pirq_tree);
      }
+#endif
 
      if ( (err = arch_domain_create(d, config, flags)) != 0 )
          goto fail;
@@ -755,7 +761,9 @@ struct domain *domain_create(domid_t dom
      {
          evtchn_destroy(d);
          evtchn_destroy_final(d);
+#ifdef CONFIG_HAS_PIRQ
          radix_tree_destroy(&d->pirq_tree, free_pirq_struct);
+#endif
      }
      if ( init_status & INIT_watchdog )
          watchdog_domain_destroy(d);
@@ -1151,7 +1159,9 @@ static void cf_check complete_domain_des
 
      evtchn_destroy_final(d);
 
+#ifdef CONFIG_HAS_PIRQ
      radix_tree_destroy(&d->pirq_tree, free_pirq_struct);
+#endif
 
      xfree(d->vcpu);
 

xen/common/domain.c long do_vm_assist(unsigned int cmd, unsi · line 1878

Quoted 5 lines
xen/common/domain.c +2 -0
@@ -1864,6 +1874,8 @@ long do_vm_assist(unsigned int cmd, unsi
  }
  #endif
 
+#ifdef CONFIG_HAS_PIRQ
+

With this change and some others, we probably can remove alloc_pirq_struct() & co. I will have a look to send a clean-up patch after this goes in.

Quoted 15 lines
xen/common/domain.c +2 -0
  struct pirq *pirq_get_info(struct domain *d, int pirq)
  {
      struct pirq *info = pirq_info(d, pirq);
@@ -1893,6 +1905,8 @@ void cf_check free_pirq_struct(void *ptr
      call_rcu(&pirq->rcu_head, _free_pirq_struct);
  }
 
+#endif /* CONFIG_HAS_PIRQ */
+
  struct migrate_info {
      long (*func)(void *data);
      void *data;

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xe · line 692

Quoted 10 lines
xen/common/domctl.c +2 -0
@@ -683,11 +683,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
          unsigned int pirq = op->u.irq_permission.pirq, irq;
          int allow = op->u.irq_permission.allow_access;
 
+#ifdef CONFIG_HAS_PIRQ
          if ( pirq >= current->domain->nr_pirqs )
          {
              ret = -EINVAL;
              break;
          }
+#endif

This #ifdef reads a little bit strange. If we can get away with the check for Arm, then why can't when CONFIG_HAS_PIRQ=y? Overall, a comment would be helpful.

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xe · line 695

Quoted 3 lines
xen/common/domctl.c +0 -0
          irq = pirq_access_permitted(current->domain, pirq);
          if ( !irq || xsm_irq_permission(XSM_HOOK, d, irq, allow) )
              ret = -EPERM;

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Quoted 5 lines · Julien Grall
Quoted 5 lines

---
I'm not really certain about XEN_DOMCTL_irq_permission: With pIRQ-s not used, the prior pIRQ -> IRQ translation cannot have succeeded on Arm, so quite possibly the entire domctl is unused there? Yet then how is access to particular device IRQs being granted/revoked?

(Leaving this in context, as it'll be relevant for the last comment you gave.)

Quoted 11 lines
Quoted 11 lines
xen/common/domain.c +2 -0
@@ -350,6 +350,8 @@ static int late_hwdom_init(struct domain
  #endif
  }
 
+#ifdef CONFIG_HAS_PIRQ
+
  static unsigned int __read_mostly extra_hwdom_irqs;
  static unsigned int __read_mostly extra_domU_irqs = 32;
 
Quoted 8 lines

xen/common/domain.c static int __init cf_check parse_extra_g · line 369

Quoted 5 lines
xen/common/domain.c +1 -0
@@ -364,6 +366,8 @@ static int __init cf_check parse_extra_g
  }
  custom_param("extra_guest_irqs", parse_extra_guest_irqs);
 
+#endif /* CONFIG_HAS_PIRQ */

NIT: I would suggest create a file pirq.c and move anything PIRQ specific there. This should reduce the number of #ifdef in the code.

I did consider that, but it looked quite a bit more intrusive to me than the few #ifdef-s added to this file. (The ones in other files would be yet uglier to eliminate, if that was to be implied from your remark.)

Quoted 10 lines

xen/common/domain.c long do_vm_assist(unsigned int cmd, unsi · line 1878

Quoted 6 lines
xen/common/domain.c +2 -0
@@ -1864,6 +1874,8 @@ long do_vm_assist(unsigned int cmd, unsi
  }
  #endif
 
+#ifdef CONFIG_HAS_PIRQ
+

With this change and some others, we probably can remove alloc_pirq_struct() & co. I will have a look to send a clean-up patch after this goes in.

Right, there's likely further cleanup possible.

Quoted 6 lines
Quoted 6 lines
xen/common/domctl.c +0 -0
@@ -683,11 +683,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
          unsigned int pirq = op->u.irq_permission.pirq, irq;
          int allow = op->u.irq_permission.allow_access;
 
Quoted 11 lines

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xe · line 692

Quoted 7 lines
xen/common/domctl.c +2 -0
+#ifdef CONFIG_HAS_PIRQ
          if ( pirq >= current->domain->nr_pirqs )
          {
              ret = -EINVAL;
              break;
          }
+#endif

This #ifdef reads a little bit strange. If we can get away with the check for Arm, then why can't when CONFIG_HAS_PIRQ=y? Overall, a comment would be helpful.

As per the post-commit-message remark first of all I need to understand why things were the way they were, and why (whether) that was correct (or at least entirely benign) for Arm in the first place. Only then I'll (hopefully) be in the position of putting a sensible comment here.

One thing is clear, I suppose: Without the #ifdef the code wouldn't build. Yet imo if things all matched up, it should have been buildable either way already in the past. Hence the questions.

Jan

#10 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 5 lines

---
I'm not really certain about XEN_DOMCTL_irq_permission: With pIRQ-s not used, the prior pIRQ -> IRQ translation cannot have succeeded on Arm, so quite possibly the entire domctl is unused there? Yet then how is access to particular device IRQs being granted/revoked?

(Leaving this in context, as it'll be relevant for the last comment you gave.)

Sorry I missed this comment.

Quoted 1 line

so quite possibly the entire domctl is unused there?

You are right, the domctl permission is not used on Arm.

Quoted 1 line

Yet then how is access to particular device IRQs being granted/revoked?

At the moment, a device can only be attached at domain creation and detached when the domain is destroyed. Also, only the toolstack can map IRQs. So we don't need to worry for granting/revoking IRQs.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
xen/common/domain.c +2 -0
@@ -350,6 +350,8 @@ static int late_hwdom_init(struct domain
   #endif
   }
 
+#ifdef CONFIG_HAS_PIRQ
+
   static unsigned int __read_mostly extra_hwdom_irqs;
   static unsigned int __read_mostly extra_domU_irqs = 32;
 
Quoted 8 lines

xen/common/domain.c static int __init cf_check parse_extra_g · line 369

Quoted 5 lines
xen/common/domain.c +1 -0
@@ -364,6 +366,8 @@ static int __init cf_check parse_extra_g
   }
   custom_param("extra_guest_irqs", parse_extra_guest_irqs);
 
+#endif /* CONFIG_HAS_PIRQ */

NIT: I would suggest create a file pirq.c and move anything PIRQ specific there. This should reduce the number of #ifdef in the code.

Quoted 4 lines

I did consider that, but it looked quite a bit more intrusive to me than the few #ifdef-s added to this file. (The ones in other files would be yet uglier to eliminate, if that was to be implied from your remark.)

I was under the impression we wanted to limit the number of #ifdef in the common code. They didn't seem to be warrant here and, from a brief look, it didn't seem the option would be that "ugly". But I guess this is a matter of taste.

Quoted 32 lines
Quoted 11 lines

xen/common/domain.c long do_vm_assist(unsigned int cmd, unsi · line 1878

Quoted 7 lines
xen/common/domain.c +2 -0
@@ -1864,6 +1874,8 @@ long do_vm_assist(unsigned int cmd, unsi
   }
   #endif
 
+#ifdef CONFIG_HAS_PIRQ
+

With this change and some others, we probably can remove alloc_pirq_struct() & co. I will have a look to send a clean-up patch after this goes in.

Right, there's likely further cleanup possible.

Quoted 6 lines
Quoted 6 lines
xen/common/domctl.c +0 -0
@@ -683,11 +683,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
           unsigned int pirq = op->u.irq_permission.pirq, irq;
           int allow = op->u.irq_permission.allow_access;
 
Quoted 11 lines

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xe · line 692

Quoted 7 lines
xen/common/domctl.c +2 -0
+#ifdef CONFIG_HAS_PIRQ
           if ( pirq >= current->domain->nr_pirqs )
           {
               ret = -EINVAL;
               break;
           }
+#endif

This #ifdef reads a little bit strange. If we can get away with the check for Arm, then why can't when CONFIG_HAS_PIRQ=y? Overall, a comment would be helpful.

Quoted 8 lines

As per the post-commit-message remark first of all I need to understand why things were the way they were, and why (whether) that was correct (or at least entirely benign) for Arm in the first place. Only then I'll (hopefully) be in the position of putting a sensible comment here.

One thing is clear, I suppose: Without the #ifdef the code wouldn't build. Yet imo if things all matched up, it should have been buildable either way already in the past. Hence the questions.

Right, it would not build. But does this check really matter even in the case where CONFIG_HAS_PIRQ=y? Looking at the code, it sounds like more an optimization/a way to return a different error code if there value is too high. For the domctl, it doesn't seem to be worth it, the more if we need to add #ifdef.

With that, the rest of the domctl should mostly work for Arm.

Cheers,

Signature · 1 line
Julien Grall
#11 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Quoted 15 lines · Julien Grall
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 5 lines

---
I'm not really certain about XEN_DOMCTL_irq_permission: With pIRQ-s not used, the prior pIRQ -> IRQ translation cannot have succeeded on Arm, so quite possibly the entire domctl is unused there? Yet then how is access to particular device IRQs being granted/revoked?

(Leaving this in context, as it'll be relevant for the last comment you gave.)

Sorry I missed this comment.

Quoted 1 line

so quite possibly the entire domctl is unused there?

Quoted 7 lines

You are right, the domctl permission is not used on Arm.

Quoted 1 line

Yet then how is access to particular device IRQs being granted/revoked?

At the moment, a device can only be attached at domain creation and detached when the domain is destroyed. Also, only the toolstack can map IRQs. So we don't need to worry for granting/revoking IRQs.

Thanks for clarifying.

Quoted 27 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
xen/common/domctl.c +0 -0
@@ -683,11 +683,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
           unsigned int pirq = op->u.irq_permission.pirq, irq;
           int allow = op->u.irq_permission.allow_access;
 
Quoted 11 lines

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xe · line 692

Quoted 7 lines
xen/common/domctl.c +2 -0
+#ifdef CONFIG_HAS_PIRQ
           if ( pirq >= current->domain->nr_pirqs )
           {
               ret = -EINVAL;
               break;
           }
+#endif

This #ifdef reads a little bit strange. If we can get away with the check for Arm, then why can't when CONFIG_HAS_PIRQ=y? Overall, a comment would be helpful.

Quoted 8 lines

As per the post-commit-message remark first of all I need to understand why things were the way they were, and why (whether) that was correct (or at least entirely benign) for Arm in the first place. Only then I'll (hopefully) be in the position of putting a sensible comment here.

One thing is clear, I suppose: Without the #ifdef the code wouldn't build. Yet imo if things all matched up, it should have been buildable either way already in the past. Hence the questions.

Quoted 5 lines

Right, it would not build. But does this check really matter even in the case where CONFIG_HAS_PIRQ=y? Looking at the code, it sounds like more an optimization/a way to return a different error code if there value is too high. For the domctl, it doesn't seem to be worth it, the more if we need to add #ifdef.

I wouldn't call it an optimization. The check has always been there, with b72cea07db32 transforming it (largely) into what we have today. In fact in an initial attempt I had gone further and also #ifdef-ed out the pirq_access_permitted() (and iirc the pirq variable altogether), seeing that without HAS_PIRQ the incoming field can only sensibly hold an IRQ. But then I thought that this would be going too far, leading to me undoing part of the change.

If we dropped the check, we'd start relying on domain_pirq_to_irq() (invoked by pirq_access_permitted()) to always fail cleanly for an out- of-range input. While I think that holds, it would still feel a little like making the code (slightly) less robust. But yes, I think doing so would be an option. (Still I also think that returning EINVAL for obviously out-of-range values is somewhat better than EPERM.) Yet then
...

Quoted 1 line

With that, the rest of the domctl should mostly work for Arm.

..., taking into account also your clarification at the top, I wonder whether we shouldn't #ifdef out the entire subop.

Jan

#12 lore raw

Re: [PATCH v2 1/2] restrict concept of pIRQ to x86

Quoted 75 lines · Jan Beulich
Quoted 15 lines · Julien Grall
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 5 lines

---
I'm not really certain about XEN_DOMCTL_irq_permission: With pIRQ-s not used, the prior pIRQ -> IRQ translation cannot have succeeded on Arm, so quite possibly the entire domctl is unused there? Yet then how is access to particular device IRQs being granted/revoked?

(Leaving this in context, as it'll be relevant for the last comment you gave.)

Sorry I missed this comment.

Quoted 1 line

so quite possibly the entire domctl is unused there?

Quoted 7 lines

You are right, the domctl permission is not used on Arm.

Quoted 1 line

Yet then how is access to particular device IRQs being granted/revoked?

At the moment, a device can only be attached at domain creation and detached when the domain is destroyed. Also, only the toolstack can map IRQs. So we don't need to worry for granting/revoking IRQs.

Thanks for clarifying.

Quoted 27 lines
Quoted 18 lines
Quoted 6 lines
Quoted 6 lines
xen/common/domctl.c +0 -0
@@ -683,11 +683,13 @@ long do_domctl(XEN_GUEST_HANDLE_PARAM(xe
           unsigned int pirq = op->u.irq_permission.pirq, irq;
           int allow = op->u.irq_permission.allow_access;
 
Quoted 11 lines

xen/common/domctl.c long do_domctl(XEN_GUEST_HANDLE_PARAM(xe · line 692

Quoted 7 lines
xen/common/domctl.c +2 -0
+#ifdef CONFIG_HAS_PIRQ
           if ( pirq >= current->domain->nr_pirqs )
           {
               ret = -EINVAL;
               break;
           }
+#endif

This #ifdef reads a little bit strange. If we can get away with the check for Arm, then why can't when CONFIG_HAS_PIRQ=y? Overall, a comment would be helpful.

Quoted 8 lines

As per the post-commit-message remark first of all I need to understand why things were the way they were, and why (whether) that was correct (or at least entirely benign) for Arm in the first place. Only then I'll (hopefully) be in the position of putting a sensible comment here.

One thing is clear, I suppose: Without the #ifdef the code wouldn't build. Yet imo if things all matched up, it should have been buildable either way already in the past. Hence the questions.

Quoted 5 lines

Right, it would not build. But does this check really matter even in the case where CONFIG_HAS_PIRQ=y? Looking at the code, it sounds like more an optimization/a way to return a different error code if there value is too high. For the domctl, it doesn't seem to be worth it, the more if we need to add #ifdef.

I wouldn't call it an optimization. The check has always been there, with b72cea07db32 transforming it (largely) into what we have today. In fact in an initial attempt I had gone further and also #ifdef-ed out the pirq_access_permitted() (and iirc the pirq variable altogether), seeing that without HAS_PIRQ the incoming field can only sensibly hold an IRQ. But then I thought that this would be going too far, leading to me undoing part of the change.

If we dropped the check, we'd start relying on domain_pirq_to_irq() (invoked by pirq_access_permitted()) to always fail cleanly for an out- of-range input. While I think that holds, it would still feel a little like making the code (slightly) less robust. But yes, I think doing so would be an option. (Still I also think that returning EINVAL for obviously out-of-range values is somewhat better than EPERM.) Yet then
...

Quoted 4 lines
Quoted 1 line

With that, the rest of the domctl should mostly work for Arm.

..., taking into account also your clarification at the top, I wonder whether we shouldn't #ifdef out the entire subop.

The more I think about it, the better this option looks to me. libxl doesn't use the sub-op for Arm (from all I can tell), so the only worrying in-tree parts are that the libxc functions is exposed both via the Python and OCamL bindings (without there being an in-tree consumer, again from all I can tell).

Since I'd like to get v3 out (first and foremost because meanwhile I've also found bugs fixing of which preferably would take this change as a prereq, or else the build would break on Arm afaict), I'd appreciate feedback (of course also from anyone else on the Cc list). I guess unless I hear otherwise, I'll make the change.

Jan

#13 lore raw

PATCH v2 2/2 cmdline: document and enforce "extra_guest_irqs" upper bounds

In reply to Jan Beulich

PHYSDEVOP_pirq_eoi_gmfn_v<N> accepting just a single GFN implies that no more than 32k pIRQ-s can be used by a domain on x86. Document this upper bound.

To also enforce the limit, (ab)use both arch_hwdom_irqs() (changing its parameter type) and setup_system_domains(). This is primarily to avoid exposing the two static variables or introducing yet further arch hooks.

---
Instead of passing dom_xen into arch_hwdom_irqs(), NULL could also be used. That would make the connection to setup_system_domains() yet more weak, though.

Passing the domain pointer instead of the domain ID would also allow to return a possibly different value if sensible for PVH Dom0 (which presently has no access to PHYSDEVOP_pirq_eoi_gmfn_v<N> in the first place).
---
v2: Also enforce these bounds. Adjust doc to constrain the bound to x86
only. Re-base over new earlier patch.

docs/misc/xen-command-line.pandoc +2 -1
@@ -1130,7 +1130,8 @@ common for all domUs, while the optional
 is for dom0.  Changing the setting for domU has no impact on dom0 and vice
 versa.  For example to change dom0 without changing domU, use
 `extra_guest_irqs=,512`.  The default value for Dom0 and an eventual separate
-hardware domain is architecture dependent.
+hardware domain is architecture dependent.  The upper limit for both values on
+x86 is such that the resulting total number of IRQs can't be higher than 32768.
 Note that specifying zero as domU value means zero, while for dom0 it means
 to use the default.
 
xen/arch/x86/io_apic.c +6 -3
@@ -2665,18 +2665,21 @@ void __init ioapic_init(void)
            nr_irqs_gsi, nr_irqs - nr_irqs_gsi);
 }
 
-unsigned int arch_hwdom_irqs(domid_t domid)
+unsigned int arch_hwdom_irqs(const struct domain *d)
 {
     unsigned int n = fls(num_present_cpus());
 
-    if ( !domid )
+    if ( is_system_domain(d) )
+        return PAGE_SIZE * BITS_PER_BYTE;
+
+    if ( !d->domain_id )
         n = min(n, dom0_max_vcpus());
     n = min(nr_irqs_gsi + n * NR_DYNAMIC_VECTORS, nr_irqs);
 
     /* Bounded by the domain pirq eoi bitmap gfn. */
     n = min_t(unsigned int, n, PAGE_SIZE * BITS_PER_BYTE);
 
-    printk("Dom%d has maximum %u PIRQs\n", domid, n);
+    printk("%pd has maximum %u PIRQs\n", d, n);
 
     return n;
 }
xen/common/domain.c +19 -1
@@ -664,7 +664,7 @@ struct domain *domain_create(domid_t dom
             d->nr_pirqs = nr_static_irqs + extra_domU_irqs;
         else
             d->nr_pirqs = extra_hwdom_irqs ? nr_static_irqs + extra_hwdom_irqs
-                                           : arch_hwdom_irqs(domid);
+                                           : arch_hwdom_irqs(d);
         d->nr_pirqs = min(d->nr_pirqs, nr_irqs);
 
         radix_tree_init(&d->pirq_tree);
@@ -790,6 +790,24 @@ void __init setup_system_domains(void)
     if ( IS_ERR(dom_xen) )
         panic("Failed to create d[XEN]: %ld\n", PTR_ERR(dom_xen));
 
+#ifdef CONFIG_HAS_PIRQ
+    /* Bound-check values passed via "extra_guest_irqs=". */
+    {
+        unsigned int n = max(arch_hwdom_irqs(dom_xen), nr_static_irqs);
+
+        if ( extra_hwdom_irqs > n - nr_static_irqs )
+        {
+            extra_hwdom_irqs = n - nr_static_irqs;
+            printk(XENLOG_WARNING "hwdom IRQs bounded to %u\n", n);
+        }
+        if ( extra_domU_irqs > max(32U, n - nr_static_irqs) )
+        {
+            extra_domU_irqs = n - nr_static_irqs;
+            printk(XENLOG_WARNING "domU IRQs bounded to %u\n", n);
+        }
+    }
+#endif
+
     /*
      * Initialise our DOMID_IO domain.
      * This domain owns I/O pages that are within the range of the page_info
xen/include/xen/irq.h +2 -1
@@ -173,8 +173,9 @@ extern irq_desc_t *pirq_spin_lock_irq_de
 
 unsigned int set_desc_affinity(struct irq_desc *, const cpumask_t *);
 
+/* When passed a system domain, this returns the maximum permissible value. */
 #ifndef arch_hwdom_irqs
-unsigned int arch_hwdom_irqs(domid_t);
+unsigned int arch_hwdom_irqs(const struct domain *);
 #endif
 
 #ifndef arch_evtchn_bind_pirq
#14 lore raw

Ping: [PATCH v2 0/2] new CONFIG_HAS_PIRQ and extra_guest_irqs adjustment

In reply to Jan Beulich
Quoted 5 lines · Jan Beulich

The 1st patch (new in v2) has the effect of the 2nd one no longer affecting Arm.

1: restrict concept of pIRQ to x86
2: cmdline: document and enforce "extra_guest_irqs" upper bounds

REST- and Arm-maintainers,

may I please ask for some feedback here? Roger did supply some, resulting in me considering to revert back to the earlier version (i.e. patch 1 dropped again and patch 2 minimally adjusted to address feedback there), but which route to go wants input from other maintainers. What I think is unacceptable is that we continue to not document and enforce the upper bound, leading to people needlessly running into issues on big enough systems.

Just to clarify, "the earlier version" was mistakenly not tagged v2, so what is meant is the 2nd submission ('cmdline: document and enforce "extra_guest_irqs" upper bounds'), not the 1st one ('cmdline: document "extra_guest_irqs" upper bound').

Jan

#15 lore raw

Re: Ping: [PATCH v2 0/2] new CONFIG_HAS_PIRQ and extra_guest_irqs adjustment

Hi Jan,

Quoted 9 lines · Jan Beulich
Quoted 5 lines · Jan Beulich

The 1st patch (new in v2) has the effect of the 2nd one no longer affecting Arm.

1: restrict concept of pIRQ to x86
2: cmdline: document and enforce "extra_guest_irqs" upper bounds

REST- and Arm-maintainers,

Quoted 7 lines

may I please ask for some feedback here? Roger did supply some, resulting in me considering to revert back to the earlier version (i.e. patch 1 dropped again and patch 2 minimally adjusted to address feedback there), but which route to go wants input from other maintainers. What I think is unacceptable is that we continue to not document and enforce the upper bound, leading to people needlessly running into issues on big enough systems.

I looked at patch #1, I think this the right approach for Arm. Do you need further input?

Cheers,

Signature · 1 line
Julien Grall
#16 lore raw

Re: Ping: [PATCH v2 0/2] new CONFIG_HAS_PIRQ and extra_guest_irqs adjustment

Quoted 18 lines · Julien Grall
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Jan Beulich

The 1st patch (new in v2) has the effect of the 2nd one no longer affecting Arm.

1: restrict concept of pIRQ to x86
2: cmdline: document and enforce "extra_guest_irqs" upper bounds

REST- and Arm-maintainers,

Quoted 7 lines

may I please ask for some feedback here? Roger did supply some, resulting in me considering to revert back to the earlier version (i.e. patch 1 dropped again and patch 2 minimally adjusted to address feedback there), but which route to go wants input from other maintainers. What I think is unacceptable is that we continue to not document and enforce the upper bound, leading to people needlessly running into issues on big enough systems.

Quoted 2 lines

I looked at patch #1, I think this the right approach for Arm. Do you need further input?

I guess I'll reply to your comments on the patch itself. Feedback there was what I was pinging for. So thanks for getting back.

Jan