Lines Matching +full:wait +full:- +full:on +full:- +full:read

1 // SPDX-License-Identifier: GPL-2.0
74 if ((atomic_read(&lock->state) & mask) != mask) in six_set_bitmask()
75 atomic_or(mask, &lock->state); in six_set_bitmask()
80 if (atomic_read(&lock->state) & mask) in six_clear_bitmask()
81 atomic_and(~mask, &lock->state); in six_clear_bitmask()
91 EBUG_ON(lock->owner); in six_set_owner()
92 lock->owner = owner; in six_set_owner()
94 EBUG_ON(lock->owner != current); in six_set_owner()
104 read_count += *per_cpu_ptr(lock->readers, cpu); in pcpu_read_count()
109 * __do_six_trylock() - main trylock routine
111 * Returns 1 on success, 0 on failure
115 * wakeup: when a wakeup is required, we return -1 - wakeup_type.
123 EBUG_ON(type == SIX_LOCK_write && lock->owner != task); in __do_six_trylock()
125 (try != !(atomic_read(&lock->state) & SIX_LOCK_HELD_write))); in __do_six_trylock()
154 if (type == SIX_LOCK_read && lock->readers) { in __do_six_trylock()
156 this_cpu_inc(*lock->readers); /* signal that we own lock */ in __do_six_trylock()
160 old = atomic_read(&lock->state); in __do_six_trylock()
163 this_cpu_sub(*lock->readers, !ret); in __do_six_trylock()
168 if (atomic_read(&lock->state) & SIX_LOCK_WAITING_write) in __do_six_trylock()
169 ret = -1 - SIX_LOCK_write; in __do_six_trylock()
171 } else if (type == SIX_LOCK_write && lock->readers) { in __do_six_trylock()
173 atomic_add(SIX_LOCK_HELD_write, &lock->state); in __do_six_trylock()
179 * Paired with the smp_mb() in read lock fast path (per-cpu mode) in __do_six_trylock()
180 * and the one before atomic_read in read unlock path. in __do_six_trylock()
186 old = atomic_sub_return(SIX_LOCK_HELD_write, &lock->state); in __do_six_trylock()
188 ret = -1 - SIX_LOCK_read; in __do_six_trylock()
191 old = atomic_read(&lock->state); in __do_six_trylock()
198 } while (!atomic_try_cmpxchg_acquire(&lock->state, &old, old + l[type].lock_val)); in __do_six_trylock()
200 EBUG_ON(ret && !(atomic_read(&lock->state) & l[type].held_mask)); in __do_six_trylock()
207 (atomic_read(&lock->state) & SIX_LOCK_HELD_write)); in __do_six_trylock()
221 raw_spin_lock(&lock->wait_lock); in __six_lock_wakeup()
223 list_for_each_entry_safe(w, next, &lock->wait_list, list) { in __six_lock_wakeup()
224 if (w->lock_want != lock_type) in __six_lock_wakeup()
231 ret = __do_six_trylock(lock, lock_type, w->task, false); in __six_lock_wakeup()
237 * against the wakee noticing w->lock_acquired, returning, and in __six_lock_wakeup()
240 task = get_task_struct(w->task); in __six_lock_wakeup()
241 __list_del(w->list.prev, w->list.next); in __six_lock_wakeup()
244 * __list_del before setting w->lock_acquired; @w is on the in __six_lock_wakeup()
246 * after it sees w->lock_acquired with no other locking: in __six_lock_wakeup()
249 smp_store_release(&w->lock_acquired, true); in __six_lock_wakeup()
256 raw_spin_unlock(&lock->wait_lock); in __six_lock_wakeup()
259 lock_type = -ret - 1; in __six_lock_wakeup()
284 __six_lock_wakeup(lock, -ret - 1); in do_six_trylock()
290 * six_trylock_ip - attempt to take a six lock without blocking
295 * Return: true on success, false on failure.
303 six_acquire(&lock->dep_map, 1, type == SIX_LOCK_read, ip); in six_trylock_ip()
309 * six_relock_ip - attempt to re-take a lock that was held previously
316 * Return: true on success, false on failure.
340 * that will live-lock because we won't let the owner complete. in six_owner_running()
343 struct task_struct *owner = READ_ONCE(lock->owner); in six_owner_running()
351 struct six_lock_waiter *wait, in six_optimistic_spin() argument
360 if (lock->wait_list.next != &wait->list) in six_optimistic_spin()
363 if (atomic_read(&lock->state) & SIX_LOCK_NOSPIN) in six_optimistic_spin()
372 * wait->lock_acquired: pairs with the smp_store_release in in six_optimistic_spin()
375 if (smp_load_acquire(&wait->lock_acquired)) { in six_optimistic_spin()
387 * everything in this loop to be re-loaded. We don't need in six_optimistic_spin()
401 struct six_lock_waiter *wait, in six_optimistic_spin() argument
411 struct six_lock_waiter *wait, in six_lock_slowpath() argument
418 EBUG_ON(atomic_read(&lock->state) & SIX_LOCK_HELD_write); in six_lock_slowpath()
419 atomic_add(SIX_LOCK_HELD_write, &lock->state); in six_lock_slowpath()
424 lock_contended(&lock->dep_map, ip); in six_lock_slowpath()
426 wait->task = current; in six_lock_slowpath()
427 wait->lock_want = type; in six_lock_slowpath()
428 wait->lock_acquired = false; in six_lock_slowpath()
430 raw_spin_lock(&lock->wait_lock); in six_lock_slowpath()
438 wait->start_time = local_clock(); in six_lock_slowpath()
440 if (!list_empty(&lock->wait_list)) { in six_lock_slowpath()
442 list_last_entry(&lock->wait_list, in six_lock_slowpath()
445 if (time_before_eq64(wait->start_time, last->start_time)) in six_lock_slowpath()
446 wait->start_time = last->start_time + 1; in six_lock_slowpath()
449 list_add_tail(&wait->list, &lock->wait_list); in six_lock_slowpath()
451 raw_spin_unlock(&lock->wait_lock); in six_lock_slowpath()
459 __six_lock_wakeup(lock, -ret - 1); in six_lock_slowpath()
463 if (six_optimistic_spin(lock, wait, type)) in six_lock_slowpath()
471 * wait->lock_acquired: pairs with the smp_store_release in in six_lock_slowpath()
474 if (smp_load_acquire(&wait->lock_acquired)) in six_lock_slowpath()
484 * acquired the lock - should_sleep_fn() might have in six_lock_slowpath()
488 raw_spin_lock(&lock->wait_lock); in six_lock_slowpath()
489 acquired = wait->lock_acquired; in six_lock_slowpath()
491 list_del(&wait->list); in six_lock_slowpath()
492 raw_spin_unlock(&lock->wait_lock); in six_lock_slowpath()
506 six_lock_wakeup(lock, atomic_read(&lock->state), SIX_LOCK_read); in six_lock_slowpath()
514 * six_lock_ip_waiter - take a lock, with full waitlist interface
517 * @wait: pointer to wait object, which will be added to lock's waitlist
527 * @wait object should be embedded into the struct that tracks held locks -
528 * which must also be accessible in a thread-safe way.
533 * When this function must block, @wait will be added to @lock's waitlist before
534 * calling trylock, and before calling @should_sleep_fn, and @wait will not be
538 * @wait.start_time will be monotonically increasing for any given waitlist, and
541 * Return: 0 on success, or the return code from @should_sleep_fn on failure.
544 struct six_lock_waiter *wait, in six_lock_ip_waiter() argument
550 wait->start_time = 0; in six_lock_ip_waiter()
553 six_acquire(&lock->dep_map, 0, type == SIX_LOCK_read, ip); in six_lock_ip_waiter()
556 : six_lock_slowpath(lock, type, wait, should_sleep_fn, p, ip); in six_lock_ip_waiter()
559 six_release(&lock->dep_map, ip); in six_lock_ip_waiter()
561 lock_acquired(&lock->dep_map, ip); in six_lock_ip_waiter()
573 lock->owner = NULL; in do_six_unlock_type()
576 lock->readers) { in do_six_unlock_type()
578 this_cpu_dec(*lock->readers); in do_six_unlock_type()
580 state = atomic_read(&lock->state); in do_six_unlock_type()
585 v += atomic_read(&lock->state) & SIX_LOCK_NOSPIN; in do_six_unlock_type()
587 EBUG_ON(!(atomic_read(&lock->state) & l[type].held_mask)); in do_six_unlock_type()
588 state = atomic_sub_return_release(v, &lock->state); in do_six_unlock_type()
595 * six_unlock_ip - drop a six lock
604 * six_lock_read(&foo->lock); read count 1
605 * six_lock_increment(&foo->lock, SIX_LOCK_read); read count 2
606 * six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 1
607 * six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 0
612 !(atomic_read(&lock->state) & SIX_LOCK_HELD_intent)); in six_unlock_ip()
615 lock->owner != current); in six_unlock_ip()
618 six_release(&lock->dep_map, ip); in six_unlock_ip()
620 lock->seq++; in six_unlock_ip()
623 lock->intent_lock_recurse) { in six_unlock_ip()
624 --lock->intent_lock_recurse; in six_unlock_ip()
633 * six_lock_downgrade - convert an intent lock to a read lock
636 * @lock will have read count incremented and intent count decremented
646 * six_lock_tryupgrade - attempt to convert read lock to an intent lock
649 * On success, @lock will have intent count incremented and read count
652 * Return: true on success, false on failure
656 u32 old = atomic_read(&lock->state), new; in six_lock_tryupgrade()
664 if (!lock->readers) { in six_lock_tryupgrade()
666 new -= l[SIX_LOCK_read].lock_val; in six_lock_tryupgrade()
670 } while (!atomic_try_cmpxchg_acquire(&lock->state, &old, new)); in six_lock_tryupgrade()
672 if (lock->readers) in six_lock_tryupgrade()
673 this_cpu_dec(*lock->readers); in six_lock_tryupgrade()
682 * six_trylock_convert - attempt to convert a held lock from one type to another
687 * On success, @lock will have intent count incremented and read count
690 * Return: true on success, false on failure
711 * six_lock_increment - increase held lock count on a lock that is already held
723 six_acquire(&lock->dep_map, 0, type == SIX_LOCK_read, _RET_IP_); in six_lock_increment()
729 if (lock->readers) { in six_lock_increment()
730 this_cpu_inc(*lock->readers); in six_lock_increment()
732 EBUG_ON(!(atomic_read(&lock->state) & in six_lock_increment()
735 atomic_add(l[type].lock_val, &lock->state); in six_lock_increment()
739 EBUG_ON(!(atomic_read(&lock->state) & SIX_LOCK_HELD_intent)); in six_lock_increment()
740 lock->intent_lock_recurse++; in six_lock_increment()
750 * six_lock_wakeup_all - wake up all waiters on @lock
753 * Wakeing up waiters will cause them to re-run should_sleep_fn, which may then
756 * This function is never needed in a bug-free program; it's only useful in
761 u32 state = atomic_read(&lock->state); in six_lock_wakeup_all()
768 raw_spin_lock(&lock->wait_lock); in six_lock_wakeup_all()
769 list_for_each_entry(w, &lock->wait_list, list) in six_lock_wakeup_all()
770 wake_up_process(w->task); in six_lock_wakeup_all()
771 raw_spin_unlock(&lock->wait_lock); in six_lock_wakeup_all()
776 * six_lock_counts - return held lock counts, for each lock type
779 * Return: the number of times a lock is held for read, intent and write.
785 ret.n[SIX_LOCK_read] = !lock->readers in six_lock_counts()
786 ? atomic_read(&lock->state) & SIX_LOCK_HELD_read in six_lock_counts()
788 ret.n[SIX_LOCK_intent] = !!(atomic_read(&lock->state) & SIX_LOCK_HELD_intent) + in six_lock_counts()
789 lock->intent_lock_recurse; in six_lock_counts()
790 ret.n[SIX_LOCK_write] = !!(atomic_read(&lock->state) & SIX_LOCK_HELD_write); in six_lock_counts()
797 * six_lock_readers_add - directly manipulate reader count of a lock
801 * When an upper layer is implementing lock reentrency, we may have both read
802 * and intent locks on the same lock.
804 * When we need to take a write lock, the read locks will cause self-deadlock,
805 * because six locks themselves do not track which read locks are held by the
806 * current thread and which are held by a different thread - it does no
807 * per-thread tracking of held locks.
810 * failed, count up its own read locks, subtract them, take the write lock, and
811 * then re-add them.
818 if (lock->readers) { in six_lock_readers_add()
819 this_cpu_add(*lock->readers, nr); in six_lock_readers_add()
821 EBUG_ON((int) (atomic_read(&lock->state) & SIX_LOCK_HELD_read) + nr < 0); in six_lock_readers_add()
823 atomic_add(nr, &lock->state); in six_lock_readers_add()
829 * six_lock_exit - release resources held by a lock prior to freeing
833 * required to free the percpu read counts.
837 WARN_ON(lock->readers && pcpu_read_count(lock)); in six_lock_exit()
838 WARN_ON(atomic_read(&lock->state) & SIX_LOCK_HELD_read); in six_lock_exit()
840 free_percpu(lock->readers); in six_lock_exit()
841 lock->readers = NULL; in six_lock_exit()
848 atomic_set(&lock->state, 0); in __six_lock_init()
849 raw_spin_lock_init(&lock->wait_lock); in __six_lock_init()
850 INIT_LIST_HEAD(&lock->wait_list); in __six_lock_init()
853 lockdep_init_map(&lock->dep_map, name, key, 0); in __six_lock_init()
863 * We don't return an error here on memory allocation failure in __six_lock_init()
865 * same semantics in non-percpu mode: callers can check for in __six_lock_init()
866 * failure if they wish by checking lock->readers, but generally in __six_lock_init()
869 lock->readers = alloc_percpu(unsigned); in __six_lock_init()