Skip to content

Commit cbfdd44

Browse files
monis410jgunthorpe
authored andcommitted
IB/uverbs: Add helper to get array size from ptr attribute
When the parser of an ioctl command has the knowledge that a ptr attribute in a bundle represents an array of structures, it is useful for it to know the number of elements in the array. This is done by dividing the attribute length with the element size. Signed-off-by: Moni Shoua <monis@mellanox.com> Reviewed-by: Guy Levi <guyle@mellanox.com> Signed-off-by: Leon Romanovsky <leonro@mellanox.com> Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
1 parent bbc13cd commit cbfdd44

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

drivers/infiniband/hw/mlx5/flow.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,26 +331,24 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_FLOW_ACTION_CREATE_MODIFY_HEADER)(
331331
struct ib_flow_action *action;
332332
size_t num_actions;
333333
void *in;
334-
int len;
335334
int ret;
336335

337336
if (!mlx5_ib_modify_header_supported(mdev))
338337
return -EOPNOTSUPP;
339338

340339
in = uverbs_attr_get_alloced_ptr(attrs,
341340
MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM);
342-
len = uverbs_attr_get_len(attrs,
343-
MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM);
344341

345-
if (len % MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto))
346-
return -EINVAL;
342+
num_actions = uverbs_attr_ptr_get_array_size(
343+
attrs, MLX5_IB_ATTR_CREATE_MODIFY_HEADER_ACTIONS_PRM,
344+
MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto));
345+
if (num_actions < 0)
346+
return num_actions;
347347

348348
ret = uverbs_get_const(&ft_type, attrs,
349349
MLX5_IB_ATTR_CREATE_MODIFY_HEADER_FT_TYPE);
350350
if (ret)
351351
return ret;
352-
353-
num_actions = len / MLX5_UN_SZ_BYTES(set_action_in_add_action_in_auto),
354352
action = mlx5_ib_create_modify_header(mdev, ft_type, num_actions, in);
355353
if (IS_ERR(action))
356354
return PTR_ERR(action);

include/rdma/uverbs_ioctl.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,28 @@ uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
719719
return attr->ptr_attr.len;
720720
}
721721

722+
/*
723+
* uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
724+
* attribute.
725+
* @attrs: The attribute bundle
726+
* @idx: The ID of the attribute
727+
* @elem_size: The size of the element in the array
728+
*/
729+
static inline int
730+
uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
731+
size_t elem_size)
732+
{
733+
int size = uverbs_attr_get_len(attrs, idx);
734+
735+
if (size < 0)
736+
return size;
737+
738+
if (size % elem_size)
739+
return -EINVAL;
740+
741+
return size / elem_size;
742+
}
743+
722744
/**
723745
* uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
724746
* UVERBS_ATTR_TYPE_IDRS_ARRAY.

0 commit comments

Comments
 (0)