futex.c 49 KB

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