[15/18] linux: Only use 64-bit syscall if required for sigtimedwait

Message ID 20210617115104.1359598-16-adhemerval.zanella@linaro.org
State New
Headers show
Series
  • More y2038 fixes
Related show

Commit Message

DJ Delorie via Libc-alpha June 17, 2021, 11:51 a.m.
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.
---
 sysdeps/unix/sysv/linux/Makefile           |  2 ++
 sysdeps/unix/sysv/linux/sigtimedwait.c     | 26 ++++++++++++++--------
 sysdeps/unix/sysv/linux/tst-sigtimedwait.c | 18 +++++++++++++++
 3 files changed, 37 insertions(+), 9 deletions(-)

-- 
2.30.2

Comments

Andreas Schwab June 17, 2021, 12:25 p.m. | #1
On Jun 17 2021, Adhemerval Zanella via Libc-alpha wrote:

> +  bool is32bit = timeout != NULL

> +		 ? in_time_t_range (timeout->tv_sec) : true;


     bool is32bit = timeout == NULL || in_time_t_range (timeout->tv_sec);

or

     bool need_time64 = timeout != NULL && !in_time_t_range (timeout->tv_sec);

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
DJ Delorie via Libc-alpha June 22, 2021, 2:58 p.m. | #2
On 17/06/2021 09:25, Andreas Schwab wrote:
> 

>      bool need_time64 = timeout != NULL && !in_time_t_range (timeout->tv_sec);


This is indeed better, I adjusted all the patches to follow this.

Patch

diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile
index 2e5b5e1dc4..9a65fc39c0 100644
--- a/sysdeps/unix/sysv/linux/Makefile
+++ b/sysdeps/unix/sysv/linux/Makefile
@@ -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)
 
diff --git a/sysdeps/unix/sysv/linux/sigtimedwait.c b/sysdeps/unix/sysv/linux/sigtimedwait.c
index 25ed0adb0d..bae9bf44bc 100644
--- a/sysdeps/unix/sysv/linux/sigtimedwait.c
+++ b/sysdeps/unix/sysv/linux/sigtimedwait.c
@@ -25,20 +25,28 @@  __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 is32bit = timeout != NULL
+		 ? in_time_t_range (timeout->tv_sec) : true;
+  if (!is32bit)
+    {
+      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;
 	}
diff --git a/sysdeps/unix/sysv/linux/tst-sigtimedwait.c b/sysdeps/unix/sysv/linux/tst-sigtimedwait.c
index 973fb5d301..a8b9893c61 100644
--- a/sysdeps/unix/sysv/linux/tst-sigtimedwait.c
+++ b/sysdeps/unix/sysv/linux/tst-sigtimedwait.c
@@ -17,11 +17,13 @@ 
    <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;
 }