From: Yu Gao LAPB calls the data_transmit() callback with lapb->lock held. lapbeth passes the frame to dev_queue_xmit(), and if the underlying device is a tunnel routed back through a lapbeth device, the frame re-enters lapbeth_xmit() and LAPB under the TX lock and up_lock. syzbot reports: lapb_list_lock --> &lapb->lock --> _xmit_X25#2 lapb_list_lock --> _xmit_X25#2 --> &lapbeth->up_lock Queue frames on a per-instance FIFO under lapb->lock and pass them to the driver after the lock is dropped. One caller drains the FIFO at a time, which keeps the frames in order. hdlc_x25 hands frames straight to the hardware, so it sends them right after the lock is dropped. lapbeth's data_transmit() can loop back into LAPB through dev_queue_xmit(), so it sets the new schedule_tx() callback and sends them from its NAPI poll instead. The FIFO holds at most 256 frames; more are dropped and left to LAPB retransmission. A drain sends at most 64 frames, then lapbeth's NAPI poll stays scheduled and hdlc_x25 defers the rest to a work item. Each frame records the TX recursion level at which it was queued, and that level is restored around data_transmit(), so a looped frame still counts against XMIT_RECURSION_LIMIT. On NETDEV_GOING_DOWN, LAPB flushes the queued frames, including the final DISC, and waits for a running sender before ndo_stop. Frames produced after that are dropped. With this series, the syzbot reproducers no longer report lock cycles. hdlc_x25 was only build-tested. Fixes: b491e6a7391e ("net: lapb: Add locking to the lapb module") Suggested-by: Willem de Bruijn Reported-by: syzbot+059a2795ad4cff2a2695@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=059a2795ad4cff2a2695 Reported-by: syzbot+8328573e4d0514392471@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=8328573e4d0514392471 Signed-off-by: Yu Gao --- Documentation/networking/lapb-module.rst | 31 ++++++++ drivers/net/wan/lapbether.c | 14 ++++ include/linux/lapb.h | 8 ++ include/net/lapb.h | 13 ++++ net/lapb/lapb_iface.c | 123 +++++++++++++++++++++++++++---- net/lapb/lapb_timer.c | 4 +- 6 files changed, 178 insertions(+), 15 deletions(-) diff --git a/Documentation/networking/lapb-module.rst b/Documentation/networking/lapb-module.rst index ff586bc..f2047e3 100644 --- a/Documentation/networking/lapb-module.rst +++ b/Documentation/networking/lapb-module.rst @@ -47,6 +47,7 @@ of the LAPB module:: void (*disconnect_indication)(int token, int reason); int (*data_indication)(int token, struct sk_buff *skb); void (*data_transmit)(int token, struct sk_buff *skb); + void (*schedule_tx)(int token); }; Each member of this structure corresponds to a function in the device driver @@ -224,6 +225,16 @@ LAPB_OK LAPB has accepted the data. LAPB_BADTOKEN Invalid/unknown LAPB token. ============= =========================== +:: + + bool lapb_transmit_pending(void *token); + +Pass queued frames to the device driver through data_transmit (see below). +Only drivers that provide schedule_tx need to call this. Each call sends at +most 64 frames and returns true if frames are left, in which case the driver +should call it again later. It must not be called with a lock held that the +driver's transmit path also takes. + Callbacks --------- @@ -303,3 +314,23 @@ This is called by the LAPB module when data is to be transmitted to the remote system by the device driver. The skbuff becomes the property of the device driver and the LAPB module will not perform any more actions on it. The skb->data pointer will be pointing to the first byte of the LAPB header. + +LAPB queues outgoing frames while it holds its internal lock and calls +data_transmit only after dropping it, so the frame may be passed to the +driver some time after the event that produced it. At most 256 frames +wait; further ones are dropped and left to the LAPB retransmission. When a +device in the initial network namespace is going down, the queued frames +are passed to the driver and frames produced after that are dropped. + +:: + + void (*schedule_tx)(void *token); + +Optional. A driver whose data_transmit can re-enter the network stack or the +LAPB module, for example by calling dev_queue_xmit(), provides this +callback. LAPB then calls schedule_tx instead of data_transmit from the +context of the LAPB call, and the driver calls lapb_transmit_pending (see +above) later from a context that holds none of its transmit locks. +schedule_tx is called with the LAPB lock held and must not call back into +the LAPB module. Without it, the caller sends the frames right after LAPB +drops its lock, and if many are waiting, the rest are sent from a work item. diff --git a/drivers/net/wan/lapbether.c b/drivers/net/wan/lapbether.c index ab71afa..a438b85 100644 --- a/drivers/net/wan/lapbether.c +++ b/drivers/net/wan/lapbether.c @@ -93,6 +93,10 @@ static int lapbeth_napi_poll(struct napi_struct *napi, int budget) napi); struct sk_buff *skb; int processed = 0; + bool tx_more; + + /* LAPB limits each call, so TX is not charged to the RX budget. */ + tx_more = lapb_transmit_pending(lapbeth->axdev); for (; processed < budget; ++processed) { skb = skb_dequeue(&lapbeth->rx_queue); @@ -101,6 +105,8 @@ static int lapbeth_napi_poll(struct napi_struct *napi, int budget) netif_receive_skb_core(skb); } + if (tx_more) + return budget; if (processed < budget) napi_complete(napi); @@ -317,6 +323,13 @@ static int lapbeth_set_mac_address(struct net_device *dev, void *addr) return 0; } +static void lapbeth_schedule_tx(struct net_device *dev) +{ + struct lapbethdev *lapbeth = netdev_priv(dev); + + napi_schedule(&lapbeth->napi); +} + static const struct lapb_register_struct lapbeth_callbacks = { .connect_confirmation = lapbeth_connected, .connect_indication = lapbeth_connected, @@ -324,6 +337,7 @@ static const struct lapb_register_struct lapbeth_callbacks = { .disconnect_indication = lapbeth_disconnected, .data_indication = lapbeth_data_indication, .data_transmit = lapbeth_data_transmit, + .schedule_tx = lapbeth_schedule_tx, }; /* open/close a device diff --git a/include/linux/lapb.h b/include/linux/lapb.h index b5333f9..5d793ea 100644 --- a/include/linux/lapb.h +++ b/include/linux/lapb.h @@ -36,6 +36,13 @@ struct lapb_register_struct { void (*disconnect_indication)(struct net_device *dev, int reason); int (*data_indication)(struct net_device *dev, struct sk_buff *skb); void (*data_transmit)(struct net_device *dev, struct sk_buff *skb); + /* Set this if data_transmit() can re-enter the network stack or LAPB. + * LAPB then calls it instead of sending from the caller's context, and + * the driver must call lapb_transmit_pending() later, without holding + * any lock its transmit path takes. It is called with the LAPB lock + * held and must not call back into LAPB. + */ + void (*schedule_tx)(struct net_device *dev); }; struct lapb_parms_struct { @@ -57,6 +64,7 @@ extern int lapb_getparms(struct net_device *dev, struct lapb_parms_struct *parms extern int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms); extern int lapb_connect_request(struct net_device *dev); extern int lapb_disconnect_request(struct net_device *dev); +bool lapb_transmit_pending(struct net_device *dev); extern int lapb_data_request(struct net_device *dev, struct sk_buff *skb); extern int lapb_data_received(struct net_device *dev, struct sk_buff *skb); diff --git a/include/net/lapb.h b/include/net/lapb.h index 6c07420..ce45cf3 100644 --- a/include/net/lapb.h +++ b/include/net/lapb.h @@ -3,6 +3,8 @@ #define _LAPB_H #include #include +#include +#include #define LAPB_HEADER_LEN MAX_HEADER /* LAPB over Ethernet + a bit more */ @@ -66,6 +68,11 @@ enum { #define LAPB_SMODULUS 8 #define LAPB_EMODULUS 128 +/* About two full extended windows. */ +#define LAPB_TX_QUEUE_MAX 256 +/* Frames sent per drain before the rest is deferred. */ +#define LAPB_TX_QUOTA 64 + /* * Information about the current frame. */ @@ -97,6 +104,11 @@ struct lapb_cb { /* Internal control information */ struct sk_buff_head write_queue; struct sk_buff_head ack_queue; + struct sk_buff_head tx_queue; + wait_queue_head_t tx_wait; + struct work_struct tx_work; + bool tx_running; + bool tx_stopped; unsigned char window; const struct lapb_register_struct *callbacks; @@ -109,6 +121,7 @@ struct lapb_cb { }; /* lapb_iface.c */ +void lapb_tx_unlock(struct lapb_cb *lapb) __releases(&lapb->lock); void lapb_connect_confirmation(struct lapb_cb *lapb, int); void lapb_connect_indication(struct lapb_cb *lapb, int); void lapb_disconnect_confirmation(struct lapb_cb *lapb, int); diff --git a/net/lapb/lapb_iface.c b/net/lapb/lapb_iface.c index 1af36fd..a04fd11 100644 --- a/net/lapb/lapb_iface.c +++ b/net/lapb/lapb_iface.c @@ -105,6 +105,16 @@ static struct lapb_cb *lapb_devtostruct(struct net_device *dev) return rc; } + +/* Sends what an inline drain left behind when it ran out of quota. */ +static void lapb_tx_work(struct work_struct *work) +{ + struct lapb_cb *lapb = container_of(work, struct lapb_cb, tx_work); + + spin_lock_bh(&lapb->lock); + lapb_tx_unlock(lapb); +} + /* * Create an empty LAPB control block. */ @@ -117,6 +127,9 @@ static struct lapb_cb *lapb_create_cb(void) skb_queue_head_init(&lapb->write_queue); skb_queue_head_init(&lapb->ack_queue); + __skb_queue_head_init(&lapb->tx_queue); + init_waitqueue_head(&lapb->tx_wait); + INIT_WORK(&lapb->tx_work, lapb_tx_work); timer_setup(&lapb->t1timer, NULL, 0); timer_setup(&lapb->t2timer, NULL, 0); @@ -198,7 +211,9 @@ int lapb_unregister(struct net_device *dev) /* Wait for running timers to stop */ timer_delete_sync(&lapb->t1timer); timer_delete_sync(&lapb->t2timer); + disable_work_sync(&lapb->tx_work); + __skb_queue_purge(&lapb->tx_queue); lapb_put(lapb); return LAPB_OK; out: @@ -282,6 +297,71 @@ int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms) } EXPORT_SYMBOL(lapb_setparms); +/* Preserve the TX nesting level if another caller sends the frame. */ +#define LAPB_TX_DEPTH(skb) ((skb)->cb[0]) + +/* One caller drains the FIFO. Other producers leave their frames to it. + * Returns true if the quota ran out with frames still queued; the caller + * then has to schedule another drain. Once tx_stopped is set no frames + * are added, so the final flush ignores the quota. + */ +static bool lapb_xmit_queued(struct lapb_cb *lapb) +{ + int quota = LAPB_TX_QUOTA; + struct sk_buff *skb; + + lockdep_assert_held(&lapb->lock); + if (lapb->tx_running) + return false; + + WRITE_ONCE(lapb->tx_running, true); + while ((quota-- > 0 || lapb->tx_stopped) && + (skb = __skb_dequeue(&lapb->tx_queue))) { + int depth = LAPB_TX_DEPTH(skb) - dev_recursion_level(); + int i; + + spin_unlock(&lapb->lock); + for (i = 0; i < depth; i++) + dev_xmit_recursion_inc(); + lapb->callbacks->data_transmit(lapb->dev, skb); + for (i = 0; i < depth; i++) + dev_xmit_recursion_dec(); + spin_lock(&lapb->lock); + } + WRITE_ONCE(lapb->tx_running, false); + if (lapb->tx_stopped) + wake_up_all(&lapb->tx_wait); + return !skb_queue_empty(&lapb->tx_queue); +} + +/* Returns true if frames are left for the next call. */ +bool lapb_transmit_pending(struct net_device *dev) +{ + struct lapb_cb *lapb = lapb_devtostruct(dev); + bool more; + + if (!lapb) + return false; + spin_lock_bh(&lapb->lock); + more = lapb_xmit_queued(lapb); + spin_unlock_bh(&lapb->lock); + lapb_put(lapb); + return more; +} +EXPORT_SYMBOL(lapb_transmit_pending); + +void lapb_tx_unlock(struct lapb_cb *lapb) + __releases(&lapb->lock) +{ + if (!skb_queue_empty(&lapb->tx_queue)) { + if (lapb->callbacks->schedule_tx) + lapb->callbacks->schedule_tx(lapb->dev); + else if (lapb_xmit_queued(lapb)) + schedule_work(&lapb->tx_work); + } + spin_unlock_bh(&lapb->lock); +} + int lapb_connect_request(struct net_device *dev) { struct lapb_cb *lapb = lapb_devtostruct(dev); @@ -307,7 +387,7 @@ int lapb_connect_request(struct net_device *dev) rc = LAPB_OK; out_put: - spin_unlock_bh(&lapb->lock); + lapb_tx_unlock(lapb); lapb_put(lapb); out: return rc; @@ -357,7 +437,7 @@ int lapb_disconnect_request(struct net_device *dev) rc = __lapb_disconnect_request(lapb); - spin_unlock_bh(&lapb->lock); + lapb_tx_unlock(lapb); lapb_put(lapb); out: return rc; @@ -378,11 +458,12 @@ int lapb_data_request(struct net_device *dev, struct sk_buff *skb) if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4) goto out_put; + LAPB_TX_DEPTH(skb) = dev_recursion_level(); skb_queue_tail(&lapb->write_queue, skb); lapb_kick(lapb); rc = LAPB_OK; out_put: - spin_unlock_bh(&lapb->lock); + lapb_tx_unlock(lapb); lapb_put(lapb); out: return rc; @@ -397,7 +478,7 @@ int lapb_data_received(struct net_device *dev, struct sk_buff *skb) if (lapb) { spin_lock_bh(&lapb->lock); lapb_data_input(lapb, skb); - spin_unlock_bh(&lapb->lock); + lapb_tx_unlock(lapb); lapb_put(lapb); rc = LAPB_OK; } @@ -441,14 +522,15 @@ int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb) int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb) { - int used = 0; - - if (lapb->callbacks->data_transmit) { - lapb->callbacks->data_transmit(lapb->dev, skb); - used = 1; - } - - return used; + lockdep_assert_held(&lapb->lock); + if (lapb->tx_stopped || !lapb->callbacks->data_transmit || + skb_queue_len(&lapb->tx_queue) >= LAPB_TX_QUEUE_MAX) + return 0; + + LAPB_TX_DEPTH(skb) = max_t(int, LAPB_TX_DEPTH(skb), + dev_recursion_level()); + __skb_queue_tail(&lapb->tx_queue, skb); + return 1; } /* Handle device status changes. */ @@ -457,6 +539,7 @@ static int lapb_device_event(struct notifier_block *this, unsigned long event, { struct net_device *dev = netdev_notifier_info_to_dev(ptr); struct lapb_cb *lapb; + bool quiesce = false; if (!net_eq(dev_net(dev), &init_net)) return NOTIFY_DONE; @@ -488,8 +571,16 @@ static int lapb_device_event(struct notifier_block *this, unsigned long event, } break; case NETDEV_GOING_DOWN: + /* Make room for the DISC; the link is closing anyway. */ + if (skb_queue_len(&lapb->tx_queue) >= LAPB_TX_QUEUE_MAX) + kfree_skb(__skb_dequeue(&lapb->tx_queue)); if (netif_carrier_ok(dev)) __lapb_disconnect_request(lapb); + /* Hand the final frames to the driver before it closes. + * ndo_stop unregisters this block, so this is not undone. + */ + lapb->tx_stopped = true; + quiesce = true; break; case NETDEV_DOWN: lapb_dbg(0, "(%p) Interface down: %s\n", dev, dev->name); @@ -524,7 +615,13 @@ static int lapb_device_event(struct notifier_block *this, unsigned long event, break; } - spin_unlock_bh(&lapb->lock); + if (quiesce) { + lapb_xmit_queued(lapb); + spin_unlock_bh(&lapb->lock); + wait_event(lapb->tx_wait, !READ_ONCE(lapb->tx_running)); + } else { + lapb_tx_unlock(lapb); + } lapb_put(lapb); return NOTIFY_DONE; } diff --git a/net/lapb/lapb_timer.c b/net/lapb/lapb_timer.c index 9fde6cf..6acfdd7 100644 --- a/net/lapb/lapb_timer.c +++ b/net/lapb/lapb_timer.c @@ -89,7 +89,7 @@ static void lapb_t2timer_expiry(struct timer_list *t) lapb->t2timer_running = false; out: - spin_unlock_bh(&lapb->lock); + lapb_tx_unlock(lapb); } static void lapb_t1timer_expiry(struct timer_list *t) @@ -202,5 +202,5 @@ static void lapb_t1timer_expiry(struct timer_list *t) lapb_start_t1timer(lapb); out: - spin_unlock_bh(&lapb->lock); + lapb_tx_unlock(lapb); } -- Git-157)