futex.c 66 KB

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