]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
linux: Only use 64-bit syscall if required for sigtimedwait
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 16 Jun 2021 12:58:29 +0000 (09:58 -0300)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 22 Jun 2021 15:09:52 +0000 (12:09 -0300)
For !__ASSUME_TIME64_SYSCALLS there is no need to issue a 64-bit syscall
if the provided timeout fits in a 32-bit one.  The 64-bit usage should
be rare since the timeout is a relative one.

Checked on i686-linux-gnu on a 4.15 kernel and on a 5.11 kernel
(with and without --enable-kernel=5.1) and on x86_64-linux-gnu.

Reviewed-by: Lukasz Majewski <lukma@denx.de>
sysdeps/unix/sysv/linux/Makefile
sysdeps/unix/sysv/linux/sigtimedwait.c
sysdeps/unix/sysv/linux/tst-sigtimedwait.c

index bda01878c346d56b4883e8404fbfa81eaddbc9fc..88d7cd7c1ba2ca2f5e99e97e2feb399641aac3e0 100644 (file)
@@ -145,6 +145,8 @@ endif
 
 $(objpfx)tst-ppoll: $(librt)
 $(objpfx)tst-ppoll-time64: $(librt)
+$(objpfx)tst-sigtimedwait: $(librt)
+$(objpfx)tst-sigtimedwait-time64: $(librt)
 $(objpfx)tst-timerfd: $(librt)
 $(objpfx)tst-timerfd-time64: $(librt)
 
index 25ed0adb0d00c32d19b99742aaff2ca7825547ba..607381a0a7d3a5dbbeea6da8f9a0d726e09dc7ca 100644 (file)
@@ -25,20 +25,27 @@ __sigtimedwait64 (const sigset_t *set, siginfo_t *info,
 #ifndef __NR_rt_sigtimedwait_time64
 # define __NR_rt_sigtimedwait_time64 __NR_rt_sigtimedwait
 #endif
-  int result = SYSCALL_CANCEL (rt_sigtimedwait_time64, set, info, timeout,
-                              __NSIG_BYTES);
 
-#ifndef __ASSUME_TIME64_SYSCALLS
-  if (result != 0 && errno == ENOSYS)
+  int result;
+#ifdef __ASSUME_TIME64_SYSCALLS
+  result = SYSCALL_CANCEL (rt_sigtimedwait_time64, set, info, timeout,
+                          __NSIG_BYTES);
+#else
+  bool need_time64 = timeout != NULL && !in_time_t_range (timeout->tv_sec);
+  if (need_time64)
+    {
+      result = SYSCALL_CANCEL (rt_sigtimedwait_time64, set, info, timeout,
+                              __NSIG_BYTES);
+      if (result == 0 || errno != ENOSYS)
+       return result;
+      __set_errno (EOVERFLOW);
+      return -1;
+    }
+  else
     {
       struct timespec ts32, *pts32 = NULL;
       if (timeout != NULL)
        {
-         if (! in_time_t_range (timeout->tv_sec))
-           {
-             __set_errno (EINVAL);
-             return -1;
-           }
          ts32 = valid_timespec64_to_timespec (*timeout);
          pts32 = &ts32;
        }
index 973fb5d30108397fffa3ba6e86afde1141e977f5..a8b9893c619d9be4ed999708d91490b464341f6a 100644 (file)
    <https://www.gnu.org/licenses/>.  */
 
 #include <time.h>
+#include <intprops.h>
 #include <errno.h>
 #include <signal.h>
 #include <support/check.h>
 #include <support/xtime.h>
 #include <support/timespec.h>
+#include <support/support.h>
 #include <stdbool.h>
 
 static int
@@ -47,6 +49,20 @@ test_sigtimedwait_timeout (bool zero_tmo)
   return 0;
 }
 
+static void
+test_sigtimedwait_large_timeout (void)
+{
+  support_create_timer (0, 100000000, false, NULL);
+  struct timespec ts = { TYPE_MAXIMUM (time_t), 0 };
+
+  sigset_t ss_usr1;
+  sigemptyset (&ss_usr1);
+  sigaddset (&ss_usr1, SIGUSR1);
+
+  TEST_COMPARE (sigtimedwait (&ss_usr1, NULL, &ts), -1);
+  TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
+}
+
 static int
 do_test (void)
 {
@@ -56,6 +72,8 @@ do_test (void)
   /* Check if sigtimedwait exits after specified timeout.  */
   test_sigtimedwait_timeout (false);
 
+  test_sigtimedwait_large_timeout ();
+
   return 0;
 }