futex.c 49 KB

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