futex.c 58 KB

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