net: a few kzalloc/memset cleanups
Subsystems (6)
cover net: a few kzalloc/memset cleanups
These are just a few k[czm]alloc/memset related cleanups.
Rasmus Villemoes (4):
net: cavium: liquidio: use kzalloc in setup_glist()
net: jme: use kzalloc() instead of kmalloc+memset
net: mv643xx_eth: use kzalloc
net: qlcnic: delete redundant memsets
drivers/net/ethernet/cavium/liquidio/lio_main.c | 3 +-- drivers/net/ethernet/jme.c | 8 ++------ drivers/net/ethernet/marvell/mv643xx_eth.c | 5 +---- drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_hw.c | 2 -- drivers/net/ethernet/qlogic/qlcnic/qlcnic_ctx.c | 2 -- drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c | 2 -- 6 files changed, 4 insertions(+), 18 deletions(-)
Signature · 1 line
2.1.3
PATCH 1/4 net: cavium: liquidio: use kzalloc in setup_glist()
We save a little .text and get rid of the sizeof(...) style inconsistency.
--- drivers/net/ethernet/cavium/liquidio/lio_main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/cavium/liquidio/lio_main.c +1 -2
@@ -818,10 +818,9 @@ static int setup_glist(struct lio *lio)INIT_LIST_HEAD (&lio->glist);for (i =0 ; i < lio->tx_qsize; i++) { - g =kmalloc (sizeof (*g),GFP_KERNEL ); + g =kzalloc (sizeof (*g),GFP_KERNEL );if (!g)break ; -memset (g,0 ,sizeof (struct octnic_gather )); g->sg_size = ((ROUNDUP4 (OCTNIC_MAX_SG ) >>2 ) *OCT_SG_ENTRY_SIZE );
Signature · 1 line
2.1.3
PATCH 2/4 net: jme: use kzalloc() instead of kmalloc+memset
In reply to Rasmus VillemoesUsing kzalloc saves a tiny bit on .text.
--- drivers/net/ethernet/jme.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
drivers/net/ethernet/jme.c +2 -6
@@ -583,7 +583,7 @@ jme_setup_tx_resources(struct jme_adapter *jme)atomic_set (&txring->next_to_clean,0 );atomic_set (&txring->nr_free, jme->tx_ring_size); - txring->bufinf =kmalloc (sizeof (struct jme_buffer_info ) * + txring->bufinf =kzalloc (sizeof (struct jme_buffer_info ) * jme->tx_ring_size,GFP_ATOMIC );if (unlikely (!(txring->bufinf))) goto err_free_txring;
@@ -592,8 +592,6 @@ jme_setup_tx_resources(struct jme_adapter *jme)* Initialize Transmit Descriptors */ memset (txring->alloc,0 ,TX_RING_ALLOC_SIZE (jme->tx_ring_size)); -memset (txring->bufinf,0 , -sizeof (struct jme_buffer_info ) * jme->tx_ring_size);return 0 ;
@@ -845,7 +843,7 @@ jme_setup_rx_resources(struct jme_adapter *jme) rxring->next_to_use =0 ;atomic_set (&rxring->next_to_clean,0 ); - rxring->bufinf =kmalloc (sizeof (struct jme_buffer_info ) * + rxring->bufinf =kzalloc (sizeof (struct jme_buffer_info ) * jme->rx_ring_size,GFP_ATOMIC );if (unlikely (!(rxring->bufinf))) goto err_free_rxring;
@@ -853,8 +851,6 @@ jme_setup_rx_resources(struct jme_adapter *jme)/* * Initiallize Receive Descriptors */ -memset (rxring->bufinf,0 , -sizeof (struct jme_buffer_info ) * jme->rx_ring_size);for (i =0 ; i < jme->rx_ring_size ; ++i) {if (unlikely (jme_make_new_rx_buf (jme, i))) {jme_free_rx_resources (jme);
Signature · 1 line
2.1.3
PATCH 3/4 net: mv643xx_eth: use kzalloc
In reply to Rasmus VillemoesThe double memset is a little ugly; using kzalloc avoids it altogether.
--- drivers/net/ethernet/marvell/mv643xx_eth.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/net/ethernet/marvell/mv643xx_eth.c +1 -4
@@ -1859,14 +1859,11 @@ oom:return ; } - mc_spec =kmalloc (0x200 ,GFP_ATOMIC ); + mc_spec =kzalloc (0x200 ,GFP_ATOMIC );if (mc_spec ==NULL ) goto oom; mc_other = mc_spec + (0x100 >>2 ); -memset (mc_spec,0 ,0x100 ); -memset (mc_other,0 ,0x100 ); -netdev_for_each_mc_addr (ha, dev) {u8 *a = ha->addr;u32 *table;
Signature · 1 line
2.1.3
Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc
Quoted 1 line · Rasmus Villemoes
The double memset is a little ugly; using kzalloc avoids it altogether.
[]
drivers/net/ethernet/marvell/mv643xx_eth.c
Quoted 1 line
drivers/net/ethernet/marvell/mv643xx_eth.c +0
-0
[]
drivers/net/ethernet/marvell/mv643xx_eth.c oom: · line 1865
Quoted 9 lines
drivers/net/ethernet/marvell/mv643xx_eth.c +1
-1@@ -1859,14 +1859,11 @@ oom:return ; } - mc_spec =kmalloc (0x200 ,GFP_ATOMIC ); + mc_spec =kzalloc (0x200 ,GFP_ATOMIC );if (mc_spec ==NULL ) goto oom; mc_other = mc_spec + (0x100 >>2 );
This sure looks wrong as it sets a pointer
to unallocated memory.
drivers/net/ethernet/marvell/mv643xx_eth.c oom: · line 1868
Quoted 2 lines
drivers/net/ethernet/marvell/mv643xx_eth.c +0
-2-memset (mc_spec,0 ,0x100 ); -memset (mc_other,0 ,0x100 );
So this does a memset of random memory.
for (i = 0; i < 0x100; i += 4) {
wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
}Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc
Quoted 18 lines · Joe Perches
Quoted 1 line · Rasmus Villemoes
The double memset is a little ugly; using kzalloc avoids it altogether.
[]
drivers/net/ethernet/marvell/mv643xx_eth.c
Quoted 1 line
drivers/net/ethernet/marvell/mv643xx_eth.c +0
-0[]
drivers/net/ethernet/marvell/mv643xx_eth.c oom: · line 1865
Quoted 9 lines
drivers/net/ethernet/marvell/mv643xx_eth.c +1
-1@@ -1859,14 +1859,11 @@ oom:return ; } - mc_spec =kmalloc (0x200 ,GFP_ATOMIC ); + mc_spec =kzalloc (0x200 ,GFP_ATOMIC );if (mc_spec ==NULL ) goto oom; mc_other = mc_spec + (0x100 >>2 );This sure looks wrong as it sets a pointer
to unallocated memory.
Quoted 5 lines
drivers/net/ethernet/marvell/mv643xx_eth.c oom: · line 1868
Quoted 2 lines
drivers/net/ethernet/marvell/mv643xx_eth.c +0
-2-memset (mc_spec,0 ,0x100 ); -memset (mc_other,0 ,0x100 );So this does a memset of random memory.
Huh? mc_spec and mc_other are u32*, we allocate 0x200 = 512 bytes = 128 u32s, and pointer arithmetic makes mc_other point to the latter 64. Then the memory is cleared 256 bytes at a time.
It's unusual and slightly obfuscated code, but I don't think it's wrong.
Quoted 5 lines
for (i = 0; i < 0x100; i += 4) {
wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
}I'd probably have written that as
for (i = 0; i < 64; ++i) {
wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + 4*i, mc_spec[i]);
wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + 4*i, mc_other[i]);
}but again, I don't think it's wrong [haven't checked what SPECIAL_MCAST_TABLE/OTHER_MCAST_TABLE do, though].
Rasmus
Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc
Quoted 6 lines · Rasmus Villemoes
mc_specandmc_otherare u32*, we allocate0x200= 512 bytes = 128 u32s, and pointer arithmetic makesmc_otherpoint to the latter 64. Then the memory is cleared 256 bytes at a time.It's unusual and slightly obfuscated code, but I don't think it's wrong.
Right, misread.
Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc
Quoted 7 lines · Joe Perches
Quoted 6 lines · Rasmus Villemoes
mc_specandmc_otherare u32*, we allocate0x200= 512 bytes = 128 u32s, and pointer arithmetic makesmc_otherpoint to the latter 64. Then the memory is cleared 256 bytes at a time.It's unusual and slightly obfuscated code, but I don't think it's wrong.
Perhaps this would make the code a bit clearer:
Use kcalloc, decimal sizes, decimal indexing,
and a promiscuous exit block using the same
style as the non-promiscuous multicast block.
--- drivers/net/ethernet/marvell/mv643xx_eth.c | 46 ++++++++++++++---------------- 1 file changed, 22 insertions(+), 24 deletions(-)
drivers/net/ethernet/marvell/mv643xx_eth.c +22 -24
@@ -1845,32 +1845,19 @@ static void mv643xx_eth_program_multicast_filter(struct net_device *dev)struct netdev_hw_addr *ha;int i; -if (dev->flags & (IFF_PROMISC |IFF_ALLMULTI )) { -int port_num; -u32 accept; +if (dev->flags & (IFF_PROMISC |IFF_ALLMULTI )) + goto promiscuous; -oom: - port_num = mp->port_num; - accept =0x01010101 ; -for (i =0 ; i <0x100 ; i +=4 ) { -wrl (mp,SPECIAL_MCAST_TABLE (port_num) + i, accept); -wrl (mp,OTHER_MCAST_TABLE (port_num) + i, accept); - } -return ; - } - - mc_spec =kmalloc (0x200 ,GFP_ATOMIC ); -if (mc_spec ==NULL ) - goto oom; - mc_other = mc_spec + (0x100 >>2 ); - -memset (mc_spec,0 ,0x100 ); -memset (mc_other,0 ,0x100 ); +/* Allocate both mc_spec and mc_other tables */ + mc_spec =kcalloc (128 ,sizeof (u32),GFP_ATOMIC ); +if (!mc_spec) + goto promiscuous; + mc_other = &mc_spec[64 ];netdev_for_each_mc_addr (ha, dev) {u8 *a = ha->addr;u32 *table; -int entry; +u8 entry;if (memcmp (a,"\x01\x00\x5e\x00\x00" ,5 ) ==0 ) { table = mc_spec;
@@ -1883,12 +1870,23 @@ oom: table[entry >>2 ] |=1 << (8 * (entry &3 )); } -for (i =0 ; i <0x100 ; i +=4 ) { -wrl (mp,SPECIAL_MCAST_TABLE (mp->port_num) + i, mc_spec[i >>2 ]); -wrl (mp,OTHER_MCAST_TABLE (mp->port_num) + i, mc_other[i >>2 ]); +for (i =0 ; i <64 ; i++) { +wrl (mp,SPECIAL_MCAST_TABLE (mp->port_num) + i *sizeof (u32), + mc_spec[i]); +wrl (mp,OTHER_MCAST_TABLE (mp->port_num) + i *sizeof (u32), + mc_other[i]); }kfree (mc_spec); +return ; + +promiscuous: +for (i =0 ; i <64 ; i++) { +wrl (mp,SPECIAL_MCAST_TABLE (mp->port_num) + i *sizeof (u32), +0x01010101u ); +wrl (mp,OTHER_MCAST_TABLE (mp->port_num) + i *sizeof (u32), +0x01010101u ); + } }static void mv643xx_eth_set_rx_mode (struct net_device *dev)
PATCH 4/4 net: qlcnic: delete redundant memsets
In reply to Rasmus VillemoesIn all cases, mbx->req.arg and mbx->rsp.arg have just been allocated using kcalloc(), so these six memsets are redundant.
---
I cranked $context_lines to 11 to make the kcallocs visible.
drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_hw.c | 2 -- drivers/net/ethernet/qlogic/qlcnic/qlcnic_ctx.c | 2 -- drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c | 2 -- 3 files changed, 6 deletions(-)
drivers/net/ethernet/qlogic/qlcnic/qlcnic_83xx_hw.c +0 -2
@@ -910,24 +910,22 @@ int qlcnic_83xx_alloc_mbx_args(struct qlcnic_cmd_args *mbx, mbx->req.arg =kcalloc (mbx->req.num,sizeof (u32),GFP_ATOMIC );if (!mbx->req.arg)return -ENOMEM ; mbx->rsp.arg =kcalloc (mbx->rsp.num,sizeof (u32),GFP_ATOMIC );if (!mbx->rsp.arg) {kfree (mbx->req.arg); mbx->req.arg =NULL ;return -ENOMEM ; } -memset (mbx->req.arg,0 ,sizeof (u32) * mbx->req.num); -memset (mbx->rsp.arg,0 ,sizeof (u32) * mbx->rsp.num); temp = adapter->ahw->fw_hal_version <<29 ; mbx->req.arg[0 ] = (type | (mbx->req.num <<16 ) | temp); mbx->cmd_op = type;return 0 ; } }dev_err (&adapter->pdev->dev,"%s: Invalid mailbox command opcode 0x%x\n" , __func__, type);return -EINVAL ; }
drivers/net/ethernet/qlogic/qlcnic/qlcnic_ctx.c +0 -2
@@ -65,24 +65,22 @@ int qlcnic_82xx_alloc_mbx_args(struct qlcnic_cmd_args *mbx, mbx->req.arg =kcalloc (mbx->req.num,sizeof (u32),GFP_ATOMIC );if (!mbx->req.arg)return -ENOMEM ; mbx->rsp.arg =kcalloc (mbx->rsp.num,sizeof (u32),GFP_ATOMIC );if (!mbx->rsp.arg) {kfree (mbx->req.arg); mbx->req.arg =NULL ;return -ENOMEM ; } -memset (mbx->req.arg,0 ,sizeof (u32) * mbx->req.num); -memset (mbx->rsp.arg,0 ,sizeof (u32) * mbx->rsp.num); mbx->req.arg[0 ] = type;break ; } }return 0 ; }/* Free up mailbox registers */ void qlcnic_free_mbx_args (struct qlcnic_cmd_args *cmd) {kfree (cmd->req.arg);
drivers/net/ethernet/qlogic/qlcnic/qlcnic_sriov_common.c +0 -2
@@ -721,24 +721,22 @@ static int qlcnic_sriov_alloc_bc_mbx_args(struct qlcnic_cmd_args *mbx, u32 type) mbx->req.arg =kcalloc (mbx->req.num,sizeof (u32),GFP_ATOMIC );if (!mbx->req.arg)return -ENOMEM ; mbx->rsp.arg =kcalloc (mbx->rsp.num,sizeof (u32),GFP_ATOMIC );if (!mbx->rsp.arg) {kfree (mbx->req.arg); mbx->req.arg =NULL ;return -ENOMEM ; } -memset (mbx->req.arg,0 ,sizeof (u32) * mbx->req.num); -memset (mbx->rsp.arg,0 ,sizeof (u32) * mbx->rsp.num); mbx->req.arg[0 ] = (type | (mbx->req.num <<16 ) | (3 <<29 )); mbx->rsp.arg[0 ] = (type &0xffff ) | mbx->rsp.num <<16 ;return 0 ; } }return -EINVAL ; } static int qlcnic_sriov_prepare_bc_hdr(struct qlcnic_bc_trans *trans, struct qlcnic_cmd_args *cmd,
Signature · 1 line
2.1.3
David Miller
maintainerFrom: Rasmus Villemoes <[email protected]>
Date: Wed, 9 Sep 2015 10:38:01 +0200
Quoted 1 line
These are just a few k[czm]alloc/memset related cleanups.
Series applied, thanks.
PATCH 1/1 mv643xx_eth: Neaten mv643xx_eth_program_multicast_filter
The code around the allocation and loops are a bit obfuscated.
Neaten it by using:
o kcalloc with decimal count and sizeof(u32)
o Decimal loop indexing and i++ not i += 4
o A promiscuous block using a similar style
to the multicast block
o Remove unnecessary variables
---
Here's a neatening patch respun against Rasmus' patch Might be useful.
drivers/net/ethernet/marvell/mv643xx_eth.c | 43 +++++++++++++++--------------- 1 file changed, 22 insertions(+), 21 deletions(-)
drivers/net/ethernet/marvell/mv643xx_eth.c +22 -21
@@ -1845,29 +1845,19 @@ static void mv643xx_eth_program_multicast_filter(struct net_device *dev)struct netdev_hw_addr *ha;int i; -if (dev->flags & (IFF_PROMISC |IFF_ALLMULTI )) { -int port_num; -u32 accept; +if (dev->flags & (IFF_PROMISC |IFF_ALLMULTI )) + goto promiscuous; -oom: - port_num = mp->port_num; - accept =0x01010101 ; -for (i =0 ; i <0x100 ; i +=4 ) { -wrl (mp,SPECIAL_MCAST_TABLE (port_num) + i, accept); -wrl (mp,OTHER_MCAST_TABLE (port_num) + i, accept); - } -return ; - } - - mc_spec =kzalloc (0x200 ,GFP_ATOMIC ); -if (mc_spec ==NULL ) - goto oom; - mc_other = mc_spec + (0x100 >>2 ); +/* Allocate both mc_spec and mc_other tables */ + mc_spec =kcalloc (128 ,sizeof (u32),GFP_ATOMIC ); +if (!mc_spec) + goto promiscuous; + mc_other = &mc_spec[64 ];netdev_for_each_mc_addr (ha, dev) {u8 *a = ha->addr;u32 *table; -int entry; +u8 entry;if (memcmp (a,"\x01\x00\x5e\x00\x00" ,5 ) ==0 ) { table = mc_spec;
@@ -1880,12 +1870,23 @@ oom: table[entry >>2 ] |=1 << (8 * (entry &3 )); } -for (i =0 ; i <0x100 ; i +=4 ) { -wrl (mp,SPECIAL_MCAST_TABLE (mp->port_num) + i, mc_spec[i >>2 ]); -wrl (mp,OTHER_MCAST_TABLE (mp->port_num) + i, mc_other[i >>2 ]); +for (i =0 ; i <64 ; i++) { +wrl (mp,SPECIAL_MCAST_TABLE (mp->port_num) + i *sizeof (u32), + mc_spec[i]); +wrl (mp,OTHER_MCAST_TABLE (mp->port_num) + i *sizeof (u32), + mc_other[i]); }kfree (mc_spec); +return ; + +promiscuous: +for (i =0 ; i <64 ; i++) { +wrl (mp,SPECIAL_MCAST_TABLE (mp->port_num) + i *sizeof (u32), +0x01010101u ); +wrl (mp,OTHER_MCAST_TABLE (mp->port_num) + i *sizeof (u32), +0x01010101u ); + } }static void mv643xx_eth_set_rx_mode (struct net_device *dev)
Re: [PATCH] mv643xx_eth: Neaten mv643xx_eth_program_multicast_filter
From: Joe Perches <[email protected]>
Date: Wed, 09 Sep 2015 17:40:56 -0700
Quoted 11 lines
The code around the allocation and loops are a bit obfuscated.
Neaten it by using:
o kcalloc with decimal count and sizeof(u32)
o Decimal loop indexing and i++ not i += 4
o A promiscuous block using a similar style
to the multicast block
o Remove unnecessary variablesSigned-off-by Joe Perches <[email protected]>
Applied.