From patchwork Thu May 13 01:58:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12255093 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 C5F51C433ED for ; Thu, 13 May 2021 01:58:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8EC4361412 for ; Thu, 13 May 2021 01:58:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229854AbhEMB7a (ORCPT ); Wed, 12 May 2021 21:59:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229801AbhEMB71 (ORCPT ); Wed, 12 May 2021 21:59:27 -0400 Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86107C061574; Wed, 12 May 2021 18:58:17 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id c13so6829360pfv.4; Wed, 12 May 2021 18:58:17 -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=s6yds0eAnM4G0Wdu571RvhPi3TKjI+npGap0XFILkfE=; b=klaPh4iM4ryJKcK4ak5Gfiffhss4QGvganMQRvxA3qZPdfc3DZscvNZalHS2iYY7l7 eh7k3GoMAAuKHe4bKB7A2eIgoI5YkAhZbeevFgMnQzAniRwfzgmKbrXup6XMFcuJXzuH OTlFUIcDR5se3vDF+RbYMJYyBuY5lpPA70fczLto5JJvhn+VQnWUDFXrYzSWT9dJ2oC+ bYVn24RIOEWg2b48Qn3ScdJAyLMfMnOOUtnHVJqAmMP/lqcWIHUXK1eQJNwma3vxR9FZ cSwcvzxAwEXITjApPsOQginDTlbi1fzYtvh0sB9yAoez58SOwXoF5cuRkZ6ZGJQIblYE Qxbw== 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=s6yds0eAnM4G0Wdu571RvhPi3TKjI+npGap0XFILkfE=; b=dpfba45GmFGAh0uE1qSEakZRVq9vIFCvbZz3v4rq8I/ZyRQ/xRblrR5pdf6hHgwdh3 5blhSSVvu1drDNzHFIpPl4XvUzA7uctGho9Ow6Mxzwr/cgZhnF/y/ffqqWGpCszga/XN /wHU5x2LuuDVyeiD4AdL12xfIDoXKYlFZtmmwz4bpvWYVZnW/+Rxe/F/Q4grxOwfDOUf 80CAJHgBWrYP10Q8uy9JE9GqwA6kcAUKe8VS+u2OEM/TzoJOX0yxBvYnrF6rQ/jYYnld /1iWyop8ffVGau9hQIOBPk84fHEZgQ6aZd+Q5sfuT3x6M7c3GElQKRS6f3bvSAwePVT4 5m1A== X-Gm-Message-State: AOAM533Brl+1339A0uPd18e5vaL0d4PJGtRUCwxjA61qyDSuvK+zHWIr bvwhQh9/FAj6clDw5ohADCpvWNfiJoo= X-Google-Smtp-Source: ABdhPJwlNz0fWh1ClQWl8HOGpto3Gg0232jmHcD6gUChaiSQ/a7krLx/KvJDva1shE16JBeioyGFvQ== X-Received: by 2002:a17:90a:fd95:: with SMTP id cx21mr1777447pjb.137.1620871096858; Wed, 12 May 2021 18:58:16 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id b7sm799560pjq.36.2021.05.12.18.58.12 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 May 2021 18:58:15 -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 , Ben Segall , Venkatesh Srinivas , David Matlack , Paul Mackerras , Suraj Jitindar Singh Subject: [PATCH v2 1/4] KVM: PPC: Book3S HV: exit halt polling on need_resched() as well Date: Thu, 13 May 2021 09:58:04 +0800 Message-Id: <1620871084-4639-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 Inspired by commit 262de4102c7bb8 (kvm: exit halt polling on need_resched() as well), CFS_BANDWIDTH throttling will use resched_task() when there is just one task to get the task to block. It was likely allowing VMs to overrun their quota when halt polling. Due to PPC implements an arch specific halt polling logic, we should add the need_resched() checking there as well. Cc: Ben Segall Cc: Venkatesh Srinivas Cc: Jim Mattson Cc: David Matlack Cc: Paul Mackerras Cc: Suraj Jitindar Singh Signed-off-by: Wanpeng Li --- v1 -> v2: * update patch description arch/powerpc/kvm/book3s_hv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c index 28a80d2..6199397 100644 --- a/arch/powerpc/kvm/book3s_hv.c +++ b/arch/powerpc/kvm/book3s_hv.c @@ -3936,7 +3936,8 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) break; } cur = ktime_get(); - } while (single_task_running() && ktime_before(cur, stop)); + } while (single_task_running() && !need_resched() && + ktime_before(cur, stop)); spin_lock(&vc->lock); vc->vcore_state = VCORE_INACTIVE; From patchwork Thu May 13 01:59:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12255095 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 E5803C43460 for ; Thu, 13 May 2021 02:00:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BFB4C613C9 for ; Thu, 13 May 2021 02:00:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230017AbhEMCBK (ORCPT ); Wed, 12 May 2021 22:01:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229801AbhEMCBI (ORCPT ); Wed, 12 May 2021 22:01:08 -0400 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 E0EABC061574; Wed, 12 May 2021 18:59:57 -0700 (PDT) Received: by mail-pg1-x52f.google.com with SMTP id m124so19808878pgm.13; Wed, 12 May 2021 18:59:57 -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=VDsDy5xQWwksvhm+nuIt7Vlx0l+g7cwsVeWeOmz1eWU=; b=AFzz3dAqLjeJDZSjvZkjVKkz7cqh6EXhlp7d/z3feu6puiyVz9+I6FrEYQcKmVlhXb LRnTmycZNhPJRT3FBvMzAYBzIv0WzeXDfIOLDWoztB7Vtsxr2m1lz/5NA0deIhIYe59q B2L3ktSnNUrEdeNtvD4tOgjS7uhL9tdi2rpqiOUrN1dFQpgausYfGYpR82JLJykCjTPj icREzk1KSRZ5Zgc+pfITx5KrBoNB4ekENbF8KJUNtMKNiBBx4Cl01u0aUMAn7Pw1Aith LCbd4/KIrrvhGnSlrDT4NFMPr9jrxj7Mo6SPVDFUQeJzK7UEPeDAUci/hHNsXT1mHCPX mcjA== 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=VDsDy5xQWwksvhm+nuIt7Vlx0l+g7cwsVeWeOmz1eWU=; b=Ky6C50Dut8bRIWETMGt2pe3K8VsP4zT/XUcfVprGhBJyrIl7ZxC/oLlQcYB58fQiiJ Wu2AoAoQ1eCV5kkDHq6vTW8pMMCY/pXvTyB8EepL/Eqb/R1og0I6qeMHUZ85V7Mhp2lA KrM1SH2Qzjp/YjsVyku0DNRHnDEqoXxJDnLyqe6rN+1Qu8QGnQZN+LOZF04rcpamNmCp wp564HjajE6RN9O3wSl4Vnkd4tppIqVTyO2gLgOLsU7zJip5MqE6gVo4xIQFAu2lrlsJ T6FFRfOYWPi1ix3IjOeFXaumtLadH6RpTSdPrr8NoC7xuKTuRfYVDO/TFF+qB2X9p+SF gdLw== X-Gm-Message-State: AOAM531aZJ7LnQFOfuDz+q4kZfCvQm/UrYTjlZFkp265j2vNTNzJUJ9m mxMkw/yidJq01W3DWpoQ28gG5oOX/EE= X-Google-Smtp-Source: ABdhPJxXq3Es3YDoK2u5q63iVXU0bF4SUyU/vyJg/b0l7HEC6hCw9fw9KG0YxOyH07TPX2XYk4CeQw== X-Received: by 2002:aa7:95b6:0:b029:28e:aa31:dd3c with SMTP id a22-20020aa795b60000b029028eaa31dd3cmr38528069pfk.43.1620871197231; Wed, 12 May 2021 18:59:57 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id w123sm812742pfw.151.2021.05.12.18.59.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 May 2021 18:59:56 -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/4] KVM: X86: Bail out of direct yield in case of under-comitted scenarios Date: Thu, 13 May 2021 09:59:47 +0800 Message-Id: <1620871189-4763-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 In case of under-comitted scenarios, vCPU can get scheduling easily, kvm_vcpu_yield_to adds extra overhead, we can observe a lot of race between vcpu->ready is true and yield fails due to p->state is TASK_RUNNING. Let's bail out in such scenarios by checking the length of current cpu runqueue, it can be treated as a hint of under-committed instead of guarantee of accuracy. Signed-off-by: Wanpeng Li --- v1 -> v2: * move the check after attempted counting * update patch description arch/x86/kvm/x86.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9b6bca6..dfb7c32 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -8360,6 +8360,9 @@ static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id) vcpu->stat.directed_yield_attempted++; + if (single_task_running()) + goto no_yield; + rcu_read_lock(); map = rcu_dereference(vcpu->kvm->arch.apic_map); From patchwork Thu May 13 01:59:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12255097 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=unavailable 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 81F45C433B4 for ; Thu, 13 May 2021 02:00:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A63361412 for ; Thu, 13 May 2021 02:00:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230102AbhEMCBN (ORCPT ); Wed, 12 May 2021 22:01:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229524AbhEMCBJ (ORCPT ); Wed, 12 May 2021 22:01:09 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80928C06174A; Wed, 12 May 2021 19:00:00 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id h16so7502056pfk.0; Wed, 12 May 2021 19:00:00 -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=W9hpVFpi8lwf9SqKI5HARO8wMQK/aDl0gDMEpsYbJt8=; b=nN9R6JjpxFHuWERf3D2ZBdy/KquinnB5sABS71UZnjOhUadfs+C9oixNxUmCWxcepR HlOXjpDyzxOdhMflcz5nRPEKHPnglF8qSB3suRtdiRmjmfrpFJXeEMxYw0tOFKc1YVFJ FvFG5vIyrV6HOr/Zkd9nOriLaqTFpEqTNtiqMtOmSiEEhzB2vaAFkgQPb4AeJqhUcYXQ G3NtBu1LOLUhS7oyrWM2Px9Vmvgj1I3c9sBnKBIqanBNqpphVRm/Amhs2uuiVjVflkxc 2Q8LAl/nMTkpooNQbfuZfcbsabf+5V+OIQMs+QuaG46LR6LA4BgO4cX54ImlqchwEY3Y fzDQ== 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=W9hpVFpi8lwf9SqKI5HARO8wMQK/aDl0gDMEpsYbJt8=; b=E49DOX3/BqqsuQJ5iLS1VH8/C/TdF1+C7PFlHWFiSUEpAmt4LUJ0W5xScuN954zmM4 BxZ9/HTevEFulgVeiuAoWjuGaE5j31bFnvHsKbR89r8loUojg1rrSMcDXLelXQTDOVQV qQ04aoP9jvIn5dnjeigmDl99fzDqI3Jpw3CEHW0RAmfTek++pvyVxCR792YecxlbbJAk Iyr+MqHZj1pnRSIP8Y+FfYjjUPg3q8Skf7VCzCJDyDB+Gk8G0MXFqLfBs9whCeHoRtbF X3k58bQ9U5PoQvJpTg1ygg5tehbBPzbjFtuDiyKqg2dG96eAcFUAdhkWXBgDyaaxYLEK r4Bw== X-Gm-Message-State: AOAM533zv6RZTug9k2i/9vyntAr0OpNioVIaLu4XSbGMfeFxHLvhbe7k c8HdV5RrT2AEc6zlpK7+Z3iRftrvAmc= X-Google-Smtp-Source: ABdhPJyVB12fxqbj8tMsEQ5JE8fYb2V5ReeY929IikgIjcLJadMH2Oj6HHbLF/rwR3cmrEw0l1XQyw== X-Received: by 2002:a62:754b:0:b029:28e:e78:d752 with SMTP id q72-20020a62754b0000b029028e0e78d752mr37880892pfc.76.1620871199895; Wed, 12 May 2021 18:59:59 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id w123sm812742pfw.151.2021.05.12.18.59.57 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 May 2021 18:59:59 -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 , stable@vger.kernel.org Subject: [PATCH v2 3/4] KVM: X86: Fix vCPU preempted state from guest's point of view Date: Thu, 13 May 2021 09:59:48 +0800 Message-Id: <1620871189-4763-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1620871189-4763-1-git-send-email-wanpengli@tencent.com> References: <1620871189-4763-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li Commit 66570e966dd9 (kvm: x86: only provide PV features if enabled in guest's CPUID) avoids to access pv tlb shootdown host side logic when this pv feature is not exposed to guest, however, kvm_steal_time.preempted not only leveraged by pv tlb shootdown logic but also mitigate the lock holder preemption issue. From guest's point of view, vCPU is always preempted since we lose the reset of kvm_steal_time.preempted before vmentry if pv tlb shootdown feature is not exposed. This patch fixes it by clearing kvm_steal_time.preempted before vmentry. Fixes: 66570e966dd9 (kvm: x86: only provide PV features if enabled in guest's CPUID) Reviewed-by: Sean Christopherson Cc: stable@vger.kernel.org Signed-off-by: Wanpeng Li --- arch/x86/kvm/x86.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index dfb7c32..bed7b53 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3105,6 +3105,8 @@ static void record_steal_time(struct kvm_vcpu *vcpu) st->preempted & KVM_VCPU_FLUSH_TLB); if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) kvm_vcpu_flush_tlb_guest(vcpu); + } else { + st->preempted = 0; } vcpu->arch.st.preempted = 0; From patchwork Thu May 13 01:59:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 12255099 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 96E6EC43462 for ; Thu, 13 May 2021 02:00:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6D365613C9 for ; Thu, 13 May 2021 02:00:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230130AbhEMCBP (ORCPT ); Wed, 12 May 2021 22:01:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46866 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230048AbhEMCBM (ORCPT ); Wed, 12 May 2021 22:01:12 -0400 Received: from mail-pg1-x529.google.com (mail-pg1-x529.google.com [IPv6:2607:f8b0:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3FD7C061574; Wed, 12 May 2021 19:00:02 -0700 (PDT) Received: by mail-pg1-x529.google.com with SMTP id l70so5146412pga.1; Wed, 12 May 2021 19:00:02 -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=v/Eg2aLgMEl3C9DBh5TiPixhOAqZrBo8ju/YFJi/mRs=; b=H0Vf/PzvX1Az7P3qelVGuIU3tInxn8PVF40BcYsq29zt+CjKyrEcg8OJGUm7V7Vw6z 8xLwCXBwBcfozuhpSdADU4XabIQxvQ0WL5ULDTgQrtmv24EfQk8CIFRyMQ8XFrec++ng NU1EefB1Vg3LaDJ6AMhhqaYSWYB+ujA+1KiDdFgZWvD9FQirxc5J3cScWqA9eJDr6cfY tKPuxK0fgiQzze8lQRkj01lDBuv1Fd/lmfJiLwLTX3CHqphuhql0kR4mB/wH4CJJ8dDG bnuUjk40oFgvnzqtUYlTwdvEnks0ZWKMn2knaehUGKeXzeJ1pJBzkmxy8ol8js6uRwW2 v3Hg== 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=v/Eg2aLgMEl3C9DBh5TiPixhOAqZrBo8ju/YFJi/mRs=; b=AeC3/M+HSJda7iksqqEOyndFTTlxNmGF3vv3fjy+AFyoCcMXTbOpV5lju3Eb/0HcI6 Bk1yinnss1AzA0Z7d6bIb3zOX5yuv1pAGCkiP7kupp/KyZ7b9qDbPqdt58CmN97JMyUI hycKyFfc+8pOFkt1a+uaLpUQm8ItHQqIbSTLDWEeaaT/r+aecsNOzFLS8PDxcl3q2SLP IHFbUgxG/FtqiQ28P8BIStRWUuJ+F19FfI6heFpdd/zbh9WzvSNMLFjRYIPELbXfPSzS UFdrqYrTtjWdc5oSdy8aoHPQSiHR0zWrlYMdu1gEAhb6W3vApt+Ge/Ic7oP19Ql6o0Jb 7Q4w== X-Gm-Message-State: AOAM530m1W8HfTMmPqrEJPCLRfpzp1plagUwOkYa54ZnKx2hn1IPIWsW EtPQNm5rgdd4QSBwQXuMYSdn/jdsULA= X-Google-Smtp-Source: ABdhPJzibtVAUmXcXbhH3CJXurXNW/vy8qrWyWeimKensF9A9n0fD8mjbfb7Kpy+62MsfQ4hHemqVg== X-Received: by 2002:a17:90a:510d:: with SMTP id t13mr1782767pjh.1.1620871202379; Wed, 12 May 2021 19:00:02 -0700 (PDT) Received: from localhost.localdomain ([103.7.29.6]) by smtp.googlemail.com with ESMTPSA id w123sm812742pfw.151.2021.05.12.19.00.00 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 12 May 2021 19:00: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 4/4] KVM: x86: hyper-v: Task srcu lock when accessing kvm_memslots() Date: Thu, 13 May 2021 09:59:49 +0800 Message-Id: <1620871189-4763-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1620871189-4763-1-git-send-email-wanpengli@tencent.com> References: <1620871189-4763-1-git-send-email-wanpengli@tencent.com> Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Wanpeng Li WARNING: suspicious RCU usage 5.13.0-rc1 #4 Not tainted ----------------------------- ./include/linux/kvm_host.h:710 suspicious rcu_dereference_check() usage! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 1 lock held by hyperv_clock/8318: #0: ffffb6b8cb05a7d8 (&hv->hv_lock){+.+.}-{3:3}, at: kvm_hv_invalidate_tsc_page+0x3e/0xa0 [kvm] stack backtrace: CPU: 3 PID: 8318 Comm: hyperv_clock Not tainted 5.13.0-rc1 #4 Call Trace: dump_stack+0x87/0xb7 lockdep_rcu_suspicious+0xce/0xf0 kvm_write_guest_page+0x1c1/0x1d0 [kvm] kvm_write_guest+0x50/0x90 [kvm] kvm_hv_invalidate_tsc_page+0x79/0xa0 [kvm] kvm_gen_update_masterclock+0x1d/0x110 [kvm] kvm_arch_vm_ioctl+0x2a7/0xc50 [kvm] kvm_vm_ioctl+0x123/0x11d0 [kvm] __x64_sys_ioctl+0x3ed/0x9d0 do_syscall_64+0x3d/0x80 entry_SYSCALL_64_after_hwframe+0x44/0xae kvm_memslots() will be called by kvm_write_guest(), so we should take the srcu lock. Fixes: e880c6ea5 (KVM: x86: hyper-v: Prevent using not-yet-updated TSC page by secondary CPUs) Reviewed-by: Vitaly Kuznetsov Signed-off-by: Wanpeng Li --- arch/x86/kvm/hyperv.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index f98370a3..f00830e 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1172,6 +1172,7 @@ void kvm_hv_invalidate_tsc_page(struct kvm *kvm) { struct kvm_hv *hv = to_kvm_hv(kvm); u64 gfn; + int idx; if (hv->hv_tsc_page_status == HV_TSC_PAGE_BROKEN || hv->hv_tsc_page_status == HV_TSC_PAGE_UNSET || @@ -1190,9 +1191,16 @@ void kvm_hv_invalidate_tsc_page(struct kvm *kvm) gfn = hv->hv_tsc_page >> HV_X64_MSR_TSC_REFERENCE_ADDRESS_SHIFT; hv->tsc_ref.tsc_sequence = 0; + + /* + * Take the srcu lock as memslots will be accessed to check the gfn + * cache generation against the memslots generation. + */ + idx = srcu_read_lock(&kvm->srcu); if (kvm_write_guest(kvm, gfn_to_gpa(gfn), &hv->tsc_ref, sizeof(hv->tsc_ref.tsc_sequence))) hv->hv_tsc_page_status = HV_TSC_PAGE_BROKEN; + srcu_read_unlock(&kvm->srcu, idx); out_unlock: mutex_unlock(&hv->hv_lock);