From patchwork Wed May 29 16:18:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Majewski X-Patchwork-Id: 32898 Received: (qmail 7978 invoked by alias); 29 May 2019 16:19:19 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 7954 invoked by uid 89); 29 May 2019 16:19:19 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 spammy=ignorance X-HELO: mail-out.m-online.net From: Lukasz Majewski To: libc-alpha@sourceware.org Cc: Stepan Golosunov , Arnd Bergmann , Paul Eggert , Joseph Myers , Lukasz Majewski Subject: [PATCH v5 3/3] y2038: linux: Provide __clock_settime64 implementation Date: Wed, 29 May 2019 18:18:50 +0200 Message-Id: <20190529161850.30442-4-lukma@denx.de> In-Reply-To: <20190529161850.30442-1-lukma@denx.de> References: <20190529161850.30442-1-lukma@denx.de> This patch provides new __clock_settime64 explicit 64 bit function for setting the time. Moreover, a 32 bit version - __clock_settime - has been refactored to internally use __clock_settime64. The __clock_settime is now supposed to be used on systems still supporting 32 bit time (__TIMESIZE != 64) - hence the necessary conversion to 64 bit struct timespec. The new clock_settime64 syscall available from Linux 5.1+ has been used, when applicable. The __ASSUME_TIME64_SYSCALLS flag indicates if the Linux kernel supports 64 bit version of clock_settime (i.e. clock_settime64). If this flag is not defined, the fallback with legacy clock_settime supporting 32 bit time is used. When working on 32 bit systems without Y2038 time support the clock_settime64 returns error when one wants to set time with wrong (overflowed) tv_sec value. Moreover, the correctness of tv_nsec is also checked. In this patch the internal padding (tv_pad) of struct __timespec64 is left untouched (on systems with __WORDSIZE == 32) as Linux kernel ignores upper 32 bits of tv_nsec. Tests: - The code has been tested with x86_64/x86 (native compilation): make PARALLELMFLAGS="-j8" && make xcheck PARALLELMFLAGS="-j8" - Run specific tests on ARM/x86 32bit systems (qemu): https://github.com/lmajewski/meta-y2038 and run tests: https://github.com/lmajewski/y2038-tests/commits/master on kernels with and without 64 bit time support. No regressions were observed. * include/time.h (__clock_settime64): Add __clock_settime alias according to __TIMESIZE define * sysdeps/unix/sysv/linux/clock_settime.c (__clock_settime): Refactor this function to be used only on 32 bit machines as a wrapper on __clock_settime64. * sysdeps/unix/sysv/linux/clock_settime.c (__clock_settime64): Add * sysdeps/unix/sysv/linux/clock_settime.c (__clock_settime64): Use clock_settime64 kernel syscall (available from 5.1-rc1+ Linux) --- Changes for v5: - Use __ASSUME_TIME64_SYSCALLS to indicate Linux kernel support for 64 bit time. - Move the in_time_t_range() check to __clock_settime64 - Alias __NR_clock_settime64 to __NR_clock_settime if the former is not defined in the headers. Changes for v4: - __ASSUME_TIME64_SYSCALLS for fall back path - Use __SYSCALL_WORDSIZE to exclude 'x32' from execution path (so it will use x86_64 syscall - Rewrite the commit message Changes for v3: - Rename __ASSUME_64BIT_TIME to __ASSUME_TIME64_SYSCALLS - Refactor in-code comment (add information regarding Linux kernel ignorance of padding - Do not use __TIMESIZE to select main execution path (for Y2038 systems __TIMESIZE would be changed from 32 to 64 bits at some point to indicate full Y2038 support Changes for v2: - Add support for __ASSUME_64BIT_TIME flag when Linux kernel provides syscalls supporting 64 bit time on 32 bit systems - Provide fallback to 32 bit version of clock_settime when clock_settime64 is not available - Do not copy *tp to timespec - this seems like an overkill as in clock_settime() the 32 bit struct timespec is copied to internal 64 bit struct __timespec64 --- include/time.h | 8 +++++++ sysdeps/unix/sysv/linux/clock_settime.c | 37 +++++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/include/time.h b/include/time.h index 1b38d019c8..7155b2e4db 100644 --- a/include/time.h +++ b/include/time.h @@ -127,6 +127,14 @@ extern __time64_t __timegm64 (struct tm *__tp) __THROW; libc_hidden_proto (__timegm64) #endif +#if __TIMESIZE == 64 +# define __clock_settime64 __clock_settime +#else +extern int __clock_settime64 (clockid_t clock_id, + const struct __timespec64 *tp); +libc_hidden_proto (__clock_settime64) +#endif + /* Compute the `struct tm' representation of T, offset OFFSET seconds east of UTC, and store year, yday, mon, mday, wday, hour, min, sec into *TP. diff --git a/sysdeps/unix/sysv/linux/clock_settime.c b/sysdeps/unix/sysv/linux/clock_settime.c index d837e3019c..7274f01846 100644 --- a/sysdeps/unix/sysv/linux/clock_settime.c +++ b/sysdeps/unix/sysv/linux/clock_settime.c @@ -19,11 +19,9 @@ #include #include -#include "kernel-posix-cpu-timers.h" - /* Set CLOCK to value TP. */ int -__clock_settime (clockid_t clock_id, const struct timespec *tp) +__clock_settime64 (clockid_t clock_id, const struct __timespec64 *tp) { /* Make sure the time cvalue is OK. */ if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000) @@ -32,6 +30,37 @@ __clock_settime (clockid_t clock_id, const struct timespec *tp) return -1; } - return INLINE_SYSCALL_CALL (clock_settime, clock_id, tp); +#ifdef __ASSUME_TIME64_SYSCALLS +# ifndef __NR_clock_settime64 +# define __NR_clock_settime64 __NR_clock_settime +# endif + return INLINE_SYSCALL_CALL (clock_settime64, clock_id, tp); +#else +# ifdef __NR_clock_settime64 + int ret = INLINE_SYSCALL_CALL (clock_settime64, clock_id, tp); + if (ret == 0 || errno != ENOSYS) + return ret; +# endif + if (! in_time_t_range (tp->tv_sec)) + { + __set_errno (EOVERFLOW); + return -1; + } + + struct timespec ts32; + valid_timespec64_to_timespec (tp, &ts32); + return INLINE_SYSCALL_CALL (clock_settime, clock_id, &ts32); +#endif } weak_alias (__clock_settime, clock_settime) + +#if __TIMESIZE != 64 +int +__clock_settime (clockid_t clock_id, const struct timespec *tp) +{ + struct __timespec64 ts64; + + valid_timespec_to_timespec64 (tp, &ts64); + return __clock_settime64 (clock_id, &ts64); +} +#endif