Skip to content

Commit

Permalink
devlink: protect devlink->dev by the instance lock
Browse files Browse the repository at this point in the history
devlink->dev is assumed to be always valid as long as any
outstanding reference to the devlink instance exists.

In prep for weakening of the references take the instance lock.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Jakub Kicinski authored and David S. Miller committed Jan 6, 2023
1 parent 7a54a51 commit 870c7ad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion net/devlink/devl_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ struct devlink_gen_cmd {

extern const struct genl_small_ops devlink_nl_ops[56];

struct devlink *devlink_get_from_attrs(struct net *net, struct nlattr **attrs);
struct devlink *
devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs);

void devlink_notify_unregister(struct devlink *devlink);
void devlink_notify_register(struct devlink *devlink);
Expand Down
7 changes: 3 additions & 4 deletions net/devlink/leftover.c
Original file line number Diff line number Diff line change
Expand Up @@ -6314,12 +6314,10 @@ static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,

start_offset = state->start_offset;

devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs);
if (IS_ERR(devlink))
return PTR_ERR(devlink);

devl_lock(devlink);

if (!attrs[DEVLINK_ATTR_REGION_NAME]) {
NL_SET_ERR_MSG(cb->extack, "No region name provided");
err = -EINVAL;
Expand Down Expand Up @@ -7735,9 +7733,10 @@ devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
struct nlattr **attrs = info->attrs;
struct devlink *devlink;

devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
devlink = devlink_get_from_attrs_lock(sock_net(cb->skb->sk), attrs);
if (IS_ERR(devlink))
return NULL;
devl_unlock(devlink);

reporter = devlink_health_reporter_get_from_attrs(devlink, attrs);
devlink_put(devlink);
Expand Down
9 changes: 6 additions & 3 deletions net/devlink/netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
[DEVLINK_ATTR_REGION_DIRECT] = { .type = NLA_FLAG },
};

struct devlink *devlink_get_from_attrs(struct net *net, struct nlattr **attrs)
struct devlink *
devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs)
{
struct devlink *devlink;
unsigned long index;
Expand All @@ -96,9 +97,11 @@ struct devlink *devlink_get_from_attrs(struct net *net, struct nlattr **attrs)
devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);

devlinks_xa_for_each_registered_get(net, index, devlink) {
devl_lock(devlink);
if (strcmp(devlink->dev->bus->name, busname) == 0 &&
strcmp(dev_name(devlink->dev), devname) == 0)
return devlink;
devl_unlock(devlink);
devlink_put(devlink);
}

Expand All @@ -113,10 +116,10 @@ static int devlink_nl_pre_doit(const struct genl_split_ops *ops,
struct devlink *devlink;
int err;

devlink = devlink_get_from_attrs(genl_info_net(info), info->attrs);
devlink = devlink_get_from_attrs_lock(genl_info_net(info), info->attrs);
if (IS_ERR(devlink))
return PTR_ERR(devlink);
devl_lock(devlink);

info->user_ptr[0] = devlink;
if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
devlink_port = devlink_port_get_from_info(devlink, info);
Expand Down

0 comments on commit 870c7ad

Please sign in to comment.