From patchwork Wed Mar 31 17:56:10 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 42823 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E8B4538515F4; Wed, 31 Mar 2021 17:56:18 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8B4538515F4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1617213379; bh=8eazvy45D2OdfQPc5qXLhkYTWFjkuqI3qBZ4jBXcKqo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=pPVcmsHXA3bP9aD7bE79Yz3gDk0jh+YXx8mSF3+mKSYzSPiCoqkfuZkSCFlIyfOKI SperZE6NrAiIJghZAXFMbITuuU9645uSUALxQGqL8nHPrATGV8CHScDbYmlbHVhrii ReulOpkjLJih0pkr79DI0uQ9+EkcEq6F+nc6A7tw= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by sourceware.org (Postfix) with ESMTPS id BA9CA3857C75 for ; Wed, 31 Mar 2021 17:56:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org BA9CA3857C75 Received: by mail-qk1-x734.google.com with SMTP id v70so20232414qkb.8 for ; Wed, 31 Mar 2021 10:56:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8eazvy45D2OdfQPc5qXLhkYTWFjkuqI3qBZ4jBXcKqo=; b=onCOIgjAvEZgQ9GaLG1p6qT9yMZ0KKXJEpM01tn9Qk060fHbP7K+IHm/IOrdzt2hkV uXo7Chk6TMA0poDr6JKzRT8SCE8NuEq+DNFRMv6pNdP0S0Yayw9UQGjZES1toUYIgvFu xmDPDcXs3k7pGcGALpJan4i0tBLVTKxfmSvj9qhCMr9kMKk8OJpax/ZkG0u5FkZCIbD3 WUDHpYxKAWWUqW5FRm30zL62Frkqj3ispwp3T/YxUANv9Ia0rsMtLsjkaTbc2/6A9SWs Q/9qDVS1F6dS//acuOE0ZrBldiWoAGW3RxDuVEPoLWohy/uyvW1ZETKYAU9tA2Vanjba Cy7w== X-Gm-Message-State: AOAM530XYBUdMiu1tiB6dY2Fd5jaan8DBKueJoZy+vw5odDHfzkyDlRT Mvmiz8/BZjDvf4gIszvpJlkpwocRNnSKSg== X-Google-Smtp-Source: ABdhPJwCTex4+xtzwzJRh6fkQyCO9NnITk5Hr0vsUnpfdEvi6Ks3UEf0UtmQ9ecZZvTM/AqoIwFUSA== X-Received: by 2002:a05:620a:46:: with SMTP id t6mr4287205qkt.358.1617213374820; Wed, 31 Mar 2021 10:56:14 -0700 (PDT) Received: from localhost.localdomain ([2804:14c:485:832:932f:4cf0:cfa7:3d8c]) by smtp.googlemail.com with ESMTPSA id 184sm1965618qki.97.2021.03.31.10.56.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Mar 2021 10:56:14 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] linux: Normalize and return timeout on select (BZ #27651) Date: Wed, 31 Mar 2021 14:56:10 -0300 Message-Id: <20210331175610.3002618-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Cc: rcd99@zipmail.com.br Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The commit 2433d39b697, which added time64 support to select, changed the function to use __NR_pselect6 (or __NR_pelect6_time64) on all architectures. However, on architectures where the symbol was implemented with __NR_select the kernel normalizes the passed timeout instead of return EINVAL. For instance, the input timeval { 0, 5000000 } is interpreted as { 5, 0 }. And as indicated by BZ #27651, this semantic seems to be expected and changing it results in some performance issues (most likely the program does not check the return code and keeps issuing select with unormalized tv_usec argument). To avoid a different semantic depending whether which syscall the architecture used to issue, select now always normalize the timeout input. This is a slight change for some ABIs (for instance aarch64). Checked on x86_64-linux-gnu and i686-linux-gnu. --- include/time.h | 5 +++ misc/tst-select.c | 17 +++++++++ sunrpc/svcauth_des.c | 1 - support/Makefile | 2 ++ support/support.h | 8 +++++ support/support_select_modify_timeout.c | 29 ++++++++++++++++ support/support_select_normalize_timeout.c | 29 ++++++++++++++++ sysdeps/unix/sysv/linux/select.c | 40 ++++++++++++++++++---- 8 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 support/support_select_modify_timeout.c create mode 100644 support/support_select_normalize_timeout.c diff --git a/include/time.h b/include/time.h index caf2af5e74..e0636132a6 100644 --- a/include/time.h +++ b/include/time.h @@ -502,6 +502,11 @@ time_now (void) __clock_gettime (TIME_CLOCK_GETTIME_CLOCKID, &ts); return ts.tv_sec; } + +#define NSEC_PER_SEC 1000000000L /* Nanoseconds per second. */ +#define USEC_PER_SEC 1000000L /* Microseconds per second. */ +#define NSEC_PER_USEC 1000L /* Nanoseconds per microsecond. */ + #endif #endif diff --git a/misc/tst-select.c b/misc/tst-select.c index 5ad057cd51..dc7717a7a6 100644 --- a/misc/tst-select.c +++ b/misc/tst-select.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,12 @@ do_test_child (void *clousure) int r = select (args->fds[0][0] + 1, &rfds, NULL, NULL, &args->tmo); TEST_COMPARE (r, 0); + if (support_select_modify_timeout ()) + { + TEST_COMPARE (args->tmo.tv_sec, 0); + TEST_COMPARE (args->tmo.tv_usec, 0); + } + TEST_TIMESPEC_NOW_OR_AFTER (CLOCK_REALTIME, ts); xwrite (args->fds[1][1], "foo", 3); @@ -69,6 +76,16 @@ do_test (void) sc_allow_none); } + if (support_select_normalize_timeout ()) + { + /* This is handled as 1 second instead of failing with EINVAL. */ + args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 1000000 }; + struct support_capture_subprocess result; + result = support_capture_subprocess (do_test_child, &args); + support_capture_subprocess_check (&result, "tst-select-child", 0, + sc_allow_none); + } + /* Same as before, but simulating polling. */ args.tmo = (struct timeval) { .tv_sec = 0, .tv_usec = 0 }; { diff --git a/sunrpc/svcauth_des.c b/sunrpc/svcauth_des.c index 7607abc818..25a85c9097 100644 --- a/sunrpc/svcauth_des.c +++ b/sunrpc/svcauth_des.c @@ -58,7 +58,6 @@ #define debug(msg) /*printf("svcauth_des: %s\n", msg) */ -#define USEC_PER_SEC ((uint32_t) 1000000L) #define BEFORE(t1, t2) timercmp(t1, t2, <) /* diff --git a/support/Makefile b/support/Makefile index 900e17f94f..1e2fc97ee6 100644 --- a/support/Makefile +++ b/support/Makefile @@ -68,6 +68,8 @@ libsupport-routines = \ support_quote_string \ support_record_failure \ support_run_diff \ + support_select_modify_timeout \ + support_select_normalize_timeout \ support_set_small_thread_stack_size \ support_shared_allocate \ support_small_stack_thread_attribute \ diff --git a/support/support.h b/support/support.h index b0352726bf..12dc1d0496 100644 --- a/support/support.h +++ b/support/support.h @@ -144,6 +144,14 @@ static __inline bool support_path_support_time64 (const char *path) /* Return true if stat supports nanoseconds resolution. */ extern bool support_stat_nanoseconds (void); +/* Return true if select modify the timeout to reflect the amount of time + no slept. */ +extern bool support_select_modify_timeout (void); + +/* Return true if select normalize the timeout input by taking in account + tv_usec larger than 1000000. */ +extern bool support_select_normalize_timeout (void); + __END_DECLS #endif /* SUPPORT_H */ diff --git a/support/support_select_modify_timeout.c b/support/support_select_modify_timeout.c new file mode 100644 index 0000000000..d70a5a5068 --- /dev/null +++ b/support/support_select_modify_timeout.c @@ -0,0 +1,29 @@ +/* Return whether select modify the timeout. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +bool +support_select_modify_timeout (void) +{ +#ifdef __linux__ + return true; +#else + return false; +#endif +} diff --git a/support/support_select_normalize_timeout.c b/support/support_select_normalize_timeout.c new file mode 100644 index 0000000000..447e3ec3e3 --- /dev/null +++ b/support/support_select_normalize_timeout.c @@ -0,0 +1,29 @@ +/* Return whether select normalize the timeout. + Copyright (C) 2021 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +bool +support_select_normalize_timeout (void) +{ +#ifdef __linux__ + return true; +#else + return false; +#endif +} diff --git a/sysdeps/unix/sysv/linux/select.c b/sysdeps/unix/sysv/linux/select.c index 415aa87d3c..d075270ff4 100644 --- a/sysdeps/unix/sysv/linux/select.c +++ b/sysdeps/unix/sysv/linux/select.c @@ -33,12 +33,34 @@ int __select64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct __timeval64 *timeout) { - struct __timespec64 ts64, *pts64 = NULL; - if (timeout != NULL) + __time64_t s = timeout != NULL ? timeout->tv_sec : 0; + int32_t us = timeout != NULL ? timeout->tv_usec : 0; + int32_t ns; + + if (s < 0 || us < 0) + return INLINE_SYSCALL_ERROR_RETURN_VALUE (EINVAL); + + /* Normalize the timeout, as legacy Linux __NR_select and __NR__newselect. + Different than syscall, it also handle possible overflow. */ + if (us / USEC_PER_SEC > INT64_MAX - s) { - ts64 = timeval64_to_timespec64 (*timeout); - pts64 = &ts64; + s = INT64_MAX; + ns = NSEC_PER_SEC - 1; } + else + { + s += us / USEC_PER_SEC; + us = us % USEC_PER_SEC; + ns = us * NSEC_PER_USEC; + } + + struct __timespec64 ts64, *pts64 = NULL; + if (timeout != NULL) + { + ts64.tv_sec = s; + ts64.tv_nsec = ns; + pts64 = &ts64; + } #ifndef __NR_pselect6_time64 # define __NR_pselect6_time64 __NR_pselect6 @@ -52,10 +74,13 @@ __select64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, (though the pselect() glibc call suppresses this behavior). Since select() on Linux has the same behavior as the pselect6 syscall, we update the timeout here. */ - if (r == 0 || errno != ENOSYS) + if (r >= 0 || errno != ENOSYS) { if (timeout != NULL) - TIMEVAL_TO_TIMESPEC (timeout, &ts64); + { + timeout->tv_sec = ts64.tv_sec; + timeout->tv_usec = ts64.tv_nsec / NSEC_PER_USEC; + } return r; } @@ -71,7 +96,8 @@ __select64 (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, __set_errno (EINVAL); return -1; } - ts32 = valid_timespec64_to_timespec (ts64); + ts32.tv_sec = s; + ts32.tv_nsec = ns; pts32 = &ts32; } # ifndef __ASSUME_PSELECT