futex.c 50 KB

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