futex.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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. rcu_read_lock();
  391. p = find_task_by_vpid(pid);
  392. if (!p || ((current->euid != p->euid) && (current->euid != p->uid)))
  393. p = ERR_PTR(-ESRCH);
  394. else
  395. get_task_struct(p);
  396. rcu_read_unlock();
  397. return p;
  398. }
  399. /*
  400. * This task is holding PI mutexes at exit time => bad.
  401. * Kernel cleans up PI-state, but userspace is likely hosed.
  402. * (Robust-futex cleanup is separate and might save the day for userspace.)
  403. */
  404. void exit_pi_state_list(struct task_struct *curr)
  405. {
  406. struct list_head *next, *head = &curr->pi_state_list;
  407. struct futex_pi_state *pi_state;
  408. struct futex_hash_bucket *hb;
  409. union futex_key key;
  410. if (!futex_cmpxchg_enabled)
  411. return;
  412. /*
  413. * We are a ZOMBIE and nobody can enqueue itself on
  414. * pi_state_list anymore, but we have to be careful
  415. * versus waiters unqueueing themselves:
  416. */
  417. spin_lock_irq(&curr->pi_lock);
  418. while (!list_empty(head)) {
  419. next = head->next;
  420. pi_state = list_entry(next, struct futex_pi_state, list);
  421. key = pi_state->key;
  422. hb = hash_futex(&key);
  423. spin_unlock_irq(&curr->pi_lock);
  424. spin_lock(&hb->lock);
  425. spin_lock_irq(&curr->pi_lock);
  426. /*
  427. * We dropped the pi-lock, so re-check whether this
  428. * task still owns the PI-state:
  429. */
  430. if (head->next != next) {
  431. spin_unlock(&hb->lock);
  432. continue;
  433. }
  434. WARN_ON(pi_state->owner != curr);
  435. WARN_ON(list_empty(&pi_state->list));
  436. list_del_init(&pi_state->list);
  437. pi_state->owner = NULL;
  438. spin_unlock_irq(&curr->pi_lock);
  439. rt_mutex_unlock(&pi_state->pi_mutex);
  440. spin_unlock(&hb->lock);
  441. spin_lock_irq(&curr->pi_lock);
  442. }
  443. spin_unlock_irq(&curr->pi_lock);
  444. }
  445. static int
  446. lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
  447. union futex_key *key, struct futex_pi_state **ps)
  448. {
  449. struct futex_pi_state *pi_state = NULL;
  450. struct futex_q *this, *next;
  451. struct plist_head *head;
  452. struct task_struct *p;
  453. pid_t pid = uval & FUTEX_TID_MASK;
  454. head = &hb->chain;
  455. plist_for_each_entry_safe(this, next, head, list) {
  456. if (match_futex(&this->key, key)) {
  457. /*
  458. * Another waiter already exists - bump up
  459. * the refcount and return its pi_state:
  460. */
  461. pi_state = this->pi_state;
  462. /*
  463. * Userspace might have messed up non PI and PI futexes
  464. */
  465. if (unlikely(!pi_state))
  466. return -EINVAL;
  467. WARN_ON(!atomic_read(&pi_state->refcount));
  468. WARN_ON(pid && pi_state->owner &&
  469. pi_state->owner->pid != pid);
  470. atomic_inc(&pi_state->refcount);
  471. *ps = pi_state;
  472. return 0;
  473. }
  474. }
  475. /*
  476. * We are the first waiter - try to look up the real owner and attach
  477. * the new pi_state to it, but bail out when TID = 0
  478. */
  479. if (!pid)
  480. return -ESRCH;
  481. p = futex_find_get_task(pid);
  482. if (IS_ERR(p))
  483. return PTR_ERR(p);
  484. /*
  485. * We need to look at the task state flags to figure out,
  486. * whether the task is exiting. To protect against the do_exit
  487. * change of the task flags, we do this protected by
  488. * p->pi_lock:
  489. */
  490. spin_lock_irq(&p->pi_lock);
  491. if (unlikely(p->flags & PF_EXITING)) {
  492. /*
  493. * The task is on the way out. When PF_EXITPIDONE is
  494. * set, we know that the task has finished the
  495. * cleanup:
  496. */
  497. int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
  498. spin_unlock_irq(&p->pi_lock);
  499. put_task_struct(p);
  500. return ret;
  501. }
  502. pi_state = alloc_pi_state();
  503. /*
  504. * Initialize the pi_mutex in locked state and make 'p'
  505. * the owner of it:
  506. */
  507. rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
  508. /* Store the key for possible exit cleanups: */
  509. pi_state->key = *key;
  510. WARN_ON(!list_empty(&pi_state->list));
  511. list_add(&pi_state->list, &p->pi_state_list);
  512. pi_state->owner = p;
  513. spin_unlock_irq(&p->pi_lock);
  514. put_task_struct(p);
  515. *ps = pi_state;
  516. return 0;
  517. }
  518. /*
  519. * The hash bucket lock must be held when this is called.
  520. * Afterwards, the futex_q must not be accessed.
  521. */
  522. static void wake_futex(struct futex_q *q)
  523. {
  524. plist_del(&q->list, &q->list.plist);
  525. /*
  526. * The lock in wake_up_all() is a crucial memory barrier after the
  527. * plist_del() and also before assigning to q->lock_ptr.
  528. */
  529. wake_up_all(&q->waiters);
  530. /*
  531. * The waiting task can free the futex_q as soon as this is written,
  532. * without taking any locks. This must come last.
  533. *
  534. * A memory barrier is required here to prevent the following store
  535. * to lock_ptr from getting ahead of the wakeup. Clearing the lock
  536. * at the end of wake_up_all() does not prevent this store from
  537. * moving.
  538. */
  539. smp_wmb();
  540. q->lock_ptr = NULL;
  541. }
  542. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  543. {
  544. struct task_struct *new_owner;
  545. struct futex_pi_state *pi_state = this->pi_state;
  546. u32 curval, newval;
  547. if (!pi_state)
  548. return -EINVAL;
  549. spin_lock(&pi_state->pi_mutex.wait_lock);
  550. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  551. /*
  552. * This happens when we have stolen the lock and the original
  553. * pending owner did not enqueue itself back on the rt_mutex.
  554. * Thats not a tragedy. We know that way, that a lock waiter
  555. * is on the fly. We make the futex_q waiter the pending owner.
  556. */
  557. if (!new_owner)
  558. new_owner = this->task;
  559. /*
  560. * We pass it to the next owner. (The WAITERS bit is always
  561. * kept enabled while there is PI state around. We must also
  562. * preserve the owner died bit.)
  563. */
  564. if (!(uval & FUTEX_OWNER_DIED)) {
  565. int ret = 0;
  566. newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
  567. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  568. if (curval == -EFAULT)
  569. ret = -EFAULT;
  570. else if (curval != uval)
  571. ret = -EINVAL;
  572. if (ret) {
  573. spin_unlock(&pi_state->pi_mutex.wait_lock);
  574. return ret;
  575. }
  576. }
  577. spin_lock_irq(&pi_state->owner->pi_lock);
  578. WARN_ON(list_empty(&pi_state->list));
  579. list_del_init(&pi_state->list);
  580. spin_unlock_irq(&pi_state->owner->pi_lock);
  581. spin_lock_irq(&new_owner->pi_lock);
  582. WARN_ON(!list_empty(&pi_state->list));
  583. list_add(&pi_state->list, &new_owner->pi_state_list);
  584. pi_state->owner = new_owner;
  585. spin_unlock_irq(&new_owner->pi_lock);
  586. spin_unlock(&pi_state->pi_mutex.wait_lock);
  587. rt_mutex_unlock(&pi_state->pi_mutex);
  588. return 0;
  589. }
  590. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  591. {
  592. u32 oldval;
  593. /*
  594. * There is no waiter, so we unlock the futex. The owner died
  595. * bit has not to be preserved here. We are the owner:
  596. */
  597. oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
  598. if (oldval == -EFAULT)
  599. return oldval;
  600. if (oldval != uval)
  601. return -EAGAIN;
  602. return 0;
  603. }
  604. /*
  605. * Express the locking dependencies for lockdep:
  606. */
  607. static inline void
  608. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  609. {
  610. if (hb1 <= hb2) {
  611. spin_lock(&hb1->lock);
  612. if (hb1 < hb2)
  613. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  614. } else { /* hb1 > hb2 */
  615. spin_lock(&hb2->lock);
  616. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  617. }
  618. }
  619. /*
  620. * Wake up all waiters hashed on the physical page that is mapped
  621. * to this virtual address:
  622. */
  623. static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
  624. int nr_wake, u32 bitset)
  625. {
  626. struct futex_hash_bucket *hb;
  627. struct futex_q *this, *next;
  628. struct plist_head *head;
  629. union futex_key key;
  630. int ret;
  631. if (!bitset)
  632. return -EINVAL;
  633. futex_lock_mm(fshared);
  634. ret = get_futex_key(uaddr, fshared, &key);
  635. if (unlikely(ret != 0))
  636. goto out;
  637. hb = hash_futex(&key);
  638. spin_lock(&hb->lock);
  639. head = &hb->chain;
  640. plist_for_each_entry_safe(this, next, head, list) {
  641. if (match_futex (&this->key, &key)) {
  642. if (this->pi_state) {
  643. ret = -EINVAL;
  644. break;
  645. }
  646. /* Check if one of the bits is set in both bitsets */
  647. if (!(this->bitset & bitset))
  648. continue;
  649. wake_futex(this);
  650. if (++ret >= nr_wake)
  651. break;
  652. }
  653. }
  654. spin_unlock(&hb->lock);
  655. out:
  656. futex_unlock_mm(fshared);
  657. return ret;
  658. }
  659. /*
  660. * Wake up all waiters hashed on the physical page that is mapped
  661. * to this virtual address:
  662. */
  663. static int
  664. futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
  665. u32 __user *uaddr2,
  666. int nr_wake, int nr_wake2, int op)
  667. {
  668. union futex_key key1, key2;
  669. struct futex_hash_bucket *hb1, *hb2;
  670. struct plist_head *head;
  671. struct futex_q *this, *next;
  672. int ret, op_ret, attempt = 0;
  673. retryfull:
  674. futex_lock_mm(fshared);
  675. ret = get_futex_key(uaddr1, fshared, &key1);
  676. if (unlikely(ret != 0))
  677. goto out;
  678. ret = get_futex_key(uaddr2, fshared, &key2);
  679. if (unlikely(ret != 0))
  680. goto out;
  681. hb1 = hash_futex(&key1);
  682. hb2 = hash_futex(&key2);
  683. retry:
  684. double_lock_hb(hb1, hb2);
  685. op_ret = futex_atomic_op_inuser(op, uaddr2);
  686. if (unlikely(op_ret < 0)) {
  687. u32 dummy;
  688. spin_unlock(&hb1->lock);
  689. if (hb1 != hb2)
  690. spin_unlock(&hb2->lock);
  691. #ifndef CONFIG_MMU
  692. /*
  693. * we don't get EFAULT from MMU faults if we don't have an MMU,
  694. * but we might get them from range checking
  695. */
  696. ret = op_ret;
  697. goto out;
  698. #endif
  699. if (unlikely(op_ret != -EFAULT)) {
  700. ret = op_ret;
  701. goto out;
  702. }
  703. /*
  704. * futex_atomic_op_inuser needs to both read and write
  705. * *(int __user *)uaddr2, but we can't modify it
  706. * non-atomically. Therefore, if get_user below is not
  707. * enough, we need to handle the fault ourselves, while
  708. * still holding the mmap_sem.
  709. */
  710. if (attempt++) {
  711. ret = futex_handle_fault((unsigned long)uaddr2,
  712. fshared, attempt);
  713. if (ret)
  714. goto out;
  715. goto retry;
  716. }
  717. /*
  718. * If we would have faulted, release mmap_sem,
  719. * fault it in and start all over again.
  720. */
  721. futex_unlock_mm(fshared);
  722. ret = get_user(dummy, uaddr2);
  723. if (ret)
  724. return ret;
  725. goto retryfull;
  726. }
  727. head = &hb1->chain;
  728. plist_for_each_entry_safe(this, next, head, list) {
  729. if (match_futex (&this->key, &key1)) {
  730. wake_futex(this);
  731. if (++ret >= nr_wake)
  732. break;
  733. }
  734. }
  735. if (op_ret > 0) {
  736. head = &hb2->chain;
  737. op_ret = 0;
  738. plist_for_each_entry_safe(this, next, head, list) {
  739. if (match_futex (&this->key, &key2)) {
  740. wake_futex(this);
  741. if (++op_ret >= nr_wake2)
  742. break;
  743. }
  744. }
  745. ret += op_ret;
  746. }
  747. spin_unlock(&hb1->lock);
  748. if (hb1 != hb2)
  749. spin_unlock(&hb2->lock);
  750. out:
  751. futex_unlock_mm(fshared);
  752. return ret;
  753. }
  754. /*
  755. * Requeue all waiters hashed on one physical page to another
  756. * physical page.
  757. */
  758. static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
  759. u32 __user *uaddr2,
  760. int nr_wake, int nr_requeue, u32 *cmpval)
  761. {
  762. union futex_key key1, key2;
  763. struct futex_hash_bucket *hb1, *hb2;
  764. struct plist_head *head1;
  765. struct futex_q *this, *next;
  766. int ret, drop_count = 0;
  767. retry:
  768. futex_lock_mm(fshared);
  769. ret = get_futex_key(uaddr1, fshared, &key1);
  770. if (unlikely(ret != 0))
  771. goto out;
  772. ret = get_futex_key(uaddr2, fshared, &key2);
  773. if (unlikely(ret != 0))
  774. goto out;
  775. hb1 = hash_futex(&key1);
  776. hb2 = hash_futex(&key2);
  777. double_lock_hb(hb1, hb2);
  778. if (likely(cmpval != NULL)) {
  779. u32 curval;
  780. ret = get_futex_value_locked(&curval, uaddr1);
  781. if (unlikely(ret)) {
  782. spin_unlock(&hb1->lock);
  783. if (hb1 != hb2)
  784. spin_unlock(&hb2->lock);
  785. /*
  786. * If we would have faulted, release mmap_sem, fault
  787. * it in and start all over again.
  788. */
  789. futex_unlock_mm(fshared);
  790. ret = get_user(curval, uaddr1);
  791. if (!ret)
  792. goto retry;
  793. return ret;
  794. }
  795. if (curval != *cmpval) {
  796. ret = -EAGAIN;
  797. goto out_unlock;
  798. }
  799. }
  800. head1 = &hb1->chain;
  801. plist_for_each_entry_safe(this, next, head1, list) {
  802. if (!match_futex (&this->key, &key1))
  803. continue;
  804. if (++ret <= nr_wake) {
  805. wake_futex(this);
  806. } else {
  807. /*
  808. * If key1 and key2 hash to the same bucket, no need to
  809. * requeue.
  810. */
  811. if (likely(head1 != &hb2->chain)) {
  812. plist_del(&this->list, &hb1->chain);
  813. plist_add(&this->list, &hb2->chain);
  814. this->lock_ptr = &hb2->lock;
  815. #ifdef CONFIG_DEBUG_PI_LIST
  816. this->list.plist.lock = &hb2->lock;
  817. #endif
  818. }
  819. this->key = key2;
  820. get_futex_key_refs(&key2);
  821. drop_count++;
  822. if (ret - nr_wake >= nr_requeue)
  823. break;
  824. }
  825. }
  826. out_unlock:
  827. spin_unlock(&hb1->lock);
  828. if (hb1 != hb2)
  829. spin_unlock(&hb2->lock);
  830. /* drop_futex_key_refs() must be called outside the spinlocks. */
  831. while (--drop_count >= 0)
  832. drop_futex_key_refs(&key1);
  833. out:
  834. futex_unlock_mm(fshared);
  835. return ret;
  836. }
  837. /* The key must be already stored in q->key. */
  838. static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
  839. {
  840. struct futex_hash_bucket *hb;
  841. init_waitqueue_head(&q->waiters);
  842. get_futex_key_refs(&q->key);
  843. hb = hash_futex(&q->key);
  844. q->lock_ptr = &hb->lock;
  845. spin_lock(&hb->lock);
  846. return hb;
  847. }
  848. static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  849. {
  850. int prio;
  851. /*
  852. * The priority used to register this element is
  853. * - either the real thread-priority for the real-time threads
  854. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  855. * - or MAX_RT_PRIO for non-RT threads.
  856. * Thus, all RT-threads are woken first in priority order, and
  857. * the others are woken last, in FIFO order.
  858. */
  859. prio = min(current->normal_prio, MAX_RT_PRIO);
  860. plist_node_init(&q->list, prio);
  861. #ifdef CONFIG_DEBUG_PI_LIST
  862. q->list.plist.lock = &hb->lock;
  863. #endif
  864. plist_add(&q->list, &hb->chain);
  865. q->task = current;
  866. spin_unlock(&hb->lock);
  867. }
  868. static inline void
  869. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  870. {
  871. spin_unlock(&hb->lock);
  872. drop_futex_key_refs(&q->key);
  873. }
  874. /*
  875. * queue_me and unqueue_me must be called as a pair, each
  876. * exactly once. They are called with the hashed spinlock held.
  877. */
  878. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  879. static int unqueue_me(struct futex_q *q)
  880. {
  881. spinlock_t *lock_ptr;
  882. int ret = 0;
  883. /* In the common case we don't take the spinlock, which is nice. */
  884. retry:
  885. lock_ptr = q->lock_ptr;
  886. barrier();
  887. if (lock_ptr != NULL) {
  888. spin_lock(lock_ptr);
  889. /*
  890. * q->lock_ptr can change between reading it and
  891. * spin_lock(), causing us to take the wrong lock. This
  892. * corrects the race condition.
  893. *
  894. * Reasoning goes like this: if we have the wrong lock,
  895. * q->lock_ptr must have changed (maybe several times)
  896. * between reading it and the spin_lock(). It can
  897. * change again after the spin_lock() but only if it was
  898. * already changed before the spin_lock(). It cannot,
  899. * however, change back to the original value. Therefore
  900. * we can detect whether we acquired the correct lock.
  901. */
  902. if (unlikely(lock_ptr != q->lock_ptr)) {
  903. spin_unlock(lock_ptr);
  904. goto retry;
  905. }
  906. WARN_ON(plist_node_empty(&q->list));
  907. plist_del(&q->list, &q->list.plist);
  908. BUG_ON(q->pi_state);
  909. spin_unlock(lock_ptr);
  910. ret = 1;
  911. }
  912. drop_futex_key_refs(&q->key);
  913. return ret;
  914. }
  915. /*
  916. * PI futexes can not be requeued and must remove themself from the
  917. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  918. * and dropped here.
  919. */
  920. static void unqueue_me_pi(struct futex_q *q)
  921. {
  922. WARN_ON(plist_node_empty(&q->list));
  923. plist_del(&q->list, &q->list.plist);
  924. BUG_ON(!q->pi_state);
  925. free_pi_state(q->pi_state);
  926. q->pi_state = NULL;
  927. spin_unlock(q->lock_ptr);
  928. drop_futex_key_refs(&q->key);
  929. }
  930. /*
  931. * Fixup the pi_state owner with the new owner.
  932. *
  933. * Must be called with hash bucket lock held and mm->sem held for non
  934. * private futexes.
  935. */
  936. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  937. struct task_struct *newowner)
  938. {
  939. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  940. struct futex_pi_state *pi_state = q->pi_state;
  941. u32 uval, curval, newval;
  942. int ret;
  943. /* Owner died? */
  944. if (pi_state->owner != NULL) {
  945. spin_lock_irq(&pi_state->owner->pi_lock);
  946. WARN_ON(list_empty(&pi_state->list));
  947. list_del_init(&pi_state->list);
  948. spin_unlock_irq(&pi_state->owner->pi_lock);
  949. } else
  950. newtid |= FUTEX_OWNER_DIED;
  951. pi_state->owner = newowner;
  952. spin_lock_irq(&newowner->pi_lock);
  953. WARN_ON(!list_empty(&pi_state->list));
  954. list_add(&pi_state->list, &newowner->pi_state_list);
  955. spin_unlock_irq(&newowner->pi_lock);
  956. /*
  957. * We own it, so we have to replace the pending owner
  958. * TID. This must be atomic as we have preserve the
  959. * owner died bit here.
  960. */
  961. ret = get_futex_value_locked(&uval, uaddr);
  962. while (!ret) {
  963. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  964. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  965. if (curval == -EFAULT)
  966. ret = -EFAULT;
  967. if (curval == uval)
  968. break;
  969. uval = curval;
  970. }
  971. return ret;
  972. }
  973. /*
  974. * In case we must use restart_block to restart a futex_wait,
  975. * we encode in the 'flags' shared capability
  976. */
  977. #define FLAGS_SHARED 1
  978. static long futex_wait_restart(struct restart_block *restart);
  979. static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
  980. u32 val, ktime_t *abs_time, u32 bitset)
  981. {
  982. struct task_struct *curr = current;
  983. DECLARE_WAITQUEUE(wait, curr);
  984. struct futex_hash_bucket *hb;
  985. struct futex_q q;
  986. u32 uval;
  987. int ret;
  988. struct hrtimer_sleeper t;
  989. int rem = 0;
  990. if (!bitset)
  991. return -EINVAL;
  992. q.pi_state = NULL;
  993. q.bitset = bitset;
  994. retry:
  995. futex_lock_mm(fshared);
  996. ret = get_futex_key(uaddr, fshared, &q.key);
  997. if (unlikely(ret != 0))
  998. goto out_release_sem;
  999. hb = queue_lock(&q);
  1000. /*
  1001. * Access the page AFTER the futex is queued.
  1002. * Order is important:
  1003. *
  1004. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  1005. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  1006. *
  1007. * The basic logical guarantee of a futex is that it blocks ONLY
  1008. * if cond(var) is known to be true at the time of blocking, for
  1009. * any cond. If we queued after testing *uaddr, that would open
  1010. * a race condition where we could block indefinitely with
  1011. * cond(var) false, which would violate the guarantee.
  1012. *
  1013. * A consequence is that futex_wait() can return zero and absorb
  1014. * a wakeup when *uaddr != val on entry to the syscall. This is
  1015. * rare, but normal.
  1016. *
  1017. * for shared futexes, we hold the mmap semaphore, so the mapping
  1018. * cannot have changed since we looked it up in get_futex_key.
  1019. */
  1020. ret = get_futex_value_locked(&uval, uaddr);
  1021. if (unlikely(ret)) {
  1022. queue_unlock(&q, hb);
  1023. /*
  1024. * If we would have faulted, release mmap_sem, fault it in and
  1025. * start all over again.
  1026. */
  1027. futex_unlock_mm(fshared);
  1028. ret = get_user(uval, uaddr);
  1029. if (!ret)
  1030. goto retry;
  1031. return ret;
  1032. }
  1033. ret = -EWOULDBLOCK;
  1034. if (uval != val)
  1035. goto out_unlock_release_sem;
  1036. /* Only actually queue if *uaddr contained val. */
  1037. queue_me(&q, hb);
  1038. /*
  1039. * Now the futex is queued and we have checked the data, we
  1040. * don't want to hold mmap_sem while we sleep.
  1041. */
  1042. futex_unlock_mm(fshared);
  1043. /*
  1044. * There might have been scheduling since the queue_me(), as we
  1045. * cannot hold a spinlock across the get_user() in case it
  1046. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  1047. * queueing ourselves into the futex hash. This code thus has to
  1048. * rely on the futex_wake() code removing us from hash when it
  1049. * wakes us up.
  1050. */
  1051. /* add_wait_queue is the barrier after __set_current_state. */
  1052. __set_current_state(TASK_INTERRUPTIBLE);
  1053. add_wait_queue(&q.waiters, &wait);
  1054. /*
  1055. * !plist_node_empty() is safe here without any lock.
  1056. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  1057. */
  1058. if (likely(!plist_node_empty(&q.list))) {
  1059. if (!abs_time)
  1060. schedule();
  1061. else {
  1062. hrtimer_init_on_stack(&t.timer, CLOCK_MONOTONIC,
  1063. HRTIMER_MODE_ABS);
  1064. hrtimer_init_sleeper(&t, current);
  1065. t.timer.expires = *abs_time;
  1066. hrtimer_start(&t.timer, t.timer.expires,
  1067. HRTIMER_MODE_ABS);
  1068. if (!hrtimer_active(&t.timer))
  1069. t.task = NULL;
  1070. /*
  1071. * the timer could have already expired, in which
  1072. * case current would be flagged for rescheduling.
  1073. * Don't bother calling schedule.
  1074. */
  1075. if (likely(t.task))
  1076. schedule();
  1077. hrtimer_cancel(&t.timer);
  1078. /* Flag if a timeout occured */
  1079. rem = (t.task == NULL);
  1080. destroy_hrtimer_on_stack(&t.timer);
  1081. }
  1082. }
  1083. __set_current_state(TASK_RUNNING);
  1084. /*
  1085. * NOTE: we don't remove ourselves from the waitqueue because
  1086. * we are the only user of it.
  1087. */
  1088. /* If we were woken (and unqueued), we succeeded, whatever. */
  1089. if (!unqueue_me(&q))
  1090. return 0;
  1091. if (rem)
  1092. return -ETIMEDOUT;
  1093. /*
  1094. * We expect signal_pending(current), but another thread may
  1095. * have handled it for us already.
  1096. */
  1097. if (!abs_time)
  1098. return -ERESTARTSYS;
  1099. else {
  1100. struct restart_block *restart;
  1101. restart = &current_thread_info()->restart_block;
  1102. restart->fn = futex_wait_restart;
  1103. restart->futex.uaddr = (u32 *)uaddr;
  1104. restart->futex.val = val;
  1105. restart->futex.time = abs_time->tv64;
  1106. restart->futex.bitset = bitset;
  1107. restart->futex.flags = 0;
  1108. if (fshared)
  1109. restart->futex.flags |= FLAGS_SHARED;
  1110. return -ERESTART_RESTARTBLOCK;
  1111. }
  1112. out_unlock_release_sem:
  1113. queue_unlock(&q, hb);
  1114. out_release_sem:
  1115. futex_unlock_mm(fshared);
  1116. return ret;
  1117. }
  1118. static long futex_wait_restart(struct restart_block *restart)
  1119. {
  1120. u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
  1121. struct rw_semaphore *fshared = NULL;
  1122. ktime_t t;
  1123. t.tv64 = restart->futex.time;
  1124. restart->fn = do_no_restart_syscall;
  1125. if (restart->futex.flags & FLAGS_SHARED)
  1126. fshared = &current->mm->mmap_sem;
  1127. return (long)futex_wait(uaddr, fshared, restart->futex.val, &t,
  1128. restart->futex.bitset);
  1129. }
  1130. /*
  1131. * Userspace tried a 0 -> TID atomic transition of the futex value
  1132. * and failed. The kernel side here does the whole locking operation:
  1133. * if there are waiters then it will block, it does PI, etc. (Due to
  1134. * races the kernel might see a 0 value of the futex too.)
  1135. */
  1136. static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
  1137. int detect, ktime_t *time, int trylock)
  1138. {
  1139. struct hrtimer_sleeper timeout, *to = NULL;
  1140. struct task_struct *curr = current;
  1141. struct futex_hash_bucket *hb;
  1142. u32 uval, newval, curval;
  1143. struct futex_q q;
  1144. int ret, lock_taken, ownerdied = 0, attempt = 0;
  1145. if (refill_pi_state_cache())
  1146. return -ENOMEM;
  1147. if (time) {
  1148. to = &timeout;
  1149. hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
  1150. HRTIMER_MODE_ABS);
  1151. hrtimer_init_sleeper(to, current);
  1152. to->timer.expires = *time;
  1153. }
  1154. q.pi_state = NULL;
  1155. retry:
  1156. futex_lock_mm(fshared);
  1157. ret = get_futex_key(uaddr, fshared, &q.key);
  1158. if (unlikely(ret != 0))
  1159. goto out_release_sem;
  1160. retry_unlocked:
  1161. hb = queue_lock(&q);
  1162. retry_locked:
  1163. ret = lock_taken = 0;
  1164. /*
  1165. * To avoid races, we attempt to take the lock here again
  1166. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  1167. * the locks. It will most likely not succeed.
  1168. */
  1169. newval = task_pid_vnr(current);
  1170. curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
  1171. if (unlikely(curval == -EFAULT))
  1172. goto uaddr_faulted;
  1173. /*
  1174. * Detect deadlocks. In case of REQUEUE_PI this is a valid
  1175. * situation and we return success to user space.
  1176. */
  1177. if (unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(current))) {
  1178. ret = -EDEADLK;
  1179. goto out_unlock_release_sem;
  1180. }
  1181. /*
  1182. * Surprise - we got the lock. Just return to userspace:
  1183. */
  1184. if (unlikely(!curval))
  1185. goto out_unlock_release_sem;
  1186. uval = curval;
  1187. /*
  1188. * Set the WAITERS flag, so the owner will know it has someone
  1189. * to wake at next unlock
  1190. */
  1191. newval = curval | FUTEX_WAITERS;
  1192. /*
  1193. * There are two cases, where a futex might have no owner (the
  1194. * owner TID is 0): OWNER_DIED. We take over the futex in this
  1195. * case. We also do an unconditional take over, when the owner
  1196. * of the futex died.
  1197. *
  1198. * This is safe as we are protected by the hash bucket lock !
  1199. */
  1200. if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
  1201. /* Keep the OWNER_DIED bit */
  1202. newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(current);
  1203. ownerdied = 0;
  1204. lock_taken = 1;
  1205. }
  1206. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  1207. if (unlikely(curval == -EFAULT))
  1208. goto uaddr_faulted;
  1209. if (unlikely(curval != uval))
  1210. goto retry_locked;
  1211. /*
  1212. * We took the lock due to owner died take over.
  1213. */
  1214. if (unlikely(lock_taken))
  1215. goto out_unlock_release_sem;
  1216. /*
  1217. * We dont have the lock. Look up the PI state (or create it if
  1218. * we are the first waiter):
  1219. */
  1220. ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
  1221. if (unlikely(ret)) {
  1222. switch (ret) {
  1223. case -EAGAIN:
  1224. /*
  1225. * Task is exiting and we just wait for the
  1226. * exit to complete.
  1227. */
  1228. queue_unlock(&q, hb);
  1229. futex_unlock_mm(fshared);
  1230. cond_resched();
  1231. goto retry;
  1232. case -ESRCH:
  1233. /*
  1234. * No owner found for this futex. Check if the
  1235. * OWNER_DIED bit is set to figure out whether
  1236. * this is a robust futex or not.
  1237. */
  1238. if (get_futex_value_locked(&curval, uaddr))
  1239. goto uaddr_faulted;
  1240. /*
  1241. * We simply start over in case of a robust
  1242. * futex. The code above will take the futex
  1243. * and return happy.
  1244. */
  1245. if (curval & FUTEX_OWNER_DIED) {
  1246. ownerdied = 1;
  1247. goto retry_locked;
  1248. }
  1249. default:
  1250. goto out_unlock_release_sem;
  1251. }
  1252. }
  1253. /*
  1254. * Only actually queue now that the atomic ops are done:
  1255. */
  1256. queue_me(&q, hb);
  1257. /*
  1258. * Now the futex is queued and we have checked the data, we
  1259. * don't want to hold mmap_sem while we sleep.
  1260. */
  1261. futex_unlock_mm(fshared);
  1262. WARN_ON(!q.pi_state);
  1263. /*
  1264. * Block on the PI mutex:
  1265. */
  1266. if (!trylock)
  1267. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1268. else {
  1269. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1270. /* Fixup the trylock return value: */
  1271. ret = ret ? 0 : -EWOULDBLOCK;
  1272. }
  1273. futex_lock_mm(fshared);
  1274. spin_lock(q.lock_ptr);
  1275. if (!ret) {
  1276. /*
  1277. * Got the lock. We might not be the anticipated owner
  1278. * if we did a lock-steal - fix up the PI-state in
  1279. * that case:
  1280. */
  1281. if (q.pi_state->owner != curr)
  1282. ret = fixup_pi_state_owner(uaddr, &q, curr);
  1283. } else {
  1284. /*
  1285. * Catch the rare case, where the lock was released
  1286. * when we were on the way back before we locked the
  1287. * hash bucket.
  1288. */
  1289. if (q.pi_state->owner == curr) {
  1290. /*
  1291. * Try to get the rt_mutex now. This might
  1292. * fail as some other task acquired the
  1293. * rt_mutex after we removed ourself from the
  1294. * rt_mutex waiters list.
  1295. */
  1296. if (rt_mutex_trylock(&q.pi_state->pi_mutex))
  1297. ret = 0;
  1298. else {
  1299. /*
  1300. * pi_state is incorrect, some other
  1301. * task did a lock steal and we
  1302. * returned due to timeout or signal
  1303. * without taking the rt_mutex. Too
  1304. * late. We can access the
  1305. * rt_mutex_owner without locking, as
  1306. * the other task is now blocked on
  1307. * the hash bucket lock. Fix the state
  1308. * up.
  1309. */
  1310. struct task_struct *owner;
  1311. int res;
  1312. owner = rt_mutex_owner(&q.pi_state->pi_mutex);
  1313. res = fixup_pi_state_owner(uaddr, &q, owner);
  1314. /* propagate -EFAULT, if the fixup failed */
  1315. if (res)
  1316. ret = res;
  1317. }
  1318. } else {
  1319. /*
  1320. * Paranoia check. If we did not take the lock
  1321. * in the trylock above, then we should not be
  1322. * the owner of the rtmutex, neither the real
  1323. * nor the pending one:
  1324. */
  1325. if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
  1326. printk(KERN_ERR "futex_lock_pi: ret = %d "
  1327. "pi-mutex: %p pi-state %p\n", ret,
  1328. q.pi_state->pi_mutex.owner,
  1329. q.pi_state->owner);
  1330. }
  1331. }
  1332. /* Unqueue and drop the lock */
  1333. unqueue_me_pi(&q);
  1334. futex_unlock_mm(fshared);
  1335. if (to)
  1336. destroy_hrtimer_on_stack(&to->timer);
  1337. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  1338. out_unlock_release_sem:
  1339. queue_unlock(&q, hb);
  1340. out_release_sem:
  1341. futex_unlock_mm(fshared);
  1342. if (to)
  1343. destroy_hrtimer_on_stack(&to->timer);
  1344. return ret;
  1345. uaddr_faulted:
  1346. /*
  1347. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1348. * non-atomically. Therefore, if get_user below is not
  1349. * enough, we need to handle the fault ourselves, while
  1350. * still holding the mmap_sem.
  1351. *
  1352. * ... and hb->lock. :-) --ANK
  1353. */
  1354. queue_unlock(&q, hb);
  1355. if (attempt++) {
  1356. ret = futex_handle_fault((unsigned long)uaddr, fshared,
  1357. attempt);
  1358. if (ret)
  1359. goto out_release_sem;
  1360. goto retry_unlocked;
  1361. }
  1362. futex_unlock_mm(fshared);
  1363. ret = get_user(uval, uaddr);
  1364. if (!ret && (uval != -EFAULT))
  1365. goto retry;
  1366. if (to)
  1367. destroy_hrtimer_on_stack(&to->timer);
  1368. return ret;
  1369. }
  1370. /*
  1371. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1372. * This is the in-kernel slowpath: we look up the PI state (if any),
  1373. * and do the rt-mutex unlock.
  1374. */
  1375. static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
  1376. {
  1377. struct futex_hash_bucket *hb;
  1378. struct futex_q *this, *next;
  1379. u32 uval;
  1380. struct plist_head *head;
  1381. union futex_key key;
  1382. int ret, attempt = 0;
  1383. retry:
  1384. if (get_user(uval, uaddr))
  1385. return -EFAULT;
  1386. /*
  1387. * We release only a lock we actually own:
  1388. */
  1389. if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
  1390. return -EPERM;
  1391. /*
  1392. * First take all the futex related locks:
  1393. */
  1394. futex_lock_mm(fshared);
  1395. ret = get_futex_key(uaddr, fshared, &key);
  1396. if (unlikely(ret != 0))
  1397. goto out;
  1398. hb = hash_futex(&key);
  1399. retry_unlocked:
  1400. spin_lock(&hb->lock);
  1401. /*
  1402. * To avoid races, try to do the TID -> 0 atomic transition
  1403. * again. If it succeeds then we can return without waking
  1404. * anyone else up:
  1405. */
  1406. if (!(uval & FUTEX_OWNER_DIED))
  1407. uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
  1408. if (unlikely(uval == -EFAULT))
  1409. goto pi_faulted;
  1410. /*
  1411. * Rare case: we managed to release the lock atomically,
  1412. * no need to wake anyone else up:
  1413. */
  1414. if (unlikely(uval == task_pid_vnr(current)))
  1415. goto out_unlock;
  1416. /*
  1417. * Ok, other tasks may need to be woken up - check waiters
  1418. * and do the wakeup if necessary:
  1419. */
  1420. head = &hb->chain;
  1421. plist_for_each_entry_safe(this, next, head, list) {
  1422. if (!match_futex (&this->key, &key))
  1423. continue;
  1424. ret = wake_futex_pi(uaddr, uval, this);
  1425. /*
  1426. * The atomic access to the futex value
  1427. * generated a pagefault, so retry the
  1428. * user-access and the wakeup:
  1429. */
  1430. if (ret == -EFAULT)
  1431. goto pi_faulted;
  1432. goto out_unlock;
  1433. }
  1434. /*
  1435. * No waiters - kernel unlocks the futex:
  1436. */
  1437. if (!(uval & FUTEX_OWNER_DIED)) {
  1438. ret = unlock_futex_pi(uaddr, uval);
  1439. if (ret == -EFAULT)
  1440. goto pi_faulted;
  1441. }
  1442. out_unlock:
  1443. spin_unlock(&hb->lock);
  1444. out:
  1445. futex_unlock_mm(fshared);
  1446. return ret;
  1447. pi_faulted:
  1448. /*
  1449. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1450. * non-atomically. Therefore, if get_user below is not
  1451. * enough, we need to handle the fault ourselves, while
  1452. * still holding the mmap_sem.
  1453. *
  1454. * ... and hb->lock. --ANK
  1455. */
  1456. spin_unlock(&hb->lock);
  1457. if (attempt++) {
  1458. ret = futex_handle_fault((unsigned long)uaddr, fshared,
  1459. attempt);
  1460. if (ret)
  1461. goto out;
  1462. uval = 0;
  1463. goto retry_unlocked;
  1464. }
  1465. futex_unlock_mm(fshared);
  1466. ret = get_user(uval, uaddr);
  1467. if (!ret && (uval != -EFAULT))
  1468. goto retry;
  1469. return ret;
  1470. }
  1471. /*
  1472. * Support for robust futexes: the kernel cleans up held futexes at
  1473. * thread exit time.
  1474. *
  1475. * Implementation: user-space maintains a per-thread list of locks it
  1476. * is holding. Upon do_exit(), the kernel carefully walks this list,
  1477. * and marks all locks that are owned by this thread with the
  1478. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  1479. * always manipulated with the lock held, so the list is private and
  1480. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  1481. * field, to allow the kernel to clean up if the thread dies after
  1482. * acquiring the lock, but just before it could have added itself to
  1483. * the list. There can only be one such pending lock.
  1484. */
  1485. /**
  1486. * sys_set_robust_list - set the robust-futex list head of a task
  1487. * @head: pointer to the list-head
  1488. * @len: length of the list-head, as userspace expects
  1489. */
  1490. asmlinkage long
  1491. sys_set_robust_list(struct robust_list_head __user *head,
  1492. size_t len)
  1493. {
  1494. if (!futex_cmpxchg_enabled)
  1495. return -ENOSYS;
  1496. /*
  1497. * The kernel knows only one size for now:
  1498. */
  1499. if (unlikely(len != sizeof(*head)))
  1500. return -EINVAL;
  1501. current->robust_list = head;
  1502. return 0;
  1503. }
  1504. /**
  1505. * sys_get_robust_list - get the robust-futex list head of a task
  1506. * @pid: pid of the process [zero for current task]
  1507. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  1508. * @len_ptr: pointer to a length field, the kernel fills in the header size
  1509. */
  1510. asmlinkage long
  1511. sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
  1512. size_t __user *len_ptr)
  1513. {
  1514. struct robust_list_head __user *head;
  1515. unsigned long ret;
  1516. if (!futex_cmpxchg_enabled)
  1517. return -ENOSYS;
  1518. if (!pid)
  1519. head = current->robust_list;
  1520. else {
  1521. struct task_struct *p;
  1522. ret = -ESRCH;
  1523. rcu_read_lock();
  1524. p = find_task_by_vpid(pid);
  1525. if (!p)
  1526. goto err_unlock;
  1527. ret = -EPERM;
  1528. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  1529. !capable(CAP_SYS_PTRACE))
  1530. goto err_unlock;
  1531. head = p->robust_list;
  1532. rcu_read_unlock();
  1533. }
  1534. if (put_user(sizeof(*head), len_ptr))
  1535. return -EFAULT;
  1536. return put_user(head, head_ptr);
  1537. err_unlock:
  1538. rcu_read_unlock();
  1539. return ret;
  1540. }
  1541. /*
  1542. * Process a futex-list entry, check whether it's owned by the
  1543. * dying task, and do notification if so:
  1544. */
  1545. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
  1546. {
  1547. u32 uval, nval, mval;
  1548. retry:
  1549. if (get_user(uval, uaddr))
  1550. return -1;
  1551. if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
  1552. /*
  1553. * Ok, this dying thread is truly holding a futex
  1554. * of interest. Set the OWNER_DIED bit atomically
  1555. * via cmpxchg, and if the value had FUTEX_WAITERS
  1556. * set, wake up a waiter (if any). (We have to do a
  1557. * futex_wake() even if OWNER_DIED is already set -
  1558. * to handle the rare but possible case of recursive
  1559. * thread-death.) The rest of the cleanup is done in
  1560. * userspace.
  1561. */
  1562. mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  1563. nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
  1564. if (nval == -EFAULT)
  1565. return -1;
  1566. if (nval != uval)
  1567. goto retry;
  1568. /*
  1569. * Wake robust non-PI futexes here. The wakeup of
  1570. * PI futexes happens in exit_pi_state():
  1571. */
  1572. if (!pi && (uval & FUTEX_WAITERS))
  1573. futex_wake(uaddr, &curr->mm->mmap_sem, 1,
  1574. FUTEX_BITSET_MATCH_ANY);
  1575. }
  1576. return 0;
  1577. }
  1578. /*
  1579. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  1580. */
  1581. static inline int fetch_robust_entry(struct robust_list __user **entry,
  1582. struct robust_list __user * __user *head,
  1583. int *pi)
  1584. {
  1585. unsigned long uentry;
  1586. if (get_user(uentry, (unsigned long __user *)head))
  1587. return -EFAULT;
  1588. *entry = (void __user *)(uentry & ~1UL);
  1589. *pi = uentry & 1;
  1590. return 0;
  1591. }
  1592. /*
  1593. * Walk curr->robust_list (very carefully, it's a userspace list!)
  1594. * and mark any locks found there dead, and notify any waiters.
  1595. *
  1596. * We silently return on any sign of list-walking problem.
  1597. */
  1598. void exit_robust_list(struct task_struct *curr)
  1599. {
  1600. struct robust_list_head __user *head = curr->robust_list;
  1601. struct robust_list __user *entry, *next_entry, *pending;
  1602. unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
  1603. unsigned long futex_offset;
  1604. int rc;
  1605. if (!futex_cmpxchg_enabled)
  1606. return;
  1607. /*
  1608. * Fetch the list head (which was registered earlier, via
  1609. * sys_set_robust_list()):
  1610. */
  1611. if (fetch_robust_entry(&entry, &head->list.next, &pi))
  1612. return;
  1613. /*
  1614. * Fetch the relative futex offset:
  1615. */
  1616. if (get_user(futex_offset, &head->futex_offset))
  1617. return;
  1618. /*
  1619. * Fetch any possibly pending lock-add first, and handle it
  1620. * if it exists:
  1621. */
  1622. if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
  1623. return;
  1624. next_entry = NULL; /* avoid warning with gcc */
  1625. while (entry != &head->list) {
  1626. /*
  1627. * Fetch the next entry in the list before calling
  1628. * handle_futex_death:
  1629. */
  1630. rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
  1631. /*
  1632. * A pending lock might already be on the list, so
  1633. * don't process it twice:
  1634. */
  1635. if (entry != pending)
  1636. if (handle_futex_death((void __user *)entry + futex_offset,
  1637. curr, pi))
  1638. return;
  1639. if (rc)
  1640. return;
  1641. entry = next_entry;
  1642. pi = next_pi;
  1643. /*
  1644. * Avoid excessively long or circular lists:
  1645. */
  1646. if (!--limit)
  1647. break;
  1648. cond_resched();
  1649. }
  1650. if (pending)
  1651. handle_futex_death((void __user *)pending + futex_offset,
  1652. curr, pip);
  1653. }
  1654. long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
  1655. u32 __user *uaddr2, u32 val2, u32 val3)
  1656. {
  1657. int ret = -ENOSYS;
  1658. int cmd = op & FUTEX_CMD_MASK;
  1659. struct rw_semaphore *fshared = NULL;
  1660. if (!(op & FUTEX_PRIVATE_FLAG))
  1661. fshared = &current->mm->mmap_sem;
  1662. switch (cmd) {
  1663. case FUTEX_WAIT:
  1664. val3 = FUTEX_BITSET_MATCH_ANY;
  1665. case FUTEX_WAIT_BITSET:
  1666. ret = futex_wait(uaddr, fshared, val, timeout, val3);
  1667. break;
  1668. case FUTEX_WAKE:
  1669. val3 = FUTEX_BITSET_MATCH_ANY;
  1670. case FUTEX_WAKE_BITSET:
  1671. ret = futex_wake(uaddr, fshared, val, val3);
  1672. break;
  1673. case FUTEX_REQUEUE:
  1674. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
  1675. break;
  1676. case FUTEX_CMP_REQUEUE:
  1677. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
  1678. break;
  1679. case FUTEX_WAKE_OP:
  1680. ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
  1681. break;
  1682. case FUTEX_LOCK_PI:
  1683. if (futex_cmpxchg_enabled)
  1684. ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
  1685. break;
  1686. case FUTEX_UNLOCK_PI:
  1687. if (futex_cmpxchg_enabled)
  1688. ret = futex_unlock_pi(uaddr, fshared);
  1689. break;
  1690. case FUTEX_TRYLOCK_PI:
  1691. if (futex_cmpxchg_enabled)
  1692. ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
  1693. break;
  1694. default:
  1695. ret = -ENOSYS;
  1696. }
  1697. return ret;
  1698. }
  1699. asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
  1700. struct timespec __user *utime, u32 __user *uaddr2,
  1701. u32 val3)
  1702. {
  1703. struct timespec ts;
  1704. ktime_t t, *tp = NULL;
  1705. u32 val2 = 0;
  1706. int cmd = op & FUTEX_CMD_MASK;
  1707. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  1708. cmd == FUTEX_WAIT_BITSET)) {
  1709. if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
  1710. return -EFAULT;
  1711. if (!timespec_valid(&ts))
  1712. return -EINVAL;
  1713. t = timespec_to_ktime(ts);
  1714. if (cmd == FUTEX_WAIT)
  1715. t = ktime_add_safe(ktime_get(), t);
  1716. tp = &t;
  1717. }
  1718. /*
  1719. * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
  1720. * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
  1721. */
  1722. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  1723. cmd == FUTEX_WAKE_OP)
  1724. val2 = (u32) (unsigned long) utime;
  1725. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  1726. }
  1727. static int __init futex_init(void)
  1728. {
  1729. u32 curval;
  1730. int i;
  1731. /*
  1732. * This will fail and we want it. Some arch implementations do
  1733. * runtime detection of the futex_atomic_cmpxchg_inatomic()
  1734. * functionality. We want to know that before we call in any
  1735. * of the complex code paths. Also we want to prevent
  1736. * registration of robust lists in that case. NULL is
  1737. * guaranteed to fault and we get -EFAULT on functional
  1738. * implementation, the non functional ones will return
  1739. * -ENOSYS.
  1740. */
  1741. curval = cmpxchg_futex_value_locked(NULL, 0, 0);
  1742. if (curval == -EFAULT)
  1743. futex_cmpxchg_enabled = 1;
  1744. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  1745. plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
  1746. spin_lock_init(&futex_queues[i].lock);
  1747. }
  1748. return 0;
  1749. }
  1750. __initcall(futex_init);