futex.c 52 KB

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