Commit b61815e2 authored by Jason Gunthorpe's avatar Jason Gunthorpe

IB/uverbs: Use uverbs_alloc for allocations

Several handlers need temporary allocations for the life of the method,
switch them to use the uverbs_alloc allocator.
Signed-off-by: default avatarJason Gunthorpe <jgg@mellanox.com>
Reviewed-by: default avatarLeon Romanovsky <leonro@mellanox.com>
parent 461bb2ee
...@@ -104,22 +104,18 @@ static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_READ)( ...@@ -104,22 +104,18 @@ static int UVERBS_HANDLER(UVERBS_METHOD_COUNTERS_READ)(
uattr = uverbs_attr_get(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF); uattr = uverbs_attr_get(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF);
read_attr.ncounters = uattr->ptr_attr.len / sizeof(u64); read_attr.ncounters = uattr->ptr_attr.len / sizeof(u64);
read_attr.counters_buff = kcalloc(read_attr.ncounters, read_attr.counters_buff = uverbs_zalloc(
sizeof(u64), GFP_KERNEL); attrs, array_size(read_attr.ncounters, sizeof(u64)));
if (!read_attr.counters_buff) if (IS_ERR(read_attr.counters_buff))
return -ENOMEM; return PTR_ERR(read_attr.counters_buff);
ret = counters->device->read_counters(counters, &read_attr, attrs); ret = counters->device->read_counters(counters, &read_attr, attrs);
if (ret) if (ret)
goto err_read; return ret;
ret = uverbs_copy_to(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF, return uverbs_copy_to(attrs, UVERBS_ATTR_READ_COUNTERS_BUFF,
read_attr.counters_buff, read_attr.counters_buff,
read_attr.ncounters * sizeof(u64)); read_attr.ncounters * sizeof(u64));
err_read:
kfree(read_attr.counters_buff);
return ret;
} }
DECLARE_UVERBS_NAMED_METHOD( DECLARE_UVERBS_NAMED_METHOD(
......
...@@ -511,22 +511,19 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OTHER)( ...@@ -511,22 +511,19 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OTHER)(
if (!devx_is_general_cmd(cmd_in)) if (!devx_is_general_cmd(cmd_in))
return -EINVAL; return -EINVAL;
cmd_out = kvzalloc(cmd_out_len, GFP_KERNEL); cmd_out = uverbs_zalloc(attrs, cmd_out_len);
if (!cmd_out) if (IS_ERR(cmd_out))
return -ENOMEM; return PTR_ERR(cmd_out);
MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid); MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid);
err = mlx5_cmd_exec(dev->mdev, cmd_in, err = mlx5_cmd_exec(dev->mdev, cmd_in,
uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_IN), uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_IN),
cmd_out, cmd_out_len); cmd_out, cmd_out_len);
if (err) if (err)
goto other_cmd_free;
err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_OUT, cmd_out, cmd_out_len);
other_cmd_free:
kvfree(cmd_out);
return err; return err;
return uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OTHER_CMD_OUT, cmd_out,
cmd_out_len);
} }
static void devx_obj_build_destroy_cmd(void *in, void *out, void *din, static void devx_obj_build_destroy_cmd(void *in, void *out, void *din,
...@@ -735,22 +732,20 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)( ...@@ -735,22 +732,20 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)(
if (!devx_is_obj_create_cmd(cmd_in)) if (!devx_is_obj_create_cmd(cmd_in))
return -EINVAL; return -EINVAL;
cmd_out = uverbs_zalloc(attrs, cmd_out_len);
if (IS_ERR(cmd_out))
return PTR_ERR(cmd_out);
obj = kzalloc(sizeof(struct devx_obj), GFP_KERNEL); obj = kzalloc(sizeof(struct devx_obj), GFP_KERNEL);
if (!obj) if (!obj)
return -ENOMEM; return -ENOMEM;
cmd_out = kvzalloc(cmd_out_len, GFP_KERNEL);
if (!cmd_out) {
err = -ENOMEM;
goto obj_free;
}
MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid); MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid);
err = mlx5_cmd_exec(dev->mdev, cmd_in, err = mlx5_cmd_exec(dev->mdev, cmd_in,
uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_IN), uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_IN),
cmd_out, cmd_out_len); cmd_out, cmd_out_len);
if (err) if (err)
goto cmd_free; goto obj_free;
uobj->object = obj; uobj->object = obj;
obj->mdev = dev->mdev; obj->mdev = dev->mdev;
...@@ -759,13 +754,10 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)( ...@@ -759,13 +754,10 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)(
err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_OUT, cmd_out, cmd_out_len); err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_OUT, cmd_out, cmd_out_len);
if (err) if (err)
goto cmd_free; goto obj_free;
kvfree(cmd_out);
return 0; return 0;
cmd_free:
kvfree(cmd_out);
obj_free: obj_free:
kfree(obj); kfree(obj);
return err; return err;
...@@ -793,23 +785,19 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_MODIFY)( ...@@ -793,23 +785,19 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_MODIFY)(
if (!devx_is_valid_obj_id(obj, cmd_in)) if (!devx_is_valid_obj_id(obj, cmd_in))
return -EINVAL; return -EINVAL;
cmd_out = kvzalloc(cmd_out_len, GFP_KERNEL); cmd_out = uverbs_zalloc(attrs, cmd_out_len);
if (!cmd_out) if (IS_ERR(cmd_out))
return -ENOMEM; return PTR_ERR(cmd_out);
MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid); MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid);
err = mlx5_cmd_exec(obj->mdev, cmd_in, err = mlx5_cmd_exec(obj->mdev, cmd_in,
uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN), uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_IN),
cmd_out, cmd_out_len); cmd_out, cmd_out_len);
if (err) if (err)
goto other_cmd_free; return err;
err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_OUT, return uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_MODIFY_CMD_OUT,
cmd_out, cmd_out_len); cmd_out, cmd_out_len);
other_cmd_free:
kvfree(cmd_out);
return err;
} }
static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_QUERY)( static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_QUERY)(
...@@ -834,22 +822,19 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_QUERY)( ...@@ -834,22 +822,19 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_QUERY)(
if (!devx_is_valid_obj_id(obj, cmd_in)) if (!devx_is_valid_obj_id(obj, cmd_in))
return -EINVAL; return -EINVAL;
cmd_out = kvzalloc(cmd_out_len, GFP_KERNEL); cmd_out = uverbs_zalloc(attrs, cmd_out_len);
if (!cmd_out) if (IS_ERR(cmd_out))
return -ENOMEM; return PTR_ERR(cmd_out);
MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid); MLX5_SET(general_obj_in_cmd_hdr, cmd_in, uid, c->devx_uid);
err = mlx5_cmd_exec(obj->mdev, cmd_in, err = mlx5_cmd_exec(obj->mdev, cmd_in,
uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN), uverbs_attr_get_len(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_IN),
cmd_out, cmd_out_len); cmd_out, cmd_out_len);
if (err) if (err)
goto other_cmd_free;
err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_OUT, cmd_out, cmd_out_len);
other_cmd_free:
kvfree(cmd_out);
return err; return err;
return uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_QUERY_CMD_OUT,
cmd_out, cmd_out_len);
} }
static int devx_umem_get(struct mlx5_ib_dev *dev, struct ib_ucontext *ucontext, static int devx_umem_get(struct mlx5_ib_dev *dev, struct ib_ucontext *ucontext,
...@@ -896,18 +881,14 @@ static int devx_umem_get(struct mlx5_ib_dev *dev, struct ib_ucontext *ucontext, ...@@ -896,18 +881,14 @@ static int devx_umem_get(struct mlx5_ib_dev *dev, struct ib_ucontext *ucontext,
return 0; return 0;
} }
static int devx_umem_reg_cmd_alloc(struct devx_umem *obj, static int devx_umem_reg_cmd_alloc(struct uverbs_attr_bundle *attrs,
struct devx_umem *obj,
struct devx_umem_reg_cmd *cmd) struct devx_umem_reg_cmd *cmd)
{ {
cmd->inlen = MLX5_ST_SZ_BYTES(create_umem_in) + cmd->inlen = MLX5_ST_SZ_BYTES(create_umem_in) +
(MLX5_ST_SZ_BYTES(mtt) * obj->ncont); (MLX5_ST_SZ_BYTES(mtt) * obj->ncont);
cmd->in = kvzalloc(cmd->inlen, GFP_KERNEL); cmd->in = uverbs_zalloc(attrs, cmd->inlen);
return cmd->in ? 0 : -ENOMEM; return PTR_ERR_OR_ZERO(cmd->in);
}
static void devx_umem_reg_cmd_free(struct devx_umem_reg_cmd *cmd)
{
kvfree(cmd->in);
} }
static void devx_umem_reg_cmd_build(struct mlx5_ib_dev *dev, static void devx_umem_reg_cmd_build(struct mlx5_ib_dev *dev,
...@@ -954,7 +935,7 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)( ...@@ -954,7 +935,7 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)(
if (err) if (err)
goto err_obj_free; goto err_obj_free;
err = devx_umem_reg_cmd_alloc(obj, &cmd); err = devx_umem_reg_cmd_alloc(attrs, obj, &cmd);
if (err) if (err)
goto err_umem_release; goto err_umem_release;
...@@ -964,7 +945,7 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)( ...@@ -964,7 +945,7 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)(
err = mlx5_cmd_exec(dev->mdev, cmd.in, cmd.inlen, cmd.out, err = mlx5_cmd_exec(dev->mdev, cmd.in, cmd.inlen, cmd.out,
sizeof(cmd.out)); sizeof(cmd.out));
if (err) if (err)
goto err_umem_reg_cmd_free; goto err_umem_release;
obj->mdev = dev->mdev; obj->mdev = dev->mdev;
uobj->object = obj; uobj->object = obj;
...@@ -973,14 +954,10 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)( ...@@ -973,14 +954,10 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_UMEM_REG)(
if (err) if (err)
goto err_umem_destroy; goto err_umem_destroy;
devx_umem_reg_cmd_free(&cmd);
return 0; return 0;
err_umem_destroy: err_umem_destroy:
mlx5_cmd_exec(obj->mdev, obj->dinbox, obj->dinlen, cmd.out, sizeof(cmd.out)); mlx5_cmd_exec(obj->mdev, obj->dinbox, obj->dinlen, cmd.out, sizeof(cmd.out));
err_umem_reg_cmd_free:
devx_umem_reg_cmd_free(&cmd);
err_umem_release: err_umem_release:
ib_umem_release(obj->umem); ib_umem_release(obj->umem);
err_obj_free: err_obj_free:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment