futex.c 66 KB

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