Skip to content

Commit 84d88d5

Browse files
committed
Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: sched_clock: Fix atomicity/continuity bug by using cmpxchg64() x86: Provide an alternative() based cmpxchg64()
2 parents e399835 + 152f9d0 commit 84d88d5

5 files changed

Lines changed: 85 additions & 16 deletions

File tree

‎arch/x86/include/asm/cmpxchg_32.h‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,19 +312,23 @@ static inline unsigned long cmpxchg_386(volatile void *ptr, unsigned long old,
312312

313313
extern unsigned long long cmpxchg_486_u64(volatile void *, u64, u64);
314314

315-
#define cmpxchg64(ptr, o, n) \
316-
({ \
317-
__typeof__(*(ptr)) __ret; \
318-
if (likely(boot_cpu_data.x86 > 4)) \
319-
__ret = (__typeof__(*(ptr)))__cmpxchg64((ptr), \
320-
(unsigned long long)(o), \
321-
(unsigned long long)(n)); \
322-
else \
323-
__ret = (__typeof__(*(ptr)))cmpxchg_486_u64((ptr), \
324-
(unsigned long long)(o), \
325-
(unsigned long long)(n)); \
326-
__ret; \
327-
})
315+
#define cmpxchg64(ptr, o, n) \
316+
({ \
317+
__typeof__(*(ptr)) __ret; \
318+
__typeof__(*(ptr)) __old = (o); \
319+
__typeof__(*(ptr)) __new = (n); \
320+
alternative_io("call cmpxchg8b_emu", \
321+
"lock; cmpxchg8b (%%esi)" , \
322+
X86_FEATURE_CX8, \
323+
"=A" (__ret), \
324+
"S" ((ptr)), "0" (__old), \
325+
"b" ((unsigned int)__new), \
326+
"c" ((unsigned int)(__new>>32)) \
327+
: "memory"); \
328+
__ret; })
329+
330+
331+
328332
#define cmpxchg64_local(ptr, o, n) \
329333
({ \
330334
__typeof__(*(ptr)) __ret; \

‎arch/x86/kernel/i386_ksyms_32.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
EXPORT_SYMBOL(mcount);
1111
#endif
1212

13+
/*
14+
* Note, this is a prototype to get at the symbol for
15+
* the export, but dont use it from C code, it is used
16+
* by assembly code and is not using C calling convention!
17+
*/
18+
extern void cmpxchg8b_emu(void);
19+
EXPORT_SYMBOL(cmpxchg8b_emu);
20+
1321
/* Networking helper routines. */
1422
EXPORT_SYMBOL(csum_partial_copy_generic);
1523

‎arch/x86/lib/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ifeq ($(CONFIG_X86_32),y)
1515
obj-y += atomic64_32.o
1616
lib-y += checksum_32.o
1717
lib-y += strstr_32.o
18-
lib-y += semaphore_32.o string_32.o
18+
lib-y += semaphore_32.o string_32.o cmpxchg8b_emu.o
1919

2020
lib-$(CONFIG_X86_USE_3DNOW) += mmx_32.o
2121
else

‎arch/x86/lib/cmpxchg8b_emu.S‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or
3+
* modify it under the terms of the GNU General Public License
4+
* as published by the Free Software Foundation; version 2
5+
* of the License.
6+
*
7+
*/
8+
9+
#include <linux/linkage.h>
10+
#include <asm/alternative-asm.h>
11+
#include <asm/frame.h>
12+
#include <asm/dwarf2.h>
13+
14+
15+
.text
16+
17+
/*
18+
* Inputs:
19+
* %esi : memory location to compare
20+
* %eax : low 32 bits of old value
21+
* %edx : high 32 bits of old value
22+
* %ebx : low 32 bits of new value
23+
* %ecx : high 32 bits of new value
24+
*/
25+
ENTRY(cmpxchg8b_emu)
26+
CFI_STARTPROC
27+
28+
#
29+
# Emulate 'cmpxchg8b (%esi)' on UP except we don't
30+
# set the whole ZF thing (caller will just compare
31+
# eax:edx with the expected value)
32+
#
33+
cmpxchg8b_emu:
34+
pushfl
35+
cli
36+
37+
cmpl (%esi), %eax
38+
jne not_same
39+
cmpl 4(%esi), %edx
40+
jne half_same
41+
42+
movl %ebx, (%esi)
43+
movl %ecx, 4(%esi)
44+
45+
popfl
46+
ret
47+
48+
not_same:
49+
movl (%esi), %eax
50+
half_same:
51+
movl 4(%esi), %edx
52+
53+
popfl
54+
ret
55+
56+
CFI_ENDPROC
57+
ENDPROC(cmpxchg8b_emu)

‎kernel/sched_clock.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static u64 sched_clock_local(struct sched_clock_data *scd)
127127
clock = wrap_max(clock, min_clock);
128128
clock = wrap_min(clock, max_clock);
129129

130-
if (cmpxchg(&scd->clock, old_clock, clock) != old_clock)
130+
if (cmpxchg64(&scd->clock, old_clock, clock) != old_clock)
131131
goto again;
132132

133133
return clock;
@@ -163,7 +163,7 @@ static u64 sched_clock_remote(struct sched_clock_data *scd)
163163
val = remote_clock;
164164
}
165165

166-
if (cmpxchg(ptr, old_val, val) != old_val)
166+
if (cmpxchg64(ptr, old_val, val) != old_val)
167167
goto again;
168168

169169
return val;

0 commit comments

Comments
 (0)