futex.c 26 KB

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