lib/cobalt: Fix a deadlock when release_buffer() attempts to re-acquire buffer_lock
PATCH v2 1/1 lib/cobalt: Fix a deadlock when release_buffer() attempts to re-acquire buffer_lock
The release_buffer() function is the destructor function for the thread-specific data (TSD) associated with buffer_key. If the current thread is executing rt_print_flush_buffers() and has already acquired buffer_lock, and is then requested to exit via pthread_cancel(), the release_buffer() function will be invoked. A deadlock can occur when release_buffer() attempts to re-acquire buffer_lock.
This patch uses the pthread_setcancelstate() function to set the thread state to PTHREAD_CANCEL_DISABLE, ensuring that the thread does not respond to cancellation requests while holding the buffer_lock.
The deadlock can be observed as gdb shown. The thread "slave" will block when calling pthread_mutex_lock() in release_buffer() while attempting to re-acquire buffer_lock again. This also causes cobalt_print to remain waiting indefinitely for buffer_lock.
(gdb) info thread
Id Target Id Frame
* 1 Thread 0x7ff7ff4010 (LWP 1654) "hello" 0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78
2 Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf" __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
3 Thread 0x7ff72791c0 (LWP 1658) "master" 0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>,
f=<optimized out>) at genops.c:393 4 Thread 0x7ff72581c0 (LWP 1659) "slave" (Exiting) __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread 0x7ff72581c0 (LWP 1659))]
52 lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
--- lib/cobalt/printf.c | 7 +++++++ 1 file changed, 7 insertions(+)
lib/cobalt/printf.c +7 -0
@@ -482,10 +482,17 @@ const char *rt_print_buffer_name(void)/* *** Deferred Output Management *** */ void rt_print_flush_buffers (void ) { +int old_state; +cobalt_thread_relax (); + +pthread_setcancelstate (PTHREAD_CANCEL_DISABLE , &old_state); +pthread_mutex_lock (&buffer_lock);print_buffers ();pthread_mutex_unlock (&buffer_lock); + +pthread_setcancelstate (old_state,NULL ); }static void release_buffer (struct print_buffer *buffer)
Signature · 1 line
2.17.1
Quoted 34 lines · Ethan
The
release_buffer()function is the destructor function for the thread-specific data (TSD) associated withbuffer_key. If the current thread is executingrt_print_flush_buffers()and has already acquiredbuffer_lock, and is then requested to exit viapthread_cancel(), therelease_buffer()function will be invoked. A deadlock can occur whenrelease_buffer()attempts to re-acquirebuffer_lock.This patch uses the
pthread_setcancelstate()function to set the thread state toPTHREAD_CANCEL_DISABLE, ensuring that the thread does not respond to cancellation requests while holding thebuffer_lock.The deadlock can be observed as gdb shown. The thread "slave" will block when calling
pthread_mutex_lock()inrelease_buffer()while attempting to re-acquirebuffer_lockagain. This also causescobalt_printto remain waiting indefinitely forbuffer_lock.(gdb) info thread
Id Target Id Frame * 1 Thread 0x7ff7ff4010 (LWP 1654) "hello" 0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78 2 Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf" __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52 3 Thread 0x7ff72791c0 (LWP 1658) "master" 0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>, f=<optimized out>) at genops.c:3934 Thread
0x7ff72581c0(LWP 1659) "slave" (Exiting)__lll_lock_wait(futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread0x7ff72581c0(LWP 1659))]
52lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
Quoted 1 line
Signed-off-by Ethan <[email protected]>
I think we would prefer a full name here, as well as inside the author information (sender display name as we don't have a From line at the top).
Quoted 14 lines
--- lib/cobalt/printf.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/cobalt/printf.c +2
-0@@ -482,10 +482,17 @@ const char *rt_print_buffer_name(void)/* *** Deferred Output Management *** */ void rt_print_flush_buffers (void ) { +int old_state; +
lib/cobalt/printf.c const char *rt_print_buffer_name(void) · line 496
Quoted 10 lines
lib/cobalt/printf.c +5
-0cobalt_thread_relax (); + +pthread_setcancelstate (PTHREAD_CANCEL_DISABLE , &old_state); +pthread_mutex_lock (&buffer_lock);print_buffers ();pthread_mutex_unlock (&buffer_lock); + +pthread_setcancelstate (old_state,NULL ); }
The problem is specific to the buffer_lock because we use it in the tear-down / destructor part for a task as well, right?
We disable cancellation before really owning the lock. Is that a problem if it takes some time for really getting the ownership?
Quoted 5 lines
lib/cobalt/printf.c +0
-0static void release_buffer (struct print_buffer *buffer)Signature · 1 line
2.17.1
Quoted 35 lines · Florian Bezdeka
Quoted 34 lines · Ethan
The
release_buffer()function is the destructor function for the thread-specific data (TSD) associated withbuffer_key. If the current thread is executingrt_print_flush_buffers()and has already acquiredbuffer_lock, and is then requested to exit viapthread_cancel(), therelease_buffer()function will be invoked. A deadlock can occur whenrelease_buffer()attempts to re-acquirebuffer_lock.This patch uses the
pthread_setcancelstate()function to set the thread state toPTHREAD_CANCEL_DISABLE, ensuring that the thread does not respond to cancellation requests while holding thebuffer_lock.The deadlock can be observed as gdb shown. The thread "slave" will block when calling
pthread_mutex_lock()inrelease_buffer()while attempting to re-acquirebuffer_lockagain. This also causescobalt_printto remain waiting indefinitely forbuffer_lock.(gdb) info thread
Id Target Id Frame * 1 Thread 0x7ff7ff4010 (LWP 1654) "hello" 0x0000007ff7bb6398 in sched_yield () at ../sysdeps/unix/syscall-template.S:78 2 Thread 0x7ff7a7a1c0 (LWP 1657) "cobalt_printf" __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52 3 Thread 0x7ff72791c0 (LWP 1658) "master" 0x0000007ff7b6d314 in __GI__IO_default_xsputn (n=<optimized out>, data=<optimized out>, f=<optimized out>) at genops.c:3934 Thread
0x7ff72581c0(LWP 1659) "slave" (Exiting)__lll_lock_wait(futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
(gdb) thread 4
[Switching to thread 4 (Thread0x7ff72581c0(LWP 1659))]
52lowlevellock.c: No such file or directory.
(gdb) bt
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
Quoted 6 lines
Quoted 1 line
Signed-off-by Ethan <[email protected]>I think we would prefer a full name here, as well as inside the author information (sender display name as we don't have a From line at the top).
Sure, I would resend patch with full name.
Quoted 20 lines
Quoted 20 lines
--- lib/cobalt/printf.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/cobalt/printf.c +5
-0@@ -482,10 +482,17 @@ const char *rt_print_buffer_name(void)/* *** Deferred Output Management *** */ void rt_print_flush_buffers (void ) { +int old_state; +cobalt_thread_relax (); + +pthread_setcancelstate (PTHREAD_CANCEL_DISABLE , &old_state); +pthread_mutex_lock (&buffer_lock);
Quoted 8 lines
lib/cobalt/printf.c const char *rt_print_buffer_name(void) · line 496
Quoted 5 lines
lib/cobalt/printf.c +2
-0print_buffers ();pthread_mutex_unlock (&buffer_lock); + +pthread_setcancelstate (old_state,NULL ); }The problem is specific to the
buffer_lockbecause we use it in the tear-down / destructor part for a task as well, right?
Yes. The print_buffers() function may encounter a cancellation point.
Quoted 4 lines
We disable cancellation before really owning the lock. Is that a problem if it takes some time for really getting the ownership?
No, it's in deadlock. As the gdb shown, buffer_lock is held by thread (LWP 1659), but destructor __nptl_deallocate_tsd()->release_buffer()->pthread_mutex_lock() re-acquires buffer_lock.
(gdb) thread 4
[Switching to thread 4 (Thread 0x7ff72581c0 (LWP 1659))]
#0 __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52
52 lowlevellock.c: No such file or directory.
(gdb) bt
#0 __lll_lock_wait (futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0) at lowlevellock.c:52 #1 0x0000007ff7f43cd8 in __GI___pthread_mutex_lock (mutex=0x7ff7fcaed8 <buffer_lock>) at pthread_mutex_lock.c:80 #2 0x0000007ff7fa3038 in release_buffer () from /usr/xenomai/lib/libcobalt.so.2 #3 0x0000007ff7f413c0 in __nptl_deallocate_tsd () at pthread_create.c:301 #4 0x0000007ff7f4156c in start_thread (arg=0x7ff7fa7430 <cobalt_thread_trampoline>) at pthread_create.c:488 #5 0x0000007ff7bcc49c in thread_start () at ../sysdeps/unix/sysv/linux/aarch64/clone.S:78
Quoted 5 lines
Quoted 5 lines
lib/cobalt/printf.c +0
-0static void release_buffer (struct print_buffer *buffer)Signature · 1 line
2.17.1