futex.c 49 KB

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