Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2959679pxj; Mon, 17 May 2021 14:09:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwSuMuFUHdx1/cBisfTUQVQJFqp0c+p5f7Va7U452XeR/rtEPyauUnNcd4aycMAnfaUSrJM X-Received: by 2002:a05:6402:1295:: with SMTP id w21mr2351612edv.60.1621285779477; Mon, 17 May 2021 14:09:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621285779; cv=none; d=google.com; s=arc-20160816; b=S8BXr31RjtFnVtHOYNk54LrGIaW5BkPpZjAHFWgJSHTDCn8YTbf7vccYf+vvcaICJB jcydtaxmfWnpf3JPIlxJXv8U4eBoeKAa9pVXcnA4chvC55lQpBXCoWrCzWNXLavEdw/L H8B9XyXfePoQ7n9ufn8tBJ/Q+msjVu4WUdF6T+i9T+3oQW1THjeYd6ym5KuPZ5/6TJ7s BUZ3tC+kc1u0WYTmJmqqBzo8NWCUq21p9F62/+W1K8hIEvQrDf3VOvjv04kq6DyV+adO xEGpzuyExUZj/9VoNSPlZsVYma3/sceLuphoW3hegA5AmLsBiZNOEe4haNru4ANdSgAs K/bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=tPPyKEGZVk5W3b/nB56WzLLuyaXrswVyUx1Ed/5o3O4=; b=qo1O6ET0PMuQFrPRy4QlWZlLSBWHdYZHbJfHEdNAiQPWq+qU8qMSCeqtje3wiPL9M+ pTtHaYp8Jf1YlSQfw0U3+rJ+CQ/NvVegM5MyEb+r0pS/pntRshdzol72LthMWQ8LV2PG X3gqUq4pOJnq2TVbizbuanAEVso2pe5nD1QNvGtSOesSz3EMFVfUoDt3AWZSZzO2yKaG YR+gZ5zUFV9W5x+wsizHK2PAI+F8seTG9GTjGlvTrqlUZusElvR2uQP/7KHkHvG4xxjc t0ea1KZW4FJ1n1qxVjJ503EGChkB8dKUTsnZY+X4pO/3S3oHWdTfuMf+Cbcq18WAYcpm 0wUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=atyrkP7B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id h9si3456326edv.97.2021.05.17.14.08.39; Mon, 17 May 2021 14:09:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=atyrkP7B; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S237647AbhEQOC6 (ORCPT + 99 others); Mon, 17 May 2021 10:02:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34742 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237645AbhEQOCw (ORCPT ); Mon, 17 May 2021 10:02:52 -0400 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C51EC06175F; Mon, 17 May 2021 07:01:35 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id gm21so3719961pjb.5; Mon, 17 May 2021 07:01:35 -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; bh=tPPyKEGZVk5W3b/nB56WzLLuyaXrswVyUx1Ed/5o3O4=; b=atyrkP7BjqyHjY2IB7GyY0a5GmCwQFfhG0p/q6m54DEi0AY69wWhJgByPa8Y6nNmdy STirIna+BxQaUMp80+jQIflI7bvR3lrb3IdK++O4vUn80zYUvtsYDnLhGTDXlt9RiU64 DyI5GDe+q9nXiprQ/HGX4de5LDWHvkjuVJIsxM+wxN8pyMGGoyw45u1v7SnGxt+fPxW9 hA/B6NB8CIL/woWH3rS5X9cf0Pd3zvsHronm3N7st5cvEM2iAiwAxTuCEFqeOgnyBWcb FvGLmlHltyHvm/0v0zL6CD7AKbrDe68ov3uLQJaZh9oMAchqii2cqkJtSEEtTXy1mdYS Y35w== 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; bh=tPPyKEGZVk5W3b/nB56WzLLuyaXrswVyUx1Ed/5o3O4=; b=NtcjQRIYnf0YTPuClgyD551zBeiIeU4B0GslxSVFwXh73+4lYywCrBiA/L5m9C8e4Q 7a0RtBNtpVd9XucIxCcQoT8/lS+HIV0QdZaKUP9oix2j4kBp4aey5x/3eMgA0Vhs6i0k mM755pFFbK4HwDQ77JCPVfd5RvJz1LugJXbbtkKGb9vaX0BKR+ef5Lxt86ecwJ7RGh/O 6kv6xF/dcK+U6VKU/Rc1slesktdHp6yEufFvYjm/iBjIYSINlBC2cz1b95GVUUSyIxJf LFPFMWeeQ/5UKnzIAiUrqRMZzqvQE8xd8fams5Tbl3KSECS1Aom1tWH4758+tajg1Lcq 37Rg== X-Gm-Message-State: AOAM532ib0hi8U3RNTsMLlc+JQfCop7UjLLg27DuK7N1a61zufaLlG1f 5ODaue4sx4PuUU6Ki0pY7EPEnjsDUvY= X-Received: by 2002:a17:90a:4404:: with SMTP id s4mr91755pjg.218.1621260094856; Mon, 17 May 2021 07:01:34 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.61]) by smtp.googlemail.com with ESMTPSA id k10sm3074229pfu.175.2021.05.17.07.01.31 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 May 2021 07:01:34 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Subject: [PATCH v3 5/5] KVM: LAPIC: Narrow the timer latency between wait_lapic_expire and world switch Date: Mon, 17 May 2021 07:00:28 -0700 Message-Id: <1621260028-6467-5-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1621260028-6467-1-git-send-email-wanpengli@tencent.com> References: <1621260028-6467-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Wanpeng Li Let's treat lapic_timer_advance_ns automatically tune logic as hypervisor overhead, move it before wait_lapic_expire instead of between wait_lapic_expire and the world switch, the wait duration should be calculated by the up-to-date guest_tsc after the overhead of automatically tune logic. This patch reduces ~30+ cycles for kvm-unit-tests/tscdeadline-latency when testing busy waits. Signed-off-by: Wanpeng Li --- arch/x86/kvm/lapic.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index c0ebef560bd1..552d2acf89ab 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1598,11 +1598,12 @@ static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); apic->lapic_timer.advance_expire_delta = guest_tsc - tsc_deadline; - if (guest_tsc < tsc_deadline) - __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc); - if (lapic_timer_advance_dynamic) adjust_lapic_timer_advance(vcpu, apic->lapic_timer.advance_expire_delta); + + guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); + if (guest_tsc < tsc_deadline) + __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc); } void kvm_wait_lapic_expire(struct kvm_vcpu *vcpu) -- 2.25.1