futex.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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. * PI-futex support started by Ingo Molnar and Thomas Gleixner
  16. * Copyright (C) 2006 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  17. * Copyright (C) 2006 Timesys Corp., Thomas Gleixner <tglx@timesys.com>
  18. *
  19. * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
  20. * enough at me, Linus for the original (flawed) idea, Matthew
  21. * Kirkwood for proof-of-concept implementation.
  22. *
  23. * "The futexes are also cursed."
  24. * "But they come in a choice of three flavours!"
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. #include <linux/slab.h>
  41. #include <linux/poll.h>
  42. #include <linux/fs.h>
  43. #include <linux/file.h>
  44. #include <linux/jhash.h>
  45. #include <linux/init.h>
  46. #include <linux/futex.h>
  47. #include <linux/mount.h>
  48. #include <linux/pagemap.h>
  49. #include <linux/syscalls.h>
  50. #include <linux/signal.h>
  51. #include <asm/futex.h>
  52. #include "rtmutex_common.h"
  53. #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
  54. /*
  55. * Futexes are matched on equal values of this key.
  56. * The key type depends on whether it's a shared or private mapping.
  57. * Don't rearrange members without looking at hash_futex().
  58. *
  59. * offset is aligned to a multiple of sizeof(u32) (== 4) by definition.
  60. * We set bit 0 to indicate if it's an inode-based key.
  61. */
  62. union futex_key {
  63. struct {
  64. unsigned long pgoff;
  65. struct inode *inode;
  66. int offset;
  67. } shared;
  68. struct {
  69. unsigned long address;
  70. struct mm_struct *mm;
  71. int offset;
  72. } private;
  73. struct {
  74. unsigned long word;
  75. void *ptr;
  76. int offset;
  77. } both;
  78. };
  79. /*
  80. * Priority Inheritance state:
  81. */
  82. struct futex_pi_state {
  83. /*
  84. * list of 'owned' pi_state instances - these have to be
  85. * cleaned up in do_exit() if the task exits prematurely:
  86. */
  87. struct list_head list;
  88. /*
  89. * The PI object:
  90. */
  91. struct rt_mutex pi_mutex;
  92. struct task_struct *owner;
  93. atomic_t refcount;
  94. union futex_key key;
  95. };
  96. /*
  97. * We use this hashed waitqueue instead of a normal wait_queue_t, so
  98. * we can wake only the relevant ones (hashed queues may be shared).
  99. *
  100. * A futex_q has a woken state, just like tasks have TASK_RUNNING.
  101. * It is considered woken when list_empty(&q->list) || q->lock_ptr == 0.
  102. * The order of wakup is always to make the first condition true, then
  103. * wake up q->waiters, then make the second condition true.
  104. */
  105. struct futex_q {
  106. struct list_head list;
  107. wait_queue_head_t waiters;
  108. /* Which hash list lock to use: */
  109. spinlock_t *lock_ptr;
  110. /* Key which the futex is hashed on: */
  111. union futex_key key;
  112. /* For fd, sigio sent using these: */
  113. int fd;
  114. struct file *filp;
  115. /* Optional priority inheritance state: */
  116. struct futex_pi_state *pi_state;
  117. struct task_struct *task;
  118. };
  119. /*
  120. * Split the global futex_lock into every hash list lock.
  121. */
  122. struct futex_hash_bucket {
  123. spinlock_t lock;
  124. struct list_head chain;
  125. };
  126. static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
  127. /* Futex-fs vfsmount entry: */
  128. static struct vfsmount *futex_mnt;
  129. /*
  130. * We hash on the keys returned from get_futex_key (see below).
  131. */
  132. static struct futex_hash_bucket *hash_futex(union futex_key *key)
  133. {
  134. u32 hash = jhash2((u32*)&key->both.word,
  135. (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
  136. key->both.offset);
  137. return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
  138. }
  139. /*
  140. * Return 1 if two futex_keys are equal, 0 otherwise.
  141. */
  142. static inline int match_futex(union futex_key *key1, union futex_key *key2)
  143. {
  144. return (key1->both.word == key2->both.word
  145. && key1->both.ptr == key2->both.ptr
  146. && key1->both.offset == key2->both.offset);
  147. }
  148. /*
  149. * Get parameters which are the keys for a futex.
  150. *
  151. * For shared mappings, it's (page->index, vma->vm_file->f_dentry->d_inode,
  152. * offset_within_page). For private mappings, it's (uaddr, current->mm).
  153. * We can usually work out the index without swapping in the page.
  154. *
  155. * Returns: 0, or negative error code.
  156. * The key words are stored in *key on success.
  157. *
  158. * Should be called with &current->mm->mmap_sem but NOT any spinlocks.
  159. */
  160. static int get_futex_key(u32 __user *uaddr, union futex_key *key)
  161. {
  162. unsigned long address = (unsigned long)uaddr;
  163. struct mm_struct *mm = current->mm;
  164. struct vm_area_struct *vma;
  165. struct page *page;
  166. int err;
  167. /*
  168. * The futex address must be "naturally" aligned.
  169. */
  170. key->both.offset = address % PAGE_SIZE;
  171. if (unlikely((key->both.offset % sizeof(u32)) != 0))
  172. return -EINVAL;
  173. address -= key->both.offset;
  174. /*
  175. * The futex is hashed differently depending on whether
  176. * it's in a shared or private mapping. So check vma first.
  177. */
  178. vma = find_extend_vma(mm, address);
  179. if (unlikely(!vma))
  180. return -EFAULT;
  181. /*
  182. * Permissions.
  183. */
  184. if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
  185. return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
  186. /*
  187. * Private mappings are handled in a simple way.
  188. *
  189. * NOTE: When userspace waits on a MAP_SHARED mapping, even if
  190. * it's a read-only handle, it's expected that futexes attach to
  191. * the object not the particular process. Therefore we use
  192. * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
  193. * mappings of _writable_ handles.
  194. */
  195. if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
  196. key->private.mm = mm;
  197. key->private.address = address;
  198. return 0;
  199. }
  200. /*
  201. * Linear file mappings are also simple.
  202. */
  203. key->shared.inode = vma->vm_file->f_dentry->d_inode;
  204. key->both.offset++; /* Bit 0 of offset indicates inode-based key. */
  205. if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
  206. key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
  207. + vma->vm_pgoff);
  208. return 0;
  209. }
  210. /*
  211. * We could walk the page table to read the non-linear
  212. * pte, and get the page index without fetching the page
  213. * from swap. But that's a lot of code to duplicate here
  214. * for a rare case, so we simply fetch the page.
  215. */
  216. err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
  217. if (err >= 0) {
  218. key->shared.pgoff =
  219. page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  220. put_page(page);
  221. return 0;
  222. }
  223. return err;
  224. }
  225. /*
  226. * Take a reference to the resource addressed by a key.
  227. * Can be called while holding spinlocks.
  228. *
  229. * NOTE: mmap_sem MUST be held between get_futex_key() and calling this
  230. * function, if it is called at all. mmap_sem keeps key->shared.inode valid.
  231. */
  232. static inline void get_key_refs(union futex_key *key)
  233. {
  234. if (key->both.ptr != 0) {
  235. if (key->both.offset & 1)
  236. atomic_inc(&key->shared.inode->i_count);
  237. else
  238. atomic_inc(&key->private.mm->mm_count);
  239. }
  240. }
  241. /*
  242. * Drop a reference to the resource addressed by a key.
  243. * The hash bucket spinlock must not be held.
  244. */
  245. static void drop_key_refs(union futex_key *key)
  246. {
  247. if (key->both.ptr != 0) {
  248. if (key->both.offset & 1)
  249. iput(key->shared.inode);
  250. else
  251. mmdrop(key->private.mm);
  252. }
  253. }
  254. static inline int get_futex_value_locked(u32 *dest, u32 __user *from)
  255. {
  256. int ret;
  257. inc_preempt_count();
  258. ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
  259. dec_preempt_count();
  260. return ret ? -EFAULT : 0;
  261. }
  262. /*
  263. * Fault handling. Called with current->mm->mmap_sem held.
  264. */
  265. static int futex_handle_fault(unsigned long address, int attempt)
  266. {
  267. struct vm_area_struct * vma;
  268. struct mm_struct *mm = current->mm;
  269. if (attempt >= 2 || !(vma = find_vma(mm, address)) ||
  270. vma->vm_start > address || !(vma->vm_flags & VM_WRITE))
  271. return -EFAULT;
  272. switch (handle_mm_fault(mm, vma, address, 1)) {
  273. case VM_FAULT_MINOR:
  274. current->min_flt++;
  275. break;
  276. case VM_FAULT_MAJOR:
  277. current->maj_flt++;
  278. break;
  279. default:
  280. return -EFAULT;
  281. }
  282. return 0;
  283. }
  284. /*
  285. * PI code:
  286. */
  287. static int refill_pi_state_cache(void)
  288. {
  289. struct futex_pi_state *pi_state;
  290. if (likely(current->pi_state_cache))
  291. return 0;
  292. pi_state = kmalloc(sizeof(*pi_state), GFP_KERNEL);
  293. if (!pi_state)
  294. return -ENOMEM;
  295. memset(pi_state, 0, sizeof(*pi_state));
  296. INIT_LIST_HEAD(&pi_state->list);
  297. /* pi_mutex gets initialized later */
  298. pi_state->owner = NULL;
  299. atomic_set(&pi_state->refcount, 1);
  300. current->pi_state_cache = pi_state;
  301. return 0;
  302. }
  303. static struct futex_pi_state * alloc_pi_state(void)
  304. {
  305. struct futex_pi_state *pi_state = current->pi_state_cache;
  306. WARN_ON(!pi_state);
  307. current->pi_state_cache = NULL;
  308. return pi_state;
  309. }
  310. static void free_pi_state(struct futex_pi_state *pi_state)
  311. {
  312. if (!atomic_dec_and_test(&pi_state->refcount))
  313. return;
  314. /*
  315. * If pi_state->owner is NULL, the owner is most probably dying
  316. * and has cleaned up the pi_state already
  317. */
  318. if (pi_state->owner) {
  319. spin_lock_irq(&pi_state->owner->pi_lock);
  320. list_del_init(&pi_state->list);
  321. spin_unlock_irq(&pi_state->owner->pi_lock);
  322. rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
  323. }
  324. if (current->pi_state_cache)
  325. kfree(pi_state);
  326. else {
  327. /*
  328. * pi_state->list is already empty.
  329. * clear pi_state->owner.
  330. * refcount is at 0 - put it back to 1.
  331. */
  332. pi_state->owner = NULL;
  333. atomic_set(&pi_state->refcount, 1);
  334. current->pi_state_cache = pi_state;
  335. }
  336. }
  337. /*
  338. * Look up the task based on what TID userspace gave us.
  339. * We dont trust it.
  340. */
  341. static struct task_struct * futex_find_get_task(pid_t pid)
  342. {
  343. struct task_struct *p;
  344. read_lock(&tasklist_lock);
  345. p = find_task_by_pid(pid);
  346. if (!p)
  347. goto out_unlock;
  348. if ((current->euid != p->euid) && (current->euid != p->uid)) {
  349. p = NULL;
  350. goto out_unlock;
  351. }
  352. if (p->state == EXIT_ZOMBIE || p->exit_state == EXIT_ZOMBIE) {
  353. p = NULL;
  354. goto out_unlock;
  355. }
  356. get_task_struct(p);
  357. out_unlock:
  358. read_unlock(&tasklist_lock);
  359. return p;
  360. }
  361. /*
  362. * This task is holding PI mutexes at exit time => bad.
  363. * Kernel cleans up PI-state, but userspace is likely hosed.
  364. * (Robust-futex cleanup is separate and might save the day for userspace.)
  365. */
  366. void exit_pi_state_list(struct task_struct *curr)
  367. {
  368. struct futex_hash_bucket *hb;
  369. struct list_head *next, *head = &curr->pi_state_list;
  370. struct futex_pi_state *pi_state;
  371. union futex_key key;
  372. /*
  373. * We are a ZOMBIE and nobody can enqueue itself on
  374. * pi_state_list anymore, but we have to be careful
  375. * versus waiters unqueueing themselfs
  376. */
  377. spin_lock_irq(&curr->pi_lock);
  378. while (!list_empty(head)) {
  379. next = head->next;
  380. pi_state = list_entry(next, struct futex_pi_state, list);
  381. key = pi_state->key;
  382. spin_unlock_irq(&curr->pi_lock);
  383. hb = hash_futex(&key);
  384. spin_lock(&hb->lock);
  385. spin_lock_irq(&curr->pi_lock);
  386. if (head->next != next) {
  387. spin_unlock(&hb->lock);
  388. continue;
  389. }
  390. list_del_init(&pi_state->list);
  391. WARN_ON(pi_state->owner != curr);
  392. pi_state->owner = NULL;
  393. spin_unlock_irq(&curr->pi_lock);
  394. rt_mutex_unlock(&pi_state->pi_mutex);
  395. spin_unlock(&hb->lock);
  396. spin_lock_irq(&curr->pi_lock);
  397. }
  398. spin_unlock_irq(&curr->pi_lock);
  399. }
  400. static int
  401. lookup_pi_state(u32 uval, struct futex_hash_bucket *hb, struct futex_q *me)
  402. {
  403. struct futex_pi_state *pi_state = NULL;
  404. struct futex_q *this, *next;
  405. struct list_head *head;
  406. struct task_struct *p;
  407. pid_t pid;
  408. head = &hb->chain;
  409. list_for_each_entry_safe(this, next, head, list) {
  410. if (match_futex (&this->key, &me->key)) {
  411. /*
  412. * Another waiter already exists - bump up
  413. * the refcount and return its pi_state:
  414. */
  415. pi_state = this->pi_state;
  416. /*
  417. * Userspace might have messed up non PI and PI futexes
  418. */
  419. if (unlikely(!pi_state))
  420. return -EINVAL;
  421. atomic_inc(&pi_state->refcount);
  422. me->pi_state = pi_state;
  423. return 0;
  424. }
  425. }
  426. /*
  427. * We are the first waiter - try to look up the real owner and
  428. * attach the new pi_state to it:
  429. */
  430. pid = uval & FUTEX_TID_MASK;
  431. p = futex_find_get_task(pid);
  432. if (!p)
  433. return -ESRCH;
  434. pi_state = alloc_pi_state();
  435. /*
  436. * Initialize the pi_mutex in locked state and make 'p'
  437. * the owner of it:
  438. */
  439. rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
  440. /* Store the key for possible exit cleanups: */
  441. pi_state->key = me->key;
  442. spin_lock_irq(&p->pi_lock);
  443. list_add(&pi_state->list, &p->pi_state_list);
  444. pi_state->owner = p;
  445. spin_unlock_irq(&p->pi_lock);
  446. put_task_struct(p);
  447. me->pi_state = pi_state;
  448. return 0;
  449. }
  450. /*
  451. * The hash bucket lock must be held when this is called.
  452. * Afterwards, the futex_q must not be accessed.
  453. */
  454. static void wake_futex(struct futex_q *q)
  455. {
  456. list_del_init(&q->list);
  457. if (q->filp)
  458. send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
  459. /*
  460. * The lock in wake_up_all() is a crucial memory barrier after the
  461. * list_del_init() and also before assigning to q->lock_ptr.
  462. */
  463. wake_up_all(&q->waiters);
  464. /*
  465. * The waiting task can free the futex_q as soon as this is written,
  466. * without taking any locks. This must come last.
  467. *
  468. * A memory barrier is required here to prevent the following store
  469. * to lock_ptr from getting ahead of the wakeup. Clearing the lock
  470. * at the end of wake_up_all() does not prevent this store from
  471. * moving.
  472. */
  473. wmb();
  474. q->lock_ptr = NULL;
  475. }
  476. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  477. {
  478. struct task_struct *new_owner;
  479. struct futex_pi_state *pi_state = this->pi_state;
  480. u32 curval, newval;
  481. if (!pi_state)
  482. return -EINVAL;
  483. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  484. /*
  485. * This happens when we have stolen the lock and the original
  486. * pending owner did not enqueue itself back on the rt_mutex.
  487. * Thats not a tragedy. We know that way, that a lock waiter
  488. * is on the fly. We make the futex_q waiter the pending owner.
  489. */
  490. if (!new_owner)
  491. new_owner = this->task;
  492. /*
  493. * We pass it to the next owner. (The WAITERS bit is always
  494. * kept enabled while there is PI state around. We must also
  495. * preserve the owner died bit.)
  496. */
  497. newval = (uval & FUTEX_OWNER_DIED) | FUTEX_WAITERS | new_owner->pid;
  498. inc_preempt_count();
  499. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  500. dec_preempt_count();
  501. if (curval == -EFAULT)
  502. return -EFAULT;
  503. if (curval != uval)
  504. return -EINVAL;
  505. list_del_init(&pi_state->owner->pi_state_list);
  506. list_add(&pi_state->list, &new_owner->pi_state_list);
  507. pi_state->owner = new_owner;
  508. rt_mutex_unlock(&pi_state->pi_mutex);
  509. return 0;
  510. }
  511. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  512. {
  513. u32 oldval;
  514. /*
  515. * There is no waiter, so we unlock the futex. The owner died
  516. * bit has not to be preserved here. We are the owner:
  517. */
  518. inc_preempt_count();
  519. oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
  520. dec_preempt_count();
  521. if (oldval == -EFAULT)
  522. return oldval;
  523. if (oldval != uval)
  524. return -EAGAIN;
  525. return 0;
  526. }
  527. /*
  528. * Express the locking dependencies for lockdep:
  529. */
  530. static inline void
  531. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  532. {
  533. if (hb1 <= hb2) {
  534. spin_lock(&hb1->lock);
  535. if (hb1 < hb2)
  536. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  537. } else { /* hb1 > hb2 */
  538. spin_lock(&hb2->lock);
  539. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  540. }
  541. }
  542. /*
  543. * Wake up all waiters hashed on the physical page that is mapped
  544. * to this virtual address:
  545. */
  546. static int futex_wake(u32 __user *uaddr, int nr_wake)
  547. {
  548. struct futex_hash_bucket *hb;
  549. struct futex_q *this, *next;
  550. struct list_head *head;
  551. union futex_key key;
  552. int ret;
  553. down_read(&current->mm->mmap_sem);
  554. ret = get_futex_key(uaddr, &key);
  555. if (unlikely(ret != 0))
  556. goto out;
  557. hb = hash_futex(&key);
  558. spin_lock(&hb->lock);
  559. head = &hb->chain;
  560. list_for_each_entry_safe(this, next, head, list) {
  561. if (match_futex (&this->key, &key)) {
  562. if (this->pi_state) {
  563. ret = -EINVAL;
  564. break;
  565. }
  566. wake_futex(this);
  567. if (++ret >= nr_wake)
  568. break;
  569. }
  570. }
  571. spin_unlock(&hb->lock);
  572. out:
  573. up_read(&current->mm->mmap_sem);
  574. return ret;
  575. }
  576. /*
  577. * Wake up all waiters hashed on the physical page that is mapped
  578. * to this virtual address:
  579. */
  580. static int
  581. futex_wake_op(u32 __user *uaddr1, u32 __user *uaddr2,
  582. int nr_wake, int nr_wake2, int op)
  583. {
  584. union futex_key key1, key2;
  585. struct futex_hash_bucket *hb1, *hb2;
  586. struct list_head *head;
  587. struct futex_q *this, *next;
  588. int ret, op_ret, attempt = 0;
  589. retryfull:
  590. down_read(&current->mm->mmap_sem);
  591. ret = get_futex_key(uaddr1, &key1);
  592. if (unlikely(ret != 0))
  593. goto out;
  594. ret = get_futex_key(uaddr2, &key2);
  595. if (unlikely(ret != 0))
  596. goto out;
  597. hb1 = hash_futex(&key1);
  598. hb2 = hash_futex(&key2);
  599. retry:
  600. double_lock_hb(hb1, hb2);
  601. op_ret = futex_atomic_op_inuser(op, uaddr2);
  602. if (unlikely(op_ret < 0)) {
  603. u32 dummy;
  604. spin_unlock(&hb1->lock);
  605. if (hb1 != hb2)
  606. spin_unlock(&hb2->lock);
  607. #ifndef CONFIG_MMU
  608. /*
  609. * we don't get EFAULT from MMU faults if we don't have an MMU,
  610. * but we might get them from range checking
  611. */
  612. ret = op_ret;
  613. goto out;
  614. #endif
  615. if (unlikely(op_ret != -EFAULT)) {
  616. ret = op_ret;
  617. goto out;
  618. }
  619. /*
  620. * futex_atomic_op_inuser needs to both read and write
  621. * *(int __user *)uaddr2, but we can't modify it
  622. * non-atomically. Therefore, if get_user below is not
  623. * enough, we need to handle the fault ourselves, while
  624. * still holding the mmap_sem.
  625. */
  626. if (attempt++) {
  627. if (futex_handle_fault((unsigned long)uaddr2,
  628. attempt))
  629. goto out;
  630. goto retry;
  631. }
  632. /*
  633. * If we would have faulted, release mmap_sem,
  634. * fault it in and start all over again.
  635. */
  636. up_read(&current->mm->mmap_sem);
  637. ret = get_user(dummy, uaddr2);
  638. if (ret)
  639. return ret;
  640. goto retryfull;
  641. }
  642. head = &hb1->chain;
  643. list_for_each_entry_safe(this, next, head, list) {
  644. if (match_futex (&this->key, &key1)) {
  645. wake_futex(this);
  646. if (++ret >= nr_wake)
  647. break;
  648. }
  649. }
  650. if (op_ret > 0) {
  651. head = &hb2->chain;
  652. op_ret = 0;
  653. list_for_each_entry_safe(this, next, head, list) {
  654. if (match_futex (&this->key, &key2)) {
  655. wake_futex(this);
  656. if (++op_ret >= nr_wake2)
  657. break;
  658. }
  659. }
  660. ret += op_ret;
  661. }
  662. spin_unlock(&hb1->lock);
  663. if (hb1 != hb2)
  664. spin_unlock(&hb2->lock);
  665. out:
  666. up_read(&current->mm->mmap_sem);
  667. return ret;
  668. }
  669. /*
  670. * Requeue all waiters hashed on one physical page to another
  671. * physical page.
  672. */
  673. static int futex_requeue(u32 __user *uaddr1, u32 __user *uaddr2,
  674. int nr_wake, int nr_requeue, u32 *cmpval)
  675. {
  676. union futex_key key1, key2;
  677. struct futex_hash_bucket *hb1, *hb2;
  678. struct list_head *head1;
  679. struct futex_q *this, *next;
  680. int ret, drop_count = 0;
  681. retry:
  682. down_read(&current->mm->mmap_sem);
  683. ret = get_futex_key(uaddr1, &key1);
  684. if (unlikely(ret != 0))
  685. goto out;
  686. ret = get_futex_key(uaddr2, &key2);
  687. if (unlikely(ret != 0))
  688. goto out;
  689. hb1 = hash_futex(&key1);
  690. hb2 = hash_futex(&key2);
  691. double_lock_hb(hb1, hb2);
  692. if (likely(cmpval != NULL)) {
  693. u32 curval;
  694. ret = get_futex_value_locked(&curval, uaddr1);
  695. if (unlikely(ret)) {
  696. spin_unlock(&hb1->lock);
  697. if (hb1 != hb2)
  698. spin_unlock(&hb2->lock);
  699. /*
  700. * If we would have faulted, release mmap_sem, fault
  701. * it in and start all over again.
  702. */
  703. up_read(&current->mm->mmap_sem);
  704. ret = get_user(curval, uaddr1);
  705. if (!ret)
  706. goto retry;
  707. return ret;
  708. }
  709. if (curval != *cmpval) {
  710. ret = -EAGAIN;
  711. goto out_unlock;
  712. }
  713. }
  714. head1 = &hb1->chain;
  715. list_for_each_entry_safe(this, next, head1, list) {
  716. if (!match_futex (&this->key, &key1))
  717. continue;
  718. if (++ret <= nr_wake) {
  719. wake_futex(this);
  720. } else {
  721. /*
  722. * If key1 and key2 hash to the same bucket, no need to
  723. * requeue.
  724. */
  725. if (likely(head1 != &hb2->chain)) {
  726. list_move_tail(&this->list, &hb2->chain);
  727. this->lock_ptr = &hb2->lock;
  728. }
  729. this->key = key2;
  730. get_key_refs(&key2);
  731. drop_count++;
  732. if (ret - nr_wake >= nr_requeue)
  733. break;
  734. }
  735. }
  736. out_unlock:
  737. spin_unlock(&hb1->lock);
  738. if (hb1 != hb2)
  739. spin_unlock(&hb2->lock);
  740. /* drop_key_refs() must be called outside the spinlocks. */
  741. while (--drop_count >= 0)
  742. drop_key_refs(&key1);
  743. out:
  744. up_read(&current->mm->mmap_sem);
  745. return ret;
  746. }
  747. /* The key must be already stored in q->key. */
  748. static inline struct futex_hash_bucket *
  749. queue_lock(struct futex_q *q, int fd, struct file *filp)
  750. {
  751. struct futex_hash_bucket *hb;
  752. q->fd = fd;
  753. q->filp = filp;
  754. init_waitqueue_head(&q->waiters);
  755. get_key_refs(&q->key);
  756. hb = hash_futex(&q->key);
  757. q->lock_ptr = &hb->lock;
  758. spin_lock(&hb->lock);
  759. return hb;
  760. }
  761. static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  762. {
  763. list_add_tail(&q->list, &hb->chain);
  764. q->task = current;
  765. spin_unlock(&hb->lock);
  766. }
  767. static inline void
  768. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  769. {
  770. spin_unlock(&hb->lock);
  771. drop_key_refs(&q->key);
  772. }
  773. /*
  774. * queue_me and unqueue_me must be called as a pair, each
  775. * exactly once. They are called with the hashed spinlock held.
  776. */
  777. /* The key must be already stored in q->key. */
  778. static void queue_me(struct futex_q *q, int fd, struct file *filp)
  779. {
  780. struct futex_hash_bucket *hb;
  781. hb = queue_lock(q, fd, filp);
  782. __queue_me(q, hb);
  783. }
  784. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  785. static int unqueue_me(struct futex_q *q)
  786. {
  787. spinlock_t *lock_ptr;
  788. int ret = 0;
  789. /* In the common case we don't take the spinlock, which is nice. */
  790. retry:
  791. lock_ptr = q->lock_ptr;
  792. if (lock_ptr != 0) {
  793. spin_lock(lock_ptr);
  794. /*
  795. * q->lock_ptr can change between reading it and
  796. * spin_lock(), causing us to take the wrong lock. This
  797. * corrects the race condition.
  798. *
  799. * Reasoning goes like this: if we have the wrong lock,
  800. * q->lock_ptr must have changed (maybe several times)
  801. * between reading it and the spin_lock(). It can
  802. * change again after the spin_lock() but only if it was
  803. * already changed before the spin_lock(). It cannot,
  804. * however, change back to the original value. Therefore
  805. * we can detect whether we acquired the correct lock.
  806. */
  807. if (unlikely(lock_ptr != q->lock_ptr)) {
  808. spin_unlock(lock_ptr);
  809. goto retry;
  810. }
  811. WARN_ON(list_empty(&q->list));
  812. list_del(&q->list);
  813. BUG_ON(q->pi_state);
  814. spin_unlock(lock_ptr);
  815. ret = 1;
  816. }
  817. drop_key_refs(&q->key);
  818. return ret;
  819. }
  820. /*
  821. * PI futexes can not be requeued and must remove themself from the
  822. * hash bucket. The hash bucket lock is held on entry and dropped here.
  823. */
  824. static void unqueue_me_pi(struct futex_q *q, struct futex_hash_bucket *hb)
  825. {
  826. WARN_ON(list_empty(&q->list));
  827. list_del(&q->list);
  828. BUG_ON(!q->pi_state);
  829. free_pi_state(q->pi_state);
  830. q->pi_state = NULL;
  831. spin_unlock(&hb->lock);
  832. drop_key_refs(&q->key);
  833. }
  834. static int futex_wait(u32 __user *uaddr, u32 val, unsigned long time)
  835. {
  836. struct task_struct *curr = current;
  837. DECLARE_WAITQUEUE(wait, curr);
  838. struct futex_hash_bucket *hb;
  839. struct futex_q q;
  840. u32 uval;
  841. int ret;
  842. q.pi_state = NULL;
  843. retry:
  844. down_read(&curr->mm->mmap_sem);
  845. ret = get_futex_key(uaddr, &q.key);
  846. if (unlikely(ret != 0))
  847. goto out_release_sem;
  848. hb = queue_lock(&q, -1, NULL);
  849. /*
  850. * Access the page AFTER the futex is queued.
  851. * Order is important:
  852. *
  853. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  854. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  855. *
  856. * The basic logical guarantee of a futex is that it blocks ONLY
  857. * if cond(var) is known to be true at the time of blocking, for
  858. * any cond. If we queued after testing *uaddr, that would open
  859. * a race condition where we could block indefinitely with
  860. * cond(var) false, which would violate the guarantee.
  861. *
  862. * A consequence is that futex_wait() can return zero and absorb
  863. * a wakeup when *uaddr != val on entry to the syscall. This is
  864. * rare, but normal.
  865. *
  866. * We hold the mmap semaphore, so the mapping cannot have changed
  867. * since we looked it up in get_futex_key.
  868. */
  869. ret = get_futex_value_locked(&uval, uaddr);
  870. if (unlikely(ret)) {
  871. queue_unlock(&q, hb);
  872. /*
  873. * If we would have faulted, release mmap_sem, fault it in and
  874. * start all over again.
  875. */
  876. up_read(&curr->mm->mmap_sem);
  877. ret = get_user(uval, uaddr);
  878. if (!ret)
  879. goto retry;
  880. return ret;
  881. }
  882. ret = -EWOULDBLOCK;
  883. if (uval != val)
  884. goto out_unlock_release_sem;
  885. /* Only actually queue if *uaddr contained val. */
  886. __queue_me(&q, hb);
  887. /*
  888. * Now the futex is queued and we have checked the data, we
  889. * don't want to hold mmap_sem while we sleep.
  890. */
  891. up_read(&curr->mm->mmap_sem);
  892. /*
  893. * There might have been scheduling since the queue_me(), as we
  894. * cannot hold a spinlock across the get_user() in case it
  895. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  896. * queueing ourselves into the futex hash. This code thus has to
  897. * rely on the futex_wake() code removing us from hash when it
  898. * wakes us up.
  899. */
  900. /* add_wait_queue is the barrier after __set_current_state. */
  901. __set_current_state(TASK_INTERRUPTIBLE);
  902. add_wait_queue(&q.waiters, &wait);
  903. /*
  904. * !list_empty() is safe here without any lock.
  905. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  906. */
  907. if (likely(!list_empty(&q.list)))
  908. time = schedule_timeout(time);
  909. __set_current_state(TASK_RUNNING);
  910. /*
  911. * NOTE: we don't remove ourselves from the waitqueue because
  912. * we are the only user of it.
  913. */
  914. /* If we were woken (and unqueued), we succeeded, whatever. */
  915. if (!unqueue_me(&q))
  916. return 0;
  917. if (time == 0)
  918. return -ETIMEDOUT;
  919. /*
  920. * We expect signal_pending(current), but another thread may
  921. * have handled it for us already.
  922. */
  923. return -EINTR;
  924. out_unlock_release_sem:
  925. queue_unlock(&q, hb);
  926. out_release_sem:
  927. up_read(&curr->mm->mmap_sem);
  928. return ret;
  929. }
  930. /*
  931. * Userspace tried a 0 -> TID atomic transition of the futex value
  932. * and failed. The kernel side here does the whole locking operation:
  933. * if there are waiters then it will block, it does PI, etc. (Due to
  934. * races the kernel might see a 0 value of the futex too.)
  935. */
  936. static int do_futex_lock_pi(u32 __user *uaddr, int detect, int trylock,
  937. struct hrtimer_sleeper *to)
  938. {
  939. struct task_struct *curr = current;
  940. struct futex_hash_bucket *hb;
  941. u32 uval, newval, curval;
  942. struct futex_q q;
  943. int ret, attempt = 0;
  944. if (refill_pi_state_cache())
  945. return -ENOMEM;
  946. q.pi_state = NULL;
  947. retry:
  948. down_read(&curr->mm->mmap_sem);
  949. ret = get_futex_key(uaddr, &q.key);
  950. if (unlikely(ret != 0))
  951. goto out_release_sem;
  952. hb = queue_lock(&q, -1, NULL);
  953. retry_locked:
  954. /*
  955. * To avoid races, we attempt to take the lock here again
  956. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  957. * the locks. It will most likely not succeed.
  958. */
  959. newval = current->pid;
  960. inc_preempt_count();
  961. curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
  962. dec_preempt_count();
  963. if (unlikely(curval == -EFAULT))
  964. goto uaddr_faulted;
  965. /* We own the lock already */
  966. if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
  967. if (!detect && 0)
  968. force_sig(SIGKILL, current);
  969. ret = -EDEADLK;
  970. goto out_unlock_release_sem;
  971. }
  972. /*
  973. * Surprise - we got the lock. Just return
  974. * to userspace:
  975. */
  976. if (unlikely(!curval))
  977. goto out_unlock_release_sem;
  978. uval = curval;
  979. newval = uval | FUTEX_WAITERS;
  980. inc_preempt_count();
  981. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  982. dec_preempt_count();
  983. if (unlikely(curval == -EFAULT))
  984. goto uaddr_faulted;
  985. if (unlikely(curval != uval))
  986. goto retry_locked;
  987. /*
  988. * We dont have the lock. Look up the PI state (or create it if
  989. * we are the first waiter):
  990. */
  991. ret = lookup_pi_state(uval, hb, &q);
  992. if (unlikely(ret)) {
  993. /*
  994. * There were no waiters and the owner task lookup
  995. * failed. When the OWNER_DIED bit is set, then we
  996. * know that this is a robust futex and we actually
  997. * take the lock. This is safe as we are protected by
  998. * the hash bucket lock. We also set the waiters bit
  999. * unconditionally here, to simplify glibc handling of
  1000. * multiple tasks racing to acquire the lock and
  1001. * cleanup the problems which were left by the dead
  1002. * owner.
  1003. */
  1004. if (curval & FUTEX_OWNER_DIED) {
  1005. uval = newval;
  1006. newval = current->pid |
  1007. FUTEX_OWNER_DIED | FUTEX_WAITERS;
  1008. inc_preempt_count();
  1009. curval = futex_atomic_cmpxchg_inatomic(uaddr,
  1010. uval, newval);
  1011. dec_preempt_count();
  1012. if (unlikely(curval == -EFAULT))
  1013. goto uaddr_faulted;
  1014. if (unlikely(curval != uval))
  1015. goto retry_locked;
  1016. ret = 0;
  1017. }
  1018. goto out_unlock_release_sem;
  1019. }
  1020. /*
  1021. * Only actually queue now that the atomic ops are done:
  1022. */
  1023. __queue_me(&q, hb);
  1024. /*
  1025. * Now the futex is queued and we have checked the data, we
  1026. * don't want to hold mmap_sem while we sleep.
  1027. */
  1028. up_read(&curr->mm->mmap_sem);
  1029. WARN_ON(!q.pi_state);
  1030. /*
  1031. * Block on the PI mutex:
  1032. */
  1033. if (!trylock)
  1034. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1035. else {
  1036. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1037. /* Fixup the trylock return value: */
  1038. ret = ret ? 0 : -EWOULDBLOCK;
  1039. }
  1040. down_read(&curr->mm->mmap_sem);
  1041. spin_lock(q.lock_ptr);
  1042. /*
  1043. * Got the lock. We might not be the anticipated owner if we
  1044. * did a lock-steal - fix up the PI-state in that case.
  1045. */
  1046. if (!ret && q.pi_state->owner != curr) {
  1047. u32 newtid = current->pid | FUTEX_WAITERS;
  1048. /* Owner died? */
  1049. if (q.pi_state->owner != NULL) {
  1050. spin_lock_irq(&q.pi_state->owner->pi_lock);
  1051. list_del_init(&q.pi_state->list);
  1052. spin_unlock_irq(&q.pi_state->owner->pi_lock);
  1053. } else
  1054. newtid |= FUTEX_OWNER_DIED;
  1055. q.pi_state->owner = current;
  1056. spin_lock_irq(&current->pi_lock);
  1057. list_add(&q.pi_state->list, &current->pi_state_list);
  1058. spin_unlock_irq(&current->pi_lock);
  1059. /* Unqueue and drop the lock */
  1060. unqueue_me_pi(&q, hb);
  1061. up_read(&curr->mm->mmap_sem);
  1062. /*
  1063. * We own it, so we have to replace the pending owner
  1064. * TID. This must be atomic as we have preserve the
  1065. * owner died bit here.
  1066. */
  1067. ret = get_user(uval, uaddr);
  1068. while (!ret) {
  1069. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  1070. curval = futex_atomic_cmpxchg_inatomic(uaddr,
  1071. uval, newval);
  1072. if (curval == -EFAULT)
  1073. ret = -EFAULT;
  1074. if (curval == uval)
  1075. break;
  1076. uval = curval;
  1077. }
  1078. } else {
  1079. /*
  1080. * Catch the rare case, where the lock was released
  1081. * when we were on the way back before we locked
  1082. * the hash bucket.
  1083. */
  1084. if (ret && q.pi_state->owner == curr) {
  1085. if (rt_mutex_trylock(&q.pi_state->pi_mutex))
  1086. ret = 0;
  1087. }
  1088. /* Unqueue and drop the lock */
  1089. unqueue_me_pi(&q, hb);
  1090. up_read(&curr->mm->mmap_sem);
  1091. }
  1092. if (!detect && ret == -EDEADLK && 0)
  1093. force_sig(SIGKILL, current);
  1094. return ret;
  1095. out_unlock_release_sem:
  1096. queue_unlock(&q, hb);
  1097. out_release_sem:
  1098. up_read(&curr->mm->mmap_sem);
  1099. return ret;
  1100. uaddr_faulted:
  1101. /*
  1102. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1103. * non-atomically. Therefore, if get_user below is not
  1104. * enough, we need to handle the fault ourselves, while
  1105. * still holding the mmap_sem.
  1106. */
  1107. if (attempt++) {
  1108. if (futex_handle_fault((unsigned long)uaddr, attempt))
  1109. goto out_unlock_release_sem;
  1110. goto retry_locked;
  1111. }
  1112. queue_unlock(&q, hb);
  1113. up_read(&curr->mm->mmap_sem);
  1114. ret = get_user(uval, uaddr);
  1115. if (!ret && (uval != -EFAULT))
  1116. goto retry;
  1117. return ret;
  1118. }
  1119. /*
  1120. * Restart handler
  1121. */
  1122. static long futex_lock_pi_restart(struct restart_block *restart)
  1123. {
  1124. struct hrtimer_sleeper timeout, *to = NULL;
  1125. int ret;
  1126. restart->fn = do_no_restart_syscall;
  1127. if (restart->arg2 || restart->arg3) {
  1128. to = &timeout;
  1129. hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_ABS);
  1130. hrtimer_init_sleeper(to, current);
  1131. to->timer.expires.tv64 = ((u64)restart->arg1 << 32) |
  1132. (u64) restart->arg0;
  1133. }
  1134. pr_debug("lock_pi restart: %p, %d (%d)\n",
  1135. (u32 __user *)restart->arg0, current->pid);
  1136. ret = do_futex_lock_pi((u32 __user *)restart->arg0, restart->arg1,
  1137. 0, to);
  1138. if (ret != -EINTR)
  1139. return ret;
  1140. restart->fn = futex_lock_pi_restart;
  1141. /* The other values are filled in */
  1142. return -ERESTART_RESTARTBLOCK;
  1143. }
  1144. /*
  1145. * Called from the syscall entry below.
  1146. */
  1147. static int futex_lock_pi(u32 __user *uaddr, int detect, unsigned long sec,
  1148. long nsec, int trylock)
  1149. {
  1150. struct hrtimer_sleeper timeout, *to = NULL;
  1151. struct restart_block *restart;
  1152. int ret;
  1153. if (sec != MAX_SCHEDULE_TIMEOUT) {
  1154. to = &timeout;
  1155. hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_ABS);
  1156. hrtimer_init_sleeper(to, current);
  1157. to->timer.expires = ktime_set(sec, nsec);
  1158. }
  1159. ret = do_futex_lock_pi(uaddr, detect, trylock, to);
  1160. if (ret != -EINTR)
  1161. return ret;
  1162. pr_debug("lock_pi interrupted: %p, %d (%d)\n", uaddr, current->pid);
  1163. restart = &current_thread_info()->restart_block;
  1164. restart->fn = futex_lock_pi_restart;
  1165. restart->arg0 = (unsigned long) uaddr;
  1166. restart->arg1 = detect;
  1167. if (to) {
  1168. restart->arg2 = to->timer.expires.tv64 & 0xFFFFFFFF;
  1169. restart->arg3 = to->timer.expires.tv64 >> 32;
  1170. } else
  1171. restart->arg2 = restart->arg3 = 0;
  1172. return -ERESTART_RESTARTBLOCK;
  1173. }
  1174. /*
  1175. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1176. * This is the in-kernel slowpath: we look up the PI state (if any),
  1177. * and do the rt-mutex unlock.
  1178. */
  1179. static int futex_unlock_pi(u32 __user *uaddr)
  1180. {
  1181. struct futex_hash_bucket *hb;
  1182. struct futex_q *this, *next;
  1183. u32 uval;
  1184. struct list_head *head;
  1185. union futex_key key;
  1186. int ret, attempt = 0;
  1187. retry:
  1188. if (get_user(uval, uaddr))
  1189. return -EFAULT;
  1190. /*
  1191. * We release only a lock we actually own:
  1192. */
  1193. if ((uval & FUTEX_TID_MASK) != current->pid)
  1194. return -EPERM;
  1195. /*
  1196. * First take all the futex related locks:
  1197. */
  1198. down_read(&current->mm->mmap_sem);
  1199. ret = get_futex_key(uaddr, &key);
  1200. if (unlikely(ret != 0))
  1201. goto out;
  1202. hb = hash_futex(&key);
  1203. spin_lock(&hb->lock);
  1204. retry_locked:
  1205. /*
  1206. * To avoid races, try to do the TID -> 0 atomic transition
  1207. * again. If it succeeds then we can return without waking
  1208. * anyone else up:
  1209. */
  1210. inc_preempt_count();
  1211. uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
  1212. dec_preempt_count();
  1213. if (unlikely(uval == -EFAULT))
  1214. goto pi_faulted;
  1215. /*
  1216. * Rare case: we managed to release the lock atomically,
  1217. * no need to wake anyone else up:
  1218. */
  1219. if (unlikely(uval == current->pid))
  1220. goto out_unlock;
  1221. /*
  1222. * Ok, other tasks may need to be woken up - check waiters
  1223. * and do the wakeup if necessary:
  1224. */
  1225. head = &hb->chain;
  1226. list_for_each_entry_safe(this, next, head, list) {
  1227. if (!match_futex (&this->key, &key))
  1228. continue;
  1229. ret = wake_futex_pi(uaddr, uval, this);
  1230. /*
  1231. * The atomic access to the futex value
  1232. * generated a pagefault, so retry the
  1233. * user-access and the wakeup:
  1234. */
  1235. if (ret == -EFAULT)
  1236. goto pi_faulted;
  1237. goto out_unlock;
  1238. }
  1239. /*
  1240. * No waiters - kernel unlocks the futex:
  1241. */
  1242. ret = unlock_futex_pi(uaddr, uval);
  1243. if (ret == -EFAULT)
  1244. goto pi_faulted;
  1245. out_unlock:
  1246. spin_unlock(&hb->lock);
  1247. out:
  1248. up_read(&current->mm->mmap_sem);
  1249. return ret;
  1250. pi_faulted:
  1251. /*
  1252. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1253. * non-atomically. Therefore, if get_user below is not
  1254. * enough, we need to handle the fault ourselves, while
  1255. * still holding the mmap_sem.
  1256. */
  1257. if (attempt++) {
  1258. if (futex_handle_fault((unsigned long)uaddr, attempt))
  1259. goto out_unlock;
  1260. goto retry_locked;
  1261. }
  1262. spin_unlock(&hb->lock);
  1263. up_read(&current->mm->mmap_sem);
  1264. ret = get_user(uval, uaddr);
  1265. if (!ret && (uval != -EFAULT))
  1266. goto retry;
  1267. return ret;
  1268. }
  1269. static int futex_close(struct inode *inode, struct file *filp)
  1270. {
  1271. struct futex_q *q = filp->private_data;
  1272. unqueue_me(q);
  1273. kfree(q);
  1274. return 0;
  1275. }
  1276. /* This is one-shot: once it's gone off you need a new fd */
  1277. static unsigned int futex_poll(struct file *filp,
  1278. struct poll_table_struct *wait)
  1279. {
  1280. struct futex_q *q = filp->private_data;
  1281. int ret = 0;
  1282. poll_wait(filp, &q->waiters, wait);
  1283. /*
  1284. * list_empty() is safe here without any lock.
  1285. * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
  1286. */
  1287. if (list_empty(&q->list))
  1288. ret = POLLIN | POLLRDNORM;
  1289. return ret;
  1290. }
  1291. static struct file_operations futex_fops = {
  1292. .release = futex_close,
  1293. .poll = futex_poll,
  1294. };
  1295. /*
  1296. * Signal allows caller to avoid the race which would occur if they
  1297. * set the sigio stuff up afterwards.
  1298. */
  1299. static int futex_fd(u32 __user *uaddr, int signal)
  1300. {
  1301. struct futex_q *q;
  1302. struct file *filp;
  1303. int ret, err;
  1304. ret = -EINVAL;
  1305. if (!valid_signal(signal))
  1306. goto out;
  1307. ret = get_unused_fd();
  1308. if (ret < 0)
  1309. goto out;
  1310. filp = get_empty_filp();
  1311. if (!filp) {
  1312. put_unused_fd(ret);
  1313. ret = -ENFILE;
  1314. goto out;
  1315. }
  1316. filp->f_op = &futex_fops;
  1317. filp->f_vfsmnt = mntget(futex_mnt);
  1318. filp->f_dentry = dget(futex_mnt->mnt_root);
  1319. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  1320. if (signal) {
  1321. err = f_setown(filp, current->pid, 1);
  1322. if (err < 0) {
  1323. goto error;
  1324. }
  1325. filp->f_owner.signum = signal;
  1326. }
  1327. q = kmalloc(sizeof(*q), GFP_KERNEL);
  1328. if (!q) {
  1329. err = -ENOMEM;
  1330. goto error;
  1331. }
  1332. q->pi_state = NULL;
  1333. down_read(&current->mm->mmap_sem);
  1334. err = get_futex_key(uaddr, &q->key);
  1335. if (unlikely(err != 0)) {
  1336. up_read(&current->mm->mmap_sem);
  1337. kfree(q);
  1338. goto error;
  1339. }
  1340. /*
  1341. * queue_me() must be called before releasing mmap_sem, because
  1342. * key->shared.inode needs to be referenced while holding it.
  1343. */
  1344. filp->private_data = q;
  1345. queue_me(q, ret, filp);
  1346. up_read(&current->mm->mmap_sem);
  1347. /* Now we map fd to filp, so userspace can access it */
  1348. fd_install(ret, filp);
  1349. out:
  1350. return ret;
  1351. error:
  1352. put_unused_fd(ret);
  1353. put_filp(filp);
  1354. ret = err;
  1355. goto out;
  1356. }
  1357. /*
  1358. * Support for robust futexes: the kernel cleans up held futexes at
  1359. * thread exit time.
  1360. *
  1361. * Implementation: user-space maintains a per-thread list of locks it
  1362. * is holding. Upon do_exit(), the kernel carefully walks this list,
  1363. * and marks all locks that are owned by this thread with the
  1364. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  1365. * always manipulated with the lock held, so the list is private and
  1366. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  1367. * field, to allow the kernel to clean up if the thread dies after
  1368. * acquiring the lock, but just before it could have added itself to
  1369. * the list. There can only be one such pending lock.
  1370. */
  1371. /**
  1372. * sys_set_robust_list - set the robust-futex list head of a task
  1373. * @head: pointer to the list-head
  1374. * @len: length of the list-head, as userspace expects
  1375. */
  1376. asmlinkage long
  1377. sys_set_robust_list(struct robust_list_head __user *head,
  1378. size_t len)
  1379. {
  1380. /*
  1381. * The kernel knows only one size for now:
  1382. */
  1383. if (unlikely(len != sizeof(*head)))
  1384. return -EINVAL;
  1385. current->robust_list = head;
  1386. return 0;
  1387. }
  1388. /**
  1389. * sys_get_robust_list - get the robust-futex list head of a task
  1390. * @pid: pid of the process [zero for current task]
  1391. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  1392. * @len_ptr: pointer to a length field, the kernel fills in the header size
  1393. */
  1394. asmlinkage long
  1395. sys_get_robust_list(int pid, struct robust_list_head __user **head_ptr,
  1396. size_t __user *len_ptr)
  1397. {
  1398. struct robust_list_head *head;
  1399. unsigned long ret;
  1400. if (!pid)
  1401. head = current->robust_list;
  1402. else {
  1403. struct task_struct *p;
  1404. ret = -ESRCH;
  1405. read_lock(&tasklist_lock);
  1406. p = find_task_by_pid(pid);
  1407. if (!p)
  1408. goto err_unlock;
  1409. ret = -EPERM;
  1410. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  1411. !capable(CAP_SYS_PTRACE))
  1412. goto err_unlock;
  1413. head = p->robust_list;
  1414. read_unlock(&tasklist_lock);
  1415. }
  1416. if (put_user(sizeof(*head), len_ptr))
  1417. return -EFAULT;
  1418. return put_user(head, head_ptr);
  1419. err_unlock:
  1420. read_unlock(&tasklist_lock);
  1421. return ret;
  1422. }
  1423. /*
  1424. * Process a futex-list entry, check whether it's owned by the
  1425. * dying task, and do notification if so:
  1426. */
  1427. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr)
  1428. {
  1429. u32 uval, nval;
  1430. retry:
  1431. if (get_user(uval, uaddr))
  1432. return -1;
  1433. if ((uval & FUTEX_TID_MASK) == curr->pid) {
  1434. /*
  1435. * Ok, this dying thread is truly holding a futex
  1436. * of interest. Set the OWNER_DIED bit atomically
  1437. * via cmpxchg, and if the value had FUTEX_WAITERS
  1438. * set, wake up a waiter (if any). (We have to do a
  1439. * futex_wake() even if OWNER_DIED is already set -
  1440. * to handle the rare but possible case of recursive
  1441. * thread-death.) The rest of the cleanup is done in
  1442. * userspace.
  1443. */
  1444. nval = futex_atomic_cmpxchg_inatomic(uaddr, uval,
  1445. uval | FUTEX_OWNER_DIED);
  1446. if (nval == -EFAULT)
  1447. return -1;
  1448. if (nval != uval)
  1449. goto retry;
  1450. if (uval & FUTEX_WAITERS)
  1451. futex_wake(uaddr, 1);
  1452. }
  1453. return 0;
  1454. }
  1455. /*
  1456. * Walk curr->robust_list (very carefully, it's a userspace list!)
  1457. * and mark any locks found there dead, and notify any waiters.
  1458. *
  1459. * We silently return on any sign of list-walking problem.
  1460. */
  1461. void exit_robust_list(struct task_struct *curr)
  1462. {
  1463. struct robust_list_head __user *head = curr->robust_list;
  1464. struct robust_list __user *entry, *pending;
  1465. unsigned int limit = ROBUST_LIST_LIMIT;
  1466. unsigned long futex_offset;
  1467. /*
  1468. * Fetch the list head (which was registered earlier, via
  1469. * sys_set_robust_list()):
  1470. */
  1471. if (get_user(entry, &head->list.next))
  1472. return;
  1473. /*
  1474. * Fetch the relative futex offset:
  1475. */
  1476. if (get_user(futex_offset, &head->futex_offset))
  1477. return;
  1478. /*
  1479. * Fetch any possibly pending lock-add first, and handle it
  1480. * if it exists:
  1481. */
  1482. if (get_user(pending, &head->list_op_pending))
  1483. return;
  1484. if (pending)
  1485. handle_futex_death((void *)pending + futex_offset, curr);
  1486. while (entry != &head->list) {
  1487. /*
  1488. * A pending lock might already be on the list, so
  1489. * don't process it twice:
  1490. */
  1491. if (entry != pending)
  1492. if (handle_futex_death((void *)entry + futex_offset,
  1493. curr))
  1494. return;
  1495. /*
  1496. * Fetch the next entry in the list:
  1497. */
  1498. if (get_user(entry, &entry->next))
  1499. return;
  1500. /*
  1501. * Avoid excessively long or circular lists:
  1502. */
  1503. if (!--limit)
  1504. break;
  1505. cond_resched();
  1506. }
  1507. }
  1508. long do_futex(u32 __user *uaddr, int op, u32 val, unsigned long timeout,
  1509. u32 __user *uaddr2, u32 val2, u32 val3)
  1510. {
  1511. int ret;
  1512. switch (op) {
  1513. case FUTEX_WAIT:
  1514. ret = futex_wait(uaddr, val, timeout);
  1515. break;
  1516. case FUTEX_WAKE:
  1517. ret = futex_wake(uaddr, val);
  1518. break;
  1519. case FUTEX_FD:
  1520. /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
  1521. ret = futex_fd(uaddr, val);
  1522. break;
  1523. case FUTEX_REQUEUE:
  1524. ret = futex_requeue(uaddr, uaddr2, val, val2, NULL);
  1525. break;
  1526. case FUTEX_CMP_REQUEUE:
  1527. ret = futex_requeue(uaddr, uaddr2, val, val2, &val3);
  1528. break;
  1529. case FUTEX_WAKE_OP:
  1530. ret = futex_wake_op(uaddr, uaddr2, val, val2, val3);
  1531. break;
  1532. case FUTEX_LOCK_PI:
  1533. ret = futex_lock_pi(uaddr, val, timeout, val2, 0);
  1534. break;
  1535. case FUTEX_UNLOCK_PI:
  1536. ret = futex_unlock_pi(uaddr);
  1537. break;
  1538. case FUTEX_TRYLOCK_PI:
  1539. ret = futex_lock_pi(uaddr, 0, timeout, val2, 1);
  1540. break;
  1541. default:
  1542. ret = -ENOSYS;
  1543. }
  1544. return ret;
  1545. }
  1546. asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
  1547. struct timespec __user *utime, u32 __user *uaddr2,
  1548. u32 val3)
  1549. {
  1550. struct timespec t;
  1551. unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
  1552. u32 val2 = 0;
  1553. if (utime && (op == FUTEX_WAIT || op == FUTEX_LOCK_PI)) {
  1554. if (copy_from_user(&t, utime, sizeof(t)) != 0)
  1555. return -EFAULT;
  1556. if (!timespec_valid(&t))
  1557. return -EINVAL;
  1558. if (op == FUTEX_WAIT)
  1559. timeout = timespec_to_jiffies(&t) + 1;
  1560. else {
  1561. timeout = t.tv_sec;
  1562. val2 = t.tv_nsec;
  1563. }
  1564. }
  1565. /*
  1566. * requeue parameter in 'utime' if op == FUTEX_REQUEUE.
  1567. */
  1568. if (op == FUTEX_REQUEUE || op == FUTEX_CMP_REQUEUE)
  1569. val2 = (u32) (unsigned long) utime;
  1570. return do_futex(uaddr, op, val, timeout, uaddr2, val2, val3);
  1571. }
  1572. static int futexfs_get_sb(struct file_system_type *fs_type,
  1573. int flags, const char *dev_name, void *data,
  1574. struct vfsmount *mnt)
  1575. {
  1576. return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
  1577. }
  1578. static struct file_system_type futex_fs_type = {
  1579. .name = "futexfs",
  1580. .get_sb = futexfs_get_sb,
  1581. .kill_sb = kill_anon_super,
  1582. };
  1583. static int __init init(void)
  1584. {
  1585. unsigned int i;
  1586. register_filesystem(&futex_fs_type);
  1587. futex_mnt = kern_mount(&futex_fs_type);
  1588. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  1589. INIT_LIST_HEAD(&futex_queues[i].chain);
  1590. spin_lock_init(&futex_queues[i].lock);
  1591. }
  1592. return 0;
  1593. }
  1594. __initcall(init);