Skip to content

Commit 60bc851

Browse files
Eric Dumazetdavem330
authored andcommitted
af_unix: fix a fatal race with bit fields
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>
1 parent c3b28ea commit 60bc851

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

‎include/net/af_unix.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ struct unix_sock {
5757
struct list_head link;
5858
atomic_long_t inflight;
5959
spinlock_t lock;
60-
unsigned int gc_candidate : 1;
61-
unsigned int gc_maybe_cycle : 1;
6260
unsigned char recursion_level;
61+
unsigned long gc_flags;
62+
#define UNIX_GC_CANDIDATE 0
63+
#define UNIX_GC_MAYBE_CYCLE 1
6364
struct socket_wq peer_wq;
6465
};
6566
#define unix_sk(__sk) ((struct unix_sock *)__sk)

‎net/unix/garbage.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void scan_inflight(struct sock *x, void (*func)(struct unix_sock *),
185185
* have been added to the queues after
186186
* starting the garbage collection
187187
*/
188-
if (u->gc_candidate) {
188+
if (test_bit(UNIX_GC_CANDIDATE, &u->gc_flags)) {
189189
hit = true;
190190
func(u);
191191
}
@@ -254,7 +254,7 @@ static void inc_inflight_move_tail(struct unix_sock *u)
254254
* of the list, so that it's checked even if it was already
255255
* passed over
256256
*/
257-
if (u->gc_maybe_cycle)
257+
if (test_bit(UNIX_GC_MAYBE_CYCLE, &u->gc_flags))
258258
list_move_tail(&u->link, &gc_candidates);
259259
}
260260

@@ -315,8 +315,8 @@ void unix_gc(void)
315315
BUG_ON(total_refs < inflight_refs);
316316
if (total_refs == inflight_refs) {
317317
list_move_tail(&u->link, &gc_candidates);
318-
u->gc_candidate = 1;
319-
u->gc_maybe_cycle = 1;
318+
__set_bit(UNIX_GC_CANDIDATE, &u->gc_flags);
319+
__set_bit(UNIX_GC_MAYBE_CYCLE, &u->gc_flags);
320320
}
321321
}
322322

@@ -344,7 +344,7 @@ void unix_gc(void)
344344

345345
if (atomic_long_read(&u->inflight) > 0) {
346346
list_move_tail(&u->link, &not_cycle_list);
347-
u->gc_maybe_cycle = 0;
347+
__clear_bit(UNIX_GC_MAYBE_CYCLE, &u->gc_flags);
348348
scan_children(&u->sk, inc_inflight_move_tail, NULL);
349349
}
350350
}
@@ -356,7 +356,7 @@ void unix_gc(void)
356356
*/
357357
while (!list_empty(&not_cycle_list)) {
358358
u = list_entry(not_cycle_list.next, struct unix_sock, link);
359-
u->gc_candidate = 0;
359+
__clear_bit(UNIX_GC_CANDIDATE, &u->gc_flags);
360360
list_move_tail(&u->link, &gc_inflight_list);
361361
}
362362

0 commit comments

Comments
 (0)