mutex_lock(&igroup->lock);
        attach = xa_load(&igroup->pasid_attach, pasid);
+       if (!attach) {
+               mutex_unlock(&igroup->lock);
+               return NULL;
+       }
+
        hwpt = attach->hwpt;
        hwpt_paging = find_hwpt_paging(hwpt);
 
        struct iommufd_hw_pagetable *hwpt;
 
        hwpt = iommufd_hw_pagetable_detach(idev, pasid);
+       if (!hwpt)
+               return;
        iommufd_hw_pagetable_put(idev->ictx, hwpt);
        refcount_dec(&idev->obj.users);
 }
 
        uint32_t pasid = 100;
        uint32_t viommu_id;
 
+       /*
+        * Negative, detach pasid without attaching, this is not expected.
+        * But it should not result in failure anyway.
+        */
+       test_cmd_pasid_detach(pasid);
+
        /* Allocate two nested hwpts sharing one common parent hwpt */
        test_cmd_hwpt_alloc(self->device_id, self->ioas_id,
                            IOMMU_HWPT_ALLOC_NEST_PARENT,