mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-01 08:04:22 +08:00
bf6b200bc8
Device drivers register with devlink from their probe routines (under the device lock) by acquiring the devlink instance lock and calling devl_register(). Drivers that support a devlink reload usually implement the reload_{down, up}() operations in a similar fashion to their remove and probe routines, respectively. However, while the remove and probe routines are invoked with the device lock held, the reload operations are only invoked with the devlink instance lock held. It is therefore impossible for drivers to acquire the device lock from their reload operations, as this would result in lock inversion. The motivating use case for invoking the reload operations with the device lock held is in mlxsw which needs to trigger a PCI reset as part of the reload. The driver cannot call pci_reset_function() as this function acquires the device lock. Instead, it needs to call __pci_reset_function_locked which expects the device lock to be held. To that end, adjust devlink to always acquire the device lock before the devlink instance lock when performing a reload. Do that when reload is explicitly triggered by user space by specifying the 'DEVLINK_NL_FLAG_NEED_DEV_LOCK' flag in the pre_doit and post_doit operations of the reload command. A previous patch already handled the case where reload is invoked as part of netns dismantle. Signed-off-by: Ido Schimmel <idosch@nvidia.com> Reviewed-by: Jiri Pirko <jiri@nvidia.com> Signed-off-by: Petr Machata <petrm@nvidia.com> Reviewed-by: Simon Horman <horms@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
260 lines
6.5 KiB
C
260 lines
6.5 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/*
|
|
* Copyright (c) 2016 Mellanox Technologies. All rights reserved.
|
|
* Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
|
|
*/
|
|
|
|
#include <net/genetlink.h>
|
|
#include <net/sock.h>
|
|
|
|
#include "devl_internal.h"
|
|
|
|
#define DEVLINK_NL_FLAG_NEED_PORT BIT(0)
|
|
#define DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT BIT(1)
|
|
#define DEVLINK_NL_FLAG_NEED_DEV_LOCK BIT(2)
|
|
|
|
static const struct genl_multicast_group devlink_nl_mcgrps[] = {
|
|
[DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
|
|
};
|
|
|
|
int devlink_nl_put_nested_handle(struct sk_buff *msg, struct net *net,
|
|
struct devlink *devlink, int attrtype)
|
|
{
|
|
struct nlattr *nested_attr;
|
|
struct net *devl_net;
|
|
|
|
nested_attr = nla_nest_start(msg, attrtype);
|
|
if (!nested_attr)
|
|
return -EMSGSIZE;
|
|
if (devlink_nl_put_handle(msg, devlink))
|
|
goto nla_put_failure;
|
|
|
|
rcu_read_lock();
|
|
devl_net = read_pnet_rcu(&devlink->_net);
|
|
if (!net_eq(net, devl_net)) {
|
|
int id = peernet2id_alloc(net, devl_net, GFP_ATOMIC);
|
|
|
|
rcu_read_unlock();
|
|
if (nla_put_s32(msg, DEVLINK_ATTR_NETNS_ID, id))
|
|
return -EMSGSIZE;
|
|
} else {
|
|
rcu_read_unlock();
|
|
}
|
|
|
|
nla_nest_end(msg, nested_attr);
|
|
return 0;
|
|
|
|
nla_put_failure:
|
|
nla_nest_cancel(msg, nested_attr);
|
|
return -EMSGSIZE;
|
|
}
|
|
|
|
int devlink_nl_msg_reply_and_new(struct sk_buff **msg, struct genl_info *info)
|
|
{
|
|
int err;
|
|
|
|
if (*msg) {
|
|
err = genlmsg_reply(*msg, info);
|
|
if (err)
|
|
return err;
|
|
}
|
|
*msg = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
|
|
if (!*msg)
|
|
return -ENOMEM;
|
|
return 0;
|
|
}
|
|
|
|
struct devlink *
|
|
devlink_get_from_attrs_lock(struct net *net, struct nlattr **attrs,
|
|
bool dev_lock)
|
|
{
|
|
struct devlink *devlink;
|
|
unsigned long index;
|
|
char *busname;
|
|
char *devname;
|
|
|
|
if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
|
|
devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
|
|
|
|
devlinks_xa_for_each_registered_get(net, index, devlink) {
|
|
devl_dev_lock(devlink, dev_lock);
|
|
if (devl_is_registered(devlink) &&
|
|
strcmp(devlink->dev->bus->name, busname) == 0 &&
|
|
strcmp(dev_name(devlink->dev), devname) == 0)
|
|
return devlink;
|
|
devl_dev_unlock(devlink, dev_lock);
|
|
devlink_put(devlink);
|
|
}
|
|
|
|
return ERR_PTR(-ENODEV);
|
|
}
|
|
|
|
static int __devlink_nl_pre_doit(struct sk_buff *skb, struct genl_info *info,
|
|
u8 flags)
|
|
{
|
|
bool dev_lock = flags & DEVLINK_NL_FLAG_NEED_DEV_LOCK;
|
|
struct devlink_port *devlink_port;
|
|
struct devlink *devlink;
|
|
int err;
|
|
|
|
devlink = devlink_get_from_attrs_lock(genl_info_net(info), info->attrs,
|
|
dev_lock);
|
|
if (IS_ERR(devlink))
|
|
return PTR_ERR(devlink);
|
|
|
|
info->user_ptr[0] = devlink;
|
|
if (flags & DEVLINK_NL_FLAG_NEED_PORT) {
|
|
devlink_port = devlink_port_get_from_info(devlink, info);
|
|
if (IS_ERR(devlink_port)) {
|
|
err = PTR_ERR(devlink_port);
|
|
goto unlock;
|
|
}
|
|
info->user_ptr[1] = devlink_port;
|
|
} else if (flags & DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT) {
|
|
devlink_port = devlink_port_get_from_info(devlink, info);
|
|
if (!IS_ERR(devlink_port))
|
|
info->user_ptr[1] = devlink_port;
|
|
}
|
|
return 0;
|
|
|
|
unlock:
|
|
devl_dev_unlock(devlink, dev_lock);
|
|
devlink_put(devlink);
|
|
return err;
|
|
}
|
|
|
|
int devlink_nl_pre_doit(const struct genl_split_ops *ops,
|
|
struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
return __devlink_nl_pre_doit(skb, info, 0);
|
|
}
|
|
|
|
int devlink_nl_pre_doit_port(const struct genl_split_ops *ops,
|
|
struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_PORT);
|
|
}
|
|
|
|
int devlink_nl_pre_doit_dev_lock(const struct genl_split_ops *ops,
|
|
struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_DEV_LOCK);
|
|
}
|
|
|
|
int devlink_nl_pre_doit_port_optional(const struct genl_split_ops *ops,
|
|
struct sk_buff *skb,
|
|
struct genl_info *info)
|
|
{
|
|
return __devlink_nl_pre_doit(skb, info, DEVLINK_NL_FLAG_NEED_DEVLINK_OR_PORT);
|
|
}
|
|
|
|
static void __devlink_nl_post_doit(struct sk_buff *skb, struct genl_info *info,
|
|
u8 flags)
|
|
{
|
|
bool dev_lock = flags & DEVLINK_NL_FLAG_NEED_DEV_LOCK;
|
|
struct devlink *devlink;
|
|
|
|
devlink = info->user_ptr[0];
|
|
devl_dev_unlock(devlink, dev_lock);
|
|
devlink_put(devlink);
|
|
}
|
|
|
|
void devlink_nl_post_doit(const struct genl_split_ops *ops,
|
|
struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
__devlink_nl_post_doit(skb, info, 0);
|
|
}
|
|
|
|
void
|
|
devlink_nl_post_doit_dev_lock(const struct genl_split_ops *ops,
|
|
struct sk_buff *skb, struct genl_info *info)
|
|
{
|
|
__devlink_nl_post_doit(skb, info, DEVLINK_NL_FLAG_NEED_DEV_LOCK);
|
|
}
|
|
|
|
static int devlink_nl_inst_single_dumpit(struct sk_buff *msg,
|
|
struct netlink_callback *cb, int flags,
|
|
devlink_nl_dump_one_func_t *dump_one,
|
|
struct nlattr **attrs)
|
|
{
|
|
struct devlink *devlink;
|
|
int err;
|
|
|
|
devlink = devlink_get_from_attrs_lock(sock_net(msg->sk), attrs, false);
|
|
if (IS_ERR(devlink))
|
|
return PTR_ERR(devlink);
|
|
err = dump_one(msg, devlink, cb, flags | NLM_F_DUMP_FILTERED);
|
|
|
|
devl_unlock(devlink);
|
|
devlink_put(devlink);
|
|
|
|
if (err != -EMSGSIZE)
|
|
return err;
|
|
return msg->len;
|
|
}
|
|
|
|
static int devlink_nl_inst_iter_dumpit(struct sk_buff *msg,
|
|
struct netlink_callback *cb, int flags,
|
|
devlink_nl_dump_one_func_t *dump_one)
|
|
{
|
|
struct devlink_nl_dump_state *state = devlink_dump_state(cb);
|
|
struct devlink *devlink;
|
|
int err = 0;
|
|
|
|
while ((devlink = devlinks_xa_find_get(sock_net(msg->sk),
|
|
&state->instance))) {
|
|
devl_lock(devlink);
|
|
|
|
if (devl_is_registered(devlink))
|
|
err = dump_one(msg, devlink, cb, flags);
|
|
else
|
|
err = 0;
|
|
|
|
devl_unlock(devlink);
|
|
devlink_put(devlink);
|
|
|
|
if (err)
|
|
break;
|
|
|
|
state->instance++;
|
|
|
|
/* restart sub-object walk for the next instance */
|
|
state->idx = 0;
|
|
}
|
|
|
|
if (err != -EMSGSIZE)
|
|
return err;
|
|
return msg->len;
|
|
}
|
|
|
|
int devlink_nl_dumpit(struct sk_buff *msg, struct netlink_callback *cb,
|
|
devlink_nl_dump_one_func_t *dump_one)
|
|
{
|
|
const struct genl_info *info = genl_info_dump(cb);
|
|
struct nlattr **attrs = info->attrs;
|
|
int flags = NLM_F_MULTI;
|
|
|
|
if (attrs &&
|
|
(attrs[DEVLINK_ATTR_BUS_NAME] || attrs[DEVLINK_ATTR_DEV_NAME]))
|
|
return devlink_nl_inst_single_dumpit(msg, cb, flags, dump_one,
|
|
attrs);
|
|
else
|
|
return devlink_nl_inst_iter_dumpit(msg, cb, flags, dump_one);
|
|
}
|
|
|
|
struct genl_family devlink_nl_family __ro_after_init = {
|
|
.name = DEVLINK_GENL_NAME,
|
|
.version = DEVLINK_GENL_VERSION,
|
|
.netnsok = true,
|
|
.parallel_ops = true,
|
|
.module = THIS_MODULE,
|
|
.split_ops = devlink_nl_ops,
|
|
.n_split_ops = ARRAY_SIZE(devlink_nl_ops),
|
|
.resv_start_op = DEVLINK_CMD_SELFTESTS_RUN + 1,
|
|
.mcgrps = devlink_nl_mcgrps,
|
|
.n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
|
|
};
|