Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Joe Perches
On Wed, 2015-09-09 at 11:45 -0700, Joe Perches wrote:
> On Wed, 2015-09-09 at 20:34 +0200, Rasmus Villemoes wrote:
> > 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. 

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(-)

diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
b/drivers/net/ethernet/marvell/mv643xx_eth.c
index d52639b..9230ed5 100644
--- a/drivers/net/ethernet/marvell/mv643xx_eth.c
+++ b/drivers/net/ethernet/marvell/mv643xx_eth.c
@@ -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 = _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)


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Joe Perches
On Wed, 2015-09-09 at 20:34 +0200, Rasmus Villemoes wrote:
> 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. 

Right, misread.


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Rasmus Villemoes
On Wed, Sep 09 2015, Joe Perches  wrote:

> On Wed, 2015-09-09 at 10:38 +0200, Rasmus Villemoes wrote:
>> The double memset is a little ugly; using kzalloc avoids it altogether.
> []
>> diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
>> b/drivers/net/ethernet/marvell/mv643xx_eth.c
> []
>> @@ -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.
>
>> -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. 

>
>   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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Joe Perches
On Wed, 2015-09-09 at 10:38 +0200, Rasmus Villemoes wrote:
> The double memset is a little ugly; using kzalloc avoids it altogether.
[]
> diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
> b/drivers/net/ethernet/marvell/mv643xx_eth.c
[]
> @@ -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.

> - 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]);
}


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Rasmus Villemoes
The double memset is a little ugly; using kzalloc avoids it altogether.

Signed-off-by: Rasmus Villemoes 
---
 drivers/net/ethernet/marvell/mv643xx_eth.c | 5 +
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
b/drivers/net/ethernet/marvell/mv643xx_eth.c
index d52639bc491f..960169efe636 100644
--- a/drivers/net/ethernet/marvell/mv643xx_eth.c
+++ b/drivers/net/ethernet/marvell/mv643xx_eth.c
@@ -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;
-- 
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


[PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Rasmus Villemoes
The double memset is a little ugly; using kzalloc avoids it altogether.

Signed-off-by: Rasmus Villemoes 
---
 drivers/net/ethernet/marvell/mv643xx_eth.c | 5 +
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
b/drivers/net/ethernet/marvell/mv643xx_eth.c
index d52639bc491f..960169efe636 100644
--- a/drivers/net/ethernet/marvell/mv643xx_eth.c
+++ b/drivers/net/ethernet/marvell/mv643xx_eth.c
@@ -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;
-- 
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Joe Perches
On Wed, 2015-09-09 at 10:38 +0200, Rasmus Villemoes wrote:
> The double memset is a little ugly; using kzalloc avoids it altogether.
[]
> diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
> b/drivers/net/ethernet/marvell/mv643xx_eth.c
[]
> @@ -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.

> - 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]);
}


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Joe Perches
On Wed, 2015-09-09 at 11:45 -0700, Joe Perches wrote:
> On Wed, 2015-09-09 at 20:34 +0200, Rasmus Villemoes wrote:
> > 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. 

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(-)

diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
b/drivers/net/ethernet/marvell/mv643xx_eth.c
index d52639b..9230ed5 100644
--- a/drivers/net/ethernet/marvell/mv643xx_eth.c
+++ b/drivers/net/ethernet/marvell/mv643xx_eth.c
@@ -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 = _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)


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Joe Perches
On Wed, 2015-09-09 at 20:34 +0200, Rasmus Villemoes wrote:
> 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. 

Right, misread.


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/


Re: [PATCH 3/4] net: mv643xx_eth: use kzalloc

2015-09-09 Thread Rasmus Villemoes
On Wed, Sep 09 2015, Joe Perches  wrote:

> On Wed, 2015-09-09 at 10:38 +0200, Rasmus Villemoes wrote:
>> The double memset is a little ugly; using kzalloc avoids it altogether.
> []
>> diff --git a/drivers/net/ethernet/marvell/mv643xx_eth.c 
>> b/drivers/net/ethernet/marvell/mv643xx_eth.c
> []
>> @@ -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.
>
>> -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. 

>
>   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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/