futex.c 67 KB

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