Skip to content

Commit 5c34d00

Browse files
Christoph Hellwigmstsirkin
authored andcommitted
virtio_pci: remove struct virtio_pci_vq_info
We don't really need struct virtio_pci_vq_info, as most field in there are redundant: - the vq backpointer is not strictly neede to start with - the entry in the vqs list is not needed - the generic virtqueue already has list, we only need to check if it has a callback to get the same semantics - we can use a simple array to look up the MSI-X vec if needed. - That simple array now also duoble serves to replace the per_vq_vectors flag Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent e3b56cd commit 5c34d00

File tree

4 files changed

+39
-115
lines changed

4 files changed

+39
-115
lines changed

drivers/virtio/virtio_pci_common.c

Lines changed: 34 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,13 @@ static irqreturn_t vp_config_changed(int irq, void *opaque)
6262
static irqreturn_t vp_vring_interrupt(int irq, void *opaque)
6363
{
6464
struct virtio_pci_device *vp_dev = opaque;
65-
struct virtio_pci_vq_info *info;
6665
irqreturn_t ret = IRQ_NONE;
67-
unsigned long flags;
66+
struct virtqueue *vq;
6867

69-
spin_lock_irqsave(&vp_dev->lock, flags);
70-
list_for_each_entry(info, &vp_dev->virtqueues, node) {
71-
if (vring_interrupt(irq, info->vq) == IRQ_HANDLED)
68+
list_for_each_entry(vq, &vp_dev->vdev.vqs, list) {
69+
if (vq->callback && vring_interrupt(irq, vq) == IRQ_HANDLED)
7270
ret = IRQ_HANDLED;
7371
}
74-
spin_unlock_irqrestore(&vp_dev->lock, flags);
7572

7673
return ret;
7774
}
@@ -167,55 +164,6 @@ static int vp_request_msix_vectors(struct virtio_device *vdev, int nvectors,
167164
return err;
168165
}
169166

170-
static struct virtqueue *vp_setup_vq(struct virtio_device *vdev, unsigned index,
171-
void (*callback)(struct virtqueue *vq),
172-
const char *name,
173-
u16 msix_vec)
174-
{
175-
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
176-
struct virtio_pci_vq_info *info = kmalloc(sizeof *info, GFP_KERNEL);
177-
struct virtqueue *vq;
178-
unsigned long flags;
179-
180-
/* fill out our structure that represents an active queue */
181-
if (!info)
182-
return ERR_PTR(-ENOMEM);
183-
184-
vq = vp_dev->setup_vq(vp_dev, info, index, callback, name, msix_vec);
185-
if (IS_ERR(vq))
186-
goto out_info;
187-
188-
info->vq = vq;
189-
if (callback) {
190-
spin_lock_irqsave(&vp_dev->lock, flags);
191-
list_add(&info->node, &vp_dev->virtqueues);
192-
spin_unlock_irqrestore(&vp_dev->lock, flags);
193-
} else {
194-
INIT_LIST_HEAD(&info->node);
195-
}
196-
197-
vp_dev->vqs[index] = info;
198-
return vq;
199-
200-
out_info:
201-
kfree(info);
202-
return vq;
203-
}
204-
205-
static void vp_del_vq(struct virtqueue *vq)
206-
{
207-
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
208-
struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
209-
unsigned long flags;
210-
211-
spin_lock_irqsave(&vp_dev->lock, flags);
212-
list_del(&info->node);
213-
spin_unlock_irqrestore(&vp_dev->lock, flags);
214-
215-
vp_dev->del_vq(info);
216-
kfree(info);
217-
}
218-
219167
/* the config->del_vqs() implementation */
220168
void vp_del_vqs(struct virtio_device *vdev)
221169
{
@@ -224,16 +172,15 @@ void vp_del_vqs(struct virtio_device *vdev)
224172
int i;
225173

226174
list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
227-
if (vp_dev->per_vq_vectors) {
228-
int v = vp_dev->vqs[vq->index]->msix_vector;
175+
if (vp_dev->msix_vector_map) {
176+
int v = vp_dev->msix_vector_map[vq->index];
229177

230178
if (v != VIRTIO_MSI_NO_VECTOR)
231179
free_irq(pci_irq_vector(vp_dev->pci_dev, v),
232180
vq);
233181
}
234-
vp_del_vq(vq);
182+
vp_dev->del_vq(vq);
235183
}
236-
vp_dev->per_vq_vectors = false;
237184

238185
if (vp_dev->intx_enabled) {
239186
free_irq(vp_dev->pci_dev->irq, vp_dev);
@@ -261,8 +208,8 @@ void vp_del_vqs(struct virtio_device *vdev)
261208
vp_dev->msix_names = NULL;
262209
kfree(vp_dev->msix_affinity_masks);
263210
vp_dev->msix_affinity_masks = NULL;
264-
kfree(vp_dev->vqs);
265-
vp_dev->vqs = NULL;
211+
kfree(vp_dev->msix_vector_map);
212+
vp_dev->msix_vector_map = NULL;
266213
}
267214

268215
static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
@@ -275,10 +222,6 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
275222
u16 msix_vec;
276223
int i, err, nvectors, allocated_vectors;
277224

278-
vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
279-
if (!vp_dev->vqs)
280-
return -ENOMEM;
281-
282225
if (per_vq_vectors) {
283226
/* Best option: one for change interrupt, one per vq. */
284227
nvectors = 1;
@@ -294,7 +237,13 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
294237
if (err)
295238
goto error_find;
296239

297-
vp_dev->per_vq_vectors = per_vq_vectors;
240+
if (per_vq_vectors) {
241+
vp_dev->msix_vector_map = kmalloc_array(nvqs,
242+
sizeof(*vp_dev->msix_vector_map), GFP_KERNEL);
243+
if (!vp_dev->msix_vector_map)
244+
goto error_find;
245+
}
246+
298247
allocated_vectors = vp_dev->msix_used_vectors;
299248
for (i = 0; i < nvqs; ++i) {
300249
if (!names[i]) {
@@ -304,19 +253,25 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
304253

305254
if (!callbacks[i])
306255
msix_vec = VIRTIO_MSI_NO_VECTOR;
307-
else if (vp_dev->per_vq_vectors)
256+
else if (per_vq_vectors)
308257
msix_vec = allocated_vectors++;
309258
else
310259
msix_vec = VP_MSIX_VQ_VECTOR;
311-
vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i], msix_vec);
260+
vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
261+
msix_vec);
312262
if (IS_ERR(vqs[i])) {
313263
err = PTR_ERR(vqs[i]);
314264
goto error_find;
315265
}
316266

317-
if (!vp_dev->per_vq_vectors || msix_vec == VIRTIO_MSI_NO_VECTOR)
267+
if (!per_vq_vectors)
318268
continue;
319269

270+
if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
271+
vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
272+
continue;
273+
}
274+
320275
/* allocate per-vq irq if available and necessary */
321276
snprintf(vp_dev->msix_names[msix_vec],
322277
sizeof *vp_dev->msix_names,
@@ -326,8 +281,12 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned nvqs,
326281
vring_interrupt, 0,
327282
vp_dev->msix_names[msix_vec],
328283
vqs[i]);
329-
if (err)
284+
if (err) {
285+
/* don't free this irq on error */
286+
vp_dev->msix_vector_map[i] = VIRTIO_MSI_NO_VECTOR;
330287
goto error_find;
288+
}
289+
vp_dev->msix_vector_map[i] = msix_vec;
331290
}
332291
return 0;
333292

@@ -343,23 +302,18 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned nvqs,
343302
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
344303
int i, err;
345304

346-
vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL);
347-
if (!vp_dev->vqs)
348-
return -ENOMEM;
349-
350305
err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED,
351306
dev_name(&vdev->dev), vp_dev);
352307
if (err)
353308
goto out_del_vqs;
354309

355310
vp_dev->intx_enabled = 1;
356-
vp_dev->per_vq_vectors = false;
357311
for (i = 0; i < nvqs; ++i) {
358312
if (!names[i]) {
359313
vqs[i] = NULL;
360314
continue;
361315
}
362-
vqs[i] = vp_setup_vq(vdev, i, callbacks[i], names[i],
316+
vqs[i] = vp_dev->setup_vq(vp_dev, i, callbacks[i], names[i],
363317
VIRTIO_MSI_NO_VECTOR);
364318
if (IS_ERR(vqs[i])) {
365319
err = PTR_ERR(vqs[i]);
@@ -409,16 +363,15 @@ int vp_set_vq_affinity(struct virtqueue *vq, int cpu)
409363
{
410364
struct virtio_device *vdev = vq->vdev;
411365
struct virtio_pci_device *vp_dev = to_vp_device(vdev);
412-
struct virtio_pci_vq_info *info = vp_dev->vqs[vq->index];
413-
struct cpumask *mask;
414-
unsigned int irq;
415366

416367
if (!vq->callback)
417368
return -EINVAL;
418369

419370
if (vp_dev->msix_enabled) {
420-
mask = vp_dev->msix_affinity_masks[info->msix_vector];
421-
irq = pci_irq_vector(vp_dev->pci_dev, info->msix_vector);
371+
int vec = vp_dev->msix_vector_map[vq->index];
372+
struct cpumask *mask = vp_dev->msix_affinity_masks[vec];
373+
unsigned int irq = pci_irq_vector(vp_dev->pci_dev, vec);
374+
422375
if (cpu == -1)
423376
irq_set_affinity_hint(irq, NULL);
424377
else {
@@ -498,8 +451,6 @@ static int virtio_pci_probe(struct pci_dev *pci_dev,
498451
vp_dev->vdev.dev.parent = &pci_dev->dev;
499452
vp_dev->vdev.dev.release = virtio_pci_release_dev;
500453
vp_dev->pci_dev = pci_dev;
501-
INIT_LIST_HEAD(&vp_dev->virtqueues);
502-
spin_lock_init(&vp_dev->lock);
503454

504455
/* enable the device */
505456
rc = pci_enable_device(pci_dev);

drivers/virtio/virtio_pci_common.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,6 @@
3131
#include <linux/highmem.h>
3232
#include <linux/spinlock.h>
3333

34-
struct virtio_pci_vq_info {
35-
/* the actual virtqueue */
36-
struct virtqueue *vq;
37-
38-
/* the list node for the virtqueues list */
39-
struct list_head node;
40-
41-
/* MSI-X vector (or none) */
42-
unsigned msix_vector;
43-
};
44-
4534
/* Our device structure */
4635
struct virtio_pci_device {
4736
struct virtio_device vdev;
@@ -75,13 +64,6 @@ struct virtio_pci_device {
7564
/* the IO mapping for the PCI config space */
7665
void __iomem *ioaddr;
7766

78-
/* a list of queues so we can dispatch IRQs */
79-
spinlock_t lock;
80-
struct list_head virtqueues;
81-
82-
/* array of all queues for house-keeping */
83-
struct virtio_pci_vq_info **vqs;
84-
8567
/* MSI-X support */
8668
int msix_enabled;
8769
int intx_enabled;
@@ -94,16 +76,15 @@ struct virtio_pci_device {
9476
/* Vectors allocated, excluding per-vq vectors if any */
9577
unsigned msix_used_vectors;
9678

97-
/* Whether we have vector per vq */
98-
bool per_vq_vectors;
79+
/* Map of per-VQ MSI-X vectors, may be NULL */
80+
unsigned *msix_vector_map;
9981

10082
struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev,
101-
struct virtio_pci_vq_info *info,
10283
unsigned idx,
10384
void (*callback)(struct virtqueue *vq),
10485
const char *name,
10586
u16 msix_vec);
106-
void (*del_vq)(struct virtio_pci_vq_info *info);
87+
void (*del_vq)(struct virtqueue *vq);
10788

10889
u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector);
10990
};

drivers/virtio/virtio_pci_legacy.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
112112
}
113113

114114
static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
115-
struct virtio_pci_vq_info *info,
116115
unsigned index,
117116
void (*callback)(struct virtqueue *vq),
118117
const char *name,
@@ -130,8 +129,6 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
130129
if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN))
131130
return ERR_PTR(-ENOENT);
132131

133-
info->msix_vector = msix_vec;
134-
135132
/* create the vring */
136133
vq = vring_create_virtqueue(index, num,
137134
VIRTIO_PCI_VRING_ALIGN, &vp_dev->vdev,
@@ -162,9 +159,8 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
162159
return ERR_PTR(err);
163160
}
164161

165-
static void del_vq(struct virtio_pci_vq_info *info)
162+
static void del_vq(struct virtqueue *vq)
166163
{
167-
struct virtqueue *vq = info->vq;
168164
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
169165

170166
iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL);

drivers/virtio/virtio_pci_modern.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
293293
}
294294

295295
static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
296-
struct virtio_pci_vq_info *info,
297296
unsigned index,
298297
void (*callback)(struct virtqueue *vq),
299298
const char *name,
@@ -323,8 +322,6 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
323322
/* get offset of notification word for this vq */
324323
off = vp_ioread16(&cfg->queue_notify_off);
325324

326-
info->msix_vector = msix_vec;
327-
328325
/* create the vring */
329326
vq = vring_create_virtqueue(index, num,
330327
SMP_CACHE_BYTES, &vp_dev->vdev,
@@ -409,9 +406,8 @@ static int vp_modern_find_vqs(struct virtio_device *vdev, unsigned nvqs,
409406
return 0;
410407
}
411408

412-
static void del_vq(struct virtio_pci_vq_info *info)
409+
static void del_vq(struct virtqueue *vq)
413410
{
414-
struct virtqueue *vq = info->vq;
415411
struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
416412

417413
vp_iowrite16(vq->index, &vp_dev->common->queue_select);

0 commit comments

Comments
 (0)