mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] mm/gup: add memfd_pin_folios_supported() helper
@ 2026-11-19  2:44 Jiahao Xu
  2026-11-19  2:44 ` [PATCH 2/2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
  2026-11-19  7:48 ` [PATCH 1/2] mm/gup: add memfd_pin_folios_supported() helper David Hildenbrand (Arm)
  0 siblings, 2 replies; 4+ messages in thread
From: Jiahao Xu @ 2026-11-19  2:44 UTC (permalink / raw)
  To: Andrew Morton, Jason Gunthorpe
  Cc: linux-mm, David Hildenbrand, John Hubbard, Peter Xu,
	Vivek Kasireddy, Kevin Tian, Joerg Roedel (AMD),
	Will Deacon, Robin Murphy, iommu, linux-kernel,
	Samiullah Khawaja, syzbot+7666ed2c3d42196bf7e3, Jiahao Xu

memfd_pin_folios() accepts only shmem and file-hugetlb files and enforces
that inline with shmem_file() and is_file_hugepages().  A caller that has
to know up front whether a file may be handed to memfd_pin_folios()
cannot use those predicates: since d3710111839d ("mm/shmem: make
shmem_file() check the file's inode") shmem_file() is an out-of-line
function in mm/shmem.c which is not exported, so modules cannot call it,
and is_file_hugepages() on its own is not the same question.

Provide the check as an exported helper next to memfd_pin_folios() and
use it inside memfd_pin_folios() itself, so that a single place decides
which files may be pinned.

Signed-off-by: Jiahao Xu <xujiahao@cmss.chinamobile.com>
---
 include/linux/mm.h |  1 +
 mm/gup.c           | 18 +++++++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff643..e401edc93 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -3600,6 +3600,7 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
 long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
 		      struct folio **folios, unsigned int max_folios,
 		      pgoff_t *offset);
+bool memfd_pin_folios_supported(struct file *file);
 int folio_add_pins(struct folio *folio, unsigned int pins);
 
 int get_user_pages_fast(unsigned long start, int nr_pages,
diff --git a/mm/gup.c b/mm/gup.c
index 7605da0e0..90ac51207 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -3449,6 +3449,22 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
 }
 EXPORT_SYMBOL(pin_user_pages_unlocked);
 
+/**
+ * memfd_pin_folios_supported() - check if memfd_pin_folios() can pin a file
+ * @file: the file to check
+ *
+ * memfd_pin_folios() only accepts files backed by shmem or by hugetlb, so
+ * callers that have to validate a file before pinning it, or before taking a
+ * reference to it, can use this helper instead of duplicating the check.
+ *
+ * Return: true if @file may be passed to memfd_pin_folios().
+ */
+bool memfd_pin_folios_supported(struct file *file)
+{
+	return shmem_file(file) || is_file_hugepages(file);
+}
+EXPORT_SYMBOL_GPL(memfd_pin_folios_supported);
+
 /**
  * memfd_pin_folios() - pin folios associated with a memfd
  * @memfd:      the memfd whose folios are to be pinned
@@ -3493,7 +3509,7 @@ long memfd_pin_folios(struct file *memfd, loff_t start, loff_t end,
 	if (!memfd)
 		return -EINVAL;
 
-	if (!shmem_file(memfd) && !is_file_hugepages(memfd))
+	if (!memfd_pin_folios_supported(memfd))
 		return -EINVAL;
 
 	if (end >= i_size_read(file_inode(memfd)))
-- 
2.43.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-19  8:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19  2:44 [PATCH 1/2] mm/gup: add memfd_pin_folios_supported() helper Jiahao Xu
2026-11-19  2:44 ` [PATCH 2/2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
2026-11-19  7:48 ` [PATCH 1/2] mm/gup: add memfd_pin_folios_supported() helper David Hildenbrand (Arm)
2026-11-19  8:35   ` Jiahao Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®