mirror_ubuntu-kernels/drivers/infiniband/hw
Leon Romanovsky e537deecda RDMA/mana_ib: Add flex array to struct mana_cfg_rx_steer_req_v2
The "struct mana_cfg_rx_steer_req_v2" uses a dynamically sized set of
trailing elements. Specifically, it uses a "mana_handle_t" array. So,
use the preferred way in the kernel declaring a flexible array [1].

At the same time, prepare for the coming implementation by GCC and Clang
of the __counted_by attribute. Flexible array members annotated with
__counted_by can have their accesses bounds-checked at run-time via
CONFIG_UBSAN_BOUNDS (for array indexing) and CONFIG_FORTIFY_SOURCE (for
strcpy/memcpy-family functions).

Also, avoid the open-coded arithmetic in the memory allocator functions
[2] using the "struct_size" macro.

Moreover, use the "offsetof" helper to get the indirect table offset
instead of the "sizeof" operator and avoid the open-coded arithmetic in
pointers using the new flex member. This new structure member also allow
us to remove the "req_indir_tab" variable since it is no longer needed.

Now, it is also possible to use the "flex_array_size" helper to compute
the size of these trailing elements in the "memcpy" function.

Specifically, the first commit adds the flex member and the patches 2
and 3 refactor the consumers of the "struct mana_cfg_rx_steer_req_v2".

This code was detected with the help of Coccinelle, and audited and
modified manually. The Coccinelle script used to detect this code
pattern is the following:

virtual report

@rule1@
type t1;
type t2;
identifier i0;
identifier i1;
identifier i2;
identifier ALLOC =~
"kmalloc|kzalloc|kmalloc_node|kzalloc_node|vmalloc|vzalloc|kvmalloc|kvzalloc";
position p1;
@@

i0 = sizeof(t1) + sizeof(t2) * i1;
...
i2 = ALLOC@p1(..., i0, ...);

@script:python depends on report@
p1 << rule1.p1;
@@

msg = "WARNING: verify allocation on line %s" % (p1[0].line)
coccilib.report.print_report(p1[0],msg)

[1] https://www.kernel.org/doc/html/next/process/deprecated.html#zero-length-and-one-element-arrays
[2] https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments
Link: https://lore.kernel.org/all/AS8PR02MB72374BD1B23728F2E3C3B1A18B022@AS8PR02MB7237.eurprd02.prod.outlook.com
Signed-off-by: Erick Archer <erick.archer@outlook.com>
Signed-off-by: Leon Romanovsky <leon@kernel.org>

* mana-ib-flex:
  net: mana: Avoid open coded arithmetic
  RDMA/mana_ib: Prefer struct_size over open coded arithmetic
  net: mana: Add flex array to struct mana_cfg_rx_steer_req_v2
2024-04-11 13:48:53 +03:00
..
bnxt_re RDMA/bnxt_re: Add a missing check in bnxt_qplib_query_srq 2024-01-25 12:04:39 +02:00
cxgb4 RDMA/cxgb4: Delete unused c4iw_ep_redirect prototype 2024-01-25 12:05:30 +02:00
efa RDMA/efa: Limit EQs to available MSI-X vectors 2024-01-31 14:32:26 +02:00
erdma RDMA/erdma: Remove unnecessary __GFP_ZERO flag 2024-04-01 14:46:01 +03:00
hfi1 RDMA v6.9 2024-03-18 15:34:03 -07:00
hns RDMA/hns: Support DSCP 2024-04-09 10:27:41 +03:00
irdma RDMA v6.9 2024-03-18 15:34:03 -07:00
mana RDMA/mana_ib: Add flex array to struct mana_cfg_rx_steer_req_v2 2024-04-11 13:48:53 +03:00
mlx4 IB/mlx4: Fix the size of a buffer in add_port_entries() 2023-09-23 21:53:24 +03:00
mlx5 RDMA/mlx5: Adding remote atomic access flag to updatable flags 2024-04-08 13:34:54 +03:00
mthca RDMA/usnic: Silence uninitialized symbol smatch warnings 2023-11-15 15:57:39 +02:00
ocrdma RDMA: Remove unnecessary ternary operators 2023-07-31 15:16:12 +03:00
qedr RDMA/qedr: Fix qedr_create_user_qp error flow 2024-02-12 13:49:04 +02:00
qib RDMA for v6.7 2023-11-02 15:20:30 -10:00
usnic RDMA/usnic: Annotate struct usnic_uiom_chunk with __counted_by 2023-10-02 14:44:54 +03:00
vmw_pvrdma RDMA/core: Remove NULL check before dev_{put, hold} 2023-10-24 18:16:04 +03:00
Makefile RDMA/mana_ib: Add a driver for Microsoft Azure Network Adapter 2022-11-11 11:40:09 +02:00