From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793116024; b=BxVmZVszeCXmtXNFW97trKRUZn1L9yvX4OTk9m4UJkrDgvWDWG0+2O7L3WvqZNLk+wxF a75SDTX/H+dU4uma11VS4auDWmgRMdkRQEYAst4b6Ti8b+U6T8F5R4G7nknGUcU+M031l pK6qwlOCZ1sxXc5yBwp7P1Th6HlVL4l63oLfNWAowD93jfAMAJsmCTKrMakN+FDn29vQ2 tejEgw7y+cKwXYuHfPvUwf+C/Ju6we6uIMJCxMnVszqwrGUomR/qNnCCbhNx9LL/nbiDR jGISRbAnhpvmymDUW7p2dCfl0qIEDwUZBuAN2rQExQkRXTwHux8Bsp6h876Fh3Kg4Ww== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793116024; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=c+t2RCt7p+zsicFUGs0SiXsBnjjHZ+TU2eQo8OoRVfQ=; b=Rb4TExs6GU+ITvMYqJFkRKEQlGIi3L3wAFpBQdy98UY3jI0eJ5O0XY1ziLP6oWo6ZBz0 85u7Gqx3XVX84bKnoJbbx3u1Cqpn19IIHwHVSxgafalkLv9fVv7RqkaFyDZtRl7uq9QYh eecuBpGeEpYrvaHcDEzxAbfu5uHuPOENSJmScPjgQRy8WhbmNxZ0ewdyX+17EYaU0VK/c yUnWpxSmVRwWxMB4xJECTsqnIV9pLoHZM0wpMaHNbQfkf46542XehERs9M1dUkSmVSGQy e++duisweoo4QqL4TRWI3FZFvqw7egPQHlFsajHZHzRob7bCJQ0pPL6PP+phFcr3TUg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kYgbAXpo; arc=none smtp.remote-ip=209.85.221.54 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CF6F1606AB48 for ; Tue, 27 Oct 2026 15:47:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CF6F1606AB48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=kYgbAXpo Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-48e20edd106so3710672f8f.1 for ; Tue, 27 Oct 2026 08:47:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793116022; x=1793720822; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=c+t2RCt7p+zsicFUGs0SiXsBnjjHZ+TU2eQo8OoRVfQ=; b=kYgbAXpozbCUdGlBS7i82c+HK8MQPHZLP6JTZwYIufdiodAlugvi+KaM8RIqPD2Sig R596RyoB8hOV1ttoXUMF6NhtygxqOn73iCeQM06LQSR0RBbsWmQPpSoxWdeuJ9vlxkGZ M7DHCeMXwNtebzXa2LsBwTU9y/3WCEu45ucigVcSwIjSXbQ064ClRw/arY3H5eECzHU2 VJ7K/msDM71SNeqzjXofQeXV1Csvz8zpPoj7D49oii1p7ggUYjXsBFQUNllFRtAEPiDr E+8gtTFLGCBodk7j0bWSpqLCL34CwW6iGhS8jvu6lryOKJUKTxdlevPp5eIRazHVG1hc jkvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793116022; x=1793720822; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=c+t2RCt7p+zsicFUGs0SiXsBnjjHZ+TU2eQo8OoRVfQ=; b=LJQPKZjmR9ujkQDGc88PkGZ/MgdYfitlSFjEJn65W5TabmQleCD0fl79ZbwBF6kI2f 6PJQEcUyybv4yJFS9sF3xRdYUPsLXX4zaWrAVwbKmQzKR//Th2518gUhSi5eGB7x1Vnn ts4+sfnDHEoBsO4i2f5RQ5hVu9k/rgXjp2kTnRGV5pM4mPrdEJwfkOo/29o3vNDsqxMO R4IhPMjcqk5viz3BhMp6dAthVv+lfVKqd+UMdwNU9JIQnFI9byvA725394yOTIodPmFq iNHp0mRjC8vH14Q0U/NKSVSxrCXvaqMMu3td0D2AZqCzBOSys0JVvC5OnbIsTuGpb3oD HXmg== X-Forwarded-Encrypted: i=1; AKwUvBx2ziaaU7d99zMqHyQwZqGNx9QLupT+ElCJHjB+N807j+vUYkC7M9OuD7guz1lVPXaJezc7O5zhMEDOC/Q=@vger.kernel.org X-Gm-Message-State: AFq9FYJEkffsP6qw7W6YAMPEBELTd57QKhhfQ4iQhqY1FOF7BO2oJpiO q6wREw+3CzwLur3WiIGR6hHr6X9Xt9rJjJ6DCfVUuJfanrltOvY6fUnj X-Gm-Gg: AYBFou0nmW+AJ0rb4Uk7kj8Yh31woQC9KT41gv8QtC/JRxwbmCqGm/AFhlyTJSkocPL rqazerWhHUaQ+CIm53bxmY/Le29xeKsQyBT5IKDiprzwFJhMEbnSEuSO50vk/GS2xQz7/JTjvOV NSoH1M24nSTWVzirVKa8g1ZtpKexU8GsPrhx9A5RQymFAc27onJhwBBxAun578xXnzOQqb8dujz Xmh3ghE6xSuTK226DuVc9ha6FV3k6eiF6HGxxQA/XmrbR3bXN3v51VdyXje6BnuOj/awPZoT/9O 7IU6qRIpRMmr7Fy5WsnVjuoO020NniLFUgCJv1mIifOfgYmVf3H0Y1cItfH8i0D8RvomNpO+7Gf yUedAyfmKOAMnwV8q+HLnmpR096Jc4UrBK/GCW1P2R//GSmX8zbwdzgpAurp0BsaLyh7TMr/Bep wbWmToSRxihDoa5mbJ5uzwYq28zCVF8uxpAtruTA37ZmFqZK7PvJ97PLxgZvavCzUt7/j1Khx+7 z42fg0ELmd5+0z4kW5xYKq5tfjiDM04Pb5Zoy1QiL0m3VtZdEIB0tjg9a1Q9pvubu+PaM7+/Jqx N2L5dsgZo0MXw2m8wiW2lxHrbfA/wKfeMkFYiNEiZDGHyBgJAMAEJGRJlTu3TU6TYEQbeVrfSui ysznHENIjUOkK9yUkw50UyMMB0W596wHzjSu2Fw== X-Received: by 2002:a05:6000:469c:b0:488:5d44:6680 with SMTP id ffacd0b85a97d-48e3eb9161fmr12046915f8f.36.1793116022012; Tue, 27 Oct 2026 08:47:02 -0700 (PDT) Received: from dev-dsk-fgriffo-1c-93421965.eu-west-1.amazon.com (54-240-197-234.amazon.com. [54.240.197.234]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e3ebcb958sm23845574f8f.21.2026.10.27.08.46.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 27 Oct 2026 08:47:01 -0700 (PDT) From: Fred Griffoul To: Ackerley Tng , Paolo Bonzini , Sean Christopherson , Jason Gunthorpe , Kevin Tian Cc: Alex Williamson , Alexander Graf , Alexander Viro , Alexey Kardashevskiy , Andrew Morton , "Aneesh Kumar K . V" , Ashish Kalra , Baolin Wang , Baolu Lu , Bill Wendling , Borislav Petkov , Catalin Marinas , Christian Brauner , =?UTF-8?q?Christian=20K=C3=B6nig?= , Connor Williamson , Dave Hansen , Dave Jiang , David Hildenbrand , David Rientjes , David Woodhouse , Erdem Aktas , Frank van der Linden , Gregory Price , "H . Peter Anvin" , Hugh Dickins , Ingo Molnar , Jacob Pan , James Houghton , Jan Kara , Jianxiong Gao , Joerg Roedel , Joey Gouly , John Starks , Jonathan Corbet , Justin Stitt , Lorenzo Stoakes , Marc Zyngier , Michael Roth , Mike Rapoport , Nathan Chancellor , Nick Desaulniers , Nicolin Chen , Oliver Upton , Pratyush Yadav , Randy Dunlap , Robin Murphy , Samiullah Khawaja , Shuah Khan , Steffen Eiden , Steven Price , Suzuki K Poulose , Tarun Sahu , Thomas Gleixner , Tom Lendacky , Tycho Andersen , Vishal Annapurve , Vlastimil Babka , Will Deacon , Xu Yilun , Yan Zhao , Zenghui Yu , x86@kernel.org, Shuah Khan , Fuad Tabba , kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, llvm@lists.linux.dev, iommu@lists.linux.dev Subject: [RFC PATCH 04/14] KVM: guest_memfd: Keep truncated folios until the exports are updated Date: Tue, 27 Oct 2026 15:46:39 +0000 Message-ID: <20261027154649.91571-5-griffoul@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261027154649.91571-1-griffoul@gmail.com> References: <20261022-gmem-alternative-providers-v2-0-5dd1dc6c660a@google.com> <20261027154649.91571-1-griffoul@gmail.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Fred Griffoul A punch hole frees the folios it truncates at once, but an export may still map them, since it holds no reference: a device would then write into freed memory. Truncate a folio mode range batch by batch, holding a reference on each folio until the exports are updated, so that a folio goes back to the allocator only once no device maps it. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Fred Griffoul --- include/linux/guest_memfd.h | 28 ++++++++++++++-------------- virt/kvm/guest_memfd.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h index fdf7f8404d6c..ba3eebb1ed1a 100644 --- a/include/linux/guest_memfd.h +++ b/include/linux/guest_memfd.h @@ -89,14 +89,14 @@ static inline int kvm_gmem_revoke(struct inode *inode, pgoff_t start, size_t nr_ * A subsystem that maps guest_memfd pages, such as iommufd, can neither pin * nor fault them. It attaches to the file, looks up the frame behind each * shared page without taking a reference, and guest_memfd calls its update() - * whenever what a lookup returns for a range may have changed: conversions and - * memory failure. update() makes the consumer's mappings of the range match - * what a lookup returns now: a frame, or a hole for a private page or a page - * without a frame. It may be called for pages that did not change, and for - * ranges that do not follow the blocks that lookups report: the consumer may - * then remap whole blocks, also outside the range, e.g. to merge them into - * larger mappings. A consumer that can fault may instead only remove its - * mappings of the range. + * whenever what a lookup returns for a range may have changed: conversions, + * punch holes and memory failure. update() makes the consumer's mappings of + * the range match what a lookup returns now: a frame, or a hole for a private + * page or a page without a frame. It may be called for pages that did not + * change, and for ranges that do not follow the blocks that lookups report: + * the consumer may then remap whole blocks, also outside the range, e.g. to + * merge them into larger mappings. A consumer that can fault may instead only + * remove its mappings of the range. * * Until update() returns, the frames it replaces stay valid, so a consumer * may switch each mapping to its new target in one step; once it returns, @@ -104,12 +104,12 @@ static inline int kvm_gmem_revoke(struct inode *inode, pgoff_t start, size_t nr_ * that needs memory to apply one, e.g. to split a large mapping, reserves it * beforehand. * - * guest_memfd calls update() with its invalidate lock held, exclusive when - * pages convert and shared on memory failure, possibly with a folio of the - * file locked; so update() may only call kvm_gmem_export_lookup_pfn(). Under - * the shared lock, updates may run at the same time, also for overlapping - * ranges. The consumer holds a lock across a lookup and the mapping it - * installs, which update() also takes. Lock order: + * guest_memfd calls update() with its invalidate lock held, exclusive when it + * takes frames away or pages convert and shared on memory failure, possibly + * with a folio of the file locked; so update() may only call + * kvm_gmem_export_lookup_pfn(). Under the shared lock, updates may run at the + * same time, also for overlapping ranges. The consumer holds a lock across a + * lookup and the mapping it installs, which update() also takes. Lock order: * * guest_memfd invalidate lock * consumer lock diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 81031483a1f1..65a951fa49c2 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -424,7 +424,7 @@ static void kvm_gmem_invalidate_end(struct inode *inode, pgoff_t start, /* * What a lookup returns for [start, end) may have changed: exclusive lock when - * pages converted, shared on memory failure. + * frames were taken away or pages converted, at least shared on memory failure. */ static void kvm_gmem_update_exports(struct inode *inode, pgoff_t start, pgoff_t end) @@ -437,6 +437,32 @@ static void kvm_gmem_update_exports(struct inode *inode, pgoff_t start, e->ops->update(e, start, end - start); } +/* + * Truncate [start, end) of a folio mode file. The exports map the folios + * without references, so hold the folios until the exports are updated. + */ +static void kvm_gmem_truncate(struct inode *inode, pgoff_t start, pgoff_t end) +{ + struct folio_batch fbatch; + pgoff_t next = start; + unsigned int i; + + folio_batch_init(&fbatch); + while (filemap_get_folios(inode->i_mapping, &next, end - 1, &fbatch)) { + pgoff_t first = max(fbatch.folios[0]->index, start); + pgoff_t last = min(next, end); + + truncate_inode_pages_range(inode->i_mapping, + (loff_t)first << PAGE_SHIFT, + ((loff_t)last << PAGE_SHIFT) - 1); + kvm_gmem_update_exports(inode, first, last); + for (i = 0; i < folio_batch_count(&fbatch); i++) + folio_put(fbatch.folios[i]); + folio_batch_reinit(&fbatch); + cond_resched(); + } +} + #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_RECLAIM static void kvm_gmem_pfn_reclaim(struct gmem_inode *gi, pgoff_t start, pgoff_t end) { @@ -499,7 +525,7 @@ static int __kvm_gmem_revoke(struct inode *inode, pgoff_t start, if (put_pfn_range) gmem_provider_put_pfn_range(GMEM_I(inode), start, nr_pages); } else { - truncate_inode_pages_range(inode->i_mapping, offset, offset + len - 1); + kvm_gmem_truncate(inode, start, end); } kvm_gmem_invalidate_end(inode, start, end); -- 2.47.3