1 /* 2 * Copyright (c) 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code contains ideas from software contributed to Berkeley by 6 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating 7 * System project at Carnegie-Mellon University. 8 * 9 * %sccs.include.redist.c% 10 * 11 * @(#)kern_lock.c 8.11 (Berkeley) 05/14/95 12 */ 13 14 #include <sys/param.h> 15 #include <sys/proc.h> 16 #include <sys/lock.h> 17 18 /* 19 * Locking primitives implementation. 20 * Locks provide shared/exclusive sychronization. 21 */ 22 23 #if NCPUS > 1 24 25 /* 26 * For multiprocessor system, try spin lock first. 27 * 28 * This should be inline expanded below, but we cannot have #if 29 * inside a multiline define. 30 */ 31 int lock_wait_time = 100; 32 #define PAUSE(lkp, wanted) \ 33 if (lock_wait_time > 0) { \ 34 int i; \ 35 \ 36 simple_unlock(&lkp->lk_interlock); \ 37 for (i = lock_wait_time; i > 0; i--) \ 38 if (!(wanted)) \ 39 break; \ 40 simple_lock(&lkp->lk_interlock); \ 41 } \ 42 if (!(wanted)) \ 43 break; 44 45 #else /* NCPUS == 1 */ 46 47 /* 48 * It is an error to spin on a uniprocessor as nothing will ever cause 49 * the simple lock to clear while we are executing. 50 */ 51 #define PAUSE(lkp, wanted) 52 53 #endif /* NCPUS == 1 */ 54 55 /* 56 * Acquire a resource. 57 */ 58 #define ACQUIRE(lkp, error, extflags, wanted) \ 59 PAUSE(lkp, wanted); \ 60 for (error = 0; wanted; ) { \ 61 (lkp)->lk_waitcount++; \ 62 simple_unlock(&(lkp)->lk_interlock); \ 63 error = tsleep((void *)lkp, (lkp)->lk_prio, \ 64 (lkp)->lk_wmesg, (lkp)->lk_timo); \ 65 simple_lock(&(lkp)->lk_interlock); \ 66 (lkp)->lk_waitcount--; \ 67 if (error) \ 68 break; \ 69 if ((extflags) & LK_SLEEPFAIL) { \ 70 error = ENOLCK; \ 71 break; \ 72 } \ 73 } 74 75 /* 76 * Initialize a lock; required before use. 77 */ 78 void 79 lockinit(lkp, prio, wmesg, timo, flags) 80 struct lock *lkp; 81 int prio; 82 char *wmesg; 83 int timo; 84 int flags; 85 { 86 87 bzero(lkp, sizeof(struct lock)); 88 simple_lock_init(&lkp->lk_interlock); 89 lkp->lk_flags = flags & LK_EXTFLG_MASK; 90 lkp->lk_prio = prio; 91 lkp->lk_timo = timo; 92 lkp->lk_wmesg = wmesg; 93 lkp->lk_lockholder = LK_NOPROC; 94 } 95 96 /* 97 * Determine the status of a lock. 98 */ 99 int 100 lockstatus(lkp) 101 struct lock *lkp; 102 { 103 int lock_type = 0; 104 105 simple_lock(&lkp->lk_interlock); 106 if (lkp->lk_exclusivecount != 0) 107 lock_type = LK_EXCLUSIVE; 108 else if (lkp->lk_sharecount != 0) 109 lock_type = LK_SHARED; 110 simple_unlock(&lkp->lk_interlock); 111 return (lock_type); 112 } 113 114 /* 115 * Set, change, or release a lock. 116 * 117 * Shared requests increment the shared count. Exclusive requests set the 118 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already 119 * accepted shared locks and shared-to-exclusive upgrades to go away. 120 */ 121 int 122 lockmgr(lkp, flags, interlkp, p) 123 __volatile struct lock *lkp; 124 u_int flags; 125 struct simplelock *interlkp; 126 struct proc *p; 127 { 128 int error; 129 pid_t pid; 130 int extflags; 131 132 error = 0; 133 if (p) 134 pid = p->p_pid; 135 else 136 pid = LK_KERNPROC; 137 simple_lock(&lkp->lk_interlock); 138 if (flags & LK_INTERLOCK) 139 simple_unlock(interlkp); 140 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK; 141 #ifdef DIAGNOSTIC 142 /* 143 * Once a lock has drained, the LK_DRAINING flag is set and an 144 * exclusive lock is returned. The only valid operation thereafter 145 * is a single release of that exclusive lock. This final release 146 * clears the LK_DRAINING flag and sets the LK_DRAINED flag. Any 147 * further requests of any sort will result in a panic. The bits 148 * selected for these two flags are chosen so that they will be set 149 * in memory that is freed (freed memory is filled with 0xdeadbeef). 150 */ 151 if (lkp->lk_flags & (LK_DRAINING|LK_DRAINED)) { 152 if (lkp->lk_flags & LK_DRAINED) 153 panic("lockmgr: using decommissioned lock"); 154 if ((flags & LK_TYPE_MASK) != LK_RELEASE || 155 lkp->lk_lockholder != pid) 156 panic("lockmgr: non-release on draining lock: %d\n", 157 flags & LK_TYPE_MASK); 158 lkp->lk_flags &= ~LK_DRAINING; 159 lkp->lk_flags |= LK_DRAINED; 160 } 161 #endif DIAGNOSTIC 162 163 switch (flags & LK_TYPE_MASK) { 164 165 case LK_SHARED: 166 if (lkp->lk_lockholder != pid) { 167 /* 168 * If just polling, check to see if we will block. 169 */ 170 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & 171 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE))) { 172 error = EBUSY; 173 break; 174 } 175 /* 176 * Wait for exclusive locks and upgrades to clear. 177 */ 178 ACQUIRE(lkp, error, extflags, lkp->lk_flags & 179 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)); 180 if (error) 181 break; 182 lkp->lk_sharecount++; 183 break; 184 } 185 /* 186 * We hold an exclusive lock, so downgrade it to shared. 187 * An alternative would be to fail with EDEADLK. 188 */ 189 lkp->lk_sharecount++; 190 /* fall into downgrade */ 191 192 case LK_DOWNGRADE: 193 if (lkp->lk_lockholder != pid || lkp->lk_exclusivecount == 0) 194 panic("lockmgr: not holding exclusive lock"); 195 lkp->lk_sharecount += lkp->lk_exclusivecount; 196 lkp->lk_exclusivecount = 0; 197 lkp->lk_flags &= ~LK_HAVE_EXCL; 198 lkp->lk_lockholder = LK_NOPROC; 199 if (lkp->lk_waitcount) 200 wakeup((void *)lkp); 201 break; 202 203 case LK_EXCLUPGRADE: 204 /* 205 * If another process is ahead of us to get an upgrade, 206 * then we want to fail rather than have an intervening 207 * exclusive access. 208 */ 209 if (lkp->lk_flags & LK_WANT_UPGRADE) { 210 lkp->lk_sharecount--; 211 error = EBUSY; 212 break; 213 } 214 /* fall into normal upgrade */ 215 216 case LK_UPGRADE: 217 /* 218 * Upgrade a shared lock to an exclusive one. If another 219 * shared lock has already requested an upgrade to an 220 * exclusive lock, our shared lock is released and an 221 * exclusive lock is requested (which will be granted 222 * after the upgrade). If we return an error, the file 223 * will always be unlocked. 224 */ 225 if (lkp->lk_lockholder == pid || lkp->lk_sharecount <= 0) 226 panic("lockmgr: upgrade exclusive lock"); 227 lkp->lk_sharecount--; 228 /* 229 * If we are just polling, check to see if we will block. 230 */ 231 if ((extflags & LK_NOWAIT) && 232 ((lkp->lk_flags & LK_WANT_UPGRADE) || 233 lkp->lk_sharecount > 1)) { 234 error = EBUSY; 235 break; 236 } 237 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) { 238 /* 239 * We are first shared lock to request an upgrade, so 240 * request upgrade and wait for the shared count to 241 * drop to zero, then take exclusive lock. 242 */ 243 lkp->lk_flags |= LK_WANT_UPGRADE; 244 ACQUIRE(lkp, error, extflags, lkp->lk_sharecount); 245 lkp->lk_flags &= ~LK_WANT_UPGRADE; 246 if (error) 247 break; 248 lkp->lk_flags |= LK_HAVE_EXCL; 249 lkp->lk_lockholder = pid; 250 if (lkp->lk_exclusivecount != 0) 251 panic("lockmgr: non-zero exclusive count"); 252 lkp->lk_exclusivecount = 1; 253 break; 254 } 255 /* 256 * Someone else has requested upgrade. Release our shared 257 * lock, awaken upgrade requestor if we are the last shared 258 * lock, then request an exclusive lock. 259 */ 260 if (lkp->lk_sharecount == 0 && lkp->lk_waitcount) 261 wakeup((void *)lkp); 262 /* fall into exclusive request */ 263 264 case LK_EXCLUSIVE: 265 if (lkp->lk_lockholder == pid && pid != LK_KERNPROC) { 266 /* 267 * Recursive lock. 268 */ 269 if ((extflags & LK_CANRECURSE) == 0) 270 panic("lockmgr: locking against myself"); 271 lkp->lk_exclusivecount++; 272 break; 273 } 274 /* 275 * If we are just polling, check to see if we will sleep. 276 */ 277 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags & 278 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) || 279 lkp->lk_sharecount != 0)) { 280 error = EBUSY; 281 break; 282 } 283 /* 284 * Try to acquire the want_exclusive flag. 285 */ 286 ACQUIRE(lkp, error, extflags, lkp->lk_flags & 287 (LK_HAVE_EXCL | LK_WANT_EXCL)); 288 if (error) 289 break; 290 lkp->lk_flags |= LK_WANT_EXCL; 291 /* 292 * Wait for shared locks and upgrades to finish. 293 */ 294 ACQUIRE(lkp, error, extflags, lkp->lk_sharecount != 0 || 295 (lkp->lk_flags & LK_WANT_UPGRADE)); 296 lkp->lk_flags &= ~LK_WANT_EXCL; 297 if (error) 298 break; 299 lkp->lk_flags |= LK_HAVE_EXCL; 300 lkp->lk_lockholder = pid; 301 if (lkp->lk_exclusivecount != 0) 302 panic("lockmgr: non-zero exclusive count"); 303 lkp->lk_exclusivecount = 1; 304 break; 305 306 case LK_RELEASE: 307 if (lkp->lk_exclusivecount != 0) { 308 if (pid != lkp->lk_lockholder) 309 panic("lockmgr: pid %d, not %s %d unlocking", 310 pid, "exclusive lock holder", 311 lkp->lk_lockholder); 312 lkp->lk_exclusivecount--; 313 if (lkp->lk_exclusivecount == 0) { 314 lkp->lk_flags &= ~LK_HAVE_EXCL; 315 lkp->lk_lockholder = LK_NOPROC; 316 } 317 } else if (lkp->lk_sharecount != 0) 318 lkp->lk_sharecount--; 319 if (lkp->lk_waitcount) 320 wakeup((void *)lkp); 321 break; 322 323 case LK_DRAIN: 324 /* 325 * Check that we do not already hold the lock, as it can 326 * never drain if we do. Unfortunately, we have no way to 327 * check for holding a shared lock, but at least we can 328 * check for an exclusive one. 329 */ 330 if (lkp->lk_lockholder == pid) 331 panic("lockmgr: draining against myself"); 332 /* 333 * If we are just polling, check to see if we will sleep. 334 */ 335 if ((extflags & LK_NOWAIT) && ((lkp->lk_flags & 336 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) || 337 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)) { 338 error = EBUSY; 339 break; 340 } 341 PAUSE(lkp, ((lkp->lk_flags & 342 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) || 343 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0)); 344 for (error = 0; ((lkp->lk_flags & 345 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) || 346 lkp->lk_sharecount != 0 || lkp->lk_waitcount != 0); ) { 347 lkp->lk_flags |= LK_WAITDRAIN; 348 simple_unlock(&lkp->lk_interlock); 349 if (error = tsleep((void *)&lkp->lk_flags, lkp->lk_prio, 350 lkp->lk_wmesg, lkp->lk_timo)) 351 return (error); 352 if ((extflags) & LK_SLEEPFAIL) 353 return (ENOLCK); 354 simple_lock(&lkp->lk_interlock); 355 } 356 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL; 357 lkp->lk_lockholder = pid; 358 lkp->lk_exclusivecount = 1; 359 break; 360 361 default: 362 simple_unlock(&lkp->lk_interlock); 363 panic("lockmgr: unknown locktype request %d", 364 flags & LK_TYPE_MASK); 365 /* NOTREACHED */ 366 } 367 if ((lkp->lk_flags & LK_WAITDRAIN) && ((lkp->lk_flags & 368 (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE)) == 0 && 369 lkp->lk_sharecount == 0 && lkp->lk_waitcount == 0)) { 370 lkp->lk_flags &= ~LK_WAITDRAIN; 371 wakeup((void *)&lkp->lk_flags); 372 } 373 simple_unlock(&lkp->lk_interlock); 374 return (error); 375 } 376 377 /* 378 * Print out information about state of a lock. Used by VOP_PRINT 379 * routines to display ststus about contained locks. 380 */ 381 lockmgr_printinfo(lkp) 382 struct lock *lkp; 383 { 384 385 if (lkp->lk_sharecount) 386 printf(" lock type %s: SHARED", lkp->lk_wmesg); 387 else if (lkp->lk_flags & LK_HAVE_EXCL) 388 printf(" lock type %s: EXCL by pid %d", lkp->lk_wmesg, 389 lkp->lk_lockholder); 390 if (lkp->lk_waitcount > 0) 391 printf(" with %d pending", lkp->lk_waitcount); 392 } 393 394 #if defined(DEBUG) && NCPUS == 1 395 #include <sys/kernel.h> 396 #include <vm/vm.h> 397 #include <sys/sysctl.h> 398 int lockpausetime = 1; 399 struct ctldebug debug2 = { "lockpausetime", &lockpausetime }; 400 /* 401 * Simple lock functions so that the debugger can see from whence 402 * they are being called. 403 */ 404 void 405 simple_lock_init(alp) 406 struct simplelock *alp; 407 { 408 409 alp->lock_data = 0; 410 } 411 412 void 413 simple_lock(alp) 414 __volatile struct simplelock *alp; 415 { 416 417 if (alp->lock_data == 1) { 418 if (lockpausetime == -1) 419 panic("simple_lock: lock held"); 420 if (lockpausetime > 0) { 421 printf("simple_lock: lock held..."); 422 tsleep(&lockpausetime, PCATCH | PPAUSE, "slock", 423 lockpausetime * hz); 424 printf(" continuing\n"); 425 } 426 } 427 alp->lock_data = 1; 428 } 429 430 int 431 simple_lock_try(alp) 432 __volatile struct simplelock *alp; 433 { 434 435 if (alp->lock_data == 1) { 436 if (lockpausetime == -1) 437 panic("simple_lock_try: lock held"); 438 if (lockpausetime > 0) { 439 printf("simple_lock_try: lock held..."); 440 tsleep(&lockpausetime, PCATCH | PPAUSE, "slock", 441 lockpausetime * hz); 442 printf(" continuing\n"); 443 } 444 } 445 alp->lock_data = 1; 446 return (1); 447 } 448 449 void 450 simple_unlock(alp) 451 __volatile struct simplelock *alp; 452 { 453 454 if (alp->lock_data == 0) { 455 if (lockpausetime == -1) 456 panic("simple_unlock: lock not held"); 457 if (lockpausetime > 0) { 458 printf("simple_unlock: lock not held..."); 459 tsleep(&lockpausetime, PCATCH | PPAUSE, "sunlock", 460 lockpausetime * hz); 461 printf(" continuing\n"); 462 } 463 } 464 alp->lock_data = 0; 465 } 466 #endif /* DEBUG && NCPUS == 1 */ 467