Skip to content

Commit b7dfde9

Browse files
committed
Merge tag 'virtio-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux
Pull virtio update from Rusty Russell: "Some nice cleanups, and even a patch my wife did as a "live" demo for Latinoware 2012. There's a slightly non-trivial merge in virtio-net, as we cleaned up the virtio add_buf interface while DaveM accepted the mq virtio-net patches." * tag 'virtio-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux: (27 commits) virtio_console: Add support for remoteproc serial virtio_console: Merge struct buffer_token into struct port_buffer virtio: add drv_to_virtio to make code clearly virtio: use dev_to_virtio wrapper in virtio virtio-mmio: Fix irq parsing in command line parameter virtio_console: Free buffers from out-queue upon close virtio: Convert dev_printk(KERN_<LEVEL> to dev_<level>( virtio_console: Use kmalloc instead of kzalloc virtio_console: Free buffer if splice fails virtio: tools: make it clear that virtqueue_add_buf() no longer returns > 0 virtio: scsi: make it clear that virtqueue_add_buf() no longer returns > 0 virtio: rpmsg: make it clear that virtqueue_add_buf() no longer returns > 0 virtio: net: make it clear that virtqueue_add_buf() no longer returns > 0 virtio: console: make it clear that virtqueue_add_buf() no longer returns > 0 virtio: make virtqueue_add_buf() returning 0 on success, not capacity. virtio: console: don't rely on virtqueue_add_buf() returning capacity. virtio_net: don't rely on virtqueue_add_buf() returning capacity. virtio-net: remove unused skb_vnet_hdr->num_sg field virtio-net: correct capacity math on ring full virtio: move queue_index and num_free fields into core struct virtqueue. ...
2 parents 03c850e + 1b63704 commit b7dfde9

File tree

17 files changed

+412
-276
lines changed

17 files changed

+412
-276
lines changed

drivers/char/virtio_console.c

Lines changed: 230 additions & 99 deletions
Large diffs are not rendered by default.

drivers/lguest/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ int run_guest(struct lg_cpu *cpu, unsigned long __user *user)
225225
* eventfd (ie. the appropriate virtqueue thread)?
226226
*/
227227
if (!send_notify_to_eventfd(cpu)) {
228-
/* OK, we tell the main Laucher. */
228+
/* OK, we tell the main Launcher. */
229229
if (put_user(cpu->pending_notify, user))
230230
return -EFAULT;
231231
return sizeof(cpu->pending_notify);

drivers/net/virtio_net.c

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ struct skb_vnet_hdr {
130130
struct virtio_net_hdr hdr;
131131
struct virtio_net_hdr_mrg_rxbuf mhdr;
132132
};
133-
unsigned int num_sg;
134133
};
135134

136135
struct padded_vnet_hdr {
@@ -530,10 +529,10 @@ static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
530529
err = add_recvbuf_small(rq, gfp);
531530

532531
oom = err == -ENOMEM;
533-
if (err < 0)
532+
if (err)
534533
break;
535534
++rq->num;
536-
} while (err > 0);
535+
} while (rq->vq->num_free);
537536
if (unlikely(rq->num > rq->max))
538537
rq->max = rq->num;
539538
virtqueue_kick(rq->vq);
@@ -640,10 +639,10 @@ static int virtnet_open(struct net_device *dev)
640639
return 0;
641640
}
642641

643-
static unsigned int free_old_xmit_skbs(struct send_queue *sq)
642+
static void free_old_xmit_skbs(struct send_queue *sq)
644643
{
645644
struct sk_buff *skb;
646-
unsigned int len, tot_sgs = 0;
645+
unsigned int len;
647646
struct virtnet_info *vi = sq->vq->vdev->priv;
648647
struct virtnet_stats *stats = this_cpu_ptr(vi->stats);
649648

@@ -655,17 +654,16 @@ static unsigned int free_old_xmit_skbs(struct send_queue *sq)
655654
stats->tx_packets++;
656655
u64_stats_update_end(&stats->tx_syncp);
657656

658-
tot_sgs += skb_vnet_hdr(skb)->num_sg;
659657
dev_kfree_skb_any(skb);
660658
}
661-
return tot_sgs;
662659
}
663660

664661
static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
665662
{
666663
struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
667664
const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
668665
struct virtnet_info *vi = sq->vq->vdev->priv;
666+
unsigned num_sg;
669667

670668
pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
671669

@@ -704,8 +702,8 @@ static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
704702
else
705703
sg_set_buf(sq->sg, &hdr->hdr, sizeof hdr->hdr);
706704

707-
hdr->num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
708-
return virtqueue_add_buf(sq->vq, sq->sg, hdr->num_sg,
705+
num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len) + 1;
706+
return virtqueue_add_buf(sq->vq, sq->sg, num_sg,
709707
0, skb, GFP_ATOMIC);
710708
}
711709

@@ -714,28 +712,20 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
714712
struct virtnet_info *vi = netdev_priv(dev);
715713
int qnum = skb_get_queue_mapping(skb);
716714
struct send_queue *sq = &vi->sq[qnum];
717-
int capacity;
715+
int err;
718716

719717
/* Free up any pending old buffers before queueing new ones. */
720718
free_old_xmit_skbs(sq);
721719

722720
/* Try to transmit */
723-
capacity = xmit_skb(sq, skb);
724-
725-
/* This can happen with OOM and indirect buffers. */
726-
if (unlikely(capacity < 0)) {
727-
if (likely(capacity == -ENOMEM)) {
728-
if (net_ratelimit())
729-
dev_warn(&dev->dev,
730-
"TXQ (%d) failure: out of memory\n",
731-
qnum);
732-
} else {
733-
dev->stats.tx_fifo_errors++;
734-
if (net_ratelimit())
735-
dev_warn(&dev->dev,
736-
"Unexpected TXQ (%d) failure: %d\n",
737-
qnum, capacity);
738-
}
721+
err = xmit_skb(sq, skb);
722+
723+
/* This should not happen! */
724+
if (unlikely(err)) {
725+
dev->stats.tx_fifo_errors++;
726+
if (net_ratelimit())
727+
dev_warn(&dev->dev,
728+
"Unexpected TXQ (%d) queue failure: %d\n", qnum, err);
739729
dev->stats.tx_dropped++;
740730
kfree_skb(skb);
741731
return NETDEV_TX_OK;
@@ -748,12 +738,12 @@ static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
748738

749739
/* Apparently nice girls don't return TX_BUSY; stop the queue
750740
* before it gets out of hand. Naturally, this wastes entries. */
751-
if (capacity < 2+MAX_SKB_FRAGS) {
741+
if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
752742
netif_stop_subqueue(dev, qnum);
753743
if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
754744
/* More just got used, free them then recheck. */
755-
capacity += free_old_xmit_skbs(sq);
756-
if (capacity >= 2+MAX_SKB_FRAGS) {
745+
free_old_xmit_skbs(sq);
746+
if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
757747
netif_start_subqueue(dev, qnum);
758748
virtqueue_disable_cb(sq->vq);
759749
}

drivers/rpmsg/virtio_rpmsg_bus.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
764764

765765
/* add message to the remote processor's virtqueue */
766766
err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
767-
if (err < 0) {
767+
if (err) {
768768
/*
769769
* need to reclaim the buffer here, otherwise it's lost
770770
* (memory won't leak, but rpmsg won't use it again for TX).
@@ -776,8 +776,6 @@ int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
776776

777777
/* tell the remote processor it has a pending message to read */
778778
virtqueue_kick(vrp->svq);
779-
780-
err = 0;
781779
out:
782780
mutex_unlock(&vrp->tx_lock);
783781
return err;
@@ -980,7 +978,7 @@ static int rpmsg_probe(struct virtio_device *vdev)
980978

981979
err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr,
982980
GFP_KERNEL);
983-
WARN_ON(err < 0); /* sanity check; this can't really happen */
981+
WARN_ON(err); /* sanity check; this can't really happen */
984982
}
985983

986984
/* suppress "tx-complete" interrupts */

drivers/scsi/virtio_scsi.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,22 @@ static void virtscsi_ctrl_done(struct virtqueue *vq)
215215
static int virtscsi_kick_event(struct virtio_scsi *vscsi,
216216
struct virtio_scsi_event_node *event_node)
217217
{
218-
int ret;
218+
int err;
219219
struct scatterlist sg;
220220
unsigned long flags;
221221

222222
sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
223223

224224
spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
225225

226-
ret = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node, GFP_ATOMIC);
227-
if (ret >= 0)
226+
err = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node,
227+
GFP_ATOMIC);
228+
if (!err)
228229
virtqueue_kick(vscsi->event_vq.vq);
229230

230231
spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
231232

232-
return ret;
233+
return err;
233234
}
234235

235236
static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
@@ -410,22 +411,23 @@ static int virtscsi_kick_cmd(struct virtio_scsi_target_state *tgt,
410411
{
411412
unsigned int out_num, in_num;
412413
unsigned long flags;
413-
int ret;
414+
int err;
415+
bool needs_kick = false;
414416

415417
spin_lock_irqsave(&tgt->tgt_lock, flags);
416418
virtscsi_map_cmd(tgt, cmd, &out_num, &in_num, req_size, resp_size);
417419

418420
spin_lock(&vq->vq_lock);
419-
ret = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp);
421+
err = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp);
420422
spin_unlock(&tgt->tgt_lock);
421-
if (ret >= 0)
422-
ret = virtqueue_kick_prepare(vq->vq);
423+
if (!err)
424+
needs_kick = virtqueue_kick_prepare(vq->vq);
423425

424426
spin_unlock_irqrestore(&vq->vq_lock, flags);
425427

426-
if (ret > 0)
428+
if (needs_kick)
427429
virtqueue_notify(vq->vq);
428-
return ret;
430+
return err;
429431
}
430432

431433
static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
@@ -467,7 +469,7 @@ static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
467469

468470
if (virtscsi_kick_cmd(tgt, &vscsi->req_vq, cmd,
469471
sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
470-
GFP_ATOMIC) >= 0)
472+
GFP_ATOMIC) == 0)
471473
ret = 0;
472474
else
473475
mempool_free(cmd, virtscsi_cmd_pool);

drivers/virtio/virtio.c

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,32 @@ static DEFINE_IDA(virtio_index_ida);
1010
static ssize_t device_show(struct device *_d,
1111
struct device_attribute *attr, char *buf)
1212
{
13-
struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
13+
struct virtio_device *dev = dev_to_virtio(_d);
1414
return sprintf(buf, "0x%04x\n", dev->id.device);
1515
}
1616
static ssize_t vendor_show(struct device *_d,
1717
struct device_attribute *attr, char *buf)
1818
{
19-
struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
19+
struct virtio_device *dev = dev_to_virtio(_d);
2020
return sprintf(buf, "0x%04x\n", dev->id.vendor);
2121
}
2222
static ssize_t status_show(struct device *_d,
2323
struct device_attribute *attr, char *buf)
2424
{
25-
struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
25+
struct virtio_device *dev = dev_to_virtio(_d);
2626
return sprintf(buf, "0x%08x\n", dev->config->get_status(dev));
2727
}
2828
static ssize_t modalias_show(struct device *_d,
2929
struct device_attribute *attr, char *buf)
3030
{
31-
struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
32-
31+
struct virtio_device *dev = dev_to_virtio(_d);
3332
return sprintf(buf, "virtio:d%08Xv%08X\n",
3433
dev->id.device, dev->id.vendor);
3534
}
3635
static ssize_t features_show(struct device *_d,
3736
struct device_attribute *attr, char *buf)
3837
{
39-
struct virtio_device *dev = container_of(_d, struct virtio_device, dev);
38+
struct virtio_device *dev = dev_to_virtio(_d);
4039
unsigned int i;
4140
ssize_t len = 0;
4241

@@ -71,10 +70,10 @@ static inline int virtio_id_match(const struct virtio_device *dev,
7170
static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
7271
{
7372
unsigned int i;
74-
struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
73+
struct virtio_device *dev = dev_to_virtio(_dv);
7574
const struct virtio_device_id *ids;
7675

77-
ids = container_of(_dr, struct virtio_driver, driver)->id_table;
76+
ids = drv_to_virtio(_dr)->id_table;
7877
for (i = 0; ids[i].device; i++)
7978
if (virtio_id_match(dev, &ids[i]))
8079
return 1;
@@ -83,7 +82,7 @@ static int virtio_dev_match(struct device *_dv, struct device_driver *_dr)
8382

8483
static int virtio_uevent(struct device *_dv, struct kobj_uevent_env *env)
8584
{
86-
struct virtio_device *dev = container_of(_dv,struct virtio_device,dev);
85+
struct virtio_device *dev = dev_to_virtio(_dv);
8786

8887
return add_uevent_var(env, "MODALIAS=virtio:d%08Xv%08X",
8988
dev->id.device, dev->id.vendor);
@@ -98,8 +97,7 @@ void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
9897
unsigned int fbit)
9998
{
10099
unsigned int i;
101-
struct virtio_driver *drv = container_of(vdev->dev.driver,
102-
struct virtio_driver, driver);
100+
struct virtio_driver *drv = drv_to_virtio(vdev->dev.driver);
103101

104102
for (i = 0; i < drv->feature_table_size; i++)
105103
if (drv->feature_table[i] == fbit)
@@ -111,9 +109,8 @@ EXPORT_SYMBOL_GPL(virtio_check_driver_offered_feature);
111109
static int virtio_dev_probe(struct device *_d)
112110
{
113111
int err, i;
114-
struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
115-
struct virtio_driver *drv = container_of(dev->dev.driver,
116-
struct virtio_driver, driver);
112+
struct virtio_device *dev = dev_to_virtio(_d);
113+
struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
117114
u32 device_features;
118115

119116
/* We have a driver! */
@@ -152,9 +149,8 @@ static int virtio_dev_probe(struct device *_d)
152149

153150
static int virtio_dev_remove(struct device *_d)
154151
{
155-
struct virtio_device *dev = container_of(_d,struct virtio_device,dev);
156-
struct virtio_driver *drv = container_of(dev->dev.driver,
157-
struct virtio_driver, driver);
152+
struct virtio_device *dev = dev_to_virtio(_d);
153+
struct virtio_driver *drv = drv_to_virtio(dev->dev.driver);
158154

159155
drv->remove(dev);
160156

drivers/virtio/virtio_balloon.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,9 @@ static void fill_balloon(struct virtio_balloon *vb, size_t num)
139139
struct page *page = balloon_page_enqueue(vb_dev_info);
140140

141141
if (!page) {
142-
if (printk_ratelimit())
143-
dev_printk(KERN_INFO, &vb->vdev->dev,
144-
"Out of puff! Can't get %u pages\n",
145-
VIRTIO_BALLOON_PAGES_PER_PAGE);
142+
dev_info_ratelimited(&vb->vdev->dev,
143+
"Out of puff! Can't get %u pages\n",
144+
VIRTIO_BALLOON_PAGES_PER_PAGE);
146145
/* Sleep for at least 1/5 of a second before retry. */
147146
msleep(200);
148147
break;

drivers/virtio/virtio_mmio.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static void vm_notify(struct virtqueue *vq)
225225

226226
/* We write the queue's selector into the notification register to
227227
* signal the other end */
228-
writel(virtqueue_get_queue_index(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
228+
writel(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY);
229229
}
230230

231231
/* Notify all virtqueues on an interrupt. */
@@ -266,7 +266,7 @@ static void vm_del_vq(struct virtqueue *vq)
266266
struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vq->vdev);
267267
struct virtio_mmio_vq_info *info = vq->priv;
268268
unsigned long flags, size;
269-
unsigned int index = virtqueue_get_queue_index(vq);
269+
unsigned int index = vq->index;
270270

271271
spin_lock_irqsave(&vm_dev->lock, flags);
272272
list_del(&info->node);
@@ -521,25 +521,33 @@ static int vm_cmdline_set(const char *device,
521521
int err;
522522
struct resource resources[2] = {};
523523
char *str;
524-
long long int base;
524+
long long int base, size;
525+
unsigned int irq;
525526
int processed, consumed = 0;
526527
struct platform_device *pdev;
527528

528-
resources[0].flags = IORESOURCE_MEM;
529-
resources[1].flags = IORESOURCE_IRQ;
530-
531-
resources[0].end = memparse(device, &str) - 1;
529+
/* Consume "size" part of the command line parameter */
530+
size = memparse(device, &str);
532531

532+
/* Get "@<base>:<irq>[:<id>]" chunks */
533533
processed = sscanf(str, "@%lli:%u%n:%d%n",
534-
&base, &resources[1].start, &consumed,
534+
&base, &irq, &consumed,
535535
&vm_cmdline_id, &consumed);
536536

537-
if (processed < 2 || processed > 3 || str[consumed])
537+
/*
538+
* sscanf() must processes at least 2 chunks; also there
539+
* must be no extra characters after the last chunk, so
540+
* str[consumed] must be '\0'
541+
*/
542+
if (processed < 2 || str[consumed])
538543
return -EINVAL;
539544

545+
resources[0].flags = IORESOURCE_MEM;
540546
resources[0].start = base;
541-
resources[0].end += base;
542-
resources[1].end = resources[1].start;
547+
resources[0].end = base + size - 1;
548+
549+
resources[1].flags = IORESOURCE_IRQ;
550+
resources[1].start = resources[1].end = irq;
543551

544552
if (!vm_cmdline_parent_registered) {
545553
err = device_register(&vm_cmdline_parent);

0 commit comments

Comments
 (0)