futex.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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. /* Optional priority inheritance state: */
  96. struct futex_pi_state *pi_state;
  97. struct task_struct *task;
  98. /* Bitset for the optional bitmasked wakeup */
  99. u32 bitset;
  100. };
  101. /*
  102. * Split the global futex_lock into every hash list lock.
  103. */
  104. struct futex_hash_bucket {
  105. spinlock_t lock;
  106. struct plist_head chain;
  107. };
  108. static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
  109. /*
  110. * Take mm->mmap_sem, when futex is shared
  111. */
  112. static inline void futex_lock_mm(struct rw_semaphore *fshared)
  113. {
  114. if (fshared)
  115. down_read(fshared);
  116. }
  117. /*
  118. * Release mm->mmap_sem, when the futex is shared
  119. */
  120. static inline void futex_unlock_mm(struct rw_semaphore *fshared)
  121. {
  122. if (fshared)
  123. up_read(fshared);
  124. }
  125. /*
  126. * We hash on the keys returned from get_futex_key (see below).
  127. */
  128. static struct futex_hash_bucket *hash_futex(union futex_key *key)
  129. {
  130. u32 hash = jhash2((u32*)&key->both.word,
  131. (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
  132. key->both.offset);
  133. return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
  134. }
  135. /*
  136. * Return 1 if two futex_keys are equal, 0 otherwise.
  137. */
  138. static inline int match_futex(union futex_key *key1, union futex_key *key2)
  139. {
  140. return (key1->both.word == key2->both.word
  141. && key1->both.ptr == key2->both.ptr
  142. && key1->both.offset == key2->both.offset);
  143. }
  144. /**
  145. * get_futex_key - Get parameters which are the keys for a futex.
  146. * @uaddr: virtual address of the futex
  147. * @shared: NULL for a PROCESS_PRIVATE futex,
  148. * &current->mm->mmap_sem for a PROCESS_SHARED futex
  149. * @key: address where result is stored.
  150. *
  151. * Returns a negative error code or 0
  152. * The key words are stored in *key on success.
  153. *
  154. * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
  155. * offset_within_page). For private mappings, it's (uaddr, current->mm).
  156. * We can usually work out the index without swapping in the page.
  157. *
  158. * fshared is NULL for PROCESS_PRIVATE futexes
  159. * For other futexes, it points to &current->mm->mmap_sem and
  160. * caller must have taken the reader lock. but NOT any spinlocks.
  161. */
  162. static int get_futex_key(u32 __user *uaddr, struct rw_semaphore *fshared,
  163. union futex_key *key)
  164. {
  165. unsigned long address = (unsigned long)uaddr;
  166. struct mm_struct *mm = current->mm;
  167. struct vm_area_struct *vma;
  168. struct page *page;
  169. int err;
  170. /*
  171. * The futex address must be "naturally" aligned.
  172. */
  173. key->both.offset = address % PAGE_SIZE;
  174. if (unlikely((address % sizeof(u32)) != 0))
  175. return -EINVAL;
  176. address -= key->both.offset;
  177. /*
  178. * PROCESS_PRIVATE futexes are fast.
  179. * As the mm cannot disappear under us and the 'key' only needs
  180. * virtual address, we dont even have to find the underlying vma.
  181. * Note : We do have to check 'uaddr' is a valid user address,
  182. * but access_ok() should be faster than find_vma()
  183. */
  184. if (!fshared) {
  185. if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
  186. return -EFAULT;
  187. key->private.mm = mm;
  188. key->private.address = address;
  189. return 0;
  190. }
  191. /*
  192. * The futex is hashed differently depending on whether
  193. * it's in a shared or private mapping. So check vma first.
  194. */
  195. vma = find_extend_vma(mm, address);
  196. if (unlikely(!vma))
  197. return -EFAULT;
  198. /*
  199. * Permissions.
  200. */
  201. if (unlikely((vma->vm_flags & (VM_IO|VM_READ)) != VM_READ))
  202. return (vma->vm_flags & VM_IO) ? -EPERM : -EACCES;
  203. /*
  204. * Private mappings are handled in a simple way.
  205. *
  206. * NOTE: When userspace waits on a MAP_SHARED mapping, even if
  207. * it's a read-only handle, it's expected that futexes attach to
  208. * the object not the particular process. Therefore we use
  209. * VM_MAYSHARE here, not VM_SHARED which is restricted to shared
  210. * mappings of _writable_ handles.
  211. */
  212. if (likely(!(vma->vm_flags & VM_MAYSHARE))) {
  213. key->both.offset |= FUT_OFF_MMSHARED; /* reference taken on mm */
  214. key->private.mm = mm;
  215. key->private.address = address;
  216. return 0;
  217. }
  218. /*
  219. * Linear file mappings are also simple.
  220. */
  221. key->shared.inode = vma->vm_file->f_path.dentry->d_inode;
  222. key->both.offset |= FUT_OFF_INODE; /* inode-based key. */
  223. if (likely(!(vma->vm_flags & VM_NONLINEAR))) {
  224. key->shared.pgoff = (((address - vma->vm_start) >> PAGE_SHIFT)
  225. + vma->vm_pgoff);
  226. return 0;
  227. }
  228. /*
  229. * We could walk the page table to read the non-linear
  230. * pte, and get the page index without fetching the page
  231. * from swap. But that's a lot of code to duplicate here
  232. * for a rare case, so we simply fetch the page.
  233. */
  234. err = get_user_pages(current, mm, address, 1, 0, 0, &page, NULL);
  235. if (err >= 0) {
  236. key->shared.pgoff =
  237. page->index << (PAGE_CACHE_SHIFT - PAGE_SHIFT);
  238. put_page(page);
  239. return 0;
  240. }
  241. return err;
  242. }
  243. /*
  244. * Take a reference to the resource addressed by a key.
  245. * Can be called while holding spinlocks.
  246. *
  247. */
  248. static void get_futex_key_refs(union futex_key *key)
  249. {
  250. if (key->both.ptr == NULL)
  251. return;
  252. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  253. case FUT_OFF_INODE:
  254. atomic_inc(&key->shared.inode->i_count);
  255. break;
  256. case FUT_OFF_MMSHARED:
  257. atomic_inc(&key->private.mm->mm_count);
  258. break;
  259. }
  260. }
  261. /*
  262. * Drop a reference to the resource addressed by a key.
  263. * The hash bucket spinlock must not be held.
  264. */
  265. static void drop_futex_key_refs(union futex_key *key)
  266. {
  267. if (!key->both.ptr)
  268. return;
  269. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  270. case FUT_OFF_INODE:
  271. iput(key->shared.inode);
  272. break;
  273. case FUT_OFF_MMSHARED:
  274. mmdrop(key->private.mm);
  275. break;
  276. }
  277. }
  278. static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
  279. {
  280. u32 curval;
  281. pagefault_disable();
  282. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  283. pagefault_enable();
  284. return curval;
  285. }
  286. static int get_futex_value_locked(u32 *dest, u32 __user *from)
  287. {
  288. int ret;
  289. pagefault_disable();
  290. ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
  291. pagefault_enable();
  292. return ret ? -EFAULT : 0;
  293. }
  294. /*
  295. * Fault handling.
  296. * if fshared is non NULL, current->mm->mmap_sem is already held
  297. */
  298. static int futex_handle_fault(unsigned long address,
  299. struct rw_semaphore *fshared, int attempt)
  300. {
  301. struct vm_area_struct * vma;
  302. struct mm_struct *mm = current->mm;
  303. int ret = -EFAULT;
  304. if (attempt > 2)
  305. return ret;
  306. if (!fshared)
  307. down_read(&mm->mmap_sem);
  308. vma = find_vma(mm, address);
  309. if (vma && address >= vma->vm_start &&
  310. (vma->vm_flags & VM_WRITE)) {
  311. int fault;
  312. fault = handle_mm_fault(mm, vma, address, 1);
  313. if (unlikely((fault & VM_FAULT_ERROR))) {
  314. #if 0
  315. /* XXX: let's do this when we verify it is OK */
  316. if (ret & VM_FAULT_OOM)
  317. ret = -ENOMEM;
  318. #endif
  319. } else {
  320. ret = 0;
  321. if (fault & VM_FAULT_MAJOR)
  322. current->maj_flt++;
  323. else
  324. current->min_flt++;
  325. }
  326. }
  327. if (!fshared)
  328. up_read(&mm->mmap_sem);
  329. return ret;
  330. }
  331. /*
  332. * PI code:
  333. */
  334. static int refill_pi_state_cache(void)
  335. {
  336. struct futex_pi_state *pi_state;
  337. if (likely(current->pi_state_cache))
  338. return 0;
  339. pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
  340. if (!pi_state)
  341. return -ENOMEM;
  342. INIT_LIST_HEAD(&pi_state->list);
  343. /* pi_mutex gets initialized later */
  344. pi_state->owner = NULL;
  345. atomic_set(&pi_state->refcount, 1);
  346. current->pi_state_cache = pi_state;
  347. return 0;
  348. }
  349. static struct futex_pi_state * alloc_pi_state(void)
  350. {
  351. struct futex_pi_state *pi_state = current->pi_state_cache;
  352. WARN_ON(!pi_state);
  353. current->pi_state_cache = NULL;
  354. return pi_state;
  355. }
  356. static void free_pi_state(struct futex_pi_state *pi_state)
  357. {
  358. if (!atomic_dec_and_test(&pi_state->refcount))
  359. return;
  360. /*
  361. * If pi_state->owner is NULL, the owner is most probably dying
  362. * and has cleaned up the pi_state already
  363. */
  364. if (pi_state->owner) {
  365. spin_lock_irq(&pi_state->owner->pi_lock);
  366. list_del_init(&pi_state->list);
  367. spin_unlock_irq(&pi_state->owner->pi_lock);
  368. rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
  369. }
  370. if (current->pi_state_cache)
  371. kfree(pi_state);
  372. else {
  373. /*
  374. * pi_state->list is already empty.
  375. * clear pi_state->owner.
  376. * refcount is at 0 - put it back to 1.
  377. */
  378. pi_state->owner = NULL;
  379. atomic_set(&pi_state->refcount, 1);
  380. current->pi_state_cache = pi_state;
  381. }
  382. }
  383. /*
  384. * Look up the task based on what TID userspace gave us.
  385. * We dont trust it.
  386. */
  387. static struct task_struct * futex_find_get_task(pid_t pid)
  388. {
  389. struct task_struct *p;
  390. const struct cred *cred = current_cred(), *pcred;
  391. rcu_read_lock();
  392. p = find_task_by_vpid(pid);
  393. if (!p) {
  394. p = ERR_PTR(-ESRCH);
  395. } else {
  396. pcred = __task_cred(p);
  397. if (cred->euid != pcred->euid &&
  398. cred->euid != pcred->uid)
  399. p = ERR_PTR(-ESRCH);
  400. else
  401. get_task_struct(p);
  402. }
  403. rcu_read_unlock();
  404. return p;
  405. }
  406. /*
  407. * This task is holding PI mutexes at exit time => bad.
  408. * Kernel cleans up PI-state, but userspace is likely hosed.
  409. * (Robust-futex cleanup is separate and might save the day for userspace.)
  410. */
  411. void exit_pi_state_list(struct task_struct *curr)
  412. {
  413. struct list_head *next, *head = &curr->pi_state_list;
  414. struct futex_pi_state *pi_state;
  415. struct futex_hash_bucket *hb;
  416. union futex_key key;
  417. if (!futex_cmpxchg_enabled)
  418. return;
  419. /*
  420. * We are a ZOMBIE and nobody can enqueue itself on
  421. * pi_state_list anymore, but we have to be careful
  422. * versus waiters unqueueing themselves:
  423. */
  424. spin_lock_irq(&curr->pi_lock);
  425. while (!list_empty(head)) {
  426. next = head->next;
  427. pi_state = list_entry(next, struct futex_pi_state, list);
  428. key = pi_state->key;
  429. hb = hash_futex(&key);
  430. spin_unlock_irq(&curr->pi_lock);
  431. spin_lock(&hb->lock);
  432. spin_lock_irq(&curr->pi_lock);
  433. /*
  434. * We dropped the pi-lock, so re-check whether this
  435. * task still owns the PI-state:
  436. */
  437. if (head->next != next) {
  438. spin_unlock(&hb->lock);
  439. continue;
  440. }
  441. WARN_ON(pi_state->owner != curr);
  442. WARN_ON(list_empty(&pi_state->list));
  443. list_del_init(&pi_state->list);
  444. pi_state->owner = NULL;
  445. spin_unlock_irq(&curr->pi_lock);
  446. rt_mutex_unlock(&pi_state->pi_mutex);
  447. spin_unlock(&hb->lock);
  448. spin_lock_irq(&curr->pi_lock);
  449. }
  450. spin_unlock_irq(&curr->pi_lock);
  451. }
  452. static int
  453. lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
  454. union futex_key *key, struct futex_pi_state **ps)
  455. {
  456. struct futex_pi_state *pi_state = NULL;
  457. struct futex_q *this, *next;
  458. struct plist_head *head;
  459. struct task_struct *p;
  460. pid_t pid = uval & FUTEX_TID_MASK;
  461. head = &hb->chain;
  462. plist_for_each_entry_safe(this, next, head, list) {
  463. if (match_futex(&this->key, key)) {
  464. /*
  465. * Another waiter already exists - bump up
  466. * the refcount and return its pi_state:
  467. */
  468. pi_state = this->pi_state;
  469. /*
  470. * Userspace might have messed up non PI and PI futexes
  471. */
  472. if (unlikely(!pi_state))
  473. return -EINVAL;
  474. WARN_ON(!atomic_read(&pi_state->refcount));
  475. WARN_ON(pid && pi_state->owner &&
  476. pi_state->owner->pid != pid);
  477. atomic_inc(&pi_state->refcount);
  478. *ps = pi_state;
  479. return 0;
  480. }
  481. }
  482. /*
  483. * We are the first waiter - try to look up the real owner and attach
  484. * the new pi_state to it, but bail out when TID = 0
  485. */
  486. if (!pid)
  487. return -ESRCH;
  488. p = futex_find_get_task(pid);
  489. if (IS_ERR(p))
  490. return PTR_ERR(p);
  491. /*
  492. * We need to look at the task state flags to figure out,
  493. * whether the task is exiting. To protect against the do_exit
  494. * change of the task flags, we do this protected by
  495. * p->pi_lock:
  496. */
  497. spin_lock_irq(&p->pi_lock);
  498. if (unlikely(p->flags & PF_EXITING)) {
  499. /*
  500. * The task is on the way out. When PF_EXITPIDONE is
  501. * set, we know that the task has finished the
  502. * cleanup:
  503. */
  504. int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
  505. spin_unlock_irq(&p->pi_lock);
  506. put_task_struct(p);
  507. return ret;
  508. }
  509. pi_state = alloc_pi_state();
  510. /*
  511. * Initialize the pi_mutex in locked state and make 'p'
  512. * the owner of it:
  513. */
  514. rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
  515. /* Store the key for possible exit cleanups: */
  516. pi_state->key = *key;
  517. WARN_ON(!list_empty(&pi_state->list));
  518. list_add(&pi_state->list, &p->pi_state_list);
  519. pi_state->owner = p;
  520. spin_unlock_irq(&p->pi_lock);
  521. put_task_struct(p);
  522. *ps = pi_state;
  523. return 0;
  524. }
  525. /*
  526. * The hash bucket lock must be held when this is called.
  527. * Afterwards, the futex_q must not be accessed.
  528. */
  529. static void wake_futex(struct futex_q *q)
  530. {
  531. plist_del(&q->list, &q->list.plist);
  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 *queue_lock(struct futex_q *q)
  846. {
  847. struct futex_hash_bucket *hb;
  848. init_waitqueue_head(&q->waiters);
  849. get_futex_key_refs(&q->key);
  850. hb = hash_futex(&q->key);
  851. q->lock_ptr = &hb->lock;
  852. spin_lock(&hb->lock);
  853. return hb;
  854. }
  855. static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  856. {
  857. int prio;
  858. /*
  859. * The priority used to register this element is
  860. * - either the real thread-priority for the real-time threads
  861. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  862. * - or MAX_RT_PRIO for non-RT threads.
  863. * Thus, all RT-threads are woken first in priority order, and
  864. * the others are woken last, in FIFO order.
  865. */
  866. prio = min(current->normal_prio, MAX_RT_PRIO);
  867. plist_node_init(&q->list, prio);
  868. #ifdef CONFIG_DEBUG_PI_LIST
  869. q->list.plist.lock = &hb->lock;
  870. #endif
  871. plist_add(&q->list, &hb->chain);
  872. q->task = current;
  873. spin_unlock(&hb->lock);
  874. }
  875. static inline void
  876. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  877. {
  878. spin_unlock(&hb->lock);
  879. drop_futex_key_refs(&q->key);
  880. }
  881. /*
  882. * queue_me and unqueue_me must be called as a pair, each
  883. * exactly once. They are called with the hashed spinlock held.
  884. */
  885. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  886. static int unqueue_me(struct futex_q *q)
  887. {
  888. spinlock_t *lock_ptr;
  889. int ret = 0;
  890. /* In the common case we don't take the spinlock, which is nice. */
  891. retry:
  892. lock_ptr = q->lock_ptr;
  893. barrier();
  894. if (lock_ptr != NULL) {
  895. spin_lock(lock_ptr);
  896. /*
  897. * q->lock_ptr can change between reading it and
  898. * spin_lock(), causing us to take the wrong lock. This
  899. * corrects the race condition.
  900. *
  901. * Reasoning goes like this: if we have the wrong lock,
  902. * q->lock_ptr must have changed (maybe several times)
  903. * between reading it and the spin_lock(). It can
  904. * change again after the spin_lock() but only if it was
  905. * already changed before the spin_lock(). It cannot,
  906. * however, change back to the original value. Therefore
  907. * we can detect whether we acquired the correct lock.
  908. */
  909. if (unlikely(lock_ptr != q->lock_ptr)) {
  910. spin_unlock(lock_ptr);
  911. goto retry;
  912. }
  913. WARN_ON(plist_node_empty(&q->list));
  914. plist_del(&q->list, &q->list.plist);
  915. BUG_ON(q->pi_state);
  916. spin_unlock(lock_ptr);
  917. ret = 1;
  918. }
  919. drop_futex_key_refs(&q->key);
  920. return ret;
  921. }
  922. /*
  923. * PI futexes can not be requeued and must remove themself from the
  924. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  925. * and dropped here.
  926. */
  927. static void unqueue_me_pi(struct futex_q *q)
  928. {
  929. WARN_ON(plist_node_empty(&q->list));
  930. plist_del(&q->list, &q->list.plist);
  931. BUG_ON(!q->pi_state);
  932. free_pi_state(q->pi_state);
  933. q->pi_state = NULL;
  934. spin_unlock(q->lock_ptr);
  935. drop_futex_key_refs(&q->key);
  936. }
  937. /*
  938. * Fixup the pi_state owner with the new owner.
  939. *
  940. * Must be called with hash bucket lock held and mm->sem held for non
  941. * private futexes.
  942. */
  943. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  944. struct task_struct *newowner,
  945. struct rw_semaphore *fshared)
  946. {
  947. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  948. struct futex_pi_state *pi_state = q->pi_state;
  949. struct task_struct *oldowner = pi_state->owner;
  950. u32 uval, curval, newval;
  951. int ret, attempt = 0;
  952. /* Owner died? */
  953. if (!pi_state->owner)
  954. newtid |= FUTEX_OWNER_DIED;
  955. /*
  956. * We are here either because we stole the rtmutex from the
  957. * pending owner or we are the pending owner which failed to
  958. * get the rtmutex. We have to replace the pending owner TID
  959. * in the user space variable. This must be atomic as we have
  960. * to preserve the owner died bit here.
  961. *
  962. * Note: We write the user space value _before_ changing the
  963. * pi_state because we can fault here. Imagine swapped out
  964. * pages or a fork, which was running right before we acquired
  965. * mmap_sem, that marked all the anonymous memory readonly for
  966. * cow.
  967. *
  968. * Modifying pi_state _before_ the user space value would
  969. * leave the pi_state in an inconsistent state when we fault
  970. * here, because we need to drop the hash bucket lock to
  971. * handle the fault. This might be observed in the PID check
  972. * in lookup_pi_state.
  973. */
  974. retry:
  975. if (get_futex_value_locked(&uval, uaddr))
  976. goto handle_fault;
  977. while (1) {
  978. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  979. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  980. if (curval == -EFAULT)
  981. goto handle_fault;
  982. if (curval == uval)
  983. break;
  984. uval = curval;
  985. }
  986. /*
  987. * We fixed up user space. Now we need to fix the pi_state
  988. * itself.
  989. */
  990. if (pi_state->owner != NULL) {
  991. spin_lock_irq(&pi_state->owner->pi_lock);
  992. WARN_ON(list_empty(&pi_state->list));
  993. list_del_init(&pi_state->list);
  994. spin_unlock_irq(&pi_state->owner->pi_lock);
  995. }
  996. pi_state->owner = newowner;
  997. spin_lock_irq(&newowner->pi_lock);
  998. WARN_ON(!list_empty(&pi_state->list));
  999. list_add(&pi_state->list, &newowner->pi_state_list);
  1000. spin_unlock_irq(&newowner->pi_lock);
  1001. return 0;
  1002. /*
  1003. * To handle the page fault we need to drop the hash bucket
  1004. * lock here. That gives the other task (either the pending
  1005. * owner itself or the task which stole the rtmutex) the
  1006. * chance to try the fixup of the pi_state. So once we are
  1007. * back from handling the fault we need to check the pi_state
  1008. * after reacquiring the hash bucket lock and before trying to
  1009. * do another fixup. When the fixup has been done already we
  1010. * simply return.
  1011. */
  1012. handle_fault:
  1013. spin_unlock(q->lock_ptr);
  1014. ret = futex_handle_fault((unsigned long)uaddr, fshared, attempt++);
  1015. spin_lock(q->lock_ptr);
  1016. /*
  1017. * Check if someone else fixed it for us:
  1018. */
  1019. if (pi_state->owner != oldowner)
  1020. return 0;
  1021. if (ret)
  1022. return ret;
  1023. goto retry;
  1024. }
  1025. /*
  1026. * In case we must use restart_block to restart a futex_wait,
  1027. * we encode in the 'flags' shared capability
  1028. */
  1029. #define FLAGS_SHARED 1
  1030. static long futex_wait_restart(struct restart_block *restart);
  1031. static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
  1032. u32 val, ktime_t *abs_time, u32 bitset)
  1033. {
  1034. struct task_struct *curr = current;
  1035. DECLARE_WAITQUEUE(wait, curr);
  1036. struct futex_hash_bucket *hb;
  1037. struct futex_q q;
  1038. u32 uval;
  1039. int ret;
  1040. struct hrtimer_sleeper t;
  1041. int rem = 0;
  1042. if (!bitset)
  1043. return -EINVAL;
  1044. q.pi_state = NULL;
  1045. q.bitset = bitset;
  1046. retry:
  1047. futex_lock_mm(fshared);
  1048. ret = get_futex_key(uaddr, fshared, &q.key);
  1049. if (unlikely(ret != 0))
  1050. goto out_release_sem;
  1051. hb = queue_lock(&q);
  1052. /*
  1053. * Access the page AFTER the futex is queued.
  1054. * Order is important:
  1055. *
  1056. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  1057. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  1058. *
  1059. * The basic logical guarantee of a futex is that it blocks ONLY
  1060. * if cond(var) is known to be true at the time of blocking, for
  1061. * any cond. If we queued after testing *uaddr, that would open
  1062. * a race condition where we could block indefinitely with
  1063. * cond(var) false, which would violate the guarantee.
  1064. *
  1065. * A consequence is that futex_wait() can return zero and absorb
  1066. * a wakeup when *uaddr != val on entry to the syscall. This is
  1067. * rare, but normal.
  1068. *
  1069. * for shared futexes, we hold the mmap semaphore, so the mapping
  1070. * cannot have changed since we looked it up in get_futex_key.
  1071. */
  1072. ret = get_futex_value_locked(&uval, uaddr);
  1073. if (unlikely(ret)) {
  1074. queue_unlock(&q, hb);
  1075. /*
  1076. * If we would have faulted, release mmap_sem, fault it in and
  1077. * start all over again.
  1078. */
  1079. futex_unlock_mm(fshared);
  1080. ret = get_user(uval, uaddr);
  1081. if (!ret)
  1082. goto retry;
  1083. return ret;
  1084. }
  1085. ret = -EWOULDBLOCK;
  1086. if (uval != val)
  1087. goto out_unlock_release_sem;
  1088. /* Only actually queue if *uaddr contained val. */
  1089. queue_me(&q, hb);
  1090. /*
  1091. * Now the futex is queued and we have checked the data, we
  1092. * don't want to hold mmap_sem while we sleep.
  1093. */
  1094. futex_unlock_mm(fshared);
  1095. /*
  1096. * There might have been scheduling since the queue_me(), as we
  1097. * cannot hold a spinlock across the get_user() in case it
  1098. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  1099. * queueing ourselves into the futex hash. This code thus has to
  1100. * rely on the futex_wake() code removing us from hash when it
  1101. * wakes us up.
  1102. */
  1103. /* add_wait_queue is the barrier after __set_current_state. */
  1104. __set_current_state(TASK_INTERRUPTIBLE);
  1105. add_wait_queue(&q.waiters, &wait);
  1106. /*
  1107. * !plist_node_empty() is safe here without any lock.
  1108. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  1109. */
  1110. if (likely(!plist_node_empty(&q.list))) {
  1111. if (!abs_time)
  1112. schedule();
  1113. else {
  1114. unsigned long slack;
  1115. slack = current->timer_slack_ns;
  1116. if (rt_task(current))
  1117. slack = 0;
  1118. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC,
  1119. HRTIMER_MODE_ABS);
  1120. hrtimer_init_sleeper(&t, current);
  1121. hrtimer_set_expires_range_ns(&t.timer, *abs_time, slack);
  1122. hrtimer_start_expires(&t.timer, HRTIMER_MODE_ABS);
  1123. if (!hrtimer_active(&t.timer))
  1124. t.task = NULL;
  1125. /*
  1126. * the timer could have already expired, in which
  1127. * case current would be flagged for rescheduling.
  1128. * Don't bother calling schedule.
  1129. */
  1130. if (likely(t.task))
  1131. schedule();
  1132. hrtimer_cancel(&t.timer);
  1133. /* Flag if a timeout occured */
  1134. rem = (t.task == NULL);
  1135. destroy_hrtimer_on_stack(&t.timer);
  1136. }
  1137. }
  1138. __set_current_state(TASK_RUNNING);
  1139. /*
  1140. * NOTE: we don't remove ourselves from the waitqueue because
  1141. * we are the only user of it.
  1142. */
  1143. /* If we were woken (and unqueued), we succeeded, whatever. */
  1144. if (!unqueue_me(&q))
  1145. return 0;
  1146. if (rem)
  1147. return -ETIMEDOUT;
  1148. /*
  1149. * We expect signal_pending(current), but another thread may
  1150. * have handled it for us already.
  1151. */
  1152. if (!abs_time)
  1153. return -ERESTARTSYS;
  1154. else {
  1155. struct restart_block *restart;
  1156. restart = &current_thread_info()->restart_block;
  1157. restart->fn = futex_wait_restart;
  1158. restart->futex.uaddr = (u32 *)uaddr;
  1159. restart->futex.val = val;
  1160. restart->futex.time = abs_time->tv64;
  1161. restart->futex.bitset = bitset;
  1162. restart->futex.flags = 0;
  1163. if (fshared)
  1164. restart->futex.flags |= FLAGS_SHARED;
  1165. return -ERESTART_RESTARTBLOCK;
  1166. }
  1167. out_unlock_release_sem:
  1168. queue_unlock(&q, hb);
  1169. out_release_sem:
  1170. futex_unlock_mm(fshared);
  1171. return ret;
  1172. }
  1173. static long futex_wait_restart(struct restart_block *restart)
  1174. {
  1175. u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
  1176. struct rw_semaphore *fshared = NULL;
  1177. ktime_t t;
  1178. t.tv64 = restart->futex.time;
  1179. restart->fn = do_no_restart_syscall;
  1180. if (restart->futex.flags & FLAGS_SHARED)
  1181. fshared = &current->mm->mmap_sem;
  1182. return (long)futex_wait(uaddr, fshared, restart->futex.val, &t,
  1183. restart->futex.bitset);
  1184. }
  1185. /*
  1186. * Userspace tried a 0 -> TID atomic transition of the futex value
  1187. * and failed. The kernel side here does the whole locking operation:
  1188. * if there are waiters then it will block, it does PI, etc. (Due to
  1189. * races the kernel might see a 0 value of the futex too.)
  1190. */
  1191. static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
  1192. int detect, ktime_t *time, int trylock)
  1193. {
  1194. struct hrtimer_sleeper timeout, *to = NULL;
  1195. struct task_struct *curr = current;
  1196. struct futex_hash_bucket *hb;
  1197. u32 uval, newval, curval;
  1198. struct futex_q q;
  1199. int ret, lock_taken, ownerdied = 0, attempt = 0;
  1200. if (refill_pi_state_cache())
  1201. return -ENOMEM;
  1202. if (time) {
  1203. to = &timeout;
  1204. hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
  1205. HRTIMER_MODE_ABS);
  1206. hrtimer_init_sleeper(to, current);
  1207. hrtimer_set_expires(&to->timer, *time);
  1208. }
  1209. q.pi_state = NULL;
  1210. retry:
  1211. futex_lock_mm(fshared);
  1212. ret = get_futex_key(uaddr, fshared, &q.key);
  1213. if (unlikely(ret != 0))
  1214. goto out_release_sem;
  1215. retry_unlocked:
  1216. hb = queue_lock(&q);
  1217. retry_locked:
  1218. ret = lock_taken = 0;
  1219. /*
  1220. * To avoid races, we attempt to take the lock here again
  1221. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  1222. * the locks. It will most likely not succeed.
  1223. */
  1224. newval = task_pid_vnr(current);
  1225. curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
  1226. if (unlikely(curval == -EFAULT))
  1227. goto uaddr_faulted;
  1228. /*
  1229. * Detect deadlocks. In case of REQUEUE_PI this is a valid
  1230. * situation and we return success to user space.
  1231. */
  1232. if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) {
  1233. ret = -EDEADLK;
  1234. goto out_unlock_release_sem;
  1235. }
  1236. /*
  1237. * Surprise - we got the lock. Just return to userspace:
  1238. */
  1239. if (unlikely(!curval))
  1240. goto out_unlock_release_sem;
  1241. uval = curval;
  1242. /*
  1243. * Set the WAITERS flag, so the owner will know it has someone
  1244. * to wake at next unlock
  1245. */
  1246. newval = curval | FUTEX_WAITERS;
  1247. /*
  1248. * There are two cases, where a futex might have no owner (the
  1249. * owner TID is 0): OWNER_DIED. We take over the futex in this
  1250. * case. We also do an unconditional take over, when the owner
  1251. * of the futex died.
  1252. *
  1253. * This is safe as we are protected by the hash bucket lock !
  1254. */
  1255. if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
  1256. /* Keep the OWNER_DIED bit */
  1257. newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(current);
  1258. ownerdied = 0;
  1259. lock_taken = 1;
  1260. }
  1261. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  1262. if (unlikely(curval == -EFAULT))
  1263. goto uaddr_faulted;
  1264. if (unlikely(curval != uval))
  1265. goto retry_locked;
  1266. /*
  1267. * We took the lock due to owner died take over.
  1268. */
  1269. if (unlikely(lock_taken))
  1270. goto out_unlock_release_sem;
  1271. /*
  1272. * We dont have the lock. Look up the PI state (or create it if
  1273. * we are the first waiter):
  1274. */
  1275. ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
  1276. if (unlikely(ret)) {
  1277. switch (ret) {
  1278. case -EAGAIN:
  1279. /*
  1280. * Task is exiting and we just wait for the
  1281. * exit to complete.
  1282. */
  1283. queue_unlock(&q, hb);
  1284. futex_unlock_mm(fshared);
  1285. cond_resched();
  1286. goto retry;
  1287. case -ESRCH:
  1288. /*
  1289. * No owner found for this futex. Check if the
  1290. * OWNER_DIED bit is set to figure out whether
  1291. * this is a robust futex or not.
  1292. */
  1293. if (get_futex_value_locked(&curval, uaddr))
  1294. goto uaddr_faulted;
  1295. /*
  1296. * We simply start over in case of a robust
  1297. * futex. The code above will take the futex
  1298. * and return happy.
  1299. */
  1300. if (curval & FUTEX_OWNER_DIED) {
  1301. ownerdied = 1;
  1302. goto retry_locked;
  1303. }
  1304. default:
  1305. goto out_unlock_release_sem;
  1306. }
  1307. }
  1308. /*
  1309. * Only actually queue now that the atomic ops are done:
  1310. */
  1311. queue_me(&q, hb);
  1312. /*
  1313. * Now the futex is queued and we have checked the data, we
  1314. * don't want to hold mmap_sem while we sleep.
  1315. */
  1316. futex_unlock_mm(fshared);
  1317. WARN_ON(!q.pi_state);
  1318. /*
  1319. * Block on the PI mutex:
  1320. */
  1321. if (!trylock)
  1322. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1323. else {
  1324. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1325. /* Fixup the trylock return value: */
  1326. ret = ret ? 0 : -EWOULDBLOCK;
  1327. }
  1328. futex_lock_mm(fshared);
  1329. spin_lock(q.lock_ptr);
  1330. if (!ret) {
  1331. /*
  1332. * Got the lock. We might not be the anticipated owner
  1333. * if we did a lock-steal - fix up the PI-state in
  1334. * that case:
  1335. */
  1336. if (q.pi_state->owner != curr)
  1337. ret = fixup_pi_state_owner(uaddr, &q, curr, fshared);
  1338. } else {
  1339. /*
  1340. * Catch the rare case, where the lock was released
  1341. * when we were on the way back before we locked the
  1342. * hash bucket.
  1343. */
  1344. if (q.pi_state->owner == curr) {
  1345. /*
  1346. * Try to get the rt_mutex now. This might
  1347. * fail as some other task acquired the
  1348. * rt_mutex after we removed ourself from the
  1349. * rt_mutex waiters list.
  1350. */
  1351. if (rt_mutex_trylock(&q.pi_state->pi_mutex))
  1352. ret = 0;
  1353. else {
  1354. /*
  1355. * pi_state is incorrect, some other
  1356. * task did a lock steal and we
  1357. * returned due to timeout or signal
  1358. * without taking the rt_mutex. Too
  1359. * late. We can access the
  1360. * rt_mutex_owner without locking, as
  1361. * the other task is now blocked on
  1362. * the hash bucket lock. Fix the state
  1363. * up.
  1364. */
  1365. struct task_struct *owner;
  1366. int res;
  1367. owner = rt_mutex_owner(&q.pi_state->pi_mutex);
  1368. res = fixup_pi_state_owner(uaddr, &q, owner,
  1369. fshared);
  1370. /* propagate -EFAULT, if the fixup failed */
  1371. if (res)
  1372. ret = res;
  1373. }
  1374. } else {
  1375. /*
  1376. * Paranoia check. If we did not take the lock
  1377. * in the trylock above, then we should not be
  1378. * the owner of the rtmutex, neither the real
  1379. * nor the pending one:
  1380. */
  1381. if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
  1382. printk(KERN_ERR "futex_lock_pi: ret = %d "
  1383. "pi-mutex: %p pi-state %p\n", ret,
  1384. q.pi_state->pi_mutex.owner,
  1385. q.pi_state->owner);
  1386. }
  1387. }
  1388. /* Unqueue and drop the lock */
  1389. unqueue_me_pi(&q);
  1390. futex_unlock_mm(fshared);
  1391. if (to)
  1392. destroy_hrtimer_on_stack(&to->timer);
  1393. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  1394. out_unlock_release_sem:
  1395. queue_unlock(&q, hb);
  1396. out_release_sem:
  1397. futex_unlock_mm(fshared);
  1398. if (to)
  1399. destroy_hrtimer_on_stack(&to->timer);
  1400. return ret;
  1401. uaddr_faulted:
  1402. /*
  1403. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1404. * non-atomically. Therefore, if get_user below is not
  1405. * enough, we need to handle the fault ourselves, while
  1406. * still holding the mmap_sem.
  1407. *
  1408. * ... and hb->lock. :-) --ANK
  1409. */
  1410. queue_unlock(&q, hb);
  1411. if (attempt++) {
  1412. ret = futex_handle_fault((unsigned long)uaddr, fshared,
  1413. attempt);
  1414. if (ret)
  1415. goto out_release_sem;
  1416. goto retry_unlocked;
  1417. }
  1418. futex_unlock_mm(fshared);
  1419. ret = get_user(uval, uaddr);
  1420. if (!ret && (uval != -EFAULT))
  1421. goto retry;
  1422. if (to)
  1423. destroy_hrtimer_on_stack(&to->timer);
  1424. return ret;
  1425. }
  1426. /*
  1427. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1428. * This is the in-kernel slowpath: we look up the PI state (if any),
  1429. * and do the rt-mutex unlock.
  1430. */
  1431. static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
  1432. {
  1433. struct futex_hash_bucket *hb;
  1434. struct futex_q *this, *next;
  1435. u32 uval;
  1436. struct plist_head *head;
  1437. union futex_key key;
  1438. int ret, attempt = 0;
  1439. retry:
  1440. if (get_user(uval, uaddr))
  1441. return -EFAULT;
  1442. /*
  1443. * We release only a lock we actually own:
  1444. */
  1445. if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
  1446. return -EPERM;
  1447. /*
  1448. * First take all the futex related locks:
  1449. */
  1450. futex_lock_mm(fshared);
  1451. ret = get_futex_key(uaddr, fshared, &key);
  1452. if (unlikely(ret != 0))
  1453. goto out;
  1454. hb = hash_futex(&key);
  1455. retry_unlocked:
  1456. spin_lock(&hb->lock);
  1457. /*
  1458. * To avoid races, try to do the TID -> 0 atomic transition
  1459. * again. If it succeeds then we can return without waking
  1460. * anyone else up:
  1461. */
  1462. if (!(uval & FUTEX_OWNER_DIED))
  1463. uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
  1464. if (unlikely(uval == -EFAULT))
  1465. goto pi_faulted;
  1466. /*
  1467. * Rare case: we managed to release the lock atomically,
  1468. * no need to wake anyone else up:
  1469. */
  1470. if (unlikely(uval == task_pid_vnr(current)))
  1471. goto out_unlock;
  1472. /*
  1473. * Ok, other tasks may need to be woken up - check waiters
  1474. * and do the wakeup if necessary:
  1475. */
  1476. head = &hb->chain;
  1477. plist_for_each_entry_safe(this, next, head, list) {
  1478. if (!match_futex (&this->key, &key))
  1479. continue;
  1480. ret = wake_futex_pi(uaddr, uval, this);
  1481. /*
  1482. * The atomic access to the futex value
  1483. * generated a pagefault, so retry the
  1484. * user-access and the wakeup:
  1485. */
  1486. if (ret == -EFAULT)
  1487. goto pi_faulted;
  1488. goto out_unlock;
  1489. }
  1490. /*
  1491. * No waiters - kernel unlocks the futex:
  1492. */
  1493. if (!(uval & FUTEX_OWNER_DIED)) {
  1494. ret = unlock_futex_pi(uaddr, uval);
  1495. if (ret == -EFAULT)
  1496. goto pi_faulted;
  1497. }
  1498. out_unlock:
  1499. spin_unlock(&hb->lock);
  1500. out:
  1501. futex_unlock_mm(fshared);
  1502. return ret;
  1503. pi_faulted:
  1504. /*
  1505. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1506. * non-atomically. Therefore, if get_user below is not
  1507. * enough, we need to handle the fault ourselves, while
  1508. * still holding the mmap_sem.
  1509. *
  1510. * ... and hb->lock. --ANK
  1511. */
  1512. spin_unlock(&hb->lock);
  1513. if (attempt++) {
  1514. ret = futex_handle_fault((unsigned long)uaddr, fshared,
  1515. attempt);
  1516. if (ret)
  1517. goto out;
  1518. uval = 0;
  1519. goto retry_unlocked;
  1520. }
  1521. futex_unlock_mm(fshared);
  1522. ret = get_user(uval, uaddr);
  1523. if (!ret && (uval != -EFAULT))
  1524. goto retry;
  1525. return ret;
  1526. }
  1527. /*
  1528. * Support for robust futexes: the kernel cleans up held futexes at
  1529. * thread exit time.
  1530. *
  1531. * Implementation: user-space maintains a per-thread list of locks it
  1532. * is holding. Upon do_exit(), the kernel carefully walks this list,
  1533. * and marks all locks that are owned by this thread with the
  1534. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  1535. * always manipulated with the lock held, so the list is private and
  1536. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  1537. * field, to allow the kernel to clean up if the thread dies after
  1538. * acquiring the lock, but just before it could have added itself to
  1539. * the list. There can only be one such pending lock.
  1540. */
  1541. /**
  1542. * sys_set_robust_list - set the robust-futex list head of a task
  1543. * @head: pointer to the list-head
  1544. * @len: length of the list-head, as userspace expects
  1545. */
  1546. asmlinkage long
  1547. sys_set_robust_list(struct robust_list_head __user *head,
  1548. size_t len)
  1549. {
  1550. if (!futex_cmpxchg_enabled)
  1551. return -ENOSYS;
  1552. /*
  1553. * The kernel knows only one size for now:
  1554. */
  1555. if (unlikely(len != sizeof(*head)))
  1556. return -EINVAL;
  1557. current->robust_list = head;
  1558. return 0;
  1559. }
  1560. /**
  1561. * sys_get_robust_list - get the robust-futex list head of a task
  1562. * @pid: pid of the process [zero for current task]
  1563. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  1564. * @len_ptr: pointer to a length field, the kernel fills in the header size
  1565. */
  1566. asmlinkage long
  1567. sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
  1568. size_t __user *len_ptr)
  1569. {
  1570. struct robust_list_head __user *head;
  1571. unsigned long ret;
  1572. const struct cred *cred = current_cred(), *pcred;
  1573. if (!futex_cmpxchg_enabled)
  1574. return -ENOSYS;
  1575. if (!pid)
  1576. head = current->robust_list;
  1577. else {
  1578. struct task_struct *p;
  1579. ret = -ESRCH;
  1580. rcu_read_lock();
  1581. p = find_task_by_vpid(pid);
  1582. if (!p)
  1583. goto err_unlock;
  1584. ret = -EPERM;
  1585. pcred = __task_cred(p);
  1586. if (cred->euid != pcred->euid &&
  1587. cred->euid != pcred->uid &&
  1588. !capable(CAP_SYS_PTRACE))
  1589. goto err_unlock;
  1590. head = p->robust_list;
  1591. rcu_read_unlock();
  1592. }
  1593. if (put_user(sizeof(*head), len_ptr))
  1594. return -EFAULT;
  1595. return put_user(head, head_ptr);
  1596. err_unlock:
  1597. rcu_read_unlock();
  1598. return ret;
  1599. }
  1600. /*
  1601. * Process a futex-list entry, check whether it's owned by the
  1602. * dying task, and do notification if so:
  1603. */
  1604. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
  1605. {
  1606. u32 uval, nval, mval;
  1607. retry:
  1608. if (get_user(uval, uaddr))
  1609. return -1;
  1610. if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
  1611. /*
  1612. * Ok, this dying thread is truly holding a futex
  1613. * of interest. Set the OWNER_DIED bit atomically
  1614. * via cmpxchg, and if the value had FUTEX_WAITERS
  1615. * set, wake up a waiter (if any). (We have to do a
  1616. * futex_wake() even if OWNER_DIED is already set -
  1617. * to handle the rare but possible case of recursive
  1618. * thread-death.) The rest of the cleanup is done in
  1619. * userspace.
  1620. */
  1621. mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  1622. nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
  1623. if (nval == -EFAULT)
  1624. return -1;
  1625. if (nval != uval)
  1626. goto retry;
  1627. /*
  1628. * Wake robust non-PI futexes here. The wakeup of
  1629. * PI futexes happens in exit_pi_state():
  1630. */
  1631. if (!pi && (uval & FUTEX_WAITERS))
  1632. futex_wake(uaddr, &curr->mm->mmap_sem, 1,
  1633. FUTEX_BITSET_MATCH_ANY);
  1634. }
  1635. return 0;
  1636. }
  1637. /*
  1638. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  1639. */
  1640. static inline int fetch_robust_entry(struct robust_list __user **entry,
  1641. struct robust_list __user * __user *head,
  1642. int *pi)
  1643. {
  1644. unsigned long uentry;
  1645. if (get_user(uentry, (unsigned long __user *)head))
  1646. return -EFAULT;
  1647. *entry = (void __user *)(uentry & ~1UL);
  1648. *pi = uentry & 1;
  1649. return 0;
  1650. }
  1651. /*
  1652. * Walk curr->robust_list (very carefully, it's a userspace list!)
  1653. * and mark any locks found there dead, and notify any waiters.
  1654. *
  1655. * We silently return on any sign of list-walking problem.
  1656. */
  1657. void exit_robust_list(struct task_struct *curr)
  1658. {
  1659. struct robust_list_head __user *head = curr->robust_list;
  1660. struct robust_list __user *entry, *next_entry, *pending;
  1661. unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
  1662. unsigned long futex_offset;
  1663. int rc;
  1664. if (!futex_cmpxchg_enabled)
  1665. return;
  1666. /*
  1667. * Fetch the list head (which was registered earlier, via
  1668. * sys_set_robust_list()):
  1669. */
  1670. if (fetch_robust_entry(&entry, &head->list.next, &pi))
  1671. return;
  1672. /*
  1673. * Fetch the relative futex offset:
  1674. */
  1675. if (get_user(futex_offset, &head->futex_offset))
  1676. return;
  1677. /*
  1678. * Fetch any possibly pending lock-add first, and handle it
  1679. * if it exists:
  1680. */
  1681. if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
  1682. return;
  1683. next_entry = NULL; /* avoid warning with gcc */
  1684. while (entry != &head->list) {
  1685. /*
  1686. * Fetch the next entry in the list before calling
  1687. * handle_futex_death:
  1688. */
  1689. rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
  1690. /*
  1691. * A pending lock might already be on the list, so
  1692. * don't process it twice:
  1693. */
  1694. if (entry != pending)
  1695. if (handle_futex_death((void __user *)entry + futex_offset,
  1696. curr, pi))
  1697. return;
  1698. if (rc)
  1699. return;
  1700. entry = next_entry;
  1701. pi = next_pi;
  1702. /*
  1703. * Avoid excessively long or circular lists:
  1704. */
  1705. if (!--limit)
  1706. break;
  1707. cond_resched();
  1708. }
  1709. if (pending)
  1710. handle_futex_death((void __user *)pending + futex_offset,
  1711. curr, pip);
  1712. }
  1713. long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
  1714. u32 __user *uaddr2, u32 val2, u32 val3)
  1715. {
  1716. int ret = -ENOSYS;
  1717. int cmd = op & FUTEX_CMD_MASK;
  1718. struct rw_semaphore *fshared = NULL;
  1719. if (!(op & FUTEX_PRIVATE_FLAG))
  1720. fshared = &current->mm->mmap_sem;
  1721. switch (cmd) {
  1722. case FUTEX_WAIT:
  1723. val3 = FUTEX_BITSET_MATCH_ANY;
  1724. case FUTEX_WAIT_BITSET:
  1725. ret = futex_wait(uaddr, fshared, val, timeout, val3);
  1726. break;
  1727. case FUTEX_WAKE:
  1728. val3 = FUTEX_BITSET_MATCH_ANY;
  1729. case FUTEX_WAKE_BITSET:
  1730. ret = futex_wake(uaddr, fshared, val, val3);
  1731. break;
  1732. case FUTEX_REQUEUE:
  1733. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
  1734. break;
  1735. case FUTEX_CMP_REQUEUE:
  1736. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
  1737. break;
  1738. case FUTEX_WAKE_OP:
  1739. ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
  1740. break;
  1741. case FUTEX_LOCK_PI:
  1742. if (futex_cmpxchg_enabled)
  1743. ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
  1744. break;
  1745. case FUTEX_UNLOCK_PI:
  1746. if (futex_cmpxchg_enabled)
  1747. ret = futex_unlock_pi(uaddr, fshared);
  1748. break;
  1749. case FUTEX_TRYLOCK_PI:
  1750. if (futex_cmpxchg_enabled)
  1751. ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
  1752. break;
  1753. default:
  1754. ret = -ENOSYS;
  1755. }
  1756. return ret;
  1757. }
  1758. asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
  1759. struct timespec __user *utime, u32 __user *uaddr2,
  1760. u32 val3)
  1761. {
  1762. struct timespec ts;
  1763. ktime_t t, *tp = NULL;
  1764. u32 val2 = 0;
  1765. int cmd = op & FUTEX_CMD_MASK;
  1766. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  1767. cmd == FUTEX_WAIT_BITSET)) {
  1768. if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
  1769. return -EFAULT;
  1770. if (!timespec_valid(&ts))
  1771. return -EINVAL;
  1772. t = timespec_to_ktime(ts);
  1773. if (cmd == FUTEX_WAIT)
  1774. t = ktime_add_safe(ktime_get(), t);
  1775. tp = &t;
  1776. }
  1777. /*
  1778. * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
  1779. * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
  1780. */
  1781. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  1782. cmd == FUTEX_WAKE_OP)
  1783. val2 = (u32) (unsigned long) utime;
  1784. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  1785. }
  1786. static int __init futex_init(void)
  1787. {
  1788. u32 curval;
  1789. int i;
  1790. /*
  1791. * This will fail and we want it. Some arch implementations do
  1792. * runtime detection of the futex_atomic_cmpxchg_inatomic()
  1793. * functionality. We want to know that before we call in any
  1794. * of the complex code paths. Also we want to prevent
  1795. * registration of robust lists in that case. NULL is
  1796. * guaranteed to fault and we get -EFAULT on functional
  1797. * implementation, the non functional ones will return
  1798. * -ENOSYS.
  1799. */
  1800. curval = cmpxchg_futex_value_locked(NULL, 0, 0);
  1801. if (curval == -EFAULT)
  1802. futex_cmpxchg_enabled = 1;
  1803. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  1804. plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
  1805. spin_lock_init(&futex_queues[i].lock);
  1806. }
  1807. return 0;
  1808. }
  1809. __initcall(futex_init);