futex.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  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 futex_hash_bucket *hb;
  369. struct list_head *next, *head = &curr->pi_state_list;
  370. struct futex_pi_state *pi_state;
  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 themselfs
  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. spin_unlock_irq(&curr->pi_lock);
  383. hb = hash_futex(&key);
  384. spin_lock(&hb->lock);
  385. spin_lock_irq(&curr->pi_lock);
  386. if (head->next != next) {
  387. spin_unlock(&hb->lock);
  388. continue;
  389. }
  390. list_del_init(&pi_state->list);
  391. WARN_ON(pi_state->owner != curr);
  392. pi_state->owner = NULL;
  393. spin_unlock_irq(&curr->pi_lock);
  394. rt_mutex_unlock(&pi_state->pi_mutex);
  395. spin_unlock(&hb->lock);
  396. spin_lock_irq(&curr->pi_lock);
  397. }
  398. spin_unlock_irq(&curr->pi_lock);
  399. }
  400. static int
  401. lookup_pi_state(u32 uval, struct futex_hash_bucket *hb, struct futex_q *me)
  402. {
  403. struct futex_pi_state *pi_state = NULL;
  404. struct futex_q *this, *next;
  405. struct list_head *head;
  406. struct task_struct *p;
  407. pid_t pid;
  408. head = &hb->chain;
  409. list_for_each_entry_safe(this, next, head, list) {
  410. if (match_futex (&this->key, &me->key)) {
  411. /*
  412. * Another waiter already exists - bump up
  413. * the refcount and return its pi_state:
  414. */
  415. pi_state = this->pi_state;
  416. atomic_inc(&pi_state->refcount);
  417. me->pi_state = pi_state;
  418. return 0;
  419. }
  420. }
  421. /*
  422. * We are the first waiter - try to look up the real owner and
  423. * attach the new pi_state to it:
  424. */
  425. pid = uval & FUTEX_TID_MASK;
  426. p = futex_find_get_task(pid);
  427. if (!p)
  428. return -ESRCH;
  429. pi_state = alloc_pi_state();
  430. /*
  431. * Initialize the pi_mutex in locked state and make 'p'
  432. * the owner of it:
  433. */
  434. rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
  435. /* Store the key for possible exit cleanups: */
  436. pi_state->key = me->key;
  437. spin_lock_irq(&p->pi_lock);
  438. list_add(&pi_state->list, &p->pi_state_list);
  439. pi_state->owner = p;
  440. spin_unlock_irq(&p->pi_lock);
  441. put_task_struct(p);
  442. me->pi_state = pi_state;
  443. return 0;
  444. }
  445. /*
  446. * The hash bucket lock must be held when this is called.
  447. * Afterwards, the futex_q must not be accessed.
  448. */
  449. static void wake_futex(struct futex_q *q)
  450. {
  451. list_del_init(&q->list);
  452. if (q->filp)
  453. send_sigio(&q->filp->f_owner, q->fd, POLL_IN);
  454. /*
  455. * The lock in wake_up_all() is a crucial memory barrier after the
  456. * list_del_init() and also before assigning to q->lock_ptr.
  457. */
  458. wake_up_all(&q->waiters);
  459. /*
  460. * The waiting task can free the futex_q as soon as this is written,
  461. * without taking any locks. This must come last.
  462. *
  463. * A memory barrier is required here to prevent the following store
  464. * to lock_ptr from getting ahead of the wakeup. Clearing the lock
  465. * at the end of wake_up_all() does not prevent this store from
  466. * moving.
  467. */
  468. wmb();
  469. q->lock_ptr = NULL;
  470. }
  471. static int wake_futex_pi(u32 __user *uaddr, u32 uval, struct futex_q *this)
  472. {
  473. struct task_struct *new_owner;
  474. struct futex_pi_state *pi_state = this->pi_state;
  475. u32 curval, newval;
  476. if (!pi_state)
  477. return -EINVAL;
  478. new_owner = rt_mutex_next_owner(&pi_state->pi_mutex);
  479. /*
  480. * This happens when we have stolen the lock and the original
  481. * pending owner did not enqueue itself back on the rt_mutex.
  482. * Thats not a tragedy. We know that way, that a lock waiter
  483. * is on the fly. We make the futex_q waiter the pending owner.
  484. */
  485. if (!new_owner)
  486. new_owner = this->task;
  487. /*
  488. * We pass it to the next owner. (The WAITERS bit is always
  489. * kept enabled while there is PI state around. We must also
  490. * preserve the owner died bit.)
  491. */
  492. newval = (uval & FUTEX_OWNER_DIED) | FUTEX_WAITERS | new_owner->pid;
  493. inc_preempt_count();
  494. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  495. dec_preempt_count();
  496. if (curval == -EFAULT)
  497. return -EFAULT;
  498. if (curval != uval)
  499. return -EINVAL;
  500. list_del_init(&pi_state->owner->pi_state_list);
  501. list_add(&pi_state->list, &new_owner->pi_state_list);
  502. pi_state->owner = new_owner;
  503. rt_mutex_unlock(&pi_state->pi_mutex);
  504. return 0;
  505. }
  506. static int unlock_futex_pi(u32 __user *uaddr, u32 uval)
  507. {
  508. u32 oldval;
  509. /*
  510. * There is no waiter, so we unlock the futex. The owner died
  511. * bit has not to be preserved here. We are the owner:
  512. */
  513. inc_preempt_count();
  514. oldval = futex_atomic_cmpxchg_inatomic(uaddr, uval, 0);
  515. dec_preempt_count();
  516. if (oldval == -EFAULT)
  517. return oldval;
  518. if (oldval != uval)
  519. return -EAGAIN;
  520. return 0;
  521. }
  522. /*
  523. * Wake up all waiters hashed on the physical page that is mapped
  524. * to this virtual address:
  525. */
  526. static int futex_wake(u32 __user *uaddr, int nr_wake)
  527. {
  528. struct futex_hash_bucket *hb;
  529. struct futex_q *this, *next;
  530. struct list_head *head;
  531. union futex_key key;
  532. int ret;
  533. down_read(&current->mm->mmap_sem);
  534. ret = get_futex_key(uaddr, &key);
  535. if (unlikely(ret != 0))
  536. goto out;
  537. hb = hash_futex(&key);
  538. spin_lock(&hb->lock);
  539. head = &hb->chain;
  540. list_for_each_entry_safe(this, next, head, list) {
  541. if (match_futex (&this->key, &key)) {
  542. if (this->pi_state)
  543. return -EINVAL;
  544. wake_futex(this);
  545. if (++ret >= nr_wake)
  546. break;
  547. }
  548. }
  549. spin_unlock(&hb->lock);
  550. out:
  551. up_read(&current->mm->mmap_sem);
  552. return ret;
  553. }
  554. /*
  555. * Wake up all waiters hashed on the physical page that is mapped
  556. * to this virtual address:
  557. */
  558. static int
  559. futex_wake_op(u32 __user *uaddr1, u32 __user *uaddr2,
  560. int nr_wake, int nr_wake2, int op)
  561. {
  562. union futex_key key1, key2;
  563. struct futex_hash_bucket *hb1, *hb2;
  564. struct list_head *head;
  565. struct futex_q *this, *next;
  566. int ret, op_ret, attempt = 0;
  567. retryfull:
  568. down_read(&current->mm->mmap_sem);
  569. ret = get_futex_key(uaddr1, &key1);
  570. if (unlikely(ret != 0))
  571. goto out;
  572. ret = get_futex_key(uaddr2, &key2);
  573. if (unlikely(ret != 0))
  574. goto out;
  575. hb1 = hash_futex(&key1);
  576. hb2 = hash_futex(&key2);
  577. retry:
  578. if (hb1 < hb2)
  579. spin_lock(&hb1->lock);
  580. spin_lock(&hb2->lock);
  581. if (hb1 > hb2)
  582. spin_lock(&hb1->lock);
  583. op_ret = futex_atomic_op_inuser(op, uaddr2);
  584. if (unlikely(op_ret < 0)) {
  585. u32 dummy;
  586. spin_unlock(&hb1->lock);
  587. if (hb1 != hb2)
  588. spin_unlock(&hb2->lock);
  589. #ifndef CONFIG_MMU
  590. /*
  591. * we don't get EFAULT from MMU faults if we don't have an MMU,
  592. * but we might get them from range checking
  593. */
  594. ret = op_ret;
  595. goto out;
  596. #endif
  597. if (unlikely(op_ret != -EFAULT)) {
  598. ret = op_ret;
  599. goto out;
  600. }
  601. /*
  602. * futex_atomic_op_inuser needs to both read and write
  603. * *(int __user *)uaddr2, but we can't modify it
  604. * non-atomically. Therefore, if get_user below is not
  605. * enough, we need to handle the fault ourselves, while
  606. * still holding the mmap_sem.
  607. */
  608. if (attempt++) {
  609. if (futex_handle_fault((unsigned long)uaddr2,
  610. attempt))
  611. goto out;
  612. goto retry;
  613. }
  614. /*
  615. * If we would have faulted, release mmap_sem,
  616. * fault it in and start all over again.
  617. */
  618. up_read(&current->mm->mmap_sem);
  619. ret = get_user(dummy, uaddr2);
  620. if (ret)
  621. return ret;
  622. goto retryfull;
  623. }
  624. head = &hb1->chain;
  625. list_for_each_entry_safe(this, next, head, list) {
  626. if (match_futex (&this->key, &key1)) {
  627. wake_futex(this);
  628. if (++ret >= nr_wake)
  629. break;
  630. }
  631. }
  632. if (op_ret > 0) {
  633. head = &hb2->chain;
  634. op_ret = 0;
  635. list_for_each_entry_safe(this, next, head, list) {
  636. if (match_futex (&this->key, &key2)) {
  637. wake_futex(this);
  638. if (++op_ret >= nr_wake2)
  639. break;
  640. }
  641. }
  642. ret += op_ret;
  643. }
  644. spin_unlock(&hb1->lock);
  645. if (hb1 != hb2)
  646. spin_unlock(&hb2->lock);
  647. out:
  648. up_read(&current->mm->mmap_sem);
  649. return ret;
  650. }
  651. /*
  652. * Requeue all waiters hashed on one physical page to another
  653. * physical page.
  654. */
  655. static int futex_requeue(u32 __user *uaddr1, u32 __user *uaddr2,
  656. int nr_wake, int nr_requeue, u32 *cmpval)
  657. {
  658. union futex_key key1, key2;
  659. struct futex_hash_bucket *hb1, *hb2;
  660. struct list_head *head1;
  661. struct futex_q *this, *next;
  662. int ret, drop_count = 0;
  663. retry:
  664. down_read(&current->mm->mmap_sem);
  665. ret = get_futex_key(uaddr1, &key1);
  666. if (unlikely(ret != 0))
  667. goto out;
  668. ret = get_futex_key(uaddr2, &key2);
  669. if (unlikely(ret != 0))
  670. goto out;
  671. hb1 = hash_futex(&key1);
  672. hb2 = hash_futex(&key2);
  673. if (hb1 < hb2)
  674. spin_lock(&hb1->lock);
  675. spin_lock(&hb2->lock);
  676. if (hb1 > hb2)
  677. spin_lock(&hb1->lock);
  678. if (likely(cmpval != NULL)) {
  679. u32 curval;
  680. ret = get_futex_value_locked(&curval, uaddr1);
  681. if (unlikely(ret)) {
  682. spin_unlock(&hb1->lock);
  683. if (hb1 != hb2)
  684. spin_unlock(&hb2->lock);
  685. /*
  686. * If we would have faulted, release mmap_sem, fault
  687. * it in and start all over again.
  688. */
  689. up_read(&current->mm->mmap_sem);
  690. ret = get_user(curval, uaddr1);
  691. if (!ret)
  692. goto retry;
  693. return ret;
  694. }
  695. if (curval != *cmpval) {
  696. ret = -EAGAIN;
  697. goto out_unlock;
  698. }
  699. }
  700. head1 = &hb1->chain;
  701. list_for_each_entry_safe(this, next, head1, list) {
  702. if (!match_futex (&this->key, &key1))
  703. continue;
  704. if (++ret <= nr_wake) {
  705. wake_futex(this);
  706. } else {
  707. list_move_tail(&this->list, &hb2->chain);
  708. this->lock_ptr = &hb2->lock;
  709. this->key = key2;
  710. get_key_refs(&key2);
  711. drop_count++;
  712. if (ret - nr_wake >= nr_requeue)
  713. break;
  714. /* Make sure to stop if key1 == key2: */
  715. if (head1 == &hb2->chain && head1 != &next->list)
  716. head1 = &this->list;
  717. }
  718. }
  719. out_unlock:
  720. spin_unlock(&hb1->lock);
  721. if (hb1 != hb2)
  722. spin_unlock(&hb2->lock);
  723. /* drop_key_refs() must be called outside the spinlocks. */
  724. while (--drop_count >= 0)
  725. drop_key_refs(&key1);
  726. out:
  727. up_read(&current->mm->mmap_sem);
  728. return ret;
  729. }
  730. /* The key must be already stored in q->key. */
  731. static inline struct futex_hash_bucket *
  732. queue_lock(struct futex_q *q, int fd, struct file *filp)
  733. {
  734. struct futex_hash_bucket *hb;
  735. q->fd = fd;
  736. q->filp = filp;
  737. init_waitqueue_head(&q->waiters);
  738. get_key_refs(&q->key);
  739. hb = hash_futex(&q->key);
  740. q->lock_ptr = &hb->lock;
  741. spin_lock(&hb->lock);
  742. return hb;
  743. }
  744. static inline void __queue_me(struct futex_q *q, struct futex_hash_bucket *hb)
  745. {
  746. list_add_tail(&q->list, &hb->chain);
  747. q->task = current;
  748. spin_unlock(&hb->lock);
  749. }
  750. static inline void
  751. queue_unlock(struct futex_q *q, struct futex_hash_bucket *hb)
  752. {
  753. spin_unlock(&hb->lock);
  754. drop_key_refs(&q->key);
  755. }
  756. /*
  757. * queue_me and unqueue_me must be called as a pair, each
  758. * exactly once. They are called with the hashed spinlock held.
  759. */
  760. /* The key must be already stored in q->key. */
  761. static void queue_me(struct futex_q *q, int fd, struct file *filp)
  762. {
  763. struct futex_hash_bucket *hb;
  764. hb = queue_lock(q, fd, filp);
  765. __queue_me(q, hb);
  766. }
  767. /* Return 1 if we were still queued (ie. 0 means we were woken) */
  768. static int unqueue_me(struct futex_q *q)
  769. {
  770. spinlock_t *lock_ptr;
  771. int ret = 0;
  772. /* In the common case we don't take the spinlock, which is nice. */
  773. retry:
  774. lock_ptr = q->lock_ptr;
  775. if (lock_ptr != 0) {
  776. spin_lock(lock_ptr);
  777. /*
  778. * q->lock_ptr can change between reading it and
  779. * spin_lock(), causing us to take the wrong lock. This
  780. * corrects the race condition.
  781. *
  782. * Reasoning goes like this: if we have the wrong lock,
  783. * q->lock_ptr must have changed (maybe several times)
  784. * between reading it and the spin_lock(). It can
  785. * change again after the spin_lock() but only if it was
  786. * already changed before the spin_lock(). It cannot,
  787. * however, change back to the original value. Therefore
  788. * we can detect whether we acquired the correct lock.
  789. */
  790. if (unlikely(lock_ptr != q->lock_ptr)) {
  791. spin_unlock(lock_ptr);
  792. goto retry;
  793. }
  794. WARN_ON(list_empty(&q->list));
  795. list_del(&q->list);
  796. BUG_ON(q->pi_state);
  797. spin_unlock(lock_ptr);
  798. ret = 1;
  799. }
  800. drop_key_refs(&q->key);
  801. return ret;
  802. }
  803. /*
  804. * PI futexes can not be requeued and must remove themself from the
  805. * hash bucket. The hash bucket lock is held on entry and dropped here.
  806. */
  807. static void unqueue_me_pi(struct futex_q *q, struct futex_hash_bucket *hb)
  808. {
  809. WARN_ON(list_empty(&q->list));
  810. list_del(&q->list);
  811. BUG_ON(!q->pi_state);
  812. free_pi_state(q->pi_state);
  813. q->pi_state = NULL;
  814. spin_unlock(&hb->lock);
  815. drop_key_refs(&q->key);
  816. }
  817. static int futex_wait(u32 __user *uaddr, u32 val, unsigned long time)
  818. {
  819. struct task_struct *curr = current;
  820. DECLARE_WAITQUEUE(wait, curr);
  821. struct futex_hash_bucket *hb;
  822. struct futex_q q;
  823. u32 uval;
  824. int ret;
  825. q.pi_state = NULL;
  826. retry:
  827. down_read(&curr->mm->mmap_sem);
  828. ret = get_futex_key(uaddr, &q.key);
  829. if (unlikely(ret != 0))
  830. goto out_release_sem;
  831. hb = queue_lock(&q, -1, NULL);
  832. /*
  833. * Access the page AFTER the futex is queued.
  834. * Order is important:
  835. *
  836. * Userspace waiter: val = var; if (cond(val)) futex_wait(&var, val);
  837. * Userspace waker: if (cond(var)) { var = new; futex_wake(&var); }
  838. *
  839. * The basic logical guarantee of a futex is that it blocks ONLY
  840. * if cond(var) is known to be true at the time of blocking, for
  841. * any cond. If we queued after testing *uaddr, that would open
  842. * a race condition where we could block indefinitely with
  843. * cond(var) false, which would violate the guarantee.
  844. *
  845. * A consequence is that futex_wait() can return zero and absorb
  846. * a wakeup when *uaddr != val on entry to the syscall. This is
  847. * rare, but normal.
  848. *
  849. * We hold the mmap semaphore, so the mapping cannot have changed
  850. * since we looked it up in get_futex_key.
  851. */
  852. ret = get_futex_value_locked(&uval, uaddr);
  853. if (unlikely(ret)) {
  854. queue_unlock(&q, hb);
  855. /*
  856. * If we would have faulted, release mmap_sem, fault it in and
  857. * start all over again.
  858. */
  859. up_read(&curr->mm->mmap_sem);
  860. ret = get_user(uval, uaddr);
  861. if (!ret)
  862. goto retry;
  863. return ret;
  864. }
  865. ret = -EWOULDBLOCK;
  866. if (uval != val)
  867. goto out_unlock_release_sem;
  868. /* Only actually queue if *uaddr contained val. */
  869. __queue_me(&q, hb);
  870. /*
  871. * Now the futex is queued and we have checked the data, we
  872. * don't want to hold mmap_sem while we sleep.
  873. */
  874. up_read(&curr->mm->mmap_sem);
  875. /*
  876. * There might have been scheduling since the queue_me(), as we
  877. * cannot hold a spinlock across the get_user() in case it
  878. * faults, and we cannot just set TASK_INTERRUPTIBLE state when
  879. * queueing ourselves into the futex hash. This code thus has to
  880. * rely on the futex_wake() code removing us from hash when it
  881. * wakes us up.
  882. */
  883. /* add_wait_queue is the barrier after __set_current_state. */
  884. __set_current_state(TASK_INTERRUPTIBLE);
  885. add_wait_queue(&q.waiters, &wait);
  886. /*
  887. * !list_empty() is safe here without any lock.
  888. * q.lock_ptr != 0 is not safe, because of ordering against wakeup.
  889. */
  890. if (likely(!list_empty(&q.list)))
  891. time = schedule_timeout(time);
  892. __set_current_state(TASK_RUNNING);
  893. /*
  894. * NOTE: we don't remove ourselves from the waitqueue because
  895. * we are the only user of it.
  896. */
  897. /* If we were woken (and unqueued), we succeeded, whatever. */
  898. if (!unqueue_me(&q))
  899. return 0;
  900. if (time == 0)
  901. return -ETIMEDOUT;
  902. /*
  903. * We expect signal_pending(current), but another thread may
  904. * have handled it for us already.
  905. */
  906. return -EINTR;
  907. out_unlock_release_sem:
  908. queue_unlock(&q, hb);
  909. out_release_sem:
  910. up_read(&curr->mm->mmap_sem);
  911. return ret;
  912. }
  913. /*
  914. * Userspace tried a 0 -> TID atomic transition of the futex value
  915. * and failed. The kernel side here does the whole locking operation:
  916. * if there are waiters then it will block, it does PI, etc. (Due to
  917. * races the kernel might see a 0 value of the futex too.)
  918. */
  919. static int do_futex_lock_pi(u32 __user *uaddr, int detect, int trylock,
  920. struct hrtimer_sleeper *to)
  921. {
  922. struct task_struct *curr = current;
  923. struct futex_hash_bucket *hb;
  924. u32 uval, newval, curval;
  925. struct futex_q q;
  926. int ret, attempt = 0;
  927. if (refill_pi_state_cache())
  928. return -ENOMEM;
  929. q.pi_state = NULL;
  930. retry:
  931. down_read(&curr->mm->mmap_sem);
  932. ret = get_futex_key(uaddr, &q.key);
  933. if (unlikely(ret != 0))
  934. goto out_release_sem;
  935. hb = queue_lock(&q, -1, NULL);
  936. retry_locked:
  937. /*
  938. * To avoid races, we attempt to take the lock here again
  939. * (by doing a 0 -> TID atomic cmpxchg), while holding all
  940. * the locks. It will most likely not succeed.
  941. */
  942. newval = current->pid;
  943. inc_preempt_count();
  944. curval = futex_atomic_cmpxchg_inatomic(uaddr, 0, newval);
  945. dec_preempt_count();
  946. if (unlikely(curval == -EFAULT))
  947. goto uaddr_faulted;
  948. /* We own the lock already */
  949. if (unlikely((curval & FUTEX_TID_MASK) == current->pid)) {
  950. if (!detect && 0)
  951. force_sig(SIGKILL, current);
  952. ret = -EDEADLK;
  953. goto out_unlock_release_sem;
  954. }
  955. /*
  956. * Surprise - we got the lock. Just return
  957. * to userspace:
  958. */
  959. if (unlikely(!curval))
  960. goto out_unlock_release_sem;
  961. uval = curval;
  962. newval = uval | FUTEX_WAITERS;
  963. inc_preempt_count();
  964. curval = futex_atomic_cmpxchg_inatomic(uaddr, uval, newval);
  965. dec_preempt_count();
  966. if (unlikely(curval == -EFAULT))
  967. goto uaddr_faulted;
  968. if (unlikely(curval != uval))
  969. goto retry_locked;
  970. /*
  971. * We dont have the lock. Look up the PI state (or create it if
  972. * we are the first waiter):
  973. */
  974. ret = lookup_pi_state(uval, hb, &q);
  975. if (unlikely(ret)) {
  976. /*
  977. * There were no waiters and the owner task lookup
  978. * failed. When the OWNER_DIED bit is set, then we
  979. * know that this is a robust futex and we actually
  980. * take the lock. This is safe as we are protected by
  981. * the hash bucket lock. We also set the waiters bit
  982. * unconditionally here, to simplify glibc handling of
  983. * multiple tasks racing to acquire the lock and
  984. * cleanup the problems which were left by the dead
  985. * owner.
  986. */
  987. if (curval & FUTEX_OWNER_DIED) {
  988. uval = newval;
  989. newval = current->pid |
  990. FUTEX_OWNER_DIED | FUTEX_WAITERS;
  991. inc_preempt_count();
  992. curval = futex_atomic_cmpxchg_inatomic(uaddr,
  993. uval, newval);
  994. dec_preempt_count();
  995. if (unlikely(curval == -EFAULT))
  996. goto uaddr_faulted;
  997. if (unlikely(curval != uval))
  998. goto retry_locked;
  999. ret = 0;
  1000. }
  1001. goto out_unlock_release_sem;
  1002. }
  1003. /*
  1004. * Only actually queue now that the atomic ops are done:
  1005. */
  1006. __queue_me(&q, hb);
  1007. /*
  1008. * Now the futex is queued and we have checked the data, we
  1009. * don't want to hold mmap_sem while we sleep.
  1010. */
  1011. up_read(&curr->mm->mmap_sem);
  1012. WARN_ON(!q.pi_state);
  1013. /*
  1014. * Block on the PI mutex:
  1015. */
  1016. if (!trylock)
  1017. ret = rt_mutex_timed_lock(&q.pi_state->pi_mutex, to, 1);
  1018. else {
  1019. ret = rt_mutex_trylock(&q.pi_state->pi_mutex);
  1020. /* Fixup the trylock return value: */
  1021. ret = ret ? 0 : -EWOULDBLOCK;
  1022. }
  1023. down_read(&curr->mm->mmap_sem);
  1024. hb = queue_lock(&q, -1, NULL);
  1025. /*
  1026. * Got the lock. We might not be the anticipated owner if we
  1027. * did a lock-steal - fix up the PI-state in that case.
  1028. */
  1029. if (!ret && q.pi_state->owner != curr) {
  1030. u32 newtid = current->pid | FUTEX_WAITERS;
  1031. /* Owner died? */
  1032. if (q.pi_state->owner != NULL) {
  1033. spin_lock_irq(&q.pi_state->owner->pi_lock);
  1034. list_del_init(&q.pi_state->list);
  1035. spin_unlock_irq(&q.pi_state->owner->pi_lock);
  1036. } else
  1037. newtid |= FUTEX_OWNER_DIED;
  1038. q.pi_state->owner = current;
  1039. spin_lock_irq(&current->pi_lock);
  1040. list_add(&q.pi_state->list, &current->pi_state_list);
  1041. spin_unlock_irq(&current->pi_lock);
  1042. /* Unqueue and drop the lock */
  1043. unqueue_me_pi(&q, hb);
  1044. up_read(&curr->mm->mmap_sem);
  1045. /*
  1046. * We own it, so we have to replace the pending owner
  1047. * TID. This must be atomic as we have preserve the
  1048. * owner died bit here.
  1049. */
  1050. ret = get_user(uval, uaddr);
  1051. while (!ret) {
  1052. newval = (uval & FUTEX_OWNER_DIED) | newtid;
  1053. curval = futex_atomic_cmpxchg_inatomic(uaddr,
  1054. uval, newval);
  1055. if (curval == -EFAULT)
  1056. ret = -EFAULT;
  1057. if (curval == uval)
  1058. break;
  1059. uval = curval;
  1060. }
  1061. } else {
  1062. /*
  1063. * Catch the rare case, where the lock was released
  1064. * when we were on the way back before we locked
  1065. * the hash bucket.
  1066. */
  1067. if (ret && q.pi_state->owner == curr) {
  1068. if (rt_mutex_trylock(&q.pi_state->pi_mutex))
  1069. ret = 0;
  1070. }
  1071. /* Unqueue and drop the lock */
  1072. unqueue_me_pi(&q, hb);
  1073. up_read(&curr->mm->mmap_sem);
  1074. }
  1075. if (!detect && ret == -EDEADLK && 0)
  1076. force_sig(SIGKILL, current);
  1077. return ret;
  1078. out_unlock_release_sem:
  1079. queue_unlock(&q, hb);
  1080. out_release_sem:
  1081. up_read(&curr->mm->mmap_sem);
  1082. return ret;
  1083. uaddr_faulted:
  1084. /*
  1085. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1086. * non-atomically. Therefore, if get_user below is not
  1087. * enough, we need to handle the fault ourselves, while
  1088. * still holding the mmap_sem.
  1089. */
  1090. if (attempt++) {
  1091. if (futex_handle_fault((unsigned long)uaddr, attempt))
  1092. goto out_unlock_release_sem;
  1093. goto retry_locked;
  1094. }
  1095. queue_unlock(&q, hb);
  1096. up_read(&curr->mm->mmap_sem);
  1097. ret = get_user(uval, uaddr);
  1098. if (!ret && (uval != -EFAULT))
  1099. goto retry;
  1100. return ret;
  1101. }
  1102. /*
  1103. * Restart handler
  1104. */
  1105. static long futex_lock_pi_restart(struct restart_block *restart)
  1106. {
  1107. struct hrtimer_sleeper timeout, *to = NULL;
  1108. int ret;
  1109. restart->fn = do_no_restart_syscall;
  1110. if (restart->arg2 || restart->arg3) {
  1111. to = &timeout;
  1112. hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_ABS);
  1113. hrtimer_init_sleeper(to, current);
  1114. to->timer.expires.tv64 = ((u64)restart->arg1 << 32) |
  1115. (u64) restart->arg0;
  1116. }
  1117. pr_debug("lock_pi restart: %p, %d (%d)\n",
  1118. (u32 __user *)restart->arg0, current->pid);
  1119. ret = do_futex_lock_pi((u32 __user *)restart->arg0, restart->arg1,
  1120. 0, to);
  1121. if (ret != -EINTR)
  1122. return ret;
  1123. restart->fn = futex_lock_pi_restart;
  1124. /* The other values are filled in */
  1125. return -ERESTART_RESTARTBLOCK;
  1126. }
  1127. /*
  1128. * Called from the syscall entry below.
  1129. */
  1130. static int futex_lock_pi(u32 __user *uaddr, int detect, unsigned long sec,
  1131. long nsec, int trylock)
  1132. {
  1133. struct hrtimer_sleeper timeout, *to = NULL;
  1134. struct restart_block *restart;
  1135. int ret;
  1136. if (sec != MAX_SCHEDULE_TIMEOUT) {
  1137. to = &timeout;
  1138. hrtimer_init(&to->timer, CLOCK_REALTIME, HRTIMER_ABS);
  1139. hrtimer_init_sleeper(to, current);
  1140. to->timer.expires = ktime_set(sec, nsec);
  1141. }
  1142. ret = do_futex_lock_pi(uaddr, detect, trylock, to);
  1143. if (ret != -EINTR)
  1144. return ret;
  1145. pr_debug("lock_pi interrupted: %p, %d (%d)\n", uaddr, current->pid);
  1146. restart = &current_thread_info()->restart_block;
  1147. restart->fn = futex_lock_pi_restart;
  1148. restart->arg0 = (unsigned long) uaddr;
  1149. restart->arg1 = detect;
  1150. if (to) {
  1151. restart->arg2 = to->timer.expires.tv64 & 0xFFFFFFFF;
  1152. restart->arg3 = to->timer.expires.tv64 >> 32;
  1153. } else
  1154. restart->arg2 = restart->arg3 = 0;
  1155. return -ERESTART_RESTARTBLOCK;
  1156. }
  1157. /*
  1158. * Userspace attempted a TID -> 0 atomic transition, and failed.
  1159. * This is the in-kernel slowpath: we look up the PI state (if any),
  1160. * and do the rt-mutex unlock.
  1161. */
  1162. static int futex_unlock_pi(u32 __user *uaddr)
  1163. {
  1164. struct futex_hash_bucket *hb;
  1165. struct futex_q *this, *next;
  1166. u32 uval;
  1167. struct list_head *head;
  1168. union futex_key key;
  1169. int ret, attempt = 0;
  1170. retry:
  1171. if (get_user(uval, uaddr))
  1172. return -EFAULT;
  1173. /*
  1174. * We release only a lock we actually own:
  1175. */
  1176. if ((uval & FUTEX_TID_MASK) != current->pid)
  1177. return -EPERM;
  1178. /*
  1179. * First take all the futex related locks:
  1180. */
  1181. down_read(&current->mm->mmap_sem);
  1182. ret = get_futex_key(uaddr, &key);
  1183. if (unlikely(ret != 0))
  1184. goto out;
  1185. hb = hash_futex(&key);
  1186. spin_lock(&hb->lock);
  1187. retry_locked:
  1188. /*
  1189. * To avoid races, try to do the TID -> 0 atomic transition
  1190. * again. If it succeeds then we can return without waking
  1191. * anyone else up:
  1192. */
  1193. inc_preempt_count();
  1194. uval = futex_atomic_cmpxchg_inatomic(uaddr, current->pid, 0);
  1195. dec_preempt_count();
  1196. if (unlikely(uval == -EFAULT))
  1197. goto pi_faulted;
  1198. /*
  1199. * Rare case: we managed to release the lock atomically,
  1200. * no need to wake anyone else up:
  1201. */
  1202. if (unlikely(uval == current->pid))
  1203. goto out_unlock;
  1204. /*
  1205. * Ok, other tasks may need to be woken up - check waiters
  1206. * and do the wakeup if necessary:
  1207. */
  1208. head = &hb->chain;
  1209. list_for_each_entry_safe(this, next, head, list) {
  1210. if (!match_futex (&this->key, &key))
  1211. continue;
  1212. ret = wake_futex_pi(uaddr, uval, this);
  1213. /*
  1214. * The atomic access to the futex value
  1215. * generated a pagefault, so retry the
  1216. * user-access and the wakeup:
  1217. */
  1218. if (ret == -EFAULT)
  1219. goto pi_faulted;
  1220. goto out_unlock;
  1221. }
  1222. /*
  1223. * No waiters - kernel unlocks the futex:
  1224. */
  1225. ret = unlock_futex_pi(uaddr, uval);
  1226. if (ret == -EFAULT)
  1227. goto pi_faulted;
  1228. out_unlock:
  1229. spin_unlock(&hb->lock);
  1230. out:
  1231. up_read(&current->mm->mmap_sem);
  1232. return ret;
  1233. pi_faulted:
  1234. /*
  1235. * We have to r/w *(int __user *)uaddr, but we can't modify it
  1236. * non-atomically. Therefore, if get_user below is not
  1237. * enough, we need to handle the fault ourselves, while
  1238. * still holding the mmap_sem.
  1239. */
  1240. if (attempt++) {
  1241. if (futex_handle_fault((unsigned long)uaddr, attempt))
  1242. goto out_unlock;
  1243. goto retry_locked;
  1244. }
  1245. spin_unlock(&hb->lock);
  1246. up_read(&current->mm->mmap_sem);
  1247. ret = get_user(uval, uaddr);
  1248. if (!ret && (uval != -EFAULT))
  1249. goto retry;
  1250. return ret;
  1251. }
  1252. static int futex_close(struct inode *inode, struct file *filp)
  1253. {
  1254. struct futex_q *q = filp->private_data;
  1255. unqueue_me(q);
  1256. kfree(q);
  1257. return 0;
  1258. }
  1259. /* This is one-shot: once it's gone off you need a new fd */
  1260. static unsigned int futex_poll(struct file *filp,
  1261. struct poll_table_struct *wait)
  1262. {
  1263. struct futex_q *q = filp->private_data;
  1264. int ret = 0;
  1265. poll_wait(filp, &q->waiters, wait);
  1266. /*
  1267. * list_empty() is safe here without any lock.
  1268. * q->lock_ptr != 0 is not safe, because of ordering against wakeup.
  1269. */
  1270. if (list_empty(&q->list))
  1271. ret = POLLIN | POLLRDNORM;
  1272. return ret;
  1273. }
  1274. static struct file_operations futex_fops = {
  1275. .release = futex_close,
  1276. .poll = futex_poll,
  1277. };
  1278. /*
  1279. * Signal allows caller to avoid the race which would occur if they
  1280. * set the sigio stuff up afterwards.
  1281. */
  1282. static int futex_fd(u32 __user *uaddr, int signal)
  1283. {
  1284. struct futex_q *q;
  1285. struct file *filp;
  1286. int ret, err;
  1287. ret = -EINVAL;
  1288. if (!valid_signal(signal))
  1289. goto out;
  1290. ret = get_unused_fd();
  1291. if (ret < 0)
  1292. goto out;
  1293. filp = get_empty_filp();
  1294. if (!filp) {
  1295. put_unused_fd(ret);
  1296. ret = -ENFILE;
  1297. goto out;
  1298. }
  1299. filp->f_op = &futex_fops;
  1300. filp->f_vfsmnt = mntget(futex_mnt);
  1301. filp->f_dentry = dget(futex_mnt->mnt_root);
  1302. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  1303. if (signal) {
  1304. err = f_setown(filp, current->pid, 1);
  1305. if (err < 0) {
  1306. goto error;
  1307. }
  1308. filp->f_owner.signum = signal;
  1309. }
  1310. q = kmalloc(sizeof(*q), GFP_KERNEL);
  1311. if (!q) {
  1312. err = -ENOMEM;
  1313. goto error;
  1314. }
  1315. q->pi_state = NULL;
  1316. down_read(&current->mm->mmap_sem);
  1317. err = get_futex_key(uaddr, &q->key);
  1318. if (unlikely(err != 0)) {
  1319. up_read(&current->mm->mmap_sem);
  1320. kfree(q);
  1321. goto error;
  1322. }
  1323. /*
  1324. * queue_me() must be called before releasing mmap_sem, because
  1325. * key->shared.inode needs to be referenced while holding it.
  1326. */
  1327. filp->private_data = q;
  1328. queue_me(q, ret, filp);
  1329. up_read(&current->mm->mmap_sem);
  1330. /* Now we map fd to filp, so userspace can access it */
  1331. fd_install(ret, filp);
  1332. out:
  1333. return ret;
  1334. error:
  1335. put_unused_fd(ret);
  1336. put_filp(filp);
  1337. ret = err;
  1338. goto out;
  1339. }
  1340. /*
  1341. * Support for robust futexes: the kernel cleans up held futexes at
  1342. * thread exit time.
  1343. *
  1344. * Implementation: user-space maintains a per-thread list of locks it
  1345. * is holding. Upon do_exit(), the kernel carefully walks this list,
  1346. * and marks all locks that are owned by this thread with the
  1347. * FUTEX_OWNER_DIED bit, and wakes up a waiter (if any). The list is
  1348. * always manipulated with the lock held, so the list is private and
  1349. * per-thread. Userspace also maintains a per-thread 'list_op_pending'
  1350. * field, to allow the kernel to clean up if the thread dies after
  1351. * acquiring the lock, but just before it could have added itself to
  1352. * the list. There can only be one such pending lock.
  1353. */
  1354. /**
  1355. * sys_set_robust_list - set the robust-futex list head of a task
  1356. * @head: pointer to the list-head
  1357. * @len: length of the list-head, as userspace expects
  1358. */
  1359. asmlinkage long
  1360. sys_set_robust_list(struct robust_list_head __user *head,
  1361. size_t len)
  1362. {
  1363. /*
  1364. * The kernel knows only one size for now:
  1365. */
  1366. if (unlikely(len != sizeof(*head)))
  1367. return -EINVAL;
  1368. current->robust_list = head;
  1369. return 0;
  1370. }
  1371. /**
  1372. * sys_get_robust_list - get the robust-futex list head of a task
  1373. * @pid: pid of the process [zero for current task]
  1374. * @head_ptr: pointer to a list-head pointer, the kernel fills it in
  1375. * @len_ptr: pointer to a length field, the kernel fills in the header size
  1376. */
  1377. asmlinkage long
  1378. sys_get_robust_list(int pid, struct robust_list_head __user **head_ptr,
  1379. size_t __user *len_ptr)
  1380. {
  1381. struct robust_list_head *head;
  1382. unsigned long ret;
  1383. if (!pid)
  1384. head = current->robust_list;
  1385. else {
  1386. struct task_struct *p;
  1387. ret = -ESRCH;
  1388. read_lock(&tasklist_lock);
  1389. p = find_task_by_pid(pid);
  1390. if (!p)
  1391. goto err_unlock;
  1392. ret = -EPERM;
  1393. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  1394. !capable(CAP_SYS_PTRACE))
  1395. goto err_unlock;
  1396. head = p->robust_list;
  1397. read_unlock(&tasklist_lock);
  1398. }
  1399. if (put_user(sizeof(*head), len_ptr))
  1400. return -EFAULT;
  1401. return put_user(head, head_ptr);
  1402. err_unlock:
  1403. read_unlock(&tasklist_lock);
  1404. return ret;
  1405. }
  1406. /*
  1407. * Process a futex-list entry, check whether it's owned by the
  1408. * dying task, and do notification if so:
  1409. */
  1410. int handle_futex_death(u32 __user *uaddr, struct task_struct *curr)
  1411. {
  1412. u32 uval, nval;
  1413. retry:
  1414. if (get_user(uval, uaddr))
  1415. return -1;
  1416. if ((uval & FUTEX_TID_MASK) == curr->pid) {
  1417. /*
  1418. * Ok, this dying thread is truly holding a futex
  1419. * of interest. Set the OWNER_DIED bit atomically
  1420. * via cmpxchg, and if the value had FUTEX_WAITERS
  1421. * set, wake up a waiter (if any). (We have to do a
  1422. * futex_wake() even if OWNER_DIED is already set -
  1423. * to handle the rare but possible case of recursive
  1424. * thread-death.) The rest of the cleanup is done in
  1425. * userspace.
  1426. */
  1427. nval = futex_atomic_cmpxchg_inatomic(uaddr, uval,
  1428. uval | FUTEX_OWNER_DIED);
  1429. if (nval == -EFAULT)
  1430. return -1;
  1431. if (nval != uval)
  1432. goto retry;
  1433. if (uval & FUTEX_WAITERS)
  1434. futex_wake(uaddr, 1);
  1435. }
  1436. return 0;
  1437. }
  1438. /*
  1439. * Walk curr->robust_list (very carefully, it's a userspace list!)
  1440. * and mark any locks found there dead, and notify any waiters.
  1441. *
  1442. * We silently return on any sign of list-walking problem.
  1443. */
  1444. void exit_robust_list(struct task_struct *curr)
  1445. {
  1446. struct robust_list_head __user *head = curr->robust_list;
  1447. struct robust_list __user *entry, *pending;
  1448. unsigned int limit = ROBUST_LIST_LIMIT;
  1449. unsigned long futex_offset;
  1450. /*
  1451. * Fetch the list head (which was registered earlier, via
  1452. * sys_set_robust_list()):
  1453. */
  1454. if (get_user(entry, &head->list.next))
  1455. return;
  1456. /*
  1457. * Fetch the relative futex offset:
  1458. */
  1459. if (get_user(futex_offset, &head->futex_offset))
  1460. return;
  1461. /*
  1462. * Fetch any possibly pending lock-add first, and handle it
  1463. * if it exists:
  1464. */
  1465. if (get_user(pending, &head->list_op_pending))
  1466. return;
  1467. if (pending)
  1468. handle_futex_death((void *)pending + futex_offset, curr);
  1469. while (entry != &head->list) {
  1470. /*
  1471. * A pending lock might already be on the list, so
  1472. * don't process it twice:
  1473. */
  1474. if (entry != pending)
  1475. if (handle_futex_death((void *)entry + futex_offset,
  1476. curr))
  1477. return;
  1478. /*
  1479. * Fetch the next entry in the list:
  1480. */
  1481. if (get_user(entry, &entry->next))
  1482. return;
  1483. /*
  1484. * Avoid excessively long or circular lists:
  1485. */
  1486. if (!--limit)
  1487. break;
  1488. cond_resched();
  1489. }
  1490. }
  1491. long do_futex(u32 __user *uaddr, int op, u32 val, unsigned long timeout,
  1492. u32 __user *uaddr2, u32 val2, u32 val3)
  1493. {
  1494. int ret;
  1495. switch (op) {
  1496. case FUTEX_WAIT:
  1497. ret = futex_wait(uaddr, val, timeout);
  1498. break;
  1499. case FUTEX_WAKE:
  1500. ret = futex_wake(uaddr, val);
  1501. break;
  1502. case FUTEX_FD:
  1503. /* non-zero val means F_SETOWN(getpid()) & F_SETSIG(val) */
  1504. ret = futex_fd(uaddr, val);
  1505. break;
  1506. case FUTEX_REQUEUE:
  1507. ret = futex_requeue(uaddr, uaddr2, val, val2, NULL);
  1508. break;
  1509. case FUTEX_CMP_REQUEUE:
  1510. ret = futex_requeue(uaddr, uaddr2, val, val2, &val3);
  1511. break;
  1512. case FUTEX_WAKE_OP:
  1513. ret = futex_wake_op(uaddr, uaddr2, val, val2, val3);
  1514. break;
  1515. case FUTEX_LOCK_PI:
  1516. ret = futex_lock_pi(uaddr, val, timeout, val2, 0);
  1517. break;
  1518. case FUTEX_UNLOCK_PI:
  1519. ret = futex_unlock_pi(uaddr);
  1520. break;
  1521. case FUTEX_TRYLOCK_PI:
  1522. ret = futex_lock_pi(uaddr, 0, timeout, val2, 1);
  1523. break;
  1524. default:
  1525. ret = -ENOSYS;
  1526. }
  1527. return ret;
  1528. }
  1529. asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
  1530. struct timespec __user *utime, u32 __user *uaddr2,
  1531. u32 val3)
  1532. {
  1533. struct timespec t;
  1534. unsigned long timeout = MAX_SCHEDULE_TIMEOUT;
  1535. u32 val2 = 0;
  1536. if (utime && (op == FUTEX_WAIT || op == FUTEX_LOCK_PI)) {
  1537. if (copy_from_user(&t, utime, sizeof(t)) != 0)
  1538. return -EFAULT;
  1539. if (!timespec_valid(&t))
  1540. return -EINVAL;
  1541. if (op == FUTEX_WAIT)
  1542. timeout = timespec_to_jiffies(&t) + 1;
  1543. else {
  1544. timeout = t.tv_sec;
  1545. val2 = t.tv_nsec;
  1546. }
  1547. }
  1548. /*
  1549. * requeue parameter in 'utime' if op == FUTEX_REQUEUE.
  1550. */
  1551. if (op == FUTEX_REQUEUE || op == FUTEX_CMP_REQUEUE)
  1552. val2 = (u32) (unsigned long) utime;
  1553. return do_futex(uaddr, op, val, timeout, uaddr2, val2, val3);
  1554. }
  1555. static int futexfs_get_sb(struct file_system_type *fs_type,
  1556. int flags, const char *dev_name, void *data,
  1557. struct vfsmount *mnt)
  1558. {
  1559. return get_sb_pseudo(fs_type, "futex", NULL, 0xBAD1DEA, mnt);
  1560. }
  1561. static struct file_system_type futex_fs_type = {
  1562. .name = "futexfs",
  1563. .get_sb = futexfs_get_sb,
  1564. .kill_sb = kill_anon_super,
  1565. };
  1566. static int __init init(void)
  1567. {
  1568. unsigned int i;
  1569. register_filesystem(&futex_fs_type);
  1570. futex_mnt = kern_mount(&futex_fs_type);
  1571. for (i = 0; i < ARRAY_SIZE(futex_queues); i++) {
  1572. INIT_LIST_HEAD(&futex_queues[i].chain);
  1573. spin_lock_init(&futex_queues[i].lock);
  1574. }
  1575. return 0;
  1576. }
  1577. __initcall(init);