On Mon, 12 Jul 2021 01:22:11 +0000 "Tian, Kevin" <kevin.tian@xxxxxxxxx> wrote: > > From: Alex Williamson <alex.williamson@xxxxxxxxxx> > > Sent: Saturday, July 10, 2021 5:51 AM > > On Fri, 9 Jul 2021 07:48:44 +0000 > > "Tian, Kevin" <kevin.tian@xxxxxxxxx> wrote: > > > For mdev the struct device should be the pointer to the parent device. > > > > I don't get how iommu_register_device() differentiates an mdev from a > > pdev in this case. > > via device cookie. Let me re-add this section for more context: > 3. Sample structures and helper functions > -------------------------------------------------------- > > Three helper functions are provided to support VFIO_BIND_IOMMU_FD: > > struct iommu_ctx *iommu_ctx_fdget(int fd); > struct iommu_dev *iommu_register_device(struct iommu_ctx *ctx, > struct device *device, u64 cookie); > int iommu_unregister_device(struct iommu_dev *dev); > > An iommu_ctx is created for each fd: > > struct iommu_ctx { > // a list of allocated IOASID data's > struct xarray ioasid_xa; > > // a list of registered devices > struct xarray dev_xa; > }; > > Later some group-tracking fields will be also introduced to support > multi-devices group. > > Each registered device is represented by iommu_dev: > > struct iommu_dev { > struct iommu_ctx *ctx; > // always be the physical device > struct device *device; > u64 cookie; > struct kref kref; > }; > > A successful binding establishes a security context for the bound > device and returns struct iommu_dev pointer to the caller. After this > point, the user is allowed to query device capabilities via IOMMU_ > DEVICE_GET_INFO. > > For mdev the struct device should be the pointer to the parent device. So we'll have a VFIO_DEVICE_BIND_IOMMU_FD ioctl where the user provides the iommu_fd and a cookie. vfio will use iommu_ctx_fdget() to get an iommu_ctx* for that iommu_fd, then we'll call iommu_register_device() using that iommu_ctx* we got from the iommu_fd, the cookie provided by the user, and for an mdev, the parent of the device the user owns (the device_fd on which this ioctl is called)... How does an arbitrary user provided cookie let you differentiate that the request is actually for an mdev versus the parent device itself? For instance, how can the IOMMU layer distinguish GVT-g (mdev) vs GVT-d (direct assignment) when both use the same struct device* and cookie is just a user provided value? Still confused. Thanks, Alex