diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2017-10-01 15:02:47 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2017-10-01 15:02:47 -0400 |
commit | 1de47f3cb705e8c19fdd432eb704f5a588ccec48 (patch) | |
tree | e1c273a316d73eddfdf616c9588ad0e4511e8c23 /kernel | |
parent | 3d9d62b99befa8b4d490e13e605d2c3540b4b82b (diff) | |
parent | 9c29c31830a4eca724e137a9339137204bbb31be (diff) |
Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull locking fixes from Thomas Gleixner:
"Two fixes for locking:
- Plug a hole the pi_stat->owner serialization which was changed
recently and failed to fixup two usage sites.
- Prevent reordering of the rwsem_has_spinner() check vs the
decrement of rwsem count in up_write() which causes a missed
wakeup"
* 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
locking/rwsem-xadd: Fix missed wakeup due to reordering of load
futex: Fix pi_state->owner serialization
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/futex.c | 33 | ||||
-rw-r--r-- | kernel/locking/rwsem-xadd.c | 27 |
2 files changed, 49 insertions, 11 deletions
diff --git a/kernel/futex.c b/kernel/futex.c index 3d38eaf05492..0518a0bfc746 100644 --- a/kernel/futex.c +++ b/kernel/futex.c | |||
@@ -821,8 +821,6 @@ static void get_pi_state(struct futex_pi_state *pi_state) | |||
821 | /* | 821 | /* |
822 | * Drops a reference to the pi_state object and frees or caches it | 822 | * Drops a reference to the pi_state object and frees or caches it |
823 | * when the last reference is gone. | 823 | * when the last reference is gone. |
824 | * | ||
825 | * Must be called with the hb lock held. | ||
826 | */ | 824 | */ |
827 | static void put_pi_state(struct futex_pi_state *pi_state) | 825 | static void put_pi_state(struct futex_pi_state *pi_state) |
828 | { | 826 | { |
@@ -837,16 +835,22 @@ static void put_pi_state(struct futex_pi_state *pi_state) | |||
837 | * and has cleaned up the pi_state already | 835 | * and has cleaned up the pi_state already |
838 | */ | 836 | */ |
839 | if (pi_state->owner) { | 837 | if (pi_state->owner) { |
840 | raw_spin_lock_irq(&pi_state->owner->pi_lock); | 838 | struct task_struct *owner; |
841 | list_del_init(&pi_state->list); | ||
842 | raw_spin_unlock_irq(&pi_state->owner->pi_lock); | ||
843 | 839 | ||
844 | rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner); | 840 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
841 | owner = pi_state->owner; | ||
842 | if (owner) { | ||
843 | raw_spin_lock(&owner->pi_lock); | ||
844 | list_del_init(&pi_state->list); | ||
845 | raw_spin_unlock(&owner->pi_lock); | ||
846 | } | ||
847 | rt_mutex_proxy_unlock(&pi_state->pi_mutex, owner); | ||
848 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); | ||
845 | } | 849 | } |
846 | 850 | ||
847 | if (current->pi_state_cache) | 851 | if (current->pi_state_cache) { |
848 | kfree(pi_state); | 852 | kfree(pi_state); |
849 | else { | 853 | } else { |
850 | /* | 854 | /* |
851 | * pi_state->list is already empty. | 855 | * pi_state->list is already empty. |
852 | * clear pi_state->owner. | 856 | * clear pi_state->owner. |
@@ -907,13 +911,14 @@ void exit_pi_state_list(struct task_struct *curr) | |||
907 | raw_spin_unlock_irq(&curr->pi_lock); | 911 | raw_spin_unlock_irq(&curr->pi_lock); |
908 | 912 | ||
909 | spin_lock(&hb->lock); | 913 | spin_lock(&hb->lock); |
910 | 914 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); | |
911 | raw_spin_lock_irq(&curr->pi_lock); | 915 | raw_spin_lock(&curr->pi_lock); |
912 | /* | 916 | /* |
913 | * We dropped the pi-lock, so re-check whether this | 917 | * We dropped the pi-lock, so re-check whether this |
914 | * task still owns the PI-state: | 918 | * task still owns the PI-state: |
915 | */ | 919 | */ |
916 | if (head->next != next) { | 920 | if (head->next != next) { |
921 | raw_spin_unlock(&pi_state->pi_mutex.wait_lock); | ||
917 | spin_unlock(&hb->lock); | 922 | spin_unlock(&hb->lock); |
918 | continue; | 923 | continue; |
919 | } | 924 | } |
@@ -922,9 +927,10 @@ void exit_pi_state_list(struct task_struct *curr) | |||
922 | WARN_ON(list_empty(&pi_state->list)); | 927 | WARN_ON(list_empty(&pi_state->list)); |
923 | list_del_init(&pi_state->list); | 928 | list_del_init(&pi_state->list); |
924 | pi_state->owner = NULL; | 929 | pi_state->owner = NULL; |
925 | raw_spin_unlock_irq(&curr->pi_lock); | 930 | raw_spin_unlock(&curr->pi_lock); |
926 | 931 | ||
927 | get_pi_state(pi_state); | 932 | get_pi_state(pi_state); |
933 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); | ||
928 | spin_unlock(&hb->lock); | 934 | spin_unlock(&hb->lock); |
929 | 935 | ||
930 | rt_mutex_futex_unlock(&pi_state->pi_mutex); | 936 | rt_mutex_futex_unlock(&pi_state->pi_mutex); |
@@ -1208,6 +1214,10 @@ static int attach_to_pi_owner(u32 uval, union futex_key *key, | |||
1208 | 1214 | ||
1209 | WARN_ON(!list_empty(&pi_state->list)); | 1215 | WARN_ON(!list_empty(&pi_state->list)); |
1210 | list_add(&pi_state->list, &p->pi_state_list); | 1216 | list_add(&pi_state->list, &p->pi_state_list); |
1217 | /* | ||
1218 | * Assignment without holding pi_state->pi_mutex.wait_lock is safe | ||
1219 | * because there is no concurrency as the object is not published yet. | ||
1220 | */ | ||
1211 | pi_state->owner = p; | 1221 | pi_state->owner = p; |
1212 | raw_spin_unlock_irq(&p->pi_lock); | 1222 | raw_spin_unlock_irq(&p->pi_lock); |
1213 | 1223 | ||
@@ -2878,6 +2888,7 @@ retry: | |||
2878 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); | 2888 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
2879 | spin_unlock(&hb->lock); | 2889 | spin_unlock(&hb->lock); |
2880 | 2890 | ||
2891 | /* drops pi_state->pi_mutex.wait_lock */ | ||
2881 | ret = wake_futex_pi(uaddr, uval, pi_state); | 2892 | ret = wake_futex_pi(uaddr, uval, pi_state); |
2882 | 2893 | ||
2883 | put_pi_state(pi_state); | 2894 | put_pi_state(pi_state); |
diff --git a/kernel/locking/rwsem-xadd.c b/kernel/locking/rwsem-xadd.c index 02f660666ab8..1fefe6dcafd7 100644 --- a/kernel/locking/rwsem-xadd.c +++ b/kernel/locking/rwsem-xadd.c | |||
@@ -613,6 +613,33 @@ struct rw_semaphore *rwsem_wake(struct rw_semaphore *sem) | |||
613 | DEFINE_WAKE_Q(wake_q); | 613 | DEFINE_WAKE_Q(wake_q); |
614 | 614 | ||
615 | /* | 615 | /* |
616 | * __rwsem_down_write_failed_common(sem) | ||
617 | * rwsem_optimistic_spin(sem) | ||
618 | * osq_unlock(sem->osq) | ||
619 | * ... | ||
620 | * atomic_long_add_return(&sem->count) | ||
621 | * | ||
622 | * - VS - | ||
623 | * | ||
624 | * __up_write() | ||
625 | * if (atomic_long_sub_return_release(&sem->count) < 0) | ||
626 | * rwsem_wake(sem) | ||
627 | * osq_is_locked(&sem->osq) | ||
628 | * | ||
629 | * And __up_write() must observe !osq_is_locked() when it observes the | ||
630 | * atomic_long_add_return() in order to not miss a wakeup. | ||
631 | * | ||
632 | * This boils down to: | ||
633 | * | ||
634 | * [S.rel] X = 1 [RmW] r0 = (Y += 0) | ||
635 | * MB RMB | ||
636 | * [RmW] Y += 1 [L] r1 = X | ||
637 | * | ||
638 | * exists (r0=1 /\ r1=0) | ||
639 | */ | ||
640 | smp_rmb(); | ||
641 | |||
642 | /* | ||
616 | * If a spinner is present, it is not necessary to do the wakeup. | 643 | * If a spinner is present, it is not necessary to do the wakeup. |
617 | * Try to do wakeup only if the trylock succeeds to minimize | 644 | * Try to do wakeup only if the trylock succeeds to minimize |
618 | * spinlock contention which may introduce too much delay in the | 645 | * spinlock contention which may introduce too much delay in the |