futex.c 59 KB

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