Skip to content

Commit 33635bd

Browse files
weiping zhangmstsirkin
authored andcommitted
virtio_pci: don't kfree device on register failure
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 vp_dev until vp_dev->vdev.dev.release be called. Signed-off-by: weiping zhang <zhangweiping@didichuxing.com> Reviewed-by: Cornelia Huck <cohuck@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent f2b44cd commit 33635bd

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

drivers/virtio/virtio_pci_common.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ static void virtio_pci_release_dev(struct device *_d)
513513
static int virtio_pci_probe(struct pci_dev *pci_dev,
514514
const struct pci_device_id *id)
515515
{
516-
struct virtio_pci_device *vp_dev;
516+
struct virtio_pci_device *vp_dev, *reg_dev = NULL;
517517
int rc;
518518

519519
/* allocate our structure and fill it out */
@@ -551,6 +551,7 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
551551
pci_set_master(pci_dev);
552552

553553
rc = register_virtio_device(&vp_dev->vdev);
554+
reg_dev = vp_dev;
554555
if (rc)
555556
goto err_register;
556557

@@ -564,7 +565,10 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
564565
err_probe:
565566
pci_disable_device(pci_dev);
566567
err_enable_device:
567-
kfree(vp_dev);
568+
if (reg_dev)
569+
put_device(&vp_dev->vdev.dev);
570+
else
571+
kfree(vp_dev);
568572
return rc;
569573
}
570574

0 commit comments

Comments
 (0)