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 5A540C433F5 for ; Tue, 19 Apr 2022 22:44:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE8AC6B0072; Tue, 19 Apr 2022 18:44:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D98786B0073; Tue, 19 Apr 2022 18:44:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C60F46B0074; Tue, 19 Apr 2022 18:44:09 -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 B721F6B0072 for ; Tue, 19 Apr 2022 18:44:09 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8FE932794 for ; Tue, 19 Apr 2022 22:44:09 +0000 (UTC) X-FDA: 79375108218.13.B0C0B06 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by imf19.hostedemail.com (Postfix) with ESMTP id 8C3461A0018 for ; Tue, 19 Apr 2022 22:44:07 +0000 (UTC) Received: by mail-pf1-f171.google.com with SMTP id x1so173683pfj.2 for ; Tue, 19 Apr 2022 15:44:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=VLhwP2K5wAQdNCL/I+laKlmhhmXTIYpsE+P3ZBVdEQY=; b=r6feRZnKvLFz70LElt69P5H+fCR2FPIhGGDf/HEN3FRecAYZ9XCe48UHMGy+CySLim Nf5DSsTsuheV1j+T6ia1xrZDpsTF3FTPXu+PVf45W3eRaPm0iYir4XnaqNkn/g+rZCO2 +mve4Z0JSADruCniouTjwzcvVDbH3e2O3juiT4JkLMIr7ZXwKFqeJlsDlQ8PoQ143YKV 4mXbdBPsHAvnmaRpFABAN6Fu2TxYwMuj7lx3m1l+FUNKtOImv1EuD09eyGgiiWGIIwwL LNoj8AvJNwOZZPiT6ZBr88Y7Sec+d18PBB4ep9ryJg1nLwP6Jf9EVG8Bvth2Y321pPhZ R+rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=VLhwP2K5wAQdNCL/I+laKlmhhmXTIYpsE+P3ZBVdEQY=; b=xFAkf0kNsFbVpWJz6CJWthC87NqrSoyrtJK36SNmYnyK5C8risMULhotvXar89DuAB XSkg8kpiHmA7Wh95G4e/V9baLy7HNhAdwxX5Lm91hTIvgpSDQ4kgHG4Itolh9QqavQpc 6qn6WnFzaf6z8LsaVePWcBEEIoIrRxanT+Nl9n6esTFvSiLliznFWifcMAgvsKbzoUI4 YmJAalh0TCAu/W3su9b1r9sAM1+KygchKOLRAKAs0+8wqvPZyRK1sotH45LmeAclTMSP VVexS4BBFUh4uwPJGgKePTSiDnqt1pYtxexwA+AP3pUalqSJmcCIAhMK+K1j8FcgVghM lkhQ== X-Gm-Message-State: AOAM5312IjTCO2zgg0mtfMAIk4HeYo7IeCT9JJW08TOHhdJmpwOEQy2P etEPF84yY2mzmnbMQ7ZIDuldifhiCxROz51kgZli0g== X-Google-Smtp-Source: ABdhPJy1nh6THruK7UwMlM2wpxzbJNt+bc4+NMZX+nwIAjwmQ/JmNyUx85odCJYd4rFIN0fRrs6lthI6/1JylF8mddI= X-Received: by 2002:a65:56cb:0:b0:378:82ed:d74 with SMTP id w11-20020a6556cb000000b0037882ed0d74mr16683302pgs.491.1650408247642; Tue, 19 Apr 2022 15:44:07 -0700 (PDT) MIME-Version: 1.0 References: <20220310140911.50924-1-chao.p.peng@linux.intel.com> <20220310140911.50924-12-chao.p.peng@linux.intel.com> In-Reply-To: <20220310140911.50924-12-chao.p.peng@linux.intel.com> From: Vishal Annapurve Date: Tue, 19 Apr 2022 15:43:56 -0700 Message-ID: Subject: Re: [PATCH v5 11/13] KVM: Zap existing KVM mappings when pages changed in the private fd To: Chao Peng 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 Content-Type: text/plain; charset="UTF-8" X-Rspam-User: X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 8C3461A0018 X-Stat-Signature: wpniy6xsbikx7gwmg4aid5e7mfqnmfwf Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=r6feRZnK; spf=pass (imf19.hostedemail.com: domain of vannapurve@google.com designates 209.85.210.171 as permitted sender) smtp.mailfrom=vannapurve@google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1650408247-947379 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 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); > + 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); > + 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 >