Skip to contentSkip to conversation

riscv: Fixup boot failure when CONFIG_DEBUG_RT_MUTEXES=y

PATCH RESEND Applied [email protected] 6 participants 7 messages Nov 30, 2024 – Dec 11, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 riscv: Fixup boot failure when CONFIG_DEBUG_RT_MUTEXES=y

From: Guo Ren <[email protected]>

When CONFIG_DEBUG_RT_MUTEXES=y, mutex_lock->rt_mutex_try_acquire would change from rt_mutex_cmpxchg_acquire to rt_mutex_slowtrylock():

	raw_spin_lock_irqsave(&lock->wait_lock, flags);
	ret = __rt_mutex_slowtrylock(lock);
	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);

Because queued_spin_#ops to ticket_#ops is changed one by one by jump_label, raw_spin_lock/unlock would cause a deadlock during the changing.

That means in arch/riscv/kernel/jump_label.c:
1.
arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> queued_spin_lock

			 |-> raw_spin_unlock -> queued_spin_unlock

patch_insn_write -> change the raw_spin_lock to ticket_lock mutex_unlock(&text_mutex);
...

2. /* Dirty the lock value */ arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> *ticket_lock*

                         |-> raw_spin_unlock -> *queued_spin_unlock*
			  /* BUG: ticket_lock with queued_spin_unlock */

patch_insn_write -> change the raw_spin_unlock to ticket_unlock mutex_unlock(&text_mutex);
...

3. /* Dead lock */
arch_jump_label_transform_queue() ->
mutex_lock(&text_mutex); +-> raw_spin_lock -> ticket_lock /* deadlock! */

                         |-> raw_spin_unlock -> ticket_unlock

patch_insn_write -> change other raw_spin_#op -> ticket_#op mutex_unlock(&text_mutex);

So, the solution is to disable mutex usage of arch_jump_label_transform_queue() during early_boot_irqs_disabled, just like we have done for stop_machine.

---
 arch/riscv/kernel/jump_label.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/kernel/jump_label.c +9 -3
index 6eee6f736f68..654ed159c830 100644
@@ -36,9 +36,15 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
 		insn = RISCV_INSN_NOP;
 	}
 
-	mutex_lock(&text_mutex);
-	patch_insn_write(addr, &insn, sizeof(insn));
-	mutex_unlock(&text_mutex);
+	if (early_boot_irqs_disabled) {
+		riscv_patch_in_stop_machine = 1;
+		patch_insn_write(addr, &insn, sizeof(insn));
+		riscv_patch_in_stop_machine = 0;
+	} else {
+		mutex_lock(&text_mutex);
+		patch_insn_write(addr, &insn, sizeof(insn));
+		mutex_unlock(&text_mutex);
+	}
 
 	return true;
 }
Signature · 1 line
2.40.1
Tested-by on v1 1/1
#1 lore raw
Quoted 35 lines · On Sat, Nov 30, 2024 at 10:33:10AM -0500, [email protected] wrote:

From: Guo Ren <[email protected]>

When CONFIG_DEBUG_RT_MUTEXES=y, mutex_lock->rt_mutex_try_acquire would change from rt_mutex_cmpxchg_acquire to rt_mutex_slowtrylock():

	raw_spin_lock_irqsave(&lock->wait_lock, flags);
	ret = __rt_mutex_slowtrylock(lock);
	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);

Because queued_spin_#ops to ticket_#ops is changed one by one by jump_label, raw_spin_lock/unlock would cause a deadlock during the changing.

That means in arch/riscv/kernel/jump_label.c:
1.
arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> queued_spin_lock

			 |-> raw_spin_unlock -> queued_spin_unlock

patch_insn_write -> change the raw_spin_lock to ticket_lock mutex_unlock(&text_mutex);
...

2. /* Dirty the lock value */ arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> *ticket_lock*

                         |-> raw_spin_unlock -> *queued_spin_unlock*
			  /* BUG: ticket_lock with queued_spin_unlock */

patch_insn_write -> change the raw_spin_unlock to ticket_unlock mutex_unlock(&text_mutex);
...

3. /* Dead lock */
arch_jump_label_transform_queue() ->
mutex_lock(&text_mutex); +-> raw_spin_lock -> ticket_lock /* deadlock! */

                         |-> raw_spin_unlock -> ticket_unlock
Quoted 8 lines

patch_insn_write -> change other raw_spin_#op -> ticket_#op mutex_unlock(&text_mutex);

So, the solution is to disable mutex usage of arch_jump_label_transform_queue() during early_boot_irqs_disabled, just like we have done for stop_machine.

Thanks,
Conor.

reviewer Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to [email protected]

Hi Guo,

Quoted 66 lines · On 30/11/2024 16:33, [email protected] wrote:

From: Guo Ren <[email protected]>

When CONFIG_DEBUG_RT_MUTEXES=y, mutex_lock->rt_mutex_try_acquire would change from rt_mutex_cmpxchg_acquire to rt_mutex_slowtrylock():

	raw_spin_lock_irqsave(&lock->wait_lock, flags);
	ret = __rt_mutex_slowtrylock(lock);
	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);

Because queued_spin_#ops to ticket_#ops is changed one by one by jump_label, raw_spin_lock/unlock would cause a deadlock during the changing.

That means in arch/riscv/kernel/jump_label.c:
1.
arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> queued_spin_lock

			 |-> raw_spin_unlock -> queued_spin_unlock

patch_insn_write -> change the raw_spin_lock to ticket_lock mutex_unlock(&text_mutex);
...

2. /* Dirty the lock value */ arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> *ticket_lock*

                          |-> raw_spin_unlock -> *queued_spin_unlock*
			  /* BUG: ticket_lock with queued_spin_unlock */

patch_insn_write -> change the raw_spin_unlock to ticket_unlock mutex_unlock(&text_mutex);
...

3. /* Dead lock */
arch_jump_label_transform_queue() ->
mutex_lock(&text_mutex); +-> raw_spin_lock -> ticket_lock /* deadlock! */

                          |-> raw_spin_unlock -> ticket_unlock

patch_insn_write -> change other raw_spin_#op -> ticket_#op mutex_unlock(&text_mutex);

So, the solution is to disable mutex usage of arch_jump_label_transform_queue() during early_boot_irqs_disabled, just like we have done for stop_machine.

---
  arch/riscv/kernel/jump_label.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/kernel/jump_label.c +2 -3
index 6eee6f736f68..654ed159c830 100644
@@ -36,9 +36,15 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
  		insn = RISCV_INSN_NOP;
  	}
 
-	mutex_lock(&text_mutex);
-	patch_insn_write(addr, &insn, sizeof(insn));
-	mutex_unlock(&text_mutex);
+	if (early_boot_irqs_disabled) {
+		riscv_patch_in_stop_machine = 1;

arch/riscv/kernel/jump_label.c bool arch_jump_label_transform_queue(struct jump_entry *entry, · line 50

Quoted 10 lines
arch/riscv/kernel/jump_label.c +7 -0
+		patch_insn_write(addr, &insn, sizeof(insn));
+		riscv_patch_in_stop_machine = 0;
+	} else {
+		mutex_lock(&text_mutex);
+		patch_insn_write(addr, &insn, sizeof(insn));
+		mutex_unlock(&text_mutex);
+	}
 
  	return true;
  }

Sorry for the late answer, I've been sick lately!

Thank you very much for looking into this and finding this not-so-bad solution! I remind everyone that this is a temporary solution until we can use an alternative instead of a static key.

You can add:

The revert is still on the table IMO, let's Palmer decide.

Thank you again Guo, really appreciate you took the time to find this solution!

Alex

author
#3 lore raw
Quoted 78 lines · Alexandre Ghiti

Hi Guo,

Quoted 74 lines · On 30/11/2024 16:33, [email protected] wrote:

From: Guo Ren <[email protected]>

When CONFIG_DEBUG_RT_MUTEXES=y, mutex_lock->rt_mutex_try_acquire would change from rt_mutex_cmpxchg_acquire to rt_mutex_slowtrylock():

      raw_spin_lock_irqsave(&lock->wait_lock, flags);
      ret = __rt_mutex_slowtrylock(lock);
      raw_spin_unlock_irqrestore(&lock->wait_lock, flags);

Because queued_spin_#ops to ticket_#ops is changed one by one by jump_label, raw_spin_lock/unlock would cause a deadlock during the changing.

That means in arch/riscv/kernel/jump_label.c:
1.
arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> queued_spin_lock

                       |-> raw_spin_unlock -> queued_spin_unlock

patch_insn_write -> change the raw_spin_lock to ticket_lock mutex_unlock(&text_mutex);
...

2. /* Dirty the lock value */ arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> *ticket_lock*

                          |-> raw_spin_unlock -> *queued_spin_unlock*
                        /* BUG: ticket_lock with queued_spin_unlock */

patch_insn_write -> change the raw_spin_unlock to ticket_unlock mutex_unlock(&text_mutex);
...

3. /* Dead lock */
arch_jump_label_transform_queue() ->
mutex_lock(&text_mutex); +-> raw_spin_lock -> ticket_lock /* deadlock! */

                          |-> raw_spin_unlock -> ticket_unlock

patch_insn_write -> change other raw_spin_#op -> ticket_#op mutex_unlock(&text_mutex);

So, the solution is to disable mutex usage of arch_jump_label_transform_queue() during early_boot_irqs_disabled, just like we have done for stop_machine.

---
  arch/riscv/kernel/jump_label.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/kernel/jump_label.c +9 -3
index 6eee6f736f68..654ed159c830 100644
@@ -36,9 +36,15 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
              insn = RISCV_INSN_NOP;
      }
 
-     mutex_lock(&text_mutex);
-     patch_insn_write(addr, &insn, sizeof(insn));
-     mutex_unlock(&text_mutex);
+     if (early_boot_irqs_disabled) {
+             riscv_patch_in_stop_machine = 1;
+             patch_insn_write(addr, &insn, sizeof(insn));
+             riscv_patch_in_stop_machine = 0;
+     } else {
+             mutex_lock(&text_mutex);
+             patch_insn_write(addr, &insn, sizeof(insn));
+             mutex_unlock(&text_mutex);
+     }
 
Quoted 5 lines

arch/riscv/kernel/jump_label.c bool arch_jump_label_transform_queue(struct jump_entry *entry, · line 50

Quoted 2 lines
arch/riscv/kernel/jump_label.c +0 -0
      return true;
  }

Sorry for the late answer, I've been sick lately!

I wish you a speedy recovery.

Quoted 4 lines

Thank you very much for looking into this and finding this not-so-bad solution! I remind everyone that this is a temporary solution until we can use an alternative instead of a static key.

Herer, I want to point out that the problem is not jump_label infrastructure but our arch_jump_label_transform_queue ->patch_insn_write. The alternative also has a similar situation. So you still need the modification like the below:

arch/riscv/kernel/cpufeature.c +0 -0
index 467c5c735bf5..475f7c520be4 100644
@@ -1017,10 +1017,17 @@ void __init_or_module

riscv_cpufeature_patch_func(struct alt_entry *begin,

                oldptr = ALT_OLD_PTR(alt);
                altptr = ALT_ALT_PTR(alt);

+if (early_boot_irqs_disabled) {

+               riscv_patch_in_stop_machine = 1;
+               patch_text_nosync(oldptr, altptr, alt->alt_len);
+               riscv_alternative_fix_offsets(oldptr, alt->alt_len,
oldptr - altptr);
+               riscv_patch_in_stop_machine = 0;
+} else {
                mutex_lock(&text_mutex);
                patch_text_nosync(oldptr, altptr, alt->alt_len);
                riscv_alternative_fix_offsets(oldptr, alt->alt_len,
oldptr - altptr);
                mutex_unlock(&text_mutex);
+}
        }

}
#endif

Quoted 7 lines

You can add:

The revert is still on the table IMO, let's Palmer decide.

Okay

Signature · 2 lines
Best Regards
 Guo Ren
Tested-by on v1 1/1
#5 lore raw
In reply to [email protected]
Quoted 42 lines · On Sat, Nov 30, 2024 at 10:33:10AM -0500, [email protected] wrote:

From: Guo Ren <[email protected]>

When CONFIG_DEBUG_RT_MUTEXES=y, mutex_lock->rt_mutex_try_acquire would change from rt_mutex_cmpxchg_acquire to rt_mutex_slowtrylock():

	raw_spin_lock_irqsave(&lock->wait_lock, flags);
	ret = __rt_mutex_slowtrylock(lock);
	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);

Because queued_spin_#ops to ticket_#ops is changed one by one by jump_label, raw_spin_lock/unlock would cause a deadlock during the changing.

That means in arch/riscv/kernel/jump_label.c:
1.
arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> queued_spin_lock

			 |-> raw_spin_unlock -> queued_spin_unlock

patch_insn_write -> change the raw_spin_lock to ticket_lock mutex_unlock(&text_mutex);
...

2. /* Dirty the lock value */ arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> *ticket_lock*

                         |-> raw_spin_unlock -> *queued_spin_unlock*
			  /* BUG: ticket_lock with queued_spin_unlock */

patch_insn_write -> change the raw_spin_unlock to ticket_unlock mutex_unlock(&text_mutex);
...

3. /* Dead lock */
arch_jump_label_transform_queue() ->
mutex_lock(&text_mutex); +-> raw_spin_lock -> ticket_lock /* deadlock! */

                         |-> raw_spin_unlock -> ticket_unlock

patch_insn_write -> change other raw_spin_#op -> ticket_#op mutex_unlock(&text_mutex);

So, the solution is to disable mutex usage of arch_jump_label_transform_queue() during early_boot_irqs_disabled, just like we have done for stop_machine.

Quoted 7 lines

Thanks for the fix,
Nam

maintainer
#4 lore raw
In reply to Alexandre Ghiti
Quoted 77 lines · On Wed, 04 Dec 2024 00:27:00 PST (-0800), [email protected] wrote:

Hi Guo,

Quoted 74 lines · On 30/11/2024 16:33, [email protected] wrote:

From: Guo Ren <[email protected]>

When CONFIG_DEBUG_RT_MUTEXES=y, mutex_lock->rt_mutex_try_acquire would change from rt_mutex_cmpxchg_acquire to rt_mutex_slowtrylock():

	raw_spin_lock_irqsave(&lock->wait_lock, flags);
	ret = __rt_mutex_slowtrylock(lock);
	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);

Because queued_spin_#ops to ticket_#ops is changed one by one by jump_label, raw_spin_lock/unlock would cause a deadlock during the changing.

That means in arch/riscv/kernel/jump_label.c:
1.
arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> queued_spin_lock

			 |-> raw_spin_unlock -> queued_spin_unlock

patch_insn_write -> change the raw_spin_lock to ticket_lock mutex_unlock(&text_mutex);
...

2. /* Dirty the lock value */ arch_jump_label_transform_queue() -> mutex_lock(&text_mutex); +-> raw_spin_lock -> *ticket_lock*

                          |-> raw_spin_unlock -> *queued_spin_unlock*
			  /* BUG: ticket_lock with queued_spin_unlock */

patch_insn_write -> change the raw_spin_unlock to ticket_unlock mutex_unlock(&text_mutex);
...

3. /* Dead lock */
arch_jump_label_transform_queue() ->
mutex_lock(&text_mutex); +-> raw_spin_lock -> ticket_lock /* deadlock! */

                          |-> raw_spin_unlock -> ticket_unlock

patch_insn_write -> change other raw_spin_#op -> ticket_#op mutex_unlock(&text_mutex);

So, the solution is to disable mutex usage of arch_jump_label_transform_queue() during early_boot_irqs_disabled, just like we have done for stop_machine.

---
  arch/riscv/kernel/jump_label.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
arch/riscv/kernel/jump_label.c +9 -3
index 6eee6f736f68..654ed159c830 100644
@@ -36,9 +36,15 @@ bool arch_jump_label_transform_queue(struct jump_entry *entry,
  		insn = RISCV_INSN_NOP;
  	}
 
-	mutex_lock(&text_mutex);
-	patch_insn_write(addr, &insn, sizeof(insn));
-	mutex_unlock(&text_mutex);
+	if (early_boot_irqs_disabled) {
+		riscv_patch_in_stop_machine = 1;
+		patch_insn_write(addr, &insn, sizeof(insn));
+		riscv_patch_in_stop_machine = 0;
+	} else {
+		mutex_lock(&text_mutex);
+		patch_insn_write(addr, &insn, sizeof(insn));
+		mutex_unlock(&text_mutex);
+	}
 
Quoted 5 lines

arch/riscv/kernel/jump_label.c bool arch_jump_label_transform_queue(struct jump_entry *entry, · line 50

Quoted 2 lines
arch/riscv/kernel/jump_label.c +0 -0
  	return true;
  }

Sorry for the late answer, I've been sick lately!

Ya, I was also sick -- bad timing.

Quoted 10 lines

Thank you very much for looking into this and finding this not-so-bad solution! I remind everyone that this is a temporary solution until we can use an alternative instead of a static key.

You can add:

The revert is still on the table IMO, let's Palmer decide.

IMO it's fine to just take this as a fix. Reverting stuff is always a nice big hammer to have if we don't get a fix in a reasonable timeframe, but with a fix avaliable in the time I was out sick anyway I think it's fine to just take this.

So I've got it queued up on fixes.

Quoted 4 lines

Thank you again Guo, really appreciate you took the time to find this solution!

Alex

#6 lore raw
In reply to [email protected]
This patch was applied to riscv/linux.git (fixes) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <[email protected]>:

Quoted 10 lines · On Sat, 30 Nov 2024 10:33:10 -0500 you wrote:

From: Guo Ren <[email protected]>

When CONFIG_DEBUG_RT_MUTEXES=y, mutex_lock->rt_mutex_try_acquire would change from rt_mutex_cmpxchg_acquire to rt_mutex_slowtrylock():

	raw_spin_lock_irqsave(&lock->wait_lock, flags);
	ret = __rt_mutex_slowtrylock(lock);
	raw_spin_unlock_irqrestore(&lock->wait_lock, flags);

[...]

Here is the summary with links:
- [RESEND] riscv: Fixup boot failure when CONFIG_DEBUG_RT_MUTEXES=y

    https://git.kernel.org/riscv/c/b3134b8c1a1c

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html