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