| @@ -147,67 +147,6 @@ int __read_mostly futex_cmpxchg_enabled; · 146 unmodified lines |
| 147 | 147 | #endif |
| 148 | 148 | |
| 149 | 149 | |
| 150 | | −/* |
| 151 | | − * Priority Inheritance state: |
| 152 | | − */ |
| 153 | | −struct futex_pi_state { |
| 154 | | − /* |
| 155 | | − * list of 'owned' pi_state instances - these have to be |
| 156 | | − * cleaned up in do_exit() if the task exits prematurely: |
| 157 | | − */ |
| 158 | | − struct list_head list; |
| 159 | | − |
| 160 | | − /* |
| 161 | | − * The PI object: |
| 162 | | − */ |
| 163 | | − struct rt_mutex_base pi_mutex; |
| 164 | | − |
| 165 | | − struct task_struct *owner; |
| 166 | | − refcount_t refcount; |
| 167 | | − |
| 168 | | − union futex_key key; |
| 169 | | −} __randomize_layout; |
| 170 | | − |
| 171 | | −/** |
| 172 | | − * struct futex_q - The hashed futex queue entry, one per waiting task |
| 173 | | − * @list: priority-sorted list of tasks waiting on this futex |
| 174 | | − * @task: the task waiting on the futex |
| 175 | | − * @lock_ptr: the hash bucket lock |
| 176 | | − * @key: the key the futex is hashed on |
| 177 | | − * @pi_state: optional priority inheritance state |
| 178 | | − * @rt_waiter: rt_waiter storage for use with requeue_pi |
| 179 | | − * @requeue_pi_key: the requeue_pi target futex key |
| 180 | | − * @bitset: bitset for the optional bitmasked wakeup |
| 181 | | − * @requeue_state: State field for futex_requeue_pi() |
| 182 | | − * @requeue_wait: RCU wait for futex_requeue_pi() (RT only) |
| 183 | | − * |
| 184 | | − * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so |
| 185 | | − * we can wake only the relevant ones (hashed queues may be shared). |
| 186 | | − * |
| 187 | | − * A futex_q has a woken state, just like tasks have TASK_RUNNING. |
| 188 | | − * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. |
| 189 | | − * The order of wakeup is always to make the first condition true, then |
| 190 | | − * the second. |
| 191 | | − * |
| 192 | | − * PI futexes are typically woken before they are removed from the hash list via |
| 193 | | − * the rt_mutex code. See futex_unqueue_pi(). |
| 194 | | − */ |
| 195 | | −struct futex_q { |
| 196 | | − struct plist_node list; |
| 197 | | − |
| 198 | | − struct task_struct *task; |
| 199 | | − spinlock_t *lock_ptr; |
| 200 | | − union futex_key key; |
| 201 | | − struct futex_pi_state *pi_state; |
| 202 | | − struct rt_mutex_waiter *rt_waiter; |
| 203 | | − union futex_key *requeue_pi_key; |
| 204 | | − u32 bitset; |
| 205 | | − atomic_t requeue_state; |
| 206 | | −#ifdef CONFIG_PREEMPT_RT |
| 207 | | − struct rcuwait requeue_wait; |
| 208 | | −#endif |
| 209 | | −} __randomize_layout; |
| 210 | | − |
| 211 | 150 | /* |
| 212 | 151 | * On PREEMPT_RT, the hash bucket lock is a 'sleeping' spinlock with an |
| 213 | 152 | * underlying rtmutex. The task which is about to be requeued could have |
| @@ -259,24 +198,13 @@ enum { · 45 unmodified lines |
| 259 | 198 | Q_REQUEUE_PI_LOCKED, |
| 260 | 199 | }; |
| 261 | 200 | |
| 262 | | −static const struct futex_q futex_q_init = { |
| 201 | +const struct futex_q futex_q_init = { |
| 263 | 202 | /* list gets initialized in futex_queue()*/ |
| 264 | 203 | .key = FUTEX_KEY_INIT, |
| 265 | 204 | .bitset = FUTEX_BITSET_MATCH_ANY, |
| 266 | 205 | .requeue_state = ATOMIC_INIT(Q_REQUEUE_PI_NONE), |
| 267 | 206 | }; |
| 268 | 207 | |
| 269 | | −/* |
| 270 | | − * Hash buckets are shared by all the futex_keys that hash to the same |
| 271 | | − * location. Each key may have multiple futex_q structures, one for each task |
| 272 | | − * waiting on a futex. |
| 273 | | − */ |
| 274 | | −struct futex_hash_bucket { |
| 275 | | − atomic_t waiters; |
| 276 | | − spinlock_t lock; |
| 277 | | − struct plist_head chain; |
| 278 | | −} ____cacheline_aligned_in_smp; |
| 279 | | − |
| 280 | 208 | /* |
| 281 | 209 | * The base of the bucket array and its size are always used together |
| 282 | 210 | * (after initialization only in futex_hash()), so ensure that they |
| @@ -386,7 +314,7 @@ static inline int hb_waiters_pending(struct futex_hash_bucket *hb) · 103 unmodified lines |
| 386 | 314 | * We hash on the keys returned from get_futex_key (see below) and return the |
| 387 | 315 | * corresponding hash bucket in the global hash. |
| 388 | 316 | */ |
| 389 | | −static struct futex_hash_bucket *futex_hash(union futex_key *key) |
| 317 | +struct futex_hash_bucket *futex_hash(union futex_key *key) |
| 390 | 318 | { |
| 391 | 319 | u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4, |
| 392 | 320 | key->both.offset); |
| @@ -410,11 +338,6 @@ static inline int match_futex(union futex_key *key1, union futex_key *key2) · 17 unmodified lines |
| 410 | 338 | && key1->both.offset == key2->both.offset); |
| 411 | 339 | } |
| 412 | 340 | |
| 413 | | −enum futex_access { |
| 414 | | − FUTEX_READ, |
| 415 | | − FUTEX_WRITE |
| 416 | | −}; |
| 417 | | − |
| 418 | 341 | /** |
| 419 | 342 | * futex_setup_timer - set up the sleeping hrtimer. |
| 420 | 343 | * @time: ptr to the given timeout value |
| @@ -425,7 +348,7 @@ enum futex_access { · 4 unmodified lines |
| 425 | 348 | * Return: Initialized hrtimer_sleeper structure or NULL if no timeout |
| 426 | 349 | * value given |
| 427 | 350 | */ |
| 428 | | −static inline struct hrtimer_sleeper * |
| 351 | +struct hrtimer_sleeper * |
| 429 | 352 | futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout, |
| 430 | 353 | int flags, u64 range_ns) |
| 431 | 354 | { |
| @@ -511,8 +434,8 @@ static u64 get_inode_sequence_number(struct inode *inode) · 79 unmodified lines |
| 511 | 434 | * |
| 512 | 435 | * lock_page() might sleep, the caller should not hold a spinlock. |
| 513 | 436 | */ |
| 514 | | −static int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key, |
| 515 | | − enum futex_access rw) |
| 437 | +int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key, |
| 438 | + enum futex_access rw) |
| 516 | 439 | { |
| 517 | 440 | unsigned long address = (unsigned long)uaddr; |
| 518 | 441 | struct mm_struct *mm = current->mm; |
| @@ -700,7 +623,7 @@ out: · 181 unmodified lines |
| 700 | 623 | * disabled section so we can as well avoid the #PF overhead by |
| 701 | 624 | * calling get_user_pages() right away. |
| 702 | 625 | */ |
| 703 | | −static int fault_in_user_writeable(u32 __user *uaddr) |
| 626 | +int fault_in_user_writeable(u32 __user *uaddr) |
| 704 | 627 | { |
| 705 | 628 | struct mm_struct *mm = current->mm; |
| 706 | 629 | int ret; |
| @@ -720,8 +643,7 @@ static int fault_in_user_writeable(u32 __user *uaddr) · 13 unmodified lines |
| 720 | 643 | * |
| 721 | 644 | * Must be called with the hb lock held. |
| 722 | 645 | */ |
| 723 | | −static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, |
| 724 | | − union futex_key *key) |
| 646 | +struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, union futex_key *key) |
| 725 | 647 | { |
| 726 | 648 | struct futex_q *this; |
| 727 | 649 | |
| @@ -732,8 +654,7 @@ static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, · 4 unmodified lines |
| 732 | 654 | return NULL; |
| 733 | 655 | } |
| 734 | 656 | |
| 735 | | −static int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, |
| 736 | | − u32 uval, u32 newval) |
| 657 | +int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, u32 uval, u32 newval) |
| 737 | 658 | { |
| 738 | 659 | int ret; |
| 739 | 660 | |
| @@ -744,7 +665,7 @@ static int futex_cmpxchg_value_locked(u32 *curval, u32 __user *uaddr, · 4 unmodified lines |
| 744 | 665 | return ret; |
| 745 | 666 | } |
| 746 | 667 | |
| 747 | | −static int futex_get_value_locked(u32 *dest, u32 __user *from) |
| 668 | +int futex_get_value_locked(u32 *dest, u32 __user *from) |
| 748 | 669 | { |
| 749 | 670 | int ret; |
| 750 | 671 | |
| @@ -755,399 +676,6 @@ static int futex_get_value_locked(u32 *dest, u32 __user *from) · 4 unmodified lines |
| 755 | 676 | return ret ? -EFAULT : 0; |
| 756 | 677 | } |
| 757 | 678 | |
| 758 | | − |
| 759 | | −/* |
| 760 | | − * PI code: |
| 761 | | − */ |
| 762 | | −static int refill_pi_state_cache(void) |
| 763 | | −{ |
| 764 | | − struct futex_pi_state *pi_state; |
| 765 | | − |
| 766 | | − if (likely(current->pi_state_cache)) |
| 767 | | − return 0; |
| 768 | | − |
| 769 | | − pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL); |
| 770 | | − |
| 771 | | − if (!pi_state) |
| 772 | | − return -ENOMEM; |
| 773 | | − |
| 774 | | − INIT_LIST_HEAD(&pi_state->list); |
| 775 | | − /* pi_mutex gets initialized later */ |
| 776 | | − pi_state->owner = NULL; |
| 777 | | − refcount_set(&pi_state->refcount, 1); |
| 778 | | − pi_state->key = FUTEX_KEY_INIT; |
| 779 | | − |
| 780 | | − current->pi_state_cache = pi_state; |
| 781 | | − |
| 782 | | − return 0; |
| 783 | | −} |
| 784 | | − |
| 785 | | −static struct futex_pi_state *alloc_pi_state(void) |
| 786 | | −{ |
| 787 | | − struct futex_pi_state *pi_state = current->pi_state_cache; |
| 788 | | − |
| 789 | | − WARN_ON(!pi_state); |
| 790 | | − current->pi_state_cache = NULL; |
| 791 | | − |
| 792 | | − return pi_state; |
| 793 | | −} |
| 794 | | − |
| 795 | | −static void pi_state_update_owner(struct futex_pi_state *pi_state, |
| 796 | | − struct task_struct *new_owner) |
| 797 | | −{ |
| 798 | | − struct task_struct *old_owner = pi_state->owner; |
| 799 | | − |
| 800 | | − lockdep_assert_held(&pi_state->pi_mutex.wait_lock); |
| 801 | | − |
| 802 | | − if (old_owner) { |
| 803 | | − raw_spin_lock(&old_owner->pi_lock); |
| 804 | | − WARN_ON(list_empty(&pi_state->list)); |
| 805 | | − list_del_init(&pi_state->list); |
| 806 | | − raw_spin_unlock(&old_owner->pi_lock); |
| 807 | | − } |
| 808 | | − |
| 809 | | − if (new_owner) { |
| 810 | | − raw_spin_lock(&new_owner->pi_lock); |
| 811 | | − WARN_ON(!list_empty(&pi_state->list)); |
| 812 | | − list_add(&pi_state->list, &new_owner->pi_state_list); |
| 813 | | − pi_state->owner = new_owner; |
| 814 | | − raw_spin_unlock(&new_owner->pi_lock); |
| 815 | | − } |
| 816 | | −} |
| 817 | | − |
| 818 | | −static void get_pi_state(struct futex_pi_state *pi_state) |
| 819 | | −{ |
| 820 | | − WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount)); |
| 821 | | −} |
| 822 | | − |
| 823 | | −/* |
| 824 | | − * Drops a reference to the pi_state object and frees or caches it |
| 825 | | − * when the last reference is gone. |
| 826 | | − */ |
| 827 | | −static void put_pi_state(struct futex_pi_state *pi_state) |
| 828 | | −{ |
| 829 | | − if (!pi_state) |
| 830 | | − return; |
| 831 | | − |
| 832 | | − if (!refcount_dec_and_test(&pi_state->refcount)) |
| 833 | | − return; |
| 834 | | − |
| 835 | | − /* |
| 836 | | − * If pi_state->owner is NULL, the owner is most probably dying |
| 837 | | − * and has cleaned up the pi_state already |
| 838 | | − */ |
| 839 | | − if (pi_state->owner) { |
| 840 | | − unsigned long flags; |
| 841 | | − |
| 842 | | − raw_spin_lock_irqsave(&pi_state->pi_mutex.wait_lock, flags); |
| 843 | | − pi_state_update_owner(pi_state, NULL); |
| 844 | | − rt_mutex_proxy_unlock(&pi_state->pi_mutex); |
| 845 | | − raw_spin_unlock_irqrestore(&pi_state->pi_mutex.wait_lock, flags); |
| 846 | | − } |
| 847 | | − |
| 848 | | − if (current->pi_state_cache) { |
| 849 | | − kfree(pi_state); |
| 850 | | − } else { |
| 851 | | − /* |
| 852 | | − * pi_state->list is already empty. |
| 853 | | − * clear pi_state->owner. |
| 854 | | − * refcount is at 0 - put it back to 1. |
| 855 | | − */ |
| 856 | | − pi_state->owner = NULL; |
| 857 | | − refcount_set(&pi_state->refcount, 1); |
| 858 | | − current->pi_state_cache = pi_state; |
| 859 | | − } |
| 860 | | −} |
| 861 | | − |
| 862 | | −#ifdef CONFIG_FUTEX_PI |
| 863 | | − |
| 864 | | −/* |
| 865 | | − * This task is holding PI mutexes at exit time => bad. |
| 866 | | − * Kernel cleans up PI-state, but userspace is likely hosed. |
| 867 | | − * (Robust-futex cleanup is separate and might save the day for userspace.) |
| 868 | | − */ |
| 869 | | −static void exit_pi_state_list(struct task_struct *curr) |
| 870 | | −{ |
| 871 | | − struct list_head *next, *head = &curr->pi_state_list; |
| 872 | | − struct futex_pi_state *pi_state; |
| 873 | | − struct futex_hash_bucket *hb; |
| 874 | | − union futex_key key = FUTEX_KEY_INIT; |
| 875 | | − |
| 876 | | − if (!futex_cmpxchg_enabled) |
| 877 | | − return; |
| 878 | | − /* |
| 879 | | − * We are a ZOMBIE and nobody can enqueue itself on |
| 880 | | − * pi_state_list anymore, but we have to be careful |
| 881 | | − * versus waiters unqueueing themselves: |
| 882 | | − */ |
| 883 | | − raw_spin_lock_irq(&curr->pi_lock); |
| 884 | | − while (!list_empty(head)) { |
| 885 | | − next = head->next; |
| 886 | | − pi_state = list_entry(next, struct futex_pi_state, list); |
| 887 | | − key = pi_state->key; |
| 888 | | − hb = futex_hash(&key); |
| 889 | | − |
| 890 | | − /* |
| 891 | | − * We can race against put_pi_state() removing itself from the |
| 892 | | − * list (a waiter going away). put_pi_state() will first |
| 893 | | − * decrement the reference count and then modify the list, so |
| 894 | | − * its possible to see the list entry but fail this reference |
| 895 | | − * acquire. |
| 896 | | − * |
| 897 | | − * In that case; drop the locks to let put_pi_state() make |
| 898 | | − * progress and retry the loop. |
| 899 | | − */ |
| 900 | | − if (!refcount_inc_not_zero(&pi_state->refcount)) { |
| 901 | | − raw_spin_unlock_irq(&curr->pi_lock); |
| 902 | | − cpu_relax(); |
| 903 | | − raw_spin_lock_irq(&curr->pi_lock); |
| 904 | | − continue; |
| 905 | | − } |
| 906 | | − raw_spin_unlock_irq(&curr->pi_lock); |
| 907 | | − |
| 908 | | − spin_lock(&hb->lock); |
| 909 | | − raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 910 | | − raw_spin_lock(&curr->pi_lock); |
| 911 | | − /* |
| 912 | | − * We dropped the pi-lock, so re-check whether this |
| 913 | | − * task still owns the PI-state: |
| 914 | | − */ |
| 915 | | − if (head->next != next) { |
| 916 | | − /* retain curr->pi_lock for the loop invariant */ |
| 917 | | − raw_spin_unlock(&pi_state->pi_mutex.wait_lock); |
| 918 | | − spin_unlock(&hb->lock); |
| 919 | | − put_pi_state(pi_state); |
| 920 | | − continue; |
| 921 | | − } |
| 922 | | − |
| 923 | | − WARN_ON(pi_state->owner != curr); |
| 924 | | − WARN_ON(list_empty(&pi_state->list)); |
| 925 | | − list_del_init(&pi_state->list); |
| 926 | | − pi_state->owner = NULL; |
| 927 | | − |
| 928 | | − raw_spin_unlock(&curr->pi_lock); |
| 929 | | − raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 930 | | − spin_unlock(&hb->lock); |
| 931 | | − |
| 932 | | − rt_mutex_futex_unlock(&pi_state->pi_mutex); |
| 933 | | − put_pi_state(pi_state); |
| 934 | | − |
| 935 | | − raw_spin_lock_irq(&curr->pi_lock); |
| 936 | | − } |
| 937 | | − raw_spin_unlock_irq(&curr->pi_lock); |
| 938 | | −} |
| 939 | | −#else |
| 940 | | −static inline void exit_pi_state_list(struct task_struct *curr) { } |
| 941 | | −#endif |
| 942 | | − |
| 943 | | −/* |
| 944 | | − * We need to check the following states: |
| 945 | | − * |
| 946 | | − * Waiter | pi_state | pi->owner | uTID | uODIED | ? |
| 947 | | − * |
| 948 | | − * [1] NULL | --- | --- | 0 | 0/1 | Valid |
| 949 | | − * [2] NULL | --- | --- | >0 | 0/1 | Valid |
| 950 | | − * |
| 951 | | − * [3] Found | NULL | -- | Any | 0/1 | Invalid |
| 952 | | − * |
| 953 | | − * [4] Found | Found | NULL | 0 | 1 | Valid |
| 954 | | − * [5] Found | Found | NULL | >0 | 1 | Invalid |
| 955 | | − * |
| 956 | | − * [6] Found | Found | task | 0 | 1 | Valid |
| 957 | | − * |
| 958 | | − * [7] Found | Found | NULL | Any | 0 | Invalid |
| 959 | | − * |
| 960 | | − * [8] Found | Found | task | ==taskTID | 0/1 | Valid |
| 961 | | − * [9] Found | Found | task | 0 | 0 | Invalid |
| 962 | | − * [10] Found | Found | task | !=taskTID | 0/1 | Invalid |
| 963 | | − * |
| 964 | | − * [1] Indicates that the kernel can acquire the futex atomically. We |
| 965 | | − * came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit. |
| 966 | | − * |
| 967 | | − * [2] Valid, if TID does not belong to a kernel thread. If no matching |
| 968 | | − * thread is found then it indicates that the owner TID has died. |
| 969 | | − * |
| 970 | | − * [3] Invalid. The waiter is queued on a non PI futex |
| 971 | | − * |
| 972 | | − * [4] Valid state after exit_robust_list(), which sets the user space |
| 973 | | − * value to FUTEX_WAITERS | FUTEX_OWNER_DIED. |
| 974 | | − * |
| 975 | | − * [5] The user space value got manipulated between exit_robust_list() |
| 976 | | − * and exit_pi_state_list() |
| 977 | | − * |
| 978 | | − * [6] Valid state after exit_pi_state_list() which sets the new owner in |
| 979 | | − * the pi_state but cannot access the user space value. |
| 980 | | − * |
| 981 | | − * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set. |
| 982 | | − * |
| 983 | | − * [8] Owner and user space value match |
| 984 | | − * |
| 985 | | − * [9] There is no transient state which sets the user space TID to 0 |
| 986 | | − * except exit_robust_list(), but this is indicated by the |
| 987 | | − * FUTEX_OWNER_DIED bit. See [4] |
| 988 | | − * |
| 989 | | − * [10] There is no transient state which leaves owner and user space |
| 990 | | − * TID out of sync. Except one error case where the kernel is denied |
| 991 | | − * write access to the user address, see fixup_pi_state_owner(). |
| 992 | | − * |
| 993 | | − * |
| 994 | | − * Serialization and lifetime rules: |
| 995 | | − * |
| 996 | | − * hb->lock: |
| 997 | | − * |
| 998 | | − * hb -> futex_q, relation |
| 999 | | − * futex_q -> pi_state, relation |
| 1000 | | − * |
| 1001 | | − * (cannot be raw because hb can contain arbitrary amount |
| 1002 | | − * of futex_q's) |
| 1003 | | − * |
| 1004 | | − * pi_mutex->wait_lock: |
| 1005 | | − * |
| 1006 | | − * {uval, pi_state} |
| 1007 | | − * |
| 1008 | | − * (and pi_mutex 'obviously') |
| 1009 | | − * |
| 1010 | | − * p->pi_lock: |
| 1011 | | − * |
| 1012 | | − * p->pi_state_list -> pi_state->list, relation |
| 1013 | | − * pi_mutex->owner -> pi_state->owner, relation |
| 1014 | | − * |
| 1015 | | − * pi_state->refcount: |
| 1016 | | − * |
| 1017 | | − * pi_state lifetime |
| 1018 | | − * |
| 1019 | | − * |
| 1020 | | − * Lock order: |
| 1021 | | − * |
| 1022 | | − * hb->lock |
| 1023 | | − * pi_mutex->wait_lock |
| 1024 | | − * p->pi_lock |
| 1025 | | − * |
| 1026 | | − */ |
| 1027 | | − |
| 1028 | | −/* |
| 1029 | | − * Validate that the existing waiter has a pi_state and sanity check |
| 1030 | | − * the pi_state against the user space value. If correct, attach to |
| 1031 | | − * it. |
| 1032 | | − */ |
| 1033 | | −static int attach_to_pi_state(u32 __user *uaddr, u32 uval, |
| 1034 | | − struct futex_pi_state *pi_state, |
| 1035 | | − struct futex_pi_state **ps) |
| 1036 | | −{ |
| 1037 | | − pid_t pid = uval & FUTEX_TID_MASK; |
| 1038 | | − u32 uval2; |
| 1039 | | − int ret; |
| 1040 | | − |
| 1041 | | − /* |
| 1042 | | − * Userspace might have messed up non-PI and PI futexes [3] |
| 1043 | | − */ |
| 1044 | | − if (unlikely(!pi_state)) |
| 1045 | | − return -EINVAL; |
| 1046 | | − |
| 1047 | | − /* |
| 1048 | | − * We get here with hb->lock held, and having found a |
| 1049 | | − * futex_top_waiter(). This means that futex_lock_pi() of said futex_q |
| 1050 | | − * has dropped the hb->lock in between futex_queue() and futex_unqueue_pi(), |
| 1051 | | − * which in turn means that futex_lock_pi() still has a reference on |
| 1052 | | − * our pi_state. |
| 1053 | | − * |
| 1054 | | − * The waiter holding a reference on @pi_state also protects against |
| 1055 | | − * the unlocked put_pi_state() in futex_unlock_pi(), futex_lock_pi() |
| 1056 | | − * and futex_wait_requeue_pi() as it cannot go to 0 and consequently |
| 1057 | | − * free pi_state before we can take a reference ourselves. |
| 1058 | | − */ |
| 1059 | | − WARN_ON(!refcount_read(&pi_state->refcount)); |
| 1060 | | − |
| 1061 | | − /* |
| 1062 | | − * Now that we have a pi_state, we can acquire wait_lock |
| 1063 | | − * and do the state validation. |
| 1064 | | − */ |
| 1065 | | − raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 1066 | | − |
| 1067 | | − /* |
| 1068 | | − * Since {uval, pi_state} is serialized by wait_lock, and our current |
| 1069 | | − * uval was read without holding it, it can have changed. Verify it |
| 1070 | | − * still is what we expect it to be, otherwise retry the entire |
| 1071 | | − * operation. |
| 1072 | | − */ |
| 1073 | | − if (futex_get_value_locked(&uval2, uaddr)) |
| 1074 | | − goto out_efault; |
| 1075 | | − |
| 1076 | | − if (uval != uval2) |
| 1077 | | − goto out_eagain; |
| 1078 | | − |
| 1079 | | − /* |
| 1080 | | − * Handle the owner died case: |
| 1081 | | − */ |
| 1082 | | − if (uval & FUTEX_OWNER_DIED) { |
| 1083 | | − /* |
| 1084 | | − * exit_pi_state_list sets owner to NULL and wakes the |
| 1085 | | − * topmost waiter. The task which acquires the |
| 1086 | | − * pi_state->rt_mutex will fixup owner. |
| 1087 | | − */ |
| 1088 | | − if (!pi_state->owner) { |
| 1089 | | − /* |
| 1090 | | − * No pi state owner, but the user space TID |
| 1091 | | − * is not 0. Inconsistent state. [5] |
| 1092 | | − */ |
| 1093 | | − if (pid) |
| 1094 | | − goto out_einval; |
| 1095 | | − /* |
| 1096 | | − * Take a ref on the state and return success. [4] |
| 1097 | | − */ |
| 1098 | | − goto out_attach; |
| 1099 | | − } |
| 1100 | | − |
| 1101 | | − /* |
| 1102 | | − * If TID is 0, then either the dying owner has not |
| 1103 | | − * yet executed exit_pi_state_list() or some waiter |
| 1104 | | − * acquired the rtmutex in the pi state, but did not |
| 1105 | | − * yet fixup the TID in user space. |
| 1106 | | − * |
| 1107 | | − * Take a ref on the state and return success. [6] |
| 1108 | | − */ |
| 1109 | | − if (!pid) |
| 1110 | | − goto out_attach; |
| 1111 | | − } else { |
| 1112 | | − /* |
| 1113 | | − * If the owner died bit is not set, then the pi_state |
| 1114 | | − * must have an owner. [7] |
| 1115 | | − */ |
| 1116 | | − if (!pi_state->owner) |
| 1117 | | − goto out_einval; |
| 1118 | | − } |
| 1119 | | − |
| 1120 | | − /* |
| 1121 | | − * Bail out if user space manipulated the futex value. If pi |
| 1122 | | − * state exists then the owner TID must be the same as the |
| 1123 | | − * user space TID. [9/10] |
| 1124 | | − */ |
| 1125 | | − if (pid != task_pid_vnr(pi_state->owner)) |
| 1126 | | − goto out_einval; |
| 1127 | | − |
| 1128 | | −out_attach: |
| 1129 | | − get_pi_state(pi_state); |
| 1130 | | − raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 1131 | | − *ps = pi_state; |
| 1132 | | − return 0; |
| 1133 | | − |
| 1134 | | −out_einval: |
| 1135 | | − ret = -EINVAL; |
| 1136 | | − goto out_error; |
| 1137 | | − |
| 1138 | | −out_eagain: |
| 1139 | | − ret = -EAGAIN; |
| 1140 | | − goto out_error; |
| 1141 | | − |
| 1142 | | −out_efault: |
| 1143 | | − ret = -EFAULT; |
| 1144 | | − goto out_error; |
| 1145 | | − |
| 1146 | | −out_error: |
| 1147 | | − raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 1148 | | − return ret; |
| 1149 | | −} |
| 1150 | | − |
| 1151 | 679 | /** |
| 1152 | 680 | * wait_for_owner_exiting - Block until the owner has exited |
| 1153 | 681 | * @ret: owner's current futex lock status |
| @@ -1155,7 +683,7 @@ out_error: · 1 unmodified line |
| 1155 | 683 | * |
| 1156 | 684 | * Caller must hold a refcount on @exiting. |
| 1157 | 685 | */ |
| 1158 | | −static void wait_for_owner_exiting(int ret, struct task_struct *exiting) |
| 686 | +void wait_for_owner_exiting(int ret, struct task_struct *exiting) |
| 1159 | 687 | { |
| 1160 | 688 | if (ret != -EBUSY) { |
| 1161 | 689 | WARN_ON_ONCE(exiting); |
| @@ -1179,296 +707,6 @@ static void wait_for_owner_exiting(int ret, struct task_struct *exiting) · 17 unmodified lines |
| 1179 | 707 | put_task_struct(exiting); |
| 1180 | 708 | } |
| 1181 | 709 | |
| 1182 | | −static int handle_exit_race(u32 __user *uaddr, u32 uval, |
| 1183 | | − struct task_struct *tsk) |
| 1184 | | −{ |
| 1185 | | − u32 uval2; |
| 1186 | | − |
| 1187 | | − /* |
| 1188 | | − * If the futex exit state is not yet FUTEX_STATE_DEAD, tell the |
| 1189 | | − * caller that the alleged owner is busy. |
| 1190 | | − */ |
| 1191 | | − if (tsk && tsk->futex_state != FUTEX_STATE_DEAD) |
| 1192 | | − return -EBUSY; |
| 1193 | | − |
| 1194 | | − /* |
| 1195 | | − * Reread the user space value to handle the following situation: |
| 1196 | | − * |
| 1197 | | − * CPU0 CPU1 |
| 1198 | | − * |
| 1199 | | − * sys_exit() sys_futex() |
| 1200 | | − * do_exit() futex_lock_pi() |
| 1201 | | − * futex_lock_pi_atomic() |
| 1202 | | − * exit_signals(tsk) No waiters: |
| 1203 | | − * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID |
| 1204 | | − * mm_release(tsk) Set waiter bit |
| 1205 | | − * exit_robust_list(tsk) { *uaddr = 0x80000PID; |
| 1206 | | − * Set owner died attach_to_pi_owner() { |
| 1207 | | − * *uaddr = 0xC0000000; tsk = get_task(PID); |
| 1208 | | − * } if (!tsk->flags & PF_EXITING) { |
| 1209 | | − * ... attach(); |
| 1210 | | − * tsk->futex_state = } else { |
| 1211 | | − * FUTEX_STATE_DEAD; if (tsk->futex_state != |
| 1212 | | − * FUTEX_STATE_DEAD) |
| 1213 | | − * return -EAGAIN; |
| 1214 | | − * return -ESRCH; <--- FAIL |
| 1215 | | − * } |
| 1216 | | − * |
| 1217 | | − * Returning ESRCH unconditionally is wrong here because the |
| 1218 | | − * user space value has been changed by the exiting task. |
| 1219 | | − * |
| 1220 | | − * The same logic applies to the case where the exiting task is |
| 1221 | | − * already gone. |
| 1222 | | − */ |
| 1223 | | − if (futex_get_value_locked(&uval2, uaddr)) |
| 1224 | | − return -EFAULT; |
| 1225 | | − |
| 1226 | | − /* If the user space value has changed, try again. */ |
| 1227 | | − if (uval2 != uval) |
| 1228 | | − return -EAGAIN; |
| 1229 | | − |
| 1230 | | − /* |
| 1231 | | − * The exiting task did not have a robust list, the robust list was |
| 1232 | | − * corrupted or the user space value in *uaddr is simply bogus. |
| 1233 | | − * Give up and tell user space. |
| 1234 | | − */ |
| 1235 | | − return -ESRCH; |
| 1236 | | −} |
| 1237 | | − |
| 1238 | | −static void __attach_to_pi_owner(struct task_struct *p, union futex_key *key, |
| 1239 | | − struct futex_pi_state **ps) |
| 1240 | | −{ |
| 1241 | | − /* |
| 1242 | | − * No existing pi state. First waiter. [2] |
| 1243 | | − * |
| 1244 | | − * This creates pi_state, we have hb->lock held, this means nothing can |
| 1245 | | − * observe this state, wait_lock is irrelevant. |
| 1246 | | − */ |
| 1247 | | − struct futex_pi_state *pi_state = alloc_pi_state(); |
| 1248 | | − |
| 1249 | | − /* |
| 1250 | | − * Initialize the pi_mutex in locked state and make @p |
| 1251 | | − * the owner of it: |
| 1252 | | − */ |
| 1253 | | − rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p); |
| 1254 | | − |
| 1255 | | − /* Store the key for possible exit cleanups: */ |
| 1256 | | − pi_state->key = *key; |
| 1257 | | − |
| 1258 | | − WARN_ON(!list_empty(&pi_state->list)); |
| 1259 | | − list_add(&pi_state->list, &p->pi_state_list); |
| 1260 | | − /* |
| 1261 | | − * Assignment without holding pi_state->pi_mutex.wait_lock is safe |
| 1262 | | − * because there is no concurrency as the object is not published yet. |
| 1263 | | − */ |
| 1264 | | − pi_state->owner = p; |
| 1265 | | − |
| 1266 | | − *ps = pi_state; |
| 1267 | | −} |
| 1268 | | −/* |
| 1269 | | − * Lookup the task for the TID provided from user space and attach to |
| 1270 | | − * it after doing proper sanity checks. |
| 1271 | | − */ |
| 1272 | | −static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key, |
| 1273 | | − struct futex_pi_state **ps, |
| 1274 | | − struct task_struct **exiting) |
| 1275 | | −{ |
| 1276 | | − pid_t pid = uval & FUTEX_TID_MASK; |
| 1277 | | − struct task_struct *p; |
| 1278 | | − |
| 1279 | | − /* |
| 1280 | | − * We are the first waiter - try to look up the real owner and attach |
| 1281 | | − * the new pi_state to it, but bail out when TID = 0 [1] |
| 1282 | | − * |
| 1283 | | − * The !pid check is paranoid. None of the call sites should end up |
| 1284 | | − * with pid == 0, but better safe than sorry. Let the caller retry |
| 1285 | | − */ |
| 1286 | | − if (!pid) |
| 1287 | | − return -EAGAIN; |
| 1288 | | − p = find_get_task_by_vpid(pid); |
| 1289 | | − if (!p) |
| 1290 | | − return handle_exit_race(uaddr, uval, NULL); |
| 1291 | | − |
| 1292 | | − if (unlikely(p->flags & PF_KTHREAD)) { |
| 1293 | | − put_task_struct(p); |
| 1294 | | − return -EPERM; |
| 1295 | | − } |
| 1296 | | − |
| 1297 | | − /* |
| 1298 | | − * We need to look at the task state to figure out, whether the |
| 1299 | | − * task is exiting. To protect against the change of the task state |
| 1300 | | − * in futex_exit_release(), we do this protected by p->pi_lock: |
| 1301 | | − */ |
| 1302 | | − raw_spin_lock_irq(&p->pi_lock); |
| 1303 | | − if (unlikely(p->futex_state != FUTEX_STATE_OK)) { |
| 1304 | | − /* |
| 1305 | | − * The task is on the way out. When the futex state is |
| 1306 | | − * FUTEX_STATE_DEAD, we know that the task has finished |
| 1307 | | − * the cleanup: |
| 1308 | | − */ |
| 1309 | | − int ret = handle_exit_race(uaddr, uval, p); |
| 1310 | | − |
| 1311 | | − raw_spin_unlock_irq(&p->pi_lock); |
| 1312 | | − /* |
| 1313 | | − * If the owner task is between FUTEX_STATE_EXITING and |
| 1314 | | − * FUTEX_STATE_DEAD then store the task pointer and keep |
| 1315 | | − * the reference on the task struct. The calling code will |
| 1316 | | − * drop all locks, wait for the task to reach |
| 1317 | | − * FUTEX_STATE_DEAD and then drop the refcount. This is |
| 1318 | | − * required to prevent a live lock when the current task |
| 1319 | | − * preempted the exiting task between the two states. |
| 1320 | | − */ |
| 1321 | | − if (ret == -EBUSY) |
| 1322 | | − *exiting = p; |
| 1323 | | − else |
| 1324 | | − put_task_struct(p); |
| 1325 | | − return ret; |
| 1326 | | − } |
| 1327 | | − |
| 1328 | | − __attach_to_pi_owner(p, key, ps); |
| 1329 | | − raw_spin_unlock_irq(&p->pi_lock); |
| 1330 | | − |
| 1331 | | − put_task_struct(p); |
| 1332 | | − |
| 1333 | | − return 0; |
| 1334 | | −} |
| 1335 | | − |
| 1336 | | −static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval) |
| 1337 | | −{ |
| 1338 | | − int err; |
| 1339 | | − u32 curval; |
| 1340 | | − |
| 1341 | | − if (unlikely(should_fail_futex(true))) |
| 1342 | | − return -EFAULT; |
| 1343 | | − |
| 1344 | | − err = futex_cmpxchg_value_locked(&curval, uaddr, uval, newval); |
| 1345 | | − if (unlikely(err)) |
| 1346 | | − return err; |
| 1347 | | − |
| 1348 | | − /* If user space value changed, let the caller retry */ |
| 1349 | | − return curval != uval ? -EAGAIN : 0; |
| 1350 | | −} |
| 1351 | | − |
| 1352 | | −/** |
| 1353 | | − * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex |
| 1354 | | − * @uaddr: the pi futex user address |
| 1355 | | − * @hb: the pi futex hash bucket |
| 1356 | | − * @key: the futex key associated with uaddr and hb |
| 1357 | | − * @ps: the pi_state pointer where we store the result of the |
| 1358 | | − * lookup |
| 1359 | | − * @task: the task to perform the atomic lock work for. This will |
| 1360 | | − * be "current" except in the case of requeue pi. |
| 1361 | | − * @exiting: Pointer to store the task pointer of the owner task |
| 1362 | | − * which is in the middle of exiting |
| 1363 | | − * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0) |
| 1364 | | − * |
| 1365 | | − * Return: |
| 1366 | | − * - 0 - ready to wait; |
| 1367 | | − * - 1 - acquired the lock; |
| 1368 | | − * - <0 - error |
| 1369 | | − * |
| 1370 | | − * The hb->lock must be held by the caller. |
| 1371 | | − * |
| 1372 | | − * @exiting is only set when the return value is -EBUSY. If so, this holds |
| 1373 | | − * a refcount on the exiting task on return and the caller needs to drop it |
| 1374 | | − * after waiting for the exit to complete. |
| 1375 | | − */ |
| 1376 | | −static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb, |
| 1377 | | − union futex_key *key, |
| 1378 | | − struct futex_pi_state **ps, |
| 1379 | | − struct task_struct *task, |
| 1380 | | − struct task_struct **exiting, |
| 1381 | | − int set_waiters) |
| 1382 | | −{ |
| 1383 | | − u32 uval, newval, vpid = task_pid_vnr(task); |
| 1384 | | − struct futex_q *top_waiter; |
| 1385 | | − int ret; |
| 1386 | | − |
| 1387 | | − /* |
| 1388 | | − * Read the user space value first so we can validate a few |
| 1389 | | − * things before proceeding further. |
| 1390 | | − */ |
| 1391 | | − if (futex_get_value_locked(&uval, uaddr)) |
| 1392 | | − return -EFAULT; |
| 1393 | | − |
| 1394 | | − if (unlikely(should_fail_futex(true))) |
| 1395 | | − return -EFAULT; |
| 1396 | | − |
| 1397 | | − /* |
| 1398 | | − * Detect deadlocks. |
| 1399 | | − */ |
| 1400 | | − if ((unlikely((uval & FUTEX_TID_MASK) == vpid))) |
| 1401 | | − return -EDEADLK; |
| 1402 | | − |
| 1403 | | − if ((unlikely(should_fail_futex(true)))) |
| 1404 | | − return -EDEADLK; |
| 1405 | | − |
| 1406 | | − /* |
| 1407 | | − * Lookup existing state first. If it exists, try to attach to |
| 1408 | | − * its pi_state. |
| 1409 | | − */ |
| 1410 | | − top_waiter = futex_top_waiter(hb, key); |
| 1411 | | − if (top_waiter) |
| 1412 | | − return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps); |
| 1413 | | − |
| 1414 | | − /* |
| 1415 | | − * No waiter and user TID is 0. We are here because the |
| 1416 | | − * waiters or the owner died bit is set or called from |
| 1417 | | − * requeue_cmp_pi or for whatever reason something took the |
| 1418 | | − * syscall. |
| 1419 | | − */ |
| 1420 | | − if (!(uval & FUTEX_TID_MASK)) { |
| 1421 | | − /* |
| 1422 | | − * We take over the futex. No other waiters and the user space |
| 1423 | | − * TID is 0. We preserve the owner died bit. |
| 1424 | | − */ |
| 1425 | | − newval = uval & FUTEX_OWNER_DIED; |
| 1426 | | − newval |= vpid; |
| 1427 | | − |
| 1428 | | − /* The futex requeue_pi code can enforce the waiters bit */ |
| 1429 | | − if (set_waiters) |
| 1430 | | − newval |= FUTEX_WAITERS; |
| 1431 | | − |
| 1432 | | − ret = lock_pi_update_atomic(uaddr, uval, newval); |
| 1433 | | − if (ret) |
| 1434 | | − return ret; |
| 1435 | | − |
| 1436 | | − /* |
| 1437 | | − * If the waiter bit was requested the caller also needs PI |
| 1438 | | − * state attached to the new owner of the user space futex. |
| 1439 | | − * |
| 1440 | | − * @task is guaranteed to be alive and it cannot be exiting |
| 1441 | | − * because it is either sleeping or waiting in |
| 1442 | | − * futex_requeue_pi_wakeup_sync(). |
| 1443 | | − * |
| 1444 | | − * No need to do the full attach_to_pi_owner() exercise |
| 1445 | | − * because @task is known and valid. |
| 1446 | | − */ |
| 1447 | | − if (set_waiters) { |
| 1448 | | − raw_spin_lock_irq(&task->pi_lock); |
| 1449 | | − __attach_to_pi_owner(task, key, ps); |
| 1450 | | − raw_spin_unlock_irq(&task->pi_lock); |
| 1451 | | − } |
| 1452 | | − return 1; |
| 1453 | | − } |
| 1454 | | − |
| 1455 | | − /* |
| 1456 | | − * First waiter. Set the waiters bit before attaching ourself to |
| 1457 | | − * the owner. If owner tries to unlock, it will be forced into |
| 1458 | | − * the kernel and blocked on hb->lock. |
| 1459 | | − */ |
| 1460 | | − newval = uval | FUTEX_WAITERS; |
| 1461 | | − ret = lock_pi_update_atomic(uaddr, uval, newval); |
| 1462 | | − if (ret) |
| 1463 | | − return ret; |
| 1464 | | − /* |
| 1465 | | − * If the update of the user space value succeeded, we try to |
| 1466 | | − * attach to the owner. If that fails, no harm done, we only |
| 1467 | | − * set the FUTEX_WAITERS bit in the user space variable. |
| 1468 | | − */ |
| 1469 | | − return attach_to_pi_owner(uaddr, newval, key, ps, exiting); |
| 1470 | | −} |
| 1471 | | − |
| 1472 | 710 | /** |
| 1473 | 711 | * __futex_unqueue() - Remove the futex_q from its futex_hash_bucket |
| 1474 | 712 | * @q: The futex_q to unqueue |
| @@ -1519,79 +757,6 @@ static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q) · 44 unmodified lines |
| 1519 | 757 | wake_q_add_safe(wake_q, p); |
| 1520 | 758 | } |
| 1521 | 759 | |
| 1522 | | −/* |
| 1523 | | − * Caller must hold a reference on @pi_state. |
| 1524 | | − */ |
| 1525 | | −static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state) |
| 1526 | | −{ |
| 1527 | | − struct rt_mutex_waiter *top_waiter; |
| 1528 | | − struct task_struct *new_owner; |
| 1529 | | − bool postunlock = false; |
| 1530 | | − DEFINE_RT_WAKE_Q(wqh); |
| 1531 | | − u32 curval, newval; |
| 1532 | | − int ret = 0; |
| 1533 | | − |
| 1534 | | − top_waiter = rt_mutex_top_waiter(&pi_state->pi_mutex); |
| 1535 | | − if (WARN_ON_ONCE(!top_waiter)) { |
| 1536 | | − /* |
| 1537 | | − * As per the comment in futex_unlock_pi() this should not happen. |
| 1538 | | − * |
| 1539 | | − * When this happens, give up our locks and try again, giving |
| 1540 | | − * the futex_lock_pi() instance time to complete, either by |
| 1541 | | − * waiting on the rtmutex or removing itself from the futex |
| 1542 | | − * queue. |
| 1543 | | − */ |
| 1544 | | − ret = -EAGAIN; |
| 1545 | | − goto out_unlock; |
| 1546 | | − } |
| 1547 | | − |
| 1548 | | − new_owner = top_waiter->task; |
| 1549 | | − |
| 1550 | | − /* |
| 1551 | | − * We pass it to the next owner. The WAITERS bit is always kept |
| 1552 | | − * enabled while there is PI state around. We cleanup the owner |
| 1553 | | − * died bit, because we are the owner. |
| 1554 | | − */ |
| 1555 | | − newval = FUTEX_WAITERS | task_pid_vnr(new_owner); |
| 1556 | | − |
| 1557 | | − if (unlikely(should_fail_futex(true))) { |
| 1558 | | − ret = -EFAULT; |
| 1559 | | − goto out_unlock; |
| 1560 | | − } |
| 1561 | | − |
| 1562 | | − ret = futex_cmpxchg_value_locked(&curval, uaddr, uval, newval); |
| 1563 | | − if (!ret && (curval != uval)) { |
| 1564 | | − /* |
| 1565 | | − * If a unconditional UNLOCK_PI operation (user space did not |
| 1566 | | − * try the TID->0 transition) raced with a waiter setting the |
| 1567 | | − * FUTEX_WAITERS flag between get_user() and locking the hash |
| 1568 | | − * bucket lock, retry the operation. |
| 1569 | | − */ |
| 1570 | | − if ((FUTEX_TID_MASK & curval) == uval) |
| 1571 | | − ret = -EAGAIN; |
| 1572 | | − else |
| 1573 | | − ret = -EINVAL; |
| 1574 | | − } |
| 1575 | | − |
| 1576 | | − if (!ret) { |
| 1577 | | − /* |
| 1578 | | − * This is a point of no return; once we modified the uval |
| 1579 | | − * there is no going back and subsequent operations must |
| 1580 | | − * not fail. |
| 1581 | | − */ |
| 1582 | | − pi_state_update_owner(pi_state, new_owner); |
| 1583 | | − postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wqh); |
| 1584 | | − } |
| 1585 | | − |
| 1586 | | −out_unlock: |
| 1587 | | − raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 1588 | | − |
| 1589 | | − if (postunlock) |
| 1590 | | − rt_mutex_postunlock(&wqh); |
| 1591 | | − |
| 1592 | | − return ret; |
| 1593 | | −} |
| 1594 | | − |
| 1595 | 760 | /* |
| 1596 | 761 | * Express the locking dependencies for lockdep: |
| 1597 | 762 | */ |
| @@ -2410,7 +1575,7 @@ out_unlock: · 812 unmodified lines |
| 2410 | 1575 | } |
| 2411 | 1576 | |
| 2412 | 1577 | /* The key must be already stored in q->key. */ |
| 2413 | | −static inline struct futex_hash_bucket *futex_q_lock(struct futex_q *q) |
| 1578 | +struct futex_hash_bucket *futex_q_lock(struct futex_q *q) |
| 2414 | 1579 | __acquires(&hb->lock) |
| 2415 | 1580 | { |
| 2416 | 1581 | struct futex_hash_bucket *hb; |
| @@ -2433,15 +1598,14 @@ static inline struct futex_hash_bucket *futex_q_lock(struct futex_q *q) · 16 unmodified lines |
| 2433 | 1598 | return hb; |
| 2434 | 1599 | } |
| 2435 | 1600 | |
| 2436 | | −static inline void |
| 2437 | | −futex_q_unlock(struct futex_hash_bucket *hb) |
| 1601 | +void futex_q_unlock(struct futex_hash_bucket *hb) |
| 2438 | 1602 | __releases(&hb->lock) |
| 2439 | 1603 | { |
| 2440 | 1604 | spin_unlock(&hb->lock); |
| 2441 | 1605 | hb_waiters_dec(hb); |
| 2442 | 1606 | } |
| 2443 | 1607 | |
| 2444 | | −static inline void __futex_queue(struct futex_q *q, struct futex_hash_bucket *hb) |
| 1608 | +void __futex_queue(struct futex_q *q, struct futex_hash_bucket *hb) |
| 2445 | 1609 | { |
| 2446 | 1610 | int prio; |
| 2447 | 1611 | |
| @@ -2537,255 +1701,17 @@ retry: · 89 unmodified lines |
| 2537 | 1701 | * PI futexes can not be requeued and must remove themselves from the |
| 2538 | 1702 | * hash bucket. The hash bucket lock (i.e. lock_ptr) is held. |
| 2539 | 1703 | */ |
| 2540 | | −static void futex_unqueue_pi(struct futex_q *q) |
| 1704 | +void futex_unqueue_pi(struct futex_q *q) |
| 2541 | 1705 | { |
| 2542 | 1706 | __futex_unqueue(q); |
| 2543 | 1707 | |
| 2544 | 1708 | BUG_ON(!q->pi_state); |
| 2545 | 1709 | put_pi_state(q->pi_state); |
| 2546 | | − q->pi_state = NULL; |
| 2547 | | −} |
| 2548 | | − |
| 2549 | | −static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, |
| 2550 | | − struct task_struct *argowner) |
| 2551 | | −{ |
| 2552 | | − struct futex_pi_state *pi_state = q->pi_state; |
| 2553 | | − struct task_struct *oldowner, *newowner; |
| 2554 | | − u32 uval, curval, newval, newtid; |
| 2555 | | − int err = 0; |
| 2556 | | − |
| 2557 | | − oldowner = pi_state->owner; |
| 2558 | | − |
| 2559 | | − /* |
| 2560 | | − * We are here because either: |
| 2561 | | − * |
| 2562 | | − * - we stole the lock and pi_state->owner needs updating to reflect |
| 2563 | | − * that (@argowner == current), |
| 2564 | | − * |
| 2565 | | − * or: |
| 2566 | | − * |
| 2567 | | − * - someone stole our lock and we need to fix things to point to the |
| 2568 | | − * new owner (@argowner == NULL). |
| 2569 | | − * |
| 2570 | | − * Either way, we have to replace the TID in the user space variable. |
| 2571 | | − * This must be atomic as we have to preserve the owner died bit here. |
| 2572 | | − * |
| 2573 | | − * Note: We write the user space value _before_ changing the pi_state |
| 2574 | | − * because we can fault here. Imagine swapped out pages or a fork |
| 2575 | | − * that marked all the anonymous memory readonly for cow. |
| 2576 | | − * |
| 2577 | | − * Modifying pi_state _before_ the user space value would leave the |
| 2578 | | − * pi_state in an inconsistent state when we fault here, because we |
| 2579 | | − * need to drop the locks to handle the fault. This might be observed |
| 2580 | | − * in the PID checks when attaching to PI state . |
| 2581 | | − */ |
| 2582 | | −retry: |
| 2583 | | − if (!argowner) { |
| 2584 | | − if (oldowner != current) { |
| 2585 | | − /* |
| 2586 | | − * We raced against a concurrent self; things are |
| 2587 | | − * already fixed up. Nothing to do. |
| 2588 | | − */ |
| 2589 | | − return 0; |
| 2590 | | − } |
| 2591 | | − |
| 2592 | | − if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) { |
| 2593 | | − /* We got the lock. pi_state is correct. Tell caller. */ |
| 2594 | | − return 1; |
| 2595 | | − } |
| 2596 | | − |
| 2597 | | − /* |
| 2598 | | − * The trylock just failed, so either there is an owner or |
| 2599 | | − * there is a higher priority waiter than this one. |
| 2600 | | − */ |
| 2601 | | − newowner = rt_mutex_owner(&pi_state->pi_mutex); |
| 2602 | | − /* |
| 2603 | | − * If the higher priority waiter has not yet taken over the |
| 2604 | | − * rtmutex then newowner is NULL. We can't return here with |
| 2605 | | − * that state because it's inconsistent vs. the user space |
| 2606 | | − * state. So drop the locks and try again. It's a valid |
| 2607 | | − * situation and not any different from the other retry |
| 2608 | | − * conditions. |
| 2609 | | − */ |
| 2610 | | − if (unlikely(!newowner)) { |
| 2611 | | − err = -EAGAIN; |
| 2612 | | − goto handle_err; |
| 2613 | | − } |
| 2614 | | − } else { |
| 2615 | | − WARN_ON_ONCE(argowner != current); |
| 2616 | | − if (oldowner == current) { |
| 2617 | | − /* |
| 2618 | | − * We raced against a concurrent self; things are |
| 2619 | | − * already fixed up. Nothing to do. |
| 2620 | | − */ |
| 2621 | | − return 1; |
| 2622 | | − } |
| 2623 | | − newowner = argowner; |
| 2624 | | − } |
| 2625 | | − |
| 2626 | | − newtid = task_pid_vnr(newowner) | FUTEX_WAITERS; |
| 2627 | | − /* Owner died? */ |
| 2628 | | − if (!pi_state->owner) |
| 2629 | | − newtid |= FUTEX_OWNER_DIED; |
| 2630 | | − |
| 2631 | | − err = futex_get_value_locked(&uval, uaddr); |
| 2632 | | − if (err) |
| 2633 | | − goto handle_err; |
| 2634 | | − |
| 2635 | | − for (;;) { |
| 2636 | | − newval = (uval & FUTEX_OWNER_DIED) | newtid; |
| 2637 | | − |
| 2638 | | − err = futex_cmpxchg_value_locked(&curval, uaddr, uval, newval); |
| 2639 | | − if (err) |
| 2640 | | − goto handle_err; |
| 2641 | | − |
| 2642 | | − if (curval == uval) |
| 2643 | | − break; |
| 2644 | | − uval = curval; |
| 2645 | | − } |
| 2646 | | − |
| 2647 | | − /* |
| 2648 | | − * We fixed up user space. Now we need to fix the pi_state |
| 2649 | | − * itself. |
| 2650 | | − */ |
| 2651 | | − pi_state_update_owner(pi_state, newowner); |
| 2652 | | − |
| 2653 | | − return argowner == current; |
| 2654 | | − |
| 2655 | | − /* |
| 2656 | | − * In order to reschedule or handle a page fault, we need to drop the |
| 2657 | | − * locks here. In the case of a fault, this gives the other task |
| 2658 | | − * (either the highest priority waiter itself or the task which stole |
| 2659 | | − * the rtmutex) the chance to try the fixup of the pi_state. So once we |
| 2660 | | − * are back from handling the fault we need to check the pi_state after |
| 2661 | | − * reacquiring the locks and before trying to do another fixup. When |
| 2662 | | − * the fixup has been done already we simply return. |
| 2663 | | − * |
| 2664 | | − * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely |
| 2665 | | − * drop hb->lock since the caller owns the hb -> futex_q relation. |
| 2666 | | − * Dropping the pi_mutex->wait_lock requires the state revalidate. |
| 2667 | | − */ |
| 2668 | | −handle_err: |
| 2669 | | − raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 2670 | | − spin_unlock(q->lock_ptr); |
| 2671 | | − |
| 2672 | | − switch (err) { |
| 2673 | | − case -EFAULT: |
| 2674 | | − err = fault_in_user_writeable(uaddr); |
| 2675 | | − break; |
| 2676 | | − |
| 2677 | | − case -EAGAIN: |
| 2678 | | − cond_resched(); |
| 2679 | | − err = 0; |
| 2680 | | − break; |
| 2681 | | − |
| 2682 | | − default: |
| 2683 | | − WARN_ON_ONCE(1); |
| 2684 | | − break; |
| 2685 | | − } |
| 2686 | | − |
| 2687 | | − spin_lock(q->lock_ptr); |
| 2688 | | − raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 2689 | | − |
| 2690 | | − /* |
| 2691 | | − * Check if someone else fixed it for us: |
| 2692 | | − */ |
| 2693 | | − if (pi_state->owner != oldowner) |
| 2694 | | − return argowner == current; |
| 2695 | | − |
| 2696 | | − /* Retry if err was -EAGAIN or the fault in succeeded */ |
| 2697 | | − if (!err) |
| 2698 | | − goto retry; |
| 2699 | | − |
| 2700 | | − /* |
| 2701 | | − * fault_in_user_writeable() failed so user state is immutable. At |
| 2702 | | − * best we can make the kernel state consistent but user state will |
| 2703 | | − * be most likely hosed and any subsequent unlock operation will be |
| 2704 | | − * rejected due to PI futex rule [10]. |
| 2705 | | − * |
| 2706 | | − * Ensure that the rtmutex owner is also the pi_state owner despite |
| 2707 | | − * the user space value claiming something different. There is no |
| 2708 | | − * point in unlocking the rtmutex if current is the owner as it |
| 2709 | | − * would need to wait until the next waiter has taken the rtmutex |
| 2710 | | − * to guarantee consistent state. Keep it simple. Userspace asked |
| 2711 | | − * for this wreckaged state. |
| 2712 | | − * |
| 2713 | | − * The rtmutex has an owner - either current or some other |
| 2714 | | − * task. See the EAGAIN loop above. |
| 2715 | | − */ |
| 2716 | | − pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex)); |
| 2717 | | − |
| 2718 | | − return err; |
| 2719 | | −} |
| 2720 | | − |
| 2721 | | −static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, |
| 2722 | | − struct task_struct *argowner) |
| 2723 | | −{ |
| 2724 | | − struct futex_pi_state *pi_state = q->pi_state; |
| 2725 | | − int ret; |
| 2726 | | − |
| 2727 | | − lockdep_assert_held(q->lock_ptr); |
| 2728 | | − |
| 2729 | | − raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 2730 | | − ret = __fixup_pi_state_owner(uaddr, q, argowner); |
| 2731 | | − raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 2732 | | − return ret; |
| 2733 | | −} |
| 2734 | | − |
| 2735 | | −static long futex_wait_restart(struct restart_block *restart); |
| 2736 | | − |
| 2737 | | −/** |
| 2738 | | − * fixup_owner() - Post lock pi_state and corner case management |
| 2739 | | − * @uaddr: user address of the futex |
| 2740 | | − * @q: futex_q (contains pi_state and access to the rt_mutex) |
| 2741 | | − * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0) |
| 2742 | | − * |
| 2743 | | − * After attempting to lock an rt_mutex, this function is called to cleanup |
| 2744 | | − * the pi_state owner as well as handle race conditions that may allow us to |
| 2745 | | − * acquire the lock. Must be called with the hb lock held. |
| 2746 | | − * |
| 2747 | | − * Return: |
| 2748 | | − * - 1 - success, lock taken; |
| 2749 | | − * - 0 - success, lock not taken; |
| 2750 | | − * - <0 - on error (-EFAULT) |
| 2751 | | − */ |
| 2752 | | −static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked) |
| 2753 | | −{ |
| 2754 | | − if (locked) { |
| 2755 | | − /* |
| 2756 | | − * Got the lock. We might not be the anticipated owner if we |
| 2757 | | − * did a lock-steal - fix up the PI-state in that case: |
| 2758 | | − * |
| 2759 | | − * Speculative pi_state->owner read (we don't hold wait_lock); |
| 2760 | | − * since we own the lock pi_state->owner == current is the |
| 2761 | | − * stable state, anything else needs more attention. |
| 2762 | | − */ |
| 2763 | | − if (q->pi_state->owner != current) |
| 2764 | | − return fixup_pi_state_owner(uaddr, q, current); |
| 2765 | | − return 1; |
| 2766 | | − } |
| 2767 | | − |
| 2768 | | − /* |
| 2769 | | − * If we didn't get the lock; check if anybody stole it from us. In |
| 2770 | | − * that case, we need to fix up the uval to point to them instead of |
| 2771 | | − * us, otherwise bad things happen. [10] |
| 2772 | | − * |
| 2773 | | − * Another speculative read; pi_state->owner == current is unstable |
| 2774 | | − * but needs our attention. |
| 2775 | | − */ |
| 2776 | | − if (q->pi_state->owner == current) |
| 2777 | | − return fixup_pi_state_owner(uaddr, q, NULL); |
| 2778 | | − |
| 2779 | | − /* |
| 2780 | | − * Paranoia check. If we did not take the lock, then we should not be |
| 2781 | | − * the owner of the rt_mutex. Warn and establish consistent state. |
| 2782 | | − */ |
| 2783 | | − if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current)) |
| 2784 | | − return fixup_pi_state_owner(uaddr, q, current); |
| 2785 | | − |
| 2786 | | − return 0; |
| 1710 | + q->pi_state = NULL; |
| 2787 | 1711 | } |
| 2788 | 1712 | |
| 1713 | +static long futex_wait_restart(struct restart_block *restart); |
| 1714 | + |
| 2789 | 1715 | /** |
| 2790 | 1716 | * futex_wait_queue() - futex_queue() and wait for wakeup, timeout, or signal |
| 2791 | 1717 | * @hb: the futex hash bucket, must be locked by the caller |
| @@ -2974,319 +1900,6 @@ static long futex_wait_restart(struct restart_block *restart) · 182 unmodified lines |
| 2974 | 1900 | } |
| 2975 | 1901 | |
| 2976 | 1902 | |
| 2977 | | −/* |
| 2978 | | − * Userspace tried a 0 -> TID atomic transition of the futex value |
| 2979 | | − * and failed. The kernel side here does the whole locking operation: |
| 2980 | | − * if there are waiters then it will block as a consequence of relying |
| 2981 | | − * on rt-mutexes, it does PI, etc. (Due to races the kernel might see |
| 2982 | | − * a 0 value of the futex too.). |
| 2983 | | − * |
| 2984 | | − * Also serves as futex trylock_pi()'ing, and due semantics. |
| 2985 | | − */ |
| 2986 | | −int futex_lock_pi(u32 __user *uaddr, unsigned int flags, ktime_t *time, int trylock) |
| 2987 | | −{ |
| 2988 | | − struct hrtimer_sleeper timeout, *to; |
| 2989 | | − struct task_struct *exiting = NULL; |
| 2990 | | − struct rt_mutex_waiter rt_waiter; |
| 2991 | | − struct futex_hash_bucket *hb; |
| 2992 | | − struct futex_q q = futex_q_init; |
| 2993 | | − int res, ret; |
| 2994 | | − |
| 2995 | | − if (!IS_ENABLED(CONFIG_FUTEX_PI)) |
| 2996 | | − return -ENOSYS; |
| 2997 | | − |
| 2998 | | − if (refill_pi_state_cache()) |
| 2999 | | − return -ENOMEM; |
| 3000 | | − |
| 3001 | | − to = futex_setup_timer(time, &timeout, flags, 0); |
| 3002 | | − |
| 3003 | | −retry: |
| 3004 | | − ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, FUTEX_WRITE); |
| 3005 | | − if (unlikely(ret != 0)) |
| 3006 | | − goto out; |
| 3007 | | − |
| 3008 | | −retry_private: |
| 3009 | | − hb = futex_q_lock(&q); |
| 3010 | | − |
| 3011 | | − ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, |
| 3012 | | − &exiting, 0); |
| 3013 | | − if (unlikely(ret)) { |
| 3014 | | − /* |
| 3015 | | − * Atomic work succeeded and we got the lock, |
| 3016 | | − * or failed. Either way, we do _not_ block. |
| 3017 | | − */ |
| 3018 | | − switch (ret) { |
| 3019 | | − case 1: |
| 3020 | | − /* We got the lock. */ |
| 3021 | | − ret = 0; |
| 3022 | | − goto out_unlock_put_key; |
| 3023 | | − case -EFAULT: |
| 3024 | | − goto uaddr_faulted; |
| 3025 | | − case -EBUSY: |
| 3026 | | − case -EAGAIN: |
| 3027 | | − /* |
| 3028 | | − * Two reasons for this: |
| 3029 | | − * - EBUSY: Task is exiting and we just wait for the |
| 3030 | | − * exit to complete. |
| 3031 | | − * - EAGAIN: The user space value changed. |
| 3032 | | − */ |
| 3033 | | − futex_q_unlock(hb); |
| 3034 | | − /* |
| 3035 | | − * Handle the case where the owner is in the middle of |
| 3036 | | − * exiting. Wait for the exit to complete otherwise |
| 3037 | | − * this task might loop forever, aka. live lock. |
| 3038 | | − */ |
| 3039 | | − wait_for_owner_exiting(ret, exiting); |
| 3040 | | − cond_resched(); |
| 3041 | | − goto retry; |
| 3042 | | − default: |
| 3043 | | − goto out_unlock_put_key; |
| 3044 | | − } |
| 3045 | | − } |
| 3046 | | − |
| 3047 | | − WARN_ON(!q.pi_state); |
| 3048 | | − |
| 3049 | | − /* |
| 3050 | | − * Only actually queue now that the atomic ops are done: |
| 3051 | | − */ |
| 3052 | | − __futex_queue(&q, hb); |
| 3053 | | − |
| 3054 | | − if (trylock) { |
| 3055 | | − ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex); |
| 3056 | | − /* Fixup the trylock return value: */ |
| 3057 | | − ret = ret ? 0 : -EWOULDBLOCK; |
| 3058 | | − goto no_block; |
| 3059 | | − } |
| 3060 | | − |
| 3061 | | − rt_mutex_init_waiter(&rt_waiter); |
| 3062 | | − |
| 3063 | | − /* |
| 3064 | | − * On PREEMPT_RT_FULL, when hb->lock becomes an rt_mutex, we must not |
| 3065 | | − * hold it while doing rt_mutex_start_proxy(), because then it will |
| 3066 | | − * include hb->lock in the blocking chain, even through we'll not in |
| 3067 | | − * fact hold it while blocking. This will lead it to report -EDEADLK |
| 3068 | | − * and BUG when futex_unlock_pi() interleaves with this. |
| 3069 | | − * |
| 3070 | | − * Therefore acquire wait_lock while holding hb->lock, but drop the |
| 3071 | | − * latter before calling __rt_mutex_start_proxy_lock(). This |
| 3072 | | − * interleaves with futex_unlock_pi() -- which does a similar lock |
| 3073 | | − * handoff -- such that the latter can observe the futex_q::pi_state |
| 3074 | | − * before __rt_mutex_start_proxy_lock() is done. |
| 3075 | | − */ |
| 3076 | | − raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock); |
| 3077 | | − spin_unlock(q.lock_ptr); |
| 3078 | | − /* |
| 3079 | | − * __rt_mutex_start_proxy_lock() unconditionally enqueues the @rt_waiter |
| 3080 | | − * such that futex_unlock_pi() is guaranteed to observe the waiter when |
| 3081 | | − * it sees the futex_q::pi_state. |
| 3082 | | − */ |
| 3083 | | − ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current); |
| 3084 | | − raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock); |
| 3085 | | − |
| 3086 | | − if (ret) { |
| 3087 | | − if (ret == 1) |
| 3088 | | − ret = 0; |
| 3089 | | − goto cleanup; |
| 3090 | | − } |
| 3091 | | − |
| 3092 | | − if (unlikely(to)) |
| 3093 | | − hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS); |
| 3094 | | − |
| 3095 | | − ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter); |
| 3096 | | − |
| 3097 | | −cleanup: |
| 3098 | | − spin_lock(q.lock_ptr); |
| 3099 | | − /* |
| 3100 | | − * If we failed to acquire the lock (deadlock/signal/timeout), we must |
| 3101 | | − * first acquire the hb->lock before removing the lock from the |
| 3102 | | − * rt_mutex waitqueue, such that we can keep the hb and rt_mutex wait |
| 3103 | | − * lists consistent. |
| 3104 | | − * |
| 3105 | | − * In particular; it is important that futex_unlock_pi() can not |
| 3106 | | − * observe this inconsistency. |
| 3107 | | − */ |
| 3108 | | − if (ret && !rt_mutex_cleanup_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter)) |
| 3109 | | − ret = 0; |
| 3110 | | − |
| 3111 | | −no_block: |
| 3112 | | − /* |
| 3113 | | − * Fixup the pi_state owner and possibly acquire the lock if we |
| 3114 | | − * haven't already. |
| 3115 | | − */ |
| 3116 | | − res = fixup_owner(uaddr, &q, !ret); |
| 3117 | | − /* |
| 3118 | | − * If fixup_owner() returned an error, propagate that. If it acquired |
| 3119 | | − * the lock, clear our -ETIMEDOUT or -EINTR. |
| 3120 | | − */ |
| 3121 | | − if (res) |
| 3122 | | − ret = (res < 0) ? res : 0; |
| 3123 | | − |
| 3124 | | − futex_unqueue_pi(&q); |
| 3125 | | − spin_unlock(q.lock_ptr); |
| 3126 | | − goto out; |
| 3127 | | − |
| 3128 | | −out_unlock_put_key: |
| 3129 | | − futex_q_unlock(hb); |
| 3130 | | − |
| 3131 | | −out: |
| 3132 | | − if (to) { |
| 3133 | | − hrtimer_cancel(&to->timer); |
| 3134 | | − destroy_hrtimer_on_stack(&to->timer); |
| 3135 | | − } |
| 3136 | | − return ret != -EINTR ? ret : -ERESTARTNOINTR; |
| 3137 | | − |
| 3138 | | −uaddr_faulted: |
| 3139 | | − futex_q_unlock(hb); |
| 3140 | | − |
| 3141 | | − ret = fault_in_user_writeable(uaddr); |
| 3142 | | − if (ret) |
| 3143 | | − goto out; |
| 3144 | | − |
| 3145 | | − if (!(flags & FLAGS_SHARED)) |
| 3146 | | − goto retry_private; |
| 3147 | | − |
| 3148 | | − goto retry; |
| 3149 | | −} |
| 3150 | | − |
| 3151 | | −/* |
| 3152 | | − * Userspace attempted a TID -> 0 atomic transition, and failed. |
| 3153 | | − * This is the in-kernel slowpath: we look up the PI state (if any), |
| 3154 | | − * and do the rt-mutex unlock. |
| 3155 | | − */ |
| 3156 | | −int futex_unlock_pi(u32 __user *uaddr, unsigned int flags) |
| 3157 | | −{ |
| 3158 | | − u32 curval, uval, vpid = task_pid_vnr(current); |
| 3159 | | − union futex_key key = FUTEX_KEY_INIT; |
| 3160 | | − struct futex_hash_bucket *hb; |
| 3161 | | − struct futex_q *top_waiter; |
| 3162 | | − int ret; |
| 3163 | | − |
| 3164 | | − if (!IS_ENABLED(CONFIG_FUTEX_PI)) |
| 3165 | | − return -ENOSYS; |
| 3166 | | − |
| 3167 | | −retry: |
| 3168 | | − if (get_user(uval, uaddr)) |
| 3169 | | − return -EFAULT; |
| 3170 | | − /* |
| 3171 | | − * We release only a lock we actually own: |
| 3172 | | − */ |
| 3173 | | − if ((uval & FUTEX_TID_MASK) != vpid) |
| 3174 | | − return -EPERM; |
| 3175 | | − |
| 3176 | | − ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_WRITE); |
| 3177 | | − if (ret) |
| 3178 | | − return ret; |
| 3179 | | − |
| 3180 | | − hb = futex_hash(&key); |
| 3181 | | − spin_lock(&hb->lock); |
| 3182 | | − |
| 3183 | | − /* |
| 3184 | | − * Check waiters first. We do not trust user space values at |
| 3185 | | − * all and we at least want to know if user space fiddled |
| 3186 | | − * with the futex value instead of blindly unlocking. |
| 3187 | | − */ |
| 3188 | | − top_waiter = futex_top_waiter(hb, &key); |
| 3189 | | − if (top_waiter) { |
| 3190 | | − struct futex_pi_state *pi_state = top_waiter->pi_state; |
| 3191 | | − |
| 3192 | | − ret = -EINVAL; |
| 3193 | | − if (!pi_state) |
| 3194 | | − goto out_unlock; |
| 3195 | | − |
| 3196 | | − /* |
| 3197 | | − * If current does not own the pi_state then the futex is |
| 3198 | | − * inconsistent and user space fiddled with the futex value. |
| 3199 | | − */ |
| 3200 | | − if (pi_state->owner != current) |
| 3201 | | − goto out_unlock; |
| 3202 | | − |
| 3203 | | − get_pi_state(pi_state); |
| 3204 | | − /* |
| 3205 | | − * By taking wait_lock while still holding hb->lock, we ensure |
| 3206 | | − * there is no point where we hold neither; and therefore |
| 3207 | | − * wake_futex_pi() must observe a state consistent with what we |
| 3208 | | − * observed. |
| 3209 | | − * |
| 3210 | | − * In particular; this forces __rt_mutex_start_proxy() to |
| 3211 | | − * complete such that we're guaranteed to observe the |
| 3212 | | − * rt_waiter. Also see the WARN in wake_futex_pi(). |
| 3213 | | − */ |
| 3214 | | − raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 3215 | | − spin_unlock(&hb->lock); |
| 3216 | | − |
| 3217 | | − /* drops pi_state->pi_mutex.wait_lock */ |
| 3218 | | − ret = wake_futex_pi(uaddr, uval, pi_state); |
| 3219 | | − |
| 3220 | | − put_pi_state(pi_state); |
| 3221 | | − |
| 3222 | | − /* |
| 3223 | | − * Success, we're done! No tricky corner cases. |
| 3224 | | − */ |
| 3225 | | − if (!ret) |
| 3226 | | − return ret; |
| 3227 | | − /* |
| 3228 | | − * The atomic access to the futex value generated a |
| 3229 | | − * pagefault, so retry the user-access and the wakeup: |
| 3230 | | − */ |
| 3231 | | − if (ret == -EFAULT) |
| 3232 | | − goto pi_faulted; |
| 3233 | | − /* |
| 3234 | | − * A unconditional UNLOCK_PI op raced against a waiter |
| 3235 | | − * setting the FUTEX_WAITERS bit. Try again. |
| 3236 | | − */ |
| 3237 | | − if (ret == -EAGAIN) |
| 3238 | | − goto pi_retry; |
| 3239 | | − /* |
| 3240 | | − * wake_futex_pi has detected invalid state. Tell user |
| 3241 | | − * space. |
| 3242 | | − */ |
| 3243 | | − return ret; |
| 3244 | | − } |
| 3245 | | − |
| 3246 | | − /* |
| 3247 | | − * We have no kernel internal state, i.e. no waiters in the |
| 3248 | | − * kernel. Waiters which are about to queue themselves are stuck |
| 3249 | | − * on hb->lock. So we can safely ignore them. We do neither |
| 3250 | | − * preserve the WAITERS bit not the OWNER_DIED one. We are the |
| 3251 | | − * owner. |
| 3252 | | − */ |
| 3253 | | − if ((ret = futex_cmpxchg_value_locked(&curval, uaddr, uval, 0))) { |
| 3254 | | − spin_unlock(&hb->lock); |
| 3255 | | − switch (ret) { |
| 3256 | | − case -EFAULT: |
| 3257 | | − goto pi_faulted; |
| 3258 | | − |
| 3259 | | − case -EAGAIN: |
| 3260 | | − goto pi_retry; |
| 3261 | | − |
| 3262 | | − default: |
| 3263 | | − WARN_ON_ONCE(1); |
| 3264 | | − return ret; |
| 3265 | | − } |
| 3266 | | − } |
| 3267 | | − |
| 3268 | | − /* |
| 3269 | | − * If uval has changed, let user space handle it. |
| 3270 | | − */ |
| 3271 | | − ret = (curval == uval) ? 0 : -EAGAIN; |
| 3272 | | − |
| 3273 | | −out_unlock: |
| 3274 | | − spin_unlock(&hb->lock); |
| 3275 | | − return ret; |
| 3276 | | − |
| 3277 | | −pi_retry: |
| 3278 | | − cond_resched(); |
| 3279 | | − goto retry; |
| 3280 | | − |
| 3281 | | −pi_faulted: |
| 3282 | | − |
| 3283 | | − ret = fault_in_user_writeable(uaddr); |
| 3284 | | − if (!ret) |
| 3285 | | − goto retry; |
| 3286 | | − |
| 3287 | | − return ret; |
| 3288 | | −} |
| 3289 | | − |
| 3290 | 1903 | /** |
| 3291 | 1904 | * handle_early_requeue_pi_wakeup() - Handle early wakeup on the initial futex |
| 3292 | 1905 | * @hb: the hash_bucket futex_q was original enqueued on |
| @@ -3441,7 +2054,7 @@ int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, · 148 unmodified lines |
| 3441 | 2054 | /* The requeue acquired the lock */ |
| 3442 | 2055 | if (q.pi_state && (q.pi_state->owner != current)) { |
| 3443 | 2056 | spin_lock(q.lock_ptr); |
| 3444 | | − ret = fixup_owner(uaddr2, &q, true); |
| 2057 | + ret = fixup_pi_owner(uaddr2, &q, true); |
| 3445 | 2058 | /* |
| 3446 | 2059 | * Drop the reference to the pi state which the |
| 3447 | 2060 | * requeue_pi() code acquired for us. |
| @@ -3471,9 +2084,9 @@ int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, · 23 unmodified lines |
| 3471 | 2084 | * Fixup the pi_state owner and possibly acquire the lock if we |
| 3472 | 2085 | * haven't already. |
| 3473 | 2086 | */ |
| 3474 | | − res = fixup_owner(uaddr2, &q, !ret); |
| 2087 | + res = fixup_pi_owner(uaddr2, &q, !ret); |
| 3475 | 2088 | /* |
| 3476 | | − * If fixup_owner() returned an error, propagate that. If it |
| 2089 | + * If fixup_pi_owner() returned an error, propagate that. If it |
| 3477 | 2090 | * acquired the lock, clear -ETIMEDOUT or -EINTR. |
| 3478 | 2091 | */ |
| 3479 | 2092 | if (res) |
| @@ -3811,6 +2424,87 @@ static void compat_exit_robust_list(struct task_struct *curr) · 331 unmodified lines |
| 3811 | 2424 | } |
| 3812 | 2425 | #endif |
| 3813 | 2426 | |
| 2427 | +#ifdef CONFIG_FUTEX_PI |
| 2428 | + |
| 2429 | +/* |
| 2430 | + * This task is holding PI mutexes at exit time => bad. |
| 2431 | + * Kernel cleans up PI-state, but userspace is likely hosed. |
| 2432 | + * (Robust-futex cleanup is separate and might save the day for userspace.) |
| 2433 | + */ |
| 2434 | +static void exit_pi_state_list(struct task_struct *curr) |
| 2435 | +{ |
| 2436 | + struct list_head *next, *head = &curr->pi_state_list; |
| 2437 | + struct futex_pi_state *pi_state; |
| 2438 | + struct futex_hash_bucket *hb; |
| 2439 | + union futex_key key = FUTEX_KEY_INIT; |
| 2440 | + |
| 2441 | + if (!futex_cmpxchg_enabled) |
| 2442 | + return; |
| 2443 | + /* |
| 2444 | + * We are a ZOMBIE and nobody can enqueue itself on |
| 2445 | + * pi_state_list anymore, but we have to be careful |
| 2446 | + * versus waiters unqueueing themselves: |
| 2447 | + */ |
| 2448 | + raw_spin_lock_irq(&curr->pi_lock); |
| 2449 | + while (!list_empty(head)) { |
| 2450 | + next = head->next; |
| 2451 | + pi_state = list_entry(next, struct futex_pi_state, list); |
| 2452 | + key = pi_state->key; |
| 2453 | + hb = futex_hash(&key); |
| 2454 | + |
| 2455 | + /* |
| 2456 | + * We can race against put_pi_state() removing itself from the |
| 2457 | + * list (a waiter going away). put_pi_state() will first |
| 2458 | + * decrement the reference count and then modify the list, so |
| 2459 | + * its possible to see the list entry but fail this reference |
| 2460 | + * acquire. |
| 2461 | + * |
| 2462 | + * In that case; drop the locks to let put_pi_state() make |
| 2463 | + * progress and retry the loop. |
| 2464 | + */ |
| 2465 | + if (!refcount_inc_not_zero(&pi_state->refcount)) { |
| 2466 | + raw_spin_unlock_irq(&curr->pi_lock); |
| 2467 | + cpu_relax(); |
| 2468 | + raw_spin_lock_irq(&curr->pi_lock); |
| 2469 | + continue; |
| 2470 | + } |
| 2471 | + raw_spin_unlock_irq(&curr->pi_lock); |
| 2472 | + |
| 2473 | + spin_lock(&hb->lock); |
| 2474 | + raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 2475 | + raw_spin_lock(&curr->pi_lock); |
| 2476 | + /* |
| 2477 | + * We dropped the pi-lock, so re-check whether this |
| 2478 | + * task still owns the PI-state: |
| 2479 | + */ |
| 2480 | + if (head->next != next) { |
| 2481 | + /* retain curr->pi_lock for the loop invariant */ |
| 2482 | + raw_spin_unlock(&pi_state->pi_mutex.wait_lock); |
| 2483 | + spin_unlock(&hb->lock); |
| 2484 | + put_pi_state(pi_state); |
| 2485 | + continue; |
| 2486 | + } |
| 2487 | + |
| 2488 | + WARN_ON(pi_state->owner != curr); |
| 2489 | + WARN_ON(list_empty(&pi_state->list)); |
| 2490 | + list_del_init(&pi_state->list); |
| 2491 | + pi_state->owner = NULL; |
| 2492 | + |
| 2493 | + raw_spin_unlock(&curr->pi_lock); |
| 2494 | + raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 2495 | + spin_unlock(&hb->lock); |
| 2496 | + |
| 2497 | + rt_mutex_futex_unlock(&pi_state->pi_mutex); |
| 2498 | + put_pi_state(pi_state); |
| 2499 | + |
| 2500 | + raw_spin_lock_irq(&curr->pi_lock); |
| 2501 | + } |
| 2502 | + raw_spin_unlock_irq(&curr->pi_lock); |
| 2503 | +} |
| 2504 | +#else |
| 2505 | +static inline void exit_pi_state_list(struct task_struct *curr) { } |
| 2506 | +#endif |
| 2507 | + |
| 3814 | 2508 | static void futex_cleanup(struct task_struct *tsk) |
| 3815 | 2509 | { |
| 3816 | 2510 | if (unlikely(tsk->robust_list)) { |