From d9017aeb40c1386920248781d24aa8823e6894ba Mon Sep 17 00:00:00 2001 From: Sven Eckelmann Date: Sun, 30 Aug 2026 17:21:51 +0200 Subject: [PATCH] batman-adv: tt: extract allocation of new global entries The code which allocates and initializes a brand new global TT entry makes up a third of batadv_tt_global_add() and only needs the tt_addr, vid and flags arguments. It is also the only part of the "new entry" branch which does not deal with the global hash. Move it to batadv_tt_global_create() and let it return the initialized entry. Signed-off-by: Sven Eckelmann --- net/batman-adv/translation-table.c | 76 ++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 25 deletions(-) diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c index e0bcef1eba93..d475f8efd44d 100644 --- a/net/batman-adv/translation-table.c +++ b/net/batman-adv/translation-table.c @@ -1832,6 +1832,56 @@ out: spin_unlock_bh(&tt_global->list_lock); } +/** + * batadv_tt_global_create() - allocate and initialize a global TT entry + * @tt_addr: the mac address of the non-mesh client + * @vid: VLAN identifier + * @flags: TT flags that have to be set for this non-mesh client + * + * The returned entry is not yet part of bat_priv->tt.global_hash and has an + * empty originator list. + * + * Return: the new entry with an initialized reference counter on success, NULL + * otherwise. + */ +static struct batadv_tt_global_entry * +batadv_tt_global_create(const unsigned char *tt_addr, unsigned short vid, + u16 flags) +{ + struct batadv_tt_global_entry *tt_global_entry; + struct batadv_tt_common_entry *common; + + tt_global_entry = kmem_cache_zalloc(batadv_tg_cache, GFP_ATOMIC); + if (!tt_global_entry) + return NULL; + + common = &tt_global_entry->common; + ether_addr_copy(common->addr, tt_addr); + common->vid = vid; + spin_lock_init(&common->flags_lock); + + if (!is_multicast_ether_addr(common->addr)) { + scoped_guard(spinlock_bh, &common->flags_lock) + common->flags = flags & (~BATADV_TT_SYNC_MASK); + } + + tt_global_entry->roam_at = 0; + /* node must store current time in case of roaming. This is + * needed to purge this entry out on timeout (if nobody claims + * it) + */ + if (flags & BATADV_TT_CLIENT_ROAM) + tt_global_entry->roam_at = jiffies; + kref_init(&common->refcount); + common->added_at = jiffies; + + INIT_HLIST_HEAD(&tt_global_entry->orig_list); + atomic_set(&tt_global_entry->orig_list_count, 0); + spin_lock_init(&tt_global_entry->list_lock); + + return tt_global_entry; +} + /** * batadv_tt_global_add() - add a new TT global entry or update an existing one * @bat_priv: the bat priv with all the mesh interface information @@ -1883,35 +1933,11 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv, } if (!tt_global_entry) { - tt_global_entry = kmem_cache_zalloc(batadv_tg_cache, - GFP_ATOMIC); + tt_global_entry = batadv_tt_global_create(tt_addr, vid, flags); if (!tt_global_entry) goto out; common = &tt_global_entry->common; - ether_addr_copy(common->addr, tt_addr); - common->vid = vid; - spin_lock_init(&common->flags_lock); - - if (!is_multicast_ether_addr(common->addr)) { - spin_lock_bh(&common->flags_lock); - common->flags = flags & (~BATADV_TT_SYNC_MASK); - spin_unlock_bh(&common->flags_lock); - } - - tt_global_entry->roam_at = 0; - /* node must store current time in case of roaming. This is - * needed to purge this entry out on timeout (if nobody claims - * it) - */ - if (flags & BATADV_TT_CLIENT_ROAM) - tt_global_entry->roam_at = jiffies; - kref_init(&common->refcount); - common->added_at = jiffies; - - INIT_HLIST_HEAD(&tt_global_entry->orig_list); - atomic_set(&tt_global_entry->orig_list_count, 0); - spin_lock_init(&tt_global_entry->list_lock); kref_get(&common->refcount); hash_added = batadv_hash_add(bat_priv->tt.global_hash,