From nobody Tue Apr 7 14:25:06 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1B723431F5 for ; Fri, 13 Mar 2026 06:12:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382370; cv=none; b=vErJTZJzsXyV85niHS6+u8UgXLSJDggQJ8noiAZNOOiP9VSO8MlIXxyQpjVqDactAyml6lll3Sb3jjjrabvXAC+WIAyNrd08bpwsMHawT0UziJ/8x/XYDmF1u2ZCa7h6Jb/huW72T+gUt3G+iHVDf4qxRrDMuieO1UkRDY3u6Vo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773382370; c=relaxed/simple; bh=dCzSCpaG1ZfhwHuWBnl8WkjBivcedlrblANYJCmmXLw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rqkSa7KLJx+79BxFOgf4n95jq/vPP7fNrR3SarA9RNOzpXoWWUa5Li0xhTnV+z/GLarn4GDLctHctSzyAwBsWCXv74KK4ksCaIMsE7x3jR0s6IKebVduXA4M/dMmEtDV6E1RWD++Bw+LcTXfbdy64Y4lLuxY1YorB+EKQMdpULo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=T2yTn5Fn; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="T2yTn5Fn" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3594620fe97so12544709a91.1 for ; Thu, 12 Mar 2026 23:12:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773382368; x=1773987168; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=igAKGPZs3Yn8RFN95Dzr41maaofuMmxtQrDmAbA4Zlo=; b=T2yTn5Fnfx+GuBQeW1cXP4Fdm+PiHpmvAN6lS15FTgq0BdSbRyGNcmE+mtL6fsUXEJ lsIH1i8QUUAMZw/UGalcTVFKoxLH4Ee3BT3sWc8UFfwDdQgJj6t2BAQ+dyhdu5X/SPPh IWWrt2yLI54hEfoX/2ECTo8s3z+Oh8nnnlbNQyS/+bXlSPRu4sjhe7ZN/kcnNRq1JHVP ZRPTRZBNIAqM0eGvZIjg6yBAGr1Oc/LjQQ98nXo+txZcITS91iPnPzB58S0KAjvvGEJh NP+AhY4c98ZpmOs3aBvKzSmtuT4gSFPBvJ29n1riIOOadnmt4kcH6q4W/6Zg5Vj+SW5E LmKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773382368; x=1773987168; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=igAKGPZs3Yn8RFN95Dzr41maaofuMmxtQrDmAbA4Zlo=; b=O/cOKvtg4zMxMSwyuEObeln21WdTa2A+JEqtlLknqCGRqkCdaLYiRlIivFq4zokApk Fh8v8cXcL3bQT7nDytls3Jw7cE+Pw2KyfmAZEBSswKAx8SfQvI7joaqng9QasU6LlVdx n/zqE1blhjIN2e5GD+3FyMigM9+7tQnwO3vt07MZ6Jo5QJEDU/F0e6gNJf0M6zhUkleM MZER+hv8A4MvDUgXKKp8s/eAPXDUdhcfwzmJ2cVHecXEisIt0YwodDn52HsSsaFcvL2N 6WuOUW5lqYDyVy6yRbpv0SO0Gd5xoFWXyASMGYuVXxQw47GxztlAusTqlRbALlkPIHv2 TXZw== X-Forwarded-Encrypted: i=1; AJvYcCXQnHmLgJO5mAizKhRmOHvyVbqau0qy5m8mPIv4PDDQvt20Rp3k6hr/crN4FVYkyWoI1mz6zAXFN9KuJTU=@vger.kernel.org X-Gm-Message-State: AOJu0Yyzu+FFBWexQTwe6uXLNrVU3ZsPDOXPXeqntAeddM2RwqqXyo24 h7I1q4JAMXDRble9egJAMT/pWQEDHc6q2DsDL4XE0lsLVQoiUHdf4CXc9z9vROe+Nc+tWoE059y JFss58JUHeOl6891jQGkVieqC6Q== X-Received: from pjji1.prod.google.com ([2002:a17:90a:6501:b0:359:8332:2cce]) (user=ackerleytng job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3888:b0:359:1063:6b02 with SMTP id 98e67ed59e1d1-35a220033c7mr1965219a91.18.1773382368016; Thu, 12 Mar 2026 23:12:48 -0700 (PDT) Date: Fri, 13 Mar 2026 06:12:40 +0000 In-Reply-To: <20260313-gmem-inplace-conversion-v3-0-5fc12a70ec89@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260313-gmem-inplace-conversion-v3-0-5fc12a70ec89@google.com> X-Developer-Key: i=ackerleytng@google.com; a=ed25519; pk=sAZDYXdm6Iz8FHitpHeFlCMXwabodTm7p8/3/8xUxuU= X-Developer-Signature: v=1; a=ed25519-sha256; t=1773382364; l=8193; i=ackerleytng@google.com; s=20260225; h=from:subject:message-id; bh=SgPEW/88BT04ZTg7kf0aC5QiYWJ/VH6PQfUxsjhUyo0=; b=TaRTC8HrKfJiiBHiOlhfyqje1q2nDglb7gTbUculap4XLUDkb9lQ2qWVkTnyUcj2naqJDRcvA lRL7RVAAR79BMgAnd/9m8Ly9Kt2/RbE7p8Ba8AmO5LH6v/D2qPNSIgp X-Mailer: b4 0.14.3 Message-ID: <20260313-gmem-inplace-conversion-v3-1-5fc12a70ec89@google.com> Subject: [PATCH RFC v3 01/43] KVM: guest_memfd: Introduce per-gmem attributes, use to guard user mappings From: Ackerley Tng To: aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, ira.weiny@intel.com, jmattson@google.com, jroedel@suse.de, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, tabba@google.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, Paolo Bonzini , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Jason Gunthorpe , Vlastimil Babka Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Ackerley Tng Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Sean Christopherson Start plumbing in guest_memfd support for in-place private<=3D>shared conversions by tracking attributes via a maple tree. KVM currently tracks private vs. shared attributes on a per-VM basis, which made sense when a guest_memfd _only_ supported private memory, but tracking per-VM simply can't work for in-place conversions as the shareability of a given page needs to be per-gmem_inode, not per-VM. Use the filemap invalidation lock to protect the maple tree, as taking the lock for read when faulting in memory (for userspace or the guest) isn't expected to result in meaningful contention, and using a separate lock would add significant complexity (avoid deadlock is quite difficult). Signed-off-by: Sean Christopherson Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Vishal Annapurve Signed-off-by: Vishal Annapurve Co-developed-by: Fuad Tabba Signed-off-by: Fuad Tabba --- virt/kvm/guest_memfd.c | 134 +++++++++++++++++++++++++++++++++++++++++++--= ---- 1 file changed, 118 insertions(+), 16 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 017d84a7adf37..67709af03f39d 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,7 @@ struct gmem_inode { struct inode vfs_inode; =20 u64 flags; + struct maple_tree attributes; }; =20 static __always_inline struct gmem_inode *GMEM_I(struct inode *inode) @@ -59,6 +61,31 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot= *slot, gfn_t gfn) return gfn - slot->base_gfn + slot->gmem.pgoff; } =20 +static u64 kvm_gmem_get_attributes(struct inode *inode, pgoff_t index) +{ + struct maple_tree *mt =3D &GMEM_I(inode)->attributes; + void *entry =3D mtree_load(mt, index); + + /* + * The lock _must_ be held for lookups, as some maple tree operations, + * e.g. append, are unsafe (return inaccurate information) with respect + * to concurrent RCU-protected lookups. + */ + lockdep_assert(mt_lock_is_held(mt)); + + return WARN_ON_ONCE(!entry) ? 0 : xa_to_value(entry); +} + +static bool kvm_gmem_is_private_mem(struct inode *inode, pgoff_t index) +{ + return kvm_gmem_get_attributes(inode, index) & KVM_MEMORY_ATTRIBUTE_PRIVA= TE; +} + +static bool kvm_gmem_is_shared_mem(struct inode *inode, pgoff_t index) +{ + return !kvm_gmem_is_private_mem(inode, index); +} + static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slo= t *slot, pgoff_t index, struct folio *folio) { @@ -397,10 +424,13 @@ static vm_fault_t kvm_gmem_fault_user_mapping(struct = vm_fault *vmf) if (((loff_t)vmf->pgoff << PAGE_SHIFT) >=3D i_size_read(inode)) return VM_FAULT_SIGBUS; =20 - if (!(GMEM_I(inode)->flags & GUEST_MEMFD_FLAG_INIT_SHARED)) - return VM_FAULT_SIGBUS; + filemap_invalidate_lock_shared(inode->i_mapping); + if (kvm_gmem_is_shared_mem(inode, vmf->pgoff)) + folio =3D kvm_gmem_get_folio(inode, vmf->pgoff); + else + folio =3D ERR_PTR(-EACCES); + filemap_invalidate_unlock_shared(inode->i_mapping); =20 - folio =3D kvm_gmem_get_folio(inode, vmf->pgoff); if (IS_ERR(folio)) { if (PTR_ERR(folio) =3D=3D -EAGAIN) return VM_FAULT_RETRY; @@ -556,6 +586,51 @@ bool __weak kvm_arch_supports_gmem_init_shared(struct = kvm *kvm) return true; } =20 +static int kvm_gmem_init_inode(struct inode *inode, loff_t size, u64 flags) +{ + struct gmem_inode *gi =3D GMEM_I(inode); + MA_STATE(mas, &gi->attributes, 0, (size >> PAGE_SHIFT) - 1); + u64 attrs; + int r; + + inode->i_op =3D &kvm_gmem_iops; + inode->i_mapping->a_ops =3D &kvm_gmem_aops; + inode->i_mode |=3D S_IFREG; + inode->i_size =3D size; + mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); + + /* + * guest_memfd memory is neither migratable nor swappable: set + * inaccessible to gate off both. + */ + mapping_set_inaccessible(inode->i_mapping); + WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); + + gi->flags =3D flags; + + mt_set_external_lock(&gi->attributes, + &inode->i_mapping->invalidate_lock); + + /* + * Store default attributes for the entire gmem instance. Ensuring every + * index is represented in the maple tree at all times simplifies the + * conversion and merging logic. + */ + attrs =3D gi->flags & GUEST_MEMFD_FLAG_INIT_SHARED ? 0 : KVM_MEMORY_ATTRI= BUTE_PRIVATE; + + /* + * Acquire the invalidation lock purely to make lockdep happy. The + * maple tree library expects all stores to be protected via the lock, + * and the library can't know when the tree is reachable only by the + * caller, as is the case here. + */ + filemap_invalidate_lock(inode->i_mapping); + r =3D mas_store_gfp(&mas, xa_mk_value(attrs), GFP_KERNEL); + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) { static const char *name =3D "[kvm-gmem]"; @@ -586,16 +661,9 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t s= ize, u64 flags) goto err_fops; } =20 - inode->i_op =3D &kvm_gmem_iops; - inode->i_mapping->a_ops =3D &kvm_gmem_aops; - inode->i_mode |=3D S_IFREG; - inode->i_size =3D size; - mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); - mapping_set_inaccessible(inode->i_mapping); - /* Unmovable mappings are supposed to be marked unevictable as well. */ - WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); - - GMEM_I(inode)->flags =3D flags; + err =3D kvm_gmem_init_inode(inode, size, flags); + if (err) + goto err_inode; =20 file =3D alloc_file_pseudo(inode, kvm_gmem_mnt, name, O_RDWR, &kvm_gmem_f= ops); if (IS_ERR(file)) { @@ -797,9 +865,13 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memor= y_slot *slot, if (!file) return -EFAULT; =20 + filemap_invalidate_lock_shared(file_inode(file)->i_mapping); + folio =3D __kvm_gmem_get_pfn(file, slot, index, pfn, max_order); - if (IS_ERR(folio)) - return PTR_ERR(folio); + if (IS_ERR(folio)) { + r =3D PTR_ERR(folio); + goto out; + } =20 if (!folio_test_uptodate(folio)) { clear_highpage(folio_page(folio, 0)); @@ -815,6 +887,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory= _slot *slot, else folio_put(folio); =20 +out: + filemap_invalidate_unlock_shared(file_inode(file)->i_mapping); return r; } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_gmem_get_pfn); @@ -944,13 +1018,41 @@ static struct inode *kvm_gmem_alloc_inode(struct sup= er_block *sb) =20 mpol_shared_policy_init(&gi->policy, NULL); =20 + /* + * Memory attributes are protected by the filemap invalidation lock, but + * the lock structure isn't available at this time. Immediately mark + * maple tree as using external locking so that accessing the tree + * before it's fully initialized results in NULL pointer dereferences + * and not more subtle bugs. + */ + mt_init_flags(&gi->attributes, MT_FLAGS_LOCK_EXTERN); + gi->flags =3D 0; return &gi->vfs_inode; } =20 static void kvm_gmem_destroy_inode(struct inode *inode) { - mpol_free_shared_policy(&GMEM_I(inode)->policy); + struct gmem_inode *gi =3D GMEM_I(inode); + + mpol_free_shared_policy(&gi->policy); + + /* + * Note! Checking for an empty tree is functionally necessary + * to avoid explosions if the tree hasn't been fully + * initialized, i.e. if the inode is being destroyed before + * guest_memfd can set the external lock, lockdep would find + * that the tree's internal ma_lock was not held. + */ + if (!mtree_empty(&gi->attributes)) { + /* + * Acquire the invalidation lock purely to make lockdep happy, + * the inode is unreachable at this point. + */ + filemap_invalidate_lock(inode->i_mapping); + __mt_destroy(&gi->attributes); + filemap_invalidate_unlock(inode->i_mapping); + } } =20 static void kvm_gmem_free_inode(struct inode *inode) --=20 2.53.0.851.ga537e3e6e9-goog