futex.c 26 KB

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