diff options
author | Jiri Slaby <jslaby@suse.cz> | 2010-11-06 05:06:50 -0400 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2010-11-23 23:51:12 -0500 |
commit | 22e132ff2645aab1e1a25c45e9544a39ae1dc106 (patch) | |
tree | 2202ed81de7b2afd7f1ce568fc0222497e464de2 | |
parent | 7ae4b866f86f9ab7b99484b56dd303a860ad1cc9 (diff) |
Char: virtio_console, fix memory leak
Stanse found that in init_vqs, memory is leaked under certain
circumstanses (the fail path order is incorrect). Fix that by checking
allocations in one turn and free all of them at once if some fails
(some may be NULL, but this is OK).
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Cc: Amit Shah <amit.shah@redhat.com>
Cc: virtualization@lists.linux-foundation.org
Cc: Greg Kroah-Hartman <gregkh@suse.de>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
-rw-r--r-- | drivers/char/virtio_console.c | 37 |
1 files changed, 9 insertions, 28 deletions
diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c index 6c1b676643a9..896a2ced1d27 100644 --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c | |||
@@ -1547,31 +1547,16 @@ static int init_vqs(struct ports_device *portdev) | |||
1547 | nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2; | 1547 | nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2; |
1548 | 1548 | ||
1549 | vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL); | 1549 | vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL); |
1550 | if (!vqs) { | ||
1551 | err = -ENOMEM; | ||
1552 | goto fail; | ||
1553 | } | ||
1554 | io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL); | 1550 | io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL); |
1555 | if (!io_callbacks) { | ||
1556 | err = -ENOMEM; | ||
1557 | goto free_vqs; | ||
1558 | } | ||
1559 | io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL); | 1551 | io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL); |
1560 | if (!io_names) { | ||
1561 | err = -ENOMEM; | ||
1562 | goto free_callbacks; | ||
1563 | } | ||
1564 | portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), | 1552 | portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), |
1565 | GFP_KERNEL); | 1553 | GFP_KERNEL); |
1566 | if (!portdev->in_vqs) { | ||
1567 | err = -ENOMEM; | ||
1568 | goto free_names; | ||
1569 | } | ||
1570 | portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), | 1554 | portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *), |
1571 | GFP_KERNEL); | 1555 | GFP_KERNEL); |
1572 | if (!portdev->out_vqs) { | 1556 | if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs || |
1557 | !portdev->out_vqs) { | ||
1573 | err = -ENOMEM; | 1558 | err = -ENOMEM; |
1574 | goto free_invqs; | 1559 | goto free; |
1575 | } | 1560 | } |
1576 | 1561 | ||
1577 | /* | 1562 | /* |
@@ -1605,7 +1590,7 @@ static int init_vqs(struct ports_device *portdev) | |||
1605 | io_callbacks, | 1590 | io_callbacks, |
1606 | (const char **)io_names); | 1591 | (const char **)io_names); |
1607 | if (err) | 1592 | if (err) |
1608 | goto free_outvqs; | 1593 | goto free; |
1609 | 1594 | ||
1610 | j = 0; | 1595 | j = 0; |
1611 | portdev->in_vqs[0] = vqs[0]; | 1596 | portdev->in_vqs[0] = vqs[0]; |
@@ -1621,23 +1606,19 @@ static int init_vqs(struct ports_device *portdev) | |||
1621 | portdev->out_vqs[i] = vqs[j + 1]; | 1606 | portdev->out_vqs[i] = vqs[j + 1]; |
1622 | } | 1607 | } |
1623 | } | 1608 | } |
1624 | kfree(io_callbacks); | ||
1625 | kfree(io_names); | 1609 | kfree(io_names); |
1610 | kfree(io_callbacks); | ||
1626 | kfree(vqs); | 1611 | kfree(vqs); |
1627 | 1612 | ||
1628 | return 0; | 1613 | return 0; |
1629 | 1614 | ||
1630 | free_names: | 1615 | free: |
1631 | kfree(io_names); | ||
1632 | free_callbacks: | ||
1633 | kfree(io_callbacks); | ||
1634 | free_outvqs: | ||
1635 | kfree(portdev->out_vqs); | 1616 | kfree(portdev->out_vqs); |
1636 | free_invqs: | ||
1637 | kfree(portdev->in_vqs); | 1617 | kfree(portdev->in_vqs); |
1638 | free_vqs: | 1618 | kfree(io_names); |
1619 | kfree(io_callbacks); | ||
1639 | kfree(vqs); | 1620 | kfree(vqs); |
1640 | fail: | 1621 | |
1641 | return err; | 1622 | return err; |
1642 | } | 1623 | } |
1643 | 1624 | ||