futex.c 48 KB

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