RDMA/uverbs: Avoid -Wflex-array-member-not-at-end warnings

-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally.

There are currently a couple of objects (`alloc_head` and `bundle`) in
`struct bundle_priv` that contain a couple of flexible structures:

struct bundle_priv {
        /* Must be first */
        struct bundle_alloc_head alloc_head;

	...

        /*
         * Must be last. bundle ends in a flex array which overlaps
         * internal_buffer.
         */
        struct uverbs_attr_bundle bundle;
        u64 internal_buffer[32];
};

So, in order to avoid ending up with a couple of flexible-array members
in the middle of a struct, we use the `struct_group_tagged()` helper to
separate the flexible array from the rest of the members in the flexible
structures:

struct uverbs_attr_bundle {
        struct_group_tagged(uverbs_attr_bundle_hdr, hdr,
		... the rest of the members
        );
        struct uverbs_attr attrs[];
};

With the change described above, we now declare objects of the type of
the tagged struct without embedding flexible arrays in the middle of
another struct:

struct bundle_priv {
        /* Must be first */
        struct bundle_alloc_head_hdr alloc_head;

        ...

        struct uverbs_attr_bundle_hdr bundle;
        u64 internal_buffer[32];
};

We also use `container_of()` whenever we need to retrieve a pointer
to the flexible structures.

Notice that the `bundle_size` computed in `uapi_compute_bundle_size()`
remains the same.

So, with these changes, fix the following warnings:

drivers/infiniband/core/uverbs_ioctl.c:45:34: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
   45 |         struct bundle_alloc_head alloc_head;
      |                                  ^~~~~~~~~~
drivers/infiniband/core/uverbs_ioctl.c:67:35: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
   67 |         struct uverbs_attr_bundle bundle;
      |                                   ^~~~~~

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/ZeIgeZ5Sb0IZTOyt@neat
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Leon Romanovsky <leon@kernel.org>
This commit is contained in:
Gustavo A. R. Silva 2024-03-01 12:37:45 -06:00 committed by Leon Romanovsky
parent 6ec429d588
commit 155f04366e
2 changed files with 53 additions and 39 deletions

View File

@ -36,13 +36,15 @@
#include "uverbs.h" #include "uverbs.h"
struct bundle_alloc_head { struct bundle_alloc_head {
struct bundle_alloc_head *next; struct_group_tagged(bundle_alloc_head_hdr, hdr,
struct bundle_alloc_head *next;
);
u8 data[]; u8 data[];
}; };
struct bundle_priv { struct bundle_priv {
/* Must be first */ /* Must be first */
struct bundle_alloc_head alloc_head; struct bundle_alloc_head_hdr alloc_head;
struct bundle_alloc_head *allocated_mem; struct bundle_alloc_head *allocated_mem;
size_t internal_avail; size_t internal_avail;
size_t internal_used; size_t internal_used;
@ -64,7 +66,7 @@ struct bundle_priv {
* Must be last. bundle ends in a flex array which overlaps * Must be last. bundle ends in a flex array which overlaps
* internal_buffer. * internal_buffer.
*/ */
struct uverbs_attr_bundle bundle; struct uverbs_attr_bundle_hdr bundle;
u64 internal_buffer[32]; u64 internal_buffer[32];
}; };
@ -77,9 +79,10 @@ void uapi_compute_bundle_size(struct uverbs_api_ioctl_method *method_elm,
unsigned int num_attrs) unsigned int num_attrs)
{ {
struct bundle_priv *pbundle; struct bundle_priv *pbundle;
struct uverbs_attr_bundle *bundle;
size_t bundle_size = size_t bundle_size =
offsetof(struct bundle_priv, internal_buffer) + offsetof(struct bundle_priv, internal_buffer) +
sizeof(*pbundle->bundle.attrs) * method_elm->key_bitmap_len + sizeof(*bundle->attrs) * method_elm->key_bitmap_len +
sizeof(*pbundle->uattrs) * num_attrs; sizeof(*pbundle->uattrs) * num_attrs;
method_elm->use_stack = bundle_size <= sizeof(*pbundle); method_elm->use_stack = bundle_size <= sizeof(*pbundle);
@ -107,7 +110,7 @@ __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
gfp_t flags) gfp_t flags)
{ {
struct bundle_priv *pbundle = struct bundle_priv *pbundle =
container_of(bundle, struct bundle_priv, bundle); container_of(&bundle->hdr, struct bundle_priv, bundle);
size_t new_used; size_t new_used;
void *res; void *res;
@ -149,7 +152,7 @@ static int uverbs_set_output(const struct uverbs_attr_bundle *bundle,
const struct uverbs_attr *attr) const struct uverbs_attr *attr)
{ {
struct bundle_priv *pbundle = struct bundle_priv *pbundle =
container_of(bundle, struct bundle_priv, bundle); container_of(&bundle->hdr, struct bundle_priv, bundle);
u16 flags; u16 flags;
flags = pbundle->uattrs[attr->ptr_attr.uattr_idx].flags | flags = pbundle->uattrs[attr->ptr_attr.uattr_idx].flags |
@ -166,6 +169,8 @@ static int uverbs_process_idrs_array(struct bundle_priv *pbundle,
struct ib_uverbs_attr *uattr, struct ib_uverbs_attr *uattr,
u32 attr_bkey) u32 attr_bkey)
{ {
struct uverbs_attr_bundle *bundle =
container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
const struct uverbs_attr_spec *spec = &attr_uapi->spec; const struct uverbs_attr_spec *spec = &attr_uapi->spec;
size_t array_len; size_t array_len;
u32 *idr_vals; u32 *idr_vals;
@ -184,7 +189,7 @@ static int uverbs_process_idrs_array(struct bundle_priv *pbundle,
return -EINVAL; return -EINVAL;
attr->uobjects = attr->uobjects =
uverbs_alloc(&pbundle->bundle, uverbs_alloc(bundle,
array_size(array_len, sizeof(*attr->uobjects))); array_size(array_len, sizeof(*attr->uobjects)));
if (IS_ERR(attr->uobjects)) if (IS_ERR(attr->uobjects))
return PTR_ERR(attr->uobjects); return PTR_ERR(attr->uobjects);
@ -209,7 +214,7 @@ static int uverbs_process_idrs_array(struct bundle_priv *pbundle,
for (i = 0; i != array_len; i++) { for (i = 0; i != array_len; i++) {
attr->uobjects[i] = uverbs_get_uobject_from_file( attr->uobjects[i] = uverbs_get_uobject_from_file(
spec->u2.objs_arr.obj_type, spec->u2.objs_arr.access, spec->u2.objs_arr.obj_type, spec->u2.objs_arr.access,
idr_vals[i], &pbundle->bundle); idr_vals[i], bundle);
if (IS_ERR(attr->uobjects[i])) { if (IS_ERR(attr->uobjects[i])) {
ret = PTR_ERR(attr->uobjects[i]); ret = PTR_ERR(attr->uobjects[i]);
break; break;
@ -240,7 +245,9 @@ static int uverbs_process_attr(struct bundle_priv *pbundle,
struct ib_uverbs_attr *uattr, u32 attr_bkey) struct ib_uverbs_attr *uattr, u32 attr_bkey)
{ {
const struct uverbs_attr_spec *spec = &attr_uapi->spec; const struct uverbs_attr_spec *spec = &attr_uapi->spec;
struct uverbs_attr *e = &pbundle->bundle.attrs[attr_bkey]; struct uverbs_attr_bundle *bundle =
container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
struct uverbs_attr *e = &bundle->attrs[attr_bkey];
const struct uverbs_attr_spec *val_spec = spec; const struct uverbs_attr_spec *val_spec = spec;
struct uverbs_obj_attr *o_attr; struct uverbs_obj_attr *o_attr;
@ -288,7 +295,7 @@ static int uverbs_process_attr(struct bundle_priv *pbundle,
if (val_spec->alloc_and_copy && !uverbs_attr_ptr_is_inline(e)) { if (val_spec->alloc_and_copy && !uverbs_attr_ptr_is_inline(e)) {
void *p; void *p;
p = uverbs_alloc(&pbundle->bundle, uattr->len); p = uverbs_alloc(bundle, uattr->len);
if (IS_ERR(p)) if (IS_ERR(p))
return PTR_ERR(p); return PTR_ERR(p);
@ -321,7 +328,7 @@ static int uverbs_process_attr(struct bundle_priv *pbundle,
*/ */
o_attr->uobject = uverbs_get_uobject_from_file( o_attr->uobject = uverbs_get_uobject_from_file(
spec->u.obj.obj_type, spec->u.obj.access, spec->u.obj.obj_type, spec->u.obj.access,
uattr->data_s64, &pbundle->bundle); uattr->data_s64, bundle);
if (IS_ERR(o_attr->uobject)) if (IS_ERR(o_attr->uobject))
return PTR_ERR(o_attr->uobject); return PTR_ERR(o_attr->uobject);
__set_bit(attr_bkey, pbundle->uobj_finalize); __set_bit(attr_bkey, pbundle->uobj_finalize);
@ -422,6 +429,8 @@ static int ib_uverbs_run_method(struct bundle_priv *pbundle,
unsigned int num_attrs) unsigned int num_attrs)
{ {
int (*handler)(struct uverbs_attr_bundle *attrs); int (*handler)(struct uverbs_attr_bundle *attrs);
struct uverbs_attr_bundle *bundle =
container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
size_t uattrs_size = array_size(sizeof(*pbundle->uattrs), num_attrs); size_t uattrs_size = array_size(sizeof(*pbundle->uattrs), num_attrs);
unsigned int destroy_bkey = pbundle->method_elm->destroy_bkey; unsigned int destroy_bkey = pbundle->method_elm->destroy_bkey;
unsigned int i; unsigned int i;
@ -434,7 +443,7 @@ static int ib_uverbs_run_method(struct bundle_priv *pbundle,
if (!handler) if (!handler)
return -EIO; return -EIO;
pbundle->uattrs = uverbs_alloc(&pbundle->bundle, uattrs_size); pbundle->uattrs = uverbs_alloc(bundle, uattrs_size);
if (IS_ERR(pbundle->uattrs)) if (IS_ERR(pbundle->uattrs))
return PTR_ERR(pbundle->uattrs); return PTR_ERR(pbundle->uattrs);
if (copy_from_user(pbundle->uattrs, pbundle->user_attrs, uattrs_size)) if (copy_from_user(pbundle->uattrs, pbundle->user_attrs, uattrs_size))
@ -453,25 +462,23 @@ static int ib_uverbs_run_method(struct bundle_priv *pbundle,
return -EINVAL; return -EINVAL;
if (pbundle->method_elm->has_udata) if (pbundle->method_elm->has_udata)
uverbs_fill_udata(&pbundle->bundle, uverbs_fill_udata(bundle, &pbundle->bundle.driver_udata,
&pbundle->bundle.driver_udata,
UVERBS_ATTR_UHW_IN, UVERBS_ATTR_UHW_OUT); UVERBS_ATTR_UHW_IN, UVERBS_ATTR_UHW_OUT);
else else
pbundle->bundle.driver_udata = (struct ib_udata){}; pbundle->bundle.driver_udata = (struct ib_udata){};
if (destroy_bkey != UVERBS_API_ATTR_BKEY_LEN) { if (destroy_bkey != UVERBS_API_ATTR_BKEY_LEN) {
struct uverbs_obj_attr *destroy_attr = struct uverbs_obj_attr *destroy_attr = &bundle->attrs[destroy_bkey].obj_attr;
&pbundle->bundle.attrs[destroy_bkey].obj_attr;
ret = uobj_destroy(destroy_attr->uobject, &pbundle->bundle); ret = uobj_destroy(destroy_attr->uobject, bundle);
if (ret) if (ret)
return ret; return ret;
__clear_bit(destroy_bkey, pbundle->uobj_finalize); __clear_bit(destroy_bkey, pbundle->uobj_finalize);
ret = handler(&pbundle->bundle); ret = handler(bundle);
uobj_put_destroy(destroy_attr->uobject); uobj_put_destroy(destroy_attr->uobject);
} else { } else {
ret = handler(&pbundle->bundle); ret = handler(bundle);
} }
/* /*
@ -481,10 +488,10 @@ static int ib_uverbs_run_method(struct bundle_priv *pbundle,
*/ */
if (!ret && pbundle->method_elm->has_udata) { if (!ret && pbundle->method_elm->has_udata) {
const struct uverbs_attr *attr = const struct uverbs_attr *attr =
uverbs_attr_get(&pbundle->bundle, UVERBS_ATTR_UHW_OUT); uverbs_attr_get(bundle, UVERBS_ATTR_UHW_OUT);
if (!IS_ERR(attr)) if (!IS_ERR(attr))
ret = uverbs_set_output(&pbundle->bundle, attr); ret = uverbs_set_output(bundle, attr);
} }
/* /*
@ -501,6 +508,8 @@ static int ib_uverbs_run_method(struct bundle_priv *pbundle,
static void bundle_destroy(struct bundle_priv *pbundle, bool commit) static void bundle_destroy(struct bundle_priv *pbundle, bool commit)
{ {
unsigned int key_bitmap_len = pbundle->method_elm->key_bitmap_len; unsigned int key_bitmap_len = pbundle->method_elm->key_bitmap_len;
struct uverbs_attr_bundle *bundle =
container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
struct bundle_alloc_head *memblock; struct bundle_alloc_head *memblock;
unsigned int i; unsigned int i;
@ -508,20 +517,19 @@ static void bundle_destroy(struct bundle_priv *pbundle, bool commit)
i = -1; i = -1;
while ((i = find_next_bit(pbundle->uobj_finalize, key_bitmap_len, while ((i = find_next_bit(pbundle->uobj_finalize, key_bitmap_len,
i + 1)) < key_bitmap_len) { i + 1)) < key_bitmap_len) {
struct uverbs_attr *attr = &pbundle->bundle.attrs[i]; struct uverbs_attr *attr = &bundle->attrs[i];
uverbs_finalize_object( uverbs_finalize_object(
attr->obj_attr.uobject, attr->obj_attr.uobject,
attr->obj_attr.attr_elm->spec.u.obj.access, attr->obj_attr.attr_elm->spec.u.obj.access,
test_bit(i, pbundle->uobj_hw_obj_valid), test_bit(i, pbundle->uobj_hw_obj_valid),
commit, commit, bundle);
&pbundle->bundle);
} }
i = -1; i = -1;
while ((i = find_next_bit(pbundle->spec_finalize, key_bitmap_len, while ((i = find_next_bit(pbundle->spec_finalize, key_bitmap_len,
i + 1)) < key_bitmap_len) { i + 1)) < key_bitmap_len) {
struct uverbs_attr *attr = &pbundle->bundle.attrs[i]; struct uverbs_attr *attr = &bundle->attrs[i];
const struct uverbs_api_attr *attr_uapi; const struct uverbs_api_attr *attr_uapi;
void __rcu **slot; void __rcu **slot;
@ -535,7 +543,7 @@ static void bundle_destroy(struct bundle_priv *pbundle, bool commit)
if (attr_uapi->spec.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) { if (attr_uapi->spec.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) {
uverbs_free_idrs_array(attr_uapi, &attr->objs_arr_attr, uverbs_free_idrs_array(attr_uapi, &attr->objs_arr_attr,
commit, &pbundle->bundle); commit, bundle);
} }
} }
@ -578,7 +586,8 @@ static int ib_uverbs_cmd_verbs(struct ib_uverbs_file *ufile,
method_elm->bundle_size - method_elm->bundle_size -
offsetof(struct bundle_priv, internal_buffer); offsetof(struct bundle_priv, internal_buffer);
pbundle->alloc_head.next = NULL; pbundle->alloc_head.next = NULL;
pbundle->allocated_mem = &pbundle->alloc_head; pbundle->allocated_mem = container_of(&pbundle->alloc_head,
struct bundle_alloc_head, hdr);
} else { } else {
pbundle = &onstack; pbundle = &onstack;
pbundle->internal_avail = sizeof(pbundle->internal_buffer); pbundle->internal_avail = sizeof(pbundle->internal_buffer);
@ -596,8 +605,9 @@ static int ib_uverbs_cmd_verbs(struct ib_uverbs_file *ufile,
pbundle->user_attrs = user_attrs; pbundle->user_attrs = user_attrs;
pbundle->internal_used = ALIGN(pbundle->method_elm->key_bitmap_len * pbundle->internal_used = ALIGN(pbundle->method_elm->key_bitmap_len *
sizeof(*pbundle->bundle.attrs), sizeof(*container_of(&pbundle->bundle,
sizeof(*pbundle->internal_buffer)); struct uverbs_attr_bundle, hdr)->attrs),
sizeof(*pbundle->internal_buffer));
memset(pbundle->bundle.attr_present, 0, memset(pbundle->bundle.attr_present, 0,
sizeof(pbundle->bundle.attr_present)); sizeof(pbundle->bundle.attr_present));
memset(pbundle->uobj_finalize, 0, sizeof(pbundle->uobj_finalize)); memset(pbundle->uobj_finalize, 0, sizeof(pbundle->uobj_finalize));
@ -700,11 +710,13 @@ void uverbs_fill_udata(struct uverbs_attr_bundle *bundle,
unsigned int attr_out) unsigned int attr_out)
{ {
struct bundle_priv *pbundle = struct bundle_priv *pbundle =
container_of(bundle, struct bundle_priv, bundle); container_of(&bundle->hdr, struct bundle_priv, bundle);
struct uverbs_attr_bundle *bundle_aux =
container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr);
const struct uverbs_attr *in = const struct uverbs_attr *in =
uverbs_attr_get(&pbundle->bundle, attr_in); uverbs_attr_get(bundle_aux, attr_in);
const struct uverbs_attr *out = const struct uverbs_attr *out =
uverbs_attr_get(&pbundle->bundle, attr_out); uverbs_attr_get(bundle_aux, attr_out);
if (!IS_ERR(in)) { if (!IS_ERR(in)) {
udata->inlen = in->ptr_attr.len; udata->inlen = in->ptr_attr.len;
@ -829,7 +841,7 @@ void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *bundle,
u16 idx) u16 idx)
{ {
struct bundle_priv *pbundle = struct bundle_priv *pbundle =
container_of(bundle, struct bundle_priv, bundle); container_of(&bundle->hdr, struct bundle_priv, bundle);
__set_bit(uapi_bkey_attr(uapi_key_attr(idx)), __set_bit(uapi_bkey_attr(uapi_key_attr(idx)),
pbundle->uobj_hw_obj_valid); pbundle->uobj_hw_obj_valid);

View File

@ -629,12 +629,14 @@ struct uverbs_attr {
}; };
struct uverbs_attr_bundle { struct uverbs_attr_bundle {
struct ib_udata driver_udata; struct_group_tagged(uverbs_attr_bundle_hdr, hdr,
struct ib_udata ucore; struct ib_udata driver_udata;
struct ib_uverbs_file *ufile; struct ib_udata ucore;
struct ib_ucontext *context; struct ib_uverbs_file *ufile;
struct ib_uobject *uobject; struct ib_ucontext *context;
DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN); struct ib_uobject *uobject;
DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
);
struct uverbs_attr attrs[]; struct uverbs_attr attrs[];
}; };