1 // SPDX-License-Identifier: GPL-2.0
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>
14 #include <trace/events/lock.h>
19 #define EBUG_ON(cond) BUG_ON(cond)
21 #define EBUG_ON(cond) do {} while (0)
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)
27 static void do_six_unlock_type(struct six_lock *lock, enum six_lock_type type);
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)
37 struct six_lock_vals {
38 /* Value we add to the lock in order to take the lock: */
41 /* If the lock has this value (used as a mask), taking the lock fails: */
44 /* Mask that indicates lock is held for this type: */
47 /* Waitlist we wakeup when releasing the lock: */
48 enum six_lock_type unlock_wakeup;
51 static const struct six_lock_vals l[] = {
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,
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,
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,
72 static inline void six_set_bitmask(struct six_lock *lock, u32 mask)
74 if ((atomic_read(&lock->state) & mask) != mask)
75 atomic_or(mask, &lock->state);
78 static inline void six_clear_bitmask(struct six_lock *lock, u32 mask)
80 if (atomic_read(&lock->state) & mask)
81 atomic_and(~mask, &lock->state);
84 static inline void six_set_owner(struct six_lock *lock, enum six_lock_type type,
85 u32 old, struct task_struct *owner)
87 if (type != SIX_LOCK_intent)
90 if (!(old & SIX_LOCK_HELD_intent)) {
94 EBUG_ON(lock->owner != current);
98 static inline unsigned pcpu_read_count(struct six_lock *lock)
100 unsigned read_count = 0;
103 for_each_possible_cpu(cpu)
104 read_count += *per_cpu_ptr(lock->readers, cpu);
109 * __do_six_trylock() - main trylock routine
111 * Returns 1 on success, 0 on failure
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.
117 static int __do_six_trylock(struct six_lock *lock, enum six_lock_type type,
118 struct task_struct *task, bool try)
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)));
128 * Percpu reader mode:
130 * The basic idea behind this algorithm is that you can implement a lock
131 * between two threads without any atomics, just memory barriers:
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".
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.
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
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.
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
154 if (type == SIX_LOCK_read && lock->readers) {
156 this_cpu_inc(*lock->readers); /* signal that we own lock */
160 old = atomic_read(&lock->state);
161 ret = !(old & l[type].lock_fail);
163 this_cpu_sub(*lock->readers, !ret);
168 if (atomic_read(&lock->state) & SIX_LOCK_WAITING_write)
169 ret = -1 - SIX_LOCK_write;
171 } else if (type == SIX_LOCK_write && lock->readers) {
173 atomic_add(SIX_LOCK_HELD_write, &lock->state);
174 smp_mb__after_atomic();
177 ret = !pcpu_read_count(lock);
180 old = atomic_sub_return(SIX_LOCK_HELD_write, &lock->state);
181 if (old & SIX_LOCK_WAITING_read)
182 ret = -1 - SIX_LOCK_read;
185 old = atomic_read(&lock->state);
187 ret = !(old & l[type].lock_fail);
188 if (!ret || (type == SIX_LOCK_write && !try)) {
192 } while (!atomic_try_cmpxchg_acquire(&lock->state, &old, old + l[type].lock_val));
194 EBUG_ON(ret && !(atomic_read(&lock->state) & l[type].held_mask));
198 six_set_owner(lock, type, old, task);
200 EBUG_ON(type == SIX_LOCK_write && try && ret <= 0 &&
201 (atomic_read(&lock->state) & SIX_LOCK_HELD_write));
206 static void __six_lock_wakeup(struct six_lock *lock, enum six_lock_type lock_type)
208 struct six_lock_waiter *w, *next;
209 struct task_struct *task;
215 raw_spin_lock(&lock->wait_lock);
217 list_for_each_entry_safe(w, next, &lock->wait_list, list) {
218 if (w->lock_want != lock_type)
221 if (saw_one && lock_type != SIX_LOCK_read)
225 ret = __do_six_trylock(lock, lock_type, w->task, false);
230 * Similar to percpu_rwsem_wake_function(), we need to guard
231 * against the wakee noticing w->lock_acquired, returning, and
232 * then exiting before we do the wakeup:
234 task = get_task_struct(w->task);
235 __list_del(w->list.prev, w->list.next);
237 * The release barrier here ensures the ordering of the
238 * __list_del before setting w->lock_acquired; @w is on the
239 * stack of the thread doing the waiting and will be reused
240 * after it sees w->lock_acquired with no other locking:
241 * pairs with smp_load_acquire() in six_lock_slowpath()
243 smp_store_release(&w->lock_acquired, true);
244 wake_up_process(task);
245 put_task_struct(task);
248 six_clear_bitmask(lock, SIX_LOCK_WAITING_read << lock_type);
250 raw_spin_unlock(&lock->wait_lock);
253 lock_type = -ret - 1;
259 static void six_lock_wakeup(struct six_lock *lock, u32 state,
260 enum six_lock_type lock_type)
262 if (lock_type == SIX_LOCK_write && (state & SIX_LOCK_HELD_read))
265 if (!(state & (SIX_LOCK_WAITING_read << lock_type)))
268 __six_lock_wakeup(lock, lock_type);
272 static bool do_six_trylock(struct six_lock *lock, enum six_lock_type type, bool try)
276 ret = __do_six_trylock(lock, type, current, try);
278 __six_lock_wakeup(lock, -ret - 1);
284 * six_trylock_ip - attempt to take a six lock without blocking
285 * @lock: lock to take
286 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write
287 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_
289 * Return: true on success, false on failure.
291 bool six_trylock_ip(struct six_lock *lock, enum six_lock_type type, unsigned long ip)
293 if (!do_six_trylock(lock, type, true))
296 if (type != SIX_LOCK_write)
297 six_acquire(&lock->dep_map, 1, type == SIX_LOCK_read, ip);
300 EXPORT_SYMBOL_GPL(six_trylock_ip);
303 * six_relock_ip - attempt to re-take a lock that was held previously
304 * @lock: lock to take
305 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write
306 * @seq: lock sequence number obtained from six_lock_seq() while lock was
308 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_
310 * Return: true on success, false on failure.
312 bool six_relock_ip(struct six_lock *lock, enum six_lock_type type,
313 unsigned seq, unsigned long ip)
315 if (six_lock_seq(lock) != seq || !six_trylock_ip(lock, type, ip))
318 if (six_lock_seq(lock) != seq) {
319 six_unlock_ip(lock, type, ip);
325 EXPORT_SYMBOL_GPL(six_relock_ip);
327 #ifdef CONFIG_BCACHEFS_SIX_OPTIMISTIC_SPIN
329 static inline bool six_owner_running(struct six_lock *lock)
332 * When there's no owner, we might have preempted between the owner
333 * acquiring the lock and setting the owner field. If we're an RT task
334 * that will live-lock because we won't let the owner complete.
337 struct task_struct *owner = READ_ONCE(lock->owner);
338 bool ret = owner ? owner_on_cpu(owner) : !rt_task(current);
344 static inline bool six_optimistic_spin(struct six_lock *lock,
345 struct six_lock_waiter *wait,
346 enum six_lock_type type)
351 if (type == SIX_LOCK_write)
354 if (lock->wait_list.next != &wait->list)
357 if (atomic_read(&lock->state) & SIX_LOCK_NOSPIN)
361 end_time = sched_clock() + 10 * NSEC_PER_USEC;
363 while (!need_resched() && six_owner_running(lock)) {
365 * Ensures that writes to the waitlist entry happen after we see
366 * wait->lock_acquired: pairs with the smp_store_release in
369 if (smp_load_acquire(&wait->lock_acquired)) {
374 if (!(++loop & 0xf) && (time_after64(sched_clock(), end_time))) {
375 six_set_bitmask(lock, SIX_LOCK_NOSPIN);
380 * The cpu_relax() call is a compiler barrier which forces
381 * everything in this loop to be re-loaded. We don't need
382 * memory barriers as we'll eventually observe the right
383 * values at the cost of a few extra spins.
392 #else /* CONFIG_LOCK_SPIN_ON_OWNER */
394 static inline bool six_optimistic_spin(struct six_lock *lock,
395 struct six_lock_waiter *wait,
396 enum six_lock_type type)
404 static int six_lock_slowpath(struct six_lock *lock, enum six_lock_type type,
405 struct six_lock_waiter *wait,
406 six_lock_should_sleep_fn should_sleep_fn, void *p,
411 if (type == SIX_LOCK_write) {
412 EBUG_ON(atomic_read(&lock->state) & SIX_LOCK_HELD_write);
413 atomic_add(SIX_LOCK_HELD_write, &lock->state);
414 smp_mb__after_atomic();
417 trace_contention_begin(lock, 0);
418 lock_contended(&lock->dep_map, ip);
420 wait->task = current;
421 wait->lock_want = type;
422 wait->lock_acquired = false;
424 raw_spin_lock(&lock->wait_lock);
425 six_set_bitmask(lock, SIX_LOCK_WAITING_read << type);
427 * Retry taking the lock after taking waitlist lock, in case we raced
430 ret = __do_six_trylock(lock, type, current, false);
432 wait->start_time = local_clock();
434 if (!list_empty(&lock->wait_list)) {
435 struct six_lock_waiter *last =
436 list_last_entry(&lock->wait_list,
437 struct six_lock_waiter, list);
439 if (time_before_eq64(wait->start_time, last->start_time))
440 wait->start_time = last->start_time + 1;
443 list_add_tail(&wait->list, &lock->wait_list);
445 raw_spin_unlock(&lock->wait_lock);
447 if (unlikely(ret > 0)) {
452 if (unlikely(ret < 0)) {
453 __six_lock_wakeup(lock, -ret - 1);
457 if (six_optimistic_spin(lock, wait, type))
461 set_current_state(TASK_UNINTERRUPTIBLE);
464 * Ensures that writes to the waitlist entry happen after we see
465 * wait->lock_acquired: pairs with the smp_store_release in
468 if (smp_load_acquire(&wait->lock_acquired))
471 ret = should_sleep_fn ? should_sleep_fn(lock, p) : 0;
476 * If should_sleep_fn() returns an error, we are
477 * required to return that error even if we already
478 * acquired the lock - should_sleep_fn() might have
479 * modified external state (e.g. when the deadlock cycle
480 * detector in bcachefs issued a transaction restart)
482 raw_spin_lock(&lock->wait_lock);
483 acquired = wait->lock_acquired;
485 list_del(&wait->list);
486 raw_spin_unlock(&lock->wait_lock);
488 if (unlikely(acquired))
489 do_six_unlock_type(lock, type);
496 __set_current_state(TASK_RUNNING);
498 if (ret && type == SIX_LOCK_write) {
499 six_clear_bitmask(lock, SIX_LOCK_HELD_write);
500 six_lock_wakeup(lock, atomic_read(&lock->state), SIX_LOCK_read);
502 trace_contention_end(lock, 0);
508 * six_lock_ip_waiter - take a lock, with full waitlist interface
509 * @lock: lock to take
510 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write
511 * @wait: pointer to wait object, which will be added to lock's waitlist
512 * @should_sleep_fn: callback run after adding to waitlist, immediately prior
514 * @p: passed through to @should_sleep_fn
515 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_
517 * This is the most general six_lock() variant, with parameters to support full
518 * cycle detection for deadlock avoidance.
520 * The code calling this function must implement tracking of held locks, and the
521 * @wait object should be embedded into the struct that tracks held locks -
522 * which must also be accessible in a thread-safe way.
524 * @should_sleep_fn should invoke the cycle detector; it should walk each
525 * lock's waiters, and for each waiter recursively walk their held locks.
527 * When this function must block, @wait will be added to @lock's waitlist before
528 * calling trylock, and before calling @should_sleep_fn, and @wait will not be
529 * removed from the lock waitlist until the lock has been successfully acquired,
532 * @wait.start_time will be monotonically increasing for any given waitlist, and
533 * thus may be used as a loop cursor.
535 * Return: 0 on success, or the return code from @should_sleep_fn on failure.
537 int six_lock_ip_waiter(struct six_lock *lock, enum six_lock_type type,
538 struct six_lock_waiter *wait,
539 six_lock_should_sleep_fn should_sleep_fn, void *p,
544 wait->start_time = 0;
546 if (type != SIX_LOCK_write)
547 six_acquire(&lock->dep_map, 0, type == SIX_LOCK_read, ip);
549 ret = do_six_trylock(lock, type, true) ? 0
550 : six_lock_slowpath(lock, type, wait, should_sleep_fn, p, ip);
552 if (ret && type != SIX_LOCK_write)
553 six_release(&lock->dep_map, ip);
555 lock_acquired(&lock->dep_map, ip);
559 EXPORT_SYMBOL_GPL(six_lock_ip_waiter);
562 static void do_six_unlock_type(struct six_lock *lock, enum six_lock_type type)
566 if (type == SIX_LOCK_intent)
569 if (type == SIX_LOCK_read &&
571 smp_mb(); /* unlock barrier */
572 this_cpu_dec(*lock->readers);
573 smp_mb(); /* between unlocking and checking for waiters */
574 state = atomic_read(&lock->state);
576 u32 v = l[type].lock_val;
578 if (type != SIX_LOCK_read)
579 v += atomic_read(&lock->state) & SIX_LOCK_NOSPIN;
581 EBUG_ON(!(atomic_read(&lock->state) & l[type].held_mask));
582 state = atomic_sub_return_release(v, &lock->state);
585 six_lock_wakeup(lock, state, l[type].unlock_wakeup);
589 * six_unlock_ip - drop a six lock
590 * @lock: lock to unlock
591 * @type: SIX_LOCK_read, SIX_LOCK_intent, or SIX_LOCK_write
592 * @ip: ip parameter for lockdep/lockstat, i.e. _THIS_IP_
594 * When a lock is held multiple times (because six_lock_incement()) was used),
595 * this decrements the 'lock held' counter by one.
598 * six_lock_read(&foo->lock); read count 1
599 * six_lock_increment(&foo->lock, SIX_LOCK_read); read count 2
600 * six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 1
601 * six_lock_unlock(&foo->lock, SIX_LOCK_read); read count 0
603 void six_unlock_ip(struct six_lock *lock, enum six_lock_type type, unsigned long ip)
605 EBUG_ON(type == SIX_LOCK_write &&
606 !(atomic_read(&lock->state) & SIX_LOCK_HELD_intent));
607 EBUG_ON((type == SIX_LOCK_write ||
608 type == SIX_LOCK_intent) &&
609 lock->owner != current);
611 if (type != SIX_LOCK_write)
612 six_release(&lock->dep_map, ip);
616 if (type == SIX_LOCK_intent &&
617 lock->intent_lock_recurse) {
618 --lock->intent_lock_recurse;
622 do_six_unlock_type(lock, type);
624 EXPORT_SYMBOL_GPL(six_unlock_ip);
627 * six_lock_downgrade - convert an intent lock to a read lock
628 * @lock: lock to dowgrade
630 * @lock will have read count incremented and intent count decremented
632 void six_lock_downgrade(struct six_lock *lock)
634 six_lock_increment(lock, SIX_LOCK_read);
635 six_unlock_intent(lock);
637 EXPORT_SYMBOL_GPL(six_lock_downgrade);
640 * six_lock_tryupgrade - attempt to convert read lock to an intent lock
641 * @lock: lock to upgrade
643 * On success, @lock will have intent count incremented and read count
646 * Return: true on success, false on failure
648 bool six_lock_tryupgrade(struct six_lock *lock)
650 u32 old = atomic_read(&lock->state), new;
655 if (new & SIX_LOCK_HELD_intent)
658 if (!lock->readers) {
659 EBUG_ON(!(new & SIX_LOCK_HELD_read));
660 new -= l[SIX_LOCK_read].lock_val;
663 new |= SIX_LOCK_HELD_intent;
664 } while (!atomic_try_cmpxchg_acquire(&lock->state, &old, new));
667 this_cpu_dec(*lock->readers);
669 six_set_owner(lock, SIX_LOCK_intent, old, current);
673 EXPORT_SYMBOL_GPL(six_lock_tryupgrade);
676 * six_trylock_convert - attempt to convert a held lock from one type to another
677 * @lock: lock to upgrade
678 * @from: SIX_LOCK_read or SIX_LOCK_intent
679 * @to: SIX_LOCK_read or SIX_LOCK_intent
681 * On success, @lock will have intent count incremented and read count
684 * Return: true on success, false on failure
686 bool six_trylock_convert(struct six_lock *lock,
687 enum six_lock_type from,
688 enum six_lock_type to)
690 EBUG_ON(to == SIX_LOCK_write || from == SIX_LOCK_write);
695 if (to == SIX_LOCK_read) {
696 six_lock_downgrade(lock);
699 return six_lock_tryupgrade(lock);
702 EXPORT_SYMBOL_GPL(six_trylock_convert);
705 * six_lock_increment - increase held lock count on a lock that is already held
706 * @lock: lock to increment
707 * @type: SIX_LOCK_read or SIX_LOCK_intent
709 * @lock must already be held, with a lock type that is greater than or equal to
712 * A corresponding six_unlock_type() call will be required for @lock to be fully
715 void six_lock_increment(struct six_lock *lock, enum six_lock_type type)
717 six_acquire(&lock->dep_map, 0, type == SIX_LOCK_read, _RET_IP_);
719 /* XXX: assert already locked, and that we don't overflow: */
724 this_cpu_inc(*lock->readers);
726 EBUG_ON(!(atomic_read(&lock->state) &
728 SIX_LOCK_HELD_intent)));
729 atomic_add(l[type].lock_val, &lock->state);
732 case SIX_LOCK_intent:
733 EBUG_ON(!(atomic_read(&lock->state) & SIX_LOCK_HELD_intent));
734 lock->intent_lock_recurse++;
741 EXPORT_SYMBOL_GPL(six_lock_increment);
744 * six_lock_wakeup_all - wake up all waiters on @lock
745 * @lock: lock to wake up waiters for
747 * Wakeing up waiters will cause them to re-run should_sleep_fn, which may then
748 * abort the lock operation.
750 * This function is never needed in a bug-free program; it's only useful in
751 * debug code, e.g. to determine if a cycle detector is at fault.
753 void six_lock_wakeup_all(struct six_lock *lock)
755 u32 state = atomic_read(&lock->state);
756 struct six_lock_waiter *w;
758 six_lock_wakeup(lock, state, SIX_LOCK_read);
759 six_lock_wakeup(lock, state, SIX_LOCK_intent);
760 six_lock_wakeup(lock, state, SIX_LOCK_write);
762 raw_spin_lock(&lock->wait_lock);
763 list_for_each_entry(w, &lock->wait_list, list)
764 wake_up_process(w->task);
765 raw_spin_unlock(&lock->wait_lock);
767 EXPORT_SYMBOL_GPL(six_lock_wakeup_all);
770 * six_lock_counts - return held lock counts, for each lock type
771 * @lock: lock to return counters for
773 * Return: the number of times a lock is held for read, intent and write.
775 struct six_lock_count six_lock_counts(struct six_lock *lock)
777 struct six_lock_count ret;
779 ret.n[SIX_LOCK_read] = !lock->readers
780 ? atomic_read(&lock->state) & SIX_LOCK_HELD_read
781 : pcpu_read_count(lock);
782 ret.n[SIX_LOCK_intent] = !!(atomic_read(&lock->state) & SIX_LOCK_HELD_intent) +
783 lock->intent_lock_recurse;
784 ret.n[SIX_LOCK_write] = !!(atomic_read(&lock->state) & SIX_LOCK_HELD_write);
788 EXPORT_SYMBOL_GPL(six_lock_counts);
791 * six_lock_readers_add - directly manipulate reader count of a lock
792 * @lock: lock to add/subtract readers for
793 * @nr: reader count to add/subtract
795 * When an upper layer is implementing lock reentrency, we may have both read
796 * and intent locks on the same lock.
798 * When we need to take a write lock, the read locks will cause self-deadlock,
799 * because six locks themselves do not track which read locks are held by the
800 * current thread and which are held by a different thread - it does no
801 * per-thread tracking of held locks.
803 * The upper layer that is tracking held locks may however, if trylock() has
804 * failed, count up its own read locks, subtract them, take the write lock, and
807 * As in any other situation when taking a write lock, @lock must be held for
808 * intent one (or more) times, so @lock will never be left unlocked.
810 void six_lock_readers_add(struct six_lock *lock, int nr)
813 this_cpu_add(*lock->readers, nr);
815 EBUG_ON((int) (atomic_read(&lock->state) & SIX_LOCK_HELD_read) + nr < 0);
816 /* reader count starts at bit 0 */
817 atomic_add(nr, &lock->state);
820 EXPORT_SYMBOL_GPL(six_lock_readers_add);
823 * six_lock_exit - release resources held by a lock prior to freeing
824 * @lock: lock to exit
826 * When a lock was initialized in percpu mode (SIX_OLCK_INIT_PCPU), this is
827 * required to free the percpu read counts.
829 void six_lock_exit(struct six_lock *lock)
831 WARN_ON(lock->readers && pcpu_read_count(lock));
832 WARN_ON(atomic_read(&lock->state) & SIX_LOCK_HELD_read);
834 free_percpu(lock->readers);
835 lock->readers = NULL;
837 EXPORT_SYMBOL_GPL(six_lock_exit);
839 void __six_lock_init(struct six_lock *lock, const char *name,
840 struct lock_class_key *key, enum six_lock_init_flags flags)
842 atomic_set(&lock->state, 0);
843 raw_spin_lock_init(&lock->wait_lock);
844 INIT_LIST_HEAD(&lock->wait_list);
845 #ifdef CONFIG_DEBUG_LOCK_ALLOC
846 debug_check_no_locks_freed((void *) lock, sizeof(*lock));
847 lockdep_init_map(&lock->dep_map, name, key, 0);
851 * Don't assume that we have real percpu variables available in
855 if (flags & SIX_LOCK_INIT_PCPU) {
857 * We don't return an error here on memory allocation failure
858 * since percpu is an optimization, and locks will work with the
859 * same semantics in non-percpu mode: callers can check for
860 * failure if they wish by checking lock->readers, but generally
861 * will not want to treat it as an error.
863 lock->readers = alloc_percpu(unsigned);
867 EXPORT_SYMBOL_GPL(__six_lock_init);