Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] virtio_net: virtnet_rx_watchdog() races with
  virtqueue_resize() and reset
- [Low] virtio_net: unsynchronized watchdog init in virtnet_set_queues()
--

commit 093ca3617532e988a9fe20901b18a2e6a86aca17
Author: Longjun Tang <[email protected]>

virtio_net: introduce an RX queue watchdog for stuck detection

This commit adds a per-device 1 Hz watchdog timer that checks active RX
queues for 5-second consumption stalls and reschedules NAPI when a queue
is stuck with a non-zero backlog.

> diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
> index 1cd64f9e12e74..7f21fe4f96a2b 100644
> --- a/drivers/net/virtio_net.c
> +++ b/drivers/net/virtio_net.c

[ ... ]

> @@ -3090,6 +3106,74 @@ static int virtnet_poll(struct napi_struct *napi, int 
> budget)
>       return received;
>  }
>  
> +/*
> + * An RX queue is stalled if it has a backlog but makes no progress and
> + * gets no new interrupt for VIRTNET_RX_WATCHDOG_TIMEO seconds, usually
> + * because an interrupt was lost.  Mark it stalled and reschedule NAPI to
> + * drain the ring and refill the buffers.
> + */
> +static void virtnet_rx_watchdog(struct timer_list *t)
> +{
> +     struct virtnet_info *vi = timer_container_of(vi, t, rx_watchdog);
> +     unsigned long timeout = VIRTNET_RX_WATCHDOG_TIMEO * HZ;
> +     int i;
> +
> +     for (i = 0; i < vi->curr_queue_pairs; i++) {
> +             struct receive_queue *rq = &vi->rq[i];
> +             u16 last_used = virtqueue_get_last_used_idx(rq->vq);
> +             u16 calls = rq->calls;
> +             bool backlog = virtqueue_poll(rq->vq, last_used);

[Severity: Medium]
Can virtnet_rx_watchdog() race with virtqueue_resize() or
virtqueue_reset() and read freed vring memory?

When an RX ring is resized via virtnet_rx_resize() (or reset for AF_XDP in
virtnet_rq_bind_xsk_pool()) while the interface is up, virtnet_rx_pause()
disables NAPI and cancels DIM, but vi->rx_watchdog keeps firing:

drivers/net/virtio_net.c:virtnet_rx_pause() {
        bool running = netif_running(vi->dev);

        if (running) {
                virtnet_napi_disable(rq);
                virtnet_cancel_dim(vi, &rq->dim);
        }
}

Inside virtqueue_resize_split() and virtqueue_resize_packed(),
vring_free() frees the old vring before virtqueue_vring_attach_split() or
virtqueue_vring_attach_packed() installs the new ring pointers:

CPU0                                  CPU1 (<softirq>)
virtnet_rx_resize()
  virtnet_rx_pause(vi, rq)
  virtqueue_resize(rq->vq, ...)
    vring_free(&vq->vq)
                                      virtnet_rx_watchdog()
                                        last_used =
                                          virtqueue_get_last_used_idx(...)
                                        virtqueue_poll(rq->vq, last_used)
                                          <-- use-after-free
    virtqueue_init(vq, ...)
    virtqueue_vring_attach_split(...)

On packed virtqueues, if virtnet_rx_resize() shrinks the ring and
virtnet_rx_watchdog() reads last_used from the old ring right before
virtqueue_init() resets vq->last_used_idx and attaches the smaller
descriptor array, virtqueue_poll_packed() also indexes
vq->packed.vring.desc[used_idx] out of bounds.

In addition, both virtqueue_resize() and virtqueue_reset() reset
vq->last_used_idx in virtqueue_init(), but virtnet_rx_resume() does not
call virtnet_rx_watchdog_init_queue(rq) to resynchronize rq->watchdog.

[ ... ]

> @@ -3839,7 +3926,14 @@ static int virtnet_set_queues(struct virtnet_info *vi, 
> u16 queue_pairs)
>       if (vi->has_rss)
>               vi->rss_trailer.max_tx_vq = cpu_to_le16(queue_pairs);
>  succ:
> +     if (dev->flags & IFF_UP) {
> +             /* Init new queues */
> +             for (int i = old_queue_pairs; i < queue_pairs; i++)
> +                     virtnet_rx_watchdog_init_queue(&vi->rq[i]);
> +     }
> +
>       vi->curr_queue_pairs = queue_pairs;

[Severity: Low]
Can virtnet_rx_watchdog() observe the updated vi->curr_queue_pairs before
the rq->watchdog fields initialized by virtnet_rx_watchdog_init_queue()?

virtnet_set_queues() runs while vi->rx_watchdog is active and uses plain
stores to initialize rq->watchdog and update vi->curr_queue_pairs.  By the
time execution reaches the succ label, the CVQ command has already allowed
the device to steer RX packets to the new queues:

CPU0                                  CPU1 (<softirq>)
virtnet_set_queues()
  virtnet_rx_watchdog_init_queue(rq)
    rq->watchdog.last_used = ...
    rq->watchdog.calls = ...
  vi->curr_queue_pairs = queue_pairs
                                      virtnet_rx_watchdog()
                                        i < vi->curr_queue_pairs
                                        time_after(jiffies,
                                          rq->watchdog.jiffies + timeout)
                                          <-- stale jiffies, false stall
    rq->watchdog.jiffies = jiffies

If a newly activated queue already has a buffer in its used ring and
virtnet_rx_watchdog() reads a stale rq->watchdog.jiffies (0 from initial
allocation or an old timestamp from before the queue count was lowered),
it logs a false RX stall warning and marks rq->watchdog.stalled.  If
vi->curr_queue_pairs is lowered and raised again while
virtnet_rx_watchdog() is mid-loop, both functions can also read and write
rq->watchdog concurrently.

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=2

Reply via email to