futex.c 84 KB

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