From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F26A3C433F5 for ; Wed, 20 Apr 2022 03:17:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F27086B0072; Tue, 19 Apr 2022 23:17:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ED7476B0073; Tue, 19 Apr 2022 23:17:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D77F46B0074; Tue, 19 Apr 2022 23:17:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id C65AE6B0072 for ; Tue, 19 Apr 2022 23:17:38 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8D71220D03 for ; Wed, 20 Apr 2022 03:17:38 +0000 (UTC) X-FDA: 79375797396.25.339E62F Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by imf29.hostedemail.com (Postfix) with ESMTP id DB836120006 for ; Wed, 20 Apr 2022 03:17:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1650424657; x=1681960657; h=date:from:to:cc:subject:message-id:reply-to:references: mime-version:in-reply-to; bh=aSlmWYo67979bXI+8rgE6ppMUFAAUoPR5mX4oC2meD8=; b=KAQJq2zJPTrg4MlnI264LLqBpFVmK4c17/ZeMD+xIuud2BM2HuSu6C3b Oap9LvPu+1BjlQIgnRltpdk3dkIWuKOz9Ciuo4/e7HBVVBTeq6RwGFpXj XZGAWRHQ/x2GZu0NAnoS7sjGbX6gEkuAKY3ukHtQED5aVBwd0f6avKffS +6MlcEkOxnphsR0JVN00trQHRdWeOaYNYBKWtx5CSjFoslBlAuisczVsQ 0yvKzczgxwTruAinlEJxLpQDti2vruSCZ4BxZRNxSVNZUu3yVjEqbWLrY g+glg0OUFKrJSp8OKuVo13xJFEFF/XhI41Gk6YrbwxKkCjpf5HD9u4XK+ A==; X-IronPort-AV: E=McAfee;i="6400,9594,10322"; a="244509673" X-IronPort-AV: E=Sophos;i="5.90,274,1643702400"; d="scan'208";a="244509673" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Apr 2022 20:17:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,274,1643702400"; d="scan'208";a="667588846" Received: from chaop.bj.intel.com (HELO localhost) ([10.240.192.101]) by orsmga004.jf.intel.com with ESMTP; 19 Apr 2022 20:17:27 -0700 Date: Wed, 20 Apr 2022 11:17:18 +0800 From: Chao Peng To: Vishal Annapurve Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Yu Zhang , "Kirill A . Shutemov" , Andy Lutomirski , Jun Nakajima , dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com Subject: Re: [PATCH v5 11/13] KVM: Zap existing KVM mappings when pages changed in the private fd Message-ID: <20220420031718.GA39591@chaop.bj.intel.com> Reply-To: Chao Peng References: <20220310140911.50924-1-chao.p.peng@linux.intel.com> <20220310140911.50924-12-chao.p.peng@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: DB836120006 X-Stat-Signature: cyso44okui46xqrtyj37azffpg7o4no9 Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=KAQJq2zJ; dmarc=pass (policy=none) header.from=intel.com; spf=none (imf29.hostedemail.com: domain of chao.p.peng@linux.intel.com has no SPF policy when checking 192.55.52.151) smtp.mailfrom=chao.p.peng@linux.intel.com X-HE-Tag: 1650424656-884670 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, Apr 19, 2022 at 03:43:56PM -0700, Vishal Annapurve wrote: > On Thu, Mar 10, 2022 at 6:11 AM Chao Peng wrote: > > > > 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 9b175aeca63f..186b9b981a65 100644 > > --- a/include/linux/kvm_host.h > > +++ b/include/linux/kvm_host.h > > @@ -236,7 +236,7 @@ bool kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, > > int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu); > > #endif > > > > -#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; > > @@ -568,6 +568,7 @@ struct kvm_memory_slot { > > loff_t private_offset; > > struct memfile_pfn_ops *pfn_ops; > > struct memfile_notifier notifier; > > + struct kvm *kvm; > > }; > > > > 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 67349421eae3..52319f49d58a 100644 > > --- a/virt/kvm/kvm_main.c > > +++ b/virt/kvm/kvm_main.c > > @@ -841,8 +841,43 @@ static int kvm_init_mmu_notifier(struct kvm *kvm) > > #endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */ > > > > #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 = container_of(notifier, > > + struct kvm_memory_slot, > > + notifier); > > + struct kvm_gfn_range gfn_range = { > > + .slot = slot, > > + .start = start - (slot->private_offset >> PAGE_SHIFT), > > + .end = end - (slot->private_offset >> PAGE_SHIFT), > > + .may_block = true, > > + }; > > + struct kvm *kvm = slot->kvm; > > + > > + gfn_range.start = max(gfn_range.start, slot->base_gfn); > > gfn_range.start seems to be page offset within the file. Should this rather be: > gfn_range.start = slot->base_gfn + min(gfn_range.start, slot->npages); Right. For start we don't really need care about the uppper bound here (will check below), so this should be enough: gfn_range.start = slot->base_gfn + gfn_range.start; > > > + gfn_range.end = min(gfn_range.end, slot->base_gfn + slot->npages); > > + > > Similar to previous comment, should this rather be: > gfn_range.end = slot->base_gfn + min(gfn_range.end, slot->npages); This is correct. Thanks, Chao > > > + if (gfn_range.start >= gfn_range.end) > > + return; > > + > > + idx = 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 = { > > + .invalidate = kvm_memfile_notifier_handler, > > + .fallocate = kvm_memfile_notifier_handler, > > +}; > > + > > static inline int kvm_memfile_register(struct kvm_memory_slot *slot) > > { > > + slot->notifier.ops = &kvm_memfile_notifier_ops; > > return memfile_register_notifier(file_inode(slot->private_file), > > &slot->notifier, > > &slot->pfn_ops); > > @@ -1963,6 +1998,7 @@ int __kvm_set_memory_region(struct kvm *kvm, > > new->private_file = file; > > new->private_offset = mem->flags & KVM_MEM_PRIVATE ? > > region_ext->private_offset : 0; > > + new->kvm = kvm; > > > > r = kvm_set_memslot(kvm, old, new, change); > > if (!r) > > -- > > 2.17.1 > >