futex.c 50 KB

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