Introduce /dev/iommu for userspace I/O address space management
Subsystems (10)
Re: [RFC 10/20] iommu/iommufd: Add IOMMU_DEVICE_GET_INFO
In reply to Liu Yi LQuoted 144 lines · Liu Yi L
After a device is bound to the iommufd, userspace can use this interface to query the underlying iommu capability and format info for this device. Based on this information the user then creates I/O address space in a compatible format with the to-be-attached devices.
Device cookie which is registered at binding time is used to mark the device which is being queried here.
Signed-off-by Liu Yi L <[email protected]>--- drivers/iommu/iommufd/iommufd.c | 68 +++++++++++++++++++++++++++++++++ include/uapi/linux/iommu.h | 49 ++++++++++++++++++++++++ 2 files changed, 117 insertions(+)drivers/iommu/iommufd/iommufd.c +68
-0@@ -117,6 +117,71 @@ static int iommufd_fops_release(struct inode *inode, struct file *filep)return 0 ; } +static struct device * +iommu_find_device_from_cookie (struct iommufd_ctx *ictx,u64 dev_cookie) +{ +struct iommufd_device *idev; +struct device *dev =NULL ; +unsigned long index; + +mutex_lock (&ictx->lock); +xa_for_each (&ictx->device_xa, index, idev) { +if (idev->dev_cookie == dev_cookie) { + dev = idev->dev; +break ; + } + } +mutex_unlock (&ictx->lock); + +return dev; +} + +static void iommu_device_build_info (struct device *dev, +struct iommu_device_info *info) +{ +bool snoop; +u64 awidth, pgsizes; + +if (!iommu_device_get_info (dev,IOMMU_DEV_INFO_FORCE_SNOOP , &snoop)) + info->flags |= snoop ?IOMMU_DEVICE_INFO_ENFORCE_SNOOP :0 ; + +if (!iommu_device_get_info (dev,IOMMU_DEV_INFO_PAGE_SIZE , &pgsizes)) { + info->pgsize_bitmap = pgsizes; + info->flags |=IOMMU_DEVICE_INFO_PGSIZES ; + } + +if (!iommu_device_get_info (dev,IOMMU_DEV_INFO_ADDR_WIDTH , &awidth)) { + info->addr_width = awidth; + info->flags |=IOMMU_DEVICE_INFO_ADDR_WIDTH ; + } +} + +static int iommufd_get_device_info (struct iommufd_ctx *ictx, +unsigned long arg) +{ +struct iommu_device_info info; +unsigned long minsz; +struct device *dev; + + minsz = offsetofend(struct iommu_device_info, addr_width); + +if (copy_from_user (&info, (void __user *)arg, minsz)) +return -EFAULT ; + +if (info.argsz < minsz) +return -EINVAL ; + + info.flags =0 ; + + dev =iommu_find_device_from_cookie (ictx, info.dev_cookie); +if (!dev) +return -EINVAL ; + +iommu_device_build_info (dev, &info); + +return copy_to_user ((void __user *)arg, &info, minsz) ? -EFAULT :0 ; +} +static long iommufd_fops_unl_ioctl (struct file *filep,unsigned int cmd,unsigned long arg) {@@ -127,6 +192,9 @@ static long iommufd_fops_unl_ioctl(struct file *filep,return ret;switch (cmd) { +case IOMMU_DEVICE_GET_INFO : + ret =iommufd_get_device_info (ictx, arg); +break ;default :pr_err_ratelimited ("unsupported cmd %u\n" , cmd);break ;include/uapi/linux/iommu.h +36
-0@@ -7,6 +7,55 @@#define _UAPI_IOMMU_H#include <linux/types.h> +#include <linux/ioctl.h> + +/* -------- IOCTLs for IOMMU file descriptor (/dev/iommu) -------- */ + +#define IOMMU_TYPE (';') +#define IOMMU_BASE 100 + +/* +* IOMMU_DEVICE_GET_INFO - _IOR(IOMMU_TYPE, IOMMU_BASE + 1, +* struct iommu_device_info) +* +* Check IOMMU capabilities and format information on a bound device. +* +* The device is identified by device cookie (registered when binding +* this device). +* +* @argsz: user filled size of this data. +* @flags: tells userspace which capability info is available +* @dev_cookie: user assinged cookie. +* @pgsize_bitmap: Bitmap of supported page sizes. 1-setting of the +* bit in pgsize_bitmap[63:12] indicates a supported +* page size. Details as below table: +* +* +===============+============+ +* | Bit[index] | Page Size | +* +---------------+------------+ +* | 12 | 4 KB | +* +---------------+------------+ +* | 13 | 8 KB | +* +---------------+------------+ +* | 14 | 16 KB | +* +---------------+------------+ +* ... +* @addr_width: the address width of supported I/O address spaces. +* +* Availability: after device is bound to iommufd
include/uapi/linux/iommu.h line 55
Quoted 10 lines
include/uapi/linux/iommu.h +10
-0+*/ +struct iommu_device_info { +__u32 argsz; +__u32 flags; +#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP (1 << 0)/* IOMMU enforced snoop */ +#define IOMMU_DEVICE_INFO_PGSIZES (1 << 1)/* supported page sizes */ +#define IOMMU_DEVICE_INFO_ADDR_WIDTH (1 << 2)/* addr_wdith field valid */ +__u64 dev_cookie; +__u64 pgsize_bitmap; +__u32 addr_width;
I think this is where you should be reporting available IOVA windows, rather than just an address width. I know that for ppc a real situation will be to have two different windows of different sizes:
that is the effective address width depends on which IOVA window you're mapping into.
Quoted 6 lines
include/uapi/linux/iommu.h +3
-0+}; + +#define IOMMU_DEVICE_GET_INFO _IO(IOMMU_TYPE, IOMMU_BASE + 1)#define IOMMU_FAULT_PERM_READ (1 << 0)/* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1)/* write */
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
RFC 11/20 iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Liu Yi LThis patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify the addr_width of the I/O address space and whether the I/O page table is created in an iommu enfore_snoop format. enforce_snoop must be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.
Userspace is expected to call IOMMU_CHECK_EXTENSION (see next patch) for what formats can be specified when allocating an IOASID.
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, as addr_width for pci devices can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1 discussion and then decide how to incorporate it in v2.
- Currently ioasid term has already been used in the kernel (drivers/iommu/ ioasid.c) to represent the hardware I/O address space ID in the wire. It covers both PCI PASID (Process Address Space ID) and ARM SSID (Sub-Stream ID). We need find a way to resolve the naming conflict between the hardware ID and software handle. One option is to rename the existing ioasid to be pasid or ssid, given their full names still sound generic. Appreciate more thoughts on this open!
--- drivers/iommu/iommufd/iommufd.c | 120 ++++++++++++++++++++++++++++++++ include/linux/iommufd.h | 3 + include/uapi/linux/iommu.h | 54 ++++++++++++++ 3 files changed, 177 insertions(+)
drivers/iommu/iommufd/iommufd.c +120 -0
@@ -24,6 +24,7 @@struct iommufd_ctx {refcount_t refs;struct mutex lock; +struct xarray ioasid_xa;/* xarray of ioasids */ struct xarray device_xa;/* xarray of bound devices */ };
@@ -42,6 +43,16 @@ struct iommufd_device {u64 dev_cookie; }; +/* Represent an I/O address space */ +struct iommufd_ioas { +int ioasid; +u32 type; +u32 addr_width; +bool enforce_snoop; +struct iommufd_ctx *ictx; +refcount_t refs; +}; +static int iommufd_fops_open (struct inode *inode,struct file *filep) {struct iommufd_ctx *ictx;
@@ -53,6 +64,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filep)refcount_set (&ictx->refs,1 );mutex_init (&ictx->lock); +xa_init_flags (&ictx->ioasid_xa,XA_FLAGS_ALLOC );xa_init_flags (&ictx->device_xa,XA_FLAGS_ALLOC ); filep->private_data = ictx;
@@ -102,16 +114,118 @@ static void iommufd_ctx_put(struct iommufd_ctx *ictx)if (!refcount_dec_and_test (&ictx->refs))return ; +WARN_ON (!xa_empty (&ictx->ioasid_xa));WARN_ON (!xa_empty (&ictx->device_xa));kfree (ictx); } +/* Caller should hold ictx->lock */ +static void ioas_put_locked (struct iommufd_ioas *ioas) +{ +struct iommufd_ctx *ictx = ioas->ictx; +int ioasid = ioas->ioasid; + +if (!refcount_dec_and_test (&ioas->refs)) +return ; + +xa_erase (&ictx->ioasid_xa, ioasid); +iommufd_ctx_put (ictx); +kfree (ioas); +} + +static int iommufd_ioasid_alloc (struct iommufd_ctx *ictx,unsigned long arg) +{ +struct iommu_ioasid_alloc req; +struct iommufd_ioas *ioas; +unsigned long minsz; +int ioasid, ret; + + minsz = offsetofend(struct iommu_ioasid_alloc, addr_width); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz || !req.addr_width || + req.flags !=IOMMU_IOASID_ENFORCE_SNOOP || + req.type !=IOMMU_IOASID_TYPE_KERNEL_TYPE1V2 ) +return -EINVAL ; + + ioas =kzalloc (sizeof (*ioas),GFP_KERNEL ); +if (!ioas) +return -ENOMEM ; + +mutex_lock (&ictx->lock); + ret =xa_alloc (&ictx->ioasid_xa, &ioasid, ioas, +XA_LIMIT (IOMMUFD_IOASID_MIN ,IOMMUFD_IOASID_MAX ), +GFP_KERNEL ); +mutex_unlock (&ictx->lock); +if (ret) { +pr_err_ratelimited ("Failed to alloc ioasid\n" ); +kfree (ioas); +return ret; + } + + ioas->ioasid = ioasid; + +/* only supports kernel managed I/O page table so far */ + ioas->type =IOMMU_IOASID_TYPE_KERNEL_TYPE1V2 ; + + ioas->addr_width = req.addr_width; + +/* only supports enforce snoop today */ + ioas->enforce_snoop = true; + +iommufd_ctx_get (ictx); + ioas->ictx = ictx; + +refcount_set (&ioas->refs,1 ); + +return ioasid; +} + +static int iommufd_ioasid_free (struct iommufd_ctx *ictx,unsigned long arg) +{ +struct iommufd_ioas *ioas =NULL ; +int ioasid, ret; + +if (copy_from_user (&ioasid, (void __user *)arg,sizeof (ioasid))) +return -EFAULT ; + +if (ioasid <0 ) +return -EINVAL ; + +mutex_lock (&ictx->lock); + ioas =xa_load (&ictx->ioasid_xa, ioasid); +if (IS_ERR (ioas)) { + ret = -EINVAL ; + goto out_unlock; + } + +/* Disallow free if refcount is not 1 */ +if (refcount_read (&ioas->refs) >1 ) { + ret = -EBUSY ; + goto out_unlock; + } + +ioas_put_locked (ioas); +out_unlock: +mutex_unlock (&ictx->lock); +return ret; +}; +static int iommufd_fops_release (struct inode *inode,struct file *filep) {struct iommufd_ctx *ictx = filep->private_data; +struct iommufd_ioas *ioas; +unsigned long index; filep->private_data =NULL ; +mutex_lock (&ictx->lock); +xa_for_each (&ictx->ioasid_xa, index, ioas) +ioas_put_locked (ioas); +mutex_unlock (&ictx->lock); +iommufd_ctx_put (ictx);return 0 ;
@@ -195,6 +309,12 @@ static long iommufd_fops_unl_ioctl(struct file *filep,case IOMMU_DEVICE_GET_INFO : ret =iommufd_get_device_info (ictx, arg);break ; +case IOMMU_IOASID_ALLOC : + ret =iommufd_ioasid_alloc (ictx, arg); +break ; +case IOMMU_IOASID_FREE : + ret =iommufd_ioasid_free (ictx, arg); +break ;default :pr_err_ratelimited ("unsupported cmd %u\n" , cmd);break ;
include/linux/iommufd.h +3 -0
@@ -14,6 +14,9 @@#include <linux/err.h> #include <linux/device.h> +#define IOMMUFD_IOASID_MAX ((unsigned int)(0x7FFFFFFF)) +#define IOMMUFD_IOASID_MIN 0 +#define IOMMUFD_DEVID_MAX ((unsigned int)(0x7FFFFFFF))#define IOMMUFD_DEVID_MIN 0
include/uapi/linux/iommu.h +54 -0
60 lines not on this page · raw message
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
kernel test robot
build botRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Liu,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on hch-configfs/for-next]
[cannot apply to joro-iommu/next awilliam-vfio/next linus/master v5.15-rc1 next-20210917] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 base: git://git.infradead.org/users/hch/configfs.git for-next config: i386-randconfig-a016-20210919 (attached as .config) compiler: clang version 14.0.0 (https://github.com/llvm/llvm-project c8b3d7d6d6de37af68b2f379d0e37304f78e115f)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/262b8a31d0b9bbcb24f7fc2eed5e7ac849265047
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 git checkout 262b8a31d0b9bbcb24f7fc2eed5e7ac849265047
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 ARCH=i386
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> drivers/iommu/iommufd/iommufd.c:205:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
if (refcount_read(&ioas->refs) > 1) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:213:9: note: uninitialized use occurs here
return ret;
^~~
drivers/iommu/iommufd/iommufd.c:205:2: note: remove the 'if' if its condition is always true
if (refcount_read(&ioas->refs) > 1) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:189:17: note: initialize the variable 'ret' to silence this warning
int ioasid, ret;
^
= 0
drivers/iommu/iommufd/iommufd.c:369:7: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:409:17: note: uninitialized use occurs here
return ERR_PTR(ret);
^~~
drivers/iommu/iommufd/iommufd.c:369:3: note: remove the 'if' if its condition is always false
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:369:7: warning: variable 'ret' is used uninitialized whenever '||' condition is true [-Wsometimes-uninitialized]
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:409:17: note: uninitialized use occurs here
return ERR_PTR(ret);
^~~
drivers/iommu/iommufd/iommufd.c:369:7: note: remove the '||' if its condition is always false
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:359:9: note: initialize the variable 'ret' to silence this warning
int ret;
^
= 03 warnings generated.
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for IOMMUFD Depends on IOMMU_SUPPORT
Selected by
- VFIO_PCI && VFIO && PCI && EVENTFD && MMU
vim +205 drivers/iommu/iommufd/iommufd.c
185
186 static int iommufd_ioasid_free(struct iommufd_ctx *ictx, unsigned long arg)
187 {
188 struct iommufd_ioas *ioas = NULL;
189 int ioasid, ret;
190
191 if (copy_from_user(&ioasid, (void __user *)arg, sizeof(ioasid)))
192 return -EFAULT;
193
194 if (ioasid < 0)
195 return -EINVAL;
196
197 mutex_lock(&ictx->lock);
198 ioas = xa_load(&ictx->ioasid_xa, ioasid);
199 if (IS_ERR(ioas)) {
200 ret = -EINVAL;
201 goto out_unlock;
202 }
203
204 /* Disallow free if refcount is not 1 */
> 205 if (refcount_read(&ioas->refs) > 1) { 206 ret = -EBUSY;
207 goto out_unlock;
208 }
209
210 ioas_put_locked(ioas);
211 out_unlock:
212 mutex_unlock(&ictx->lock);
213 return ret;
214 };
215
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Liu Yi LQuoted 15 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.
Quoted 8 lines
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pci devices can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1 discussion and then decide how to incorporate it in v2.
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the actual geometry including any holes via a query.
Quoted 7 lines
- Currently ioasid term has already been used in the kernel (
drivers/iommu/ioasid.c) to represent the hardware I/O address space ID in the wire. It covers both PCI PASID (Process Address Space ID) and ARM SSID (Sub-Stream ID). We need find a way to resolve the naming conflict between the hardware ID and software handle. One option is to rename the existing ioasid to be pasid or ssid, given their full names still sound generic. Appreciate more thoughts on this open!
ioas works well here I think. Use ioas_id to refer to the xarray index.
Quoted 18 lines
Signed-off-by Liu Yi L <[email protected]>drivers/iommu/iommufd/iommufd.c | 120 ++++++++++++++++++++++++++++++++ include/linux/iommufd.h | 3 + include/uapi/linux/iommu.h | 54 ++++++++++++++ 3 files changed, 177 insertions(+)drivers/iommu/iommufd/iommufd.c +2
-0+++ b/drivers/iommu/iommufd/iommufd.c@@ -24,6 +24,7 @@struct iommufd_ctx {refcount_t refs;struct mutex lock; +struct xarray ioasid_xa;/* xarray of ioasids */ struct xarray device_xa;/* xarray of bound devices */ };
drivers/iommu/iommufd/iommufd.c struct iommufd_device { · line 48
Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +3
-0@@ -42,6 +43,16 @@ struct iommufd_device {u64 dev_cookie; }; +/* Represent an I/O address space */ +struct iommufd_ioas { +int ioasid;
xarray id's should consistently be u32s everywhere.
Many of the same prior comments repeated here
Jason
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 24 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pci
Quoted 8 lines
devices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
Quoted 1 line
actual geometry including any holes via a query.
I'd like to see a detail flow from David on how the uAPI works today with existing spapr driver and what exact changes he'd like to make on this proposed interface. Above info is still insufficient for us to think about the right solution.
Quoted 6 lines
Quoted 2 lines
- Currently ioasid term has already been used in the kernel
(
drivers/iommu/Quoted 2 lines
ioasid.c) to represent the hardware I/O address space ID in the wire. It covers both PCI PASID (Process Address Space ID) and ARM SSID (Sub-Stream
Quoted 8 lines
Quoted 1 line
ID). We need find a way to resolve the naming conflict between the
hardware
Quoted 3 lines
ID and software handle. One option is to rename the existing ioasid to be pasid or ssid, given their full names still sound generic. Appreciate more thoughts on this open!
ioas works well here I think. Use
ioas_idto refer to the xarray index.
What about when introducing pasid to this uAPI? Then use ioas_id for the xarray index and ioasid to represent pasid/ssid? At this point the software handle and hardware id are mixed together thus need a clear terminology to differentiate them.
Thanks
Kevin
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 27 lines · Kevin
Quoted 27 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pcidevices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
Quoted 7 lines
Quoted 5 lines
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
David explained it here:
https://lore.kernel.org/kvm/YMrKksUeNW%2FPEGPM@yekko/
qeumu needs to be able to chooose if it gets the 32 bit range or 64 bit range.
So a 'range hint' will do the job
David also suggested this:
https://lore.kernel.org/kvm/YL6%2FbjHyuHJTn4Rd@yekko/
So I like this better:
struct iommu_ioasid_alloc {
__u32 argsz;
__u32 flags;#define IOMMU_IOASID_ENFORCE_SNOOP (1 << 0)
#define IOMMU_IOASID_HINT_BASE_IOVA (1 << 1)
__aligned_u64 max_iova_hint; __aligned_u64 base_iova_hint; // Used only if IOMMU_IOASID_HINT_BASE_IOVA // For creating nested page tables __u32 parent_ios_id; __u32 format; #define IOMMU_FORMAT_KERNEL 0 #define IOMMU_FORMAT_PPC_XXX 2 #define IOMMU_FORMAT_[..] u32 format_flags; // Layout depends on format above __aligned_u64 user_page_directory; // Used if parent_ios_id != 0 };
Again 'type' as an overall API indicator should not exist, feature flags need to have clear narrow meanings.
This does both of David's suggestions at once. If quemu wants the 1G limited region it could specify max_iova_hint = 1G, if it wants the extend 64bit region with the hole it can give either the high base or a large max_iova_hint. format/format_flags allows a further device-specific escape if more specific customization is needed and is needed to specify user space page tables anyhow.
Quoted 5 lines
Quoted 2 lines
ioas works well here I think. Use
ioas_idto refer to the xarray index.What about when introducing pasid to this uAPI? Then use
ioas_idfor the xarray index
Yes, ioas_id should always be the xarray index.
PASID needs to be called out as PASID or as a generic "hw description" blob.
kvm's API to program the vPASID translation table should probably take in a (iommufd,ioas_id,device_id) tuple and extract the IOMMU side information using an in-kernel API. Userspace shouldn't have to shuttle it around.
I'm starting to feel like the struct approach for describing this uAPI might not scale well, but lets see..
Jason
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 81 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 10:09 PMQuoted 9 lines · Kevin
Quoted 9 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 5 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type
(
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2),Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify the
addr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at thispoint,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see nextpatch)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
for what formats can be specified when allocating an IOASID.
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide addressspace.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
This requirement doesn't sound PPC specific, as
addr_widthfor pcidevices
Quoted 1 line
can be also represented by a range [0, 2^addr_width-1]. This RFC
hasn't
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
David explained it here:
https://lore.kernel.org/kvm/YMrKksUeNW%2FPEGPM@yekko/
qeumu needs to be able to chooose if it gets the 32 bit range or 64 bit range.
So a 'range hint' will do the job
David also suggested this:
https://lore.kernel.org/kvm/YL6%2FbjHyuHJTn4Rd@yekko/
So I like this better:
struct iommu_ioasid_alloc { __u32 argsz; __u32 flags;#define
IOMMU_IOASID_ENFORCE_SNOOP(1 << 0)
#defineIOMMU_IOASID_HINT_BASE_IOVA(1 << 1)__aligned_u64 max_iova_hint; __aligned_u64 base_iova_hint; // Used only if
IOMMU_IOASID_HINT_BASE_IOVA// For creating nested page tables __u32 parent_ios_id; __u32 format; #define IOMMU_FORMAT_KERNEL 0 #define IOMMU_FORMAT_PPC_XXX 2 #define IOMMU_FORMAT_[..] u32 format_flags; // Layout depends on format above __aligned_u64 user_page_directory; // Used if parent_ios_id != 0 };
Quoted 2 lines
Again 'type' as an overall API indicator should not exist, feature flags need to have clear narrow meanings.
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Quoted 5 lines
This does both of David's suggestions at once. If quemu wants the 1G limited region it could specify
max_iova_hint= 1G, if it wants the extend 64bit region with the hole it can give either the high base or a largemax_iova_hint. format/format_flags allows a further
Dave's links didn't answer one puzzle from me. Does PPC needs accurate range information or be ok with a large range including holes (then let the kernel to figure out where the holes locate)?
Quoted 2 lines
device-specific escape if more specific customization is needed and is needed to specify user space page tables anyhow.
and I didn't understand the 2nd link. How does user-managed page table jump into this range claim problem? I'm getting confused...
Quoted 11 lines
Quoted 6 lines
Quoted 3 lines
ioas works well here I think. Use
ioas_idto refer to the xarray index.What about when introducing pasid to this uAPI? Then use
ioas_idfor the xarray indexYes,
ioas_idshould always be the xarray index.PASID needs to be called out as PASID or as a generic "hw description" blob.
ARM doesn't use PASID. So we need a generic blob, e.g. ioas_hwid?
and still we have both ioas_id (iommufd) and ioasid (ioasid.c) in the kernel. Do we want to clear this confusion? Or possibly it's fine because ioas_id is never used outside of iommufd and iommufd doesn't directly call ioasid_alloc() from ioasid.c?
Quoted 5 lines
kvm's API to program the vPASID translation table should probably take in a (iommufd,ioas_id,device_id) tuple and extract the IOMMU side information using an in-kernel API. Userspace shouldn't have to shuttle it around.
the vPASID info is carried in VFIO_DEVICE_ATTACH_IOASID uAPI. when kvm calls iommufd with above tuple, vPASID->pPASID is returned to kvm. So we still need a generic blob to represent vPASID in the uAPI.
Quoted 5 lines
I'm starting to feel like the struct approach for describing this uAPI might not scale well, but lets see..
Jason
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 5 lines · Kevin
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)
Creating a shared ios is something that should probably be a different command.
Quoted 2 lines
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Format should be
FORMAT_KERNEL/FORMAT_INTEL_PTE_V1/FORMAT_INTEL_PTE_V2/etc
Quoted 3 lines
Dave's links didn't answer one puzzle from me. Does PPC needs accurate range information or be ok with a large range including holes (then let the kernel to figure out where the holes locate)?
My impression was it only needed a way to select between the two different cases as they are exclusive. I'd see this API as being a hint and userspace should query the exact ranges to learn what was actually created.
Quoted 5 lines
Quoted 2 lines
device-specific escape if more specific customization is needed and is needed to specify user space page tables anyhow.
and I didn't understand the 2nd link. How does user-managed page table jump into this range claim problem? I'm getting confused...
PPC could also model it using a FORMAT_KERNEL_PPC_X, FORMAT_KERNEL_PPC_Y though it is less nice..
Quoted 6 lines
Quoted 4 lines
Yes,
ioas_idshould always be the xarray index.PASID needs to be called out as PASID or as a generic "hw description" blob.
ARM doesn't use PASID. So we need a generic blob, e.g.
ioas_hwid?
ARM *does* need PASID! PASID is the label of the DMA on the PCI bus, and it MUST be exposed in that format to be programmed into the PCI device itself.
All of this should be able to support a userspace, like DPDK, creating a PASID on its own without any special VFIO drivers.
- Open iommufd
- Attach the vfio device FD
- Request a PASID device id
- Create an ios against the pasid device id
- Query the ios for the PCI PASID #
- Program the HW to issue TLPs with the PASID
Quoted 4 lines
and still we have both
ioas_id(iommufd) and ioasid (ioasid.c) in the kernel. Do we want to clear this confusion? Or possibly it's fine becauseioas_idis never used outside of iommufd and iommufd doesn't directly callioasid_alloc()fromioasid.c?
As long as it is ioas_id and ioasid it is probably fine..
Quoted 9 lines
Quoted 4 lines
kvm's API to program the vPASID translation table should probably take in a (iommufd,ioas_id,device_id) tuple and extract the IOMMU side information using an in-kernel API. Userspace shouldn't have to shuttle it around.
the vPASID info is carried in
VFIO_DEVICE_ATTACH_IOASIDuAPI. when kvm calls iommufd with above tuple,vPASID->pPASIDis returned to kvm. So we still need a generic blob to represent vPASID in the uAPI.
I think you have to be clear about what the value is being used for. Is it an IOMMU page table handle or is it a PCI PASID value?
AFAICT I think it is the former in the Intel scheme as the "vPASID" is really about presenting a consistent IOMMU handle to the guest across migration, it is not the value that shows up on the PCI bus.
Jason
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 5 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:07 PMOn Thu, Sep 23, 2021 at 09:14:58AM +0000, Tian, Kevin wrote:
Quoted 8 lines
Quoted 5 lines
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)Creating a shared ios is something that should probably be a different command.
why? I didn't understand the criteria here...
Quoted 7 lines
Quoted 3 lines
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Format should be
FORMAT_KERNEL/FORMAT_INTEL_PTE_V1/FORMAT_INTEL_PTE_V2/etc
INTEL_PTE_V1/V2 are formats. Why is kernel-managed called a format?
Quoted 9 lines
Quoted 4 lines
Dave's links didn't answer one puzzle from me. Does PPC needs accurate range information or be ok with a large range including holes (then let the kernel to figure out where the holes locate)?
My impression was it only needed a way to select between the two different cases as they are exclusive. I'd see this API as being a hint and userspace should query the exact ranges to learn what was actually created.
yes, the user can query the permitted range using DEVICE_GET_INFO. But in the end if the user wants two separate regions, I'm afraid that the underlying iommu driver wants to know the exact info. iirc PPC has one global system address space shared by all devices. It is possible that the user may want to claim range-A and range-C, with range-B in-between but claimed by another user. Then simply using one hint range [A-lowend, C-highend] might not work.
Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
device-specific escape if more specific customization is needed and is needed to specify user space page tables anyhow.
and I didn't understand the 2nd link. How does user-managed page table jump into this range claim problem? I'm getting confused...
PPC could also model it using a
FORMAT_KERNEL_PPC_X,FORMAT_KERNEL_PPC_Y
though it is less nice..
yes PPC can use different format, but I didn't understand why it is related user-managed page table which further requires nesting. sound disconnected topics here...
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Yes,
ioas_idshould always be the xarray index.PASID needs to be called out as PASID or as a generic "hw description" blob.
ARM doesn't use PASID. So we need a generic blob, e.g.
ioas_hwid?ARM *does* need PASID! PASID is the label of the DMA on the PCI bus, and it MUST be exposed in that format to be programmed into the PCI device itself.
In the entire discussion in previous design RFC, I kept an impression that ARM-equivalent PASID is called SSID. If we can use PASID as a general term in iommufd context, definitely it's much better!
Quoted 10 lines
All of this should be able to support a userspace, like DPDK, creating a PASID on its own without any special VFIO drivers.
- Open iommufd
- Attach the vfio device FD
- Request a PASID device id
- Create an ios against the pasid device id
- Query the ios for the PCI PASID #
- Program the HW to issue TLPs with the PASID
this all makes me very confused, and completely different from what we agreed in previous v2 design proposal:
- open iommufd
- create an ioas
- attach vfio device to ioasid, with vPASID info
* vfio converts vPASID to pPASID and then call iommufd_device_attach_ioasid()
* the latter then installs ioas to the IOMMU with RID/PASID
Quoted 7 lines
Quoted 5 lines
and still we have both
ioas_id(iommufd) and ioasid (ioasid.c) in the kernel. Do we want to clear this confusion? Or possibly it's fine becauseioas_idis never used outside of iommufd and iommufd doesn't directly callioasid_alloc()fromioasid.c?As long as it is
ioas_idand ioasid it is probably fine..
let's align with others in a few hours.
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines
kvm's API to program the vPASID translation table should probably take in a (iommufd,ioas_id,device_id) tuple and extract the IOMMU side information using an in-kernel API. Userspace shouldn't have to shuttle it around.
the vPASID info is carried in
VFIO_DEVICE_ATTACH_IOASIDuAPI. when kvm calls iommufd with above tuple,vPASID->pPASIDis returned to kvm. So we still need a generic blob to represent vPASID in the uAPI.
Quoted 7 lines
I think you have to be clear about what the value is being used for. Is it an IOMMU page table handle or is it a PCI PASID value?
AFAICT I think it is the former in the Intel scheme as the "vPASID" is really about presenting a consistent IOMMU handle to the guest across migration, it is not the value that shows up on the PCI bus.
It's the former. But vfio driver needs to maintain vPASID->pPASID translation in the mediation path, since what guest programs is vPASID.
Thanks
Kevin
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 7 lines · Kevin
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:07 PMQuoted 2 lines · Kevin
currently the type is aimed to differentiate three usages:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)Creating a shared ios is something that should probably be a different command.
why? I didn't understand the criteria here...
I suspect the input args will be very different, no?
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Format should be
FORMAT_KERNEL/FORMAT_INTEL_PTE_V1/FORMAT_INTEL_PTE_V2/etc
INTEL_PTE_V1/V2 are formats. Why is kernel-managed called a format?
So long as we are using structs we need to have values then the field isn't being used. FORMAT_KERNEL is a reasonable value to have when we are not creating a userspace page table.
Alternatively a userspace page table could have a different API
Quoted 7 lines
yes, the user can query the permitted range using
DEVICE_GET_INFO. But in the end if the user wants two separate regions, I'm afraid that the underlying iommu driver wants to know the exact info. iirc PPC has one global system address space shared by all devices. It is possible that the user may want to claim range-A and range-C, with range-B in-between but claimed by another user. Then simply using one hint range [A-lowend, C-highend] might not work.
I don't know, that sounds strange.. In any event hint is a hint, it can be ignored, the only information the kernel needs to extract is low/high bank?
Quoted 3 lines
yes PPC can use different format, but I didn't understand why it is related user-managed page table which further requires nesting. sound disconnected topics here...
It is just a way to feed through more information if we get stuck someday.
Quoted 7 lines
Quoted 3 lines
ARM *does* need PASID! PASID is the label of the DMA on the PCI bus, and it MUST be exposed in that format to be programmed into the PCI device itself.
In the entire discussion in previous design RFC, I kept an impression that ARM-equivalent PASID is called SSID. If we can use PASID as a general term in iommufd context, definitely it's much better!
SSID is inside the chip and part of the IOMMU. PASID is part of the PCI spec.
iommufd should keep these things distinct.
If we are talking about a PCI TLP then the name to use is PASID.
Quoted 10 lines
Quoted 9 lines
All of this should be able to support a userspace, like DPDK, creating a PASID on its own without any special VFIO drivers.
- Open iommufd
- Attach the vfio device FD
- Request a PASID device id
- Create an ios against the pasid device id
- Query the ios for the PCI PASID #
- Program the HW to issue TLPs with the PASID
Quoted 8 lines
this all makes me very confused, and completely different from what we agreed in previous v2 design proposal:
- open iommufd
- create an ioas
- attach vfio device to ioasid, with vPASID info
* vfio converts vPASID to pPASID and then calliommufd_device_attach_ioasid()
* the latter then installs ioas to the IOMMU with RID/PASID
This was your flow for mdev's, I've always been talking about wanting to see this supported for all use cases, including physical PCI devices w/ PASID support.
A normal vfio_pci userspace should be able to create PASIDs unrelated to the mdev stuff.
Quoted 6 lines
Quoted 3 lines
AFAICT I think it is the former in the Intel scheme as the "vPASID" is really about presenting a consistent IOMMU handle to the guest across migration, it is not the value that shows up on the PCI bus.
It's the former. But vfio driver needs to maintain
vPASID->pPASIDtranslation in the mediation path, since what guest programs is vPASID.
The pPASID definately is a PASID as it goes out on the PCIe wire
Suggest you come up with a more general name for vPASID?
Jason
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 15 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:31 PMQuoted 11 lines · Kevin
Quoted 11 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:07 PMQuoted 5 lines · Kevin
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Creating a shared ios is something that should probably be a different command.
why? I didn't understand the criteria here...
I suspect the input args will be very different, no?
yes, but can't the structure be extended to incorporate it?
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Format should be
FORMAT_KERNEL/FORMAT_INTEL_PTE_V1/FORMAT_INTEL_PTE_V2/etc
Quoted 7 lines
Quoted 1 line
INTEL_PTE_V1/V2 are formats. Why is kernel-managed called a format?
So long as we are using structs we need to have values then the field isn't being used.
FORMAT_KERNELis a reasonable value to have when we are not creating a userspace page table.Alternatively a userspace page table could have a different API
I don't know. Your comments really confused me on what's the right way to design the uAPI. If you still remember, the original v1 proposal introduced different uAPIs for kernel/user-managed cases. Then you recommended to consolidate everything related to ioas in one allocation command.
Can you help articulate the criteria first?
Quoted 9 lines
Quoted 8 lines
yes, the user can query the permitted range using
DEVICE_GET_INFO. But in the end if the user wants two separate regions, I'm afraid that the underlying iommu driver wants to know the exact info. iirc PPC has one global system address space shared by all devices. It is possible that the user may want to claim range-A and range-C, with range-B in-between but claimed by another user. Then simply using one hint range [A-lowend, C-highend] might not work.
Quoted 3 lines
I don't know, that sounds strange.. In any event hint is a hint, it can be ignored, the only information the kernel needs to extract is low/high bank?
iirc Dave said that the user needs to claim a range explicitly. 'claim' sounds not a hint to me. Possibly it's time for Dave to chime in.
Quoted 7 lines
Quoted 4 lines
yes PPC can use different format, but I didn't understand why it is related user-managed page table which further requires nesting. sound disconnected topics here...
It is just a way to feed through more information if we get stuck someday.
You mean that we should define uAPI for all future possible extensions now to minimize the frequency of changing it?
Quoted 9 lines
Quoted 8 lines
Quoted 4 lines
ARM *does* need PASID! PASID is the label of the DMA on the PCI bus, and it MUST be exposed in that format to be programmed into the PCI device itself.
In the entire discussion in previous design RFC, I kept an impression that ARM-equivalent PASID is called SSID. If we can use PASID as a general term in iommufd context, definitely it's much better!
Quoted 6 lines
SSID is inside the chip and part of the IOMMU. PASID is part of the PCI spec.
iommufd should keep these things distinct.
If we are talking about a PCI TLP then the name to use is PASID.
If Jean doesn't object...
Quoted 18 lines
Quoted 11 lines
Quoted 10 lines
All of this should be able to support a userspace, like DPDK, creating a PASID on its own without any special VFIO drivers.
- Open iommufd
- Attach the vfio device FD
- Request a PASID device id
- Create an ios against the pasid device id
- Query the ios for the PCI PASID #
- Program the HW to issue TLPs with the PASIDQuoted 7 lines
this all makes me very confused, and completely different from what we agreed in previous v2 design proposal:
- open iommufd
- create an ioas
- attach vfio device to ioasid, with vPASID info
* vfio converts vPASID to pPASID and then call
Quoted 6 lines
iommufd_device_attach_ioasid()Quoted 1 line
* the latter then installs ioas to the IOMMU with RID/PASID
This was your flow for mdev's, I've always been talking about wanting to see this supported for all use cases, including physical PCI devices w/ PASID support.
this is not a flow for mdev. It's also required for pdev on Intel platform, because the pasid table is in HPA space thus must be managed by host kernel. Even no translation we still need the user to provide the pasid info.
Quoted 8 lines
A normal
vfio_pciuserspace should be able to create PASIDs unrelated to the mdev stuff.Quoted 4 lines
Quoted 3 lines
AFAICT I think it is the former in the Intel scheme as the "vPASID" is really about presenting a consistent IOMMU handle to the guest across migration, it is not the value that shows up on the PCI bus.
Quoted 7 lines
Quoted 2 lines
It's the former. But vfio driver needs to maintain
vPASID->pPASIDtranslation in the mediation path, since what guest programs is vPASID.The pPASID definately is a PASID as it goes out on the PCIe wire
Suggest you come up with a more general name for vPASID?
as explained earlier, on Intel platform the user always needs to provide a PASID in the attaching call. whether it's directly used (for pdev) or translated (for mdev) is the underlying driver thing. From kernel p.o.v, since this PASID is provided by the user, it's fine to call it vPASID in the uAPI.
Thanks
Kevin
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 15 lines · Kevin
Quoted 15 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:31 PMQuoted 11 lines · Kevin
Quoted 11 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:07 PMQuoted 5 lines · Kevin
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Creating a shared ios is something that should probably be a different command.
why? I didn't understand the criteria here...
I suspect the input args will be very different, no?
yes, but can't the structure be extended to incorporate it?
You need to be thoughtful, giant structures with endless combinations of optional fields turn out very hard. I haven't even seen what args this shared thing will need, but I'm guessing it is almost none, so maybe a new call is OK?
If it is literally just 'give me an ioas for current mm' then it has no args or complexity at all.
Quoted 13 lines
Quoted 13 lines
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Format should be
FORMAT_KERNEL/FORMAT_INTEL_PTE_V1/FORMAT_INTEL_PTE_V2/etc
INTEL_PTE_V1/V2 are formats. Why is kernel-managed called a format?
So long as we are using structs we need to have values then the field isn't being used.
FORMAT_KERNELis a reasonable value to have when we are not creating a userspace page table.
Quoted 7 lines
Quoted 1 line
Alternatively a userspace page table could have a different API
I don't know. Your comments really confused me on what's the right way to design the uAPI. If you still remember, the original v1 proposal introduced different uAPIs for
kernel/user-managedcases. Then you recommended to consolidate everything related to ioas in one allocation command.
This is because you had almost completely duplicated the input args between the two calls.
If it turns out they have very different args, then they should have different calls.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
- open iommufd
- create an ioas
- attach vfio device to ioasid, with vPASID info
* vfio converts vPASID to pPASID and then call
iommufd_device_attach_ioasid()Quoted 1 line
* the latter then installs ioas to the IOMMU with RID/PASID
Quoted 7 lines
Quoted 3 lines
This was your flow for mdev's, I've always been talking about wanting to see this supported for all use cases, including physical PCI devices w/ PASID support.
this is not a flow for mdev. It's also required for pdev on Intel platform, because the pasid table is in HPA space thus must be managed by host kernel. Even no translation we still need the user to provide the pasid info.
There should be no mandatory vPASID stuff in most of these flows, that is just a special thing ENQCMD virtualization needs. If userspace isn't doing ENQCMD virtualization it shouldn't need to touch this stuff.
Quoted 5 lines
as explained earlier, on Intel platform the user always needs to provide a PASID in the attaching call. whether it's directly used (for pdev) or translated (for mdev) is the underlying driver thing. From kernel p.o.v, since this PASID is provided by the user, it's fine to call it vPASID in the uAPI.
I've always disagreed with this. There should be an option for the kernel to pick an appropriate PASID for portability to other IOMMUs and simplicity of the interface.
You need to keep it clear what is in the minimum basic path and what is needed for special cases, like ENQCMD virtualization.
Not every user of iommufd is doing virtualization.
Jason
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 24 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 9:02 PMQuoted 20 lines · Kevin
Quoted 20 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:31 PMQuoted 15 lines · Kevin
Quoted 5 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:07 PMOn Thu, Sep 23, 2021 at 09:14:58AM +0000, Tian, Kevin wrote:
Quoted 8 lines
Quoted 5 lines
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)Creating a shared ios is something that should probably be a different command.
why? I didn't understand the criteria here...
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
I suspect the input args will be very different, no?
yes, but can't the structure be extended to incorporate it?
You need to be thoughtful, giant structures with endless combinations of optional fields turn out very hard. I haven't even seen what args this shared thing will need, but I'm guessing it is almost none, so maybe a new call is OK?
To judge this looks we may have to do some practice on this front e.g. coming up an example structure for future intended usages and then see whether one structure can fit?
Quoted 3 lines
If it is literally just 'give me an ioas for current mm' then it has no args or complexity at all.
for mm, yes, should be simple. for ept it might be more complex e.g. requiring a handle in kvm and some other format info to match ept page table.
Quoted 40 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Format should be
FORMAT_KERNEL/FORMAT_INTEL_PTE_V1/FORMAT_INTEL_PTE_V2/etc
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
INTEL_PTE_V1/V2 are formats. Why is kernel-managed called a format?
So long as we are using structs we need to have values then the field isn't being used.
FORMAT_KERNELis a reasonable value to have when we are not creating a userspace page table.Quoted 7 lines
Quoted 1 line
Alternatively a userspace page table could have a different API
I don't know. Your comments really confused me on what's the right way to design the uAPI. If you still remember, the original v1 proposal introduced different uAPIs for
kernel/user-managedcases. Then you recommended to consolidate everything related to ioas in one allocation command.This is because you had almost completely duplicated the input args between the two calls.
If it turns out they have very different args, then they should have different calls.
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
- open iommufd
- create an ioas
- attach vfio device to ioasid, with vPASID info
* vfio converts vPASID to pPASID and then call
iommufd_device_attach_ioasid()Quoted 1 line
* the latter then installs ioas to the IOMMU with RID/PASID
This was your flow for mdev's, I've always been talking about wanting to see this supported for all use cases, including physical PCI devices w/ PASID support.
Quoted 8 lines
Quoted 3 lines
this is not a flow for mdev. It's also required for pdev on Intel platform, because the pasid table is in HPA space thus must be managed by host kernel. Even no translation we still need the user to provide the pasid info.
There should be no mandatory vPASID stuff in most of these flows, that is just a special thing ENQCMD virtualization needs. If userspace isn't doing ENQCMD virtualization it shouldn't need to touch this stuff.
No. for one, we also support SVA w/o using ENQCMD. For two, the key is that the PASID table cannot be delegated to the userspace like ARM or AMD. This implies that for any pasid that the userspace wants to enable, it must be configured via the kernel.
Quoted 7 lines
Quoted 6 lines
as explained earlier, on Intel platform the user always needs to provide a PASID in the attaching call. whether it's directly used (for pdev) or translated (for mdev) is the underlying driver thing. From kernel p.o.v, since this PASID is provided by the user, it's fine to call it vPASID in the uAPI.
Quoted 9 lines
I've always disagreed with this. There should be an option for the kernel to pick an appropriate PASID for portability to other IOMMUs and simplicity of the interface.
You need to keep it clear what is in the minimum basic path and what is needed for special cases, like ENQCMD virtualization.
Not every user of iommufd is doing virtualization.
just for a short summary of PASID model from previous design RFC:
for arm/amd:
- pasid space delegated to userspace
- pasid table delegated to userspace
- just one call to bind pasid_table() then pasids are fully managed by user
for intel:
- pasid table is always managed by kernel
- for pdev,
- pasid space is delegated to userspace
- attach_ioasid(dev, ioasid, pasid) so the kernel can setup the pasid entry
- for mdev,
- pasid space is managed by userspace
- attach_ioasid(dev, ioasid, vpasid). vfio converts vpasid to ppasid. iommufd setups the ppasid entry
- additional a contract to kvm for setup CPU pasid translation if enqcmd is used
- to unify pdev/mdev, just always call it vpasid in attach_ioasid(). let underlying driver to figure out whether vpasid should be translated.
Thanks
Kevin
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 9 lines · Kevin
Quoted 8 lines
Quoted 3 lines
this is not a flow for mdev. It's also required for pdev on Intel platform, because the pasid table is in HPA space thus must be managed by host kernel. Even no translation we still need the user to provide the pasid info.
There should be no mandatory vPASID stuff in most of these flows, that is just a special thing ENQCMD virtualization needs. If userspace isn't doing ENQCMD virtualization it shouldn't need to touch this stuff.
Quoted 4 lines
No. for one, we also support SVA w/o using ENQCMD. For two, the key is that the PASID table cannot be delegated to the userspace like ARM or AMD. This implies that for any pasid that the userspace wants to enable, it must be configured via the kernel.
Yes, configured through the kernel, but the simplified flow should have the kernel handle everything and just emit a PASID for userspace to use.
Quoted 7 lines
just for a short summary of PASID model from previous design RFC:
for arm/amd:
- pasid space delegated to userspace
- pasid table delegated to userspace
- just one call to bindpasid_table()then pasids are fully managed by user
Quoted 10 lines
for intel:
- pasid table is always managed by kernel
- for pdev,
- pasid space is delegated to userspace
-attach_ioasid(dev, ioasid, pasid)so the kernel can setup the pasid entry
- for mdev,
- pasid space is managed by userspace
-attach_ioasid(dev, ioasid, vpasid). vfio converts vpasid to ppasid. iommufd setups the ppasid entry
- additional a contract to kvm for setup CPU pasid translation if enqcmd is used
- to unify pdev/mdev, just always call it vpasid inattach_ioasid(). let underlying driver to figure out whether vpasid should be translated.
All cases should support a kernel owned ioas associated with a PASID. This is the universal basic API that all PASID supporting IOMMUs need to implement.
I should not need to write generic users space that has to know how to setup architecture specific nested userspace page tables just to use PASID!
All of the above is qemu accelerated vIOMMU stuff. It is a good idea to keep the two areas seperate as it greatly informs what is general code and what is HW specific code.
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 48 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 9:31 PMQuoted 3 lines · Kevin
Quoted 3 lines
Quoted 3 lines
this is not a flow for mdev. It's also required for pdev on Intel platform, because the pasid table is in HPA space thus must be managed by host kernel. Even no translation we still need the user to provide the pasid
info.
Quoted 10 lines
Quoted 5 lines
There should be no mandatory vPASID stuff in most of these flows, that is just a special thing ENQCMD virtualization needs. If userspace isn't doing ENQCMD virtualization it shouldn't need to touch this stuff.
No. for one, we also support SVA w/o using ENQCMD. For two, the key is that the PASID table cannot be delegated to the userspace like ARM or AMD. This implies that for any pasid that the userspace wants to enable, it must be configured via the kernel.
Yes, configured through the kernel, but the simplified flow should have the kernel handle everything and just emit a PASID for userspace to use.
Quoted 6 lines
just for a short summary of PASID model from previous design RFC:
for arm/amd:
- pasid space delegated to userspace
- pasid table delegated to userspace
- just one call to bindpasid_table()then pasids are fully managed byuser
Quoted 6 lines
for intel:
- pasid table is always managed by kernel
- for pdev,
- pasid space is delegated to userspace
-attach_ioasid(dev, ioasid, pasid)so the kernel can setup thepasid entry
Quoted 3 lines
- for mdev,
- pasid space is managed by userspace
-attach_ioasid(dev, ioasid, vpasid). vfio converts vpasid toppasid. iommufd setups the ppasid entry
Quoted 1 line
- additional a contract to kvm for setup CPU pasid translation
if enqcmd is used
Quoted 1 line
- to unify pdev/mdev, just always call it vpasid in
attach_ioasid(). letunderlying driver to figure out whether vpasid should be translated.
Quoted 7 lines
All cases should support a kernel owned ioas associated with a PASID. This is the universal basic API that all PASID supporting IOMMUs need to implement.
I should not need to write generic users space that has to know how to setup architecture specific nested userspace page tables just to use PASID!
ah, got you! I have to admit that my previous thoughts are all from VM p.o.v, with true userspace application ignored...
Quoted 5 lines
All of the above is qemu accelerated vIOMMU stuff. It is a good idea to keep the two areas seperate as it greatly informs what is general code and what is HW specific code.
Agree. will think more along this direction. possibly this discussion deviated a lot from what this skeleton series provide. We still have plenty of time to figure it out when starting the pasid support. For now at least the minimal output is that PASID might be a good candidate to be used in iommufd. 😊
Thanks
Kevin
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Tian, KevinQuoted 36 lines · Kevin
Quoted 5 lines
From: Jason Gunthorpe <[email protected]>
Sent: Thursday, September 23, 2021 8:07 PMOn Thu, Sep 23, 2021 at 09:14:58AM +0000, Tian, Kevin wrote:
Quoted 8 lines
Quoted 5 lines
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)Creating a shared ios is something that should probably be a different command.
why? I didn't understand the criteria here...
Quoted 7 lines
Quoted 3 lines
we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
Format should be
FORMAT_KERNEL/FORMAT_INTEL_PTE_V1/FORMAT_INTEL_PTE_V2/etc
INTEL_PTE_V1/V2 are formats. Why is kernel-managed called a format?
Quoted 9 lines
Quoted 4 lines
Dave's links didn't answer one puzzle from me. Does PPC needs accurate range information or be ok with a large range including holes (then let the kernel to figure out where the holes locate)?
My impression was it only needed a way to select between the two different cases as they are exclusive. I'd see this API as being a hint and userspace should query the exact ranges to learn what was actually created.
Quoted 4 lines
yes, the user can query the permitted range using
DEVICE_GET_INFO. But in the end if the user wants two separate regions, I'm afraid that the underlying iommu driver wants to know the exact info. iirc PPC has one global system address space shared by all devices.
I think certain POWER models do this, yes, there's *protection* between DMAs from different devices, but you can't translate the same address to different places for different devices. I *think* that's a firmware/hypervisor convention rather than a hardware limitation, but I'm not entirely sure. We don't do things this way when emulating the POWER vIOMMU in POWER, but PowerVM might and we still have to deal with that when running as a POWERVM guest.
Quoted 91 lines
It is possible
that the user may want to claim range-A and range-C, with range-B in-between but claimed by another user. Then simply using one hint range [A-lowend, C-highend] might not work.Quoted 10 lines
Quoted 6 lines
Quoted 3 lines
device-specific escape if more specific customization is needed and is needed to specify user space page tables anyhow.
and I didn't understand the 2nd link. How does user-managed page table jump into this range claim problem? I'm getting confused...
PPC could also model it using a
FORMAT_KERNEL_PPC_X,FORMAT_KERNEL_PPC_Y
though it is less nice..yes PPC can use different format, but I didn't understand why it is related user-managed page table which further requires nesting. sound disconnected topics here...
Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
Yes,
ioas_idshould always be the xarray index.PASID needs to be called out as PASID or as a generic "hw description" blob.
ARM doesn't use PASID. So we need a generic blob, e.g.
ioas_hwid?ARM *does* need PASID! PASID is the label of the DMA on the PCI bus, and it MUST be exposed in that format to be programmed into the PCI device itself.
In the entire discussion in previous design RFC, I kept an impression that ARM-equivalent PASID is called SSID. If we can use PASID as a general term in iommufd context, definitely it's much better!
Quoted 10 lines
All of this should be able to support a userspace, like DPDK, creating a PASID on its own without any special VFIO drivers.
- Open iommufd
- Attach the vfio device FD
- Request a PASID device id
- Create an ios against the pasid device id
- Query the ios for the PCI PASID #
- Program the HW to issue TLPs with the PASIDthis all makes me very confused, and completely different from what we agreed in previous v2 design proposal:
- open iommufd
- create an ioas
- attach vfio device to ioasid, with vPASID info
* vfio converts vPASID to pPASID and then calliommufd_device_attach_ioasid()
* the latter then installs ioas to the IOMMU with RID/PASIDQuoted 7 lines
Quoted 5 lines
and still we have both
ioas_id(iommufd) and ioasid (ioasid.c) in the kernel. Do we want to clear this confusion? Or possibly it's fine becauseioas_idis never used outside of iommufd and iommufd doesn't directly callioasid_alloc()fromioasid.c?As long as it is
ioas_idand ioasid it is probably fine..let's align with others in a few hours.
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines
kvm's API to program the vPASID translation table should probably take in a (iommufd,ioas_id,device_id) tuple and extract the IOMMU side information using an in-kernel API. Userspace shouldn't have to shuttle it around.
the vPASID info is carried in
VFIO_DEVICE_ATTACH_IOASIDuAPI. when kvm calls iommufd with above tuple,vPASID->pPASIDis returned to kvm. So we still need a generic blob to represent vPASID in the uAPI.Quoted 7 lines
I think you have to be clear about what the value is being used for. Is it an IOMMU page table handle or is it a PCI PASID value?
AFAICT I think it is the former in the Intel scheme as the "vPASID" is really about presenting a consistent IOMMU handle to the guest across migration, it is not the value that shows up on the PCI bus.
It's the former. But vfio driver needs to maintain
vPASID->pPASIDtranslation in the mediation path, since what guest programs is vPASID.Thanks
Kevin
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Tian, KevinQuoted 84 lines · Kevin
Quoted 81 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 10:09 PMQuoted 9 lines · Kevin
Quoted 9 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 5 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type
(
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2),Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify the
addr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at thispoint,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see nextpatch)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
for what formats can be specified when allocating an IOASID.
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide addressspace.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
This requirement doesn't sound PPC specific, as
addr_widthfor pcidevices
Quoted 1 line
can be also represented by a range [0, 2^addr_width-1]. This RFC
hasn't
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
David explained it here:
https://lore.kernel.org/kvm/YMrKksUeNW%2FPEGPM@yekko/
qeumu needs to be able to chooose if it gets the 32 bit range or 64 bit range.
So a 'range hint' will do the job
David also suggested this:
https://lore.kernel.org/kvm/YL6%2FbjHyuHJTn4Rd@yekko/
So I like this better:
struct iommu_ioasid_alloc { __u32 argsz; __u32 flags;#define
IOMMU_IOASID_ENFORCE_SNOOP(1 << 0)
#defineIOMMU_IOASID_HINT_BASE_IOVA(1 << 1)__aligned_u64 max_iova_hint; __aligned_u64 base_iova_hint; // Used only if
IOMMU_IOASID_HINT_BASE_IOVA// For creating nested page tables __u32 parent_ios_id; __u32 format; #define IOMMU_FORMAT_KERNEL 0 #define IOMMU_FORMAT_PPC_XXX 2 #define IOMMU_FORMAT_[..] u32 format_flags; // Layout depends on format above __aligned_u64 user_page_directory; // Used if parent_ios_id != 0 };Quoted 2 lines
Again 'type' as an overall API indicator should not exist, feature flags need to have clear narrow meanings.
Quoted 8 lines
currently the type is aimed to differentiate three usages:
- kernel-managed I/O page table
- user-managed I/O page table
- shared I/O page table (e.g. with mm, or ept)we can remove 'type', but is FORMAT_KENREL/USER/SHARED a good indicator? their difference is not about format.
To me "format" indicates how the IO translation information is encoded. We potentially have two different encodings: from userspace to the kernel and from the kernel to the hardware. But since this is the userspace API, it's only the userspace to kernel one that matters here.
In that sense, KERNEL, is a "format": we encode the translation information as a series of IOMAP operations to the kernel, rather than as an in-memory structure.
Quoted 8 lines
Quoted 4 lines
This does both of David's suggestions at once. If quemu wants the 1G limited region it could specify
max_iova_hint= 1G, if it wants the extend 64bit region with the hole it can give either the high base or a largemax_iova_hint. format/format_flags allows a furtherDave's links didn't answer one puzzle from me. Does PPC needs accurate range information or be ok with a large range including holes (then let the kernel to figure out where the holes locate)?
I need more specifics to answer that. Are you talking from a userspace PoV, a guest kernel's or the host kernel's? In general I think requiring userspace to locate and work aronud holes is a bad idea. If userspace requests a range, it should get *all* of that range.
The ppc case is further complicated because there are multiple ranges and each range could have separate IO page tables. In practice non-kernel managed IO pagetables are likely to be hard on ppc (or at least rely on firmware/hypervisor interfaces which don't exist yet, AFAIK). But even then, the underlying hardware page table format can affect the minimum pagesize of each range, which could be different.
How all of this interacts with PASIDs I really haven't figured out.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Jason GunthorpeQuoted 31 lines · Jason Gunthorpe
Quoted 30 lines · Kevin
Quoted 30 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pcidevices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
David explained it here:
Apparently not well enough. I've attempted again in this thread.
Quoted 2 lines
qeumu needs to be able to chooose if it gets the 32 bit range or 64 bit range.
No. qemu needs to supply *both* the 32-bit and 64-bit range to its guest, and therefore needs to request both from the host.
Or rather, it *might* need to supply both. It will supply just the 32-bit range by default, but the guest can request the 64-bit range and/or remove and resize the 32-bit range via hypercall interfaces. Vaguely recent Linux guests certainly will request the 64-bit range in addition to the default 32-bit range.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 41 lines · On Fri, Oct 01, 2021 at 04:19:22PM +1000, [email protected] wrote:
Quoted 31 lines · Jason Gunthorpe
Quoted 30 lines · Kevin
Quoted 30 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pcidevices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
David explained it here:
Quoted 7 lines
Apparently not well enough. I've attempted again in this thread.
Quoted 2 lines
qeumu needs to be able to chooose if it gets the 32 bit range or 64 bit range.
No. qemu needs to supply *both* the 32-bit and 64-bit range to its guest, and therefore needs to request both from the host.
As I understood your remarks each IOAS can only be one of the formats as they have a different PTE layout. So here I ment that qmeu needs to be able to pick *for each IOAS* which of the two formats it is.
Quoted 5 lines
Or rather, it *might* need to supply both. It will supply just the 32-bit range by default, but the guest can request the 64-bit range and/or remove and resize the 32-bit range via hypercall interfaces. Vaguely recent Linux guests certainly will request the 64-bit range in addition to the default 32-bit range.
And this would result in two different IOAS objects
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 47 lines · Jason Gunthorpe
Quoted 46 lines · On Fri, Oct 01, 2021 at 04:19:22PM +1000, [email protected] wrote:
Quoted 31 lines · Jason Gunthorpe
Quoted 30 lines · Kevin
Quoted 30 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pcidevices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
David explained it here:
Apparently not well enough. I've attempted again in this thread.
Quoted 2 lines
qeumu needs to be able to chooose if it gets the 32 bit range or 64 bit range.
Quoted 6 lines
Quoted 2 lines
No. qemu needs to supply *both* the 32-bit and 64-bit range to its guest, and therefore needs to request both from the host.
As I understood your remarks each IOAS can only be one of the formats as they have a different PTE layout. So here I ment that qmeu needs to be able to pick *for each IOAS* which of the two formats it is.
No. Both windows are in the same IOAS. A device could do DMA simultaneously to both windows. More realstically a 64-bit DMA capable and a non-64-bit DMA capable device could be in the same group and be doing DMAs to different windows simultaneously.
Quoted 7 lines
Quoted 5 lines
Or rather, it *might* need to supply both. It will supply just the 32-bit range by default, but the guest can request the 64-bit range and/or remove and resize the 32-bit range via hypercall interfaces. Vaguely recent Linux guests certainly will request the 64-bit range in addition to the default 32-bit range.
And this would result in two different IOAS objects
There might be two different IOAS objects for setup, but at some point they need to be combined into one IOAS to which the device is actually attached.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 9 lines · On Sat, Oct 02, 2021 at 02:21:38PM +1000, [email protected] wrote:
Quoted 6 lines
Quoted 2 lines
No. qemu needs to supply *both* the 32-bit and 64-bit range to its guest, and therefore needs to request both from the host.
As I understood your remarks each IOAS can only be one of the formats as they have a different PTE layout. So here I ment that qmeu needs to be able to pick *for each IOAS* which of the two formats it is.
No. Both windows are in the same IOAS. A device could do DMA simultaneously to both windows.
Sure, but that doesn't force us to model it as one IOAS in the iommufd. A while back you were talking about using nesting and 3 IOAS's, right?
1, 2 or 3 IOAS's seems like a decision we can make.
PASID support will already require that a device can be multi-bound to many IOAS's, couldn't PPC do the same with the windows?
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 9 lines · Jason Gunthorpe
Quoted 7 lines · On Sat, Oct 02, 2021 at 02:21:38PM +1000, [email protected] wrote:
Quoted 6 lines
Quoted 2 lines
No. qemu needs to supply *both* the 32-bit and 64-bit range to its guest, and therefore needs to request both from the host.
As I understood your remarks each IOAS can only be one of the formats as they have a different PTE layout. So here I ment that qmeu needs to be able to pick *for each IOAS* which of the two formats it is.
Quoted 8 lines
Quoted 2 lines
No. Both windows are in the same IOAS. A device could do DMA simultaneously to both windows.
Sure, but that doesn't force us to model it as one IOAS in the iommufd. A while back you were talking about using nesting and 3 IOAS's, right?
1, 2 or 3 IOAS's seems like a decision we can make.
Well, up to a point. We can decide how such a thing should be constructed. However at some point there needs to exist an IOAS in which both windows are mapped, whether it's directly or indirectly. That's what the device will be attached to.
Quoted 2 lines
PASID support will already require that a device can be multi-bound to many IOAS's, couldn't PPC do the same with the windows?
I don't see how that would make sense. The device has no awareness of multiple windows the way it does of PASIDs. It just sends transactions over the bus with the IOVAs it's told. If those IOVAs lie within one of the windows, the IOMMU picks them up and translates them. If they don't, it doesn't.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 8 lines · On Mon, Oct 11, 2021 at 04:37:38PM +1100, [email protected] wrote:
Quoted 2 lines
PASID support will already require that a device can be multi-bound to many IOAS's, couldn't PPC do the same with the windows?
I don't see how that would make sense. The device has no awareness of multiple windows the way it does of PASIDs. It just sends transactions over the bus with the IOVAs it's told. If those IOVAs lie within one of the windows, the IOMMU picks them up and translates them. If they don't, it doesn't.
To my mind that address centric routing is awareness.
If the HW can attach multiple non-overlapping IOAS's to the same device then the HW is routing to the correct IOAS by using the address bits. This is not much different from the prior discussion we had where we were thinking of the PASID as an 80 bit address
The fact the PPC HW actually has multiple page table roots and those roots even have different page tables layouts while still connected to the same device suggests this is not even an unnatural modelling approach...
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 11 lines · Jason Gunthorpe
Quoted 8 lines · On Mon, Oct 11, 2021 at 04:37:38PM +1100, [email protected] wrote:
Quoted 2 lines
PASID support will already require that a device can be multi-bound to many IOAS's, couldn't PPC do the same with the windows?
I don't see how that would make sense. The device has no awareness of multiple windows the way it does of PASIDs. It just sends transactions over the bus with the IOVAs it's told. If those IOVAs lie within one of the windows, the IOMMU picks them up and translates them. If they don't, it doesn't.
To my mind that address centric routing is awareness.
I don't really understand that position. A PASID capable device has to be built to be PASID capable, and will generally have registers into which you store PASIDs to use.
Any 64-bit DMA capable device can use the POWER IOMMU just fine - it's up to the driver to program it with addresses that will be translated (and in Linux the driver will get those from the DMA subsystem).
Quoted 4 lines
If the HW can attach multiple non-overlapping IOAS's to the same device then the HW is routing to the correct IOAS by using the address bits. This is not much different from the prior discussion we had where we were thinking of the PASID as an 80 bit address
Ah... that might be a workable approach. And it even helps me get my head around multiple attachment which I was struggling with before.
So, the rule would be that you can attach multiple IOASes to a device, as long as none of them overlap. The non-overlapping could be because each IOAS covers a disjoint address range, or it could be because there's some attached information - such as a PASID - to disambiguate.
What remains a question is where the disambiguating information comes from in each case: does it come from properties of the IOAS, propertues of the device, or from extra parameters supplied at attach time. IIUC, the current draft suggests it always comes at attach time for the PASID information. Obviously the more consistency we can have here the better.
I can also see an additional problem in implementation, once we start looking at hot-adding devices to existing address spaces. Suppose our software (maybe qemu) wants to set up a single DMA view for a bunch of devices, that has such a split window. It can set up IOASes easily enough for the two windows, then it needs to attach them. Presumbly, it attaches them one at a time, which means that each device (or group) goes through an interim state where it's attached to one, but not the other. That can probably be achieved by using an extra IOMMU domain (or the local equivalent) in the hardware for that interim state. However it means we have to repeatedly create and destroy that extra domain for each device after the first we add, rather than simply adding each device to the domain which has both windows.
[I think this doesn't arise on POWER when running under PowerVM. That
has no concept like IOMMU domains, and instead the mapping is always done per "partitionable endpoint" (PE), essentially a group. That means it's just a question of whether we mirror mappings on both windows into a given PE or just those from one IOAS. It's not an unreasonable extension/combination of existing hardware quirks to consider, though]
Quoted 8 lines
The fact the PPC HW actually has multiple page table roots and those roots even have different page tables layouts while still connected to the same device suggests this is not even an unnatural modelling approach...
Jason
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 5 lines · On Thu, Oct 14, 2021 at 03:33:21PM +1100, [email protected] wrote:
Quoted 4 lines
If the HW can attach multiple non-overlapping IOAS's to the same device then the HW is routing to the correct IOAS by using the address bits. This is not much different from the prior discussion we had where we were thinking of the PASID as an 80 bit address
Quoted 7 lines
Ah... that might be a workable approach. And it even helps me get my head around multiple attachment which I was struggling with before.
So, the rule would be that you can attach multiple IOASes to a device, as long as none of them overlap. The non-overlapping could be because each IOAS covers a disjoint address range, or it could be because there's some attached information - such as a PASID - to disambiguate.
Right exactly - it is very parallel to PASID
And obviously HW support is required to have multiple page table pointers per RID - which sounds like PPC does (high/low pointer?)
Quoted 6 lines
What remains a question is where the disambiguating information comes from in each case: does it come from properties of the IOAS, propertues of the device, or from extra parameters supplied at attach time. IIUC, the current draft suggests it always comes at attach time for the PASID information. Obviously the more consistency we can have here the better.
From a generic view point I'd say all are fair game. It is up to the IOMMU driver to take the requested set of IOAS's, the "at attachment" information (like PASID) and decide what to do, or fail.
Quoted 2 lines
I can also see an additional problem in implementation, once we start looking at hot-adding devices to existing address spaces.
I won't pretend to guess how to implement this :) Just from a modeling perspective is something that works logically. If the kernel implementation is too hard then PPC should do one of the other ideas.
Personally I'd probably try for a nice multi-domain attachment model like PASID and not try to create/destroy domains.
As I said in my last email I think it is up to each IOMMU HW driver to make these decisions, the iommufd framework just provides a standardized API toward the attaching driver that the IOMMU HW must fit into.
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 15 lines · Jason Gunthorpe
Quoted 5 lines · On Thu, Oct 14, 2021 at 03:33:21PM +1100, [email protected] wrote:
Quoted 4 lines
If the HW can attach multiple non-overlapping IOAS's to the same device then the HW is routing to the correct IOAS by using the address bits. This is not much different from the prior discussion we had where we were thinking of the PASID as an 80 bit address
Quoted 7 lines
Ah... that might be a workable approach. And it even helps me get my head around multiple attachment which I was struggling with before.
So, the rule would be that you can attach multiple IOASes to a device, as long as none of them overlap. The non-overlapping could be because each IOAS covers a disjoint address range, or it could be because there's some attached information - such as a PASID - to disambiguate.
Quoted 4 lines
Right exactly - it is very parallel to PASID
And obviously HW support is required to have multiple page table pointers per RID - which sounds like PPC does (high/low pointer?)
Hardware support is require *in the IOMMU*. Nothing (beyond regular 64-bit DMA support) is required in the endpoint devices. That's not true of PASID.
Quoted 10 lines
Quoted 6 lines
What remains a question is where the disambiguating information comes from in each case: does it come from properties of the IOAS, propertues of the device, or from extra parameters supplied at attach time. IIUC, the current draft suggests it always comes at attach time for the PASID information. Obviously the more consistency we can have here the better.
From a generic view point I'd say all are fair game. It is up to the IOMMU driver to take the requested set of IOAS's, the "at attachment" information (like PASID) and decide what to do, or fail.
Ok, that's a model that makes sense to me.
Quoted 9 lines
Quoted 2 lines
I can also see an additional problem in implementation, once we start looking at hot-adding devices to existing address spaces.
I won't pretend to guess how to implement this :) Just from a modeling perspective is something that works logically. If the kernel implementation is too hard then PPC should do one of the other ideas.
Personally I'd probably try for a nice multi-domain attachment model like PASID and not try to create/destroy domains.
I don't really follow what you mean by that.
Quoted 7 lines
As I said in my last email I think it is up to each IOMMU HW driver to make these decisions, the iommufd framework just provides a standardized API toward the attaching driver that the IOMMU HW must fit into.
Jason
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Tian, KevinQuoted 54 lines · Kevin
Quoted 24 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pciQuoted 8 lines
devices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the
is the hint single-range or could be multiple-ranges?
Quoted 1 line
actual geometry including any holes via a query.
I'd like to see a detail flow from David on how the uAPI works today with existing spapr driver and what exact changes he'd like to make on this proposed interface. Above info is still insufficient for us to think about the right solution.
Quoted 12 lines
Quoted 2 lines
- Currently ioasid term has already been used in the kernel
(
drivers/iommu/Quoted 2 lines
ioasid.c) to represent the hardware I/O address space ID in the wire. It covers both PCI PASID (Process Address Space ID) and ARM SSID (Sub-Stream
Quoted 1 line
ID). We need find a way to resolve the naming conflict between the
hardware
Quoted 3 lines
ID and software handle. One option is to rename the existing ioasid to be pasid or ssid, given their full names still sound generic. Appreciate more thoughts on this open!
Quoted 5 lines
Quoted 2 lines
ioas works well here I think. Use
ioas_idto refer to the xarray index.What about when introducing pasid to this uAPI? Then use
ioas_idfor the xarray index and ioasid to represent pasid/ssid?
This is probably obsoleted by Jason's other comments, but definitely don't use "ioas_id" and "ioasid" to mean different things. Having meaningfully different things distinguished only by an underscore is not a good idea.
Quoted 8 lines
At this point
the software handle and hardware id are mixed together thus need a clear terminology to differentiate them.Thanks
Kevin
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
RE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Jason GunthorpeQuoted 3 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AM
[...]
Quoted 16 lines
Review
Quoted 1 line
(quoted diff) +0
-0
b/drivers/iommu/iommufd/iommufd.cQuoted 14 lines
index
641f199f2d41..4839f128b24a100644
+++b/drivers/iommu/iommufd/iommufd.cdrivers/iommu/iommufd/iommufd.c +1
-0@@ -24,6 +24,7 @@struct iommufd_ctx {refcount_t refs;struct mutex lock; +struct xarray ioasid_xa;/* xarray of ioasids */ struct xarray device_xa;/* xarray of bound devices */ };@@ -42,6 +43,16 @@ struct iommufd_device {u64 dev_cookie; };
Quoted 5 lines
drivers/iommu/iommufd/iommufd.c struct iommufd_device { · line 48
Quoted 3 lines
drivers/iommu/iommufd/iommufd.c +3
-0+/* Represent an I/O address space */ +struct iommufd_ioas { +int ioasid;xarray id's should consistently be u32s everywhere.
sure. just one more check, this id is supposed to be returned to userspace as the return value of ioctl(IOASID_ALLOC). That's why I chose to use "int" as its prototype to make it aligned with the return type of ioctl(). Based on this, do you think it's still better to use "u32" here?
Regards,
Yi Liu
Quoted 3 lines
Many of the same prior comments repeated here
Jason
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 26 lines · Yi L
Quoted 3 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AM[...]
Quoted 16 lines
Review
Quoted 1 line
(quoted diff) +0
-0
b/drivers/iommu/iommufd/iommufd.cQuoted 14 lines
index
641f199f2d41..4839f128b24a100644
+++b/drivers/iommu/iommufd/iommufd.cdrivers/iommu/iommufd/iommufd.c +1
-0@@ -24,6 +24,7 @@struct iommufd_ctx {refcount_t refs;struct mutex lock; +struct xarray ioasid_xa;/* xarray of ioasids */ struct xarray device_xa;/* xarray of bound devices */ };@@ -42,6 +43,16 @@ struct iommufd_device {u64 dev_cookie; };Quoted 5 lines
drivers/iommu/iommufd/iommufd.c struct iommufd_device { · line 48
Quoted 3 lines
drivers/iommu/iommufd/iommufd.c +3
-0+/* Represent an I/O address space */ +struct iommufd_ioas { +int ioasid;xarray id's should consistently be u32s everywhere.
Quoted 5 lines
sure. just one more check, this id is supposed to be returned to userspace as the return value of
ioctl(IOASID_ALLOC). That's why I chose to use "int" as its prototype to make it aligned with the return type ofioctl(). Based on this, do you think it's still better to use "u32" here?
I suggest not using the return code from ioctl to exchange data.. The rest of the uAPI uses an in/out struct, everything should do that consistently.
Jason
RE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 36 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 9:32 PMQuoted 26 lines · Yi L
Quoted 3 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:45 AM[...]
Quoted 16 lines
Review
Quoted 1 line
(quoted diff) +0
-0
b/drivers/iommu/iommufd/iommufd.cQuoted 14 lines
index
641f199f2d41..4839f128b24a100644
+++b/drivers/iommu/iommufd/iommufd.cdrivers/iommu/iommufd/iommufd.c +1
-0@@ -24,6 +24,7 @@struct iommufd_ctx {refcount_t refs;struct mutex lock; +struct xarray ioasid_xa;/* xarray of ioasids */ struct xarray device_xa;/* xarray of bound devices */ };@@ -42,6 +43,16 @@ struct iommufd_device {u64 dev_cookie; };Quoted 5 lines
drivers/iommu/iommufd/iommufd.c struct iommufd_device { · line 48
Quoted 3 lines
drivers/iommu/iommufd/iommufd.c +3
-0+/* Represent an I/O address space */ +struct iommufd_ioas { +int ioasid;xarray id's should consistently be u32s everywhere.
Quoted 5 lines
sure. just one more check, this id is supposed to be returned to userspace as the return value of
ioctl(IOASID_ALLOC). That's why I chose to use "int" as its prototype to make it aligned with the return type ofioctl(). Based on this, do you think it's still better to use "u32" here?
Quoted 3 lines
I suggest not using the return code from ioctl to exchange data.. The rest of the uAPI uses an in/out struct, everything should do that consistently.
got it.
Thanks,
Yi Liu
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Jason GunthorpeQuoted 25 lines · Jason Gunthorpe
Quoted 15 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Quoted 8 lines
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pci devices can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1 discussion and then decide how to incorporate it in v2.
Quoted 3 lines
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the actual geometry including any holes via a query.
So part of the point of specifying start/end addresses is that explicitly querying holes shouldn't be necessary: if the requested range crosses a hole, it should fail. If you didn't really need all that range, you shouldn't have asked for it.
Which means these aren't really "hints" but optionally supplied constraints.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 30 lines · David Gibson
Quoted 25 lines · Jason Gunthorpe
Quoted 15 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Quoted 8 lines
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pci devices can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1 discussion and then decide how to incorporate it in v2.Quoted 3 lines
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the actual geometry including any holes via a query.
Quoted 7 lines
So part of the point of specifying start/end addresses is that explicitly querying holes shouldn't be necessary: if the requested range crosses a hole, it should fail. If you didn't really need all that range, you shouldn't have asked for it.
Which means these aren't really "hints" but optionally supplied constraints.
We have to be very careful here, there are two very different use cases. When we are talking about the generic API I am mostly interested to see that applications like DPDK can use this API and be portable to any IOMMU HW the kernel supports. I view the fact that there is VFIO PPC specific code in DPDK as a failing of the kernel to provide a HW abstraction.
This means we cannot define an input that has a magic HW specific value. DPDK can never provide that portably. Thus all these kinds of inputs in the generic API need to be hints, if they exist at all.
As 'address space size hint'/'address space start hint' is both generic, useful, and providable by DPDK I think it is OK. PPC can use it to pick which of the two page table formats to use for this IOAS if it wants.
The second use case is when we have a userspace driver for a specific HW IOMMU. Eg a vIOMMU in qemu doing specific PPC/ARM/x86 acceleration. We can look here for things to make general, but I would expect a fairly high bar. Instead, I would rather see the userspace driver communicate with the kernel driver in its own private language, so that the entire functionality of the unique HW can be used.
So, when it comes to providing exact ranges as an input parameter we have to decide if that is done as some additional general data, or if it should be part of a IOAS_FORMAT_KERNEL_PPC. In this case I suggest the guiding factor should be if every single IOMMU implementation can be updated to support the value.
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 39 lines · Jason Gunthorpe
Quoted 30 lines · David Gibson
Quoted 25 lines · Jason Gunthorpe
Quoted 15 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Quoted 8 lines
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pci devices can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1 discussion and then decide how to incorporate it in v2.Quoted 3 lines
I think the request was to include a start/end IO address hint when creating the ios. When the kernel creates it then it can return the actual geometry including any holes via a query.
Quoted 7 lines
So part of the point of specifying start/end addresses is that explicitly querying holes shouldn't be necessary: if the requested range crosses a hole, it should fail. If you didn't really need all that range, you shouldn't have asked for it.
Which means these aren't really "hints" but optionally supplied constraints.
Quoted 6 lines
We have to be very careful here, there are two very different use cases. When we are talking about the generic API I am mostly interested to see that applications like DPDK can use this API and be portable to any IOMMU HW the kernel supports. I view the fact that there is VFIO PPC specific code in DPDK as a failing of the kernel to provide a HW abstraction.
I would agree. At the time we were making this, we thought there were irreconcilable differences between what could be done with the x86 vs ppc IOMMUs. Turns out we just didn't think it through hard enough to find a common model.
Quoted 2 lines
This means we cannot define an input that has a magic HW specific value.
I'm not entirely sure what you mean by that.
Quoted 2 lines
DPDK can never provide that portably. Thus all these kinds of inputs in the generic API need to be hints, if they exist at all.
I don't follow your reasoning. First, note that in qemu these valus are *target* hardware specific, not *host* hardware specific. If those requests aren't honoured, qemu cannot faithfully emulate the target hardware and has to fail. That's what I mean when I say this is not a constraint, not a hint.
But when I say the constraint is optional, I mean that things which don't have that requirement - like DPDK - shouldn't apply the constraint.
Quoted 2 lines
As 'address space size hint'/'address space start hint' is both generic, useful, and providable by DPDK I think it is OK.
Size is certainly providable, and probably useful. For DPDK, I don't think start is useful.
Quoted 3 lines
PPC can use
it to pick which of the two page table formats to use for this IOAS if it wants.
Clarification: it's not that each window has a specific page table format. The two windows are independent of each other, which means you can separately select the page table format for each one (although the 32-bit one generally won't be big enough that there's any point selecting something other than a 1-level TCE table). When I say format here, I basically mean number of levels and size of each level
- the IOPTE (a.k.a. TCE) format is the same in each case.
Quoted 6 lines
The second use case is when we have a userspace driver for a specific HW IOMMU. Eg a vIOMMU in qemu doing specific PPC/ARM/x86 acceleration. We can look here for things to make general, but I would expect a fairly high bar. Instead, I would rather see the userspace driver communicate with the kernel driver in its own private language, so that the entire functionality of the unique HW can be used.
I don't think we actually need to do this. Or rather, we might want to do this for maximum performance in some cases, but I think we can have something that at least usually works without having explicit host == target logic for each case. I believe this can work (at least when using kernel managed IO page tables) in a lot of cases even with a different vIOMMU from the host IOMMU.
e.g. suppose the host is some x86 (or arm, or whatever) machine with an IOMMU capable of translating any address from 0..2^60, with maybe the exception of an IO hole somewhere between 2GiB and 4GiB.
qemu wants to emulate a PAPR vIOMMU, so it says (via interfaces yet to be determined) that it needs an IOAS where things can be mapped in the range 0..2GiB (for the 32-bit window) and 2^59..2^59+1TiB (for the 64-bit window).
Ideally the host /dev/iommu will say "ok!", since both those ranges are within the 0..2^60 translated range of the host IOMMU, and don't touch the IO hole. When the guest calls the IO mapping hypercalls, qemu translates those into DMA_MAP operations, and since they're all within the previously verified windows, they should work fine.
Quoted 5 lines
So, when it comes to providing exact ranges as an input parameter we have to decide if that is done as some additional general data, or if it should be part of a
IOAS_FORMAT_KERNEL_PPC. In this case I suggest the guiding factor should be if every single IOMMU implementation can be updated to support the value.
No, I don't think that needs to be a condition. I think it's perfectly reasonable for a constraint to be given, and for the host IOMMU to just say "no, I can't do that". But that does mean that each of these values has to have an explicit way of userspace specifying "I don't care", so that the kernel will select a suitable value for those instead - that's what DPDK or other userspace would use nearly all the time.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 10 lines · David Gibson
qemu wants to emulate a PAPR vIOMMU, so it says (via interfaces yet to be determined) that it needs an IOAS where things can be mapped in the range 0..2GiB (for the 32-bit window) and 2^59..2^59+1TiB (for the 64-bit window).
Ideally the host
/dev/iommuwill say "ok!", since both those ranges are within the 0..2^60 translated range of the host IOMMU, and don't touch the IO hole. When the guest calls the IO mapping hypercalls, qemu translates those intoDMA_MAPoperations, and since they're all within the previously verified windows, they should work fine.
Seems like we don't need the negotiation part? The host kernel communicates available IOVA ranges to userspace including holes (patch 17), and userspace can check that the ranges it needs are within the IOVA space boundaries. That part is necessary for DPDK as well since it needs to know about holes in the IOVA space where DMA wouldn't work as expected (MSI doorbells for example). And there already is a negotiation happening, when the host kernel rejects MAP ioctl outside the advertised area.
Thanks,
Jean
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 6 lines · Jean-Philippe Brucker
Seems like we don't need the negotiation part? The host kernel communicates available IOVA ranges to userspace including holes (patch 17), and userspace can check that the ranges it needs are within the IOVA space boundaries. That part is necessary for DPDK as well since it needs to know about holes in the IOVA space where DMA wouldn't work as expected (MSI doorbells for example).
I haven't looked super closely at DPDK, but the other simple VFIO app I am aware of struggled to properly implement this semantic (Indeed it wasn't even clear to the author this was even needed).
It requires interval tree logic inside the application which is not a trivial algorithm to implement in C.
I do wonder if the "simple" interface should have an option more like the DMA API where userspace just asks to DMA map some user memory and gets back the dma_addr_t to use. Kernel manages the allocation space/etc.
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 13 lines · Jason Gunthorpe
Quoted 6 lines · Jean-Philippe Brucker
Seems like we don't need the negotiation part? The host kernel communicates available IOVA ranges to userspace including holes (patch 17), and userspace can check that the ranges it needs are within the IOVA space boundaries. That part is necessary for DPDK as well since it needs to know about holes in the IOVA space where DMA wouldn't work as expected (MSI doorbells for example).
I haven't looked super closely at DPDK, but the other simple VFIO app I am aware of struggled to properly implement this semantic (Indeed it wasn't even clear to the author this was even needed).
Quoted 7 lines
It requires interval tree logic inside the application which is not a trivial algorithm to implement in C.
I do wonder if the "simple" interface should have an option more like the DMA API where userspace just asks to DMA map some user memory and gets back the
dma_addr_tto use. Kernel manages the allocation space/etc.
Agreed, it's tempting to use IOVA = VA but the two spaces aren't necessarily compatible. An extension that plugs into the IOVA allocator could be useful to userspace drivers.
Thanks,
Jean
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 23 lines
From: Jean-Philippe Brucker <[email protected]>
Sent: Tuesday, October 12, 2021 4:34 PMQuoted 7 lines · Jason Gunthorpe
Quoted 5 lines · Jean-Philippe Brucker
Seems like we don't need the negotiation part? The host kernel communicates available IOVA ranges to userspace including holes (patch 17), and userspace can check that the ranges it needs are within the IOVA space boundaries. That part is necessary for DPDK as well since it needs to know about holes in the IOVA space where DMA wouldn't work as
expected
Quoted 11 lines
Quoted 1 line
(MSI doorbells for example).
I haven't looked super closely at DPDK, but the other simple VFIO app I am aware of struggled to properly implement this semantic (Indeed it wasn't even clear to the author this was even needed).
It requires interval tree logic inside the application which is not a trivial algorithm to implement in C.
I do wonder if the "simple" interface should have an option more like the DMA API where userspace just asks to DMA map some user memory
Quoted 8 lines
and
Quoted 2 lines
gets back the
dma_addr_tto use. Kernel manages the allocation space/etc.Agreed, it's tempting to use IOVA = VA but the two spaces aren't necessarily compatible. An extension that plugs into the IOVA allocator could be useful to userspace drivers.
Make sense. We can have a flag in IOMMUFD_MAP_DMA to tell whether the user provides vaddr or expects the kernel to allocate and return.
Thanks
Kevin
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Jean-Philippe BruckerQuoted 15 lines
From: Jean-Philippe Brucker <[email protected]>
Sent: Monday, October 11, 2021 4:50 PMQuoted 10 lines · David Gibson
qemu wants to emulate a PAPR vIOMMU, so it says (via interfaces yet to be determined) that it needs an IOAS where things can be mapped in the range 0..2GiB (for the 32-bit window) and 2^59..2^59+1TiB (for the 64-bit window).
Ideally the host
/dev/iommuwill say "ok!", since both those ranges are within the 0..2^60 translated range of the host IOMMU, and don't touch the IO hole. When the guest calls the IO mapping hypercalls, qemu translates those intoDMA_MAPoperations, and since they're all within the previously verified windows, they should work fine.
Quoted 9 lines
Seems like we don't need the negotiation part? The host kernel communicates available IOVA ranges to userspace including holes (patch 17), and userspace can check that the ranges it needs are within the IOVA space boundaries. That part is necessary for DPDK as well since it needs to know about holes in the IOVA space where DMA wouldn't work as expected
(MSI doorbells for example). And there already is a negotiation happening, when the host kernel rejects MAP ioctl outside the advertised area.
Agree. This can cover the ppc platforms with fixed reserved ranges. It's meaningless to have user further tell kernel that it is only willing to use a subset of advertised area. for ppc platforms with dynamic reserved ranges which are claimed by user, we can leave it out of the common set and handled in a different way, either leveraging ioas nesting if applied or having ppc specific cmd.
Thanks
Kevin
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Jean-Philippe BruckerQuoted 12 lines · Jean-Philippe Brucker
Quoted 10 lines · David Gibson
qemu wants to emulate a PAPR vIOMMU, so it says (via interfaces yet to be determined) that it needs an IOAS where things can be mapped in the range 0..2GiB (for the 32-bit window) and 2^59..2^59+1TiB (for the 64-bit window).
Ideally the host
/dev/iommuwill say "ok!", since both those ranges are within the 0..2^60 translated range of the host IOMMU, and don't touch the IO hole. When the guest calls the IO mapping hypercalls, qemu translates those intoDMA_MAPoperations, and since they're all within the previously verified windows, they should work fine.
Quoted 7 lines
Seems like we don't need the negotiation part? The host kernel communicates available IOVA ranges to userspace including holes (patch 17), and userspace can check that the ranges it needs are within the IOVA space boundaries. That part is necessary for DPDK as well since it needs to know about holes in the IOVA space where DMA wouldn't work as expected (MSI doorbells for example). And there already is a negotiation happening, when the host kernel rejects MAP ioctl outside the advertised area.
The problem with the approach where the kernel advertises and userspace selects based on that, is that it locks us into a specific representation of what's possible. If we get new hardware with new weird constraints that can't be expressed with the representation we chose, we're kind of out of stuffed. Userspace will have to change to accomodate the new extension and have any chance of working on the new hardware.
With the model where userspace requests, and the kernel acks or nacks, we can still support existing userspace if the only things it requests can still be accomodated in the new constraints. That's pretty likely if the majority of userspaces request very simple things (say a single IOVA block where it doesn't care about the base address).
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to David GibsonQuoted 4 lines · David Gibson
Quoted 2 lines
This means we cannot define an input that has a magic HW specific value.
I'm not entirely sure what you mean by that.
I mean if you make a general property 'foo' that userspace must specify correctly then your API isn't general anymore. Userspace must know if it is A or B HW to set foo=A or foo=B.
Supported IOVA ranges are easially like that as every IOMMU is different. So DPDK shouldn't provide such specific or binding information.
Quoted 7 lines
No, I don't think that needs to be a condition. I think it's perfectly reasonable for a constraint to be given, and for the host IOMMU to just say "no, I can't do that". But that does mean that each of these values has to have an explicit way of userspace specifying "I don't care", so that the kernel will select a suitable value for those instead - that's what DPDK or other userspace would use nearly all the time.
My feeling is that qemu should be dealing with the host != target case, not the kernel.
The kernel's job should be to expose the IOMMU HW it has, with all features accessible, to userspace.
Qemu's job should be to have a userspace driver for each kernel IOMMU and the internal infrastructure to make accelerated emulations for all supported target IOMMUs.
In other words, it is not the kernel's job to provide target IOMMU emulation.
The kernel should provide truely generic "works everywhere" interface that qemu/etc can rely on to implement the least accelerated emulation path.
So when I see proposals to have "generic" interfaces that actually require very HW specific setup, and cannot be used by a generic qemu userpace driver, I think it breaks this model. If qemu needs to know it is on PPC (as it does today with VFIO's PPC specific API) then it may as well speak PPC specific language and forget about pretending to be generic.
This approach is grounded in 15 years of trying to build these user/kernel split HW subsystems (particularly RDMA) where it has become painfully obvious that the kernel is the worst place to try and wrangle really divergent HW into a "common" uAPI.
This is because the kernel/user boundary is fixed. Introducing anything generic here requires a lot of time, thought, arguing and risk. Usually it ends up being done wrong (like the PPC specific ioctls, for instance) and when this happens we can't learn and adapt, we are stuck with stable uABI forever.
Exposing a device's native programming interface is much simpler. Each device is fixed, defined and someone can sit down and figure out how to expose it. Then that is it, it doesn't need revisiting, it doesn't need harmonizing with a future slightly different device, it just stays as is.
The cost, is that there must be a userspace driver component for each HW piece - which we are already paying here!
Quoted 5 lines
Ideally the host
/dev/iommuwill say "ok!", since both those ranges are within the 0..2^60 translated range of the host IOMMU, and don't touch the IO hole. When the guest calls the IO mapping hypercalls, qemu translates those intoDMA_MAPoperations, and since they're all within the previously verified windows, they should work fine.
For instance, we are going to see HW with nested page tables, user space owned page tables and even kernel-bypass fast IOTLB invalidation.
In that world does it even make sense for qmeu to use slow DMA_MAP ioctls for emulation?
A userspace framework in qemu can make these optimizations and is also necessarily HW specific as the host page table is HW specific..
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 10 lines · Jason Gunthorpe
Quoted 4 lines · David Gibson
Quoted 2 lines
This means we cannot define an input that has a magic HW specific value.
I'm not entirely sure what you mean by that.
I mean if you make a general property 'foo' that userspace must specify correctly then your API isn't general anymore. Userspace must know if it is A or B HW to set foo=A or foo=B.
I absolutely agree. Which is exactly why I'm advocating that userspace should request from the kernel what it needs (providing a *minimum* of information) and the kernel satisfies that (filling in the missing information as suitable for the platform) or outright fails.
I think that is more robust across multiple platforms and usecases than advertising a bunch of capabilities and forcing userspace to interpret those to work out what it can do.
Quoted 3 lines
Supported IOVA ranges are easially like that as every IOMMU is different. So DPDK shouldn't provide such specific or binding information.
Absolutely, DPDK should not provide that. qemu *should* provide that, because the specific IOVAs matter to the guest. That will inevitably mean that the request is more likely to fail, but that's a fundamental tradeoff.
Quoted 8 lines
Quoted 7 lines
No, I don't think that needs to be a condition. I think it's perfectly reasonable for a constraint to be given, and for the host IOMMU to just say "no, I can't do that". But that does mean that each of these values has to have an explicit way of userspace specifying "I don't care", so that the kernel will select a suitable value for those instead - that's what DPDK or other userspace would use nearly all the time.
Quoted 5 lines
My feeling is that qemu should be dealing with the host != target case, not the kernel.
The kernel's job should be to expose the IOMMU HW it has, with all features accessible, to userspace.
See... to me this is contrary to the point we agreed on above.
Quoted 3 lines
Qemu's job should be to have a userspace driver for each kernel IOMMU and the internal infrastructure to make accelerated emulations for all supported target IOMMUs.
This seems the wrong way around to me. I see qemu as providing logic to emulate each target IOMMU. Where that matches the host, there's the potential for an accelerated implementation, but it makes life a lot easier if we can at least have a fallback that will work on any sufficiently capable host IOMMU.
Quoted 2 lines
In other words, it is not the kernel's job to provide target IOMMU emulation.
Absolutely not. But it *is* the kernel's job to let qemu do as mach as it can with the *host* IOMMU.
Quoted 3 lines
The kernel should provide truely generic "works everywhere" interface that qemu/etc can rely on to implement the least accelerated emulation path.
Right... seems like we're agreeing again.
Quoted 6 lines
So when I see proposals to have "generic" interfaces that actually require very HW specific setup, and cannot be used by a generic qemu userpace driver, I think it breaks this model. If qemu needs to know it is on PPC (as it does today with VFIO's PPC specific API) then it may as well speak PPC specific language and forget about pretending to be generic.
Absolutely, the current situation is a mess.
Quoted 9 lines
This approach is grounded in 15 years of trying to build these user/kernel split HW subsystems (particularly RDMA) where it has become painfully obvious that the kernel is the worst place to try and wrangle really divergent HW into a "common" uAPI.
This is because the kernel/user boundary is fixed. Introducing anything generic here requires a lot of time, thought, arguing and risk. Usually it ends up being done wrong (like the PPC specific ioctls, for instance)
Those are certainly wrong, but they came about explicitly by *not* being generic rather than by being too generic. So I'm really confused aso to what you're arguing for / against.
Quoted 8 lines
and when this happens we can't learn and adapt,
we are stuck with stable uABI forever.Exposing a device's native programming interface is much simpler. Each device is fixed, defined and someone can sit down and figure out how to expose it. Then that is it, it doesn't need revisiting, it doesn't need harmonizing with a future slightly different device, it just stays as is.
I can certainly see the case for that approach. That seems utterly at odds with what /dev/iommu is trying to do, though.
Quoted 9 lines
The cost, is that there must be a userspace driver component for each HW piece - which we are already paying here!
Quoted 5 lines
Ideally the host
/dev/iommuwill say "ok!", since both those ranges are within the 0..2^60 translated range of the host IOMMU, and don't touch the IO hole. When the guest calls the IO mapping hypercalls, qemu translates those intoDMA_MAPoperations, and since they're all within the previously verified windows, they should work fine.
Quoted 6 lines
For instance, we are going to see HW with nested page tables, user space owned page tables and even kernel-bypass fast IOTLB invalidation.
In that world does it even make sense for qmeu to use slow
DMA_MAPioctls for emulation?
Probably not what you want ideally, but it's a really useful fallback case to have.
Quoted 5 lines
A userspace framework in qemu can make these optimizations and is also necessarily HW specific as the host page table is HW specific..
Jason
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 7 lines · David Gibson
Quoted 5 lines
My feeling is that qemu should be dealing with the host != target case, not the kernel.
The kernel's job should be to expose the IOMMU HW it has, with all features accessible, to userspace.
See... to me this is contrary to the point we agreed on above.
I'm not thinking of these as exclusive ideas.
The IOCTL interface in iommu can quite happily expose:
Create IOAS generically
Manipulate IOAS generically
Create IOAS with IOMMU driver specific attributes HW specific Manipulate IOAS
IOCTL commands all together.
So long as everything is focused on a generic in-kernel IOAS object it is fine to have multiple ways in the uAPI to create and manipulate the objects.
When I speak about a generic interface I mean "Create IOAS generically" - ie a set of IOCTLs that work on most IOMMU HW and can be relied upon by things like DPDK/etc to always work and be portable. This is why I like "hints" to provide some limited widely applicable micro-optimization.
When I said "expose the IOMMU HW it has with all features accessible" I mean also providing "Create IOAS with IOMMU driver specific attributes".
These other IOCTLs would allow the IOMMU driver to expose every configuration knob its HW has, in a natural HW centric language. There is no pretense of genericness here, no crazy foo=A, foo=B hidden device specific interface.
Think of it as a high level/low level interface to the same thing.
Quoted 3 lines
Those are certainly wrong, but they came about explicitly by *not* being generic rather than by being too generic. So I'm really confused aso to what you're arguing for / against.
IMHO it is not having a PPC specific interface that was the problem, it was making the PPC specific interface exclusive to the type 1 interface. If type 1 continued to work on PPC then DPDK/etc would never learned PPC specific code.
For iommufd with the high/low interface each IOMMU HW should ask basic questions:
- What should the generic high level interface do on this HW? For instance what should 'Create IOAS generically' do for PPC? It should not fail, it should create *something* What is the best thing for DPDK?
I guess the 64 bit window is most broadly useful.
- How to accurately describe the HW in terms of standard IOAS objects and where to put HW specific structs to support this.
This is where PPC would decide how best to expose a control over its low/high window (eg 1,2,3 IOAS). Whatever the IOMMU driver wants, so long as it fits into the kernel IOAS model facing the connected device driver.
QEMU would have IOMMU userspace drivers. One would be the "generic driver" using only the high level generic interface. It should work as best it can on all HW devices. This is the fallback path you talked of.
QEMU would also have HW specific IOMMU userspace drivers that know how to operate the exact HW. eg these drivers would know how to use userspace page tables, how to form IOPTEs and how to access the special features.
This is how QEMU could use an optimzed path with nested page tables, for instance.
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 34 lines · Jason Gunthorpe
Quoted 7 lines · David Gibson
Quoted 5 lines
My feeling is that qemu should be dealing with the host != target case, not the kernel.
The kernel's job should be to expose the IOMMU HW it has, with all features accessible, to userspace.
See... to me this is contrary to the point we agreed on above.
I'm not thinking of these as exclusive ideas.
The IOCTL interface in iommu can quite happily expose:
Create IOAS generically
Manipulate IOAS generically
Create IOAS with IOMMU driver specific attributes HW specific Manipulate IOASIOCTL commands all together.
So long as everything is focused on a generic in-kernel IOAS object it is fine to have multiple ways in the uAPI to create and manipulate the objects.
When I speak about a generic interface I mean "Create IOAS generically" - ie a set of IOCTLs that work on most IOMMU HW and can be relied upon by things like DPDK/etc to always work and be portable. This is why I like "hints" to provide some limited widely applicable micro-optimization.
When I said "expose the IOMMU HW it has with all features accessible" I mean also providing "Create IOAS with IOMMU driver specific attributes".
Quoted 6 lines
These other IOCTLs would allow the IOMMU driver to expose every configuration knob its HW has, in a natural HW centric language. There is no pretense of genericness here, no crazy foo=A, foo=B hidden device specific interface.
Think of it as a high level/low level interface to the same thing.
Ok, I see what you mean.
Quoted 8 lines
Quoted 3 lines
Those are certainly wrong, but they came about explicitly by *not* being generic rather than by being too generic. So I'm really confused aso to what you're arguing for / against.
IMHO it is not having a PPC specific interface that was the problem, it was making the PPC specific interface exclusive to the type 1 interface. If type 1 continued to work on PPC then DPDK/etc would never learned PPC specific code.
Ok, but the reason this happened is that the initial version of type 1 *could not* be used on PPC. The original Type 1 implicitly promised a "large" IOVA range beginning at IOVA 0 without any real way of specifying or discovering how large that range was. Since ppc could typically only give a 2GiB range at IOVA 0, that wasn't usable.
That's why I say the problem was not making type1 generic enough. I believe the current version of Type1 has addressed this - at least enough to be usable in common cases. But by this time the ppc backend is already out there, so no-one's had the capacity to go back and make ppc work with Type1.
Quoted 8 lines
For iommufd with the high/low interface each IOMMU HW should ask basic questions:
- What should the generic high level interface do on this HW? For instance what should 'Create IOAS generically' do for PPC? It should not fail, it should create *something* What is the best thing for DPDK?
I guess the 64 bit window is most broadly useful.
Right, which means the kernel must (at least in the common case) have the capcity to choose and report a non-zero base-IOVA.
Hrm... which makes me think... if we allow this for the common kernel-managed case, do we even need to have capcity in the high-level interface for reporting IO holes? If the kernel can choose a non-zero base, it could just choose on x86 to place it's advertised window above the IO hole.
Quoted 13 lines
- How to accurately describe the HW in terms of standard IOAS objects and where to put HW specific structs to support this.
This is where PPC would decide how best to expose a control over its low/high window (eg 1,2,3 IOAS). Whatever the IOMMU driver wants, so long as it fits into the kernel IOAS model facing the connected device driver.
QEMU would have IOMMU userspace drivers. One would be the "generic driver" using only the high level generic interface. It should work as best it can on all HW devices. This is the fallback path you talked of.
Quoted 7 lines
QEMU would also have HW specific IOMMU userspace drivers that know how to operate the exact HW. eg these drivers would know how to use userspace page tables, how to form IOPTEs and how to access the special features.
This is how QEMU could use an optimzed path with nested page tables, for instance.
The concept makes sense in general. The devil's in the details, as usual.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 5 lines · David Gibson
Hrm... which makes me think... if we allow this for the common kernel-managed case, do we even need to have capcity in the high-level interface for reporting IO holes? If the kernel can choose a non-zero base, it could just choose on x86 to place it's advertised window above the IO hole.
If the high level interface is like dma_map() then, no it doesn't need the ability to report holes. Kernel would find and return the IOVA from dma_map not accept it in.
Since dma_map is a well proven model I'm inclined to model the simplied interface after it..
That said, if we have some ioctl 'query iova ranges' I would expect it to work on an IOAS created by the simplified interface too.
Jason
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Liu Yi LQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.
Quoted 7 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space.
Is this the reason for passing addr_width to IOASID_ALLOC? I didn't get what it's used for or why it's mandatory. But for PPC it sounds like it should be an address range instead of an upper limit?
Thanks,
Jean
Quoted 12 lines
This requirement doesn't sound PPC specific, as
addr_widthfor pci devices can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1 discussion and then decide how to incorporate it in v2.- Currently ioasid term has already been used in the kernel (
drivers/iommu/ioasid.c) to represent the hardware I/O address space ID in the wire. It covers both PCI PASID (Process Address Space ID) and ARM SSID (Sub-Stream ID). We need find a way to resolve the naming conflict between the hardware ID and software handle. One option is to rename the existing ioasid to be pasid or ssid, given their full names still sound generic. Appreciate more thoughts on this open!
RE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 21 lines
From: Jean-Philippe Brucker <[email protected]>
Sent: Wednesday, September 22, 2021 9:45 PMQuoted 5 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type
(
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2),Quoted 4 lines
implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify the
addr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at thispoint,
Quoted 6 lines
as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.
Quoted 8 lines
Quoted 4 lines
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space.Is this the reason for passing
addr_widthtoIOASID_ALLOC? I didn't get what it's used for or why it's mandatory. But for PPC it sounds like it should be an address range instead of an upper limit?
yes, as this open described, it may need to be a range. But not sure if PPC requires multiple ranges or just one range. Perhaps, David may guide there.
Regards,
Yi Liu
Quoted 20 lines
Thanks,
JeanQuoted 1 line
This requirement doesn't sound PPC specific, as
addr_widthfor pcidevices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 3 lines
and then decide how to incorporate it in v2.
- Currently ioasid term has already been used in the kernel
(
drivers/iommu/Quoted 2 lines
ioasid.c) to represent the hardware I/O address space ID in the wire. It covers both PCI PASID (Process Address Space ID) and ARM SSID (Sub-Stream
Quoted 1 line
ID). We need find a way to resolve the naming conflict between the
hardware
Quoted 3 lines
ID and software handle. One option is to rename the existing ioasid to be pasid or ssid, given their full names still sound generic. Appreciate more thoughts on this open!
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
In reply to Liu Yi LQuoted 15 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.
Quoted 8 lines
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pci devices can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1 discussion and then decide how to incorporate it in v2.
Ok, there are several things we need for ppc. None of which are inherently ppc specific and some of which will I think be useful for most platforms. So, starting from most general to most specific here's basically what's needed:
1. We need to represent the fact that the IOMMU can only translate *some* IOVAs, not a full 64-bit range. You have the addr_width already, but I'm entirely sure if the translatable range on ppc (or other platforms) is always a power-of-2 size. It usually will be, of course, but I'm not sure that's a hard requirement. So using a size/max rather than just a number of bits might be safer.
I think basically every platform will need this. Most platforms don't actually implement full 64-bit translation in any case, but rather some smaller number of bits that fits their page table format.
2. The translatable range of IOVAs may not begin at 0. So we need to advertise to userspace what the base address is, as well as the size. POWER's main IOVA range begins at 2^59 (at least on the models I know about).
I think a number of platforms are likely to want this, though I couldn't name them apart from POWER. Putting the translated IOVA window at some huge address is a pretty obvious approach to making an IOMMU which can translate a wide address range without colliding with any legacy PCI addresses down low (the IOMMU can check if this transaction is for it by just looking at some high bits in the address).
3. There might be multiple translatable ranges. So, on POWER the IOMMU can typically translate IOVAs from 0..2GiB, and also from 2^59..2^59+<RAM size>. The two ranges have completely separate IO page tables, with (usually) different layouts. (The low range will nearly always be a single-level page table with 4kiB or 64kiB entries, the high one will be multiple levels depending on the size of the range and pagesize).
This may be less common, but I suspect POWER won't be the only platform to do something like this. As above, using a high range is a pretty obvious approach, but clearly won't handle older devices which can't do 64-bit DMA. So adding a smaller range for those devices is again a pretty obvious solution. Any platform with an "IO hole" can be treated as having two ranges, one below the hole and one above it (although in that case they may well not have separate page tables
4. The translatable ranges might not be fixed. On ppc that 0..2GiB and 2^59..whatever ranges are kernel conventions, not specified by the hardware or firmware. When running as a guest (which is the normal case on POWER), there are explicit hypercalls for configuring the allowed IOVA windows (along with pagesize, number of levels etc.). At the moment it is fixed in hardware that there are only 2 windows, one starting at 0 and one at 2^59 but there's no inherent reason those couldn't also be configurable.
This will probably be rarer, but I wouldn't be surprised if it appears on another platform. If you were designing an IOMMU ASIC for use in a variety of platforms, making the base address and size of the translatable range(s) configurable in registers would make sense.
Now, for (3) and (4), representing lists of windows explicitly in ioctl()s is likely to be pretty ugly. We might be able to avoid that, for at least some of the interfaces, by using the nested IOAS stuff. One way or another, though, the IOASes which are actually attached to devices need to represent both windows.
e.g.
Create a "top-level" IOAS <A> representing the device's view. This would be either TYPE_KERNEL or maybe a special type. Into that you'd make just two iomappings one for each of the translation windows, pointing to IOASes <B> and <C>. IOAS <B> and <C> would have a single window, and would represent the IO page tables for each of the translation windows. These could be either TYPE_KERNEL or (say) TYPE_POWER_TCE for a user managed table. Well.. in theory, anyway. The way paravirtualization on POWER is done might mean user managed tables aren't really possible for other reasons, but that's not relevant here.
The next problem here is that we don't want userspace to have to do different things for POWER, at least not for the easy case of a userspace driver that just wants a chunk of IOVA space and doesn't really care where it is.
In general I think the right approach to handle that is to de-emphasize "info" or "query" interfaces. We'll probably still need some for debugging and edge cases, but in the normal case userspace should just specify what it *needs* and (ideally) no more with optional hints, and the kernel will either supply that or fail.
e.g. A simple userspace driver would simply say "I need an IOAS with at least 1GiB of IOVA space" and the kernel says "Ok, you can use 2^59..2^59+2GiB". qemu, emulating the POWER vIOMMU might say "I need an IOAS with translatable addresses from 0..2GiB with 4kiB page size and from 2^59..2^59+1TiB with 64kiB page size" and the kernel would either say "ok", or "I can't do that".
Quoted 263 lines
- Currently ioasid term has already been used in the kernel (
drivers/iommu/ioasid.c) to represent the hardware I/O address space ID in the wire. It covers both PCI PASID (Process Address Space ID) and ARM SSID (Sub-Stream ID). We need find a way to resolve the naming conflict between the hardware ID and software handle. One option is to rename the existing ioasid to be pasid or ssid, given their full names still sound generic. Appreciate more thoughts on this open!Signed-off-by Liu Yi L <[email protected]>--- drivers/iommu/iommufd/iommufd.c | 120 ++++++++++++++++++++++++++++++++ include/linux/iommufd.h | 3 + include/uapi/linux/iommu.h | 54 ++++++++++++++ 3 files changed, 177 insertions(+)drivers/iommu/iommufd/iommufd.c +120
-0@@ -24,6 +24,7 @@struct iommufd_ctx {refcount_t refs;struct mutex lock; +struct xarray ioasid_xa;/* xarray of ioasids */ struct xarray device_xa;/* xarray of bound devices */ };@@ -42,6 +43,16 @@ struct iommufd_device {u64 dev_cookie; }; +/* Represent an I/O address space */ +struct iommufd_ioas { +int ioasid; +u32 type; +u32 addr_width; +bool enforce_snoop; +struct iommufd_ctx *ictx; +refcount_t refs; +}; +static int iommufd_fops_open (struct inode *inode,struct file *filep) {struct iommufd_ctx *ictx;@@ -53,6 +64,7 @@ static int iommufd_fops_open(struct inode *inode, struct file *filep)refcount_set (&ictx->refs,1 );mutex_init (&ictx->lock); +xa_init_flags (&ictx->ioasid_xa,XA_FLAGS_ALLOC );xa_init_flags (&ictx->device_xa,XA_FLAGS_ALLOC ); filep->private_data = ictx;@@ -102,16 +114,118 @@ static void iommufd_ctx_put(struct iommufd_ctx *ictx)if (!refcount_dec_and_test (&ictx->refs))return ; +WARN_ON (!xa_empty (&ictx->ioasid_xa));WARN_ON (!xa_empty (&ictx->device_xa));kfree (ictx); } +/* Caller should hold ictx->lock */ +static void ioas_put_locked (struct iommufd_ioas *ioas) +{ +struct iommufd_ctx *ictx = ioas->ictx; +int ioasid = ioas->ioasid; + +if (!refcount_dec_and_test (&ioas->refs)) +return ; + +xa_erase (&ictx->ioasid_xa, ioasid); +iommufd_ctx_put (ictx); +kfree (ioas); +} + +static int iommufd_ioasid_alloc (struct iommufd_ctx *ictx,unsigned long arg) +{ +struct iommu_ioasid_alloc req; +struct iommufd_ioas *ioas; +unsigned long minsz; +int ioasid, ret; + + minsz = offsetofend(struct iommu_ioasid_alloc, addr_width); + +if (copy_from_user (&req, (void __user *)arg, minsz)) +return -EFAULT ; + +if (req.argsz < minsz || !req.addr_width || + req.flags !=IOMMU_IOASID_ENFORCE_SNOOP || + req.type !=IOMMU_IOASID_TYPE_KERNEL_TYPE1V2 ) +return -EINVAL ; + + ioas =kzalloc (sizeof (*ioas),GFP_KERNEL ); +if (!ioas) +return -ENOMEM ; + +mutex_lock (&ictx->lock); + ret =xa_alloc (&ictx->ioasid_xa, &ioasid, ioas, +XA_LIMIT (IOMMUFD_IOASID_MIN ,IOMMUFD_IOASID_MAX ), +GFP_KERNEL ); +mutex_unlock (&ictx->lock); +if (ret) { +pr_err_ratelimited ("Failed to alloc ioasid\n" ); +kfree (ioas); +return ret; + } + + ioas->ioasid = ioasid; + +/* only supports kernel managed I/O page table so far */ + ioas->type =IOMMU_IOASID_TYPE_KERNEL_TYPE1V2 ; + + ioas->addr_width = req.addr_width; + +/* only supports enforce snoop today */ + ioas->enforce_snoop = true; + +iommufd_ctx_get (ictx); + ioas->ictx = ictx; + +refcount_set (&ioas->refs,1 ); + +return ioasid; +} + +static int iommufd_ioasid_free (struct iommufd_ctx *ictx,unsigned long arg) +{ +struct iommufd_ioas *ioas =NULL ; +int ioasid, ret; + +if (copy_from_user (&ioasid, (void __user *)arg,sizeof (ioasid))) +return -EFAULT ; + +if (ioasid <0 ) +return -EINVAL ; + +mutex_lock (&ictx->lock); + ioas =xa_load (&ictx->ioasid_xa, ioasid); +if (IS_ERR (ioas)) { + ret = -EINVAL ; + goto out_unlock; + } + +/* Disallow free if refcount is not 1 */ +if (refcount_read (&ioas->refs) >1 ) { + ret = -EBUSY ; + goto out_unlock; + } + +ioas_put_locked (ioas); +out_unlock: +mutex_unlock (&ictx->lock); +return ret; +}; +static int iommufd_fops_release (struct inode *inode,struct file *filep) {struct iommufd_ctx *ictx = filep->private_data; +struct iommufd_ioas *ioas; +unsigned long index; filep->private_data =NULL ; +mutex_lock (&ictx->lock); +xa_for_each (&ictx->ioasid_xa, index, ioas) +ioas_put_locked (ioas); +mutex_unlock (&ictx->lock); +iommufd_ctx_put (ictx);return 0 ;@@ -195,6 +309,12 @@ static long iommufd_fops_unl_ioctl(struct file *filep,case IOMMU_DEVICE_GET_INFO : ret =iommufd_get_device_info (ictx, arg);break ; +case IOMMU_IOASID_ALLOC : + ret =iommufd_ioasid_alloc (ictx, arg); +break ; +case IOMMU_IOASID_FREE : + ret =iommufd_ioasid_free (ictx, arg); +break ;default :pr_err_ratelimited ("unsupported cmd %u\n" , cmd);break ;include/linux/iommufd.h +3
-0@@ -14,6 +14,9 @@#include <linux/err.h> #include <linux/device.h> +#define IOMMUFD_IOASID_MAX ((unsigned int)(0x7FFFFFFF)) +#define IOMMUFD_IOASID_MIN 0 +#define IOMMUFD_DEVID_MAX ((unsigned int)(0x7FFFFFFF))#define IOMMUFD_DEVID_MIN 0include/uapi/linux/iommu.h +54
-0@@ -57,6 +57,60 @@ struct iommu_device_info {#define IOMMU_DEVICE_GET_INFO _IO(IOMMU_TYPE, IOMMU_BASE + 1) +/* +* IOMMU_IOASID_ALLOC - _IOWR(IOMMU_TYPE, IOMMU_BASE + 2, +* struct iommu_ioasid_alloc) +* +* Allocate an IOASID. +* +* IOASID is the FD-local software handle representing an I/O address +* space. Each IOASID is associated with a single I/O page table. User +* must call this ioctl to get an IOASID for every I/O address space +* that is intended to be tracked by the kernel. +* +* User needs to specify the attributes of the IOASID and associated +* I/O page table format information according to one or multiple devices +* which will be attached to this IOASID right after. The I/O page table +* is activated in the IOMMU when it's attached by a device. Incompatible +* format between device and IOASID will lead to attaching failure in +* device side. +* +* Currently only one flag (IOMMU_IOASID_ENFORCE_SNOOP) is supported and +* must be always set. +* +* Only one I/O page table type (kernel-managed) is supported, with vfio +* type1v2 mapping semantics. +* +* User should call IOMMU_CHECK_EXTENSION for future extensions. +* +* @argsz: user filled size of this data. +* @flags: additional information for IOASID allocation. +* @type: I/O address space page table type. +* @addr_width: address width of the I/O address space. +* +* Return: allocated ioasid on success, -errno on failure. +*/ +struct iommu_ioasid_alloc { +__u32 argsz; +__u32 flags; +#define IOMMU_IOASID_ENFORCE_SNOOP (1 << 0) +__u32 type; +#define IOMMU_IOASID_TYPE_KERNEL_TYPE1V2 1 +__u32 addr_width; +}; + +#define IOMMU_IOASID_ALLOC _IO(IOMMU_TYPE, IOMMU_BASE + 2) + +/** +* IOMMU_IOASID_FREE - _IOWR(IOMMU_TYPE, IOMMU_BASE + 3, int) +* +* Free an IOASID. +* +* returns: 0 on success, -errno on failure +*/ + +#define IOMMU_IOASID_FREE _IO(IOMMU_TYPE, IOMMU_BASE + 3) +#define IOMMU_FAULT_PERM_READ (1 << 0)/* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1)/* write */ #define IOMMU_FAULT_PERM_EXEC (1 << 2)/* exec */
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 68 lines
From: David Gibson
Sent: Friday, October 1, 2021 2:11 PMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pcidevices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
Ok, there are several things we need for ppc. None of which are inherently ppc specific and some of which will I think be useful for most platforms. So, starting from most general to most specific here's basically what's needed:
1. We need to represent the fact that the IOMMU can only translate *some* IOVAs, not a full 64-bit range. You have the
addr_widthalready, but I'm entirely sure if the translatable range on ppc (or other platforms) is always a power-of-2 size. It usually will be, of course, but I'm not sure that's a hard requirement. So using a size/max rather than just a number of bits might be safer.I think basically every platform will need this. Most platforms don't actually implement full 64-bit translation in any case, but rather some smaller number of bits that fits their page table format.
2. The translatable range of IOVAs may not begin at 0. So we need to advertise to userspace what the base address is, as well as the size. POWER's main IOVA range begins at 2^59 (at least on the models I know about).
I think a number of platforms are likely to want this, though I couldn't name them apart from POWER. Putting the translated IOVA window at some huge address is a pretty obvious approach to making an IOMMU which can translate a wide address range without colliding with any legacy PCI addresses down low (the IOMMU can check if this transaction is for it by just looking at some high bits in the address).
3. There might be multiple translatable ranges. So, on POWER the IOMMU can typically translate IOVAs from 0..2GiB, and also from 2^59..2^59+<RAM size>. The two ranges have completely separate IO page tables, with (usually) different layouts. (The low range will nearly always be a single-level page table with 4kiB or 64kiB entries, the high one will be multiple levels depending on the size of the range and pagesize).
Quoted 8 lines
This may be less common, but I suspect POWER won't be the only platform to do something like this. As above, using a high range is a pretty obvious approach, but clearly won't handle older devices which can't do 64-bit DMA. So adding a smaller range for those devices is again a pretty obvious solution. Any platform with an "IO hole" can be treated as having two ranges, one below the hole and one above it (although in that case they may well not have separate page tables
1-3 are common on all platforms with fixed reserved ranges. Current vfio already reports permitted iova ranges to user via VFIO_IOMMU_ TYPE1_INFO_CAP_IOVA_RANGE and the user is expected to construct maps only in those ranges. iommufd can follow the same logic for the baseline uAPI.
For above cases a [base, max] hint can be provided by the user per Jason's recommendation. It is a hint as no additional restriction is imposed, since the kernel only cares about no violation on permitted ranges that it reports to the user. Underlying iommu driver may use this hint to optimize e.g. deciding how many levels are used for the kernel-managed page table according to max addr.
Quoted 9 lines
4. The translatable ranges might not be fixed. On ppc that 0..2GiB and 2^59..whatever ranges are kernel conventions, not specified by the hardware or firmware. When running as a guest (which is the normal case on POWER), there are explicit hypercalls for configuring the allowed IOVA windows (along with pagesize, number of levels etc.). At the moment it is fixed in hardware that there are only 2 windows, one starting at 0 and one at 2^59 but there's no inherent reason those couldn't also be configurable.
If ppc iommu driver needs to configure hardware according to the specified ranges, then it requires more than a hint thus better be conveyed via ppc specific cmd as Jason suggested.
Quoted 37 lines
This will probably be rarer, but I wouldn't be surprised if it appears on another platform. If you were designing an IOMMU ASIC for use in a variety of platforms, making the base address and size of the translatable range(s) configurable in registers would make sense.
Now, for (3) and (4), representing lists of windows explicitly in ioctl()s is likely to be pretty ugly. We might be able to avoid that, for at least some of the interfaces, by using the nested IOAS stuff. One way or another, though, the IOASes which are actually attached to devices need to represent both windows.
e.g.
Create a "top-level" IOAS <A> representing the device's view. This would be eitherTYPE_KERNELor maybe a special type. Into that you'd make just two iomappings one for each of the translation windows, pointing to IOASes <B> and <C>. IOAS <B> and <C> would have a single window, and would represent the IO page tables for each of the translation windows. These could be eitherTYPE_KERNELor (say)TYPE_POWER_TCEfor a user managed table. Well.. in theory, anyway. The way paravirtualization on POWER is done might mean user managed tables aren't really possible for other reasons, but that's not relevant here.The next problem here is that we don't want userspace to have to do different things for POWER, at least not for the easy case of a userspace driver that just wants a chunk of IOVA space and doesn't really care where it is.
In general I think the right approach to handle that is to de-emphasize "info" or "query" interfaces. We'll probably still need some for debugging and edge cases, but in the normal case userspace should just specify what it *needs* and (ideally) no more with optional hints, and the kernel will either supply that or fail.
Quoted 7 lines
e.g. A simple userspace driver would simply say "I need an IOAS with at least 1GiB of IOVA space" and the kernel says "Ok, you can use 2^59..2^59+2GiB". qemu, emulating the POWER vIOMMU might say "I need an IOAS with translatable addresses from 0..2GiB with 4kiB page size and from 2^59..2^59+1TiB with 64kiB page size" and the kernel would either say "ok", or "I can't do that".
This doesn't work for other platforms, which don't have vIOMMU mandatory as on ppc. For those platforms, the initial address space is GPA (for vm case) and Qemu needs to mark those GPA holes as reserved in firmware structure. I don't think anyone wants a tedious try-and-fail process to figure out how many holes exists in a 64bit address space...
Thanks
Kevin
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 77 lines · Kevin
Quoted 68 lines
From: David Gibson
Sent: Friday, October 1, 2021 2:11 PMQuoted 12 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type (
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2), implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify theaddr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at this point, as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.Quoted 8 lines
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see next patch) for what formats can be specified when allocating an IOASID.Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide address space. This requirement doesn't sound PPC specific, asaddr_widthfor pcidevices
Quoted 2 lines
can be also represented by a range [0, 2^addr_width-1]. This RFC hasn't adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
Ok, there are several things we need for ppc. None of which are inherently ppc specific and some of which will I think be useful for most platforms. So, starting from most general to most specific here's basically what's needed:
1. We need to represent the fact that the IOMMU can only translate *some* IOVAs, not a full 64-bit range. You have the
addr_widthalready, but I'm entirely sure if the translatable range on ppc (or other platforms) is always a power-of-2 size. It usually will be, of course, but I'm not sure that's a hard requirement. So using a size/max rather than just a number of bits might be safer.I think basically every platform will need this. Most platforms don't actually implement full 64-bit translation in any case, but rather some smaller number of bits that fits their page table format.
2. The translatable range of IOVAs may not begin at 0. So we need to advertise to userspace what the base address is, as well as the size. POWER's main IOVA range begins at 2^59 (at least on the models I know about).
I think a number of platforms are likely to want this, though I couldn't name them apart from POWER. Putting the translated IOVA window at some huge address is a pretty obvious approach to making an IOMMU which can translate a wide address range without colliding with any legacy PCI addresses down low (the IOMMU can check if this transaction is for it by just looking at some high bits in the address).
3. There might be multiple translatable ranges. So, on POWER the IOMMU can typically translate IOVAs from 0..2GiB, and also from 2^59..2^59+<RAM size>. The two ranges have completely separate IO page tables, with (usually) different layouts. (The low range will nearly always be a single-level page table with 4kiB or 64kiB entries, the high one will be multiple levels depending on the size of the range and pagesize).
Quoted 8 lines
This may be less common, but I suspect POWER won't be the only platform to do something like this. As above, using a high range is a pretty obvious approach, but clearly won't handle older devices which can't do 64-bit DMA. So adding a smaller range for those devices is again a pretty obvious solution. Any platform with an "IO hole" can be treated as having two ranges, one below the hole and one above it (although in that case they may well not have separate page tables
Quoted 8 lines
1-3 are common on all platforms with fixed reserved ranges. Current vfio already reports permitted iova ranges to user via
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEand the user is expected to construct maps only in those ranges. iommufd can follow the same logic for the baseline uAPI.For above cases a [base, max] hint can be provided by the user per Jason's recommendation.
Provided at which stage?
Quoted 2 lines
It is a hint as no additional restriction is
imposed,
For the qemu type use case, that's not true. In that case we *require* the available mapping ranges to match what the guest platform expects.
Quoted 15 lines
since the kernel only cares about no violation on permitted ranges that it reports to the user. Underlying iommu driver may use this hint to optimize e.g. deciding how many levels are used for the kernel-managed page table according to max addr.
Quoted 9 lines
4. The translatable ranges might not be fixed. On ppc that 0..2GiB and 2^59..whatever ranges are kernel conventions, not specified by the hardware or firmware. When running as a guest (which is the normal case on POWER), there are explicit hypercalls for configuring the allowed IOVA windows (along with pagesize, number of levels etc.). At the moment it is fixed in hardware that there are only 2 windows, one starting at 0 and one at 2^59 but there's no inherent reason those couldn't also be configurable.
Quoted 3 lines
If ppc iommu driver needs to configure hardware according to the specified ranges, then it requires more than a hint thus better be conveyed via ppc specific cmd as Jason suggested.
Again, a hint at what stage of the setup process are you thinking?
Quoted 44 lines
Quoted 36 lines
This will probably be rarer, but I wouldn't be surprised if it appears on another platform. If you were designing an IOMMU ASIC for use in a variety of platforms, making the base address and size of the translatable range(s) configurable in registers would make sense.
Now, for (3) and (4), representing lists of windows explicitly in ioctl()s is likely to be pretty ugly. We might be able to avoid that, for at least some of the interfaces, by using the nested IOAS stuff. One way or another, though, the IOASes which are actually attached to devices need to represent both windows.
e.g.
Create a "top-level" IOAS <A> representing the device's view. This would be eitherTYPE_KERNELor maybe a special type. Into that you'd make just two iomappings one for each of the translation windows, pointing to IOASes <B> and <C>. IOAS <B> and <C> would have a single window, and would represent the IO page tables for each of the translation windows. These could be eitherTYPE_KERNELor (say)TYPE_POWER_TCEfor a user managed table. Well.. in theory, anyway. The way paravirtualization on POWER is done might mean user managed tables aren't really possible for other reasons, but that's not relevant here.The next problem here is that we don't want userspace to have to do different things for POWER, at least not for the easy case of a userspace driver that just wants a chunk of IOVA space and doesn't really care where it is.
In general I think the right approach to handle that is to de-emphasize "info" or "query" interfaces. We'll probably still need some for debugging and edge cases, but in the normal case userspace should just specify what it *needs* and (ideally) no more with optional hints, and the kernel will either supply that or fail.
Quoted 7 lines
e.g. A simple userspace driver would simply say "I need an IOAS with at least 1GiB of IOVA space" and the kernel says "Ok, you can use 2^59..2^59+2GiB". qemu, emulating the POWER vIOMMU might say "I need an IOAS with translatable addresses from 0..2GiB with 4kiB page size and from 2^59..2^59+1TiB with 64kiB page size" and the kernel would either say "ok", or "I can't do that".
Quoted 6 lines
This doesn't work for other platforms, which don't have vIOMMU mandatory as on ppc. For those platforms, the initial address space is GPA (for vm case) and Qemu needs to mark those GPA holes as reserved in firmware structure. I don't think anyone wants a tedious try-and-fail process to figure out how many holes exists in a 64bit address space...
Ok, I'm not quite sure how this works. The holes are guest visible, which generally means they have to be fixed by the guest *platform* and can't depend on host information. Otherwise, migration is totally broken. I'm wondering if this only works by accident now, because the holes are usually in the same place on all x86 machines.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 92 lines
From: David Gibson <[email protected]>
Sent: Thursday, October 14, 2021 1:00 PMQuoted 9 lines · Kevin
Quoted 9 lines
From: David Gibson
Sent: Friday, October 1, 2021 2:11 PMQuoted 5 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type
(
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2),Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify the
addr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at thispoint,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see nextpatch)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
for what formats can be specified when allocating an IOASID.
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide addressspace.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
This requirement doesn't sound PPC specific, as
addr_widthfor pcidevices
Quoted 1 line
can be also represented by a range [0, 2^addr_width-1]. This RFC
hasn't
Quoted 57 lines
Quoted 42 lines
Quoted 1 line
adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
Ok, there are several things we need for ppc. None of which are inherently ppc specific and some of which will I think be useful for most platforms. So, starting from most general to most specific here's basically what's needed:
1. We need to represent the fact that the IOMMU can only translate *some* IOVAs, not a full 64-bit range. You have the
addr_widthalready, but I'm entirely sure if the translatable range on ppc (or other platforms) is always a power-of-2 size. It usually will be, of course, but I'm not sure that's a hard requirement. So using a size/max rather than just a number of bits might be safer.I think basically every platform will need this. Most platforms don't actually implement full 64-bit translation in any case, but rather some smaller number of bits that fits their page table format.
2. The translatable range of IOVAs may not begin at 0. So we need to advertise to userspace what the base address is, as well as the size. POWER's main IOVA range begins at 2^59 (at least on the models I know about).
I think a number of platforms are likely to want this, though I couldn't name them apart from POWER. Putting the translated IOVA window at some huge address is a pretty obvious approach to making an IOMMU which can translate a wide address range without colliding with any legacy PCI addresses down low (the IOMMU can check if this transaction is for it by just looking at some high bits in the address).
3. There might be multiple translatable ranges. So, on POWER the IOMMU can typically translate IOVAs from 0..2GiB, and also from 2^59..2^59+<RAM size>. The two ranges have completely separate IO page tables, with (usually) different layouts. (The low range will nearly always be a single-level page table with 4kiB or 64kiB entries, the high one will be multiple levels depending on the size of the range and pagesize).
Quoted 8 lines
This may be less common, but I suspect POWER won't be the only platform to do something like this. As above, using a high range is a pretty obvious approach, but clearly won't handle older devices which can't do 64-bit DMA. So adding a smaller range for those devices is again a pretty obvious solution. Any platform with an "IO hole" can be treated as having two ranges, one below the hole and one above it (although in that case they may well not have separate page tables
1-3 are common on all platforms with fixed reserved ranges. Current vfio already reports permitted iova ranges to user via
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEand the user is expected to construct maps only in those ranges. iommufd can follow the same logic for the baseline uAPI.
Quoted 4 lines
Quoted 2 lines
For above cases a [base, max] hint can be provided by the user per Jason's recommendation.
Provided at which stage?
IOMMU_IOASID_ALLOC
Quoted 7 lines
Quoted 3 lines
It is a hint as no additional restriction is
imposed,For the qemu type use case, that's not true. In that case we *require* the available mapping ranges to match what the guest platform expects.
I didn't get the 'match' part. Here we are talking about your case 3 where the available ranges are fixed. There is nothing that the guest can change in this case, as long as it allocates iova always in the reported ranges.
Quoted 74 lines
Quoted 16 lines
since the kernel only cares about no violation on permitted ranges that it reports to the user. Underlying iommu driver may use this hint to optimize e.g. deciding how many levels are used for the kernel-managed page table according to max addr.
Quoted 9 lines
4. The translatable ranges might not be fixed. On ppc that 0..2GiB and 2^59..whatever ranges are kernel conventions, not specified by the hardware or firmware. When running as a guest (which is the normal case on POWER), there are explicit hypercalls for configuring the allowed IOVA windows (along with pagesize, number of levels etc.). At the moment it is fixed in hardware that there are only 2 windows, one starting at 0 and one at 2^59 but there's no inherent reason those couldn't also be configurable.
Quoted 3 lines
If ppc iommu driver needs to configure hardware according to the specified ranges, then it requires more than a hint thus better be conveyed via ppc specific cmd as Jason suggested.
Again, a hint at what stage of the setup process are you thinking?
Quoted 36 lines
Quoted 36 lines
This will probably be rarer, but I wouldn't be surprised if it appears on another platform. If you were designing an IOMMU ASIC for use in a variety of platforms, making the base address and size of the translatable range(s) configurable in registers would make sense.
Now, for (3) and (4), representing lists of windows explicitly in ioctl()s is likely to be pretty ugly. We might be able to avoid that, for at least some of the interfaces, by using the nested IOAS stuff. One way or another, though, the IOASes which are actually attached to devices need to represent both windows.
e.g.
Create a "top-level" IOAS <A> representing the device's view. This would be eitherTYPE_KERNELor maybe a special type. Into that you'd make just two iomappings one for each of the translation windows, pointing to IOASes <B> and <C>. IOAS <B> and <C> would have a single window, and would represent the IO page tables for each of the translation windows. These could be eitherTYPE_KERNELor (say)TYPE_POWER_TCEfor a user managed table. Well.. in theory, anyway. The way paravirtualization on POWER is done might mean user managed tables aren't really possible for other reasons, but that's not relevant here.The next problem here is that we don't want userspace to have to do different things for POWER, at least not for the easy case of a userspace driver that just wants a chunk of IOVA space and doesn't really care where it is.
In general I think the right approach to handle that is to de-emphasize "info" or "query" interfaces. We'll probably still need some for debugging and edge cases, but in the normal case userspace should just specify what it *needs* and (ideally) no more with optional hints, and the kernel will either supply that or fail.
Quoted 3 lines
Quoted 3 lines
e.g. A simple userspace driver would simply say "I need an IOAS with at least 1GiB of IOVA space" and the kernel says "Ok, you can use 2^59..2^59+2GiB". qemu, emulating the POWER vIOMMU might say "I
need
Quoted 11 lines
Quoted 4 lines
an IOAS with translatable addresses from 0..2GiB with 4kiB page size and from 2^59..2^59+1TiB with 64kiB page size" and the kernel would either say "ok", or "I can't do that".
This doesn't work for other platforms, which don't have vIOMMU mandatory as on ppc. For those platforms, the initial address space is GPA (for vm case) and Qemu needs to mark those GPA holes as reserved in firmware structure. I don't think anyone wants a tedious try-and-fail process to figure out how many holes exists in a 64bit address space...
Quoted 6 lines
Ok, I'm not quite sure how this works. The holes are guest visible, which generally means they have to be fixed by the guest *platform* and can't depend on host information. Otherwise, migration is totally broken. I'm wondering if this only works by accident now, because the holes are usually in the same place on all x86 machines.
I haven't checked how qemu handle it today after vfio introduces the capability of reporting valid iova ranges (Alex, can you help confirm?). But there is no elegant answer. if qemu doesn't put the holes in GPA space it means guest driver might be broken if dma buffer happens to sit in the hole. this is even more severe than missing live migration. for x86 the situation is simpler as the only hole is 0xfeexxxxx on all platforms (with gpu as an exception). other arch may have more holes though.
regarding live migration with vfio devices, it's still in early stage. there are tons of compatibility check opens to be addressed before it can be widely deployed. this might just add another annoying open to that long list...
Thanks
Kevin
Re: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 92 lines · Kevin
Quoted 92 lines
From: David Gibson <[email protected]>
Sent: Thursday, October 14, 2021 1:00 PMQuoted 9 lines · Kevin
Quoted 9 lines
From: David Gibson
Sent: Friday, October 1, 2021 2:11 PMQuoted 5 lines · Liu Yi L
This patch adds IOASID allocation/free interface per iommufd. When allocating an IOASID, userspace is expected to specify the type and format information for the target I/O page table.
This RFC supports only one type
(
IOMMU_IOASID_TYPE_KERNEL_TYPE1V2),Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
implying a kernel-managed I/O page table with vfio type1v2 mapping semantics. For this type the user should specify the
addr_widthof the I/O address space and whether the I/O page table is created in an iommuenfore_snoopformat.enforce_snoopmust be true at thispoint,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
as the false setting requires additional contract with KVM on handling WBINVD emulation, which can be added later.
Userspace is expected to call
IOMMU_CHECK_EXTENSION(see nextpatch)
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
for what formats can be specified when allocating an IOASID.
Open:
- Devices on PPC platform currently use a different iommu driver in vfio. Per previous discussion they can also use vfio type1v2 as long as there is a way to claim a specific iova range from a system-wide addressspace.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
This requirement doesn't sound PPC specific, as
addr_widthfor pcidevices
Quoted 1 line
can be also represented by a range [0, 2^addr_width-1]. This RFC
hasn't
Quoted 57 lines
Quoted 42 lines
Quoted 1 line
adopted this design yet. We hope to have formal alignment in v1
discussion
Quoted 1 line
and then decide how to incorporate it in v2.
Ok, there are several things we need for ppc. None of which are inherently ppc specific and some of which will I think be useful for most platforms. So, starting from most general to most specific here's basically what's needed:
1. We need to represent the fact that the IOMMU can only translate *some* IOVAs, not a full 64-bit range. You have the
addr_widthalready, but I'm entirely sure if the translatable range on ppc (or other platforms) is always a power-of-2 size. It usually will be, of course, but I'm not sure that's a hard requirement. So using a size/max rather than just a number of bits might be safer.I think basically every platform will need this. Most platforms don't actually implement full 64-bit translation in any case, but rather some smaller number of bits that fits their page table format.
2. The translatable range of IOVAs may not begin at 0. So we need to advertise to userspace what the base address is, as well as the size. POWER's main IOVA range begins at 2^59 (at least on the models I know about).
I think a number of platforms are likely to want this, though I couldn't name them apart from POWER. Putting the translated IOVA window at some huge address is a pretty obvious approach to making an IOMMU which can translate a wide address range without colliding with any legacy PCI addresses down low (the IOMMU can check if this transaction is for it by just looking at some high bits in the address).
3. There might be multiple translatable ranges. So, on POWER the IOMMU can typically translate IOVAs from 0..2GiB, and also from 2^59..2^59+<RAM size>. The two ranges have completely separate IO page tables, with (usually) different layouts. (The low range will nearly always be a single-level page table with 4kiB or 64kiB entries, the high one will be multiple levels depending on the size of the range and pagesize).
Quoted 8 lines
This may be less common, but I suspect POWER won't be the only platform to do something like this. As above, using a high range is a pretty obvious approach, but clearly won't handle older devices which can't do 64-bit DMA. So adding a smaller range for those devices is again a pretty obvious solution. Any platform with an "IO hole" can be treated as having two ranges, one below the hole and one above it (although in that case they may well not have separate page tables
1-3 are common on all platforms with fixed reserved ranges. Current vfio already reports permitted iova ranges to user via
VFIO_IOMMU_TYPE1_INFO_CAP_IOVA_RANGEand the user is expected to construct maps only in those ranges. iommufd can follow the same logic for the baseline uAPI.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
For above cases a [base, max] hint can be provided by the user per Jason's recommendation.
Provided at which stage?
IOMMU_IOASID_ALLOC
Ok. I have mixed thoughts on this. Doing this at ALLOC time was my first instict as well. However with Jason's suggestion that any of a number of things could disambiguate multiple IOAS attached to a device, I wonder if it makes more sense for consistency to put base address at attach time, as with PASID.
I do think putting the size of the IOVA range makes sense to add at IOASID_ALLOC time - for basically every type of window. They'll nearly always have some limit, which is relevant pretty early.
Quoted 11 lines
Quoted 6 lines
Quoted 2 lines
It is a hint as no additional restriction is
imposed,For the qemu type use case, that's not true. In that case we *require* the available mapping ranges to match what the guest platform expects.
I didn't get the 'match' part. Here we are talking about your case 3 where the available ranges are fixed. There is nothing that the guest can change in this case, as long as it allocates iova always in the reported ranges.
Sorry, I don't understand the question.
Quoted 79 lines
Quoted 73 lines
Quoted 15 lines
since the kernel only cares about no violation on permitted ranges that it reports to the user. Underlying iommu driver may use this hint to optimize e.g. deciding how many levels are used for the kernel-managed page table according to max addr.
Quoted 9 lines
4. The translatable ranges might not be fixed. On ppc that 0..2GiB and 2^59..whatever ranges are kernel conventions, not specified by the hardware or firmware. When running as a guest (which is the normal case on POWER), there are explicit hypercalls for configuring the allowed IOVA windows (along with pagesize, number of levels etc.). At the moment it is fixed in hardware that there are only 2 windows, one starting at 0 and one at 2^59 but there's no inherent reason those couldn't also be configurable.
Quoted 3 lines
If ppc iommu driver needs to configure hardware according to the specified ranges, then it requires more than a hint thus better be conveyed via ppc specific cmd as Jason suggested.
Again, a hint at what stage of the setup process are you thinking?
Quoted 36 lines
Quoted 36 lines
This will probably be rarer, but I wouldn't be surprised if it appears on another platform. If you were designing an IOMMU ASIC for use in a variety of platforms, making the base address and size of the translatable range(s) configurable in registers would make sense.
Now, for (3) and (4), representing lists of windows explicitly in ioctl()s is likely to be pretty ugly. We might be able to avoid that, for at least some of the interfaces, by using the nested IOAS stuff. One way or another, though, the IOASes which are actually attached to devices need to represent both windows.
e.g.
Create a "top-level" IOAS <A> representing the device's view. This would be eitherTYPE_KERNELor maybe a special type. Into that you'd make just two iomappings one for each of the translation windows, pointing to IOASes <B> and <C>. IOAS <B> and <C> would have a single window, and would represent the IO page tables for each of the translation windows. These could be eitherTYPE_KERNELor (say)TYPE_POWER_TCEfor a user managed table. Well.. in theory, anyway. The way paravirtualization on POWER is done might mean user managed tables aren't really possible for other reasons, but that's not relevant here.The next problem here is that we don't want userspace to have to do different things for POWER, at least not for the easy case of a userspace driver that just wants a chunk of IOVA space and doesn't really care where it is.
In general I think the right approach to handle that is to de-emphasize "info" or "query" interfaces. We'll probably still need some for debugging and edge cases, but in the normal case userspace should just specify what it *needs* and (ideally) no more with optional hints, and the kernel will either supply that or fail.
Quoted 3 lines
Quoted 3 lines
e.g. A simple userspace driver would simply say "I need an IOAS with at least 1GiB of IOVA space" and the kernel says "Ok, you can use 2^59..2^59+2GiB". qemu, emulating the POWER vIOMMU might say "I
need
Quoted 11 lines
Quoted 4 lines
an IOAS with translatable addresses from 0..2GiB with 4kiB page size and from 2^59..2^59+1TiB with 64kiB page size" and the kernel would either say "ok", or "I can't do that".
This doesn't work for other platforms, which don't have vIOMMU mandatory as on ppc. For those platforms, the initial address space is GPA (for vm case) and Qemu needs to mark those GPA holes as reserved in firmware structure. I don't think anyone wants a tedious try-and-fail process to figure out how many holes exists in a 64bit address space...
Quoted 5 lines
Ok, I'm not quite sure how this works. The holes are guest visible, which generally means they have to be fixed by the guest *platform* and can't depend on host information. Otherwise, migration is totally broken. I'm wondering if this only works by accident now, because the holes are usually in the same place on all x86 machines.
Quoted 7 lines
I haven't checked how qemu handle it today after vfio introduces the capability of reporting valid iova ranges (Alex, can you help confirm?). But there is no elegant answer. if qemu doesn't put the holes in GPA space it means guest driver might be broken if dma buffer happens to sit in the hole. this is even more severe than missing live migration. for x86 the situation is simpler as the only hole is 0xfeexxxxx on all platforms (with gpu as an exception).
Right.. I suspect this is the only reason it's working now on x86.
Quoted 7 lines
other arch may have more holes
though.regarding live migration with vfio devices, it's still in early stage. there are tons of compatibility check opens to be addressed before it can be widely deployed. this might just add another annoying open to that long list...
So, yes, live migration with VFIO is limited, unfortunately this still affects us even if we don't (currently) have VFIO devices. The problem arises from the combination of two limitations:
1) Live migration means that we can't dynamically select guest visible IOVA parameters at qemu start up time. We need to get consistent guest visible behaviour for a given set of qemu options, so that we can migrate between them.
2) Device hotplug means that we don't know if a PCI domain will have VFIO devices on it when we start qemu. So, we don't know if host limitations on IOVA ranges will affect the guest or not.
Together these mean that the best we can do is to define a *fixed* (per machine type) configuration based on qemu options only. That is, defined by the guest platform we're trying to present, only, never host capabilities. We can then see if that configuration is possible on the host and pass or fail. It's never safe to go the other direction and take host capabilities and present those to the guest.
Obviously, we then try to define the default platform configuration in qemu to be usable on the greatest number of hosts we can.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Tian, Kevin
maintainerRE: [RFC 11/20] iommu/iommufd: Add IOMMU_IOASID_ALLOC/FREE
Quoted 8 lines
From: David Gibson <[email protected]>
Sent: Monday, October 25, 2021 1:05 PMQuoted 5 lines
Quoted 4 lines
Quoted 2 lines
For above cases a [base, max] hint can be provided by the user per Jason's recommendation.
Provided at which stage?
Quoted 7 lines
Quoted 1 line
IOMMU_IOASID_ALLOCOk. I have mixed thoughts on this. Doing this at ALLOC time was my first instict as well. However with Jason's suggestion that any of a number of things could disambiguate multiple IOAS attached to a device, I wonder if it makes more sense for consistency to put base address at attach time, as with PASID.
In that case the base address provided at attach time is used as an address space ID similar to PASID, which imho is orthogonal to the generic [base, size] info for IOAS itself. The 2nd base sort of becomes an offset on top of the first base in ppc case.
Quoted 19 lines
Quoted 5 lines
regarding live migration with vfio devices, it's still in early stage. there are tons of compatibility check opens to be addressed before it can be widely deployed. this might just add another annoying open to that long list...
So, yes, live migration with VFIO is limited, unfortunately this still affects us even if we don't (currently) have VFIO devices. The problem arises from the combination of two limitations:
1) Live migration means that we can't dynamically select guest visible IOVA parameters at qemu start up time. We need to get consistent guest visible behaviour for a given set of qemu options, so that we can migrate between them.
2) Device hotplug means that we don't know if a PCI domain will have VFIO devices on it when we start qemu. So, we don't know if host limitations on IOVA ranges will affect the guest or not.
Quoted 7 lines
Together these mean that the best we can do is to define a *fixed* (per machine type) configuration based on qemu options only. That is, defined by the guest platform we're trying to present, only, never host capabilities. We can then see if that configuration is possible on the host and pass or fail. It's never safe to go the other direction and take host capabilities and present those to the guest.
That is just one userspace policy. We don't want to design a uAPI just for a specific userspace implementation. In concept the userspace could:
1) use DMA-API like map/unmap i.e. letting IOVA address space managed by the kernel;
* suitable for simple applications e.g. dpdk.
2) manage IOVA address space with *fixed* layout:
* fail device passthrough at MAP_DMA if conflict is detected between mapped range and device specific IOVA holes
* suitable for VM when live migration is highly concerned
* potential problem with vIOMMU since the guest is unaware of host constraints thus undefined behavior may occur if guest IOVA addresses happens to overlap with host IOVA holes.
* ppc is special as you need to claim guest IOVA ranges in the host. But it's not the case for other emulated IOMMUs.
3) manage IOVA address space with host constraints:
* create IOVA layout by combining qemu options and IOVA holes of all boot-time passthrough devices
* reject hotplugged device if it has conflicting IOVA holes with the initial IOVA layout
* suitable for vIOMMU since host constraints can be further reported to the guest
* suitable for VM w/o live migration requirement, e.g. in many client virtualization scenarios
* suboptimal with VM live migration with compatibility limitation
Overall the proposed uAPI will provide:
1) a simple DMA-API-like mapping protocol for kernel managed IOVA address space:
2) a vfio-like mapping protocol for user managed IOVA address space:
a) check IOVA conflict in MAP_DMA ioctl;
b) allows the user to query available IOVA ranges;Then it's totally user policy on how it wants to utilize those ioctls.
Thanks
Kevin
RFC 12/20 iommu/iommufd: Add IOMMU_CHECK_EXTENSION
In reply to Liu Yi LAs aforementioned, userspace should check extension for what formats can be specified when allocating an IOASID. This patch adds such interface for userspace. In this RFC, iommufd reports EXT_MAP_TYPE1V2 support and no no-snoop support yet.
--- drivers/iommu/iommufd/iommufd.c | 7 +++++++ include/uapi/linux/iommu.h | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)
drivers/iommu/iommufd/iommufd.c +7 -0
@@ -306,6 +306,13 @@ static long iommufd_fops_unl_ioctl(struct file *filep,return ret;switch (cmd) { +case IOMMU_CHECK_EXTENSION : +switch (arg) { +case EXT_MAP_TYPE1V2 : +return 1 ; +default : +return 0 ; + }case IOMMU_DEVICE_GET_INFO : ret =iommufd_get_device_info (ictx, arg);break ;
include/uapi/linux/iommu.h +27 -0
@@ -14,6 +14,33 @@#define IOMMU_TYPE (';')#define IOMMU_BASE 100 +/* +* IOMMU_CHECK_EXTENSION - _IO(IOMMU_TYPE, IOMMU_BASE + 0) +* +* Check whether an uAPI extension is supported. +* +* It's unlikely that all planned capabilities in IOMMU fd will be ready +* in one breath. User should check which uAPI extension is supported +* according to its intended usage. +* +* A rough list of possible extensions may include: +* +* - EXT_MAP_TYPE1V2 for vfio type1v2 map semantics; +* - EXT_DMA_NO_SNOOP for no-snoop DMA support; +* - EXT_MAP_NEWTYPE for an enhanced map semantics; +* - EXT_MULTIDEV_GROUP for 1:N iommu group; +* - EXT_IOASID_NESTING for what the name stands; +* - EXT_USER_PAGE_TABLE for user managed page table; +* - EXT_USER_PASID_TABLE for user managed PASID table; +* - EXT_DIRTY_TRACKING for tracking pages dirtied by DMA; +* - ... +* +* Return: 0 if not supported, 1 if supported. +*/ +#define EXT_MAP_TYPE1V2 1 +#define EXT_DMA_NO_SNOOP 2 +#define IOMMU_CHECK_EXTENSION _IO(IOMMU_TYPE, IOMMU_BASE + 0) +/* * IOMMU_DEVICE_GET_INFO - _IOR(IOMMU_TYPE, IOMMU_BASE + 1, * struct iommu_device_info)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 12/20] iommu/iommufd: Add IOMMU_CHECK_EXTENSION
Quoted 50 lines · Liu Yi L
As aforementioned, userspace should check extension for what formats can be specified when allocating an IOASID. This patch adds such interface for userspace. In this RFC, iommufd reports
EXT_MAP_TYPE1V2support and no no-snoop support yet.Signed-off-by Liu Yi L <[email protected]>drivers/iommu/iommufd/iommufd.c | 7 +++++++ include/uapi/linux/iommu.h | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)drivers/iommu/iommufd/iommufd.c +8
-0+++ b/drivers/iommu/iommufd/iommufd.c@@ -306,6 +306,13 @@ static long iommufd_fops_unl_ioctl(struct file *filep,return ret;switch (cmd) { +case IOMMU_CHECK_EXTENSION : +switch (arg) { +case EXT_MAP_TYPE1V2 : +return 1 ; +default : +return 0 ; + }case IOMMU_DEVICE_GET_INFO : ret =iommufd_get_device_info (ictx, arg);break ;include/uapi/linux/iommu.h +17
-0+++ b/include/uapi/linux/iommu.h@@ -14,6 +14,33 @@#define IOMMU_TYPE (';')#define IOMMU_BASE 100 +/* +* IOMMU_CHECK_EXTENSION - _IO(IOMMU_TYPE, IOMMU_BASE + 0) +* +* Check whether an uAPI extension is supported. +* +* It's unlikely that all planned capabilities in IOMMU fd will be ready +* in one breath. User should check which uAPI extension is supported +* according to its intended usage. +* +* A rough list of possible extensions may include: +* +* - EXT_MAP_TYPE1V2 for vfio type1v2 map semantics; +* - EXT_DMA_NO_SNOOP for no-snoop DMA support; +* - EXT_MAP_NEWTYPE for an enhanced map semantics; +* - EXT_MULTIDEV_GROUP for 1:N iommu group; +* - EXT_IOASID_NESTING for what the name stands;
include/uapi/linux/iommu.h line 42
Quoted 10 lines
include/uapi/linux/iommu.h +10
-0+* - EXT_USER_PAGE_TABLE for user managed page table; +* - EXT_USER_PASID_TABLE for user managed PASID table; +* - EXT_DIRTY_TRACKING for tracking pages dirtied by DMA; +* - ... +* +* Return: 0 if not supported, 1 if supported. +*/ +#define EXT_MAP_TYPE1V2 1 +#define EXT_DMA_NO_SNOOP 2 +#define IOMMU_CHECK_EXTENSION _IO(IOMMU_TYPE, IOMMU_BASE + 0)
I generally advocate for a 'try and fail' approach to discovering compatibility.
If that doesn't work for the userspace then a query to return a generic capability flag is the next best idea. Each flag should clearly define what 'try and fail' it is talking about
Eg dma_no_snoop is about creating an IOS with flag NO SNOOP set
TYPE1V2 seems like nonsense
Not sure about the others.
IOW, this should recast to a generic 'query capabilities' IOCTL
Jason
Tian, Kevin
maintainerRE: [RFC 12/20] iommu/iommufd: Add IOMMU_CHECK_EXTENSION
Quoted 68 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:47 AMReview
Quoted 11 lines · Liu Yi L
As aforementioned, userspace should check extension for what formats can be specified when allocating an IOASID. This patch adds such interface for userspace. In this RFC, iommufd reports
EXT_MAP_TYPE1V2support and no no-snoop support yet.Signed-off-by Liu Yi L <[email protected]>drivers/iommu/iommufd/iommufd.c | 7 +++++++ include/uapi/linux/iommu.h | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)(quoted diff) +0
-0
b/drivers/iommu/iommufd/iommufd.cReview static long iommufd_fops_unl_ioctl(struct file
Quoted 3 lines
index
4839f128b24a..e45d76359e34100644
+++b/drivers/iommu/iommufd/iommufd.c(quoted diff) +0
-0@@ -306,6 +306,13 @@ static long iommufd_fops_unl_ioctl(struct file*filep,
Quoted 36 lines
drivers/iommu/iommufd/iommufd.c +7
-0return ret;switch (cmd) { +case IOMMU_CHECK_EXTENSION : +switch (arg) { +case EXT_MAP_TYPE1V2 : +return 1 ; +default : +return 0 ; + }case IOMMU_DEVICE_GET_INFO : ret =iommufd_get_device_info (ictx, arg);break ;include/uapi/linux/iommu.h +17
-0+++ b/include/uapi/linux/iommu.h@@ -14,6 +14,33 @@#define IOMMU_TYPE (';')#define IOMMU_BASE 100 +/* +* IOMMU_CHECK_EXTENSION - _IO(IOMMU_TYPE, IOMMU_BASE + 0) +* +* Check whether an uAPI extension is supported. +* +* It's unlikely that all planned capabilities in IOMMU fd will be ready +* in one breath. User should check which uAPI extension is supported +* according to its intended usage. +* +* A rough list of possible extensions may include: +* +* - EXT_MAP_TYPE1V2 for vfio type1v2 map semantics; +* - EXT_DMA_NO_SNOOP for no-snoop DMA support; +* - EXT_MAP_NEWTYPE for an enhanced map semantics; +* - EXT_MULTIDEV_GROUP for 1:N iommu group; +* - EXT_IOASID_NESTING for what the name stands; include/uapi/linux/iommu.h line 42
Quoted 10 lines
include/uapi/linux/iommu.h +10
-0+* - EXT_USER_PAGE_TABLE for user managed page table; +* - EXT_USER_PASID_TABLE for user managed PASID table; +* - EXT_DIRTY_TRACKING for tracking pages dirtied by DMA; +* - ... +* +* Return: 0 if not supported, 1 if supported. +*/ +#define EXT_MAP_TYPE1V2 1 +#define EXT_DMA_NO_SNOOP 2 +#define IOMMU_CHECK_EXTENSION _IO(IOMMU_TYPE,
IOMMU_BASE+ 0)
Quoted 6 lines
I generally advocate for a 'try and fail' approach to discovering compatibility.
If that doesn't work for the userspace then a query to return a generic capability flag is the next best idea. Each flag should clearly define what 'try and fail' it is talking about
We don't have strong preference here. Just follow what vfio does today. So Alex's opinion is appreciated here. 😊
Quoted 4 lines
Eg
dma_no_snoopis about creating an IOS with flag NO SNOOP setTYPE1V2 seems like nonsense
just in case other mapping protocols are introduced in the future
Quoted 6 lines
Not sure about the others.
IOW, this should recast to a generic 'query capabilities' IOCTL
Jason
Jason Gunthorpe
maintainerRe: [RFC 12/20] iommu/iommufd: Add IOMMU_CHECK_EXTENSION
Quoted 71 lines · Kevin
Quoted 71 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:47 AMReview
Quoted 11 lines · Liu Yi L
As aforementioned, userspace should check extension for what formats can be specified when allocating an IOASID. This patch adds such interface for userspace. In this RFC, iommufd reports
EXT_MAP_TYPE1V2support and no no-snoop support yet.Signed-off-by Liu Yi L <[email protected]>drivers/iommu/iommufd/iommufd.c | 7 +++++++ include/uapi/linux/iommu.h | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)(quoted diff) +0
-0
b/drivers/iommu/iommufd/iommufd.cReview static long iommufd_fops_unl_ioctl(struct file
Quoted 3 lines
index
4839f128b24a..e45d76359e34100644
+++b/drivers/iommu/iommufd/iommufd.c(quoted diff) +0
-0@@ -306,6 +306,13 @@ static long iommufd_fops_unl_ioctl(struct file*filep,
Quoted 36 lines
drivers/iommu/iommufd/iommufd.c +7
-0return ret;switch (cmd) { +case IOMMU_CHECK_EXTENSION : +switch (arg) { +case EXT_MAP_TYPE1V2 : +return 1 ; +default : +return 0 ; + }case IOMMU_DEVICE_GET_INFO : ret =iommufd_get_device_info (ictx, arg);break ;include/uapi/linux/iommu.h +17
-0+++ b/include/uapi/linux/iommu.h@@ -14,6 +14,33 @@#define IOMMU_TYPE (';')#define IOMMU_BASE 100 +/* +* IOMMU_CHECK_EXTENSION - _IO(IOMMU_TYPE, IOMMU_BASE + 0) +* +* Check whether an uAPI extension is supported. +* +* It's unlikely that all planned capabilities in IOMMU fd will be ready +* in one breath. User should check which uAPI extension is supported +* according to its intended usage. +* +* A rough list of possible extensions may include: +* +* - EXT_MAP_TYPE1V2 for vfio type1v2 map semantics; +* - EXT_DMA_NO_SNOOP for no-snoop DMA support; +* - EXT_MAP_NEWTYPE for an enhanced map semantics; +* - EXT_MULTIDEV_GROUP for 1:N iommu group; +* - EXT_IOASID_NESTING for what the name stands; include/uapi/linux/iommu.h line 42
Quoted 10 lines
include/uapi/linux/iommu.h +10
-0+* - EXT_USER_PAGE_TABLE for user managed page table; +* - EXT_USER_PASID_TABLE for user managed PASID table; +* - EXT_DIRTY_TRACKING for tracking pages dirtied by DMA; +* - ... +* +* Return: 0 if not supported, 1 if supported. +*/ +#define EXT_MAP_TYPE1V2 1 +#define EXT_DMA_NO_SNOOP 2 +#define IOMMU_CHECK_EXTENSION _IO(IOMMU_TYPE,
IOMMU_BASE+ 0)I generally advocate for a 'try and fail' approach to discovering compatibility.
Quoted 6 lines
Quoted 3 lines
If that doesn't work for the userspace then a query to return a generic capability flag is the next best idea. Each flag should clearly define what 'try and fail' it is talking about
We don't have strong preference here. Just follow what vfio does today. So Alex's opinion is appreciated here. 😊
This is a uAPI design, it should follow the current mainstream thinking on how to build these things. There is a lot of old stuff in vfio that doesn't match the modern thinking. IMHO.
Quoted 3 lines
Quoted 1 line
TYPE1V2 seems like nonsense
just in case other mapping protocols are introduced in the future
Well, we should never, ever do that. Allowing PPC and evrything else to split in VFIO has created a compelte disaster in userspace. HW specific extensions should be modeled as extensions not a wholesale replacement of everything.
I'd say this is part of the modern thinking on uAPI design.
What I want to strive for is the basic API is usable with all HW - and is what something like DPDK can exclusively use.
An extended API with HW specific facets exists for qemu to use to build a HW backed accelereated and featureful vIOMMU emulation.
The needs of qmeu should not trump the requirement for a universal basic API.
Eg if we can't figure out a basic API version of the PPC range issue then that should be punted to a PPC specific API.
Jason
Tian, Kevin
maintainerRE: [RFC 12/20] iommu/iommufd: Add IOMMU_CHECK_EXTENSION
Quoted 101 lines
From: Jason Gunthorpe
Sent: Wednesday, September 22, 2021 8:55 PMQuoted 7 lines · Kevin
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 1:47 AMQuoted 3 lines · Liu Yi L
As aforementioned, userspace should check extension for what formats can be specified when allocating an IOASID. This patch adds such interface for userspace. In this RFC, iommufd reports
EXT_MAP_TYPE1V2Quoted 64 lines
Quoted 64 lines
Review
Quoted 8 lines
support and no no-snoop support yet.
Signed-off-by Liu Yi L <[email protected]>drivers/iommu/iommufd/iommufd.c | 7 +++++++ include/uapi/linux/iommu.h | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+)(quoted diff) +0
-0
b/drivers/iommu/iommufd/iommufd.cReview static long iommufd_fops_unl_ioctl(struct file
Quoted 3 lines
index
4839f128b24a..e45d76359e34100644
+++b/drivers/iommu/iommufd/iommufd.c(quoted diff) +0
-0@@ -306,6 +306,13 @@ static long iommufd_fops_unl_ioctl(struct file*filep,
Quoted 36 lines
drivers/iommu/iommufd/iommufd.c +7
-0return ret;switch (cmd) { +case IOMMU_CHECK_EXTENSION : +switch (arg) { +case EXT_MAP_TYPE1V2 : +return 1 ; +default : +return 0 ; + }case IOMMU_DEVICE_GET_INFO : ret =iommufd_get_device_info (ictx, arg);break ;include/uapi/linux/iommu.h +17
-0+++ b/include/uapi/linux/iommu.h@@ -14,6 +14,33 @@#define IOMMU_TYPE (';')#define IOMMU_BASE 100 +/* +* IOMMU_CHECK_EXTENSION - _IO(IOMMU_TYPE, IOMMU_BASE + 0) +* +* Check whether an uAPI extension is supported. +* +* It's unlikely that all planned capabilities in IOMMU fd will be ready +* in one breath. User should check which uAPI extension is supported +* according to its intended usage. +* +* A rough list of possible extensions may include: +* +* - EXT_MAP_TYPE1V2 for vfio type1v2 map semantics; +* - EXT_DMA_NO_SNOOP for no-snoop DMA support; +* - EXT_MAP_NEWTYPE for an enhanced map semantics; +* - EXT_MULTIDEV_GROUP for 1:N iommu group; +* - EXT_IOASID_NESTING for what the name stands; include/uapi/linux/iommu.h line 42
Quoted 10 lines
include/uapi/linux/iommu.h +10
-0+* - EXT_USER_PAGE_TABLE for user managed page table; +* - EXT_USER_PASID_TABLE for user managed PASID table; +* - EXT_DIRTY_TRACKING for tracking pages dirtied by DMA; +* - ... +* +* Return: 0 if not supported, 1 if supported. +*/ +#define EXT_MAP_TYPE1V2 1 +#define EXT_DMA_NO_SNOOP 2 +#define IOMMU_CHECK_EXTENSION _IO(IOMMU_TYPE,
IOMMU_BASE+ 0)I generally advocate for a 'try and fail' approach to discovering compatibility.
Quoted 6 lines
Quoted 3 lines
If that doesn't work for the userspace then a query to return a generic capability flag is the next best idea. Each flag should clearly define what 'try and fail' it is talking about
We don't have strong preference here. Just follow what vfio does today. So Alex's opinion is appreciated here. 😊
This is a uAPI design, it should follow the current mainstream thinking on how to build these things. There is a lot of old stuff in vfio that doesn't match the modern thinking. IMHO.
Quoted 3 lines
Quoted 1 line
TYPE1V2 seems like nonsense
just in case other mapping protocols are introduced in the future
Well, we should never, ever do that. Allowing PPC and evrything else to split in VFIO has created a compelte disaster in userspace. HW specific extensions should be modeled as extensions not a wholesale replacement of everything.
I'd say this is part of the modern thinking on uAPI design.
What I want to strive for is the basic API is usable with all HW - and is what something like DPDK can exclusively use.
Quoted 9 lines
An extended API with HW specific facets exists for qemu to use to build a HW backed accelereated and featureful vIOMMU emulation.
The needs of qmeu should not trump the requirement for a universal basic API.
Eg if we can't figure out a basic API version of the PPC range issue then that should be punted to a PPC specific API.
sounds good. I may keep an wrong memory on the multiple mapping protocols thing. 😊
RFC 13/20 iommu: Extend iommu_at[de]tach_device() for multiple devices group
In reply to Liu Yi LFrom: Lu Baolu <[email protected]>
These two helpers could be used when 1) the iommu group is singleton, or 2) the upper layer has put the iommu group into the secure state by calling iommu_device_init_user_dma().
As we want the iommufd design to be a device-centric model, we want to remove any group knowledge in iommufd. Given that we already have iommu_at[de]tach_device() interface, we could extend it for iommufd simply by doing below:
- first device in a group does group attach;
- last device in a group does group detach.
as long as the group has been put into the secure context.
The commit <426a273834eae> ("iommu: Limit iommu_attach/detach_device to device with their own group") deliberately restricts the two interfaces to single-device group. To avoid the conflict with existing usages, we keep this policy and put the new extension only when the group has been marked for user_dma.
--- drivers/iommu/iommu.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)
drivers/iommu/iommu.c +21 -4
@@ -47,6 +47,7 @@ struct iommu_group {struct list_head entry;unsigned long user_dma_owner_id;refcount_t owner_cnt; +refcount_t attach_cnt; };struct group_device {
@@ -1994,7 +1995,7 @@ static int __iommu_attach_device(struct iommu_domain *domain,int iommu_attach_device (struct iommu_domain *domain,struct device *dev) {struct iommu_group *group; -int ret; +int ret =0 ; group =iommu_group_get (dev);if (!group)
@@ -2005,11 +2006,23 @@ int iommu_attach_device(struct iommu_domain *domain, struct device *dev)* change while we are attaching */ mutex_lock (&group->mutex); - ret = -EINVAL ; -if (iommu_group_device_count (group) !=1 ) +if (group->user_dma_owner_id) { +if (group->domain) { +if (group->domain != domain) + ret = -EBUSY ; +else +refcount_inc (&group->attach_cnt); + + goto out_unlock; + } + }else if (iommu_group_device_count (group) !=1 ) { + ret = -EINVAL ; goto out_unlock; + } ret =__iommu_attach_group (domain, group); +if (!ret && group->user_dma_owner_id) +refcount_set (&group->attach_cnt,1 ); out_unlock:mutex_unlock (&group->mutex);
@@ -2261,7 +2274,10 @@ void iommu_detach_device(struct iommu_domain *domain, struct device *dev)return ;mutex_lock (&group->mutex); -if (iommu_group_device_count (group) !=1 ) { +if (group->user_dma_owner_id) { +if (!refcount_dec_and_test (&group->attach_cnt)) + goto out_unlock; + }else if (iommu_group_device_count (group) !=1 ) {WARN_ON (1 ); goto out_unlock; }
@@ -3368,6 +3384,7 @@ static int iommu_group_init_user_dma(struct iommu_group *group, group->user_dma_owner_id = owner;refcount_set (&group->owner_cnt,1 ); +refcount_set (&group->attach_cnt,0 );/* default domain is unsafe for user-initiated dma */ if (group->domain == group->default_domain)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 14 lines · Liu Yi L
From: Lu Baolu <[email protected]>
These two helpers could be used when 1) the iommu group is singleton, or 2) the upper layer has put the iommu group into the secure state by calling
iommu_device_init_user_dma().As we want the iommufd design to be a device-centric model, we want to remove any group knowledge in iommufd. Given that we already have iommu_at[de]tach_device() interface, we could extend it for iommufd simply by doing below:
- first device in a group does group attach;
- last device in a group does group detach.
Quoted 7 lines
as long as the group has been put into the secure context.
The commit <
426a273834eae> ("iommu: Limit iommu_attach/detach_device to device with their own group") deliberately restricts the two interfaces to single-device group. To avoid the conflict with existing usages, we keep this policy and put the new extension only when the group has been marked foruser_dma.
I still kind of hate this interface because it means an operation that appears to be explicitly on a single device has an implicit effect on other devices.
Quoted 32 lines
Signed-off-by Lu Baolu <[email protected]>--- drivers/iommu/iommu.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-)drivers/iommu/iommu.c +2
-3@@ -47,6 +47,7 @@ struct iommu_group {struct list_head entry;unsigned long user_dma_owner_id;refcount_t owner_cnt; +refcount_t attach_cnt; };struct group_device {@@ -1994,7 +1995,7 @@ static int __iommu_attach_device(struct iommu_domain *domain,int iommu_attach_device (struct iommu_domain *domain,struct device *dev) {struct iommu_group *group; -int ret; +int ret =0 ; group =iommu_group_get (dev);if (!group)@@ -2005,11 +2006,23 @@ int iommu_attach_device(struct iommu_domain *domain, struct device *dev)* change while we are attaching */ mutex_lock (&group->mutex); - ret = -EINVAL ; -if (iommu_group_device_count (group) !=1 )
drivers/iommu/iommu.c int iommu_attach_device(struct iommu_domain *domain, struct device *dev) · line 2018
Quoted 10 lines
drivers/iommu/iommu.c +10
-0+if (group->user_dma_owner_id) { +if (group->domain) { +if (group->domain != domain) + ret = -EBUSY ; +else +refcount_inc (&group->attach_cnt); + + goto out_unlock; + } + }else if (iommu_group_device_count (group) !=1 ) {
With this condition in the else, how can you ever attach the first device of a multi-device group?
Quoted 11 lines
drivers/iommu/iommu.c +4
-0+ ret = -EINVAL ; goto out_unlock; + } ret =__iommu_attach_group (domain, group); +if (!ret && group->user_dma_owner_id) +refcount_set (&group->attach_cnt,1 ); out_unlock:mutex_unlock (&group->mutex);
drivers/iommu/iommu.c void iommu_detach_device(struct iommu_domain *domain, struct device *dev) · line 2280
Quoted 8 lines
drivers/iommu/iommu.c +4
-1@@ -2261,7 +2274,10 @@ void iommu_detach_device(struct iommu_domain *domain, struct device *dev)return ;mutex_lock (&group->mutex); -if (iommu_group_device_count (group) !=1 ) { +if (group->user_dma_owner_id) { +if (!refcount_dec_and_test (&group->attach_cnt)) + goto out_unlock; + }else if (iommu_group_device_count (group) !=1 ) {
Shouldn't this path (detach a thing that's not attached), be a no-op regardless of whether it's a singleton group or not? Why does one deserve a warning and one not?
Quoted 11 lines
drivers/iommu/iommu.c +1
-0WARN_ON (1 ); goto out_unlock; }@@ -3368,6 +3384,7 @@ static int iommu_group_init_user_dma(struct iommu_group *group, group->user_dma_owner_id = owner;refcount_set (&group->owner_cnt,1 ); +refcount_set (&group->attach_cnt,0 );/* default domain is unsafe for user-initiated dma */ if (group->domain == group->default_domain)
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Tian, Kevin
maintainerRE: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 22 lines
From: David Gibson <[email protected]>
Sent: Thursday, October 14, 2021 1:24 PMQuoted 11 lines · Liu Yi L
From: Lu Baolu <[email protected]>
These two helpers could be used when 1) the iommu group is singleton, or 2) the upper layer has put the iommu group into the secure state by calling
iommu_device_init_user_dma().As we want the iommufd design to be a device-centric model, we want to remove any group knowledge in iommufd. Given that we already have iommu_at[de]tach_device() interface, we could extend it for iommufd simply by doing below:
Quoted 6 lines
- first device in a group does group attach;
- last device in a group does group detach.as long as the group has been put into the secure context.
The commit <
426a273834eae> ("iommu: Limitiommu_attach/detach_device to
Quoted 9 lines
Quoted 4 lines
device with their own group") deliberately restricts the two interfaces to single-device group. To avoid the conflict with existing usages, we keep this policy and put the new extension only when the group has been marked for
user_dma.I still kind of hate this interface because it means an operation that appears to be explicitly on a single device has an implicit effect on other devices.
I still didn't get your concern why it's such a big deal. With this proposal the group restriction will be 'explicitly' documented in the attach uAPI comment and sample flow in iommufd.rst. A sane user should read all those information to understand how this new sub-system works and follow whatever constraints claimed there. In the end the user should maintain the same group knowledge regardless of whether to use an explicit group uAPI or a device uAPI which has group constraint...
Thanks,
Kevin
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 32 lines · Kevin
Quoted 22 lines
From: David Gibson <[email protected]>
Sent: Thursday, October 14, 2021 1:24 PMQuoted 11 lines · Liu Yi L
From: Lu Baolu <[email protected]>
These two helpers could be used when 1) the iommu group is singleton, or 2) the upper layer has put the iommu group into the secure state by calling
iommu_device_init_user_dma().As we want the iommufd design to be a device-centric model, we want to remove any group knowledge in iommufd. Given that we already have iommu_at[de]tach_device() interface, we could extend it for iommufd simply by doing below:
Quoted 6 lines
- first device in a group does group attach;
- last device in a group does group detach.as long as the group has been put into the secure context.
The commit <
426a273834eae> ("iommu: Limitiommu_attach/detach_device to
Quoted 9 lines
Quoted 4 lines
device with their own group") deliberately restricts the two interfaces to single-device group. To avoid the conflict with existing usages, we keep this policy and put the new extension only when the group has been marked for
user_dma.I still kind of hate this interface because it means an operation that appears to be explicitly on a single device has an implicit effect on other devices.
Quoted 7 lines
I still didn't get your concern why it's such a big deal. With this proposal the group restriction will be 'explicitly' documented in the attach uAPI comment and sample flow in
iommufd.rst. A sane user should read all those information to understand how this new sub-system works and follow whatever constraints claimed there. In the end the user should maintain the same group knowledge regardless of whether to use an explicit group uAPI or a device uAPI which has group constraint...
The first user might read this. Subsequent users are likely to just copy paste examples from earlier things without fully understanding them. In general documenting restrictions somewhere is never as effective as making those restrictions part of the interface signature itself.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 5 lines · David Gibson
The first user might read this. Subsequent users are likely to just copy paste examples from earlier things without fully understanding them. In general documenting restrictions somewhere is never as effective as making those restrictions part of the interface signature itself.
I'd think this argument would hold more water if you could point to someplace in existing userspace that cares about the VFIO grouping.
From what I see the applications do what the admin tells them to do - and if the admin says to use a certain VFIO device then that is excatly what they do. I don't know of any applications that ask the admin to tell them group information.
What I see is aligning what the kernel provides to the APIs the applications have already built.
Jason
Re: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · David Gibson
The first user might read this. Subsequent users are likely to just copy paste examples from earlier things without fully understanding them. In general documenting restrictions somewhere is never as effective as making those restrictions part of the interface signature itself.
I'd think this argument would hold more water if you could point to someplace in existing userspace that cares about the VFIO grouping.
My whole point here is that the proposed semantics mean that we have weird side effects even if the app doesn't think it cares about groups.
e.g. App's input is a bunch of PCI addresses for NICs. It attaches each one to a separate IOAS and bridges packets between them all. As far as the app is concerned, it doesn't care about groups, as you say.
Except that it breaks if any two of the devices are in the same group. Worse, it has a completely horrible failure mode: no syscall returns an, it just starts trying to do dma with device A, and the packets get written into the IOAS that belongs to device B instead. Sounds like a complete nightmare to debug if you don't know about groups, because you never thought you cared.
And yes, for a simple bridge like this app, attaching all the devices to the same IOAS is a more likely setup. But using an IOAS per device is a perfectly valid configuration as well, and with the current draft nothing will warn the app that this is a bad idea.
Quoted 10 lines
From what I see the applications do what the admin tells them to do - and if the admin says to use a certain VFIO device then that is excatly what they do. I don't know of any applications that ask the admin to tell them group information.
What I see is aligning what the kernel provides to the APIs the applications have already built.
Jason
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 16 lines · David Gibson
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · David Gibson
The first user might read this. Subsequent users are likely to just copy paste examples from earlier things without fully understanding them. In general documenting restrictions somewhere is never as effective as making those restrictions part of the interface signature itself.
I'd think this argument would hold more water if you could point to someplace in existing userspace that cares about the VFIO grouping.
My whole point here is that the proposed semantics mean that we have weird side effects even if the app doesn't think it cares about groups.
Quoted 6 lines
e.g. App's input is a bunch of PCI addresses for NICs. It attaches each one to a separate IOAS and bridges packets between them all. As far as the app is concerned, it doesn't care about groups, as you say.
Except that it breaks if any two of the devices are in the same group. Worse, it has a completely horrible failure mode: no syscall returns
Huh? If an app requests an IOAS attach that is not possible then the attachment IOCTL will fail.
The kernel must track groups and know that group A is on IOAS A and any further attach of a group A device must specify IOAS A or receive a failure.
The kernel should never blindly acknowledge a failed attachment.
Jason
Re: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 24 lines · Jason Gunthorpe
Quoted 16 lines · David Gibson
Quoted 10 lines · Jason Gunthorpe
Quoted 5 lines · David Gibson
The first user might read this. Subsequent users are likely to just copy paste examples from earlier things without fully understanding them. In general documenting restrictions somewhere is never as effective as making those restrictions part of the interface signature itself.
I'd think this argument would hold more water if you could point to someplace in existing userspace that cares about the VFIO grouping.
My whole point here is that the proposed semantics mean that we have weird side effects even if the app doesn't think it cares about groups.
Quoted 6 lines
e.g. App's input is a bunch of PCI addresses for NICs. It attaches each one to a separate IOAS and bridges packets between them all. As far as the app is concerned, it doesn't care about groups, as you say.
Except that it breaks if any two of the devices are in the same group. Worse, it has a completely horrible failure mode: no syscall returns
Quoted 6 lines
Huh? If an app requests an IOAS attach that is not possible then the attachment IOCTL will fail.
The kernel must track groups and know that group A is on IOAS A and any further attach of a group A device must specify IOAS A or receive a failure.
Ok, I misunderstood the semantics that were suggested.
So, IIUC what you're suggested is that if group X is attached to IOAS 1, then attaching the group to IOAS 1 again should succeed (as a no-op), but attaching to any other IOAS should fail?
That's certainly an improvement, but there's still some questions.
If you attach devices A and B (both in group X) to IOAS 1, then detach device A, what happens? Do you detach both devices? Or do you have a counter so you have to detach as many time as you attached?
Quoted 4 lines
The kernel should never blindly acknowledge a failed attachment.
Jason
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 3 lines · David Gibson
If you attach devices A and B (both in group X) to IOAS 1, then detach device A, what happens? Do you detach both devices? Or do you have a counter so you have to detach as many time as you attached?
I would refcount it since that is the only thing that makes semantic sense with the device centric model.
Jason
Re: [RFC 13/20] iommu: Extend iommu_at[de]tach_device() for multiple devices group
Quoted 7 lines · Jason Gunthorpe
Quoted 3 lines · David Gibson
If you attach devices A and B (both in group X) to IOAS 1, then detach device A, what happens? Do you detach both devices? Or do you have a counter so you have to detach as many time as you attached?
I would refcount it since that is the only thing that makes semantic sense with the device centric model.
Yes, I definitely think that's the better option here. This does still leave (at least) one weird edge case where the group structure can "leak" into the awareness of code that otherwise doesn't care, though it's definitely less nasty that the ones I mentioned before:
If an app wants to move a bunch of devices from one IOAS to another, it can do it either:
A)
for each dev:
detach dev from IOAS
for each dev:
attach dev to new IOAS
or B)
for each dev:
detach dev from IOAS
attach dev to new IOAS
With only singleton groups they're pretty much equivalent, but with multiple devices in a group, (B) will fail.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
RFC 14/20 iommu/iommufd: Add iommufd_device_[de]attach_ioasid()
In reply to Liu Yi LAn I/O address space takes effect in the iommu only after it's attached by a device. This patch provides iommufd_device_[de/at]tach_ioasid() helpers for this purpose. One device can be only attached to one ioasid at this point, but one ioasid can be attached by multiple devices.
The caller specifies the iommufd_device (returned at binding time) and the target ioasid when calling the helper function. Upon request, iommufd installs the specified I/O page table to the correct place in the IOMMU, according to the routing information (struct device* which represents RID) recorded in iommufd_device. Future variants could allow the caller to specify additional routing information (e.g. pasid/ssid) when multiple I/O address spaces are supported per device.
Open:
Per Jason's comment in below link, bus-specific wrappers are recommended. This RFC implements one wrapper for pci device. But it looks that struct pci_device is not used at all since iommufd_ device already carries all necessary info. So want to have another discussion on its necessity, e.g. whether making more sense to have bus-specific wrappers for binding, while leaving a common attaching helper per iommufd_device.
https://lore.kernel.org/linux-iommu/[email protected]/
TODO:
When multiple devices are attached to a same ioasid, the permitted iova ranges and supported pgsize bitmap on this ioasid should be a common subset of all attached devices. iommufd needs to track such info per ioasid and update it every time when a new device is attached to the ioasid. This has not been done in this version yet, due to the temporary hack adopted in patch 16-18. The hack reuses vfio type1 driver which already includes the necessary logic for iova ranges and pgsize bitmap. Once we get a clear direction for those patches, that logic will be moved to this patch.
--- drivers/iommu/iommufd/iommufd.c | 226 ++++++++++++++++++++++++++++++++ include/linux/iommufd.h | 29 ++++ 2 files changed, 255 insertions(+)
drivers/iommu/iommufd/iommufd.c +226 -0
@@ -51,6 +51,19 @@ struct iommufd_ioas {bool enforce_snoop;struct iommufd_ctx *ictx;refcount_t refs; +struct mutex lock; +struct list_head device_list; +struct iommu_domain *domain; +}; + +/* +* An ioas_device_info object is created per each successful attaching +* request. A list of objects are maintained per ioas when the address +* space is shared by multiple devices. +*/ +struct ioas_device_info { +struct iommufd_device *idev; +struct list_head next; };static int iommufd_fops_open (struct inode *inode,struct file *filep)
@@ -119,6 +132,21 @@ static void iommufd_ctx_put(struct iommufd_ctx *ictx)kfree (ictx); } +static struct iommufd_ioas *ioasid_get_ioas (struct iommufd_ctx *ictx,int ioasid) +{ +struct iommufd_ioas *ioas; + +if (ioasid <0 ) +return NULL ; + +mutex_lock (&ictx->lock); + ioas =xa_load (&ictx->ioasid_xa, ioasid); +if (ioas) +refcount_inc (&ioas->refs); +mutex_unlock (&ictx->lock); +return ioas; +} +/* Caller should hold ictx->lock */ static void ioas_put_locked (struct iommufd_ioas *ioas) {
@@ -128,11 +156,28 @@ static void ioas_put_locked(struct iommufd_ioas *ioas)if (!refcount_dec_and_test (&ioas->refs))return ; +WARN_ON (!list_empty (&ioas->device_list));xa_erase (&ictx->ioasid_xa, ioasid);iommufd_ctx_put (ictx);kfree (ioas); } +/* +* Caller should hold a ictx reference when calling this function +* otherwise ictx might be freed in ioas_put_locked() then the last +* unlock becomes problematic. Alternatively we could have a fresh +* implementation of ioas_put instead of calling the locked function. +* In this case it can ensure ictx is freed after mutext_unlock(). +*/ +static void ioas_put (struct iommufd_ioas *ioas) +{ +struct iommufd_ctx *ictx = ioas->ictx; + +mutex_lock (&ictx->lock); +ioas_put_locked (ioas); +mutex_unlock (&ictx->lock); +} +static int iommufd_ioasid_alloc (struct iommufd_ctx *ictx,unsigned long arg) {struct iommu_ioasid_alloc req;
@@ -178,6 +223,9 @@ static int iommufd_ioasid_alloc(struct iommufd_ctx *ictx, unsigned long arg)iommufd_ctx_get (ictx); ioas->ictx = ictx; +mutex_init (&ioas->lock); +INIT_LIST_HEAD (&ioas->device_list); +refcount_set (&ioas->refs,1 );return ioasid;
@@ -344,6 +392,166 @@ static struct miscdevice iommu_misc_dev = { .mode = 0666, }; +/* Caller should hold ioas->lock */ +static struct ioas_device_info *ioas_find_device (struct iommufd_ioas *ioas, +struct iommufd_device *idev) +{ +struct ioas_device_info *ioas_dev; + +list_for_each_entry (ioas_dev, &ioas->device_list, next) { +if (ioas_dev->idev == idev) +return ioas_dev; + } + +return NULL ; +} + +static void ioas_free_domain_if_empty (struct iommufd_ioas *ioas) +{ +if (list_empty (&ioas->device_list)) { +iommu_domain_free (ioas->domain); + ioas->domain =NULL ; + } +} + +static int ioas_check_device_compatibility (struct iommufd_ioas *ioas, +struct device *dev) +{ +bool snoop = false; +u32 addr_width; +int ret; + +/* +* currently we only support I/O page table with iommu enforce-snoop +* format. Attaching a device which doesn't support this format in its +* upstreaming iommu is rejected. +*/ + ret =iommu_device_get_info (dev,IOMMU_DEV_INFO_FORCE_SNOOP , &snoop); +if (ret || !snoop) +return -EINVAL ; + + ret =iommu_device_get_info (dev,IOMMU_DEV_INFO_ADDR_WIDTH , &addr_width); +if (ret || addr_width < ioas->addr_width) +return -EINVAL ; + +/* TODO: also need to check permitted iova ranges and pgsize bitmap */ + +return 0 ; +} + +/** +* iommufd_device_attach_ioasid - attach device to an ioasid +* @idev: [in] Pointer to struct iommufd_device. +* @ioasid: [in] ioasid points to an I/O address space. +* +* Returns 0 for successful attach, otherwise returns error. +* +*/ +int iommufd_device_attach_ioasid (struct iommufd_device *idev,int ioasid) +{ +struct iommufd_ioas *ioas; +struct ioas_device_info *ioas_dev; +struct iommu_domain *domain; +int ret; + + ioas =ioasid_get_ioas (idev->ictx, ioasid); +if (!ioas) { +pr_err_ratelimited ("Trying to attach illegal or unkonwn IOASID %u\n" , ioasid); +return -EINVAL ; + } + +mutex_lock (&ioas->lock); + +/* Check for duplicates */ +if (ioas_find_device (ioas, idev)) { + ret = -EINVAL ; + goto out_unlock; + } + + ret =ioas_check_device_compatibility (ioas, idev->dev); +if (ret) + goto out_unlock; + + ioas_dev =kzalloc (sizeof (*ioas_dev),GFP_KERNEL ); +if (!ioas_dev) { + ret = -ENOMEM ; + goto out_unlock; + } + +/* +* Each ioas is backed by an iommu domain, which is allocated +* when the ioas is attached for the first time and then shared +* by following devices. +*/ +if (list_empty (&ioas->device_list)) { +struct iommu_domain *d; + + d =iommu_domain_alloc (idev->dev->bus); +if (!d) { + ret = -ENOMEM ; + goto out_free; + } + ioas->domain = d; + } + domain = ioas->domain; + +/* Install the I/O page table to the iommu for this device */ + ret =iommu_attach_device (domain, idev->dev); +if (ret) + goto out_domain; + + ioas_dev->idev = idev; +list_add (&ioas_dev->next, &ioas->device_list); +mutex_unlock (&ioas->lock); + +return 0 ; +out_domain: +ioas_free_domain_if_empty (ioas); +out_free: +kfree (ioas_dev); +out_unlock: +mutex_unlock (&ioas->lock); +ioas_put (ioas); + +return ret; +} +EXPORT_SYMBOL_GPL (iommufd_device_attach_ioasid); + +/** +* iommufd_device_detach_ioasid - Detach an ioasid from a device. +* @idev: [in] Pointer to struct iommufd_device. +* @ioasid: [in] ioasid points to an I/O address space. +* +*/ +void iommufd_device_detach_ioasid (struct iommufd_device *idev,int ioasid) +{ +struct iommufd_ioas *ioas; +struct ioas_device_info *ioas_dev; + + ioas =ioasid_get_ioas (idev->ictx, ioasid); +if (!ioas) +return ; + +mutex_lock (&ioas->lock); + ioas_dev =ioas_find_device (ioas, idev); +if (!ioas_dev) { +mutex_unlock (&ioas->lock); + goto out; + } + +list_del (&ioas_dev->next); +iommu_detach_device (ioas->domain, idev->dev); +ioas_free_domain_if_empty (ioas); +kfree (ioas_dev); +mutex_unlock (&ioas->lock); + +/* release the reference acquired at the start of this function */ +ioas_put (ioas); +out: +ioas_put (ioas); +} +EXPORT_SYMBOL_GPL (iommufd_device_detach_ioasid); +/** * iommufd_bind_device - Bind a physical device marked by a device * cookie to an iommu fd.
@@ -426,8 +634,26 @@ EXPORT_SYMBOL_GPL(iommufd_bind_device);void iommufd_unbind_device (struct iommufd_device *idev) {struct iommufd_ctx *ictx = idev->ictx; +struct iommufd_ioas *ioas; +unsigned long index;mutex_lock (&ictx->lock); +xa_for_each (&ictx->ioasid_xa, index, ioas) { +struct ioas_device_info *ioas_dev; + +mutex_lock (&ioas->lock); + ioas_dev =ioas_find_device (ioas, idev); +if (!ioas_dev) { +mutex_unlock (&ioas->lock); +continue ; + } +list_del (&ioas_dev->next); +iommu_detach_device (ioas->domain, idev->dev); +ioas_free_domain_if_empty (ioas); +kfree (ioas_dev); +mutex_unlock (&ioas->lock); +ioas_put_locked (ioas); + }xa_erase (&ictx->device_xa, idev->id);mutex_unlock (&ictx->lock);/* Exit the security context */
include/linux/iommufd.h +29 -0
@@ -13,6 +13,7 @@#include <linux/errno.h> #include <linux/err.h> #include <linux/device.h> +#include <linux/pci.h> #define IOMMUFD_IOASID_MAX ((unsigned int)(0x7FFFFFFF))#define IOMMUFD_IOASID_MIN 0
@@ -27,6 +28,16 @@ struct iommufd_device * iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie);void iommufd_unbind_device (struct iommufd_device *idev); +int iommufd_device_attach_ioasid (struct iommufd_device *idev,int ioasid); +void iommufd_device_detach_ioasid (struct iommufd_device *idev,int ioasid); + +static inline int +__pci_iommufd_device_attach_ioasid (struct pci_dev *pdev, +struct iommufd_device *idev,int ioasid) +{ +return iommufd_device_attach_ioasid (idev, ioasid); +} +#else /* !CONFIG_IOMMUFD */ static inline struct iommufd_device *iommufd_bind_device (int fd,struct device *dev,u64 dev_cookie)
@@ -37,5 +48,23 @@ iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie)static inline void iommufd_unbind_device (struct iommufd_device *idev) { } + +static inline int iommufd_device_attach_ioasid (struct iommufd_device *idev, +int ioasid) +{ +return -ENODEV ; +} + +static inline void iommufd_device_detach_ioasid (struct iommufd_device *idev, +int ioasid) +{ +} + +static inline int +__pci_iommufd_device_attach_ioasid (struct pci_dev *pdev, +struct iommufd_device *idev,int ioasid) +{ +return -ENODEV ; +}#endif /* CONFIG_IOMMUFD */ #endif /* __LINUX_IOMMUFD_H */
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 14/20] iommu/iommufd: Add iommufd_device_[de]attach_ioasid()
Quoted 42 lines · Liu Yi L
An I/O address space takes effect in the iommu only after it's attached by a device. This patch provides iommufd_device_[de/at]tach_ioasid() helpers for this purpose. One device can be only attached to one ioasid at this point, but one ioasid can be attached by multiple devices.
The caller specifies the
iommufd_device(returned at binding time) and the target ioasid when calling the helper function. Upon request, iommufd installs the specified I/O page table to the correct place in the IOMMU, according to the routing information (struct device* which represents RID) recorded iniommufd_device. Future variants could allow the caller to specify additional routing information (e.g. pasid/ssid) when multiple I/O address spaces are supported per device.Open:
Per Jason's comment in below link, bus-specific wrappers are recommended. This RFC implements one wrapper for pci device. But it looks that structpci_deviceis not used at all sinceiommufd_device already carries all necessary info. So want to have another discussion on its necessity, e.g. whether making more sense to have bus-specific wrappers for binding, while leaving a common attaching helper periommufd_device.
https://lore.kernel.org/linux-iommu/[email protected]/TODO:
When multiple devices are attached to a same ioasid, the permitted iova ranges and supported pgsize bitmap on this ioasid should be a common subset of all attached devices. iommufd needs to track such info per ioasid and update it every time when a new device is attached to the ioasid. This has not been done in this version yet, due to the temporary hack adopted in patch 16-18. The hack reuses vfio type1 driver which already includes the necessary logic for iova ranges and pgsize bitmap. Once we get a clear direction for those patches, that logic will be moved to this patch.Signed-off-by Liu Yi L <[email protected]>drivers/iommu/iommufd/iommufd.c | 226 ++++++++++++++++++++++++++++++++ include/linux/iommufd.h | 29 ++++ 2 files changed, 255 insertions(+)drivers/iommu/iommufd/iommufd.c +1
-0+++ b/drivers/iommu/iommufd/iommufd.c
drivers/iommu/iommufd/iommufd.c struct iommufd_ioas { · line 56
Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +3
-0@@ -51,6 +51,19 @@ struct iommufd_ioas {bool enforce_snoop;struct iommufd_ctx *ictx;refcount_t refs; +struct mutex lock; +struct list_head device_list; +struct iommu_domain *domain;
This should just be another xarray indexed by the device id
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 404
Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10
-0+/* Caller should hold ioas->lock */ +static struct ioas_device_info *ioas_find_device (struct iommufd_ioas *ioas, +struct iommufd_device *idev) +{ +struct ioas_device_info *ioas_dev; + +list_for_each_entry (ioas_dev, &ioas->device_list, next) { +if (ioas_dev->idev == idev) +return ioas_dev; + }
Which eliminates this search. xarray with tightly packed indexes is generally more efficient than linked lists..
Quoted 14 lines
drivers/iommu/iommufd/iommufd.c +14
-0+static int ioas_check_device_compatibility (struct iommufd_ioas *ioas, +struct device *dev) +{ +bool snoop = false; +u32 addr_width; +int ret; + +/* +* currently we only support I/O page table with iommu enforce-snoop +* format. Attaching a device which doesn't support this format in its +* upstreaming iommu is rejected. +*/ + ret =iommu_device_get_info (dev,IOMMU_DEV_INFO_FORCE_SNOOP , &snoop); +if (ret || !snoop)
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 440
Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10
-0+return -EINVAL ; + + ret =iommu_device_get_info (dev,IOMMU_DEV_INFO_ADDR_WIDTH , &addr_width); +if (ret || addr_width < ioas->addr_width) +return -EINVAL ; + +/* TODO: also need to check permitted iova ranges and pgsize bitmap */ + +return 0 ; +}
This seems kind of weird..
I expect the iommufd to hold a SW copy of the IO page table and each time a new domain is to be created it should push the SW copy into the domain. If the domain cannot support it then the domain driver should naturally fail a request.
When the user changes the IO page table the SW copy is updated then all of the domains are updated too - again if any domain cannot support the change then it fails and the change is rolled back.
It seems like this is a side effect of roughly hacking in the vfio code?
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 450
Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10
-0+ +/** +* iommufd_device_attach_ioasid - attach device to an ioasid +* @idev: [in] Pointer to struct iommufd_device. +* @ioasid: [in] ioasid points to an I/O address space. +* +* Returns 0 for successful attach, otherwise returns error. +* +*/ +int iommufd_device_attach_ioasid (struct iommufd_device *idev,int ioasid)
Types for the ioas_id again..
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 460
Quoted 10 lines
drivers/iommu/iommufd/iommufd.c +10
-0+{ +struct iommufd_ioas *ioas; +struct ioas_device_info *ioas_dev; +struct iommu_domain *domain; +int ret; + + ioas =ioasid_get_ioas (idev->ictx, ioasid); +if (!ioas) { +pr_err_ratelimited ("Trying to attach illegal or unkonwn IOASID %u\n" , ioasid); +return -EINVAL ;
No prints triggered by bad userspace
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 469
Quoted 9 lines
drivers/iommu/iommufd/iommufd.c +9
-0+ } + +mutex_lock (&ioas->lock); + +/* Check for duplicates */ +if (ioas_find_device (ioas, idev)) { + ret = -EINVAL ; + goto out_unlock; + }
just xa_cmpxchg NULL, XA_ZERO_ENTRY
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 486
Quoted 6 lines
drivers/iommu/iommufd/iommufd.c +6
-0+/* +* Each ioas is backed by an iommu domain, which is allocated +* when the ioas is attached for the first time and then shared +* by following devices. +*/ +if (list_empty (&ioas->device_list)) {
Seems strange, what if the devices are forced to have different domains? We don't want to model that in the SW layer..
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 501
Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4
-0+/* Install the I/O page table to the iommu for this device */ + ret =iommu_attach_device (domain, idev->dev); +if (ret) + goto out_domain;
This is where things start to get confusing when you talk about PASID as the above call needs to be some PASID centric API.
drivers/iommu/iommufd/iommufd.c struct iommufd_device * · line 39
Quoted 13 lines
drivers/iommu/iommufd/iommufd.c +9
-0@@ -27,6 +28,16 @@ struct iommufd_device * iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie);void iommufd_unbind_device (struct iommufd_device *idev); +int iommufd_device_attach_ioasid (struct iommufd_device *idev,int ioasid); +void iommufd_device_detach_ioasid (struct iommufd_device *idev,int ioasid); + +static inline int +__pci_iommufd_device_attach_ioasid (struct pci_dev *pdev, +struct iommufd_device *idev,int ioasid) +{ +return iommufd_device_attach_ioasid (idev, ioasid); +}
If think sis taking in the iommfd_device then there isn't a logical place to signal the PCIness
But, I think the API should at least have a kdoc that this is capturing the entire device and specify that for PCI this means all TLPs with the RID.
Jason
Tian, Kevin
maintainerRE: [RFC 14/20] iommu/iommufd: Add iommufd_device_[de]attach_ioasid()
Quoted 39 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 2:02 AMdrivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = {
Quoted 9 lines
drivers/iommu/iommufd/iommufd.c +9
-0+static int ioas_check_device_compatibility (struct iommufd_ioas *ioas, +struct device *dev) +{ +bool snoop = false; +u32 addr_width; +int ret; + +/* +* currently we only support I/O page table with iommu enforce- snoop
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = {
Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4
-0+* format. Attaching a device which doesn't support this format in its +* upstreaming iommu is rejected. +*/ + ret = iommu_device_get_info(dev,
IOMMU_DEV_INFO_FORCE_SNOOP, &snoop);drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = {
Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4
-0+if (ret || !snoop) +return -EINVAL ; + + ret = iommu_device_get_info(dev,
IOMMU_DEV_INFO_ADDR_WIDTH,&addr_width);drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = {
Quoted 4 lines
drivers/iommu/iommufd/iommufd.c +4
-0+if (ret || addr_width < ioas->addr_width) +return -EINVAL ; + +/* TODO: also need to check permitted iova ranges and pgsize bitmap */
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 440
Quoted 3 lines
drivers/iommu/iommufd/iommufd.c +3
-0+ +return 0 ; +}This seems kind of weird..
I expect the iommufd to hold a SW copy of the IO page table and each time a new domain is to be created it should push the SW copy into the domain. If the domain cannot support it then the domain driver should naturally fail a request.
Quoted 6 lines
When the user changes the IO page table the SW copy is updated then all of the domains are updated too - again if any domain cannot support the change then it fails and the change is rolled back.
It seems like this is a side effect of roughly hacking in the vfio code?
Actually this was one open we closed in previous design proposal, but looks you have a different thought now.
vfio maintains one ioas per container. Devices in the container can be attached to different domains (e.g. due to snoop format). Every time when the ioas is updated, every attached domain is updated in accordance.
You recommended one-ioas-one-domain model instead, i.e. any device with a format incompatible with the one currently used in ioas has to be attached to a new ioas, even if the two ioas's have the same mapping. This leads to compatibility check at attaching time.
Now you want returning back to the vfio model?
Quoted 10 lines
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 486
Quoted 7 lines
drivers/iommu/iommufd/iommufd.c +6
-0+/* +* Each ioas is backed by an iommu domain, which is allocated +* when the ioas is attached for the first time and then shared +* by following devices. +*/ +if (list_empty (&ioas->device_list)) {Seems strange, what if the devices are forced to have different domains? We don't want to model that in the SW layer..
this is due to above background
Quoted 8 lines
drivers/iommu/iommufd/iommufd.c static struct miscdevice iommu_misc_dev = { · line 501
Quoted 5 lines
drivers/iommu/iommufd/iommufd.c +4
-0+/* Install the I/O page table to the iommu for this device */ + ret =iommu_attach_device (domain, idev->dev); +if (ret) + goto out_domain;This is where things start to get confusing when you talk about PASID as the above call needs to be some PASID centric API.
yes, for pasid new api (e.g. iommu_attach_device_pasid()) will be added.
but here we only talk about physical device, and iommu_attach_device() is only for physical device.
Quoted 9 lines
include/linux/iommufd.h struct iommufd_device * · line 31
Quoted 6 lines
include/linux/iommufd.h +1
-0@@ -27,6 +28,16 @@ struct iommufd_device * iommufd_bind_device(int fd, struct device *dev, u64 dev_cookie);void iommufd_unbind_device (struct iommufd_device *idev); +int iommufd_device_attach_ioasid(struct iommufd_device *idev, intioasid);
Review struct iommufd_device * · line 32
Quoted 1 line
(quoted diff) +1
-0+void iommufd_device_detach_ioasid(struct iommufd_device *idev, intioasid);
Quoted 10 lines
include/linux/iommufd.h struct iommufd_device * · line 39
Quoted 7 lines
include/linux/iommufd.h +7
-0+ +static inline int +__pci_iommufd_device_attach_ioasid (struct pci_dev *pdev, +struct iommufd_device *idev,int ioasid) +{ +return iommufd_device_attach_ioasid (idev, ioasid); +}If think sis taking in the
iommfd_devicethen there isn't a logical place to signal the PCIness
can you elaborate?
Quoted 5 lines
But, I think the API should at least have a kdoc that this is capturing the entire device and specify that for PCI this means all TLPs with the RID.
yes, this should be documented.
Jason Gunthorpe
maintainerRe: [RFC 14/20] iommu/iommufd: Add iommufd_device_[de]attach_ioasid()
Quoted 8 lines · Kevin
Actually this was one open we closed in previous design proposal, but looks you have a different thought now.
vfio maintains one ioas per container. Devices in the container can be attached to different domains (e.g. due to snoop format). Every time when the ioas is updated, every attached domain is updated in accordance.
Quoted 6 lines
You recommended one-ioas-one-domain model instead, i.e. any device with a format incompatible with the one currently used in ioas has to be attached to a new ioas, even if the two ioas's have the same mapping. This leads to compatibility check at attaching time.
Now you want returning back to the vfio model?
Oh, I thought we circled back again.. If we are all OK with one ioas one domain then great.
Quoted 4 lines
Quoted 2 lines
If think sis taking in the
iommfd_devicethen there isn't a logical place to signal the PCInesscan you elaborate?
I mean just drop it and document it.
Jason
Tian, Kevin
maintainerRE: [RFC 14/20] iommu/iommufd: Add iommufd_device_[de]attach_ioasid()
Quoted 18 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:57 PMQuoted 13 lines · Kevin
Actually this was one open we closed in previous design proposal, but looks you have a different thought now.
vfio maintains one ioas per container. Devices in the container can be attached to different domains (e.g. due to snoop format). Every time when the ioas is updated, every attached domain is updated in accordance.
You recommended one-ioas-one-domain model instead, i.e. any device with a format incompatible with the one currently used in ioas has to be attached to a new ioas, even if the two ioas's have the same mapping. This leads to compatibility check at attaching time.
Quoted 4 lines
Quoted 1 line
Now you want returning back to the vfio model?
Oh, I thought we circled back again.. If we are all OK with one ioas one domain then great.
yes, at least I haven't seen a blocking issue with this assumption. Later when converting vfio type1 into a shim, it could create multiple ioas's if container would have a list of domains before the shim.
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
If think sis taking in the
iommfd_devicethen there isn't a logical place to signal the PCInesscan you elaborate?
I mean just drop it and document it.
got you
RFC 15/20 vfio/pci: Add VFIO_DEVICE_[DE]ATTACH_IOASID
In reply to Liu Yi LThis patch adds interface for userspace to attach device to specified IOASID.
Note:
One device can only be attached to one IOASID in this version. This is on par with what vfio provides today. In the future this restriction can be relaxed when multiple I/O address spaces are supported per device
--- drivers/vfio/pci/vfio_pci.c | 82 +++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_private.h | 1 + include/linux/iommufd.h | 1 + include/uapi/linux/vfio.h | 26 +++++++++ 4 files changed, 110 insertions(+)
drivers/vfio/pci/vfio_pci.c +82 -0
@@ -557,6 +557,11 @@ static void vfio_pci_release(struct vfio_device *core_vdev)if (vdev->videv) {struct vfio_iommufd_device *videv = vdev->videv; +if (videv->ioasid !=IOMMUFD_INVALID_IOASID ) { +iommufd_device_detach_ioasid (videv->idev, + videv->ioasid); + videv->ioasid =IOMMUFD_INVALID_IOASID ; + } vdev->videv =NULL ;iommufd_unbind_device (videv->idev);kfree (videv);
@@ -839,6 +844,7 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev, } videv->idev = idev; videv->iommu_fd = bind_data.iommu_fd; + videv->ioasid =IOMMUFD_INVALID_IOASID ;/* * A security context has been established. Unblock * user access.
@@ -848,6 +854,82 @@ static long vfio_pci_ioctl(struct vfio_device *core_vdev, vdev->videv = videv;mutex_unlock (&vdev->videv_lock); +return 0 ; + }else if (cmd ==VFIO_DEVICE_ATTACH_IOASID ) { +struct vfio_device_attach_ioasid attach; +unsigned long minsz; +struct vfio_iommufd_device *videv; +int ret =0 ; + +/* not allowed if the device is opened in legacy interface */ +if (vfio_device_in_container (core_vdev)) +return -ENOTTY ; + + minsz = offsetofend(struct vfio_device_attach_ioasid, ioasid); +if (copy_from_user (&attach, (void __user *)arg, minsz)) +return -EFAULT ; + +if (attach.argsz < minsz || attach.flags || + attach.iommu_fd <0 || attach.ioasid <0 ) +return -EINVAL ; + +mutex_lock (&vdev->videv_lock); + + videv = vdev->videv; +if (!videv || videv->iommu_fd != attach.iommu_fd) { +mutex_unlock (&vdev->videv_lock); +return -EINVAL ; + } + +/* Currently only allows one IOASID attach */ +if (videv->ioasid !=IOMMUFD_INVALID_IOASID ) { +mutex_unlock (&vdev->videv_lock); +return -EBUSY ; + } + + ret =__pci_iommufd_device_attach_ioasid (vdev->pdev, + videv->idev, + attach.ioasid); +if (!ret) + videv->ioasid = attach.ioasid; +mutex_unlock (&vdev->videv_lock); + +return ret; + }else if (cmd ==VFIO_DEVICE_DETACH_IOASID ) { +struct vfio_device_attach_ioasid attach; +unsigned long minsz; +struct vfio_iommufd_device *videv; + +/* not allowed if the device is opened in legacy interface */ +if (vfio_device_in_container (core_vdev)) +return -ENOTTY ; + + minsz = offsetofend(struct vfio_device_attach_ioasid, ioasid); +if (copy_from_user (&attach, (void __user *)arg, minsz)) +return -EFAULT ; + +if (attach.argsz < minsz || attach.flags || + attach.iommu_fd <0 || attach.ioasid <0 ) +return -EINVAL ; + +mutex_lock (&vdev->videv_lock); + + videv = vdev->videv; +if (!videv || videv->iommu_fd != attach.iommu_fd) { +mutex_unlock (&vdev->videv_lock); +return -EINVAL ; + } + +if (videv->ioasid ==IOMMUFD_INVALID_IOASID || + videv->ioasid != attach.ioasid) { +mutex_unlock (&vdev->videv_lock); +return -EINVAL ; + } + + videv->ioasid =IOMMUFD_INVALID_IOASID ; +iommufd_device_detach_ioasid (videv->idev, attach.ioasid); +mutex_unlock (&vdev->videv_lock); +return 0 ; }else if (cmd ==VFIO_DEVICE_GET_INFO ) {struct vfio_device_info info;
drivers/vfio/pci/vfio_pci_private.h +1 -0
@@ -103,6 +103,7 @@ struct vfio_pci_mmap_vma {struct vfio_iommufd_device {struct iommufd_device *idev;int iommu_fd; +int ioasid; };struct vfio_pci_device {
include/linux/iommufd.h +1 -0
@@ -17,6 +17,7 @@#define IOMMUFD_IOASID_MAX ((unsigned int)(0x7FFFFFFF))#define IOMMUFD_IOASID_MIN 0 +#define IOMMUFD_INVALID_IOASID -1#define IOMMUFD_DEVID_MAX ((unsigned int)(0x7FFFFFFF))#define IOMMUFD_DEVID_MIN 0
include/uapi/linux/vfio.h +26 -0
@@ -220,6 +220,32 @@ struct vfio_device_iommu_bind_data {#define VFIO_DEVICE_BIND_IOMMUFD _IO(VFIO_TYPE, VFIO_BASE + 19) +/* +* VFIO_DEVICE_ATTACH_IOASID - _IOW(VFIO_TYPE, VFIO_BASE + 21, +* struct vfio_device_attach_ioasid) +* +* Attach a vfio device to the specified IOASID +* +* Multiple vfio devices can be attached to the same IOASID. One device can +* be attached to only one ioasid at this point. +* +* @argsz: user filled size of this data. +* @flags: reserved for future extension. +* @iommu_fd: iommufd where the ioasid comes from. +* @ioasid: target I/O address space. +* +* Return: 0 on success, -errno on failure. +*/ +struct vfio_device_attach_ioasid { +__u32 argsz; +__u32 flags; +__s32 iommu_fd; +__s32 ioasid; +}; + +#define VFIO_DEVICE_ATTACH_IOASID _IO(VFIO_TYPE, VFIO_BASE + 20) +#define VFIO_DEVICE_DETACH_IOASID _IO(VFIO_TYPE, VFIO_BASE + 21) +/** * VFIO_DEVICE_GET_INFO - _IOR(VFIO_TYPE, VFIO_BASE + 7, * struct vfio_device_info)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 15/20] vfio/pci: Add VFIO_DEVICE_[DE]ATTACH_IOASID
Quoted 7 lines · Liu Yi L
This patch adds interface for userspace to attach device to specified IOASID.
Note:
One device can only be attached to one IOASID in this version. This is on par with what vfio provides today. In the future this restriction can be relaxed when multiple I/O address spaces are supported per device
?? In VFIO the container is the IOS and the container can be shared with multiple devices. This needs to start at about the same functionality.
drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 858
Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1
-0+ }else if (cmd ==VFIO_DEVICE_ATTACH_IOASID ) {
This should be in the core code, right? There is nothing PCI specific here.
Jason
Tian, Kevin
maintainerRE: [RFC 15/20] vfio/pci: Add VFIO_DEVICE_[DE]ATTACH_IOASID
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 2:04 AMQuoted 3 lines · Liu Yi L
This patch adds interface for userspace to attach device to specified IOASID.
Quoted 8 lines
Quoted 4 lines
Note:
One device can only be attached to one IOASID in this version. This is on par with what vfio provides today. In the future this restriction can be relaxed when multiple I/O address spaces are supported per device?? In VFIO the container is the IOS and the container can be shared with multiple devices. This needs to start at about the same functionality.
a device can be only attached to one container. One container can be shared by multiple devices.
a device can be only attached to one IOASID. One IOASID can be shared by multiple devices.
it does start at the same functionality.
Quoted 6 lines
drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 858
Quoted 2 lines
drivers/vfio/pci/vfio_pci.c +1
-0+ }else if (cmd ==VFIO_DEVICE_ATTACH_IOASID ) {This should be in the core code, right? There is nothing PCI specific here.
but if you insist on a pci-wrapper attach function, we still need something here (e.g. with .attach_ioasid() callback)?
Jason Gunthorpe
maintainerRe: [RFC 15/20] vfio/pci: Add VFIO_DEVICE_[DE]ATTACH_IOASID
Quoted 25 lines · Kevin
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 2:04 AMQuoted 3 lines · Liu Yi L
This patch adds interface for userspace to attach device to specified IOASID.
Quoted 8 lines
Quoted 4 lines
Note:
One device can only be attached to one IOASID in this version. This is on par with what vfio provides today. In the future this restriction can be relaxed when multiple I/O address spaces are supported per device?? In VFIO the container is the IOS and the container can be shared with multiple devices. This needs to start at about the same functionality.
a device can be only attached to one container. One container can be shared by multiple devices.
a device can be only attached to one IOASID. One IOASID can be shared by multiple devices.
it does start at the same functionality.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 858
Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1
-0+ }else if (cmd ==VFIO_DEVICE_ATTACH_IOASID ) {This should be in the core code, right? There is nothing PCI specific here.
but if you insist on a pci-wrapper attach function, we still need something here (e.g. with .attach_ioasid() callback)?
I would like to stop adding ioctls to this switch, the core code should decode the ioctl and call an per-ioctl op like every other subsystem does..
If you do that then you could have an op
.attach_ioasid = vfio_full_device_attach,
And that is it for driver changes.
Every driver that use type1 today should be updated to have the above line and will work with iommufd. mdevs will not be updated and won't work.
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Tian, Kevin
maintainerRE: [RFC 15/20] vfio/pci: Add VFIO_DEVICE_[DE]ATTACH_IOASID
Quoted 44 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 8:59 PMQuoted 25 lines · Kevin
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 2:04 AMQuoted 3 lines · Liu Yi L
This patch adds interface for userspace to attach device to specified IOASID.
Quoted 8 lines
Quoted 4 lines
Note:
One device can only be attached to one IOASID in this version. This is on par with what vfio provides today. In the future this restriction can be relaxed when multiple I/O address spaces are supported per device?? In VFIO the container is the IOS and the container can be shared with multiple devices. This needs to start at about the same functionality.
a device can be only attached to one container. One container can be shared by multiple devices.
a device can be only attached to one IOASID. One IOASID can be shared by multiple devices.
it does start at the same functionality.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
drivers/vfio/pci/vfio_pci.c static long vfio_pci_ioctl(struct vfio_device *core_vdev, · line 858
Quoted 1 line
drivers/vfio/pci/vfio_pci.c +1
-0+ }else if (cmd ==VFIO_DEVICE_ATTACH_IOASID ) {This should be in the core code, right? There is nothing PCI specific here.
but if you insist on a pci-wrapper attach function, we still need something here (e.g. with .attach_ioasid() callback)?
I would like to stop adding ioctls to this switch, the core code should decode the ioctl and call an per-ioctl op like every other subsystem does..
If you do that then you could have an op
Quoted 8 lines
.attach_ioasid =
vfio_full_device_attach,And that is it for driver changes.
Every driver that use type1 today should be updated to have the above line and will work with iommufd. mdevs will not be updated and won't work.
will do.
RFC 16/20 vfio/type1: Export symbols for dma [un]map code sharing
In reply to Liu Yi L[HACK. will fix in v2]
There are two options to impelement vfio type1v2 mapping semantics in /dev/iommu.
One is to duplicate the related code from vfio as the starting point, and then merge with vfio type1 at a later time. However vfio_iommu_type1.c has over 3000LOC with ~80% related to dma management logic, including:
- the dma map/unmap metadata management
- page pinning, and related accounting
- iova range reporting
- dirty bitmap retrieving
- dynamic vaddr update, etc.
Not sure whether duplicating such amount of code in the transition phase is acceptable.
The alternative is to consolidate type1v2 logic in /dev/iommu immediately, which requires converting vfio_iommu_type1 to be a shim driver. The upside is no code duplication and it is anyway the long-term goal even with the first approach. The downside is that more effort is required for the 'initial' skeleton thus all new iommu features will be blocked for a longer time. Main task is to figure out how to handle the remaining 20% code (tied with group) in vfio_iommu_type1 with device-centric model in iommufd (with group managed by iommu core). It also implies that no-snoop DMA must be handled now with extra work on reworked kvm-vfio contract. and also need to support external page pinning as required by sw mdev.
Due to limited time, we choose a hacky approach in this RFC by directly calling vfio_iommu_type1 functions in iommufd and raising this open for discussion. This should not impact the review on other key aspects of the new framework. Once we reach consensus, we'll follow it to do a clean implementation 'in' next version.
--- drivers/vfio/vfio_iommu_type1.c | 199 +++++++++++++++++++++++++++++++- include/linux/vfio.h | 13 +++ 2 files changed, 206 insertions(+), 6 deletions(-)
drivers/vfio/vfio_iommu_type1.c +193 -6
@@ -115,6 +115,7 @@ struct vfio_iommu_group {struct list_head next;bool mdev_group;/* An mdev group */ bool pinned_page_dirty_scope; +int attach_cnt; };struct vfio_iova {
@@ -2240,6 +2241,135 @@ static void vfio_iommu_iova_insert_copy(struct vfio_iommu *iommu,list_splice_tail (iova_copy, iova); } +/* HACK: called by /dev/iommu core to init group to vfio_iommu_type1 */ +int vfio_iommu_add_group (struct vfio_iommu *iommu, +struct iommu_group *iommu_group, +struct iommu_domain *iommu_domain) +{ +struct vfio_iommu_group *group; +struct vfio_domain *domain =NULL ; +struct bus_type *bus =NULL ; +int ret =0 ; +bool resv_msi, msi_remap; +phys_addr_t resv_msi_base =0 ; +struct iommu_domain_geometry *geo; +LIST_HEAD (iova_copy); +LIST_HEAD (group_resv_regions); + +/* Determine bus_type */ + ret =iommu_group_for_each_dev (iommu_group, &bus, vfio_bus_type); +if (ret) +return ret; + +mutex_lock (&iommu->lock); + +/* Check for duplicates */ + group =vfio_iommu_find_iommu_group (iommu, iommu_group); +if (group) { + group->attach_cnt++; +mutex_unlock (&iommu->lock); +return 0 ; + } + +/* Get aperture info */ + geo = &iommu_domain->geometry; +if (vfio_iommu_aper_conflict (iommu, geo->aperture_start, + geo->aperture_end)) { + ret = -EINVAL ; + goto out_free; + } + + ret =iommu_get_group_resv_regions (iommu_group, &group_resv_regions); +if (ret) + goto out_free; + +if (vfio_iommu_resv_conflict (iommu, &group_resv_regions)) { + ret = -EINVAL ; + goto out_free; + } + +/* +* We don't want to work on the original iova list as the list +* gets modified and in case of failure we have to retain the +* original list. Get a copy here. +*/ + ret =vfio_iommu_iova_get_copy (iommu, &iova_copy); +if (ret) + goto out_free; + + ret =vfio_iommu_aper_resize (&iova_copy, geo->aperture_start, + geo->aperture_end); +if (ret) + goto out_free; + + ret =vfio_iommu_resv_exclude (&iova_copy, &group_resv_regions); +if (ret) + goto out_free; + + resv_msi =vfio_iommu_has_sw_msi (&group_resv_regions, &resv_msi_base); + + msi_remap =irq_domain_check_msi_remap () || +iommu_capable (bus,IOMMU_CAP_INTR_REMAP ); + +if (!allow_unsafe_interrupts && !msi_remap) { +pr_warn ("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n" , + __func__); + ret = -EPERM ; + goto out_free; + } + +if (resv_msi) { + ret =iommu_get_msi_cookie (iommu_domain, resv_msi_base); +if (ret && ret != -ENODEV ) + goto out_free; + } + + group =kzalloc (sizeof (*group),GFP_KERNEL ); +if (!group) { + ret = -ENOMEM ; + goto out_free; + } + + group->iommu_group = iommu_group; + +if (!list_empty (&iommu->domain_list)) { + domain =list_first_entry (&iommu->domain_list, + struct vfio_domain, next); + }else { + domain =kzalloc (sizeof (*domain),GFP_KERNEL ); +if (!domain) { +kfree (group); + ret = -ENOMEM ; + goto out_free; + } + domain->domain = iommu_domain; +INIT_LIST_HEAD (&domain->group_list); +list_add (&domain->next, &iommu->domain_list); + } + +list_add (&group->next, &domain->group_list); + +vfio_test_domain_fgsp (domain); + +vfio_update_pgsize_bitmap (iommu); + +/* Delete the old one and insert new iova list */ +vfio_iommu_iova_insert_copy (iommu, &iova_copy); + + group->attach_cnt =1 ; +mutex_unlock (&iommu->lock); +vfio_iommu_resv_free (&group_resv_regions); + +return 0 ; + +out_free: +vfio_iommu_iova_free (&iova_copy); +vfio_iommu_resv_free (&group_resv_regions); +mutex_unlock (&iommu->lock); +return ret; +} +EXPORT_SYMBOL_GPL (vfio_iommu_add_group); +static int vfio_iommu_type1_attach_group (void *iommu_data,struct iommu_group *iommu_group) {
@@ -2557,6 +2687,59 @@ static int vfio_iommu_resv_refresh(struct vfio_iommu *iommu,return ret; } +/* HACK: called by /dev/iommu core to remove group to vfio_iommu_type1 */ +void vfio_iommu_remove_group (struct vfio_iommu *iommu, +struct iommu_group *iommu_group) +{ +struct vfio_iommu_group *group; +struct vfio_domain *domain =NULL ; +LIST_HEAD (iova_copy); + +mutex_lock (&iommu->lock); + domain =list_first_entry (&iommu->domain_list, + struct vfio_domain, next); + group =find_iommu_group (domain, iommu_group); +if (!group) { +mutex_unlock (&iommu->lock); +return ; + } + +if (!--group->attach_cnt) { +mutex_unlock (&iommu->lock); +return ; + } + +/* +* Get a copy of iova list. This will be used to update +* and to replace the current one later. Please note that +* we will leave the original list as it is if update fails. +*/ +vfio_iommu_iova_get_copy (iommu, &iova_copy); + +list_del (&group->next); +kfree (group); +/* +* Group ownership provides privilege, if the device list is +* empty, the domain goes away. +*/ +if (list_empty (&domain->group_list)) { +WARN_ON (iommu->notifier.head); +vfio_iommu_unmap_unpin_all (iommu); +list_del (&domain->next); +kfree (domain); +vfio_iommu_aper_expand (iommu, &iova_copy); +vfio_update_pgsize_bitmap (iommu); + } + +if (!vfio_iommu_resv_refresh (iommu, &iova_copy)) +vfio_iommu_iova_insert_copy (iommu, &iova_copy); +else +vfio_iommu_iova_free (&iova_copy); + +mutex_unlock (&iommu->lock); +} +EXPORT_SYMBOL_GPL (vfio_iommu_remove_group); +static void vfio_iommu_type1_detach_group (void *iommu_data,struct iommu_group *iommu_group) {
@@ -2647,7 +2830,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,mutex_unlock (&iommu->lock); } -static void *vfio_iommu_type1_open (unsigned long arg) +void *vfio_iommu_type1_open (unsigned long arg) {struct vfio_iommu *iommu;
@@ -2680,6 +2863,7 @@ static void *vfio_iommu_type1_open(unsigned long arg)return iommu; } +EXPORT_SYMBOL_GPL (vfio_iommu_type1_open);static void vfio_release_domain (struct vfio_domain *domain,bool external) {
@@ -2697,7 +2881,7 @@ static void vfio_release_domain(struct vfio_domain *domain, bool external)iommu_domain_free (domain->domain); } -static void vfio_iommu_type1_release (void *iommu_data) +void vfio_iommu_type1_release (void *iommu_data) {struct vfio_iommu *iommu = iommu_data;struct vfio_domain *domain, *domain_tmp;
@@ -2720,6 +2904,7 @@ static void vfio_iommu_type1_release(void *iommu_data)kfree (iommu); } +EXPORT_SYMBOL_GPL (vfio_iommu_type1_release);static int vfio_domains_have_iommu_cache (struct vfio_iommu *iommu) {
@@ -2913,8 +3098,8 @@ static int vfio_iommu_type1_get_info(struct vfio_iommu *iommu, -EFAULT : 0; } -static int vfio_iommu_type1_map_dma (struct vfio_iommu *iommu, -unsigned long arg) +int vfio_iommu_type1_map_dma (struct vfio_iommu *iommu, +unsigned long arg) {struct vfio_iommu_type1_dma_map map;unsigned long minsz;
@@ -2931,9 +3116,10 @@ static int vfio_iommu_type1_map_dma(struct vfio_iommu *iommu,return vfio_dma_do_map (iommu, &map); } +EXPORT_SYMBOL_GPL (vfio_iommu_type1_map_dma); -static int vfio_iommu_type1_unmap_dma (struct vfio_iommu *iommu, -unsigned long arg) +int vfio_iommu_type1_unmap_dma (struct vfio_iommu *iommu, +unsigned long arg) {struct vfio_iommu_type1_dma_unmap unmap;struct vfio_bitmap bitmap = {0 };
@@ -2984,6 +3170,7 @@ static int vfio_iommu_type1_unmap_dma(struct vfio_iommu *iommu,return copy_to_user ((void __user *)arg, &unmap, minsz) ? -EFAULT :0 ; } +EXPORT_SYMBOL_GPL (vfio_iommu_type1_unmap_dma);static int vfio_iommu_type1_dirty_pages (struct vfio_iommu *iommu,unsigned long arg)
include/linux/vfio.h +13 -0
@@ -158,6 +158,19 @@ extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,extern struct iommu_domain *vfio_group_iommu_domain (struct vfio_group *group); +struct vfio_iommu ; +extern void *vfio_iommu_type1_open (unsigned long arg); +extern void vfio_iommu_type1_release (void *iommu_data); +extern int vfio_iommu_add_group (struct vfio_iommu *iommu, +struct iommu_group *iommu_group, +struct iommu_domain *iommu_domain); +extern void vfio_iommu_remove_group (struct vfio_iommu *iommu, +struct iommu_group *iommu_group); +extern int vfio_iommu_type1_unmap_dma (struct vfio_iommu *iommu, +unsigned long arg); +extern int vfio_iommu_type1_map_dma (struct vfio_iommu *iommu, +unsigned long arg); +/* each type has independent events */ enum vfio_notify_type {VFIO_IOMMU_NOTIFY =0 ,
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 16/20] vfio/type1: Export symbols for dma [un]map code sharing
Quoted 8 lines · Liu Yi L
[HACK. will fix in v2]
There are two options to impelement vfio type1v2 mapping semantics in
/dev/iommu.One is to duplicate the related code from vfio as the starting point, and then merge with vfio type1 at a later time. However
vfio_iommu_type1.chas over 3000LOC with ~80% related to dma management logic, including:
I can't really see a way forward like this. I think some scheme to move the vfio datastructure is going to be necessary.
Quoted 5 lines
- the dma map/unmap metadata management
- page pinning, and related accounting
- iova range reporting
- dirty bitmap retrieving
- dynamic vaddr update, etc.
All of this needs to be part of the iommufd anyhow..
Quoted 2 lines
The alternative is to consolidate type1v2 logic in
/dev/iommuimmediately, which requires convertingvfio_iommu_type1to be a shim driver.
Another choice is the the datastructure coulde move and the two drivers could share its code and continue to exist more independently
Jason
Tian, Kevin
maintainerRE: [RFC 16/20] vfio/type1: Export symbols for dma [un]map code sharing
Quoted 17 lines
From: Jason Gunthorpe <[email protected]>
Sent: Wednesday, September 22, 2021 2:15 AMQuoted 7 lines · Liu Yi L
[HACK. will fix in v2]
There are two options to impelement vfio type1v2 mapping semantics in
/dev/iommu.One is to duplicate the related code from vfio as the starting point, and then merge with vfio type1 at a later time. However
vfio_iommu_type1.cQuoted 1 line
has over 3000LOC with ~80% related to dma management logic, including:
I can't really see a way forward like this. I think some scheme to move the vfio datastructure is going to be necessary.
Quoted 7 lines
Quoted 5 lines
- the dma map/unmap metadata management
- page pinning, and related accounting
- iova range reporting
- dirty bitmap retrieving
- dynamic vaddr update, etc.All of this needs to be part of the iommufd anyhow..
yes
Quoted 7 lines
Quoted 3 lines
The alternative is to consolidate type1v2 logic in
/dev/iommuimmediately, which requires convertingvfio_iommu_type1to be a shim driver.Another choice is the the datastructure coulde move and the two drivers could share its code and continue to exist more independently
where to put the shared code?
btw this is one major open that I plan to discuss in LPC. 😊
RFC 17/20 iommu/iommufd: Report iova range to userspace
In reply to Liu Yi L[HACK. will fix in v2]
IOVA range is critical info for userspace to manage DMA for an I/O address space. This patch reports the valid iova range info of a given device.
Due to aforementioned hack, this info comes from the hacked vfio type1 driver. To follow the same format in vfio, we also introduce a cap chain format in IOMMU_DEVICE_GET_INFO to carry the iova range info.
--- drivers/iommu/iommu.c | 2 ++ drivers/iommu/iommufd/iommufd.c | 41 +++++++++++++++++++++++++++- drivers/vfio/vfio_iommu_type1.c | 47 ++++++++++++++++++++++++++++++--- include/linux/vfio.h | 2 ++ include/uapi/linux/iommu.h | 3 +++ 5 files changed, 90 insertions(+), 5 deletions(-)
drivers/iommu/iommu.c +2 -0
@@ -2755,6 +2755,7 @@ void iommu_get_resv_regions(struct device *dev, struct list_head *list)if (ops && ops->get_resv_regions) ops->get_resv_regions (dev, list); } +EXPORT_SYMBOL_GPL (iommu_get_resv_regions);void iommu_put_resv_regions (struct device *dev,struct list_head *list) {
@@ -2763,6 +2764,7 @@ void iommu_put_resv_regions(struct device *dev, struct list_head *list)if (ops && ops->put_resv_regions) ops->put_resv_regions (dev, list); } +EXPORT_SYMBOL_GPL (iommu_put_resv_regions);/** * generic_iommu_put_resv_regions - Reserved region driver helper
drivers/iommu/iommufd/iommufd.c +40 -1
66 lines not on this page · raw message
drivers/vfio/vfio_iommu_type1.c +43 -4
71 lines not on this page · raw message
include/linux/vfio.h +2 -0
@@ -212,6 +212,8 @@ extern int vfio_info_add_capability(struct vfio_info_cap *caps,extern int vfio_set_irqs_validate_and_prepare (struct vfio_irq_set *hdr,int num_irqs,int max_irq_type,size_t *data_size); +extern int vfio_device_add_iova_cap (struct device *dev, +struct vfio_info_cap *caps);struct pci_dev ;#if IS_ENABLED (CONFIG_VFIO_SPAPR_EEH )
include/uapi/linux/iommu.h +3 -0
@@ -68,6 +68,7 @@* +---------------+------------+ * ... * @addr_width: the address width of supported I/O address spaces. +* @cap_offset: Offset within info struct of first cap * * Availability: after device is bound to iommufd */
@@ -77,9 +78,11 @@ struct iommu_device_info {#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP (1 << 0)/* IOMMU enforced snoop */ #define IOMMU_DEVICE_INFO_PGSIZES (1 << 1)/* supported page sizes */ #define IOMMU_DEVICE_INFO_ADDR_WIDTH (1 << 2)/* addr_wdith field valid */ +#define IOMMU_DEVICE_INFO_CAPS (1 << 3)/* info supports cap chain */ __u64 dev_cookie;__u64 pgsize_bitmap;__u32 addr_width; +__u32 cap_offset; };#define IOMMU_DEVICE_GET_INFO _IO(IOMMU_TYPE, IOMMU_BASE + 1)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
kernel test robot
build botRe: [RFC 17/20] iommu/iommufd: Report iova range to userspace
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Liu,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on hch-configfs/for-next]
[cannot apply to joro-iommu/next awilliam-vfio/next linus/master v5.15-rc1 next-20210917] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 base: git://git.infradead.org/users/hch/configfs.git for-next config: ia64-buildonly-randconfig-r001-20210919 (attached as .config) compiler: ia64-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/89ec01e705473b8e3b52112ef19dc2e5f4b1c3cc
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 git checkout 89ec01e705473b8e3b52112ef19dc2e5f4b1c3cc
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=ia64 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
ia64-linux-ld: drivers/iommu/iommufd/iommufd.o: in function `iommu_device_add_cap_chain.constprop.0':
>> iommufd.c:(.text+0x1d2): undefined reference to `vfio_device_add_iova_cap'
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for IOMMUFD Depends on IOMMU_SUPPORT
Selected by
- VFIO_PCI && VFIO && PCI && EVENTFD && MMU
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
kernel test robot
build botRe: [RFC 17/20] iommu/iommufd: Report iova range to userspace
In reply to Liu Yi L[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Liu,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on hch-configfs/for-next]
[cannot apply to joro-iommu/next awilliam-vfio/next linus/master v5.15-rc1 next-20210917] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 base: git://git.infradead.org/users/hch/configfs.git for-next config: mips-allmodconfig (attached as .config) compiler: mips-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/89ec01e705473b8e3b52112ef19dc2e5f4b1c3cc
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 git checkout 89ec01e705473b8e3b52112ef19dc2e5f4b1c3cc
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross ARCH=mips
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>, old ones prefixed by <<):
>> ERROR: modpost: "vfio_device_add_iova_cap" [drivers/iommu/iommufd/iommufd.ko] undefined!
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [RFC 17/20] iommu/iommufd: Report iova range to userspace
In reply to Liu Yi LQuoted 8 lines · Liu Yi L
[HACK. will fix in v2]
IOVA range is critical info for userspace to manage DMA for an I/O address space. This patch reports the valid iova range info of a given device.
Due to aforementioned hack, this info comes from the hacked vfio type1 driver. To follow the same format in vfio, we also introduce a cap chain format in
IOMMU_DEVICE_GET_INFOto carry the iova range info.
[...]
Quoted 13 lines
include/uapi/linux/iommu.h +1
-0@@ -68,6 +68,7 @@* +---------------+------------+ * ... * @addr_width: the address width of supported I/O address spaces. +* @cap_offset: Offset within info struct of first cap * * Availability: after device is bound to iommufd */
include/uapi/linux/iommu.h struct iommu_device_info { · line 85
Quoted 8 lines
include/uapi/linux/iommu.h +2
-0@@ -77,9 +78,11 @@ struct iommu_device_info {#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP (1 << 0)/* IOMMU enforced snoop */ #define IOMMU_DEVICE_INFO_PGSIZES (1 << 1)/* supported page sizes */ #define IOMMU_DEVICE_INFO_ADDR_WIDTH (1 << 2)/* addr_wdith field valid */ +#define IOMMU_DEVICE_INFO_CAPS (1 << 3)/* info supports cap chain */ __u64 dev_cookie;__u64 pgsize_bitmap;__u32 addr_width; +__u32 cap_offset;
We can also add vendor-specific page table and PASID table properties as capabilities, otherwise we'll need giant unions in the iommu_device_info struct. That made me wonder whether pgsize and addr_width should also be separate capabilities for consistency, but this way might be good enough. There won't be many more generic capabilities. I have "output address width" and "PASID width", the rest is specific to Arm and SMMU table formats.
Thanks,
Jean
Quoted 6 lines
include/uapi/linux/iommu.h +0
-0};#define IOMMU_DEVICE_GET_INFO _IO(IOMMU_TYPE, IOMMU_BASE + 1)Signature · 1 line
2.25.1
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
RE: [RFC 17/20] iommu/iommufd: Report iova range to userspace
Quoted 34 lines
From: Jean-Philippe Brucker <[email protected]>
Sent: Wednesday, September 22, 2021 10:49 PMQuoted 8 lines · Liu Yi L
[HACK. will fix in v2]
IOVA range is critical info for userspace to manage DMA for an I/O address space. This patch reports the valid iova range info of a given device.
Due to aforementioned hack, this info comes from the hacked vfio type1 driver. To follow the same format in vfio, we also introduce a cap chain format in
IOMMU_DEVICE_GET_INFOto carry the iova range info.[...]
Quoted 13 lines
include/uapi/linux/iommu.h +1
-0@@ -68,6 +68,7 @@* +---------------+------------+ * ... * @addr_width: the address width of supported I/O address spaces. +* @cap_offset: Offset within info struct of first cap * * Availability: after device is bound to iommufd */ include/uapi/linux/iommu.h struct iommu_device_info { · line 78
Quoted 1 line
include/uapi/linux/iommu.h +0
-0@@ -77,9 +78,11 @@ struct iommu_device_info {#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP (1 << 0)/* IOMMU enforced snoop */
include/uapi/linux/iommu.h struct iommu_device_info { · line 79
Quoted 1 line
include/uapi/linux/iommu.h +0
-0#define IOMMU_DEVICE_INFO_PGSIZES (1 << 1)/* supported page sizes */
include/uapi/linux/iommu.h struct iommu_device_info { · line 80
Quoted 1 line
include/uapi/linux/iommu.h +0
-0#define IOMMU_DEVICE_INFO_ADDR_WIDTH (1 << 2)/*
addr_wdithfield valid */include/uapi/linux/iommu.h struct iommu_device_info { · line 81
Quoted 1 line
include/uapi/linux/iommu.h +1
-0+#define IOMMU_DEVICE_INFO_CAPS (1 << 3)/* info supports cap chain */
Quoted 12 lines
include/uapi/linux/iommu.h struct iommu_device_info { · line 85
Quoted 4 lines
include/uapi/linux/iommu.h +1
-0__u64 dev_cookie;__u64 pgsize_bitmap;__u32 addr_width; +__u32 cap_offset;We can also add vendor-specific page table and PASID table properties as capabilities, otherwise we'll need giant unions in the
iommu_device_infostruct. That made me wonder whether pgsize andaddr_widthshould also be
separate capabilities for consistency, but this way might be good enough. There won't be many more generic capabilities. I have "output address width"
what do you mean by "output address width"? Is it the output address of stage-1 translation?
Quoted 1 line
and "PASID width", the rest is specific to Arm and SMMU table
Quoted 1 line
formats.
When coming to nested translation support, the stage-1 related info are likely to be vendor-specific, and will be reported in cap chain.
Regards,
Yi Liu
Quoted 10 lines
Thanks,
Jeaninclude/uapi/linux/iommu.h struct iommu_device_info { · line 88
Quoted 3 lines
include/uapi/linux/iommu.h +0
-0};#define IOMMU_DEVICE_GET_INFO _IO(IOMMU_TYPE, IOMMU_BASE +1)
Quoted 3 lines
--
2.25.1
Re: [RFC 17/20] iommu/iommufd: Report iova range to userspace
Quoted 47 lines · Yi L
Quoted 34 lines
From: Jean-Philippe Brucker <[email protected]>
Sent: Wednesday, September 22, 2021 10:49 PMQuoted 8 lines · Liu Yi L
[HACK. will fix in v2]
IOVA range is critical info for userspace to manage DMA for an I/O address space. This patch reports the valid iova range info of a given device.
Due to aforementioned hack, this info comes from the hacked vfio type1 driver. To follow the same format in vfio, we also introduce a cap chain format in
IOMMU_DEVICE_GET_INFOto carry the iova range info.[...]
Quoted 13 lines
include/uapi/linux/iommu.h +1
-0@@ -68,6 +68,7 @@* +---------------+------------+ * ... * @addr_width: the address width of supported I/O address spaces. +* @cap_offset: Offset within info struct of first cap * * Availability: after device is bound to iommufd */ include/uapi/linux/iommu.h struct iommu_device_info { · line 78
Quoted 1 line
include/uapi/linux/iommu.h +0
-0@@ -77,9 +78,11 @@ struct iommu_device_info {#define IOMMU_DEVICE_INFO_ENFORCE_SNOOP (1 << 0)/* IOMMU enforced snoop */
include/uapi/linux/iommu.h struct iommu_device_info { · line 79
Quoted 1 line
include/uapi/linux/iommu.h +0
-0#define IOMMU_DEVICE_INFO_PGSIZES (1 << 1)/* supported page sizes */
include/uapi/linux/iommu.h struct iommu_device_info { · line 80
Quoted 1 line
include/uapi/linux/iommu.h +0
-0#define IOMMU_DEVICE_INFO_ADDR_WIDTH (1 << 2)/*
addr_wdithfield valid */include/uapi/linux/iommu.h struct iommu_device_info { · line 81
Quoted 1 line
include/uapi/linux/iommu.h +1
-0+#define IOMMU_DEVICE_INFO_CAPS (1 << 3)/* info supports cap chain */
Quoted 12 lines
include/uapi/linux/iommu.h struct iommu_device_info { · line 85
Quoted 4 lines
include/uapi/linux/iommu.h +1
-0__u64 dev_cookie;__u64 pgsize_bitmap;__u32 addr_width; +__u32 cap_offset;We can also add vendor-specific page table and PASID table properties as capabilities, otherwise we'll need giant unions in the
iommu_device_infostruct. That made me wonder whether pgsize andaddr_widthshould also be
separate capabilities for consistency, but this way might be good enough. There won't be many more generic capabilities. I have "output address width"
Quoted 2 lines
what do you mean by "output address width"? Is it the output address of stage-1 translation?
Yes, so the guest knows the size of GPA it can write into the page table. For Arm SMMU the GPA size is determined by both the SMMU implementation and the host kernel configuration. But maybe that could also be vendor-specific, if other architectures don't need to communicate it.
Quoted 6 lines
Quoted 1 line
and "PASID width", the rest is specific to Arm and SMMU table
Quoted 1 line
formats.
When coming to nested translation support, the stage-1 related info are likely to be vendor-specific, and will be reported in cap chain.
Agreed
Thanks,
Jean
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Jason Gunthorpe
maintainerRe: [RFC 17/20] iommu/iommufd: Report iova range to userspace
Quoted 4 lines · Jean-Philippe Brucker
Yes, so the guest knows the size of GPA it can write into the page table. For Arm SMMU the GPA size is determined by both the SMMU implementation and the host kernel configuration. But maybe that could also be vendor-specific, if other architectures don't need to communicate it.
I think there should be a dedicated query to return HW specific parmaters for a user page table format. Somehow I think there will be a lot of these.
So 'user page table format arm smmu v1' can be queried to return its own unique struct that has everything needed to operate that format of page table.
Userspace already needs to know how to form that specific HW PTEs, so processing a HW specific query is not a problem.
Jason
RFC 18/20 iommu/iommufd: Add IOMMU_[UN]MAP_DMA on IOASID
In reply to Liu Yi L[HACK. will fix in v2]
This patch introduces vfio type1v2-equivalent interface to userspace. Due to aforementioned hack, iommufd currently calls exported vfio symbols to handle map/unmap requests from the user.
--- drivers/iommu/iommufd/iommufd.c | 104 ++++++++++++++++++++++++++++++++ include/uapi/linux/iommu.h | 29 +++++++++ 2 files changed, 133 insertions(+)
drivers/iommu/iommufd/iommufd.c +104 -0
@@ -55,6 +55,7 @@ struct iommufd_ioas {struct mutex lock;struct list_head device_list;struct iommu_domain *domain; +struct vfio_iommu *vfio_iommu;/* FIXME: added for reusing vfio_iommu_type1 code */ };/*
@@ -158,6 +159,7 @@ static void ioas_put_locked(struct iommufd_ioas *ioas)return ;WARN_ON (!list_empty (&ioas->device_list)); +vfio_iommu_type1_release (ioas->vfio_iommu);/* FIXME: reused vfio code */ xa_erase (&ictx->ioasid_xa, ioasid);iommufd_ctx_put (ictx);kfree (ioas);
@@ -185,6 +187,7 @@ static int iommufd_ioasid_alloc(struct iommufd_ctx *ictx, unsigned long arg)struct iommufd_ioas *ioas;unsigned long minsz;int ioasid, ret; +struct vfio_iommu *vfio_iommu; minsz = offsetofend(struct iommu_ioasid_alloc, addr_width);
@@ -211,6 +214,18 @@ static int iommufd_ioasid_alloc(struct iommufd_ctx *ictx, unsigned long arg)return ret; } +/* FIXME: get a vfio_iommu object for dma map/unmap management */ + vfio_iommu =vfio_iommu_type1_open (VFIO_TYPE1v2_IOMMU); +if (IS_ERR (vfio_iommu)) { +pr_err_ratelimited ("Failed to get vfio_iommu object\n" ); +mutex_lock (&ictx->lock); +xa_erase (&ictx->ioasid_xa, ioasid); +mutex_unlock (&ictx->lock); +kfree (ioas); +return PTR_ERR (vfio_iommu); + } + ioas->vfio_iommu = vfio_iommu; + ioas->ioasid = ioasid;/* only supports kernel managed I/O page table so far */
@@ -383,6 +398,49 @@ static int iommufd_get_device_info(struct iommufd_ctx *ictx,return copy_to_user ((void __user *)arg, &info, minsz) ? -EFAULT :0 ; } +static int iommufd_process_dma_op (struct iommufd_ctx *ictx, +unsigned long arg,bool map) +{ +struct iommu_ioasid_dma_op dma; +unsigned long minsz; +struct iommufd_ioas *ioas =NULL ; +int ret; + + minsz = offsetofend(struct iommu_ioasid_dma_op, padding); + +if (copy_from_user (&dma, (void __user *)arg, minsz)) +return -EFAULT ; + +if (dma.argsz < minsz || dma.flags || dma.ioasid <0 ) +return -EINVAL ; + + ioas =ioasid_get_ioas (ictx, dma.ioasid); +if (!ioas) { +pr_err_ratelimited ("unkonwn IOASID %u\n" , dma.ioasid); +return -EINVAL ; + } + +mutex_lock (&ioas->lock); + +/* +* Needs to block map/unmap request from userspace before IOASID +* is attached to any device. +*/ +if (list_empty (&ioas->device_list)) { + ret = -EINVAL ; + goto out; + } + +if (map) + ret =vfio_iommu_type1_map_dma (ioas->vfio_iommu, arg + minsz); +else + ret =vfio_iommu_type1_unmap_dma (ioas->vfio_iommu, arg + minsz); +out: +mutex_unlock (&ioas->lock); +ioas_put (ioas); +return ret; +}; +static long iommufd_fops_unl_ioctl (struct file *filep,unsigned int cmd,unsigned long arg) {
@@ -409,6 +467,12 @@ static long iommufd_fops_unl_ioctl(struct file *filep,case IOMMU_IOASID_FREE : ret =iommufd_ioasid_free (ictx, arg);break ; +case IOMMU_MAP_DMA : + ret =iommufd_process_dma_op (ictx, arg, true); +break ; +case IOMMU_UNMAP_DMA : + ret =iommufd_process_dma_op (ictx, arg, false); +break ;default :pr_err_ratelimited ("unsupported cmd %u\n" , cmd);break ;
@@ -478,6 +542,39 @@ static int ioas_check_device_compatibility(struct iommufd_ioas *ioas,return 0 ; } +/* HACK: +* vfio_iommu_add/remove_device() is hacky implementation for +* this version to add the device/group to vfio iommu type1. +*/ +static int vfio_iommu_add_device (struct vfio_iommu *vfio_iommu, +struct device *dev, +struct iommu_domain *domain) +{ +struct iommu_group *group; +int ret; + + group =iommu_group_get (dev); +if (!group) +return -EINVAL ; + + ret =vfio_iommu_add_group (vfio_iommu, group, domain); +iommu_group_put (group); +return ret; +} + +static void vfio_iommu_remove_device (struct vfio_iommu *vfio_iommu, +struct device *dev) +{ +struct iommu_group *group; + + group =iommu_group_get (dev); +if (!group) +return ; + +vfio_iommu_remove_group (vfio_iommu, group); +iommu_group_put (group); +} +/** * iommufd_device_attach_ioasid - attach device to an ioasid * @idev: [in] Pointer to struct iommufd_device.
@@ -539,11 +636,17 @@ int iommufd_device_attach_ioasid(struct iommufd_device *idev, int ioasid)if (ret) goto out_domain; + ret =vfio_iommu_add_device (ioas->vfio_iommu, idev->dev, domain); +if (ret) + goto out_detach; + ioas_dev->idev = idev;list_add (&ioas_dev->next, &ioas->device_list);mutex_unlock (&ioas->lock);return 0 ; +out_detach: +iommu_detach_device (domain, idev->dev); out_domain:ioas_free_domain_if_empty (ioas); out_free:
@@ -579,6 +682,7 @@ void iommufd_device_detach_ioasid(struct iommufd_device *idev, int ioasid) }list_del (&ioas_dev->next); +vfio_iommu_remove_device (ioas->vfio_iommu, idev->dev);iommu_detach_device (ioas->domain, idev->dev);ioas_free_domain_if_empty (ioas);kfree (ioas_dev);
include/uapi/linux/iommu.h +29 -0
@@ -141,6 +141,35 @@ struct iommu_ioasid_alloc {#define IOMMU_IOASID_FREE _IO(IOMMU_TYPE, IOMMU_BASE + 3) +/* +* Map/unmap process virtual addresses to I/O virtual addresses. +* +* Provide VFIO type1 equivalent semantics. Start with the same +* restriction e.g. the unmap size should match those used in the +* original mapping call. +* +* @argsz: user filled size of this data. +* @flags: reserved for future extension. +* @ioasid: the handle of target I/O address space. +* @data: the operation payload, refer to vfio_iommu_type1_dma_{un}map. +* +* FIXME: +* userspace needs to include uapi/vfio.h as well as interface reuses +* the map/unmap logic from vfio iommu type1. +* +* Return: 0 on success, -errno on failure. +*/ +struct iommu_ioasid_dma_op { +__u32 argsz; +__u32 flags; +__s32 ioasid; +__u32 padding; +__u8 data[]; +}; + +#define IOMMU_MAP_DMA _IO(IOMMU_TYPE, IOMMU_BASE + 4) +#define IOMMU_UNMAP_DMA _IO(IOMMU_TYPE, IOMMU_BASE + 5) +#define IOMMU_FAULT_PERM_READ (1 << 0)/* read */ #define IOMMU_FAULT_PERM_WRITE (1 << 1)/* write */ #define IOMMU_FAULT_PERM_EXEC (1 << 2)/* exec */
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
kernel test robot
build botRe: [RFC 18/20] iommu/iommufd: Add IOMMU_[UN]MAP_DMA on IOASID
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Liu,
[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on hch-configfs/for-next]
[cannot apply to joro-iommu/next awilliam-vfio/next linus/master v5.15-rc1 next-20210917] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 base: git://git.infradead.org/users/hch/configfs.git for-next config: ia64-buildonly-randconfig-r001-20210919 (attached as .config) compiler: ia64-linux-gcc (GCC) 11.2.0
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/611f151814f65b3087eeeb3f9115f42c647746c2
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Liu-Yi-L/Introduce-dev-iommu-for-userspace-I-O-address-space-management/20210919-144631 git checkout 611f151814f65b3087eeeb3f9115f42c647746c2
# save the attached .config to linux build tree
mkdir build_dir
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-11.2.0 make.cross O=build_dir ARCH=ia64 SHELL=/bin/bash
If you fix the issue, kindly add following tag as appropriate
All errors (new ones prefixed by >>):
ia64-linux-ld: drivers/iommu/iommufd/iommufd.o: in function `iommu_device_add_cap_chain.constprop.0':
iommufd.c:(.text+0x1d2): undefined reference to `vfio_device_add_iova_cap'
ia64-linux-ld: drivers/iommu/iommufd/iommufd.o: in function `ioas_put_locked':
>> iommufd.c:(.text+0x792): undefined reference to `vfio_iommu_type1_release' >> ia64-linux-ld: iommufd.c:(.text+0x872): undefined reference to `vfio_iommu_type1_release' ia64-linux-ld: drivers/iommu/iommufd/iommufd.o: in function `iommufd_device_attach_ioasid': >> iommufd.c:(.text+0x1682): undefined reference to `vfio_iommu_add_group' ia64-linux-ld: drivers/iommu/iommufd/iommufd.o: in function `iommufd_device_detach_ioasid': >> iommufd.c:(.text+0x1bd2): undefined reference to `vfio_iommu_remove_group' ia64-linux-ld: drivers/iommu/iommufd/iommufd.o: in function `iommufd_process_dma_op': >> iommufd.c:(.text+0x1f12): undefined reference to `vfio_iommu_type1_map_dma' >> ia64-linux-ld: iommufd.c:(.text+0x1fb2): undefined reference to `vfio_iommu_type1_unmap_dma' ia64-linux-ld: drivers/iommu/iommufd/iommufd.o: in function `iommufd_ioasid_alloc': >> iommufd.c:(.text+0x2752): undefined reference to `vfio_iommu_type1_open'
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for IOMMUFD Depends on IOMMU_SUPPORT
Selected by
- VFIO_PCI && VFIO && PCI && EVENTFD && MMU
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
RFC 19/20 iommu/vt-d: Implement device_info iommu_ops callback
In reply to Liu Yi LFrom: Lu Baolu <[email protected]>
Expose per-device IOMMU attributes to the upper layers.
--- drivers/iommu/intel/iommu.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+)
drivers/iommu/intel/iommu.c +35 -0
@@ -5583,6 +5583,40 @@ static void intel_iommu_iotlb_sync_map(struct iommu_domain *domain, } } +static int +intel_iommu_device_info (struct device *dev,enum iommu_devattr type,void *data) +{ +struct intel_iommu *iommu =device_to_iommu (dev,NULL ,NULL ); +int ret =0 ; + +if (!iommu) +return -ENODEV ; + +switch (type) { +case IOMMU_DEV_INFO_PAGE_SIZE : + *(u64 *)data =SZ_4K | + (cap_super_page_val (iommu->cap) &BIT (0 ) ?SZ_2M :0 ) | + (cap_super_page_val (iommu->cap) &BIT (1 ) ?SZ_1G :0 ); +break ; +case IOMMU_DEV_INFO_FORCE_SNOOP : +/* +* Force snoop is always supported in the scalable mode. For the legacy +* mode, check the capability register. +*/ + *(bool *)data =sm_supported (iommu) ||ecap_sc_support (iommu->ecap); +break ; +case IOMMU_DEV_INFO_ADDR_WIDTH : + *(u32 *)data =min_t (u32,agaw_to_width (iommu->agaw), +cap_mgaw (iommu->cap)); +break ; +default : + ret = -EINVAL ; +break ; + } + +return ret; +} +const struct iommu_ops intel_iommu_ops = { .capable = intel_iommu_capable, .domain_alloc = intel_iommu_domain_alloc,
@@ -5621,6 +5655,7 @@ const struct iommu_ops intel_iommu_ops = { .sva_get_pasid = intel_svm_get_pasid, .page_response = intel_svm_page_response, #endif + .device_info = intel_iommu_device_info, };static void quirk_iommu_igfx (struct pci_dev *dev)
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
RFC 20/20 Doc: Add documentation for /dev/iommu
In reply to Liu Yi LDocument the /dev/iommu framework for user.
Open:
Do we want to document /dev/iommu in Documentation/userspace-api/iommu.rst? Existing iommu.rst is for the vSVA interfaces, honestly, may need to rewrite this doc entirely.
--- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/iommufd.rst | 183 ++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 Documentation/userspace-api/iommufd.rst
Documentation/userspace-api/index.rst +1 -0
@@ -25,6 +25,7 @@ place where this information is gathered. ebpf/index ioctl/index iommu + iommufd media/index sysfs-platform_profile
Documentation/userspace-api/iommufd.rst new file +183 -0
183 lines not on this page · raw message
Signature · 6 lines
2.25.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [RFC 20/20] Doc: Add documentation for /dev/iommu
Quoted 77 lines · Liu Yi L
Document the
/dev/iommuframework for user.Open:
Do we want to document/dev/iommuinDocumentation/userspace-api/iommu.rst? Existingiommu.rstis for the vSVA interfaces, honestly, may need to rewrite this doc entirely.Signed-off-by Kevin Tian <[email protected]>Signed-off-by Liu Yi L <[email protected]>--- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/iommufd.rst | 183 ++++++++++++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 Documentation/userspace-api/iommufd.rstDocumentation/userspace-api/index.rst +1
-0@@ -25,6 +25,7 @@ place where this information is gathered. ebpf/index ioctl/index iommu + iommufd media/index sysfs-platform_profileDocumentation/userspace-api/iommufd.rst new file +44
-0@@ -0,0 +1,183 @@ +.. SPDX-License-Identifier: GPL-2.0 +.. iommu: + +=================== +IOMMU Userspace API +=================== + +Direct device access from userspace has been a crtical feature in +high performance computing and virtualization usages. Linux now +includes multiple device-passthrough frameworks (e.g. VFIO and vDPA) +to manage secure device access from the userspace. One critical +task of those frameworks is to put the assigned device in a secure, +IOMMU-protected context so the device is prevented from doing harm +to the rest of the system. + +Currently those frameworks implement their own logic for managing +I/O page tables to isolate user-initiated DMAs. This doesn't scale +to support many new IOMMU features, such as PASID-granular DMA +remapping, nested translation, I/O page fault, IOMMU dirty bit, etc. + +The /dev/iommu framework provides an unified interface for managing +I/O page tables for passthrough devices. Existing passthrough +frameworks are expected to use this interface instead of continuing +their ad-hoc implementations. + +IOMMUFDs, IOASIDs, Devices and Groups +------------------------------------- + +The core concepts in /dev/iommu are IOMMUFDs and IOASIDs. IOMMUFD (by +opening /dev/iommu) is the container holding multiple I/O address +spaces for a user, while IOASID is the fd-local software handle +representing an I/O address space and associated with a single I/O +page table. User manages those address spaces through fd operations, +e.g. by using vfio type1v2 mapping semantics to manage respective +I/O page tables. + +IOASID is comparable to the conatiner concept in VFIO. The latter +is also associated to a single I/O address space. A main difference +between them is that multiple IOASIDs in the same IOMMUFD can be +nested together (not supported yet) to allow centralized accounting +of locked pages, while multiple containers are disconnected thus +duplicated accounting is incurred. Typically one IOMMUFD is +sufficient for all intended IOMMU usages for a user. +
Documentation/userspace-api/iommufd.rst line 54
Quoted 10 lines
Documentation/userspace-api/iommufd.rst new file +10
-0+An I/O address space takes effect in the IOMMU only after it is +attached by a device. One I/O address space can be attached by +multiple devices. One device can be only attached to a single I/O +address space at this point (on par with current vfio behavior). + +Device must be bound to an iommufd before the attach operation can +be conducted. The binding operation builds the connection between +the devicefd (opened via device-passthrough framework) and IOMMUFD. +IOMMU-protected security context is esbliashed when the binding +operation is completed.
This can't be quite right. You can't establish a safe security context until all devices in the groun are bound, but you can only bind them one at a time.
Quoted 13 lines
Documentation/userspace-api/iommufd.rst new file +12
-0The passthrough framework must block user +access to the assigned device until bind() returns success. + +The entire /dev/iommu framework adopts a device-centric model w/o +carrying any container/group legacy as current vfio does. However +the group is the minimum granularity that must be used to ensure +secure user access (refer to vfio.rst). This framework relies on +the IOMMU core layer to map device-centric model into group-granular +isolation. + +Managing I/O Address Spaces +--------------------------- +
Documentation/userspace-api/iommufd.rst
Quoted 10 lines
Documentation/userspace-api/iommufd.rst new file +10
-0+When creating an I/O address space (by allocating IOASID), the user +must specify the type of underlying I/O page table. Currently only +one type (kernel-managed) is supported. In the future other types +will be introduced, e.g. to support user-managed I/O page table or +a shared I/O page table which is managed by another kernel sub- +system (mm, ept, etc.). Kernel-managed I/O page table is currently +managed via vfio type1v2 equivalent mapping semantics. + +The user also needs to specify the format of the I/O page table +when allocating an IOASID.
This almost seems redundant with the previous paragraph. I think maybe it's making a distinction between "type" and "format", but I don't think it's very clear what the distinction is.
Quoted 108 lines
The format must be compatible to the
Documentation/userspace-api/iommufd.rst new file +107
-0+attached devices (or more specifically to the IOMMU which serves +the DMA from the attached devices). User can query the device IOMMU +format via IOMMUFD once a device is successfully bound. Attaching a +device to an IOASID with incompatible format is simply rejected. + +Currently no-snoop DMA is not supported yet. This implies that +IOASID must be created in an enforce-snoop format and only devices +which can be forced to snoop cache by IOMMU are allowed to be +attached to IOASID. The user should check uAPI extension and get +device info via IOMMUFD to handle such restriction. + +Usage Example +------------- + +Assume user wants to access PCI device 0000:06:0d.0, which is +exposed under the new /dev/vfio/devices directory by VFIO: + + /* Open device-centric interface and /dev/iommu interface */ + device_fd = open("/dev/vfio/devices/0000:06:0d.0", O_RDWR); + iommu_fd = open("/dev/iommu", O_RDWR); + + /* Bind device to IOMMUFD */ + bind_data = { .iommu_fd = iommu_fd, .dev_cookie = cookie }; + ioctl(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind_data); + + /* Query per-device IOMMU capability/format */ + info = { .dev_cookie = cookie, }; + ioctl(iommu_fd, IOMMU_DEVICE_GET_INFO, &info); + + if (!(info.flags & IOMMU_DEVICE_INFO_ENFORCE_SNOOP)) { + if (!ioctl(iommu_fd, IOMMU_CHECK_EXTENSION, + EXT_DMA_NO_SNOOP)) + /* No support of no-snoop DMA */ + } + + if (!ioctl(iommu_fd, IOMMU_CHECK_EXTENSION, EXT_MAP_TYPE1V2)) + /* No support of vfio type1v2 mapping semantics */ + + /* Decides IOASID alloc fields based on info */ + alloc_data = { .type = IOMMU_IOASID_TYPE_KERNEL, + .flags = IOMMU_IOASID_ENFORCE_SNOOP, + .addr_width = info.addr_width, }; + + /* Allocate IOASID */ + gpa_ioasid = ioctl(iommu_fd, IOMMU_IOASID_ALLOC, &alloc_data); + + /* Attach device to an IOASID */ + at_data = { .iommu_fd = iommu_fd; .ioasid = gpa_ioasid}; + ioctl(device_fd, VFIO_DEVICE_ATTACH_IOASID, &at_data); + + /* Setup GPA mapping [0 - 1GB] */ + dma_map = { + .ioasid = gpa_ioasid, + .data { + .flags = R/W /* permission */ + .iova = 0, /* GPA */ + .vaddr = 0x40000000, /* HVA */ + .size = 1GB, + }, + }; + ioctl(iommu_fd, IOMMU_MAP_DMA, &dma_map); + + /* DMA */ + + /* Unmap GPA mapping [0 - 1GB] */ + dma_unmap = { + .ioasid = gpa_ioasid, + .data { + .iova = 0, /* GPA */ + .size = 1GB, + }, + }; + ioctl(iommu_fd, IOMMU_UNMAP_DMA, &dma_unmap); + + /* Detach device from an IOASID */ + dt_data = { .iommu_fd = iommu_fd; .ioasid = gpa_ioasid}; + ioctl(device_fd, VFIO_DEVICE_DETACH_IOASID, &dt_data); + + /* Free IOASID */ + ioctl(iommu_fd, IOMMU_IOASID_FREE, gpa_ioasid); + + close(device_fd); + close(iommu_fd); + +API for device-passthrough frameworks +------------------------------------- + +iommufd binding and IOASID attach/detach are initiated via the device- +passthrough framework uAPI. + +When a binding operation is requested by the user, the passthrough +framework should call iommufd_bind_device(). When the device fd is +closed by the user, iommufd_unbind_device() should be called +automatically:: + + struct iommufd_device * + iommufd_bind_device(int fd, struct device *dev, + u64 dev_cookie); + void iommufd_unbind_device(struct iommufd_device *idev); + +IOASID attach/detach operations are per iommufd_device which is +returned by iommufd_bind_device(): + + int iommufd_device_attach_ioasid(struct iommufd_device *idev, + int ioasid); + void iommufd_device_detach_ioasid(struct iommufd_device *idev, + int ioasid);
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Jason Gunthorpe
maintainerRe: [RFC 20/20] Doc: Add documentation for /dev/iommu
Quoted 9 lines · David Gibson
Documentation/userspace-api/iommufd.rst
Quoted 5 lines
Documentation/userspace-api/iommufd.rst new file +5
-0+Device must be bound to an iommufd before the attach operation can +be conducted. The binding operation builds the connection between +the devicefd (opened via device-passthrough framework) and IOMMUFD. +IOMMU-protected security context is esbliashed when the binding +operation is completed.This can't be quite right. You can't establish a safe security context until all devices in the groun are bound, but you can only bind them one at a time.
When any device is bound the entire group is implicitly adopted to this iommufd and the whole group enters a safe-for-userspace state.
It is symmetrical with the kernel side which is also device focused, when any struct device is bound to a kernel driver the entire group is implicitly adopted to kernel mode.
Lu should send a patch series soon that harmonize how this works, it is a very nice cleanup.
Jason
Quoted 2 lines
From: Liu, Yi L <[email protected]>
Sent: Sunday, September 19, 2021 2:38 PM
[...]
Quoted 32 lines
[Series Overview]
* Basic skeleton:
0001-iommu-iommufd-Add-dev-iommu-core.patch* VFIO PCI creates device-centric interface:
0002-vfio-Add-device-class-for-dev-vfio-devices.patch 0003-vfio-Add-vfio_-un-register_device.patch 0004-iommu-Add-iommu_device_get_info-interface.patch 0005-vfio-pci-Register-device-to-dev-vfio-devices.patch* Bind device fd with iommufd:
0006-iommu-Add-iommu_device_init-exit-_user_dma-interface.patch 0007-iommu-iommufd-Add-iommufd_-un-bind_device.patch 0008-vfio-pci-Add-VFIO_DEVICE_BIND_IOMMUFD.patch* IOASID allocation:
0009-iommu-Add-page-size-and-address-width-attributes.patch 0010-iommu-iommufd-Add-IOMMU_DEVICE_GET_INFO.patch 0011-iommu-iommufd-Add-IOMMU_IOASID_ALLOC-FREE.patch 0012-iommu-iommufd-Add-IOMMU_CHECK_EXTENSION.patch* IOASID [de]attach:
0013-iommu-Extend-iommu_at-de-tach_device-for-multiple-de.patch 0014-iommu-iommufd-Add-iommufd_device_-de-attach_ioasid.patch 0015-vfio-pci-Add-VFIO_DEVICE_-DE-ATTACH_IOASID.patch* DMA (un)map:
0016-vfio-type1-Export-symbols-for-dma-un-map-code-sharin.patch 0017-iommu-iommufd-Report-iova-range-to-userspace.patch 0018-iommu-iommufd-Add-IOMMU_-UN-MAP_DMA-on-IOASID.patch
Quoted 5 lines
* Report the device info in vt-d driver to enable whole series:
0019-iommu-vt-d-Implement-device_info-iommu_ops-callback.patch* Add doc:
0020-Doc-Add-documentation-for-dev-iommu.patch
Please refer to the above patch overview. sorry for the duplicated contents.
thanks,
Yi Liu
Jason Gunthorpe
maintainerQuoted 5 lines · Liu Yi L
Linux now includes multiple device-passthrough frameworks (e.g. VFIO and vDPA) to manage secure device access from the userspace. One critical task of those frameworks is to put the assigned device in a secure, IOMMU- protected context so user-initiated DMAs are prevented from doing harm to the rest of the system.
Some bot will probably send this too, but it has compile warnings and needs to be rebased to 5.15-rc1
drivers/iommu/iommufd/iommufd.c:269:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
if (refcount_read(&ioas->refs) > 1) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:277:9: note: uninitialized use occurs here
return ret;
^~~
drivers/iommu/iommufd/iommufd.c:269:2: note: remove the 'if' if its condition is always true
if (refcount_read(&ioas->refs) > 1) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:253:17: note: initialize the variable 'ret' to silence this warning
int ioasid, ret;
^
= 0
drivers/iommu/iommufd/iommufd.c:727:7: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:767:17: note: uninitialized use occurs here
return ERR_PTR(ret);
^~~
drivers/iommu/iommufd/iommufd.c:727:3: note: remove the 'if' if its condition is always false
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:727:7: warning: variable 'ret' is used uninitialized whenever '||' condition is true [-Wsometimes-uninitialized]
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:767:17: note: uninitialized use occurs here
return ERR_PTR(ret);
^~~
drivers/iommu/iommufd/iommufd.c:727:7: note: remove the '||' if its condition is always false
if (idev->dev == dev || idev->dev_cookie == dev_cookie) {
^~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:717:9: note: initialize the variable 'ret' to silence this warning
int ret;
^
= 0Jason
Quoted 7 lines
From: Jason Gunthorpe <[email protected]>
Sent: Tuesday, September 21, 2021 9:45 PMQuoted 1 line · Liu Yi L
Linux now includes multiple device-passthrough frameworks (e.g. VFIO
and
Quoted 1 line
vDPA) to manage secure device access from the userspace. One critical
Quoted 8 lines
task
Quoted 2 lines
of those frameworks is to put the assigned device in a secure, IOMMU- protected context so user-initiated DMAs are prevented from doing harm
to
Quoted 1 line
the rest of the system.
Some bot will probably send this too, but it has compile warnings and needs to be rebased to 5.15-rc1
thanks Jason, will fix the warnings. yeah, I was using 5.14 in the test, will rebase to 5.15-rc# in next version.
Regards,
Yi Liu
Quoted 49 lines
drivers/iommu/iommufd/iommufd.c:269:6: warning: variable 'ret' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]if (refcount_read(&ioas->refs) > 1) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/iommu/iommufd/iommufd.c:277:9: note: uninitialized use occurs here return ret; ^~~ drivers/iommu/iommufd/iommufd.c:269:2: note: remove the 'if' if its condition is always true if (refcount_read(&ioas->refs) > 1) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/iommu/iommufd/iommufd.c:253:17: note: initialize the variable 'ret' to silence this warning int ioasid, ret; ^ = 0 drivers/iommu/iommufd/iommufd.c:727:7: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized] if (idev->dev == dev || idev->dev_cookie == dev_cookie) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/iommu/iommufd/iommufd.c:767:17: note: uninitialized use occurs here return ERR_PTR(ret); ^~~ drivers/iommu/iommufd/iommufd.c:727:3: note: remove the 'if' if its condition is always false if (idev->dev == dev || idev->dev_cookie == dev_cookie) {^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/iommu/iommufd/iommufd.c:727:7: warning: variable 'ret' is used uninitialized whenever '||' condition is true [-Wsometimes-uninitialized]if (idev->dev == dev || idev->dev_cookie == dev_cookie) { ^~~~~~~~~~~~~~~~ drivers/iommu/iommufd/iommufd.c:767:17: note: uninitialized use occurs here return ERR_PTR(ret); ^~~ drivers/iommu/iommufd/iommufd.c:727:7: note: remove the '||' if its condition is always false if (idev->dev == dev || idev->dev_cookie == dev_cookie) { ^~~~~~~~~~~~~~~~~~~ drivers/iommu/iommufd/iommufd.c:717:9: note: initialize the variable 'ret' to silence this warning int ret; ^ = 0Jason