Commit af07adbb authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by Jeff Kirsher

iavf: use struct_size() helper

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes, in particular in the
context in which this code is being used.

So, replace code of the following form:

sizeof(struct virtchnl_ether_addr_list) + (count * sizeof(struct virtchnl_ether_addr))

with:

struct_size(veal, list, count)

and so on...

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>
parent 583cf7be
...@@ -242,7 +242,8 @@ void iavf_configure_queues(struct iavf_adapter *adapter) ...@@ -242,7 +242,8 @@ void iavf_configure_queues(struct iavf_adapter *adapter)
struct virtchnl_vsi_queue_config_info *vqci; struct virtchnl_vsi_queue_config_info *vqci;
struct virtchnl_queue_pair_info *vqpi; struct virtchnl_queue_pair_info *vqpi;
int pairs = adapter->num_active_queues; int pairs = adapter->num_active_queues;
int i, len, max_frame = IAVF_MAX_RXBUFFER; int i, max_frame = IAVF_MAX_RXBUFFER;
size_t len;
if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) { if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
/* bail because we already have a command pending */ /* bail because we already have a command pending */
...@@ -251,8 +252,7 @@ void iavf_configure_queues(struct iavf_adapter *adapter) ...@@ -251,8 +252,7 @@ void iavf_configure_queues(struct iavf_adapter *adapter)
return; return;
} }
adapter->current_op = VIRTCHNL_OP_CONFIG_VSI_QUEUES; adapter->current_op = VIRTCHNL_OP_CONFIG_VSI_QUEUES;
len = sizeof(struct virtchnl_vsi_queue_config_info) + len = struct_size(vqci, qpair, pairs);
(sizeof(struct virtchnl_queue_pair_info) * pairs);
vqci = kzalloc(len, GFP_KERNEL); vqci = kzalloc(len, GFP_KERNEL);
if (!vqci) if (!vqci)
return; return;
...@@ -351,8 +351,9 @@ void iavf_map_queues(struct iavf_adapter *adapter) ...@@ -351,8 +351,9 @@ void iavf_map_queues(struct iavf_adapter *adapter)
{ {
struct virtchnl_irq_map_info *vimi; struct virtchnl_irq_map_info *vimi;
struct virtchnl_vector_map *vecmap; struct virtchnl_vector_map *vecmap;
int v_idx, q_vectors, len;
struct iavf_q_vector *q_vector; struct iavf_q_vector *q_vector;
int v_idx, q_vectors;
size_t len;
if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) { if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
/* bail because we already have a command pending */ /* bail because we already have a command pending */
...@@ -364,9 +365,7 @@ void iavf_map_queues(struct iavf_adapter *adapter) ...@@ -364,9 +365,7 @@ void iavf_map_queues(struct iavf_adapter *adapter)
q_vectors = adapter->num_msix_vectors - NONQ_VECS; q_vectors = adapter->num_msix_vectors - NONQ_VECS;
len = sizeof(struct virtchnl_irq_map_info) + len = struct_size(vimi, vecmap, adapter->num_msix_vectors);
(adapter->num_msix_vectors *
sizeof(struct virtchnl_vector_map));
vimi = kzalloc(len, GFP_KERNEL); vimi = kzalloc(len, GFP_KERNEL);
if (!vimi) if (!vimi)
return; return;
...@@ -433,9 +432,10 @@ int iavf_request_queues(struct iavf_adapter *adapter, int num) ...@@ -433,9 +432,10 @@ int iavf_request_queues(struct iavf_adapter *adapter, int num)
void iavf_add_ether_addrs(struct iavf_adapter *adapter) void iavf_add_ether_addrs(struct iavf_adapter *adapter)
{ {
struct virtchnl_ether_addr_list *veal; struct virtchnl_ether_addr_list *veal;
int len, i = 0, count = 0;
struct iavf_mac_filter *f; struct iavf_mac_filter *f;
int i = 0, count = 0;
bool more = false; bool more = false;
size_t len;
if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) { if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
/* bail because we already have a command pending */ /* bail because we already have a command pending */
...@@ -457,15 +457,13 @@ void iavf_add_ether_addrs(struct iavf_adapter *adapter) ...@@ -457,15 +457,13 @@ void iavf_add_ether_addrs(struct iavf_adapter *adapter)
} }
adapter->current_op = VIRTCHNL_OP_ADD_ETH_ADDR; adapter->current_op = VIRTCHNL_OP_ADD_ETH_ADDR;
len = sizeof(struct virtchnl_ether_addr_list) + len = struct_size(veal, list, count);
(count * sizeof(struct virtchnl_ether_addr));
if (len > IAVF_MAX_AQ_BUF_SIZE) { if (len > IAVF_MAX_AQ_BUF_SIZE) {
dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n"); dev_warn(&adapter->pdev->dev, "Too many add MAC changes in one request\n");
count = (IAVF_MAX_AQ_BUF_SIZE - count = (IAVF_MAX_AQ_BUF_SIZE -
sizeof(struct virtchnl_ether_addr_list)) / sizeof(struct virtchnl_ether_addr_list)) /
sizeof(struct virtchnl_ether_addr); sizeof(struct virtchnl_ether_addr);
len = sizeof(struct virtchnl_ether_addr_list) + len = struct_size(veal, list, count);
(count * sizeof(struct virtchnl_ether_addr));
more = true; more = true;
} }
...@@ -505,8 +503,9 @@ void iavf_del_ether_addrs(struct iavf_adapter *adapter) ...@@ -505,8 +503,9 @@ void iavf_del_ether_addrs(struct iavf_adapter *adapter)
{ {
struct virtchnl_ether_addr_list *veal; struct virtchnl_ether_addr_list *veal;
struct iavf_mac_filter *f, *ftmp; struct iavf_mac_filter *f, *ftmp;
int len, i = 0, count = 0; int i = 0, count = 0;
bool more = false; bool more = false;
size_t len;
if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) { if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
/* bail because we already have a command pending */ /* bail because we already have a command pending */
...@@ -528,15 +527,13 @@ void iavf_del_ether_addrs(struct iavf_adapter *adapter) ...@@ -528,15 +527,13 @@ void iavf_del_ether_addrs(struct iavf_adapter *adapter)
} }
adapter->current_op = VIRTCHNL_OP_DEL_ETH_ADDR; adapter->current_op = VIRTCHNL_OP_DEL_ETH_ADDR;
len = sizeof(struct virtchnl_ether_addr_list) + len = struct_size(veal, list, count);
(count * sizeof(struct virtchnl_ether_addr));
if (len > IAVF_MAX_AQ_BUF_SIZE) { if (len > IAVF_MAX_AQ_BUF_SIZE) {
dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n"); dev_warn(&adapter->pdev->dev, "Too many delete MAC changes in one request\n");
count = (IAVF_MAX_AQ_BUF_SIZE - count = (IAVF_MAX_AQ_BUF_SIZE -
sizeof(struct virtchnl_ether_addr_list)) / sizeof(struct virtchnl_ether_addr_list)) /
sizeof(struct virtchnl_ether_addr); sizeof(struct virtchnl_ether_addr);
len = sizeof(struct virtchnl_ether_addr_list) + len = struct_size(veal, list, count);
(count * sizeof(struct virtchnl_ether_addr));
more = true; more = true;
} }
veal = kzalloc(len, GFP_ATOMIC); veal = kzalloc(len, GFP_ATOMIC);
...@@ -973,7 +970,7 @@ static void iavf_print_link_message(struct iavf_adapter *adapter) ...@@ -973,7 +970,7 @@ static void iavf_print_link_message(struct iavf_adapter *adapter)
void iavf_enable_channels(struct iavf_adapter *adapter) void iavf_enable_channels(struct iavf_adapter *adapter)
{ {
struct virtchnl_tc_info *vti = NULL; struct virtchnl_tc_info *vti = NULL;
u16 len; size_t len;
int i; int i;
if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) { if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
...@@ -983,9 +980,7 @@ void iavf_enable_channels(struct iavf_adapter *adapter) ...@@ -983,9 +980,7 @@ void iavf_enable_channels(struct iavf_adapter *adapter)
return; return;
} }
len = ((adapter->num_tc - 1) * sizeof(struct virtchnl_channel_info)) + len = struct_size(vti, list, adapter->num_tc - 1);
sizeof(struct virtchnl_tc_info);
vti = kzalloc(len, GFP_KERNEL); vti = kzalloc(len, GFP_KERNEL);
if (!vti) if (!vti)
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment