Skip to content

Commit 0644f18

Browse files
committed
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
Pull virtio fixups from Michael Tsirkin: - Latest header update will break QEMU (if it's rebuilt with the new header) - and it seems that the code there is so fragile that any change in this header will break it. Add a better interface so users do not need to change their code every time that header changes. - Fix virtio console for spec compliance. * tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost: virtio_console: reset on out of memory virtio_console: move removal code virtio_console: drop custom control queue cleanup virtio_console: free buffers after reset virtio: add ability to iterate over vqs virtio_console: don't tie bufs to a vq virtio_balloon: add array of stat names
2 parents 0871062 + 5c60300 commit 0644f18

File tree

3 files changed

+89
-86
lines changed

3 files changed

+89
-86
lines changed

drivers/char/virtio_console.c

Lines changed: 71 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ static void reclaim_dma_bufs(void)
422422
}
423423
}
424424

425-
static struct port_buffer *alloc_buf(struct virtqueue *vq, size_t buf_size,
425+
static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
426426
int pages)
427427
{
428428
struct port_buffer *buf;
@@ -445,16 +445,16 @@ static struct port_buffer *alloc_buf(struct virtqueue *vq, size_t buf_size,
445445
return buf;
446446
}
447447

448-
if (is_rproc_serial(vq->vdev)) {
448+
if (is_rproc_serial(vdev)) {
449449
/*
450450
* Allocate DMA memory from ancestor. When a virtio
451451
* device is created by remoteproc, the DMA memory is
452452
* associated with the grandparent device:
453453
* vdev => rproc => platform-dev.
454454
*/
455-
if (!vq->vdev->dev.parent || !vq->vdev->dev.parent->parent)
455+
if (!vdev->dev.parent || !vdev->dev.parent->parent)
456456
goto free_buf;
457-
buf->dev = vq->vdev->dev.parent->parent;
457+
buf->dev = vdev->dev.parent->parent;
458458

459459
/* Increase device refcnt to avoid freeing it */
460460
get_device(buf->dev);
@@ -838,7 +838,7 @@ static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
838838

839839
count = min((size_t)(32 * 1024), count);
840840

841-
buf = alloc_buf(port->out_vq, count, 0);
841+
buf = alloc_buf(port->portdev->vdev, count, 0);
842842
if (!buf)
843843
return -ENOMEM;
844844

@@ -957,7 +957,7 @@ static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
957957
if (ret < 0)
958958
goto error_out;
959959

960-
buf = alloc_buf(port->out_vq, 0, pipe->nrbufs);
960+
buf = alloc_buf(port->portdev->vdev, 0, pipe->nrbufs);
961961
if (!buf) {
962962
ret = -ENOMEM;
963963
goto error_out;
@@ -1374,7 +1374,7 @@ static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
13741374

13751375
nr_added_bufs = 0;
13761376
do {
1377-
buf = alloc_buf(vq, PAGE_SIZE, 0);
1377+
buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
13781378
if (!buf)
13791379
break;
13801380

@@ -1402,7 +1402,6 @@ static int add_port(struct ports_device *portdev, u32 id)
14021402
{
14031403
char debugfs_name[16];
14041404
struct port *port;
1405-
struct port_buffer *buf;
14061405
dev_t devt;
14071406
unsigned int nr_added_bufs;
14081407
int err;
@@ -1513,8 +1512,6 @@ static int add_port(struct ports_device *portdev, u32 id)
15131512
return 0;
15141513

15151514
free_inbufs:
1516-
while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1517-
free_buf(buf, true);
15181515
free_device:
15191516
device_destroy(pdrvdata.class, port->dev->devt);
15201517
free_cdev:
@@ -1539,34 +1536,14 @@ static void remove_port(struct kref *kref)
15391536

15401537
static void remove_port_data(struct port *port)
15411538
{
1542-
struct port_buffer *buf;
1543-
15441539
spin_lock_irq(&port->inbuf_lock);
15451540
/* Remove unused data this port might have received. */
15461541
discard_port_data(port);
15471542
spin_unlock_irq(&port->inbuf_lock);
15481543

1549-
/* Remove buffers we queued up for the Host to send us data in. */
1550-
do {
1551-
spin_lock_irq(&port->inbuf_lock);
1552-
buf = virtqueue_detach_unused_buf(port->in_vq);
1553-
spin_unlock_irq(&port->inbuf_lock);
1554-
if (buf)
1555-
free_buf(buf, true);
1556-
} while (buf);
1557-
15581544
spin_lock_irq(&port->outvq_lock);
15591545
reclaim_consumed_buffers(port);
15601546
spin_unlock_irq(&port->outvq_lock);
1561-
1562-
/* Free pending buffers from the out-queue. */
1563-
do {
1564-
spin_lock_irq(&port->outvq_lock);
1565-
buf = virtqueue_detach_unused_buf(port->out_vq);
1566-
spin_unlock_irq(&port->outvq_lock);
1567-
if (buf)
1568-
free_buf(buf, true);
1569-
} while (buf);
15701547
}
15711548

15721549
/*
@@ -1791,13 +1768,24 @@ static void control_work_handler(struct work_struct *work)
17911768
spin_unlock(&portdev->c_ivq_lock);
17921769
}
17931770

1771+
static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1772+
{
1773+
struct port_buffer *buf;
1774+
unsigned int len;
1775+
1776+
while ((buf = virtqueue_get_buf(vq, &len)))
1777+
free_buf(buf, can_sleep);
1778+
}
1779+
17941780
static void out_intr(struct virtqueue *vq)
17951781
{
17961782
struct port *port;
17971783

17981784
port = find_port_by_vq(vq->vdev->priv, vq);
1799-
if (!port)
1785+
if (!port) {
1786+
flush_bufs(vq, false);
18001787
return;
1788+
}
18011789

18021790
wake_up_interruptible(&port->waitqueue);
18031791
}
@@ -1808,8 +1796,10 @@ static void in_intr(struct virtqueue *vq)
18081796
unsigned long flags;
18091797

18101798
port = find_port_by_vq(vq->vdev->priv, vq);
1811-
if (!port)
1799+
if (!port) {
1800+
flush_bufs(vq, false);
18121801
return;
1802+
}
18131803

18141804
spin_lock_irqsave(&port->inbuf_lock, flags);
18151805
port->inbuf = get_inbuf(port);
@@ -1984,24 +1974,54 @@ static const struct file_operations portdev_fops = {
19841974

19851975
static void remove_vqs(struct ports_device *portdev)
19861976
{
1977+
struct virtqueue *vq;
1978+
1979+
virtio_device_for_each_vq(portdev->vdev, vq) {
1980+
struct port_buffer *buf;
1981+
1982+
flush_bufs(vq, true);
1983+
while ((buf = virtqueue_detach_unused_buf(vq)))
1984+
free_buf(buf, true);
1985+
}
19871986
portdev->vdev->config->del_vqs(portdev->vdev);
19881987
kfree(portdev->in_vqs);
19891988
kfree(portdev->out_vqs);
19901989
}
19911990

1992-
static void remove_controlq_data(struct ports_device *portdev)
1991+
static void virtcons_remove(struct virtio_device *vdev)
19931992
{
1994-
struct port_buffer *buf;
1995-
unsigned int len;
1993+
struct ports_device *portdev;
1994+
struct port *port, *port2;
19961995

1997-
if (!use_multiport(portdev))
1998-
return;
1996+
portdev = vdev->priv;
19991997

2000-
while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
2001-
free_buf(buf, true);
1998+
spin_lock_irq(&pdrvdata_lock);
1999+
list_del(&portdev->list);
2000+
spin_unlock_irq(&pdrvdata_lock);
20022001

2003-
while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
2004-
free_buf(buf, true);
2002+
/* Disable interrupts for vqs */
2003+
vdev->config->reset(vdev);
2004+
/* Finish up work that's lined up */
2005+
if (use_multiport(portdev))
2006+
cancel_work_sync(&portdev->control_work);
2007+
else
2008+
cancel_work_sync(&portdev->config_work);
2009+
2010+
list_for_each_entry_safe(port, port2, &portdev->ports, list)
2011+
unplug_port(port);
2012+
2013+
unregister_chrdev(portdev->chr_major, "virtio-portsdev");
2014+
2015+
/*
2016+
* When yanking out a device, we immediately lose the
2017+
* (device-side) queues. So there's no point in keeping the
2018+
* guest side around till we drop our final reference. This
2019+
* also means that any ports which are in an open state will
2020+
* have to just stop using the port, as the vqs are going
2021+
* away.
2022+
*/
2023+
remove_vqs(portdev);
2024+
kfree(portdev);
20052025
}
20062026

20072027
/*
@@ -2070,6 +2090,7 @@ static int virtcons_probe(struct virtio_device *vdev)
20702090

20712091
spin_lock_init(&portdev->ports_lock);
20722092
INIT_LIST_HEAD(&portdev->ports);
2093+
INIT_LIST_HEAD(&portdev->list);
20732094

20742095
virtio_device_ready(portdev->vdev);
20752096

@@ -2087,8 +2108,15 @@ static int virtcons_probe(struct virtio_device *vdev)
20872108
if (!nr_added_bufs) {
20882109
dev_err(&vdev->dev,
20892110
"Error allocating buffers for control queue\n");
2090-
err = -ENOMEM;
2091-
goto free_vqs;
2111+
/*
2112+
* The host might want to notify mgmt sw about device
2113+
* add failure.
2114+
*/
2115+
__send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2116+
VIRTIO_CONSOLE_DEVICE_READY, 0);
2117+
/* Device was functional: we need full cleanup. */
2118+
virtcons_remove(vdev);
2119+
return -ENOMEM;
20922120
}
20932121
} else {
20942122
/*
@@ -2119,11 +2147,6 @@ static int virtcons_probe(struct virtio_device *vdev)
21192147

21202148
return 0;
21212149

2122-
free_vqs:
2123-
/* The host might want to notify mgmt sw about device add failure */
2124-
__send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2125-
VIRTIO_CONSOLE_DEVICE_READY, 0);
2126-
remove_vqs(portdev);
21272150
free_chrdev:
21282151
unregister_chrdev(portdev->chr_major, "virtio-portsdev");
21292152
free:
@@ -2132,43 +2155,6 @@ static int virtcons_probe(struct virtio_device *vdev)
21322155
return err;
21332156
}
21342157

2135-
static void virtcons_remove(struct virtio_device *vdev)
2136-
{
2137-
struct ports_device *portdev;
2138-
struct port *port, *port2;
2139-
2140-
portdev = vdev->priv;
2141-
2142-
spin_lock_irq(&pdrvdata_lock);
2143-
list_del(&portdev->list);
2144-
spin_unlock_irq(&pdrvdata_lock);
2145-
2146-
/* Disable interrupts for vqs */
2147-
vdev->config->reset(vdev);
2148-
/* Finish up work that's lined up */
2149-
if (use_multiport(portdev))
2150-
cancel_work_sync(&portdev->control_work);
2151-
else
2152-
cancel_work_sync(&portdev->config_work);
2153-
2154-
list_for_each_entry_safe(port, port2, &portdev->ports, list)
2155-
unplug_port(port);
2156-
2157-
unregister_chrdev(portdev->chr_major, "virtio-portsdev");
2158-
2159-
/*
2160-
* When yanking out a device, we immediately lose the
2161-
* (device-side) queues. So there's no point in keeping the
2162-
* guest side around till we drop our final reference. This
2163-
* also means that any ports which are in an open state will
2164-
* have to just stop using the port, as the vqs are going
2165-
* away.
2166-
*/
2167-
remove_controlq_data(portdev);
2168-
remove_vqs(portdev);
2169-
kfree(portdev);
2170-
}
2171-
21722158
static struct virtio_device_id id_table[] = {
21732159
{ VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
21742160
{ 0 },
@@ -2209,7 +2195,6 @@ static int virtcons_freeze(struct virtio_device *vdev)
22092195
*/
22102196
if (use_multiport(portdev))
22112197
virtqueue_disable_cb(portdev->c_ivq);
2212-
remove_controlq_data(portdev);
22132198

22142199
list_for_each_entry(port, &portdev->ports, list) {
22152200
virtqueue_disable_cb(port->in_vq);

include/linux/virtio.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ int virtio_device_freeze(struct virtio_device *dev);
157157
int virtio_device_restore(struct virtio_device *dev);
158158
#endif
159159

160+
#define virtio_device_for_each_vq(vdev, vq) \
161+
list_for_each_entry(vq, &vdev->vqs, list)
162+
160163
/**
161164
* virtio_driver - operations for a virtio I/O driver
162165
* @driver: underlying device driver (populate name and owner).

include/uapi/linux/virtio_balloon.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ struct virtio_balloon_config {
5757
#define VIRTIO_BALLOON_S_HTLB_PGFAIL 9 /* Hugetlb page allocation failures */
5858
#define VIRTIO_BALLOON_S_NR 10
5959

60+
#define VIRTIO_BALLOON_S_NAMES_WITH_PREFIX(VIRTIO_BALLOON_S_NAMES_prefix) { \
61+
VIRTIO_BALLOON_S_NAMES_prefix "swap-in", \
62+
VIRTIO_BALLOON_S_NAMES_prefix "swap-out", \
63+
VIRTIO_BALLOON_S_NAMES_prefix "major-faults", \
64+
VIRTIO_BALLOON_S_NAMES_prefix "minor-faults", \
65+
VIRTIO_BALLOON_S_NAMES_prefix "free-memory", \
66+
VIRTIO_BALLOON_S_NAMES_prefix "total-memory", \
67+
VIRTIO_BALLOON_S_NAMES_prefix "available-memory", \
68+
VIRTIO_BALLOON_S_NAMES_prefix "disk-caches", \
69+
VIRTIO_BALLOON_S_NAMES_prefix "hugetlb-allocations", \
70+
VIRTIO_BALLOON_S_NAMES_prefix "hugetlb-failures" \
71+
}
72+
73+
#define VIRTIO_BALLOON_S_NAMES VIRTIO_BALLOON_S_NAMES_WITH_PREFIX("")
74+
6075
/*
6176
* Memory statistics structure.
6277
* Driver fills an array of these structures and passes to device.

0 commit comments

Comments
 (0)