linux-2.6.2_vsyscall-gtod_B2.patch
RFC PATCH 1/1 linux-2.6.2_vsyscall-gtod_B2.patch
All,
This is my port of the x86-64 vsyscall gettimeofday code to
i386. This patch moves gettimeofday into userspace, so it can be called without the syscall overhead, greatly improving performance. This is important for any application, like a database, which heavily uses gettimeofday for time-stamping. It supports both the TSC and IBM x44X cyclone time source.
Unlike previous versions, no LD_PRELOADing or changes to glibc are needed with this patch. However, the entry point to the user-space vgettimeofday function is called by the kernel's vsyscall-sysenter page so a recent glibc w/ sysenter support is needed to see any benefit (Thanks to Ulrich for the suggestion!)
Example performance gain (using cyclone timesource):
int80 gettimeofday:
gettimeofday ( 1665576us / 1000000runs ) = 1.665574us
systenter gettimeofday:
gettimeofday ( 1239215us / 1000000runs ) = 1.239214us
vsyscall gettimeofday:
gettimeofday ( 875876us / 1000000runs ) = 0.875875us
Please let me know if you have any comments or suggestions.
thanks
-john
Release History:
----------------
B1 -> B2:
o No LD_PRELOADing or changes to userspace required!
o removed hard-coded address in linker script
B0 -> B1:
o Cleaned up 4/4 split code, so no additional patch is needed.
o Fixed permissions on fixmapped cyclone page
o Improved alternate_instruction workaround
o Use NTP variables to avoid related time inconsistencies
o minor code cleanups
arch/i386/Kconfig +4 -0
@@ -416,6 +416,10 @@ config HPET_EMULATE_RTC def_bool HPET_TIMER && RTC=y +config VSYSCALL_GTOD + depends on EXPERIMENTAL + bool "VSYSCALL gettimeofday() interface" + config SMP bool "Symmetric multi-processing support" ---help---
arch/i386/kernel/Makefile +1 -0
@@ -31,6 +31,7 @@ obj-$(CONFIG_ACPI_SRAT) += srat.o obj-$(CONFIG_HPET_TIMER) += time_hpet.o obj-$(CONFIG_EFI) += efi.o efi_stub.o +obj-$(CONFIG_VSYSCALL_GTOD) += vsyscall-gtod.o EXTRA_AFLAGS := -traditional
arch/i386/kernel/setup.c +2 -0
@@ -47,6 +47,7 @@#include <asm/sections.h> #include <asm/io_apic.h> #include <asm/ist.h> +#include <asm/vsyscall-gtod.h> #include "setup_arch_pre.h" #include "mach_resources.h"
@@ -1146,6 +1147,7 @@ conswitchp = &dummy_con;#endif #endif +vsyscall_init (); }#include "setup_arch_post.h"
arch/i386/kernel/time.c +3 -0
@@ -393,5 +393,8 @@ cur_timer =select_timer ();printk (KERN_INFO "Using %s for high-res timesource\n" ,cur_timer->name); +/* set vsyscall to use selected time source */ +vsyscall_set_timesource (cur_timer->name); +time_init_hook (); }
arch/i386/kernel/timers/timer.c +4 -0
@@ -2,6 +2,7 @@#include <linux/kernel.h> #include <linux/string.h> #include <asm/timer.h> +#include <asm/vsyscall-gtod.h> #ifdef CONFIG_HPET_TIMER /*
@@ -41,6 +42,9 @@void clock_fallback (void ) { cur_timer = &timer_pit; + +/* set vsyscall to use selected time source */ +vsyscall_set_timesource (cur_timer->name); }/* iterates through the list of timers, returning the first
arch/i386/kernel/timers/timer_cyclone.c +18 -12
@@ -21,18 +21,24 @@extern spinlock_t i8253_lock;/* Number of usecs that the last interrupt was delayed */ -static int delay_at_last_interrupt; +int cyclone_delay_at_last_interrupt; + +/* FIXMAP flag */ +#ifdef CONFIG_VSYSCALL_GTOD +#define PAGE_CYCLONE PAGE_KERNEL_VSYSCALL_NOCACHE +#else +#define PAGE_CYCLONE PAGE_KERNEL_NOCACHE +#endif #define CYCLONE_CBAR_ADDR 0xFEB00CD0#define CYCLONE_PMCC_OFFSET 0x51A0#define CYCLONE_MPMC_OFFSET 0x51D0#define CYCLONE_MPCS_OFFSET 0x51A8 -#define CYCLONE_TIMER_FREQ 100000000#define CYCLONE_TIMER_MASK (((u64)1<<40)-1)/* 40 bit mask */ int use_cyclone =0 ; -static u32 *volatile cyclone_timer;/* Cyclone MPMC0 register */ -static u32 last_cyclone_low; +u32 *volatile cyclone_timer;/* Cyclone MPMC0 register */ +u32 last_cyclone_low;static u32 last_cyclone_high;static unsigned long long monotonic_base;static seqlock_t monotonic_lock =SEQLOCK_UNLOCKED ;
@@ -57,7 +63,7 @@spin_lock (&i8253_lock);read_cyclone_counter (last_cyclone_low,last_cyclone_high); -/* read values for delay_at_last_interrupt */ +/* read values for cyclone_delay_at_last_interrupt */ outb_p (0x00 ,0x43 );/* latch the count ASAP */ count =inb_p (0x40 );/* read the latched count */
@@ -67,7 +73,7 @@/* lost tick compensation */ delta = last_cyclone_low - delta; delta /= (CYCLONE_TIMER_FREQ /1000000 ); - delta += delay_at_last_interrupt; + delta += cyclone_delay_at_last_interrupt; lost = delta/(1000000 /HZ ); delay = delta%(1000000 /HZ );if (lost >=2 )
@@ -78,16 +84,16 @@ monotonic_base += (this_offset - last_offset) &CYCLONE_TIMER_MASK ;write_sequnlock (&monotonic_lock); -/* calculate delay_at_last_interrupt */ +/* calculate cyclone_delay_at_last_interrupt */ count = ((LATCH -1 ) - count) *TICK_SIZE ; - delay_at_last_interrupt = (count +LATCH /2 ) /LATCH ; + cyclone_delay_at_last_interrupt = (count +LATCH /2 ) /LATCH ;/* catch corner case where tick rollover occured * between cyclone and pit reads (as noted when * usec delta is > 90% # of usecs/tick) */ -if (lost &&abs (delay - delay_at_last_interrupt) > (900000 /HZ )) +if (lost &&abs (delay - cyclone_delay_at_last_interrupt) > (900000 /HZ )) jiffies_64++; }
@@ -96,7 +102,7 @@u32 offset;if (!cyclone_timer) -return delay_at_last_interrupt; +return cyclone_delay_at_last_interrupt;/* Read the cyclone timer */ offset = cyclone_timer[0 ];
@@ -109,7 +115,7 @@ offset = offset/(CYCLONE_TIMER_FREQ /1000000 );/* our adjusted time offset in microseconds */ -return delay_at_last_interrupt + offset; +return cyclone_delay_at_last_interrupt + offset; }static unsigned long long monotonic_clock_cyclone (void )
@@ -193,7 +199,7 @@/* map in cyclone_timer */ pageaddr = (base +CYCLONE_MPMC_OFFSET )&PAGE_MASK ; offset = (base +CYCLONE_MPMC_OFFSET )&(~PAGE_MASK ); -set_fixmap_nocache (FIX_CYCLONE_TIMER , pageaddr); +__set_fixmap (FIX_CYCLONE_TIMER , pageaddr,PAGE_CYCLONE ); cyclone_timer = (u32 *)(fix_to_virt (FIX_CYCLONE_TIMER ) + offset);if (!cyclone_timer){printk (KERN_ERR "Summit chipset: Could not find valid MPMC register.\n" );
arch/i386/kernel/timers/timer_tsc.c +10 -10
@@ -33,7 +33,7 @@static int use_tsc;/* Number of usecs that the last interrupt was delayed */ -static int delay_at_last_interrupt; +int tsc_delay_at_last_interrupt;static unsigned long last_tsc_low;/* lsb 32 bits of Time Stamp Counter */ static unsigned long last_tsc_high;/* msb 32 bits of Time Stamp Counter */
@@ -104,7 +104,7 @@ "0" (eax));/* our adjusted time offset in microseconds */ -return delay_at_last_interrupt + edx; +return tsc_delay_at_last_interrupt + edx; }static unsigned long long monotonic_clock_tsc (void )
@@ -223,7 +223,7 @@ "0" (eax)); delta = edx; } - delta += delay_at_last_interrupt; + delta += tsc_delay_at_last_interrupt; lost = delta/(1000000 /HZ ); delay = delta%(1000000 /HZ );if (lost >=2 ) {
@@ -248,15 +248,15 @@ monotonic_base +=cycles_2_ns (this_offset - last_offset);write_sequnlock (&monotonic_lock); -/* calculate delay_at_last_interrupt */ +/* calculate tsc_delay_at_last_interrupt */ count = ((LATCH -1 ) - count) *TICK_SIZE ; - delay_at_last_interrupt = (count +LATCH /2 ) /LATCH ; + tsc_delay_at_last_interrupt = (count +LATCH /2 ) /LATCH ;/* catch corner case where tick rollover occured * between tsc and pit reads (as noted when * usec delta is > 90% # of usecs/tick) */ -if (lost &&abs (delay - delay_at_last_interrupt) > (900000 /HZ )) +if (lost &&abs (delay - tsc_delay_at_last_interrupt) > (900000 /HZ )) jiffies_64++; }
@@ -308,7 +308,7 @@ monotonic_base +=cycles_2_ns (this_offset - last_offset);write_sequnlock (&monotonic_lock); -/* calculate delay_at_last_interrupt */ +/* calculate tsc_delay_at_last_interrupt */ /* * Time offset = (hpet delta) * ( usecs per HPET clock ) * = (hpet delta) * ( usecs per tick / HPET clocks per tick)
@@ -316,9 +316,9 @@* Where, * hpet_usec_quotient = (2^32 * usecs per tick)/HPET clocks per tick */ - delay_at_last_interrupt = hpet_current - offset; -ASM_MUL64_REG (temp, delay_at_last_interrupt, - hpet_usec_quotient, delay_at_last_interrupt); + tsc_delay_at_last_interrupt = hpet_current - offset; +ASM_MUL64_REG (temp, tsc_delay_at_last_interrupt, + hpet_usec_quotient, tsc_delay_at_last_interrupt); }#endif
arch/i386/kernel/vmlinux.lds.S +76 -2
93 lines not on this page · raw message
arch/i386/kernel/vsyscall-gtod.c new file +275 -0
275 lines not on this page · raw message
arch/i386/kernel/vsyscall-sysenter.S +21 -0
@@ -1,3 +1,6 @@ +#include <linux/config.h> +#include <asm/unistd.h> +#include <asm/vsyscall-gtod.h> /* * Code for the vsyscall page. This version uses the sysenter instruction. */
@@ -6,6 +9,8 @@ .globl __kernel_vsyscall .type __kernel_vsyscall,@function __kernel_vsyscall: + cmp $__NR_gettimeofday, %eax + je .Lvgettimeofday .LSTART_vsyscall: push %ecx .Lpush_ecx:
@@ -31,6 +36,22 @@ pop %ecx .Lpop_ecx: ret + +/* vsyscall-gettimeofday code */ +.Lvgettimeofday: +#ifdef CONFIG_VSYSCALL_GTOD + pushl %edx + pushl %ecx + pushl %ebx + call VSYSCALL_GTOD_START + popl %ebx + popl %ecx + popl %edx + ret +#else + jmp .LSTART_vsyscall +#endif + .LEND_vsyscall: .size __kernel_vsyscall,.-.LSTART_vsyscall .previous
include/asm-i386/fixmap.h +12 -0
@@ -18,6 +18,7 @@#include <asm/acpi.h> #include <asm/apicdef.h> #include <asm/page.h> +#include <asm/vsyscall-gtod.h> #ifdef CONFIG_HIGHMEM #include <linux/threads.h> #include <asm/kmap_types.h>
@@ -44,6 +45,17 @@enum fixed_addresses {FIX_HOLE ,FIX_VSYSCALL , +#ifdef CONFIG_VSYSCALL_GTOD +#ifndef CONFIG_X86_4G +FIX_VSYSCALL_GTOD_PAD , +#endif /* !CONFIG_X86_4G */ +FIX_VSYSCALL_GTOD_LAST_PAGE , +FIX_VSYSCALL_GTOD_FIRST_PAGE =FIX_VSYSCALL_GTOD_LAST_PAGE + +VSYSCALL_GTOD_NUMPAGES -1 , +#ifdef CONFIG_X86_4G +FIX_VSYSCALL_GTOD_4GALIGN , +#endif /* CONFIG_X86_4G */ +#endif /* CONFIG_VSYSCALL_GTOD */ #ifdef CONFIG_X86_LOCAL_APICFIX_APIC_BASE ,/* local (CPU) APIC) -- required for SMP or not */ #endif
include/asm-i386/pgtable.h +5 -1
@@ -137,11 +137,15 @@#define __PAGE_KERNEL_RO (__PAGE_KERNEL & ~_PAGE_RW)#define __PAGE_KERNEL_NOCACHE (__PAGE_KERNEL | _PAGE_PCD)#define __PAGE_KERNEL_LARGE (__PAGE_KERNEL | _PAGE_PSE) - +#define __PAGE_KERNEL_VSYSCALL \ + (_PAGE_PRESENT | _PAGE_USER | _PAGE_ACCESSED) +#define PAGE_KERNEL __pgprot(__PAGE_KERNEL)#define PAGE_KERNEL_RO __pgprot(__PAGE_KERNEL_RO)#define PAGE_KERNEL_NOCACHE __pgprot(__PAGE_KERNEL_NOCACHE)#define PAGE_KERNEL_LARGE __pgprot(__PAGE_KERNEL_LARGE) +#define PAGE_KERNEL_VSYSCALL __pgprot(__PAGE_KERNEL_VSYSCALL) +#define PAGE_KERNEL_VSYSCALL_NOCACHE __pgprot(__PAGE_KERNEL_VSYSCALL|(__PAGE_KERNEL_RO | _PAGE_PCD))/* * The i386 can't do page protection for execute, and considers that
include/asm-i386/timer.h +1 -0
@@ -20,6 +20,7 @@ };#define TICK_SIZE (tick_nsec / 1000) +#define CYCLONE_TIMER_FREQ 100000000extern struct timer_opts *select_timer (void );extern void clock_fallback (void );
include/asm-i386/vsyscall-gtod.h new file +68 -0
68 lines not on this page · raw message
arch/i386/kernel/vsyscall-sysenter.S line 14
Quoted 7 lines · john stultz
arch/i386/kernel/vsyscall-sysenter.S +2
-0@@ -6,6 +9,8 @@ .globl __kernel_vsyscall .type __kernel_vsyscall,@function __kernel_vsyscall: + cmp $__NR_gettimeofday, %eax + je .Lvgettimeofday .LSTART_vsyscall:
this is the sort of slowdown that could be avoided with the fixed address.
with regards to Ulrich's security related comments, this won't make any difference compared to the fixed address version either, since the vsyscall page is still at a fixed address in the fixmap area, and nevertheless regardless the vgettimeofday api, the sysenter instruction is always placed at a fixed address in the address space for the sysenter vsyscall support, so at the light of the i386 current status, those comments w.r.t to security make even less sense.
This has the feature that it doesn't need the LD_PRELOAD and it's sort of backwards compatible, but it is equivalent in terms of security and as a matter of fact it's less efficient and the worst part is that not only it makes gettimeofday slower, it makes _all_ the syscall slower (slowing down vgettimeofday wouldn't matter since we're improving it huge anyways).
I prefer the previous version, glibc has to be changed at the same time with the kernel anyways to use the sysenter. I think it's not nice to speedup gettimeofday by slowing down all other syscalls, when we can implement it running all syscalls at full speed with the optimal API of x86-64.
Quoted 3 lines · Andrea Arcangeli
with regards to Ulrich's security related comments, this won't make any difference compared to the fixed address version either, since the vsyscall page is still at a fixed address in the fixmap area,
Gee, you don't want to understand it.
Even if the official kernel's handling of the vdso puts it at the same address all the time this does not mean this can be engraved in stone. It must be possible to move the page. And I expect this will be the case in our kernels.
It is completely unacceptable to use fixed addresses or require the libc to be recompiled for a new address. At the highest security level the vdso address should vary from program run to program run which means there is no way to change the libc.
Signature · 1 line
➧ Ulrich Drepper ➧ Red Hat, Inc. ➧ 444 Castro St ➧ Mountain View, CA ❖
Quoted 10 lines · Ulrich Drepper
Quoted 3 lines · Andrea Arcangeli
with regards to Ulrich's security related comments, this won't make any difference compared to the fixed address version either, since the vsyscall page is still at a fixed address in the fixmap area,
Gee, you don't want to understand it.
Even if the official kernel's handling of the vdso puts it at the same address all the time this does not mean this can be engraved in stone.
i386 in 2.6.2 has the very same security issues. Fixed address for all binary kernel shipped for the sysenter or int 0x80 instructions. go complain who wrote the i386 code.
Quoted 2 lines
It must be possible to move the page. And I expect this will be the case in our kernels.
what are "yours" kernels? You mean mainline or what? I'm saying it's perfectly fine to relocate the vsyscall page on demand with an additional syscall but this will have an overhead, like relocating the .text executable as well will have an overhead, and before you can care about relocating the vsyscall address, you should relocate the .text of the executable as Andi noted.
Quoted 4 lines
It is completely unacceptable to use fixed addresses or require the libc to be recompiled for a new address. At the highest security level the vdso address should vary from program run to program run which means there is no way to change the libc.
This is fine, the new syscall I'm advocating will simply relocate the vsyscall page with a new pte and a tlb flush during every context switch, write it for i386 now since 2.6.2 has those int 0x80 and syscall instructions at fixed address too.
Quoted 12 lines · Andrea Arcangeli
arch/i386/kernel/vsyscall-sysenter.S line 14
Quoted 7 lines · john stultz
arch/i386/kernel/vsyscall-sysenter.S +2
-0@@ -6,6 +9,8 @@ .globl __kernel_vsyscall .type __kernel_vsyscall,@function __kernel_vsyscall: + cmp $__NR_gettimeofday, %eax + je .Lvgettimeofday .LSTART_vsyscall:this is the sort of slowdown that could be avoided with the fixed address.
Quoted 2 lines
... the worst part is that not
only it makes gettimeofday slower, it makes _all_ the syscall slower
Andrea, please keep this argument separate from the argument about varying vdso addresses.
The Glibc method of calling syscalls is already sub-optimal: the two instructions in the above patch are nothing, time wise, compared with the segment-prefixed indirect jump in Glibc and the branches which check for and toggle POSIX asynchronous cancellations.
Other libcs might be more direct about the syscalls, and then it is nice to imagine the optimal vdso for them. But other libcs don't have to enter the vdso at all, they can just copy the initial instructions of the vdso and optimise away the path not taken (checking at run time whether they match the current kernel's vdso of course). Thus an optimal vdso isn't strictly required for those of us who are into hard core binary optimisation.
-- Jamie