From patchwork Tue Jun 8 15:48:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 12307295 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=-16.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,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 52ABFC47082 for ; Tue, 8 Jun 2021 15:57:53 +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 20669610A2 for ; Tue, 8 Jun 2021 15:57:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 20669610A2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@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-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=K0ZvRjp9flhLLSGXiBcVKdJ7v8p3W5vVS59fOAQB0oA=; b=c40W9fmoyX8ARY xY+ActvyDPZH9HZV1Z6yTV6kNcupoPMsTDJIi+rAnkz0I6rRuQw0jlWUuVx6PCGUUpRMeYAeGNDf8 OkuAlrOftSADxNl4C/T0FNP0Al0OPj2ydslKKOA2f5pFwvZTHimYIgPHwLutchg1X1MdWu5oq1C2j EPwn5AMlXCSSpyl8SU2NiQWepwCNUPgluFLBHxvfWd8KmWk90YC8HtMWGMULKiBtuQ0R0mrsG3YCc EEkdLtddsKp/YSOmM3x5QPQ1K0j8sNYGncy8yJ4l8HZZT/ZSTTuI+59JEuVw3OiXenOsytWPqMs85 fI1YGNaWPDnhcpkz2yOw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1lqe4u-009OZW-F7; Tue, 08 Jun 2021 15:55:45 +0000 Received: from mail-wr1-f46.google.com ([209.85.221.46]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1lqe4H-009ONl-DL for linux-arm-kernel@lists.infradead.org; Tue, 08 Jun 2021 15:55:07 +0000 Received: by mail-wr1-f46.google.com with SMTP id e11so11936680wrg.3 for ; Tue, 08 Jun 2021 08:55:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6VUTi6r0zGjvD+ox5LbEVdcu7SOJ3CxYqosX/6quRvc=; b=iZxQMnxOlNisd6FMWcutFJg9ihWBRrP18+4KoCR7isLbBgJRjV2h9bm7ro5tM0SuA5 MKA963p2Fjmaerhs0VLNc3mXjmVJbt4LXKOds3/hhLBfPZTNhM4ALrICmh9wRHLm3mDj 9X0bwod0yko2OzqVbHqT6E91S1tWi3hL1+YyfpA/AefIXhN0izWgnv2m4AsrQM4gTRFb XPGRIVmgyNb6TPkqf5zH5+OpFuMLdvJItw67EM2UPkwxqiE40pkNyBxiSgrBs7QNMC1Q UqgLxRznUGNuKj9K/rYCy6GTXUiiCElOBKA3oqupM1W8sLyaqix9NLGflqG3lQaM8Z05 rHZg== 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:mime-version:content-transfer-encoding; bh=6VUTi6r0zGjvD+ox5LbEVdcu7SOJ3CxYqosX/6quRvc=; b=AZUJKC2S9vpLkmRGBRE0O4+S+zbQHDncJtkhi7wNVoWaRwxr5ePDinqCSt945uzgLM qMOyhtZ7v4SQ1GpA1Z4bmoPoNsBM8331xvXaF/yaIQpJXkbY0i3MmJ5DpVYZ9ftM2ZOT K84eRlOVkyCZ3gOtWvs+3iKXPdacowfSstFHSGDRcS20xxymkMru5ijB1yXvdwebsevv oXezcPdjSOxxy8rNNCwQdkcxmHdt85rVwdY4JfY2gSrvhXbZAl93ZtDmCAdD3V88HIOs uDNino3qrK03NBwDamrujam25WYfNbHGg8jmv4Npi3jl3fmI5NPDgFi7WV5ugbD8DzaG Rmzg== X-Gm-Message-State: AOAM533tQlaXqH8IJwzx5xHy1C/p2MiMJ0cRQMJ0N33ijMxWOsJN8cAI ozZTkGF9QNtiMqVCxqaEb24Ynw== X-Google-Smtp-Source: ABdhPJy61JSG9YYU4eU/zql91JN1YArLPTW12IIWCRmtkz+g9RAUJUbtnqHF/W7eyUKs8LNMBHfbCw== X-Received: by 2002:adf:e50b:: with SMTP id j11mr22681729wrm.377.1623167643627; Tue, 08 Jun 2021 08:54:03 -0700 (PDT) Received: from localhost.localdomain (adsl-84-226-111-173.adslplus.ch. [84.226.111.173]) by smtp.gmail.com with ESMTPSA id l31sm3314180wms.16.2021.06.08.08.54.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Jun 2021 08:54:03 -0700 (PDT) From: Jean-Philippe Brucker To: maz@kernel.org Cc: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, corbet@lwn.net, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, lorenzo.pieralisi@arm.com, salil.mehta@huawei.com, shameerali.kolothum.thodi@huawei.com, jonathan.cameron@huawei.com, Jean-Philippe Brucker Subject: [RFC PATCH 3/5] KVM: arm64: Allow userspace to request WFI Date: Tue, 8 Jun 2021 17:48:04 +0200 Message-Id: <20210608154805.216869-4-jean-philippe@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210608154805.216869-1-jean-philippe@linaro.org> References: <20210608154805.216869-1-jean-philippe@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210608_085505_480849_63376F3C X-CRM114-Status: GOOD ( 16.49 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org To help userspace implement PSCI CPU_SUSPEND, allow setting the "HALTED" MP state to request a WFI before returning to the guest. Userspace won't obtain a HALTED mp_state from a KVM_GET_MP_STATE call unless they set it themselves. When set by KVM, to handle wfi or CPU_SUSPEND, it is consumed before returning to userspace. Signed-off-by: Jean-Philippe Brucker --- Documentation/virt/kvm/api.rst | 15 +++++++++------ include/uapi/linux/kvm.h | 1 + arch/arm64/kvm/arm.c | 11 ++++++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 7fcb2fd38f42..e4fe7fb60d5d 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -1416,8 +1416,8 @@ Possible values are: which has not yet received an INIT signal [x86] KVM_MP_STATE_INIT_RECEIVED the vcpu has received an INIT signal, and is now ready for a SIPI [x86] - KVM_MP_STATE_HALTED the vcpu has executed a HLT instruction and - is waiting for an interrupt [x86] + KVM_MP_STATE_HALTED the vcpu has executed a HLT/WFI instruction + and is waiting for an interrupt [x86,arm64] KVM_MP_STATE_SIPI_RECEIVED the vcpu has just received a SIPI (vector accessible via KVM_GET_VCPU_EVENTS) [x86] KVM_MP_STATE_STOPPED the vcpu is stopped [s390,arm/arm64] @@ -1435,8 +1435,9 @@ these architectures. For arm/arm64: ^^^^^^^^^^^^^^ -The only states that are valid are KVM_MP_STATE_STOPPED and -KVM_MP_STATE_RUNNABLE which reflect if the vcpu is paused or not. +Valid states are KVM_MP_STATE_STOPPED and KVM_MP_STATE_RUNNABLE which reflect +if the vcpu is paused or not. If KVM_CAP_ARM_MP_HALTED is present, state +KVM_MP_STATE_HALTED is also valid. 4.39 KVM_SET_MP_STATE --------------------- @@ -1457,8 +1458,10 @@ these architectures. For arm/arm64: ^^^^^^^^^^^^^^ -The only states that are valid are KVM_MP_STATE_STOPPED and -KVM_MP_STATE_RUNNABLE which reflect if the vcpu should be paused or not. +Valid states are KVM_MP_STATE_STOPPED and KVM_MP_STATE_RUNNABLE which reflect +if the vcpu should be paused or not. If KVM_CAP_ARM_MP_HALTED is present, +KVM_MP_STATE_HALTED can be set, to wait for interrupts targeted at the vcpu +before running it. 4.40 KVM_SET_IDENTITY_MAP_ADDR ------------------------------ diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 79d9c44d1ad7..06ba64c49737 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1083,6 +1083,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_SGX_ATTRIBUTE 196 #define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197 #define KVM_CAP_PTP_KVM 198 +#define KVM_CAP_ARM_MP_HALTED 199 #ifdef KVM_CAP_IRQ_ROUTING diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index d8cbaa0373c7..d6ad977fea5f 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -207,6 +207,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_SET_GUEST_DEBUG: case KVM_CAP_VCPU_ATTRIBUTES: case KVM_CAP_PTP_KVM: + case KVM_CAP_ARM_MP_HALTED: r = 1; break; case KVM_CAP_SET_GUEST_DEBUG2: @@ -469,6 +470,9 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, case KVM_MP_STATE_RUNNABLE: vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; break; + case KVM_MP_STATE_HALTED: + kvm_arm_vcpu_suspend(vcpu); + break; case KVM_MP_STATE_STOPPED: kvm_arm_vcpu_power_off(vcpu); break; @@ -699,7 +703,12 @@ static void check_vcpu_requests(struct kvm_vcpu *vcpu) preempt_enable(); } - if (kvm_check_request(KVM_REQ_SUSPEND, vcpu)) { + /* + * Check mp_state again in case userspace changed their mind + * after requesting suspend. + */ + if (kvm_check_request(KVM_REQ_SUSPEND, vcpu) && + vcpu->arch.mp_state == KVM_MP_STATE_HALTED) { if (!irq_pending) { kvm_vcpu_block(vcpu); kvm_clear_request(KVM_REQ_UNHALT, vcpu);