From: Sven Eckelmann batadv_tt_local_add() modifies the flags of a local entry in two steps. batadv_tt_local_add_existing() cancels a pending removal and batadv_tt_local_update_flags() updates the dynamic flags. Each of them has its own flags_lock section and the ADD events are queued by batadv_tt_local_add() with a snapshot of the flags which is taken after the lock was released. Merge both helpers into batadv_tt_local_refresh(). It modifies the flags and queues the (single) ADD event in the same flags_lock section. The announced flags are therefore always the ones which were just stored in the entry. The initial ADD of a new entry is requested via the announce parameter after the entry was added to the hash. This is a preparation for the validation of the entry under the hash bucket list_lock, which then only needs to cover a single region. Reported-by: Sashiko Closes: https://sashiko.dev/#/bug/linux-7602d89e-b2cd-43ca-8aca-c165e5d674b2 Signed-off-by: Sven Eckelmann Signed-off-by: Simon Wunderlich --- net/batman-adv/translation-table.c | 129 ++++++++++++----------------- 1 file changed, 55 insertions(+), 74 deletions(-) diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c index 1c784a0e8c9b..63eac1e1c65e 100644 --- a/net/batman-adv/translation-table.c +++ b/net/batman-adv/translation-table.c @@ -903,57 +903,6 @@ static bool batadv_tt_iif_is_wifi(struct net *net, int ifindex) return batadv_is_wifi(wifi_flags); } -/** - * batadv_tt_local_add_existing() - refresh an already known local TT entry - * @bat_priv: the bat priv with all the mesh interface information - * @tt_local: the local TT entry which was found in the local table - * @roamed_back: set to true when the client returned to its original location - * - * Return: true when the client has to be announced to the mesh again, false - * otherwise. - */ -static bool batadv_tt_local_add_existing(struct batadv_priv *bat_priv, - struct batadv_tt_local_entry *tt_local, - bool *roamed_back) -{ - struct batadv_tt_common_entry *common = &tt_local->common; - bool readded = false; - - WRITE_ONCE(tt_local->last_seen, jiffies); - - scoped_guard(spinlock_bh, &common->flags_lock) { - if (common->flags & BATADV_TT_CLIENT_PENDING) { - batadv_dbg(BATADV_DBG_TT, bat_priv, - "Re-adding pending client %pM (vid: %d)\n", - common->key.addr, - batadv_print_vid(common->key.vid)); - /* whatever the reason why the PENDING flag was set, - * this is a client which was enqueued to be removed in - * this orig_interval. Since it popped up again, the - * flag can be reset like it was never enqueued - */ - common->flags &= ~BATADV_TT_CLIENT_PENDING; - readded = true; - } - - if (common->flags & BATADV_TT_CLIENT_ROAM) { - batadv_dbg(BATADV_DBG_TT, bat_priv, - "Roaming client %pM (vid: %d) came back to its original location\n", - common->key.addr, - batadv_print_vid(common->key.vid)); - /* the ROAM flag is set because this client roamed away - * and the node got a roaming_advertisement message. Now - * that the client popped up again at its original - * location such flag can be unset - */ - common->flags &= ~BATADV_TT_CLIENT_ROAM; - *roamed_back = true; - } - } - - return readded; -} - /** * batadv_tt_local_create() - allocate and initialize a local TT entry * @mesh_iface: netdev struct of the mesh interface @@ -1028,27 +977,63 @@ batadv_tt_local_create(struct net_device *mesh_iface, const u8 *addr, } /** - * batadv_tt_local_update_flags() - update the dynamic flags of a local entry + * batadv_tt_local_refresh() - refresh a local TT entry of an active client * @bat_priv: the bat priv with all the mesh interface information - * @tt_local: the local TT entry to update + * @tt_local: the local TT entry to refresh * @iif_is_wifi: whether the client is connected via a wifi interface * @mark: the value contained in the skb->mark field of the received packet (if * any) + * @announce: whether an ADD event has to be queued even when no announced + * flag was modified + * @roamed_back: set to true when the client returned to its original location * - * Return: true if a flag announced to the other nodes was modified, false - * otherwise. + * A pending removal of the entry is cancelled and its dynamic flags are + * updated. An ADD event is queued when @announce is set, the pending removal + * was cancelled or a flag announced to the other nodes was modified. + * + * All checks and modifications of the flags and the queueing of the ADD event + * happen in a single flags_lock section. The announced flags are therefore the + * ones which were just stored in the entry. */ -static bool -batadv_tt_local_update_flags(struct batadv_priv *bat_priv, - struct batadv_tt_local_entry *tt_local, - bool iif_is_wifi, u32 mark) +static void +batadv_tt_local_refresh(struct batadv_priv *bat_priv, + struct batadv_tt_local_entry *tt_local, + bool iif_is_wifi, u32 mark, bool announce, + bool *roamed_back) { struct batadv_tt_common_entry *common = &tt_local->common; u8 remote_flags; u32 match_mark; - bool modified; scoped_guard(spinlock_bh, &common->flags_lock) { + if (common->flags & BATADV_TT_CLIENT_PENDING) { + batadv_dbg(BATADV_DBG_TT, bat_priv, + "Re-adding pending client %pM (vid: %d)\n", + common->key.addr, + batadv_print_vid(common->key.vid)); + /* whatever the reason why the PENDING flag was set, + * this is a client which was enqueued to be removed in + * this orig_interval. Since it popped up again, the + * flag can be reset like it was never enqueued + */ + common->flags &= ~BATADV_TT_CLIENT_PENDING; + announce = true; + } + + if (common->flags & BATADV_TT_CLIENT_ROAM) { + batadv_dbg(BATADV_DBG_TT, bat_priv, + "Roaming client %pM (vid: %d) came back to its original location\n", + common->key.addr, + batadv_print_vid(common->key.vid)); + /* the ROAM flag is set because this client roamed away + * and the node got a roaming_advertisement message. Now + * that the client popped up again at its original + * location such flag can be unset + */ + common->flags &= ~BATADV_TT_CLIENT_ROAM; + *roamed_back = true; + } + /* store the current remote flags before altering them. This * helps understanding is flags are changing or not */ @@ -1070,10 +1055,13 @@ batadv_tt_local_update_flags(struct batadv_priv *bat_priv, else common->flags &= ~BATADV_TT_CLIENT_ISOLA; - modified = remote_flags ^ (common->flags & BATADV_TT_REMOTE_MASK); - } + if (remote_flags ^ (common->flags & BATADV_TT_REMOTE_MASK)) + announce = true; - return modified; + if (announce) + __batadv_tt_local_event(bat_priv, common, + common->flags); + } } /** @@ -1109,8 +1097,7 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr, tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid); if (tt_local) { - added = batadv_tt_local_add_existing(bat_priv, tt_local, - &roamed_back); + WRITE_ONCE(tt_local->last_seen, jiffies); } else { tt_local = batadv_tt_local_create(mesh_iface, addr, vid, iif_is_wifi); if (!tt_local) @@ -1130,18 +1117,12 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr, added = true; } - /* announce the (re-)added client to the mesh */ - if (added) - batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS); + /* announce the (re-)added client or its modified flags to the mesh */ + batadv_tt_local_refresh(bat_priv, tt_local, iif_is_wifi, mark, added, + &roamed_back); batadv_tt_local_add_roam(bat_priv, tt_global, roamed_back); - /* if any "dynamic" flag has been modified, resend an ADD event for this - * entry so that all the nodes can get the new flags - */ - if (batadv_tt_local_update_flags(bat_priv, tt_local, iif_is_wifi, mark)) - batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS); - ret = true; out: batadv_tt_local_entry_put(tt_local); -- 2.47.3