A new SET_OPTION will reuse this helper for a sanity check before setting
a per-idev property.

Given that the attach handle can be NULL if device is not attached, add a
pointer check prior to the xa_load(). This is not a problem currently, as
the only caller iommufd_device_do_replace() verifies the attach handle.

Also, add lockdep_assert_held on igroup's mutex;

Signed-off-by: Nicolin Chen <[email protected]>
---
 drivers/iommu/iommufd/iommufd_private.h | 1 +
 drivers/iommu/iommufd/device.c          | 7 ++++---
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/iommu/iommufd/iommufd_private.h 
b/drivers/iommu/iommufd/iommufd_private.h
index cc758610b9f7c..c458ab16736b6 100644
--- a/drivers/iommu/iommufd/iommufd_private.h
+++ b/drivers/iommu/iommufd/iommufd_private.h
@@ -502,6 +502,7 @@ iommufd_get_device(struct iommufd_ucmd *ucmd, u32 id)
 
 void iommufd_device_pre_destroy(struct iommufd_object *obj);
 void iommufd_device_destroy(struct iommufd_object *obj);
+bool iommufd_device_is_attached(struct iommufd_device *idev, ioasid_t pasid);
 int iommufd_get_hw_info(struct iommufd_ucmd *ucmd);
 
 struct iommufd_access {
diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c
index 2a816533dc10e..45a1d1603c009 100644
--- a/drivers/iommu/iommufd/device.c
+++ b/drivers/iommu/iommufd/device.c
@@ -436,13 +436,14 @@ iommufd_device_attach_reserved_iova(struct iommufd_device 
*idev,
 
 /* The device attach/detach/replace helpers for attach_handle */
 
-static bool iommufd_device_is_attached(struct iommufd_device *idev,
-                                      ioasid_t pasid)
+bool iommufd_device_is_attached(struct iommufd_device *idev, ioasid_t pasid)
 {
        struct iommufd_attach *attach;
 
+       lockdep_assert_held(&idev->igroup->lock);
+
        attach = xa_load(&idev->igroup->pasid_attach, pasid);
-       return xa_load(&attach->device_array, idev->obj.id);
+       return attach && xa_load(&attach->device_array, idev->obj.id);
 }
 
 static int iommufd_hwpt_pasid_compat(struct iommufd_hw_pagetable *hwpt,
-- 
2.43.0


Reply via email to