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