summaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorAlex Williamson <alex.williamson@redhat.com>2021-06-26 00:20:06 +0300
committerAlex Williamson <alex.williamson@redhat.com>2021-06-28 23:09:01 +0300
commit97d0a6874478802b68e3bea7aa9b9a333d257182 (patch)
treefdc11ba1b81e755a993fc1d80200e5dc0159ad33 /samples
parent0dd1b7fc3e7d30802d5839f6bf8957023b437ad4 (diff)
downloadlinux-97d0a6874478802b68e3bea7aa9b9a333d257182.tar.xz
vfio/mtty: Enforce available_instances
The sample mtty mdev driver doesn't actually enforce the number of device instances it claims are available. Implement this properly. Link: https://lore.kernel.org/r/162465624894.3338367.12935940647049917981.stgit@omen Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Reviewed-by: Cornelia Huck <cohuck@redhat.com> Reviewed by: Kirti Wankhede <kwankhede@nvidia.com> Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Diffstat (limited to 'samples')
-rw-r--r--samples/vfio-mdev/mtty.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c
index ffbaf07a17ea..8b26fecc4afe 100644
--- a/samples/vfio-mdev/mtty.c
+++ b/samples/vfio-mdev/mtty.c
@@ -144,7 +144,7 @@ struct mdev_state {
int nr_ports;
};
-static atomic_t mdev_used_ports;
+static atomic_t mdev_avail_ports = ATOMIC_INIT(MAX_MTTYS);
static const struct file_operations vd_fops = {
.owner = THIS_MODULE,
@@ -707,11 +707,20 @@ static int mtty_probe(struct mdev_device *mdev)
{
struct mdev_state *mdev_state;
int nr_ports = mdev_get_type_group_id(mdev) + 1;
+ int avail_ports = atomic_read(&mdev_avail_ports);
int ret;
+ do {
+ if (avail_ports < nr_ports)
+ return -ENOSPC;
+ } while (!atomic_try_cmpxchg(&mdev_avail_ports,
+ &avail_ports, avail_ports - nr_ports));
+
mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
- if (mdev_state == NULL)
+ if (mdev_state == NULL) {
+ atomic_add(nr_ports, &mdev_avail_ports);
return -ENOMEM;
+ }
vfio_init_group_dev(&mdev_state->vdev, &mdev->dev, &mtty_dev_ops);
@@ -724,6 +733,7 @@ static int mtty_probe(struct mdev_device *mdev)
if (mdev_state->vconfig == NULL) {
kfree(mdev_state);
+ atomic_add(nr_ports, &mdev_avail_ports);
return -ENOMEM;
}
@@ -735,9 +745,9 @@ static int mtty_probe(struct mdev_device *mdev)
ret = vfio_register_group_dev(&mdev_state->vdev);
if (ret) {
kfree(mdev_state);
+ atomic_add(nr_ports, &mdev_avail_ports);
return ret;
}
- atomic_add(mdev_state->nr_ports, &mdev_used_ports);
dev_set_drvdata(&mdev->dev, mdev_state);
return 0;
@@ -746,12 +756,13 @@ static int mtty_probe(struct mdev_device *mdev)
static void mtty_remove(struct mdev_device *mdev)
{
struct mdev_state *mdev_state = dev_get_drvdata(&mdev->dev);
+ int nr_ports = mdev_state->nr_ports;
- atomic_sub(mdev_state->nr_ports, &mdev_used_ports);
vfio_unregister_group_dev(&mdev_state->vdev);
kfree(mdev_state->vconfig);
kfree(mdev_state);
+ atomic_add(nr_ports, &mdev_avail_ports);
}
static int mtty_reset(struct mdev_state *mdev_state)
@@ -1271,8 +1282,7 @@ static ssize_t available_instances_show(struct mdev_type *mtype,
{
unsigned int ports = mtype_get_type_group_id(mtype) + 1;
- return sprintf(buf, "%d\n",
- (MAX_MTTYS - atomic_read(&mdev_used_ports)) / ports);
+ return sprintf(buf, "%d\n", atomic_read(&mdev_avail_ports) / ports);
}
static MDEV_TYPE_ATTR_RO(available_instances);