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