From patchwork Fri Mar 25 13:58:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12791616 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 79EB4C433F5 for ; Fri, 25 Mar 2022 13:59:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358796AbiCYOBH (ORCPT ); Fri, 25 Mar 2022 10:01:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352923AbiCYOBB (ORCPT ); Fri, 25 Mar 2022 10:01:01 -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 DBFEC5E75D; Fri, 25 Mar 2022 06:59:27 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id n7-20020a17090aab8700b001c6aa871860so8413846pjq.2; Fri, 25 Mar 2022 06:59:27 -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=gDqiN5pf4YDkXe9dP0rfveBOw3P+FW2+px95EQefoNY=; b=kkYNqJpY8u9gDoA9DnmCRX7/62AGWobuqKlzAlcYN+uWXouh/BeH2tZLvpP1Pe5x8N odUpBRTSCOad2wiq7N7r6BVHd7Tq3kfkf1E02MNx4v1o6iot5eEohU3h2MArIvqoLUNO WEYzgPqODwFOQ94RBABFjlSNGNeRGTu6VPYltarL+8TlZXyC74qo2nKyoUnVDwakPW1v bgK6qRtDpIXuMhwsEByfEpza4BqRiySjBynWM/jG0csg6eMtKEosH73rYuew+iXlXyw8 Dkt8JcgmXT3tshkJhLi3xirQ6/0pQ6HDHktRByei3kU53y6tGeytGASCqa3e86ICPyt5 nkCg== 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=cgy9X8k9rGAq5k/iZsEGBSrlG+hsmnsGziCqJUfCX9jYhDdiL8pTynxpV+UWsFk4Cp jLfF0fFHu69qtJSuMeDtCgfvHJFZBwKPqv/Mj7eko2R7sbYS2XrD+jbUDRe4/qrhmmGP a4oIlASfLVYcbpot+rxWjGkpOaseavxmdBQWzfw3HGqzFxSycQ+b8yiikcSapBQcEZkm eFVXhDxJMpe2OY2QFePhwt3MRN17bclRENU/p3R2yfH7251fNs31Q4rIf5doeMIMcdbu L+uR+1Za1rGWstnIVwJ4Rm3XvrR6MDfo/EtTW9GQfKXvPifmTycexs1Yzwpe2Yv7Hk28 yYeA== X-Gm-Message-State: AOAM5324bCPH6u99iSnT8HmKOWienGgMvC/LFWcxQ3UqjU1cReOJComO eBr9aiEGFNwG4X+iHTPv9rMj3/EJEO0= X-Google-Smtp-Source: ABdhPJz7rYWP7B/KvxYgG8AMv5MkxbfY9Bt+GYvPawceO5AABPa4vilU0dOYqMC6kplGuZmI2PtUzg== X-Received: by 2002:a17:902:7888:b0:148:adf2:9725 with SMTP id q8-20020a170902788800b00148adf29725mr11724634pll.136.1648216767272; Fri, 25 Mar 2022 06:59:27 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id lw4-20020a17090b180400b001c7327d09c3sm14470875pjb.53.2022.03.25.06.59.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Mar 2022 06:59:27 -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 RESEND 1/5] KVM: X86: Add MSR_KVM_PREEMPT_COUNT support Date: Fri, 25 Mar 2022 06:58:25 -0700 Message-Id: <1648216709-44755-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648216709-44755-1-git-send-email-wanpengli@tencent.com> References: <1648216709-44755-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 25 13:58:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12791617 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 C99C7C433EF for ; Fri, 25 Mar 2022 13:59:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358809AbiCYOBL (ORCPT ); Fri, 25 Mar 2022 10:01:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243565AbiCYOBE (ORCPT ); Fri, 25 Mar 2022 10:01:04 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AE195F274; Fri, 25 Mar 2022 06:59:30 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id w8so8093717pll.10; Fri, 25 Mar 2022 06:59:30 -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=bg55DIKCupebm5f4NqZ1g4YC1+NWU4JZUPE3B9his68=; b=AaqdW4yCybWq9oeRTyuwcMjcgay7UvUWcDwjQS7vzblvc3BBYtlsBPKtpSP97wC0YG KEhu667dydDhzy1pNmdvCsCZDomlYCNwSA1DMezLRj7Pq/65P2q5b75eJkskFCgHupcC mE0mjOYtaU5jCpJakMmHqigh1PXXQIM02iVieoZr375Nqrel6RZyTZ0LyGIP91IH11pN LAmGzWNZp5S2yDgG/Pppsnv5lBqeFS/RGx3EPNoEysuNPNztKCdN4BnhmlkfXg/jQ1bx fJS8u/HEGtb60VDNx4Sjr7lKCxXsQZ7BnWAEOg6RCdqmwKygB6DqUSZDkZc/KsB5fL6h 1F9A== 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=Bz6TXbYMmYF3htiYKJSuztjQkdkVbQTnfLCstFUYuLRXQmFwM4kzwc5aBTFo/QEkos xkFC/Rr9JsSM0K+3qyquJh0oktXzAUwKa1Jvljox3tcSS0y472BUg/X8FFSBd/VS7UmZ 2uz8EWwaPIkNLPHKmW1hx97hST0otktyfiwm5Sd8+2bLJIdTr6xLDICNtOHRtbSmJvKR BB/MjkNy5syHOADEgjqRBBcCFMRrC8nagRKT547TIIS2z5Mw+yE5oXxwcYavKMw3phng gu4dOBPkU++A4NNhU6YXHoY3tR7IgABMP1MPMFqx0LS2jYLZw4ga4rfk24kJLOhrqqTZ tnLQ== X-Gm-Message-State: AOAM531zKRTFE0oAEFCraTQWXl4aUzlN28ooHDVwQ9ycabaVaP2+yzJT c8hNLdk8A0vcXK6p+i4NvXU+iYlqxms= X-Google-Smtp-Source: ABdhPJz5BQba3hY0HQZbweCxsAW/gWaHhHMfuPXeOppEO/6EwRv27Kh+wlMeGZnn9C+jU7MoD//2qg== X-Received: by 2002:a17:902:d486:b0:154:50c6:a0b5 with SMTP id c6-20020a170902d48600b0015450c6a0b5mr11622659plg.108.1648216769928; Fri, 25 Mar 2022 06:59:29 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id lw4-20020a17090b180400b001c7327d09c3sm14470875pjb.53.2022.03.25.06.59.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Mar 2022 06:59:29 -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 RESEND 2/5] KVM: X86: Add guest interrupt disable state support Date: Fri, 25 Mar 2022 06:58:26 -0700 Message-Id: <1648216709-44755-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648216709-44755-1-git-send-email-wanpengli@tencent.com> References: <1648216709-44755-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 25 13:58:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12791618 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 12E73C433F5 for ; Fri, 25 Mar 2022 13:59:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358970AbiCYOBO (ORCPT ); Fri, 25 Mar 2022 10:01:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358899AbiCYOBI (ORCPT ); Fri, 25 Mar 2022 10:01:08 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34B88606CA; Fri, 25 Mar 2022 06:59:33 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id g9-20020a17090ace8900b001c7cce3c0aeso3597732pju.2; Fri, 25 Mar 2022 06:59:33 -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=zFHRAV31Ch1tp+2iNpA7ALNISwTJjRRUfUkhPuq13Zo=; b=Cu4xcWiyozPIH5KA4agN/rdwpv5i8VxFgBjItP6j8wz9TTGY2XuJfrGQw+GTMtCW9T /ORB7tR1sdl/Q2caWDHAQLD00WcfLj/Q606h4CIpEdO2KXSfZ9epi8QKsbG4RrC27HCW 3bcl81VVQeIQU2umHw3Ml2DfmM9cksdbbAXFKXmAVCYkOjZUbl8WR/SoKc3xHU0i/42Z qNfFvvi8PJNXfrH9OZYWGhsUPsdWgKVzeXvo+qiDoC1NHkGRcUiMWqov+A8N+zmYPMvN pReCRc0a1pUtGV3hu6WFntKrJ/dt5aL5paVw+ZjZUcCKD5WEcyr7E4+H3R6phBGZCNmb NX4A== 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=zFHRAV31Ch1tp+2iNpA7ALNISwTJjRRUfUkhPuq13Zo=; b=YWRaIob8XCMvb8TyPiyEwR/IQPB5SnxSoM/QJ9EgglA5mclhAL20D/hD3ab5iV4Mkt dM13p00ITYXkLmgoTM38h4wsfcwO4x+PzXLL1powV6P7mA+RjIQ5i+frQ/O5li1v9A+S XmA44ZLpILVgUhYGAmTzH7R3Zjkfpw6hWVdbaDNqss1L8JzgaGXXgTpmST2XKnG8BHuD bIYQepicZHtSq8o5uMkZC7K6/KDUPab1Xy4cg2uFR6GmTFjJII2S7Os/IC6O32OGG79q gugVlmciZnk2LHNvFXlAjYiWMCHSx0+O2H9Dx+8+4fYg+njkMuVltBe4VKElur3YlPjM wm+g== X-Gm-Message-State: AOAM5305/nHeDuhksIINso8nrjFJjjEf2KPxHPu1H3IBnNKp98wjwfei CFE86V35fsucKpkrHwaWKYlWWFsVusM= X-Google-Smtp-Source: ABdhPJwLEuM4gsxnbwwEPABdOHYRI14l6NbAdRqXSGJjqYX+3gfuV43mgriZl0bNGYiGc+e7x+iErg== X-Received: by 2002:a17:90b:1809:b0:1c7:2032:4b34 with SMTP id lw9-20020a17090b180900b001c720324b34mr25034960pjb.4.1648216772580; Fri, 25 Mar 2022 06:59:32 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id lw4-20020a17090b180400b001c7327d09c3sm14470875pjb.53.2022.03.25.06.59.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Mar 2022 06:59:32 -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 RESEND 3/5] KVM: X86: Boost vCPU which is in critical section Date: Fri, 25 Mar 2022 06:58:27 -0700 Message-Id: <1648216709-44755-4-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648216709-44755-1-git-send-email-wanpengli@tencent.com> References: <1648216709-44755-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 25 13:58:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12791619 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 6E634C433EF for ; Fri, 25 Mar 2022 13:59:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359028AbiCYOBS (ORCPT ); Fri, 25 Mar 2022 10:01:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358938AbiCYOBN (ORCPT ); Fri, 25 Mar 2022 10:01:13 -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 06A9360A92; Fri, 25 Mar 2022 06:59:35 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id m18so3214506plx.3; Fri, 25 Mar 2022 06:59:35 -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=4jbCwzr0mct8OKs/Lgqse4ZkWggPrV/4VXWHupxqHk0=; b=nT728+6kR20X1SoIl0V5jnB6v7lr0+IpMj+2Vafc9rc9beExEQmjLTm2p2TOZLz2rN VfynJ8uduC92IgQRDDXKz/VEJvV4l2XdlT1yKY7wOAjqJQcsXG7iuyruqR/wOpQGAsWo pjPvTowRroLFlAwJab3PdPh3/3TplhmVsRinWI3HXC2hBRNC0SVDAc47xfj17McKbNPW bXB3QzVuCLiyXA92OtOv2/lh7VxfeEDuVGshLX8LhatAr+lEyXIByScvhSnMFYduKAr1 LvsMoIO5xvdO8X0BTnDrDXMrmaS76+bW51+kwbvFDCCQkvEDbKng68ovq4G5ueo8VW4c wZLw== 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=jj1j6yKEs+dk5k8FXd9ddVXbz9DfaWWqGV+5ggJOXig+2KIRnohCxRHgxn5Y4pdqc6 dJZYjKo28dqJZkTpImpyGl0m8u0mSAy/S14CNSocvFj2O+iOPdVbth/MUKJFQke2G4IT WpogTS0HeJbwIpNXRPFUjKY0DIQDPtr5xnFmQSLiv1OSWeiRsMWjlgeKSYzx84Qq/gbQ +s0FxDBa/4cpqZQqll0TOx9XM7iLq27U0zIcM9a3aaCg76+PjF9fr1YbbuR3HnKib/nZ k3jOEkaaBa10ZvZF5sEkiXQ+EZAp+0z+nJy/oyKnsy65H8GQsIte74/fpUyZqBZvNEC7 R9/w== X-Gm-Message-State: AOAM533hMVg9f024QL3ppAJWcydy4pleHivKxZvMFxs6z1HLXxxM6jqK P4N3wJrqpMgdKW8eLS0IpR2rwA7Uml8= X-Google-Smtp-Source: ABdhPJxjmkgO4hGCqv5qcydxjfqbeQere/uio1LGQxcMv22iZukZrTDZQvjrF16csQcLHjItIV2f8w== X-Received: by 2002:a17:90b:1d08:b0:1c7:5523:6a1f with SMTP id on8-20020a17090b1d0800b001c755236a1fmr25263357pjb.225.1648216775134; Fri, 25 Mar 2022 06:59:35 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id lw4-20020a17090b180400b001c7327d09c3sm14470875pjb.53.2022.03.25.06.59.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Mar 2022 06:59: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 RESEND 4/5] x86/kvm: Add MSR_KVM_PREEMPT_COUNT guest support Date: Fri, 25 Mar 2022 06:58:28 -0700 Message-Id: <1648216709-44755-5-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648216709-44755-1-git-send-email-wanpengli@tencent.com> References: <1648216709-44755-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 25 13:58:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12791620 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 EFAC0C433F5 for ; Fri, 25 Mar 2022 13:59:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353432AbiCYOBU (ORCPT ); Fri, 25 Mar 2022 10:01:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358940AbiCYOBN (ORCPT ); Fri, 25 Mar 2022 10:01:13 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D3ED5F253; Fri, 25 Mar 2022 06:59:38 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id j13so8095732plj.8; Fri, 25 Mar 2022 06:59:38 -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=EV0wmNwRVxX8NQcoop+BEUS5js89FYD4nmxPHK49pWQ=; b=MaTdW3To90ijPhW/yZNfIQWH8ozmuI4LzhKZZvBDJlsmUM8Q8KOcBH9ehtDzmtC3YW eBh8n4j26HCJ6UIWcx/uCWY44xhL2PQ0sfAA1+jRChFcFazSPdkTIVADj9Dv0lsOLPG8 UI6O41pQBpGMsUUKkhL0jM5O/hLzOmYIEJMDkkVr9krBTgPBzhVDEh6+yZt7JQooPEVz /pAkUQoqYT62KUT1s8stfPcwAp1cuKUBU4np52nZnrjZQZwV4kFEl9zVyssIJ+qwZFc+ sgY7F2ftMg5zXJ7ohNd1pPwnjmwomWxeC8VnAE0powpbiCvktOMAr80vds0yyEliKNyN 3zww== 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=YELN5Lrk0E0oFZCpvqtW5YYzQesUqFNMWJwSBo8q2O+JfBgWeLOmPVJWTVU34EQK7A 6AfDYd1nZo6wOIZHNyrO+CkycXq4xM/vep5R99obENEN9fIZE9AyvKN+mkcmV5sq+mYL DasVZAjawP5iWN8kNlB0xXW/Bm5fl0fUya2vB7Gdh0P1ekBt8aKPmoF4++rG31bcr7z6 5gRAbPbpU/GxWxkYh2CDYuEkEJrrHIk/hnZr8PTDlYovaD6xIKxUcDXU20e1mCBkPby1 2Ci/GY4+57P/Ze4zkGg5jcCyed+fVICsQIbtmiV9kvDHPPkD9OocWpFzi4Jwug/QQiXE VHmA== X-Gm-Message-State: AOAM530lb9ID88TBVF4W42yiIU1Xk27QaYX6O0wH2xqqqQBDV8wUnf0x Xh8u5K6UAlnFOBCY2On2e8Ga7rqAQn0= X-Google-Smtp-Source: ABdhPJzz7K+/5uo4/3em0DsU11ggYGjYid3uyziE4s7FG3CHnGx59o2D7meiWG5Ps8LDY3KfTEYf5g== X-Received: by 2002:a17:90b:1e43:b0:1c7:46bf:ba29 with SMTP id pi3-20020a17090b1e4300b001c746bfba29mr12861850pjb.100.1648216777881; Fri, 25 Mar 2022 06:59:37 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.113]) by smtp.googlemail.com with ESMTPSA id lw4-20020a17090b180400b001c7327d09c3sm14470875pjb.53.2022.03.25.06.59.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Mar 2022 06:59:37 -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 RESEND 5/5] KVM: X86: Expose PREEMT_COUNT CPUID feature bit to guest Date: Fri, 25 Mar 2022 06:58:29 -0700 Message-Id: <1648216709-44755-6-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1648216709-44755-1-git-send-email-wanpengli@tencent.com> References: <1648216709-44755-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);