futex.c 67 KB

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