Linux Kernel Selftest development
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: apasichnik9@gmail.com
Cc: Jason@zx2c4.com, wireguard@lists.zx2c4.com,
	netdev@vger.kernel.org, andrew+netdev@lunn.ch,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, shuah@kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net v2 1/2] wireguard: peer: free packets left on the per-peer queues on removal
Date: Fri, 27 Nov 2026 09:48:07 +0000	[thread overview]
Message-ID: <179577288773.3135.10772290029017294104@kernel.org> (raw)
In-Reply-To: <20261126094722.73025-2-apasichnik9@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The new tx_queue purge in peer_remove_after_dead()
  (drivers/net/wireguard/peer.c:148) can run while…
- [Low] In peer_remove_after_dead() (drivers/net/wireguard/peer.c), the
  patch adds a purge of the per-peer queues and a comment saying entries…

--

wireguard: peer: free packets left on the per-peer queues on removal

This patch adds peer_purge_queue(). peer_remove_after_dead() calls it
after netif_napi_del() to free anything still left on peer->rx_queue and
peer->tx_queue. The aim is to fix a peer/keypair leak: once a disable is
pending, NAPI completes even when more than one budget of decrypted
packets is still sitting on rx_queue.

> diff --git a/drivers/net/wireguard/peer.c b/drivers/net/wireguard/peer.c
> index 1cb502a932e07..3e08898cec5ec 100644
> --- a/drivers/net/wireguard/peer.c
> +++ b/drivers/net/wireguard/peer.c
> @@ -91,6 +91,25 @@ static void peer_make_dead(struct wg_peer *peer)
>  	/* The caller must now synchronize_net() for this to take effect. */
>  }
>  
> +/* Each queue entry holds a keypair and a peer reference. Transmit entries
> + * are lists of packets, receive entries single packets.
> + */
> +static void peer_purge_queue(struct wg_peer *peer, struct prev_queue *queue,
> +			     bool lists)
> +{
> +	struct sk_buff *first;
> +
> +	while ((first = wg_prev_queue_peek(queue)) != NULL) {
> +		wg_prev_queue_drop_peeked(queue);
> +		wg_noise_keypair_put(PACKET_CB(first)->keypair, false);
> +		wg_peer_put(peer);
> +		if (lists)
> +			kfree_skb_list(first);
> +		else
> +			dev_kfree_skb(first);
> +	}
> +}
> +

[ ... ]

> @@ -122,6 +141,11 @@ static void peer_remove_after_dead(struct wg_peer *peer)
>  	 * here from process context.
>  	 */
>  	netif_napi_del(&peer->napi);
> +	/* A NAPI being disabled completes after at most one more poll, which
> +	 * may leave packets on rx_queue that still hold references.
> +	 */
> +	peer_purge_queue(peer, &peer->rx_queue, false);
> +	peer_purge_queue(peer, &peer->tx_queue, true);

[Severity: High]
Can this purge of peer->tx_queue run while peer->transmit_packet_work is
still pending or running on packet_crypt_wq?

All peers on a device share wg->encrypt_queue, and the per-CPU
wg_packet_encrypt_worker() drains it. Each flush_workqueue() call only
waits for work that was queued before that flush started. Here is one
possible interleaving:

flush_workqueue() #1 (step a) is running
  another peer's xmit queues encrypt worker W on packet_crypt_wq
    (flush #1 does not wait for W)
  W pops one of this peer's entries from the shared ring
flush #1 returns

flush_workqueue() #2 (step b.1) waits for W
  W: wg_packet_encrypt_worker()->wg_queue_enqueue_per_peer_tx()
       queue_work_on(..., peer->device->packet_crypt_wq,
                     &peer->transmit_packet_work);
    (queued after flush #2 started, so nothing waits for it)
flush #2 returns

napi_disable();
netif_napi_del();    /* only a synchronize_net() */
peer_purge_queue(peer, &peer->tx_queue, true);

If wg_packet_tx_worker() runs at the same time, it and peer_purge_queue()
both consume tx_queue. prev_queue is single-consumer, so
wg_prev_queue_peek() and wg_prev_queue_dequeue() update queue->peeked and
queue->tail without a lock.

Could both sides get the same skb list? That would mean a double
kfree_skb_list() plus a double wg_noise_keypair_put() and wg_peer_put().

If transmit_packet_work is still pending instead, the purge drops the
peer references held by the tx_queue entries. The final wg_peer_put() in
peer_remove_after_dead() can then go through
kref_release()->call_rcu(rcu_release) and free the peer while
transmit_packet_work is still linked on the workqueue.

Would wg_packet_tx_worker() then run container_of() on freed memory?

Before this patch, a late tx worker was the only consumer, and the queued
entries kept the peer alive.

This also seems to contradict the commit message:

  Free whatever is left on both per-peer queues once nothing can feed
  them any more

The follow-up patch in the series only touches the selftests and does
not change this code.

Would a flush_work() or cancel_work_sync() on &peer->transmit_packet_work
before purging tx_queue close this window?

[Severity: Low]
This isn't a bug, but the earlier comment in peer_remove_after_dead() is
left as it was, and it now contradicts the new one:

	/* The transition between packet encryption/decryption queues isn't
	 * guarded by is_dead, but each reference's life is strictly bounded by
	 * two generations: once for parallel crypto and once for serial
	 * ingestion, so we can simply flush twice, and be sure that we no
	 * longer have references inside these queues.
	 */

The a), b.1) and b.2.1) step comments also still describe the two
flushes plus napi_disable() as a complete drain.

__napi_poll() in net/core/dev.c does this:

	if (unlikely(napi_disable_pending(n))) {
		napi_complete(n);
		return work;
	}

That makes the new comment the accurate one. Should the old comment be
updated to match?

Also, the new comment only covers rx_queue, but the next line purges
tx_queue too. Could the comment say why the tx_queue purge is needed, or
that it is only defensive?

The follow-up selftest patch in the series doesn't update this comment
either.

>  
>  	/* Ensure any workstructs we own (like transmit_handshake_work or
>  	 * clear_peer_work) no longer are in use.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126094722.73025-1-apasichnik9%40gmail.com

  reply	other threads:[~2026-11-27  9:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26  9:47 [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it Andrii Pasichnyk
2026-11-26  9:47 ` [PATCH net v2 1/2] wireguard: peer: free packets left on the per-peer queues on removal Andrii Pasichnyk
2026-11-27  9:48   ` netdev-bot+sashiko [this message]
2026-11-27  9:59     ` Andrii Pasichnyk
2026-11-26  9:47 ` [PATCH net v2 2/2] wireguard: selftests: remove a peer under load Andrii Pasichnyk
2026-11-27  9:48   ` netdev-bot+sashiko
2026-11-27  9:59     ` Andrii Pasichnyk
2026-11-26  9:52 ` [PATCH net v2 0/2] wireguard: free queued packets on peer removal, and test it netdev-bot+sinfo
2026-11-27 10:22   ` Andrii Pasichnyk

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179577288773.3135.10772290029017294104@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=apasichnik9@gmail.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox