futex.c 49 KB

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