futex.c 45 KB

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