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

From: Shuai Liu

Date: Wed Nov 25 2026 - 19:35:08 EST


tap_sock_destruct() calls ptr_ring_cleanup(), which in turn uses
ptr_ring_consume() with a plain spin_lock() for consumer_lock.
This destructor runs in two distinct contexts:

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.

2. process context: from tap_release() -> __fput() -> sk_free()
-> sk_destruct() -> tap_sock_destruct() (triggered by
close_range syscall). Here softirq is enabled when the
same consumer_lock is taken.

lockdep flags this as inconsistent {IN-SOFTIRQ-W} ->
{SOFTIRQ-ON-W} usage of (&r->consumer_lock) and warns about
a possible deadlock scenario.

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.

Reported-by: syzbot <syzbot+a2220efe757ca2a7358d@xxxxxxxxxxxxxxxxxxxxxxxxx>
Link: https://syzkaller.appspot.com/bug?extid=a2220efe757ca2a7358d
Signed-off-by: Shuai Liu <847807867@xxxxxx>
---
drivers/net/tap.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)

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);
+ if (!ptr)
+ break;
+ __skb_array_destroy_skb(ptr);
+ }
+ kvfree(q->ring.queue);
}

static int tap_open(struct inode *inode, struct file *file)
--
2.43.0