futex.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593
  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. * Requeue-PI support by Darren Hart <dvhltc@us.ibm.com>
  23. * Copyright (C) IBM Corporation, 2009
  24. * Thanks to Thomas Gleixner for conceptual design and careful reviews.
  25. *
  26. * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
  27. * enough at me, Linus for the original (flawed) idea, Matthew
  28. * Kirkwood for proof-of-concept implementation.
  29. *
  30. * "The futexes are also cursed."
  31. * "But they come in a choice of three flavours!"
  32. *
  33. * This program is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2 of the License, or
  36. * (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program; if not, write to the Free Software
  45. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  46. */
  47. #include <linux/slab.h>
  48. #include <linux/poll.h>
  49. #include <linux/fs.h>
  50. #include <linux/file.h>
  51. #include <linux/jhash.h>
  52. #include <linux/init.h>
  53. #include <linux/futex.h>
  54. #include <linux/mount.h>
  55. #include <linux/pagemap.h>
  56. #include <linux/syscalls.h>
  57. #include <linux/signal.h>
  58. #include <linux/module.h>
  59. #include <linux/magic.h>
  60. #include <linux/pid.h>
  61. #include <linux/nsproxy.h>
  62. #include <asm/futex.h>
  63. #include "rtmutex_common.h"
  64. int __read_mostly futex_cmpxchg_enabled;
  65. #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
  66. /*
  67. * Priority Inheritance state:
  68. */
  69. struct futex_pi_state {
  70. /*
  71. * list of 'owned' pi_state instances - these have to be
  72. * cleaned up in do_exit() if the task exits prematurely:
  73. */
  74. struct list_head list;
  75. /*
  76. * The PI object:
  77. */
  78. struct rt_mutex pi_mutex;
  79. struct task_struct *owner;
  80. atomic_t refcount;
  81. union futex_key key;
  82. };
  83. /*
  84. * We use this hashed waitqueue instead of a normal wait_queue_t, so
  85. * we can wake only the relevant ones (hashed queues may be shared).
  86. *
  87. * A futex_q has a woken state, just like tasks have TASK_RUNNING.
  88. * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
  89. * The order of wakup is always to make the first condition true, then
  90. * wake up q->waiter, then make the second condition true.
  91. */
  92. struct futex_q {
  93. struct plist_node list;
  94. /* Waiter reference */
  95. struct task_struct *task;
  96. /* Which hash list lock to use: */
  97. spinlock_t *lock_ptr;
  98. /* Key which the futex is hashed on: */
  99. union futex_key key;
  100. /* Optional priority inheritance state: */
  101. struct futex_pi_state *pi_state;
  102. /* rt_waiter storage for requeue_pi: */
  103. struct rt_mutex_waiter *rt_waiter;
  104. /* Bitset for the optional bitmasked wakeup */
  105. u32 bitset;
  106. };
  107. /*
  108. * Hash buckets are shared by all the futex_keys that hash to the same
  109. * location. Each key may have multiple futex_q structures, one for each task
  110. * waiting on a futex.
  111. */
  112. struct futex_hash_bucket {
  113. spinlock_t lock;
  114. struct plist_head chain;
  115. };
  116. static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
  117. /*
  118. * We hash on the keys returned from get_futex_key (see below).
  119. */
  120. static struct futex_hash_bucket *hash_futex(union futex_key *key)
  121. {
  122. u32 hash = jhash2((u32*)&key->both.word,
  123. (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
  124. key->both.offset);
  125. return &futex_queues[hash & ((1 << FUTEX_HASHBITS)-1)];
  126. }
  127. /*
  128. * Return 1 if two futex_keys are equal, 0 otherwise.
  129. */
  130. static inline int match_futex(union futex_key *key1, union futex_key *key2)
  131. {
  132. return (key1->both.word == key2->both.word
  133. && key1->both.ptr == key2->both.ptr
  134. && key1->both.offset == key2->both.offset);
  135. }
  136. /*
  137. * Take a reference to the resource addressed by a key.
  138. * Can be called while holding spinlocks.
  139. *
  140. */
  141. static void get_futex_key_refs(union futex_key *key)
  142. {
  143. if (!key->both.ptr)
  144. return;
  145. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  146. case FUT_OFF_INODE:
  147. atomic_inc(&key->shared.inode->i_count);
  148. break;
  149. case FUT_OFF_MMSHARED:
  150. atomic_inc(&key->private.mm->mm_count);
  151. break;
  152. }
  153. }
  154. /*
  155. * Drop a reference to the resource addressed by a key.
  156. * The hash bucket spinlock must not be held.
  157. */
  158. static void drop_futex_key_refs(union futex_key *key)
  159. {
  160. if (!key->both.ptr) {
  161. /* If we're here then we tried to put a key we failed to get */
  162. WARN_ON_ONCE(1);
  163. return;
  164. }
  165. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  166. case FUT_OFF_INODE:
  167. iput(key->shared.inode);
  168. break;
  169. case FUT_OFF_MMSHARED:
  170. mmdrop(key->private.mm);
  171. break;
  172. }
  173. }
  174. /**
  175. * get_futex_key - Get parameters which are the keys for a futex.
  176. * @uaddr: virtual address of the futex
  177. * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED
  178. * @key: address where result is stored.
  179. * @rw: mapping needs to be read/write (values: VERIFY_READ, VERIFY_WRITE)
  180. *
  181. * Returns a negative error code or 0
  182. * The key words are stored in *key on success.
  183. *
  184. * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
  185. * offset_within_page). For private mappings, it's (uaddr, current->mm).
  186. * We can usually work out the index without swapping in the page.
  187. *
  188. * lock_page() might sleep, the caller should not hold a spinlock.
  189. */
  190. static int
  191. get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
  192. {
  193. unsigned long address = (unsigned long)uaddr;
  194. struct mm_struct *mm = current->mm;
  195. struct page *page;
  196. int err;
  197. /*
  198. * The futex address must be "naturally" aligned.
  199. */
  200. key->both.offset = address % PAGE_SIZE;
  201. if (unlikely((address % sizeof(u32)) != 0))
  202. return -EINVAL;
  203. address -= key->both.offset;
  204. /*
  205. * PROCESS_PRIVATE futexes are fast.
  206. * As the mm cannot disappear under us and the 'key' only needs
  207. * virtual address, we dont even have to find the underlying vma.
  208. * Note : We do have to check 'uaddr' is a valid user address,
  209. * but access_ok() should be faster than find_vma()
  210. */
  211. if (!fshared) {
  212. if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
  213. return -EFAULT;
  214. key->private.mm = mm;
  215. key->private.address = address;
  216. get_futex_key_refs(key);
  217. return 0;
  218. }
  219. again:
  220. err = get_user_pages_fast(address, 1, rw == VERIFY_WRITE, &page);
  221. if (err < 0)
  222. return err;
  223. page = compound_head(page);
  224. lock_page(page);
  225. if (!page->mapping) {
  226. unlock_page(page);
  227. put_page(page);
  228. goto again;
  229. }
  230. /*
  231. * Private mappings are handled in a simple way.
  232. *
  233. * NOTE: When userspace waits on a MAP_SHARED mapping, even if
  234. * it's a read-only handle, it's expected that futexes attach to
  235. * the object not the particular process.
  236. */
  237. if (PageAnon(page)) {
  238. key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
  239. key->private.mm = mm;
  240. key->private.address = address;
  241. } else {
  242. key->both.offset |= FUT_OFF_INODE; /* inode-based key */
  243. key->shared.inode = page->mapping->host;
  244. key->shared.pgoff = page->index;
  245. }
  246. get_futex_key_refs(key);
  247. unlock_page(page);
  248. put_page(page);
  249. return 0;
  250. }
  251. static inline
  252. void put_futex_key(int fshared, union futex_key *key)
  253. {
  254. drop_futex_key_refs(key);
  255. }
  256. /*
  257. * fault_in_user_writeable - fault in user address and verify RW access
  258. * @uaddr: pointer to faulting user space address
  259. *
  260. * Slow path to fixup the fault we just took in the atomic write
  261. * access to @uaddr.
  262. *
  263. * We have no generic implementation of a non destructive write to the
  264. * user address. We know that we faulted in the atomic pagefault
  265. * disabled section so we can as well avoid the #PF overhead by
  266. * calling get_user_pages() right away.
  267. */
  268. static int fault_in_user_writeable(u32 __user *uaddr)
  269. {
  270. int ret = get_user_pages(current, current->mm, (unsigned long)uaddr,
  271. 1, 1, 0, NULL, NULL);
  272. return ret < 0 ? ret : 0;
  273. }
  274. /**
  275. * futex_top_waiter() - Return the highest priority waiter on a futex
  276. * @hb: the hash bucket the futex_q's reside in
  277. * @key: the futex key (to distinguish it from other futex futex_q's)
  278. *
  279. * Must be called with the hb lock held.
  280. */
  281. static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
  282. union futex_key *key)
  283. {
  284. struct futex_q *this;
  285. plist_for_each_entry(this, &hb->chain, list) {
  286. if (match_futex(&this->key, key))
  287. return this;
  288. }
  289. return NULL;
  290. }
  291. static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
  292. {
  293. u32 curval;
  294. pagefault_disable();
  295. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  296. pagefault_enable();
  297. return curval;
  298. }
  299. static int get_futex_value_locked(u32 *dest, u32 __user *from)
  300. {
  301. int ret;
  302. pagefault_disable();
  303. ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
  304. pagefault_enable();
  305. return ret ? -EFAULT : 0;
  306. }
  307. /*
  308. * PI code:
  309. */
  310. static int refill_pi_state_cache(void)
  311. {
  312. struct futex_pi_state *pi_state;
  313. if (likely(current->pi_state_cache))
  314. return 0;
  315. pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
  316. if (!pi_state)
  317. return -ENOMEM;
  318. INIT_LIST_HEAD(&pi_state->list);
  319. /* pi_mutex gets initialized later */
  320. pi_state->owner = NULL;
  321. atomic_set(&pi_state->refcount, 1);
  322. pi_state->key = FUTEX_KEY_INIT;
  323. current->pi_state_cache = pi_state;
  324. return 0;
  325. }
  326. static struct futex_pi_state * alloc_pi_state(void)
  327. {
  328. struct futex_pi_state *pi_state = current->pi_state_cache;
  329. WARN_ON(!pi_state);
  330. current->pi_state_cache = NULL;
  331. return pi_state;
  332. }
  333. static void free_pi_state(struct futex_pi_state *pi_state)
  334. {
  335. if (!atomic_dec_and_test(&pi_state->refcount))
  336. return;
  337. /*
  338. * If pi_state->owner is NULL, the owner is most probably dying
  339. * and has cleaned up the pi_state already
  340. */
  341. if (pi_state->owner) {
  342. spin_lock_irq(&pi_state->owner->pi_lock);
  343. list_del_init(&pi_state->list);
  344. spin_unlock_irq(&pi_state->owner->pi_lock);
  345. rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
  346. }
  347. if (current->pi_state_cache)
  348. kfree(pi_state);
  349. else {
  350. /*
  351. * pi_state->list is already empty.
  352. * clear pi_state->owner.
  353. * refcount is at 0 - put it back to 1.
  354. */
  355. pi_state->owner = NULL;
  356. atomic_set(&pi_state->refcount, 1);
  357. current->pi_state_cache = pi_state;
  358. }
  359. }
  360. /*
  361. * Look up the task based on what TID userspace gave us.
  362. * We dont trust it.
  363. */
  364. static struct task_struct * futex_find_get_task(pid_t pid)
  365. {
  366. struct task_struct *p;
  367. const struct cred *cred = current_cred(), *pcred;
  368. rcu_read_lock();
  369. p = find_task_by_vpid(pid);
  370. if (!p) {
  371. p = ERR_PTR(-ESRCH);
  372. } else {
  373. pcred = __task_cred(p);
  374. if (cred->euid != pcred->euid &&
  375. cred->euid != pcred->uid)
  376. p = ERR_PTR(-ESRCH);
  377. else
  378. get_task_struct(p);
  379. }
  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. * futex_lock_pi_atomic() - atomic work required to acquire a pi aware futex
  504. * @uaddr: the pi futex user address
  505. * @hb: the pi futex hash bucket
  506. * @key: the futex key associated with uaddr and hb
  507. * @ps: the pi_state pointer where we store the result of the
  508. * lookup
  509. * @task: the task to perform the atomic lock work for. This will
  510. * be "current" except in the case of requeue pi.
  511. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  512. *
  513. * Returns:
  514. * 0 - ready to wait
  515. * 1 - acquired the lock
  516. * <0 - error
  517. *
  518. * The hb->lock and futex_key refs shall be held by the caller.
  519. */
  520. static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
  521. union futex_key *key,
  522. struct futex_pi_state **ps,
  523. struct task_struct *task, int set_waiters)
  524. {
  525. int lock_taken, ret, ownerdied = 0;
  526. u32 uval, newval, curval;
  527. retry:
  528. ret = lock_taken = 0;
  529. /*
  530. * To avoid races, we attempt to take the lock here again
  531. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  532. * the locks. It will most likely not succeed.
  533. */
  534. newval = task_pid_vnr(task);
  535. if (set_waiters)
  536. newval |= FUTEX_WAITERS;
  537. curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
  538. if (unlikely(curval == -EFAULT))
  539. return -EFAULT;
  540. /*
  541. * Detect deadlocks.
  542. */
  543. if ((unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(task))))
  544. return -EDEADLK;
  545. /*
  546. * Surprise - we got the lock. Just return to userspace:
  547. */
  548. if (unlikely(!curval))
  549. return 1;
  550. uval = curval;
  551. /*
  552. * Set the FUTEX_WAITERS flag, so the owner will know it has someone
  553. * to wake at the next unlock.
  554. */
  555. newval = curval | FUTEX_WAITERS;
  556. /*
  557. * There are two cases, where a futex might have no owner (the
  558. * owner TID is 0): OWNER_DIED. We take over the futex in this
  559. * case. We also do an unconditional take over, when the owner
  560. * of the futex died.
  561. *
  562. * This is safe as we are protected by the hash bucket lock !
  563. */
  564. if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
  565. /* Keep the OWNER_DIED bit */
  566. newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(task);
  567. ownerdied = 0;
  568. lock_taken = 1;
  569. }
  570. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  571. if (unlikely(curval == -EFAULT))
  572. return -EFAULT;
  573. if (unlikely(curval != uval))
  574. goto retry;
  575. /*
  576. * We took the lock due to owner died take over.
  577. */
  578. if (unlikely(lock_taken))
  579. return 1;
  580. /*
  581. * We dont have the lock. Look up the PI state (or create it if
  582. * we are the first waiter):
  583. */
  584. ret = lookup_pi_state(uval, hb, key, ps);
  585. if (unlikely(ret)) {
  586. switch (ret) {
  587. case -ESRCH:
  588. /*
  589. * No owner found for this futex. Check if the
  590. * OWNER_DIED bit is set to figure out whether
  591. * this is a robust futex or not.
  592. */
  593. if (get_futex_value_locked(&curval, uaddr))
  594. return -EFAULT;
  595. /*
  596. * We simply start over in case of a robust
  597. * futex. The code above will take the futex
  598. * and return happy.
  599. */
  600. if (curval & FUTEX_OWNER_DIED) {
  601. ownerdied = 1;
  602. goto retry;
  603. }
  604. default:
  605. break;
  606. }
  607. }
  608. return ret;
  609. }
  610. /*
  611. * The hash bucket lock must be held when this is called.
  612. * Afterwards, the futex_q must not be accessed.
  613. */
  614. static void wake_futex(struct futex_q *q)
  615. {
  616. struct task_struct *p = q->task;
  617. /*
  618. * We set q->lock_ptr = NULL _before_ we wake up the task. If
  619. * a non futex wake up happens on another CPU then the task
  620. * might exit and p would dereference a non existing task
  621. * struct. Prevent this by holding a reference on p across the
  622. * wake up.
  623. */
  624. get_task_struct(p);
  625. plist_del(&q->list, &q->list.plist);
  626. /*
  627. * The waiting task can free the futex_q as soon as
  628. * q->lock_ptr = NULL is written, without taking any locks. A
  629. * memory barrier is required here to prevent the following
  630. * store to lock_ptr from getting ahead of the plist_del.
  631. */
  632. smp_wmb();
  633. q->lock_ptr = NULL;
  634. wake_up_state(p, TASK_NORMAL);
  635. put_task_struct(p);
  636. }
  637. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  638. {
  639. struct task_struct *new_owner;
  640. struct futex_pi_state *pi_state = this->pi_state;
  641. u32 curval, newval;
  642. if (!pi_state)
  643. return -EINVAL;
  644. spin_lock(&pi_state->pi_mutex.wait_lock);
  645. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  646. /*
  647. * This happens when we have stolen the lock and the original
  648. * pending owner did not enqueue itself back on the rt_mutex.
  649. * Thats not a tragedy. We know that way, that a lock waiter
  650. * is on the fly. We make the futex_q waiter the pending owner.
  651. */
  652. if (!new_owner)
  653. new_owner = this->task;
  654. /*
  655. * We pass it to the next owner. (The WAITERS bit is always
  656. * kept enabled while there is PI state around. We must also
  657. * preserve the owner died bit.)
  658. */
  659. if (!(uval & FUTEX_OWNER_DIED)) {
  660. int ret = 0;
  661. newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
  662. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  663. if (curval == -EFAULT)
  664. ret = -EFAULT;
  665. else if (curval != uval)
  666. ret = -EINVAL;
  667. if (ret) {
  668. spin_unlock(&pi_state->pi_mutex.wait_lock);
  669. return ret;
  670. }
  671. }
  672. spin_lock_irq(&pi_state->owner->pi_lock);
  673. WARN_ON(list_empty(&pi_state->list));
  674. list_del_init(&pi_state->list);
  675. spin_unlock_irq(&pi_state->owner->pi_lock);
  676. spin_lock_irq(&new_owner->pi_lock);
  677. WARN_ON(!list_empty(&pi_state->list));
  678. list_add(&pi_state->list, &new_owner->pi_state_list);
  679. pi_state->owner = new_owner;
  680. spin_unlock_irq(&new_owner->pi_lock);
  681. spin_unlock(&pi_state->pi_mutex.wait_lock);
  682. rt_mutex_unlock(&pi_state->pi_mutex);
  683. return 0;
  684. }
  685. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  686. {
  687. u32 oldval;
  688. /*
  689. * There is no waiter, so we unlock the futex. The owner died
  690. * bit has not to be preserved here. We are the owner:
  691. */
  692. oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
  693. if (oldval == -EFAULT)
  694. return oldval;
  695. if (oldval != uval)
  696. return -EAGAIN;
  697. return 0;
  698. }
  699. /*
  700. * Express the locking dependencies for lockdep:
  701. */
  702. static inline void
  703. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  704. {
  705. if (hb1 <= hb2) {
  706. spin_lock(&hb1->lock);
  707. if (hb1 < hb2)
  708. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  709. } else { /* hb1 > hb2 */
  710. spin_lock(&hb2->lock);
  711. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  712. }
  713. }
  714. static inline void
  715. double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  716. {
  717. spin_unlock(&hb1->lock);
  718. if (hb1 != hb2)
  719. spin_unlock(&hb2->lock);
  720. }
  721. /*
  722. * Wake up waiters matching bitset queued on this futex (uaddr).
  723. */
  724. static int futex_wake(u32 __user *uaddr, int fshared, int nr_wake, u32 bitset)
  725. {
  726. struct futex_hash_bucket *hb;
  727. struct futex_q *this, *next;
  728. struct plist_head *head;
  729. union futex_key key = FUTEX_KEY_INIT;
  730. int ret;
  731. if (!bitset)
  732. return -EINVAL;
  733. ret = get_futex_key(uaddr, fshared, &key, VERIFY_READ);
  734. if (unlikely(ret != 0))
  735. goto out;
  736. hb = hash_futex(&key);
  737. spin_lock(&hb->lock);
  738. head = &hb->chain;
  739. plist_for_each_entry_safe(this, next, head, list) {
  740. if (match_futex (&this->key, &key)) {
  741. if (this->pi_state || this->rt_waiter) {
  742. ret = -EINVAL;
  743. break;
  744. }
  745. /* Check if one of the bits is set in both bitsets */
  746. if (!(this->bitset & bitset))
  747. continue;
  748. wake_futex(this);
  749. if (++ret >= nr_wake)
  750. break;
  751. }
  752. }
  753. spin_unlock(&hb->lock);
  754. put_futex_key(fshared, &key);
  755. out:
  756. return ret;
  757. }
  758. /*
  759. * Wake up all waiters hashed on the physical page that is mapped
  760. * to this virtual address:
  761. */
  762. static int
  763. futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
  764. int nr_wake, int nr_wake2, int op)
  765. {
  766. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  767. struct futex_hash_bucket *hb1, *hb2;
  768. struct plist_head *head;
  769. struct futex_q *this, *next;
  770. int ret, op_ret;
  771. retry:
  772. ret = get_futex_key(uaddr1, fshared, &key1, VERIFY_READ);
  773. if (unlikely(ret != 0))
  774. goto out;
  775. ret = get_futex_key(uaddr2, fshared, &key2, VERIFY_WRITE);
  776. if (unlikely(ret != 0))
  777. goto out_put_key1;
  778. hb1 = hash_futex(&key1);
  779. hb2 = hash_futex(&key2);
  780. double_lock_hb(hb1, hb2);
  781. retry_private:
  782. op_ret = futex_atomic_op_inuser(op, uaddr2);
  783. if (unlikely(op_ret < 0)) {
  784. double_unlock_hb(hb1, hb2);
  785. #ifndef CONFIG_MMU
  786. /*
  787. * we don't get EFAULT from MMU faults if we don't have an MMU,
  788. * but we might get them from range checking
  789. */
  790. ret = op_ret;
  791. goto out_put_keys;
  792. #endif
  793. if (unlikely(op_ret != -EFAULT)) {
  794. ret = op_ret;
  795. goto out_put_keys;
  796. }
  797. ret = fault_in_user_writeable(uaddr2);
  798. if (ret)
  799. goto out_put_keys;
  800. if (!fshared)
  801. goto retry_private;
  802. put_futex_key(fshared, &key2);
  803. put_futex_key(fshared, &key1);
  804. goto retry;
  805. }
  806. head = &hb1->chain;
  807. plist_for_each_entry_safe(this, next, head, list) {
  808. if (match_futex (&this->key, &key1)) {
  809. wake_futex(this);
  810. if (++ret >= nr_wake)
  811. break;
  812. }
  813. }
  814. if (op_ret > 0) {
  815. head = &hb2->chain;
  816. op_ret = 0;
  817. plist_for_each_entry_safe(this, next, head, list) {
  818. if (match_futex (&this->key, &key2)) {
  819. wake_futex(this);
  820. if (++op_ret >= nr_wake2)
  821. break;
  822. }
  823. }
  824. ret += op_ret;
  825. }
  826. double_unlock_hb(hb1, hb2);
  827. out_put_keys:
  828. put_futex_key(fshared, &key2);
  829. out_put_key1:
  830. put_futex_key(fshared, &key1);
  831. out:
  832. return ret;
  833. }
  834. /**
  835. * requeue_futex() - Requeue a futex_q from one hb to another
  836. * @q: the futex_q to requeue
  837. * @hb1: the source hash_bucket
  838. * @hb2: the target hash_bucket
  839. * @key2: the new key for the requeued futex_q
  840. */
  841. static inline
  842. void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
  843. struct futex_hash_bucket *hb2, union futex_key *key2)
  844. {
  845. /*
  846. * If key1 and key2 hash to the same bucket, no need to
  847. * requeue.
  848. */
  849. if (likely(&hb1->chain != &hb2->chain)) {
  850. plist_del(&q->list, &hb1->chain);
  851. plist_add(&q->list, &hb2->chain);
  852. q->lock_ptr = &hb2->lock;
  853. #ifdef CONFIG_DEBUG_PI_LIST
  854. q->list.plist.lock = &hb2->lock;
  855. #endif
  856. }
  857. get_futex_key_refs(key2);
  858. q->key = *key2;
  859. }
  860. /**
  861. * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
  862. * q: the futex_q
  863. * key: the key of the requeue target futex
  864. *
  865. * During futex_requeue, with requeue_pi=1, it is possible to acquire the
  866. * target futex if it is uncontended or via a lock steal. Set the futex_q key
  867. * to the requeue target futex so the waiter can detect the wakeup on the right
  868. * futex, but remove it from the hb and NULL the rt_waiter so it can detect
  869. * atomic lock acquisition. Must be called with the q->lock_ptr held.
  870. */
  871. static inline
  872. void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key)
  873. {
  874. drop_futex_key_refs(&q->key);
  875. get_futex_key_refs(key);
  876. q->key = *key;
  877. WARN_ON(plist_node_empty(&q->list));
  878. plist_del(&q->list, &q->list.plist);
  879. WARN_ON(!q->rt_waiter);
  880. q->rt_waiter = NULL;
  881. wake_up_state(q->task, TASK_NORMAL);
  882. }
  883. /**
  884. * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
  885. * @pifutex: the user address of the to futex
  886. * @hb1: the from futex hash bucket, must be locked by the caller
  887. * @hb2: the to futex hash bucket, must be locked by the caller
  888. * @key1: the from futex key
  889. * @key2: the to futex key
  890. * @ps: address to store the pi_state pointer
  891. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  892. *
  893. * Try and get the lock on behalf of the top waiter if we can do it atomically.
  894. * Wake the top waiter if we succeed. If the caller specified set_waiters,
  895. * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
  896. * hb1 and hb2 must be held by the caller.
  897. *
  898. * Returns:
  899. * 0 - failed to acquire the lock atomicly
  900. * 1 - acquired the lock
  901. * <0 - error
  902. */
  903. static int futex_proxy_trylock_atomic(u32 __user *pifutex,
  904. struct futex_hash_bucket *hb1,
  905. struct futex_hash_bucket *hb2,
  906. union futex_key *key1, union futex_key *key2,
  907. struct futex_pi_state **ps, int set_waiters)
  908. {
  909. struct futex_q *top_waiter = NULL;
  910. u32 curval;
  911. int ret;
  912. if (get_futex_value_locked(&curval, pifutex))
  913. return -EFAULT;
  914. /*
  915. * Find the top_waiter and determine if there are additional waiters.
  916. * If the caller intends to requeue more than 1 waiter to pifutex,
  917. * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
  918. * as we have means to handle the possible fault. If not, don't set
  919. * the bit unecessarily as it will force the subsequent unlock to enter
  920. * the kernel.
  921. */
  922. top_waiter = futex_top_waiter(hb1, key1);
  923. /* There are no waiters, nothing for us to do. */
  924. if (!top_waiter)
  925. return 0;
  926. /*
  927. * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
  928. * the contended case or if set_waiters is 1. The pi_state is returned
  929. * in ps in contended cases.
  930. */
  931. ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
  932. set_waiters);
  933. if (ret == 1)
  934. requeue_pi_wake_futex(top_waiter, key2);
  935. return ret;
  936. }
  937. /**
  938. * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
  939. * uaddr1: source futex user address
  940. * uaddr2: target futex user address
  941. * nr_wake: number of waiters to wake (must be 1 for requeue_pi)
  942. * nr_requeue: number of waiters to requeue (0-INT_MAX)
  943. * requeue_pi: if we are attempting to requeue from a non-pi futex to a
  944. * pi futex (pi to pi requeue is not supported)
  945. *
  946. * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
  947. * uaddr2 atomically on behalf of the top waiter.
  948. *
  949. * Returns:
  950. * >=0 - on success, the number of tasks requeued or woken
  951. * <0 - on error
  952. */
  953. static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
  954. int nr_wake, int nr_requeue, u32 *cmpval,
  955. int requeue_pi)
  956. {
  957. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  958. int drop_count = 0, task_count = 0, ret;
  959. struct futex_pi_state *pi_state = NULL;
  960. struct futex_hash_bucket *hb1, *hb2;
  961. struct plist_head *head1;
  962. struct futex_q *this, *next;
  963. u32 curval2;
  964. if (requeue_pi) {
  965. /*
  966. * requeue_pi requires a pi_state, try to allocate it now
  967. * without any locks in case it fails.
  968. */
  969. if (refill_pi_state_cache())
  970. return -ENOMEM;
  971. /*
  972. * requeue_pi must wake as many tasks as it can, up to nr_wake
  973. * + nr_requeue, since it acquires the rt_mutex prior to
  974. * returning to userspace, so as to not leave the rt_mutex with
  975. * waiters and no owner. However, second and third wake-ups
  976. * cannot be predicted as they involve race conditions with the
  977. * first wake and a fault while looking up the pi_state. Both
  978. * pthread_cond_signal() and pthread_cond_broadcast() should
  979. * use nr_wake=1.
  980. */
  981. if (nr_wake != 1)
  982. return -EINVAL;
  983. }
  984. retry:
  985. if (pi_state != NULL) {
  986. /*
  987. * We will have to lookup the pi_state again, so free this one
  988. * to keep the accounting correct.
  989. */
  990. free_pi_state(pi_state);
  991. pi_state = NULL;
  992. }
  993. ret = get_futex_key(uaddr1, fshared, &key1, VERIFY_READ);
  994. if (unlikely(ret != 0))
  995. goto out;
  996. ret = get_futex_key(uaddr2, fshared, &key2,
  997. requeue_pi ? VERIFY_WRITE : VERIFY_READ);
  998. if (unlikely(ret != 0))
  999. goto out_put_key1;
  1000. hb1 = hash_futex(&key1);
  1001. hb2 = hash_futex(&key2);
  1002. retry_private:
  1003. double_lock_hb(hb1, hb2);
  1004. if (likely(cmpval != NULL)) {
  1005. u32 curval;
  1006. ret = get_futex_value_locked(&curval, uaddr1);
  1007. if (unlikely(ret)) {
  1008. double_unlock_hb(hb1, hb2);
  1009. ret = get_user(curval, uaddr1);
  1010. if (ret)
  1011. goto out_put_keys;
  1012. if (!fshared)
  1013. goto retry_private;
  1014. put_futex_key(fshared, &key2);
  1015. put_futex_key(fshared, &key1);
  1016. goto retry;
  1017. }
  1018. if (curval != *cmpval) {
  1019. ret = -EAGAIN;
  1020. goto out_unlock;
  1021. }
  1022. }
  1023. if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
  1024. /*
  1025. * Attempt to acquire uaddr2 and wake the top waiter. If we
  1026. * intend to requeue waiters, force setting the FUTEX_WAITERS
  1027. * bit. We force this here where we are able to easily handle
  1028. * faults rather in the requeue loop below.
  1029. */
  1030. ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
  1031. &key2, &pi_state, nr_requeue);
  1032. /*
  1033. * At this point the top_waiter has either taken uaddr2 or is
  1034. * waiting on it. If the former, then the pi_state will not
  1035. * exist yet, look it up one more time to ensure we have a
  1036. * reference to it.
  1037. */
  1038. if (ret == 1) {
  1039. WARN_ON(pi_state);
  1040. task_count++;
  1041. ret = get_futex_value_locked(&curval2, uaddr2);
  1042. if (!ret)
  1043. ret = lookup_pi_state(curval2, hb2, &key2,
  1044. &pi_state);
  1045. }
  1046. switch (ret) {
  1047. case 0:
  1048. break;
  1049. case -EFAULT:
  1050. double_unlock_hb(hb1, hb2);
  1051. put_futex_key(fshared, &key2);
  1052. put_futex_key(fshared, &key1);
  1053. ret = fault_in_user_writeable(uaddr2);
  1054. if (!ret)
  1055. goto retry;
  1056. goto out;
  1057. case -EAGAIN:
  1058. /* The owner was exiting, try again. */
  1059. double_unlock_hb(hb1, hb2);
  1060. put_futex_key(fshared, &key2);
  1061. put_futex_key(fshared, &key1);
  1062. cond_resched();
  1063. goto retry;
  1064. default:
  1065. goto out_unlock;
  1066. }
  1067. }
  1068. head1 = &hb1->chain;
  1069. plist_for_each_entry_safe(this, next, head1, list) {
  1070. if (task_count - nr_wake >= nr_requeue)
  1071. break;
  1072. if (!match_futex(&this->key, &key1))
  1073. continue;
  1074. WARN_ON(!requeue_pi && this->rt_waiter);
  1075. WARN_ON(requeue_pi && !this->rt_waiter);
  1076. /*
  1077. * Wake nr_wake waiters. For requeue_pi, if we acquired the
  1078. * lock, we already woke the top_waiter. If not, it will be
  1079. * woken by futex_unlock_pi().
  1080. */
  1081. if (++task_count <= nr_wake && !requeue_pi) {
  1082. wake_futex(this);
  1083. continue;
  1084. }
  1085. /*
  1086. * Requeue nr_requeue waiters and possibly one more in the case
  1087. * of requeue_pi if we couldn't acquire the lock atomically.
  1088. */
  1089. if (requeue_pi) {
  1090. /* Prepare the waiter to take the rt_mutex. */
  1091. atomic_inc(&pi_state->refcount);
  1092. this->pi_state = pi_state;
  1093. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  1094. this->rt_waiter,
  1095. this->task, 1);
  1096. if (ret == 1) {
  1097. /* We got the lock. */
  1098. requeue_pi_wake_futex(this, &key2);
  1099. continue;
  1100. } else if (ret) {
  1101. /* -EDEADLK */
  1102. this->pi_state = NULL;
  1103. free_pi_state(pi_state);
  1104. goto out_unlock;
  1105. }
  1106. }
  1107. requeue_futex(this, hb1, hb2, &key2);
  1108. drop_count++;
  1109. }
  1110. out_unlock:
  1111. double_unlock_hb(hb1, hb2);
  1112. /*
  1113. * drop_futex_key_refs() must be called outside the spinlocks. During
  1114. * the requeue we moved futex_q's from the hash bucket at key1 to the
  1115. * one at key2 and updated their key pointer. We no longer need to
  1116. * hold the references to key1.
  1117. */
  1118. while (--drop_count >= 0)
  1119. drop_futex_key_refs(&key1);
  1120. out_put_keys:
  1121. put_futex_key(fshared, &key2);
  1122. out_put_key1:
  1123. put_futex_key(fshared, &key1);
  1124. out:
  1125. if (pi_state != NULL)
  1126. free_pi_state(pi_state);
  1127. return ret ? ret : task_count;
  1128. }
  1129. /* The key must be already stored in q->key. */
  1130. static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
  1131. {
  1132. struct futex_hash_bucket *hb;
  1133. get_futex_key_refs(&q->key);
  1134. hb = hash_futex(&q->key);
  1135. q->lock_ptr = &hb->lock;
  1136. spin_lock(&hb->lock);
  1137. return hb;
  1138. }
  1139. static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  1140. {
  1141. int prio;
  1142. /*
  1143. * The priority used to register this element is
  1144. * - either the real thread-priority for the real-time threads
  1145. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  1146. * - or MAX_RT_PRIO for non-RT threads.
  1147. * Thus, all RT-threads are woken first in priority order, and
  1148. * the others are woken last, in FIFO order.
  1149. */
  1150. prio = min(current->normal_prio, MAX_RT_PRIO);
  1151. plist_node_init(&q->list, prio);
  1152. #ifdef CONFIG_DEBUG_PI_LIST
  1153. q->list.plist.lock = &hb->lock;
  1154. #endif
  1155. plist_add(&q->list, &hb->chain);
  1156. q->task = current;
  1157. spin_unlock(&hb->lock);
  1158. }
  1159. static inline void
  1160. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  1161. {
  1162. spin_unlock(&hb->lock);
  1163. drop_futex_key_refs(&q->key);
  1164. }
  1165. /*
  1166. * queue_me and unqueue_me must be called as a pair, each
  1167. * exactly once. They are called with the hashed spinlock held.
  1168. */
  1169. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  1170. static int unqueue_me(struct futex_q *q)
  1171. {
  1172. spinlock_t *lock_ptr;
  1173. int ret = 0;
  1174. /* In the common case we don't take the spinlock, which is nice. */
  1175. retry:
  1176. lock_ptr = q->lock_ptr;
  1177. barrier();
  1178. if (lock_ptr != NULL) {
  1179. spin_lock(lock_ptr);
  1180. /*
  1181. * q->lock_ptr can change between reading it and
  1182. * spin_lock(), causing us to take the wrong lock. This
  1183. * corrects the race condition.
  1184. *
  1185. * Reasoning goes like this: if we have the wrong lock,
  1186. * q->lock_ptr must have changed (maybe several times)
  1187. * between reading it and the spin_lock(). It can
  1188. * change again after the spin_lock() but only if it was
  1189. * already changed before the spin_lock(). It cannot,
  1190. * however, change back to the original value. Therefore
  1191. * we can detect whether we acquired the correct lock.
  1192. */
  1193. if (unlikely(lock_ptr != q->lock_ptr)) {
  1194. spin_unlock(lock_ptr);
  1195. goto retry;
  1196. }
  1197. WARN_ON(plist_node_empty(&q->list));
  1198. plist_del(&q->list, &q->list.plist);
  1199. BUG_ON(q->pi_state);
  1200. spin_unlock(lock_ptr);
  1201. ret = 1;
  1202. }
  1203. drop_futex_key_refs(&q->key);
  1204. return ret;
  1205. }
  1206. /*
  1207. * PI futexes can not be requeued and must remove themself from the
  1208. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  1209. * and dropped here.
  1210. */
  1211. static void unqueue_me_pi(struct futex_q *q)
  1212. {
  1213. WARN_ON(plist_node_empty(&q->list));
  1214. plist_del(&q->list, &q->list.plist);
  1215. BUG_ON(!q->pi_state);
  1216. free_pi_state(q->pi_state);
  1217. q->pi_state = NULL;
  1218. spin_unlock(q->lock_ptr);
  1219. drop_futex_key_refs(&q->key);
  1220. }
  1221. /*
  1222. * Fixup the pi_state owner with the new owner.
  1223. *
  1224. * Must be called with hash bucket lock held and mm->sem held for non
  1225. * private futexes.
  1226. */
  1227. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  1228. struct task_struct *newowner, int fshared)
  1229. {
  1230. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  1231. struct futex_pi_state *pi_state = q->pi_state;
  1232. struct task_struct *oldowner = pi_state->owner;
  1233. u32 uval, curval, newval;
  1234. int ret;
  1235. /* Owner died? */
  1236. if (!pi_state->owner)
  1237. newtid |= FUTEX_OWNER_DIED;
  1238. /*
  1239. * We are here either because we stole the rtmutex from the
  1240. * pending owner or we are the pending owner which failed to
  1241. * get the rtmutex. We have to replace the pending owner TID
  1242. * in the user space variable. This must be atomic as we have
  1243. * to preserve the owner died bit here.
  1244. *
  1245. * Note: We write the user space value _before_ changing the pi_state
  1246. * because we can fault here. Imagine swapped out pages or a fork
  1247. * that marked all the anonymous memory readonly for cow.
  1248. *
  1249. * Modifying pi_state _before_ the user space value would
  1250. * leave the pi_state in an inconsistent state when we fault
  1251. * here, because we need to drop the hash bucket lock to
  1252. * handle the fault. This might be observed in the PID check
  1253. * in lookup_pi_state.
  1254. */
  1255. retry:
  1256. if (get_futex_value_locked(&uval, uaddr))
  1257. goto handle_fault;
  1258. while (1) {
  1259. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  1260. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  1261. if (curval == -EFAULT)
  1262. goto handle_fault;
  1263. if (curval == uval)
  1264. break;
  1265. uval = curval;
  1266. }
  1267. /*
  1268. * We fixed up user space. Now we need to fix the pi_state
  1269. * itself.
  1270. */
  1271. if (pi_state->owner != NULL) {
  1272. spin_lock_irq(&pi_state->owner->pi_lock);
  1273. WARN_ON(list_empty(&pi_state->list));
  1274. list_del_init(&pi_state->list);
  1275. spin_unlock_irq(&pi_state->owner->pi_lock);
  1276. }
  1277. pi_state->owner = newowner;
  1278. spin_lock_irq(&newowner->pi_lock);
  1279. WARN_ON(!list_empty(&pi_state->list));
  1280. list_add(&pi_state->list, &newowner->pi_state_list);
  1281. spin_unlock_irq(&newowner->pi_lock);
  1282. return 0;
  1283. /*
  1284. * To handle the page fault we need to drop the hash bucket
  1285. * lock here. That gives the other task (either the pending
  1286. * owner itself or the task which stole the rtmutex) the
  1287. * chance to try the fixup of the pi_state. So once we are
  1288. * back from handling the fault we need to check the pi_state
  1289. * after reacquiring the hash bucket lock and before trying to
  1290. * do another fixup. When the fixup has been done already we
  1291. * simply return.
  1292. */
  1293. handle_fault:
  1294. spin_unlock(q->lock_ptr);
  1295. ret = fault_in_user_writeable(uaddr);
  1296. spin_lock(q->lock_ptr);
  1297. /*
  1298. * Check if someone else fixed it for us:
  1299. */
  1300. if (pi_state->owner != oldowner)
  1301. return 0;
  1302. if (ret)
  1303. return ret;
  1304. goto retry;
  1305. }
  1306. /*
  1307. * In case we must use restart_block to restart a futex_wait,
  1308. * we encode in the 'flags' shared capability
  1309. */
  1310. #define FLAGS_SHARED 0x01
  1311. #define FLAGS_CLOCKRT 0x02
  1312. #define FLAGS_HAS_TIMEOUT 0x04
  1313. static long futex_wait_restart(struct restart_block *restart);
  1314. /**
  1315. * fixup_owner() - Post lock pi_state and corner case management
  1316. * @uaddr: user address of the futex
  1317. * @fshared: whether the futex is shared (1) or not (0)
  1318. * @q: futex_q (contains pi_state and access to the rt_mutex)
  1319. * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
  1320. *
  1321. * After attempting to lock an rt_mutex, this function is called to cleanup
  1322. * the pi_state owner as well as handle race conditions that may allow us to
  1323. * acquire the lock. Must be called with the hb lock held.
  1324. *
  1325. * Returns:
  1326. * 1 - success, lock taken
  1327. * 0 - success, lock not taken
  1328. * <0 - on error (-EFAULT)
  1329. */
  1330. static int fixup_owner(u32 __user *uaddr, int fshared, struct futex_q *q,
  1331. int locked)
  1332. {
  1333. struct task_struct *owner;
  1334. int ret = 0;
  1335. if (locked) {
  1336. /*
  1337. * Got the lock. We might not be the anticipated owner if we
  1338. * did a lock-steal - fix up the PI-state in that case:
  1339. */
  1340. if (q->pi_state->owner != current)
  1341. ret = fixup_pi_state_owner(uaddr, q, current, fshared);
  1342. goto out;
  1343. }
  1344. /*
  1345. * Catch the rare case, where the lock was released when we were on the
  1346. * way back before we locked the hash bucket.
  1347. */
  1348. if (q->pi_state->owner == current) {
  1349. /*
  1350. * Try to get the rt_mutex now. This might fail as some other
  1351. * task acquired the rt_mutex after we removed ourself from the
  1352. * rt_mutex waiters list.
  1353. */
  1354. if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
  1355. locked = 1;
  1356. goto out;
  1357. }
  1358. /*
  1359. * pi_state is incorrect, some other task did a lock steal and
  1360. * we returned due to timeout or signal without taking the
  1361. * rt_mutex. Too late. We can access the rt_mutex_owner without
  1362. * locking, as the other task is now blocked on the hash bucket
  1363. * lock. Fix the state up.
  1364. */
  1365. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  1366. ret = fixup_pi_state_owner(uaddr, q, owner, fshared);
  1367. goto out;
  1368. }
  1369. /*
  1370. * Paranoia check. If we did not take the lock, then we should not be
  1371. * the owner, nor the pending owner, of the rt_mutex.
  1372. */
  1373. if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
  1374. printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
  1375. "pi-state %p\n", ret,
  1376. q->pi_state->pi_mutex.owner,
  1377. q->pi_state->owner);
  1378. out:
  1379. return ret ? ret : locked;
  1380. }
  1381. /**
  1382. * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
  1383. * @hb: the futex hash bucket, must be locked by the caller
  1384. * @q: the futex_q to queue up on
  1385. * @timeout: the prepared hrtimer_sleeper, or null for no timeout
  1386. */
  1387. static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
  1388. struct hrtimer_sleeper *timeout)
  1389. {
  1390. queue_me(q, hb);
  1391. /*
  1392. * There might have been scheduling since the queue_me(), as we
  1393. * cannot hold a spinlock across the get_user() in case it
  1394. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  1395. * queueing ourselves into the futex hash. This code thus has to
  1396. * rely on the futex_wake() code removing us from hash when it
  1397. * wakes us up.
  1398. */
  1399. set_current_state(TASK_INTERRUPTIBLE);
  1400. /* Arm the timer */
  1401. if (timeout) {
  1402. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1403. if (!hrtimer_active(&timeout->timer))
  1404. timeout->task = NULL;
  1405. }
  1406. /*
  1407. * !plist_node_empty() is safe here without any lock.
  1408. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  1409. */
  1410. if (likely(!plist_node_empty(&q->list))) {
  1411. /*
  1412. * If the timer has already expired, current will already be
  1413. * flagged for rescheduling. Only call schedule if there
  1414. * is no timeout, or if it has yet to expire.
  1415. */
  1416. if (!timeout || timeout->task)
  1417. schedule();
  1418. }
  1419. __set_current_state(TASK_RUNNING);
  1420. }
  1421. /**
  1422. * futex_wait_setup() - Prepare to wait on a futex
  1423. * @uaddr: the futex userspace address
  1424. * @val: the expected value
  1425. * @fshared: whether the futex is shared (1) or not (0)
  1426. * @q: the associated futex_q
  1427. * @hb: storage for hash_bucket pointer to be returned to caller
  1428. *
  1429. * Setup the futex_q and locate the hash_bucket. Get the futex value and
  1430. * compare it with the expected value. Handle atomic faults internally.
  1431. * Return with the hb lock held and a q.key reference on success, and unlocked
  1432. * with no q.key reference on failure.
  1433. *
  1434. * Returns:
  1435. * 0 - uaddr contains val and hb has been locked
  1436. * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlcoked
  1437. */
  1438. static int futex_wait_setup(u32 __user *uaddr, u32 val, int fshared,
  1439. struct futex_q *q, struct futex_hash_bucket **hb)
  1440. {
  1441. u32 uval;
  1442. int ret;
  1443. /*
  1444. * Access the page AFTER the hash-bucket is locked.
  1445. * Order is important:
  1446. *
  1447. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  1448. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  1449. *
  1450. * The basic logical guarantee of a futex is that it blocks ONLY
  1451. * if cond(var) is known to be true at the time of blocking, for
  1452. * any cond. If we queued after testing *uaddr, that would open
  1453. * a race condition where we could block indefinitely with
  1454. * cond(var) false, which would violate the guarantee.
  1455. *
  1456. * A consequence is that futex_wait() can return zero and absorb
  1457. * a wakeup when *uaddr != val on entry to the syscall. This is
  1458. * rare, but normal.
  1459. */
  1460. retry:
  1461. q->key = FUTEX_KEY_INIT;
  1462. ret = get_futex_key(uaddr, fshared, &q->key, VERIFY_READ);
  1463. if (unlikely(ret != 0))
  1464. return ret;
  1465. retry_private:
  1466. *hb = queue_lock(q);
  1467. ret = get_futex_value_locked(&uval, uaddr);
  1468. if (ret) {
  1469. queue_unlock(q, *hb);
  1470. ret = get_user(uval, uaddr);
  1471. if (ret)
  1472. goto out;
  1473. if (!fshared)
  1474. goto retry_private;
  1475. put_futex_key(fshared, &q->key);
  1476. goto retry;
  1477. }
  1478. if (uval != val) {
  1479. queue_unlock(q, *hb);
  1480. ret = -EWOULDBLOCK;
  1481. }
  1482. out:
  1483. if (ret)
  1484. put_futex_key(fshared, &q->key);
  1485. return ret;
  1486. }
  1487. static int futex_wait(u32 __user *uaddr, int fshared,
  1488. u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
  1489. {
  1490. struct hrtimer_sleeper timeout, *to = NULL;
  1491. struct restart_block *restart;
  1492. struct futex_hash_bucket *hb;
  1493. struct futex_q q;
  1494. int ret;
  1495. if (!bitset)
  1496. return -EINVAL;
  1497. q.pi_state = NULL;
  1498. q.bitset = bitset;
  1499. q.rt_waiter = NULL;
  1500. if (abs_time) {
  1501. to = &timeout;
  1502. hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
  1503. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1504. hrtimer_init_sleeper(to, current);
  1505. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  1506. current->timer_slack_ns);
  1507. }
  1508. /* Prepare to wait on uaddr. */
  1509. ret = futex_wait_setup(uaddr, val, fshared, &q, &hb);
  1510. if (ret)
  1511. goto out;
  1512. /* queue_me and wait for wakeup, timeout, or a signal. */
  1513. futex_wait_queue_me(hb, &q, to);
  1514. /* If we were woken (and unqueued), we succeeded, whatever. */
  1515. ret = 0;
  1516. if (!unqueue_me(&q))
  1517. goto out_put_key;
  1518. ret = -ETIMEDOUT;
  1519. if (to && !to->task)
  1520. goto out_put_key;
  1521. /*
  1522. * We expect signal_pending(current), but another thread may
  1523. * have handled it for us already.
  1524. */
  1525. ret = -ERESTARTSYS;
  1526. if (!abs_time)
  1527. goto out_put_key;
  1528. restart = &current_thread_info()->restart_block;
  1529. restart->fn = futex_wait_restart;
  1530. restart->futex.uaddr = (u32 *)uaddr;
  1531. restart->futex.val = val;
  1532. restart->futex.time = abs_time->tv64;
  1533. restart->futex.bitset = bitset;
  1534. restart->futex.flags = FLAGS_HAS_TIMEOUT;
  1535. if (fshared)
  1536. restart->futex.flags |= FLAGS_SHARED;
  1537. if (clockrt)
  1538. restart->futex.flags |= FLAGS_CLOCKRT;
  1539. ret = -ERESTART_RESTARTBLOCK;
  1540. out_put_key:
  1541. put_futex_key(fshared, &q.key);
  1542. out:
  1543. if (to) {
  1544. hrtimer_cancel(&to->timer);
  1545. destroy_hrtimer_on_stack(&to->timer);
  1546. }
  1547. return ret;
  1548. }
  1549. static long futex_wait_restart(struct restart_block *restart)
  1550. {
  1551. u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
  1552. int fshared = 0;
  1553. ktime_t t, *tp = NULL;
  1554. if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
  1555. t.tv64 = restart->futex.time;
  1556. tp = &t;
  1557. }
  1558. restart->fn = do_no_restart_syscall;
  1559. if (restart->futex.flags & FLAGS_SHARED)
  1560. fshared = 1;
  1561. return (long)futex_wait(uaddr, fshared, restart->futex.val, tp,
  1562. restart->futex.bitset,
  1563. restart->futex.flags & FLAGS_CLOCKRT);
  1564. }
  1565. /*
  1566. * Userspace tried a 0 -> TID atomic transition of the futex value
  1567. * and failed. The kernel side here does the whole locking operation:
  1568. * if there are waiters then it will block, it does PI, etc. (Due to
  1569. * races the kernel might see a 0 value of the futex too.)
  1570. */
  1571. static int futex_lock_pi(u32 __user *uaddr, int fshared,
  1572. int detect, ktime_t *time, int trylock)
  1573. {
  1574. struct hrtimer_sleeper timeout, *to = NULL;
  1575. struct futex_hash_bucket *hb;
  1576. struct futex_q q;
  1577. int res, ret;
  1578. if (refill_pi_state_cache())
  1579. return -ENOMEM;
  1580. if (time) {
  1581. to = &timeout;
  1582. hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
  1583. HRTIMER_MODE_ABS);
  1584. hrtimer_init_sleeper(to, current);
  1585. hrtimer_set_expires(&to->timer, *time);
  1586. }
  1587. q.pi_state = NULL;
  1588. q.rt_waiter = NULL;
  1589. retry:
  1590. q.key = FUTEX_KEY_INIT;
  1591. ret = get_futex_key(uaddr, fshared, &q.key, VERIFY_WRITE);
  1592. if (unlikely(ret != 0))
  1593. goto out;
  1594. retry_private:
  1595. hb = queue_lock(&q);
  1596. ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
  1597. if (unlikely(ret)) {
  1598. switch (ret) {
  1599. case 1:
  1600. /* We got the lock. */
  1601. ret = 0;
  1602. goto out_unlock_put_key;
  1603. case -EFAULT:
  1604. goto uaddr_faulted;
  1605. case -EAGAIN:
  1606. /*
  1607. * Task is exiting and we just wait for the
  1608. * exit to complete.
  1609. */
  1610. queue_unlock(&q, hb);
  1611. put_futex_key(fshared, &q.key);
  1612. cond_resched();
  1613. goto retry;
  1614. default:
  1615. goto out_unlock_put_key;
  1616. }
  1617. }
  1618. /*
  1619. * Only actually queue now that the atomic ops are done:
  1620. */
  1621. queue_me(&q, hb);
  1622. WARN_ON(!q.pi_state);
  1623. /*
  1624. * Block on the PI mutex:
  1625. */
  1626. if (!trylock)
  1627. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1628. else {
  1629. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1630. /* Fixup the trylock return value: */
  1631. ret = ret ? 0 : -EWOULDBLOCK;
  1632. }
  1633. spin_lock(q.lock_ptr);
  1634. /*
  1635. * Fixup the pi_state owner and possibly acquire the lock if we
  1636. * haven't already.
  1637. */
  1638. res = fixup_owner(uaddr, fshared, &q, !ret);
  1639. /*
  1640. * If fixup_owner() returned an error, proprogate that. If it acquired
  1641. * the lock, clear our -ETIMEDOUT or -EINTR.
  1642. */
  1643. if (res)
  1644. ret = (res < 0) ? res : 0;
  1645. /*
  1646. * If fixup_owner() faulted and was unable to handle the fault, unlock
  1647. * it and return the fault to userspace.
  1648. */
  1649. if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
  1650. rt_mutex_unlock(&q.pi_state->pi_mutex);
  1651. /* Unqueue and drop the lock */
  1652. unqueue_me_pi(&q);
  1653. goto out;
  1654. out_unlock_put_key:
  1655. queue_unlock(&q, hb);
  1656. out_put_key:
  1657. put_futex_key(fshared, &q.key);
  1658. out:
  1659. if (to)
  1660. destroy_hrtimer_on_stack(&to->timer);
  1661. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  1662. uaddr_faulted:
  1663. queue_unlock(&q, hb);
  1664. ret = fault_in_user_writeable(uaddr);
  1665. if (ret)
  1666. goto out_put_key;
  1667. if (!fshared)
  1668. goto retry_private;
  1669. put_futex_key(fshared, &q.key);
  1670. goto retry;
  1671. }
  1672. /*
  1673. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1674. * This is the in-kernel slowpath: we look up the PI state (if any),
  1675. * and do the rt-mutex unlock.
  1676. */
  1677. static int futex_unlock_pi(u32 __user *uaddr, int fshared)
  1678. {
  1679. struct futex_hash_bucket *hb;
  1680. struct futex_q *this, *next;
  1681. u32 uval;
  1682. struct plist_head *head;
  1683. union futex_key key = FUTEX_KEY_INIT;
  1684. int ret;
  1685. retry:
  1686. if (get_user(uval, uaddr))
  1687. return -EFAULT;
  1688. /*
  1689. * We release only a lock we actually own:
  1690. */
  1691. if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
  1692. return -EPERM;
  1693. ret = get_futex_key(uaddr, fshared, &key, VERIFY_WRITE);
  1694. if (unlikely(ret != 0))
  1695. goto out;
  1696. hb = hash_futex(&key);
  1697. spin_lock(&hb->lock);
  1698. /*
  1699. * To avoid races, try to do the TID -> 0 atomic transition
  1700. * again. If it succeeds then we can return without waking
  1701. * anyone else up:
  1702. */
  1703. if (!(uval & FUTEX_OWNER_DIED))
  1704. uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
  1705. if (unlikely(uval == -EFAULT))
  1706. goto pi_faulted;
  1707. /*
  1708. * Rare case: we managed to release the lock atomically,
  1709. * no need to wake anyone else up:
  1710. */
  1711. if (unlikely(uval == task_pid_vnr(current)))
  1712. goto out_unlock;
  1713. /*
  1714. * Ok, other tasks may need to be woken up - check waiters
  1715. * and do the wakeup if necessary:
  1716. */
  1717. head = &hb->chain;
  1718. plist_for_each_entry_safe(this, next, head, list) {
  1719. if (!match_futex (&this->key, &key))
  1720. continue;
  1721. ret = wake_futex_pi(uaddr, uval, this);
  1722. /*
  1723. * The atomic access to the futex value
  1724. * generated a pagefault, so retry the
  1725. * user-access and the wakeup:
  1726. */
  1727. if (ret == -EFAULT)
  1728. goto pi_faulted;
  1729. goto out_unlock;
  1730. }
  1731. /*
  1732. * No waiters - kernel unlocks the futex:
  1733. */
  1734. if (!(uval & FUTEX_OWNER_DIED)) {
  1735. ret = unlock_futex_pi(uaddr, uval);
  1736. if (ret == -EFAULT)
  1737. goto pi_faulted;
  1738. }
  1739. out_unlock:
  1740. spin_unlock(&hb->lock);
  1741. put_futex_key(fshared, &key);
  1742. out:
  1743. return ret;
  1744. pi_faulted:
  1745. spin_unlock(&hb->lock);
  1746. put_futex_key(fshared, &key);
  1747. ret = fault_in_user_writeable(uaddr);
  1748. if (!ret)
  1749. goto retry;
  1750. return ret;
  1751. }
  1752. /**
  1753. * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
  1754. * @hb: the hash_bucket futex_q was original enqueued on
  1755. * @q: the futex_q woken while waiting to be requeued
  1756. * @key2: the futex_key of the requeue target futex
  1757. * @timeout: the timeout associated with the wait (NULL if none)
  1758. *
  1759. * Detect if the task was woken on the initial futex as opposed to the requeue
  1760. * target futex. If so, determine if it was a timeout or a signal that caused
  1761. * the wakeup and return the appropriate error code to the caller. Must be
  1762. * called with the hb lock held.
  1763. *
  1764. * Returns
  1765. * 0 - no early wakeup detected
  1766. * <0 - -ETIMEDOUT or -ERESTARTNOINTR
  1767. */
  1768. static inline
  1769. int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
  1770. struct futex_q *q, union futex_key *key2,
  1771. struct hrtimer_sleeper *timeout)
  1772. {
  1773. int ret = 0;
  1774. /*
  1775. * With the hb lock held, we avoid races while we process the wakeup.
  1776. * We only need to hold hb (and not hb2) to ensure atomicity as the
  1777. * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
  1778. * It can't be requeued from uaddr2 to something else since we don't
  1779. * support a PI aware source futex for requeue.
  1780. */
  1781. if (!match_futex(&q->key, key2)) {
  1782. WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
  1783. /*
  1784. * We were woken prior to requeue by a timeout or a signal.
  1785. * Unqueue the futex_q and determine which it was.
  1786. */
  1787. plist_del(&q->list, &q->list.plist);
  1788. drop_futex_key_refs(&q->key);
  1789. if (timeout && !timeout->task)
  1790. ret = -ETIMEDOUT;
  1791. else
  1792. ret = -ERESTARTNOINTR;
  1793. }
  1794. return ret;
  1795. }
  1796. /**
  1797. * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
  1798. * @uaddr: the futex we initialyl wait on (non-pi)
  1799. * @fshared: whether the futexes are shared (1) or not (0). They must be
  1800. * the same type, no requeueing from private to shared, etc.
  1801. * @val: the expected value of uaddr
  1802. * @abs_time: absolute timeout
  1803. * @bitset: 32 bit wakeup bitset set by userspace, defaults to all.
  1804. * @clockrt: whether to use CLOCK_REALTIME (1) or CLOCK_MONOTONIC (0)
  1805. * @uaddr2: the pi futex we will take prior to returning to user-space
  1806. *
  1807. * The caller will wait on uaddr and will be requeued by futex_requeue() to
  1808. * uaddr2 which must be PI aware. Normal wakeup will wake on uaddr2 and
  1809. * complete the acquisition of the rt_mutex prior to returning to userspace.
  1810. * This ensures the rt_mutex maintains an owner when it has waiters; without
  1811. * one, the pi logic wouldn't know which task to boost/deboost, if there was a
  1812. * need to.
  1813. *
  1814. * We call schedule in futex_wait_queue_me() when we enqueue and return there
  1815. * via the following:
  1816. * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
  1817. * 2) wakeup on uaddr2 after a requeue and subsequent unlock
  1818. * 3) signal (before or after requeue)
  1819. * 4) timeout (before or after requeue)
  1820. *
  1821. * If 3, we setup a restart_block with futex_wait_requeue_pi() as the function.
  1822. *
  1823. * If 2, we may then block on trying to take the rt_mutex and return via:
  1824. * 5) successful lock
  1825. * 6) signal
  1826. * 7) timeout
  1827. * 8) other lock acquisition failure
  1828. *
  1829. * If 6, we setup a restart_block with futex_lock_pi() as the function.
  1830. *
  1831. * If 4 or 7, we cleanup and return with -ETIMEDOUT.
  1832. *
  1833. * Returns:
  1834. * 0 - On success
  1835. * <0 - On error
  1836. */
  1837. static int futex_wait_requeue_pi(u32 __user *uaddr, int fshared,
  1838. u32 val, ktime_t *abs_time, u32 bitset,
  1839. int clockrt, u32 __user *uaddr2)
  1840. {
  1841. struct hrtimer_sleeper timeout, *to = NULL;
  1842. struct rt_mutex_waiter rt_waiter;
  1843. struct rt_mutex *pi_mutex = NULL;
  1844. struct futex_hash_bucket *hb;
  1845. union futex_key key2;
  1846. struct futex_q q;
  1847. int res, ret;
  1848. if (!bitset)
  1849. return -EINVAL;
  1850. if (abs_time) {
  1851. to = &timeout;
  1852. hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
  1853. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1854. hrtimer_init_sleeper(to, current);
  1855. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  1856. current->timer_slack_ns);
  1857. }
  1858. /*
  1859. * The waiter is allocated on our stack, manipulated by the requeue
  1860. * code while we sleep on uaddr.
  1861. */
  1862. debug_rt_mutex_init_waiter(&rt_waiter);
  1863. rt_waiter.task = NULL;
  1864. q.pi_state = NULL;
  1865. q.bitset = bitset;
  1866. q.rt_waiter = &rt_waiter;
  1867. key2 = FUTEX_KEY_INIT;
  1868. ret = get_futex_key(uaddr2, fshared, &key2, VERIFY_WRITE);
  1869. if (unlikely(ret != 0))
  1870. goto out;
  1871. /* Prepare to wait on uaddr. */
  1872. ret = futex_wait_setup(uaddr, val, fshared, &q, &hb);
  1873. if (ret)
  1874. goto out_key2;
  1875. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  1876. futex_wait_queue_me(hb, &q, to);
  1877. spin_lock(&hb->lock);
  1878. ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  1879. spin_unlock(&hb->lock);
  1880. if (ret)
  1881. goto out_put_keys;
  1882. /*
  1883. * In order for us to be here, we know our q.key == key2, and since
  1884. * we took the hb->lock above, we also know that futex_requeue() has
  1885. * completed and we no longer have to concern ourselves with a wakeup
  1886. * race with the atomic proxy lock acquition by the requeue code.
  1887. */
  1888. /* Check if the requeue code acquired the second futex for us. */
  1889. if (!q.rt_waiter) {
  1890. /*
  1891. * Got the lock. We might not be the anticipated owner if we
  1892. * did a lock-steal - fix up the PI-state in that case.
  1893. */
  1894. if (q.pi_state && (q.pi_state->owner != current)) {
  1895. spin_lock(q.lock_ptr);
  1896. ret = fixup_pi_state_owner(uaddr2, &q, current,
  1897. fshared);
  1898. spin_unlock(q.lock_ptr);
  1899. }
  1900. } else {
  1901. /*
  1902. * We have been woken up by futex_unlock_pi(), a timeout, or a
  1903. * signal. futex_unlock_pi() will not destroy the lock_ptr nor
  1904. * the pi_state.
  1905. */
  1906. WARN_ON(!&q.pi_state);
  1907. pi_mutex = &q.pi_state->pi_mutex;
  1908. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter, 1);
  1909. debug_rt_mutex_free_waiter(&rt_waiter);
  1910. spin_lock(q.lock_ptr);
  1911. /*
  1912. * Fixup the pi_state owner and possibly acquire the lock if we
  1913. * haven't already.
  1914. */
  1915. res = fixup_owner(uaddr2, fshared, &q, !ret);
  1916. /*
  1917. * If fixup_owner() returned an error, proprogate that. If it
  1918. * acquired the lock, clear our -ETIMEDOUT or -EINTR.
  1919. */
  1920. if (res)
  1921. ret = (res < 0) ? res : 0;
  1922. /* Unqueue and drop the lock. */
  1923. unqueue_me_pi(&q);
  1924. }
  1925. /*
  1926. * If fixup_pi_state_owner() faulted and was unable to handle the
  1927. * fault, unlock the rt_mutex and return the fault to userspace.
  1928. */
  1929. if (ret == -EFAULT) {
  1930. if (rt_mutex_owner(pi_mutex) == current)
  1931. rt_mutex_unlock(pi_mutex);
  1932. } else if (ret == -EINTR) {
  1933. /*
  1934. * We've already been requeued, but we have no way to
  1935. * restart by calling futex_lock_pi() directly. We
  1936. * could restart the syscall, but that will look at
  1937. * the user space value and return right away. So we
  1938. * drop back with EWOULDBLOCK to tell user space that
  1939. * "val" has been changed. That's the same what the
  1940. * restart of the syscall would do in
  1941. * futex_wait_setup().
  1942. */
  1943. ret = -EWOULDBLOCK;
  1944. }
  1945. out_put_keys:
  1946. put_futex_key(fshared, &q.key);
  1947. out_key2:
  1948. put_futex_key(fshared, &key2);
  1949. out:
  1950. if (to) {
  1951. hrtimer_cancel(&to->timer);
  1952. destroy_hrtimer_on_stack(&to->timer);
  1953. }
  1954. return ret;
  1955. }
  1956. /*
  1957. * Support for robust futexes: the kernel cleans up held futexes at
  1958. * thread exit time.
  1959. *
  1960. * Implementation: user-space maintains a per-thread list of locks it
  1961. * is holding. Upon do_exit(), the kernel carefully walks this list,
  1962. * and marks all locks that are owned by this thread with the
  1963. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  1964. * always manipulated with the lock held, so the list is private and
  1965. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  1966. * field, to allow the kernel to clean up if the thread dies after
  1967. * acquiring the lock, but just before it could have added itself to
  1968. * the list. There can only be one such pending lock.
  1969. */
  1970. /**
  1971. * sys_set_robust_list - set the robust-futex list head of a task
  1972. * @head: pointer to the list-head
  1973. * @len: length of the list-head, as userspace expects
  1974. */
  1975. SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
  1976. size_t, len)
  1977. {
  1978. if (!futex_cmpxchg_enabled)
  1979. return -ENOSYS;
  1980. /*
  1981. * The kernel knows only one size for now:
  1982. */
  1983. if (unlikely(len != sizeof(*head)))
  1984. return -EINVAL;
  1985. current->robust_list = head;
  1986. return 0;
  1987. }
  1988. /**
  1989. * sys_get_robust_list - get the robust-futex list head of a task
  1990. * @pid: pid of the process [zero for current task]
  1991. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  1992. * @len_ptr: pointer to a length field, the kernel fills in the header size
  1993. */
  1994. SYSCALL_DEFINE3(get_robust_list, int, pid,
  1995. struct robust_list_head __user * __user *, head_ptr,
  1996. size_t __user *, len_ptr)
  1997. {
  1998. struct robust_list_head __user *head;
  1999. unsigned long ret;
  2000. const struct cred *cred = current_cred(), *pcred;
  2001. if (!futex_cmpxchg_enabled)
  2002. return -ENOSYS;
  2003. if (!pid)
  2004. head = current->robust_list;
  2005. else {
  2006. struct task_struct *p;
  2007. ret = -ESRCH;
  2008. rcu_read_lock();
  2009. p = find_task_by_vpid(pid);
  2010. if (!p)
  2011. goto err_unlock;
  2012. ret = -EPERM;
  2013. pcred = __task_cred(p);
  2014. if (cred->euid != pcred->euid &&
  2015. cred->euid != pcred->uid &&
  2016. !capable(CAP_SYS_PTRACE))
  2017. goto err_unlock;
  2018. head = p->robust_list;
  2019. rcu_read_unlock();
  2020. }
  2021. if (put_user(sizeof(*head), len_ptr))
  2022. return -EFAULT;
  2023. return put_user(head, head_ptr);
  2024. err_unlock:
  2025. rcu_read_unlock();
  2026. return ret;
  2027. }
  2028. /*
  2029. * Process a futex-list entry, check whether it's owned by the
  2030. * dying task, and do notification if so:
  2031. */
  2032. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
  2033. {
  2034. u32 uval, nval, mval;
  2035. retry:
  2036. if (get_user(uval, uaddr))
  2037. return -1;
  2038. if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
  2039. /*
  2040. * Ok, this dying thread is truly holding a futex
  2041. * of interest. Set the OWNER_DIED bit atomically
  2042. * via cmpxchg, and if the value had FUTEX_WAITERS
  2043. * set, wake up a waiter (if any). (We have to do a
  2044. * futex_wake() even if OWNER_DIED is already set -
  2045. * to handle the rare but possible case of recursive
  2046. * thread-death.) The rest of the cleanup is done in
  2047. * userspace.
  2048. */
  2049. mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  2050. nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
  2051. if (nval == -EFAULT)
  2052. return -1;
  2053. if (nval != uval)
  2054. goto retry;
  2055. /*
  2056. * Wake robust non-PI futexes here. The wakeup of
  2057. * PI futexes happens in exit_pi_state():
  2058. */
  2059. if (!pi && (uval & FUTEX_WAITERS))
  2060. futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
  2061. }
  2062. return 0;
  2063. }
  2064. /*
  2065. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  2066. */
  2067. static inline int fetch_robust_entry(struct robust_list __user **entry,
  2068. struct robust_list __user * __user *head,
  2069. int *pi)
  2070. {
  2071. unsigned long uentry;
  2072. if (get_user(uentry, (unsigned long __user *)head))
  2073. return -EFAULT;
  2074. *entry = (void __user *)(uentry & ~1UL);
  2075. *pi = uentry & 1;
  2076. return 0;
  2077. }
  2078. /*
  2079. * Walk curr->robust_list (very carefully, it's a userspace list!)
  2080. * and mark any locks found there dead, and notify any waiters.
  2081. *
  2082. * We silently return on any sign of list-walking problem.
  2083. */
  2084. void exit_robust_list(struct task_struct *curr)
  2085. {
  2086. struct robust_list_head __user *head = curr->robust_list;
  2087. struct robust_list __user *entry, *next_entry, *pending;
  2088. unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
  2089. unsigned long futex_offset;
  2090. int rc;
  2091. if (!futex_cmpxchg_enabled)
  2092. return;
  2093. /*
  2094. * Fetch the list head (which was registered earlier, via
  2095. * sys_set_robust_list()):
  2096. */
  2097. if (fetch_robust_entry(&entry, &head->list.next, &pi))
  2098. return;
  2099. /*
  2100. * Fetch the relative futex offset:
  2101. */
  2102. if (get_user(futex_offset, &head->futex_offset))
  2103. return;
  2104. /*
  2105. * Fetch any possibly pending lock-add first, and handle it
  2106. * if it exists:
  2107. */
  2108. if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
  2109. return;
  2110. next_entry = NULL; /* avoid warning with gcc */
  2111. while (entry != &head->list) {
  2112. /*
  2113. * Fetch the next entry in the list before calling
  2114. * handle_futex_death:
  2115. */
  2116. rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
  2117. /*
  2118. * A pending lock might already be on the list, so
  2119. * don't process it twice:
  2120. */
  2121. if (entry != pending)
  2122. if (handle_futex_death((void __user *)entry + futex_offset,
  2123. curr, pi))
  2124. return;
  2125. if (rc)
  2126. return;
  2127. entry = next_entry;
  2128. pi = next_pi;
  2129. /*
  2130. * Avoid excessively long or circular lists:
  2131. */
  2132. if (!--limit)
  2133. break;
  2134. cond_resched();
  2135. }
  2136. if (pending)
  2137. handle_futex_death((void __user *)pending + futex_offset,
  2138. curr, pip);
  2139. }
  2140. long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
  2141. u32 __user *uaddr2, u32 val2, u32 val3)
  2142. {
  2143. int clockrt, ret = -ENOSYS;
  2144. int cmd = op & FUTEX_CMD_MASK;
  2145. int fshared = 0;
  2146. if (!(op & FUTEX_PRIVATE_FLAG))
  2147. fshared = 1;
  2148. clockrt = op & FUTEX_CLOCK_REALTIME;
  2149. if (clockrt && cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
  2150. return -ENOSYS;
  2151. switch (cmd) {
  2152. case FUTEX_WAIT:
  2153. val3 = FUTEX_BITSET_MATCH_ANY;
  2154. case FUTEX_WAIT_BITSET:
  2155. ret = futex_wait(uaddr, fshared, val, timeout, val3, clockrt);
  2156. break;
  2157. case FUTEX_WAKE:
  2158. val3 = FUTEX_BITSET_MATCH_ANY;
  2159. case FUTEX_WAKE_BITSET:
  2160. ret = futex_wake(uaddr, fshared, val, val3);
  2161. break;
  2162. case FUTEX_REQUEUE:
  2163. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL, 0);
  2164. break;
  2165. case FUTEX_CMP_REQUEUE:
  2166. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3,
  2167. 0);
  2168. break;
  2169. case FUTEX_WAKE_OP:
  2170. ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
  2171. break;
  2172. case FUTEX_LOCK_PI:
  2173. if (futex_cmpxchg_enabled)
  2174. ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
  2175. break;
  2176. case FUTEX_UNLOCK_PI:
  2177. if (futex_cmpxchg_enabled)
  2178. ret = futex_unlock_pi(uaddr, fshared);
  2179. break;
  2180. case FUTEX_TRYLOCK_PI:
  2181. if (futex_cmpxchg_enabled)
  2182. ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
  2183. break;
  2184. case FUTEX_WAIT_REQUEUE_PI:
  2185. val3 = FUTEX_BITSET_MATCH_ANY;
  2186. ret = futex_wait_requeue_pi(uaddr, fshared, val, timeout, val3,
  2187. clockrt, uaddr2);
  2188. break;
  2189. case FUTEX_CMP_REQUEUE_PI:
  2190. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3,
  2191. 1);
  2192. break;
  2193. default:
  2194. ret = -ENOSYS;
  2195. }
  2196. return ret;
  2197. }
  2198. SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
  2199. struct timespec __user *, utime, u32 __user *, uaddr2,
  2200. u32, val3)
  2201. {
  2202. struct timespec ts;
  2203. ktime_t t, *tp = NULL;
  2204. u32 val2 = 0;
  2205. int cmd = op & FUTEX_CMD_MASK;
  2206. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  2207. cmd == FUTEX_WAIT_BITSET ||
  2208. cmd == FUTEX_WAIT_REQUEUE_PI)) {
  2209. if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
  2210. return -EFAULT;
  2211. if (!timespec_valid(&ts))
  2212. return -EINVAL;
  2213. t = timespec_to_ktime(ts);
  2214. if (cmd == FUTEX_WAIT)
  2215. t = ktime_add_safe(ktime_get(), t);
  2216. tp = &t;
  2217. }
  2218. /*
  2219. * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
  2220. * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
  2221. */
  2222. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  2223. cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
  2224. val2 = (u32) (unsigned long) utime;
  2225. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  2226. }
  2227. static int __init futex_init(void)
  2228. {
  2229. u32 curval;
  2230. int i;
  2231. /*
  2232. * This will fail and we want it. Some arch implementations do
  2233. * runtime detection of the futex_atomic_cmpxchg_inatomic()
  2234. * functionality. We want to know that before we call in any
  2235. * of the complex code paths. Also we want to prevent
  2236. * registration of robust lists in that case. NULL is
  2237. * guaranteed to fault and we get -EFAULT on functional
  2238. * implementation, the non functional ones will return
  2239. * -ENOSYS.
  2240. */
  2241. curval = cmpxchg_futex_value_locked(NULL, 0, 0);
  2242. if (curval == -EFAULT)
  2243. futex_cmpxchg_enabled = 1;
  2244. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  2245. plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
  2246. spin_lock_init(&futex_queues[i].lock);
  2247. }
  2248. return 0;
  2249. }
  2250. __initcall(futex_init);