lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <CANRm+Cx1AKXnvqPxvY+vgKULS+hq=JTaNoSL7qnNoQ90NcAWTQ@mail.gmail.com>
Date:   Tue, 9 Oct 2018 10:08:35 +0800
From:   Wanpeng Li <kernellwp@...il.com>
To:     Liran Alon <liran.alon@...cle.com>
Cc:     LKML <linux-kernel@...r.kernel.org>, kvm <kvm@...r.kernel.org>,
        Paolo Bonzini <pbonzini@...hat.com>,
        Radim Krcmar <rkrcmar@...hat.com>
Subject: Re: [PATCH] KVM: LAPIC: Tune lapic_timer_advance_ns automatically

On Mon, 8 Oct 2018 at 20:04, Liran Alon <liran.alon@...cle.com> wrote:
>
>
>
> > On 8 Oct 2018, at 13:59, Wanpeng Li <kernellwp@...il.com> wrote:
> >
> > On Mon, 8 Oct 2018 at 05:02, Liran Alon <liran.alon@...cle.com> wrote:
> >>
> >>
> >>
> >>> On 28 Sep 2018, at 9:12, Wanpeng Li <kernellwp@...il.com> wrote:
> >>>
> >>> From: Wanpeng Li <wanpengli@...cent.com>
> >>>
> >>> In cloud environment, lapic_timer_advance_ns is needed to be tuned for every CPU
> >>> generations, and every host kernel versions(the kvm-unit-tests/tscdeadline_latency.flat
> >>> is 5700 cycles for upstream kernel and 9600 cycles for our 3.10 product kernel,
> >>> both preemption_timer=N, Skylake server).
> >>>
> >>> This patch adds the capability to automatically tune lapic_timer_advance_ns
> >>> step by step, the initial value is 1000ns as d0659d946be05 (KVM: x86: add
> >>> option to advance tscdeadline hrtimer expiration) recommended, it will be
> >>> reduced when it is too early, and increased when it is too late. The guest_tsc
> >>> and tsc_deadline are hard to equal, so we assume we are done when the delta
> >>> is within a small scope e.g. 100 cycles. This patch reduces latency
> >>> (kvm-unit-tests/tscdeadline_latency, busy waits, preemption_timer enabled)
> >>> from ~2600 cyles to ~1200 cyles on our Skylake server.
> >>>
> >>> Cc: Paolo Bonzini <pbonzini@...hat.com>
> >>> Cc: Radim Krčmář <rkrcmar@...hat.com>
> >>> Signed-off-by: Wanpeng Li <wanpengli@...cent.com>
> >>> ---
> >>> arch/x86/kvm/lapic.c | 7 +++++++
> >>> arch/x86/kvm/x86.c   | 2 +-
> >>> 2 files changed, 8 insertions(+), 1 deletion(-)
> >>>
> >>> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> >>> index fbb0e6d..b756f12 100644
> >>> --- a/arch/x86/kvm/lapic.c
> >>> +++ b/arch/x86/kvm/lapic.c
> >>> @@ -70,6 +70,8 @@
> >>> #define APIC_BROADCAST                        0xFF
> >>> #define X2APIC_BROADCAST              0xFFFFFFFFul
> >>>
> >>> +static bool __read_mostly lapic_timer_advance_adjust_done = false;
> >>> +
> >>> static inline int apic_test_vector(int vec, void *bitmap)
> >>> {
> >>>      return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
> >>> @@ -1492,6 +1494,11 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
> >>>      if (guest_tsc < tsc_deadline)
> >>>              __delay(min(tsc_deadline - guest_tsc,
> >>>                      nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
> >>> +     if (!lapic_timer_advance_adjust_done) {
> >>> +             lapic_timer_advance_ns += (s64)(guest_tsc - tsc_deadline) / 8;
> >>
> >> I don’t understand how this “/ 8” converts between guest TSC units to host nanoseconds.
> >
> > Oh, I miss it. In addition, /8 here I mean adjust
> > lapic_timer_advance_ns step by step. I can observe big fluctuated
>
> If that’s the case, I would also put the “8” as a #define to make it more clear of it’s purpose.
>
> > value between early and late when running real guest os like linux
> > instead of kvm-unit-tests. After more testing, I saw
> > lapic_timer_advance_ns can be overflow since the delta between
> > guest_tsc and tsc_deadline is too huge.
> >
> >>
> >> I think that instead you should do something like:
> >> s64 ns = (s64)(guest_tsc - tsc_deadline) * 1000000ULL;
> >> do_div(ns, vcpu->arch.virtual_tsc_khz);
> >> lapic_timer_advance_ns += ns;
> >>
> >>> +             if (abs(guest_tsc - tsc_deadline) < 100)
> >>
> >> I would put this “100” hard-coded value as some “#define” to make code more clear.
> >
> > How about something like below:
> >
> > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> > index fbb0e6d..354eb13c 100644
> > --- a/arch/x86/kvm/lapic.c
> > +++ b/arch/x86/kvm/lapic.c
> > @@ -70,6 +70,9 @@
> > #define APIC_BROADCAST            0xFF
> > #define X2APIC_BROADCAST        0xFFFFFFFFul
> >
> > +static bool __read_mostly lapic_timer_advance_adjust_done = false;
> > +#define LAPIC_TIMER_ADVANCE_ADJUST_DONE 100
> > +
> > static inline int apic_test_vector(int vec, void *bitmap)
> > {
> >     return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
> > @@ -1472,7 +1475,7 @@ static bool lapic_timer_int_injected(struct
> > kvm_vcpu *vcpu)
> > void wait_lapic_expire(struct kvm_vcpu *vcpu)
> > {
> >     struct kvm_lapic *apic = vcpu->arch.apic;
> > -    u64 guest_tsc, tsc_deadline;
> > +    u64 guest_tsc, tsc_deadline, ns;
> >
> >     if (!lapic_in_kernel(vcpu))
> >         return;
> > @@ -1492,6 +1495,19 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
> >     if (guest_tsc < tsc_deadline)
> >         __delay(min(tsc_deadline - guest_tsc,
> >             nsec_to_cycles(vcpu, lapic_timer_advance_ns)));
> > +    if (!lapic_timer_advance_adjust_done) {
> > +        if (guest_tsc < tsc_deadline) {
> > +            ns = (tsc_deadline - guest_tsc) * 1000000ULL;
> > +            do_div(ns, vcpu->arch.virtual_tsc_khz);
> > +            lapic_timer_advance_ns -= min((unsigned int)ns,
> > lapic_timer_advance_ns / 8);
> > +        } else {
> > +            ns = (guest_tsc - tsc_deadline) * 1000000ULL;
> > +            do_div(ns, vcpu->arch.virtual_tsc_khz);
> > +            lapic_timer_advance_ns += min((unsigned int)ns,
> > lapic_timer_advance_ns / 8);
> > +        }
> > +        if (ns < LAPIC_TIMER_ADVANCE_ADJUST_DONE)
>
> Didn’t you meant to compare here that abs(guest_tsc - tsc_deadline) < LAPIC_TIMER_ADVANCE_ADJUST_DONE?
> This is also a good example on why I would also rename LAPIC_TIMER_ADVANCE_ADJUST_DONE to something
> which indicates it represents a number in guest TSC units.

Done in v2.

Regards,
Wanpeng Li

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ