iommu.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
From: Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
To: Jean-Philippe Brucker
	<jean-philippe.brucker-5wv7dgnIgG8@public.gmane.org>,
	iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org
Cc: kevin.tian-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org,
	rafael-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org,
	will.deacon-5wv7dgnIgG8@public.gmane.org,
	christian.koenig-5C7GfCeVMHo@public.gmane.org,
	alex.williamson-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
	robin.murphy-5wv7dgnIgG8@public.gmane.org
Subject: Re: [RFC PATCH 2/6] drivers core: Add I/O ASID allocator
Date: Mon, 22 Oct 2018 12:49:47 +0800	[thread overview]
Message-ID: <9c6cd6c1-3569-4251-8344-fc9df0e743bc@linux.intel.com> (raw)
In-Reply-To: <20181019181158.2395-3-jean-philippe.brucker-5wv7dgnIgG8@public.gmane.org>

Hi,

On 10/20/18 2:11 AM, Jean-Philippe Brucker wrote:
> Some devices might support multiple DMA address spaces, in particular
> those that have the PCI PASID feature. PASID (Process Address Space ID)
> allows to share process address spaces with devices (SVA), partition a
> device into VM-assignable entities (VFIO mdev) or simply provide
> multiple DMA address space to kernel drivers. Add a global PASID
> allocator usable by different drivers at the same time. Name it I/O ASID
> to avoid confusion with ASIDs allocated by arch code, which are usually
> a separate ID space.
> 
> The IOASID space is global. Each device can have its own PASID space,
> but by convention the IOMMU ended up having a global PASID space, so
> that with SVA, each mm_struct is associated to a single PASID.
> 
> The allocator doesn't really belong in drivers/iommu because some
> drivers would like to allocate PASIDs for devices that aren't managed by
> an IOMMU, using the same ID space as IOMMU. It doesn't really belong in
> drivers/pci either since platform device also support PASID. Add the
> allocator in drivers/base.

One concern of moving pasid allocator here is about paravirtual
allocation of pasid.

Since there is only a single set of pasid tables which is controlled by
the host, the pasid is a system wide resource. When a driver running in
a guest VM wants to consume a pasid, it must be intercepted by the
simulation software and routed the allocation to the host via VFIO. Some
iommu arch's provide mechanisms to aid this, for example, the virtual
command interfaces defined in vt-d 3.0. Any pasid used in guest VM
should go through the virtual command interfaces.

Best regards,
Lu Baolu

> 
> Signed-off-by: Jean-Philippe Brucker <jean-philippe.brucker-5wv7dgnIgG8@public.gmane.org>
> ---
>   drivers/base/Kconfig   |   7 +++
>   drivers/base/Makefile  |   1 +
>   drivers/base/ioasid.c  | 140 +++++++++++++++++++++++++++++++++++++++++
>   include/linux/ioasid.h |  45 +++++++++++++
>   4 files changed, 193 insertions(+)
>   create mode 100644 drivers/base/ioasid.c
>   create mode 100644 include/linux/ioasid.h
> 
> diff --git a/drivers/base/Kconfig b/drivers/base/Kconfig
> index 3e63a900b330..9e41653467d7 100644
> --- a/drivers/base/Kconfig
> +++ b/drivers/base/Kconfig
> @@ -182,6 +182,13 @@ config DMA_SHARED_BUFFER
>   	  APIs extension; the file's descriptor can then be passed on to other
>   	  driver.
>   
> +config IOASID
> +	bool
> +	default n
> +	help
> +	  Enable the I/O Address Space ID allocator. A single ID space shared
> +	  between different users.
> +
>   config DMA_FENCE_TRACE
>   	bool "Enable verbose DMA_FENCE_TRACE messages"
>   	depends on DMA_SHARED_BUFFER
> diff --git a/drivers/base/Makefile b/drivers/base/Makefile
> index 704f44295810..79e83a1b344b 100644
> --- a/drivers/base/Makefile
> +++ b/drivers/base/Makefile
> @@ -23,6 +23,7 @@ obj-$(CONFIG_PINCTRL) += pinctrl.o
>   obj-$(CONFIG_DEV_COREDUMP) += devcoredump.o
>   obj-$(CONFIG_GENERIC_MSI_IRQ_DOMAIN) += platform-msi.o
>   obj-$(CONFIG_GENERIC_ARCH_TOPOLOGY) += arch_topology.o
> +obj-$(CONFIG_IOASID) += ioasid.o
>   
>   obj-y			+= test/
>   
> diff --git a/drivers/base/ioasid.c b/drivers/base/ioasid.c
> new file mode 100644
> index 000000000000..71ab7ee73ecb
> --- /dev/null
> +++ b/drivers/base/ioasid.c
> @@ -0,0 +1,140 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * I/O Address Space ID allocator. There is one global IOASID space, split into
> + * subsets. Users create a subset with DECLARE_IOASID_SET, then allocate and
> + * free IOASIDs with ioasid_alloc and ioasid_free.
> + */
> +#include <linux/idr.h>
> +#include <linux/ioasid.h>
> +#include <linux/slab.h>
> +#include <linux/spinlock.h>
> +
> +struct ioasid_data {
> +	ioasid_t id;
> +	struct ioasid_set *set;
> +	void *private;
> +};
> +
> +static DEFINE_IDR(ioasid_idr);
> +
> +/**
> + * ioasid_alloc - Allocate an IOASID
> + * @set: the IOASID set
> + * @min: the minimum ID (inclusive)
> + * @max: the maximum ID (exclusive)
> + * @private: data private to the caller
> + *
> + * Allocate an ID between @min and @max (or %0 and %INT_MAX). Return the
> + * allocated ID on success, or INVALID_IOASID on failure. The @private pointer
> + * is stored internally and can be retrieved with ioasid_find().
> + */
> +ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
> +		      void *private)
> +{
> +	int id;
> +	struct ioasid_data *data;
> +
> +	data = kzalloc(sizeof(*data), GFP_KERNEL);
> +	if (!data)
> +		return INVALID_IOASID;
> +
> +	data->set = set;
> +	data->private = private;
> +
> +	idr_preload(GFP_KERNEL);
> +	idr_lock(&ioasid_idr);
> +	data->id = id = idr_alloc(&ioasid_idr, data, min, max, GFP_ATOMIC);
> +	idr_unlock(&ioasid_idr);
> +	idr_preload_end();
> +
> +	if (id < 0) {
> +		kfree(data);
> +		return INVALID_IOASID;
> +	}
> +
> +	return data->id;
> +}
> +EXPORT_SYMBOL_GPL(ioasid_alloc);
> +
> +/**
> + * ioasid_free - Free an IOASID
> + * @ioasid: the ID to remove
> + */
> +void ioasid_free(ioasid_t ioasid)
> +{
> +	struct ioasid_data *ioasid_data;
> +
> +	idr_lock(&ioasid_idr);
> +	ioasid_data = idr_remove(&ioasid_idr, ioasid);
> +	idr_unlock(&ioasid_idr);
> +
> +	kfree(ioasid_data);
> +}
> +EXPORT_SYMBOL_GPL(ioasid_free);
> +
> +struct ioasid_iter_data {
> +	struct ioasid_set *set;
> +	ioasid_iter_t func;
> +	void *data;
> +};
> +
> +static int ioasid_iter(int ioasid, void *p, void *data)
> +{
> +	struct ioasid_iter_data *iter_data = data;
> +	struct ioasid_data *ioasid_data = p;
> +
> +	if (iter_data->set != ioasid_data->set)
> +		return 0;
> +
> +	return iter_data->func(ioasid, ioasid_data->private, iter_data->data);
> +}
> +
> +/**
> + * ioasid_for_each - Iterate over IOASIDs for this set.
> + * @set: the IOASID set
> + * @func: called for each matching IOASID.
> + * @data: data passed to the callback
> + *
> + * Execute @func for all IOASIDs in the given set. If @func returns anything
> + * other than %0, the iteration stops and that value is returned from this
> + * function.
> + */
> +int ioasid_for_each(struct ioasid_set *set, ioasid_iter_t func, void *data)
> +{
> +	int ret;
> +	struct ioasid_iter_data iter_data = {
> +		.set	= set,
> +		.func	= func,
> +		.data	= data,
> +	};
> +
> +	idr_lock(&ioasid_idr);
> +	ret = idr_for_each(&ioasid_idr, ioasid_iter, &iter_data);
> +	idr_unlock(&ioasid_idr);
> +
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(ioasid_for_each);
> +
> +/**
> + * ioasid_find - Find IOASID data
> + * @set: the IOASID set
> + * @ioasid: the IOASID to find
> + *
> + * If the IOASID has been allocated for this set, return the private pointer
> + * passed to ioasid_alloc. Otherwise return NULL.
> + */
> +void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid)
> +{
> +	void *priv = NULL;
> +	struct ioasid_data *ioasid_data;
> +
> +	idr_lock(&ioasid_idr);
> +	ioasid_data = idr_find(&ioasid_idr, ioasid);
> +	if (ioasid_data && ioasid_data->set == set)
> +		priv = ioasid_data->private;
> +	idr_unlock(&ioasid_idr);
> +
> +	return priv;
> +}
> +EXPORT_SYMBOL_GPL(ioasid_find);
> diff --git a/include/linux/ioasid.h b/include/linux/ioasid.h
> new file mode 100644
> index 000000000000..cf6fb3496692
> --- /dev/null
> +++ b/include/linux/ioasid.h
> @@ -0,0 +1,45 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef __LINUX_IOASID_H
> +#define __LINUX_IOASID_H
> +
> +#define INVALID_IOASID ((ioasid_t)-1)
> +typedef unsigned int ioasid_t;
> +typedef int (*ioasid_iter_t)(ioasid_t ioasid, void *private, void *data);
> +
> +struct ioasid_set {
> +	int dummy;
> +};
> +
> +#define DECLARE_IOASID_SET(name) struct ioasid_set name = { 0 }
> +
> +#ifdef CONFIG_IOASID
> +ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min, ioasid_t max,
> +		      void *private);
> +void ioasid_free(ioasid_t ioasid);
> +
> +int ioasid_for_each(struct ioasid_set *set, ioasid_iter_t func, void *data);
> +void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid);
> +
> +#else /* !CONFIG_IOASID */
> +static inline ioasid_t ioasid_alloc(struct ioasid_set *set, ioasid_t min,
> +				    ioasid_t max, void *private)
> +{
> +	return INVALID_IOASID;
> +}
> +
> +static inline void ioasid_free(ioasid_t ioasid)
> +{
> +}
> +
> +static inline int ioasid_for_each(struct ioasid_set *set, ioasid_iter_t func,
> +				  void *data)
> +{
> +	return -ESRCH;
> +}
> +
> +static inline void *ioasid_find(struct ioasid_set *set, ioasid_t ioasid)
> +{
> +	return -ESRCH;
> +}
> +#endif /* CONFIG_IOASID */
> +#endif /* __LINUX_IOASID_H */
> 

  parent reply	other threads:[~2018-10-22  4:49 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-19 18:11 [RFC PATCH 0/6] Auxiliary IOMMU domains and Arm SMMUv3 Jean-Philippe Brucker
     [not found] ` <20181019181158.2395-1-jean-philippe.brucker-5wv7dgnIgG8@public.gmane.org>
2018-10-19 18:11   ` [RFC PATCH 1/6] iommu: Adapt attach/detach_dev() for auxiliary domains Jean-Philippe Brucker
     [not found]     ` <20181019181158.2395-2-jean-philippe.brucker-5wv7dgnIgG8@public.gmane.org>
2018-10-22  2:32       ` Lu Baolu
2018-10-19 18:11   ` [RFC PATCH 2/6] drivers core: Add I/O ASID allocator Jean-Philippe Brucker
     [not found]     ` <20181019181158.2395-3-jean-philippe.brucker-5wv7dgnIgG8@public.gmane.org>
2018-10-22  4:49       ` Lu Baolu [this message]
     [not found]         ` <9c6cd6c1-3569-4251-8344-fc9df0e743bc-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2018-10-22 10:22           ` Raj, Ashok
     [not found]             ` <20181022102254.GA25399-7RUrO8UaCDyr4tA6zuQqW9h3ngVCH38I@public.gmane.org>
2018-10-23  6:56               ` Lu Baolu
     [not found]                 ` <02006e4f-2acf-6ff8-b695-c54c99509b46-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2018-10-23 22:13                   ` Tian, Kevin
2018-11-07  4:53       ` Lu Baolu
     [not found]         ` <fb2bd5fe-5742-fcd8-b8f0-1885040e8d4f-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2018-11-08 18:51           ` Jean-Philippe Brucker
2018-11-12 14:40       ` Joerg Roedel
     [not found]         ` <20181112144039.GA25808-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-11-21 11:16           ` Jean-Philippe Brucker
     [not found]             ` <8f17757a-c657-aab9-a6a0-fb0cc9c610a8-5wv7dgnIgG8@public.gmane.org>
2018-11-21 19:10               ` Koenig, Christian
     [not found]                 ` <62f05552-df46-6e12-10ed-820429dfda59-5C7GfCeVMHo@public.gmane.org>
2018-11-22  6:59                   ` Tian, Kevin
2018-11-22  8:38                   ` Joerg Roedel
2018-11-22  8:44               ` Joerg Roedel
     [not found]                 ` <20181122084429.GB1586-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-11-22 11:17                   ` Jean-Philippe Brucker
2018-10-19 18:11   ` [RFC PATCH 3/6] iommu/sva: Use external PASID allocator Jean-Philippe Brucker
2018-10-19 18:11   ` [RFC PATCH 4/6] iommu/sva: Support AUXD feature Jean-Philippe Brucker
2018-10-19 18:11   ` [RFC PATCH 5/6] iommu/arm-smmu-v3: Implement detach_dev op Jean-Philippe Brucker
2018-10-19 18:11   ` [RFC PATCH 6/6] iommu/arm-smmu-v3: Add support for auxiliary domains Jean-Philippe Brucker
2018-10-20  3:36   ` [RFC PATCH 0/6] Auxiliary IOMMU domains and Arm SMMUv3 Xu Zaibo
2018-10-22  6:53   ` Tian, Kevin
     [not found]     ` <AADFC41AFE54684AB9EE6CBC0274A5D19BE0E176-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2018-10-22 11:50       ` Robin Murphy
     [not found]         ` <11f88122-afd3-a34c-3cd4-db681bf5498b-5wv7dgnIgG8@public.gmane.org>
2018-10-22 15:35           ` Jordan Crouse
2018-10-22 18:01           ` Jean-Philippe Brucker
2018-11-06 16:25           ` joro-zLv9SwRftAIdnm+yROfE0A
     [not found]             ` <20181106162539.4gmkvg57mja3bn7k-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-11-07  3:40               ` Lu Baolu
     [not found]                 ` <e22e3631-2ecb-664d-5666-8e0f865dec83-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2018-11-07 16:43                   ` joro-zLv9SwRftAIdnm+yROfE0A
     [not found]                     ` <20181107164323.GA19831-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-11-07 17:23                       ` Alex Williamson
2018-11-21  4:40                       ` Lu Baolu
     [not found]                         ` <758bb120-5bc0-1e2d-ccd0-9be0bcc5d8bc-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2018-11-23 11:21                           ` joro-zLv9SwRftAIdnm+yROfE0A
     [not found]                             ` <20181123112125.GF1586-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-11-25  6:51                               ` Lu Baolu
2018-11-26  3:01                               ` Tian, Kevin
     [not found]                             ` <AADFC41AFE54684AB9EE6CBC0274A5D19BE68777@SHSMSX101.ccr.corp.intel.com>
     [not found]                               ` <AADFC41AFE54684AB9EE6CBC0274A5D19BE68777-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2018-11-26  7:29                                 ` Tian, Kevin
     [not found]                                   ` <AADFC41AFE54684AB9EE6CBC0274A5D19BE689B8-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2018-12-07 10:29                                     ` 'joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org'
     [not found]                                       ` <20181207102926.GM16835-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-12-10  2:06                                         ` Tian, Kevin
     [not found]                                           ` <AADFC41AFE54684AB9EE6CBC0274A5D19BE95394-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2018-12-10  8:57                                             ` 'joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org'
     [not found]                                               ` <20181210085745.GN16835-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-12-11 18:34                                                 ` Jean-Philippe Brucker
     [not found]                                                   ` <4be63d12-fa1c-b180-761b-5e8ceed58545-5wv7dgnIgG8@public.gmane.org>
2018-12-12  9:22                                                     ` 'joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org'
2018-12-12  9:31                                                 ` Tian, Kevin
     [not found]                                                   ` <AADFC41AFE54684AB9EE6CBC0274A5D19BE9D6CA-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2018-12-12  9:54                                                     ` 'joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org'
     [not found]                                                       ` <20181212095403.GU16835-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-12-12 10:03                                                         ` Tian, Kevin
2018-12-10  2:57                                         ` Lu Baolu
     [not found]                                           ` <bf1ee4a3-6d3f-e0db-a02a-1db819843a60-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2018-12-10  8:59                                             ` 'joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org'
2018-12-11 13:35                                         ` Jean-Philippe Brucker
     [not found]                                           ` <fc173d9f-57e2-dd87-95d0-1c615f2e14e3-5wv7dgnIgG8@public.gmane.org>
2018-12-12  9:29                                             ` 'joro-zLv9SwRftAIdnm+yROfE0A@public.gmane.org'
2018-11-08 18:29               ` Jean-Philippe Brucker
     [not found]                 ` <42949d93-e22c-dd4d-cd49-46efc0b73cdb-5wv7dgnIgG8@public.gmane.org>
2018-11-12 14:55                   ` joro-zLv9SwRftAIdnm+yROfE0A
     [not found]                     ` <20181112145541.GB25808-zLv9SwRftAIdnm+yROfE0A@public.gmane.org>
2018-11-21 19:05                       ` Jean-Philippe Brucker
     [not found]                         ` <5dcf9238-62b2-8df6-b378-183ee09c5951-5wv7dgnIgG8@public.gmane.org>
2018-11-23 12:50                           ` joro-zLv9SwRftAIdnm+yROfE0A
2018-11-22  8:39                       ` Tian, Kevin
     [not found]                         ` <AADFC41AFE54684AB9EE6CBC0274A5D19BE5A7A7-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org>
2018-11-23 12:14                           ` joro-zLv9SwRftAIdnm+yROfE0A
2018-10-22 10:07   ` Raj, Ashok
     [not found]     ` <20181021194426.GA11201-7RUrO8UaCDyr4tA6zuQqW9h3ngVCH38I@public.gmane.org>
2018-10-22 16:03       ` Jean-Philippe Brucker
     [not found]         ` <d45c5222-68e9-1d6e-730b-bb8dbc060586-5wv7dgnIgG8@public.gmane.org>
2018-10-23 17:16           ` Raj, Ashok
     [not found]             ` <1540314963.21962.20.camel-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2018-10-23 22:08               ` Tian, Kevin
2018-10-26  3:00           ` Lu Baolu
2018-10-22 16:48   ` Jordan Crouse
     [not found]     ` <20181022164834.GH26762-9PYrDHPZ2Orvke4nUoYGnHL1okKdlPRT@public.gmane.org>
2018-11-02  3:19       ` Lu Baolu

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=9c6cd6c1-3569-4251-8344-fc9df0e743bc@linux.intel.com \
    --to=baolu.lu-vuqaysv1563yd54fqh9/ca@public.gmane.org \
    --cc=alex.williamson-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
    --cc=christian.koenig-5C7GfCeVMHo@public.gmane.org \
    --cc=gregkh-hQyY1W1yCW8ekmWlsbkhG0B+6BGkLq7r@public.gmane.org \
    --cc=iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org \
    --cc=jean-philippe.brucker-5wv7dgnIgG8@public.gmane.org \
    --cc=kevin.tian-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org \
    --cc=rafael-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=robin.murphy-5wv7dgnIgG8@public.gmane.org \
    --cc=will.deacon-5wv7dgnIgG8@public.gmane.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).