| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- /*
- * Copyright 2005, Red Hat, Inc., Ingo Molnar
- * Released under the General Public License (GPL).
- *
- * This file contains the spinlock/rwlock implementations for
- * DEBUG_SPINLOCK.
- */
- #define pr_fmt(fmt) "spinlock_debug: " fmt
- #include <linux/spinlock.h>
- #include <linux/nmi.h>
- #include <linux/interrupt.h>
- #include <linux/debug_locks.h>
- #include <linux/delay.h>
- #include <linux/export.h>
- #include <linux/kernel.h>
- #include <linux/printk.h>
- #include <mt-plat/aee.h>
- #ifdef CONFIG_MTPROF
- #include "mt_sched_mon.h"
- #endif
- void __raw_spin_lock_init(raw_spinlock_t *lock, const char *name,
- struct lock_class_key *key)
- {
- #ifdef CONFIG_DEBUG_LOCK_ALLOC
- /*
- * Make sure we are not reinitializing a held lock:
- */
- debug_check_no_locks_freed((void *)lock, sizeof(*lock));
- lockdep_init_map(&lock->dep_map, name, key, 0);
- #endif
- lock->raw_lock = (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
- lock->magic = SPINLOCK_MAGIC;
- lock->owner = SPINLOCK_OWNER_INIT;
- lock->owner_cpu = -1;
- }
- EXPORT_SYMBOL(__raw_spin_lock_init);
- void __rwlock_init(rwlock_t *lock, const char *name,
- struct lock_class_key *key)
- {
- #ifdef CONFIG_DEBUG_LOCK_ALLOC
- /*
- * Make sure we are not reinitializing a held lock:
- */
- debug_check_no_locks_freed((void *)lock, sizeof(*lock));
- lockdep_init_map(&lock->dep_map, name, key, 0);
- #endif
- lock->raw_lock = (arch_rwlock_t) __ARCH_RW_LOCK_UNLOCKED;
- lock->magic = RWLOCK_MAGIC;
- lock->owner = SPINLOCK_OWNER_INIT;
- lock->owner_cpu = -1;
- }
- EXPORT_SYMBOL(__rwlock_init);
- static void spin_dump(raw_spinlock_t *lock, const char *msg)
- {
- struct task_struct *owner = NULL;
- if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
- owner = lock->owner;
- printk(KERN_EMERG "BUG: spinlock %s on CPU#%d, %s/%d\n",
- msg, raw_smp_processor_id(),
- current->comm, task_pid_nr(current));
- pr_emerg(" lock: %pS, .magic: %08x, .owner: %s/%d, .owner_cpu: %d value:0x%08x\n",
- lock, lock->magic, owner ? owner->comm : "<none>", owner ? task_pid_nr(owner) : -1,
- lock->owner_cpu, *((unsigned int *)&lock->raw_lock));
- dump_stack();
- }
- static void spin_bug(raw_spinlock_t *lock, const char *msg)
- {
- char aee_str[50];
- /*
- if (!debug_locks_off())
- return;
- */
- spin_dump(lock, msg);
- snprintf(aee_str, 50, "Spinlock %s :%s\n", current->comm, msg);
- if ((!strcmp(msg, "bad magic")) || (!strcmp(msg, "already unlocked"))
- || (!strcmp(msg, "wrong owner")) || (!strcmp(msg, "wrong CPU"))) {
- pr_emerg("%s\n", aee_str);
- pr_emerg("[spindebug] maybe use an un-initial spin_lock or mem corrupt\n");
- pr_emerg("[spindebug] maybe already unlocked or wrong owner or wrong CPU\n");
- pr_emerg("[spindebug] maybe bad magic:%08x, should be %08x\n", lock->magic, SPINLOCK_MAGIC);
- pr_emerg(">>>>>>>>>>>>>> Let's KE <<<<<<<<<<<<<<\n");
- BUG_ON(1);
- }
- aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DUMMY_DUMP | DB_OPT_FTRACE,
- aee_str, "spinlock debugger\n");
- }
- #define SPIN_BUG_ON(cond, lock, msg) if (unlikely(cond)) spin_bug(lock, msg)
- static inline void
- debug_spin_lock_before(raw_spinlock_t *lock)
- {
- SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
- SPIN_BUG_ON(lock->owner == current, lock, "recursion");
- SPIN_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
- lock, "cpu recursion");
- }
- static inline void debug_spin_lock_after(raw_spinlock_t *lock)
- {
- lock->owner_cpu = raw_smp_processor_id();
- lock->owner = current;
- }
- static inline void debug_spin_unlock(raw_spinlock_t *lock)
- {
- SPIN_BUG_ON(lock->magic != SPINLOCK_MAGIC, lock, "bad magic");
- SPIN_BUG_ON(!raw_spin_is_locked(lock), lock, "already unlocked");
- SPIN_BUG_ON(lock->owner != current, lock, "wrong owner");
- SPIN_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
- lock, "wrong CPU");
- lock->owner = SPINLOCK_OWNER_INIT;
- lock->owner_cpu = -1;
- }
- /*
- Select appropriate loop counts to 1~2sec
- */
- #if HZ == 100
- #define LOOP_HZ 100 /* temp 10 */
- #elif HZ == 10
- #define LOOP_HZ 2 /* temp 2 */
- #else
- #define LOOP_HZ HZ
- #endif
- #define WARNING_TIME 1000000000 /* warning time 1 seconds */
- static void __spin_lock_debug(raw_spinlock_t *lock)
- {
- #ifdef CONFIG_MTK_LOCK_DEBUG
- u64 i;
- u64 loops = loops_per_jiffy * LOOP_HZ;
- int print_once = 1;
- char aee_str[50];
- unsigned long long t1, t2, t3;
- struct task_struct *owner = NULL;
- #ifdef CONFIG_MTPROF
- MT_trace_raw_spin_lock_s(lock);
- #endif
- t1 = sched_clock();
- t2 = t1;
- for (;;) {
- for (i = 0; i < loops; i++) {
- if (arch_spin_trylock(&lock->raw_lock)) {
- #ifdef CONFIG_MTPROF
- MT_trace_raw_spin_lock_e(lock);
- #endif
- return;
- }
- __delay(1);
- }
- t3 = sched_clock();
- if (t3 < t2)
- continue;
- else if (t3 - t2 < WARNING_TIME)
- continue;
- /* if(sched_clock() - t2 < WARNING_TIME) continue; */
- t2 = sched_clock();
- if (oops_in_progress != 0)
- continue; /* in exception follow, printk maybe spinlock error */
- if (is_logbuf_lock(lock)) /*block by logbuf lock */
- continue;
- /* lockup suspected: */
- if (lock->owner && lock->owner != SPINLOCK_OWNER_INIT)
- owner = lock->owner;
- pr_emerg("spin time: %llu ns(start:%llu ns, lpj:%lu, LPHZ:%d), value: 0x%08x\n",
- sched_clock() - t1, t1, loops_per_jiffy, (int)LOOP_HZ,
- *((unsigned int *)&lock->raw_lock));
- pr_emerg("spinlock .owner: %s/%d, .owner_cpu: %d\n",
- owner ? owner->comm : "<none>",
- owner ? task_pid_nr(owner) : -1,
- lock->owner_cpu);
- if (print_once) {
- print_once = 0;
- spin_dump(lock, "lockup suspected");
- #ifdef CONFIG_SMP
- trigger_all_cpu_backtrace();
- #endif
- /* ensure debug_locks is true,then can call aee */
- if (debug_locks) {
- debug_show_all_locks();
- snprintf(aee_str, 50, "Spinlock lockup:%s\n", current->comm);
- aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_DUMMY_DUMP | DB_OPT_FTRACE,
- aee_str, "spinlock debugger\n");
- }
- }
- }
- #else /* CONFIG_MTK_LOCK_DEBUG*/
- u64 i;
- u64 loops = loops_per_jiffy * HZ;
- for (i = 0; i < loops; i++) {
- if (arch_spin_trylock(&lock->raw_lock))
- return;
- __delay(1);
- }
- /* lockup suspected: */
- spin_dump(lock, "lockup suspected");
- #ifdef CONFIG_SMP
- trigger_all_cpu_backtrace();
- #endif
- /*
- * The trylock above was causing a livelock. Give the lower level arch
- * specific lock code a chance to acquire the lock. We have already
- * printed a warning/backtrace at this point. The non-debug arch
- * specific code might actually succeed in acquiring the lock. If it is
- * not successful, the end-result is the same - there is no forward
- * progress.
- */
- arch_spin_lock(&lock->raw_lock);
- #endif /* CONFIG_MTK_LOCK_DEBUG */
- }
- void do_raw_spin_lock(raw_spinlock_t *lock)
- {
- debug_spin_lock_before(lock);
- if (unlikely(!arch_spin_trylock(&lock->raw_lock)))
- __spin_lock_debug(lock);
- debug_spin_lock_after(lock);
- }
- int do_raw_spin_trylock(raw_spinlock_t *lock)
- {
- int ret = arch_spin_trylock(&lock->raw_lock);
- if (ret)
- debug_spin_lock_after(lock);
- #ifndef CONFIG_SMP
- /*
- * Must not happen on UP:
- */
- SPIN_BUG_ON(!ret, lock, "trylock failure on UP");
- #endif
- return ret;
- }
- void do_raw_spin_unlock(raw_spinlock_t *lock)
- {
- debug_spin_unlock(lock);
- arch_spin_unlock(&lock->raw_lock);
- }
- static void rwlock_bug(rwlock_t *lock, const char *msg)
- {
- if (!debug_locks_off())
- return;
- printk(KERN_EMERG "BUG: rwlock %s on CPU#%d, %s/%d, %p\n",
- msg, raw_smp_processor_id(), current->comm,
- task_pid_nr(current), lock);
- dump_stack();
- }
- #define RWLOCK_BUG_ON(cond, lock, msg) if (unlikely(cond)) rwlock_bug(lock, msg)
- #if 0 /* __write_lock_debug() can lock up - maybe this can too? */
- static void __read_lock_debug(rwlock_t *lock)
- {
- u64 i;
- u64 loops = loops_per_jiffy * HZ;
- int print_once = 1;
- for (;;) {
- for (i = 0; i < loops; i++) {
- if (arch_read_trylock(&lock->raw_lock))
- return;
- __delay(1);
- }
- /* lockup suspected: */
- if (print_once) {
- print_once = 0;
- printk(KERN_EMERG "BUG: read-lock lockup on CPU#%d, "
- "%s/%d, %p\n",
- raw_smp_processor_id(), current->comm,
- current->pid, lock);
- dump_stack();
- }
- }
- }
- #endif
- void do_raw_read_lock(rwlock_t *lock)
- {
- RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
- arch_read_lock(&lock->raw_lock);
- }
- int do_raw_read_trylock(rwlock_t *lock)
- {
- int ret = arch_read_trylock(&lock->raw_lock);
- #ifndef CONFIG_SMP
- /*
- * Must not happen on UP:
- */
- RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
- #endif
- return ret;
- }
- void do_raw_read_unlock(rwlock_t *lock)
- {
- RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
- arch_read_unlock(&lock->raw_lock);
- }
- static inline void debug_write_lock_before(rwlock_t *lock)
- {
- RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
- RWLOCK_BUG_ON(lock->owner == current, lock, "recursion");
- RWLOCK_BUG_ON(lock->owner_cpu == raw_smp_processor_id(),
- lock, "cpu recursion");
- }
- static inline void debug_write_lock_after(rwlock_t *lock)
- {
- lock->owner_cpu = raw_smp_processor_id();
- lock->owner = current;
- }
- static inline void debug_write_unlock(rwlock_t *lock)
- {
- RWLOCK_BUG_ON(lock->magic != RWLOCK_MAGIC, lock, "bad magic");
- RWLOCK_BUG_ON(lock->owner != current, lock, "wrong owner");
- RWLOCK_BUG_ON(lock->owner_cpu != raw_smp_processor_id(),
- lock, "wrong CPU");
- lock->owner = SPINLOCK_OWNER_INIT;
- lock->owner_cpu = -1;
- }
- #if 0 /* This can cause lockups */
- static void __write_lock_debug(rwlock_t *lock)
- {
- u64 i;
- u64 loops = loops_per_jiffy * HZ;
- int print_once = 1;
- for (;;) {
- for (i = 0; i < loops; i++) {
- if (arch_write_trylock(&lock->raw_lock))
- return;
- __delay(1);
- }
- /* lockup suspected: */
- if (print_once) {
- print_once = 0;
- printk(KERN_EMERG "BUG: write-lock lockup on CPU#%d, "
- "%s/%d, %p\n",
- raw_smp_processor_id(), current->comm,
- current->pid, lock);
- dump_stack();
- }
- }
- }
- #endif
- void do_raw_write_lock(rwlock_t *lock)
- {
- debug_write_lock_before(lock);
- arch_write_lock(&lock->raw_lock);
- debug_write_lock_after(lock);
- }
- int do_raw_write_trylock(rwlock_t *lock)
- {
- int ret = arch_write_trylock(&lock->raw_lock);
- if (ret)
- debug_write_lock_after(lock);
- #ifndef CONFIG_SMP
- /*
- * Must not happen on UP:
- */
- RWLOCK_BUG_ON(!ret, lock, "trylock failure on UP");
- #endif
- return ret;
- }
- void do_raw_write_unlock(rwlock_t *lock)
- {
- debug_write_unlock(lock);
- arch_write_unlock(&lock->raw_lock);
- }
|