futex.c 52 KB

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