net: lapb: fix circular locking dependency with lapbether and hdlc_x25
Subsystems (4)
syzbot
syzbotLockdep reports a circular locking dependency between lapb_list_lock, &lapb->lock, _xmit_X25, and &lapbeth->up_lock:
WARNING: possible circular locking dependency detected task is trying to acquire lock:ffffffff91666a98 (lapb_list_lock){++..}-{3:3}, at: lapb_devtostruct net/lapb/lapb_iface.c:102 [inline]ffffffff91666a98 (lapb_list_lock){++..}-{3:3}, at:lapb_connect_request+0x2c/0x270 net/lapb/lapb_iface.c:287
but task is already holding lock:ffff888033d5cf40 (&lapbeth->up_lock){+...}-{3:3}, at:lapbeth_xmit+0x32/0x260 drivers/net/wan/lapbether.c:191
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #3 (&lapbeth->up_lock){+...}-{3:3}:
lapbeth_xmit+0x32/0x260 drivers/net/wan/lapbether.c:191
dev_hard_start_xmit+0x2cd/0x830 net/core/dev.c:4084
__dev_queue_xmit+0x1237/0x3260 net/core/dev.c:5100-> #2 (_xmit_X25#2){+...}-{3:3}:
__dev_queue_xmit+0xd8c/0x3260 net/core/dev.c:5094
gre_tap_xmit+0x543/0x760 net/ipv4/ip_gre.c:783
__dev_queue_xmit+0x14d3/0x3260 net/core/dev.c:5058
lapb_data_transmit+0x90/0xb0 net/lapb/lapb_iface.c:447
lapb_transmit_buffer+0x163/0x200 net/lapb/lapb_out.c:149
lapb_establish_data_link+0x89/0xe0 net/lapb/lapb_out.c:-1
lapb_device_event+0x4eb/0x680 net/lapb/lapb_iface.c:-1-> #1 (&lapb->lock){+.-.}-{3:3}:
lapb_unregister+0x1e1/0x450 net/lapb/lapb_iface.c:187
lapbeth_close+0x56/0xb0 drivers/net/wan/lapbether.c:350-> #0 (lapb_list_lock){++..}-{3:3}:
lapb_devtostruct net/lapb/lapb_iface.c:102 [inline]
lapb_connect_request+0x2c/0x270 net/lapb/lapb_iface.c:287
lapbeth_xmit+0x133/0x260 drivers/net/wan/lapbether.c:205
dev_hard_start_xmit+0x2cd/0x830 net/core/dev.c:4084
__dev_queue_xmit+0x1237/0x3260 net/core/dev.c:5100
packet_sendmsg+0x4089/0x5330 net/packet/af_packet.c:3189
Chain exists of:
lapb_list_lock --> _xmit_X25#2 --> &lapbeth->up_lockThe cycle arises from bidirectional lock-ordering inversions across the lapbether, lapb, and netdevice transmit paths. In lapbeth_xmit() (invoked under the device TX queue lock _xmit_X25) and lapbeth_rcv(), lapbether acquires &lapbeth->up_lock and keeps it held across calls into lapb_connect_request(), lapb_disconnect_request(), lapb_data_request(), and lapb_data_received(), which acquire lapb_list_lock in lapb_devtostruct() and then &lapb->lock. Meanwhile, lapbeth_close() calls lapb_unregister(), which acquires write_lock_bh(&lapb_list_lock) and nests spin_lock_bh(&lapb->lock) inside it (and could also call usleep_range() while holding lapb_list_lock). Conversely, lapb_device_event(), LAPB timers, and other LAPB paths call lapb_transmit_buffer() -> lapb_data_transmit() -> lapbeth_data_transmit() -> dev_queue_xmit() synchronously while holding &lapb->lock. When the underlying Ethernet device is an L2 tunnel such as gretap whose outer route passes back through a lapb device, dev_queue_xmit() acquires _xmit_X25 and calls lapbeth_xmit() (&lapbeth->up_lock), closing the cycle and enabling real ABBA deadlocks as well as single-CPU self-deadlocks on &lapbeth->up_lock or &lapb->lock. The hdlc_x25 driver shares the same up_lock pattern across x25_xmit() and x25_rx(). In addition, lapbeth_setup() called netdev_lockdep_set_classes() before alloc_netdev_mqs() allocated TX queues (dev->num_tx_queues == 0), leaving the TX queue lock in the default _xmit_X25 class, and lapbeth did not mark itself lltx.
Fix this by decoupling locking across the LAPB and driver boundaries:
1. Defer outbound lower-layer transmission outside &lapb->lock by adding tx_queue and tx_work to struct lapb_cb. lapb_data_transmit() now queues frames onto lapb->tx_queue (bounded by LAPB_TX_QUEUE_LEN_MAX) and schedules lapb->tx_work on system_highpri_wq, while lapb_tx_work() splices the queue and invokes lapb->callbacks->data_transmit() under local_bh_disable()/local_bh_enable() without holding &lapb->lock.
2. Restructure lapb_unregister() to unlink the lapb_cb via __lapb_remove_cb() under write_lock_bh(&lapb_list_lock) and immediately release lapb_list_lock before waiting for other references to drop (refcount_read(&lapb->refcnt) > 1), acquiring &lapb->lock to clear queues and stop timers, calling timer_delete_sync(), cancelling tx_work, and purging tx_queue.
3. Remove up_lock from struct lapbethdev and struct x25_state. Publish device readiness with smp_store_release(&...->up, true) in lapbeth_open()/x25_open(), guard TX and RX fastpaths (lapbeth_xmit(), lapbeth_rcv(), x25_xmit(), x25_rx()) with RCU read lock and smp_load_acquire(&...->up) so no driver spinlock is held across lapb_*() calls, and set WRITE_ONCE(...->up, false) followed by synchronize_net() in lapbeth_close()/x25_close() before lapb_unregister(), napi_disable()/tasklet_kill(), and skb_queue_purge(&...->rx_queue).
4. In lapbether, move netdev_lockdep_set_classes() into a new .ndo_init callback (lapbeth_dev_init()) so it runs after TX queue allocation, set dev->lltx = true in lapbeth_setup(), and defer dev_put(ethdev) in lapbeth_free_device() and lapbeth_cleanup_driver() until after unregister_netdevice(lapbeth->axdev) closes the device and drains lapb->tx_work.
To: "Andrew Lunn" <[email protected]>
To: "David S. Miller" <[email protected]>
To: "Eric Dumazet" <[email protected]>
To: "Krzysztof Halasa" <[email protected]>
To: "Jakub Kicinski" <[email protected]>
To: <[email protected]>
To: "Martin Schiller" <[email protected]>
To: <[email protected]>
To: "Paolo Abeni" <[email protected]>
To: "Xie He" <[email protected]>
Cc: "Simon Horman" <[email protected]>
---
drivers/net/wan/hdlc_x25.c +13 -20
@@ -17,6 +17,7 @@#include <linux/module.h> #include <linux/pkt_sched.h> #include <linux/poll.h> +#include <linux/rcupdate.h> #include <linux/rtnetlink.h> #include <linux/skbuff.h> #include <net/x25device.h>
@@ -24,7 +25,6 @@struct x25_state {x25_hdlc_proto settings;bool up; -spinlock_t up_lock;/* Protects "up" */ struct sk_buff_head rx_queue;struct tasklet_struct rx_tasklet; };
@@ -127,9 +127,9 @@ static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)return NETDEV_TX_OK ; } -spin_lock_bh (&x25st->up_lock); -if (!x25st->up) { -spin_unlock_bh (&x25st->up_lock); +guard (rcu)(); +/* Pairs with smp_store_release() in x25_open(). */ +if (!smp_load_acquire (&x25st->up)) {kfree_skb (skb);return NETDEV_TX_OK ; }
@@ -140,7 +140,6 @@ static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev) result =lapb_data_request (dev, skb);if (result !=LAPB_OK )dev_kfree_skb (skb); -spin_unlock_bh (&x25st->up_lock);return NETDEV_TX_OK ;case X25_IFACE_CONNECT :
@@ -171,7 +170,6 @@ static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)break ; } -spin_unlock_bh (&x25st->up_lock);dev_kfree_skb (skb);return NETDEV_TX_OK ; }
@@ -214,9 +212,8 @@ static int x25_open(struct net_device *dev)if (result !=LAPB_OK )return -EINVAL ; -spin_lock_bh (&x25st->up_lock); - x25st->up = true; -spin_unlock_bh (&x25st->up_lock); +/* Publish LAPB setup before x25_xmit()/x25_rx() see up == true. */ +smp_store_release (&x25st->up, true);return 0 ; }
@@ -226,12 +223,12 @@ static void x25_close(struct net_device *dev)hdlc_device *hdlc =dev_to_hdlc (dev);struct x25_state *x25st =state (hdlc); -spin_lock_bh (&x25st->up_lock); - x25st->up = false; -spin_unlock_bh (&x25st->up_lock); +WRITE_ONCE (x25st->up, false); +synchronize_net ();lapb_unregister (dev);tasklet_kill (&x25st->rx_tasklet); +skb_queue_purge (&x25st->rx_queue); }static int x25_rx (struct sk_buff *skb)
@@ -246,20 +243,17 @@ static int x25_rx(struct sk_buff *skb)return NET_RX_DROP ; } -spin_lock_bh (&x25st->up_lock); -if (!x25st->up) { -spin_unlock_bh (&x25st->up_lock); +guard (rcu)(); +/* Pairs with smp_store_release() in x25_open(). */ +if (!smp_load_acquire (&x25st->up)) {kfree_skb (skb); dev->stats.rx_dropped++;return NET_RX_DROP ; } -if (lapb_data_received (dev, skb) ==LAPB_OK ) { -spin_unlock_bh (&x25st->up_lock); +if (lapb_data_received (dev, skb) ==LAPB_OK )return NET_RX_SUCCESS ; - } -spin_unlock_bh (&x25st->up_lock); dev->stats.rx_errors++;dev_kfree_skb_any (skb);return NET_RX_DROP ;
@@ -344,7 +338,6 @@ static int x25_ioctl(struct net_device *dev, struct if_settings *ifs)memcpy (&state (hdlc)->settings, &new_settings, size);state (hdlc)->up = false; -spin_lock_init (&state (hdlc)->up_lock);skb_queue_head_init (&state (hdlc)->rx_queue);tasklet_setup (&state (hdlc)->rx_tasklet, x25_rx_queue_kick);
drivers/net/wan/lapbether.c +29 -33
163 lines not on this page · raw message
include/net/lapb.h +3 -0
@@ -3,6 +3,7 @@#define _LAPB_H #include <linux/lapb.h>#include <linux/refcount.h> +#include <linux/workqueue.h> #define LAPB_HEADER_LEN MAX_HEADER/* LAPB over Ethernet + a bit more */
@@ -97,6 +98,8 @@ struct lapb_cb {/* Internal control information */ struct sk_buff_head write_queue;struct sk_buff_head ack_queue; +struct sk_buff_head tx_queue; +struct work_struct tx_work;unsigned char window;const struct lapb_register_struct *callbacks;
net/lapb/lapb_iface.c +42 -13
108 lines not on this page · raw message
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Signature · 9 lines
This is an AI-generated patch subject to moderation.
Reply with '#syz upstream' to Sign-off the patch as a human author
and send it to the upstream kernel mailing lists.
Reply with '#syz reject' to reject it ('#syz unreject' to undo).
See https://goo.gle/syzbot-ai-patches for information about AI-generated patches.
You can comment on the patch as usual, syzbot will try to address
the comments and send a new version of the patch if necessary.
syzbot engineers can be reached at [email protected].