Skip to content

Commit

Permalink
ktime: Kill non-scalar ktime_t implementation for 2038
Browse files Browse the repository at this point in the history
The non-scalar ktime_t implementation is basically a timespec
which has to be changed to support dates past 2038 on 32bit
systems.

This patch removes the non-scalar ktime_t implementation, forcing
the scalar s64 nanosecond version on all architectures.

This may have additional performance overhead on some 32bit
systems when converting between ktime_t and timespec structures,
however the majority of 32bit systems (arm and i386) were already
using scalar ktime_t, so no performance regressions will be seen
on those platforms.

On affected platforms, I'm open to finding optimizations, including
avoiding converting to timespecs where possible.

[ tglx: We can now cleanup the ktime_t.tv64 mess, but thats a
  different issue and we can throw a coccinelle script at it ]

Signed-off-by: John Stultz <[email protected]>
Signed-off-by: Thomas Gleixner <[email protected]>
Signed-off-by: John Stultz <[email protected]>
  • Loading branch information
johnstultz-work committed Jul 23, 2014
1 parent 76f4108 commit 24e4a8c
Show file tree
Hide file tree
Showing 9 changed files with 7 additions and 246 deletions.
1 change: 0 additions & 1 deletion arch/arm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ config ARM
select HAVE_UID16
select HAVE_VIRT_CPU_ACCOUNTING_GEN
select IRQ_FORCED_THREADING
select KTIME_SCALAR
select MODULES_USE_ELF_REL
select NO_BOOTMEM
select OLD_SIGACTION
Expand Down
1 change: 0 additions & 1 deletion arch/hexagon/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ config HEXAGON
select GENERIC_IOMAP
select GENERIC_SMP_IDLE_THREAD
select STACKTRACE_SUPPORT
select KTIME_SCALAR
select GENERIC_CLOCKEVENTS
select GENERIC_CLOCKEVENTS_BROADCAST
select MODULES_USE_ELF_RELA
Expand Down
1 change: 0 additions & 1 deletion arch/s390/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ config S390
select HAVE_SYSCALL_TRACEPOINTS
select HAVE_UID16 if 32BIT
select HAVE_VIRT_CPU_ACCOUNTING
select KTIME_SCALAR if 32BIT
select MODULES_USE_ELF_RELA
select NO_BOOTMEM
select OLD_SIGACTION
Expand Down
1 change: 0 additions & 1 deletion arch/x86/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ config X86
select ARCH_CLOCKSOURCE_DATA
select GENERIC_CLOCKEVENTS_BROADCAST if X86_64 || (X86_32 && X86_LOCAL_APIC)
select GENERIC_TIME_VSYSCALL
select KTIME_SCALAR if X86_32
select GENERIC_STRNCPY_FROM_USER
select GENERIC_STRNLEN_USER
select HAVE_CONTEXT_TRACKING if X86_64
Expand Down
173 changes: 1 addition & 172 deletions include/linux/ktime.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,43 +27,19 @@
/*
* ktime_t:
*
* On 64-bit CPUs a single 64-bit variable is used to store the hrtimers
* A single 64-bit variable is used to store the hrtimers
* internal representation of time values in scalar nanoseconds. The
* design plays out best on 64-bit CPUs, where most conversions are
* NOPs and most arithmetic ktime_t operations are plain arithmetic
* operations.
*
* On 32-bit CPUs an optimized representation of the timespec structure
* is used to avoid expensive conversions from and to timespecs. The
* endian-aware order of the tv struct members is chosen to allow
* mathematical operations on the tv64 member of the union too, which
* for certain operations produces better code.
*
* For architectures with efficient support for 64/32-bit conversions the
* plain scalar nanosecond based representation can be selected by the
* config switch CONFIG_KTIME_SCALAR.
*/
union ktime {
s64 tv64;
#if BITS_PER_LONG != 64 && !defined(CONFIG_KTIME_SCALAR)
struct {
# ifdef __BIG_ENDIAN
s32 sec, nsec;
# else
s32 nsec, sec;
# endif
} tv;
#endif
};

typedef union ktime ktime_t; /* Kill this */

/*
* ktime_t definitions when using the 64-bit scalar representation:
*/

#if (BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)

/**
* ktime_set - Set a ktime_t variable from a seconds/nanoseconds value
* @secs: seconds to set
Expand Down Expand Up @@ -123,153 +99,6 @@ static inline ktime_t timeval_to_ktime(struct timeval tv)
/* Convert ktime_t to nanoseconds - NOP in the scalar storage format: */
#define ktime_to_ns(kt) ((kt).tv64)

#else /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */

/*
* Helper macros/inlines to get the ktime_t math right in the timespec
* representation. The macros are sometimes ugly - their actual use is
* pretty okay-ish, given the circumstances. We do all this for
* performance reasons. The pure scalar nsec_t based code was nice and
* simple, but created too many 64-bit / 32-bit conversions and divisions.
*
* Be especially aware that negative values are represented in a way
* that the tv.sec field is negative and the tv.nsec field is greater
* or equal to zero but less than nanoseconds per second. This is the
* same representation which is used by timespecs.
*
* tv.sec < 0 and 0 >= tv.nsec < NSEC_PER_SEC
*/

/* Set a ktime_t variable to a value in sec/nsec representation: */
static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)
{
return (ktime_t) { .tv = { .sec = secs, .nsec = nsecs } };
}

/**
* ktime_sub - subtract two ktime_t variables
* @lhs: minuend
* @rhs: subtrahend
*
* Return: The remainder of the subtraction.
*/
static inline ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs)
{
ktime_t res;

res.tv64 = lhs.tv64 - rhs.tv64;
if (res.tv.nsec < 0)
res.tv.nsec += NSEC_PER_SEC;

return res;
}

/**
* ktime_add - add two ktime_t variables
* @add1: addend1
* @add2: addend2
*
* Return: The sum of @add1 and @add2.
*/
static inline ktime_t ktime_add(const ktime_t add1, const ktime_t add2)
{
ktime_t res;

res.tv64 = add1.tv64 + add2.tv64;
/*
* performance trick: the (u32) -NSEC gives 0x00000000Fxxxxxxx
* so we subtract NSEC_PER_SEC and add 1 to the upper 32 bit.
*
* it's equivalent to:
* tv.nsec -= NSEC_PER_SEC
* tv.sec ++;
*/
if (res.tv.nsec >= NSEC_PER_SEC)
res.tv64 += (u32)-NSEC_PER_SEC;

return res;
}

/**
* ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
* @kt: addend
* @nsec: the scalar nsec value to add
*
* Return: The sum of @kt and @nsec in ktime_t format.
*/
extern ktime_t ktime_add_ns(const ktime_t kt, u64 nsec);

/**
* ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
* @kt: minuend
* @nsec: the scalar nsec value to subtract
*
* Return: The subtraction of @nsec from @kt in ktime_t format.
*/
extern ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec);

/**
* timespec_to_ktime - convert a timespec to ktime_t format
* @ts: the timespec variable to convert
*
* Return: A ktime_t variable with the converted timespec value.
*/
static inline ktime_t timespec_to_ktime(const struct timespec ts)
{
return (ktime_t) { .tv = { .sec = (s32)ts.tv_sec,
.nsec = (s32)ts.tv_nsec } };
}

/**
* timeval_to_ktime - convert a timeval to ktime_t format
* @tv: the timeval variable to convert
*
* Return: A ktime_t variable with the converted timeval value.
*/
static inline ktime_t timeval_to_ktime(const struct timeval tv)
{
return (ktime_t) { .tv = { .sec = (s32)tv.tv_sec,
.nsec = (s32)(tv.tv_usec *
NSEC_PER_USEC) } };
}

/**
* ktime_to_timespec - convert a ktime_t variable to timespec format
* @kt: the ktime_t variable to convert
*
* Return: The timespec representation of the ktime value.
*/
static inline struct timespec ktime_to_timespec(const ktime_t kt)
{
return (struct timespec) { .tv_sec = (time_t) kt.tv.sec,
.tv_nsec = (long) kt.tv.nsec };
}

/**
* ktime_to_timeval - convert a ktime_t variable to timeval format
* @kt: the ktime_t variable to convert
*
* Return: The timeval representation of the ktime value.
*/
static inline struct timeval ktime_to_timeval(const ktime_t kt)
{
return (struct timeval) {
.tv_sec = (time_t) kt.tv.sec,
.tv_usec = (suseconds_t) (kt.tv.nsec / NSEC_PER_USEC) };
}

/**
* ktime_to_ns - convert a ktime_t variable to scalar nanoseconds
* @kt: the ktime_t variable to convert
*
* Return: The scalar nanoseconds representation of @kt.
*/
static inline s64 ktime_to_ns(const ktime_t kt)
{
return (s64) kt.tv.sec * NSEC_PER_SEC + kt.tv.nsec;
}

#endif /* !((BITS_PER_LONG == 64) || defined(CONFIG_KTIME_SCALAR)) */

/**
* ktime_equal - Compares two ktime_t variables to see if they are equal
Expand Down
11 changes: 4 additions & 7 deletions include/linux/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ extern struct timezone sys_tz;

#define TIME_T_MAX (time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)

/* Located here for timespec_valid_strict */
#define KTIME_MAX ((s64)~((u64)1 << 63))
#define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)

static inline int timespec_equal(const struct timespec *a,
const struct timespec *b)
{
Expand Down Expand Up @@ -84,13 +88,6 @@ static inline struct timespec timespec_sub(struct timespec lhs,
return ts_delta;
}

#define KTIME_MAX ((s64)~((u64)1 << 63))
#if (BITS_PER_LONG == 64)
# define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC)
#else
# define KTIME_SEC_MAX LONG_MAX
#endif

/*
* Returns true if the timespec is norm, false if denorm:
*/
Expand Down
4 changes: 0 additions & 4 deletions kernel/time/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ config GENERIC_TIME_VSYSCALL
config GENERIC_TIME_VSYSCALL_OLD
bool

# ktime_t scalar 64bit nsec representation
config KTIME_SCALAR
bool

# Old style timekeeping
config ARCH_USES_GETTIMEOFFSET
bool
Expand Down
54 changes: 0 additions & 54 deletions kernel/time/hrtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,60 +261,6 @@ lock_hrtimer_base(const struct hrtimer *timer, unsigned long *flags)
* too large for inlining:
*/
#if BITS_PER_LONG < 64
# ifndef CONFIG_KTIME_SCALAR
/**
* ktime_add_ns - Add a scalar nanoseconds value to a ktime_t variable
* @kt: addend
* @nsec: the scalar nsec value to add
*
* Returns the sum of kt and nsec in ktime_t format
*/
ktime_t ktime_add_ns(const ktime_t kt, u64 nsec)
{
ktime_t tmp;

if (likely(nsec < NSEC_PER_SEC)) {
tmp.tv64 = nsec;
} else {
unsigned long rem = do_div(nsec, NSEC_PER_SEC);

/* Make sure nsec fits into long */
if (unlikely(nsec > KTIME_SEC_MAX))
return (ktime_t){ .tv64 = KTIME_MAX };

tmp = ktime_set((long)nsec, rem);
}

return ktime_add(kt, tmp);
}

EXPORT_SYMBOL_GPL(ktime_add_ns);

/**
* ktime_sub_ns - Subtract a scalar nanoseconds value from a ktime_t variable
* @kt: minuend
* @nsec: the scalar nsec value to subtract
*
* Returns the subtraction of @nsec from @kt in ktime_t format
*/
ktime_t ktime_sub_ns(const ktime_t kt, u64 nsec)
{
ktime_t tmp;

if (likely(nsec < NSEC_PER_SEC)) {
tmp.tv64 = nsec;
} else {
unsigned long rem = do_div(nsec, NSEC_PER_SEC);

tmp = ktime_set((long)nsec, rem);
}

return ktime_sub(kt, tmp);
}

EXPORT_SYMBOL_GPL(ktime_sub_ns);
# endif /* !CONFIG_KTIME_SCALAR */

/*
* Divide a ktime value by a nanosecond value
*/
Expand Down
7 changes: 2 additions & 5 deletions kernel/time/timekeeping.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,8 @@ ktime_t ktime_get(void)
nsecs = timekeeping_get_ns(tk) + tk->wall_to_monotonic.tv_nsec;

} while (read_seqcount_retry(&timekeeper_seq, seq));
/*
* Use ktime_set/ktime_add_ns to create a proper ktime on
* 32-bit architectures without CONFIG_KTIME_SCALAR.
*/
return ktime_add_ns(ktime_set(secs, 0), nsecs);

return ktime_set(secs, nsecs);
}
EXPORT_SYMBOL_GPL(ktime_get);

Expand Down

0 comments on commit 24e4a8c

Please sign in to comment.