Skip to content

Commit 9b5c4df

Browse files
Vadim Lomovtsevdavem330
authored andcommitted
net: thunderx: rework mac addresses list to u64 array
It is too expensive to pass u64 values via linked list, instead allocate array for them by overall number of mac addresses from netdev. This eventually removes multiple kmalloc() calls, aviod memory fragmentation and allow to put single null check on kmalloc return value in order to prevent a potential null pointer dereference. Addresses-Coverity-ID: 1467429 ("Dereference null return value") Fixes: 37c3347 ("net: thunderx: add ndo_set_rx_mode callback implementation for VF") Reported-by: Dan Carpenter <[email protected]> Signed-off-by: Vadim Lomovtsev <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b6a37e5 commit 9b5c4df

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

drivers/net/ethernet/cavium/thunder/nic.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,9 @@ struct nicvf_drv_stats {
265265

266266
struct cavium_ptp;
267267

268-
struct xcast_addr {
269-
struct list_head list;
270-
u64 addr;
271-
};
272-
273268
struct xcast_addr_list {
274-
struct list_head list;
275269
int count;
270+
u64 mc[];
276271
};
277272

278273
struct nicvf_work {

drivers/net/ethernet/cavium/thunder/nicvf_main.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,7 @@ static void nicvf_set_rx_mode_task(struct work_struct *work_arg)
19291929
work.work);
19301930
struct nicvf *nic = container_of(vf_work, struct nicvf, rx_mode_work);
19311931
union nic_mbx mbx = {};
1932-
struct xcast_addr *xaddr, *next;
1932+
int idx;
19331933

19341934
if (!vf_work)
19351935
return;
@@ -1956,16 +1956,10 @@ static void nicvf_set_rx_mode_task(struct work_struct *work_arg)
19561956
/* check if we have any specific MACs to be added to PF DMAC filter */
19571957
if (vf_work->mc) {
19581958
/* now go through kernel list of MACs and add them one by one */
1959-
list_for_each_entry_safe(xaddr, next,
1960-
&vf_work->mc->list, list) {
1959+
for (idx = 0; idx < vf_work->mc->count; idx++) {
19611960
mbx.xcast.msg = NIC_MBOX_MSG_ADD_MCAST;
1962-
mbx.xcast.data.mac = xaddr->addr;
1961+
mbx.xcast.data.mac = vf_work->mc->mc[idx];
19631962
nicvf_send_msg_to_pf(nic, &mbx);
1964-
1965-
/* after receiving ACK from PF release memory */
1966-
list_del(&xaddr->list);
1967-
kfree(xaddr);
1968-
vf_work->mc->count--;
19691963
}
19701964
kfree(vf_work->mc);
19711965
}
@@ -1996,17 +1990,15 @@ static void nicvf_set_rx_mode(struct net_device *netdev)
19961990
mode |= BGX_XCAST_MCAST_FILTER;
19971991
/* here we need to copy mc addrs */
19981992
if (netdev_mc_count(netdev)) {
1999-
struct xcast_addr *xaddr;
2000-
2001-
mc_list = kmalloc(sizeof(*mc_list), GFP_ATOMIC);
2002-
INIT_LIST_HEAD(&mc_list->list);
1993+
mc_list = kmalloc(offsetof(typeof(*mc_list),
1994+
mc[netdev_mc_count(netdev)]),
1995+
GFP_ATOMIC);
1996+
if (unlikely(!mc_list))
1997+
return;
1998+
mc_list->count = 0;
20031999
netdev_hw_addr_list_for_each(ha, &netdev->mc) {
2004-
xaddr = kmalloc(sizeof(*xaddr),
2005-
GFP_ATOMIC);
2006-
xaddr->addr =
2000+
mc_list->mc[mc_list->count] =
20072001
ether_addr_to_u64(ha->addr);
2008-
list_add_tail(&xaddr->list,
2009-
&mc_list->list);
20102002
mc_list->count++;
20112003
}
20122004
}

0 commit comments

Comments
 (0)