[PATCH] hw/net/virtio-net: skip automatic zero-init of TX iovec arrays

Tan Chi posted 1 patch 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261101151038.164841-1-tanchi25@mails.ucas.ac.cn
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Jason Wang <jasowangio@gmail.com>
There is a newer version of this series
hw/net/virtio-net.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] hw/net/virtio-net: skip automatic zero-init of TX iovec arrays
Posted by Tan Chi 3 weeks ago
virtio_net_flush_tx() has two large iovec arrays that are populated
as needed. Entries are initialized explicitly or through iov_copy(),
and out_num bounds the entries subsequently consumed through out_sg.
Zero-initializing the entire arrays is therefore unnecessary.

Similar arrays in virtio_net_receive_rcu() already use
QEMU_UNINITIALIZED to avoid automatic zero-initialization overhead.
Apply the same optimization to the TX path.

Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
 hw/net/virtio-net.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
index 54e42cfec9..76e3281831 100644
--- a/hw/net/virtio-net.c
+++ b/hw/net/virtio-net.c
@@ -2783,7 +2783,9 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
     for (;;) {
         ssize_t ret;
         unsigned int out_num;
-        struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
+        QEMU_UNINITIALIZED struct iovec sg[VIRTQUEUE_MAX_SIZE];
+        QEMU_UNINITIALIZED struct iovec sg2[VIRTQUEUE_MAX_SIZE + 1];
+        struct iovec *out_sg;
         struct virtio_net_hdr vhdr;
 
         elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
-- 
2.53.0
Re: [PATCH] hw/net/virtio-net: skip automatic zero-init of TX iovec arrays
Posted by Michael S. Tsirkin 3 weeks ago
On Sun, Nov 01, 2026 at 11:10:38PM +0800, Tan Chi wrote:
> virtio_net_flush_tx() has two large iovec arrays that are populated
> as needed. Entries are initialized explicitly or through iov_copy(),
> and out_num bounds the entries subsequently consumed through out_sg.
> Zero-initializing the entire arrays is therefore unnecessary.
> 
> Similar arrays in virtio_net_receive_rcu() already use
> QEMU_UNINITIALIZED to avoid automatic zero-initialization overhead.
> Apply the same optimization to the TX path.
> 
> Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>

Any optimizations should come with perf testing numbers showing the
performance gains.

> ---
>  hw/net/virtio-net.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c
> index 54e42cfec9..76e3281831 100644
> --- a/hw/net/virtio-net.c
> +++ b/hw/net/virtio-net.c
> @@ -2783,7 +2783,9 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
>      for (;;) {
>          ssize_t ret;
>          unsigned int out_num;
> -        struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
> +        QEMU_UNINITIALIZED struct iovec sg[VIRTQUEUE_MAX_SIZE];
> +        QEMU_UNINITIALIZED struct iovec sg2[VIRTQUEUE_MAX_SIZE + 1];
> +        struct iovec *out_sg;
>          struct virtio_net_hdr vhdr;
>  
>          elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
> -- 
> 2.53.0