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. * @rw: mapping needs to be read/write (values: VERIFY_READ, VERIFY_WRITE)
  180. *
  181. * Returns a negative error code or 0
  182. * The key words are stored in *key on success.
  183. *
  184. * For shared mappings, it's (page->index, vma->vm_file->f_path.dentry->d_inode,
  185. * offset_within_page). For private mappings, it's (uaddr, current->mm).
  186. * We can usually work out the index without swapping in the page.
  187. *
  188. * lock_page() might sleep, the caller should not hold a spinlock.
  189. */
  190. static int
  191. get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)
  192. {
  193. unsigned long address = (unsigned long)uaddr;
  194. struct mm_struct *mm = current->mm;
  195. struct page *page;
  196. int err;
  197. /*
  198. * The futex address must be "naturally" aligned.
  199. */
  200. key->both.offset = address % PAGE_SIZE;
  201. if (unlikely((address % sizeof(u32)) != 0))
  202. return -EINVAL;
  203. address -= key->both.offset;
  204. /*
  205. * PROCESS_PRIVATE futexes are fast.
  206. * As the mm cannot disappear under us and the 'key' only needs
  207. * virtual address, we dont even have to find the underlying vma.
  208. * Note : We do have to check 'uaddr' is a valid user address,
  209. * but access_ok() should be faster than find_vma()
  210. */
  211. if (!fshared) {
  212. if (unlikely(!access_ok(rw, uaddr, sizeof(u32))))
  213. return -EFAULT;
  214. key->private.mm = mm;
  215. key->private.address = address;
  216. get_futex_key_refs(key);
  217. return 0;
  218. }
  219. again:
  220. err = get_user_pages_fast(address, 1, rw == VERIFY_WRITE, &page);
  221. if (err < 0)
  222. return err;
  223. lock_page(page);
  224. if (!page->mapping) {
  225. unlock_page(page);
  226. put_page(page);
  227. goto again;
  228. }
  229. /*
  230. * Private mappings are handled in a simple way.
  231. *
  232. * NOTE: When userspace waits on a MAP_SHARED mapping, even if
  233. * it's a read-only handle, it's expected that futexes attach to
  234. * the object not the particular process.
  235. */
  236. if (PageAnon(page)) {
  237. key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
  238. key->private.mm = mm;
  239. key->private.address = address;
  240. } else {
  241. key->both.offset |= FUT_OFF_INODE; /* inode-based key */
  242. key->shared.inode = page->mapping->host;
  243. key->shared.pgoff = page->index;
  244. }
  245. get_futex_key_refs(key);
  246. unlock_page(page);
  247. put_page(page);
  248. return 0;
  249. }
  250. static inline
  251. void put_futex_key(int fshared, union futex_key *key)
  252. {
  253. drop_futex_key_refs(key);
  254. }
  255. /**
  256. * futex_top_waiter() - Return the highest priority waiter on a futex
  257. * @hb: the hash bucket the futex_q's reside in
  258. * @key: the futex key (to distinguish it from other futex futex_q's)
  259. *
  260. * Must be called with the hb lock held.
  261. */
  262. static struct futex_q *futex_top_waiter(struct futex_hash_bucket *hb,
  263. union futex_key *key)
  264. {
  265. struct futex_q *this;
  266. plist_for_each_entry(this, &hb->chain, list) {
  267. if (match_futex(&this->key, key))
  268. return this;
  269. }
  270. return NULL;
  271. }
  272. static u32 cmpxchg_futex_value_locked(u32 __user *uaddr, u32 uval, u32 newval)
  273. {
  274. u32 curval;
  275. pagefault_disable();
  276. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  277. pagefault_enable();
  278. return curval;
  279. }
  280. static int get_futex_value_locked(u32 *dest, u32 __user *from)
  281. {
  282. int ret;
  283. pagefault_disable();
  284. ret = __copy_from_user_inatomic(dest, from, sizeof(u32));
  285. pagefault_enable();
  286. return ret ? -EFAULT : 0;
  287. }
  288. /*
  289. * PI code:
  290. */
  291. static int refill_pi_state_cache(void)
  292. {
  293. struct futex_pi_state *pi_state;
  294. if (likely(current->pi_state_cache))
  295. return 0;
  296. pi_state = kzalloc(sizeof(*pi_state), GFP_KERNEL);
  297. if (!pi_state)
  298. return -ENOMEM;
  299. INIT_LIST_HEAD(&pi_state->list);
  300. /* pi_mutex gets initialized later */
  301. pi_state->owner = NULL;
  302. atomic_set(&pi_state->refcount, 1);
  303. pi_state->key = FUTEX_KEY_INIT;
  304. current->pi_state_cache = pi_state;
  305. return 0;
  306. }
  307. static struct futex_pi_state * alloc_pi_state(void)
  308. {
  309. struct futex_pi_state *pi_state = current->pi_state_cache;
  310. WARN_ON(!pi_state);
  311. current->pi_state_cache = NULL;
  312. return pi_state;
  313. }
  314. static void free_pi_state(struct futex_pi_state *pi_state)
  315. {
  316. if (!atomic_dec_and_test(&pi_state->refcount))
  317. return;
  318. /*
  319. * If pi_state->owner is NULL, the owner is most probably dying
  320. * and has cleaned up the pi_state already
  321. */
  322. if (pi_state->owner) {
  323. spin_lock_irq(&pi_state->owner->pi_lock);
  324. list_del_init(&pi_state->list);
  325. spin_unlock_irq(&pi_state->owner->pi_lock);
  326. rt_mutex_proxy_unlock(&pi_state->pi_mutex, pi_state->owner);
  327. }
  328. if (current->pi_state_cache)
  329. kfree(pi_state);
  330. else {
  331. /*
  332. * pi_state->list is already empty.
  333. * clear pi_state->owner.
  334. * refcount is at 0 - put it back to 1.
  335. */
  336. pi_state->owner = NULL;
  337. atomic_set(&pi_state->refcount, 1);
  338. current->pi_state_cache = pi_state;
  339. }
  340. }
  341. /*
  342. * Look up the task based on what TID userspace gave us.
  343. * We dont trust it.
  344. */
  345. static struct task_struct * futex_find_get_task(pid_t pid)
  346. {
  347. struct task_struct *p;
  348. const struct cred *cred = current_cred(), *pcred;
  349. rcu_read_lock();
  350. p = find_task_by_vpid(pid);
  351. if (!p) {
  352. p = ERR_PTR(-ESRCH);
  353. } else {
  354. pcred = __task_cred(p);
  355. if (cred->euid != pcred->euid &&
  356. cred->euid != pcred->uid)
  357. p = ERR_PTR(-ESRCH);
  358. else
  359. get_task_struct(p);
  360. }
  361. rcu_read_unlock();
  362. return p;
  363. }
  364. /*
  365. * This task is holding PI mutexes at exit time => bad.
  366. * Kernel cleans up PI-state, but userspace is likely hosed.
  367. * (Robust-futex cleanup is separate and might save the day for userspace.)
  368. */
  369. void exit_pi_state_list(struct task_struct *curr)
  370. {
  371. struct list_head *next, *head = &curr->pi_state_list;
  372. struct futex_pi_state *pi_state;
  373. struct futex_hash_bucket *hb;
  374. union futex_key key = FUTEX_KEY_INIT;
  375. if (!futex_cmpxchg_enabled)
  376. return;
  377. /*
  378. * We are a ZOMBIE and nobody can enqueue itself on
  379. * pi_state_list anymore, but we have to be careful
  380. * versus waiters unqueueing themselves:
  381. */
  382. spin_lock_irq(&curr->pi_lock);
  383. while (!list_empty(head)) {
  384. next = head->next;
  385. pi_state = list_entry(next, struct futex_pi_state, list);
  386. key = pi_state->key;
  387. hb = hash_futex(&key);
  388. spin_unlock_irq(&curr->pi_lock);
  389. spin_lock(&hb->lock);
  390. spin_lock_irq(&curr->pi_lock);
  391. /*
  392. * We dropped the pi-lock, so re-check whether this
  393. * task still owns the PI-state:
  394. */
  395. if (head->next != next) {
  396. spin_unlock(&hb->lock);
  397. continue;
  398. }
  399. WARN_ON(pi_state->owner != curr);
  400. WARN_ON(list_empty(&pi_state->list));
  401. list_del_init(&pi_state->list);
  402. pi_state->owner = NULL;
  403. spin_unlock_irq(&curr->pi_lock);
  404. rt_mutex_unlock(&pi_state->pi_mutex);
  405. spin_unlock(&hb->lock);
  406. spin_lock_irq(&curr->pi_lock);
  407. }
  408. spin_unlock_irq(&curr->pi_lock);
  409. }
  410. static int
  411. lookup_pi_state(u32 uval, struct futex_hash_bucket *hb,
  412. union futex_key *key, struct futex_pi_state **ps)
  413. {
  414. struct futex_pi_state *pi_state = NULL;
  415. struct futex_q *this, *next;
  416. struct plist_head *head;
  417. struct task_struct *p;
  418. pid_t pid = uval & FUTEX_TID_MASK;
  419. head = &hb->chain;
  420. plist_for_each_entry_safe(this, next, head, list) {
  421. if (match_futex(&this->key, key)) {
  422. /*
  423. * Another waiter already exists - bump up
  424. * the refcount and return its pi_state:
  425. */
  426. pi_state = this->pi_state;
  427. /*
  428. * Userspace might have messed up non PI and PI futexes
  429. */
  430. if (unlikely(!pi_state))
  431. return -EINVAL;
  432. WARN_ON(!atomic_read(&pi_state->refcount));
  433. WARN_ON(pid && pi_state->owner &&
  434. pi_state->owner->pid != pid);
  435. atomic_inc(&pi_state->refcount);
  436. *ps = pi_state;
  437. return 0;
  438. }
  439. }
  440. /*
  441. * We are the first waiter - try to look up the real owner and attach
  442. * the new pi_state to it, but bail out when TID = 0
  443. */
  444. if (!pid)
  445. return -ESRCH;
  446. p = futex_find_get_task(pid);
  447. if (IS_ERR(p))
  448. return PTR_ERR(p);
  449. /*
  450. * We need to look at the task state flags to figure out,
  451. * whether the task is exiting. To protect against the do_exit
  452. * change of the task flags, we do this protected by
  453. * p->pi_lock:
  454. */
  455. spin_lock_irq(&p->pi_lock);
  456. if (unlikely(p->flags & PF_EXITING)) {
  457. /*
  458. * The task is on the way out. When PF_EXITPIDONE is
  459. * set, we know that the task has finished the
  460. * cleanup:
  461. */
  462. int ret = (p->flags & PF_EXITPIDONE) ? -ESRCH : -EAGAIN;
  463. spin_unlock_irq(&p->pi_lock);
  464. put_task_struct(p);
  465. return ret;
  466. }
  467. pi_state = alloc_pi_state();
  468. /*
  469. * Initialize the pi_mutex in locked state and make 'p'
  470. * the owner of it:
  471. */
  472. rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
  473. /* Store the key for possible exit cleanups: */
  474. pi_state->key = *key;
  475. WARN_ON(!list_empty(&pi_state->list));
  476. list_add(&pi_state->list, &p->pi_state_list);
  477. pi_state->owner = p;
  478. spin_unlock_irq(&p->pi_lock);
  479. put_task_struct(p);
  480. *ps = pi_state;
  481. return 0;
  482. }
  483. /**
  484. * futex_lock_pi_atomic() - atomic work required to acquire a pi aware futex
  485. * @uaddr: the pi futex user address
  486. * @hb: the pi futex hash bucket
  487. * @key: the futex key associated with uaddr and hb
  488. * @ps: the pi_state pointer where we store the result of the
  489. * lookup
  490. * @task: the task to perform the atomic lock work for. This will
  491. * be "current" except in the case of requeue pi.
  492. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  493. *
  494. * Returns:
  495. * 0 - ready to wait
  496. * 1 - acquired the lock
  497. * <0 - error
  498. *
  499. * The hb->lock and futex_key refs shall be held by the caller.
  500. */
  501. static int futex_lock_pi_atomic(u32 __user *uaddr, struct futex_hash_bucket *hb,
  502. union futex_key *key,
  503. struct futex_pi_state **ps,
  504. struct task_struct *task, int set_waiters)
  505. {
  506. int lock_taken, ret, ownerdied = 0;
  507. u32 uval, newval, curval;
  508. retry:
  509. ret = lock_taken = 0;
  510. /*
  511. * To avoid races, we attempt to take the lock here again
  512. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  513. * the locks. It will most likely not succeed.
  514. */
  515. newval = task_pid_vnr(task);
  516. if (set_waiters)
  517. newval |= FUTEX_WAITERS;
  518. curval = cmpxchg_futex_value_locked(uaddr, 0, newval);
  519. if (unlikely(curval == -EFAULT))
  520. return -EFAULT;
  521. /*
  522. * Detect deadlocks.
  523. */
  524. if ((unlikely((curval & FUTEX_TID_MASK) == task_pid_vnr(task))))
  525. return -EDEADLK;
  526. /*
  527. * Surprise - we got the lock. Just return to userspace:
  528. */
  529. if (unlikely(!curval))
  530. return 1;
  531. uval = curval;
  532. /*
  533. * Set the FUTEX_WAITERS flag, so the owner will know it has someone
  534. * to wake at the next unlock.
  535. */
  536. newval = curval | FUTEX_WAITERS;
  537. /*
  538. * There are two cases, where a futex might have no owner (the
  539. * owner TID is 0): OWNER_DIED. We take over the futex in this
  540. * case. We also do an unconditional take over, when the owner
  541. * of the futex died.
  542. *
  543. * This is safe as we are protected by the hash bucket lock !
  544. */
  545. if (unlikely(ownerdied || !(curval & FUTEX_TID_MASK))) {
  546. /* Keep the OWNER_DIED bit */
  547. newval = (curval & ~FUTEX_TID_MASK) | task_pid_vnr(task);
  548. ownerdied = 0;
  549. lock_taken = 1;
  550. }
  551. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  552. if (unlikely(curval == -EFAULT))
  553. return -EFAULT;
  554. if (unlikely(curval != uval))
  555. goto retry;
  556. /*
  557. * We took the lock due to owner died take over.
  558. */
  559. if (unlikely(lock_taken))
  560. return 1;
  561. /*
  562. * We dont have the lock. Look up the PI state (or create it if
  563. * we are the first waiter):
  564. */
  565. ret = lookup_pi_state(uval, hb, key, ps);
  566. if (unlikely(ret)) {
  567. switch (ret) {
  568. case -ESRCH:
  569. /*
  570. * No owner found for this futex. Check if the
  571. * OWNER_DIED bit is set to figure out whether
  572. * this is a robust futex or not.
  573. */
  574. if (get_futex_value_locked(&curval, uaddr))
  575. return -EFAULT;
  576. /*
  577. * We simply start over in case of a robust
  578. * futex. The code above will take the futex
  579. * and return happy.
  580. */
  581. if (curval & FUTEX_OWNER_DIED) {
  582. ownerdied = 1;
  583. goto retry;
  584. }
  585. default:
  586. break;
  587. }
  588. }
  589. return ret;
  590. }
  591. /*
  592. * The hash bucket lock must be held when this is called.
  593. * Afterwards, the futex_q must not be accessed.
  594. */
  595. static void wake_futex(struct futex_q *q)
  596. {
  597. struct task_struct *p = q->task;
  598. /*
  599. * We set q->lock_ptr = NULL _before_ we wake up the task. If
  600. * a non futex wake up happens on another CPU then the task
  601. * might exit and p would dereference a non existing task
  602. * struct. Prevent this by holding a reference on p across the
  603. * wake up.
  604. */
  605. get_task_struct(p);
  606. plist_del(&q->list, &q->list.plist);
  607. /*
  608. * The waiting task can free the futex_q as soon as
  609. * q->lock_ptr = NULL is written, without taking any locks. A
  610. * memory barrier is required here to prevent the following
  611. * store to lock_ptr from getting ahead of the plist_del.
  612. */
  613. smp_wmb();
  614. q->lock_ptr = NULL;
  615. wake_up_state(p, TASK_NORMAL);
  616. put_task_struct(p);
  617. }
  618. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  619. {
  620. struct task_struct *new_owner;
  621. struct futex_pi_state *pi_state = this->pi_state;
  622. u32 curval, newval;
  623. if (!pi_state)
  624. return -EINVAL;
  625. spin_lock(&pi_state->pi_mutex.wait_lock);
  626. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  627. /*
  628. * This happens when we have stolen the lock and the original
  629. * pending owner did not enqueue itself back on the rt_mutex.
  630. * Thats not a tragedy. We know that way, that a lock waiter
  631. * is on the fly. We make the futex_q waiter the pending owner.
  632. */
  633. if (!new_owner)
  634. new_owner = this->task;
  635. /*
  636. * We pass it to the next owner. (The WAITERS bit is always
  637. * kept enabled while there is PI state around. We must also
  638. * preserve the owner died bit.)
  639. */
  640. if (!(uval & FUTEX_OWNER_DIED)) {
  641. int ret = 0;
  642. newval = FUTEX_WAITERS | task_pid_vnr(new_owner);
  643. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  644. if (curval == -EFAULT)
  645. ret = -EFAULT;
  646. else if (curval != uval)
  647. ret = -EINVAL;
  648. if (ret) {
  649. spin_unlock(&pi_state->pi_mutex.wait_lock);
  650. return ret;
  651. }
  652. }
  653. spin_lock_irq(&pi_state->owner->pi_lock);
  654. WARN_ON(list_empty(&pi_state->list));
  655. list_del_init(&pi_state->list);
  656. spin_unlock_irq(&pi_state->owner->pi_lock);
  657. spin_lock_irq(&new_owner->pi_lock);
  658. WARN_ON(!list_empty(&pi_state->list));
  659. list_add(&pi_state->list, &new_owner->pi_state_list);
  660. pi_state->owner = new_owner;
  661. spin_unlock_irq(&new_owner->pi_lock);
  662. spin_unlock(&pi_state->pi_mutex.wait_lock);
  663. rt_mutex_unlock(&pi_state->pi_mutex);
  664. return 0;
  665. }
  666. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  667. {
  668. u32 oldval;
  669. /*
  670. * There is no waiter, so we unlock the futex. The owner died
  671. * bit has not to be preserved here. We are the owner:
  672. */
  673. oldval = cmpxchg_futex_value_locked(uaddr, uval, 0);
  674. if (oldval == -EFAULT)
  675. return oldval;
  676. if (oldval != uval)
  677. return -EAGAIN;
  678. return 0;
  679. }
  680. /*
  681. * Express the locking dependencies for lockdep:
  682. */
  683. static inline void
  684. double_lock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  685. {
  686. if (hb1 <= hb2) {
  687. spin_lock(&hb1->lock);
  688. if (hb1 < hb2)
  689. spin_lock_nested(&hb2->lock, SINGLE_DEPTH_NESTING);
  690. } else { /* hb1 > hb2 */
  691. spin_lock(&hb2->lock);
  692. spin_lock_nested(&hb1->lock, SINGLE_DEPTH_NESTING);
  693. }
  694. }
  695. static inline void
  696. double_unlock_hb(struct futex_hash_bucket *hb1, struct futex_hash_bucket *hb2)
  697. {
  698. spin_unlock(&hb1->lock);
  699. if (hb1 != hb2)
  700. spin_unlock(&hb2->lock);
  701. }
  702. /*
  703. * Wake up waiters matching bitset queued on this futex (uaddr).
  704. */
  705. static int futex_wake(u32 __user *uaddr, int fshared, int nr_wake, u32 bitset)
  706. {
  707. struct futex_hash_bucket *hb;
  708. struct futex_q *this, *next;
  709. struct plist_head *head;
  710. union futex_key key = FUTEX_KEY_INIT;
  711. int ret;
  712. if (!bitset)
  713. return -EINVAL;
  714. ret = get_futex_key(uaddr, fshared, &key, VERIFY_READ);
  715. if (unlikely(ret != 0))
  716. goto out;
  717. hb = hash_futex(&key);
  718. spin_lock(&hb->lock);
  719. head = &hb->chain;
  720. plist_for_each_entry_safe(this, next, head, list) {
  721. if (match_futex (&this->key, &key)) {
  722. if (this->pi_state || this->rt_waiter) {
  723. ret = -EINVAL;
  724. break;
  725. }
  726. /* Check if one of the bits is set in both bitsets */
  727. if (!(this->bitset & bitset))
  728. continue;
  729. wake_futex(this);
  730. if (++ret >= nr_wake)
  731. break;
  732. }
  733. }
  734. spin_unlock(&hb->lock);
  735. put_futex_key(fshared, &key);
  736. out:
  737. return ret;
  738. }
  739. /*
  740. * Wake up all waiters hashed on the physical page that is mapped
  741. * to this virtual address:
  742. */
  743. static int
  744. futex_wake_op(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
  745. int nr_wake, int nr_wake2, int op)
  746. {
  747. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  748. struct futex_hash_bucket *hb1, *hb2;
  749. struct plist_head *head;
  750. struct futex_q *this, *next;
  751. int ret, op_ret;
  752. retry:
  753. ret = get_futex_key(uaddr1, fshared, &key1, VERIFY_READ);
  754. if (unlikely(ret != 0))
  755. goto out;
  756. ret = get_futex_key(uaddr2, fshared, &key2, VERIFY_WRITE);
  757. if (unlikely(ret != 0))
  758. goto out_put_key1;
  759. hb1 = hash_futex(&key1);
  760. hb2 = hash_futex(&key2);
  761. double_lock_hb(hb1, hb2);
  762. retry_private:
  763. op_ret = futex_atomic_op_inuser(op, uaddr2);
  764. if (unlikely(op_ret < 0)) {
  765. u32 dummy;
  766. double_unlock_hb(hb1, hb2);
  767. #ifndef CONFIG_MMU
  768. /*
  769. * we don't get EFAULT from MMU faults if we don't have an MMU,
  770. * but we might get them from range checking
  771. */
  772. ret = op_ret;
  773. goto out_put_keys;
  774. #endif
  775. if (unlikely(op_ret != -EFAULT)) {
  776. ret = op_ret;
  777. goto out_put_keys;
  778. }
  779. ret = get_user(dummy, uaddr2);
  780. if (ret)
  781. goto out_put_keys;
  782. if (!fshared)
  783. goto retry_private;
  784. put_futex_key(fshared, &key2);
  785. put_futex_key(fshared, &key1);
  786. goto retry;
  787. }
  788. head = &hb1->chain;
  789. plist_for_each_entry_safe(this, next, head, list) {
  790. if (match_futex (&this->key, &key1)) {
  791. wake_futex(this);
  792. if (++ret >= nr_wake)
  793. break;
  794. }
  795. }
  796. if (op_ret > 0) {
  797. head = &hb2->chain;
  798. op_ret = 0;
  799. plist_for_each_entry_safe(this, next, head, list) {
  800. if (match_futex (&this->key, &key2)) {
  801. wake_futex(this);
  802. if (++op_ret >= nr_wake2)
  803. break;
  804. }
  805. }
  806. ret += op_ret;
  807. }
  808. double_unlock_hb(hb1, hb2);
  809. out_put_keys:
  810. put_futex_key(fshared, &key2);
  811. out_put_key1:
  812. put_futex_key(fshared, &key1);
  813. out:
  814. return ret;
  815. }
  816. /**
  817. * requeue_futex() - Requeue a futex_q from one hb to another
  818. * @q: the futex_q to requeue
  819. * @hb1: the source hash_bucket
  820. * @hb2: the target hash_bucket
  821. * @key2: the new key for the requeued futex_q
  822. */
  823. static inline
  824. void requeue_futex(struct futex_q *q, struct futex_hash_bucket *hb1,
  825. struct futex_hash_bucket *hb2, union futex_key *key2)
  826. {
  827. /*
  828. * If key1 and key2 hash to the same bucket, no need to
  829. * requeue.
  830. */
  831. if (likely(&hb1->chain != &hb2->chain)) {
  832. plist_del(&q->list, &hb1->chain);
  833. plist_add(&q->list, &hb2->chain);
  834. q->lock_ptr = &hb2->lock;
  835. #ifdef CONFIG_DEBUG_PI_LIST
  836. q->list.plist.lock = &hb2->lock;
  837. #endif
  838. }
  839. get_futex_key_refs(key2);
  840. q->key = *key2;
  841. }
  842. /**
  843. * requeue_pi_wake_futex() - Wake a task that acquired the lock during requeue
  844. * q: the futex_q
  845. * key: the key of the requeue target futex
  846. *
  847. * During futex_requeue, with requeue_pi=1, it is possible to acquire the
  848. * target futex if it is uncontended or via a lock steal. Set the futex_q key
  849. * to the requeue target futex so the waiter can detect the wakeup on the right
  850. * futex, but remove it from the hb and NULL the rt_waiter so it can detect
  851. * atomic lock acquisition. Must be called with the q->lock_ptr held.
  852. */
  853. static inline
  854. void requeue_pi_wake_futex(struct futex_q *q, union futex_key *key)
  855. {
  856. drop_futex_key_refs(&q->key);
  857. get_futex_key_refs(key);
  858. q->key = *key;
  859. WARN_ON(plist_node_empty(&q->list));
  860. plist_del(&q->list, &q->list.plist);
  861. WARN_ON(!q->rt_waiter);
  862. q->rt_waiter = NULL;
  863. wake_up_state(q->task, TASK_NORMAL);
  864. }
  865. /**
  866. * futex_proxy_trylock_atomic() - Attempt an atomic lock for the top waiter
  867. * @pifutex: the user address of the to futex
  868. * @hb1: the from futex hash bucket, must be locked by the caller
  869. * @hb2: the to futex hash bucket, must be locked by the caller
  870. * @key1: the from futex key
  871. * @key2: the to futex key
  872. * @ps: address to store the pi_state pointer
  873. * @set_waiters: force setting the FUTEX_WAITERS bit (1) or not (0)
  874. *
  875. * Try and get the lock on behalf of the top waiter if we can do it atomically.
  876. * Wake the top waiter if we succeed. If the caller specified set_waiters,
  877. * then direct futex_lock_pi_atomic() to force setting the FUTEX_WAITERS bit.
  878. * hb1 and hb2 must be held by the caller.
  879. *
  880. * Returns:
  881. * 0 - failed to acquire the lock atomicly
  882. * 1 - acquired the lock
  883. * <0 - error
  884. */
  885. static int futex_proxy_trylock_atomic(u32 __user *pifutex,
  886. struct futex_hash_bucket *hb1,
  887. struct futex_hash_bucket *hb2,
  888. union futex_key *key1, union futex_key *key2,
  889. struct futex_pi_state **ps, int set_waiters)
  890. {
  891. struct futex_q *top_waiter = NULL;
  892. u32 curval;
  893. int ret;
  894. if (get_futex_value_locked(&curval, pifutex))
  895. return -EFAULT;
  896. /*
  897. * Find the top_waiter and determine if there are additional waiters.
  898. * If the caller intends to requeue more than 1 waiter to pifutex,
  899. * force futex_lock_pi_atomic() to set the FUTEX_WAITERS bit now,
  900. * as we have means to handle the possible fault. If not, don't set
  901. * the bit unecessarily as it will force the subsequent unlock to enter
  902. * the kernel.
  903. */
  904. top_waiter = futex_top_waiter(hb1, key1);
  905. /* There are no waiters, nothing for us to do. */
  906. if (!top_waiter)
  907. return 0;
  908. /*
  909. * Try to take the lock for top_waiter. Set the FUTEX_WAITERS bit in
  910. * the contended case or if set_waiters is 1. The pi_state is returned
  911. * in ps in contended cases.
  912. */
  913. ret = futex_lock_pi_atomic(pifutex, hb2, key2, ps, top_waiter->task,
  914. set_waiters);
  915. if (ret == 1)
  916. requeue_pi_wake_futex(top_waiter, key2);
  917. return ret;
  918. }
  919. /**
  920. * futex_requeue() - Requeue waiters from uaddr1 to uaddr2
  921. * uaddr1: source futex user address
  922. * uaddr2: target futex user address
  923. * nr_wake: number of waiters to wake (must be 1 for requeue_pi)
  924. * nr_requeue: number of waiters to requeue (0-INT_MAX)
  925. * requeue_pi: if we are attempting to requeue from a non-pi futex to a
  926. * pi futex (pi to pi requeue is not supported)
  927. *
  928. * Requeue waiters on uaddr1 to uaddr2. In the requeue_pi case, try to acquire
  929. * uaddr2 atomically on behalf of the top waiter.
  930. *
  931. * Returns:
  932. * >=0 - on success, the number of tasks requeued or woken
  933. * <0 - on error
  934. */
  935. static int futex_requeue(u32 __user *uaddr1, int fshared, u32 __user *uaddr2,
  936. int nr_wake, int nr_requeue, u32 *cmpval,
  937. int requeue_pi)
  938. {
  939. union futex_key key1 = FUTEX_KEY_INIT, key2 = FUTEX_KEY_INIT;
  940. int drop_count = 0, task_count = 0, ret;
  941. struct futex_pi_state *pi_state = NULL;
  942. struct futex_hash_bucket *hb1, *hb2;
  943. struct plist_head *head1;
  944. struct futex_q *this, *next;
  945. u32 curval2;
  946. if (requeue_pi) {
  947. /*
  948. * requeue_pi requires a pi_state, try to allocate it now
  949. * without any locks in case it fails.
  950. */
  951. if (refill_pi_state_cache())
  952. return -ENOMEM;
  953. /*
  954. * requeue_pi must wake as many tasks as it can, up to nr_wake
  955. * + nr_requeue, since it acquires the rt_mutex prior to
  956. * returning to userspace, so as to not leave the rt_mutex with
  957. * waiters and no owner. However, second and third wake-ups
  958. * cannot be predicted as they involve race conditions with the
  959. * first wake and a fault while looking up the pi_state. Both
  960. * pthread_cond_signal() and pthread_cond_broadcast() should
  961. * use nr_wake=1.
  962. */
  963. if (nr_wake != 1)
  964. return -EINVAL;
  965. }
  966. retry:
  967. if (pi_state != NULL) {
  968. /*
  969. * We will have to lookup the pi_state again, so free this one
  970. * to keep the accounting correct.
  971. */
  972. free_pi_state(pi_state);
  973. pi_state = NULL;
  974. }
  975. ret = get_futex_key(uaddr1, fshared, &key1, VERIFY_READ);
  976. if (unlikely(ret != 0))
  977. goto out;
  978. ret = get_futex_key(uaddr2, fshared, &key2,
  979. requeue_pi ? VERIFY_WRITE : VERIFY_READ);
  980. if (unlikely(ret != 0))
  981. goto out_put_key1;
  982. hb1 = hash_futex(&key1);
  983. hb2 = hash_futex(&key2);
  984. retry_private:
  985. double_lock_hb(hb1, hb2);
  986. if (likely(cmpval != NULL)) {
  987. u32 curval;
  988. ret = get_futex_value_locked(&curval, uaddr1);
  989. if (unlikely(ret)) {
  990. double_unlock_hb(hb1, hb2);
  991. ret = get_user(curval, uaddr1);
  992. if (ret)
  993. goto out_put_keys;
  994. if (!fshared)
  995. goto retry_private;
  996. put_futex_key(fshared, &key2);
  997. put_futex_key(fshared, &key1);
  998. goto retry;
  999. }
  1000. if (curval != *cmpval) {
  1001. ret = -EAGAIN;
  1002. goto out_unlock;
  1003. }
  1004. }
  1005. if (requeue_pi && (task_count - nr_wake < nr_requeue)) {
  1006. /*
  1007. * Attempt to acquire uaddr2 and wake the top waiter. If we
  1008. * intend to requeue waiters, force setting the FUTEX_WAITERS
  1009. * bit. We force this here where we are able to easily handle
  1010. * faults rather in the requeue loop below.
  1011. */
  1012. ret = futex_proxy_trylock_atomic(uaddr2, hb1, hb2, &key1,
  1013. &key2, &pi_state, nr_requeue);
  1014. /*
  1015. * At this point the top_waiter has either taken uaddr2 or is
  1016. * waiting on it. If the former, then the pi_state will not
  1017. * exist yet, look it up one more time to ensure we have a
  1018. * reference to it.
  1019. */
  1020. if (ret == 1) {
  1021. WARN_ON(pi_state);
  1022. task_count++;
  1023. ret = get_futex_value_locked(&curval2, uaddr2);
  1024. if (!ret)
  1025. ret = lookup_pi_state(curval2, hb2, &key2,
  1026. &pi_state);
  1027. }
  1028. switch (ret) {
  1029. case 0:
  1030. break;
  1031. case -EFAULT:
  1032. double_unlock_hb(hb1, hb2);
  1033. put_futex_key(fshared, &key2);
  1034. put_futex_key(fshared, &key1);
  1035. ret = get_user(curval2, uaddr2);
  1036. if (!ret)
  1037. goto retry;
  1038. goto out;
  1039. case -EAGAIN:
  1040. /* The owner was exiting, try again. */
  1041. double_unlock_hb(hb1, hb2);
  1042. put_futex_key(fshared, &key2);
  1043. put_futex_key(fshared, &key1);
  1044. cond_resched();
  1045. goto retry;
  1046. default:
  1047. goto out_unlock;
  1048. }
  1049. }
  1050. head1 = &hb1->chain;
  1051. plist_for_each_entry_safe(this, next, head1, list) {
  1052. if (task_count - nr_wake >= nr_requeue)
  1053. break;
  1054. if (!match_futex(&this->key, &key1))
  1055. continue;
  1056. WARN_ON(!requeue_pi && this->rt_waiter);
  1057. WARN_ON(requeue_pi && !this->rt_waiter);
  1058. /*
  1059. * Wake nr_wake waiters. For requeue_pi, if we acquired the
  1060. * lock, we already woke the top_waiter. If not, it will be
  1061. * woken by futex_unlock_pi().
  1062. */
  1063. if (++task_count <= nr_wake && !requeue_pi) {
  1064. wake_futex(this);
  1065. continue;
  1066. }
  1067. /*
  1068. * Requeue nr_requeue waiters and possibly one more in the case
  1069. * of requeue_pi if we couldn't acquire the lock atomically.
  1070. */
  1071. if (requeue_pi) {
  1072. /* Prepare the waiter to take the rt_mutex. */
  1073. atomic_inc(&pi_state->refcount);
  1074. this->pi_state = pi_state;
  1075. ret = rt_mutex_start_proxy_lock(&pi_state->pi_mutex,
  1076. this->rt_waiter,
  1077. this->task, 1);
  1078. if (ret == 1) {
  1079. /* We got the lock. */
  1080. requeue_pi_wake_futex(this, &key2);
  1081. continue;
  1082. } else if (ret) {
  1083. /* -EDEADLK */
  1084. this->pi_state = NULL;
  1085. free_pi_state(pi_state);
  1086. goto out_unlock;
  1087. }
  1088. }
  1089. requeue_futex(this, hb1, hb2, &key2);
  1090. drop_count++;
  1091. }
  1092. out_unlock:
  1093. double_unlock_hb(hb1, hb2);
  1094. /*
  1095. * drop_futex_key_refs() must be called outside the spinlocks. During
  1096. * the requeue we moved futex_q's from the hash bucket at key1 to the
  1097. * one at key2 and updated their key pointer. We no longer need to
  1098. * hold the references to key1.
  1099. */
  1100. while (--drop_count >= 0)
  1101. drop_futex_key_refs(&key1);
  1102. out_put_keys:
  1103. put_futex_key(fshared, &key2);
  1104. out_put_key1:
  1105. put_futex_key(fshared, &key1);
  1106. out:
  1107. if (pi_state != NULL)
  1108. free_pi_state(pi_state);
  1109. return ret ? ret : task_count;
  1110. }
  1111. /* The key must be already stored in q->key. */
  1112. static inline struct futex_hash_bucket *queue_lock(struct futex_q *q)
  1113. {
  1114. struct futex_hash_bucket *hb;
  1115. get_futex_key_refs(&q->key);
  1116. hb = hash_futex(&q->key);
  1117. q->lock_ptr = &hb->lock;
  1118. spin_lock(&hb->lock);
  1119. return hb;
  1120. }
  1121. static inline void queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  1122. {
  1123. int prio;
  1124. /*
  1125. * The priority used to register this element is
  1126. * - either the real thread-priority for the real-time threads
  1127. * (i.e. threads with a priority lower than MAX_RT_PRIO)
  1128. * - or MAX_RT_PRIO for non-RT threads.
  1129. * Thus, all RT-threads are woken first in priority order, and
  1130. * the others are woken last, in FIFO order.
  1131. */
  1132. prio = min(current->normal_prio, MAX_RT_PRIO);
  1133. plist_node_init(&q->list, prio);
  1134. #ifdef CONFIG_DEBUG_PI_LIST
  1135. q->list.plist.lock = &hb->lock;
  1136. #endif
  1137. plist_add(&q->list, &hb->chain);
  1138. q->task = current;
  1139. spin_unlock(&hb->lock);
  1140. }
  1141. static inline void
  1142. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  1143. {
  1144. spin_unlock(&hb->lock);
  1145. drop_futex_key_refs(&q->key);
  1146. }
  1147. /*
  1148. * queue_me and unqueue_me must be called as a pair, each
  1149. * exactly once. They are called with the hashed spinlock held.
  1150. */
  1151. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  1152. static int unqueue_me(struct futex_q *q)
  1153. {
  1154. spinlock_t *lock_ptr;
  1155. int ret = 0;
  1156. /* In the common case we don't take the spinlock, which is nice. */
  1157. retry:
  1158. lock_ptr = q->lock_ptr;
  1159. barrier();
  1160. if (lock_ptr != NULL) {
  1161. spin_lock(lock_ptr);
  1162. /*
  1163. * q->lock_ptr can change between reading it and
  1164. * spin_lock(), causing us to take the wrong lock. This
  1165. * corrects the race condition.
  1166. *
  1167. * Reasoning goes like this: if we have the wrong lock,
  1168. * q->lock_ptr must have changed (maybe several times)
  1169. * between reading it and the spin_lock(). It can
  1170. * change again after the spin_lock() but only if it was
  1171. * already changed before the spin_lock(). It cannot,
  1172. * however, change back to the original value. Therefore
  1173. * we can detect whether we acquired the correct lock.
  1174. */
  1175. if (unlikely(lock_ptr != q->lock_ptr)) {
  1176. spin_unlock(lock_ptr);
  1177. goto retry;
  1178. }
  1179. WARN_ON(plist_node_empty(&q->list));
  1180. plist_del(&q->list, &q->list.plist);
  1181. BUG_ON(q->pi_state);
  1182. spin_unlock(lock_ptr);
  1183. ret = 1;
  1184. }
  1185. drop_futex_key_refs(&q->key);
  1186. return ret;
  1187. }
  1188. /*
  1189. * PI futexes can not be requeued and must remove themself from the
  1190. * hash bucket. The hash bucket lock (i.e. lock_ptr) is held on entry
  1191. * and dropped here.
  1192. */
  1193. static void unqueue_me_pi(struct futex_q *q)
  1194. {
  1195. WARN_ON(plist_node_empty(&q->list));
  1196. plist_del(&q->list, &q->list.plist);
  1197. BUG_ON(!q->pi_state);
  1198. free_pi_state(q->pi_state);
  1199. q->pi_state = NULL;
  1200. spin_unlock(q->lock_ptr);
  1201. drop_futex_key_refs(&q->key);
  1202. }
  1203. /*
  1204. * Fixup the pi_state owner with the new owner.
  1205. *
  1206. * Must be called with hash bucket lock held and mm->sem held for non
  1207. * private futexes.
  1208. */
  1209. static int fixup_pi_state_owner(u32 __user *uaddr, struct futex_q *q,
  1210. struct task_struct *newowner, int fshared)
  1211. {
  1212. u32 newtid = task_pid_vnr(newowner) | FUTEX_WAITERS;
  1213. struct futex_pi_state *pi_state = q->pi_state;
  1214. struct task_struct *oldowner = pi_state->owner;
  1215. u32 uval, curval, newval;
  1216. int ret;
  1217. /* Owner died? */
  1218. if (!pi_state->owner)
  1219. newtid |= FUTEX_OWNER_DIED;
  1220. /*
  1221. * We are here either because we stole the rtmutex from the
  1222. * pending owner or we are the pending owner which failed to
  1223. * get the rtmutex. We have to replace the pending owner TID
  1224. * in the user space variable. This must be atomic as we have
  1225. * to preserve the owner died bit here.
  1226. *
  1227. * Note: We write the user space value _before_ changing the pi_state
  1228. * because we can fault here. Imagine swapped out pages or a fork
  1229. * that marked all the anonymous memory readonly for cow.
  1230. *
  1231. * Modifying pi_state _before_ the user space value would
  1232. * leave the pi_state in an inconsistent state when we fault
  1233. * here, because we need to drop the hash bucket lock to
  1234. * handle the fault. This might be observed in the PID check
  1235. * in lookup_pi_state.
  1236. */
  1237. retry:
  1238. if (get_futex_value_locked(&uval, uaddr))
  1239. goto handle_fault;
  1240. while (1) {
  1241. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  1242. curval = cmpxchg_futex_value_locked(uaddr, uval, newval);
  1243. if (curval == -EFAULT)
  1244. goto handle_fault;
  1245. if (curval == uval)
  1246. break;
  1247. uval = curval;
  1248. }
  1249. /*
  1250. * We fixed up user space. Now we need to fix the pi_state
  1251. * itself.
  1252. */
  1253. if (pi_state->owner != NULL) {
  1254. spin_lock_irq(&pi_state->owner->pi_lock);
  1255. WARN_ON(list_empty(&pi_state->list));
  1256. list_del_init(&pi_state->list);
  1257. spin_unlock_irq(&pi_state->owner->pi_lock);
  1258. }
  1259. pi_state->owner = newowner;
  1260. spin_lock_irq(&newowner->pi_lock);
  1261. WARN_ON(!list_empty(&pi_state->list));
  1262. list_add(&pi_state->list, &newowner->pi_state_list);
  1263. spin_unlock_irq(&newowner->pi_lock);
  1264. return 0;
  1265. /*
  1266. * To handle the page fault we need to drop the hash bucket
  1267. * lock here. That gives the other task (either the pending
  1268. * owner itself or the task which stole the rtmutex) the
  1269. * chance to try the fixup of the pi_state. So once we are
  1270. * back from handling the fault we need to check the pi_state
  1271. * after reacquiring the hash bucket lock and before trying to
  1272. * do another fixup. When the fixup has been done already we
  1273. * simply return.
  1274. */
  1275. handle_fault:
  1276. spin_unlock(q->lock_ptr);
  1277. ret = get_user(uval, uaddr);
  1278. spin_lock(q->lock_ptr);
  1279. /*
  1280. * Check if someone else fixed it for us:
  1281. */
  1282. if (pi_state->owner != oldowner)
  1283. return 0;
  1284. if (ret)
  1285. return ret;
  1286. goto retry;
  1287. }
  1288. /*
  1289. * In case we must use restart_block to restart a futex_wait,
  1290. * we encode in the 'flags' shared capability
  1291. */
  1292. #define FLAGS_SHARED 0x01
  1293. #define FLAGS_CLOCKRT 0x02
  1294. #define FLAGS_HAS_TIMEOUT 0x04
  1295. static long futex_wait_restart(struct restart_block *restart);
  1296. static long futex_lock_pi_restart(struct restart_block *restart);
  1297. /**
  1298. * fixup_owner() - Post lock pi_state and corner case management
  1299. * @uaddr: user address of the futex
  1300. * @fshared: whether the futex is shared (1) or not (0)
  1301. * @q: futex_q (contains pi_state and access to the rt_mutex)
  1302. * @locked: if the attempt to take the rt_mutex succeeded (1) or not (0)
  1303. *
  1304. * After attempting to lock an rt_mutex, this function is called to cleanup
  1305. * the pi_state owner as well as handle race conditions that may allow us to
  1306. * acquire the lock. Must be called with the hb lock held.
  1307. *
  1308. * Returns:
  1309. * 1 - success, lock taken
  1310. * 0 - success, lock not taken
  1311. * <0 - on error (-EFAULT)
  1312. */
  1313. static int fixup_owner(u32 __user *uaddr, int fshared, struct futex_q *q,
  1314. int locked)
  1315. {
  1316. struct task_struct *owner;
  1317. int ret = 0;
  1318. if (locked) {
  1319. /*
  1320. * Got the lock. We might not be the anticipated owner if we
  1321. * did a lock-steal - fix up the PI-state in that case:
  1322. */
  1323. if (q->pi_state->owner != current)
  1324. ret = fixup_pi_state_owner(uaddr, q, current, fshared);
  1325. goto out;
  1326. }
  1327. /*
  1328. * Catch the rare case, where the lock was released when we were on the
  1329. * way back before we locked the hash bucket.
  1330. */
  1331. if (q->pi_state->owner == current) {
  1332. /*
  1333. * Try to get the rt_mutex now. This might fail as some other
  1334. * task acquired the rt_mutex after we removed ourself from the
  1335. * rt_mutex waiters list.
  1336. */
  1337. if (rt_mutex_trylock(&q->pi_state->pi_mutex)) {
  1338. locked = 1;
  1339. goto out;
  1340. }
  1341. /*
  1342. * pi_state is incorrect, some other task did a lock steal and
  1343. * we returned due to timeout or signal without taking the
  1344. * rt_mutex. Too late. We can access the rt_mutex_owner without
  1345. * locking, as the other task is now blocked on the hash bucket
  1346. * lock. Fix the state up.
  1347. */
  1348. owner = rt_mutex_owner(&q->pi_state->pi_mutex);
  1349. ret = fixup_pi_state_owner(uaddr, q, owner, fshared);
  1350. goto out;
  1351. }
  1352. /*
  1353. * Paranoia check. If we did not take the lock, then we should not be
  1354. * the owner, nor the pending owner, of the rt_mutex.
  1355. */
  1356. if (rt_mutex_owner(&q->pi_state->pi_mutex) == current)
  1357. printk(KERN_ERR "fixup_owner: ret = %d pi-mutex: %p "
  1358. "pi-state %p\n", ret,
  1359. q->pi_state->pi_mutex.owner,
  1360. q->pi_state->owner);
  1361. out:
  1362. return ret ? ret : locked;
  1363. }
  1364. /**
  1365. * futex_wait_queue_me() - queue_me() and wait for wakeup, timeout, or signal
  1366. * @hb: the futex hash bucket, must be locked by the caller
  1367. * @q: the futex_q to queue up on
  1368. * @timeout: the prepared hrtimer_sleeper, or null for no timeout
  1369. */
  1370. static void futex_wait_queue_me(struct futex_hash_bucket *hb, struct futex_q *q,
  1371. struct hrtimer_sleeper *timeout)
  1372. {
  1373. queue_me(q, hb);
  1374. /*
  1375. * There might have been scheduling since the queue_me(), as we
  1376. * cannot hold a spinlock across the get_user() in case it
  1377. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  1378. * queueing ourselves into the futex hash. This code thus has to
  1379. * rely on the futex_wake() code removing us from hash when it
  1380. * wakes us up.
  1381. */
  1382. set_current_state(TASK_INTERRUPTIBLE);
  1383. /* Arm the timer */
  1384. if (timeout) {
  1385. hrtimer_start_expires(&timeout->timer, HRTIMER_MODE_ABS);
  1386. if (!hrtimer_active(&timeout->timer))
  1387. timeout->task = NULL;
  1388. }
  1389. /*
  1390. * !plist_node_empty() is safe here without any lock.
  1391. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  1392. */
  1393. if (likely(!plist_node_empty(&q->list))) {
  1394. /*
  1395. * If the timer has already expired, current will already be
  1396. * flagged for rescheduling. Only call schedule if there
  1397. * is no timeout, or if it has yet to expire.
  1398. */
  1399. if (!timeout || timeout->task)
  1400. schedule();
  1401. }
  1402. __set_current_state(TASK_RUNNING);
  1403. }
  1404. /**
  1405. * futex_wait_setup() - Prepare to wait on a futex
  1406. * @uaddr: the futex userspace address
  1407. * @val: the expected value
  1408. * @fshared: whether the futex is shared (1) or not (0)
  1409. * @q: the associated futex_q
  1410. * @hb: storage for hash_bucket pointer to be returned to caller
  1411. *
  1412. * Setup the futex_q and locate the hash_bucket. Get the futex value and
  1413. * compare it with the expected value. Handle atomic faults internally.
  1414. * Return with the hb lock held and a q.key reference on success, and unlocked
  1415. * with no q.key reference on failure.
  1416. *
  1417. * Returns:
  1418. * 0 - uaddr contains val and hb has been locked
  1419. * <1 - -EFAULT or -EWOULDBLOCK (uaddr does not contain val) and hb is unlcoked
  1420. */
  1421. static int futex_wait_setup(u32 __user *uaddr, u32 val, int fshared,
  1422. struct futex_q *q, struct futex_hash_bucket **hb)
  1423. {
  1424. u32 uval;
  1425. int ret;
  1426. /*
  1427. * Access the page AFTER the hash-bucket is locked.
  1428. * Order is important:
  1429. *
  1430. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  1431. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  1432. *
  1433. * The basic logical guarantee of a futex is that it blocks ONLY
  1434. * if cond(var) is known to be true at the time of blocking, for
  1435. * any cond. If we queued after testing *uaddr, that would open
  1436. * a race condition where we could block indefinitely with
  1437. * cond(var) false, which would violate the guarantee.
  1438. *
  1439. * A consequence is that futex_wait() can return zero and absorb
  1440. * a wakeup when *uaddr != val on entry to the syscall. This is
  1441. * rare, but normal.
  1442. */
  1443. retry:
  1444. q->key = FUTEX_KEY_INIT;
  1445. ret = get_futex_key(uaddr, fshared, &q->key, VERIFY_READ);
  1446. if (unlikely(ret != 0))
  1447. return ret;
  1448. retry_private:
  1449. *hb = queue_lock(q);
  1450. ret = get_futex_value_locked(&uval, uaddr);
  1451. if (ret) {
  1452. queue_unlock(q, *hb);
  1453. ret = get_user(uval, uaddr);
  1454. if (ret)
  1455. goto out;
  1456. if (!fshared)
  1457. goto retry_private;
  1458. put_futex_key(fshared, &q->key);
  1459. goto retry;
  1460. }
  1461. if (uval != val) {
  1462. queue_unlock(q, *hb);
  1463. ret = -EWOULDBLOCK;
  1464. }
  1465. out:
  1466. if (ret)
  1467. put_futex_key(fshared, &q->key);
  1468. return ret;
  1469. }
  1470. static int futex_wait(u32 __user *uaddr, int fshared,
  1471. u32 val, ktime_t *abs_time, u32 bitset, int clockrt)
  1472. {
  1473. struct hrtimer_sleeper timeout, *to = NULL;
  1474. struct restart_block *restart;
  1475. struct futex_hash_bucket *hb;
  1476. struct futex_q q;
  1477. int ret;
  1478. if (!bitset)
  1479. return -EINVAL;
  1480. q.pi_state = NULL;
  1481. q.bitset = bitset;
  1482. q.rt_waiter = NULL;
  1483. if (abs_time) {
  1484. to = &timeout;
  1485. hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
  1486. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1487. hrtimer_init_sleeper(to, current);
  1488. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  1489. current->timer_slack_ns);
  1490. }
  1491. /* Prepare to wait on uaddr. */
  1492. ret = futex_wait_setup(uaddr, val, fshared, &q, &hb);
  1493. if (ret)
  1494. goto out;
  1495. /* queue_me and wait for wakeup, timeout, or a signal. */
  1496. futex_wait_queue_me(hb, &q, to);
  1497. /* If we were woken (and unqueued), we succeeded, whatever. */
  1498. ret = 0;
  1499. if (!unqueue_me(&q))
  1500. goto out_put_key;
  1501. ret = -ETIMEDOUT;
  1502. if (to && !to->task)
  1503. goto out_put_key;
  1504. /*
  1505. * We expect signal_pending(current), but another thread may
  1506. * have handled it for us already.
  1507. */
  1508. ret = -ERESTARTSYS;
  1509. if (!abs_time)
  1510. goto out_put_key;
  1511. restart = &current_thread_info()->restart_block;
  1512. restart->fn = futex_wait_restart;
  1513. restart->futex.uaddr = (u32 *)uaddr;
  1514. restart->futex.val = val;
  1515. restart->futex.time = abs_time->tv64;
  1516. restart->futex.bitset = bitset;
  1517. restart->futex.flags = FLAGS_HAS_TIMEOUT;
  1518. if (fshared)
  1519. restart->futex.flags |= FLAGS_SHARED;
  1520. if (clockrt)
  1521. restart->futex.flags |= FLAGS_CLOCKRT;
  1522. ret = -ERESTART_RESTARTBLOCK;
  1523. out_put_key:
  1524. put_futex_key(fshared, &q.key);
  1525. out:
  1526. if (to) {
  1527. hrtimer_cancel(&to->timer);
  1528. destroy_hrtimer_on_stack(&to->timer);
  1529. }
  1530. return ret;
  1531. }
  1532. static long futex_wait_restart(struct restart_block *restart)
  1533. {
  1534. u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
  1535. int fshared = 0;
  1536. ktime_t t, *tp = NULL;
  1537. if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
  1538. t.tv64 = restart->futex.time;
  1539. tp = &t;
  1540. }
  1541. restart->fn = do_no_restart_syscall;
  1542. if (restart->futex.flags & FLAGS_SHARED)
  1543. fshared = 1;
  1544. return (long)futex_wait(uaddr, fshared, restart->futex.val, tp,
  1545. restart->futex.bitset,
  1546. restart->futex.flags & FLAGS_CLOCKRT);
  1547. }
  1548. /*
  1549. * Userspace tried a 0 -> TID atomic transition of the futex value
  1550. * and failed. The kernel side here does the whole locking operation:
  1551. * if there are waiters then it will block, it does PI, etc. (Due to
  1552. * races the kernel might see a 0 value of the futex too.)
  1553. */
  1554. static int futex_lock_pi(u32 __user *uaddr, int fshared,
  1555. int detect, ktime_t *time, int trylock)
  1556. {
  1557. struct hrtimer_sleeper timeout, *to = NULL;
  1558. struct futex_hash_bucket *hb;
  1559. u32 uval;
  1560. struct futex_q q;
  1561. int res, ret;
  1562. if (refill_pi_state_cache())
  1563. return -ENOMEM;
  1564. if (time) {
  1565. to = &timeout;
  1566. hrtimer_init_on_stack(&to->timer, CLOCK_REALTIME,
  1567. HRTIMER_MODE_ABS);
  1568. hrtimer_init_sleeper(to, current);
  1569. hrtimer_set_expires(&to->timer, *time);
  1570. }
  1571. q.pi_state = NULL;
  1572. q.rt_waiter = NULL;
  1573. retry:
  1574. q.key = FUTEX_KEY_INIT;
  1575. ret = get_futex_key(uaddr, fshared, &q.key, VERIFY_WRITE);
  1576. if (unlikely(ret != 0))
  1577. goto out;
  1578. retry_private:
  1579. hb = queue_lock(&q);
  1580. ret = futex_lock_pi_atomic(uaddr, hb, &q.key, &q.pi_state, current, 0);
  1581. if (unlikely(ret)) {
  1582. switch (ret) {
  1583. case 1:
  1584. /* We got the lock. */
  1585. ret = 0;
  1586. goto out_unlock_put_key;
  1587. case -EFAULT:
  1588. goto uaddr_faulted;
  1589. case -EAGAIN:
  1590. /*
  1591. * Task is exiting and we just wait for the
  1592. * exit to complete.
  1593. */
  1594. queue_unlock(&q, hb);
  1595. put_futex_key(fshared, &q.key);
  1596. cond_resched();
  1597. goto retry;
  1598. default:
  1599. goto out_unlock_put_key;
  1600. }
  1601. }
  1602. /*
  1603. * Only actually queue now that the atomic ops are done:
  1604. */
  1605. queue_me(&q, hb);
  1606. WARN_ON(!q.pi_state);
  1607. /*
  1608. * Block on the PI mutex:
  1609. */
  1610. if (!trylock)
  1611. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1612. else {
  1613. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1614. /* Fixup the trylock return value: */
  1615. ret = ret ? 0 : -EWOULDBLOCK;
  1616. }
  1617. spin_lock(q.lock_ptr);
  1618. /*
  1619. * Fixup the pi_state owner and possibly acquire the lock if we
  1620. * haven't already.
  1621. */
  1622. res = fixup_owner(uaddr, fshared, &q, !ret);
  1623. /*
  1624. * If fixup_owner() returned an error, proprogate that. If it acquired
  1625. * the lock, clear our -ETIMEDOUT or -EINTR.
  1626. */
  1627. if (res)
  1628. ret = (res < 0) ? res : 0;
  1629. /*
  1630. * If fixup_owner() faulted and was unable to handle the fault, unlock
  1631. * it and return the fault to userspace.
  1632. */
  1633. if (ret && (rt_mutex_owner(&q.pi_state->pi_mutex) == current))
  1634. rt_mutex_unlock(&q.pi_state->pi_mutex);
  1635. /* Unqueue and drop the lock */
  1636. unqueue_me_pi(&q);
  1637. goto out;
  1638. out_unlock_put_key:
  1639. queue_unlock(&q, hb);
  1640. out_put_key:
  1641. put_futex_key(fshared, &q.key);
  1642. out:
  1643. if (to)
  1644. destroy_hrtimer_on_stack(&to->timer);
  1645. return ret != -EINTR ? ret : -ERESTARTNOINTR;
  1646. uaddr_faulted:
  1647. /*
  1648. * We have to r/w *(int __user *)uaddr, and we have to modify it
  1649. * atomically. Therefore, if we continue to fault after get_user()
  1650. * below, we need to handle the fault ourselves, while still holding
  1651. * the mmap_sem. This can occur if the uaddr is under contention as
  1652. * we have to drop the mmap_sem in order to call get_user().
  1653. */
  1654. queue_unlock(&q, hb);
  1655. ret = get_user(uval, uaddr);
  1656. if (ret)
  1657. goto out_put_key;
  1658. if (!fshared)
  1659. goto retry_private;
  1660. put_futex_key(fshared, &q.key);
  1661. goto retry;
  1662. }
  1663. static long futex_lock_pi_restart(struct restart_block *restart)
  1664. {
  1665. u32 __user *uaddr = (u32 __user *)restart->futex.uaddr;
  1666. ktime_t t, *tp = NULL;
  1667. int fshared = restart->futex.flags & FLAGS_SHARED;
  1668. if (restart->futex.flags & FLAGS_HAS_TIMEOUT) {
  1669. t.tv64 = restart->futex.time;
  1670. tp = &t;
  1671. }
  1672. restart->fn = do_no_restart_syscall;
  1673. return (long)futex_lock_pi(uaddr, fshared, restart->futex.val, tp, 0);
  1674. }
  1675. /*
  1676. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1677. * This is the in-kernel slowpath: we look up the PI state (if any),
  1678. * and do the rt-mutex unlock.
  1679. */
  1680. static int futex_unlock_pi(u32 __user *uaddr, int fshared)
  1681. {
  1682. struct futex_hash_bucket *hb;
  1683. struct futex_q *this, *next;
  1684. u32 uval;
  1685. struct plist_head *head;
  1686. union futex_key key = FUTEX_KEY_INIT;
  1687. int ret;
  1688. retry:
  1689. if (get_user(uval, uaddr))
  1690. return -EFAULT;
  1691. /*
  1692. * We release only a lock we actually own:
  1693. */
  1694. if ((uval & FUTEX_TID_MASK) != task_pid_vnr(current))
  1695. return -EPERM;
  1696. ret = get_futex_key(uaddr, fshared, &key, VERIFY_WRITE);
  1697. if (unlikely(ret != 0))
  1698. goto out;
  1699. hb = hash_futex(&key);
  1700. spin_lock(&hb->lock);
  1701. /*
  1702. * To avoid races, try to do the TID -> 0 atomic transition
  1703. * again. If it succeeds then we can return without waking
  1704. * anyone else up:
  1705. */
  1706. if (!(uval & FUTEX_OWNER_DIED))
  1707. uval = cmpxchg_futex_value_locked(uaddr, task_pid_vnr(current), 0);
  1708. if (unlikely(uval == -EFAULT))
  1709. goto pi_faulted;
  1710. /*
  1711. * Rare case: we managed to release the lock atomically,
  1712. * no need to wake anyone else up:
  1713. */
  1714. if (unlikely(uval == task_pid_vnr(current)))
  1715. goto out_unlock;
  1716. /*
  1717. * Ok, other tasks may need to be woken up - check waiters
  1718. * and do the wakeup if necessary:
  1719. */
  1720. head = &hb->chain;
  1721. plist_for_each_entry_safe(this, next, head, list) {
  1722. if (!match_futex (&this->key, &key))
  1723. continue;
  1724. ret = wake_futex_pi(uaddr, uval, this);
  1725. /*
  1726. * The atomic access to the futex value
  1727. * generated a pagefault, so retry the
  1728. * user-access and the wakeup:
  1729. */
  1730. if (ret == -EFAULT)
  1731. goto pi_faulted;
  1732. goto out_unlock;
  1733. }
  1734. /*
  1735. * No waiters - kernel unlocks the futex:
  1736. */
  1737. if (!(uval & FUTEX_OWNER_DIED)) {
  1738. ret = unlock_futex_pi(uaddr, uval);
  1739. if (ret == -EFAULT)
  1740. goto pi_faulted;
  1741. }
  1742. out_unlock:
  1743. spin_unlock(&hb->lock);
  1744. put_futex_key(fshared, &key);
  1745. out:
  1746. return ret;
  1747. pi_faulted:
  1748. /*
  1749. * We have to r/w *(int __user *)uaddr, and we have to modify it
  1750. * atomically. Therefore, if we continue to fault after get_user()
  1751. * below, we need to handle the fault ourselves, while still holding
  1752. * the mmap_sem. This can occur if the uaddr is under contention as
  1753. * we have to drop the mmap_sem in order to call get_user().
  1754. */
  1755. spin_unlock(&hb->lock);
  1756. put_futex_key(fshared, &key);
  1757. ret = get_user(uval, uaddr);
  1758. if (!ret)
  1759. goto retry;
  1760. return ret;
  1761. }
  1762. /**
  1763. * handle_early_requeue_pi_wakeup() - Detect early wakeup on the initial futex
  1764. * @hb: the hash_bucket futex_q was original enqueued on
  1765. * @q: the futex_q woken while waiting to be requeued
  1766. * @key2: the futex_key of the requeue target futex
  1767. * @timeout: the timeout associated with the wait (NULL if none)
  1768. *
  1769. * Detect if the task was woken on the initial futex as opposed to the requeue
  1770. * target futex. If so, determine if it was a timeout or a signal that caused
  1771. * the wakeup and return the appropriate error code to the caller. Must be
  1772. * called with the hb lock held.
  1773. *
  1774. * Returns
  1775. * 0 - no early wakeup detected
  1776. * <0 - -ETIMEDOUT or -ERESTARTNOINTR
  1777. */
  1778. static inline
  1779. int handle_early_requeue_pi_wakeup(struct futex_hash_bucket *hb,
  1780. struct futex_q *q, union futex_key *key2,
  1781. struct hrtimer_sleeper *timeout)
  1782. {
  1783. int ret = 0;
  1784. /*
  1785. * With the hb lock held, we avoid races while we process the wakeup.
  1786. * We only need to hold hb (and not hb2) to ensure atomicity as the
  1787. * wakeup code can't change q.key from uaddr to uaddr2 if we hold hb.
  1788. * It can't be requeued from uaddr2 to something else since we don't
  1789. * support a PI aware source futex for requeue.
  1790. */
  1791. if (!match_futex(&q->key, key2)) {
  1792. WARN_ON(q->lock_ptr && (&hb->lock != q->lock_ptr));
  1793. /*
  1794. * We were woken prior to requeue by a timeout or a signal.
  1795. * Unqueue the futex_q and determine which it was.
  1796. */
  1797. plist_del(&q->list, &q->list.plist);
  1798. drop_futex_key_refs(&q->key);
  1799. if (timeout && !timeout->task)
  1800. ret = -ETIMEDOUT;
  1801. else
  1802. ret = -ERESTARTNOINTR;
  1803. }
  1804. return ret;
  1805. }
  1806. /**
  1807. * futex_wait_requeue_pi() - Wait on uaddr and take uaddr2
  1808. * @uaddr: the futex we initialyl wait on (non-pi)
  1809. * @fshared: whether the futexes are shared (1) or not (0). They must be
  1810. * the same type, no requeueing from private to shared, etc.
  1811. * @val: the expected value of uaddr
  1812. * @abs_time: absolute timeout
  1813. * @bitset: 32 bit wakeup bitset set by userspace, defaults to all.
  1814. * @clockrt: whether to use CLOCK_REALTIME (1) or CLOCK_MONOTONIC (0)
  1815. * @uaddr2: the pi futex we will take prior to returning to user-space
  1816. *
  1817. * The caller will wait on uaddr and will be requeued by futex_requeue() to
  1818. * uaddr2 which must be PI aware. Normal wakeup will wake on uaddr2 and
  1819. * complete the acquisition of the rt_mutex prior to returning to userspace.
  1820. * This ensures the rt_mutex maintains an owner when it has waiters; without
  1821. * one, the pi logic wouldn't know which task to boost/deboost, if there was a
  1822. * need to.
  1823. *
  1824. * We call schedule in futex_wait_queue_me() when we enqueue and return there
  1825. * via the following:
  1826. * 1) wakeup on uaddr2 after an atomic lock acquisition by futex_requeue()
  1827. * 2) wakeup on uaddr2 after a requeue and subsequent unlock
  1828. * 3) signal (before or after requeue)
  1829. * 4) timeout (before or after requeue)
  1830. *
  1831. * If 3, we setup a restart_block with futex_wait_requeue_pi() as the function.
  1832. *
  1833. * If 2, we may then block on trying to take the rt_mutex and return via:
  1834. * 5) successful lock
  1835. * 6) signal
  1836. * 7) timeout
  1837. * 8) other lock acquisition failure
  1838. *
  1839. * If 6, we setup a restart_block with futex_lock_pi() as the function.
  1840. *
  1841. * If 4 or 7, we cleanup and return with -ETIMEDOUT.
  1842. *
  1843. * Returns:
  1844. * 0 - On success
  1845. * <0 - On error
  1846. */
  1847. static int futex_wait_requeue_pi(u32 __user *uaddr, int fshared,
  1848. u32 val, ktime_t *abs_time, u32 bitset,
  1849. int clockrt, u32 __user *uaddr2)
  1850. {
  1851. struct hrtimer_sleeper timeout, *to = NULL;
  1852. struct rt_mutex_waiter rt_waiter;
  1853. struct rt_mutex *pi_mutex = NULL;
  1854. struct restart_block *restart;
  1855. struct futex_hash_bucket *hb;
  1856. union futex_key key2;
  1857. struct futex_q q;
  1858. int res, ret;
  1859. u32 uval;
  1860. if (!bitset)
  1861. return -EINVAL;
  1862. if (abs_time) {
  1863. to = &timeout;
  1864. hrtimer_init_on_stack(&to->timer, clockrt ? CLOCK_REALTIME :
  1865. CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  1866. hrtimer_init_sleeper(to, current);
  1867. hrtimer_set_expires_range_ns(&to->timer, *abs_time,
  1868. current->timer_slack_ns);
  1869. }
  1870. /*
  1871. * The waiter is allocated on our stack, manipulated by the requeue
  1872. * code while we sleep on uaddr.
  1873. */
  1874. debug_rt_mutex_init_waiter(&rt_waiter);
  1875. rt_waiter.task = NULL;
  1876. q.pi_state = NULL;
  1877. q.bitset = bitset;
  1878. q.rt_waiter = &rt_waiter;
  1879. key2 = FUTEX_KEY_INIT;
  1880. ret = get_futex_key(uaddr2, fshared, &key2, VERIFY_WRITE);
  1881. if (unlikely(ret != 0))
  1882. goto out;
  1883. /* Prepare to wait on uaddr. */
  1884. ret = futex_wait_setup(uaddr, val, fshared, &q, &hb);
  1885. if (ret)
  1886. goto out_key2;
  1887. /* Queue the futex_q, drop the hb lock, wait for wakeup. */
  1888. futex_wait_queue_me(hb, &q, to);
  1889. spin_lock(&hb->lock);
  1890. ret = handle_early_requeue_pi_wakeup(hb, &q, &key2, to);
  1891. spin_unlock(&hb->lock);
  1892. if (ret)
  1893. goto out_put_keys;
  1894. /*
  1895. * In order for us to be here, we know our q.key == key2, and since
  1896. * we took the hb->lock above, we also know that futex_requeue() has
  1897. * completed and we no longer have to concern ourselves with a wakeup
  1898. * race with the atomic proxy lock acquition by the requeue code.
  1899. */
  1900. /* Check if the requeue code acquired the second futex for us. */
  1901. if (!q.rt_waiter) {
  1902. /*
  1903. * Got the lock. We might not be the anticipated owner if we
  1904. * did a lock-steal - fix up the PI-state in that case.
  1905. */
  1906. if (q.pi_state && (q.pi_state->owner != current)) {
  1907. spin_lock(q.lock_ptr);
  1908. ret = fixup_pi_state_owner(uaddr2, &q, current,
  1909. fshared);
  1910. spin_unlock(q.lock_ptr);
  1911. }
  1912. } else {
  1913. /*
  1914. * We have been woken up by futex_unlock_pi(), a timeout, or a
  1915. * signal. futex_unlock_pi() will not destroy the lock_ptr nor
  1916. * the pi_state.
  1917. */
  1918. WARN_ON(!&q.pi_state);
  1919. pi_mutex = &q.pi_state->pi_mutex;
  1920. ret = rt_mutex_finish_proxy_lock(pi_mutex, to, &rt_waiter, 1);
  1921. debug_rt_mutex_free_waiter(&rt_waiter);
  1922. spin_lock(q.lock_ptr);
  1923. /*
  1924. * Fixup the pi_state owner and possibly acquire the lock if we
  1925. * haven't already.
  1926. */
  1927. res = fixup_owner(uaddr2, fshared, &q, !ret);
  1928. /*
  1929. * If fixup_owner() returned an error, proprogate that. If it
  1930. * acquired the lock, clear our -ETIMEDOUT or -EINTR.
  1931. */
  1932. if (res)
  1933. ret = (res < 0) ? res : 0;
  1934. /* Unqueue and drop the lock. */
  1935. unqueue_me_pi(&q);
  1936. }
  1937. /*
  1938. * If fixup_pi_state_owner() faulted and was unable to handle the
  1939. * fault, unlock the rt_mutex and return the fault to userspace.
  1940. */
  1941. if (ret == -EFAULT) {
  1942. if (rt_mutex_owner(pi_mutex) == current)
  1943. rt_mutex_unlock(pi_mutex);
  1944. } else if (ret == -EINTR) {
  1945. ret = -EFAULT;
  1946. if (get_user(uval, uaddr2))
  1947. goto out_put_keys;
  1948. /*
  1949. * We've already been requeued, so restart by calling
  1950. * futex_lock_pi() directly, rather then returning to this
  1951. * function.
  1952. */
  1953. ret = -ERESTART_RESTARTBLOCK;
  1954. restart = &current_thread_info()->restart_block;
  1955. restart->fn = futex_lock_pi_restart;
  1956. restart->futex.uaddr = (u32 *)uaddr2;
  1957. restart->futex.val = uval;
  1958. restart->futex.flags = 0;
  1959. if (abs_time) {
  1960. restart->futex.flags |= FLAGS_HAS_TIMEOUT;
  1961. restart->futex.time = abs_time->tv64;
  1962. }
  1963. if (fshared)
  1964. restart->futex.flags |= FLAGS_SHARED;
  1965. if (clockrt)
  1966. restart->futex.flags |= FLAGS_CLOCKRT;
  1967. }
  1968. out_put_keys:
  1969. put_futex_key(fshared, &q.key);
  1970. out_key2:
  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);