futex.c 58.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10
/*
 *  Fast Userspace Mutexes (which I call "Futexes!").
 *  (C) Rusty Russell, IBM 2002
 *
 *  Generalized futexes, futex requeueing, misc fixes by Ingo Molnar
 *  (C) Copyright 2003 Red Hat Inc, All Rights Reserved
 *
 *  Removed page pinning, fix privately mapped COW pages and other cleanups
 *  (C) Copyright 2003, 2004 Jamie Lokier
 *
11 12 13 14
 *  Robust futex support started by Ingo Molnar
 *  (C) Copyright 2006 Red Hat Inc, All Rights Reserved
 *  Thanks to Thomas Gleixner for suggestions, analysis and fixes.
 *
15 16 17 18
 *  PI-futex support started by Ingo Molnar and Thomas Gleixner
 *  Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
 *  Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
 *
E
Eric Dumazet 已提交
19 20 21
 *  PRIVATE futexes by Eric Dumazet
 *  Copyright (C) 2007 Eric Dumazet <dada1@cosmosbay.com>
 *
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *  Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
 *  enough at me, Linus for the original (flawed) idea, Matthew
 *  Kirkwood for proof-of-concept implementation.
 *
 *  "The futexes are also cursed."
 *  "But they come in a choice of three flavours!"
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/jhash.h>
#include <linux/init.h>
#include <linux/futex.h>
#include <linux/mount.h>
#include <linux/pagemap.h>
#include <linux/syscalls.h>
53
#include <linux/signal.h>
54
#include <linux/module.h>
55
#include <asm/futex.h>
L
Linus Torvalds 已提交
56

57 58
#include "rtmutex_common.h"

P
Pierre Peiffer 已提交
59 60 61 62 63 64
#ifdef CONFIG_DEBUG_RT_MUTEXES
# include "rtmutex-debug.h"
#else
# include "rtmutex.h"
#endif

L
Linus Torvalds 已提交
65 66
#define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/*
 * Priority Inheritance state:
 */
struct futex_pi_state {
	/*
	 * list of 'owned' pi_state instances - these have to be
	 * cleaned up in do_exit() if the task exits prematurely:
	 */
	struct list_head list;

	/*
	 * The PI object:
	 */
	struct rt_mutex pi_mutex;

	struct task_struct *owner;
	atomic_t refcount;

	union futex_key key;
};

L
Linus Torvalds 已提交
88 89 90 91 92
/*
 * We use this hashed waitqueue instead of a normal wait_queue_t, so
 * we can wake only the relevant ones (hashed queues may be shared).
 *
 * A futex_q has a woken state, just like tasks have TASK_RUNNING.
P
Pierre Peiffer 已提交
93
 * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
L
Linus Torvalds 已提交
94 95 96 97
 * The order of wakup is always to make the first condition true, then
 * wake up q->waiters, then make the second condition true.
 */
struct futex_q {
P
Pierre Peiffer 已提交
98
	struct plist_node list;
L
Linus Torvalds 已提交
99 100
	wait_queue_head_t waiters;

101
	/* Which hash list lock to use: */
L
Linus Torvalds 已提交
102 103
	spinlock_t *lock_ptr;

104
	/* Key which the futex is hashed on: */
L
Linus Torvalds 已提交
105 106
	union futex_key key;

107
	/* For fd, sigio sent using these: */
L
Linus Torvalds 已提交
108 109
	int fd;
	struct file *filp;
110 111 112 113

	/* Optional priority inheritance state: */
	struct futex_pi_state *pi_state;
	struct task_struct *task;
P
Pierre Peiffer 已提交
114 115 116 117 118 119

	/*
	 * This waiter is used in case of requeue from a
	 * normal futex to a PI-futex
	 */
	struct rt_mutex_waiter waiter;
L
Linus Torvalds 已提交
120 121 122 123 124 125
};

/*
 * Split the global futex_lock into every hash list lock.
 */
struct futex_hash_bucket {
P
Pierre Peiffer 已提交
126 127
	spinlock_t lock;
	struct plist_head chain;
L
Linus Torvalds 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
};

static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];

/* Futex-fs vfsmount entry: */
static struct vfsmount *futex_mnt;

/*
 * We hash on the keys returned from get_futex_key (see below).
 */
static struct futex_hash_bucket *hash_futex(union futex_key *key)
{
	u32 hash = jhash2((u32*)&key->both.word,
			  (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
			  key->both.offset);
	return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
}

/*
 * Return 1 if two futex_keys are equal, 0 otherwise.
 */
static inline int match_futex(union futex_key *key1, union futex_key *key2)
{
	return (key1->both.word == key2->both.word
		&& key1->both.ptr == key2->both.ptr
		&& key1->both.offset == key2->both.offset);
}

E
Eric Dumazet 已提交
156 157 158 159 160 161 162 163 164
/**
 * get_futex_key - Get parameters which are the keys for a futex.
 * @uaddr: virtual address of the futex
 * @shared: NULL for a PROCESS_PRIVATE futex,
 *	&current->mm->mmap_sem for a PROCESS_SHARED futex
 * @key: address where result is stored.
 *
 * Returns a negative error code or 0
 * The key words are stored in *key on success.
L
Linus Torvalds 已提交
165
 *
166
 * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
L
Linus Torvalds 已提交
167 168 169
 * offset_within_page).  For private mappings, it's (uaddr, current->mm).
 * We can usually work out the index without swapping in the page.
 *
E
Eric Dumazet 已提交
170 171 172
 * fshared is NULL for PROCESS_PRIVATE futexes
 * For other futexes, it points to &current->mm->mmap_sem and
 * caller must have taken the reader lock. but NOT any spinlocks.
L
Linus Torvalds 已提交
173
 */
E
Eric Dumazet 已提交
174 175
int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
		  union futex_key *key)
L
Linus Torvalds 已提交
176
{
177
	unsigned long address = (unsigned long)uaddr;
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185
	struct mm_struct *mm = current->mm;
	struct vm_area_struct *vma;
	struct page *page;
	int err;

	/*
	 * The futex address must be "naturally" aligned.
	 */
186
	key->both.offset = address % PAGE_SIZE;
E
Eric Dumazet 已提交
187
	if (unlikely((address % sizeof(u32)) != 0))
L
Linus Torvalds 已提交
188
		return -EINVAL;
189
	address -= key->both.offset;
L
Linus Torvalds 已提交
190

E
Eric Dumazet 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204
	/*
	 * PROCESS_PRIVATE futexes are fast.
	 * As the mm cannot disappear under us and the 'key' only needs
	 * virtual address, we dont even have to find the underlying vma.
	 * Note : We do have to check 'uaddr' is a valid user address,
	 *        but access_ok() should be faster than find_vma()
	 */
	if (!fshared) {
		if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
			return -EFAULT;
		key->private.mm = mm;
		key->private.address = address;
		return 0;
	}
L
Linus Torvalds 已提交
205 206 207 208
	/*
	 * The futex is hashed differently depending on whether
	 * it's in a shared or private mapping.  So check vma first.
	 */
209
	vma = find_extend_vma(mm, address);
L
Linus Torvalds 已提交
210 211 212 213 214 215 216 217 218
	if (unlikely(!vma))
		return -EFAULT;

	/*
	 * Permissions.
	 */
	if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
		return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;

P
Pierre Peiffer 已提交
219 220 221
	/* Save the user address in the ley */
	key->uaddr = uaddr;

L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230 231
	/*
	 * Private mappings are handled in a simple way.
	 *
	 * NOTE: When userspace waits on a MAP_SHARED mapping, even if
	 * it's a read-only handle, it's expected that futexes attach to
	 * the object not the particular process.  Therefore we use
	 * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
	 * mappings of _writable_ handles.
	 */
	if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
E
Eric Dumazet 已提交
232
		key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */
L
Linus Torvalds 已提交
233
		key->private.mm = mm;
234
		key->private.address = address;
L
Linus Torvalds 已提交
235 236 237 238 239 240
		return 0;
	}

	/*
	 * Linear file mappings are also simple.
	 */
241
	key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
E
Eric Dumazet 已提交
242
	key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
L
Linus Torvalds 已提交
243
	if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
244
		key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254
				     + vma->vm_pgoff);
		return 0;
	}

	/*
	 * We could walk the page table to read the non-linear
	 * pte, and get the page index without fetching the page
	 * from swap.  But that's a lot of code to duplicate here
	 * for a rare case, so we simply fetch the page.
	 */
255
	err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263
	if (err >= 0) {
		key->shared.pgoff =
			page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
		put_page(page);
		return 0;
	}
	return err;
}
264
EXPORT_SYMBOL_GPL(get_futex_key);
L
Linus Torvalds 已提交
265 266 267 268 269 270

/*
 * Take a reference to the resource addressed by a key.
 * Can be called while holding spinlocks.
 *
 */
271
inline void get_futex_key_refs(union futex_key *key)
L
Linus Torvalds 已提交
272
{
E
Eric Dumazet 已提交
273 274 275 276
	if (key->both.ptr == 0)
		return;
	switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
		case FUT_OFF_INODE:
L
Linus Torvalds 已提交
277
			atomic_inc(&key->shared.inode->i_count);
E
Eric Dumazet 已提交
278 279
			break;
		case FUT_OFF_MMSHARED:
L
Linus Torvalds 已提交
280
			atomic_inc(&key->private.mm->mm_count);
E
Eric Dumazet 已提交
281
			break;
L
Linus Torvalds 已提交
282 283
	}
}
284
EXPORT_SYMBOL_GPL(get_futex_key_refs);
L
Linus Torvalds 已提交
285 286 287 288 289

/*
 * Drop a reference to the resource addressed by a key.
 * The hash bucket spinlock must not be held.
 */
290
void drop_futex_key_refs(union futex_key *key)
L
Linus Torvalds 已提交
291
{
E
Eric Dumazet 已提交
292 293 294 295
	if (key->both.ptr == 0)
		return;
	switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
		case FUT_OFF_INODE:
L
Linus Torvalds 已提交
296
			iput(key->shared.inode);
E
Eric Dumazet 已提交
297 298
			break;
		case FUT_OFF_MMSHARED:
L
Linus Torvalds 已提交
299
			mmdrop(key->private.mm);
E
Eric Dumazet 已提交
300
			break;
L
Linus Torvalds 已提交
301 302
	}
}
303
EXPORT_SYMBOL_GPL(drop_futex_key_refs);
L
Linus Torvalds 已提交
304

305
static inline int get_futex_value_locked(u32 *dest, u32 __user *from)
L
Linus Torvalds 已提交
306 307 308
{
	int ret;

309
	pagefault_disable();
310
	ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
311
	pagefault_enable();
L
Linus Torvalds 已提交
312 313 314 315

	return ret ? -EFAULT : 0;
}

316
/*
E
Eric Dumazet 已提交
317 318
 * Fault handling.
 * if fshared is non NULL, current->mm->mmap_sem is already held
319
 */
E
Eric Dumazet 已提交
320 321
static int futex_handle_fault(unsigned long address,
			      struct rw_semaphore *fshared, int attempt)
322 323 324
{
	struct vm_area_struct * vma;
	struct mm_struct *mm = current->mm;
E
Eric Dumazet 已提交
325
	int ret = -EFAULT;
326

E
Eric Dumazet 已提交
327 328
	if (attempt > 2)
		return ret;
329

E
Eric Dumazet 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	if (!fshared)
		down_read(&mm->mmap_sem);
	vma = find_vma(mm, address);
	if (vma && address >= vma->vm_start &&
	    (vma->vm_flags & VM_WRITE)) {
		switch (handle_mm_fault(mm, vma, address, 1)) {
		case VM_FAULT_MINOR:
			ret = 0;
			current->min_flt++;
			break;
		case VM_FAULT_MAJOR:
			ret = 0;
			current->maj_flt++;
			break;
		}
345
	}
E
Eric Dumazet 已提交
346 347 348
	if (!fshared)
		up_read(&mm->mmap_sem);
	return ret;
349 350 351 352 353 354 355 356 357 358 359 360
}

/*
 * PI code:
 */
static int refill_pi_state_cache(void)
{
	struct futex_pi_state *pi_state;

	if (likely(current->pi_state_cache))
		return 0;

361
	pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

	if (!pi_state)
		return -ENOMEM;

	INIT_LIST_HEAD(&pi_state->list);
	/* pi_mutex gets initialized later */
	pi_state->owner = NULL;
	atomic_set(&pi_state->refcount, 1);

	current->pi_state_cache = pi_state;

	return 0;
}

static struct futex_pi_state * alloc_pi_state(void)
{
	struct futex_pi_state *pi_state = current->pi_state_cache;

	WARN_ON(!pi_state);
	current->pi_state_cache = NULL;

	return pi_state;
}

static void free_pi_state(struct futex_pi_state *pi_state)
{
	if (!atomic_dec_and_test(&pi_state->refcount))
		return;

	/*
	 * If pi_state->owner is NULL, the owner is most probably dying
	 * and has cleaned up the pi_state already
	 */
	if (pi_state->owner) {
		spin_lock_irq(&pi_state->owner->pi_lock);
		list_del_init(&pi_state->list);
		spin_unlock_irq(&pi_state->owner->pi_lock);

		rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
	}

	if (current->pi_state_cache)
		kfree(pi_state);
	else {
		/*
		 * pi_state->list is already empty.
		 * clear pi_state->owner.
		 * refcount is at 0 - put it back to 1.
		 */
		pi_state->owner = NULL;
		atomic_set(&pi_state->refcount, 1);
		current->pi_state_cache = pi_state;
	}
}

/*
 * Look up the task based on what TID userspace gave us.
 * We dont trust it.
 */
static struct task_struct * futex_find_get_task(pid_t pid)
{
	struct task_struct *p;

425
	rcu_read_lock();
426 427 428 429 430 431 432 433 434
	p = find_task_by_pid(pid);
	if (!p)
		goto out_unlock;
	if ((current->euid != p->euid) && (current->euid != p->uid)) {
		p = NULL;
		goto out_unlock;
	}
	get_task_struct(p);
out_unlock:
435
	rcu_read_unlock();
436 437 438 439 440 441 442 443 444 445 446 447 448

	return p;
}

/*
 * This task is holding PI mutexes at exit time => bad.
 * Kernel cleans up PI-state, but userspace is likely hosed.
 * (Robust-futex cleanup is separate and might save the day for userspace.)
 */
void exit_pi_state_list(struct task_struct *curr)
{
	struct list_head *next, *head = &curr->pi_state_list;
	struct futex_pi_state *pi_state;
449
	struct futex_hash_bucket *hb;
450 451 452 453 454
	union futex_key key;

	/*
	 * We are a ZOMBIE and nobody can enqueue itself on
	 * pi_state_list anymore, but we have to be careful
455
	 * versus waiters unqueueing themselves:
456 457 458 459 460 461 462
	 */
	spin_lock_irq(&curr->pi_lock);
	while (!list_empty(head)) {

		next = head->next;
		pi_state = list_entry(next, struct futex_pi_state, list);
		key = pi_state->key;
463
		hb = hash_futex(&key);
464 465 466 467 468
		spin_unlock_irq(&curr->pi_lock);

		spin_lock(&hb->lock);

		spin_lock_irq(&curr->pi_lock);
469 470 471 472
		/*
		 * We dropped the pi-lock, so re-check whether this
		 * task still owns the PI-state:
		 */
473 474 475 476 477 478
		if (head->next != next) {
			spin_unlock(&hb->lock);
			continue;
		}

		WARN_ON(pi_state->owner != curr);
479 480
		WARN_ON(list_empty(&pi_state->list));
		list_del_init(&pi_state->list);
481 482 483 484 485 486 487 488 489 490 491 492 493
		pi_state->owner = NULL;
		spin_unlock_irq(&curr->pi_lock);

		rt_mutex_unlock(&pi_state->pi_mutex);

		spin_unlock(&hb->lock);

		spin_lock_irq(&curr->pi_lock);
	}
	spin_unlock_irq(&curr->pi_lock);
}

static int
P
Pierre Peiffer 已提交
494 495
lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
		union futex_key *key, struct futex_pi_state **ps)
496 497 498
{
	struct futex_pi_state *pi_state = NULL;
	struct futex_q *this, *next;
P
Pierre Peiffer 已提交
499
	struct plist_head *head;
500
	struct task_struct *p;
501
	pid_t pid = uval & FUTEX_TID_MASK;
502 503 504

	head = &hb->chain;

P
Pierre Peiffer 已提交
505
	plist_for_each_entry_safe(this, next, head, list) {
P
Pierre Peiffer 已提交
506
		if (match_futex(&this->key, key)) {
507 508 509 510 511
			/*
			 * Another waiter already exists - bump up
			 * the refcount and return its pi_state:
			 */
			pi_state = this->pi_state;
512 513 514 515 516 517
			/*
			 * Userspace might have messed up non PI and PI futexes
			 */
			if (unlikely(!pi_state))
				return -EINVAL;

518
			WARN_ON(!atomic_read(&pi_state->refcount));
519 520
			WARN_ON(pid && pi_state->owner &&
				pi_state->owner->pid != pid);
521

522
			atomic_inc(&pi_state->refcount);
P
Pierre Peiffer 已提交
523
			*ps = pi_state;
524 525 526 527 528 529

			return 0;
		}
	}

	/*
530
	 * We are the first waiter - try to look up the real owner and attach
531
	 * the new pi_state to it, but bail out when TID = 0
532
	 */
533
	if (!pid)
534
		return -ESRCH;
535
	p = futex_find_get_task(pid);
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	if (IS_ERR(p))
		return PTR_ERR(p);

	/*
	 * We need to look at the task state flags to figure out,
	 * whether the task is exiting. To protect against the do_exit
	 * change of the task flags, we do this protected by
	 * p->pi_lock:
	 */
	spin_lock_irq(&p->pi_lock);
	if (unlikely(p->flags & PF_EXITING)) {
		/*
		 * The task is on the way out. When PF_EXITPIDONE is
		 * set, we know that the task has finished the
		 * cleanup:
		 */
		int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;

		spin_unlock_irq(&p->pi_lock);
		put_task_struct(p);
		return ret;
	}
558 559 560 561 562 563 564 565 566 567

	pi_state = alloc_pi_state();

	/*
	 * Initialize the pi_mutex in locked state and make 'p'
	 * the owner of it:
	 */
	rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);

	/* Store the key for possible exit cleanups: */
P
Pierre Peiffer 已提交
568
	pi_state->key = *key;
569

570
	WARN_ON(!list_empty(&pi_state->list));
571 572 573 574 575 576
	list_add(&pi_state->list, &p->pi_state_list);
	pi_state->owner = p;
	spin_unlock_irq(&p->pi_lock);

	put_task_struct(p);

P
Pierre Peiffer 已提交
577
	*ps = pi_state;
578 579 580 581

	return 0;
}

L
Linus Torvalds 已提交
582 583 584 585 586 587
/*
 * The hash bucket lock must be held when this is called.
 * Afterwards, the futex_q must not be accessed.
 */
static void wake_futex(struct futex_q *q)
{
P
Pierre Peiffer 已提交
588
	plist_del(&q->list, &q->list.plist);
L
Linus Torvalds 已提交
589 590 591 592
	if (q->filp)
		send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
	/*
	 * The lock in wake_up_all() is a crucial memory barrier after the
P
Pierre Peiffer 已提交
593
	 * plist_del() and also before assigning to q->lock_ptr.
L
Linus Torvalds 已提交
594 595 596 597 598
	 */
	wake_up_all(&q->waiters);
	/*
	 * The waiting task can free the futex_q as soon as this is written,
	 * without taking any locks.  This must come last.
599 600 601 602 603
	 *
	 * A memory barrier is required here to prevent the following store
	 * to lock_ptr from getting ahead of the wakeup. Clearing the lock
	 * at the end of wake_up_all() does not prevent this store from
	 * moving.
L
Linus Torvalds 已提交
604
	 */
605
	smp_wmb();
L
Linus Torvalds 已提交
606 607 608
	q->lock_ptr = NULL;
}

609 610 611 612 613 614 615 616 617
static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
{
	struct task_struct *new_owner;
	struct futex_pi_state *pi_state = this->pi_state;
	u32 curval, newval;

	if (!pi_state)
		return -EINVAL;

618
	spin_lock(&pi_state->pi_mutex.wait_lock);
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);

	/*
	 * This happens when we have stolen the lock and the original
	 * pending owner did not enqueue itself back on the rt_mutex.
	 * Thats not a tragedy. We know that way, that a lock waiter
	 * is on the fly. We make the futex_q waiter the pending owner.
	 */
	if (!new_owner)
		new_owner = this->task;

	/*
	 * We pass it to the next owner. (The WAITERS bit is always
	 * kept enabled while there is PI state around. We must also
	 * preserve the owner died bit.)
	 */
635
	if (!(uval & FUTEX_OWNER_DIED)) {
636 637
		int ret = 0;

638
		newval = FUTEX_WAITERS | new_owner->pid;
P
Pierre Peiffer 已提交
639 640
		/* Keep the FUTEX_WAITER_REQUEUED flag if it was set */
		newval |= (uval & FUTEX_WAITER_REQUEUED);
641

642
		pagefault_disable();
643
		curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
644
		pagefault_enable();
645

646
		if (curval == -EFAULT)
647
			ret = -EFAULT;
648
		if (curval != uval)
649 650 651 652 653
			ret = -EINVAL;
		if (ret) {
			spin_unlock(&pi_state->pi_mutex.wait_lock);
			return ret;
		}
654
	}
655

656 657 658 659 660 661 662
	spin_lock_irq(&pi_state->owner->pi_lock);
	WARN_ON(list_empty(&pi_state->list));
	list_del_init(&pi_state->list);
	spin_unlock_irq(&pi_state->owner->pi_lock);

	spin_lock_irq(&new_owner->pi_lock);
	WARN_ON(!list_empty(&pi_state->list));
663 664
	list_add(&pi_state->list, &new_owner->pi_state_list);
	pi_state->owner = new_owner;
665 666
	spin_unlock_irq(&new_owner->pi_lock);

667
	spin_unlock(&pi_state->pi_mutex.wait_lock);
668 669 670 671 672 673 674 675 676 677 678 679 680
	rt_mutex_unlock(&pi_state->pi_mutex);

	return 0;
}

static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
{
	u32 oldval;

	/*
	 * There is no waiter, so we unlock the futex. The owner died
	 * bit has not to be preserved here. We are the owner:
	 */
681
	pagefault_disable();
682
	oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
683
	pagefault_enable();
684 685 686 687 688 689 690 691 692

	if (oldval == -EFAULT)
		return oldval;
	if (oldval != uval)
		return -EAGAIN;

	return 0;
}

I
Ingo Molnar 已提交
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
/*
 * Express the locking dependencies for lockdep:
 */
static inline void
double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
{
	if (hb1 <= hb2) {
		spin_lock(&hb1->lock);
		if (hb1 < hb2)
			spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
	} else { /* hb1 > hb2 */
		spin_lock(&hb2->lock);
		spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
	}
}

L
Linus Torvalds 已提交
709 710 711 712
/*
 * Wake up all waiters hashed on the physical page that is mapped
 * to this virtual address:
 */
E
Eric Dumazet 已提交
713 714
static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
		      int nr_wake)
L
Linus Torvalds 已提交
715
{
716
	struct futex_hash_bucket *hb;
L
Linus Torvalds 已提交
717
	struct futex_q *this, *next;
P
Pierre Peiffer 已提交
718
	struct plist_head *head;
719
	union futex_key key;
L
Linus Torvalds 已提交
720 721
	int ret;

E
Eric Dumazet 已提交
722 723
	if (fshared)
		down_read(fshared);
L
Linus Torvalds 已提交
724

E
Eric Dumazet 已提交
725
	ret = get_futex_key(uaddr, fshared, &key);
L
Linus Torvalds 已提交
726 727 728
	if (unlikely(ret != 0))
		goto out;

729 730 731
	hb = hash_futex(&key);
	spin_lock(&hb->lock);
	head = &hb->chain;
L
Linus Torvalds 已提交
732

P
Pierre Peiffer 已提交
733
	plist_for_each_entry_safe(this, next, head, list) {
L
Linus Torvalds 已提交
734
		if (match_futex (&this->key, &key)) {
735 736 737 738
			if (this->pi_state) {
				ret = -EINVAL;
				break;
			}
L
Linus Torvalds 已提交
739 740 741 742 743 744
			wake_futex(this);
			if (++ret >= nr_wake)
				break;
		}
	}

745
	spin_unlock(&hb->lock);
L
Linus Torvalds 已提交
746
out:
E
Eric Dumazet 已提交
747 748
	if (fshared)
		up_read(fshared);
L
Linus Torvalds 已提交
749 750 751
	return ret;
}

P
Pierre Peiffer 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
/*
 * Called from futex_requeue_pi.
 * Set FUTEX_WAITERS and FUTEX_WAITER_REQUEUED flags on the
 * PI-futex value; search its associated pi_state if an owner exist
 * or create a new one without owner.
 */
static inline int
lookup_pi_state_for_requeue(u32 __user *uaddr, struct futex_hash_bucket *hb,
			    union futex_key *key,
			    struct futex_pi_state **pi_state)
{
	u32 curval, uval, newval;

retry:
	/*
	 * We can't handle a fault cleanly because we can't
	 * release the locks here. Simply return the fault.
	 */
	if (get_futex_value_locked(&curval, uaddr))
		return -EFAULT;

	/* set the flags FUTEX_WAITERS and FUTEX_WAITER_REQUEUED */
	if ((curval & (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED))
	    != (FUTEX_WAITERS | FUTEX_WAITER_REQUEUED)) {
		/*
		 * No waiters yet, we prepare the futex to have some waiters.
		 */

		uval = curval;
		newval = uval | FUTEX_WAITERS | FUTEX_WAITER_REQUEUED;

		pagefault_disable();
		curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
		pagefault_enable();

		if (unlikely(curval == -EFAULT))
			return -EFAULT;
		if (unlikely(curval != uval))
			goto retry;
	}

	if (!(curval & FUTEX_TID_MASK)
	    || lookup_pi_state(curval, hb, key, pi_state)) {
		/* the futex has no owner (yet) or the lookup failed:
		   allocate one pi_state without owner */

		*pi_state = alloc_pi_state();

		/* Already stores the key: */
		(*pi_state)->key = *key;

		/* init the mutex without owner */
		__rt_mutex_init(&(*pi_state)->pi_mutex, NULL);
	}

	return 0;
}

/*
 * Keep the first nr_wake waiter from futex1, wake up one,
 * and requeue the next nr_requeue waiters following hashed on
 * one physical page to another physical page (PI-futex uaddr2)
 */
E
Eric Dumazet 已提交
815 816 817
static int futex_requeue_pi(u32 __user *uaddr1,
			    struct rw_semaphore *fshared,
			    u32 __user *uaddr2,
P
Pierre Peiffer 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
			    int nr_wake, int nr_requeue, u32 *cmpval)
{
	union futex_key key1, key2;
	struct futex_hash_bucket *hb1, *hb2;
	struct plist_head *head1;
	struct futex_q *this, *next;
	struct futex_pi_state *pi_state2 = NULL;
	struct rt_mutex_waiter *waiter, *top_waiter = NULL;
	struct rt_mutex *lock2 = NULL;
	int ret, drop_count = 0;

	if (refill_pi_state_cache())
		return -ENOMEM;

retry:
	/*
	 * First take all the futex related locks:
	 */
E
Eric Dumazet 已提交
836 837
	if (fshared)
		down_read(fshared);
P
Pierre Peiffer 已提交
838

E
Eric Dumazet 已提交
839
	ret = get_futex_key(uaddr1, fshared, &key1);
P
Pierre Peiffer 已提交
840 841
	if (unlikely(ret != 0))
		goto out;
E
Eric Dumazet 已提交
842
	ret = get_futex_key(uaddr2, fshared, &key2);
P
Pierre Peiffer 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
	if (unlikely(ret != 0))
		goto out;

	hb1 = hash_futex(&key1);
	hb2 = hash_futex(&key2);

	double_lock_hb(hb1, hb2);

	if (likely(cmpval != NULL)) {
		u32 curval;

		ret = get_futex_value_locked(&curval, uaddr1);

		if (unlikely(ret)) {
			spin_unlock(&hb1->lock);
			if (hb1 != hb2)
				spin_unlock(&hb2->lock);

			/*
			 * If we would have faulted, release mmap_sem, fault
			 * it in and start all over again.
			 */
E
Eric Dumazet 已提交
865 866
			if (fshared)
				up_read(fshared);
P
Pierre Peiffer 已提交
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

			ret = get_user(curval, uaddr1);

			if (!ret)
				goto retry;

			return ret;
		}
		if (curval != *cmpval) {
			ret = -EAGAIN;
			goto out_unlock;
		}
	}

	head1 = &hb1->chain;
	plist_for_each_entry_safe(this, next, head1, list) {
		if (!match_futex (&this->key, &key1))
			continue;
		if (++ret <= nr_wake) {
			wake_futex(this);
		} else {
			/*
			 * FIRST: get and set the pi_state
			 */
			if (!pi_state2) {
				int s;
				/* do this only the first time we requeue someone */
				s = lookup_pi_state_for_requeue(uaddr2, hb2,
								&key2, &pi_state2);
				if (s) {
					ret = s;
					goto out_unlock;
				}

				lock2 = &pi_state2->pi_mutex;
				spin_lock(&lock2->wait_lock);

				/* Save the top waiter of the wait_list */
				if (rt_mutex_has_waiters(lock2))
					top_waiter = rt_mutex_top_waiter(lock2);
			} else
				atomic_inc(&pi_state2->refcount);


			this->pi_state = pi_state2;

			/*
			 * SECOND: requeue futex_q to the correct hashbucket
			 */

			/*
			 * If key1 and key2 hash to the same bucket, no need to
			 * requeue.
			 */
			if (likely(head1 != &hb2->chain)) {
				plist_del(&this->list, &hb1->chain);
				plist_add(&this->list, &hb2->chain);
				this->lock_ptr = &hb2->lock;
#ifdef CONFIG_DEBUG_PI_LIST
				this->list.plist.lock = &hb2->lock;
#endif
			}
			this->key = key2;
			get_futex_key_refs(&key2);
			drop_count++;


			/*
			 * THIRD: queue it to lock2
			 */
			spin_lock_irq(&this->task->pi_lock);
			waiter = &this->waiter;
			waiter->task = this->task;
			waiter->lock = lock2;
			plist_node_init(&waiter->list_entry, this->task->prio);
			plist_node_init(&waiter->pi_list_entry, this->task->prio);
			plist_add(&waiter->list_entry, &lock2->wait_list);
			this->task->pi_blocked_on = waiter;
			spin_unlock_irq(&this->task->pi_lock);

			if (ret - nr_wake >= nr_requeue)
				break;
		}
	}

	/* If we've requeued some tasks and the top_waiter of the rt_mutex
	   has changed, we must adjust the priority of the owner, if any */
	if (drop_count) {
		struct task_struct *owner = rt_mutex_owner(lock2);
		if (owner &&
		    (top_waiter != (waiter = rt_mutex_top_waiter(lock2)))) {
			int chain_walk = 0;

			spin_lock_irq(&owner->pi_lock);
			if (top_waiter)
				plist_del(&top_waiter->pi_list_entry, &owner->pi_waiters);
			else
				/*
				 * There was no waiters before the requeue,
				 * the flag must be updated
				 */
				mark_rt_mutex_waiters(lock2);

			plist_add(&waiter->pi_list_entry, &owner->pi_waiters);
			__rt_mutex_adjust_prio(owner);
			if (owner->pi_blocked_on) {
				chain_walk = 1;
				get_task_struct(owner);
			}

			spin_unlock_irq(&owner->pi_lock);
			spin_unlock(&lock2->wait_lock);

			if (chain_walk)
				rt_mutex_adjust_prio_chain(owner, 0, lock2, NULL,
							   current);
		} else {
			/* No owner or the top_waiter does not change */
			mark_rt_mutex_waiters(lock2);
			spin_unlock(&lock2->wait_lock);
		}
	}

out_unlock:
	spin_unlock(&hb1->lock);
	if (hb1 != hb2)
		spin_unlock(&hb2->lock);

	/* drop_futex_key_refs() must be called outside the spinlocks. */
	while (--drop_count >= 0)
		drop_futex_key_refs(&key1);

out:
E
Eric Dumazet 已提交
1000 1001
	if (fshared)
		up_read(fshared);
P
Pierre Peiffer 已提交
1002 1003 1004
	return ret;
}

1005 1006 1007 1008
/*
 * Wake up all waiters hashed on the physical page that is mapped
 * to this virtual address:
 */
1009
static int
E
Eric Dumazet 已提交
1010 1011
futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
	      u32 __user *uaddr2,
1012
	      int nr_wake, int nr_wake2, int op)
1013 1014
{
	union futex_key key1, key2;
1015
	struct futex_hash_bucket *hb1, *hb2;
P
Pierre Peiffer 已提交
1016
	struct plist_head *head;
1017 1018 1019 1020
	struct futex_q *this, *next;
	int ret, op_ret, attempt = 0;

retryfull:
E
Eric Dumazet 已提交
1021 1022
	if (fshared)
		down_read(fshared);
1023

E
Eric Dumazet 已提交
1024
	ret = get_futex_key(uaddr1, fshared, &key1);
1025 1026
	if (unlikely(ret != 0))
		goto out;
E
Eric Dumazet 已提交
1027
	ret = get_futex_key(uaddr2, fshared, &key2);
1028 1029 1030
	if (unlikely(ret != 0))
		goto out;

1031 1032
	hb1 = hash_futex(&key1);
	hb2 = hash_futex(&key2);
1033 1034

retry:
I
Ingo Molnar 已提交
1035
	double_lock_hb(hb1, hb2);
1036

1037
	op_ret = futex_atomic_op_inuser(op, uaddr2);
1038
	if (unlikely(op_ret < 0)) {
1039
		u32 dummy;
1040

1041 1042 1043
		spin_unlock(&hb1->lock);
		if (hb1 != hb2)
			spin_unlock(&hb2->lock);
1044

1045
#ifndef CONFIG_MMU
1046 1047 1048 1049
		/*
		 * we don't get EFAULT from MMU faults if we don't have an MMU,
		 * but we might get them from range checking
		 */
1050 1051 1052 1053
		ret = op_ret;
		goto out;
#endif

1054 1055 1056 1057 1058
		if (unlikely(op_ret != -EFAULT)) {
			ret = op_ret;
			goto out;
		}

1059 1060
		/*
		 * futex_atomic_op_inuser needs to both read and write
1061 1062 1063
		 * *(int __user *)uaddr2, but we can't modify it
		 * non-atomically.  Therefore, if get_user below is not
		 * enough, we need to handle the fault ourselves, while
1064 1065
		 * still holding the mmap_sem.
		 */
1066
		if (attempt++) {
E
Eric Dumazet 已提交
1067 1068 1069
			ret = futex_handle_fault((unsigned long)uaddr2,
						fshared, attempt);
			if (ret)
1070 1071 1072 1073
				goto out;
			goto retry;
		}

1074 1075 1076 1077
		/*
		 * If we would have faulted, release mmap_sem,
		 * fault it in and start all over again.
		 */
E
Eric Dumazet 已提交
1078 1079
		if (fshared)
			up_read(fshared);
1080

1081
		ret = get_user(dummy, uaddr2);
1082 1083 1084 1085 1086 1087
		if (ret)
			return ret;

		goto retryfull;
	}

1088
	head = &hb1->chain;
1089

P
Pierre Peiffer 已提交
1090
	plist_for_each_entry_safe(this, next, head, list) {
1091 1092 1093 1094 1095 1096 1097 1098
		if (match_futex (&this->key, &key1)) {
			wake_futex(this);
			if (++ret >= nr_wake)
				break;
		}
	}

	if (op_ret > 0) {
1099
		head = &hb2->chain;
1100 1101

		op_ret = 0;
P
Pierre Peiffer 已提交
1102
		plist_for_each_entry_safe(this, next, head, list) {
1103 1104 1105 1106 1107 1108 1109 1110 1111
			if (match_futex (&this->key, &key2)) {
				wake_futex(this);
				if (++op_ret >= nr_wake2)
					break;
			}
		}
		ret += op_ret;
	}

1112 1113 1114
	spin_unlock(&hb1->lock);
	if (hb1 != hb2)
		spin_unlock(&hb2->lock);
1115
out:
E
Eric Dumazet 已提交
1116 1117
	if (fshared)
		up_read(fshared);
1118 1119 1120
	return ret;
}

L
Linus Torvalds 已提交
1121 1122 1123 1124
/*
 * Requeue all waiters hashed on one physical page to another
 * physical page.
 */
E
Eric Dumazet 已提交
1125 1126
static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
			 u32 __user *uaddr2,
1127
			 int nr_wake, int nr_requeue, u32 *cmpval)
L
Linus Torvalds 已提交
1128 1129
{
	union futex_key key1, key2;
1130
	struct futex_hash_bucket *hb1, *hb2;
P
Pierre Peiffer 已提交
1131
	struct plist_head *head1;
L
Linus Torvalds 已提交
1132 1133 1134 1135
	struct futex_q *this, *next;
	int ret, drop_count = 0;

 retry:
E
Eric Dumazet 已提交
1136 1137
	if (fshared)
		down_read(fshared);
L
Linus Torvalds 已提交
1138

E
Eric Dumazet 已提交
1139
	ret = get_futex_key(uaddr1, fshared, &key1);
L
Linus Torvalds 已提交
1140 1141
	if (unlikely(ret != 0))
		goto out;
E
Eric Dumazet 已提交
1142
	ret = get_futex_key(uaddr2, fshared, &key2);
L
Linus Torvalds 已提交
1143 1144 1145
	if (unlikely(ret != 0))
		goto out;

1146 1147
	hb1 = hash_futex(&key1);
	hb2 = hash_futex(&key2);
L
Linus Torvalds 已提交
1148

I
Ingo Molnar 已提交
1149
	double_lock_hb(hb1, hb2);
L
Linus Torvalds 已提交
1150

1151 1152
	if (likely(cmpval != NULL)) {
		u32 curval;
L
Linus Torvalds 已提交
1153

1154
		ret = get_futex_value_locked(&curval, uaddr1);
L
Linus Torvalds 已提交
1155 1156

		if (unlikely(ret)) {
1157 1158 1159
			spin_unlock(&hb1->lock);
			if (hb1 != hb2)
				spin_unlock(&hb2->lock);
L
Linus Torvalds 已提交
1160

1161 1162
			/*
			 * If we would have faulted, release mmap_sem, fault
L
Linus Torvalds 已提交
1163 1164
			 * it in and start all over again.
			 */
E
Eric Dumazet 已提交
1165 1166
			if (fshared)
				up_read(fshared);
L
Linus Torvalds 已提交
1167

1168
			ret = get_user(curval, uaddr1);
L
Linus Torvalds 已提交
1169 1170 1171 1172 1173 1174

			if (!ret)
				goto retry;

			return ret;
		}
1175
		if (curval != *cmpval) {
L
Linus Torvalds 已提交
1176 1177 1178 1179 1180
			ret = -EAGAIN;
			goto out_unlock;
		}
	}

1181
	head1 = &hb1->chain;
P
Pierre Peiffer 已提交
1182
	plist_for_each_entry_safe(this, next, head1, list) {
L
Linus Torvalds 已提交
1183 1184 1185 1186 1187
		if (!match_futex (&this->key, &key1))
			continue;
		if (++ret <= nr_wake) {
			wake_futex(this);
		} else {
1188 1189 1190 1191 1192
			/*
			 * If key1 and key2 hash to the same bucket, no need to
			 * requeue.
			 */
			if (likely(head1 != &hb2->chain)) {
P
Pierre Peiffer 已提交
1193 1194
				plist_del(&this->list, &hb1->chain);
				plist_add(&this->list, &hb2->chain);
1195
				this->lock_ptr = &hb2->lock;
P
Pierre Peiffer 已提交
1196 1197 1198
#ifdef CONFIG_DEBUG_PI_LIST
				this->list.plist.lock = &hb2->lock;
#endif
1199
			}
L
Linus Torvalds 已提交
1200
			this->key = key2;
1201
			get_futex_key_refs(&key2);
L
Linus Torvalds 已提交
1202 1203 1204 1205 1206 1207 1208 1209
			drop_count++;

			if (ret - nr_wake >= nr_requeue)
				break;
		}
	}

out_unlock:
1210 1211 1212
	spin_unlock(&hb1->lock);
	if (hb1 != hb2)
		spin_unlock(&hb2->lock);
L
Linus Torvalds 已提交
1213

1214
	/* drop_futex_key_refs() must be called outside the spinlocks. */
L
Linus Torvalds 已提交
1215
	while (--drop_count >= 0)
1216
		drop_futex_key_refs(&key1);
L
Linus Torvalds 已提交
1217 1218

out:
E
Eric Dumazet 已提交
1219 1220
	if (fshared)
		up_read(fshared);
L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226 1227
	return ret;
}

/* The key must be already stored in q->key. */
static inline struct futex_hash_bucket *
queue_lock(struct futex_q *q, int fd, struct file *filp)
{
1228
	struct futex_hash_bucket *hb;
L
Linus Torvalds 已提交
1229 1230 1231 1232 1233 1234

	q->fd = fd;
	q->filp = filp;

	init_waitqueue_head(&q->waiters);

1235
	get_futex_key_refs(&q->key);
1236 1237
	hb = hash_futex(&q->key);
	q->lock_ptr = &hb->lock;
L
Linus Torvalds 已提交
1238

1239 1240
	spin_lock(&hb->lock);
	return hb;
L
Linus Torvalds 已提交
1241 1242
}

1243
static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
L
Linus Torvalds 已提交
1244
{
P
Pierre Peiffer 已提交
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
	int prio;

	/*
	 * The priority used to register this element is
	 * - either the real thread-priority for the real-time threads
	 * (i.e. threads with a priority lower than MAX_RT_PRIO)
	 * - or MAX_RT_PRIO for non-RT threads.
	 * Thus, all RT-threads are woken first in priority order, and
	 * the others are woken last, in FIFO order.
	 */
	prio = min(current->normal_prio, MAX_RT_PRIO);

	plist_node_init(&q->list, prio);
#ifdef CONFIG_DEBUG_PI_LIST
	q->list.plist.lock = &hb->lock;
#endif
	plist_add(&q->list, &hb->chain);
1262
	q->task = current;
1263
	spin_unlock(&hb->lock);
L
Linus Torvalds 已提交
1264 1265 1266
}

static inline void
1267
queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
L
Linus Torvalds 已提交
1268
{
1269
	spin_unlock(&hb->lock);
1270
	drop_futex_key_refs(&q->key);
L
Linus Torvalds 已提交
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
}

/*
 * queue_me and unqueue_me must be called as a pair, each
 * exactly once.  They are called with the hashed spinlock held.
 */

/* The key must be already stored in q->key. */
static void queue_me(struct futex_q *q, int fd, struct file *filp)
{
1281 1282 1283 1284
	struct futex_hash_bucket *hb;

	hb = queue_lock(q, fd, filp);
	__queue_me(q, hb);
L
Linus Torvalds 已提交
1285 1286 1287 1288 1289 1290
}

/* Return 1 if we were still queued (ie. 0 means we were woken) */
static int unqueue_me(struct futex_q *q)
{
	spinlock_t *lock_ptr;
1291
	int ret = 0;
L
Linus Torvalds 已提交
1292 1293 1294 1295

	/* In the common case we don't take the spinlock, which is nice. */
 retry:
	lock_ptr = q->lock_ptr;
1296
	barrier();
L
Linus Torvalds 已提交
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
	if (lock_ptr != 0) {
		spin_lock(lock_ptr);
		/*
		 * q->lock_ptr can change between reading it and
		 * spin_lock(), causing us to take the wrong lock.  This
		 * corrects the race condition.
		 *
		 * Reasoning goes like this: if we have the wrong lock,
		 * q->lock_ptr must have changed (maybe several times)
		 * between reading it and the spin_lock().  It can
		 * change again after the spin_lock() but only if it was
		 * already changed before the spin_lock().  It cannot,
		 * however, change back to the original value.  Therefore
		 * we can detect whether we acquired the correct lock.
		 */
		if (unlikely(lock_ptr != q->lock_ptr)) {
			spin_unlock(lock_ptr);
			goto retry;
		}
P
Pierre Peiffer 已提交
1316 1317
		WARN_ON(plist_node_empty(&q->list));
		plist_del(&q->list, &q->list.plist);
1318 1319 1320

		BUG_ON(q->pi_state);

L
Linus Torvalds 已提交
1321 1322 1323 1324
		spin_unlock(lock_ptr);
		ret = 1;
	}

1325
	drop_futex_key_refs(&q->key);
L
Linus Torvalds 已提交
1326 1327 1328
	return ret;
}

1329 1330
/*
 * PI futexes can not be requeued and must remove themself from the
P
Pierre Peiffer 已提交
1331 1332
 * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
 * and dropped here.
1333
 */
P
Pierre Peiffer 已提交
1334
static void unqueue_me_pi(struct futex_q *q)
1335
{
P
Pierre Peiffer 已提交
1336 1337
	WARN_ON(plist_node_empty(&q->list));
	plist_del(&q->list, &q->list.plist);
1338 1339 1340 1341 1342

	BUG_ON(!q->pi_state);
	free_pi_state(q->pi_state);
	q->pi_state = NULL;

P
Pierre Peiffer 已提交
1343
	spin_unlock(q->lock_ptr);
1344

1345
	drop_futex_key_refs(&q->key);
1346 1347
}

P
Pierre Peiffer 已提交
1348 1349 1350
/*
 * Fixup the pi_state owner with current.
 *
1351 1352
 * Must be called with hash bucket lock held and mm->sem held for non
 * private futexes.
P
Pierre Peiffer 已提交
1353
 */
1354
static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
P
Pierre Peiffer 已提交
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
				struct task_struct *curr)
{
	u32 newtid = curr->pid | FUTEX_WAITERS;
	struct futex_pi_state *pi_state = q->pi_state;
	u32 uval, curval, newval;
	int ret;

	/* Owner died? */
	if (pi_state->owner != NULL) {
		spin_lock_irq(&pi_state->owner->pi_lock);
		WARN_ON(list_empty(&pi_state->list));
		list_del_init(&pi_state->list);
		spin_unlock_irq(&pi_state->owner->pi_lock);
	} else
		newtid |= FUTEX_OWNER_DIED;

	pi_state->owner = curr;

	spin_lock_irq(&curr->pi_lock);
	WARN_ON(!list_empty(&pi_state->list));
	list_add(&pi_state->list, &curr->pi_state_list);
	spin_unlock_irq(&curr->pi_lock);

	/*
	 * We own it, so we have to replace the pending owner
	 * TID. This must be atomic as we have preserve the
	 * owner died bit here.
	 */
1383 1384
	ret = get_futex_value_locked(&uval, uaddr);

P
Pierre Peiffer 已提交
1385 1386 1387
	while (!ret) {
		newval = (uval & FUTEX_OWNER_DIED) | newtid;
		newval |= (uval & FUTEX_WAITER_REQUEUED);
1388 1389

		pagefault_disable();
P
Pierre Peiffer 已提交
1390 1391
		curval = futex_atomic_cmpxchg_inatomic(uaddr,
						       uval, newval);
1392 1393
		pagefault_enable();

P
Pierre Peiffer 已提交
1394
		if (curval == -EFAULT)
1395
			ret = -EFAULT;
P
Pierre Peiffer 已提交
1396 1397 1398 1399 1400 1401 1402
		if (curval == uval)
			break;
		uval = curval;
	}
	return ret;
}

E
Eric Dumazet 已提交
1403 1404 1405 1406 1407 1408
/*
 * In case we must use restart_block to restart a futex_wait,
 * we encode in the 'arg3' shared capability
 */
#define ARG3_SHARED  1

N
Nick Piggin 已提交
1409
static long futex_wait_restart(struct restart_block *restart);
E
Eric Dumazet 已提交
1410 1411
static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
		      u32 val, ktime_t *abs_time)
L
Linus Torvalds 已提交
1412
{
1413 1414
	struct task_struct *curr = current;
	DECLARE_WAITQUEUE(wait, curr);
1415
	struct futex_hash_bucket *hb;
L
Linus Torvalds 已提交
1416
	struct futex_q q;
1417 1418
	u32 uval;
	int ret;
P
Pierre Peiffer 已提交
1419
	struct hrtimer_sleeper t, *to = NULL;
1420
	int rem = 0;
L
Linus Torvalds 已提交
1421

1422
	q.pi_state = NULL;
L
Linus Torvalds 已提交
1423
 retry:
E
Eric Dumazet 已提交
1424 1425
	if (fshared)
		down_read(fshared);
L
Linus Torvalds 已提交
1426

E
Eric Dumazet 已提交
1427
	ret = get_futex_key(uaddr, fshared, &q.key);
L
Linus Torvalds 已提交
1428 1429 1430
	if (unlikely(ret != 0))
		goto out_release_sem;

1431
	hb = queue_lock(&q, -1, NULL);
L
Linus Torvalds 已提交
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449

	/*
	 * Access the page AFTER the futex is queued.
	 * Order is important:
	 *
	 *   Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
	 *   Userspace waker:  if (cond(var)) { var = new; futex_wake(&var); }
	 *
	 * The basic logical guarantee of a futex is that it blocks ONLY
	 * if cond(var) is known to be true at the time of blocking, for
	 * any cond.  If we queued after testing *uaddr, that would open
	 * a race condition where we could block indefinitely with
	 * cond(var) false, which would violate the guarantee.
	 *
	 * A consequence is that futex_wait() can return zero and absorb
	 * a wakeup when *uaddr != val on entry to the syscall.  This is
	 * rare, but normal.
	 *
E
Eric Dumazet 已提交
1450 1451
	 * for shared futexes, we hold the mmap semaphore, so the mapping
	 * cannot have changed since we looked it up in get_futex_key.
L
Linus Torvalds 已提交
1452
	 */
1453
	ret = get_futex_value_locked(&uval, uaddr);
L
Linus Torvalds 已提交
1454 1455

	if (unlikely(ret)) {
1456
		queue_unlock(&q, hb);
L
Linus Torvalds 已提交
1457

1458 1459
		/*
		 * If we would have faulted, release mmap_sem, fault it in and
L
Linus Torvalds 已提交
1460 1461
		 * start all over again.
		 */
E
Eric Dumazet 已提交
1462 1463
		if (fshared)
			up_read(fshared);
L
Linus Torvalds 已提交
1464

1465
		ret = get_user(uval, uaddr);
L
Linus Torvalds 已提交
1466 1467 1468 1469 1470

		if (!ret)
			goto retry;
		return ret;
	}
1471 1472 1473
	ret = -EWOULDBLOCK;
	if (uval != val)
		goto out_unlock_release_sem;
L
Linus Torvalds 已提交
1474

P
Pierre Peiffer 已提交
1475 1476 1477 1478 1479 1480 1481 1482
	/*
	 * This rt_mutex_waiter structure is prepared here and will
	 * be used only if this task is requeued from a normal futex to
	 * a PI-futex with futex_requeue_pi.
	 */
	debug_rt_mutex_init_waiter(&q.waiter);
	q.waiter.task = NULL;

L
Linus Torvalds 已提交
1483
	/* Only actually queue if *uaddr contained val.  */
1484
	__queue_me(&q, hb);
L
Linus Torvalds 已提交
1485 1486 1487 1488

	/*
	 * Now the futex is queued and we have checked the data, we
	 * don't want to hold mmap_sem while we sleep.
1489
	 */
E
Eric Dumazet 已提交
1490 1491
	if (fshared)
		up_read(fshared);
L
Linus Torvalds 已提交
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505

	/*
	 * There might have been scheduling since the queue_me(), as we
	 * cannot hold a spinlock across the get_user() in case it
	 * faults, and we cannot just set TASK_INTERRUPTIBLE state when
	 * queueing ourselves into the futex hash.  This code thus has to
	 * rely on the futex_wake() code removing us from hash when it
	 * wakes us up.
	 */

	/* add_wait_queue is the barrier after __set_current_state. */
	__set_current_state(TASK_INTERRUPTIBLE);
	add_wait_queue(&q.waiters, &wait);
	/*
P
Pierre Peiffer 已提交
1506
	 * !plist_node_empty() is safe here without any lock.
L
Linus Torvalds 已提交
1507 1508
	 * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
	 */
P
Pierre Peiffer 已提交
1509
	if (likely(!plist_node_empty(&q.list))) {
1510 1511 1512
		if (!abs_time)
			schedule();
		else {
P
Pierre Peiffer 已提交
1513
			to = &t;
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
			hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
			hrtimer_init_sleeper(&t, current);
			t.timer.expires = *abs_time;

			hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);

			/*
			 * the timer could have already expired, in which
			 * case current would be flagged for rescheduling.
			 * Don't bother calling schedule.
			 */
			if (likely(t.task))
				schedule();

			hrtimer_cancel(&t.timer);
N
Nick Piggin 已提交
1529

1530 1531 1532
			/* Flag if a timeout occured */
			rem = (t.task == NULL);
		}
N
Nick Piggin 已提交
1533
	}
L
Linus Torvalds 已提交
1534 1535 1536 1537 1538 1539 1540
	__set_current_state(TASK_RUNNING);

	/*
	 * NOTE: we don't remove ourselves from the waitqueue because
	 * we are the only user of it.
	 */

P
Pierre Peiffer 已提交
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
	if (q.pi_state) {
		/*
		 * We were woken but have been requeued on a PI-futex.
		 * We have to complete the lock acquisition by taking
		 * the rtmutex.
		 */

		struct rt_mutex *lock = &q.pi_state->pi_mutex;

		spin_lock(&lock->wait_lock);
		if (unlikely(q.waiter.task)) {
			remove_waiter(lock, &q.waiter);
		}
		spin_unlock(&lock->wait_lock);

		if (rem)
			ret = -ETIMEDOUT;
		else
			ret = rt_mutex_timed_lock(lock, to, 1);

E
Eric Dumazet 已提交
1561 1562
		if (fshared)
			down_read(fshared);
P
Pierre Peiffer 已提交
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
		spin_lock(q.lock_ptr);

		/*
		 * Got the lock. We might not be the anticipated owner if we
		 * did a lock-steal - fix up the PI-state in that case.
		 */
		if (!ret && q.pi_state->owner != curr) {
			/*
			 * We MUST play with the futex we were requeued on,
			 * NOT the current futex.
			 * We can retrieve it from the key of the pi_state
			 */
			uaddr = q.pi_state->key.uaddr;

1577
			ret = fixup_pi_state_owner(uaddr, &q, curr);
P
Pierre Peiffer 已提交
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
		} else {
			/*
			 * Catch the rare case, where the lock was released
			 * when we were on the way back before we locked
			 * the hash bucket.
			 */
			if (ret && q.pi_state->owner == curr) {
				if (rt_mutex_trylock(&q.pi_state->pi_mutex))
					ret = 0;
			}
		}

1590 1591 1592 1593 1594
		/* Unqueue and drop the lock */
		unqueue_me_pi(&q);
		if (fshared)
			up_read(fshared);

P
Pierre Peiffer 已提交
1595 1596 1597 1598 1599 1600 1601
		debug_rt_mutex_free_waiter(&q.waiter);

		return ret;
	}

	debug_rt_mutex_free_waiter(&q.waiter);

L
Linus Torvalds 已提交
1602 1603 1604
	/* If we were woken (and unqueued), we succeeded, whatever. */
	if (!unqueue_me(&q))
		return 0;
1605
	if (rem)
L
Linus Torvalds 已提交
1606
		return -ETIMEDOUT;
N
Nick Piggin 已提交
1607

1608 1609 1610 1611
	/*
	 * We expect signal_pending(current), but another thread may
	 * have handled it for us already.
	 */
1612
	if (!abs_time)
N
Nick Piggin 已提交
1613 1614 1615 1616 1617 1618 1619
		return -ERESTARTSYS;
	else {
		struct restart_block *restart;
		restart = &current_thread_info()->restart_block;
		restart->fn = futex_wait_restart;
		restart->arg0 = (unsigned long)uaddr;
		restart->arg1 = (unsigned long)val;
1620
		restart->arg2 = (unsigned long)abs_time;
E
Eric Dumazet 已提交
1621 1622 1623
		restart->arg3 = 0;
		if (fshared)
			restart->arg3 |= ARG3_SHARED;
N
Nick Piggin 已提交
1624 1625
		return -ERESTART_RESTARTBLOCK;
	}
L
Linus Torvalds 已提交
1626

1627 1628 1629
 out_unlock_release_sem:
	queue_unlock(&q, hb);

L
Linus Torvalds 已提交
1630
 out_release_sem:
E
Eric Dumazet 已提交
1631 1632
	if (fshared)
		up_read(fshared);
1633 1634 1635
	return ret;
}

N
Nick Piggin 已提交
1636 1637 1638 1639 1640

static long futex_wait_restart(struct restart_block *restart)
{
	u32 __user *uaddr = (u32 __user *)restart->arg0;
	u32 val = (u32)restart->arg1;
1641
	ktime_t *abs_time = (ktime_t *)restart->arg2;
E
Eric Dumazet 已提交
1642
	struct rw_semaphore *fshared = NULL;
N
Nick Piggin 已提交
1643 1644

	restart->fn = do_no_restart_syscall;
E
Eric Dumazet 已提交
1645 1646 1647
	if (restart->arg3 & ARG3_SHARED)
		fshared = &current->mm->mmap_sem;
	return (long)futex_wait(uaddr, fshared, val, abs_time);
N
Nick Piggin 已提交
1648 1649 1650
}


P
Pierre Peiffer 已提交
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
static void set_pi_futex_owner(struct futex_hash_bucket *hb,
			       union futex_key *key, struct task_struct *p)
{
	struct plist_head *head;
	struct futex_q *this, *next;
	struct futex_pi_state *pi_state = NULL;
	struct rt_mutex *lock;

	/* Search a waiter that should already exists */

	head = &hb->chain;

	plist_for_each_entry_safe(this, next, head, list) {
		if (match_futex (&this->key, key)) {
			pi_state = this->pi_state;
			break;
		}
	}

	BUG_ON(!pi_state);

	/* set p as pi_state's owner */
	lock = &pi_state->pi_mutex;

	spin_lock(&lock->wait_lock);
	spin_lock_irq(&p->pi_lock);

	list_add(&pi_state->list, &p->pi_state_list);
	pi_state->owner = p;


	/* set p as pi_mutex's owner */
	debug_rt_mutex_proxy_lock(lock, p);
	WARN_ON(rt_mutex_owner(lock));
	rt_mutex_set_owner(lock, p, 0);
	rt_mutex_deadlock_account_lock(lock, p);

	plist_add(&rt_mutex_top_waiter(lock)->pi_list_entry,
		  &p->pi_waiters);
	__rt_mutex_adjust_prio(p);

	spin_unlock_irq(&p->pi_lock);
	spin_unlock(&lock->wait_lock);
}

1696 1697 1698 1699 1700 1701
/*
 * Userspace tried a 0 -> TID atomic transition of the futex value
 * and failed. The kernel side here does the whole locking operation:
 * if there are waiters then it will block, it does PI, etc. (Due to
 * races the kernel might see a 0 value of the futex too.)
 */
E
Eric Dumazet 已提交
1702 1703
static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
			 int detect, ktime_t *time, int trylock)
1704
{
1705
	struct hrtimer_sleeper timeout, *to = NULL;
1706 1707 1708 1709
	struct task_struct *curr = current;
	struct futex_hash_bucket *hb;
	u32 uval, newval, curval;
	struct futex_q q;
1710
	int ret, lock_taken, ownerdied = 0, attempt = 0;
1711 1712 1713 1714

	if (refill_pi_state_cache())
		return -ENOMEM;

1715
	if (time) {
1716
		to = &timeout;
1717
		hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
1718
		hrtimer_init_sleeper(to, current);
1719
		to->timer.expires = *time;
1720 1721
	}

1722 1723
	q.pi_state = NULL;
 retry:
E
Eric Dumazet 已提交
1724 1725
	if (fshared)
		down_read(fshared);
1726

E
Eric Dumazet 已提交
1727
	ret = get_futex_key(uaddr, fshared, &q.key);
1728 1729 1730
	if (unlikely(ret != 0))
		goto out_release_sem;

1731
 retry_unlocked:
1732 1733 1734
	hb = queue_lock(&q, -1, NULL);

 retry_locked:
1735
	ret = lock_taken = 0;
P
Pierre Peiffer 已提交
1736

1737 1738 1739 1740 1741 1742 1743
	/*
	 * To avoid races, we attempt to take the lock here again
	 * (by doing a 0 -> TID atomic cmpxchg), while holding all
	 * the locks. It will most likely not succeed.
	 */
	newval = current->pid;

1744
	pagefault_disable();
1745
	curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
1746
	pagefault_enable();
1747 1748 1749 1750

	if (unlikely(curval == -EFAULT))
		goto uaddr_faulted;

1751 1752 1753 1754
	/*
	 * Detect deadlocks. In case of REQUEUE_PI this is a valid
	 * situation and we return success to user space.
	 */
1755
	if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
P
Pierre Peiffer 已提交
1756 1757
		if (!(curval & FUTEX_WAITER_REQUEUED))
			ret = -EDEADLK;
1758 1759 1760 1761
		goto out_unlock_release_sem;
	}

	/*
1762
	 * Surprise - we got the lock. Just return to userspace:
1763 1764 1765 1766 1767
	 */
	if (unlikely(!curval))
		goto out_unlock_release_sem;

	uval = curval;
1768

P
Pierre Peiffer 已提交
1769
	/*
1770 1771
	 * Set the WAITERS flag, so the owner will know it has someone
	 * to wake at next unlock
P
Pierre Peiffer 已提交
1772
	 */
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
	newval = curval | FUTEX_WAITERS;

	/*
	 * There are two cases, where a futex might have no owner (the
	 * owner TID is 0): OWNER_DIED or REQUEUE. We take over the
	 * futex in this case. We also do an unconditional take over,
	 * when the owner of the futex died.
	 *
	 * This is safe as we are protected by the hash bucket lock !
	 */
	if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
		/* Keep the OWNER_DIED and REQUEUE bits */
		newval = (curval & ~FUTEX_TID_MASK) | current->pid;
		ownerdied = 0;
		lock_taken = 1;
	}
1789

1790
	pagefault_disable();
1791
	curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
1792
	pagefault_enable();
1793 1794 1795 1796 1797 1798

	if (unlikely(curval == -EFAULT))
		goto uaddr_faulted;
	if (unlikely(curval != uval))
		goto retry_locked;

1799 1800 1801 1802 1803 1804 1805
	/*
	 * We took the lock due to requeue or owner died take over.
	 */
	if (unlikely(lock_taken)) {
		/* For requeue we need to fixup the pi_futex */
		if (curval & FUTEX_WAITER_REQUEUED)
			set_pi_futex_owner(hb, &q.key, curr);
P
Pierre Peiffer 已提交
1806 1807 1808
		goto out_unlock_release_sem;
	}

1809 1810 1811 1812
	/*
	 * We dont have the lock. Look up the PI state (or create it if
	 * we are the first waiter):
	 */
P
Pierre Peiffer 已提交
1813
	ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
1814 1815

	if (unlikely(ret)) {
1816
		switch (ret) {
1817

1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
		case -EAGAIN:
			/*
			 * Task is exiting and we just wait for the
			 * exit to complete.
			 */
			queue_unlock(&q, hb);
			if (fshared)
				up_read(fshared);
			cond_resched();
			goto retry;
1828

1829 1830 1831 1832 1833 1834 1835
		case -ESRCH:
			/*
			 * No owner found for this futex. Check if the
			 * OWNER_DIED bit is set to figure out whether
			 * this is a robust futex or not.
			 */
			if (get_futex_value_locked(&curval, uaddr))
1836
				goto uaddr_faulted;
1837 1838 1839 1840 1841 1842 1843 1844

			/*
			 * We simply start over in case of a robust
			 * futex. The code above will take the futex
			 * and return happy.
			 */
			if (curval & FUTEX_OWNER_DIED) {
				ownerdied = 1;
1845
				goto retry_locked;
1846 1847 1848
			}
		default:
			goto out_unlock_release_sem;
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860
		}
	}

	/*
	 * Only actually queue now that the atomic ops are done:
	 */
	__queue_me(&q, hb);

	/*
	 * Now the futex is queued and we have checked the data, we
	 * don't want to hold mmap_sem while we sleep.
	 */
E
Eric Dumazet 已提交
1861 1862
	if (fshared)
		up_read(fshared);
1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875

	WARN_ON(!q.pi_state);
	/*
	 * Block on the PI mutex:
	 */
	if (!trylock)
		ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
	else {
		ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
		/* Fixup the trylock return value: */
		ret = ret ? 0 : -EWOULDBLOCK;
	}

E
Eric Dumazet 已提交
1876 1877
	if (fshared)
		down_read(fshared);
1878
	spin_lock(q.lock_ptr);
1879

1880 1881 1882 1883 1884 1885 1886 1887 1888
	if (!ret) {
		/*
		 * Got the lock. We might not be the anticipated owner
		 * if we did a lock-steal - fix up the PI-state in
		 * that case:
		 */
		if (q.pi_state->owner != curr)
			ret = fixup_pi_state_owner(uaddr, &q, curr);
	} else {
1889 1890
		/*
		 * Catch the rare case, where the lock was released
1891 1892
		 * when we were on the way back before we locked the
		 * hash bucket.
1893
		 */
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908
		if (q.pi_state->owner == curr &&
		    rt_mutex_trylock(&q.pi_state->pi_mutex)) {
			ret = 0;
		} else {
			/*
			 * Paranoia check. If we did not take the lock
			 * in the trylock above, then we should not be
			 * the owner of the rtmutex, neither the real
			 * nor the pending one:
			 */
			if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
				printk(KERN_ERR "futex_lock_pi: ret = %d "
				       "pi-mutex: %p pi-state %p\n", ret,
				       q.pi_state->pi_mutex.owner,
				       q.pi_state->owner);
1909 1910 1911
		}
	}

1912 1913 1914 1915
	/* Unqueue and drop the lock */
	unqueue_me_pi(&q);
	if (fshared)
		up_read(fshared);
1916

1917
	return ret != -EINTR ? ret : -ERESTARTNOINTR;
1918 1919 1920 1921 1922

 out_unlock_release_sem:
	queue_unlock(&q, hb);

 out_release_sem:
E
Eric Dumazet 已提交
1923 1924
	if (fshared)
		up_read(fshared);
1925 1926 1927 1928 1929 1930 1931 1932
	return ret;

 uaddr_faulted:
	/*
	 * We have to r/w  *(int __user *)uaddr, but we can't modify it
	 * non-atomically.  Therefore, if get_user below is not
	 * enough, we need to handle the fault ourselves, while
	 * still holding the mmap_sem.
1933 1934
	 *
	 * ... and hb->lock. :-) --ANK
1935
	 */
1936 1937
	queue_unlock(&q, hb);

1938
	if (attempt++) {
E
Eric Dumazet 已提交
1939 1940 1941
		ret = futex_handle_fault((unsigned long)uaddr, fshared,
					 attempt);
		if (ret)
1942 1943
			goto out_release_sem;
		goto retry_unlocked;
1944 1945
	}

E
Eric Dumazet 已提交
1946 1947
	if (fshared)
		up_read(fshared);
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960

	ret = get_user(uval, uaddr);
	if (!ret && (uval != -EFAULT))
		goto retry;

	return ret;
}

/*
 * Userspace attempted a TID -> 0 atomic transition, and failed.
 * This is the in-kernel slowpath: we look up the PI state (if any),
 * and do the rt-mutex unlock.
 */
E
Eric Dumazet 已提交
1961
static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
1962 1963 1964 1965
{
	struct futex_hash_bucket *hb;
	struct futex_q *this, *next;
	u32 uval;
P
Pierre Peiffer 已提交
1966
	struct plist_head *head;
1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
	union futex_key key;
	int ret, attempt = 0;

retry:
	if (get_user(uval, uaddr))
		return -EFAULT;
	/*
	 * We release only a lock we actually own:
	 */
	if ((uval & FUTEX_TID_MASK) != current->pid)
		return -EPERM;
	/*
	 * First take all the futex related locks:
	 */
E
Eric Dumazet 已提交
1981 1982
	if (fshared)
		down_read(fshared);
1983

E
Eric Dumazet 已提交
1984
	ret = get_futex_key(uaddr, fshared, &key);
1985 1986 1987 1988
	if (unlikely(ret != 0))
		goto out;

	hb = hash_futex(&key);
1989
retry_unlocked:
1990 1991 1992 1993 1994 1995 1996
	spin_lock(&hb->lock);

	/*
	 * To avoid races, try to do the TID -> 0 atomic transition
	 * again. If it succeeds then we can return without waking
	 * anyone else up:
	 */
1997
	if (!(uval & FUTEX_OWNER_DIED)) {
1998
		pagefault_disable();
1999
		uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
2000
		pagefault_enable();
2001
	}
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017

	if (unlikely(uval == -EFAULT))
		goto pi_faulted;
	/*
	 * Rare case: we managed to release the lock atomically,
	 * no need to wake anyone else up:
	 */
	if (unlikely(uval == current->pid))
		goto out_unlock;

	/*
	 * Ok, other tasks may need to be woken up - check waiters
	 * and do the wakeup if necessary:
	 */
	head = &hb->chain;

P
Pierre Peiffer 已提交
2018
	plist_for_each_entry_safe(this, next, head, list) {
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
		if (!match_futex (&this->key, &key))
			continue;
		ret = wake_futex_pi(uaddr, uval, this);
		/*
		 * The atomic access to the futex value
		 * generated a pagefault, so retry the
		 * user-access and the wakeup:
		 */
		if (ret == -EFAULT)
			goto pi_faulted;
		goto out_unlock;
	}
	/*
	 * No waiters - kernel unlocks the futex:
	 */
2034 2035 2036 2037 2038
	if (!(uval & FUTEX_OWNER_DIED)) {
		ret = unlock_futex_pi(uaddr, uval);
		if (ret == -EFAULT)
			goto pi_faulted;
	}
2039 2040 2041 2042

out_unlock:
	spin_unlock(&hb->lock);
out:
E
Eric Dumazet 已提交
2043 2044
	if (fshared)
		up_read(fshared);
2045 2046 2047 2048 2049 2050 2051 2052 2053

	return ret;

pi_faulted:
	/*
	 * We have to r/w  *(int __user *)uaddr, but we can't modify it
	 * non-atomically.  Therefore, if get_user below is not
	 * enough, we need to handle the fault ourselves, while
	 * still holding the mmap_sem.
2054 2055
	 *
	 * ... and hb->lock. --ANK
2056
	 */
2057 2058
	spin_unlock(&hb->lock);

2059
	if (attempt++) {
E
Eric Dumazet 已提交
2060 2061 2062
		ret = futex_handle_fault((unsigned long)uaddr, fshared,
					 attempt);
		if (ret)
2063 2064
			goto out;
		goto retry_unlocked;
2065 2066
	}

E
Eric Dumazet 已提交
2067 2068
	if (fshared)
		up_read(fshared);
2069 2070 2071 2072 2073

	ret = get_user(uval, uaddr);
	if (!ret && (uval != -EFAULT))
		goto retry;

L
Linus Torvalds 已提交
2074 2075 2076 2077 2078 2079 2080 2081 2082
	return ret;
}

static int futex_close(struct inode *inode, struct file *filp)
{
	struct futex_q *q = filp->private_data;

	unqueue_me(q);
	kfree(q);
2083

L
Linus Torvalds 已提交
2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
	return 0;
}

/* This is one-shot: once it's gone off you need a new fd */
static unsigned int futex_poll(struct file *filp,
			       struct poll_table_struct *wait)
{
	struct futex_q *q = filp->private_data;
	int ret = 0;

	poll_wait(filp, &q->waiters, wait);

	/*
P
Pierre Peiffer 已提交
2097
	 * plist_node_empty() is safe here without any lock.
L
Linus Torvalds 已提交
2098 2099
	 * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
	 */
P
Pierre Peiffer 已提交
2100
	if (plist_node_empty(&q->list))
L
Linus Torvalds 已提交
2101 2102 2103 2104 2105
		ret = POLLIN | POLLRDNORM;

	return ret;
}

2106
static const struct file_operations futex_fops = {
L
Linus Torvalds 已提交
2107 2108 2109 2110 2111 2112 2113 2114
	.release	= futex_close,
	.poll		= futex_poll,
};

/*
 * Signal allows caller to avoid the race which would occur if they
 * set the sigio stuff up afterwards.
 */
2115
static int futex_fd(u32 __user *uaddr, int signal)
L
Linus Torvalds 已提交
2116 2117 2118 2119
{
	struct futex_q *q;
	struct file *filp;
	int ret, err;
E
Eric Dumazet 已提交
2120
	struct rw_semaphore *fshared;
2121 2122 2123 2124 2125 2126 2127
	static unsigned long printk_interval;

	if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
		printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
		    	"will be removed from the kernel in June 2007\n",
			current->comm);
	}
L
Linus Torvalds 已提交
2128 2129

	ret = -EINVAL;
2130
	if (!valid_signal(signal))
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
		goto out;

	ret = get_unused_fd();
	if (ret < 0)
		goto out;
	filp = get_empty_filp();
	if (!filp) {
		put_unused_fd(ret);
		ret = -ENFILE;
		goto out;
	}
	filp->f_op = &futex_fops;
2143 2144 2145
	filp->f_path.mnt = mntget(futex_mnt);
	filp->f_path.dentry = dget(futex_mnt->mnt_root);
	filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
L
Linus Torvalds 已提交
2146 2147

	if (signal) {
2148
		err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
L
Linus Torvalds 已提交
2149
		if (err < 0) {
2150
			goto error;
L
Linus Torvalds 已提交
2151 2152 2153 2154 2155 2156
		}
		filp->f_owner.signum = signal;
	}

	q = kmalloc(sizeof(*q), GFP_KERNEL);
	if (!q) {
2157 2158
		err = -ENOMEM;
		goto error;
L
Linus Torvalds 已提交
2159
	}
2160
	q->pi_state = NULL;
L
Linus Torvalds 已提交
2161

E
Eric Dumazet 已提交
2162 2163 2164
	fshared = &current->mm->mmap_sem;
	down_read(fshared);
	err = get_futex_key(uaddr, fshared, &q->key);
L
Linus Torvalds 已提交
2165 2166

	if (unlikely(err != 0)) {
E
Eric Dumazet 已提交
2167
		up_read(fshared);
L
Linus Torvalds 已提交
2168
		kfree(q);
2169
		goto error;
L
Linus Torvalds 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178
	}

	/*
	 * queue_me() must be called before releasing mmap_sem, because
	 * key->shared.inode needs to be referenced while holding it.
	 */
	filp->private_data = q;

	queue_me(q, ret, filp);
E
Eric Dumazet 已提交
2179
	up_read(fshared);
L
Linus Torvalds 已提交
2180 2181 2182 2183 2184

	/* Now we map fd to filp, so userspace can access it */
	fd_install(ret, filp);
out:
	return ret;
2185 2186 2187 2188 2189
error:
	put_unused_fd(ret);
	put_filp(filp);
	ret = err;
	goto out;
L
Linus Torvalds 已提交
2190 2191
}

2192 2193 2194 2195 2196 2197 2198
/*
 * Support for robust futexes: the kernel cleans up held futexes at
 * thread exit time.
 *
 * Implementation: user-space maintains a per-thread list of locks it
 * is holding. Upon do_exit(), the kernel carefully walks this list,
 * and marks all locks that are owned by this thread with the
2199
 * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
 * always manipulated with the lock held, so the list is private and
 * per-thread. Userspace also maintains a per-thread 'list_op_pending'
 * field, to allow the kernel to clean up if the thread dies after
 * acquiring the lock, but just before it could have added itself to
 * the list. There can only be one such pending lock.
 */

/**
 * sys_set_robust_list - set the robust-futex list head of a task
 * @head: pointer to the list-head
 * @len: length of the list-head, as userspace expects
 */
asmlinkage long
sys_set_robust_list(struct robust_list_head __user *head,
		    size_t len)
{
	/*
	 * The kernel knows only one size for now:
	 */
	if (unlikely(len != sizeof(*head)))
		return -EINVAL;

	current->robust_list = head;

	return 0;
}

/**
 * sys_get_robust_list - get the robust-futex list head of a task
 * @pid: pid of the process [zero for current task]
 * @head_ptr: pointer to a list-head pointer, the kernel fills it in
 * @len_ptr: pointer to a length field, the kernel fills in the header size
 */
asmlinkage long
A
Al Viro 已提交
2234
sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
2235 2236
		    size_t __user *len_ptr)
{
A
Al Viro 已提交
2237
	struct robust_list_head __user *head;
2238 2239 2240 2241 2242 2243 2244 2245
	unsigned long ret;

	if (!pid)
		head = current->robust_list;
	else {
		struct task_struct *p;

		ret = -ESRCH;
2246
		rcu_read_lock();
2247 2248 2249 2250 2251 2252 2253 2254
		p = find_task_by_pid(pid);
		if (!p)
			goto err_unlock;
		ret = -EPERM;
		if ((current->euid != p->euid) && (current->euid != p->uid) &&
				!capable(CAP_SYS_PTRACE))
			goto err_unlock;
		head = p->robust_list;
2255
		rcu_read_unlock();
2256 2257 2258 2259 2260 2261 2262
	}

	if (put_user(sizeof(*head), len_ptr))
		return -EFAULT;
	return put_user(head, head_ptr);

err_unlock:
2263
	rcu_read_unlock();
2264 2265 2266 2267 2268 2269 2270 2271

	return ret;
}

/*
 * Process a futex-list entry, check whether it's owned by the
 * dying task, and do notification if so:
 */
2272
int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
2273
{
2274
	u32 uval, nval, mval;
2275

2276 2277
retry:
	if (get_user(uval, uaddr))
2278 2279
		return -1;

2280
	if ((uval & FUTEX_TID_MASK) == curr->pid) {
2281 2282 2283 2284 2285 2286 2287 2288 2289 2290
		/*
		 * Ok, this dying thread is truly holding a futex
		 * of interest. Set the OWNER_DIED bit atomically
		 * via cmpxchg, and if the value had FUTEX_WAITERS
		 * set, wake up a waiter (if any). (We have to do a
		 * futex_wake() even if OWNER_DIED is already set -
		 * to handle the rare but possible case of recursive
		 * thread-death.) The rest of the cleanup is done in
		 * userspace.
		 */
2291
		mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
P
Pierre Peiffer 已提交
2292 2293
		/* Also keep the FUTEX_WAITER_REQUEUED flag if set */
		mval |= (uval & FUTEX_WAITER_REQUEUED);
2294 2295
		nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);

2296 2297 2298 2299
		if (nval == -EFAULT)
			return -1;

		if (nval != uval)
2300
			goto retry;
2301

2302 2303 2304 2305 2306 2307
		/*
		 * Wake robust non-PI futexes here. The wakeup of
		 * PI futexes happens in exit_pi_state():
		 */
		if (!pi) {
			if (uval & FUTEX_WAITERS)
E
Eric Dumazet 已提交
2308
				futex_wake(uaddr, &curr->mm->mmap_sem, 1);
2309
		}
2310 2311 2312 2313
	}
	return 0;
}

2314 2315 2316 2317
/*
 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
 */
static inline int fetch_robust_entry(struct robust_list __user **entry,
A
Al Viro 已提交
2318 2319
				     struct robust_list __user * __user *head,
				     int *pi)
2320 2321 2322
{
	unsigned long uentry;

A
Al Viro 已提交
2323
	if (get_user(uentry, (unsigned long __user *)head))
2324 2325
		return -EFAULT;

A
Al Viro 已提交
2326
	*entry = (void __user *)(uentry & ~1UL);
2327 2328 2329 2330 2331
	*pi = uentry & 1;

	return 0;
}

2332 2333 2334 2335 2336 2337 2338 2339 2340 2341
/*
 * Walk curr->robust_list (very carefully, it's a userspace list!)
 * and mark any locks found there dead, and notify any waiters.
 *
 * We silently return on any sign of list-walking problem.
 */
void exit_robust_list(struct task_struct *curr)
{
	struct robust_list_head __user *head = curr->robust_list;
	struct robust_list __user *entry, *pending;
2342
	unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
2343 2344 2345 2346 2347 2348
	unsigned long futex_offset;

	/*
	 * Fetch the list head (which was registered earlier, via
	 * sys_set_robust_list()):
	 */
2349
	if (fetch_robust_entry(&entry, &head->list.next, &pi))
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359
		return;
	/*
	 * Fetch the relative futex offset:
	 */
	if (get_user(futex_offset, &head->futex_offset))
		return;
	/*
	 * Fetch any possibly pending lock-add first, and handle it
	 * if it exists:
	 */
2360
	if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
2361
		return;
2362

2363
	if (pending)
E
Eric Dumazet 已提交
2364 2365
		handle_futex_death((void __user *)pending + futex_offset,
				   curr, pip);
2366 2367 2368 2369

	while (entry != &head->list) {
		/*
		 * A pending lock might already be on the list, so
2370
		 * don't process it twice:
2371 2372
		 */
		if (entry != pending)
A
Al Viro 已提交
2373
			if (handle_futex_death((void __user *)entry + futex_offset,
2374
						curr, pi))
2375 2376 2377 2378
				return;
		/*
		 * Fetch the next entry in the list:
		 */
2379
		if (fetch_robust_entry(&entry, &entry->next, &pi))
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390
			return;
		/*
		 * Avoid excessively long or circular lists:
		 */
		if (!--limit)
			break;

		cond_resched();
	}
}

2391
long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
2392
		u32 __user *uaddr2, u32 val2, u32 val3)
L
Linus Torvalds 已提交
2393 2394
{
	int ret;
E
Eric Dumazet 已提交
2395 2396 2397 2398 2399
	int cmd = op & FUTEX_CMD_MASK;
	struct rw_semaphore *fshared = NULL;

	if (!(op & FUTEX_PRIVATE_FLAG))
		fshared = &current->mm->mmap_sem;
L
Linus Torvalds 已提交
2400

E
Eric Dumazet 已提交
2401
	switch (cmd) {
L
Linus Torvalds 已提交
2402
	case FUTEX_WAIT:
E
Eric Dumazet 已提交
2403
		ret = futex_wait(uaddr, fshared, val, timeout);
L
Linus Torvalds 已提交
2404 2405
		break;
	case FUTEX_WAKE:
E
Eric Dumazet 已提交
2406
		ret = futex_wake(uaddr, fshared, val);
L
Linus Torvalds 已提交
2407 2408 2409 2410 2411 2412
		break;
	case FUTEX_FD:
		/* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
		ret = futex_fd(uaddr, val);
		break;
	case FUTEX_REQUEUE:
E
Eric Dumazet 已提交
2413
		ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
L
Linus Torvalds 已提交
2414 2415
		break;
	case FUTEX_CMP_REQUEUE:
E
Eric Dumazet 已提交
2416
		ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
L
Linus Torvalds 已提交
2417
		break;
2418
	case FUTEX_WAKE_OP:
E
Eric Dumazet 已提交
2419
		ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
2420
		break;
2421
	case FUTEX_LOCK_PI:
E
Eric Dumazet 已提交
2422
		ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
2423 2424
		break;
	case FUTEX_UNLOCK_PI:
E
Eric Dumazet 已提交
2425
		ret = futex_unlock_pi(uaddr, fshared);
2426 2427
		break;
	case FUTEX_TRYLOCK_PI:
E
Eric Dumazet 已提交
2428
		ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
2429
		break;
P
Pierre Peiffer 已提交
2430
	case FUTEX_CMP_REQUEUE_PI:
E
Eric Dumazet 已提交
2431
		ret = futex_requeue_pi(uaddr, fshared, uaddr2, val, val2, &val3);
P
Pierre Peiffer 已提交
2432
		break;
L
Linus Torvalds 已提交
2433 2434 2435 2436 2437 2438 2439
	default:
		ret = -ENOSYS;
	}
	return ret;
}


2440
asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
L
Linus Torvalds 已提交
2441
			  struct timespec __user *utime, u32 __user *uaddr2,
2442
			  u32 val3)
L
Linus Torvalds 已提交
2443
{
2444 2445
	struct timespec ts;
	ktime_t t, *tp = NULL;
2446
	u32 val2 = 0;
E
Eric Dumazet 已提交
2447
	int cmd = op & FUTEX_CMD_MASK;
L
Linus Torvalds 已提交
2448

E
Eric Dumazet 已提交
2449
	if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
2450
		if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
L
Linus Torvalds 已提交
2451
			return -EFAULT;
2452
		if (!timespec_valid(&ts))
2453
			return -EINVAL;
2454 2455

		t = timespec_to_ktime(ts);
E
Eric Dumazet 已提交
2456
		if (cmd == FUTEX_WAIT)
2457 2458
			t = ktime_add(ktime_get(), t);
		tp = &t;
L
Linus Torvalds 已提交
2459 2460
	}
	/*
E
Eric Dumazet 已提交
2461
	 * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
L
Linus Torvalds 已提交
2462
	 */
E
Eric Dumazet 已提交
2463 2464
	if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE
	    || cmd == FUTEX_CMP_REQUEUE_PI)
2465
		val2 = (u32) (unsigned long) utime;
L
Linus Torvalds 已提交
2466

2467
	return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
L
Linus Torvalds 已提交
2468 2469
}

2470 2471 2472
static int futexfs_get_sb(struct file_system_type *fs_type,
			  int flags, const char *dev_name, void *data,
			  struct vfsmount *mnt)
L
Linus Torvalds 已提交
2473
{
2474
	return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
L
Linus Torvalds 已提交
2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
}

static struct file_system_type futex_fs_type = {
	.name		= "futexfs",
	.get_sb		= futexfs_get_sb,
	.kill_sb	= kill_anon_super,
};

static int __init init(void)
{
A
Akinobu Mita 已提交
2485 2486 2487 2488
	int i = register_filesystem(&futex_fs_type);

	if (i)
		return i;
L
Linus Torvalds 已提交
2489 2490

	futex_mnt = kern_mount(&futex_fs_type);
A
Akinobu Mita 已提交
2491 2492 2493 2494
	if (IS_ERR(futex_mnt)) {
		unregister_filesystem(&futex_fs_type);
		return PTR_ERR(futex_mnt);
	}
L
Linus Torvalds 已提交
2495 2496

	for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
P
Pierre Peiffer 已提交
2497
		plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
L
Linus Torvalds 已提交
2498 2499 2500 2501 2502
		spin_lock_init(&futex_queues[i].lock);
	}
	return 0;
}
__initcall(init);