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