futex.c 68 KB

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