Message ID | 20230309075358.571567-1-yi.l.liu@intel.com |
---|---|
Headers | show |
Series | iommufd: Add iommu hardware info reporting | expand |
> From: Jason Gunthorpe <jgg@nvidia.com> > Sent: Friday, March 10, 2023 1:21 AM > > On Thu, Mar 09, 2023 at 09:50:18PM +0800, Baolu Lu wrote: > > > > + if (cmd->flags || cmd->__reserved || !cmd->data_len) > > > + return -EOPNOTSUPP; > > > + > > > + idev = iommufd_get_device(ucmd, cmd->dev_id); > > > + if (IS_ERR(idev)) > > > + return PTR_ERR(idev); > > > + > > > + ops = dev_iommu_ops(idev->dev); > > > + if (!ops || !ops->hw_info) { > > > > dev_iommu_ops() will never return a NULL. > > > > Need below check > > > > dev->iommu && dev->iommu->iommu_dev > > > > before dev_iommu_ops(). Perhaps something like below? > > > > if (!dev->iommu || !dev->iommu->iommu_dev) > > return -EINVAL; > > At this point the device has become owned through the ownership API, > it absolutely has to have an iommu and an ops. No need to check > anything. ok. so just needs to check hw_info callback.
> From: Liu, Yi L <yi.l.liu@intel.com> > Sent: Thursday, March 9, 2023 3:54 PM > + > +int iommufd_device_get_hw_info(struct iommufd_ucmd *ucmd) > +{ > + struct iommu_hw_info *cmd = ucmd->cmd; > + struct iommufd_device *idev; > + const struct iommu_ops *ops; > + void *data; > + unsigned int length, data_len; > + int rc; > + > + if (cmd->flags || cmd->__reserved || !cmd->data_len) > + return -EOPNOTSUPP; > + > + idev = iommufd_get_device(ucmd, cmd->dev_id); > + if (IS_ERR(idev)) > + return PTR_ERR(idev); > + > + ops = dev_iommu_ops(idev->dev); > + if (!ops || !ops->hw_info) { > + rc = -EOPNOTSUPP; > + goto out_put; > + } > + > + /* driver has hw_info callback should have a unique driver_type */ > + if (WARN_ON(ops->driver_type == > IOMMU_HW_INFO_TYPE_DEFAULT)) { > + rc = -EOPNOTSUPP; > + goto out_put; > + } ok, here is where the check is done. > + > + data = ops->hw_info(idev->dev, &data_len); if we directly return type in @hw_info, this becomes: data = ops->hw_info(idev->dev, &data_len, &driver_type); > +/** > + * struct iommu_hw_info - ioctl(IOMMU_DEVICE_GET_HW_INFO) > + * @size: sizeof(struct iommu_hw_info) > + * @flags: Must be 0 > + * @dev_id: The device being attached to the iommufd "The device bound to the iommufd" > + * @data_len: Input the length of the user buffer in bytes. Output the > + * length of data filled to the user buffer. s/to/in/ > + * @data_ptr: Pointer to the type specific structure "Pointer to the user buffer" > + * @out_data_type: Output the iommu hardware info type, it is one of > + * enum iommu_hw_info_type. s/it is one of/as defined by/ > + * @__reserved: Must be 0 > + * > + * Query the hardware iommu information for given device which has been > + * bound to iommufd. @data_len is the size of the buffer which captures > + * iommu type specific data and the data will be filled. Trailing bytes > + * are zeroed if the user buffer is larger than the data kernel has. > + * > + * The type specific data would be used to sync capability between the > + * vIOMMU and the hardware IOMMU. e.g. nested translation requires to s/vIOMMU/virtual IOMMU/ > + * check the hardware IOMMU capability, since a stage-1 translation table > + * is owned by user but used by hardware IOMMU. "check ... capability so guest stage-1 page table uses a format compatible to the hardware IOMMU" > + * > + * The @out_data_type will be filled if the ioctl succeeds. It would > + * be used to decode the data filled in the buffer pointed by @data_ptr. > + * > + * This is only available for the physical devices bound to iommufd as > + * only physical devices can have hardware IOMMU. not required. User doesn't know whether it's physical or emulated device.