Skip to content

Commit 0e2d00e

Browse files
jgunthorpedledford
authored andcommitted
RDMA: Add NLDEV_GET_CHARDEV to allow char dev discovery and autoload
Allow userspace to issue a netlink query against the ib_device for something like "uverbs" and get back the char dev name, inode major/minor, and interface ABI information for "uverbs0". Since we are now in netlink this can also trigger a module autoload to make the uverbs device come into existence. Largely this will let us replace searching and reading inside sysfs to setup devices, and provides an alternative (using driver_id) to device name based provider binding for things like rxe. Signed-off-by: Jason Gunthorpe <jgg@mellanox.com> Signed-off-by: Doug Ledford <dledford@redhat.com>
1 parent 5d60c11 commit 0e2d00e

6 files changed

Lines changed: 221 additions & 0 deletions

File tree

‎drivers/infiniband/core/core_priv.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ typedef int (*nldev_callback)(struct ib_device *device,
8888
int ib_enum_all_devs(nldev_callback nldev_cb, struct sk_buff *skb,
8989
struct netlink_callback *cb);
9090

91+
struct ib_client_nl_info {
92+
struct sk_buff *nl_msg;
93+
struct device *cdev;
94+
unsigned int port;
95+
u64 abi;
96+
};
97+
int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name,
98+
struct ib_client_nl_info *res);
99+
91100
enum ib_cache_gid_default_mode {
92101
IB_CACHE_GID_DEFAULT_MODE_SET,
93102
IB_CACHE_GID_DEFAULT_MODE_DELETE

‎drivers/infiniband/core/device.c‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,104 @@ void ib_unregister_client(struct ib_client *client)
17261726
}
17271727
EXPORT_SYMBOL(ib_unregister_client);
17281728

1729+
static int __ib_get_global_client_nl_info(const char *client_name,
1730+
struct ib_client_nl_info *res)
1731+
{
1732+
struct ib_client *client;
1733+
unsigned long index;
1734+
int ret = -ENOENT;
1735+
1736+
down_read(&clients_rwsem);
1737+
xa_for_each_marked (&clients, index, client, CLIENT_REGISTERED) {
1738+
if (strcmp(client->name, client_name) != 0)
1739+
continue;
1740+
if (!client->get_global_nl_info) {
1741+
ret = -EOPNOTSUPP;
1742+
break;
1743+
}
1744+
ret = client->get_global_nl_info(res);
1745+
if (WARN_ON(ret == -ENOENT))
1746+
ret = -EINVAL;
1747+
if (!ret && res->cdev)
1748+
get_device(res->cdev);
1749+
break;
1750+
}
1751+
up_read(&clients_rwsem);
1752+
return ret;
1753+
}
1754+
1755+
static int __ib_get_client_nl_info(struct ib_device *ibdev,
1756+
const char *client_name,
1757+
struct ib_client_nl_info *res)
1758+
{
1759+
unsigned long index;
1760+
void *client_data;
1761+
int ret = -ENOENT;
1762+
1763+
down_read(&ibdev->client_data_rwsem);
1764+
xan_for_each_marked (&ibdev->client_data, index, client_data,
1765+
CLIENT_DATA_REGISTERED) {
1766+
struct ib_client *client = xa_load(&clients, index);
1767+
1768+
if (!client || strcmp(client->name, client_name) != 0)
1769+
continue;
1770+
if (!client->get_nl_info) {
1771+
ret = -EOPNOTSUPP;
1772+
break;
1773+
}
1774+
ret = client->get_nl_info(ibdev, client_data, res);
1775+
if (WARN_ON(ret == -ENOENT))
1776+
ret = -EINVAL;
1777+
1778+
/*
1779+
* The cdev is guaranteed valid as long as we are inside the
1780+
* client_data_rwsem as remove_one can't be called. Keep it
1781+
* valid for the caller.
1782+
*/
1783+
if (!ret && res->cdev)
1784+
get_device(res->cdev);
1785+
break;
1786+
}
1787+
up_read(&ibdev->client_data_rwsem);
1788+
1789+
return ret;
1790+
}
1791+
1792+
/**
1793+
* ib_get_client_nl_info - Fetch the nl_info from a client
1794+
* @device - IB device
1795+
* @client_name - Name of the client
1796+
* @res - Result of the query
1797+
*/
1798+
int ib_get_client_nl_info(struct ib_device *ibdev, const char *client_name,
1799+
struct ib_client_nl_info *res)
1800+
{
1801+
int ret;
1802+
1803+
if (ibdev)
1804+
ret = __ib_get_client_nl_info(ibdev, client_name, res);
1805+
else
1806+
ret = __ib_get_global_client_nl_info(client_name, res);
1807+
#ifdef CONFIG_MODULES
1808+
if (ret == -ENOENT) {
1809+
request_module("rdma-client-%s", client_name);
1810+
if (ibdev)
1811+
ret = __ib_get_client_nl_info(ibdev, client_name, res);
1812+
else
1813+
ret = __ib_get_global_client_nl_info(client_name, res);
1814+
}
1815+
#endif
1816+
if (ret) {
1817+
if (ret == -ENOENT)
1818+
return -EOPNOTSUPP;
1819+
return ret;
1820+
}
1821+
1822+
if (WARN_ON(!res->cdev))
1823+
return -EINVAL;
1824+
return 0;
1825+
}
1826+
17291827
/**
17301828
* ib_set_client_data - Set IB client context
17311829
* @device:Device to set context for

‎drivers/infiniband/core/nldev.c‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ static const struct nla_policy nldev_policy[RDMA_NLDEV_ATTR_MAX] = {
120120
[RDMA_NLDEV_ATTR_DEV_PROTOCOL] = { .type = NLA_NUL_STRING,
121121
.len = RDMA_NLDEV_ATTR_ENTRY_STRLEN },
122122
[RDMA_NLDEV_NET_NS_FD] = { .type = NLA_U32 },
123+
[RDMA_NLDEV_ATTR_CHARDEV] = { .type = NLA_U64 },
124+
[RDMA_NLDEV_ATTR_CHARDEV_ABI] = { .type = NLA_U64 },
125+
[RDMA_NLDEV_ATTR_CHARDEV_TYPE] = { .type = NLA_NUL_STRING,
126+
.len = 128 },
127+
[RDMA_NLDEV_ATTR_CHARDEV_NAME] = { .type = NLA_NUL_STRING,
128+
.len = RDMA_NLDEV_ATTR_ENTRY_STRLEN },
123129
};
124130

125131
static int put_driver_name_print_type(struct sk_buff *msg, const char *name,
@@ -1347,6 +1353,91 @@ static int nldev_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
13471353
return 0;
13481354
}
13491355

1356+
static int nldev_get_chardev(struct sk_buff *skb, struct nlmsghdr *nlh,
1357+
struct netlink_ext_ack *extack)
1358+
{
1359+
struct nlattr *tb[RDMA_NLDEV_ATTR_MAX];
1360+
char client_name[IB_DEVICE_NAME_MAX];
1361+
struct ib_client_nl_info data = {};
1362+
struct ib_device *ibdev = NULL;
1363+
struct sk_buff *msg;
1364+
u32 index;
1365+
int err;
1366+
1367+
err = nlmsg_parse(nlh, 0, tb, RDMA_NLDEV_ATTR_MAX - 1, nldev_policy,
1368+
extack);
1369+
if (err || !tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE])
1370+
return -EINVAL;
1371+
1372+
if (nla_strlcpy(client_name, tb[RDMA_NLDEV_ATTR_CHARDEV_TYPE],
1373+
sizeof(client_name)) >= sizeof(client_name))
1374+
return -EINVAL;
1375+
1376+
if (tb[RDMA_NLDEV_ATTR_DEV_INDEX]) {
1377+
index = nla_get_u32(tb[RDMA_NLDEV_ATTR_DEV_INDEX]);
1378+
ibdev = ib_device_get_by_index(sock_net(skb->sk), index);
1379+
if (!ibdev)
1380+
return -EINVAL;
1381+
1382+
if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1383+
data.port = nla_get_u32(tb[RDMA_NLDEV_ATTR_PORT_INDEX]);
1384+
if (!rdma_is_port_valid(ibdev, data.port)) {
1385+
err = -EINVAL;
1386+
goto out_put;
1387+
}
1388+
} else {
1389+
data.port = -1;
1390+
}
1391+
} else if (tb[RDMA_NLDEV_ATTR_PORT_INDEX]) {
1392+
return -EINVAL;
1393+
}
1394+
1395+
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1396+
if (!msg) {
1397+
err = -ENOMEM;
1398+
goto out_put;
1399+
}
1400+
nlh = nlmsg_put(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1401+
RDMA_NL_GET_TYPE(RDMA_NL_NLDEV,
1402+
RDMA_NLDEV_CMD_GET_CHARDEV),
1403+
0, 0);
1404+
1405+
data.nl_msg = msg;
1406+
err = ib_get_client_nl_info(ibdev, client_name, &data);
1407+
if (err)
1408+
goto out_nlmsg;
1409+
1410+
err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV,
1411+
huge_encode_dev(data.cdev->devt),
1412+
RDMA_NLDEV_ATTR_PAD);
1413+
if (err)
1414+
goto out_data;
1415+
err = nla_put_u64_64bit(msg, RDMA_NLDEV_ATTR_CHARDEV_ABI, data.abi,
1416+
RDMA_NLDEV_ATTR_PAD);
1417+
if (err)
1418+
goto out_data;
1419+
if (nla_put_string(msg, RDMA_NLDEV_ATTR_CHARDEV_NAME,
1420+
dev_name(data.cdev))) {
1421+
err = -EMSGSIZE;
1422+
goto out_data;
1423+
}
1424+
1425+
nlmsg_end(msg, nlh);
1426+
put_device(data.cdev);
1427+
if (ibdev)
1428+
ib_device_put(ibdev);
1429+
return rdma_nl_unicast(msg, NETLINK_CB(skb).portid);
1430+
1431+
out_data:
1432+
put_device(data.cdev);
1433+
out_nlmsg:
1434+
nlmsg_free(msg);
1435+
out_put:
1436+
if (ibdev)
1437+
ib_device_put(ibdev);
1438+
return err;
1439+
}
1440+
13501441
static int nldev_sys_get_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
13511442
struct netlink_ext_ack *extack)
13521443
{
@@ -1404,6 +1495,9 @@ static const struct rdma_nl_cbs nldev_cb_table[RDMA_NLDEV_NUM_OPS] = {
14041495
.doit = nldev_get_doit,
14051496
.dump = nldev_get_dumpit,
14061497
},
1498+
[RDMA_NLDEV_CMD_GET_CHARDEV] = {
1499+
.doit = nldev_get_chardev,
1500+
},
14071501
[RDMA_NLDEV_CMD_SET] = {
14081502
.doit = nldev_set_doit,
14091503
.flags = RDMA_NL_ADMIN_PERM,

‎include/rdma/ib_verbs.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,10 +2684,14 @@ struct ib_device {
26842684
u32 iw_driver_flags;
26852685
};
26862686

2687+
struct ib_client_nl_info;
26872688
struct ib_client {
26882689
const char *name;
26892690
void (*add) (struct ib_device *);
26902691
void (*remove)(struct ib_device *, void *client_data);
2692+
int (*get_nl_info)(struct ib_device *ibdev, void *client_data,
2693+
struct ib_client_nl_info *res);
2694+
int (*get_global_nl_info)(struct ib_client_nl_info *res);
26912695

26922696
/* Returns the net_dev belonging to this ib_client and matching the
26932697
* given parameters.

‎include/rdma/rdma_netlink.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,6 @@ void rdma_link_register(struct rdma_link_ops *ops);
110110
void rdma_link_unregister(struct rdma_link_ops *ops);
111111

112112
#define MODULE_ALIAS_RDMA_LINK(type) MODULE_ALIAS("rdma-link-" type)
113+
#define MODULE_ALIAS_RDMA_CLIENT(type) MODULE_ALIAS("rdma-client-" type)
114+
113115
#endif /* _RDMA_NETLINK_H */

‎include/uapi/rdma/rdma_netlink.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ enum rdma_nldev_command {
279279

280280
RDMA_NLDEV_CMD_RES_PD_GET, /* can dump */
281281

282+
RDMA_NLDEV_CMD_GET_CHARDEV,
283+
282284
RDMA_NLDEV_NUM_OPS
283285
};
284286

@@ -491,6 +493,18 @@ enum rdma_nldev_attr {
491493
*/
492494
RDMA_NLDEV_NET_NS_FD, /* u32 */
493495

496+
/*
497+
* Information about a chardev.
498+
* CHARDEV_TYPE is the name of the chardev ABI (ie uverbs, umad, etc)
499+
* CHARDEV_ABI signals the ABI revision (historical)
500+
* CHARDEV_NAME is the kernel name for the /dev/ file (no directory)
501+
* CHARDEV is the 64 bit dev_t for the inode
502+
*/
503+
RDMA_NLDEV_ATTR_CHARDEV_TYPE, /* string */
504+
RDMA_NLDEV_ATTR_CHARDEV_NAME, /* string */
505+
RDMA_NLDEV_ATTR_CHARDEV_ABI, /* u64 */
506+
RDMA_NLDEV_ATTR_CHARDEV, /* u64 */
507+
494508
/*
495509
* Always the end
496510
*/

0 commit comments

Comments
 (0)