aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael S. Tsirkin <mst@redhat.com>2018-04-19 01:30:48 -0400
committerDavid S. Miller <davem@davemloft.net>2018-04-19 16:33:20 -0400
commit12e571693837d6164bda61e316b1944972ee0d97 (patch)
tree0a5bede2fd2c9304c7a5c861b802466bcebff509
parentf4ea89110df237da6fbcaab76af431e85f07d904 (diff)
virtio_net: split out ctrl buffer
When sending control commands, virtio net sets up several buffers for DMA. The buffers are all part of the net device which means it's actually allocated by kvmalloc so it's in theory (on extreme memory pressure) possible to get a vmalloc'ed buffer which on some platforms means we can't DMA there. Fix up by moving the DMA buffers into a separate structure. Reported-by: Mikulas Patocka <mpatocka@redhat.com> Suggested-by: Eric Dumazet <eric.dumazet@gmail.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--drivers/net/virtio_net.c68
1 files changed, 39 insertions, 29 deletions
diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index dfe78308044d..9a675250107f 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -147,6 +147,17 @@ struct receive_queue {
147 struct xdp_rxq_info xdp_rxq; 147 struct xdp_rxq_info xdp_rxq;
148}; 148};
149 149
150/* Control VQ buffers: protected by the rtnl lock */
151struct control_buf {
152 struct virtio_net_ctrl_hdr hdr;
153 virtio_net_ctrl_ack status;
154 struct virtio_net_ctrl_mq mq;
155 u8 promisc;
156 u8 allmulti;
157 u16 vid;
158 u64 offloads;
159};
160
150struct virtnet_info { 161struct virtnet_info {
151 struct virtio_device *vdev; 162 struct virtio_device *vdev;
152 struct virtqueue *cvq; 163 struct virtqueue *cvq;
@@ -192,14 +203,7 @@ struct virtnet_info {
192 struct hlist_node node; 203 struct hlist_node node;
193 struct hlist_node node_dead; 204 struct hlist_node node_dead;
194 205
195 /* Control VQ buffers: protected by the rtnl lock */ 206 struct control_buf *ctrl;
196 struct virtio_net_ctrl_hdr ctrl_hdr;
197 virtio_net_ctrl_ack ctrl_status;
198 struct virtio_net_ctrl_mq ctrl_mq;
199 u8 ctrl_promisc;
200 u8 ctrl_allmulti;
201 u16 ctrl_vid;
202 u64 ctrl_offloads;
203 207
204 /* Ethtool settings */ 208 /* Ethtool settings */
205 u8 duplex; 209 u8 duplex;
@@ -1461,25 +1465,25 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1461 /* Caller should know better */ 1465 /* Caller should know better */
1462 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ)); 1466 BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
1463 1467
1464 vi->ctrl_status = ~0; 1468 vi->ctrl->status = ~0;
1465 vi->ctrl_hdr.class = class; 1469 vi->ctrl->hdr.class = class;
1466 vi->ctrl_hdr.cmd = cmd; 1470 vi->ctrl->hdr.cmd = cmd;
1467 /* Add header */ 1471 /* Add header */
1468 sg_init_one(&hdr, &vi->ctrl_hdr, sizeof(vi->ctrl_hdr)); 1472 sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1469 sgs[out_num++] = &hdr; 1473 sgs[out_num++] = &hdr;
1470 1474
1471 if (out) 1475 if (out)
1472 sgs[out_num++] = out; 1476 sgs[out_num++] = out;
1473 1477
1474 /* Add return status. */ 1478 /* Add return status. */
1475 sg_init_one(&stat, &vi->ctrl_status, sizeof(vi->ctrl_status)); 1479 sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1476 sgs[out_num] = &stat; 1480 sgs[out_num] = &stat;
1477 1481
1478 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); 1482 BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1479 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); 1483 virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1480 1484
1481 if (unlikely(!virtqueue_kick(vi->cvq))) 1485 if (unlikely(!virtqueue_kick(vi->cvq)))
1482 return vi->ctrl_status == VIRTIO_NET_OK; 1486 return vi->ctrl->status == VIRTIO_NET_OK;
1483 1487
1484 /* Spin for a response, the kick causes an ioport write, trapping 1488 /* Spin for a response, the kick causes an ioport write, trapping
1485 * into the hypervisor, so the request should be handled immediately. 1489 * into the hypervisor, so the request should be handled immediately.
@@ -1488,7 +1492,7 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1488 !virtqueue_is_broken(vi->cvq)) 1492 !virtqueue_is_broken(vi->cvq))
1489 cpu_relax(); 1493 cpu_relax();
1490 1494
1491 return vi->ctrl_status == VIRTIO_NET_OK; 1495 return vi->ctrl->status == VIRTIO_NET_OK;
1492} 1496}
1493 1497
1494static int virtnet_set_mac_address(struct net_device *dev, void *p) 1498static int virtnet_set_mac_address(struct net_device *dev, void *p)
@@ -1600,8 +1604,8 @@ static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
1600 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ)) 1604 if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
1601 return 0; 1605 return 0;
1602 1606
1603 vi->ctrl_mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs); 1607 vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
1604 sg_init_one(&sg, &vi->ctrl_mq, sizeof(vi->ctrl_mq)); 1608 sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
1605 1609
1606 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ, 1610 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
1607 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) { 1611 VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
@@ -1660,22 +1664,22 @@ static void virtnet_set_rx_mode(struct net_device *dev)
1660 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX)) 1664 if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
1661 return; 1665 return;
1662 1666
1663 vi->ctrl_promisc = ((dev->flags & IFF_PROMISC) != 0); 1667 vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
1664 vi->ctrl_allmulti = ((dev->flags & IFF_ALLMULTI) != 0); 1668 vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
1665 1669
1666 sg_init_one(sg, &vi->ctrl_promisc, sizeof(vi->ctrl_promisc)); 1670 sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
1667 1671
1668 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1672 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1669 VIRTIO_NET_CTRL_RX_PROMISC, sg)) 1673 VIRTIO_NET_CTRL_RX_PROMISC, sg))
1670 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n", 1674 dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
1671 vi->ctrl_promisc ? "en" : "dis"); 1675 vi->ctrl->promisc ? "en" : "dis");
1672 1676
1673 sg_init_one(sg, &vi->ctrl_allmulti, sizeof(vi->ctrl_allmulti)); 1677 sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
1674 1678
1675 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX, 1679 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
1676 VIRTIO_NET_CTRL_RX_ALLMULTI, sg)) 1680 VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
1677 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n", 1681 dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
1678 vi->ctrl_allmulti ? "en" : "dis"); 1682 vi->ctrl->allmulti ? "en" : "dis");
1679 1683
1680 uc_count = netdev_uc_count(dev); 1684 uc_count = netdev_uc_count(dev);
1681 mc_count = netdev_mc_count(dev); 1685 mc_count = netdev_mc_count(dev);
@@ -1721,8 +1725,8 @@ static int virtnet_vlan_rx_add_vid(struct net_device *dev,
1721 struct virtnet_info *vi = netdev_priv(dev); 1725 struct virtnet_info *vi = netdev_priv(dev);
1722 struct scatterlist sg; 1726 struct scatterlist sg;
1723 1727
1724 vi->ctrl_vid = vid; 1728 vi->ctrl->vid = vid;
1725 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 1729 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
1726 1730
1727 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1731 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1728 VIRTIO_NET_CTRL_VLAN_ADD, &sg)) 1732 VIRTIO_NET_CTRL_VLAN_ADD, &sg))
@@ -1736,8 +1740,8 @@ static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
1736 struct virtnet_info *vi = netdev_priv(dev); 1740 struct virtnet_info *vi = netdev_priv(dev);
1737 struct scatterlist sg; 1741 struct scatterlist sg;
1738 1742
1739 vi->ctrl_vid = vid; 1743 vi->ctrl->vid = vid;
1740 sg_init_one(&sg, &vi->ctrl_vid, sizeof(vi->ctrl_vid)); 1744 sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
1741 1745
1742 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN, 1746 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
1743 VIRTIO_NET_CTRL_VLAN_DEL, &sg)) 1747 VIRTIO_NET_CTRL_VLAN_DEL, &sg))
@@ -2133,9 +2137,9 @@ static int virtnet_restore_up(struct virtio_device *vdev)
2133static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads) 2137static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
2134{ 2138{
2135 struct scatterlist sg; 2139 struct scatterlist sg;
2136 vi->ctrl_offloads = cpu_to_virtio64(vi->vdev, offloads); 2140 vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
2137 2141
2138 sg_init_one(&sg, &vi->ctrl_offloads, sizeof(vi->ctrl_offloads)); 2142 sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
2139 2143
2140 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS, 2144 if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
2141 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) { 2145 VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
@@ -2358,6 +2362,7 @@ static void virtnet_free_queues(struct virtnet_info *vi)
2358 2362
2359 kfree(vi->rq); 2363 kfree(vi->rq);
2360 kfree(vi->sq); 2364 kfree(vi->sq);
2365 kfree(vi->ctrl);
2361} 2366}
2362 2367
2363static void _free_receive_bufs(struct virtnet_info *vi) 2368static void _free_receive_bufs(struct virtnet_info *vi)
@@ -2550,6 +2555,9 @@ static int virtnet_alloc_queues(struct virtnet_info *vi)
2550{ 2555{
2551 int i; 2556 int i;
2552 2557
2558 vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
2559 if (!vi->ctrl)
2560 goto err_ctrl;
2553 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL); 2561 vi->sq = kzalloc(sizeof(*vi->sq) * vi->max_queue_pairs, GFP_KERNEL);
2554 if (!vi->sq) 2562 if (!vi->sq)
2555 goto err_sq; 2563 goto err_sq;
@@ -2578,6 +2586,8 @@ static int virtnet_alloc_queues(struct virtnet_info *vi)
2578err_rq: 2586err_rq:
2579 kfree(vi->sq); 2587 kfree(vi->sq);
2580err_sq: 2588err_sq:
2589 kfree(vi->ctrl);
2590err_ctrl:
2581 return -ENOMEM; 2591 return -ENOMEM;
2582} 2592}
2583 2593