futex_compat.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * linux/kernel/futex_compat.c
  3. *
  4. * Futex compatibililty routines.
  5. *
  6. * Copyright 2006, Red Hat, Inc., Ingo Molnar
  7. */
  8. #include <linux/linkage.h>
  9. #include <linux/compat.h>
  10. #include <linux/futex.h>
  11. #include <asm/uaccess.h>
  12. /*
  13. * Fetch a robust-list pointer. Bit 0 signals PI futexes:
  14. */
  15. static inline int
  16. fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
  17. compat_uptr_t __user *head, int *pi)
  18. {
  19. if (get_user(*uentry, head))
  20. return -EFAULT;
  21. *entry = compat_ptr((*uentry) & ~1);
  22. *pi = (unsigned int)(*uentry) & 1;
  23. return 0;
  24. }
  25. /*
  26. * Walk curr->robust_list (very carefully, it's a userspace list!)
  27. * and mark any locks found there dead, and notify any waiters.
  28. *
  29. * We silently return on any sign of list-walking problem.
  30. */
  31. void compat_exit_robust_list(struct task_struct *curr)
  32. {
  33. struct compat_robust_list_head __user *head = curr->compat_robust_list;
  34. struct robust_list __user *entry, *next_entry, *pending;
  35. unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
  36. compat_uptr_t uentry, next_uentry, upending;
  37. compat_long_t futex_offset;
  38. int rc;
  39. /*
  40. * Fetch the list head (which was registered earlier, via
  41. * sys_set_robust_list()):
  42. */
  43. if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
  44. return;
  45. /*
  46. * Fetch the relative futex offset:
  47. */
  48. if (get_user(futex_offset, &head->futex_offset))
  49. return;
  50. /*
  51. * Fetch any possibly pending lock-add first, and handle it
  52. * if it exists:
  53. */
  54. if (fetch_robust_entry(&upending, &pending,
  55. &head->list_op_pending, &pip))
  56. return;
  57. next_entry = NULL; /* avoid warning with gcc */
  58. while (entry != (struct robust_list __user *) &head->list) {
  59. /*
  60. * Fetch the next entry in the list before calling
  61. * handle_futex_death:
  62. */
  63. rc = fetch_robust_entry(&next_uentry, &next_entry,
  64. (compat_uptr_t __user *)&entry->next, &next_pi);
  65. /*
  66. * A pending lock might already be on the list, so
  67. * dont process it twice:
  68. */
  69. if (entry != pending)
  70. if (handle_futex_death((void __user *)entry + futex_offset,
  71. curr, pi))
  72. return;
  73. if (rc)
  74. return;
  75. uentry = next_uentry;
  76. entry = next_entry;
  77. pi = next_pi;
  78. /*
  79. * Avoid excessively long or circular lists:
  80. */
  81. if (!--limit)
  82. break;
  83. cond_resched();
  84. }
  85. if (pending)
  86. handle_futex_death((void __user *)pending + futex_offset,
  87. curr, pip);
  88. }
  89. asmlinkage long
  90. compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
  91. compat_size_t len)
  92. {
  93. if (unlikely(len != sizeof(*head)))
  94. return -EINVAL;
  95. current->compat_robust_list = head;
  96. return 0;
  97. }
  98. asmlinkage long
  99. compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
  100. compat_size_t __user *len_ptr)
  101. {
  102. struct compat_robust_list_head __user *head;
  103. unsigned long ret;
  104. if (!pid)
  105. head = current->compat_robust_list;
  106. else {
  107. struct task_struct *p;
  108. ret = -ESRCH;
  109. read_lock(&tasklist_lock);
  110. p = find_task_by_pid(pid);
  111. if (!p)
  112. goto err_unlock;
  113. ret = -EPERM;
  114. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  115. !capable(CAP_SYS_PTRACE))
  116. goto err_unlock;
  117. head = p->compat_robust_list;
  118. read_unlock(&tasklist_lock);
  119. }
  120. if (put_user(sizeof(*head), len_ptr))
  121. return -EFAULT;
  122. return put_user(ptr_to_compat(head), head_ptr);
  123. err_unlock:
  124. read_unlock(&tasklist_lock);
  125. return ret;
  126. }
  127. asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
  128. struct compat_timespec __user *utime, u32 __user *uaddr2,
  129. u32 val3)
  130. {
  131. struct timespec ts;
  132. ktime_t t, *tp = NULL;
  133. int val2 = 0;
  134. int cmd = op & FUTEX_CMD_MASK;
  135. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
  136. if (get_compat_timespec(&ts, utime))
  137. return -EFAULT;
  138. if (!timespec_valid(&ts))
  139. return -EINVAL;
  140. t = timespec_to_ktime(ts);
  141. if (cmd == FUTEX_WAIT)
  142. t = ktime_add(ktime_get(), t);
  143. tp = &t;
  144. }
  145. if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE)
  146. val2 = (int) (unsigned long) utime;
  147. return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
  148. }