This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / parisc / lib / debuglocks.c
1 /* 
2  *    Debugging versions of SMP locking primitives.
3  *
4  *    Copyright (C) 2004 Thibaut VARENE <varenet@esiee.fr>
5  *
6  *    Some code stollen from alpha & sparc64 ;)
7  *
8  *    This program is free software; you can redistribute it and/or modify
9  *    it under the terms of the GNU General Public License as published by
10  *    the Free Software Foundation; either version 2 of the License, or
11  *    (at your option) any later version.
12  *
13  *    This program is distributed in the hope that it will be useful,
14  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *    GNU General Public License for more details.
17  *
18  *    You should have received a copy of the GNU General Public License
19  *    along with this program; if not, write to the Free Software
20  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23
24 #include <linux/config.h>
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <asm/system.h>
29 #include <asm/hardirq.h>        /* in_interrupt() */
30
31 #undef INIT_STUCK
32 #define INIT_STUCK 1L << 30
33
34 #ifdef CONFIG_DEBUG_SPINLOCK
35
36 void _dbg_spin_lock(spinlock_t * lock, const char *base_file, int line_no)
37 {
38         volatile unsigned int *a;
39         long stuck = INIT_STUCK;
40         void *inline_pc = __builtin_return_address(0);
41         unsigned long started = jiffies;
42         int printed = 0;
43         int cpu = smp_processor_id();
44
45 try_again:
46
47         /* Do the actual locking */
48         /* <T-Bone> ggg: we can't get stuck on the outter loop?
49          * <ggg> T-Bone: We can hit the outer loop
50          *      alot if multiple CPUs are constantly racing for a lock
51          *      and the backplane is NOT fair about which CPU sees
52          *      the update first. But it won't hang since every failed
53          *      attempt will drop us back into the inner loop and
54          *      decrement `stuck'.
55          * <ggg> K-class and some of the others are NOT fair in the HW
56          *      implementation so we could see false positives.
57          *      But fixing the lock contention is easier than
58          *      fixing the HW to be fair.
59          * <tausq> __ldcw() returns 1 if we get the lock; otherwise we
60          *      spin until the value of the lock changes, or we time out.
61          */
62         a = __ldcw_align(lock);
63         while (stuck && (__ldcw(a) == 0))
64                 while ((*a == 0) && --stuck);
65
66         if (unlikely(stuck <= 0)) {
67                 printk(KERN_WARNING
68                         "%s:%d: spin_lock(%s/%p) stuck in %s at %p(%d)"
69                         " owned by %s:%d in %s at %p(%d)\n",
70                         base_file, line_no, lock->module, lock,
71                         current->comm, inline_pc, cpu,
72                         lock->bfile, lock->bline, lock->task->comm,
73                         lock->previous, lock->oncpu);
74                 stuck = INIT_STUCK;
75                 printed = 1;
76                 goto try_again;
77         }
78
79         /* Exiting.  Got the lock.  */
80         lock->oncpu = cpu;
81         lock->previous = inline_pc;
82         lock->task = current;
83         lock->bfile = (char *)base_file;
84         lock->bline = line_no;
85
86         if (unlikely(printed)) {
87                 printk(KERN_WARNING
88                         "%s:%d: spin_lock grabbed in %s at %p(%d) %ld ticks\n",
89                         base_file, line_no, current->comm, inline_pc,
90                         cpu, jiffies - started);
91         }
92 }
93
94 void _dbg_spin_unlock(spinlock_t * lock, const char *base_file, int line_no)
95 {
96         CHECK_LOCK(lock);
97         volatile unsigned int *a = __ldcw_align(lock);
98         if (unlikely((*a != 0) && lock->babble)) {
99                 lock->babble--;
100                 printk(KERN_WARNING
101                         "%s:%d: spin_unlock(%s:%p) not locked\n",
102                         base_file, line_no, lock->module, lock);
103         }
104         *a = 1; 
105 }
106
107 int _dbg_spin_trylock(spinlock_t * lock, const char *base_file, int line_no)
108 {
109         int ret;
110         volatile unsigned int *a = __ldcw_align(lock);
111         if ((ret = (__ldcw(a) != 0))) {
112                 lock->oncpu = smp_processor_id();
113                 lock->previous = __builtin_return_address(0);
114                 lock->task = current;
115         } else {
116                 lock->bfile = (char *)base_file;
117                 lock->bline = line_no;
118         }
119         return ret;
120 }
121
122 #endif /* CONFIG_DEBUG_SPINLOCK */
123
124 #ifdef CONFIG_DEBUG_RWLOCK
125
126 /* Interrupts trouble detailed explanation, thx Grant:
127  *
128  * o writer (wants to modify data) attempts to acquire the rwlock
129  * o He gets the write lock.
130  * o Interupts are still enabled, we take an interrupt with the
131  *   write still holding the lock.
132  * o interrupt handler tries to acquire the rwlock for read.
133  * o deadlock since the writer can't release it at this point.
134  * 
135  * In general, any use of spinlocks that competes between "base"
136  * level and interrupt level code will risk deadlock. Interrupts
137  * need to be disabled in the base level routines to avoid it.
138  * Or more precisely, only the IRQ the base level routine
139  * is competing with for the lock.  But it's more efficient/faster
140  * to just disable all interrupts on that CPU to guarantee
141  * once it gets the lock it can release it quickly too.
142  */
143  
144 void _dbg_write_lock(rwlock_t *rw, const char *bfile, int bline)
145 {
146         void *inline_pc = __builtin_return_address(0);
147         unsigned long started = jiffies;
148         long stuck = INIT_STUCK;
149         int printed = 0;
150         int cpu = smp_processor_id();
151         
152         if(unlikely(in_interrupt())) {  /* acquiring write lock in interrupt context, bad idea */
153                 printk(KERN_WARNING "write_lock caller: %s:%d, IRQs enabled,\n", bfile, bline);
154                 BUG();
155         }
156
157         /* Note: if interrupts are disabled (which is most likely), the printk
158         will never show on the console. We might need a polling method to flush
159         the dmesg buffer anyhow. */
160         
161 retry:
162         _raw_spin_lock(&rw->lock);
163
164         if(rw->counter != 0) {
165                 /* this basically never happens */
166                 _raw_spin_unlock(&rw->lock);
167                 
168                 stuck--;
169                 if ((unlikely(stuck <= 0)) && (rw->counter < 0)) {
170                         printk(KERN_WARNING
171                                 "%s:%d: write_lock stuck on writer"
172                                 " in %s at %p(%d) %ld ticks\n",
173                                 bfile, bline, current->comm, inline_pc,
174                                 cpu, jiffies - started);
175                         stuck = INIT_STUCK;
176                         printed = 1;
177                 }
178                 else if (unlikely(stuck <= 0)) {
179                         printk(KERN_WARNING
180                                 "%s:%d: write_lock stuck on reader"
181                                 " in %s at %p(%d) %ld ticks\n",
182                                 bfile, bline, current->comm, inline_pc,
183                                 cpu, jiffies - started);
184                         stuck = INIT_STUCK;
185                         printed = 1;
186                 }
187                 
188                 while(rw->counter != 0);
189
190                 goto retry;
191         }
192
193         /* got it.  now leave without unlocking */
194         rw->counter = -1; /* remember we are locked */
195
196         if (unlikely(printed)) {
197                 printk(KERN_WARNING
198                         "%s:%d: write_lock grabbed in %s at %p(%d) %ld ticks\n",
199                         bfile, bline, current->comm, inline_pc,
200                         cpu, jiffies - started);
201         }
202 }
203
204 void _dbg_read_lock(rwlock_t * rw, const char *bfile, int bline)
205 {
206 #if 0
207         void *inline_pc = __builtin_return_address(0);
208         unsigned long started = jiffies;
209         int cpu = smp_processor_id();
210 #endif
211         unsigned long flags;
212
213         local_irq_save(flags);
214         _raw_spin_lock(&rw->lock); 
215
216         rw->counter++;
217 #if 0
218         printk(KERN_WARNING
219                 "%s:%d: read_lock grabbed in %s at %p(%d) %ld ticks\n",
220                 bfile, bline, current->comm, inline_pc,
221                 cpu, jiffies - started);
222 #endif
223         _raw_spin_unlock(&rw->lock);
224         local_irq_restore(flags);
225 }
226
227 #endif /* CONFIG_DEBUG_RWLOCK */