Skip to contentSkip to conversation

devlink: add an explicit locking API

RFC In review Jakub Kicinski 4 participants 23 messages Oct 30, 2021 – Nov 3, 2021

Subsystems (4)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

cover devlink: add an explicit locking API

This implements what I think is the right direction for devlink API overhaul. It's an early RFC/PoC because the body of code is rather large so I'd appreciate feedback early... The patches are very roughly split, the point of the posting is primarily to prove that from the driver side the conversion is an net improvement in complexity.

IMO the problems with devlink locking are caused by two things:

(1) driver has no way to block devlink calls like it does in case of netedev / rtnl_lock, note that for devlink each driver has it's own lock so contention is not an issue;

(2) sometimes devlink calls into the driver without holding its lock
- for operations which may need the driver to call devlink (port splitting, switch config, reload etc.), the circular dependency is there, the driver can't solve this problem.

This set "fixes" the ordering by allowing the driver to participate in locking. The lock order remains:

device_lock -> [devlink_mutex] -> devlink instance -> rtnl_lock

but now driver can take devlink lock itself, and _ALL_ devlink ops are locked.

The expectation is that driver will take the devlink instance lock on its probe and remove paths, hence protecting all configuration paths with the devlink instance lock.

This is clearly demonstrated by the netdevsim conversion. All entry points to driver config are protected by devlink instance lock, so the driver switches to the "I'm already holding the devlink lock" API when calling devlink. All driver locks and trickery is removed.

The part which is slightly more challanging is quiescing async entry points which need to be closed on the devlink reload path (workqueue, debugfs etc.) and which also take devlink instance lock. For that we need to be able to take refs on the devlink instance and let them clean up after themselves rather than waiting synchronously.

That last part is not 100% finished in this patch set - it works but we need the driver to take devlink_mutex (the global lock) from its probe/remove path. I hope this is good enough for an RFC, the problem is easily solved by protecting the devlink XArray with something else than devlink_mutex and/or not holding devlink_mutex around each op (so that there is no ordering between the global and instance locks). Speaking of not holding devlink_mutex around each op this patch set also opens the path for parallel ops to different devlink instances which is currently impossible because all user space calls take devlink_mutex...

The patches are on top of the cleanups I posted earlier.

Jakub Kicinski (5):
devlink: add unlocked APIs
devlink: add API for explicit locking
devlink: allow locking of all ops
netdevsim: minor code move
netdevsim: use devlink locking

 drivers/net/netdevsim/bus.c       |  19 -
 drivers/net/netdevsim/dev.c       | 450 ++++++++-------
 drivers/net/netdevsim/fib.c       |  62 +--
 drivers/net/netdevsim/netdevsim.h |   5 -
 include/net/devlink.h             |  88 +++
 net/core/devlink.c                | 875 +++++++++++++++++++++---------
 6 files changed, 996 insertions(+), 503 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

RFC 1/5 devlink: add unlocked APIs

Much noise...

---
 include/net/devlink.h |  60 ++++
 net/core/devlink.c    | 696 +++++++++++++++++++++++++++---------------
 2 files changed, 514 insertions(+), 242 deletions(-)
include/net/devlink.h +60 -0
index aab3d007c577..d8e4274e2af4 100644
@@ -1514,7 +1514,11 @@ void devlink_free(struct devlink *devlink);
 int devlink_port_register(struct devlink *devlink,
 			  struct devlink_port *devlink_port,
 			  unsigned int port_index);
+int __devlink_port_register(struct devlink *devlink,
+			    struct devlink_port *devlink_port,
+			    unsigned int port_index);
 void devlink_port_unregister(struct devlink_port *devlink_port);
+void __devlink_port_unregister(struct devlink_port *devlink_port);
 void devlink_port_type_eth_set(struct devlink_port *devlink_port,
 			       struct net_device *netdev);
 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
@@ -1530,8 +1534,11 @@ void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port,
 				   u32 controller, u16 pf, u32 sf,
 				   bool external);
 int devlink_rate_leaf_create(struct devlink_port *port, void *priv);
+int __devlink_rate_leaf_create(struct devlink_port *port, void *priv);
 void devlink_rate_leaf_destroy(struct devlink_port *devlink_port);
+void __devlink_rate_leaf_destroy(struct devlink_port *devlink_port);
 void devlink_rate_nodes_destroy(struct devlink *devlink);
+void __devlink_rate_nodes_destroy(struct devlink *devlink);
 int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
 			u32 size, u16 ingress_pools_count,
 			u16 egress_pools_count, u16 ingress_tc_count,
@@ -1567,11 +1574,23 @@ int devlink_resource_register(struct devlink *devlink,
 			      u64 resource_id,
 			      u64 parent_resource_id,
 			      const struct devlink_resource_size_params *size_params);
+int __devlink_resource_register(struct devlink *devlink,
+				const char *resource_name,
+				u64 resource_size,
+				u64 resource_id,
+				u64 parent_resource_id,
+				const struct devlink_resource_size_params *size_params);
+
 void devlink_resources_unregister(struct devlink *devlink,
 				  struct devlink_resource *resource);
+void __devlink_resources_unregister(struct devlink *devlink,
+				    struct devlink_resource *resource);
 int devlink_resource_size_get(struct devlink *devlink,
 			      u64 resource_id,
 			      u64 *p_resource_size);
+int __devlink_resource_size_get(struct devlink *devlink,
+				u64 resource_id,
+				u64 *p_resource_size);
 int devlink_dpipe_table_resource_set(struct devlink *devlink,
 				     const char *table_name, u64 resource_id,
 				     u64 resource_units);
@@ -1579,14 +1598,26 @@ void devlink_resource_occ_get_register(struct devlink *devlink,
 				       u64 resource_id,
 				       devlink_resource_occ_get_t *occ_get,
 				       void *occ_get_priv);
+void __devlink_resource_occ_get_register(struct devlink *devlink,
+					 u64 resource_id,
+					 devlink_resource_occ_get_t *occ_get,
+					 void *occ_get_priv);
 void devlink_resource_occ_get_unregister(struct devlink *devlink,
 					 u64 resource_id);
+void __devlink_resource_occ_get_unregister(struct devlink *devlink,
+					   u64 resource_id);
 int devlink_params_register(struct devlink *devlink,
 			    const struct devlink_param *params,
 			    size_t params_count);
+int __devlink_params_register(struct devlink *devlink,
+			      const struct devlink_param *params,
+			      size_t params_count);
 void devlink_params_unregister(struct devlink *devlink,
 			       const struct devlink_param *params,
 			       size_t params_count);
+void __devlink_params_unregister(struct devlink *devlink,
+				 const struct devlink_param *params,
+				 size_t params_count);
 int devlink_param_register(struct devlink *devlink,
 			   const struct devlink_param *param);
 void devlink_param_unregister(struct devlink *devlink,
@@ -1601,16 +1632,25 @@ devlink_region_create(struct devlink *devlink,
 		      const struct devlink_region_ops *ops,
 		      u32 region_max_snapshots, u64 region_size);
 struct devlink_region *
+__devlink_region_create(struct devlink *devlink,
+			const struct devlink_region_ops *ops,
+			u32 region_max_snapshots, u64 region_size);
+struct devlink_region *
 devlink_port_region_create(struct devlink_port *port,
 			   const struct devlink_port_region_ops *ops,
 			   u32 region_max_snapshots, u64 region_size);
 void devlink_region_destroy(struct devlink_region *region);
+void __devlink_region_destroy(struct devlink_region *region);
 void devlink_port_region_destroy(struct devlink_region *region);
 
 int devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
+int __devlink_region_snapshot_id_get(struct devlink *devlink, u32 *id);
 void devlink_region_snapshot_id_put(struct devlink *devlink, u32 id);
+void __devlink_region_snapshot_id_put(struct devlink *devlink, u32 id);
 int devlink_region_snapshot_create(struct devlink_region *region,
 				   u8 *data, u32 snapshot_id);
+int __devlink_region_snapshot_create(struct devlink_region *region,
+				     u8 *data, u32 snapshot_id);
 int devlink_info_serial_number_put(struct devlink_info_req *req,
 				   const char *sn);
 int devlink_info_driver_name_put(struct devlink_info_req *req,
@@ -1702,9 +1742,15 @@ void devlink_flash_update_timeout_notify(struct devlink *devlink,
 int devlink_traps_register(struct devlink *devlink,
 			   const struct devlink_trap *traps,
 			   size_t traps_count, void *priv);
+int __devlink_traps_register(struct devlink *devlink,
+			     const struct devlink_trap *traps,
+			     size_t traps_count, void *priv);
 void devlink_traps_unregister(struct devlink *devlink,
 			      const struct devlink_trap *traps,
 			      size_t traps_count);
+void __devlink_traps_unregister(struct devlink *devlink,
+				const struct devlink_trap *traps,
+				size_t traps_count);
 void devlink_trap_report(struct devlink *devlink, struct sk_buff *skb,
 			 void *trap_ctx, struct devlink_port *in_devlink_port,
 			 const struct flow_action_cookie *fa_cookie);
@@ -1712,17 +1758,31 @@ void *devlink_trap_ctx_priv(void *trap_ctx);
 int devlink_trap_groups_register(struct devlink *devlink,
 				 const struct devlink_trap_group *groups,
 				 size_t groups_count);
+int __devlink_trap_groups_register(struct devlink *devlink,
+				   const struct devlink_trap_group *groups,
+				   size_t groups_count);
 void devlink_trap_groups_unregister(struct devlink *devlink,
 				    const struct devlink_trap_group *groups,
 				    size_t groups_count);
+void __devlink_trap_groups_unregister(struct devlink *devlink,
+				      const struct devlink_trap_group *groups,
+				      size_t groups_count);
 int
 devlink_trap_policers_register(struct devlink *devlink,
 			       const struct devlink_trap_policer *policers,
 			       size_t policers_count);
+int
+__devlink_trap_policers_register(struct devlink *devlink,
+				 const struct devlink_trap_policer *policers,
+				 size_t policers_count);
 void
 devlink_trap_policers_unregister(struct devlink *devlink,
 				 const struct devlink_trap_policer *policers,
 				 size_t policers_count);
+void
+__devlink_trap_policers_unregister(struct devlink *devlink,
+				   const struct devlink_trap_policer *policers,
+				   size_t policers_count);
 
 #if IS_ENABLED(CONFIG_NET_DEVLINK)
 
net/core/devlink.c +454 -242

1,068 lines not on this page · raw message

Signature · 1 line
2.31.1
author maintainer
#2 lore raw

RFC 2/5 devlink: add API for explicit locking

In reply to Jakub Kicinski

Expose devlink instance lock and create the registration helpers as needed. The new unregistration helper does not wait for all refs to be gone, and free just gives up the reference instead of actually freeing the memory.

---
 include/net/devlink.h |  16 ++++++
 net/core/devlink.c    | 122 +++++++++++++++++++++++++++++++++++++++---
 2 files changed, 130 insertions(+), 8 deletions(-)
include/net/devlink.h +16 -0
index d8e4274e2af4..66c1951a6f0e 100644
@@ -1191,6 +1191,7 @@ enum {
 };
 
 struct devlink_ops {
+	struct module *owner;
 	/**
 	 * @supported_flash_update_params:
 	 * mask of parameters supported by the driver's .flash_update
@@ -1490,6 +1491,8 @@ void *devlink_priv(struct devlink *devlink);
 struct devlink *priv_to_devlink(void *priv);
 struct device *devlink_to_dev(const struct devlink *devlink);
 
+int lockdep_devlink_is_held(struct devlink *devlink);
+
 struct ib_device;
 
 struct net *devlink_net(const struct devlink *devlink);
@@ -1507,10 +1510,23 @@ static inline struct devlink *devlink_alloc(const struct devlink_ops *ops,
 {
 	return devlink_alloc_ns(ops, priv_size, &init_net, dev);
 }
+
+void devlink_lock(struct devlink *devlink);
+void devlink_unlock(struct devlink *devlink);
+
+void devlink_lock_reg(struct devlink *devlink);
+void devlink_unlock_reg(struct devlink *devlink);
+
+void devlink_get(struct devlink *devlink);
+bool devlink_is_alive(const struct devlink *devlink);
+
 void devlink_set_features(struct devlink *devlink, u64 features);
 void devlink_register(struct devlink *devlink);
+void __devlink_register(struct devlink *devlink);
 void devlink_unregister(struct devlink *devlink);
+void __devlink_unregister(struct devlink *devlink);
 void devlink_free(struct devlink *devlink);
+void __devlink_free(struct devlink *devlink);
 int devlink_port_register(struct devlink *devlink,
 			  struct devlink_port *devlink_port,
 			  unsigned int port_index);
net/core/devlink.c +114 -8

193 lines not on this page · raw message

Signature · 1 line
2.31.1
author maintainer
#3 lore raw

RFC 3/5 devlink: allow locking of all ops

In reply to Jakub Kicinski

Drivers using the "explicit locking" API can opt-in to have all callbacks locked by the devlink instance lock.

---
 include/net/devlink.h | 12 +++++++++
 net/core/devlink.c    | 63 ++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 68 insertions(+), 7 deletions(-)
include/net/devlink.h +12 -0
index 66c1951a6f0e..41ab6a2b63f0 100644
@@ -1190,7 +1190,19 @@ enum {
 	DEVLINK_F_RELOAD = 1UL << 0,
 };
 
+enum {
+	DEVLINK_LOCK_REF_MODE	= 1UL << 0,
+	DEVLINK_LOCK_ESWITCH	= 1UL << 1,
+	DEVLINK_LOCK_PORT_OPS	= 1UL << 2,
+	DEVLINK_LOCK_HEALTH_OPS	= 1UL << 3,
+};
+#define DEVLINK_LOCK_ALL_OPS (DEVLINK_LOCK_REF_MODE |	\
+			      DEVLINK_LOCK_ESWITCH |	\
+			      DEVLINK_LOCK_PORT_OPS |	\
+			      DEVLINK_LOCK_HEALTH_OPS)
+
 struct devlink_ops {
+	u8 lock_flags;
 	struct module *owner;
 	/**
 	 * @supported_flash_update_params:
net/core/devlink.c +56 -7

192 lines not on this page · raw message

Signature · 1 line
2.31.1
author maintainer
#4 lore raw

RFC 4/5 netdevsim: minor code move

In reply to Jakub Kicinski

Move the port add/del helpers

---
 drivers/net/netdevsim/dev.c | 54 ++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 28 deletions(-)
drivers/net/netdevsim/dev.c +26 -28
index 5db40d713d2a..b15763a8e89a 100644
@@ -567,7 +567,32 @@ static void nsim_dev_dummy_region_exit(struct nsim_dev *nsim_dev)
 	devlink_region_destroy(nsim_dev->dummy_region);
 }
 
-static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port);
+static struct nsim_dev_port *
+__nsim_dev_port_lookup(struct nsim_dev *nsim_dev, enum nsim_dev_port_type type,
+		       unsigned int port_index)
+{
+	struct nsim_dev_port *nsim_dev_port;
+
+	port_index = nsim_dev_port_index(type, port_index);
+	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
+		if (nsim_dev_port->port_index == port_index)
+			return nsim_dev_port;
+	return NULL;
+}
+
+static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
+{
+	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
+
+	list_del(&nsim_dev_port->list);
+	if (nsim_dev_port_is_vf(nsim_dev_port))
+		devlink_rate_leaf_destroy(&nsim_dev_port->devlink_port);
+	devlink_port_type_clear(devlink_port);
+	nsim_destroy(nsim_dev_port->ns);
+	nsim_dev_port_debugfs_exit(nsim_dev_port);
+	devlink_port_unregister(devlink_port);
+	kfree(nsim_dev_port);
+}
 
 static int nsim_esw_legacy_enable(struct nsim_dev *nsim_dev,
 				  struct netlink_ext_ack *extack)
@@ -1418,20 +1443,6 @@ static int __nsim_dev_port_add(struct nsim_dev *nsim_dev, enum nsim_dev_port_typ
 	return err;
 }
 
-static void __nsim_dev_port_del(struct nsim_dev_port *nsim_dev_port)
-{
-	struct devlink_port *devlink_port = &nsim_dev_port->devlink_port;
-
-	list_del(&nsim_dev_port->list);
-	if (nsim_dev_port_is_vf(nsim_dev_port))
-		devlink_rate_leaf_destroy(&nsim_dev_port->devlink_port);
-	devlink_port_type_clear(devlink_port);
-	nsim_destroy(nsim_dev_port->ns);
-	nsim_dev_port_debugfs_exit(nsim_dev_port);
-	devlink_port_unregister(devlink_port);
-	kfree(nsim_dev_port);
-}
-
 static void nsim_dev_port_del_all(struct nsim_dev *nsim_dev)
 {
 	struct nsim_dev_port *nsim_dev_port, *tmp;
@@ -1674,19 +1685,6 @@ void nsim_drv_remove(struct nsim_bus_dev *nsim_bus_dev)
 	dev_set_drvdata(&nsim_bus_dev->dev, NULL);
 }
 
-static struct nsim_dev_port *
-__nsim_dev_port_lookup(struct nsim_dev *nsim_dev, enum nsim_dev_port_type type,
-		       unsigned int port_index)
-{
-	struct nsim_dev_port *nsim_dev_port;
-
-	port_index = nsim_dev_port_index(type, port_index);
-	list_for_each_entry(nsim_dev_port, &nsim_dev->port_list, list)
-		if (nsim_dev_port->port_index == port_index)
-			return nsim_dev_port;
-	return NULL;
-}
-
 int nsim_drv_port_add(struct nsim_bus_dev *nsim_bus_dev, enum nsim_dev_port_type type,
 		      unsigned int port_index)
 {
Signature · 1 line
2.31.1
author maintainer
#5 lore raw

RFC 5/5 netdevsim: use devlink locking

In reply to Jakub Kicinski

We can remove all 3 mutexes and use devlink lock
instead.

For trap delayed work we need to take a ref on
the devlink instance and check if the instance
is still active.

Similar thing for the debugfs handlers which
need locking.

---
 drivers/net/netdevsim/bus.c       |  19 --
 drivers/net/netdevsim/dev.c       | 400 +++++++++++++++++-------------
 drivers/net/netdevsim/fib.c       |  62 ++---
 drivers/net/netdevsim/netdevsim.h |   5 -
 4 files changed, 263 insertions(+), 223 deletions(-)
drivers/net/netdevsim/bus.c +0 -19
index 25cb2e600d53..b5f4df1a07a3 100644
@@ -72,16 +72,7 @@ new_port_store(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	if (!mutex_trylock(&nsim_bus_dev->nsim_bus_reload_lock))
-		return -EBUSY;
-
-	if (nsim_bus_dev->in_reload) {
-		mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
-		return -EBUSY;
-	}
-
 	ret = nsim_drv_port_add(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
-	mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
 	return ret ? ret : count;
 }
 
@@ -102,16 +93,7 @@ del_port_store(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	if (!mutex_trylock(&nsim_bus_dev->nsim_bus_reload_lock))
-		return -EBUSY;
-
-	if (nsim_bus_dev->in_reload) {
-		mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
-		return -EBUSY;
-	}
-
 	ret = nsim_drv_port_del(nsim_bus_dev, NSIM_DEV_PORT_TYPE_PF, port_index);
-	mutex_unlock(&nsim_bus_dev->nsim_bus_reload_lock);
 	return ret ? ret : count;
 }
 
@@ -298,7 +280,6 @@ nsim_bus_dev_new(unsigned int id, unsigned int port_count, unsigned int num_queu
 	nsim_bus_dev->num_queues = num_queues;
 	nsim_bus_dev->initial_net = current->nsproxy->net_ns;
 	nsim_bus_dev->max_vfs = NSIM_BUS_DEV_MAX_VFS;
-	mutex_init(&nsim_bus_dev->nsim_bus_reload_lock);
 	/* Disallow using nsim_bus_dev */
 	smp_store_release(&nsim_bus_dev->init, false);
 
drivers/net/netdevsim/dev.c +232 -168

792 lines not on this page · raw message

drivers/net/netdevsim/fib.c +31 -31

80 lines not on this page · raw message

drivers/net/netdevsim/netdevsim.h +0 -5
index c49771f27f17..97ff4e2e72ac 100644
@@ -239,7 +239,6 @@ struct nsim_dev {
 	struct dentry *take_snapshot;
 	struct dentry *nodes_ddir;
 
-	struct mutex vfs_lock;  /* Protects vfconfigs */
 	struct nsim_vf_config *vfconfigs;
 
 	struct bpf_offload_dev *bpf_dev;
@@ -252,7 +251,6 @@ struct nsim_dev {
 	struct list_head bpf_bound_maps;
 	struct netdev_phys_item_id switch_id;
 	struct list_head port_list;
-	struct mutex port_list_lock; /* protects port list */
 	bool fw_update_status;
 	u32 fw_update_overwrite_mask;
 	u32 max_macs;
@@ -355,9 +353,6 @@ struct nsim_bus_dev {
 				  */
 	unsigned int max_vfs;
 	unsigned int num_vfs;
-	/* Lock for devlink->reload_enabled in netdevsim module */
-	struct mutex nsim_bus_reload_lock;
-	bool in_reload;
 	bool init;
 };
 
Signature · 1 line
2.31.1
#6 lore raw
In reply to Jakub Kicinski
Quoted 41 lines · Jakub Kicinski

This implements what I think is the right direction for devlink API overhaul. It's an early RFC/PoC because the body of code is rather large so I'd appreciate feedback early... The patches are very roughly split, the point of the posting is primarily to prove that from the driver side the conversion is an net improvement in complexity.

IMO the problems with devlink locking are caused by two things:

(1) driver has no way to block devlink calls like it does in case of netedev / rtnl_lock, note that for devlink each driver has it's own lock so contention is not an issue;

(2) sometimes devlink calls into the driver without holding its lock
- for operations which may need the driver to call devlink (port splitting, switch config, reload etc.), the circular dependency is there, the driver can't solve this problem.

This set "fixes" the ordering by allowing the driver to participate in locking. The lock order remains:

device_lock -> [devlink_mutex] -> devlink instance -> rtnl_lock

but now driver can take devlink lock itself, and _ALL_ devlink ops are locked.

The expectation is that driver will take the devlink instance lock on its probe and remove paths, hence protecting all configuration paths with the devlink instance lock.

This is clearly demonstrated by the netdevsim conversion. All entry points to driver config are protected by devlink instance lock, so the driver switches to the "I'm already holding the devlink lock" API when calling devlink. All driver locks and trickery is removed.

The part which is slightly more challanging is quiescing async entry points which need to be closed on the devlink reload path (workqueue, debugfs etc.) and which also take devlink instance lock. For that we need to be able to take refs on the devlink instance and let them clean up after themselves rather than waiting synchronously.

Quoted 10 lines

That last part is not 100% finished in this patch set - it works but we need the driver to take devlink_mutex (the global lock) from its probe/remove path. I hope this is good enough for an RFC, the problem is easily solved by protecting the devlink XArray with something else than devlink_mutex and/or not holding devlink_mutex around each op (so that there is no ordering between the global and instance locks). Speaking of not holding devlink_mutex around each op this patch set also opens the path for parallel ops to different devlink instances which is currently impossible because all user space calls take devlink_mutex...

No, please no.

This RFC goes against everything that I tried to make before.

It pushes complexity from the devlink core code to the drivers by hiding bugs in the drivers. The average driver author doesn't know locking well and won't be able to use devlink reference counting correctly.

Already your netdevsim conversion shows that it is hard to do and for complex drivers it will be a nightmare to convert and maintain.

At the end (long run), we will find ourselves with a maze of completely random devlink_get and devlink_lock, exactly like we have now with RTNL lock, which people add/delete based on their testing if to judge by the commit messages.

Regarding devlink_mutex, I'm on the path to removing it, this is why XArray and reference counting was used in the first place.

Our current complexity is due to the situation that devlink_reload doesn't behave like any other _set_ commands, where it supposed to take devlink->lock internally.

Please give me (testing) time and I'll post a full solution that separates _get_ from _set_ commands by changing devlink->lock to be RW semaphore. Together with logic to understand that we are in devlink_reload will give us a solution that won't require changing drivers and won't push locking burden on them.

Please, let's not give up on standalone devlink implementation without drivers need to know internal devlink details. It is hard to do but possible.

Thanks

Quoted 21 lines

The patches are on top of the cleanups I posted earlier.

Jakub Kicinski (5):
devlink: add unlocked APIs
devlink: add API for explicit locking
devlink: allow locking of all ops
netdevsim: minor code move
netdevsim: use devlink locking

 drivers/net/netdevsim/bus.c       |  19 -
 drivers/net/netdevsim/dev.c       | 450 ++++++++-------
 drivers/net/netdevsim/fib.c       |  62 +--
 drivers/net/netdevsim/netdevsim.h |   5 -
 include/net/devlink.h             |  88 +++
 net/core/devlink.c                | 875 +++++++++++++++++++++---------
 6 files changed, 996 insertions(+), 503 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#7 lore raw
Quoted 53 lines · On Sun, 31 Oct 2021 09:23:42 +0200 Leon Romanovsky wrote:
Quoted 41 lines · Jakub Kicinski

This implements what I think is the right direction for devlink API overhaul. It's an early RFC/PoC because the body of code is rather large so I'd appreciate feedback early... The patches are very roughly split, the point of the posting is primarily to prove that from the driver side the conversion is an net improvement in complexity.

IMO the problems with devlink locking are caused by two things:

(1) driver has no way to block devlink calls like it does in case of netedev / rtnl_lock, note that for devlink each driver has it's own lock so contention is not an issue;

(2) sometimes devlink calls into the driver without holding its lock
- for operations which may need the driver to call devlink (port splitting, switch config, reload etc.), the circular dependency is there, the driver can't solve this problem.

This set "fixes" the ordering by allowing the driver to participate in locking. The lock order remains:

device_lock -> [devlink_mutex] -> devlink instance -> rtnl_lock

but now driver can take devlink lock itself, and _ALL_ devlink ops are locked.

The expectation is that driver will take the devlink instance lock on its probe and remove paths, hence protecting all configuration paths with the devlink instance lock.

This is clearly demonstrated by the netdevsim conversion. All entry points to driver config are protected by devlink instance lock, so the driver switches to the "I'm already holding the devlink lock" API when calling devlink. All driver locks and trickery is removed.

The part which is slightly more challanging is quiescing async entry points which need to be closed on the devlink reload path (workqueue, debugfs etc.) and which also take devlink instance lock. For that we need to be able to take refs on the devlink instance and let them clean up after themselves rather than waiting synchronously.

Quoted 10 lines

That last part is not 100% finished in this patch set - it works but we need the driver to take devlink_mutex (the global lock) from its probe/remove path. I hope this is good enough for an RFC, the problem is easily solved by protecting the devlink XArray with something else than devlink_mutex and/or not holding devlink_mutex around each op (so that there is no ordering between the global and instance locks). Speaking of not holding devlink_mutex around each op this patch set also opens the path for parallel ops to different devlink instances which is currently impossible because all user space calls take devlink_mutex...

Quoted 3 lines

No, please no.

This RFC goes against everything that I tried to make before.

Yup, that we agree on :) I think the reload makes your approach a dead end.

Quoted 7 lines

It pushes complexity from the devlink core code to the drivers by hiding bugs in the drivers. The average driver author doesn't know locking well and won't be able to use devlink reference counting correctly.

Already your netdevsim conversion shows that it is hard to do and for complex drivers it will be a nightmare to convert and maintain.

IDK, the code is very idiomatic for the kernel. Most drivers should not have to use the ref counts, and I'll provide good enough docs to understand how to use it.

In fact drivers which don't implement currently unlocked ops require no changes between you API and this one.

I think "complex core, simple drivers" is the right trade off. With this set it is now and order of magnitude easier to reason about netdevsim's locking.

Quoted 7 lines

At the end (long run), we will find ourselves with a maze of completely random devlink_get and devlink_lock, exactly like we have now with RTNL lock, which people add/delete based on their testing if to judge by the commit messages.

Regarding devlink_mutex, I'm on the path to removing it, this is why XArray and reference counting was used in the first place.

devlink_mutex is the only thing protecting the "unlocked" calls right now, you'd be placing a lot of implicit expectations about driver's internal locking if you do that.

Quoted 9 lines

Our current complexity is due to the situation that devlink_reload doesn't behave like any other _set_ commands, where it supposed to take devlink->lock internally.

Please give me (testing) time and I'll post a full solution that separates _get_ from _set_ commands by changing devlink->lock to be RW semaphore. Together with logic to understand that we are in devlink_reload will give us a solution that won't require changing drivers and won't push locking burden on them.

How is RW semaphore going to solve the problem that ops are unlocked and have to take the instance lock from within to add/remove ports?

Quoted 2 lines

Please, let's not give up on standalone devlink implementation without drivers need to know internal devlink details. It is hard to do but possible.

We may just disagree on this one. Please answer my question above - so far IDK how you're going to fix the problem of re-reg'ing subobjects from the reload path.

My experience writing drivers is that it was painfully unclear what the devlink locking rules are. Sounds like you'd make them even more complicated.

This RFC makes the rules simple - all devlink ops are locked.

For the convenience of the drivers they can also take the instance lock whenever they want to prevent ops from being called. Experience with rtnl_lock teaches us that this is very useful for drivers.

Accessible lock + refs is how driver core works.

Let me sketch out the driver facing documentation here.

---->8------

Devlink locking guide
=====================

Basic locking
-------------

After calling devlink_register() and until devlink_unregister() is called the devlink instance is "live" and any callback can be invoked. If you want to prevent devlink from calling the driver take the instance lock (devlink_lock()), you will need to call the locked version of devlink API (prefixed with '__devlink') while holding the lock.

All devlink callbacks (including health callbacks) are locked so locked devlink API needs to be used from within them.

Note that devlink locks are per-instance (like device_lock but unlike rtnl_lock).

Reference use
-------------

Advanced use of devlink may require taking references on the instance. The primary use for this is to avoid deadlocks between reload/remove paths and async works which need to take the devlink instance lock.

devlink_get() takes a reference on the instance and should only be called when the driver is sure to already have some references.

The reference alone does _not_ prevent devlink instance from getting unregistered so you probably want to check if devlink_is_alive() before using that ref.

For example a use with a workqueue may look like this:

```
/* schedule work from a path which knows devlink is alive */
void sched_my_work()
{
	devlink_get(devlink);
	if (!schedule_work(priv->some_work))
		/* it was already scheduled */
		devlink_put(devlink);

}

void work_fn()
{
	devlink_lock(devlink);
	if (devlink_is_alive(devlink))
		perform_the_task();
	devlink_lock(devlink);

	/* give up our reference */
	devlink_put(devlink);
}
```

If you want to use devlink references from a context which does not take a reference on the driver module (e.g. workqueue) - make sure to set .owner of devlink_ops to THIS_MODULE.

#8 lore raw
Quoted 55 lines · Jakub Kicinski
Quoted 54 lines · On Sun, 31 Oct 2021 09:23:42 +0200 Leon Romanovsky wrote:
Quoted 41 lines · Jakub Kicinski

This implements what I think is the right direction for devlink API overhaul. It's an early RFC/PoC because the body of code is rather large so I'd appreciate feedback early... The patches are very roughly split, the point of the posting is primarily to prove that from the driver side the conversion is an net improvement in complexity.

IMO the problems with devlink locking are caused by two things:

(1) driver has no way to block devlink calls like it does in case of netedev / rtnl_lock, note that for devlink each driver has it's own lock so contention is not an issue;

(2) sometimes devlink calls into the driver without holding its lock
- for operations which may need the driver to call devlink (port splitting, switch config, reload etc.), the circular dependency is there, the driver can't solve this problem.

This set "fixes" the ordering by allowing the driver to participate in locking. The lock order remains:

device_lock -> [devlink_mutex] -> devlink instance -> rtnl_lock

but now driver can take devlink lock itself, and _ALL_ devlink ops are locked.

The expectation is that driver will take the devlink instance lock on its probe and remove paths, hence protecting all configuration paths with the devlink instance lock.

This is clearly demonstrated by the netdevsim conversion. All entry points to driver config are protected by devlink instance lock, so the driver switches to the "I'm already holding the devlink lock" API when calling devlink. All driver locks and trickery is removed.

The part which is slightly more challanging is quiescing async entry points which need to be closed on the devlink reload path (workqueue, debugfs etc.) and which also take devlink instance lock. For that we need to be able to take refs on the devlink instance and let them clean up after themselves rather than waiting synchronously.

Quoted 10 lines

That last part is not 100% finished in this patch set - it works but we need the driver to take devlink_mutex (the global lock) from its probe/remove path. I hope this is good enough for an RFC, the problem is easily solved by protecting the devlink XArray with something else than devlink_mutex and/or not holding devlink_mutex around each op (so that there is no ordering between the global and instance locks). Speaking of not holding devlink_mutex around each op this patch set also opens the path for parallel ops to different devlink instances which is currently impossible because all user space calls take devlink_mutex...

No, please no.

<...>

Quoted 2 lines

How is RW semaphore going to solve the problem that ops are unlocked and have to take the instance lock from within to add/remove ports?

This is three step process, but mainly it is first step. We need to make sure that functions that can re-entry will use nested locks.

Steps:
1. Use proper locking API that supports nesting:
https://lore.kernel.org/netdev/YYABqfFy%2F%2Fg5Gdis@nanopsycho/T/#mf9dc5cac2013abe413545bbe4a09cc231ae209a4
2. Convert devlink->lock to be RW semaphore:
commit 4506dd3a90a82a0b6bde238f507907747ab88407

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 24 16:54:16 2021 +0300

devlink: Convert devlink lock to be RW semaphore

This is naive conversion of devlink->lock to RW semaphore, so we will be able to differentiate commands that require exclusive access vs. parallel ready-to-run ones.

All "set" commands that used devlink->lock are converted to write lock, while all "get" commands are marked with read lock.

@@ -578,8 +584,12 @@ static int devlink_nl_pre_doit(const struct genl_ops *ops,

                mutex_unlock(&devlink_mutex);
                return PTR_ERR(devlink);
        }

- if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
- mutex_lock(&devlink->lock);
+
+ if (~ops->internal_flags & DEVLINK_NL_FLAG_SHARED_ACCESS)
+ down_write(&devlink->rwsem);
+ else
+ down_read(&devlink->rwsem);
+

3. Drop devlink_mutex:
commit 3177af9971c4cd95f9633aeb9b0434687da62fd0

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 31 16:05:40 2021 +0200

devlink: Use xarray locking mechanism instead big devlink lock

The conversion to XArray together with devlink reference counting allows us reuse the following locking pattern:
xa_lock()
xa_for_each() {
devlink_try_get()
xa_unlock()
....
xa_lock()
}

This pattern gives us a way to run any commands between xa_unlock() and xa_lock() without big devlink mutex, while making sure that devlink instance won't be released.

Steps 2 and 3 were not posted due to merge window and my desire to get mileage in our regression.

Quoted 11 lines
Quoted 3 lines

Please, let's not give up on standalone devlink implementation without drivers need to know internal devlink details. It is hard to do but possible.

We may just disagree on this one. Please answer my question above - so far IDK how you're going to fix the problem of re-reg'ing subobjects from the reload path.

My experience writing drivers is that it was painfully unclear what the devlink locking rules are. Sounds like you'd make them even more complicated.

How? I have only one rule:
* Driver author should be aware that between devlink_register() and devlink_unregister(), users can send netlink commands.

In your solution, driver authors will need to follow whole devlink how-to-use book.

Quoted 6 lines

This RFC makes the rules simple - all devlink ops are locked.

For the convenience of the drivers they can also take the instance lock whenever they want to prevent ops from being called. Experience with rtnl_lock teaches us that this is very useful for drivers.

Strange, I see completely opposite picture in the git log with so much changes that have Fixes line and add/remove/mention rtnl_lock. Maybe it is useful, but almost all if not all drivers full of fixes of rtnl_lock usage. I don't want same for the devlink.

Thanks

author maintainer
#9 lore raw
Quoted 9 lines · On Mon, 1 Nov 2021 20:36:15 +0200 Leon Romanovsky wrote:
Quoted 2 lines

How is RW semaphore going to solve the problem that ops are unlocked and have to take the instance lock from within to add/remove ports?

This is three step process, but mainly it is first step. We need to make sure that functions that can re-entry will use nested locks.

Steps:
1. Use proper locking API that supports nesting:
https://lore.kernel.org/netdev/YYABqfFy%2F%2Fg5Gdis@nanopsycho/T/#mf9dc5cac2013abe413545bbe4a09cc231ae209a4

Whether we provide the an unlocked API or allow lock nesting on the driver API is not that important to me.

Quoted 16 lines

2. Convert devlink->lock to be RW semaphore:
commit 4506dd3a90a82a0b6bde238f507907747ab88407

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 24 16:54:16 2021 +0300

devlink: Convert devlink lock to be RW semaphore

This is naive conversion of devlink->lock to RW semaphore, so we will be able to differentiate commands that require exclusive access vs. parallel ready-to-run ones.

All "set" commands that used devlink->lock are converted to write lock, while all "get" commands are marked with read lock.

(quoted diff) +0 -0
@@ -578,8 +584,12 @@ static int devlink_nl_pre_doit(const struct genl_ops *ops,
                mutex_unlock(&devlink_mutex);

Review static int devlink_nl_pre_doit(const struct genl_ops *ops, · line 592

Quoted 10 lines
(quoted diff) +6 -2
                return PTR_ERR(devlink);
        }
-       if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
-               mutex_lock(&devlink->lock);
+
+       if (~ops->internal_flags & DEVLINK_NL_FLAG_SHARED_ACCESS)
+               down_write(&devlink->rwsem);
+       else
+               down_read(&devlink->rwsem);
+

IIUC the RW sem thing is an optimization, it's besides the point.

Quoted 17 lines

3. Drop devlink_mutex:
commit 3177af9971c4cd95f9633aeb9b0434687da62fd0

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 31 16:05:40 2021 +0200

devlink: Use xarray locking mechanism instead big devlink lock

The conversion to XArray together with devlink reference counting allows us reuse the following locking pattern:
xa_lock()
xa_for_each() {
devlink_try_get()
xa_unlock()
....
xa_lock()
}

Quoted 3 lines

This pattern gives us a way to run any commands between xa_unlock() and xa_lock() without big devlink mutex, while making sure that devlink instance won't be released.

Yup, I think this part we agree on.

Quoted 2 lines

Steps 2 and 3 were not posted due to merge window and my desire to get mileage in our regression.

:)

Quoted 11 lines
Quoted 10 lines
Quoted 2 lines

Please, let's not give up on standalone devlink implementation without drivers need to know internal devlink details. It is hard to do but possible.

We may just disagree on this one. Please answer my question above - so far IDK how you're going to fix the problem of re-reg'ing subobjects from the reload path.

My experience writing drivers is that it was painfully unclear what the devlink locking rules are. Sounds like you'd make them even more complicated.

Quoted 6 lines

How? I have only one rule:
* Driver author should be aware that between devlink_register() and devlink_unregister(), users can send netlink commands.

In your solution, driver authors will need to follow whole devlink how-to-use book.

Only if they need refs. If you don't the API is the same as yours.

IOW you don't provide an API for advanced use cases at all. You force the drivers to implement their own reference counting and locking. I want them to just rely on devlink as a framework.

How many driver locks do you have in netdevsim after conversion? All 3? How do you add a port to the instance from sysfs without a AB / BA deadlock between the port lock and the devlink lock if the driver can't take the devlink lock? Are you still going to need the "in_reload" wart?

Quoted 10 lines
Quoted 5 lines

This RFC makes the rules simple - all devlink ops are locked.

For the convenience of the drivers they can also take the instance lock whenever they want to prevent ops from being called. Experience with rtnl_lock teaches us that this is very useful for drivers.

Strange, I see completely opposite picture in the git log with so much changes that have Fixes line and add/remove/mention rtnl_lock. Maybe it is useful, but almost all if not all drivers full of fixes of rtnl_lock usage. I don't want same for the devlink.

If you don't provide a locking API to the drivers you'll have to fix 2x the bugs, and each of them subtly different. At least maintainers know what rtnl_lock rules are.

#10 lore raw
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Mon, 1 Nov 2021 20:36:15 +0200 Leon Romanovsky wrote:
Quoted 2 lines

How is RW semaphore going to solve the problem that ops are unlocked and have to take the instance lock from within to add/remove ports?

This is three step process, but mainly it is first step. We need to make sure that functions that can re-entry will use nested locks.

Quoted 6 lines
Quoted 3 lines

Steps:
1. Use proper locking API that supports nesting:
https://lore.kernel.org/netdev/YYABqfFy%2F%2Fg5Gdis@nanopsycho/T/#mf9dc5cac2013abe413545bbe4a09cc231ae209a4

Whether we provide the an unlocked API or allow lock nesting on the driver API is not that important to me.

Thanks

Quoted 15 lines
Quoted 15 lines

2. Convert devlink->lock to be RW semaphore:
commit 4506dd3a90a82a0b6bde238f507907747ab88407

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 24 16:54:16 2021 +0300

devlink: Convert devlink lock to be RW semaphore

This is naive conversion of devlink->lock to RW semaphore, so we will be able to differentiate commands that require exclusive access vs. parallel ready-to-run ones.

All "set" commands that used devlink->lock are converted to write lock, while all "get" commands are marked with read lock.

Quoted 14 lines

Review static int devlink_nl_pre_doit(const struct genl_ops *ops, · line 592

Quoted 12 lines
(quoted diff) +6 -2
@@ -578,8 +584,12 @@ static int devlink_nl_pre_doit(const struct genl_ops *ops,
                mutex_unlock(&devlink_mutex);
                return PTR_ERR(devlink);
        }
-       if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
-               mutex_lock(&devlink->lock);
+
+       if (~ops->internal_flags & DEVLINK_NL_FLAG_SHARED_ACCESS)
+               down_write(&devlink->rwsem);
+       else
+               down_read(&devlink->rwsem);
+

IIUC the RW sem thing is an optimization, it's besides the point.

I need RW as a way to ensure "exclusive" access during _set_ operation. It is not an optimization, but simple way to understand if parallel access is possible at this specific point of time.

Quoted 44 lines
Quoted 18 lines

3. Drop devlink_mutex:
commit 3177af9971c4cd95f9633aeb9b0434687da62fd0

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 31 16:05:40 2021 +0200

devlink: Use xarray locking mechanism instead big devlink lock

The conversion to XArray together with devlink reference counting allows us reuse the following locking pattern:
xa_lock()
xa_for_each() {
devlink_try_get()
xa_unlock()
....
xa_lock()
}

Quoted 3 lines

This pattern gives us a way to run any commands between xa_unlock() and xa_lock() without big devlink mutex, while making sure that devlink instance won't be released.

Yup, I think this part we agree on.

Quoted 2 lines

Steps 2 and 3 were not posted due to merge window and my desire to get mileage in our regression.

:)

Quoted 15 lines
Quoted 10 lines
Quoted 2 lines

Please, let's not give up on standalone devlink implementation without drivers need to know internal devlink details. It is hard to do but possible.

We may just disagree on this one. Please answer my question above - so far IDK how you're going to fix the problem of re-reg'ing subobjects from the reload path.

My experience writing drivers is that it was painfully unclear what the devlink locking rules are. Sounds like you'd make them even more complicated.

How? I have only one rule:
* Driver author should be aware that between devlink_register() and devlink_unregister(), users can send netlink commands.

Quoted 7 lines
Quoted 2 lines

In your solution, driver authors will need to follow whole devlink how-to-use book.

Only if they need refs. If you don't the API is the same as yours.

IOW you don't provide an API for advanced use cases at all. You force the drivers to implement their own reference counting and locking.

The complex drivers already do it anyway, because they need to reference counting their own structures to make sure that the lifetime of these structures meats their model.

Quoted 1 line

I want them to just rely on devlink as a framework.

And I don't :). For me devlink is a way to configure device, not manage lifetime of driver specific data structures.

Quoted 6 lines

How many driver locks do you have in netdevsim after conversion? All 3? How do you add a port to the instance from sysfs without a AB / BA deadlock between the port lock and the devlink lock if the driver can't take the devlink lock? Are you still going to need the "in_reload" wart?

I don't know yet, because as you wrote before netdevsim is for prototyping and such ABBA deadlock doesn't exist in real devices.

My current focus is real devices for now.

Maybe the solution will be to go away from sysfs completely. We will see.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

This RFC makes the rules simple - all devlink ops are locked.

For the convenience of the drivers they can also take the instance lock whenever they want to prevent ops from being called. Experience with rtnl_lock teaches us that this is very useful for drivers.

Quoted 8 lines
Quoted 4 lines

Strange, I see completely opposite picture in the git log with so much changes that have Fixes line and add/remove/mention rtnl_lock. Maybe it is useful, but almost all if not all drivers full of fixes of rtnl_lock usage. I don't want same for the devlink.

If you don't provide a locking API to the drivers you'll have to fix 2x the bugs, and each of them subtly different. At least maintainers know what rtnl_lock rules are.

I clearly remember this patch and the sentence "...in
some devices' resume function(igb_resum,igc_resume) they calls rtnl_lock() again". The word "... some ..." hints to me that maintainers have different opinion on how to use rtnl_lock.

https://lore.kernel.org/netdev/[email protected]/

Thanks

author maintainer
#11 lore raw
Quoted 5 lines · On Tue, 2 Nov 2021 10:08:39 +0200 Leon Romanovsky wrote:
Quoted 4 lines · Jakub Kicinski
Quoted 3 lines · On Mon, 1 Nov 2021 20:36:15 +0200 Leon Romanovsky wrote:

This is three step process, but mainly it is first step. We need to make sure that functions that can re-entry will use nested locks.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Steps:
1. Use proper locking API that supports nesting:
https://lore.kernel.org/netdev/YYABqfFy%2F%2Fg5Gdis@nanopsycho/T/#mf9dc5cac2013abe413545bbe4a09cc231ae209a4

Whether we provide the an unlocked API or allow lock nesting on the driver API is not that important to me.

Thanks

Not sure what you're thanking for. I still prefer two explicit APIs. Allowing nesting is not really necessary here. Callers know whether they hold the lock or not.

Please see netdevsim, it takes the devlink_lock() at entry points to the driver - which in turn means the API which takes a lock can be removed once all drivers are converted.

Quoted 29 lines
Quoted 14 lines
Quoted 14 lines

2. Convert devlink->lock to be RW semaphore:
commit 4506dd3a90a82a0b6bde238f507907747ab88407

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 24 16:54:16 2021 +0300

devlink: Convert devlink lock to be RW semaphore

This is naive conversion of devlink->lock to RW semaphore, so we will be able to differentiate commands that require exclusive access vs. parallel ready-to-run ones.

All "set" commands that used devlink->lock are converted to write lock, while all "get" commands are marked with read lock.

Quoted 14 lines

Review static int devlink_nl_pre_doit(const struct genl_ops *ops, · line 592

Quoted 12 lines
(quoted diff) +6 -2
@@ -578,8 +584,12 @@ static int devlink_nl_pre_doit(const struct genl_ops *ops,
                mutex_unlock(&devlink_mutex);
                return PTR_ERR(devlink);
        }
-       if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
-               mutex_lock(&devlink->lock);
+
+       if (~ops->internal_flags & DEVLINK_NL_FLAG_SHARED_ACCESS)
+               down_write(&devlink->rwsem);
+       else
+               down_read(&devlink->rwsem);
+  

IIUC the RW sem thing is an optimization, it's besides the point.

Quoted 3 lines

I need RW as a way to ensure "exclusive" access during _set_ operation. It is not an optimization, but simple way to understand if parallel access is possible at this specific point of time.

How is this not an optimization to allow parallel "reads"?

Anything that calls the driver has no idea whether the driver needs read or write locking (assuming drivers depend on devlink for locking as they should) so I presume you only plan this for dumps which don't call the driver?

Anyway, consider this a nack, it should most definitely not be a part of the initial conversions. We have enough fallout to deal with.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

How? I have only one rule:
* Driver author should be aware that between devlink_register() and devlink_unregister(), users can send netlink commands.

In your solution, driver authors will need to follow whole devlink how-to-use book.

Quoted 8 lines
Quoted 4 lines

Only if they need refs. If you don't the API is the same as yours.

IOW you don't provide an API for advanced use cases at all. You force the drivers to implement their own reference counting and locking.

The complex drivers already do it anyway, because they need to reference counting their own structures to make sure that the lifetime of these structures meats their model.

Well, no comments on what mlx5 does or has to do but it's likely the most complex driver. Don't judge what others will benefit from based on mlx5.

Quoted 4 lines
Quoted 1 line

I want them to just rely on devlink as a framework.

And I don't :). For me devlink is a way to configure device, not manage lifetime of driver specific data structures.

My mind is set here. Obviously "community" can override, so gather support from significant devs or it's my way... sorry to be blunt.

Quoted 10 lines
Quoted 5 lines

How many driver locks do you have in netdevsim after conversion? All 3? How do you add a port to the instance from sysfs without a AB / BA deadlock between the port lock and the devlink lock if the driver can't take the devlink lock? Are you still going to need the "in_reload" wart?

I don't know yet, because as you wrote before netdevsim is for prototyping and such ABBA deadlock doesn't exist in real devices.

My current focus is real devices for now.

I wrote it with nfp in mind as well. It has a delayed work which needs to take the port lock. Sadly I don't have any nfps handy and I didn't want to post an untested patch.

Quoted 1 line

Maybe the solution will be to go away from sysfs completely. We will see.

Sigh, I feel like I just fixed netdevsim after it has been getting bit rotted for a couple of years.

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Strange, I see completely opposite picture in the git log with so much changes that have Fixes line and add/remove/mention rtnl_lock. Maybe it is useful, but almost all if not all drivers full of fixes of rtnl_lock usage. I don't want same for the devlink.

If you don't provide a locking API to the drivers you'll have to fix 2x the bugs, and each of them subtly different. At least maintainers know what rtnl_lock rules are.

Quoted 6 lines

I clearly remember this patch and the sentence "...in
some devices' resume function(igb_resum,igc_resume) they calls rtnl_lock() again". The word "... some ..." hints to me that maintainers have different opinion on how to use rtnl_lock.

https://lore.kernel.org/netdev/[email protected]/

Yes, using rtnl_lock for PM is certainly a bad idea, and I'm not sure why Intel does it. There are 10s of drivers which take rtnl_lock correctly and it greatly simplifies their lives.

#12 lore raw
Quoted 10 lines · Jakub Kicinski
Quoted 9 lines · On Tue, 2 Nov 2021 10:08:39 +0200 Leon Romanovsky wrote:
Quoted 8 lines · Jakub Kicinski
Quoted 6 lines · On Mon, 1 Nov 2021 20:36:15 +0200 Leon Romanovsky wrote:

This is three step process, but mainly it is first step. We need to make sure that functions that can re-entry will use nested locks.

Steps:
1. Use proper locking API that supports nesting:
https://lore.kernel.org/netdev/YYABqfFy%2F%2Fg5Gdis@nanopsycho/T/#mf9dc5cac2013abe413545bbe4a09cc231ae209a4

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Whether we provide the an unlocked API or allow lock nesting on the driver API is not that important to me.

Thanks

Not sure what you're thanking for. I still prefer two explicit APIs. Allowing nesting is not really necessary here. Callers know whether they hold the lock or not.

I'm doubt about. It maybe easy to tell in reload flow, but it is much harder inside eswitch mode change (as an example).

Quoted 4 lines

Please see netdevsim, it takes the devlink_lock() at entry points to the driver - which in turn means the API which takes a lock can be removed once all drivers are converted.

Netdevsim is a nice example, but it is very different from the real device.

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 17 lines

2. Convert devlink->lock to be RW semaphore:
commit 4506dd3a90a82a0b6bde238f507907747ab88407

Author: Leon Romanovsky <[email protected]>
Date:   Sun Oct 24 16:54:16 2021 +0300

devlink: Convert devlink lock to be RW semaphore

This is naive conversion of devlink->lock to RW semaphore, so we will be able to differentiate commands that require exclusive access vs. parallel ready-to-run ones.

All "set" commands that used devlink->lock are converted to write lock, while all "get" commands are marked with read lock.

(quoted diff) +0 -0
@@ -578,8 +584,12 @@ static int devlink_nl_pre_doit(const struct genl_ops *ops,
                mutex_unlock(&devlink_mutex);

Review static int devlink_nl_pre_doit(const struct genl_ops *ops, · line 592

Quoted 10 lines
(quoted diff) +6 -2
                return PTR_ERR(devlink);
        }
-       if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
-               mutex_lock(&devlink->lock);
+
+       if (~ops->internal_flags & DEVLINK_NL_FLAG_SHARED_ACCESS)
+               down_write(&devlink->rwsem);
+       else
+               down_read(&devlink->rwsem);
+  

IIUC the RW sem thing is an optimization, it's besides the point.

Quoted 5 lines
Quoted 3 lines

I need RW as a way to ensure "exclusive" access during _set_ operation. It is not an optimization, but simple way to understand if parallel access is possible at this specific point of time.

How is this not an optimization to allow parallel "reads"?

You need to stop everything when _set_ command is called. One way is to require for all netlink devlink calls to have lock, another solution is to use RW semaphore. This is why it is not optimization, but an implementation. Parallel "reads" are nice bonus.

Quoted 5 lines

Anything that calls the driver has no idea whether the driver needs read or write locking (assuming drivers depend on devlink for locking as they should) so I presume you only plan this for dumps which don't call the driver?

I planned to separate based on command nature:
* _set_ == write lock
* _get_ == read lock

Quoted 3 lines

Anyway, consider this a nack, it should most definitely not be a part of the initial conversions. We have enough fallout to deal with.

Any solution should take this into account. We can't design scheme that will be needed to be rewritten immediately after merge.

Quoted 21 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

How? I have only one rule:
* Driver author should be aware that between devlink_register() and devlink_unregister(), users can send netlink commands.

In your solution, driver authors will need to follow whole devlink how-to-use book.

Quoted 8 lines
Quoted 4 lines

Only if they need refs. If you don't the API is the same as yours.

IOW you don't provide an API for advanced use cases at all. You force the drivers to implement their own reference counting and locking.

The complex drivers already do it anyway, because they need to reference counting their own structures to make sure that the lifetime of these structures meats their model.

Well, no comments on what mlx5 does or has to do but it's likely the most complex driver. Don't judge what others will benefit from based on mlx5.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

I want them to just rely on devlink as a framework.

And I don't :). For me devlink is a way to configure device, not manage lifetime of driver specific data structures.

My mind is set here. Obviously "community" can override, so gather support from significant devs or it's my way... sorry to be blunt.

No problem

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

How many driver locks do you have in netdevsim after conversion? All 3? How do you add a port to the instance from sysfs without a AB / BA deadlock between the port lock and the devlink lock if the driver can't take the devlink lock? Are you still going to need the "in_reload" wart?

Quoted 8 lines
Quoted 4 lines

I don't know yet, because as you wrote before netdevsim is for prototyping and such ABBA deadlock doesn't exist in real devices.

My current focus is real devices for now.

I wrote it with nfp in mind as well. It has a delayed work which needs to take the port lock. Sadly I don't have any nfps handy and I didn't want to post an untested patch.

Do you remember why was port configuration implemented with delayed work?

Quoted 20 lines
Quoted 2 lines

Maybe the solution will be to go away from sysfs completely. We will see.

Sigh, I feel like I just fixed netdevsim after it has been getting bit rotted for a couple of years.

Quoted 14 lines
Quoted 8 lines
Quoted 4 lines

Strange, I see completely opposite picture in the git log with so much changes that have Fixes line and add/remove/mention rtnl_lock. Maybe it is useful, but almost all if not all drivers full of fixes of rtnl_lock usage. I don't want same for the devlink.

If you don't provide a locking API to the drivers you'll have to fix 2x the bugs, and each of them subtly different. At least maintainers know what rtnl_lock rules are.

I clearly remember this patch and the sentence "...in
some devices' resume function(igb_resum,igc_resume) they calls rtnl_lock() again". The word "... some ..." hints to me that maintainers have different opinion on how to use rtnl_lock.

Quoted 5 lines
Quoted 1 line

https://lore.kernel.org/netdev/[email protected]/

Yes, using rtnl_lock for PM is certainly a bad idea, and I'm not sure why Intel does it. There are 10s of drivers which take rtnl_lock correctly and it greatly simplifies their lives.

I would say that you are ignoring that most of such drivers don't add new functionality.

Anyway, I got your point, please give me time to see what I can do.

In case, we will adopt your model, will you convert all drivers?

Thanks

author maintainer
#13 lore raw
Quoted 8 lines · On Tue, 2 Nov 2021 20:14:22 +0200 Leon Romanovsky wrote:
Quoted 5 lines
Quoted 1 line

Thanks

Not sure what you're thanking for. I still prefer two explicit APIs. Allowing nesting is not really necessary here. Callers know whether they hold the lock or not.

I'm doubt about. It maybe easy to tell in reload flow, but it is much harder inside eswitch mode change (as an example).

Hm, interesting counter example, why is eswitch mode change harder? From devlink side they should be locked the same, and I take the devlink lock on all driver callbacks (probe, remove, sriov).

Quoted 10 lines
Quoted 5 lines
Quoted 3 lines

I need RW as a way to ensure "exclusive" access during _set_ operation. It is not an optimization, but simple way to understand if parallel access is possible at this specific point of time.

How is this not an optimization to allow parallel "reads"?

You need to stop everything when _set_ command is called. One way is to require for all netlink devlink calls to have lock, another solution is to use RW semaphore. This is why it is not optimization, but an implementation. Parallel "reads" are nice bonus.

Sorry I still don't understand. Why is devlink instance lock not enough? Are you saying parallel get is a hard requirement for the rework?

Quoted 10 lines
Quoted 8 lines
Quoted 4 lines

I don't know yet, because as you wrote before netdevsim is for prototyping and such ABBA deadlock doesn't exist in real devices.

My current focus is real devices for now.

I wrote it with nfp in mind as well. It has a delayed work which needs to take the port lock. Sadly I don't have any nfps handy and I didn't want to post an untested patch.

Do you remember why was port configuration implemented with delayed work?

IIRC it was because of the FW API for link info, all ports would get accessed at once so we used a work which would lock out devlink port splitting and update state of all ports.

Link state has to be read under rtnl_lock, yet port splitting needs to take rtnl_lock to register new netdevs so there was no prettier way to solve this.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

I clearly remember this patch and the sentence "...in
some devices' resume function(igb_resum,igc_resume) they calls rtnl_lock() again". The word "... some ..." hints to me that maintainers have different opinion on how to use rtnl_lock.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

https://lore.kernel.org/netdev/[email protected]/

Yes, using rtnl_lock for PM is certainly a bad idea, and I'm not sure why Intel does it. There are 10s of drivers which take rtnl_lock correctly and it greatly simplifies their lives.

I would say that you are ignoring that most of such drivers don't add new functionality.

You lost me again. You don't disagree that ability to lock out higher layers is useful for drivers but... ?

Quoted 3 lines

Anyway, I got your point, please give me time to see what I can do.

In case, we will adopt your model, will you convert all drivers?

Yes, sure. The way this RFC is done it should be possible to land it without any driver changes and then go driver by driver. I find that approach much more manageable.

#14 lore raw
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Tue, 2 Nov 2021 20:14:22 +0200 Leon Romanovsky wrote:
Quoted 5 lines
Quoted 1 line

Thanks

Not sure what you're thanking for. I still prefer two explicit APIs. Allowing nesting is not really necessary here. Callers know whether they hold the lock or not.

Quoted 6 lines
Quoted 2 lines

I'm doubt about. It maybe easy to tell in reload flow, but it is much harder inside eswitch mode change (as an example).

Hm, interesting counter example, why is eswitch mode change harder? From devlink side they should be locked the same, and I take the devlink lock on all driver callbacks (probe, remove, sriov).

I chose it as an example, because I see calls to eswitch enable/disable in so many driver paths that I can't tell for sure which API to use and if I need to take devlink lock or not inside the driver.

We also have other troublesome paths, like PCI recovery and health recovery which need some sort of protection.

It can be seen as an example that bringing devlink locking complexity to the real HW drivers won't be as good as it is for netdevsim.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines

I need RW as a way to ensure "exclusive" access during _set_ operation. It is not an optimization, but simple way to understand if parallel access is possible at this specific point of time.

How is this not an optimization to allow parallel "reads"?

Quoted 8 lines
Quoted 4 lines

You need to stop everything when _set_ command is called. One way is to require for all netlink devlink calls to have lock, another solution is to use RW semaphore. This is why it is not optimization, but an implementation. Parallel "reads" are nice bonus.

Sorry I still don't understand. Why is devlink instance lock not enough? Are you saying parallel get is a hard requirement for the rework?

Let's try to use the following example:

terminal A:                                  | terminal B:
                                             |
 while [ true ]                              | while [ true ]
   devlink sb pool show pci/0000:00:09.0     |   devlink dev eswitch set pci/0000:00:09.0 mode switchdev
                                             |   devlink dev eswitch set pci/0000:00:09.0 mode legacy

In current implementation without global devlink_mutex, it works ok, because every devlink command takes that global mutex and only one command runs at the same time. Plus no parallel access is allowed in rtneltink level.

So imagine that we allow parallel access without requiring devlink->lock for all .doit() and .dumpit() and continue rely on DEVLINK_NL_FLAG_NO_LOCK flag. In this case, terminal A will access driver without any protection while terminal B continues to use devlink->lock.

For this case, we need RW semaphore, terminal A will take read lock, terminal B will take write lock.

So unless you want to require that all devlink calls will have devlink->lock, which I think is wrong, we need RW semaphore.

Quoted 31 lines
Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

I don't know yet, because as you wrote before netdevsim is for prototyping and such ABBA deadlock doesn't exist in real devices.

My current focus is real devices for now.

I wrote it with nfp in mind as well. It has a delayed work which needs to take the port lock. Sadly I don't have any nfps handy and I didn't want to post an untested patch.

Do you remember why was port configuration implemented with delayed work?

IIRC it was because of the FW API for link info, all ports would get accessed at once so we used a work which would lock out devlink port splitting and update state of all ports.

Link state has to be read under rtnl_lock, yet port splitting needs to take rtnl_lock to register new netdevs so there was no prettier way to solve this.

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

I clearly remember this patch and the sentence "...in
some devices' resume function(igb_resum,igc_resume) they calls rtnl_lock() again". The word "... some ..." hints to me that maintainers have different opinion on how to use rtnl_lock.

https://lore.kernel.org/netdev/[email protected]/

Yes, using rtnl_lock for PM is certainly a bad idea, and I'm not sure why Intel does it. There are 10s of drivers which take rtnl_lock correctly and it greatly simplifies their lives.

Quoted 5 lines
Quoted 2 lines

I would say that you are ignoring that most of such drivers don't add new functionality.

You lost me again. You don't disagree that ability to lock out higher layers is useful for drivers but... ?

I disagree, but our views are so different here that nothing good will come out of arguing.

Quoted 8 lines
Quoted 4 lines

Anyway, I got your point, please give me time to see what I can do.

In case, we will adopt your model, will you convert all drivers?

Yes, sure. The way this RFC is done it should be possible to land it without any driver changes and then go driver by driver. I find that approach much more manageable.

I still believe that we can do everything inside devlink.c without touching drivers at all.

Thanks

author maintainer
#15 lore raw
Quoted 10 lines · On Wed, 3 Nov 2021 09:23:21 +0200 Leon Romanovsky wrote:
Quoted 6 lines
Quoted 2 lines

I'm doubt about. It maybe easy to tell in reload flow, but it is much harder inside eswitch mode change (as an example).

Hm, interesting counter example, why is eswitch mode change harder? From devlink side they should be locked the same, and I take the devlink lock on all driver callbacks (probe, remove, sriov).

I chose it as an example, because I see calls to eswitch enable/disable in so many driver paths that I can't tell for sure which API to use and if I need to take devlink lock or not inside the driver.

Really? Certainly not the case for nfp and bnxt. The two paths that care are devlink mode setting callback (already locked), and sriov config for spawning the right ports (I recommend all PCI callbacks take the devlink lock).

Quoted 2 lines

We also have other troublesome paths, like PCI recovery and health recovery which need some sort of protection.

PCI recovery should take the devlink lock like any PCI callback. Health callbacks are locked in my RFC.

Quoted 2 lines

It can be seen as an example that bringing devlink locking complexity to the real HW drivers won't be as good as it is for netdevsim.

I don't see that. Again, do whatever you want for mlx5, but don't stop others from creating shared infra.

Quoted 26 lines
Quoted 8 lines
Quoted 4 lines

You need to stop everything when _set_ command is called. One way is to require for all netlink devlink calls to have lock, another solution is to use RW semaphore. This is why it is not optimization, but an implementation. Parallel "reads" are nice bonus.

Sorry I still don't understand. Why is devlink instance lock not enough? Are you saying parallel get is a hard requirement for the rework?

Let's try to use the following example:

terminal A:                                  | terminal B:
                                             |
 while [ true ]                              | while [ true ]
   devlink sb pool show pci/0000:00:09.0     |   devlink dev eswitch set pci/0000:00:09.0 mode switchdev
                                             |   devlink dev eswitch set pci/0000:00:09.0 mode legacy

In current implementation without global devlink_mutex, it works ok, because every devlink command takes that global mutex and only one command runs at the same time. Plus no parallel access is allowed in rtneltink level.

So imagine that we allow parallel access without requiring devlink->lock for all .doit() and .dumpit() and continue rely on DEVLINK_NL_FLAG_NO_LOCK flag. In this case, terminal A will access driver without any protection while terminal B continues to use devlink->lock.

Quoted 5 lines

For this case, we need RW semaphore, terminal A will take read lock, terminal B will take write lock.

So unless you want to require that all devlink calls will have devlink->lock, which I think is wrong, we need RW semaphore.

Oh! I don't know how many times I said already that all callbacks should take the instance lock. Let me try one more time - all callbacks should take the instance lock.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

I would say that you are ignoring that most of such drivers don't add new functionality.

You lost me again. You don't disagree that ability to lock out higher layers is useful for drivers but... ?

I disagree, but our views are so different here that nothing good will come out of arguing.

You can't disagree with facts.

If I'm counting right there are ~80 Ethernet drivers which take rtnl_lock. Do you really think they would all do that if it was easier to implement their own locking? Or that they are all buggy?

Quoted 10 lines
Quoted 7 lines
Quoted 3 lines

Anyway, I got your point, please give me time to see what I can do.

In case, we will adopt your model, will you convert all drivers?

Yes, sure. The way this RFC is done it should be possible to land it without any driver changes and then go driver by driver. I find that approach much more manageable.

I still believe that we can do everything inside devlink.c without touching drivers at all.

?? The git history says you have been touching the drivers quite a bit.

#16 lore raw
In reply to Leon Romanovsky
Quoted 2 lines · Leon Romanovsky

The average driver author doesn't know locking well and won't be able to use devlink reference counting correctly.

I think this problem largely only exists to the extent that locking and lifecycle requirements are poorly documented. :P

Regards,
Edwin Peer

#17 lore raw
Quoted 7 lines · Edwin Peer
Quoted 2 lines · Leon Romanovsky

The average driver author doesn't know locking well and won't be able to use devlink reference counting correctly.

I think this problem largely only exists to the extent that locking and lifecycle requirements are poorly documented. :P

I'm talking about general locking concepts that are perfectly documented and still people do crazy things with it. :)

Quoted 3 lines

Regards,
Edwin Peer

author maintainer
#18 lore raw
Quoted 11 lines · On Tue, 2 Nov 2021 09:44:17 +0200 Leon Romanovsky wrote:
Quoted 7 lines · Edwin Peer
Quoted 2 lines · Leon Romanovsky

The average driver author doesn't know locking well and won't be able to use devlink reference counting correctly.

I think this problem largely only exists to the extent that locking and lifecycle requirements are poorly documented. :P

I'm talking about general locking concepts that are perfectly documented and still people do crazy things with it. :)

Yet you seem to be pushing for drivers to implement their own locking.

#19 lore raw
Quoted 7 lines · Jakub Kicinski
Quoted 6 lines · On Tue, 2 Nov 2021 09:44:17 +0200 Leon Romanovsky wrote:
Quoted 5 lines · Edwin Peer
Quoted 2 lines · Leon Romanovsky

The average driver author doesn't know locking well and won't be able to use devlink reference counting correctly.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

I think this problem largely only exists to the extent that locking and lifecycle requirements are poorly documented. :P

I'm talking about general locking concepts that are perfectly documented and still people do crazy things with it. :)

Yet you seem to be pushing for drivers to implement their own locking.

It wasn't me who suggested to expose devlink internal locks and reference counting to the drivers. In my implementation, drivers don't need to manage devlink at all and we will be able internally understand if lock is needed or not.

At least in theory, working to make it true.

Thanks

maintainer
#20 lore raw
In reply to Jakub Kicinski
Quoted 52 lines · Sun, Oct 31, 2021 at 01:12:49AM CEST, [email protected] wrote:

This implements what I think is the right direction for devlink API overhaul. It's an early RFC/PoC because the body of code is rather large so I'd appreciate feedback early... The patches are very roughly split, the point of the posting is primarily to prove that from the driver side the conversion is an net improvement in complexity.

IMO the problems with devlink locking are caused by two things:

(1) driver has no way to block devlink calls like it does in case of netedev / rtnl_lock, note that for devlink each driver has it's own lock so contention is not an issue;

(2) sometimes devlink calls into the driver without holding its lock
- for operations which may need the driver to call devlink (port splitting, switch config, reload etc.), the circular dependency is there, the driver can't solve this problem.

This set "fixes" the ordering by allowing the driver to participate in locking. The lock order remains:

device_lock -> [devlink_mutex] -> devlink instance -> rtnl_lock

but now driver can take devlink lock itself, and _ALL_ devlink ops are locked.

The expectation is that driver will take the devlink instance lock on its probe and remove paths, hence protecting all configuration paths with the devlink instance lock.

This is clearly demonstrated by the netdevsim conversion. All entry points to driver config are protected by devlink instance lock, so the driver switches to the "I'm already holding the devlink lock" API when calling devlink. All driver locks and trickery is removed.

The part which is slightly more challanging is quiescing async entry points which need to be closed on the devlink reload path (workqueue, debugfs etc.) and which also take devlink instance lock. For that we need to be able to take refs on the devlink instance and let them clean up after themselves rather than waiting synchronously.

That last part is not 100% finished in this patch set - it works but we need the driver to take devlink_mutex (the global lock) from its probe/remove path. I hope this is good enough for an RFC, the problem is easily solved by protecting the devlink XArray with something else than devlink_mutex and/or not holding devlink_mutex around each op (so that there is no ordering between the global and instance locks). Speaking of not holding devlink_mutex around each op this patch set also opens the path for parallel ops to different devlink instances which is currently impossible because all user space calls take devlink_mutex...

Quoted 1 line

The patches are on top of the cleanups I posted earlier.

Hi Jakub.

I took my time to read this thread and talked with Leon as well. My original intention of locking in devlink was to maintain the locking inside devlink.c to avoid the rtnl_lock-scenario.

However, I always feared that eventually we'll get to the point, when it won't be possible to maintain any longer. I think may be it.

In general, I like your approach. It is very clean and explicit. The driver knows what to do, in which context it is and it can behave accordingly. In theory or course, but the reality of drivers code tells us often something different :)

One small thing I don't fully undestand is the "opt-out" scenario which makes things a bit tangled. But perhaps you can explain it a bit more.

Leon claims that he thinks that he would be able to solve the locking scheme leaving all locking internal to devlink.c. I suggest to give him a week or 2 to present the solution. If he is not successful, lets continue on your approach.

What do you think?

Thanks!

Quoted 19 lines

Jakub Kicinski (5):
devlink: add unlocked APIs
devlink: add API for explicit locking
devlink: allow locking of all ops
netdevsim: minor code move
netdevsim: use devlink locking

drivers/net/netdevsim/bus.c       |  19 -
drivers/net/netdevsim/dev.c       | 450 ++++++++-------
drivers/net/netdevsim/fib.c       |  62 +--
drivers/net/netdevsim/netdevsim.h |   5 -
include/net/devlink.h             |  88 +++
net/core/devlink.c                | 875 +++++++++++++++++++++---------
6 files changed, 996 insertions(+), 503 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#21 lore raw
Quoted 8 lines · On Wed, 3 Nov 2021 10:03:32 +0100 Jiri Pirko wrote:

Hi Jakub.

I took my time to read this thread and talked with Leon as well. My original intention of locking in devlink was to maintain the locking inside devlink.c to avoid the rtnl_lock-scenario.

However, I always feared that eventually we'll get to the point, when it won't be possible to maintain any longer. I think may be it.

Indeed, the two things I think we can avoid from rtnl_lock pitfalls is that lock should be per-instance and that we should not wait for all refs to be gone at unregister time.

Both are rather trivial to achieve with devlink.

Quoted 4 lines

In general, I like your approach. It is very clean and explicit. The driver knows what to do, in which context it is and it can behave accordingly. In theory or course, but the reality of drivers code tells us often something different :)

Right. I'll convert a few more drivers but the real test will be seeing if potential races are gone - hard to measure.

Quoted 2 lines

One small thing I don't fully undestand is the "opt-out" scenario which makes things a bit tangled. But perhaps you can explain it a bit more.

Do you mean the .lock_flags? That's a transitional thing so that people can convert drivers callback by callback to make prettier patch sets. I may collapse all those flags into one, remains to be seen how useful it is when I create proper patches. This RFC is more of a code dump.

The whole opt-out is to create the entire new API at once, and then convert drivers one-by-one (or allow the maintainers who care to do it themselves). I find that easier and more friendly than slicing the API and drivers multiple times.

Long story short I expect the opt-out would be gone by the time 5.17 merge window rolls around.

Quoted 6 lines

Leon claims that he thinks that he would be able to solve the locking scheme leaving all locking internal to devlink.c. I suggest to give him a week or 2 to present the solution. If he is not successful, lets continue on your approach.

What do you think?

I do worry a little bit that our goals differ. Seems like Leon wants to fix devlink for devlink and I want drivers to be able to lean on it.

But I'm not attached to the exact approach or code, so as long as nobody is attached to theirs more RFCs can only help.

Please be courteous and send as RFCs, tho.

maintainer
#22 lore raw
Quoted 32 lines · Wed, Nov 03, 2021 at 03:52:31PM CET, [email protected] wrote:
Quoted 8 lines · On Wed, 3 Nov 2021 10:03:32 +0100 Jiri Pirko wrote:

Hi Jakub.

I took my time to read this thread and talked with Leon as well. My original intention of locking in devlink was to maintain the locking inside devlink.c to avoid the rtnl_lock-scenario.

However, I always feared that eventually we'll get to the point, when it won't be possible to maintain any longer. I think may be it.

Indeed, the two things I think we can avoid from rtnl_lock pitfalls is that lock should be per-instance and that we should not wait for all refs to be gone at unregister time.

Both are rather trivial to achieve with devlink.

Quoted 4 lines

In general, I like your approach. It is very clean and explicit. The driver knows what to do, in which context it is and it can behave accordingly. In theory or course, but the reality of drivers code tells us often something different :)

Right. I'll convert a few more drivers but the real test will be seeing if potential races are gone - hard to measure.

Quoted 2 lines

One small thing I don't fully undestand is the "opt-out" scenario which makes things a bit tangled. But perhaps you can explain it a bit more.

Do you mean the .lock_flags? That's a transitional thing so that people can convert drivers callback by callback to make prettier patch sets. I may collapse all those flags into one, remains to be seen how useful it is when I create proper patches. This RFC is more of a code dump.

Quoted 7 lines

The whole opt-out is to create the entire new API at once, and then convert drivers one-by-one (or allow the maintainers who care to do it themselves). I find that easier and more friendly than slicing the API and drivers multiple times.

Long story short I expect the opt-out would be gone by the time 5.17 merge window rolls around.

Okay, got it.

Quoted 8 lines
Quoted 7 lines

Leon claims that he thinks that he would be able to solve the locking scheme leaving all locking internal to devlink.c. I suggest to give him a week or 2 to present the solution. If he is not successful, lets continue on your approach.

What do you think?

Quoted 7 lines

I do worry a little bit that our goals differ. Seems like Leon wants to fix devlink for devlink and I want drivers to be able to lean on it.

But I'm not attached to the exact approach or code, so as long as nobody is attached to theirs more RFCs can only help.

Please be courteous and send as RFCs, tho.

Makes sense. Thanks!