futex.c 71 KB

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