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