Skip to content

Commit 17bb6d4

Browse files
Jason Wangrustyrussell
authored andcommitted
virtio-ring: move queue_index to vring_virtqueue
Instead of storing the queue index in transport-specific virtio structs, this patch moves them to vring_virtqueue and introduces an helper to get the value. This lets drivers simplify their management and tracing of virtqueues. Signed-off-by: Jason Wang <jasowang@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 7a23eb2 commit 17bb6d4

8 files changed

Lines changed: 29 additions & 21 deletions

File tree

‎drivers/lguest/lguest_device.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static struct virtqueue *lg_find_vq(struct virtio_device *vdev,
296296
* to 'true': the host just a(nother) SMP CPU, so we only need inter-cpu
297297
* barriers.
298298
*/
299-
vq = vring_new_virtqueue(lvq->config.num, LGUEST_VRING_ALIGN, vdev,
299+
vq = vring_new_virtqueue(index, lvq->config.num, LGUEST_VRING_ALIGN, vdev,
300300
true, lvq->pages, lg_notify, callback, name);
301301
if (!vq) {
302302
err = -ENOMEM;

‎drivers/remoteproc/remoteproc_virtio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
103103
* Create the new vq, and tell virtio we're not interested in
104104
* the 'weak' smp barriers, since we're talking with a real device.
105105
*/
106-
vq = vring_new_virtqueue(len, rvring->align, vdev, false, addr,
106+
vq = vring_new_virtqueue(id, len, rvring->align, vdev, false, addr,
107107
rproc_virtio_notify, callback, name);
108108
if (!vq) {
109109
dev_err(dev, "vring_new_virtqueue %s failed\n", name);

‎drivers/s390/kvm/kvm_virtio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static struct virtqueue *kvm_find_vq(struct virtio_device *vdev,
198198
if (err)
199199
goto out;
200200

201-
vq = vring_new_virtqueue(config->num, KVM_S390_VIRTIO_RING_ALIGN,
201+
vq = vring_new_virtqueue(index, config->num, KVM_S390_VIRTIO_RING_ALIGN,
202202
vdev, true, (void *) config->address,
203203
kvm_notify, callback, name);
204204
if (!vq) {

‎drivers/virtio/virtio_mmio.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,6 @@ struct virtio_mmio_vq_info {
131131
/* the number of entries in the queue */
132132
unsigned int num;
133133

134-
/* the index of the queue */
135-
int queue_index;
136-
137134
/* the virtual address of the ring queue */
138135
void *queue;
139136

@@ -225,11 +222,10 @@ static void vm_reset(struct virtio_device *vdev)
225222
static void vm_notify(struct virtqueue *vq)
226223
{
227224
struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
228-
struct virtio_mmio_vq_info *info = vq->priv;
229225

230226
/* We write the queue's selector into the notification register to
231227
* signal the other end */
232-
writel(info->queue_index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
228+
writel(virtqueue_get_queue_index(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
233229
}
234230

235231
/* Notify all virtqueues on an interrupt. */
@@ -270,6 +266,7 @@ static void vm_del_vq(struct virtqueue *vq)
270266
struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
271267
struct virtio_mmio_vq_info *info = vq->priv;
272268
unsigned long flags, size;
269+
unsigned int index = virtqueue_get_queue_index(vq);
273270

274271
spin_lock_irqsave(&vm_dev->lock, flags);
275272
list_del(&info->node);
@@ -278,7 +275,7 @@ static void vm_del_vq(struct virtqueue *vq)
278275
vring_del_virtqueue(vq);
279276

280277
/* Select and deactivate the queue */
281-
writel(info->queue_index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
278+
writel(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL);
282279
writel(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
283280

284281
size = PAGE_ALIGN(vring_size(info->num, VIRTIO_MMIO_VRING_ALIGN));
@@ -324,7 +321,6 @@ static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
324321
err = -ENOMEM;
325322
goto error_kmalloc;
326323
}
327-
info->queue_index = index;
328324

329325
/* Allocate pages for the queue - start with a queue as big as
330326
* possible (limited by maximum size allowed by device), drop down
@@ -356,7 +352,7 @@ static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
356352
vm_dev->base + VIRTIO_MMIO_QUEUE_PFN);
357353

358354
/* Create the vring */
359-
vq = vring_new_virtqueue(info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
355+
vq = vring_new_virtqueue(index, info->num, VIRTIO_MMIO_VRING_ALIGN, vdev,
360356
true, info->queue, vm_notify, callback, name);
361357
if (!vq) {
362358
err = -ENOMEM;

‎drivers/virtio/virtio_pci.c‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ struct virtio_pci_vq_info
7979
/* the number of entries in the queue */
8080
int num;
8181

82-
/* the index of the queue */
83-
int queue_index;
84-
8582
/* the virtual address of the ring queue */
8683
void *queue;
8784

@@ -202,11 +199,11 @@ static void vp_reset(struct virtio_device *vdev)
202199
static void vp_notify(struct virtqueue *vq)
203200
{
204201
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
205-
struct virtio_pci_vq_info *info = vq->priv;
206202

207203
/* we write the queue's selector into the notification register to
208204
* signal the other end */
209-
iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
205+
iowrite16(virtqueue_get_queue_index(vq),
206+
vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY);
210207
}
211208

212209
/* Handle a configuration change: Tell driver if it wants to know. */
@@ -402,7 +399,6 @@ static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
402399
if (!info)
403400
return ERR_PTR(-ENOMEM);
404401

405-
info->queue_index = index;
406402
info->num = num;
407403
info->msix_vector = msix_vec;
408404

@@ -418,7 +414,7 @@ static struct virtqueue *setup_vq(struct virtio_device *vdev, unsigned index,
418414
vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN);
419415

420416
/* create the vring */
421-
vq = vring_new_virtqueue(info->num, VIRTIO_PCI_VRING_ALIGN, vdev,
417+
vq = vring_new_virtqueue(index, info->num, VIRTIO_PCI_VRING_ALIGN, vdev,
422418
true, info->queue, vp_notify, callback, name);
423419
if (!vq) {
424420
err = -ENOMEM;
@@ -467,7 +463,8 @@ static void vp_del_vq(struct virtqueue *vq)
467463
list_del(&info->node);
468464
spin_unlock_irqrestore(&vp_dev->lock, flags);
469465

470-
iowrite16(info->queue_index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
466+
iowrite16(virtqueue_get_queue_index(vq),
467+
vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);
471468

472469
if (vp_dev->msix_enabled) {
473470
iowrite16(VIRTIO_MSI_NO_VECTOR,

‎drivers/virtio/virtio_ring.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ struct vring_virtqueue
106106
/* How to notify other side. FIXME: commonalize hcalls! */
107107
void (*notify)(struct virtqueue *vq);
108108

109+
/* Index of the queue */
110+
int queue_index;
111+
109112
#ifdef DEBUG
110113
/* They're supposed to lock for us. */
111114
unsigned int in_use;
@@ -171,6 +174,13 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
171174
return head;
172175
}
173176

177+
int virtqueue_get_queue_index(struct virtqueue *_vq)
178+
{
179+
struct vring_virtqueue *vq = to_vvq(_vq);
180+
return vq->queue_index;
181+
}
182+
EXPORT_SYMBOL_GPL(virtqueue_get_queue_index);
183+
174184
/**
175185
* virtqueue_add_buf - expose buffer to other end
176186
* @vq: the struct virtqueue we're talking about.
@@ -616,7 +626,8 @@ irqreturn_t vring_interrupt(int irq, void *_vq)
616626
}
617627
EXPORT_SYMBOL_GPL(vring_interrupt);
618628

619-
struct virtqueue *vring_new_virtqueue(unsigned int num,
629+
struct virtqueue *vring_new_virtqueue(unsigned int index,
630+
unsigned int num,
620631
unsigned int vring_align,
621632
struct virtio_device *vdev,
622633
bool weak_barriers,
@@ -647,6 +658,7 @@ struct virtqueue *vring_new_virtqueue(unsigned int num,
647658
vq->broken = false;
648659
vq->last_used_idx = 0;
649660
vq->num_added = 0;
661+
vq->queue_index = index;
650662
list_add_tail(&vq->vq.list, &vdev->vqs);
651663
#ifdef DEBUG
652664
vq->in_use = false;

‎include/linux/virtio.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void *virtqueue_detach_unused_buf(struct virtqueue *vq);
5050

5151
unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
5252

53+
int virtqueue_get_queue_index(struct virtqueue *vq);
54+
5355
/**
5456
* virtio_device - representation of a device using virtio
5557
* @index: unique position on the virtio bus

‎include/linux/virtio_ring.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ static inline int vring_need_event(__u16 event_idx, __u16 new_idx, __u16 old)
165165
struct virtio_device;
166166
struct virtqueue;
167167

168-
struct virtqueue *vring_new_virtqueue(unsigned int num,
168+
struct virtqueue *vring_new_virtqueue(unsigned int index,
169+
unsigned int num,
169170
unsigned int vring_align,
170171
struct virtio_device *vdev,
171172
bool weak_barriers,

0 commit comments

Comments
 (0)