Bug 26801 - pthread_mutex_clocklock with CLOCK_MONOTONIC can fail on PI mutexes
Summary: pthread_mutex_clocklock with CLOCK_MONOTONIC can fail on PI mutexes
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: nptl (show other bugs)
Version: 2.30
: P2 normal
Target Milestone: 2.33
Assignee: Adhemerval Zanella
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-10-28 13:54 UTC by Ric Yeates
Modified: 2020-11-25 15:31 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Test program showing early return from pthread_mutex_clocklock() (875 bytes, text/plain)
2020-10-28 13:54 UTC, Ric Yeates
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ric Yeates 2020-10-28 13:54:30 UTC
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.
Comment 1 Ric Yeates 2020-10-28 13:59:19 UTC
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
Comment 2 Adhemerval Zanella 2020-10-28 20:02:50 UTC
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.
Comment 3 Adhemerval Zanella 2020-11-25 15:31:07 UTC
Fixed on 2.33.