Root Cause Analysis
The lockdep warning (WARNING: SOFTIRQ-safe -> SOFTIRQ-unsafe lock order detected) is caused by tap_sock_destruct() in drivers/net/tap.c calling ptr_ring_cleanup() (which acquires &q->ring.consumer_lock via ptr_ring_consume()) from a socket destruction path that can execute under softirq-safe locks or in softirq context, while the tap ptr_ring consumer_lock class is acquired in softirq-unsafe (BH-enabled) process context.
1. SOFTIRQ-unsafe usage of &q->ring.consumer_lock
In drivers/net/tap.c, tap_open() initializes struct tap_queue::ring (struct ptr_ring) via ptr_ring_init() (creating a single static lockdep key ptr_ring_init_noprof.__key for all tap_queue instances) and sets q->sk.sk_destruct = tap_sock_destruct.
Multiple process-context paths acquire &q->ring.consumer_lock with softirqs enabled via plain spin_lock():
Because of these acquisitions (such as vhost_net_ioctl() -> vhost_net_set_backend() -> vhost_net_enable_vq() -> vhost_poll_start() -> tap_poll()), lockdep classifies &q->ring.consumer_lock as SOFTIRQ-unsafe.
2. Deferred destruction of tap_queue in BH-disabled / SOFTIRQ-safe context
Outbound packets allocated by tap_get_user() -> tap_alloc_skb() (drivers/net/tap.c:556, calling sock_alloc_send_pskb()) charge sk->sk_wmem_alloc on &q->sk and set skb->destructor = sock_wfree.
When the tap file is closed (tap_release() -> tap_put_queue() in drivers/net/tap.c:159), tap_put_queue() disables the queue, calls synchronize_rcu(), and drops the socket references via sock_put(&q->sk). However, if any outbound sk_buff owned by &q->sk is still in flight (sk_wmem_alloc > 0), sk_destruct() (tap_sock_destruct()) does not run during tap_put_queue(). Instead, it runs later when the last in-flight sk_buff is freed in sock_wfree() (net/core/sock.c:2753) -> __sk_free() -> sk_destruct() -> __sk_destruct() -> tap_sock_destruct().
In the reported trace:
wg_expired_retransmit_handshake() (drivers/net/wireguard/timers.c:73, running in timer softirq) calls wg_socket_clear_peer_endpoint_src() (drivers/net/wireguard/socket.c:309), acquiring write_lock_bh(&peer->endpoint_lock). Lockdep therefore marks &peer->endpoint_lock as SOFTIRQ-safe.
wg_packet_tx_worker() -> wg_packet_create_data_done() -> wg_socket_send_skb_to_peer() (drivers/net/wireguard/socket.c:172) acquires read_lock_bh(&peer->endpoint_lock) and holds it across send4() -> udp_tunnel_xmit_skb() -> iptunnel_xmit() -> ip_local_out() -> ip_output() -> ip_finish_output2() -> neigh_hh_output() -> dev_queue_xmit() -> __dev_queue_xmit() -> __dev_xmit_skb().
- At
net/core/dev.c:4496, __dev_xmit_skb() calls tcf_kfree_skb_list() (net/sched/sch_generic.c:59), freeing a dropped tap-owned sk_buff via kfree_skb_reason() -> sock_wfree() -> __sk_free() -> sk_destruct() -> tap_sock_destruct().
tap_sock_destruct() (drivers/net/tap.c:444) calls ptr_ring_cleanup(&q->ring, __skb_array_destroy_skb) (include/linux/ptr_ring.h:700), which loops calling ptr_ring_consume(&q->ring) -> spin_lock(&r->consumer_lock).
Acquiring &q->ring.consumer_lock (SOFTIRQ-unsafe) while holding &peer->endpoint_lock (SOFTIRQ-safe) establishes the dependency &peer->endpoint_lock -> &r->consumer_lock and triggers the lockdep warning. (Contrast with drivers/net/tun.c, which calls ptr_ring_cleanup() in __tun_detach() during file teardown in process context rather than from sk_destruct().)