Linux IOMMU Development
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Steve Sistare <steven.sistare@oracle.com>
Cc: iommu@lists.linux.dev, Kevin Tian <kevin.tian@intel.com>,
	Nicolin Chen <nicolinc@nvidia.com>
Subject: Re: [PATCH V1 4/9] iommufd: pfn reader for file mappings
Date: Sun, 15 Sep 2024 17:51:07 -0300	[thread overview]
Message-ID: <ZudIu+nvmY1N0CKR@nvidia.com> (raw)
In-Reply-To: <1726319158-283074-5-git-send-email-steven.sistare@oracle.com>

On Sat, Sep 14, 2024 at 06:05:53AM -0700, Steve Sistare wrote:
> Extend pfn_reader_user to pin file mappings, by calling memfd_pin_folios.
> Repin at small page granularity and unpack pages into upages[] to mesh
> with the existing code paths.
> 
> Signed-off-by: Steve Sistare <steven.sistare@oracle.com>
> ---
>  drivers/iommu/iommufd/io_pagetable.h |  5 ++++
>  drivers/iommu/iommufd/pages.c        | 58 ++++++++++++++++++++++++++++++++----
>  2 files changed, 57 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/iommu/iommufd/io_pagetable.h b/drivers/iommu/iommufd/io_pagetable.h
> index 7c4a338..3a28f46 100644
> --- a/drivers/iommu/iommufd/io_pagetable.h
> +++ b/drivers/iommu/iommufd/io_pagetable.h
> @@ -177,6 +177,7 @@ enum {
>  
>  enum iopt_address_type {
>  	IOPT_ADDRESS_USER = 0,
> +	IOPT_ADDRESS_FILE = 1,
>  };
>  
>  /*
> @@ -202,6 +203,10 @@ struct iopt_pages {
>  	enum iopt_address_type type;
>  	union {
>  		void __user *uptr;		/* IOPT_ADDRESS_USER */
> +		struct {			/* IOPT_ADDRESS_FILE */
> +			struct file *file;
> +			unsigned long start;
> +		};
>  	};
>  	bool writable:1;
>  	u8 account_mode;
> diff --git a/drivers/iommu/iommufd/pages.c b/drivers/iommu/iommufd/pages.c
> index 69822d4..065c28f 100644
> --- a/drivers/iommu/iommufd/pages.c
> +++ b/drivers/iommu/iommufd/pages.c
> @@ -695,6 +695,7 @@ static unsigned long batch_rw(struct pfn_batch *batch, void *data,
>  struct pfn_reader_user {
>  	struct page **upages;
>  	size_t upages_len;
> +	struct file *file;
>  	unsigned long upages_start;
>  	unsigned long upages_end;
>  	unsigned int gup_flags;
> @@ -712,6 +713,7 @@ static void pfn_reader_user_init(struct pfn_reader_user *user,
>  	user->upages_start = 0;
>  	user->upages_end = 0;
>  	user->locked = -1;
> +	user->file = (pages->type == IOPT_ADDRESS_FILE) ? pages->file : NULL;
>  
>  	user->gup_flags = FOLL_LONGTERM;
>  	if (pages->writable)
> @@ -733,13 +735,54 @@ static void pfn_reader_user_destroy(struct pfn_reader_user *user,
>  	user->upages = NULL;
>  }
>  
> +static long pin_memfd_pages(struct pfn_reader_user *user,
> +			    unsigned long start,
> +			    unsigned long npages)
> +{
> +	unsigned long end, nr, i, j, k, npin, nfolios, pgoff, max_folios;
> +	unsigned long npages_orig = npages;
> +	struct folio *folio;
> +	size_t size = npages * sizeof(folio);
> +	struct folio **folios = temp_kmalloc(&size, NULL, 0);
> +
> +	if (!folios)
> +		return -ENOMEM;
> +
> +	k = 0;
> +	max_folios = size / sizeof(folio);
> +	end = start + (npages << PAGE_SHIFT) - 1;
> +
> +	while (npages > 0) {
> +		nfolios = memfd_pin_folios(user->file, start, end,
> +					   folios, max_folios, &pgoff);
> +		if (nfolios <= 0)
> +			return nfolios;
> +
> +		pgoff >>= PAGE_SHIFT;
> +		for (i = 0; i < nfolios; i++) {
> +			folio = folios[i];
> +			nr = folio_nr_pages(folio);
> +			npin = min(nr - pgoff, npages);
> +			repin_folio_unhugely(folio, npin);
> +			for (j = pgoff; j < pgoff + npin; j++)
> +				user->upages[k++] = folio_page(folio, j);
> +			npages -= npin;
> +			start += npin << PAGE_SHIFT;
> +			pgoff = 0;

You should try harder to avoid this loop, the batch already can just
swallow a full folio, so this would be much better to copy more of the
code from it's caller and be more fully stand alone. Use only a temp
folio array and stick that directly into the batch full folio at a
time.

This above will be functionally the same, but alot slower..

Jason

  reply	other threads:[~2024-09-15 20:51 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-14 13:05 [PATCH V1 0/9] iommu_ioas_map_file Steve Sistare
2024-09-14 13:05 ` [PATCH V1 1/9] mm/gup: repin_folio_unhugely Steve Sistare
2024-09-14 13:19   ` Steven Sistare
2024-09-17 12:25     ` David Hildenbrand
2024-09-18 14:51       ` Steven Sistare
2024-09-19  8:11         ` David Hildenbrand
2024-09-19 21:06           ` Steven Sistare
2024-09-26 11:38             ` David Hildenbrand
2024-09-20 13:28           ` Jason Gunthorpe
2024-09-26 11:32             ` David Hildenbrand
2024-09-26 11:40               ` Jason Gunthorpe
2024-09-26 12:57                 ` David Hildenbrand
2024-09-26 12:58                   ` David Hildenbrand
2024-09-15 20:37   ` Jason Gunthorpe
2024-09-18 14:51     ` Steven Sistare
2024-09-14 13:05 ` [PATCH V1 2/9] iommufd: remove uptr from iopt_alloc_iova Steve Sistare
2024-09-15 20:41   ` Jason Gunthorpe
2024-09-18 14:51     ` Steven Sistare
2024-09-24 19:50       ` Jason Gunthorpe
2024-09-14 13:05 ` [PATCH V1 3/9] iommufd: generalize iopt_pages address Steve Sistare
2024-09-18 14:52   ` Steven Sistare
2024-09-14 13:05 ` [PATCH V1 4/9] iommufd: pfn reader for file mappings Steve Sistare
2024-09-15 20:51   ` Jason Gunthorpe [this message]
2024-09-18 14:51     ` Steven Sistare
2024-09-14 13:05 ` [PATCH V1 5/9] iommufd: IOMMU_IOAS_MAP_FILE interface Steve Sistare
2024-09-15 20:52   ` Jason Gunthorpe
2024-09-18 14:51     ` Steven Sistare
2024-09-14 13:05 ` [PATCH V1 6/9] iommufd: IOMMU_IOAS_MAP_FILE implementation Steve Sistare
2024-09-17  1:48   ` kernel test robot
2024-09-14 13:05 ` [PATCH V1 7/9] iommufd: file mappings for mdev Steve Sistare
2024-09-18 14:52   ` Steven Sistare
2024-09-14 13:05 ` [PATCH V1 8/9] iommufd: replace upages_len Steve Sistare
2024-09-14 13:05 ` [PATCH V1 9/9] iommufd: optimize file mapping Steve Sistare
2024-09-15 20:59   ` Jason Gunthorpe
2024-09-18 14:52     ` Steven Sistare
2024-09-14 13:21 ` [PATCH V1 0/9] iommu_ioas_map_file Steven Sistare
2024-09-15 20:30 ` Jason Gunthorpe
2024-09-18 14:52   ` Steven Sistare
2024-09-23 17:33     ` Jason Gunthorpe

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=ZudIu+nvmY1N0CKR@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=iommu@lists.linux.dev \
    --cc=kevin.tian@intel.com \
    --cc=nicolinc@nvidia.com \
    --cc=steven.sistare@oracle.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