Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4659961pxj; Tue, 8 Jun 2021 21:32:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDtytuNDKUwnyKNSizfxhmLq2HAxXq2jgNcePz29SEE0UwDN8OygNkO2xWd0SpiTnLkNxi X-Received: by 2002:a05:6402:2689:: with SMTP id w9mr10595963edd.375.1623213129413; Tue, 08 Jun 2021 21:32:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623213129; cv=none; d=google.com; s=arc-20160816; b=ZdPElGAae0tWN+jI4yrYV1jKsSXhMfViZU1ZHJaNXRobrwr9+iyQSQzF6b5dL3DeIe qXMokvmAl7Vgj9x6IQtiMzHl7B+FiWblRElJpGxwLFKmYZjeN/aVRnCQkB0OyORZr7bQ /LWWhH/Exbk2fJJDSSvtt0A9LlIyvK0iDvidTYwzgK6hHMni7VBUeY+SBSeEkHvcacaT qWgSql1BWIfmyYciGCW/FhSREzHJYqnFw9FOINhHDjG3uOtz85TV3At1MorX7icIuoKD VzKLV2k90eEwBRSwd3BdK5x/xlaCd2+5hg2E4nJL/gu1SQbObb5qYG8fI864/zBAx6y3 oSMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=6VUTi6r0zGjvD+ox5LbEVdcu7SOJ3CxYqosX/6quRvc=; b=oGCZ1rZHatXC6hPiEb/5OYQUwFpb+mHMlieakTmq+UmBvLxutdS/UL5SJV4H5daXF6 7U6I7+m0Fm56r+B3lB7WVLAby3ubPuBMLJKt7ek0PCbJyFf9KK84e6VJMsA+o1MZwgw9 YAMa1thNBDEsVml9tP7iRCbqgRRQs/o4kACGpUtKrLn5PWBCwbKtX6LetVk9CZlvtDUS rsRWaX8ygmahuDbaCr24KDOBz2lkiH60zZ0iKkfVHnpAFsFr+GkX8UwEARWy3xdTqOEF z/ItMVeu1zJjEQS6PtA709H/wndBhqRwYsa0pS5LvDqd+2iJVMlqVaMvnyO2ZkOcCViM nsmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iZxQMnxO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r18si1422257edd.158.2021.06.08.21.31.45; Tue, 08 Jun 2021 21:32:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iZxQMnxO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233071AbhFHP5L (ORCPT + 99 others); Tue, 8 Jun 2021 11:57:11 -0400 Received: from mail-wr1-f50.google.com ([209.85.221.50]:33675 "EHLO mail-wr1-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231548AbhFHP5K (ORCPT ); Tue, 8 Jun 2021 11:57:10 -0400 Received: by mail-wr1-f50.google.com with SMTP id a20so22194902wrc.0 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=hzLJuVs3OC/MD4zLlX0Eq8VJcL2iVld5rOLZbeudhzgU9Gv64vjpV8MChOApisT7om dl9QjgWrBxc2cAZzeKaWUvzfVIHSSGSAi5e67pZLM+xBxcpXoHVCwCMZTrSlf/3KB+DZ 8OIbWPZQ4u0nAsZbrxFq1aI9WOryF1kuf4WwtHw3UBb3x8GauX0V5Es1Xb1W40oC/MUi MngFvZWD1TKyV3men9Sh18wUe1LIfohAH1CZpzRmoX8wL7pnpWkzm2As7Wdwzk57C/bo 8U65zlkQFOXJiXw6BVu1d3kurwbnX2l7edQOOH4jsJjAqsx3MVmlZazvSYvu046f2QCT WDKQ== X-Gm-Message-State: AOAM531OiTBslNvJiaG99GGe91UeVXhbo5NS20/VZz+xWU1nLZ/VcHjG S7B2a/Qz4nYBikOXIOsOSi6pDQ== 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 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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); -- 2.31.1