From patchwork Fri Apr 1 08:10:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12797988 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 42B06C433FE for ; Fri, 1 Apr 2022 08:11:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344240AbiDAINT (ORCPT ); Fri, 1 Apr 2022 04:13:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344245AbiDAIMx (ORCPT ); Fri, 1 Apr 2022 04:12:53 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7431A20289E; Fri, 1 Apr 2022 01:11:03 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id t13so1830191pgn.8; Fri, 01 Apr 2022 01:11:03 -0700 (PDT) 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=hK83t9f5HQafaC7imUrsohagDN5P+WqmTJrwFOTNByc=; b=Hb6r8iUYn8KML/e3dSFoz16o8xys6lniHfXomQc7/yT4DWbu5qjMiryK/nOqoLHoLq UhXmoPl+BYg/pxxI6oMFRGMXCzvt+8Y79P0tUkOT+YfFTDoP8rDL78C4nOzoRV2+Zwgg OoFU/k/oRz7YWAAYTXZDbP/MkMIT/pptQim9O2JUG3p7QId+Sn5QfwiDSPgM1dDY5x2l 7rAYw2pbQiuKfRpL/l+BDqOEz4hc4925PV5TC7s+1fMbCUTMLAmitYFOEKzZoy9xlMjh vEOJTaAUm3aB9vn1RCRT0La/nqqdWu3//GxBCJIqyBIX16VdVwuEXdNHjvS15ia2Sz4R 62sQ== 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=hK83t9f5HQafaC7imUrsohagDN5P+WqmTJrwFOTNByc=; b=CK5YC3EMGFUiOYcYRQoQ2OoIwJVpVI805mwZUtrG+KrbozFN6dj5mJi9cX3WCR2Nb3 kSkZU6cKFfij2hYpBHnDHkQ2TO2GaSowG7MP8On3UF/B7hnoprN1cXw8C8awK1qVsPG7 gy5u1Ql3zJThKihIu9/sNPDBT3+UJhR0fUIxpR2z8XloMjA5PpVB4lVmQWdPDpEuqLvq kR8f2xpcjTpVpBxBdGTVjvW9qxlSmJwzUBpRjl5yE9fTjEqzg8trdypEv8hlz3D1nmrc WbyBMoVUHbX0gMYoorriJ01fnAHxD4FcRuJDzRHySfEHy6lXBeoMS4iTfEKvldrdX21y 0rkQ== X-Gm-Message-State: AOAM532hkZ0C5Iz1LkqmHs1V2Hw3US1cn6RkxRWFIMSnq5o9qEdZTvvr +L01clDfrdllKzw4tJdIpIlRQCcQnWs= X-Google-Smtp-Source: ABdhPJx1q466naYnenpZZ56jSDgSLsGsCKFBLwHKaYWeEED5WjcSsFf+UQx3As/Us+l+D7MJ5Mu+Ig== X-Received: by 2002:a05:6a00:114e:b0:4c8:55f7:faad with SMTP id b14-20020a056a00114e00b004c855f7faadmr9781149pfm.86.1648800662811; Fri, 01 Apr 2022 01:11:02 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id hg5-20020a17090b300500b001c795eedcffsm11634790pjb.13.2022.04.01.01.11.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Apr 2022 01:11:02 -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 v2 1/5] KVM: X86: Add MSR_KVM_PREEMPT_COUNT support Date: Fri, 1 Apr 2022 01:10:01 -0700 Message-Id: <1648800605-18074-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648800605-18074-1-git-send-email-wanpengli@tencent.com> References: <1648800605-18074-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 4138939532c6..c13c9ed50903 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -852,6 +852,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 02cf0a7e1d14..f2d2e3d25230 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, @@ -3442,6 +3443,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; @@ -3661,6 +3681,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: @@ -4001,6 +4029,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: @@ -11192,6 +11226,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 Apr 1 08:10:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12797989 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 3F01DC433F5 for ; Fri, 1 Apr 2022 08:11:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344257AbiDAINW (ORCPT ); Fri, 1 Apr 2022 04:13:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344255AbiDAIM4 (ORCPT ); Fri, 1 Apr 2022 04:12:56 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0764D205944; Fri, 1 Apr 2022 01:11:06 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id p17so1830279plo.9; Fri, 01 Apr 2022 01:11:06 -0700 (PDT) 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=eDstlFrAvrXwvAexPbURvjB3hXUck9RQZ0b7BQhloGk=; b=WYcAwxhNIDI0KvdIFnJY7SXXVUsPbAbq+KiIlgtwMepTkOMe77If6zEJ72oV4RqZKI gXLZUpNtz+YA7XwDR2YsYNv+GvkLaHnK2Nkr2bKFwZj/lZEJuFjcNJXQG6vBC4T4N6kr 7MT7s0LC48is8W1tI+tEnjsnbuYVqhq+ZRi21+v1UbPVJB59eaE2Eb8v9MASw0uWYQMU 7/OlbAjgFBjf7i5hU5J5Aka82HCe0eRRmRCUBcP599RhZ5gqsP/sTuRIXSbJe7vKaN83 kNfRyxSBBkvV0NEU+ndWPa6mQwZ8itgiOh88yc8pKWpYpww93yhF1jUJ3uR51hL8Cg2r ZhaQ== 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=eDstlFrAvrXwvAexPbURvjB3hXUck9RQZ0b7BQhloGk=; b=whkoG813GeR+2Wu3UhYQ3gVKvPweVrhkNJIU3As03DamWefAt/pBQmq+9KGBGuSw6L j/6GN/1TcxhV+lOSozdTscpbQ9TuAojBFPrSj+4RNmH3bQuf/HfzNIEOx2fHiTH9XtM4 oE1+4lyQK1F9HhW12wxBq4MWqSZRexOVQwJRYiZZs6PfjscpSuSU8jtex4NdJWKP7z8U y63aUVrf54vjBJJ5KFn3fxBfm/+nouMFEzMhtqYW0ne7+adMlOV1NftGVvbdpzsuLrV5 mWIVZYWP+FsEk3TX7OyPt9eupW7sR0+y4JxvNwUOT+ztBmRi006s6XvsJjHTPu+hXRVT UHNw== X-Gm-Message-State: AOAM5338rkys9yYCOSiNFdVMpBLo4jiQMXngXzmXBDYSswyh9E2Zk/6r i6aVq9PdlybIBo6ANg906AdFTA8ouPU= X-Google-Smtp-Source: ABdhPJy1Jz6RuIeQG6ZFPKVCzSu8yA2b/7j2eo1H/SJ7yWCC7Vw1V5ueouR+wjqzHLAQPrK+pt1loQ== X-Received: by 2002:a17:902:db03:b0:155:cb6a:7c8e with SMTP id m3-20020a170902db0300b00155cb6a7c8emr9314404plx.125.1648800665400; Fri, 01 Apr 2022 01:11:05 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id hg5-20020a17090b300500b001c795eedcffsm11634790pjb.13.2022.04.01.01.11.03 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Apr 2022 01:11:05 -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 v2 2/5] KVM: X86: Add last guest interrupt disable state support Date: Fri, 1 Apr 2022 01:10:02 -0700 Message-Id: <1648800605-18074-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648800605-18074-1-git-send-email-wanpengli@tencent.com> References: <1648800605-18074-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. Except preempt_count, interrupt disable can be treated as another in critical section scenario. Signed-off-by: Wanpeng Li --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 3 +++ 2 files changed, 5 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index c13c9ed50903..7a3eb2ba1d0f 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -857,6 +857,8 @@ struct kvm_vcpu_arch { bool preempt_count_enabled; struct gfn_to_hva_cache preempt_count_cache; } pv_pc; + /* guest irq disabled state, valid iff the vCPU is not loaded */ + bool last_guest_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 f2d2e3d25230..9aa05f79b743 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4584,6 +4584,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) static_call(kvm_x86_vcpu_load)(vcpu, cpu); + vcpu->arch.last_guest_irq_disabled = false; /* Save host pkru register if supported */ vcpu->arch.host_pkru = read_pkru(); @@ -4676,6 +4677,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.last_guest_irq_disabled = !static_call(kvm_x86_get_if_flag)(vcpu); } static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, @@ -11227,6 +11229,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.last_guest_irq_disabled = false; #if IS_ENABLED(CONFIG_HYPERV) vcpu->arch.hv_root_tdp = INVALID_PAGE; From patchwork Fri Apr 1 08:10:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12797990 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 8935EC433EF for ; Fri, 1 Apr 2022 08:11:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344282AbiDAINZ (ORCPT ); Fri, 1 Apr 2022 04:13:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344267AbiDAIM5 (ORCPT ); Fri, 1 Apr 2022 04:12:57 -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 A26A0208C28; Fri, 1 Apr 2022 01:11:08 -0700 (PDT) Received: by mail-pj1-x102e.google.com with SMTP id bx5so1756142pjb.3; Fri, 01 Apr 2022 01:11:08 -0700 (PDT) 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=xXAykXWGOXNzJAmKUrp7qCF5qytKrFOAH5hU4PrPxCQ=; b=E1TKy9D48OqmNmtCAsTugXIkq+8IN9/KmgRiHcEJpR2EmPaU3gDUdUEMeVN4UqBMgf h36MjkSZIy9+23T+kyb0fKYteamHz/BmDmKJQ+jTaNHzjFhzPYBko6tuoWXCpYDObvo+ qK2fehbUqvOGBCrM6mQYGl6wxcZnJQz9/DCpoijFbtpcrrt8FFycmm3flFwhCNUViEOa 9pIgH4AKdBPDunkno4P0jUK1rnZezquKmzM/lbS7yVYekCSrNz1CVF49LOrC9jL6UEq+ +FoubqMOEhfyhnWq7PluMHfhk8CyA5xw5CgOTSYsh/BxUJAR3H+pu0h2dQd0VhklVdx+ vOdw== 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=xXAykXWGOXNzJAmKUrp7qCF5qytKrFOAH5hU4PrPxCQ=; b=rrLYeC339VaCE6EkOeeKAzoKDFMb2sRUXMht8dvFDll9+I2iSDRpnyG4psyEqZit1X ZqFz0EEvHDgMTA0NLMqafkEmVsi3MgeKrRaqqXkH+SLHVAkzBBqs5iKPpdzGtEDFB0im 22LCppA6J748odVbsnqVLjyvyg1H60cq1fafJtX+2HCy58Sj42mBgTZ7ML/n/5Yogo6Y vLZko03TJrD3whHpmWBl9iV4Oioyqm/z9kfeswp2dMAiYBAtjg4ugzjUJ2RX+bknwSbv VZw8n0ZmrBiZ/ILn8+qN08smItOL7VlQATYz05YmGNwZpI3FatptEl3n/x1P4QY4VeeH H04Q== X-Gm-Message-State: AOAM532vDEGQXzxSJw1+X7vsZXY/u1mF9JATTomqVq9QfngsOTWova86 2d01D5TegTX68HGa88K/zJffboiDR0I= X-Google-Smtp-Source: ABdhPJx6ja5dY70puxnLAuFUzqo0LPQQ5fNT6wN3QIVajy/vSs0Dd/4mS4eZe4xj7zTbFYQqGBAgwA== X-Received: by 2002:a17:902:f544:b0:154:5ecb:eb24 with SMTP id h4-20020a170902f54400b001545ecbeb24mr9456306plf.11.1648800667985; Fri, 01 Apr 2022 01:11:07 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id hg5-20020a17090b300500b001c795eedcffsm11634790pjb.13.2022.04.01.01.11.05 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Apr 2022 01:11:07 -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 v2 3/5] KVM: X86: Boost vCPU which is in critical section Date: Fri, 1 Apr 2022 01:10:03 -0700 Message-Id: <1648800605-18074-4-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648800605-18074-1-git-send-email-wanpengli@tencent.com> References: <1648800605-18074-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 9aa05f79b743..b613cd2b822a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10377,6 +10377,28 @@ static int vcpu_run(struct kvm_vcpu *vcpu) return r; } +static bool kvm_vcpu_is_preemptible(struct kvm_vcpu *vcpu) +{ + int count; + + if (!vcpu->arch.pv_pc.preempt_count_enabled) + return false; + + if (!kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_pc.preempt_count_cache, + &count, sizeof(int))) + return !(count & ~PREEMPT_NEED_RESCHED); + + return false; +} + +bool kvm_arch_boost_candidate(struct kvm_vcpu *vcpu) +{ + if (vcpu->arch.last_guest_irq_disabled || !kvm_vcpu_is_preemptible(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 9536ffa0473b..28d9e99284f1 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1420,6 +1420,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 69c318fdff61..018a87af01a1 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3544,6 +3544,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; @@ -3579,6 +3584,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 Apr 1 08:10:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12797991 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 C890CC433F5 for ; Fri, 1 Apr 2022 08:11:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344208AbiDAIN2 (ORCPT ); Fri, 1 Apr 2022 04:13:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344281AbiDAIM7 (ORCPT ); Fri, 1 Apr 2022 04:12:59 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49391204C92; Fri, 1 Apr 2022 01:11:11 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id c2so1827945pga.10; Fri, 01 Apr 2022 01:11:11 -0700 (PDT) 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=LuKtWHDzvGHRbTbHDfM3kJ4nrniR7w02cItd/4r8DqU=; b=YdX39apgvnvZ8QQVQdWr5LlQxY8QpbVH31PhVXTAMoy91XsIiSLDTvhI7PCXp10QRP F39A+XLdemPxw9t/hrX1kk62N4zBPjQkxNM1qA0kVNpK/fL4Lud/12YNHeL5vt1IdeZO n+GfYKa5uAQrmqq9ZI0C6Xlc7kMY/DbBj/j+ts/HBhQMt6VS7lyhaPrwPgcgUDbny/YD VZbIcMuzV9HULOEwrh0ZbNlmtcW3HMDd/taNaonlgvEbCJG1Re/Fs5SOPynqFPwYt2K1 ko270Zis3TJSJEEqQk1PbC3q23n0GATDTYVdX9wmM66K/3eRY6PG6DB0BykYq9VOZ4r9 XsTA== 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=LuKtWHDzvGHRbTbHDfM3kJ4nrniR7w02cItd/4r8DqU=; b=KRSTuYEqwwdy/4UFM4Lr/n1N5K/1guiKqI9BzC1+W05TrAlGWdvpkv/bAcZH5sLq/s LBIvaHNZXXU14UEJKShC/Ofxvt2HXv4g5dsx4Q3kW7uEjjK5thUFdHKyaeSsIzyWp+IU PGsw+Q+JPn+dCSRiCkOa3iINR7iNBNFXnNGfLiegyLpNY25llWGoYl2Eb9/+IWHLkytI DhkdiugQlBmELccyEQp4qrwex2RA4q0XQ9zVHn0YI5zXUOOVFM0E1an8/E9RowKvdOoK AT9sU1OoWj4MiAFG81diNefQGnzEoCGgBw+2sadiCyVTc4OAJ936TnxolYSf7s5Cw6rg Abww== X-Gm-Message-State: AOAM531cUiXIzmEVrFAmmbSKogN60lkE/7nfevt2hVDqtFM5vP/e2fae hFgg5ADD7qxxkrNkTCPMsH7Ah81ubvw= X-Google-Smtp-Source: ABdhPJwvsw4GLwJVMFco4Y6dng/C9MrWFoCGIYj0lk98wlFSIAaeN8N8I+LQnuoKvVQb9K/faEfR1A== X-Received: by 2002:a63:1c14:0:b0:36b:28ef:f8ce with SMTP id c20-20020a631c14000000b0036b28eff8cemr14111944pgc.96.1648800670613; Fri, 01 Apr 2022 01:11:10 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id hg5-20020a17090b300500b001c795eedcffsm11634790pjb.13.2022.04.01.01.11.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Apr 2022 01:11:10 -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 v2 4/5] x86/kvm: Add MSR_KVM_PREEMPT_COUNT guest support Date: Fri, 1 Apr 2022 01:10:04 -0700 Message-Id: <1648800605-18074-5-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648800605-18074-1-git-send-email-wanpengli@tencent.com> References: <1648800605-18074-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 79e0b8d63ffa..5b900334de6e 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 Apr 1 08:10:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12797992 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 6366DC433EF for ; Fri, 1 Apr 2022 08:11:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344250AbiDAINb (ORCPT ); Fri, 1 Apr 2022 04:13:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344293AbiDAINC (ORCPT ); Fri, 1 Apr 2022 04:13:02 -0400 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 CD4A920A97A; Fri, 1 Apr 2022 01:11:13 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id y16so1751634pju.4; Fri, 01 Apr 2022 01:11:13 -0700 (PDT) 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=TZ6Va3/+y3V6v2Y3p92U6OT9/1I7oLnm/gWi+nTj8w4=; b=Db0dHl5Nb4Z7FC3ZIPNhial2UQcrkej6YsUpFfjjObjtyj+y2CUtgItUo6IGGRRRpN hX5M6qqZfzzk395bR0i7Xagk4c4yDWx9ZQDGDOizXKd2ykooJxz9L9ZEB4Kr0N2FRCRW XsQzJuohuSgmrzynOyUKMX+RvBp8TzJn5Lo2SX3OjyBF+zwzqqlgg2SpgKUN/OJ28d9Z KbNBKMSbetRcrB9Uh3/3QhvPFvXc8N4alGGDgcBPi1syEcnypaCWX1TzQxNWHJSe25B8 gO0bkC47sN/0qjyDO43MIEdcGVvKIlD+rH0TOewjr8Vy5qD3t2zk/9UmfqatBx/FYlZu JJRw== 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=TZ6Va3/+y3V6v2Y3p92U6OT9/1I7oLnm/gWi+nTj8w4=; b=Bm/XDuy3BaEiLG3VnvRgHyATyV0hxhUTIJafe2p4xGErAVmWBLmY8I8ZLCnRzUzVkT fRXk27oCWRxXwhKLp9yOKCQY/IlZ0xEbmYJOuGz/DJf9HLbaPOQmZzN6L5C1LglFLOga 4k0ptM8+L3cpbbmAu//17a231fNIAscNnt6wRZDt7FPxVarqiTH1H1vDyFRf/N609UZs TAnFIE90a+HfwLATIF8GtIjWiukB2GiV5LYnQLwGw/+yOCPK4gccdZoNYJD51zh/D/OM LuMDy6xD1XHbS1BwEYqYJYMraGx9TCG/ZXRYoLbhDK+PzF/dckf8NeUISCS0E5FXkZR/ wfZQ== X-Gm-Message-State: AOAM533BJwqOuXCsAz/kHKm//KKZs4cgTt4Xm2IIv2QQp3BbYMDUKRWm T2am/6xLMKKXB4OkIZWF96SuYXf2PXk= X-Google-Smtp-Source: ABdhPJzh3y4A3uDHCtvJ4AZuJ9uWAuoYQqRzuwbs9cG6/RGOWF6sxK8NxMs/g/TCC1lgSGehcyA0Gw== X-Received: by 2002:a17:902:e8d2:b0:156:32bf:b1b9 with SMTP id v18-20020a170902e8d200b0015632bfb1b9mr9162389plg.107.1648800673182; Fri, 01 Apr 2022 01:11:13 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id hg5-20020a17090b300500b001c795eedcffsm11634790pjb.13.2022.04.01.01.11.10 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 01 Apr 2022 01:11:12 -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 v2 5/5] KVM: X86: Expose PREEMT_COUNT CPUID feature bit to guest Date: Fri, 1 Apr 2022 01:10:05 -0700 Message-Id: <1648800605-18074-6-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648800605-18074-1-git-send-email-wanpengli@tencent.com> References: <1648800605-18074-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 a00cd97b2623..c514c0419593 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -1072,7 +1072,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);