futex.c 49 KB

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