Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp522470ybe; Fri, 6 Sep 2019 03:12:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqzwrDTaAlQxcxGK3OoyNboz8GLYf1kDvzKVHiZ/m2NminUmf4Zv8FK2FRXfvpFAzYBulwSe X-Received: by 2002:aa7:9341:: with SMTP id 1mr9932227pfn.202.1567764728999; Fri, 06 Sep 2019 03:12:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567764728; cv=none; d=google.com; s=arc-20160816; b=afEDjVdIYpcWDXUjRky84Q+VDW96fzXF5LTDiKOCcY8hsFsfmVOptkxA/IL7z6tXH1 NB04RykzYv1TfkBRNwiotfIoiG3CPLFoEvZY0UxL4L7QWGNbu6rdZ3ypzxKmCBRqMFZF GfXBHt7N5zozOyhnyYMKKGiSOg7oYrVgb1Lsb8H1XKR0BQ4Y/Cdl/giPs6WjI0MKVSj4 xOf3pLZyhbv5sJIv01BDTMThumht3AAAtOcW8mxDvW1eWCrNpeLPUEOkCknifOhbCDUj 57RgBTGLlThCNERQ45Ajhlzlz9GkcWADRSVdDsp6IJfPUmX0INwNx8ssJVUyJQXY/EnF f6yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=TGB4uJiUeL5Fo2JfDzEml4xHl1KEyJtx9FM84cwKunw=; b=CTCVyU+XTU+oDMzkOQmw6RIpaWUGj7VY+9dMb+va+gNtJ/19Jn7U9JqTgpyqXAoLjq VCoEYOofNJagBbqABevZwaN65CSPSRR020wmrmyPQUy1IFxmhUONgHswVWYxUFXtClUs pxqv0rv9+cmpAnwTLBXLnXcypfONo9c0C4XwDOiBm/4Fw4GyxYjapHfktYfxQuqB9f4G OLVwVXz2fTo+XLadWEgytlTYTq3eIsHH1wDuhEhnWgrodTzx0JYFsy+xXSRab6i25EAD /T4xXQk9KS3C7gDQ3L+yy5AhYLi53PCqZVH2WRxWFF33W2VeNFV9IIu0s9/wGk8dexZu Z5eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=dstsaqxC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 37si4167387pgz.39.2019.09.06.03.11.51; Fri, 06 Sep 2019 03:12:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=dstsaqxC; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391925AbfIFBaU (ORCPT + 99 others); Thu, 5 Sep 2019 21:30:20 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:37330 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391913AbfIFBaS (ORCPT ); Thu, 5 Sep 2019 21:30:18 -0400 Received: by mail-pf1-f196.google.com with SMTP id y5so427057pfo.4; Thu, 05 Sep 2019 18:30:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TGB4uJiUeL5Fo2JfDzEml4xHl1KEyJtx9FM84cwKunw=; b=dstsaqxCZRcfN6gGH6ctFsOO7xhq5phOxmqAm/OB1NMyIBTc0QTP8lK8u29elsTd6T JnzSPMr3Uu0gU6diT7nvgY1fbXUwy88aPlQTtNVWDIWJuwMYlK7YaXNWPQxKyK1b+xj2 fQmL2Yv9U/7++ML6zUAtFHJqTjnY5v1rLmcNTHMWonaTkoTdyIxzbxDHZctYHNpq2suy Ftj36gQRIaZX2cxdTi/d4MwgOvDroDzu+6jCOd5ycHHFfR9jaGcI+8ypcTMOELJfSIC0 iCUDVKSUY6p4tdxQ9LL/DieyvZ0gERKPGv8yD3tcy5v57qPfk1niukCxM+khJfJdPJn7 X/kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TGB4uJiUeL5Fo2JfDzEml4xHl1KEyJtx9FM84cwKunw=; b=OU6F5xjKVn0zA2GEpbiOayp6Ear6bBS4m1bHu/vp7BVmb/DDNvgzorfiYgvPaeDGkN AoQZYgHcbUmjbQRFS6Wow2esStxS3n/T5Z/qxmJ+8fExosOUFMtZsPEgczG0YDUEVvwI 3EFkIHxZaXOH26bqOKjPtliN9hAxxUT4SkfJruNN5V3Sh6tJdRqOw2fIz9aAmvno2ssM 2N9g7WpyRhtULQgWyWS46llWLsvP3n4vi7jv4qsgRU5svt0ZBzNtkKtWlyM34752J7if YViSP/nXGFVM2Xesmabpe2gjQjoVL7hdSGMUIksSGHZSSbx4gg4HHsoMZ/xHCGl6z6XN veYg== X-Gm-Message-State: APjAAAUspCAIMOsiKT0puvLtYae0LbX/aSYRX/g7ZC0/Hq45wvaJmnnj KqtO5bQuqOOQTFwswdM9Q4mFWJ53 X-Received: by 2002:a17:90a:b118:: with SMTP id z24mr7107999pjq.79.1567733417905; Thu, 05 Sep 2019 18:30:17 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id g11sm3332294pgu.11.2019.09.05.18.30.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Sep 2019 18:30:17 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH RESEND v3 2/5] KVM: LAPIC: Periodically revaluate to get conservative lapic_timer_advance_ns Date: Fri, 6 Sep 2019 09:30:01 +0800 Message-Id: <1567733404-7759-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1567733404-7759-1-git-send-email-wanpengli@tencent.com> References: <1567733404-7759-1-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li Even if for realtime CPUs, cache line bounces, frequency scaling, presence of higher-priority RT tasks, etc can still cause different response. These interferences should be considered and periodically revaluate whether or not the lapic_timer_advance_ns value is the best, do nothing if it is, otherwise recaluate again. Set lapic_timer_advance_ns to the minimal conservative value from all the estimated values. Testing on Skylake server, cat vcpu*/lapic_timer_advance_ns, before patch: 1628 4161 4321 3236 ... Testing on Skylake server, cat vcpu*/lapic_timer_advance_ns, after patch: 1553 1499 1509 1489 ... Testing on Haswell desktop, cat vcpu*/lapic_timer_advance_ns, before patch: 4617 3641 4102 4577 ... Testing on Haswell desktop, cat vcpu*/lapic_timer_advance_ns, after patch: 2775 2892 2764 2775 ... Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/kvm/lapic.c | 37 ++++++++++++++++++++++++++++++------- arch/x86/kvm/lapic.h | 2 ++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 2f4a48a..12ade70 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -70,6 +70,7 @@ /* step-by-step approximation to mitigate fluctuation */ #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8 #define LAPIC_TIMER_ADVANCE_FILTER 10000 +#define LAPIC_TIMER_ADVANCE_RECALC_PERIOD (600 * HZ) static inline int apic_test_vector(int vec, void *bitmap) { @@ -1484,13 +1485,24 @@ static inline void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles) static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu, s64 advance_expire_delta) { - struct kvm_lapic *apic = vcpu->arch.apic; - u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns, ns; + struct kvm_timer *ktimer = &vcpu->arch.apic->lapic_timer; + u32 timer_advance_ns = ktimer->timer_advance_ns, ns; if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_FILTER) /* filter out drastic fluctuations */ return; + /* periodic revaluate */ + if (unlikely(ktimer->timer_advance_adjust_done)) { + ktimer->recalc_timer_advance_ns = jiffies + + LAPIC_TIMER_ADVANCE_RECALC_PERIOD; + if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_DONE) { + timer_advance_ns = LAPIC_TIMER_ADVANCE_ADJUST_INIT; + ktimer->timer_advance_adjust_done = false; + } else + return; + } + /* too early */ if (advance_expire_delta < 0) { ns = -advance_expire_delta * 1000000ULL; @@ -1503,17 +1515,24 @@ static inline void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu, timer_advance_ns += ns; } - timer_advance_ns = (apic->lapic_timer.timer_advance_ns * + timer_advance_ns = (ktimer->timer_advance_ns * (LAPIC_TIMER_ADVANCE_ADJUST_STEP - 1) + advance_expire_delta) / LAPIC_TIMER_ADVANCE_ADJUST_STEP; if (abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_DONE) - apic->lapic_timer.timer_advance_adjust_done = true; + ktimer->timer_advance_adjust_done = true; if (unlikely(timer_advance_ns > 5000)) { timer_advance_ns = LAPIC_TIMER_ADVANCE_ADJUST_INIT; - apic->lapic_timer.timer_advance_adjust_done = false; + ktimer->timer_advance_adjust_done = false; + } + + ktimer->timer_advance_ns = timer_advance_ns; + + if (ktimer->timer_advance_adjust_done) { + if (ktimer->min_timer_advance_ns > timer_advance_ns) + ktimer->min_timer_advance_ns = timer_advance_ns; + ktimer->timer_advance_ns = ktimer->min_timer_advance_ns; } - apic->lapic_timer.timer_advance_ns = timer_advance_ns; } static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) @@ -1532,7 +1551,8 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) if (guest_tsc < tsc_deadline) __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc); - if (unlikely(!apic->lapic_timer.timer_advance_adjust_done)) + if (unlikely(!apic->lapic_timer.timer_advance_adjust_done) || + time_before(apic->lapic_timer.recalc_timer_advance_ns, jiffies)) adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta); } @@ -2310,9 +2330,12 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) if (timer_advance_ns == -1) { apic->lapic_timer.timer_advance_ns = LAPIC_TIMER_ADVANCE_ADJUST_INIT; apic->lapic_timer.timer_advance_adjust_done = false; + apic->lapic_timer.recalc_timer_advance_ns = jiffies; + apic->lapic_timer.min_timer_advance_ns = UINT_MAX; } else { apic->lapic_timer.timer_advance_ns = timer_advance_ns; apic->lapic_timer.timer_advance_adjust_done = true; + apic->lapic_timer.recalc_timer_advance_ns = MAX_JIFFY_OFFSET; } diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index 50053d2..56a05eb 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -31,6 +31,8 @@ struct kvm_timer { u32 timer_mode_mask; u64 tscdeadline; u64 expired_tscdeadline; + unsigned long recalc_timer_advance_ns; + u32 min_timer_advance_ns; u32 timer_advance_ns; s64 advance_expire_delta; atomic_t pending; /* accumulated triggered timers */ -- 2.7.4