Commit 9513e5e3f5a6b429da8a9fd4330f71f1e547c8e0

Authored by Heiko Carstens
Committed by Linus Torvalds
1 parent c563077e52

[PATCH] s390: spinlock corner case

On s390 the lock value used for spinlocks consists of the lower 32 bits of the
PSW that holds the lock.  If this address happens to be on a four gigabyte
boundary the lock is left unlocked.  This allows other cpus to grab the same
lock and enter a lock protected code path concurrently.  In theory this can
happen if the vmalloc area for the code of a module crosses a 4 GB boundary.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>

Showing 1 changed file with 2 additions and 2 deletions Inline Diff

include/asm-s390/spinlock.h
1 /* 1 /*
2 * include/asm-s390/spinlock.h 2 * include/asm-s390/spinlock.h
3 * 3 *
4 * S390 version 4 * S390 version
5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation 5 * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com) 6 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com)
7 * 7 *
8 * Derived from "include/asm-i386/spinlock.h" 8 * Derived from "include/asm-i386/spinlock.h"
9 */ 9 */
10 10
11 #ifndef __ASM_SPINLOCK_H 11 #ifndef __ASM_SPINLOCK_H
12 #define __ASM_SPINLOCK_H 12 #define __ASM_SPINLOCK_H
13 13
14 static inline int 14 static inline int
15 _raw_compare_and_swap(volatile unsigned int *lock, 15 _raw_compare_and_swap(volatile unsigned int *lock,
16 unsigned int old, unsigned int new) 16 unsigned int old, unsigned int new)
17 { 17 {
18 asm volatile ("cs %0,%3,0(%4)" 18 asm volatile ("cs %0,%3,0(%4)"
19 : "=d" (old), "=m" (*lock) 19 : "=d" (old), "=m" (*lock)
20 : "0" (old), "d" (new), "a" (lock), "m" (*lock) 20 : "0" (old), "d" (new), "a" (lock), "m" (*lock)
21 : "cc", "memory" ); 21 : "cc", "memory" );
22 return old; 22 return old;
23 } 23 }
24 24
25 /* 25 /*
26 * Simple spin lock operations. There are two variants, one clears IRQ's 26 * Simple spin lock operations. There are two variants, one clears IRQ's
27 * on the local processor, one does not. 27 * on the local processor, one does not.
28 * 28 *
29 * We make no fairness assumptions. They have a cost. 29 * We make no fairness assumptions. They have a cost.
30 */ 30 */
31 31
32 typedef struct { 32 typedef struct {
33 volatile unsigned int lock; 33 volatile unsigned int lock;
34 #ifdef CONFIG_PREEMPT 34 #ifdef CONFIG_PREEMPT
35 unsigned int break_lock; 35 unsigned int break_lock;
36 #endif 36 #endif
37 } __attribute__ ((aligned (4))) spinlock_t; 37 } __attribute__ ((aligned (4))) spinlock_t;
38 38
39 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 } 39 #define SPIN_LOCK_UNLOCKED (spinlock_t) { 0 }
40 #define spin_lock_init(lp) do { (lp)->lock = 0; } while(0) 40 #define spin_lock_init(lp) do { (lp)->lock = 0; } while(0)
41 #define spin_unlock_wait(lp) do { barrier(); } while(((volatile spinlock_t *)(lp))->lock) 41 #define spin_unlock_wait(lp) do { barrier(); } while(((volatile spinlock_t *)(lp))->lock)
42 #define spin_is_locked(x) ((x)->lock != 0) 42 #define spin_is_locked(x) ((x)->lock != 0)
43 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock) 43 #define _raw_spin_lock_flags(lock, flags) _raw_spin_lock(lock)
44 44
45 extern void _raw_spin_lock_wait(spinlock_t *lp, unsigned int pc); 45 extern void _raw_spin_lock_wait(spinlock_t *lp, unsigned int pc);
46 extern int _raw_spin_trylock_retry(spinlock_t *lp, unsigned int pc); 46 extern int _raw_spin_trylock_retry(spinlock_t *lp, unsigned int pc);
47 47
48 static inline void _raw_spin_lock(spinlock_t *lp) 48 static inline void _raw_spin_lock(spinlock_t *lp)
49 { 49 {
50 unsigned long pc = (unsigned long) __builtin_return_address(0); 50 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
51 51
52 if (unlikely(_raw_compare_and_swap(&lp->lock, 0, pc) != 0)) 52 if (unlikely(_raw_compare_and_swap(&lp->lock, 0, pc) != 0))
53 _raw_spin_lock_wait(lp, pc); 53 _raw_spin_lock_wait(lp, pc);
54 } 54 }
55 55
56 static inline int _raw_spin_trylock(spinlock_t *lp) 56 static inline int _raw_spin_trylock(spinlock_t *lp)
57 { 57 {
58 unsigned long pc = (unsigned long) __builtin_return_address(0); 58 unsigned long pc = 1 | (unsigned long) __builtin_return_address(0);
59 59
60 if (likely(_raw_compare_and_swap(&lp->lock, 0, pc) == 0)) 60 if (likely(_raw_compare_and_swap(&lp->lock, 0, pc) == 0))
61 return 1; 61 return 1;
62 return _raw_spin_trylock_retry(lp, pc); 62 return _raw_spin_trylock_retry(lp, pc);
63 } 63 }
64 64
65 static inline void _raw_spin_unlock(spinlock_t *lp) 65 static inline void _raw_spin_unlock(spinlock_t *lp)
66 { 66 {
67 _raw_compare_and_swap(&lp->lock, lp->lock, 0); 67 _raw_compare_and_swap(&lp->lock, lp->lock, 0);
68 } 68 }
69 69
70 /* 70 /*
71 * Read-write spinlocks, allowing multiple readers 71 * Read-write spinlocks, allowing multiple readers
72 * but only one writer. 72 * but only one writer.
73 * 73 *
74 * NOTE! it is quite common to have readers in interrupts 74 * NOTE! it is quite common to have readers in interrupts
75 * but no interrupt writers. For those circumstances we 75 * but no interrupt writers. For those circumstances we
76 * can "mix" irq-safe locks - any writer needs to get a 76 * can "mix" irq-safe locks - any writer needs to get a
77 * irq-safe write-lock, but readers can get non-irqsafe 77 * irq-safe write-lock, but readers can get non-irqsafe
78 * read-locks. 78 * read-locks.
79 */ 79 */
80 typedef struct { 80 typedef struct {
81 volatile unsigned int lock; 81 volatile unsigned int lock;
82 volatile unsigned long owner_pc; 82 volatile unsigned long owner_pc;
83 #ifdef CONFIG_PREEMPT 83 #ifdef CONFIG_PREEMPT
84 unsigned int break_lock; 84 unsigned int break_lock;
85 #endif 85 #endif
86 } rwlock_t; 86 } rwlock_t;
87 87
88 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 } 88 #define RW_LOCK_UNLOCKED (rwlock_t) { 0, 0 }
89 89
90 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0) 90 #define rwlock_init(x) do { *(x) = RW_LOCK_UNLOCKED; } while(0)
91 91
92 /** 92 /**
93 * read_can_lock - would read_trylock() succeed? 93 * read_can_lock - would read_trylock() succeed?
94 * @lock: the rwlock in question. 94 * @lock: the rwlock in question.
95 */ 95 */
96 #define read_can_lock(x) ((int)(x)->lock >= 0) 96 #define read_can_lock(x) ((int)(x)->lock >= 0)
97 97
98 /** 98 /**
99 * write_can_lock - would write_trylock() succeed? 99 * write_can_lock - would write_trylock() succeed?
100 * @lock: the rwlock in question. 100 * @lock: the rwlock in question.
101 */ 101 */
102 #define write_can_lock(x) ((x)->lock == 0) 102 #define write_can_lock(x) ((x)->lock == 0)
103 103
104 extern void _raw_read_lock_wait(rwlock_t *lp); 104 extern void _raw_read_lock_wait(rwlock_t *lp);
105 extern int _raw_read_trylock_retry(rwlock_t *lp); 105 extern int _raw_read_trylock_retry(rwlock_t *lp);
106 extern void _raw_write_lock_wait(rwlock_t *lp); 106 extern void _raw_write_lock_wait(rwlock_t *lp);
107 extern int _raw_write_trylock_retry(rwlock_t *lp); 107 extern int _raw_write_trylock_retry(rwlock_t *lp);
108 108
109 static inline void _raw_read_lock(rwlock_t *rw) 109 static inline void _raw_read_lock(rwlock_t *rw)
110 { 110 {
111 unsigned int old; 111 unsigned int old;
112 old = rw->lock & 0x7fffffffU; 112 old = rw->lock & 0x7fffffffU;
113 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old) 113 if (_raw_compare_and_swap(&rw->lock, old, old + 1) != old)
114 _raw_read_lock_wait(rw); 114 _raw_read_lock_wait(rw);
115 } 115 }
116 116
117 static inline void _raw_read_unlock(rwlock_t *rw) 117 static inline void _raw_read_unlock(rwlock_t *rw)
118 { 118 {
119 unsigned int old, cmp; 119 unsigned int old, cmp;
120 120
121 old = rw->lock; 121 old = rw->lock;
122 do { 122 do {
123 cmp = old; 123 cmp = old;
124 old = _raw_compare_and_swap(&rw->lock, old, old - 1); 124 old = _raw_compare_and_swap(&rw->lock, old, old - 1);
125 } while (cmp != old); 125 } while (cmp != old);
126 } 126 }
127 127
128 static inline void _raw_write_lock(rwlock_t *rw) 128 static inline void _raw_write_lock(rwlock_t *rw)
129 { 129 {
130 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0)) 130 if (unlikely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) != 0))
131 _raw_write_lock_wait(rw); 131 _raw_write_lock_wait(rw);
132 } 132 }
133 133
134 static inline void _raw_write_unlock(rwlock_t *rw) 134 static inline void _raw_write_unlock(rwlock_t *rw)
135 { 135 {
136 _raw_compare_and_swap(&rw->lock, 0x80000000, 0); 136 _raw_compare_and_swap(&rw->lock, 0x80000000, 0);
137 } 137 }
138 138
139 static inline int _raw_read_trylock(rwlock_t *rw) 139 static inline int _raw_read_trylock(rwlock_t *rw)
140 { 140 {
141 unsigned int old; 141 unsigned int old;
142 old = rw->lock & 0x7fffffffU; 142 old = rw->lock & 0x7fffffffU;
143 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old)) 143 if (likely(_raw_compare_and_swap(&rw->lock, old, old + 1) == old))
144 return 1; 144 return 1;
145 return _raw_read_trylock_retry(rw); 145 return _raw_read_trylock_retry(rw);
146 } 146 }
147 147
148 static inline int _raw_write_trylock(rwlock_t *rw) 148 static inline int _raw_write_trylock(rwlock_t *rw)
149 { 149 {
150 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0)) 150 if (likely(_raw_compare_and_swap(&rw->lock, 0, 0x80000000) == 0))
151 return 1; 151 return 1;
152 return _raw_write_trylock_retry(rw); 152 return _raw_write_trylock_retry(rw);
153 } 153 }
154 154
155 #endif /* __ASM_SPINLOCK_H */ 155 #endif /* __ASM_SPINLOCK_H */
156 156