Root Cause Analysis
The lockdep warning (inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} usage on &r->consumer_lock#3) is caused by tap_sock_destruct() calling ptr_ring_cleanup() when q->sk is destroyed from softirq context (sock_wfree()), while q->ring.consumer_lock is otherwise acquired with plain spin_lock() in process context (with softirqs enabled).
Mechanism and Involved Code
-
Ring initialization and lock usage (drivers/net/tap.c, include/linux/ptr_ring.h):
tap_open() (drivers/net/tap.c:464) initializes q->ring via ptr_ring_init(&q->ring, tap->dev->tx_queue_len, GFP_KERNEL), creating the lockdep class &r->consumer_lock for q->ring.consumer_lock.
tap_open() sets q->sk.sk_destruct = tap_sock_destruct (drivers/net/tap.c:476).
- In normal operation,
&q->ring.consumer_lock is acquired in process context with softirqs enabled (SOFTIRQ-ON-W) via plain spin_lock(&r->consumer_lock):
-
Socket ownership of TX sk_buffs and deferred destruction in softirq:
- When userspace writes a packet via
tap_write_iter() / tap_sendmsg() -> tap_get_user() (drivers/net/tap.c:623) -> tap_alloc_skb() (drivers/net/tap.c:556), sock_alloc_send_pskb() calls skb_set_owner_w(skb, &q->sk) (net/core/sock.c:2772), which sets skb->sk = &q->sk, sets skb->destructor = sock_wfree, and adds skb->truesize to q->sk.sk_wmem_alloc.
tap_get_user() then submits the packet via dev_queue_xmit(skb) (drivers/net/tap.c:675).
- If the tap file descriptor is closed while such a TX
sk_buff is still in flight, tap_release() -> tap_put_queue() detaches q, calls synchronize_rcu(), and drops sk_refcnt via sock_put(&q->sk) (drivers/net/tap.c:179). sk_free(&q->sk) (net/core/sock.c:2469) decrements sk->sk_wmem_alloc by the bias (1), which does not reach 0 because of the in-flight skb.
- Later, when the in-flight
skb is freed in softirq context (here in ksoftirqd: net_rx_action() -> process_backlog() -> llc_rcv() -> kfree_skb() -> skb_release_head_state() -> sock_wfree() at net/core/sock.c:2754), sk->sk_wmem_alloc reaches 0, triggering __sk_free(&q->sk) -> sk_destruct() -> __sk_destruct() -> tap_sock_destruct(&q->sk).
-
Lockdep violation in ptr_ring_cleanup():
tap_sock_destruct() (drivers/net/tap.c:444) calls ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb).
ptr_ring_cleanup() (include/linux/ptr_ring.h:695-703) drains the ring by calling ptr_ring_consume(r) (include/linux/ptr_ring.h:359-368), which acquires r->consumer_lock using plain spin_lock(&r->consumer_lock) (IN-SOFTIRQ-W) rather than draining locklessly (e.g., __ptr_ring_consume(r)) or using BH/IRQ-safe locking.
- Because
&q->ring.consumer_lock shares the same lockdep class with process-context SOFTIRQ-ON-W acquisitions, lockdep detects {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} and emits the inconsistent lock state warning.
Why No Real Deadlock Occurs on the Lock Instance
Although this violates lockdep's lock-class rules, tap_sock_destruct() only executes once per struct tap_queue after both sk_refcnt and sk_wmem_alloc have reached 0 (and after tap_put_queue()'s synchronize_rcu() or tap_del_queues() teardown). At that point, no file descriptor, tap_dev, tap_queue_resize() caller (which uses spin_lock_bh() anyway), or vhost_net consumer retains a reference to q or q->ring, so no process context can contend for &q->ring.consumer_lock concurrently with tap_sock_destruct().