Skip to content

Commit a84d116

Browse files
arndbKAGA-KOKO
authored andcommitted
y2038: Introduce struct __kernel_old_timeval
Dealing with 'struct timeval' users in the y2038 series is a bit tricky: We have two definitions of timeval that are visible to user space, one comes from glibc (or some other C library), the other comes from linux/time.h. The kernel copy is what we want to be used for a number of structures defined by the kernel itself, e.g. elf_prstatus (used it core dumps), sysinfo and rusage (used in system calls). These generally tend to be used for passing time intervals rather than absolute (epoch-based) times, so they do not suffer from the y2038 overflow. Some of them could be changed to use 64-bit timestamps by creating new system calls, others like the core files cannot easily be changed. An application using these interfaces likely also uses gettimeofday() or other interfaces that use absolute times, and pass 'struct timeval' pointers directly into kernel interfaces, so glibc must redefine their timeval based on a 64-bit time_t when they introduce their y2038-safe interfaces. The only reasonable way forward I see is to remove the 'timeval' definion from the kernel's uapi headers, and change the interfaces that we do not want to (or cannot) duplicate for 64-bit times to use a new __kernel_old_timeval definition instead. This type should be avoided for all new interfaces (those can use 64-bit nanoseconds, or the 64-bit version of timespec instead), and should be used with great care when converting existing interfaces from timeval, to be sure they don't suffer from the y2038 overflow, and only with consensus for the particular user that using __kernel_old_timeval is better than moving to a 64-bit based interface. The structure name is intentionally chosen to not conflict with user space types, and to be ugly enough to discourage its use. Note that ioctl based interfaces that pass a bare 'timeval' pointer cannot change to '__kernel_old_timeval' because the user space source code refers to 'timeval' instead, and we don't want to modify the user space sources if possible. However, any application that relies on a structure to contain an embedded 'timeval' (e.g. by passing a pointer to the member into a function call that expects a timeval pointer) is broken when that structure gets converted to __kernel_old_timeval. I don't see any way around that, and we have to rely on the compiler to produce a warning or compile failure that will alert users when they recompile their sources against a new libc. Signed-off-by: Arnd Bergmann <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: Stephen Boyd <[email protected]> Cc: John Stultz <[email protected]> Cc: Al Viro <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 92af4dc commit a84d116

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

include/linux/time32.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,6 @@ static inline s64 timeval_to_ns(const struct timeval *tv)
217217
* Returns the timeval representation of the nsec parameter.
218218
*/
219219
extern struct timeval ns_to_timeval(const s64 nsec);
220+
extern struct __kernel_old_timeval ns_to_kernel_old_timeval(s64 nsec);
220221

221222
#endif

include/uapi/linux/time.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ struct itimerval {
4242
struct timeval it_value; /* current value */
4343
};
4444

45+
/*
46+
* legacy timeval structure, only embedded in structures that
47+
* traditionally used 'timeval' to pass time intervals (not absolute
48+
* times). Do not add new users. If user space fails to compile
49+
* here, this is probably because it is not y2038 safe and needs to
50+
* be changed to use another interface.
51+
*/
52+
struct __kernel_old_timeval {
53+
__kernel_long_t tv_sec;
54+
__kernel_long_t tv_usec;
55+
};
56+
4557
/*
4658
* The IDs of the various system clocks (for POSIX.1b interval timers):
4759
*/

kernel/time/time.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,18 @@ struct timeval ns_to_timeval(const s64 nsec)
488488
}
489489
EXPORT_SYMBOL(ns_to_timeval);
490490

491+
struct __kernel_old_timeval ns_to_kernel_old_timeval(const s64 nsec)
492+
{
493+
struct timespec64 ts = ns_to_timespec64(nsec);
494+
struct __kernel_old_timeval tv;
495+
496+
tv.tv_sec = ts.tv_sec;
497+
tv.tv_usec = (suseconds_t)ts.tv_nsec / 1000;
498+
499+
return tv;
500+
}
501+
EXPORT_SYMBOL(ns_to_kernel_old_timeval);
502+
491503
/**
492504
* set_normalized_timespec - set timespec sec and nsec parts and normalize
493505
*

0 commit comments

Comments
 (0)