Skip to content

Commit ae63828

Browse files
jgunthorperleon
authored andcommitted
RDMA: Consolidate patterns with offsetofend() to ib_copy_validate_udata_in()
Go treewide and consolidate all existing patterns using: * offsetofend() and variations * ib_is_udata_cleared() * ib_copy_from_udata() into a direct call to the new ib_copy_validate_udata_in(). Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
1 parent cef2842 commit ae63828

4 files changed

Lines changed: 26 additions & 120 deletions

File tree

drivers/infiniband/hw/efa/efa_verbs.c

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -699,29 +699,9 @@ int efa_create_qp(struct ib_qp *ibqp, struct ib_qp_init_attr *init_attr,
699699
if (err)
700700
goto err_out;
701701

702-
if (offsetofend(typeof(cmd), driver_qp_type) > udata->inlen) {
703-
ibdev_dbg(&dev->ibdev,
704-
"Incompatible ABI params, no input udata\n");
705-
err = -EINVAL;
706-
goto err_out;
707-
}
708-
709-
if (udata->inlen > sizeof(cmd) &&
710-
!ib_is_udata_cleared(udata, sizeof(cmd),
711-
udata->inlen - sizeof(cmd))) {
712-
ibdev_dbg(&dev->ibdev,
713-
"Incompatible ABI params, unknown fields in udata\n");
714-
err = -EINVAL;
715-
goto err_out;
716-
}
717-
718-
err = ib_copy_from_udata(&cmd, udata,
719-
min(sizeof(cmd), udata->inlen));
720-
if (err) {
721-
ibdev_dbg(&dev->ibdev,
722-
"Cannot copy udata for create_qp\n");
702+
err = ib_copy_validate_udata_in(udata, cmd, driver_qp_type);
703+
if (err)
723704
goto err_out;
724-
}
725705

726706
if (cmd.comp_mask || !is_reserved_cleared(cmd.reserved_98)) {
727707
ibdev_dbg(&dev->ibdev,
@@ -1160,28 +1140,9 @@ int efa_create_user_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr,
11601140
goto err_out;
11611141
}
11621142

1163-
if (offsetofend(typeof(cmd), num_sub_cqs) > udata->inlen) {
1164-
ibdev_dbg(ibdev,
1165-
"Incompatible ABI params, no input udata\n");
1166-
err = -EINVAL;
1167-
goto err_out;
1168-
}
1169-
1170-
if (udata->inlen > sizeof(cmd) &&
1171-
!ib_is_udata_cleared(udata, sizeof(cmd),
1172-
udata->inlen - sizeof(cmd))) {
1173-
ibdev_dbg(ibdev,
1174-
"Incompatible ABI params, unknown fields in udata\n");
1175-
err = -EINVAL;
1176-
goto err_out;
1177-
}
1178-
1179-
err = ib_copy_from_udata(&cmd, udata,
1180-
min(sizeof(cmd), udata->inlen));
1181-
if (err) {
1182-
ibdev_dbg(ibdev, "Cannot copy udata for create_cq\n");
1143+
err = ib_copy_validate_udata_in(udata, cmd, num_sub_cqs);
1144+
if (err)
11831145
goto err_out;
1184-
}
11851146

11861147
if (cmd.comp_mask || !is_reserved_cleared(cmd.reserved_58)) {
11871148
ibdev_dbg(ibdev,

drivers/infiniband/hw/irdma/verbs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,21 @@ static void irdma_alloc_push_page(struct irdma_qp *iwqp)
284284
static int irdma_alloc_ucontext(struct ib_ucontext *uctx,
285285
struct ib_udata *udata)
286286
{
287-
#define IRDMA_ALLOC_UCTX_MIN_REQ_LEN offsetofend(struct irdma_alloc_ucontext_req, rsvd8)
288287
#define IRDMA_ALLOC_UCTX_MIN_RESP_LEN offsetofend(struct irdma_alloc_ucontext_resp, rsvd)
289288
struct ib_device *ibdev = uctx->device;
290289
struct irdma_device *iwdev = to_iwdev(ibdev);
291290
struct irdma_alloc_ucontext_req req = {};
292291
struct irdma_alloc_ucontext_resp uresp = {};
293292
struct irdma_ucontext *ucontext = to_ucontext(uctx);
294293
struct irdma_uk_attrs *uk_attrs = &iwdev->rf->sc_dev.hw_attrs.uk_attrs;
294+
int ret;
295295

296-
if (udata->inlen < IRDMA_ALLOC_UCTX_MIN_REQ_LEN ||
297-
udata->outlen < IRDMA_ALLOC_UCTX_MIN_RESP_LEN)
296+
if (udata->outlen < IRDMA_ALLOC_UCTX_MIN_RESP_LEN)
298297
return -EINVAL;
299298

300-
if (ib_copy_from_udata(&req, udata, min(sizeof(req), udata->inlen)))
301-
return -EINVAL;
299+
ret = ib_copy_validate_udata_in(udata, req, rsvd8);
300+
if (ret)
301+
return ret;
302302

303303
if (req.userspace_ver < 4 || req.userspace_ver > IRDMA_ABI_VER)
304304
goto ver_error;

drivers/infiniband/hw/mlx4/qp.c

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,6 @@ static int _mlx4_ib_create_qp_rss(struct ib_pd *pd, struct mlx4_ib_qp *qp,
710710
struct ib_udata *udata)
711711
{
712712
struct mlx4_ib_create_qp_rss ucmd = {};
713-
size_t required_cmd_sz;
714713
int err;
715714

716715
if (!udata) {
@@ -721,16 +720,10 @@ static int _mlx4_ib_create_qp_rss(struct ib_pd *pd, struct mlx4_ib_qp *qp,
721720
if (udata->outlen)
722721
return -EOPNOTSUPP;
723722

724-
required_cmd_sz = offsetof(typeof(ucmd), reserved1) +
725-
sizeof(ucmd.reserved1);
726-
if (udata->inlen < required_cmd_sz) {
727-
pr_debug("invalid inlen\n");
728-
return -EINVAL;
729-
}
730-
731-
if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
723+
err = ib_copy_validate_udata_in(udata, ucmd, reserved1);
724+
if (err) {
732725
pr_debug("copy failed\n");
733-
return -EFAULT;
726+
return err;
734727
}
735728

736729
if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)))
@@ -739,13 +732,6 @@ static int _mlx4_ib_create_qp_rss(struct ib_pd *pd, struct mlx4_ib_qp *qp,
739732
if (ucmd.comp_mask || ucmd.reserved1)
740733
return -EOPNOTSUPP;
741734

742-
if (udata->inlen > sizeof(ucmd) &&
743-
!ib_is_udata_cleared(udata, sizeof(ucmd),
744-
udata->inlen - sizeof(ucmd))) {
745-
pr_debug("inlen is not supported\n");
746-
return -EOPNOTSUPP;
747-
}
748-
749735
if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
750736
pr_debug("RSS QP with unsupported QP type %d\n",
751737
init_attr->qp_type);
@@ -4269,22 +4255,12 @@ int mlx4_ib_modify_wq(struct ib_wq *ibwq, struct ib_wq_attr *wq_attr,
42694255
{
42704256
struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
42714257
struct mlx4_ib_modify_wq ucmd = {};
4272-
size_t required_cmd_sz;
42734258
enum ib_wq_state cur_state, new_state;
4274-
int err = 0;
4275-
4276-
required_cmd_sz = offsetof(typeof(ucmd), reserved) +
4277-
sizeof(ucmd.reserved);
4278-
if (udata->inlen < required_cmd_sz)
4279-
return -EINVAL;
4280-
4281-
if (udata->inlen > sizeof(ucmd) &&
4282-
!ib_is_udata_cleared(udata, sizeof(ucmd),
4283-
udata->inlen - sizeof(ucmd)))
4284-
return -EOPNOTSUPP;
4259+
int err;
42854260

4286-
if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
4287-
return -EFAULT;
4261+
err = ib_copy_validate_udata_in(udata, ucmd, reserved);
4262+
if (err)
4263+
return err;
42884264

42894265
if (ucmd.comp_mask || ucmd.reserved)
42904266
return -EOPNOTSUPP;

drivers/infiniband/hw/mlx5/qp.c

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4707,17 +4707,9 @@ int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
47074707
return -ENOSYS;
47084708

47094709
if (udata && udata->inlen) {
4710-
if (udata->inlen < offsetofend(typeof(ucmd), ece_options))
4711-
return -EINVAL;
4712-
4713-
if (udata->inlen > sizeof(ucmd) &&
4714-
!ib_is_udata_cleared(udata, sizeof(ucmd),
4715-
udata->inlen - sizeof(ucmd)))
4716-
return -EOPNOTSUPP;
4717-
4718-
if (ib_copy_from_udata(&ucmd, udata,
4719-
min(udata->inlen, sizeof(ucmd))))
4720-
return -EFAULT;
4710+
err = ib_copy_validate_udata_in(udata, ucmd, ece_options);
4711+
if (err)
4712+
return err;
47214713

47224714
if (ucmd.comp_mask & ~MLX5_IB_MODIFY_QP_OOO_DP ||
47234715
memchr_inv(&ucmd.burst_info.reserved, 0,
@@ -5389,25 +5381,11 @@ static int prepare_user_rq(struct ib_pd *pd,
53895381
struct mlx5_ib_dev *dev = to_mdev(pd->device);
53905382
struct mlx5_ib_create_wq ucmd = {};
53915383
int err;
5392-
size_t required_cmd_sz;
5393-
5394-
required_cmd_sz = offsetofend(struct mlx5_ib_create_wq,
5395-
single_stride_log_num_of_bytes);
5396-
if (udata->inlen < required_cmd_sz) {
5397-
mlx5_ib_dbg(dev, "invalid inlen\n");
5398-
return -EINVAL;
5399-
}
5400-
5401-
if (udata->inlen > sizeof(ucmd) &&
5402-
!ib_is_udata_cleared(udata, sizeof(ucmd),
5403-
udata->inlen - sizeof(ucmd))) {
5404-
mlx5_ib_dbg(dev, "inlen is not supported\n");
5405-
return -EOPNOTSUPP;
5406-
}
5407-
5408-
if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
5384+
err = ib_copy_validate_udata_in(udata, ucmd,
5385+
single_stride_log_num_of_bytes);
5386+
if (err) {
54095387
mlx5_ib_dbg(dev, "copy failed\n");
5410-
return -EFAULT;
5388+
return err;
54115389
}
54125390

54135391
if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
@@ -5626,25 +5604,16 @@ int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
56265604
struct mlx5_ib_dev *dev = to_mdev(wq->device);
56275605
struct mlx5_ib_rwq *rwq = to_mrwq(wq);
56285606
struct mlx5_ib_modify_wq ucmd = {};
5629-
size_t required_cmd_sz;
56305607
int curr_wq_state;
56315608
int wq_state;
56325609
int inlen;
56335610
int err;
56345611
void *rqc;
56355612
void *in;
56365613

5637-
required_cmd_sz = offsetofend(struct mlx5_ib_modify_wq, reserved);
5638-
if (udata->inlen < required_cmd_sz)
5639-
return -EINVAL;
5640-
5641-
if (udata->inlen > sizeof(ucmd) &&
5642-
!ib_is_udata_cleared(udata, sizeof(ucmd),
5643-
udata->inlen - sizeof(ucmd)))
5644-
return -EOPNOTSUPP;
5645-
5646-
if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
5647-
return -EFAULT;
5614+
err = ib_copy_validate_udata_in(udata, ucmd, reserved);
5615+
if (err)
5616+
return err;
56485617

56495618
if (ucmd.comp_mask || ucmd.reserved)
56505619
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)