]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
netdev: add "ops compat locking" helpers
authorJakub Kicinski <kuba@kernel.org>
Tue, 8 Apr 2025 19:59:50 +0000 (12:59 -0700)
committerJakub Kicinski <kuba@kernel.org>
Thu, 10 Apr 2025 00:01:51 +0000 (17:01 -0700)
Add helpers to "lock a netdev in a backward-compatible way",
which for ops-locked netdevs will mean take the instance lock.
For drivers which haven't opted into the ops locking we'll take
rtnl_lock.

The scoped foreach is dropping and re-taking the lock for each
device, even if prev and next are both under rtnl_lock.
I hope that's fine since we expect that netdev nl to be mostly
supported by modern drivers, and modern drivers should also
opt into the instance locking.

Note that these helpers are mostly needed for queue related state,
because drivers modify queue config in their ops in a non-atomic
way. Or differently put, queue changes don't have a clear-cut API
like NAPI configuration. Any state that can should just use the
instance lock directly, not the "compat" hacks.

Reviewed-by: Joe Damato <jdamato@fastly.com>
Acked-by: Stanislav Fomichev <sdf@fomichev.me>
Link: https://patch.msgid.link/20250408195956.412733-4-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/netdev_lock.h
net/core/dev.c
net/core/dev.h

index c316b551df8d61b748a9489e40225cea667cf5ac..5706835a660c1fb5345c441e36ec355372e1382e 100644 (file)
@@ -64,6 +64,22 @@ netdev_ops_assert_locked_or_invisible(const struct net_device *dev)
                netdev_ops_assert_locked(dev);
 }
 
+static inline void netdev_lock_ops_compat(struct net_device *dev)
+{
+       if (netdev_need_ops_lock(dev))
+               netdev_lock(dev);
+       else
+               rtnl_lock();
+}
+
+static inline void netdev_unlock_ops_compat(struct net_device *dev)
+{
+       if (netdev_need_ops_lock(dev))
+               netdev_unlock(dev);
+       else
+               rtnl_unlock();
+}
+
 static inline int netdev_lock_cmp_fn(const struct lockdep_map *a,
                                     const struct lockdep_map *b)
 {
index 8c13e5339cc98a638e59ae4fa8a76668433a4585..b52efa4cec564132b631c38c5b8cff1d6a597efb 100644 (file)
@@ -1052,6 +1052,20 @@ struct net_device *__netdev_put_lock(struct net_device *dev, struct net *net)
        return dev;
 }
 
+static struct net_device *
+__netdev_put_lock_ops_compat(struct net_device *dev, struct net *net)
+{
+       netdev_lock_ops_compat(dev);
+       if (dev->reg_state > NETREG_REGISTERED ||
+           dev->moving_ns || !net_eq(dev_net(dev), net)) {
+               netdev_unlock_ops_compat(dev);
+               dev_put(dev);
+               return NULL;
+       }
+       dev_put(dev);
+       return dev;
+}
+
 /**
  *     netdev_get_by_index_lock() - find a device by its ifindex
  *     @net: the applicable net namespace
@@ -1074,6 +1088,18 @@ struct net_device *netdev_get_by_index_lock(struct net *net, int ifindex)
        return __netdev_put_lock(dev, net);
 }
 
+struct net_device *
+netdev_get_by_index_lock_ops_compat(struct net *net, int ifindex)
+{
+       struct net_device *dev;
+
+       dev = dev_get_by_index(net, ifindex);
+       if (!dev)
+               return NULL;
+
+       return __netdev_put_lock_ops_compat(dev, net);
+}
+
 struct net_device *
 netdev_xa_find_lock(struct net *net, struct net_device *dev,
                    unsigned long *index)
@@ -1099,6 +1125,31 @@ netdev_xa_find_lock(struct net *net, struct net_device *dev,
        } while (true);
 }
 
+struct net_device *
+netdev_xa_find_lock_ops_compat(struct net *net, struct net_device *dev,
+                              unsigned long *index)
+{
+       if (dev)
+               netdev_unlock_ops_compat(dev);
+
+       do {
+               rcu_read_lock();
+               dev = xa_find(&net->dev_by_index, index, ULONG_MAX, XA_PRESENT);
+               if (!dev) {
+                       rcu_read_unlock();
+                       return NULL;
+               }
+               dev_hold(dev);
+               rcu_read_unlock();
+
+               dev = __netdev_put_lock_ops_compat(dev, net);
+               if (dev)
+                       return dev;
+
+               (*index)++;
+       } while (true);
+}
+
 static DEFINE_SEQLOCK(netdev_rename_lock);
 
 void netdev_copy_name(struct net_device *dev, char *name)
index e7bf21f52fc7639b7c9382a09da484a8ed71c3ec..e93f36b7ddf369e99dc4b6eee011e5e998ed0837 100644 (file)
@@ -42,6 +42,21 @@ DEFINE_FREE(netdev_unlock, struct net_device *, if (_T) netdev_unlock(_T));
             (var_name = netdev_xa_find_lock(net, var_name, &ifindex)); \
             ifindex++)
 
+struct net_device *
+netdev_get_by_index_lock_ops_compat(struct net *net, int ifindex);
+struct net_device *
+netdev_xa_find_lock_ops_compat(struct net *net, struct net_device *dev,
+                              unsigned long *index);
+
+DEFINE_FREE(netdev_unlock_ops_compat, struct net_device *,
+           if (_T) netdev_unlock_ops_compat(_T));
+
+#define for_each_netdev_lock_ops_compat_scoped(net, var_name, ifindex) \
+       for (struct net_device *var_name __free(netdev_unlock_ops_compat) = NULL; \
+            (var_name = netdev_xa_find_lock_ops_compat(net, var_name,  \
+                                                       &ifindex));     \
+            ifindex++)
+
 #ifdef CONFIG_PROC_FS
 int __init dev_proc_init(void);
 #else