* [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper
@ 2026-11-19 8:35 Jiahao Xu
2026-11-19 8:35 ` [PATCH v2 2/2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Jiahao Xu @ 2026-11-19 8:35 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>
---
v2:
- Rename memfd_pin_folios_supported() to file_is_memfd_compatible(),
suggested by David Hildenbrand: the predicate asks about the file, not
about the memfd interface.
v1: https://lore.kernel.org/all/20261119104417.312237-1-xujiahao@cmss.chinamobile.com/
---
include/linux/mm.h | 1 +
mm/gup.c | 23 ++++++++++++++++++++++-
2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 040cff643..dffcc0b5a 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 file_is_memfd_compatible(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..da8a2c29b 100644
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -3449,6 +3449,27 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
}
EXPORT_SYMBOL(pin_user_pages_unlocked);
+/**
+ * file_is_memfd_compatible() - check if a file may be used with
+ * memfd_pin_folios()
+ * @file: the file to check
+ *
+ * memfd_pin_folios() only accepts files backed by shmem or by hugetlb.
+ * This is wider than "is a memfd": every shmem file matches, not only the
+ * unlinked ones memfd_create() produces, and file-hugetlb files are not
+ * memfds at all.
+ *
+ * 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 file_is_memfd_compatible(struct file *file)
+{
+ return shmem_file(file) || is_file_hugepages(file);
+}
+EXPORT_SYMBOL_GPL(file_is_memfd_compatible);
+
/**
* memfd_pin_folios() - pin folios associated with a memfd
* @memfd: the memfd whose folios are to be pinned
@@ -3493,7 +3514,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 (!file_is_memfd_compatible(memfd))
return -EINVAL;
if (end >= i_size_read(file_inode(memfd)))
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] iommufd: only hold references to files memfd_pin_folios() can pin
2026-11-19 8:35 [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper Jiahao Xu
@ 2026-11-19 8:35 ` Jiahao Xu
2026-11-20 20:04 ` Samiullah Khawaja
2026-11-20 9:31 ` [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper David Hildenbrand (Arm)
2026-11-20 20:03 ` Samiullah Khawaja
2 siblings, 1 reply; 5+ messages in thread
From: Jiahao Xu @ 2026-11-19 8:35 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
iopt_map_file_pages() stores a reference to the file passed to
IOMMU_IOAS_MAP_FILE in the iopt_pages of the new mapping:
iopt_map_file_pages()
iopt_alloc_file_pages()
pages->file = get_file(file)
iopt_release_pages() drops that reference only when the iopt_pages is
destroyed, which for a file mapping happens while the iommufd file is
being torn down. Nothing restricts which file can be passed, so
userspace can map the iommufd file itself, or a vfio device fd bound to
the same iommufd, both of which own an iommufd_ctx reference. The
reference graph then contains a cycle and the file, the iopt_pages, the
IOAS and the iommufd_ctx are all leaked; kmemleak reports it as
"memory leak in path_openat" with the holder taken through
iopt_map_file_pages().
Reverse the test instead of trying to enumerate the files that own an
iommufd_ctx: accept only the files that memfd_pin_folios() is able to
pin, which is exactly the set of files the mapping can be built from.
file_is_memfd_compatible() reports that set, so the same test can run
here before the reference is handed to iopt_pages. dma-bufs are
unaffected: they are taken by the dma_buf_get() branch above and never
reach the file branch, and iopt_pages holds the dma_buf there, not a
struct file.
Fixes: f4986a72d6e4 ("iommufd: Add IOMMU_IOAS_MAP_FILE")
Reported-by: syzbot+7666ed2c3d42196bf7e3@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=7666ed2c3d42196bf7e3
Signed-off-by: Jiahao Xu <xujiahao@cmss.chinamobile.com>
---
v2:
- Call an exported mm helper instead of shmem_file(): shmem_file()
became an out-of-line function in mm/shmem.c that is not exported, so
calling it from iommufd.ko failed at modpost with "symbol 'shmem_file'
undefined!". Reported by Jason Gunthorpe.
- Use file_is_memfd_compatible(), renamed from
memfd_pin_folios_supported() as suggested by David Hildenbrand.
v1: https://lore.kernel.org/all/20261119104417.459962-1-xujiahao@cmss.chinamobile.com/
---
drivers/iommu/iommufd/io_pagetable.c | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
index 5ac736cf5..c9aad275f 100644
--- a/drivers/iommu/iommufd/io_pagetable.c
+++ b/drivers/iommu/iommufd/io_pagetable.c
@@ -515,6 +515,18 @@ int iopt_map_file_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt,
if (!file)
return -EBADF;
+ /*
+ * iopt_pages keeps a reference to the file until the mapping is
+ * destroyed, so only accept the files memfd_pin_folios() is able
+ * to pin. Any other fd, in particular one that itself owns an
+ * iommufd_ctx (iommufd, vfio, ...), would put the file into a
+ * reference cycle with this iommufd and never be released.
+ */
+ if (!file_is_memfd_compatible(file)) {
+ fput(file);
+ return -EINVAL;
+ }
+
pages = iopt_alloc_file_pages(file, start_byte, start, length,
iommu_prot & IOMMU_WRITE);
fput(file);
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper
2026-11-19 8:35 [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper Jiahao Xu
2026-11-19 8:35 ` [PATCH v2 2/2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
@ 2026-11-20 9:31 ` David Hildenbrand (Arm)
2026-11-20 20:03 ` Samiullah Khawaja
2 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-11-20 9:31 UTC (permalink / raw)
To: Jiahao Xu, Andrew Morton, Jason Gunthorpe
Cc: linux-mm, John Hubbard, Peter Xu, Vivek Kasireddy, Kevin Tian,
Joerg Roedel (AMD), Will Deacon, Robin Murphy, iommu,
linux-kernel, Samiullah Khawaja, syzbot+7666ed2c3d42196bf7e3
On 11/19/26 09:35, Jiahao Xu wrote:
> 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>
> ---
> v2:
> - Rename memfd_pin_folios_supported() to file_is_memfd_compatible(),
> suggested by David Hildenbrand: the predicate asks about the file, not
> about the memfd interface.
> v1: https://lore.kernel.org/all/20261119104417.312237-1-xujiahao@cmss.chinamobile.com/
> ---
> include/linux/mm.h | 1 +
> mm/gup.c | 23 ++++++++++++++++++++++-
> 2 files changed, 23 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/mm.h b/include/linux/mm.h
> index 040cff643..dffcc0b5a 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 file_is_memfd_compatible(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..da8a2c29b 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -3449,6 +3449,27 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
> }
> EXPORT_SYMBOL(pin_user_pages_unlocked);
>
> +/**
> + * file_is_memfd_compatible() - check if a file may be used with
> + * memfd_pin_folios()
> + * @file: the file to check
> + *
> + * memfd_pin_folios() only accepts files backed by shmem or by hugetlb.
> + * This is wider than "is a memfd": every shmem file matches, not only the
> + * unlinked ones memfd_create() produces, and file-hugetlb files are not
> + * memfds at all.
> + *
> + * 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 file_is_memfd_compatible(struct file *file)
> +{
> + return shmem_file(file) || is_file_hugepages(file);
> +}
> +EXPORT_SYMBOL_GPL(file_is_memfd_compatible);
Work for me, but maybe others have better naming suggestions.
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper
2026-11-19 8:35 [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper Jiahao Xu
2026-11-19 8:35 ` [PATCH v2 2/2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
2026-11-20 9:31 ` [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper David Hildenbrand (Arm)
@ 2026-11-20 20:03 ` Samiullah Khawaja
2 siblings, 0 replies; 5+ messages in thread
From: Samiullah Khawaja @ 2026-11-20 20:03 UTC (permalink / raw)
To: Jiahao Xu
Cc: Andrew Morton, Jason Gunthorpe, linux-mm, David Hildenbrand,
John Hubbard, Peter Xu, Vivek Kasireddy, Kevin Tian,
Joerg Roedel (AMD), Will Deacon, Robin Murphy, iommu,
linux-kernel, syzbot+7666ed2c3d42196bf7e3
On Thu, Nov 19, 2026 at 04:35:31PM +0800, Jiahao Xu wrote:
>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>
>---
>v2:
> - Rename memfd_pin_folios_supported() to file_is_memfd_compatible(),
> suggested by David Hildenbrand: the predicate asks about the file, not
> about the memfd interface.
>v1: https://lore.kernel.org/all/20261119104417.312237-1-xujiahao@cmss.chinamobile.com/
>---
> include/linux/mm.h | 1 +
> mm/gup.c | 23 ++++++++++++++++++++++-
> 2 files changed, 23 insertions(+), 1 deletion(-)
>
>diff --git a/include/linux/mm.h b/include/linux/mm.h
>index 040cff643..dffcc0b5a 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 file_is_memfd_compatible(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..da8a2c29b 100644
>--- a/mm/gup.c
>+++ b/mm/gup.c
>@@ -3449,6 +3449,27 @@ long pin_user_pages_unlocked(unsigned long start, unsigned long nr_pages,
> }
> EXPORT_SYMBOL(pin_user_pages_unlocked);
>
>+/**
>+ * file_is_memfd_compatible() - check if a file may be used with
>+ * memfd_pin_folios()
>+ * @file: the file to check
>+ *
>+ * memfd_pin_folios() only accepts files backed by shmem or by hugetlb.
>+ * This is wider than "is a memfd": every shmem file matches, not only the
>+ * unlinked ones memfd_create() produces, and file-hugetlb files are not
>+ * memfds at all.
>+ *
>+ * 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 file_is_memfd_compatible(struct file *file)
>+{
>+ return shmem_file(file) || is_file_hugepages(file);
>+}
>+EXPORT_SYMBOL_GPL(file_is_memfd_compatible);
>+
> /**
> * memfd_pin_folios() - pin folios associated with a memfd
> * @memfd: the memfd whose folios are to be pinned
>@@ -3493,7 +3514,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 (!file_is_memfd_compatible(memfd))
> return -EINVAL;
>
> if (end >= i_size_read(file_inode(memfd)))
>--
>2.43.0
>
>
>
Reviewed-by: Samiullah Khawaja <skhawaja@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] iommufd: only hold references to files memfd_pin_folios() can pin
2026-11-19 8:35 ` [PATCH v2 2/2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
@ 2026-11-20 20:04 ` Samiullah Khawaja
0 siblings, 0 replies; 5+ messages in thread
From: Samiullah Khawaja @ 2026-11-20 20:04 UTC (permalink / raw)
To: Jiahao Xu
Cc: Andrew Morton, Jason Gunthorpe, linux-mm, David Hildenbrand,
John Hubbard, Peter Xu, Vivek Kasireddy, Kevin Tian,
Joerg Roedel (AMD), Will Deacon, Robin Murphy, iommu,
linux-kernel, syzbot+7666ed2c3d42196bf7e3
On Thu, Nov 19, 2026 at 04:35:31PM +0800, Jiahao Xu wrote:
>iopt_map_file_pages() stores a reference to the file passed to
>IOMMU_IOAS_MAP_FILE in the iopt_pages of the new mapping:
>
> iopt_map_file_pages()
> iopt_alloc_file_pages()
> pages->file = get_file(file)
>
>iopt_release_pages() drops that reference only when the iopt_pages is
>destroyed, which for a file mapping happens while the iommufd file is
>being torn down. Nothing restricts which file can be passed, so
>userspace can map the iommufd file itself, or a vfio device fd bound to
>the same iommufd, both of which own an iommufd_ctx reference. The
>reference graph then contains a cycle and the file, the iopt_pages, the
>IOAS and the iommufd_ctx are all leaked; kmemleak reports it as
>"memory leak in path_openat" with the holder taken through
>iopt_map_file_pages().
>
>Reverse the test instead of trying to enumerate the files that own an
>iommufd_ctx: accept only the files that memfd_pin_folios() is able to
>pin, which is exactly the set of files the mapping can be built from.
>file_is_memfd_compatible() reports that set, so the same test can run
>here before the reference is handed to iopt_pages. dma-bufs are
>unaffected: they are taken by the dma_buf_get() branch above and never
>reach the file branch, and iopt_pages holds the dma_buf there, not a
>struct file.
>
>Fixes: f4986a72d6e4 ("iommufd: Add IOMMU_IOAS_MAP_FILE")
>Reported-by: syzbot+7666ed2c3d42196bf7e3@syzkaller.appspotmail.com
>Closes: https://syzkaller.appspot.com/bug?extid=7666ed2c3d42196bf7e3
>Signed-off-by: Jiahao Xu <xujiahao@cmss.chinamobile.com>
>---
>v2:
> - Call an exported mm helper instead of shmem_file(): shmem_file()
> became an out-of-line function in mm/shmem.c that is not exported, so
> calling it from iommufd.ko failed at modpost with "symbol 'shmem_file'
> undefined!". Reported by Jason Gunthorpe.
> - Use file_is_memfd_compatible(), renamed from
> memfd_pin_folios_supported() as suggested by David Hildenbrand.
>v1: https://lore.kernel.org/all/20261119104417.459962-1-xujiahao@cmss.chinamobile.com/
>---
> drivers/iommu/iommufd/io_pagetable.c | 12 ++++++++++++
> 1 file changed, 12 insertions(+)
>
>diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/io_pagetable.c
>index 5ac736cf5..c9aad275f 100644
>--- a/drivers/iommu/iommufd/io_pagetable.c
>+++ b/drivers/iommu/iommufd/io_pagetable.c
>@@ -515,6 +515,18 @@ int iopt_map_file_pages(struct iommufd_ctx *ictx, struct io_pagetable *iopt,
> if (!file)
> return -EBADF;
>
>+ /*
>+ * iopt_pages keeps a reference to the file until the mapping is
>+ * destroyed, so only accept the files memfd_pin_folios() is able
>+ * to pin. Any other fd, in particular one that itself owns an
>+ * iommufd_ctx (iommufd, vfio, ...), would put the file into a
>+ * reference cycle with this iommufd and never be released.
>+ */
>+ if (!file_is_memfd_compatible(file)) {
>+ fput(file);
>+ return -EINVAL;
>+ }
>+
> pages = iopt_alloc_file_pages(file, start_byte, start, length,
> iommu_prot & IOMMU_WRITE);
> fput(file);
>--
>2.43.0
>
>
Reviewed-by: Samiullah Khawaja <skhawaja@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-20 20:04 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 8:35 [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper Jiahao Xu
2026-11-19 8:35 ` [PATCH v2 2/2] iommufd: only hold references to files memfd_pin_folios() can pin Jiahao Xu
2026-11-20 20:04 ` Samiullah Khawaja
2026-11-20 9:31 ` [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper David Hildenbrand (Arm)
2026-11-20 20:03 ` Samiullah Khawaja
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.