futex.c 51 KB

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