mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: sidchintamaneni@gmail.com
Cc: kuniyu@google.com, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, shuah@kernel.org,
	willemb@google.com, jannh@google.com, tkhai@ya.ru,
	apais@microsoft.com, rlmenge@gmail.com,
	hargar@linux.microsoft.com, netdev@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net 3/4] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected().
Date: Tue, 10 Nov 2026 06:38:20 +0000	[thread overview]
Message-ID: <179429270039.994134.15193839988115101095@kernel.org> (raw)
In-Reply-To: <20261106051905.218638-4-sidchintamaneni@gmail.com>

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

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

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).

- [Medium] In net/unix/af_unix.c, unix_dgram_disconnected() used to free
  a fixed snapshot of the receive queue.

--

af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected().

This patch changes unix_dgram_disconnected() so that it dequeues each skb
from sk->sk_receive_queue, calls scm_stat_del(), and only then frees the
skb. Before this, skb_queue_purge_reason() freed the skbs without updating
the counter, so u->scm_stat.nr_fds stayed too high after a disconnect.
scm_stat_add() and scm_stat_del() move above unix_dgram_disconnected() so
they can be called from there.

> diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
> index 44c0a81dd423a..47120d81aca28 100644
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c

[ ... ]

> @@ -616,8 +638,12 @@ static void unix_write_space(struct sock *sk)
>  static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
>  {
>  	if (!skb_queue_empty(&sk->sk_receive_queue)) {
> -		skb_queue_purge_reason(&sk->sk_receive_queue,
> -				       SKB_DROP_REASON_UNIX_DISCONNECT);
> +		struct sk_buff *skb;
> +
> +		while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
> +			scm_stat_del(sk, skb);
> +			kfree_skb_reason(skb, SKB_DROP_REASON_UNIX_DISCONNECT);
> +		}

[Severity: Medium]
Can this loop keep running while other senders refill the queue?

skb_queue_purge_reason() took a snapshot of the queue. It took the lock
once, spliced everything onto a private list, and freed only that list:

net/core/skbuff.c:skb_queue_purge_reason() {
    ...
	spin_lock_irqsave(&list->lock, flags);
	skb_queue_splice_init(list, &tmp);
	spin_unlock_irqrestore(&list->lock, flags);

	__skb_queue_purge_reason(&tmp, reason);
}

The new loop drains the live queue until it happens to be empty. It also
takes and drops the irq-disabling queue lock once per skb.

Both callers have already published the new peer state and dropped the
unix state locks before they call this. In unix_dgram_connect():

	unix_peer(sk) = other;
	...
	unix_state_double_unlock(sk, other);

	if (other != old_peer) {
		unix_dgram_disconnected(sk, old_peer);

On the dead-peer path in unix_dgram_sendmsg():

	unix_peer(sk) = NULL;
	unix_dgram_peer_wake_disconnect_wakeup(sk, other);

	WRITE_ONCE(sk->sk_state, TCP_CLOSE);
	unix_state_unlock(sk);

	unix_dgram_disconnected(sk, other);

>From then on the new peer can send to sk. If the peer is now NULL
(AF_UNSPEC or the dead-peer case), any socket that can reach sk's bound
address can send to it as well. Those senders reach this code in
unix_dgram_sendmsg() while the loop is still running:

	scm_stat_add(other, skb);
	skb_queue_tail(&other->sk_receive_queue, skb);

The receiver-side flow control does not hold them back:

	if (other != sk &&
	    unlikely(unix_peer(other) != sk &&
	    unix_recvq_full_lockless(other))) {

This check is skipped completely when the sender is sk's new peer. For
other senders it only applies once the queue is above sk_max_ack_backlog,
and the loop keeps the queue below that.

If several CPUs keep sending, connect() or sendmsg() could stay in this
loop with no fixed bound and no cond_resched(). Could that cause soft
lockups on non-preemptible kernels?

Datagrams that the new peer queues after the peer change would also be
dropped as SKB_DROP_REASON_UNIX_DISCONNECT for as long as the loop runs.
Before this patch, that window ended at the single splice.

For reference, the old skb_queue_purge_reason() used the same
dequeue-until-empty form before commit d86e5fbd4c96 changed it to splice.

Would it work to keep the splice? The queue could be spliced into a local
sk_buff_head under sk->sk_receive_queue.lock, and then the local list
walked with scm_stat_del() and kfree_skb_reason() on each skb. That keeps
the new nr_fds accounting and bounds the work at the time of the splice.

>  
>  		wake_up_interruptible_all(&unix_sk(sk)->peer_wait);
>  

[ ... ]

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

  reply	other threads:[~2026-11-10  6:38 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-06  5:19 [PATCH net 0/4] af_unix: Fix inq_len and scm_stat.nr_fds accounting leaks Siddharth Chintamaneni
2026-11-06  5:19 ` [PATCH net 1/4] af_unix: Fix inq_len leak when skipping unread OOB skb Siddharth Chintamaneni
2026-11-06  5:19 ` [PATCH net 2/4] af_unix: Fix scm_stat.nr_fds leaks for " Siddharth Chintamaneni
2026-11-10  6:38   ` netdev-bot+sashiko
2026-11-06  5:19 ` [PATCH net 3/4] af_unix: Fix scm_stat.nr_fds leak in unix_dgram_disconnected() Siddharth Chintamaneni
2026-11-10  6:38   ` netdev-bot+sashiko [this message]
2026-11-06  5:19 ` [PATCH net 4/4] selftest: af_unix: Test SIOCINQ and scm_fds with OOB and dgram disconnect Siddharth Chintamaneni
2026-11-10  6:38   ` netdev-bot+sashiko
2026-11-06  5:21 ` [PATCH net 0/4] af_unix: Fix inq_len and scm_stat.nr_fds accounting leaks netdev-bot+sinfo
2026-11-10  6:38 ` Kuniyuki Iwashima

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=179429270039.994134.15193839988115101095@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=apais@microsoft.com \
    --cc=edumazet@kernel.org \
    --cc=hargar@linux.microsoft.com \
    --cc=horms@kernel.org \
    --cc=jannh@google.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=rlmenge@gmail.com \
    --cc=shuah@kernel.org \
    --cc=sidchintamaneni@gmail.com \
    --cc=tkhai@ya.ru \
    --cc=willemb@google.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

all inboxes | Powered by JetHome®