๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #23279

Re: [PATCH] net: tap: fix inconsistent lock state in tap_sock_destruct

#23279
From: netdev-bot+sashiko
Date: Fri, 27 Nov 2026 01:00
161 lines
14022 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 ยท High: 1 ยท Medium: 1 ยท 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 patch does not remove the lockdep report it says it fixes.
- [Medium] The commit message's technical explanation has factual errors.
- [Low] The patch fixes a syzbot-reported lockdep problem in
  tap_sock_destruct() but has no Fixes: tag.

--

net: tap: fix inconsistent lock state in tap_sock_destruct

This patch replaces the ptr_ring_cleanup() call in tap_sock_destruct()
with an open-coded loop. The loop takes the ring's consumer_lock with
spin_lock_irqsave() and then calls kvfree() on the ring queue. The aim is
to silence a syzbot lockdep report of inconsistent {IN-SOFTIRQ-W} ->
{SOFTIRQ-ON-W} usage of consumer_lock.

Link: https://syzkaller.appspot.com/bug?extid=a2220efe757ca2a7358d

> 1. softirq context: from softirq rx drop paths via
>    skb_release_head_state() -> sock_wfree() -> __sk_free() ->
>    sk_destruct() -> tap_sock_destruct().  In softirq the BH
>    state is already disabled, so consumer_lock is taken with
>    SOFTIRQ-ON-W semantics.

[ ... ]

> Fix by hand-rolling the cleanup loop with spin_lock_irqsave() /
> spin_unlock_irqrestore() so consumer_lock is always taken from a
> uniform softirq state.  irqsave is a no-op on the softirq path
> (interrupts are already off), so there is no extra cost there.

[Severity: Medium]
Are the lockdep states the wrong way round here? When the lock is taken
from softirq context, lockdep records IN-SOFTIRQ-W. SOFTIRQ-ON-W is the
process context case, where softirqs are enabled.

Are interrupts actually off on the softirq path? Softirq handlers and
RCU callbacks run with hardirqs enabled. So spin_lock_irqsave() really
does disable interrupts there, and it is not a no-op.

No hardirq context takes consumer_lock, so why mask hardirqs at all? The
_bh helpers such as ptr_ring_consume_bh() seem to fit a softirq versus
process context conflict.

tap_sock_destruct() only runs after the last socket reference is gone.
At that point no other context can hold the same ring's consumer_lock.
That makes this a lock class report, not a real deadlock on a single
ring. Could the commit message say that?

Also, the new loop in tap_sock_destruct() is the same as the existing
helper in include/linux/ptr_ring.h:

static inline void *ptr_ring_consume_any(struct ptr_ring *r)
{
	...
	spin_lock_irqsave(&r->consumer_lock, flags);
	ptr = __ptr_ring_consume(r);
	spin_unlock_irqrestore(&r->consumer_lock, flags);
	...
}

The loop plus the kvfree(q->ring.queue) copies the internals of
ptr_ring_cleanup() into the driver. Would tap then miss future changes
to that helper?

> Reported-by: syzbot <syzbot+a2220efe757ca2a7358d@syzkaller.appspotmail.com>
> Link: https://syzkaller.appspot.com/bug?extid=a2220efe757ca2a7358d
> Signed-off-by: Shuai Liu <847807867@qq.com>

[Severity: Low]
This isn't a bug, but should there be a Fixes: tag? The replaced
ptr_ring_cleanup() call in tap_sock_destruct() came from commit
5990a30510ed ("tun/tap: use ptr_ring instead of skb_array"). That commit
kept the ring cleanup in the destructor that commit 362899b8725b
("macvtap: switch to use skb array") added.

> diff --git a/drivers/net/tap.c b/drivers/net/tap.c
> index 2f19fb07bb2d4..d749c7e73858a 100644
> --- a/drivers/net/tap.c
> +++ b/drivers/net/tap.c
> @@ -440,8 +440,18 @@ static void tap_sock_write_space(struct sock *sk)
>  static void tap_sock_destruct(struct sock *sk)
>  {
>  	struct tap_queue *q = container_of(sk, struct tap_queue, sk);
> -
> -	ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
> +	unsigned long flags;
> +	void *ptr;
> +
> +	while (true) {
> +		spin_lock_irqsave(&q->ring.consumer_lock, flags);
> +		ptr = __ptr_ring_consume(&q->ring);
> +		spin_unlock_irqrestore(&q->ring.consumer_lock, flags);

[Severity: High]
Does this change actually stop the lockdep report? Lockdep tracks lock
classes, not individual locks. Every tap queue's consumer_lock shares
one class, set by the spin_lock_init() in ptr_ring_init(), which
tap_open() calls.

tap_sock_destruct() can still run in softirq. One example is an skb
allocated by sock_alloc_send_pskb() in tap_get_user(), sent with
dev_queue_xmit() and then freed in softirq:

kfree_skb()
  sock_wfree()
    __sk_free()
      sk_destruct()
        tap_sock_destruct()

Lockdep marks the class IN-SOFTIRQ-W whenever it is taken in softirq
context, even when spin_lock_irqsave() is used.

Several other users of the same class still take it with a plain
spin_lock() in process context, with softirqs enabled:

drivers/net/tap.c:tap_do_read()
    skb = ptr_ring_consume(&q->ring);

drivers/net/tap.c:tap_poll()
    if (!ptr_ring_empty(&q->ring))

drivers/net/tap.c:tap_peek_len()
    return PTR_RING_PEEK_CALL(&q->ring, __skb_array_len_with_tag);

drivers/vhost/net.c:vhost_net_buf_produce()
    spin_lock(&nvq->rx_ring->consumer_lock);

The vhost case works on the ring returned by tap_get_ptr_ring().

Won't any normal tap reader, poller or vhost-net backend still record
SOFTIRQ-ON-W? Then the same {IN-SOFTIRQ-W} -> {SOFTIRQ-ON-W} splat can
fire again. The patch seems to stop only the destructor's own process
context call from counting as SOFTIRQ-ON.

Because this is a class-level report, should the fix cover the whole
class rather than one call site? Some options:

- Make every consumer_lock user in tap and vhost BH-safe, as resize
  already does with ptr_ring_resize_multiple_bh().

- Drain the ring in the destructor without taking the lock, since
  nothing else can use the ring at that point.

- Give the destructor's acquisition a separate lockdep annotation.

[ ... ]

-- 
Sashiko AI review ยท https://netdev-ai.bots.linux.dev/sashiko/#/patchset/tencent_934E899D4D24BB3B4827B2A0C9D73461E00A%40qq.com
Technical Details
Message-ID: <179574121714.3135.6932520604039855103@kernel.org>
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372396-linux-kernel-netdev=dewy.ru
References: <tencent_934E899D4D24BB3B4827B2A0C9D73461E00A@qq.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-372396-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH] net: tap: fix inconsistent lock state in tap_sock_destruct
Date: Fri, 27 Nov 2026 01:00:17 +0000
Organization: Usenet Network
Message-ID: <179574121714.3135.6932520604039855103@kernel.org>
References: <tencent_934E899D4D24BB3B4827B2A0C9D73461E00A@qq.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="606305"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:hL0EdrGKY1bYe/RDbBCI3e8P1xE= sha256:pVxPJs94PIZgTR/FrV+joXUHnaIAqSOftb56oY/iSx0=
	sha1:sdVCmu8rWhsDZK0UPNyunZIK1Bs= sha256:YQLiJKJB+OlRh6nDl5x4rbffITXuIyLskctG9+XrDLE=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 47656125F0C; Fri, 27 Nov 2026 04:00:49 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.501
X-Spam-Status: No, score=-3.501 tagged_above=-9999 required=6.31
	tests=[DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 5I0qmY5iMeze;
	Fri, 27 Nov 2026 04:00:41 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-372396-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id EF158125F0A; Fri, 27 Nov 2026 04:00:31 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id F3E37603194; Fri, 27 Nov 2026 01:00:30 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795741229;
	b=NB4UUnMDMUQF7UB7ZG2Fj8pqsqGmkWubL0py184H70JSdGNH+8MZZtBqbO9rH4U7gf+V
	 sFcKaV2M289VlHu6nQxGR06BElAB1UL5kbZ/D1j+mDPuKMtfacwfzVMZz/+IHKjs/8J8y
	 Oy+v+r8fPb5kZoKJ/6Re34FyoW1+srfPrVG1FSJXn9GcxT/S5OoyaXQ4++yu2rbVWn+bl
	 Cg3yDMZQ1LLq+xyGROQmXmkMfmTLfdJBCKT4BpA+0LI9ge2HNzwQEHFUi0UaA7oMknX6T
	 qxxZZF6YwInPaUrKdpY7NMPLrkPKkAppQuir40RFzTT2+E+mKyTZ/eYaBD0lAE7roUg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795741229;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=FkfiyFa5z5LQjJ6nkngWn4LI3fHUmNEL40d6e9Mt7Eo=;
	b=jAkIxaovxx5S5Qhzz3K2Pm3EUoanbMNtCT0+0YbXkMe9hwX8hRwn7hIxRCJng5CfzCuR
	 mJc13x7RmFCA8d8ZHG9sQWBb+1GkxG0AxkgWxdGz9lZeT67DorIlrpg4daPFWcrhlLwVc
	 BmOe1SlQlFyWf4lMLV5QHgyypaPpOWXZCrl1Zh68FVY5GP4Y6YLLva7jWxi3Rsw5bydo9
	 iOTblS3ByKQc//TioPKo/DwAPvoeWRyIcpelhT5rY/J+yGeffinpwj18jtUHZpzB5l8oP
	 Rhyf3JwQsCjGQuAoUUjat+Sj/o+JjAqc8IanueBzuGg91DROctROn52CKEsJl33tzZA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=hadvIiEm;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 99773606A324;
	Fri, 27 Nov 2026 01:00:29 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=hadvIiEm
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795741218;
	b=zVQZ5dsNBulFzQFs9Axmr/G0I6kZrguVfB54WySVSDXtH5qRndnv4MJ6B0FH/L9xWkfx
	 TW8oEkAQ196oZMcG6rfG4z1h/RQh/vXItPRw8eTBi3FVnRQ9+6mZHo9huHzkDARQrB1uH
	 GgneurxXpRgdj20kdZK44pCsBVP+wp9s5Ol9fVy4vgjFtcGwzpIz89vTBJyDih0e9JFRI
	 0S/1LqJZ6gywnVQW8mnM1RXlYjT07oktYbsEThnnIWtmhDNNK4Co6S3B9YKK7Z3ZpFXds
	 esTlgBqC99VOb28PdpUveWkVwAjvDQwTt7NkwJBrPNBDBQ7TFkBN1Zmo3uUzUWxtUbQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795741218;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=FkfiyFa5z5LQjJ6nkngWn4LI3fHUmNEL40d6e9Mt7Eo=;
	b=3rVe1zuHgNavf9AKxmfksqNECwgrjkxapCuS1uz0tQhRuuBp4YSErwrDahZ9+baEB0Io
	 WoTNc6mtLp5CXIh33YdedgDC+dQLQKXVGN5VOg6Df7UjIPnLgJRyTD3RUeqgfazJO+MuJ
	 m7nlQPdLaxmTwo0sxBcFIkM97nR7ijmLtDyqZGHQytBS+cdbxQAfiz+o+eTBDXAQsRDdk
	 /MgZs8b0rWwLlrWEnMe+WoTkpqXzx5FAD13xwETNSR3B8cEa0kTIIGB7z00KiqZDb9c8X
	 79/MRgOOI2rjsnZeuveNh5Ue6ymDQP6UNkhtQmHGjPRvs/D7+1WDBLUsu7AuzyKgsqA==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=hadvIiEm;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id B65D7606A31C;
	Fri, 27 Nov 2026 01:00:18 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id A25311F00897;
	Fri, 27 Nov 2026 01:00:17 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795741218;
	bh=FkfiyFa5z5LQjJ6nkngWn4LI3fHUmNEL40d6e9Mt7Eo=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=hadvIiEmPgUxab67HE6XQIwyhNuJNM2ivDJM2zEL7wyhPfY6StS1xvWITIo04J2uB
	 IAf1wKJPg3yaIva90aNpr/j4PMYaPq7aFolrHIbeWMd+//3RucTvUDvI+wnSkbKNYZ
	 llWM9AcshK96iO4hajENJFsBy9NPh9kycsS7RQVWRbzV62WofPI2UWns4oWemccTxM
	 O6WP3ev7ge9XOhg0dbsO7my6ZNUeFkV5yVgrThA5dLqtBtH2lqkcv1JA5X17SW3+5s
	 QabBSQ1y83q5wlxuBxT7UhAu1crifGCeyWB1P/eunp4/AdHdnXEB+reoMj+wEB6+R9
	 METdJ/SnxaUYQ==
X-Cc: willemdebruijn.kernel@gmail.com, jasowangio@gmail.com,
 andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@kernel.org,
 kuba@kernel.org, pabeni@redhat.com,
 syzbot+a2220efe757ca2a7358d@syzkaller.appspotmail.com, netdev@vger.kernel.org,
 linux-kernel@vger.kernel.org
X-In-Reply-To: <tencent_934E899D4D24BB3B4827B2A0C9D73461E00A@qq.com>
X-sashiko-severity: High
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list