lib/cobalt: Fix cancellation issues over pthread_cond_[timed]wait
PATCH 1/1 lib/cobalt: Fix cancellation issues over pthread_cond_[timed]wait
From: Jan Kiszka <[email protected]>
First of all, the usage of cobalt_cond_cleanup_t::count got broken long ago, leaving this field initialized to 0 rather than the actual state on entry. This could have corrupted lockcnt of the associated mutex when triggering the cleanup.
The second issue, and that was always broken, is that cancellation was only checked outside of the -EINTR look. But as cancellation actually triggers an early return from the cobalt syscall with -EINTR, we never canceled and just continued to block unless async cancellation was enabled.
---
This is sensitive code, reviews highly appreciated! Additional test cases may follow later. Found while making [1] Xenomai- aware - which is kind of test case as well.
[1] https://github.com/scheduler-tools/rt-app
lib/cobalt/cond.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-)
lib/cobalt/cond.c +12 -14
@@ -300,7 +300,6 @@ COBALT_IMPL(int, pthread_cond_wait, (pthread_cond_t *cond, pthread_mutex_t *mute .err = 0, };int err, oldtype; -unsigned count;if (_mx->magic !=COBALT_MUTEX_MAGIC )return EINVAL ;
@@ -319,9 +318,9 @@ COBALT_IMPL(int, pthread_cond_wait, (pthread_cond_t *cond, pthread_mutex_t *mutereturn EPERM ; } -pthread_cleanup_push (&__pthread_cond_cleanup, &c); + c.count = _mx->lockcnt; - count = _mx->lockcnt; +pthread_cleanup_push (&__pthread_cond_cleanup, &c);pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS , &oldtype);
@@ -331,12 +330,12 @@ COBALT_IMPL(int, pthread_cond_wait, (pthread_cond_t *cond, pthread_mutex_t *mutepthread_cleanup_pop (0 ); -while (err == -EINTR ) +while (err == -EINTR ) { err =XENOMAI_SYSCALL2 (sc_cobalt_cond_wait_epilogue, _cnd, _mx); +pthread_testcancel (); + } - _mx->lockcnt = count; - -pthread_testcancel (); + _mx->lockcnt = c.count;return -err ?: -c.err; }
@@ -388,7 +387,6 @@ COBALT_IMPL_TIME64(int, pthread_cond_timedwait, __pthread_cond_timedwait64, .mutex = _mx, };int err, oldtype; -unsigned count;if (_mx->magic !=COBALT_MUTEX_MAGIC )return EINVAL ;
@@ -407,9 +405,9 @@ COBALT_IMPL_TIME64(int, pthread_cond_timedwait, __pthread_cond_timedwait64,return EPERM ; } -pthread_cleanup_push (&__pthread_cond_cleanup, &c); + c.count = _mx->lockcnt; - count = _mx->lockcnt; +pthread_cleanup_push (&__pthread_cond_cleanup, &c);pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS , &oldtype);
@@ -419,12 +417,12 @@ COBALT_IMPL_TIME64(int, pthread_cond_timedwait, __pthread_cond_timedwait64,pthread_cleanup_pop (0 ); -while (err == -EINTR ) +while (err == -EINTR ) { err =XENOMAI_SYSCALL2 (sc_cobalt_cond_wait_epilogue, _cnd, _mx); +pthread_testcancel (); + } - _mx->lockcnt = count; - -pthread_testcancel (); + _mx->lockcnt = c.count;return -err ?: -c.err; }
Signature · 1 line
2.43.0