futex.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140
  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/export.h>
  59. #include <linux/magic.h>
  60. #include <linux/pid.h>
  61. #include <linux/nsproxy.h>
  62. #include <linux/ptrace.h>
  63. #include <linux/sched/rt.h>
  64. #include <linux/hugetlb.h>
  65. #include <linux/freezer.h>
  66. #include <linux/bootmem.h>
  67. #include <asm/futex.h>
  68. #include "locking/rtmutex_common.h"
  69. /*
  70. * READ this before attempting to hack on futexes!
  71. *
  72. * Basic futex operation and ordering guarantees
  73. * =============================================
  74. *
  75. * The waiter reads the futex value in user space and calls
  76. * futex_wait(). This function computes the hash bucket and acquires
  77. * the hash bucket lock. After that it reads the futex user space value
  78. * again and verifies that the data has not changed. If it has not changed
  79. * it enqueues itself into the hash bucket, releases the hash bucket lock
  80. * and schedules.
  81. *
  82. * The waker side modifies the user space value of the futex and calls
  83. * futex_wake(). This function computes the hash bucket and acquires the
  84. * hash bucket lock. Then it looks for waiters on that futex in the hash
  85. * bucket and wakes them.
  86. *
  87. * In futex wake up scenarios where no tasks are blocked on a futex, taking
  88. * the hb spinlock can be avoided and simply return. In order for this
  89. * optimization to work, ordering guarantees must exist so that the waiter
  90. * being added to the list is acknowledged when the list is concurrently being
  91. * checked by the waker, avoiding scenarios like the following:
  92. *
  93. * CPU 0 CPU 1
  94. * val = *futex;
  95. * sys_futex(WAIT, futex, val);
  96. * futex_wait(futex, val);
  97. * uval = *futex;
  98. * *futex = newval;
  99. * sys_futex(WAKE, futex);
  100. * futex_wake(futex);
  101. * if (queue_empty())
  102. * return;
  103. * if (uval == val)
  104. * lock(hash_bucket(futex));
  105. * queue();
  106. * unlock(hash_bucket(futex));
  107. * schedule();
  108. *
  109. * This would cause the waiter on CPU 0 to wait forever because it
  110. * missed the transition of the user space value from val to newval
  111. * and the waker did not find the waiter in the hash bucket queue.
  112. *
  113. * The correct serialization ensures that a waiter either observes
  114. * the changed user space value before blocking or is woken by a
  115. * concurrent waker:
  116. *
  117. * CPU 0 CPU 1
  118. * val = *futex;
  119. * sys_futex(WAIT, futex, val);
  120. * futex_wait(futex, val);
  121. *
  122. * waiters++; (a)
  123. * mb(); (A) <-- paired with -.
  124. * |
  125. * lock(hash_bucket(futex)); |
  126. * |
  127. * uval = *futex; |
  128. * | *futex = newval;
  129. * | sys_futex(WAKE, futex);
  130. * | futex_wake(futex);
  131. * |
  132. * `-------> mb(); (B)
  133. * if (uval == val)
  134. * queue();
  135. * unlock(hash_bucket(futex));
  136. * schedule(); if (waiters)
  137. * lock(hash_bucket(futex));
  138. * else wake_waiters(futex);
  139. * waiters--; (b) unlock(hash_bucket(futex));
  140. *
  141. * Where (A) orders the waiters increment and the futex value read through
  142. * atomic operations (see hb_waiters_inc) and where (B) orders the write
  143. * to futex and the waiters read -- this is done by the barriers for both
  144. * shared and private futexes in get_futex_key_refs().
  145. *
  146. * This yields the following case (where X:=waiters, Y:=futex):
  147. *
  148. * X = Y = 0
  149. *
  150. * w[X]=1 w[Y]=1
  151. * MB MB
  152. * r[Y]=y r[X]=x
  153. *
  154. * Which guarantees that x==0 && y==0 is impossible; which translates back into
  155. * the guarantee that we cannot both miss the futex variable change and the
  156. * enqueue.
  157. *
  158. * Note that a new waiter is accounted for in (a) even when it is possible that
  159. * the wait call can return error, in which case we backtrack from it in (b).
  160. * Refer to the comment in queue_lock().
  161. *
  162. * Similarly, in order to account for waiters being requeued on another
  163. * address we always increment the waiters for the destination bucket before
  164. * acquiring the lock. It then decrements them again after releasing it -
  165. * the code that actually moves the futex(es) between hash buckets (requeue_futex)
  166. * will do the additional required waiter count housekeeping. This is done for
  167. * double_lock_hb() and double_unlock_hb(), respectively.
  168. */
  169. #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
  170. int __read_mostly futex_cmpxchg_enabled;
  171. #endif
  172. /*
  173. * Futex flags used to encode options to functions and preserve them across
  174. * restarts.
  175. */
  176. #define FLAGS_SHARED 0x01
  177. #define FLAGS_CLOCKRT 0x02
  178. #define FLAGS_HAS_TIMEOUT 0x04
  179. /*
  180. * Priority Inheritance state:
  181. */
  182. struct futex_pi_state {
  183. /*
  184. * list of 'owned' pi_state instances - these have to be
  185. * cleaned up in do_exit() if the task exits prematurely:
  186. */
  187. struct list_head list;
  188. /*
  189. * The PI object:
  190. */
  191. struct rt_mutex pi_mutex;
  192. struct task_struct *owner;
  193. atomic_t refcount;
  194. union futex_key key;
  195. };
  196. /**
  197. * struct futex_q - The hashed futex queue entry, one per waiting task
  198. * @list: priority-sorted list of tasks waiting on this futex
  199. * @task: the task waiting on the futex
  200. * @lock_ptr: the hash bucket lock
  201. * @key: the key the futex is hashed on
  202. * @pi_state: optional priority inheritance state
  203. * @rt_waiter: rt_waiter storage for use with requeue_pi
  204. * @requeue_pi_key: the requeue_pi target futex key
  205. * @bitset: bitset for the optional bitmasked wakeup
  206. *
  207. * We use this hashed waitqueue, instead of a normal wait_queue_t, so
  208. * we can wake only the relevant ones (hashed queues may be shared).
  209. *
  210. * A futex_q has a woken state, just like tasks have TASK_RUNNING.
  211. * It is considered woken when plist_node_empty(&q->list) || q->lock_ptr == 0.
  212. * The order of wakeup is always to make the first condition true, then
  213. * the second.
  214. *
  215. * PI futexes are typically woken before they are removed from the hash list via
  216. * the rt_mutex code. See unqueue_me_pi().
  217. */
  218. struct futex_q {
  219. struct plist_node list;
  220. struct task_struct *task;
  221. spinlock_t *lock_ptr;
  222. union futex_key key;
  223. struct futex_pi_state *pi_state;
  224. struct rt_mutex_waiter *rt_waiter;
  225. union futex_key *requeue_pi_key;
  226. u32 bitset;
  227. };
  228. static const struct futex_q futex_q_init = {
  229. /* list gets initialized in queue_me()*/
  230. .key = FUTEX_KEY_INIT,
  231. .bitset = FUTEX_BITSET_MATCH_ANY
  232. };
  233. /*
  234. * Hash buckets are shared by all the futex_keys that hash to the same
  235. * location. Each key may have multiple futex_q structures, one for each task
  236. * waiting on a futex.
  237. */
  238. struct futex_hash_bucket {
  239. atomic_t waiters;
  240. spinlock_t lock;
  241. struct plist_head chain;
  242. } ____cacheline_aligned_in_smp;
  243. static unsigned long __read_mostly futex_hashsize;
  244. static struct futex_hash_bucket *futex_queues;
  245. static inline void futex_get_mm(union futex_key *key)
  246. {
  247. atomic_inc(&key->private.mm->mm_count);
  248. /*
  249. * Ensure futex_get_mm() implies a full barrier such that
  250. * get_futex_key() implies a full barrier. This is relied upon
  251. * as full barrier (B), see the ordering comment above.
  252. */
  253. smp_mb__after_atomic();
  254. }
  255. /*
  256. * Reflects a new waiter being added to the waitqueue.
  257. */
  258. static inline void hb_waiters_inc(struct futex_hash_bucket *hb)
  259. {
  260. #ifdef CONFIG_SMP
  261. atomic_inc(&hb->waiters);
  262. /*
  263. * Full barrier (A), see the ordering comment above.
  264. */
  265. smp_mb__after_atomic();
  266. #endif
  267. }
  268. /*
  269. * Reflects a waiter being removed from the waitqueue by wakeup
  270. * paths.
  271. */
  272. static inline void hb_waiters_dec(struct futex_hash_bucket *hb)
  273. {
  274. #ifdef CONFIG_SMP
  275. atomic_dec(&hb->waiters);
  276. #endif
  277. }
  278. static inline int hb_waiters_pending(struct futex_hash_bucket *hb)
  279. {
  280. #ifdef CONFIG_SMP
  281. return atomic_read(&hb->waiters);
  282. #else
  283. return 1;
  284. #endif
  285. }
  286. /*
  287. * We hash on the keys returned from get_futex_key (see below).
  288. */
  289. static struct futex_hash_bucket *hash_futex(union futex_key *key)
  290. {
  291. u32 hash = jhash2((u32*)&key->both.word,
  292. (sizeof(key->both.word)+sizeof(key->both.ptr))/4,
  293. key->both.offset);
  294. return &futex_queues[hash & (futex_hashsize - 1)];
  295. }
  296. /*
  297. * Return 1 if two futex_keys are equal, 0 otherwise.
  298. */
  299. static inline int match_futex(union futex_key *key1, union futex_key *key2)
  300. {
  301. return (key1 && key2
  302. && key1->both.word == key2->both.word
  303. && key1->both.ptr == key2->both.ptr
  304. && key1->both.offset == key2->both.offset);
  305. }
  306. /*
  307. * Take a reference to the resource addressed by a key.
  308. * Can be called while holding spinlocks.
  309. *
  310. */
  311. static void get_futex_key_refs(union futex_key *key)
  312. {
  313. if (!key->both.ptr)
  314. return;
  315. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  316. case FUT_OFF_INODE:
  317. ihold(key->shared.inode); /* implies MB (B) */
  318. break;
  319. case FUT_OFF_MMSHARED:
  320. futex_get_mm(key); /* implies MB (B) */
  321. break;
  322. default:
  323. /*
  324. * Private futexes do not hold reference on an inode or
  325. * mm, therefore the only purpose of calling get_futex_key_refs
  326. * is because we need the barrier for the lockless waiter check.
  327. */
  328. smp_mb(); /* explicit MB (B) */
  329. }
  330. }
  331. /*
  332. * Drop a reference to the resource addressed by a key.
  333. * The hash bucket spinlock must not be held. This is
  334. * a no-op for private futexes, see comment in the get
  335. * counterpart.
  336. */
  337. static void drop_futex_key_refs(union futex_key *key)
  338. {
  339. if (!key->both.ptr) {
  340. /* If we're here then we tried to put a key we failed to get */
  341. WARN_ON_ONCE(1);
  342. return;
  343. }
  344. switch (key->both.offset & (FUT_OFF_INODE|FUT_OFF_MMSHARED)) {
  345. case FUT_OFF_INODE:
  346. iput(key->shared.inode);
  347. break;
  348. case FUT_OFF_MMSHARED:
  349. mmdrop(key->private.mm);
  350. break;
  351. }
  352. }
  353. /**
  354. * get_futex_key() - Get parameters which are the keys for a futex
  355. * @uaddr: virtual address of the futex
  356. * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED
  357. * @key: address where result is stored.
  358. * @rw: mapping needs to be read/write (values: VERIFY_READ,
  359. * VERIFY_WRITE)
  360. *
  361. * Return: a negative error code or 0
  362. *
  363. * The key words are stored in *key on success.
  364. *
  365. * For shared mappings, it's (page->index, file_inode(vma->vm_file),
  366. * offset_within_page). For private mappings, it's (uaddr, current->mm).
  367. * We can usually work out the index without swapping in the page.
  368. *
  369. * lock_page() might sleep, the caller should not hold a spinlock.
  370. */
  371. static int
  372. get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
  373. {
  374. unsigned long address = (unsigned long)uaddr;
  375. struct mm_struct *mm = current->mm;
  376. struct page *page, *page_head;
  377. struct address_space *mapping;
  378. int err, ro = 0;
  379. /*
  380. * The futex address must be "naturally" aligned.
  381. */
  382. key->both.offset = address % PAGE_SIZE;
  383. if (unlikely((address % sizeof(u32)) != 0))
  384. return -EINVAL;
  385. address -= key->both.offset;
  386. if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
  387. return -EFAULT;
  388. /*
  389. * PROCESS_PRIVATE futexes are fast.
  390. * As the mm cannot disappear under us and the 'key' only needs
  391. * virtual address, we dont even have to find the underlying vma.
  392. * Note : We do have to check 'uaddr' is a valid user address,
  393. * but access_ok() should be faster than find_vma()
  394. */
  395. if (!fshared) {
  396. key->private.mm = mm;
  397. key->private.address = address;
  398. get_futex_key_refs(key); /* implies MB (B) */
  399. return 0;
  400. }
  401. again:
  402. err = get_user_pages_fast(address, 1, 1, &page);
  403. /*
  404. * If write access is not required (eg. FUTEX_WAIT), try
  405. * and get read-only access.
  406. */
  407. if (err == -EFAULT && rw == VERIFY_READ) {
  408. err = get_user_pages_fast(address, 1, 0, &page);
  409. ro = 1;
  410. }
  411. if (err < 0)
  412. return err;
  413. else
  414. err = 0;
  415. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  416. page_head = page;
  417. if (unlikely(PageTail(page))) {
  418. put_page(page);
  419. /* serialize against __split_huge_page_splitting() */
  420. local_irq_disable();
  421. if (likely(__get_user_pages_fast(address, 1, !ro, &page) == 1)) {
  422. page_head = compound_head(page);
  423. /*
  424. * page_head is valid pointer but we must pin
  425. * it before taking the PG_lock and/or
  426. * PG_compound_lock. The moment we re-enable
  427. * irqs __split_huge_page_splitting() can
  428. * return and the head page can be freed from
  429. * under us. We can't take the PG_lock and/or
  430. * PG_compound_lock on a page that could be
  431. * freed from under us.
  432. */
  433. if (page != page_head) {
  434. get_page(page_head);
  435. put_page(page);
  436. }
  437. local_irq_enable();
  438. } else {
  439. local_irq_enable();
  440. goto again;
  441. }
  442. }
  443. #else
  444. page_head = compound_head(page);
  445. if (page != page_head) {
  446. get_page(page_head);
  447. put_page(page);
  448. }
  449. #endif
  450. /*
  451. * The treatment of mapping from this point on is critical. The page
  452. * lock protects many things but in this context the page lock
  453. * stabilizes mapping, prevents inode freeing in the shared
  454. * file-backed region case and guards against movement to swap cache.
  455. *
  456. * Strictly speaking the page lock is not needed in all cases being
  457. * considered here and page lock forces unnecessarily serialization
  458. * From this point on, mapping will be re-verified if necessary and
  459. * page lock will be acquired only if it is unavoidable
  460. */
  461. mapping = READ_ONCE(page_head->mapping);
  462. /*
  463. * If page_head->mapping is NULL, then it cannot be a PageAnon
  464. * page; but it might be the ZERO_PAGE or in the gate area or
  465. * in a special mapping (all cases which we are happy to fail);
  466. * or it may have been a good file page when get_user_pages_fast
  467. * found it, but truncated or holepunched or subjected to
  468. * invalidate_complete_page2 before we got the page lock (also
  469. * cases which we are happy to fail). And we hold a reference,
  470. * so refcount care in invalidate_complete_page's remove_mapping
  471. * prevents drop_caches from setting mapping to NULL beneath us.
  472. *
  473. * The case we do have to guard against is when memory pressure made
  474. * shmem_writepage move it from filecache to swapcache beneath us:
  475. * an unlikely race, but we do need to retry for page_head->mapping.
  476. */
  477. if (unlikely(!mapping)) {
  478. int shmem_swizzled;
  479. /*
  480. * Page lock is required to identify which special case above
  481. * applies. If this is really a shmem page then the page lock
  482. * will prevent unexpected transitions.
  483. */
  484. lock_page(page);
  485. shmem_swizzled = PageSwapCache(page) || page->mapping;
  486. unlock_page(page_head);
  487. put_page(page_head);
  488. if (shmem_swizzled)
  489. goto again;
  490. return -EFAULT;
  491. }
  492. /*
  493. * Private mappings are handled in a simple way.
  494. *
  495. * If the futex key is stored on an anonymous page, then the associated
  496. * object is the mm which is implicitly pinned by the calling process.
  497. *
  498. * NOTE: When userspace waits on a MAP_SHARED mapping, even if
  499. * it's a read-only handle, it's expected that futexes attach to
  500. * the object not the particular process.
  501. */
  502. if (PageAnon(page_head)) {
  503. /*
  504. * A RO anonymous page will never change and thus doesn't make
  505. * sense for futex operations.
  506. */
  507. if (ro) {
  508. err = -EFAULT;
  509. goto out;
  510. }
  511. key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
  512. key->private.mm = mm;
  513. key->private.address = address;
  514. get_futex_key_refs(key); /* implies smp_mb(); (B) */
  515. } else {
  516. struct inode *inode;
  517. /*
  518. * The associated futex object in this case is the inode and
  519. * the page->mapping must be traversed. Ordinarily this should
  520. * be stabilised under page lock but it's not strictly
  521. * necessary in this case as we just want to pin the inode, not
  522. * update the radix tree or anything like that.
  523. *
  524. * The RCU read lock is taken as the inode is finally freed
  525. * under RCU. If the mapping still matches expectations then the
  526. * mapping->host can be safely accessed as being a valid inode.
  527. */
  528. rcu_read_lock();
  529. if (READ_ONCE(page_head->mapping) != mapping) {
  530. rcu_read_unlock();
  531. put_page(page_head);
  532. goto again;
  533. }
  534. inode = READ_ONCE(mapping->host);
  535. if (!inode) {
  536. rcu_read_unlock();
  537. put_page(page_head);
  538. goto again;
  539. }
  540. /*
  541. * Take a reference unless it is about to be freed. Previously
  542. * this reference was taken by ihold under the page lock
  543. * pinning the inode in place so i_lock was unnecessary. The
  544. * only way for this check to fail is if the inode was
  545. * truncated in parallel so warn for now if this happens.
  546. *
  547. * We are not calling into get_futex_key_refs() in file-backed
  548. * cases, therefore a successful atomic_inc return below will
  549. * guarantee that get_futex_key() will still imply smp_mb(); (B).
  550. */
  551. if (WARN_ON_ONCE(!atomic_inc_not_zero(&inode->i_count))) {
  552. rcu_read_unlock();
  553. put_page(page_head);
  554. goto again;
  555. }
  556. /* Should be impossible but lets be paranoid for now */
  557. if (WARN_ON_ONCE(inode->i_mapping != mapping)) {
  558. err = -EFAULT;
  559. rcu_read_unlock();
  560. iput(inode);
  561. goto out;
  562. }
  563. key->both.offset |= FUT_OFF_INODE; /* inode-based key */
  564. key->shared.inode = inode;
  565. key->shared.pgoff = basepage_index(page);
  566. rcu_read_unlock();
  567. }
  568. out:
  569. put_page(page_head);
  570. return err;
  571. }
  572. static inline void put_futex_key(union futex_key *key)
  573. {
  574. drop_futex_key_refs(key);
  575. }
  576. /**
  577. * fault_in_user_writeable() - Fault in user address and verify RW access
  578. * @uaddr: pointer to faulting user space address
  579. *
  580. * Slow path to fixup the fault we just took in the atomic write
  581. * access to @uaddr.
  582. *
  583. * We have no generic implementation of a non-destructive write to the
  584. * user address. We know that we faulted in the atomic pagefault
  585. * disabled section so we can as well avoid the #PF overhead by
  586. * calling get_user_pages() right away.
  587. */
  588. static int fault_in_user_writeable(u32 __user *uaddr)
  589. {
  590. struct mm_struct *mm = current->mm;
  591. int ret;
  592. down_read(&mm->mmap_sem);
  593. ret = fixup_user_fault(current, mm, (unsigned long)uaddr,
  594. FAULT_FLAG_WRITE);
  595. up_read(&mm->mmap_sem);
  596. return ret < 0 ? ret : 0;
  597. }
  598. /**
  599. * futex_top_waiter() - Return the highest priority waiter on a futex
  600. * @hb: the hash bucket the futex_q's reside in
  601. * @key: the futex key (to distinguish it from other futex futex_q's)
  602. *
  603. * Must be called with the hb lock held.
  604. */
  605. static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
  606. union futex_key *key)
  607. {
  608. struct futex_q *this;
  609. plist_for_each_entry(this, &hb->chain, list) {
  610. if (match_futex(&this->key, key))
  611. return this;
  612. }
  613. return NULL;
  614. }
  615. static int cmpxchg_futex_value_locked(u32 *curval, u32 __user *uaddr,
  616. u32 uval, u32 newval)
  617. {
  618. int ret;
  619. pagefault_disable();
  620. ret = futex_atomic_cmpxchg_inatomic(curval, uaddr, uval, newval);
  621. pagefault_enable();
  622. return ret;
  623. }
  624. static int get_futex_value_locked(u32 *dest, u32 __user *from)
  625. {
  626. int ret;
  627. pagefault_disable();
  628. ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
  629. pagefault_enable();
  630. return ret ? -EFAULT : 0;
  631. }
  632. /*
  633. * PI code:
  634. */
  635. static int refill_pi_state_cache(void)
  636. {
  637. struct futex_pi_state *pi_state;
  638. if (likely(current->pi_state_cache))
  639. return 0;
  640. pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
  641. if (!pi_state)
  642. return -ENOMEM;
  643. INIT_LIST_HEAD(&pi_state->list);
  644. /* pi_mutex gets initialized later */
  645. pi_state->owner = NULL;
  646. atomic_set(&pi_state->refcount, 1);
  647. pi_state->key = FUTEX_KEY_INIT;
  648. current->pi_state_cache = pi_state;
  649. return 0;
  650. }
  651. static struct futex_pi_state * alloc_pi_state(void)
  652. {
  653. struct futex_pi_state *pi_state = current->pi_state_cache;
  654. WARN_ON(!pi_state);
  655. current->pi_state_cache = NULL;
  656. return pi_state;
  657. }
  658. /*
  659. * Must be called with the hb lock held.
  660. */
  661. static void free_pi_state(struct futex_pi_state *pi_state)
  662. {
  663. if (!pi_state)
  664. return;
  665. if (!atomic_dec_and_test(&pi_state->refcount))
  666. return;
  667. /*
  668. * If pi_state->owner is NULL, the owner is most probably dying
  669. * and has cleaned up the pi_state already
  670. */
  671. if (pi_state->owner) {
  672. raw_spin_lock_irq(&pi_state->owner->pi_lock);
  673. list_del_init(&pi_state->list);
  674. raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  675. rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
  676. }
  677. if (current->pi_state_cache)
  678. kfree(pi_state);
  679. else {
  680. /*
  681. * pi_state->list is already empty.
  682. * clear pi_state->owner.
  683. * refcount is at 0 - put it back to 1.
  684. */
  685. pi_state->owner = NULL;
  686. atomic_set(&pi_state->refcount, 1);
  687. current->pi_state_cache = pi_state;
  688. }
  689. }
  690. /*
  691. * Look up the task based on what TID userspace gave us.
  692. * We dont trust it.
  693. */
  694. static struct task_struct * futex_find_get_task(pid_t pid)
  695. {
  696. struct task_struct *p;
  697. rcu_read_lock();
  698. p = find_task_by_vpid(pid);
  699. if (p)
  700. get_task_struct(p);
  701. rcu_read_unlock();
  702. return p;
  703. }
  704. /*
  705. * This task is holding PI mutexes at exit time => bad.
  706. * Kernel cleans up PI-state, but userspace is likely hosed.
  707. * (Robust-futex cleanup is separate and might save the day for userspace.)
  708. */
  709. void exit_pi_state_list(struct task_struct *curr)
  710. {
  711. struct list_head *next, *head = &curr->pi_state_list;
  712. struct futex_pi_state *pi_state;
  713. struct futex_hash_bucket *hb;
  714. union futex_key key = FUTEX_KEY_INIT;
  715. if (!futex_cmpxchg_enabled)
  716. return;
  717. /*
  718. * We are a ZOMBIE and nobody can enqueue itself on
  719. * pi_state_list anymore, but we have to be careful
  720. * versus waiters unqueueing themselves:
  721. */
  722. raw_spin_lock_irq(&curr->pi_lock);
  723. while (!list_empty(head)) {
  724. next = head->next;
  725. pi_state = list_entry(next, struct futex_pi_state, list);
  726. key = pi_state->key;
  727. hb = hash_futex(&key);
  728. raw_spin_unlock_irq(&curr->pi_lock);
  729. spin_lock(&hb->lock);
  730. raw_spin_lock_irq(&curr->pi_lock);
  731. /*
  732. * We dropped the pi-lock, so re-check whether this
  733. * task still owns the PI-state:
  734. */
  735. if (head->next != next) {
  736. spin_unlock(&hb->lock);
  737. continue;
  738. }
  739. WARN_ON(pi_state->owner != curr);
  740. WARN_ON(list_empty(&pi_state->list));
  741. list_del_init(&pi_state->list);
  742. pi_state->owner = NULL;
  743. raw_spin_unlock_irq(&curr->pi_lock);
  744. rt_mutex_unlock(&pi_state->pi_mutex);
  745. spin_unlock(&hb->lock);
  746. raw_spin_lock_irq(&curr->pi_lock);
  747. }
  748. raw_spin_unlock_irq(&curr->pi_lock);
  749. }
  750. /*
  751. * We need to check the following states:
  752. *
  753. * Waiter | pi_state | pi->owner | uTID | uODIED | ?
  754. *
  755. * [1] NULL | --- | --- | 0 | 0/1 | Valid
  756. * [2] NULL | --- | --- | >0 | 0/1 | Valid
  757. *
  758. * [3] Found | NULL | -- | Any | 0/1 | Invalid
  759. *
  760. * [4] Found | Found | NULL | 0 | 1 | Valid
  761. * [5] Found | Found | NULL | >0 | 1 | Invalid
  762. *
  763. * [6] Found | Found | task | 0 | 1 | Valid
  764. *
  765. * [7] Found | Found | NULL | Any | 0 | Invalid
  766. *
  767. * [8] Found | Found | task | ==taskTID | 0/1 | Valid
  768. * [9] Found | Found | task | 0 | 0 | Invalid
  769. * [10] Found | Found | task | !=taskTID | 0/1 | Invalid
  770. *
  771. * [1] Indicates that the kernel can acquire the futex atomically. We
  772. * came came here due to a stale FUTEX_WAITERS/FUTEX_OWNER_DIED bit.
  773. *
  774. * [2] Valid, if TID does not belong to a kernel thread. If no matching
  775. * thread is found then it indicates that the owner TID has died.
  776. *
  777. * [3] Invalid. The waiter is queued on a non PI futex
  778. *
  779. * [4] Valid state after exit_robust_list(), which sets the user space
  780. * value to FUTEX_WAITERS | FUTEX_OWNER_DIED.
  781. *
  782. * [5] The user space value got manipulated between exit_robust_list()
  783. * and exit_pi_state_list()
  784. *
  785. * [6] Valid state after exit_pi_state_list() which sets the new owner in
  786. * the pi_state but cannot access the user space value.
  787. *
  788. * [7] pi_state->owner can only be NULL when the OWNER_DIED bit is set.
  789. *
  790. * [8] Owner and user space value match
  791. *
  792. * [9] There is no transient state which sets the user space TID to 0
  793. * except exit_robust_list(), but this is indicated by the
  794. * FUTEX_OWNER_DIED bit. See [4]
  795. *
  796. * [10] There is no transient state which leaves owner and user space
  797. * TID out of sync.
  798. */
  799. /*
  800. * Validate that the existing waiter has a pi_state and sanity check
  801. * the pi_state against the user space value. If correct, attach to
  802. * it.
  803. */
  804. static int attach_to_pi_state(u32 uval, struct futex_pi_state *pi_state,
  805. struct futex_pi_state **ps)
  806. {
  807. pid_t pid = uval & FUTEX_TID_MASK;
  808. /*
  809. * Userspace might have messed up non-PI and PI futexes [3]
  810. */
  811. if (unlikely(!pi_state))
  812. return -EINVAL;
  813. WARN_ON(!atomic_read(&pi_state->refcount));
  814. /*
  815. * Handle the owner died case:
  816. */
  817. if (uval & FUTEX_OWNER_DIED) {
  818. /*
  819. * exit_pi_state_list sets owner to NULL and wakes the
  820. * topmost waiter. The task which acquires the
  821. * pi_state->rt_mutex will fixup owner.
  822. */
  823. if (!pi_state->owner) {
  824. /*
  825. * No pi state owner, but the user space TID
  826. * is not 0. Inconsistent state. [5]
  827. */
  828. if (pid)
  829. return -EINVAL;
  830. /*
  831. * Take a ref on the state and return success. [4]
  832. */
  833. goto out_state;
  834. }
  835. /*
  836. * If TID is 0, then either the dying owner has not
  837. * yet executed exit_pi_state_list() or some waiter
  838. * acquired the rtmutex in the pi state, but did not
  839. * yet fixup the TID in user space.
  840. *
  841. * Take a ref on the state and return success. [6]
  842. */
  843. if (!pid)
  844. goto out_state;
  845. } else {
  846. /*
  847. * If the owner died bit is not set, then the pi_state
  848. * must have an owner. [7]
  849. */
  850. if (!pi_state->owner)
  851. return -EINVAL;
  852. }
  853. /*
  854. * Bail out if user space manipulated the futex value. If pi
  855. * state exists then the owner TID must be the same as the
  856. * user space TID. [9/10]
  857. */
  858. if (pid != task_pid_vnr(pi_state->owner))
  859. return -EINVAL;
  860. out_state:
  861. atomic_inc(&pi_state->refcount);
  862. *ps = pi_state;
  863. return 0;
  864. }
  865. /*
  866. * Lookup the task for the TID provided from user space and attach to
  867. * it after doing proper sanity checks.
  868. */
  869. static int attach_to_pi_owner(u32 uval, union futex_key *key,
  870. struct futex_pi_state **ps)
  871. {
  872. pid_t pid = uval & FUTEX_TID_MASK;
  873. struct futex_pi_state *pi_state;
  874. struct task_struct *p;
  875. /*
  876. * We are the first waiter - try to look up the real owner and attach
  877. * the new pi_state to it, but bail out when TID = 0 [1]
  878. */
  879. if (!pid)
  880. return -ESRCH;
  881. p = futex_find_get_task(pid);
  882. if (!p)
  883. return -ESRCH;
  884. if (!p->mm) {
  885. put_task_struct(p);
  886. return -EPERM;
  887. }
  888. /*
  889. * We need to look at the task state flags to figure out,
  890. * whether the task is exiting. To protect against the do_exit
  891. * change of the task flags, we do this protected by
  892. * p->pi_lock:
  893. */
  894. raw_spin_lock_irq(&p->pi_lock);
  895. if (unlikely(p->flags & PF_EXITING)) {
  896. /*
  897. * The task is on the way out. When PF_EXITPIDONE is
  898. * set, we know that the task has finished the
  899. * cleanup:
  900. */
  901. int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
  902. raw_spin_unlock_irq(&p->pi_lock);
  903. put_task_struct(p);
  904. return ret;
  905. }
  906. /*
  907. * No existing pi state. First waiter. [2]
  908. */
  909. pi_state = alloc_pi_state();
  910. /*
  911. * Initialize the pi_mutex in locked state and make @p
  912. * the owner of it:
  913. */
  914. rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
  915. /* Store the key for possible exit cleanups: */
  916. pi_state->key = *key;
  917. WARN_ON(!list_empty(&pi_state->list));
  918. list_add(&pi_state->list, &p->pi_state_list);
  919. pi_state->owner = p;
  920. raw_spin_unlock_irq(&p->pi_lock);
  921. put_task_struct(p);
  922. *ps = pi_state;
  923. return 0;
  924. }
  925. static int lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
  926. union futex_key *key, struct futex_pi_state **ps)
  927. {
  928. struct futex_q *match = futex_top_waiter(hb, key);
  929. /*
  930. * If there is a waiter on that futex, validate it and
  931. * attach to the pi_state when the validation succeeds.
  932. */
  933. if (match)
  934. return attach_to_pi_state(uval, match->pi_state, ps);
  935. /*
  936. * We are the first waiter - try to look up the owner based on
  937. * @uval and attach to it.
  938. */
  939. return attach_to_pi_owner(uval, key, ps);
  940. }
  941. static int lock_pi_update_atomic(u32 __user *uaddr, u32 uval, u32 newval)
  942. {
  943. u32 uninitialized_var(curval);
  944. if (unlikely(cmpxchg_futex_value_locked(&curval, uaddr, uval, newval)))
  945. return -EFAULT;
  946. /*If user space value changed, let the caller retry */
  947. return curval != uval ? -EAGAIN : 0;
  948. }
  949. /**
  950. * futex_lock_pi_atomic() - Atomic work required to acquire a pi aware futex
  951. * @uaddr: the pi futex user address
  952. * @hb: the pi futex hash bucket
  953. * @key: the futex key associated with uaddr and hb
  954. * @ps: the pi_state pointer where we store the result of the
  955. * lookup
  956. * @task: the task to perform the atomic lock work for. This will
  957. * be "current" except in the case of requeue pi.
  958. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  959. *
  960. * Return:
  961. * 0 - ready to wait;
  962. * 1 - acquired the lock;
  963. * <0 - error
  964. *
  965. * The hb->lock and futex_key refs shall be held by the caller.
  966. */
  967. static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
  968. union futex_key *key,
  969. struct futex_pi_state **ps,
  970. struct task_struct *task, int set_waiters)
  971. {
  972. u32 uval, newval, vpid = task_pid_vnr(task);
  973. struct futex_q *match;
  974. int ret;
  975. /*
  976. * Read the user space value first so we can validate a few
  977. * things before proceeding further.
  978. */
  979. if (get_futex_value_locked(&uval, uaddr))
  980. return -EFAULT;
  981. /*
  982. * Detect deadlocks.
  983. */
  984. if ((unlikely((uval & FUTEX_TID_MASK) == vpid)))
  985. return -EDEADLK;
  986. /*
  987. * Lookup existing state first. If it exists, try to attach to
  988. * its pi_state.
  989. */
  990. match = futex_top_waiter(hb, key);
  991. if (match)
  992. return attach_to_pi_state(uval, match->pi_state, ps);
  993. /*
  994. * No waiter and user TID is 0. We are here because the
  995. * waiters or the owner died bit is set or called from
  996. * requeue_cmp_pi or for whatever reason something took the
  997. * syscall.
  998. */
  999. if (!(uval & FUTEX_TID_MASK)) {
  1000. /*
  1001. * We take over the futex. No other waiters and the user space
  1002. * TID is 0. We preserve the owner died bit.
  1003. */
  1004. newval = uval & FUTEX_OWNER_DIED;
  1005. newval |= vpid;
  1006. /* The futex requeue_pi code can enforce the waiters bit */
  1007. if (set_waiters)
  1008. newval |= FUTEX_WAITERS;
  1009. ret = lock_pi_update_atomic(uaddr, uval, newval);
  1010. /* If the take over worked, return 1 */
  1011. return ret < 0 ? ret : 1;
  1012. }
  1013. /*
  1014. * First waiter. Set the waiters bit before attaching ourself to
  1015. * the owner. If owner tries to unlock, it will be forced into
  1016. * the kernel and blocked on hb->lock.
  1017. */
  1018. newval = uval | FUTEX_WAITERS;
  1019. ret = lock_pi_update_atomic(uaddr, uval, newval);
  1020. if (ret)
  1021. return ret;
  1022. /*
  1023. * If the update of the user space value succeeded, we try to
  1024. * attach to the owner. If that fails, no harm done, we only
  1025. * set the FUTEX_WAITERS bit in the user space variable.
  1026. */
  1027. return attach_to_pi_owner(uval, key, ps);
  1028. }
  1029. /**
  1030. * __unqueue_futex() - Remove the futex_q from its futex_hash_bucket
  1031. * @q: The futex_q to unqueue
  1032. *
  1033. * The q->lock_ptr must not be NULL and must be held by the caller.
  1034. */
  1035. static void __unqueue_futex(struct futex_q *q)
  1036. {
  1037. struct futex_hash_bucket *hb;
  1038. if (WARN_ON_SMP(!q->lock_ptr || !spin_is_locked(q->lock_ptr))
  1039. || WARN_ON(plist_node_empty(&q->list)))
  1040. return;
  1041. hb = container_of(q->lock_ptr, struct futex_hash_bucket, lock);
  1042. plist_del(&q->list, &hb->chain);
  1043. hb_waiters_dec(hb);
  1044. }
  1045. /*
  1046. * The hash bucket lock must be held when this is called.
  1047. * Afterwards, the futex_q must not be accessed.
  1048. */
  1049. static void wake_futex(struct futex_q *q)
  1050. {
  1051. struct task_struct *p = q->task;
  1052. if (WARN(q->pi_state || q->rt_waiter, "refusing to wake PI futex\n"))
  1053. return;
  1054. /*
  1055. * We set q->lock_ptr = NULL _before_ we wake up the task. If
  1056. * a non-futex wake up happens on another CPU then the task
  1057. * might exit and p would dereference a non-existing task
  1058. * struct. Prevent this by holding a reference on p across the
  1059. * wake up.
  1060. */
  1061. get_task_struct(p);
  1062. __unqueue_futex(q);
  1063. /*
  1064. * The waiting task can free the futex_q as soon as
  1065. * q->lock_ptr = NULL is written, without taking any locks. A
  1066. * memory barrier is required here to prevent the following
  1067. * store to lock_ptr from getting ahead of the plist_del.
  1068. */
  1069. smp_wmb();
  1070. q->lock_ptr = NULL;
  1071. wake_up_state(p, TASK_NORMAL);
  1072. put_task_struct(p);
  1073. }
  1074. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  1075. {
  1076. struct task_struct *new_owner;
  1077. struct futex_pi_state *pi_state = this->pi_state;
  1078. u32 uninitialized_var(curval), newval;
  1079. int ret = 0;
  1080. if (!pi_state)
  1081. return -EINVAL;
  1082. /*
  1083. * If current does not own the pi_state then the futex is
  1084. * inconsistent and user space fiddled with the futex value.
  1085. */
  1086. if (pi_state->owner != current)
  1087. return -EINVAL;
  1088. raw_spin_lock(&pi_state->pi_mutex.wait_lock);
  1089. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  1090. /*
  1091. * It is possible that the next waiter (the one that brought
  1092. * this owner to the kernel) timed out and is no longer
  1093. * waiting on the lock.
  1094. */
  1095. if (!new_owner)
  1096. new_owner = this->task;
  1097. /*
  1098. * We pass it to the next owner. The WAITERS bit is always
  1099. * kept enabled while there is PI state around. We cleanup the
  1100. * owner died bit, because we are the owner.
  1101. */
  1102. newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
  1103. if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
  1104. ret = -EFAULT;
  1105. else if (curval != uval)
  1106. ret = -EINVAL;
  1107. if (ret) {
  1108. raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  1109. return ret;
  1110. }
  1111. raw_spin_lock_irq(&pi_state->owner->pi_lock);
  1112. WARN_ON(list_empty(&pi_state->list));
  1113. list_del_init(&pi_state->list);
  1114. raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  1115. raw_spin_lock_irq(&new_owner->pi_lock);
  1116. WARN_ON(!list_empty(&pi_state->list));
  1117. list_add(&pi_state->list, &new_owner->pi_state_list);
  1118. pi_state->owner = new_owner;
  1119. raw_spin_unlock_irq(&new_owner->pi_lock);
  1120. raw_spin_unlock(&pi_state->pi_mutex.wait_lock);
  1121. rt_mutex_unlock(&pi_state->pi_mutex);
  1122. return 0;
  1123. }
  1124. /*
  1125. * Express the locking dependencies for lockdep:
  1126. */
  1127. static inline void
  1128. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  1129. {
  1130. if (hb1 <= hb2) {
  1131. spin_lock(&hb1->lock);
  1132. if (hb1 < hb2)
  1133. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  1134. } else { /* hb1 > hb2 */
  1135. spin_lock(&hb2->lock);
  1136. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  1137. }
  1138. }
  1139. static inline void
  1140. double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  1141. {
  1142. spin_unlock(&hb1->lock);
  1143. if (hb1 != hb2)
  1144. spin_unlock(&hb2->lock);
  1145. }
  1146. /*
  1147. * Wake up waiters matching bitset queued on this futex (uaddr).
  1148. */
  1149. static int
  1150. futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset)
  1151. {
  1152. struct futex_hash_bucket *hb;
  1153. struct futex_q *this, *next;
  1154. union futex_key key = FUTEX_KEY_INIT;
  1155. int ret;
  1156. if (!bitset)
  1157. return -EINVAL;
  1158. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ);
  1159. if (unlikely(ret != 0))
  1160. goto out;
  1161. hb = hash_futex(&key);
  1162. /* Make sure we really have tasks to wakeup */
  1163. if (!hb_waiters_pending(hb))
  1164. goto out_put_key;
  1165. spin_lock(&hb->lock);
  1166. plist_for_each_entry_safe(this, next, &hb->chain, list) {
  1167. if (match_futex (&this->key, &key)) {
  1168. if (this->pi_state || this->rt_waiter) {
  1169. ret = -EINVAL;
  1170. break;
  1171. }
  1172. /* Check if one of the bits is set in both bitsets */
  1173. if (!(this->bitset & bitset))
  1174. continue;
  1175. wake_futex(this);
  1176. if (++ret >= nr_wake)
  1177. break;
  1178. }
  1179. }
  1180. spin_unlock(&hb->lock);
  1181. out_put_key:
  1182. put_futex_key(&key);
  1183. out:
  1184. return ret;
  1185. }
  1186. /*
  1187. * Wake up all waiters hashed on the physical page that is mapped
  1188. * to this virtual address:
  1189. */
  1190. static int
  1191. futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2,
  1192. int nr_wake, int nr_wake2, int op)
  1193. {
  1194. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  1195. struct futex_hash_bucket *hb1, *hb2;
  1196. struct futex_q *this, *next;
  1197. int ret, op_ret;
  1198. retry:
  1199. ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
  1200. if (unlikely(ret != 0))
  1201. goto out;
  1202. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  1203. if (unlikely(ret != 0))
  1204. goto out_put_key1;
  1205. hb1 = hash_futex(&key1);
  1206. hb2 = hash_futex(&key2);
  1207. retry_private:
  1208. double_lock_hb(hb1, hb2);
  1209. op_ret = futex_atomic_op_inuser(op, uaddr2);
  1210. if (unlikely(op_ret < 0)) {
  1211. double_unlock_hb(hb1, hb2);
  1212. #ifndef CONFIG_MMU
  1213. /*
  1214. * we don't get EFAULT from MMU faults if we don't have an MMU,
  1215. * but we might get them from range checking
  1216. */
  1217. ret = op_ret;
  1218. goto out_put_keys;
  1219. #endif
  1220. if (unlikely(op_ret != -EFAULT)) {
  1221. ret = op_ret;
  1222. goto out_put_keys;
  1223. }
  1224. ret = fault_in_user_writeable(uaddr2);
  1225. if (ret)
  1226. goto out_put_keys;
  1227. if (!(flags & FLAGS_SHARED))
  1228. goto retry_private;
  1229. put_futex_key(&key2);
  1230. put_futex_key(&key1);
  1231. goto retry;
  1232. }
  1233. plist_for_each_entry_safe(this, next, &hb1->chain, list) {
  1234. if (match_futex (&this->key, &key1)) {
  1235. if (this->pi_state || this->rt_waiter) {
  1236. ret = -EINVAL;
  1237. goto out_unlock;
  1238. }
  1239. wake_futex(this);
  1240. if (++ret >= nr_wake)
  1241. break;
  1242. }
  1243. }
  1244. if (op_ret > 0) {
  1245. op_ret = 0;
  1246. plist_for_each_entry_safe(this, next, &hb2->chain, list) {
  1247. if (match_futex (&this->key, &key2)) {
  1248. if (this->pi_state || this->rt_waiter) {
  1249. ret = -EINVAL;
  1250. goto out_unlock;
  1251. }
  1252. wake_futex(this);
  1253. if (++op_ret >= nr_wake2)
  1254. break;
  1255. }
  1256. }
  1257. ret += op_ret;
  1258. }
  1259. out_unlock:
  1260. double_unlock_hb(hb1, hb2);
  1261. out_put_keys:
  1262. put_futex_key(&key2);
  1263. out_put_key1:
  1264. put_futex_key(&key1);
  1265. out:
  1266. return ret;
  1267. }
  1268. /**
  1269. * requeue_futex() - Requeue a futex_q from one hb to another
  1270. * @q: the futex_q to requeue
  1271. * @hb1: the source hash_bucket
  1272. * @hb2: the target hash_bucket
  1273. * @key2: the new key for the requeued futex_q
  1274. */
  1275. static inline
  1276. void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
  1277. struct futex_hash_bucket *hb2, union futex_key *key2)
  1278. {
  1279. /*
  1280. * If key1 and key2 hash to the same bucket, no need to
  1281. * requeue.
  1282. */
  1283. if (likely(&hb1->chain != &hb2->chain)) {
  1284. plist_del(&q->list, &hb1->chain);
  1285. hb_waiters_dec(hb1);
  1286. plist_add(&q->list, &hb2->chain);
  1287. hb_waiters_inc(hb2);
  1288. q->lock_ptr = &hb2->lock;
  1289. }
  1290. get_futex_key_refs(key2);
  1291. q->key = *key2;
  1292. }
  1293. /**
  1294. * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
  1295. * @q: the futex_q
  1296. * @key: the key of the requeue target futex
  1297. * @hb: the hash_bucket of the requeue target futex
  1298. *
  1299. * During futex_requeue, with requeue_pi=1, it is possible to acquire the
  1300. * target futex if it is uncontended or via a lock steal. Set the futex_q key
  1301. * to the requeue target futex so the waiter can detect the wakeup on the right
  1302. * futex, but remove it from the hb and NULL the rt_waiter so it can detect
  1303. * atomic lock acquisition. Set the q->lock_ptr to the requeue target hb->lock
  1304. * to protect access to the pi_state to fixup the owner later. Must be called
  1305. * with both q->lock_ptr and hb->lock held.
  1306. */
  1307. static inline
  1308. void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key,
  1309. struct futex_hash_bucket *hb)
  1310. {
  1311. get_futex_key_refs(key);
  1312. q->key = *key;
  1313. __unqueue_futex(q);
  1314. WARN_ON(!q->rt_waiter);
  1315. q->rt_waiter = NULL;
  1316. q->lock_ptr = &hb->lock;
  1317. wake_up_state(q->task, TASK_NORMAL);
  1318. }
  1319. /**
  1320. * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
  1321. * @pifutex: the user address of the to futex
  1322. * @hb1: the from futex hash bucket, must be locked by the caller
  1323. * @hb2: the to futex hash bucket, must be locked by the caller
  1324. * @key1: the from futex key
  1325. * @key2: the to futex key
  1326. * @ps: address to store the pi_state pointer
  1327. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  1328. *
  1329. * Try and get the lock on behalf of the top waiter if we can do it atomically.
  1330. * Wake the top waiter if we succeed. If the caller specified set_waiters,
  1331. * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
  1332. * hb1 and hb2 must be held by the caller.
  1333. *
  1334. * Return:
  1335. * 0 - failed to acquire the lock atomically;
  1336. * >0 - acquired the lock, return value is vpid of the top_waiter
  1337. * <0 - error
  1338. */
  1339. static int futex_proxy_trylock_atomic(u32 __user *pifutex,
  1340. struct futex_hash_bucket *hb1,
  1341. struct futex_hash_bucket *hb2,
  1342. union futex_key *key1, union futex_key *key2,
  1343. struct futex_pi_state **ps, int set_waiters)
  1344. {
  1345. struct futex_q *top_waiter = NULL;
  1346. u32 curval;
  1347. int ret, vpid;
  1348. if (get_futex_value_locked(&curval, pifutex))
  1349. return -EFAULT;
  1350. /*
  1351. * Find the top_waiter and determine if there are additional waiters.
  1352. * If the caller intends to requeue more than 1 waiter to pifutex,
  1353. * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
  1354. * as we have means to handle the possible fault. If not, don't set
  1355. * the bit unecessarily as it will force the subsequent unlock to enter
  1356. * the kernel.
  1357. */
  1358. top_waiter = futex_top_waiter(hb1, key1);
  1359. /* There are no waiters, nothing for us to do. */
  1360. if (!top_waiter)
  1361. return 0;
  1362. /* Ensure we requeue to the expected futex. */
  1363. if (!match_futex(top_waiter->requeue_pi_key, key2))
  1364. return -EINVAL;
  1365. /*
  1366. * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
  1367. * the contended case or if set_waiters is 1. The pi_state is returned
  1368. * in ps in contended cases.
  1369. */
  1370. vpid = task_pid_vnr(top_waiter->task);
  1371. ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
  1372. set_waiters);
  1373. if (ret == 1) {
  1374. requeue_pi_wake_futex(top_waiter, key2, hb2);
  1375. return vpid;
  1376. }
  1377. return ret;
  1378. }
  1379. /**
  1380. * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
  1381. * @uaddr1: source futex user address
  1382. * @flags: futex flags (FLAGS_SHARED, etc.)
  1383. * @uaddr2: target futex user address
  1384. * @nr_wake: number of waiters to wake (must be 1 for requeue_pi)
  1385. * @nr_requeue: number of waiters to requeue (0-INT_MAX)
  1386. * @cmpval: @uaddr1 expected value (or %NULL)
  1387. * @requeue_pi: if we are attempting to requeue from a non-pi futex to a
  1388. * pi futex (pi to pi requeue is not supported)
  1389. *
  1390. * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
  1391. * uaddr2 atomically on behalf of the top waiter.
  1392. *
  1393. * Return:
  1394. * >=0 - on success, the number of tasks requeued or woken;
  1395. * <0 - on error
  1396. */
  1397. static int futex_requeue(u32 __user *uaddr1, unsigned int flags,
  1398. u32 __user *uaddr2, int nr_wake, int nr_requeue,
  1399. u32 *cmpval, int requeue_pi)
  1400. {
  1401. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  1402. int drop_count = 0, task_count = 0, ret;
  1403. struct futex_pi_state *pi_state = NULL;
  1404. struct futex_hash_bucket *hb1, *hb2;
  1405. struct futex_q *this, *next;
  1406. if (nr_wake < 0 || nr_requeue < 0)
  1407. return -EINVAL;
  1408. if (requeue_pi) {
  1409. /*
  1410. * Requeue PI only works on two distinct uaddrs. This
  1411. * check is only valid for private futexes. See below.
  1412. */
  1413. if (uaddr1 == uaddr2)
  1414. return -EINVAL;
  1415. /*
  1416. * requeue_pi requires a pi_state, try to allocate it now
  1417. * without any locks in case it fails.
  1418. */
  1419. if (refill_pi_state_cache())
  1420. return -ENOMEM;
  1421. /*
  1422. * requeue_pi must wake as many tasks as it can, up to nr_wake
  1423. * + nr_requeue, since it acquires the rt_mutex prior to
  1424. * returning to userspace, so as to not leave the rt_mutex with
  1425. * waiters and no owner. However, second and third wake-ups
  1426. * cannot be predicted as they involve race conditions with the
  1427. * first wake and a fault while looking up the pi_state. Both
  1428. * pthread_cond_signal() and pthread_cond_broadcast() should
  1429. * use nr_wake=1.
  1430. */
  1431. if (nr_wake != 1)
  1432. return -EINVAL;
  1433. }
  1434. retry:
  1435. ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ);
  1436. if (unlikely(ret != 0))
  1437. goto out;
  1438. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2,
  1439. requeue_pi ? VERIFY_WRITE : VERIFY_READ);
  1440. if (unlikely(ret != 0))
  1441. goto out_put_key1;
  1442. /*
  1443. * The check above which compares uaddrs is not sufficient for
  1444. * shared futexes. We need to compare the keys:
  1445. */
  1446. if (requeue_pi && match_futex(&key1, &key2)) {
  1447. ret = -EINVAL;
  1448. goto out_put_keys;
  1449. }
  1450. hb1 = hash_futex(&key1);
  1451. hb2 = hash_futex(&key2);
  1452. retry_private:
  1453. hb_waiters_inc(hb2);
  1454. double_lock_hb(hb1, hb2);
  1455. if (likely(cmpval != NULL)) {
  1456. u32 curval;
  1457. ret = get_futex_value_locked(&curval, uaddr1);
  1458. if (unlikely(ret)) {
  1459. double_unlock_hb(hb1, hb2);
  1460. hb_waiters_dec(hb2);
  1461. ret = get_user(curval, uaddr1);
  1462. if (ret)
  1463. goto out_put_keys;
  1464. if (!(flags & FLAGS_SHARED))
  1465. goto retry_private;
  1466. put_futex_key(&key2);
  1467. put_futex_key(&key1);
  1468. goto retry;
  1469. }
  1470. if (curval != *cmpval) {
  1471. ret = -EAGAIN;
  1472. goto out_unlock;
  1473. }
  1474. }
  1475. if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
  1476. /*
  1477. * Attempt to acquire uaddr2 and wake the top waiter. If we
  1478. * intend to requeue waiters, force setting the FUTEX_WAITERS
  1479. * bit. We force this here where we are able to easily handle
  1480. * faults rather in the requeue loop below.
  1481. */
  1482. ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
  1483. &key2, &pi_state, nr_requeue);
  1484. /*
  1485. * At this point the top_waiter has either taken uaddr2 or is
  1486. * waiting on it. If the former, then the pi_state will not
  1487. * exist yet, look it up one more time to ensure we have a
  1488. * reference to it. If the lock was taken, ret contains the
  1489. * vpid of the top waiter task.
  1490. */
  1491. if (ret > 0) {
  1492. WARN_ON(pi_state);
  1493. drop_count++;
  1494. task_count++;
  1495. /*
  1496. * If we acquired the lock, then the user
  1497. * space value of uaddr2 should be vpid. It
  1498. * cannot be changed by the top waiter as it
  1499. * is blocked on hb2 lock if it tries to do
  1500. * so. If something fiddled with it behind our
  1501. * back the pi state lookup might unearth
  1502. * it. So we rather use the known value than
  1503. * rereading and handing potential crap to
  1504. * lookup_pi_state.
  1505. */
  1506. ret = lookup_pi_state(ret, hb2, &key2, &pi_state);
  1507. }
  1508. switch (ret) {
  1509. case 0:
  1510. break;
  1511. case -EFAULT:
  1512. free_pi_state(pi_state);
  1513. pi_state = NULL;
  1514. double_unlock_hb(hb1, hb2);
  1515. hb_waiters_dec(hb2);
  1516. put_futex_key(&key2);
  1517. put_futex_key(&key1);
  1518. ret = fault_in_user_writeable(uaddr2);
  1519. if (!ret)
  1520. goto retry;
  1521. goto out;
  1522. case -EAGAIN:
  1523. /*
  1524. * Two reasons for this:
  1525. * - Owner is exiting and we just wait for the
  1526. * exit to complete.
  1527. * - The user space value changed.
  1528. */
  1529. free_pi_state(pi_state);
  1530. pi_state = NULL;
  1531. double_unlock_hb(hb1, hb2);
  1532. hb_waiters_dec(hb2);
  1533. put_futex_key(&key2);
  1534. put_futex_key(&key1);
  1535. cond_resched();
  1536. goto retry;
  1537. default:
  1538. goto out_unlock;
  1539. }
  1540. }
  1541. plist_for_each_entry_safe(this, next, &hb1->chain, list) {
  1542. if (task_count - nr_wake >= nr_requeue)
  1543. break;
  1544. if (!match_futex(&this->key, &key1))
  1545. continue;
  1546. /*
  1547. * FUTEX_WAIT_REQEUE_PI and FUTEX_CMP_REQUEUE_PI should always
  1548. * be paired with each other and no other futex ops.
  1549. *
  1550. * We should never be requeueing a futex_q with a pi_state,
  1551. * which is awaiting a futex_unlock_pi().
  1552. */
  1553. if ((requeue_pi && !this->rt_waiter) ||
  1554. (!requeue_pi && this->rt_waiter) ||
  1555. this->pi_state) {
  1556. ret = -EINVAL;
  1557. break;
  1558. }
  1559. /*
  1560. * Wake nr_wake waiters. For requeue_pi, if we acquired the
  1561. * lock, we already woke the top_waiter. If not, it will be
  1562. * woken by futex_unlock_pi().
  1563. */
  1564. if (++task_count <= nr_wake && !requeue_pi) {
  1565. wake_futex(this);
  1566. continue;
  1567. }
  1568. /* Ensure we requeue to the expected futex for requeue_pi. */
  1569. if (requeue_pi && !match_futex(this->requeue_pi_key, &key2)) {
  1570. ret = -EINVAL;
  1571. break;
  1572. }
  1573. /*
  1574. * Requeue nr_requeue waiters and possibly one more in the case
  1575. * of requeue_pi if we couldn't acquire the lock atomically.
  1576. */
  1577. if (requeue_pi) {
  1578. /* Prepare the waiter to take the rt_mutex. */
  1579. atomic_inc(&pi_state->refcount);
  1580. this->pi_state = pi_state;
  1581. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  1582. this->rt_waiter,
  1583. this->task);
  1584. if (ret == 1) {
  1585. /* We got the lock. */
  1586. requeue_pi_wake_futex(this, &key2, hb2);
  1587. drop_count++;
  1588. continue;
  1589. } else if (ret) {
  1590. /* -EDEADLK */
  1591. this->pi_state = NULL;
  1592. free_pi_state(pi_state);
  1593. goto out_unlock;
  1594. }
  1595. }
  1596. requeue_futex(this, hb1, hb2, &key2);
  1597. drop_count++;
  1598. }
  1599. out_unlock:
  1600. free_pi_state(pi_state);
  1601. double_unlock_hb(hb1, hb2);
  1602. hb_waiters_dec(hb2);
  1603. /*
  1604. * drop_futex_key_refs() must be called outside the spinlocks. During
  1605. * the requeue we moved futex_q's from the hash bucket at key1 to the
  1606. * one at key2 and updated their key pointer. We no longer need to
  1607. * hold the references to key1.
  1608. */
  1609. while (--drop_count >= 0)
  1610. drop_futex_key_refs(&key1);
  1611. out_put_keys:
  1612. put_futex_key(&key2);
  1613. out_put_key1:
  1614. put_futex_key(&key1);
  1615. out:
  1616. return ret ? ret : task_count;
  1617. }
  1618. /* The key must be already stored in q->key. */
  1619. static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
  1620. __acquires(&hb->lock)
  1621. {
  1622. struct futex_hash_bucket *hb;
  1623. hb = hash_futex(&q->key);
  1624. /*
  1625. * Increment the counter before taking the lock so that
  1626. * a potential waker won't miss a to-be-slept task that is
  1627. * waiting for the spinlock. This is safe as all queue_lock()
  1628. * users end up calling queue_me(). Similarly, for housekeeping,
  1629. * decrement the counter at queue_unlock() when some error has
  1630. * occurred and we don't end up adding the task to the list.
  1631. */
  1632. hb_waiters_inc(hb);
  1633. q->lock_ptr = &hb->lock;
  1634. spin_lock(&hb->lock); /* implies MB (A) */
  1635. return hb;
  1636. }
  1637. static inline void
  1638. queue_unlock(struct futex_hash_bucket *hb)
  1639. __releases(&hb->lock)
  1640. {
  1641. spin_unlock(&hb->lock);
  1642. hb_waiters_dec(hb);
  1643. }
  1644. /**
  1645. * queue_me() - Enqueue the futex_q on the futex_hash_bucket
  1646. * @q: The futex_q to enqueue
  1647. * @hb: The destination hash bucket
  1648. *
  1649. * The hb->lock must be held by the caller, and is released here. A call to
  1650. * queue_me() is typically paired with exactly one call to unqueue_me(). The
  1651. * exceptions involve the PI related operations, which may use unqueue_me_pi()
  1652. * or nothing if the unqueue is done as part of the wake process and the unqueue
  1653. * state is implicit in the state of woken task (see futex_wait_requeue_pi() for
  1654. * an example).
  1655. */
  1656. static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  1657. __releases(&hb->lock)
  1658. {
  1659. int prio;
  1660. /*
  1661. * The priority used to register this element is
  1662. * - either the real thread-priority for the real-time threads
  1663. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  1664. * - or MAX_RT_PRIO for non-RT threads.
  1665. * Thus, all RT-threads are woken first in priority order, and
  1666. * the others are woken last, in FIFO order.
  1667. */
  1668. prio = min(current->normal_prio, MAX_RT_PRIO);
  1669. plist_node_init(&q->list, prio);
  1670. plist_add(&q->list, &hb->chain);
  1671. q->task = current;
  1672. spin_unlock(&hb->lock);
  1673. }
  1674. /**
  1675. * unqueue_me() - Remove the futex_q from its futex_hash_bucket
  1676. * @q: The futex_q to unqueue
  1677. *
  1678. * The q->lock_ptr must not be held by the caller. A call to unqueue_me() must
  1679. * be paired with exactly one earlier call to queue_me().
  1680. *
  1681. * Return:
  1682. * 1 - if the futex_q was still queued (and we removed unqueued it);
  1683. * 0 - if the futex_q was already removed by the waking thread
  1684. */
  1685. static int unqueue_me(struct futex_q *q)
  1686. {
  1687. spinlock_t *lock_ptr;
  1688. int ret = 0;
  1689. /* In the common case we don't take the spinlock, which is nice. */
  1690. retry:
  1691. lock_ptr = q->lock_ptr;
  1692. barrier();
  1693. if (lock_ptr != NULL) {
  1694. spin_lock(lock_ptr);
  1695. /*
  1696. * q->lock_ptr can change between reading it and
  1697. * spin_lock(), causing us to take the wrong lock. This
  1698. * corrects the race condition.
  1699. *
  1700. * Reasoning goes like this: if we have the wrong lock,
  1701. * q->lock_ptr must have changed (maybe several times)
  1702. * between reading it and the spin_lock(). It can
  1703. * change again after the spin_lock() but only if it was
  1704. * already changed before the spin_lock(). It cannot,
  1705. * however, change back to the original value. Therefore
  1706. * we can detect whether we acquired the correct lock.
  1707. */
  1708. if (unlikely(lock_ptr != q->lock_ptr)) {
  1709. spin_unlock(lock_ptr);
  1710. goto retry;
  1711. }
  1712. __unqueue_futex(q);
  1713. BUG_ON(q->pi_state);
  1714. spin_unlock(lock_ptr);
  1715. ret = 1;
  1716. }
  1717. drop_futex_key_refs(&q->key);
  1718. return ret;
  1719. }
  1720. /*
  1721. * PI futexes can not be requeued and must remove themself from the
  1722. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  1723. * and dropped here.
  1724. */
  1725. static void unqueue_me_pi(struct futex_q *q)
  1726. __releases(q->lock_ptr)
  1727. {
  1728. __unqueue_futex(q);
  1729. BUG_ON(!q->pi_state);
  1730. free_pi_state(q->pi_state);
  1731. q->pi_state = NULL;
  1732. spin_unlock(q->lock_ptr);
  1733. }
  1734. /*
  1735. * Fixup the pi_state owner with the new owner.
  1736. *
  1737. * Must be called with hash bucket lock held and mm->sem held for non
  1738. * private futexes.
  1739. */
  1740. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  1741. struct task_struct *newowner)
  1742. {
  1743. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  1744. struct futex_pi_state *pi_state = q->pi_state;
  1745. struct task_struct *oldowner = pi_state->owner;
  1746. u32 uval, uninitialized_var(curval), newval;
  1747. int ret;
  1748. /* Owner died? */
  1749. if (!pi_state->owner)
  1750. newtid |= FUTEX_OWNER_DIED;
  1751. /*
  1752. * We are here either because we stole the rtmutex from the
  1753. * previous highest priority waiter or we are the highest priority
  1754. * waiter but failed to get the rtmutex the first time.
  1755. * We have to replace the newowner TID in the user space variable.
  1756. * This must be atomic as we have to preserve the owner died bit here.
  1757. *
  1758. * Note: We write the user space value _before_ changing the pi_state
  1759. * because we can fault here. Imagine swapped out pages or a fork
  1760. * that marked all the anonymous memory readonly for cow.
  1761. *
  1762. * Modifying pi_state _before_ the user space value would
  1763. * leave the pi_state in an inconsistent state when we fault
  1764. * here, because we need to drop the hash bucket lock to
  1765. * handle the fault. This might be observed in the PID check
  1766. * in lookup_pi_state.
  1767. */
  1768. retry:
  1769. if (get_futex_value_locked(&uval, uaddr))
  1770. goto handle_fault;
  1771. while (1) {
  1772. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  1773. if (cmpxchg_futex_value_locked(&curval, uaddr, uval, newval))
  1774. goto handle_fault;
  1775. if (curval == uval)
  1776. break;
  1777. uval = curval;
  1778. }
  1779. /*
  1780. * We fixed up user space. Now we need to fix the pi_state
  1781. * itself.
  1782. */
  1783. if (pi_state->owner != NULL) {
  1784. raw_spin_lock_irq(&pi_state->owner->pi_lock);
  1785. WARN_ON(list_empty(&pi_state->list));
  1786. list_del_init(&pi_state->list);
  1787. raw_spin_unlock_irq(&pi_state->owner->pi_lock);
  1788. }
  1789. pi_state->owner = newowner;
  1790. raw_spin_lock_irq(&newowner->pi_lock);
  1791. WARN_ON(!list_empty(&pi_state->list));
  1792. list_add(&pi_state->list, &newowner->pi_state_list);
  1793. raw_spin_unlock_irq(&newowner->pi_lock);
  1794. return 0;
  1795. /*
  1796. * To handle the page fault we need to drop the hash bucket
  1797. * lock here. That gives the other task (either the highest priority
  1798. * waiter itself or the task which stole the rtmutex) the
  1799. * chance to try the fixup of the pi_state. So once we are
  1800. * back from handling the fault we need to check the pi_state
  1801. * after reacquiring the hash bucket lock and before trying to
  1802. * do another fixup. When the fixup has been done already we
  1803. * simply return.
  1804. */
  1805. handle_fault:
  1806. spin_unlock(q->lock_ptr);
  1807. ret = fault_in_user_writeable(uaddr);
  1808. spin_lock(q->lock_ptr);
  1809. /*
  1810. * Check if someone else fixed it for us:
  1811. */
  1812. if (pi_state->owner != oldowner)
  1813. return 0;
  1814. if (ret)
  1815. return ret;
  1816. goto retry;
  1817. }
  1818. static long futex_wait_restart(struct restart_block *restart);
  1819. /**
  1820. * fixup_owner() - Post lock pi_state and corner case management
  1821. * @uaddr: user address of the futex
  1822. * @q: futex_q (contains pi_state and access to the rt_mutex)
  1823. * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
  1824. *
  1825. * After attempting to lock an rt_mutex, this function is called to cleanup
  1826. * the pi_state owner as well as handle race conditions that may allow us to
  1827. * acquire the lock. Must be called with the hb lock held.
  1828. *
  1829. * Return:
  1830. * 1 - success, lock taken;
  1831. * 0 - success, lock not taken;
  1832. * <0 - on error (-EFAULT)
  1833. */
  1834. static int fixup_owner(u32 __user *uaddr, struct futex_q *q, int locked)
  1835. {
  1836. struct task_struct *owner;
  1837. int ret = 0;
  1838. if (locked) {
  1839. /*
  1840. * Got the lock. We might not be the anticipated owner if we
  1841. * did a lock-steal - fix up the PI-state in that case:
  1842. */
  1843. if (q->pi_state->owner != current)
  1844. ret = fixup_pi_state_owner(uaddr, q, current);
  1845. goto out;
  1846. }
  1847. /*
  1848. * Catch the rare case, where the lock was released when we were on the
  1849. * way back before we locked the hash bucket.
  1850. */
  1851. if (q->pi_state->owner == current) {
  1852. /*
  1853. * Try to get the rt_mutex now. This might fail as some other
  1854. * task acquired the rt_mutex after we removed ourself from the
  1855. * rt_mutex waiters list.
  1856. */
  1857. if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
  1858. locked = 1;
  1859. goto out;
  1860. }
  1861. /*
  1862. * pi_state is incorrect, some other task did a lock steal and
  1863. * we returned due to timeout or signal without taking the
  1864. * rt_mutex. Too late.
  1865. */
  1866. raw_spin_lock(&q->pi_state->pi_mutex.wait_lock);
  1867. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  1868. if (!owner)
  1869. owner = rt_mutex_next_owner(&q->pi_state->pi_mutex);
  1870. raw_spin_unlock(&q->pi_state->pi_mutex.wait_lock);
  1871. ret = fixup_pi_state_owner(uaddr, q, owner);
  1872. goto out;
  1873. }
  1874. /*
  1875. * Paranoia check. If we did not take the lock, then we should not be
  1876. * the owner of the rt_mutex.
  1877. */
  1878. if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
  1879. printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
  1880. "pi-state %p\n", ret,
  1881. q->pi_state->pi_mutex.owner,
  1882. q->pi_state->owner);
  1883. out:
  1884. return ret ? ret : locked;
  1885. }
  1886. /**
  1887. * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
  1888. * @hb: the futex hash bucket, must be locked by the caller
  1889. * @q: the futex_q to queue up on
  1890. * @timeout: the prepared hrtimer_sleeper, or null for no timeout
  1891. */
  1892. static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
  1893. struct hrtimer_sleeper *timeout)
  1894. {
  1895. /*
  1896. * The task state is guaranteed to be set before another task can
  1897. * wake it. set_current_state() is implemented using set_mb() and
  1898. * queue_me() calls spin_unlock() upon completion, both serializing
  1899. * access to the hash list and forcing another memory barrier.
  1900. */
  1901. set_current_state(TASK_INTERRUPTIBLE);
  1902. queue_me(q, hb);
  1903. /* Arm the timer */
  1904. if (timeout) {
  1905. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1906. if (!hrtimer_active(&timeout->timer))
  1907. timeout->task = NULL;
  1908. }
  1909. /*
  1910. * If we have been removed from the hash list, then another task
  1911. * has tried to wake us, and we can skip the call to schedule().
  1912. */
  1913. if (likely(!plist_node_empty(&q->list))) {
  1914. /*
  1915. * If the timer has already expired, current will already be
  1916. * flagged for rescheduling. Only call schedule if there
  1917. * is no timeout, or if it has yet to expire.
  1918. */
  1919. if (!timeout || timeout->task)
  1920. freezable_schedule();
  1921. }
  1922. __set_current_state(TASK_RUNNING);
  1923. }
  1924. /**
  1925. * futex_wait_setup() - Prepare to wait on a futex
  1926. * @uaddr: the futex userspace address
  1927. * @val: the expected value
  1928. * @flags: futex flags (FLAGS_SHARED, etc.)
  1929. * @q: the associated futex_q
  1930. * @hb: storage for hash_bucket pointer to be returned to caller
  1931. *
  1932. * Setup the futex_q and locate the hash_bucket. Get the futex value and
  1933. * compare it with the expected value. Handle atomic faults internally.
  1934. * Return with the hb lock held and a q.key reference on success, and unlocked
  1935. * with no q.key reference on failure.
  1936. *
  1937. * Return:
  1938. * 0 - uaddr contains val and hb has been locked;
  1939. * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlocked
  1940. */
  1941. static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags,
  1942. struct futex_q *q, struct futex_hash_bucket **hb)
  1943. {
  1944. u32 uval;
  1945. int ret;
  1946. /*
  1947. * Access the page AFTER the hash-bucket is locked.
  1948. * Order is important:
  1949. *
  1950. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  1951. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  1952. *
  1953. * The basic logical guarantee of a futex is that it blocks ONLY
  1954. * if cond(var) is known to be true at the time of blocking, for
  1955. * any cond. If we locked the hash-bucket after testing *uaddr, that
  1956. * would open a race condition where we could block indefinitely with
  1957. * cond(var) false, which would violate the guarantee.
  1958. *
  1959. * On the other hand, we insert q and release the hash-bucket only
  1960. * after testing *uaddr. This guarantees that futex_wait() will NOT
  1961. * absorb a wakeup if *uaddr does not match the desired values
  1962. * while the syscall executes.
  1963. */
  1964. retry:
  1965. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ);
  1966. if (unlikely(ret != 0))
  1967. return ret;
  1968. retry_private:
  1969. *hb = queue_lock(q);
  1970. ret = get_futex_value_locked(&uval, uaddr);
  1971. if (ret) {
  1972. queue_unlock(*hb);
  1973. ret = get_user(uval, uaddr);
  1974. if (ret)
  1975. goto out;
  1976. if (!(flags & FLAGS_SHARED))
  1977. goto retry_private;
  1978. put_futex_key(&q->key);
  1979. goto retry;
  1980. }
  1981. if (uval != val) {
  1982. queue_unlock(*hb);
  1983. ret = -EWOULDBLOCK;
  1984. }
  1985. out:
  1986. if (ret)
  1987. put_futex_key(&q->key);
  1988. return ret;
  1989. }
  1990. static int futex_wait(u32 __user *uaddr, unsigned int flags, u32 val,
  1991. ktime_t *abs_time, u32 bitset)
  1992. {
  1993. struct hrtimer_sleeper timeout, *to = NULL;
  1994. struct restart_block *restart;
  1995. struct futex_hash_bucket *hb;
  1996. struct futex_q q = futex_q_init;
  1997. int ret;
  1998. if (!bitset)
  1999. return -EINVAL;
  2000. q.bitset = bitset;
  2001. if (abs_time) {
  2002. to = &timeout;
  2003. hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
  2004. CLOCK_REALTIME : CLOCK_MONOTONIC,
  2005. HRTIMER_MODE_ABS);
  2006. hrtimer_init_sleeper(to, current);
  2007. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  2008. current->timer_slack_ns);
  2009. }
  2010. retry:
  2011. /*
  2012. * Prepare to wait on uaddr. On success, holds hb lock and increments
  2013. * q.key refs.
  2014. */
  2015. ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
  2016. if (ret)
  2017. goto out;
  2018. /* queue_me and wait for wakeup, timeout, or a signal. */
  2019. futex_wait_queue_me(hb, &q, to);
  2020. /* If we were woken (and unqueued), we succeeded, whatever. */
  2021. ret = 0;
  2022. /* unqueue_me() drops q.key ref */
  2023. if (!unqueue_me(&q))
  2024. goto out;
  2025. ret = -ETIMEDOUT;
  2026. if (to && !to->task)
  2027. goto out;
  2028. /*
  2029. * We expect signal_pending(current), but we might be the
  2030. * victim of a spurious wakeup as well.
  2031. */
  2032. if (!signal_pending(current))
  2033. goto retry;
  2034. ret = -ERESTARTSYS;
  2035. if (!abs_time)
  2036. goto out;
  2037. restart = &current_thread_info()->restart_block;
  2038. restart->fn = futex_wait_restart;
  2039. restart->futex.uaddr = uaddr;
  2040. restart->futex.val = val;
  2041. restart->futex.time = abs_time->tv64;
  2042. restart->futex.bitset = bitset;
  2043. restart->futex.flags = flags | FLAGS_HAS_TIMEOUT;
  2044. ret = -ERESTART_RESTARTBLOCK;
  2045. out:
  2046. if (to) {
  2047. hrtimer_cancel(&to->timer);
  2048. destroy_hrtimer_on_stack(&to->timer);
  2049. }
  2050. return ret;
  2051. }
  2052. static long futex_wait_restart(struct restart_block *restart)
  2053. {
  2054. u32 __user *uaddr = restart->futex.uaddr;
  2055. ktime_t t, *tp = NULL;
  2056. if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
  2057. t.tv64 = restart->futex.time;
  2058. tp = &t;
  2059. }
  2060. restart->fn = do_no_restart_syscall;
  2061. return (long)futex_wait(uaddr, restart->futex.flags,
  2062. restart->futex.val, tp, restart->futex.bitset);
  2063. }
  2064. /*
  2065. * Userspace tried a 0 -> TID atomic transition of the futex value
  2066. * and failed. The kernel side here does the whole locking operation:
  2067. * if there are waiters then it will block, it does PI, etc. (Due to
  2068. * races the kernel might see a 0 value of the futex too.)
  2069. */
  2070. static int futex_lock_pi(u32 __user *uaddr, unsigned int flags, int detect,
  2071. ktime_t *time, int trylock)
  2072. {
  2073. struct hrtimer_sleeper timeout, *to = NULL;
  2074. struct futex_hash_bucket *hb;
  2075. struct futex_q q = futex_q_init;
  2076. int res, ret;
  2077. if (refill_pi_state_cache())
  2078. return -ENOMEM;
  2079. if (time) {
  2080. to = &timeout;
  2081. hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
  2082. HRTIMER_MODE_ABS);
  2083. hrtimer_init_sleeper(to, current);
  2084. hrtimer_set_expires(&to->timer, *time);
  2085. }
  2086. retry:
  2087. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE);
  2088. if (unlikely(ret != 0))
  2089. goto out;
  2090. retry_private:
  2091. hb = queue_lock(&q);
  2092. ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
  2093. if (unlikely(ret)) {
  2094. switch (ret) {
  2095. case 1:
  2096. /* We got the lock. */
  2097. ret = 0;
  2098. goto out_unlock_put_key;
  2099. case -EFAULT:
  2100. goto uaddr_faulted;
  2101. case -EAGAIN:
  2102. /*
  2103. * Two reasons for this:
  2104. * - Task is exiting and we just wait for the
  2105. * exit to complete.
  2106. * - The user space value changed.
  2107. */
  2108. queue_unlock(hb);
  2109. put_futex_key(&q.key);
  2110. cond_resched();
  2111. goto retry;
  2112. default:
  2113. goto out_unlock_put_key;
  2114. }
  2115. }
  2116. /*
  2117. * Only actually queue now that the atomic ops are done:
  2118. */
  2119. queue_me(&q, hb);
  2120. WARN_ON(!q.pi_state);
  2121. /*
  2122. * Block on the PI mutex:
  2123. */
  2124. if (!trylock) {
  2125. ret = rt_mutex_timed_futex_lock(&q.pi_state->pi_mutex, to);
  2126. } else {
  2127. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  2128. /* Fixup the trylock return value: */
  2129. ret = ret ? 0 : -EWOULDBLOCK;
  2130. }
  2131. spin_lock(q.lock_ptr);
  2132. /*
  2133. * Fixup the pi_state owner and possibly acquire the lock if we
  2134. * haven't already.
  2135. */
  2136. res = fixup_owner(uaddr, &q, !ret);
  2137. /*
  2138. * If fixup_owner() returned an error, proprogate that. If it acquired
  2139. * the lock, clear our -ETIMEDOUT or -EINTR.
  2140. */
  2141. if (res)
  2142. ret = (res < 0) ? res : 0;
  2143. /*
  2144. * If fixup_owner() faulted and was unable to handle the fault, unlock
  2145. * it and return the fault to userspace.
  2146. */
  2147. if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
  2148. rt_mutex_unlock(&q.pi_state->pi_mutex);
  2149. /* Unqueue and drop the lock */
  2150. unqueue_me_pi(&q);
  2151. goto out_put_key;
  2152. out_unlock_put_key:
  2153. queue_unlock(hb);
  2154. out_put_key:
  2155. put_futex_key(&q.key);
  2156. out:
  2157. if (to)
  2158. destroy_hrtimer_on_stack(&to->timer);
  2159. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  2160. uaddr_faulted:
  2161. queue_unlock(hb);
  2162. ret = fault_in_user_writeable(uaddr);
  2163. if (ret)
  2164. goto out_put_key;
  2165. if (!(flags & FLAGS_SHARED))
  2166. goto retry_private;
  2167. put_futex_key(&q.key);
  2168. goto retry;
  2169. }
  2170. /*
  2171. * Userspace attempted a TID -> 0 atomic transition, and failed.
  2172. * This is the in-kernel slowpath: we look up the PI state (if any),
  2173. * and do the rt-mutex unlock.
  2174. */
  2175. static int futex_unlock_pi(u32 __user *uaddr, unsigned int flags)
  2176. {
  2177. u32 uninitialized_var(curval), uval, vpid = task_pid_vnr(current);
  2178. union futex_key key = FUTEX_KEY_INIT;
  2179. struct futex_hash_bucket *hb;
  2180. struct futex_q *match;
  2181. int ret;
  2182. retry:
  2183. if (get_user(uval, uaddr))
  2184. return -EFAULT;
  2185. /*
  2186. * We release only a lock we actually own:
  2187. */
  2188. if ((uval & FUTEX_TID_MASK) != vpid)
  2189. return -EPERM;
  2190. ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE);
  2191. if (ret)
  2192. return ret;
  2193. hb = hash_futex(&key);
  2194. spin_lock(&hb->lock);
  2195. /*
  2196. * Check waiters first. We do not trust user space values at
  2197. * all and we at least want to know if user space fiddled
  2198. * with the futex value instead of blindly unlocking.
  2199. */
  2200. match = futex_top_waiter(hb, &key);
  2201. if (match) {
  2202. ret = wake_futex_pi(uaddr, uval, match);
  2203. /*
  2204. * The atomic access to the futex value generated a
  2205. * pagefault, so retry the user-access and the wakeup:
  2206. */
  2207. if (ret == -EFAULT)
  2208. goto pi_faulted;
  2209. goto out_unlock;
  2210. }
  2211. /*
  2212. * We have no kernel internal state, i.e. no waiters in the
  2213. * kernel. Waiters which are about to queue themselves are stuck
  2214. * on hb->lock. So we can safely ignore them. We do neither
  2215. * preserve the WAITERS bit not the OWNER_DIED one. We are the
  2216. * owner.
  2217. */
  2218. if (cmpxchg_futex_value_locked(&curval, uaddr, uval, 0))
  2219. goto pi_faulted;
  2220. /*
  2221. * If uval has changed, let user space handle it.
  2222. */
  2223. ret = (curval == uval) ? 0 : -EAGAIN;
  2224. out_unlock:
  2225. spin_unlock(&hb->lock);
  2226. put_futex_key(&key);
  2227. return ret;
  2228. pi_faulted:
  2229. spin_unlock(&hb->lock);
  2230. put_futex_key(&key);
  2231. ret = fault_in_user_writeable(uaddr);
  2232. if (!ret)
  2233. goto retry;
  2234. return ret;
  2235. }
  2236. /**
  2237. * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
  2238. * @hb: the hash_bucket futex_q was original enqueued on
  2239. * @q: the futex_q woken while waiting to be requeued
  2240. * @key2: the futex_key of the requeue target futex
  2241. * @timeout: the timeout associated with the wait (NULL if none)
  2242. *
  2243. * Detect if the task was woken on the initial futex as opposed to the requeue
  2244. * target futex. If so, determine if it was a timeout or a signal that caused
  2245. * the wakeup and return the appropriate error code to the caller. Must be
  2246. * called with the hb lock held.
  2247. *
  2248. * Return:
  2249. * 0 = no early wakeup detected;
  2250. * <0 = -ETIMEDOUT or -ERESTARTNOINTR
  2251. */
  2252. static inline
  2253. int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
  2254. struct futex_q *q, union futex_key *key2,
  2255. struct hrtimer_sleeper *timeout)
  2256. {
  2257. int ret = 0;
  2258. /*
  2259. * With the hb lock held, we avoid races while we process the wakeup.
  2260. * We only need to hold hb (and not hb2) to ensure atomicity as the
  2261. * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
  2262. * It can't be requeued from uaddr2 to something else since we don't
  2263. * support a PI aware source futex for requeue.
  2264. */
  2265. if (!match_futex(&q->key, key2)) {
  2266. WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
  2267. /*
  2268. * We were woken prior to requeue by a timeout or a signal.
  2269. * Unqueue the futex_q and determine which it was.
  2270. */
  2271. plist_del(&q->list, &hb->chain);
  2272. hb_waiters_dec(hb);
  2273. /* Handle spurious wakeups gracefully */
  2274. ret = -EWOULDBLOCK;
  2275. if (timeout && !timeout->task)
  2276. ret = -ETIMEDOUT;
  2277. else if (signal_pending(current))
  2278. ret = -ERESTARTNOINTR;
  2279. }
  2280. return ret;
  2281. }
  2282. /**
  2283. * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
  2284. * @uaddr: the futex we initially wait on (non-pi)
  2285. * @flags: futex flags (FLAGS_SHARED, FLAGS_CLOCKRT, etc.), they must be
  2286. * the same type, no requeueing from private to shared, etc.
  2287. * @val: the expected value of uaddr
  2288. * @abs_time: absolute timeout
  2289. * @bitset: 32 bit wakeup bitset set by userspace, defaults to all
  2290. * @uaddr2: the pi futex we will take prior to returning to user-space
  2291. *
  2292. * The caller will wait on uaddr and will be requeued by futex_requeue() to
  2293. * uaddr2 which must be PI aware and unique from uaddr. Normal wakeup will wake
  2294. * on uaddr2 and complete the acquisition of the rt_mutex prior to returning to
  2295. * userspace. This ensures the rt_mutex maintains an owner when it has waiters;
  2296. * without one, the pi logic would not know which task to boost/deboost, if
  2297. * there was a need to.
  2298. *
  2299. * We call schedule in futex_wait_queue_me() when we enqueue and return there
  2300. * via the following--
  2301. * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
  2302. * 2) wakeup on uaddr2 after a requeue
  2303. * 3) signal
  2304. * 4) timeout
  2305. *
  2306. * If 3, cleanup and return -ERESTARTNOINTR.
  2307. *
  2308. * If 2, we may then block on trying to take the rt_mutex and return via:
  2309. * 5) successful lock
  2310. * 6) signal
  2311. * 7) timeout
  2312. * 8) other lock acquisition failure
  2313. *
  2314. * If 6, return -EWOULDBLOCK (restarting the syscall would do the same).
  2315. *
  2316. * If 4 or 7, we cleanup and return with -ETIMEDOUT.
  2317. *
  2318. * Return:
  2319. * 0 - On success;
  2320. * <0 - On error
  2321. */
  2322. static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags,
  2323. u32 val, ktime_t *abs_time, u32 bitset,
  2324. u32 __user *uaddr2)
  2325. {
  2326. struct hrtimer_sleeper timeout, *to = NULL;
  2327. struct rt_mutex_waiter rt_waiter;
  2328. struct rt_mutex *pi_mutex = NULL;
  2329. struct futex_hash_bucket *hb;
  2330. union futex_key key2 = FUTEX_KEY_INIT;
  2331. struct futex_q q = futex_q_init;
  2332. int res, ret;
  2333. if (uaddr == uaddr2)
  2334. return -EINVAL;
  2335. if (!bitset)
  2336. return -EINVAL;
  2337. if (abs_time) {
  2338. to = &timeout;
  2339. hrtimer_init_on_stack(&to->timer, (flags & FLAGS_CLOCKRT) ?
  2340. CLOCK_REALTIME : CLOCK_MONOTONIC,
  2341. HRTIMER_MODE_ABS);
  2342. hrtimer_init_sleeper(to, current);
  2343. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  2344. current->timer_slack_ns);
  2345. }
  2346. /*
  2347. * The waiter is allocated on our stack, manipulated by the requeue
  2348. * code while we sleep on uaddr.
  2349. */
  2350. debug_rt_mutex_init_waiter(&rt_waiter);
  2351. RB_CLEAR_NODE(&rt_waiter.pi_tree_entry);
  2352. RB_CLEAR_NODE(&rt_waiter.tree_entry);
  2353. rt_waiter.task = NULL;
  2354. ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE);
  2355. if (unlikely(ret != 0))
  2356. goto out;
  2357. q.bitset = bitset;
  2358. q.rt_waiter = &rt_waiter;
  2359. q.requeue_pi_key = &key2;
  2360. /*
  2361. * Prepare to wait on uaddr. On success, increments q.key (key1) ref
  2362. * count.
  2363. */
  2364. ret = futex_wait_setup(uaddr, val, flags, &q, &hb);
  2365. if (ret)
  2366. goto out_key2;
  2367. /*
  2368. * The check above which compares uaddrs is not sufficient for
  2369. * shared futexes. We need to compare the keys:
  2370. */
  2371. if (match_futex(&q.key, &key2)) {
  2372. queue_unlock(hb);
  2373. ret = -EINVAL;
  2374. goto out_put_keys;
  2375. }
  2376. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  2377. futex_wait_queue_me(hb, &q, to);
  2378. spin_lock(&hb->lock);
  2379. ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  2380. spin_unlock(&hb->lock);
  2381. if (ret)
  2382. goto out_put_keys;
  2383. /*
  2384. * In order for us to be here, we know our q.key == key2, and since
  2385. * we took the hb->lock above, we also know that futex_requeue() has
  2386. * completed and we no longer have to concern ourselves with a wakeup
  2387. * race with the atomic proxy lock acquisition by the requeue code. The
  2388. * futex_requeue dropped our key1 reference and incremented our key2
  2389. * reference count.
  2390. */
  2391. /* Check if the requeue code acquired the second futex for us. */
  2392. if (!q.rt_waiter) {
  2393. /*
  2394. * Got the lock. We might not be the anticipated owner if we
  2395. * did a lock-steal - fix up the PI-state in that case.
  2396. */
  2397. if (q.pi_state && (q.pi_state->owner != current)) {
  2398. spin_lock(q.lock_ptr);
  2399. ret = fixup_pi_state_owner(uaddr2, &q, current);
  2400. spin_unlock(q.lock_ptr);
  2401. }
  2402. } else {
  2403. /*
  2404. * We have been woken up by futex_unlock_pi(), a timeout, or a
  2405. * signal. futex_unlock_pi() will not destroy the lock_ptr nor
  2406. * the pi_state.
  2407. */
  2408. WARN_ON(!q.pi_state);
  2409. pi_mutex = &q.pi_state->pi_mutex;
  2410. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter);
  2411. debug_rt_mutex_free_waiter(&rt_waiter);
  2412. spin_lock(q.lock_ptr);
  2413. /*
  2414. * Fixup the pi_state owner and possibly acquire the lock if we
  2415. * haven't already.
  2416. */
  2417. res = fixup_owner(uaddr2, &q, !ret);
  2418. /*
  2419. * If fixup_owner() returned an error, proprogate that. If it
  2420. * acquired the lock, clear -ETIMEDOUT or -EINTR.
  2421. */
  2422. if (res)
  2423. ret = (res < 0) ? res : 0;
  2424. /* Unqueue and drop the lock. */
  2425. unqueue_me_pi(&q);
  2426. }
  2427. /*
  2428. * If fixup_pi_state_owner() faulted and was unable to handle the
  2429. * fault, unlock the rt_mutex and return the fault to userspace.
  2430. */
  2431. if (ret == -EFAULT) {
  2432. if (pi_mutex && rt_mutex_owner(pi_mutex) == current)
  2433. rt_mutex_unlock(pi_mutex);
  2434. } else if (ret == -EINTR) {
  2435. /*
  2436. * We've already been requeued, but cannot restart by calling
  2437. * futex_lock_pi() directly. We could restart this syscall, but
  2438. * it would detect that the user space "val" changed and return
  2439. * -EWOULDBLOCK. Save the overhead of the restart and return
  2440. * -EWOULDBLOCK directly.
  2441. */
  2442. ret = -EWOULDBLOCK;
  2443. }
  2444. out_put_keys:
  2445. put_futex_key(&q.key);
  2446. out_key2:
  2447. put_futex_key(&key2);
  2448. out:
  2449. if (to) {
  2450. hrtimer_cancel(&to->timer);
  2451. destroy_hrtimer_on_stack(&to->timer);
  2452. }
  2453. return ret;
  2454. }
  2455. /*
  2456. * Support for robust futexes: the kernel cleans up held futexes at
  2457. * thread exit time.
  2458. *
  2459. * Implementation: user-space maintains a per-thread list of locks it
  2460. * is holding. Upon do_exit(), the kernel carefully walks this list,
  2461. * and marks all locks that are owned by this thread with the
  2462. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  2463. * always manipulated with the lock held, so the list is private and
  2464. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  2465. * field, to allow the kernel to clean up if the thread dies after
  2466. * acquiring the lock, but just before it could have added itself to
  2467. * the list. There can only be one such pending lock.
  2468. */
  2469. /**
  2470. * sys_set_robust_list() - Set the robust-futex list head of a task
  2471. * @head: pointer to the list-head
  2472. * @len: length of the list-head, as userspace expects
  2473. */
  2474. SYSCALL_DEFINE2(set_robust_list, struct robust_list_head __user *, head,
  2475. size_t, len)
  2476. {
  2477. if (!futex_cmpxchg_enabled)
  2478. return -ENOSYS;
  2479. /*
  2480. * The kernel knows only one size for now:
  2481. */
  2482. if (unlikely(len != sizeof(*head)))
  2483. return -EINVAL;
  2484. current->robust_list = head;
  2485. return 0;
  2486. }
  2487. /**
  2488. * sys_get_robust_list() - Get the robust-futex list head of a task
  2489. * @pid: pid of the process [zero for current task]
  2490. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  2491. * @len_ptr: pointer to a length field, the kernel fills in the header size
  2492. */
  2493. SYSCALL_DEFINE3(get_robust_list, int, pid,
  2494. struct robust_list_head __user * __user *, head_ptr,
  2495. size_t __user *, len_ptr)
  2496. {
  2497. struct robust_list_head __user *head;
  2498. unsigned long ret;
  2499. struct task_struct *p;
  2500. if (!futex_cmpxchg_enabled)
  2501. return -ENOSYS;
  2502. rcu_read_lock();
  2503. ret = -ESRCH;
  2504. if (!pid)
  2505. p = current;
  2506. else {
  2507. p = find_task_by_vpid(pid);
  2508. if (!p)
  2509. goto err_unlock;
  2510. }
  2511. ret = -EPERM;
  2512. if (!ptrace_may_access(p, PTRACE_MODE_READ))
  2513. goto err_unlock;
  2514. head = p->robust_list;
  2515. rcu_read_unlock();
  2516. if (put_user(sizeof(*head), len_ptr))
  2517. return -EFAULT;
  2518. return put_user(head, head_ptr);
  2519. err_unlock:
  2520. rcu_read_unlock();
  2521. return ret;
  2522. }
  2523. /*
  2524. * Process a futex-list entry, check whether it's owned by the
  2525. * dying task, and do notification if so:
  2526. */
  2527. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi)
  2528. {
  2529. u32 uval, uninitialized_var(nval), mval;
  2530. retry:
  2531. if (get_user(uval, uaddr))
  2532. return -1;
  2533. if ((uval & FUTEX_TID_MASK) == task_pid_vnr(curr)) {
  2534. /*
  2535. * Ok, this dying thread is truly holding a futex
  2536. * of interest. Set the OWNER_DIED bit atomically
  2537. * via cmpxchg, and if the value had FUTEX_WAITERS
  2538. * set, wake up a waiter (if any). (We have to do a
  2539. * futex_wake() even if OWNER_DIED is already set -
  2540. * to handle the rare but possible case of recursive
  2541. * thread-death.) The rest of the cleanup is done in
  2542. * userspace.
  2543. */
  2544. mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  2545. /*
  2546. * We are not holding a lock here, but we want to have
  2547. * the pagefault_disable/enable() protection because
  2548. * we want to handle the fault gracefully. If the
  2549. * access fails we try to fault in the futex with R/W
  2550. * verification via get_user_pages. get_user() above
  2551. * does not guarantee R/W access. If that fails we
  2552. * give up and leave the futex locked.
  2553. */
  2554. if (cmpxchg_futex_value_locked(&nval, uaddr, uval, mval)) {
  2555. if (fault_in_user_writeable(uaddr))
  2556. return -1;
  2557. goto retry;
  2558. }
  2559. if (nval != uval)
  2560. goto retry;
  2561. /*
  2562. * Wake robust non-PI futexes here. The wakeup of
  2563. * PI futexes happens in exit_pi_state():
  2564. */
  2565. if (!pi && (uval & FUTEX_WAITERS))
  2566. futex_wake(uaddr, 1, 1, FUTEX_BITSET_MATCH_ANY);
  2567. }
  2568. return 0;
  2569. }
  2570. /*
  2571. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  2572. */
  2573. static inline int fetch_robust_entry(struct robust_list __user **entry,
  2574. struct robust_list __user * __user *head,
  2575. unsigned int *pi)
  2576. {
  2577. unsigned long uentry;
  2578. if (get_user(uentry, (unsigned long __user *)head))
  2579. return -EFAULT;
  2580. *entry = (void __user *)(uentry & ~1UL);
  2581. *pi = uentry & 1;
  2582. return 0;
  2583. }
  2584. /*
  2585. * Walk curr->robust_list (very carefully, it's a userspace list!)
  2586. * and mark any locks found there dead, and notify any waiters.
  2587. *
  2588. * We silently return on any sign of list-walking problem.
  2589. */
  2590. void exit_robust_list(struct task_struct *curr)
  2591. {
  2592. struct robust_list_head __user *head = curr->robust_list;
  2593. struct robust_list __user *entry, *next_entry, *pending;
  2594. unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
  2595. unsigned int uninitialized_var(next_pi);
  2596. unsigned long futex_offset;
  2597. int rc;
  2598. if (!futex_cmpxchg_enabled)
  2599. return;
  2600. /*
  2601. * Fetch the list head (which was registered earlier, via
  2602. * sys_set_robust_list()):
  2603. */
  2604. if (fetch_robust_entry(&entry, &head->list.next, &pi))
  2605. return;
  2606. /*
  2607. * Fetch the relative futex offset:
  2608. */
  2609. if (get_user(futex_offset, &head->futex_offset))
  2610. return;
  2611. /*
  2612. * Fetch any possibly pending lock-add first, and handle it
  2613. * if it exists:
  2614. */
  2615. if (fetch_robust_entry(&pending, &head->list_op_pending, &pip))
  2616. return;
  2617. next_entry = NULL; /* avoid warning with gcc */
  2618. while (entry != &head->list) {
  2619. /*
  2620. * Fetch the next entry in the list before calling
  2621. * handle_futex_death:
  2622. */
  2623. rc = fetch_robust_entry(&next_entry, &entry->next, &next_pi);
  2624. /*
  2625. * A pending lock might already be on the list, so
  2626. * don't process it twice:
  2627. */
  2628. if (entry != pending)
  2629. if (handle_futex_death((void __user *)entry + futex_offset,
  2630. curr, pi))
  2631. return;
  2632. if (rc)
  2633. return;
  2634. entry = next_entry;
  2635. pi = next_pi;
  2636. /*
  2637. * Avoid excessively long or circular lists:
  2638. */
  2639. if (!--limit)
  2640. break;
  2641. cond_resched();
  2642. }
  2643. if (pending)
  2644. handle_futex_death((void __user *)pending + futex_offset,
  2645. curr, pip);
  2646. }
  2647. long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
  2648. u32 __user *uaddr2, u32 val2, u32 val3)
  2649. {
  2650. int cmd = op & FUTEX_CMD_MASK;
  2651. unsigned int flags = 0;
  2652. if (!(op & FUTEX_PRIVATE_FLAG))
  2653. flags |= FLAGS_SHARED;
  2654. if (op & FUTEX_CLOCK_REALTIME) {
  2655. flags |= FLAGS_CLOCKRT;
  2656. if (cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI)
  2657. return -ENOSYS;
  2658. }
  2659. switch (cmd) {
  2660. case FUTEX_LOCK_PI:
  2661. case FUTEX_UNLOCK_PI:
  2662. case FUTEX_TRYLOCK_PI:
  2663. case FUTEX_WAIT_REQUEUE_PI:
  2664. case FUTEX_CMP_REQUEUE_PI:
  2665. if (!futex_cmpxchg_enabled)
  2666. return -ENOSYS;
  2667. }
  2668. switch (cmd) {
  2669. case FUTEX_WAIT:
  2670. val3 = FUTEX_BITSET_MATCH_ANY;
  2671. case FUTEX_WAIT_BITSET:
  2672. return futex_wait(uaddr, flags, val, timeout, val3);
  2673. case FUTEX_WAKE:
  2674. val3 = FUTEX_BITSET_MATCH_ANY;
  2675. case FUTEX_WAKE_BITSET:
  2676. return futex_wake(uaddr, flags, val, val3);
  2677. case FUTEX_REQUEUE:
  2678. return futex_requeue(uaddr, flags, uaddr2, val, val2, NULL, 0);
  2679. case FUTEX_CMP_REQUEUE:
  2680. return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 0);
  2681. case FUTEX_WAKE_OP:
  2682. return futex_wake_op(uaddr, flags, uaddr2, val, val2, val3);
  2683. case FUTEX_LOCK_PI:
  2684. return futex_lock_pi(uaddr, flags, val, timeout, 0);
  2685. case FUTEX_UNLOCK_PI:
  2686. return futex_unlock_pi(uaddr, flags);
  2687. case FUTEX_TRYLOCK_PI:
  2688. return futex_lock_pi(uaddr, flags, 0, timeout, 1);
  2689. case FUTEX_WAIT_REQUEUE_PI:
  2690. val3 = FUTEX_BITSET_MATCH_ANY;
  2691. return futex_wait_requeue_pi(uaddr, flags, val, timeout, val3,
  2692. uaddr2);
  2693. case FUTEX_CMP_REQUEUE_PI:
  2694. return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
  2695. }
  2696. return -ENOSYS;
  2697. }
  2698. SYSCALL_DEFINE6(futex, u32 __user *, uaddr, int, op, u32, val,
  2699. struct timespec __user *, utime, u32 __user *, uaddr2,
  2700. u32, val3)
  2701. {
  2702. struct timespec ts;
  2703. ktime_t t, *tp = NULL;
  2704. u32 val2 = 0;
  2705. int cmd = op & FUTEX_CMD_MASK;
  2706. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
  2707. cmd == FUTEX_WAIT_BITSET ||
  2708. cmd == FUTEX_WAIT_REQUEUE_PI)) {
  2709. if (copy_from_user(&ts, utime, sizeof(ts)) != 0)
  2710. return -EFAULT;
  2711. if (!timespec_valid(&ts))
  2712. return -EINVAL;
  2713. t = timespec_to_ktime(ts);
  2714. if (cmd == FUTEX_WAIT)
  2715. t = ktime_add_safe(ktime_get(), t);
  2716. tp = &t;
  2717. }
  2718. /*
  2719. * requeue parameter in 'utime' if cmd == FUTEX_*_REQUEUE_*.
  2720. * number of waiters to wake in 'utime' if cmd == FUTEX_WAKE_OP.
  2721. */
  2722. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
  2723. cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
  2724. val2 = (u32) (unsigned long) utime;
  2725. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  2726. }
  2727. static void __init futex_detect_cmpxchg(void)
  2728. {
  2729. #ifndef CONFIG_HAVE_FUTEX_CMPXCHG
  2730. u32 curval;
  2731. /*
  2732. * This will fail and we want it. Some arch implementations do
  2733. * runtime detection of the futex_atomic_cmpxchg_inatomic()
  2734. * functionality. We want to know that before we call in any
  2735. * of the complex code paths. Also we want to prevent
  2736. * registration of robust lists in that case. NULL is
  2737. * guaranteed to fault and we get -EFAULT on functional
  2738. * implementation, the non-functional ones will return
  2739. * -ENOSYS.
  2740. */
  2741. if (cmpxchg_futex_value_locked(&curval, NULL, 0, 0) == -EFAULT)
  2742. futex_cmpxchg_enabled = 1;
  2743. #endif
  2744. }
  2745. static int __init futex_init(void)
  2746. {
  2747. unsigned int futex_shift;
  2748. unsigned long i;
  2749. #if CONFIG_BASE_SMALL
  2750. futex_hashsize = 16;
  2751. #else
  2752. futex_hashsize = roundup_pow_of_two(256 * num_possible_cpus());
  2753. #endif
  2754. futex_queues = alloc_large_system_hash("futex", sizeof(*futex_queues),
  2755. futex_hashsize, 0,
  2756. futex_hashsize < 256 ? HASH_SMALL : 0,
  2757. &futex_shift, NULL,
  2758. futex_hashsize, futex_hashsize);
  2759. futex_hashsize = 1UL << futex_shift;
  2760. futex_detect_cmpxchg();
  2761. for (i = 0; i < futex_hashsize; i++) {
  2762. atomic_set(&futex_queues[i].waiters, 0);
  2763. plist_head_init(&futex_queues[i].chain);
  2764. spin_lock_init(&futex_queues[i].lock);
  2765. }
  2766. return 0;
  2767. }
  2768. __initcall(futex_init);