@@ -332,7 +332,14 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
332332
333333 /* Try to find a domain */
334334 QLIST_FOREACH (hwpt , & container -> hwpt_list , next ) {
335- ret = iommufd_cdev_attach_ioas_hwpt (vbasedev , hwpt -> hwpt_id , errp );
335+ if (!cpr_is_incoming ()) {
336+ ret = iommufd_cdev_attach_ioas_hwpt (vbasedev , hwpt -> hwpt_id , errp );
337+ } else if (vbasedev -> cpr .hwpt_id == hwpt -> hwpt_id ) {
338+ ret = 0 ;
339+ } else {
340+ continue ;
341+ }
342+
336343 if (ret ) {
337344 /* -EINVAL means the domain is incompatible with the device. */
338345 if (ret == - EINVAL ) {
@@ -349,6 +356,7 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
349356 return false;
350357 } else {
351358 vbasedev -> hwpt = hwpt ;
359+ vbasedev -> cpr .hwpt_id = hwpt -> hwpt_id ;
352360 QLIST_INSERT_HEAD (& hwpt -> device_list , vbasedev , hwpt_next );
353361 vbasedev -> iommu_dirty_tracking = iommufd_hwpt_dirty_tracking (hwpt );
354362 return true;
@@ -371,26 +379,32 @@ static bool iommufd_cdev_autodomains_get(VFIODevice *vbasedev,
371379 flags = IOMMU_HWPT_ALLOC_DIRTY_TRACKING ;
372380 }
373381
382+ if (cpr_is_incoming ()) {
383+ hwpt_id = vbasedev -> cpr .hwpt_id ;
384+ goto skip_alloc ;
385+ }
386+
374387 if (!iommufd_backend_alloc_hwpt (iommufd , vbasedev -> devid ,
375388 container -> ioas_id , flags ,
376389 IOMMU_HWPT_DATA_NONE , 0 , NULL ,
377390 & hwpt_id , errp )) {
378391 return false;
379392 }
380393
394+ ret = iommufd_cdev_attach_ioas_hwpt (vbasedev , hwpt_id , errp );
395+ if (ret ) {
396+ iommufd_backend_free_id (container -> be , hwpt_id );
397+ return false;
398+ }
399+
400+ skip_alloc :
381401 hwpt = g_malloc0 (sizeof (* hwpt ));
382402 hwpt -> hwpt_id = hwpt_id ;
383403 hwpt -> hwpt_flags = flags ;
384404 QLIST_INIT (& hwpt -> device_list );
385405
386- ret = iommufd_cdev_attach_ioas_hwpt (vbasedev , hwpt -> hwpt_id , errp );
387- if (ret ) {
388- iommufd_backend_free_id (container -> be , hwpt -> hwpt_id );
389- g_free (hwpt );
390- return false;
391- }
392-
393406 vbasedev -> hwpt = hwpt ;
407+ vbasedev -> cpr .hwpt_id = hwpt -> hwpt_id ;
394408 vbasedev -> iommu_dirty_tracking = iommufd_hwpt_dirty_tracking (hwpt );
395409 QLIST_INSERT_HEAD (& hwpt -> device_list , vbasedev , hwpt_next );
396410 QLIST_INSERT_HEAD (& container -> hwpt_list , hwpt , next );
0 commit comments