|
| 1 | +From: Jiexun Wang <wangjiexun2025@gmail.com> |
| 2 | +Date: Mon, 27 Apr 2026 14:43:34 +0800 |
| 3 | +Subject: batman-adv: stop tp_meter sessions during mesh teardown |
| 4 | + |
| 5 | +TP meter sessions remain linked on bat_priv->tp_list after the netlink |
| 6 | +request has already finished. When the mesh interface is removed, |
| 7 | +batadv_mesh_free() currently tears down the mesh without first draining |
| 8 | +these sessions. |
| 9 | + |
| 10 | +A running sender thread or a late incoming tp_meter packet can then keep |
| 11 | +processing against a mesh instance which is already shutting down. |
| 12 | +Synchronize tp_meter with the mesh lifetime by stopping all active |
| 13 | +sessions from batadv_mesh_free() and waiting for sender threads to exit |
| 14 | +before teardown continues. |
| 15 | + |
| 16 | +Fixes: 98d7a766b645 ("batman-adv: throughput meter implementation") |
| 17 | +Reported-by: Yuan Tan <yuantan098@gmail.com> |
| 18 | +Reported-by: Yifan Wu <yifanwucs@gmail.com> |
| 19 | +Reported-by: Juefei Pu <tomapufckgml@gmail.com> |
| 20 | +Reported-by: Xin Liu <bird@lzu.edu.cn> |
| 21 | +Co-developed-by: Luxing Yin <tr0jan@lzu.edu.cn> |
| 22 | +Signed-off-by: Luxing Yin <tr0jan@lzu.edu.cn> |
| 23 | +Signed-off-by: Jiexun Wang <wangjiexun2025@gmail.com> |
| 24 | +Signed-off-by: Ren Wei <n05ec@lzu.edu.cn> |
| 25 | +Signed-off-by: Sven Eckelmann <sven@narfation.org> |
| 26 | +Origin: upstream, https://git.open-mesh.org/batman-adv.git/commit/?id=13f49ae8972583ae9142b548a2789ade7dd1411f |
| 27 | + |
| 28 | +--- a/compat-include/linux/timer.h |
| 29 | ++++ b/compat-include/linux/timer.h |
| 30 | +@@ -18,6 +18,11 @@ |
| 31 | + #define timer_delete_sync(_timer) del_timer_sync(_timer) |
| 32 | + #define timer_delete(_timer) del_timer(_timer) |
| 33 | + |
| 34 | ++static inline int timer_shutdown_sync(struct timer_list *timer) |
| 35 | ++{ |
| 36 | ++ return del_timer_sync(timer); |
| 37 | ++} |
| 38 | ++ |
| 39 | + #endif /* LINUX_VERSION_IS_LESS(6, 2, 0) */ |
| 40 | + |
| 41 | + #if LINUX_VERSION_IS_LESS(6, 16, 0) |
| 42 | +--- a/net/batman-adv/main.c |
| 43 | ++++ b/net/batman-adv/main.c |
| 44 | +@@ -249,6 +249,7 @@ void batadv_mesh_free(struct net_device |
| 45 | + atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING); |
| 46 | + |
| 47 | + batadv_purge_outstanding_packets(bat_priv, NULL); |
| 48 | ++ batadv_tp_stop_all(bat_priv); |
| 49 | + |
| 50 | + batadv_gw_node_free(bat_priv); |
| 51 | + |
| 52 | +--- a/net/batman-adv/tp_meter.c |
| 53 | ++++ b/net/batman-adv/tp_meter.c |
| 54 | +@@ -365,23 +365,38 @@ static void batadv_tp_vars_put(struct ba |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | +- * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer |
| 59 | +- * @bat_priv: the bat priv with all the mesh interface information |
| 60 | +- * @tp_vars: the private data of the current TP meter session to cleanup |
| 61 | ++ * batadv_tp_list_detach() - remove tp session from mesh session list once |
| 62 | ++ * @tp_vars: the private data of the current TP meter session |
| 63 | + */ |
| 64 | +-static void batadv_tp_sender_cleanup(struct batadv_priv *bat_priv, |
| 65 | +- struct batadv_tp_vars *tp_vars) |
| 66 | ++static void batadv_tp_list_detach(struct batadv_tp_vars *tp_vars) |
| 67 | + { |
| 68 | +- cancel_delayed_work(&tp_vars->finish_work); |
| 69 | ++ bool detached = false; |
| 70 | + |
| 71 | + spin_lock_bh(&tp_vars->bat_priv->tp_list_lock); |
| 72 | +- hlist_del_rcu(&tp_vars->list); |
| 73 | ++ if (!hlist_unhashed(&tp_vars->list)) { |
| 74 | ++ hlist_del_init_rcu(&tp_vars->list); |
| 75 | ++ detached = true; |
| 76 | ++ } |
| 77 | + spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock); |
| 78 | + |
| 79 | ++ if (!detached) |
| 80 | ++ return; |
| 81 | ++ |
| 82 | ++ atomic_dec(&tp_vars->bat_priv->tp_num); |
| 83 | ++ |
| 84 | + /* drop list reference */ |
| 85 | + batadv_tp_vars_put(tp_vars); |
| 86 | ++} |
| 87 | + |
| 88 | +- atomic_dec(&tp_vars->bat_priv->tp_num); |
| 89 | ++/** |
| 90 | ++ * batadv_tp_sender_cleanup() - cleanup sender data and drop and timer |
| 91 | ++ * @tp_vars: the private data of the current TP meter session to cleanup |
| 92 | ++ */ |
| 93 | ++static void batadv_tp_sender_cleanup(struct batadv_tp_vars *tp_vars) |
| 94 | ++{ |
| 95 | ++ cancel_delayed_work_sync(&tp_vars->finish_work); |
| 96 | ++ |
| 97 | ++ batadv_tp_list_detach(tp_vars); |
| 98 | + |
| 99 | + /* kill the timer and remove its reference */ |
| 100 | + timer_delete_sync(&tp_vars->timer); |
| 101 | +@@ -886,7 +901,8 @@ out: |
| 102 | + batadv_orig_node_put(orig_node); |
| 103 | + |
| 104 | + batadv_tp_sender_end(bat_priv, tp_vars); |
| 105 | +- batadv_tp_sender_cleanup(bat_priv, tp_vars); |
| 106 | ++ batadv_tp_sender_cleanup(tp_vars); |
| 107 | ++ complete(&tp_vars->finished); |
| 108 | + |
| 109 | + batadv_tp_vars_put(tp_vars); |
| 110 | + |
| 111 | +@@ -918,7 +934,8 @@ static void batadv_tp_start_kthread(stru |
| 112 | + batadv_tp_vars_put(tp_vars); |
| 113 | + |
| 114 | + /* cleanup of failed tp meter variables */ |
| 115 | +- batadv_tp_sender_cleanup(bat_priv, tp_vars); |
| 116 | ++ batadv_tp_sender_cleanup(tp_vars); |
| 117 | ++ complete(&tp_vars->finished); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | +@@ -1024,6 +1041,7 @@ void batadv_tp_start(struct batadv_priv |
| 122 | + tp_vars->start_time = jiffies; |
| 123 | + |
| 124 | + init_waitqueue_head(&tp_vars->more_bytes); |
| 125 | ++ init_completion(&tp_vars->finished); |
| 126 | + |
| 127 | + spin_lock_init(&tp_vars->unacked_lock); |
| 128 | + INIT_LIST_HEAD(&tp_vars->unacked_list); |
| 129 | +@@ -1126,14 +1144,7 @@ static void batadv_tp_receiver_shutdown( |
| 130 | + "Shutting down for inactivity (more than %dms) from %pM\n", |
| 131 | + BATADV_TP_RECV_TIMEOUT, tp_vars->other_end); |
| 132 | + |
| 133 | +- spin_lock_bh(&tp_vars->bat_priv->tp_list_lock); |
| 134 | +- hlist_del_rcu(&tp_vars->list); |
| 135 | +- spin_unlock_bh(&tp_vars->bat_priv->tp_list_lock); |
| 136 | +- |
| 137 | +- /* drop list reference */ |
| 138 | +- batadv_tp_vars_put(tp_vars); |
| 139 | +- |
| 140 | +- atomic_dec(&bat_priv->tp_num); |
| 141 | ++ batadv_tp_list_detach(tp_vars); |
| 142 | + |
| 143 | + spin_lock_bh(&tp_vars->unacked_lock); |
| 144 | + list_for_each_entry_safe(un, safe, &tp_vars->unacked_list, list) { |
| 145 | +@@ -1497,6 +1508,52 @@ out: |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | ++ * batadv_tp_stop_all() - stop all currently running tp meter sessions |
| 150 | ++ * @bat_priv: the bat priv with all the mesh interface information |
| 151 | ++ */ |
| 152 | ++void batadv_tp_stop_all(struct batadv_priv *bat_priv) |
| 153 | ++{ |
| 154 | ++ struct batadv_tp_vars *tp_vars[BATADV_TP_MAX_NUM]; |
| 155 | ++ struct batadv_tp_vars *tp_var; |
| 156 | ++ size_t count = 0; |
| 157 | ++ size_t i; |
| 158 | ++ |
| 159 | ++ spin_lock_bh(&bat_priv->tp_list_lock); |
| 160 | ++ hlist_for_each_entry(tp_var, &bat_priv->tp_list, list) { |
| 161 | ++ if (WARN_ON_ONCE(count >= BATADV_TP_MAX_NUM)) |
| 162 | ++ break; |
| 163 | ++ |
| 164 | ++ if (!kref_get_unless_zero(&tp_var->refcount)) |
| 165 | ++ continue; |
| 166 | ++ |
| 167 | ++ tp_vars[count++] = tp_var; |
| 168 | ++ } |
| 169 | ++ spin_unlock_bh(&bat_priv->tp_list_lock); |
| 170 | ++ |
| 171 | ++ for (i = 0; i < count; i++) { |
| 172 | ++ tp_var = tp_vars[i]; |
| 173 | ++ |
| 174 | ++ switch (tp_var->role) { |
| 175 | ++ case BATADV_TP_SENDER: |
| 176 | ++ batadv_tp_sender_shutdown(tp_var, |
| 177 | ++ BATADV_TP_REASON_CANCEL); |
| 178 | ++ wake_up(&tp_var->more_bytes); |
| 179 | ++ wait_for_completion(&tp_var->finished); |
| 180 | ++ break; |
| 181 | ++ case BATADV_TP_RECEIVER: |
| 182 | ++ batadv_tp_list_detach(tp_var); |
| 183 | ++ if (timer_shutdown_sync(&tp_var->timer)) |
| 184 | ++ batadv_tp_vars_put(tp_var); |
| 185 | ++ break; |
| 186 | ++ } |
| 187 | ++ |
| 188 | ++ batadv_tp_vars_put(tp_var); |
| 189 | ++ } |
| 190 | ++ |
| 191 | ++ synchronize_net(); |
| 192 | ++} |
| 193 | ++ |
| 194 | ++/** |
| 195 | + * batadv_tp_meter_init() - initialize global tp_meter structures |
| 196 | + */ |
| 197 | + void __init batadv_tp_meter_init(void) |
| 198 | +--- a/net/batman-adv/tp_meter.h |
| 199 | ++++ b/net/batman-adv/tp_meter.h |
| 200 | +@@ -17,6 +17,7 @@ void batadv_tp_start(struct batadv_priv |
| 201 | + u32 test_length, u32 *cookie); |
| 202 | + void batadv_tp_stop(struct batadv_priv *bat_priv, const u8 *dst, |
| 203 | + u8 return_value); |
| 204 | ++void batadv_tp_stop_all(struct batadv_priv *bat_priv); |
| 205 | + void batadv_tp_meter_recv(struct batadv_priv *bat_priv, struct sk_buff *skb); |
| 206 | + |
| 207 | + #endif /* _NET_BATMAN_ADV_TP_METER_H_ */ |
| 208 | +--- a/net/batman-adv/types.h |
| 209 | ++++ b/net/batman-adv/types.h |
| 210 | +@@ -14,6 +14,7 @@ |
| 211 | + #include <linux/average.h> |
| 212 | + #include <linux/bitops.h> |
| 213 | + #include <linux/compiler.h> |
| 214 | ++#include <linux/completion.h> |
| 215 | + #include <linux/if.h> |
| 216 | + #include <linux/if_ether.h> |
| 217 | + #include <linux/kref.h> |
| 218 | +@@ -1328,6 +1329,9 @@ struct batadv_tp_vars { |
| 219 | + /** @finish_work: work item for the finishing procedure */ |
| 220 | + struct delayed_work finish_work; |
| 221 | + |
| 222 | ++ /** @finished: completion signaled when a sender thread exits */ |
| 223 | ++ struct completion finished; |
| 224 | ++ |
| 225 | + /** @test_length: test length in milliseconds */ |
| 226 | + u32 test_length; |
| 227 | + |
0 commit comments