mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-11-27 00:10:51 +07:00
8fd63a9ea7
Currently it is possible for userspace to see the result of gettimeofday() going backwards by 1 microsecond, assuming that userspace is using the gettimeofday() in the VDSO. The VDSO gettimeofday() algorithm computes the time in "xsecs", which are units of 2^-20 seconds, or approximately 0.954 microseconds, using the algorithm now = (timebase - tb_orig_stamp) * tb_to_xs + stamp_xsec and then converts the time in xsecs to seconds and microseconds. The kernel updates the tb_orig_stamp and stamp_xsec values every tick in update_vsyscall(). If the length of the tick is not an integer number of xsecs, then some precision is lost in converting the current time to xsecs. For example, with CONFIG_HZ=1000, the tick is 1ms long, which is 1048.576 xsecs. That means that stamp_xsec will advance by either 1048 or 1049 on each tick. With the right conditions, it is possible for userspace to get (timebase - tb_orig_stamp) * tb_to_xs being 1049 if the kernel is slightly late in updating the vdso_datapage, and then for stamp_xsec to advance by 1048 when the kernel does update it, and for userspace to then see (timebase - tb_orig_stamp) * tb_to_xs being zero due to integer truncation. The result is that time appears to go backwards by 1 microsecond. To fix this we change the VDSO gettimeofday to use a new field in the VDSO datapage which stores the nanoseconds part of the time as a fractional number of seconds in a 0.32 binary fraction format. (Or put another way, as a 32-bit number in units of 0.23283 ns.) This is convenient because we can use the mulhwu instruction to convert it to either microseconds or nanoseconds. Since it turns out that computing the time of day using this new field is simpler than either using stamp_xsec (as gettimeofday does) or stamp_xtime.tv_nsec (as clock_gettime does), this converts both gettimeofday and clock_gettime to use the new field. The existing __do_get_tspec function is converted to use the new field and take a parameter in r7 that indicates the desired resolution, 1,000,000 for microseconds or 1,000,000,000 for nanoseconds. The __do_get_xsec function is then unused and is deleted. The new algorithm is now = ((timebase - tb_orig_stamp) << 12) * tb_to_xs + (stamp_xtime_seconds << 32) + stamp_sec_fraction with 'now' in units of 2^-32 seconds. That is then converted to seconds and either microseconds or nanoseconds with seconds = now >> 32 partseconds = ((now & 0xffffffff) * resolution) >> 32 The 32-bit VDSO code also makes a further simplification: it ignores the bottom 32 bits of the tb_to_xs value, which is a 0.64 format binary fraction. Doing so gets rid of 4 multiply instructions. Assuming a timebase frequency of 1GHz or less and an update interval of no more than 10ms, the upper 32 bits of tb_to_xs will be at least 4503599, so the error from ignoring the low 32 bits will be at most 2.2ns, which is more than an order of magnitude less than the time taken to do gettimeofday or clock_gettime on our fastest processors, so there is no possibility of seeing inconsistent values due to this. This also moves update_gtod() down next to its only caller, and makes update_vsyscall use the time passed in via the wall_time argument rather than accessing xtime directly. At present, wall_time always points to xtime, but that could change in future. Signed-off-by: Paul Mackerras <paulus@samba.org> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
127 lines
4.3 KiB
C
127 lines
4.3 KiB
C
#ifndef _VDSO_DATAPAGE_H
|
|
#define _VDSO_DATAPAGE_H
|
|
#ifdef __KERNEL__
|
|
|
|
/*
|
|
* Copyright (C) 2002 Peter Bergner <bergner@vnet.ibm.com>, IBM
|
|
* Copyright (C) 2005 Benjamin Herrenschmidy <benh@kernel.crashing.org>,
|
|
* IBM Corp.
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version
|
|
* 2 of the License, or (at your option) any later version.
|
|
*/
|
|
|
|
|
|
/*
|
|
* Note about this structure:
|
|
*
|
|
* This structure was historically called systemcfg and exposed to
|
|
* userland via /proc/ppc64/systemcfg. Unfortunately, this became an
|
|
* ABI issue as some proprietary software started relying on being able
|
|
* to mmap() it, thus we have to keep the base layout at least for a
|
|
* few kernel versions.
|
|
*
|
|
* However, since ppc32 doesn't suffer from this backward handicap,
|
|
* a simpler version of the data structure is used there with only the
|
|
* fields actually used by the vDSO.
|
|
*
|
|
*/
|
|
|
|
/*
|
|
* If the major version changes we are incompatible.
|
|
* Minor version changes are a hint.
|
|
*/
|
|
#define SYSTEMCFG_MAJOR 1
|
|
#define SYSTEMCFG_MINOR 1
|
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
#include <linux/unistd.h>
|
|
#include <linux/time.h>
|
|
|
|
#define SYSCALL_MAP_SIZE ((__NR_syscalls + 31) / 32)
|
|
|
|
/*
|
|
* So here is the ppc64 backward compatible version
|
|
*/
|
|
|
|
#ifdef CONFIG_PPC64
|
|
|
|
struct vdso_data {
|
|
__u8 eye_catcher[16]; /* Eyecatcher: SYSTEMCFG:PPC64 0x00 */
|
|
struct { /* Systemcfg version numbers */
|
|
__u32 major; /* Major number 0x10 */
|
|
__u32 minor; /* Minor number 0x14 */
|
|
} version;
|
|
|
|
/* Note about the platform flags: it now only contains the lpar
|
|
* bit. The actual platform number is dead and burried
|
|
*/
|
|
__u32 platform; /* Platform flags 0x18 */
|
|
__u32 processor; /* Processor type 0x1C */
|
|
__u64 processorCount; /* # of physical processors 0x20 */
|
|
__u64 physicalMemorySize; /* Size of real memory(B) 0x28 */
|
|
__u64 tb_orig_stamp; /* Timebase at boot 0x30 */
|
|
__u64 tb_ticks_per_sec; /* Timebase tics / sec 0x38 */
|
|
__u64 tb_to_xs; /* Inverse of TB to 2^20 0x40 */
|
|
__u64 stamp_xsec; /* 0x48 */
|
|
__u64 tb_update_count; /* Timebase atomicity ctr 0x50 */
|
|
__u32 tz_minuteswest; /* Minutes west of Greenwich 0x58 */
|
|
__u32 tz_dsttime; /* Type of dst correction 0x5C */
|
|
__u32 dcache_size; /* L1 d-cache size 0x60 */
|
|
__u32 dcache_line_size; /* L1 d-cache line size 0x64 */
|
|
__u32 icache_size; /* L1 i-cache size 0x68 */
|
|
__u32 icache_line_size; /* L1 i-cache line size 0x6C */
|
|
|
|
/* those additional ones don't have to be located anywhere
|
|
* special as they were not part of the original systemcfg
|
|
*/
|
|
__u32 dcache_block_size; /* L1 d-cache block size */
|
|
__u32 icache_block_size; /* L1 i-cache block size */
|
|
__u32 dcache_log_block_size; /* L1 d-cache log block size */
|
|
__u32 icache_log_block_size; /* L1 i-cache log block size */
|
|
__s32 wtom_clock_sec; /* Wall to monotonic clock */
|
|
__s32 wtom_clock_nsec;
|
|
struct timespec stamp_xtime; /* xtime as at tb_orig_stamp */
|
|
__u32 stamp_sec_fraction; /* fractional seconds of stamp_xtime */
|
|
__u32 syscall_map_64[SYSCALL_MAP_SIZE]; /* map of syscalls */
|
|
__u32 syscall_map_32[SYSCALL_MAP_SIZE]; /* map of syscalls */
|
|
};
|
|
|
|
#else /* CONFIG_PPC64 */
|
|
|
|
/*
|
|
* And here is the simpler 32 bits version
|
|
*/
|
|
struct vdso_data {
|
|
__u64 tb_orig_stamp; /* Timebase at boot 0x30 */
|
|
__u64 tb_ticks_per_sec; /* Timebase tics / sec 0x38 */
|
|
__u64 tb_to_xs; /* Inverse of TB to 2^20 0x40 */
|
|
__u64 stamp_xsec; /* 0x48 */
|
|
__u32 tb_update_count; /* Timebase atomicity ctr 0x50 */
|
|
__u32 tz_minuteswest; /* Minutes west of Greenwich 0x58 */
|
|
__u32 tz_dsttime; /* Type of dst correction 0x5C */
|
|
__s32 wtom_clock_sec; /* Wall to monotonic clock */
|
|
__s32 wtom_clock_nsec;
|
|
struct timespec stamp_xtime; /* xtime as at tb_orig_stamp */
|
|
__u32 stamp_sec_fraction; /* fractional seconds of stamp_xtime */
|
|
__u32 syscall_map_32[SYSCALL_MAP_SIZE]; /* map of syscalls */
|
|
__u32 dcache_block_size; /* L1 d-cache block size */
|
|
__u32 icache_block_size; /* L1 i-cache block size */
|
|
__u32 dcache_log_block_size; /* L1 d-cache log block size */
|
|
__u32 icache_log_block_size; /* L1 i-cache log block size */
|
|
};
|
|
|
|
#endif /* CONFIG_PPC64 */
|
|
|
|
#ifdef __KERNEL__
|
|
extern struct vdso_data *vdso_data;
|
|
#endif
|
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
#endif /* __KERNEL__ */
|
|
#endif /* _SYSTEMCFG_H */
|