linux: Only use 64-bit syscall if required for pselect

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.  This also avoids the need
to use supports_time64() (which breaks the usage case of live migration
like CRIU or similar).

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>
This commit is contained in:
Adhemerval Zanella 2021-06-15 16:59:50 -03:00
parent ecf2661281
commit 91cf411ad3
5 changed files with 94 additions and 91 deletions

View File

@ -148,6 +148,12 @@ CFLAGS-brk.op = $(no-stack-protector)
include ../Rules include ../Rules
ifeq (yes,$(build-shared))
librt = $(common-objpfx)rt/librt.so
else
librt = $(common-objpfx)rt/librt.a
endif
$(objpfx)libg.a: $(dep-dummy-lib); $(make-dummy-lib) $(objpfx)libg.a: $(dep-dummy-lib); $(make-dummy-lib)
$(objpfx)tst-tsearch: $(libm) $(objpfx)tst-tsearch: $(libm)
@ -162,3 +168,6 @@ tst-allocate_once-ENV = MALLOC_TRACE=$(objpfx)tst-allocate_once.mtrace
$(objpfx)tst-allocate_once-mem.out: $(objpfx)tst-allocate_once.out $(objpfx)tst-allocate_once-mem.out: $(objpfx)tst-allocate_once.out
$(common-objpfx)malloc/mtrace $(objpfx)tst-allocate_once.mtrace > $@; \ $(common-objpfx)malloc/mtrace $(objpfx)tst-allocate_once.mtrace > $@; \
$(evaluate-test) $(evaluate-test)
$(objpfx)tst-pselect: $(librt)
$(objpfx)tst-pselect-time64: $(librt)

View File

@ -16,14 +16,14 @@
<https://www.gnu.org/licenses/>. */ <https://www.gnu.org/licenses/>. */
#include <errno.h> #include <errno.h>
#include <signal.h> #include <intprops.h>
#include <stdio.h> #include <support/check.h>
#include <unistd.h> #include <support/support.h>
#include <sys/select.h> #include <support/xsignal.h>
#include <sys/wait.h> #include <support/xunistd.h>
#include <support/xtime.h>
#include <stdlib.h> #include <stdlib.h>
static volatile int handler_called; static volatile int handler_called;
static void static void
@ -33,59 +33,43 @@ handler (int sig)
} }
static int static void
do_test (void) test_pselect_basic (void)
{ {
struct sigaction sa; struct sigaction sa;
sa.sa_handler = handler; sa.sa_handler = handler;
sa.sa_flags = 0; sa.sa_flags = 0;
sigemptyset (&sa.sa_mask); sigemptyset (&sa.sa_mask);
if (sigaction (SIGUSR1, &sa, NULL) != 0) xsigaction (SIGUSR1, &sa, NULL);
{
puts ("sigaction failed");
return 1;
}
sa.sa_handler = SIG_IGN; sa.sa_handler = SIG_IGN;
if (sigaction (SIGCHLD, &sa, NULL) != 0) xsigaction (SIGCHLD, &sa, NULL);
{
puts ("2nd sigaction failed");
return 1;
}
sigset_t ss_usr1; sigset_t ss_usr1;
sigemptyset (&ss_usr1); sigemptyset (&ss_usr1);
sigaddset (&ss_usr1, SIGUSR1); sigaddset (&ss_usr1, SIGUSR1);
if (sigprocmask (SIG_BLOCK, &ss_usr1, NULL) != 0) TEST_COMPARE (sigprocmask (SIG_BLOCK, &ss_usr1, NULL), 0);
{
puts ("sigprocmask failed");
return 1;
}
int fds[2][2]; int fds[2][2];
xpipe (fds[0]);
if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0) xpipe (fds[1]);
{
puts ("pipe failed");
return 1;
}
fd_set rfds; fd_set rfds;
FD_ZERO (&rfds); FD_ZERO (&rfds);
sigset_t ss; sigset_t ss;
sigprocmask (SIG_SETMASK, NULL, &ss); TEST_COMPARE (sigprocmask (SIG_SETMASK, NULL, &ss), 0);
sigdelset (&ss, SIGUSR1); sigdelset (&ss, SIGUSR1);
struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 }; struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
pid_t parent = getpid (); pid_t parent = getpid ();
pid_t p = fork (); pid_t p = xfork ();
if (p == 0) if (p == 0)
{ {
close (fds[0][1]); xclose (fds[0][1]);
close (fds[1][0]); xclose (fds[1][0]);
FD_SET (fds[0][0], &rfds); FD_SET (fds[0][0], &rfds);
@ -93,55 +77,63 @@ do_test (void)
do do
{ {
if (getppid () != parent) if (getppid () != parent)
exit (2); FAIL_EXIT1 ("getppid()=%d != parent=%d", getppid(), parent);
errno = 0; errno = 0;
e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss); e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
} }
while (e == 0); while (e == 0);
if (e != -1) TEST_COMPARE (e, -1);
{ TEST_COMPARE (errno, EINTR);
puts ("child: pselect did not fail");
return 0;
}
if (errno != EINTR)
{
puts ("child: pselect did not set errno to EINTR");
return 0;
}
TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3)); TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
exit (0); exit (0);
} }
close (fds[0][0]); xclose (fds[0][0]);
close (fds[1][1]); xclose (fds[1][1]);
FD_SET (fds[1][0], &rfds); FD_SET (fds[1][0], &rfds);
kill (p, SIGUSR1); TEST_COMPARE (kill (p, SIGUSR1), 0);
int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss); int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
if (e == -1) TEST_COMPARE (e, 1);
{ TEST_VERIFY (FD_ISSET (fds[1][0], &rfds));
puts ("parent: pselect failed"); }
return 1;
} static void
if (e != 1) test_pselect_large_timeout (void)
{ {
puts ("parent: pselect did not report readable fd"); support_create_timer (0, 100000000, false, NULL);
return 1;
} int fds[2];
if (!FD_ISSET (fds[1][0], &rfds)) xpipe (fds);
{
puts ("parent: pselect reports wrong fd"); fd_set rfds;
return 1; FD_ZERO (&rfds);
} FD_SET (fds[0], &rfds);
sigset_t ss;
TEST_COMPARE (sigprocmask (SIG_SETMASK, NULL, &ss), 0);
sigdelset (&ss, SIGALRM);
struct timespec ts = { TYPE_MAXIMUM (time_t), 0 };
TEST_COMPARE (pselect (fds[0] + 1, &rfds, NULL, NULL, &ts, &ss), -1);
TEST_VERIFY (errno == EINTR || errno == EOVERFLOW);
}
static int
do_test (void)
{
test_pselect_basic ();
test_pselect_large_timeout ();
return 0; return 0;
} }
#define TEST_FUNCTION do_test () #include <support/test-driver.c>
#include "../test-skeleton.c"

View File

@ -35,8 +35,7 @@ __pselect32 (int nfds, fd_set *readfds, fd_set *writefds,
struct timeval tv32, *ptv32 = NULL; struct timeval tv32, *ptv32 = NULL;
if (timeout != NULL) if (timeout != NULL)
{ {
if (! in_time_t_range (timeout->tv_sec) if (! valid_nanoseconds (timeout->tv_nsec))
|| ! valid_nanoseconds (timeout->tv_nsec))
{ {
__set_errno (EINVAL); __set_errno (EINVAL);
return -1; return -1;

View File

@ -18,7 +18,23 @@
#include <sys/select.h> #include <sys/select.h>
#include <sysdep-cancel.h> #include <sysdep-cancel.h>
#include <time64-support.h>
static int
pselect64_syscall (int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, const struct __timespec64 *timeout,
const sigset_t *sigmask)
{
#ifndef __NR_pselect6_time64
# define __NR_pselect6_time64 __NR_pselect6
#endif
/* NB: This is required by ARGIFY used in x32 internal_syscallN. */
__syscall_ulong_t data[2] =
{
(uintptr_t) sigmask, __NSIG_BYTES
};
return SYSCALL_CANCEL (pselect6_time64, nfds, readfds, writefds, exceptfds,
timeout, data);
}
int int
__pselect64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, __pselect64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
@ -37,30 +53,23 @@ __pselect64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
we can only pass in 6 directly. If there is an architecture with we can only pass in 6 directly. If there is an architecture with
support for more parameters a new version of this file needs to support for more parameters a new version of this file needs to
be created. */ be created. */
#ifdef __ASSUME_TIME64_SYSCALLS
#ifndef __NR_pselect6_time64 return pselect64_syscall (nfds, readfds, writefds, exceptfds, timeout,
# define __NR_pselect6_time64 __NR_pselect6 sigmask);
#endif #else
int r; bool need_time64 = timeout != NULL && !in_time_t_range (timeout->tv_sec);
if (supports_time64 ()) if (need_time64)
{ {
/* NB: This is required by ARGIFY used in x32 internal_syscallN. */ int r = pselect64_syscall (nfds, readfds, writefds, exceptfds, timeout,
__syscall_ulong_t data[2] = sigmask);
{
(uintptr_t) sigmask, __NSIG_BYTES
};
r = SYSCALL_CANCEL (pselect6_time64, nfds, readfds, writefds, exceptfds,
timeout, data);
if (r == 0 || errno != ENOSYS) if (r == 0 || errno != ENOSYS)
return r; return r;
__set_errno (EOVERFLOW);
mark_time64_unsupported (); return -1;
} }
#ifndef __ASSUME_TIME64_SYSCALLS return __pselect32 (nfds, readfds, writefds, exceptfds, timeout, sigmask);
r = __pselect32 (nfds, readfds, writefds, exceptfds, timeout, sigmask);
#endif #endif
return r;
} }
#if __TIMESIZE != 64 #if __TIMESIZE != 64

View File

@ -29,12 +29,6 @@ __pselect32 (int nfds, fd_set *readfds, fd_set *writefds,
struct timespec ts32, *pts32 = NULL; struct timespec ts32, *pts32 = NULL;
if (timeout != NULL) if (timeout != NULL)
{ {
if (! in_time_t_range (timeout->tv_sec))
{
__set_errno (EINVAL);
return -1;
}
ts32 = valid_timespec64_to_timespec (*timeout); ts32 = valid_timespec64_to_timespec (*timeout);
pts32 = &ts32; pts32 = &ts32;
} }