futex.c 45 KB

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