]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
devlink: allow devlink instance allocation without a backing device
authorJiri Pirko <jiri@nvidia.com>
Thu, 12 Mar 2026 10:04:04 +0000 (11:04 +0100)
committerJakub Kicinski <kuba@kernel.org>
Sat, 14 Mar 2026 20:08:49 +0000 (13:08 -0700)
Allow devlink_alloc_ns() to be called with dev=NULL to support
device-less devlink instances. When dev is NULL, the instance is
identified over netlink using "devlink_index" as bus_name and
the decimal index value as dev_name.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
Link: https://patch.msgid.link/20260312100407.551173-11-jiri@resnulli.us
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/devlink/core.c
net/devlink/dev.c
net/devlink/devl_internal.h

index 34eb06d88544570083919a894a00d7531b90de8b..eeb6a71f5f56ef0538da07d42d25f92473cd275b 100644 (file)
@@ -250,13 +250,13 @@ EXPORT_SYMBOL_GPL(devlink_to_dev);
 
 const char *devlink_bus_name(const struct devlink *devlink)
 {
-       return devlink->dev->bus->name;
+       return devlink->dev ? devlink->dev->bus->name : DEVLINK_INDEX_BUS_NAME;
 }
 EXPORT_SYMBOL_GPL(devlink_bus_name);
 
 const char *devlink_dev_name(const struct devlink *devlink)
 {
-       return dev_name(devlink->dev);
+       return devlink->dev ? dev_name(devlink->dev) : devlink->dev_name_index;
 }
 EXPORT_SYMBOL_GPL(devlink_dev_name);
 
@@ -329,7 +329,10 @@ static void devlink_release(struct work_struct *work)
 
        mutex_destroy(&devlink->lock);
        lockdep_unregister_key(&devlink->lock_key);
-       put_device(devlink->dev);
+       if (devlink->dev)
+               put_device(devlink->dev);
+       else
+               kfree(devlink->dev_name_index);
        kvfree(devlink);
 }
 
@@ -432,7 +435,7 @@ struct devlink *__devlink_alloc(const struct devlink_ops *ops, size_t priv_size,
        static u32 last_id;
        int ret;
 
-       WARN_ON(!ops || !dev || !dev_driver);
+       WARN_ON(!ops || !dev_driver);
        if (!devlink_reload_actions_valid(ops))
                return NULL;
 
@@ -445,7 +448,14 @@ struct devlink *__devlink_alloc(const struct devlink_ops *ops, size_t priv_size,
        if (ret < 0)
                goto err_xa_alloc;
 
-       devlink->dev = get_device(dev);
+       if (dev) {
+               devlink->dev = get_device(dev);
+       } else {
+               devlink->dev_name_index = kasprintf(GFP_KERNEL, "%u", devlink->index);
+               if (!devlink->dev_name_index)
+                       goto err_kasprintf;
+       }
+
        devlink->ops = ops;
        devlink->dev_driver = dev_driver;
        xa_init_flags(&devlink->ports, XA_FLAGS_ALLOC);
@@ -471,6 +481,8 @@ struct devlink *__devlink_alloc(const struct devlink_ops *ops, size_t priv_size,
 
        return devlink;
 
+err_kasprintf:
+       xa_erase(&devlinks, devlink->index);
 err_xa_alloc:
        kvfree(devlink);
        return NULL;
@@ -492,6 +504,7 @@ struct devlink *devlink_alloc_ns(const struct devlink_ops *ops,
                                 size_t priv_size, struct net *net,
                                 struct device *dev)
 {
+       WARN_ON(!dev);
        return __devlink_alloc(ops, priv_size, net, dev, dev->driver);
 }
 EXPORT_SYMBOL_GPL(devlink_alloc_ns);
index e3a36de4f4aec410434b1e6cadc5fb888ea3e02b..57b2b8f035436b689355c8adb2788a15122dbf60 100644 (file)
@@ -453,7 +453,8 @@ int devlink_reload(struct devlink *devlink, struct net *dest_net,
         * (e.g., PCI reset) and to close possible races between these
         * operations and probe/remove.
         */
-       device_lock_assert(devlink->dev);
+       if (devlink->dev)
+               device_lock_assert(devlink->dev);
 
        memcpy(remote_reload_stats, devlink->stats.remote_reload_stats,
               sizeof(remote_reload_stats));
@@ -854,7 +855,7 @@ int devlink_info_version_running_put_ext(struct devlink_info_req *req,
 }
 EXPORT_SYMBOL_GPL(devlink_info_version_running_put_ext);
 
-static int devlink_nl_driver_info_get(struct device_driver *drv,
+static int devlink_nl_driver_info_get(const struct device_driver *drv,
                                      struct devlink_info_req *req)
 {
        if (!drv)
@@ -872,7 +873,6 @@ devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
                     enum devlink_command cmd, u32 portid,
                     u32 seq, int flags, struct netlink_ext_ack *extack)
 {
-       struct device *dev = devlink_to_dev(devlink);
        struct devlink_info_req req = {};
        void *hdr;
        int err;
@@ -892,7 +892,7 @@ devlink_nl_info_fill(struct sk_buff *msg, struct devlink *devlink,
                        goto err_cancel_msg;
        }
 
-       err = devlink_nl_driver_info_get(dev->driver, &req);
+       err = devlink_nl_driver_info_get(devlink->dev_driver, &req);
        if (err)
                goto err_cancel_msg;
 
index cb2ffef1ac2d2e5fdef15cf97a1986fed39cf9fa..7dfb7cdd2d23e1bbcfc4ffb60399731994df142b 100644 (file)
@@ -49,6 +49,7 @@ struct devlink {
        struct xarray snapshot_ids;
        struct devlink_dev_stats stats;
        struct device *dev;
+       const char *dev_name_index;
        const struct device_driver *dev_driver;
        possible_net_t _net;
        /* Serializes access to devlink instance specific objects such as
@@ -119,7 +120,7 @@ static inline bool devl_is_registered(struct devlink *devlink)
 
 static inline void devl_dev_lock(struct devlink *devlink, bool dev_lock)
 {
-       if (dev_lock)
+       if (dev_lock && devlink->dev)
                device_lock(devlink->dev);
        devl_lock(devlink);
 }
@@ -127,7 +128,7 @@ static inline void devl_dev_lock(struct devlink *devlink, bool dev_lock)
 static inline void devl_dev_unlock(struct devlink *devlink, bool dev_lock)
 {
        devl_unlock(devlink);
-       if (dev_lock)
+       if (dev_lock && devlink->dev)
                device_unlock(devlink->dev);
 }