From patchwork Fri Sep 22 15:00:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143528 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5699023vqi; Fri, 22 Sep 2023 09:13:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHO4RscrkIiOada5OsNPtlmv7zNwbFoW0LJVnBqN1VwgvJArmsj2XuqGA6YQQyUxAxexL5S X-Received: by 2002:a05:6a20:13d5:b0:132:ff57:7fab with SMTP id ho21-20020a056a2013d500b00132ff577fabmr54579pzc.2.1695399200449; Fri, 22 Sep 2023 09:13:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695399200; cv=none; d=google.com; s=arc-20160816; b=sacnLFMNyKueR8NVtklycuFv12JVXU7mc28viUoWn0HKD/Eab5/q1NWJPhAzU9/5r8 /vJZJVylslY/Vfo4EROZk/oJYXDPD8NRpDjnGSN46NA2Jg55wrQluS1mjs+t9nEL0s2i +t6SMUEu+/IzM3gvsq/jBr+HeVfBcI5dA4hxhe+M2mYrkYPkKVUwfVskeLCRqGpORmzu v4kklQFJgH1XoZtHNaELmKVuPz+6UsdKyYbRS5ibGj01RxEGRhE/CuPFqO25cYUBIigQ 5SohpMk5qZUry97DGz40by0QxyTVy5w323Ek4/2Ou/1IBcvjKk7Ku7ZRn2LG58qQ+/J7 jQ3w== 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=cQ02ZWi3OXJZ36oKzuEnX4ff/ZDsG5TQqSf2+fKM4a0=; fh=eFj9v25W9Wrh6fN+JLpeVaL0rwE8+rsOVorNY/OMc1Y=; b=hG6SLdG8sxlvRp6Gt8XxlRZTPgW0kmz9Wmf4RBVldTMa+5eTTQdfUe04m1LoD8ZqVS qxLuppRyW59iXBzJ2rRnOqqOVWOEQhzSHoTJX0hdeccxFU7D7Z2EST3R2maUrOAa1DDT ImYEIeWwYOyfwj+yLoQtq6hC1f9fpX3GMnlI+ZYmi3radusrOGAkkYPlowJPjFoMimvr lzXJbAGiH1SzgNBuHa8DRvU1Gw3WAfWenA7DPld6ahnT7yhfZVPNpVdVvJYJCUkvpgCp lZ7n7slAZ+BsQjCA+9N/662f51Otw0PVMMvRtTlT5KBdkCtZNdwunQj39HmM73HjiG80 ra3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=RpTZGM5L; 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 r80-20020a632b53000000b005636ef310adsi4192693pgr.597.2023.09.22.09.13.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 09:13:20 -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=RpTZGM5L; 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 7A49A8364287; Fri, 22 Sep 2023 08:01:04 -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 S232446AbjIVPAp (ORCPT + 29 others); Fri, 22 Sep 2023 11:00:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232197AbjIVPAg (ORCPT ); Fri, 22 Sep 2023 11:00:36 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4646619B; Fri, 22 Sep 2023 08:00:29 -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=cQ02ZWi3OXJZ36oKzuEnX4ff/ZDsG5TQqSf2+fKM4a0=; b=RpTZGM5L3MPK1fAd07Cxa9hJcR QYZrUH2zz8r5TuKok+L8rripfXPP3pTCsc45EHG42mXShuvzhdWsYDIn2T9Fob0JgGcyKMXwTL3Gc y1GubD9ftlVK2GS1crzblvVy+df36K8erTJHJo19wh5nWFLyA09bYnJu/KWqjFOmhZ80=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdm-00044b-Eg; Fri, 22 Sep 2023 15:00:22 +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 1qjhdm-0005y2-6O; Fri, 22 Sep 2023 15:00:22 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , David Woodhouse , Paolo Bonzini Subject: [PATCH v5 01/10] KVM: pfncache: add a map helper function Date: Fri, 22 Sep 2023 15:00:00 +0000 Message-Id: <20230922150009.3319-2-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:01:04 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777754911857217788 X-GMAIL-MSGID: 1777754911857217788 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: Sean Christopherson Cc: 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 Fri Sep 22 15:00:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143544 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5739469vqi; Fri, 22 Sep 2023 10:08:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHh4kI8PGgri3nywLoAbK0zxj/VRYzQUGVvz9JadG1z+HbT6gAMIPh9RH/Qrv/ZdYHKYxry X-Received: by 2002:a17:902:f814:b0:1c5:5d45:6a41 with SMTP id ix20-20020a170902f81400b001c55d456a41mr31360plb.66.1695402494816; Fri, 22 Sep 2023 10:08:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695402494; cv=none; d=google.com; s=arc-20160816; b=AiK7iHFmgW9lnWMCLYx767+/nOF2KzyN27YH63BvXkiY7s9Z/Ms/iosTsFNbUj8uLo KjulWAxf8rqMVlzPqWbbWjhj5E/PIyrNCJzZdAGlK5lZ15u3InOAvfgADIjEZgoft6Ii QaUoDconlBS+W83LpOXXM+VpYVwGR2ehxSAU3znz+j2xZOy9FtX0gydEMfMYJ1RxyaeN DKWYSsFkNBOrg+xNy9eDFgFC5o1eThYHBbXCfjgGrrGxlPOEfusOeUL7H5Pzn9XYvFTT F67U+fJMbPQVCON3nn9yVKyzECgfjGH+my1eXib7l3vFnKEknQwO0j6SKL5RL5dRXHgo b/Pw== 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=b9VmcvZpefxWPZmnCsAAti7ImeYt2ilYGEmGGgNIuv0=; fh=8AvbwpPKF0Qe5WwS5eyABOVDUChKGxIrkPaOnjrOqKo=; b=fqFzpy98e+m/nbj7+YlEzeMp4ZE6FYNvDCAiI3TF17DwicdRJU7lD5Jdjkmr+ZZ0t4 2HU5wiPDa2LaVHY/U434m9W3CtOvCTPd9yPNeGwfRfNnMj75a/d6yWvfXu8r/8nqWVOA Piz3Z7ZBiRV4pLJHqMQ0t1FOmRPYubpqNoPHZs+e3aQNOrLuEPQqtiTR1U66lVi76VW5 EULbrYdkR+/HQXxDqPoKyS2qCdioVu7o9hrGw1iIGQGuGc9GLYI+0t+uMf5wS9+8/eaK KmTf2cwPROJoUtDQmx+h9HLJcAU5Y/zqJhhKUDfZqLy2XfdKSDq21SjAfBCxN50Vm72K 373g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=F1SITcqA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id p18-20020a170902e75200b001c07bac13d0si4553139plf.383.2023.09.22.10.08.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 10:08:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=F1SITcqA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 CA8538322278; Fri, 22 Sep 2023 08:01:05 -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 S232470AbjIVPAr (ORCPT + 29 others); Fri, 22 Sep 2023 11:00:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232174AbjIVPAg (ORCPT ); Fri, 22 Sep 2023 11:00:36 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 463C119A; Fri, 22 Sep 2023 08:00:29 -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=b9VmcvZpefxWPZmnCsAAti7ImeYt2ilYGEmGGgNIuv0=; b=F1SITcqAVVcWf92KC+INQChQmg ypFF0hxsBWV9ey7RrTX/+S5W7H19gxMFVYeENCmcxbYqO2c8/6FG1/4q204biGMauEsXKsJDy0Ktt sOgRCfHrtyBcN1FAkNvVhxKmigoTTfTII27s2A+pvOfwmAD5WDD8PYPcK4ypBNwuIk4o=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdo-00044d-4H; Fri, 22 Sep 2023 15:00:24 +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 1qjhdn-0005y2-SP; Fri, 22 Sep 2023 15:00:24 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v5 02/10] KVM: pfncache: add a mark-dirty helper Date: Fri, 22 Sep 2023 15:00:01 +0000 Message-Id: <20230922150009.3319-3-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:01:05 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777758366231502317 X-GMAIL-MSGID: 1777758366231502317 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: 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 Fri Sep 22 15:00:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143743 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:910f:0:b0:403:3b70:6f57 with SMTP id r15csp3916vqg; Fri, 22 Sep 2023 17:26:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFgLTuv9MgxgTU9FU7WLmZYhmQYTumsN4uFUoIFeDPNCWYry6NgKeup6+TsYXJ4bohJfQo8 X-Received: by 2002:a05:6870:c1c4:b0:1d6:567b:79aa with SMTP id i4-20020a056870c1c400b001d6567b79aamr914101oad.56.1695428800758; Fri, 22 Sep 2023 17:26:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695428800; cv=none; d=google.com; s=arc-20160816; b=sac2pAy22svzkxDUQMQiqjjPco4+1Ul3aKjMIUulqugxB147VCNVUTpl1HvNMiHNWf 1sLh+WH//4a7ZPSDE8MY/NfZY0aK8GV3+RSMnt/Pm4M2s3ZX6m1T94WbgbWeA3qdIg6c zM1lZWhOyHcg41FlsxmNjrUA9zD1zvtMBMtfbLc+SmtpMRID43jPEdGKzHsidgWBXHfB NKP8F5OkRmPcENirfjm9Y1P3vm44kPvPSHFTp8M8aeDRDVhw+9/icSCgT4ynHGho41dp qIujDQhYjoNor+S5k9OoBBocLxtExrZfo74i+GeL97/dpV79MnX6GPuAEU7dWla2Xree w1Lw== 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=CnnzQ4eiCWT/+IlcdQWASoCfyneq5mdBodDzIdnbsCc=; fh=8AvbwpPKF0Qe5WwS5eyABOVDUChKGxIrkPaOnjrOqKo=; b=cgB9HVOc3ennS6f3WGu+OkNTQJpMPOP9KPgvzjQHf2niZjyZkd01YCstY7GAZthwP3 MDWLzL0J0K79xnULPeNTgTI/sT8rSQ3vrvxIuxbp79mKRCqN6PbkLhpLSRlLBsEAlQ5D 0zO0DMGeWqKYI0BdfW1zKNJNB6JwWYguKO/zinVaaHBdTJarS+0KyIzS9a0Abq47DFXH ya3i1ZmNC4PpZtakaMuZ1cxnquYZGYoZqJMSdPcFbpIqriyQB6LCyhjSBfXR8rryIHoF RIGOFVnKMWYt+Cd6E08cgRdcuFPYgjmMiHwRntVHMtu9qOp54GFwlam3rgkQQCYXC3S9 lLUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b="U10PHX/x"; 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 v10-20020a63610a000000b00565e87d8707si4942009pgb.496.2023.09.22.17.26.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 17:26:40 -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="U10PHX/x"; 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 01A8A822C63A; Fri, 22 Sep 2023 08:00:55 -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 S232306AbjIVPAv (ORCPT + 29 others); Fri, 22 Sep 2023 11:00:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232266AbjIVPAi (ORCPT ); Fri, 22 Sep 2023 11:00:38 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C079A100; Fri, 22 Sep 2023 08:00:31 -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=CnnzQ4eiCWT/+IlcdQWASoCfyneq5mdBodDzIdnbsCc=; b=U10PHX/xHVLEL1Zk8fq1zMZ9sx 3SEqhz+miWLv3DmkfWkXMX9yHeMh77tq80VDTuX3C+qYgB+yuuhYl+6DESj4rR7w5GacHNUxg3DqD phKIJq3TRzyhQBL2zWfbz96KyaI/D8Kpo2iCiNlJVlCUlSdxmcIlEMP9jYh2Pw5cRhiE=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdp-00044f-St; Fri, 22 Sep 2023 15:00:25 +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 1qjhdp-0005y2-IC; Fri, 22 Sep 2023 15:00:25 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v5 03/10] KVM: pfncache: add a helper to get the gpa Date: Fri, 22 Sep 2023 15:00:02 +0000 Message-Id: <20230922150009.3319-4-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:00:55 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777785950406756895 X-GMAIL-MSGID: 1777785950406756895 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: 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 Fri Sep 22 15:00:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143499 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5650290vqi; Fri, 22 Sep 2023 08:07:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGEs6VChdf0rrXkYRsZ7lpLzFNGia2cK/1ImtergCjPf0Z4uTgx20DaHP8UazGanq5XfWf X-Received: by 2002:a17:90a:df06:b0:26d:2158:10ac with SMTP id gp6-20020a17090adf0600b0026d215810acmr8279708pjb.14.1695395251363; Fri, 22 Sep 2023 08:07:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695395251; cv=none; d=google.com; s=arc-20160816; b=b33kmtvrNw82I4w6Z9b0Ml25lev92uypEhf8Pl/490C+p2gb/gu2eKJxO3n0o2s9as uD9N/zIibQk/zwSIcUT9GJSWZGa8buutItPbncG2xCPJrjgomWTkO4dxU+mpscN9vSC3 rCe1mxLzAaowMft/VJJA/0GszX+NGvsSYDvoDL+J/Nh/b0bpGh+JF3MbiXHQEyd/KEul WMy+/KACWviKp20wm/r7cCWERgRXJrrCv/YdGACxYVxNlheHfxyQHuwetDMJioH5WxJx F1/cRZgi8qFSAz+qM3/K+znDtPJ3lgarASgRl5B7b5VmBOQijXaZIoEMwIHuVZHokNqs PGeQ== 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=e3Vz0tH7MeijOYhCrJMXMH52NUfYnF7z8/TCVVMfMcA=; fh=eFj9v25W9Wrh6fN+JLpeVaL0rwE8+rsOVorNY/OMc1Y=; b=kdbeTUhfSo2HoJRPagSzKyGX1Ocu5Da5YaA5JGkKnX//F2jz5OTQiaPPdEfKSDaSCI 5KqK1LAb2ludT8L8iX65VtwOBCG3PREx3oO5uqcOlPyNxbKncsZQqTAoXoa+T5/QDtjC VpDH0SRNhArWtUy4D0VapGCJMNQcS1pr1e+H9u1jV5XG7sDhMpsGRYl7J+TyrhpzMcKY J2mGByF1wTPsiE0rnKIKDe/0mm915p9q7v/92CXjhdFPuMesplsoMBplKUxdLd7sUc/4 U7wpGVklgb/bNPJBz4nx/+DdKqxQUvBzjKcPDpTtflQSoXOtt6VOQ9m7QxjHlbLNEA1X u0Ng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=jl6XYVcA; 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 fv9-20020a17090b0e8900b00274880d2b70si6397918pjb.8.2023.09.22.08.07.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 08:07:31 -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=jl6XYVcA; 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 956AE81EE53E; Fri, 22 Sep 2023 08:00:51 -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 S232329AbjIVPAk (ORCPT + 29 others); Fri, 22 Sep 2023 11:00:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232149AbjIVPAf (ORCPT ); Fri, 22 Sep 2023 11:00:35 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2786E199; Fri, 22 Sep 2023 08:00:28 -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=e3Vz0tH7MeijOYhCrJMXMH52NUfYnF7z8/TCVVMfMcA=; b=jl6XYVcAHjvLe2yNyG3ez4kYiB 98VppdFRXq+COgYtWd7/o2BvQ5Dfi0XSrKju03QFtxBTAp79vaxLTDU93/Fc3dEYq6onwPy3zz7ch wZU61jxHbRtCS2HiIUaqXWnfQzWTgmkpi4y8rJ6jjQhSDRCZSsE9xk27ZqSTM50VJ+T8=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdq-00044j-Sn; Fri, 22 Sep 2023 15:00:26 +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 1qjhdq-0005y2-L4; Fri, 22 Sep 2023 15:00:26 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , David Woodhouse , Paolo Bonzini Subject: [PATCH v5 04/10] KVM: pfncache: base offset check on khva rather than gpa Date: Fri, 22 Sep 2023 15:00:03 +0000 Message-Id: <20230922150009.3319-5-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:00:51 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777750771335900058 X-GMAIL-MSGID: 1777750771335900058 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: Sean Christopherson Cc: 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 Fri Sep 22 15:00:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143500 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5652088vqi; Fri, 22 Sep 2023 08:09:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF+H7KCkco4IjdXh3ntSj873mSc4eTi0uRWxQdfEBNBqQvQS4eBWwl9y2LEh4byxmMndogk X-Received: by 2002:a17:90a:888:b0:274:78:4f0e with SMTP id v8-20020a17090a088800b0027400784f0emr8563761pjc.20.1695395390048; Fri, 22 Sep 2023 08:09:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695395390; cv=none; d=google.com; s=arc-20160816; b=jTx84fMLPsagWRPCeh3AHsauH/ads+miPXlrauo+SkiSSC2wQzDQoslX6SWh/VChbx aka7QTZOZM1dfH4qN/QBKr3fDl37+QSwx9W98Ik0g/e1IcbZbmYTH5Er7XnPKIWGhNN/ /gld4gEnOAP2DeKRYE+QRf7B/WchRdR7oGtrI2oYzmFtKToYJ2wnBIRjwnJ3En9oN+yk mB/z2lG9MWGHrvtQhES0xhdEdKEP+kyzwkmi1fUJ5fT2Tqe+sF+xLiX0dp2Nt01Edk2V PhA7nEqti0FwF0sUeIv3OOqaP4IZRcXTJF1eAVk3mdoTT5dMquD9dTFwNLpWan01QD0C Ez+w== 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=uR3RY44FOYUdZ/yYeXcGIkRXrRmwuyl6HvHad0MhhqE=; fh=lKhNn3P8ZvksS1fElybz1iP8zlw6JVVQ9gjuN2JwMdc=; b=wjFDWlhNOKhzqq4810usJBZgeNsOGMsXoN8MuFXMgYDi5LEJ3AlMVYuhk1RmNwRyDs s/SnE7KdD6BwxDRFjAcuVRaVWE8iJm2StVn8JADwWYOP1uADli5EWhQlvNAwt7gkX4JA DBR88CUK+jU6FUFbtMv154E9krmPAeLeNSkeCSPTKqBnzquGt+I4ENo5eW5sXFNyhAGt Hi60qMRnmlDRbfiZVT+qY/fWmWAa3M+eG8zzcM7f4VSBdGmVediX3wOIXlQDn0QZUYw2 75e/wAkMFG7KoFhKVePm2lvD4ISKkvl4YVE0dHbs1QpKCUO1/uikbaVAP3Nu+NBwYQwW 0H8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=vD0eENGb; 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 h13-20020a17090acf0d00b00263fa42fdcbsi6332509pju.107.2023.09.22.08.09.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 08:09:50 -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=vD0eENGb; 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 3861A80C2E16; Fri, 22 Sep 2023 08:01:32 -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 S232605AbjIVPAy (ORCPT + 29 others); Fri, 22 Sep 2023 11:00:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232297AbjIVPAi (ORCPT ); Fri, 22 Sep 2023 11:00:38 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C16119B; Fri, 22 Sep 2023 08:00:31 -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=uR3RY44FOYUdZ/yYeXcGIkRXrRmwuyl6HvHad0MhhqE=; b=vD0eENGb99cUaUCNbJj0KzFlDU /ZYbDr5kGnibuiGqh3JpTpQuBRYw+tQKYPWqNMsCPw8U8OCAbdVFN+7dpSOOc+Z/iPrPb7AswuhhD 4zgkuyiUZ/p2yk0X/DeYzKHIuF8Sn6XCgsd7HU1oyyZcvcmCEcqLppNm1IkOYt+Pfx5o=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdr-00044n-Ve; Fri, 22 Sep 2023 15:00:27 +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 1qjhdr-0005y2-Ny; Fri, 22 Sep 2023 15:00:27 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , Paolo Bonzini , David Woodhouse Subject: [PATCH v5 05/10] KVM: pfncache: allow a cache to be activated with a fixed (userspace) HVA Date: Fri, 22 Sep 2023 15:00:04 +0000 Message-Id: <20230922150009.3319-6-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:01:32 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777750916725463597 X-GMAIL-MSGID: 1777750916725463597 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: Sean Christopherson 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 Fri Sep 22 15:00:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143727 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5936033vqi; Fri, 22 Sep 2023 16:57:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHuFh0xvhLUV8CevYoEwXEhWm+w664KltbPqbA6NB0x+EVyDvzRNQSXXwPkpio4ufPkzQJ6 X-Received: by 2002:a17:902:7049:b0:1c5:cd1c:46fa with SMTP id h9-20020a170902704900b001c5cd1c46famr838879plt.36.1695427068994; Fri, 22 Sep 2023 16:57:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695427068; cv=none; d=google.com; s=arc-20160816; b=HNZGHv1sYu/Iw9LjSEXyYhcnVud+tgEXNumUnp4FvMzQecAo3ockd0FLIz6GuUHLlz Gpi1lgJZwry6h4ZEdHohbIzGdH2544SSQWeg3el52yDkC/9oTFpL+S4KZAqx46A3phPD thDr7xZABAYLNsCuW2WMhcUPkISd+1WU/Wj+dzR1+Q1K6Wp41ZyAZBUL0kdkZxG1bGXf +hokXJn1SL7sPR1CSAJSTSY3tpn0jmif4B+ui6Ccf8ePy7UT8dEYa4WMfOCq0M10c6tO di27iurmZX6oY3908B6r+FgK+iyFb8u9ACA1gp7QvP2vPIYr2+52YlVKaemE/bTlpI8M elmA== 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=7Xg3VsT+niHE2xRvzgMpdBgGz0Csn+Ig0JZVwwD1HsY=; fh=8AvbwpPKF0Qe5WwS5eyABOVDUChKGxIrkPaOnjrOqKo=; b=ndNTv9GVAAL/TtGa8frqA/hmGvhp4iIYYaPTtwd5T7No5rUX+lVFs0EYwCIxp51r5M tJXsBJdjA6Z8q5r32odpXBtOu21YgHhFSAqXUG1wHZ4fL7N/APQgdWLtPvzDs5L74DHe 0d1p9VN5AoSfP6H+DE0lPYXtTepnHaU3gduYzFJ6T24jt8rcb8WBQUCWaNIrGBIFVdC0 MLXR6T2GwCXUri0bEilzDVCru8AkiAyB+0Q5e5frSEUbMSsXjWMNhxgGfvMA6FN9zvZ2 s56/ZSEQ6S06P6/wIpfzZQql/1AJ7tGb1Kh9CyfXqDu4Neym9LEy5ElUSVQAZ/l4nnlt LbNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=q2fnifBG; 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 l16-20020a170902f69000b001c5e0672f53si3623164plg.466.2023.09.22.16.57.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 16:57:48 -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=q2fnifBG; 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 38373822E565; Fri, 22 Sep 2023 08:02:09 -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 S232624AbjIVPBD (ORCPT + 29 others); Fri, 22 Sep 2023 11:01:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232327AbjIVPAk (ORCPT ); Fri, 22 Sep 2023 11:00:40 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E95F194; Fri, 22 Sep 2023 08:00:32 -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=7Xg3VsT+niHE2xRvzgMpdBgGz0Csn+Ig0JZVwwD1HsY=; b=q2fnifBGofysyROgqfxrNhFc+T HPOPa4mspgeB7R2y7jM7SmMA9nwm9TO7b/RdaTGQvFZ7/RChzUyUvB+VbHVEg7o1QAlHp5g73xNj2 xkgoG7nKDyl7Et8Qcn5wemKrV9zieH3IhAc1JOIIJlu0ZIy2pz5SL8RjmSii4txgGFtg=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdt-000451-La; Fri, 22 Sep 2023 15:00:29 +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 1qjhdt-0005y2-Dh; Fri, 22 Sep 2023 15:00:29 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , David Woodhouse , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , x86@kernel.org Subject: [PATCH v5 06/10] KVM: xen: allow shared_info to be mapped by fixed HVA Date: Fri, 22 Sep 2023 15:00:05 +0000 Message-Id: <20230922150009.3319-7-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:02:09 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777784134262774930 X-GMAIL-MSGID: 1777784134262774930 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: 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 Fri Sep 22 15:00:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143714 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5921127vqi; Fri, 22 Sep 2023 16:15:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE8kr8XMwDeNH57EMBKNzrmH0KKB+bxJNSPeQv5LpTnMCVS5Zfbv2+Tmj211xz6r7FTmVtk X-Received: by 2002:a05:6a00:17a4:b0:68a:6cbe:35a7 with SMTP id s36-20020a056a0017a400b0068a6cbe35a7mr801955pfg.2.1695424550389; Fri, 22 Sep 2023 16:15:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695424550; cv=none; d=google.com; s=arc-20160816; b=QmuV25f/PBAJ1QqeMCR2ua9LhMpRqXYIsqeyGcbeXhT16o5+TK6YLbWinIrR0ASlvW hSAldJQ+vgijMqfHvbsvP9FnVEcaqmF6DuoZq8UvBXRlkVqF7CaSIGiPPHgrj4ycwMoW yjBxAV35JRc1pW1k+SFr+FOiYs0O1h1+/vklfXqBnPsCM3bnkWgmG6gZ5OHlzPYU6tNE Be61gvvkexS3QiAEQGiu62ICntWYvvbCS720s/vHnfe/DmYXiStwS5SC/n/T4Qbh/rIH hxm1jlXDZZVrPPnEWpQ+KF+kHJGrhdhldE0h4GCh8ik7CuuznCRhtileUsmATe8bv7p1 SolQ== 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=jnPSdLV2+HBfMn+U7RLHFrc6Ez+hteUN/fc7x4c3c78=; fh=F/y/rQAitAoVhyEvV7npxQC+pmk+LQOxsxEGW4WYgok=; b=0fNGfxDdCYIoPzx+12Pk4LN1HFBG6DGB+PQ+ec/HGFHjDZhZagLxex7PAk+1xwA2mc Ehf5PcxB4kbljn4VU7jz28oIIvFYHdl+drfkgvkRZGR6PzHm0wc7FZoCdgy/Y2yiK72B ABvPJ18ZyC4yIPtaN9I3C+etcvq6F4gv4f+9+5yy9cZzfzOZvRDONKwZhX28lldduENz /hh+S1RPvDkg/My/HXHiNLZcIZkMPs9H2KBrD2wzbJpCuZev7RKvN1tnrSfWKoV2cdK/ Y9NEqg+JXlTqFVAHvb4I0GWTi+nURKRkk8d2e18RdhA/BHppvec2tLlrGTPd2VfHlpwq oTdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=htYxrVfL; 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 z35-20020a056a001da300b00690f9e835d8si4659670pfw.132.2023.09.22.16.15.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 16:15:50 -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=htYxrVfL; 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 BBF85829BC67; Fri, 22 Sep 2023 08:01:50 -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 S232820AbjIVPBL (ORCPT + 29 others); Fri, 22 Sep 2023 11:01:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232305AbjIVPAn (ORCPT ); Fri, 22 Sep 2023 11:00:43 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E697A100; Fri, 22 Sep 2023 08:00:34 -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=jnPSdLV2+HBfMn+U7RLHFrc6Ez+hteUN/fc7x4c3c78=; b=htYxrVfLXN2/L8vr5yqzOIlGgp wdKSfK9/Ts1uJ/dWd3XoQV6P3lXtvDJEh2YqnL+xg9Z3Roby2zHCpaw7lUQFUcIZ9oOuGOpmULaPf vubrcIRhwatzkgkZAeP4f66cCxW43WAo3s3AvU379HSzW7LQsF17YWZt1NM+YgJuXT3Y=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdv-00045G-8v; Fri, 22 Sep 2023 15:00:31 +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 1qjhdv-0005y2-0E; Fri, 22 Sep 2023 15:00:31 +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 v5 07/10] KVM: xen: allow vcpu_info to be mapped by fixed HVA Date: Fri, 22 Sep 2023 15:00:06 +0000 Message-Id: <20230922150009.3319-8-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_PASS, SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:01:50 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777758995367168004 X-GMAIL-MSGID: 1777781492889794954 From: Paul Durrant If the guest does not explicitly set the GPA of vcpu_info structure in memory then, for guests with 32 vCPUs or fewer, the vcpu_info embedded in the shared_info page may be used. As described in a previous commit, the shared_info page is an overlay at a fixed HVA within the VMM, so in this case it also more optimal to activate the vcpu_info cache with a fixed HVA to avoid unnecessary invalidation if the guest memory layout is modified. Signed-off-by: Paul Durrant --- Cc: David Woodhouse Cc: Sean Christopherson Cc: Paolo Bonzini Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: x86@kernel.org v5: - New in this version. --- Documentation/virt/kvm/api.rst | 26 +++++++++++++++++++++----- arch/x86/kvm/xen.c | 33 +++++++++++++++++++++++++++------ include/uapi/linux/kvm.h | 3 +++ 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index e9df4df6fe48..5adc6dfc8c6e 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -5444,11 +5444,12 @@ KVM_XEN_ATTR_TYPE_SHARED_INFO Sets the guest physical frame number at which the Xen shared_info page resides. Note that although Xen places vcpu_info for the first 32 vCPUs in the shared_info page, KVM does not automatically do so - and instead requires that KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO be used - explicitly even when the vcpu_info for a given vCPU resides at the - "default" location in the shared_info page. This is because KVM may - not be aware of the Xen CPU id which is used as the index into the - vcpu_info[] array, so may know the correct default location. + and instead requires that KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO or + KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA be used explicitly even when + the vcpu_info for a given vCPU resides at the "default" location + in the shared_info page. This is because KVM may not be aware of + the Xen CPU id which is used as the index into the vcpu_info[] + array, so may know the correct default location. Note that the shared_info page may be constantly written to by KVM; it contains the event channel bitmap used to deliver interrupts to @@ -5570,6 +5571,21 @@ KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO on dirty logging. Setting the gpa to KVM_XEN_INVALID_GPA will disable the vcpu_info. +KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA + If the KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA flag is also set in the + Xen capabilities, then this attribute may be used to set the + userspace address of the vcpu_info for a given vCPU. It should + only be used when the vcpu_info resides at the "default" location + in the shared_info page. In this case it is safe to assume the + userspace address will not change, because the shared_info page is + an overlay on guest memory and remains at a fixed host address + regardless of where it is mapped in guest physical address space + and hence unnecessary invalidation of an internal cache may be + avoided if the guest memory layout is modified. + If the vcpu_info does not reside at the "default" location then + it is not guaranteed to remain at the same host address and + hence the aforementioned cache invalidation is required. + KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO Sets the guest physical address of an additional pvclock structure for a given vCPU. This is typically used for guest vsyscall support. diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 1abb4547642a..aafc794940e4 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -736,20 +736,33 @@ int kvm_xen_vcpu_set_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) switch (data->type) { case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO: + case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA: /* No compat necessary here. */ BUILD_BUG_ON(sizeof(struct vcpu_info) != sizeof(struct compat_vcpu_info)); BUILD_BUG_ON(offsetof(struct vcpu_info, time) != offsetof(struct compat_vcpu_info, time)); - if (data->u.gpa == KVM_XEN_INVALID_GPA) { - kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache); - r = 0; - break; + if (data->type == KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO) { + if (data->u.gpa == KVM_XEN_INVALID_GPA) { + kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache); + r = 0; + break; + } + + r = kvm_gpc_activate(&vcpu->arch.xen.vcpu_info_cache, + data->u.gpa, sizeof(struct vcpu_info)); + } else { + if (data->u.hva == 0) { + kvm_gpc_deactivate(&vcpu->arch.xen.vcpu_info_cache); + r = 0; + break; + } + + r = kvm_gpc_activate_hva(&vcpu->arch.xen.vcpu_info_cache, + data->u.hva, sizeof(struct vcpu_info)); } - r = kvm_gpc_activate(&vcpu->arch.xen.vcpu_info_cache, - data->u.gpa, sizeof(struct vcpu_info)); if (!r) kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); @@ -978,6 +991,14 @@ int kvm_xen_vcpu_get_attr(struct kvm_vcpu *vcpu, struct kvm_xen_vcpu_attr *data) r = 0; break; + case KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA: + if (vcpu->arch.xen.vcpu_info_cache.active) + data->u.hva = kvm_gpc_hva(&vcpu->arch.xen.vcpu_info_cache); + else + data->u.hva = 0; + r = 0; + break; + case KVM_XEN_VCPU_ATTR_TYPE_VCPU_TIME_INFO: if (vcpu->arch.xen.vcpu_time_info_cache.active) data->u.gpa = kvm_gpc_gpa(&vcpu->arch.xen.vcpu_time_info_cache); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 062bfa14b4d9..0267c2ef43de 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1858,6 +1858,7 @@ struct kvm_xen_vcpu_attr { union { __u64 gpa; #define KVM_XEN_INVALID_GPA ((__u64)-1) + __u64 hva; __u64 pad[8]; struct { __u64 state; @@ -1888,6 +1889,8 @@ struct kvm_xen_vcpu_attr { #define KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID 0x6 #define KVM_XEN_VCPU_ATTR_TYPE_TIMER 0x7 #define KVM_XEN_VCPU_ATTR_TYPE_UPCALL_VECTOR 0x8 +/* Available with KVM_CAP_XEN_HVM / KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA */ +#define KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA 0x9 /* Secure Encrypted Virtualization command */ enum sev_cmd_id { From patchwork Fri Sep 22 15:00:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143587 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5793245vqi; Fri, 22 Sep 2023 11:41:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGATPIVTaTfkPh1BeR6WZoPBrHxrABiF454LeNEcHY5vKfoUg7tFoCVzIhNhs45nfYBU15x X-Received: by 2002:a17:90a:4f4a:b0:276:cd68:608d with SMTP id w10-20020a17090a4f4a00b00276cd68608dmr459323pjl.22.1695408117281; Fri, 22 Sep 2023 11:41:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695408117; cv=none; d=google.com; s=arc-20160816; b=TROSyt54P4d9lQRShdVpAGXXWIe5GeofQgiMlbNrH+Zpigr51utynJXkfFGbG1lwZA 9QMRn93OquE/y2TvPHL1+QdvQapweJYYPGQVlueMl9eELEEvOGwi2xxjyFVpdif10b1q whKBTjbnlJeQLRvrTxs5KRPAcRXh9B2ugdXc4zLLmGZRQTcBsuYt3Yr2EJ4sDISWdTIq fUI+KaZv3M4cUoF3rzosjOrMKR9WxSZ3EedMW5tIqTfsy/8zbQtolW733b3mkp9GhnKG IWSs7VdpOaNlGEOefJyd0mBx2XjXCs49IcGzRZYaQBuuGxYpuQiwz2AvXhCmqnHjQvky zEvA== 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=kUR93LtEabvdWOBCjvrvvT9F7wNuAiZe4sASBA03DEM=; fh=lKhNn3P8ZvksS1fElybz1iP8zlw6JVVQ9gjuN2JwMdc=; b=Wekc4v+CW9C+i9eBY++cFg6LooqGMoo1zvZr6GYhGFtkZMr5eH+G9Qw2X86o3JWuQd Flnv7kIS0pIBw+Sl/md5+2cfiqkiFA3mhxkV9Rv198N8Qrqx3RKFC0SqqU3Qf3vhhc+a DRzZlqZBxSMF+SZ4MDWwRVqoZWO1eQTBg3TMIH431x4WDVcE75la+tyjoA2UFXkZ0usx /W2ovVzFsql/LYOtCZ9dR/WlQSk5Ye5noEbcvR8PuGd35YHSVfFiJujZp32SAYfIHjFC lPjCr0Yen5PMAoV1dd3kfmdmcc3Ic7lvpBwq0kCTHUWnef27b0pK6Q6Zmg2taZ1wZRFG simQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b="F4/3GEdZ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id e17-20020a17090a9a9100b00273f61d8773si4413755pjp.160.2023.09.22.11.41.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 11:41:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b="F4/3GEdZ"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 08D9480BE7D7; Fri, 22 Sep 2023 08:02:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232362AbjIVPA7 (ORCPT + 29 others); Fri, 22 Sep 2023 11:00:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232281AbjIVPAk (ORCPT ); Fri, 22 Sep 2023 11:00:40 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E08EE1A8; Fri, 22 Sep 2023 08:00:33 -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=kUR93LtEabvdWOBCjvrvvT9F7wNuAiZe4sASBA03DEM=; b=F4/3GEdZ47lRbXAFRxjOoDWMKP EbVZo9mtI3tlKeZWGYAa4uJDQ4vDOC4Wa55iJc6cKWDxEup493CeVJJ/49HKyf1ifdewOqyJgQ/An uNFoHjIwwoC3VV4JRRvjDUoGsnEGm+LkraqgQzyfzP+6JmRd2cU9Dqf+Y9xN5LMZ5e4A=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdw-00045X-Ac; Fri, 22 Sep 2023 15:00:32 +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 1qjhdw-0005y2-39; Fri, 22 Sep 2023 15:00:32 +0000 From: Paul Durrant To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Paul Durrant , David Woodhouse , Sean Christopherson , Paolo Bonzini , David Woodhouse Subject: [PATCH v5 08/10] KVM: selftests / xen: map shared_info using HVA rather than GFN Date: Fri, 22 Sep 2023 15:00:07 +0000 Message-Id: <20230922150009.3319-9-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Fri, 22 Sep 2023 08:02:31 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777764262265517819 X-GMAIL-MSGID: 1777764262265517819 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. NOTE: Have the juggle_shinfo_state() thread map and unmap using both GFN and HVA, to make sure the older mechanism is not broken. Signed-off-by: Paul Durrant Reviewed-by: David Woodhouse --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Woodhouse v3: - Re-work the juggle_shinfo_state() thread v2: - New in this version. --- .../selftests/kvm/x86_64/xen_shinfo_test.c | 44 +++++++++++++++---- 1 file changed, 35 insertions(+), 9 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..e6672ae1d9de 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -393,6 +393,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; @@ -408,20 +409,38 @@ static void *juggle_shinfo_state(void *arg) { struct kvm_vm *vm = (struct kvm_vm *)arg; - struct kvm_xen_hvm_attr cache_activate = { + struct kvm_xen_hvm_attr cache_activate_gfn = { .type = KVM_XEN_ATTR_TYPE_SHARED_INFO, .u.shared_info.gfn = SHINFO_REGION_GPA / PAGE_SIZE }; - struct kvm_xen_hvm_attr cache_deactivate = { + struct kvm_xen_hvm_attr cache_deactivate_gfn = { .type = KVM_XEN_ATTR_TYPE_SHARED_INFO, .u.shared_info.gfn = KVM_XEN_INVALID_GFN }; + struct kvm_xen_hvm_attr cache_activate_hva = { + .type = KVM_XEN_ATTR_TYPE_SHARED_INFO_HVA, + .u.shared_info.hva = (unsigned long)shinfo + }; + + struct kvm_xen_hvm_attr cache_deactivate_hva = { + .type = KVM_XEN_ATTR_TYPE_SHARED_INFO, + .u.shared_info.hva = 0 + }; + + int xen_caps = kvm_check_cap(KVM_CAP_XEN_HVM); + for (;;) { - __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate); - __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate); + __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate_gfn); pthread_testcancel(); + __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate_gfn); + + if (xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA) { + __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_activate_hva); + pthread_testcancel(); + __vm_ioctl(vm, KVM_XEN_HVM_SET_ATTR, &cache_deactivate_hva); + } } return NULL; @@ -446,6 +465,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_shinfo_hva = !!(xen_caps & KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA); clock_gettime(CLOCK_REALTIME, &min_ts); @@ -456,7 +476,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"); @@ -492,10 +512,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_REGION_GPA / 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); /* From patchwork Fri Sep 22 15:00:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143700 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5898860vqi; Fri, 22 Sep 2023 15:21:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH3nI2wkMQn5xH4lsYuQZw1ou40F1Cj/+aPo+u5XhbcdDOohHL0Y/L25/xquk0FdD8UgHap X-Received: by 2002:a17:902:d2cf:b0:1c3:aef9:872e with SMTP id n15-20020a170902d2cf00b001c3aef9872emr985890plc.34.1695421306531; Fri, 22 Sep 2023 15:21:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695421306; cv=none; d=google.com; s=arc-20160816; b=qC5xz9/u7n1FiLevdyQTrgA6Szconxk4VtFMojEvJP5qFaKFsci1m5ALeRQdUBMuqZ elxGLvLumNw7KRaZBBHqX1wpVm2LtJvg2MUnmRgN4IZU20bo9nhUEZyqtuIU3C9GJljp j9nZLtrpxgDMsOJFnLBFxdJVPJgt/WbV5WIwspyPhlH7j+gWjeHNoSqCetLKw84hv/7y 0YwnoozneCtzoHwZhWzNw+qpvezlqF63Rr1gxVyVAQFkloTmTU7owg/2ZmNwxcMPcA9g TvNWs7BXo39SI1JjvEaFxRfBzOfOPr+0f6sjMfqKlvbBKMaYKuW1RltsClTHt8v9uzMm hXgA== 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=wvF5vbKQ0oqapTek2ihIVp8v4huETUVpmznY/AbmTjg=; fh=NVKswJvclYk1MEC7AfFzo67YIylApYMwZ7eFzXKDgdk=; b=dxwCW+7/TyhuPlOhPBdt9Q2/jQAN/g7PxvvAVycaA5dXjkFp45W2OfF5JoTfdtDe7k Anki/4TESoLF/2MSLAG6OL4ovPdjnMxaynqBf7HsM6pQcmuIkZ08qPi2+mhYHDkJe97+ Xg07pz5gBuipp8gv/NFb3dMP99w17joKRNdrO2djBppsHWcvtlA/Hn/0xNJlheuIkSz3 4ztx83rplVkLK+1HsCMBRlHKwJAXbxuF7CaaahfTwy/CqRRtWQQX/f+w2a6gygiCSnyX dqJVwSQkKc0luGYZXL7qNdu3PLVZ3VCVv2JoaS4EBGRlJsjltZCSuVqgoYvYgONd+GsV MJjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=HUU9PELS; 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 l11-20020a170903120b00b001b5589848absi4870440plh.234.2023.09.22.15.21.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 15:21:46 -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=HUU9PELS; 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 06AA8822E577; Fri, 22 Sep 2023 08:02:03 -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 S232683AbjIVPBH (ORCPT + 29 others); Fri, 22 Sep 2023 11:01:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232326AbjIVPAk (ORCPT ); Fri, 22 Sep 2023 11:00:40 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9863EC6; Fri, 22 Sep 2023 08:00:34 -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=wvF5vbKQ0oqapTek2ihIVp8v4huETUVpmznY/AbmTjg=; b=HUU9PELSPBkRTUD0OXTp2POR7l F/8W4olaRgQe4T4AYBgIYC+7o/Go6lA7GAEkCvUPJ7Ij4AQAxto9ME+y6CMUOJMOmVhanY9O+XXvU vrQiR2fsqxZlC4Zk7iv+AnD7BfC3jQsgDednOmUpsRXLNUedBupmj7SPf2tF3xuUlPSE=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qjhdx-00045k-AE; Fri, 22 Sep 2023 15:00:33 +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 1qjhdx-0005y2-2o; Fri, 22 Sep 2023 15:00:33 +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 v5 09/10] KVM: selftests / xen: re-map vcpu_info using HVA rather than GPA Date: Fri, 22 Sep 2023 15:00:08 +0000 Message-Id: <20230922150009.3319-10-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED 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]); Fri, 22 Sep 2023 08:02:03 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777763782424910924 X-GMAIL-MSGID: 1777778091912661097 From: Paul Durrant If the relevant capability (KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA) is present then re-map vcpu_info using the HVA part way through the tests to make sure then there is no functional change. Signed-off-by: Paul Durrant --- Cc: Sean Christopherson Cc: Paolo Bonzini Cc: David Woodhouse v5: - New in this version. --- .../selftests/kvm/x86_64/xen_shinfo_test.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 e6672ae1d9de..a5d3aea8fd95 100644 --- a/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c +++ b/tools/testing/selftests/kvm/x86_64/xen_shinfo_test.c @@ -66,6 +66,7 @@ enum { TEST_POLL_TIMEOUT, TEST_POLL_MASKED, TEST_POLL_WAKE, + SET_VCPU_INFO, TEST_TIMER_PAST, TEST_LOCKING_SEND_RACE, TEST_LOCKING_POLL_RACE, @@ -325,6 +326,10 @@ static void guest_code(void) GUEST_SYNC(TEST_POLL_WAKE); + /* Set the vcpu_info to point at exactly the place it already is to + * make sure the attribute is functional. */ + GUEST_SYNC(SET_VCPU_INFO); + /* A timer wake an *unmasked* port which should wake us with an * actual interrupt, while we're polling on a different port. */ ports[0]++; @@ -892,6 +897,16 @@ int main(int argc, char *argv[]) alarm(1); break; + case SET_VCPU_INFO: + if (has_shinfo_hva) { + struct kvm_xen_vcpu_attr vih = { + .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_INFO_HVA, + .u.hva = (unsigned long)vinfo + }; + vcpu_ioctl(vcpu, KVM_XEN_VCPU_SET_ATTR, &vih); + } + break; + case TEST_TIMER_PAST: TEST_ASSERT(!evtchn_irq_expected, "Expected event channel IRQ but it didn't happen"); From patchwork Fri Sep 22 15:00:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Durrant X-Patchwork-Id: 143512 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp5671437vqi; Fri, 22 Sep 2023 08:37:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH2vxKXbqIh1T3EqD8dp4qHHfnysw8ax3gHzHHHn+GqdEwDBELzkjidtn4pxJu7/nX+Th49 X-Received: by 2002:a17:90a:4507:b0:276:fd4c:c20b with SMTP id u7-20020a17090a450700b00276fd4cc20bmr57856pjg.5.1695397019835; Fri, 22 Sep 2023 08:36:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695397019; cv=none; d=google.com; s=arc-20160816; b=FTwYXHutWGwnVouQ4W7KWrBE4+wb69wyaVIt2aRja2hi3wKnJTmMwetwFMI2peCl/r ZoujDyIxIJSg2Gj40PZjncN0atKbD4wuNiWx3kg3sVL7TZp7hKnWW7A+pCWaF0NH3hk/ lSTu+yy3N+GISSQo7OLqTduHxlCmKaNT2qoj5XzJGQFPWKCKZqu/yIRuKT3it06/gfLF IX9enxsg3/2QdN2E7J4BwuBZXkI6HSAHi5hr9pMJzlQtcuHINNuatR6XsEN7i9y6okeB +/0WTXIKMepJkknhL7sfrjjg3J5nDSVviffmvW6O9qYBrKQW6E6fCM1OB9UVkH9kQa5V xVcg== 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=OnZ8zSAkss2pUJLPO3TlCjqbELhF/Q5FJAtYDHD/8T0=; fh=jcKAuV9sx5P88dpWYyXihS1UO5TLmUGd/2Iq3QREE4o=; b=pYhCAOeInm2+BKjSlV0fChFf4+V1H7SCN9CaOs3NVK5SS/ME/qOqLdyu07qnD5/WG/ P2955wRkSswefk60ADj9QYxiwYoXO0ze35oif4crFZBQXeqXVUofwQ3d3F+j+rhKxDP7 T+BVpROSOXohUkei5yIETwXJ/auRw89AhKZD6D5JD3e6rmh15zKSfbEs+fJnDDnnZtW5 1VbwGHQYcEqTGnuhYniKXozyeUhQuQLg9O2Od6KNrJRTK3ZsrWRM/fzqsqTtCZjKrOt1 y2hbFLD9Xu2QlEzPoBwfDjX6lPHzpJjIzXBno7j89jcOdVYxCJkS6MslOvYjeaC69M0W 6LVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=e9jnP1XK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id d19-20020a637353000000b00578aedd8e8bsi3714267pgn.716.2023.09.22.08.36.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 08:36:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@xen.org header.s=20200302mail header.b=e9jnP1XK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 813228076173; Fri, 22 Sep 2023 08:30:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231594AbjIVPa1 (ORCPT + 29 others); Fri, 22 Sep 2023 11:30:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229849AbjIVPa0 (ORCPT ); Fri, 22 Sep 2023 11:30:26 -0400 Received: from mail.xenproject.org (mail.xenproject.org [104.130.215.37]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F0A6A3; Fri, 22 Sep 2023 08:30: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=OnZ8zSAkss2pUJLPO3TlCjqbELhF/Q5FJAtYDHD/8T0=; b=e9jnP1XKXGk6uTnl1Y8k/Il6Ys yNWAouT1UGbBCyEHFe2xRl6m16hNHh8P19R+cdCpYalGS69cvda395sVUg3iwji6Q1iJxuwXw8Zjo TY4E3ivA95PVIxbQCjdjkqnkceIvRpNYobjLltWfuZ/3PsUVh4K5t9YAzrZ1gNylabcs=; Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1qji6i-0004nj-CD; Fri, 22 Sep 2023 15:30: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 1qjhdy-0005y2-Oi; Fri, 22 Sep 2023 15:00:34 +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" , David Woodhouse , x86@kernel.org Subject: [PATCH v5 10/10] KVM: xen: advertize the KVM_XEN_HVM_CONFIG_SHARED_INFO_HVA capability Date: Fri, 22 Sep 2023 15:00:09 +0000 Message-Id: <20230922150009.3319-11-paul@xen.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230922150009.3319-1-paul@xen.org> References: <20230922150009.3319-1-paul@xen.org> MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Fri, 22 Sep 2023 08:30:41 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777752625483654325 X-GMAIL-MSGID: 1777752625483654325 From: Paul Durrant Now that all relevant kernel changes and selftests are in place, enable the new capability. 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: 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 d669a8801265..0df06f47801c 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4531,7 +4531,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;