lib/cobalt: Fix a deadlock when release_buffer() attempts to re-acquire buffer_lock
PATCH v3 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
#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
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0
---
Patch v2:
- Use the pthread_setcancelstate() function to set the thread state
to PTHREAD_CANCEL_DISABLE.
Patch v3:
- Add backtrace output in gdb.
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 66 lines · Haiyang He
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 Thread0x7ff7ff4010(LWP 1654) "hello"0x0000007ff7bb6398insched_yield() at../sysdeps/unix/syscall-template.S:78
2 Thread0x7ff7a7a1c0(LWP 1657) "cobalt_printf"__lll_lock_wait(futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
3 Thread0x7ff72791c0(LWP 1658) "master"0x0000007ff7b6d314in__GI__IO_default_xsputn(n=<optimized out>, data=<optimized out>,
f=<optimized out>) at genops.c:393
4 Thread0x7ff72581c0(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#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(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0Signed-off-by Haiyang He <[email protected]>---
Patch v2:
- Use thepthread_setcancelstate()function to set the thread state toPTHREAD_CANCEL_DISABLE.Patch v3:
- Add backtrace output in gdb.
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; +cobalt_thread_relax ();
lib/cobalt/printf.c const char *rt_print_buffer_name(void) · line 497
Quoted 10 lines
lib/cobalt/printf.c +5
-0+ +pthread_setcancelstate (PTHREAD_CANCEL_DISABLE , &old_state); +pthread_mutex_lock (&buffer_lock);print_buffers ();pthread_mutex_unlock (&buffer_lock); + +pthread_setcancelstate (old_state,NULL ); }
When I try to do more test, I find this:
When print_buffers() calls fwrite(), fwrite() checks whether the __flags2 of the
filestream stdout has the _IO_FLAGS2_NOTCANCEL bit set. By default, this bit is
not set, so __libc_enable_asynccancel is called. This causes PTHREAD_CANCEL_DISABLE
to become ineffective, resulting in the thread still exiting unexpectedly.
This is not the finally fix though. Any ideas? Shall we use write() instead of fwrite() inside print_buffers()?
Haiyang.
Quoted 3 lines
lib/cobalt/printf.c +0
-0static void release_buffer (struct print_buffer *buffer)Signature · 1 line
2.17.1
Quoted 84 lines · Haiyang He
Quoted 66 lines · Haiyang He
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 Thread0x7ff7ff4010(LWP 1654) "hello"0x0000007ff7bb6398insched_yield() at../sysdeps/unix/syscall-template.S:78
2 Thread0x7ff7a7a1c0(LWP 1657) "cobalt_printf"__lll_lock_wait(futex=futex@entry=0x7ff7fcaed8 <buffer_lock>, private=0)
at lowlevellock.c:52
3 Thread0x7ff72791c0(LWP 1658) "master"0x0000007ff7b6d314in__GI__IO_default_xsputn(n=<optimized out>, data=<optimized out>,
f=<optimized out>) at genops.c:393
4 Thread0x7ff72581c0(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#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(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0Signed-off-by Haiyang He <[email protected]>---
Patch v2:
- Use thepthread_setcancelstate()function to set the thread state toPTHREAD_CANCEL_DISABLE.Patch v3:
- Add backtrace output in gdb.
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; +cobalt_thread_relax ();lib/cobalt/printf.c const char *rt_print_buffer_name(void) · line 497
Quoted 10 lines
lib/cobalt/printf.c +5
-0+ +pthread_setcancelstate (PTHREAD_CANCEL_DISABLE , &old_state); +pthread_mutex_lock (&buffer_lock);print_buffers ();pthread_mutex_unlock (&buffer_lock); + +pthread_setcancelstate (old_state,NULL ); }When I try to do more test, I find this:
When
print_buffers()callsfwrite(),fwrite()checks whether the__flags2of thefilestream stdout has the
_IO_FLAGS2_NOTCANCELbit set. By default, this bit is
Quoted 9 lines
not set, so
__libc_enable_asynccancelis called. This causesPTHREAD_CANCEL_DISABLEto become ineffective, resulting in the thread still exiting unexpectedly.
This is not the finally fix though. Any ideas? Shall we use
write()instead offwrite()insideprint_buffers()?Haiyang.
Please ignore above comments. Actually this test results caused by a wrong branch which I have not applied above patch v3.
In a word, this patch pass all my test.
Please have a review, thanks!
Haiyang.
Quoted 3 lines
Quoted 3 lines
lib/cobalt/printf.c +0
-0static void release_buffer (struct print_buffer *buffer)Signature · 1 line
2.17.1
Jan Kiszka
AppliedQuoted 67 lines · Haiyang He
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#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(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__owner $4 = 1659
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__lock $5 = 2
(gdb) p ((pthread_mutex_t*) &buffer_lock)->__data.__count $6 = 0Signed-off-by Haiyang He <[email protected]>---
Patch v2:
- Use thepthread_setcancelstate()function to set the thread state
toPTHREAD_CANCEL_DISABLE.Patch v3:
- Add backtrace output in gdb.lib/cobalt/printf.c | 7 +++++++ 1 file changed, 7 insertions(+)lib/cobalt/printf.c +3
-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 (); +
lib/cobalt/printf.c const char *rt_print_buffer_name(void) · line 498
Quoted 10 lines
lib/cobalt/printf.c +4
-0+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)
Thanks, applied.
Jan