futex.c 67 KB

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