futex.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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>
  42. #include <linux/signal.h>
  43. #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
  44. /*
  45. * Futexes are matched on equal values of this key.
  46. * The key type depends on whether it's a shared or private mapping.
  47. * Don't rearrange members without looking at hash_futex().
  48. *
  49. * offset is aligned to a multiple of sizeof(u32) (== 4) by definition.
  50. * We set bit 0 to indicate if it's an inode-based key.
  51. */
  52. union futex_key {
  53. struct {
  54. unsigned long pgoff;
  55. struct inode *inode;
  56. int offset;
  57. } shared;
  58. struct {
  59. unsigned long uaddr;
  60. struct mm_struct *mm;
  61. int offset;
  62. } private;
  63. struct {
  64. unsigned long word;
  65. void *ptr;
  66. int offset;
  67. } both;
  68. };
  69. /*
  70. * We use this hashed waitqueue instead of a normal wait_queue_t, so
  71. * we can wake only the relevant ones (hashed queues may be shared).
  72. *
  73. * A futex_q has a woken state, just like tasks have TASK_RUNNING.
  74. * It is considered woken when list_empty(&q->list) || q->lock_ptr == 0.
  75. * The order of wakup is always to make the first condition true, then
  76. * wake up q->waiters, then make the second condition true.
  77. */
  78. struct futex_q {
  79. struct list_head list;
  80. wait_queue_head_t waiters;
  81. /* Which hash list lock to use. */
  82. spinlock_t *lock_ptr;
  83. /* Key which the futex is hashed on. */
  84. union futex_key key;
  85. /* For fd, sigio sent using these. */
  86. int fd;
  87. struct file *filp;
  88. };
  89. /*
  90. * Split the global futex_lock into every hash list lock.
  91. */
  92. struct futex_hash_bucket {
  93. spinlock_t lock;
  94. struct list_head chain;
  95. };
  96. static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
  97. /* Futex-fs vfsmount entry: */
  98. static struct vfsmount *futex_mnt;
  99. /*
  100. * We hash on the keys returned from get_futex_key (see below).
  101. */
  102. static struct futex_hash_bucket *hash_futex(union futex_key *key)
  103. {
  104. u32 hash = jhash2((u32*)&key->both.word,
  105. (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
  106. key->both.offset);
  107. return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
  108. }
  109. /*
  110. * Return 1 if two futex_keys are equal, 0 otherwise.
  111. */
  112. static inline int match_futex(union futex_key *key1, union futex_key *key2)
  113. {
  114. return (key1->both.word == key2->both.word
  115. && key1->both.ptr == key2->both.ptr
  116. && key1->both.offset == key2->both.offset);
  117. }
  118. /*
  119. * Get parameters which are the keys for a futex.
  120. *
  121. * For shared mappings, it's (page->index, vma->vm_file->f_dentry->d_inode,
  122. * offset_within_page). For private mappings, it's (uaddr, current->mm).
  123. * We can usually work out the index without swapping in the page.
  124. *
  125. * Returns: 0, or negative error code.
  126. * The key words are stored in *key on success.
  127. *
  128. * Should be called with &current->mm->mmap_sem but NOT any spinlocks.
  129. */
  130. static int get_futex_key(unsigned long uaddr, union futex_key *key)
  131. {
  132. struct mm_struct *mm = current->mm;
  133. struct vm_area_struct *vma;
  134. struct page *page;
  135. int err;
  136. /*
  137. * The futex address must be "naturally" aligned.
  138. */
  139. key->both.offset = uaddr % PAGE_SIZE;
  140. if (unlikely((key->both.offset % sizeof(u32)) != 0))
  141. return -EINVAL;
  142. uaddr -= key->both.offset;
  143. /*
  144. * The futex is hashed differently depending on whether
  145. * it's in a shared or private mapping. So check vma first.
  146. */
  147. vma = find_extend_vma(mm, uaddr);
  148. if (unlikely(!vma))
  149. return -EFAULT;
  150. /*
  151. * Permissions.
  152. */
  153. if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
  154. return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
  155. /*
  156. * Private mappings are handled in a simple way.
  157. *
  158. * NOTE: When userspace waits on a MAP_SHARED mapping, even if
  159. * it's a read-only handle, it's expected that futexes attach to
  160. * the object not the particular process. Therefore we use
  161. * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
  162. * mappings of _writable_ handles.
  163. */
  164. if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
  165. key->private.mm = mm;
  166. key->private.uaddr = uaddr;
  167. return 0;
  168. }
  169. /*
  170. * Linear file mappings are also simple.
  171. */
  172. key->shared.inode = vma->vm_file->f_dentry->d_inode;
  173. key->both.offset++; /* Bit 0 of offset indicates inode-based key. */
  174. if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
  175. key->shared.pgoff = (((uaddr - vma->vm_start) >> PAGE_SHIFT)
  176. + vma->vm_pgoff);
  177. return 0;
  178. }
  179. /*
  180. * We could walk the page table to read the non-linear
  181. * pte, and get the page index without fetching the page
  182. * from swap. But that's a lot of code to duplicate here
  183. * for a rare case, so we simply fetch the page.
  184. */
  185. /*
  186. * Do a quick atomic lookup first - this is the fastpath.
  187. */
  188. spin_lock(&current->mm->page_table_lock);
  189. page = follow_page(mm, uaddr, 0);
  190. if (likely(page != NULL)) {
  191. key->shared.pgoff =
  192. page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  193. spin_unlock(&current->mm->page_table_lock);
  194. return 0;
  195. }
  196. spin_unlock(&current->mm->page_table_lock);
  197. /*
  198. * Do it the general way.
  199. */
  200. err = get_user_pages(current, mm, uaddr, 1, 0, 0, &page, NULL);
  201. if (err >= 0) {
  202. key->shared.pgoff =
  203. page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  204. put_page(page);
  205. return 0;
  206. }
  207. return err;
  208. }
  209. /*
  210. * Take a reference to the resource addressed by a key.
  211. * Can be called while holding spinlocks.
  212. *
  213. * NOTE: mmap_sem MUST be held between get_futex_key() and calling this
  214. * function, if it is called at all. mmap_sem keeps key->shared.inode valid.
  215. */
  216. static inline void get_key_refs(union futex_key *key)
  217. {
  218. if (key->both.ptr != 0) {
  219. if (key->both.offset & 1)
  220. atomic_inc(&key->shared.inode->i_count);
  221. else
  222. atomic_inc(&key->private.mm->mm_count);
  223. }
  224. }
  225. /*
  226. * Drop a reference to the resource addressed by a key.
  227. * The hash bucket spinlock must not be held.
  228. */
  229. static void drop_key_refs(union futex_key *key)
  230. {
  231. if (key->both.ptr != 0) {
  232. if (key->both.offset & 1)
  233. iput(key->shared.inode);
  234. else
  235. mmdrop(key->private.mm);
  236. }
  237. }
  238. static inline int get_futex_value_locked(int *dest, int __user *from)
  239. {
  240. int ret;
  241. inc_preempt_count();
  242. ret = __copy_from_user_inatomic(dest, from, sizeof(int));
  243. dec_preempt_count();
  244. return ret ? -EFAULT : 0;
  245. }
  246. /*
  247. * The hash bucket lock must be held when this is called.
  248. * Afterwards, the futex_q must not be accessed.
  249. */
  250. static void wake_futex(struct futex_q *q)
  251. {
  252. list_del_init(&q->list);
  253. if (q->filp)
  254. send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
  255. /*
  256. * The lock in wake_up_all() is a crucial memory barrier after the
  257. * list_del_init() and also before assigning to q->lock_ptr.
  258. */
  259. wake_up_all(&q->waiters);
  260. /*
  261. * The waiting task can free the futex_q as soon as this is written,
  262. * without taking any locks. This must come last.
  263. */
  264. q->lock_ptr = NULL;
  265. }
  266. /*
  267. * Wake up all waiters hashed on the physical page that is mapped
  268. * to this virtual address:
  269. */
  270. static int futex_wake(unsigned long uaddr, int nr_wake)
  271. {
  272. union futex_key key;
  273. struct futex_hash_bucket *bh;
  274. struct list_head *head;
  275. struct futex_q *this, *next;
  276. int ret;
  277. down_read(&current->mm->mmap_sem);
  278. ret = get_futex_key(uaddr, &key);
  279. if (unlikely(ret != 0))
  280. goto out;
  281. bh = hash_futex(&key);
  282. spin_lock(&bh->lock);
  283. head = &bh->chain;
  284. list_for_each_entry_safe(this, next, head, list) {
  285. if (match_futex (&this->key, &key)) {
  286. wake_futex(this);
  287. if (++ret >= nr_wake)
  288. break;
  289. }
  290. }
  291. spin_unlock(&bh->lock);
  292. out:
  293. up_read(&current->mm->mmap_sem);
  294. return ret;
  295. }
  296. /*
  297. * Requeue all waiters hashed on one physical page to another
  298. * physical page.
  299. */
  300. static int futex_requeue(unsigned long uaddr1, unsigned long uaddr2,
  301. int nr_wake, int nr_requeue, int *valp)
  302. {
  303. union futex_key key1, key2;
  304. struct futex_hash_bucket *bh1, *bh2;
  305. struct list_head *head1;
  306. struct futex_q *this, *next;
  307. int ret, drop_count = 0;
  308. retry:
  309. down_read(&current->mm->mmap_sem);
  310. ret = get_futex_key(uaddr1, &key1);
  311. if (unlikely(ret != 0))
  312. goto out;
  313. ret = get_futex_key(uaddr2, &key2);
  314. if (unlikely(ret != 0))
  315. goto out;
  316. bh1 = hash_futex(&key1);
  317. bh2 = hash_futex(&key2);
  318. if (bh1 < bh2)
  319. spin_lock(&bh1->lock);
  320. spin_lock(&bh2->lock);
  321. if (bh1 > bh2)
  322. spin_lock(&bh1->lock);
  323. if (likely(valp != NULL)) {
  324. int curval;
  325. ret = get_futex_value_locked(&curval, (int __user *)uaddr1);
  326. if (unlikely(ret)) {
  327. spin_unlock(&bh1->lock);
  328. if (bh1 != bh2)
  329. spin_unlock(&bh2->lock);
  330. /* If we would have faulted, release mmap_sem, fault
  331. * it in and start all over again.
  332. */
  333. up_read(&current->mm->mmap_sem);
  334. ret = get_user(curval, (int __user *)uaddr1);
  335. if (!ret)
  336. goto retry;
  337. return ret;
  338. }
  339. if (curval != *valp) {
  340. ret = -EAGAIN;
  341. goto out_unlock;
  342. }
  343. }
  344. head1 = &bh1->chain;
  345. list_for_each_entry_safe(this, next, head1, list) {
  346. if (!match_futex (&this->key, &key1))
  347. continue;
  348. if (++ret <= nr_wake) {
  349. wake_futex(this);
  350. } else {
  351. list_move_tail(&this->list, &bh2->chain);
  352. this->lock_ptr = &bh2->lock;
  353. this->key = key2;
  354. get_key_refs(&key2);
  355. drop_count++;
  356. if (ret - nr_wake >= nr_requeue)
  357. break;
  358. /* Make sure to stop if key1 == key2 */
  359. if (head1 == &bh2->chain && head1 != &next->list)
  360. head1 = &this->list;
  361. }
  362. }
  363. out_unlock:
  364. spin_unlock(&bh1->lock);
  365. if (bh1 != bh2)
  366. spin_unlock(&bh2->lock);
  367. /* drop_key_refs() must be called outside the spinlocks. */
  368. while (--drop_count >= 0)
  369. drop_key_refs(&key1);
  370. out:
  371. up_read(&current->mm->mmap_sem);
  372. return ret;
  373. }
  374. /* The key must be already stored in q->key. */
  375. static inline struct futex_hash_bucket *
  376. queue_lock(struct futex_q *q, int fd, struct file *filp)
  377. {
  378. struct futex_hash_bucket *bh;
  379. q->fd = fd;
  380. q->filp = filp;
  381. init_waitqueue_head(&q->waiters);
  382. get_key_refs(&q->key);
  383. bh = hash_futex(&q->key);
  384. q->lock_ptr = &bh->lock;
  385. spin_lock(&bh->lock);
  386. return bh;
  387. }
  388. static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *bh)
  389. {
  390. list_add_tail(&q->list, &bh->chain);
  391. spin_unlock(&bh->lock);
  392. }
  393. static inline void
  394. queue_unlock(struct futex_q *q, struct futex_hash_bucket *bh)
  395. {
  396. spin_unlock(&bh->lock);
  397. drop_key_refs(&q->key);
  398. }
  399. /*
  400. * queue_me and unqueue_me must be called as a pair, each
  401. * exactly once. They are called with the hashed spinlock held.
  402. */
  403. /* The key must be already stored in q->key. */
  404. static void queue_me(struct futex_q *q, int fd, struct file *filp)
  405. {
  406. struct futex_hash_bucket *bh;
  407. bh = queue_lock(q, fd, filp);
  408. __queue_me(q, bh);
  409. }
  410. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  411. static int unqueue_me(struct futex_q *q)
  412. {
  413. int ret = 0;
  414. spinlock_t *lock_ptr;
  415. /* In the common case we don't take the spinlock, which is nice. */
  416. retry:
  417. lock_ptr = q->lock_ptr;
  418. if (lock_ptr != 0) {
  419. spin_lock(lock_ptr);
  420. /*
  421. * q->lock_ptr can change between reading it and
  422. * spin_lock(), causing us to take the wrong lock. This
  423. * corrects the race condition.
  424. *
  425. * Reasoning goes like this: if we have the wrong lock,
  426. * q->lock_ptr must have changed (maybe several times)
  427. * between reading it and the spin_lock(). It can
  428. * change again after the spin_lock() but only if it was
  429. * already changed before the spin_lock(). It cannot,
  430. * however, change back to the original value. Therefore
  431. * we can detect whether we acquired the correct lock.
  432. */
  433. if (unlikely(lock_ptr != q->lock_ptr)) {
  434. spin_unlock(lock_ptr);
  435. goto retry;
  436. }
  437. WARN_ON(list_empty(&q->list));
  438. list_del(&q->list);
  439. spin_unlock(lock_ptr);
  440. ret = 1;
  441. }
  442. drop_key_refs(&q->key);
  443. return ret;
  444. }
  445. static int futex_wait(unsigned long uaddr, int val, unsigned long time)
  446. {
  447. DECLARE_WAITQUEUE(wait, current);
  448. int ret, curval;
  449. struct futex_q q;
  450. struct futex_hash_bucket *bh;
  451. retry:
  452. down_read(&current->mm->mmap_sem);
  453. ret = get_futex_key(uaddr, &q.key);
  454. if (unlikely(ret != 0))
  455. goto out_release_sem;
  456. bh = queue_lock(&q, -1, NULL);
  457. /*
  458. * Access the page AFTER the futex is queued.
  459. * Order is important:
  460. *
  461. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  462. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  463. *
  464. * The basic logical guarantee of a futex is that it blocks ONLY
  465. * if cond(var) is known to be true at the time of blocking, for
  466. * any cond. If we queued after testing *uaddr, that would open
  467. * a race condition where we could block indefinitely with
  468. * cond(var) false, which would violate the guarantee.
  469. *
  470. * A consequence is that futex_wait() can return zero and absorb
  471. * a wakeup when *uaddr != val on entry to the syscall. This is
  472. * rare, but normal.
  473. *
  474. * We hold the mmap semaphore, so the mapping cannot have changed
  475. * since we looked it up in get_futex_key.
  476. */
  477. ret = get_futex_value_locked(&curval, (int __user *)uaddr);
  478. if (unlikely(ret)) {
  479. queue_unlock(&q, bh);
  480. /* If we would have faulted, release mmap_sem, fault it in and
  481. * start all over again.
  482. */
  483. up_read(&current->mm->mmap_sem);
  484. ret = get_user(curval, (int __user *)uaddr);
  485. if (!ret)
  486. goto retry;
  487. return ret;
  488. }
  489. if (curval != val) {
  490. ret = -EWOULDBLOCK;
  491. queue_unlock(&q, bh);
  492. goto out_release_sem;
  493. }
  494. /* Only actually queue if *uaddr contained val. */
  495. __queue_me(&q, bh);
  496. /*
  497. * Now the futex is queued and we have checked the data, we
  498. * don't want to hold mmap_sem while we sleep.
  499. */
  500. up_read(&current->mm->mmap_sem);
  501. /*
  502. * There might have been scheduling since the queue_me(), as we
  503. * cannot hold a spinlock across the get_user() in case it
  504. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  505. * queueing ourselves into the futex hash. This code thus has to
  506. * rely on the futex_wake() code removing us from hash when it
  507. * wakes us up.
  508. */
  509. /* add_wait_queue is the barrier after __set_current_state. */
  510. __set_current_state(TASK_INTERRUPTIBLE);
  511. add_wait_queue(&q.waiters, &wait);
  512. /*
  513. * !list_empty() is safe here without any lock.
  514. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  515. */
  516. if (likely(!list_empty(&q.list)))
  517. time = schedule_timeout(time);
  518. __set_current_state(TASK_RUNNING);
  519. /*
  520. * NOTE: we don't remove ourselves from the waitqueue because
  521. * we are the only user of it.
  522. */
  523. /* If we were woken (and unqueued), we succeeded, whatever. */
  524. if (!unqueue_me(&q))
  525. return 0;
  526. if (time == 0)
  527. return -ETIMEDOUT;
  528. /* We expect signal_pending(current), but another thread may
  529. * have handled it for us already. */
  530. return -EINTR;
  531. out_release_sem:
  532. up_read(&current->mm->mmap_sem);
  533. return ret;
  534. }
  535. static int futex_close(struct inode *inode, struct file *filp)
  536. {
  537. struct futex_q *q = filp->private_data;
  538. unqueue_me(q);
  539. kfree(q);
  540. return 0;
  541. }
  542. /* This is one-shot: once it's gone off you need a new fd */
  543. static unsigned int futex_poll(struct file *filp,
  544. struct poll_table_struct *wait)
  545. {
  546. struct futex_q *q = filp->private_data;
  547. int ret = 0;
  548. poll_wait(filp, &q->waiters, wait);
  549. /*
  550. * list_empty() is safe here without any lock.
  551. * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
  552. */
  553. if (list_empty(&q->list))
  554. ret = POLLIN | POLLRDNORM;
  555. return ret;
  556. }
  557. static struct file_operations futex_fops = {
  558. .release = futex_close,
  559. .poll = futex_poll,
  560. };
  561. /*
  562. * Signal allows caller to avoid the race which would occur if they
  563. * set the sigio stuff up afterwards.
  564. */
  565. static int futex_fd(unsigned long uaddr, int signal)
  566. {
  567. struct futex_q *q;
  568. struct file *filp;
  569. int ret, err;
  570. ret = -EINVAL;
  571. if (!valid_signal(signal))
  572. goto out;
  573. ret = get_unused_fd();
  574. if (ret < 0)
  575. goto out;
  576. filp = get_empty_filp();
  577. if (!filp) {
  578. put_unused_fd(ret);
  579. ret = -ENFILE;
  580. goto out;
  581. }
  582. filp->f_op = &futex_fops;
  583. filp->f_vfsmnt = mntget(futex_mnt);
  584. filp->f_dentry = dget(futex_mnt->mnt_root);
  585. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  586. if (signal) {
  587. int err;
  588. err = f_setown(filp, current->pid, 1);
  589. if (err < 0) {
  590. put_unused_fd(ret);
  591. put_filp(filp);
  592. ret = err;
  593. goto out;
  594. }
  595. filp->f_owner.signum = signal;
  596. }
  597. q = kmalloc(sizeof(*q), GFP_KERNEL);
  598. if (!q) {
  599. put_unused_fd(ret);
  600. put_filp(filp);
  601. ret = -ENOMEM;
  602. goto out;
  603. }
  604. down_read(&current->mm->mmap_sem);
  605. err = get_futex_key(uaddr, &q->key);
  606. if (unlikely(err != 0)) {
  607. up_read(&current->mm->mmap_sem);
  608. put_unused_fd(ret);
  609. put_filp(filp);
  610. kfree(q);
  611. return err;
  612. }
  613. /*
  614. * queue_me() must be called before releasing mmap_sem, because
  615. * key->shared.inode needs to be referenced while holding it.
  616. */
  617. filp->private_data = q;
  618. queue_me(q, ret, filp);
  619. up_read(&current->mm->mmap_sem);
  620. /* Now we map fd to filp, so userspace can access it */
  621. fd_install(ret, filp);
  622. out:
  623. return ret;
  624. }
  625. long do_futex(unsigned long uaddr, int op, int val, unsigned long timeout,
  626. unsigned long uaddr2, int val2, int val3)
  627. {
  628. int ret;
  629. switch (op) {
  630. case FUTEX_WAIT:
  631. ret = futex_wait(uaddr, val, timeout);
  632. break;
  633. case FUTEX_WAKE:
  634. ret = futex_wake(uaddr, val);
  635. break;
  636. case FUTEX_FD:
  637. /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
  638. ret = futex_fd(uaddr, val);
  639. break;
  640. case FUTEX_REQUEUE:
  641. ret = futex_requeue(uaddr, uaddr2, val, val2, NULL);
  642. break;
  643. case FUTEX_CMP_REQUEUE:
  644. ret = futex_requeue(uaddr, uaddr2, val, val2, &val3);
  645. break;
  646. default:
  647. ret = -ENOSYS;
  648. }
  649. return ret;
  650. }
  651. asmlinkage long sys_futex(u32 __user *uaddr, int op, int val,
  652. struct timespec __user *utime, u32 __user *uaddr2,
  653. int val3)
  654. {
  655. struct timespec t;
  656. unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
  657. int val2 = 0;
  658. if ((op == FUTEX_WAIT) && utime) {
  659. if (copy_from_user(&t, utime, sizeof(t)) != 0)
  660. return -EFAULT;
  661. timeout = timespec_to_jiffies(&t) + 1;
  662. }
  663. /*
  664. * requeue parameter in 'utime' if op == FUTEX_REQUEUE.
  665. */
  666. if (op >= FUTEX_REQUEUE)
  667. val2 = (int) (unsigned long) utime;
  668. return do_futex((unsigned long)uaddr, op, val, timeout,
  669. (unsigned long)uaddr2, val2, val3);
  670. }
  671. static struct super_block *
  672. futexfs_get_sb(struct file_system_type *fs_type,
  673. int flags, const char *dev_name, void *data)
  674. {
  675. return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA);
  676. }
  677. static struct file_system_type futex_fs_type = {
  678. .name = "futexfs",
  679. .get_sb = futexfs_get_sb,
  680. .kill_sb = kill_anon_super,
  681. };
  682. static int __init init(void)
  683. {
  684. unsigned int i;
  685. register_filesystem(&futex_fs_type);
  686. futex_mnt = kern_mount(&futex_fs_type);
  687. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  688. INIT_LIST_HEAD(&futex_queues[i].chain);
  689. spin_lock_init(&futex_queues[i].lock);
  690. }
  691. return 0;
  692. }
  693. __initcall(init);