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 D6247C43217 for ; Tue, 12 Apr 2022 12:56:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FAA36B0082; Tue, 12 Apr 2022 08:56:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0AA2F6B0083; Tue, 12 Apr 2022 08:56:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E8C766B0085; Tue, 12 Apr 2022 08:56:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.a.hostedemail.com [64.99.140.24]) by kanga.kvack.org (Postfix) with ESMTP id D62706B0082 for ; Tue, 12 Apr 2022 08:56:38 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9A31D24A04 for ; Tue, 12 Apr 2022 12:56:38 +0000 (UTC) X-FDA: 79348226076.02.694E37B Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf22.hostedemail.com (Postfix) with ESMTP id 8746FC0004 for ; Tue, 12 Apr 2022 12:56:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1649768197; x=1681304197; h=date:from:to:cc:subject:message-id:reply-to:references: mime-version:in-reply-to; bh=C6dk3/dvShTsf13TiSzcqE0mSsR+MyMx9FW9JNjPSEc=; b=essNIswIG9yjzI/mAIU0BFKfyefssjEBa3x/JBYAWYjDrt5gOKqB5gqj 2Q/S0f/OQ5OolveoXcOXkGKsxXUq392JdAYPxQhP5yR28/AIXWvcBzHyY tCKQov4zDCE20Q2y1Ty3PAj75b400s1aUCKU0Pc0lrxo7OOnEzQnAzAsn Dz/RsQRYcQ5IQLIEDROiDjlX/EMAUJ7rnoSYtYDehWtI0jT/OUMU83fIX FEy961o9o5wFoNbcYTTrWK/dBOR/x0Y5tos9ZgYuijVO5WCDOAgiYfLWw hZPWaUqhNMwhKfcPv3Se2wyB4NOS2xAwDG+0xiGbtrSG5ahVUS/kjJtBp Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10314"; a="262114701" X-IronPort-AV: E=Sophos;i="5.90,253,1643702400"; d="scan'208";a="262114701" Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Apr 2022 05:56:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.90,253,1643702400"; d="scan'208";a="699824982" Received: from chaop.bj.intel.com (HELO localhost) ([10.240.192.101]) by fmsmga001.fm.intel.com with ESMTP; 12 Apr 2022 05:56:28 -0700 Date: Tue, 12 Apr 2022 20:56:18 +0800 From: Chao Peng To: Sean Christopherson 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 , 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 , Vishal Annapurve , Yu Zhang , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com Subject: Re: [PATCH v5 12/13] KVM: Expose KVM_MEM_PRIVATE Message-ID: <20220412125618.GC8013@chaop.bj.intel.com> Reply-To: Chao Peng References: <20220310140911.50924-1-chao.p.peng@linux.intel.com> <20220310140911.50924-13-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-Stat-Signature: epfcrnmrsijoz7drnncciw88hfu6nko9 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=essNIswI; spf=none (imf22.hostedemail.com: domain of chao.p.peng@linux.intel.com has no SPF policy when checking 134.134.136.65) smtp.mailfrom=chao.p.peng@linux.intel.com; dmarc=pass (policy=none) header.from=intel.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 8746FC0004 X-HE-Tag: 1649768197-336977 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, Mar 29, 2022 at 07:13:00PM +0000, Sean Christopherson wrote: > On Thu, Mar 10, 2022, Chao Peng wrote: > > KVM_MEM_PRIVATE is not exposed by default but architecture code can turn > > on it by implementing kvm_arch_private_memory_supported(). > > > > Signed-off-by: Yu Zhang > > Signed-off-by: Chao Peng > > --- > > include/linux/kvm_host.h | 1 + > > virt/kvm/kvm_main.c | 24 +++++++++++++++++++----- > > 2 files changed, 20 insertions(+), 5 deletions(-) > > > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > > index 186b9b981a65..0150e952a131 100644 > > --- a/include/linux/kvm_host.h > > +++ b/include/linux/kvm_host.h > > @@ -1432,6 +1432,7 @@ bool kvm_arch_dy_has_pending_interrupt(struct kvm_vcpu *vcpu); > > int kvm_arch_post_init_vm(struct kvm *kvm); > > void kvm_arch_pre_destroy_vm(struct kvm *kvm); > > int kvm_arch_create_vm_debugfs(struct kvm *kvm); > > +bool kvm_arch_private_memory_supported(struct kvm *kvm); > > > > #ifndef __KVM_HAVE_ARCH_VM_ALLOC > > /* > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > > index 52319f49d58a..df5311755a40 100644 > > --- a/virt/kvm/kvm_main.c > > +++ b/virt/kvm/kvm_main.c > > @@ -1485,10 +1485,19 @@ static void kvm_replace_memslot(struct kvm *kvm, > > } > > } > > > > -static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem) > > +bool __weak kvm_arch_private_memory_supported(struct kvm *kvm) > > +{ > > + return false; > > +} > > + > > +static int check_memory_region_flags(struct kvm *kvm, > > + const struct kvm_userspace_memory_region *mem) > > { > > u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES; > > > > + if (kvm_arch_private_memory_supported(kvm)) > > + valid_flags |= KVM_MEM_PRIVATE; > > + > > #ifdef __KVM_HAVE_READONLY_MEM > > valid_flags |= KVM_MEM_READONLY; > > #endif > > @@ -1900,7 +1909,7 @@ int __kvm_set_memory_region(struct kvm *kvm, > > int as_id, id; > > int r; > > > > - r = check_memory_region_flags(mem); > > + r = check_memory_region_flags(kvm, mem); > > if (r) > > return r; > > > > @@ -1913,10 +1922,12 @@ int __kvm_set_memory_region(struct kvm *kvm, > > return -EINVAL; > > if (mem->guest_phys_addr & (PAGE_SIZE - 1)) > > return -EINVAL; > > - /* We can read the guest memory with __xxx_user() later on. */ > > if ((mem->userspace_addr & (PAGE_SIZE - 1)) || > > - (mem->userspace_addr != untagged_addr(mem->userspace_addr)) || > > - !access_ok((void __user *)(unsigned long)mem->userspace_addr, > > + (mem->userspace_addr != untagged_addr(mem->userspace_addr))) > > + return -EINVAL; > > + /* We can read the guest memory with __xxx_user() later on. */ > > + if (!(mem->flags & KVM_MEM_PRIVATE) && > > + !access_ok((void __user *)(unsigned long)mem->userspace_addr, > > This should sanity check private_offset for private memslots. At a bare minimum, > wrapping should be disallowed. Will add this. > > > mem->memory_size)) > > return -EINVAL; > > if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM) > > @@ -1957,6 +1968,9 @@ int __kvm_set_memory_region(struct kvm *kvm, > > if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages) > > return -EINVAL; > > } else { /* Modify an existing slot. */ > > + /* Private memslots are immutable, they can only be deleted. */ > > + if (mem->flags & KVM_MEM_PRIVATE) > > + return -EINVAL; > > These sanity checks belong in "KVM: Register private memslot to memory backing store", > e.g. that patch is "broken" without the immutability restriction. It's somewhat moot > because the code is unreachable, but it makes reviewing confusing/difficult. > > But rather than move the sanity checks back, I think I'd prefer to pull all of patch 10 > here. I think it also makes sense to drop "KVM: Use memfile_pfn_ops to obtain pfn for > private pages" and add the pointer in "struct kvm_memory_slot" in patch "KVM: Extend the > memslot to support fd-based private memory", with the use of the ops folded into > "KVM: Handle page fault for private memory". Adding code to KVM and KVM-x86 in a single > patch is ok, and overall makes things easier to review because the new helpers have a > user right away, especially since there will be #ifdeffery. > > I.e. end up with something like: > > mm: Introduce memfile_notifier > mm/shmem: Restrict MFD_INACCESSIBLE memory against RLIMIT_MEMLOCK > KVM: Extend the memslot to support fd-based private memory > KVM: Use kvm_userspace_memory_region_ext > KVM: Add KVM_EXIT_MEMORY_ERROR exit > KVM: Handle page fault for private memory > KVM: Register private memslot to memory backing store > KVM: Zap existing KVM mappings when pages changed in the private fd > KVM: Enable and expose KVM_MEM_PRIVATE Thanks for the suggestion. That makes sense. Chao > > > if ((mem->userspace_addr != old->userspace_addr) || > > (npages != old->npages) || > > ((mem->flags ^ old->flags) & KVM_MEM_READONLY)) > > -- > > 2.17.1 > >