futex.c 71 KB

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