Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Jiahao Xu <xujiahao@cmss.chinamobile.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jason Gunthorpe <jgg@ziepe.ca>
Cc: linux-mm@kvack.org, John Hubbard <jhubbard@nvidia.com>,
	Peter Xu <peterx@redhat.com>,
	Vivek Kasireddy <vivek.kasireddy@intel.com>,
	Kevin Tian <kevin.tian@intel.com>,
	"Joerg Roedel (AMD)" <joro@8bytes.org>,
	Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	iommu@lists.linux.dev, linux-kernel@vger.kernel.org,
	Samiullah Khawaja <skhawaja@google.com>,
	syzbot+7666ed2c3d42196bf7e3@syzkaller.appspotmail.com
Subject: Re: [PATCH v2 1/2] mm/gup: add file_is_memfd_compatible() helper
Date: Fri, 20 Nov 2026 10:31:13 +0100	[thread overview]
Message-ID: <3f23e91b-7616-418f-8f31-c7af667156ee@kernel.org> (raw)
In-Reply-To: <20261119163531.788514-1-xujiahao@cmss.chinamobile.com>

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


      parent reply	other threads:[~2026-11-20  9:31 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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) [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3f23e91b-7616-418f-8f31-c7af667156ee@kernel.org \
    --to=david@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=jhubbard@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=peterx@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=skhawaja@google.com \
    --cc=syzbot+7666ed2c3d42196bf7e3@syzkaller.appspotmail.com \
    --cc=vivek.kasireddy@intel.com \
    --cc=will@kernel.org \
    --cc=xujiahao@cmss.chinamobile.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox