x86/irq: cleanup use of open-coded values
cover x86/irq: cleanup use of open-coded values
Remove uses of open-coded value 16 in x86's interrupt management code to enhance readability.
Patch 0 makes a cosmetic rename NR_ISAIRQS -> NR_ISA_IRQS as per code review [1].
Patch 1 makes use of NR_ISA_IRQS in the code where necessary.
Patch 2 adds new symbol APIC_VECTOR_VALID and makes use of it.
No functional changes.
[1]: https://lore.kernel.org/xen-devel/[email protected]/
Denis Mukhin (3):
x86/irq: rename NR_ISAIRQS to NR_ISA_IRQS x86/irq: use NR_ISA_IRQS instead of open-coded value x86/irq: introduce APIC_VECTOR_VALID
xen/arch/x86/cpu/mcheck/mce_intel.c | 2 +- xen/arch/x86/hvm/dm.c | 2 +- xen/arch/x86/hvm/hvm.c | 4 ++-- xen/arch/x86/hvm/irq.c | 17 +++++++++-------- xen/arch/x86/hvm/vlapic.c | 10 +++++----- xen/arch/x86/hvm/vpic.c | 4 ++-- xen/arch/x86/hvm/vpt.c | 2 +- xen/arch/x86/include/asm/apicdef.h | 1 + xen/arch/x86/include/asm/hvm/irq.h | 4 ++-- xen/arch/x86/include/asm/irq.h | 2 +- xen/arch/x86/io_apic.c | 12 ++++++------ xen/arch/x86/irq.c | 6 +++--- xen/arch/x86/physdev.c | 2 +- xen/drivers/passthrough/x86/hvm.c | 4 ++-- 14 files changed, 37 insertions(+), 35 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/3 x86/irq: rename NR_ISAIRQS to NR_ISA_IRQS
Rename NR_ISAIRQS to NR_ISA_IRQS to enhance readability.
No functional changes.
--- xen/arch/x86/hvm/hvm.c | 4 ++-- xen/arch/x86/hvm/vpt.c | 2 +- xen/arch/x86/include/asm/hvm/irq.h | 4 ++-- xen/arch/x86/physdev.c | 2 +- xen/drivers/passthrough/x86/hvm.c | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -2
@@ -633,8 +633,8 @@ int hvm_domain_initialise(struct domain *d,/* Set the number of GSIs */ hvm_domain_irq (d)->nr_gsis = nr_gsis; -BUILD_BUG_ON (NR_HVM_DOMU_IRQS <NR_ISAIRQS ); -ASSERT (hvm_domain_irq (d)->nr_gsis >=NR_ISAIRQS ); +BUILD_BUG_ON (NR_HVM_DOMU_IRQS <NR_ISA_IRQS ); +ASSERT (hvm_domain_irq (d)->nr_gsis >=NR_ISA_IRQS );/* need link to containing domain */ d->arch.hvm.pl_time->domain = d;
xen/arch/x86/hvm/vpt.c +1 -1
@@ -491,7 +491,7 @@ void create_periodic_time( uint64_t period, uint8_t irq, time_cb *cb, void *data, bool level) { if ( !pt->source || - (irq >= NR_ISAIRQS && pt->source == PTSRC_isa) || + (irq >= NR_ISA_IRQS && pt->source == PTSRC_isa) || (level && period) || (pt->source == PTSRC_ioapic ? irq >= hvm_domain_irq(v->domain)->nr_gsis : level) )
xen/arch/x86/include/asm/hvm/irq.h +2 -2
@@ -134,7 +134,7 @@ struct hvm_girq_dpci_mapping {uint8_t machine_gsi; }; -#define NR_ISAIRQS 16 +#define NR_ISA_IRQS 16#define NR_LINK 4#define NR_HVM_DOMU_IRQS ARRAY_SIZE(((struct hvm_hw_vioapic *)NULL)->redirtbl)
@@ -143,7 +143,7 @@ struct hvm_irq_dpci {/* Guest IRQ to guest device/intx mapping. */ struct list_head girq[NR_HVM_DOMU_IRQS ];/* Record of mapped ISA IRQs */ -DECLARE_BITMAP (isairq_map,NR_ISAIRQS ); +DECLARE_BITMAP (isairq_map,NR_ISA_IRQS );/* Record of mapped Links */ uint8_t link_cnt[NR_LINK ]; };
xen/arch/x86/physdev.c +1 -1
@@ -198,7 +198,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)/* if this is a level irq and count > 0, send another * notification */ -if ( gsi >=NR_ISAIRQS /* ISA irqs are edge triggered */ +if ( gsi >=NR_ISA_IRQS /* ISA irqs are edge triggered */ && hvm_irq->gsi_assert_count[gsi] )send_guest_pirq (currd, pirq); }
xen/drivers/passthrough/x86/hvm.c +2 -2
@@ -958,7 +958,7 @@ static void hvm_dpci_isairq_eoi(struct domain *d, unsigned int isairq) {const struct hvm_irq_dpci *dpci =NULL ; -ASSERT (isairq <NR_ISAIRQS ); +ASSERT (isairq <NR_ISA_IRQS );if ( !is_iommu_enabled (d) )return ;
@@ -991,7 +991,7 @@ void hvm_dpci_eoi(struct domain *d, unsigned int guest_gsi) goto unlock; } -if ( guest_gsi <NR_ISAIRQS ) +if ( guest_gsi <NR_ISA_IRQS ) {hvm_dpci_isairq_eoi (d, guest_gsi);return ;
Signature · 1 line
2.34.1
Re: [PATCH 1/3] x86/irq: rename NR_ISAIRQS to NR_ISA_IRQS
Quoted 3 lines · On 15.03.2025 02:00, [email protected] wrote:
Rename
NR_ISAIRQStoNR_ISA_IRQSto enhance readability.No functional changes.
Quoted 1 line
Signed-off-by Denis Mukhin <[email protected]>
PATCH 2/3 x86/irq: use NR_ISA_IRQS instead of open-coded value
In reply to [email protected]Replace the open-coded value 16 with the NR_ISA_IRQS symbol to enhance readability.
No functional changes.
--- xen/arch/x86/hvm/dm.c | 2 +- xen/arch/x86/hvm/irq.c | 17 +++++++++-------- xen/arch/x86/hvm/vpic.c | 4 ++-- xen/arch/x86/include/asm/irq.h | 2 +- xen/arch/x86/io_apic.c | 12 ++++++------ xen/arch/x86/irq.c | 6 +++--- 6 files changed, 22 insertions(+), 21 deletions(-)
xen/arch/x86/hvm/dm.c +1 -1
@@ -90,7 +90,7 @@ static int set_pci_intx_level(struct domain *d, uint16_t domain,static int set_isa_irq_level (struct domain *d,uint8_t isa_irq,uint8_t level) { -if ( isa_irq >15 ) +if ( isa_irq >=NR_ISA_IRQS )return -EINVAL ; switch ( level )
xen/arch/x86/hvm/irq.c +9 -8
@@ -209,7 +209,7 @@ int hvm_isa_irq_assert(struct domain *d, unsigned int isa_irq,unsigned int gsi =hvm_isa_irq_to_gsi (isa_irq);int vector =-1 ; -ASSERT (isa_irq <=15 ); +ASSERT (isa_irq <NR_ISA_IRQS );spin_lock (&d->arch.hvm.irq_lock);
@@ -231,7 +231,7 @@ void hvm_isa_irq_deassert(struct hvm_irq *hvm_irq =hvm_domain_irq (d);unsigned int gsi =hvm_isa_irq_to_gsi (isa_irq); -ASSERT (isa_irq <=15 ); +ASSERT (isa_irq <NR_ISA_IRQS );spin_lock (&d->arch.hvm.irq_lock);
@@ -266,12 +266,12 @@ static void hvm_set_callback_irq_level(struct vcpu *v)if ( asserted && (hvm_irq->gsi_assert_count[gsi]++ ==0 ) ) {vioapic_irq_positive_edge (d, gsi); -if ( gsi <=15 ) +if ( gsi <NR_ISA_IRQS )vpic_irq_positive_edge (d, gsi); }else if ( !asserted && (--hvm_irq->gsi_assert_count[gsi] ==0 ) ) { -if ( gsi <=15 ) +if ( gsi <NR_ISA_IRQS )vpic_irq_negative_edge (d, gsi); }break ;
@@ -328,7 +328,7 @@ int hvm_set_pci_link_route(struct domain *d, u8 link, u8 isa_irq)u8 old_isa_irq;int i; -if ( (link >3 ) || (isa_irq >15 ) ) +if ( (link >3 ) || (isa_irq >=NR_ISA_IRQS ) )return -EINVAL ;spin_lock (&d->arch.hvm.irq_lock);
@@ -440,7 +440,8 @@ void hvm_set_callback_via(struct domain *d, uint64_t via) {case HVMIRQ_callback_gsi: gsi = hvm_irq->callback_via.gsi; -if ( (--hvm_irq->gsi_assert_count[gsi] ==0 ) && (gsi <=15 ) ) +if ( (--hvm_irq->gsi_assert_count[gsi] ==0 ) && + (gsi <NR_ISA_IRQS ) )vpic_irq_negative_edge (d, gsi);break ;case HVMIRQ_callback_pci_intx:
@@ -464,7 +465,7 @@ void hvm_set_callback_via(struct domain *d, uint64_t via) (hvm_irq->gsi_assert_count[gsi]++ == 0) ) {vioapic_irq_positive_edge (d, gsi); -if ( gsi <=15 ) +if ( gsi <NR_ISA_IRQS )vpic_irq_positive_edge (d, gsi); }break ;
@@ -764,7 +765,7 @@ static int cf_check irq_check_link(const struct domain *d,return -EINVAL ;for ( link =0 ; link <ARRAY_SIZE (pci_link->route); link++ ) -if ( pci_link->route[link] >15 ) +if ( pci_link->route[link] >=NR_ISA_IRQS ) { printk(XENLOG_G_ERR "HVM restore: PCI-ISA link %u out of range (%u)\n",
xen/arch/x86/hvm/vpic.c +2 -2
@@ -523,7 +523,7 @@ void vpic_irq_positive_edge(struct domain *d, int irq)uint8_t mask =1 << (irq &7 );ASSERT (has_vpic (d)); -ASSERT (irq <=15 ); +ASSERT (irq <NR_ISA_IRQS );ASSERT (vpic_is_locked (vpic));TRACE_TIME (TRC_HVM_EMUL_PIC_POSEDGE , irq);
@@ -541,7 +541,7 @@ void vpic_irq_negative_edge(struct domain *d, int irq)uint8_t mask =1 << (irq &7 );ASSERT (has_vpic (d)); -ASSERT (irq <=15 ); +ASSERT (irq <NR_ISA_IRQS );ASSERT (vpic_is_locked (vpic));TRACE_TIME (TRC_HVM_EMUL_PIC_NEGEDGE , irq);
xen/arch/x86/include/asm/irq.h +1 -1
@@ -108,7 +108,7 @@ extern bool opt_noirqbalance;extern int opt_irq_vector_map; -#define platform_legacy_irq (irq) ((irq) < 16) +#define platform_legacy_irq (irq) ((irq) < NR_ISA_IRQS) void cf_check event_check_interrupt(void); void cf_check invalidate_interrupt(void);
xen/arch/x86/io_apic.c +6 -6
@@ -2715,15 +2715,15 @@ void __init ioapic_init(void) " than \"nr_irqs=\"\n"); max_gsi_irqs = nr_irqs; } -if ( max_gsi_irqs <16 ) - max_gsi_irqs =16 ; +if ( max_gsi_irqs <NR_ISA_IRQS ) + max_gsi_irqs =NR_ISA_IRQS ;/* for PHYSDEVOP_pirq_eoi_gmfn guest assumptions */ if ( max_gsi_irqs >PAGE_SIZE *8 ) max_gsi_irqs =PAGE_SIZE *8 ; -if ( !smp_found_config || skip_ioapic_setup || nr_irqs_gsi <16 ) - nr_irqs_gsi =16 ; +if ( !smp_found_config || skip_ioapic_setup || nr_irqs_gsi <NR_ISA_IRQS ) + nr_irqs_gsi =NR_ISA_IRQS ;else if ( nr_irqs_gsi > max_gsi_irqs ) { printk(XENLOG_WARNING "Limiting to %u GSI IRQs (found %u)\n",
@@ -2736,8 +2736,8 @@ void __init ioapic_init(void)max (0U +num_present_cpus () *NR_DYNAMIC_VECTORS , 8 * nr_irqs_gsi) : nr_irqs_gsi; - else if ( nr_irqs < 16 ) - nr_irqs =16 ; + else if ( nr_irqs < NR_ISA_IRQS ) + nr_irqs =NR_ISA_IRQS ;printk (XENLOG_INFO "IRQ limits: %u GSI, %u MSI/MSI-X\n" , nr_irqs_gsi, nr_irqs - nr_irqs_gsi); }
xen/arch/x86/irq.c +3 -3
@@ -36,7 +36,7 @@bool __read_mostly opt_noirqbalance;boolean_param ("noirqbalance" , opt_noirqbalance); -unsigned int __read_mostly nr_irqs_gsi =16 ; +unsigned int __read_mostly nr_irqs_gsi =NR_ISA_IRQS ;unsigned int __read_mostly nr_irqs;integer_param ("nr_irqs" , nr_irqs);
@@ -1525,7 +1525,7 @@ void desc_guest_eoi(struct irq_desc *desc, struct pirq *pirq) int pirq_guest_unmask(struct domain *d) { unsigned int pirq = 0, n, i; - struct pirq *pirqs[16]; + struct pirq *pirqs[NR_ISA_IRQS]; do { n = radix_tree_gang_lookup(&d->pirq_tree, (void **)pirqs, pirq,
@@ -2113,7 +2113,7 @@ int get_free_pirq(struct domain *d, int type)if ( type ==MAP_PIRQ_TYPE_GSI ) { -for ( i =16 ; i < nr_irqs_gsi; i++ ) +for ( i =NR_ISA_IRQS ; i < nr_irqs_gsi; i++ )if (is_free_pirq (d,pirq_info (d, i)) ) {pirq_get_info (d, i);
Signature · 1 line
2.34.1
Re: [PATCH 2/3] x86/irq: use NR_ISA_IRQS instead of open-coded value
Quoted 6 lines · On 15.03.2025 02:00, [email protected] wrote:
Replace the open-coded value 16 with the
NR_ISA_IRQSsymbol to enhance readability.No functional changes.
Signed-off-by Denis Mukhin <[email protected]>
PATCH 3/3 x86/irq: introduce APIC_VECTOR_VALID
In reply to [email protected]Add new symbol APIC_VECTOR_VALID to replace open-coded value 16 in LAPIC and virtual LAPIC code.
See:
Intel SDM volume 3A
Chapter "ADVANCED PROGRAMMABLE INTERRUPT CONTROLLER" Section "Valid Interrupt Vectors"
No functional change.
--- xen/arch/x86/cpu/mcheck/mce_intel.c | 2 +- xen/arch/x86/hvm/vlapic.c | 10 +++++----- xen/arch/x86/include/asm/apicdef.h | 1 + 3 files changed, 7 insertions(+), 6 deletions(-)
xen/arch/x86/cpu/mcheck/mce_intel.c +1 -1
@@ -136,7 +136,7 @@ static void intel_init_thermal(struct cpuinfo_x86 *c)* is required to set the same value for all threads/cores). */ if ( (val &APIC_DM_MASK ) !=APIC_DM_FIXED - || (val &APIC_VECTOR_MASK ) >0xf ) + || (val &APIC_VECTOR_MASK ) >APIC_VECTOR_VALID )apic_write (APIC_LVTTHMR , val); if ( (msr_content & (1ULL<<3))
xen/arch/x86/hvm/vlapic.c +5 -5
@@ -123,7 +123,7 @@ static void vlapic_error(struct vlapic *vlapic, unsigned int err_bit)* will end up back here. Break the cycle by only injecting LVTERR * if it will succeed, and folding in RECVILL otherwise. */ -if ( (lvterr &APIC_VECTOR_MASK ) >=16 ) +if ( (lvterr &APIC_VECTOR_MASK ) >=APIC_VECTOR_VALID ) inj = true;else set_bit (ilog2 (APIC_ESR_RECVILL ), &vlapic->hw.pending_esr);
@@ -136,7 +136,7 @@ static void vlapic_error(struct vlapic *vlapic, unsigned int err_bit)bool vlapic_test_irq (const struct vlapic *vlapic,uint8_t vec) { -if (unlikely (vec <16 ) ) +if (unlikely (vec <APIC_VECTOR_VALID ) )return false; if ( hvm_funcs.test_pir &&
@@ -150,7 +150,7 @@ void vlapic_set_irq(struct vlapic *vlapic, uint8_t vec, uint8_t trig) {struct vcpu *target =vlapic_vcpu (vlapic); -if (unlikely (vec <16 ) ) +if (unlikely (vec <APIC_VECTOR_VALID ) ) {vlapic_error (vlapic,ilog2 (APIC_ESR_RECVILL ));return ;
@@ -523,7 +523,7 @@ void vlapic_ipi(struct vlapic *target =vlapic_lowest_prio (vlapic_domain (vlapic), vlapic, short_hand, dest, dest_mode); -if (unlikely ((icr_low &APIC_VECTOR_MASK ) <16 ) ) +if (unlikely ((icr_low &APIC_VECTOR_MASK ) <APIC_VECTOR_VALID ) )vlapic_error (vlapic,ilog2 (APIC_ESR_SENDILL ));else if ( target )vlapic_accept_irq (vlapic_vcpu (target), icr_low);
@@ -531,7 +531,7 @@ void vlapic_ipi( }case APIC_DM_FIXED : -if (unlikely ((icr_low &APIC_VECTOR_MASK ) <16 ) ) +if (unlikely ((icr_low &APIC_VECTOR_MASK ) <APIC_VECTOR_VALID ) ) {vlapic_error (vlapic,ilog2 (APIC_ESR_SENDILL ));break ;
xen/arch/x86/include/asm/apicdef.h +1 -0
@@ -78,6 +78,7 @@#define APIC_DM_STARTUP 0x00600#define APIC_DM_EXTINT 0x00700#define APIC_VECTOR_MASK 0x000FF +#define APIC_VECTOR_VALID (16)#define APIC_ICR2 0x310#define GET_xAPIC_DEST_FIELD (x) (((x)>>24)&0xFF)#define SET_xAPIC_DEST_FIELD (x) ((x)<<24)
Signature · 1 line
2.34.1
Re: [PATCH 3/3] x86/irq: introduce APIC_VECTOR_VALID
Quoted 2 lines · On 15.03.2025 02:00, [email protected] wrote:
Add new symbol
APIC_VECTOR_VALIDto replace open-coded value 16 in LAPIC and virtual LAPIC code.
First a good name is needed to make such a change. APIC_VECTOR_VALID could imo be the name of a predicate macro, but it can't be a mere number.
Then ...
xen/arch/x86/cpu/mcheck/mce_intel.c static void intel_init_thermal(struct cpuinfo_x86 *c) · line 139
Quoted 8 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +1
-1@@ -136,7 +136,7 @@ static void intel_init_thermal(struct cpuinfo_x86 *c)* is required to set the same value for all threads/cores). */ if ( (val &APIC_DM_MASK ) !=APIC_DM_FIXED - || (val &APIC_VECTOR_MASK ) >0xf ) + || (val &APIC_VECTOR_MASK ) >APIC_VECTOR_VALID )
... care needs to be taken that replacements are done such that the "no functional change" claim is actually correct. (The 0xf, i.e. 15, is replaced by 16 here. I didn't check if there are other similar issues.)
xen/arch/x86/include/asm/apicdef.h line 82
Quoted 8 lines
xen/arch/x86/include/asm/apicdef.h +1
-0@@ -78,6 +78,7 @@#define APIC_DM_STARTUP 0x00600#define APIC_DM_EXTINT 0x00700#define APIC_VECTOR_MASK 0x000FF +#define APIC_VECTOR_VALID (16)#define APIC_ICR2 0x310
Nit: No real need for parentheses here; adjacent #define-s don't have any, so it's a little hard to see why you added them.
Jan
Re: [PATCH 3/3] x86/irq: introduce APIC_VECTOR_VALID
Quoted 11 lines · Jan Beulich
Quoted 2 lines · On 15.03.2025 02:00, [email protected] wrote:
Add new symbol
APIC_VECTOR_VALIDto replace open-coded value 16 in LAPIC and virtual LAPIC code.First a good name is needed to make such a change.
APIC_VECTOR_VALIDcould imo be the name of a predicate macro, but it can't be a mere number.
Do you think something like
#define APIC_VECTOR_VALID_START 16 #define APIC_VECTOR_VALID_END 255
will be satisfactory names to use?
Quoted 17 lines
Then ...
Quoted 8 lines
xen/arch/x86/cpu/mcheck/mce_intel.c +0
-0@@ -136,7 +136,7 @@ static void intel_init_thermal(struct cpuinfo_x86 *c)* is required to set the same value for all threads/cores). */ if ( (val &
APIC_DM_MASK) !=APIC_DM_FIXED- || (val & APIC_VECTOR_MASK) > 0xf ) + || (val & APIC_VECTOR_MASK) > APIC_VECTOR_VALID )... care needs to be taken that replacements are done such that the "no functional change" claim is actually correct. (The
0xf, i.e. 15, is replaced by 16 here. I didn't check if there are other similar issues.)
My bad.
Thanks for the catch.
Quoted 13 lines
xen/arch/x86/include/asm/apicdef.h line 82
Quoted 9 lines
xen/arch/x86/include/asm/apicdef.h +1
-0@@ -78,6 +78,7 @@#define APIC_DM_STARTUP 0x00600#define APIC_DM_EXTINT 0x00700#define APIC_VECTOR_MASK 0x000FF +#define APIC_VECTOR_VALID (16)#define APIC_ICR2 0x310Nit: No real need for parentheses here; adjacent #define-s don't have any, so it's a little hard to see why you added them.
My understanding was MISRA requires parentheses around expressions resulting from the expansion of a macro.
After double checking, such requirement only applicable to macros w/ parameters.
Quoted 2 lines
Jan
Re: [PATCH 3/3] x86/irq: introduce APIC_VECTOR_VALID
Quoted 11 lines · Denis Mukhin
Quoted 9 lines · Jan Beulich
Quoted 2 lines · On 15.03.2025 02:00, [email protected] wrote:
Add new symbol
APIC_VECTOR_VALIDto replace open-coded value 16 in LAPIC and virtual LAPIC code.First a good name is needed to make such a change.
APIC_VECTOR_VALIDcould imo be the name of a predicate macro, but it can't be a mere number.
Quoted 6 lines
Do you think something like
#define APIC_VECTOR_VALID_START 16 #define APIC_VECTOR_VALID_END 255will be satisfactory names to use?
Maybe. In some of the places the _END one may be awkward to use, though. If you absolutely want to replace those numbers, imo the previously alluded to APIC_VECTOR_VALID() predicate macro may be a better thing to use. Personally, despite generally disliking plain literal numbers, I'm not overly concerned of the ones here.
Jan
Quoted 10 lines · On 15/03/2025 1:00 am, [email protected] wrote:
Remove uses of open-coded value 16 in x86's interrupt management code to enhance readability.
Patch 0 makes a cosmetic rename
NR_ISAIRQS->NR_ISA_IRQSas per code review [1].Patch 1 makes use of
NR_ISA_IRQSin the code where necessary.Patch 2 adds new symbol
APIC_VECTOR_VALIDand makes use of it.
Quoted 8 lines
No functional changes.
[1]: https://lore.kernel.org/xen-devel/[email protected]/
Denis Mukhin (3):
x86/irq: renameNR_ISAIRQStoNR_ISA_IRQSx86/irq: useNR_ISA_IRQSinstead of open-coded value x86/irq: introduceAPIC_VECTOR_VALID
I've taken patches 1 and 2, and confirmed that 2 has no compiled delta in it.
I'm on the fence over 3, for similar reasons to Jan.
~Andrew