History

vfio/ap_ops: Convert to use vfio_register_group_dev()

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.

Jason Gunthorpe committed 5 years ago

Commit eb0feef1 parent 86956e72 files · +91 −66Browse files

Unified · Split

2 files
  1. drivers/s390/crypto/vfio_ap_ops.c +89 −66
  2. drivers/s390/crypto/vfio_ap_private.h +2 −0
drivers/s390/crypto/vfio_ap_ops.c+89−66
@@ -24,8 +24,9 @@ · 23 unmodified lines
24#define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"24#define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
25#define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"25#define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
26 26
27static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev);27static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
28static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);28static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
29static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
29 30
30static int match_apqn(struct device *dev, const void *data)31static 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 matrix->adm_max = info->apxa ? info->Nd : 15;327 matrix->adm_max = info->apxa ? info->Nd : 15;
327}328}
328 329
329static int vfio_ap_mdev_create(struct mdev_device *mdev)330static int vfio_ap_mdev_probe(struct mdev_device *mdev)
330{331{
331 struct ap_matrix_mdev *matrix_mdev;332 struct ap_matrix_mdev *matrix_mdev;
333 int ret;
332 334
333 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))335 if ((atomic_dec_if_positive(&matrix_dev->available_instances) < 0))
334 return -EPERM;336 return -EPERM;
335 337
336 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);338 matrix_mdev = kzalloc(sizeof(*matrix_mdev), GFP_KERNEL);
337 if (!matrix_mdev) {339 if (!matrix_mdev) {
338 atomic_inc(&matrix_dev->available_instances);340 ret = -ENOMEM;
339 return -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 matrix_mdev->mdev = mdev;346 matrix_mdev->mdev = mdev;
343 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);347 vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
344 mdev_set_drvdata(mdev, matrix_mdev);
345 matrix_mdev->pqap_hook = handle_pqap;348 matrix_mdev->pqap_hook = handle_pqap;
346 mutex_lock(&matrix_dev->lock);349 mutex_lock(&matrix_dev->lock);
347 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);350 list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
348 mutex_unlock(&matrix_dev->lock);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 return 0;357 return 0;
358
359err_list:
360 mutex_lock(&matrix_dev->lock);
361 list_del(&matrix_mdev->node);
362 mutex_unlock(&matrix_dev->lock);
363 kfree(matrix_mdev);
364err_dec_available:
365 atomic_inc(&matrix_dev->available_instances);
366 return ret;
351}367}
352 368
353static int vfio_ap_mdev_remove(struct mdev_device *mdev)369static 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 mutex_lock(&matrix_dev->lock);375 mutex_lock(&matrix_dev->lock);
358 vfio_ap_mdev_reset_queues(mdev);376 vfio_ap_mdev_reset_queues(matrix_mdev);
359 list_del(&matrix_mdev->node);377 list_del(&matrix_mdev->node);
360 kfree(matrix_mdev);378 kfree(matrix_mdev);
361 mdev_set_drvdata(mdev, NULL);
362 atomic_inc(&matrix_dev->available_instances);379 atomic_inc(&matrix_dev->available_instances);
363 mutex_unlock(&matrix_dev->lock);380 mutex_unlock(&matrix_dev->lock);
364
365 return 0;
366}381}
367 382
368static ssize_t name_show(struct mdev_type *mtype,383static 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 int ret;620 int ret;
606 unsigned long apid;621 unsigned long apid;
607 struct mdev_device *mdev = mdev_from_dev(dev);622 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
608 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
609 623
610 mutex_lock(&matrix_dev->lock);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 int ret;689 int ret;
676 unsigned long apid;690 unsigned long apid;
677 struct mdev_device *mdev = mdev_from_dev(dev);691 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
678 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
679 692
680 mutex_lock(&matrix_dev->lock);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 int ret;774 int ret;
762 unsigned long apqi;775 unsigned long apqi;
763 struct mdev_device *mdev = mdev_from_dev(dev);776 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
764 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
765 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;777 unsigned long max_apqi = matrix_mdev->matrix.aqm_max;
766 778
767 mutex_lock(&matrix_dev->lock);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 int ret;839 int ret;
828 unsigned long apqi;840 unsigned long apqi;
829 struct mdev_device *mdev = mdev_from_dev(dev);841 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
830 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
831 842
832 mutex_lock(&matrix_dev->lock);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 int ret;889 int ret;
879 unsigned long id;890 unsigned long id;
880 struct mdev_device *mdev = mdev_from_dev(dev);891 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
881 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
882 892
883 mutex_lock(&matrix_dev->lock);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 int ret;943 int ret;
934 unsigned long domid;944 unsigned long domid;
935 struct mdev_device *mdev = mdev_from_dev(dev);945 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
936 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
937 unsigned long max_domid = matrix_mdev->matrix.adm_max;946 unsigned long max_domid = matrix_mdev->matrix.adm_max;
938 947
939 mutex_lock(&matrix_dev->lock);948 mutex_lock(&matrix_dev->lock);
@@ -968,8 +977,7 @@ static ssize_t control_domains_show(struct device *dev, · 28 unmodified lines
968 int nchars = 0;977 int nchars = 0;
969 int n;978 int n;
970 char *bufpos = buf;979 char *bufpos = buf;
971 struct mdev_device *mdev = mdev_from_dev(dev);980 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
972 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
973 unsigned long max_domid = matrix_mdev->matrix.adm_max;981 unsigned long max_domid = matrix_mdev->matrix.adm_max;
974 982
975 mutex_lock(&matrix_dev->lock);983 mutex_lock(&matrix_dev->lock);
@@ -987,8 +995,7 @@ static DEVICE_ATTR_RO(control_domains); · 11 unmodified lines
987static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,995static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
988 char *buf)996 char *buf)
989{997{
990 struct mdev_device *mdev = mdev_from_dev(dev);998 struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
991 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
992 char *bufpos = buf;999 char *bufpos = buf;
993 unsigned long apid;1000 unsigned long apid;
994 unsigned long apqi;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 mutex_lock(&matrix_dev->lock);1172 mutex_lock(&matrix_dev->lock);
1166 1173
1167 kvm_arch_crypto_clear_masks(kvm);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 kvm_put_kvm(kvm);1176 kvm_put_kvm(kvm);
1170 matrix_mdev->kvm = NULL;1177 matrix_mdev->kvm = NULL;
1171 1178
@@ -1259,13 +1266,12 @@ free_resources: · 87 unmodified lines
1259 return ret;1266 return ret;
1260}1267}
1261 1268
1262static int vfio_ap_mdev_reset_queues(struct mdev_device *mdev)1269static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
1263{1270{
1264 int ret;1271 int ret;
1265 int rc = 0;1272 int rc = 0;
1266 unsigned long apid, apqi;1273 unsigned long apid, apqi;
1267 struct vfio_ap_queue *q;1274 struct vfio_ap_queue *q;
1268 struct ap_matrix_mdev *matrix_mdev = mdev_get_drvdata(mdev);
1269 1275
1270 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,1276 for_each_set_bit_inv(apid, matrix_mdev->matrix.apm,
1271 matrix_mdev->matrix.apm_max + 1) {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 return rc;1292 return rc;
1287}1293}
1288 1294
1289static int vfio_ap_mdev_open_device(struct mdev_device *mdev)1295static 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 unsigned long events;1299 unsigned long events;
1293 int ret;1300 int ret;
1294 1301
1295
1296 if (!try_module_get(THIS_MODULE))
1297 return -ENODEV;
1298
1299 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;1302 matrix_mdev->group_notifier.notifier_call = vfio_ap_mdev_group_notifier;
1300 events = VFIO_GROUP_NOTIFY_SET_KVM;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 &events, &matrix_mdev->group_notifier);1306 &events, &matrix_mdev->group_notifier);
1304 if (ret) {1307 if (ret)
1305 module_put(THIS_MODULE);
1306 return ret;1308 return ret;
1307 }
1308 1309
1309 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;1310 matrix_mdev->iommu_notifier.notifier_call = vfio_ap_mdev_iommu_notifier;
1310 events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;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 &events, &matrix_mdev->iommu_notifier);1313 &events, &matrix_mdev->iommu_notifier);
1313 if (!ret)1314 if (ret)
1314 return ret;1315 goto out_unregister_group;
1316 return 0;
1315 1317
1316 vfio_unregister_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY,1318out_unregister_group:
1319 vfio_unregister_notifier(vdev->dev, VFIO_GROUP_NOTIFY,
1317 &matrix_mdev->group_notifier);1320 &matrix_mdev->group_notifier);
1318 module_put(THIS_MODULE);
1319 return ret;1321 return ret;
1320}1322}
1321 1323
1322static void vfio_ap_mdev_close_device(struct mdev_device *mdev)1324static 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 &matrix_mdev->iommu_notifier);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 &matrix_mdev->group_notifier);1332 &matrix_mdev->group_notifier);
1330 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);1333 vfio_ap_mdev_unset_kvm(matrix_mdev, matrix_mdev->kvm);
1331 module_put(THIS_MODULE);
1332}1334}
1333 1335
1334static int vfio_ap_mdev_get_device_info(unsigned long arg)1336static 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 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;1353 return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
1352}1354}
1353 1355
1354static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev,1356static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
1355 unsigned int cmd, unsigned long arg)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 int ret;1361 int ret;
1358 struct ap_matrix_mdev *matrix_mdev;
1359 1362
1360 mutex_lock(&matrix_dev->lock);1363 mutex_lock(&matrix_dev->lock);
1361 switch (cmd) {1364 switch (cmd) {
@@ -1363,13 +1366,7 @@ static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev, · 1 unmodified line
1363 ret = vfio_ap_mdev_get_device_info(arg);1366 ret = vfio_ap_mdev_get_device_info(arg);
1364 break;1367 break;
1365 case VFIO_DEVICE_RESET:1368 case VFIO_DEVICE_RESET:
1366 matrix_mdev = mdev_get_drvdata(mdev);1369 ret = vfio_ap_mdev_reset_queues(matrix_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);
1373 break;1370 break;
1374 default:1371 default:
1375 ret = -EOPNOTSUPP;1372 ret = -EOPNOTSUPP;
@@ -1380,25 +1377,51 @@ static ssize_t vfio_ap_mdev_ioctl(struct mdev_device *mdev, · 4 unmodified lines
1380 return ret;1377 return ret;
1381}1378}
1382 1379
1380static 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
1386static 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
1383static const struct mdev_parent_ops vfio_ap_matrix_ops = {1397static const struct mdev_parent_ops vfio_ap_matrix_ops = {
1384 .owner = THIS_MODULE,1398 .owner = THIS_MODULE,
1399 .device_driver = &vfio_ap_matrix_driver,
1385 .supported_type_groups = vfio_ap_mdev_type_groups,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
1394int vfio_ap_mdev_register(void)1403int vfio_ap_mdev_register(void)
1395{1404{
1405 int ret;
1406
1396 atomic_set(&matrix_dev->available_instances, MAX_ZDEV_ENTRIES_EXT);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
1418err_driver:
1419 mdev_unregister_driver(&vfio_ap_matrix_driver);
1420 return ret;
1399}1421}
1400 1422
1401void vfio_ap_mdev_unregister(void)1423void vfio_ap_mdev_unregister(void)
1402{1424{
1403 mdev_unregister_device(&matrix_dev->device);1425 mdev_unregister_device(&matrix_dev->device);
1426 mdev_unregister_driver(&vfio_ap_matrix_driver);
1404}1427}
drivers/s390/crypto/vfio_ap_private.h+2−0
@@ -18,6 +18,7 @@ · 17 unmodified lines
18#include <linux/delay.h>18#include <linux/delay.h>
19#include <linux/mutex.h>19#include <linux/mutex.h>
20#include <linux/kvm_host.h>20#include <linux/kvm_host.h>
21#include <linux/vfio.h>
21 22
22#include "ap_bus.h"23#include "ap_bus.h"
23 24
@@ -79,6 +80,7 @@ struct ap_matrix { · 55 unmodified lines
79 * @kvm: the struct holding guest's state80 * @kvm: the struct holding guest's state
80 */81 */
81struct ap_matrix_mdev {82struct ap_matrix_mdev {
83 struct vfio_device vdev;
82 struct list_head node;84 struct list_head node;
83 struct ap_matrix matrix;85 struct ap_matrix matrix;
84 struct notifier_block group_notifier;86 struct notifier_block group_notifier;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t