1 #include <linux/kernel.h>
2 #include <linux/kthread.h>
3 #include <linux/spinlock.h>
4 #include <linux/sort.h>
5 #include <linux/timer.h>
6 #include <linux/time.h>
10 #include "lockcheck.h"
13 // Number of outputs possible before output stops
14 // set to zero to remove the limit
15 #define OUTPUT_LIMIT 0
17 // Number of outputs to skip before
18 // printing and counting
22 // Whether or not to print these events
24 #define PRINT_LOCK_ALLOC 0
25 #define PRINT_LOCK_FREE 0
26 #define PRINT_LOCK_LOCK 0
27 #define PRINT_LOCK_UNLOCK 0
29 // How far up the stack to track the caller
32 // 2 => caller of v3_lock..
34 #define STEP_BACK_DEPTH_FIRST 1
35 #define STEP_BACK_DEPTH_LAST 4
36 #define STEP_BACK_DEPTH (STEP_BACK_DEPTH_LAST-STEP_BACK_DEPTH_FIRST+1)
38 // show when multiple locks are held simultaneously
39 // This is the minimum number
40 #define WARN_MULTIPLE_THRESHOLD 3
42 // Detect when last lock a processor holds is released
43 // but interrupts remain off, similarly for first acquire
44 #define CHECK_IRQ_LAST_RELEASE 0
45 #define CHECK_IRQ_FIRST_ACQUIRE 1
47 // Show hottest locks every this many locks or lock_irqsaves
48 // 0 indicates this should not be shown
49 #define HOT_LOCK_INTERVAL 100000
51 // How often the long lock timer checker should run
52 // And how long a lock needs to be held before it complains
53 // set to zero to deactivate
54 #define LONG_LOCK_INTERVAL_MS 10
55 #define LONG_LOCK_HELD_MS 20
58 // Whether lockcheck should lock its own data structures during an
59 // event (alloc, dealloc, lock, unlock, etc) and the subsequent
60 // checking. If this is off, it will only lock on a lock allocation,
61 // in order to assure each lock has a distinct slot. If this is off,
62 // lockcheck may miss problems (or see additional problems) due to
63 // internal race conditions. However, if it is on, lockcheck is
64 // essentially adding a global lock acuire to each lock operation in
65 // palacios, which will perturb palacios.
71 int inuse; // nonzero if this is in use
72 void *lock; // the lock
73 void *holder; // current thread holding it
74 u32 holdingcpu; // on which cpu it acquired it
75 void *allocator[STEP_BACK_DEPTH];
77 int lockcount; // how many times it's been locked/unlocked (lock=+1, unlock=-1)
78 int irqcount; // how many times interrupts have been turned off (+1/-1)
79 void *lastlocker[STEP_BACK_DEPTH];
81 void *lastunlocker[STEP_BACK_DEPTH];
83 void *lastirqlocker[STEP_BACK_DEPTH];
85 unsigned long lastlockflags; // their flags
86 unsigned long locktime;
88 void *lastirqunlocker[STEP_BACK_DEPTH]
89 ; // who last unlocked
90 unsigned long lastunlockflags; // their flags
91 int hotlockcount; // how many times it's been locked
95 // This lock is currently used only to control
96 // allocation of entries in the global state
97 static spinlock_t mylock;
98 static lockcheck_state_t state[NUM_LOCKS];
99 static lockcheck_state_t *sorted_state[NUM_LOCKS];
103 static int globallockcount=0;
105 static struct timer_list long_lock_timer;
108 #define DEBUG_OUTPUT(fmt, args...) \
111 if (numout>=OUTPUT_SKIP) { \
112 if (OUTPUT_LIMIT==0 ||(numout-OUTPUT_SKIP)<OUTPUT_LIMIT) { \
113 DEBUG(fmt, ##args); \
115 if ((numout-OUTPUT_SKIP)==OUTPUT_LIMIT) { \
116 DEBUG("LOCKCHECK: Output limit hit - no further printouts\n"); \
122 #define DEBUG_DUMPSTACK() \
124 if (numout>=OUTPUT_SKIP) { \
125 if (OUTPUT_LIMIT==0 || (numout-OUTPUT_SKIP)<OUTPUT_LIMIT) { \
132 #define LOCK_DECL unsigned long f
133 #define LOCK() spin_lock_irqsave(&mylock,f)
134 #define UNLOCK() spin_unlock_irqrestore(&mylock,f)
135 #define LOCK_ALLOC_DECL
137 #define UNLOCK_ALLOC()
142 #define LOCK_ALLOC_DECL unsigned long f
143 #define LOCK_ALLOC() spin_lock_irqsave(&mylock,f)
144 #define UNLOCK_ALLOC() spin_unlock_irqrestore(&mylock,f)
147 static void printlock(char *prefix, lockcheck_state_t *l);
150 u32 top; // next open slot 0..
151 void *lock[LOCK_STACK_DEPTH]; // the stack
152 char irq[LOCK_STACK_DEPTH]; // locked with irqsave?
153 char irqoff[LOCK_STACK_DEPTH]; // if so, what was the flag?
156 static DEFINE_PER_CPU(lock_stack_t, lock_stack);
158 static lockcheck_state_t *get_lock_entry(void)
161 lockcheck_state_t *l;
165 for (i=0;i<NUM_LOCKS;i++) {
182 static lockcheck_state_t *find_lock_entry(void *lock)
185 lockcheck_state_t *l;
187 for (i=0;i<NUM_LOCKS;i++) {
189 if (l->inuse && l->lock == lock) {
197 static void free_lock_entry(lockcheck_state_t *l)
203 static void lock_stack_print(void)
207 lock_stack_t *mystack = &(get_cpu_var(lock_stack));
208 u32 cpu = get_cpu(); put_cpu();
210 if ((mystack->top)>0) {
211 for (i=mystack->top; i>0;i--) {
212 snprintf(buf,64,"LOCK STACK (cpu=%u, index=%u, irq=%d, irqoff=%d)",cpu, i-1, (int)(mystack->irq[i-1]), (int)(mystack->irqoff[i-1]));
213 printlock(buf,find_lock_entry(mystack->lock[i-1]));
216 put_cpu_var(lock_stack);
220 static void lock_stack_lock(void *lock, char irq, unsigned long flags)
222 lock_stack_t *mystack = &(get_cpu_var(lock_stack));
223 u32 cpu = get_cpu(); put_cpu();
225 if (mystack->top>=(LOCK_STACK_DEPTH-1)) {
226 put_cpu_var(lock_stack);
227 DEBUG_OUTPUT("LOCKCHECK: Locking lock 0x%p on cpu %u exceeds stack limit of %d\n",lock,cpu,LOCK_STACK_DEPTH);
230 int oldtop = mystack->top;
231 mystack->lock[mystack->top] = lock;
232 mystack->irq[mystack->top] = irq;
233 mystack->irqoff[mystack->top] = irqs_disabled_flags(flags);
235 put_cpu_var(lock_stack);
236 if (CHECK_IRQ_FIRST_ACQUIRE && oldtop==0 && irqs_disabled_flags(flags) ) {
237 DEBUG_OUTPUT("LOCKCHECK: First lock on lock stack of processor %d but irqs were already disabled - stack trace follows\n", cpu);
246 static void lock_stack_unlock(void *lock, char irq, unsigned long flags)
248 lock_stack_t *mystack = &(get_cpu_var(lock_stack));
249 u32 cpu = get_cpu(); put_cpu();
251 if (mystack->top==0) {
252 put_cpu_var(lock_stack);
253 DEBUG_OUTPUT("LOCKCHECK: Unlocking lock 0x%p on cpu %u when lock stack is empty\n",lock,cpu);
255 if (mystack->lock[mystack->top-1] != lock) {
256 void *otherlock=mystack->lock[mystack->top-1];
257 put_cpu_var(lock_stack);
258 DEBUG_OUTPUT("LOCKCHECK: Unlocking lock 0x%p on cpu %u when top of stack is lock 0x%p\n",lock,cpu, otherlock);
261 if (irq!=mystack->irq[mystack->top-1]) {
262 char otherirq = mystack->irq[mystack->top-1];
263 put_cpu_var(lock_stack);
264 DEBUG_OUTPUT("LOCKCHECK: Unlocking lock 0x%p on cpu %u with irq=%d, but was locked with irq=%d\n",lock,cpu,irq,otherirq);
266 mystack = &(get_cpu_var(lock_stack));
268 put_cpu_var(lock_stack);
271 if (irqs_disabled_flags(flags)!=mystack->irqoff[mystack->top-1]) {
272 char otherirqoff = mystack->irqoff[mystack->top-1];
273 put_cpu_var(lock_stack);
274 DEBUG_OUTPUT("LOCKCHECK: Unlocking lock 0x%p on cpu %u sets irqoff=%d but the matching lock returned irqoff=%d\n", lock, cpu, irqs_disabled_flags(flags), otherirqoff);
276 mystack = &(get_cpu_var(lock_stack));
278 put_cpu_var(lock_stack);
280 // irq, and irq states match - good
282 put_cpu_var(lock_stack);
285 // !irq - we are good
287 put_cpu_var(lock_stack);
293 mystack = &(get_cpu_var(lock_stack));
294 if (mystack->top == 0) {
295 put_cpu_var(lock_stack);
296 if (CHECK_IRQ_LAST_RELEASE && irqs_disabled()) {
297 DEBUG_OUTPUT("LOCKCHECK: Lock stack on cpu %u is now empty, but irqs are still disabled! Stack trace follows\n", cpu);
301 put_cpu_var(lock_stack);
307 // pointers are to the pointers in the sorted_state array
308 int compare(const void *a, const void *b)
310 lockcheck_state_t *l = *((lockcheck_state_t **)a);
311 lockcheck_state_t *r = *((lockcheck_state_t **)b);
313 return -(l->hotlockcount - r->hotlockcount);
316 static void hot_lock_show(void)
322 for (i=0;i<NUM_LOCKS;i++) {
323 if (state[i].inuse) {
324 sorted_state[n]=&(state[i]);
329 sort(sorted_state,n,sizeof(lockcheck_state_t *),compare,NULL);
332 snprintf(buf,64,"HOT LOCK (%d of %d) %d acquires", i,n,sorted_state[i]->hotlockcount);
333 printlock(buf,sorted_state[i]);
338 static void hot_lock_lock(void *lock)
340 lockcheck_state_t *l = find_lock_entry(lock);
347 if (HOT_LOCK_INTERVAL && !(globallockcount % HOT_LOCK_INTERVAL )) {
348 DEBUG_OUTPUT("LOCKCHECK: Hot locks after %d acquires Follow\n",globallockcount);
354 #define hot_lock_unlock(X) // nothing for now
357 void long_lock_callback(unsigned long ignored)
359 // See if any lock has been held for too long
360 // then print all the locks if it has
362 // Note that this function does not lock because the
363 // idea is to try to catch a deadlock, which might
364 // imply that we are stalled waiting on our lock already
368 lockcheck_state_t *l;
375 for (i=0;i<NUM_LOCKS;i++) {
377 if (l->inuse && (l->irqcount>0 || l->lockcount>0)) {
378 if (jiffies_to_msecs(now-l->locktime)>LONG_LOCK_HELD_MS) {
385 DEBUG_OUTPUT("LOCKCHECK: LONG LOCK: %d locks have been held longer than %u ms - Dump of all held locks Follows\n",have,LONG_LOCK_HELD_MS);
387 for (i=0;i<NUM_LOCKS;i++) {
389 if (l->inuse && (l->irqcount>0 || l->lockcount>0)) {
390 snprintf(buf,64,"LONG LOCK (%d - %s)",
392 jiffies_to_msecs(now-l->locktime)>LONG_LOCK_HELD_MS ? "**OLD**" : "ok");
399 // reset the timer to run again
401 mod_timer(&long_lock_timer,jiffies+msecs_to_jiffies(LONG_LOCK_INTERVAL_MS));
406 void palacios_lockcheck_init()
408 memset(state,0,sizeof(lockcheck_state_t)*NUM_LOCKS);
409 spin_lock_init(&mylock);
411 #if LONG_LOCK_INTERVAL_MS>0
412 setup_timer(&long_lock_timer,long_lock_callback,0);
413 mod_timer(&long_lock_timer, jiffies + msecs_to_jiffies(LONG_LOCK_INTERVAL_MS));
416 DEBUG_OUTPUT("LOCKCHECK: LOCK CHECKING INITED (internal locking: %d, output limit: %d, output skip: %d)\n",LOCK_SELF,OUTPUT_LIMIT,OUTPUT_SKIP);
420 // This needs to be defined explictly since the intrinsic does not take a var
422 #define backtrace(t) \
423 t[0]=__builtin_return_address(STEP_BACK_DEPTH_FIRST); \
424 t[1]=__builtin_return_address(STEP_BACK_DEPTH_FIRST+1); \
425 t[2]=__builtin_return_address(STEP_BACK_DEPTH_FIRST+2); \
426 t[3]=__builtin_return_address(STEP_BACK_DEPTH_FIRST+3);
429 // For printing a backtrace
432 #define backtrace_format "%pS << %pS << %pS << %pS"
433 #define backtrace_expand(t) ((t)[0]),((t)[1]),((t)[2]),((t)[3])
436 static void clear_trace(void **trace)
440 for (i=0;i<STEP_BACK_DEPTH;i++) {
446 static void printlock(char *prefix, lockcheck_state_t *l)
448 if (!l || !(l->lock) ) {
449 DEBUG_OUTPUT("LOCKCHECK: %s: lock 0x%p BOGUS\n",prefix,l);
453 DEBUG_OUTPUT("LOCKCHECK: %s: lock 0x%p, holder %s on %u, allocator="
455 ", lockcount=%d, lastlocker="
459 ", irqcount=%d, lastirqlocker="
461 ", lastlockflags=%lu, lastirqunlocker="
463 ", lastunlockflags=%lu"
466 l->holder ? ((struct task_struct *)(l->holder))->comm : "none",
468 backtrace_expand(l->allocator),
470 backtrace_expand(l->lastlocker),
471 backtrace_expand(l->lastunlocker),
473 backtrace_expand(l->lastirqlocker),
475 backtrace_expand(l->lastirqunlocker),
477 (l->lockcount>0) || (l->irqcount>0) ? jiffies_to_msecs(jiffies-l->locktime) : 0);
486 static void find_multiple_locks_held(void)
490 lockcheck_state_t *l;
493 for (i=0;i<NUM_LOCKS;i++) {
495 if (l->inuse && l->lockcount>0) {
497 if (have>=WARN_MULTIPLE_THRESHOLD) {
503 if (have>=WARN_MULTIPLE_THRESHOLD) {
505 for (i=0;i<NUM_LOCKS;i++) {
507 if (l->inuse && l->lockcount>0) {
508 snprintf(buf,64,"MULTIPLE LOCKS HELD (%d)",have);
517 static void find_multiple_irqs_held(void)
521 lockcheck_state_t *l;
524 for (i=0;i<NUM_LOCKS;i++) {
526 if (l->inuse && l->irqcount>0) {
528 if (have>=WARN_MULTIPLE_THRESHOLD) {
534 if (have>=WARN_MULTIPLE_THRESHOLD) {
536 for (i=0;i<NUM_LOCKS;i++) {
538 if (l->inuse && l->irqcount>0) {
539 snprintf(buf,64,"MULTIPLE IRQS HELD (%d)",have);
549 void palacios_lockcheck_deinit()
552 lockcheck_state_t *l;
557 for (i=0;i<NUM_LOCKS;i++) {
560 printlock("ALLOCATED LOCK AT DEINIT",l);
561 if ((l->lockcount)) {
562 printlock("BAD LOCK COUNT AT DEINIT",l);
565 printlock("BAD IRQ COUNT AT DEINIT",l);
569 #if LONG_LOCK_INTERVAL_MS>0
570 del_timer(&long_lock_timer);
573 INFO("LOCKCHECK: DEINITED\n");
577 void palacios_lockcheck_alloc(void *lock)
579 lockcheck_state_t *l;
587 DEBUG_OUTPUT("LOCKCHECK: UNABLE TO ALLOCATE TRACKING DATA FOR LOCK 0x%p\n",lock);
590 backtrace(l->allocator);
591 l->lockcount=l->irqcount=0;
592 clear_trace(l->lastlocker);
593 clear_trace(l->lastunlocker);
594 clear_trace(l->lastirqlocker);
595 clear_trace(l->lastirqunlocker);
596 //INFO("LOCKCHECK: LOCK ALLOCATE 0x%p\n",lock);
598 printlock("NEW LOCK", l);
604 void palacios_lockcheck_free(void *lock)
606 lockcheck_state_t *l;
610 l=find_lock_entry(lock);
614 DEBUG_OUTPUT("LOCKCHECK: FREEING UNTRACKED LOCK 0x%p - stack trace follows\n",lock);
619 if ((l->lockcount)) {
620 printlock("BAD LOCK COUNT AT FREE",l);
624 printlock("BAD IRQ COUNT AT FREE",l);
628 printlock("FREE LOCK",l);
637 void palacios_lockcheck_lock(void *lock)
640 lockcheck_state_t *l;
645 l=find_lock_entry(lock);
649 DEBUG_OUTPUT("LOCKCHECK: LOCKING UNTRACKED LOCK 0x%p - stack follows\n",lock);
654 if (l->lockcount!=0) {
655 printlock("BAD LOCKCOUNT AT LOCK - stack follows",l);
658 if (l->irqcount!=0) {
659 printlock("BAD IRQCOUNT AT LOCK - stack follows",l);
665 l->holdingcpu=get_cpu(); put_cpu();
667 backtrace(l->lastlocker);
669 find_multiple_locks_held();
671 lock_stack_lock(lock,0,0);
682 void palacios_lockcheck_unlock(void *lock)
685 lockcheck_state_t *l;
689 l=find_lock_entry(lock);
693 DEBUG_OUTPUT("LOCKCHECK: UNLOCKING UNTRACKED LOCK 0x%p - stack follows\n",lock);
698 if (l->lockcount!=1) {
699 printlock("LOCKCHECK: BAD LOCKCOUNT AT UNLOCK - stack follows",l);
702 if (l->irqcount!=0) {
703 printlock("LOCKCHECK: BAD IRQCOUNT AT UNLOCK - stack follows",l);
707 lock_stack_unlock(lock,0,0);
709 hot_lock_unlock(lock);
714 backtrace(l->lastunlocker);
716 #if PRINT_LOCK_UNLOCK
717 printlock("UNLOCK",l);
724 void palacios_lockcheck_lock_irqsave(void *lock,unsigned long flags)
727 lockcheck_state_t *l;
731 l=find_lock_entry(lock);
735 DEBUG_OUTPUT("LOCKCHECK: IRQ LOCKING UNTRACKED LOCK 0x%p - stack follows\n",lock);
740 if (l->lockcount!=0) {
741 printlock("BAD LOCKCOUNT AT IRQ LOCK - stack follows",l);
744 if (l->irqcount!=0) {
745 printlock("BAD IRQCOUNT AT IRQ LOCK - stack follows",l);
751 l->holdingcpu=get_cpu(); put_cpu();
752 l->lastlockflags=flags;
754 backtrace(l->lastirqlocker);
756 find_multiple_irqs_held();
758 lock_stack_lock(lock,1,flags);
763 printlock("LOCK_IRQSAVE",l);
773 // This is separated into two components to avoid a race between
774 // the underlying spin_unlock_irqrestore and the next lockcheck_lock_irqsave
775 // If simply record the state after the unlock, we might see that the
776 // irqcount has already increased. Therefore, we will acquire the
777 // lockchecker lock in _pre and release it in _post. Note that when
778 // we release the lock in _post, we restore the flags provided by the
779 // code under test - NOT our original flags
781 // unlock_pre() - stores flags, takes mylock discard flags
782 // At this point, a lockcheck_lock cannot enter, since it's stuck on mylock
783 // spinunlock - restores lock, restores original flags
784 // unlock_post() - restores mylock WITH orig flags
786 void palacios_lockcheck_unlock_irqrestore_pre(void *lock,unsigned long flags)
790 LOCK(); // flags are discarded
791 // at this point, the actual spin unlock can run
792 // if another thread hits lockcheck_irqsave at this point, it
793 // will block on mylock
796 void palacios_lockcheck_unlock_irqrestore_post(void *lock,unsigned long flags)
799 lockcheck_state_t *l;
802 // when we unlock, want to restore the flags *the user wants*
806 // Note that we DO NOT take mylock here, since we already took it in
809 l=find_lock_entry(lock);
812 UNLOCK(); // release any waiting threads on lockcheck_lock_irqsave
813 DEBUG_OUTPUT("LOCKCHECK: IRQ UNLOCKING UNTRACKED LOCK 0x%p - stack follows\n",lock);
818 if (l->lockcount!=0) {
819 printlock("LOCKCHECK: BAD LOCKCOUNT AT IRQ UNLOCK - stack follows",l);
822 if (l->irqcount!=1) {
823 printlock("LOCKCHECK: BAD IRQCOUNT AT IRQ UNLOCK - stack follows",l);
830 l->lastunlockflags = flags;
832 lock_stack_unlock(lock,1,flags);
834 hot_lock_unlock(lock);
836 backtrace(l->lastirqunlocker);
838 #if PRINT_LOCK_UNLOCK
839 printlock("UNLOCK_IRQRESTORE",l);
841 UNLOCK(); // release any waiting threads on lockcheck_lock_irqsave