Skip to content

Commit

Permalink
IB/uverbs: Add PTR_IN attributes that are allocated/copied automatically
Browse files Browse the repository at this point in the history
Adding UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY flag to PTR_IN attributes.
By using this flag, the parse automatically allocates and copies the
user-space data. This data is accessible by using uverbs_attr_get_len
and uverbs_attr_get_alloced_ptr inline accessor functions from the
handler.

Signed-off-by: Matan Barak <matanb@mellanox.com>
Signed-off-by: Yishai Hadas <yishaih@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
  • Loading branch information
Matan Barak authored and Jason Gunthorpe committed Jun 19, 2018
1 parent 9442d8b commit 8762d14
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
25 changes: 24 additions & 1 deletion drivers/infiniband/core/uverbs_ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,27 @@ static int uverbs_process_attr(struct ib_device *ibdev,
uattr->attr_data.reserved)
return -EINVAL;

e->ptr_attr.data = uattr->data;
e->ptr_attr.len = uattr->len;
e->ptr_attr.flags = uattr->flags;

if (val_spec->flags & UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY &&
!uverbs_attr_ptr_is_inline(e)) {
void *p;

p = kvmalloc(uattr->len, GFP_KERNEL);
if (!p)
return -ENOMEM;

e->ptr_attr.ptr = p;

if (copy_from_user(p, u64_to_user_ptr(uattr->data),
uattr->len)) {
kvfree(p);
return -EFAULT;
}
} else {
e->ptr_attr.data = uattr->data;
}
break;

case UVERBS_ATTR_TYPE_IDR:
Expand Down Expand Up @@ -200,6 +218,11 @@ static int uverbs_finalize_attrs(struct uverbs_attr_bundle *attrs_bundle,
spec->obj.access, commit);
if (!ret)
ret = current_ret;
} else if (spec->type == UVERBS_ATTR_TYPE_PTR_IN &&
spec->flags &
UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY &&
!uverbs_attr_ptr_is_inline(attr)) {
kvfree(attr->ptr_attr.ptr);
}
}
}
Expand Down
36 changes: 35 additions & 1 deletion include/rdma/uverbs_ioctl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ enum {
UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
/* Support extending attributes by length, validate all unknown size == zero */
UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1,
/*
* Valid only for PTR_IN. Allocate and copy the data inside the parser
*/
UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY = 1U << 2,
};

/* Specification of a single attribute inside the ioctl message */
Expand Down Expand Up @@ -323,7 +327,14 @@ struct uverbs_object_tree_def {
*/

struct uverbs_ptr_attr {
u64 data;
/*
* If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
* used.
*/
union {
void *ptr;
u64 data;
};
u16 len;
/* Combination of bits from enum UVERBS_ATTR_F_XXXX */
u16 flags;
Expand Down Expand Up @@ -431,6 +442,17 @@ static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_att
return attr->obj_attr.uobject;
}

static inline int
uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
{
const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);

if (IS_ERR(attr))
return PTR_ERR(attr);

return attr->ptr_attr.len;
}

static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
size_t idx, const void *from, size_t size)
{
Expand All @@ -457,6 +479,18 @@ static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
}

static inline void *uverbs_attr_get_alloced_ptr(
const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
{
const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);

if (IS_ERR(attr))
return (void *)attr;

return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
attr->ptr_attr.ptr;
}

static inline int _uverbs_copy_from(void *to,
const struct uverbs_attr_bundle *attrs_bundle,
size_t idx,
Expand Down

0 comments on commit 8762d14

Please sign in to comment.