From: Simon Wunderlich <sw@simonwunderlich.de>
To: netdev@vger.kernel.org
Cc: "David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Simon Horman <horms@kernel.org>,
b.a.t.m.a.n@lists.open-mesh.org,
Sven Eckelmann <sven@narfation.org>,
Sashiko <sashiko-bot@kernel.org>,
Simon Wunderlich <sw@simonwunderlich.de>
Subject: [PATCH net-next 08/15] batman-adv: tt: don't queue local events for unhashed entries
Date: Mon, 23 Nov 2026 18:18:20 +0100 [thread overview]
Message-ID: <20261123171827.3294525-9-sw@simonwunderlich.de> (raw)
In-Reply-To: <20261123171827.3294525-1-sw@simonwunderlich.de>
From: Sven Eckelmann <sven@narfation.org>
batadv_tt_local_add() looks up the local entry via RCU only.
batadv_tt_local_purge_pending_clients() or batadv_tt_local_remove() can
unhash it before the entry is modified and an event is queued for it:
CPU0 CPU1
/* DEL#1 queued, PENDING set */
batadv_tt_local_hash_find()
batadv_tt_local_commit_changes()
..._purge_pending_clients()
hlist_del_rcu(&...->hash_entry);
/* DEL#1 announced */
batadv_tt_local_refresh()
flags &= ~..._PENDING;
/* ADD queued for unhashed entry */
The queued event will then be announced for a client which is not part
anymore of the table (+ its CRC). A full table request in this and similar
scenarios is required to fix the inconsistency.
Only the hash bucket list_lock can ensure that an entry is still part of
the local table. batadv_tt_local_refresh() therefore checks with
hlist_unhashed() under this lock whether the entry is still hashed. An
entry which is no longer hashed is not refreshed and batadv_tt_local_add()
creates a new entry instead. batadv_tt_local_insert() adds this entry to
the hash and queues its initial ADD under the same list_lock. Otherwise
batadv_tt_local_commit_changes() could count the entry in the CRC of a ttvn
before its ADD was queued or batadv_tt_local_remove() could unhash it
before the ADD was queued.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260930094558.3723766-2-sw%40simonwunderlich.de?part=7
Closes: https://sashiko.dev/#/bug/linux-557fd001-5041-4b0a-a2ea-cdf22526ee84
Closes: https://sashiko.dev/#/bug/linux-c7f17a4b-620f-4307-a058-be6523cc6fa2
Signed-off-by: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
---
net/batman-adv/translation-table.c | 156 ++++++++++++++++++++++++-----
1 file changed, 129 insertions(+), 27 deletions(-)
diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c
index 7968931d5d3c..2828e362640a 100644
--- a/net/batman-adv/translation-table.c
+++ b/net/batman-adv/translation-table.c
@@ -966,7 +966,7 @@ batadv_tt_local_create(struct net_device *mesh_iface, const u8 *addr,
}
/**
- * batadv_tt_local_refresh() - refresh a local TT entry of an active client
+ * batadv_tt_local_refresh_locked() - refresh a hashed local TT entry
* @bat_priv: the bat priv with all the mesh interface information
* @tt_local: the local TT entry to refresh
* @iif_is_wifi: whether the client is connected via a wifi interface
@@ -980,19 +980,23 @@ batadv_tt_local_create(struct net_device *mesh_iface, const u8 *addr,
* 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.
+ * The caller must hold the hash bucket list_lock of the entry and must have
+ * checked that the entry is still part of the local table.
*/
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)
+batadv_tt_local_refresh_locked(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;
+ struct batadv_hashtable *hash = bat_priv->tt.local_hash;
u8 remote_flags;
u32 match_mark;
+ u32 i;
+
+ i = batadv_choose_tt(&common->key, hash->size);
+ lockdep_assert_held(&hash->list_locks[i]);
scoped_guard(spinlock_bh, &common->flags_lock) {
if (common->flags & BATADV_TT_CLIENT_PENDING) {
@@ -1053,6 +1057,109 @@ batadv_tt_local_refresh(struct batadv_priv *bat_priv,
}
}
+/**
+ * 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 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)
+ * @roamed_back: set to true when the client returned to its original location
+ *
+ * See batadv_tt_local_refresh_locked() for the actual refresh.
+ *
+ * Return: 0 if the entry was refreshed, -ENOENT if the entry is no longer part
+ * of the local table.
+ */
+static int
+batadv_tt_local_refresh(struct batadv_priv *bat_priv,
+ struct batadv_tt_local_entry *tt_local,
+ bool iif_is_wifi, u32 mark, bool *roamed_back)
+{
+ spinlock_t *list_lock; /* protects write access to the hash lists */
+ struct batadv_tt_common_entry *common = &tt_local->common;
+ struct batadv_hashtable *hash = bat_priv->tt.local_hash;
+ u32 i;
+
+ i = batadv_choose_tt(&common->key, hash->size);
+ list_lock = &hash->list_locks[i];
+
+ WRITE_ONCE(tt_local->last_seen, jiffies);
+
+ spin_lock_bh(list_lock);
+
+ /* the entry was removed from the local table after it was looked up */
+ if (hlist_unhashed(&common->hash_entry)) {
+ spin_unlock_bh(list_lock);
+ return -ENOENT;
+ }
+
+ batadv_tt_local_refresh_locked(bat_priv, tt_local, iif_is_wifi, mark,
+ false, roamed_back);
+
+ spin_unlock_bh(list_lock);
+
+ return 0;
+}
+
+/**
+ * batadv_tt_local_insert() - add a new local TT entry to the local table
+ * @bat_priv: the bat priv with all the mesh interface information
+ * @tt_local: the new local TT entry (not yet hashed)
+ * @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)
+ * @roamed_back: set to true when the client returned to its original location
+ *
+ * The entry is inserted into the local hash and its initial ADD event is
+ * queued under the same hash bucket list_lock. A concurrent
+ * batadv_tt_local_commit_changes() can therefore not count the entry in the
+ * CRC of a ttvn before its ADD was queued and a concurrent
+ * batadv_tt_local_remove() can not unhash it before the ADD was queued.
+ *
+ * Return: true if the entry was added, false if the client already has an
+ * entry in the local table.
+ */
+static bool batadv_tt_local_insert(struct batadv_priv *bat_priv,
+ struct batadv_tt_local_entry *tt_local,
+ bool iif_is_wifi, u32 mark,
+ bool *roamed_back)
+{
+ spinlock_t *list_lock; /* protects write access to the hash lists */
+ struct batadv_tt_common_entry *common = &tt_local->common;
+ struct batadv_hashtable *hash = bat_priv->tt.local_hash;
+ struct hlist_head *head;
+ struct hlist_node *node;
+ u32 i;
+
+ i = batadv_choose_tt(&common->key, hash->size);
+ head = &hash->table[i];
+ list_lock = &hash->list_locks[i];
+
+ spin_lock_bh(list_lock);
+
+ hlist_for_each(node, head) {
+ if (!batadv_compare_tt(node, &common->key))
+ continue;
+
+ spin_unlock_bh(list_lock);
+ return false;
+ }
+
+ /* reference for the hash */
+ kref_get(&common->refcount);
+ hlist_add_head_rcu(&common->hash_entry, head);
+ atomic_inc(&hash->generation);
+
+ /* announce the added client to the mesh */
+ batadv_tt_local_refresh_locked(bat_priv, tt_local, iif_is_wifi, mark,
+ true, roamed_back);
+
+ spin_unlock_bh(list_lock);
+
+ return true;
+}
+
/**
* batadv_tt_local_add() - add a new client to the local table or update an
* existing client
@@ -1073,10 +1180,9 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
struct batadv_tt_global_entry *tt_global = NULL;
struct batadv_tt_local_entry *tt_local;
bool roamed_back = false;
- bool added = false;
bool iif_is_wifi;
bool ret = false;
- int hash_added;
+ int err;
iif_is_wifi = batadv_tt_iif_is_wifi(dev_net(mesh_iface), ifindex);
@@ -1086,30 +1192,26 @@ 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) {
- WRITE_ONCE(tt_local->last_seen, jiffies);
- } else {
+ err = batadv_tt_local_refresh(bat_priv, tt_local, iif_is_wifi,
+ mark, &roamed_back);
+ if (err == -ENOENT) {
+ /* stale entry which has to be replaced by a new one */
+ batadv_tt_local_entry_put(tt_local);
+ tt_local = NULL;
+ }
+ }
+
+ if (!tt_local) {
tt_local = batadv_tt_local_create(mesh_iface, addr, vid, iif_is_wifi);
if (!tt_local)
goto out;
- kref_get(&tt_local->common.refcount);
- hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
- batadv_choose_tt, &tt_local->common.key,
- &tt_local->common.hash_entry);
-
- if (unlikely(hash_added != 0)) {
- /* remove the reference for the hash */
- batadv_tt_local_entry_put(tt_local);
+ if (unlikely(!batadv_tt_local_insert(bat_priv, tt_local,
+ iif_is_wifi, mark,
+ &roamed_back)))
goto out;
- }
-
- added = true;
}
- /* 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);
ret = true;
--
2.47.3
next prev parent reply other threads:[~2026-11-23 17:18 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 17:18 [PATCH net-next 00/15] pull request for net-next: batman-adv 2026-11-23 Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 01/15] batman-adv: document refcount of returned objects Simon Wunderlich
2026-11-23 17:23 ` netdev-bot+sinfo
2026-11-23 17:18 ` [PATCH net-next 02/15] batman-adv: mcast: use mla_lock also during mcast_free Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 03/15] batman-adv: mark functions inheriting critical sections Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 04/15] batman-adv: bla: register own backbone gw when claiming for local node Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 05/15] batman-adv: tt: unhash local entries with hlist_del_init_rcu() Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 06/15] batman-adv: tt: refresh local entry in single flags_lock section Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 07/15] batman-adv: tt: don't remove unhashed local entries Simon Wunderlich
2026-11-23 17:18 ` Simon Wunderlich [this message]
2026-11-23 17:18 ` [PATCH net-next 09/15] batman-adv: tt: remove unannounced local entries via pending state Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 10/15] batman-adv: tt: keep local entry unchanged when ADD can't be queued Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 11/15] batman-adv: tt: requeue DEL of pending local entry after failed allocation Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 12/15] batman-adv: tt: extract removal of pending local entry Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 13/15] batman-adv: tt: commit only the announced local changes Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 14/15] batman-adv: tt: avoid list_lock for unchanged local clients Simon Wunderlich
2026-11-23 17:18 ` [PATCH net-next 15/15] batman-adv: tt: drop ADD+DEL events of never announced clients Simon Wunderlich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261123171827.3294525-9-sw@simonwunderlich.de \
--to=sw@simonwunderlich.de \
--cc=b.a.t.m.a.n@lists.open-mesh.org \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=sashiko-bot@kernel.org \
--cc=sven@narfation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox