futex.c 51 KB

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