vxlan: Convert FDB garbage collection to RCU
authorIdo Schimmel <idosch@nvidia.com>
Tue, 15 Apr 2025 12:11:37 +0000 (15:11 +0300)
committerPaolo Abeni <pabeni@redhat.com>
Tue, 22 Apr 2025 09:11:15 +0000 (11:11 +0200)
Instead of holding the FDB hash lock when traversing the FDB linked list
during garbage collection, use RCU and only acquire the lock for entries
that need to be removed (aged out).

Avoid races by using hlist_unhashed() to check that the entry has not
been removed from the list by another thread.

Note that vxlan_fdb_destroy() uses hlist_del_init_rcu() to remove an
entry from the list which should cause list_unhashed() to return true.

Reviewed-by: Petr Machata <petrm@nvidia.com>
Signed-off-by: Ido Schimmel <idosch@nvidia.com>
Link: https://patch.msgid.link/20250415121143.345227-10-idosch@nvidia.com
Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
drivers/net/vxlan/vxlan_core.c

index f9840a4..c3511a4 100644 (file)
@@ -2819,14 +2819,13 @@ static void vxlan_cleanup(struct timer_list *t)
 {
        struct vxlan_dev *vxlan = from_timer(vxlan, t, age_timer);
        unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
-       struct hlist_node *n;
        struct vxlan_fdb *f;
 
        if (!netif_running(vxlan->dev))
                return;
 
-       spin_lock(&vxlan->hash_lock);
-       hlist_for_each_entry_safe(f, n, &vxlan->fdb_list, fdb_node) {
+       rcu_read_lock();
+       hlist_for_each_entry_rcu(f, &vxlan->fdb_list, fdb_node) {
                unsigned long timeout;
 
                if (f->state & (NUD_PERMANENT | NUD_NOARP))
@@ -2837,15 +2836,19 @@ static void vxlan_cleanup(struct timer_list *t)
 
                timeout = READ_ONCE(f->updated) + vxlan->cfg.age_interval * HZ;
                if (time_before_eq(timeout, jiffies)) {
-                       netdev_dbg(vxlan->dev, "garbage collect %pM\n",
-                                  f->eth_addr);
-                       f->state = NUD_STALE;
-                       vxlan_fdb_destroy(vxlan, f, true, true);
+                       spin_lock(&vxlan->hash_lock);
+                       if (!hlist_unhashed(&f->fdb_node)) {
+                               netdev_dbg(vxlan->dev, "garbage collect %pM\n",
+                                          f->eth_addr);
+                               f->state = NUD_STALE;
+                               vxlan_fdb_destroy(vxlan, f, true, true);
+                       }
+                       spin_unlock(&vxlan->hash_lock);
                } else if (time_before(timeout, next_timer)) {
                        next_timer = timeout;
                }
        }
-       spin_unlock(&vxlan->hash_lock);
+       rcu_read_unlock();
 
        mod_timer(&vxlan->age_timer, next_timer);
 }