af_unix: fix a fatal race with bit fields
authorEric Dumazet <eric.dumazet@gmail.com>
Wed, 1 May 2013 05:24:03 +0000 (05:24 +0000)
committerZefan Li <lizefan@huawei.com>
Wed, 26 Oct 2016 15:15:42 +0000 (23:15 +0800)
commit5781d89c5468dd7a9a17df7995541b284599e00a
tree139ed197f24150f383414c9bf37be1fa9cf725ce
parentedd32246ed08de91f2270e7c4b0b65bbec6aba09
af_unix: fix a fatal race with bit fields

commit 60bc851ae59bfe99be6ee89d6bc50008c85ec75d upstream.

Using bit fields is dangerous on ppc64/sparc64, as the compiler [1]
uses 64bit instructions to manipulate them.
If the 64bit word includes any atomic_t or spinlock_t, we can lose
critical concurrent changes.

This is happening in af_unix, where unix_sk(sk)->gc_candidate/
gc_maybe_cycle/lock share the same 64bit word.

This leads to fatal deadlock, as one/several cpus spin forever
on a spinlock that will never be available again.

A safer way would be to use a long to store flags.
This way we are sure compiler/arch wont do bad things.

As we own unix_gc_lock spinlock when clearing or setting bits,
we can use the non atomic __set_bit()/__clear_bit().

recursion_level can share the same 64bit location with the spinlock,
as it is set only with this spinlock held.

[1] bug fixed in gcc-4.8.0 :
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52080

Reported-by: Ambrose Feinstein <ambrose@google.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: Paul Mackerras <paulus@samba.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
Cc: hejianet <hejianet@gmail.com>
Signed-off-by: Zefan Li <lizefan@huawei.com>
include/net/af_unix.h
net/unix/garbage.c