Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Fast Userspace Mutexes (which I call "Futexes!"). |
| 4 | * (C) Rusty Russell, IBM 2002 |
| 5 | * |
| 6 | * Generalized futexes, futex requeueing, misc fixes by Ingo Molnar |
| 7 | * (C) Copyright 2003 Red Hat Inc, All Rights Reserved |
| 8 | * |
| 9 | * Removed page pinning, fix privately mapped COW pages and other cleanups |
| 10 | * (C) Copyright 2003, 2004 Jamie Lokier |
| 11 | * |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 12 | * Robust futex support started by Ingo Molnar |
| 13 | * (C) Copyright 2006 Red Hat Inc, All Rights Reserved |
| 14 | * Thanks to Thomas Gleixner for suggestions, analysis and fixes. |
| 15 | * |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 16 | * PI-futex support started by Ingo Molnar and Thomas Gleixner |
| 17 | * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> |
| 18 | * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com> |
| 19 | * |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 20 | * PRIVATE futexes by Eric Dumazet |
| 21 | * Copyright (C) 2007 Eric Dumazet <dada1@cosmosbay.com> |
| 22 | * |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 23 | * Requeue-PI support by Darren Hart <dvhltc@us.ibm.com> |
| 24 | * Copyright (C) IBM Corporation, 2009 |
| 25 | * Thanks to Thomas Gleixner for conceptual design and careful reviews. |
| 26 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly |
| 28 | * enough at me, Linus for the original (flawed) idea, Matthew |
| 29 | * Kirkwood for proof-of-concept implementation. |
| 30 | * |
| 31 | * "The futexes are also cursed." |
| 32 | * "But they come in a choice of three flavours!" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 34 | #include <linux/compat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/jhash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/pagemap.h> |
| 37 | #include <linux/syscalls.h> |
Colin Cross | 88c8004 | 2013-05-01 18:35:05 -0700 | [diff] [blame] | 38 | #include <linux/freezer.h> |
Mike Rapoport | 57c8a66 | 2018-10-30 15:09:49 -0700 | [diff] [blame] | 39 | #include <linux/memblock.h> |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 40 | #include <linux/fault-inject.h> |
Andrei Vagin | c2f7d08 | 2020-10-15 09:00:19 -0700 | [diff] [blame] | 41 | #include <linux/time_namespace.h> |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 42 | |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 43 | #include <asm/futex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Peter Zijlstra | 1696a8b | 2013-10-31 18:18:19 +0100 | [diff] [blame] | 45 | #include "locking/rtmutex_common.h" |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 46 | |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 47 | /* |
Davidlohr Bueso | d7e8af1 | 2014-04-09 11:55:07 -0700 | [diff] [blame] | 48 | * READ this before attempting to hack on futexes! |
| 49 | * |
| 50 | * Basic futex operation and ordering guarantees |
| 51 | * ============================================= |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 52 | * |
| 53 | * The waiter reads the futex value in user space and calls |
| 54 | * futex_wait(). This function computes the hash bucket and acquires |
| 55 | * the hash bucket lock. After that it reads the futex user space value |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 56 | * again and verifies that the data has not changed. If it has not changed |
| 57 | * it enqueues itself into the hash bucket, releases the hash bucket lock |
| 58 | * and schedules. |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 59 | * |
| 60 | * The waker side modifies the user space value of the futex and calls |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 61 | * futex_wake(). This function computes the hash bucket and acquires the |
| 62 | * hash bucket lock. Then it looks for waiters on that futex in the hash |
| 63 | * bucket and wakes them. |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 64 | * |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 65 | * In futex wake up scenarios where no tasks are blocked on a futex, taking |
| 66 | * the hb spinlock can be avoided and simply return. In order for this |
| 67 | * optimization to work, ordering guarantees must exist so that the waiter |
| 68 | * being added to the list is acknowledged when the list is concurrently being |
| 69 | * checked by the waker, avoiding scenarios like the following: |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 70 | * |
| 71 | * CPU 0 CPU 1 |
| 72 | * val = *futex; |
| 73 | * sys_futex(WAIT, futex, val); |
| 74 | * futex_wait(futex, val); |
| 75 | * uval = *futex; |
| 76 | * *futex = newval; |
| 77 | * sys_futex(WAKE, futex); |
| 78 | * futex_wake(futex); |
| 79 | * if (queue_empty()) |
| 80 | * return; |
| 81 | * if (uval == val) |
| 82 | * lock(hash_bucket(futex)); |
| 83 | * queue(); |
| 84 | * unlock(hash_bucket(futex)); |
| 85 | * schedule(); |
| 86 | * |
| 87 | * This would cause the waiter on CPU 0 to wait forever because it |
| 88 | * missed the transition of the user space value from val to newval |
| 89 | * and the waker did not find the waiter in the hash bucket queue. |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 90 | * |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 91 | * The correct serialization ensures that a waiter either observes |
| 92 | * the changed user space value before blocking or is woken by a |
| 93 | * concurrent waker: |
| 94 | * |
| 95 | * CPU 0 CPU 1 |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 96 | * val = *futex; |
| 97 | * sys_futex(WAIT, futex, val); |
| 98 | * futex_wait(futex, val); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 99 | * |
Davidlohr Bueso | d7e8af1 | 2014-04-09 11:55:07 -0700 | [diff] [blame] | 100 | * waiters++; (a) |
Davidlohr Bueso | 8ad7b37 | 2016-02-09 11:15:13 -0800 | [diff] [blame] | 101 | * smp_mb(); (A) <-- paired with -. |
| 102 | * | |
| 103 | * lock(hash_bucket(futex)); | |
| 104 | * | |
| 105 | * uval = *futex; | |
| 106 | * | *futex = newval; |
| 107 | * | sys_futex(WAKE, futex); |
| 108 | * | futex_wake(futex); |
| 109 | * | |
| 110 | * `--------> smp_mb(); (B) |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 111 | * if (uval == val) |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 112 | * queue(); |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 113 | * unlock(hash_bucket(futex)); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 114 | * schedule(); if (waiters) |
| 115 | * lock(hash_bucket(futex)); |
Davidlohr Bueso | d7e8af1 | 2014-04-09 11:55:07 -0700 | [diff] [blame] | 116 | * else wake_waiters(futex); |
| 117 | * waiters--; (b) unlock(hash_bucket(futex)); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 118 | * |
Davidlohr Bueso | d7e8af1 | 2014-04-09 11:55:07 -0700 | [diff] [blame] | 119 | * Where (A) orders the waiters increment and the futex value read through |
| 120 | * atomic operations (see hb_waiters_inc) and where (B) orders the write |
Peter Zijlstra | 4b39f99 | 2020-03-04 13:24:24 +0100 | [diff] [blame] | 121 | * to futex and the waiters read (see hb_waiters_pending()). |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 122 | * |
| 123 | * This yields the following case (where X:=waiters, Y:=futex): |
| 124 | * |
| 125 | * X = Y = 0 |
| 126 | * |
| 127 | * w[X]=1 w[Y]=1 |
| 128 | * MB MB |
| 129 | * r[Y]=y r[X]=x |
| 130 | * |
| 131 | * Which guarantees that x==0 && y==0 is impossible; which translates back into |
| 132 | * the guarantee that we cannot both miss the futex variable change and the |
| 133 | * enqueue. |
Davidlohr Bueso | d7e8af1 | 2014-04-09 11:55:07 -0700 | [diff] [blame] | 134 | * |
| 135 | * Note that a new waiter is accounted for in (a) even when it is possible that |
| 136 | * the wait call can return error, in which case we backtrack from it in (b). |
| 137 | * Refer to the comment in queue_lock(). |
| 138 | * |
| 139 | * Similarly, in order to account for waiters being requeued on another |
| 140 | * address we always increment the waiters for the destination bucket before |
| 141 | * acquiring the lock. It then decrements them again after releasing it - |
| 142 | * the code that actually moves the futex(es) between hash buckets (requeue_futex) |
| 143 | * will do the additional required waiter count housekeeping. This is done for |
| 144 | * double_lock_hb() and double_unlock_hb(), respectively. |
Thomas Gleixner | 99b60ce | 2014-01-12 15:31:24 -0800 | [diff] [blame] | 145 | */ |
| 146 | |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 147 | #ifdef CONFIG_HAVE_FUTEX_CMPXCHG |
| 148 | #define futex_cmpxchg_enabled 1 |
| 149 | #else |
| 150 | static int __read_mostly futex_cmpxchg_enabled; |
Heiko Carstens | 03b8c7b | 2014-03-02 13:09:47 +0100 | [diff] [blame] | 151 | #endif |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | /* |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 154 | * Futex flags used to encode options to functions and preserve them across |
| 155 | * restarts. |
| 156 | */ |
Thomas Gleixner | 784bdf3 | 2016-07-29 16:32:30 +0200 | [diff] [blame] | 157 | #ifdef CONFIG_MMU |
| 158 | # define FLAGS_SHARED 0x01 |
| 159 | #else |
| 160 | /* |
| 161 | * NOMMU does not have per process address space. Let the compiler optimize |
| 162 | * code away. |
| 163 | */ |
| 164 | # define FLAGS_SHARED 0x00 |
| 165 | #endif |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 166 | #define FLAGS_CLOCKRT 0x02 |
| 167 | #define FLAGS_HAS_TIMEOUT 0x04 |
| 168 | |
| 169 | /* |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 170 | * Priority Inheritance state: |
| 171 | */ |
| 172 | struct futex_pi_state { |
| 173 | /* |
| 174 | * list of 'owned' pi_state instances - these have to be |
| 175 | * cleaned up in do_exit() if the task exits prematurely: |
| 176 | */ |
| 177 | struct list_head list; |
| 178 | |
| 179 | /* |
| 180 | * The PI object: |
| 181 | */ |
Peter Zijlstra | 830e6ac | 2021-08-15 23:27:58 +0200 | [diff] [blame] | 182 | struct rt_mutex_base pi_mutex; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 183 | |
| 184 | struct task_struct *owner; |
Elena Reshetova | 49262de | 2019-02-05 14:24:27 +0200 | [diff] [blame] | 185 | refcount_t refcount; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 186 | |
| 187 | union futex_key key; |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 188 | } __randomize_layout; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 189 | |
Darren Hart | d8d88fb | 2009-09-21 22:30:30 -0700 | [diff] [blame] | 190 | /** |
| 191 | * struct futex_q - The hashed futex queue entry, one per waiting task |
Randy Dunlap | fb62db2 | 2010-10-13 11:02:34 -0700 | [diff] [blame] | 192 | * @list: priority-sorted list of tasks waiting on this futex |
Darren Hart | d8d88fb | 2009-09-21 22:30:30 -0700 | [diff] [blame] | 193 | * @task: the task waiting on the futex |
| 194 | * @lock_ptr: the hash bucket lock |
| 195 | * @key: the key the futex is hashed on |
| 196 | * @pi_state: optional priority inheritance state |
| 197 | * @rt_waiter: rt_waiter storage for use with requeue_pi |
| 198 | * @requeue_pi_key: the requeue_pi target futex key |
| 199 | * @bitset: bitset for the optional bitmasked wakeup |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 200 | * @requeue_state: State field for futex_requeue_pi() |
| 201 | * @requeue_wait: RCU wait for futex_requeue_pi() (RT only) |
Darren Hart | d8d88fb | 2009-09-21 22:30:30 -0700 | [diff] [blame] | 202 | * |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 203 | * We use this hashed waitqueue, instead of a normal wait_queue_entry_t, so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * we can wake only the relevant ones (hashed queues may be shared). |
| 205 | * |
| 206 | * A futex_q has a woken state, just like tasks have TASK_RUNNING. |
Pierre Peiffer | ec92d08 | 2007-05-09 02:35:00 -0700 | [diff] [blame] | 207 | * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0. |
Randy Dunlap | fb62db2 | 2010-10-13 11:02:34 -0700 | [diff] [blame] | 208 | * The order of wakeup is always to make the first condition true, then |
Darren Hart | d8d88fb | 2009-09-21 22:30:30 -0700 | [diff] [blame] | 209 | * the second. |
| 210 | * |
| 211 | * PI futexes are typically woken before they are removed from the hash list via |
| 212 | * the rt_mutex code. See unqueue_me_pi(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | */ |
| 214 | struct futex_q { |
Pierre Peiffer | ec92d08 | 2007-05-09 02:35:00 -0700 | [diff] [blame] | 215 | struct plist_node list; |
Darren Hart | d8d88fb | 2009-09-21 22:30:30 -0700 | [diff] [blame] | 216 | |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 217 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | spinlock_t *lock_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | union futex_key key; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 220 | struct futex_pi_state *pi_state; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 221 | struct rt_mutex_waiter *rt_waiter; |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 222 | union futex_key *requeue_pi_key; |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 223 | u32 bitset; |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 224 | atomic_t requeue_state; |
| 225 | #ifdef CONFIG_PREEMPT_RT |
| 226 | struct rcuwait requeue_wait; |
| 227 | #endif |
Kees Cook | 3859a27 | 2016-10-28 01:22:25 -0700 | [diff] [blame] | 228 | } __randomize_layout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 230 | /* |
| 231 | * On PREEMPT_RT, the hash bucket lock is a 'sleeping' spinlock with an |
| 232 | * underlying rtmutex. The task which is about to be requeued could have |
| 233 | * just woken up (timeout, signal). After the wake up the task has to |
| 234 | * acquire hash bucket lock, which is held by the requeue code. As a task |
| 235 | * can only be blocked on _ONE_ rtmutex at a time, the proxy lock blocking |
| 236 | * and the hash bucket lock blocking would collide and corrupt state. |
| 237 | * |
| 238 | * On !PREEMPT_RT this is not a problem and everything could be serialized |
| 239 | * on hash bucket lock, but aside of having the benefit of common code, |
| 240 | * this allows to avoid doing the requeue when the task is already on the |
| 241 | * way out and taking the hash bucket lock of the original uaddr1 when the |
| 242 | * requeue has been completed. |
| 243 | * |
| 244 | * The following state transitions are valid: |
| 245 | * |
| 246 | * On the waiter side: |
| 247 | * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_IGNORE |
| 248 | * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_WAIT |
| 249 | * |
| 250 | * On the requeue side: |
| 251 | * Q_REQUEUE_PI_NONE -> Q_REQUEUE_PI_INPROGRESS |
| 252 | * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_DONE/LOCKED |
| 253 | * Q_REQUEUE_PI_IN_PROGRESS -> Q_REQUEUE_PI_NONE (requeue failed) |
| 254 | * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_DONE/LOCKED |
| 255 | * Q_REQUEUE_PI_WAIT -> Q_REQUEUE_PI_IGNORE (requeue failed) |
| 256 | * |
| 257 | * The requeue side ignores a waiter with state Q_REQUEUE_PI_IGNORE as this |
| 258 | * signals that the waiter is already on the way out. It also means that |
| 259 | * the waiter is still on the 'wait' futex, i.e. uaddr1. |
| 260 | * |
| 261 | * The waiter side signals early wakeup to the requeue side either through |
| 262 | * setting state to Q_REQUEUE_PI_IGNORE or to Q_REQUEUE_PI_WAIT depending |
| 263 | * on the current state. In case of Q_REQUEUE_PI_IGNORE it can immediately |
| 264 | * proceed to take the hash bucket lock of uaddr1. If it set state to WAIT, |
| 265 | * which means the wakeup is interleaving with a requeue in progress it has |
| 266 | * to wait for the requeue side to change the state. Either to DONE/LOCKED |
| 267 | * or to IGNORE. DONE/LOCKED means the waiter q is now on the uaddr2 futex |
| 268 | * and either blocked (DONE) or has acquired it (LOCKED). IGNORE is set by |
| 269 | * the requeue side when the requeue attempt failed via deadlock detection |
| 270 | * and therefore the waiter q is still on the uaddr1 futex. |
| 271 | */ |
| 272 | enum { |
| 273 | Q_REQUEUE_PI_NONE = 0, |
| 274 | Q_REQUEUE_PI_IGNORE, |
| 275 | Q_REQUEUE_PI_IN_PROGRESS, |
| 276 | Q_REQUEUE_PI_WAIT, |
| 277 | Q_REQUEUE_PI_DONE, |
| 278 | Q_REQUEUE_PI_LOCKED, |
| 279 | }; |
| 280 | |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 281 | static const struct futex_q futex_q_init = { |
| 282 | /* list gets initialized in queue_me()*/ |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 283 | .key = FUTEX_KEY_INIT, |
| 284 | .bitset = FUTEX_BITSET_MATCH_ANY, |
| 285 | .requeue_state = ATOMIC_INIT(Q_REQUEUE_PI_NONE), |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 286 | }; |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | /* |
Darren Hart | b2d0994 | 2009-03-12 00:55:37 -0700 | [diff] [blame] | 289 | * Hash buckets are shared by all the futex_keys that hash to the same |
| 290 | * location. Each key may have multiple futex_q structures, one for each task |
| 291 | * waiting on a futex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | */ |
| 293 | struct futex_hash_bucket { |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 294 | atomic_t waiters; |
Pierre Peiffer | ec92d08 | 2007-05-09 02:35:00 -0700 | [diff] [blame] | 295 | spinlock_t lock; |
| 296 | struct plist_head chain; |
Davidlohr Bueso | a52b89e | 2014-01-12 15:31:23 -0800 | [diff] [blame] | 297 | } ____cacheline_aligned_in_smp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Rasmus Villemoes | ac742d3 | 2015-09-09 23:36:40 +0200 | [diff] [blame] | 299 | /* |
| 300 | * The base of the bucket array and its size are always used together |
| 301 | * (after initialization only in hash_futex()), so ensure that they |
| 302 | * reside in the same cacheline. |
| 303 | */ |
| 304 | static struct { |
| 305 | struct futex_hash_bucket *queues; |
| 306 | unsigned long hashsize; |
| 307 | } __futex_data __read_mostly __aligned(2*sizeof(long)); |
| 308 | #define futex_queues (__futex_data.queues) |
| 309 | #define futex_hashsize (__futex_data.hashsize) |
Davidlohr Bueso | a52b89e | 2014-01-12 15:31:23 -0800 | [diff] [blame] | 310 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 312 | /* |
| 313 | * Fault injections for futexes. |
| 314 | */ |
| 315 | #ifdef CONFIG_FAIL_FUTEX |
| 316 | |
| 317 | static struct { |
| 318 | struct fault_attr attr; |
| 319 | |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 320 | bool ignore_private; |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 321 | } fail_futex = { |
| 322 | .attr = FAULT_ATTR_INITIALIZER, |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 323 | .ignore_private = false, |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | static int __init setup_fail_futex(char *str) |
| 327 | { |
| 328 | return setup_fault_attr(&fail_futex.attr, str); |
| 329 | } |
| 330 | __setup("fail_futex=", setup_fail_futex); |
| 331 | |
kbuild test robot | 5d285a7 | 2015-07-21 01:40:45 +0800 | [diff] [blame] | 332 | static bool should_fail_futex(bool fshared) |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 333 | { |
| 334 | if (fail_futex.ignore_private && !fshared) |
| 335 | return false; |
| 336 | |
| 337 | return should_fail(&fail_futex.attr, 1); |
| 338 | } |
| 339 | |
| 340 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
| 341 | |
| 342 | static int __init fail_futex_debugfs(void) |
| 343 | { |
| 344 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
| 345 | struct dentry *dir; |
| 346 | |
| 347 | dir = fault_create_debugfs_attr("fail_futex", NULL, |
| 348 | &fail_futex.attr); |
| 349 | if (IS_ERR(dir)) |
| 350 | return PTR_ERR(dir); |
| 351 | |
Greg Kroah-Hartman | 0365aeb | 2019-01-22 16:21:39 +0100 | [diff] [blame] | 352 | debugfs_create_bool("ignore-private", mode, dir, |
| 353 | &fail_futex.ignore_private); |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | late_initcall(fail_futex_debugfs); |
| 358 | |
| 359 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |
| 360 | |
| 361 | #else |
| 362 | static inline bool should_fail_futex(bool fshared) |
| 363 | { |
| 364 | return false; |
| 365 | } |
| 366 | #endif /* CONFIG_FAIL_FUTEX */ |
| 367 | |
Thomas Gleixner | ba31c1a4 | 2019-11-06 22:55:36 +0100 | [diff] [blame] | 368 | #ifdef CONFIG_COMPAT |
| 369 | static void compat_exit_robust_list(struct task_struct *curr); |
Thomas Gleixner | ba31c1a4 | 2019-11-06 22:55:36 +0100 | [diff] [blame] | 370 | #endif |
| 371 | |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 372 | /* |
| 373 | * Reflects a new waiter being added to the waitqueue. |
| 374 | */ |
| 375 | static inline void hb_waiters_inc(struct futex_hash_bucket *hb) |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 376 | { |
| 377 | #ifdef CONFIG_SMP |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 378 | atomic_inc(&hb->waiters); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 379 | /* |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 380 | * Full barrier (A), see the ordering comment above. |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 381 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 382 | smp_mb__after_atomic(); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 383 | #endif |
| 384 | } |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 385 | |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 386 | /* |
| 387 | * Reflects a waiter being removed from the waitqueue by wakeup |
| 388 | * paths. |
| 389 | */ |
| 390 | static inline void hb_waiters_dec(struct futex_hash_bucket *hb) |
| 391 | { |
| 392 | #ifdef CONFIG_SMP |
| 393 | atomic_dec(&hb->waiters); |
| 394 | #endif |
| 395 | } |
| 396 | |
| 397 | static inline int hb_waiters_pending(struct futex_hash_bucket *hb) |
| 398 | { |
| 399 | #ifdef CONFIG_SMP |
Peter Zijlstra | 4b39f99 | 2020-03-04 13:24:24 +0100 | [diff] [blame] | 400 | /* |
| 401 | * Full barrier (B), see the ordering comment above. |
| 402 | */ |
| 403 | smp_mb(); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 404 | return atomic_read(&hb->waiters); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 405 | #else |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 406 | return 1; |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 407 | #endif |
| 408 | } |
| 409 | |
Thomas Gleixner | e8b61b3 | 2016-06-01 10:43:29 +0200 | [diff] [blame] | 410 | /** |
| 411 | * hash_futex - Return the hash bucket in the global hash |
| 412 | * @key: Pointer to the futex key for which the hash is calculated |
| 413 | * |
| 414 | * We hash on the keys returned from get_futex_key (see below) and return the |
| 415 | * corresponding hash bucket in the global hash. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | */ |
| 417 | static struct futex_hash_bucket *hash_futex(union futex_key *key) |
| 418 | { |
Thomas Gleixner | 8d67743 | 2020-03-08 19:07:17 +0100 | [diff] [blame] | 419 | u32 hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | key->both.offset); |
Thomas Gleixner | 8d67743 | 2020-03-08 19:07:17 +0100 | [diff] [blame] | 421 | |
Davidlohr Bueso | a52b89e | 2014-01-12 15:31:23 -0800 | [diff] [blame] | 422 | return &futex_queues[hash & (futex_hashsize - 1)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Thomas Gleixner | e8b61b3 | 2016-06-01 10:43:29 +0200 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * match_futex - Check whether two futex keys are equal |
| 428 | * @key1: Pointer to key1 |
| 429 | * @key2: Pointer to key2 |
| 430 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * Return 1 if two futex_keys are equal, 0 otherwise. |
| 432 | */ |
| 433 | static inline int match_futex(union futex_key *key1, union futex_key *key2) |
| 434 | { |
Darren Hart | 2bc8720 | 2009-10-14 10:12:39 -0700 | [diff] [blame] | 435 | return (key1 && key2 |
| 436 | && key1->both.word == key2->both.word |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | && key1->both.ptr == key2->both.ptr |
| 438 | && key1->both.offset == key2->both.offset); |
| 439 | } |
| 440 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 441 | enum futex_access { |
| 442 | FUTEX_READ, |
| 443 | FUTEX_WRITE |
| 444 | }; |
| 445 | |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 446 | /** |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 447 | * futex_setup_timer - set up the sleeping hrtimer. |
| 448 | * @time: ptr to the given timeout value |
| 449 | * @timeout: the hrtimer_sleeper structure to be set up |
| 450 | * @flags: futex flags |
| 451 | * @range_ns: optional range in ns |
| 452 | * |
| 453 | * Return: Initialized hrtimer_sleeper structure or NULL if no timeout |
| 454 | * value given |
| 455 | */ |
| 456 | static inline struct hrtimer_sleeper * |
| 457 | futex_setup_timer(ktime_t *time, struct hrtimer_sleeper *timeout, |
| 458 | int flags, u64 range_ns) |
| 459 | { |
| 460 | if (!time) |
| 461 | return NULL; |
| 462 | |
Sebastian Andrzej Siewior | dbc1625 | 2019-07-26 20:30:50 +0200 | [diff] [blame] | 463 | hrtimer_init_sleeper_on_stack(timeout, (flags & FLAGS_CLOCKRT) ? |
| 464 | CLOCK_REALTIME : CLOCK_MONOTONIC, |
| 465 | HRTIMER_MODE_ABS); |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 466 | /* |
| 467 | * If range_ns is 0, calling hrtimer_set_expires_range_ns() is |
| 468 | * effectively the same as calling hrtimer_set_expires(). |
| 469 | */ |
| 470 | hrtimer_set_expires_range_ns(&timeout->timer, *time, range_ns); |
| 471 | |
| 472 | return timeout; |
| 473 | } |
| 474 | |
Peter Zijlstra | 8019ad1 | 2020-03-04 11:28:31 +0100 | [diff] [blame] | 475 | /* |
| 476 | * Generate a machine wide unique identifier for this inode. |
| 477 | * |
| 478 | * This relies on u64 not wrapping in the life-time of the machine; which with |
| 479 | * 1ns resolution means almost 585 years. |
| 480 | * |
| 481 | * This further relies on the fact that a well formed program will not unmap |
| 482 | * the file while it has a (shared) futex waiting on it. This mapping will have |
| 483 | * a file reference which pins the mount and inode. |
| 484 | * |
| 485 | * If for some reason an inode gets evicted and read back in again, it will get |
| 486 | * a new sequence number and will _NOT_ match, even though it is the exact same |
| 487 | * file. |
| 488 | * |
| 489 | * It is important that match_futex() will never have a false-positive, esp. |
| 490 | * for PI futexes that can mess up the state. The above argues that false-negatives |
| 491 | * are only possible for malformed programs. |
| 492 | */ |
| 493 | static u64 get_inode_sequence_number(struct inode *inode) |
| 494 | { |
| 495 | static atomic64_t i_seq; |
| 496 | u64 old; |
| 497 | |
| 498 | /* Does the inode already have a sequence number? */ |
| 499 | old = atomic64_read(&inode->i_sequence); |
| 500 | if (likely(old)) |
| 501 | return old; |
| 502 | |
| 503 | for (;;) { |
| 504 | u64 new = atomic64_add_return(1, &i_seq); |
| 505 | if (WARN_ON_ONCE(!new)) |
| 506 | continue; |
| 507 | |
| 508 | old = atomic64_cmpxchg_relaxed(&inode->i_sequence, 0, new); |
| 509 | if (old) |
| 510 | return old; |
| 511 | return new; |
| 512 | } |
| 513 | } |
| 514 | |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 515 | /** |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 516 | * get_futex_key() - Get parameters which are the keys for a futex |
| 517 | * @uaddr: virtual address of the futex |
André Almeida | 9261308 | 2020-07-02 17:28:43 -0300 | [diff] [blame] | 518 | * @fshared: false for a PROCESS_PRIVATE futex, true for PROCESS_SHARED |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 519 | * @key: address where result is stored. |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 520 | * @rw: mapping needs to be read/write (values: FUTEX_READ, |
| 521 | * FUTEX_WRITE) |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 522 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 523 | * Return: a negative error code or 0 |
| 524 | * |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 525 | * The key words are stored in @key on success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | * |
Peter Zijlstra | 8019ad1 | 2020-03-04 11:28:31 +0100 | [diff] [blame] | 527 | * For shared mappings (when @fshared), the key is: |
Mauro Carvalho Chehab | 03c109d | 2020-04-14 18:48:58 +0200 | [diff] [blame] | 528 | * |
Peter Zijlstra | 8019ad1 | 2020-03-04 11:28:31 +0100 | [diff] [blame] | 529 | * ( inode->i_sequence, page->index, offset_within_page ) |
Mauro Carvalho Chehab | 03c109d | 2020-04-14 18:48:58 +0200 | [diff] [blame] | 530 | * |
Peter Zijlstra | 8019ad1 | 2020-03-04 11:28:31 +0100 | [diff] [blame] | 531 | * [ also see get_inode_sequence_number() ] |
| 532 | * |
| 533 | * For private mappings (or when !@fshared), the key is: |
Mauro Carvalho Chehab | 03c109d | 2020-04-14 18:48:58 +0200 | [diff] [blame] | 534 | * |
Peter Zijlstra | 8019ad1 | 2020-03-04 11:28:31 +0100 | [diff] [blame] | 535 | * ( current->mm, address, 0 ) |
| 536 | * |
| 537 | * This allows (cross process, where applicable) identification of the futex |
| 538 | * without keeping the page pinned for the duration of the FUTEX_WAIT. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | * |
Darren Hart | b2d0994 | 2009-03-12 00:55:37 -0700 | [diff] [blame] | 540 | * lock_page() might sleep, the caller should not hold a spinlock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | */ |
André Almeida | 9261308 | 2020-07-02 17:28:43 -0300 | [diff] [blame] | 542 | static int get_futex_key(u32 __user *uaddr, bool fshared, union futex_key *key, |
| 543 | enum futex_access rw) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | { |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 545 | unsigned long address = (unsigned long)uaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | struct mm_struct *mm = current->mm; |
Mel Gorman | 077fa7a | 2016-06-08 14:25:22 +0100 | [diff] [blame] | 547 | struct page *page, *tail; |
Kirill A. Shutemov | 14d27ab | 2016-01-15 16:53:00 -0800 | [diff] [blame] | 548 | struct address_space *mapping; |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 549 | int err, ro = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * The futex address must be "naturally" aligned. |
| 553 | */ |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 554 | key->both.offset = address % PAGE_SIZE; |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 555 | if (unlikely((address % sizeof(u32)) != 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | return -EINVAL; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 557 | address -= key->both.offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 559 | if (unlikely(!access_ok(uaddr, sizeof(u32)))) |
Linus Torvalds | 5cdec2d | 2013-12-12 09:53:51 -0800 | [diff] [blame] | 560 | return -EFAULT; |
| 561 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 562 | if (unlikely(should_fail_futex(fshared))) |
| 563 | return -EFAULT; |
| 564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | /* |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 566 | * PROCESS_PRIVATE futexes are fast. |
| 567 | * As the mm cannot disappear under us and the 'key' only needs |
| 568 | * virtual address, we dont even have to find the underlying vma. |
| 569 | * Note : We do have to check 'uaddr' is a valid user address, |
| 570 | * but access_ok() should be faster than find_vma() |
| 571 | */ |
| 572 | if (!fshared) { |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 573 | key->private.mm = mm; |
| 574 | key->private.address = address; |
| 575 | return 0; |
| 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 578 | again: |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 579 | /* Ignore any VERIFY_READ mapping (futex common case) */ |
André Almeida | 9261308 | 2020-07-02 17:28:43 -0300 | [diff] [blame] | 580 | if (unlikely(should_fail_futex(true))) |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 581 | return -EFAULT; |
| 582 | |
Ira Weiny | 73b0140 | 2019-05-13 17:17:11 -0700 | [diff] [blame] | 583 | err = get_user_pages_fast(address, 1, FOLL_WRITE, &page); |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 584 | /* |
| 585 | * If write access is not required (eg. FUTEX_WAIT), try |
| 586 | * and get read-only access. |
| 587 | */ |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 588 | if (err == -EFAULT && rw == FUTEX_READ) { |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 589 | err = get_user_pages_fast(address, 1, 0, &page); |
| 590 | ro = 1; |
| 591 | } |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 592 | if (err < 0) |
| 593 | return err; |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 594 | else |
| 595 | err = 0; |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 596 | |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 597 | /* |
| 598 | * The treatment of mapping from this point on is critical. The page |
| 599 | * lock protects many things but in this context the page lock |
| 600 | * stabilizes mapping, prevents inode freeing in the shared |
| 601 | * file-backed region case and guards against movement to swap cache. |
| 602 | * |
| 603 | * Strictly speaking the page lock is not needed in all cases being |
| 604 | * considered here and page lock forces unnecessarily serialization |
| 605 | * From this point on, mapping will be re-verified if necessary and |
| 606 | * page lock will be acquired only if it is unavoidable |
Mel Gorman | 077fa7a | 2016-06-08 14:25:22 +0100 | [diff] [blame] | 607 | * |
| 608 | * Mapping checks require the head page for any compound page so the |
| 609 | * head page and mapping is looked up now. For anonymous pages, it |
| 610 | * does not matter if the page splits in the future as the key is |
| 611 | * based on the address. For filesystem-backed pages, the tail is |
| 612 | * required as the index of the page determines the key. For |
| 613 | * base pages, there is no tail page and tail == page. |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 614 | */ |
Mel Gorman | 077fa7a | 2016-06-08 14:25:22 +0100 | [diff] [blame] | 615 | tail = page; |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 616 | page = compound_head(page); |
| 617 | mapping = READ_ONCE(page->mapping); |
| 618 | |
Hugh Dickins | e6780f7 | 2011-12-31 11:44:01 -0800 | [diff] [blame] | 619 | /* |
Kirill A. Shutemov | 14d27ab | 2016-01-15 16:53:00 -0800 | [diff] [blame] | 620 | * If page->mapping is NULL, then it cannot be a PageAnon |
Hugh Dickins | e6780f7 | 2011-12-31 11:44:01 -0800 | [diff] [blame] | 621 | * page; but it might be the ZERO_PAGE or in the gate area or |
| 622 | * in a special mapping (all cases which we are happy to fail); |
| 623 | * or it may have been a good file page when get_user_pages_fast |
| 624 | * found it, but truncated or holepunched or subjected to |
| 625 | * invalidate_complete_page2 before we got the page lock (also |
| 626 | * cases which we are happy to fail). And we hold a reference, |
| 627 | * so refcount care in invalidate_complete_page's remove_mapping |
| 628 | * prevents drop_caches from setting mapping to NULL beneath us. |
| 629 | * |
| 630 | * The case we do have to guard against is when memory pressure made |
| 631 | * shmem_writepage move it from filecache to swapcache beneath us: |
Kirill A. Shutemov | 14d27ab | 2016-01-15 16:53:00 -0800 | [diff] [blame] | 632 | * an unlikely race, but we do need to retry for page->mapping. |
Hugh Dickins | e6780f7 | 2011-12-31 11:44:01 -0800 | [diff] [blame] | 633 | */ |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 634 | if (unlikely(!mapping)) { |
| 635 | int shmem_swizzled; |
| 636 | |
| 637 | /* |
| 638 | * Page lock is required to identify which special case above |
| 639 | * applies. If this is really a shmem page then the page lock |
| 640 | * will prevent unexpected transitions. |
| 641 | */ |
| 642 | lock_page(page); |
| 643 | shmem_swizzled = PageSwapCache(page) || page->mapping; |
Kirill A. Shutemov | 14d27ab | 2016-01-15 16:53:00 -0800 | [diff] [blame] | 644 | unlock_page(page); |
| 645 | put_page(page); |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 646 | |
Hugh Dickins | e6780f7 | 2011-12-31 11:44:01 -0800 | [diff] [blame] | 647 | if (shmem_swizzled) |
| 648 | goto again; |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 649 | |
Hugh Dickins | e6780f7 | 2011-12-31 11:44:01 -0800 | [diff] [blame] | 650 | return -EFAULT; |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 651 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | /* |
| 654 | * Private mappings are handled in a simple way. |
| 655 | * |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 656 | * If the futex key is stored on an anonymous page, then the associated |
| 657 | * object is the mm which is implicitly pinned by the calling process. |
| 658 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | * NOTE: When userspace waits on a MAP_SHARED mapping, even if |
| 660 | * it's a read-only handle, it's expected that futexes attach to |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 661 | * the object not the particular process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | */ |
Kirill A. Shutemov | 14d27ab | 2016-01-15 16:53:00 -0800 | [diff] [blame] | 663 | if (PageAnon(page)) { |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 664 | /* |
| 665 | * A RO anonymous page will never change and thus doesn't make |
| 666 | * sense for futex operations. |
| 667 | */ |
André Almeida | 9261308 | 2020-07-02 17:28:43 -0300 | [diff] [blame] | 668 | if (unlikely(should_fail_futex(true)) || ro) { |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 669 | err = -EFAULT; |
| 670 | goto out; |
| 671 | } |
| 672 | |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 673 | key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | key->private.mm = mm; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 675 | key->private.address = address; |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 676 | |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 677 | } else { |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 678 | struct inode *inode; |
| 679 | |
| 680 | /* |
| 681 | * The associated futex object in this case is the inode and |
| 682 | * the page->mapping must be traversed. Ordinarily this should |
| 683 | * be stabilised under page lock but it's not strictly |
| 684 | * necessary in this case as we just want to pin the inode, not |
| 685 | * update the radix tree or anything like that. |
| 686 | * |
| 687 | * The RCU read lock is taken as the inode is finally freed |
| 688 | * under RCU. If the mapping still matches expectations then the |
| 689 | * mapping->host can be safely accessed as being a valid inode. |
| 690 | */ |
| 691 | rcu_read_lock(); |
| 692 | |
| 693 | if (READ_ONCE(page->mapping) != mapping) { |
| 694 | rcu_read_unlock(); |
| 695 | put_page(page); |
| 696 | |
| 697 | goto again; |
| 698 | } |
| 699 | |
| 700 | inode = READ_ONCE(mapping->host); |
| 701 | if (!inode) { |
| 702 | rcu_read_unlock(); |
| 703 | put_page(page); |
| 704 | |
| 705 | goto again; |
| 706 | } |
| 707 | |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 708 | key->both.offset |= FUT_OFF_INODE; /* inode-based key */ |
Peter Zijlstra | 8019ad1 | 2020-03-04 11:28:31 +0100 | [diff] [blame] | 709 | key->shared.i_seq = get_inode_sequence_number(inode); |
Hugh Dickins | fe19bd3 | 2021-06-24 18:39:52 -0700 | [diff] [blame] | 710 | key->shared.pgoff = page_to_pgoff(tail); |
Mel Gorman | 65d8fc7 | 2016-02-09 11:15:14 -0800 | [diff] [blame] | 711 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 714 | out: |
Kirill A. Shutemov | 14d27ab | 2016-01-15 16:53:00 -0800 | [diff] [blame] | 715 | put_page(page); |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 716 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 719 | /** |
| 720 | * fault_in_user_writeable() - Fault in user address and verify RW access |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 721 | * @uaddr: pointer to faulting user space address |
| 722 | * |
| 723 | * Slow path to fixup the fault we just took in the atomic write |
| 724 | * access to @uaddr. |
| 725 | * |
Randy Dunlap | fb62db2 | 2010-10-13 11:02:34 -0700 | [diff] [blame] | 726 | * We have no generic implementation of a non-destructive write to the |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 727 | * user address. We know that we faulted in the atomic pagefault |
| 728 | * disabled section so we can as well avoid the #PF overhead by |
| 729 | * calling get_user_pages() right away. |
| 730 | */ |
| 731 | static int fault_in_user_writeable(u32 __user *uaddr) |
| 732 | { |
Andi Kleen | 722d017 | 2009-12-08 13:19:42 +0100 | [diff] [blame] | 733 | struct mm_struct *mm = current->mm; |
| 734 | int ret; |
| 735 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 736 | mmap_read_lock(mm); |
Peter Xu | 64019a2 | 2020-08-11 18:39:01 -0700 | [diff] [blame] | 737 | ret = fixup_user_fault(mm, (unsigned long)uaddr, |
Dominik Dingel | 4a9e1cd | 2016-01-15 16:57:04 -0800 | [diff] [blame] | 738 | FAULT_FLAG_WRITE, NULL); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 739 | mmap_read_unlock(mm); |
Andi Kleen | 722d017 | 2009-12-08 13:19:42 +0100 | [diff] [blame] | 740 | |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 741 | return ret < 0 ? ret : 0; |
| 742 | } |
| 743 | |
Darren Hart | 4b1c486 | 2009-04-03 13:39:42 -0700 | [diff] [blame] | 744 | /** |
| 745 | * futex_top_waiter() - Return the highest priority waiter on a futex |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 746 | * @hb: the hash bucket the futex_q's reside in |
| 747 | * @key: the futex key (to distinguish it from other futex futex_q's) |
Darren Hart | 4b1c486 | 2009-04-03 13:39:42 -0700 | [diff] [blame] | 748 | * |
| 749 | * Must be called with the hb lock held. |
| 750 | */ |
| 751 | static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb, |
| 752 | union futex_key *key) |
| 753 | { |
| 754 | struct futex_q *this; |
| 755 | |
| 756 | plist_for_each_entry(this, &hb->chain, list) { |
| 757 | if (match_futex(&this->key, key)) |
| 758 | return this; |
| 759 | } |
| 760 | return NULL; |
| 761 | } |
| 762 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 763 | static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr, |
| 764 | u32 uval, u32 newval) |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 765 | { |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 766 | int ret; |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 767 | |
| 768 | pagefault_disable(); |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 769 | ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval); |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 770 | pagefault_enable(); |
| 771 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 772 | return ret; |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | static int get_futex_value_locked(u32 *dest, u32 __user *from) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
| 777 | int ret; |
| 778 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 779 | pagefault_disable(); |
Linus Torvalds | bd28b14 | 2016-05-22 17:21:27 -0700 | [diff] [blame] | 780 | ret = __get_user(*dest, from); |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame] | 781 | pagefault_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
| 783 | return ret ? -EFAULT : 0; |
| 784 | } |
| 785 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 786 | |
| 787 | /* |
| 788 | * PI code: |
| 789 | */ |
| 790 | static int refill_pi_state_cache(void) |
| 791 | { |
| 792 | struct futex_pi_state *pi_state; |
| 793 | |
| 794 | if (likely(current->pi_state_cache)) |
| 795 | return 0; |
| 796 | |
Burman Yan | 4668edc | 2006-12-06 20:38:51 -0800 | [diff] [blame] | 797 | pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 798 | |
| 799 | if (!pi_state) |
| 800 | return -ENOMEM; |
| 801 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 802 | INIT_LIST_HEAD(&pi_state->list); |
| 803 | /* pi_mutex gets initialized later */ |
| 804 | pi_state->owner = NULL; |
Elena Reshetova | 49262de | 2019-02-05 14:24:27 +0200 | [diff] [blame] | 805 | refcount_set(&pi_state->refcount, 1); |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 806 | pi_state->key = FUTEX_KEY_INIT; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 807 | |
| 808 | current->pi_state_cache = pi_state; |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
Peter Zijlstra | bf92cf3 | 2017-03-22 11:35:53 +0100 | [diff] [blame] | 813 | static struct futex_pi_state *alloc_pi_state(void) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 814 | { |
| 815 | struct futex_pi_state *pi_state = current->pi_state_cache; |
| 816 | |
| 817 | WARN_ON(!pi_state); |
| 818 | current->pi_state_cache = NULL; |
| 819 | |
| 820 | return pi_state; |
| 821 | } |
| 822 | |
Thomas Gleixner | c5cade2 | 2021-01-19 15:21:35 +0100 | [diff] [blame] | 823 | static void pi_state_update_owner(struct futex_pi_state *pi_state, |
| 824 | struct task_struct *new_owner) |
| 825 | { |
| 826 | struct task_struct *old_owner = pi_state->owner; |
| 827 | |
| 828 | lockdep_assert_held(&pi_state->pi_mutex.wait_lock); |
| 829 | |
| 830 | if (old_owner) { |
| 831 | raw_spin_lock(&old_owner->pi_lock); |
| 832 | WARN_ON(list_empty(&pi_state->list)); |
| 833 | list_del_init(&pi_state->list); |
| 834 | raw_spin_unlock(&old_owner->pi_lock); |
| 835 | } |
| 836 | |
| 837 | if (new_owner) { |
| 838 | raw_spin_lock(&new_owner->pi_lock); |
| 839 | WARN_ON(!list_empty(&pi_state->list)); |
| 840 | list_add(&pi_state->list, &new_owner->pi_state_list); |
| 841 | pi_state->owner = new_owner; |
| 842 | raw_spin_unlock(&new_owner->pi_lock); |
| 843 | } |
| 844 | } |
| 845 | |
Peter Zijlstra | bf92cf3 | 2017-03-22 11:35:53 +0100 | [diff] [blame] | 846 | static void get_pi_state(struct futex_pi_state *pi_state) |
| 847 | { |
Elena Reshetova | 49262de | 2019-02-05 14:24:27 +0200 | [diff] [blame] | 848 | WARN_ON_ONCE(!refcount_inc_not_zero(&pi_state->refcount)); |
Peter Zijlstra | bf92cf3 | 2017-03-22 11:35:53 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Brian Silverman | 30a6b80 | 2014-10-25 20:20:37 -0400 | [diff] [blame] | 851 | /* |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 852 | * Drops a reference to the pi_state object and frees or caches it |
| 853 | * when the last reference is gone. |
Brian Silverman | 30a6b80 | 2014-10-25 20:20:37 -0400 | [diff] [blame] | 854 | */ |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 855 | static void put_pi_state(struct futex_pi_state *pi_state) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 856 | { |
Brian Silverman | 30a6b80 | 2014-10-25 20:20:37 -0400 | [diff] [blame] | 857 | if (!pi_state) |
| 858 | return; |
| 859 | |
Elena Reshetova | 49262de | 2019-02-05 14:24:27 +0200 | [diff] [blame] | 860 | if (!refcount_dec_and_test(&pi_state->refcount)) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 861 | return; |
| 862 | |
| 863 | /* |
| 864 | * If pi_state->owner is NULL, the owner is most probably dying |
| 865 | * and has cleaned up the pi_state already |
| 866 | */ |
| 867 | if (pi_state->owner) { |
Dan Carpenter | 1e106aa | 2020-11-06 11:52:05 +0300 | [diff] [blame] | 868 | unsigned long flags; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 869 | |
Dan Carpenter | 1e106aa | 2020-11-06 11:52:05 +0300 | [diff] [blame] | 870 | raw_spin_lock_irqsave(&pi_state->pi_mutex.wait_lock, flags); |
Thomas Gleixner | 6ccc84f | 2021-01-20 11:35:19 +0100 | [diff] [blame] | 871 | pi_state_update_owner(pi_state, NULL); |
Thomas Gleixner | 2156ac1 | 2021-01-20 11:32:07 +0100 | [diff] [blame] | 872 | rt_mutex_proxy_unlock(&pi_state->pi_mutex); |
Dan Carpenter | 1e106aa | 2020-11-06 11:52:05 +0300 | [diff] [blame] | 873 | raw_spin_unlock_irqrestore(&pi_state->pi_mutex.wait_lock, flags); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Peter Zijlstra | c74aef2 | 2017-09-22 17:48:06 +0200 | [diff] [blame] | 876 | if (current->pi_state_cache) { |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 877 | kfree(pi_state); |
Peter Zijlstra | c74aef2 | 2017-09-22 17:48:06 +0200 | [diff] [blame] | 878 | } else { |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 879 | /* |
| 880 | * pi_state->list is already empty. |
| 881 | * clear pi_state->owner. |
| 882 | * refcount is at 0 - put it back to 1. |
| 883 | */ |
| 884 | pi_state->owner = NULL; |
Elena Reshetova | 49262de | 2019-02-05 14:24:27 +0200 | [diff] [blame] | 885 | refcount_set(&pi_state->refcount, 1); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 886 | current->pi_state_cache = pi_state; |
| 887 | } |
| 888 | } |
| 889 | |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 890 | #ifdef CONFIG_FUTEX_PI |
| 891 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 892 | /* |
| 893 | * This task is holding PI mutexes at exit time => bad. |
| 894 | * Kernel cleans up PI-state, but userspace is likely hosed. |
| 895 | * (Robust-futex cleanup is separate and might save the day for userspace.) |
| 896 | */ |
Thomas Gleixner | ba31c1a4 | 2019-11-06 22:55:36 +0100 | [diff] [blame] | 897 | static void exit_pi_state_list(struct task_struct *curr) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 898 | { |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 899 | struct list_head *next, *head = &curr->pi_state_list; |
| 900 | struct futex_pi_state *pi_state; |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 901 | struct futex_hash_bucket *hb; |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 902 | union futex_key key = FUTEX_KEY_INIT; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 903 | |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 904 | if (!futex_cmpxchg_enabled) |
| 905 | return; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 906 | /* |
| 907 | * We are a ZOMBIE and nobody can enqueue itself on |
| 908 | * pi_state_list anymore, but we have to be careful |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 909 | * versus waiters unqueueing themselves: |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 910 | */ |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 911 | raw_spin_lock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 912 | while (!list_empty(head)) { |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 913 | next = head->next; |
| 914 | pi_state = list_entry(next, struct futex_pi_state, list); |
| 915 | key = pi_state->key; |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 916 | hb = hash_futex(&key); |
Peter Zijlstra | 153fbd1 | 2017-10-31 11:18:53 +0100 | [diff] [blame] | 917 | |
| 918 | /* |
| 919 | * We can race against put_pi_state() removing itself from the |
| 920 | * list (a waiter going away). put_pi_state() will first |
| 921 | * decrement the reference count and then modify the list, so |
| 922 | * its possible to see the list entry but fail this reference |
| 923 | * acquire. |
| 924 | * |
| 925 | * In that case; drop the locks to let put_pi_state() make |
| 926 | * progress and retry the loop. |
| 927 | */ |
Elena Reshetova | 49262de | 2019-02-05 14:24:27 +0200 | [diff] [blame] | 928 | if (!refcount_inc_not_zero(&pi_state->refcount)) { |
Peter Zijlstra | 153fbd1 | 2017-10-31 11:18:53 +0100 | [diff] [blame] | 929 | raw_spin_unlock_irq(&curr->pi_lock); |
| 930 | cpu_relax(); |
| 931 | raw_spin_lock_irq(&curr->pi_lock); |
| 932 | continue; |
| 933 | } |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 934 | raw_spin_unlock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 935 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 936 | spin_lock(&hb->lock); |
Peter Zijlstra | c74aef2 | 2017-09-22 17:48:06 +0200 | [diff] [blame] | 937 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 938 | raw_spin_lock(&curr->pi_lock); |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 939 | /* |
| 940 | * We dropped the pi-lock, so re-check whether this |
| 941 | * task still owns the PI-state: |
| 942 | */ |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 943 | if (head->next != next) { |
Peter Zijlstra | 153fbd1 | 2017-10-31 11:18:53 +0100 | [diff] [blame] | 944 | /* retain curr->pi_lock for the loop invariant */ |
Peter Zijlstra | c74aef2 | 2017-09-22 17:48:06 +0200 | [diff] [blame] | 945 | raw_spin_unlock(&pi_state->pi_mutex.wait_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 946 | spin_unlock(&hb->lock); |
Peter Zijlstra | 153fbd1 | 2017-10-31 11:18:53 +0100 | [diff] [blame] | 947 | put_pi_state(pi_state); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 948 | continue; |
| 949 | } |
| 950 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 951 | WARN_ON(pi_state->owner != curr); |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 952 | WARN_ON(list_empty(&pi_state->list)); |
| 953 | list_del_init(&pi_state->list); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 954 | pi_state->owner = NULL; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 955 | |
Peter Zijlstra | 153fbd1 | 2017-10-31 11:18:53 +0100 | [diff] [blame] | 956 | raw_spin_unlock(&curr->pi_lock); |
Peter Zijlstra | c74aef2 | 2017-09-22 17:48:06 +0200 | [diff] [blame] | 957 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 958 | spin_unlock(&hb->lock); |
| 959 | |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 960 | rt_mutex_futex_unlock(&pi_state->pi_mutex); |
| 961 | put_pi_state(pi_state); |
| 962 | |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 963 | raw_spin_lock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 964 | } |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 965 | raw_spin_unlock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 966 | } |
Thomas Gleixner | ba31c1a4 | 2019-11-06 22:55:36 +0100 | [diff] [blame] | 967 | #else |
| 968 | static inline void exit_pi_state_list(struct task_struct *curr) { } |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 969 | #endif |
| 970 | |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 971 | /* |
| 972 | * We need to check the following states: |
| 973 | * |
| 974 | * Waiter | pi_state | pi->owner | uTID | uODIED | ? |
| 975 | * |
| 976 | * [1] NULL | --- | --- | 0 | 0/1 | Valid |
| 977 | * [2] NULL | --- | --- | >0 | 0/1 | Valid |
| 978 | * |
| 979 | * [3] Found | NULL | -- | Any | 0/1 | Invalid |
| 980 | * |
| 981 | * [4] Found | Found | NULL | 0 | 1 | Valid |
| 982 | * [5] Found | Found | NULL | >0 | 1 | Invalid |
| 983 | * |
| 984 | * [6] Found | Found | task | 0 | 1 | Valid |
| 985 | * |
| 986 | * [7] Found | Found | NULL | Any | 0 | Invalid |
| 987 | * |
| 988 | * [8] Found | Found | task | ==taskTID | 0/1 | Valid |
| 989 | * [9] Found | Found | task | 0 | 0 | Invalid |
| 990 | * [10] Found | Found | task | !=taskTID | 0/1 | Invalid |
| 991 | * |
| 992 | * [1] Indicates that the kernel can acquire the futex atomically. We |
Randy Dunlap | 7b7b8a2 | 2020-10-15 20:10:28 -0700 | [diff] [blame] | 993 | * came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit. |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 994 | * |
| 995 | * [2] Valid, if TID does not belong to a kernel thread. If no matching |
| 996 | * thread is found then it indicates that the owner TID has died. |
| 997 | * |
| 998 | * [3] Invalid. The waiter is queued on a non PI futex |
| 999 | * |
| 1000 | * [4] Valid state after exit_robust_list(), which sets the user space |
| 1001 | * value to FUTEX_WAITERS | FUTEX_OWNER_DIED. |
| 1002 | * |
| 1003 | * [5] The user space value got manipulated between exit_robust_list() |
| 1004 | * and exit_pi_state_list() |
| 1005 | * |
| 1006 | * [6] Valid state after exit_pi_state_list() which sets the new owner in |
| 1007 | * the pi_state but cannot access the user space value. |
| 1008 | * |
| 1009 | * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set. |
| 1010 | * |
| 1011 | * [8] Owner and user space value match |
| 1012 | * |
| 1013 | * [9] There is no transient state which sets the user space TID to 0 |
| 1014 | * except exit_robust_list(), but this is indicated by the |
| 1015 | * FUTEX_OWNER_DIED bit. See [4] |
| 1016 | * |
| 1017 | * [10] There is no transient state which leaves owner and user space |
Thomas Gleixner | 34b1a1c | 2021-01-18 19:01:21 +0100 | [diff] [blame] | 1018 | * TID out of sync. Except one error case where the kernel is denied |
| 1019 | * write access to the user address, see fixup_pi_state_owner(). |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1020 | * |
| 1021 | * |
| 1022 | * Serialization and lifetime rules: |
| 1023 | * |
| 1024 | * hb->lock: |
| 1025 | * |
| 1026 | * hb -> futex_q, relation |
| 1027 | * futex_q -> pi_state, relation |
| 1028 | * |
| 1029 | * (cannot be raw because hb can contain arbitrary amount |
| 1030 | * of futex_q's) |
| 1031 | * |
| 1032 | * pi_mutex->wait_lock: |
| 1033 | * |
| 1034 | * {uval, pi_state} |
| 1035 | * |
| 1036 | * (and pi_mutex 'obviously') |
| 1037 | * |
| 1038 | * p->pi_lock: |
| 1039 | * |
| 1040 | * p->pi_state_list -> pi_state->list, relation |
Davidlohr Bueso | c2e4bfe | 2021-02-26 09:50:29 -0800 | [diff] [blame] | 1041 | * pi_mutex->owner -> pi_state->owner, relation |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1042 | * |
| 1043 | * pi_state->refcount: |
| 1044 | * |
| 1045 | * pi_state lifetime |
| 1046 | * |
| 1047 | * |
| 1048 | * Lock order: |
| 1049 | * |
| 1050 | * hb->lock |
| 1051 | * pi_mutex->wait_lock |
| 1052 | * p->pi_lock |
| 1053 | * |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 1054 | */ |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1055 | |
| 1056 | /* |
| 1057 | * Validate that the existing waiter has a pi_state and sanity check |
| 1058 | * the pi_state against the user space value. If correct, attach to |
| 1059 | * it. |
| 1060 | */ |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1061 | static int attach_to_pi_state(u32 __user *uaddr, u32 uval, |
| 1062 | struct futex_pi_state *pi_state, |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1063 | struct futex_pi_state **ps) |
| 1064 | { |
| 1065 | pid_t pid = uval & FUTEX_TID_MASK; |
Peter Zijlstra | 94ffac5 | 2017-04-07 09:04:07 +0200 | [diff] [blame] | 1066 | u32 uval2; |
| 1067 | int ret; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1068 | |
| 1069 | /* |
| 1070 | * Userspace might have messed up non-PI and PI futexes [3] |
| 1071 | */ |
| 1072 | if (unlikely(!pi_state)) |
| 1073 | return -EINVAL; |
| 1074 | |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1075 | /* |
| 1076 | * We get here with hb->lock held, and having found a |
| 1077 | * futex_top_waiter(). This means that futex_lock_pi() of said futex_q |
| 1078 | * has dropped the hb->lock in between queue_me() and unqueue_me_pi(), |
| 1079 | * which in turn means that futex_lock_pi() still has a reference on |
| 1080 | * our pi_state. |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1081 | * |
| 1082 | * The waiter holding a reference on @pi_state also protects against |
| 1083 | * the unlocked put_pi_state() in futex_unlock_pi(), futex_lock_pi() |
| 1084 | * and futex_wait_requeue_pi() as it cannot go to 0 and consequently |
| 1085 | * free pi_state before we can take a reference ourselves. |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1086 | */ |
Elena Reshetova | 49262de | 2019-02-05 14:24:27 +0200 | [diff] [blame] | 1087 | WARN_ON(!refcount_read(&pi_state->refcount)); |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1088 | |
| 1089 | /* |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1090 | * Now that we have a pi_state, we can acquire wait_lock |
| 1091 | * and do the state validation. |
| 1092 | */ |
| 1093 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 1094 | |
| 1095 | /* |
| 1096 | * Since {uval, pi_state} is serialized by wait_lock, and our current |
| 1097 | * uval was read without holding it, it can have changed. Verify it |
| 1098 | * still is what we expect it to be, otherwise retry the entire |
| 1099 | * operation. |
| 1100 | */ |
| 1101 | if (get_futex_value_locked(&uval2, uaddr)) |
| 1102 | goto out_efault; |
| 1103 | |
| 1104 | if (uval != uval2) |
| 1105 | goto out_eagain; |
| 1106 | |
| 1107 | /* |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1108 | * Handle the owner died case: |
| 1109 | */ |
| 1110 | if (uval & FUTEX_OWNER_DIED) { |
| 1111 | /* |
| 1112 | * exit_pi_state_list sets owner to NULL and wakes the |
| 1113 | * topmost waiter. The task which acquires the |
| 1114 | * pi_state->rt_mutex will fixup owner. |
| 1115 | */ |
| 1116 | if (!pi_state->owner) { |
| 1117 | /* |
| 1118 | * No pi state owner, but the user space TID |
| 1119 | * is not 0. Inconsistent state. [5] |
| 1120 | */ |
| 1121 | if (pid) |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1122 | goto out_einval; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1123 | /* |
| 1124 | * Take a ref on the state and return success. [4] |
| 1125 | */ |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1126 | goto out_attach; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * If TID is 0, then either the dying owner has not |
| 1131 | * yet executed exit_pi_state_list() or some waiter |
| 1132 | * acquired the rtmutex in the pi state, but did not |
| 1133 | * yet fixup the TID in user space. |
| 1134 | * |
| 1135 | * Take a ref on the state and return success. [6] |
| 1136 | */ |
| 1137 | if (!pid) |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1138 | goto out_attach; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1139 | } else { |
| 1140 | /* |
| 1141 | * If the owner died bit is not set, then the pi_state |
| 1142 | * must have an owner. [7] |
| 1143 | */ |
| 1144 | if (!pi_state->owner) |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1145 | goto out_einval; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | * Bail out if user space manipulated the futex value. If pi |
| 1150 | * state exists then the owner TID must be the same as the |
| 1151 | * user space TID. [9/10] |
| 1152 | */ |
| 1153 | if (pid != task_pid_vnr(pi_state->owner)) |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1154 | goto out_einval; |
| 1155 | |
| 1156 | out_attach: |
Peter Zijlstra | bf92cf3 | 2017-03-22 11:35:53 +0100 | [diff] [blame] | 1157 | get_pi_state(pi_state); |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1158 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1159 | *ps = pi_state; |
| 1160 | return 0; |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1161 | |
| 1162 | out_einval: |
| 1163 | ret = -EINVAL; |
| 1164 | goto out_error; |
| 1165 | |
| 1166 | out_eagain: |
| 1167 | ret = -EAGAIN; |
| 1168 | goto out_error; |
| 1169 | |
| 1170 | out_efault: |
| 1171 | ret = -EFAULT; |
| 1172 | goto out_error; |
| 1173 | |
| 1174 | out_error: |
| 1175 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 1176 | return ret; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1179 | /** |
| 1180 | * wait_for_owner_exiting - Block until the owner has exited |
Randy Dunlap | 51bfb1d | 2019-12-08 20:26:55 -0800 | [diff] [blame] | 1181 | * @ret: owner's current futex lock status |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1182 | * @exiting: Pointer to the exiting task |
| 1183 | * |
| 1184 | * Caller must hold a refcount on @exiting. |
| 1185 | */ |
| 1186 | static void wait_for_owner_exiting(int ret, struct task_struct *exiting) |
| 1187 | { |
| 1188 | if (ret != -EBUSY) { |
| 1189 | WARN_ON_ONCE(exiting); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | if (WARN_ON_ONCE(ret == -EBUSY && !exiting)) |
| 1194 | return; |
| 1195 | |
| 1196 | mutex_lock(&exiting->futex_exit_mutex); |
| 1197 | /* |
| 1198 | * No point in doing state checking here. If the waiter got here |
| 1199 | * while the task was in exec()->exec_futex_release() then it can |
| 1200 | * have any FUTEX_STATE_* value when the waiter has acquired the |
| 1201 | * mutex. OK, if running, EXITING or DEAD if it reached exit() |
| 1202 | * already. Highly unlikely and not a problem. Just one more round |
| 1203 | * through the futex maze. |
| 1204 | */ |
| 1205 | mutex_unlock(&exiting->futex_exit_mutex); |
| 1206 | |
| 1207 | put_task_struct(exiting); |
| 1208 | } |
| 1209 | |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1210 | static int handle_exit_race(u32 __user *uaddr, u32 uval, |
| 1211 | struct task_struct *tsk) |
| 1212 | { |
| 1213 | u32 uval2; |
| 1214 | |
| 1215 | /* |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 1216 | * If the futex exit state is not yet FUTEX_STATE_DEAD, tell the |
| 1217 | * caller that the alleged owner is busy. |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1218 | */ |
Thomas Gleixner | 3d4775d | 2019-11-06 22:55:37 +0100 | [diff] [blame] | 1219 | if (tsk && tsk->futex_state != FUTEX_STATE_DEAD) |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 1220 | return -EBUSY; |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1221 | |
| 1222 | /* |
| 1223 | * Reread the user space value to handle the following situation: |
| 1224 | * |
| 1225 | * CPU0 CPU1 |
| 1226 | * |
| 1227 | * sys_exit() sys_futex() |
| 1228 | * do_exit() futex_lock_pi() |
| 1229 | * futex_lock_pi_atomic() |
| 1230 | * exit_signals(tsk) No waiters: |
| 1231 | * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID |
| 1232 | * mm_release(tsk) Set waiter bit |
| 1233 | * exit_robust_list(tsk) { *uaddr = 0x80000PID; |
| 1234 | * Set owner died attach_to_pi_owner() { |
| 1235 | * *uaddr = 0xC0000000; tsk = get_task(PID); |
| 1236 | * } if (!tsk->flags & PF_EXITING) { |
| 1237 | * ... attach(); |
Thomas Gleixner | 3d4775d | 2019-11-06 22:55:37 +0100 | [diff] [blame] | 1238 | * tsk->futex_state = } else { |
| 1239 | * FUTEX_STATE_DEAD; if (tsk->futex_state != |
| 1240 | * FUTEX_STATE_DEAD) |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1241 | * return -EAGAIN; |
| 1242 | * return -ESRCH; <--- FAIL |
| 1243 | * } |
| 1244 | * |
| 1245 | * Returning ESRCH unconditionally is wrong here because the |
| 1246 | * user space value has been changed by the exiting task. |
| 1247 | * |
| 1248 | * The same logic applies to the case where the exiting task is |
| 1249 | * already gone. |
| 1250 | */ |
| 1251 | if (get_futex_value_locked(&uval2, uaddr)) |
| 1252 | return -EFAULT; |
| 1253 | |
| 1254 | /* If the user space value has changed, try again. */ |
| 1255 | if (uval2 != uval) |
| 1256 | return -EAGAIN; |
| 1257 | |
| 1258 | /* |
| 1259 | * The exiting task did not have a robust list, the robust list was |
| 1260 | * corrupted or the user space value in *uaddr is simply bogus. |
| 1261 | * Give up and tell user space. |
| 1262 | */ |
| 1263 | return -ESRCH; |
| 1264 | } |
| 1265 | |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1266 | /* |
| 1267 | * Lookup the task for the TID provided from user space and attach to |
| 1268 | * it after doing proper sanity checks. |
| 1269 | */ |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1270 | static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key, |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1271 | struct futex_pi_state **ps, |
| 1272 | struct task_struct **exiting) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1273 | { |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1274 | pid_t pid = uval & FUTEX_TID_MASK; |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1275 | struct futex_pi_state *pi_state; |
| 1276 | struct task_struct *p; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1277 | |
| 1278 | /* |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 1279 | * We are the first waiter - try to look up the real owner and attach |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 1280 | * the new pi_state to it, but bail out when TID = 0 [1] |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1281 | * |
| 1282 | * The !pid check is paranoid. None of the call sites should end up |
| 1283 | * with pid == 0, but better safe than sorry. Let the caller retry |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1284 | */ |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1285 | if (!pid) |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1286 | return -EAGAIN; |
Mike Rapoport | 2ee0826 | 2018-02-06 15:40:17 -0800 | [diff] [blame] | 1287 | p = find_get_task_by_vpid(pid); |
Michal Hocko | 7a0ea09 | 2010-06-30 09:51:19 +0200 | [diff] [blame] | 1288 | if (!p) |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1289 | return handle_exit_race(uaddr, uval, NULL); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1290 | |
Oleg Nesterov | a212946 | 2015-02-02 15:05:36 +0100 | [diff] [blame] | 1291 | if (unlikely(p->flags & PF_KTHREAD)) { |
Thomas Gleixner | f0d71b3 | 2014-05-12 20:45:35 +0000 | [diff] [blame] | 1292 | put_task_struct(p); |
| 1293 | return -EPERM; |
| 1294 | } |
| 1295 | |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1296 | /* |
Thomas Gleixner | 3d4775d | 2019-11-06 22:55:37 +0100 | [diff] [blame] | 1297 | * We need to look at the task state to figure out, whether the |
| 1298 | * task is exiting. To protect against the change of the task state |
| 1299 | * in futex_exit_release(), we do this protected by p->pi_lock: |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1300 | */ |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 1301 | raw_spin_lock_irq(&p->pi_lock); |
Thomas Gleixner | 3d4775d | 2019-11-06 22:55:37 +0100 | [diff] [blame] | 1302 | if (unlikely(p->futex_state != FUTEX_STATE_OK)) { |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1303 | /* |
Thomas Gleixner | 3d4775d | 2019-11-06 22:55:37 +0100 | [diff] [blame] | 1304 | * The task is on the way out. When the futex state is |
| 1305 | * FUTEX_STATE_DEAD, we know that the task has finished |
| 1306 | * the cleanup: |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1307 | */ |
Thomas Gleixner | da791a6 | 2018-12-10 14:35:14 +0100 | [diff] [blame] | 1308 | int ret = handle_exit_race(uaddr, uval, p); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1309 | |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 1310 | raw_spin_unlock_irq(&p->pi_lock); |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1311 | /* |
| 1312 | * If the owner task is between FUTEX_STATE_EXITING and |
| 1313 | * FUTEX_STATE_DEAD then store the task pointer and keep |
| 1314 | * the reference on the task struct. The calling code will |
| 1315 | * drop all locks, wait for the task to reach |
| 1316 | * FUTEX_STATE_DEAD and then drop the refcount. This is |
| 1317 | * required to prevent a live lock when the current task |
| 1318 | * preempted the exiting task between the two states. |
| 1319 | */ |
| 1320 | if (ret == -EBUSY) |
| 1321 | *exiting = p; |
| 1322 | else |
| 1323 | put_task_struct(p); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1324 | return ret; |
| 1325 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1326 | |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 1327 | /* |
| 1328 | * No existing pi state. First waiter. [2] |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1329 | * |
| 1330 | * This creates pi_state, we have hb->lock held, this means nothing can |
| 1331 | * observe this state, wait_lock is irrelevant. |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 1332 | */ |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1333 | pi_state = alloc_pi_state(); |
| 1334 | |
| 1335 | /* |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1336 | * Initialize the pi_mutex in locked state and make @p |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1337 | * the owner of it: |
| 1338 | */ |
| 1339 | rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p); |
| 1340 | |
| 1341 | /* Store the key for possible exit cleanups: */ |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 1342 | pi_state->key = *key; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1343 | |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 1344 | WARN_ON(!list_empty(&pi_state->list)); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1345 | list_add(&pi_state->list, &p->pi_state_list); |
Peter Zijlstra | c74aef2 | 2017-09-22 17:48:06 +0200 | [diff] [blame] | 1346 | /* |
| 1347 | * Assignment without holding pi_state->pi_mutex.wait_lock is safe |
| 1348 | * because there is no concurrency as the object is not published yet. |
| 1349 | */ |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1350 | pi_state->owner = p; |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 1351 | raw_spin_unlock_irq(&p->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1352 | |
| 1353 | put_task_struct(p); |
| 1354 | |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 1355 | *ps = pi_state; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1356 | |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1360 | static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval) |
| 1361 | { |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 1362 | int err; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 1363 | u32 curval; |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1364 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1365 | if (unlikely(should_fail_futex(true))) |
| 1366 | return -EFAULT; |
| 1367 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 1368 | err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval); |
| 1369 | if (unlikely(err)) |
| 1370 | return err; |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1371 | |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1372 | /* If user space value changed, let the caller retry */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1373 | return curval != uval ? -EAGAIN : 0; |
| 1374 | } |
| 1375 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1376 | /** |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 1377 | * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1378 | * @uaddr: the pi futex user address |
| 1379 | * @hb: the pi futex hash bucket |
| 1380 | * @key: the futex key associated with uaddr and hb |
| 1381 | * @ps: the pi_state pointer where we store the result of the |
| 1382 | * lookup |
| 1383 | * @task: the task to perform the atomic lock work for. This will |
| 1384 | * be "current" except in the case of requeue pi. |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1385 | * @exiting: Pointer to store the task pointer of the owner task |
| 1386 | * which is in the middle of exiting |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1387 | * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1388 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 1389 | * Return: |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 1390 | * - 0 - ready to wait; |
| 1391 | * - 1 - acquired the lock; |
| 1392 | * - <0 - error |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1393 | * |
Thomas Gleixner | c363b7e | 2021-08-15 23:29:06 +0200 | [diff] [blame] | 1394 | * The hb->lock must be held by the caller. |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1395 | * |
| 1396 | * @exiting is only set when the return value is -EBUSY. If so, this holds |
| 1397 | * a refcount on the exiting task on return and the caller needs to drop it |
| 1398 | * after waiting for the exit to complete. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1399 | */ |
| 1400 | static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb, |
| 1401 | union futex_key *key, |
| 1402 | struct futex_pi_state **ps, |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1403 | struct task_struct *task, |
| 1404 | struct task_struct **exiting, |
| 1405 | int set_waiters) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1406 | { |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1407 | u32 uval, newval, vpid = task_pid_vnr(task); |
Peter Zijlstra | 499f5ac | 2017-03-22 11:35:48 +0100 | [diff] [blame] | 1408 | struct futex_q *top_waiter; |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1409 | int ret; |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1410 | |
| 1411 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1412 | * Read the user space value first so we can validate a few |
| 1413 | * things before proceeding further. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1414 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1415 | if (get_futex_value_locked(&uval, uaddr)) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1416 | return -EFAULT; |
| 1417 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1418 | if (unlikely(should_fail_futex(true))) |
| 1419 | return -EFAULT; |
| 1420 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1421 | /* |
| 1422 | * Detect deadlocks. |
| 1423 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1424 | if ((unlikely((uval & FUTEX_TID_MASK) == vpid))) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1425 | return -EDEADLK; |
| 1426 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1427 | if ((unlikely(should_fail_futex(true)))) |
| 1428 | return -EDEADLK; |
| 1429 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1430 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1431 | * Lookup existing state first. If it exists, try to attach to |
| 1432 | * its pi_state. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1433 | */ |
Peter Zijlstra | 499f5ac | 2017-03-22 11:35:48 +0100 | [diff] [blame] | 1434 | top_waiter = futex_top_waiter(hb, key); |
| 1435 | if (top_waiter) |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1436 | return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps); |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1437 | |
| 1438 | /* |
| 1439 | * No waiter and user TID is 0. We are here because the |
| 1440 | * waiters or the owner died bit is set or called from |
| 1441 | * requeue_cmp_pi or for whatever reason something took the |
| 1442 | * syscall. |
| 1443 | */ |
| 1444 | if (!(uval & FUTEX_TID_MASK)) { |
Thomas Gleixner | b3eaa9f | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 1445 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1446 | * We take over the futex. No other waiters and the user space |
| 1447 | * TID is 0. We preserve the owner died bit. |
Thomas Gleixner | b3eaa9f | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 1448 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1449 | newval = uval & FUTEX_OWNER_DIED; |
| 1450 | newval |= vpid; |
| 1451 | |
| 1452 | /* The futex requeue_pi code can enforce the waiters bit */ |
| 1453 | if (set_waiters) |
| 1454 | newval |= FUTEX_WAITERS; |
| 1455 | |
| 1456 | ret = lock_pi_update_atomic(uaddr, uval, newval); |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 1457 | if (ret) |
| 1458 | return ret; |
| 1459 | |
| 1460 | /* |
| 1461 | * If the waiter bit was requested the caller also needs PI |
| 1462 | * state attached to the new owner of the user space futex. |
| 1463 | * |
| 1464 | * @task is guaranteed to be alive and it cannot be exiting |
| 1465 | * because it is either sleeping or waiting in |
| 1466 | * futex_requeue_pi_wakeup_sync(). |
| 1467 | */ |
| 1468 | if (set_waiters) { |
| 1469 | ret = attach_to_pi_owner(uaddr, newval, key, ps, |
| 1470 | exiting); |
| 1471 | WARN_ON(ret); |
| 1472 | } |
| 1473 | return 1; |
Thomas Gleixner | b3eaa9f | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 1474 | } |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1475 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1476 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1477 | * First waiter. Set the waiters bit before attaching ourself to |
| 1478 | * the owner. If owner tries to unlock, it will be forced into |
| 1479 | * the kernel and blocked on hb->lock. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1480 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1481 | newval = uval | FUTEX_WAITERS; |
| 1482 | ret = lock_pi_update_atomic(uaddr, uval, newval); |
| 1483 | if (ret) |
| 1484 | return ret; |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1485 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1486 | * If the update of the user space value succeeded, we try to |
| 1487 | * attach to the owner. If that fails, no harm done, we only |
| 1488 | * set the FUTEX_WAITERS bit in the user space variable. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1489 | */ |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1490 | return attach_to_pi_owner(uaddr, newval, key, ps, exiting); |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1493 | /** |
| 1494 | * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket |
| 1495 | * @q: The futex_q to unqueue |
| 1496 | * |
| 1497 | * The q->lock_ptr must not be NULL and must be held by the caller. |
| 1498 | */ |
| 1499 | static void __unqueue_futex(struct futex_q *q) |
| 1500 | { |
| 1501 | struct futex_hash_bucket *hb; |
| 1502 | |
Lance Roy | 4de1a29 | 2018-10-02 22:38:57 -0700 | [diff] [blame] | 1503 | if (WARN_ON_SMP(!q->lock_ptr) || WARN_ON(plist_node_empty(&q->list))) |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1504 | return; |
Lance Roy | 4de1a29 | 2018-10-02 22:38:57 -0700 | [diff] [blame] | 1505 | lockdep_assert_held(q->lock_ptr); |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1506 | |
| 1507 | hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock); |
| 1508 | plist_del(&q->list, &hb->chain); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 1509 | hb_waiters_dec(hb); |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1510 | } |
| 1511 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1512 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | * The hash bucket lock must be held when this is called. |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1514 | * Afterwards, the futex_q must not be accessed. Callers |
| 1515 | * must ensure to later call wake_up_q() for the actual |
| 1516 | * wakeups to occur. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | */ |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1518 | static void mark_wake_futex(struct wake_q_head *wake_q, struct futex_q *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | { |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 1520 | struct task_struct *p = q->task; |
| 1521 | |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1522 | if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n")) |
| 1523 | return; |
| 1524 | |
Peter Zijlstra | b061c38 | 2018-11-29 14:44:49 +0100 | [diff] [blame] | 1525 | get_task_struct(p); |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1526 | __unqueue_futex(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | /* |
Darren Hart (VMware) | 38fcd06 | 2017-04-14 15:31:38 -0700 | [diff] [blame] | 1528 | * The waiting task can free the futex_q as soon as q->lock_ptr = NULL |
| 1529 | * is written, without taking any locks. This is possible in the event |
| 1530 | * of a spurious wakeup, for example. A memory barrier is required here |
| 1531 | * to prevent the following store to lock_ptr from getting ahead of the |
| 1532 | * plist_del in __unqueue_futex(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | */ |
Peter Zijlstra | 1b367ec | 2017-03-22 11:35:49 +0100 | [diff] [blame] | 1534 | smp_store_release(&q->lock_ptr, NULL); |
Peter Zijlstra | b061c38 | 2018-11-29 14:44:49 +0100 | [diff] [blame] | 1535 | |
| 1536 | /* |
| 1537 | * Queue the task for later wakeup for after we've released |
Davidlohr Bueso | 7514590 | 2019-10-22 20:34:50 -0700 | [diff] [blame] | 1538 | * the hb->lock. |
Peter Zijlstra | b061c38 | 2018-11-29 14:44:49 +0100 | [diff] [blame] | 1539 | */ |
Davidlohr Bueso | 07879c6 | 2018-12-18 11:53:52 -0800 | [diff] [blame] | 1540 | wake_q_add_safe(wake_q, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1543 | /* |
| 1544 | * Caller must hold a reference on @pi_state. |
| 1545 | */ |
| 1546 | static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_pi_state *pi_state) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1547 | { |
Davidlohr Bueso | 9a4b99f | 2021-02-26 09:50:26 -0800 | [diff] [blame] | 1548 | struct rt_mutex_waiter *top_waiter; |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1549 | struct task_struct *new_owner; |
Peter Zijlstra | aa2bfe5 | 2017-03-23 15:56:10 +0100 | [diff] [blame] | 1550 | bool postunlock = false; |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame] | 1551 | DEFINE_RT_WAKE_Q(wqh); |
| 1552 | u32 curval, newval; |
Thomas Gleixner | 13fbca4 | 2014-06-03 12:27:07 +0000 | [diff] [blame] | 1553 | int ret = 0; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1554 | |
Davidlohr Bueso | 9a4b99f | 2021-02-26 09:50:26 -0800 | [diff] [blame] | 1555 | top_waiter = rt_mutex_top_waiter(&pi_state->pi_mutex); |
| 1556 | if (WARN_ON_ONCE(!top_waiter)) { |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1557 | /* |
Peter Zijlstra | bebe5b5 | 2017-03-22 11:35:59 +0100 | [diff] [blame] | 1558 | * As per the comment in futex_unlock_pi() this should not happen. |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1559 | * |
| 1560 | * When this happens, give up our locks and try again, giving |
| 1561 | * the futex_lock_pi() instance time to complete, either by |
| 1562 | * waiting on the rtmutex or removing itself from the futex |
| 1563 | * queue. |
| 1564 | */ |
| 1565 | ret = -EAGAIN; |
| 1566 | goto out_unlock; |
Peter Zijlstra | 73d786b | 2017-03-22 11:35:54 +0100 | [diff] [blame] | 1567 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1568 | |
Davidlohr Bueso | 9a4b99f | 2021-02-26 09:50:26 -0800 | [diff] [blame] | 1569 | new_owner = top_waiter->task; |
| 1570 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1571 | /* |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1572 | * We pass it to the next owner. The WAITERS bit is always kept |
| 1573 | * enabled while there is PI state around. We cleanup the owner |
| 1574 | * died bit, because we are the owner. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1575 | */ |
Thomas Gleixner | 13fbca4 | 2014-06-03 12:27:07 +0000 | [diff] [blame] | 1576 | newval = FUTEX_WAITERS | task_pid_vnr(new_owner); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1577 | |
Mateusz Nosek | 921c7eb | 2020-09-27 02:08:58 +0200 | [diff] [blame] | 1578 | if (unlikely(should_fail_futex(true))) { |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1579 | ret = -EFAULT; |
Mateusz Nosek | 921c7eb | 2020-09-27 02:08:58 +0200 | [diff] [blame] | 1580 | goto out_unlock; |
| 1581 | } |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1582 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 1583 | ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval); |
| 1584 | if (!ret && (curval != uval)) { |
Sebastian Andrzej Siewior | 89e9e66 | 2016-04-15 14:35:39 +0200 | [diff] [blame] | 1585 | /* |
| 1586 | * If a unconditional UNLOCK_PI operation (user space did not |
| 1587 | * try the TID->0 transition) raced with a waiter setting the |
| 1588 | * FUTEX_WAITERS flag between get_user() and locking the hash |
| 1589 | * bucket lock, retry the operation. |
| 1590 | */ |
| 1591 | if ((FUTEX_TID_MASK & curval) == uval) |
| 1592 | ret = -EAGAIN; |
| 1593 | else |
| 1594 | ret = -EINVAL; |
| 1595 | } |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 1596 | |
Thomas Gleixner | c5cade2 | 2021-01-19 15:21:35 +0100 | [diff] [blame] | 1597 | if (!ret) { |
| 1598 | /* |
| 1599 | * This is a point of no return; once we modified the uval |
| 1600 | * there is no going back and subsequent operations must |
| 1601 | * not fail. |
| 1602 | */ |
| 1603 | pi_state_update_owner(pi_state, new_owner); |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame] | 1604 | postunlock = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wqh); |
Thomas Gleixner | c5cade2 | 2021-01-19 15:21:35 +0100 | [diff] [blame] | 1605 | } |
Peter Zijlstra | 5293c2e | 2017-03-22 11:35:51 +0100 | [diff] [blame] | 1606 | |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1607 | out_unlock: |
Peter Zijlstra | 5293c2e | 2017-03-22 11:35:51 +0100 | [diff] [blame] | 1608 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
Peter Zijlstra | 5293c2e | 2017-03-22 11:35:51 +0100 | [diff] [blame] | 1609 | |
Peter Zijlstra | aa2bfe5 | 2017-03-23 15:56:10 +0100 | [diff] [blame] | 1610 | if (postunlock) |
Thomas Gleixner | 7980aa3 | 2021-08-15 23:28:09 +0200 | [diff] [blame] | 1611 | rt_mutex_postunlock(&wqh); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1612 | |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 1613 | return ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1614 | } |
| 1615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | /* |
Ingo Molnar | 8b8f319 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1617 | * Express the locking dependencies for lockdep: |
| 1618 | */ |
| 1619 | static inline void |
| 1620 | double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2) |
| 1621 | { |
| 1622 | if (hb1 <= hb2) { |
| 1623 | spin_lock(&hb1->lock); |
| 1624 | if (hb1 < hb2) |
| 1625 | spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING); |
| 1626 | } else { /* hb1 > hb2 */ |
| 1627 | spin_lock(&hb2->lock); |
| 1628 | spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING); |
| 1629 | } |
| 1630 | } |
| 1631 | |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1632 | static inline void |
| 1633 | double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2) |
| 1634 | { |
Darren Hart | f061d35 | 2009-03-12 15:11:18 -0700 | [diff] [blame] | 1635 | spin_unlock(&hb1->lock); |
Ingo Molnar | 88f502f | 2009-03-13 10:32:07 +0100 | [diff] [blame] | 1636 | if (hb1 != hb2) |
| 1637 | spin_unlock(&hb2->lock); |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Ingo Molnar | 8b8f319 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1640 | /* |
Darren Hart | b2d0994 | 2009-03-12 00:55:37 -0700 | [diff] [blame] | 1641 | * Wake up waiters matching bitset queued on this futex (uaddr). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 1643 | static int |
| 1644 | futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | { |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1646 | struct futex_hash_bucket *hb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | struct futex_q *this, *next; |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 1648 | union futex_key key = FUTEX_KEY_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | int ret; |
Waiman Long | 194a6b5 | 2016-11-17 11:46:38 -0500 | [diff] [blame] | 1650 | DEFINE_WAKE_Q(wake_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 1652 | if (!bitset) |
| 1653 | return -EINVAL; |
| 1654 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1655 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | if (unlikely(ret != 0)) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 1657 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1659 | hb = hash_futex(&key); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 1660 | |
| 1661 | /* Make sure we really have tasks to wakeup */ |
| 1662 | if (!hb_waiters_pending(hb)) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 1663 | return ret; |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 1664 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1665 | spin_lock(&hb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 1667 | plist_for_each_entry_safe(this, next, &hb->chain, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | if (match_futex (&this->key, &key)) { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1669 | if (this->pi_state || this->rt_waiter) { |
Ingo Molnar | ed6f7b1 | 2006-07-01 04:35:46 -0700 | [diff] [blame] | 1670 | ret = -EINVAL; |
| 1671 | break; |
| 1672 | } |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 1673 | |
| 1674 | /* Check if one of the bits is set in both bitsets */ |
| 1675 | if (!(this->bitset & bitset)) |
| 1676 | continue; |
| 1677 | |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1678 | mark_wake_futex(&wake_q, this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | if (++ret >= nr_wake) |
| 1680 | break; |
| 1681 | } |
| 1682 | } |
| 1683 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1684 | spin_unlock(&hb->lock); |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1685 | wake_up_q(&wake_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1686 | return ret; |
| 1687 | } |
| 1688 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1689 | static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr) |
| 1690 | { |
| 1691 | unsigned int op = (encoded_op & 0x70000000) >> 28; |
| 1692 | unsigned int cmp = (encoded_op & 0x0f000000) >> 24; |
Jiri Slaby | d70ef22 | 2017-11-30 15:35:44 +0100 | [diff] [blame] | 1693 | int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11); |
| 1694 | int cmparg = sign_extend32(encoded_op & 0x00000fff, 11); |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1695 | int oldval, ret; |
| 1696 | |
| 1697 | if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) { |
Jiri Slaby | e78c38f6 | 2017-10-23 13:41:51 +0200 | [diff] [blame] | 1698 | if (oparg < 0 || oparg > 31) { |
| 1699 | char comm[sizeof(current->comm)]; |
| 1700 | /* |
| 1701 | * kill this print and return -EINVAL when userspace |
| 1702 | * is sane again |
| 1703 | */ |
| 1704 | pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n", |
| 1705 | get_task_comm(comm, current), oparg); |
| 1706 | oparg &= 31; |
| 1707 | } |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1708 | oparg = 1 << oparg; |
| 1709 | } |
| 1710 | |
Al Viro | a08971e | 2020-02-16 10:17:27 -0500 | [diff] [blame] | 1711 | pagefault_disable(); |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1712 | ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr); |
Al Viro | a08971e | 2020-02-16 10:17:27 -0500 | [diff] [blame] | 1713 | pagefault_enable(); |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1714 | if (ret) |
| 1715 | return ret; |
| 1716 | |
| 1717 | switch (cmp) { |
| 1718 | case FUTEX_OP_CMP_EQ: |
| 1719 | return oldval == cmparg; |
| 1720 | case FUTEX_OP_CMP_NE: |
| 1721 | return oldval != cmparg; |
| 1722 | case FUTEX_OP_CMP_LT: |
| 1723 | return oldval < cmparg; |
| 1724 | case FUTEX_OP_CMP_GE: |
| 1725 | return oldval >= cmparg; |
| 1726 | case FUTEX_OP_CMP_LE: |
| 1727 | return oldval <= cmparg; |
| 1728 | case FUTEX_OP_CMP_GT: |
| 1729 | return oldval > cmparg; |
| 1730 | default: |
| 1731 | return -ENOSYS; |
| 1732 | } |
| 1733 | } |
| 1734 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | /* |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1736 | * Wake up all waiters hashed on the physical page that is mapped |
| 1737 | * to this virtual address: |
| 1738 | */ |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1739 | static int |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 1740 | futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2, |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1741 | int nr_wake, int nr_wake2, int op) |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1742 | { |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 1743 | union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1744 | struct futex_hash_bucket *hb1, *hb2; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1745 | struct futex_q *this, *next; |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1746 | int ret, op_ret; |
Waiman Long | 194a6b5 | 2016-11-17 11:46:38 -0500 | [diff] [blame] | 1747 | DEFINE_WAKE_Q(wake_q); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1748 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1749 | retry: |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1750 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1751 | if (unlikely(ret != 0)) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 1752 | return ret; |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 1753 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1754 | if (unlikely(ret != 0)) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 1755 | return ret; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1756 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1757 | hb1 = hash_futex(&key1); |
| 1758 | hb2 = hash_futex(&key2); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1759 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1760 | retry_private: |
Thomas Gleixner | eaaea80 | 2009-10-04 09:34:17 +0200 | [diff] [blame] | 1761 | double_lock_hb(hb1, hb2); |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1762 | op_ret = futex_atomic_op_inuser(op, uaddr2); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1763 | if (unlikely(op_ret < 0)) { |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1764 | double_unlock_hb(hb1, hb2); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1765 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 1766 | if (!IS_ENABLED(CONFIG_MMU) || |
| 1767 | unlikely(op_ret != -EFAULT && op_ret != -EAGAIN)) { |
| 1768 | /* |
| 1769 | * we don't get EFAULT from MMU faults if we don't have |
| 1770 | * an MMU, but we might get them from range checking |
| 1771 | */ |
David Gibson | 796f8d9 | 2005-11-07 00:59:33 -0800 | [diff] [blame] | 1772 | ret = op_ret; |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 1773 | return ret; |
David Gibson | 796f8d9 | 2005-11-07 00:59:33 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 1776 | if (op_ret == -EFAULT) { |
| 1777 | ret = fault_in_user_writeable(uaddr2); |
| 1778 | if (ret) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 1779 | return ret; |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 1780 | } |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1781 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 1782 | cond_resched(); |
Pavel Begunkov | a82adc7 | 2021-05-17 14:30:12 +0100 | [diff] [blame] | 1783 | if (!(flags & FLAGS_SHARED)) |
| 1784 | goto retry_private; |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1785 | goto retry; |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1786 | } |
| 1787 | |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 1788 | plist_for_each_entry_safe(this, next, &hb1->chain, list) { |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1789 | if (match_futex (&this->key, &key1)) { |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1790 | if (this->pi_state || this->rt_waiter) { |
| 1791 | ret = -EINVAL; |
| 1792 | goto out_unlock; |
| 1793 | } |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1794 | mark_wake_futex(&wake_q, this); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1795 | if (++ret >= nr_wake) |
| 1796 | break; |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | if (op_ret > 0) { |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1801 | op_ret = 0; |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 1802 | plist_for_each_entry_safe(this, next, &hb2->chain, list) { |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1803 | if (match_futex (&this->key, &key2)) { |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1804 | if (this->pi_state || this->rt_waiter) { |
| 1805 | ret = -EINVAL; |
| 1806 | goto out_unlock; |
| 1807 | } |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1808 | mark_wake_futex(&wake_q, this); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1809 | if (++op_ret >= nr_wake2) |
| 1810 | break; |
| 1811 | } |
| 1812 | } |
| 1813 | ret += op_ret; |
| 1814 | } |
| 1815 | |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1816 | out_unlock: |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1817 | double_unlock_hb(hb1, hb2); |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1818 | wake_up_q(&wake_q); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1819 | return ret; |
| 1820 | } |
| 1821 | |
Darren Hart | 9121e47 | 2009-04-03 13:40:31 -0700 | [diff] [blame] | 1822 | /** |
| 1823 | * requeue_futex() - Requeue a futex_q from one hb to another |
| 1824 | * @q: the futex_q to requeue |
| 1825 | * @hb1: the source hash_bucket |
| 1826 | * @hb2: the target hash_bucket |
| 1827 | * @key2: the new key for the requeued futex_q |
| 1828 | */ |
| 1829 | static inline |
| 1830 | void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1, |
| 1831 | struct futex_hash_bucket *hb2, union futex_key *key2) |
| 1832 | { |
| 1833 | |
| 1834 | /* |
| 1835 | * If key1 and key2 hash to the same bucket, no need to |
| 1836 | * requeue. |
| 1837 | */ |
| 1838 | if (likely(&hb1->chain != &hb2->chain)) { |
| 1839 | plist_del(&q->list, &hb1->chain); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 1840 | hb_waiters_dec(hb1); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 1841 | hb_waiters_inc(hb2); |
Davidlohr Bueso | fe1bce9 | 2016-04-20 20:09:24 -0700 | [diff] [blame] | 1842 | plist_add(&q->list, &hb2->chain); |
Darren Hart | 9121e47 | 2009-04-03 13:40:31 -0700 | [diff] [blame] | 1843 | q->lock_ptr = &hb2->lock; |
Darren Hart | 9121e47 | 2009-04-03 13:40:31 -0700 | [diff] [blame] | 1844 | } |
Darren Hart | 9121e47 | 2009-04-03 13:40:31 -0700 | [diff] [blame] | 1845 | q->key = *key2; |
| 1846 | } |
| 1847 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 1848 | static inline bool futex_requeue_pi_prepare(struct futex_q *q, |
| 1849 | struct futex_pi_state *pi_state) |
| 1850 | { |
| 1851 | int old, new; |
| 1852 | |
| 1853 | /* |
| 1854 | * Set state to Q_REQUEUE_PI_IN_PROGRESS unless an early wakeup has |
| 1855 | * already set Q_REQUEUE_PI_IGNORE to signal that requeue should |
| 1856 | * ignore the waiter. |
| 1857 | */ |
| 1858 | old = atomic_read_acquire(&q->requeue_state); |
| 1859 | do { |
| 1860 | if (old == Q_REQUEUE_PI_IGNORE) |
| 1861 | return false; |
| 1862 | |
| 1863 | /* |
| 1864 | * futex_proxy_trylock_atomic() might have set it to |
| 1865 | * IN_PROGRESS and a interleaved early wake to WAIT. |
| 1866 | * |
| 1867 | * It was considered to have an extra state for that |
| 1868 | * trylock, but that would just add more conditionals |
| 1869 | * all over the place for a dubious value. |
| 1870 | */ |
| 1871 | if (old != Q_REQUEUE_PI_NONE) |
| 1872 | break; |
| 1873 | |
| 1874 | new = Q_REQUEUE_PI_IN_PROGRESS; |
| 1875 | } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new)); |
| 1876 | |
| 1877 | q->pi_state = pi_state; |
| 1878 | return true; |
| 1879 | } |
| 1880 | |
| 1881 | static inline void futex_requeue_pi_complete(struct futex_q *q, int locked) |
| 1882 | { |
| 1883 | int old, new; |
| 1884 | |
| 1885 | old = atomic_read_acquire(&q->requeue_state); |
| 1886 | do { |
| 1887 | if (old == Q_REQUEUE_PI_IGNORE) |
| 1888 | return; |
| 1889 | |
| 1890 | if (locked >= 0) { |
| 1891 | /* Requeue succeeded. Set DONE or LOCKED */ |
| 1892 | WARN_ON_ONCE(old != Q_REQUEUE_PI_IN_PROGRESS && |
| 1893 | old != Q_REQUEUE_PI_WAIT); |
| 1894 | new = Q_REQUEUE_PI_DONE + locked; |
| 1895 | } else if (old == Q_REQUEUE_PI_IN_PROGRESS) { |
| 1896 | /* Deadlock, no early wakeup interleave */ |
| 1897 | new = Q_REQUEUE_PI_NONE; |
| 1898 | } else { |
| 1899 | /* Deadlock, early wakeup interleave. */ |
| 1900 | WARN_ON_ONCE(old != Q_REQUEUE_PI_WAIT); |
| 1901 | new = Q_REQUEUE_PI_IGNORE; |
| 1902 | } |
| 1903 | } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new)); |
| 1904 | |
| 1905 | #ifdef CONFIG_PREEMPT_RT |
| 1906 | /* If the waiter interleaved with the requeue let it know */ |
| 1907 | if (unlikely(old == Q_REQUEUE_PI_WAIT)) |
| 1908 | rcuwait_wake_up(&q->requeue_wait); |
| 1909 | #endif |
| 1910 | } |
| 1911 | |
| 1912 | static inline int futex_requeue_pi_wakeup_sync(struct futex_q *q) |
| 1913 | { |
| 1914 | int old, new; |
| 1915 | |
| 1916 | old = atomic_read_acquire(&q->requeue_state); |
| 1917 | do { |
| 1918 | /* Is requeue done already? */ |
| 1919 | if (old >= Q_REQUEUE_PI_DONE) |
| 1920 | return old; |
| 1921 | |
| 1922 | /* |
| 1923 | * If not done, then tell the requeue code to either ignore |
| 1924 | * the waiter or to wake it up once the requeue is done. |
| 1925 | */ |
| 1926 | new = Q_REQUEUE_PI_WAIT; |
| 1927 | if (old == Q_REQUEUE_PI_NONE) |
| 1928 | new = Q_REQUEUE_PI_IGNORE; |
| 1929 | } while (!atomic_try_cmpxchg(&q->requeue_state, &old, new)); |
| 1930 | |
| 1931 | /* If the requeue was in progress, wait for it to complete */ |
| 1932 | if (old == Q_REQUEUE_PI_IN_PROGRESS) { |
| 1933 | #ifdef CONFIG_PREEMPT_RT |
| 1934 | rcuwait_wait_event(&q->requeue_wait, |
| 1935 | atomic_read(&q->requeue_state) != Q_REQUEUE_PI_WAIT, |
| 1936 | TASK_UNINTERRUPTIBLE); |
| 1937 | #else |
| 1938 | (void)atomic_cond_read_relaxed(&q->requeue_state, VAL != Q_REQUEUE_PI_WAIT); |
| 1939 | #endif |
| 1940 | } |
| 1941 | |
| 1942 | /* |
| 1943 | * Requeue is now either prohibited or complete. Reread state |
| 1944 | * because during the wait above it might have changed. Nothing |
| 1945 | * will modify q->requeue_state after this point. |
| 1946 | */ |
| 1947 | return atomic_read(&q->requeue_state); |
| 1948 | } |
| 1949 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1950 | /** |
| 1951 | * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 1952 | * @q: the futex_q |
| 1953 | * @key: the key of the requeue target futex |
| 1954 | * @hb: the hash_bucket of the requeue target futex |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1955 | * |
| 1956 | * During futex_requeue, with requeue_pi=1, it is possible to acquire the |
| 1957 | * target futex if it is uncontended or via a lock steal. Set the futex_q key |
| 1958 | * to the requeue target futex so the waiter can detect the wakeup on the right |
| 1959 | * futex, but remove it from the hb and NULL the rt_waiter so it can detect |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1960 | * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock |
| 1961 | * to protect access to the pi_state to fixup the owner later. Must be called |
| 1962 | * with both q->lock_ptr and hb->lock held. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1963 | */ |
| 1964 | static inline |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1965 | void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key, |
| 1966 | struct futex_hash_bucket *hb) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1967 | { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1968 | q->key = *key; |
| 1969 | |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1970 | __unqueue_futex(q); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1971 | |
| 1972 | WARN_ON(!q->rt_waiter); |
| 1973 | q->rt_waiter = NULL; |
| 1974 | |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1975 | q->lock_ptr = &hb->lock; |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1976 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 1977 | /* Signal locked state to the waiter */ |
| 1978 | futex_requeue_pi_complete(q, 1); |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 1979 | wake_up_state(q->task, TASK_NORMAL); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | /** |
| 1983 | * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1984 | * @pifutex: the user address of the to futex |
| 1985 | * @hb1: the from futex hash bucket, must be locked by the caller |
| 1986 | * @hb2: the to futex hash bucket, must be locked by the caller |
| 1987 | * @key1: the from futex key |
| 1988 | * @key2: the to futex key |
| 1989 | * @ps: address to store the pi_state pointer |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1990 | * @exiting: Pointer to store the task pointer of the owner task |
| 1991 | * which is in the middle of exiting |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1992 | * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1993 | * |
| 1994 | * Try and get the lock on behalf of the top waiter if we can do it atomically. |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1995 | * Wake the top waiter if we succeed. If the caller specified set_waiters, |
| 1996 | * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit. |
| 1997 | * hb1 and hb2 must be held by the caller. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1998 | * |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 1999 | * @exiting is only set when the return value is -EBUSY. If so, this holds |
| 2000 | * a refcount on the exiting task on return and the caller needs to drop it |
| 2001 | * after waiting for the exit to complete. |
| 2002 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2003 | * Return: |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 2004 | * - 0 - failed to acquire the lock atomically; |
| 2005 | * - >0 - acquired the lock, return value is vpid of the top_waiter |
| 2006 | * - <0 - error |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2007 | */ |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 2008 | static int |
| 2009 | futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1, |
| 2010 | struct futex_hash_bucket *hb2, union futex_key *key1, |
| 2011 | union futex_key *key2, struct futex_pi_state **ps, |
| 2012 | struct task_struct **exiting, int set_waiters) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2013 | { |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 2014 | struct futex_q *top_waiter = NULL; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2015 | u32 curval; |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 2016 | int ret, vpid; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2017 | |
| 2018 | if (get_futex_value_locked(&curval, pifutex)) |
| 2019 | return -EFAULT; |
| 2020 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 2021 | if (unlikely(should_fail_futex(true))) |
| 2022 | return -EFAULT; |
| 2023 | |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 2024 | /* |
| 2025 | * Find the top_waiter and determine if there are additional waiters. |
| 2026 | * If the caller intends to requeue more than 1 waiter to pifutex, |
| 2027 | * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now, |
| 2028 | * as we have means to handle the possible fault. If not, don't set |
Ingo Molnar | 93d0955 | 2021-05-12 20:04:28 +0200 | [diff] [blame] | 2029 | * the bit unnecessarily as it will force the subsequent unlock to enter |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 2030 | * the kernel. |
| 2031 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2032 | top_waiter = futex_top_waiter(hb1, key1); |
| 2033 | |
| 2034 | /* There are no waiters, nothing for us to do. */ |
| 2035 | if (!top_waiter) |
| 2036 | return 0; |
| 2037 | |
Thomas Gleixner | dc7109a | 2021-08-15 23:29:04 +0200 | [diff] [blame] | 2038 | /* |
| 2039 | * Ensure that this is a waiter sitting in futex_wait_requeue_pi() |
| 2040 | * and waiting on the 'waitqueue' futex which is always !PI. |
| 2041 | */ |
| 2042 | if (!top_waiter->rt_waiter || top_waiter->pi_state) |
Colin Ian King | a974b54 | 2021-08-18 14:18:40 +0100 | [diff] [blame] | 2043 | return -EINVAL; |
Thomas Gleixner | dc7109a | 2021-08-15 23:29:04 +0200 | [diff] [blame] | 2044 | |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 2045 | /* Ensure we requeue to the expected futex. */ |
| 2046 | if (!match_futex(top_waiter->requeue_pi_key, key2)) |
| 2047 | return -EINVAL; |
| 2048 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2049 | /* Ensure that this does not race against an early wakeup */ |
| 2050 | if (!futex_requeue_pi_prepare(top_waiter, NULL)) |
| 2051 | return -EAGAIN; |
| 2052 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2053 | /* |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 2054 | * Try to take the lock for top_waiter and set the FUTEX_WAITERS bit |
| 2055 | * in the contended case or if @set_waiters is true. |
| 2056 | * |
| 2057 | * In the contended case PI state is attached to the lock owner. If |
| 2058 | * the user space lock can be acquired then PI state is attached to |
| 2059 | * the new owner (@top_waiter->task) when @set_waiters is true. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2060 | */ |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 2061 | vpid = task_pid_vnr(top_waiter->task); |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 2062 | ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task, |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 2063 | exiting, set_waiters); |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 2064 | if (ret == 1) { |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 2065 | /* |
| 2066 | * Lock was acquired in user space and PI state was |
| 2067 | * attached to @top_waiter->task. That means state is fully |
| 2068 | * consistent and the waiter can return to user space |
| 2069 | * immediately after the wakeup. |
| 2070 | */ |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 2071 | requeue_pi_wake_futex(top_waiter, key2, hb2); |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2072 | } else if (ret < 0) { |
| 2073 | /* Rewind top_waiter::requeue_state */ |
| 2074 | futex_requeue_pi_complete(top_waiter, ret); |
| 2075 | } else { |
| 2076 | /* |
| 2077 | * futex_lock_pi_atomic() did not acquire the user space |
| 2078 | * futex, but managed to establish the proxy lock and pi |
| 2079 | * state. top_waiter::requeue_state cannot be fixed up here |
| 2080 | * because the waiter is not enqueued on the rtmutex |
| 2081 | * yet. This is handled at the callsite depending on the |
| 2082 | * result of rt_mutex_start_proxy_lock() which is |
| 2083 | * guaranteed to be reached with this function returning 0. |
| 2084 | */ |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 2085 | } |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2086 | return ret; |
| 2087 | } |
| 2088 | |
| 2089 | /** |
| 2090 | * futex_requeue() - Requeue waiters from uaddr1 to uaddr2 |
Randy Dunlap | fb62db2 | 2010-10-13 11:02:34 -0700 | [diff] [blame] | 2091 | * @uaddr1: source futex user address |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2092 | * @flags: futex flags (FLAGS_SHARED, etc.) |
Randy Dunlap | fb62db2 | 2010-10-13 11:02:34 -0700 | [diff] [blame] | 2093 | * @uaddr2: target futex user address |
| 2094 | * @nr_wake: number of waiters to wake (must be 1 for requeue_pi) |
| 2095 | * @nr_requeue: number of waiters to requeue (0-INT_MAX) |
| 2096 | * @cmpval: @uaddr1 expected value (or %NULL) |
| 2097 | * @requeue_pi: if we are attempting to requeue from a non-pi futex to a |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2098 | * pi futex (pi to pi requeue is not supported) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2099 | * |
| 2100 | * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire |
| 2101 | * uaddr2 atomically on behalf of the top waiter. |
| 2102 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2103 | * Return: |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 2104 | * - >=0 - on success, the number of tasks requeued or woken; |
| 2105 | * - <0 - on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2107 | static int futex_requeue(u32 __user *uaddr1, unsigned int flags, |
| 2108 | u32 __user *uaddr2, int nr_wake, int nr_requeue, |
| 2109 | u32 *cmpval, int requeue_pi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | { |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 2111 | union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT; |
Peter Zijlstra | 4b39f99 | 2020-03-04 13:24:24 +0100 | [diff] [blame] | 2112 | int task_count = 0, ret; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2113 | struct futex_pi_state *pi_state = NULL; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2114 | struct futex_hash_bucket *hb1, *hb2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | struct futex_q *this, *next; |
Waiman Long | 194a6b5 | 2016-11-17 11:46:38 -0500 | [diff] [blame] | 2116 | DEFINE_WAKE_Q(wake_q); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2117 | |
Li Jinyue | fbe0e83 | 2017-12-14 17:04:54 +0800 | [diff] [blame] | 2118 | if (nr_wake < 0 || nr_requeue < 0) |
| 2119 | return -EINVAL; |
| 2120 | |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 2121 | /* |
| 2122 | * When PI not supported: return -ENOSYS if requeue_pi is true, |
| 2123 | * consequently the compiler knows requeue_pi is always false past |
| 2124 | * this point which will optimize away all the conditional code |
| 2125 | * further down. |
| 2126 | */ |
| 2127 | if (!IS_ENABLED(CONFIG_FUTEX_PI) && requeue_pi) |
| 2128 | return -ENOSYS; |
| 2129 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2130 | if (requeue_pi) { |
| 2131 | /* |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 2132 | * Requeue PI only works on two distinct uaddrs. This |
| 2133 | * check is only valid for private futexes. See below. |
| 2134 | */ |
| 2135 | if (uaddr1 == uaddr2) |
| 2136 | return -EINVAL; |
| 2137 | |
| 2138 | /* |
Thomas Gleixner | c18eaa3 | 2021-08-15 23:29:14 +0200 | [diff] [blame] | 2139 | * futex_requeue() allows the caller to define the number |
| 2140 | * of waiters to wake up via the @nr_wake argument. With |
| 2141 | * REQUEUE_PI, waking up more than one waiter is creating |
| 2142 | * more problems than it solves. Waking up a waiter makes |
| 2143 | * only sense if the PI futex @uaddr2 is uncontended as |
| 2144 | * this allows the requeue code to acquire the futex |
| 2145 | * @uaddr2 before waking the waiter. The waiter can then |
| 2146 | * return to user space without further action. A secondary |
| 2147 | * wakeup would just make the futex_wait_requeue_pi() |
| 2148 | * handling more complex, because that code would have to |
| 2149 | * look up pi_state and do more or less all the handling |
| 2150 | * which the requeue code has to do for the to be requeued |
| 2151 | * waiters. So restrict the number of waiters to wake to |
| 2152 | * one, and only wake it up when the PI futex is |
| 2153 | * uncontended. Otherwise requeue it and let the unlock of |
| 2154 | * the PI futex handle the wakeup. |
| 2155 | * |
| 2156 | * All REQUEUE_PI users, e.g. pthread_cond_signal() and |
| 2157 | * pthread_cond_broadcast() must use nr_wake=1. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2158 | */ |
| 2159 | if (nr_wake != 1) |
| 2160 | return -EINVAL; |
Thomas Gleixner | d69cba5 | 2021-08-15 23:29:15 +0200 | [diff] [blame] | 2161 | |
| 2162 | /* |
| 2163 | * requeue_pi requires a pi_state, try to allocate it now |
| 2164 | * without any locks in case it fails. |
| 2165 | */ |
| 2166 | if (refill_pi_state_cache()) |
| 2167 | return -ENOMEM; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2170 | retry: |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2171 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, FUTEX_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | if (unlikely(ret != 0)) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 2173 | return ret; |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 2174 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2175 | requeue_pi ? FUTEX_WRITE : FUTEX_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | if (unlikely(ret != 0)) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 2177 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 2179 | /* |
| 2180 | * The check above which compares uaddrs is not sufficient for |
| 2181 | * shared futexes. We need to compare the keys: |
| 2182 | */ |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 2183 | if (requeue_pi && match_futex(&key1, &key2)) |
| 2184 | return -EINVAL; |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 2185 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2186 | hb1 = hash_futex(&key1); |
| 2187 | hb2 = hash_futex(&key2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2189 | retry_private: |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2190 | hb_waiters_inc(hb2); |
Ingo Molnar | 8b8f319 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2191 | double_lock_hb(hb1, hb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2193 | if (likely(cmpval != NULL)) { |
| 2194 | u32 curval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2196 | ret = get_futex_value_locked(&curval, uaddr1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
| 2198 | if (unlikely(ret)) { |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 2199 | double_unlock_hb(hb1, hb2); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2200 | hb_waiters_dec(hb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2202 | ret = get_user(curval, uaddr1); |
| 2203 | if (ret) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 2204 | return ret; |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2205 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2206 | if (!(flags & FLAGS_SHARED)) |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2207 | goto retry_private; |
| 2208 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2209 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2210 | } |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2211 | if (curval != *cmpval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2212 | ret = -EAGAIN; |
| 2213 | goto out_unlock; |
| 2214 | } |
| 2215 | } |
| 2216 | |
Thomas Gleixner | 8e74633 | 2021-08-15 23:29:09 +0200 | [diff] [blame] | 2217 | if (requeue_pi) { |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 2218 | struct task_struct *exiting = NULL; |
| 2219 | |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 2220 | /* |
| 2221 | * Attempt to acquire uaddr2 and wake the top waiter. If we |
| 2222 | * intend to requeue waiters, force setting the FUTEX_WAITERS |
| 2223 | * bit. We force this here where we are able to easily handle |
| 2224 | * faults rather in the requeue loop below. |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2225 | * |
| 2226 | * Updates topwaiter::requeue_state if a top waiter exists. |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 2227 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2228 | ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1, |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 2229 | &key2, &pi_state, |
| 2230 | &exiting, nr_requeue); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2231 | |
| 2232 | /* |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 2233 | * At this point the top_waiter has either taken uaddr2 or |
| 2234 | * is waiting on it. In both cases pi_state has been |
| 2235 | * established and an initial refcount on it. In case of an |
| 2236 | * error there's nothing. |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2237 | * |
| 2238 | * The top waiter's requeue_state is up to date: |
| 2239 | * |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 2240 | * - If the lock was acquired atomically (ret == 1), then |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2241 | * the state is Q_REQUEUE_PI_LOCKED. |
| 2242 | * |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 2243 | * The top waiter has been dequeued and woken up and can |
| 2244 | * return to user space immediately. The kernel/user |
| 2245 | * space state is consistent. In case that there must be |
| 2246 | * more waiters requeued the WAITERS bit in the user |
| 2247 | * space futex is set so the top waiter task has to go |
| 2248 | * into the syscall slowpath to unlock the futex. This |
| 2249 | * will block until this requeue operation has been |
| 2250 | * completed and the hash bucket locks have been |
| 2251 | * dropped. |
| 2252 | * |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2253 | * - If the trylock failed with an error (ret < 0) then |
| 2254 | * the state is either Q_REQUEUE_PI_NONE, i.e. "nothing |
| 2255 | * happened", or Q_REQUEUE_PI_IGNORE when there was an |
| 2256 | * interleaved early wakeup. |
| 2257 | * |
| 2258 | * - If the trylock did not succeed (ret == 0) then the |
| 2259 | * state is either Q_REQUEUE_PI_IN_PROGRESS or |
| 2260 | * Q_REQUEUE_PI_WAIT if an early wakeup interleaved. |
| 2261 | * This will be cleaned up in the loop below, which |
| 2262 | * cannot fail because futex_proxy_trylock_atomic() did |
| 2263 | * the same sanity checks for requeue_pi as the loop |
| 2264 | * below does. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2265 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2266 | switch (ret) { |
| 2267 | case 0: |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2268 | /* We hold a reference on the pi state. */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2269 | break; |
Thomas Gleixner | 4959f2d | 2015-12-19 20:07:40 +0000 | [diff] [blame] | 2270 | |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 2271 | case 1: |
| 2272 | /* |
| 2273 | * futex_proxy_trylock_atomic() acquired the user space |
| 2274 | * futex. Adjust task_count. |
| 2275 | */ |
| 2276 | task_count++; |
| 2277 | ret = 0; |
| 2278 | break; |
| 2279 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2280 | /* |
| 2281 | * If the above failed, then pi_state is NULL and |
| 2282 | * waiter::requeue_state is correct. |
| 2283 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2284 | case -EFAULT: |
| 2285 | double_unlock_hb(hb1, hb2); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2286 | hb_waiters_dec(hb2); |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 2287 | ret = fault_in_user_writeable(uaddr2); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2288 | if (!ret) |
| 2289 | goto retry; |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 2290 | return ret; |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 2291 | case -EBUSY: |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2292 | case -EAGAIN: |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2293 | /* |
| 2294 | * Two reasons for this: |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 2295 | * - EBUSY: Owner is exiting and we just wait for the |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2296 | * exit to complete. |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 2297 | * - EAGAIN: The user space value changed. |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2298 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2299 | double_unlock_hb(hb1, hb2); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2300 | hb_waiters_dec(hb2); |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 2301 | /* |
| 2302 | * Handle the case where the owner is in the middle of |
| 2303 | * exiting. Wait for the exit to complete otherwise |
| 2304 | * this task might loop forever, aka. live lock. |
| 2305 | */ |
| 2306 | wait_for_owner_exiting(ret, exiting); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2307 | cond_resched(); |
| 2308 | goto retry; |
| 2309 | default: |
| 2310 | goto out_unlock; |
| 2311 | } |
| 2312 | } |
| 2313 | |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2314 | plist_for_each_entry_safe(this, next, &hb1->chain, list) { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2315 | if (task_count - nr_wake >= nr_requeue) |
| 2316 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2318 | if (!match_futex(&this->key, &key1)) |
| 2319 | continue; |
| 2320 | |
Darren Hart | 392741e | 2009-08-07 15:20:48 -0700 | [diff] [blame] | 2321 | /* |
Ingo Molnar | 93d0955 | 2021-05-12 20:04:28 +0200 | [diff] [blame] | 2322 | * FUTEX_WAIT_REQUEUE_PI and FUTEX_CMP_REQUEUE_PI should always |
Darren Hart | 392741e | 2009-08-07 15:20:48 -0700 | [diff] [blame] | 2323 | * be paired with each other and no other futex ops. |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 2324 | * |
| 2325 | * We should never be requeueing a futex_q with a pi_state, |
| 2326 | * which is awaiting a futex_unlock_pi(). |
Darren Hart | 392741e | 2009-08-07 15:20:48 -0700 | [diff] [blame] | 2327 | */ |
| 2328 | if ((requeue_pi && !this->rt_waiter) || |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 2329 | (!requeue_pi && this->rt_waiter) || |
| 2330 | this->pi_state) { |
Darren Hart | 392741e | 2009-08-07 15:20:48 -0700 | [diff] [blame] | 2331 | ret = -EINVAL; |
| 2332 | break; |
| 2333 | } |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2334 | |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2335 | /* Plain futexes just wake or requeue and are done */ |
| 2336 | if (!requeue_pi) { |
| 2337 | if (++task_count <= nr_wake) |
| 2338 | mark_wake_futex(&wake_q, this); |
| 2339 | else |
| 2340 | requeue_futex(this, hb1, hb2, &key2); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2341 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | } |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2343 | |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 2344 | /* Ensure we requeue to the expected futex for requeue_pi. */ |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2345 | if (!match_futex(this->requeue_pi_key, &key2)) { |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 2346 | ret = -EINVAL; |
| 2347 | break; |
| 2348 | } |
| 2349 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2350 | /* |
| 2351 | * Requeue nr_requeue waiters and possibly one more in the case |
| 2352 | * of requeue_pi if we couldn't acquire the lock atomically. |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2353 | * |
| 2354 | * Prepare the waiter to take the rt_mutex. Take a refcount |
| 2355 | * on the pi_state and store the pointer in the futex_q |
| 2356 | * object of the waiter. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2357 | */ |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2358 | get_pi_state(pi_state); |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2359 | |
| 2360 | /* Don't requeue when the waiter is already on the way out. */ |
| 2361 | if (!futex_requeue_pi_prepare(this, pi_state)) { |
| 2362 | /* |
| 2363 | * Early woken waiter signaled that it is on the |
| 2364 | * way out. Drop the pi_state reference and try the |
| 2365 | * next waiter. @this->pi_state is still NULL. |
| 2366 | */ |
| 2367 | put_pi_state(pi_state); |
| 2368 | continue; |
| 2369 | } |
| 2370 | |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2371 | ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex, |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2372 | this->rt_waiter, |
| 2373 | this->task); |
| 2374 | |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2375 | if (ret == 1) { |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2376 | /* |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2377 | * We got the lock. We do neither drop the refcount |
| 2378 | * on pi_state nor clear this->pi_state because the |
| 2379 | * waiter needs the pi_state for cleaning up the |
| 2380 | * user space value. It will drop the refcount |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2381 | * after doing so. this::requeue_state is updated |
| 2382 | * in the wakeup as well. |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2383 | */ |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2384 | requeue_pi_wake_futex(this, &key2, hb2); |
| 2385 | task_count++; |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2386 | } else if (!ret) { |
| 2387 | /* Waiter is queued, move it to hb2 */ |
| 2388 | requeue_futex(this, hb1, hb2, &key2); |
| 2389 | futex_requeue_pi_complete(this, 0); |
| 2390 | task_count++; |
| 2391 | } else { |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2392 | /* |
| 2393 | * rt_mutex_start_proxy_lock() detected a potential |
| 2394 | * deadlock when we tried to queue that waiter. |
| 2395 | * Drop the pi_state reference which we took above |
| 2396 | * and remove the pointer to the state from the |
| 2397 | * waiters futex_q object. |
| 2398 | */ |
| 2399 | this->pi_state = NULL; |
| 2400 | put_pi_state(pi_state); |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 2401 | futex_requeue_pi_complete(this, ret); |
Thomas Gleixner | 64b7b71 | 2021-08-15 23:29:12 +0200 | [diff] [blame] | 2402 | /* |
| 2403 | * We stop queueing more waiters and let user space |
| 2404 | * deal with the mess. |
| 2405 | */ |
| 2406 | break; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2410 | /* |
Thomas Gleixner | 4f07ec0 | 2021-09-02 11:48:48 +0200 | [diff] [blame^] | 2411 | * We took an extra initial reference to the pi_state in |
| 2412 | * futex_proxy_trylock_atomic(). We need to drop it here again. |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2413 | */ |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2414 | put_pi_state(pi_state); |
Thomas Gleixner | 885c2cb | 2015-12-19 20:07:41 +0000 | [diff] [blame] | 2415 | |
| 2416 | out_unlock: |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 2417 | double_unlock_hb(hb1, hb2); |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 2418 | wake_up_q(&wake_q); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2419 | hb_waiters_dec(hb2); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2420 | return ret ? ret : task_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | /* The key must be already stored in q->key. */ |
Eric Sesterhenn | 82af7ac | 2008-01-25 10:40:46 +0100 | [diff] [blame] | 2424 | static inline struct futex_hash_bucket *queue_lock(struct futex_q *q) |
Namhyung Kim | 15e408c | 2010-09-14 21:43:48 +0900 | [diff] [blame] | 2425 | __acquires(&hb->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | { |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2427 | struct futex_hash_bucket *hb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2429 | hb = hash_futex(&q->key); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 2430 | |
| 2431 | /* |
| 2432 | * Increment the counter before taking the lock so that |
| 2433 | * a potential waker won't miss a to-be-slept task that is |
| 2434 | * waiting for the spinlock. This is safe as all queue_lock() |
| 2435 | * users end up calling queue_me(). Similarly, for housekeeping, |
| 2436 | * decrement the counter at queue_unlock() when some error has |
| 2437 | * occurred and we don't end up adding the task to the list. |
| 2438 | */ |
Davidlohr Bueso | 6f568eb | 2019-02-06 10:56:02 -0800 | [diff] [blame] | 2439 | hb_waiters_inc(hb); /* implies smp_mb(); (A) */ |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 2440 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2441 | q->lock_ptr = &hb->lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | |
Davidlohr Bueso | 6f568eb | 2019-02-06 10:56:02 -0800 | [diff] [blame] | 2443 | spin_lock(&hb->lock); |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2444 | return hb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2447 | static inline void |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2448 | queue_unlock(struct futex_hash_bucket *hb) |
Namhyung Kim | 15e408c | 2010-09-14 21:43:48 +0900 | [diff] [blame] | 2449 | __releases(&hb->lock) |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2450 | { |
| 2451 | spin_unlock(&hb->lock); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 2452 | hb_waiters_dec(hb); |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2453 | } |
| 2454 | |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 2455 | static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | { |
Pierre Peiffer | ec92d08 | 2007-05-09 02:35:00 -0700 | [diff] [blame] | 2457 | int prio; |
| 2458 | |
| 2459 | /* |
| 2460 | * The priority used to register this element is |
| 2461 | * - either the real thread-priority for the real-time threads |
| 2462 | * (i.e. threads with a priority lower than MAX_RT_PRIO) |
| 2463 | * - or MAX_RT_PRIO for non-RT threads. |
| 2464 | * Thus, all RT-threads are woken first in priority order, and |
| 2465 | * the others are woken last, in FIFO order. |
| 2466 | */ |
| 2467 | prio = min(current->normal_prio, MAX_RT_PRIO); |
| 2468 | |
| 2469 | plist_node_init(&q->list, prio); |
Pierre Peiffer | ec92d08 | 2007-05-09 02:35:00 -0700 | [diff] [blame] | 2470 | plist_add(&q->list, &hb->chain); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2471 | q->task = current; |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | /** |
| 2475 | * queue_me() - Enqueue the futex_q on the futex_hash_bucket |
| 2476 | * @q: The futex_q to enqueue |
| 2477 | * @hb: The destination hash bucket |
| 2478 | * |
| 2479 | * The hb->lock must be held by the caller, and is released here. A call to |
| 2480 | * queue_me() is typically paired with exactly one call to unqueue_me(). The |
| 2481 | * exceptions involve the PI related operations, which may use unqueue_me_pi() |
| 2482 | * or nothing if the unqueue is done as part of the wake process and the unqueue |
| 2483 | * state is implicit in the state of woken task (see futex_wait_requeue_pi() for |
| 2484 | * an example). |
| 2485 | */ |
| 2486 | static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb) |
| 2487 | __releases(&hb->lock) |
| 2488 | { |
| 2489 | __queue_me(q, hb); |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2490 | spin_unlock(&hb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2493 | /** |
| 2494 | * unqueue_me() - Remove the futex_q from its futex_hash_bucket |
| 2495 | * @q: The futex_q to unqueue |
| 2496 | * |
| 2497 | * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must |
| 2498 | * be paired with exactly one earlier call to queue_me(). |
| 2499 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2500 | * Return: |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 2501 | * - 1 - if the futex_q was still queued (and we removed unqueued it); |
| 2502 | * - 0 - if the futex_q was already removed by the waking thread |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | static int unqueue_me(struct futex_q *q) |
| 2505 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | spinlock_t *lock_ptr; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2507 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | |
| 2509 | /* In the common case we don't take the spinlock, which is nice. */ |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2510 | retry: |
Jianyu Zhan | 29b75eb | 2016-03-07 09:32:24 +0800 | [diff] [blame] | 2511 | /* |
| 2512 | * q->lock_ptr can change between this read and the following spin_lock. |
| 2513 | * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and |
| 2514 | * optimizing lock_ptr out of the logic below. |
| 2515 | */ |
| 2516 | lock_ptr = READ_ONCE(q->lock_ptr); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 2517 | if (lock_ptr != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | spin_lock(lock_ptr); |
| 2519 | /* |
| 2520 | * q->lock_ptr can change between reading it and |
| 2521 | * spin_lock(), causing us to take the wrong lock. This |
| 2522 | * corrects the race condition. |
| 2523 | * |
| 2524 | * Reasoning goes like this: if we have the wrong lock, |
| 2525 | * q->lock_ptr must have changed (maybe several times) |
| 2526 | * between reading it and the spin_lock(). It can |
| 2527 | * change again after the spin_lock() but only if it was |
| 2528 | * already changed before the spin_lock(). It cannot, |
| 2529 | * however, change back to the original value. Therefore |
| 2530 | * we can detect whether we acquired the correct lock. |
| 2531 | */ |
| 2532 | if (unlikely(lock_ptr != q->lock_ptr)) { |
| 2533 | spin_unlock(lock_ptr); |
| 2534 | goto retry; |
| 2535 | } |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 2536 | __unqueue_futex(q); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2537 | |
| 2538 | BUG_ON(q->pi_state); |
| 2539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | spin_unlock(lock_ptr); |
| 2541 | ret = 1; |
| 2542 | } |
| 2543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | return ret; |
| 2545 | } |
| 2546 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2547 | /* |
Ingo Molnar | 93d0955 | 2021-05-12 20:04:28 +0200 | [diff] [blame] | 2548 | * PI futexes can not be requeued and must remove themselves from the |
Davidlohr Bueso | a3f2428 | 2021-02-26 09:50:28 -0800 | [diff] [blame] | 2549 | * hash bucket. The hash bucket lock (i.e. lock_ptr) is held. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2550 | */ |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2551 | static void unqueue_me_pi(struct futex_q *q) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2552 | { |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 2553 | __unqueue_futex(q); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2554 | |
| 2555 | BUG_ON(!q->pi_state); |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2556 | put_pi_state(q->pi_state); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2557 | q->pi_state = NULL; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2560 | static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, |
| 2561 | struct task_struct *argowner) |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2562 | { |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2563 | struct futex_pi_state *pi_state = q->pi_state; |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2564 | struct task_struct *oldowner, *newowner; |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2565 | u32 uval, curval, newval, newtid; |
| 2566 | int err = 0; |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2567 | |
| 2568 | oldowner = pi_state->owner; |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2569 | |
| 2570 | /* |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2571 | * We are here because either: |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 2572 | * |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2573 | * - we stole the lock and pi_state->owner needs updating to reflect |
| 2574 | * that (@argowner == current), |
| 2575 | * |
| 2576 | * or: |
| 2577 | * |
| 2578 | * - someone stole our lock and we need to fix things to point to the |
| 2579 | * new owner (@argowner == NULL). |
| 2580 | * |
| 2581 | * Either way, we have to replace the TID in the user space variable. |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 2582 | * This must be atomic as we have to preserve the owner died bit here. |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2583 | * |
Darren Hart | b2d0994 | 2009-03-12 00:55:37 -0700 | [diff] [blame] | 2584 | * Note: We write the user space value _before_ changing the pi_state |
| 2585 | * because we can fault here. Imagine swapped out pages or a fork |
| 2586 | * that marked all the anonymous memory readonly for cow. |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2587 | * |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2588 | * Modifying pi_state _before_ the user space value would leave the |
| 2589 | * pi_state in an inconsistent state when we fault here, because we |
| 2590 | * need to drop the locks to handle the fault. This might be observed |
Thomas Gleixner | f6f4ec0 | 2021-08-15 23:29:07 +0200 | [diff] [blame] | 2591 | * in the PID checks when attaching to PI state . |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2592 | */ |
| 2593 | retry: |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2594 | if (!argowner) { |
| 2595 | if (oldowner != current) { |
| 2596 | /* |
| 2597 | * We raced against a concurrent self; things are |
| 2598 | * already fixed up. Nothing to do. |
| 2599 | */ |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2600 | return 0; |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) { |
Thomas Gleixner | 12bb3f7 | 2021-01-20 16:00:24 +0100 | [diff] [blame] | 2604 | /* We got the lock. pi_state is correct. Tell caller. */ |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2605 | return 1; |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | /* |
Mike Galbraith | 9f5d1c3 | 2020-11-04 16:12:44 +0100 | [diff] [blame] | 2609 | * The trylock just failed, so either there is an owner or |
| 2610 | * there is a higher priority waiter than this one. |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2611 | */ |
| 2612 | newowner = rt_mutex_owner(&pi_state->pi_mutex); |
Mike Galbraith | 9f5d1c3 | 2020-11-04 16:12:44 +0100 | [diff] [blame] | 2613 | /* |
| 2614 | * If the higher priority waiter has not yet taken over the |
| 2615 | * rtmutex then newowner is NULL. We can't return here with |
| 2616 | * that state because it's inconsistent vs. the user space |
| 2617 | * state. So drop the locks and try again. It's a valid |
| 2618 | * situation and not any different from the other retry |
| 2619 | * conditions. |
| 2620 | */ |
| 2621 | if (unlikely(!newowner)) { |
| 2622 | err = -EAGAIN; |
| 2623 | goto handle_err; |
| 2624 | } |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2625 | } else { |
| 2626 | WARN_ON_ONCE(argowner != current); |
| 2627 | if (oldowner == current) { |
| 2628 | /* |
| 2629 | * We raced against a concurrent self; things are |
| 2630 | * already fixed up. Nothing to do. |
| 2631 | */ |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2632 | return 1; |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2633 | } |
| 2634 | newowner = argowner; |
| 2635 | } |
| 2636 | |
| 2637 | newtid = task_pid_vnr(newowner) | FUTEX_WAITERS; |
Peter Zijlstra | a97cb0e | 2018-01-22 11:39:47 +0100 | [diff] [blame] | 2638 | /* Owner died? */ |
| 2639 | if (!pi_state->owner) |
| 2640 | newtid |= FUTEX_OWNER_DIED; |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2641 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2642 | err = get_futex_value_locked(&uval, uaddr); |
| 2643 | if (err) |
| 2644 | goto handle_err; |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2645 | |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 2646 | for (;;) { |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2647 | newval = (uval & FUTEX_OWNER_DIED) | newtid; |
| 2648 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2649 | err = cmpxchg_futex_value_locked(&curval, uaddr, uval, newval); |
| 2650 | if (err) |
| 2651 | goto handle_err; |
| 2652 | |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2653 | if (curval == uval) |
| 2654 | break; |
| 2655 | uval = curval; |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * We fixed up user space. Now we need to fix the pi_state |
| 2660 | * itself. |
| 2661 | */ |
Thomas Gleixner | c5cade2 | 2021-01-19 15:21:35 +0100 | [diff] [blame] | 2662 | pi_state_update_owner(pi_state, newowner); |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2663 | |
Thomas Gleixner | 12bb3f7 | 2021-01-20 16:00:24 +0100 | [diff] [blame] | 2664 | return argowner == current; |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2665 | |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2666 | /* |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2667 | * In order to reschedule or handle a page fault, we need to drop the |
| 2668 | * locks here. In the case of a fault, this gives the other task |
| 2669 | * (either the highest priority waiter itself or the task which stole |
| 2670 | * the rtmutex) the chance to try the fixup of the pi_state. So once we |
| 2671 | * are back from handling the fault we need to check the pi_state after |
| 2672 | * reacquiring the locks and before trying to do another fixup. When |
| 2673 | * the fixup has been done already we simply return. |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2674 | * |
| 2675 | * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely |
| 2676 | * drop hb->lock since the caller owns the hb -> futex_q relation. |
| 2677 | * Dropping the pi_mutex->wait_lock requires the state revalidate. |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2678 | */ |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2679 | handle_err: |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2680 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2681 | spin_unlock(q->lock_ptr); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2682 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2683 | switch (err) { |
| 2684 | case -EFAULT: |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2685 | err = fault_in_user_writeable(uaddr); |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2686 | break; |
| 2687 | |
| 2688 | case -EAGAIN: |
| 2689 | cond_resched(); |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2690 | err = 0; |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2691 | break; |
| 2692 | |
| 2693 | default: |
| 2694 | WARN_ON_ONCE(1); |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 2695 | break; |
| 2696 | } |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2697 | |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2698 | spin_lock(q->lock_ptr); |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2699 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2700 | |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2701 | /* |
| 2702 | * Check if someone else fixed it for us: |
| 2703 | */ |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2704 | if (pi_state->owner != oldowner) |
| 2705 | return argowner == current; |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2706 | |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2707 | /* Retry if err was -EAGAIN or the fault in succeeded */ |
| 2708 | if (!err) |
| 2709 | goto retry; |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2710 | |
Thomas Gleixner | 34b1a1c | 2021-01-18 19:01:21 +0100 | [diff] [blame] | 2711 | /* |
| 2712 | * fault_in_user_writeable() failed so user state is immutable. At |
| 2713 | * best we can make the kernel state consistent but user state will |
| 2714 | * be most likely hosed and any subsequent unlock operation will be |
| 2715 | * rejected due to PI futex rule [10]. |
| 2716 | * |
| 2717 | * Ensure that the rtmutex owner is also the pi_state owner despite |
| 2718 | * the user space value claiming something different. There is no |
| 2719 | * point in unlocking the rtmutex if current is the owner as it |
| 2720 | * would need to wait until the next waiter has taken the rtmutex |
| 2721 | * to guarantee consistent state. Keep it simple. Userspace asked |
| 2722 | * for this wreckaged state. |
| 2723 | * |
| 2724 | * The rtmutex has an owner - either current or some other |
| 2725 | * task. See the EAGAIN loop above. |
| 2726 | */ |
| 2727 | pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex)); |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2728 | |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2729 | return err; |
| 2730 | } |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2731 | |
Thomas Gleixner | f2dac39 | 2021-01-19 16:26:38 +0100 | [diff] [blame] | 2732 | static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, |
| 2733 | struct task_struct *argowner) |
| 2734 | { |
| 2735 | struct futex_pi_state *pi_state = q->pi_state; |
| 2736 | int ret; |
| 2737 | |
| 2738 | lockdep_assert_held(q->lock_ptr); |
| 2739 | |
| 2740 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 2741 | ret = __fixup_pi_state_owner(uaddr, q, argowner); |
Peter Zijlstra | 734009e | 2017-03-22 11:35:52 +0100 | [diff] [blame] | 2742 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 2743 | return ret; |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2746 | static long futex_wait_restart(struct restart_block *restart); |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 2747 | |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2748 | /** |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2749 | * fixup_owner() - Post lock pi_state and corner case management |
| 2750 | * @uaddr: user address of the futex |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2751 | * @q: futex_q (contains pi_state and access to the rt_mutex) |
| 2752 | * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0) |
| 2753 | * |
| 2754 | * After attempting to lock an rt_mutex, this function is called to cleanup |
| 2755 | * the pi_state owner as well as handle race conditions that may allow us to |
| 2756 | * acquire the lock. Must be called with the hb lock held. |
| 2757 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2758 | * Return: |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 2759 | * - 1 - success, lock taken; |
| 2760 | * - 0 - success, lock not taken; |
| 2761 | * - <0 - on error (-EFAULT) |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2762 | */ |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2763 | static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked) |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2764 | { |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2765 | if (locked) { |
| 2766 | /* |
| 2767 | * Got the lock. We might not be the anticipated owner if we |
| 2768 | * did a lock-steal - fix up the PI-state in that case: |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 2769 | * |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2770 | * Speculative pi_state->owner read (we don't hold wait_lock); |
| 2771 | * since we own the lock pi_state->owner == current is the |
| 2772 | * stable state, anything else needs more attention. |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2773 | */ |
| 2774 | if (q->pi_state->owner != current) |
Thomas Gleixner | 12bb3f7 | 2021-01-20 16:00:24 +0100 | [diff] [blame] | 2775 | return fixup_pi_state_owner(uaddr, q, current); |
| 2776 | return 1; |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | /* |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2780 | * If we didn't get the lock; check if anybody stole it from us. In |
| 2781 | * that case, we need to fix up the uval to point to them instead of |
| 2782 | * us, otherwise bad things happen. [10] |
| 2783 | * |
| 2784 | * Another speculative read; pi_state->owner == current is unstable |
| 2785 | * but needs our attention. |
| 2786 | */ |
Thomas Gleixner | 12bb3f7 | 2021-01-20 16:00:24 +0100 | [diff] [blame] | 2787 | if (q->pi_state->owner == current) |
| 2788 | return fixup_pi_state_owner(uaddr, q, NULL); |
Peter Zijlstra | c1e2f0e | 2017-12-08 13:49:39 +0100 | [diff] [blame] | 2789 | |
| 2790 | /* |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2791 | * Paranoia check. If we did not take the lock, then we should not be |
Thomas Gleixner | 04b79c5 | 2021-01-19 16:06:10 +0100 | [diff] [blame] | 2792 | * the owner of the rt_mutex. Warn and establish consistent state. |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2793 | */ |
Thomas Gleixner | 04b79c5 | 2021-01-19 16:06:10 +0100 | [diff] [blame] | 2794 | if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current)) |
| 2795 | return fixup_pi_state_owner(uaddr, q, current); |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2796 | |
Thomas Gleixner | 12bb3f7 | 2021-01-20 16:00:24 +0100 | [diff] [blame] | 2797 | return 0; |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | /** |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2801 | * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal |
| 2802 | * @hb: the futex hash bucket, must be locked by the caller |
| 2803 | * @q: the futex_q to queue up on |
| 2804 | * @timeout: the prepared hrtimer_sleeper, or null for no timeout |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2805 | */ |
| 2806 | static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q, |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 2807 | struct hrtimer_sleeper *timeout) |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2808 | { |
Darren Hart | 9beba3c | 2009-09-24 11:54:47 -0700 | [diff] [blame] | 2809 | /* |
| 2810 | * The task state is guaranteed to be set before another task can |
Peter Zijlstra | b92b8b3 | 2015-05-12 10:51:55 +0200 | [diff] [blame] | 2811 | * wake it. set_current_state() is implemented using smp_store_mb() and |
Darren Hart | 9beba3c | 2009-09-24 11:54:47 -0700 | [diff] [blame] | 2812 | * queue_me() calls spin_unlock() upon completion, both serializing |
| 2813 | * access to the hash list and forcing another memory barrier. |
| 2814 | */ |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 2815 | set_current_state(TASK_INTERRUPTIBLE); |
Darren Hart | 0729e19 | 2009-09-21 22:30:38 -0700 | [diff] [blame] | 2816 | queue_me(q, hb); |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2817 | |
| 2818 | /* Arm the timer */ |
Thomas Gleixner | 2e4b0d3 | 2015-04-14 21:09:13 +0000 | [diff] [blame] | 2819 | if (timeout) |
Thomas Gleixner | 9dd8813 | 2019-07-30 21:16:55 +0200 | [diff] [blame] | 2820 | hrtimer_sleeper_start_expires(timeout, HRTIMER_MODE_ABS); |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2821 | |
| 2822 | /* |
Darren Hart | 0729e19 | 2009-09-21 22:30:38 -0700 | [diff] [blame] | 2823 | * If we have been removed from the hash list, then another task |
| 2824 | * has tried to wake us, and we can skip the call to schedule(). |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2825 | */ |
| 2826 | if (likely(!plist_node_empty(&q->list))) { |
| 2827 | /* |
| 2828 | * If the timer has already expired, current will already be |
| 2829 | * flagged for rescheduling. Only call schedule if there |
| 2830 | * is no timeout, or if it has yet to expire. |
| 2831 | */ |
| 2832 | if (!timeout || timeout->task) |
Colin Cross | 88c8004 | 2013-05-01 18:35:05 -0700 | [diff] [blame] | 2833 | freezable_schedule(); |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2834 | } |
| 2835 | __set_current_state(TASK_RUNNING); |
| 2836 | } |
| 2837 | |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2838 | /** |
| 2839 | * futex_wait_setup() - Prepare to wait on a futex |
| 2840 | * @uaddr: the futex userspace address |
| 2841 | * @val: the expected value |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2842 | * @flags: futex flags (FLAGS_SHARED, etc.) |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2843 | * @q: the associated futex_q |
| 2844 | * @hb: storage for hash_bucket pointer to be returned to caller |
| 2845 | * |
| 2846 | * Setup the futex_q and locate the hash_bucket. Get the futex value and |
| 2847 | * compare it with the expected value. Handle atomic faults internally. |
Thomas Gleixner | c363b7e | 2021-08-15 23:29:06 +0200 | [diff] [blame] | 2848 | * Return with the hb lock held on success, and unlocked on failure. |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2849 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2850 | * Return: |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 2851 | * - 0 - uaddr contains val and hb has been locked; |
| 2852 | * - <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2853 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2854 | static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags, |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2855 | struct futex_q *q, struct futex_hash_bucket **hb) |
| 2856 | { |
| 2857 | u32 uval; |
| 2858 | int ret; |
| 2859 | |
| 2860 | /* |
| 2861 | * Access the page AFTER the hash-bucket is locked. |
| 2862 | * Order is important: |
| 2863 | * |
| 2864 | * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val); |
| 2865 | * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); } |
| 2866 | * |
| 2867 | * The basic logical guarantee of a futex is that it blocks ONLY |
| 2868 | * if cond(var) is known to be true at the time of blocking, for |
Michel Lespinasse | 8fe8f54 | 2011-03-06 18:07:50 -0800 | [diff] [blame] | 2869 | * any cond. If we locked the hash-bucket after testing *uaddr, that |
| 2870 | * would open a race condition where we could block indefinitely with |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2871 | * cond(var) false, which would violate the guarantee. |
| 2872 | * |
Michel Lespinasse | 8fe8f54 | 2011-03-06 18:07:50 -0800 | [diff] [blame] | 2873 | * On the other hand, we insert q and release the hash-bucket only |
| 2874 | * after testing *uaddr. This guarantees that futex_wait() will NOT |
| 2875 | * absorb a wakeup if *uaddr does not match the desired values |
| 2876 | * while the syscall executes. |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2877 | */ |
| 2878 | retry: |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 2879 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, FUTEX_READ); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2880 | if (unlikely(ret != 0)) |
Darren Hart | a5a2a0c | 2009-04-10 09:50:05 -0700 | [diff] [blame] | 2881 | return ret; |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2882 | |
| 2883 | retry_private: |
| 2884 | *hb = queue_lock(q); |
| 2885 | |
| 2886 | ret = get_futex_value_locked(&uval, uaddr); |
| 2887 | |
| 2888 | if (ret) { |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2889 | queue_unlock(*hb); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2890 | |
| 2891 | ret = get_user(uval, uaddr); |
| 2892 | if (ret) |
André Almeida | d7c5ed7 | 2020-07-02 17:28:41 -0300 | [diff] [blame] | 2893 | return ret; |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2894 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2895 | if (!(flags & FLAGS_SHARED)) |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2896 | goto retry_private; |
| 2897 | |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2898 | goto retry; |
| 2899 | } |
| 2900 | |
| 2901 | if (uval != val) { |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2902 | queue_unlock(*hb); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2903 | ret = -EWOULDBLOCK; |
| 2904 | } |
| 2905 | |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2906 | return ret; |
| 2907 | } |
| 2908 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2909 | static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val, |
| 2910 | ktime_t *abs_time, u32 bitset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | { |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 2912 | struct hrtimer_sleeper timeout, *to; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2913 | struct restart_block *restart; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2914 | struct futex_hash_bucket *hb; |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 2915 | struct futex_q q = futex_q_init; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2916 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2917 | |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 2918 | if (!bitset) |
| 2919 | return -EINVAL; |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 2920 | q.bitset = bitset; |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2921 | |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 2922 | to = futex_setup_timer(abs_time, &timeout, flags, |
| 2923 | current->timer_slack_ns); |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2924 | retry: |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2925 | /* |
Thomas Gleixner | c363b7e | 2021-08-15 23:29:06 +0200 | [diff] [blame] | 2926 | * Prepare to wait on uaddr. On success, it holds hb->lock and q |
| 2927 | * is initialized. |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2928 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2929 | ret = futex_wait_setup(uaddr, val, flags, &q, &hb); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2930 | if (ret) |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2931 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2933 | /* queue_me and wait for wakeup, timeout, or a signal. */ |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 2934 | futex_wait_queue_me(hb, &q, to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | |
| 2936 | /* If we were woken (and unqueued), we succeeded, whatever. */ |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2937 | ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2938 | if (!unqueue_me(&q)) |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2939 | goto out; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2940 | ret = -ETIMEDOUT; |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2941 | if (to && !to->task) |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2942 | goto out; |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2943 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2944 | /* |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2945 | * We expect signal_pending(current), but we might be the |
| 2946 | * victim of a spurious wakeup as well. |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2947 | */ |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2948 | if (!signal_pending(current)) |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2949 | goto retry; |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2950 | |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2951 | ret = -ERESTARTSYS; |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2952 | if (!abs_time) |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2953 | goto out; |
Steven Rostedt | ce6bd42 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 2954 | |
Andy Lutomirski | f56141e | 2015-02-12 15:01:14 -0800 | [diff] [blame] | 2955 | restart = ¤t->restart_block; |
Namhyung Kim | a3c74c5 | 2010-09-14 21:43:47 +0900 | [diff] [blame] | 2956 | restart->futex.uaddr = uaddr; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2957 | restart->futex.val = val; |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 2958 | restart->futex.time = *abs_time; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2959 | restart->futex.bitset = bitset; |
Darren Hart | 0cd9c64 | 2011-04-14 15:41:57 -0700 | [diff] [blame] | 2960 | restart->futex.flags = flags | FLAGS_HAS_TIMEOUT; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2961 | |
Oleg Nesterov | 5abbe51 | 2021-02-01 18:46:41 +0100 | [diff] [blame] | 2962 | ret = set_restart_fn(restart, futex_wait_restart); |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2963 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2964 | out: |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2965 | if (to) { |
| 2966 | hrtimer_cancel(&to->timer); |
| 2967 | destroy_hrtimer_on_stack(&to->timer); |
| 2968 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2969 | return ret; |
| 2970 | } |
| 2971 | |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2972 | |
| 2973 | static long futex_wait_restart(struct restart_block *restart) |
| 2974 | { |
Namhyung Kim | a3c74c5 | 2010-09-14 21:43:47 +0900 | [diff] [blame] | 2975 | u32 __user *uaddr = restart->futex.uaddr; |
Darren Hart | a72188d | 2009-04-03 13:40:22 -0700 | [diff] [blame] | 2976 | ktime_t t, *tp = NULL; |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2977 | |
Darren Hart | a72188d | 2009-04-03 13:40:22 -0700 | [diff] [blame] | 2978 | if (restart->futex.flags & FLAGS_HAS_TIMEOUT) { |
Thomas Gleixner | 2456e85 | 2016-12-25 11:38:40 +0100 | [diff] [blame] | 2979 | t = restart->futex.time; |
Darren Hart | a72188d | 2009-04-03 13:40:22 -0700 | [diff] [blame] | 2980 | tp = &t; |
| 2981 | } |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2982 | restart->fn = do_no_restart_syscall; |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2983 | |
| 2984 | return (long)futex_wait(uaddr, restart->futex.flags, |
| 2985 | restart->futex.val, tp, restart->futex.bitset); |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2989 | /* |
| 2990 | * Userspace tried a 0 -> TID atomic transition of the futex value |
| 2991 | * and failed. The kernel side here does the whole locking operation: |
Davidlohr Bueso | 767f509 | 2015-06-29 23:26:01 -0700 | [diff] [blame] | 2992 | * if there are waiters then it will block as a consequence of relying |
| 2993 | * on rt-mutexes, it does PI, etc. (Due to races the kernel might see |
| 2994 | * a 0 value of the futex too.). |
| 2995 | * |
| 2996 | * Also serves as futex trylock_pi()'ing, and due semantics. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2997 | */ |
Michael Kerrisk | 996636d | 2015-01-16 20:28:06 +0100 | [diff] [blame] | 2998 | static int futex_lock_pi(u32 __user *uaddr, unsigned int flags, |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2999 | ktime_t *time, int trylock) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3000 | { |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 3001 | struct hrtimer_sleeper timeout, *to; |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 3002 | struct task_struct *exiting = NULL; |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3003 | struct rt_mutex_waiter rt_waiter; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3004 | struct futex_hash_bucket *hb; |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 3005 | struct futex_q q = futex_q_init; |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 3006 | int res, ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3007 | |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 3008 | if (!IS_ENABLED(CONFIG_FUTEX_PI)) |
| 3009 | return -ENOSYS; |
| 3010 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3011 | if (refill_pi_state_cache()) |
| 3012 | return -ENOMEM; |
| 3013 | |
Thomas Gleixner | e112c41 | 2021-04-22 21:44:22 +0200 | [diff] [blame] | 3014 | to = futex_setup_timer(time, &timeout, flags, 0); |
Thomas Gleixner | c5780e9 | 2006-09-08 09:47:15 -0700 | [diff] [blame] | 3015 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 3016 | retry: |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 3017 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, FUTEX_WRITE); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3018 | if (unlikely(ret != 0)) |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 3019 | goto out; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3020 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 3021 | retry_private: |
Eric Sesterhenn | 82af7ac | 2008-01-25 10:40:46 +0100 | [diff] [blame] | 3022 | hb = queue_lock(&q); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3023 | |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 3024 | ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, |
| 3025 | &exiting, 0); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3026 | if (unlikely(ret)) { |
Davidlohr Bueso | 767f509 | 2015-06-29 23:26:01 -0700 | [diff] [blame] | 3027 | /* |
| 3028 | * Atomic work succeeded and we got the lock, |
| 3029 | * or failed. Either way, we do _not_ block. |
| 3030 | */ |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3031 | switch (ret) { |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 3032 | case 1: |
| 3033 | /* We got the lock. */ |
| 3034 | ret = 0; |
| 3035 | goto out_unlock_put_key; |
| 3036 | case -EFAULT: |
| 3037 | goto uaddr_faulted; |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 3038 | case -EBUSY: |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3039 | case -EAGAIN: |
| 3040 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 3041 | * Two reasons for this: |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 3042 | * - EBUSY: Task is exiting and we just wait for the |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 3043 | * exit to complete. |
Thomas Gleixner | ac31c7f | 2019-11-06 22:55:45 +0100 | [diff] [blame] | 3044 | * - EAGAIN: The user space value changed. |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3045 | */ |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 3046 | queue_unlock(hb); |
Thomas Gleixner | 3ef240e | 2019-11-06 22:55:46 +0100 | [diff] [blame] | 3047 | /* |
| 3048 | * Handle the case where the owner is in the middle of |
| 3049 | * exiting. Wait for the exit to complete otherwise |
| 3050 | * this task might loop forever, aka. live lock. |
| 3051 | */ |
| 3052 | wait_for_owner_exiting(ret, exiting); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3053 | cond_resched(); |
| 3054 | goto retry; |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3055 | default: |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 3056 | goto out_unlock_put_key; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3057 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3058 | } |
| 3059 | |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3060 | WARN_ON(!q.pi_state); |
| 3061 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3062 | /* |
| 3063 | * Only actually queue now that the atomic ops are done: |
| 3064 | */ |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3065 | __queue_me(&q, hb); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3066 | |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3067 | if (trylock) { |
Peter Zijlstra | 5293c2e | 2017-03-22 11:35:51 +0100 | [diff] [blame] | 3068 | ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3069 | /* Fixup the trylock return value: */ |
| 3070 | ret = ret ? 0 : -EWOULDBLOCK; |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3071 | goto no_block; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3072 | } |
| 3073 | |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3074 | rt_mutex_init_waiter(&rt_waiter); |
Peter Zijlstra | 56222b2 | 2017-03-22 11:36:00 +0100 | [diff] [blame] | 3075 | |
| 3076 | /* |
| 3077 | * On PREEMPT_RT_FULL, when hb->lock becomes an rt_mutex, we must not |
| 3078 | * hold it while doing rt_mutex_start_proxy(), because then it will |
| 3079 | * include hb->lock in the blocking chain, even through we'll not in |
| 3080 | * fact hold it while blocking. This will lead it to report -EDEADLK |
| 3081 | * and BUG when futex_unlock_pi() interleaves with this. |
| 3082 | * |
| 3083 | * Therefore acquire wait_lock while holding hb->lock, but drop the |
Thomas Gleixner | 1a1fb98 | 2019-01-29 23:15:12 +0100 | [diff] [blame] | 3084 | * latter before calling __rt_mutex_start_proxy_lock(). This |
| 3085 | * interleaves with futex_unlock_pi() -- which does a similar lock |
| 3086 | * handoff -- such that the latter can observe the futex_q::pi_state |
| 3087 | * before __rt_mutex_start_proxy_lock() is done. |
Peter Zijlstra | 56222b2 | 2017-03-22 11:36:00 +0100 | [diff] [blame] | 3088 | */ |
| 3089 | raw_spin_lock_irq(&q.pi_state->pi_mutex.wait_lock); |
| 3090 | spin_unlock(q.lock_ptr); |
Thomas Gleixner | 1a1fb98 | 2019-01-29 23:15:12 +0100 | [diff] [blame] | 3091 | /* |
| 3092 | * __rt_mutex_start_proxy_lock() unconditionally enqueues the @rt_waiter |
| 3093 | * such that futex_unlock_pi() is guaranteed to observe the waiter when |
| 3094 | * it sees the futex_q::pi_state. |
| 3095 | */ |
Peter Zijlstra | 56222b2 | 2017-03-22 11:36:00 +0100 | [diff] [blame] | 3096 | ret = __rt_mutex_start_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter, current); |
| 3097 | raw_spin_unlock_irq(&q.pi_state->pi_mutex.wait_lock); |
| 3098 | |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3099 | if (ret) { |
| 3100 | if (ret == 1) |
| 3101 | ret = 0; |
Thomas Gleixner | 1a1fb98 | 2019-01-29 23:15:12 +0100 | [diff] [blame] | 3102 | goto cleanup; |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3103 | } |
| 3104 | |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3105 | if (unlikely(to)) |
Thomas Gleixner | 9dd8813 | 2019-07-30 21:16:55 +0200 | [diff] [blame] | 3106 | hrtimer_sleeper_start_expires(to, HRTIMER_MODE_ABS); |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3107 | |
| 3108 | ret = rt_mutex_wait_proxy_lock(&q.pi_state->pi_mutex, to, &rt_waiter); |
| 3109 | |
Thomas Gleixner | 1a1fb98 | 2019-01-29 23:15:12 +0100 | [diff] [blame] | 3110 | cleanup: |
Vernon Mauery | a99e4e4 | 2006-07-01 04:35:42 -0700 | [diff] [blame] | 3111 | spin_lock(q.lock_ptr); |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 3112 | /* |
Thomas Gleixner | 1a1fb98 | 2019-01-29 23:15:12 +0100 | [diff] [blame] | 3113 | * If we failed to acquire the lock (deadlock/signal/timeout), we must |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3114 | * first acquire the hb->lock before removing the lock from the |
Thomas Gleixner | 1a1fb98 | 2019-01-29 23:15:12 +0100 | [diff] [blame] | 3115 | * rt_mutex waitqueue, such that we can keep the hb and rt_mutex wait |
| 3116 | * lists consistent. |
Peter Zijlstra | 56222b2 | 2017-03-22 11:36:00 +0100 | [diff] [blame] | 3117 | * |
| 3118 | * In particular; it is important that futex_unlock_pi() can not |
| 3119 | * observe this inconsistency. |
Peter Zijlstra | cfafcd1 | 2017-03-22 11:35:58 +0100 | [diff] [blame] | 3120 | */ |
| 3121 | if (ret && !rt_mutex_cleanup_proxy_lock(&q.pi_state->pi_mutex, &rt_waiter)) |
| 3122 | ret = 0; |
| 3123 | |
| 3124 | no_block: |
| 3125 | /* |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 3126 | * Fixup the pi_state owner and possibly acquire the lock if we |
| 3127 | * haven't already. |
| 3128 | */ |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3129 | res = fixup_owner(uaddr, &q, !ret); |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 3130 | /* |
Ingo Molnar | 93d0955 | 2021-05-12 20:04:28 +0200 | [diff] [blame] | 3131 | * If fixup_owner() returned an error, propagate that. If it acquired |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 3132 | * the lock, clear our -ETIMEDOUT or -EINTR. |
| 3133 | */ |
| 3134 | if (res) |
| 3135 | ret = (res < 0) ? res : 0; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3136 | |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3137 | unqueue_me_pi(&q); |
Davidlohr Bueso | a3f2428 | 2021-02-26 09:50:28 -0800 | [diff] [blame] | 3138 | spin_unlock(q.lock_ptr); |
André Almeida | 9180bd4 | 2020-07-02 17:28:40 -0300 | [diff] [blame] | 3139 | goto out; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3140 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 3141 | out_unlock_put_key: |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 3142 | queue_unlock(hb); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3143 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 3144 | out: |
Thomas Gleixner | 97181f9 | 2017-04-10 18:03:36 +0200 | [diff] [blame] | 3145 | if (to) { |
| 3146 | hrtimer_cancel(&to->timer); |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 3147 | destroy_hrtimer_on_stack(&to->timer); |
Thomas Gleixner | 97181f9 | 2017-04-10 18:03:36 +0200 | [diff] [blame] | 3148 | } |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 3149 | return ret != -EINTR ? ret : -ERESTARTNOINTR; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3150 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 3151 | uaddr_faulted: |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 3152 | queue_unlock(hb); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3153 | |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 3154 | ret = fault_in_user_writeable(uaddr); |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 3155 | if (ret) |
André Almeida | 9180bd4 | 2020-07-02 17:28:40 -0300 | [diff] [blame] | 3156 | goto out; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3157 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3158 | if (!(flags & FLAGS_SHARED)) |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 3159 | goto retry_private; |
| 3160 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 3161 | goto retry; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
| 3164 | /* |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3165 | * Userspace attempted a TID -> 0 atomic transition, and failed. |
| 3166 | * This is the in-kernel slowpath: we look up the PI state (if any), |
| 3167 | * and do the rt-mutex unlock. |
| 3168 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3169 | static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3170 | { |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 3171 | u32 curval, uval, vpid = task_pid_vnr(current); |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 3172 | union futex_key key = FUTEX_KEY_INIT; |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3173 | struct futex_hash_bucket *hb; |
Peter Zijlstra | 499f5ac | 2017-03-22 11:35:48 +0100 | [diff] [blame] | 3174 | struct futex_q *top_waiter; |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 3175 | int ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3176 | |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 3177 | if (!IS_ENABLED(CONFIG_FUTEX_PI)) |
| 3178 | return -ENOSYS; |
| 3179 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3180 | retry: |
| 3181 | if (get_user(uval, uaddr)) |
| 3182 | return -EFAULT; |
| 3183 | /* |
| 3184 | * We release only a lock we actually own: |
| 3185 | */ |
Thomas Gleixner | c0c9ed1 | 2011-03-11 11:51:22 +0100 | [diff] [blame] | 3186 | if ((uval & FUTEX_TID_MASK) != vpid) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3187 | return -EPERM; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3188 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 3189 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, FUTEX_WRITE); |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3190 | if (ret) |
| 3191 | return ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3192 | |
| 3193 | hb = hash_futex(&key); |
| 3194 | spin_lock(&hb->lock); |
| 3195 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3196 | /* |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3197 | * Check waiters first. We do not trust user space values at |
| 3198 | * all and we at least want to know if user space fiddled |
| 3199 | * with the futex value instead of blindly unlocking. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3200 | */ |
Peter Zijlstra | 499f5ac | 2017-03-22 11:35:48 +0100 | [diff] [blame] | 3201 | top_waiter = futex_top_waiter(hb, &key); |
| 3202 | if (top_waiter) { |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 3203 | struct futex_pi_state *pi_state = top_waiter->pi_state; |
| 3204 | |
| 3205 | ret = -EINVAL; |
| 3206 | if (!pi_state) |
| 3207 | goto out_unlock; |
| 3208 | |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3209 | /* |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 3210 | * If current does not own the pi_state then the futex is |
| 3211 | * inconsistent and user space fiddled with the futex value. |
| 3212 | */ |
| 3213 | if (pi_state->owner != current) |
| 3214 | goto out_unlock; |
| 3215 | |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 3216 | get_pi_state(pi_state); |
Peter Zijlstra | bebe5b5 | 2017-03-22 11:35:59 +0100 | [diff] [blame] | 3217 | /* |
Peter Zijlstra | bebe5b5 | 2017-03-22 11:35:59 +0100 | [diff] [blame] | 3218 | * By taking wait_lock while still holding hb->lock, we ensure |
| 3219 | * there is no point where we hold neither; and therefore |
| 3220 | * wake_futex_pi() must observe a state consistent with what we |
| 3221 | * observed. |
Thomas Gleixner | 1a1fb98 | 2019-01-29 23:15:12 +0100 | [diff] [blame] | 3222 | * |
| 3223 | * In particular; this forces __rt_mutex_start_proxy() to |
| 3224 | * complete such that we're guaranteed to observe the |
| 3225 | * rt_waiter. Also see the WARN in wake_futex_pi(). |
Peter Zijlstra | bebe5b5 | 2017-03-22 11:35:59 +0100 | [diff] [blame] | 3226 | */ |
| 3227 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 3228 | spin_unlock(&hb->lock); |
| 3229 | |
Peter Zijlstra | c74aef2 | 2017-09-22 17:48:06 +0200 | [diff] [blame] | 3230 | /* drops pi_state->pi_mutex.wait_lock */ |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 3231 | ret = wake_futex_pi(uaddr, uval, pi_state); |
| 3232 | |
| 3233 | put_pi_state(pi_state); |
| 3234 | |
| 3235 | /* |
| 3236 | * Success, we're done! No tricky corner cases. |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3237 | */ |
| 3238 | if (!ret) |
Jangwoong Kim | 0f943850 | 2020-12-30 21:29:53 +0900 | [diff] [blame] | 3239 | return ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3240 | /* |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3241 | * The atomic access to the futex value generated a |
| 3242 | * pagefault, so retry the user-access and the wakeup: |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3243 | */ |
| 3244 | if (ret == -EFAULT) |
| 3245 | goto pi_faulted; |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3246 | /* |
Sebastian Andrzej Siewior | 89e9e66 | 2016-04-15 14:35:39 +0200 | [diff] [blame] | 3247 | * A unconditional UNLOCK_PI op raced against a waiter |
| 3248 | * setting the FUTEX_WAITERS bit. Try again. |
| 3249 | */ |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3250 | if (ret == -EAGAIN) |
| 3251 | goto pi_retry; |
Sebastian Andrzej Siewior | 89e9e66 | 2016-04-15 14:35:39 +0200 | [diff] [blame] | 3252 | /* |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3253 | * wake_futex_pi has detected invalid state. Tell user |
| 3254 | * space. |
| 3255 | */ |
Jangwoong Kim | 0f943850 | 2020-12-30 21:29:53 +0900 | [diff] [blame] | 3256 | return ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3257 | } |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3258 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3259 | /* |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3260 | * We have no kernel internal state, i.e. no waiters in the |
| 3261 | * kernel. Waiters which are about to queue themselves are stuck |
| 3262 | * on hb->lock. So we can safely ignore them. We do neither |
| 3263 | * preserve the WAITERS bit not the OWNER_DIED one. We are the |
| 3264 | * owner. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3265 | */ |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3266 | if ((ret = cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))) { |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 3267 | spin_unlock(&hb->lock); |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3268 | switch (ret) { |
| 3269 | case -EFAULT: |
| 3270 | goto pi_faulted; |
| 3271 | |
| 3272 | case -EAGAIN: |
| 3273 | goto pi_retry; |
| 3274 | |
| 3275 | default: |
| 3276 | WARN_ON_ONCE(1); |
Jangwoong Kim | 0f943850 | 2020-12-30 21:29:53 +0900 | [diff] [blame] | 3277 | return ret; |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3278 | } |
Peter Zijlstra | 16ffa12 | 2017-03-22 11:35:55 +0100 | [diff] [blame] | 3279 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3280 | |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3281 | /* |
| 3282 | * If uval has changed, let user space handle it. |
| 3283 | */ |
| 3284 | ret = (curval == uval) ? 0 : -EAGAIN; |
| 3285 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3286 | out_unlock: |
| 3287 | spin_unlock(&hb->lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3288 | return ret; |
| 3289 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3290 | pi_retry: |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3291 | cond_resched(); |
| 3292 | goto retry; |
| 3293 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3294 | pi_faulted: |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3295 | |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 3296 | ret = fault_in_user_writeable(uaddr); |
Darren Hart | b568636 | 2008-12-18 15:06:34 -0800 | [diff] [blame] | 3297 | if (!ret) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3298 | goto retry; |
| 3299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | return ret; |
| 3301 | } |
| 3302 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3303 | /** |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3304 | * handle_early_requeue_pi_wakeup() - Handle early wakeup on the initial futex |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3305 | * @hb: the hash_bucket futex_q was original enqueued on |
| 3306 | * @q: the futex_q woken while waiting to be requeued |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3307 | * @timeout: the timeout associated with the wait (NULL if none) |
| 3308 | * |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3309 | * Determine the cause for the early wakeup. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3310 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 3311 | * Return: |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3312 | * -EWOULDBLOCK or -ETIMEDOUT or -ERESTARTNOINTR |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3313 | */ |
| 3314 | static inline |
| 3315 | int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb, |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3316 | struct futex_q *q, |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3317 | struct hrtimer_sleeper *timeout) |
| 3318 | { |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3319 | int ret; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3320 | |
| 3321 | /* |
| 3322 | * With the hb lock held, we avoid races while we process the wakeup. |
| 3323 | * We only need to hold hb (and not hb2) to ensure atomicity as the |
| 3324 | * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb. |
| 3325 | * It can't be requeued from uaddr2 to something else since we don't |
| 3326 | * support a PI aware source futex for requeue. |
| 3327 | */ |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3328 | WARN_ON_ONCE(&hb->lock != q->lock_ptr); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3329 | |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3330 | /* |
| 3331 | * We were woken prior to requeue by a timeout or a signal. |
| 3332 | * Unqueue the futex_q and determine which it was. |
| 3333 | */ |
| 3334 | plist_del(&q->list, &hb->chain); |
| 3335 | hb_waiters_dec(hb); |
| 3336 | |
| 3337 | /* Handle spurious wakeups gracefully */ |
| 3338 | ret = -EWOULDBLOCK; |
| 3339 | if (timeout && !timeout->task) |
| 3340 | ret = -ETIMEDOUT; |
| 3341 | else if (signal_pending(current)) |
| 3342 | ret = -ERESTARTNOINTR; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3343 | return ret; |
| 3344 | } |
| 3345 | |
| 3346 | /** |
| 3347 | * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2 |
Darren Hart | 56ec160 | 2009-09-21 22:29:59 -0700 | [diff] [blame] | 3348 | * @uaddr: the futex we initially wait on (non-pi) |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3349 | * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 3350 | * the same type, no requeueing from private to shared, etc. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3351 | * @val: the expected value of uaddr |
| 3352 | * @abs_time: absolute timeout |
Darren Hart | 56ec160 | 2009-09-21 22:29:59 -0700 | [diff] [blame] | 3353 | * @bitset: 32 bit wakeup bitset set by userspace, defaults to all |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3354 | * @uaddr2: the pi futex we will take prior to returning to user-space |
| 3355 | * |
| 3356 | * The caller will wait on uaddr and will be requeued by futex_requeue() to |
Darren Hart | 6f7b0a2 | 2012-07-20 11:53:31 -0700 | [diff] [blame] | 3357 | * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake |
| 3358 | * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to |
| 3359 | * userspace. This ensures the rt_mutex maintains an owner when it has waiters; |
| 3360 | * without one, the pi logic would not know which task to boost/deboost, if |
| 3361 | * there was a need to. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3362 | * |
| 3363 | * We call schedule in futex_wait_queue_me() when we enqueue and return there |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 3364 | * via the following-- |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3365 | * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue() |
Darren Hart | cc6db4e | 2009-07-31 16:20:10 -0700 | [diff] [blame] | 3366 | * 2) wakeup on uaddr2 after a requeue |
| 3367 | * 3) signal |
| 3368 | * 4) timeout |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3369 | * |
Darren Hart | cc6db4e | 2009-07-31 16:20:10 -0700 | [diff] [blame] | 3370 | * If 3, cleanup and return -ERESTARTNOINTR. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3371 | * |
| 3372 | * If 2, we may then block on trying to take the rt_mutex and return via: |
| 3373 | * 5) successful lock |
| 3374 | * 6) signal |
| 3375 | * 7) timeout |
| 3376 | * 8) other lock acquisition failure |
| 3377 | * |
Darren Hart | cc6db4e | 2009-07-31 16:20:10 -0700 | [diff] [blame] | 3378 | * If 6, return -EWOULDBLOCK (restarting the syscall would do the same). |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3379 | * |
| 3380 | * If 4 or 7, we cleanup and return with -ETIMEDOUT. |
| 3381 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 3382 | * Return: |
Mauro Carvalho Chehab | 7b4ff1a | 2017-05-11 10:17:45 -0300 | [diff] [blame] | 3383 | * - 0 - On success; |
| 3384 | * - <0 - On error |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3385 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3386 | static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3387 | u32 val, ktime_t *abs_time, u32 bitset, |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3388 | u32 __user *uaddr2) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3389 | { |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 3390 | struct hrtimer_sleeper timeout, *to; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3391 | struct rt_mutex_waiter rt_waiter; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3392 | struct futex_hash_bucket *hb; |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 3393 | union futex_key key2 = FUTEX_KEY_INIT; |
| 3394 | struct futex_q q = futex_q_init; |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3395 | struct rt_mutex_base *pi_mutex; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3396 | int res, ret; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3397 | |
Nicolas Pitre | bc2eecd | 2017-08-01 00:31:32 -0400 | [diff] [blame] | 3398 | if (!IS_ENABLED(CONFIG_FUTEX_PI)) |
| 3399 | return -ENOSYS; |
| 3400 | |
Darren Hart | 6f7b0a2 | 2012-07-20 11:53:31 -0700 | [diff] [blame] | 3401 | if (uaddr == uaddr2) |
| 3402 | return -EINVAL; |
| 3403 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3404 | if (!bitset) |
| 3405 | return -EINVAL; |
| 3406 | |
Waiman Long | 5ca584d | 2019-05-28 12:03:45 -0400 | [diff] [blame] | 3407 | to = futex_setup_timer(abs_time, &timeout, flags, |
| 3408 | current->timer_slack_ns); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3409 | |
| 3410 | /* |
| 3411 | * The waiter is allocated on our stack, manipulated by the requeue |
| 3412 | * code while we sleep on uaddr. |
| 3413 | */ |
Peter Zijlstra | 5080935 | 2017-03-22 11:35:56 +0100 | [diff] [blame] | 3414 | rt_mutex_init_waiter(&rt_waiter); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3415 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 3416 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, FUTEX_WRITE); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3417 | if (unlikely(ret != 0)) |
| 3418 | goto out; |
| 3419 | |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 3420 | q.bitset = bitset; |
| 3421 | q.rt_waiter = &rt_waiter; |
| 3422 | q.requeue_pi_key = &key2; |
| 3423 | |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 3424 | /* |
Thomas Gleixner | c363b7e | 2021-08-15 23:29:06 +0200 | [diff] [blame] | 3425 | * Prepare to wait on uaddr. On success, it holds hb->lock and q |
| 3426 | * is initialized. |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 3427 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3428 | ret = futex_wait_setup(uaddr, val, flags, &q, &hb); |
Thomas Gleixner | c8b15a7 | 2009-05-20 09:18:50 +0200 | [diff] [blame] | 3429 | if (ret) |
André Almeida | 9180bd4 | 2020-07-02 17:28:40 -0300 | [diff] [blame] | 3430 | goto out; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3431 | |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 3432 | /* |
| 3433 | * The check above which compares uaddrs is not sufficient for |
| 3434 | * shared futexes. We need to compare the keys: |
| 3435 | */ |
| 3436 | if (match_futex(&q.key, &key2)) { |
Thomas Gleixner | 13c42c2 | 2014-09-11 23:44:35 +0200 | [diff] [blame] | 3437 | queue_unlock(hb); |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 3438 | ret = -EINVAL; |
André Almeida | 9180bd4 | 2020-07-02 17:28:40 -0300 | [diff] [blame] | 3439 | goto out; |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3442 | /* Queue the futex_q, drop the hb lock, wait for wakeup. */ |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 3443 | futex_wait_queue_me(hb, &q, to); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3444 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3445 | switch (futex_requeue_pi_wakeup_sync(&q)) { |
| 3446 | case Q_REQUEUE_PI_IGNORE: |
| 3447 | /* The waiter is still on uaddr1 */ |
| 3448 | spin_lock(&hb->lock); |
Thomas Gleixner | 6231acb | 2021-08-15 23:29:17 +0200 | [diff] [blame] | 3449 | ret = handle_early_requeue_pi_wakeup(hb, &q, to); |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3450 | spin_unlock(&hb->lock); |
| 3451 | break; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3452 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3453 | case Q_REQUEUE_PI_LOCKED: |
| 3454 | /* The requeue acquired the lock */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3455 | if (q.pi_state && (q.pi_state->owner != current)) { |
| 3456 | spin_lock(q.lock_ptr); |
Davidlohr Bueso | a1565aa | 2021-02-26 09:50:27 -0800 | [diff] [blame] | 3457 | ret = fixup_owner(uaddr2, &q, true); |
Thomas Gleixner | fb75a42 | 2015-12-19 20:07:38 +0000 | [diff] [blame] | 3458 | /* |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3459 | * Drop the reference to the pi state which the |
| 3460 | * requeue_pi() code acquired for us. |
Thomas Gleixner | fb75a42 | 2015-12-19 20:07:38 +0000 | [diff] [blame] | 3461 | */ |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 3462 | put_pi_state(q.pi_state); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3463 | spin_unlock(q.lock_ptr); |
Thomas Gleixner | 12bb3f7 | 2021-01-20 16:00:24 +0100 | [diff] [blame] | 3464 | /* |
| 3465 | * Adjust the return value. It's either -EFAULT or |
| 3466 | * success (1) but the caller expects 0 for success. |
| 3467 | */ |
| 3468 | ret = ret < 0 ? ret : 0; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3469 | } |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3470 | break; |
Peter Zijlstra | c236c8e | 2017-03-04 10:27:18 +0100 | [diff] [blame] | 3471 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3472 | case Q_REQUEUE_PI_DONE: |
| 3473 | /* Requeue completed. Current is 'pi_blocked_on' the rtmutex */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3474 | pi_mutex = &q.pi_state->pi_mutex; |
Peter Zijlstra | 38d589f | 2017-03-22 11:35:57 +0100 | [diff] [blame] | 3475 | ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3476 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3477 | /* Current is not longer pi_blocked_on */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3478 | spin_lock(q.lock_ptr); |
Peter Zijlstra | 38d589f | 2017-03-22 11:35:57 +0100 | [diff] [blame] | 3479 | if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter)) |
| 3480 | ret = 0; |
| 3481 | |
| 3482 | debug_rt_mutex_free_waiter(&rt_waiter); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3483 | /* |
| 3484 | * Fixup the pi_state owner and possibly acquire the lock if we |
| 3485 | * haven't already. |
| 3486 | */ |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3487 | res = fixup_owner(uaddr2, &q, !ret); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3488 | /* |
Ingo Molnar | 93d0955 | 2021-05-12 20:04:28 +0200 | [diff] [blame] | 3489 | * If fixup_owner() returned an error, propagate that. If it |
Darren Hart | 56ec160 | 2009-09-21 22:29:59 -0700 | [diff] [blame] | 3490 | * acquired the lock, clear -ETIMEDOUT or -EINTR. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3491 | */ |
| 3492 | if (res) |
| 3493 | ret = (res < 0) ? res : 0; |
| 3494 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3495 | unqueue_me_pi(&q); |
Davidlohr Bueso | a3f2428 | 2021-02-26 09:50:28 -0800 | [diff] [blame] | 3496 | spin_unlock(q.lock_ptr); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3497 | |
Thomas Gleixner | 07d91ef5 | 2021-08-15 23:29:18 +0200 | [diff] [blame] | 3498 | if (ret == -EINTR) { |
| 3499 | /* |
| 3500 | * We've already been requeued, but cannot restart |
| 3501 | * by calling futex_lock_pi() directly. We could |
| 3502 | * restart this syscall, but it would detect that |
| 3503 | * the user space "val" changed and return |
| 3504 | * -EWOULDBLOCK. Save the overhead of the restart |
| 3505 | * and return -EWOULDBLOCK directly. |
| 3506 | */ |
| 3507 | ret = -EWOULDBLOCK; |
| 3508 | } |
| 3509 | break; |
| 3510 | default: |
| 3511 | BUG(); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3512 | } |
| 3513 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3514 | out: |
| 3515 | if (to) { |
| 3516 | hrtimer_cancel(&to->timer); |
| 3517 | destroy_hrtimer_on_stack(&to->timer); |
| 3518 | } |
| 3519 | return ret; |
| 3520 | } |
| 3521 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3522 | /* |
| 3523 | * Support for robust futexes: the kernel cleans up held futexes at |
| 3524 | * thread exit time. |
| 3525 | * |
| 3526 | * Implementation: user-space maintains a per-thread list of locks it |
| 3527 | * is holding. Upon do_exit(), the kernel carefully walks this list, |
| 3528 | * and marks all locks that are owned by this thread with the |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3529 | * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3530 | * always manipulated with the lock held, so the list is private and |
| 3531 | * per-thread. Userspace also maintains a per-thread 'list_op_pending' |
| 3532 | * field, to allow the kernel to clean up if the thread dies after |
| 3533 | * acquiring the lock, but just before it could have added itself to |
| 3534 | * the list. There can only be one such pending lock. |
| 3535 | */ |
| 3536 | |
| 3537 | /** |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 3538 | * sys_set_robust_list() - Set the robust-futex list head of a task |
| 3539 | * @head: pointer to the list-head |
| 3540 | * @len: length of the list-head, as userspace expects |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3541 | */ |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 3542 | SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head, |
| 3543 | size_t, len) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3544 | { |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 3545 | if (!futex_cmpxchg_enabled) |
| 3546 | return -ENOSYS; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3547 | /* |
| 3548 | * The kernel knows only one size for now: |
| 3549 | */ |
| 3550 | if (unlikely(len != sizeof(*head))) |
| 3551 | return -EINVAL; |
| 3552 | |
| 3553 | current->robust_list = head; |
| 3554 | |
| 3555 | return 0; |
| 3556 | } |
| 3557 | |
| 3558 | /** |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 3559 | * sys_get_robust_list() - Get the robust-futex list head of a task |
| 3560 | * @pid: pid of the process [zero for current task] |
| 3561 | * @head_ptr: pointer to a list-head pointer, the kernel fills it in |
| 3562 | * @len_ptr: pointer to a length field, the kernel fills in the header size |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3563 | */ |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 3564 | SYSCALL_DEFINE3(get_robust_list, int, pid, |
| 3565 | struct robust_list_head __user * __user *, head_ptr, |
| 3566 | size_t __user *, len_ptr) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3567 | { |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3568 | struct robust_list_head __user *head; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3569 | unsigned long ret; |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3570 | struct task_struct *p; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3571 | |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 3572 | if (!futex_cmpxchg_enabled) |
| 3573 | return -ENOSYS; |
| 3574 | |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3575 | rcu_read_lock(); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3576 | |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3577 | ret = -ESRCH; |
| 3578 | if (!pid) |
| 3579 | p = current; |
| 3580 | else { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 3581 | p = find_task_by_vpid(pid); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3582 | if (!p) |
| 3583 | goto err_unlock; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3584 | } |
| 3585 | |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3586 | ret = -EPERM; |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 3587 | if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3588 | goto err_unlock; |
| 3589 | |
| 3590 | head = p->robust_list; |
| 3591 | rcu_read_unlock(); |
| 3592 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3593 | if (put_user(sizeof(*head), len_ptr)) |
| 3594 | return -EFAULT; |
| 3595 | return put_user(head, head_ptr); |
| 3596 | |
| 3597 | err_unlock: |
Oleg Nesterov | aaa2a97 | 2006-09-29 02:00:55 -0700 | [diff] [blame] | 3598 | rcu_read_unlock(); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3599 | |
| 3600 | return ret; |
| 3601 | } |
| 3602 | |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3603 | /* Constants for the pending_op argument of handle_futex_death */ |
| 3604 | #define HANDLE_DEATH_PENDING true |
| 3605 | #define HANDLE_DEATH_LIST false |
| 3606 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3607 | /* |
| 3608 | * Process a futex-list entry, check whether it's owned by the |
| 3609 | * dying task, and do notification if so: |
| 3610 | */ |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3611 | static int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, |
| 3612 | bool pi, bool pending_op) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3613 | { |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 3614 | u32 uval, nval, mval; |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3615 | int err; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3616 | |
Chen Jie | 5a07168 | 2019-03-15 03:44:38 +0000 | [diff] [blame] | 3617 | /* Futex address must be 32bit aligned */ |
| 3618 | if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0) |
| 3619 | return -1; |
| 3620 | |
Ingo Molnar | 8f17d3a | 2006-03-27 01:16:27 -0800 | [diff] [blame] | 3621 | retry: |
| 3622 | if (get_user(uval, uaddr)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3623 | return -1; |
| 3624 | |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3625 | /* |
| 3626 | * Special case for regular (non PI) futexes. The unlock path in |
| 3627 | * user space has two race scenarios: |
| 3628 | * |
| 3629 | * 1. The unlock path releases the user space futex value and |
| 3630 | * before it can execute the futex() syscall to wake up |
| 3631 | * waiters it is killed. |
| 3632 | * |
| 3633 | * 2. A woken up waiter is killed before it can acquire the |
| 3634 | * futex in user space. |
| 3635 | * |
| 3636 | * In both cases the TID validation below prevents a wakeup of |
| 3637 | * potential waiters which can cause these waiters to block |
| 3638 | * forever. |
| 3639 | * |
| 3640 | * In both cases the following conditions are met: |
| 3641 | * |
| 3642 | * 1) task->robust_list->list_op_pending != NULL |
| 3643 | * @pending_op == true |
| 3644 | * 2) User space futex value == 0 |
| 3645 | * 3) Regular futex: @pi == false |
| 3646 | * |
| 3647 | * If these conditions are met, it is safe to attempt waking up a |
| 3648 | * potential waiter without touching the user space futex value and |
| 3649 | * trying to set the OWNER_DIED bit. The user space futex value is |
| 3650 | * uncontended and the rest of the user space mutex state is |
| 3651 | * consistent, so a woken waiter will just take over the |
| 3652 | * uncontended futex. Setting the OWNER_DIED bit would create |
| 3653 | * inconsistent state and malfunction of the user space owner died |
| 3654 | * handling. |
| 3655 | */ |
| 3656 | if (pending_op && !pi && !uval) { |
| 3657 | futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY); |
| 3658 | return 0; |
| 3659 | } |
| 3660 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3661 | if ((uval & FUTEX_TID_MASK) != task_pid_vnr(curr)) |
| 3662 | return 0; |
| 3663 | |
| 3664 | /* |
| 3665 | * Ok, this dying thread is truly holding a futex |
| 3666 | * of interest. Set the OWNER_DIED bit atomically |
| 3667 | * via cmpxchg, and if the value had FUTEX_WAITERS |
| 3668 | * set, wake up a waiter (if any). (We have to do a |
| 3669 | * futex_wake() even if OWNER_DIED is already set - |
| 3670 | * to handle the rare but possible case of recursive |
| 3671 | * thread-death.) The rest of the cleanup is done in |
| 3672 | * userspace. |
| 3673 | */ |
| 3674 | mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED; |
| 3675 | |
| 3676 | /* |
| 3677 | * We are not holding a lock here, but we want to have |
| 3678 | * the pagefault_disable/enable() protection because |
| 3679 | * we want to handle the fault gracefully. If the |
| 3680 | * access fails we try to fault in the futex with R/W |
| 3681 | * verification via get_user_pages. get_user() above |
| 3682 | * does not guarantee R/W access. If that fails we |
| 3683 | * give up and leave the futex locked. |
| 3684 | */ |
| 3685 | if ((err = cmpxchg_futex_value_locked(&nval, uaddr, uval, mval))) { |
| 3686 | switch (err) { |
| 3687 | case -EFAULT: |
Thomas Gleixner | 6e0aa9f | 2011-03-14 10:34:35 +0100 | [diff] [blame] | 3688 | if (fault_in_user_writeable(uaddr)) |
| 3689 | return -1; |
| 3690 | goto retry; |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3691 | |
| 3692 | case -EAGAIN: |
| 3693 | cond_resched(); |
Ingo Molnar | 8f17d3a | 2006-03-27 01:16:27 -0800 | [diff] [blame] | 3694 | goto retry; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3695 | |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3696 | default: |
| 3697 | WARN_ON_ONCE(1); |
| 3698 | return err; |
| 3699 | } |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3700 | } |
Will Deacon | 6b4f4bc | 2019-02-28 11:58:08 +0000 | [diff] [blame] | 3701 | |
| 3702 | if (nval != uval) |
| 3703 | goto retry; |
| 3704 | |
| 3705 | /* |
| 3706 | * Wake robust non-PI futexes here. The wakeup of |
| 3707 | * PI futexes happens in exit_pi_state(): |
| 3708 | */ |
| 3709 | if (!pi && (uval & FUTEX_WAITERS)) |
| 3710 | futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY); |
| 3711 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3712 | return 0; |
| 3713 | } |
| 3714 | |
| 3715 | /* |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3716 | * Fetch a robust-list pointer. Bit 0 signals PI futexes: |
| 3717 | */ |
| 3718 | static inline int fetch_robust_entry(struct robust_list __user **entry, |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3719 | struct robust_list __user * __user *head, |
Namhyung Kim | 1dcc41b | 2010-09-14 21:43:46 +0900 | [diff] [blame] | 3720 | unsigned int *pi) |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3721 | { |
| 3722 | unsigned long uentry; |
| 3723 | |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3724 | if (get_user(uentry, (unsigned long __user *)head)) |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3725 | return -EFAULT; |
| 3726 | |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3727 | *entry = (void __user *)(uentry & ~1UL); |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3728 | *pi = uentry & 1; |
| 3729 | |
| 3730 | return 0; |
| 3731 | } |
| 3732 | |
| 3733 | /* |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3734 | * Walk curr->robust_list (very carefully, it's a userspace list!) |
| 3735 | * and mark any locks found there dead, and notify any waiters. |
| 3736 | * |
| 3737 | * We silently return on any sign of list-walking problem. |
| 3738 | */ |
Thomas Gleixner | ba31c1a4 | 2019-11-06 22:55:36 +0100 | [diff] [blame] | 3739 | static void exit_robust_list(struct task_struct *curr) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3740 | { |
| 3741 | struct robust_list_head __user *head = curr->robust_list; |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3742 | struct robust_list __user *entry, *next_entry, *pending; |
Darren Hart | 4c115e9 | 2010-11-04 15:00:00 -0400 | [diff] [blame] | 3743 | unsigned int limit = ROBUST_LIST_LIMIT, pi, pip; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 3744 | unsigned int next_pi; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3745 | unsigned long futex_offset; |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3746 | int rc; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3747 | |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 3748 | if (!futex_cmpxchg_enabled) |
| 3749 | return; |
| 3750 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3751 | /* |
| 3752 | * Fetch the list head (which was registered earlier, via |
| 3753 | * sys_set_robust_list()): |
| 3754 | */ |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3755 | if (fetch_robust_entry(&entry, &head->list.next, &pi)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3756 | return; |
| 3757 | /* |
| 3758 | * Fetch the relative futex offset: |
| 3759 | */ |
| 3760 | if (get_user(futex_offset, &head->futex_offset)) |
| 3761 | return; |
| 3762 | /* |
| 3763 | * Fetch any possibly pending lock-add first, and handle it |
| 3764 | * if it exists: |
| 3765 | */ |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3766 | if (fetch_robust_entry(&pending, &head->list_op_pending, &pip)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3767 | return; |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3768 | |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3769 | next_entry = NULL; /* avoid warning with gcc */ |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3770 | while (entry != &head->list) { |
| 3771 | /* |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3772 | * Fetch the next entry in the list before calling |
| 3773 | * handle_futex_death: |
| 3774 | */ |
| 3775 | rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi); |
| 3776 | /* |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3777 | * A pending lock might already be on the list, so |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3778 | * don't process it twice: |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3779 | */ |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3780 | if (entry != pending) { |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3781 | if (handle_futex_death((void __user *)entry + futex_offset, |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3782 | curr, pi, HANDLE_DEATH_LIST)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3783 | return; |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3784 | } |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3785 | if (rc) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3786 | return; |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3787 | entry = next_entry; |
| 3788 | pi = next_pi; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3789 | /* |
| 3790 | * Avoid excessively long or circular lists: |
| 3791 | */ |
| 3792 | if (!--limit) |
| 3793 | break; |
| 3794 | |
| 3795 | cond_resched(); |
| 3796 | } |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3797 | |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3798 | if (pending) { |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3799 | handle_futex_death((void __user *)pending + futex_offset, |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 3800 | curr, pip, HANDLE_DEATH_PENDING); |
| 3801 | } |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3802 | } |
| 3803 | |
Thomas Gleixner | af8cbda | 2019-11-06 22:55:43 +0100 | [diff] [blame] | 3804 | static void futex_cleanup(struct task_struct *tsk) |
Thomas Gleixner | ba31c1a4 | 2019-11-06 22:55:36 +0100 | [diff] [blame] | 3805 | { |
| 3806 | if (unlikely(tsk->robust_list)) { |
| 3807 | exit_robust_list(tsk); |
| 3808 | tsk->robust_list = NULL; |
| 3809 | } |
| 3810 | |
| 3811 | #ifdef CONFIG_COMPAT |
| 3812 | if (unlikely(tsk->compat_robust_list)) { |
| 3813 | compat_exit_robust_list(tsk); |
| 3814 | tsk->compat_robust_list = NULL; |
| 3815 | } |
| 3816 | #endif |
| 3817 | |
| 3818 | if (unlikely(!list_empty(&tsk->pi_state_list))) |
| 3819 | exit_pi_state_list(tsk); |
| 3820 | } |
| 3821 | |
Thomas Gleixner | 18f6943 | 2019-11-06 22:55:41 +0100 | [diff] [blame] | 3822 | /** |
| 3823 | * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD |
| 3824 | * @tsk: task to set the state on |
| 3825 | * |
| 3826 | * Set the futex exit state of the task lockless. The futex waiter code |
| 3827 | * observes that state when a task is exiting and loops until the task has |
| 3828 | * actually finished the futex cleanup. The worst case for this is that the |
| 3829 | * waiter runs through the wait loop until the state becomes visible. |
| 3830 | * |
| 3831 | * This is called from the recursive fault handling path in do_exit(). |
| 3832 | * |
| 3833 | * This is best effort. Either the futex exit code has run already or |
| 3834 | * not. If the OWNER_DIED bit has been set on the futex then the waiter can |
| 3835 | * take it over. If not, the problem is pushed back to user space. If the |
| 3836 | * futex exit code did not run yet, then an already queued waiter might |
| 3837 | * block forever, but there is nothing which can be done about that. |
| 3838 | */ |
| 3839 | void futex_exit_recursive(struct task_struct *tsk) |
| 3840 | { |
Thomas Gleixner | 3f186d9 | 2019-11-06 22:55:44 +0100 | [diff] [blame] | 3841 | /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */ |
| 3842 | if (tsk->futex_state == FUTEX_STATE_EXITING) |
| 3843 | mutex_unlock(&tsk->futex_exit_mutex); |
Thomas Gleixner | 18f6943 | 2019-11-06 22:55:41 +0100 | [diff] [blame] | 3844 | tsk->futex_state = FUTEX_STATE_DEAD; |
| 3845 | } |
| 3846 | |
Thomas Gleixner | af8cbda | 2019-11-06 22:55:43 +0100 | [diff] [blame] | 3847 | static void futex_cleanup_begin(struct task_struct *tsk) |
Thomas Gleixner | 150d715 | 2019-11-06 22:55:39 +0100 | [diff] [blame] | 3848 | { |
Thomas Gleixner | 18f6943 | 2019-11-06 22:55:41 +0100 | [diff] [blame] | 3849 | /* |
Thomas Gleixner | 3f186d9 | 2019-11-06 22:55:44 +0100 | [diff] [blame] | 3850 | * Prevent various race issues against a concurrent incoming waiter |
| 3851 | * including live locks by forcing the waiter to block on |
| 3852 | * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in |
| 3853 | * attach_to_pi_owner(). |
| 3854 | */ |
| 3855 | mutex_lock(&tsk->futex_exit_mutex); |
| 3856 | |
| 3857 | /* |
Thomas Gleixner | 4a8e991 | 2019-11-06 22:55:42 +0100 | [diff] [blame] | 3858 | * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock. |
| 3859 | * |
| 3860 | * This ensures that all subsequent checks of tsk->futex_state in |
| 3861 | * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with |
| 3862 | * tsk->pi_lock held. |
| 3863 | * |
| 3864 | * It guarantees also that a pi_state which was queued right before |
| 3865 | * the state change under tsk->pi_lock by a concurrent waiter must |
| 3866 | * be observed in exit_pi_state_list(). |
Thomas Gleixner | 18f6943 | 2019-11-06 22:55:41 +0100 | [diff] [blame] | 3867 | */ |
| 3868 | raw_spin_lock_irq(&tsk->pi_lock); |
Thomas Gleixner | 4a8e991 | 2019-11-06 22:55:42 +0100 | [diff] [blame] | 3869 | tsk->futex_state = FUTEX_STATE_EXITING; |
Thomas Gleixner | 18f6943 | 2019-11-06 22:55:41 +0100 | [diff] [blame] | 3870 | raw_spin_unlock_irq(&tsk->pi_lock); |
Thomas Gleixner | af8cbda | 2019-11-06 22:55:43 +0100 | [diff] [blame] | 3871 | } |
Thomas Gleixner | 18f6943 | 2019-11-06 22:55:41 +0100 | [diff] [blame] | 3872 | |
Thomas Gleixner | af8cbda | 2019-11-06 22:55:43 +0100 | [diff] [blame] | 3873 | static void futex_cleanup_end(struct task_struct *tsk, int state) |
| 3874 | { |
| 3875 | /* |
| 3876 | * Lockless store. The only side effect is that an observer might |
| 3877 | * take another loop until it becomes visible. |
| 3878 | */ |
| 3879 | tsk->futex_state = state; |
Thomas Gleixner | 3f186d9 | 2019-11-06 22:55:44 +0100 | [diff] [blame] | 3880 | /* |
| 3881 | * Drop the exit protection. This unblocks waiters which observed |
| 3882 | * FUTEX_STATE_EXITING to reevaluate the state. |
| 3883 | */ |
| 3884 | mutex_unlock(&tsk->futex_exit_mutex); |
Thomas Gleixner | af8cbda | 2019-11-06 22:55:43 +0100 | [diff] [blame] | 3885 | } |
Thomas Gleixner | 18f6943 | 2019-11-06 22:55:41 +0100 | [diff] [blame] | 3886 | |
Thomas Gleixner | af8cbda | 2019-11-06 22:55:43 +0100 | [diff] [blame] | 3887 | void futex_exec_release(struct task_struct *tsk) |
| 3888 | { |
| 3889 | /* |
| 3890 | * The state handling is done for consistency, but in the case of |
Ingo Molnar | 93d0955 | 2021-05-12 20:04:28 +0200 | [diff] [blame] | 3891 | * exec() there is no way to prevent further damage as the PID stays |
Thomas Gleixner | af8cbda | 2019-11-06 22:55:43 +0100 | [diff] [blame] | 3892 | * the same. But for the unlikely and arguably buggy case that a |
| 3893 | * futex is held on exec(), this provides at least as much state |
| 3894 | * consistency protection which is possible. |
| 3895 | */ |
| 3896 | futex_cleanup_begin(tsk); |
| 3897 | futex_cleanup(tsk); |
| 3898 | /* |
| 3899 | * Reset the state to FUTEX_STATE_OK. The task is alive and about |
| 3900 | * exec a new binary. |
| 3901 | */ |
| 3902 | futex_cleanup_end(tsk, FUTEX_STATE_OK); |
| 3903 | } |
| 3904 | |
| 3905 | void futex_exit_release(struct task_struct *tsk) |
| 3906 | { |
| 3907 | futex_cleanup_begin(tsk); |
| 3908 | futex_cleanup(tsk); |
| 3909 | futex_cleanup_end(tsk, FUTEX_STATE_DEAD); |
Thomas Gleixner | 150d715 | 2019-11-06 22:55:39 +0100 | [diff] [blame] | 3910 | } |
| 3911 | |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3912 | long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout, |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 3913 | u32 __user *uaddr2, u32 val2, u32 val3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3914 | { |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3915 | int cmd = op & FUTEX_CMD_MASK; |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3916 | unsigned int flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3917 | |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3918 | if (!(op & FUTEX_PRIVATE_FLAG)) |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3919 | flags |= FLAGS_SHARED; |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3920 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3921 | if (op & FUTEX_CLOCK_REALTIME) { |
| 3922 | flags |= FLAGS_CLOCKRT; |
Thomas Gleixner | bf22a69 | 2021-04-22 21:44:23 +0200 | [diff] [blame] | 3923 | if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI && |
| 3924 | cmd != FUTEX_LOCK_PI2) |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3925 | return -ENOSYS; |
| 3926 | } |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3927 | |
| 3928 | switch (cmd) { |
Thomas Gleixner | 59263b5 | 2012-02-15 12:08:34 +0100 | [diff] [blame] | 3929 | case FUTEX_LOCK_PI: |
Thomas Gleixner | bf22a69 | 2021-04-22 21:44:23 +0200 | [diff] [blame] | 3930 | case FUTEX_LOCK_PI2: |
Thomas Gleixner | 59263b5 | 2012-02-15 12:08:34 +0100 | [diff] [blame] | 3931 | case FUTEX_UNLOCK_PI: |
| 3932 | case FUTEX_TRYLOCK_PI: |
| 3933 | case FUTEX_WAIT_REQUEUE_PI: |
| 3934 | case FUTEX_CMP_REQUEUE_PI: |
| 3935 | if (!futex_cmpxchg_enabled) |
| 3936 | return -ENOSYS; |
| 3937 | } |
| 3938 | |
| 3939 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | case FUTEX_WAIT: |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 3941 | val3 = FUTEX_BITSET_MATCH_ANY; |
Miaohe Lin | 405fa8a | 2020-08-13 08:21:17 -0400 | [diff] [blame] | 3942 | fallthrough; |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 3943 | case FUTEX_WAIT_BITSET: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3944 | return futex_wait(uaddr, flags, val, timeout, val3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3945 | case FUTEX_WAKE: |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 3946 | val3 = FUTEX_BITSET_MATCH_ANY; |
Miaohe Lin | 405fa8a | 2020-08-13 08:21:17 -0400 | [diff] [blame] | 3947 | fallthrough; |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 3948 | case FUTEX_WAKE_BITSET: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3949 | return futex_wake(uaddr, flags, val, val3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | case FUTEX_REQUEUE: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3951 | return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3952 | case FUTEX_CMP_REQUEUE: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3953 | return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0); |
Jakub Jelinek | 4732efbe | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 3954 | case FUTEX_WAKE_OP: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3955 | return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3956 | case FUTEX_LOCK_PI: |
Thomas Gleixner | e112c41 | 2021-04-22 21:44:22 +0200 | [diff] [blame] | 3957 | flags |= FLAGS_CLOCKRT; |
Thomas Gleixner | bf22a69 | 2021-04-22 21:44:23 +0200 | [diff] [blame] | 3958 | fallthrough; |
| 3959 | case FUTEX_LOCK_PI2: |
Michael Kerrisk | 996636d | 2015-01-16 20:28:06 +0100 | [diff] [blame] | 3960 | return futex_lock_pi(uaddr, flags, timeout, 0); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3961 | case FUTEX_UNLOCK_PI: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3962 | return futex_unlock_pi(uaddr, flags); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3963 | case FUTEX_TRYLOCK_PI: |
Michael Kerrisk | 996636d | 2015-01-16 20:28:06 +0100 | [diff] [blame] | 3964 | return futex_lock_pi(uaddr, flags, NULL, 1); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3965 | case FUTEX_WAIT_REQUEUE_PI: |
| 3966 | val3 = FUTEX_BITSET_MATCH_ANY; |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3967 | return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3, |
| 3968 | uaddr2); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3969 | case FUTEX_CMP_REQUEUE_PI: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3970 | return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | } |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3972 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3973 | } |
| 3974 | |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 3975 | static __always_inline bool futex_cmd_has_timeout(u32 cmd) |
| 3976 | { |
| 3977 | switch (cmd) { |
| 3978 | case FUTEX_WAIT: |
| 3979 | case FUTEX_LOCK_PI: |
Thomas Gleixner | bf22a69 | 2021-04-22 21:44:23 +0200 | [diff] [blame] | 3980 | case FUTEX_LOCK_PI2: |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 3981 | case FUTEX_WAIT_BITSET: |
| 3982 | case FUTEX_WAIT_REQUEUE_PI: |
| 3983 | return true; |
| 3984 | } |
| 3985 | return false; |
| 3986 | } |
| 3987 | |
| 3988 | static __always_inline int |
| 3989 | futex_init_timeout(u32 cmd, u32 op, struct timespec64 *ts, ktime_t *t) |
| 3990 | { |
| 3991 | if (!timespec64_valid(ts)) |
| 3992 | return -EINVAL; |
| 3993 | |
| 3994 | *t = timespec64_to_ktime(*ts); |
| 3995 | if (cmd == FUTEX_WAIT) |
| 3996 | *t = ktime_add_safe(ktime_get(), *t); |
| 3997 | else if (cmd != FUTEX_LOCK_PI && !(op & FUTEX_CLOCK_REALTIME)) |
| 3998 | *t = timens_ktime_to_host(CLOCK_MONOTONIC, *t); |
| 3999 | return 0; |
| 4000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 4002 | SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val, |
Alejandro Colomar | 1ce53e2 | 2020-11-28 13:39:46 +0100 | [diff] [blame] | 4003 | const struct __kernel_timespec __user *, utime, |
| 4004 | u32 __user *, uaddr2, u32, val3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | { |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4006 | int ret, cmd = op & FUTEX_CMD_MASK; |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 4007 | ktime_t t, *tp = NULL; |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4008 | struct timespec64 ts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4010 | if (utime && futex_cmd_has_timeout(cmd)) { |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 4011 | if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG)))) |
| 4012 | return -EFAULT; |
Arnd Bergmann | bec2f7c | 2018-04-17 17:23:35 +0200 | [diff] [blame] | 4013 | if (get_timespec64(&ts, utime)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | return -EFAULT; |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4015 | ret = futex_init_timeout(cmd, op, &ts, &t); |
| 4016 | if (ret) |
| 4017 | return ret; |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 4018 | tp = &t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | |
Thomas Gleixner | b097d5e | 2021-04-22 21:44:20 +0200 | [diff] [blame] | 4021 | return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | } |
| 4023 | |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4024 | #ifdef CONFIG_COMPAT |
| 4025 | /* |
| 4026 | * Fetch a robust-list pointer. Bit 0 signals PI futexes: |
| 4027 | */ |
| 4028 | static inline int |
| 4029 | compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry, |
| 4030 | compat_uptr_t __user *head, unsigned int *pi) |
| 4031 | { |
| 4032 | if (get_user(*uentry, head)) |
| 4033 | return -EFAULT; |
| 4034 | |
| 4035 | *entry = compat_ptr((*uentry) & ~1); |
| 4036 | *pi = (unsigned int)(*uentry) & 1; |
| 4037 | |
| 4038 | return 0; |
| 4039 | } |
| 4040 | |
| 4041 | static void __user *futex_uaddr(struct robust_list __user *entry, |
| 4042 | compat_long_t futex_offset) |
| 4043 | { |
| 4044 | compat_uptr_t base = ptr_to_compat(entry); |
| 4045 | void __user *uaddr = compat_ptr(base + futex_offset); |
| 4046 | |
| 4047 | return uaddr; |
| 4048 | } |
| 4049 | |
| 4050 | /* |
| 4051 | * Walk curr->robust_list (very carefully, it's a userspace list!) |
| 4052 | * and mark any locks found there dead, and notify any waiters. |
| 4053 | * |
| 4054 | * We silently return on any sign of list-walking problem. |
| 4055 | */ |
Thomas Gleixner | ba31c1a4 | 2019-11-06 22:55:36 +0100 | [diff] [blame] | 4056 | static void compat_exit_robust_list(struct task_struct *curr) |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4057 | { |
| 4058 | struct compat_robust_list_head __user *head = curr->compat_robust_list; |
| 4059 | struct robust_list __user *entry, *next_entry, *pending; |
| 4060 | unsigned int limit = ROBUST_LIST_LIMIT, pi, pip; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 4061 | unsigned int next_pi; |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4062 | compat_uptr_t uentry, next_uentry, upending; |
| 4063 | compat_long_t futex_offset; |
| 4064 | int rc; |
| 4065 | |
| 4066 | if (!futex_cmpxchg_enabled) |
| 4067 | return; |
| 4068 | |
| 4069 | /* |
| 4070 | * Fetch the list head (which was registered earlier, via |
| 4071 | * sys_set_robust_list()): |
| 4072 | */ |
| 4073 | if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi)) |
| 4074 | return; |
| 4075 | /* |
| 4076 | * Fetch the relative futex offset: |
| 4077 | */ |
| 4078 | if (get_user(futex_offset, &head->futex_offset)) |
| 4079 | return; |
| 4080 | /* |
| 4081 | * Fetch any possibly pending lock-add first, and handle it |
| 4082 | * if it exists: |
| 4083 | */ |
| 4084 | if (compat_fetch_robust_entry(&upending, &pending, |
| 4085 | &head->list_op_pending, &pip)) |
| 4086 | return; |
| 4087 | |
| 4088 | next_entry = NULL; /* avoid warning with gcc */ |
| 4089 | while (entry != (struct robust_list __user *) &head->list) { |
| 4090 | /* |
| 4091 | * Fetch the next entry in the list before calling |
| 4092 | * handle_futex_death: |
| 4093 | */ |
| 4094 | rc = compat_fetch_robust_entry(&next_uentry, &next_entry, |
| 4095 | (compat_uptr_t __user *)&entry->next, &next_pi); |
| 4096 | /* |
| 4097 | * A pending lock might already be on the list, so |
| 4098 | * dont process it twice: |
| 4099 | */ |
| 4100 | if (entry != pending) { |
| 4101 | void __user *uaddr = futex_uaddr(entry, futex_offset); |
| 4102 | |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 4103 | if (handle_futex_death(uaddr, curr, pi, |
| 4104 | HANDLE_DEATH_LIST)) |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4105 | return; |
| 4106 | } |
| 4107 | if (rc) |
| 4108 | return; |
| 4109 | uentry = next_uentry; |
| 4110 | entry = next_entry; |
| 4111 | pi = next_pi; |
| 4112 | /* |
| 4113 | * Avoid excessively long or circular lists: |
| 4114 | */ |
| 4115 | if (!--limit) |
| 4116 | break; |
| 4117 | |
| 4118 | cond_resched(); |
| 4119 | } |
| 4120 | if (pending) { |
| 4121 | void __user *uaddr = futex_uaddr(pending, futex_offset); |
| 4122 | |
Yang Tao | ca16d5b | 2019-11-06 22:55:35 +0100 | [diff] [blame] | 4123 | handle_futex_death(uaddr, curr, pip, HANDLE_DEATH_PENDING); |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | COMPAT_SYSCALL_DEFINE2(set_robust_list, |
| 4128 | struct compat_robust_list_head __user *, head, |
| 4129 | compat_size_t, len) |
| 4130 | { |
| 4131 | if (!futex_cmpxchg_enabled) |
| 4132 | return -ENOSYS; |
| 4133 | |
| 4134 | if (unlikely(len != sizeof(*head))) |
| 4135 | return -EINVAL; |
| 4136 | |
| 4137 | current->compat_robust_list = head; |
| 4138 | |
| 4139 | return 0; |
| 4140 | } |
| 4141 | |
| 4142 | COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid, |
| 4143 | compat_uptr_t __user *, head_ptr, |
| 4144 | compat_size_t __user *, len_ptr) |
| 4145 | { |
| 4146 | struct compat_robust_list_head __user *head; |
| 4147 | unsigned long ret; |
| 4148 | struct task_struct *p; |
| 4149 | |
| 4150 | if (!futex_cmpxchg_enabled) |
| 4151 | return -ENOSYS; |
| 4152 | |
| 4153 | rcu_read_lock(); |
| 4154 | |
| 4155 | ret = -ESRCH; |
| 4156 | if (!pid) |
| 4157 | p = current; |
| 4158 | else { |
| 4159 | p = find_task_by_vpid(pid); |
| 4160 | if (!p) |
| 4161 | goto err_unlock; |
| 4162 | } |
| 4163 | |
| 4164 | ret = -EPERM; |
| 4165 | if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) |
| 4166 | goto err_unlock; |
| 4167 | |
| 4168 | head = p->compat_robust_list; |
| 4169 | rcu_read_unlock(); |
| 4170 | |
| 4171 | if (put_user(sizeof(*head), len_ptr)) |
| 4172 | return -EFAULT; |
| 4173 | return put_user(ptr_to_compat(head), head_ptr); |
| 4174 | |
| 4175 | err_unlock: |
| 4176 | rcu_read_unlock(); |
| 4177 | |
| 4178 | return ret; |
| 4179 | } |
Arnd Bergmann | bec2f7c | 2018-04-17 17:23:35 +0200 | [diff] [blame] | 4180 | #endif /* CONFIG_COMPAT */ |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4181 | |
Arnd Bergmann | bec2f7c | 2018-04-17 17:23:35 +0200 | [diff] [blame] | 4182 | #ifdef CONFIG_COMPAT_32BIT_TIME |
Arnd Bergmann | 8dabe72 | 2019-01-07 00:33:08 +0100 | [diff] [blame] | 4183 | SYSCALL_DEFINE6(futex_time32, u32 __user *, uaddr, int, op, u32, val, |
Alejandro Colomar | 1ce53e2 | 2020-11-28 13:39:46 +0100 | [diff] [blame] | 4184 | const struct old_timespec32 __user *, utime, u32 __user *, uaddr2, |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4185 | u32, val3) |
| 4186 | { |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4187 | int ret, cmd = op & FUTEX_CMD_MASK; |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4188 | ktime_t t, *tp = NULL; |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4189 | struct timespec64 ts; |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4190 | |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4191 | if (utime && futex_cmd_has_timeout(cmd)) { |
Arnd Bergmann | bec2f7c | 2018-04-17 17:23:35 +0200 | [diff] [blame] | 4192 | if (get_old_timespec32(&ts, utime)) |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4193 | return -EFAULT; |
Thomas Gleixner | 51cf94d | 2021-04-22 21:44:21 +0200 | [diff] [blame] | 4194 | ret = futex_init_timeout(cmd, op, &ts, &t); |
| 4195 | if (ret) |
| 4196 | return ret; |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4197 | tp = &t; |
| 4198 | } |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4199 | |
Thomas Gleixner | b097d5e | 2021-04-22 21:44:20 +0200 | [diff] [blame] | 4200 | return do_futex(uaddr, op, val, tp, uaddr2, (unsigned long)utime, val3); |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4201 | } |
Arnd Bergmann | bec2f7c | 2018-04-17 17:23:35 +0200 | [diff] [blame] | 4202 | #endif /* CONFIG_COMPAT_32BIT_TIME */ |
Arnd Bergmann | 04e7712 | 2018-04-17 16:31:07 +0200 | [diff] [blame] | 4203 | |
Heiko Carstens | 03b8c7b | 2014-03-02 13:09:47 +0100 | [diff] [blame] | 4204 | static void __init futex_detect_cmpxchg(void) |
| 4205 | { |
| 4206 | #ifndef CONFIG_HAVE_FUTEX_CMPXCHG |
| 4207 | u32 curval; |
| 4208 | |
| 4209 | /* |
| 4210 | * This will fail and we want it. Some arch implementations do |
| 4211 | * runtime detection of the futex_atomic_cmpxchg_inatomic() |
| 4212 | * functionality. We want to know that before we call in any |
| 4213 | * of the complex code paths. Also we want to prevent |
| 4214 | * registration of robust lists in that case. NULL is |
| 4215 | * guaranteed to fault and we get -EFAULT on functional |
| 4216 | * implementation, the non-functional ones will return |
| 4217 | * -ENOSYS. |
| 4218 | */ |
| 4219 | if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT) |
| 4220 | futex_cmpxchg_enabled = 1; |
| 4221 | #endif |
| 4222 | } |
| 4223 | |
Benjamin Herrenschmidt | f6d107f | 2008-03-27 14:52:15 +1100 | [diff] [blame] | 4224 | static int __init futex_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4225 | { |
Heiko Carstens | 63b1a81 | 2014-01-16 14:54:50 +0100 | [diff] [blame] | 4226 | unsigned int futex_shift; |
Davidlohr Bueso | a52b89e | 2014-01-12 15:31:23 -0800 | [diff] [blame] | 4227 | unsigned long i; |
| 4228 | |
| 4229 | #if CONFIG_BASE_SMALL |
| 4230 | futex_hashsize = 16; |
| 4231 | #else |
| 4232 | futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus()); |
| 4233 | #endif |
| 4234 | |
| 4235 | futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues), |
| 4236 | futex_hashsize, 0, |
| 4237 | futex_hashsize < 256 ? HASH_SMALL : 0, |
Heiko Carstens | 63b1a81 | 2014-01-16 14:54:50 +0100 | [diff] [blame] | 4238 | &futex_shift, NULL, |
| 4239 | futex_hashsize, futex_hashsize); |
| 4240 | futex_hashsize = 1UL << futex_shift; |
Heiko Carstens | 03b8c7b | 2014-03-02 13:09:47 +0100 | [diff] [blame] | 4241 | |
| 4242 | futex_detect_cmpxchg(); |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 4243 | |
Davidlohr Bueso | a52b89e | 2014-01-12 15:31:23 -0800 | [diff] [blame] | 4244 | for (i = 0; i < futex_hashsize; i++) { |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 4245 | atomic_set(&futex_queues[i].waiters, 0); |
Dima Zavin | 732375c | 2011-07-07 17:27:59 -0700 | [diff] [blame] | 4246 | plist_head_init(&futex_queues[i].chain); |
Thomas Gleixner | 3e4ab74 | 2008-02-23 15:23:55 -0800 | [diff] [blame] | 4247 | spin_lock_init(&futex_queues[i].lock); |
| 4248 | } |
| 4249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4250 | return 0; |
| 4251 | } |
Yang Yang | 25f71d1 | 2016-12-30 16:17:55 +0800 | [diff] [blame] | 4252 | core_initcall(futex_init); |