> -----Original Message----- > From: Intel-wired-lan [mailto:intel-wired-lan-boun...@osuosl.org] On > Behalf Of Gustavo A. R. Silva > Sent: Wednesday, June 5, 2019 8:41 AM > To: Kirsher, Jeffrey T <jeffrey.t.kirs...@intel.com>; David S. Miller > <da...@davemloft.net> > Cc: net...@vger.kernel.org; intel-wired-...@lists.osuosl.org; linux- > ker...@vger.kernel.org > 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 <gust...@embeddedor.com> > --- > .../net/ethernet/intel/i40e/i40e_virtchnl_pf.c | 15 ++++++--------- > 1 file changed, 6 insertions(+), 9 deletions(-)
Tested-by: Andrew Bowers <andrewx.bow...@intel.com>