Skip to contentSkip to conversation

alpha: marvel: Fix lock ordering in init_io7_irqs()

PATCH In review Matt Turner 2 participants 3 messages Apr 3 – May 26 Alpha port

lore
Share & tools
lore

Matt Turner

author maintainer
#0 lore raw

PATCH 1/1 alpha: marvel: Fix lock ordering in init_io7_irqs()

Move irq_set_chip_and_handler() and irq_set_status_flags() calls outside the io7->irq_lock raw spinlock. These functions take sparse_irq_lock, which is a mutex, and taking a sleeping lock while holding a raw spinlock is invalid. The raw spinlock only needs to protect the hardware CSR accesses.

This fixes the following lockdep splat during boot:

[ BUG: Invalid wait context ]
swapper/0/0 is trying to lock:
sparse_irq_lock{....}-{4:4}, at: irq_mark_irq
other info that might help us debug this:
context-{5:5}
1 lock held by swapper/0/0:
#0: &io7->irq_lock{....}-{2:2}, at: init_io7_irqs.constprop.0

Assisted-by Claude:claude-opus-4-6
---
 arch/alpha/kernel/sys_marvel.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
arch/alpha/kernel/sys_marvel.c +13 -14
index 1f99b03effc2..d0bdd5e6cfd7 100644
@@ -263,6 +263,18 @@ init_io7_irqs(struct io7 *io7,
 	 */
 	printk("  Interrupts reported to CPU at PE %u\n", boot_cpuid);
 
+	/* Set up the lsi irqs.  */
+	for (i = 0; i < 128; ++i) {
+		irq_set_chip_and_handler(base + i, lsi_ops, handle_level_irq);
+		irq_set_status_flags(i, IRQ_LEVEL);
+	}
+
+	/* Set up the msi irqs.  */
+	for (i = 128; i < (128 + 512); ++i) {
+		irq_set_chip_and_handler(base + i, msi_ops, handle_level_irq);
+		irq_set_status_flags(i, IRQ_LEVEL);
+	}
+
 	raw_spin_lock(&io7->irq_lock);
 
 	/* set up the error irqs */
@@ -272,26 +284,13 @@ init_io7_irqs(struct io7 *io7,
 	io7_redirect_irq(io7, &io7->csrs->STV_CTL.csr, boot_cpuid);
 	io7_redirect_irq(io7, &io7->csrs->HEI_CTL.csr, boot_cpuid);
 
-	/* Set up the lsi irqs.  */
-	for (i = 0; i < 128; ++i) {
-		irq_set_chip_and_handler(base + i, lsi_ops, handle_level_irq);
-		irq_set_status_flags(i, IRQ_LEVEL);
-	}
-
 	/* Disable the implemented irqs in hardware.  */
-	for (i = 0; i < 0x60; ++i) 
+	for (i = 0; i < 0x60; ++i)
 		init_one_io7_lsi(io7, i, boot_cpuid);
 
 	init_one_io7_lsi(io7, 0x74, boot_cpuid);
 	init_one_io7_lsi(io7, 0x75, boot_cpuid);
 
-
-	/* Set up the msi irqs.  */
-	for (i = 128; i < (128 + 512); ++i) {
-		irq_set_chip_and_handler(base + i, msi_ops, handle_level_irq);
-		irq_set_status_flags(i, IRQ_LEVEL);
-	}
-
 	for (i = 0; i < 16; ++i)
 		init_one_io7_msi(io7, i, boot_cpuid);
 
Signature · 1 line
2.52.0
maintainer
#1 lore raw
Quoted 65 lines · Matt Turner

Move irq_set_chip_and_handler() and irq_set_status_flags() calls outside the io7->irq_lock raw spinlock. These functions take sparse_irq_lock, which is a mutex, and taking a sleeping lock while holding a raw spinlock is invalid. The raw spinlock only needs to protect the hardware CSR accesses.

This fixes the following lockdep splat during boot:

[ BUG: Invalid wait context ]
swapper/0/0 is trying to lock:
sparse_irq_lock{....}-{4:4}, at: irq_mark_irq
other info that might help us debug this:
context-{5:5}
1 lock held by swapper/0/0:

   #0: &io7->irq_lock{....}-{2:2}, at: init_io7_irqs.constprop.0
Assisted-by Claude:claude-opus-4-6
---
 arch/alpha/kernel/sys_marvel.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
arch/alpha/kernel/sys_marvel.c +13 -8
index 1f99b03effc2..d0bdd5e6cfd7 100644
@@ -263,6 +263,18 @@ init_io7_irqs(struct io7 *io7,
         */
        printk("  Interrupts reported to CPU at PE %u\n", boot_cpuid);
 
+       /* Set up the lsi irqs.  */
+       for (i = 0; i < 128; ++i) {
+               irq_set_chip_and_handler(base + i, lsi_ops, handle_level_irq);
+               irq_set_status_flags(i, IRQ_LEVEL);
+       }
+
+       /* Set up the msi irqs.  */
+       for (i = 128; i < (128 + 512); ++i) {
+               irq_set_chip_and_handler(base + i, msi_ops, handle_level_irq);
+               irq_set_status_flags(i, IRQ_LEVEL);
+       }
+
        raw_spin_lock(&io7->irq_lock);
 
        /* set up the error irqs */
@@ -272,26 +284,13 @@ init_io7_irqs(struct io7 *io7,
        io7_redirect_irq(io7, &io7->csrs->STV_CTL.csr, boot_cpuid);
        io7_redirect_irq(io7, &io7->csrs->HEI_CTL.csr, boot_cpuid);
 
-       /* Set up the lsi irqs.  */
-       for (i = 0; i < 128; ++i) {
-               irq_set_chip_and_handler(base + i, lsi_ops, handle_level_irq);
-               irq_set_status_flags(i, IRQ_LEVEL);
-       }
-
        /* Disable the implemented irqs in hardware.  */
-       for (i = 0; i < 0x60; ++i)
+       for (i = 0; i < 0x60; ++i)
                init_one_io7_lsi(io7, i, boot_cpuid);
 
        init_one_io7_lsi(io7, 0x74, boot_cpuid);
        init_one_io7_lsi(io7, 0x75, boot_cpuid);
 
-

arch/alpha/kernel/sys_marvel.c init_io7_irqs(struct io7 *io7,

Quoted 10 lines
arch/alpha/kernel/sys_marvel.c +0 -7
-       /* Set up the msi irqs.  */
-       for (i = 128; i < (128 + 512); ++i) {
-               irq_set_chip_and_handler(base + i, msi_ops, handle_level_irq);
-               irq_set_status_flags(i, IRQ_LEVEL);
-       }
-
        for (i = 0; i < 16; ++i)
                init_one_io7_msi(io7, i, boot_cpuid);
 
--

The lock ordering fix makes sense to me.

One question though: in the moved setup loops we now have

    irq_set_chip_and_handler(base + i, ...)

but still

    irq_set_status_flags(i, IRQ_LEVEL);

Why does irq_set_status_flags() not need base + i as well? I would have expected that to match the IRQ number passed to irq_set_chip_and_handler(), i.e. use

    irq_set_status_flags(base + i, IRQ_LEVEL);

Magnus

author maintainer
#2 lore raw
Quoted 88 lines · Magnus Lindholm
Quoted 65 lines · Matt Turner

Move irq_set_chip_and_handler() and irq_set_status_flags() calls outside the io7->irq_lock raw spinlock. These functions take sparse_irq_lock, which is a mutex, and taking a sleeping lock while holding a raw spinlock is invalid. The raw spinlock only needs to protect the hardware CSR accesses.

This fixes the following lockdep splat during boot:

[ BUG: Invalid wait context ]
swapper/0/0 is trying to lock:
sparse_irq_lock{....}-{4:4}, at: irq_mark_irq
other info that might help us debug this:
context-{5:5}
1 lock held by swapper/0/0:

   #0: &io7->irq_lock{....}-{2:2}, at: init_io7_irqs.constprop.0
Assisted-by Claude:claude-opus-4-6
---
 arch/alpha/kernel/sys_marvel.c | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
arch/alpha/kernel/sys_marvel.c +13 -8
index 1f99b03effc2..d0bdd5e6cfd7 100644
@@ -263,6 +263,18 @@ init_io7_irqs(struct io7 *io7,
         */
        printk("  Interrupts reported to CPU at PE %u\n", boot_cpuid);
 
+       /* Set up the lsi irqs.  */
+       for (i = 0; i < 128; ++i) {
+               irq_set_chip_and_handler(base + i, lsi_ops, handle_level_irq);
+               irq_set_status_flags(i, IRQ_LEVEL);
+       }
+
+       /* Set up the msi irqs.  */
+       for (i = 128; i < (128 + 512); ++i) {
+               irq_set_chip_and_handler(base + i, msi_ops, handle_level_irq);
+               irq_set_status_flags(i, IRQ_LEVEL);
+       }
+
        raw_spin_lock(&io7->irq_lock);
 
        /* set up the error irqs */
@@ -272,26 +284,13 @@ init_io7_irqs(struct io7 *io7,
        io7_redirect_irq(io7, &io7->csrs->STV_CTL.csr, boot_cpuid);
        io7_redirect_irq(io7, &io7->csrs->HEI_CTL.csr, boot_cpuid);
 
-       /* Set up the lsi irqs.  */
-       for (i = 0; i < 128; ++i) {
-               irq_set_chip_and_handler(base + i, lsi_ops, handle_level_irq);
-               irq_set_status_flags(i, IRQ_LEVEL);
-       }
-
        /* Disable the implemented irqs in hardware.  */
-       for (i = 0; i < 0x60; ++i)
+       for (i = 0; i < 0x60; ++i)
                init_one_io7_lsi(io7, i, boot_cpuid);
 
        init_one_io7_lsi(io7, 0x74, boot_cpuid);
        init_one_io7_lsi(io7, 0x75, boot_cpuid);
 
-

arch/alpha/kernel/sys_marvel.c init_io7_irqs(struct io7 *io7,

Quoted 10 lines
arch/alpha/kernel/sys_marvel.c +0 -7
-       /* Set up the msi irqs.  */
-       for (i = 128; i < (128 + 512); ++i) {
-               irq_set_chip_and_handler(base + i, msi_ops, handle_level_irq);
-               irq_set_status_flags(i, IRQ_LEVEL);
-       }
-
        for (i = 0; i < 16; ++i)
                init_one_io7_msi(io7, i, boot_cpuid);
 
--

The lock ordering fix makes sense to me.

One question though: in the moved setup loops we now have

    irq_set_chip_and_handler(base + i, ...)

but still

    irq_set_status_flags(i, IRQ_LEVEL);
Quoted 8 lines

Why does irq_set_status_flags() not need base + i as well? I would have expected that to match the IRQ number passed to irq_set_chip_and_handler(), i.e. use

    irq_set_status_flags(base + i, IRQ_LEVEL);

Magnus

Nice catch.

This is a pre-existing bug introduced in commit 08876fe8519c4f7625efba8aa3d51b1d24cd8a13. I'll include a patch in my series to fix this.