Skip to content

Commit 7556052

Browse files
elic307imstsirkin
authored andcommitted
vdpa/mlx5: Support configuring max data virtqueue
Check whether the max number of data virtqueue pairs was provided when a adding a new device and verify the new value does not exceed device capabilities. In addition, change the arrays holding virtqueue and callback contexts to be dynamically allocated. Signed-off-by: Eli Cohen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Includes fixup: vdpa/mlx5: fix error handling in mlx5_vdpa_dev_add() Clang build fails with mlx5_vnet.c:2574:6: error: variable 'mvdev' is used uninitialized whenever 'if' condition is true if (!ndev->vqs || !ndev->event_cbs) { ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ mlx5_vnet.c:2660:14: note: uninitialized use occurs here put_device(&mvdev->vdev.dev); ^~~~~ This because mvdev is set after trying to allocate ndev->vqs,event_cbs. So move the allocation to after mvdev is set but before the arrays are used in init_mvqs() Signed-off-by: Tom Rix <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]> Includes fixup: vdpa/mlx5: fix endian-ness for max vqs sparse warnings: (new ones prefixed by >>) >> drivers/vdpa/mlx5/net/mlx5_vnet.c:1247:23: sparse: sparse: cast to restricted __le16 >> drivers/vdpa/mlx5/net/mlx5_vnet.c:1247:23: sparse: sparse: cast from restricted __virtio16 > 1247 num = le16_to_cpu(ndev->config.max_virtqueue_pairs); Address this using the appropriate wrapper. Cc: "Eli Cohen" <[email protected]> Reported-by: kernel test robot <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]> Acked-by: Jason Wang <[email protected]> Reviewed-by: Eli Cohen <[email protected]>
1 parent e313705 commit 7556052

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

drivers/vdpa/mlx5/net/mlx5_vnet.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@ struct mlx5_vdpa_virtqueue {
131131
struct mlx5_vq_restore_info ri;
132132
};
133133

134-
/* We will remove this limitation once mlx5_vdpa_alloc_resources()
135-
* provides for driver space allocation
136-
*/
137-
#define MLX5_MAX_SUPPORTED_VQS 16
138-
139134
static bool is_index_valid(struct mlx5_vdpa_dev *mvdev, u16 idx)
140135
{
141136
if (unlikely(idx > mvdev->max_idx))
@@ -148,8 +143,8 @@ struct mlx5_vdpa_net {
148143
struct mlx5_vdpa_dev mvdev;
149144
struct mlx5_vdpa_net_resources res;
150145
struct virtio_net_config config;
151-
struct mlx5_vdpa_virtqueue vqs[MLX5_MAX_SUPPORTED_VQS];
152-
struct vdpa_callback event_cbs[MLX5_MAX_SUPPORTED_VQS + 1];
146+
struct mlx5_vdpa_virtqueue *vqs;
147+
struct vdpa_callback *event_cbs;
153148

154149
/* Serialize vq resources creation and destruction. This is required
155150
* since memory map might change and we need to destroy and create
@@ -1216,7 +1211,7 @@ static void suspend_vqs(struct mlx5_vdpa_net *ndev)
12161211
{
12171212
int i;
12181213

1219-
for (i = 0; i < MLX5_MAX_SUPPORTED_VQS; i++)
1214+
for (i = 0; i < ndev->mvdev.max_vqs; i++)
12201215
suspend_vq(ndev, &ndev->vqs[i]);
12211216
}
12221217

@@ -1242,8 +1237,15 @@ static int create_rqt(struct mlx5_vdpa_net *ndev)
12421237
void *in;
12431238
int i, j;
12441239
int err;
1240+
int num;
12451241

1246-
max_rqt = min_t(int, MLX5_MAX_SUPPORTED_VQS / 2,
1242+
if (!(ndev->mvdev.actual_features & BIT_ULL(VIRTIO_NET_F_MQ)))
1243+
num = 1;
1244+
else
1245+
num = mlx5vdpa16_to_cpu(&ndev->mvdev,
1246+
ndev->config.max_virtqueue_pairs);
1247+
1248+
max_rqt = min_t(int, roundup_pow_of_two(num),
12471249
1 << MLX5_CAP_GEN(ndev->mvdev.mdev, log_max_rqt_size));
12481250
if (max_rqt < 1)
12491251
return -EOPNOTSUPP;
@@ -1260,7 +1262,7 @@ static int create_rqt(struct mlx5_vdpa_net *ndev)
12601262
MLX5_SET(rqtc, rqtc, rqt_max_size, max_rqt);
12611263
list = MLX5_ADDR_OF(rqtc, rqtc, rq_num[0]);
12621264
for (i = 0, j = 0; i < max_rqt; i++, j += 2)
1263-
list[i] = cpu_to_be32(ndev->vqs[j % ndev->mvdev.max_vqs].virtq_id);
1265+
list[i] = cpu_to_be32(ndev->vqs[j % (2 * num)].virtq_id);
12641266

12651267
MLX5_SET(rqtc, rqtc, rqt_actual_size, max_rqt);
12661268
err = mlx5_vdpa_create_rqt(&ndev->mvdev, in, inlen, &ndev->res.rqtn);
@@ -2218,7 +2220,7 @@ static int mlx5_vdpa_reset(struct vdpa_device *vdev)
22182220
clear_vqs_ready(ndev);
22192221
mlx5_vdpa_destroy_mr(&ndev->mvdev);
22202222
ndev->mvdev.status = 0;
2221-
memset(ndev->event_cbs, 0, sizeof(ndev->event_cbs));
2223+
memset(ndev->event_cbs, 0, sizeof(*ndev->event_cbs) * (mvdev->max_vqs + 1));
22222224
ndev->mvdev.actual_features = 0;
22232225
++mvdev->generation;
22242226
if (MLX5_CAP_GEN(mvdev->mdev, umem_uid_0)) {
@@ -2291,6 +2293,8 @@ static void mlx5_vdpa_free(struct vdpa_device *vdev)
22912293
}
22922294
mlx5_vdpa_free_resources(&ndev->mvdev);
22932295
mutex_destroy(&ndev->reslock);
2296+
kfree(ndev->event_cbs);
2297+
kfree(ndev->vqs);
22942298
}
22952299

22962300
static struct vdpa_notification_area mlx5_get_vq_notification(struct vdpa_device *vdev, u16 idx)
@@ -2536,9 +2540,21 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
25362540
return -EOPNOTSUPP;
25372541
}
25382542

2539-
/* we save one virtqueue for control virtqueue should we require it */
25402543
max_vqs = MLX5_CAP_DEV_VDPA_EMULATION(mdev, max_num_virtio_queues);
2541-
max_vqs = min_t(u32, max_vqs, MLX5_MAX_SUPPORTED_VQS);
2544+
if (max_vqs < 2) {
2545+
dev_warn(mdev->device,
2546+
"%d virtqueues are supported. At least 2 are required\n",
2547+
max_vqs);
2548+
return -EAGAIN;
2549+
}
2550+
2551+
if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP)) {
2552+
if (add_config->net.max_vq_pairs > max_vqs / 2)
2553+
return -EINVAL;
2554+
max_vqs = min_t(u32, max_vqs, 2 * add_config->net.max_vq_pairs);
2555+
} else {
2556+
max_vqs = 2;
2557+
}
25422558

25432559
ndev = vdpa_alloc_device(struct mlx5_vdpa_net, mvdev.vdev, mdev->device, &mlx5_vdpa_ops,
25442560
name, false);
@@ -2548,6 +2564,14 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
25482564
ndev->mvdev.max_vqs = max_vqs;
25492565
mvdev = &ndev->mvdev;
25502566
mvdev->mdev = mdev;
2567+
2568+
ndev->vqs = kcalloc(max_vqs, sizeof(*ndev->vqs), GFP_KERNEL);
2569+
ndev->event_cbs = kcalloc(max_vqs + 1, sizeof(*ndev->event_cbs), GFP_KERNEL);
2570+
if (!ndev->vqs || !ndev->event_cbs) {
2571+
err = -ENOMEM;
2572+
goto err_alloc;
2573+
}
2574+
25512575
init_mvqs(ndev);
25522576
mutex_init(&ndev->reslock);
25532577
config = &ndev->config;
@@ -2625,6 +2649,7 @@ static int mlx5_vdpa_dev_add(struct vdpa_mgmt_dev *v_mdev, const char *name,
26252649
mlx5_mpfs_del_mac(pfmdev, config->mac);
26262650
err_mtu:
26272651
mutex_destroy(&ndev->reslock);
2652+
err_alloc:
26282653
put_device(&mvdev->vdev.dev);
26292654
return err;
26302655
}
@@ -2667,7 +2692,8 @@ static int mlx5v_probe(struct auxiliary_device *adev,
26672692
mgtdev->mgtdev.ops = &mdev_ops;
26682693
mgtdev->mgtdev.device = mdev->device;
26692694
mgtdev->mgtdev.id_table = id_table;
2670-
mgtdev->mgtdev.config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
2695+
mgtdev->mgtdev.config_attr_mask = BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR) |
2696+
BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MAX_VQP);
26712697
mgtdev->madev = madev;
26722698

26732699
err = vdpa_mgmtdev_register(&mgtdev->mgtdev);

0 commit comments

Comments
 (0)