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