From patchwork Fri Sep 6 01:30:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 11134177 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F3AEA1398 for ; Fri, 6 Sep 2019 01:30:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BCE9D2082C for ; Fri, 6 Sep 2019 01:30:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XklUKNyK" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391901AbfIFBaQ (ORCPT ); Thu, 5 Sep 2019 21:30:16 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:46396 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732899AbfIFBaQ (ORCPT ); Thu, 5 Sep 2019 21:30:16 -0400 Received: by mail-pg1-f194.google.com with SMTP id m3so2469674pgv.13; Thu, 05 Sep 2019 18:30:15 -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:mime-version :content-transfer-encoding; bh=7A2filp88gYNW30KqtsTddaPW1vJgWmE/N1oJgsUja0=; b=XklUKNyKTyc8Hs/8yMz9srJeGYceQ07QmpMhvNcGHY0j4mDq7EZxkMStITo3oi2SSL TEE1SoOfl8PMBs2woEl3CWqFQjanlC25C4Lpvl0M5X5f45VcFoCyoenBiKcRZ0WsdGWj Mxi9Ycgn4YInJoKs6WWbqU3uahUSK2fzZ7JPuwuHtLhALEqujI/GcfNlg+dLByduWDJC 9u8EmoB1gmmfutKEDWhph/H2xjA1VKKjaKtAss/qGopsF8ahpeiNB6nlzlCBidRaj3gg mKJlz/3t59Lh0tNrV6Y+ncQK8hZmtXqzQ2VrppvCRM/gZhxMaSgbym1jnKmS1QscDfQs TPQw== 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:mime-version :content-transfer-encoding; bh=7A2filp88gYNW30KqtsTddaPW1vJgWmE/N1oJgsUja0=; b=Es6MjbV/6v6+Nxr7uBcXzKAVjqGj0aDV046pg49m/lVDRry4WJZLn9TR62mX2qglfF wCfDHjAVbogJEcnSDkPw2KGdMBzIWl4cVopuZ8xkOtxmYVqB6H2W1qUpLA24InP2LLK/ I1IYOc9UU7AO6fuFzZNS0lkUKA+29PYLOabls3gjj4+6B0K33oJvaH0/Wnq8hndRwE0H ek0QTm4oMRANLKtoM1JO+SvIFNzlO4k5lh/pEnS1FjruU3WU7+lIw/66P5V507Qgmpnc Sro9uFFUAgVrp9tDKknjsEIWbFR8QkerFkwA+KfhtwWCOIsqwUKvMpT8mUY9PTp3nChk pDKA== X-Gm-Message-State: APjAAAWp5UdGCnNl9oqAtYx+ulom1tQhkKX4mCle7tfS4uyRAmmWrn3m /mNltoW49PauVFyCIkZNwzbgLlUi X-Google-Smtp-Source: APXvYqxFnG3Y8aMXP4FrpIDRhqZF/OXp0uMbOHjN2dV0BlzC+z2Y978K+5JoexAfJO0pNIphklpvPA== X-Received: by 2002:a17:90a:983:: with SMTP id 3mr7227355pjo.57.1567733415131; Thu, 05 Sep 2019 18:30:15 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id g11sm3332294pgu.11.2019.09.05.18.30.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Sep 2019 18:30:14 -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?b?UmFkaW0gS3LEjW3DocWZ?= , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH RESEND v3 1/5] KVM: LAPIC: Tune lapic_timer_advance_ns smoothly Date: Fri, 6 Sep 2019 09:30:00 +0800 Message-Id: <1567733404-7759-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Using a moving average based on per-vCPU lapic_timer_advance_ns to tune smoothly, filter out drastic fluctuation which prevents this before, let's assume it is 10000 cycles. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/kvm/lapic.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index e904ff0..2f4a48a 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -69,6 +69,7 @@ #define LAPIC_TIMER_ADVANCE_ADJUST_INIT 1000 /* step-by-step approximation to mitigate fluctuation */ #define LAPIC_TIMER_ADVANCE_ADJUST_STEP 8 +#define LAPIC_TIMER_ADVANCE_FILTER 10000 static inline int apic_test_vector(int vec, void *bitmap) { @@ -1484,23 +1485,28 @@ 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; - u64 ns; + u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns, ns; + + if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_FILTER) + /* filter out drastic fluctuations */ + return; /* too early */ if (advance_expire_delta < 0) { ns = -advance_expire_delta * 1000000ULL; do_div(ns, vcpu->arch.virtual_tsc_khz); - timer_advance_ns -= min((u32)ns, - timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP); + timer_advance_ns -= ns; } else { /* too late */ ns = advance_expire_delta * 1000000ULL; do_div(ns, vcpu->arch.virtual_tsc_khz); - timer_advance_ns += min((u32)ns, - timer_advance_ns / LAPIC_TIMER_ADVANCE_ADJUST_STEP); + timer_advance_ns += ns; } + timer_advance_ns = (apic->lapic_timer.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; if (unlikely(timer_advance_ns > 5000)) { From patchwork Fri Sep 6 01:30:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 11134169 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 184DD112B for ; Fri, 6 Sep 2019 01:30:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D13C520B1F for ; Fri, 6 Sep 2019 01:30:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dstsaqxC" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391922AbfIFBaU (ORCPT ); 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-Google-Smtp-Source: APXvYqwkT90touvJ1jKr02oZ3a3Vt6s2FTsqRJSY6OLRK0rLVNyYttyEO3FHF6pjnBUTXKcMWLsP6g== 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?b?UmFkaW0gS3LEjW3DocWZ?= , 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 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@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 */ From patchwork Fri Sep 6 01:30:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 11134171 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 85375112B for ; Fri, 6 Sep 2019 01:30:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 532A52082C for ; Fri, 6 Sep 2019 01:30:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Zs5mXO3t" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403977AbfIFBaZ (ORCPT ); Thu, 5 Sep 2019 21:30:25 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:39308 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732899AbfIFBaV (ORCPT ); Thu, 5 Sep 2019 21:30:21 -0400 Received: by mail-pf1-f195.google.com with SMTP id s12so3170929pfe.6; Thu, 05 Sep 2019 18:30:21 -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=PasM8GCX7mEGAVRsf1zB0IrqtOMIBNlDj4wL+XfNBQQ=; b=Zs5mXO3t1tXqEJq3xTtO8EL2rTiUMUX4xV/WUnYDObiV+cMcP+jE5nDcCcB3EWyXed Tn+EwRV8aavfIHIqTUTrfG7D8cD4UU10FUoUfZ5dY5Nx9syOyQOgLzZaaM5zAZ98YoEB 3gdboRk0UeB6FHt/y6CVBt+cdLF/8JyJVgrP8DOH6cfSdEIYUnhfmDJttCqGrIZvOC80 L8bbrsvs2JB/ULTgiHwTRcf3vDrVTlmjpvRLl+55ft45V000O5tZvk4UlEgWB08zEwlW 3RqJy7Tl4THVmszYLc7hDEYZuZI5PgFDxc2XQ/Hv5mfCzYA6CgvYUhQz/s+rkjtAIRm/ X1Yg== 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=PasM8GCX7mEGAVRsf1zB0IrqtOMIBNlDj4wL+XfNBQQ=; b=RGKJEMSTopNS7lVDgl3s9N/vURrdaw2rPXlIl30qDstIBFHZZY9eGPy+17n+QJDdy+ S3xLwHjnIeehtBVvNRNx6PPzVFMj73DHV5qKcKYHCtzGEemRCgPwUTfVZTzE0fZfW1ev OBhTEfsF1bX66vm0RWzYLPAvQiyR6iF0bdDBba/Mo8eEShMgtFfk4jF8KOQImspqV6hx 70CZgqzti3xAl1/6/SmsATbA7sPUi9uop4tHFlczESyfkm25LW/i9rqPNUnlOgexHjZs DsQ1vdA33nk+FGykKaxcFQtW/L2An7I+usHbiCTvzIAFolGhETjX58gNiSYeAPIjqR9Y ukfQ== X-Gm-Message-State: APjAAAWhxi+zQDhFaU+DbuHfnzIGcKnORYvSqHhrKimpdpSz2hlEPsGB g7D6sfUmQLRRDc37S1SxYQPEVTeD X-Google-Smtp-Source: APXvYqzzP253JQWcY/nY6IUQxjGomgk/V63k7jg1IqIl/TjGVPhFJPQAf+Ly7VYSe4JWYElKoJBMBA== X-Received: by 2002:a17:90a:c38d:: with SMTP id h13mr7332714pjt.115.1567733420667; Thu, 05 Sep 2019 18:30:20 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id g11sm3332294pgu.11.2019.09.05.18.30.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Sep 2019 18:30:20 -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?b?UmFkaW0gS3LEjW3DocWZ?= , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH RESEND 3/5] KVM: LAPIC: Micro optimize IPI latency Date: Fri, 6 Sep 2019 09:30:02 +0800 Message-Id: <1567733404-7759-3-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 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li This patch optimizes the virtual IPI emulation sequence: write ICR2 write ICR2 write ICR read ICR2 read ICR ==> send virtual IPI read ICR2 write ICR send virtual IPI It can reduce kvm-unit-tests/vmexit.flat IPI testing latency(from sender send IPI to sender receive the ACK) from 3319 cycles to 3203 cycles on SKylake server. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/kvm/lapic.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 12ade70..34fd299 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1200,10 +1200,8 @@ void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector) } EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated); -static void apic_send_ipi(struct kvm_lapic *apic) +static void apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high) { - u32 icr_low = kvm_lapic_get_reg(apic, APIC_ICR); - u32 icr_high = kvm_lapic_get_reg(apic, APIC_ICR2); struct kvm_lapic_irq irq; irq.vector = icr_low & APIC_VECTOR_MASK; @@ -1940,8 +1938,9 @@ int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val) } case APIC_ICR: /* No delay here, so we always clear the pending bit */ - kvm_lapic_set_reg(apic, APIC_ICR, val & ~(1 << 12)); - apic_send_ipi(apic); + val &= ~(1 << 12); + apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2)); + kvm_lapic_set_reg(apic, APIC_ICR, val); break; case APIC_ICR2: From patchwork Fri Sep 6 01:30:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 11134175 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EDCD2112B for ; Fri, 6 Sep 2019 01:30:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B9222212CC for ; Fri, 6 Sep 2019 01:30:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="puPqJNuU" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391928AbfIFBaY (ORCPT ); Thu, 5 Sep 2019 21:30:24 -0400 Received: from mail-pg1-f194.google.com ([209.85.215.194]:37628 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391915AbfIFBaY (ORCPT ); Thu, 5 Sep 2019 21:30:24 -0400 Received: by mail-pg1-f194.google.com with SMTP id d1so2496983pgp.4; Thu, 05 Sep 2019 18:30:23 -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=AziVDiNsJfWSoLEyeTjoxxBYGSeB6j2LvcpA2gkjX9Y=; b=puPqJNuU2re3ZarBofADdbCQPRA4Nexdfs0IazA91Nh/pAC0CdSSGIy/aM/XrewZaD Zs49HgtAeUSmbE644alwC4NcOKIgUZ80JrsVWPB9tFfOzHHuoCNx24trIsZFgvF6v8mo kZ+ryhZ4TAoPVq5N7Yd3XzQBrj68Smus+JiRND9WzsWRyVIClfuciOGRtikbProl+7GL mPEGUPHk+WwWio6t5YxhE6uhlWKB4dcvXWdJnyHjpyKtNnfrWVsQ4rKrIUNpFHRrr0bP A7F7F37KJOaO0RdWim4j/FCmuKSCPP6K7xq+8CbLOufjID17J3G0oV75CQ6Nl57WMbHg SkAw== 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=AziVDiNsJfWSoLEyeTjoxxBYGSeB6j2LvcpA2gkjX9Y=; b=Ieemg7/lT5QvmmH8WN52UYT5nWgq+O7tMox03m0YNuMxt48JuNagZENVCPmZEUPkt3 hbXuXMY2UVzWCB4YpNyQk2yD177frQG39z2qNywun2kAm8T6OSkSY5qYYzMBJTQH6Hxy 4qXyHIRtEzmkpoq8WutMlSSv4kVhPEjKsKmehkcmm4g3npY30nQ9nDVw/OneXg1U+45A Bh+zr2h+CZdltI1fjhvEz98003CpLpfdUxSw3HjLFZilGXl5z7yzFkLo9OggLkpX8oJf tSPHjqLFf3skh7U8w24RnoDTr5r/PCNSdaKvvs+VceQytMTVx0LM9YPp9pPV1i82gZVQ zXFw== X-Gm-Message-State: APjAAAWVxZQzb3OTPJKSV0O3H/CAtmu9NjVhwLnJl2aJ7Wy76h7nzW8P xWESGen2dOmxmkobVBnApqApSp7w X-Google-Smtp-Source: APXvYqy1fUbJXE5zasPgxg4UQV5TR/ejP9L4h//1lvPpmqMTiUelwv/c0XoLmR3pFUht8zTHGnmYKA== X-Received: by 2002:a63:2903:: with SMTP id p3mr5863365pgp.306.1567733423455; Thu, 05 Sep 2019 18:30:23 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id g11sm3332294pgu.11.2019.09.05.18.30.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Sep 2019 18:30:22 -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?b?UmFkaW0gS3LEjW3DocWZ?= , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH RESEND 4/5] KVM: VMX: Stop the preemption timer during vCPU reset Date: Fri, 6 Sep 2019 09:30:03 +0800 Message-Id: <1567733404-7759-4-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 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li The hrtimer which is used to emulate lapic timer is stopped during vcpu reset, preemption timer should do the same. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/kvm/vmx/vmx.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 570a233..f794929 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -4162,6 +4162,7 @@ static void vmx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->arch.microcode_version = 0x100000000ULL; vmx->vcpu.arch.regs[VCPU_REGS_RDX] = get_rdx_init_val(); + vmx->hv_deadline_tsc = -1; kvm_set_cr8(vcpu, 0); if (!init_event) { From patchwork Fri Sep 6 01:30:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 11134173 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 59679112B for ; Fri, 6 Sep 2019 01:30:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 27D9620820 for ; Fri, 6 Sep 2019 01:30:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BFmak78K" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404182AbfIFBa1 (ORCPT ); Thu, 5 Sep 2019 21:30:27 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:34445 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403979AbfIFBa1 (ORCPT ); Thu, 5 Sep 2019 21:30:27 -0400 Received: by mail-pf1-f196.google.com with SMTP id r12so3191236pfh.1; Thu, 05 Sep 2019 18:30:26 -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=lzU0KnwlLwZRT/00/44UaOi16RS92F7Mftz12Dneqw0=; b=BFmak78KIEynzF4dsEtKE2pyq+InTsB0IwkxiZIhXyCANQaY+y2RNUcjNY+eQP0297 /3bxyIm1pAnXXCbWwE3pCwQRcnvtI3Jmki9dxQ0VJa43AFWsgjsMuvdUSPpGRY3ulEW3 DiR14PUuuB4xDypIGY3uwsdDa1BaQNohXBqlcK1Evci2KZ+M7e4DdhmeLho9Qo36fHQu qf52Vsjaw5jOXPCKwVPc7a9Y0rglI3z9Agc/2+zHYlhnfh6/nDBNQ8GVn5pp9QdwA1kS m7AosbQmRSdZjhmb/vrTF1zwu898BZn74VD1veuFFLUc5sAGhavFEtrFxBEs3UnvSVeN 28jw== 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=lzU0KnwlLwZRT/00/44UaOi16RS92F7Mftz12Dneqw0=; b=su8StgWMqC/QPPxj+xvWYabG+CHx9tzAgDVOY49wNMM2NFZU/siZPYQLNhc0NqhfIY KrNeebwkkmSY41K4uNvMYA1mp+YB2pXblk7rJZ1uKbt0QWsBfrF0FPT54Tr+PkiejYZr ZQKzDk0d8TwQWxX2+mzB6l6Mh4KPo7ZsSpdZJa/fnAj5/Q8Obk4e11u2d0Z7a8eSEBUH IO0YlYV1vKIsVR2K0IXSdusG1ka+J6YLVCXhwEIBDV3kMizv+I3iJ/f7SPtQE1ohqVak JcnL2+WM85O5mCscj8etE6RJgyzO5jgpQXcAQn1EfLQ1RrlPYAmqjcVX2feUhLiYmsJi SraQ== X-Gm-Message-State: APjAAAUthm0foSjuszinybBxNXnYRU9Z2S/yrsp4z1MsWc8yhGxk9/9s lq5VT8qhcQyjmD0E885u1qOR8Hjk X-Google-Smtp-Source: APXvYqyPzRoAay3eF3jG73d8xyiw9BP2olD/7SS7dUF38q/pUW5/uylf1N/SvNnzddQ3nZ3DoHyg2A== X-Received: by 2002:a65:5a8c:: with SMTP id c12mr5706037pgt.73.1567733426195; Thu, 05 Sep 2019 18:30:26 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id g11sm3332294pgu.11.2019.09.05.18.30.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 05 Sep 2019 18:30:25 -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?b?UmFkaW0gS3LEjW3DocWZ?= , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH v2 5/5] KVM: hyperv: Fix Direct Synthetic timers assert an interrupt w/o lapic_in_kernel Date: Fri, 6 Sep 2019 09:30:04 +0800 Message-Id: <1567733404-7759-5-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 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Reported by syzkaller: kasan: GPF could be caused by NULL-ptr deref or user memory access general protection fault: 0000 [#1] PREEMPT SMP KASAN RIP: 0010:__apic_accept_irq+0x46/0x740 arch/x86/kvm/lapic.c:1029 Call Trace: kvm_apic_set_irq+0xb4/0x140 arch/x86/kvm/lapic.c:558 stimer_notify_direct arch/x86/kvm/hyperv.c:648 [inline] stimer_expiration arch/x86/kvm/hyperv.c:659 [inline] kvm_hv_process_stimers+0x594/0x1650 arch/x86/kvm/hyperv.c:686 vcpu_enter_guest+0x2b2a/0x54b0 arch/x86/kvm/x86.c:7896 vcpu_run+0x393/0xd40 arch/x86/kvm/x86.c:8152 kvm_arch_vcpu_ioctl_run+0x636/0x900 arch/x86/kvm/x86.c:8360 kvm_vcpu_ioctl+0x6cf/0xaf0 arch/x86/kvm/../../../virt/kvm/kvm_main.c:2765 The testcase programs HV_X64_MSR_STIMERn_CONFIG/HV_X64_MSR_STIMERn_COUNT, in addition, there is no lapic in the kernel, the counters value are small enough in order that kvm_hv_process_stimers() inject this already-expired timer interrupt into the guest through lapic in the kernel which triggers the NULL deferencing. This patch fixes it by don't advertise direct mode synthetic timers and discarding the inject when lapic is not in kernel. Reported-by: syzbot+dff25ee91f0c7d5c1695@syzkaller.appspotmail.com Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Vitaly Kuznetsov Signed-off-by: Wanpeng Li --- v1 -> v2: * don't advertise direct mode synthetic timers when lapic is not in kernel arch/x86/kvm/hyperv.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index c10a8b1..069e655 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -645,7 +645,9 @@ static int stimer_notify_direct(struct kvm_vcpu_hv_stimer *stimer) .vector = stimer->config.apic_vector }; - return !kvm_apic_set_irq(vcpu, &irq, NULL); + if (lapic_in_kernel(vcpu)) + return !kvm_apic_set_irq(vcpu, &irq, NULL); + return 0; } static void stimer_expiration(struct kvm_vcpu_hv_stimer *stimer) @@ -1849,7 +1851,13 @@ int kvm_vcpu_ioctl_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE; ent->edx |= HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE; - ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE; + + /* + * Direct Synthetic timers only make sense with in-kernel + * LAPIC + */ + if (lapic_in_kernel(vcpu)) + ent->edx |= HV_STIMER_DIRECT_MODE_AVAILABLE; break;