00db10
commit c2f5813ae0a68f6c6d69e66dac2da6e46b9df034
00db10
Author: Joseph Myers <joseph@codesourcery.com>
00db10
Date:   Wed Mar 18 17:05:38 2015 +0000
00db10
00db10
    Make sem_timedwait use FUTEX_CLOCK_REALTIME (bug 18138).
00db10
    
00db10
    sem_timedwait converts absolute timeouts to relative to pass them to
00db10
    the futex syscall.  (Before the recent reimplementation, on x86_64 it
00db10
    used FUTEX_CLOCK_REALTIME, but not on other architectures.)
00db10
    
00db10
    Correctly implementing POSIX requirements, however, requires use of
00db10
    FUTEX_CLOCK_REALTIME; passing a relative timeout to the kernel does
00db10
    not conform to POSIX.  The POSIX specification for sem_timedwait says
00db10
    "The timeout shall be based on the CLOCK_REALTIME clock.".  The POSIX
00db10
    specification for clock_settime says "If the value of the
00db10
    CLOCK_REALTIME clock is set via clock_settime(), the new value of the
00db10
    clock shall be used to determine the time of expiration for absolute
00db10
    time services based upon the CLOCK_REALTIME clock. This applies to the
00db10
    time at which armed absolute timers expire. If the absolute time
00db10
    requested at the invocation of such a time service is before the new
00db10
    value of the clock, the time service shall expire immediately as if
00db10
    the clock had reached the requested time normally.".  If a relative
00db10
    timeout is passed to the kernel, it is interpreted according to the
00db10
    CLOCK_MONOTONIC clock, and so fails to meet that POSIX requirement in
00db10
    the event of clock changes.
00db10
    
00db10
    This patch makes sem_timedwait use lll_futex_timed_wait_bitset with
00db10
    FUTEX_CLOCK_REALTIME when possible, as done in some other places in
00db10
    NPTL.  FUTEX_CLOCK_REALTIME is always available for supported Linux
00db10
    kernel versions; unavailability of lll_futex_timed_wait_bitset is only
00db10
    an issue for hppa (an issue noted in
00db10
    <https://sourceware.org/glibc/wiki/PortStatus>, and fixed by the
00db10
    unreviewed
00db10
    <https://sourceware.org/ml/libc-alpha/2014-12/msg00655.html> that
00db10
    removes the hppa lowlevellock.h completely).
00db10
    
00db10
    In the FUTEX_CLOCK_REALTIME case, the glibc code still needs to check
00db10
    for negative tv_sec and handle that as timeout, because the Linux
00db10
    kernel returns EINVAL not ETIMEDOUT for that case, so resulting in
00db10
    failures of nptl/tst-abstime and nptl/tst-sem13 in the absence of that
00db10
    check.  If we're trying to distinguish between Linux-specific and
00db10
    generic-futex NPTL code, I suppose having this in an nptl/ file isn't
00db10
    ideal, but there doesn't seem to be any better place at present.
00db10
    
00db10
    It's not possible to add a testcase for this issue to the testsuite
00db10
    because of the requirement to change the system clock as part of a
00db10
    test (this is a case where testing would require some form of
00db10
    container, with root in that container, and one whose CLOCK_REALTIME
00db10
    is isolated from that of the host; I'm not sure what forms of
00db10
    containers, short of a full virtual machine, provide that clock
00db10
    isolation).
00db10
    
00db10
    Tested for x86_64.  Also tested for powerpc with the testcase included
00db10
    in the bug.
00db10
    
00db10
        [BZ #18138]
00db10
        * nptl/sem_waitcommon.c: Include <kernel-features.h>.
00db10
        (futex_abstimed_wait)
00db10
        [__ASSUME_FUTEX_CLOCK_REALTIME && lll_futex_timed_wait_bitset]:
00db10
        Use lll_futex_timed_wait_bitset with FUTEX_CLOCK_REALTIME instead
00db10
        of lll_futex_timed_wait.
00db10
00db10
Index: glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/sem_waitcommon.c
00db10
===================================================================
00db10
--- glibc-2.17-c758a686.orig/nptl/sysdeps/unix/sysv/linux/sem_waitcommon.c
00db10
+++ glibc-2.17-c758a686/nptl/sysdeps/unix/sysv/linux/sem_waitcommon.c
00db10
@@ -17,6 +17,7 @@
00db10
    License along with the GNU C Library; if not, see
00db10
    <http://www.gnu.org/licenses/>.  */
00db10
 
00db10
+#include <kernel-features.h>
00db10
 #include <errno.h>
00db10
 #include <sysdep.h>
00db10
 #include <lowlevellock.h>
00db10
@@ -45,6 +46,13 @@ futex_abstimed_wait (unsigned int* futex
00db10
     }
00db10
   else
00db10
     {
00db10
+#if (defined __ASSUME_FUTEX_CLOCK_REALTIME	\
00db10
+     && defined lll_futex_timed_wait_bitset)
00db10
+      /* The Linux kernel returns EINVAL for this, but in userspace
00db10
+	 such a value is valid.  */
00db10
+      if (abstime->tv_sec < 0)
00db10
+	return ETIMEDOUT;
00db10
+#else
00db10
       struct timeval tv;
00db10
       struct timespec rt;
00db10
       int sec, nsec;
00db10
@@ -68,9 +76,16 @@ futex_abstimed_wait (unsigned int* futex
00db10
       /* Do wait.  */
00db10
       rt.tv_sec = sec;
00db10
       rt.tv_nsec = nsec;
00db10
+#endif
00db10
       if (cancel)
00db10
 	oldtype = __pthread_enable_asynccancel ();
00db10
+#if (defined __ASSUME_FUTEX_CLOCK_REALTIME	\
00db10
+     && defined lll_futex_timed_wait_bitset)
00db10
+      err = lll_futex_timed_wait_bitset (futex, expected, abstime,
00db10
+					 FUTEX_CLOCK_REALTIME, private);
00db10
+#else
00db10
       err = lll_futex_timed_wait (futex, expected, &rt, private);
00db10
+#endif
00db10
       if (cancel)
00db10
 	__pthread_disable_asynccancel (oldtype);
00db10
     }