1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <linux/export.h> 4 #include <linux/log2.h> 5 #include <linux/percpu.h> 6 #include <linux/preempt.h> 7 #include <linux/rcupdate.h> 8 #include <linux/sched.h> 9 #include <linux/sched/clock.h> 10 #include <linux/sched/rt.h> 11 #include <linux/sched/task.h> 12 #include <linux/slab.h> 13 14 #include <trace/events/lock.h> 15 16 #include "six.h" 17 18 #ifdef DEBUG 19 #define EBUG_ON(cond) BUG_ON(cond) 20 #else 21 #define EBUG_ON(cond) do {} while (0) 22 #endif 23 24 #define six_acquire(l, t, r, ip) lock_acquire(l, 0, t, r, 1, NULL, ip) 25 #define six_release(l, ip) lock_release(l, ip) 26 27 static void do_six_unlock_type(struct six_lock *lock, enum six_lock_type type); 28 29 #define SIX_LOCK_HELD_read_OFFSET 0 30 #define SIX_LOCK_HELD_read ~(~0U << 26) 31 #define SIX_LOCK_HELD_intent (1U << 26) 32 #define SIX_LOCK_HELD_write (1U << 27) 33 #define SIX_LOCK_WAITING_read (1U << (28 + SIX_LOCK_read)) 34 #define SIX_LOCK_WAITING_write (1U << (28 + SIX_LOCK_write)) 35 #define SIX_LOCK_NOSPIN (1U << 31) 36 37 struct six_lock_vals { 38 /* Value we add to the lock in order to take the lock: */ 39 u32 lock_val; 40 41 /* If the lock has this value (used as a mask), taking the lock fails: */ 42 u32 lock_fail; 43 44 /* Mask that indicates lock is held for this type: */ 45 u32 held_mask; 46 47 /* Waitlist we wakeup when releasing the lock: */ 48 enum six_lock_type unlock_wakeup; 49 }; 50 51 static const struct six_lock_vals l[] = { 52 [SIX_LOCK_read] = { 53 .lock_val = 1U << SIX_LOCK_HELD_read_OFFSET, 54 .lock_fail = SIX_LOCK_HELD_write, 55 .held_mask = SIX_LOCK_HELD_read, 56 .unlock_wakeup = SIX_LOCK_write, 57 }, 58 [SIX_LOCK_intent] = { 59 .lock_val = SIX_LOCK_HELD_intent, 60 .lock_fail = SIX_LOCK_HELD_intent, 61 .held_mask = SIX_LOCK_HELD_intent, 62 .unlock_wakeup = SIX_LOCK_intent, 63 }, 64 [SIX_LOCK_write] = { 65 .lock_val = SIX_LOCK_HELD_write, 66 .lock_fail = SIX_LOCK_HELD_read, 67 .held_mask = SIX_LOCK_HELD_write, 68 .unlock_wakeup = SIX_LOCK_read, 69 }, 70 }; 71 72 static inline void six_set_bitmask(struct six_lock *lock, u32 mask) 73 { 74 if ((atomic_read(&lock->state) & mask) != mask) 75 atomic_or(mask, &lock->state); 76 } 77 78 static inline void six_clear_bitmask(struct six_lock *lock, u32 mask) 79 { 80 if (atomic_read(&lock->state) & mask) 81 atomic_and(~mask, &lock->state); 82 } 83 84 static inline void six_set_owner(struct six_lock *lock, enum six_lock_type type, 85 u32 old, struct task_struct *owner) 86 { 87 if (type != SIX_LOCK_intent) 88 return; 89 90 if (!(old & SIX_LOCK_HELD_intent)) { 91 EBUG_ON(lock->owner); 92 lock->owner = owner; 93 } else { 94 EBUG_ON(lock->owner != current); 95 } 96 } 97 98 static inline unsigned pcpu_read_count(struct six_lock *lock) 99 { 100 unsigned read_count = 0; 101 int cpu; 102 103 for_each_possible_cpu(cpu) 104 read_count += *per_cpu_ptr(lock->readers, cpu); 105 return read_count; 106 } 107 108 /* 109 * __do_six_trylock() - main trylock routine 110 * 111 * Returns 1 on success, 0 on failure 112 * 113 * In percpu reader mode, a failed trylock may cause a spurious trylock failure 114 * for anoter thread taking the competing lock type, and we may havve to do a 115 * wakeup: when a wakeup is required, we return -1 - wakeup_type. 116 */ 117 static int __do_six_trylock(struct six_lock *lock, enum six_lock_type type, 118 struct task_struct *task, bool try) 119 { 120 int ret; 121 u32 old; 122 123 EBUG_ON(type == SIX_LOCK_write && lock->owner != task); 124 EBUG_ON(type == SIX_LOCK_write && 125 (try != !(atomic_read(&lock->state) & SIX_LOCK_HELD_write))); 126 127 /* 128 * Percpu reader mode: 129 * 130 * The basic idea behind this algorithm is that you can implement a lock 131 * between two threads without any atomics, just memory barriers: 132 * 133 * For two threads you'll need two variables, one variable for "thread a 134 * has the lock" and another for "thread b has the lock". 135 * 136 * To take the lock, a thread sets its variable indicating that it holds 137 * the lock, then issues a full memory barrier, then reads from the 138 * other thread's variable to check if the other thread thinks it has 139 * the lock. If we raced, we backoff and retry/sleep. 140 * 141 * Failure to take the lock may cause a spurious trylock failure in 142 * another thread, because we temporarily set the lock to indicate that 143 * we held it. This would be a problem for a thread in six_lock(), when 144 * they are calling trylock after adding themself to the waitlist and 145 * prior to sleeping. 146 * 147 * Therefore, if we fail to get the lock, and there were waiters of the 148 * type we conflict with, we will have to issue a wakeup. 149 * 150 * Since we may be called under wait_lock (and by the wakeup code 151 * itself), we return that the wakeup has to be done instead of doing it 152 * here. 153 */ 154 if (type == SIX_LOCK_read && lock->readers) { 155 preempt_disable(); 156 this_cpu_inc(*lock->readers); /* signal that we own lock */ 157 158 smp_mb(); 159 160 old = atomic_read(&lock->state); 161 ret = !(old & l[type].lock_fail); 162 163 this_cpu_sub(*lock->readers, !ret); 164 preempt_enable(); 165 166 if (!ret) { 167 smp_mb(); 168 if (atomic_read(&lock->state) & SIX_LOCK_WAITING_write) 169 ret = -1 - SIX_LOCK_write; 170 } 171 } else if (type == SIX_LOCK_write && lock->readers) { 172 if (try) 173 atomic_add(SIX_LOCK_HELD_write, &lock->state); 174 175 /* 176 * Make sure atomic_add happens before pcpu_read_count and 177 * six_set_bitmask in slow path happens before pcpu_read_count. 178 * 179 * Paired with the smp_mb() in read lock fast path (per-cpu mode) 180 * and the one before atomic_read in read unlock path. 181 */ 182 smp_mb(); 183 ret = !pcpu_read_count(lock); 184 185 if (try && !ret) { 186 old = atomic_sub_return(SIX_LOCK_HELD_write, &lock->state); 187 if (old & SIX_LOCK_WAITING_read) 188 ret = -1 - SIX_LOCK_read; 189 } 190 } else { 191 old = atomic_read(&lock->state); 192 do { 193 ret = !(old & l[type].lock_fail); 194 if (!ret || (type == SIX_LOCK_write && !try)) { 195 smp_mb(); 196 break; 197 } 198 } while (!atomic_try_cmpxchg_acquire(&lock->state, &old, old + l[type].lock_val)); 199 200 EBUG_ON(ret && !(atomic_read(&lock->state) & l[type].held_mask)); 201 } 202 203 if (ret > 0) 204 six_set_owner(lock, type, old, task); 205 206 EBUG_ON(type == SIX_LOCK_write && try && ret <= 0 && 207 (atomic_read(&lock->state) & SIX_LOCK_HELD_write)); 208 209 return ret; 210 } 211 212 static void __six_lock_wakeup(struct six_lock *lock, enum six_lock_type lock_type) 213 { 214 struct six_lock_waiter *w, *next; 215 struct task_struct *task; 216 bool saw_one; 217 int ret; 218 again: 219 ret = 0; 220 saw_one = false; 221 raw_spin_lock(&lock->wait_lock); 222 223 list_for_each_entry_safe(w, next, &lock->wait_list, list) { 224 if (w->lock_want != lock_type) 225 continue; 226 227 if (saw_one && lock_type != SIX_LOCK_read) 228 goto unlock; 229 saw_one = true; 230 231 ret = __do_six_trylock(lock, lock_type, w->task, false); 232 if (ret <= 0) 233 goto unlock; 234 235 /* 236 * Similar to percpu_rwsem_wake_function(), we need to guard 237 * against the wakee noticing w->lock_acquired, returning, and 238 * then exiting before we do the wakeup: 239 */ 240 task = get_task_struct(w->task); 241 __list_del(w->list.prev, w->list.next); 242 /* 243 * The release barrier here ensures the ordering of the 244 * __list_del before setting w->lock_acquired; @w is on the 245 * stack of the thread doing the waiting and will be reused 246 * after it sees w->lock_acquired with no other locking: 247 * pairs with smp_load_acquire() in six_lock_slowpath() 248 */ 249 smp_store_release(&w->lock_acquired, true); 250 wake_up_process(task); 251 put_task_struct(task); 252 } 253 254 six_clear_bitmask(lock, SIX_LOCK_WAITING_read << lock_type); 255 unlock: 256 raw_spin_unlock(&lock->wait_lock); 257 258 if (ret < 0) { 259 lock_type = -ret - 1; 260 goto again; 261 } 262 } 263 264 __always_inline 265 static void six_lock_wakeup(struct six_lock *lock, u32 state, 266 enum six_lock_type lock_type) 267 { 268 if (lock_type == SIX_LOCK_write && (state & SIX_LOCK_HELD_read)) 269 return; 270 271 if (!(state & (SIX_LOCK_WAITING_read << lock_type))) 272 return; 273 274 __six_lock_wakeup(lock, lock_type); 275 } 276 277 __always_inline 278 static bool do_six_trylock(struct six_lock *lock, enum six_lock_type type, bool try) 279 { 280 int ret; 281 282 ret = __do_six_trylock(lock, type, current, try); 283 if (ret < 0) 284 __six_lock_wakeup(lock, -ret - 1); 285 286 return ret > 0; 287 } 288 289 /** 290 * six_trylock_ip - attempt to take a six lock without blocking 291 * @lock: lock to take 292 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write 293 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_ 294 * 295 * Return: true on success, false on failure. 296 */ 297 bool six_trylock_ip(struct six_lock *lock, enum six_lock_type type, unsigned long ip) 298 { 299 if (!do_six_trylock(lock, type, true)) 300 return false; 301 302 if (type != SIX_LOCK_write) 303 six_acquire(&lock->dep_map, 1, type == SIX_LOCK_read, ip); 304 return true; 305 } 306 EXPORT_SYMBOL_GPL(six_trylock_ip); 307 308 /** 309 * six_relock_ip - attempt to re-take a lock that was held previously 310 * @lock: lock to take 311 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write 312 * @seq: lock sequence number obtained from six_lock_seq() while lock was 313 * held previously 314 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_ 315 * 316 * Return: true on success, false on failure. 317 */ 318 bool six_relock_ip(struct six_lock *lock, enum six_lock_type type, 319 unsigned seq, unsigned long ip) 320 { 321 if (six_lock_seq(lock) != seq || !six_trylock_ip(lock, type, ip)) 322 return false; 323 324 if (six_lock_seq(lock) != seq) { 325 six_unlock_ip(lock, type, ip); 326 return false; 327 } 328 329 return true; 330 } 331 EXPORT_SYMBOL_GPL(six_relock_ip); 332 333 #ifdef CONFIG_BCACHEFS_SIX_OPTIMISTIC_SPIN 334 335 static inline bool six_owner_running(struct six_lock *lock) 336 { 337 /* 338 * When there's no owner, we might have preempted between the owner 339 * acquiring the lock and setting the owner field. If we're an RT task 340 * that will live-lock because we won't let the owner complete. 341 */ 342 guard(rcu)(); 343 struct task_struct *owner = READ_ONCE(lock->owner); 344 return owner ? owner_on_cpu(owner) : !rt_or_dl_task(current); 345 } 346 347 static inline bool six_optimistic_spin(struct six_lock *lock, 348 struct six_lock_waiter *wait, 349 enum six_lock_type type) 350 { 351 unsigned loop = 0; 352 u64 end_time; 353 354 if (type == SIX_LOCK_write) 355 return false; 356 357 if (lock->wait_list.next != &wait->list) 358 return false; 359 360 if (atomic_read(&lock->state) & SIX_LOCK_NOSPIN) 361 return false; 362 363 preempt_disable(); 364 end_time = sched_clock() + 10 * NSEC_PER_USEC; 365 366 while (!need_resched() && six_owner_running(lock)) { 367 /* 368 * Ensures that writes to the waitlist entry happen after we see 369 * wait->lock_acquired: pairs with the smp_store_release in 370 * __six_lock_wakeup 371 */ 372 if (smp_load_acquire(&wait->lock_acquired)) { 373 preempt_enable(); 374 return true; 375 } 376 377 if (!(++loop & 0xf) && (time_after64(sched_clock(), end_time))) { 378 six_set_bitmask(lock, SIX_LOCK_NOSPIN); 379 break; 380 } 381 382 /* 383 * The cpu_relax() call is a compiler barrier which forces 384 * everything in this loop to be re-loaded. We don't need 385 * memory barriers as we'll eventually observe the right 386 * values at the cost of a few extra spins. 387 */ 388 cpu_relax(); 389 } 390 391 preempt_enable(); 392 return false; 393 } 394 395 #else /* CONFIG_LOCK_SPIN_ON_OWNER */ 396 397 static inline bool six_optimistic_spin(struct six_lock *lock, 398 struct six_lock_waiter *wait, 399 enum six_lock_type type) 400 { 401 return false; 402 } 403 404 #endif 405 406 noinline 407 static int six_lock_slowpath(struct six_lock *lock, enum six_lock_type type, 408 struct six_lock_waiter *wait, 409 six_lock_should_sleep_fn should_sleep_fn, void *p, 410 unsigned long ip) 411 { 412 int ret = 0; 413 414 if (type == SIX_LOCK_write) { 415 EBUG_ON(atomic_read(&lock->state) & SIX_LOCK_HELD_write); 416 atomic_add(SIX_LOCK_HELD_write, &lock->state); 417 smp_mb__after_atomic(); 418 } 419 420 trace_contention_begin(lock, 0); 421 lock_contended(&lock->dep_map, ip); 422 423 wait->task = current; 424 wait->lock_want = type; 425 wait->lock_acquired = false; 426 427 raw_spin_lock(&lock->wait_lock); 428 six_set_bitmask(lock, SIX_LOCK_WAITING_read << type); 429 /* 430 * Retry taking the lock after taking waitlist lock, in case we raced 431 * with an unlock: 432 */ 433 ret = __do_six_trylock(lock, type, current, false); 434 if (ret <= 0) { 435 wait->start_time = local_clock(); 436 437 if (!list_empty(&lock->wait_list)) { 438 struct six_lock_waiter *last = 439 list_last_entry(&lock->wait_list, 440 struct six_lock_waiter, list); 441 442 if (time_before_eq64(wait->start_time, last->start_time)) 443 wait->start_time = last->start_time + 1; 444 } 445 446 list_add_tail(&wait->list, &lock->wait_list); 447 } 448 raw_spin_unlock(&lock->wait_lock); 449 450 if (unlikely(ret > 0)) { 451 ret = 0; 452 goto out; 453 } 454 455 if (unlikely(ret < 0)) { 456 __six_lock_wakeup(lock, -ret - 1); 457 ret = 0; 458 } 459 460 if (six_optimistic_spin(lock, wait, type)) 461 goto out; 462 463 while (1) { 464 set_current_state(TASK_UNINTERRUPTIBLE); 465 466 /* 467 * Ensures that writes to the waitlist entry happen after we see 468 * wait->lock_acquired: pairs with the smp_store_release in 469 * __six_lock_wakeup 470 */ 471 if (smp_load_acquire(&wait->lock_acquired)) 472 break; 473 474 ret = should_sleep_fn ? should_sleep_fn(lock, p) : 0; 475 if (unlikely(ret)) { 476 bool acquired; 477 478 /* 479 * If should_sleep_fn() returns an error, we are 480 * required to return that error even if we already 481 * acquired the lock - should_sleep_fn() might have 482 * modified external state (e.g. when the deadlock cycle 483 * detector in bcachefs issued a transaction restart) 484 */ 485 raw_spin_lock(&lock->wait_lock); 486 acquired = wait->lock_acquired; 487 if (!acquired) 488 list_del(&wait->list); 489 raw_spin_unlock(&lock->wait_lock); 490 491 if (unlikely(acquired)) { 492 do_six_unlock_type(lock, type); 493 } else if (type == SIX_LOCK_write) { 494 six_clear_bitmask(lock, SIX_LOCK_HELD_write); 495 six_lock_wakeup(lock, atomic_read(&lock->state), SIX_LOCK_read); 496 } 497 break; 498 } 499 500 schedule(); 501 } 502 503 __set_current_state(TASK_RUNNING); 504 out: 505 trace_contention_end(lock, 0); 506 507 return ret; 508 } 509 510 /** 511 * six_lock_ip_waiter - take a lock, with full waitlist interface 512 * @lock: lock to take 513 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write 514 * @wait: pointer to wait object, which will be added to lock's waitlist 515 * @should_sleep_fn: callback run after adding to waitlist, immediately prior 516 * to scheduling 517 * @p: passed through to @should_sleep_fn 518 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_ 519 * 520 * This is the most general six_lock() variant, with parameters to support full 521 * cycle detection for deadlock avoidance. 522 * 523 * The code calling this function must implement tracking of held locks, and the 524 * @wait object should be embedded into the struct that tracks held locks - 525 * which must also be accessible in a thread-safe way. 526 * 527 * @should_sleep_fn should invoke the cycle detector; it should walk each 528 * lock's waiters, and for each waiter recursively walk their held locks. 529 * 530 * When this function must block, @wait will be added to @lock's waitlist before 531 * calling trylock, and before calling @should_sleep_fn, and @wait will not be 532 * removed from the lock waitlist until the lock has been successfully acquired, 533 * or we abort. 534 * 535 * @wait.start_time will be monotonically increasing for any given waitlist, and 536 * thus may be used as a loop cursor. 537 * 538 * Return: 0 on success, or the return code from @should_sleep_fn on failure. 539 */ 540 int six_lock_ip_waiter(struct six_lock *lock, enum six_lock_type type, 541 struct six_lock_waiter *wait, 542 six_lock_should_sleep_fn should_sleep_fn, void *p, 543 unsigned long ip) 544 { 545 int ret; 546 547 wait->start_time = 0; 548 549 if (type != SIX_LOCK_write) 550 six_acquire(&lock->dep_map, 0, type == SIX_LOCK_read, ip); 551 552 ret = do_six_trylock(lock, type, true) ? 0 553 : six_lock_slowpath(lock, type, wait, should_sleep_fn, p, ip); 554 555 if (ret && type != SIX_LOCK_write) 556 six_release(&lock->dep_map, ip); 557 if (!ret) 558 lock_acquired(&lock->dep_map, ip); 559 560 return ret; 561 } 562 EXPORT_SYMBOL_GPL(six_lock_ip_waiter); 563 564 __always_inline 565 static void do_six_unlock_type(struct six_lock *lock, enum six_lock_type type) 566 { 567 u32 state; 568 569 if (type == SIX_LOCK_intent) 570 lock->owner = NULL; 571 572 if (type == SIX_LOCK_read && 573 lock->readers) { 574 smp_mb(); /* unlock barrier */ 575 this_cpu_dec(*lock->readers); 576 smp_mb(); /* between unlocking and checking for waiters */ 577 state = atomic_read(&lock->state); 578 } else { 579 u32 v = l[type].lock_val; 580 581 if (type != SIX_LOCK_read) 582 v += atomic_read(&lock->state) & SIX_LOCK_NOSPIN; 583 584 EBUG_ON(!(atomic_read(&lock->state) & l[type].held_mask)); 585 state = atomic_sub_return_release(v, &lock->state); 586 } 587 588 six_lock_wakeup(lock, state, l[type].unlock_wakeup); 589 } 590 591 /** 592 * six_unlock_ip - drop a six lock 593 * @lock: lock to unlock 594 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write 595 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_ 596 * 597 * When a lock is held multiple times (because six_lock_incement()) was used), 598 * this decrements the 'lock held' counter by one. 599 * 600 * For example: 601 * six_lock_read(&foo->lock); read count 1 602 * six_lock_increment(&foo->lock, SIX_LOCK_read); read count 2 603 * six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 1 604 * six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 0 605 */ 606 void six_unlock_ip(struct six_lock *lock, enum six_lock_type type, unsigned long ip) 607 { 608 EBUG_ON(type == SIX_LOCK_write && 609 !(atomic_read(&lock->state) & SIX_LOCK_HELD_intent)); 610 EBUG_ON((type == SIX_LOCK_write || 611 type == SIX_LOCK_intent) && 612 lock->owner != current); 613 614 if (type != SIX_LOCK_write) 615 six_release(&lock->dep_map, ip); 616 617 if (type == SIX_LOCK_intent && 618 lock->intent_lock_recurse) { 619 --lock->intent_lock_recurse; 620 return; 621 } 622 623 if (type == SIX_LOCK_write && 624 lock->write_lock_recurse) { 625 --lock->write_lock_recurse; 626 return; 627 } 628 629 if (type == SIX_LOCK_write) 630 lock->seq++; 631 632 do_six_unlock_type(lock, type); 633 } 634 EXPORT_SYMBOL_GPL(six_unlock_ip); 635 636 /** 637 * six_lock_downgrade - convert an intent lock to a read lock 638 * @lock: lock to dowgrade 639 * 640 * @lock will have read count incremented and intent count decremented 641 */ 642 void six_lock_downgrade(struct six_lock *lock) 643 { 644 six_lock_increment(lock, SIX_LOCK_read); 645 six_unlock_intent(lock); 646 } 647 EXPORT_SYMBOL_GPL(six_lock_downgrade); 648 649 /** 650 * six_lock_tryupgrade - attempt to convert read lock to an intent lock 651 * @lock: lock to upgrade 652 * 653 * On success, @lock will have intent count incremented and read count 654 * decremented 655 * 656 * Return: true on success, false on failure 657 */ 658 bool six_lock_tryupgrade(struct six_lock *lock) 659 { 660 u32 old = atomic_read(&lock->state), new; 661 662 do { 663 new = old; 664 665 if (new & SIX_LOCK_HELD_intent) 666 return false; 667 668 if (!lock->readers) { 669 EBUG_ON(!(new & SIX_LOCK_HELD_read)); 670 new -= l[SIX_LOCK_read].lock_val; 671 } 672 673 new |= SIX_LOCK_HELD_intent; 674 } while (!atomic_try_cmpxchg_acquire(&lock->state, &old, new)); 675 676 if (lock->readers) 677 this_cpu_dec(*lock->readers); 678 679 six_set_owner(lock, SIX_LOCK_intent, old, current); 680 681 return true; 682 } 683 EXPORT_SYMBOL_GPL(six_lock_tryupgrade); 684 685 /** 686 * six_trylock_convert - attempt to convert a held lock from one type to another 687 * @lock: lock to upgrade 688 * @from: SIX_LOCK_read or SIX_LOCK_intent 689 * @to: SIX_LOCK_read or SIX_LOCK_intent 690 * 691 * On success, @lock will have intent count incremented and read count 692 * decremented 693 * 694 * Return: true on success, false on failure 695 */ 696 bool six_trylock_convert(struct six_lock *lock, 697 enum six_lock_type from, 698 enum six_lock_type to) 699 { 700 EBUG_ON(to == SIX_LOCK_write || from == SIX_LOCK_write); 701 702 if (to == from) 703 return true; 704 705 if (to == SIX_LOCK_read) { 706 six_lock_downgrade(lock); 707 return true; 708 } else { 709 return six_lock_tryupgrade(lock); 710 } 711 } 712 EXPORT_SYMBOL_GPL(six_trylock_convert); 713 714 /** 715 * six_lock_increment - increase held lock count on a lock that is already held 716 * @lock: lock to increment 717 * @type: SIX_LOCK_read or SIX_LOCK_intent 718 * 719 * @lock must already be held, with a lock type that is greater than or equal to 720 * @type 721 * 722 * A corresponding six_unlock_type() call will be required for @lock to be fully 723 * unlocked. 724 */ 725 void six_lock_increment(struct six_lock *lock, enum six_lock_type type) 726 { 727 six_acquire(&lock->dep_map, 0, type == SIX_LOCK_read, _RET_IP_); 728 729 /* XXX: assert already locked, and that we don't overflow: */ 730 731 switch (type) { 732 case SIX_LOCK_read: 733 if (lock->readers) { 734 this_cpu_inc(*lock->readers); 735 } else { 736 EBUG_ON(!(atomic_read(&lock->state) & 737 (SIX_LOCK_HELD_read| 738 SIX_LOCK_HELD_intent))); 739 atomic_add(l[type].lock_val, &lock->state); 740 } 741 break; 742 case SIX_LOCK_write: 743 lock->write_lock_recurse++; 744 fallthrough; 745 case SIX_LOCK_intent: 746 EBUG_ON(!(atomic_read(&lock->state) & SIX_LOCK_HELD_intent)); 747 lock->intent_lock_recurse++; 748 break; 749 } 750 } 751 EXPORT_SYMBOL_GPL(six_lock_increment); 752 753 /** 754 * six_lock_wakeup_all - wake up all waiters on @lock 755 * @lock: lock to wake up waiters for 756 * 757 * Wakeing up waiters will cause them to re-run should_sleep_fn, which may then 758 * abort the lock operation. 759 * 760 * This function is never needed in a bug-free program; it's only useful in 761 * debug code, e.g. to determine if a cycle detector is at fault. 762 */ 763 void six_lock_wakeup_all(struct six_lock *lock) 764 { 765 u32 state = atomic_read(&lock->state); 766 struct six_lock_waiter *w; 767 768 six_lock_wakeup(lock, state, SIX_LOCK_read); 769 six_lock_wakeup(lock, state, SIX_LOCK_intent); 770 six_lock_wakeup(lock, state, SIX_LOCK_write); 771 772 raw_spin_lock(&lock->wait_lock); 773 list_for_each_entry(w, &lock->wait_list, list) 774 wake_up_process(w->task); 775 raw_spin_unlock(&lock->wait_lock); 776 } 777 EXPORT_SYMBOL_GPL(six_lock_wakeup_all); 778 779 /** 780 * six_lock_counts - return held lock counts, for each lock type 781 * @lock: lock to return counters for 782 * 783 * Return: the number of times a lock is held for read, intent and write. 784 */ 785 struct six_lock_count six_lock_counts(struct six_lock *lock) 786 { 787 struct six_lock_count ret; 788 789 ret.n[SIX_LOCK_read] = !lock->readers 790 ? atomic_read(&lock->state) & SIX_LOCK_HELD_read 791 : pcpu_read_count(lock); 792 ret.n[SIX_LOCK_intent] = !!(atomic_read(&lock->state) & SIX_LOCK_HELD_intent) + 793 lock->intent_lock_recurse; 794 ret.n[SIX_LOCK_write] = !!(atomic_read(&lock->state) & SIX_LOCK_HELD_write); 795 796 return ret; 797 } 798 EXPORT_SYMBOL_GPL(six_lock_counts); 799 800 /** 801 * six_lock_readers_add - directly manipulate reader count of a lock 802 * @lock: lock to add/subtract readers for 803 * @nr: reader count to add/subtract 804 * 805 * When an upper layer is implementing lock reentrency, we may have both read 806 * and intent locks on the same lock. 807 * 808 * When we need to take a write lock, the read locks will cause self-deadlock, 809 * because six locks themselves do not track which read locks are held by the 810 * current thread and which are held by a different thread - it does no 811 * per-thread tracking of held locks. 812 * 813 * The upper layer that is tracking held locks may however, if trylock() has 814 * failed, count up its own read locks, subtract them, take the write lock, and 815 * then re-add them. 816 * 817 * As in any other situation when taking a write lock, @lock must be held for 818 * intent one (or more) times, so @lock will never be left unlocked. 819 */ 820 void six_lock_readers_add(struct six_lock *lock, int nr) 821 { 822 if (lock->readers) { 823 this_cpu_add(*lock->readers, nr); 824 } else { 825 EBUG_ON((int) (atomic_read(&lock->state) & SIX_LOCK_HELD_read) + nr < 0); 826 /* reader count starts at bit 0 */ 827 atomic_add(nr, &lock->state); 828 } 829 } 830 EXPORT_SYMBOL_GPL(six_lock_readers_add); 831 832 /** 833 * six_lock_exit - release resources held by a lock prior to freeing 834 * @lock: lock to exit 835 * 836 * When a lock was initialized in percpu mode (SIX_OLCK_INIT_PCPU), this is 837 * required to free the percpu read counts. 838 */ 839 void six_lock_exit(struct six_lock *lock) 840 { 841 WARN_ON(lock->readers && pcpu_read_count(lock)); 842 WARN_ON(atomic_read(&lock->state) & SIX_LOCK_HELD_read); 843 844 free_percpu(lock->readers); 845 lock->readers = NULL; 846 } 847 EXPORT_SYMBOL_GPL(six_lock_exit); 848 849 void __six_lock_init(struct six_lock *lock, const char *name, 850 struct lock_class_key *key, enum six_lock_init_flags flags, 851 gfp_t gfp) 852 { 853 atomic_set(&lock->state, 0); 854 raw_spin_lock_init(&lock->wait_lock); 855 INIT_LIST_HEAD(&lock->wait_list); 856 #ifdef CONFIG_DEBUG_LOCK_ALLOC 857 debug_check_no_locks_freed((void *) lock, sizeof(*lock)); 858 lockdep_init_map(&lock->dep_map, name, key, 0); 859 #endif 860 861 /* 862 * Don't assume that we have real percpu variables available in 863 * userspace: 864 */ 865 #ifdef __KERNEL__ 866 if (flags & SIX_LOCK_INIT_PCPU) { 867 /* 868 * We don't return an error here on memory allocation failure 869 * since percpu is an optimization, and locks will work with the 870 * same semantics in non-percpu mode: callers can check for 871 * failure if they wish by checking lock->readers, but generally 872 * will not want to treat it as an error. 873 */ 874 lock->readers = alloc_percpu_gfp(unsigned, gfp); 875 } 876 #endif 877 } 878 EXPORT_SYMBOL_GPL(__six_lock_init); 879