Skip to content

Commit 46db1b7

Browse files
kuba-moodavem330
authored andcommitted
devlink: expose get/put functions
Allow those who hold implicit reference on a devlink instance to try to take a full ref on it. This will be used from netdev code which has an implicit ref because of driver call ordering. Note that after recent changes devlink_unregister() may happen before netdev unregister, but devlink_free() should still happen after, so we are safe to try, but we can't just refcount_inc() and assume it's not zero. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Leon Romanovsky <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 095cfcf commit 46db1b7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

include/net/devlink.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,9 @@ devlink_trap_policers_unregister(struct devlink *devlink,
17261726

17271727
#if IS_ENABLED(CONFIG_NET_DEVLINK)
17281728

1729+
struct devlink *__must_check devlink_try_get(struct devlink *devlink);
1730+
void devlink_put(struct devlink *devlink);
1731+
17291732
void devlink_compat_running_version(struct net_device *dev,
17301733
char *buf, size_t len);
17311734
int devlink_compat_flash_update(struct net_device *dev, const char *file_name);
@@ -1736,6 +1739,15 @@ int devlink_compat_switch_id_get(struct net_device *dev,
17361739

17371740
#else
17381741

1742+
static inline struct devlink *devlink_try_get(struct devlink *devlink)
1743+
{
1744+
return NULL;
1745+
}
1746+
1747+
static inline void devlink_put(struct devlink *devlink)
1748+
{
1749+
}
1750+
17391751
static inline void
17401752
devlink_compat_running_version(struct net_device *dev, char *buf, size_t len)
17411753
{

net/core/devlink.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,17 @@ struct net *devlink_net(const struct devlink *devlink)
182182
}
183183
EXPORT_SYMBOL_GPL(devlink_net);
184184

185-
static void devlink_put(struct devlink *devlink)
185+
void devlink_put(struct devlink *devlink)
186186
{
187187
if (refcount_dec_and_test(&devlink->refcount))
188188
complete(&devlink->comp);
189189
}
190190

191-
static bool __must_check devlink_try_get(struct devlink *devlink)
191+
struct devlink *__must_check devlink_try_get(struct devlink *devlink)
192192
{
193-
return refcount_inc_not_zero(&devlink->refcount);
193+
if (refcount_inc_not_zero(&devlink->refcount))
194+
return devlink;
195+
return NULL;
194196
}
195197

196198
static struct devlink *devlink_get_from_attrs(struct net *net,

0 commit comments

Comments
 (0)