Skip to contentSkip to conversation

lib/cobalt/internal: Fix lock-ups of cobalt_monitor_wait over signals

PATCH In review Jan Kiszka 2 participants 2 messages Oct 11, 2024 – Oct 13, 2024

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 lib/cobalt/internal: Fix lock-ups of cobalt_monitor_wait over signals

From: Jan Kiszka <[email protected]>

This is a very long pending issue, actually since day #1 of this code. We were checking the wrong return code for interruptions by Linux signals. Rather than using the opret code that the cobalt_monitor_wait syscalls set when the xnsynch_sleep_on was interrupted, we checked the syscall return code that might have been set to -EINTR as well in case prepare_for_signal found a pending signal while returning to userspace. However, in that case the monitor was still entered, and our attempt to enter it again from userspace caused a deadlock.

This was first reported in [1] over alchemy's rt_buffer_read, but it could affect many more direct or indirect users of cobalt_monitor_wait & Co.

[1] https://lore.kernel.org/xenomai/[email protected]/

---

It took a few hours to understand and fix, but 11 months to finally reproduce locally. Anyway, the bug was 13 years old, so... :)

Mauro, in the end it was just a main thread handling a signal (not even terminating) over timing-out rt_buffer_read. As the race window was small in the kernel, I added there an udelay once I thought I understood it, and it triggered on any setup withing a minute or less.

 lib/cobalt/internal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/cobalt/internal.c +1 -1
index d721e7ec69..dd3da08558 100644
@@ -269,7 +269,7 @@ int cobalt_monitor_wait(cobalt_monitor_t *mon, int event,
 	 * we need to redo. In the meantime, any pending linux signal
 	 * has been processed.
 	 */
-	if (ret == -EINTR)
+	if (opret == -EINTR)
 		ret = cobalt_monitor_enter(mon);
 
 	return ret ?: opret;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 21 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

This is a very long pending issue, actually since day #1 of this code. We were checking the wrong return code for interruptions by Linux signals. Rather than using the opret code that the cobalt_monitor_wait syscalls set when the xnsynch_sleep_on was interrupted, we checked the syscall return code that might have been set to -EINTR as well in case prepare_for_signal found a pending signal while returning to userspace. However, in that case the monitor was still entered, and our attempt to enter it again from userspace caused a deadlock.

This was first reported in [1] over alchemy's rt_buffer_read, but it could affect many more direct or indirect users of cobalt_monitor_wait & Co.

[1] https://lore.kernel.org/xenomai/[email protected]/

---

Quoted 7 lines

It took a few hours to understand and fix, but 11 months to finally reproduce locally. Anyway, the bug was 13 years old, so... :)

Mauro, in the end it was just a main thread handling a signal (not even terminating) over timing-out rt_buffer_read. As the race window was small in the kernel, I added there an udelay once I thought I understood it, and it triggered on any setup withing a minute or less.

Great catch Jan!
Thank you!

Quoted 17 lines
  lib/cobalt/internal.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
lib/cobalt/internal.c +1 -1
index d721e7ec69..dd3da08558 100644
@@ -269,7 +269,7 @@ int cobalt_monitor_wait(cobalt_monitor_t *mon, int event,
  	 * we need to redo. In the meantime, any pending linux signal
  	 * has been processed.
  	 */
-	if (ret == -EINTR)
+	if (opret == -EINTR)
  		ret = cobalt_monitor_enter(mon);
 
  	return ret ?: opret;
Signature · 1 line
Mauro S.