Skip to content

Commit

Permalink
virtio_mmio: add cleanup for virtio_mmio_probe
Browse files Browse the repository at this point in the history
As mentioned at drivers/base/core.c:
/*
 * NOTE: _Never_ directly free @dev after calling this function, even
 * if it returned an error! Always use put_device() to give up the
 * reference initialized in this function instead.
 */
so we don't free vm_dev until vm_dev.dev.release be called.

Signed-off-by: weiping zhang <zhangweiping@didichuxing.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Cornelia Huck <cohuck@redhat.com>
  • Loading branch information
weiping zhang authored and Michael S. Tsirkin committed Dec 7, 2017
1 parent d9e427f commit 7eb781b
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions drivers/virtio/virtio_mmio.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,16 @@ static const struct virtio_config_ops virtio_mmio_config_ops = {
};


static void virtio_mmio_release_dev_empty(struct device *_d) {}
static void virtio_mmio_release_dev(struct device *_d)
{
struct virtio_device *vdev =
container_of(_d, struct virtio_device, dev);
struct virtio_mmio_device *vm_dev =
container_of(vdev, struct virtio_mmio_device, vdev);
struct platform_device *pdev = vm_dev->pdev;

devm_kfree(&pdev->dev, vm_dev);
}

/* Platform device */

Expand All @@ -513,33 +522,39 @@ static int virtio_mmio_probe(struct platform_device *pdev)
return -EBUSY;

vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
if (!vm_dev)
return -ENOMEM;
if (!vm_dev) {
rc = -ENOMEM;
goto free_mem;
}

vm_dev->vdev.dev.parent = &pdev->dev;
vm_dev->vdev.dev.release = virtio_mmio_release_dev_empty;
vm_dev->vdev.dev.release = virtio_mmio_release_dev;
vm_dev->vdev.config = &virtio_mmio_config_ops;
vm_dev->pdev = pdev;
INIT_LIST_HEAD(&vm_dev->virtqueues);
spin_lock_init(&vm_dev->lock);

vm_dev->base = devm_ioremap(&pdev->dev, mem->start, resource_size(mem));
if (vm_dev->base == NULL)
return -EFAULT;
if (vm_dev->base == NULL) {
rc = -EFAULT;
goto free_vmdev;
}

/* Check magic value */
magic = readl(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE);
if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
return -ENODEV;
rc = -ENODEV;
goto unmap;
}

/* Check device version */
vm_dev->version = readl(vm_dev->base + VIRTIO_MMIO_VERSION);
if (vm_dev->version < 1 || vm_dev->version > 2) {
dev_err(&pdev->dev, "Version %ld not supported!\n",
vm_dev->version);
return -ENXIO;
rc = -ENXIO;
goto unmap;
}

vm_dev->vdev.id.device = readl(vm_dev->base + VIRTIO_MMIO_DEVICE_ID);
Expand All @@ -548,7 +563,8 @@ static int virtio_mmio_probe(struct platform_device *pdev)
* virtio-mmio device with an ID 0 is a (dummy) placeholder
* with no function. End probing now with no error reported.
*/
return -ENODEV;
rc = -ENODEV;
goto unmap;
}
vm_dev->vdev.id.vendor = readl(vm_dev->base + VIRTIO_MMIO_VENDOR_ID);

Expand All @@ -573,7 +589,22 @@ static int virtio_mmio_probe(struct platform_device *pdev)

platform_set_drvdata(pdev, vm_dev);

return register_virtio_device(&vm_dev->vdev);
rc = register_virtio_device(&vm_dev->vdev);
if (rc) {
iounmap(vm_dev->base);
devm_release_mem_region(&pdev->dev, mem->start,
resource_size(mem));
put_device(&vm_dev->vdev.dev);
}
return rc;
unmap:
iounmap(vm_dev->base);
free_mem:
devm_release_mem_region(&pdev->dev, mem->start,
resource_size(mem));
free_vmdev:
devm_kfree(&pdev->dev, vm_dev);
return rc;
}

static int virtio_mmio_remove(struct platform_device *pdev)
Expand Down

0 comments on commit 7eb781b

Please sign in to comment.