[patch net-next 3/7] net: devlink: introduce a flag to indicate devlink port being registered

Jiri Pirko
2022-08-25T10:34:15Z

Instead of relying on devlink pointer not being initialized, introduce
an extra flag to indicate if devlink port is registered. This is needed
as later on devlink pointer is going to be initialized even in case
devlink port is not registered yet.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
---
include/net/devlink.h | 3 ++-
net/core/devlink.c | 6 ++++--
2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/include/net/devlink.h b/include/net/devlink.h
index 0b45d44a3348..7b41ebbaf379 100644
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -129,7 +129,8 @@ struct devlink_port {
void *type_dev;
struct devlink_port_attrs attrs;
u8 attrs_set:1,
- switch_port:1;
+ switch_port:1,
+ registered:1;
struct delayed_work type_warn_dw;
struct list_head reporter_list;
struct mutex reporters_lock; /* Protects reporter_list */
diff --git a/net/core/devlink.c b/net/core/devlink.c
index 2737dad89f51..af13f95384d9 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -372,9 +372,9 @@ static struct devlink *devlink_get_from_attrs(struct net *net,
}

#define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port) \
- WARN_ON_ONCE(!(devlink_port)->devlink)
+ WARN_ON_ONCE(!(devlink_port)->registered)
#define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port) \
- WARN_ON_ONCE((devlink_port)->devlink)
+ WARN_ON_ONCE((devlink_port)->registered)

static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
unsigned int port_index)
@@ -9778,6 +9778,7 @@ int devl_port_register(struct devlink *devlink,

ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);

+ devlink_port->registered = true;
devlink_port->devlink = devlink;
devlink_port->index = port_index;
spin_lock_init(&devlink_port->type_lock);
@@ -9836,6 +9837,7 @@ void devl_port_unregister(struct devlink_port *devlink_port)
WARN_ON(!list_empty(&devlink_port->reporter_list));
WARN_ON(!list_empty(&devlink_port->region_list));
mutex_destroy(&devlink_port->reporters_lock);
+ devlink_port->registered = false;
}
EXPORT_SYMBOL_GPL(devl_port_unregister);

--
2.37.1