futex.c 22 KB

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