From nobody Sun Feb 8 13:53:22 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail header.i=@intel.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linux.intel.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1642517230842666.6658698776008; Tue, 18 Jan 2022 06:47:10 -0800 (PST) Received: from localhost ([::1]:34834 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1n9plN-0001kt-R6 for importer@patchew.org; Tue, 18 Jan 2022 09:47:09 -0500 Received: from eggs.gnu.org ([209.51.188.92]:40632) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n9oSI-0001Po-Bh for qemu-devel@nongnu.org; Tue, 18 Jan 2022 08:23:23 -0500 Received: from mga04.intel.com ([192.55.52.120]:18828) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1n9oSG-0003I3-G9 for qemu-devel@nongnu.org; Tue, 18 Jan 2022 08:23:22 -0500 Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Jan 2022 05:23:19 -0800 Received: from chaop.bj.intel.com ([10.240.192.101]) by orsmga008.jf.intel.com with ESMTP; 18 Jan 2022 05:23:11 -0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1642512200; x=1674048200; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=Doiq2R3hAItBU5LQnTBxQCkUWyVHPsvtgI1nQR7ap4A=; b=nw8XdpK83ub5ouEpKhp+qBIKBr4g+RbxGY6mo3JUfKQE4651wpbm/ldO tiiZYahVRlKoq1rv9vu4rAsbfLP4sgpYABxJQ32omRSvaStfJ9gAJ4szq Wf8z49am59aAANOXsWlDrWFHHgmmXu1o/8y9dyGVpvZb1I8b7F+/eOsW7 MWrShOlbh6oHulKCi6A3FxFg/jufmjhQ3yMklfYSoxjbFeBh/6OxLiQ9q WoDI21BL5npeWJJRVAqgK7ldaa5N4XhklIs+jmMougUBzLS2awxpR9q9I O2TilyCST7BNcYdRB7Cq0cPK82yXgPHE0MnTx0DMzWoWpz+nMK0HN9CNZ g==; X-IronPort-AV: E=McAfee;i="6200,9189,10230"; a="243636413" X-IronPort-AV: E=Sophos;i="5.88,297,1635231600"; d="scan'208";a="243636413" X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,297,1635231600"; d="scan'208";a="531791936" From: Chao Peng To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, qemu-devel@nongnu.org Subject: [PATCH v4 11/12] KVM: Zap existing KVM mappings when pages changed in the private fd Date: Tue, 18 Jan 2022 21:21:20 +0800 Message-Id: <20220118132121.31388-12-chao.p.peng@linux.intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220118132121.31388-1-chao.p.peng@linux.intel.com> References: <20220118132121.31388-1-chao.p.peng@linux.intel.com> Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=192.55.52.120; envelope-from=chao.p.peng@linux.intel.com; helo=mga04.intel.com X-Spam_score_int: -49 X-Spam_score: -5.0 X-Spam_bar: ----- X-Spam_report: (-5.0 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.7, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Wanpeng Li , luto@kernel.org, david@redhat.com, "J . Bruce Fields" , dave.hansen@intel.com, "H . Peter Anvin" , Chao Peng , ak@linux.intel.com, Jonathan Corbet , Joerg Roedel , x86@kernel.org, Hugh Dickins , Ingo Molnar , Borislav Petkov , jun.nakajima@intel.com, Thomas Gleixner , Vitaly Kuznetsov , Jim Mattson , Sean Christopherson , Jeff Layton , Yu Zhang , Paolo Bonzini , Andrew Morton , "Kirill A . Shutemov" Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1642517231412100003 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" KVM gets notified when memory pages changed in the memory backing store. When userspace allocates the memory with fallocate() or frees memory with fallocate(FALLOC_FL_PUNCH_HOLE), memory backing store calls into KVM fallocate/invalidate callbacks respectively. To ensure KVM never maps both the private and shared variants of a GPA into the guest, in the fallocate callback, we should zap the existing shared mapping and in the invalidate callback we should zap the existing private mapping. In the callbacks, KVM firstly converts the offset range into the gfn_range and then calls existing kvm_unmap_gfn_range() which will zap the shared or private mapping. Both callbacks pass in a memslot reference but we need 'kvm' so add a reference in memslot structure. Signed-off-by: Yu Zhang Signed-off-by: Chao Peng --- include/linux/kvm_host.h | 3 ++- virt/kvm/kvm_main.c | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 667efe839767..117cf0da9c5e 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -235,7 +235,7 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr= 2_or_gpa, int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); #endif =20 -#ifdef KVM_ARCH_WANT_MMU_NOTIFIER +#if defined(KVM_ARCH_WANT_MMU_NOTIFIER) || defined(CONFIG_MEMFILE_NOTIFIER) struct kvm_gfn_range { struct kvm_memory_slot *slot; gfn_t start; @@ -463,6 +463,7 @@ struct kvm_memory_slot { loff_t private_offset; struct memfile_pfn_ops *pfn_ops; struct memfile_notifier notifier; + struct kvm *kvm; }; =20 static inline bool kvm_slot_is_private(const struct kvm_memory_slot *slot) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6b78ddef7880..10e553215618 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -847,8 +847,43 @@ static int kvm_init_mmu_notifier(struct kvm *kvm) #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ =20 #ifdef CONFIG_MEMFILE_NOTIFIER +static void kvm_memfile_notifier_handler(struct memfile_notifier *notifier, + pgoff_t start, pgoff_t end) +{ + int idx; + struct kvm_memory_slot *slot =3D container_of(notifier, + struct kvm_memory_slot, + notifier); + struct kvm_gfn_range gfn_range =3D { + .slot =3D slot, + .start =3D start - (slot->private_offset >> PAGE_SHIFT), + .end =3D end - (slot->private_offset >> PAGE_SHIFT), + .may_block =3D true, + }; + struct kvm *kvm =3D slot->kvm; + + gfn_range.start =3D max(gfn_range.start, slot->base_gfn); + gfn_range.end =3D min(gfn_range.end, slot->base_gfn + slot->npages); + + if (gfn_range.start >=3D gfn_range.end) + return; + + idx =3D srcu_read_lock(&kvm->srcu); + KVM_MMU_LOCK(kvm); + kvm_unmap_gfn_range(kvm, &gfn_range); + kvm_flush_remote_tlbs(kvm); + KVM_MMU_UNLOCK(kvm); + srcu_read_unlock(&kvm->srcu, idx); +} + +static struct memfile_notifier_ops kvm_memfile_notifier_ops =3D { + .invalidate =3D kvm_memfile_notifier_handler, + .fallocate =3D kvm_memfile_notifier_handler, +}; + static inline int kvm_memfile_register(struct kvm_memory_slot *slot) { + slot->notifier.ops =3D &kvm_memfile_notifier_ops; return memfile_register_notifier(file_inode(slot->private_file), &slot->notifier, &slot->pfn_ops); @@ -1969,6 +2004,7 @@ int __kvm_set_memory_region(struct kvm *kvm, new->private_file =3D file; new->private_offset =3D mem->flags & KVM_MEM_PRIVATE ? region_ext->private_offset : 0; + new->kvm =3D kvm; =20 r =3D kvm_set_memslot(kvm, old, new, change); if (!r) --=20 2.17.1