futex.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609
  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. * Requeue-PI support by Darren Hart <dvhltc@us.ibm.com>
  23. * Copyright (C) IBM Corporation, 2009
  24. * Thanks to Thomas Gleixner for conceptual design and careful reviews.
  25. *
  26. * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
  27. * enough at me, Linus for the original (flawed) idea, Matthew
  28. * Kirkwood for proof-of-concept implementation.
  29. *
  30. * "The futexes are also cursed."
  31. * "But they come in a choice of three flavours!"
  32. *
  33. * This program is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2 of the License, or
  36. * (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License
  44. * along with this program; if not, write to the Free Software
  45. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  46. */
  47. #include <linux/slab.h>
  48. #include <linux/poll.h>
  49. #include <linux/fs.h>
  50. #include <linux/file.h>
  51. #include <linux/jhash.h>
  52. #include <linux/init.h>
  53. #include <linux/futex.h>
  54. #include <linux/mount.h>
  55. #include <linux/pagemap.h>
  56. #include <linux/syscalls.h>
  57. #include <linux/signal.h>
  58. #include <linux/module.h>
  59. #include <linux/magic.h>
  60. #include <linux/pid.h>
  61. #include <linux/nsproxy.h>
  62. #include <asm/futex.h>
  63. #include "rtmutex_common.h"
  64. int __read_mostly futex_cmpxchg_enabled;
  65. #define FUTEX_HASHBITS (CONFIG_BASE_SMALL ? 4 : 8)
  66. /*
  67. * Priority Inheritance state:
  68. */
  69. struct futex_pi_state {
  70. /*
  71. * list of 'owned' pi_state instances - these have to be
  72. * cleaned up in do_exit() if the task exits prematurely:
  73. */
  74. struct list_head list;
  75. /*
  76. * The PI object:
  77. */
  78. struct rt_mutex pi_mutex;
  79. struct task_struct *owner;
  80. atomic_t refcount;
  81. union futex_key key;
  82. };
  83. /*
  84. * We use this hashed waitqueue instead of a normal wait_queue_t, so
  85. * we can wake only the relevant ones (hashed queues may be shared).
  86. *
  87. * A futex_q has a woken state, just like tasks have TASK_RUNNING.
  88. * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
  89. * The order of wakup is always to make the first condition true, then
  90. * wake up q->waiter, then make the second condition true.
  91. */
  92. struct futex_q {
  93. struct plist_node list;
  94. /* There can only be a single waiter */
  95. wait_queue_head_t waiter;
  96. /* Which hash list lock to use: */
  97. spinlock_t *lock_ptr;
  98. /* Key which the futex is hashed on: */
  99. union futex_key key;
  100. /* Optional priority inheritance state: */
  101. struct futex_pi_state *pi_state;
  102. struct task_struct *task;
  103. /* rt_waiter storage for requeue_pi: */
  104. struct rt_mutex_waiter *rt_waiter;
  105. /* Bitset for the optional bitmasked wakeup */
  106. u32 bitset;
  107. };
  108. /*
  109. * Hash buckets are shared by all the futex_keys that hash to the same
  110. * location. Each key may have multiple futex_q structures, one for each task
  111. * waiting on a futex.
  112. */
  113. struct futex_hash_bucket {
  114. spinlock_t lock;
  115. struct plist_head chain;
  116. };
  117. static struct futex_hash_bucket futex_queues[1<<FUTEX_HASHBITS];
  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. * Take a reference to the resource addressed by a key.
  139. * Can be called while holding spinlocks.
  140. *
  141. */
  142. static void get_futex_key_refs(union futex_key *key)
  143. {
  144. if (!key->both.ptr)
  145. return;
  146. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  147. case FUT_OFF_INODE:
  148. atomic_inc(&key->shared.inode->i_count);
  149. break;
  150. case FUT_OFF_MMSHARED:
  151. atomic_inc(&key->private.mm->mm_count);
  152. break;
  153. }
  154. }
  155. /*
  156. * Drop a reference to the resource addressed by a key.
  157. * The hash bucket spinlock must not be held.
  158. */
  159. static void drop_futex_key_refs(union futex_key *key)
  160. {
  161. if (!key->both.ptr) {
  162. /* If we're here then we tried to put a key we failed to get */
  163. WARN_ON_ONCE(1);
  164. return;
  165. }
  166. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  167. case FUT_OFF_INODE:
  168. iput(key->shared.inode);
  169. break;
  170. case FUT_OFF_MMSHARED:
  171. mmdrop(key->private.mm);
  172. break;
  173. }
  174. }
  175. /**
  176. * get_futex_key - Get parameters which are the keys for a futex.
  177. * @uaddr: virtual address of the futex
  178. * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED
  179. * @key: address where result is stored.
  180. *
  181. * Returns a negative error code or 0
  182. * The key words are stored in *key on success.
  183. *
  184. * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
  185. * offset_within_page). For private mappings, it's (uaddr, current->mm).
  186. * We can usually work out the index without swapping in the page.
  187. *
  188. * lock_page() might sleep, the caller should not hold a spinlock.
  189. */
  190. static int get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key)
  191. {
  192. unsigned long address = (unsigned long)uaddr;
  193. struct mm_struct *mm = current->mm;
  194. struct page *page;
  195. int err;
  196. /*
  197. * The futex address must be "naturally" aligned.
  198. */
  199. key->both.offset = address % PAGE_SIZE;
  200. if (unlikely((address % sizeof(u32)) != 0))
  201. return -EINVAL;
  202. address -= key->both.offset;
  203. /*
  204. * PROCESS_PRIVATE futexes are fast.
  205. * As the mm cannot disappear under us and the 'key' only needs
  206. * virtual address, we dont even have to find the underlying vma.
  207. * Note : We do have to check 'uaddr' is a valid user address,
  208. * but access_ok() should be faster than find_vma()
  209. */
  210. if (!fshared) {
  211. if (unlikely(!access_ok(VERIFY_WRITE, uaddr, sizeof(u32))))
  212. return -EFAULT;
  213. key->private.mm = mm;
  214. key->private.address = address;
  215. get_futex_key_refs(key);
  216. return 0;
  217. }
  218. again:
  219. err = get_user_pages_fast(address, 1, 0, &page);
  220. if (err < 0)
  221. return err;
  222. lock_page(page);
  223. if (!page->mapping) {
  224. unlock_page(page);
  225. put_page(page);
  226. goto again;
  227. }
  228. /*
  229. * Private mappings are handled in a simple way.
  230. *
  231. * NOTE: When userspace waits on a MAP_SHARED mapping, even if
  232. * it's a read-only handle, it's expected that futexes attach to
  233. * the object not the particular process.
  234. */
  235. if (PageAnon(page)) {
  236. key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
  237. key->private.mm = mm;
  238. key->private.address = address;
  239. } else {
  240. key->both.offset |= FUT_OFF_INODE; /* inode-based key */
  241. key->shared.inode = page->mapping->host;
  242. key->shared.pgoff = page->index;
  243. }
  244. get_futex_key_refs(key);
  245. unlock_page(page);
  246. put_page(page);
  247. return 0;
  248. }
  249. static inline
  250. void put_futex_key(int fshared, union futex_key *key)
  251. {
  252. drop_futex_key_refs(key);
  253. }
  254. /**
  255. * futex_top_waiter() - Return the highest priority waiter on a futex
  256. * @hb: the hash bucket the futex_q's reside in
  257. * @key: the futex key (to distinguish it from other futex futex_q's)
  258. *
  259. * Must be called with the hb lock held.
  260. */
  261. static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
  262. union futex_key *key)
  263. {
  264. struct futex_q *this;
  265. plist_for_each_entry(this, &hb->chain, list) {
  266. if (match_futex(&this->key, key))
  267. return this;
  268. }
  269. return NULL;
  270. }
  271. static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
  272. {
  273. u32 curval;
  274. pagefault_disable();
  275. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  276. pagefault_enable();
  277. return curval;
  278. }
  279. static int get_futex_value_locked(u32 *dest, u32 __user *from)
  280. {
  281. int ret;
  282. pagefault_disable();
  283. ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
  284. pagefault_enable();
  285. return ret ? -EFAULT : 0;
  286. }
  287. /*
  288. * PI code:
  289. */
  290. static int refill_pi_state_cache(void)
  291. {
  292. struct futex_pi_state *pi_state;
  293. if (likely(current->pi_state_cache))
  294. return 0;
  295. pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
  296. if (!pi_state)
  297. return -ENOMEM;
  298. INIT_LIST_HEAD(&pi_state->list);
  299. /* pi_mutex gets initialized later */
  300. pi_state->owner = NULL;
  301. atomic_set(&pi_state->refcount, 1);
  302. pi_state->key = FUTEX_KEY_INIT;
  303. current->pi_state_cache = pi_state;
  304. return 0;
  305. }
  306. static struct futex_pi_state * alloc_pi_state(void)
  307. {
  308. struct futex_pi_state *pi_state = current->pi_state_cache;
  309. WARN_ON(!pi_state);
  310. current->pi_state_cache = NULL;
  311. return pi_state;
  312. }
  313. static void free_pi_state(struct futex_pi_state *pi_state)
  314. {
  315. if (!atomic_dec_and_test(&pi_state->refcount))
  316. return;
  317. /*
  318. * If pi_state->owner is NULL, the owner is most probably dying
  319. * and has cleaned up the pi_state already
  320. */
  321. if (pi_state->owner) {
  322. spin_lock_irq(&pi_state->owner->pi_lock);
  323. list_del_init(&pi_state->list);
  324. spin_unlock_irq(&pi_state->owner->pi_lock);
  325. rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
  326. }
  327. if (current->pi_state_cache)
  328. kfree(pi_state);
  329. else {
  330. /*
  331. * pi_state->list is already empty.
  332. * clear pi_state->owner.
  333. * refcount is at 0 - put it back to 1.
  334. */
  335. pi_state->owner = NULL;
  336. atomic_set(&pi_state->refcount, 1);
  337. current->pi_state_cache = pi_state;
  338. }
  339. }
  340. /*
  341. * Look up the task based on what TID userspace gave us.
  342. * We dont trust it.
  343. */
  344. static struct task_struct * futex_find_get_task(pid_t pid)
  345. {
  346. struct task_struct *p;
  347. const struct cred *cred = current_cred(), *pcred;
  348. rcu_read_lock();
  349. p = find_task_by_vpid(pid);
  350. if (!p) {
  351. p = ERR_PTR(-ESRCH);
  352. } else {
  353. pcred = __task_cred(p);
  354. if (cred->euid != pcred->euid &&
  355. cred->euid != pcred->uid)
  356. p = ERR_PTR(-ESRCH);
  357. else
  358. get_task_struct(p);
  359. }
  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. * futex_lock_pi_atomic() - atomic work required to acquire a pi aware futex
  484. * @uaddr: the pi futex user address
  485. * @hb: the pi futex hash bucket
  486. * @key: the futex key associated with uaddr and hb
  487. * @ps: the pi_state pointer where we store the result of the lookup
  488. * @task: the task to perform the atomic lock work for. This will be
  489. * "current" except in the case of requeue pi.
  490. *
  491. * Returns:
  492. * 0 - ready to wait
  493. * 1 - acquired the lock
  494. * <0 - error
  495. *
  496. * The hb->lock and futex_key refs shall be held by the caller.
  497. */
  498. static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
  499. union futex_key *key,
  500. struct futex_pi_state **ps,
  501. struct task_struct *task)
  502. {
  503. int lock_taken, ret, ownerdied = 0;
  504. u32 uval, newval, curval;
  505. retry:
  506. ret = lock_taken = 0;
  507. /*
  508. * To avoid races, we attempt to take the lock here again
  509. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  510. * the locks. It will most likely not succeed.
  511. */
  512. newval = task_pid_vnr(task);
  513. curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
  514. if (unlikely(curval == -EFAULT))
  515. return -EFAULT;
  516. /*
  517. * Detect deadlocks.
  518. */
  519. if ((unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(task))))
  520. return -EDEADLK;
  521. /*
  522. * Surprise - we got the lock. Just return to userspace:
  523. */
  524. if (unlikely(!curval))
  525. return 1;
  526. uval = curval;
  527. /*
  528. * Set the FUTEX_WAITERS flag, so the owner will know it has someone
  529. * to wake at the next unlock.
  530. */
  531. newval = curval | FUTEX_WAITERS;
  532. /*
  533. * There are two cases, where a futex might have no owner (the
  534. * owner TID is 0): OWNER_DIED. We take over the futex in this
  535. * case. We also do an unconditional take over, when the owner
  536. * of the futex died.
  537. *
  538. * This is safe as we are protected by the hash bucket lock !
  539. */
  540. if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
  541. /* Keep the OWNER_DIED bit */
  542. newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(task);
  543. ownerdied = 0;
  544. lock_taken = 1;
  545. }
  546. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  547. if (unlikely(curval == -EFAULT))
  548. return -EFAULT;
  549. if (unlikely(curval != uval))
  550. goto retry;
  551. /*
  552. * We took the lock due to owner died take over.
  553. */
  554. if (unlikely(lock_taken))
  555. return 1;
  556. /*
  557. * We dont have the lock. Look up the PI state (or create it if
  558. * we are the first waiter):
  559. */
  560. ret = lookup_pi_state(uval, hb, key, ps);
  561. if (unlikely(ret)) {
  562. switch (ret) {
  563. case -ESRCH:
  564. /*
  565. * No owner found for this futex. Check if the
  566. * OWNER_DIED bit is set to figure out whether
  567. * this is a robust futex or not.
  568. */
  569. if (get_futex_value_locked(&curval, uaddr))
  570. return -EFAULT;
  571. /*
  572. * We simply start over in case of a robust
  573. * futex. The code above will take the futex
  574. * and return happy.
  575. */
  576. if (curval & FUTEX_OWNER_DIED) {
  577. ownerdied = 1;
  578. goto retry;
  579. }
  580. default:
  581. break;
  582. }
  583. }
  584. return ret;
  585. }
  586. /*
  587. * The hash bucket lock must be held when this is called.
  588. * Afterwards, the futex_q must not be accessed.
  589. */
  590. static void wake_futex(struct futex_q *q)
  591. {
  592. plist_del(&q->list, &q->list.plist);
  593. /*
  594. * The lock in wake_up_all() is a crucial memory barrier after the
  595. * plist_del() and also before assigning to q->lock_ptr.
  596. */
  597. wake_up(&q->waiter);
  598. /*
  599. * The waiting task can free the futex_q as soon as this is written,
  600. * without taking any locks. This must come last.
  601. *
  602. * A memory barrier is required here to prevent the following store to
  603. * lock_ptr from getting ahead of the wakeup. Clearing the lock at the
  604. * end of wake_up() does not prevent this store from moving.
  605. */
  606. smp_wmb();
  607. q->lock_ptr = NULL;
  608. }
  609. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  610. {
  611. struct task_struct *new_owner;
  612. struct futex_pi_state *pi_state = this->pi_state;
  613. u32 curval, newval;
  614. if (!pi_state)
  615. return -EINVAL;
  616. spin_lock(&pi_state->pi_mutex.wait_lock);
  617. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  618. /*
  619. * This happens when we have stolen the lock and the original
  620. * pending owner did not enqueue itself back on the rt_mutex.
  621. * Thats not a tragedy. We know that way, that a lock waiter
  622. * is on the fly. We make the futex_q waiter the pending owner.
  623. */
  624. if (!new_owner)
  625. new_owner = this->task;
  626. /*
  627. * We pass it to the next owner. (The WAITERS bit is always
  628. * kept enabled while there is PI state around. We must also
  629. * preserve the owner died bit.)
  630. */
  631. if (!(uval & FUTEX_OWNER_DIED)) {
  632. int ret = 0;
  633. newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
  634. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  635. if (curval == -EFAULT)
  636. ret = -EFAULT;
  637. else if (curval != uval)
  638. ret = -EINVAL;
  639. if (ret) {
  640. spin_unlock(&pi_state->pi_mutex.wait_lock);
  641. return ret;
  642. }
  643. }
  644. spin_lock_irq(&pi_state->owner->pi_lock);
  645. WARN_ON(list_empty(&pi_state->list));
  646. list_del_init(&pi_state->list);
  647. spin_unlock_irq(&pi_state->owner->pi_lock);
  648. spin_lock_irq(&new_owner->pi_lock);
  649. WARN_ON(!list_empty(&pi_state->list));
  650. list_add(&pi_state->list, &new_owner->pi_state_list);
  651. pi_state->owner = new_owner;
  652. spin_unlock_irq(&new_owner->pi_lock);
  653. spin_unlock(&pi_state->pi_mutex.wait_lock);
  654. rt_mutex_unlock(&pi_state->pi_mutex);
  655. return 0;
  656. }
  657. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  658. {
  659. u32 oldval;
  660. /*
  661. * There is no waiter, so we unlock the futex. The owner died
  662. * bit has not to be preserved here. We are the owner:
  663. */
  664. oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
  665. if (oldval == -EFAULT)
  666. return oldval;
  667. if (oldval != uval)
  668. return -EAGAIN;
  669. return 0;
  670. }
  671. /*
  672. * Express the locking dependencies for lockdep:
  673. */
  674. static inline void
  675. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  676. {
  677. if (hb1 <= hb2) {
  678. spin_lock(&hb1->lock);
  679. if (hb1 < hb2)
  680. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  681. } else { /* hb1 > hb2 */
  682. spin_lock(&hb2->lock);
  683. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  684. }
  685. }
  686. static inline void
  687. double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  688. {
  689. spin_unlock(&hb1->lock);
  690. if (hb1 != hb2)
  691. spin_unlock(&hb2->lock);
  692. }
  693. /*
  694. * Wake up waiters matching bitset queued on this futex (uaddr).
  695. */
  696. static int futex_wake(u32 __user *uaddr, int fshared, int nr_wake, u32 bitset)
  697. {
  698. struct futex_hash_bucket *hb;
  699. struct futex_q *this, *next;
  700. struct plist_head *head;
  701. union futex_key key = FUTEX_KEY_INIT;
  702. int ret;
  703. if (!bitset)
  704. return -EINVAL;
  705. ret = get_futex_key(uaddr, fshared, &key);
  706. if (unlikely(ret != 0))
  707. goto out;
  708. hb = hash_futex(&key);
  709. spin_lock(&hb->lock);
  710. head = &hb->chain;
  711. plist_for_each_entry_safe(this, next, head, list) {
  712. if (match_futex (&this->key, &key)) {
  713. if (this->pi_state || this->rt_waiter) {
  714. ret = -EINVAL;
  715. break;
  716. }
  717. /* Check if one of the bits is set in both bitsets */
  718. if (!(this->bitset & bitset))
  719. continue;
  720. wake_futex(this);
  721. if (++ret >= nr_wake)
  722. break;
  723. }
  724. }
  725. spin_unlock(&hb->lock);
  726. put_futex_key(fshared, &key);
  727. out:
  728. return ret;
  729. }
  730. /*
  731. * Wake up all waiters hashed on the physical page that is mapped
  732. * to this virtual address:
  733. */
  734. static int
  735. futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
  736. int nr_wake, int nr_wake2, int op)
  737. {
  738. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  739. struct futex_hash_bucket *hb1, *hb2;
  740. struct plist_head *head;
  741. struct futex_q *this, *next;
  742. int ret, op_ret;
  743. retry:
  744. ret = get_futex_key(uaddr1, fshared, &key1);
  745. if (unlikely(ret != 0))
  746. goto out;
  747. ret = get_futex_key(uaddr2, fshared, &key2);
  748. if (unlikely(ret != 0))
  749. goto out_put_key1;
  750. hb1 = hash_futex(&key1);
  751. hb2 = hash_futex(&key2);
  752. double_lock_hb(hb1, hb2);
  753. retry_private:
  754. op_ret = futex_atomic_op_inuser(op, uaddr2);
  755. if (unlikely(op_ret < 0)) {
  756. u32 dummy;
  757. double_unlock_hb(hb1, hb2);
  758. #ifndef CONFIG_MMU
  759. /*
  760. * we don't get EFAULT from MMU faults if we don't have an MMU,
  761. * but we might get them from range checking
  762. */
  763. ret = op_ret;
  764. goto out_put_keys;
  765. #endif
  766. if (unlikely(op_ret != -EFAULT)) {
  767. ret = op_ret;
  768. goto out_put_keys;
  769. }
  770. ret = get_user(dummy, uaddr2);
  771. if (ret)
  772. goto out_put_keys;
  773. if (!fshared)
  774. goto retry_private;
  775. put_futex_key(fshared, &key2);
  776. put_futex_key(fshared, &key1);
  777. goto retry;
  778. }
  779. head = &hb1->chain;
  780. plist_for_each_entry_safe(this, next, head, list) {
  781. if (match_futex (&this->key, &key1)) {
  782. wake_futex(this);
  783. if (++ret >= nr_wake)
  784. break;
  785. }
  786. }
  787. if (op_ret > 0) {
  788. head = &hb2->chain;
  789. op_ret = 0;
  790. plist_for_each_entry_safe(this, next, head, list) {
  791. if (match_futex (&this->key, &key2)) {
  792. wake_futex(this);
  793. if (++op_ret >= nr_wake2)
  794. break;
  795. }
  796. }
  797. ret += op_ret;
  798. }
  799. double_unlock_hb(hb1, hb2);
  800. out_put_keys:
  801. put_futex_key(fshared, &key2);
  802. out_put_key1:
  803. put_futex_key(fshared, &key1);
  804. out:
  805. return ret;
  806. }
  807. /**
  808. * requeue_futex() - Requeue a futex_q from one hb to another
  809. * @q: the futex_q to requeue
  810. * @hb1: the source hash_bucket
  811. * @hb2: the target hash_bucket
  812. * @key2: the new key for the requeued futex_q
  813. */
  814. static inline
  815. void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
  816. struct futex_hash_bucket *hb2, union futex_key *key2)
  817. {
  818. /*
  819. * If key1 and key2 hash to the same bucket, no need to
  820. * requeue.
  821. */
  822. if (likely(&hb1->chain != &hb2->chain)) {
  823. plist_del(&q->list, &hb1->chain);
  824. plist_add(&q->list, &hb2->chain);
  825. q->lock_ptr = &hb2->lock;
  826. #ifdef CONFIG_DEBUG_PI_LIST
  827. q->list.plist.lock = &hb2->lock;
  828. #endif
  829. }
  830. get_futex_key_refs(key2);
  831. q->key = *key2;
  832. }
  833. /**
  834. * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
  835. * q: the futex_q
  836. * key: the key of the requeue target futex
  837. *
  838. * During futex_requeue, with requeue_pi=1, it is possible to acquire the
  839. * target futex if it is uncontended or via a lock steal. Set the futex_q key
  840. * to the requeue target futex so the waiter can detect the wakeup on the right
  841. * futex, but remove it from the hb and NULL the rt_waiter so it can detect
  842. * atomic lock acquisition. Must be called with the q->lock_ptr held.
  843. */
  844. static inline
  845. void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key)
  846. {
  847. drop_futex_key_refs(&q->key);
  848. get_futex_key_refs(key);
  849. q->key = *key;
  850. WARN_ON(plist_node_empty(&q->list));
  851. plist_del(&q->list, &q->list.plist);
  852. WARN_ON(!q->rt_waiter);
  853. q->rt_waiter = NULL;
  854. wake_up(&q->waiter);
  855. }
  856. /**
  857. * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
  858. * @pifutex: the user address of the to futex
  859. * @hb1: the from futex hash bucket, must be locked by the caller
  860. * @hb2: the to futex hash bucket, must be locked by the caller
  861. * @key1: the from futex key
  862. * @key2: the to futex key
  863. *
  864. * Try and get the lock on behalf of the top waiter if we can do it atomically.
  865. * Wake the top waiter if we succeed. hb1 and hb2 must be held by the caller.
  866. *
  867. * Returns:
  868. * 0 - failed to acquire the lock atomicly
  869. * 1 - acquired the lock
  870. * <0 - error
  871. */
  872. static int futex_proxy_trylock_atomic(u32 __user *pifutex,
  873. struct futex_hash_bucket *hb1,
  874. struct futex_hash_bucket *hb2,
  875. union futex_key *key1, union futex_key *key2,
  876. struct futex_pi_state **ps)
  877. {
  878. struct futex_q *top_waiter;
  879. u32 curval;
  880. int ret;
  881. if (get_futex_value_locked(&curval, pifutex))
  882. return -EFAULT;
  883. top_waiter = futex_top_waiter(hb1, key1);
  884. /* There are no waiters, nothing for us to do. */
  885. if (!top_waiter)
  886. return 0;
  887. /*
  888. * Either take the lock for top_waiter or set the FUTEX_WAITERS bit.
  889. * The pi_state is returned in ps in contended cases.
  890. */
  891. ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task);
  892. if (ret == 1)
  893. requeue_pi_wake_futex(top_waiter, key2);
  894. return ret;
  895. }
  896. /**
  897. * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
  898. * uaddr1: source futex user address
  899. * uaddr2: target futex user address
  900. * nr_wake: number of waiters to wake (must be 1 for requeue_pi)
  901. * nr_requeue: number of waiters to requeue (0-INT_MAX)
  902. * requeue_pi: if we are attempting to requeue from a non-pi futex to a
  903. * pi futex (pi to pi requeue is not supported)
  904. *
  905. * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
  906. * uaddr2 atomically on behalf of the top waiter.
  907. *
  908. * Returns:
  909. * >=0 - on success, the number of tasks requeued or woken
  910. * <0 - on error
  911. */
  912. static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
  913. int nr_wake, int nr_requeue, u32 *cmpval,
  914. int requeue_pi)
  915. {
  916. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  917. int drop_count = 0, task_count = 0, ret;
  918. struct futex_pi_state *pi_state = NULL;
  919. struct futex_hash_bucket *hb1, *hb2;
  920. struct plist_head *head1;
  921. struct futex_q *this, *next;
  922. u32 curval2;
  923. if (requeue_pi) {
  924. /*
  925. * requeue_pi requires a pi_state, try to allocate it now
  926. * without any locks in case it fails.
  927. */
  928. if (refill_pi_state_cache())
  929. return -ENOMEM;
  930. /*
  931. * requeue_pi must wake as many tasks as it can, up to nr_wake
  932. * + nr_requeue, since it acquires the rt_mutex prior to
  933. * returning to userspace, so as to not leave the rt_mutex with
  934. * waiters and no owner. However, second and third wake-ups
  935. * cannot be predicted as they involve race conditions with the
  936. * first wake and a fault while looking up the pi_state. Both
  937. * pthread_cond_signal() and pthread_cond_broadcast() should
  938. * use nr_wake=1.
  939. */
  940. if (nr_wake != 1)
  941. return -EINVAL;
  942. }
  943. retry:
  944. if (pi_state != NULL) {
  945. /*
  946. * We will have to lookup the pi_state again, so free this one
  947. * to keep the accounting correct.
  948. */
  949. free_pi_state(pi_state);
  950. pi_state = NULL;
  951. }
  952. ret = get_futex_key(uaddr1, fshared, &key1);
  953. if (unlikely(ret != 0))
  954. goto out;
  955. ret = get_futex_key(uaddr2, fshared, &key2);
  956. if (unlikely(ret != 0))
  957. goto out_put_key1;
  958. hb1 = hash_futex(&key1);
  959. hb2 = hash_futex(&key2);
  960. retry_private:
  961. double_lock_hb(hb1, hb2);
  962. if (likely(cmpval != NULL)) {
  963. u32 curval;
  964. ret = get_futex_value_locked(&curval, uaddr1);
  965. if (unlikely(ret)) {
  966. double_unlock_hb(hb1, hb2);
  967. ret = get_user(curval, uaddr1);
  968. if (ret)
  969. goto out_put_keys;
  970. if (!fshared)
  971. goto retry_private;
  972. put_futex_key(fshared, &key2);
  973. put_futex_key(fshared, &key1);
  974. goto retry;
  975. }
  976. if (curval != *cmpval) {
  977. ret = -EAGAIN;
  978. goto out_unlock;
  979. }
  980. }
  981. if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
  982. /* Attempt to acquire uaddr2 and wake the top_waiter. */
  983. ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
  984. &key2, &pi_state);
  985. /*
  986. * At this point the top_waiter has either taken uaddr2 or is
  987. * waiting on it. If the former, then the pi_state will not
  988. * exist yet, look it up one more time to ensure we have a
  989. * reference to it.
  990. */
  991. if (ret == 1) {
  992. WARN_ON(pi_state);
  993. task_count++;
  994. ret = get_futex_value_locked(&curval2, uaddr2);
  995. if (!ret)
  996. ret = lookup_pi_state(curval2, hb2, &key2,
  997. &pi_state);
  998. }
  999. switch (ret) {
  1000. case 0:
  1001. break;
  1002. case -EFAULT:
  1003. double_unlock_hb(hb1, hb2);
  1004. put_futex_key(fshared, &key2);
  1005. put_futex_key(fshared, &key1);
  1006. ret = get_user(curval2, uaddr2);
  1007. if (!ret)
  1008. goto retry;
  1009. goto out;
  1010. case -EAGAIN:
  1011. /* The owner was exiting, try again. */
  1012. double_unlock_hb(hb1, hb2);
  1013. put_futex_key(fshared, &key2);
  1014. put_futex_key(fshared, &key1);
  1015. cond_resched();
  1016. goto retry;
  1017. default:
  1018. goto out_unlock;
  1019. }
  1020. }
  1021. head1 = &hb1->chain;
  1022. plist_for_each_entry_safe(this, next, head1, list) {
  1023. if (task_count - nr_wake >= nr_requeue)
  1024. break;
  1025. if (!match_futex(&this->key, &key1))
  1026. continue;
  1027. WARN_ON(!requeue_pi && this->rt_waiter);
  1028. WARN_ON(requeue_pi && !this->rt_waiter);
  1029. /*
  1030. * Wake nr_wake waiters. For requeue_pi, if we acquired the
  1031. * lock, we already woke the top_waiter. If not, it will be
  1032. * woken by futex_unlock_pi().
  1033. */
  1034. if (++task_count <= nr_wake && !requeue_pi) {
  1035. wake_futex(this);
  1036. continue;
  1037. }
  1038. /*
  1039. * Requeue nr_requeue waiters and possibly one more in the case
  1040. * of requeue_pi if we couldn't acquire the lock atomically.
  1041. */
  1042. if (requeue_pi) {
  1043. /* Prepare the waiter to take the rt_mutex. */
  1044. atomic_inc(&pi_state->refcount);
  1045. this->pi_state = pi_state;
  1046. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  1047. this->rt_waiter,
  1048. this->task, 1);
  1049. if (ret == 1) {
  1050. /* We got the lock. */
  1051. requeue_pi_wake_futex(this, &key2);
  1052. continue;
  1053. } else if (ret) {
  1054. /* -EDEADLK */
  1055. this->pi_state = NULL;
  1056. free_pi_state(pi_state);
  1057. goto out_unlock;
  1058. }
  1059. }
  1060. requeue_futex(this, hb1, hb2, &key2);
  1061. drop_count++;
  1062. }
  1063. out_unlock:
  1064. double_unlock_hb(hb1, hb2);
  1065. /* drop_futex_key_refs() must be called outside the spinlocks. */
  1066. while (--drop_count >= 0)
  1067. drop_futex_key_refs(&key1);
  1068. out_put_keys:
  1069. put_futex_key(fshared, &key2);
  1070. out_put_key1:
  1071. put_futex_key(fshared, &key1);
  1072. out:
  1073. if (pi_state != NULL)
  1074. free_pi_state(pi_state);
  1075. return ret ? ret : task_count;
  1076. }
  1077. /* The key must be already stored in q->key. */
  1078. static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
  1079. {
  1080. struct futex_hash_bucket *hb;
  1081. init_waitqueue_head(&q->waiter);
  1082. get_futex_key_refs(&q->key);
  1083. hb = hash_futex(&q->key);
  1084. q->lock_ptr = &hb->lock;
  1085. spin_lock(&hb->lock);
  1086. return hb;
  1087. }
  1088. static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  1089. {
  1090. int prio;
  1091. /*
  1092. * The priority used to register this element is
  1093. * - either the real thread-priority for the real-time threads
  1094. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  1095. * - or MAX_RT_PRIO for non-RT threads.
  1096. * Thus, all RT-threads are woken first in priority order, and
  1097. * the others are woken last, in FIFO order.
  1098. */
  1099. prio = min(current->normal_prio, MAX_RT_PRIO);
  1100. plist_node_init(&q->list, prio);
  1101. #ifdef CONFIG_DEBUG_PI_LIST
  1102. q->list.plist.lock = &hb->lock;
  1103. #endif
  1104. plist_add(&q->list, &hb->chain);
  1105. q->task = current;
  1106. spin_unlock(&hb->lock);
  1107. }
  1108. static inline void
  1109. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  1110. {
  1111. spin_unlock(&hb->lock);
  1112. drop_futex_key_refs(&q->key);
  1113. }
  1114. /*
  1115. * queue_me and unqueue_me must be called as a pair, each
  1116. * exactly once. They are called with the hashed spinlock held.
  1117. */
  1118. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  1119. static int unqueue_me(struct futex_q *q)
  1120. {
  1121. spinlock_t *lock_ptr;
  1122. int ret = 0;
  1123. /* In the common case we don't take the spinlock, which is nice. */
  1124. retry:
  1125. lock_ptr = q->lock_ptr;
  1126. barrier();
  1127. if (lock_ptr != NULL) {
  1128. spin_lock(lock_ptr);
  1129. /*
  1130. * q->lock_ptr can change between reading it and
  1131. * spin_lock(), causing us to take the wrong lock. This
  1132. * corrects the race condition.
  1133. *
  1134. * Reasoning goes like this: if we have the wrong lock,
  1135. * q->lock_ptr must have changed (maybe several times)
  1136. * between reading it and the spin_lock(). It can
  1137. * change again after the spin_lock() but only if it was
  1138. * already changed before the spin_lock(). It cannot,
  1139. * however, change back to the original value. Therefore
  1140. * we can detect whether we acquired the correct lock.
  1141. */
  1142. if (unlikely(lock_ptr != q->lock_ptr)) {
  1143. spin_unlock(lock_ptr);
  1144. goto retry;
  1145. }
  1146. WARN_ON(plist_node_empty(&q->list));
  1147. plist_del(&q->list, &q->list.plist);
  1148. BUG_ON(q->pi_state);
  1149. spin_unlock(lock_ptr);
  1150. ret = 1;
  1151. }
  1152. drop_futex_key_refs(&q->key);
  1153. return ret;
  1154. }
  1155. /*
  1156. * PI futexes can not be requeued and must remove themself from the
  1157. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  1158. * and dropped here.
  1159. */
  1160. static void unqueue_me_pi(struct futex_q *q)
  1161. {
  1162. WARN_ON(plist_node_empty(&q->list));
  1163. plist_del(&q->list, &q->list.plist);
  1164. BUG_ON(!q->pi_state);
  1165. free_pi_state(q->pi_state);
  1166. q->pi_state = NULL;
  1167. spin_unlock(q->lock_ptr);
  1168. drop_futex_key_refs(&q->key);
  1169. }
  1170. /*
  1171. * Fixup the pi_state owner with the new owner.
  1172. *
  1173. * Must be called with hash bucket lock held and mm->sem held for non
  1174. * private futexes.
  1175. */
  1176. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  1177. struct task_struct *newowner, int fshared)
  1178. {
  1179. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  1180. struct futex_pi_state *pi_state = q->pi_state;
  1181. struct task_struct *oldowner = pi_state->owner;
  1182. u32 uval, curval, newval;
  1183. int ret;
  1184. /* Owner died? */
  1185. if (!pi_state->owner)
  1186. newtid |= FUTEX_OWNER_DIED;
  1187. /*
  1188. * We are here either because we stole the rtmutex from the
  1189. * pending owner or we are the pending owner which failed to
  1190. * get the rtmutex. We have to replace the pending owner TID
  1191. * in the user space variable. This must be atomic as we have
  1192. * to preserve the owner died bit here.
  1193. *
  1194. * Note: We write the user space value _before_ changing the pi_state
  1195. * because we can fault here. Imagine swapped out pages or a fork
  1196. * that marked all the anonymous memory readonly for cow.
  1197. *
  1198. * Modifying pi_state _before_ the user space value would
  1199. * leave the pi_state in an inconsistent state when we fault
  1200. * here, because we need to drop the hash bucket lock to
  1201. * handle the fault. This might be observed in the PID check
  1202. * in lookup_pi_state.
  1203. */
  1204. retry:
  1205. if (get_futex_value_locked(&uval, uaddr))
  1206. goto handle_fault;
  1207. while (1) {
  1208. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  1209. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  1210. if (curval == -EFAULT)
  1211. goto handle_fault;
  1212. if (curval == uval)
  1213. break;
  1214. uval = curval;
  1215. }
  1216. /*
  1217. * We fixed up user space. Now we need to fix the pi_state
  1218. * itself.
  1219. */
  1220. if (pi_state->owner != NULL) {
  1221. spin_lock_irq(&pi_state->owner->pi_lock);
  1222. WARN_ON(list_empty(&pi_state->list));
  1223. list_del_init(&pi_state->list);
  1224. spin_unlock_irq(&pi_state->owner->pi_lock);
  1225. }
  1226. pi_state->owner = newowner;
  1227. spin_lock_irq(&newowner->pi_lock);
  1228. WARN_ON(!list_empty(&pi_state->list));
  1229. list_add(&pi_state->list, &newowner->pi_state_list);
  1230. spin_unlock_irq(&newowner->pi_lock);
  1231. return 0;
  1232. /*
  1233. * To handle the page fault we need to drop the hash bucket
  1234. * lock here. That gives the other task (either the pending
  1235. * owner itself or the task which stole the rtmutex) the
  1236. * chance to try the fixup of the pi_state. So once we are
  1237. * back from handling the fault we need to check the pi_state
  1238. * after reacquiring the hash bucket lock and before trying to
  1239. * do another fixup. When the fixup has been done already we
  1240. * simply return.
  1241. */
  1242. handle_fault:
  1243. spin_unlock(q->lock_ptr);
  1244. ret = get_user(uval, uaddr);
  1245. spin_lock(q->lock_ptr);
  1246. /*
  1247. * Check if someone else fixed it for us:
  1248. */
  1249. if (pi_state->owner != oldowner)
  1250. return 0;
  1251. if (ret)
  1252. return ret;
  1253. goto retry;
  1254. }
  1255. /*
  1256. * In case we must use restart_block to restart a futex_wait,
  1257. * we encode in the 'flags' shared capability
  1258. */
  1259. #define FLAGS_SHARED 0x01
  1260. #define FLAGS_CLOCKRT 0x02
  1261. #define FLAGS_HAS_TIMEOUT 0x04
  1262. static long futex_wait_restart(struct restart_block *restart);
  1263. static long futex_lock_pi_restart(struct restart_block *restart);
  1264. /**
  1265. * fixup_owner() - Post lock pi_state and corner case management
  1266. * @uaddr: user address of the futex
  1267. * @fshared: whether the futex is shared (1) or not (0)
  1268. * @q: futex_q (contains pi_state and access to the rt_mutex)
  1269. * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
  1270. *
  1271. * After attempting to lock an rt_mutex, this function is called to cleanup
  1272. * the pi_state owner as well as handle race conditions that may allow us to
  1273. * acquire the lock. Must be called with the hb lock held.
  1274. *
  1275. * Returns:
  1276. * 1 - success, lock taken
  1277. * 0 - success, lock not taken
  1278. * <0 - on error (-EFAULT)
  1279. */
  1280. static int fixup_owner(u32 __user *uaddr, int fshared, struct futex_q *q,
  1281. int locked)
  1282. {
  1283. struct task_struct *owner;
  1284. int ret = 0;
  1285. if (locked) {
  1286. /*
  1287. * Got the lock. We might not be the anticipated owner if we
  1288. * did a lock-steal - fix up the PI-state in that case:
  1289. */
  1290. if (q->pi_state->owner != current)
  1291. ret = fixup_pi_state_owner(uaddr, q, current, fshared);
  1292. goto out;
  1293. }
  1294. /*
  1295. * Catch the rare case, where the lock was released when we were on the
  1296. * way back before we locked the hash bucket.
  1297. */
  1298. if (q->pi_state->owner == current) {
  1299. /*
  1300. * Try to get the rt_mutex now. This might fail as some other
  1301. * task acquired the rt_mutex after we removed ourself from the
  1302. * rt_mutex waiters list.
  1303. */
  1304. if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
  1305. locked = 1;
  1306. goto out;
  1307. }
  1308. /*
  1309. * pi_state is incorrect, some other task did a lock steal and
  1310. * we returned due to timeout or signal without taking the
  1311. * rt_mutex. Too late. We can access the rt_mutex_owner without
  1312. * locking, as the other task is now blocked on the hash bucket
  1313. * lock. Fix the state up.
  1314. */
  1315. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  1316. ret = fixup_pi_state_owner(uaddr, q, owner, fshared);
  1317. goto out;
  1318. }
  1319. /*
  1320. * Paranoia check. If we did not take the lock, then we should not be
  1321. * the owner, nor the pending owner, of the rt_mutex.
  1322. */
  1323. if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
  1324. printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
  1325. "pi-state %p\n", ret,
  1326. q->pi_state->pi_mutex.owner,
  1327. q->pi_state->owner);
  1328. out:
  1329. return ret ? ret : locked;
  1330. }
  1331. /**
  1332. * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
  1333. * @hb: the futex hash bucket, must be locked by the caller
  1334. * @q: the futex_q to queue up on
  1335. * @timeout: the prepared hrtimer_sleeper, or null for no timeout
  1336. * @wait: the wait_queue to add to the futex_q after queueing in the hb
  1337. */
  1338. static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
  1339. struct hrtimer_sleeper *timeout,
  1340. wait_queue_t *wait)
  1341. {
  1342. queue_me(q, hb);
  1343. /*
  1344. * There might have been scheduling since the queue_me(), as we
  1345. * cannot hold a spinlock across the get_user() in case it
  1346. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  1347. * queueing ourselves into the futex hash. This code thus has to
  1348. * rely on the futex_wake() code removing us from hash when it
  1349. * wakes us up.
  1350. */
  1351. /* add_wait_queue is the barrier after __set_current_state. */
  1352. __set_current_state(TASK_INTERRUPTIBLE);
  1353. /*
  1354. * Add current as the futex_q waiter. We don't remove ourselves from
  1355. * the wait_queue because we are the only user of it.
  1356. */
  1357. add_wait_queue(&q->waiter, wait);
  1358. /* Arm the timer */
  1359. if (timeout) {
  1360. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1361. if (!hrtimer_active(&timeout->timer))
  1362. timeout->task = NULL;
  1363. }
  1364. /*
  1365. * !plist_node_empty() is safe here without any lock.
  1366. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  1367. */
  1368. if (likely(!plist_node_empty(&q->list))) {
  1369. /*
  1370. * If the timer has already expired, current will already be
  1371. * flagged for rescheduling. Only call schedule if there
  1372. * is no timeout, or if it has yet to expire.
  1373. */
  1374. if (!timeout || timeout->task)
  1375. schedule();
  1376. }
  1377. __set_current_state(TASK_RUNNING);
  1378. }
  1379. /**
  1380. * futex_wait_setup() - Prepare to wait on a futex
  1381. * @uaddr: the futex userspace address
  1382. * @val: the expected value
  1383. * @fshared: whether the futex is shared (1) or not (0)
  1384. * @q: the associated futex_q
  1385. * @hb: storage for hash_bucket pointer to be returned to caller
  1386. *
  1387. * Setup the futex_q and locate the hash_bucket. Get the futex value and
  1388. * compare it with the expected value. Handle atomic faults internally.
  1389. * Return with the hb lock held and a q.key reference on success, and unlocked
  1390. * with no q.key reference on failure.
  1391. *
  1392. * Returns:
  1393. * 0 - uaddr contains val and hb has been locked
  1394. * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlcoked
  1395. */
  1396. static int futex_wait_setup(u32 __user *uaddr, u32 val, int fshared,
  1397. struct futex_q *q, struct futex_hash_bucket **hb)
  1398. {
  1399. u32 uval;
  1400. int ret;
  1401. /*
  1402. * Access the page AFTER the hash-bucket is locked.
  1403. * Order is important:
  1404. *
  1405. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  1406. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  1407. *
  1408. * The basic logical guarantee of a futex is that it blocks ONLY
  1409. * if cond(var) is known to be true at the time of blocking, for
  1410. * any cond. If we queued after testing *uaddr, that would open
  1411. * a race condition where we could block indefinitely with
  1412. * cond(var) false, which would violate the guarantee.
  1413. *
  1414. * A consequence is that futex_wait() can return zero and absorb
  1415. * a wakeup when *uaddr != val on entry to the syscall. This is
  1416. * rare, but normal.
  1417. */
  1418. retry:
  1419. q->key = FUTEX_KEY_INIT;
  1420. ret = get_futex_key(uaddr, fshared, &q->key);
  1421. if (unlikely(ret != 0))
  1422. goto out;
  1423. retry_private:
  1424. *hb = queue_lock(q);
  1425. ret = get_futex_value_locked(&uval, uaddr);
  1426. if (ret) {
  1427. queue_unlock(q, *hb);
  1428. ret = get_user(uval, uaddr);
  1429. if (ret)
  1430. goto out;
  1431. if (!fshared)
  1432. goto retry_private;
  1433. put_futex_key(fshared, &q->key);
  1434. goto retry;
  1435. }
  1436. if (uval != val) {
  1437. queue_unlock(q, *hb);
  1438. ret = -EWOULDBLOCK;
  1439. }
  1440. out:
  1441. if (ret)
  1442. put_futex_key(fshared, &q->key);
  1443. return ret;
  1444. }
  1445. static int futex_wait(u32 __user *uaddr, int fshared,
  1446. u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
  1447. {
  1448. struct hrtimer_sleeper timeout, *to = NULL;
  1449. DECLARE_WAITQUEUE(wait, current);
  1450. struct restart_block *restart;
  1451. struct futex_hash_bucket *hb;
  1452. struct futex_q q;
  1453. int ret;
  1454. if (!bitset)
  1455. return -EINVAL;
  1456. q.pi_state = NULL;
  1457. q.bitset = bitset;
  1458. q.rt_waiter = NULL;
  1459. if (abs_time) {
  1460. to = &timeout;
  1461. hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
  1462. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1463. hrtimer_init_sleeper(to, current);
  1464. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  1465. current->timer_slack_ns);
  1466. }
  1467. /* Prepare to wait on uaddr. */
  1468. ret = futex_wait_setup(uaddr, val, fshared, &q, &hb);
  1469. if (ret)
  1470. goto out;
  1471. /* queue_me and wait for wakeup, timeout, or a signal. */
  1472. futex_wait_queue_me(hb, &q, to, &wait);
  1473. /* If we were woken (and unqueued), we succeeded, whatever. */
  1474. ret = 0;
  1475. if (!unqueue_me(&q))
  1476. goto out_put_key;
  1477. ret = -ETIMEDOUT;
  1478. if (to && !to->task)
  1479. goto out_put_key;
  1480. /*
  1481. * We expect signal_pending(current), but another thread may
  1482. * have handled it for us already.
  1483. */
  1484. ret = -ERESTARTSYS;
  1485. if (!abs_time)
  1486. goto out_put_key;
  1487. restart = &current_thread_info()->restart_block;
  1488. restart->fn = futex_wait_restart;
  1489. restart->futex.uaddr = (u32 *)uaddr;
  1490. restart->futex.val = val;
  1491. restart->futex.time = abs_time->tv64;
  1492. restart->futex.bitset = bitset;
  1493. restart->futex.flags = FLAGS_HAS_TIMEOUT;
  1494. if (fshared)
  1495. restart->futex.flags |= FLAGS_SHARED;
  1496. if (clockrt)
  1497. restart->futex.flags |= FLAGS_CLOCKRT;
  1498. ret = -ERESTART_RESTARTBLOCK;
  1499. out_put_key:
  1500. put_futex_key(fshared, &q.key);
  1501. out:
  1502. if (to) {
  1503. hrtimer_cancel(&to->timer);
  1504. destroy_hrtimer_on_stack(&to->timer);
  1505. }
  1506. return ret;
  1507. }
  1508. static long futex_wait_restart(struct restart_block *restart)
  1509. {
  1510. u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
  1511. int fshared = 0;
  1512. ktime_t t, *tp = NULL;
  1513. if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
  1514. t.tv64 = restart->futex.time;
  1515. tp = &t;
  1516. }
  1517. restart->fn = do_no_restart_syscall;
  1518. if (restart->futex.flags & FLAGS_SHARED)
  1519. fshared = 1;
  1520. return (long)futex_wait(uaddr, fshared, restart->futex.val, tp,
  1521. restart->futex.bitset,
  1522. restart->futex.flags & FLAGS_CLOCKRT);
  1523. }
  1524. /*
  1525. * Userspace tried a 0 -> TID atomic transition of the futex value
  1526. * and failed. The kernel side here does the whole locking operation:
  1527. * if there are waiters then it will block, it does PI, etc. (Due to
  1528. * races the kernel might see a 0 value of the futex too.)
  1529. */
  1530. static int futex_lock_pi(u32 __user *uaddr, int fshared,
  1531. int detect, ktime_t *time, int trylock)
  1532. {
  1533. struct hrtimer_sleeper timeout, *to = NULL;
  1534. struct futex_hash_bucket *hb;
  1535. u32 uval;
  1536. struct futex_q q;
  1537. int res, ret;
  1538. if (refill_pi_state_cache())
  1539. return -ENOMEM;
  1540. if (time) {
  1541. to = &timeout;
  1542. hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
  1543. HRTIMER_MODE_ABS);
  1544. hrtimer_init_sleeper(to, current);
  1545. hrtimer_set_expires(&to->timer, *time);
  1546. }
  1547. q.pi_state = NULL;
  1548. q.rt_waiter = NULL;
  1549. retry:
  1550. q.key = FUTEX_KEY_INIT;
  1551. ret = get_futex_key(uaddr, fshared, &q.key);
  1552. if (unlikely(ret != 0))
  1553. goto out;
  1554. retry_private:
  1555. hb = queue_lock(&q);
  1556. ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current);
  1557. if (unlikely(ret)) {
  1558. switch (ret) {
  1559. case 1:
  1560. /* We got the lock. */
  1561. ret = 0;
  1562. goto out_unlock_put_key;
  1563. case -EFAULT:
  1564. goto uaddr_faulted;
  1565. case -EAGAIN:
  1566. /*
  1567. * Task is exiting and we just wait for the
  1568. * exit to complete.
  1569. */
  1570. queue_unlock(&q, hb);
  1571. put_futex_key(fshared, &q.key);
  1572. cond_resched();
  1573. goto retry;
  1574. default:
  1575. goto out_unlock_put_key;
  1576. }
  1577. }
  1578. /*
  1579. * Only actually queue now that the atomic ops are done:
  1580. */
  1581. queue_me(&q, hb);
  1582. WARN_ON(!q.pi_state);
  1583. /*
  1584. * Block on the PI mutex:
  1585. */
  1586. if (!trylock)
  1587. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1588. else {
  1589. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1590. /* Fixup the trylock return value: */
  1591. ret = ret ? 0 : -EWOULDBLOCK;
  1592. }
  1593. spin_lock(q.lock_ptr);
  1594. /*
  1595. * Fixup the pi_state owner and possibly acquire the lock if we
  1596. * haven't already.
  1597. */
  1598. res = fixup_owner(uaddr, fshared, &q, !ret);
  1599. /*
  1600. * If fixup_owner() returned an error, proprogate that. If it acquired
  1601. * the lock, clear our -ETIMEDOUT or -EINTR.
  1602. */
  1603. if (res)
  1604. ret = (res < 0) ? res : 0;
  1605. /*
  1606. * If fixup_owner() faulted and was unable to handle the fault, unlock
  1607. * it and return the fault to userspace.
  1608. */
  1609. if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
  1610. rt_mutex_unlock(&q.pi_state->pi_mutex);
  1611. /* Unqueue and drop the lock */
  1612. unqueue_me_pi(&q);
  1613. goto out;
  1614. out_unlock_put_key:
  1615. queue_unlock(&q, hb);
  1616. out_put_key:
  1617. put_futex_key(fshared, &q.key);
  1618. out:
  1619. if (to)
  1620. destroy_hrtimer_on_stack(&to->timer);
  1621. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  1622. uaddr_faulted:
  1623. /*
  1624. * We have to r/w *(int __user *)uaddr, and we have to modify it
  1625. * atomically. Therefore, if we continue to fault after get_user()
  1626. * below, we need to handle the fault ourselves, while still holding
  1627. * the mmap_sem. This can occur if the uaddr is under contention as
  1628. * we have to drop the mmap_sem in order to call get_user().
  1629. */
  1630. queue_unlock(&q, hb);
  1631. ret = get_user(uval, uaddr);
  1632. if (ret)
  1633. goto out_put_key;
  1634. if (!fshared)
  1635. goto retry_private;
  1636. put_futex_key(fshared, &q.key);
  1637. goto retry;
  1638. }
  1639. static long futex_lock_pi_restart(struct restart_block *restart)
  1640. {
  1641. u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
  1642. ktime_t t, *tp = NULL;
  1643. int fshared = restart->futex.flags & FLAGS_SHARED;
  1644. if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
  1645. t.tv64 = restart->futex.time;
  1646. tp = &t;
  1647. }
  1648. restart->fn = do_no_restart_syscall;
  1649. return (long)futex_lock_pi(uaddr, fshared, restart->futex.val, tp, 0);
  1650. }
  1651. /*
  1652. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1653. * This is the in-kernel slowpath: we look up the PI state (if any),
  1654. * and do the rt-mutex unlock.
  1655. */
  1656. static int futex_unlock_pi(u32 __user *uaddr, int fshared)
  1657. {
  1658. struct futex_hash_bucket *hb;
  1659. struct futex_q *this, *next;
  1660. u32 uval;
  1661. struct plist_head *head;
  1662. union futex_key key = FUTEX_KEY_INIT;
  1663. int ret;
  1664. retry:
  1665. if (get_user(uval, uaddr))
  1666. return -EFAULT;
  1667. /*
  1668. * We release only a lock we actually own:
  1669. */
  1670. if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
  1671. return -EPERM;
  1672. ret = get_futex_key(uaddr, fshared, &key);
  1673. if (unlikely(ret != 0))
  1674. goto out;
  1675. hb = hash_futex(&key);
  1676. spin_lock(&hb->lock);
  1677. /*
  1678. * To avoid races, try to do the TID -> 0 atomic transition
  1679. * again. If it succeeds then we can return without waking
  1680. * anyone else up:
  1681. */
  1682. if (!(uval & FUTEX_OWNER_DIED))
  1683. uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
  1684. if (unlikely(uval == -EFAULT))
  1685. goto pi_faulted;
  1686. /*
  1687. * Rare case: we managed to release the lock atomically,
  1688. * no need to wake anyone else up:
  1689. */
  1690. if (unlikely(uval == task_pid_vnr(current)))
  1691. goto out_unlock;
  1692. /*
  1693. * Ok, other tasks may need to be woken up - check waiters
  1694. * and do the wakeup if necessary:
  1695. */
  1696. head = &hb->chain;
  1697. plist_for_each_entry_safe(this, next, head, list) {
  1698. if (!match_futex (&this->key, &key))
  1699. continue;
  1700. ret = wake_futex_pi(uaddr, uval, this);
  1701. /*
  1702. * The atomic access to the futex value
  1703. * generated a pagefault, so retry the
  1704. * user-access and the wakeup:
  1705. */
  1706. if (ret == -EFAULT)
  1707. goto pi_faulted;
  1708. goto out_unlock;
  1709. }
  1710. /*
  1711. * No waiters - kernel unlocks the futex:
  1712. */
  1713. if (!(uval & FUTEX_OWNER_DIED)) {
  1714. ret = unlock_futex_pi(uaddr, uval);
  1715. if (ret == -EFAULT)
  1716. goto pi_faulted;
  1717. }
  1718. out_unlock:
  1719. spin_unlock(&hb->lock);
  1720. put_futex_key(fshared, &key);
  1721. out:
  1722. return ret;
  1723. pi_faulted:
  1724. /*
  1725. * We have to r/w *(int __user *)uaddr, and we have to modify it
  1726. * atomically. Therefore, if we continue to fault after get_user()
  1727. * below, we need to handle the fault ourselves, while still holding
  1728. * the mmap_sem. This can occur if the uaddr is under contention as
  1729. * we have to drop the mmap_sem in order to call get_user().
  1730. */
  1731. spin_unlock(&hb->lock);
  1732. put_futex_key(fshared, &key);
  1733. ret = get_user(uval, uaddr);
  1734. if (!ret)
  1735. goto retry;
  1736. return ret;
  1737. }
  1738. /**
  1739. * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
  1740. * @hb: the hash_bucket futex_q was original enqueued on
  1741. * @q: the futex_q woken while waiting to be requeued
  1742. * @key2: the futex_key of the requeue target futex
  1743. * @timeout: the timeout associated with the wait (NULL if none)
  1744. *
  1745. * Detect if the task was woken on the initial futex as opposed to the requeue
  1746. * target futex. If so, determine if it was a timeout or a signal that caused
  1747. * the wakeup and return the appropriate error code to the caller. Must be
  1748. * called with the hb lock held.
  1749. *
  1750. * Returns
  1751. * 0 - no early wakeup detected
  1752. * <0 - -ETIMEDOUT or -ERESTARTSYS (FIXME: or ERESTARTNOINTR?)
  1753. */
  1754. static inline
  1755. int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
  1756. struct futex_q *q, union futex_key *key2,
  1757. struct hrtimer_sleeper *timeout)
  1758. {
  1759. int ret = 0;
  1760. /*
  1761. * With the hb lock held, we avoid races while we process the wakeup.
  1762. * We only need to hold hb (and not hb2) to ensure atomicity as the
  1763. * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
  1764. * It can't be requeued from uaddr2 to something else since we don't
  1765. * support a PI aware source futex for requeue.
  1766. */
  1767. if (!match_futex(&q->key, key2)) {
  1768. WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
  1769. /*
  1770. * We were woken prior to requeue by a timeout or a signal.
  1771. * Unqueue the futex_q and determine which it was.
  1772. */
  1773. plist_del(&q->list, &q->list.plist);
  1774. drop_futex_key_refs(&q->key);
  1775. if (timeout && !timeout->task)
  1776. ret = -ETIMEDOUT;
  1777. else {
  1778. /*
  1779. * We expect signal_pending(current), but another
  1780. * thread may have handled it for us already.
  1781. */
  1782. /* FIXME: ERESTARTSYS or ERESTARTNOINTR? Do we care if
  1783. * the user specified SA_RESTART or not? */
  1784. ret = -ERESTARTSYS;
  1785. }
  1786. }
  1787. return ret;
  1788. }
  1789. /**
  1790. * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
  1791. * @uaddr: the futex we initialyl wait on (non-pi)
  1792. * @fshared: whether the futexes are shared (1) or not (0). They must be
  1793. * the same type, no requeueing from private to shared, etc.
  1794. * @val: the expected value of uaddr
  1795. * @abs_time: absolute timeout
  1796. * @bitset: 32 bit wakeup bitset set by userspace, defaults to all.
  1797. * @clockrt: whether to use CLOCK_REALTIME (1) or CLOCK_MONOTONIC (0)
  1798. * @uaddr2: the pi futex we will take prior to returning to user-space
  1799. *
  1800. * The caller will wait on uaddr and will be requeued by futex_requeue() to
  1801. * uaddr2 which must be PI aware. Normal wakeup will wake on uaddr2 and
  1802. * complete the acquisition of the rt_mutex prior to returning to userspace.
  1803. * This ensures the rt_mutex maintains an owner when it has waiters; without
  1804. * one, the pi logic wouldn't know which task to boost/deboost, if there was a
  1805. * need to.
  1806. *
  1807. * We call schedule in futex_wait_queue_me() when we enqueue and return there
  1808. * via the following:
  1809. * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
  1810. * 2) wakeup on uaddr2 after a requeue and subsequent unlock
  1811. * 3) signal (before or after requeue)
  1812. * 4) timeout (before or after requeue)
  1813. *
  1814. * If 3, we setup a restart_block with futex_wait_requeue_pi() as the function.
  1815. *
  1816. * If 2, we may then block on trying to take the rt_mutex and return via:
  1817. * 5) successful lock
  1818. * 6) signal
  1819. * 7) timeout
  1820. * 8) other lock acquisition failure
  1821. *
  1822. * If 6, we setup a restart_block with futex_lock_pi() as the function.
  1823. *
  1824. * If 4 or 7, we cleanup and return with -ETIMEDOUT.
  1825. *
  1826. * Returns:
  1827. * 0 - On success
  1828. * <0 - On error
  1829. */
  1830. static int futex_wait_requeue_pi(u32 __user *uaddr, int fshared,
  1831. u32 val, ktime_t *abs_time, u32 bitset,
  1832. int clockrt, u32 __user *uaddr2)
  1833. {
  1834. struct hrtimer_sleeper timeout, *to = NULL;
  1835. struct rt_mutex_waiter rt_waiter;
  1836. struct rt_mutex *pi_mutex = NULL;
  1837. DECLARE_WAITQUEUE(wait, current);
  1838. struct restart_block *restart;
  1839. struct futex_hash_bucket *hb;
  1840. union futex_key key2;
  1841. struct futex_q q;
  1842. int res, ret;
  1843. u32 uval;
  1844. if (!bitset)
  1845. return -EINVAL;
  1846. if (abs_time) {
  1847. to = &timeout;
  1848. hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
  1849. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1850. hrtimer_init_sleeper(to, current);
  1851. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  1852. current->timer_slack_ns);
  1853. }
  1854. /*
  1855. * The waiter is allocated on our stack, manipulated by the requeue
  1856. * code while we sleep on uaddr.
  1857. */
  1858. debug_rt_mutex_init_waiter(&rt_waiter);
  1859. rt_waiter.task = NULL;
  1860. q.pi_state = NULL;
  1861. q.bitset = bitset;
  1862. q.rt_waiter = &rt_waiter;
  1863. key2 = FUTEX_KEY_INIT;
  1864. ret = get_futex_key(uaddr2, fshared, &key2);
  1865. if (unlikely(ret != 0))
  1866. goto out;
  1867. /* Prepare to wait on uaddr. */
  1868. ret = futex_wait_setup(uaddr, val, fshared, &q, &hb);
  1869. if (ret) {
  1870. put_futex_key(fshared, &key2);
  1871. goto out;
  1872. }
  1873. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  1874. futex_wait_queue_me(hb, &q, to, &wait);
  1875. spin_lock(&hb->lock);
  1876. ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  1877. spin_unlock(&hb->lock);
  1878. if (ret)
  1879. goto out_put_keys;
  1880. /*
  1881. * In order for us to be here, we know our q.key == key2, and since
  1882. * we took the hb->lock above, we also know that futex_requeue() has
  1883. * completed and we no longer have to concern ourselves with a wakeup
  1884. * race with the atomic proxy lock acquition by the requeue code.
  1885. */
  1886. /* Check if the requeue code acquired the second futex for us. */
  1887. if (!q.rt_waiter) {
  1888. /*
  1889. * Got the lock. We might not be the anticipated owner if we
  1890. * did a lock-steal - fix up the PI-state in that case.
  1891. */
  1892. if (q.pi_state && (q.pi_state->owner != current)) {
  1893. spin_lock(q.lock_ptr);
  1894. ret = fixup_pi_state_owner(uaddr2, &q, current,
  1895. fshared);
  1896. spin_unlock(q.lock_ptr);
  1897. }
  1898. } else {
  1899. /*
  1900. * We have been woken up by futex_unlock_pi(), a timeout, or a
  1901. * signal. futex_unlock_pi() will not destroy the lock_ptr nor
  1902. * the pi_state.
  1903. */
  1904. WARN_ON(!&q.pi_state);
  1905. pi_mutex = &q.pi_state->pi_mutex;
  1906. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter, 1);
  1907. debug_rt_mutex_free_waiter(&rt_waiter);
  1908. spin_lock(q.lock_ptr);
  1909. /*
  1910. * Fixup the pi_state owner and possibly acquire the lock if we
  1911. * haven't already.
  1912. */
  1913. res = fixup_owner(uaddr2, fshared, &q, !ret);
  1914. /*
  1915. * If fixup_owner() returned an error, proprogate that. If it
  1916. * acquired the lock, clear our -ETIMEDOUT or -EINTR.
  1917. */
  1918. if (res)
  1919. ret = (res < 0) ? res : 0;
  1920. /* Unqueue and drop the lock. */
  1921. unqueue_me_pi(&q);
  1922. }
  1923. /*
  1924. * If fixup_pi_state_owner() faulted and was unable to handle the
  1925. * fault, unlock the rt_mutex and return the fault to userspace.
  1926. */
  1927. if (ret == -EFAULT) {
  1928. if (rt_mutex_owner(pi_mutex) == current)
  1929. rt_mutex_unlock(pi_mutex);
  1930. } else if (ret == -EINTR) {
  1931. ret = -EFAULT;
  1932. if (get_user(uval, uaddr2))
  1933. goto out_put_keys;
  1934. /*
  1935. * We've already been requeued, so restart by calling
  1936. * futex_lock_pi() directly, rather then returning to this
  1937. * function.
  1938. */
  1939. ret = -ERESTART_RESTARTBLOCK;
  1940. restart = &current_thread_info()->restart_block;
  1941. restart->fn = futex_lock_pi_restart;
  1942. restart->futex.uaddr = (u32 *)uaddr2;
  1943. restart->futex.val = uval;
  1944. restart->futex.flags = 0;
  1945. if (abs_time) {
  1946. restart->futex.flags |= FLAGS_HAS_TIMEOUT;
  1947. restart->futex.time = abs_time->tv64;
  1948. }
  1949. if (fshared)
  1950. restart->futex.flags |= FLAGS_SHARED;
  1951. if (clockrt)
  1952. restart->futex.flags |= FLAGS_CLOCKRT;
  1953. }
  1954. out_put_keys:
  1955. put_futex_key(fshared, &q.key);
  1956. put_futex_key(fshared, &key2);
  1957. out:
  1958. if (to) {
  1959. hrtimer_cancel(&to->timer);
  1960. destroy_hrtimer_on_stack(&to->timer);
  1961. }
  1962. return ret;
  1963. }
  1964. /*
  1965. * Support for robust futexes: the kernel cleans up held futexes at
  1966. * thread exit time.
  1967. *
  1968. * Implementation: user-space maintains a per-thread list of locks it
  1969. * is holding. Upon do_exit(), the kernel carefully walks this list,
  1970. * and marks all locks that are owned by this thread with the
  1971. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  1972. * always manipulated with the lock held, so the list is private and
  1973. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  1974. * field, to allow the kernel to clean up if the thread dies after
  1975. * acquiring the lock, but just before it could have added itself to
  1976. * the list. There can only be one such pending lock.
  1977. */
  1978. /**
  1979. * sys_set_robust_list - set the robust-futex list head of a task
  1980. * @head: pointer to the list-head
  1981. * @len: length of the list-head, as userspace expects
  1982. */
  1983. SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
  1984. size_t, len)
  1985. {
  1986. if (!futex_cmpxchg_enabled)
  1987. return -ENOSYS;
  1988. /*
  1989. * The kernel knows only one size for now:
  1990. */
  1991. if (unlikely(len != sizeof(*head)))
  1992. return -EINVAL;
  1993. current->robust_list = head;
  1994. return 0;
  1995. }
  1996. /**
  1997. * sys_get_robust_list - get the robust-futex list head of a task
  1998. * @pid: pid of the process [zero for current task]
  1999. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  2000. * @len_ptr: pointer to a length field, the kernel fills in the header size
  2001. */
  2002. SYSCALL_DEFINE3(get_robust_list, int, pid,
  2003. struct robust_list_head __user * __user *, head_ptr,
  2004. size_t __user *, len_ptr)
  2005. {
  2006. struct robust_list_head __user *head;
  2007. unsigned long ret;
  2008. const struct cred *cred = current_cred(), *pcred;
  2009. if (!futex_cmpxchg_enabled)
  2010. return -ENOSYS;
  2011. if (!pid)
  2012. head = current->robust_list;
  2013. else {
  2014. struct task_struct *p;
  2015. ret = -ESRCH;
  2016. rcu_read_lock();
  2017. p = find_task_by_vpid(pid);
  2018. if (!p)
  2019. goto err_unlock;
  2020. ret = -EPERM;
  2021. pcred = __task_cred(p);
  2022. if (cred->euid != pcred->euid &&
  2023. cred->euid != pcred->uid &&
  2024. !capable(CAP_SYS_PTRACE))
  2025. goto err_unlock;
  2026. head = p->robust_list;
  2027. rcu_read_unlock();
  2028. }
  2029. if (put_user(sizeof(*head), len_ptr))
  2030. return -EFAULT;
  2031. return put_user(head, head_ptr);
  2032. err_unlock:
  2033. rcu_read_unlock();
  2034. return ret;
  2035. }
  2036. /*
  2037. * Process a futex-list entry, check whether it's owned by the
  2038. * dying task, and do notification if so:
  2039. */
  2040. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
  2041. {
  2042. u32 uval, nval, mval;
  2043. retry:
  2044. if (get_user(uval, uaddr))
  2045. return -1;
  2046. if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
  2047. /*
  2048. * Ok, this dying thread is truly holding a futex
  2049. * of interest. Set the OWNER_DIED bit atomically
  2050. * via cmpxchg, and if the value had FUTEX_WAITERS
  2051. * set, wake up a waiter (if any). (We have to do a
  2052. * futex_wake() even if OWNER_DIED is already set -
  2053. * to handle the rare but possible case of recursive
  2054. * thread-death.) The rest of the cleanup is done in
  2055. * userspace.
  2056. */
  2057. mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  2058. nval = futex_atomic_cmpxchg_inatomic(uaddr, uval, mval);
  2059. if (nval == -EFAULT)
  2060. return -1;
  2061. if (nval != uval)
  2062. goto retry;
  2063. /*
  2064. * Wake robust non-PI futexes here. The wakeup of
  2065. * PI futexes happens in exit_pi_state():
  2066. */
  2067. if (!pi && (uval & FUTEX_WAITERS))
  2068. futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
  2069. }
  2070. return 0;
  2071. }
  2072. /*
  2073. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  2074. */
  2075. static inline int fetch_robust_entry(struct robust_list __user **entry,
  2076. struct robust_list __user * __user *head,
  2077. int *pi)
  2078. {
  2079. unsigned long uentry;
  2080. if (get_user(uentry, (unsigned long __user *)head))
  2081. return -EFAULT;
  2082. *entry = (void __user *)(uentry & ~1UL);
  2083. *pi = uentry & 1;
  2084. return 0;
  2085. }
  2086. /*
  2087. * Walk curr->robust_list (very carefully, it's a userspace list!)
  2088. * and mark any locks found there dead, and notify any waiters.
  2089. *
  2090. * We silently return on any sign of list-walking problem.
  2091. */
  2092. void exit_robust_list(struct task_struct *curr)
  2093. {
  2094. struct robust_list_head __user *head = curr->robust_list;
  2095. struct robust_list __user *entry, *next_entry, *pending;
  2096. unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
  2097. unsigned long futex_offset;
  2098. int rc;
  2099. if (!futex_cmpxchg_enabled)
  2100. return;
  2101. /*
  2102. * Fetch the list head (which was registered earlier, via
  2103. * sys_set_robust_list()):
  2104. */
  2105. if (fetch_robust_entry(&entry, &head->list.next, &pi))
  2106. return;
  2107. /*
  2108. * Fetch the relative futex offset:
  2109. */
  2110. if (get_user(futex_offset, &head->futex_offset))
  2111. return;
  2112. /*
  2113. * Fetch any possibly pending lock-add first, and handle it
  2114. * if it exists:
  2115. */
  2116. if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
  2117. return;
  2118. next_entry = NULL; /* avoid warning with gcc */
  2119. while (entry != &head->list) {
  2120. /*
  2121. * Fetch the next entry in the list before calling
  2122. * handle_futex_death:
  2123. */
  2124. rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
  2125. /*
  2126. * A pending lock might already be on the list, so
  2127. * don't process it twice:
  2128. */
  2129. if (entry != pending)
  2130. if (handle_futex_death((void __user *)entry + futex_offset,
  2131. curr, pi))
  2132. return;
  2133. if (rc)
  2134. return;
  2135. entry = next_entry;
  2136. pi = next_pi;
  2137. /*
  2138. * Avoid excessively long or circular lists:
  2139. */
  2140. if (!--limit)
  2141. break;
  2142. cond_resched();
  2143. }
  2144. if (pending)
  2145. handle_futex_death((void __user *)pending + futex_offset,
  2146. curr, pip);
  2147. }
  2148. long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
  2149. u32 __user *uaddr2, u32 val2, u32 val3)
  2150. {
  2151. int clockrt, ret = -ENOSYS;
  2152. int cmd = op & FUTEX_CMD_MASK;
  2153. int fshared = 0;
  2154. if (!(op & FUTEX_PRIVATE_FLAG))
  2155. fshared = 1;
  2156. clockrt = op & FUTEX_CLOCK_REALTIME;
  2157. if (clockrt && cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
  2158. return -ENOSYS;
  2159. switch (cmd) {
  2160. case FUTEX_WAIT:
  2161. val3 = FUTEX_BITSET_MATCH_ANY;
  2162. case FUTEX_WAIT_BITSET:
  2163. ret = futex_wait(uaddr, fshared, val, timeout, val3, clockrt);
  2164. break;
  2165. case FUTEX_WAKE:
  2166. val3 = FUTEX_BITSET_MATCH_ANY;
  2167. case FUTEX_WAKE_BITSET:
  2168. ret = futex_wake(uaddr, fshared, val, val3);
  2169. break;
  2170. case FUTEX_REQUEUE:
  2171. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL, 0);
  2172. break;
  2173. case FUTEX_CMP_REQUEUE:
  2174. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3,
  2175. 0);
  2176. break;
  2177. case FUTEX_WAKE_OP:
  2178. ret = futex_wake_op(uaddr, fshared, uaddr2, val, val2, val3);
  2179. break;
  2180. case FUTEX_LOCK_PI:
  2181. if (futex_cmpxchg_enabled)
  2182. ret = futex_lock_pi(uaddr, fshared, val, timeout, 0);
  2183. break;
  2184. case FUTEX_UNLOCK_PI:
  2185. if (futex_cmpxchg_enabled)
  2186. ret = futex_unlock_pi(uaddr, fshared);
  2187. break;
  2188. case FUTEX_TRYLOCK_PI:
  2189. if (futex_cmpxchg_enabled)
  2190. ret = futex_lock_pi(uaddr, fshared, 0, timeout, 1);
  2191. break;
  2192. case FUTEX_WAIT_REQUEUE_PI:
  2193. val3 = FUTEX_BITSET_MATCH_ANY;
  2194. ret = futex_wait_requeue_pi(uaddr, fshared, val, timeout, val3,
  2195. clockrt, uaddr2);
  2196. break;
  2197. case FUTEX_REQUEUE_PI:
  2198. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, NULL, 1);
  2199. break;
  2200. case FUTEX_CMP_REQUEUE_PI:
  2201. ret = futex_requeue(uaddr, fshared, uaddr2, val, val2, &val3,
  2202. 1);
  2203. break;
  2204. default:
  2205. ret = -ENOSYS;
  2206. }
  2207. return ret;
  2208. }
  2209. SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
  2210. struct timespec __user *, utime, u32 __user *, uaddr2,
  2211. u32, val3)
  2212. {
  2213. struct timespec ts;
  2214. ktime_t t, *tp = NULL;
  2215. u32 val2 = 0;
  2216. int cmd = op & FUTEX_CMD_MASK;
  2217. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  2218. cmd == FUTEX_WAIT_BITSET ||
  2219. cmd == FUTEX_WAIT_REQUEUE_PI)) {
  2220. if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
  2221. return -EFAULT;
  2222. if (!timespec_valid(&ts))
  2223. return -EINVAL;
  2224. t = timespec_to_ktime(ts);
  2225. if (cmd == FUTEX_WAIT)
  2226. t = ktime_add_safe(ktime_get(), t);
  2227. tp = &t;
  2228. }
  2229. /*
  2230. * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
  2231. * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
  2232. */
  2233. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  2234. cmd == FUTEX_REQUEUE_PI || cmd == FUTEX_CMP_REQUEUE_PI ||
  2235. cmd == FUTEX_WAKE_OP)
  2236. val2 = (u32) (unsigned long) utime;
  2237. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  2238. }
  2239. static int __init futex_init(void)
  2240. {
  2241. u32 curval;
  2242. int i;
  2243. /*
  2244. * This will fail and we want it. Some arch implementations do
  2245. * runtime detection of the futex_atomic_cmpxchg_inatomic()
  2246. * functionality. We want to know that before we call in any
  2247. * of the complex code paths. Also we want to prevent
  2248. * registration of robust lists in that case. NULL is
  2249. * guaranteed to fault and we get -EFAULT on functional
  2250. * implementation, the non functional ones will return
  2251. * -ENOSYS.
  2252. */
  2253. curval = cmpxchg_futex_value_locked(NULL, 0, 0);
  2254. if (curval == -EFAULT)
  2255. futex_cmpxchg_enabled = 1;
  2256. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  2257. plist_head_init(&futex_queues[i].chain, &futex_queues[i].lock);
  2258. spin_lock_init(&futex_queues[i].lock);
  2259. }
  2260. return 0;
  2261. }
  2262. __initcall(futex_init);