* [PATCH] net: unix socket code abuses csum_partial
@ 2014-03-04 10:01 Anton Blanchard
2014-03-04 18:41 ` David Miller
2014-03-05 3:29 ` Anton Blanchard
0 siblings, 2 replies; 8+ messages in thread
From: Anton Blanchard @ 2014-03-04 10:01 UTC (permalink / raw)
To: davem, edumazet, gustavold; +Cc: netdev
The unix socket code is using the result of csum_partial to
hash into a lookup table:
unix_hash_fold(csum_partial(sunaddr, len, 0));
csum_partial is only guaranteed to produce something that can be
folded into a checksum, as its prototype explains:
* returns a 32-bit number suitable for feeding into itself
* or csum_tcpudp_magic
The 32bit value should not be used directly.
Depending on the alignment the ppc64 csum_partial will return
different 32bit values that will fold into the same checksum.
This difference causes the following testcase (courtesy of
Gustavo) to sometimes fail:
#include <sys/socket.h>
#include <stdio.h>
int main()
{
int fd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC, 0);
int i = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, 4);
struct sockaddr addr;
addr.sa_family = AF_LOCAL;
bind(fd, &addr, 2);
listen(fd, 128);
struct sockaddr_storage ss;
socklen_t sslen = (socklen_t)sizeof(ss);
getsockname(fd, (struct sockaddr*)&ss, &sslen);
fd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (connect(fd, (struct sockaddr*)&ss, sslen) == -1){
perror(NULL);
return 1;
}
printf("OK\n");
return 0;
}
Use jhash instead of the current hand crafted csum_partial +
xor based hash.
Signed-off-by: Anton Blanchard <anton@samba.org>
Cc: stable@vger.kernel.org
---
Would appreciate a sanity check here. Are there other users that
expect a stable 32bit value out of csum_partial?
Is this performance critical enough that jhash is going to hurt?
Another option would be to csum_fold the 32bit intermediate value
and just hash based on the result.
Index: b/net/unix/af_unix.c
===================================================================
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -117,6 +117,7 @@
#include <net/checksum.h>
#include <linux/security.h>
#include <linux/freezer.h>
+#include <linux/jhash.h>
struct hlist_head unix_socket_table[2 * UNIX_HASH_SIZE];
EXPORT_SYMBOL_GPL(unix_socket_table);
@@ -161,13 +162,9 @@ static inline void unix_set_secdata(stru
* each socket state is protected by separate spin lock.
*/
-static inline unsigned int unix_hash_fold(__wsum n)
+static inline unsigned int unix_hash(void *data, unsigned int len)
{
- unsigned int hash = (__force unsigned int)n;
-
- hash ^= hash>>16;
- hash ^= hash>>8;
- return hash&(UNIX_HASH_SIZE-1);
+ return jhash(data, len, 0) & (UNIX_HASH_SIZE - 1);
}
#define unix_peer(sk) (unix_sk(sk)->peer)
@@ -232,7 +229,7 @@ static int unix_mkname(struct sockaddr_u
return len;
}
- *hashp = unix_hash_fold(csum_partial(sunaddr, len, 0));
+ *hashp = unix_hash(sunaddr, len);
return len;
}
@@ -738,7 +735,7 @@ static int unix_autobind(struct socket *
retry:
addr->len = sprintf(addr->name->sun_path+1, "%05x", ordernum) + 1 + sizeof(short);
- addr->hash = unix_hash_fold(csum_partial(addr->name, addr->len, 0));
+ addr->hash = unix_hash(addr->name, addr->len);
spin_lock(&unix_table_lock);
ordernum = (ordernum+1)&0xFFFFF;
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] net: unix socket code abuses csum_partial
2014-03-04 10:01 [PATCH] net: unix socket code abuses csum_partial Anton Blanchard
@ 2014-03-04 18:41 ` David Miller
2014-03-04 18:50 ` Eric Dumazet
2014-03-05 3:28 ` Anton Blanchard
2014-03-05 3:29 ` Anton Blanchard
1 sibling, 2 replies; 8+ messages in thread
From: David Miller @ 2014-03-04 18:41 UTC (permalink / raw)
To: anton; +Cc: edumazet, gustavold, netdev
From: Anton Blanchard <anton@samba.org>
Date: Tue, 4 Mar 2014 21:01:04 +1100
> The unix socket code is using the result of csum_partial to
> hash into a lookup table:
>
> unix_hash_fold(csum_partial(sunaddr, len, 0));
>
> csum_partial is only guaranteed to produce something that can be
> folded into a checksum, as its prototype explains:
>
> * returns a 32-bit number suitable for feeding into itself
> * or csum_tcpudp_magic
>
> The 32bit value should not be used directly.
>
> Depending on the alignment the ppc64 csum_partial will return
> different 32bit values that will fold into the same checksum.
>
> This difference causes the following testcase (courtesy of
> Gustavo) to sometimes fail:
Alternatively you could just make unix_hash_fold() use csum_fold(),
right?
static inline unsigned int unix_hash_fold(__wsum n)
{
unsigned int hash = (__force unsigned int) csum_fold(n);
hash ^= hash>>8;
return hash&(UNIX_HASH_SIZE-1);
}
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] net: unix socket code abuses csum_partial
2014-03-04 18:41 ` David Miller
@ 2014-03-04 18:50 ` Eric Dumazet
2014-03-04 21:23 ` David Miller
2014-03-05 3:28 ` Anton Blanchard
1 sibling, 1 reply; 8+ messages in thread
From: Eric Dumazet @ 2014-03-04 18:50 UTC (permalink / raw)
To: David Miller; +Cc: anton, edumazet, gustavold, netdev
On Tue, 2014-03-04 at 13:41 -0500, David Miller wrote:
> Alternatively you could just make unix_hash_fold() use csum_fold(),
> right?
>
> static inline unsigned int unix_hash_fold(__wsum n)
> {
> unsigned int hash = (__force unsigned int) csum_fold(n);
>
> hash ^= hash>>8;
> return hash&(UNIX_HASH_SIZE-1);
> }
Or simply use full_name_hash()
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] net: unix socket code abuses csum_partial
2014-03-04 18:50 ` Eric Dumazet
@ 2014-03-04 21:23 ` David Miller
2014-03-04 21:55 ` Eric Dumazet
0 siblings, 1 reply; 8+ messages in thread
From: David Miller @ 2014-03-04 21:23 UTC (permalink / raw)
To: eric.dumazet; +Cc: anton, edumazet, gustavold, netdev
From: Eric Dumazet <eric.dumazet@gmail.com>
Date: Tue, 04 Mar 2014 10:50:53 -0800
> On Tue, 2014-03-04 at 13:41 -0500, David Miller wrote:
>
>> Alternatively you could just make unix_hash_fold() use csum_fold(),
>> right?
>>
>> static inline unsigned int unix_hash_fold(__wsum n)
>> {
>> unsigned int hash = (__force unsigned int) csum_fold(n);
>>
>> hash ^= hash>>8;
>> return hash&(UNIX_HASH_SIZE-1);
>> }
>
> Or simply use full_name_hash()
I would prefer not, because this would be a performance regression
on platforms other than x86 and ARM, because only they implement
the optimized load_unaligned_zeropad().
Other architectures would end up with a byte-by-byte hash computation.
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] net: unix socket code abuses csum_partial
2014-03-04 21:23 ` David Miller
@ 2014-03-04 21:55 ` Eric Dumazet
0 siblings, 0 replies; 8+ messages in thread
From: Eric Dumazet @ 2014-03-04 21:55 UTC (permalink / raw)
To: David Miller; +Cc: anton, edumazet, gustavold, netdev
On Tue, 2014-03-04 at 16:23 -0500, David Miller wrote:
> From: Eric Dumazet <eric.dumazet@gmail.com>
> > Or simply use full_name_hash()
>
> I would prefer not, because this would be a performance regression
> on platforms other than x86 and ARM, because only they implement
> the optimized load_unaligned_zeropad().
>
> Other architectures would end up with a byte-by-byte hash computation.
Oh right, I missed this was fast only on x86 and ARM.
Sound like a project for arches maintainers then ;)
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH] net: unix socket code abuses csum_partial
2014-03-04 18:41 ` David Miller
2014-03-04 18:50 ` Eric Dumazet
@ 2014-03-05 3:28 ` Anton Blanchard
1 sibling, 0 replies; 8+ messages in thread
From: Anton Blanchard @ 2014-03-05 3:28 UTC (permalink / raw)
To: David Miller; +Cc: edumazet, gustavold, netdev
Hi Dave,
> Alternatively you could just make unix_hash_fold() use csum_fold(),
> right?
>
> static inline unsigned int unix_hash_fold(__wsum n)
> {
> unsigned int hash = (__force unsigned int) csum_fold(n);
>
> hash ^= hash>>8;
> return hash&(UNIX_HASH_SIZE-1);
> }
Thanks, sounds good. Updated patch on the way.
Anton
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] net: unix socket code abuses csum_partial
2014-03-04 10:01 [PATCH] net: unix socket code abuses csum_partial Anton Blanchard
2014-03-04 18:41 ` David Miller
@ 2014-03-05 3:29 ` Anton Blanchard
2014-03-06 21:19 ` David Miller
1 sibling, 1 reply; 8+ messages in thread
From: Anton Blanchard @ 2014-03-05 3:29 UTC (permalink / raw)
To: davem, edumazet, gustavold; +Cc: netdev
The unix socket code is using the result of csum_partial to
hash into a lookup table:
unix_hash_fold(csum_partial(sunaddr, len, 0));
csum_partial is only guaranteed to produce something that can be
folded into a checksum, as its prototype explains:
* returns a 32-bit number suitable for feeding into itself
* or csum_tcpudp_magic
The 32bit value should not be used directly.
Depending on the alignment, the ppc64 csum_partial will return
different 32bit partial checksums that will fold into the same
16bit checksum.
This difference causes the following testcase (courtesy of
Gustavo) to sometimes fail:
#include <sys/socket.h>
#include <stdio.h>
int main()
{
int fd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC, 0);
int i = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &i, 4);
struct sockaddr addr;
addr.sa_family = AF_LOCAL;
bind(fd, &addr, 2);
listen(fd, 128);
struct sockaddr_storage ss;
socklen_t sslen = (socklen_t)sizeof(ss);
getsockname(fd, (struct sockaddr*)&ss, &sslen);
fd = socket(PF_LOCAL, SOCK_STREAM|SOCK_CLOEXEC, 0);
if (connect(fd, (struct sockaddr*)&ss, sslen) == -1){
perror(NULL);
return 1;
}
printf("OK\n");
return 0;
}
As suggested by davem, fix this by using csum_fold to fold the
partial 32bit checksum into a 16bit checksum before using it.
Signed-off-by: Anton Blanchard <anton@samba.org>
Cc: stable@vger.kernel.org
---
Index: b/net/unix/af_unix.c
===================================================================
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -163,9 +163,8 @@ static inline void unix_set_secdata(stru
static inline unsigned int unix_hash_fold(__wsum n)
{
- unsigned int hash = (__force unsigned int)n;
+ unsigned int hash = (__force unsigned int)csum_fold(n);
- hash ^= hash>>16;
hash ^= hash>>8;
return hash&(UNIX_HASH_SIZE-1);
}
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] net: unix socket code abuses csum_partial
2014-03-05 3:29 ` Anton Blanchard
@ 2014-03-06 21:19 ` David Miller
0 siblings, 0 replies; 8+ messages in thread
From: David Miller @ 2014-03-06 21:19 UTC (permalink / raw)
To: anton; +Cc: edumazet, gustavold, netdev
From: Anton Blanchard <anton@samba.org>
Date: Wed, 5 Mar 2014 14:29:58 +1100
> Index: b/net/unix/af_unix.c
> ===================================================================
> --- a/net/unix/af_unix.c
> +++ b/net/unix/af_unix.c
> @@ -163,9 +163,8 @@ static inline void unix_set_secdata(stru
>
> static inline unsigned int unix_hash_fold(__wsum n)
> {
> - unsigned int hash = (__force unsigned int)n;
> + unsigned int hash = (__force unsigned int)csum_fold(n);
>
> - hash ^= hash>>16;
> hash ^= hash>>8;
> return hash&(UNIX_HASH_SIZE-1);
Looks great, I'll apply this, thanks Anton!
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2014-03-06 21:19 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-04 10:01 [PATCH] net: unix socket code abuses csum_partial Anton Blanchard
2014-03-04 18:41 ` David Miller
2014-03-04 18:50 ` Eric Dumazet
2014-03-04 21:23 ` David Miller
2014-03-04 21:55 ` Eric Dumazet
2014-03-05 3:28 ` Anton Blanchard
2014-03-05 3:29 ` Anton Blanchard
2014-03-06 21:19 ` David Miller
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).