Skip to content

Commit 6e5aa7e

Browse files
committed
virtio: reset function
A reset function solves three problems: 1) It allows us to renegotiate features, eg. if we want to upgrade a guest driver without rebooting the guest. 2) It gives us a clean way of shutting down virtqueues: after a reset, we know that the buffers won't be used by the host, and 3) It helps the guest recover from messed-up drivers. So we remove the ->shutdown hook, and the only way we now remove feature bits is via reset. We leave it to the driver to do the reset before it deletes queues: the balloon driver, for example, needs to chat to the host in its remove function. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent b3369c1 commit 6e5aa7e

8 files changed

Lines changed: 87 additions & 32 deletions

File tree

‎Documentation/lguest/lguest.c‎

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ static void *_convert(struct iovec *iov, size_t size, size_t align,
193193
#define le32_to_cpu(v32) (v32)
194194
#define le64_to_cpu(v64) (v64)
195195

196+
/* The device virtqueue descriptors are followed by feature bitmasks. */
197+
static u8 *get_feature_bits(struct device *dev)
198+
{
199+
return (u8 *)(dev->desc + 1)
200+
+ dev->desc->num_vq * sizeof(struct lguest_vqconfig);
201+
}
202+
196203
/*L:100 The Launcher code itself takes us out into userspace, that scary place
197204
* where pointers run wild and free! Unfortunately, like most userspace
198205
* programs, it's quite boring (which is why everyone likes to hack on the
@@ -914,21 +921,58 @@ static void enable_fd(int fd, struct virtqueue *vq)
914921
write(waker_fd, &vq->dev->fd, sizeof(vq->dev->fd));
915922
}
916923

924+
/* Resetting a device is fairly easy. */
925+
static void reset_device(struct device *dev)
926+
{
927+
struct virtqueue *vq;
928+
929+
verbose("Resetting device %s\n", dev->name);
930+
/* Clear the status. */
931+
dev->desc->status = 0;
932+
933+
/* Clear any features they've acked. */
934+
memset(get_feature_bits(dev) + dev->desc->feature_len, 0,
935+
dev->desc->feature_len);
936+
937+
/* Zero out the virtqueues. */
938+
for (vq = dev->vq; vq; vq = vq->next) {
939+
memset(vq->vring.desc, 0,
940+
vring_size(vq->config.num, getpagesize()));
941+
vq->last_avail_idx = 0;
942+
}
943+
}
944+
917945
/* This is the generic routine we call when the Guest uses LHCALL_NOTIFY. */
918946
static void handle_output(int fd, unsigned long addr)
919947
{
920948
struct device *i;
921949
struct virtqueue *vq;
922950

923-
/* Check each virtqueue. */
951+
/* Check each device and virtqueue. */
924952
for (i = devices.dev; i; i = i->next) {
953+
/* Notifications to device descriptors reset the device. */
954+
if (from_guest_phys(addr) == i->desc) {
955+
reset_device(i);
956+
return;
957+
}
958+
959+
/* Notifications to virtqueues mean output has occurred. */
925960
for (vq = i->vq; vq; vq = vq->next) {
926-
if (vq->config.pfn == addr/getpagesize()) {
927-
verbose("Output to %s\n", vq->dev->name);
928-
if (vq->handle_output)
929-
vq->handle_output(fd, vq);
961+
if (vq->config.pfn != addr/getpagesize())
962+
continue;
963+
964+
/* Guest should acknowledge (and set features!) before
965+
* using the device. */
966+
if (i->desc->status == 0) {
967+
warnx("%s gave early output", i->name);
930968
return;
931969
}
970+
971+
if (strcmp(vq->dev->name, "console") != 0)
972+
verbose("Output to %s\n", vq->dev->name);
973+
if (vq->handle_output)
974+
vq->handle_output(fd, vq);
975+
return;
932976
}
933977
}
934978

@@ -1074,20 +1118,18 @@ static void add_virtqueue(struct device *dev, unsigned int num_descs,
10741118
vq->vring.used->flags = VRING_USED_F_NO_NOTIFY;
10751119
}
10761120

1077-
/* The virtqueue descriptors are followed by feature bytes. */
1121+
/* The first half of the feature bitmask is for us to advertise features. The
1122+
* second half if for the Guest to accept features. */
10781123
static void add_feature(struct device *dev, unsigned bit)
10791124
{
1080-
u8 *features;
1125+
u8 *features = get_feature_bits(dev);
10811126

10821127
/* We can't extend the feature bits once we've added config bytes */
10831128
if (dev->desc->feature_len <= bit / CHAR_BIT) {
10841129
assert(dev->desc->config_len == 0);
10851130
dev->desc->feature_len = (bit / CHAR_BIT) + 1;
10861131
}
10871132

1088-
features = (u8 *)(dev->desc + 1)
1089-
+ dev->desc->num_vq * sizeof(struct lguest_vqconfig);
1090-
10911133
features[bit / CHAR_BIT] |= (1 << (bit % CHAR_BIT));
10921134
}
10931135

‎drivers/block/virtio_blk.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,16 @@ static void virtblk_remove(struct virtio_device *vdev)
264264
struct virtio_blk *vblk = vdev->priv;
265265
int major = vblk->disk->major;
266266

267+
/* Nothing should be pending. */
267268
BUG_ON(!list_empty(&vblk->reqs));
269+
270+
/* Stop all the virtqueues. */
271+
vdev->config->reset(vdev);
272+
268273
blk_cleanup_queue(vblk->disk->queue);
269274
put_disk(vblk->disk);
270275
unregister_blkdev(major, "virtblk");
271276
mempool_destroy(vblk->pool);
272-
/* There should be nothing in the queue now, so no need to shutdown */
273277
vdev->config->del_vq(vblk->vq);
274278
kfree(vblk);
275279
}

‎drivers/lguest/lguest_device.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct lguest_device {
5454
*
5555
* The configuration information for a device consists of one or more
5656
* virtqueues, a feature bitmaks, and some configuration bytes. The
57-
* configuration bytes don't really matter to us: the Launcher set them up, and
57+
* configuration bytes don't really matter to us: the Launcher sets them up, and
5858
* the driver will look at them during setup.
5959
*
6060
* A convenient routine to return the device's virtqueue config array:
@@ -139,9 +139,20 @@ static u8 lg_get_status(struct virtio_device *vdev)
139139

140140
static void lg_set_status(struct virtio_device *vdev, u8 status)
141141
{
142+
BUG_ON(!status);
142143
to_lgdev(vdev)->desc->status = status;
143144
}
144145

146+
/* To reset the device, we (ab)use the NOTIFY hypercall, with the descriptor
147+
* address of the device. The Host will zero the status and all the
148+
* features. */
149+
static void lg_reset(struct virtio_device *vdev)
150+
{
151+
unsigned long offset = (void *)to_lgdev(vdev)->desc - lguest_devices;
152+
153+
hcall(LHCALL_NOTIFY, (max_pfn<<PAGE_SHIFT) + offset, 0, 0);
154+
}
155+
145156
/*
146157
* Virtqueues
147158
*
@@ -279,6 +290,7 @@ static struct virtio_config_ops lguest_config_ops = {
279290
.set = lg_set,
280291
.get_status = lg_get_status,
281292
.set_status = lg_set_status,
293+
.reset = lg_reset,
282294
.find_vq = lg_find_vq,
283295
.del_vq = lg_del_vq,
284296
};

‎drivers/net/virtio_net.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,14 @@ static void virtnet_remove(struct virtio_device *vdev)
390390
struct virtnet_info *vi = vdev->priv;
391391
struct sk_buff *skb;
392392

393+
/* Stop all the virtqueues. */
394+
vdev->config->reset(vdev);
395+
393396
/* Free our skbs in send and recv queues, if any. */
394-
vi->rvq->vq_ops->shutdown(vi->rvq);
395397
while ((skb = __skb_dequeue(&vi->recv)) != NULL) {
396398
kfree_skb(skb);
397399
vi->num--;
398400
}
399-
vi->svq->vq_ops->shutdown(vi->svq);
400401
while ((skb = __skb_dequeue(&vi->send)) != NULL)
401402
kfree_skb(skb);
402403

‎drivers/virtio/virtio.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,13 @@ static int virtio_dev_remove(struct device *_d)
102102
struct virtio_driver *drv = container_of(dev->dev.driver,
103103
struct virtio_driver, driver);
104104

105-
dev->config->set_status(dev, dev->config->get_status(dev)
106-
& ~VIRTIO_CONFIG_S_DRIVER);
107105
drv->remove(dev);
106+
107+
/* Driver should have reset device. */
108+
BUG_ON(dev->config->get_status(dev));
109+
110+
/* Acknowledge the device's existence again. */
111+
add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
108112
return 0;
109113
}
110114

@@ -130,6 +134,10 @@ int register_virtio_device(struct virtio_device *dev)
130134
dev->dev.bus = &virtio_bus;
131135
sprintf(dev->dev.bus_id, "%u", dev->index);
132136

137+
/* We always start by resetting the device, in case a previous
138+
* driver messed it up. This also tests that code path a little. */
139+
dev->config->reset(dev);
140+
133141
/* Acknowledge that we've seen the device. */
134142
add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);
135143

‎drivers/virtio/virtio_ring.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,6 @@ static void detach_buf(struct vring_virtqueue *vq, unsigned int head)
173173
vq->num_free++;
174174
}
175175

176-
/* FIXME: We need to tell other side about removal, to synchronize. */
177-
static void vring_shutdown(struct virtqueue *_vq)
178-
{
179-
struct vring_virtqueue *vq = to_vvq(_vq);
180-
unsigned int i;
181-
182-
for (i = 0; i < vq->vring.num; i++)
183-
detach_buf(vq, i);
184-
}
185-
186176
static inline bool more_used(const struct vring_virtqueue *vq)
187177
{
188178
return vq->last_used_idx != vq->vring.used->idx;
@@ -278,7 +268,6 @@ static struct virtqueue_ops vring_vq_ops = {
278268
.kick = vring_kick,
279269
.disable_cb = vring_disable_cb,
280270
.enable_cb = vring_enable_cb,
281-
.shutdown = vring_shutdown,
282271
};
283272

284273
struct virtqueue *vring_new_virtqueue(unsigned int num,

‎include/linux/virtio.h‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ struct virtqueue
4545
* vq: the struct virtqueue we're talking about.
4646
* This returns "false" (and doesn't re-enable) if there are pending
4747
* buffers in the queue, to avoid a race.
48-
* @shutdown: "unadd" all buffers.
49-
* vq: the struct virtqueue we're talking about.
50-
* Remove everything from the queue.
5148
*
5249
* Locking rules are straightforward: the driver is responsible for
5350
* locking. No two operations may be invoked simultaneously.
@@ -67,8 +64,6 @@ struct virtqueue_ops {
6764

6865
void (*disable_cb)(struct virtqueue *vq);
6966
bool (*enable_cb)(struct virtqueue *vq);
70-
71-
void (*shutdown)(struct virtqueue *vq);
7267
};
7368

7469
/**

‎include/linux/virtio_config.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ struct virtio_device;
4343
* @set_status: write the status byte
4444
* vdev: the virtio_device
4545
* status: the new status byte
46+
* @reset: reset the device
47+
* vdev: the virtio device
48+
* After this, status and feature negotiation must be done again
4649
* @find_vq: find a virtqueue and instantiate it.
4750
* vdev: the virtio_device
4851
* index: the 0-based virtqueue number in case there's more than one.
@@ -59,6 +62,7 @@ struct virtio_config_ops
5962
const void *buf, unsigned len);
6063
u8 (*get_status)(struct virtio_device *vdev);
6164
void (*set_status)(struct virtio_device *vdev, u8 status);
65+
void (*reset)(struct virtio_device *vdev);
6266
struct virtqueue *(*find_vq)(struct virtio_device *vdev,
6367
unsigned index,
6468
void (*callback)(struct virtqueue *));

0 commit comments

Comments
 (0)