mirror of
https://github.com/torvalds/linux.git
synced 2026-11-20 13:03:48 +00:00
amt_update_handler() refreshes a relay tunnel's garbage-collection timer with mod_delayed_work() on a tunnel found by an RCU walk. A concurrent amt_tunnel_expire() can unlink and free that tunnel while the Update path still has the cached pointer, leaving gc_wq armed in freed memory. A tunnel can also be freed twice. amt_dev_stop() unlinks and frees tunnels without amt->lock, while amt_tunnel_expire() unlinks and frees them under it, so an ifdown that races a tunnel timeout can run list_del_rcu() and kfree_rcu() on the same tunnel twice. Group and source nodes have lifetime bugs of their own: - __amt_source_gc_work() frees a source, and amt_del_group() a group, after only cancel_delayed_work(), which does not wait for a timer work that is already running. amt_source_work() or amt_group_work() can then run on the freed node. - amt_source_work() reads snode->gnode and gnode->tunnel_list, and amt_group_work() gnode->tunnel_list, before taking tunnel->lock or the RCU read lock, so either can follow a pointer to a group or tunnel that has already been freed. - A timer work that waits for tunnel->lock while its node is deleted can then act on the deleted node: amt_source_work() destroys the source again, or amt_group_work() deletes the group again. Either decrements nr_sources or nr_groups a second time and unlinks, queues or frees the node a second time. Make amt_tunnel_expire() the only timeout-path freer. The Update path records last_seen under tunnel->lock, and expire reads it under tunnel->lock and, in the same section, either re-arms gc_wq from it or marks the tunnel dead for freeing, so an Update either keeps the tunnel alive or finds it dead. Protect the dead state with tunnel->lock so report handling cannot add multicast state after teardown begins. Device stop marks tunnels dead under amt->lock, unlinks them one at a time under it, synchronously cancels their work, and frees them after lockless readers have passed. Apply the same lifetime rules to group and source timer work: mark nodes dead under tunnel->lock, keep RCU held while workers resolve their parents, clear the parent pointers before a node is queued for freeing, defer node freeing, and synchronously cancel each node's timer before freeing that node. Park sources and groups through a gc_node of their own. A destroyed source used to reuse snode->node, its link into the group's hash; no lockless reader is known to follow it onto the GC list, so this is hardening. Run the deferred GC directly before freeing a tunnel so nodes are reclaimed promptly. Do not flush the GC work instead: flush_delayed_work() queues the work and waits for it to run, which needs a free amt_wq active slot, so amt_tunnel_expire() and the group GC, both amt_wq work, can deadlock once every slot is held by such a waiter. The cancel_delayed_work_sync() calls the GC makes need no slot: each takes a pending timer work off its timer or queue, or waits for a running one on a barrier that max_active does not count. Lifetime does not rest on that GC run, which misses nodes a concurrent run has already spliced off: it rests on kfree_rcu() and on the parent pointers being cleared, and the dead checks made, under tunnel->lock. Release the group timer's device reference on every exit. A node on a GC list is no longer charged to max_groups or max_sources, and unless a tunnel expires or the device stops, only the periodic GC drains the lists, every 30 seconds. Sources were already parked that way; groups, which went to kfree_rcu() before, now are too, so Report/Leave churn from one gateway could pin memory without bound in between. Count the nodes parked on both lists, under the one lock that now protects both, and when the count reaches AMT_GC_KICK (2048) queue the drain of both lists at once. A parked group still holds its AMT_HSIZE (256) source hash heads, so it is a kmalloc-4k object, and 2048 of them is about 8 MiB; the kick keeps the backlog near that instead of letting it grow for up to 30 seconds. A periodic drain that ends with that many parked again runs again at once rather than after the interval. This also bounds what amt_dev_stop() has to free in its drain under RTNL, and both drains now call cond_resched() after each node. A Request-only tunnel retains the original GMI timeout. Its last_seen stays 0, a value an Update never stores, and only a valid, authenticated Membership Update refreshes last_seen and gives the tunnel the longer lifetime: an Update whose encapsulated message fails ip_mc_check_igmp() or ipv6_mc_check_mld() no longer extends the tunnel. amt_tunnel_expire() reads jiffies once, so the re-arm delay comes from the value it compared. The Sashiko AI review of an earlier posting of this series pointed out the gc_wq re-arm, and reading amt_update_handler() against amt_tunnel_expire() and amt_dev_stop() confirms it. It is theoretical: it needs a Membership Update that has already found the tunnel under RCU to arrive while amt_tunnel_expire() is freeing that tunnel. Review of later postings raised the races in the group and source timer work, the amt_dev_stop() double free and the unbounded GC lists. I have not triggered any of them, so there is no trace to include. Fixes:cbc21dc1cf("amt: add data plane of amt interface") Fixes:bc54e49c14("amt: add multicast(IGMP) report message handler") Signed-off-by: Omar Ramadan <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>