futex.c 68 KB

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