futex.c 19 KB

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