futex.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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 || ((current->euid != p->euid) && (current->euid != p->uid)))
  366. p = ERR_PTR(-ESRCH);
  367. else
  368. get_task_struct(p);
  369. rcu_read_unlock();
  370. return p;
  371. }
  372. /*
  373. * This task is holding PI mutexes at exit time => bad.
  374. * Kernel cleans up PI-state, but userspace is likely hosed.
  375. * (Robust-futex cleanup is separate and might save the day for userspace.)
  376. */
  377. void exit_pi_state_list(struct task_struct *curr)
  378. {
  379. struct list_head *next, *head = &curr->pi_state_list;
  380. struct futex_pi_state *pi_state;
  381. struct futex_hash_bucket *hb;
  382. union futex_key key;
  383. /*
  384. * We are a ZOMBIE and nobody can enqueue itself on
  385. * pi_state_list anymore, but we have to be careful
  386. * versus waiters unqueueing themselves:
  387. */
  388. spin_lock_irq(&curr->pi_lock);
  389. while (!list_empty(head)) {
  390. next = head->next;
  391. pi_state = list_entry(next, struct futex_pi_state, list);
  392. key = pi_state->key;
  393. hb = hash_futex(&key);
  394. spin_unlock_irq(&curr->pi_lock);
  395. spin_lock(&hb->lock);
  396. spin_lock_irq(&curr->pi_lock);
  397. /*
  398. * We dropped the pi-lock, so re-check whether this
  399. * task still owns the PI-state:
  400. */
  401. if (head->next != next) {
  402. spin_unlock(&hb->lock);
  403. continue;
  404. }
  405. WARN_ON(pi_state->owner != curr);
  406. WARN_ON(list_empty(&pi_state->list));
  407. list_del_init(&pi_state->list);
  408. pi_state->owner = NULL;
  409. spin_unlock_irq(&curr->pi_lock);
  410. rt_mutex_unlock(&pi_state->pi_mutex);
  411. spin_unlock(&hb->lock);
  412. spin_lock_irq(&curr->pi_lock);
  413. }
  414. spin_unlock_irq(&curr->pi_lock);
  415. }
  416. static int
  417. lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
  418. union futex_key *key, struct futex_pi_state **ps)
  419. {
  420. struct futex_pi_state *pi_state = NULL;
  421. struct futex_q *this, *next;
  422. struct plist_head *head;
  423. struct task_struct *p;
  424. pid_t pid = uval & FUTEX_TID_MASK;
  425. head = &hb->chain;
  426. plist_for_each_entry_safe(this, next, head, list) {
  427. if (match_futex(&this->key, key)) {
  428. /*
  429. * Another waiter already exists - bump up
  430. * the refcount and return its pi_state:
  431. */
  432. pi_state = this->pi_state;
  433. /*
  434. * Userspace might have messed up non PI and PI futexes
  435. */
  436. if (unlikely(!pi_state))
  437. return -EINVAL;
  438. WARN_ON(!atomic_read(&pi_state->refcount));
  439. WARN_ON(pid && pi_state->owner &&
  440. pi_state->owner->pid != pid);
  441. atomic_inc(&pi_state->refcount);
  442. *ps = pi_state;
  443. return 0;
  444. }
  445. }
  446. /*
  447. * We are the first waiter - try to look up the real owner and attach
  448. * the new pi_state to it, but bail out when TID = 0
  449. */
  450. if (!pid)
  451. return -ESRCH;
  452. p = futex_find_get_task(pid);
  453. if (IS_ERR(p))
  454. return PTR_ERR(p);
  455. /*
  456. * We need to look at the task state flags to figure out,
  457. * whether the task is exiting. To protect against the do_exit
  458. * change of the task flags, we do this protected by
  459. * p->pi_lock:
  460. */
  461. spin_lock_irq(&p->pi_lock);
  462. if (unlikely(p->flags & PF_EXITING)) {
  463. /*
  464. * The task is on the way out. When PF_EXITPIDONE is
  465. * set, we know that the task has finished the
  466. * cleanup:
  467. */
  468. int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
  469. spin_unlock_irq(&p->pi_lock);
  470. put_task_struct(p);
  471. return ret;
  472. }
  473. pi_state = alloc_pi_state();
  474. /*
  475. * Initialize the pi_mutex in locked state and make 'p'
  476. * the owner of it:
  477. */
  478. rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
  479. /* Store the key for possible exit cleanups: */
  480. pi_state->key = *key;
  481. WARN_ON(!list_empty(&pi_state->list));
  482. list_add(&pi_state->list, &p->pi_state_list);
  483. pi_state->owner = p;
  484. spin_unlock_irq(&p->pi_lock);
  485. put_task_struct(p);
  486. *ps = pi_state;
  487. return 0;
  488. }
  489. /*
  490. * The hash bucket lock must be held when this is called.
  491. * Afterwards, the futex_q must not be accessed.
  492. */
  493. static void wake_futex(struct futex_q *q)
  494. {
  495. plist_del(&q->list, &q->list.plist);
  496. if (q->filp)
  497. send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
  498. /*
  499. * The lock in wake_up_all() is a crucial memory barrier after the
  500. * plist_del() and also before assigning to q->lock_ptr.
  501. */
  502. wake_up_all(&q->waiters);
  503. /*
  504. * The waiting task can free the futex_q as soon as this is written,
  505. * without taking any locks. This must come last.
  506. *
  507. * A memory barrier is required here to prevent the following store
  508. * to lock_ptr from getting ahead of the wakeup. Clearing the lock
  509. * at the end of wake_up_all() does not prevent this store from
  510. * moving.
  511. */
  512. smp_wmb();
  513. q->lock_ptr = NULL;
  514. }
  515. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  516. {
  517. struct task_struct *new_owner;
  518. struct futex_pi_state *pi_state = this->pi_state;
  519. u32 curval, newval;
  520. if (!pi_state)
  521. return -EINVAL;
  522. spin_lock(&pi_state->pi_mutex.wait_lock);
  523. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  524. /*
  525. * This happens when we have stolen the lock and the original
  526. * pending owner did not enqueue itself back on the rt_mutex.
  527. * Thats not a tragedy. We know that way, that a lock waiter
  528. * is on the fly. We make the futex_q waiter the pending owner.
  529. */
  530. if (!new_owner)
  531. new_owner = this->task;
  532. /*
  533. * We pass it to the next owner. (The WAITERS bit is always
  534. * kept enabled while there is PI state around. We must also
  535. * preserve the owner died bit.)
  536. */
  537. if (!(uval & FUTEX_OWNER_DIED)) {
  538. int ret = 0;
  539. newval = FUTEX_WAITERS | new_owner->pid;
  540. pagefault_disable();
  541. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  542. pagefault_enable();
  543. if (curval == -EFAULT)
  544. ret = -EFAULT;
  545. if (curval != uval)
  546. ret = -EINVAL;
  547. if (ret) {
  548. spin_unlock(&pi_state->pi_mutex.wait_lock);
  549. return ret;
  550. }
  551. }
  552. spin_lock_irq(&pi_state->owner->pi_lock);
  553. WARN_ON(list_empty(&pi_state->list));
  554. list_del_init(&pi_state->list);
  555. spin_unlock_irq(&pi_state->owner->pi_lock);
  556. spin_lock_irq(&new_owner->pi_lock);
  557. WARN_ON(!list_empty(&pi_state->list));
  558. list_add(&pi_state->list, &new_owner->pi_state_list);
  559. pi_state->owner = new_owner;
  560. spin_unlock_irq(&new_owner->pi_lock);
  561. spin_unlock(&pi_state->pi_mutex.wait_lock);
  562. rt_mutex_unlock(&pi_state->pi_mutex);
  563. return 0;
  564. }
  565. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  566. {
  567. u32 oldval;
  568. /*
  569. * There is no waiter, so we unlock the futex. The owner died
  570. * bit has not to be preserved here. We are the owner:
  571. */
  572. pagefault_disable();
  573. oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
  574. pagefault_enable();
  575. if (oldval == -EFAULT)
  576. return oldval;
  577. if (oldval != uval)
  578. return -EAGAIN;
  579. return 0;
  580. }
  581. /*
  582. * Express the locking dependencies for lockdep:
  583. */
  584. static inline void
  585. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  586. {
  587. if (hb1 <= hb2) {
  588. spin_lock(&hb1->lock);
  589. if (hb1 < hb2)
  590. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  591. } else { /* hb1 > hb2 */
  592. spin_lock(&hb2->lock);
  593. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  594. }
  595. }
  596. /*
  597. * Wake up all waiters hashed on the physical page that is mapped
  598. * to this virtual address:
  599. */
  600. static int futex_wake(u32 __user *uaddr, struct rw_semaphore *fshared,
  601. int nr_wake)
  602. {
  603. struct futex_hash_bucket *hb;
  604. struct futex_q *this, *next;
  605. struct plist_head *head;
  606. union futex_key key;
  607. int ret;
  608. if (fshared)
  609. down_read(fshared);
  610. ret = get_futex_key(uaddr, fshared, &key);
  611. if (unlikely(ret != 0))
  612. goto out;
  613. hb = hash_futex(&key);
  614. spin_lock(&hb->lock);
  615. head = &hb->chain;
  616. plist_for_each_entry_safe(this, next, head, list) {
  617. if (match_futex (&this->key, &key)) {
  618. if (this->pi_state) {
  619. ret = -EINVAL;
  620. break;
  621. }
  622. wake_futex(this);
  623. if (++ret >= nr_wake)
  624. break;
  625. }
  626. }
  627. spin_unlock(&hb->lock);
  628. out:
  629. if (fshared)
  630. up_read(fshared);
  631. return ret;
  632. }
  633. /*
  634. * Wake up all waiters hashed on the physical page that is mapped
  635. * to this virtual address:
  636. */
  637. static int
  638. futex_wake_op(u32 __user *uaddr1, struct rw_semaphore *fshared,
  639. u32 __user *uaddr2,
  640. int nr_wake, int nr_wake2, int op)
  641. {
  642. union futex_key key1, key2;
  643. struct futex_hash_bucket *hb1, *hb2;
  644. struct plist_head *head;
  645. struct futex_q *this, *next;
  646. int ret, op_ret, attempt = 0;
  647. retryfull:
  648. if (fshared)
  649. down_read(fshared);
  650. ret = get_futex_key(uaddr1, fshared, &key1);
  651. if (unlikely(ret != 0))
  652. goto out;
  653. ret = get_futex_key(uaddr2, fshared, &key2);
  654. if (unlikely(ret != 0))
  655. goto out;
  656. hb1 = hash_futex(&key1);
  657. hb2 = hash_futex(&key2);
  658. retry:
  659. double_lock_hb(hb1, hb2);
  660. op_ret = futex_atomic_op_inuser(op, uaddr2);
  661. if (unlikely(op_ret < 0)) {
  662. u32 dummy;
  663. spin_unlock(&hb1->lock);
  664. if (hb1 != hb2)
  665. spin_unlock(&hb2->lock);
  666. #ifndef CONFIG_MMU
  667. /*
  668. * we don't get EFAULT from MMU faults if we don't have an MMU,
  669. * but we might get them from range checking
  670. */
  671. ret = op_ret;
  672. goto out;
  673. #endif
  674. if (unlikely(op_ret != -EFAULT)) {
  675. ret = op_ret;
  676. goto out;
  677. }
  678. /*
  679. * futex_atomic_op_inuser needs to both read and write
  680. * *(int __user *)uaddr2, but we can't modify it
  681. * non-atomically. Therefore, if get_user below is not
  682. * enough, we need to handle the fault ourselves, while
  683. * still holding the mmap_sem.
  684. */
  685. if (attempt++) {
  686. ret = futex_handle_fault((unsigned long)uaddr2,
  687. fshared, attempt);
  688. if (ret)
  689. goto out;
  690. goto retry;
  691. }
  692. /*
  693. * If we would have faulted, release mmap_sem,
  694. * fault it in and start all over again.
  695. */
  696. if (fshared)
  697. up_read(fshared);
  698. ret = get_user(dummy, uaddr2);
  699. if (ret)
  700. return ret;
  701. goto retryfull;
  702. }
  703. head = &hb1->chain;
  704. plist_for_each_entry_safe(this, next, head, list) {
  705. if (match_futex (&this->key, &key1)) {
  706. wake_futex(this);
  707. if (++ret >= nr_wake)
  708. break;
  709. }
  710. }
  711. if (op_ret > 0) {
  712. head = &hb2->chain;
  713. op_ret = 0;
  714. plist_for_each_entry_safe(this, next, head, list) {
  715. if (match_futex (&this->key, &key2)) {
  716. wake_futex(this);
  717. if (++op_ret >= nr_wake2)
  718. break;
  719. }
  720. }
  721. ret += op_ret;
  722. }
  723. spin_unlock(&hb1->lock);
  724. if (hb1 != hb2)
  725. spin_unlock(&hb2->lock);
  726. out:
  727. if (fshared)
  728. up_read(fshared);
  729. return ret;
  730. }
  731. /*
  732. * Requeue all waiters hashed on one physical page to another
  733. * physical page.
  734. */
  735. static int futex_requeue(u32 __user *uaddr1, struct rw_semaphore *fshared,
  736. u32 __user *uaddr2,
  737. int nr_wake, int nr_requeue, u32 *cmpval)
  738. {
  739. union futex_key key1, key2;
  740. struct futex_hash_bucket *hb1, *hb2;
  741. struct plist_head *head1;
  742. struct futex_q *this, *next;
  743. int ret, drop_count = 0;
  744. retry:
  745. if (fshared)
  746. down_read(fshared);
  747. ret = get_futex_key(uaddr1, fshared, &key1);
  748. if (unlikely(ret != 0))
  749. goto out;
  750. ret = get_futex_key(uaddr2, fshared, &key2);
  751. if (unlikely(ret != 0))
  752. goto out;
  753. hb1 = hash_futex(&key1);
  754. hb2 = hash_futex(&key2);
  755. double_lock_hb(hb1, hb2);
  756. if (likely(cmpval != NULL)) {
  757. u32 curval;
  758. ret = get_futex_value_locked(&curval, uaddr1);
  759. if (unlikely(ret)) {
  760. spin_unlock(&hb1->lock);
  761. if (hb1 != hb2)
  762. spin_unlock(&hb2->lock);
  763. /*
  764. * If we would have faulted, release mmap_sem, fault
  765. * it in and start all over again.
  766. */
  767. if (fshared)
  768. up_read(fshared);
  769. ret = get_user(curval, uaddr1);
  770. if (!ret)
  771. goto retry;
  772. return ret;
  773. }
  774. if (curval != *cmpval) {
  775. ret = -EAGAIN;
  776. goto out_unlock;
  777. }
  778. }
  779. head1 = &hb1->chain;
  780. plist_for_each_entry_safe(this, next, head1, list) {
  781. if (!match_futex (&this->key, &key1))
  782. continue;
  783. if (++ret <= nr_wake) {
  784. wake_futex(this);
  785. } else {
  786. /*
  787. * If key1 and key2 hash to the same bucket, no need to
  788. * requeue.
  789. */
  790. if (likely(head1 != &hb2->chain)) {
  791. plist_del(&this->list, &hb1->chain);
  792. plist_add(&this->list, &hb2->chain);
  793. this->lock_ptr = &hb2->lock;
  794. #ifdef CONFIG_DEBUG_PI_LIST
  795. this->list.plist.lock = &hb2->lock;
  796. #endif
  797. }
  798. this->key = key2;
  799. get_futex_key_refs(&key2);
  800. drop_count++;
  801. if (ret - nr_wake >= nr_requeue)
  802. break;
  803. }
  804. }
  805. out_unlock:
  806. spin_unlock(&hb1->lock);
  807. if (hb1 != hb2)
  808. spin_unlock(&hb2->lock);
  809. /* drop_futex_key_refs() must be called outside the spinlocks. */
  810. while (--drop_count >= 0)
  811. drop_futex_key_refs(&key1);
  812. out:
  813. if (fshared)
  814. up_read(fshared);
  815. return ret;
  816. }
  817. /* The key must be already stored in q->key. */
  818. static inline struct futex_hash_bucket *
  819. queue_lock(struct futex_q *q, int fd, struct file *filp)
  820. {
  821. struct futex_hash_bucket *hb;
  822. q->fd = fd;
  823. q->filp = filp;
  824. init_waitqueue_head(&q->waiters);
  825. get_futex_key_refs(&q->key);
  826. hb = hash_futex(&q->key);
  827. q->lock_ptr = &hb->lock;
  828. spin_lock(&hb->lock);
  829. return hb;
  830. }
  831. static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  832. {
  833. int prio;
  834. /*
  835. * The priority used to register this element is
  836. * - either the real thread-priority for the real-time threads
  837. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  838. * - or MAX_RT_PRIO for non-RT threads.
  839. * Thus, all RT-threads are woken first in priority order, and
  840. * the others are woken last, in FIFO order.
  841. */
  842. prio = min(current->normal_prio, MAX_RT_PRIO);
  843. plist_node_init(&q->list, prio);
  844. #ifdef CONFIG_DEBUG_PI_LIST
  845. q->list.plist.lock = &hb->lock;
  846. #endif
  847. plist_add(&q->list, &hb->chain);
  848. q->task = current;
  849. spin_unlock(&hb->lock);
  850. }
  851. static inline void
  852. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  853. {
  854. spin_unlock(&hb->lock);
  855. drop_futex_key_refs(&q->key);
  856. }
  857. /*
  858. * queue_me and unqueue_me must be called as a pair, each
  859. * exactly once. They are called with the hashed spinlock held.
  860. */
  861. /* The key must be already stored in q->key. */
  862. static void queue_me(struct futex_q *q, int fd, struct file *filp)
  863. {
  864. struct futex_hash_bucket *hb;
  865. hb = queue_lock(q, fd, filp);
  866. __queue_me(q, hb);
  867. }
  868. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  869. static int unqueue_me(struct futex_q *q)
  870. {
  871. spinlock_t *lock_ptr;
  872. int ret = 0;
  873. /* In the common case we don't take the spinlock, which is nice. */
  874. retry:
  875. lock_ptr = q->lock_ptr;
  876. barrier();
  877. if (lock_ptr != 0) {
  878. spin_lock(lock_ptr);
  879. /*
  880. * q->lock_ptr can change between reading it and
  881. * spin_lock(), causing us to take the wrong lock. This
  882. * corrects the race condition.
  883. *
  884. * Reasoning goes like this: if we have the wrong lock,
  885. * q->lock_ptr must have changed (maybe several times)
  886. * between reading it and the spin_lock(). It can
  887. * change again after the spin_lock() but only if it was
  888. * already changed before the spin_lock(). It cannot,
  889. * however, change back to the original value. Therefore
  890. * we can detect whether we acquired the correct lock.
  891. */
  892. if (unlikely(lock_ptr != q->lock_ptr)) {
  893. spin_unlock(lock_ptr);
  894. goto retry;
  895. }
  896. WARN_ON(plist_node_empty(&q->list));
  897. plist_del(&q->list, &q->list.plist);
  898. BUG_ON(q->pi_state);
  899. spin_unlock(lock_ptr);
  900. ret = 1;
  901. }
  902. drop_futex_key_refs(&q->key);
  903. return ret;
  904. }
  905. /*
  906. * PI futexes can not be requeued and must remove themself from the
  907. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  908. * and dropped here.
  909. */
  910. static void unqueue_me_pi(struct futex_q *q)
  911. {
  912. WARN_ON(plist_node_empty(&q->list));
  913. plist_del(&q->list, &q->list.plist);
  914. BUG_ON(!q->pi_state);
  915. free_pi_state(q->pi_state);
  916. q->pi_state = NULL;
  917. spin_unlock(q->lock_ptr);
  918. drop_futex_key_refs(&q->key);
  919. }
  920. /*
  921. * Fixup the pi_state owner with current.
  922. *
  923. * Must be called with hash bucket lock held and mm->sem held for non
  924. * private futexes.
  925. */
  926. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  927. struct task_struct *curr)
  928. {
  929. u32 newtid = curr->pid | FUTEX_WAITERS;
  930. struct futex_pi_state *pi_state = q->pi_state;
  931. u32 uval, curval, newval;
  932. int ret;
  933. /* Owner died? */
  934. if (pi_state->owner != NULL) {
  935. spin_lock_irq(&pi_state->owner->pi_lock);
  936. WARN_ON(list_empty(&pi_state->list));
  937. list_del_init(&pi_state->list);
  938. spin_unlock_irq(&pi_state->owner->pi_lock);
  939. } else
  940. newtid |= FUTEX_OWNER_DIED;
  941. pi_state->owner = curr;
  942. spin_lock_irq(&curr->pi_lock);
  943. WARN_ON(!list_empty(&pi_state->list));
  944. list_add(&pi_state->list, &curr->pi_state_list);
  945. spin_unlock_irq(&curr->pi_lock);
  946. /*
  947. * We own it, so we have to replace the pending owner
  948. * TID. This must be atomic as we have preserve the
  949. * owner died bit here.
  950. */
  951. ret = get_futex_value_locked(&uval, uaddr);
  952. while (!ret) {
  953. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  954. pagefault_disable();
  955. curval = futex_atomic_cmpxchg_inatomic(uaddr,
  956. uval, newval);
  957. pagefault_enable();
  958. if (curval == -EFAULT)
  959. ret = -EFAULT;
  960. if (curval == uval)
  961. break;
  962. uval = curval;
  963. }
  964. return ret;
  965. }
  966. /*
  967. * In case we must use restart_block to restart a futex_wait,
  968. * we encode in the 'arg3' shared capability
  969. */
  970. #define ARG3_SHARED 1
  971. static long futex_wait_restart(struct restart_block *restart);
  972. static int futex_wait(u32 __user *uaddr, struct rw_semaphore *fshared,
  973. u32 val, ktime_t *abs_time)
  974. {
  975. struct task_struct *curr = current;
  976. DECLARE_WAITQUEUE(wait, curr);
  977. struct futex_hash_bucket *hb;
  978. struct futex_q q;
  979. u32 uval;
  980. int ret;
  981. struct hrtimer_sleeper t;
  982. int rem = 0;
  983. q.pi_state = NULL;
  984. retry:
  985. if (fshared)
  986. down_read(fshared);
  987. ret = get_futex_key(uaddr, fshared, &q.key);
  988. if (unlikely(ret != 0))
  989. goto out_release_sem;
  990. hb = queue_lock(&q, -1, NULL);
  991. /*
  992. * Access the page AFTER the futex is queued.
  993. * Order is important:
  994. *
  995. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  996. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  997. *
  998. * The basic logical guarantee of a futex is that it blocks ONLY
  999. * if cond(var) is known to be true at the time of blocking, for
  1000. * any cond. If we queued after testing *uaddr, that would open
  1001. * a race condition where we could block indefinitely with
  1002. * cond(var) false, which would violate the guarantee.
  1003. *
  1004. * A consequence is that futex_wait() can return zero and absorb
  1005. * a wakeup when *uaddr != val on entry to the syscall. This is
  1006. * rare, but normal.
  1007. *
  1008. * for shared futexes, we hold the mmap semaphore, so the mapping
  1009. * cannot have changed since we looked it up in get_futex_key.
  1010. */
  1011. ret = get_futex_value_locked(&uval, uaddr);
  1012. if (unlikely(ret)) {
  1013. queue_unlock(&q, hb);
  1014. /*
  1015. * If we would have faulted, release mmap_sem, fault it in and
  1016. * start all over again.
  1017. */
  1018. if (fshared)
  1019. up_read(fshared);
  1020. ret = get_user(uval, uaddr);
  1021. if (!ret)
  1022. goto retry;
  1023. return ret;
  1024. }
  1025. ret = -EWOULDBLOCK;
  1026. if (uval != val)
  1027. goto out_unlock_release_sem;
  1028. /* Only actually queue if *uaddr contained val. */
  1029. __queue_me(&q, hb);
  1030. /*
  1031. * Now the futex is queued and we have checked the data, we
  1032. * don't want to hold mmap_sem while we sleep.
  1033. */
  1034. if (fshared)
  1035. up_read(fshared);
  1036. /*
  1037. * There might have been scheduling since the queue_me(), as we
  1038. * cannot hold a spinlock across the get_user() in case it
  1039. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  1040. * queueing ourselves into the futex hash. This code thus has to
  1041. * rely on the futex_wake() code removing us from hash when it
  1042. * wakes us up.
  1043. */
  1044. /* add_wait_queue is the barrier after __set_current_state. */
  1045. __set_current_state(TASK_INTERRUPTIBLE);
  1046. add_wait_queue(&q.waiters, &wait);
  1047. /*
  1048. * !plist_node_empty() is safe here without any lock.
  1049. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  1050. */
  1051. if (likely(!plist_node_empty(&q.list))) {
  1052. if (!abs_time)
  1053. schedule();
  1054. else {
  1055. hrtimer_init(&t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1056. hrtimer_init_sleeper(&t, current);
  1057. t.timer.expires = *abs_time;
  1058. hrtimer_start(&t.timer, t.timer.expires, HRTIMER_MODE_ABS);
  1059. /*
  1060. * the timer could have already expired, in which
  1061. * case current would be flagged for rescheduling.
  1062. * Don't bother calling schedule.
  1063. */
  1064. if (likely(t.task))
  1065. schedule();
  1066. hrtimer_cancel(&t.timer);
  1067. /* Flag if a timeout occured */
  1068. rem = (t.task == NULL);
  1069. }
  1070. }
  1071. __set_current_state(TASK_RUNNING);
  1072. /*
  1073. * NOTE: we don't remove ourselves from the waitqueue because
  1074. * we are the only user of it.
  1075. */
  1076. /* If we were woken (and unqueued), we succeeded, whatever. */
  1077. if (!unqueue_me(&q))
  1078. return 0;
  1079. if (rem)
  1080. return -ETIMEDOUT;
  1081. /*
  1082. * We expect signal_pending(current), but another thread may
  1083. * have handled it for us already.
  1084. */
  1085. if (!abs_time)
  1086. return -ERESTARTSYS;
  1087. else {
  1088. struct restart_block *restart;
  1089. restart = &current_thread_info()->restart_block;
  1090. restart->fn = futex_wait_restart;
  1091. restart->arg0 = (unsigned long)uaddr;
  1092. restart->arg1 = (unsigned long)val;
  1093. restart->arg2 = (unsigned long)abs_time;
  1094. restart->arg3 = 0;
  1095. if (fshared)
  1096. restart->arg3 |= ARG3_SHARED;
  1097. return -ERESTART_RESTARTBLOCK;
  1098. }
  1099. out_unlock_release_sem:
  1100. queue_unlock(&q, hb);
  1101. out_release_sem:
  1102. if (fshared)
  1103. up_read(fshared);
  1104. return ret;
  1105. }
  1106. static long futex_wait_restart(struct restart_block *restart)
  1107. {
  1108. u32 __user *uaddr = (u32 __user *)restart->arg0;
  1109. u32 val = (u32)restart->arg1;
  1110. ktime_t *abs_time = (ktime_t *)restart->arg2;
  1111. struct rw_semaphore *fshared = NULL;
  1112. restart->fn = do_no_restart_syscall;
  1113. if (restart->arg3 & ARG3_SHARED)
  1114. fshared = &current->mm->mmap_sem;
  1115. return (long)futex_wait(uaddr, fshared, val, abs_time);
  1116. }
  1117. /*
  1118. * Userspace tried a 0 -> TID atomic transition of the futex value
  1119. * and failed. The kernel side here does the whole locking operation:
  1120. * if there are waiters then it will block, it does PI, etc. (Due to
  1121. * races the kernel might see a 0 value of the futex too.)
  1122. */
  1123. static int futex_lock_pi(u32 __user *uaddr, struct rw_semaphore *fshared,
  1124. int detect, ktime_t *time, int trylock)
  1125. {
  1126. struct hrtimer_sleeper timeout, *to = NULL;
  1127. struct task_struct *curr = current;
  1128. struct futex_hash_bucket *hb;
  1129. u32 uval, newval, curval;
  1130. struct futex_q q;
  1131. int ret, lock_taken, ownerdied = 0, attempt = 0;
  1132. if (refill_pi_state_cache())
  1133. return -ENOMEM;
  1134. if (time) {
  1135. to = &timeout;
  1136. hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
  1137. hrtimer_init_sleeper(to, current);
  1138. to->timer.expires = *time;
  1139. }
  1140. q.pi_state = NULL;
  1141. retry:
  1142. if (fshared)
  1143. down_read(fshared);
  1144. ret = get_futex_key(uaddr, fshared, &q.key);
  1145. if (unlikely(ret != 0))
  1146. goto out_release_sem;
  1147. retry_unlocked:
  1148. hb = queue_lock(&q, -1, NULL);
  1149. retry_locked:
  1150. ret = lock_taken = 0;
  1151. /*
  1152. * To avoid races, we attempt to take the lock here again
  1153. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  1154. * the locks. It will most likely not succeed.
  1155. */
  1156. newval = current->pid;
  1157. pagefault_disable();
  1158. curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
  1159. pagefault_enable();
  1160. if (unlikely(curval == -EFAULT))
  1161. goto uaddr_faulted;
  1162. /*
  1163. * Detect deadlocks. In case of REQUEUE_PI this is a valid
  1164. * situation and we return success to user space.
  1165. */
  1166. if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
  1167. ret = -EDEADLK;
  1168. goto out_unlock_release_sem;
  1169. }
  1170. /*
  1171. * Surprise - we got the lock. Just return to userspace:
  1172. */
  1173. if (unlikely(!curval))
  1174. goto out_unlock_release_sem;
  1175. uval = curval;
  1176. /*
  1177. * Set the WAITERS flag, so the owner will know it has someone
  1178. * to wake at next unlock
  1179. */
  1180. newval = curval | FUTEX_WAITERS;
  1181. /*
  1182. * There are two cases, where a futex might have no owner (the
  1183. * owner TID is 0): OWNER_DIED. We take over the futex in this
  1184. * case. We also do an unconditional take over, when the owner
  1185. * of the futex died.
  1186. *
  1187. * This is safe as we are protected by the hash bucket lock !
  1188. */
  1189. if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
  1190. /* Keep the OWNER_DIED bit */
  1191. newval = (curval & ~FUTEX_TID_MASK) | current->pid;
  1192. ownerdied = 0;
  1193. lock_taken = 1;
  1194. }
  1195. pagefault_disable();
  1196. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  1197. pagefault_enable();
  1198. if (unlikely(curval == -EFAULT))
  1199. goto uaddr_faulted;
  1200. if (unlikely(curval != uval))
  1201. goto retry_locked;
  1202. /*
  1203. * We took the lock due to owner died take over.
  1204. */
  1205. if (unlikely(lock_taken))
  1206. goto out_unlock_release_sem;
  1207. /*
  1208. * We dont have the lock. Look up the PI state (or create it if
  1209. * we are the first waiter):
  1210. */
  1211. ret = lookup_pi_state(uval, hb, &q.key, &q.pi_state);
  1212. if (unlikely(ret)) {
  1213. switch (ret) {
  1214. case -EAGAIN:
  1215. /*
  1216. * Task is exiting and we just wait for the
  1217. * exit to complete.
  1218. */
  1219. queue_unlock(&q, hb);
  1220. if (fshared)
  1221. up_read(fshared);
  1222. cond_resched();
  1223. goto retry;
  1224. case -ESRCH:
  1225. /*
  1226. * No owner found for this futex. Check if the
  1227. * OWNER_DIED bit is set to figure out whether
  1228. * this is a robust futex or not.
  1229. */
  1230. if (get_futex_value_locked(&curval, uaddr))
  1231. goto uaddr_faulted;
  1232. /*
  1233. * We simply start over in case of a robust
  1234. * futex. The code above will take the futex
  1235. * and return happy.
  1236. */
  1237. if (curval & FUTEX_OWNER_DIED) {
  1238. ownerdied = 1;
  1239. goto retry_locked;
  1240. }
  1241. default:
  1242. goto out_unlock_release_sem;
  1243. }
  1244. }
  1245. /*
  1246. * Only actually queue now that the atomic ops are done:
  1247. */
  1248. __queue_me(&q, hb);
  1249. /*
  1250. * Now the futex is queued and we have checked the data, we
  1251. * don't want to hold mmap_sem while we sleep.
  1252. */
  1253. if (fshared)
  1254. up_read(fshared);
  1255. WARN_ON(!q.pi_state);
  1256. /*
  1257. * Block on the PI mutex:
  1258. */
  1259. if (!trylock)
  1260. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1261. else {
  1262. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1263. /* Fixup the trylock return value: */
  1264. ret = ret ? 0 : -EWOULDBLOCK;
  1265. }
  1266. if (fshared)
  1267. down_read(fshared);
  1268. spin_lock(q.lock_ptr);
  1269. if (!ret) {
  1270. /*
  1271. * Got the lock. We might not be the anticipated owner
  1272. * if we did a lock-steal - fix up the PI-state in
  1273. * that case:
  1274. */
  1275. if (q.pi_state->owner != curr)
  1276. ret = fixup_pi_state_owner(uaddr, &q, curr);
  1277. } else {
  1278. /*
  1279. * Catch the rare case, where the lock was released
  1280. * when we were on the way back before we locked the
  1281. * hash bucket.
  1282. */
  1283. if (q.pi_state->owner == curr &&
  1284. rt_mutex_trylock(&q.pi_state->pi_mutex)) {
  1285. ret = 0;
  1286. } else {
  1287. /*
  1288. * Paranoia check. If we did not take the lock
  1289. * in the trylock above, then we should not be
  1290. * the owner of the rtmutex, neither the real
  1291. * nor the pending one:
  1292. */
  1293. if (rt_mutex_owner(&q.pi_state->pi_mutex) == curr)
  1294. printk(KERN_ERR "futex_lock_pi: ret = %d "
  1295. "pi-mutex: %p pi-state %p\n", ret,
  1296. q.pi_state->pi_mutex.owner,
  1297. q.pi_state->owner);
  1298. }
  1299. }
  1300. /* Unqueue and drop the lock */
  1301. unqueue_me_pi(&q);
  1302. if (fshared)
  1303. up_read(fshared);
  1304. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  1305. out_unlock_release_sem:
  1306. queue_unlock(&q, hb);
  1307. out_release_sem:
  1308. if (fshared)
  1309. up_read(fshared);
  1310. return ret;
  1311. uaddr_faulted:
  1312. /*
  1313. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1314. * non-atomically. Therefore, if get_user below is not
  1315. * enough, we need to handle the fault ourselves, while
  1316. * still holding the mmap_sem.
  1317. *
  1318. * ... and hb->lock. :-) --ANK
  1319. */
  1320. queue_unlock(&q, hb);
  1321. if (attempt++) {
  1322. ret = futex_handle_fault((unsigned long)uaddr, fshared,
  1323. attempt);
  1324. if (ret)
  1325. goto out_release_sem;
  1326. goto retry_unlocked;
  1327. }
  1328. if (fshared)
  1329. up_read(fshared);
  1330. ret = get_user(uval, uaddr);
  1331. if (!ret && (uval != -EFAULT))
  1332. goto retry;
  1333. return ret;
  1334. }
  1335. /*
  1336. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1337. * This is the in-kernel slowpath: we look up the PI state (if any),
  1338. * and do the rt-mutex unlock.
  1339. */
  1340. static int futex_unlock_pi(u32 __user *uaddr, struct rw_semaphore *fshared)
  1341. {
  1342. struct futex_hash_bucket *hb;
  1343. struct futex_q *this, *next;
  1344. u32 uval;
  1345. struct plist_head *head;
  1346. union futex_key key;
  1347. int ret, attempt = 0;
  1348. retry:
  1349. if (get_user(uval, uaddr))
  1350. return -EFAULT;
  1351. /*
  1352. * We release only a lock we actually own:
  1353. */
  1354. if ((uval & FUTEX_TID_MASK) != current->pid)
  1355. return -EPERM;
  1356. /*
  1357. * First take all the futex related locks:
  1358. */
  1359. if (fshared)
  1360. down_read(fshared);
  1361. ret = get_futex_key(uaddr, fshared, &key);
  1362. if (unlikely(ret != 0))
  1363. goto out;
  1364. hb = hash_futex(&key);
  1365. retry_unlocked:
  1366. spin_lock(&hb->lock);
  1367. /*
  1368. * To avoid races, try to do the TID -> 0 atomic transition
  1369. * again. If it succeeds then we can return without waking
  1370. * anyone else up:
  1371. */
  1372. if (!(uval & FUTEX_OWNER_DIED)) {
  1373. pagefault_disable();
  1374. uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
  1375. pagefault_enable();
  1376. }
  1377. if (unlikely(uval == -EFAULT))
  1378. goto pi_faulted;
  1379. /*
  1380. * Rare case: we managed to release the lock atomically,
  1381. * no need to wake anyone else up:
  1382. */
  1383. if (unlikely(uval == current->pid))
  1384. goto out_unlock;
  1385. /*
  1386. * Ok, other tasks may need to be woken up - check waiters
  1387. * and do the wakeup if necessary:
  1388. */
  1389. head = &hb->chain;
  1390. plist_for_each_entry_safe(this, next, head, list) {
  1391. if (!match_futex (&this->key, &key))
  1392. continue;
  1393. ret = wake_futex_pi(uaddr, uval, this);
  1394. /*
  1395. * The atomic access to the futex value
  1396. * generated a pagefault, so retry the
  1397. * user-access and the wakeup:
  1398. */
  1399. if (ret == -EFAULT)
  1400. goto pi_faulted;
  1401. goto out_unlock;
  1402. }
  1403. /*
  1404. * No waiters - kernel unlocks the futex:
  1405. */
  1406. if (!(uval & FUTEX_OWNER_DIED)) {
  1407. ret = unlock_futex_pi(uaddr, uval);
  1408. if (ret == -EFAULT)
  1409. goto pi_faulted;
  1410. }
  1411. out_unlock:
  1412. spin_unlock(&hb->lock);
  1413. out:
  1414. if (fshared)
  1415. up_read(fshared);
  1416. return ret;
  1417. pi_faulted:
  1418. /*
  1419. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1420. * non-atomically. Therefore, if get_user below is not
  1421. * enough, we need to handle the fault ourselves, while
  1422. * still holding the mmap_sem.
  1423. *
  1424. * ... and hb->lock. --ANK
  1425. */
  1426. spin_unlock(&hb->lock);
  1427. if (attempt++) {
  1428. ret = futex_handle_fault((unsigned long)uaddr, fshared,
  1429. attempt);
  1430. if (ret)
  1431. goto out;
  1432. goto retry_unlocked;
  1433. }
  1434. if (fshared)
  1435. up_read(fshared);
  1436. ret = get_user(uval, uaddr);
  1437. if (!ret && (uval != -EFAULT))
  1438. goto retry;
  1439. return ret;
  1440. }
  1441. static int futex_close(struct inode *inode, struct file *filp)
  1442. {
  1443. struct futex_q *q = filp->private_data;
  1444. unqueue_me(q);
  1445. kfree(q);
  1446. return 0;
  1447. }
  1448. /* This is one-shot: once it's gone off you need a new fd */
  1449. static unsigned int futex_poll(struct file *filp,
  1450. struct poll_table_struct *wait)
  1451. {
  1452. struct futex_q *q = filp->private_data;
  1453. int ret = 0;
  1454. poll_wait(filp, &q->waiters, wait);
  1455. /*
  1456. * plist_node_empty() is safe here without any lock.
  1457. * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
  1458. */
  1459. if (plist_node_empty(&q->list))
  1460. ret = POLLIN | POLLRDNORM;
  1461. return ret;
  1462. }
  1463. static const struct file_operations futex_fops = {
  1464. .release = futex_close,
  1465. .poll = futex_poll,
  1466. };
  1467. /*
  1468. * Signal allows caller to avoid the race which would occur if they
  1469. * set the sigio stuff up afterwards.
  1470. */
  1471. static int futex_fd(u32 __user *uaddr, int signal)
  1472. {
  1473. struct futex_q *q;
  1474. struct file *filp;
  1475. int ret, err;
  1476. struct rw_semaphore *fshared;
  1477. static unsigned long printk_interval;
  1478. if (printk_timed_ratelimit(&printk_interval, 60 * 60 * 1000)) {
  1479. printk(KERN_WARNING "Process `%s' used FUTEX_FD, which "
  1480. "will be removed from the kernel in June 2007\n",
  1481. current->comm);
  1482. }
  1483. ret = -EINVAL;
  1484. if (!valid_signal(signal))
  1485. goto out;
  1486. ret = get_unused_fd();
  1487. if (ret < 0)
  1488. goto out;
  1489. filp = get_empty_filp();
  1490. if (!filp) {
  1491. put_unused_fd(ret);
  1492. ret = -ENFILE;
  1493. goto out;
  1494. }
  1495. filp->f_op = &futex_fops;
  1496. filp->f_path.mnt = mntget(futex_mnt);
  1497. filp->f_path.dentry = dget(futex_mnt->mnt_root);
  1498. filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
  1499. if (signal) {
  1500. err = __f_setown(filp, task_pid(current), PIDTYPE_PID, 1);
  1501. if (err < 0) {
  1502. goto error;
  1503. }
  1504. filp->f_owner.signum = signal;
  1505. }
  1506. q = kmalloc(sizeof(*q), GFP_KERNEL);
  1507. if (!q) {
  1508. err = -ENOMEM;
  1509. goto error;
  1510. }
  1511. q->pi_state = NULL;
  1512. fshared = &current->mm->mmap_sem;
  1513. down_read(fshared);
  1514. err = get_futex_key(uaddr, fshared, &q->key);
  1515. if (unlikely(err != 0)) {
  1516. up_read(fshared);
  1517. kfree(q);
  1518. goto error;
  1519. }
  1520. /*
  1521. * queue_me() must be called before releasing mmap_sem, because
  1522. * key->shared.inode needs to be referenced while holding it.
  1523. */
  1524. filp->private_data = q;
  1525. queue_me(q, ret, filp);
  1526. up_read(fshared);
  1527. /* Now we map fd to filp, so userspace can access it */
  1528. fd_install(ret, filp);
  1529. out:
  1530. return ret;
  1531. error:
  1532. put_unused_fd(ret);
  1533. put_filp(filp);
  1534. ret = err;
  1535. goto out;
  1536. }
  1537. /*
  1538. * Support for robust futexes: the kernel cleans up held futexes at
  1539. * thread exit time.
  1540. *
  1541. * Implementation: user-space maintains a per-thread list of locks it
  1542. * is holding. Upon do_exit(), the kernel carefully walks this list,
  1543. * and marks all locks that are owned by this thread with the
  1544. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  1545. * always manipulated with the lock held, so the list is private and
  1546. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  1547. * field, to allow the kernel to clean up if the thread dies after
  1548. * acquiring the lock, but just before it could have added itself to
  1549. * the list. There can only be one such pending lock.
  1550. */
  1551. /**
  1552. * sys_set_robust_list - set the robust-futex list head of a task
  1553. * @head: pointer to the list-head
  1554. * @len: length of the list-head, as userspace expects
  1555. */
  1556. asmlinkage long
  1557. sys_set_robust_list(struct robust_list_head __user *head,
  1558. size_t len)
  1559. {
  1560. /*
  1561. * The kernel knows only one size for now:
  1562. */
  1563. if (unlikely(len != sizeof(*head)))
  1564. return -EINVAL;
  1565. current->robust_list = head;
  1566. return 0;
  1567. }
  1568. /**
  1569. * sys_get_robust_list - get the robust-futex list head of a task
  1570. * @pid: pid of the process [zero for current task]
  1571. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  1572. * @len_ptr: pointer to a length field, the kernel fills in the header size
  1573. */
  1574. asmlinkage long
  1575. sys_get_robust_list(int pid, struct robust_list_head __user * __user *head_ptr,
  1576. size_t __user *len_ptr)
  1577. {
  1578. struct robust_list_head __user *head;
  1579. unsigned long ret;
  1580. if (!pid)
  1581. head = current->robust_list;
  1582. else {
  1583. struct task_struct *p;
  1584. ret = -ESRCH;
  1585. rcu_read_lock();
  1586. p = find_task_by_pid(pid);
  1587. if (!p)
  1588. goto err_unlock;
  1589. ret = -EPERM;
  1590. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  1591. !capable(CAP_SYS_PTRACE))
  1592. goto err_unlock;
  1593. head = p->robust_list;
  1594. rcu_read_unlock();
  1595. }
  1596. if (put_user(sizeof(*head), len_ptr))
  1597. return -EFAULT;
  1598. return put_user(head, head_ptr);
  1599. err_unlock:
  1600. rcu_read_unlock();
  1601. return ret;
  1602. }
  1603. /*
  1604. * Process a futex-list entry, check whether it's owned by the
  1605. * dying task, and do notification if so:
  1606. */
  1607. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
  1608. {
  1609. u32 uval, nval, mval;
  1610. retry:
  1611. if (get_user(uval, uaddr))
  1612. return -1;
  1613. if ((uval & FUTEX_TID_MASK) == curr->pid) {
  1614. /*
  1615. * Ok, this dying thread is truly holding a futex
  1616. * of interest. Set the OWNER_DIED bit atomically
  1617. * via cmpxchg, and if the value had FUTEX_WAITERS
  1618. * set, wake up a waiter (if any). (We have to do a
  1619. * futex_wake() even if OWNER_DIED is already set -
  1620. * to handle the rare but possible case of recursive
  1621. * thread-death.) The rest of the cleanup is done in
  1622. * userspace.
  1623. */
  1624. mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  1625. nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
  1626. if (nval == -EFAULT)
  1627. return -1;
  1628. if (nval != uval)
  1629. goto retry;
  1630. /*
  1631. * Wake robust non-PI futexes here. The wakeup of
  1632. * PI futexes happens in exit_pi_state():
  1633. */
  1634. if (!pi) {
  1635. if (uval & FUTEX_WAITERS)
  1636. futex_wake(uaddr, &curr->mm->mmap_sem, 1);
  1637. }
  1638. }
  1639. return 0;
  1640. }
  1641. /*
  1642. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  1643. */
  1644. static inline int fetch_robust_entry(struct robust_list __user **entry,
  1645. struct robust_list __user * __user *head,
  1646. int *pi)
  1647. {
  1648. unsigned long uentry;
  1649. if (get_user(uentry, (unsigned long __user *)head))
  1650. return -EFAULT;
  1651. *entry = (void __user *)(uentry & ~1UL);
  1652. *pi = uentry & 1;
  1653. return 0;
  1654. }
  1655. /*
  1656. * Walk curr->robust_list (very carefully, it's a userspace list!)
  1657. * and mark any locks found there dead, and notify any waiters.
  1658. *
  1659. * We silently return on any sign of list-walking problem.
  1660. */
  1661. void exit_robust_list(struct task_struct *curr)
  1662. {
  1663. struct robust_list_head __user *head = curr->robust_list;
  1664. struct robust_list __user *entry, *pending;
  1665. unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
  1666. unsigned long futex_offset;
  1667. /*
  1668. * Fetch the list head (which was registered earlier, via
  1669. * sys_set_robust_list()):
  1670. */
  1671. if (fetch_robust_entry(&entry, &head->list.next, &pi))
  1672. return;
  1673. /*
  1674. * Fetch the relative futex offset:
  1675. */
  1676. if (get_user(futex_offset, &head->futex_offset))
  1677. return;
  1678. /*
  1679. * Fetch any possibly pending lock-add first, and handle it
  1680. * if it exists:
  1681. */
  1682. if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
  1683. return;
  1684. if (pending)
  1685. handle_futex_death((void __user *)pending + futex_offset,
  1686. curr, pip);
  1687. while (entry != &head->list) {
  1688. /*
  1689. * A pending lock might already be on the list, so
  1690. * don't process it twice:
  1691. */
  1692. if (entry != pending)
  1693. if (handle_futex_death((void __user *)entry + futex_offset,
  1694. curr, pi))
  1695. return;
  1696. /*
  1697. * Fetch the next entry in the list:
  1698. */
  1699. if (fetch_robust_entry(&entry, &entry->next, &pi))
  1700. return;
  1701. /*
  1702. * Avoid excessively long or circular lists:
  1703. */
  1704. if (!--limit)
  1705. break;
  1706. cond_resched();
  1707. }
  1708. }
  1709. long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
  1710. u32 __user *uaddr2, u32 val2, u32 val3)
  1711. {
  1712. int ret;
  1713. int cmd = op & FUTEX_CMD_MASK;
  1714. struct rw_semaphore *fshared = NULL;
  1715. if (!(op & FUTEX_PRIVATE_FLAG))
  1716. fshared = &current->mm->mmap_sem;
  1717. switch (cmd) {
  1718. case FUTEX_WAIT:
  1719. ret = futex_wait(uaddr, fshared, val, timeout);
  1720. break;
  1721. case FUTEX_WAKE:
  1722. ret = futex_wake(uaddr, fshared, val);
  1723. break;
  1724. case FUTEX_FD:
  1725. /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
  1726. ret = futex_fd(uaddr, val);
  1727. break;
  1728. case FUTEX_REQUEUE:
  1729. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL);
  1730. break;
  1731. case FUTEX_CMP_REQUEUE:
  1732. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3);
  1733. break;
  1734. case FUTEX_WAKE_OP:
  1735. ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
  1736. break;
  1737. case FUTEX_LOCK_PI:
  1738. ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
  1739. break;
  1740. case FUTEX_UNLOCK_PI:
  1741. ret = futex_unlock_pi(uaddr, fshared);
  1742. break;
  1743. case FUTEX_TRYLOCK_PI:
  1744. ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
  1745. break;
  1746. default:
  1747. ret = -ENOSYS;
  1748. }
  1749. return ret;
  1750. }
  1751. asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
  1752. struct timespec __user *utime, u32 __user *uaddr2,
  1753. u32 val3)
  1754. {
  1755. struct timespec ts;
  1756. ktime_t t, *tp = NULL;
  1757. u32 val2 = 0;
  1758. int cmd = op & FUTEX_CMD_MASK;
  1759. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
  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(ktime_get(), t);
  1767. tp = &t;
  1768. }
  1769. /*
  1770. * requeue parameter in 'utime' if cmd == FUTEX_REQUEUE.
  1771. */
  1772. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE)
  1773. val2 = (u32) (unsigned long) utime;
  1774. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  1775. }
  1776. static int futexfs_get_sb(struct file_system_type *fs_type,
  1777. int flags, const char *dev_name, void *data,
  1778. struct vfsmount *mnt)
  1779. {
  1780. return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
  1781. }
  1782. static struct file_system_type futex_fs_type = {
  1783. .name = "futexfs",
  1784. .get_sb = futexfs_get_sb,
  1785. .kill_sb = kill_anon_super,
  1786. };
  1787. static int __init init(void)
  1788. {
  1789. int i = register_filesystem(&futex_fs_type);
  1790. if (i)
  1791. return i;
  1792. futex_mnt = kern_mount(&futex_fs_type);
  1793. if (IS_ERR(futex_mnt)) {
  1794. unregister_filesystem(&futex_fs_type);
  1795. return PTR_ERR(futex_mnt);
  1796. }
  1797. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  1798. plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
  1799. spin_lock_init(&futex_queues[i].lock);
  1800. }
  1801. return 0;
  1802. }
  1803. __initcall(init);