From patchwork Fri Mar 11 08:29:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12777602 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 80747C433F5 for ; Fri, 11 Mar 2022 08:30:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235630AbiCKIbM (ORCPT ); Fri, 11 Mar 2022 03:31:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229558AbiCKIbK (ORCPT ); Fri, 11 Mar 2022 03:31:10 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BEF7D1B45FD; Fri, 11 Mar 2022 00:30:07 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id 9so7116795pll.6; Fri, 11 Mar 2022 00:30:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=gDqiN5pf4YDkXe9dP0rfveBOw3P+FW2+px95EQefoNY=; b=AZEZcXX6nMsaAwas+IdJHLE7pZOp4Ov+sL7IwkhOxtxDJQfEFh0cgmGklog0Sm7tRP E3yk44onMe7eFe037XDU2MyVeqiV/ide7lkKskpnZ0k51oBxaCVZ6A0oVGBZ6z89F0dt aa8g5wl5qsI+hQcDSS1PDktAOOlpMxPBunZUaOTbYab468G1CrUwy5Ab2d1LhgT3Jeom d6Hx6DPWZvYHqcx8n4mncACAZKZvzDAzsgioKeah6ZWYEZPdubWNNpLm/ENzF+t4kM1a Ne5yFHm/FxO3kC1S6+6mc5njSf74/nsmqzkKTRSVsQipQbWugRkIjPbpMKEoTnsyem1K 2N1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=gDqiN5pf4YDkXe9dP0rfveBOw3P+FW2+px95EQefoNY=; b=pq19kadIZVMjGIMhXmM4nzp3yebaKcRN+sXogkEL4WglDvvr2PqlWrdGjiEBx0td6a SLe0ilubbrMzluh3rLW+iKD2qY9JYtCu+6lrJiYAUBiNk5Kk+Wj57E2+WHn39TiQQMmh BmosAl5/ryRKfkGlVgUDU4B5XnOCr5KdGtxsZKXjXE+FlxcVWTNOIrXQhH7UvJbyTZf3 dQIG2dyvN2btDxOrQX4TmLra6lWe8YwA97+Qp0Wm6LYUSFE9P0q/kqa+BfKVvCQvHdzP CpzalnoUuoMY3RHYfwZrH+Pz4ZzihjBzOcmTNnXiektto5vi72AJ91Gvw8jsvSyODfRv abXQ== X-Gm-Message-State: AOAM533Reha0H0uA3wwkNCMTh8uf9bwO3/pPer8Ml2SyHnhoEtY6kudL Ddj2zlI/mNLxX4uSLECoENXeYWNkklA= X-Google-Smtp-Source: ABdhPJxF+HbhIh7uRDvBNu9loK5OTQP9MoDFZkm1ntli+DYFDfjHUae21ehsfZ2ba2ViD332ECJXTQ== X-Received: by 2002:a17:90b:4595:b0:1be:db22:8327 with SMTP id hd21-20020a17090b459500b001bedb228327mr20604506pjb.99.1646987406951; Fri, 11 Mar 2022 00:30:06 -0800 (PST) Received: from localhost.localdomain ([203.205.141.111]) by smtp.googlemail.com with ESMTPSA id l1-20020a17090aec0100b001bfa1bafeadsm9090576pjy.53.2022.03.11.00.30.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Mar 2022 00:30:06 -0800 (PST) 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 1/5] KVM: X86: Add MSR_KVM_PREEMPT_COUNT support Date: Fri, 11 Mar 2022 00:29:10 -0800 Message-Id: <1646987354-28644-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> References: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li x86 preempt_count is per-cpu, any non-zero value for preempt_count indicates that either preemption has been disabled explicitly or the CPU is currently servicing some sort of interrupt. The guest will pass this value to the hypervisor, so the hypervisor knows whether the guest is running in the critical section. Signed-off-by: Wanpeng Li --- arch/x86/include/asm/kvm_host.h | 6 +++++ arch/x86/include/uapi/asm/kvm_para.h | 2 ++ arch/x86/kvm/x86.c | 35 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index f72e80178ffc..50f011a7445a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -856,6 +856,12 @@ struct kvm_vcpu_arch { u64 msr_kvm_poll_control; + struct { + u64 msr_val; + bool preempt_count_enabled; + struct gfn_to_hva_cache preempt_count_cache; + } pv_pc; + /* * Indicates the guest is trying to write a gfn that contains one or * more of the PTEs used to translate the write itself, i.e. the access diff --git a/arch/x86/include/uapi/asm/kvm_para.h b/arch/x86/include/uapi/asm/kvm_para.h index 6e64b27b2c1e..f99fa4407604 100644 --- a/arch/x86/include/uapi/asm/kvm_para.h +++ b/arch/x86/include/uapi/asm/kvm_para.h @@ -36,6 +36,7 @@ #define KVM_FEATURE_MSI_EXT_DEST_ID 15 #define KVM_FEATURE_HC_MAP_GPA_RANGE 16 #define KVM_FEATURE_MIGRATION_CONTROL 17 +#define KVM_FEATURE_PREEMPT_COUNT 18 #define KVM_HINTS_REALTIME 0 @@ -58,6 +59,7 @@ #define MSR_KVM_ASYNC_PF_INT 0x4b564d06 #define MSR_KVM_ASYNC_PF_ACK 0x4b564d07 #define MSR_KVM_MIGRATION_CONTROL 0x4b564d08 +#define MSR_KVM_PREEMPT_COUNT 0x4b564d09 struct kvm_steal_time { __u64 steal; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 51106d32f04e..af75e273cb32 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1456,6 +1456,7 @@ static const u32 emulated_msrs_all[] = { MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK, + MSR_KVM_PREEMPT_COUNT, MSR_IA32_TSC_ADJUST, MSR_IA32_TSC_DEADLINE, @@ -3433,6 +3434,25 @@ static void record_steal_time(struct kvm_vcpu *vcpu) mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); } +static int kvm_pv_enable_preempt_count(struct kvm_vcpu *vcpu, u64 data) +{ + u64 addr = data & ~KVM_MSR_ENABLED; + struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_pc.preempt_count_cache; + + vcpu->arch.pv_pc.preempt_count_enabled = false; + vcpu->arch.pv_pc.msr_val = data; + + if (!(data & KVM_MSR_ENABLED)) + return 0; + + if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, sizeof(int))) + return 1; + + vcpu->arch.pv_pc.preempt_count_enabled = true; + + return 0; +} + int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { bool pr = false; @@ -3652,6 +3672,14 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.msr_kvm_poll_control = data; break; + case MSR_KVM_PREEMPT_COUNT: + if (!guest_pv_has(vcpu, KVM_FEATURE_PREEMPT_COUNT)) + return 1; + + if (kvm_pv_enable_preempt_count(vcpu, data)) + return 1; + break; + case MSR_IA32_MCG_CTL: case MSR_IA32_MCG_STATUS: case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: @@ -3992,6 +4020,12 @@ int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) msr_info->data = vcpu->arch.msr_kvm_poll_control; break; + case MSR_KVM_PREEMPT_COUNT: + if (!guest_pv_has(vcpu, KVM_FEATURE_PREEMPT_COUNT)) + return 1; + + msr_info->data = vcpu->arch.pv_pc.msr_val; + break; case MSR_IA32_P5_MC_ADDR: case MSR_IA32_P5_MC_TYPE: case MSR_IA32_MCG_CAP: @@ -11190,6 +11224,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.pending_external_vector = -1; vcpu->arch.preempted_in_kernel = false; + vcpu->arch.pv_pc.preempt_count_enabled = false; #if IS_ENABLED(CONFIG_HYPERV) vcpu->arch.hv_root_tdp = INVALID_PAGE; From patchwork Fri Mar 11 08:29:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12777603 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6FAE8C433F5 for ; Fri, 11 Mar 2022 08:30:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236322AbiCKIbU (ORCPT ); Fri, 11 Mar 2022 03:31:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236040AbiCKIbM (ORCPT ); Fri, 11 Mar 2022 03:31:12 -0500 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 394361B45FD; Fri, 11 Mar 2022 00:30:10 -0800 (PST) Received: by mail-pj1-x102f.google.com with SMTP id gj15-20020a17090b108f00b001bef86c67c1so7548677pjb.3; Fri, 11 Mar 2022 00:30:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bg55DIKCupebm5f4NqZ1g4YC1+NWU4JZUPE3B9his68=; b=FSBiFrp2EFh5RmqWuzLMcjAmroQnEnE9yw7vJXNjwVHqRPIWfux2pKZbvcwhluUVV+ FoPvvMujHYJ5BfIDZ2ABkv/RlqE3Rj4oY5HtjReuw0zGKe/P+KNzExmXwhuhVTrWdGM9 egDKBCdtHFCpSta8gVPdn3LLtE985CVPqd/TtRuwmw+yg2LBBpVlHLj3G5TzRabUe8Kb c1fdRBCTyujvvI3X/kkQ7WC0vmkCoTH4P9qL/Yn8rbHIh4VFUg8z6Dg5KCoebRjFL5Ga eiRcdOBDhRsuYANJ6SH2XDKTPikqpSrAjPsuEj/JhweLWgidvO57UHVIgIHog0BqbDIR zUyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bg55DIKCupebm5f4NqZ1g4YC1+NWU4JZUPE3B9his68=; b=QyIjUYxDlic6d4WY1k17JJNkSJLtzHZMlMjvcHOT+L3FXUEllKqXYLNFLlsrt1E6DM V0CoOFubYQXDKE3/zaImPqLx+fTMlpeWpQqCISd0L807/OZQFF9wqrc6ioigluZmJbMd kLNFmLWP1nbi1dPOmS02ruMtZBm26+RrFPuG9AAf6YEJIgSoQPeUs7/WWT0vQnbRSRuZ 2SQ4jP4M7EbzC5epsclDaw72ZR25WHUPw7YmmJ4uRDGZO73cSscSD4/L1xGshwbtC2f/ FTjPHp6GVkb6lZ/ORMFXmE1uaMYFKJah7BrSlakCrp7BqXieHfsfQb8xtwaxrt/u/eVc c98g== X-Gm-Message-State: AOAM533jpF1XVSAT2yhBPobtvkhqgb7KME/OEQsoOxujR3A9eaYY0TxE TFjhL6Dtlg/8GAC80nLYHt55IfNA2cg= X-Google-Smtp-Source: ABdhPJyGH6VBWlCi6KneX/AvpkwmrBm1Oe7zOEAaHBwhOGeVdFskQ29FwrAzKSI73+1WUByBy2T9cg== X-Received: by 2002:a17:90a:1941:b0:1bf:3918:d49e with SMTP id 1-20020a17090a194100b001bf3918d49emr9411051pjh.136.1646987409672; Fri, 11 Mar 2022 00:30:09 -0800 (PST) Received: from localhost.localdomain ([203.205.141.111]) by smtp.googlemail.com with ESMTPSA id l1-20020a17090aec0100b001bfa1bafeadsm9090576pjy.53.2022.03.11.00.30.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Mar 2022 00:30:09 -0800 (PST) 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 2/5] KVM: X86: Add guest interrupt disable state support Date: Fri, 11 Mar 2022 00:29:11 -0800 Message-Id: <1646987354-28644-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> References: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Let's get the information whether or not guests disable interruptions. Signed-off-by: Wanpeng Li --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 3 +++ 2 files changed, 4 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 50f011a7445a..8e05cbfa9827 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -861,6 +861,7 @@ struct kvm_vcpu_arch { bool preempt_count_enabled; struct gfn_to_hva_cache preempt_count_cache; } pv_pc; + bool irq_disabled; /* * Indicates the guest is trying to write a gfn that contains one or diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index af75e273cb32..425fd7f38fa9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4576,6 +4576,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) static_call(kvm_x86_vcpu_load)(vcpu, cpu); + vcpu->arch.irq_disabled = false; /* Save host pkru register if supported */ vcpu->arch.host_pkru = read_pkru(); @@ -4668,6 +4669,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) static_call(kvm_x86_vcpu_put)(vcpu); vcpu->arch.last_host_tsc = rdtsc(); + vcpu->arch.irq_disabled = !static_call(kvm_x86_get_if_flag)(vcpu); } static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, @@ -11225,6 +11227,7 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.pending_external_vector = -1; vcpu->arch.preempted_in_kernel = false; vcpu->arch.pv_pc.preempt_count_enabled = false; + vcpu->arch.irq_disabled = false; #if IS_ENABLED(CONFIG_HYPERV) vcpu->arch.hv_root_tdp = INVALID_PAGE; From patchwork Fri Mar 11 08:29:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12777604 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C561C433FE for ; Fri, 11 Mar 2022 08:30:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236917AbiCKIbV (ORCPT ); Fri, 11 Mar 2022 03:31:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233350AbiCKIbP (ORCPT ); Fri, 11 Mar 2022 03:31:15 -0500 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28F761B9882; Fri, 11 Mar 2022 00:30:13 -0800 (PST) Received: by mail-pf1-x436.google.com with SMTP id s11so7266163pfu.13; Fri, 11 Mar 2022 00:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=NsYPjIMYV5tX+pXV9c5Xqy6DdjLmleLEjTqHZPocps0=; b=S3MDpP6CG9hy5C3F2jdmw1F5XMoi2qqCEwDIu62mcrMxBW0MQa2eWbFRLpNjkMIpXw pyTV0p+3ZwMzIG4FT8wxYYMBK7fL8BtbMVJmjVPiDtu3UDSIUVWV8SffTR5pjo5Wpmux pwmnl4Pb0KtNEGe79mJJSVuLbE81u9XqSrjwhDO/r2kyUMzzU9CACsIdUerLf7Wj8cVr KoWhHlQR2ELFHFv7UhCo9Hp3DFd5StfFTYaTLGaK9f3IvAgErkHb8CKycYG/hwPbfBNh 1IkbWFJi5mUTeLXdxEYxjxB9vFdR2Q+8MPkHdRCG30KgSD6LrzUX8c8czAHx2+RsIP9y RtQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=NsYPjIMYV5tX+pXV9c5Xqy6DdjLmleLEjTqHZPocps0=; b=d/+gREeM4U36OjP/qETZ32LQwCn6FMdsmrEJuNS2X7cUDkGxKtL8Ns7B6TCD1WsKEE C6PLYTMXBcxMb976PjX0X9wk/3AmSif4qoKYBKjt1RV4NohAZnXxkmPBdDHu2G8sz8JD srHdnkrlfRrOWhXxV0lahb+u3Vlurmr5kHUKgfFOUu+zB3TIFnmnINwpLldfggoV/YCE s62tH4VWMnb77nvAalfEFE8tELibQY9XB52FLPPfsi67hvoPq71Py4o/nUzBdSiEjcQZ IuJG0/HBX3oaHDK5ng6PBRFOvBNBHXy4Ay3wrAqQLH5lUvEBt7bScHmK+UodiPJzRdhp K0FQ== X-Gm-Message-State: AOAM532Q1k7VMvwGUac4XM1sDHs6GwrbnUdG5uyTWdsi6DynG6kDPnkz 19HgCc1vg5/3NMBnJSrzh2RPAfr5dmk= X-Google-Smtp-Source: ABdhPJw3gCV80jRT9CDgfCsAV9mCk1p3R1GPkSXxWXR1LPAwd+ESOJ8gmooAWRB5PeOLIcEFg9Rxng== X-Received: by 2002:a05:6a00:10cb:b0:4f7:942:6a22 with SMTP id d11-20020a056a0010cb00b004f709426a22mr9016766pfu.84.1646987412231; Fri, 11 Mar 2022 00:30:12 -0800 (PST) Received: from localhost.localdomain ([203.205.141.111]) by smtp.googlemail.com with ESMTPSA id l1-20020a17090aec0100b001bfa1bafeadsm9090576pjy.53.2022.03.11.00.30.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Mar 2022 00:30:12 -0800 (PST) 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 3/5] KVM: X86: Boost vCPU which is in the critical section Date: Fri, 11 Mar 2022 00:29:12 -0800 Message-Id: <1646987354-28644-4-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> References: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li The missing semantic gap that occurs when a guest OS is preempted when executing its own critical section, this leads to degradation of application scalability. We try to bridge this semantic gap in some ways, by passing guest preempt_count to the host and checking guest irq disable state, the hypervisor now knows whether guest OSes are running in the critical section, the hypervisor yield-on-spin heuristics can be more smart this time to boost the vCPU candidate who is in the critical section to mitigate this preemption problem, in addition, it is more likely to be a potential lock holder. Testing on 96 HT 2 socket Xeon CLX server, with 96 vCPUs VM 100GB RAM, one VM running benchmark, the other(none-2) VMs running cpu-bound workloads, There is no performance regression for other benchmarks like Unixbench etc. 1VM: vanilla optimized improved hackbench -l 50000 28 21.45 30.5% ebizzy -M 12189 12354 1.4% dbench 712 MB/sec 722 MB/sec 1.4% 2VM: vanilla optimized improved hackbench -l 10000 29.4 26 13% ebizzy -M 3834 4033 5% dbench 42.3 MB/sec 44.1 MB/sec 4.3% 3VM: vanilla optimized improved hackbench -l 10000 47 35.46 33% ebizzy -M 3828 4031 5% dbench 30.5 MB/sec 31.16 MB/sec 2.3% Signed-off-by: Wanpeng Li --- arch/x86/kvm/x86.c | 22 ++++++++++++++++++++++ include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 425fd7f38fa9..6b300496bbd0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10375,6 +10375,28 @@ static int vcpu_run(struct kvm_vcpu *vcpu) return r; } +static int kvm_vcpu_non_preemptable(struct kvm_vcpu *vcpu) +{ + int count; + + if (!vcpu->arch.pv_pc.preempt_count_enabled) + return 0; + + if (!kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_pc.preempt_count_cache, + &count, sizeof(int))) + return (count & ~PREEMPT_NEED_RESCHED); + + return 0; +} + +bool kvm_arch_boost_candidate(struct kvm_vcpu *vcpu) +{ + if (vcpu->arch.irq_disabled || kvm_vcpu_non_preemptable(vcpu)) + return true; + + return false; +} + static inline int complete_emulated_io(struct kvm_vcpu *vcpu) { int r; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 252ee4a61b58..9f1a7d9540de 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1428,6 +1428,7 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); int kvm_arch_post_init_vm(struct kvm *kvm); void kvm_arch_pre_destroy_vm(struct kvm *kvm); int kvm_arch_create_vm_debugfs(struct kvm *kvm); +bool kvm_arch_boost_candidate(struct kvm_vcpu *vcpu); #ifndef __KVM_HAVE_ARCH_VM_ALLOC /* diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 9581a24c3d17..ee5a788892e0 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3545,6 +3545,11 @@ bool __weak kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu) return false; } +bool __weak kvm_arch_boost_candidate(struct kvm_vcpu *vcpu) +{ + return true; +} + void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) { struct kvm *kvm = me->kvm; @@ -3580,6 +3585,8 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) !kvm_arch_dy_has_pending_interrupt(vcpu) && !kvm_arch_vcpu_in_kernel(vcpu)) continue; + if (!kvm_arch_boost_candidate(vcpu)) + continue; if (!kvm_vcpu_eligible_for_directed_yield(vcpu)) continue; From patchwork Fri Mar 11 08:29:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12777605 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8CB1EC433EF for ; Fri, 11 Mar 2022 08:30:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237025AbiCKIbW (ORCPT ); Fri, 11 Mar 2022 03:31:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47050 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236470AbiCKIbT (ORCPT ); Fri, 11 Mar 2022 03:31:19 -0500 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69FEA1B988B; Fri, 11 Mar 2022 00:30:15 -0800 (PST) Received: by mail-pf1-x42b.google.com with SMTP id z16so7309133pfh.3; Fri, 11 Mar 2022 00:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=4jbCwzr0mct8OKs/Lgqse4ZkWggPrV/4VXWHupxqHk0=; b=VqrLn451/V+9LmXpVS924CzvE55eM93ytE7ZgyeTObSH2OrmQXwl9qDfSaFJ/vazxR AODWzxxPe5qwA4AO7E6sUHWvtmCTUlQVPgu8wmZ8H05v3RPixnsKTEMtE5t9z7ehRSuI 18prbPVHZ8xhGX4LdwK5xRsK1GJU/gRpl29QQ+bdAUriUyuR3vHW+3vg9eSzeXOnj6vH pQ9xSN7CksgJkbOEcJmwR1cqFBpkZGodZJOMigZjJOpRiVwUWppFuoINEzsXIO+bPyTJ Q+6HbzGdD5gmEaiVq2zUrAvb3g9/lM/Kv9BYcRggrDqB6vb7DRLRhhzlUG7T6czvaUia qs0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=4jbCwzr0mct8OKs/Lgqse4ZkWggPrV/4VXWHupxqHk0=; b=M7ZIwkjLDhbNqt6yAeTKCs+kbBKNDf4eaFc53dZZ8c+EIKfgxAItFyYE+WeIzCcrDT QNy2C3XLUuM355lApIt88p36MgN4VgMkmWbKXNdvmBL0QBnyUwpLuyPutt/TtHQVmQls b4unoTuatD6onDqWQzjAe0Y8CzYM1z+uIwgdNiecX0isqUklmOFRTDSMqAPf9+zQpjjD 12YB1aTyNHiVtAST2H4uAlmTvnO8wmETTr88NEny/LHZA78cgtxmwPkD2R5IzbzXerUe 683YRLeHVskS4K6dkwi4gZn1amlwzZ99i275nixzc4D2lKhWZUawU/W3N1Wm7rIhbw7T Zwag== X-Gm-Message-State: AOAM530OHxHQbrp4IawIYVKmoYjyYrtk8u8uEyIMnMQKi2BOOc9vLtir YPvUhZED+bsdtA2nywBkJwNKBVxX1kM= X-Google-Smtp-Source: ABdhPJxkwC/ucfh7BHQHX2m5BBwxJbpRbTv0HYpts41pL4Iwh309L3G4S9C8putZrR2mYydEc+WHOQ== X-Received: by 2002:a05:6a00:885:b0:4f4:17d8:be31 with SMTP id q5-20020a056a00088500b004f417d8be31mr9128043pfj.57.1646987414815; Fri, 11 Mar 2022 00:30:14 -0800 (PST) Received: from localhost.localdomain ([203.205.141.111]) by smtp.googlemail.com with ESMTPSA id l1-20020a17090aec0100b001bfa1bafeadsm9090576pjy.53.2022.03.11.00.30.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Mar 2022 00:30:14 -0800 (PST) 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 4/5] x86/kvm: Add MSR_KVM_PREEMPT_COUNT guest support Date: Fri, 11 Mar 2022 00:29:13 -0800 Message-Id: <1646987354-28644-5-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> References: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li The x86 guest passes the per-cpu preempt_count value to the hypervisor, so the hypervisor knows whether the guest is running in the critical section. Signed-off-by: Wanpeng Li --- arch/x86/kernel/kvm.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 21933095a10e..e389fa4393ae 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -366,6 +366,14 @@ static void kvm_guest_cpu_init(void) if (has_steal_clock) kvm_register_steal_time(); + + if (kvm_para_has_feature(KVM_FEATURE_PREEMPT_COUNT)) { + u64 pa = slow_virt_to_phys(this_cpu_ptr(&__preempt_count)) + | KVM_MSR_ENABLED; + wrmsrl(MSR_KVM_PREEMPT_COUNT, pa); + + pr_debug("setup pv preempt_count: cpu %d\n", smp_processor_id()); + } } static void kvm_pv_disable_apf(void) @@ -442,6 +450,8 @@ static void kvm_guest_cpu_offline(bool shutdown) if (!shutdown) apf_task_wake_all(); kvmclock_disable(); + if (kvm_para_has_feature(KVM_FEATURE_PREEMPT_COUNT)) + wrmsrl(MSR_KVM_PREEMPT_COUNT, 0); } static int kvm_cpu_online(unsigned int cpu) From patchwork Fri Mar 11 08:29:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12777606 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E205BC433F5 for ; Fri, 11 Mar 2022 08:30:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236636AbiCKIbZ (ORCPT ); Fri, 11 Mar 2022 03:31:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236694AbiCKIbU (ORCPT ); Fri, 11 Mar 2022 03:31:20 -0500 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 156E31B45FD; Fri, 11 Mar 2022 00:30:18 -0800 (PST) Received: by mail-pg1-x52f.google.com with SMTP id o23so6871494pgk.13; Fri, 11 Mar 2022 00:30:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=EV0wmNwRVxX8NQcoop+BEUS5js89FYD4nmxPHK49pWQ=; b=RrVI3XYBW4Johmu7rVRoRIRMELwbIMEQGU6gIqjMpTGdMT8ebwN8sRXICZNvCvvu5j l/hb2+A5ZXJ5vqw3gXP4MwbmhAA4HGSZr+0IJH7DGxTVBjqkbrLeqhzE9fv6UzIx+QNj xj3JpteEUBMII006M1xmgqcp4q2G2hem5fEMpyzal58kW/U6ZXQCN/AvAn9m39qbuN2U P41237l1RVwxdvtfJ2ZOh7rIvBwrOe3JwfG3sEe9+jLU140EJ/F3C6z5i4JfddZikt7p bCHCoPSISq/ePfh8UIoEjfabIAP9g5i1bPIB41eWaVqAIMHS8U2gA8rJn7GxyoAYlNS5 kcog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EV0wmNwRVxX8NQcoop+BEUS5js89FYD4nmxPHK49pWQ=; b=yMR1fynUYq9YPX9QI19wAzpPpvHPHy0PCy+t853xja9xSrf0NpSrIoDs/Zaz47ToUk IkaivxMT91tr2pXJUamu4gIyyPLh/0ki+mnyzt8PQHI+5KIeC3nEpzDmZsGVPg42EZ83 8MFYdM8HzDuULe3/UIiKJhiqRsF945We5QcXVvV6gVWQ/0sAdyPDl6ii8lacIBXHCMhh 7azUvAHAc6N8oiDNLxlnWPVhH7tCAdY76Q5OFkKU4tgefV4Wrad2XYP6t8Wfe4Q14MWJ hM3QpDOya9srRylqGb/4Zb+LcgI2F3U3OKdkR9Bx+vnNx+0ndggOE+FsJpLKDiFMM7Ie FdKg== X-Gm-Message-State: AOAM531ZRxEKsKrYs+E2jBtyEfirVitXSu3OjrbxynEg0rVlw/g7//UU DiPghfryqsI2v64fDQe+MJJGmE1BsFs= X-Google-Smtp-Source: ABdhPJzlNsy3e1ptcNA1gVdmyGzeVVvFjO+vtZKTXz+mMFFivnXYL8Q+yd6xL5Xp3kOTB5m7PdGErg== X-Received: by 2002:a63:82c2:0:b0:37c:942b:96db with SMTP id w185-20020a6382c2000000b0037c942b96dbmr7422065pgd.286.1646987417357; Fri, 11 Mar 2022 00:30:17 -0800 (PST) Received: from localhost.localdomain ([203.205.141.111]) by smtp.googlemail.com with ESMTPSA id l1-20020a17090aec0100b001bfa1bafeadsm9090576pjy.53.2022.03.11.00.30.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Mar 2022 00:30:17 -0800 (PST) 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 5/5] KVM: X86: Expose PREEMT_COUNT CPUID feature bit to guest Date: Fri, 11 Mar 2022 00:29:14 -0800 Message-Id: <1646987354-28644-6-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> References: <1646987354-28644-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Expose the PREEMPT_COUNT feature bit to the guest, the guest can check this feature bit before using MSR_KVM_PREEMPT_COUNT. Signed-off-by: Wanpeng Li --- Documentation/virt/kvm/cpuid.rst | 3 +++ arch/x86/kvm/cpuid.c | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/cpuid.rst b/Documentation/virt/kvm/cpuid.rst index bda3e3e737d7..c45158af98a7 100644 --- a/Documentation/virt/kvm/cpuid.rst +++ b/Documentation/virt/kvm/cpuid.rst @@ -103,6 +103,9 @@ KVM_FEATURE_HC_MAP_GPA_RANGE 16 guest checks this feature bit bef KVM_FEATURE_MIGRATION_CONTROL 17 guest checks this feature bit before using MSR_KVM_MIGRATION_CONTROL +KVM_FEATURE_PREEMPT_COUNT 18 guest checks this feature bit before + using MSR_KVM_PREEMPT_COUNT + KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24 host will warn if no guest-side per-cpu warps are expected in kvmclock diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 58b0b4e0263c..4785f5a63d8d 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1071,7 +1071,8 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) (1 << KVM_FEATURE_PV_SEND_IPI) | (1 << KVM_FEATURE_POLL_CONTROL) | (1 << KVM_FEATURE_PV_SCHED_YIELD) | - (1 << KVM_FEATURE_ASYNC_PF_INT); + (1 << KVM_FEATURE_ASYNC_PF_INT) | + (1 << KVM_FEATURE_PREEMPT_COUNT); if (sched_info_on()) entry->eax |= (1 << KVM_FEATURE_STEAL_TIME);