From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9121AC433FE for ; Mon, 25 Oct 2021 13:32:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6FFA160F46 for ; Mon, 25 Oct 2021 13:32:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232424AbhJYNeY (ORCPT ); Mon, 25 Oct 2021 09:34:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:57652 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231241AbhJYNeW (ORCPT ); Mon, 25 Oct 2021 09:34:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635168719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=SpvM56gOQxdR2GkQfeSmO+O/QwaoLWm5eUlFc95EtEJ6WV0po9Bi8WS7HsQjdn9GzFG1ju 6YKz+JxvVOqnjNuFN8z8yiBL9ghxfN0/sLTk35vM0QqSCbgTJy8hGKf+8NwwM5AEaR0tBQ mfUC1q4uy7Ay1Dfyv00r0snJP9ma5nY= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-120-UDcFOy5AOI2jj3_Ry0CJDg-1; Mon, 25 Oct 2021 09:31:58 -0400 X-MC-Unique: UDcFOy5AOI2jj3_Ry0CJDg-1 Received: by mail-ed1-f71.google.com with SMTP id i9-20020a508709000000b003dd4b55a3caso253138edb.19 for ; Mon, 25 Oct 2021 06:31:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=Ufr8+CNeexKs9je4jgh45rqgcLhNyrTeOPa5WdQG56FmHl1IuB+L5XDkHXKZ9ezhaI CHbN4zxwk217uM46gxyYjjSOi8T3nsnzprFXiOOQKqykl1jOIARRpwVH7zXkhRYMRkQZ hnJ95LnbXra0GszLKw3kb1x7XRyEcjV7qJUQ4lRkB+aBq/to6k2bm1VB79wFDTUcE/pu RkNAK7ZqIS1pnXG5F0/DnOVSTTCSO95w6EpB3bZ79uJBT0xsdc/4mFaOFtxacP+RiLXe a08uOyVlNjdw+GL7Df6W4TFm1jPRU1t5MowShIkyu78DuRzMh10Jq/1Hw80MNXhlRRUx PrCg== X-Gm-Message-State: AOAM530Z5N/FxWdjp01lf1DiagjtKfxMJvJAcjrK6c4BT1Ga0TERIZGS xVLhKKcXSiJXl0XX6Ju5zS01rcg+1axcnxTn2XdTRM8Ue9nJNdbhIb+SZhl/jjMbAvWc7kMSJco Kr4X7CBq97W1Z1bOaonG8UzUR X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238657ejc.301.1635168717197; Mon, 25 Oct 2021 06:31:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqwGQb/OPNb2TrcrVpQ2kfMVsFqyH8YAEneTvl+Ho2w1Ti+MJiG6IAW7s9nE4hJCbcjwxz7g== X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238632ejc.301.1635168716958; Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id kw10sm7463469ejc.71.2021.10.25.06.31.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Message-ID: <9236e715-c471-e1c8-6117-6f37b908a6bd@redhat.com> Date: Mon, 25 Oct 2021 15:31:48 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH v2 10/43] KVM: arm64: Move vGIC v4 handling for WFI out arch callback hook Content-Language: en-US To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-11-seanjc@google.com> From: Paolo Bonzini In-Reply-To: <20211009021236.4122790-11-seanjc@google.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 09/10/21 04:12, Sean Christopherson wrote: > Move the put and reload of the vGIC out of the block/unblock callbacks > and into a dedicated WFI helper. Functionally, this is nearly a nop as > the block hook is called at the very beginning of kvm_vcpu_block(), and > the only code in kvm_vcpu_block() after the unblock hook is to update the > halt-polling controls, i.e. can only affect the next WFI. > > Back when the arch (un)blocking hooks were added by commits 3217f7c25bca > ("KVM: Add kvm_arch_vcpu_{un}blocking callbacks) and d35268da6687 > ("arm/arm64: KVM: arch_timer: Only schedule soft timer on vcpu_block"), > the hooks were invoked only when KVM was about to "block", i.e. schedule > out the vCPU. The use case at the time was to schedule a timer in the > host based on the earliest timer in the guest in order to wake the > blocking vCPU when the emulated guest timer fired. Commit accb99bcd0ca > ("KVM: arm/arm64: Simplify bg_timer programming") reworked the timer > logic to be even more precise, by waiting until the vCPU was actually > scheduled out, and so move the timer logic from the (un)blocking hooks to > vcpu_load/put. > > In the meantime, the hooks gained usage for enabling vGIC v4 doorbells in > commit df9ba95993b9 ("KVM: arm/arm64: GICv4: Use the doorbell interrupt > as an unblocking source"), and added related logic for the VMCR in commit > 5eeaf10eec39 ("KVM: arm/arm64: Sync ICH_VMCR_EL2 back when about to block"). > > Finally, commit 07ab0f8d9a12 ("KVM: Call kvm_arch_vcpu_blocking early > into the blocking sequence") hoisted the (un)blocking hooks so that they > wrapped KVM's halt-polling logic in addition to the core "block" logic. > > In other words, the original need for arch hooks to take action _only_ > in the block path is long since gone. > > Cc: Oliver Upton > Cc: Marc Zyngier > Signed-off-by: Sean Christopherson This needs a word on why kvm_psci_vcpu_suspend does not need the hooks. Or it needs to be changed to also use kvm_vcpu_wfi in the PSCI code, I don't know. Marc, can you review and/or advise? Thanks, Paolo > --- > arch/arm64/include/asm/kvm_emulate.h | 2 ++ > arch/arm64/kvm/arm.c | 52 +++++++++++++++++++--------- > arch/arm64/kvm/handle_exit.c | 3 +- > 3 files changed, 38 insertions(+), 19 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index fd418955e31e..de8b4f5922b7 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -41,6 +41,8 @@ void kvm_inject_vabt(struct kvm_vcpu *vcpu); > void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); > void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); > > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu); > + > static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) > { > return !(vcpu->arch.hcr_el2 & HCR_RW); > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 7838e9fb693e..1346f81b34df 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -359,27 +359,12 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) > > void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) > { > - /* > - * If we're about to block (most likely because we've just hit a > - * WFI), we need to sync back the state of the GIC CPU interface > - * so that we have the latest PMR and group enables. This ensures > - * that kvm_arch_vcpu_runnable has up-to-date data to decide > - * whether we have pending interrupts. > - * > - * For the same reason, we want to tell GICv4 that we need > - * doorbells to be signalled, should an interrupt become pending. > - */ > - preempt_disable(); > - kvm_vgic_vmcr_sync(vcpu); > - vgic_v4_put(vcpu, true); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) > { > - preempt_disable(); > - vgic_v4_load(vcpu); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > @@ -662,6 +647,39 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu) > smp_rmb(); > } > > +/** > + * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior > + * @vcpu: The VCPU pointer > + * > + * Suspend execution of a vCPU until a valid wake event is detected, i.e. until > + * the vCPU is runnable. The vCPU may or may not be scheduled out, depending > + * on when a wake event arrives, e.g. there may already be a pending wake event. > + */ > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) > +{ > + /* > + * Sync back the state of the GIC CPU interface so that we have > + * the latest PMR and group enables. This ensures that > + * kvm_arch_vcpu_runnable has up-to-date data to decide whether > + * we have pending interrupts, e.g. when determining if the > + * vCPU should block. > + * > + * For the same reason, we want to tell GICv4 that we need > + * doorbells to be signalled, should an interrupt become pending. > + */ > + preempt_disable(); > + kvm_vgic_vmcr_sync(vcpu); > + vgic_v4_put(vcpu, true); > + preempt_enable(); > + > + kvm_vcpu_block(vcpu); > + kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + > + preempt_disable(); > + vgic_v4_load(vcpu); > + preempt_enable(); > +} > + > static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) > { > return vcpu->arch.target >= 0; > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c > index 275a27368a04..4794563a506b 100644 > --- a/arch/arm64/kvm/handle_exit.c > +++ b/arch/arm64/kvm/handle_exit.c > @@ -95,8 +95,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu) > } else { > trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false); > vcpu->stat.wfi_exit_stat++; > - kvm_vcpu_block(vcpu); > - kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + kvm_vcpu_wfi(vcpu); > } > > kvm_incr_pc(vcpu); > From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8ED38C433F5 for ; Mon, 25 Oct 2021 13:32:23 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 50DEF6023B for ; Mon, 25 Oct 2021 13:32:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 50DEF6023B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=giFKepD5ORCGk9vO7H07ndzVq+RI9SlLMh9zFrvQA+I=; b=YbKAksKCjjPcy8 njhHLIVyST1Bh9fs8fBrhnQVmC2jYYrzC4qxd7ndgcv001rFklbuYU5++4SezxGPZ1AmFJvFUuPXp OAJMu5mFj/etBCVI+WQg9+J4JBUtTQJV5NnI0I1wKZtZh2/pikf6JQ0YuYB7eDDm/0jIFoC1OdsEJ o9bAaf+VMLE9xqGGkbD/V0SK8VOqVU/0cfP+0460xa0GgVfDV/KdmAIH2I2J5NINiqbaRz89WP4UJ RRGYCXr/Sgo07HuyVeWr85kJRRW2ixbtDL8CU/PaMZvhDAXZ+aVn4IEpAHEDyXv3u++pbgkOy1TZL 3AS9QXDfGGU7XP4UHJEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mf05H-00GXnJ-5Z; Mon, 25 Oct 2021 13:32:15 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mf053-00GXk0-8Y for linux-riscv@lists.infradead.org; Mon, 25 Oct 2021 13:32:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635168720; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=aqYivRuWMYTwi0WI4uNeIuO/gjACXjKQGHNrbC/xB3qRH4FfSU7tTS1BvE2s/nhDvbUIDv kxUFgccxE5M8YKfi4rP6sb2BKxyrnz9BObRt37KiMY3FDqkAwAeWSWAVSDzDfHbDahldcd 2H70sO+21mYSusOgneVuc4ghSan7o4U= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-404-AXgN3BY7OUab9WK_1UKY-g-1; Mon, 25 Oct 2021 09:31:58 -0400 X-MC-Unique: AXgN3BY7OUab9WK_1UKY-g-1 Received: by mail-ed1-f72.google.com with SMTP id y3-20020a056402358300b003dd490c775cso662935edc.22 for ; Mon, 25 Oct 2021 06:31:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=4N26uo0HyqmuUJIwByfDRiSKDkjsw+uUhg2p0GoKFUGlcwtRq7zMo8UbxDlMcOdkGD 7lbOT1qEsNQQQpWIEM2ruNi1Y7mI9N9a/nRMguEJqhwFa4dskHzogSQt16AheN34DJ+f h9J++9ugzKEj/srCrhqMM/9OmonvUtJPmEpIE1gmVU/87dj0pbGuBkHFU3P3Dk3xVbbz GqrdJ3rVolubQ3oJN03o3m26h38yqJC7JsBRLQIdbANRHmwWoYM3bg0at4yzAg2yDGcg 5+ao9lkSZyf97isH4iMjnYvDcdWKt/tPOSSbl+H1Qzdsh/YRS9H59E6tWEyaZmvArrIF utzw== X-Gm-Message-State: AOAM531heLW0Ysl2FO66Y7BSvNb0DffMSfdqGoZl9PGrPt4WWAjtKxEY 21zf+YW87Zhk/79BAWV88I8Kokyaz4iaWAK5I9JMhzvBrDkX99Warh6d/GkYU5lCM0A8iQ0pDmi 1+fONgxAPBfx8awDAizKO+X+L8Bi/ X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238658ejc.301.1635168717198; Mon, 25 Oct 2021 06:31:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqwGQb/OPNb2TrcrVpQ2kfMVsFqyH8YAEneTvl+Ho2w1Ti+MJiG6IAW7s9nE4hJCbcjwxz7g== X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238632ejc.301.1635168716958; Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id kw10sm7463469ejc.71.2021.10.25.06.31.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Message-ID: <9236e715-c471-e1c8-6117-6f37b908a6bd@redhat.com> Date: Mon, 25 Oct 2021 15:31:48 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH v2 10/43] KVM: arm64: Move vGIC v4 handling for WFI out arch callback hook To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-11-seanjc@google.com> From: Paolo Bonzini In-Reply-To: <20211009021236.4122790-11-seanjc@google.com> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211025_063201_391601_C333BFA7 X-CRM114-Status: GOOD ( 33.42 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org On 09/10/21 04:12, Sean Christopherson wrote: > Move the put and reload of the vGIC out of the block/unblock callbacks > and into a dedicated WFI helper. Functionally, this is nearly a nop as > the block hook is called at the very beginning of kvm_vcpu_block(), and > the only code in kvm_vcpu_block() after the unblock hook is to update the > halt-polling controls, i.e. can only affect the next WFI. > > Back when the arch (un)blocking hooks were added by commits 3217f7c25bca > ("KVM: Add kvm_arch_vcpu_{un}blocking callbacks) and d35268da6687 > ("arm/arm64: KVM: arch_timer: Only schedule soft timer on vcpu_block"), > the hooks were invoked only when KVM was about to "block", i.e. schedule > out the vCPU. The use case at the time was to schedule a timer in the > host based on the earliest timer in the guest in order to wake the > blocking vCPU when the emulated guest timer fired. Commit accb99bcd0ca > ("KVM: arm/arm64: Simplify bg_timer programming") reworked the timer > logic to be even more precise, by waiting until the vCPU was actually > scheduled out, and so move the timer logic from the (un)blocking hooks to > vcpu_load/put. > > In the meantime, the hooks gained usage for enabling vGIC v4 doorbells in > commit df9ba95993b9 ("KVM: arm/arm64: GICv4: Use the doorbell interrupt > as an unblocking source"), and added related logic for the VMCR in commit > 5eeaf10eec39 ("KVM: arm/arm64: Sync ICH_VMCR_EL2 back when about to block"). > > Finally, commit 07ab0f8d9a12 ("KVM: Call kvm_arch_vcpu_blocking early > into the blocking sequence") hoisted the (un)blocking hooks so that they > wrapped KVM's halt-polling logic in addition to the core "block" logic. > > In other words, the original need for arch hooks to take action _only_ > in the block path is long since gone. > > Cc: Oliver Upton > Cc: Marc Zyngier > Signed-off-by: Sean Christopherson This needs a word on why kvm_psci_vcpu_suspend does not need the hooks. Or it needs to be changed to also use kvm_vcpu_wfi in the PSCI code, I don't know. Marc, can you review and/or advise? Thanks, Paolo > --- > arch/arm64/include/asm/kvm_emulate.h | 2 ++ > arch/arm64/kvm/arm.c | 52 +++++++++++++++++++--------- > arch/arm64/kvm/handle_exit.c | 3 +- > 3 files changed, 38 insertions(+), 19 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index fd418955e31e..de8b4f5922b7 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -41,6 +41,8 @@ void kvm_inject_vabt(struct kvm_vcpu *vcpu); > void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); > void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); > > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu); > + > static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) > { > return !(vcpu->arch.hcr_el2 & HCR_RW); > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 7838e9fb693e..1346f81b34df 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -359,27 +359,12 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) > > void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) > { > - /* > - * If we're about to block (most likely because we've just hit a > - * WFI), we need to sync back the state of the GIC CPU interface > - * so that we have the latest PMR and group enables. This ensures > - * that kvm_arch_vcpu_runnable has up-to-date data to decide > - * whether we have pending interrupts. > - * > - * For the same reason, we want to tell GICv4 that we need > - * doorbells to be signalled, should an interrupt become pending. > - */ > - preempt_disable(); > - kvm_vgic_vmcr_sync(vcpu); > - vgic_v4_put(vcpu, true); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) > { > - preempt_disable(); > - vgic_v4_load(vcpu); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > @@ -662,6 +647,39 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu) > smp_rmb(); > } > > +/** > + * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior > + * @vcpu: The VCPU pointer > + * > + * Suspend execution of a vCPU until a valid wake event is detected, i.e. until > + * the vCPU is runnable. The vCPU may or may not be scheduled out, depending > + * on when a wake event arrives, e.g. there may already be a pending wake event. > + */ > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) > +{ > + /* > + * Sync back the state of the GIC CPU interface so that we have > + * the latest PMR and group enables. This ensures that > + * kvm_arch_vcpu_runnable has up-to-date data to decide whether > + * we have pending interrupts, e.g. when determining if the > + * vCPU should block. > + * > + * For the same reason, we want to tell GICv4 that we need > + * doorbells to be signalled, should an interrupt become pending. > + */ > + preempt_disable(); > + kvm_vgic_vmcr_sync(vcpu); > + vgic_v4_put(vcpu, true); > + preempt_enable(); > + > + kvm_vcpu_block(vcpu); > + kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + > + preempt_disable(); > + vgic_v4_load(vcpu); > + preempt_enable(); > +} > + > static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) > { > return vcpu->arch.target >= 0; > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c > index 275a27368a04..4794563a506b 100644 > --- a/arch/arm64/kvm/handle_exit.c > +++ b/arch/arm64/kvm/handle_exit.c > @@ -95,8 +95,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu) > } else { > trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false); > vcpu->stat.wfi_exit_stat++; > - kvm_vcpu_block(vcpu); > - kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + kvm_vcpu_wfi(vcpu); > } > > kvm_incr_pc(vcpu); > _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 572BAC433EF for ; Mon, 25 Oct 2021 13:32:20 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id D1CB860F46 for ; Mon, 25 Oct 2021 13:32:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D1CB860F46 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 5DC494B168; Mon, 25 Oct 2021 09:32:19 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@redhat.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id O58UA51-ensG; Mon, 25 Oct 2021 09:32:17 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id B9E314B0CC; Mon, 25 Oct 2021 09:32:17 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id DD3A64B08E for ; Mon, 25 Oct 2021 09:32:16 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 94NV987uuCgB for ; Mon, 25 Oct 2021 09:32:15 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mm01.cs.columbia.edu (Postfix) with ESMTP id B2ADC4A500 for ; Mon, 25 Oct 2021 09:32:15 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635168735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=UZFl2DOvyf1mtH8k1Xu+riK4roZvlzZJhmjTtDUMvktKGB7gh+zwo6jyDis4w8mAT+dDJK et3r6X+tVvBJFYLOifIy+6h22Rhf9CuZwh4inB1Dmy3VGVHJTn9x1v/YqT+O0D7D6jKE54 CEmXj09MMFTczXehvZ6iYhXg2/9Czqs= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-518-TICaHilLNfKnC28qRuP5bA-1; Mon, 25 Oct 2021 09:32:14 -0400 X-MC-Unique: TICaHilLNfKnC28qRuP5bA-1 Received: by mail-ed1-f70.google.com with SMTP id u10-20020a50d94a000000b003dc51565894so9863462edj.21 for ; Mon, 25 Oct 2021 06:32:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=b/Ngn6d1vo8dyIitgF3O6DbC4CbXykfToVetoJOuShZxGY8wMqtJhW4NoTUaxit4I7 xj/lIRYR3fwovy3FHTxfFKsmELaHmJqYXOEGzFKHUCWEA6BITnvibNLO50tEsOzM1ryP N2wI7E4JIsZZG6EIN/CZ0uZW68HYFzOztWMI+11zelt5fzYelOvVUBWtDTgYp9+dFoZm yihTwOEnBVhV0smEE226LzWAw650BCt482FANL3qBtBrmsqgBTyXYk524N3DA6Y3yNOO oS5p7+PxkIoRYEpuCo27wsGWVN+CaE+YJS+ZCrOCMbA8nqh8LkS4LmGKJa3MKvfZZnrm oe5Q== X-Gm-Message-State: AOAM531d+NIqPG3alZmPDvvf0ayT7ZEqY+g1So2EnrsWqc9rg+YCULkF 1hmV3wI/iinXQJDfT/M6ms9jHxxdEolfF6FfziL052m4/CWVdUlEnngpO+cCHMyu8WhElzV+QOj hlqqVHL4mQ1Hvllc8h4oQYOYO X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238669ejc.301.1635168717199; Mon, 25 Oct 2021 06:31:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqwGQb/OPNb2TrcrVpQ2kfMVsFqyH8YAEneTvl+Ho2w1Ti+MJiG6IAW7s9nE4hJCbcjwxz7g== X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238632ejc.301.1635168716958; Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id kw10sm7463469ejc.71.2021.10.25.06.31.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Message-ID: <9236e715-c471-e1c8-6117-6f37b908a6bd@redhat.com> Date: Mon, 25 Oct 2021 15:31:48 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH v2 10/43] KVM: arm64: Move vGIC v4 handling for WFI out arch callback hook To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-11-seanjc@google.com> From: Paolo Bonzini In-Reply-To: <20211009021236.4122790-11-seanjc@google.com> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Cc: Wanpeng Li , kvm@vger.kernel.org, David Hildenbrand , linux-kernel@vger.kernel.org, Atish Patra , linux-riscv@lists.infradead.org, Claudio Imbrenda , kvmarm@lists.cs.columbia.edu, Joerg Roedel , kvm-ppc@vger.kernel.org, David Matlack , linux-arm-kernel@lists.infradead.org, Jim Mattson , Cornelia Huck , linux-mips@vger.kernel.org, kvm-riscv@lists.infradead.org, Vitaly Kuznetsov X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On 09/10/21 04:12, Sean Christopherson wrote: > Move the put and reload of the vGIC out of the block/unblock callbacks > and into a dedicated WFI helper. Functionally, this is nearly a nop as > the block hook is called at the very beginning of kvm_vcpu_block(), and > the only code in kvm_vcpu_block() after the unblock hook is to update the > halt-polling controls, i.e. can only affect the next WFI. > > Back when the arch (un)blocking hooks were added by commits 3217f7c25bca > ("KVM: Add kvm_arch_vcpu_{un}blocking callbacks) and d35268da6687 > ("arm/arm64: KVM: arch_timer: Only schedule soft timer on vcpu_block"), > the hooks were invoked only when KVM was about to "block", i.e. schedule > out the vCPU. The use case at the time was to schedule a timer in the > host based on the earliest timer in the guest in order to wake the > blocking vCPU when the emulated guest timer fired. Commit accb99bcd0ca > ("KVM: arm/arm64: Simplify bg_timer programming") reworked the timer > logic to be even more precise, by waiting until the vCPU was actually > scheduled out, and so move the timer logic from the (un)blocking hooks to > vcpu_load/put. > > In the meantime, the hooks gained usage for enabling vGIC v4 doorbells in > commit df9ba95993b9 ("KVM: arm/arm64: GICv4: Use the doorbell interrupt > as an unblocking source"), and added related logic for the VMCR in commit > 5eeaf10eec39 ("KVM: arm/arm64: Sync ICH_VMCR_EL2 back when about to block"). > > Finally, commit 07ab0f8d9a12 ("KVM: Call kvm_arch_vcpu_blocking early > into the blocking sequence") hoisted the (un)blocking hooks so that they > wrapped KVM's halt-polling logic in addition to the core "block" logic. > > In other words, the original need for arch hooks to take action _only_ > in the block path is long since gone. > > Cc: Oliver Upton > Cc: Marc Zyngier > Signed-off-by: Sean Christopherson This needs a word on why kvm_psci_vcpu_suspend does not need the hooks. Or it needs to be changed to also use kvm_vcpu_wfi in the PSCI code, I don't know. Marc, can you review and/or advise? Thanks, Paolo > --- > arch/arm64/include/asm/kvm_emulate.h | 2 ++ > arch/arm64/kvm/arm.c | 52 +++++++++++++++++++--------- > arch/arm64/kvm/handle_exit.c | 3 +- > 3 files changed, 38 insertions(+), 19 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index fd418955e31e..de8b4f5922b7 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -41,6 +41,8 @@ void kvm_inject_vabt(struct kvm_vcpu *vcpu); > void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); > void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); > > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu); > + > static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) > { > return !(vcpu->arch.hcr_el2 & HCR_RW); > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 7838e9fb693e..1346f81b34df 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -359,27 +359,12 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) > > void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) > { > - /* > - * If we're about to block (most likely because we've just hit a > - * WFI), we need to sync back the state of the GIC CPU interface > - * so that we have the latest PMR and group enables. This ensures > - * that kvm_arch_vcpu_runnable has up-to-date data to decide > - * whether we have pending interrupts. > - * > - * For the same reason, we want to tell GICv4 that we need > - * doorbells to be signalled, should an interrupt become pending. > - */ > - preempt_disable(); > - kvm_vgic_vmcr_sync(vcpu); > - vgic_v4_put(vcpu, true); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) > { > - preempt_disable(); > - vgic_v4_load(vcpu); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > @@ -662,6 +647,39 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu) > smp_rmb(); > } > > +/** > + * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior > + * @vcpu: The VCPU pointer > + * > + * Suspend execution of a vCPU until a valid wake event is detected, i.e. until > + * the vCPU is runnable. The vCPU may or may not be scheduled out, depending > + * on when a wake event arrives, e.g. there may already be a pending wake event. > + */ > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) > +{ > + /* > + * Sync back the state of the GIC CPU interface so that we have > + * the latest PMR and group enables. This ensures that > + * kvm_arch_vcpu_runnable has up-to-date data to decide whether > + * we have pending interrupts, e.g. when determining if the > + * vCPU should block. > + * > + * For the same reason, we want to tell GICv4 that we need > + * doorbells to be signalled, should an interrupt become pending. > + */ > + preempt_disable(); > + kvm_vgic_vmcr_sync(vcpu); > + vgic_v4_put(vcpu, true); > + preempt_enable(); > + > + kvm_vcpu_block(vcpu); > + kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + > + preempt_disable(); > + vgic_v4_load(vcpu); > + preempt_enable(); > +} > + > static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) > { > return vcpu->arch.target >= 0; > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c > index 275a27368a04..4794563a506b 100644 > --- a/arch/arm64/kvm/handle_exit.c > +++ b/arch/arm64/kvm/handle_exit.c > @@ -95,8 +95,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu) > } else { > trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false); > vcpu->stat.wfi_exit_stat++; > - kvm_vcpu_block(vcpu); > - kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + kvm_vcpu_wfi(vcpu); > } > > kvm_incr_pc(vcpu); > _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A3FD0C433EF for ; Mon, 25 Oct 2021 13:33:33 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6762B60C49 for ; Mon, 25 Oct 2021 13:33:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 6762B60C49 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=L1o/HsV1sNHUcvkF6Tq3UM0SnBFI3TyeWpIDU1AFXDE=; b=YwobADDKDlYlhi xhN482VaaY7hiHrYJ943KKzrp9heFlRVT24S9FgUn17d4PqhaynJHjkezfQdDXdgf/pJXgFAwIuZl s6G3rVSBlwk+Ht/JcQw6woKwd7+XYaXV4A0pBaGx9cunCNYLQCkuk4k3VisCL0ZQUqeIFlfnWgelt DH1BS9yDsFp/o/GjgDPtYara1jAVemXWyhdczZaZPiq7lrrhuQiQSDs1BuvH1JIVqi3URtQ1Tez32 x0ULJpmvdHqyTf2e6aaAjr8p5cwo2ntqx4KsdqoRp/Oovkb5DvXTgXLFWyXJz5NPPYcTCqGhtM3oq JgLilssiuUVuV+f2yUAA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mf057-00GXly-RR; Mon, 25 Oct 2021 13:32:06 +0000 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mf052-00GXjy-UD for linux-arm-kernel@lists.infradead.org; Mon, 25 Oct 2021 13:32:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635168720; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=aqYivRuWMYTwi0WI4uNeIuO/gjACXjKQGHNrbC/xB3qRH4FfSU7tTS1BvE2s/nhDvbUIDv kxUFgccxE5M8YKfi4rP6sb2BKxyrnz9BObRt37KiMY3FDqkAwAeWSWAVSDzDfHbDahldcd 2H70sO+21mYSusOgneVuc4ghSan7o4U= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-191-CjxRMh_dMv6uQwGZZ8OG3Q-1; Mon, 25 Oct 2021 09:31:58 -0400 X-MC-Unique: CjxRMh_dMv6uQwGZZ8OG3Q-1 Received: by mail-ed1-f71.google.com with SMTP id w7-20020a056402268700b003dd46823a18so4028925edd.18 for ; Mon, 25 Oct 2021 06:31:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=jwuvCkOHo62fGDRtX62BpdTPwCRqLZcCn5V8ASXdLIA=; b=er/fYhskVBY9Xo81H7po+x7s77axTFwp/4EMTTqmntROgAqksYwHgApOwipMO4VehH OB+iMo1Q8SbET2LKM6b5Lksi/EiSWcFpydI9dBPok5Tk1mhE0R2npEdFIDkybYBfsFFM ENsg+2Ind1sqWFOMg/uZKZxdx/vuH5GSTicZw3Py2TbdvlHF6oUjOUjRTDIxUHSE9t9V 3+SKv4j91FSR+/HAxcQZDr9Ykb4RTMC4CVN0zs+Aob5l1fPW7nZOwG4lrNETu1F2yu/w /bs/vnoSwGrJJBIITDkczl3NYk+AHVobUIPtIrPVjo80CH8HrDUIVTm7qbdBTXJzLq75 f1kg== X-Gm-Message-State: AOAM531msD0Ibyge7PVpslDX7EfPNhyVLVXCNb2vf2FvDWA13LOfFGw2 BO8dab/IGXIvOTVnHs26dIlfT6+krfNDXfz20jriYQ4knH+DoPpJDLKAkPVGqKXPpvWc8UXjt6y fOadO+pf3Y5sLj1bXgMDaXvbRPMefgA7394E= X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238699ejc.301.1635168717278; Mon, 25 Oct 2021 06:31:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzqwGQb/OPNb2TrcrVpQ2kfMVsFqyH8YAEneTvl+Ho2w1Ti+MJiG6IAW7s9nE4hJCbcjwxz7g== X-Received: by 2002:a17:906:2606:: with SMTP id h6mr22238632ejc.301.1635168716958; Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id kw10sm7463469ejc.71.2021.10.25.06.31.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Oct 2021 06:31:56 -0700 (PDT) Message-ID: <9236e715-c471-e1c8-6117-6f37b908a6bd@redhat.com> Date: Mon, 25 Oct 2021 15:31:48 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: [PATCH v2 10/43] KVM: arm64: Move vGIC v4 handling for WFI out arch callback hook To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-11-seanjc@google.com> From: Paolo Bonzini In-Reply-To: <20211009021236.4122790-11-seanjc@google.com> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211025_063201_088502_E5F95DC7 X-CRM114-Status: GOOD ( 34.92 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 09/10/21 04:12, Sean Christopherson wrote: > Move the put and reload of the vGIC out of the block/unblock callbacks > and into a dedicated WFI helper. Functionally, this is nearly a nop as > the block hook is called at the very beginning of kvm_vcpu_block(), and > the only code in kvm_vcpu_block() after the unblock hook is to update the > halt-polling controls, i.e. can only affect the next WFI. > > Back when the arch (un)blocking hooks were added by commits 3217f7c25bca > ("KVM: Add kvm_arch_vcpu_{un}blocking callbacks) and d35268da6687 > ("arm/arm64: KVM: arch_timer: Only schedule soft timer on vcpu_block"), > the hooks were invoked only when KVM was about to "block", i.e. schedule > out the vCPU. The use case at the time was to schedule a timer in the > host based on the earliest timer in the guest in order to wake the > blocking vCPU when the emulated guest timer fired. Commit accb99bcd0ca > ("KVM: arm/arm64: Simplify bg_timer programming") reworked the timer > logic to be even more precise, by waiting until the vCPU was actually > scheduled out, and so move the timer logic from the (un)blocking hooks to > vcpu_load/put. > > In the meantime, the hooks gained usage for enabling vGIC v4 doorbells in > commit df9ba95993b9 ("KVM: arm/arm64: GICv4: Use the doorbell interrupt > as an unblocking source"), and added related logic for the VMCR in commit > 5eeaf10eec39 ("KVM: arm/arm64: Sync ICH_VMCR_EL2 back when about to block"). > > Finally, commit 07ab0f8d9a12 ("KVM: Call kvm_arch_vcpu_blocking early > into the blocking sequence") hoisted the (un)blocking hooks so that they > wrapped KVM's halt-polling logic in addition to the core "block" logic. > > In other words, the original need for arch hooks to take action _only_ > in the block path is long since gone. > > Cc: Oliver Upton > Cc: Marc Zyngier > Signed-off-by: Sean Christopherson This needs a word on why kvm_psci_vcpu_suspend does not need the hooks. Or it needs to be changed to also use kvm_vcpu_wfi in the PSCI code, I don't know. Marc, can you review and/or advise? Thanks, Paolo > --- > arch/arm64/include/asm/kvm_emulate.h | 2 ++ > arch/arm64/kvm/arm.c | 52 +++++++++++++++++++--------- > arch/arm64/kvm/handle_exit.c | 3 +- > 3 files changed, 38 insertions(+), 19 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index fd418955e31e..de8b4f5922b7 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -41,6 +41,8 @@ void kvm_inject_vabt(struct kvm_vcpu *vcpu); > void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); > void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); > > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu); > + > static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) > { > return !(vcpu->arch.hcr_el2 & HCR_RW); > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 7838e9fb693e..1346f81b34df 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -359,27 +359,12 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) > > void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) > { > - /* > - * If we're about to block (most likely because we've just hit a > - * WFI), we need to sync back the state of the GIC CPU interface > - * so that we have the latest PMR and group enables. This ensures > - * that kvm_arch_vcpu_runnable has up-to-date data to decide > - * whether we have pending interrupts. > - * > - * For the same reason, we want to tell GICv4 that we need > - * doorbells to be signalled, should an interrupt become pending. > - */ > - preempt_disable(); > - kvm_vgic_vmcr_sync(vcpu); > - vgic_v4_put(vcpu, true); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) > { > - preempt_disable(); > - vgic_v4_load(vcpu); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > @@ -662,6 +647,39 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu) > smp_rmb(); > } > > +/** > + * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior > + * @vcpu: The VCPU pointer > + * > + * Suspend execution of a vCPU until a valid wake event is detected, i.e. until > + * the vCPU is runnable. The vCPU may or may not be scheduled out, depending > + * on when a wake event arrives, e.g. there may already be a pending wake event. > + */ > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) > +{ > + /* > + * Sync back the state of the GIC CPU interface so that we have > + * the latest PMR and group enables. This ensures that > + * kvm_arch_vcpu_runnable has up-to-date data to decide whether > + * we have pending interrupts, e.g. when determining if the > + * vCPU should block. > + * > + * For the same reason, we want to tell GICv4 that we need > + * doorbells to be signalled, should an interrupt become pending. > + */ > + preempt_disable(); > + kvm_vgic_vmcr_sync(vcpu); > + vgic_v4_put(vcpu, true); > + preempt_enable(); > + > + kvm_vcpu_block(vcpu); > + kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + > + preempt_disable(); > + vgic_v4_load(vcpu); > + preempt_enable(); > +} > + > static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) > { > return vcpu->arch.target >= 0; > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c > index 275a27368a04..4794563a506b 100644 > --- a/arch/arm64/kvm/handle_exit.c > +++ b/arch/arm64/kvm/handle_exit.c > @@ -95,8 +95,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu) > } else { > trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false); > vcpu->stat.wfi_exit_stat++; > - kvm_vcpu_block(vcpu); > - kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + kvm_vcpu_wfi(vcpu); > } > > kvm_incr_pc(vcpu); > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paolo Bonzini Date: Mon, 25 Oct 2021 13:31:48 +0000 Subject: Re: [PATCH v2 10/43] KVM: arm64: Move vGIC v4 handling for WFI out arch callback hook Message-Id: <9236e715-c471-e1c8-6117-6f37b908a6bd@redhat.com> List-Id: References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-11-seanjc@google.com> In-Reply-To: <20211009021236.4122790-11-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang On 09/10/21 04:12, Sean Christopherson wrote: > Move the put and reload of the vGIC out of the block/unblock callbacks > and into a dedicated WFI helper. Functionally, this is nearly a nop as > the block hook is called at the very beginning of kvm_vcpu_block(), and > the only code in kvm_vcpu_block() after the unblock hook is to update the > halt-polling controls, i.e. can only affect the next WFI. > > Back when the arch (un)blocking hooks were added by commits 3217f7c25bca > ("KVM: Add kvm_arch_vcpu_{un}blocking callbacks) and d35268da6687 > ("arm/arm64: KVM: arch_timer: Only schedule soft timer on vcpu_block"), > the hooks were invoked only when KVM was about to "block", i.e. schedule > out the vCPU. The use case at the time was to schedule a timer in the > host based on the earliest timer in the guest in order to wake the > blocking vCPU when the emulated guest timer fired. Commit accb99bcd0ca > ("KVM: arm/arm64: Simplify bg_timer programming") reworked the timer > logic to be even more precise, by waiting until the vCPU was actually > scheduled out, and so move the timer logic from the (un)blocking hooks to > vcpu_load/put. > > In the meantime, the hooks gained usage for enabling vGIC v4 doorbells in > commit df9ba95993b9 ("KVM: arm/arm64: GICv4: Use the doorbell interrupt > as an unblocking source"), and added related logic for the VMCR in commit > 5eeaf10eec39 ("KVM: arm/arm64: Sync ICH_VMCR_EL2 back when about to block"). > > Finally, commit 07ab0f8d9a12 ("KVM: Call kvm_arch_vcpu_blocking early > into the blocking sequence") hoisted the (un)blocking hooks so that they > wrapped KVM's halt-polling logic in addition to the core "block" logic. > > In other words, the original need for arch hooks to take action _only_ > in the block path is long since gone. > > Cc: Oliver Upton > Cc: Marc Zyngier > Signed-off-by: Sean Christopherson This needs a word on why kvm_psci_vcpu_suspend does not need the hooks. Or it needs to be changed to also use kvm_vcpu_wfi in the PSCI code, I don't know. Marc, can you review and/or advise? Thanks, Paolo > --- > arch/arm64/include/asm/kvm_emulate.h | 2 ++ > arch/arm64/kvm/arm.c | 52 +++++++++++++++++++--------- > arch/arm64/kvm/handle_exit.c | 3 +- > 3 files changed, 38 insertions(+), 19 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h > index fd418955e31e..de8b4f5922b7 100644 > --- a/arch/arm64/include/asm/kvm_emulate.h > +++ b/arch/arm64/include/asm/kvm_emulate.h > @@ -41,6 +41,8 @@ void kvm_inject_vabt(struct kvm_vcpu *vcpu); > void kvm_inject_dabt(struct kvm_vcpu *vcpu, unsigned long addr); > void kvm_inject_pabt(struct kvm_vcpu *vcpu, unsigned long addr); > > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu); > + > static __always_inline bool vcpu_el1_is_32bit(struct kvm_vcpu *vcpu) > { > return !(vcpu->arch.hcr_el2 & HCR_RW); > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 7838e9fb693e..1346f81b34df 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -359,27 +359,12 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) > > void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) > { > - /* > - * If we're about to block (most likely because we've just hit a > - * WFI), we need to sync back the state of the GIC CPU interface > - * so that we have the latest PMR and group enables. This ensures > - * that kvm_arch_vcpu_runnable has up-to-date data to decide > - * whether we have pending interrupts. > - * > - * For the same reason, we want to tell GICv4 that we need > - * doorbells to be signalled, should an interrupt become pending. > - */ > - preempt_disable(); > - kvm_vgic_vmcr_sync(vcpu); > - vgic_v4_put(vcpu, true); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu) > { > - preempt_disable(); > - vgic_v4_load(vcpu); > - preempt_enable(); > + > } > > void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > @@ -662,6 +647,39 @@ static void vcpu_req_sleep(struct kvm_vcpu *vcpu) > smp_rmb(); > } > > +/** > + * kvm_vcpu_wfi - emulate Wait-For-Interrupt behavior > + * @vcpu: The VCPU pointer > + * > + * Suspend execution of a vCPU until a valid wake event is detected, i.e. until > + * the vCPU is runnable. The vCPU may or may not be scheduled out, depending > + * on when a wake event arrives, e.g. there may already be a pending wake event. > + */ > +void kvm_vcpu_wfi(struct kvm_vcpu *vcpu) > +{ > + /* > + * Sync back the state of the GIC CPU interface so that we have > + * the latest PMR and group enables. This ensures that > + * kvm_arch_vcpu_runnable has up-to-date data to decide whether > + * we have pending interrupts, e.g. when determining if the > + * vCPU should block. > + * > + * For the same reason, we want to tell GICv4 that we need > + * doorbells to be signalled, should an interrupt become pending. > + */ > + preempt_disable(); > + kvm_vgic_vmcr_sync(vcpu); > + vgic_v4_put(vcpu, true); > + preempt_enable(); > + > + kvm_vcpu_block(vcpu); > + kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + > + preempt_disable(); > + vgic_v4_load(vcpu); > + preempt_enable(); > +} > + > static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) > { > return vcpu->arch.target >= 0; > diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c > index 275a27368a04..4794563a506b 100644 > --- a/arch/arm64/kvm/handle_exit.c > +++ b/arch/arm64/kvm/handle_exit.c > @@ -95,8 +95,7 @@ static int kvm_handle_wfx(struct kvm_vcpu *vcpu) > } else { > trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false); > vcpu->stat.wfi_exit_stat++; > - kvm_vcpu_block(vcpu); > - kvm_clear_request(KVM_REQ_UNHALT, vcpu); > + kvm_vcpu_wfi(vcpu); > } > > kvm_incr_pc(vcpu); >