From patchwork Mon Sep 18 11:21:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141517 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2879188vqi; Mon, 18 Sep 2023 11:51:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH5z4dXV2NbE5iRspESrQI8kKPn9k4WbLP7RW+ZO24xEHOcxgBFYs+N9LnUbbMPRsymoRbz X-Received: by 2002:a17:90a:714c:b0:26b:24ed:e0d9 with SMTP id g12-20020a17090a714c00b0026b24ede0d9mr8928194pjs.33.1695063092033; Mon, 18 Sep 2023 11:51:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695063092; cv=none; d=google.com; s=arc-20160816; b=xf+hNa4UYDCk+4SIUU9BIBVqzgp0YReIejI7+makHwflOA+Aqe9cel76zb01wGoxvm +PqN4hM1KBF923PlTk25REK03XPMq5FLVdXsWpneRcqlpl+SgA3DzvE5GChp/R0w8uc9 ULyKL3YgeeiETo599gKqPKoCnU2N/iXDJSMcpfh2mTVsE0rzkWGBWssto2vqgUoPfiiM hHPIyuuVBX+gW5fPd7+iRH3FOTvoaSC7q5dKZak7IZ5fkZl4Q4LCYtzx+jJGwv4t5k8T eCKAxn7AWkx/kEcqYrsJwIfwzXKwsR96c9wYsyfUtdvcb8//GFArC5qF2Ac1FNAg76O7 YchA== 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=fTbn2s70JU4n6q7zX5A96rrHyiVEJajbsq63guaOv5g=; fh=huad6iRJsyL9duZ/mEsnypNTRWjlSl25gjzGtGjMd5k=; b=itSym4Tb2AVXQdQf+vWM47F/SK1Z1BFzxO3Kz25/UGkA7cAHSzFYG88i2+nmMCAQeM f7gE/FoQ9uQSCEWmSo2s6fj6fydow5uf1BW5JNq2+7gNKVCtLB12pu1Msqtam/QwQWA7 0EfOhrs7wsXC6Q0ko8kjJDxMG2M5TszXqF9hQjM+aDC9mIlXDYEBc5G5fMvTSssGP+JQ 3yQ54139xm45t+4FOMRu23Q9JsmuiQIzR8BeKGVROE1i4S5qqU4fo7V3O1CV6ENhAlqW 2DRCuXqY5xvDr2NHtyryfx19GOMxRhm7FpD9IopCWbB0t6lwpDn9Vq19usmZ29c1XMBP i30Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=HaR9Wx4m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id w1-20020a17090ac98100b0026829a94587si8288439pjt.176.2023.09.18.11.51.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 11:51:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=HaR9Wx4m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 1597A806E3E8; Mon, 18 Sep 2023 04:23:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241571AbjIRLWr (ORCPT + 27 others); Mon, 18 Sep 2023 07:22:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51990 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240006AbjIRLWU (ORCPT ); Mon, 18 Sep 2023 07:22:20 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3184C3; Mon, 18 Sep 2023 04:22:14 -0700 (PDT) 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:Cc:To:From; bh=fTbn2s70JU4n6q7zX5A96rrHyiVEJajbsq63guaOv5g=; b=HaR9Wx4m0UWITRpcI3ReX3juWh t7c0zmSrvP4F/zqnGlXxs3AZSc4SkZTVJYWuzJW51K9H0gm645xjxd8oUWaK7YcK0Z9SNhqk036f0 ajgtVfrtdafxrd/R+IDgFCpXvxD+Q2x1DOQm+V10BInuCiqu+JLMzst8QfBE7R9wgd+g=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKN-00071p-J1; Mon, 18 Sep 2023 11:22:07 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKN-0005f3-Bc; Mon, 18 Sep 2023 11:22:07 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Paolo Bonzini Subject: [PATCH v2 01/12] KVM: pfncache: add a map helper function Date: Mon, 18 Sep 2023 11:21:37 +0000 Message-Id: <20230918112148.28855-2-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:27 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777402476520799928 X-GMAIL-MSGID: 1777402476520799928 From: Paul Durrant We have an unmap helper but mapping is open-coded. Arguably this is fine because mapping is done in only one place, hva_to_pfn_retry(), but adding the helper does make that function more readable. No functional change intended. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Paolo Bonzini --- virt/kvm/pfncache.c | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 2d6aba677830..0f36acdf577f 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -96,17 +96,28 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) } EXPORT_SYMBOL_GPL(kvm_gpc_check); -static void gpc_unmap_khva(kvm_pfn_t pfn, void *khva) +static void *gpc_map(kvm_pfn_t pfn) +{ + if (pfn_valid(pfn)) + return kmap(pfn_to_page(pfn)); +#ifdef CONFIG_HAS_IOMEM + else + return memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB); +#endif +} + +static void gpc_unmap(kvm_pfn_t pfn, void *khva) { /* Unmap the old pfn/page if it was mapped before. */ - if (!is_error_noslot_pfn(pfn) && khva) { - if (pfn_valid(pfn)) - kunmap(pfn_to_page(pfn)); + if (is_error_noslot_pfn(pfn) || !khva) + return; + + if (pfn_valid(pfn)) + kunmap(pfn_to_page(pfn)); #ifdef CONFIG_HAS_IOMEM - else - memunmap(khva); + else + memunmap(khva); #endif - } } static inline bool mmu_notifier_retry_cache(struct kvm *kvm, unsigned long mmu_seq) @@ -175,7 +186,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) * the existing mapping and didn't create a new one. */ if (new_khva != old_khva) - gpc_unmap_khva(new_pfn, new_khva); + gpc_unmap(new_pfn, new_khva); kvm_release_pfn_clean(new_pfn); @@ -193,15 +204,11 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) * too must be done outside of gpc->lock! */ if (gpc->usage & KVM_HOST_USES_PFN) { - if (new_pfn == gpc->pfn) { + if (new_pfn == gpc->pfn) new_khva = old_khva; - } else if (pfn_valid(new_pfn)) { - new_khva = kmap(pfn_to_page(new_pfn)); -#ifdef CONFIG_HAS_IOMEM - } else { - new_khva = memremap(pfn_to_hpa(new_pfn), PAGE_SIZE, MEMREMAP_WB); -#endif - } + else + new_khva = gpc_map(new_pfn); + if (!new_khva) { kvm_release_pfn_clean(new_pfn); goto out_error; @@ -326,7 +333,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, mutex_unlock(&gpc->refresh_lock); if (unmap_old) - gpc_unmap_khva(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva); return ret; } @@ -412,7 +419,7 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) list_del(&gpc->list); spin_unlock(&kvm->gpc_lock); - gpc_unmap_khva(old_pfn, old_khva); + gpc_unmap(old_pfn, old_khva); } } EXPORT_SYMBOL_GPL(kvm_gpc_deactivate); From patchwork Mon Sep 18 11:21:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141466 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2777262vqi; Mon, 18 Sep 2023 09:15:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFNctum9CZaqMyfrWoKYDZceVopyL0i/fvR7RLAxkcc+3d6sVOrUjZhS6G4c9SzXEChX+pQ X-Received: by 2002:a05:6a20:a110:b0:152:cb38:5b47 with SMTP id q16-20020a056a20a11000b00152cb385b47mr11130156pzk.55.1695053721309; Mon, 18 Sep 2023 09:15:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695053721; cv=none; d=google.com; s=arc-20160816; b=YA15efaRupbENud0iG0ZEnTJsZSjlbnkkKuyR7w4rG3AMf9PWoFrwyxAgklo7/TJgD jKQE67OEr3yqlC7Pu7HHOhNE52tZljPSKMe8JVJBFDD+2O6/BBXnoqicIT3q2rgbCMvc gUReNyzGE1Xw27wxd9aHwCqN9RCjKpu3FImM4ZfW1AW8Hy8ndvmTkZwiw6Ye6L79Wq7f ROrnJgRJsUzbhf/WFqd9zjTUnieoCxzf2X4sVLW1YkFY1BcbXFEY31haUYXfxexx8WAt hLD8oy/lcire+97liaoAizrMpvv8dlokQ83SYp7okxLUzauXjhDOjNFlMblQbwYnu5HE Ef3A== 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=T7LplA95gOG/1c7Ox89IpkSCjofwoWKCL2CQHjB9sbY=; fh=F/y/rQAitAoVhyEvV7npxQC+pmk+LQOxsxEGW4WYgok=; b=hvZztFNaBzZ8/XbAod0gkgN3gNpmmNobp9kLQ4sQ3XreApNTUe6TgfdG3d8QLd3P6Y 2AGBLyrBW19VTgSQUNml/rQLjn5hbITy5wlrje6PHOGilMtUbY0+5438xvxsLeGozQMl fvryjRARGtAr5yWcf5fq7iZGXqvDeXLx8s8EMjZzXrjAhPdfcp14J9Rv3bV3sgsVflsz bQMDLanhq9TiBhgLFWXHLAg706R9Cfav9ii59Y80v9A4FD//icxikrUeNWyn/7SYnj2y fwKHZ3mpRntbYJTUY1jcgehvvaPAssgnzZ+wtLq6I64C/dVbQ1+duN1K5bOYlD5m8t07 fOyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=cf3qOUgR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id a9-20020a63d209000000b0056a77ae0b55si8125111pgg.458.2023.09.18.09.15.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 09:15:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=cf3qOUgR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 5D0E8808458A; Mon, 18 Sep 2023 04:23:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241652AbjIRLW4 (ORCPT + 27 others); Mon, 18 Sep 2023 07:22:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240227AbjIRLWV (ORCPT ); Mon, 18 Sep 2023 07:22:21 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19291E1; Mon, 18 Sep 2023 04:22:15 -0700 (PDT) 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:Cc:To:From; bh=T7LplA95gOG/1c7Ox89IpkSCjofwoWKCL2CQHjB9sbY=; b=cf3qOUgR9uYfqX+LffKk4VEyuI FL99YDrxJOatEmrCV/RiHJj8kMVnJZ9tIz85Vl9FVcTqxkkhJCmflmvMYDaRzMO3QPopyOjr1CiMV TGu/kLs321LkZuuRuSQtgA1r5Bl9xIC2f9RNOiWcORMzAGs1DsXzPbpHWe+9GvTAVHh0=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKP-00071w-41; Mon, 18 Sep 2023 11:22:09 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKO-0005f3-SR; Mon, 18 Sep 2023 11:22:09 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v2 02/12] KVM: pfncache: add a mark-dirty helper Date: Mon, 18 Sep 2023 11:21:38 +0000 Message-Id: <20230918112148.28855-3-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:23 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777392651016691957 X-GMAIL-MSGID: 1777392651016691957 From: Paul Durrant At the moment pages are marked dirty by open-coded calls to mark_page_dirty_in_slot(), directly deferefencing the gpa and memslot from the cache. After a subsequent patch these may not always be set so add a helper now so that caller will protected from the need to know about this detail. NOTE: Pages are now marked dirty while the cache lock is held. This is to ensure that gpa and memslot are mutually consistent. Signed-off-by: Paul Durrant Reviewed-by: 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 --- arch/x86/kvm/x86.c | 2 +- arch/x86/kvm/xen.c | 13 ++++++------- include/linux/kvm_host.h | 7 +++++++ virt/kvm/pfncache.c | 6 ++++++ 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6c9c81e82e65..d669a8801265 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3137,7 +3137,7 @@ static void kvm_setup_guest_pvclock(struct kvm_vcpu *v, guest_hv_clock->version = ++vcpu->hv_clock.version; - mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); + kvm_gpc_mark_dirty(gpc); read_unlock_irqrestore(&gpc->lock, flags); trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 40edf4d1974c..33fddd29824b 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -430,14 +430,13 @@ static void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, bool atomic) smp_wmb(); } - if (user_len2) + if (user_len2) { + kvm_gpc_mark_dirty(gpc2); read_unlock(&gpc2->lock); + } + kvm_gpc_mark_dirty(gpc1); read_unlock_irqrestore(&gpc1->lock, flags); - - mark_page_dirty_in_slot(v->kvm, gpc1->memslot, gpc1->gpa >> PAGE_SHIFT); - if (user_len2) - mark_page_dirty_in_slot(v->kvm, gpc2->memslot, gpc2->gpa >> PAGE_SHIFT); } void kvm_xen_update_runstate(struct kvm_vcpu *v, int state) @@ -543,13 +542,13 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) : "0" (evtchn_pending_sel32)); WRITE_ONCE(vi->evtchn_upcall_pending, 1); } + + kvm_gpc_mark_dirty(gpc); read_unlock_irqrestore(&gpc->lock, flags); /* For the per-vCPU lapic vector, deliver it as MSI. */ if (v->arch.xen.upcall_vector) kvm_xen_inject_vcpu_vector(v); - - mark_page_dirty_in_slot(v->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); } int __kvm_xen_has_interrupt(struct kvm_vcpu *v) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index fb6c6109fdca..c71e8fbccaaf 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1367,6 +1367,13 @@ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len); */ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc); +/** + * kvm_gpc_mark_dirty - mark a cached page as dirty. + * + * @gpc: struct gfn_to_pfn_cache object. + */ +void kvm_gpc_mark_dirty(struct gfn_to_pfn_cache *gpc); + void kvm_sigset_activate(struct kvm_vcpu *vcpu); void kvm_sigset_deactivate(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 0f36acdf577f..b68ed7fa56a2 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -386,6 +386,12 @@ int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len) } EXPORT_SYMBOL_GPL(kvm_gpc_activate); +void kvm_gpc_mark_dirty(struct gfn_to_pfn_cache *gpc) +{ + mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); +} +EXPORT_SYMBOL_GPL(kvm_gpc_mark_dirty); + void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) { struct kvm *kvm = gpc->kvm; From patchwork Mon Sep 18 11:21:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141364 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2585364vqi; Mon, 18 Sep 2023 04:36:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IECzqpjelDi2NdxaSYgvHS2wYEssjyqX6O9a4Q5uJdyrcjsJI8W6upL1HeSE3wE+nfImoyu X-Received: by 2002:a05:6a00:24c8:b0:68f:b5a3:5ec6 with SMTP id d8-20020a056a0024c800b0068fb5a35ec6mr11179987pfv.0.1695036985590; Mon, 18 Sep 2023 04:36:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695036985; cv=none; d=google.com; s=arc-20160816; b=lutulWVrFEmfV5yamGCVqptRDetY7i//LlVDofdS7+rBHY6M4vY/CouOH/RRu+8gHa aynnHc2+A/27gIPXtnS2qpGuAWFLbOVl1yCswcr5/q7sDG1pFEEZr2rDdHqpA4J1XsEi tFfn5ZSpsjS94wZHswWX/2+Y8rlE5/KpWaPJF/aJu7U962vwolOVkTj5bQwuF2XvRB8q e4c6A2dz+lKNA3+2yp3EJ95SQuCAEYS9Oxa0EkjvFIX99esigQgr2xqveQCXJTxeUv/M EJuJrndrKDB6rE8sbcZobgogtQ/WkBrA95yFNE0u/OuSXZg8uKsuzSeVyyMYlF9yR+QD xV0w== 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=eeu+urJPOth738JvQxYBHofT60aPW+Hm97jfNgVIxEg=; fh=F/y/rQAitAoVhyEvV7npxQC+pmk+LQOxsxEGW4WYgok=; b=uY1qS/VVgFqzRzfJTmFHh9E8F+4F5bXG1qfjA0GpssANkz2Yh6AmG5FGcByo0mHMB0 wUXNNGheOX/xwk6IrZBcNIACGQwOm5mNEWxcDFdldLsSU1/uRueRSxd0qAasNwEPLnUs IUoJPkyS2CQcj9/3fG0SP81oRNfVt7sKmo2l9jECrDisc4awyOztGwraZRmi5cfLI6eh BJ9R2y1wvR/AfZccxCtC+IKnTe7RCElqi7dtENDZSFTiKLPcUiLOypdZsyxiFyYzo7+Q 3OzBm43K7Yx4RdEEQ4YBFEr+e+yuraRvgJqyg/IK+yRWUXndEa7lVEmg0fWqnjtDrTA1 QQKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=2otuMQJ9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id d15-20020a056a00198f00b0068ff3a3c9d0si8127237pfl.91.2023.09.18.04.36.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 04:36:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=2otuMQJ9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id EC1FE823D9FF; Mon, 18 Sep 2023 04:23:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241637AbjIRLWx (ORCPT + 27 others); Mon, 18 Sep 2023 07:22:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239235AbjIRLWU (ORCPT ); Mon, 18 Sep 2023 07:22:20 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C79594; Mon, 18 Sep 2023 04:22:14 -0700 (PDT) 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:Cc:To:From; bh=eeu+urJPOth738JvQxYBHofT60aPW+Hm97jfNgVIxEg=; b=2otuMQJ9l7urFjUNDUg876Pa8P IXNu6SokhCKqYQcNNck3/2b0i/Ydb5XaUUVgiErGetVRiJkkzqw9M4SaqtPrtmFE6Y6O18hO6eZMH 76uh6P1EhQpoZnZMQUrmVwUfhCtlFCIKES8jXbkBcIcEATDV/9ZuJRJUPbyM38CmeOoA=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKQ-000720-Kk; Mon, 18 Sep 2023 11:22:10 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKQ-0005f3-D1; Mon, 18 Sep 2023 11:22:10 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v2 03/12] KVM: pfncache: add a helper to get the gpa Date: Mon, 18 Sep 2023 11:21:39 +0000 Message-Id: <20230918112148.28855-4-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:35 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777375101977007698 X-GMAIL-MSGID: 1777375101977007698 From: Paul Durrant A subsequent patch will rename this field since it will become overloaded. To avoid churn in places that currently retrieve the gpa, add a helper for that purpose now. No functional change intended. Signed-off-by: Paul Durrant Reviewed-by: 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 --- arch/x86/kvm/xen.c | 15 ++++++++------- include/linux/kvm_host.h | 7 +++++++ virt/kvm/pfncache.c | 6 ++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 33fddd29824b..8e6fdcd7bb6e 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -261,8 +261,8 @@ static void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, bool atomic) * alignment (and the 32-bit ABI doesn't align the 64-bit integers * anyway, even if the overall struct had been 64-bit aligned). */ - if ((gpc1->gpa & ~PAGE_MASK) + user_len >= PAGE_SIZE) { - user_len1 = PAGE_SIZE - (gpc1->gpa & ~PAGE_MASK); + if ((kvm_gpc_gpa(gpc1) & ~PAGE_MASK) + user_len >= PAGE_SIZE) { + user_len1 = PAGE_SIZE - (kvm_gpc_gpa(gpc1) & ~PAGE_MASK); user_len2 = user_len - user_len1; } else { user_len1 = user_len; @@ -343,7 +343,7 @@ static void kvm_xen_update_runstate_guest(struct kvm_vcpu *v, bool atomic) * to the second page now because the guest changed to * 64-bit mode, the second GPC won't have been set up. */ - if (kvm_gpc_activate(gpc2, gpc1->gpa + user_len1, + if (kvm_gpc_activate(gpc2, kvm_gpc_gpa(gpc1) + user_len1, user_len2)) return; @@ -677,7 +677,8 @@ int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data) case KVM_XEN_ATTR_TYPE_SHARED_INFO: if (kvm->arch.xen.shinfo_cache.active) - data->u.shared_info.gfn = gpa_to_gfn(kvm->arch.xen.shinfo_cache.gpa); + data->u.shared_info.gfn = + gpa_to_gfn(kvm_gpc_gpa(&kvm->arch.xen.shinfo_cache)); else data->u.shared_info.gfn = KVM_XEN_INVALID_GFN; r = 0; @@ -955,7 +956,7 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) switch (data->type) { case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO: if (vcpu->arch.xen.vcpu_info_cache.active) - data->u.gpa = vcpu->arch.xen.vcpu_info_cache.gpa; + data->u.gpa = kvm_gpc_gpa(&vcpu->arch.xen.vcpu_info_cache); else data->u.gpa = KVM_XEN_INVALID_GPA; r = 0; @@ -963,7 +964,7 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) 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; + data->u.gpa = kvm_gpc_gpa(&vcpu->arch.xen.vcpu_time_info_cache); else data->u.gpa = KVM_XEN_INVALID_GPA; r = 0; @@ -975,7 +976,7 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) break; } if (vcpu->arch.xen.runstate_cache.active) { - data->u.gpa = vcpu->arch.xen.runstate_cache.gpa; + data->u.gpa = kvm_gpc_gpa(&vcpu->arch.xen.runstate_cache); r = 0; } break; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c71e8fbccaaf..4d8027fe9928 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1374,6 +1374,13 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc); */ void kvm_gpc_mark_dirty(struct gfn_to_pfn_cache *gpc); +/** + * kvm_gpc_gpa - retrieve the guest physical address of a cached mapping + * + * @gpc: struct gfn_to_pfn_cache object. + */ +gpa_t kvm_gpc_gpa(struct gfn_to_pfn_cache *gpc); + void kvm_sigset_activate(struct kvm_vcpu *vcpu); void kvm_sigset_deactivate(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index b68ed7fa56a2..17afbb464a70 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -386,6 +386,12 @@ int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len) } EXPORT_SYMBOL_GPL(kvm_gpc_activate); +gpa_t kvm_gpc_gpa(struct gfn_to_pfn_cache *gpc) +{ + return gpc->gpa; +} +EXPORT_SYMBOL_GPL(kvm_gpc_gpa); + void kvm_gpc_mark_dirty(struct gfn_to_pfn_cache *gpc) { mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); From patchwork Mon Sep 18 11:21:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141370 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2593991vqi; Mon, 18 Sep 2023 04:52:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWGb619DKnwSMoCrU3fcaKJLskGFb3xXY/YRVtNql8JfUJbTGzreBjrHLb9mo2Mefje/VZ X-Received: by 2002:a05:6a00:2396:b0:690:1720:aa83 with SMTP id f22-20020a056a00239600b006901720aa83mr12389486pfc.21.1695037928336; Mon, 18 Sep 2023 04:52:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695037928; cv=none; d=google.com; s=arc-20160816; b=NpllLvylDxXD3GWLhaBMOacLJMQKnxM+CXIkilqZg7BIjUZfZoiZwFXPqdLL5iGVHI MUu+wXrM51MrmFeIxJSnIOCqcJo4lAImDzKeoyPuCYV+Pc+McstDb/EghHiIF6GFCfhW 8r+134X5s00AJNgNB/wmaPXZGR+9UuVWUK/gYo8SqRjpKRQPLhO0e6m1IE4/U2WPLaCW MX6Vy2OzecjgUonPRrzN6/OW0ZRmv5O2wxpOTq/XOJx51okm9DKCkM/mjrY/3pgBfIsM ElqqhhLft+WnOikAWaoqTkE/8V2a+LbeYw7Y6iPeR5NuytJS119nGZsG/VtYI0owXx14 5foA== 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=aoVoeCi9/rtzNMORNn8BQRGqHJo92bkJdeOMKDpUkA4=; fh=huad6iRJsyL9duZ/mEsnypNTRWjlSl25gjzGtGjMd5k=; b=QK0cwMdGhv+d18vDLAsJIPeXy/W5eVC440ZezIs5wHK4yYbShOHQRBkMA55rL2C1R1 MPiv/JvmPLzHXcvBjLpXDRkJRPFXg3MtMmeQllKqi/8yZLg8bTnmUuWANG6MiNyWp6SW Mlc7qu81ImUnAOJRleGrJ4NcWeuAFbE01m8OiuLrojGto/WLG99d5NeMrzBe2WhaC6RX 6aSHE7URaghKj0T22Bqub+jWSbnXQCUtvWtgGyE7kPXHNqLAmwzqSibpse+s/IEJzTYZ RMMOacfnmVx7CjKxzpTJ1d/sRp81NOFW/TzX2Gmn7vx5lueOaCWq0J5PJb8lRyIkpreV dicg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=HQRKlLQA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id cb8-20020a056a00430800b0068fb5cbcef2si7652475pfb.117.2023.09.18.04.52.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 04:52:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=HQRKlLQA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 9175B806E3F0; Mon, 18 Sep 2023 04:23:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241674AbjIRLXA (ORCPT + 27 others); Mon, 18 Sep 2023 07:23:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240278AbjIRLWW (ORCPT ); Mon, 18 Sep 2023 07:22:22 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12FCAFD; Mon, 18 Sep 2023 04:22:16 -0700 (PDT) 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:Cc:To:From; bh=aoVoeCi9/rtzNMORNn8BQRGqHJo92bkJdeOMKDpUkA4=; b=HQRKlLQA9pV4dXmzirFalSxWvo Ptne6KDr0vA4qCsBazcZrftvz3XAjikB3wMXhiLEsvebYP4tJaYZegHDlRwo40bfkV81ywnTOH5eQ LySZJDwKQ2jFXzd4FaiavCA2qn1gSVvlOfHbWco64xJ4cWrIA0LVGQu0NtzkgyoW43SA=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKR-000722-Fg; Mon, 18 Sep 2023 11:22:11 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKR-0005f3-8O; Mon, 18 Sep 2023 11:22:11 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Paolo Bonzini Subject: [PATCH v2 04/12] KVM: pfncache: base offset check on khva rather than gpa Date: Mon, 18 Sep 2023 11:21:40 +0000 Message-Id: <20230918112148.28855-5-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:27 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777376090810557084 X-GMAIL-MSGID: 1777376090810557084 From: Paul Durrant After a subsequent patch, the gpa may not always be set whereas khva will (as long as the cache valid flag is also set). No functional change intended. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Paolo Bonzini --- virt/kvm/pfncache.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 17afbb464a70..37bcb4399780 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -83,15 +83,18 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) if (!gpc->active) return false; - if ((gpc->gpa & ~PAGE_MASK) + len > PAGE_SIZE) + if (gpc->generation != slots->generation) return false; - if (gpc->generation != slots->generation || kvm_is_error_hva(gpc->uhva)) + if (kvm_is_error_hva(gpc->uhva)) return false; if (!gpc->valid) return false; + if (offset_in_page(gpc->khva) + len > PAGE_SIZE) + return false; + return true; } EXPORT_SYMBOL_GPL(kvm_gpc_check); From patchwork Mon Sep 18 11:21:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141417 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2662535vqi; Mon, 18 Sep 2023 06:35:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnFJgQqkRQs8/KWE2lCGiMgTOixXQgprIbYb2l9nk8fNOH1p7nlsvU4z3ZaI5MuY+gEF5e X-Received: by 2002:a05:6a00:2394:b0:690:2ad9:145a with SMTP id f20-20020a056a00239400b006902ad9145amr8705797pfc.30.1695044106695; Mon, 18 Sep 2023 06:35:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695044106; cv=none; d=google.com; s=arc-20160816; b=sHbyPghe+V9aAihqaV7v82aUeNux16HA8Al24PusmI/OJA+3Kvac8mKPIJ8SGeErGD /Rq0Tejvdu/EvZ+4lHe6e3K+F4h+qz30zFiUMXwGkQaIAMhWieBLxytqJ8Za7I12dMMx c3JYrpK/lApm4ybWQVvPeRXgQa3u+I4pyW39b1ee/bci3JJTva+E0JBIEA8tjM/BFgAz vPr3NZcCKTYYsokAeQRXS5fcTRYNxSq336P+axq6DA8MFjciRbaTCIDKxCD4Ccr67BXa GlItcQ7Y8BSWoxc0KVmFPZjeiGBGgd9cXiYlxDx6MldckzYGtG56G4XumUSDsbSuRtcV W6Ig== 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=dozmN/ao3yu6ELwEpDJlaWpAf1h+h4oBUyHu8TvVQHM=; fh=garbXx6xThcytiY3nDlsqHTysvuL2ydFqtzT1pbUU/Y=; b=d3jac9rDg8nDRRA/O2nJ7sQedRJbc+G2RWgDZ2dI0lx6OtK+NeCeTYxud4b+SMHqB8 SbNJZX5RAsNp24tqJZyxJJjiNeROaTHvPzAkhzsH7rem38BilzfAGy4AZ8YSQzi7d0Za 3RMSZOBW58hxbiTYl0eHANBAav2aATXjuP127tvL16PYDtPgWhCREGUfKIxCL8JvlxzN c1/AEAPh9qr0pHW7jVVKSjkYjJBXfws5PhYiNE28dV2SGF47ERgt4UKMDnEz5eNIEM8R j1/po37YZ06kslCM7dFYeNIAxWS904wChWMhnXx3Tslr2C8+QLRvs/puuCNUPV3RhrN5 XzDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=ZksbXdWh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id b67-20020a633446000000b00565e9c3a308si8020840pga.788.2023.09.18.06.35.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 06:35:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=ZksbXdWh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 2C78581F1E92; Mon, 18 Sep 2023 04:23:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241667AbjIRLW6 (ORCPT + 27 others); Mon, 18 Sep 2023 07:22:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241193AbjIRLWW (ORCPT ); Mon, 18 Sep 2023 07:22:22 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F292E6; Mon, 18 Sep 2023 04:22:16 -0700 (PDT) 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:Cc:To:From; bh=dozmN/ao3yu6ELwEpDJlaWpAf1h+h4oBUyHu8TvVQHM=; b=ZksbXdWhOhlKalm7lYplZdTgLW SN+6Z/pM8igeyCxiV1j/S4caha2zpkX5Fb2j9x7kGpwnvjOIXA/5oNW+sW7qQDWZ0Vskhmc6El2ru ET1DTqXZ+8CvQUI1UMed5kLZS9z6bei4XLb+EYsiCi6zG7ZMeX9S6uoiszwEssNwuTLs=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKS-000728-Ay; Mon, 18 Sep 2023 11:22:12 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKS-0005f3-3p; Mon, 18 Sep 2023 11:22:12 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Paolo Bonzini , David Woodhouse Subject: [PATCH v2 05/12] KVM: pfncache: allow a cache to be activated with a fixed (userspace) HVA Date: Mon, 18 Sep 2023 11:21:41 +0000 Message-Id: <20230918112148.28855-6-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:31 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777382569226848358 X-GMAIL-MSGID: 1777382569226848358 From: Paul Durrant Some cached pages may actually be overlays on guest memory that have a fixed HVA within the VMM. It's pointless to invalidate such cached mappings if the overlay is moved so allow a cache to be activated directly with the HVA to cater for such cases. A subsequent patch will make use of this facility. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Paolo Bonzini Cc: David Woodhouse --- include/linux/kvm_host.h | 29 ++++++++++++++++ include/linux/kvm_types.h | 3 +- virt/kvm/pfncache.c | 73 ++++++++++++++++++++++++++++----------- 3 files changed, 84 insertions(+), 21 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4d8027fe9928..6823bae5c66c 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1321,6 +1321,22 @@ void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm, */ int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len); +/** + * kvm_gpc_activate_hva - prepare a cached kernel mapping and HPA for a given HVA. + * + * @gpc: struct gfn_to_pfn_cache object. + * @hva: userspace virtual address to map. + * @len: sanity check; the range being access must fit a single page. + * + * @return: 0 for success. + * -EINVAL for a mapping which would cross a page boundary. + * -EFAULT for an untranslatable guest physical address. + * + * The semantics of this function are the same as those of kvm_gpc_activate(). It + * merely bypasses a layer of address translation. + */ +int kvm_gpc_activate_hva(struct gfn_to_pfn_cache *gpc, unsigned long hva, unsigned long len); + /** * kvm_gpc_check - check validity of a gfn_to_pfn_cache. * @@ -1378,9 +1394,22 @@ void kvm_gpc_mark_dirty(struct gfn_to_pfn_cache *gpc); * kvm_gpc_gpa - retrieve the guest physical address of a cached mapping * * @gpc: struct gfn_to_pfn_cache object. + * + * @return: If the cache was activated with a fixed HVA then INVALID_GPA + * will be returned. */ gpa_t kvm_gpc_gpa(struct gfn_to_pfn_cache *gpc); +/** + * kvm_gpc_hva - retrieve the fixed host physical address of a cached mapping + * + * @gpc: struct gfn_to_pfn_cache object. + * + * @return: If the cache was activated with a guest physical address then + * 0 will be returned. + */ +unsigned long kvm_gpc_hva(struct gfn_to_pfn_cache *gpc); + void kvm_sigset_activate(struct kvm_vcpu *vcpu); void kvm_sigset_deactivate(struct kvm_vcpu *vcpu); diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index 6f4737d5046a..d49946ee7ae3 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -64,7 +64,7 @@ struct gfn_to_hva_cache { struct gfn_to_pfn_cache { u64 generation; - gpa_t gpa; + u64 addr; unsigned long uhva; struct kvm_memory_slot *memslot; struct kvm *kvm; @@ -77,6 +77,7 @@ struct gfn_to_pfn_cache { enum pfn_cache_usage usage; bool active; bool valid; + bool addr_is_gpa; }; #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 37bcb4399780..b3e3f7e38410 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -83,7 +83,7 @@ bool kvm_gpc_check(struct gfn_to_pfn_cache *gpc, unsigned long len) if (!gpc->active) return false; - if (gpc->generation != slots->generation) + if (gpc->addr_is_gpa && gpc->generation != slots->generation) return false; if (kvm_is_error_hva(gpc->uhva)) @@ -229,7 +229,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) gpc->valid = true; gpc->pfn = new_pfn; - gpc->khva = new_khva + (gpc->gpa & ~PAGE_MASK); + gpc->khva = new_khva + (gpc->addr & ~PAGE_MASK); /* * Put the reference to the _new_ pfn. The pfn is now tracked by the @@ -246,11 +246,11 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cache *gpc) return -EFAULT; } -static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, - unsigned long len) +static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, u64 addr, + unsigned long len, bool addr_is_gpa) { struct kvm_memslots *slots = kvm_memslots(gpc->kvm); - unsigned long page_offset = gpa & ~PAGE_MASK; + unsigned long page_offset = addr & ~PAGE_MASK; bool unmap_old = false; unsigned long old_uhva; kvm_pfn_t old_pfn; @@ -282,22 +282,34 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, old_khva = gpc->khva - offset_in_page(gpc->khva); old_uhva = gpc->uhva; - /* If the userspace HVA is invalid, refresh that first */ - if (gpc->gpa != gpa || gpc->generation != slots->generation || + /* + * If the address has changed, switched from guest to host (or vice + * versa), or it's a guest address and the memory slots have been + * updated, we need to refresh the userspace HVA. + */ + if (gpc->addr != addr || + gpc->addr_is_gpa != addr_is_gpa || + (addr_is_gpa && gpc->generation != slots->generation) || kvm_is_error_hva(gpc->uhva)) { - gfn_t gfn = gpa_to_gfn(gpa); + gpc->addr = addr; + gpc->addr_is_gpa = addr_is_gpa; - gpc->gpa = gpa; - gpc->generation = slots->generation; - gpc->memslot = __gfn_to_memslot(slots, gfn); - gpc->uhva = gfn_to_hva_memslot(gpc->memslot, gfn); + if (addr_is_gpa) { + gfn_t gfn = gpa_to_gfn(addr); - if (kvm_is_error_hva(gpc->uhva)) { - ret = -EFAULT; - goto out; + gpc->generation = slots->generation; + gpc->memslot = __gfn_to_memslot(slots, gfn); + gpc->uhva = gfn_to_hva_memslot(gpc->memslot, gfn); + } else { + gpc->uhva = addr & PAGE_MASK; } } + if (kvm_is_error_hva(gpc->uhva)) { + ret = -EFAULT; + goto out; + } + /* * If the userspace HVA changed or the PFN was already invalid, * drop the lock and do the HVA to PFN lookup again. @@ -343,7 +355,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, gpa_t gpa, int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsigned long len) { - return __kvm_gpc_refresh(gpc, gpc->gpa, len); + return __kvm_gpc_refresh(gpc, gpc->addr, len, gpc->addr_is_gpa); } EXPORT_SYMBOL_GPL(kvm_gpc_refresh); @@ -364,7 +376,8 @@ void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm, } EXPORT_SYMBOL_GPL(kvm_gpc_init); -int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len) +static int __kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, u64 addr, unsigned long len, + bool addr_is_gpa) { struct kvm *kvm = gpc->kvm; @@ -385,19 +398,39 @@ int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len) gpc->active = true; write_unlock_irq(&gpc->lock); } - return __kvm_gpc_refresh(gpc, gpa, len); + return __kvm_gpc_refresh(gpc, addr, len, addr_is_gpa); +} + +int kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, unsigned long len) +{ + return __kvm_gpc_activate(gpc, gpa, len, true); } EXPORT_SYMBOL_GPL(kvm_gpc_activate); gpa_t kvm_gpc_gpa(struct gfn_to_pfn_cache *gpc) { - return gpc->gpa; + return gpc->addr_is_gpa ? gpc->addr : INVALID_GPA; } EXPORT_SYMBOL_GPL(kvm_gpc_gpa); +int kvm_gpc_activate_hva(struct gfn_to_pfn_cache *gpc, unsigned long hva, unsigned long len) +{ + return __kvm_gpc_activate(gpc, hva, len, false); +} +EXPORT_SYMBOL_GPL(kvm_gpc_activate_hva); + +unsigned long kvm_gpc_hva(struct gfn_to_pfn_cache *gpc) +{ + return !gpc->addr_is_gpa ? gpc->addr : 0; +} +EXPORT_SYMBOL_GPL(kvm_gpc_hva); + void kvm_gpc_mark_dirty(struct gfn_to_pfn_cache *gpc) { - mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpc->gpa >> PAGE_SHIFT); + if (!gpc->addr_is_gpa) + return; + + mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpc->addr >> PAGE_SHIFT); } EXPORT_SYMBOL_GPL(kvm_gpc_mark_dirty); From patchwork Mon Sep 18 11:21:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141586 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2972983vqi; Mon, 18 Sep 2023 14:49:02 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGblfGh1zZnVJJR+fAF0gjX6gAvvkExWNupWNpFUS4yYsCo9vyOf7jTx2dGgQczTZoO23b3 X-Received: by 2002:a05:6a00:1351:b0:68e:45df:4648 with SMTP id k17-20020a056a00135100b0068e45df4648mr12153242pfu.23.1695073742144; Mon, 18 Sep 2023 14:49:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695073742; cv=none; d=google.com; s=arc-20160816; b=qCjXPd2Y7hSDUCwbdrD/Bn4whS93o4kHd3Ru6Nto9t5NUNcTZ4rBn4QwMxjvjIAPt4 OEAGSWoPigpHL6AJx79XOKZaIXVV0ncn/b7upnxOP8OlS4wqXj3j6qxOGLsMjLdozPGL vFV2fVgRtCD+5/EgpaVfI+MN5J5WnGaVEPlchXlBWO7/knVkKoBwsOiZa+xidtWLyBKw zF6tgUvKlYQXnIXEu/Ho8Isy3IwVUk9rnT6ZqZCcSta4oU55Nkhr4OLYB5RsqusM1Z3J Ywiv1Kd2v+9CMGBevPwtw3a8CVmogICsuHvNHTKNa8FSrUqk9pyy0lFjndwjOu1yJo5P WcMg== 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=nnpKeQBtwPjvxTHgphDfzLGtTBiI7BDIYpA6sLVmgdE=; fh=F/y/rQAitAoVhyEvV7npxQC+pmk+LQOxsxEGW4WYgok=; b=DbVvtigJZErGBkphRI3IngJ8NrSDc2s+0odf4QrDvvfcAdsg9uWCYcKw3I0pOHK9BJ vNEskvYJ9WYe1JQWayHYC1OrjwHGlgGcjUDoqQmxY+flwbtABalhcC2pEip05Zgf2Me4 gkaCzhHrL92SS9CuXi7c241cadNVtsx4i1i1C2OzP+r/bmTo6DC6p0BYzg+F2K9IQRyA WSs1/zpP8haawUsfgY8IQOAXehUTSm847UFKjmQGcXzCwMmEvG8ruqrMXROqBAPgN7uw H7xlwe1gXeKzJa54Ic+qei3uWnV13OEI9uUuLIsmOUlWW4PhBxkkqVoXBFadkLdQO2rW ZRmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=THet80rI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id p23-20020a056a000a1700b0069026fd5a29si8802466pfh.19.2023.09.18.14.49.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 14:49:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=THet80rI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id C8D428083AA2; Mon, 18 Sep 2023 04:23:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241689AbjIRLXA (ORCPT + 27 others); Mon, 18 Sep 2023 07:23:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241273AbjIRLWX (ORCPT ); Mon, 18 Sep 2023 07:22:23 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1B32EA; Mon, 18 Sep 2023 04:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=nnpKeQBtwPjvxTHgphDfzLGtTBiI7BDIYpA6sLVmgdE=; b=THet80rIIYQMVilXQm5fY8Nnhc uCE0gCwFKfGgwjOWP8GlVOS+mUOUER0lKftGSZEGibSe5MT8zzTqkpz9JW2q6R6XJwxEaG11QMim5 pVuvFbiMduxcCwztztsPg9X+tixdLYVrcxHQT5drWoMJk8ZTmnmwFrI5Oc76DaiL+vNY=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKT-00072G-Tk; Mon, 18 Sep 2023 11:22:13 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKT-0005f3-Kd; Mon, 18 Sep 2023 11:22:13 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v2 06/12] KVM: xen: allow shared_info to be mapped by fixed HVA Date: Mon, 18 Sep 2023 11:21:42 +0000 Message-Id: <20230918112148.28855-7-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:21 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777413644137772163 X-GMAIL-MSGID: 1777413644137772163 From: Paul Durrant The shared_info page is not guest memory as such. It is a dedicated page allocated by the VMM and overlaid onto guest memory in a GFN chosen by the guest. The guest may even request that shared_info be moved from one GFN to another, but the HVA is never going to change. Thus it makes much more sense to map the shared_info page in kernel once using this fixed HVA. Hence add a new KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA attribute type for this purpose and a KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA flag to advertize its availability. Don't actually advertize it yet though. That will be done in a subsequent patch, which will also add tests for the new attribute type. Also update the KVM API documentation with the new attribute and also fix it up to consistently refer to 'shared_info' (with the underscore). NOTE: The change of the kvm_xen_hvm_attr shared_info from struct to union is technically an ABI change but it's entirely compatible with existing users. Signed-off-by: Paul Durrant Reviewed-by: 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 v2: - Define the new attribute and capability but don't advertize the capability yet. - Add API documentation. --- Documentation/virt/kvm/api.rst | 25 +++++++++++++++++++------ arch/x86/kvm/xen.c | 28 ++++++++++++++++++++++------ include/uapi/linux/kvm.h | 6 +++++- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 21a7578142a1..e9df4df6fe48 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -353,7 +353,7 @@ The bits in the dirty bitmap are cleared before the ioctl returns, unless KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2 is enabled. For more information, see the description of the capability. -Note that the Xen shared info page, if configured, shall always be assumed +Note that the Xen shared_info page, if configured, shall always be assumed to be dirty. KVM will not explicitly mark it such. @@ -5408,8 +5408,9 @@ KVM_PV_ASYNC_CLEANUP_PERFORM __u8 long_mode; __u8 vector; __u8 runstate_update_flag; - struct { + union { __u64 gfn; + __u64 hva; } shared_info; struct { __u32 send_port; @@ -5437,10 +5438,10 @@ type values: KVM_XEN_ATTR_TYPE_LONG_MODE Sets the ABI mode of the VM to 32-bit or 64-bit (long mode). This - determines the layout of the shared info pages exposed to the VM. + determines the layout of the shared_info page exposed to the VM. KVM_XEN_ATTR_TYPE_SHARED_INFO - Sets the guest physical frame number at which the Xen "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 @@ -5449,7 +5450,7 @@ KVM_XEN_ATTR_TYPE_SHARED_INFO 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; + Note that the shared_info page may be constantly written to by KVM; it contains the event channel bitmap used to deliver interrupts to a Xen guest, amongst other things. It is exempt from dirty tracking mechanisms — KVM will not explicitly mark the page as dirty each @@ -5458,9 +5459,21 @@ KVM_XEN_ATTR_TYPE_SHARED_INFO any vCPU has been running or any event channel interrupts can be routed to the guest. - Setting the gfn to KVM_XEN_INVALID_GFN will disable the shared info + Setting the gfn to KVM_XEN_INVALID_GFN will disable the shared_info page. +KVM_XEN_ATTR_TYPE_SHARED_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 at which the shared_info page resides, which + will always be fixed in the VMM regardless of where it is mapped + in guest physical address space. This attribute should be used in + preference to KVM_XEN_ATTR_TYPE_SHARED_INFO as it avoids + unnecessary invalidation of an internal cache when the page is + re-mapped in guest physcial address space. + + Setting the hva to zero will disable the shared_info page. + KVM_XEN_ATTR_TYPE_UPCALL_VECTOR Sets the exception vector used to deliver Xen event channel upcalls. This is the HVM-wide vector injected directly by the hypervisor diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 8e6fdcd7bb6e..1abb4547642a 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -34,24 +34,27 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *vcpu, u64 param, u64 *r); DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ); -static int kvm_xen_shared_info_init(struct kvm *kvm, gfn_t gfn) +static int kvm_xen_shared_info_init(struct kvm *kvm, u64 addr, bool addr_is_gfn) { struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache; struct pvclock_wall_clock *wc; - gpa_t gpa = gfn_to_gpa(gfn); u32 *wc_sec_hi; u32 wc_version; u64 wall_nsec; int ret = 0; int idx = srcu_read_lock(&kvm->srcu); - if (gfn == KVM_XEN_INVALID_GFN) { + if ((addr_is_gfn && addr == KVM_XEN_INVALID_GFN) || + (!addr_is_gfn && addr == 0)) { kvm_gpc_deactivate(gpc); goto out; } do { - ret = kvm_gpc_activate(gpc, gpa, PAGE_SIZE); + if (addr_is_gfn) + ret = kvm_gpc_activate(gpc, gfn_to_gpa(addr), PAGE_SIZE); + else + ret = kvm_gpc_activate_hva(gpc, addr, PAGE_SIZE); if (ret) goto out; @@ -604,7 +607,6 @@ int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data) { int r = -ENOENT; - switch (data->type) { case KVM_XEN_ATTR_TYPE_LONG_MODE: if (!IS_ENABLED(CONFIG_64BIT) && data->u.long_mode) { @@ -619,7 +621,13 @@ int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data) case KVM_XEN_ATTR_TYPE_SHARED_INFO: mutex_lock(&kvm->arch.xen.xen_lock); - r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn); + r = kvm_xen_shared_info_init(kvm, data->u.shared_info.gfn, true); + mutex_unlock(&kvm->arch.xen.xen_lock); + break; + + case KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA: + mutex_lock(&kvm->arch.xen.xen_lock); + r = kvm_xen_shared_info_init(kvm, data->u.shared_info.hva, false); mutex_unlock(&kvm->arch.xen.xen_lock); break; @@ -684,6 +692,14 @@ int kvm_xen_hvm_get_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data) r = 0; break; + case KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA: + if (kvm->arch.xen.shinfo_cache.active) + data->u.shared_info.hva = kvm_gpc_hva(&kvm->arch.xen.shinfo_cache); + else + data->u.shared_info.hva = 0; + r = 0; + break; + case KVM_XEN_ATTR_TYPE_UPCALL_VECTOR: data->u.vector = kvm->arch.xen.upcall_vector; r = 0; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 13065dd96132..062bfa14b4d9 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1282,6 +1282,7 @@ struct kvm_x86_mce { #define KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL (1 << 4) #define KVM_XEN_HVM_CONFIG_EVTCHN_SEND (1 << 5) #define KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG (1 << 6) +#define KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA (1 << 7) struct kvm_xen_hvm_config { __u32 flags; @@ -1793,9 +1794,10 @@ struct kvm_xen_hvm_attr { __u8 long_mode; __u8 vector; __u8 runstate_update_flag; - struct { + union { __u64 gfn; #define KVM_XEN_INVALID_GFN ((__u64)-1) + __u64 hva; } shared_info; struct { __u32 send_port; @@ -1837,6 +1839,8 @@ struct kvm_xen_hvm_attr { #define KVM_XEN_ATTR_TYPE_XEN_VERSION 0x4 /* Available with KVM_CAP_XEN_HVM / KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG */ #define KVM_XEN_ATTR_TYPE_RUNSTATE_UPDATE_FLAG 0x5 +/* Available with KVM_CAP_XEN_HVM / KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA */ +#define KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA 0x6 /* Per-vCPU Xen attributes */ #define KVM_XEN_VCPU_GET_ATTR _IOWR(KVMIO, 0xca, struct kvm_xen_vcpu_attr) From patchwork Mon Sep 18 11:21:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141399 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2619861vqi; Mon, 18 Sep 2023 05:31:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFvOTKXzgBTxVLSAQa/pBTxvOWdlh4IhozbEv5hSAxQZwt2lOC9CaRl4dQ5cAQHJLYmoIFX X-Received: by 2002:a17:902:d2cd:b0:1c3:aef9:872e with SMTP id n13-20020a170902d2cd00b001c3aef9872emr8945763plc.34.1695040310054; Mon, 18 Sep 2023 05:31:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695040310; cv=none; d=google.com; s=arc-20160816; b=MK6uJtLuM6c4VGuAhgRODgmUdEUYthxBqT4xdcxCfVrjmfEogm4j6kpNgOyel46cFY tM4IvY3x9/ybQ7lqMieJGWifAwvkbLWSPzSZwdFcRoEdMl9T9lHpsxrF47EH89MztzEJ ZL3Fhn2dBoEAMLnEjtWxCqVw/5sBkL7Py/zAuYox1COPy8x7iwXcQcwDFFiiatNP4lek e/UhcF7VGDlV/MilyrqZPfOMWWjIjsqDm7fnUVLMfpa5QESATPrvI94OkmNOrl6qSlb2 qWEM6pIEfjpPvzof20P5pvm0vlnoObnGKq5T9Ao/UEYDtYZcMEFYWT7dc0jGnfHmqmIZ AfyQ== 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=NjU5Cy4jYQ61luXdEkL5wcGtwr03KyAKhtQAIMcDJIc=; fh=F/y/rQAitAoVhyEvV7npxQC+pmk+LQOxsxEGW4WYgok=; b=sRdPNwEefGjwfQr0/D5Uu53jXT5HMvA7kARdNA2CMismDgC65wrahRujQt5fy0p0Io 9NP/UnpnDCsA8gH9Jx47/xipJfitVW7Se1LBg3lwDwD2u9LBwOGS/4H01Fvp/6xWBYpL YmxxNkkSFmKyvgcsE5t1LUfuE9w9Ljy/u8ZFPIMonFf9PuxUln2TxUeH3nr/OS6wml2c P8kJj2XEOO9ynLyg/UZ38foT4iCe+0t8sukTTDvQ7wnBDTIqPgwuQAp0At0Z666gMSX1 QVsRNv3NwEfAOgoXhCHDQ1GhpEiNDpa2IhwwVdEPmSGYv4c211LOJr6QPzq9K05tv+6Q jbHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=hxW2QigA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id y14-20020a17090322ce00b001c0cbaf6970si1792083plg.501.2023.09.18.05.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 05:31:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=hxW2QigA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 81B38808458E; Mon, 18 Sep 2023 04:23:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241708AbjIRLXF (ORCPT + 27 others); Mon, 18 Sep 2023 07:23:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241350AbjIRLW0 (ORCPT ); Mon, 18 Sep 2023 07:22:26 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AFAFED2; Mon, 18 Sep 2023 04:22:19 -0700 (PDT) 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:Cc:To:From; bh=NjU5Cy4jYQ61luXdEkL5wcGtwr03KyAKhtQAIMcDJIc=; b=hxW2QigAROXOCTm1nHmbOL+hT0 atNw1DtdxjHChb+dx/VONcCcIE8xR8M0OzLWX81J+3aOS1QPbya1Z6Xj5ViHHiaswx5cbXilIV6YM boKMK3pXYphxsOBxTy0qrp3xuZWkV5gjkRlQNpkE/KsXsC5QeGa1O5flz2P0Anv5DDAY=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKV-00072P-Cj; Mon, 18 Sep 2023 11:22:15 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKV-0005f3-5E; Mon, 18 Sep 2023 11:22:15 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v2 07/12] KVM: xen: prepare for using 'default' vcpu_info Date: Mon, 18 Sep 2023 11:21:43 +0000 Message-Id: <20230918112148.28855-8-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:26 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777378588310701242 X-GMAIL-MSGID: 1777378588310701242 From: Paul Durrant The shared_info page contains an array of 32 vcpu_info structures which may be used by guests (with fewer than 32 vCPUs) if they don't explicitly register vcpu_info structures in their own memory, using a VCPUOP_register_vcpu_info hypercall. Currently we rely on the VMM always registering vcpu_info structures, even if the guest doesn't make that hypercall, which is somewhat bogus as (as has been stated in the comment of a previous commit) the shared_info page is not guest memory. Prepare to automatically use the vcpu_info info embedded in shared_info by default, by adding a get_vcpu_info_cache() helper function. This function also passes back an offset to be added to the cached khva. This is currently always zero since we're still relying on the current VMM behaviour. A subsequent patch will make proper use of it. No functional change intended. NOTE: To avoid leaking detail of the vcpu_info duality into the main x86 code, a kvm_xen_guest_time_update() has also been added and use of this requires that kvm_setup_guest_pvclock() ceases to be a static function. Signed-off-by: Paul Durrant Reviewed-by: 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 v2: - Fix build warning. --- arch/x86/include/asm/kvm_host.h | 4 +++ arch/x86/kvm/x86.c | 12 +++----- arch/x86/kvm/xen.c | 50 ++++++++++++++++++++++++++------- arch/x86/kvm/xen.h | 6 +++- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 1a4def36d5bb..6d896f9161c2 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2238,4 +2238,8 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages); */ #define KVM_EXIT_HYPERCALL_MBZ GENMASK_ULL(31, 1) +void kvm_setup_guest_pvclock(struct kvm_vcpu *v, + struct gfn_to_pfn_cache *gpc, + unsigned int offset); + #endif /* _ASM_X86_KVM_HOST_H */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d669a8801265..2d7d0ae18820 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3094,9 +3094,9 @@ u64 get_kvmclock_ns(struct kvm *kvm) return data.clock; } -static void kvm_setup_guest_pvclock(struct kvm_vcpu *v, - struct gfn_to_pfn_cache *gpc, - unsigned int offset) +void kvm_setup_guest_pvclock(struct kvm_vcpu *v, + struct gfn_to_pfn_cache *gpc, + unsigned int offset) { struct kvm_vcpu_arch *vcpu = &v->arch; struct pvclock_vcpu_time_info *guest_hv_clock; @@ -3232,11 +3232,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v) if (vcpu->pv_time.active) kvm_setup_guest_pvclock(v, &vcpu->pv_time, 0); - if (vcpu->xen.vcpu_info_cache.active) - kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_info_cache, - offsetof(struct compat_vcpu_info, time)); - if (vcpu->xen.vcpu_time_info_cache.active) - kvm_setup_guest_pvclock(v, &vcpu->xen.vcpu_time_info_cache, 0); + kvm_xen_guest_time_update(v); kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); return 0; } diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 1abb4547642a..7fc4fc2e54d8 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -489,6 +489,29 @@ static void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v) WARN_ON_ONCE(!kvm_irq_delivery_to_apic_fast(v->kvm, NULL, &irq, &r, NULL)); } +static struct gfn_to_pfn_cache *get_vcpu_info_cache(struct kvm_vcpu *v, unsigned long *offset) +{ + if (offset) + *offset = 0; + + return &v->arch.xen.vcpu_info_cache; +} + +void kvm_xen_guest_time_update(struct kvm_vcpu *v) +{ + unsigned long offset; + struct gfn_to_pfn_cache *gpc = get_vcpu_info_cache(v, &offset); + + BUILD_BUG_ON(offsetof(struct vcpu_info, time) != + offsetof(struct compat_vcpu_info, time)); + + if (gpc->active) + kvm_setup_guest_pvclock(v, gpc, offset + offsetof(struct compat_vcpu_info, time)); + + if (v->arch.xen.vcpu_time_info_cache.active) + kvm_setup_guest_pvclock(v, &v->arch.xen.vcpu_time_info_cache, 0); +} + /* * On event channel delivery, the vcpu_info may not have been accessible. * In that case, there are bits in vcpu->arch.xen.evtchn_pending_sel which @@ -499,7 +522,8 @@ static void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v) void kvm_xen_inject_pending_events(struct kvm_vcpu *v) { unsigned long evtchn_pending_sel = READ_ONCE(v->arch.xen.evtchn_pending_sel); - struct gfn_to_pfn_cache *gpc = &v->arch.xen.vcpu_info_cache; + unsigned long offset; + struct gfn_to_pfn_cache *gpc = get_vcpu_info_cache(v, &offset); unsigned long flags; if (!evtchn_pending_sel) @@ -522,7 +546,7 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) /* Now gpc->khva is a valid kernel address for the vcpu_info */ if (IS_ENABLED(CONFIG_64BIT) && v->kvm->arch.xen.long_mode) { - struct vcpu_info *vi = gpc->khva; + struct vcpu_info *vi = gpc->khva + offset; asm volatile(LOCK_PREFIX "orq %0, %1\n" "notq %0\n" @@ -534,7 +558,7 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) WRITE_ONCE(vi->evtchn_upcall_pending, 1); } else { u32 evtchn_pending_sel32 = evtchn_pending_sel; - struct compat_vcpu_info *vi = gpc->khva; + struct compat_vcpu_info *vi = gpc->khva + offset; asm volatile(LOCK_PREFIX "orl %0, %1\n" "notl %0\n" @@ -556,7 +580,8 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) int __kvm_xen_has_interrupt(struct kvm_vcpu *v) { - struct gfn_to_pfn_cache *gpc = &v->arch.xen.vcpu_info_cache; + unsigned long offset; + struct gfn_to_pfn_cache *gpc = get_vcpu_info_cache(v, &offset); unsigned long flags; u8 rc = 0; @@ -598,7 +623,7 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) read_lock_irqsave(&gpc->lock, flags); } - rc = ((struct vcpu_info *)gpc->khva)->evtchn_upcall_pending; + rc = ((struct vcpu_info *)(gpc->khva + offset))->evtchn_upcall_pending; read_unlock_irqrestore(&gpc->lock, flags); return rc; } @@ -1567,7 +1592,7 @@ static void kvm_xen_check_poller(struct kvm_vcpu *vcpu, int port) */ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm) { - struct gfn_to_pfn_cache *gpc = &kvm->arch.xen.shinfo_cache; + struct gfn_to_pfn_cache *gpc; struct kvm_vcpu *vcpu; unsigned long *pending_bits, *mask_bits; unsigned long flags; @@ -1585,7 +1610,8 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm) WRITE_ONCE(xe->vcpu_idx, vcpu->vcpu_idx); } - if (!vcpu->arch.xen.vcpu_info_cache.active) + gpc = get_vcpu_info_cache(vcpu, NULL); + if (!gpc->active) return -EINVAL; if (xe->port >= max_evtchn_port(kvm)) @@ -1594,6 +1620,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm) rc = -EWOULDBLOCK; idx = srcu_read_lock(&kvm->srcu); + gpc = &kvm->arch.xen.shinfo_cache; read_lock_irqsave(&gpc->lock, flags); if (!kvm_gpc_check(gpc, PAGE_SIZE)) @@ -1624,10 +1651,13 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm) rc = -ENOTCONN; /* Masked */ kvm_xen_check_poller(vcpu, xe->port); } else { + unsigned long offset; + rc = 1; /* Delivered to the bitmap in shared_info. */ + /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */ read_unlock_irqrestore(&gpc->lock, flags); - gpc = &vcpu->arch.xen.vcpu_info_cache; + gpc = get_vcpu_info_cache(vcpu, &offset); read_lock_irqsave(&gpc->lock, flags); if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { @@ -1641,13 +1671,13 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe, struct kvm *kvm) } if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { - struct vcpu_info *vcpu_info = gpc->khva; + struct vcpu_info *vcpu_info = gpc->khva + offset; if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) { WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); kick_vcpu = true; } } else { - struct compat_vcpu_info *vcpu_info = gpc->khva; + struct compat_vcpu_info *vcpu_info = gpc->khva + offset; if (!test_and_set_bit(port_word_bit, (unsigned long *)&vcpu_info->evtchn_pending_sel)) { WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); diff --git a/arch/x86/kvm/xen.h b/arch/x86/kvm/xen.h index f8f1fe22d090..c4d29ccbc3ab 100644 --- a/arch/x86/kvm/xen.h +++ b/arch/x86/kvm/xen.h @@ -16,6 +16,7 @@ extern struct static_key_false_deferred kvm_xen_enabled; +void kvm_xen_guest_time_update(struct kvm_vcpu *vcpu); int __kvm_xen_has_interrupt(struct kvm_vcpu *vcpu); void kvm_xen_inject_pending_events(struct kvm_vcpu *vcpu); int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data); @@ -52,7 +53,6 @@ static inline bool kvm_xen_hypercall_enabled(struct kvm *kvm) static inline int kvm_xen_has_interrupt(struct kvm_vcpu *vcpu) { if (static_branch_unlikely(&kvm_xen_enabled.key) && - vcpu->arch.xen.vcpu_info_cache.active && vcpu->kvm->arch.xen.upcall_vector) return __kvm_xen_has_interrupt(vcpu); @@ -80,6 +80,10 @@ static inline int kvm_xen_has_pending_timer(struct kvm_vcpu *vcpu) void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu); #else +static inline void kvm_xen_guest_time_update(struct kvm_vcpu *vcpu) +{ +} + static inline int kvm_xen_write_hypercall_page(struct kvm_vcpu *vcpu, u64 data) { return 1; From patchwork Mon Sep 18 11:21:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141449 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2746714vqi; Mon, 18 Sep 2023 08:31:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJ2JUl8++im/TE07z2RXflIeyPeo0pLi3b4KYSlp9Jk2Nc57L+dkrqUep+Y8JtXBOBtMZz X-Received: by 2002:a17:902:864b:b0:1c5:76b6:d4f7 with SMTP id y11-20020a170902864b00b001c576b6d4f7mr1724112plt.36.1695051104319; Mon, 18 Sep 2023 08:31:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695051104; cv=none; d=google.com; s=arc-20160816; b=SQ6R/hBxFOvk3IhQnaiSSjpyJrocxmFRJyRnsfqnEkn8dAe/JO67Sm40vQdzi81Mxh DmkShYrdJM6m3RIQcbsOKJBGxGxGECmZAl4vkgfBQ4u4z+MKQd8lN4n0tBQTSbCirk+F iNIIP+GiVdr3lcqqGcWGAIsv9PofLMPBConl3XIckTuTdz/myRTDI1iQHTGxMSAzszUH H9VFvUNKz123UYVJXn2iwXfmmSovTW+Hp0WyzW+cfEPAtoedc5hJJiEAId9JLNkohgn6 GT+oeyKEUntfgvATHmYl4QKZRa3PVKhdkaSivcCLVa7a65lFOv1dlQ/ksMhyvc0j7rUy gpIQ== 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=181kMJGEgoEqfoCDVltNp8LDaNFEvU6oh7tagdepNEc=; fh=Pw8ME2ioat+0rO24kCOjPSzdtMZ7IXXfoe/kHVOMV8g=; b=pYDp4r7pz1zc2T+zDvXd4itrDiauAJMnPV/3l5tr8oN+agOkffEcdQBIgqjUiNQLWx ebUeECSPjCkjgVjhn7IoECVturDSCyvnJnkdliS2/47n7qTQSSMj8AyNkFPlCtzQWanm BljDa6zzXZ07P7pDR1cJiHdBmjU55W70qNL8DszvYmiFBS1sb7ZG5iH44+VarlhMHn89 Ewj/vW6c1SK7m3zDpDXmIo4G6qQuBr/SjZUAc8l0dE4C62j3PQ/4Qnzfh7jo93ZUP3i9 Hw26SgJZaLcciguSvaiVrZJC8CwALKLu5/dgAqFWcTmL0M3+qwPf2X2xKRLXqTXieQho g2HQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=oq+6PmnO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id m11-20020a1709026bcb00b001b829a32f2dsi4770295plt.457.2023.09.18.08.31.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 08:31:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=oq+6PmnO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 35271808458D; Mon, 18 Sep 2023 04:23:25 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241696AbjIRLXB (ORCPT + 27 others); Mon, 18 Sep 2023 07:23:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241335AbjIRLWY (ORCPT ); Mon, 18 Sep 2023 07:22:24 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46A6D94; Mon, 18 Sep 2023 04:22:18 -0700 (PDT) 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:Cc:To:From; bh=181kMJGEgoEqfoCDVltNp8LDaNFEvU6oh7tagdepNEc=; b=oq+6PmnOKOwiTVAkKhKhi3aAOv XXZjt/UJM2hgYEQA3ajIJgWyThXez/PjJbKCsrH9TRyYkDwyypcfIEo3VakSG7HOQs6bJgoYXbp/8 uzTtrERDEDeapHpA1RG4mRAXOmNZbRYLoDQCTLs2q3csYkD27iBlV8cIXhbRvFXjQ1Ys=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKW-00072n-U4; Mon, 18 Sep 2023 11:22:16 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKW-0005f3-M7; Mon, 18 Sep 2023 11:22:16 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , David Woodhouse , x86@kernel.org Subject: [PATCH v2 08/12] KVM: xen: automatically use the vcpu_info embedded in shared_info Date: Mon, 18 Sep 2023 11:21:44 +0000 Message-Id: <20230918112148.28855-9-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:25 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777389906985232302 X-GMAIL-MSGID: 1777389906985232302 From: Paul Durrant The VMM should only need to set the KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO attribute in response to a VCPUOP_register_vcpu_info hypercall. We can handle the default case internally since we already know where the shared_info page is. Modify get_vcpu_info_cache() to pass back a pointer to the shared info pfn cache (and appropriate offset) for any of the first 32 vCPUs if the attribute has not been set. A VMM will be able to determine whether it needs to set up default vcpu_info using the previously defined KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA Xen capability flag, which will be advertized in a subsequent patch. Also update the KVM API documentation to describe the new behaviour. Signed-off-by: Paul Durrant --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: David Woodhouse Cc: x86@kernel.org v2: - Dispense with the KVM_XEN_HVM_CONFIG_DEFAULT_VCPU_INFO capability. - Add API documentation. --- Documentation/virt/kvm/api.rst | 16 +++++++++------- arch/x86/kvm/xen.c | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e9df4df6fe48..54f5d7392fe8 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -5442,13 +5442,7 @@ KVM_XEN_ATTR_TYPE_LONG_MODE 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. + page resides. Note that the shared_info page may be constantly written to by KVM; it contains the event channel bitmap used to deliver interrupts to @@ -5564,6 +5558,14 @@ type values: KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO Sets the guest physical address of the vcpu_info for a given vCPU. + The vcpu_info for the first 32 vCPUs defaults to the structures + embedded in the shared_info page. + + If the KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA flag is also set in the + Xen capabilities then the VMM is not required to set this default + location; KVM will handle that internally. Otherwise this attribute + must be set for all vCPUs. + As with the shared_info page for the VM, the corresponding page may be dirtied at any time if event channel interrupt delivery is enabled, so userspace should always assume that the page is dirty without relying diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 7fc4fc2e54d8..7cb5f8b55205 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -491,6 +491,21 @@ static void kvm_xen_inject_vcpu_vector(struct kvm_vcpu *v) static struct gfn_to_pfn_cache *get_vcpu_info_cache(struct kvm_vcpu *v, unsigned long *offset) { + if (!v->arch.xen.vcpu_info_cache.active && v->arch.xen.vcpu_id < MAX_VIRT_CPUS) { + struct kvm *kvm = v->kvm; + + if (offset) { + if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) + *offset = offsetof(struct shared_info, + vcpu_info[v->arch.xen.vcpu_id]); + else + *offset = offsetof(struct compat_shared_info, + vcpu_info[v->arch.xen.vcpu_id]); + } + + return &kvm->arch.xen.shinfo_cache; + } + if (offset) *offset = 0; From patchwork Mon Sep 18 11:21:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141502 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2855206vqi; Mon, 18 Sep 2023 11:11:18 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFgKs+jZXS8t4FCGFvpJqRRqp+N6KNSeahCvljCCginASW/vh4Wds5Srg5DChlJ0Nru6aB8 X-Received: by 2002:a17:902:db03:b0:1c4:2ca5:8b7c with SMTP id m3-20020a170902db0300b001c42ca58b7cmr9238896plx.61.1695060678178; Mon, 18 Sep 2023 11:11:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695060678; cv=none; d=google.com; s=arc-20160816; b=RTsMEmy6Wcv0NxAVnWC5jIiIy+r3RvwQwvLcCaj3eyaF/NRz9yY6Ruj563QS11QmUy 9jZWikA3aakIAgdCFeBTsrBjnIaoESHPnztv7DJrgOxdR/0QGccZt0phHYLa22J6h1G+ cBOShBroTNzdwMn74YnCoz+csDX/V8Sz8FexntbNEiXzDQ1SboDJIirdDpMqnmrgd/j2 zt2IOqQ+9i2WA1Gm1vwqvEhycORit3nXW/IFNdc/kJTSj0bcbo2Q259B2ZaY18HifyDB ufgp19EOBJuoxTarLMRtMLogICwTjGOUEhifGPEqlU0KoGgxgK9qavS+BbO60y2WBCEo MDGg== 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=ED0l6T0DdIBVNn8czLldNPS0uF0oubT1etNCdRtl7Qw=; fh=NVKswJvclYk1MEC7AfFzo67YIylApYMwZ7eFzXKDgdk=; b=VV/wI0PI5naZAitEDZTEPqNcY1sD99Upyg4WC8LFHyzyU+PqJdbFqM6vsUjsRiVEtT Rx9MxJHnUZKjpDA5nV0nCwob366BRg2CGKqil9XYYvPPTTn1cD0hSuOzDkE6j/9zEavz zKErz5ty/aVbjWpbTXlhV+JC+5mv7D13/s1C7p42bsROdsCHsDD5rUlNr9boJV8s102H ox6uJHpqkSG0KViTkKjllH8xz4p3XRxf5J2LwzhZ7gxrJU/n5yP3e0XYkr1r6gjN2+rr VjAhu2WsNcmnZpWie4vTjG9Np4QBxrJzYElHk1dbsvL9e63QbOLHklkzZ06WguM0JLqm aUjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=QHG7DRpC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id ks6-20020a170903084600b001bf0cc53d1fsi1900515plb.343.2023.09.18.11.11.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 11:11:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=QHG7DRpC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id BC10C8243086; Mon, 18 Sep 2023 04:23:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241702AbjIRLXD (ORCPT + 27 others); Mon, 18 Sep 2023 07:23:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241292AbjIRLWY (ORCPT ); Mon, 18 Sep 2023 07:22:24 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D02A0B3; Mon, 18 Sep 2023 04:22:18 -0700 (PDT) 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:Cc:To:From; bh=ED0l6T0DdIBVNn8czLldNPS0uF0oubT1etNCdRtl7Qw=; b=QHG7DRpC84gyDdVc8cAcwIFQOr nGA81HVkPAJdJdy7t4vTi/xC2FTFAxW+e7uAGNbihxdvrDaSbmmBTb6niIU04lRzAz26F6TqMiErv SN7CRY7OxmfVONirdegQzk+lPQtLhaQVsxKCa5nA7EyxLT6C/z5mbu2877x+9QTetkpE=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCKX-000732-Rp; Mon, 18 Sep 2023 11:22:17 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKX-0005f3-Kd; Mon, 18 Sep 2023 11:22:17 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Sean Christopherson , Paolo Bonzini , David Woodhouse Subject: [PATCH v2 09/12] KVM: selftests / xen: set KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID Date: Mon, 18 Sep 2023 11:21:45 +0000 Message-Id: <20230918112148.28855-10-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:23:40 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777376890658106022 X-GMAIL-MSGID: 1777399946166968055 From: Paul Durrant If the capability (KVM_XEN_HVM_CONFIG_EVTCHN_SEND) is present then set the guest's vCPU id to match the chosen vcpu_info offset. Also make some cosmetic fixes to the code for clarity. Signed-off-by: Paul Durrant --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Woodhouse v2: - New in this version. --- .../selftests/kvm/x86_64/xen_shinfo_test.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c index 05898ad9f4d9..49d0c91ee078 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -38,6 +38,8 @@ #define VCPU_INFO_VADDR (SHINFO_REGION_GVA + 0x40) #define RUNSTATE_VADDR (SHINFO_REGION_GVA + PAGE_SIZE + PAGE_SIZE - 15) +#define VCPU_ID 1 /* Must correspond to offset of VCPU_INFO_[V]ADDR */ + #define EVTCHN_VECTOR 0x10 #define EVTCHN_TEST1 15 @@ -410,7 +412,7 @@ static void *juggle_shinfo_state(void *arg) struct kvm_xen_hvm_attr cache_activate = { .type = KVM_XEN_ATTR_TYPE_SHARED_INFO, - .u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE + .u.shared_info.gfn = SHINFO_ADDR / PAGE_SIZE }; struct kvm_xen_hvm_attr cache_deactivate = { @@ -446,6 +448,7 @@ int main(int argc, char *argv[]) bool do_runstate_flag = !!(xen_caps & KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG); bool do_eventfd_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL); bool do_evtchn_tests = do_eventfd_tests && !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND); + bool has_vcpu_id = !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND); clock_gettime(CLOCK_REALTIME, &min_ts); @@ -494,7 +497,7 @@ int main(int argc, char *argv[]) struct kvm_xen_hvm_attr ha = { .type = KVM_XEN_ATTR_TYPE_SHARED_INFO, - .u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE, + .u.shared_info.gfn = SHINFO_ADDR / PAGE_SIZE, }; vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ha); @@ -508,6 +511,14 @@ int main(int argc, char *argv[]) TEST_ASSERT(m == shinfo, "Failed to map /dev/zero over shared info"); shinfo->wc = wc_copy; + if (has_vcpu_id) { + struct kvm_xen_vcpu_attr vid = { + .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID, + .u.vcpu_id = VCPU_ID, + }; + vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vid); + } + struct kvm_xen_vcpu_attr vi = { .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO, .u.gpa = VCPU_INFO_ADDR, @@ -983,8 +994,8 @@ int main(int argc, char *argv[]) struct pvclock_wall_clock *wc; struct pvclock_vcpu_time_info *ti, *ti2; - wc = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0xc00); - ti = addr_gpa2hva(vm, SHINFO_REGION_GPA + 0x40 + 0x20); + wc = addr_gpa2hva(vm, SHINFO_ADDR + 0xc00); + ti = addr_gpa2hva(vm, VCPU_INFO_ADDR + 0x20); ti2 = addr_gpa2hva(vm, PVTIME_ADDR); if (verbose) { From patchwork Mon Sep 18 11:21:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141423 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2683753vqi; Mon, 18 Sep 2023 07:05:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHUQepDO8lUING8zq9dRCjRqHR3ela5bb0eC5FpgpjbBGXZee1/PG5Dypm3yCAFHTrKGXwd X-Received: by 2002:a05:6e02:13d3:b0:345:d58d:9ae5 with SMTP id v19-20020a056e0213d300b00345d58d9ae5mr11845983ilj.7.1695045956036; Mon, 18 Sep 2023 07:05:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695045956; cv=none; d=google.com; s=arc-20160816; b=affoInNHsciOk2zhk4KM6LkInHACaTfltPQrLO/7WttHeAOOsk+GmoaW3ZOzdThg2h ZyRKH2h6ABWnhz9TexXSbBgIdeP5m0mdouK+yrwnR5ze4WJZeljakpls6ohExCfHYqWg f8JyWrjevB12JKZJOe5vfKR37RLoXNwkFWB8+BnJwWi1iF9mXWMCPuA/IEUSqJDS/vfT WTHpbjVfGNQlnTWtVtaAECSAX6mpPLl3tTpXOtisbYuvYE0mvuP0eOHNqFpQYRw5O+2/ TizHJubktb+DMiR5IutfYMOU5czBTnmQW7ROYgiCIh8qONW9CF+r7OZjrVOmAqew2sjY UFRQ== 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=F2tt1vng5GeCrD0vCTbIE4sW0FPuoGj/KLthbnkHO/8=; fh=NVKswJvclYk1MEC7AfFzo67YIylApYMwZ7eFzXKDgdk=; b=AKUKelY8rNYQtByPCOi6cTtUKwIfQ+PGUAOvAmn64Y/VYG0zrXGd0PHIqqT8UZ/THZ umLgdr+kb2J01GiJ1FaHkKpfh0D0JmZqPtuyU4QCAUGaPs+AprGI/nsQZPyRjGujhYLN tctImC3GiBAP3dRUocnZZ5Hpqwj7FfVv8OA4YghtmTGV8QcsxN+JlJVkH+yurzXbIBet 061Cybu3P/qti6LBjuMWqpxIuZaI1ZcamQUampOBWtH5jB69YlmPO6G3jtlRVitBGxh2 C8MQjrp1lttBikHcfKLzLBbgjWVaqVDggjDz2fYRxbl4dowPnwkdOu4t6x2uCkbNCgg5 ss5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=ZDN0LQkR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id z4-20020a656104000000b0056c297d163csi7418484pgu.530.2023.09.18.07.05.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 07:05:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=ZDN0LQkR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id B3CE4803202D; Mon, 18 Sep 2023 04:44:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241756AbjIRLnc (ORCPT + 27 others); Mon, 18 Sep 2023 07:43:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241716AbjIRLnD (ORCPT ); Mon, 18 Sep 2023 07:43:03 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71BAEFD; Mon, 18 Sep 2023 04:42:56 -0700 (PDT) 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:Cc:To:From; bh=F2tt1vng5GeCrD0vCTbIE4sW0FPuoGj/KLthbnkHO/8=; b=ZDN0LQkR52OCZ+dcXyY461a9r+ 7l1WiuKByZk0SU3yaOe1HlaKKcjw4lCY3mH/Nc2Ap4bfDVsT+bLVNvqsEFkv0G5gGLaz1ej+GeXJ7 zv9NTLNyfY3fd0YgA/dPANIaEkjaPeLswMcFmmScxoY17etAkI8dGJNYrO8Be03Dh23k=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCeV-0007d9-Ek; Mon, 18 Sep 2023 11:42:55 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKY-0005f3-J9; Mon, 18 Sep 2023 11:22:18 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Sean Christopherson , Paolo Bonzini , David Woodhouse Subject: [PATCH v2 10/12] KVM: selftests / xen: map shared_info using HVA rather than GFN Date: Mon, 18 Sep 2023 11:21:46 +0000 Message-Id: <20230918112148.28855-11-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:44:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777384508370416552 X-GMAIL-MSGID: 1777384508370416552 From: Paul Durrant Using the HVA of the shared_info page is more efficient, so if the capability (KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA) is present use that method to do the mapping. Signed-off-by: Paul Durrant --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Woodhouse v2: - New in this version. --- .../selftests/kvm/x86_64/xen_shinfo_test.c | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c index 49d0c91ee078..fa829d6e0848 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -395,6 +395,7 @@ static int cmp_timespec(struct timespec *a, struct timespec *b) return 0; } +static struct shared_info *shinfo; static struct vcpu_info *vinfo; static struct kvm_vcpu *vcpu; @@ -406,7 +407,7 @@ static void handle_alrm(int sig) TEST_FAIL("IRQ delivery timed out"); } -static void *juggle_shinfo_state(void *arg) +static void *juggle_shinfo_state_gfn(void *arg) { struct kvm_vm *vm = (struct kvm_vm *)arg; @@ -429,6 +430,29 @@ static void *juggle_shinfo_state(void *arg) return NULL; } +static void *juggle_shinfo_state_hva(void *arg) +{ + struct kvm_vm *vm = (struct kvm_vm *)arg; + + struct kvm_xen_hvm_attr cache_activate = { + .type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA, + .u.shared_info.hva = (unsigned long)shinfo + }; + + struct kvm_xen_hvm_attr cache_deactivate = { + .type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA, + .u.shared_info.hva = 0 + }; + + for (;;) { + __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate); + __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate); + pthread_testcancel(); + } + + return NULL; +} + int main(int argc, char *argv[]) { struct timespec min_ts, max_ts, vm_ts; @@ -449,6 +473,7 @@ int main(int argc, char *argv[]) bool do_eventfd_tests = !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL); bool do_evtchn_tests = do_eventfd_tests && !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND); bool has_vcpu_id = !!(xen_caps & KVM_XEN_HVM_CONFIG_EVTCHN_SEND); + bool has_shinfo_hva = !!(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA); clock_gettime(CLOCK_REALTIME, &min_ts); @@ -459,7 +484,7 @@ int main(int argc, char *argv[]) SHINFO_REGION_GPA, SHINFO_REGION_SLOT, 3, 0); virt_map(vm, SHINFO_REGION_GVA, SHINFO_REGION_GPA, 3); - struct shared_info *shinfo = addr_gpa2hva(vm, SHINFO_VADDR); + shinfo = addr_gpa2hva(vm, SHINFO_VADDR); int zero_fd = open("/dev/zero", O_RDONLY); TEST_ASSERT(zero_fd != -1, "Failed to open /dev/zero"); @@ -495,10 +520,16 @@ int main(int argc, char *argv[]) "Failed to read back RUNSTATE_UPDATE_FLAG attr"); } - struct kvm_xen_hvm_attr ha = { - .type = KVM_XEN_ATTR_TYPE_SHARED_INFO, - .u.shared_info.gfn = SHINFO_ADDR / PAGE_SIZE, - }; + struct kvm_xen_hvm_attr ha = {}; + + if (has_shinfo_hva) { + ha.type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA; + ha.u.shared_info.hva = (unsigned long)shinfo; + } else { + ha.type = KVM_XEN_ATTR_TYPE_SHARED_INFO; + ha.u.shared_info.gfn = SHINFO_ADDR / PAGE_SIZE; + } + vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &ha); /* @@ -902,7 +933,12 @@ int main(int argc, char *argv[]) if (verbose) printf("Testing shinfo lock corruption (KVM_XEN_HVM_EVTCHN_SEND)\n"); - ret = pthread_create(&thread, NULL, &juggle_shinfo_state, (void *)vm); + if (has_shinfo_hva) + ret = pthread_create(&thread, NULL, + &juggle_shinfo_state_hva, (void *)vm); + else + ret = pthread_create(&thread, NULL, + &juggle_shinfo_state_gfn, (void *)vm); TEST_ASSERT(ret == 0, "pthread_create() failed: %s", strerror(ret)); struct kvm_irq_routing_xen_evtchn uxe = { From patchwork Mon Sep 18 11:21:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2784438vqi; Mon, 18 Sep 2023 09:24:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG9mzA/H0lfJTPCZrXqSBmEQXwI8GUssJJdKlNJ6kkOhztohBs4J5hZbpO/h4ntAROYMJjp X-Received: by 2002:a05:6a20:105a:b0:153:73dd:282c with SMTP id gt26-20020a056a20105a00b0015373dd282cmr7851185pzc.58.1695054276669; Mon, 18 Sep 2023 09:24:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695054276; cv=none; d=google.com; s=arc-20160816; b=gsMofHWnO+hxByonW7R7nSK32KA4PQrdTCzl7Ran5xSUNzw5BUo0Z+dlaXzNB/19pa SUhU8REOizZgliioiPVq8NSFHhMjdAThp+m1cLba9kWm0mvrK1O9R1nrfZ0XzIzdneQ+ nXFoi53CetFcBzQMUyH0ZakU9QU+lANbKTpTUJnHb4EWFYL2v/UacXSn8yHZZJyEApLX I5pImIgaHW0FH5WHajA6O9wdBptc5pa5IMNLTlm0jxmWs6lk/xhXHYO0wc7px0qrPT+L Dmfdv9gMZZChWbj19h7lZkDUw6pAxTGFcMiEEemJ8AMdYITHOHKgz+Ce1MlGGmLBppH9 zhVA== 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=eOXlTdKLTa3rMbpImKuxJFenfTeNgDL5ucB7IcBcqTs=; fh=NVKswJvclYk1MEC7AfFzo67YIylApYMwZ7eFzXKDgdk=; b=NDSTjkS8ngujg0o+9J2YdoFnmbGjRZ65BwPEDgU1Tc0y56LQBhz0pXKIOlXuRcIl3w 3Vp5mWKzMUndbb29mICPuTSWelTLaLwrhbhvhCEXC7WMnHoTHxLUbRBZTJiu9aQELMq2 uoV7LvqbT2oCNtcoyV8IweYK1Q75aTPrdvUKZJoTEjcmVynFBt+DW5oT+j2bsdwON9kp DbShCdnoDRF4ub0grKhT4jiw7wbzjBlIEJEW5UTZpi1JNbcCKpSbK6O746QYeeIdfB90 JPqu9W0x8ky8z3tDbu/26fYJX/NT4JaVFsFwxGuHixEs5Rpg1covW3sZtSF7M/UFAGH0 E3Nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=Q8NK4k4O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id a62-20020a639041000000b00578a9192d90si202699pge.140.2023.09.18.09.24.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 09:24:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=Q8NK4k4O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id A744E8056A2D; Mon, 18 Sep 2023 04:44:05 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241729AbjIRLnb (ORCPT + 27 others); Mon, 18 Sep 2023 07:43:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241708AbjIRLnD (ORCPT ); Mon, 18 Sep 2023 07:43:03 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95EF7E7; Mon, 18 Sep 2023 04:42:56 -0700 (PDT) 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:Cc:To:From; bh=eOXlTdKLTa3rMbpImKuxJFenfTeNgDL5ucB7IcBcqTs=; b=Q8NK4k4O+l9/v/t+fmfVpyecZ7 nC47sQZgLUC7BINw39TMVvaz4u7IWDLoyAikoorf2O5a8Pv+q2Tz2/eDRwdrnKTEcQrqNrDb7gFzS nGQSwKapViQHipY4nwnN1XwHoCPHiGU85i+ya1ip7dXKkv0G3heLShlekgYK/r2jN0+0=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCeV-0007dB-GO; Mon, 18 Sep 2023 11:42:55 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKZ-0005f3-He; Mon, 18 Sep 2023 11:22:19 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Sean Christopherson , Paolo Bonzini , David Woodhouse Subject: [PATCH v2 11/12] KVM: selftests / xen: don't explicitly set the vcpu_info address Date: Mon, 18 Sep 2023 11:21:47 +0000 Message-Id: <20230918112148.28855-12-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:44:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777393233455695166 X-GMAIL-MSGID: 1777393233455695166 From: Paul Durrant If the vCPU id is set and the shared_info is mapped using HVA then we can infer that KVM has the ability to use a default vcpu_info mapping. Hence we can stop setting the address of the vcpu_info structure. Signed-off-by: Paul Durrant --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Woodhouse v2: - New in this version. --- tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c index fa829d6e0848..d1c88deec0b2 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -550,11 +550,13 @@ int main(int argc, char *argv[]) vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vid); } - struct kvm_xen_vcpu_attr vi = { - .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO, - .u.gpa = VCPU_INFO_ADDR, - }; - vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vi); + if (!has_vcpu_id || !has_shinfo_hva) { + struct kvm_xen_vcpu_attr vi = { + .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO, + .u.gpa = VCPU_INFO_ADDR, + }; + vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vi); + } struct kvm_xen_vcpu_attr pvclock = { .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO, From patchwork Mon Sep 18 11:21:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 141615 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp3016544vqi; Mon, 18 Sep 2023 16:24:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFrerKgC9qxmNKGksJx/5xZ5PegaVLhtTS3dPf6/8LMBMInyNLIRbxVejoJpOMjU6fe2y0l X-Received: by 2002:a17:903:2284:b0:1c4:202d:8747 with SMTP id b4-20020a170903228400b001c4202d8747mr10844839plh.50.1695079448243; Mon, 18 Sep 2023 16:24:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695079448; cv=none; d=google.com; s=arc-20160816; b=LYaYUFRsnT6Dohp4ah1P92S1L++anYJmohpJUnZw3FrA4UfyGn/kSjCc722azPvkCa yR62amyy0RFFYITVRxGj4YbreFIQrjlam0mKCbAvbq6MjGkPuPp4vs2ZMq8A0yLo/0re Yxv1dBl5EevMyEtMY+ZBHxaypWpZG3+Oc0QJSLUfxUJa+pDvPv1CXay9y/O8HPZRUoMz IvCD786MiWcI6KtS7lnZGOfHYBW6nyAeEWRXPHQM+E037UyOFy0oBi27PuYvShtF1llL iE/LV5BpooplCCclfuHuhl1tNNVspEVbweE39r7uMjkPrRtAoKXyyJOjNwYuT1EhjTC9 7CjA== 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=obHkKlWolKcmhKlSo8Jk0b2FNUVF9nDqgJwemksFUzs=; fh=Pw8ME2ioat+0rO24kCOjPSzdtMZ7IXXfoe/kHVOMV8g=; b=fLgJo0MmvgzzixdlYOoSdHze5+TTAEkw6xxWBqcxxO8blR9MFivmq6t8QScSsRqdKz GPuasSS1gH2jFH38Ibf9y9hk+mJXEsl5srBit2el30nFEzzhIS4sgGDi9S8EGuO2mQ+Q QnlgH4dKidwf7d0LVHhQLmcDH5EZqPleUO/HSimKctQYXuUb8FaOYq1aPQ/mfAWVsP0O DAnr8UfaFJeXDeiZLlewsdGkOg6hTBbNdldd2Nq4tg2tKQXKfxVns0MSa+L/6Ef/zUHC KH+Q8DxEbLHvDT33DjAurnaWUQJbU3yfAfjoZC2R8YTcNKJsawC2oblPNif+BJeKedNg +T0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=mrJEW9R3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id w1-20020a170902e88100b001bb8df95094si9153213plg.509.2023.09.18.16.24.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 16:24:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=mrJEW9R3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 17C6E82E7B38; Mon, 18 Sep 2023 04:44:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241776AbjIRLnf (ORCPT + 27 others); Mon, 18 Sep 2023 07:43:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35538 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241733AbjIRLnE (ORCPT ); Mon, 18 Sep 2023 07:43:04 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA311C3; Mon, 18 Sep 2023 04:42:57 -0700 (PDT) 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:Cc:To:From; bh=obHkKlWolKcmhKlSo8Jk0b2FNUVF9nDqgJwemksFUzs=; b=mrJEW9R3FUj54SgbyLxYh/mePd XQwjnu6EdZbTCUYy2LVHC9ihqFNm+93fRUZH08GkyCr5fa31MfuddQzftDhRjnpy1sLIOknjrTzNJ dwF5cKd6oesA2YgNXpGwjeDqpNHEOelW9DtVby9GbLA1r2DuKqKQNJ2IzEAmGcFTaNAA=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qiCeV-0007d7-C7; Mon, 18 Sep 2023 11:42:55 +0000 Received: from ec2-63-33-11-17.eu-west-1.compute.amazonaws.com ([63.33.11.17] 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 1qiCKb-0005f3-2B; Mon, 18 Sep 2023 11:22:21 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , David Woodhouse , x86@kernel.org Subject: [PATCH v2 12/12] KVM: xen: advertize the KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA capability Date: Mon, 18 Sep 2023 11:21:48 +0000 Message-Id: <20230918112148.28855-13-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230918112148.28855-1-paul@xen.org> References: <20230918112148.28855-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_PASS,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:44:04 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777394061119844605 X-GMAIL-MSGID: 1777419627739859454 From: Paul Durrant Now that all relevant kernel changes and selftests are in place, enable the new capability. Signed-off-by: Paul Durrant --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: David Woodhouse Cc: x86@kernel.org v2: - New in this version. --- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2d7d0ae18820..4cd577d01bc4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4527,7 +4527,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) KVM_XEN_HVM_CONFIG_INTERCEPT_HCALL | KVM_XEN_HVM_CONFIG_SHARED_INFO | KVM_XEN_HVM_CONFIG_EVTCHN_2LEVEL | - KVM_XEN_HVM_CONFIG_EVTCHN_SEND; + KVM_XEN_HVM_CONFIG_EVTCHN_SEND | + KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA; if (sched_info_on()) r |= KVM_XEN_HVM_CONFIG_RUNSTATE | KVM_XEN_HVM_CONFIG_RUNSTATE_UPDATE_FLAG;