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