From: Lukasz Majewski <lukma@denx.de>
To: libc-alpha@sourceware.org
Cc: Stepan Golosunov <stepan@golosunov.pp.ru>,
Arnd Bergmann <arnd@arndb.de>, Paul Eggert <eggert@cs.ucla.edu>,
Joseph Myers <joseph@codesourcery.com>,
Lukasz Majewski <lukma@denx.de>
Subject: [PATCH v3 4/5] y2038: linux: Provide __clock_settime64 implementation
Date: Tue, 7 May 2019 15:18:41 +0200 [thread overview]
Message-ID: <20190507131848.30980-5-lukma@denx.de> (raw)
In-Reply-To: <20190507131848.30980-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 - hence the necessary checks and conversion to 64 bit type.
After this change it is intrinsically Y2038 safe.
The new 64 bit syscall (clock_settime64) available from Linux
5.1+ has been used when applicable on 32 bit systems.
The __ASSUME_TIME64_SYSCALLS flag indicates if the Linux kernel provides
64 bit version of clock_settime (i.e. clock_settime64). If this syscall is
not provided by the kernel - the 32 bit version of it is executed instead.
When working on 32 bit systems without Y2038 time support the clock_settime
returns error when one wants to set time with wrong (overflowed) tv_sec
value. Moreover, the correctness of tv_nsec is checked.
In this patch the internal padding (tv_pad) of struct __timespec64 is
left untouched (on 32 bit systems) as Linux kernel ignores upper 32 bits
of tv_nsec.
The execution path on 64 bit systems has not been changed or affected in
any way.
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) by
32 bit Y2038 safe systems
---
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 | 53 +++++++++++++++++++++++++++++++--
2 files changed, 58 insertions(+), 3 deletions(-)
diff --git a/include/time.h b/include/time.h
index 18587fdd8b..670226df0c 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..084edeaa61 100644
--- a/sysdeps/unix/sysv/linux/clock_settime.c
+++ b/sysdeps/unix/sysv/linux/clock_settime.c
@@ -19,11 +19,9 @@
#include <sysdep.h>
#include <time.h>
-#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,55 @@ __clock_settime (clockid_t clock_id, const struct timespec *tp)
return -1;
}
+#ifdef __ASSUME_TIME64_SYSCALLS
+# ifdef __NR_clock_settime64
+ /* For Y2038 safe systems with __WORDSIZE==32 and __TIMESIZE==64
+ (x86, arm) the glibc exported struct timespec has 64 bit tv_sec,
+ 32 bit tv_nsec (to be still POSIX compliant -> long tv_nsec )
+ and 32 bits of unnamed padding.
+
+ It may happen that due to dynamic allocation the tv_pad, which
+ corresponds to upper 32 bits of kernel's 64 bit tv_nsec accepted
+ by syscalls, may not be zero.
+
+ However, the Linux kernel is ignoring those 32 bits (to be more
+ precise - as of 5.1 - it casts 64 bit tv_nsec to internal's 32 bit
+ representation) and hence the padding clearing is not needed. */
+ int ret = INLINE_SYSCALL_CALL (clock_settime64, clock_id, tp);
+ if (ret == 0 || errno != ENOSYS)
+ return ret;
+# endif
+#endif
+
+/* For systems supporting 32 bit time only __WORDSIZE==32 and
+ __TIMESIZE==32 (!=64) the passed struct __timespec64 must be
+ converted to 32 bit one before invoking Linux syscall. */
+#if __WORDSIZE == 32 && __TIMESIZE != 64
+ /* Fall back to syscall supporting 32bit struct timespec. */
+ struct timespec ts32;
+ valid_timespec64_to_timespec (tp, &ts32);
+ return INLINE_SYSCALL_CALL (clock_settime, clock_id, &ts32);
+#else
+/* Systems with __WORDSIZE==64 (i.e. x86_64, aarch64) or
+ __WORDSIZE==32 && __TIMESIZE == 64 (i.e. x32 - special case). */
return INLINE_SYSCALL_CALL (clock_settime, clock_id, tp);
+#endif
}
weak_alias (__clock_settime, clock_settime)
+
+#if __TIMESIZE != 64
+int
+__clock_settime (clockid_t clock_id, const struct timespec *tp)
+{
+ struct __timespec64 ts64;
+
+ if (! in_time_t_range (tp->tv_sec))
+ {
+ __set_errno (EOVERFLOW);
+ return -1;
+ }
+
+ valid_timespec_to_timespec64 (tp, &ts64);
+ return __clock_settime64 (clock_id, &ts64);
+}
+#endif
--
2.11.0
next prev parent reply other threads:[~2019-05-07 13:19 UTC|newest]
Thread overview: 111+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-14 22:08 [PATCH 0/6] y2038: Linux: Provide __clock_* functions supporting 64 bit time Lukasz Majewski
2019-04-14 22:08 ` [PATCH 1/6] y2038: Introduce internal for glibc struct __timespec64 Lukasz Majewski
2019-04-14 22:08 ` [PATCH 2/6] y2038: Provide conversion helpers for " Lukasz Majewski
2019-04-14 22:08 ` [PATCH 3/6] y2038: linux: Provide __clock_settime64 implementation Lukasz Majewski
2019-04-17 22:08 ` Joseph Myers
2019-04-18 6:46 ` Lukasz Majewski
2019-04-20 0:20 ` Stepan Golosunov
2019-04-20 11:21 ` Lukasz Majewski
2019-04-22 9:07 ` Stepan Golosunov
2019-04-22 21:45 ` Arnd Bergmann
2019-04-23 15:45 ` Lukasz Majewski
2019-04-14 22:08 ` [PATCH 4/6] y2038: linux: Provide __clock_getres64 implementation Lukasz Majewski
2019-04-14 22:08 ` [PATCH 5/6] y2038: linux: Provide __clock_gettime64 implementation Lukasz Majewski
2019-04-14 22:08 ` [PATCH 6/6] y2038: linux: Provide __clock_nanosleep64 implementation Lukasz Majewski
2019-04-17 22:11 ` Joseph Myers
2019-04-18 5:54 ` Lukasz Majewski
2019-04-29 10:46 ` [PATCH v2 0/7] y2038: Linux: Provide __clock_* functions supporting 64 bit time Lukasz Majewski
2019-04-29 10:46 ` [PATCH v2 1/7] y2038: Introduce internal for glibc struct __timespec64 Lukasz Majewski
2019-04-29 10:46 ` [PATCH v2 2/7] y2038: Introduce __ASSUME_64BIT_TIME define Lukasz Majewski
2019-04-29 21:50 ` Joseph Myers
2019-04-30 9:05 ` Lukasz Majewski
2019-04-30 21:47 ` Stepan Golosunov
2019-05-02 8:51 ` Lukasz Majewski
2019-05-02 11:54 ` Stepan Golosunov
2019-05-02 13:55 ` Lukasz Majewski
2019-05-06 13:38 ` Lukasz Majewski
2019-05-06 14:26 ` Lukasz Majewski
2019-05-06 19:36 ` Stepan Golosunov
2019-05-06 21:14 ` Lukasz Majewski
2019-05-07 20:03 ` Stepan Golosunov
2019-05-07 20:44 ` Joseph Myers
2019-05-08 7:51 ` Lukasz Majewski
2019-05-08 19:48 ` Stepan Golosunov
2019-05-09 16:12 ` Joseph Myers
2019-05-16 4:00 ` Arnd Bergmann
2019-05-16 19:54 ` Stepan Golosunov
2019-05-16 19:59 ` Joseph Myers
2019-05-17 7:23 ` Florian Weimer
2019-05-17 8:34 ` Arnd Bergmann
2019-05-07 15:43 ` Joseph Myers
2019-05-02 15:04 ` Joseph Myers
2019-05-05 14:10 ` Stepan Golosunov
2019-05-05 20:46 ` Lukasz Majewski
2019-05-06 14:56 ` Lukasz Majewski
2019-05-07 15:35 ` Joseph Myers
2019-05-08 10:18 ` Lukasz Majewski
2019-05-09 15:46 ` Joseph Myers
2019-05-15 15:39 ` Lukasz Majewski
2019-05-16 3:57 ` Arnd Bergmann
2019-05-16 6:58 ` Lukasz Majewski
2019-05-31 11:37 ` Stepan Golosunov
2019-06-05 16:35 ` Lukasz Majewski
2019-06-06 6:50 ` Stepan Golosunov
2019-04-29 10:46 ` [PATCH v2 3/7] y2038: Provide conversion helpers for struct __timespec64 Lukasz Majewski
2019-05-02 11:56 ` Stepan Golosunov
2019-05-02 14:17 ` Lukasz Majewski
2019-05-02 15:59 ` Joseph Myers
2019-05-02 16:19 ` Lukasz Majewski
2019-04-29 10:46 ` [PATCH v2 4/7] y2038: linux: Provide __clock_settime64 implementation Lukasz Majewski
2019-04-29 10:46 ` [PATCH v2 5/7] y2038: linux: Provide __clock_getres64 implementation Lukasz Majewski
2019-04-29 10:46 ` [PATCH v2 6/7] y2038: linux: Provide __clock_gettime64 implementation Lukasz Majewski
2019-04-29 10:46 ` [PATCH v2 7/7] y2038: linux: Provide __clock_nanosleep64 implementation Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 0/5] y2038: Linux: Provide __clock_settime function supporting 64 bit time Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 1/5] y2038: Introduce __ASSUME_TIME64_SYSCALLS define Lukasz Majewski
2019-05-07 16:04 ` Joseph Myers
2019-05-07 13:18 ` [PATCH v3 2/5] y2038: Introduce internal for glibc struct __timespec64 Lukasz Majewski
2019-05-07 13:30 ` Andreas Schwab
2019-05-07 14:07 ` Lukasz Majewski
2019-05-07 14:13 ` Andreas Schwab
2019-05-08 8:32 ` Lukasz Majewski
2019-05-08 8:38 ` Andreas Schwab
2019-05-08 9:10 ` Lukasz Majewski
2019-05-08 9:12 ` Andreas Schwab
2019-05-08 9:37 ` Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 3/5] y2038: Provide conversion helpers for " Lukasz Majewski
2019-05-07 13:18 ` Lukasz Majewski [this message]
2019-05-07 16:40 ` [PATCH v3 4/5] y2038: linux: Provide __clock_settime64 implementation Joseph Myers
2019-05-07 13:18 ` [PATCH v3 5/5] y2038: Support for Y2038 safe time on 32 bit systems Lukasz Majewski
2019-05-07 16:37 ` Joseph Myers
2019-05-08 10:27 ` Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 0/5] y2038: Linux: Provide __clock_settime function supporting 64 bit time Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 1/5] y2038: Introduce __ASSUME_TIME64_SYSCALLS define Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 2/5] y2038: Introduce internal for glibc struct __timespec64 Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 3/5] y2038: Provide conversion helpers for " Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 4/5] y2038: linux: Provide __clock_settime64 implementation Lukasz Majewski
2019-05-07 13:18 ` [PATCH v3 5/5] y2038: Support for Y2038 safe time on 32 bit systems Lukasz Majewski
2019-05-07 13:34 ` Andreas Schwab
2019-05-08 15:56 ` [PATCH] y2038: Introduce __ASSUME_TIME64_SYSCALLS define Lukasz Majewski
2019-05-09 15:58 ` Joseph Myers
2019-05-09 16:18 ` Stepan Golosunov
2019-05-15 14:27 ` [PATCH v5] " Lukasz Majewski
2019-05-21 15:15 ` Lukasz Majewski
2019-05-23 7:34 ` Stepan Golosunov
2019-05-23 9:35 ` Lukasz Majewski
2019-05-25 16:19 ` Lukasz Majewski
2019-05-25 19:45 ` Stepan Golosunov
2019-05-26 12:39 ` Lukasz Majewski
2019-05-28 16:00 ` Lukasz Majewski
2019-05-28 19:53 ` Joseph Myers
2019-05-28 22:46 ` Lukasz Majewski
2019-05-28 22:50 ` Joseph Myers
2019-05-28 19:47 ` Joseph Myers
2019-05-28 22:09 ` Lukasz Majewski
2019-05-28 22:43 ` Joseph Myers
2019-05-23 21:17 ` Joseph Myers
2019-05-20 10:27 ` [PATCH v4 0/3] y2038: Linux: Provide __clock_settime64 function supporting 64 bit time Lukasz Majewski
2019-05-20 10:27 ` [PATCH v4 1/3] y2038: Introduce internal for glibc struct __timespec64 Lukasz Majewski
2019-05-20 10:27 ` [PATCH v4 2/3] y2038: Provide conversion helpers for " Lukasz Majewski
2019-05-20 10:27 ` [PATCH v4 3/3] y2038: linux: Provide __clock_settime64 implementation Lukasz Majewski
2019-05-23 7:08 ` Stepan Golosunov
2019-05-23 9:28 ` Lukasz Majewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/libc/involved.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190507131848.30980-5-lukma@denx.de \
--to=lukma@denx.de \
--cc=arnd@arndb.de \
--cc=eggert@cs.ucla.edu \
--cc=joseph@codesourcery.com \
--cc=libc-alpha@sourceware.org \
--cc=stepan@golosunov.pp.ru \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).