futex.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  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/export.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, force_take = 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. * Should we force take the futex? See below.
  662. */
  663. if (unlikely(force_take)) {
  664. /*
  665. * Keep the OWNER_DIED and the WAITERS bit and set the
  666. * new TID value.
  667. */
  668. newval = (curval & ~FUTEX_TID_MASK) | vpid;
  669. force_take = 0;
  670. lock_taken = 1;
  671. }
  672. if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
  673. return -EFAULT;
  674. if (unlikely(curval != uval))
  675. goto retry;
  676. /*
  677. * We took the lock due to forced take over.
  678. */
  679. if (unlikely(lock_taken))
  680. return 1;
  681. /*
  682. * We dont have the lock. Look up the PI state (or create it if
  683. * we are the first waiter):
  684. */
  685. ret = lookup_pi_state(uval, hb, key, ps);
  686. if (unlikely(ret)) {
  687. switch (ret) {
  688. case -ESRCH:
  689. /*
  690. * We failed to find an owner for this
  691. * futex. So we have no pi_state to block
  692. * on. This can happen in two cases:
  693. *
  694. * 1) The owner died
  695. * 2) A stale FUTEX_WAITERS bit
  696. *
  697. * Re-read the futex value.
  698. */
  699. if (get_futex_value_locked(&curval, uaddr))
  700. return -EFAULT;
  701. /*
  702. * If the owner died or we have a stale
  703. * WAITERS bit the owner TID in the user space
  704. * futex is 0.
  705. */
  706. if (!(curval & FUTEX_TID_MASK)) {
  707. force_take = 1;
  708. goto retry;
  709. }
  710. default:
  711. break;
  712. }
  713. }
  714. return ret;
  715. }
  716. /**
  717. * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
  718. * @q: The futex_q to unqueue
  719. *
  720. * The q->lock_ptr must not be NULL and must be held by the caller.
  721. */
  722. static void __unqueue_futex(struct futex_q *q)
  723. {
  724. struct futex_hash_bucket *hb;
  725. if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
  726. || WARN_ON(plist_node_empty(&q->list)))
  727. return;
  728. hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
  729. plist_del(&q->list, &hb->chain);
  730. }
  731. /*
  732. * The hash bucket lock must be held when this is called.
  733. * Afterwards, the futex_q must not be accessed.
  734. */
  735. static void wake_futex(struct futex_q *q)
  736. {
  737. struct task_struct *p = q->task;
  738. if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
  739. return;
  740. /*
  741. * We set q->lock_ptr = NULL _before_ we wake up the task. If
  742. * a non-futex wake up happens on another CPU then the task
  743. * might exit and p would dereference a non-existing task
  744. * struct. Prevent this by holding a reference on p across the
  745. * wake up.
  746. */
  747. get_task_struct(p);
  748. __unqueue_futex(q);
  749. /*
  750. * The waiting task can free the futex_q as soon as
  751. * q->lock_ptr = NULL is written, without taking any locks. A
  752. * memory barrier is required here to prevent the following
  753. * store to lock_ptr from getting ahead of the plist_del.
  754. */
  755. smp_wmb();
  756. q->lock_ptr = NULL;
  757. wake_up_state(p, TASK_NORMAL);
  758. put_task_struct(p);
  759. }
  760. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  761. {
  762. struct task_struct *new_owner;
  763. struct futex_pi_state *pi_state = this->pi_state;
  764. u32 uninitialized_var(curval), newval;
  765. if (!pi_state)
  766. return -EINVAL;
  767. /*
  768. * If current does not own the pi_state then the futex is
  769. * inconsistent and user space fiddled with the futex value.
  770. */
  771. if (pi_state->owner != current)
  772. return -EINVAL;
  773. raw_spin_lock(&pi_state->pi_mutex.wait_lock);
  774. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  775. /*
  776. * It is possible that the next waiter (the one that brought
  777. * this owner to the kernel) timed out and is no longer
  778. * waiting on the lock.
  779. */
  780. if (!new_owner)
  781. new_owner = this->task;
  782. /*
  783. * We pass it to the next owner. (The WAITERS bit is always
  784. * kept enabled while there is PI state around. We must also
  785. * preserve the owner died bit.)
  786. */
  787. if (!(uval & FUTEX_OWNER_DIED)) {
  788. int ret = 0;
  789. newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
  790. if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
  791. ret = -EFAULT;
  792. else if (curval != uval)
  793. ret = -EINVAL;
  794. if (ret) {
  795. raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  796. return ret;
  797. }
  798. }
  799. raw_spin_lock_irq(&pi_state->owner->pi_lock);
  800. WARN_ON(list_empty(&pi_state->list));
  801. list_del_init(&pi_state->list);
  802. raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  803. raw_spin_lock_irq(&new_owner->pi_lock);
  804. WARN_ON(!list_empty(&pi_state->list));
  805. list_add(&pi_state->list, &new_owner->pi_state_list);
  806. pi_state->owner = new_owner;
  807. raw_spin_unlock_irq(&new_owner->pi_lock);
  808. raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  809. rt_mutex_unlock(&pi_state->pi_mutex);
  810. return 0;
  811. }
  812. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  813. {
  814. u32 uninitialized_var(oldval);
  815. /*
  816. * There is no waiter, so we unlock the futex. The owner died
  817. * bit has not to be preserved here. We are the owner:
  818. */
  819. if (cmpxchg_futex_value_locked(&oldval, uaddr, uval, 0))
  820. return -EFAULT;
  821. if (oldval != uval)
  822. return -EAGAIN;
  823. return 0;
  824. }
  825. /*
  826. * Express the locking dependencies for lockdep:
  827. */
  828. static inline void
  829. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  830. {
  831. if (hb1 <= hb2) {
  832. spin_lock(&hb1->lock);
  833. if (hb1 < hb2)
  834. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  835. } else { /* hb1 > hb2 */
  836. spin_lock(&hb2->lock);
  837. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  838. }
  839. }
  840. static inline void
  841. double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  842. {
  843. spin_unlock(&hb1->lock);
  844. if (hb1 != hb2)
  845. spin_unlock(&hb2->lock);
  846. }
  847. /*
  848. * Wake up waiters matching bitset queued on this futex (uaddr).
  849. */
  850. static int
  851. futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
  852. {
  853. struct futex_hash_bucket *hb;
  854. struct futex_q *this, *next;
  855. struct plist_head *head;
  856. union futex_key key = FUTEX_KEY_INIT;
  857. int ret;
  858. if (!bitset)
  859. return -EINVAL;
  860. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
  861. if (unlikely(ret != 0))
  862. goto out;
  863. hb = hash_futex(&key);
  864. spin_lock(&hb->lock);
  865. head = &hb->chain;
  866. plist_for_each_entry_safe(this, next, head, list) {
  867. if (match_futex (&this->key, &key)) {
  868. if (this->pi_state || this->rt_waiter) {
  869. ret = -EINVAL;
  870. break;
  871. }
  872. /* Check if one of the bits is set in both bitsets */
  873. if (!(this->bitset & bitset))
  874. continue;
  875. wake_futex(this);
  876. if (++ret >= nr_wake)
  877. break;
  878. }
  879. }
  880. spin_unlock(&hb->lock);
  881. put_futex_key(&key);
  882. out:
  883. return ret;
  884. }
  885. /*
  886. * Wake up all waiters hashed on the physical page that is mapped
  887. * to this virtual address:
  888. */
  889. static int
  890. futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
  891. int nr_wake, int nr_wake2, int op)
  892. {
  893. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  894. struct futex_hash_bucket *hb1, *hb2;
  895. struct plist_head *head;
  896. struct futex_q *this, *next;
  897. int ret, op_ret;
  898. retry:
  899. ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
  900. if (unlikely(ret != 0))
  901. goto out;
  902. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  903. if (unlikely(ret != 0))
  904. goto out_put_key1;
  905. hb1 = hash_futex(&key1);
  906. hb2 = hash_futex(&key2);
  907. retry_private:
  908. double_lock_hb(hb1, hb2);
  909. op_ret = futex_atomic_op_inuser(op, uaddr2);
  910. if (unlikely(op_ret < 0)) {
  911. double_unlock_hb(hb1, hb2);
  912. #ifndef CONFIG_MMU
  913. /*
  914. * we don't get EFAULT from MMU faults if we don't have an MMU,
  915. * but we might get them from range checking
  916. */
  917. ret = op_ret;
  918. goto out_put_keys;
  919. #endif
  920. if (unlikely(op_ret != -EFAULT)) {
  921. ret = op_ret;
  922. goto out_put_keys;
  923. }
  924. ret = fault_in_user_writeable(uaddr2);
  925. if (ret)
  926. goto out_put_keys;
  927. if (!(flags & FLAGS_SHARED))
  928. goto retry_private;
  929. put_futex_key(&key2);
  930. put_futex_key(&key1);
  931. goto retry;
  932. }
  933. head = &hb1->chain;
  934. plist_for_each_entry_safe(this, next, head, list) {
  935. if (match_futex (&this->key, &key1)) {
  936. if (this->pi_state || this->rt_waiter) {
  937. ret = -EINVAL;
  938. goto out_unlock;
  939. }
  940. wake_futex(this);
  941. if (++ret >= nr_wake)
  942. break;
  943. }
  944. }
  945. if (op_ret > 0) {
  946. head = &hb2->chain;
  947. op_ret = 0;
  948. plist_for_each_entry_safe(this, next, head, list) {
  949. if (match_futex (&this->key, &key2)) {
  950. if (this->pi_state || this->rt_waiter) {
  951. ret = -EINVAL;
  952. goto out_unlock;
  953. }
  954. wake_futex(this);
  955. if (++op_ret >= nr_wake2)
  956. break;
  957. }
  958. }
  959. ret += op_ret;
  960. }
  961. out_unlock:
  962. double_unlock_hb(hb1, hb2);
  963. out_put_keys:
  964. put_futex_key(&key2);
  965. out_put_key1:
  966. put_futex_key(&key1);
  967. out:
  968. return ret;
  969. }
  970. /**
  971. * requeue_futex() - Requeue a futex_q from one hb to another
  972. * @q: the futex_q to requeue
  973. * @hb1: the source hash_bucket
  974. * @hb2: the target hash_bucket
  975. * @key2: the new key for the requeued futex_q
  976. */
  977. static inline
  978. void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
  979. struct futex_hash_bucket *hb2, union futex_key *key2)
  980. {
  981. /*
  982. * If key1 and key2 hash to the same bucket, no need to
  983. * requeue.
  984. */
  985. if (likely(&hb1->chain != &hb2->chain)) {
  986. plist_del(&q->list, &hb1->chain);
  987. plist_add(&q->list, &hb2->chain);
  988. q->lock_ptr = &hb2->lock;
  989. }
  990. get_futex_key_refs(key2);
  991. q->key = *key2;
  992. }
  993. /**
  994. * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
  995. * @q: the futex_q
  996. * @key: the key of the requeue target futex
  997. * @hb: the hash_bucket of the requeue target futex
  998. *
  999. * During futex_requeue, with requeue_pi=1, it is possible to acquire the
  1000. * target futex if it is uncontended or via a lock steal. Set the futex_q key
  1001. * to the requeue target futex so the waiter can detect the wakeup on the right
  1002. * futex, but remove it from the hb and NULL the rt_waiter so it can detect
  1003. * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
  1004. * to protect access to the pi_state to fixup the owner later. Must be called
  1005. * with both q->lock_ptr and hb->lock held.
  1006. */
  1007. static inline
  1008. void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
  1009. struct futex_hash_bucket *hb)
  1010. {
  1011. get_futex_key_refs(key);
  1012. q->key = *key;
  1013. __unqueue_futex(q);
  1014. WARN_ON(!q->rt_waiter);
  1015. q->rt_waiter = NULL;
  1016. q->lock_ptr = &hb->lock;
  1017. wake_up_state(q->task, TASK_NORMAL);
  1018. }
  1019. /**
  1020. * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
  1021. * @pifutex: the user address of the to futex
  1022. * @hb1: the from futex hash bucket, must be locked by the caller
  1023. * @hb2: the to futex hash bucket, must be locked by the caller
  1024. * @key1: the from futex key
  1025. * @key2: the to futex key
  1026. * @ps: address to store the pi_state pointer
  1027. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  1028. *
  1029. * Try and get the lock on behalf of the top waiter if we can do it atomically.
  1030. * Wake the top waiter if we succeed. If the caller specified set_waiters,
  1031. * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
  1032. * hb1 and hb2 must be held by the caller.
  1033. *
  1034. * Returns:
  1035. * 0 - failed to acquire the lock atomicly
  1036. * 1 - acquired the lock
  1037. * <0 - error
  1038. */
  1039. static int futex_proxy_trylock_atomic(u32 __user *pifutex,
  1040. struct futex_hash_bucket *hb1,
  1041. struct futex_hash_bucket *hb2,
  1042. union futex_key *key1, union futex_key *key2,
  1043. struct futex_pi_state **ps, int set_waiters)
  1044. {
  1045. struct futex_q *top_waiter = NULL;
  1046. u32 curval;
  1047. int ret;
  1048. if (get_futex_value_locked(&curval, pifutex))
  1049. return -EFAULT;
  1050. /*
  1051. * Find the top_waiter and determine if there are additional waiters.
  1052. * If the caller intends to requeue more than 1 waiter to pifutex,
  1053. * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
  1054. * as we have means to handle the possible fault. If not, don't set
  1055. * the bit unecessarily as it will force the subsequent unlock to enter
  1056. * the kernel.
  1057. */
  1058. top_waiter = futex_top_waiter(hb1, key1);
  1059. /* There are no waiters, nothing for us to do. */
  1060. if (!top_waiter)
  1061. return 0;
  1062. /* Ensure we requeue to the expected futex. */
  1063. if (!match_futex(top_waiter->requeue_pi_key, key2))
  1064. return -EINVAL;
  1065. /*
  1066. * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
  1067. * the contended case or if set_waiters is 1. The pi_state is returned
  1068. * in ps in contended cases.
  1069. */
  1070. ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
  1071. set_waiters);
  1072. if (ret == 1)
  1073. requeue_pi_wake_futex(top_waiter, key2, hb2);
  1074. return ret;
  1075. }
  1076. /**
  1077. * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
  1078. * @uaddr1: source futex user address
  1079. * @flags: futex flags (FLAGS_SHARED, etc.)
  1080. * @uaddr2: target futex user address
  1081. * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
  1082. * @nr_requeue: number of waiters to requeue (0-INT_MAX)
  1083. * @cmpval: @uaddr1 expected value (or %NULL)
  1084. * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
  1085. * pi futex (pi to pi requeue is not supported)
  1086. *
  1087. * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
  1088. * uaddr2 atomically on behalf of the top waiter.
  1089. *
  1090. * Returns:
  1091. * >=0 - on success, the number of tasks requeued or woken
  1092. * <0 - on error
  1093. */
  1094. static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
  1095. u32 __user *uaddr2, int nr_wake, int nr_requeue,
  1096. u32 *cmpval, int requeue_pi)
  1097. {
  1098. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  1099. int drop_count = 0, task_count = 0, ret;
  1100. struct futex_pi_state *pi_state = NULL;
  1101. struct futex_hash_bucket *hb1, *hb2;
  1102. struct plist_head *head1;
  1103. struct futex_q *this, *next;
  1104. u32 curval2;
  1105. if (requeue_pi) {
  1106. /*
  1107. * requeue_pi requires a pi_state, try to allocate it now
  1108. * without any locks in case it fails.
  1109. */
  1110. if (refill_pi_state_cache())
  1111. return -ENOMEM;
  1112. /*
  1113. * requeue_pi must wake as many tasks as it can, up to nr_wake
  1114. * + nr_requeue, since it acquires the rt_mutex prior to
  1115. * returning to userspace, so as to not leave the rt_mutex with
  1116. * waiters and no owner. However, second and third wake-ups
  1117. * cannot be predicted as they involve race conditions with the
  1118. * first wake and a fault while looking up the pi_state. Both
  1119. * pthread_cond_signal() and pthread_cond_broadcast() should
  1120. * use nr_wake=1.
  1121. */
  1122. if (nr_wake != 1)
  1123. return -EINVAL;
  1124. }
  1125. retry:
  1126. if (pi_state != NULL) {
  1127. /*
  1128. * We will have to lookup the pi_state again, so free this one
  1129. * to keep the accounting correct.
  1130. */
  1131. free_pi_state(pi_state);
  1132. pi_state = NULL;
  1133. }
  1134. ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
  1135. if (unlikely(ret != 0))
  1136. goto out;
  1137. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
  1138. requeue_pi ? VERIFY_WRITE : VERIFY_READ);
  1139. if (unlikely(ret != 0))
  1140. goto out_put_key1;
  1141. hb1 = hash_futex(&key1);
  1142. hb2 = hash_futex(&key2);
  1143. retry_private:
  1144. double_lock_hb(hb1, hb2);
  1145. if (likely(cmpval != NULL)) {
  1146. u32 curval;
  1147. ret = get_futex_value_locked(&curval, uaddr1);
  1148. if (unlikely(ret)) {
  1149. double_unlock_hb(hb1, hb2);
  1150. ret = get_user(curval, uaddr1);
  1151. if (ret)
  1152. goto out_put_keys;
  1153. if (!(flags & FLAGS_SHARED))
  1154. goto retry_private;
  1155. put_futex_key(&key2);
  1156. put_futex_key(&key1);
  1157. goto retry;
  1158. }
  1159. if (curval != *cmpval) {
  1160. ret = -EAGAIN;
  1161. goto out_unlock;
  1162. }
  1163. }
  1164. if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
  1165. /*
  1166. * Attempt to acquire uaddr2 and wake the top waiter. If we
  1167. * intend to requeue waiters, force setting the FUTEX_WAITERS
  1168. * bit. We force this here where we are able to easily handle
  1169. * faults rather in the requeue loop below.
  1170. */
  1171. ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
  1172. &key2, &pi_state, nr_requeue);
  1173. /*
  1174. * At this point the top_waiter has either taken uaddr2 or is
  1175. * waiting on it. If the former, then the pi_state will not
  1176. * exist yet, look it up one more time to ensure we have a
  1177. * reference to it.
  1178. */
  1179. if (ret == 1) {
  1180. WARN_ON(pi_state);
  1181. drop_count++;
  1182. task_count++;
  1183. ret = get_futex_value_locked(&curval2, uaddr2);
  1184. if (!ret)
  1185. ret = lookup_pi_state(curval2, hb2, &key2,
  1186. &pi_state);
  1187. }
  1188. switch (ret) {
  1189. case 0:
  1190. break;
  1191. case -EFAULT:
  1192. double_unlock_hb(hb1, hb2);
  1193. put_futex_key(&key2);
  1194. put_futex_key(&key1);
  1195. ret = fault_in_user_writeable(uaddr2);
  1196. if (!ret)
  1197. goto retry;
  1198. goto out;
  1199. case -EAGAIN:
  1200. /* The owner was exiting, try again. */
  1201. double_unlock_hb(hb1, hb2);
  1202. put_futex_key(&key2);
  1203. put_futex_key(&key1);
  1204. cond_resched();
  1205. goto retry;
  1206. default:
  1207. goto out_unlock;
  1208. }
  1209. }
  1210. head1 = &hb1->chain;
  1211. plist_for_each_entry_safe(this, next, head1, list) {
  1212. if (task_count - nr_wake >= nr_requeue)
  1213. break;
  1214. if (!match_futex(&this->key, &key1))
  1215. continue;
  1216. /*
  1217. * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
  1218. * be paired with each other and no other futex ops.
  1219. *
  1220. * We should never be requeueing a futex_q with a pi_state,
  1221. * which is awaiting a futex_unlock_pi().
  1222. */
  1223. if ((requeue_pi && !this->rt_waiter) ||
  1224. (!requeue_pi && this->rt_waiter) ||
  1225. this->pi_state) {
  1226. ret = -EINVAL;
  1227. break;
  1228. }
  1229. /*
  1230. * Wake nr_wake waiters. For requeue_pi, if we acquired the
  1231. * lock, we already woke the top_waiter. If not, it will be
  1232. * woken by futex_unlock_pi().
  1233. */
  1234. if (++task_count <= nr_wake && !requeue_pi) {
  1235. wake_futex(this);
  1236. continue;
  1237. }
  1238. /* Ensure we requeue to the expected futex for requeue_pi. */
  1239. if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
  1240. ret = -EINVAL;
  1241. break;
  1242. }
  1243. /*
  1244. * Requeue nr_requeue waiters and possibly one more in the case
  1245. * of requeue_pi if we couldn't acquire the lock atomically.
  1246. */
  1247. if (requeue_pi) {
  1248. /* Prepare the waiter to take the rt_mutex. */
  1249. atomic_inc(&pi_state->refcount);
  1250. this->pi_state = pi_state;
  1251. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  1252. this->rt_waiter,
  1253. this->task, 1);
  1254. if (ret == 1) {
  1255. /* We got the lock. */
  1256. requeue_pi_wake_futex(this, &key2, hb2);
  1257. drop_count++;
  1258. continue;
  1259. } else if (ret) {
  1260. /* -EDEADLK */
  1261. this->pi_state = NULL;
  1262. free_pi_state(pi_state);
  1263. goto out_unlock;
  1264. }
  1265. }
  1266. requeue_futex(this, hb1, hb2, &key2);
  1267. drop_count++;
  1268. }
  1269. out_unlock:
  1270. double_unlock_hb(hb1, hb2);
  1271. /*
  1272. * drop_futex_key_refs() must be called outside the spinlocks. During
  1273. * the requeue we moved futex_q's from the hash bucket at key1 to the
  1274. * one at key2 and updated their key pointer. We no longer need to
  1275. * hold the references to key1.
  1276. */
  1277. while (--drop_count >= 0)
  1278. drop_futex_key_refs(&key1);
  1279. out_put_keys:
  1280. put_futex_key(&key2);
  1281. out_put_key1:
  1282. put_futex_key(&key1);
  1283. out:
  1284. if (pi_state != NULL)
  1285. free_pi_state(pi_state);
  1286. return ret ? ret : task_count;
  1287. }
  1288. /* The key must be already stored in q->key. */
  1289. static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
  1290. __acquires(&hb->lock)
  1291. {
  1292. struct futex_hash_bucket *hb;
  1293. hb = hash_futex(&q->key);
  1294. q->lock_ptr = &hb->lock;
  1295. spin_lock(&hb->lock);
  1296. return hb;
  1297. }
  1298. static inline void
  1299. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  1300. __releases(&hb->lock)
  1301. {
  1302. spin_unlock(&hb->lock);
  1303. }
  1304. /**
  1305. * queue_me() - Enqueue the futex_q on the futex_hash_bucket
  1306. * @q: The futex_q to enqueue
  1307. * @hb: The destination hash bucket
  1308. *
  1309. * The hb->lock must be held by the caller, and is released here. A call to
  1310. * queue_me() is typically paired with exactly one call to unqueue_me(). The
  1311. * exceptions involve the PI related operations, which may use unqueue_me_pi()
  1312. * or nothing if the unqueue is done as part of the wake process and the unqueue
  1313. * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
  1314. * an example).
  1315. */
  1316. static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  1317. __releases(&hb->lock)
  1318. {
  1319. int prio;
  1320. /*
  1321. * The priority used to register this element is
  1322. * - either the real thread-priority for the real-time threads
  1323. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  1324. * - or MAX_RT_PRIO for non-RT threads.
  1325. * Thus, all RT-threads are woken first in priority order, and
  1326. * the others are woken last, in FIFO order.
  1327. */
  1328. prio = min(current->normal_prio, MAX_RT_PRIO);
  1329. plist_node_init(&q->list, prio);
  1330. plist_add(&q->list, &hb->chain);
  1331. q->task = current;
  1332. spin_unlock(&hb->lock);
  1333. }
  1334. /**
  1335. * unqueue_me() - Remove the futex_q from its futex_hash_bucket
  1336. * @q: The futex_q to unqueue
  1337. *
  1338. * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
  1339. * be paired with exactly one earlier call to queue_me().
  1340. *
  1341. * Returns:
  1342. * 1 - if the futex_q was still queued (and we removed unqueued it)
  1343. * 0 - if the futex_q was already removed by the waking thread
  1344. */
  1345. static int unqueue_me(struct futex_q *q)
  1346. {
  1347. spinlock_t *lock_ptr;
  1348. int ret = 0;
  1349. /* In the common case we don't take the spinlock, which is nice. */
  1350. retry:
  1351. lock_ptr = q->lock_ptr;
  1352. barrier();
  1353. if (lock_ptr != NULL) {
  1354. spin_lock(lock_ptr);
  1355. /*
  1356. * q->lock_ptr can change between reading it and
  1357. * spin_lock(), causing us to take the wrong lock. This
  1358. * corrects the race condition.
  1359. *
  1360. * Reasoning goes like this: if we have the wrong lock,
  1361. * q->lock_ptr must have changed (maybe several times)
  1362. * between reading it and the spin_lock(). It can
  1363. * change again after the spin_lock() but only if it was
  1364. * already changed before the spin_lock(). It cannot,
  1365. * however, change back to the original value. Therefore
  1366. * we can detect whether we acquired the correct lock.
  1367. */
  1368. if (unlikely(lock_ptr != q->lock_ptr)) {
  1369. spin_unlock(lock_ptr);
  1370. goto retry;
  1371. }
  1372. __unqueue_futex(q);
  1373. BUG_ON(q->pi_state);
  1374. spin_unlock(lock_ptr);
  1375. ret = 1;
  1376. }
  1377. drop_futex_key_refs(&q->key);
  1378. return ret;
  1379. }
  1380. /*
  1381. * PI futexes can not be requeued and must remove themself from the
  1382. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  1383. * and dropped here.
  1384. */
  1385. static void unqueue_me_pi(struct futex_q *q)
  1386. __releases(q->lock_ptr)
  1387. {
  1388. __unqueue_futex(q);
  1389. BUG_ON(!q->pi_state);
  1390. free_pi_state(q->pi_state);
  1391. q->pi_state = NULL;
  1392. spin_unlock(q->lock_ptr);
  1393. }
  1394. /*
  1395. * Fixup the pi_state owner with the new owner.
  1396. *
  1397. * Must be called with hash bucket lock held and mm->sem held for non
  1398. * private futexes.
  1399. */
  1400. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  1401. struct task_struct *newowner)
  1402. {
  1403. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  1404. struct futex_pi_state *pi_state = q->pi_state;
  1405. struct task_struct *oldowner = pi_state->owner;
  1406. u32 uval, uninitialized_var(curval), newval;
  1407. int ret;
  1408. /* Owner died? */
  1409. if (!pi_state->owner)
  1410. newtid |= FUTEX_OWNER_DIED;
  1411. /*
  1412. * We are here either because we stole the rtmutex from the
  1413. * previous highest priority waiter or we are the highest priority
  1414. * waiter but failed to get the rtmutex the first time.
  1415. * We have to replace the newowner TID in the user space variable.
  1416. * This must be atomic as we have to preserve the owner died bit here.
  1417. *
  1418. * Note: We write the user space value _before_ changing the pi_state
  1419. * because we can fault here. Imagine swapped out pages or a fork
  1420. * that marked all the anonymous memory readonly for cow.
  1421. *
  1422. * Modifying pi_state _before_ the user space value would
  1423. * leave the pi_state in an inconsistent state when we fault
  1424. * here, because we need to drop the hash bucket lock to
  1425. * handle the fault. This might be observed in the PID check
  1426. * in lookup_pi_state.
  1427. */
  1428. retry:
  1429. if (get_futex_value_locked(&uval, uaddr))
  1430. goto handle_fault;
  1431. while (1) {
  1432. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  1433. if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
  1434. goto handle_fault;
  1435. if (curval == uval)
  1436. break;
  1437. uval = curval;
  1438. }
  1439. /*
  1440. * We fixed up user space. Now we need to fix the pi_state
  1441. * itself.
  1442. */
  1443. if (pi_state->owner != NULL) {
  1444. raw_spin_lock_irq(&pi_state->owner->pi_lock);
  1445. WARN_ON(list_empty(&pi_state->list));
  1446. list_del_init(&pi_state->list);
  1447. raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  1448. }
  1449. pi_state->owner = newowner;
  1450. raw_spin_lock_irq(&newowner->pi_lock);
  1451. WARN_ON(!list_empty(&pi_state->list));
  1452. list_add(&pi_state->list, &newowner->pi_state_list);
  1453. raw_spin_unlock_irq(&newowner->pi_lock);
  1454. return 0;
  1455. /*
  1456. * To handle the page fault we need to drop the hash bucket
  1457. * lock here. That gives the other task (either the highest priority
  1458. * waiter itself or the task which stole the rtmutex) the
  1459. * chance to try the fixup of the pi_state. So once we are
  1460. * back from handling the fault we need to check the pi_state
  1461. * after reacquiring the hash bucket lock and before trying to
  1462. * do another fixup. When the fixup has been done already we
  1463. * simply return.
  1464. */
  1465. handle_fault:
  1466. spin_unlock(q->lock_ptr);
  1467. ret = fault_in_user_writeable(uaddr);
  1468. spin_lock(q->lock_ptr);
  1469. /*
  1470. * Check if someone else fixed it for us:
  1471. */
  1472. if (pi_state->owner != oldowner)
  1473. return 0;
  1474. if (ret)
  1475. return ret;
  1476. goto retry;
  1477. }
  1478. static long futex_wait_restart(struct restart_block *restart);
  1479. /**
  1480. * fixup_owner() - Post lock pi_state and corner case management
  1481. * @uaddr: user address of the futex
  1482. * @q: futex_q (contains pi_state and access to the rt_mutex)
  1483. * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
  1484. *
  1485. * After attempting to lock an rt_mutex, this function is called to cleanup
  1486. * the pi_state owner as well as handle race conditions that may allow us to
  1487. * acquire the lock. Must be called with the hb lock held.
  1488. *
  1489. * Returns:
  1490. * 1 - success, lock taken
  1491. * 0 - success, lock not taken
  1492. * <0 - on error (-EFAULT)
  1493. */
  1494. static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
  1495. {
  1496. struct task_struct *owner;
  1497. int ret = 0;
  1498. if (locked) {
  1499. /*
  1500. * Got the lock. We might not be the anticipated owner if we
  1501. * did a lock-steal - fix up the PI-state in that case:
  1502. */
  1503. if (q->pi_state->owner != current)
  1504. ret = fixup_pi_state_owner(uaddr, q, current);
  1505. goto out;
  1506. }
  1507. /*
  1508. * Catch the rare case, where the lock was released when we were on the
  1509. * way back before we locked the hash bucket.
  1510. */
  1511. if (q->pi_state->owner == current) {
  1512. /*
  1513. * Try to get the rt_mutex now. This might fail as some other
  1514. * task acquired the rt_mutex after we removed ourself from the
  1515. * rt_mutex waiters list.
  1516. */
  1517. if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
  1518. locked = 1;
  1519. goto out;
  1520. }
  1521. /*
  1522. * pi_state is incorrect, some other task did a lock steal and
  1523. * we returned due to timeout or signal without taking the
  1524. * rt_mutex. Too late.
  1525. */
  1526. raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
  1527. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  1528. if (!owner)
  1529. owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  1530. raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
  1531. ret = fixup_pi_state_owner(uaddr, q, owner);
  1532. goto out;
  1533. }
  1534. /*
  1535. * Paranoia check. If we did not take the lock, then we should not be
  1536. * the owner of the rt_mutex.
  1537. */
  1538. if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
  1539. printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
  1540. "pi-state %p\n", ret,
  1541. q->pi_state->pi_mutex.owner,
  1542. q->pi_state->owner);
  1543. out:
  1544. return ret ? ret : locked;
  1545. }
  1546. /**
  1547. * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
  1548. * @hb: the futex hash bucket, must be locked by the caller
  1549. * @q: the futex_q to queue up on
  1550. * @timeout: the prepared hrtimer_sleeper, or null for no timeout
  1551. */
  1552. static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
  1553. struct hrtimer_sleeper *timeout)
  1554. {
  1555. /*
  1556. * The task state is guaranteed to be set before another task can
  1557. * wake it. set_current_state() is implemented using set_mb() and
  1558. * queue_me() calls spin_unlock() upon completion, both serializing
  1559. * access to the hash list and forcing another memory barrier.
  1560. */
  1561. set_current_state(TASK_INTERRUPTIBLE);
  1562. queue_me(q, hb);
  1563. /* Arm the timer */
  1564. if (timeout) {
  1565. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1566. if (!hrtimer_active(&timeout->timer))
  1567. timeout->task = NULL;
  1568. }
  1569. /*
  1570. * If we have been removed from the hash list, then another task
  1571. * has tried to wake us, and we can skip the call to schedule().
  1572. */
  1573. if (likely(!plist_node_empty(&q->list))) {
  1574. /*
  1575. * If the timer has already expired, current will already be
  1576. * flagged for rescheduling. Only call schedule if there
  1577. * is no timeout, or if it has yet to expire.
  1578. */
  1579. if (!timeout || timeout->task)
  1580. schedule();
  1581. }
  1582. __set_current_state(TASK_RUNNING);
  1583. }
  1584. /**
  1585. * futex_wait_setup() - Prepare to wait on a futex
  1586. * @uaddr: the futex userspace address
  1587. * @val: the expected value
  1588. * @flags: futex flags (FLAGS_SHARED, etc.)
  1589. * @q: the associated futex_q
  1590. * @hb: storage for hash_bucket pointer to be returned to caller
  1591. *
  1592. * Setup the futex_q and locate the hash_bucket. Get the futex value and
  1593. * compare it with the expected value. Handle atomic faults internally.
  1594. * Return with the hb lock held and a q.key reference on success, and unlocked
  1595. * with no q.key reference on failure.
  1596. *
  1597. * Returns:
  1598. * 0 - uaddr contains val and hb has been locked
  1599. * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
  1600. */
  1601. static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
  1602. struct futex_q *q, struct futex_hash_bucket **hb)
  1603. {
  1604. u32 uval;
  1605. int ret;
  1606. /*
  1607. * Access the page AFTER the hash-bucket is locked.
  1608. * Order is important:
  1609. *
  1610. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  1611. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  1612. *
  1613. * The basic logical guarantee of a futex is that it blocks ONLY
  1614. * if cond(var) is known to be true at the time of blocking, for
  1615. * any cond. If we locked the hash-bucket after testing *uaddr, that
  1616. * would open a race condition where we could block indefinitely with
  1617. * cond(var) false, which would violate the guarantee.
  1618. *
  1619. * On the other hand, we insert q and release the hash-bucket only
  1620. * after testing *uaddr. This guarantees that futex_wait() will NOT
  1621. * absorb a wakeup if *uaddr does not match the desired values
  1622. * while the syscall executes.
  1623. */
  1624. retry:
  1625. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
  1626. if (unlikely(ret != 0))
  1627. return ret;
  1628. retry_private:
  1629. *hb = queue_lock(q);
  1630. ret = get_futex_value_locked(&uval, uaddr);
  1631. if (ret) {
  1632. queue_unlock(q, *hb);
  1633. ret = get_user(uval, uaddr);
  1634. if (ret)
  1635. goto out;
  1636. if (!(flags & FLAGS_SHARED))
  1637. goto retry_private;
  1638. put_futex_key(&q->key);
  1639. goto retry;
  1640. }
  1641. if (uval != val) {
  1642. queue_unlock(q, *hb);
  1643. ret = -EWOULDBLOCK;
  1644. }
  1645. out:
  1646. if (ret)
  1647. put_futex_key(&q->key);
  1648. return ret;
  1649. }
  1650. static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
  1651. ktime_t *abs_time, u32 bitset)
  1652. {
  1653. struct hrtimer_sleeper timeout, *to = NULL;
  1654. struct restart_block *restart;
  1655. struct futex_hash_bucket *hb;
  1656. struct futex_q q = futex_q_init;
  1657. int ret;
  1658. if (!bitset)
  1659. return -EINVAL;
  1660. q.bitset = bitset;
  1661. if (abs_time) {
  1662. to = &timeout;
  1663. hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
  1664. CLOCK_REALTIME : CLOCK_MONOTONIC,
  1665. HRTIMER_MODE_ABS);
  1666. hrtimer_init_sleeper(to, current);
  1667. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  1668. current->timer_slack_ns);
  1669. }
  1670. retry:
  1671. /*
  1672. * Prepare to wait on uaddr. On success, holds hb lock and increments
  1673. * q.key refs.
  1674. */
  1675. ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
  1676. if (ret)
  1677. goto out;
  1678. /* queue_me and wait for wakeup, timeout, or a signal. */
  1679. futex_wait_queue_me(hb, &q, to);
  1680. /* If we were woken (and unqueued), we succeeded, whatever. */
  1681. ret = 0;
  1682. /* unqueue_me() drops q.key ref */
  1683. if (!unqueue_me(&q))
  1684. goto out;
  1685. ret = -ETIMEDOUT;
  1686. if (to && !to->task)
  1687. goto out;
  1688. /*
  1689. * We expect signal_pending(current), but we might be the
  1690. * victim of a spurious wakeup as well.
  1691. */
  1692. if (!signal_pending(current))
  1693. goto retry;
  1694. ret = -ERESTARTSYS;
  1695. if (!abs_time)
  1696. goto out;
  1697. restart = &current_thread_info()->restart_block;
  1698. restart->fn = futex_wait_restart;
  1699. restart->futex.uaddr = uaddr;
  1700. restart->futex.val = val;
  1701. restart->futex.time = abs_time->tv64;
  1702. restart->futex.bitset = bitset;
  1703. restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
  1704. ret = -ERESTART_RESTARTBLOCK;
  1705. out:
  1706. if (to) {
  1707. hrtimer_cancel(&to->timer);
  1708. destroy_hrtimer_on_stack(&to->timer);
  1709. }
  1710. return ret;
  1711. }
  1712. static long futex_wait_restart(struct restart_block *restart)
  1713. {
  1714. u32 __user *uaddr = restart->futex.uaddr;
  1715. ktime_t t, *tp = NULL;
  1716. if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
  1717. t.tv64 = restart->futex.time;
  1718. tp = &t;
  1719. }
  1720. restart->fn = do_no_restart_syscall;
  1721. return (long)futex_wait(uaddr, restart->futex.flags,
  1722. restart->futex.val, tp, restart->futex.bitset);
  1723. }
  1724. /*
  1725. * Userspace tried a 0 -> TID atomic transition of the futex value
  1726. * and failed. The kernel side here does the whole locking operation:
  1727. * if there are waiters then it will block, it does PI, etc. (Due to
  1728. * races the kernel might see a 0 value of the futex too.)
  1729. */
  1730. static int futex_lock_pi(u32 __user *uaddr, unsigned int flags, int detect,
  1731. ktime_t *time, int trylock)
  1732. {
  1733. struct hrtimer_sleeper timeout, *to = NULL;
  1734. struct futex_hash_bucket *hb;
  1735. struct futex_q q = futex_q_init;
  1736. int res, ret;
  1737. if (refill_pi_state_cache())
  1738. return -ENOMEM;
  1739. if (time) {
  1740. to = &timeout;
  1741. hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
  1742. HRTIMER_MODE_ABS);
  1743. hrtimer_init_sleeper(to, current);
  1744. hrtimer_set_expires(&to->timer, *time);
  1745. }
  1746. retry:
  1747. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
  1748. if (unlikely(ret != 0))
  1749. goto out;
  1750. retry_private:
  1751. hb = queue_lock(&q);
  1752. ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
  1753. if (unlikely(ret)) {
  1754. switch (ret) {
  1755. case 1:
  1756. /* We got the lock. */
  1757. ret = 0;
  1758. goto out_unlock_put_key;
  1759. case -EFAULT:
  1760. goto uaddr_faulted;
  1761. case -EAGAIN:
  1762. /*
  1763. * Task is exiting and we just wait for the
  1764. * exit to complete.
  1765. */
  1766. queue_unlock(&q, hb);
  1767. put_futex_key(&q.key);
  1768. cond_resched();
  1769. goto retry;
  1770. default:
  1771. goto out_unlock_put_key;
  1772. }
  1773. }
  1774. /*
  1775. * Only actually queue now that the atomic ops are done:
  1776. */
  1777. queue_me(&q, hb);
  1778. WARN_ON(!q.pi_state);
  1779. /*
  1780. * Block on the PI mutex:
  1781. */
  1782. if (!trylock)
  1783. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1784. else {
  1785. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1786. /* Fixup the trylock return value: */
  1787. ret = ret ? 0 : -EWOULDBLOCK;
  1788. }
  1789. spin_lock(q.lock_ptr);
  1790. /*
  1791. * Fixup the pi_state owner and possibly acquire the lock if we
  1792. * haven't already.
  1793. */
  1794. res = fixup_owner(uaddr, &q, !ret);
  1795. /*
  1796. * If fixup_owner() returned an error, proprogate that. If it acquired
  1797. * the lock, clear our -ETIMEDOUT or -EINTR.
  1798. */
  1799. if (res)
  1800. ret = (res < 0) ? res : 0;
  1801. /*
  1802. * If fixup_owner() faulted and was unable to handle the fault, unlock
  1803. * it and return the fault to userspace.
  1804. */
  1805. if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
  1806. rt_mutex_unlock(&q.pi_state->pi_mutex);
  1807. /* Unqueue and drop the lock */
  1808. unqueue_me_pi(&q);
  1809. goto out_put_key;
  1810. out_unlock_put_key:
  1811. queue_unlock(&q, hb);
  1812. out_put_key:
  1813. put_futex_key(&q.key);
  1814. out:
  1815. if (to)
  1816. destroy_hrtimer_on_stack(&to->timer);
  1817. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  1818. uaddr_faulted:
  1819. queue_unlock(&q, hb);
  1820. ret = fault_in_user_writeable(uaddr);
  1821. if (ret)
  1822. goto out_put_key;
  1823. if (!(flags & FLAGS_SHARED))
  1824. goto retry_private;
  1825. put_futex_key(&q.key);
  1826. goto retry;
  1827. }
  1828. /*
  1829. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1830. * This is the in-kernel slowpath: we look up the PI state (if any),
  1831. * and do the rt-mutex unlock.
  1832. */
  1833. static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
  1834. {
  1835. struct futex_hash_bucket *hb;
  1836. struct futex_q *this, *next;
  1837. struct plist_head *head;
  1838. union futex_key key = FUTEX_KEY_INIT;
  1839. u32 uval, vpid = task_pid_vnr(current);
  1840. int ret;
  1841. retry:
  1842. if (get_user(uval, uaddr))
  1843. return -EFAULT;
  1844. /*
  1845. * We release only a lock we actually own:
  1846. */
  1847. if ((uval & FUTEX_TID_MASK) != vpid)
  1848. return -EPERM;
  1849. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
  1850. if (unlikely(ret != 0))
  1851. goto out;
  1852. hb = hash_futex(&key);
  1853. spin_lock(&hb->lock);
  1854. /*
  1855. * To avoid races, try to do the TID -> 0 atomic transition
  1856. * again. If it succeeds then we can return without waking
  1857. * anyone else up:
  1858. */
  1859. if (!(uval & FUTEX_OWNER_DIED) &&
  1860. cmpxchg_futex_value_locked(&uval, uaddr, vpid, 0))
  1861. goto pi_faulted;
  1862. /*
  1863. * Rare case: we managed to release the lock atomically,
  1864. * no need to wake anyone else up:
  1865. */
  1866. if (unlikely(uval == vpid))
  1867. goto out_unlock;
  1868. /*
  1869. * Ok, other tasks may need to be woken up - check waiters
  1870. * and do the wakeup if necessary:
  1871. */
  1872. head = &hb->chain;
  1873. plist_for_each_entry_safe(this, next, head, list) {
  1874. if (!match_futex (&this->key, &key))
  1875. continue;
  1876. ret = wake_futex_pi(uaddr, uval, this);
  1877. /*
  1878. * The atomic access to the futex value
  1879. * generated a pagefault, so retry the
  1880. * user-access and the wakeup:
  1881. */
  1882. if (ret == -EFAULT)
  1883. goto pi_faulted;
  1884. goto out_unlock;
  1885. }
  1886. /*
  1887. * No waiters - kernel unlocks the futex:
  1888. */
  1889. if (!(uval & FUTEX_OWNER_DIED)) {
  1890. ret = unlock_futex_pi(uaddr, uval);
  1891. if (ret == -EFAULT)
  1892. goto pi_faulted;
  1893. }
  1894. out_unlock:
  1895. spin_unlock(&hb->lock);
  1896. put_futex_key(&key);
  1897. out:
  1898. return ret;
  1899. pi_faulted:
  1900. spin_unlock(&hb->lock);
  1901. put_futex_key(&key);
  1902. ret = fault_in_user_writeable(uaddr);
  1903. if (!ret)
  1904. goto retry;
  1905. return ret;
  1906. }
  1907. /**
  1908. * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
  1909. * @hb: the hash_bucket futex_q was original enqueued on
  1910. * @q: the futex_q woken while waiting to be requeued
  1911. * @key2: the futex_key of the requeue target futex
  1912. * @timeout: the timeout associated with the wait (NULL if none)
  1913. *
  1914. * Detect if the task was woken on the initial futex as opposed to the requeue
  1915. * target futex. If so, determine if it was a timeout or a signal that caused
  1916. * the wakeup and return the appropriate error code to the caller. Must be
  1917. * called with the hb lock held.
  1918. *
  1919. * Returns
  1920. * 0 - no early wakeup detected
  1921. * <0 - -ETIMEDOUT or -ERESTARTNOINTR
  1922. */
  1923. static inline
  1924. int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
  1925. struct futex_q *q, union futex_key *key2,
  1926. struct hrtimer_sleeper *timeout)
  1927. {
  1928. int ret = 0;
  1929. /*
  1930. * With the hb lock held, we avoid races while we process the wakeup.
  1931. * We only need to hold hb (and not hb2) to ensure atomicity as the
  1932. * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
  1933. * It can't be requeued from uaddr2 to something else since we don't
  1934. * support a PI aware source futex for requeue.
  1935. */
  1936. if (!match_futex(&q->key, key2)) {
  1937. WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
  1938. /*
  1939. * We were woken prior to requeue by a timeout or a signal.
  1940. * Unqueue the futex_q and determine which it was.
  1941. */
  1942. plist_del(&q->list, &hb->chain);
  1943. /* Handle spurious wakeups gracefully */
  1944. ret = -EWOULDBLOCK;
  1945. if (timeout && !timeout->task)
  1946. ret = -ETIMEDOUT;
  1947. else if (signal_pending(current))
  1948. ret = -ERESTARTNOINTR;
  1949. }
  1950. return ret;
  1951. }
  1952. /**
  1953. * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
  1954. * @uaddr: the futex we initially wait on (non-pi)
  1955. * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
  1956. * the same type, no requeueing from private to shared, etc.
  1957. * @val: the expected value of uaddr
  1958. * @abs_time: absolute timeout
  1959. * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
  1960. * @clockrt: whether to use CLOCK_REALTIME (1) or CLOCK_MONOTONIC (0)
  1961. * @uaddr2: the pi futex we will take prior to returning to user-space
  1962. *
  1963. * The caller will wait on uaddr and will be requeued by futex_requeue() to
  1964. * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
  1965. * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
  1966. * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
  1967. * without one, the pi logic would not know which task to boost/deboost, if
  1968. * there was a need to.
  1969. *
  1970. * We call schedule in futex_wait_queue_me() when we enqueue and return there
  1971. * via the following:
  1972. * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
  1973. * 2) wakeup on uaddr2 after a requeue
  1974. * 3) signal
  1975. * 4) timeout
  1976. *
  1977. * If 3, cleanup and return -ERESTARTNOINTR.
  1978. *
  1979. * If 2, we may then block on trying to take the rt_mutex and return via:
  1980. * 5) successful lock
  1981. * 6) signal
  1982. * 7) timeout
  1983. * 8) other lock acquisition failure
  1984. *
  1985. * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
  1986. *
  1987. * If 4 or 7, we cleanup and return with -ETIMEDOUT.
  1988. *
  1989. * Returns:
  1990. * 0 - On success
  1991. * <0 - On error
  1992. */
  1993. static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  1994. u32 val, ktime_t *abs_time, u32 bitset,
  1995. u32 __user *uaddr2)
  1996. {
  1997. struct hrtimer_sleeper timeout, *to = NULL;
  1998. struct rt_mutex_waiter rt_waiter;
  1999. struct rt_mutex *pi_mutex = NULL;
  2000. struct futex_hash_bucket *hb;
  2001. union futex_key key2 = FUTEX_KEY_INIT;
  2002. struct futex_q q = futex_q_init;
  2003. int res, ret;
  2004. if (uaddr == uaddr2)
  2005. return -EINVAL;
  2006. if (!bitset)
  2007. return -EINVAL;
  2008. if (abs_time) {
  2009. to = &timeout;
  2010. hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
  2011. CLOCK_REALTIME : CLOCK_MONOTONIC,
  2012. HRTIMER_MODE_ABS);
  2013. hrtimer_init_sleeper(to, current);
  2014. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  2015. current->timer_slack_ns);
  2016. }
  2017. /*
  2018. * The waiter is allocated on our stack, manipulated by the requeue
  2019. * code while we sleep on uaddr.
  2020. */
  2021. debug_rt_mutex_init_waiter(&rt_waiter);
  2022. rt_waiter.task = NULL;
  2023. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  2024. if (unlikely(ret != 0))
  2025. goto out;
  2026. q.bitset = bitset;
  2027. q.rt_waiter = &rt_waiter;
  2028. q.requeue_pi_key = &key2;
  2029. /*
  2030. * Prepare to wait on uaddr. On success, increments q.key (key1) ref
  2031. * count.
  2032. */
  2033. ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
  2034. if (ret)
  2035. goto out_key2;
  2036. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  2037. futex_wait_queue_me(hb, &q, to);
  2038. spin_lock(&hb->lock);
  2039. ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  2040. spin_unlock(&hb->lock);
  2041. if (ret)
  2042. goto out_put_keys;
  2043. /*
  2044. * In order for us to be here, we know our q.key == key2, and since
  2045. * we took the hb->lock above, we also know that futex_requeue() has
  2046. * completed and we no longer have to concern ourselves with a wakeup
  2047. * race with the atomic proxy lock acquisition by the requeue code. The
  2048. * futex_requeue dropped our key1 reference and incremented our key2
  2049. * reference count.
  2050. */
  2051. /* Check if the requeue code acquired the second futex for us. */
  2052. if (!q.rt_waiter) {
  2053. /*
  2054. * Got the lock. We might not be the anticipated owner if we
  2055. * did a lock-steal - fix up the PI-state in that case.
  2056. */
  2057. if (q.pi_state && (q.pi_state->owner != current)) {
  2058. spin_lock(q.lock_ptr);
  2059. ret = fixup_pi_state_owner(uaddr2, &q, current);
  2060. spin_unlock(q.lock_ptr);
  2061. }
  2062. } else {
  2063. /*
  2064. * We have been woken up by futex_unlock_pi(), a timeout, or a
  2065. * signal. futex_unlock_pi() will not destroy the lock_ptr nor
  2066. * the pi_state.
  2067. */
  2068. WARN_ON(!q.pi_state);
  2069. pi_mutex = &q.pi_state->pi_mutex;
  2070. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter, 1);
  2071. debug_rt_mutex_free_waiter(&rt_waiter);
  2072. spin_lock(q.lock_ptr);
  2073. /*
  2074. * Fixup the pi_state owner and possibly acquire the lock if we
  2075. * haven't already.
  2076. */
  2077. res = fixup_owner(uaddr2, &q, !ret);
  2078. /*
  2079. * If fixup_owner() returned an error, proprogate that. If it
  2080. * acquired the lock, clear -ETIMEDOUT or -EINTR.
  2081. */
  2082. if (res)
  2083. ret = (res < 0) ? res : 0;
  2084. /* Unqueue and drop the lock. */
  2085. unqueue_me_pi(&q);
  2086. }
  2087. /*
  2088. * If fixup_pi_state_owner() faulted and was unable to handle the
  2089. * fault, unlock the rt_mutex and return the fault to userspace.
  2090. */
  2091. if (ret == -EFAULT) {
  2092. if (pi_mutex && rt_mutex_owner(pi_mutex) == current)
  2093. rt_mutex_unlock(pi_mutex);
  2094. } else if (ret == -EINTR) {
  2095. /*
  2096. * We've already been requeued, but cannot restart by calling
  2097. * futex_lock_pi() directly. We could restart this syscall, but
  2098. * it would detect that the user space "val" changed and return
  2099. * -EWOULDBLOCK. Save the overhead of the restart and return
  2100. * -EWOULDBLOCK directly.
  2101. */
  2102. ret = -EWOULDBLOCK;
  2103. }
  2104. out_put_keys:
  2105. put_futex_key(&q.key);
  2106. out_key2:
  2107. put_futex_key(&key2);
  2108. out:
  2109. if (to) {
  2110. hrtimer_cancel(&to->timer);
  2111. destroy_hrtimer_on_stack(&to->timer);
  2112. }
  2113. return ret;
  2114. }
  2115. /*
  2116. * Support for robust futexes: the kernel cleans up held futexes at
  2117. * thread exit time.
  2118. *
  2119. * Implementation: user-space maintains a per-thread list of locks it
  2120. * is holding. Upon do_exit(), the kernel carefully walks this list,
  2121. * and marks all locks that are owned by this thread with the
  2122. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  2123. * always manipulated with the lock held, so the list is private and
  2124. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  2125. * field, to allow the kernel to clean up if the thread dies after
  2126. * acquiring the lock, but just before it could have added itself to
  2127. * the list. There can only be one such pending lock.
  2128. */
  2129. /**
  2130. * sys_set_robust_list() - Set the robust-futex list head of a task
  2131. * @head: pointer to the list-head
  2132. * @len: length of the list-head, as userspace expects
  2133. */
  2134. SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
  2135. size_t, len)
  2136. {
  2137. if (!futex_cmpxchg_enabled)
  2138. return -ENOSYS;
  2139. /*
  2140. * The kernel knows only one size for now:
  2141. */
  2142. if (unlikely(len != sizeof(*head)))
  2143. return -EINVAL;
  2144. current->robust_list = head;
  2145. return 0;
  2146. }
  2147. /**
  2148. * sys_get_robust_list() - Get the robust-futex list head of a task
  2149. * @pid: pid of the process [zero for current task]
  2150. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  2151. * @len_ptr: pointer to a length field, the kernel fills in the header size
  2152. */
  2153. SYSCALL_DEFINE3(get_robust_list, int, pid,
  2154. struct robust_list_head __user * __user *, head_ptr,
  2155. size_t __user *, len_ptr)
  2156. {
  2157. struct robust_list_head __user *head;
  2158. unsigned long ret;
  2159. struct task_struct *p;
  2160. if (!futex_cmpxchg_enabled)
  2161. return -ENOSYS;
  2162. WARN_ONCE(1, "deprecated: get_robust_list will be deleted in 2013.\n");
  2163. rcu_read_lock();
  2164. ret = -ESRCH;
  2165. if (!pid)
  2166. p = current;
  2167. else {
  2168. p = find_task_by_vpid(pid);
  2169. if (!p)
  2170. goto err_unlock;
  2171. }
  2172. ret = -EPERM;
  2173. if (!ptrace_may_access(p, PTRACE_MODE_READ))
  2174. goto err_unlock;
  2175. head = p->robust_list;
  2176. rcu_read_unlock();
  2177. if (put_user(sizeof(*head), len_ptr))
  2178. return -EFAULT;
  2179. return put_user(head, head_ptr);
  2180. err_unlock:
  2181. rcu_read_unlock();
  2182. return ret;
  2183. }
  2184. /*
  2185. * Process a futex-list entry, check whether it's owned by the
  2186. * dying task, and do notification if so:
  2187. */
  2188. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
  2189. {
  2190. u32 uval, uninitialized_var(nval), mval;
  2191. retry:
  2192. if (get_user(uval, uaddr))
  2193. return -1;
  2194. if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
  2195. /*
  2196. * Ok, this dying thread is truly holding a futex
  2197. * of interest. Set the OWNER_DIED bit atomically
  2198. * via cmpxchg, and if the value had FUTEX_WAITERS
  2199. * set, wake up a waiter (if any). (We have to do a
  2200. * futex_wake() even if OWNER_DIED is already set -
  2201. * to handle the rare but possible case of recursive
  2202. * thread-death.) The rest of the cleanup is done in
  2203. * userspace.
  2204. */
  2205. mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  2206. /*
  2207. * We are not holding a lock here, but we want to have
  2208. * the pagefault_disable/enable() protection because
  2209. * we want to handle the fault gracefully. If the
  2210. * access fails we try to fault in the futex with R/W
  2211. * verification via get_user_pages. get_user() above
  2212. * does not guarantee R/W access. If that fails we
  2213. * give up and leave the futex locked.
  2214. */
  2215. if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
  2216. if (fault_in_user_writeable(uaddr))
  2217. return -1;
  2218. goto retry;
  2219. }
  2220. if (nval != uval)
  2221. goto retry;
  2222. /*
  2223. * Wake robust non-PI futexes here. The wakeup of
  2224. * PI futexes happens in exit_pi_state():
  2225. */
  2226. if (!pi && (uval & FUTEX_WAITERS))
  2227. futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
  2228. }
  2229. return 0;
  2230. }
  2231. /*
  2232. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  2233. */
  2234. static inline int fetch_robust_entry(struct robust_list __user **entry,
  2235. struct robust_list __user * __user *head,
  2236. unsigned int *pi)
  2237. {
  2238. unsigned long uentry;
  2239. if (get_user(uentry, (unsigned long __user *)head))
  2240. return -EFAULT;
  2241. *entry = (void __user *)(uentry & ~1UL);
  2242. *pi = uentry & 1;
  2243. return 0;
  2244. }
  2245. /*
  2246. * Walk curr->robust_list (very carefully, it's a userspace list!)
  2247. * and mark any locks found there dead, and notify any waiters.
  2248. *
  2249. * We silently return on any sign of list-walking problem.
  2250. */
  2251. void exit_robust_list(struct task_struct *curr)
  2252. {
  2253. struct robust_list_head __user *head = curr->robust_list;
  2254. struct robust_list __user *entry, *next_entry, *pending;
  2255. unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
  2256. unsigned int uninitialized_var(next_pi);
  2257. unsigned long futex_offset;
  2258. int rc;
  2259. if (!futex_cmpxchg_enabled)
  2260. return;
  2261. /*
  2262. * Fetch the list head (which was registered earlier, via
  2263. * sys_set_robust_list()):
  2264. */
  2265. if (fetch_robust_entry(&entry, &head->list.next, &pi))
  2266. return;
  2267. /*
  2268. * Fetch the relative futex offset:
  2269. */
  2270. if (get_user(futex_offset, &head->futex_offset))
  2271. return;
  2272. /*
  2273. * Fetch any possibly pending lock-add first, and handle it
  2274. * if it exists:
  2275. */
  2276. if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
  2277. return;
  2278. next_entry = NULL; /* avoid warning with gcc */
  2279. while (entry != &head->list) {
  2280. /*
  2281. * Fetch the next entry in the list before calling
  2282. * handle_futex_death:
  2283. */
  2284. rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
  2285. /*
  2286. * A pending lock might already be on the list, so
  2287. * don't process it twice:
  2288. */
  2289. if (entry != pending)
  2290. if (handle_futex_death((void __user *)entry + futex_offset,
  2291. curr, pi))
  2292. return;
  2293. if (rc)
  2294. return;
  2295. entry = next_entry;
  2296. pi = next_pi;
  2297. /*
  2298. * Avoid excessively long or circular lists:
  2299. */
  2300. if (!--limit)
  2301. break;
  2302. cond_resched();
  2303. }
  2304. if (pending)
  2305. handle_futex_death((void __user *)pending + futex_offset,
  2306. curr, pip);
  2307. }
  2308. long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
  2309. u32 __user *uaddr2, u32 val2, u32 val3)
  2310. {
  2311. int cmd = op & FUTEX_CMD_MASK;
  2312. unsigned int flags = 0;
  2313. if (!(op & FUTEX_PRIVATE_FLAG))
  2314. flags |= FLAGS_SHARED;
  2315. if (op & FUTEX_CLOCK_REALTIME) {
  2316. flags |= FLAGS_CLOCKRT;
  2317. if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
  2318. return -ENOSYS;
  2319. }
  2320. switch (cmd) {
  2321. case FUTEX_LOCK_PI:
  2322. case FUTEX_UNLOCK_PI:
  2323. case FUTEX_TRYLOCK_PI:
  2324. case FUTEX_WAIT_REQUEUE_PI:
  2325. case FUTEX_CMP_REQUEUE_PI:
  2326. if (!futex_cmpxchg_enabled)
  2327. return -ENOSYS;
  2328. }
  2329. switch (cmd) {
  2330. case FUTEX_WAIT:
  2331. val3 = FUTEX_BITSET_MATCH_ANY;
  2332. case FUTEX_WAIT_BITSET:
  2333. return futex_wait(uaddr, flags, val, timeout, val3);
  2334. case FUTEX_WAKE:
  2335. val3 = FUTEX_BITSET_MATCH_ANY;
  2336. case FUTEX_WAKE_BITSET:
  2337. return futex_wake(uaddr, flags, val, val3);
  2338. case FUTEX_REQUEUE:
  2339. return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
  2340. case FUTEX_CMP_REQUEUE:
  2341. return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
  2342. case FUTEX_WAKE_OP:
  2343. return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
  2344. case FUTEX_LOCK_PI:
  2345. return futex_lock_pi(uaddr, flags, val, timeout, 0);
  2346. case FUTEX_UNLOCK_PI:
  2347. return futex_unlock_pi(uaddr, flags);
  2348. case FUTEX_TRYLOCK_PI:
  2349. return futex_lock_pi(uaddr, flags, 0, timeout, 1);
  2350. case FUTEX_WAIT_REQUEUE_PI:
  2351. val3 = FUTEX_BITSET_MATCH_ANY;
  2352. return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
  2353. uaddr2);
  2354. case FUTEX_CMP_REQUEUE_PI:
  2355. return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
  2356. }
  2357. return -ENOSYS;
  2358. }
  2359. SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
  2360. struct timespec __user *, utime, u32 __user *, uaddr2,
  2361. u32, val3)
  2362. {
  2363. struct timespec ts;
  2364. ktime_t t, *tp = NULL;
  2365. u32 val2 = 0;
  2366. int cmd = op & FUTEX_CMD_MASK;
  2367. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  2368. cmd == FUTEX_WAIT_BITSET ||
  2369. cmd == FUTEX_WAIT_REQUEUE_PI)) {
  2370. if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
  2371. return -EFAULT;
  2372. if (!timespec_valid(&ts))
  2373. return -EINVAL;
  2374. t = timespec_to_ktime(ts);
  2375. if (cmd == FUTEX_WAIT)
  2376. t = ktime_add_safe(ktime_get(), t);
  2377. tp = &t;
  2378. }
  2379. /*
  2380. * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
  2381. * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
  2382. */
  2383. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  2384. cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
  2385. val2 = (u32) (unsigned long) utime;
  2386. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  2387. }
  2388. static int __init futex_init(void)
  2389. {
  2390. u32 curval;
  2391. int i;
  2392. /*
  2393. * This will fail and we want it. Some arch implementations do
  2394. * runtime detection of the futex_atomic_cmpxchg_inatomic()
  2395. * functionality. We want to know that before we call in any
  2396. * of the complex code paths. Also we want to prevent
  2397. * registration of robust lists in that case. NULL is
  2398. * guaranteed to fault and we get -EFAULT on functional
  2399. * implementation, the non-functional ones will return
  2400. * -ENOSYS.
  2401. */
  2402. if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
  2403. futex_cmpxchg_enabled = 1;
  2404. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  2405. plist_head_init(&futex_queues[i].chain);
  2406. spin_lock_init(&futex_queues[i].lock);
  2407. }
  2408. return 0;
  2409. }
  2410. __initcall(futex_init);