futex.c 45 KB

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