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 | |
Thomas Gleixner | 76bc0ec | 2021-02-03 13:45:35 +0000 | [diff] [blame] | 840 | static void pi_state_update_owner(struct futex_pi_state *pi_state, |
| 841 | struct task_struct *new_owner) |
| 842 | { |
| 843 | struct task_struct *old_owner = pi_state->owner; |
| 844 | |
| 845 | lockdep_assert_held(&pi_state->pi_mutex.wait_lock); |
| 846 | |
| 847 | if (old_owner) { |
| 848 | raw_spin_lock(&old_owner->pi_lock); |
| 849 | WARN_ON(list_empty(&pi_state->list)); |
| 850 | list_del_init(&pi_state->list); |
| 851 | raw_spin_unlock(&old_owner->pi_lock); |
| 852 | } |
| 853 | |
| 854 | if (new_owner) { |
| 855 | raw_spin_lock(&new_owner->pi_lock); |
| 856 | WARN_ON(!list_empty(&pi_state->list)); |
| 857 | list_add(&pi_state->list, &new_owner->pi_state_list); |
| 858 | pi_state->owner = new_owner; |
| 859 | raw_spin_unlock(&new_owner->pi_lock); |
| 860 | } |
| 861 | } |
| 862 | |
Brian Silverman | 30a6b80 | 2014-10-25 20:20:37 -0400 | [diff] [blame] | 863 | /* |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 864 | * Drops a reference to the pi_state object and frees or caches it |
| 865 | * when the last reference is gone. |
| 866 | * |
Brian Silverman | 30a6b80 | 2014-10-25 20:20:37 -0400 | [diff] [blame] | 867 | * Must be called with the hb lock held. |
| 868 | */ |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 869 | static void put_pi_state(struct futex_pi_state *pi_state) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 870 | { |
Brian Silverman | 30a6b80 | 2014-10-25 20:20:37 -0400 | [diff] [blame] | 871 | if (!pi_state) |
| 872 | return; |
| 873 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 874 | if (!atomic_dec_and_test(&pi_state->refcount)) |
| 875 | return; |
| 876 | |
| 877 | /* |
| 878 | * If pi_state->owner is NULL, the owner is most probably dying |
| 879 | * and has cleaned up the pi_state already |
| 880 | */ |
| 881 | if (pi_state->owner) { |
Thomas Gleixner | 7d455bb | 2021-02-03 13:45:37 +0000 | [diff] [blame] | 882 | pi_state_update_owner(pi_state, NULL); |
Thomas Gleixner | 285b624 | 2021-02-03 13:45:36 +0000 | [diff] [blame] | 883 | rt_mutex_proxy_unlock(&pi_state->pi_mutex); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | if (current->pi_state_cache) |
| 887 | kfree(pi_state); |
| 888 | else { |
| 889 | /* |
| 890 | * pi_state->list is already empty. |
| 891 | * clear pi_state->owner. |
| 892 | * refcount is at 0 - put it back to 1. |
| 893 | */ |
| 894 | pi_state->owner = NULL; |
| 895 | atomic_set(&pi_state->refcount, 1); |
| 896 | current->pi_state_cache = pi_state; |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /* |
| 901 | * Look up the task based on what TID userspace gave us. |
| 902 | * We dont trust it. |
| 903 | */ |
| 904 | static struct task_struct * futex_find_get_task(pid_t pid) |
| 905 | { |
| 906 | struct task_struct *p; |
| 907 | |
Oleg Nesterov | d359b54 | 2006-09-29 02:00:55 -0700 | [diff] [blame] | 908 | rcu_read_lock(); |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 909 | p = find_task_by_vpid(pid); |
Michal Hocko | 7a0ea09 | 2010-06-30 09:51:19 +0200 | [diff] [blame] | 910 | if (p) |
| 911 | get_task_struct(p); |
Thomas Gleixner | a06381f | 2007-06-23 11:48:40 +0200 | [diff] [blame] | 912 | |
Oleg Nesterov | d359b54 | 2006-09-29 02:00:55 -0700 | [diff] [blame] | 913 | rcu_read_unlock(); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 914 | |
| 915 | return p; |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * This task is holding PI mutexes at exit time => bad. |
| 920 | * Kernel cleans up PI-state, but userspace is likely hosed. |
| 921 | * (Robust-futex cleanup is separate and might save the day for userspace.) |
| 922 | */ |
Thomas Gleixner | 25f319b | 2021-02-01 10:01:33 +0000 | [diff] [blame] | 923 | static void exit_pi_state_list(struct task_struct *curr) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 924 | { |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 925 | struct list_head *next, *head = &curr->pi_state_list; |
| 926 | struct futex_pi_state *pi_state; |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 927 | struct futex_hash_bucket *hb; |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 928 | union futex_key key = FUTEX_KEY_INIT; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 929 | |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 930 | if (!futex_cmpxchg_enabled) |
| 931 | return; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 932 | /* |
| 933 | * We are a ZOMBIE and nobody can enqueue itself on |
| 934 | * pi_state_list anymore, but we have to be careful |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 935 | * versus waiters unqueueing themselves: |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 936 | */ |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 937 | raw_spin_lock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 938 | while (!list_empty(head)) { |
| 939 | |
| 940 | next = head->next; |
| 941 | pi_state = list_entry(next, struct futex_pi_state, list); |
| 942 | key = pi_state->key; |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 943 | hb = hash_futex(&key); |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 944 | raw_spin_unlock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 945 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 946 | spin_lock(&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 | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 949 | /* |
| 950 | * We dropped the pi-lock, so re-check whether this |
| 951 | * task still owns the PI-state: |
| 952 | */ |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 953 | if (head->next != next) { |
| 954 | spin_unlock(&hb->lock); |
| 955 | continue; |
| 956 | } |
| 957 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 958 | WARN_ON(pi_state->owner != curr); |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 959 | WARN_ON(list_empty(&pi_state->list)); |
| 960 | list_del_init(&pi_state->list); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 961 | pi_state->owner = NULL; |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 962 | raw_spin_unlock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 963 | |
Peter Zijlstra | 2c60d4a | 2021-02-03 13:45:30 +0000 | [diff] [blame] | 964 | rt_mutex_futex_unlock(&pi_state->pi_mutex); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 965 | |
| 966 | spin_unlock(&hb->lock); |
| 967 | |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 968 | raw_spin_lock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 969 | } |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 970 | raw_spin_unlock_irq(&curr->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 973 | /* |
| 974 | * We need to check the following states: |
| 975 | * |
| 976 | * Waiter | pi_state | pi->owner | uTID | uODIED | ? |
| 977 | * |
| 978 | * [1] NULL | --- | --- | 0 | 0/1 | Valid |
| 979 | * [2] NULL | --- | --- | >0 | 0/1 | Valid |
| 980 | * |
| 981 | * [3] Found | NULL | -- | Any | 0/1 | Invalid |
| 982 | * |
| 983 | * [4] Found | Found | NULL | 0 | 1 | Valid |
| 984 | * [5] Found | Found | NULL | >0 | 1 | Invalid |
| 985 | * |
| 986 | * [6] Found | Found | task | 0 | 1 | Valid |
| 987 | * |
| 988 | * [7] Found | Found | NULL | Any | 0 | Invalid |
| 989 | * |
| 990 | * [8] Found | Found | task | ==taskTID | 0/1 | Valid |
| 991 | * [9] Found | Found | task | 0 | 0 | Invalid |
| 992 | * [10] Found | Found | task | !=taskTID | 0/1 | Invalid |
| 993 | * |
| 994 | * [1] Indicates that the kernel can acquire the futex atomically. We |
| 995 | * came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit. |
| 996 | * |
| 997 | * [2] Valid, if TID does not belong to a kernel thread. If no matching |
| 998 | * thread is found then it indicates that the owner TID has died. |
| 999 | * |
| 1000 | * [3] Invalid. The waiter is queued on a non PI futex |
| 1001 | * |
| 1002 | * [4] Valid state after exit_robust_list(), which sets the user space |
| 1003 | * value to FUTEX_WAITERS | FUTEX_OWNER_DIED. |
| 1004 | * |
| 1005 | * [5] The user space value got manipulated between exit_robust_list() |
| 1006 | * and exit_pi_state_list() |
| 1007 | * |
| 1008 | * [6] Valid state after exit_pi_state_list() which sets the new owner in |
| 1009 | * the pi_state but cannot access the user space value. |
| 1010 | * |
| 1011 | * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set. |
| 1012 | * |
| 1013 | * [8] Owner and user space value match |
| 1014 | * |
| 1015 | * [9] There is no transient state which sets the user space TID to 0 |
| 1016 | * except exit_robust_list(), but this is indicated by the |
| 1017 | * FUTEX_OWNER_DIED bit. See [4] |
| 1018 | * |
| 1019 | * [10] There is no transient state which leaves owner and user space |
Thomas Gleixner | b960d9a | 2021-02-03 13:45:39 +0000 | [diff] [blame] | 1020 | * TID out of sync. Except one error case where the kernel is denied |
| 1021 | * write access to the user address, see fixup_pi_state_owner(). |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1022 | * |
| 1023 | * |
| 1024 | * Serialization and lifetime rules: |
| 1025 | * |
| 1026 | * hb->lock: |
| 1027 | * |
| 1028 | * hb -> futex_q, relation |
| 1029 | * futex_q -> pi_state, relation |
| 1030 | * |
| 1031 | * (cannot be raw because hb can contain arbitrary amount |
| 1032 | * of futex_q's) |
| 1033 | * |
| 1034 | * pi_mutex->wait_lock: |
| 1035 | * |
| 1036 | * {uval, pi_state} |
| 1037 | * |
| 1038 | * (and pi_mutex 'obviously') |
| 1039 | * |
| 1040 | * p->pi_lock: |
| 1041 | * |
| 1042 | * p->pi_state_list -> pi_state->list, relation |
| 1043 | * |
| 1044 | * pi_state->refcount: |
| 1045 | * |
| 1046 | * pi_state lifetime |
| 1047 | * |
| 1048 | * |
| 1049 | * Lock order: |
| 1050 | * |
| 1051 | * hb->lock |
| 1052 | * pi_mutex->wait_lock |
| 1053 | * p->pi_lock |
| 1054 | * |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 1055 | */ |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* |
| 1058 | * Validate that the existing waiter has a pi_state and sanity check |
| 1059 | * the pi_state against the user space value. If correct, attach to |
| 1060 | * it. |
| 1061 | */ |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1062 | static int attach_to_pi_state(u32 __user *uaddr, u32 uval, |
| 1063 | struct futex_pi_state *pi_state, |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1064 | struct futex_pi_state **ps) |
| 1065 | { |
| 1066 | pid_t pid = uval & FUTEX_TID_MASK; |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1067 | int ret, uval2; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1068 | |
| 1069 | /* |
| 1070 | * Userspace might have messed up non-PI and PI futexes [3] |
| 1071 | */ |
| 1072 | if (unlikely(!pi_state)) |
| 1073 | return -EINVAL; |
| 1074 | |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1075 | /* |
| 1076 | * We get here with hb->lock held, and having found a |
| 1077 | * futex_top_waiter(). This means that futex_lock_pi() of said futex_q |
| 1078 | * has dropped the hb->lock in between queue_me() and unqueue_me_pi(), |
| 1079 | * which in turn means that futex_lock_pi() still has a reference on |
| 1080 | * our pi_state. |
| 1081 | */ |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1082 | WARN_ON(!atomic_read(&pi_state->refcount)); |
| 1083 | |
| 1084 | /* |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1085 | * Now that we have a pi_state, we can acquire wait_lock |
| 1086 | * and do the state validation. |
| 1087 | */ |
| 1088 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 1089 | |
| 1090 | /* |
| 1091 | * Since {uval, pi_state} is serialized by wait_lock, and our current |
| 1092 | * uval was read without holding it, it can have changed. Verify it |
| 1093 | * still is what we expect it to be, otherwise retry the entire |
| 1094 | * operation. |
| 1095 | */ |
| 1096 | if (get_futex_value_locked(&uval2, uaddr)) |
| 1097 | goto out_efault; |
| 1098 | |
| 1099 | if (uval != uval2) |
| 1100 | goto out_eagain; |
| 1101 | |
| 1102 | /* |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1103 | * Handle the owner died case: |
| 1104 | */ |
| 1105 | if (uval & FUTEX_OWNER_DIED) { |
| 1106 | /* |
| 1107 | * exit_pi_state_list sets owner to NULL and wakes the |
| 1108 | * topmost waiter. The task which acquires the |
| 1109 | * pi_state->rt_mutex will fixup owner. |
| 1110 | */ |
| 1111 | if (!pi_state->owner) { |
| 1112 | /* |
| 1113 | * No pi state owner, but the user space TID |
| 1114 | * is not 0. Inconsistent state. [5] |
| 1115 | */ |
| 1116 | if (pid) |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1117 | goto out_einval; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1118 | /* |
| 1119 | * Take a ref on the state and return success. [4] |
| 1120 | */ |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1121 | goto out_attach; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * If TID is 0, then either the dying owner has not |
| 1126 | * yet executed exit_pi_state_list() or some waiter |
| 1127 | * acquired the rtmutex in the pi state, but did not |
| 1128 | * yet fixup the TID in user space. |
| 1129 | * |
| 1130 | * Take a ref on the state and return success. [6] |
| 1131 | */ |
| 1132 | if (!pid) |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1133 | goto out_attach; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1134 | } else { |
| 1135 | /* |
| 1136 | * If the owner died bit is not set, then the pi_state |
| 1137 | * must have an owner. [7] |
| 1138 | */ |
| 1139 | if (!pi_state->owner) |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1140 | goto out_einval; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /* |
| 1144 | * Bail out if user space manipulated the futex value. If pi |
| 1145 | * state exists then the owner TID must be the same as the |
| 1146 | * user space TID. [9/10] |
| 1147 | */ |
| 1148 | if (pid != task_pid_vnr(pi_state->owner)) |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1149 | goto out_einval; |
| 1150 | |
| 1151 | out_attach: |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1152 | atomic_inc(&pi_state->refcount); |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1153 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1154 | *ps = pi_state; |
| 1155 | return 0; |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1156 | |
| 1157 | out_einval: |
| 1158 | ret = -EINVAL; |
| 1159 | goto out_error; |
| 1160 | |
| 1161 | out_eagain: |
| 1162 | ret = -EAGAIN; |
| 1163 | goto out_error; |
| 1164 | |
| 1165 | out_efault: |
| 1166 | ret = -EFAULT; |
| 1167 | goto out_error; |
| 1168 | |
| 1169 | out_error: |
| 1170 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 1171 | return ret; |
Thomas Gleixner | e60cbc5 | 2014-06-11 20:45:39 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1174 | /** |
| 1175 | * wait_for_owner_exiting - Block until the owner has exited |
| 1176 | * @exiting: Pointer to the exiting task |
| 1177 | * |
| 1178 | * Caller must hold a refcount on @exiting. |
| 1179 | */ |
| 1180 | static void wait_for_owner_exiting(int ret, struct task_struct *exiting) |
| 1181 | { |
| 1182 | if (ret != -EBUSY) { |
| 1183 | WARN_ON_ONCE(exiting); |
| 1184 | return; |
| 1185 | } |
| 1186 | |
| 1187 | if (WARN_ON_ONCE(ret == -EBUSY && !exiting)) |
| 1188 | return; |
| 1189 | |
| 1190 | mutex_lock(&exiting->futex_exit_mutex); |
| 1191 | /* |
| 1192 | * No point in doing state checking here. If the waiter got here |
| 1193 | * while the task was in exec()->exec_futex_release() then it can |
| 1194 | * have any FUTEX_STATE_* value when the waiter has acquired the |
| 1195 | * mutex. OK, if running, EXITING or DEAD if it reached exit() |
| 1196 | * already. Highly unlikely and not a problem. Just one more round |
| 1197 | * through the futex maze. |
| 1198 | */ |
| 1199 | mutex_unlock(&exiting->futex_exit_mutex); |
| 1200 | |
| 1201 | put_task_struct(exiting); |
| 1202 | } |
| 1203 | |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1204 | static int handle_exit_race(u32 __user *uaddr, u32 uval, |
| 1205 | struct task_struct *tsk) |
| 1206 | { |
| 1207 | u32 uval2; |
| 1208 | |
| 1209 | /* |
Thomas Gleixner | 91509e8 | 2021-02-24 18:09:23 +0800 | [diff] [blame] | 1210 | * If the futex exit state is not yet FUTEX_STATE_DEAD, tell the |
| 1211 | * caller that the alleged owner is busy. |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1212 | */ |
| 1213 | if (tsk && tsk->futex_state != FUTEX_STATE_DEAD) |
Thomas Gleixner | 91509e8 | 2021-02-24 18:09:23 +0800 | [diff] [blame] | 1214 | return -EBUSY; |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1215 | |
| 1216 | /* |
| 1217 | * Reread the user space value to handle the following situation: |
| 1218 | * |
| 1219 | * CPU0 CPU1 |
| 1220 | * |
| 1221 | * sys_exit() sys_futex() |
| 1222 | * do_exit() futex_lock_pi() |
| 1223 | * futex_lock_pi_atomic() |
| 1224 | * exit_signals(tsk) No waiters: |
| 1225 | * tsk->flags |= PF_EXITING; *uaddr == 0x00000PID |
| 1226 | * mm_release(tsk) Set waiter bit |
| 1227 | * exit_robust_list(tsk) { *uaddr = 0x80000PID; |
| 1228 | * Set owner died attach_to_pi_owner() { |
| 1229 | * *uaddr = 0xC0000000; tsk = get_task(PID); |
| 1230 | * } if (!tsk->flags & PF_EXITING) { |
| 1231 | * ... attach(); |
| 1232 | * tsk->futex_state = } else { |
| 1233 | * FUTEX_STATE_DEAD; if (tsk->futex_state != |
| 1234 | * FUTEX_STATE_DEAD) |
| 1235 | * return -EAGAIN; |
| 1236 | * return -ESRCH; <--- FAIL |
| 1237 | * } |
| 1238 | * |
| 1239 | * Returning ESRCH unconditionally is wrong here because the |
| 1240 | * user space value has been changed by the exiting task. |
| 1241 | * |
| 1242 | * The same logic applies to the case where the exiting task is |
| 1243 | * already gone. |
| 1244 | */ |
| 1245 | if (get_futex_value_locked(&uval2, uaddr)) |
| 1246 | return -EFAULT; |
| 1247 | |
| 1248 | /* If the user space value has changed, try again. */ |
| 1249 | if (uval2 != uval) |
| 1250 | return -EAGAIN; |
| 1251 | |
| 1252 | /* |
| 1253 | * The exiting task did not have a robust list, the robust list was |
| 1254 | * corrupted or the user space value in *uaddr is simply bogus. |
| 1255 | * Give up and tell user space. |
| 1256 | */ |
| 1257 | return -ESRCH; |
| 1258 | } |
| 1259 | |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1260 | /* |
| 1261 | * Lookup the task for the TID provided from user space and attach to |
| 1262 | * it after doing proper sanity checks. |
| 1263 | */ |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1264 | static int attach_to_pi_owner(u32 __user *uaddr, u32 uval, union futex_key *key, |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1265 | struct futex_pi_state **ps, |
| 1266 | struct task_struct **exiting) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1267 | { |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1268 | pid_t pid = uval & FUTEX_TID_MASK; |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1269 | struct futex_pi_state *pi_state; |
| 1270 | struct task_struct *p; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1271 | |
| 1272 | /* |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 1273 | * 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] | 1274 | * the new pi_state to it, but bail out when TID = 0 [1] |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1275 | * |
| 1276 | * The !pid check is paranoid. None of the call sites should end up |
| 1277 | * with pid == 0, but better safe than sorry. Let the caller retry |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1278 | */ |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1279 | if (!pid) |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1280 | return -EAGAIN; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1281 | p = futex_find_get_task(pid); |
Michal Hocko | 7a0ea09 | 2010-06-30 09:51:19 +0200 | [diff] [blame] | 1282 | if (!p) |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1283 | return handle_exit_race(uaddr, uval, NULL); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1284 | |
Oleg Nesterov | a212946 | 2015-02-02 15:05:36 +0100 | [diff] [blame] | 1285 | if (unlikely(p->flags & PF_KTHREAD)) { |
Thomas Gleixner | f0d71b3 | 2014-05-12 20:45:35 +0000 | [diff] [blame] | 1286 | put_task_struct(p); |
| 1287 | return -EPERM; |
| 1288 | } |
| 1289 | |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1290 | /* |
Thomas Gleixner | 2c11689 | 2021-02-01 10:01:34 +0000 | [diff] [blame] | 1291 | * We need to look at the task state to figure out, whether the |
| 1292 | * task is exiting. To protect against the change of the task state |
| 1293 | * in futex_exit_release(), we do this protected by p->pi_lock: |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1294 | */ |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 1295 | raw_spin_lock_irq(&p->pi_lock); |
Thomas Gleixner | 2c11689 | 2021-02-01 10:01:34 +0000 | [diff] [blame] | 1296 | if (unlikely(p->futex_state != FUTEX_STATE_OK)) { |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1297 | /* |
Thomas Gleixner | 2c11689 | 2021-02-01 10:01:34 +0000 | [diff] [blame] | 1298 | * The task is on the way out. When the futex state is |
| 1299 | * FUTEX_STATE_DEAD, we know that the task has finished |
| 1300 | * the cleanup: |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1301 | */ |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1302 | int ret = handle_exit_race(uaddr, uval, p); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1303 | |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 1304 | raw_spin_unlock_irq(&p->pi_lock); |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1305 | /* |
| 1306 | * If the owner task is between FUTEX_STATE_EXITING and |
| 1307 | * FUTEX_STATE_DEAD then store the task pointer and keep |
| 1308 | * the reference on the task struct. The calling code will |
| 1309 | * drop all locks, wait for the task to reach |
| 1310 | * FUTEX_STATE_DEAD and then drop the refcount. This is |
| 1311 | * required to prevent a live lock when the current task |
| 1312 | * preempted the exiting task between the two states. |
| 1313 | */ |
| 1314 | if (ret == -EBUSY) |
| 1315 | *exiting = p; |
| 1316 | else |
| 1317 | put_task_struct(p); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1318 | return ret; |
| 1319 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1320 | |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 1321 | /* |
| 1322 | * No existing pi state. First waiter. [2] |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1323 | * |
| 1324 | * This creates pi_state, we have hb->lock held, this means nothing can |
| 1325 | * observe this state, wait_lock is irrelevant. |
Thomas Gleixner | 54a2178 | 2014-06-03 12:27:08 +0000 | [diff] [blame] | 1326 | */ |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1327 | pi_state = alloc_pi_state(); |
| 1328 | |
| 1329 | /* |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1330 | * Initialize the pi_mutex in locked state and make @p |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1331 | * the owner of it: |
| 1332 | */ |
| 1333 | rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p); |
| 1334 | |
| 1335 | /* Store the key for possible exit cleanups: */ |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 1336 | pi_state->key = *key; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1337 | |
Ingo Molnar | 627371d | 2006-07-29 05:16:20 +0200 | [diff] [blame] | 1338 | WARN_ON(!list_empty(&pi_state->list)); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1339 | list_add(&pi_state->list, &p->pi_state_list); |
| 1340 | pi_state->owner = p; |
Thomas Gleixner | 1d61548 | 2009-11-17 14:54:03 +0100 | [diff] [blame] | 1341 | raw_spin_unlock_irq(&p->pi_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1342 | |
| 1343 | put_task_struct(p); |
| 1344 | |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 1345 | *ps = pi_state; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1350 | static int lookup_pi_state(u32 __user *uaddr, u32 uval, |
| 1351 | struct futex_hash_bucket *hb, |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1352 | union futex_key *key, struct futex_pi_state **ps, |
| 1353 | struct task_struct **exiting) |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1354 | { |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 1355 | struct futex_q *top_waiter = futex_top_waiter(hb, key); |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * If there is a waiter on that futex, validate it and |
| 1359 | * attach to the pi_state when the validation succeeds. |
| 1360 | */ |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 1361 | if (top_waiter) |
| 1362 | return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps); |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1363 | |
| 1364 | /* |
| 1365 | * We are the first waiter - try to look up the owner based on |
| 1366 | * @uval and attach to it. |
| 1367 | */ |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1368 | return attach_to_pi_owner(uaddr, uval, key, ps, exiting); |
Thomas Gleixner | 04e1b2e | 2014-06-11 20:45:40 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1371 | static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval) |
| 1372 | { |
| 1373 | u32 uninitialized_var(curval); |
| 1374 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1375 | if (unlikely(should_fail_futex(true))) |
| 1376 | return -EFAULT; |
| 1377 | |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1378 | if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))) |
| 1379 | return -EFAULT; |
| 1380 | |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1381 | /* If user space value changed, let the caller retry */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1382 | return curval != uval ? -EAGAIN : 0; |
| 1383 | } |
| 1384 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1385 | /** |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 1386 | * 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] | 1387 | * @uaddr: the pi futex user address |
| 1388 | * @hb: the pi futex hash bucket |
| 1389 | * @key: the futex key associated with uaddr and hb |
| 1390 | * @ps: the pi_state pointer where we store the result of the |
| 1391 | * lookup |
| 1392 | * @task: the task to perform the atomic lock work for. This will |
| 1393 | * be "current" except in the case of requeue pi. |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1394 | * @exiting: Pointer to store the task pointer of the owner task |
| 1395 | * which is in the middle of exiting |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1396 | * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1397 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 1398 | * Return: |
| 1399 | * 0 - ready to wait; |
| 1400 | * 1 - acquired the lock; |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1401 | * <0 - error |
| 1402 | * |
| 1403 | * The hb->lock and futex_key refs shall be held by the caller. |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1404 | * |
| 1405 | * @exiting is only set when the return value is -EBUSY. If so, this holds |
| 1406 | * a refcount on the exiting task on return and the caller needs to drop it |
| 1407 | * after waiting for the exit to complete. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1408 | */ |
| 1409 | static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb, |
| 1410 | union futex_key *key, |
| 1411 | struct futex_pi_state **ps, |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1412 | struct task_struct *task, |
| 1413 | struct task_struct **exiting, |
| 1414 | int set_waiters) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1415 | { |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1416 | u32 uval, newval, vpid = task_pid_vnr(task); |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 1417 | struct futex_q *top_waiter; |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1418 | int ret; |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1419 | |
| 1420 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1421 | * Read the user space value first so we can validate a few |
| 1422 | * things before proceeding further. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1423 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1424 | if (get_futex_value_locked(&uval, uaddr)) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1425 | return -EFAULT; |
| 1426 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1427 | if (unlikely(should_fail_futex(true))) |
| 1428 | return -EFAULT; |
| 1429 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1430 | /* |
| 1431 | * Detect deadlocks. |
| 1432 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1433 | if ((unlikely((uval & FUTEX_TID_MASK) == vpid))) |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1434 | return -EDEADLK; |
| 1435 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1436 | if ((unlikely(should_fail_futex(true)))) |
| 1437 | return -EDEADLK; |
| 1438 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1439 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1440 | * Lookup existing state first. If it exists, try to attach to |
| 1441 | * its pi_state. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1442 | */ |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 1443 | top_waiter = futex_top_waiter(hb, key); |
| 1444 | if (top_waiter) |
| 1445 | return attach_to_pi_state(uaddr, uval, top_waiter->pi_state, ps); |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1446 | |
| 1447 | /* |
| 1448 | * No waiter and user TID is 0. We are here because the |
| 1449 | * waiters or the owner died bit is set or called from |
| 1450 | * requeue_cmp_pi or for whatever reason something took the |
| 1451 | * syscall. |
| 1452 | */ |
| 1453 | if (!(uval & FUTEX_TID_MASK)) { |
Thomas Gleixner | b3eaa9f | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 1454 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1455 | * We take over the futex. No other waiters and the user space |
| 1456 | * TID is 0. We preserve the owner died bit. |
Thomas Gleixner | b3eaa9f | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 1457 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1458 | newval = uval & FUTEX_OWNER_DIED; |
| 1459 | newval |= vpid; |
| 1460 | |
| 1461 | /* The futex requeue_pi code can enforce the waiters bit */ |
| 1462 | if (set_waiters) |
| 1463 | newval |= FUTEX_WAITERS; |
| 1464 | |
| 1465 | ret = lock_pi_update_atomic(uaddr, uval, newval); |
| 1466 | /* If the take over worked, return 1 */ |
| 1467 | return ret < 0 ? ret : 1; |
Thomas Gleixner | b3eaa9f | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 1468 | } |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1469 | |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1470 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1471 | * First waiter. Set the waiters bit before attaching ourself to |
| 1472 | * the owner. If owner tries to unlock, it will be forced into |
| 1473 | * the kernel and blocked on hb->lock. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1474 | */ |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1475 | newval = uval | FUTEX_WAITERS; |
| 1476 | ret = lock_pi_update_atomic(uaddr, uval, newval); |
| 1477 | if (ret) |
| 1478 | return ret; |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1479 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 1480 | * If the update of the user space value succeeded, we try to |
| 1481 | * attach to the owner. If that fails, no harm done, we only |
| 1482 | * set the FUTEX_WAITERS bit in the user space variable. |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1483 | */ |
Thomas Gleixner | 9c3f398 | 2021-02-11 09:27:00 +0000 | [diff] [blame] | 1484 | return attach_to_pi_owner(uaddr, newval, key, ps, exiting); |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1487 | /** |
| 1488 | * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket |
| 1489 | * @q: The futex_q to unqueue |
| 1490 | * |
| 1491 | * The q->lock_ptr must not be NULL and must be held by the caller. |
| 1492 | */ |
| 1493 | static void __unqueue_futex(struct futex_q *q) |
| 1494 | { |
| 1495 | struct futex_hash_bucket *hb; |
| 1496 | |
Steven Rostedt | 2909620 | 2011-03-17 15:21:07 -0400 | [diff] [blame] | 1497 | if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr)) |
| 1498 | || WARN_ON(plist_node_empty(&q->list))) |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1499 | return; |
| 1500 | |
| 1501 | hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock); |
| 1502 | plist_del(&q->list, &hb->chain); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 1503 | hb_waiters_dec(hb); |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1504 | } |
| 1505 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1506 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | * The hash bucket lock must be held when this is called. |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1508 | * Afterwards, the futex_q must not be accessed. Callers |
| 1509 | * must ensure to later call wake_up_q() for the actual |
| 1510 | * wakeups to occur. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | */ |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1512 | 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] | 1513 | { |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 1514 | struct task_struct *p = q->task; |
| 1515 | |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1516 | if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n")) |
| 1517 | return; |
| 1518 | |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 1519 | /* |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1520 | * Queue the task for later wakeup for after we've released |
| 1521 | * the hb->lock. wake_q_add() grabs reference to p. |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 1522 | */ |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1523 | wake_q_add(wake_q, p); |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1524 | __unqueue_futex(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | /* |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 1526 | * The waiting task can free the futex_q as soon as |
| 1527 | * q->lock_ptr = NULL is written, without taking any locks. A |
| 1528 | * memory barrier is required here to prevent the following |
| 1529 | * store to lock_ptr from getting ahead of the plist_del. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | */ |
Ralf Baechle | ccdea2f | 2006-12-06 20:40:26 -0800 | [diff] [blame] | 1531 | smp_wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | q->lock_ptr = NULL; |
| 1533 | } |
| 1534 | |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 1535 | static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *top_waiter, |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 1536 | struct futex_hash_bucket *hb) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1537 | { |
| 1538 | struct task_struct *new_owner; |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 1539 | struct futex_pi_state *pi_state = top_waiter->pi_state; |
Vitaliy Ivanov | 7cfdaf3 | 2011-07-07 15:10:31 +0300 | [diff] [blame] | 1540 | u32 uninitialized_var(curval), newval; |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 1541 | WAKE_Q(wake_q); |
| 1542 | bool deboost; |
Thomas Gleixner | 13fbca4 | 2014-06-03 12:27:07 +0000 | [diff] [blame] | 1543 | int ret = 0; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1544 | |
| 1545 | if (!pi_state) |
| 1546 | return -EINVAL; |
| 1547 | |
Thomas Gleixner | 51246bf | 2010-02-02 11:40:27 +0100 | [diff] [blame] | 1548 | /* |
| 1549 | * If current does not own the pi_state then the futex is |
| 1550 | * inconsistent and user space fiddled with the futex value. |
| 1551 | */ |
| 1552 | if (pi_state->owner != current) |
| 1553 | return -EINVAL; |
| 1554 | |
Thomas Gleixner | b4abf91 | 2016-01-13 11:25:38 +0100 | [diff] [blame] | 1555 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1556 | new_owner = rt_mutex_next_owner(&pi_state->pi_mutex); |
| 1557 | |
| 1558 | /* |
Peter Zijlstra | 71f093c | 2021-02-03 13:45:32 +0000 | [diff] [blame] | 1559 | * When we interleave with futex_lock_pi() where it does |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 1560 | * rt_mutex_timed_futex_lock(), we might observe @top_waiter futex_q waiter, |
Peter Zijlstra | 71f093c | 2021-02-03 13:45:32 +0000 | [diff] [blame] | 1561 | * but the rt_mutex's wait_list can be empty (either still, or again, |
| 1562 | * depending on which side we land). |
| 1563 | * |
| 1564 | * When this happens, give up our locks and try again, giving the |
| 1565 | * futex_lock_pi() instance time to complete, either by waiting on the |
| 1566 | * rtmutex or removing itself from the futex queue. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1567 | */ |
Peter Zijlstra | 71f093c | 2021-02-03 13:45:32 +0000 | [diff] [blame] | 1568 | if (!new_owner) { |
| 1569 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 1570 | return -EAGAIN; |
| 1571 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1572 | |
| 1573 | /* |
Thomas Gleixner | 13fbca4 | 2014-06-03 12:27:07 +0000 | [diff] [blame] | 1574 | * We pass it to the next owner. The WAITERS bit is always |
| 1575 | * kept enabled while there is PI state around. We cleanup the |
| 1576 | * owner died bit, because we are the owner. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1577 | */ |
Thomas Gleixner | 13fbca4 | 2014-06-03 12:27:07 +0000 | [diff] [blame] | 1578 | newval = FUTEX_WAITERS | task_pid_vnr(new_owner); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 1579 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1580 | if (unlikely(should_fail_futex(true))) |
| 1581 | ret = -EFAULT; |
| 1582 | |
Sebastian Andrzej Siewior | 89e9e66 | 2016-04-15 14:35:39 +0200 | [diff] [blame] | 1583 | if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) { |
Thomas Gleixner | 13fbca4 | 2014-06-03 12:27:07 +0000 | [diff] [blame] | 1584 | ret = -EFAULT; |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 1585 | |
Sebastian Andrzej Siewior | 89e9e66 | 2016-04-15 14:35:39 +0200 | [diff] [blame] | 1586 | } else if (curval != uval) { |
| 1587 | /* |
| 1588 | * If a unconditional UNLOCK_PI operation (user space did not |
| 1589 | * try the TID->0 transition) raced with a waiter setting the |
| 1590 | * FUTEX_WAITERS flag between get_user() and locking the hash |
| 1591 | * bucket lock, retry the operation. |
| 1592 | */ |
| 1593 | if ((FUTEX_TID_MASK & curval) == uval) |
| 1594 | ret = -EAGAIN; |
| 1595 | else |
| 1596 | ret = -EINVAL; |
| 1597 | } |
Thomas Gleixner | 76bc0ec | 2021-02-03 13:45:35 +0000 | [diff] [blame] | 1598 | |
| 1599 | if (!ret) { |
| 1600 | /* |
| 1601 | * This is a point of no return; once we modified the uval |
| 1602 | * there is no going back and subsequent operations must |
| 1603 | * not fail. |
| 1604 | */ |
| 1605 | pi_state_update_owner(pi_state, new_owner); |
| 1606 | deboost = __rt_mutex_futex_unlock(&pi_state->pi_mutex, &wake_q); |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 1607 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1608 | |
Peter Zijlstra | 2c60d4a | 2021-02-03 13:45:30 +0000 | [diff] [blame] | 1609 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 1610 | spin_unlock(&hb->lock); |
Peter Zijlstra | 2c60d4a | 2021-02-03 13:45:30 +0000 | [diff] [blame] | 1611 | |
| 1612 | if (deboost) { |
| 1613 | wake_up_q(&wake_q); |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 1614 | rt_mutex_adjust_prio(current); |
Peter Zijlstra | 2c60d4a | 2021-02-03 13:45:30 +0000 | [diff] [blame] | 1615 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 1616 | |
| 1617 | return 0; |
| 1618 | } |
| 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | /* |
Ingo Molnar | 8b8f319 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1621 | * Express the locking dependencies for lockdep: |
| 1622 | */ |
| 1623 | static inline void |
| 1624 | double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2) |
| 1625 | { |
| 1626 | if (hb1 <= hb2) { |
| 1627 | spin_lock(&hb1->lock); |
| 1628 | if (hb1 < hb2) |
| 1629 | spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING); |
| 1630 | } else { /* hb1 > hb2 */ |
| 1631 | spin_lock(&hb2->lock); |
| 1632 | spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING); |
| 1633 | } |
| 1634 | } |
| 1635 | |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1636 | static inline void |
| 1637 | double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2) |
| 1638 | { |
Darren Hart | f061d35 | 2009-03-12 15:11:18 -0700 | [diff] [blame] | 1639 | spin_unlock(&hb1->lock); |
Ingo Molnar | 88f502f | 2009-03-13 10:32:07 +0100 | [diff] [blame] | 1640 | if (hb1 != hb2) |
| 1641 | spin_unlock(&hb2->lock); |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
Ingo Molnar | 8b8f319 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 1644 | /* |
Darren Hart | b2d0994 | 2009-03-12 00:55:37 -0700 | [diff] [blame] | 1645 | * Wake up waiters matching bitset queued on this futex (uaddr). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 1647 | static int |
| 1648 | 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] | 1649 | { |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1650 | struct futex_hash_bucket *hb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | struct futex_q *this, *next; |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 1652 | union futex_key key = FUTEX_KEY_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | int ret; |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1654 | WAKE_Q(wake_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 1656 | if (!bitset) |
| 1657 | return -EINVAL; |
| 1658 | |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 1659 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | if (unlikely(ret != 0)) |
| 1661 | goto out; |
| 1662 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1663 | hb = hash_futex(&key); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 1664 | |
| 1665 | /* Make sure we really have tasks to wakeup */ |
| 1666 | if (!hb_waiters_pending(hb)) |
| 1667 | goto out_put_key; |
| 1668 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1669 | spin_lock(&hb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 1671 | plist_for_each_entry_safe(this, next, &hb->chain, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | if (match_futex (&this->key, &key)) { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1673 | if (this->pi_state || this->rt_waiter) { |
Ingo Molnar | ed6f7b1 | 2006-07-01 04:35:46 -0700 | [diff] [blame] | 1674 | ret = -EINVAL; |
| 1675 | break; |
| 1676 | } |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 1677 | |
| 1678 | /* Check if one of the bits is set in both bitsets */ |
| 1679 | if (!(this->bitset & bitset)) |
| 1680 | continue; |
| 1681 | |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1682 | mark_wake_futex(&wake_q, this); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | if (++ret >= nr_wake) |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
| 1687 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1688 | spin_unlock(&hb->lock); |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1689 | wake_up_q(&wake_q); |
Davidlohr Bueso | b0c29f7 | 2014-01-12 15:31:25 -0800 | [diff] [blame] | 1690 | out_put_key: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 1691 | put_futex_key(&key); |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 1692 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | return ret; |
| 1694 | } |
| 1695 | |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1696 | static int futex_atomic_op_inuser(unsigned int encoded_op, u32 __user *uaddr) |
| 1697 | { |
| 1698 | unsigned int op = (encoded_op & 0x70000000) >> 28; |
| 1699 | unsigned int cmp = (encoded_op & 0x0f000000) >> 24; |
Jiri Slaby | a164009 | 2017-11-30 15:35:44 +0100 | [diff] [blame] | 1700 | int oparg = sign_extend32((encoded_op & 0x00fff000) >> 12, 11); |
| 1701 | int cmparg = sign_extend32(encoded_op & 0x00000fff, 11); |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1702 | int oldval, ret; |
| 1703 | |
| 1704 | if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28)) { |
Jiri Slaby | da92e8a | 2017-10-23 13:41:51 +0200 | [diff] [blame] | 1705 | if (oparg < 0 || oparg > 31) { |
| 1706 | char comm[sizeof(current->comm)]; |
| 1707 | /* |
| 1708 | * kill this print and return -EINVAL when userspace |
| 1709 | * is sane again |
| 1710 | */ |
| 1711 | pr_info_ratelimited("futex_wake_op: %s tries to shift op by %d; fix this program\n", |
| 1712 | get_task_comm(comm, current), oparg); |
| 1713 | oparg &= 31; |
| 1714 | } |
Jiri Slaby | 81da9f8 | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 1715 | oparg = 1 << oparg; |
| 1716 | } |
| 1717 | |
| 1718 | if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))) |
| 1719 | return -EFAULT; |
| 1720 | |
| 1721 | ret = arch_futex_atomic_op_inuser(op, oparg, &oldval, uaddr); |
| 1722 | if (ret) |
| 1723 | return ret; |
| 1724 | |
| 1725 | switch (cmp) { |
| 1726 | case FUTEX_OP_CMP_EQ: |
| 1727 | return oldval == cmparg; |
| 1728 | case FUTEX_OP_CMP_NE: |
| 1729 | return oldval != cmparg; |
| 1730 | case FUTEX_OP_CMP_LT: |
| 1731 | return oldval < cmparg; |
| 1732 | case FUTEX_OP_CMP_GE: |
| 1733 | return oldval >= cmparg; |
| 1734 | case FUTEX_OP_CMP_LE: |
| 1735 | return oldval <= cmparg; |
| 1736 | case FUTEX_OP_CMP_GT: |
| 1737 | return oldval > cmparg; |
| 1738 | default: |
| 1739 | return -ENOSYS; |
| 1740 | } |
| 1741 | } |
| 1742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | /* |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1744 | * Wake up all waiters hashed on the physical page that is mapped |
| 1745 | * to this virtual address: |
| 1746 | */ |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1747 | static int |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 1748 | futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2, |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1749 | int nr_wake, int nr_wake2, int op) |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1750 | { |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 1751 | union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1752 | struct futex_hash_bucket *hb1, *hb2; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1753 | struct futex_q *this, *next; |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1754 | int ret, op_ret; |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1755 | WAKE_Q(wake_q); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1756 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1757 | retry: |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 1758 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1759 | if (unlikely(ret != 0)) |
| 1760 | goto out; |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 1761 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1762 | if (unlikely(ret != 0)) |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 1763 | goto out_put_key1; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1764 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1765 | hb1 = hash_futex(&key1); |
| 1766 | hb2 = hash_futex(&key2); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1767 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1768 | retry_private: |
Thomas Gleixner | eaaea80 | 2009-10-04 09:34:17 +0200 | [diff] [blame] | 1769 | double_lock_hb(hb1, hb2); |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1770 | op_ret = futex_atomic_op_inuser(op, uaddr2); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1771 | if (unlikely(op_ret < 0)) { |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1772 | |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1773 | double_unlock_hb(hb1, hb2); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1774 | |
David Howells | 7ee1dd3 | 2006-01-06 00:11:44 -0800 | [diff] [blame] | 1775 | #ifndef CONFIG_MMU |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1776 | /* |
| 1777 | * we don't get EFAULT from MMU faults if we don't have an MMU, |
| 1778 | * but we might get them from range checking |
| 1779 | */ |
David Howells | 7ee1dd3 | 2006-01-06 00:11:44 -0800 | [diff] [blame] | 1780 | ret = op_ret; |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 1781 | goto out_put_keys; |
David Howells | 7ee1dd3 | 2006-01-06 00:11:44 -0800 | [diff] [blame] | 1782 | #endif |
| 1783 | |
David Gibson | 796f8d9 | 2005-11-07 00:59:33 -0800 | [diff] [blame] | 1784 | if (unlikely(op_ret != -EFAULT)) { |
| 1785 | ret = op_ret; |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 1786 | goto out_put_keys; |
David Gibson | 796f8d9 | 2005-11-07 00:59:33 -0800 | [diff] [blame] | 1787 | } |
| 1788 | |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 1789 | ret = fault_in_user_writeable(uaddr2); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1790 | if (ret) |
Darren Hart | de87fcc | 2009-03-12 00:55:46 -0700 | [diff] [blame] | 1791 | goto out_put_keys; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1792 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 1793 | if (!(flags & FLAGS_SHARED)) |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1794 | goto retry_private; |
| 1795 | |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 1796 | put_futex_key(&key2); |
| 1797 | put_futex_key(&key1); |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 1798 | goto retry; |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1799 | } |
| 1800 | |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 1801 | plist_for_each_entry_safe(this, next, &hb1->chain, list) { |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1802 | if (match_futex (&this->key, &key1)) { |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1803 | if (this->pi_state || this->rt_waiter) { |
| 1804 | ret = -EINVAL; |
| 1805 | goto out_unlock; |
| 1806 | } |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1807 | mark_wake_futex(&wake_q, this); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1808 | if (++ret >= nr_wake) |
| 1809 | break; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | if (op_ret > 0) { |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1814 | op_ret = 0; |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 1815 | plist_for_each_entry_safe(this, next, &hb2->chain, list) { |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1816 | if (match_futex (&this->key, &key2)) { |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1817 | if (this->pi_state || this->rt_waiter) { |
| 1818 | ret = -EINVAL; |
| 1819 | goto out_unlock; |
| 1820 | } |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1821 | mark_wake_futex(&wake_q, this); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1822 | if (++op_ret >= nr_wake2) |
| 1823 | break; |
| 1824 | } |
| 1825 | } |
| 1826 | ret += op_ret; |
| 1827 | } |
| 1828 | |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 1829 | out_unlock: |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 1830 | double_unlock_hb(hb1, hb2); |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 1831 | wake_up_q(&wake_q); |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 1832 | out_put_keys: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 1833 | put_futex_key(&key2); |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 1834 | out_put_key1: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 1835 | put_futex_key(&key1); |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 1836 | out: |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 1837 | return ret; |
| 1838 | } |
| 1839 | |
Darren Hart | 9121e47 | 2009-04-03 13:40:31 -0700 | [diff] [blame] | 1840 | /** |
| 1841 | * requeue_futex() - Requeue a futex_q from one hb to another |
| 1842 | * @q: the futex_q to requeue |
| 1843 | * @hb1: the source hash_bucket |
| 1844 | * @hb2: the target hash_bucket |
| 1845 | * @key2: the new key for the requeued futex_q |
| 1846 | */ |
| 1847 | static inline |
| 1848 | void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1, |
| 1849 | struct futex_hash_bucket *hb2, union futex_key *key2) |
| 1850 | { |
| 1851 | |
| 1852 | /* |
| 1853 | * If key1 and key2 hash to the same bucket, no need to |
| 1854 | * requeue. |
| 1855 | */ |
| 1856 | if (likely(&hb1->chain != &hb2->chain)) { |
| 1857 | plist_del(&q->list, &hb1->chain); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 1858 | hb_waiters_dec(hb1); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 1859 | hb_waiters_inc(hb2); |
Davidlohr Bueso | fe1bce9 | 2016-04-20 20:09:24 -0700 | [diff] [blame] | 1860 | plist_add(&q->list, &hb2->chain); |
Darren Hart | 9121e47 | 2009-04-03 13:40:31 -0700 | [diff] [blame] | 1861 | q->lock_ptr = &hb2->lock; |
Darren Hart | 9121e47 | 2009-04-03 13:40:31 -0700 | [diff] [blame] | 1862 | } |
| 1863 | get_futex_key_refs(key2); |
| 1864 | q->key = *key2; |
| 1865 | } |
| 1866 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1867 | /** |
| 1868 | * 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] | 1869 | * @q: the futex_q |
| 1870 | * @key: the key of the requeue target futex |
| 1871 | * @hb: the hash_bucket of the requeue target futex |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1872 | * |
| 1873 | * During futex_requeue, with requeue_pi=1, it is possible to acquire the |
| 1874 | * target futex if it is uncontended or via a lock steal. Set the futex_q key |
| 1875 | * to the requeue target futex so the waiter can detect the wakeup on the right |
| 1876 | * 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] | 1877 | * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock |
| 1878 | * to protect access to the pi_state to fixup the owner later. Must be called |
| 1879 | * with both q->lock_ptr and hb->lock held. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1880 | */ |
| 1881 | static inline |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1882 | void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key, |
| 1883 | struct futex_hash_bucket *hb) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1884 | { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1885 | get_futex_key_refs(key); |
| 1886 | q->key = *key; |
| 1887 | |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 1888 | __unqueue_futex(q); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1889 | |
| 1890 | WARN_ON(!q->rt_waiter); |
| 1891 | q->rt_waiter = NULL; |
| 1892 | |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1893 | q->lock_ptr = &hb->lock; |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1894 | |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 1895 | wake_up_state(q->task, TASK_NORMAL); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | /** |
| 1899 | * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1900 | * @pifutex: the user address of the to futex |
| 1901 | * @hb1: the from futex hash bucket, must be locked by the caller |
| 1902 | * @hb2: the to futex hash bucket, must be locked by the caller |
| 1903 | * @key1: the from futex key |
| 1904 | * @key2: the to futex key |
| 1905 | * @ps: address to store the pi_state pointer |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1906 | * @exiting: Pointer to store the task pointer of the owner task |
| 1907 | * which is in the middle of exiting |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1908 | * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1909 | * |
| 1910 | * 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] | 1911 | * Wake the top waiter if we succeed. If the caller specified set_waiters, |
| 1912 | * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit. |
| 1913 | * hb1 and hb2 must be held by the caller. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1914 | * |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1915 | * @exiting is only set when the return value is -EBUSY. If so, this holds |
| 1916 | * a refcount on the exiting task on return and the caller needs to drop it |
| 1917 | * after waiting for the exit to complete. |
| 1918 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 1919 | * Return: |
| 1920 | * 0 - failed to acquire the lock atomically; |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 1921 | * >0 - acquired the lock, return value is vpid of the top_waiter |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1922 | * <0 - error |
| 1923 | */ |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1924 | static int |
| 1925 | futex_proxy_trylock_atomic(u32 __user *pifutex, struct futex_hash_bucket *hb1, |
| 1926 | struct futex_hash_bucket *hb2, union futex_key *key1, |
| 1927 | union futex_key *key2, struct futex_pi_state **ps, |
| 1928 | struct task_struct **exiting, int set_waiters) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1929 | { |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1930 | struct futex_q *top_waiter = NULL; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1931 | u32 curval; |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 1932 | int ret, vpid; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1933 | |
| 1934 | if (get_futex_value_locked(&curval, pifutex)) |
| 1935 | return -EFAULT; |
| 1936 | |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 1937 | if (unlikely(should_fail_futex(true))) |
| 1938 | return -EFAULT; |
| 1939 | |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1940 | /* |
| 1941 | * Find the top_waiter and determine if there are additional waiters. |
| 1942 | * If the caller intends to requeue more than 1 waiter to pifutex, |
| 1943 | * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now, |
| 1944 | * as we have means to handle the possible fault. If not, don't set |
| 1945 | * the bit unecessarily as it will force the subsequent unlock to enter |
| 1946 | * the kernel. |
| 1947 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1948 | top_waiter = futex_top_waiter(hb1, key1); |
| 1949 | |
| 1950 | /* There are no waiters, nothing for us to do. */ |
| 1951 | if (!top_waiter) |
| 1952 | return 0; |
| 1953 | |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 1954 | /* Ensure we requeue to the expected futex. */ |
| 1955 | if (!match_futex(top_waiter->requeue_pi_key, key2)) |
| 1956 | return -EINVAL; |
| 1957 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1958 | /* |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1959 | * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in |
| 1960 | * the contended case or if set_waiters is 1. The pi_state is returned |
| 1961 | * in ps in contended cases. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1962 | */ |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 1963 | vpid = task_pid_vnr(top_waiter->task); |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 1964 | ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task, |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 1965 | exiting, set_waiters); |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 1966 | if (ret == 1) { |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 1967 | requeue_pi_wake_futex(top_waiter, key2, hb2); |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 1968 | return vpid; |
| 1969 | } |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1970 | return ret; |
| 1971 | } |
| 1972 | |
| 1973 | /** |
| 1974 | * futex_requeue() - Requeue waiters from uaddr1 to uaddr2 |
Randy Dunlap | fb62db2 | 2010-10-13 11:02:34 -0700 | [diff] [blame] | 1975 | * @uaddr1: source futex user address |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 1976 | * @flags: futex flags (FLAGS_SHARED, etc.) |
Randy Dunlap | fb62db2 | 2010-10-13 11:02:34 -0700 | [diff] [blame] | 1977 | * @uaddr2: target futex user address |
| 1978 | * @nr_wake: number of waiters to wake (must be 1 for requeue_pi) |
| 1979 | * @nr_requeue: number of waiters to requeue (0-INT_MAX) |
| 1980 | * @cmpval: @uaddr1 expected value (or %NULL) |
| 1981 | * @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] | 1982 | * pi futex (pi to pi requeue is not supported) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1983 | * |
| 1984 | * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire |
| 1985 | * uaddr2 atomically on behalf of the top waiter. |
| 1986 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 1987 | * Return: |
| 1988 | * >=0 - on success, the number of tasks requeued or woken; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1989 | * <0 - on error |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 1991 | static int futex_requeue(u32 __user *uaddr1, unsigned int flags, |
| 1992 | u32 __user *uaddr2, int nr_wake, int nr_requeue, |
| 1993 | u32 *cmpval, int requeue_pi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | { |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 1995 | union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 1996 | int drop_count = 0, task_count = 0, ret; |
| 1997 | struct futex_pi_state *pi_state = NULL; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 1998 | struct futex_hash_bucket *hb1, *hb2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | struct futex_q *this, *next; |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 2000 | WAKE_Q(wake_q); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2001 | |
Li Jinyue | d8a3170 | 2017-12-14 17:04:54 +0800 | [diff] [blame] | 2002 | if (nr_wake < 0 || nr_requeue < 0) |
| 2003 | return -EINVAL; |
| 2004 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2005 | if (requeue_pi) { |
| 2006 | /* |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 2007 | * Requeue PI only works on two distinct uaddrs. This |
| 2008 | * check is only valid for private futexes. See below. |
| 2009 | */ |
| 2010 | if (uaddr1 == uaddr2) |
| 2011 | return -EINVAL; |
| 2012 | |
| 2013 | /* |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2014 | * requeue_pi requires a pi_state, try to allocate it now |
| 2015 | * without any locks in case it fails. |
| 2016 | */ |
| 2017 | if (refill_pi_state_cache()) |
| 2018 | return -ENOMEM; |
| 2019 | /* |
| 2020 | * requeue_pi must wake as many tasks as it can, up to nr_wake |
| 2021 | * + nr_requeue, since it acquires the rt_mutex prior to |
| 2022 | * returning to userspace, so as to not leave the rt_mutex with |
| 2023 | * waiters and no owner. However, second and third wake-ups |
| 2024 | * cannot be predicted as they involve race conditions with the |
| 2025 | * first wake and a fault while looking up the pi_state. Both |
| 2026 | * pthread_cond_signal() and pthread_cond_broadcast() should |
| 2027 | * use nr_wake=1. |
| 2028 | */ |
| 2029 | if (nr_wake != 1) |
| 2030 | return -EINVAL; |
| 2031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2033 | retry: |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 2034 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | if (unlikely(ret != 0)) |
| 2036 | goto out; |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 2037 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, |
| 2038 | requeue_pi ? VERIFY_WRITE : VERIFY_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | if (unlikely(ret != 0)) |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2040 | goto out_put_key1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 2042 | /* |
| 2043 | * The check above which compares uaddrs is not sufficient for |
| 2044 | * shared futexes. We need to compare the keys: |
| 2045 | */ |
| 2046 | if (requeue_pi && match_futex(&key1, &key2)) { |
| 2047 | ret = -EINVAL; |
| 2048 | goto out_put_keys; |
| 2049 | } |
| 2050 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2051 | hb1 = hash_futex(&key1); |
| 2052 | hb2 = hash_futex(&key2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2054 | retry_private: |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2055 | hb_waiters_inc(hb2); |
Ingo Molnar | 8b8f319 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2056 | double_lock_hb(hb1, hb2); |
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 | if (likely(cmpval != NULL)) { |
| 2059 | u32 curval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2061 | ret = get_futex_value_locked(&curval, uaddr1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | |
| 2063 | if (unlikely(ret)) { |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 2064 | double_unlock_hb(hb1, hb2); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2065 | hb_waiters_dec(hb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2067 | ret = get_user(curval, uaddr1); |
| 2068 | if (ret) |
| 2069 | goto out_put_keys; |
| 2070 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2071 | if (!(flags & FLAGS_SHARED)) |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2072 | goto retry_private; |
| 2073 | |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2074 | put_futex_key(&key2); |
| 2075 | put_futex_key(&key1); |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2076 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | } |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2078 | if (curval != *cmpval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | ret = -EAGAIN; |
| 2080 | goto out_unlock; |
| 2081 | } |
| 2082 | } |
| 2083 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2084 | if (requeue_pi && (task_count - nr_wake < nr_requeue)) { |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 2085 | struct task_struct *exiting = NULL; |
| 2086 | |
Darren Hart | bab5bc9 | 2009-04-07 23:23:50 -0700 | [diff] [blame] | 2087 | /* |
| 2088 | * Attempt to acquire uaddr2 and wake the top waiter. If we |
| 2089 | * intend to requeue waiters, force setting the FUTEX_WAITERS |
| 2090 | * bit. We force this here where we are able to easily handle |
| 2091 | * faults rather in the requeue loop below. |
| 2092 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2093 | ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1, |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 2094 | &key2, &pi_state, |
| 2095 | &exiting, nr_requeue); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2096 | |
| 2097 | /* |
| 2098 | * At this point the top_waiter has either taken uaddr2 or is |
| 2099 | * waiting on it. If the former, then the pi_state will not |
| 2100 | * 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] | 2101 | * reference to it. If the lock was taken, ret contains the |
| 2102 | * vpid of the top waiter task. |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2103 | * If the lock was not taken, we have pi_state and an initial |
| 2104 | * refcount on it. In case of an error we have nothing. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2105 | */ |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 2106 | if (ret > 0) { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2107 | WARN_ON(pi_state); |
Darren Hart | 89061d3 | 2009-10-15 15:30:48 -0700 | [diff] [blame] | 2108 | drop_count++; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2109 | task_count++; |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 2110 | /* |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2111 | * If we acquired the lock, then the user space value |
| 2112 | * of uaddr2 should be vpid. It cannot be changed by |
| 2113 | * the top waiter as it is blocked on hb2 lock if it |
| 2114 | * tries to do so. If something fiddled with it behind |
| 2115 | * our back the pi state lookup might unearth it. So |
| 2116 | * we rather use the known value than rereading and |
| 2117 | * handing potential crap to lookup_pi_state. |
| 2118 | * |
| 2119 | * If that call succeeds then we have pi_state and an |
| 2120 | * initial refcount on it. |
Thomas Gleixner | 866293e | 2014-05-12 20:45:34 +0000 | [diff] [blame] | 2121 | */ |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 2122 | ret = lookup_pi_state(uaddr2, ret, hb2, &key2, |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 2123 | &pi_state, &exiting); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | switch (ret) { |
| 2127 | case 0: |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2128 | /* We hold a reference on the pi state. */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2129 | break; |
Thomas Gleixner | 4959f2d | 2015-12-19 20:07:40 +0000 | [diff] [blame] | 2130 | |
| 2131 | /* If the above failed, then pi_state is NULL */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2132 | case -EFAULT: |
| 2133 | double_unlock_hb(hb1, hb2); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2134 | hb_waiters_dec(hb2); |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2135 | put_futex_key(&key2); |
| 2136 | put_futex_key(&key1); |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 2137 | ret = fault_in_user_writeable(uaddr2); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2138 | if (!ret) |
| 2139 | goto retry; |
| 2140 | goto out; |
Thomas Gleixner | c27f392 | 2021-02-01 10:01:42 +0000 | [diff] [blame] | 2141 | case -EBUSY: |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2142 | case -EAGAIN: |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2143 | /* |
| 2144 | * Two reasons for this: |
Thomas Gleixner | c27f392 | 2021-02-01 10:01:42 +0000 | [diff] [blame] | 2145 | * - EBUSY: Owner is exiting and we just wait for the |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2146 | * exit to complete. |
Thomas Gleixner | c27f392 | 2021-02-01 10:01:42 +0000 | [diff] [blame] | 2147 | * - EAGAIN: The user space value changed. |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2148 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2149 | double_unlock_hb(hb1, hb2); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2150 | hb_waiters_dec(hb2); |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2151 | put_futex_key(&key2); |
| 2152 | put_futex_key(&key1); |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 2153 | /* |
| 2154 | * Handle the case where the owner is in the middle of |
| 2155 | * exiting. Wait for the exit to complete otherwise |
| 2156 | * this task might loop forever, aka. live lock. |
| 2157 | */ |
| 2158 | wait_for_owner_exiting(ret, exiting); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2159 | cond_resched(); |
| 2160 | goto retry; |
| 2161 | default: |
| 2162 | goto out_unlock; |
| 2163 | } |
| 2164 | } |
| 2165 | |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2166 | plist_for_each_entry_safe(this, next, &hb1->chain, list) { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2167 | if (task_count - nr_wake >= nr_requeue) |
| 2168 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2170 | if (!match_futex(&this->key, &key1)) |
| 2171 | continue; |
| 2172 | |
Darren Hart | 392741e | 2009-08-07 15:20:48 -0700 | [diff] [blame] | 2173 | /* |
| 2174 | * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always |
| 2175 | * be paired with each other and no other futex ops. |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 2176 | * |
| 2177 | * We should never be requeueing a futex_q with a pi_state, |
| 2178 | * which is awaiting a futex_unlock_pi(). |
Darren Hart | 392741e | 2009-08-07 15:20:48 -0700 | [diff] [blame] | 2179 | */ |
| 2180 | if ((requeue_pi && !this->rt_waiter) || |
Darren Hart | aa10990 | 2012-11-26 16:29:56 -0800 | [diff] [blame] | 2181 | (!requeue_pi && this->rt_waiter) || |
| 2182 | this->pi_state) { |
Darren Hart | 392741e | 2009-08-07 15:20:48 -0700 | [diff] [blame] | 2183 | ret = -EINVAL; |
| 2184 | break; |
| 2185 | } |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2186 | |
| 2187 | /* |
| 2188 | * Wake nr_wake waiters. For requeue_pi, if we acquired the |
| 2189 | * lock, we already woke the top_waiter. If not, it will be |
| 2190 | * woken by futex_unlock_pi(). |
| 2191 | */ |
| 2192 | if (++task_count <= nr_wake && !requeue_pi) { |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 2193 | mark_wake_futex(&wake_q, this); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2194 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2195 | } |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2196 | |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 2197 | /* Ensure we requeue to the expected futex for requeue_pi. */ |
| 2198 | if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) { |
| 2199 | ret = -EINVAL; |
| 2200 | break; |
| 2201 | } |
| 2202 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2203 | /* |
| 2204 | * Requeue nr_requeue waiters and possibly one more in the case |
| 2205 | * of requeue_pi if we couldn't acquire the lock atomically. |
| 2206 | */ |
| 2207 | if (requeue_pi) { |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2208 | /* |
| 2209 | * Prepare the waiter to take the rt_mutex. Take a |
| 2210 | * refcount on the pi_state and store the pointer in |
| 2211 | * the futex_q object of the waiter. |
| 2212 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2213 | atomic_inc(&pi_state->refcount); |
| 2214 | this->pi_state = pi_state; |
| 2215 | ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex, |
| 2216 | this->rt_waiter, |
Thomas Gleixner | c051b21 | 2014-05-22 03:25:50 +0000 | [diff] [blame] | 2217 | this->task); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2218 | if (ret == 1) { |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2219 | /* |
| 2220 | * We got the lock. We do neither drop the |
| 2221 | * refcount on pi_state nor clear |
| 2222 | * this->pi_state because the waiter needs the |
| 2223 | * pi_state for cleaning up the user space |
| 2224 | * value. It will drop the refcount after |
| 2225 | * doing so. |
| 2226 | */ |
Darren Hart | beda2c7 | 2009-08-09 15:34:39 -0700 | [diff] [blame] | 2227 | requeue_pi_wake_futex(this, &key2, hb2); |
Darren Hart | 89061d3 | 2009-10-15 15:30:48 -0700 | [diff] [blame] | 2228 | drop_count++; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2229 | continue; |
| 2230 | } else if (ret) { |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2231 | /* |
| 2232 | * rt_mutex_start_proxy_lock() detected a |
| 2233 | * potential deadlock when we tried to queue |
| 2234 | * that waiter. Drop the pi_state reference |
| 2235 | * which we took above and remove the pointer |
| 2236 | * to the state from the waiters futex_q |
| 2237 | * object. |
| 2238 | */ |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2239 | this->pi_state = NULL; |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2240 | put_pi_state(pi_state); |
Thomas Gleixner | 885c2cb | 2015-12-19 20:07:41 +0000 | [diff] [blame] | 2241 | /* |
| 2242 | * We stop queueing more waiters and let user |
| 2243 | * space deal with the mess. |
| 2244 | */ |
| 2245 | break; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2246 | } |
| 2247 | } |
| 2248 | requeue_futex(this, hb1, hb2, &key2); |
| 2249 | drop_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Thomas Gleixner | ecb38b7 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2252 | /* |
| 2253 | * We took an extra initial reference to the pi_state either |
| 2254 | * in futex_proxy_trylock_atomic() or in lookup_pi_state(). We |
| 2255 | * need to drop it here again. |
| 2256 | */ |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2257 | put_pi_state(pi_state); |
Thomas Gleixner | 885c2cb | 2015-12-19 20:07:41 +0000 | [diff] [blame] | 2258 | |
| 2259 | out_unlock: |
Darren Hart | 5eb3dc6 | 2009-03-12 00:55:52 -0700 | [diff] [blame] | 2260 | double_unlock_hb(hb1, hb2); |
Davidlohr Bueso | 1d0dcb3 | 2015-05-01 08:27:51 -0700 | [diff] [blame] | 2261 | wake_up_q(&wake_q); |
Linus Torvalds | 69cd9eb | 2014-04-08 15:30:07 -0700 | [diff] [blame] | 2262 | hb_waiters_dec(hb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | |
Darren Hart | cd84a42 | 2009-04-02 14:19:38 -0700 | [diff] [blame] | 2264 | /* |
| 2265 | * drop_futex_key_refs() must be called outside the spinlocks. During |
| 2266 | * the requeue we moved futex_q's from the hash bucket at key1 to the |
| 2267 | * one at key2 and updated their key pointer. We no longer need to |
| 2268 | * hold the references to key1. |
| 2269 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | while (--drop_count >= 0) |
Rusty Russell | 9adef58 | 2007-05-08 00:26:42 -0700 | [diff] [blame] | 2271 | drop_futex_key_refs(&key1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2273 | out_put_keys: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2274 | put_futex_key(&key2); |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2275 | out_put_key1: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2276 | put_futex_key(&key1); |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2277 | out: |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 2278 | return ret ? ret : task_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /* The key must be already stored in q->key. */ |
Eric Sesterhenn | 82af7ac | 2008-01-25 10:40:46 +0100 | [diff] [blame] | 2282 | static inline struct futex_hash_bucket *queue_lock(struct futex_q *q) |
Namhyung Kim | 15e408c | 2010-09-14 21:43:48 +0900 | [diff] [blame] | 2283 | __acquires(&hb->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | { |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2285 | struct futex_hash_bucket *hb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2287 | hb = hash_futex(&q->key); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 2288 | |
| 2289 | /* |
| 2290 | * Increment the counter before taking the lock so that |
| 2291 | * a potential waker won't miss a to-be-slept task that is |
| 2292 | * waiting for the spinlock. This is safe as all queue_lock() |
| 2293 | * users end up calling queue_me(). Similarly, for housekeeping, |
| 2294 | * decrement the counter at queue_unlock() when some error has |
| 2295 | * occurred and we don't end up adding the task to the list. |
| 2296 | */ |
| 2297 | hb_waiters_inc(hb); |
| 2298 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2299 | q->lock_ptr = &hb->lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | |
Davidlohr Bueso | 8ad7b37 | 2016-02-09 11:15:13 -0800 | [diff] [blame] | 2301 | spin_lock(&hb->lock); /* implies smp_mb(); (A) */ |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2302 | return hb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2305 | static inline void |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2306 | queue_unlock(struct futex_hash_bucket *hb) |
Namhyung Kim | 15e408c | 2010-09-14 21:43:48 +0900 | [diff] [blame] | 2307 | __releases(&hb->lock) |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2308 | { |
| 2309 | spin_unlock(&hb->lock); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 2310 | hb_waiters_dec(hb); |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | /** |
| 2314 | * queue_me() - Enqueue the futex_q on the futex_hash_bucket |
| 2315 | * @q: The futex_q to enqueue |
| 2316 | * @hb: The destination hash bucket |
| 2317 | * |
| 2318 | * The hb->lock must be held by the caller, and is released here. A call to |
| 2319 | * queue_me() is typically paired with exactly one call to unqueue_me(). The |
| 2320 | * exceptions involve the PI related operations, which may use unqueue_me_pi() |
| 2321 | * or nothing if the unqueue is done as part of the wake process and the unqueue |
| 2322 | * state is implicit in the state of woken task (see futex_wait_requeue_pi() for |
| 2323 | * an example). |
| 2324 | */ |
Eric Sesterhenn | 82af7ac | 2008-01-25 10:40:46 +0100 | [diff] [blame] | 2325 | 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] | 2326 | __releases(&hb->lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | { |
Pierre Peiffer | ec92d08 | 2007-05-09 02:35:00 -0700 | [diff] [blame] | 2328 | int prio; |
| 2329 | |
| 2330 | /* |
| 2331 | * The priority used to register this element is |
| 2332 | * - either the real thread-priority for the real-time threads |
| 2333 | * (i.e. threads with a priority lower than MAX_RT_PRIO) |
| 2334 | * - or MAX_RT_PRIO for non-RT threads. |
| 2335 | * Thus, all RT-threads are woken first in priority order, and |
| 2336 | * the others are woken last, in FIFO order. |
| 2337 | */ |
| 2338 | prio = min(current->normal_prio, MAX_RT_PRIO); |
| 2339 | |
| 2340 | plist_node_init(&q->list, prio); |
Pierre Peiffer | ec92d08 | 2007-05-09 02:35:00 -0700 | [diff] [blame] | 2341 | plist_add(&q->list, &hb->chain); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2342 | q->task = current; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2343 | spin_unlock(&hb->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | } |
| 2345 | |
Darren Hart | d40d65c | 2009-09-21 22:30:15 -0700 | [diff] [blame] | 2346 | /** |
| 2347 | * unqueue_me() - Remove the futex_q from its futex_hash_bucket |
| 2348 | * @q: The futex_q to unqueue |
| 2349 | * |
| 2350 | * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must |
| 2351 | * be paired with exactly one earlier call to queue_me(). |
| 2352 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2353 | * Return: |
| 2354 | * 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] | 2355 | * 0 - if the futex_q was already removed by the waking thread |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2356 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | static int unqueue_me(struct futex_q *q) |
| 2358 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | spinlock_t *lock_ptr; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2360 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | |
| 2362 | /* 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] | 2363 | retry: |
Jianyu Zhan | 29b75eb | 2016-03-07 09:32:24 +0800 | [diff] [blame] | 2364 | /* |
| 2365 | * q->lock_ptr can change between this read and the following spin_lock. |
| 2366 | * Use READ_ONCE to forbid the compiler from reloading q->lock_ptr and |
| 2367 | * optimizing lock_ptr out of the logic below. |
| 2368 | */ |
| 2369 | lock_ptr = READ_ONCE(q->lock_ptr); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 2370 | if (lock_ptr != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | spin_lock(lock_ptr); |
| 2372 | /* |
| 2373 | * q->lock_ptr can change between reading it and |
| 2374 | * spin_lock(), causing us to take the wrong lock. This |
| 2375 | * corrects the race condition. |
| 2376 | * |
| 2377 | * Reasoning goes like this: if we have the wrong lock, |
| 2378 | * q->lock_ptr must have changed (maybe several times) |
| 2379 | * between reading it and the spin_lock(). It can |
| 2380 | * change again after the spin_lock() but only if it was |
| 2381 | * already changed before the spin_lock(). It cannot, |
| 2382 | * however, change back to the original value. Therefore |
| 2383 | * we can detect whether we acquired the correct lock. |
| 2384 | */ |
| 2385 | if (unlikely(lock_ptr != q->lock_ptr)) { |
| 2386 | spin_unlock(lock_ptr); |
| 2387 | goto retry; |
| 2388 | } |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 2389 | __unqueue_futex(q); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2390 | |
| 2391 | BUG_ON(q->pi_state); |
| 2392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | spin_unlock(lock_ptr); |
| 2394 | ret = 1; |
| 2395 | } |
| 2396 | |
Rusty Russell | 9adef58 | 2007-05-08 00:26:42 -0700 | [diff] [blame] | 2397 | drop_futex_key_refs(&q->key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | return ret; |
| 2399 | } |
| 2400 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2401 | /* |
| 2402 | * PI futexes can not be requeued and must remove themself from the |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2403 | * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry |
| 2404 | * and dropped here. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2405 | */ |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2406 | static void unqueue_me_pi(struct futex_q *q) |
Namhyung Kim | 15e408c | 2010-09-14 21:43:48 +0900 | [diff] [blame] | 2407 | __releases(q->lock_ptr) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2408 | { |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 2409 | __unqueue_futex(q); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2410 | |
| 2411 | BUG_ON(!q->pi_state); |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 2412 | put_pi_state(q->pi_state); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2413 | q->pi_state = NULL; |
| 2414 | |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2415 | spin_unlock(q->lock_ptr); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2418 | static int __fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, |
| 2419 | struct task_struct *argowner) |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2420 | { |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2421 | struct futex_pi_state *pi_state = q->pi_state; |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2422 | struct task_struct *oldowner, *newowner; |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2423 | u32 uval, curval, newval, newtid; |
| 2424 | int err = 0; |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2425 | |
| 2426 | oldowner = pi_state->owner; |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2427 | |
| 2428 | /* |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2429 | * We are here because either: |
| 2430 | * |
| 2431 | * - we stole the lock and pi_state->owner needs updating to reflect |
| 2432 | * that (@argowner == current), |
| 2433 | * |
| 2434 | * or: |
| 2435 | * |
| 2436 | * - someone stole our lock and we need to fix things to point to the |
| 2437 | * new owner (@argowner == NULL). |
| 2438 | * |
| 2439 | * Either way, we have to replace the TID in the user space variable. |
Lai Jiangshan | 8161239 | 2011-01-14 17:09:41 +0800 | [diff] [blame] | 2440 | * 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] | 2441 | * |
Darren Hart | b2d0994 | 2009-03-12 00:55:37 -0700 | [diff] [blame] | 2442 | * Note: We write the user space value _before_ changing the pi_state |
| 2443 | * because we can fault here. Imagine swapped out pages or a fork |
| 2444 | * that marked all the anonymous memory readonly for cow. |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2445 | * |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 2446 | * Modifying pi_state _before_ the user space value would leave the |
| 2447 | * pi_state in an inconsistent state when we fault here, because we |
| 2448 | * need to drop the locks to handle the fault. This might be observed |
| 2449 | * in the PID check in lookup_pi_state. |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2450 | */ |
| 2451 | retry: |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2452 | if (!argowner) { |
| 2453 | if (oldowner != current) { |
| 2454 | /* |
| 2455 | * We raced against a concurrent self; things are |
| 2456 | * already fixed up. Nothing to do. |
| 2457 | */ |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
| 2461 | if (__rt_mutex_futex_trylock(&pi_state->pi_mutex)) { |
Thomas Gleixner | ad8fdba | 2021-02-11 09:26:58 +0000 | [diff] [blame] | 2462 | /* We got the lock. pi_state is correct. Tell caller. */ |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2463 | return 1; |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2464 | } |
| 2465 | |
| 2466 | /* |
| 2467 | * Since we just failed the trylock; there must be an owner. |
| 2468 | */ |
| 2469 | newowner = rt_mutex_owner(&pi_state->pi_mutex); |
| 2470 | BUG_ON(!newowner); |
| 2471 | } else { |
| 2472 | WARN_ON_ONCE(argowner != current); |
| 2473 | if (oldowner == current) { |
| 2474 | /* |
| 2475 | * We raced against a concurrent self; things are |
| 2476 | * already fixed up. Nothing to do. |
| 2477 | */ |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2478 | return 1; |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2479 | } |
| 2480 | newowner = argowner; |
| 2481 | } |
| 2482 | |
| 2483 | newtid = task_pid_vnr(newowner) | FUTEX_WAITERS; |
Peter Zijlstra | 0d35180 | 2018-01-22 11:39:47 +0100 | [diff] [blame] | 2484 | /* Owner died? */ |
| 2485 | if (!pi_state->owner) |
| 2486 | newtid |= FUTEX_OWNER_DIED; |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2487 | |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2488 | if (get_futex_value_locked(&uval, uaddr)) |
| 2489 | goto handle_fault; |
| 2490 | |
| 2491 | while (1) { |
| 2492 | newval = (uval & FUTEX_OWNER_DIED) | newtid; |
| 2493 | |
Michel Lespinasse | 37a9d91 | 2011-03-10 18:48:51 -0800 | [diff] [blame] | 2494 | if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)) |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2495 | goto handle_fault; |
| 2496 | if (curval == uval) |
| 2497 | break; |
| 2498 | uval = curval; |
| 2499 | } |
| 2500 | |
| 2501 | /* |
| 2502 | * We fixed up user space. Now we need to fix the pi_state |
| 2503 | * itself. |
| 2504 | */ |
Thomas Gleixner | 76bc0ec | 2021-02-03 13:45:35 +0000 | [diff] [blame] | 2505 | pi_state_update_owner(pi_state, newowner); |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2506 | |
Thomas Gleixner | ad8fdba | 2021-02-11 09:26:58 +0000 | [diff] [blame] | 2507 | return argowner == current; |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2508 | |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2509 | /* |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 2510 | * To handle the page fault we need to drop the locks here. That gives |
| 2511 | * the other task (either the highest priority waiter itself or the |
| 2512 | * task which stole the rtmutex) the chance to try the fixup of the |
| 2513 | * pi_state. So once we are back from handling the fault we need to |
| 2514 | * check the pi_state after reacquiring the locks and before trying to |
| 2515 | * do another fixup. When the fixup has been done already we simply |
| 2516 | * return. |
| 2517 | * |
| 2518 | * Note: we hold both hb->lock and pi_mutex->wait_lock. We can safely |
| 2519 | * drop hb->lock since the caller owns the hb -> futex_q relation. |
| 2520 | * Dropping the pi_mutex->wait_lock requires the state revalidate. |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2521 | */ |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2522 | handle_fault: |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 2523 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2524 | spin_unlock(q->lock_ptr); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2525 | |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2526 | err = fault_in_user_writeable(uaddr); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2527 | |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2528 | spin_lock(q->lock_ptr); |
Peter Zijlstra | dc3f2ff | 2021-02-11 09:26:59 +0000 | [diff] [blame] | 2529 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2530 | |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2531 | /* |
| 2532 | * Check if someone else fixed it for us: |
| 2533 | */ |
| 2534 | if (pi_state->owner != oldowner) |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2535 | return argowner == current; |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2536 | |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2537 | /* Retry if err was -EAGAIN or the fault in succeeded */ |
| 2538 | if (!err) |
| 2539 | goto retry; |
Thomas Gleixner | 1b7558e | 2008-06-23 11:21:58 +0200 | [diff] [blame] | 2540 | |
Thomas Gleixner | b960d9a | 2021-02-03 13:45:39 +0000 | [diff] [blame] | 2541 | /* |
| 2542 | * fault_in_user_writeable() failed so user state is immutable. At |
| 2543 | * best we can make the kernel state consistent but user state will |
| 2544 | * be most likely hosed and any subsequent unlock operation will be |
| 2545 | * rejected due to PI futex rule [10]. |
| 2546 | * |
| 2547 | * Ensure that the rtmutex owner is also the pi_state owner despite |
| 2548 | * the user space value claiming something different. There is no |
| 2549 | * point in unlocking the rtmutex if current is the owner as it |
| 2550 | * would need to wait until the next waiter has taken the rtmutex |
| 2551 | * to guarantee consistent state. Keep it simple. Userspace asked |
| 2552 | * for this wreckaged state. |
| 2553 | * |
| 2554 | * The rtmutex has an owner - either current or some other |
| 2555 | * task. See the EAGAIN loop above. |
| 2556 | */ |
| 2557 | pi_state_update_owner(pi_state, rt_mutex_owner(&pi_state->pi_mutex)); |
| 2558 | |
Thomas Gleixner | 48ab8e8 | 2021-02-03 13:45:38 +0000 | [diff] [blame] | 2559 | return err; |
| 2560 | } |
| 2561 | |
| 2562 | static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q, |
| 2563 | struct task_struct *argowner) |
| 2564 | { |
| 2565 | struct futex_pi_state *pi_state = q->pi_state; |
| 2566 | int ret; |
| 2567 | |
| 2568 | lockdep_assert_held(q->lock_ptr); |
| 2569 | |
| 2570 | raw_spin_lock_irq(&pi_state->pi_mutex.wait_lock); |
| 2571 | ret = __fixup_pi_state_owner(uaddr, q, argowner); |
| 2572 | raw_spin_unlock_irq(&pi_state->pi_mutex.wait_lock); |
| 2573 | return ret; |
Pierre Peiffer | d0aa7a7 | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2576 | static long futex_wait_restart(struct restart_block *restart); |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 2577 | |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2578 | /** |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2579 | * fixup_owner() - Post lock pi_state and corner case management |
| 2580 | * @uaddr: user address of the futex |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2581 | * @q: futex_q (contains pi_state and access to the rt_mutex) |
| 2582 | * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0) |
| 2583 | * |
| 2584 | * After attempting to lock an rt_mutex, this function is called to cleanup |
| 2585 | * the pi_state owner as well as handle race conditions that may allow us to |
| 2586 | * acquire the lock. Must be called with the hb lock held. |
| 2587 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2588 | * Return: |
| 2589 | * 1 - success, lock taken; |
| 2590 | * 0 - success, lock not taken; |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2591 | * <0 - on error (-EFAULT) |
| 2592 | */ |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2593 | 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] | 2594 | { |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2595 | if (locked) { |
| 2596 | /* |
| 2597 | * Got the lock. We might not be the anticipated owner if we |
| 2598 | * did a lock-steal - fix up the PI-state in that case: |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2599 | * |
| 2600 | * Speculative pi_state->owner read (we don't hold wait_lock); |
| 2601 | * since we own the lock pi_state->owner == current is the |
| 2602 | * stable state, anything else needs more attention. |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2603 | */ |
| 2604 | if (q->pi_state->owner != current) |
Thomas Gleixner | ad8fdba | 2021-02-11 09:26:58 +0000 | [diff] [blame] | 2605 | return fixup_pi_state_owner(uaddr, q, current); |
| 2606 | return 1; |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | /* |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2610 | * If we didn't get the lock; check if anybody stole it from us. In |
| 2611 | * that case, we need to fix up the uval to point to them instead of |
| 2612 | * us, otherwise bad things happen. [10] |
| 2613 | * |
| 2614 | * Another speculative read; pi_state->owner == current is unstable |
| 2615 | * but needs our attention. |
| 2616 | */ |
Thomas Gleixner | ad8fdba | 2021-02-11 09:26:58 +0000 | [diff] [blame] | 2617 | if (q->pi_state->owner == current) |
| 2618 | return fixup_pi_state_owner(uaddr, q, NULL); |
Peter Zijlstra | 781691c | 2021-02-03 13:45:33 +0000 | [diff] [blame] | 2619 | |
| 2620 | /* |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2621 | * Paranoia check. If we did not take the lock, then we should not be |
Thomas Gleixner | 083895e | 2021-02-03 13:45:34 +0000 | [diff] [blame] | 2622 | * the owner of the rt_mutex. Warn and establish consistent state. |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2623 | */ |
Thomas Gleixner | 083895e | 2021-02-03 13:45:34 +0000 | [diff] [blame] | 2624 | if (WARN_ON_ONCE(rt_mutex_owner(&q->pi_state->pi_mutex) == current)) |
| 2625 | return fixup_pi_state_owner(uaddr, q, current); |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2626 | |
Thomas Gleixner | ad8fdba | 2021-02-11 09:26:58 +0000 | [diff] [blame] | 2627 | return 0; |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2628 | } |
| 2629 | |
| 2630 | /** |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2631 | * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal |
| 2632 | * @hb: the futex hash bucket, must be locked by the caller |
| 2633 | * @q: the futex_q to queue up on |
| 2634 | * @timeout: the prepared hrtimer_sleeper, or null for no timeout |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2635 | */ |
| 2636 | 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] | 2637 | struct hrtimer_sleeper *timeout) |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2638 | { |
Darren Hart | 9beba3c | 2009-09-24 11:54:47 -0700 | [diff] [blame] | 2639 | /* |
| 2640 | * The task state is guaranteed to be set before another task can |
Peter Zijlstra | b92b8b3 | 2015-05-12 10:51:55 +0200 | [diff] [blame] | 2641 | * wake it. set_current_state() is implemented using smp_store_mb() and |
Darren Hart | 9beba3c | 2009-09-24 11:54:47 -0700 | [diff] [blame] | 2642 | * queue_me() calls spin_unlock() upon completion, both serializing |
| 2643 | * access to the hash list and forcing another memory barrier. |
| 2644 | */ |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 2645 | set_current_state(TASK_INTERRUPTIBLE); |
Darren Hart | 0729e19 | 2009-09-21 22:30:38 -0700 | [diff] [blame] | 2646 | queue_me(q, hb); |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2647 | |
| 2648 | /* Arm the timer */ |
Thomas Gleixner | 2e4b0d3 | 2015-04-14 21:09:13 +0000 | [diff] [blame] | 2649 | if (timeout) |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2650 | hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS); |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2651 | |
| 2652 | /* |
Darren Hart | 0729e19 | 2009-09-21 22:30:38 -0700 | [diff] [blame] | 2653 | * If we have been removed from the hash list, then another task |
| 2654 | * 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] | 2655 | */ |
| 2656 | if (likely(!plist_node_empty(&q->list))) { |
| 2657 | /* |
| 2658 | * If the timer has already expired, current will already be |
| 2659 | * flagged for rescheduling. Only call schedule if there |
| 2660 | * is no timeout, or if it has yet to expire. |
| 2661 | */ |
| 2662 | if (!timeout || timeout->task) |
Colin Cross | 88c8004 | 2013-05-01 18:35:05 -0700 | [diff] [blame] | 2663 | freezable_schedule(); |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2664 | } |
| 2665 | __set_current_state(TASK_RUNNING); |
| 2666 | } |
| 2667 | |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2668 | /** |
| 2669 | * futex_wait_setup() - Prepare to wait on a futex |
| 2670 | * @uaddr: the futex userspace address |
| 2671 | * @val: the expected value |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2672 | * @flags: futex flags (FLAGS_SHARED, etc.) |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2673 | * @q: the associated futex_q |
| 2674 | * @hb: storage for hash_bucket pointer to be returned to caller |
| 2675 | * |
| 2676 | * Setup the futex_q and locate the hash_bucket. Get the futex value and |
| 2677 | * compare it with the expected value. Handle atomic faults internally. |
| 2678 | * Return with the hb lock held and a q.key reference on success, and unlocked |
| 2679 | * with no q.key reference on failure. |
| 2680 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 2681 | * Return: |
| 2682 | * 0 - uaddr contains val and hb has been locked; |
Bart Van Assche | ca4a04c | 2011-07-17 09:01:00 +0200 | [diff] [blame] | 2683 | * <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] | 2684 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2685 | 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] | 2686 | struct futex_q *q, struct futex_hash_bucket **hb) |
| 2687 | { |
| 2688 | u32 uval; |
| 2689 | int ret; |
| 2690 | |
| 2691 | /* |
| 2692 | * Access the page AFTER the hash-bucket is locked. |
| 2693 | * Order is important: |
| 2694 | * |
| 2695 | * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val); |
| 2696 | * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); } |
| 2697 | * |
| 2698 | * The basic logical guarantee of a futex is that it blocks ONLY |
| 2699 | * 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] | 2700 | * any cond. If we locked the hash-bucket after testing *uaddr, that |
| 2701 | * would open a race condition where we could block indefinitely with |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2702 | * cond(var) false, which would violate the guarantee. |
| 2703 | * |
Michel Lespinasse | 8fe8f54 | 2011-03-06 18:07:50 -0800 | [diff] [blame] | 2704 | * On the other hand, we insert q and release the hash-bucket only |
| 2705 | * after testing *uaddr. This guarantees that futex_wait() will NOT |
| 2706 | * absorb a wakeup if *uaddr does not match the desired values |
| 2707 | * while the syscall executes. |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2708 | */ |
| 2709 | retry: |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 2710 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2711 | if (unlikely(ret != 0)) |
Darren Hart | a5a2a0c | 2009-04-10 09:50:05 -0700 | [diff] [blame] | 2712 | return ret; |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2713 | |
| 2714 | retry_private: |
| 2715 | *hb = queue_lock(q); |
| 2716 | |
| 2717 | ret = get_futex_value_locked(&uval, uaddr); |
| 2718 | |
| 2719 | if (ret) { |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2720 | queue_unlock(*hb); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2721 | |
| 2722 | ret = get_user(uval, uaddr); |
| 2723 | if (ret) |
| 2724 | goto out; |
| 2725 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2726 | if (!(flags & FLAGS_SHARED)) |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2727 | goto retry_private; |
| 2728 | |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2729 | put_futex_key(&q->key); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2730 | goto retry; |
| 2731 | } |
| 2732 | |
| 2733 | if (uval != val) { |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2734 | queue_unlock(*hb); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2735 | ret = -EWOULDBLOCK; |
| 2736 | } |
| 2737 | |
| 2738 | out: |
| 2739 | if (ret) |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2740 | put_futex_key(&q->key); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2741 | return ret; |
| 2742 | } |
| 2743 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2744 | static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val, |
| 2745 | ktime_t *abs_time, u32 bitset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | { |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2747 | struct hrtimer_sleeper timeout, *to = NULL; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2748 | struct restart_block *restart; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2749 | struct futex_hash_bucket *hb; |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 2750 | struct futex_q q = futex_q_init; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2751 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 2753 | if (!bitset) |
| 2754 | return -EINVAL; |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 2755 | q.bitset = bitset; |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2756 | |
| 2757 | if (abs_time) { |
| 2758 | to = &timeout; |
| 2759 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2760 | hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ? |
| 2761 | CLOCK_REALTIME : CLOCK_MONOTONIC, |
| 2762 | HRTIMER_MODE_ABS); |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2763 | hrtimer_init_sleeper(to, current); |
| 2764 | hrtimer_set_expires_range_ns(&to->timer, *abs_time, |
| 2765 | current->timer_slack_ns); |
| 2766 | } |
| 2767 | |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2768 | retry: |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2769 | /* |
| 2770 | * Prepare to wait on uaddr. On success, holds hb lock and increments |
| 2771 | * q.key refs. |
| 2772 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2773 | ret = futex_wait_setup(uaddr, val, flags, &q, &hb); |
Darren Hart | f801073 | 2009-04-03 13:40:40 -0700 | [diff] [blame] | 2774 | if (ret) |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2775 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2777 | /* queue_me and wait for wakeup, timeout, or a signal. */ |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 2778 | futex_wait_queue_me(hb, &q, to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | |
| 2780 | /* If we were woken (and unqueued), we succeeded, whatever. */ |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2781 | ret = 0; |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2782 | /* unqueue_me() drops q.key ref */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | if (!unqueue_me(&q)) |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2784 | goto out; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2785 | ret = -ETIMEDOUT; |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2786 | if (to && !to->task) |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2787 | goto out; |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2788 | |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2789 | /* |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2790 | * We expect signal_pending(current), but we might be the |
| 2791 | * victim of a spurious wakeup as well. |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 2792 | */ |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2793 | if (!signal_pending(current)) |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2794 | goto retry; |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 2795 | |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2796 | ret = -ERESTARTSYS; |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2797 | if (!abs_time) |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 2798 | goto out; |
Steven Rostedt | ce6bd42 | 2007-12-05 15:46:09 +0100 | [diff] [blame] | 2799 | |
Andy Lutomirski | f56141e | 2015-02-12 15:01:14 -0800 | [diff] [blame] | 2800 | restart = ¤t->restart_block; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2801 | restart->fn = futex_wait_restart; |
Namhyung Kim | a3c74c5 | 2010-09-14 21:43:47 +0900 | [diff] [blame] | 2802 | restart->futex.uaddr = uaddr; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2803 | restart->futex.val = val; |
| 2804 | restart->futex.time = abs_time->tv64; |
| 2805 | restart->futex.bitset = bitset; |
Darren Hart | 0cd9c64 | 2011-04-14 15:41:57 -0700 | [diff] [blame] | 2806 | restart->futex.flags = flags | FLAGS_HAS_TIMEOUT; |
Peter Zijlstra | 2fff78c | 2009-02-11 18:10:10 +0100 | [diff] [blame] | 2807 | |
| 2808 | ret = -ERESTART_RESTARTBLOCK; |
| 2809 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2810 | out: |
Darren Hart | ca5f952 | 2009-04-03 13:39:33 -0700 | [diff] [blame] | 2811 | if (to) { |
| 2812 | hrtimer_cancel(&to->timer); |
| 2813 | destroy_hrtimer_on_stack(&to->timer); |
| 2814 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2815 | return ret; |
| 2816 | } |
| 2817 | |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2818 | |
| 2819 | static long futex_wait_restart(struct restart_block *restart) |
| 2820 | { |
Namhyung Kim | a3c74c5 | 2010-09-14 21:43:47 +0900 | [diff] [blame] | 2821 | u32 __user *uaddr = restart->futex.uaddr; |
Darren Hart | a72188d | 2009-04-03 13:40:22 -0700 | [diff] [blame] | 2822 | ktime_t t, *tp = NULL; |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2823 | |
Darren Hart | a72188d | 2009-04-03 13:40:22 -0700 | [diff] [blame] | 2824 | if (restart->futex.flags & FLAGS_HAS_TIMEOUT) { |
| 2825 | t.tv64 = restart->futex.time; |
| 2826 | tp = &t; |
| 2827 | } |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2828 | restart->fn = do_no_restart_syscall; |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2829 | |
| 2830 | return (long)futex_wait(uaddr, restart->futex.flags, |
| 2831 | restart->futex.val, tp, restart->futex.bitset); |
Nick Piggin | 72c1bbf | 2007-05-08 00:26:43 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2835 | /* |
| 2836 | * Userspace tried a 0 -> TID atomic transition of the futex value |
| 2837 | * and failed. The kernel side here does the whole locking operation: |
Davidlohr Bueso | 767f509 | 2015-06-29 23:26:01 -0700 | [diff] [blame] | 2838 | * if there are waiters then it will block as a consequence of relying |
| 2839 | * on rt-mutexes, it does PI, etc. (Due to races the kernel might see |
| 2840 | * a 0 value of the futex too.). |
| 2841 | * |
| 2842 | * Also serves as futex trylock_pi()'ing, and due semantics. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2843 | */ |
Michael Kerrisk | 996636d | 2015-01-16 20:28:06 +0100 | [diff] [blame] | 2844 | static int futex_lock_pi(u32 __user *uaddr, unsigned int flags, |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2845 | ktime_t *time, int trylock) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2846 | { |
Thomas Gleixner | c5780e9 | 2006-09-08 09:47:15 -0700 | [diff] [blame] | 2847 | struct hrtimer_sleeper timeout, *to = NULL; |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 2848 | struct task_struct *exiting = NULL; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2849 | struct futex_hash_bucket *hb; |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 2850 | struct futex_q q = futex_q_init; |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2851 | int res, ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2852 | |
| 2853 | if (refill_pi_state_cache()) |
| 2854 | return -ENOMEM; |
| 2855 | |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 2856 | if (time) { |
Thomas Gleixner | c5780e9 | 2006-09-08 09:47:15 -0700 | [diff] [blame] | 2857 | to = &timeout; |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 2858 | hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME, |
| 2859 | HRTIMER_MODE_ABS); |
Thomas Gleixner | c5780e9 | 2006-09-08 09:47:15 -0700 | [diff] [blame] | 2860 | hrtimer_init_sleeper(to, current); |
Arjan van de Ven | cc584b2 | 2008-09-01 15:02:30 -0700 | [diff] [blame] | 2861 | hrtimer_set_expires(&to->timer, *time); |
Thomas Gleixner | c5780e9 | 2006-09-08 09:47:15 -0700 | [diff] [blame] | 2862 | } |
| 2863 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2864 | retry: |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 2865 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2866 | if (unlikely(ret != 0)) |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2867 | goto out; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2868 | |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2869 | retry_private: |
Eric Sesterhenn | 82af7ac | 2008-01-25 10:40:46 +0100 | [diff] [blame] | 2870 | hb = queue_lock(&q); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2871 | |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 2872 | ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, |
| 2873 | &exiting, 0); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2874 | if (unlikely(ret)) { |
Davidlohr Bueso | 767f509 | 2015-06-29 23:26:01 -0700 | [diff] [blame] | 2875 | /* |
| 2876 | * Atomic work succeeded and we got the lock, |
| 2877 | * or failed. Either way, we do _not_ block. |
| 2878 | */ |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2879 | switch (ret) { |
Darren Hart | 1a52084 | 2009-04-03 13:39:52 -0700 | [diff] [blame] | 2880 | case 1: |
| 2881 | /* We got the lock. */ |
| 2882 | ret = 0; |
| 2883 | goto out_unlock_put_key; |
| 2884 | case -EFAULT: |
| 2885 | goto uaddr_faulted; |
Thomas Gleixner | c27f392 | 2021-02-01 10:01:42 +0000 | [diff] [blame] | 2886 | case -EBUSY: |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2887 | case -EAGAIN: |
| 2888 | /* |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2889 | * Two reasons for this: |
Thomas Gleixner | c27f392 | 2021-02-01 10:01:42 +0000 | [diff] [blame] | 2890 | * - EBUSY: Task is exiting and we just wait for the |
Thomas Gleixner | af54d6a | 2014-06-11 20:45:41 +0000 | [diff] [blame] | 2891 | * exit to complete. |
Thomas Gleixner | c27f392 | 2021-02-01 10:01:42 +0000 | [diff] [blame] | 2892 | * - EAGAIN: The user space value changed. |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2893 | */ |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2894 | queue_unlock(hb); |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2895 | put_futex_key(&q.key); |
Thomas Gleixner | cf16e42 | 2021-02-01 10:01:43 +0000 | [diff] [blame] | 2896 | /* |
| 2897 | * Handle the case where the owner is in the middle of |
| 2898 | * exiting. Wait for the exit to complete otherwise |
| 2899 | * this task might loop forever, aka. live lock. |
| 2900 | */ |
| 2901 | wait_for_owner_exiting(ret, exiting); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2902 | cond_resched(); |
| 2903 | goto retry; |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2904 | default: |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2905 | goto out_unlock_put_key; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2906 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | /* |
| 2910 | * Only actually queue now that the atomic ops are done: |
| 2911 | */ |
Eric Sesterhenn | 82af7ac | 2008-01-25 10:40:46 +0100 | [diff] [blame] | 2912 | queue_me(&q, hb); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2913 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2914 | WARN_ON(!q.pi_state); |
| 2915 | /* |
| 2916 | * Block on the PI mutex: |
| 2917 | */ |
Thomas Gleixner | c051b21 | 2014-05-22 03:25:50 +0000 | [diff] [blame] | 2918 | if (!trylock) { |
| 2919 | ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to); |
| 2920 | } else { |
Peter Zijlstra | 2c60d4a | 2021-02-03 13:45:30 +0000 | [diff] [blame] | 2921 | ret = rt_mutex_futex_trylock(&q.pi_state->pi_mutex); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2922 | /* Fixup the trylock return value: */ |
| 2923 | ret = ret ? 0 : -EWOULDBLOCK; |
| 2924 | } |
| 2925 | |
Vernon Mauery | a99e4e4 | 2006-07-01 04:35:42 -0700 | [diff] [blame] | 2926 | spin_lock(q.lock_ptr); |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2927 | /* |
| 2928 | * Fixup the pi_state owner and possibly acquire the lock if we |
| 2929 | * haven't already. |
| 2930 | */ |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2931 | res = fixup_owner(uaddr, &q, !ret); |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2932 | /* |
| 2933 | * If fixup_owner() returned an error, proprogate that. If it acquired |
| 2934 | * the lock, clear our -ETIMEDOUT or -EINTR. |
| 2935 | */ |
| 2936 | if (res) |
| 2937 | ret = (res < 0) ? res : 0; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2938 | |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2939 | /* Unqueue and drop the lock */ |
| 2940 | unqueue_me_pi(&q); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2941 | |
Mikael Pettersson | 5ecb01c | 2010-01-23 22:36:29 +0100 | [diff] [blame] | 2942 | goto out_put_key; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2943 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2944 | out_unlock_put_key: |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2945 | queue_unlock(hb); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2946 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2947 | out_put_key: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2948 | put_futex_key(&q.key); |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2949 | out: |
Thomas Gleixner | 237fc6e | 2008-04-30 00:55:04 -0700 | [diff] [blame] | 2950 | if (to) |
| 2951 | destroy_hrtimer_on_stack(&to->timer); |
Darren Hart | dd97399 | 2009-04-03 13:40:02 -0700 | [diff] [blame] | 2952 | return ret != -EINTR ? ret : -ERESTARTNOINTR; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2953 | |
Darren Hart | 42d35d4 | 2008-12-29 15:49:53 -0800 | [diff] [blame] | 2954 | uaddr_faulted: |
Jason Low | 0d00c7b | 2014-01-12 15:31:22 -0800 | [diff] [blame] | 2955 | queue_unlock(hb); |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 2956 | |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 2957 | ret = fault_in_user_writeable(uaddr); |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2958 | if (ret) |
| 2959 | goto out_put_key; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2960 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2961 | if (!(flags & FLAGS_SHARED)) |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2962 | goto retry_private; |
| 2963 | |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 2964 | put_futex_key(&q.key); |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2965 | goto retry; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | /* |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2969 | * Userspace attempted a TID -> 0 atomic transition, and failed. |
| 2970 | * This is the in-kernel slowpath: we look up the PI state (if any), |
| 2971 | * and do the rt-mutex unlock. |
| 2972 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 2973 | static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2974 | { |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 2975 | u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current); |
Peter Zijlstra | 38d47c1 | 2008-09-26 19:32:20 +0200 | [diff] [blame] | 2976 | union futex_key key = FUTEX_KEY_INIT; |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 2977 | struct futex_hash_bucket *hb; |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 2978 | struct futex_q *top_waiter; |
Darren Hart | e4dc5b7 | 2009-03-12 00:56:13 -0700 | [diff] [blame] | 2979 | int ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2980 | |
| 2981 | retry: |
| 2982 | if (get_user(uval, uaddr)) |
| 2983 | return -EFAULT; |
| 2984 | /* |
| 2985 | * We release only a lock we actually own: |
| 2986 | */ |
Thomas Gleixner | c0c9ed1 | 2011-03-11 11:51:22 +0100 | [diff] [blame] | 2987 | if ((uval & FUTEX_TID_MASK) != vpid) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2988 | return -EPERM; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2989 | |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 2990 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE); |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 2991 | if (ret) |
| 2992 | return ret; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2993 | |
| 2994 | hb = hash_futex(&key); |
| 2995 | spin_lock(&hb->lock); |
| 2996 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 2997 | /* |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 2998 | * Check waiters first. We do not trust user space values at |
| 2999 | * all and we at least want to know if user space fiddled |
| 3000 | * with the futex value instead of blindly unlocking. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3001 | */ |
Ben Hutchings | bfefc9e | 2021-03-01 18:30:39 +0100 | [diff] [blame^] | 3002 | top_waiter = futex_top_waiter(hb, &key); |
| 3003 | if (top_waiter) { |
| 3004 | ret = wake_futex_pi(uaddr, uval, top_waiter, hb); |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3005 | /* |
| 3006 | * In case of success wake_futex_pi dropped the hash |
| 3007 | * bucket lock. |
| 3008 | */ |
| 3009 | if (!ret) |
| 3010 | goto out_putkey; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3011 | /* |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3012 | * The atomic access to the futex value generated a |
| 3013 | * pagefault, so retry the user-access and the wakeup: |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3014 | */ |
| 3015 | if (ret == -EFAULT) |
| 3016 | goto pi_faulted; |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3017 | /* |
Sebastian Andrzej Siewior | 89e9e66 | 2016-04-15 14:35:39 +0200 | [diff] [blame] | 3018 | * A unconditional UNLOCK_PI op raced against a waiter |
| 3019 | * setting the FUTEX_WAITERS bit. Try again. |
| 3020 | */ |
| 3021 | if (ret == -EAGAIN) { |
| 3022 | spin_unlock(&hb->lock); |
| 3023 | put_futex_key(&key); |
| 3024 | goto retry; |
| 3025 | } |
| 3026 | /* |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3027 | * wake_futex_pi has detected invalid state. Tell user |
| 3028 | * space. |
| 3029 | */ |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3030 | goto out_unlock; |
| 3031 | } |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3032 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3033 | /* |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3034 | * We have no kernel internal state, i.e. no waiters in the |
| 3035 | * kernel. Waiters which are about to queue themselves are stuck |
| 3036 | * on hb->lock. So we can safely ignore them. We do neither |
| 3037 | * preserve the WAITERS bit not the OWNER_DIED one. We are the |
| 3038 | * owner. |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3039 | */ |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3040 | if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0)) |
Thomas Gleixner | 13fbca4 | 2014-06-03 12:27:07 +0000 | [diff] [blame] | 3041 | goto pi_faulted; |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3042 | |
Thomas Gleixner | ccf9e6a | 2014-06-11 20:45:38 +0000 | [diff] [blame] | 3043 | /* |
| 3044 | * If uval has changed, let user space handle it. |
| 3045 | */ |
| 3046 | ret = (curval == uval) ? 0 : -EAGAIN; |
| 3047 | |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3048 | out_unlock: |
| 3049 | spin_unlock(&hb->lock); |
Sebastian Andrzej Siewior | 802ab58 | 2015-06-17 10:33:50 +0200 | [diff] [blame] | 3050 | out_putkey: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3051 | put_futex_key(&key); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3052 | return ret; |
| 3053 | |
| 3054 | pi_faulted: |
Alexey Kuznetsov | 778e9a9 | 2007-06-08 13:47:00 -0700 | [diff] [blame] | 3055 | spin_unlock(&hb->lock); |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3056 | put_futex_key(&key); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3057 | |
Thomas Gleixner | d072599 | 2009-06-11 23:15:43 +0200 | [diff] [blame] | 3058 | ret = fault_in_user_writeable(uaddr); |
Darren Hart | b568636 | 2008-12-18 15:06:34 -0800 | [diff] [blame] | 3059 | if (!ret) |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3060 | goto retry; |
| 3061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | return ret; |
| 3063 | } |
| 3064 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3065 | /** |
| 3066 | * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex |
| 3067 | * @hb: the hash_bucket futex_q was original enqueued on |
| 3068 | * @q: the futex_q woken while waiting to be requeued |
| 3069 | * @key2: the futex_key of the requeue target futex |
| 3070 | * @timeout: the timeout associated with the wait (NULL if none) |
| 3071 | * |
| 3072 | * Detect if the task was woken on the initial futex as opposed to the requeue |
| 3073 | * target futex. If so, determine if it was a timeout or a signal that caused |
| 3074 | * the wakeup and return the appropriate error code to the caller. Must be |
| 3075 | * called with the hb lock held. |
| 3076 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 3077 | * Return: |
| 3078 | * 0 = no early wakeup detected; |
| 3079 | * <0 = -ETIMEDOUT or -ERESTARTNOINTR |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3080 | */ |
| 3081 | static inline |
| 3082 | int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb, |
| 3083 | struct futex_q *q, union futex_key *key2, |
| 3084 | struct hrtimer_sleeper *timeout) |
| 3085 | { |
| 3086 | int ret = 0; |
| 3087 | |
| 3088 | /* |
| 3089 | * With the hb lock held, we avoid races while we process the wakeup. |
| 3090 | * We only need to hold hb (and not hb2) to ensure atomicity as the |
| 3091 | * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb. |
| 3092 | * It can't be requeued from uaddr2 to something else since we don't |
| 3093 | * support a PI aware source futex for requeue. |
| 3094 | */ |
| 3095 | if (!match_futex(&q->key, key2)) { |
| 3096 | WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr)); |
| 3097 | /* |
| 3098 | * We were woken prior to requeue by a timeout or a signal. |
| 3099 | * Unqueue the futex_q and determine which it was. |
| 3100 | */ |
Lai Jiangshan | 2e12978 | 2010-12-22 14:18:50 +0800 | [diff] [blame] | 3101 | plist_del(&q->list, &hb->chain); |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 3102 | hb_waiters_dec(hb); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3103 | |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 3104 | /* Handle spurious wakeups gracefully */ |
Thomas Gleixner | 11df6dd | 2009-10-28 20:26:48 +0100 | [diff] [blame] | 3105 | ret = -EWOULDBLOCK; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3106 | if (timeout && !timeout->task) |
| 3107 | ret = -ETIMEDOUT; |
Thomas Gleixner | d58e657 | 2009-10-13 20:40:43 +0200 | [diff] [blame] | 3108 | else if (signal_pending(current)) |
Thomas Gleixner | 1c840c1 | 2009-05-20 09:22:40 +0200 | [diff] [blame] | 3109 | ret = -ERESTARTNOINTR; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3110 | } |
| 3111 | return ret; |
| 3112 | } |
| 3113 | |
| 3114 | /** |
| 3115 | * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2 |
Darren Hart | 56ec160 | 2009-09-21 22:29:59 -0700 | [diff] [blame] | 3116 | * @uaddr: the futex we initially wait on (non-pi) |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3117 | * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 3118 | * the same type, no requeueing from private to shared, etc. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3119 | * @val: the expected value of uaddr |
| 3120 | * @abs_time: absolute timeout |
Darren Hart | 56ec160 | 2009-09-21 22:29:59 -0700 | [diff] [blame] | 3121 | * @bitset: 32 bit wakeup bitset set by userspace, defaults to all |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3122 | * @uaddr2: the pi futex we will take prior to returning to user-space |
| 3123 | * |
| 3124 | * 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] | 3125 | * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake |
| 3126 | * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to |
| 3127 | * userspace. This ensures the rt_mutex maintains an owner when it has waiters; |
| 3128 | * without one, the pi logic would not know which task to boost/deboost, if |
| 3129 | * there was a need to. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3130 | * |
| 3131 | * 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] | 3132 | * via the following-- |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3133 | * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue() |
Darren Hart | cc6db4e | 2009-07-31 16:20:10 -0700 | [diff] [blame] | 3134 | * 2) wakeup on uaddr2 after a requeue |
| 3135 | * 3) signal |
| 3136 | * 4) timeout |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3137 | * |
Darren Hart | cc6db4e | 2009-07-31 16:20:10 -0700 | [diff] [blame] | 3138 | * If 3, cleanup and return -ERESTARTNOINTR. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3139 | * |
| 3140 | * If 2, we may then block on trying to take the rt_mutex and return via: |
| 3141 | * 5) successful lock |
| 3142 | * 6) signal |
| 3143 | * 7) timeout |
| 3144 | * 8) other lock acquisition failure |
| 3145 | * |
Darren Hart | cc6db4e | 2009-07-31 16:20:10 -0700 | [diff] [blame] | 3146 | * If 6, return -EWOULDBLOCK (restarting the syscall would do the same). |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3147 | * |
| 3148 | * If 4 or 7, we cleanup and return with -ETIMEDOUT. |
| 3149 | * |
Randy Dunlap | 6c23cbb | 2013-03-05 10:00:24 -0800 | [diff] [blame] | 3150 | * Return: |
| 3151 | * 0 - On success; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3152 | * <0 - On error |
| 3153 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3154 | static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3155 | u32 val, ktime_t *abs_time, u32 bitset, |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3156 | u32 __user *uaddr2) |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3157 | { |
| 3158 | struct hrtimer_sleeper timeout, *to = NULL; |
| 3159 | struct rt_mutex_waiter rt_waiter; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3160 | struct futex_hash_bucket *hb; |
Darren Hart | 5bdb05f | 2010-11-08 13:40:28 -0800 | [diff] [blame] | 3161 | union futex_key key2 = FUTEX_KEY_INIT; |
| 3162 | struct futex_q q = futex_q_init; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3163 | int res, ret; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3164 | |
Darren Hart | 6f7b0a2 | 2012-07-20 11:53:31 -0700 | [diff] [blame] | 3165 | if (uaddr == uaddr2) |
| 3166 | return -EINVAL; |
| 3167 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3168 | if (!bitset) |
| 3169 | return -EINVAL; |
| 3170 | |
| 3171 | if (abs_time) { |
| 3172 | to = &timeout; |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3173 | hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ? |
| 3174 | CLOCK_REALTIME : CLOCK_MONOTONIC, |
| 3175 | HRTIMER_MODE_ABS); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3176 | hrtimer_init_sleeper(to, current); |
| 3177 | hrtimer_set_expires_range_ns(&to->timer, *abs_time, |
| 3178 | current->timer_slack_ns); |
| 3179 | } |
| 3180 | |
| 3181 | /* |
| 3182 | * The waiter is allocated on our stack, manipulated by the requeue |
| 3183 | * code while we sleep on uaddr. |
| 3184 | */ |
| 3185 | debug_rt_mutex_init_waiter(&rt_waiter); |
Peter Zijlstra | fb00aca | 2013-11-07 14:43:43 +0100 | [diff] [blame] | 3186 | RB_CLEAR_NODE(&rt_waiter.pi_tree_entry); |
| 3187 | RB_CLEAR_NODE(&rt_waiter.tree_entry); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3188 | rt_waiter.task = NULL; |
| 3189 | |
Shawn Bohrer | 9ea7150 | 2011-06-30 11:21:32 -0500 | [diff] [blame] | 3190 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3191 | if (unlikely(ret != 0)) |
| 3192 | goto out; |
| 3193 | |
Darren Hart | 84bc4af | 2009-08-13 17:36:53 -0700 | [diff] [blame] | 3194 | q.bitset = bitset; |
| 3195 | q.rt_waiter = &rt_waiter; |
| 3196 | q.requeue_pi_key = &key2; |
| 3197 | |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 3198 | /* |
| 3199 | * Prepare to wait on uaddr. On success, increments q.key (key1) ref |
| 3200 | * count. |
| 3201 | */ |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3202 | ret = futex_wait_setup(uaddr, val, flags, &q, &hb); |
Thomas Gleixner | c8b15a7 | 2009-05-20 09:18:50 +0200 | [diff] [blame] | 3203 | if (ret) |
| 3204 | goto out_key2; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3205 | |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 3206 | /* |
| 3207 | * The check above which compares uaddrs is not sufficient for |
| 3208 | * shared futexes. We need to compare the keys: |
| 3209 | */ |
| 3210 | if (match_futex(&q.key, &key2)) { |
Thomas Gleixner | 13c42c2 | 2014-09-11 23:44:35 +0200 | [diff] [blame] | 3211 | queue_unlock(hb); |
Thomas Gleixner | e9c243a | 2014-06-03 12:27:06 +0000 | [diff] [blame] | 3212 | ret = -EINVAL; |
| 3213 | goto out_put_keys; |
| 3214 | } |
| 3215 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3216 | /* Queue the futex_q, drop the hb lock, wait for wakeup. */ |
Thomas Gleixner | f1a11e0 | 2009-05-05 19:21:40 +0200 | [diff] [blame] | 3217 | futex_wait_queue_me(hb, &q, to); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3218 | |
| 3219 | spin_lock(&hb->lock); |
| 3220 | ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to); |
| 3221 | spin_unlock(&hb->lock); |
| 3222 | if (ret) |
| 3223 | goto out_put_keys; |
| 3224 | |
| 3225 | /* |
| 3226 | * In order for us to be here, we know our q.key == key2, and since |
| 3227 | * we took the hb->lock above, we also know that futex_requeue() has |
| 3228 | * completed and we no longer have to concern ourselves with a wakeup |
Darren Hart | 7ada876 | 2010-10-17 08:35:04 -0700 | [diff] [blame] | 3229 | * race with the atomic proxy lock acquisition by the requeue code. The |
| 3230 | * futex_requeue dropped our key1 reference and incremented our key2 |
| 3231 | * reference count. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3232 | */ |
| 3233 | |
| 3234 | /* Check if the requeue code acquired the second futex for us. */ |
| 3235 | if (!q.rt_waiter) { |
| 3236 | /* |
| 3237 | * Got the lock. We might not be the anticipated owner if we |
| 3238 | * did a lock-steal - fix up the PI-state in that case. |
| 3239 | */ |
| 3240 | if (q.pi_state && (q.pi_state->owner != current)) { |
| 3241 | spin_lock(q.lock_ptr); |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3242 | ret = fixup_pi_state_owner(uaddr2, &q, current); |
Thomas Gleixner | fb75a42 | 2015-12-19 20:07:38 +0000 | [diff] [blame] | 3243 | /* |
| 3244 | * Drop the reference to the pi state which |
| 3245 | * the requeue_pi() code acquired for us. |
| 3246 | */ |
Thomas Gleixner | 29e9ee5 | 2015-12-19 20:07:39 +0000 | [diff] [blame] | 3247 | put_pi_state(q.pi_state); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3248 | spin_unlock(q.lock_ptr); |
Thomas Gleixner | ad8fdba | 2021-02-11 09:26:58 +0000 | [diff] [blame] | 3249 | /* |
| 3250 | * Adjust the return value. It's either -EFAULT or |
| 3251 | * success (1) but the caller expects 0 for success. |
| 3252 | */ |
| 3253 | ret = ret < 0 ? ret : 0; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3254 | } |
| 3255 | } else { |
Peter Zijlstra | 6244ffc | 2017-03-04 10:27:18 +0100 | [diff] [blame] | 3256 | struct rt_mutex *pi_mutex; |
| 3257 | |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3258 | /* |
| 3259 | * We have been woken up by futex_unlock_pi(), a timeout, or a |
| 3260 | * signal. futex_unlock_pi() will not destroy the lock_ptr nor |
| 3261 | * the pi_state. |
| 3262 | */ |
Darren Hart | f27071c | 2012-07-20 11:53:30 -0700 | [diff] [blame] | 3263 | WARN_ON(!q.pi_state); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3264 | pi_mutex = &q.pi_state->pi_mutex; |
Peter Zijlstra | ce81355 | 2017-03-22 11:35:57 +0100 | [diff] [blame] | 3265 | ret = rt_mutex_wait_proxy_lock(pi_mutex, to, &rt_waiter); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3266 | |
| 3267 | spin_lock(q.lock_ptr); |
Peter Zijlstra | ce81355 | 2017-03-22 11:35:57 +0100 | [diff] [blame] | 3268 | if (ret && !rt_mutex_cleanup_proxy_lock(pi_mutex, &rt_waiter)) |
| 3269 | ret = 0; |
| 3270 | |
| 3271 | debug_rt_mutex_free_waiter(&rt_waiter); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3272 | /* |
| 3273 | * Fixup the pi_state owner and possibly acquire the lock if we |
| 3274 | * haven't already. |
| 3275 | */ |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3276 | res = fixup_owner(uaddr2, &q, !ret); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3277 | /* |
| 3278 | * If fixup_owner() returned an error, proprogate that. If it |
Darren Hart | 56ec160 | 2009-09-21 22:29:59 -0700 | [diff] [blame] | 3279 | * acquired the lock, clear -ETIMEDOUT or -EINTR. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3280 | */ |
| 3281 | if (res) |
| 3282 | ret = (res < 0) ? res : 0; |
| 3283 | |
| 3284 | /* Unqueue and drop the lock. */ |
| 3285 | unqueue_me_pi(&q); |
| 3286 | } |
| 3287 | |
Peter Zijlstra | 6244ffc | 2017-03-04 10:27:18 +0100 | [diff] [blame] | 3288 | if (ret == -EINTR) { |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3289 | /* |
Darren Hart | cc6db4e | 2009-07-31 16:20:10 -0700 | [diff] [blame] | 3290 | * We've already been requeued, but cannot restart by calling |
| 3291 | * futex_lock_pi() directly. We could restart this syscall, but |
| 3292 | * it would detect that the user space "val" changed and return |
| 3293 | * -EWOULDBLOCK. Save the overhead of the restart and return |
| 3294 | * -EWOULDBLOCK directly. |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3295 | */ |
Thomas Gleixner | 2070887 | 2009-05-19 23:04:59 +0200 | [diff] [blame] | 3296 | ret = -EWOULDBLOCK; |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | out_put_keys: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3300 | put_futex_key(&q.key); |
Thomas Gleixner | c8b15a7 | 2009-05-20 09:18:50 +0200 | [diff] [blame] | 3301 | out_key2: |
Thomas Gleixner | ae791a2 | 2010-11-10 13:30:36 +0100 | [diff] [blame] | 3302 | put_futex_key(&key2); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3303 | |
| 3304 | out: |
| 3305 | if (to) { |
| 3306 | hrtimer_cancel(&to->timer); |
| 3307 | destroy_hrtimer_on_stack(&to->timer); |
| 3308 | } |
| 3309 | return ret; |
| 3310 | } |
| 3311 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3312 | /* |
| 3313 | * Support for robust futexes: the kernel cleans up held futexes at |
| 3314 | * thread exit time. |
| 3315 | * |
| 3316 | * Implementation: user-space maintains a per-thread list of locks it |
| 3317 | * is holding. Upon do_exit(), the kernel carefully walks this list, |
| 3318 | * and marks all locks that are owned by this thread with the |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3319 | * 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] | 3320 | * always manipulated with the lock held, so the list is private and |
| 3321 | * per-thread. Userspace also maintains a per-thread 'list_op_pending' |
| 3322 | * field, to allow the kernel to clean up if the thread dies after |
| 3323 | * acquiring the lock, but just before it could have added itself to |
| 3324 | * the list. There can only be one such pending lock. |
| 3325 | */ |
| 3326 | |
| 3327 | /** |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 3328 | * sys_set_robust_list() - Set the robust-futex list head of a task |
| 3329 | * @head: pointer to the list-head |
| 3330 | * @len: length of the list-head, as userspace expects |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3331 | */ |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 3332 | SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head, |
| 3333 | size_t, len) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3334 | { |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 3335 | if (!futex_cmpxchg_enabled) |
| 3336 | return -ENOSYS; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3337 | /* |
| 3338 | * The kernel knows only one size for now: |
| 3339 | */ |
| 3340 | if (unlikely(len != sizeof(*head))) |
| 3341 | return -EINVAL; |
| 3342 | |
| 3343 | current->robust_list = head; |
| 3344 | |
| 3345 | return 0; |
| 3346 | } |
| 3347 | |
| 3348 | /** |
Darren Hart | d96ee56 | 2009-09-21 22:30:22 -0700 | [diff] [blame] | 3349 | * sys_get_robust_list() - Get the robust-futex list head of a task |
| 3350 | * @pid: pid of the process [zero for current task] |
| 3351 | * @head_ptr: pointer to a list-head pointer, the kernel fills it in |
| 3352 | * @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] | 3353 | */ |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 3354 | SYSCALL_DEFINE3(get_robust_list, int, pid, |
| 3355 | struct robust_list_head __user * __user *, head_ptr, |
| 3356 | size_t __user *, len_ptr) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3357 | { |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3358 | struct robust_list_head __user *head; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3359 | unsigned long ret; |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3360 | struct task_struct *p; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3361 | |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 3362 | if (!futex_cmpxchg_enabled) |
| 3363 | return -ENOSYS; |
| 3364 | |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3365 | rcu_read_lock(); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3366 | |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3367 | ret = -ESRCH; |
| 3368 | if (!pid) |
| 3369 | p = current; |
| 3370 | else { |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 3371 | p = find_task_by_vpid(pid); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3372 | if (!p) |
| 3373 | goto err_unlock; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3374 | } |
| 3375 | |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3376 | ret = -EPERM; |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 3377 | if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) |
Kees Cook | bdbb776 | 2012-03-19 16:12:53 -0700 | [diff] [blame] | 3378 | goto err_unlock; |
| 3379 | |
| 3380 | head = p->robust_list; |
| 3381 | rcu_read_unlock(); |
| 3382 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3383 | if (put_user(sizeof(*head), len_ptr)) |
| 3384 | return -EFAULT; |
| 3385 | return put_user(head, head_ptr); |
| 3386 | |
| 3387 | err_unlock: |
Oleg Nesterov | aaa2a97 | 2006-09-29 02:00:55 -0700 | [diff] [blame] | 3388 | rcu_read_unlock(); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3389 | |
| 3390 | return ret; |
| 3391 | } |
| 3392 | |
| 3393 | /* |
| 3394 | * Process a futex-list entry, check whether it's owned by the |
| 3395 | * dying task, and do notification if so: |
| 3396 | */ |
Arnd Bergmann | bdb116c | 2021-02-01 10:01:32 +0000 | [diff] [blame] | 3397 | 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] | 3398 | { |
Vitaliy Ivanov | 7cfdaf3 | 2011-07-07 15:10:31 +0300 | [diff] [blame] | 3399 | u32 uval, uninitialized_var(nval), mval; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3400 | |
Chen Jie | 726c28f | 2019-03-15 03:44:38 +0000 | [diff] [blame] | 3401 | /* Futex address must be 32bit aligned */ |
| 3402 | if ((((unsigned long)uaddr) % sizeof(*uaddr)) != 0) |
| 3403 | return -1; |
| 3404 | |
Ingo Molnar | 8f17d3a | 2006-03-27 01:16:27 -0800 | [diff] [blame] | 3405 | retry: |
| 3406 | if (get_user(uval, uaddr)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3407 | return -1; |
| 3408 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3409 | if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) { |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3410 | /* |
| 3411 | * Ok, this dying thread is truly holding a futex |
| 3412 | * of interest. Set the OWNER_DIED bit atomically |
| 3413 | * via cmpxchg, and if the value had FUTEX_WAITERS |
| 3414 | * set, wake up a waiter (if any). (We have to do a |
| 3415 | * futex_wake() even if OWNER_DIED is already set - |
| 3416 | * to handle the rare but possible case of recursive |
| 3417 | * thread-death.) The rest of the cleanup is done in |
| 3418 | * userspace. |
| 3419 | */ |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3420 | mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED; |
Thomas Gleixner | 6e0aa9f | 2011-03-14 10:34:35 +0100 | [diff] [blame] | 3421 | /* |
| 3422 | * We are not holding a lock here, but we want to have |
| 3423 | * the pagefault_disable/enable() protection because |
| 3424 | * we want to handle the fault gracefully. If the |
| 3425 | * access fails we try to fault in the futex with R/W |
| 3426 | * verification via get_user_pages. get_user() above |
| 3427 | * does not guarantee R/W access. If that fails we |
| 3428 | * give up and leave the futex locked. |
| 3429 | */ |
| 3430 | if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) { |
| 3431 | if (fault_in_user_writeable(uaddr)) |
| 3432 | return -1; |
| 3433 | goto retry; |
| 3434 | } |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3435 | if (nval != uval) |
Ingo Molnar | 8f17d3a | 2006-03-27 01:16:27 -0800 | [diff] [blame] | 3436 | goto retry; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3437 | |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3438 | /* |
| 3439 | * Wake robust non-PI futexes here. The wakeup of |
| 3440 | * PI futexes happens in exit_pi_state(): |
| 3441 | */ |
Thomas Gleixner | 36cf3b5 | 2007-07-15 23:41:20 -0700 | [diff] [blame] | 3442 | if (!pi && (uval & FUTEX_WAITERS)) |
Peter Zijlstra | c2f9f20 | 2008-09-26 19:32:23 +0200 | [diff] [blame] | 3443 | futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3444 | } |
| 3445 | return 0; |
| 3446 | } |
| 3447 | |
| 3448 | /* |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3449 | * Fetch a robust-list pointer. Bit 0 signals PI futexes: |
| 3450 | */ |
| 3451 | static inline int fetch_robust_entry(struct robust_list __user **entry, |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3452 | struct robust_list __user * __user *head, |
Namhyung Kim | 1dcc41b | 2010-09-14 21:43:46 +0900 | [diff] [blame] | 3453 | unsigned int *pi) |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3454 | { |
| 3455 | unsigned long uentry; |
| 3456 | |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3457 | if (get_user(uentry, (unsigned long __user *)head)) |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3458 | return -EFAULT; |
| 3459 | |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3460 | *entry = (void __user *)(uentry & ~1UL); |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3461 | *pi = uentry & 1; |
| 3462 | |
| 3463 | return 0; |
| 3464 | } |
| 3465 | |
| 3466 | /* |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3467 | * Walk curr->robust_list (very carefully, it's a userspace list!) |
| 3468 | * and mark any locks found there dead, and notify any waiters. |
| 3469 | * |
| 3470 | * We silently return on any sign of list-walking problem. |
| 3471 | */ |
Thomas Gleixner | 25f319b | 2021-02-01 10:01:33 +0000 | [diff] [blame] | 3472 | static void exit_robust_list(struct task_struct *curr) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3473 | { |
| 3474 | struct robust_list_head __user *head = curr->robust_list; |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3475 | struct robust_list __user *entry, *next_entry, *pending; |
Darren Hart | 4c115e9 | 2010-11-04 15:00:00 -0400 | [diff] [blame] | 3476 | unsigned int limit = ROBUST_LIST_LIMIT, pi, pip; |
| 3477 | unsigned int uninitialized_var(next_pi); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3478 | unsigned long futex_offset; |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3479 | int rc; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3480 | |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 3481 | if (!futex_cmpxchg_enabled) |
| 3482 | return; |
| 3483 | |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3484 | /* |
| 3485 | * Fetch the list head (which was registered earlier, via |
| 3486 | * sys_set_robust_list()): |
| 3487 | */ |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3488 | if (fetch_robust_entry(&entry, &head->list.next, &pi)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3489 | return; |
| 3490 | /* |
| 3491 | * Fetch the relative futex offset: |
| 3492 | */ |
| 3493 | if (get_user(futex_offset, &head->futex_offset)) |
| 3494 | return; |
| 3495 | /* |
| 3496 | * Fetch any possibly pending lock-add first, and handle it |
| 3497 | * if it exists: |
| 3498 | */ |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3499 | if (fetch_robust_entry(&pending, &head->list_op_pending, &pip)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3500 | return; |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3501 | |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3502 | next_entry = NULL; /* avoid warning with gcc */ |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3503 | while (entry != &head->list) { |
| 3504 | /* |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3505 | * Fetch the next entry in the list before calling |
| 3506 | * handle_futex_death: |
| 3507 | */ |
| 3508 | rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi); |
| 3509 | /* |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3510 | * A pending lock might already be on the list, so |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3511 | * don't process it twice: |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3512 | */ |
| 3513 | if (entry != pending) |
Al Viro | ba46df9 | 2006-10-10 22:46:07 +0100 | [diff] [blame] | 3514 | if (handle_futex_death((void __user *)entry + futex_offset, |
Ingo Molnar | e3f2dde | 2006-07-29 05:17:57 +0200 | [diff] [blame] | 3515 | curr, pi)) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3516 | return; |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3517 | if (rc) |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3518 | return; |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3519 | entry = next_entry; |
| 3520 | pi = next_pi; |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3521 | /* |
| 3522 | * Avoid excessively long or circular lists: |
| 3523 | */ |
| 3524 | if (!--limit) |
| 3525 | break; |
| 3526 | |
| 3527 | cond_resched(); |
| 3528 | } |
Martin Schwidefsky | 9f96cb1 | 2007-10-01 01:20:13 -0700 | [diff] [blame] | 3529 | |
| 3530 | if (pending) |
| 3531 | handle_futex_death((void __user *)pending + futex_offset, |
| 3532 | curr, pip); |
Ingo Molnar | 0771dfe | 2006-03-27 01:16:22 -0800 | [diff] [blame] | 3533 | } |
| 3534 | |
Thomas Gleixner | ff3a33f | 2021-02-01 10:01:40 +0000 | [diff] [blame] | 3535 | static void futex_cleanup(struct task_struct *tsk) |
Thomas Gleixner | 25f319b | 2021-02-01 10:01:33 +0000 | [diff] [blame] | 3536 | { |
| 3537 | if (unlikely(tsk->robust_list)) { |
| 3538 | exit_robust_list(tsk); |
| 3539 | tsk->robust_list = NULL; |
| 3540 | } |
| 3541 | |
| 3542 | #ifdef CONFIG_COMPAT |
| 3543 | if (unlikely(tsk->compat_robust_list)) { |
| 3544 | compat_exit_robust_list(tsk); |
| 3545 | tsk->compat_robust_list = NULL; |
| 3546 | } |
| 3547 | #endif |
| 3548 | |
| 3549 | if (unlikely(!list_empty(&tsk->pi_state_list))) |
| 3550 | exit_pi_state_list(tsk); |
| 3551 | } |
| 3552 | |
Thomas Gleixner | 32d7828 | 2021-02-01 10:01:38 +0000 | [diff] [blame] | 3553 | /** |
| 3554 | * futex_exit_recursive - Set the tasks futex state to FUTEX_STATE_DEAD |
| 3555 | * @tsk: task to set the state on |
| 3556 | * |
| 3557 | * Set the futex exit state of the task lockless. The futex waiter code |
| 3558 | * observes that state when a task is exiting and loops until the task has |
| 3559 | * actually finished the futex cleanup. The worst case for this is that the |
| 3560 | * waiter runs through the wait loop until the state becomes visible. |
| 3561 | * |
| 3562 | * This is called from the recursive fault handling path in do_exit(). |
| 3563 | * |
| 3564 | * This is best effort. Either the futex exit code has run already or |
| 3565 | * not. If the OWNER_DIED bit has been set on the futex then the waiter can |
| 3566 | * take it over. If not, the problem is pushed back to user space. If the |
| 3567 | * futex exit code did not run yet, then an already queued waiter might |
| 3568 | * block forever, but there is nothing which can be done about that. |
| 3569 | */ |
| 3570 | void futex_exit_recursive(struct task_struct *tsk) |
| 3571 | { |
Thomas Gleixner | ad3466a | 2021-02-01 10:01:41 +0000 | [diff] [blame] | 3572 | /* If the state is FUTEX_STATE_EXITING then futex_exit_mutex is held */ |
| 3573 | if (tsk->futex_state == FUTEX_STATE_EXITING) |
| 3574 | mutex_unlock(&tsk->futex_exit_mutex); |
Thomas Gleixner | 32d7828 | 2021-02-01 10:01:38 +0000 | [diff] [blame] | 3575 | tsk->futex_state = FUTEX_STATE_DEAD; |
| 3576 | } |
| 3577 | |
Thomas Gleixner | ff3a33f | 2021-02-01 10:01:40 +0000 | [diff] [blame] | 3578 | static void futex_cleanup_begin(struct task_struct *tsk) |
Thomas Gleixner | 8a16d8a | 2021-02-01 10:01:36 +0000 | [diff] [blame] | 3579 | { |
Thomas Gleixner | 32d7828 | 2021-02-01 10:01:38 +0000 | [diff] [blame] | 3580 | /* |
Thomas Gleixner | ad3466a | 2021-02-01 10:01:41 +0000 | [diff] [blame] | 3581 | * Prevent various race issues against a concurrent incoming waiter |
| 3582 | * including live locks by forcing the waiter to block on |
| 3583 | * tsk->futex_exit_mutex when it observes FUTEX_STATE_EXITING in |
| 3584 | * attach_to_pi_owner(). |
| 3585 | */ |
| 3586 | mutex_lock(&tsk->futex_exit_mutex); |
| 3587 | |
| 3588 | /* |
Thomas Gleixner | 0ba263f | 2021-02-01 10:01:39 +0000 | [diff] [blame] | 3589 | * Switch the state to FUTEX_STATE_EXITING under tsk->pi_lock. |
| 3590 | * |
| 3591 | * This ensures that all subsequent checks of tsk->futex_state in |
| 3592 | * attach_to_pi_owner() must observe FUTEX_STATE_EXITING with |
| 3593 | * tsk->pi_lock held. |
| 3594 | * |
| 3595 | * It guarantees also that a pi_state which was queued right before |
| 3596 | * the state change under tsk->pi_lock by a concurrent waiter must |
| 3597 | * be observed in exit_pi_state_list(). |
Thomas Gleixner | 32d7828 | 2021-02-01 10:01:38 +0000 | [diff] [blame] | 3598 | */ |
| 3599 | raw_spin_lock_irq(&tsk->pi_lock); |
Thomas Gleixner | 0ba263f | 2021-02-01 10:01:39 +0000 | [diff] [blame] | 3600 | tsk->futex_state = FUTEX_STATE_EXITING; |
Thomas Gleixner | 32d7828 | 2021-02-01 10:01:38 +0000 | [diff] [blame] | 3601 | raw_spin_unlock_irq(&tsk->pi_lock); |
Thomas Gleixner | ff3a33f | 2021-02-01 10:01:40 +0000 | [diff] [blame] | 3602 | } |
Thomas Gleixner | 32d7828 | 2021-02-01 10:01:38 +0000 | [diff] [blame] | 3603 | |
Thomas Gleixner | ff3a33f | 2021-02-01 10:01:40 +0000 | [diff] [blame] | 3604 | static void futex_cleanup_end(struct task_struct *tsk, int state) |
| 3605 | { |
| 3606 | /* |
| 3607 | * Lockless store. The only side effect is that an observer might |
| 3608 | * take another loop until it becomes visible. |
| 3609 | */ |
| 3610 | tsk->futex_state = state; |
Thomas Gleixner | ad3466a | 2021-02-01 10:01:41 +0000 | [diff] [blame] | 3611 | /* |
| 3612 | * Drop the exit protection. This unblocks waiters which observed |
| 3613 | * FUTEX_STATE_EXITING to reevaluate the state. |
| 3614 | */ |
| 3615 | mutex_unlock(&tsk->futex_exit_mutex); |
Thomas Gleixner | ff3a33f | 2021-02-01 10:01:40 +0000 | [diff] [blame] | 3616 | } |
Thomas Gleixner | 32d7828 | 2021-02-01 10:01:38 +0000 | [diff] [blame] | 3617 | |
Thomas Gleixner | ff3a33f | 2021-02-01 10:01:40 +0000 | [diff] [blame] | 3618 | void futex_exec_release(struct task_struct *tsk) |
| 3619 | { |
| 3620 | /* |
| 3621 | * The state handling is done for consistency, but in the case of |
| 3622 | * exec() there is no way to prevent futher damage as the PID stays |
| 3623 | * the same. But for the unlikely and arguably buggy case that a |
| 3624 | * futex is held on exec(), this provides at least as much state |
| 3625 | * consistency protection which is possible. |
| 3626 | */ |
| 3627 | futex_cleanup_begin(tsk); |
| 3628 | futex_cleanup(tsk); |
| 3629 | /* |
| 3630 | * Reset the state to FUTEX_STATE_OK. The task is alive and about |
| 3631 | * exec a new binary. |
| 3632 | */ |
| 3633 | futex_cleanup_end(tsk, FUTEX_STATE_OK); |
| 3634 | } |
| 3635 | |
| 3636 | void futex_exit_release(struct task_struct *tsk) |
| 3637 | { |
| 3638 | futex_cleanup_begin(tsk); |
| 3639 | futex_cleanup(tsk); |
| 3640 | futex_cleanup_end(tsk, FUTEX_STATE_DEAD); |
Thomas Gleixner | 8a16d8a | 2021-02-01 10:01:36 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3643 | 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] | 3644 | u32 __user *uaddr2, u32 val2, u32 val3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | { |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3646 | int cmd = op & FUTEX_CMD_MASK; |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3647 | unsigned int flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3649 | if (!(op & FUTEX_PRIVATE_FLAG)) |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3650 | flags |= FLAGS_SHARED; |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3651 | |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3652 | if (op & FUTEX_CLOCK_REALTIME) { |
| 3653 | flags |= FLAGS_CLOCKRT; |
Darren Hart | 337f130 | 2015-12-18 13:36:37 -0800 | [diff] [blame] | 3654 | if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && \ |
| 3655 | cmd != FUTEX_WAIT_REQUEUE_PI) |
Darren Hart | b41277d | 2010-11-08 13:10:09 -0800 | [diff] [blame] | 3656 | return -ENOSYS; |
| 3657 | } |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3658 | |
| 3659 | switch (cmd) { |
Thomas Gleixner | 59263b5 | 2012-02-15 12:08:34 +0100 | [diff] [blame] | 3660 | case FUTEX_LOCK_PI: |
| 3661 | case FUTEX_UNLOCK_PI: |
| 3662 | case FUTEX_TRYLOCK_PI: |
| 3663 | case FUTEX_WAIT_REQUEUE_PI: |
| 3664 | case FUTEX_CMP_REQUEUE_PI: |
| 3665 | if (!futex_cmpxchg_enabled) |
| 3666 | return -ENOSYS; |
| 3667 | } |
| 3668 | |
| 3669 | switch (cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | case FUTEX_WAIT: |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 3671 | val3 = FUTEX_BITSET_MATCH_ANY; |
| 3672 | case FUTEX_WAIT_BITSET: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3673 | return futex_wait(uaddr, flags, val, timeout, val3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | case FUTEX_WAKE: |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 3675 | val3 = FUTEX_BITSET_MATCH_ANY; |
| 3676 | case FUTEX_WAKE_BITSET: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3677 | return futex_wake(uaddr, flags, val, val3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | case FUTEX_REQUEUE: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3679 | return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | case FUTEX_CMP_REQUEUE: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3681 | return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0); |
Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 3682 | case FUTEX_WAKE_OP: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3683 | return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3684 | case FUTEX_LOCK_PI: |
Michael Kerrisk | 996636d | 2015-01-16 20:28:06 +0100 | [diff] [blame] | 3685 | return futex_lock_pi(uaddr, flags, timeout, 0); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3686 | case FUTEX_UNLOCK_PI: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3687 | return futex_unlock_pi(uaddr, flags); |
Ingo Molnar | c87e283 | 2006-06-27 02:54:58 -0700 | [diff] [blame] | 3688 | case FUTEX_TRYLOCK_PI: |
Michael Kerrisk | 996636d | 2015-01-16 20:28:06 +0100 | [diff] [blame] | 3689 | return futex_lock_pi(uaddr, flags, NULL, 1); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3690 | case FUTEX_WAIT_REQUEUE_PI: |
| 3691 | val3 = FUTEX_BITSET_MATCH_ANY; |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3692 | return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3, |
| 3693 | uaddr2); |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3694 | case FUTEX_CMP_REQUEUE_PI: |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3695 | return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | } |
Thomas Gleixner | 81b4053 | 2012-02-15 12:17:09 +0100 | [diff] [blame] | 3697 | return -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | |
Heiko Carstens | 17da2bd | 2009-01-14 14:14:10 +0100 | [diff] [blame] | 3701 | SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val, |
| 3702 | struct timespec __user *, utime, u32 __user *, uaddr2, |
| 3703 | u32, val3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | { |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3705 | struct timespec ts; |
| 3706 | ktime_t t, *tp = NULL; |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 3707 | u32 val2 = 0; |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3708 | int cmd = op & FUTEX_CMD_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | |
Thomas Gleixner | cd68998 | 2008-02-01 17:45:14 +0100 | [diff] [blame] | 3710 | if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI || |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3711 | cmd == FUTEX_WAIT_BITSET || |
| 3712 | cmd == FUTEX_WAIT_REQUEUE_PI)) { |
Davidlohr Bueso | ab51fba | 2015-06-29 23:26:02 -0700 | [diff] [blame] | 3713 | if (unlikely(should_fail_futex(!(op & FUTEX_PRIVATE_FLAG)))) |
| 3714 | return -EFAULT; |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3715 | if (copy_from_user(&ts, utime, sizeof(ts)) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | return -EFAULT; |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3717 | if (!timespec_valid(&ts)) |
Thomas Gleixner | 9741ef96 | 2006-03-31 02:31:32 -0800 | [diff] [blame] | 3718 | return -EINVAL; |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3719 | |
| 3720 | t = timespec_to_ktime(ts); |
Eric Dumazet | 34f01cc | 2007-05-09 02:35:04 -0700 | [diff] [blame] | 3721 | if (cmd == FUTEX_WAIT) |
Thomas Gleixner | 5a7780e | 2008-02-13 09:20:43 +0100 | [diff] [blame] | 3722 | t = ktime_add_safe(ktime_get(), t); |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3723 | tp = &t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | } |
| 3725 | /* |
Darren Hart | 52400ba | 2009-04-03 13:40:49 -0700 | [diff] [blame] | 3726 | * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*. |
Andreas Schwab | f54f098 | 2007-07-31 00:38:51 -0700 | [diff] [blame] | 3727 | * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3728 | */ |
Andreas Schwab | f54f098 | 2007-07-31 00:38:51 -0700 | [diff] [blame] | 3729 | if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE || |
Darren Hart | ba9c22f | 2009-04-20 22:22:22 -0700 | [diff] [blame] | 3730 | cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP) |
Ingo Molnar | e2970f2 | 2006-06-27 02:54:47 -0700 | [diff] [blame] | 3731 | val2 = (u32) (unsigned long) utime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3732 | |
Pierre Peiffer | c19384b | 2007-05-09 02:35:02 -0700 | [diff] [blame] | 3733 | return do_futex(uaddr, op, val, tp, uaddr2, val2, val3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | } |
| 3735 | |
Arnd Bergmann | bdb116c | 2021-02-01 10:01:32 +0000 | [diff] [blame] | 3736 | #ifdef CONFIG_COMPAT |
| 3737 | /* |
| 3738 | * Fetch a robust-list pointer. Bit 0 signals PI futexes: |
| 3739 | */ |
| 3740 | static inline int |
| 3741 | compat_fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry, |
| 3742 | compat_uptr_t __user *head, unsigned int *pi) |
| 3743 | { |
| 3744 | if (get_user(*uentry, head)) |
| 3745 | return -EFAULT; |
| 3746 | |
| 3747 | *entry = compat_ptr((*uentry) & ~1); |
| 3748 | *pi = (unsigned int)(*uentry) & 1; |
| 3749 | |
| 3750 | return 0; |
| 3751 | } |
| 3752 | |
| 3753 | static void __user *futex_uaddr(struct robust_list __user *entry, |
| 3754 | compat_long_t futex_offset) |
| 3755 | { |
| 3756 | compat_uptr_t base = ptr_to_compat(entry); |
| 3757 | void __user *uaddr = compat_ptr(base + futex_offset); |
| 3758 | |
| 3759 | return uaddr; |
| 3760 | } |
| 3761 | |
| 3762 | /* |
| 3763 | * Walk curr->robust_list (very carefully, it's a userspace list!) |
| 3764 | * and mark any locks found there dead, and notify any waiters. |
| 3765 | * |
| 3766 | * We silently return on any sign of list-walking problem. |
| 3767 | */ |
| 3768 | void compat_exit_robust_list(struct task_struct *curr) |
| 3769 | { |
| 3770 | struct compat_robust_list_head __user *head = curr->compat_robust_list; |
| 3771 | struct robust_list __user *entry, *next_entry, *pending; |
| 3772 | unsigned int limit = ROBUST_LIST_LIMIT, pi, pip; |
| 3773 | unsigned int uninitialized_var(next_pi); |
| 3774 | compat_uptr_t uentry, next_uentry, upending; |
| 3775 | compat_long_t futex_offset; |
| 3776 | int rc; |
| 3777 | |
| 3778 | if (!futex_cmpxchg_enabled) |
| 3779 | return; |
| 3780 | |
| 3781 | /* |
| 3782 | * Fetch the list head (which was registered earlier, via |
| 3783 | * sys_set_robust_list()): |
| 3784 | */ |
| 3785 | if (compat_fetch_robust_entry(&uentry, &entry, &head->list.next, &pi)) |
| 3786 | return; |
| 3787 | /* |
| 3788 | * Fetch the relative futex offset: |
| 3789 | */ |
| 3790 | if (get_user(futex_offset, &head->futex_offset)) |
| 3791 | return; |
| 3792 | /* |
| 3793 | * Fetch any possibly pending lock-add first, and handle it |
| 3794 | * if it exists: |
| 3795 | */ |
| 3796 | if (compat_fetch_robust_entry(&upending, &pending, |
| 3797 | &head->list_op_pending, &pip)) |
| 3798 | return; |
| 3799 | |
| 3800 | next_entry = NULL; /* avoid warning with gcc */ |
| 3801 | while (entry != (struct robust_list __user *) &head->list) { |
| 3802 | /* |
| 3803 | * Fetch the next entry in the list before calling |
| 3804 | * handle_futex_death: |
| 3805 | */ |
| 3806 | rc = compat_fetch_robust_entry(&next_uentry, &next_entry, |
| 3807 | (compat_uptr_t __user *)&entry->next, &next_pi); |
| 3808 | /* |
| 3809 | * A pending lock might already be on the list, so |
| 3810 | * dont process it twice: |
| 3811 | */ |
| 3812 | if (entry != pending) { |
| 3813 | void __user *uaddr = futex_uaddr(entry, futex_offset); |
| 3814 | |
| 3815 | if (handle_futex_death(uaddr, curr, pi)) |
| 3816 | return; |
| 3817 | } |
| 3818 | if (rc) |
| 3819 | return; |
| 3820 | uentry = next_uentry; |
| 3821 | entry = next_entry; |
| 3822 | pi = next_pi; |
| 3823 | /* |
| 3824 | * Avoid excessively long or circular lists: |
| 3825 | */ |
| 3826 | if (!--limit) |
| 3827 | break; |
| 3828 | |
| 3829 | cond_resched(); |
| 3830 | } |
| 3831 | if (pending) { |
| 3832 | void __user *uaddr = futex_uaddr(pending, futex_offset); |
| 3833 | |
| 3834 | handle_futex_death(uaddr, curr, pip); |
| 3835 | } |
| 3836 | } |
| 3837 | |
| 3838 | COMPAT_SYSCALL_DEFINE2(set_robust_list, |
| 3839 | struct compat_robust_list_head __user *, head, |
| 3840 | compat_size_t, len) |
| 3841 | { |
| 3842 | if (!futex_cmpxchg_enabled) |
| 3843 | return -ENOSYS; |
| 3844 | |
| 3845 | if (unlikely(len != sizeof(*head))) |
| 3846 | return -EINVAL; |
| 3847 | |
| 3848 | current->compat_robust_list = head; |
| 3849 | |
| 3850 | return 0; |
| 3851 | } |
| 3852 | |
| 3853 | COMPAT_SYSCALL_DEFINE3(get_robust_list, int, pid, |
| 3854 | compat_uptr_t __user *, head_ptr, |
| 3855 | compat_size_t __user *, len_ptr) |
| 3856 | { |
| 3857 | struct compat_robust_list_head __user *head; |
| 3858 | unsigned long ret; |
| 3859 | struct task_struct *p; |
| 3860 | |
| 3861 | if (!futex_cmpxchg_enabled) |
| 3862 | return -ENOSYS; |
| 3863 | |
| 3864 | rcu_read_lock(); |
| 3865 | |
| 3866 | ret = -ESRCH; |
| 3867 | if (!pid) |
| 3868 | p = current; |
| 3869 | else { |
| 3870 | p = find_task_by_vpid(pid); |
| 3871 | if (!p) |
| 3872 | goto err_unlock; |
| 3873 | } |
| 3874 | |
| 3875 | ret = -EPERM; |
| 3876 | if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS)) |
| 3877 | goto err_unlock; |
| 3878 | |
| 3879 | head = p->compat_robust_list; |
| 3880 | rcu_read_unlock(); |
| 3881 | |
| 3882 | if (put_user(sizeof(*head), len_ptr)) |
| 3883 | return -EFAULT; |
| 3884 | return put_user(ptr_to_compat(head), head_ptr); |
| 3885 | |
| 3886 | err_unlock: |
| 3887 | rcu_read_unlock(); |
| 3888 | |
| 3889 | return ret; |
| 3890 | } |
| 3891 | |
| 3892 | COMPAT_SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val, |
| 3893 | struct compat_timespec __user *, utime, u32 __user *, uaddr2, |
| 3894 | u32, val3) |
| 3895 | { |
| 3896 | struct timespec ts; |
| 3897 | ktime_t t, *tp = NULL; |
| 3898 | int val2 = 0; |
| 3899 | int cmd = op & FUTEX_CMD_MASK; |
| 3900 | |
| 3901 | if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI || |
| 3902 | cmd == FUTEX_WAIT_BITSET || |
| 3903 | cmd == FUTEX_WAIT_REQUEUE_PI)) { |
| 3904 | if (compat_get_timespec(&ts, utime)) |
| 3905 | return -EFAULT; |
| 3906 | if (!timespec_valid(&ts)) |
| 3907 | return -EINVAL; |
| 3908 | |
| 3909 | t = timespec_to_ktime(ts); |
| 3910 | if (cmd == FUTEX_WAIT) |
| 3911 | t = ktime_add_safe(ktime_get(), t); |
| 3912 | tp = &t; |
| 3913 | } |
| 3914 | if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE || |
| 3915 | cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP) |
| 3916 | val2 = (int) (unsigned long) utime; |
| 3917 | |
| 3918 | return do_futex(uaddr, op, val, tp, uaddr2, val2, val3); |
| 3919 | } |
| 3920 | #endif /* CONFIG_COMPAT */ |
| 3921 | |
Heiko Carstens | 03b8c7b | 2014-03-02 13:09:47 +0100 | [diff] [blame] | 3922 | static void __init futex_detect_cmpxchg(void) |
| 3923 | { |
| 3924 | #ifndef CONFIG_HAVE_FUTEX_CMPXCHG |
| 3925 | u32 curval; |
| 3926 | |
| 3927 | /* |
| 3928 | * This will fail and we want it. Some arch implementations do |
| 3929 | * runtime detection of the futex_atomic_cmpxchg_inatomic() |
| 3930 | * functionality. We want to know that before we call in any |
| 3931 | * of the complex code paths. Also we want to prevent |
| 3932 | * registration of robust lists in that case. NULL is |
| 3933 | * guaranteed to fault and we get -EFAULT on functional |
| 3934 | * implementation, the non-functional ones will return |
| 3935 | * -ENOSYS. |
| 3936 | */ |
| 3937 | if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT) |
| 3938 | futex_cmpxchg_enabled = 1; |
| 3939 | #endif |
| 3940 | } |
| 3941 | |
Benjamin Herrenschmidt | f6d107f | 2008-03-27 14:52:15 +1100 | [diff] [blame] | 3942 | static int __init futex_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | { |
Heiko Carstens | 63b1a81 | 2014-01-16 14:54:50 +0100 | [diff] [blame] | 3944 | unsigned int futex_shift; |
Davidlohr Bueso | a52b89e | 2014-01-12 15:31:23 -0800 | [diff] [blame] | 3945 | unsigned long i; |
| 3946 | |
| 3947 | #if CONFIG_BASE_SMALL |
| 3948 | futex_hashsize = 16; |
| 3949 | #else |
| 3950 | futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus()); |
| 3951 | #endif |
| 3952 | |
| 3953 | futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues), |
| 3954 | futex_hashsize, 0, |
| 3955 | futex_hashsize < 256 ? HASH_SMALL : 0, |
Heiko Carstens | 63b1a81 | 2014-01-16 14:54:50 +0100 | [diff] [blame] | 3956 | &futex_shift, NULL, |
| 3957 | futex_hashsize, futex_hashsize); |
| 3958 | futex_hashsize = 1UL << futex_shift; |
Heiko Carstens | 03b8c7b | 2014-03-02 13:09:47 +0100 | [diff] [blame] | 3959 | |
| 3960 | futex_detect_cmpxchg(); |
Thomas Gleixner | a0c1e90 | 2008-02-23 15:23:57 -0800 | [diff] [blame] | 3961 | |
Davidlohr Bueso | a52b89e | 2014-01-12 15:31:23 -0800 | [diff] [blame] | 3962 | for (i = 0; i < futex_hashsize; i++) { |
Linus Torvalds | 11d4616 | 2014-03-20 22:11:17 -0700 | [diff] [blame] | 3963 | atomic_set(&futex_queues[i].waiters, 0); |
Dima Zavin | 732375c | 2011-07-07 17:27:59 -0700 | [diff] [blame] | 3964 | plist_head_init(&futex_queues[i].chain); |
Thomas Gleixner | 3e4ab74 | 2008-02-23 15:23:55 -0800 | [diff] [blame] | 3965 | spin_lock_init(&futex_queues[i].lock); |
| 3966 | } |
| 3967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | return 0; |
| 3969 | } |
Yang Yang | 808de34 | 2016-12-30 16:17:55 +0800 | [diff] [blame] | 3970 | core_initcall(futex_init); |