From patchwork Fri Apr 9 04:18:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12193003 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1838C433ED for ; Fri, 9 Apr 2021 04:18:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B7ED561026 for ; Fri, 9 Apr 2021 04:18:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230118AbhDIESy (ORCPT ); Fri, 9 Apr 2021 00:18:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229526AbhDIESy (ORCPT ); Fri, 9 Apr 2021 00:18:54 -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 783E3C061760; Thu, 8 Apr 2021 21:18:42 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id i4so2259215pjk.1; Thu, 08 Apr 2021 21:18:42 -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; bh=Gd+gXcz6Jd6OEHUz9h21bPRreafNfkInMEF4YPsbOpk=; b=tefdWYpWO2XnAMMkeY/iwlwssVIS8QxEFUzgz5cgjuzgTa71RZuaRnkw6Ut8ArDbyt vDNQsQgqr3DIjUSZO6EfCN7beVAnJVrK9U+hlpT6YjmgOcs84zwx0bdl64ZbUg/9Dc7o Z733MXUGKLVBoLTrLPzWC5ZcJaPAw3fjuEdR7MrVOMS6t3oDGNH5vu9kcntpfYSZAFph RA/3iUXqQse83yjjcQ684JF+a0fhcRKpF7zMTgtUIqVQSobgttedo1xpusc1zPgXbfNA VBD4ecS7hAqyFryS25kHF0+mN8gbWKf9m4KtgMEZRXkrV+xr8ndqzxA/NYMfEuNbrLyh cWnA== 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; bh=Gd+gXcz6Jd6OEHUz9h21bPRreafNfkInMEF4YPsbOpk=; b=Mb2s8ABvf6IUYFbVa5uq/3+rjIcsA2+1Jl79Go+6MrH6641uBdbjK/IwAXLd+xA83v nG1HLhEudgQEuXdrlWb0UUgYY+TTwUzfZcOdmx+o/y0FOvKQqfWf5GxYv1yRWh7t7gIu RjPwT3GBBUSbgcXFabDb3K5K2HWo2FeJGmY5JwJdbwNnJOlLwxa1VrYtzDp/SZLqNVYw Q5asMZejogj9RDJLVYQ48Zo3E3uDEI97uzQ9Zw+qG/qYumyfCoqIcTflF7pN/Gj04CNK kZoPxNLM0gB3fjPbBy91St3eVdTRKCW6x36Lgo9o4Imsp1o/grlq6zzJ9NKEMFoYbHed uzAg== X-Gm-Message-State: AOAM531NDu+g+X63yWv3eq38AmOYyQYoSVXm9oP7TDD6Ia5InH8NbVf/ xUcVEA18tanPK3CSG8QooYWA97qXjuo= X-Google-Smtp-Source: ABdhPJyYC5We5GOGs227qlbjCUawiRAdPZfMPTKKs2CTtjRaW/csMkfeTbEps/Ol1v0ogr1/E98Fpg== X-Received: by 2002:a17:90a:5889:: with SMTP id j9mr12376248pji.69.1617941921712; Thu, 08 Apr 2021 21:18:41 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id gw24sm765553pjb.42.2021.04.08.21.18.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Apr 2021 21:18:41 -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/3] x86/kvm: Don't bother __pv_cpu_mask when !CONFIG_SMP Date: Fri, 9 Apr 2021 12:18:29 +0800 Message-Id: <1617941911-5338-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Enable PV TLB shootdown when !CONFIG_SMP doesn't make sense. Let's move it inside CONFIG_SMP. In addition, we can avoid define and alloc __pv_cpu_mask when !CONFIG_SMP and get rid of 'alloc' variable in kvm_alloc_cpumask. Signed-off-by: Wanpeng Li --- v1 -> v2: * shuffle things around a bit more arch/x86/kernel/kvm.c | 118 +++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 63 deletions(-) diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 5e78e01..224a7a1 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -451,6 +451,10 @@ static void __init sev_map_percpu_data(void) } } +#ifdef CONFIG_SMP + +static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask); + static bool pv_tlb_flush_supported(void) { return (kvm_para_has_feature(KVM_FEATURE_PV_TLB_FLUSH) && @@ -458,10 +462,6 @@ static bool pv_tlb_flush_supported(void) kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)); } -static DEFINE_PER_CPU(cpumask_var_t, __pv_cpu_mask); - -#ifdef CONFIG_SMP - static bool pv_ipi_supported(void) { return kvm_para_has_feature(KVM_FEATURE_PV_SEND_IPI); @@ -574,6 +574,49 @@ static void kvm_smp_send_call_func_ipi(const struct cpumask *mask) } } +static void kvm_flush_tlb_others(const struct cpumask *cpumask, + const struct flush_tlb_info *info) +{ + u8 state; + int cpu; + struct kvm_steal_time *src; + struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask); + + cpumask_copy(flushmask, cpumask); + /* + * We have to call flush only on online vCPUs. And + * queue flush_on_enter for pre-empted vCPUs + */ + for_each_cpu(cpu, flushmask) { + src = &per_cpu(steal_time, cpu); + state = READ_ONCE(src->preempted); + if ((state & KVM_VCPU_PREEMPTED)) { + if (try_cmpxchg(&src->preempted, &state, + state | KVM_VCPU_FLUSH_TLB)) + __cpumask_clear_cpu(cpu, flushmask); + } + } + + native_flush_tlb_others(flushmask, info); +} + +static __init int kvm_alloc_cpumask(void) +{ + int cpu; + + if (!kvm_para_available() || nopv) + return 0; + + if (pv_tlb_flush_supported() || pv_ipi_supported()) + for_each_possible_cpu(cpu) { + zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu), + GFP_KERNEL, cpu_to_node(cpu)); + } + + return 0; +} +arch_initcall(kvm_alloc_cpumask); + static void __init kvm_smp_prepare_boot_cpu(void) { /* @@ -611,33 +654,8 @@ static int kvm_cpu_down_prepare(unsigned int cpu) local_irq_enable(); return 0; } -#endif - -static void kvm_flush_tlb_others(const struct cpumask *cpumask, - const struct flush_tlb_info *info) -{ - u8 state; - int cpu; - struct kvm_steal_time *src; - struct cpumask *flushmask = this_cpu_cpumask_var_ptr(__pv_cpu_mask); - - cpumask_copy(flushmask, cpumask); - /* - * We have to call flush only on online vCPUs. And - * queue flush_on_enter for pre-empted vCPUs - */ - for_each_cpu(cpu, flushmask) { - src = &per_cpu(steal_time, cpu); - state = READ_ONCE(src->preempted); - if ((state & KVM_VCPU_PREEMPTED)) { - if (try_cmpxchg(&src->preempted, &state, - state | KVM_VCPU_FLUSH_TLB)) - __cpumask_clear_cpu(cpu, flushmask); - } - } - native_flush_tlb_others(flushmask, info); -} +#endif static void __init kvm_guest_init(void) { @@ -653,12 +671,6 @@ static void __init kvm_guest_init(void) pv_ops.time.steal_clock = kvm_steal_clock; } - if (pv_tlb_flush_supported()) { - pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others; - pv_ops.mmu.tlb_remove_table = tlb_remove_table; - pr_info("KVM setup pv remote TLB flush\n"); - } - if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) apic_set_eoi_write(kvm_guest_apic_eoi_write); @@ -668,6 +680,12 @@ static void __init kvm_guest_init(void) } #ifdef CONFIG_SMP + if (pv_tlb_flush_supported()) { + pv_ops.mmu.flush_tlb_others = kvm_flush_tlb_others; + pv_ops.mmu.tlb_remove_table = tlb_remove_table; + pr_info("KVM setup pv remote TLB flush\n"); + } + smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu; if (pv_sched_yield_supported()) { smp_ops.send_call_func_ipi = kvm_smp_send_call_func_ipi; @@ -734,7 +752,7 @@ static uint32_t __init kvm_detect(void) static void __init kvm_apic_init(void) { -#if defined(CONFIG_SMP) +#ifdef CONFIG_SMP if (pv_ipi_supported()) kvm_setup_pv_ipi(); #endif @@ -794,32 +812,6 @@ static __init int activate_jump_labels(void) } arch_initcall(activate_jump_labels); -static __init int kvm_alloc_cpumask(void) -{ - int cpu; - bool alloc = false; - - if (!kvm_para_available() || nopv) - return 0; - - if (pv_tlb_flush_supported()) - alloc = true; - -#if defined(CONFIG_SMP) - if (pv_ipi_supported()) - alloc = true; -#endif - - if (alloc) - for_each_possible_cpu(cpu) { - zalloc_cpumask_var_node(per_cpu_ptr(&__pv_cpu_mask, cpu), - GFP_KERNEL, cpu_to_node(cpu)); - } - - return 0; -} -arch_initcall(kvm_alloc_cpumask); - #ifdef CONFIG_PARAVIRT_SPINLOCKS /* Kick a cpu by its apicid. Used to wake up a halted vcpu */ From patchwork Fri Apr 9 04:18:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12193005 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F40C3C433ED for ; Fri, 9 Apr 2021 04:18:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CC4B261177 for ; Fri, 9 Apr 2021 04:18:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231410AbhDIES6 (ORCPT ); Fri, 9 Apr 2021 00:18:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229526AbhDIES4 (ORCPT ); Fri, 9 Apr 2021 00:18:56 -0400 Received: from mail-pg1-x52e.google.com (mail-pg1-x52e.google.com [IPv6:2607:f8b0:4864:20::52e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D6F40C061761; Thu, 8 Apr 2021 21:18:44 -0700 (PDT) Received: by mail-pg1-x52e.google.com with SMTP id y32so2908158pga.11; Thu, 08 Apr 2021 21:18:44 -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=xaENlWbODyEto5XlOvRuMJolo5sGz0JfL3nPFrLfnfo=; b=WFOqixJdGiNjr07rL6it5V1WYpE09f3ANsOibIqH+z3m2ysCf1BWg5cRIEFrt+d8Km HEOq1TOYqfid8x+kcU6mSW5JRzTqFxJ+Wt6pzYHM1N1bHL08rf2f8yzGDbu5PZxkvTMH Rpv8xZHGxL6kvCCsjBzHPfYLFwGY1WAmKesKAdxSuTK+2xJocwInYWGLVrGAsyhMBn/6 gFsZ21i9XG+d8o5OPxJAtyhmk5zj9HFcKmuVSZNnV6ge1EgN80BNP+p0+Cwq97J4aOl4 kTOU2X2Jznp/xTkpcoxXjw9HHjG3nwvLC/a6Lo6sioTQoYYDCobo3BL2VyorIMohoeJL iSpA== 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=xaENlWbODyEto5XlOvRuMJolo5sGz0JfL3nPFrLfnfo=; b=LavGPdQIlOtE/aDYODL4nsZIE9RO9lVVtKZH4df84PUjBdKHTNGH7eUAR/E4ieGR0y h9Dn6SJYr6/TEYqU8l3hh2zJXhMLzyblAoqd1bAEIBSqvXjTKd+B4DiA7C9MdZRh4a+y 4p9gDu+FgT4tavBIhiCSVZs4cM6GfXYOThbuMlTg2YRL+V93XTFr9Bq4MHECVtjkFn1I KbKQpox73oMcxnVwK7nhzaIs/TqbYGxkQL0xIH6c2tvDo/MoVRwaQKP0DZaSqblx7QqD Hh1pkzkCNFjBgrPI4Tre+4XndBf/2X8uWxmGVtKBSACFhNEdM8e1qaS5HveQBWSgPrRr vIjQ== X-Gm-Message-State: AOAM533aeW8pdra8sY6yvh2Cgn7qtWHuMk8PpLu537DvN31SqpueM9If nj4wIM6+8mkBKRwKrA8lesA8Jstlzn0= X-Google-Smtp-Source: ABdhPJyasOZHxTZMMtmdH6OMq5eDliacx+llgkwbI4zN3cBwXe8Axv8eKrCkRfauuIP4Wo6PBCdy4g== X-Received: by 2002:a63:d755:: with SMTP id w21mr11514874pgi.400.1617941924255; Thu, 08 Apr 2021 21:18:44 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id gw24sm765553pjb.42.2021.04.08.21.18.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Apr 2021 21:18:43 -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/3] KVM: X86: Count attempted/successful directed yield Date: Fri, 9 Apr 2021 12:18:30 +0800 Message-Id: <1617941911-5338-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1617941911-5338-1-git-send-email-wanpengli@tencent.com> References: <1617941911-5338-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li To analyze some performance issues with lock contention and scheduling, it is nice to know when directed yield are successful or failing. Signed-off-by: Wanpeng Li --- v1 -> v2: * rename new vcpu stat * account success instead of ignore arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/x86.c | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 44f8930..5af7411 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1126,6 +1126,8 @@ struct kvm_vcpu_stat { u64 halt_poll_success_ns; u64 halt_poll_fail_ns; u64 nested_run; + u64 directed_yield_attempted; + u64 directed_yield_successful; }; struct x86_instruction_info; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 16fb395..f08e9b4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -246,6 +246,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] = { VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns), VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns), VCPU_STAT("nested_run", nested_run), + VCPU_STAT("directed_yield_attempted", directed_yield_attempted), + VCPU_STAT("directed_yield_successful", directed_yield_successful), VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped), VM_STAT("mmu_pte_write", mmu_pte_write), VM_STAT("mmu_pde_zapped", mmu_pde_zapped), @@ -8211,21 +8213,31 @@ void kvm_apicv_init(struct kvm *kvm, bool enable) } EXPORT_SYMBOL_GPL(kvm_apicv_init); -static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id) +static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id) { struct kvm_vcpu *target = NULL; struct kvm_apic_map *map; + vcpu->stat.directed_yield_attempted++; + rcu_read_lock(); - map = rcu_dereference(kvm->arch.apic_map); + map = rcu_dereference(vcpu->kvm->arch.apic_map); if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) target = map->phys_map[dest_id]->vcpu; rcu_read_unlock(); - if (target && READ_ONCE(target->ready)) - kvm_vcpu_yield_to(target); + if (!target || !READ_ONCE(target->ready)) + goto no_yield; + + if (kvm_vcpu_yield_to(target) <= 0) + goto no_yield; + + vcpu->stat.directed_yield_successful++; + +no_yield: + return; } int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) @@ -8272,7 +8284,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) break; kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1); - kvm_sched_yield(vcpu->kvm, a1); + kvm_sched_yield(vcpu, a1); ret = 0; break; #ifdef CONFIG_X86_64 @@ -8290,7 +8302,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD)) break; - kvm_sched_yield(vcpu->kvm, a0); + kvm_sched_yield(vcpu, a0); ret = 0; break; default: From patchwork Fri Apr 9 04:18:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12193007 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DC95FC433ED for ; Fri, 9 Apr 2021 04:18:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A6CE961168 for ; Fri, 9 Apr 2021 04:18:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231496AbhDIETB (ORCPT ); Fri, 9 Apr 2021 00:19:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231452AbhDIES7 (ORCPT ); Fri, 9 Apr 2021 00:18:59 -0400 Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 69170C061761; Thu, 8 Apr 2021 21:18:47 -0700 (PDT) Received: by mail-pg1-x534.google.com with SMTP id l76so2926491pga.6; Thu, 08 Apr 2021 21:18:47 -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=MsqyvVn7YBppCO1Fy05Gz6niO5R1x3MO0z1PGcRbBxY=; b=EP/Sgod+jeOnf4VL9JfZDQ9Pr5uhtieaqoD05W2JaPAwKkIeH2TSS2/ICzmk11ixoJ Mp7kUIvVnHkdFGY2s1YFBw9X2KXTXtXgD156TFWRYtw3vxwGmxv+wNuNV2fMVqyFE7dL UgeE3duktk5Pv3DY5JZMIK/QTIX07PDZm4y1yFfYpQys12QZeCwJy2W08DYWxJnoR4wc +Hz/OSt+BTrwoz3T3MhvGKXiXBmkcwbfcbsixw8vROZ1hnvNzq+gJBmIxqMtf15Cr/Mp RXHo/zZ94Sw6SdFtkXiss3N8+rJajCGzwmTh9LtPav5y2nkAM9M5degZ9AS9Xy3Ws9SZ O6qQ== 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=MsqyvVn7YBppCO1Fy05Gz6niO5R1x3MO0z1PGcRbBxY=; b=bu7TNJ+G2YCK5jjOiM9mFo6FCyO56Q/K7O8r/UpWEyH1B33WIO4YbXQLtagqohKRDp j2yuFnu4F4WG+1uFBbn4E6os+IMxTkynF5zQIX+0AZjaoPIkhd5e/M4XGs3F33s0ZoUo MK9fDv14F01P/JkHMrDYdIUm3WhX3qd9b7dKyqcTzKrKyn6Yqf4GP/nWqRgZ5gK/34K5 0+Z6zk/YDRQKZxzMzYbC9+JcKGKfgf1cYV0lzDAY8nUKoNPCI7MeYEwHeMzcjxmlw/w9 g1sI194nt0JmU5E3tAPMmxxiwe4zMFVs7riBQYVHvNRDq85DaFwToOUcMXd9So5CDJ41 8scw== X-Gm-Message-State: AOAM5323cgIfXPwd5+CEMEVXaCDaA43e7l8XfXYpI3ZZ6r9eW31ZFFpA JWPZcz+u853CGGgMPsmbEmT5A7DvuwY= X-Google-Smtp-Source: ABdhPJw9hAF280dZWKgB4cI3JFgI/AtnKjbHPUMqQZF6H88Bfz7UmghVe0rFUZ/XIYFv5eyzIpnFVw== X-Received: by 2002:a63:1a11:: with SMTP id a17mr10968569pga.371.1617941926840; Thu, 08 Apr 2021 21:18:46 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id gw24sm765553pjb.42.2021.04.08.21.18.44 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Apr 2021 21:18:46 -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/3] KVM: X86: Do not yield to self Date: Fri, 9 Apr 2021 12:18:31 +0800 Message-Id: <1617941911-5338-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1617941911-5338-1-git-send-email-wanpengli@tencent.com> References: <1617941911-5338-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li If the target is self we do not need to yield, we can avoid malicious guest to play this. Signed-off-by: Wanpeng Li --- v1 -> v2: * update comments arch/x86/kvm/x86.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f08e9b4..ce9a1d2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8231,6 +8231,10 @@ static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id) if (!target || !READ_ONCE(target->ready)) goto no_yield; + /* Ignore requests to yield to self */ + if (vcpu == target) + goto no_yield; + if (kvm_vcpu_yield_to(target) <= 0) goto no_yield;