futex.c 50 KB

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