Skip to contentSkip to conversation

evlink: Add boot-time eswitch mode defaults

PATCH Superseded Mark Bloch 3 participants 19 messages Jun 29 – Jul 7

Subsystems (6)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Mark Bloch

author maintainer
#0 lore raw

v4 cover evlink: Add boot-time eswitch mode defaults

This series adds a devlink_eswitch_mode= kernel command line parameter for setting a default devlink eswitch mode during boot.

Following the discussion with Jakub[1] and the feedback on the RFC postings, this version keeps the scope limited to a boot-time devlink eswitch mode default only.

The option selects either all devlink handles or an explicit comma-separated handle list:

devlink_eswitch_mode=*=switchdev devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive

The supported modes are legacy, switchdev and switchdev_inactive. The selected mode is applied through the existing eswitch_mode_set() devlink operation, the same operation used by the devlink eswitch mode command.

Registration may happen before a driver is ready to change eswitch mode, so devlink core queues an asynchronous apply request from devl_register(). The worker takes the devlink instance lock before calling into the driver.

After a successful reload that performed DRIVER_REINIT, devlink core already holds the devlink instance lock and the driver completed reload_up(), so the default is applied directly from the reload path.

Drivers that know exactly when the device is ready can call devl_apply_default_esw_mode() directly. mlx5 uses this after initial probe, when the device is initialized and the devlink lock is already held.

Patch 1 clears the mlx5 FW reset-in-progress bit before reload.

Patch 2 factors the common eswitch mode set validation into a helper.

Patch 3 adds the devlink_eswitch_mode= parser and documentation.

Patch 4 applies parsed defaults from devlink core.

Patch 5 adds devl_apply_default_esw_mode() for drivers.

Patch 6 wires mlx5 to apply the default after initial probe.

Changelog:

v3 -> v4:

- Rework registration time apply to use per devlink delayed work instead of calling eswitch_mode_set() directly from devl_register().

- Apply the default directly after successful DRIVER_REINIT devlink reload, where the devlink lock is already held and reload_up() has completed.

- Add devl_apply_default_esw_mode() for drivers that know their exact ready point.

- Drop the driver registration-ordering preparation patches that are no longer needed with the async registration apply path.

v2 -> v3:

- Change the devlink_eswitch_mode= API syntax to use <selector>=<mode> instead of [<selector>]:<mode>, following a comment from Randy Dunlap.

v1 -> v2:

- Move default eswitch mode application into devlink core. The default is now applied during devlink registration and after a successful devlink reload that performed DRIVER_REINIT.

- Remove the exported devl_apply_default_esw_mode() driver API and the mlx5 driver-side call to it.

- Skip devlink health recovery notifications while the devlink instance is not registered, so drivers can move registration later without early health work hitting registration assertions.

- Move mlx5 devlink registration after device initialization, including the lightweight init path, so the core can apply the default through the normal registration flow.

- Move the matching netdevsim and mlx5 unregister paths before object teardown, so unregister notifications come from devl_unregister() and the later object teardown paths run while the devlink instance is no longer registered.

- Add registration-ordering preparation patches for netdevsim and octeontx2 AF/PF, so their eswitch state is ready before registration-time defaults may call eswitch_mode_set().

[1] lore.kernel.org/r/[email protected]/ RFC v1: lore.kernel.org/r/[email protected]/ RFC v2: lore.kernel.org/r/[email protected]/
v1: lore.kernel.org/r/[email protected]/
v2: lore.kernel.org/all/[email protected]/
v3: lore.kernel.org/all/[email protected]/

Mark Bloch (6):
net/mlx5: Clear FW reset-in-progress bit before reload
devlink: Factor out eswitch mode setting
devlink: Parse eswitch mode boot defaults
devlink: Apply eswitch mode boot defaults
devlink: Add API to apply eswitch mode boot default net/mlx5: Apply devlink eswitch mode boot default on probe

 .../admin-guide/kernel-parameters.txt         |  25 ++
 .../networking/devlink/devlink-defaults.rst   |  78 ++++
 Documentation/networking/devlink/index.rst    |   1 +
 .../ethernet/mellanox/mlx5/core/fw_reset.c    |  28 +-
 .../net/ethernet/mellanox/mlx5/core/main.c    |  13 +
 include/net/devlink.h                         |   1 +
 net/devlink/core.c                            | 393 ++++++++++++++++++
 net/devlink/dev.c                             |  33 +-
 net/devlink/devl_internal.h                   |   8 +
 9 files changed, 562 insertions(+), 18 deletions(-)
 create mode 100644 Documentation/networking/devlink/devlink-defaults.rst

base-commit: 805185b7c7a1069e407b6f7b3bc98e44d415f484

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v4 1/6 net/mlx5: Clear FW reset-in-progress bit before reload

mlx5 sets MLX5_FW_RESET_FLAGS_RESET_IN_PROGRESS when acknowledging a sync reset request. This bit blocks devlink reload and other devlink operations while the firmware reset is running, but it was kept set until after the driver reload finished.

Clear the reset-in-progress bit once the reset unload flow is done and PCI access is back, before reloading the device. For a reset initiated through devlink, clear it before completing the reload waiter. For a reset reported through an asynchronous firmware event, keep the unload flow outside devl_lock, then take devl_lock before clearing the bit and reloading through the devl-locked load helper.

---
 .../ethernet/mellanox/mlx5/core/fw_reset.c    | 28 +++++++++++--------
 1 file changed, 17 insertions(+), 11 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/fw_reset.c +17 -11
index 07440c58713a..7283e5b49eed 100644
@@ -238,24 +238,30 @@ static void mlx5_fw_reset_complete_reload(struct mlx5_core_dev *dev)
 {
 	struct mlx5_fw_reset *fw_reset = dev->priv.fw_reset;
 	struct devlink *devlink = priv_to_devlink(dev);
+	int err;
 
 	/* if this is the driver that initiated the fw reset, devlink completed the reload */
 	if (test_bit(MLX5_FW_RESET_FLAGS_PENDING_COMP, &fw_reset->reset_flags)) {
+		clear_bit(MLX5_FW_RESET_FLAGS_RESET_IN_PROGRESS,
+			  &fw_reset->reset_flags);
 		complete(&fw_reset->done);
-	} else {
-		mlx5_sync_reset_unload_flow(dev, false);
-		if (mlx5_health_wait_pci_up(dev))
-			mlx5_core_err(dev, "reset reload flow aborted, PCI reads still not working\n");
-		else
-			mlx5_load_one(dev, true);
-		devl_lock(devlink);
-		devlink_remote_reload_actions_performed(devlink, 0,
-							BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
-							BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE));
-		devl_unlock(devlink);
+		return;
 	}
 
+	mlx5_sync_reset_unload_flow(dev, false);
+	err = mlx5_health_wait_pci_up(dev);
+
+	devl_lock(devlink);
 	clear_bit(MLX5_FW_RESET_FLAGS_RESET_IN_PROGRESS, &fw_reset->reset_flags);
+	if (err)
+		mlx5_core_err(dev, "reset reload flow aborted, PCI reads still not working\n");
+	else
+		mlx5_load_one_devl_locked(dev, true);
+
+	devlink_remote_reload_actions_performed(devlink, 0,
+						BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
+						BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE));
+	devl_unlock(devlink);
 }
 
 static void mlx5_stop_sync_reset_poll(struct mlx5_core_dev *dev)
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v4 2/6 devlink: Factor out eswitch mode setting

In reply to Mark Bloch

Move the common eswitch mode set checks into a small helper and use it from the netlink eswitch set command. Making the same validation available to the devlink core path that applies eswitch mode defaults.

---
 net/devlink/dev.c           | 27 ++++++++++++++++++++-------
 net/devlink/devl_internal.h |  3 +++
 2 files changed, 23 insertions(+), 7 deletions(-)
net/devlink/dev.c +20 -7
index 57b2b8f03543..4fb02bb993c1 100644
@@ -702,6 +702,25 @@ int devlink_nl_eswitch_get_doit(struct sk_buff *skb, struct genl_info *info)
 	return genlmsg_reply(msg, info);
 }
 
+int devlink_eswitch_mode_set(struct devlink *devlink,
+			     enum devlink_eswitch_mode mode,
+			     struct netlink_ext_ack *extack)
+{
+	const struct devlink_ops *ops = devlink->ops;
+	int err;
+
+	devl_assert_locked(devlink);
+
+	if (!ops->eswitch_mode_set)
+		return -EOPNOTSUPP;
+
+	err = devlink_rates_check(devlink, devlink_rate_is_node, extack);
+	if (err)
+		return err;
+
+	return ops->eswitch_mode_set(devlink, mode, extack);
+}
+
 int devlink_nl_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info)
 {
 	struct devlink *devlink = info->user_ptr[0];
@@ -712,14 +731,8 @@ int devlink_nl_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info)
 	u16 mode;
 
 	if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
-		if (!ops->eswitch_mode_set)
-			return -EOPNOTSUPP;
-		err = devlink_rates_check(devlink, devlink_rate_is_node,
-					  info->extack);
-		if (err)
-			return err;
 		mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
-		err = ops->eswitch_mode_set(devlink, mode, info->extack);
+		err = devlink_eswitch_mode_set(devlink, mode, info->extack);
 		if (err)
 			return err;
 	}
net/devlink/devl_internal.h +3 -0
index e4e48ee2da5a..97be77d3ed42 100644
@@ -328,6 +328,9 @@ bool devlink_rate_is_node(const struct devlink_rate *devlink_rate);
 int devlink_rates_check(struct devlink *devlink,
 			bool (*rate_filter)(const struct devlink_rate *),
 			struct netlink_ext_ack *extack);
+int devlink_eswitch_mode_set(struct devlink *devlink,
+			     enum devlink_eswitch_mode mode,
+			     struct netlink_ext_ack *extack);
 
 /* Linecards */
 unsigned int devlink_linecard_index(struct devlink_linecard *linecard);
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v4 3/6 devlink: Parse eswitch mode boot defaults

In reply to Mark Bloch

Add devlink_eswitch_mode= kernel command line parsing for a default eswitch mode.

The supported syntax selects either all devlink handles or one explicit comma-separated handle list:

devlink_eswitch_mode=*=<mode>

devlink_eswitch_mode=<handle>[,<handle>...]=<mode>

where <mode> is one of legacy, switchdev or switchdev_inactive. All selected handles receive the same mode. Assigning different modes to different handle lists in the same parameter value is not supported.

Store the parsed selector and mode in devlink core so the default can be applied by a downstream patch.

Document the devlink_eswitch_mode= syntax and duplicate handle handling.

---
 .../admin-guide/kernel-parameters.txt         |  25 ++
 .../networking/devlink/devlink-defaults.rst   |  78 ++++++
 Documentation/networking/devlink/index.rst    |   1 +
 net/devlink/core.c                            | 227 ++++++++++++++++++
 4 files changed, 331 insertions(+)
 create mode 100644 Documentation/networking/devlink/devlink-defaults.rst
Documentation/admin-guide/kernel-parameters.txt +25 -0
index b5493a7f8f22..117300dd589c 100644
@@ -1249,6 +1249,31 @@ Kernel parameters
 	dell_smm_hwmon.fan_max=
 			[HW] Maximum configurable fan speed.
 
+	devlink_eswitch_mode=
+			[NET]
+			Format:
+			<selector>=<mode>
+
+			<selector>:
+			* | <handle>[,<handle>...]
+
+			<handle>:
+			<bus-name>/<dev-name>
+
+			Configure default devlink eswitch mode for matching
+			devlink instances during device initialization.
+
+			<mode>:
+			legacy | switchdev | switchdev_inactive
+
+			Examples:
+			devlink_eswitch_mode=*=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive
+
+			See Documentation/networking/devlink/devlink-defaults.rst
+			for the full syntax.
+
 	dfltcc=		[HW,S390]
 			Format: { on | off | def_only | inf_only | always }
 			on:       s390 zlib hardware support for compression on
Documentation/networking/devlink/devlink-defaults.rst new file +78 -0

78 lines not on this page · raw message

Documentation/networking/devlink/index.rst +1 -0
index 32f70879ddd0..93f09cb18c44 100644
@@ -56,6 +56,7 @@ general.
    :maxdepth: 1
 
    devlink-dpipe
+   devlink-defaults
    devlink-eswitch-attr
    devlink-flash
    devlink-health
net/devlink/core.c +227 -0

258 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#4 lore raw

Re: [PATCH net-next V4 3/6] devlink: Parse eswitch mode boot defaults

Quoted 138 lines · Mon, Jun 29, 2026 at 08:20:58PM +0200, [email protected] wrote:

Add devlink_eswitch_mode= kernel command line parsing for a default eswitch mode.

The supported syntax selects either all devlink handles or one explicit comma-separated handle list:

devlink_eswitch_mode=*=<mode>

devlink_eswitch_mode=<handle>[,<handle>...]=<mode>

where <mode> is one of legacy, switchdev or switchdev_inactive. All selected handles receive the same mode. Assigning different modes to different handle lists in the same parameter value is not supported.

Store the parsed selector and mode in devlink core so the default can be applied by a downstream patch.

Document the devlink_eswitch_mode= syntax and duplicate handle handling.

---

.../admin-guide/kernel-parameters.txt         |  25 ++
.../networking/devlink/devlink-defaults.rst   |  78 ++++++
Documentation/networking/devlink/index.rst    |   1 +
net/devlink/core.c                            | 227 ++++++++++++++++++
4 files changed, 331 insertions(+)

create mode 100644 Documentation/networking/devlink/devlink-defaults.rst

Documentation/admin-guide/kernel-parameters.txt +25 -0
index b5493a7f8f22..117300dd589c 100644
@@ -1249,6 +1249,31 @@ Kernel parameters
 	dell_smm_hwmon.fan_max=
 			[HW] Maximum configurable fan speed.
 
+	devlink_eswitch_mode=
+			[NET]
+			Format:
+			<selector>=<mode>
+
+			<selector>:
+			* | <handle>[,<handle>...]
+
+			<handle>:
+			<bus-name>/<dev-name>
+
+			Configure default devlink eswitch mode for matching
+			devlink instances during device initialization.
+
+			<mode>:
+			legacy | switchdev | switchdev_inactive
+
+			Examples:
+			devlink_eswitch_mode=*=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive
+
+			See Documentation/networking/devlink/devlink-defaults.rst
+			for the full syntax.
+
 	dfltcc=		[HW,S390]
 			Format: { on | off | def_only | inf_only | always }
 			on:       s390 zlib hardware support for compression on
Documentation/networking/devlink/devlink-defaults.rst new file +68 -0
new file mode 100644
index 000000000000..380c9e99210e
@@ -0,0 +1,78 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==============================
+Devlink Eswitch Mode Defaults
+==============================
+
+Devlink eswitch mode defaults allow the eswitch mode to be provided on the
+kernel command line and applied to matching devlink instances during device
+initialization.
+
+The devlink device is selected by its devlink handle. For PCI devices this is
+the same handle shown by ``devlink dev show``, for example
+``pci/0000:08:00.0``.
+
+Kernel command line syntax
+==========================
+
+Defaults are specified with the ``devlink_eswitch_mode=`` kernel command line
+parameter.
+
+The general syntax is::
+
+  devlink_eswitch_mode=<selector>=<mode>
+
+``<selector>`` is either ``*`` or one or more devlink handles::
+
+  * | <bus-name>/<dev-name>[,<bus-name>/<dev-name>...]
+
+``*`` applies the mode to every devlink instance. All handles in the same
+selector receive the same eswitch mode.
+
+``<mode>`` is one of ``legacy``, ``switchdev`` or ``switchdev_inactive``.
+
+Syntax rules
+------------
+
+The following syntax rules apply:
+
+* Specify the default in one ``devlink_eswitch_mode=`` parameter. Repeated
+  ``devlink_eswitch_mode=`` parameters are not accumulated.
+* The ``devlink_eswitch_mode=`` value is limited by the kernel command line
+  size.
+* Whitespace is not allowed within the parameter value.
+* ``<selector>`` must be either ``*`` or a handle list. ``*`` cannot be
+  combined with explicit handles.
+* ``<bus-name>`` and ``<dev-name>`` must not be empty.
+* ``<dev-name>`` may contain ``:``. This allows PCI names such as
+  ``0000:08:00.0``.
+* Handles must not contain whitespace, ``*``, ``=`` or more than one ``/``.
+* A comma separates handles.
+* Comma-separated default assignments are not supported.
+* Duplicate handles are rejected and the devlink eswitch mode default is
+  ignored.
+
+The eswitch mode default corresponds to the userspace command::
+
+  devlink dev eswitch set <handle> mode <value>
+
+
+Examples
+========
+
+Set all devlink instances to switchdev mode::
+
+  devlink_eswitch_mode=*=switchdev
+
+Set one PCI devlink instance to switchdev mode::
+

Documentation/networking/devlink/devlink-defaults.rst line 78

Quoted 10 lines
Documentation/networking/devlink/devlink-defaults.rst new file +10 -0
+  devlink_eswitch_mode=pci/0000:08:00.0=switchdev
+
+Set two PCI devlink instances to switchdev inactive mode::
+
+  devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive
+
+The following is invalid because comma-separated default assignments are not
+supported::
+
+  devlink_eswitch_mode=pci/0000:08:00.0=switchdev,pci/0000:09:00.0=switchdev_inactive

Interesting. I would think that this is something user may want to set for some usecases, no?

net/devlink/core.c

Quoted 13 lines
Documentation/networking/devlink/index.rst +1 -0
index 32f70879ddd0..93f09cb18c44 100644
@@ -56,6 +56,7 @@ general.
   :maxdepth: 1
 
   devlink-dpipe
+   devlink-defaults
   devlink-eswitch-attr
   devlink-flash
   devlink-health
net/devlink/core.c +0 -0

Wanna have this in a separate file perhaps? "default.c"?

Quoted 239 lines

index fe9f6a0a67d5..5126509a9c4e 100644

net/devlink/core.c +211 -0
@@ -4,6 +4,10 @@
 * Copyright (c) 2016 Jiri Pirko <[email protected]>
 */
 
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/string.h>
 #include <net/genetlink.h>
 #define CREATE_TRACE_POINTS
 #include <trace/events/devlink.h>
@@ -16,6 +20,193 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
 
 DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
 
+static char *devlink_default_esw_mode_param;
+static bool devlink_default_esw_mode_match_all;
+static enum devlink_eswitch_mode devlink_default_esw_mode;
+static LIST_HEAD(devlink_default_esw_mode_nodes);
+
+struct devlink_default_esw_mode_node {
+	struct list_head list;
+	char *bus_name;
+	char *dev_name;
+};
+
+static int __init
+devlink_default_esw_mode_to_value(const char *str,
+				  enum devlink_eswitch_mode *mode)
+{
+	if (!strcmp(str, "legacy")) {
+		*mode = DEVLINK_ESWITCH_MODE_LEGACY;
+		return 0;
+	}
+	if (!strcmp(str, "switchdev")) {
+		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
+		return 0;
+	}
+	if (!strcmp(str, "switchdev_inactive")) {
+		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV_INACTIVE;
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static int __init
+devlink_default_esw_mode_handle_parse(char *handle, char **bus_name,
+				      char **dev_name)
+{
+	char *slash;
+	char *p;
+
+	if (!*handle)
+		return -EINVAL;
+
+	for (p = handle; *p; p++) {
+		if (*p == '*' || *p == '=')
+			return -EINVAL;
+	}
+
+	slash = strchr(handle, '/');
+	if (!slash || slash == handle || !slash[1])
+		return -EINVAL;
+	if (strchr(slash + 1, '/'))
+		return -EINVAL;
+
+	*slash = '\0';
+
+	*bus_name = handle;
+	*dev_name = slash + 1;
+	return 0;
+}
+
+static struct devlink_default_esw_mode_node *
+devlink_default_esw_mode_node_find(const char *bus_name, const char *dev_name)
+{
+	struct devlink_default_esw_mode_node *node;
+
+	list_for_each_entry(node, &devlink_default_esw_mode_nodes, list) {
+		if (!strcmp(node->bus_name, bus_name) &&
+		    !strcmp(node->dev_name, dev_name))
+			return node;
+	}
+
+	return NULL;
+}
+
+static int __init
+devlink_default_esw_mode_node_add(const char *bus_name, const char *dev_name)
+{
+	struct devlink_default_esw_mode_node *node;
+
+	if (devlink_default_esw_mode_node_find(bus_name, dev_name))
+		return -EEXIST;
+
+	node = kzalloc_obj(*node);
+	if (!node)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&node->list);
+	node->bus_name = kstrdup(bus_name, GFP_KERNEL);
+	node->dev_name = kstrdup(dev_name, GFP_KERNEL);
+	if (!node->bus_name || !node->dev_name) {
+		kfree(node->bus_name);
+		kfree(node->dev_name);
+		kfree(node);
+		return -ENOMEM;
+	}
+
+	list_add_tail(&node->list, &devlink_default_esw_mode_nodes);
+	return 0;
+}
+
+static int __init devlink_default_esw_mode_handles_parse(char *handles)
+{
+	char *handle;
+	int err;
+
+	if (!strcmp(handles, "*")) {
+		devlink_default_esw_mode_match_all = true;
+		return 0;
+	}
+
+	while ((handle = strsep(&handles, ",")) != NULL) {
+		char *bus_name;
+		char *dev_name;
+
+		err = devlink_default_esw_mode_handle_parse(handle, &bus_name,
+							    &dev_name);
+		if (err)
+			return err;
+
+		err = devlink_default_esw_mode_node_add(bus_name, dev_name);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
+static void __init
+devlink_default_esw_mode_node_free(struct devlink_default_esw_mode_node *node)
+{
+	kfree(node->bus_name);
+	kfree(node->dev_name);
+	kfree(node);
+}
+
+static void __init devlink_default_esw_mode_nodes_clear(void)
+{
+	struct devlink_default_esw_mode_node *node;
+	struct devlink_default_esw_mode_node *node_tmp;
+
+	list_for_each_entry_safe(node, node_tmp,
+				 &devlink_default_esw_mode_nodes, list) {
+		list_del(&node->list);
+		devlink_default_esw_mode_node_free(node);
+	}
+
+	devlink_default_esw_mode_match_all = false;
+}
+
+static int __init devlink_default_esw_mode_parse(char *str)
+{
+	char *handles;
+	char *separator;
+	char *mode;
+	enum devlink_eswitch_mode esw_mode;
+	int err;
+
+	if (!*str)
+		return -EINVAL;
+
+	separator = strrchr(str, '=');
+	if (!separator || separator == str || !separator[1])
+		return -EINVAL;
+
+	*separator = '\0';
+	handles = str;
+	mode = separator + 1;
+
+	err = devlink_default_esw_mode_to_value(mode, &esw_mode);
+	if (err)
+		return err;
+
+	err = devlink_default_esw_mode_handles_parse(handles);
+	if (err)
+		devlink_default_esw_mode_nodes_clear();
+	else
+		devlink_default_esw_mode = esw_mode;
+
+	return err;
+}
+
+static int __init devlink_default_esw_mode_setup(char *str)
+{
+	devlink_default_esw_mode_param = str;
+	return 1;
+}
+__setup("devlink_eswitch_mode=", devlink_default_esw_mode_setup);
+
 static struct devlink *devlinks_xa_get(unsigned long index)
 {
 	struct devlink *devlink;
@@ -382,6 +573,14 @@ struct devlink *devlinks_xa_lookup_get(struct net *net, unsigned long index)
 /**
 * devl_register - Register devlink instance
 * @devlink: devlink
+ *
+ * Make @devlink visible to userspace. Drivers must call this only after the
+ * instance is fully initialized and its devlink operations can be called.
+ *
+ * Context: Caller must hold the devlink instance lock. Use devlink_register()
+ * when the lock is not already held.
+ *
+ * Return: 0 on success.
 */
 int devl_register(struct devlink *devlink)
 {
@@ -580,6 +779,31 @@ static int __init devlink_init(void)
 {
 	int err;
 
+	if (devlink_default_esw_mode_param) {
+		char *def;
+
+		def = kstrdup(devlink_default_esw_mode_param, GFP_KERNEL);
+		if (!def) {
+			devlink_default_esw_mode_param = NULL;
+			pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+		} else {
+			err = devlink_default_esw_mode_parse(def);
+			kfree(def);
+			if (err == -EEXIST) {
+				devlink_default_esw_mode_param = NULL;

net/devlink/core.c static int __init devlink_init(void) · line 803

Quoted 10 lines
net/devlink/core.c +10 -0
+				pr_warn("devlink: duplicate eswitch mode handles ignored\n");
+			} else if (err == -EINVAL) {
+				devlink_default_esw_mode_param = NULL;
+				pr_warn("devlink: invalid devlink_eswitch_mode parameter ignored\n");
+			} else if (err == -ENOMEM) {
+				devlink_default_esw_mode_param = NULL;
+				pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+			} else if (err) {
+				goto out;
+			}

Move this to a separate helper alongside the other "default" functions?

Quoted 20 lines
net/devlink/core.c +3 -0
+		}
+	}
+
 	err = register_pernet_subsys(&devlink_pernet_ops);
 	if (err)
 		goto out;
@@ -595,7 +819,10 @@ static int __init devlink_init(void)

out_unreg_pernet_subsys:

	unregister_pernet_subsys(&devlink_pernet_ops);

out:

net/devlink/core.c +2 -0
+	if (err)
+		devlink_default_esw_mode_nodes_clear();
	WARN_ON(err);
+
	return err;
}
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

Re: [PATCH net-next V4 3/6] devlink: Parse eswitch mode boot defaults

Quoted 414 lines · Jiri Pirko
Quoted 138 lines · Mon, Jun 29, 2026 at 08:20:58PM +0200, [email protected] wrote:

Add devlink_eswitch_mode= kernel command line parsing for a default eswitch mode.

The supported syntax selects either all devlink handles or one explicit comma-separated handle list:

devlink_eswitch_mode=*=<mode>

devlink_eswitch_mode=<handle>[,<handle>...]=<mode>

where <mode> is one of legacy, switchdev or switchdev_inactive. All selected handles receive the same mode. Assigning different modes to different handle lists in the same parameter value is not supported.

Store the parsed selector and mode in devlink core so the default can be applied by a downstream patch.

Document the devlink_eswitch_mode= syntax and duplicate handle handling.

---

.../admin-guide/kernel-parameters.txt         |  25 ++
.../networking/devlink/devlink-defaults.rst   |  78 ++++++
Documentation/networking/devlink/index.rst    |   1 +
net/devlink/core.c                            | 227 ++++++++++++++++++
4 files changed, 331 insertions(+)

create mode 100644 Documentation/networking/devlink/devlink-defaults.rst

Documentation/admin-guide/kernel-parameters.txt +25 -0
index b5493a7f8f22..117300dd589c 100644
@@ -1249,6 +1249,31 @@ Kernel parameters
 	dell_smm_hwmon.fan_max=
 			[HW] Maximum configurable fan speed.
 
+	devlink_eswitch_mode=
+			[NET]
+			Format:
+			<selector>=<mode>
+
+			<selector>:
+			* | <handle>[,<handle>...]
+
+			<handle>:
+			<bus-name>/<dev-name>
+
+			Configure default devlink eswitch mode for matching
+			devlink instances during device initialization.
+
+			<mode>:
+			legacy | switchdev | switchdev_inactive
+
+			Examples:
+			devlink_eswitch_mode=*=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive
+
+			See Documentation/networking/devlink/devlink-defaults.rst
+			for the full syntax.
+
 	dfltcc=		[HW,S390]
 			Format: { on | off | def_only | inf_only | always }
 			on:       s390 zlib hardware support for compression on
Documentation/networking/devlink/devlink-defaults.rst new file +68 -0
new file mode 100644
index 000000000000..380c9e99210e
@@ -0,0 +1,78 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==============================
+Devlink Eswitch Mode Defaults
+==============================
+
+Devlink eswitch mode defaults allow the eswitch mode to be provided on the
+kernel command line and applied to matching devlink instances during device
+initialization.
+
+The devlink device is selected by its devlink handle. For PCI devices this is
+the same handle shown by ``devlink dev show``, for example
+``pci/0000:08:00.0``.
+
+Kernel command line syntax
+==========================
+
+Defaults are specified with the ``devlink_eswitch_mode=`` kernel command line
+parameter.
+
+The general syntax is::
+
+  devlink_eswitch_mode=<selector>=<mode>
+
+``<selector>`` is either ``*`` or one or more devlink handles::
+
+  * | <bus-name>/<dev-name>[,<bus-name>/<dev-name>...]
+
+``*`` applies the mode to every devlink instance. All handles in the same
+selector receive the same eswitch mode.
+
+``<mode>`` is one of ``legacy``, ``switchdev`` or ``switchdev_inactive``.
+
+Syntax rules
+------------
+
+The following syntax rules apply:
+
+* Specify the default in one ``devlink_eswitch_mode=`` parameter. Repeated
+  ``devlink_eswitch_mode=`` parameters are not accumulated.
+* The ``devlink_eswitch_mode=`` value is limited by the kernel command line
+  size.
+* Whitespace is not allowed within the parameter value.
+* ``<selector>`` must be either ``*`` or a handle list. ``*`` cannot be
+  combined with explicit handles.
+* ``<bus-name>`` and ``<dev-name>`` must not be empty.
+* ``<dev-name>`` may contain ``:``. This allows PCI names such as
+  ``0000:08:00.0``.
+* Handles must not contain whitespace, ``*``, ``=`` or more than one ``/``.
+* A comma separates handles.
+* Comma-separated default assignments are not supported.
+* Duplicate handles are rejected and the devlink eswitch mode default is
+  ignored.
+
+The eswitch mode default corresponds to the userspace command::
+
+  devlink dev eswitch set <handle> mode <value>
+
+
+Examples
+========
+
+Set all devlink instances to switchdev mode::
+
+  devlink_eswitch_mode=*=switchdev
+
+Set one PCI devlink instance to switchdev mode::
+

Documentation/networking/devlink/devlink-defaults.rst line 78

Quoted 10 lines
Documentation/networking/devlink/devlink-defaults.rst new file +10 -0
+  devlink_eswitch_mode=pci/0000:08:00.0=switchdev
+
+Set two PCI devlink instances to switchdev inactive mode::
+
+  devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive
+
+The following is invalid because comma-separated default assignments are not
+supported::
+
+  devlink_eswitch_mode=pci/0000:08:00.0=switchdev,pci/0000:09:00.0=switchdev_inactive

Interesting. I would think that this is something user may want to set for some usecases, no?

net/devlink/core.c

Quoted 13 lines
Documentation/networking/devlink/index.rst +1 -0
index 32f70879ddd0..93f09cb18c44 100644
@@ -56,6 +56,7 @@ general.
   :maxdepth: 1
 
   devlink-dpipe
+   devlink-defaults
   devlink-eswitch-attr
   devlink-flash
   devlink-health
net/devlink/core.c +0 -0

Wanna have this in a separate file perhaps? "default.c"?

Quoted 243 lines

index fe9f6a0a67d5..5126509a9c4e 100644

net/devlink/core.c +215 -0
@@ -4,6 +4,10 @@
 * Copyright (c) 2016 Jiri Pirko <[email protected]>
 */
 
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/string.h>
 #include <net/genetlink.h>
 #define CREATE_TRACE_POINTS
 #include <trace/events/devlink.h>
@@ -16,6 +20,193 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
 
 DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
 
+static char *devlink_default_esw_mode_param;
+static bool devlink_default_esw_mode_match_all;
+static enum devlink_eswitch_mode devlink_default_esw_mode;
+static LIST_HEAD(devlink_default_esw_mode_nodes);
+
+struct devlink_default_esw_mode_node {
+	struct list_head list;
+	char *bus_name;
+	char *dev_name;
+};
+
+static int __init
+devlink_default_esw_mode_to_value(const char *str,
+				  enum devlink_eswitch_mode *mode)
+{
+	if (!strcmp(str, "legacy")) {
+		*mode = DEVLINK_ESWITCH_MODE_LEGACY;
+		return 0;
+	}
+	if (!strcmp(str, "switchdev")) {
+		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
+		return 0;
+	}
+	if (!strcmp(str, "switchdev_inactive")) {
+		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV_INACTIVE;
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static int __init
+devlink_default_esw_mode_handle_parse(char *handle, char **bus_name,
+				      char **dev_name)
+{
+	char *slash;
+	char *p;
+
+	if (!*handle)
+		return -EINVAL;
+
+	for (p = handle; *p; p++) {
+		if (*p == '*' || *p == '=')
+			return -EINVAL;
+	}
+
+	slash = strchr(handle, '/');
+	if (!slash || slash == handle || !slash[1])
+		return -EINVAL;
+	if (strchr(slash + 1, '/'))
+		return -EINVAL;
+
+	*slash = '\0';
+
+	*bus_name = handle;
+	*dev_name = slash + 1;
+	return 0;
+}
+
+static struct devlink_default_esw_mode_node *
+devlink_default_esw_mode_node_find(const char *bus_name, const char *dev_name)
+{
+	struct devlink_default_esw_mode_node *node;
+
+	list_for_each_entry(node, &devlink_default_esw_mode_nodes, list) {
+		if (!strcmp(node->bus_name, bus_name) &&
+		    !strcmp(node->dev_name, dev_name))
+			return node;
+	}
+
+	return NULL;
+}
+
+static int __init
+devlink_default_esw_mode_node_add(const char *bus_name, const char *dev_name)
+{
+	struct devlink_default_esw_mode_node *node;
+
+	if (devlink_default_esw_mode_node_find(bus_name, dev_name))
+		return -EEXIST;
+
+	node = kzalloc_obj(*node);
+	if (!node)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&node->list);
+	node->bus_name = kstrdup(bus_name, GFP_KERNEL);
+	node->dev_name = kstrdup(dev_name, GFP_KERNEL);
+	if (!node->bus_name || !node->dev_name) {
+		kfree(node->bus_name);
+		kfree(node->dev_name);
+		kfree(node);
+		return -ENOMEM;
+	}
+
+	list_add_tail(&node->list, &devlink_default_esw_mode_nodes);
+	return 0;
+}
+
+static int __init devlink_default_esw_mode_handles_parse(char *handles)
+{
+	char *handle;
+	int err;
+
+	if (!strcmp(handles, "*")) {
+		devlink_default_esw_mode_match_all = true;
+		return 0;
+	}
+
+	while ((handle = strsep(&handles, ",")) != NULL) {
+		char *bus_name;
+		char *dev_name;
+
+		err = devlink_default_esw_mode_handle_parse(handle, &bus_name,
+							    &dev_name);
+		if (err)
+			return err;
+
+		err = devlink_default_esw_mode_node_add(bus_name, dev_name);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
+static void __init
+devlink_default_esw_mode_node_free(struct devlink_default_esw_mode_node *node)
+{
+	kfree(node->bus_name);
+	kfree(node->dev_name);
+	kfree(node);
+}
+
+static void __init devlink_default_esw_mode_nodes_clear(void)
+{
+	struct devlink_default_esw_mode_node *node;
+	struct devlink_default_esw_mode_node *node_tmp;
+
+	list_for_each_entry_safe(node, node_tmp,
+				 &devlink_default_esw_mode_nodes, list) {
+		list_del(&node->list);
+		devlink_default_esw_mode_node_free(node);
+	}
+
+	devlink_default_esw_mode_match_all = false;
+}
+
+static int __init devlink_default_esw_mode_parse(char *str)
+{
+	char *handles;
+	char *separator;
+	char *mode;
+	enum devlink_eswitch_mode esw_mode;
+	int err;
+
+	if (!*str)
+		return -EINVAL;
+
+	separator = strrchr(str, '=');
+	if (!separator || separator == str || !separator[1])
+		return -EINVAL;
+
+	*separator = '\0';
+	handles = str;
+	mode = separator + 1;
+
+	err = devlink_default_esw_mode_to_value(mode, &esw_mode);
+	if (err)
+		return err;
+
+	err = devlink_default_esw_mode_handles_parse(handles);
+	if (err)
+		devlink_default_esw_mode_nodes_clear();
+	else
+		devlink_default_esw_mode = esw_mode;
+
+	return err;
+}
+
+static int __init devlink_default_esw_mode_setup(char *str)
+{
+	devlink_default_esw_mode_param = str;
+	return 1;
+}
+__setup("devlink_eswitch_mode=", devlink_default_esw_mode_setup);
+
 static struct devlink *devlinks_xa_get(unsigned long index)
 {
 	struct devlink *devlink;
@@ -382,6 +573,14 @@ struct devlink *devlinks_xa_lookup_get(struct net *net, unsigned long index)
 /**
 * devl_register - Register devlink instance
 * @devlink: devlink
+ *
+ * Make @devlink visible to userspace. Drivers must call this only after the
+ * instance is fully initialized and its devlink operations can be called.
+ *
+ * Context: Caller must hold the devlink instance lock. Use devlink_register()
+ * when the lock is not already held.
+ *
+ * Return: 0 on success.
 */
 int devl_register(struct devlink *devlink)
 {
@@ -580,6 +779,31 @@ static int __init devlink_init(void)
 {
 	int err;
 
+	if (devlink_default_esw_mode_param) {
+		char *def;
+
+		def = kstrdup(devlink_default_esw_mode_param, GFP_KERNEL);
+		if (!def) {
+			devlink_default_esw_mode_param = NULL;
+			pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+		} else {
+			err = devlink_default_esw_mode_parse(def);
+			kfree(def);
+			if (err == -EEXIST) {
+				devlink_default_esw_mode_param = NULL;
+				pr_warn("devlink: duplicate eswitch mode handles ignored\n");
+			} else if (err == -EINVAL) {
+				devlink_default_esw_mode_param = NULL;
+				pr_warn("devlink: invalid devlink_eswitch_mode parameter ignored\n");
Quoted 8 lines

net/devlink/core.c static int __init devlink_init(void) · line 803

Quoted 6 lines
net/devlink/core.c +6 -0
+			} else if (err == -ENOMEM) {
+				devlink_default_esw_mode_param = NULL;
+				pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+			} else if (err) {
+				goto out;
+			}

Move this to a separate helper alongside the other "default" functions?

I did that in the following patch. If I respin I'll do it in this one.

Mark

Quoted 22 lines
Quoted 22 lines
net/devlink/core.c +3 -0
+		}
+	}
+
 	err = register_pernet_subsys(&devlink_pernet_ops);
 	if (err)
 		goto out;
@@ -595,7 +819,10 @@ static int __init devlink_init(void)

out_unreg_pernet_subsys:

	unregister_pernet_subsys(&devlink_pernet_ops);

out:

net/devlink/core.c +2 -0
+	if (err)
+		devlink_default_esw_mode_nodes_clear();
	WARN_ON(err);
+
	return err;
}
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

Re: [PATCH net-next V4 3/6] devlink: Parse eswitch mode boot defaults

In reply to Jiri Pirko
Quoted 144 lines · Jiri Pirko
Quoted 143 lines · Mon, Jun 29, 2026 at 08:20:58PM +0200, [email protected] wrote:

Add devlink_eswitch_mode= kernel command line parsing for a default eswitch mode.

The supported syntax selects either all devlink handles or one explicit comma-separated handle list:

devlink_eswitch_mode=*=<mode>

devlink_eswitch_mode=<handle>[,<handle>...]=<mode>

where <mode> is one of legacy, switchdev or switchdev_inactive. All selected handles receive the same mode. Assigning different modes to different handle lists in the same parameter value is not supported.

Store the parsed selector and mode in devlink core so the default can be applied by a downstream patch.

Document the devlink_eswitch_mode= syntax and duplicate handle handling.

---

.../admin-guide/kernel-parameters.txt         |  25 ++
.../networking/devlink/devlink-defaults.rst   |  78 ++++++
Documentation/networking/devlink/index.rst    |   1 +
net/devlink/core.c                            | 227 ++++++++++++++++++
4 files changed, 331 insertions(+)

create mode 100644 Documentation/networking/devlink/devlink-defaults.rst

Documentation/admin-guide/kernel-parameters.txt +25 -0
index b5493a7f8f22..117300dd589c 100644
@@ -1249,6 +1249,31 @@ Kernel parameters
 	dell_smm_hwmon.fan_max=
 			[HW] Maximum configurable fan speed.
 
+	devlink_eswitch_mode=
+			[NET]
+			Format:
+			<selector>=<mode>
+
+			<selector>:
+			* | <handle>[,<handle>...]
+
+			<handle>:
+			<bus-name>/<dev-name>
+
+			Configure default devlink eswitch mode for matching
+			devlink instances during device initialization.
+
+			<mode>:
+			legacy | switchdev | switchdev_inactive
+
+			Examples:
+			devlink_eswitch_mode=*=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0=switchdev
+			devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive
+
+			See Documentation/networking/devlink/devlink-defaults.rst
+			for the full syntax.
+
 	dfltcc=		[HW,S390]
 			Format: { on | off | def_only | inf_only | always }
 			on:       s390 zlib hardware support for compression on
Documentation/networking/devlink/devlink-defaults.rst new file +73 -0
new file mode 100644
index 000000000000..380c9e99210e
@@ -0,0 +1,78 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+==============================
+Devlink Eswitch Mode Defaults
+==============================
+
+Devlink eswitch mode defaults allow the eswitch mode to be provided on the
+kernel command line and applied to matching devlink instances during device
+initialization.
+
+The devlink device is selected by its devlink handle. For PCI devices this is
+the same handle shown by ``devlink dev show``, for example
+``pci/0000:08:00.0``.
+
+Kernel command line syntax
+==========================
+
+Defaults are specified with the ``devlink_eswitch_mode=`` kernel command line
+parameter.
+
+The general syntax is::
+
+  devlink_eswitch_mode=<selector>=<mode>
+
+``<selector>`` is either ``*`` or one or more devlink handles::
+
+  * | <bus-name>/<dev-name>[,<bus-name>/<dev-name>...]
+
+``*`` applies the mode to every devlink instance. All handles in the same
+selector receive the same eswitch mode.
+
+``<mode>`` is one of ``legacy``, ``switchdev`` or ``switchdev_inactive``.
+
+Syntax rules
+------------
+
+The following syntax rules apply:
+
+* Specify the default in one ``devlink_eswitch_mode=`` parameter. Repeated
+  ``devlink_eswitch_mode=`` parameters are not accumulated.
+* The ``devlink_eswitch_mode=`` value is limited by the kernel command line
+  size.
+* Whitespace is not allowed within the parameter value.
+* ``<selector>`` must be either ``*`` or a handle list. ``*`` cannot be
+  combined with explicit handles.
+* ``<bus-name>`` and ``<dev-name>`` must not be empty.
+* ``<dev-name>`` may contain ``:``. This allows PCI names such as
+  ``0000:08:00.0``.
+* Handles must not contain whitespace, ``*``, ``=`` or more than one ``/``.
+* A comma separates handles.
+* Comma-separated default assignments are not supported.
+* Duplicate handles are rejected and the devlink eswitch mode default is
+  ignored.
+
+The eswitch mode default corresponds to the userspace command::
+
+  devlink dev eswitch set <handle> mode <value>
+
+
+Examples
+========
+
+Set all devlink instances to switchdev mode::
+
+  devlink_eswitch_mode=*=switchdev
+
+Set one PCI devlink instance to switchdev mode::
+
+  devlink_eswitch_mode=pci/0000:08:00.0=switchdev
+
+Set two PCI devlink instances to switchdev inactive mode::
+
+  devlink_eswitch_mode=pci/0000:08:00.0,pci/0000:09:00.1=switchdev_inactive
Quoted 8 lines

Documentation/networking/devlink/devlink-defaults.rst line 78

Quoted 5 lines
Documentation/networking/devlink/devlink-defaults.rst new file +5 -0
+
+The following is invalid because comma-separated default assignments are not
+supported::
+
+  devlink_eswitch_mode=pci/0000:08:00.0=switchdev,pci/0000:09:00.0=switchdev_inactive

Interesting. I would think that this is something user may want to set for some usecases, no?

sorry, totally missed some comments.

Wanted to keep this as simple as possible right now (the inital version was too complex), we can always add it in the future.

Quoted 9 lines
Quoted 9 lines
Documentation/networking/devlink/index.rst +0 -0
index 32f70879ddd0..93f09cb18c44 100644
@@ -56,6 +56,7 @@ general.
   :maxdepth: 1
 
Quoted 8 lines

net/devlink/core.c

Quoted 6 lines
Documentation/networking/devlink/index.rst +1 -0
   devlink-dpipe
+   devlink-defaults
   devlink-eswitch-attr
   devlink-flash
   devlink-health
net/devlink/core.c +0 -0

Wanna have this in a separate file perhaps? "default.c"?

Okay.

Mark

Quoted 275 lines
Quoted 241 lines

index fe9f6a0a67d5..5126509a9c4e 100644

net/devlink/core.c +211 -0
@@ -4,6 +4,10 @@
 * Copyright (c) 2016 Jiri Pirko <[email protected]>
 */
 
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+#include <linux/string.h>
 #include <net/genetlink.h>
 #define CREATE_TRACE_POINTS
 #include <trace/events/devlink.h>
@@ -16,6 +20,193 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_trap_report);
 
 DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
 
+static char *devlink_default_esw_mode_param;
+static bool devlink_default_esw_mode_match_all;
+static enum devlink_eswitch_mode devlink_default_esw_mode;
+static LIST_HEAD(devlink_default_esw_mode_nodes);
+
+struct devlink_default_esw_mode_node {
+	struct list_head list;
+	char *bus_name;
+	char *dev_name;
+};
+
+static int __init
+devlink_default_esw_mode_to_value(const char *str,
+				  enum devlink_eswitch_mode *mode)
+{
+	if (!strcmp(str, "legacy")) {
+		*mode = DEVLINK_ESWITCH_MODE_LEGACY;
+		return 0;
+	}
+	if (!strcmp(str, "switchdev")) {
+		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV;
+		return 0;
+	}
+	if (!strcmp(str, "switchdev_inactive")) {
+		*mode = DEVLINK_ESWITCH_MODE_SWITCHDEV_INACTIVE;
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static int __init
+devlink_default_esw_mode_handle_parse(char *handle, char **bus_name,
+				      char **dev_name)
+{
+	char *slash;
+	char *p;
+
+	if (!*handle)
+		return -EINVAL;
+
+	for (p = handle; *p; p++) {
+		if (*p == '*' || *p == '=')
+			return -EINVAL;
+	}
+
+	slash = strchr(handle, '/');
+	if (!slash || slash == handle || !slash[1])
+		return -EINVAL;
+	if (strchr(slash + 1, '/'))
+		return -EINVAL;
+
+	*slash = '\0';
+
+	*bus_name = handle;
+	*dev_name = slash + 1;
+	return 0;
+}
+
+static struct devlink_default_esw_mode_node *
+devlink_default_esw_mode_node_find(const char *bus_name, const char *dev_name)
+{
+	struct devlink_default_esw_mode_node *node;
+
+	list_for_each_entry(node, &devlink_default_esw_mode_nodes, list) {
+		if (!strcmp(node->bus_name, bus_name) &&
+		    !strcmp(node->dev_name, dev_name))
+			return node;
+	}
+
+	return NULL;
+}
+
+static int __init
+devlink_default_esw_mode_node_add(const char *bus_name, const char *dev_name)
+{
+	struct devlink_default_esw_mode_node *node;
+
+	if (devlink_default_esw_mode_node_find(bus_name, dev_name))
+		return -EEXIST;
+
+	node = kzalloc_obj(*node);
+	if (!node)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&node->list);
+	node->bus_name = kstrdup(bus_name, GFP_KERNEL);
+	node->dev_name = kstrdup(dev_name, GFP_KERNEL);
+	if (!node->bus_name || !node->dev_name) {
+		kfree(node->bus_name);
+		kfree(node->dev_name);
+		kfree(node);
+		return -ENOMEM;
+	}
+
+	list_add_tail(&node->list, &devlink_default_esw_mode_nodes);
+	return 0;
+}
+
+static int __init devlink_default_esw_mode_handles_parse(char *handles)
+{
+	char *handle;
+	int err;
+
+	if (!strcmp(handles, "*")) {
+		devlink_default_esw_mode_match_all = true;
+		return 0;
+	}
+
+	while ((handle = strsep(&handles, ",")) != NULL) {
+		char *bus_name;
+		char *dev_name;
+
+		err = devlink_default_esw_mode_handle_parse(handle, &bus_name,
+							    &dev_name);
+		if (err)
+			return err;
+
+		err = devlink_default_esw_mode_node_add(bus_name, dev_name);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
+static void __init
+devlink_default_esw_mode_node_free(struct devlink_default_esw_mode_node *node)
+{
+	kfree(node->bus_name);
+	kfree(node->dev_name);
+	kfree(node);
+}
+
+static void __init devlink_default_esw_mode_nodes_clear(void)
+{
+	struct devlink_default_esw_mode_node *node;
+	struct devlink_default_esw_mode_node *node_tmp;
+
+	list_for_each_entry_safe(node, node_tmp,
+				 &devlink_default_esw_mode_nodes, list) {
+		list_del(&node->list);
+		devlink_default_esw_mode_node_free(node);
+	}
+
+	devlink_default_esw_mode_match_all = false;
+}
+
+static int __init devlink_default_esw_mode_parse(char *str)
+{
+	char *handles;
+	char *separator;
+	char *mode;
+	enum devlink_eswitch_mode esw_mode;
+	int err;
+
+	if (!*str)
+		return -EINVAL;
+
+	separator = strrchr(str, '=');
+	if (!separator || separator == str || !separator[1])
+		return -EINVAL;
+
+	*separator = '\0';
+	handles = str;
+	mode = separator + 1;
+
+	err = devlink_default_esw_mode_to_value(mode, &esw_mode);
+	if (err)
+		return err;
+
+	err = devlink_default_esw_mode_handles_parse(handles);
+	if (err)
+		devlink_default_esw_mode_nodes_clear();
+	else
+		devlink_default_esw_mode = esw_mode;
+
+	return err;
+}
+
+static int __init devlink_default_esw_mode_setup(char *str)
+{
+	devlink_default_esw_mode_param = str;
+	return 1;
+}
+__setup("devlink_eswitch_mode=", devlink_default_esw_mode_setup);
+
 static struct devlink *devlinks_xa_get(unsigned long index)
 {
 	struct devlink *devlink;
@@ -382,6 +573,14 @@ struct devlink *devlinks_xa_lookup_get(struct net *net, unsigned long index)
 /**
 * devl_register - Register devlink instance
 * @devlink: devlink
+ *
+ * Make @devlink visible to userspace. Drivers must call this only after the
+ * instance is fully initialized and its devlink operations can be called.
+ *
+ * Context: Caller must hold the devlink instance lock. Use devlink_register()
+ * when the lock is not already held.
+ *
+ * Return: 0 on success.
 */
 int devl_register(struct devlink *devlink)
 {
@@ -580,6 +779,31 @@ static int __init devlink_init(void)
 {
 	int err;
 
+	if (devlink_default_esw_mode_param) {
+		char *def;
+
+		def = kstrdup(devlink_default_esw_mode_param, GFP_KERNEL);
+		if (!def) {
+			devlink_default_esw_mode_param = NULL;
+			pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+		} else {
+			err = devlink_default_esw_mode_parse(def);
+			kfree(def);
+			if (err == -EEXIST) {
+				devlink_default_esw_mode_param = NULL;

net/devlink/core.c static int __init devlink_init(void) · line 803

Quoted 10 lines
net/devlink/core.c +10 -0
+				pr_warn("devlink: duplicate eswitch mode handles ignored\n");
+			} else if (err == -EINVAL) {
+				devlink_default_esw_mode_param = NULL;
+				pr_warn("devlink: invalid devlink_eswitch_mode parameter ignored\n");
+			} else if (err == -ENOMEM) {
+				devlink_default_esw_mode_param = NULL;
+				pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+			} else if (err) {
+				goto out;
+			}

Move this to a separate helper alongside the other "default" functions?

Quoted 20 lines
net/devlink/core.c +3 -0
+		}
+	}
+
 	err = register_pernet_subsys(&devlink_pernet_ops);
 	if (err)
 		goto out;
@@ -595,7 +819,10 @@ static int __init devlink_init(void)

out_unreg_pernet_subsys:

	unregister_pernet_subsys(&devlink_pernet_ops);

out:

net/devlink/core.c +2 -0
+	if (err)
+		devlink_default_esw_mode_nodes_clear();
	WARN_ON(err);
+
	return err;
}
Signature · 1 line
2.43.0
author maintainer
#7 lore raw

PATCH v4 4/6 devlink: Apply eswitch mode boot defaults

In reply to Mark Bloch

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

---
 net/devlink/core.c          | 198 +++++++++++++++++++++++++++++++-----
 net/devlink/dev.c           |   6 ++
 net/devlink/devl_internal.h |   5 +
 3 files changed, 182 insertions(+), 27 deletions(-)
net/devlink/core.c +171 -27
index 5126509a9c4e..998e4ffd5dce 100644
@@ -5,6 +5,7 @@
  */
 
 #include <linux/init.h>
+#include <linux/jiffies.h>
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -22,8 +23,12 @@ DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
 
 static char *devlink_default_esw_mode_param;
 static bool devlink_default_esw_mode_match_all;
+static bool devlink_default_esw_mode_enabled;
 static enum devlink_eswitch_mode devlink_default_esw_mode;
 static LIST_HEAD(devlink_default_esw_mode_nodes);
+static struct workqueue_struct *devlink_default_esw_mode_wq;
+
+#define DEVLINK_DEFAULT_ESW_MODE_APPLY_DELAY msecs_to_jiffies(100)
 
 struct devlink_default_esw_mode_node {
 	struct list_head list;
@@ -166,6 +171,7 @@ static void __init devlink_default_esw_mode_nodes_clear(void)
 	}
 
 	devlink_default_esw_mode_match_all = false;
+	devlink_default_esw_mode_enabled = false;
 }
 
 static int __init devlink_default_esw_mode_parse(char *str)
@@ -192,14 +198,113 @@ static int __init devlink_default_esw_mode_parse(char *str)
 		return err;
 
 	err = devlink_default_esw_mode_handles_parse(handles);
-	if (err)
+	if (err) {
 		devlink_default_esw_mode_nodes_clear();
-	else
+	} else {
 		devlink_default_esw_mode = esw_mode;
+		devlink_default_esw_mode_enabled = true;
+	}
 
 	return err;
 }
 
+static bool devlink_default_esw_mode_match(struct devlink *devlink)
+{
+	const char *bus_name = devlink_bus_name(devlink);
+	const char *dev_name = devlink_dev_name(devlink);
+	struct devlink_default_esw_mode_node *node;
+
+	if (devlink_default_esw_mode_match_all)
+		return true;
+
+	node = devlink_default_esw_mode_node_find(bus_name, dev_name);
+	return !!node;
+}
+
+void devlink_default_esw_mode_apply(struct devlink *devlink)
+{
+	const struct devlink_ops *ops = devlink->ops;
+	int err;
+
+	devl_assert_locked(devlink);
+
+	if (!devlink_default_esw_mode_match(devlink))
+		return;
+
+	if (!ops->eswitch_mode_set) {
+		if (!devlink_default_esw_mode_match_all)
+			devl_warn(devlink,
+				  "devlink_eswitch_mode= selected this device but eswitch mode setting is not supported\n");
+		return;
+	}
+
+	err = devlink_eswitch_mode_set(devlink, devlink_default_esw_mode, NULL);
+	if (err)
+		devl_warn(devlink,
+			  "Couldn't apply default eswitch mode, err %d\n",
+			  err);
+}
+
+static void
+devlink_default_esw_mode_apply_queue(struct devlink *devlink,
+				     unsigned long delay)
+{
+	if (!devlink_default_esw_mode_enabled || !devlink_default_esw_mode_wq)
+		return;
+	if (!devlink_try_get(devlink))
+		return;
+	if (!queue_delayed_work(devlink_default_esw_mode_wq,
+				&devlink->default_esw_mode_apply_dw,
+				delay))
+		devlink_put(devlink);
+}
+
+static void devlink_default_esw_mode_apply_work(struct work_struct *work)
+{
+	unsigned long delay = DEVLINK_DEFAULT_ESW_MODE_APPLY_DELAY;
+	struct delayed_work *dwork = to_delayed_work(work);
+	struct devlink *devlink;
+
+	devlink = container_of(dwork, struct devlink,
+			       default_esw_mode_apply_dw);
+	if (!devl_trylock(devlink)) {
+		if (__devl_is_registered(devlink))
+			devlink_default_esw_mode_apply_queue(devlink, delay);
+		devlink_put(devlink);
+		return;
+	}
+
+	if (devl_is_registered(devlink) &&
+	    devlink->default_esw_mode_apply_pending) {
+		devlink_default_esw_mode_apply(devlink);
+		devlink->default_esw_mode_apply_pending = false;
+	}
+
+	devl_unlock(devlink);
+	devlink_put(devlink);
+}
+
+void devlink_default_esw_mode_apply_schedule(struct devlink *devlink)
+{
+	devl_assert_locked(devlink);
+
+	devlink->default_esw_mode_apply_pending = true;
+	devlink_default_esw_mode_apply_queue(devlink, 0);
+}
+
+void devlink_default_esw_mode_apply_disable(struct devlink *devlink)
+{
+	devl_assert_locked(devlink);
+
+	devlink->default_esw_mode_apply_pending = false;
+}
+
+static void devlink_default_esw_mode_apply_cancel(struct devlink *devlink)
+{
+	if (cancel_delayed_work_sync(&devlink->default_esw_mode_apply_dw))
+		devlink_put(devlink);
+}
+
 static int __init devlink_default_esw_mode_setup(char *str)
 {
 	devlink_default_esw_mode_param = str;
@@ -577,6 +682,12 @@ struct devlink *devlinks_xa_lookup_get(struct net *net, unsigned long index)
  * Make @devlink visible to userspace. Drivers must call this only after the
  * instance is fully initialized and its devlink operations can be called.
  *
+ * If a matching devlink_eswitch_mode= default was provided on the kernel
+ * command line, devlink core schedules async work to apply it after
+ * registration. Drivers implementing eswitch_mode_set() must therefore be
+ * ready to perform the same work as a userspace eswitch mode set request from
+ * this point, including creation of representors and other eswitch state.
+ *
  * Context: Caller must hold the devlink instance lock. Use devlink_register()
  * when the lock is not already held.
  *
@@ -590,6 +701,7 @@ int devl_register(struct devlink *devlink)
 	xa_set_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
 	devlink_notify_register(devlink);
 	devlink_rel_nested_in_notify(devlink);
+	devlink_default_esw_mode_apply_schedule(devlink);
 
 	return 0;
 }
@@ -612,6 +724,7 @@ void devl_unregister(struct devlink *devlink)
 	ASSERT_DEVLINK_REGISTERED(devlink);
 	devl_assert_locked(devlink);
 
+	devlink_default_esw_mode_apply_disable(devlink);
 	devlink_notify_unregister(devlink);
 	xa_clear_mark(&devlinks, devlink->index, DEVLINK_REGISTERED);
 	devlink_rel_put(devlink);
@@ -673,6 +786,9 @@ struct devlink *__devlink_alloc(const struct devlink_ops *ops, size_t priv_size,
 	INIT_LIST_HEAD(&devlink->trap_group_list);
 	INIT_LIST_HEAD(&devlink->trap_policer_list);
 	INIT_RCU_WORK(&devlink->rwork, devlink_release);
+	INIT_DELAYED_WORK(&devlink->default_esw_mode_apply_dw,
+			  devlink_default_esw_mode_apply_work);
+	devlink->default_esw_mode_apply_pending = true;
 	lockdep_register_key(&devlink->lock_key);
 	mutex_init(&devlink->lock);
 	lockdep_set_class(&devlink->lock, &devlink->lock_key);
@@ -716,6 +832,7 @@ EXPORT_SYMBOL_GPL(devlink_alloc_ns);
 void devlink_free(struct devlink *devlink)
 {
 	ASSERT_DEVLINK_NOT_REGISTERED(devlink);
+	devlink_default_esw_mode_apply_cancel(devlink);
 
 	devlink_rel_put(devlink);
 
@@ -775,35 +892,59 @@ static struct notifier_block devlink_port_netdevice_nb = {
 	.notifier_call = devlink_port_netdevice_event,
 };
 
-static int __init devlink_init(void)
+static int __init devlink_default_esw_mode_init(void)
 {
+	char *def;
 	int err;
 
-	if (devlink_default_esw_mode_param) {
-		char *def;
-
-		def = kstrdup(devlink_default_esw_mode_param, GFP_KERNEL);
-		if (!def) {
-			devlink_default_esw_mode_param = NULL;
-			pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
-		} else {
-			err = devlink_default_esw_mode_parse(def);
-			kfree(def);
-			if (err == -EEXIST) {
-				devlink_default_esw_mode_param = NULL;
-				pr_warn("devlink: duplicate eswitch mode handles ignored\n");
-			} else if (err == -EINVAL) {
-				devlink_default_esw_mode_param = NULL;
-				pr_warn("devlink: invalid devlink_eswitch_mode parameter ignored\n");
-			} else if (err == -ENOMEM) {
-				devlink_default_esw_mode_param = NULL;
-				pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
-			} else if (err) {
-				goto out;
-			}
-		}
+	if (!devlink_default_esw_mode_param)
+		return 0;
+
+	def = kstrdup(devlink_default_esw_mode_param, GFP_KERNEL);
+	if (!def) {
+		devlink_default_esw_mode_param = NULL;
+		pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+		return 0;
+	}
+
+	err = devlink_default_esw_mode_parse(def);
+	kfree(def);
+	if (err == -EEXIST) {
+		devlink_default_esw_mode_param = NULL;
+		pr_warn("devlink: duplicate eswitch mode handles ignored\n");
+		return 0;
+	} else if (err == -EINVAL) {
+		devlink_default_esw_mode_param = NULL;
+		pr_warn("devlink: invalid devlink_eswitch_mode parameter ignored\n");
+		return 0;
+	} else if (err == -ENOMEM) {
+		devlink_default_esw_mode_param = NULL;
+		pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate memory\n");
+		return 0;
+	} else if (err) {
+		return err;
 	}
 
+	devlink_default_esw_mode_wq = alloc_workqueue("devlink_default_esw_mode",
+						      WQ_UNBOUND | WQ_MEM_RECLAIM,
+						      0);
+	if (!devlink_default_esw_mode_wq) {
+		devlink_default_esw_mode_param = NULL;
+		devlink_default_esw_mode_nodes_clear();
+		pr_warn("devlink: devlink_eswitch_mode parameter ignored, failed to allocate workqueue\n");
+	}
+
+	return 0;
+}
+
+static int __init devlink_init(void)
+{
+	int err;
+
+	err = devlink_default_esw_mode_init();
+	if (err)
+		goto out;
+
 	err = register_pernet_subsys(&devlink_pernet_ops);
 	if (err)
 		goto out;
@@ -819,8 +960,11 @@ static int __init devlink_init(void)
 out_unreg_pernet_subsys:
 	unregister_pernet_subsys(&devlink_pernet_ops);
 out:
-	if (err)
+	if (err) {
+		if (devlink_default_esw_mode_wq)
+			destroy_workqueue(devlink_default_esw_mode_wq);
 		devlink_default_esw_mode_nodes_clear();
+	}
 	WARN_ON(err);
 
 	return err;
net/devlink/dev.c +6 -0
index 4fb02bb993c1..7f6ed52a5f73 100644
@@ -478,6 +478,11 @@ int devlink_reload(struct devlink *devlink, struct net *dest_net,
 		return err;
 
 	WARN_ON(!(*actions_performed & BIT(action)));
+	if (*actions_performed & BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT)) {
+		devlink_default_esw_mode_apply_disable(devlink);
+		devlink_default_esw_mode_apply(devlink);
+	}
+
 	/* Catch driver on updating the remote action within devlink reload */
 	WARN_ON(memcmp(remote_reload_stats, devlink->stats.remote_reload_stats,
 		       sizeof(remote_reload_stats)));
@@ -731,6 +736,7 @@ int devlink_nl_eswitch_set_doit(struct sk_buff *skb, struct genl_info *info)
 	u16 mode;
 
 	if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
+		devlink_default_esw_mode_apply_disable(devlink);
 		mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
 		err = devlink_eswitch_mode_set(devlink, mode, info->extack);
 		if (err)
net/devlink/devl_internal.h +5 -0
index 97be77d3ed42..d6ff233da974 100644
@@ -58,8 +58,10 @@ struct devlink {
 	struct mutex lock;
 	struct lock_class_key lock_key;
 	u8 reload_failed:1;
+	u8 default_esw_mode_apply_pending:1;
 	refcount_t refcount;
 	struct rcu_work rwork;
+	struct delayed_work default_esw_mode_apply_dw;
 	struct devlink_rel *rel;
 	struct xarray nested_rels;
 	char priv[] __aligned(NETDEV_ALIGN);
@@ -71,6 +73,9 @@ extern struct genl_family devlink_nl_family;
 struct devlink *__devlink_alloc(const struct devlink_ops *ops, size_t priv_size,
 				struct net *net, struct device *dev,
 				const struct device_driver *dev_driver);
+void devlink_default_esw_mode_apply(struct devlink *devlink);
+void devlink_default_esw_mode_apply_schedule(struct devlink *devlink);
+void devlink_default_esw_mode_apply_disable(struct devlink *devlink);
 
 #define devl_warn(devlink, format, args...)				\
 	do {								\
Signature · 1 line
2.43.0
maintainer
#8 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 4 lines · Mon, Jun 29, 2026 at 08:20:59PM +0200, [email protected] wrote:

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an

How so? I would assume that driver calls devl_register only after everything is up and running and ready. If not, isn't it a bug?

Quoted 11 lines

eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

Quoted 4 lines

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

These AI generated code descriptive messages are generally not very useful :(

author maintainer
#9 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 9 lines · Jiri Pirko
Quoted 4 lines · Mon, Jun 29, 2026 at 08:20:59PM +0200, [email protected] wrote:

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an

How so? I would assume that driver calls devl_register only after everything is up and running and ready. If not, isn't it a bug?

You would think so :)

Some drivers, mlx5 included, call devl_register() while holding the devlink instance lock and then finish setting up state before releasing the lock.

In v3 I tried to enforce exactly that model, move devl_register() to be the last thing the driver does. Jakub pushed back on making that a general rule. So in v4 I changed the approach. devl_register() only schedules the work, and the actual eswitch mode change can run only after the driver releases the devlink lock.

Mark

Quoted 20 lines
Quoted 12 lines

eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

Quoted 4 lines

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

These AI generated code descriptive messages are generally not very useful :(

maintainer
#10 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 19 lines · Wed, Jul 01, 2026 at 02:57:21PM +0200, [email protected] wrote:
Quoted 9 lines · Jiri Pirko
Quoted 4 lines · Mon, Jun 29, 2026 at 08:20:59PM +0200, [email protected] wrote:

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an

How so? I would assume that driver calls devl_register only after everything is up and running and ready. If not, isn't it a bug?

You would think so :)

Some drivers, mlx5 included, call devl_register() while holding the devlink instance lock and then finish setting up state before releasing the lock.

Quoted 5 lines

In v3 I tried to enforce exactly that model, move devl_register() to be the last thing the driver does. Jakub pushed back on making that a general rule. So in v4 I changed the approach. devl_register() only schedules the work, and the actual eswitch mode change can run only after the driver releases the devlink lock.

Wouldn't it make sense to use a completion instead of loop-reschedule of delayed work?

Quoted 24 lines

Mark

Quoted 20 lines
Quoted 12 lines

eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

Quoted 4 lines

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

These AI generated code descriptive messages are generally not very useful :(

author maintainer
#11 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 20 lines · Jiri Pirko
Quoted 19 lines · Wed, Jul 01, 2026 at 02:57:21PM +0200, [email protected] wrote:
Quoted 9 lines · Jiri Pirko
Quoted 4 lines · Mon, Jun 29, 2026 at 08:20:59PM +0200, [email protected] wrote:

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an

How so? I would assume that driver calls devl_register only after everything is up and running and ready. If not, isn't it a bug?

You would think so :)

Some drivers, mlx5 included, call devl_register() while holding the devlink instance lock and then finish setting up state before releasing the lock.

Quoted 8 lines
Quoted 5 lines

In v3 I tried to enforce exactly that model, move devl_register() to be the last thing the driver does. Jakub pushed back on making that a general rule. So in v4 I changed the approach. devl_register() only schedules the work, and the actual eswitch mode change can run only after the driver releases the devlink lock.

Wouldn't it make sense to use a completion instead of loop-reschedule of delayed work?

Just to make sure I understand the suggestion, this would mean that the work waits until the devlink lock holder drops the lock, and devl_unlock() would signal it, something like:

void devl_unlock(struct devlink *devlink)

{
	ool complete_apply = devlink->default_esw_mode_apply_pending;

	mutex_unlock(&devlink->lock);

	if (complete_apply)
		complete(&devlink->default_esw_mode_apply_ready);
}

That would avoid the retry loop, but it also means the queued work sleeps until the driver drops devl_lock. It does keep one worker blocked per pending instance and adds this default-esw-mode signalling to the generic devl_unlock() path.

The delayed retry was meant to avoid a sleeping worker and keep the instances independent. If one devlink instance is still locked, we just try it again later while other instances can progress.

If you prefer the completion approach I can switch to it, but I don't see it as simpler overall.

Mark

Quoted 25 lines
Quoted 25 lines

Mark

Quoted 20 lines
Quoted 12 lines

eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

Quoted 4 lines

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

These AI generated code descriptive messages are generally not very useful :(

maintainer
#12 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 51 lines · Wed, Jul 01, 2026 at 07:42:57PM +0200, [email protected] wrote:
Quoted 20 lines · Jiri Pirko
Quoted 19 lines · Wed, Jul 01, 2026 at 02:57:21PM +0200, [email protected] wrote:
Quoted 9 lines · Jiri Pirko
Quoted 4 lines · Mon, Jun 29, 2026 at 08:20:59PM +0200, [email protected] wrote:

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an

How so? I would assume that driver calls devl_register only after everything is up and running and ready. If not, isn't it a bug?

You would think so :)

Some drivers, mlx5 included, call devl_register() while holding the devlink instance lock and then finish setting up state before releasing the lock.

Quoted 8 lines
Quoted 5 lines

In v3 I tried to enforce exactly that model, move devl_register() to be the last thing the driver does. Jakub pushed back on making that a general rule. So in v4 I changed the approach. devl_register() only schedules the work, and the actual eswitch mode change can run only after the driver releases the devlink lock.

Wouldn't it make sense to use a completion instead of loop-reschedule of delayed work?

Just to make sure I understand the suggestion, this would mean that the work waits until the devlink lock holder drops the lock, and devl_unlock() would signal it, something like:

void devl_unlock(struct devlink *devlink)

{
	ool complete_apply = devlink->default_esw_mode_apply_pending;

	mutex_unlock(&devlink->lock);

	if (complete_apply)
		complete(&devlink->default_esw_mode_apply_ready);
}

That would avoid the retry loop, but it also means the queued work sleeps until the driver drops devl_lock. It does keep one worker blocked per pending instance and adds this default-esw-mode signalling to the generic devl_unlock() path.

Quoted 6 lines

The delayed retry was meant to avoid a sleeping worker and keep the instances independent. If one devlink instance is still locked, we just try it again later while other instances can progress.

If you prefer the completion approach I can switch to it, but I don't see it as simpler overall.

Yeah, I don't have preference. I was just wondering. Feel free to leave it as is.

Maybe, instead of "complete", you can schedule with "0" delay in devl_unlock? Well, it does not really need to be delayed work, right? The only single schedule may be done from devl_unlock. That would help to eliminate the rescheduling. Am I missing something?

Quoted 29 lines

Mark

Quoted 25 lines
Quoted 25 lines

Mark

Quoted 20 lines
Quoted 12 lines

eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

Quoted 4 lines

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

These AI generated code descriptive messages are generally not very useful :(

author maintainer
#13 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 59 lines · Jiri Pirko
Quoted 51 lines · Wed, Jul 01, 2026 at 07:42:57PM +0200, [email protected] wrote:
Quoted 20 lines · Jiri Pirko
Quoted 19 lines · Wed, Jul 01, 2026 at 02:57:21PM +0200, [email protected] wrote:
Quoted 9 lines · Jiri Pirko
Quoted 4 lines · Mon, Jun 29, 2026 at 08:20:59PM +0200, [email protected] wrote:

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an

How so? I would assume that driver calls devl_register only after everything is up and running and ready. If not, isn't it a bug?

You would think so :)

Some drivers, mlx5 included, call devl_register() while holding the devlink instance lock and then finish setting up state before releasing the lock.

Quoted 8 lines
Quoted 5 lines

In v3 I tried to enforce exactly that model, move devl_register() to be the last thing the driver does. Jakub pushed back on making that a general rule. So in v4 I changed the approach. devl_register() only schedules the work, and the actual eswitch mode change can run only after the driver releases the devlink lock.

Wouldn't it make sense to use a completion instead of loop-reschedule of delayed work?

Just to make sure I understand the suggestion, this would mean that the work waits until the devlink lock holder drops the lock, and devl_unlock() would signal it, something like:

void devl_unlock(struct devlink *devlink)

{
	ool complete_apply = devlink->default_esw_mode_apply_pending;

	mutex_unlock(&devlink->lock);

	if (complete_apply)
		complete(&devlink->default_esw_mode_apply_ready);
}

That would avoid the retry loop, but it also means the queued work sleeps until the driver drops devl_lock. It does keep one worker blocked per pending instance and adds this default-esw-mode signalling to the generic devl_unlock() path.

Quoted 6 lines

The delayed retry was meant to avoid a sleeping worker and keep the instances independent. If one devlink instance is still locked, we just try it again later while other instances can progress.

If you prefer the completion approach I can switch to it, but I don't see it as simpler overall.

Quoted 7 lines

Yeah, I don't have preference. I was just wondering. Feel free to leave it as is.

Maybe, instead of "complete", you can schedule with "0" delay in devl_unlock? Well, it does not really need to be delayed work, right? The only single schedule may be done from devl_unlock. That would help to eliminate the rescheduling. Am I missing something?

Yeah, that can work.

The only part I don't really like is adding default-esw-mode specific logic to devl_unlock(). But if you are fine with that, I can switch to this approach.

There is still a small race between mutex_unlock() and queue_work(), where someone else can take devl_lock() first. So the worker may still wait on the lock, but the window should be small and we get rid of the delayed retry loop.

Mark

Quoted 31 lines
Quoted 31 lines

Mark

Quoted 25 lines
Quoted 25 lines

Mark

Quoted 20 lines
Quoted 12 lines

eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

Quoted 4 lines

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

These AI generated code descriptive messages are generally not very useful :(

maintainer
#14 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 70 lines · Fri, Jul 03, 2026 at 08:27:28PM +0200, [email protected] wrote:
Quoted 59 lines · Jiri Pirko
Quoted 51 lines · Wed, Jul 01, 2026 at 07:42:57PM +0200, [email protected] wrote:
Quoted 20 lines · Jiri Pirko
Quoted 19 lines · Wed, Jul 01, 2026 at 02:57:21PM +0200, [email protected] wrote:
Quoted 9 lines · Jiri Pirko
Quoted 4 lines · Mon, Jun 29, 2026 at 08:20:59PM +0200, [email protected] wrote:

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an

How so? I would assume that driver calls devl_register only after everything is up and running and ready. If not, isn't it a bug?

You would think so :)

Some drivers, mlx5 included, call devl_register() while holding the devlink instance lock and then finish setting up state before releasing the lock.

Quoted 8 lines
Quoted 5 lines

In v3 I tried to enforce exactly that model, move devl_register() to be the last thing the driver does. Jakub pushed back on making that a general rule. So in v4 I changed the approach. devl_register() only schedules the work, and the actual eswitch mode change can run only after the driver releases the devlink lock.

Wouldn't it make sense to use a completion instead of loop-reschedule of delayed work?

Just to make sure I understand the suggestion, this would mean that the work waits until the devlink lock holder drops the lock, and devl_unlock() would signal it, something like:

void devl_unlock(struct devlink *devlink)

{
	ool complete_apply = devlink->default_esw_mode_apply_pending;

	mutex_unlock(&devlink->lock);

	if (complete_apply)
		complete(&devlink->default_esw_mode_apply_ready);
}

That would avoid the retry loop, but it also means the queued work sleeps until the driver drops devl_lock. It does keep one worker blocked per pending instance and adds this default-esw-mode signalling to the generic devl_unlock() path.

Quoted 6 lines

The delayed retry was meant to avoid a sleeping worker and keep the instances independent. If one devlink instance is still locked, we just try it again later while other instances can progress.

If you prefer the completion approach I can switch to it, but I don't see it as simpler overall.

Quoted 7 lines

Yeah, I don't have preference. I was just wondering. Feel free to leave it as is.

Maybe, instead of "complete", you can schedule with "0" delay in devl_unlock? Well, it does not really need to be delayed work, right? The only single schedule may be done from devl_unlock. That would help to eliminate the rescheduling. Am I missing something?

Quoted 4 lines

Yeah, that can work.

The only part I don't really like is adding default-esw-mode specific logic to devl_unlock(). But if you are fine with that, I can switch to

Could be a devl_unlock_x variant? Idk.

Quoted 6 lines

this approach.

There is still a small race between mutex_unlock() and queue_work(), where someone else can take devl_lock() first. So the worker may still wait on the lock, but the window should be small and we get rid of the delayed retry loop.

No problem.

Quoted 36 lines

Mark

Quoted 31 lines
Quoted 31 lines

Mark

Quoted 25 lines
Quoted 25 lines

Mark

Quoted 20 lines
Quoted 12 lines

eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

Quoted 4 lines

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

These AI generated code descriptive messages are generally not very useful :(

maintainer
#15 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

In reply to Mark Bloch
Quoted 93 lines · Mark Bloch

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

---
 net/devlink/core.c          | 198 +++++++++++++++++++++++++++++++-----
 net/devlink/dev.c           |   6 ++
 net/devlink/devl_internal.h |   5 +
 3 files changed, 182 insertions(+), 27 deletions(-)
net/devlink/core.c +27 -2
index 5126509a9c4e..998e4ffd5dce 100644
@@ -5,6 +5,7 @@
  */
 
 #include <linux/init.h>
+#include <linux/jiffies.h>
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -22,8 +23,12 @@ DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
 
 static char *devlink_default_esw_mode_param;
 static bool devlink_default_esw_mode_match_all;
+static bool devlink_default_esw_mode_enabled;
 static enum devlink_eswitch_mode devlink_default_esw_mode;
 static LIST_HEAD(devlink_default_esw_mode_nodes);
+static struct workqueue_struct *devlink_default_esw_mode_wq;
+
+#define DEVLINK_DEFAULT_ESW_MODE_APPLY_DELAY msecs_to_jiffies(100)
 
 struct devlink_default_esw_mode_node {
 	struct list_head list;
@@ -166,6 +171,7 @@ static void __init devlink_default_esw_mode_nodes_clear(void)
 	}
 
 	devlink_default_esw_mode_match_all = false;
+	devlink_default_esw_mode_enabled = false;
 }
 
 static int __init devlink_default_esw_mode_parse(char *str)
@@ -192,14 +198,113 @@ static int __init devlink_default_esw_mode_parse(char *str)
 		return err;
 
 	err = devlink_default_esw_mode_handles_parse(handles);
-	if (err)
+	if (err) {
 		devlink_default_esw_mode_nodes_clear();
-	else
+	} else {
 		devlink_default_esw_mode = esw_mode;
+		devlink_default_esw_mode_enabled = true;
+	}
 
 	return err;
 }
 
+static bool devlink_default_esw_mode_match(struct devlink *devlink)
+{
+	const char *bus_name = devlink_bus_name(devlink);
+	const char *dev_name = devlink_dev_name(devlink);
+	struct devlink_default_esw_mode_node *node;
+
+	if (devlink_default_esw_mode_match_all)
+		return true;
+
+	node = devlink_default_esw_mode_node_find(bus_name, dev_name);
+	return !!node;
+}
+
+void devlink_default_esw_mode_apply(struct devlink *devlink)
+{
+	const struct devlink_ops *ops = devlink->ops;
+	int err;

net/devlink/core.c static int __init devlink_default_esw_mode_parse(char *str) · line 237

Quoted 10 lines
net/devlink/core.c +10 -0
+
+	devl_assert_locked(devlink);
+
+	if (!devlink_default_esw_mode_match(devlink))
+		return;
+
+	if (!ops->eswitch_mode_set) {
+		if (!devlink_default_esw_mode_match_all)
+			devl_warn(devlink,
+				  "devlink_eswitch_mode= selected this device but eswitch mode setting is not supported\n");

Not a very strong opinion on my side, but I *think* it would be more consistent to emit this warning even for devlink_default_esw_mode_match_all

/P

author maintainer
#16 lore raw

Re: [PATCH net-next V4 4/6] devlink: Apply eswitch mode boot defaults

Quoted 99 lines · Paolo Abeni
Quoted 98 lines · Mark Bloch

Apply parsed devlink_eswitch_mode= defaults after devlink registration and after successful reload.

devl_register() may still be called before the device is ready for an eswitch mode change, so keep a per-devlink delayed work item and pending flag for the registration path. Registration queues the work, and the worker tries to take the devlink instance lock.

If the lock is busy, the worker requeues itself with a delay.

For successful reloads that performed DRIVER_REINIT, devlink_reload() already holds the devlink instance lock and the driver has completed reload_up(). Clear pending work and apply the default directly from the reload path instead of queueing work.

If a user sets eswitch mode through netlink before the pending registration work runs, clear the pending flag so the queued default does not override that user request. Cancel pending default apply work when freeing the devlink instance.

---
 net/devlink/core.c          | 198 +++++++++++++++++++++++++++++++-----
 net/devlink/dev.c           |   6 ++
 net/devlink/devl_internal.h |   5 +
 3 files changed, 182 insertions(+), 27 deletions(-)
net/devlink/core.c +32 -2
index 5126509a9c4e..998e4ffd5dce 100644
@@ -5,6 +5,7 @@
  */
 
 #include <linux/init.h>
+#include <linux/jiffies.h>
 #include <linux/list.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -22,8 +23,12 @@ DEFINE_XARRAY_FLAGS(devlinks, XA_FLAGS_ALLOC);
 
 static char *devlink_default_esw_mode_param;
 static bool devlink_default_esw_mode_match_all;
+static bool devlink_default_esw_mode_enabled;
 static enum devlink_eswitch_mode devlink_default_esw_mode;
 static LIST_HEAD(devlink_default_esw_mode_nodes);
+static struct workqueue_struct *devlink_default_esw_mode_wq;
+
+#define DEVLINK_DEFAULT_ESW_MODE_APPLY_DELAY msecs_to_jiffies(100)
 
 struct devlink_default_esw_mode_node {
 	struct list_head list;
@@ -166,6 +171,7 @@ static void __init devlink_default_esw_mode_nodes_clear(void)
 	}
 
 	devlink_default_esw_mode_match_all = false;
+	devlink_default_esw_mode_enabled = false;
 }
 
 static int __init devlink_default_esw_mode_parse(char *str)
@@ -192,14 +198,113 @@ static int __init devlink_default_esw_mode_parse(char *str)
 		return err;
 
 	err = devlink_default_esw_mode_handles_parse(handles);
-	if (err)
+	if (err) {
 		devlink_default_esw_mode_nodes_clear();
-	else
+	} else {
 		devlink_default_esw_mode = esw_mode;
+		devlink_default_esw_mode_enabled = true;
+	}
 
 	return err;
 }
 
+static bool devlink_default_esw_mode_match(struct devlink *devlink)
+{
+	const char *bus_name = devlink_bus_name(devlink);
+	const char *dev_name = devlink_dev_name(devlink);
+	struct devlink_default_esw_mode_node *node;
+
+	if (devlink_default_esw_mode_match_all)
+		return true;
+
+	node = devlink_default_esw_mode_node_find(bus_name, dev_name);
+	return !!node;
+}
+
+void devlink_default_esw_mode_apply(struct devlink *devlink)
+{
+	const struct devlink_ops *ops = devlink->ops;
+	int err;
+
+	devl_assert_locked(devlink);
+
+	if (!devlink_default_esw_mode_match(devlink))
+		return;
Quoted 9 lines

net/devlink/core.c static int __init devlink_default_esw_mode_parse(char *str) · line 237

Quoted 5 lines
net/devlink/core.c +5 -0
+
+	if (!ops->eswitch_mode_set) {
+		if (!devlink_default_esw_mode_match_all)
+			devl_warn(devlink,
+				  "devlink_eswitch_mode= selected this device but eswitch mode setting is not supported\n");

Not a very strong opinion on my side, but I *think* it would be more consistent to emit this warning even for devlink_default_esw_mode_match_all

I kept it only for the explicit handle case intentionally.

With "*" most devlink instances are not expected to support eswitch mode. In the current tree I see 9 drivers that do, and many more devlink users without it.

So warning for every unsupported instance in the "*" case would be noisy and would look like errors for devices this knob was never meant for.

Mark

Quoted 2 lines

/P

author maintainer
#17 lore raw

PATCH v4 5/6 devlink: Add API to apply eswitch mode boot default

In reply to Mark Bloch

Add devl_apply_default_esw_mode() for drivers that can apply the devlink_eswitch_mode= boot default once their device is ready instead of waiting for the asynchronous registration work.

---
 include/net/devlink.h |  1 +
 net/devlink/core.c    | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/net/devlink.h +1 -0
index dd546dbd57cf..b71d282c6d52 100644
@@ -1652,6 +1652,7 @@ static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
 
 int devl_register(struct devlink *devlink);
 void devl_unregister(struct devlink *devlink);
+void devl_apply_default_esw_mode(struct devlink *devlink);
 void devlink_register(struct devlink *devlink);
 void devlink_unregister(struct devlink *devlink);
 void devlink_free(struct devlink *devlink);
net/devlink/core.c +22 -0
index 998e4ffd5dce..d8f273e1732c 100644
@@ -299,6 +299,28 @@ void devlink_default_esw_mode_apply_disable(struct devlink *devlink)
 	devlink->default_esw_mode_apply_pending = false;
 }
 
+/**
+ * devl_apply_default_esw_mode - Apply devlink eswitch mode boot default
+ * @devlink: devlink
+ *
+ * Apply a matching devlink_eswitch_mode= boot default immediately. Drivers may
+ * use this helper when the device is ready for an eswitch mode change and the
+ * caller already holds the devlink instance lock.
+ *
+ * Any pending asynchronous default apply is cleared before applying the
+ * default, so work queued by devl_register() will not apply it again.
+ *
+ * Context: Caller must hold the devlink instance lock.
+ */
+void devl_apply_default_esw_mode(struct devlink *devlink)
+{
+	devl_assert_locked(devlink);
+
+	devlink->default_esw_mode_apply_pending = false;
+	devlink_default_esw_mode_apply(devlink);
+}
+EXPORT_SYMBOL_GPL(devl_apply_default_esw_mode);
+
 static void devlink_default_esw_mode_apply_cancel(struct devlink *devlink)
 {
 	if (cancel_delayed_work_sync(&devlink->default_esw_mode_apply_dw))
Signature · 1 line
2.43.0
author maintainer
#18 lore raw

PATCH v4 6/6 net/mlx5: Apply devlink eswitch mode boot default on probe

In reply to Mark Bloch

Apply devlink_eswitch_mode= boot defaults for mlx5 after the initial probe finishes device initialization while holding the devlink instance lock.

At this point the devlink instance is registered and mlx5 can perform an eswitch mode change. Calling devl_apply_default_esw_mode() also clears any pending default apply work queued by devl_register(), so the queued work will not apply the same default again.

Keep this call in mlx5_init_one() rather than the lower-level devl-locked init helper. That helper is also used by devlink reload, and devlink core already applies the boot default after a successful DRIVER_REINIT reload.

---
 drivers/net/ethernet/mellanox/mlx5/core/main.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/main.c +13 -0
index 643b4aac2033..0712efea74cc 100644
@@ -1392,6 +1392,17 @@ static void mlx5_unload(struct mlx5_core_dev *dev)
 	mlx5_free_bfreg(dev, &dev->priv.bfreg);
 }
 
+static void mlx5_devl_apply_default_esw_mode(struct mlx5_core_dev *dev)
+{
+	struct devlink *devlink = priv_to_devlink(dev);
+
+	if (!MLX5_ESWITCH_MANAGER(dev))
+		return;
+
+	devl_assert_locked(devlink);
+	devl_apply_default_esw_mode(devlink);
+}
+
 int mlx5_init_one_devl_locked(struct mlx5_core_dev *dev)
 {
 	bool light_probe = mlx5_dev_is_lightweight(dev);
@@ -1471,6 +1482,8 @@ int mlx5_init_one(struct mlx5_core_dev *dev)
 	err = mlx5_init_one_devl_locked(dev);
 	if (err)
 		devl_unregister(devlink);
+	else
+		mlx5_devl_apply_default_esw_mode(dev);
 unlock:
 	devl_unlock(devlink);
 	return err;
Signature · 1 line
2.43.0