futex.c 68 KB

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