| 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 |  * | 
 | 11 |  *  Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly | 
 | 12 |  *  enough at me, Linus for the original (flawed) idea, Matthew | 
 | 13 |  *  Kirkwood for proof-of-concept implementation. | 
 | 14 |  * | 
 | 15 |  *  "The futexes are also cursed." | 
 | 16 |  *  "But they come in a choice of three flavours!" | 
 | 17 |  * | 
 | 18 |  *  This program is free software; you can redistribute it and/or modify | 
 | 19 |  *  it under the terms of the GNU General Public License as published by | 
 | 20 |  *  the Free Software Foundation; either version 2 of the License, or | 
 | 21 |  *  (at your option) any later version. | 
 | 22 |  * | 
 | 23 |  *  This program is distributed in the hope that it will be useful, | 
 | 24 |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 25 |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 26 |  *  GNU General Public License for more details. | 
 | 27 |  * | 
 | 28 |  *  You should have received a copy of the GNU General Public License | 
 | 29 |  *  along with this program; if not, write to the Free Software | 
 | 30 |  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
 | 31 |  */ | 
 | 32 | #include <linux/slab.h> | 
 | 33 | #include <linux/poll.h> | 
 | 34 | #include <linux/fs.h> | 
 | 35 | #include <linux/file.h> | 
 | 36 | #include <linux/jhash.h> | 
 | 37 | #include <linux/init.h> | 
 | 38 | #include <linux/futex.h> | 
 | 39 | #include <linux/mount.h> | 
 | 40 | #include <linux/pagemap.h> | 
 | 41 | #include <linux/syscalls.h> | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 42 | #include <linux/signal.h> | 
| Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 43 | #include <asm/futex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 |  | 
 | 45 | #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8) | 
 | 46 |  | 
 | 47 | /* | 
 | 48 |  * Futexes are matched on equal values of this key. | 
 | 49 |  * The key type depends on whether it's a shared or private mapping. | 
 | 50 |  * Don't rearrange members without looking at hash_futex(). | 
 | 51 |  * | 
 | 52 |  * offset is aligned to a multiple of sizeof(u32) (== 4) by definition. | 
 | 53 |  * We set bit 0 to indicate if it's an inode-based key. | 
 | 54 |  */ | 
 | 55 | union futex_key { | 
 | 56 | 	struct { | 
 | 57 | 		unsigned long pgoff; | 
 | 58 | 		struct inode *inode; | 
 | 59 | 		int offset; | 
 | 60 | 	} shared; | 
 | 61 | 	struct { | 
 | 62 | 		unsigned long uaddr; | 
 | 63 | 		struct mm_struct *mm; | 
 | 64 | 		int offset; | 
 | 65 | 	} private; | 
 | 66 | 	struct { | 
 | 67 | 		unsigned long word; | 
 | 68 | 		void *ptr; | 
 | 69 | 		int offset; | 
 | 70 | 	} both; | 
 | 71 | }; | 
 | 72 |  | 
 | 73 | /* | 
 | 74 |  * We use this hashed waitqueue instead of a normal wait_queue_t, so | 
 | 75 |  * we can wake only the relevant ones (hashed queues may be shared). | 
 | 76 |  * | 
 | 77 |  * A futex_q has a woken state, just like tasks have TASK_RUNNING. | 
 | 78 |  * It is considered woken when list_empty(&q->list) || q->lock_ptr == 0. | 
 | 79 |  * The order of wakup is always to make the first condition true, then | 
 | 80 |  * wake up q->waiters, then make the second condition true. | 
 | 81 |  */ | 
 | 82 | struct futex_q { | 
 | 83 | 	struct list_head list; | 
 | 84 | 	wait_queue_head_t waiters; | 
 | 85 |  | 
 | 86 | 	/* Which hash list lock to use. */ | 
 | 87 | 	spinlock_t *lock_ptr; | 
 | 88 |  | 
 | 89 | 	/* Key which the futex is hashed on. */ | 
 | 90 | 	union futex_key key; | 
 | 91 |  | 
 | 92 | 	/* For fd, sigio sent using these. */ | 
 | 93 | 	int fd; | 
 | 94 | 	struct file *filp; | 
 | 95 | }; | 
 | 96 |  | 
 | 97 | /* | 
 | 98 |  * Split the global futex_lock into every hash list lock. | 
 | 99 |  */ | 
 | 100 | struct futex_hash_bucket { | 
 | 101 |        spinlock_t              lock; | 
 | 102 |        struct list_head       chain; | 
 | 103 | }; | 
 | 104 |  | 
 | 105 | static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS]; | 
 | 106 |  | 
 | 107 | /* Futex-fs vfsmount entry: */ | 
 | 108 | static struct vfsmount *futex_mnt; | 
 | 109 |  | 
 | 110 | /* | 
 | 111 |  * We hash on the keys returned from get_futex_key (see below). | 
 | 112 |  */ | 
 | 113 | static struct futex_hash_bucket *hash_futex(union futex_key *key) | 
 | 114 | { | 
 | 115 | 	u32 hash = jhash2((u32*)&key->both.word, | 
 | 116 | 			  (sizeof(key->both.word)+sizeof(key->both.ptr))/4, | 
 | 117 | 			  key->both.offset); | 
 | 118 | 	return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)]; | 
 | 119 | } | 
 | 120 |  | 
 | 121 | /* | 
 | 122 |  * Return 1 if two futex_keys are equal, 0 otherwise. | 
 | 123 |  */ | 
 | 124 | static inline int match_futex(union futex_key *key1, union futex_key *key2) | 
 | 125 | { | 
 | 126 | 	return (key1->both.word == key2->both.word | 
 | 127 | 		&& key1->both.ptr == key2->both.ptr | 
 | 128 | 		&& key1->both.offset == key2->both.offset); | 
 | 129 | } | 
 | 130 |  | 
 | 131 | /* | 
 | 132 |  * Get parameters which are the keys for a futex. | 
 | 133 |  * | 
 | 134 |  * For shared mappings, it's (page->index, vma->vm_file->f_dentry->d_inode, | 
 | 135 |  * offset_within_page).  For private mappings, it's (uaddr, current->mm). | 
 | 136 |  * We can usually work out the index without swapping in the page. | 
 | 137 |  * | 
 | 138 |  * Returns: 0, or negative error code. | 
 | 139 |  * The key words are stored in *key on success. | 
 | 140 |  * | 
 | 141 |  * Should be called with ¤t->mm->mmap_sem but NOT any spinlocks. | 
 | 142 |  */ | 
 | 143 | static int get_futex_key(unsigned long uaddr, union futex_key *key) | 
 | 144 | { | 
 | 145 | 	struct mm_struct *mm = current->mm; | 
 | 146 | 	struct vm_area_struct *vma; | 
 | 147 | 	struct page *page; | 
 | 148 | 	int err; | 
 | 149 |  | 
 | 150 | 	/* | 
 | 151 | 	 * The futex address must be "naturally" aligned. | 
 | 152 | 	 */ | 
 | 153 | 	key->both.offset = uaddr % PAGE_SIZE; | 
 | 154 | 	if (unlikely((key->both.offset % sizeof(u32)) != 0)) | 
 | 155 | 		return -EINVAL; | 
 | 156 | 	uaddr -= key->both.offset; | 
 | 157 |  | 
 | 158 | 	/* | 
 | 159 | 	 * The futex is hashed differently depending on whether | 
 | 160 | 	 * it's in a shared or private mapping.  So check vma first. | 
 | 161 | 	 */ | 
 | 162 | 	vma = find_extend_vma(mm, uaddr); | 
 | 163 | 	if (unlikely(!vma)) | 
 | 164 | 		return -EFAULT; | 
 | 165 |  | 
 | 166 | 	/* | 
 | 167 | 	 * Permissions. | 
 | 168 | 	 */ | 
 | 169 | 	if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ)) | 
 | 170 | 		return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES; | 
 | 171 |  | 
 | 172 | 	/* | 
 | 173 | 	 * Private mappings are handled in a simple way. | 
 | 174 | 	 * | 
 | 175 | 	 * NOTE: When userspace waits on a MAP_SHARED mapping, even if | 
 | 176 | 	 * it's a read-only handle, it's expected that futexes attach to | 
 | 177 | 	 * the object not the particular process.  Therefore we use | 
 | 178 | 	 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared | 
 | 179 | 	 * mappings of _writable_ handles. | 
 | 180 | 	 */ | 
 | 181 | 	if (likely(!(vma->vm_flags & VM_MAYSHARE))) { | 
 | 182 | 		key->private.mm = mm; | 
 | 183 | 		key->private.uaddr = uaddr; | 
 | 184 | 		return 0; | 
 | 185 | 	} | 
 | 186 |  | 
 | 187 | 	/* | 
 | 188 | 	 * Linear file mappings are also simple. | 
 | 189 | 	 */ | 
 | 190 | 	key->shared.inode = vma->vm_file->f_dentry->d_inode; | 
 | 191 | 	key->both.offset++; /* Bit 0 of offset indicates inode-based key. */ | 
 | 192 | 	if (likely(!(vma->vm_flags & VM_NONLINEAR))) { | 
 | 193 | 		key->shared.pgoff = (((uaddr - vma->vm_start) >> PAGE_SHIFT) | 
 | 194 | 				     + vma->vm_pgoff); | 
 | 195 | 		return 0; | 
 | 196 | 	} | 
 | 197 |  | 
 | 198 | 	/* | 
 | 199 | 	 * We could walk the page table to read the non-linear | 
 | 200 | 	 * pte, and get the page index without fetching the page | 
 | 201 | 	 * from swap.  But that's a lot of code to duplicate here | 
 | 202 | 	 * for a rare case, so we simply fetch the page. | 
 | 203 | 	 */ | 
 | 204 |  | 
 | 205 | 	/* | 
 | 206 | 	 * Do a quick atomic lookup first - this is the fastpath. | 
 | 207 | 	 */ | 
| Hugh Dickins | deceb6c | 2005-10-29 18:16:33 -0700 | [diff] [blame] | 208 | 	page = follow_page(mm, uaddr, FOLL_TOUCH|FOLL_GET); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | 	if (likely(page != NULL)) { | 
 | 210 | 		key->shared.pgoff = | 
 | 211 | 			page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); | 
| Hugh Dickins | deceb6c | 2005-10-29 18:16:33 -0700 | [diff] [blame] | 212 | 		put_page(page); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | 		return 0; | 
 | 214 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 |  | 
 | 216 | 	/* | 
 | 217 | 	 * Do it the general way. | 
 | 218 | 	 */ | 
 | 219 | 	err = get_user_pages(current, mm, uaddr, 1, 0, 0, &page, NULL); | 
 | 220 | 	if (err >= 0) { | 
 | 221 | 		key->shared.pgoff = | 
 | 222 | 			page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT); | 
 | 223 | 		put_page(page); | 
 | 224 | 		return 0; | 
 | 225 | 	} | 
 | 226 | 	return err; | 
 | 227 | } | 
 | 228 |  | 
 | 229 | /* | 
 | 230 |  * Take a reference to the resource addressed by a key. | 
 | 231 |  * Can be called while holding spinlocks. | 
 | 232 |  * | 
 | 233 |  * NOTE: mmap_sem MUST be held between get_futex_key() and calling this | 
 | 234 |  * function, if it is called at all.  mmap_sem keeps key->shared.inode valid. | 
 | 235 |  */ | 
 | 236 | static inline void get_key_refs(union futex_key *key) | 
 | 237 | { | 
 | 238 | 	if (key->both.ptr != 0) { | 
 | 239 | 		if (key->both.offset & 1) | 
 | 240 | 			atomic_inc(&key->shared.inode->i_count); | 
 | 241 | 		else | 
 | 242 | 			atomic_inc(&key->private.mm->mm_count); | 
 | 243 | 	} | 
 | 244 | } | 
 | 245 |  | 
 | 246 | /* | 
 | 247 |  * Drop a reference to the resource addressed by a key. | 
 | 248 |  * The hash bucket spinlock must not be held. | 
 | 249 |  */ | 
 | 250 | static void drop_key_refs(union futex_key *key) | 
 | 251 | { | 
 | 252 | 	if (key->both.ptr != 0) { | 
 | 253 | 		if (key->both.offset & 1) | 
 | 254 | 			iput(key->shared.inode); | 
 | 255 | 		else | 
 | 256 | 			mmdrop(key->private.mm); | 
 | 257 | 	} | 
 | 258 | } | 
 | 259 |  | 
 | 260 | static inline int get_futex_value_locked(int *dest, int __user *from) | 
 | 261 | { | 
 | 262 | 	int ret; | 
 | 263 |  | 
 | 264 | 	inc_preempt_count(); | 
 | 265 | 	ret = __copy_from_user_inatomic(dest, from, sizeof(int)); | 
 | 266 | 	dec_preempt_count(); | 
 | 267 |  | 
 | 268 | 	return ret ? -EFAULT : 0; | 
 | 269 | } | 
 | 270 |  | 
 | 271 | /* | 
 | 272 |  * The hash bucket lock must be held when this is called. | 
 | 273 |  * Afterwards, the futex_q must not be accessed. | 
 | 274 |  */ | 
 | 275 | static void wake_futex(struct futex_q *q) | 
 | 276 | { | 
 | 277 | 	list_del_init(&q->list); | 
 | 278 | 	if (q->filp) | 
 | 279 | 		send_sigio(&q->filp->f_owner, q->fd, POLL_IN); | 
 | 280 | 	/* | 
 | 281 | 	 * The lock in wake_up_all() is a crucial memory barrier after the | 
 | 282 | 	 * list_del_init() and also before assigning to q->lock_ptr. | 
 | 283 | 	 */ | 
 | 284 | 	wake_up_all(&q->waiters); | 
 | 285 | 	/* | 
 | 286 | 	 * The waiting task can free the futex_q as soon as this is written, | 
 | 287 | 	 * without taking any locks.  This must come last. | 
 | 288 | 	 */ | 
 | 289 | 	q->lock_ptr = NULL; | 
 | 290 | } | 
 | 291 |  | 
 | 292 | /* | 
 | 293 |  * Wake up all waiters hashed on the physical page that is mapped | 
 | 294 |  * to this virtual address: | 
 | 295 |  */ | 
 | 296 | static int futex_wake(unsigned long uaddr, int nr_wake) | 
 | 297 | { | 
 | 298 | 	union futex_key key; | 
 | 299 | 	struct futex_hash_bucket *bh; | 
 | 300 | 	struct list_head *head; | 
 | 301 | 	struct futex_q *this, *next; | 
 | 302 | 	int ret; | 
 | 303 |  | 
 | 304 | 	down_read(¤t->mm->mmap_sem); | 
 | 305 |  | 
 | 306 | 	ret = get_futex_key(uaddr, &key); | 
 | 307 | 	if (unlikely(ret != 0)) | 
 | 308 | 		goto out; | 
 | 309 |  | 
 | 310 | 	bh = hash_futex(&key); | 
 | 311 | 	spin_lock(&bh->lock); | 
 | 312 | 	head = &bh->chain; | 
 | 313 |  | 
 | 314 | 	list_for_each_entry_safe(this, next, head, list) { | 
 | 315 | 		if (match_futex (&this->key, &key)) { | 
 | 316 | 			wake_futex(this); | 
 | 317 | 			if (++ret >= nr_wake) | 
 | 318 | 				break; | 
 | 319 | 		} | 
 | 320 | 	} | 
 | 321 |  | 
 | 322 | 	spin_unlock(&bh->lock); | 
 | 323 | out: | 
 | 324 | 	up_read(¤t->mm->mmap_sem); | 
 | 325 | 	return ret; | 
 | 326 | } | 
 | 327 |  | 
 | 328 | /* | 
| Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 329 |  * Wake up all waiters hashed on the physical page that is mapped | 
 | 330 |  * to this virtual address: | 
 | 331 |  */ | 
 | 332 | static int futex_wake_op(unsigned long uaddr1, unsigned long uaddr2, int nr_wake, int nr_wake2, int op) | 
 | 333 | { | 
 | 334 | 	union futex_key key1, key2; | 
 | 335 | 	struct futex_hash_bucket *bh1, *bh2; | 
 | 336 | 	struct list_head *head; | 
 | 337 | 	struct futex_q *this, *next; | 
 | 338 | 	int ret, op_ret, attempt = 0; | 
 | 339 |  | 
 | 340 | retryfull: | 
 | 341 | 	down_read(¤t->mm->mmap_sem); | 
 | 342 |  | 
 | 343 | 	ret = get_futex_key(uaddr1, &key1); | 
 | 344 | 	if (unlikely(ret != 0)) | 
 | 345 | 		goto out; | 
 | 346 | 	ret = get_futex_key(uaddr2, &key2); | 
 | 347 | 	if (unlikely(ret != 0)) | 
 | 348 | 		goto out; | 
 | 349 |  | 
 | 350 | 	bh1 = hash_futex(&key1); | 
 | 351 | 	bh2 = hash_futex(&key2); | 
 | 352 |  | 
 | 353 | retry: | 
 | 354 | 	if (bh1 < bh2) | 
 | 355 | 		spin_lock(&bh1->lock); | 
 | 356 | 	spin_lock(&bh2->lock); | 
 | 357 | 	if (bh1 > bh2) | 
 | 358 | 		spin_lock(&bh1->lock); | 
 | 359 |  | 
 | 360 | 	op_ret = futex_atomic_op_inuser(op, (int __user *)uaddr2); | 
 | 361 | 	if (unlikely(op_ret < 0)) { | 
 | 362 | 		int dummy; | 
 | 363 |  | 
 | 364 | 		spin_unlock(&bh1->lock); | 
 | 365 | 		if (bh1 != bh2) | 
 | 366 | 			spin_unlock(&bh2->lock); | 
 | 367 |  | 
| David Gibson | 796f8d9 | 2005-11-07 00:59:33 -0800 | [diff] [blame] | 368 | 		if (unlikely(op_ret != -EFAULT)) { | 
 | 369 | 			ret = op_ret; | 
 | 370 | 			goto out; | 
 | 371 | 		} | 
 | 372 |  | 
| Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 373 | 		/* futex_atomic_op_inuser needs to both read and write | 
 | 374 | 		 * *(int __user *)uaddr2, but we can't modify it | 
 | 375 | 		 * non-atomically.  Therefore, if get_user below is not | 
 | 376 | 		 * enough, we need to handle the fault ourselves, while | 
 | 377 | 		 * still holding the mmap_sem.  */ | 
 | 378 | 		if (attempt++) { | 
 | 379 | 			struct vm_area_struct * vma; | 
 | 380 | 			struct mm_struct *mm = current->mm; | 
 | 381 |  | 
 | 382 | 			ret = -EFAULT; | 
 | 383 | 			if (attempt >= 2 || | 
 | 384 | 			    !(vma = find_vma(mm, uaddr2)) || | 
 | 385 | 			    vma->vm_start > uaddr2 || | 
 | 386 | 			    !(vma->vm_flags & VM_WRITE)) | 
 | 387 | 				goto out; | 
 | 388 |  | 
 | 389 | 			switch (handle_mm_fault(mm, vma, uaddr2, 1)) { | 
 | 390 | 			case VM_FAULT_MINOR: | 
 | 391 | 				current->min_flt++; | 
 | 392 | 				break; | 
 | 393 | 			case VM_FAULT_MAJOR: | 
 | 394 | 				current->maj_flt++; | 
 | 395 | 				break; | 
 | 396 | 			default: | 
 | 397 | 				goto out; | 
 | 398 | 			} | 
 | 399 | 			goto retry; | 
 | 400 | 		} | 
 | 401 |  | 
 | 402 | 		/* If we would have faulted, release mmap_sem, | 
 | 403 | 		 * fault it in and start all over again.  */ | 
 | 404 | 		up_read(¤t->mm->mmap_sem); | 
 | 405 |  | 
 | 406 | 		ret = get_user(dummy, (int __user *)uaddr2); | 
 | 407 | 		if (ret) | 
 | 408 | 			return ret; | 
 | 409 |  | 
 | 410 | 		goto retryfull; | 
 | 411 | 	} | 
 | 412 |  | 
 | 413 | 	head = &bh1->chain; | 
 | 414 |  | 
 | 415 | 	list_for_each_entry_safe(this, next, head, list) { | 
 | 416 | 		if (match_futex (&this->key, &key1)) { | 
 | 417 | 			wake_futex(this); | 
 | 418 | 			if (++ret >= nr_wake) | 
 | 419 | 				break; | 
 | 420 | 		} | 
 | 421 | 	} | 
 | 422 |  | 
 | 423 | 	if (op_ret > 0) { | 
 | 424 | 		head = &bh2->chain; | 
 | 425 |  | 
 | 426 | 		op_ret = 0; | 
 | 427 | 		list_for_each_entry_safe(this, next, head, list) { | 
 | 428 | 			if (match_futex (&this->key, &key2)) { | 
 | 429 | 				wake_futex(this); | 
 | 430 | 				if (++op_ret >= nr_wake2) | 
 | 431 | 					break; | 
 | 432 | 			} | 
 | 433 | 		} | 
 | 434 | 		ret += op_ret; | 
 | 435 | 	} | 
 | 436 |  | 
 | 437 | 	spin_unlock(&bh1->lock); | 
 | 438 | 	if (bh1 != bh2) | 
 | 439 | 		spin_unlock(&bh2->lock); | 
 | 440 | out: | 
 | 441 | 	up_read(¤t->mm->mmap_sem); | 
 | 442 | 	return ret; | 
 | 443 | } | 
 | 444 |  | 
 | 445 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 |  * Requeue all waiters hashed on one physical page to another | 
 | 447 |  * physical page. | 
 | 448 |  */ | 
 | 449 | static int futex_requeue(unsigned long uaddr1, unsigned long uaddr2, | 
 | 450 | 			 int nr_wake, int nr_requeue, int *valp) | 
 | 451 | { | 
 | 452 | 	union futex_key key1, key2; | 
 | 453 | 	struct futex_hash_bucket *bh1, *bh2; | 
 | 454 | 	struct list_head *head1; | 
 | 455 | 	struct futex_q *this, *next; | 
 | 456 | 	int ret, drop_count = 0; | 
 | 457 |  | 
 | 458 |  retry: | 
 | 459 | 	down_read(¤t->mm->mmap_sem); | 
 | 460 |  | 
 | 461 | 	ret = get_futex_key(uaddr1, &key1); | 
 | 462 | 	if (unlikely(ret != 0)) | 
 | 463 | 		goto out; | 
 | 464 | 	ret = get_futex_key(uaddr2, &key2); | 
 | 465 | 	if (unlikely(ret != 0)) | 
 | 466 | 		goto out; | 
 | 467 |  | 
 | 468 | 	bh1 = hash_futex(&key1); | 
 | 469 | 	bh2 = hash_futex(&key2); | 
 | 470 |  | 
 | 471 | 	if (bh1 < bh2) | 
 | 472 | 		spin_lock(&bh1->lock); | 
 | 473 | 	spin_lock(&bh2->lock); | 
 | 474 | 	if (bh1 > bh2) | 
 | 475 | 		spin_lock(&bh1->lock); | 
 | 476 |  | 
 | 477 | 	if (likely(valp != NULL)) { | 
 | 478 | 		int curval; | 
 | 479 |  | 
 | 480 | 		ret = get_futex_value_locked(&curval, (int __user *)uaddr1); | 
 | 481 |  | 
 | 482 | 		if (unlikely(ret)) { | 
 | 483 | 			spin_unlock(&bh1->lock); | 
 | 484 | 			if (bh1 != bh2) | 
 | 485 | 				spin_unlock(&bh2->lock); | 
 | 486 |  | 
 | 487 | 			/* If we would have faulted, release mmap_sem, fault | 
 | 488 | 			 * it in and start all over again. | 
 | 489 | 			 */ | 
 | 490 | 			up_read(¤t->mm->mmap_sem); | 
 | 491 |  | 
 | 492 | 			ret = get_user(curval, (int __user *)uaddr1); | 
 | 493 |  | 
 | 494 | 			if (!ret) | 
 | 495 | 				goto retry; | 
 | 496 |  | 
 | 497 | 			return ret; | 
 | 498 | 		} | 
 | 499 | 		if (curval != *valp) { | 
 | 500 | 			ret = -EAGAIN; | 
 | 501 | 			goto out_unlock; | 
 | 502 | 		} | 
 | 503 | 	} | 
 | 504 |  | 
 | 505 | 	head1 = &bh1->chain; | 
 | 506 | 	list_for_each_entry_safe(this, next, head1, list) { | 
 | 507 | 		if (!match_futex (&this->key, &key1)) | 
 | 508 | 			continue; | 
 | 509 | 		if (++ret <= nr_wake) { | 
 | 510 | 			wake_futex(this); | 
 | 511 | 		} else { | 
 | 512 | 			list_move_tail(&this->list, &bh2->chain); | 
 | 513 | 			this->lock_ptr = &bh2->lock; | 
 | 514 | 			this->key = key2; | 
 | 515 | 			get_key_refs(&key2); | 
 | 516 | 			drop_count++; | 
 | 517 |  | 
 | 518 | 			if (ret - nr_wake >= nr_requeue) | 
 | 519 | 				break; | 
 | 520 | 			/* Make sure to stop if key1 == key2 */ | 
 | 521 | 			if (head1 == &bh2->chain && head1 != &next->list) | 
 | 522 | 				head1 = &this->list; | 
 | 523 | 		} | 
 | 524 | 	} | 
 | 525 |  | 
 | 526 | out_unlock: | 
 | 527 | 	spin_unlock(&bh1->lock); | 
 | 528 | 	if (bh1 != bh2) | 
 | 529 | 		spin_unlock(&bh2->lock); | 
 | 530 |  | 
 | 531 | 	/* drop_key_refs() must be called outside the spinlocks. */ | 
 | 532 | 	while (--drop_count >= 0) | 
 | 533 | 		drop_key_refs(&key1); | 
 | 534 |  | 
 | 535 | out: | 
 | 536 | 	up_read(¤t->mm->mmap_sem); | 
 | 537 | 	return ret; | 
 | 538 | } | 
 | 539 |  | 
 | 540 | /* The key must be already stored in q->key. */ | 
 | 541 | static inline struct futex_hash_bucket * | 
 | 542 | queue_lock(struct futex_q *q, int fd, struct file *filp) | 
 | 543 | { | 
 | 544 | 	struct futex_hash_bucket *bh; | 
 | 545 |  | 
 | 546 | 	q->fd = fd; | 
 | 547 | 	q->filp = filp; | 
 | 548 |  | 
 | 549 | 	init_waitqueue_head(&q->waiters); | 
 | 550 |  | 
 | 551 | 	get_key_refs(&q->key); | 
 | 552 | 	bh = hash_futex(&q->key); | 
 | 553 | 	q->lock_ptr = &bh->lock; | 
 | 554 |  | 
 | 555 | 	spin_lock(&bh->lock); | 
 | 556 | 	return bh; | 
 | 557 | } | 
 | 558 |  | 
 | 559 | static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *bh) | 
 | 560 | { | 
 | 561 | 	list_add_tail(&q->list, &bh->chain); | 
 | 562 | 	spin_unlock(&bh->lock); | 
 | 563 | } | 
 | 564 |  | 
 | 565 | static inline void | 
 | 566 | queue_unlock(struct futex_q *q, struct futex_hash_bucket *bh) | 
 | 567 | { | 
 | 568 | 	spin_unlock(&bh->lock); | 
 | 569 | 	drop_key_refs(&q->key); | 
 | 570 | } | 
 | 571 |  | 
 | 572 | /* | 
 | 573 |  * queue_me and unqueue_me must be called as a pair, each | 
 | 574 |  * exactly once.  They are called with the hashed spinlock held. | 
 | 575 |  */ | 
 | 576 |  | 
 | 577 | /* The key must be already stored in q->key. */ | 
 | 578 | static void queue_me(struct futex_q *q, int fd, struct file *filp) | 
 | 579 | { | 
 | 580 | 	struct futex_hash_bucket *bh; | 
 | 581 | 	bh = queue_lock(q, fd, filp); | 
 | 582 | 	__queue_me(q, bh); | 
 | 583 | } | 
 | 584 |  | 
 | 585 | /* Return 1 if we were still queued (ie. 0 means we were woken) */ | 
 | 586 | static int unqueue_me(struct futex_q *q) | 
 | 587 | { | 
 | 588 | 	int ret = 0; | 
 | 589 | 	spinlock_t *lock_ptr; | 
 | 590 |  | 
 | 591 | 	/* In the common case we don't take the spinlock, which is nice. */ | 
 | 592 |  retry: | 
 | 593 | 	lock_ptr = q->lock_ptr; | 
 | 594 | 	if (lock_ptr != 0) { | 
 | 595 | 		spin_lock(lock_ptr); | 
 | 596 | 		/* | 
 | 597 | 		 * q->lock_ptr can change between reading it and | 
 | 598 | 		 * spin_lock(), causing us to take the wrong lock.  This | 
 | 599 | 		 * corrects the race condition. | 
 | 600 | 		 * | 
 | 601 | 		 * Reasoning goes like this: if we have the wrong lock, | 
 | 602 | 		 * q->lock_ptr must have changed (maybe several times) | 
 | 603 | 		 * between reading it and the spin_lock().  It can | 
 | 604 | 		 * change again after the spin_lock() but only if it was | 
 | 605 | 		 * already changed before the spin_lock().  It cannot, | 
 | 606 | 		 * however, change back to the original value.  Therefore | 
 | 607 | 		 * we can detect whether we acquired the correct lock. | 
 | 608 | 		 */ | 
 | 609 | 		if (unlikely(lock_ptr != q->lock_ptr)) { | 
 | 610 | 			spin_unlock(lock_ptr); | 
 | 611 | 			goto retry; | 
 | 612 | 		} | 
 | 613 | 		WARN_ON(list_empty(&q->list)); | 
 | 614 | 		list_del(&q->list); | 
 | 615 | 		spin_unlock(lock_ptr); | 
 | 616 | 		ret = 1; | 
 | 617 | 	} | 
 | 618 |  | 
 | 619 | 	drop_key_refs(&q->key); | 
 | 620 | 	return ret; | 
 | 621 | } | 
 | 622 |  | 
 | 623 | static int futex_wait(unsigned long uaddr, int val, unsigned long time) | 
 | 624 | { | 
 | 625 | 	DECLARE_WAITQUEUE(wait, current); | 
 | 626 | 	int ret, curval; | 
 | 627 | 	struct futex_q q; | 
 | 628 | 	struct futex_hash_bucket *bh; | 
 | 629 |  | 
 | 630 |  retry: | 
 | 631 | 	down_read(¤t->mm->mmap_sem); | 
 | 632 |  | 
 | 633 | 	ret = get_futex_key(uaddr, &q.key); | 
 | 634 | 	if (unlikely(ret != 0)) | 
 | 635 | 		goto out_release_sem; | 
 | 636 |  | 
 | 637 | 	bh = queue_lock(&q, -1, NULL); | 
 | 638 |  | 
 | 639 | 	/* | 
 | 640 | 	 * Access the page AFTER the futex is queued. | 
 | 641 | 	 * Order is important: | 
 | 642 | 	 * | 
 | 643 | 	 *   Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val); | 
 | 644 | 	 *   Userspace waker:  if (cond(var)) { var = new; futex_wake(&var); } | 
 | 645 | 	 * | 
 | 646 | 	 * The basic logical guarantee of a futex is that it blocks ONLY | 
 | 647 | 	 * if cond(var) is known to be true at the time of blocking, for | 
 | 648 | 	 * any cond.  If we queued after testing *uaddr, that would open | 
 | 649 | 	 * a race condition where we could block indefinitely with | 
 | 650 | 	 * cond(var) false, which would violate the guarantee. | 
 | 651 | 	 * | 
 | 652 | 	 * A consequence is that futex_wait() can return zero and absorb | 
 | 653 | 	 * a wakeup when *uaddr != val on entry to the syscall.  This is | 
 | 654 | 	 * rare, but normal. | 
 | 655 | 	 * | 
 | 656 | 	 * We hold the mmap semaphore, so the mapping cannot have changed | 
 | 657 | 	 * since we looked it up in get_futex_key. | 
 | 658 | 	 */ | 
 | 659 |  | 
 | 660 | 	ret = get_futex_value_locked(&curval, (int __user *)uaddr); | 
 | 661 |  | 
 | 662 | 	if (unlikely(ret)) { | 
 | 663 | 		queue_unlock(&q, bh); | 
 | 664 |  | 
 | 665 | 		/* If we would have faulted, release mmap_sem, fault it in and | 
 | 666 | 		 * start all over again. | 
 | 667 | 		 */ | 
 | 668 | 		up_read(¤t->mm->mmap_sem); | 
 | 669 |  | 
 | 670 | 		ret = get_user(curval, (int __user *)uaddr); | 
 | 671 |  | 
 | 672 | 		if (!ret) | 
 | 673 | 			goto retry; | 
 | 674 | 		return ret; | 
 | 675 | 	} | 
 | 676 | 	if (curval != val) { | 
 | 677 | 		ret = -EWOULDBLOCK; | 
 | 678 | 		queue_unlock(&q, bh); | 
 | 679 | 		goto out_release_sem; | 
 | 680 | 	} | 
 | 681 |  | 
 | 682 | 	/* Only actually queue if *uaddr contained val.  */ | 
 | 683 | 	__queue_me(&q, bh); | 
 | 684 |  | 
 | 685 | 	/* | 
 | 686 | 	 * Now the futex is queued and we have checked the data, we | 
 | 687 | 	 * don't want to hold mmap_sem while we sleep. | 
 | 688 | 	 */	 | 
 | 689 | 	up_read(¤t->mm->mmap_sem); | 
 | 690 |  | 
 | 691 | 	/* | 
 | 692 | 	 * There might have been scheduling since the queue_me(), as we | 
 | 693 | 	 * cannot hold a spinlock across the get_user() in case it | 
 | 694 | 	 * faults, and we cannot just set TASK_INTERRUPTIBLE state when | 
 | 695 | 	 * queueing ourselves into the futex hash.  This code thus has to | 
 | 696 | 	 * rely on the futex_wake() code removing us from hash when it | 
 | 697 | 	 * wakes us up. | 
 | 698 | 	 */ | 
 | 699 |  | 
 | 700 | 	/* add_wait_queue is the barrier after __set_current_state. */ | 
 | 701 | 	__set_current_state(TASK_INTERRUPTIBLE); | 
 | 702 | 	add_wait_queue(&q.waiters, &wait); | 
 | 703 | 	/* | 
 | 704 | 	 * !list_empty() is safe here without any lock. | 
 | 705 | 	 * q.lock_ptr != 0 is not safe, because of ordering against wakeup. | 
 | 706 | 	 */ | 
 | 707 | 	if (likely(!list_empty(&q.list))) | 
 | 708 | 		time = schedule_timeout(time); | 
 | 709 | 	__set_current_state(TASK_RUNNING); | 
 | 710 |  | 
 | 711 | 	/* | 
 | 712 | 	 * NOTE: we don't remove ourselves from the waitqueue because | 
 | 713 | 	 * we are the only user of it. | 
 | 714 | 	 */ | 
 | 715 |  | 
 | 716 | 	/* If we were woken (and unqueued), we succeeded, whatever. */ | 
 | 717 | 	if (!unqueue_me(&q)) | 
 | 718 | 		return 0; | 
 | 719 | 	if (time == 0) | 
 | 720 | 		return -ETIMEDOUT; | 
 | 721 | 	/* We expect signal_pending(current), but another thread may | 
 | 722 | 	 * have handled it for us already. */ | 
 | 723 | 	return -EINTR; | 
 | 724 |  | 
 | 725 |  out_release_sem: | 
 | 726 | 	up_read(¤t->mm->mmap_sem); | 
 | 727 | 	return ret; | 
 | 728 | } | 
 | 729 |  | 
 | 730 | static int futex_close(struct inode *inode, struct file *filp) | 
 | 731 | { | 
 | 732 | 	struct futex_q *q = filp->private_data; | 
 | 733 |  | 
 | 734 | 	unqueue_me(q); | 
 | 735 | 	kfree(q); | 
 | 736 | 	return 0; | 
 | 737 | } | 
 | 738 |  | 
 | 739 | /* This is one-shot: once it's gone off you need a new fd */ | 
 | 740 | static unsigned int futex_poll(struct file *filp, | 
 | 741 | 			       struct poll_table_struct *wait) | 
 | 742 | { | 
 | 743 | 	struct futex_q *q = filp->private_data; | 
 | 744 | 	int ret = 0; | 
 | 745 |  | 
 | 746 | 	poll_wait(filp, &q->waiters, wait); | 
 | 747 |  | 
 | 748 | 	/* | 
 | 749 | 	 * list_empty() is safe here without any lock. | 
 | 750 | 	 * q->lock_ptr != 0 is not safe, because of ordering against wakeup. | 
 | 751 | 	 */ | 
 | 752 | 	if (list_empty(&q->list)) | 
 | 753 | 		ret = POLLIN | POLLRDNORM; | 
 | 754 |  | 
 | 755 | 	return ret; | 
 | 756 | } | 
 | 757 |  | 
 | 758 | static struct file_operations futex_fops = { | 
 | 759 | 	.release	= futex_close, | 
 | 760 | 	.poll		= futex_poll, | 
 | 761 | }; | 
 | 762 |  | 
 | 763 | /* | 
 | 764 |  * Signal allows caller to avoid the race which would occur if they | 
 | 765 |  * set the sigio stuff up afterwards. | 
 | 766 |  */ | 
 | 767 | static int futex_fd(unsigned long uaddr, int signal) | 
 | 768 | { | 
 | 769 | 	struct futex_q *q; | 
 | 770 | 	struct file *filp; | 
 | 771 | 	int ret, err; | 
 | 772 |  | 
 | 773 | 	ret = -EINVAL; | 
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 774 | 	if (!valid_signal(signal)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | 		goto out; | 
 | 776 |  | 
 | 777 | 	ret = get_unused_fd(); | 
 | 778 | 	if (ret < 0) | 
 | 779 | 		goto out; | 
 | 780 | 	filp = get_empty_filp(); | 
 | 781 | 	if (!filp) { | 
 | 782 | 		put_unused_fd(ret); | 
 | 783 | 		ret = -ENFILE; | 
 | 784 | 		goto out; | 
 | 785 | 	} | 
 | 786 | 	filp->f_op = &futex_fops; | 
 | 787 | 	filp->f_vfsmnt = mntget(futex_mnt); | 
 | 788 | 	filp->f_dentry = dget(futex_mnt->mnt_root); | 
 | 789 | 	filp->f_mapping = filp->f_dentry->d_inode->i_mapping; | 
 | 790 |  | 
 | 791 | 	if (signal) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | 		err = f_setown(filp, current->pid, 1); | 
 | 793 | 		if (err < 0) { | 
| Pekka Enberg | 39ed3fd | 2005-09-06 15:17:44 -0700 | [diff] [blame] | 794 | 			goto error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | 		} | 
 | 796 | 		filp->f_owner.signum = signal; | 
 | 797 | 	} | 
 | 798 |  | 
 | 799 | 	q = kmalloc(sizeof(*q), GFP_KERNEL); | 
 | 800 | 	if (!q) { | 
| Pekka Enberg | 39ed3fd | 2005-09-06 15:17:44 -0700 | [diff] [blame] | 801 | 		err = -ENOMEM; | 
 | 802 | 		goto error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | 	} | 
 | 804 |  | 
 | 805 | 	down_read(¤t->mm->mmap_sem); | 
 | 806 | 	err = get_futex_key(uaddr, &q->key); | 
 | 807 |  | 
 | 808 | 	if (unlikely(err != 0)) { | 
 | 809 | 		up_read(¤t->mm->mmap_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | 		kfree(q); | 
| Pekka Enberg | 39ed3fd | 2005-09-06 15:17:44 -0700 | [diff] [blame] | 811 | 		goto error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | 	} | 
 | 813 |  | 
 | 814 | 	/* | 
 | 815 | 	 * queue_me() must be called before releasing mmap_sem, because | 
 | 816 | 	 * key->shared.inode needs to be referenced while holding it. | 
 | 817 | 	 */ | 
 | 818 | 	filp->private_data = q; | 
 | 819 |  | 
 | 820 | 	queue_me(q, ret, filp); | 
 | 821 | 	up_read(¤t->mm->mmap_sem); | 
 | 822 |  | 
 | 823 | 	/* Now we map fd to filp, so userspace can access it */ | 
 | 824 | 	fd_install(ret, filp); | 
 | 825 | out: | 
 | 826 | 	return ret; | 
| Pekka Enberg | 39ed3fd | 2005-09-06 15:17:44 -0700 | [diff] [blame] | 827 | error: | 
 | 828 | 	put_unused_fd(ret); | 
 | 829 | 	put_filp(filp); | 
 | 830 | 	ret = err; | 
 | 831 | 	goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | } | 
 | 833 |  | 
 | 834 | long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout, | 
 | 835 | 		unsigned long uaddr2, int val2, int val3) | 
 | 836 | { | 
 | 837 | 	int ret; | 
 | 838 |  | 
 | 839 | 	switch (op) { | 
 | 840 | 	case FUTEX_WAIT: | 
 | 841 | 		ret = futex_wait(uaddr, val, timeout); | 
 | 842 | 		break; | 
 | 843 | 	case FUTEX_WAKE: | 
 | 844 | 		ret = futex_wake(uaddr, val); | 
 | 845 | 		break; | 
 | 846 | 	case FUTEX_FD: | 
 | 847 | 		/* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */ | 
 | 848 | 		ret = futex_fd(uaddr, val); | 
 | 849 | 		break; | 
 | 850 | 	case FUTEX_REQUEUE: | 
 | 851 | 		ret = futex_requeue(uaddr, uaddr2, val, val2, NULL); | 
 | 852 | 		break; | 
 | 853 | 	case FUTEX_CMP_REQUEUE: | 
 | 854 | 		ret = futex_requeue(uaddr, uaddr2, val, val2, &val3); | 
 | 855 | 		break; | 
| Jakub Jelinek | 4732efb | 2005-09-06 15:16:25 -0700 | [diff] [blame] | 856 | 	case FUTEX_WAKE_OP: | 
 | 857 | 		ret = futex_wake_op(uaddr, uaddr2, val, val2, val3); | 
 | 858 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | 	default: | 
 | 860 | 		ret = -ENOSYS; | 
 | 861 | 	} | 
 | 862 | 	return ret; | 
 | 863 | } | 
 | 864 |  | 
 | 865 |  | 
 | 866 | asmlinkage long sys_futex(u32 __user *uaddr, int op, int val, | 
 | 867 | 			  struct timespec __user *utime, u32 __user *uaddr2, | 
 | 868 | 			  int val3) | 
 | 869 | { | 
 | 870 | 	struct timespec t; | 
 | 871 | 	unsigned long timeout = MAX_SCHEDULE_TIMEOUT; | 
 | 872 | 	int val2 = 0; | 
 | 873 |  | 
 | 874 | 	if ((op == FUTEX_WAIT) && utime) { | 
 | 875 | 		if (copy_from_user(&t, utime, sizeof(t)) != 0) | 
 | 876 | 			return -EFAULT; | 
 | 877 | 		timeout = timespec_to_jiffies(&t) + 1; | 
 | 878 | 	} | 
 | 879 | 	/* | 
 | 880 | 	 * requeue parameter in 'utime' if op == FUTEX_REQUEUE. | 
 | 881 | 	 */ | 
 | 882 | 	if (op >= FUTEX_REQUEUE) | 
 | 883 | 		val2 = (int) (unsigned long) utime; | 
 | 884 |  | 
 | 885 | 	return do_futex((unsigned long)uaddr, op, val, timeout, | 
 | 886 | 			(unsigned long)uaddr2, val2, val3); | 
 | 887 | } | 
 | 888 |  | 
 | 889 | static struct super_block * | 
 | 890 | futexfs_get_sb(struct file_system_type *fs_type, | 
 | 891 | 	       int flags, const char *dev_name, void *data) | 
 | 892 | { | 
 | 893 | 	return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA); | 
 | 894 | } | 
 | 895 |  | 
 | 896 | static struct file_system_type futex_fs_type = { | 
 | 897 | 	.name		= "futexfs", | 
 | 898 | 	.get_sb		= futexfs_get_sb, | 
 | 899 | 	.kill_sb	= kill_anon_super, | 
 | 900 | }; | 
 | 901 |  | 
 | 902 | static int __init init(void) | 
 | 903 | { | 
 | 904 | 	unsigned int i; | 
 | 905 |  | 
 | 906 | 	register_filesystem(&futex_fs_type); | 
 | 907 | 	futex_mnt = kern_mount(&futex_fs_type); | 
 | 908 |  | 
 | 909 | 	for (i = 0; i < ARRAY_SIZE(futex_queues); i++) { | 
 | 910 | 		INIT_LIST_HEAD(&futex_queues[i].chain); | 
 | 911 | 		spin_lock_init(&futex_queues[i].lock); | 
 | 912 | 	} | 
 | 913 | 	return 0; | 
 | 914 | } | 
 | 915 | __initcall(init); |