Skip to content

Commit a8b92ca

Browse files
yishaihjgunthorpe
authored andcommitted
IB/mlx5: Introduce DEVX
Introduce DEVX to enable direct device commands in downstream patches from this series. In that mode of work the firmware manages the isolation between processes' resources and as such a DEVX user id is created and assigned to the given user context upon allocation request. A capability check is done to make sure that this feature is really supported by the firmware prior to creating the DEVX user id. Signed-off-by: Yishai Hadas <yishaih@mellanox.com> Signed-off-by: Leon Romanovsky <leonro@mellanox.com> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
1 parent 7dc08dc commit a8b92ca

5 files changed

Lines changed: 96 additions & 3 deletions

File tree

‎drivers/infiniband/hw/mlx5/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ obj-$(CONFIG_MLX5_INFINIBAND) += mlx5_ib.o
33
mlx5_ib-y := main.o cq.o doorbell.o qp.o mem.o srq.o mr.o ah.o mad.o gsi.o ib_virt.o cmd.o cong.o
44
mlx5_ib-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += odp.o
55
mlx5_ib-$(CONFIG_MLX5_ESWITCH) += ib_rep.o
6+
mlx5_ib-$(CONFIG_INFINIBAND_USER_ACCESS) += devx.o

‎drivers/infiniband/hw/mlx5/devx.c‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2+
/*
3+
* Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
4+
*/
5+
6+
#include <rdma/ib_user_verbs.h>
7+
#include <rdma/ib_verbs.h>
8+
#include <rdma/uverbs_types.h>
9+
#include <rdma/uverbs_ioctl.h>
10+
#include <rdma/mlx5_user_ioctl_cmds.h>
11+
#include <rdma/ib_umem.h>
12+
#include <linux/mlx5/driver.h>
13+
#include <linux/mlx5/fs.h>
14+
#include "mlx5_ib.h"
15+
16+
int mlx5_ib_devx_create(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
17+
{
18+
u32 in[MLX5_ST_SZ_DW(create_uctx_in)] = {0};
19+
u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)] = {0};
20+
u64 general_obj_types;
21+
void *uctx;
22+
void *hdr;
23+
int err;
24+
25+
uctx = MLX5_ADDR_OF(create_uctx_in, in, uctx);
26+
hdr = MLX5_ADDR_OF(create_uctx_in, in, hdr);
27+
28+
general_obj_types = MLX5_CAP_GEN_64(dev->mdev, general_obj_types);
29+
if (!(general_obj_types & MLX5_GENERAL_OBJ_TYPES_CAP_UCTX) ||
30+
!(general_obj_types & MLX5_GENERAL_OBJ_TYPES_CAP_UMEM))
31+
return -EINVAL;
32+
33+
if (!capable(CAP_NET_RAW))
34+
return -EPERM;
35+
36+
MLX5_SET(general_obj_in_cmd_hdr, hdr, opcode, MLX5_CMD_OP_CREATE_GENERAL_OBJECT);
37+
MLX5_SET(general_obj_in_cmd_hdr, hdr, obj_type, MLX5_OBJ_TYPE_UCTX);
38+
39+
err = mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
40+
if (err)
41+
return err;
42+
43+
context->devx_uid = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id);
44+
return 0;
45+
}
46+
47+
void mlx5_ib_devx_destroy(struct mlx5_ib_dev *dev,
48+
struct mlx5_ib_ucontext *context)
49+
{
50+
u32 in[MLX5_ST_SZ_DW(general_obj_in_cmd_hdr)] = {0};
51+
u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)] = {0};
52+
53+
MLX5_SET(general_obj_in_cmd_hdr, in, opcode, MLX5_CMD_OP_DESTROY_GENERAL_OBJECT);
54+
MLX5_SET(general_obj_in_cmd_hdr, in, obj_type, MLX5_OBJ_TYPE_UCTX);
55+
MLX5_SET(general_obj_in_cmd_hdr, in, obj_id, context->devx_uid);
56+
57+
mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
58+
}

‎drivers/infiniband/hw/mlx5/main.c‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,8 +1650,8 @@ static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
16501650
if (err)
16511651
return ERR_PTR(err);
16521652

1653-
if (req.flags)
1654-
return ERR_PTR(-EINVAL);
1653+
if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1654+
return ERR_PTR(-EOPNOTSUPP);
16551655

16561656
if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
16571657
return ERR_PTR(-EOPNOTSUPP);
@@ -1735,6 +1735,18 @@ static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
17351735
goto out_uars;
17361736
}
17371737

1738+
if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1739+
/* Block DEVX on Infiniband as of SELinux */
1740+
if (mlx5_ib_port_link_layer(ibdev, 1) != IB_LINK_LAYER_ETHERNET) {
1741+
err = -EPERM;
1742+
goto out_td;
1743+
}
1744+
1745+
err = mlx5_ib_devx_create(dev, context);
1746+
if (err)
1747+
goto out_td;
1748+
}
1749+
17381750
INIT_LIST_HEAD(&context->vma_private_list);
17391751
mutex_init(&context->vma_private_list_mutex);
17401752
INIT_LIST_HEAD(&context->db_page_list);
@@ -1795,7 +1807,7 @@ static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
17951807

17961808
err = ib_copy_to_udata(udata, &resp, resp.response_length);
17971809
if (err)
1798-
goto out_td;
1810+
goto out_mdev;
17991811

18001812
bfregi->ver = ver;
18011813
bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
@@ -1805,6 +1817,9 @@ static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
18051817

18061818
return &context->ibucontext;
18071819

1820+
out_mdev:
1821+
if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1822+
mlx5_ib_devx_destroy(dev, context);
18081823
out_td:
18091824
if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
18101825
mlx5_ib_dealloc_transport_domain(dev, context->tdn);
@@ -1830,6 +1845,9 @@ static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
18301845
struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
18311846
struct mlx5_bfreg_info *bfregi;
18321847

1848+
if (context->devx_uid)
1849+
mlx5_ib_devx_destroy(dev, context);
1850+
18331851
bfregi = &context->bfregi;
18341852
if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
18351853
mlx5_ib_dealloc_transport_domain(dev, context->tdn);

‎drivers/infiniband/hw/mlx5/mlx5_ib.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct mlx5_ib_ucontext {
143143

144144
u64 lib_caps;
145145
DECLARE_BITMAP(dm_pages, MLX5_MAX_MEMIC_PAGES);
146+
u16 devx_uid;
146147
};
147148

148149
static inline struct mlx5_ib_ucontext *to_mucontext(struct ib_ucontext *ibucontext)
@@ -1215,6 +1216,18 @@ struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *dev,
12151216
void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *dev,
12161217
u8 port_num);
12171218

1219+
#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
1220+
int mlx5_ib_devx_create(struct mlx5_ib_dev *dev,
1221+
struct mlx5_ib_ucontext *context);
1222+
void mlx5_ib_devx_destroy(struct mlx5_ib_dev *dev,
1223+
struct mlx5_ib_ucontext *context);
1224+
#else
1225+
static inline int
1226+
mlx5_ib_devx_create(struct mlx5_ib_dev *dev,
1227+
struct mlx5_ib_ucontext *context) { return -EOPNOTSUPP; };
1228+
static inline void mlx5_ib_devx_destroy(struct mlx5_ib_dev *dev,
1229+
struct mlx5_ib_ucontext *context) {}
1230+
#endif
12181231
static inline void init_query_mad(struct ib_smp *mad)
12191232
{
12201233
mad->base_version = 1;

‎include/uapi/rdma/mlx5-abi.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ enum mlx5_lib_caps {
7676
MLX5_LIB_CAP_4K_UAR = (__u64)1 << 0,
7777
};
7878

79+
enum mlx5_ib_alloc_uctx_v2_flags {
80+
MLX5_IB_ALLOC_UCTX_DEVX = 1 << 0,
81+
};
7982
struct mlx5_ib_alloc_ucontext_req_v2 {
8083
__u32 total_num_bfregs;
8184
__u32 num_low_latency_bfregs;

0 commit comments

Comments
 (0)