diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2010-04-26 05:48:20 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2010-04-26 05:48:20 +0000 |
commit | 36e448944b0b2563eedabe7250a2155262915c8d (patch) | |
tree | 7aad0603dde8309761d3ab1f0e1a1a311ade474a | |
parent | c108a8fdc871c4428549ee6862f22b56f2eddc21 (diff) |
cut down simple locks (so simple that they don't even lock) to the point
where there is almost nothing left to them, so that we can continue getting
rid of them
ok oga
-rw-r--r-- | sys/arch/m68k/include/lock.h | 4 | ||||
-rw-r--r-- | sys/arch/sh/include/lock.h | 4 | ||||
-rw-r--r-- | sys/kern/kern_lock.c | 422 | ||||
-rw-r--r-- | sys/sys/lock.h | 64 | ||||
-rw-r--r-- | sys/uvm/uvm_map.c | 6 |
5 files changed, 22 insertions, 478 deletions
diff --git a/sys/arch/m68k/include/lock.h b/sys/arch/m68k/include/lock.h index 475236a26d4..2e7e04dce73 100644 --- a/sys/arch/m68k/include/lock.h +++ b/sys/arch/m68k/include/lock.h @@ -1,10 +1,12 @@ -/* $OpenBSD: lock.h,v 1.1 2007/05/01 18:56:30 miod Exp $ */ +/* $OpenBSD: lock.h,v 1.2 2010/04/26 05:48:19 deraadt Exp $ */ /* public domain */ #ifndef _M68K_LOCK_H_ #define _M68K_LOCK_H_ +#include <machine/atomic.h> + typedef volatile u_int __cpu_simple_lock_t; #define __SIMPLELOCK_LOCKED 1 diff --git a/sys/arch/sh/include/lock.h b/sys/arch/sh/include/lock.h index 10d433c8e8e..e228fabc292 100644 --- a/sys/arch/sh/include/lock.h +++ b/sys/arch/sh/include/lock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: lock.h,v 1.2 2008/06/26 05:42:12 ray Exp $ */ +/* $OpenBSD: lock.h,v 1.3 2010/04/26 05:48:19 deraadt Exp $ */ /* $NetBSD: lock.h,v 1.10 2006/01/03 01:29:46 uwe Exp $ */ /*- @@ -37,6 +37,8 @@ #ifndef _SH_LOCK_H_ #define _SH_LOCK_H_ +#include <sh/atomic.h> + typedef volatile u_int8_t __cpu_simple_lock_t; #define __SIMPLELOCK_LOCKED 0x80 diff --git a/sys/kern/kern_lock.c b/sys/kern/kern_lock.c index eaa427e5d27..2e4bd1f8ac7 100644 --- a/sys/kern/kern_lock.c +++ b/sys/kern/kern_lock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_lock.c,v 1.34 2010/01/14 23:12:11 schwarze Exp $ */ +/* $OpenBSD: kern_lock.c,v 1.35 2010/04/26 05:48:17 deraadt Exp $ */ /* * Copyright (c) 1995 @@ -43,39 +43,11 @@ #include <machine/cpu.h> -#ifndef spllock -#define spllock() splhigh() -#endif - -#ifdef MULTIPROCESSOR -#define CPU_NUMBER() cpu_number() -#else -#define CPU_NUMBER() 0 -#endif - -void record_stacktrace(int *, int); -void playback_stacktrace(int *, int); - /* * Locking primitives implementation. * Locks provide shared/exclusive synchronization. */ -#ifdef DDB /* { */ -#ifdef MULTIPROCESSOR -int simple_lock_debugger = 1; /* more serious on MP */ -#else -int simple_lock_debugger = 0; -#endif -#define SLOCK_DEBUGGER() if (simple_lock_debugger) Debugger() -#define SLOCK_TRACE() \ - db_stack_trace_print((db_expr_t)__builtin_frame_address(0), \ - TRUE, 65535, "", lock_printf); -#else -#define SLOCK_DEBUGGER() /* nothing */ -#define SLOCK_TRACE() /* nothing */ -#endif /* } */ - /* * Acquire a resource. */ @@ -103,44 +75,6 @@ do { \ #define WEHOLDIT(lkp, pid, cpu_id) \ ((lkp)->lk_lockholder == (pid)) -#define WAKEUP_WAITER(lkp) \ -do { \ - if ((lkp)->lk_waitcount) { \ - /* XXX Cast away volatile. */ \ - wakeup((void *)(lkp)); \ - } \ -} while (/*CONSTCOND*/0) - -#define HAVEIT(lkp) \ -do { \ -} while (/*CONSTCOND*/0) - -#define DONTHAVEIT(lkp) \ -do { \ -} while (/*CONSTCOND*/0) - -#if defined(LOCKDEBUG) -/* - * Lock debug printing routine; can be configured to print to console - * or log to syslog. - */ -void -lock_printf(const char *fmt, ...) -{ - char b[150]; - va_list ap; - - va_start(ap, fmt); - if (lock_debug_syslog) - vlog(LOG_DEBUG, fmt, ap); - else { - vsnprintf(b, sizeof(b), fmt, ap); - printf_nolog("%s", b); - } - va_end(ap); -} -#endif /* LOCKDEBUG */ - /* * Initialize a lock; required before use. */ @@ -154,10 +88,6 @@ lockinit(struct lock *lkp, int prio, char *wmesg, int timo, int flags) lkp->lk_prio = prio; lkp->lk_timo = timo; lkp->lk_wmesg = wmesg; /* just a name for spin locks */ -#if defined(LOCKDEBUG) - lkp->lk_lock_file = NULL; - lkp->lk_unlock_file = NULL; -#endif } /* @@ -183,7 +113,7 @@ lockstatus(struct lock *lkp) * accepted shared locks and shared-to-exclusive upgrades to go away. */ int -lockmgr(__volatile struct lock *lkp, u_int flags, struct simplelock *interlkp) +lockmgr(__volatile struct lock *lkp, u_int flags, void *notused) { int error; pid_t pid; @@ -200,7 +130,7 @@ lockmgr(__volatile struct lock *lkp, u_int flags, struct simplelock *interlkp) #endif /* Process context required. */ pid = p->p_pid; - cpu_id = CPU_NUMBER(); + cpu_id = cpu_number(); /* * Once a lock has drained, the LK_DRAINING flag is set and an @@ -266,12 +196,8 @@ lockmgr(__volatile struct lock *lkp, u_int flags, struct simplelock *interlkp) lkp->lk_exclusivecount = 0; lkp->lk_flags &= ~LK_HAVE_EXCL; SETHOLDER(lkp, LK_NOPROC, LK_NOCPU); -#if defined(LOCKDEBUG) - lkp->lk_unlock_file = file; - lkp->lk_unlock_line = line; -#endif - DONTHAVEIT(lkp); - WAKEUP_WAITER(lkp); + if (lkp->lk_waitcount) + wakeup((void *)(lkp)); break; case LK_EXCLUSIVE: @@ -315,11 +241,6 @@ lockmgr(__volatile struct lock *lkp, u_int flags, struct simplelock *interlkp) break; lkp->lk_flags |= LK_HAVE_EXCL; SETHOLDER(lkp, pid, cpu_id); -#if defined(LOCKDEBUG) - lkp->lk_lock_file = file; - lkp->lk_lock_line = line; -#endif - HAVEIT(lkp); if (lkp->lk_exclusivecount != 0) panic("lockmgr: non-zero exclusive count"); lkp->lk_exclusivecount = 1; @@ -336,11 +257,6 @@ lockmgr(__volatile struct lock *lkp, u_int flags, struct simplelock *interlkp) if (lkp->lk_exclusivecount == 0) { lkp->lk_flags &= ~LK_HAVE_EXCL; SETHOLDER(lkp, LK_NOPROC, LK_NOCPU); -#if defined(LOCKDEBUG) - lkp->lk_unlock_file = file; - lkp->lk_unlock_line = line; -#endif - DONTHAVEIT(lkp); } } else if (lkp->lk_sharecount != 0) { lkp->lk_sharecount--; @@ -349,7 +265,8 @@ lockmgr(__volatile struct lock *lkp, u_int flags, struct simplelock *interlkp) else panic("lockmgr: release of unlocked lock!"); #endif - WAKEUP_WAITER(lkp); + if (lkp->lk_waitcount) + wakeup((void *)(lkp)); break; case LK_DRAIN: @@ -379,11 +296,6 @@ lockmgr(__volatile struct lock *lkp, u_int flags, struct simplelock *interlkp) break; lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL; SETHOLDER(lkp, pid, cpu_id); -#if defined(LOCKDEBUG) - lkp->lk_lock_file = file; - lkp->lk_lock_line = line; -#endif - HAVEIT(lkp); lkp->lk_exclusivecount = 1; break; @@ -425,326 +337,6 @@ lockmgr_printinfo(__volatile struct lock *lkp) } #endif /* DIAGNOSTIC */ -#if defined(LOCKDEBUG) -TAILQ_HEAD(, simplelock) simplelock_list = - TAILQ_HEAD_INITIALIZER(simplelock_list); - -#if defined(MULTIPROCESSOR) /* { */ -struct simplelock simplelock_list_slock = SIMPLELOCK_INITIALIZER; - -#define SLOCK_LIST_LOCK() \ - __cpu_simple_lock(&simplelock_list_slock.lock_data) - -#define SLOCK_LIST_UNLOCK() \ - __cpu_simple_unlock(&simplelock_list_slock.lock_data) - -#define SLOCK_COUNT(x) \ - curcpu()->ci_simple_locks += (x) -#else -u_long simple_locks; - -#define SLOCK_LIST_LOCK() /* nothing */ - -#define SLOCK_LIST_UNLOCK() /* nothing */ - -#define SLOCK_COUNT(x) simple_locks += (x) -#endif /* MULTIPROCESSOR */ /* } */ - -#ifdef MULTIPROCESSOR -#define SLOCK_MP() lock_printf("on cpu %ld\n", \ - (u_long) cpu_number()) -#else -#define SLOCK_MP() /* nothing */ -#endif - -#define SLOCK_WHERE(str, alp, id, l) \ -do { \ - lock_printf("\n"); \ - lock_printf(str); \ - lock_printf("lock: %p, currently at: %s:%d\n", (alp), (id), (l)); \ - SLOCK_MP(); \ - if ((alp)->lock_file != NULL) \ - lock_printf("last locked: %s:%d\n", (alp)->lock_file, \ - (alp)->lock_line); \ - if ((alp)->unlock_file != NULL) \ - lock_printf("last unlocked: %s:%d\n", (alp)->unlock_file, \ - (alp)->unlock_line); \ - SLOCK_TRACE() \ - SLOCK_DEBUGGER(); \ -} while (/*CONSTCOND*/0) - -/* - * Simple lock functions so that the debugger can see from whence - * they are being called. - */ -void -simple_lock_init(struct simplelock *lkp) -{ - -#if defined(MULTIPROCESSOR) /* { */ - __cpu_simple_lock_init(&alp->lock_data); -#else - alp->lock_data = __SIMPLELOCK_UNLOCKED; -#endif /* } */ - alp->lock_file = NULL; - alp->lock_line = 0; - alp->unlock_file = NULL; - alp->unlock_line = 0; - alp->lock_holder = LK_NOCPU; -} - -void -_simple_lock(__volatile struct simplelock *lkp, const char *id, int l) -{ - cpuid_t cpu_id = CPU_NUMBER(); - int s; - - s = spllock(); - - /* - * MULTIPROCESSOR case: This is `safe' since if it's not us, we - * don't take any action, and just fall into the normal spin case. - */ - if (alp->lock_data == __SIMPLELOCK_LOCKED) { -#if defined(MULTIPROCESSOR) /* { */ - if (alp->lock_holder == cpu_id) { - SLOCK_WHERE("simple_lock: locking against myself\n", - alp, id, l); - goto out; - } -#else - SLOCK_WHERE("simple_lock: lock held\n", alp, id, l); - goto out; -#endif /* MULTIPROCESSOR */ /* } */ - } - -#if defined(MULTIPROCESSOR) /* { */ - /* Acquire the lock before modifying any fields. */ - splx(s); - __cpu_simple_lock(&alp->lock_data); - s = spllock(); -#else - alp->lock_data = __SIMPLELOCK_LOCKED; -#endif /* } */ - - if (alp->lock_holder != LK_NOCPU) { - SLOCK_WHERE("simple_lock: uninitialized lock\n", - alp, id, l); - } - alp->lock_file = id; - alp->lock_line = l; - alp->lock_holder = cpu_id; - - SLOCK_LIST_LOCK(); - /* XXX Cast away volatile */ - TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list); - SLOCK_LIST_UNLOCK(); - - SLOCK_COUNT(1); - - out: - splx(s); -} - -int -_simple_lock_held(__volatile struct simplelock *alp) -{ - cpuid_t cpu_id = CPU_NUMBER(); - int s, locked = 0; - - s = spllock(); - -#if defined(MULTIPROCESSOR) - if (__cpu_simple_lock_try(&alp->lock_data) == 0) - locked = (alp->lock_holder == cpu_id); - else - __cpu_simple_unlock(&alp->lock_data); -#else - if (alp->lock_data == __SIMPLELOCK_LOCKED) { - locked = 1; - KASSERT(alp->lock_holder == cpu_id); - } -#endif - - splx(s); - - return (locked); -} - -int -_simple_lock_try(__volatile struct simplelock *lkp, const char *id, int l) -{ - cpuid_t cpu_id = CPU_NUMBER(); - int s, rv = 0; - - s = spllock(); - - /* - * MULTIPROCESSOR case: This is `safe' since if it's not us, we - * don't take any action. - */ -#if defined(MULTIPROCESSOR) /* { */ - if ((rv = __cpu_simple_lock_try(&alp->lock_data)) == 0) { - if (alp->lock_holder == cpu_id) - SLOCK_WHERE("simple_lock_try: locking against myself\n", - alp, id, l); - goto out; - } -#else - if (alp->lock_data == __SIMPLELOCK_LOCKED) { - SLOCK_WHERE("simple_lock_try: lock held\n", alp, id, l); - goto out; - } - alp->lock_data = __SIMPLELOCK_LOCKED; -#endif /* MULTIPROCESSOR */ /* } */ - - /* - * At this point, we have acquired the lock. - */ - - rv = 1; - - alp->lock_file = id; - alp->lock_line = l; - alp->lock_holder = cpu_id; - - SLOCK_LIST_LOCK(); - /* XXX Cast away volatile. */ - TAILQ_INSERT_TAIL(&simplelock_list, (struct simplelock *)alp, list); - SLOCK_LIST_UNLOCK(); - - SLOCK_COUNT(1); - - out: - splx(s); - return (rv); -} - -void -_simple_unlock(__volatile struct simplelock *lkp, const char *id, int l) -{ - int s; - - s = spllock(); - - /* - * MULTIPROCESSOR case: This is `safe' because we think we hold - * the lock, and if we don't, we don't take any action. - */ - if (alp->lock_data == __SIMPLELOCK_UNLOCKED) { - SLOCK_WHERE("simple_unlock: lock not held\n", - alp, id, l); - goto out; - } - - SLOCK_LIST_LOCK(); - TAILQ_REMOVE(&simplelock_list, alp, list); - SLOCK_LIST_UNLOCK(); - - SLOCK_COUNT(-1); - - alp->list.tqe_next = NULL; /* sanity */ - alp->list.tqe_prev = NULL; /* sanity */ - - alp->unlock_file = id; - alp->unlock_line = l; - -#if defined(MULTIPROCESSOR) /* { */ - alp->lock_holder = LK_NOCPU; - /* Now that we've modified all fields, release the lock. */ - __cpu_simple_unlock(&alp->lock_data); -#else - alp->lock_data = __SIMPLELOCK_UNLOCKED; - KASSERT(alp->lock_holder == CPU_NUMBER()); - alp->lock_holder = LK_NOCPU; -#endif /* } */ - - out: - splx(s); -} - -void -simple_lock_dump(void) -{ - struct simplelock *alp; - int s; - - s = spllock(); - SLOCK_LIST_LOCK(); - lock_printf("all simple locks:\n"); - TAILQ_FOREACH(alp, &simplelock_list, list) { - lock_printf("%p CPU %lu %s:%d\n", alp, alp->lock_holder, - alp->lock_file, alp->lock_line); - } - SLOCK_LIST_UNLOCK(); - splx(s); -} - -void -simple_lock_freecheck(void *start, void *end) -{ - struct simplelock *alp; - int s; - - s = spllock(); - SLOCK_LIST_LOCK(); - TAILQ_FOREACH(alp, &simplelock_list, list) { - if ((void *)alp >= start && (void *)alp < end) { - lock_printf("freeing simple_lock %p CPU %lu %s:%d\n", - alp, alp->lock_holder, alp->lock_file, - alp->lock_line); - SLOCK_DEBUGGER(); - } - } - SLOCK_LIST_UNLOCK(); - splx(s); - } - -/* - * We must be holding exactly one lock: the sched_lock. - */ - -#ifdef notyet -void -simple_lock_switchcheck(void) -{ - - simple_lock_only_held(&sched_lock, "switching"); -} -#endif - -void -simple_lock_only_held(volatile struct simplelock *lp, const char *where) -{ - struct simplelock *alp; - cpuid_t cpu_id = CPU_NUMBER(); - int s; - - if (lp) { - LOCK_ASSERT(simple_lock_held(lp)); - } - s = spllock(); - SLOCK_LIST_LOCK(); - TAILQ_FOREACH(alp, &simplelock_list, list) { - if (alp == lp) - continue; - if (alp->lock_holder == cpu_id) - break; - } - SLOCK_LIST_UNLOCK(); - splx(s); - - if (alp != NULL) { - lock_printf("\n%s with held simple_lock %p " - "CPU %lu %s:%d\n", - where, alp, alp->lock_holder, alp->lock_file, - alp->lock_line); - SLOCK_TRACE(); - SLOCK_DEBUGGER(); - } -} -#endif /* LOCKDEBUG */ - #if defined(MULTIPROCESSOR) /* * Functions for manipulating the kernel_lock. We put them here diff --git a/sys/sys/lock.h b/sys/sys/lock.h index 881e9b93daa..0e943869111 100644 --- a/sys/sys/lock.h +++ b/sys/sys/lock.h @@ -1,4 +1,4 @@ -/* $OpenBSD: lock.h,v 1.20 2010/04/23 21:34:40 deraadt Exp $ */ +/* $OpenBSD: lock.h,v 1.21 2010/04/26 05:48:19 deraadt Exp $ */ /* * Copyright (c) 1995 @@ -40,30 +40,15 @@ #ifdef _KERNEL #include <machine/lock.h> -#endif /* _KERNEL */ +#endif -/* - * A simple spin lock. - * - * This structure only sets one bit of data, but is sized based on the - * minimum word size that can be operated on by the hardware test-and-set - * instruction. It is only needed for multiprocessors, as uniprocessors - * will always run to completion or a sleep. It is an error to hold one - * of these locks while a process is sleeping. - */ struct simplelock { -#ifdef MULTIPROCESSOR - __cpu_simple_lock_t lock_data; -#else - int lock_data; -#endif }; -#ifdef _KERNEL - -#define SLOCK_LOCKED 1 -#define SLOCK_UNLOCKED 0 +typedef struct simplelock simple_lock_data_t; +typedef struct simplelock *simple_lock_t; +#ifdef _KERNEL #define simple_lock(lkp) #define simple_lock_try(lkp) (1) /* always succeeds */ #define simple_unlock(lkp) @@ -71,14 +56,10 @@ struct simplelock { static __inline void simple_lock_init(struct simplelock *lkp) { - - lkp->lock_data = SLOCK_UNLOCKED; } #endif /* _KERNEL */ -typedef struct simplelock simple_lock_data_t; -typedef struct simplelock *simple_lock_t; typedef struct lock lock_data_t; typedef struct lock *lock_t; @@ -107,13 +88,6 @@ struct lock { /* maximum sleep time (for tsleep) */ int lk_timo; - -#if defined(LOCKDEBUG) - const char *lk_lock_file; - const char *lk_unlock_file; - int lk_lock_line; - int lk_unlock_line; -#endif }; /* @@ -187,41 +161,15 @@ struct lock { #define LK_NOPROC ((pid_t) -1) #define LK_NOCPU ((cpuid_t) -1) -struct proc; - void lockinit(struct lock *, int prio, char *wmesg, int timo, int flags); -int lockmgr(__volatile struct lock *, u_int flags, struct simplelock *); +int lockmgr(__volatile struct lock *, u_int flags, void *); void lockmgr_printinfo(__volatile struct lock *); int lockstatus(struct lock *); -#if defined(LOCKDEBUG) -int _spinlock_release_all(__volatile struct lock *, const char *, int); -void _spinlock_acquire_count(__volatile struct lock *, int, const char *, - int); - -#define spinlock_release_all(l) _spinlock_release_all((l), __FILE__, __LINE__) -#define spinlock_acquire_count(l, c) _spinlock_acquire_count((l), (c), \ - __FILE__, __LINE__) -#else int spinlock_release_all(__volatile struct lock *); void spinlock_acquire_count(__volatile struct lock *, int); -#endif -#ifdef LOCKDEBUG -#define LOCK_ASSERT(x) KASSERT(x) -#else #define LOCK_ASSERT(x) /* nothing */ -#endif - -#if !defined(MULTIPROCESSOR) -/* - * XXX Simplelock macros used at "trusted" places. - */ -#define SIMPLELOCK simplelock -#define SIMPLE_LOCK_INIT simple_lock_init -#define SIMPLE_LOCK simple_lock -#define SIMPLE_UNLOCK simple_unlock -#endif #endif /* !_LOCK_H_ */ diff --git a/sys/uvm/uvm_map.c b/sys/uvm/uvm_map.c index df56d861d11..aa6327fe5a6 100644 --- a/sys/uvm/uvm_map.c +++ b/sys/uvm/uvm_map.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uvm_map.c,v 1.125 2010/04/23 04:49:46 tedu Exp $ */ +/* $OpenBSD: uvm_map.c,v 1.126 2010/04/26 05:48:19 deraadt Exp $ */ /* $NetBSD: uvm_map.c,v 1.86 2000/11/27 08:40:03 chs Exp $ */ /* @@ -3908,8 +3908,8 @@ uvm_object_printit(uobj, full, pr) struct vm_page *pg; int cnt = 0; - (*pr)("OBJECT %p: locked=%d, pgops=%p, npages=%d, ", - uobj, uobj->vmobjlock.lock_data, uobj->pgops, uobj->uo_npages); + (*pr)("OBJECT %p: pgops=%p, npages=%d, ", + uobj, uobj->pgops, uobj->uo_npages); if (UVM_OBJ_IS_KERN_OBJECT(uobj)) (*pr)("refs=<SYSTEM>\n"); else |