iommufd/device: Add helper to detect the first attach of a group

The existing code detects the first attach by checking the
igroup->device_list. However, the igroup->hwpt can also be used to detect
the first attach. In future modifications, it is better to check the
igroup->hwpt instead of the device_list. To improve readbility and also
prepare for further modifications on this part, this adds a helper for it.

Link: https://patch.msgid.link/r/20250321171940.7213-7-yi.l.liu@intel.com
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com>
Reviewed-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Yi Liu <yi.l.liu@intel.com>
Tested-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
pull/1193/head
Yi Liu 2025-03-21 10:19:28 -07:00 committed by Jason Gunthorpe
parent 2eaa7f845e
commit ba1de6cd41
1 changed files with 9 additions and 2 deletions

View File

@ -329,6 +329,13 @@ iommufd_group_setup_msi(struct iommufd_group *igroup,
}
#endif
static bool
iommufd_group_first_attach(struct iommufd_group *igroup, ioasid_t pasid)
{
lockdep_assert_held(&igroup->lock);
return !igroup->hwpt;
}
static int
iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
struct iommufd_hwpt_paging *hwpt_paging)
@ -344,7 +351,7 @@ iommufd_device_attach_reserved_iova(struct iommufd_device *idev,
if (rc)
return rc;
if (list_empty(&igroup->device_list)) {
if (iommufd_group_first_attach(igroup, IOMMU_NO_PASID)) {
rc = iommufd_group_setup_msi(igroup, hwpt_paging);
if (rc) {
iopt_remove_reserved_iova(&hwpt_paging->ioas->iopt,
@ -508,7 +515,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
* reserved regions are only updated during individual device
* attachment.
*/
if (list_empty(&igroup->device_list)) {
if (iommufd_group_first_attach(igroup, pasid)) {
rc = iommufd_hwpt_attach_device(hwpt, idev, pasid);
if (rc)
goto err_unresv;