MIPS: Remove irqflags.h dependency from bitops.h
Subsystems (2)
PATCH v3 2/2 MIPS: make funcs preempt-safe for non-mipsr2 cpus
In reply to Jim QuinlanFor non MIPSr2 processors, such as the BMIPS 5000, calls to arch_local_irq_disable() and others may be preempted, and in doing so a stale value may be restored to c0_status. This fix disables preemption for such processors prior to the call and enables it after the call.
Those functions that needed this fix have been "outlined" to mips-atomic.c, as they are no longer good candidates for inlining.
This bug was observed in a BMIPS 5000, occuring once every few hours in a continuous reboot test. It was traced to the write_lock_irq() function which was being invoked in release_task() in exit.c. By placing a number of "nops" inbetween the mfc0/mtc0 pair in arch_local_irq_disable(), which is called by write_lock_irq(), we were able to greatly increase the occurance of this bug. Similarly, the application of this commit silenced the bug.
--- arch/mips/include/asm/irqflags.h | 92 +++++--------------- arch/mips/lib/Makefile | 3 +- arch/mips/lib/mips-atomic.c | 179 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 72 deletions(-) create mode 100644 arch/mips/lib/mips-atomic.c
arch/mips/include/asm/irqflags.h +21 -71
@@ -16,6 +16,15 @@#include <linux/compiler.h> #include <asm/hazards.h> +#if !defined(CONFIG_CPU_MIPSR2 ) || defined(CONFIG_MT_SMTC ) +/* Functions that require preempt_{dis,en}able() are in mips-atomic.c */ +extern void arch_local_irq_disable (void ); +extern unsigned long arch_local_irq_save (void ); +extern void arch_local_irq_restore (unsigned long flags); +extern void __arch_local_irq_restore (unsigned long flags); +#endif + + __asm__(" .macro arch_local_irq_enable \n" " .set push \n"
@@ -57,42 +66,12 @@ static inline void arch_local_irq_enable(void) } -/* -* For cli() we have to insert nops to make sure that the new value -* has actually arrived in the status register before the end of this -* macro. -* R4000/R4400 need three nops, the R4600 two nops and the R10000 needs -* no nops at all. -*/ -/* -* For TX49, operating only IE bit is not enough. -* -* If mfc0 $12 follows store and the mfc0 is last instruction of a -* page and fetching the next instruction causes TLB miss, the result -* of the mfc0 might wrongly contain EXL bit. -* -* ERT-TX49H2-027, ERT-TX49H3-012, ERT-TX49HL3-006, ERT-TX49H4-008 -* -* Workaround: mask EXL bit of the result or place a nop before mfc0. -*/ +#if defined(CONFIG_CPU_MIPSR2 ) && !defined(CONFIG_MT_SMTC ) __asm__(" .macro arch_local_irq_disable\n" " .set push \n" " .set noat \n" -#ifdef CONFIG_MIPS_MT_SMTC -" mfc0 $1, $2, 1 \n" -" ori $1, 0x400 \n" -" .set noreorder \n" -" mtc0 $1, $2, 1 \n" -#elif defined(CONFIG_CPU_MIPSR2)" di \n" -#else - " mfc0 $1,$12 \n" - " ori $1,0x1f \n" - " xori $1,0x1f \n" - " .set noreorder \n" - " mtc0 $1,$12 \n" -#endif" irq_disable_hazard \n" " .set pop \n" " .endm \n" );
@@ -105,6 +84,8 @@ static inline void arch_local_irq_disable(void) : /* no inputs */ : "memory"); } +#endif + __asm__( " .macro arch_local_save_flags flags \n"
@@ -125,27 +106,15 @@ static inline unsigned long arch_local_save_flags(void)return flags; } + +#if defined(CONFIG_CPU_MIPSR2 ) && !defined(CONFIG_MT_SMTC ) __asm__(" .macro arch_local_irq_save result \n" " .set push \n" " .set reorder \n" " .set noat \n" -#ifdef CONFIG_MIPS_MT_SMTC -" mfc0 \\result, $2, 1 \n" -" ori $1, \\result, 0x400 \n" -" .set noreorder \n" -" mtc0 $1, $2, 1 \n" - " andi \\result, \\result, 0x400 \n" -#elif defined(CONFIG_CPU_MIPSR2 )" di \\result \n" " andi \\result, 1 \n" -#else -" mfc0 \\result, $12 \n" -" ori $1, \\result, 0x1f \n" -" xori $1, 0x1f \n" -" .set noreorder \n" - " mtc0 $1, $12 \n" -#endif " irq_disable_hazard \n" " .set pop \n" " .endm \n" );
@@ -159,20 +128,16 @@ static inline unsigned long arch_local_irq_save(void) : "memory");return flags; } +#endif + +#if defined(CONFIG_CPU_MIPSR2 ) && !defined(CONFIG_MT_SMTC ) __asm__(" .macro arch_local_irq_restore flags \n" " .set push \n" " .set noreorder \n" " .set noat \n" -#ifdef CONFIG_MIPS_MT_SMTC -"mfc0 $1, $2, 1 \n" -"andi \\flags, 0x400 \n" -"ori $1, 0x400 \n" -"xori $1, 0x400 \n" -"or \\flags, $1 \n" -"mtc0 \\flags, $2, 1 \n" -#elif defined(CONFIG_CPU_MIPSR2) && defined(CONFIG_IRQ_CPU) +#if defined(CONFIG_IRQ_CPU)/* * Slow, but doesn't suffer from a relatively unlikely race * condition we're having since days 1.
@@ -181,20 +146,13 @@ __asm__(" di \n" " ei \n" "1: \n" -#elif defined(CONFIG_CPU_MIPSR2 ) +#else /* * Fast, dangerous. Life is fun, life is good. */ " mfc0 $1, $12 \n" " ins $1, \\flags, 0, 1 \n" " mtc0 $1, $12 \n" -#else -" mfc0 $1, $12 \n" -" andi \\flags, 1 \n" -" ori $1, 0x1f \n" -" xori $1, 0x1f \n" -" or \\flags, $1 \n" - " mtc0 \\flags, $12 \n"#endif " irq_disable_hazard \n" " .set pop \n"
@@ -205,16 +163,6 @@ static inline void arch_local_irq_restore(unsigned long flags) {unsigned long __tmp1; -#ifdef CONFIG_MIPS_MT_SMTC -/* -* SMTC kernel needs to do a software replay of queued -* IPIs, at the cost of branch and call overhead on each -* local_irq_restore() -*/ -if (unlikely (!(flags &0x0400 ))) -smtc_ipi_replay (); -#endif - __asm__ __volatile__("arch_local_irq_restore\t%0" : "=r" (__tmp1)
@@ -232,6 +180,8 @@ static inline void __arch_local_irq_restore(unsigned long flags) : "0" (flags) : "memory"); } +#endif +static inline int arch_irqs_disabled_flags (unsigned long flags) {
arch/mips/lib/Makefile +2 -1
@@ -3,7 +3,8 @@ # lib-y += bitops.o csum_partial.o delay.o memcpy.o memset.o \ - strlen_user.o strncpy_user.o strnlen_user.o uncached.o + mips-atomic.o strlen_user.o strncpy_user.o \ + strnlen_user.o uncached.o obj-y += iomap.o obj-$(CONFIG_PCI) += iomap-pci.o
arch/mips/lib/mips-atomic.c new file +179 -0
179 lines not on this page · raw message
Signature · 1 line
1.7.6
PATCH v3 1/2 MIPS: Remove irqflags.h dependency from bitops.h
The "else clause" of most functions in bitops.h invoked raw_local_irq_{save,restore}() and so had a dependency on irqflags.h. This fix moves said code to bitops.c, removing the dependency.
--- arch/mips/include/asm/bitops.h | 114 +++++++------------------ arch/mips/include/asm/io.h | 1 + arch/mips/lib/Makefile | 2 +- arch/mips/lib/bitops.c | 180 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+), 83 deletions(-) create mode 100644 arch/mips/lib/bitops.c
arch/mips/include/asm/bitops.h +32 -82
@@ -14,7 +14,6 @@#endif #include <linux/compiler.h> -#include <linux/irqflags.h> #include <linux/types.h> #include <asm/barrier.h> #include <asm/byteorder.h> /* sigh ... */
@@ -44,6 +43,24 @@#define smp_mb__before_clear_bit () smp_mb__before_llsc()#define smp_mb__after_clear_bit () smp_llsc_mb() + +/* +* These are the "slower" versions of the functions and are in bitops.c. +* These functions call raw_local_irq_{save,restore}(). +*/ +extern void atomic_set_bit (unsigned long nr,volatile unsigned long *addr); +extern void atomic_clear_bit (unsigned long nr,volatile unsigned long *addr); +extern void atomic_change_bit (unsigned long nr,volatile unsigned long *addr); +extern int atomic_test_and_set_bit (unsigned long nr, +volatile unsigned long *addr); +extern int atomic_test_and_set_bit_lock (unsigned long nr, +volatile unsigned long *addr); +extern int atomic_test_and_clear_bit (unsigned long nr, +volatile unsigned long *addr); +extern int atomic_test_and_change_bit (unsigned long nr, +volatile unsigned long *addr); + +/* * set_bit - Atomically set a bit in memory * @nr: the bit to set
@@ -92,17 +109,8 @@ static inline void set_bit(unsigned long nr, volatile unsigned long *addr) : "=&r" (temp), "+m" (*m) : "ir" (1UL << bit)); }while (unlikely (!temp)); - }else { -volatile unsigned long *a = addr; -unsigned long mask; -unsigned long flags; - - a += nr >>SZLONG_LOG ; - mask =1UL << bit; -raw_local_irq_save (flags); - *a |= mask; -raw_local_irq_restore (flags); - } + }else +atomic_set_bit (nr, addr); }/*
@@ -153,17 +161,8 @@ static inline void clear_bit(unsigned long nr, volatile unsigned long *addr) : "=&r" (temp), "+m" (*m) : "ir" (~(1UL << bit))); }while (unlikely (!temp)); - }else { -volatile unsigned long *a = addr; -unsigned long mask; -unsigned long flags; - - a += nr >>SZLONG_LOG ; - mask =1UL << bit; -raw_local_irq_save (flags); - *a &= ~mask; -raw_local_irq_restore (flags); - } + }else +atomic_clear_bit (nr, addr); }/*
@@ -220,17 +219,8 @@ static inline void change_bit(unsigned long nr, volatile unsigned long *addr) : "=&r" (temp), "+m" (*m) : "ir" (1UL << bit)); }while (unlikely (!temp)); - }else { -volatile unsigned long *a = addr; -unsigned long mask; -unsigned long flags; - - a += nr >>SZLONG_LOG ; - mask =1UL << bit; -raw_local_irq_save (flags); - *a ^= mask; -raw_local_irq_restore (flags); - } + }else +atomic_change_bit (nr, addr); }/*
@@ -281,18 +271,8 @@ static inline int test_and_set_bit(unsigned long nr, }while (unlikely (!res)); res = temp & (1UL << bit); - }else { -volatile unsigned long *a = addr; -unsigned long mask; -unsigned long flags; - - a += nr >>SZLONG_LOG ; - mask =1UL << bit; -raw_local_irq_save (flags); - res = (mask & *a); - *a |= mask; -raw_local_irq_restore (flags); - } + }else + res =atomic_test_and_set_bit (nr, addr);smp_llsc_mb ();
@@ -345,18 +325,8 @@ static inline int test_and_set_bit_lock(unsigned long nr, }while (unlikely (!res)); res = temp & (1UL << bit); - }else { -volatile unsigned long *a = addr; -unsigned long mask; -unsigned long flags; - - a += nr >>SZLONG_LOG ; - mask =1UL << bit; -raw_local_irq_save (flags); - res = (mask & *a); - *a |= mask; -raw_local_irq_restore (flags); - } + }else + res =atomic_test_and_set_bit_lock (nr, addr);smp_llsc_mb ();
@@ -428,18 +398,8 @@ static inline int test_and_clear_bit(unsigned long nr, }while (unlikely (!res)); res = temp & (1UL << bit); - }else { -volatile unsigned long *a = addr; -unsigned long mask; -unsigned long flags; - - a += nr >>SZLONG_LOG ; - mask =1UL << bit; -raw_local_irq_save (flags); - res = (mask & *a); - *a &= ~mask; -raw_local_irq_restore (flags); - } + }else + res =atomic_test_and_clear_bit (nr, addr);smp_llsc_mb ();
@@ -494,18 +454,8 @@ static inline int test_and_change_bit(unsigned long nr, }while (unlikely (!res)); res = temp & (1UL << bit); - }else { -volatile unsigned long *a = addr; -unsigned long mask; -unsigned long flags; - - a += nr >>SZLONG_LOG ; - mask =1UL << bit; -raw_local_irq_save (flags); - res = (mask & *a); - *a ^= mask; -raw_local_irq_restore (flags); - } + }else + res =atomic_test_and_change_bit (nr, addr);smp_llsc_mb ();
arch/mips/include/asm/io.h +1 -0
@@ -15,6 +15,7 @@#include <linux/compiler.h> #include <linux/kernel.h> #include <linux/types.h> +#include <linux/irqflags.h> #include <asm/addrspace.h> #include <asm/bug.h>
arch/mips/lib/Makefile +1 -1
@@ -2,7 +2,7 @@ # Makefile for MIPS-specific library files.. # -lib-y += csum_partial.o delay.o memcpy.o memset.o \ +lib-y += bitops.o csum_partial.o delay.o memcpy.o memset.o \ strlen_user.o strncpy_user.o strnlen_user.o uncached.o obj-y += iomap.o
arch/mips/lib/bitops.c new file +180 -0
180 lines not on this page · raw message
Signature · 1 line
1.7.6
Quoted 5 lines · Jim Quinlan
The "else clause" of most functions in
bitops.hinvoked raw_local_irq_{save,restore}() and so had a dependency onirqflags.h. This fix moves said code tobitops.c, removing the dependency.Signed-off-by Jim Quinlan <[email protected]>
This is much better I think.
Now only a few very minor things I would change...
Quoted 31 lines
--- arch/mips/include/asm/bitops.h | 114 +++++++------------------ arch/mips/include/asm/io.h | 1 + arch/mips/lib/Makefile | 2 +- arch/mips/lib/bitops.c | 180 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 214 insertions(+), 83 deletions(-)create mode 100644
arch/mips/lib/bitops.carch/mips/include/asm/bitops.h +7
-1@@ -14,7 +14,6 @@#endif #include <linux/compiler.h> -#include <linux/irqflags.h> #include <linux/types.h> #include <asm/barrier.h> #include <asm/byteorder.h> /* sigh ... */ @@ -44,6 +43,24 @@#define smp_mb__before_clear_bit () smp_mb__before_llsc()#define smp_mb__after_clear_bit () smp_llsc_mb() + +/* +* These are the "slower" versions of the functions and are in bitops.c. +* These functions call raw_local_irq_{save,restore}(). +*/ +extern void atomic_set_bit (unsigned long nr,volatile unsigned long *addr); +extern void atomic_clear_bit (unsigned long nr,volatile unsigned long *addr);
arch/mips/include/asm/bitops.h line 62
Quoted 10 lines
arch/mips/include/asm/bitops.h +10
-0+extern void atomic_change_bit (unsigned long nr,volatile unsigned long *addr); +extern int atomic_test_and_set_bit (unsigned long nr, +volatile unsigned long *addr); +extern int atomic_test_and_set_bit_lock (unsigned long nr, +volatile unsigned long *addr); +extern int atomic_test_and_clear_bit (unsigned long nr, +volatile unsigned long *addr); +extern int atomic_test_and_change_bit (unsigned long nr, +volatile unsigned long *addr); +
No 'extern' needed.
These shouldn't be directly called from user code. I would suggest renaming the functions to something like:
__mips_set_bit();
[...]
Quoted 16 lines
arch/mips/lib/bitops.c new file +10
-0@@ -0,0 +1,180 @@ + +/* +* This file is subject to the terms and conditions of the GNU General Public +* License. See the file "COPYING" in the main directory of this archive +* for more details. +* +* Copyright (c) 1994-1997, 99, 2000, 06, 07 Ralf Baechle ([email protected]) +* Copyright (c) 1999, 2000 Silicon Graphics, Inc. +*/ +
arch/mips/lib/bitops.c line 20
Quoted 10 lines
arch/mips/lib/bitops.c new file +10
-0+#include <linux/irqflags.h> + +#if _MIPS_SZLONG ==32 +#define SZLONG_LOG 5 +#define SZLONG_MASK 31UL +#elif _MIPS_SZLONG ==64 +#define SZLONG_MASK 63UL +#define SZLONG_LOG 6 +#endif +
There has to be a cleaner way to do this... Perhaps:
#define SZLONG_LOG (ilog2(sizeof(unsigned long)) + 3) #define SZLONG_MASK ((1 << SZLONG_LOG) - 1)
arch/mips/lib/bitops.c line 31
Quoted 11 lines
arch/mips/lib/bitops.c new file +11
-0+ +/* +* atomic_set_bit - Atomically set a bit in memory. This is called by +* if set_bit() if it cannot find a faster solution. +* @nr: the bit to set +* @addr: the address to start counting from +*/ +void atomic_set_bit (unsigned long nr,volatile unsigned long *addr) +{ +volatile unsigned long *a = addr; +unsigned short bit = nr &SZLONG_MASK ;
just make bit type 'int'. In some cases forcing a narrower type than necessary requires the compiler to emit extra code. I am not sure if it would here, but why use a type other than int unless absolutely necessary?
arch/mips/lib/bitops.c line 40
Quoted 9 lines
arch/mips/lib/bitops.c new file +9
-0+unsigned long mask; +unsigned long flags; + + a += nr >>SZLONG_LOG ; + mask =1UL << bit; +raw_local_irq_save (flags); + *a |= mask; +raw_local_irq_restore (flags); +}
All these must be EXPORT_SYMBOL(), so the bitop intrinsics can be used from modules.
Re: [PATCH V3 2/2] MIPS: make funcs preempt-safe for non-mipsr2 cpus
In reply to Jim QuinlanQuoted 17 lines · Jim Quinlan
For non MIPSr2 processors, such as the BMIPS 5000, calls to
arch_local_irq_disable()and others may be preempted, and in doing so a stale value may be restored toc0_status. This fix disables preemption for such processors prior to the call and enables it after the call.Those functions that needed this fix have been "outlined" to
mips-atomic.c, as they are no longer good candidates for inlining.This bug was observed in a BMIPS 5000, occuring once every few hours in a continuous reboot test. It was traced to the
write_lock_irq()function which was being invoked inrelease_task()inexit.c. By placing a number of "nops" inbetween the mfc0/mtc0 pair inarch_local_irq_disable(), which is called bywrite_lock_irq(), we were able to greatly increase the occurance of this bug. Similarly, the application of this commit silenced the bug.
Quoted 1 line
Signed-off-by Jim Quinlan <[email protected]>
This one seems better too...
[...]
Quoted 72 lines
arch/mips/lib/mips-atomic.c new file +66
-0@@ -0,0 +1,179 @@ +/* +* This file is subject to the terms and conditions of the GNU General Public +* License. See the file "COPYING" in the main directory of this archive +* for more details. +* +* Copyright (C) 1994, 95, 96, 97, 98, 99, 2003 by Ralf Baechle +* Copyright (C) 1996 by Paul M. Antoine +* Copyright (C) 1999 Silicon Graphics +* Copyright (C) 2000 MIPS Technologies, Inc. +*/ +#include <asm/irqflags.h> +#include <asm/hazards.h> +#include <linux/compiler.h> +#include <linux/preempt.h> + +#if !defined(CONFIG_CPU_MIPSR2 ) || defined(CONFIG_MIPS_MT_SMTC ) + +#if defined(CONFIG_PREEMPT ) +#define arch_local_preempt_enable () preempt_enable() +#define arch_local_preempt_disable () preempt_disable() +#else +#define arch_local_preempt_enable () +#define arch_local_preempt_disable () +#endif + + +/* +* For cli() we have to insert nops to make sure that the new value +* has actually arrived in the status register before the end of this +* macro. +* R4000/R4400 need three nops, the R4600 two nops and the R10000 needs +* no nops at all. +*/ +/* +* For TX49, operating only IE bit is not enough. +* +* If mfc0 $12 follows store and the mfc0 is last instruction of a +* page and fetching the next instruction causes TLB miss, the result +* of the mfc0 might wrongly contain EXL bit. +* +* ERT-TX49H2-027, ERT-TX49H3-012, ERT-TX49HL3-006, ERT-TX49H4-008 +* +* Workaround: mask EXL bit of the result or place a nop before mfc0. +*/ +__asm__( +" .macro arch_local_irq_disable\n" +" .set push \n" +" .set noat \n" +#ifdef CONFIG_MIPS_MT_SMTC +" mfc0 $1, $2, 1 \n" +" ori $1, 0x400 \n" +" .set noreorder \n" +" mtc0 $1, $2, 1 \n" +#elif defined(CONFIG_CPU_MIPSR2) + /* see irqflags.h for inline function */ +#else +" mfc0 $1,$12 \n" +" ori $1,0x1f \n" +" xori $1,0x1f \n" +" .set noreorder \n" + " mtc0 $1,$12 \n" +#endif +" irq_disable_hazard \n" +" .set pop \n" + " .endm \n"); +
arch/mips/lib/mips-atomic.c line 76
Quoted 10 lines
arch/mips/lib/mips-atomic.c new file +10
-0+void arch_local_irq_disable (void ) +{ +arch_local_preempt_disable (); + __asm__ __volatile__( +"arch_local_irq_disable" + :/* no outputs */ + :/* no inputs */ + :"memory" ); +arch_local_preempt_enable (); +}
I think this function must be EXPORT_SYMBOL() too.
David Daney