Created attachment 12925 [details] Test program showing early return from pthread_mutex_clocklock() When a pthread mutex is ROBUST_NP, ERRORCHECK, and PRIO_INHERIT (other combination may fall into this same category) and pthread_mutex_clocklock() is used with CLOCK_MONOTONIC, the call will return immediately with ETIMEDOUT if the mutex is already taken at the time of the call. This was tested with glibc 2.30, but still appears to be present in the latest source code. The root cause is at line 381 of pthread_mutex_timedlock.c: int e = futex_lock_pi64 (&mutex->__data.__lock, abstime, private); Here, the CLOCK_MONOTONIC abstime is being passed to futex_lock_pi64 which requires a CLOCK_REALTIME abstime. Of course, a CLOCK_MONOTONIC looks a lot "earlier" than a CLOCK_REALTIME, so it times out immediately. It's not sufficient to convert from CLOCK_MONOTONIC to CLOCK_REALTIME since that would violate the assumption one is using, that changes to the wall clock time won't interfere with CLOCK_MONOTONIC timed waits. This was tested with kernel 5.4.47. Example program is attached.
Forgot to include example output. $ futex_bug FAILURE: ETIMEDOUT should have been returned after 10 or more seconds, returned after 0 seconds. $ uname -a Linux PCMA4MA000002.local.domain 5.4.47-2.2.0 #1 SMP PREEMPT Mon Oct 26 23:52:12 UTC 2020 aarch64 GNU/Linux
We need kernel support for FUTEX_LOCK_PI with CLOCK_REALTIME due possible non continuous clock, but currently Linux does not supporting set the clock for the PI operations (it always assumes CLOCK_REALTIME). I think it would be better to just return EINVAL is a clock different than CLOCK_REALTIME is used with pthread_mutex_clocklock and PTHREAD_PRIO_INHERIT.
Fixed on 2.33.