futex.c 45 KB

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