• Gustavo A. R. Silva's avatar
    i40e/i40e_virtchnl_pf: Use struct_size() in kzalloc() · fae6cad1
    Gustavo A. R. Silva authored
    One of the more common cases of allocation size calculations is finding
    the size of a structure that has a zero-sized array at the end, along
    with memory for some number of elements for that array. For example:
    
    struct virtchnl_iwarp_qvlist_info {
    	...
            struct virtchnl_iwarp_qv_info qv_info[1];
    };
    
    size = sizeof(struct virtchnl_iwarp_qvlist_info) + (sizeof(struct virtchnl_iwarp_qv_info) * count;
    instance = kzalloc(size, GFP_KERNEL);
    
    and
    
    struct virtchnl_vf_resource {
    	...
            struct virtchnl_vsi_resource vsi_res[1];
    };
    
    size = sizeof(struct virtchnl_vf_resource) + sizeof(struct virtchnl_vsi_resource) * count;
    instance = kzalloc(size, GFP_KERNEL);
    
    Instead of leaving these open-coded and prone to type mistakes, we can
    now use the new struct_size() helper:
    
    instance = kzalloc(struct_size(instance, qv_info, count), GFP_KERNEL);
    
    and
    
    instance = kzalloc(struct_size(instance, vsi_res, count), GFP_KERNEL);
    
    Notice that, in the first case above, variable size is not necessary, hence it
    is removed.
    
    This code was detected with the help of Coccinelle.
    Signed-off-by: default avatar"Gustavo A. R. Silva" <gustavo@embeddedor.com>
    Tested-by: default avatarAndrew Bowers <andrewx.bowers@intel.com>
    Signed-off-by: default avatarJeff Kirsher <jeffrey.t.kirsher@intel.com>
    fae6cad1
i40e_virtchnl_pf.c 118 KB