@@ -67,7 +67,8 @@ struct vfio_iommu {
67
67
struct list_head iova_list ;
68
68
struct mutex lock ;
69
69
struct rb_root dma_list ;
70
- struct blocking_notifier_head notifier ;
70
+ struct list_head device_list ;
71
+ struct mutex device_list_lock ;
71
72
unsigned int dma_avail ;
72
73
unsigned int vaddr_invalid_count ;
73
74
uint64_t pgsize_bitmap ;
@@ -865,8 +866,8 @@ static int vfio_iommu_type1_pin_pages(void *iommu_data,
865
866
}
866
867
}
867
868
868
- /* Fail if notifier list is empty */
869
- if (! iommu -> notifier . head ) {
869
+ /* Fail if no dma_umap notifier is registered */
870
+ if (list_empty ( & iommu -> device_list ) ) {
870
871
ret = - EINVAL ;
871
872
goto pin_done ;
872
873
}
@@ -1287,6 +1288,35 @@ static int verify_bitmap_size(uint64_t npages, uint64_t bitmap_size)
1287
1288
return 0 ;
1288
1289
}
1289
1290
1291
+ /*
1292
+ * Notify VFIO drivers using vfio_register_emulated_iommu_dev() to invalidate
1293
+ * and unmap iovas within the range we're about to unmap. Drivers MUST unpin
1294
+ * pages in response to an invalidation.
1295
+ */
1296
+ static void vfio_notify_dma_unmap (struct vfio_iommu * iommu ,
1297
+ struct vfio_dma * dma )
1298
+ {
1299
+ struct vfio_device * device ;
1300
+
1301
+ if (list_empty (& iommu -> device_list ))
1302
+ return ;
1303
+
1304
+ /*
1305
+ * The device is expected to call vfio_unpin_pages() for any IOVA it has
1306
+ * pinned within the range. Since vfio_unpin_pages() will eventually
1307
+ * call back down to this code and try to obtain the iommu->lock we must
1308
+ * drop it.
1309
+ */
1310
+ mutex_lock (& iommu -> device_list_lock );
1311
+ mutex_unlock (& iommu -> lock );
1312
+
1313
+ list_for_each_entry (device , & iommu -> device_list , iommu_entry )
1314
+ device -> ops -> dma_unmap (device , dma -> iova , dma -> size );
1315
+
1316
+ mutex_unlock (& iommu -> device_list_lock );
1317
+ mutex_lock (& iommu -> lock );
1318
+ }
1319
+
1290
1320
static int vfio_dma_do_unmap (struct vfio_iommu * iommu ,
1291
1321
struct vfio_iommu_type1_dma_unmap * unmap ,
1292
1322
struct vfio_bitmap * bitmap )
@@ -1400,29 +1430,14 @@ static int vfio_dma_do_unmap(struct vfio_iommu *iommu,
1400
1430
}
1401
1431
1402
1432
if (!RB_EMPTY_ROOT (& dma -> pfn_list )) {
1403
- struct vfio_iommu_type1_dma_unmap nb_unmap ;
1404
-
1405
1433
if (dma_last == dma ) {
1406
1434
BUG_ON (++ retries > 10 );
1407
1435
} else {
1408
1436
dma_last = dma ;
1409
1437
retries = 0 ;
1410
1438
}
1411
1439
1412
- nb_unmap .iova = dma -> iova ;
1413
- nb_unmap .size = dma -> size ;
1414
-
1415
- /*
1416
- * Notify anyone (mdev vendor drivers) to invalidate and
1417
- * unmap iovas within the range we're about to unmap.
1418
- * Vendor drivers MUST unpin pages in response to an
1419
- * invalidation.
1420
- */
1421
- mutex_unlock (& iommu -> lock );
1422
- blocking_notifier_call_chain (& iommu -> notifier ,
1423
- VFIO_IOMMU_NOTIFY_DMA_UNMAP ,
1424
- & nb_unmap );
1425
- mutex_lock (& iommu -> lock );
1440
+ vfio_notify_dma_unmap (iommu , dma );
1426
1441
goto again ;
1427
1442
}
1428
1443
@@ -2475,7 +2490,7 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,
2475
2490
2476
2491
if (list_empty (& iommu -> emulated_iommu_groups ) &&
2477
2492
list_empty (& iommu -> domain_list )) {
2478
- WARN_ON (iommu -> notifier . head );
2493
+ WARN_ON (! list_empty ( & iommu -> device_list ) );
2479
2494
vfio_iommu_unmap_unpin_all (iommu );
2480
2495
}
2481
2496
goto detach_group_done ;
@@ -2507,7 +2522,8 @@ static void vfio_iommu_type1_detach_group(void *iommu_data,
2507
2522
if (list_empty (& domain -> group_list )) {
2508
2523
if (list_is_singular (& iommu -> domain_list )) {
2509
2524
if (list_empty (& iommu -> emulated_iommu_groups )) {
2510
- WARN_ON (iommu -> notifier .head );
2525
+ WARN_ON (!list_empty (
2526
+ & iommu -> device_list ));
2511
2527
vfio_iommu_unmap_unpin_all (iommu );
2512
2528
} else {
2513
2529
vfio_iommu_unmap_unpin_reaccount (iommu );
@@ -2568,7 +2584,8 @@ static void *vfio_iommu_type1_open(unsigned long arg)
2568
2584
iommu -> dma_avail = dma_entry_limit ;
2569
2585
iommu -> container_open = true;
2570
2586
mutex_init (& iommu -> lock );
2571
- BLOCKING_INIT_NOTIFIER_HEAD (& iommu -> notifier );
2587
+ mutex_init (& iommu -> device_list_lock );
2588
+ INIT_LIST_HEAD (& iommu -> device_list );
2572
2589
init_waitqueue_head (& iommu -> vaddr_wait );
2573
2590
iommu -> pgsize_bitmap = PAGE_MASK ;
2574
2591
INIT_LIST_HEAD (& iommu -> emulated_iommu_groups );
@@ -3005,28 +3022,40 @@ static long vfio_iommu_type1_ioctl(void *iommu_data,
3005
3022
}
3006
3023
}
3007
3024
3008
- static int vfio_iommu_type1_register_notifier (void * iommu_data ,
3009
- unsigned long * events ,
3010
- struct notifier_block * nb )
3025
+ static void vfio_iommu_type1_register_device (void * iommu_data ,
3026
+ struct vfio_device * vdev )
3011
3027
{
3012
3028
struct vfio_iommu * iommu = iommu_data ;
3013
3029
3014
- /* clear known events */
3015
- * events &= ~VFIO_IOMMU_NOTIFY_DMA_UNMAP ;
3016
-
3017
- /* refuse to register if still events remaining */
3018
- if (* events )
3019
- return - EINVAL ;
3030
+ if (!vdev -> ops -> dma_unmap )
3031
+ return ;
3020
3032
3021
- return blocking_notifier_chain_register (& iommu -> notifier , nb );
3033
+ /*
3034
+ * list_empty(&iommu->device_list) is tested under the iommu->lock while
3035
+ * iteration for dma_unmap must be done under the device_list_lock.
3036
+ * Holding both locks here allows avoiding the device_list_lock in
3037
+ * several fast paths. See vfio_notify_dma_unmap()
3038
+ */
3039
+ mutex_lock (& iommu -> lock );
3040
+ mutex_lock (& iommu -> device_list_lock );
3041
+ list_add (& vdev -> iommu_entry , & iommu -> device_list );
3042
+ mutex_unlock (& iommu -> device_list_lock );
3043
+ mutex_unlock (& iommu -> lock );
3022
3044
}
3023
3045
3024
- static int vfio_iommu_type1_unregister_notifier (void * iommu_data ,
3025
- struct notifier_block * nb )
3046
+ static void vfio_iommu_type1_unregister_device (void * iommu_data ,
3047
+ struct vfio_device * vdev )
3026
3048
{
3027
3049
struct vfio_iommu * iommu = iommu_data ;
3028
3050
3029
- return blocking_notifier_chain_unregister (& iommu -> notifier , nb );
3051
+ if (!vdev -> ops -> dma_unmap )
3052
+ return ;
3053
+
3054
+ mutex_lock (& iommu -> lock );
3055
+ mutex_lock (& iommu -> device_list_lock );
3056
+ list_del (& vdev -> iommu_entry );
3057
+ mutex_unlock (& iommu -> device_list_lock );
3058
+ mutex_unlock (& iommu -> lock );
3030
3059
}
3031
3060
3032
3061
static int vfio_iommu_type1_dma_rw_chunk (struct vfio_iommu * iommu ,
@@ -3160,8 +3189,8 @@ static const struct vfio_iommu_driver_ops vfio_iommu_driver_ops_type1 = {
3160
3189
.detach_group = vfio_iommu_type1_detach_group ,
3161
3190
.pin_pages = vfio_iommu_type1_pin_pages ,
3162
3191
.unpin_pages = vfio_iommu_type1_unpin_pages ,
3163
- .register_notifier = vfio_iommu_type1_register_notifier ,
3164
- .unregister_notifier = vfio_iommu_type1_unregister_notifier ,
3192
+ .register_device = vfio_iommu_type1_register_device ,
3193
+ .unregister_device = vfio_iommu_type1_unregister_device ,
3165
3194
.dma_rw = vfio_iommu_type1_dma_rw ,
3166
3195
.group_iommu_domain = vfio_iommu_type1_group_iommu_domain ,
3167
3196
.notify = vfio_iommu_type1_notify ,
0 commit comments