linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Robin Murphy <robin.murphy@arm.com>
To: Boris Brezillon <boris.brezillon@collabora.com>,
	Joerg Roedel <joro@8bytes.org>,
	iommu@lists.linux.dev, Will Deacon <will@kernel.org>,
	linux-arm-kernel@lists.infradead.org
Cc: Rob Clark <robdclark@gmail.com>,
	Gaurav Kohli <quic_gkohli@quicinc.com>,
	Steven Price <steven.price@arm.com>
Subject: Re: [PATCH v2 1/2] iommu: Allow passing custom allocators to pgtable drivers
Date: Wed, 22 Nov 2023 13:08:20 +0000	[thread overview]
Message-ID: <501de6e6-420b-4419-bc8f-66327875e1ca@arm.com> (raw)
In-Reply-To: <20231110094352.565347-2-boris.brezillon@collabora.com>

On 2023-11-10 9:43 am, Boris Brezillon wrote:
> This will be useful for GPU drivers who want to keep page tables in a
> pool so they can:
> 
> - keep freed page tables in a free pool and speed-up upcoming page
>    table allocations
> - batch page table allocation instead of allocating one page at a time
> - pre-reserve pages for page tables needed for map/unmap operations,
>    to ensure map/unmap operations don't try to allocate memory in paths
>    they're allowed to block or fail
> 
> It might also be valuable for other aspects of GPU and similar
> use-cases, like fine-grained memory accounting and resource limiting.
> 
> We will extend the Arm LPAE format to support custom allocators in a
> separate commit.

A couple of very minor nits below, but regardless,

Reviewed-by: Robin Murphy <robin.murphy@arm.com>

> Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
> Reviewed-by: Steven Price <steven.price@arm.com>
> ---
> v2:
> - Add Steven R-b
> - Expand on possible use-cases for custom allocators
> - Add a caps fields to io_pgtable_init_fns so we can simplify the
>    check_custom_allocator() logic (Robin Murphy)
> ---
>   drivers/iommu/io-pgtable.c | 23 +++++++++++++++++++++++
>   include/linux/io-pgtable.h | 31 +++++++++++++++++++++++++++++++
>   2 files changed, 54 insertions(+)
> 
> diff --git a/drivers/iommu/io-pgtable.c b/drivers/iommu/io-pgtable.c
> index b843fcd365d2..4febf73c83ff 100644
> --- a/drivers/iommu/io-pgtable.c
> +++ b/drivers/iommu/io-pgtable.c
> @@ -34,6 +34,26 @@ io_pgtable_init_table[IO_PGTABLE_NUM_FMTS] = {
>   #endif
>   };
>   
> +static int check_custom_allocator(enum io_pgtable_fmt fmt,
> +				  struct io_pgtable_cfg *cfg)
> +{
> +	/* When passing a custom allocator, both the alloc and free
> +	 * functions should be provided.
> +	 */
> +	if ((cfg->alloc != NULL) != (cfg->free != NULL))
> +		return -EINVAL;
> +
> +	/* No custom allocator, no need to check the format. */
> +	if (!cfg->alloc)
> +		return 0;

I can see why you've done it this way round, but logically it seems 
weird to check that the allocator is valid *before* checking that we 
have an allocator. My instinct would be something like:

	if (!cfg->alloc && !cfg->free)
		return 0;

	if (!cfg->alloc || !cfg->free)
		return -EINVAL

but yeah, I'm still not sure it's *significantly* more readable :/

> +
> +	/* Make sure the format supports custom allocators. */
> +	if (io_pgtable_init_table[fmt]->caps & IO_PGTABLE_CAP_CUSTOM_ALLOCATOR)
> +		return 0;
> +
> +	return -EINVAL;
> +}
> +
>   struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
>   					    struct io_pgtable_cfg *cfg,
>   					    void *cookie)
> @@ -44,6 +64,9 @@ struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
>   	if (fmt >= IO_PGTABLE_NUM_FMTS)
>   		return NULL;
>   
> +	if (check_custom_allocator(fmt, cfg))
> +		return NULL;
> +
>   	fns = io_pgtable_init_table[fmt];
>   	if (!fns)
>   		return NULL;
> diff --git a/include/linux/io-pgtable.h b/include/linux/io-pgtable.h
> index 1b7a44b35616..17681ac678ee 100644
> --- a/include/linux/io-pgtable.h
> +++ b/include/linux/io-pgtable.h
> @@ -100,6 +100,27 @@ struct io_pgtable_cfg {
>   	const struct iommu_flush_ops	*tlb;
>   	struct device			*iommu_dev;
>   
> +	/**
> +	 * @alloc: Custom page allocator.
> +	 *
> +	 * Optional hook used to allocate page tables. If this function is NULL,
> +	 * @free must be NULL too.
> +	 *
> +	 * Not all formats support custom page allocators. Before considering
> +	 * passing a non-NULL value, make sure the chosen page format supports
> +	 * this feature.
> +	 */
> +	void *(*alloc)(void *cookie, size_t size, gfp_t gfp);
> +
> +	/**
> +	 * @free: Custom page de-allocator.
> +	 *
> +	 * Optional hook used to free page tables allocated with the @alloc
> +	 * hook. Must be non-NULL if @alloc is not NULL, must be NULL
> +	 * otherwise.
> +	 */
> +	void (*free)(void *cookie, void *pages, size_t size);
> +
>   	/* Low-level data specific to the table format */
>   	union {
>   		struct {
> @@ -237,14 +258,24 @@ io_pgtable_tlb_add_page(struct io_pgtable *iop,
>   		iop->cfg.tlb->tlb_add_page(gather, iova, granule, iop->cookie);
>   }
>   
> +/**
> + * enum io_pgtable_caps - IO page table backend capabilities.
> + */
> +enum io_pgtable_caps {
> +	/** @IO_PGTABLE_CAP_CUSTOM_ALLOCATOR: Backend accepts custom page table allocators. */
> +	IO_PGTABLE_CAP_CUSTOM_ALLOCATOR = BIT(0),
> +};
> +
>   /**
>    * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
>    *                              particular format.
>    *
> + * @caps:  Combination of @io_pgtable_caps flags encoding the backend capabilities.
>    * @alloc: Allocate a set of page tables described by cfg.
>    * @free:  Free the page tables associated with iop.
>    */
>   struct io_pgtable_init_fns {
> +	u32 caps;

I'd put this last in the structure for cosmetic and padding reasons.

Cheers,
Robin.

>   	struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
>   	void (*free)(struct io_pgtable *iop);
>   };

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2023-11-22 13:09 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-10  9:43 [PATCH v2 0/2] iommu: Allow passing custom allocators to pgtable drivers Boris Brezillon
2023-11-10  9:43 ` [PATCH v2 1/2] " Boris Brezillon
2023-11-22 13:08   ` Robin Murphy [this message]
2023-11-10  9:43 ` [PATCH v2 2/2] iommu: Extend LPAE page table format to support custom allocators Boris Brezillon
2023-11-22 14:24   ` Robin Murphy
2023-11-10 10:47 ` [PATCH v2 0/2] iommu: Allow passing custom allocators to pgtable drivers Gaurav Kohli
2023-11-10 15:14 ` Jason Gunthorpe
2023-11-10 15:48   ` Boris Brezillon
2023-11-10 16:12     ` Jason Gunthorpe
2023-11-10 19:16       ` Boris Brezillon
2023-11-10 19:42         ` Jason Gunthorpe
2023-11-13  9:11           ` Boris Brezillon
2023-11-14 16:27             ` Jason Gunthorpe
2023-11-20 14:04               ` Jason Gunthorpe
2023-11-20 14:38                 ` Boris Brezillon
2023-11-20 14:46                   ` Jason Gunthorpe
2023-11-20 15:14                     ` Boris Brezillon
2023-11-20 15:45                       ` Jason Gunthorpe
2023-11-22 17:23                         ` Robin Murphy
2023-11-22 17:50                           ` Jason Gunthorpe
2023-11-23  8:51                             ` Boris Brezillon
2023-11-23 13:48                               ` Jason Gunthorpe
2023-11-23 16:49                                 ` Robin Murphy
2023-11-23 16:59                                   ` 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=501de6e6-420b-4419-bc8f-66327875e1ca@arm.com \
    --to=robin.murphy@arm.com \
    --cc=boris.brezillon@collabora.com \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=quic_gkohli@quicinc.com \
    --cc=robdclark@gmail.com \
    --cc=steven.price@arm.com \
    --cc=will@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).