batman-adv: fix rare race conditions on interface removal
PATCH v4 1/1 batman-adv: fix rare race conditions on interface removal
In rare cases during shutdown the following general protection fault can happen:
general protection fault: 0000 [#1] SMP Modules linked in: batman_adv(O-) [...] CPU: 3 PID: 1714 Comm: rmmod Tainted: G O 4.6.0-rc6+ #1 [...] Call Trace: [<ffffffffa0363294>] batadv_hardif_disable_interface+0x29a/0x3a6 [batman_adv] [<ffffffffa0373db4>] batadv_softif_destroy_netlink+0x4b/0xa4 [batman_adv] [<ffffffff813b52f3>] __rtnl_link_unregister+0x48/0x92 [<ffffffff813b9240>] rtnl_link_unregister+0xc1/0xdb [<ffffffff8108547c>] ? bit_waitqueue+0x87/0x87 [<ffffffffa03850d2>] batadv_exit+0x1a/0xf48 [batman_adv] [<ffffffff810c26f9>] SyS_delete_module+0x136/0x1b0 [<ffffffff8144dc65>] entry_SYSCALL_64_fastpath+0x18/0xa8 [<ffffffff8108aaca>] ? trace_hardirqs_off_caller+0x37/0xa6 Code: 89 f7 e8 21 bd 0d e1 4d 85 e4 75 0e 31 f6 48 c7 c7 50 d7 3b a0 e8 50 16 f2 e0 49 8b 9c 24 28 01 00 00 48 85 db 0f 84 b2 00 00 00 <48> 8b 03 4d 85 ed 48 89 45 c8 74 09 4c 39 ab f8 00 00 00 75 1c RIP [<ffffffffa0371852>] batadv_purge_outstanding_packets+0x1c8/0x291 [batman_adv] RSP <ffff88001da5fd78> ---[ end trace 803b9bdc6a4a952b ]--- Kernel panic - not syncing: Fatal exception in interrupt Kernel Offset: disabled ---[ end Kernel panic - not syncing: Fatal exception in interrupt
It does not happen often, but may potentially happen when frequently shutting down and reinitializing an interface. With some carefully placed msleep()s/mdelay()s it can be reproduced easily.
The issue is, that on interface removal, any still running worker thread of a forwarding packet will race with the interface purging routine to free a forwarding packet. Temporarily giving up a spin-lock to be able to sleep in the purging routine is not safe.
Furthermore, there is a potential general protection fault not just for the purging side shown above, but also on the worker side: Temporarily removing a forw_packet from the according forw_{bcast,bat}_list will make it impossible for the purging routine to catch and cancel it.
# How this patch tries to fix it:
With this patch we split the queue purging into three steps: Step 1), removing forward packets from the queue of an interface and by that claim it as our responsibility to free.
Step 2), we are either lucky to cancel a pending worker before it starts to run. Or if it is already running, we wait and let it do its thing, except two things:
Through the claiming in step 1) we prevent workers from a) re-arming themselves. And b) prevent workers from freeing packets which we still hold in the interface purging routine.
Finally, step 3, we are sure that no forwarding packets are pending or even running anymore on the interface to remove. We can then safely free the claimed forwarding packets.
---
Changes in v4:
* rebased to master
* tried to better explain the severity in the commit message
* trimmed call trace in commit message
* made code comments and the WARN_ONCE less funny, but more informative
* reworded bm_list to cleanup_list
* shortened batadv_forw_packet_steal() declaration in send.h to avoid a faulty checkpatch warning.
* removed unnecessary hlist_add_head() in batadv_forw_packet_steal()
* changed hlist_del() to hlist_del_init() in batadv_forw_packet_steal() -> to be able to reach WARN_ONCE instead of crashing upon calling
hlist_del()
Changes in v3:
* added compat code for hlist_fake()
* added kerneldoc for @send_time for batadv_forw_packet_queue()
* added "#include <linux/bug.h>" for WARN_ONCE() (thanks Sven!)
Changes in v2:
* Merged the following two commits:
- "batman-adv: Fix a potential bcast/ogm queue purging race condition (1)")
- "batman-adv: Fix a potential bcast/ogm queue purging race condition (2)")
- "batman-adv: Fix a potential bcast/ogm queue purging race condition (3)")
* Updated commit message
- e.g. added kernel oops
* renamed:
- forw_packet->canceled_list -> forw_packet->bm_list
- batadv_cancel_packets() -> batadv_forw_packet_list_steal()
- batadv_canceled_packets_free() -> batadv_forw_packet_list_free() => overall, tried to keep it more API-like for easier validation
* rebased to master
-> batadv_forw_packet_list_cancel() takes if_outgoing into account,
too
* added kerneldoc for forw_packet->bm_list in types.h
* replaced an INIT_HLIST_HEAD() call with direct
"head = HLIST_INIT_HEAD" assignment
PS: checkpatch throws the following at me, but seems to be bogus?
--- compat-include/linux/list.h | 13 +- net/batman-adv/bat_iv_ogm.c | 19 +-- net/batman-adv/send.c | 287 +++++++++++++++++++++++++++++++++----------- net/batman-adv/send.h | 5 + net/batman-adv/types.h | 2 + 5 files changed, 240 insertions(+), 86 deletions(-)
compat-include/linux/list.h +11 -2
@@ -43,12 +43,21 @@ pos && ({ n = pos->member.next;1 ; }); \ pos = hlist_entry_safe(n, typeof(*pos), member)) -#endif +#endif /* < KERNEL_VERSION(3, 9, 0) */ #if LINUX_VERSION_CODE <KERNEL_VERSION (3 ,17 ,0 )#define hlist_add_behind (n, prev) hlist_add_after(prev, n) -#endif +#endif /* < KERNEL_VERSION(3, 17, 0) */ + +#if LINUX_VERSION_CODE <KERNEL_VERSION (4 ,3 ,0 ) + +static inline bool hlist_fake (struct hlist_node *h) +{ +return h->pprev == &h->next; +} + +#endif /* < KERNEL_VERSION(4, 3, 0) */ #endif /* _NET_BATMAN_ADV_COMPAT_LINUX_LIST_H_ */
net/batman-adv/bat_iv_ogm.c +6 -13
@@ -717,17 +717,10 @@ static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,if (direct_link) forw_packet_aggr->direct_link_flags |=1 ; -/* add new packet to packet list */ -spin_lock_bh (&bat_priv->forw_bat_list_lock); -hlist_add_head (&forw_packet_aggr->list, &bat_priv->forw_bat_list); -spin_unlock_bh (&bat_priv->forw_bat_list_lock); - -/* start timer for this packet */ INIT_DELAYED_WORK (&forw_packet_aggr->delayed_work, batadv_iv_send_outstanding_bat_ogm_packet); -queue_delayed_work (batadv_event_workqueue, - &forw_packet_aggr->delayed_work, - send_time - jiffies); + +batadv_forw_packet_ogmv1_queue (bat_priv, forw_packet_aggr, send_time); }/* aggregate a new packet into the existing ogm packet */
@@ -1789,9 +1782,6 @@ static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work) forw_packet = container_of(delayed_work, struct batadv_forw_packet, delayed_work); bat_priv =netdev_priv (forw_packet->if_incoming->soft_iface); -spin_lock_bh (&bat_priv->forw_bat_list_lock); -hlist_del (&forw_packet->list); -spin_unlock_bh (&bat_priv->forw_bat_list_lock);if (atomic_read (&bat_priv->mesh_state) ==BATADV_MESH_DEACTIVATING ) { dropped = true;
@@ -1813,7 +1803,10 @@ static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)batadv_iv_ogm_schedule (forw_packet->if_incoming); out: -batadv_forw_packet_free (forw_packet, dropped); +/* do we get something for free()? */ +if (batadv_forw_packet_steal (forw_packet, + &bat_priv->forw_bat_list_lock)) +batadv_forw_packet_free (forw_packet, dropped); } static int batadv_iv_ogm_receive(struct sk_buff *skb,
net/batman-adv/send.c +216 -71
349 lines not on this page · raw message
net/batman-adv/send.h +5 -0
@@ -21,6 +21,7 @@#include "main.h" #include <linux/compiler.h> +#include <linux/spinlock.h> #include <linux/types.h> #include "packet.h"
@@ -34,6 +35,10 @@ batadv_forw_packet_alloc(struct batadv_hard_iface *if_incoming,struct batadv_hard_iface *if_outgoing, atomic_t *queue_left, struct batadv_priv *bat_priv); +bool batadv_forw_packet_steal (struct batadv_forw_packet *packet,spinlock_t *l); +void batadv_forw_packet_ogmv1_queue (struct batadv_priv *bat_priv, +struct batadv_forw_packet *forw_packet, +unsigned long send_time); int batadv_send_skb_to_orig(struct sk_buff *skb, struct batadv_orig_node *orig_node,
net/batman-adv/types.h +2 -0
@@ -1385,6 +1385,7 @@ struct batadv_skb_cb {/** * struct batadv_forw_packet - structure for bcast packets to be sent/forwarded * @list: list node for batadv_priv::forw_{bat,bcast}_list +* @cleanup_list: list node for purging functions * @send_time: execution time for delayed_work (packet sending) * @own: bool for locally generated packets (local OGMs are re-scheduled after * sending)
@@ -1401,6 +1402,7 @@ struct batadv_skb_cb {*/ struct batadv_forw_packet {struct hlist_node list; +struct hlist_node cleanup_list;unsigned long send_time;u8 own;struct sk_buff *skb;
Signature · 1 line
2.1.4
On Dienstag, 1. November 2016 09:44:44 CET Linus Lüssing wrote:
[...]
Quoted 13 lines
---
Changes in v4:
* rebased to master
* tried to better explain the severity in the commit message
* trimmed call trace in commit message
* made code comments and theWARN_ONCEless funny, but more informative
* rewordedbm_listtocleanup_list
* shortenedbatadv_forw_packet_steal()declaration insend.hto avoid a faulty checkpatch warning.
* removed unnecessaryhlist_add_head()inbatadv_forw_packet_steal()
* changedhlist_del()tohlist_del_init()inbatadv_forw_packet_steal()-> to be able to reachWARN_ONCEinstead of crashing upon calling
Quoted 1 line
hlist_del()
Thanks for all the changes. Lets see if we finally got this one (please no regression, please no regressions, ...) :)
Applied in 8a436b94d731873bd3d771aa146956420fbec458 [1].
Thanks,
Sven
[1] https://git.open-mesh.org/batman-adv.git/commit/8a436b94d731873bd3d771aa146956420fbec458