Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp1862539pxm; Thu, 24 Feb 2022 10:46:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJxDoDUss7O14D656MyA1ZX2RQwlUEd+oVi4F5D6fEAh9kLQJC65h6uZe+GI9mYmOFWGeTVv X-Received: by 2002:a17:906:1995:b0:6ce:6b78:f9ec with SMTP id g21-20020a170906199500b006ce6b78f9ecmr3336245ejd.459.1645728409071; Thu, 24 Feb 2022 10:46:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645728409; cv=none; d=google.com; s=arc-20160816; b=I8yIT1ixY2dIdFKbyMkZzswtRt5w+S5MXWyNeVhG3tuBxyZuL4Hh3Opkk2ArOpYtpo Grq2thsbtF/8I7yTlisMVdAae/RaIebWqjgYJdnT0+ft0B5rKm3OCuAIwru2Aq5FGyrG QyuIvnw07seWBVE8oOkPsVWE+FVaDHZzOpWYXp+EugOC/Tah9oKb8zD8O+TUQK/zriUA OevJ7HqkKMVIsvsp4oQdMEH9zmkcWfcnrKOTH+TPtnuWO+TzIGxL8OirkJKJw0WPZVRT 4mXXoQmQZ/gfgGSZ/S2lDlh0H7FSCH9jyFug9YCKye7RejDzKECX5tFovOMnHysPIibR zL/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=zwE5cEiZetqja2vTCL0nln/ypguSZC8fkvH9hAsICEY=; b=k9zUyS/n6J8WiLL3iobnON8bzQSJz7zQimiNR8GVGuTo2DmoAnvuXtL2S5XXlDPmEC Upa+e5hUBTLBmaBW6btOzysW4zaO/qPxZi+49Rd3+alSZ/M3D8zpU6SUMfkhKne1AN1i qoBYboT6X8HA5ZnBWPl1VzhbZRA7U+pQEoqqN+sECS2+tMhAXYHeKSXZ2wkkzMDFWIYU kDsbyMt5vhCe0rmmasf+aZCfnnfw1DONhugxDv+ft9kRjDqMO3Rub5ds/jjKfEKl8vca tbD0JMipufX73p14ZhkTvtE5w/ltxWbh1h9ByefBi2GY4N+emq1tYFYQ6Bp9OzlowSn/ i5vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=LH+vDy+8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oy15-20020a170907104f00b006d0c17b592esi150219ejb.37.2022.02.24.10.46.24; Thu, 24 Feb 2022 10:46:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=LH+vDy+8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231636AbiBXR0z (ORCPT + 99 others); Thu, 24 Feb 2022 12:26:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230505AbiBXR0m (ORCPT ); Thu, 24 Feb 2022 12:26:42 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EFA7278C9D for ; Thu, 24 Feb 2022 09:26:11 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id w3-20020a17090ac98300b001b8b914e91aso1765824pjt.0 for ; Thu, 24 Feb 2022 09:26:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zwE5cEiZetqja2vTCL0nln/ypguSZC8fkvH9hAsICEY=; b=LH+vDy+8ljzpicztnBAsbYWVN/aATE2Kmw8jVkRvb5AmwHm3Ky1fKJ4iIhhozC0xfN 00nBusB/t7F5cZwHxAuierFsQrUSZ3rCPuoDVxm8QuFjcgWpF32iwuXO6dGgAKY+D4Nk FvKJRHLNE5tNX0D89rzPT/YTI4nLxoktj+rfcCckFSljNttUEvkXz9xJ5EhnyKA518Pj Dj0Yr70wBcWcRbOGY6FVpu2plko5nX7djwY45eKlfm2KgjR2xayqTAlhashQB/NxPCEY oPZM50Y73mowM3F4rMjiF4pcYJIRtPl4PjMaZ52lAMHvyDQC7K2GQNNn/+3C38/C9HEm Cw0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zwE5cEiZetqja2vTCL0nln/ypguSZC8fkvH9hAsICEY=; b=mV0FSUccImGQThRRuv0gpNkLrW+YOqCr71plFroOb0JfFnknYtzFzgLyQmRRF8zers kBqZH6ByTq2zJUPOmjuaKPS9ey6V2sxkmuDaIUe1DaKLbkTpQmUWU8JnDvKt2/meCMCU JjYqo6F4pV2yEojV7A19RzFcy3+fcpPisFhgDKzXY9Nm0x0G1XlBs7XXVs/Sxbapcpl5 ryoyEtSv99ok2JR6DVR5mUWdhlTeEreDQRkaS/rgu5NHC57SNUWC/9X81bhPEoNGiZ6T LGalbdsIiU85kHRVT3WDhaJnAtZgrHUBo2a1lvak5+GLhpMj20azqyaju3ME+C67VJm7 2s7w== X-Gm-Message-State: AOAM530pqxtXIDHXsetV0k/D9wWHBvMphQnnrXT6S/hSJsjSDN9LG00A 2qG5dBgQdpeu1Fut5AFH0VTCAKFAQBCy X-Received: from rananta-virt.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1bcc]) (user=rananta job=sendgmr) by 2002:a17:902:6902:b0:14d:6aa4:f3f5 with SMTP id j2-20020a170902690200b0014d6aa4f3f5mr3575116plk.20.1645723571006; Thu, 24 Feb 2022 09:26:11 -0800 (PST) Date: Thu, 24 Feb 2022 17:25:48 +0000 In-Reply-To: <20220224172559.4170192-1-rananta@google.com> Message-Id: <20220224172559.4170192-3-rananta@google.com> Mime-Version: 1.0 References: <20220224172559.4170192-1-rananta@google.com> X-Mailer: git-send-email 2.35.1.574.g5d30c73bfb-goog Subject: [PATCH v4 02/13] KVM: arm64: Introduce KVM_CAP_ARM_REG_SCOPE From: Raghavendra Rao Ananta To: Marc Zyngier , Andrew Jones , James Morse , Alexandru Elisei , Suzuki K Poulose Cc: Paolo Bonzini , Catalin Marinas , Will Deacon , Peter Shier , Ricardo Koller , Oliver Upton , Reiji Watanabe , Jing Zhang , Raghavendra Rao Anata , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org KVM_[GET|SET]_ONE_REG act on per-vCPU basis. Currently certain ARM64 registers, such as KVM_REG_ARM_SMCCC_ARCH_WORKAROUND_[1|2], are accessed via this interface even though the effect that they have are really per-VM. As a result, userspace could just waste cycles to read/write the same information for every vCPU that it spawns, only to realize that there's absolutely no change in the VM's state. The problem gets worse in proportion to the number of vCPUs created. As a result, to avoid this redundancy, introduce the capability KVM_CAP_ARM_REG_SCOPE. If enabled, KVM_GET_REG_LIST will advertise the registers that are VM-scoped by dynamically modifying the register encoding. KVM_REG_ARM_SCOPE_* helper macros are introduced to decode the same. By learning this, userspace can access such registers only once. Signed-off-by: Raghavendra Rao Ananta --- Documentation/virt/kvm/api.rst | 16 ++++++++++++++++ arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/include/uapi/asm/kvm.h | 6 ++++++ arch/arm64/kvm/arm.c | 13 +++++++------ include/uapi/linux/kvm.h | 1 + 5 files changed, 33 insertions(+), 6 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index a4267104db50..7e7b3439f540 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7561,3 +7561,19 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.34 KVM_CAP_ARM_REG_SCOPE +-------------------------- + +:Architectures: arm64 + +The capability, if enabled, amends the existing register encoding +with additional information to the userspace if a particular register +is scoped per-vCPU or per-VM via KVM_GET_REG_LIST. KVM provides +KVM_REG_ARM_SCOPE_* helper macros to decode the same. Userspace can +use this information from the register encoding to access a VM-scopped +regiser only once, as opposed to accessing it for every vCPU for the +same effect. + +On the other hand, if the capability is disabled, all the registers +remain vCPU-scopped by default, retaining backward compatibility. diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 5bc01e62c08a..8132de6bd718 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -136,6 +136,9 @@ struct kvm_arch { /* Memory Tagging Extension enabled for the guest */ bool mte_enabled; + + /* Register scoping enabled for KVM registers */ + bool reg_scope_enabled; }; struct kvm_vcpu_fault_info { diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index b3edde68bc3e..c35447cc0e0c 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -199,6 +199,12 @@ struct kvm_arm_copy_mte_tags { #define KVM_REG_ARM_COPROC_MASK 0x000000000FFF0000 #define KVM_REG_ARM_COPROC_SHIFT 16 +/* Defines if a KVM register is one per-vCPU or one per-VM */ +#define KVM_REG_ARM_SCOPE_MASK 0x0000000010000000 +#define KVM_REG_ARM_SCOPE_SHIFT 28 +#define KVM_REG_ARM_SCOPE_VCPU 0 +#define KVM_REG_ARM_SCOPE_VM 1 + /* Normal registers are mapped as coprocessor 16. */ #define KVM_REG_ARM_CORE (0x0010 << KVM_REG_ARM_COPROC_SHIFT) #define KVM_REG_ARM_CORE_REG(name) (offsetof(struct kvm_regs, name) / sizeof(__u32)) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index ecc5958e27fe..107977c82c6c 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -81,26 +81,26 @@ int kvm_arch_check_processor_compat(void *opaque) int kvm_vm_ioctl_enable_cap(struct kvm *kvm, struct kvm_enable_cap *cap) { - int r; + int r = 0; if (cap->flags) return -EINVAL; switch (cap->cap) { case KVM_CAP_ARM_NISV_TO_USER: - r = 0; kvm->arch.return_nisv_io_abort_to_user = true; break; case KVM_CAP_ARM_MTE: mutex_lock(&kvm->lock); - if (!system_supports_mte() || kvm->created_vcpus) { + if (!system_supports_mte() || kvm->created_vcpus) r = -EINVAL; - } else { - r = 0; + else kvm->arch.mte_enabled = true; - } mutex_unlock(&kvm->lock); break; + case KVM_CAP_ARM_REG_SCOPE: + WRITE_ONCE(kvm->arch.reg_scope_enabled, true); + break; default: r = -EINVAL; break; @@ -209,6 +209,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_REG_SCOPE: r = 1; break; case KVM_CAP_SET_GUEST_DEBUG2: diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 5191b57e1562..c4fe81ed9ee6 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1134,6 +1134,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_VM_GPA_BITS 207 #define KVM_CAP_XSAVE2 208 #define KVM_CAP_SYS_ATTRIBUTES 209 +#define KVM_CAP_ARM_REG_SCOPE 210 #ifdef KVM_CAP_IRQ_ROUTING -- 2.35.1.473.g83b2b277ed-goog