From patchwork Thu Feb 15 15:29:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 201586 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp489405dyb; Thu, 15 Feb 2024 07:49:21 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVtNfzfFRZ4pv741AgR2xGN9H/k/n+fWtEvlhE1XFERr4s85D5yhgNFK4Pv84BCk0GaDTmUF3P2Y96p73Wz7QCuw+tXXw== X-Google-Smtp-Source: AGHT+IGm9Z5/lNoPSVSsBieYfqnOjV1dZIWPsaeTLhvJhuRRqHmPd/8OhBzaC/PV8wOyusotCJKs X-Received: by 2002:a17:906:7c46:b0:a3b:e975:c530 with SMTP id g6-20020a1709067c4600b00a3be975c530mr1713916ejp.51.1708012161471; Thu, 15 Feb 2024 07:49:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708012161; cv=pass; d=google.com; s=arc-20160816; b=eljA3lS3gyGXsyslOsG2Mps3uW/AYkJH3yLRpWBaFOKtfmnq7FD5N/tcMANHO2vHWy jnju/Azt/zACUbEdcWcHCgjCy3ohAfp5+hOtwFcVeItnGaVKRbqhK1WFDGcPlYzMyjMU c0gaim3tAebTXCuS74L67QQhIVXYB3Hdc06XJWcxDYhYN+b84HKllURA2eI97aZNin60 r5+GgRDa0jyc0pTryFO/3ntNsppcWUH+S2vndPeZxf5DDF5ZtE4qFRV+DWpxr7nIQPJp lTi/cfGUohbOALJ3lV31l7ff2BsG0UkDv4aP5k5fp3I+FO+1JbFTDxmoHKA6qv7fE8IA HfDw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:to:from:dkim-signature; bh=hxa3aMnIMpALzs/BKRBU3KgvSe4qAfUuhmqmYo4D2xE=; fh=NrOggL3VZvuSFh5W9Wj2+kLgS4v0nogfnAWvBJoouXQ=; b=lvuFLXg9N1NkOqcyWsZI6NaerjE9ia4S+/mclwna9ieZ2CCJ17sqyDrP6WvD0LW7OJ rE15kGd0xrW1Ayf1BSxi35FIvVkTK8pfWtDWOyBgl7qVSRliq/te367HWXzUfgNLKdwW va5hrja3BYug3YpLIRif4MnHxBUrtAs5Bu+JlJ3pWjZol5l7GPdlIgEcAO8KQccUWpZZ 0BLAtf0MwtGsShf1TlcIIPA1wa0b64sYDdKD21QAM1Ex5AFFa0hN+ZMkaRoJdtR3Q5JK JgA86TFtZiOSmlOIYqzRXW8C1/Uf08Zohxw8ZCAUEcJsazSI1MX4tHijDRN/HriLEGYQ +vnw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=W0Xh2IpS; arc=pass (i=1 spf=pass spfdomain=xen.org dkim=pass dkdomain=xen.org); spf=pass (google.com: domain of linux-kernel+bounces-67226-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67226-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id kb16-20020a1709070f9000b00a3ba6fe0848si736571ejc.765.2024.02.15.07.49.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Feb 2024 07:49:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-67226-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=W0Xh2IpS; arc=pass (i=1 spf=pass spfdomain=xen.org dkim=pass dkdomain=xen.org); spf=pass (google.com: domain of linux-kernel+bounces-67226-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-67226-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 9AFC01F297BE for ; Thu, 15 Feb 2024 15:47:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 751EF1369A7; Thu, 15 Feb 2024 15:44:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xen.org header.i=@xen.org header.b="W0Xh2IpS" Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A371E1339A3; Thu, 15 Feb 2024 15:44:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=104.130.215.37 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708011875; cv=none; b=h+ACL5m69HkmuzSJklakPCCrpmlBRAwE7FGSr+emVKso81tLmybTGwY5NOgK29l+LfE3268upG8AzbQgduDg5nMnRvGLPOWdlyxzl4hRtBZvazq/vZ2XQChN9dimhKf8B1ZjwQ1fbmEw2zlKXzHBP+68yy/ol5WhQxDV9V6zfRY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708011875; c=relaxed/simple; bh=Iczx8uGS2pMyj32Isdver4gpjd/cFUuEvTlQrjXhYQY=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=raeH3/8ijctjRgmdg7H4vh3xZ2pASoVmUMTjKeVBgM1fZ+rLc+N3k6yO+18MjSDob3RHaos8kRVzwjMr2ZgIYVA09fQMWpMYC3HXL13sRdeTTIYezQaHJbvO975A21+HXaZx/kl+yJAJDa6HhqrmYTesXo6h5+RhlGG01rhDn7I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org; spf=pass smtp.mailfrom=xen.org; dkim=pass (1024-bit key) header.d=xen.org header.i=@xen.org header.b=W0Xh2IpS; arc=none smtp.client-ip=104.130.215.37 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xen.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xen.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From; bh=hxa3aMnIMpALzs/BKRBU3KgvSe4qAfUuhmqmYo4D2xE=; b=W0Xh2IpSD3cg7v7JGK4EqOEKRQ ZMUFy8cJV5euvEeXqI4CufAZmhF5H9Pn4ADhyeJUJKNLIrnC4pguiIniOZHBjWIqYRf1iynsOq0TF RckocucBwFdIl2b8bZ4dVccZfNF0p5rAy/S+uATEakbDix3oPozl9IE9U537MHV0cnbE=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1raduL-0001WG-LB; Thu, 15 Feb 2024 15:44:17 +0000 Received: from 54-240-197-226.amazon.com ([54.240.197.226] helo=REM-PW02S00X.ant.amazon.com) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1radhG-00089r-RI; Thu, 15 Feb 2024 15:30:47 +0000 From: Paul Durrant To: Paolo Bonzini , Jonathan Corbet , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , David Hildenbrand , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Sven Schnelle , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , David Woodhouse , Paul Durrant , Shuah Khan , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-s390@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH v13 13/21] KVM: x86/xen: allow vcpu_info to be mapped by fixed HVA Date: Thu, 15 Feb 2024 15:29:08 +0000 Message-Id: <20240215152916.1158-14-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240215152916.1158-1-paul@xen.org> References: <20240215152916.1158-1-paul@xen.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790980559876474368 X-GMAIL-MSGID: 1790980559876474368 From: Paul Durrant If the guest does not explicitly set the GPA of vcpu_info structure in memory then, for guests with 32 vCPUs or fewer, the vcpu_info embedded in the shared_info page may be used. As described in a previous commit, the shared_info page is an overlay at a fixed HVA within the VMM, so in this case it also more optimal to activate the vcpu_info cache with a fixed HVA to avoid unnecessary invalidation if the guest memory layout is modified. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: David Woodhouse Cc: Sean Christopherson Cc: Paolo Bonzini Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: x86@kernel.org v13: - Patch title change. v8: - Re-base. v5: - New in this version. --- Documentation/virt/kvm/api.rst | 26 +++++++++++++++++++++----- arch/x86/kvm/xen.c | 34 ++++++++++++++++++++++++++++------ include/uapi/linux/kvm.h | 3 +++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 3372be85b335..bd93cafd3e4e 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -5523,11 +5523,12 @@ KVM_XEN_ATTR_TYPE_SHARED_INFO Sets the guest physical frame number at which the Xen shared_info page resides. Note that although Xen places vcpu_info for the first 32 vCPUs in the shared_info page, KVM does not automatically do so - and instead requires that KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO be used - explicitly even when the vcpu_info for a given vCPU resides at the - "default" location in the shared_info page. This is because KVM may - not be aware of the Xen CPU id which is used as the index into the - vcpu_info[] array, so may know the correct default location. + and instead requires that KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO or + KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA be used explicitly even when + the vcpu_info for a given vCPU resides at the "default" location + in the shared_info page. This is because KVM may not be aware of + the Xen CPU id which is used as the index into the vcpu_info[] + array, so may know the correct default location. Note that the shared_info page may be constantly written to by KVM; it contains the event channel bitmap used to deliver interrupts to @@ -5649,6 +5650,21 @@ KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO on dirty logging. Setting the gpa to KVM_XEN_INVALID_GPA will disable the vcpu_info. +KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA + If the KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA flag is also set in the + Xen capabilities, then this attribute may be used to set the + userspace address of the vcpu_info for a given vCPU. It should + only be used when the vcpu_info resides at the "default" location + in the shared_info page. In this case it is safe to assume the + userspace address will not change, because the shared_info page is + an overlay on guest memory and remains at a fixed host address + regardless of where it is mapped in guest physical address space + and hence unnecessary invalidation of an internal cache may be + avoided if the guest memory layout is modified. + If the vcpu_info does not reside at the "default" location then + it is not guaranteed to remain at the same host address and + hence the aforementioned cache invalidation is required. + KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO Sets the guest physical address of an additional pvclock structure for a given vCPU. This is typically used for guest vsyscall support. diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 6fb268e424fa..4e79cc68e0a9 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -784,20 +784,33 @@ int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) switch (data->type) { case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO: + case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA: /* No compat necessary here. */ BUILD_BUG_ON(sizeof(struct vcpu_info) != sizeof(struct compat_vcpu_info)); BUILD_BUG_ON(offsetof(struct vcpu_info, time) != offsetof(struct compat_vcpu_info, time)); - if (data->u.gpa == KVM_XEN_INVALID_GPA) { - kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache); - r = 0; - break; + if (data->type == KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO) { + if (data->u.gpa == KVM_XEN_INVALID_GPA) { + kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache); + r = 0; + break; + } + + r = kvm_gpc_activate(&vcpu->arch.xen.vcpu_info_cache, + data->u.gpa, sizeof(struct vcpu_info)); + } else { + if (data->u.hva == 0) { + kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache); + r = 0; + break; + } + + r = kvm_gpc_activate_hva(&vcpu->arch.xen.vcpu_info_cache, + data->u.hva, sizeof(struct vcpu_info)); } - r = kvm_gpc_activate(&vcpu->arch.xen.vcpu_info_cache, - data->u.gpa, sizeof(struct vcpu_info)); if (!r) kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); @@ -1026,6 +1039,15 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) r = 0; break; + case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA: + if (vcpu->arch.xen.vcpu_info_cache.active && + vcpu->arch.xen.vcpu_info_cache.gpa == KVM_XEN_INVALID_GPA) + data->u.hva = vcpu->arch.xen.vcpu_info_cache.uhva; + else + data->u.hva = 0; + r = 0; + break; + case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO: if (vcpu->arch.xen.vcpu_time_info_cache.active) data->u.gpa = vcpu->arch.xen.vcpu_time_info_cache.gpa; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index ac5caba313d1..d2665319db6e 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1809,6 +1809,7 @@ struct kvm_xen_vcpu_attr { union { __u64 gpa; #define KVM_XEN_INVALID_GPA ((__u64)-1) + __u64 hva; __u64 pad[8]; struct { __u64 state; @@ -1839,6 +1840,8 @@ struct kvm_xen_vcpu_attr { #define KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID 0x6 #define KVM_XEN_VCPU_ATTR_TYPE_TIMER 0x7 #define KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR 0x8 +/* Available with KVM_CAP_XEN_HVM / KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA */ +#define KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA 0x9 /* Secure Encrypted Virtualization command */ enum sev_cmd_id {