futex.c 68 KB

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