mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] tap: do not take consumer_lock in tap_sock_destruct()
@ 2026-11-26  9:03 Jiayi Zhang
  2026-11-26 10:16 ` Shuai Liu
  0 siblings, 1 reply; 2+ messages in thread
From: Jiayi Zhang @ 2026-11-26  9:03 UTC (permalink / raw)
  To: willemdebruijn.kernel, jasowangio
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, mst, 847807867,
	netdev, linux-kernel, syzbot+a2220efe757ca2a7358d,
	syzkaller-bugs, Jiayi Zhang

syzbot reported an inconsistent lock state for the consumer_lock of the
ptr_ring of a tap queue:

  WARNING: inconsistent lock state
  inconsistent {IN-SOFTIRQ-W} -> {SOFTIRQ-ON-W} usage.
  (&r->consumer_lock#4){+.?.}-{3:3}, at: tap_sock_destruct+0x13b/0x330
  {IN-SOFTIRQ-W} state was registered at:
    ptr_ring_consume include/linux/ptr_ring.h:363 [inline]
    ptr_ring_cleanup include/linux/ptr_ring.h:700 [inline]
    tap_sock_destruct+0x13b/0x330 drivers/net/tap.c:444
    __sk_destruct+0x85/0xb70 net/core/sock.c:2386
    sock_wfree+0x254/0x7f0 net/core/sock.c:2754
    [...]
    llc_rcv+0x4d5/0xa70 net/llc/llc_input.c:112
  Call Trace:
    tap_sock_destruct+0x13b/0x330 drivers/net/tap.c:444
    __sk_destruct+0x85/0xb70 net/core/sock.c:2386
    tap_put_queue drivers/net/tap.c:179 [inline]
    tap_release+0x2fc/0x450 drivers/net/tap.c:517

tap_sock_destruct() drains the ring with ptr_ring_cleanup(), which takes
consumer_lock with spin_lock(). The destructor runs in process context
when tap_release() drops the last reference. It also runs in softirq
context: skbs sent through the tap file are charged to the socket by
sock_alloc_send_pskb(), so if some of them are still in flight when the
file is closed, the socket is destroyed from sock_wfree() when the last
one is freed, here by llc_rcv().

All other users of the lock run in process context: tap_do_read(),
tap_poll(), tap_peek_len() and vhost-net's vhost_net_buf_produce() take
it with spin_lock(), tap_queue_resize() with spin_lock_bh().

This cannot deadlock: a ring that is being destroyed has no other user,
and lockdep complains because all tap rings share one lock class. When
the destructor runs, the file and the device have dropped their
references and the queue was unlinked from the device under RTNL.
tap_handle_frame() can no longer reach it either: tap_put_queue() waits
for an RCU grace period, and the dellink paths unregister the rx handler
before tap_del_queues().

So the destructor does not need the lock. Drain the ring with
__ptr_ring_consume() and let ptr_ring_cleanup() only free the array.
Making every user of the lock BH-safe instead would add work to the read
path of tap and of vhost-net for a ring that nobody else can access.

Tested with lockdep and KASAN, using the syzbot config. The syzbot
reproducer no longer triggers the report. I also wrote a test that
closes a macvtap queue while frames are still unread in its ring and
while frames it sent are held back by netem, so that the destructor
runs in softirq. It triggers the same report on an unpatched kernel and
runs clean with this patch. The tap selftest passes, and so does
tools/virtio/vhost_net_test changed to use a macvtap queue as backend.

Fixes: 362899b8725b ("macvtap: switch to use skb array")
Reported-by: syzbot+a2220efe757ca2a7358d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a2220efe757ca2a7358d
Signed-off-by: Jiayi Zhang <zhangjiayi1014@gmail.com>
---
AI tools assisted with the analysis, the code and the changelog.

A patch for the same report was posted earlier today:
https://lore.kernel.org/r/tencent_934E899D4D24BB3B4827B2A0C9D73461E00A@qq.com
It takes the lock with spin_lock_irqsave() in the destructor only. With
it applied, the close test above still triggers the report: the lock is
taken in softirq by tap_sock_destruct() and with softirqs enabled by
tap_poll().

 drivers/net/tap.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/net/tap.c b/drivers/net/tap.c
index 2f19fb07bb2d..81dd616909d5 100644
--- a/drivers/net/tap.c
+++ b/drivers/net/tap.c
@@ -440,8 +440,12 @@ 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);
+	struct sk_buff *skb;
 
-	ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb);
+	/* The ring has no other user left: no need for consumer_lock. */
+	while ((skb = __ptr_ring_consume(&q->ring)))
+		kfree_skb(skb);
+	ptr_ring_cleanup(&q->ring, NULL);
 }
 
 static int tap_open(struct inode *inode, struct file *file)
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH net] tap: do not take consumer_lock in tap_sock_destruct()
  2026-11-26  9:03 [PATCH net] tap: do not take consumer_lock in tap_sock_destruct() Jiayi Zhang
@ 2026-11-26 10:16 ` Shuai Liu
  0 siblings, 0 replies; 2+ messages in thread
From: Shuai Liu @ 2026-11-26 10:16 UTC (permalink / raw)
  To: zhangjiayi1014, willemdebruijn.kernel, jasowangio
  Cc: andrew+netdev, davem, edumazet, kuba, pabeni, mst, 847807867,
	netdev, linux-kernel, syzbot+a2220efe757ca2a7358d,
	syzkaller-bugs

Hi Jiayi,

Thanks for your detailed analysis — your fix is definitely the right
approach, and you caught a flaw in my reasoning that I missed entirely.

My patch only changed tap_sock_destruct() to acquire consumer_lock with
spin_lock_irqsave(), assuming that making the destructor take the lock
with uniform BH semantics would be enough. But I completely failed to
consider the other holders of consumer_lock: tap_do_read(), tap_poll(),
tap_peek_len() and vhost_net_buf_produce() all take it with plain
spin_lock(), and tap_queue_resize() with spin_lock_bh(). So lockdep
would still flag the inconsistency between {IN-SOFTIRQ-W} and
{SOFTIRQ-ON-W} for the same lock class — exactly as you demonstrated.

Your argument that the destructor does not need the lock at all is
solid: tap_put_queue() runs synchronize_rcu() and RCU_INIT_POINTER(q->tap,
NULL) before the final sock_put(), so tap_handle_frame() can no longer
reach the queue via RCU dereference. The dellink paths unregister the rx
handler before tap_del_queues(). No other path can race with the
destructor.

Applied your patch locally, builds cleanly and passes checkpatch strict
with 0 errors / 0 warnings.

Please ignore my earlier patch — it's wrong and should not be applied.

Reviewed-by: Shuai Liu <847807867@qq.com>
Tested-by: Shuai Liu <847807867@qq.com>

Shuai

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26 10:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26  9:03 [PATCH net] tap: do not take consumer_lock in tap_sock_destruct() Jiayi Zhang
2026-11-26 10:16 ` Shuai Liu

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®