RE: [Intel-wired-lan] [PATCH][next] i40e/i40e_virtchnl_pf: Use struct_size() in kzalloc()

From: Bowers, AndrewX
Date: Thu Jun 13 2019 - 17:38:19 EST


> -----Original Message-----
> From: Intel-wired-lan [mailto:intel-wired-lan-bounces@xxxxxxxxxx] On
> Behalf Of Gustavo A. R. Silva
> Sent: Wednesday, June 5, 2019 8:41 AM
> To: Kirsher, Jeffrey T <jeffrey.t.kirsher@xxxxxxxxx>; David S. Miller
> <davem@xxxxxxxxxxxxx>
> Cc: netdev@xxxxxxxxxxxxxxx; intel-wired-lan@xxxxxxxxxxxxxxxx; linux-
> kernel@xxxxxxxxxxxxxxx
> Subject: [Intel-wired-lan] [PATCH][next] i40e/i40e_virtchnl_pf: Use
> struct_size() in kzalloc()
>
> 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: Gustavo A. R. Silva <gustavo@xxxxxxxxxxxxxx>
> ---
> .../net/ethernet/intel/i40e/i40e_virtchnl_pf.c | 15 ++++++---------
> 1 file changed, 6 insertions(+), 9 deletions(-)

Tested-by: Andrew Bowers <andrewx.bowers@xxxxxxxxx>