futex.c 48 KB

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