futex.c 71 KB

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