This is straightforward conversion, the ap_matrix_mdev is actually serving as the vfio_device and we can replace all the mdev_get_drvdata()'s with a simple container_of() or a dev_get_drvdata() for sysfs paths.
Unified · Split
| @@ -24,8 +24,9 @@ · 23 unmodified lines | ||
| 24 | 24 | #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough" |
| 25 | 25 | #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device" |
| 26 | 26 | |
| 27 | −static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev); | |
| 27 | +static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev); | |
| 28 | 28 | static struct vfio_ap_queue *vfio_ap_find_queue(int apqn); |
| 29 | +static const struct vfio_device_ops vfio_ap_matrix_dev_ops; | |
| 29 | 30 | |
| 30 | 31 | static int match_apqn(struct device *dev, const void *data) |
| 31 | 32 | { |
| @@ -326,43 +327,57 @@ static void vfio_ap_matrix_init(struct ap_config_info *info, · 294 unmodified lines | ||
| 326 | 327 | matrix->adm_max = info->apxa ? info->Nd : 15; |
| 327 | 328 | } |
| 328 | 329 | |
| 329 | −static int vfio_ap_mdev_create(struct mdev_device *mdev) | |
| 330 | +static int vfio_ap_mdev_probe(struct mdev_device *mdev) | |
| 330 | 331 | { |
| 331 | 332 | struct ap_matrix_mdev *matrix_mdev; |
| 333 | + int ret; | |
| 332 | 334 | |
| 333 | 335 | if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0)) |
| 334 | 336 | return -EPERM; |
| 335 | 337 | |
| 336 | 338 | matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL); |
| 337 | 339 | if (!matrix_mdev) { |
| 338 | − atomic_inc(&matrix_dev->available_instances); | |
| 339 | − return -ENOMEM; | |
| 340 | + ret = -ENOMEM; | |
| 341 | + goto err_dec_available; | |
| 340 | 342 | } |
| 343 | + vfio_init_group_dev(&matrix_mdev->vdev, &mdev->dev, | |
| 344 | + &vfio_ap_matrix_dev_ops); | |
| 341 | 345 | |
| 342 | 346 | matrix_mdev->mdev = mdev; |
| 343 | 347 | vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix); |
| 344 | − mdev_set_drvdata(mdev, matrix_mdev); | |
| 345 | 348 | matrix_mdev->pqap_hook = handle_pqap; |
| 346 | 349 | mutex_lock(&matrix_dev->lock); |
| 347 | 350 | list_add(&matrix_mdev->node, &matrix_dev->mdev_list); |
| 348 | 351 | mutex_unlock(&matrix_dev->lock); |
| 349 | 352 | |
| 353 | + ret = vfio_register_group_dev(&matrix_mdev->vdev); | |
| 354 | + if (ret) | |
| 355 | + goto err_list; | |
| 356 | + dev_set_drvdata(&mdev->dev, matrix_mdev); | |
| 350 | 357 | return 0; |
| 358 | + | |
| 359 | +err_list: | |
| 360 | + mutex_lock(&matrix_dev->lock); | |
| 361 | + list_del(&matrix_mdev->node); | |
| 362 | + mutex_unlock(&matrix_dev->lock); | |
| 363 | + kfree(matrix_mdev); | |
| 364 | +err_dec_available: | |
| 365 | + atomic_inc(&matrix_dev->available_instances); | |
| 366 | + return ret; | |
| 351 | 367 | } |
| 352 | 368 | |
| 353 | −static int vfio_ap_mdev_remove(struct mdev_device *mdev) | |
| 369 | +static void vfio_ap_mdev_remove(struct mdev_device *mdev) | |
| 354 | 370 | { |
| 355 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 371 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev); | |
| 372 | + | |
| 373 | + vfio_unregister_group_dev(&matrix_mdev->vdev); | |
| 356 | 374 | |
| 357 | 375 | mutex_lock(&matrix_dev->lock); |
| 358 | − vfio_ap_mdev_reset_queues(mdev); | |
| 376 | + vfio_ap_mdev_reset_queues(matrix_mdev); | |
| 359 | 377 | list_del(&matrix_mdev->node); |
| 360 | 378 | kfree(matrix_mdev); |
| 361 | − mdev_set_drvdata(mdev, NULL); | |
| 362 | 379 | atomic_inc(&matrix_dev->available_instances); |
| 363 | 380 | mutex_unlock(&matrix_dev->lock); |
| 364 | − | |
| 365 | − return 0; | |
| 366 | 381 | } |
| 367 | 382 | |
| 368 | 383 | static ssize_t name_show(struct mdev_type *mtype, |
| @@ -604,8 +619,7 @@ static ssize_t assign_adapter_store(struct device *dev, · 235 unmodified lines | ||
| 604 | 619 | { |
| 605 | 620 | int ret; |
| 606 | 621 | unsigned long apid; |
| 607 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 608 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 622 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 609 | 623 | |
| 610 | 624 | mutex_lock(&matrix_dev->lock); |
| 611 | 625 | |
| @@ -674,8 +688,7 @@ static ssize_t unassign_adapter_store(struct device *dev, · 62 unmodified lines | ||
| 674 | 688 | { |
| 675 | 689 | int ret; |
| 676 | 690 | unsigned long apid; |
| 677 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 678 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 691 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 679 | 692 | |
| 680 | 693 | mutex_lock(&matrix_dev->lock); |
| 681 | 694 | |
| @@ -760,8 +773,7 @@ static ssize_t assign_domain_store(struct device *dev, · 78 unmodified lines | ||
| 760 | 773 | { |
| 761 | 774 | int ret; |
| 762 | 775 | unsigned long apqi; |
| 763 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 764 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 776 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 765 | 777 | unsigned long max_apqi = matrix_mdev->matrix.aqm_max; |
| 766 | 778 | |
| 767 | 779 | mutex_lock(&matrix_dev->lock); |
| @@ -826,8 +838,7 @@ static ssize_t unassign_domain_store(struct device *dev, · 58 unmodified lines | ||
| 826 | 838 | { |
| 827 | 839 | int ret; |
| 828 | 840 | unsigned long apqi; |
| 829 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 830 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 841 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 831 | 842 | |
| 832 | 843 | mutex_lock(&matrix_dev->lock); |
| 833 | 844 | |
| @@ -877,8 +888,7 @@ static ssize_t assign_control_domain_store(struct device *dev, · 43 unmodified lines | ||
| 877 | 888 | { |
| 878 | 889 | int ret; |
| 879 | 890 | unsigned long id; |
| 880 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 881 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 891 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 882 | 892 | |
| 883 | 893 | mutex_lock(&matrix_dev->lock); |
| 884 | 894 | |
| @@ -932,8 +942,7 @@ static ssize_t unassign_control_domain_store(struct device *dev, · 47 unmodified lines | ||
| 932 | 942 | { |
| 933 | 943 | int ret; |
| 934 | 944 | unsigned long domid; |
| 935 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 936 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 945 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 937 | 946 | unsigned long max_domid = matrix_mdev->matrix.adm_max; |
| 938 | 947 | |
| 939 | 948 | mutex_lock(&matrix_dev->lock); |
| @@ -968,8 +977,7 @@ static ssize_t control_domains_show(struct device *dev, · 28 unmodified lines | ||
| 968 | 977 | int nchars = 0; |
| 969 | 978 | int n; |
| 970 | 979 | char *bufpos = buf; |
| 971 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 972 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 980 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 973 | 981 | unsigned long max_domid = matrix_mdev->matrix.adm_max; |
| 974 | 982 | |
| 975 | 983 | mutex_lock(&matrix_dev->lock); |
| @@ -987,8 +995,7 @@ static DEVICE_ATTR_RO(control_domains); · 11 unmodified lines | ||
| 987 | 995 | static ssize_t matrix_show(struct device *dev, struct device_attribute *attr, |
| 988 | 996 | char *buf) |
| 989 | 997 | { |
| 990 | − struct mdev_device *mdev = mdev_from_dev(dev); | |
| 991 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 998 | + struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev); | |
| 992 | 999 | char *bufpos = buf; |
| 993 | 1000 | unsigned long apid; |
| 994 | 1001 | unsigned long apqi; |
| @@ -1165,7 +1172,7 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev, · 170 unmodified lines | ||
| 1165 | 1172 | mutex_lock(&matrix_dev->lock); |
| 1166 | 1173 | |
| 1167 | 1174 | kvm_arch_crypto_clear_masks(kvm); |
| 1168 | − vfio_ap_mdev_reset_queues(matrix_mdev->mdev); | |
| 1175 | + vfio_ap_mdev_reset_queues(matrix_mdev); | |
| 1169 | 1176 | kvm_put_kvm(kvm); |
| 1170 | 1177 | matrix_mdev->kvm = NULL; |
| 1171 | 1178 | |
| @@ -1259,13 +1266,12 @@ free_resources: · 87 unmodified lines | ||
| 1259 | 1266 | return ret; |
| 1260 | 1267 | } |
| 1261 | 1268 | |
| 1262 | −static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev) | |
| 1269 | +static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev) | |
| 1263 | 1270 | { |
| 1264 | 1271 | int ret; |
| 1265 | 1272 | int rc = 0; |
| 1266 | 1273 | unsigned long apid, apqi; |
| 1267 | 1274 | struct vfio_ap_queue *q; |
| 1268 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 1269 | 1275 | |
| 1270 | 1276 | for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, |
| 1271 | 1277 | matrix_mdev->matrix.apm_max + 1) { |
| @@ -1286,49 +1292,45 @@ static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev) · 14 unmodified lines | ||
| 1286 | 1292 | return rc; |
| 1287 | 1293 | } |
| 1288 | 1294 | |
| 1289 | −static int vfio_ap_mdev_open_device(struct mdev_device *mdev) | |
| 1295 | +static int vfio_ap_mdev_open_device(struct vfio_device *vdev) | |
| 1290 | 1296 | { |
| 1291 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 1297 | + struct ap_matrix_mdev *matrix_mdev = | |
| 1298 | + container_of(vdev, struct ap_matrix_mdev, vdev); | |
| 1292 | 1299 | unsigned long events; |
| 1293 | 1300 | int ret; |
| 1294 | 1301 | |
| 1295 | − | |
| 1296 | − if (!try_module_get(THIS_MODULE)) | |
| 1297 | − return -ENODEV; | |
| 1298 | − | |
| 1299 | 1302 | matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier; |
| 1300 | 1303 | events = VFIO_GROUP_NOTIFY_SET_KVM; |
| 1301 | 1304 | |
| 1302 | − ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, | |
| 1305 | + ret = vfio_register_notifier(vdev->dev, VFIO_GROUP_NOTIFY, | |
| 1303 | 1306 | &events, &matrix_mdev->group_notifier); |
| 1304 | − if (ret) { | |
| 1305 | − module_put(THIS_MODULE); | |
| 1307 | + if (ret) | |
| 1306 | 1308 | return ret; |
| 1307 | − } | |
| 1308 | 1309 | |
| 1309 | 1310 | matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier; |
| 1310 | 1311 | events = VFIO_IOMMU_NOTIFY_DMA_UNMAP; |
| 1311 | − ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, | |
| 1312 | + ret = vfio_register_notifier(vdev->dev, VFIO_IOMMU_NOTIFY, | |
| 1312 | 1313 | &events, &matrix_mdev->iommu_notifier); |
| 1313 | − if (!ret) | |
| 1314 | − return ret; | |
| 1314 | + if (ret) | |
| 1315 | + goto out_unregister_group; | |
| 1316 | + return 0; | |
| 1315 | 1317 | |
| 1316 | − vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, | |
| 1318 | +out_unregister_group: | |
| 1319 | + vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY, | |
| 1317 | 1320 | &matrix_mdev->group_notifier); |
| 1318 | − module_put(THIS_MODULE); | |
| 1319 | 1321 | return ret; |
| 1320 | 1322 | } |
| 1321 | 1323 | |
| 1322 | −static void vfio_ap_mdev_close_device(struct mdev_device *mdev) | |
| 1324 | +static void vfio_ap_mdev_close_device(struct vfio_device *vdev) | |
| 1323 | 1325 | { |
| 1324 | − struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev); | |
| 1326 | + struct ap_matrix_mdev *matrix_mdev = | |
| 1327 | + container_of(vdev, struct ap_matrix_mdev, vdev); | |
| 1325 | 1328 | |
| 1326 | − vfio_unregister_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, | |
| 1329 | + vfio_unregister_notifier(vdev->dev, VFIO_IOMMU_NOTIFY, | |
| 1327 | 1330 | &matrix_mdev->iommu_notifier); |
| 1328 | − vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, | |
| 1331 | + vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY, | |
| 1329 | 1332 | &matrix_mdev->group_notifier); |
| 1330 | 1333 | vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm); |
| 1331 | − module_put(THIS_MODULE); | |
| 1332 | 1334 | } |
| 1333 | 1335 | |
| 1334 | 1336 | static int vfio_ap_mdev_get_device_info(unsigned long arg) |
| @@ -1351,11 +1353,12 @@ static int vfio_ap_mdev_get_device_info(unsigned long arg) · 16 unmodified lines | ||
| 1351 | 1353 | return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0; |
| 1352 | 1354 | } |
| 1353 | 1355 | |
| 1354 | −static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev, | |
| 1356 | +static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev, | |
| 1355 | 1357 | unsigned int cmd, unsigned long arg) |
| 1356 | 1358 | { |
| 1359 | + struct ap_matrix_mdev *matrix_mdev = | |
| 1360 | + container_of(vdev, struct ap_matrix_mdev, vdev); | |
| 1357 | 1361 | int ret; |
| 1358 | − struct ap_matrix_mdev *matrix_mdev; | |
| 1359 | 1362 | |
| 1360 | 1363 | mutex_lock(&matrix_dev->lock); |
| 1361 | 1364 | switch (cmd) { |
| @@ -1363,13 +1366,7 @@ static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev, · 1 unmodified line | ||
| 1363 | 1366 | ret = vfio_ap_mdev_get_device_info(arg); |
| 1364 | 1367 | break; |
| 1365 | 1368 | case VFIO_DEVICE_RESET: |
| 1366 | − matrix_mdev = mdev_get_drvdata(mdev); | |
| 1367 | − if (WARN(!matrix_mdev, "Driver data missing from mdev!!")) { | |
| 1368 | − ret = -EINVAL; | |
| 1369 | − break; | |
| 1370 | − } | |
| 1371 | − | |
| 1372 | − ret = vfio_ap_mdev_reset_queues(mdev); | |
| 1369 | + ret = vfio_ap_mdev_reset_queues(matrix_mdev); | |
| 1373 | 1370 | break; |
| 1374 | 1371 | default: |
| 1375 | 1372 | ret = -EOPNOTSUPP; |
| @@ -1380,25 +1377,51 @@ static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev, · 4 unmodified lines | ||
| 1380 | 1377 | return ret; |
| 1381 | 1378 | } |
| 1382 | 1379 | |
| 1380 | +static const struct vfio_device_ops vfio_ap_matrix_dev_ops = { | |
| 1381 | + .open_device = vfio_ap_mdev_open_device, | |
| 1382 | + .close_device = vfio_ap_mdev_close_device, | |
| 1383 | + .ioctl = vfio_ap_mdev_ioctl, | |
| 1384 | +}; | |
| 1385 | + | |
| 1386 | +static struct mdev_driver vfio_ap_matrix_driver = { | |
| 1387 | + .driver = { | |
| 1388 | + .name = "vfio_ap_mdev", | |
| 1389 | + .owner = THIS_MODULE, | |
| 1390 | + .mod_name = KBUILD_MODNAME, | |
| 1391 | + .dev_groups = vfio_ap_mdev_attr_groups, | |
| 1392 | + }, | |
| 1393 | + .probe = vfio_ap_mdev_probe, | |
| 1394 | + .remove = vfio_ap_mdev_remove, | |
| 1395 | +}; | |
| 1396 | + | |
| 1383 | 1397 | static const struct mdev_parent_ops vfio_ap_matrix_ops = { |
| 1384 | 1398 | .owner = THIS_MODULE, |
| 1399 | + .device_driver = &vfio_ap_matrix_driver, | |
| 1385 | 1400 | .supported_type_groups = vfio_ap_mdev_type_groups, |
| 1386 | − .mdev_attr_groups = vfio_ap_mdev_attr_groups, | |
| 1387 | − .create = vfio_ap_mdev_create, | |
| 1388 | − .remove = vfio_ap_mdev_remove, | |
| 1389 | − .open_device = vfio_ap_mdev_open_device, | |
| 1390 | − .close_device = vfio_ap_mdev_close_device, | |
| 1391 | − .ioctl = vfio_ap_mdev_ioctl, | |
| 1392 | 1401 | }; |
| 1393 | 1402 | |
| 1394 | 1403 | int vfio_ap_mdev_register(void) |
| 1395 | 1404 | { |
| 1405 | + int ret; | |
| 1406 | + | |
| 1396 | 1407 | atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT); |
| 1397 | 1408 | |
| 1398 | − return mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops); | |
| 1409 | + ret = mdev_register_driver(&vfio_ap_matrix_driver); | |
| 1410 | + if (ret) | |
| 1411 | + return ret; | |
| 1412 | + | |
| 1413 | + ret = mdev_register_device(&matrix_dev->device, &vfio_ap_matrix_ops); | |
| 1414 | + if (ret) | |
| 1415 | + goto err_driver; | |
| 1416 | + return 0; | |
| 1417 | + | |
| 1418 | +err_driver: | |
| 1419 | + mdev_unregister_driver(&vfio_ap_matrix_driver); | |
| 1420 | + return ret; | |
| 1399 | 1421 | } |
| 1400 | 1422 | |
| 1401 | 1423 | void vfio_ap_mdev_unregister(void) |
| 1402 | 1424 | { |
| 1403 | 1425 | mdev_unregister_device(&matrix_dev->device); |
| 1426 | + mdev_unregister_driver(&vfio_ap_matrix_driver); | |
| 1404 | 1427 | } |
| @@ -18,6 +18,7 @@ · 17 unmodified lines | ||
| 18 | 18 | #include <linux/delay.h> |
| 19 | 19 | #include <linux/mutex.h> |
| 20 | 20 | #include <linux/kvm_host.h> |
| 21 | +#include <linux/vfio.h> | |
| 21 | 22 | |
| 22 | 23 | #include "ap_bus.h" |
| 23 | 24 | |
| @@ -79,6 +80,7 @@ struct ap_matrix { · 55 unmodified lines | ||
| 79 | 80 | * @kvm: the struct holding guest's state |
| 80 | 81 | */ |
| 81 | 82 | struct ap_matrix_mdev { |
| 83 | + struct vfio_device vdev; | |
| 82 | 84 | struct list_head node; |
| 83 | 85 | struct ap_matrix matrix; |
| 84 | 86 | struct notifier_block group_notifier; |