Skip to content

Commit

Permalink
net/mlx5: Replace zero-length array with flexible-array
Browse files Browse the repository at this point in the history
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:

struct foo {
        int stuff;
        struct boo array[];
};

By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.

Also, notice that, dynamic memory allocations won't be affected by
this change:

"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]

sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.

This issue was found with the help of Coccinelle.

[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 7649773 ("cxgb3/l2t: Fix undefined behaviour")

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Saeed Mahameed <saeedm@mellanox.com>
  • Loading branch information
Gustavo A. R. Silva authored and Saeed Mahameed committed May 11, 2020
1 parent c6bc604 commit b6ca09c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion include/linux/mlx5/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ struct mlx5_rsc_debug {
void *object;
enum dbg_rsc_type type;
struct dentry *root;
struct mlx5_field_desc fields[0];
struct mlx5_field_desc fields[];
};

enum mlx5_dev_event {
Expand Down
66 changes: 33 additions & 33 deletions include/linux/mlx5/mlx5_ifc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ struct mlx5_ifc_wq_bits {

u8 reserved_at_140[0x4c0];

struct mlx5_ifc_cmd_pas_bits pas[0];
struct mlx5_ifc_cmd_pas_bits pas[];
};

struct mlx5_ifc_rq_num_bits {
Expand Down Expand Up @@ -1921,7 +1921,7 @@ struct mlx5_ifc_resource_dump_menu_segment_bits {
u8 reserved_at_20[0x10];
u8 num_of_records[0x10];

struct mlx5_ifc_resource_dump_menu_record_bits record[0];
struct mlx5_ifc_resource_dump_menu_record_bits record[];
};

struct mlx5_ifc_resource_dump_resource_segment_bits {
Expand All @@ -1933,7 +1933,7 @@ struct mlx5_ifc_resource_dump_resource_segment_bits {

u8 index2[0x20];

u8 payload[0][0x20];
u8 payload[][0x20];
};

struct mlx5_ifc_resource_dump_terminate_segment_bits {
Expand Down Expand Up @@ -3010,7 +3010,7 @@ struct mlx5_ifc_flow_context_bits {

u8 reserved_at_1200[0x600];

union mlx5_ifc_dest_format_struct_flow_counter_list_auto_bits destination[0];
union mlx5_ifc_dest_format_struct_flow_counter_list_auto_bits destination[];
};

enum {
Expand Down Expand Up @@ -3303,7 +3303,7 @@ struct mlx5_ifc_rqtc_bits {

u8 reserved_at_e0[0x6a0];

struct mlx5_ifc_rq_num_bits rq_num[0];
struct mlx5_ifc_rq_num_bits rq_num[];
};

enum {
Expand Down Expand Up @@ -3415,7 +3415,7 @@ struct mlx5_ifc_nic_vport_context_bits {

u8 reserved_at_7e0[0x20];

u8 current_uc_mac_address[0][0x40];
u8 current_uc_mac_address[][0x40];
};

enum {
Expand Down Expand Up @@ -4338,7 +4338,7 @@ struct mlx5_ifc_query_xrc_srq_out_bits {

u8 reserved_at_280[0x600];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_query_xrc_srq_in_bits {
Expand Down Expand Up @@ -4616,7 +4616,7 @@ struct mlx5_ifc_query_srq_out_bits {

u8 reserved_at_280[0x600];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_query_srq_in_bits {
Expand Down Expand Up @@ -4827,7 +4827,7 @@ struct mlx5_ifc_query_qp_out_bits {

u8 reserved_at_800[0x80];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_query_qp_in_bits {
Expand Down Expand Up @@ -5160,7 +5160,7 @@ struct mlx5_ifc_query_hca_vport_pkey_out_bits {

u8 reserved_at_40[0x40];

struct mlx5_ifc_pkey_bits pkey[0];
struct mlx5_ifc_pkey_bits pkey[];
};

struct mlx5_ifc_query_hca_vport_pkey_in_bits {
Expand Down Expand Up @@ -5196,7 +5196,7 @@ struct mlx5_ifc_query_hca_vport_gid_out_bits {
u8 gids_num[0x10];
u8 reserved_at_70[0x10];

struct mlx5_ifc_array128_auto_bits gid[0];
struct mlx5_ifc_array128_auto_bits gid[];
};

struct mlx5_ifc_query_hca_vport_gid_in_bits {
Expand Down Expand Up @@ -5464,7 +5464,7 @@ struct mlx5_ifc_query_flow_counter_out_bits {

u8 reserved_at_40[0x40];

struct mlx5_ifc_traffic_counter_bits flow_statistics[0];
struct mlx5_ifc_traffic_counter_bits flow_statistics[];
};

struct mlx5_ifc_query_flow_counter_in_bits {
Expand Down Expand Up @@ -5558,7 +5558,7 @@ struct mlx5_ifc_query_eq_out_bits {

u8 reserved_at_300[0x580];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_query_eq_in_bits {
Expand All @@ -5583,7 +5583,7 @@ struct mlx5_ifc_packet_reformat_context_in_bits {
u8 reserved_at_20[0x10];
u8 reformat_data[2][0x8];

u8 more_reformat_data[0][0x8];
u8 more_reformat_data[][0x8];
};

struct mlx5_ifc_query_packet_reformat_context_out_bits {
Expand All @@ -5594,7 +5594,7 @@ struct mlx5_ifc_query_packet_reformat_context_out_bits {

u8 reserved_at_40[0xa0];

struct mlx5_ifc_packet_reformat_context_in_bits packet_reformat_context[0];
struct mlx5_ifc_packet_reformat_context_in_bits packet_reformat_context[];
};

struct mlx5_ifc_query_packet_reformat_context_in_bits {
Expand Down Expand Up @@ -5833,7 +5833,7 @@ struct mlx5_ifc_query_cq_out_bits {

u8 reserved_at_280[0x600];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_query_cq_in_bits {
Expand Down Expand Up @@ -6440,7 +6440,7 @@ struct mlx5_ifc_modify_cq_in_bits {

u8 reserved_at_300[0x580];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_modify_cong_status_out_bits {
Expand Down Expand Up @@ -6504,7 +6504,7 @@ struct mlx5_ifc_manage_pages_out_bits {

u8 reserved_at_60[0x20];

u8 pas[0][0x40];
u8 pas[][0x40];
};

enum {
Expand All @@ -6526,7 +6526,7 @@ struct mlx5_ifc_manage_pages_in_bits {

u8 input_num_entries[0x20];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_mad_ifc_out_bits {
Expand Down Expand Up @@ -7481,7 +7481,7 @@ struct mlx5_ifc_create_xrc_srq_in_bits {

u8 reserved_at_300[0x580];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_create_tis_out_bits {
Expand Down Expand Up @@ -7557,7 +7557,7 @@ struct mlx5_ifc_create_srq_in_bits {

u8 reserved_at_280[0x600];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_create_sq_out_bits {
Expand Down Expand Up @@ -7718,7 +7718,7 @@ struct mlx5_ifc_create_qp_in_bits {
u8 wq_umem_valid[0x1];
u8 reserved_at_861[0x1f];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_create_psv_out_bits {
Expand Down Expand Up @@ -7789,7 +7789,7 @@ struct mlx5_ifc_create_mkey_in_bits {

u8 reserved_at_320[0x560];

u8 klm_pas_mtt[0][0x20];
u8 klm_pas_mtt[][0x20];
};

enum {
Expand Down Expand Up @@ -7922,7 +7922,7 @@ struct mlx5_ifc_create_eq_in_bits {

u8 reserved_at_3c0[0x4c0];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_create_dct_out_bits {
Expand Down Expand Up @@ -7979,7 +7979,7 @@ struct mlx5_ifc_create_cq_in_bits {
u8 cq_umem_valid[0x1];
u8 reserved_at_2e1[0x59f];

u8 pas[0][0x40];
u8 pas[][0x40];
};

struct mlx5_ifc_config_int_moderation_out_bits {
Expand Down Expand Up @@ -8335,7 +8335,7 @@ struct mlx5_ifc_access_register_out_bits {

u8 reserved_at_40[0x40];

u8 register_data[0][0x20];
u8 register_data[][0x20];
};

enum {
Expand All @@ -8355,7 +8355,7 @@ struct mlx5_ifc_access_register_in_bits {

u8 argument[0x20];

u8 register_data[0][0x20];
u8 register_data[][0x20];
};

struct mlx5_ifc_sltp_reg_bits {
Expand Down Expand Up @@ -9372,7 +9372,7 @@ struct mlx5_ifc_cmd_in_bits {
u8 reserved_at_20[0x10];
u8 op_mod[0x10];

u8 command[0][0x20];
u8 command[][0x20];
};

struct mlx5_ifc_cmd_if_box_bits {
Expand Down Expand Up @@ -9666,7 +9666,7 @@ struct mlx5_ifc_mcqi_reg_bits {
u8 reserved_at_a0[0x10];
u8 data_size[0x10];

union mlx5_ifc_mcqi_reg_data_bits data[0];
union mlx5_ifc_mcqi_reg_data_bits data[];
};

struct mlx5_ifc_mcc_reg_bits {
Expand Down Expand Up @@ -10252,7 +10252,7 @@ struct mlx5_ifc_umem_bits {

u8 num_of_mtt[0x40];

struct mlx5_ifc_mtt_bits mtt[0];
struct mlx5_ifc_mtt_bits mtt[];
};

struct mlx5_ifc_uctx_bits {
Expand Down Expand Up @@ -10377,7 +10377,7 @@ struct mlx5_ifc_mtrc_stdb_bits {
u8 reserved_at_4[0x4];
u8 read_size[0x18];
u8 start_offset[0x20];
u8 string_db_data[0];
u8 string_db_data[];
};

struct mlx5_ifc_mtrc_ctrl_bits {
Expand Down Expand Up @@ -10431,7 +10431,7 @@ struct mlx5_ifc_query_esw_functions_out_bits {
struct mlx5_ifc_host_params_context_bits host_params_context;

u8 reserved_at_280[0x180];
u8 host_sf_enable[0][0x40];
u8 host_sf_enable[][0x40];
};

struct mlx5_ifc_sf_partition_bits {
Expand All @@ -10451,7 +10451,7 @@ struct mlx5_ifc_query_sf_partitions_out_bits {

u8 reserved_at_60[0x20];

struct mlx5_ifc_sf_partition_bits sf_partition[0];
struct mlx5_ifc_sf_partition_bits sf_partition[];
};

struct mlx5_ifc_query_sf_partitions_in_bits {
Expand Down
2 changes: 1 addition & 1 deletion include/linux/mlx5/qp.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ struct mlx5_wqe_signature_seg {

struct mlx5_wqe_inline_seg {
__be32 byte_count;
__be32 data[0];
__be32 data[];
};

enum mlx5_sig_type {
Expand Down

0 comments on commit b6ca09c

Please sign in to comment.