futex.c 71 KB

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