futex.c 50 KB

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