blob: a9642d528630260749f760f3c02bc8164f51af2b [file] [log] [blame]
Ingo Molnar34f192c2006-03-27 01:16:24 -08001/*
2 * linux/kernel/futex_compat.c
3 *
4 * Futex compatibililty routines.
5 *
6 * Copyright 2006, Red Hat, Inc., Ingo Molnar
7 */
8
9#include <linux/linkage.h>
10#include <linux/compat.h>
Pavel Emelyanovb4888932007-10-18 23:40:14 -070011#include <linux/nsproxy.h>
Ingo Molnar34f192c2006-03-27 01:16:24 -080012#include <linux/futex.h>
Kees Cookbdbb7762012-03-19 16:12:53 -070013#include <linux/ptrace.h>
Ingo Molnar34f192c2006-03-27 01:16:24 -080014
15#include <asm/uaccess.h>
16
Ingo Molnare3f2dde2006-07-29 05:17:57 +020017
18/*
19 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
20 */
21static inline int
22fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
Namhyung Kim1dcc41b2010-09-14 21:43:46 +090023 compat_uptr_t __user *head, unsigned int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +020024{
25 if (get_user(*uentry, head))
26 return -EFAULT;
27
28 *entry = compat_ptr((*uentry) & ~1);
29 *pi = (unsigned int)(*uentry) & 1;
30
31 return 0;
32}
33
Al Viro84816642008-03-29 03:07:58 +000034static void __user *futex_uaddr(struct robust_list __user *entry,
David Miller3c5fd9c72007-11-06 21:13:56 -080035 compat_long_t futex_offset)
36{
37 compat_uptr_t base = ptr_to_compat(entry);
38 void __user *uaddr = compat_ptr(base + futex_offset);
39
40 return uaddr;
41}
42
Ingo Molnar34f192c2006-03-27 01:16:24 -080043/*
44 * Walk curr->robust_list (very carefully, it's a userspace list!)
45 * and mark any locks found there dead, and notify any waiters.
46 *
47 * We silently return on any sign of list-walking problem.
48 */
49void compat_exit_robust_list(struct task_struct *curr)
50{
51 struct compat_robust_list_head __user *head = curr->compat_robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070052 struct robust_list __user *entry, *next_entry, *pending;
Darren Hart4c115e92010-11-04 15:00:00 -040053 unsigned int limit = ROBUST_LIST_LIMIT, pi, pip;
54 unsigned int uninitialized_var(next_pi);
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070055 compat_uptr_t uentry, next_uentry, upending;
Ingo Molnar34f192c2006-03-27 01:16:24 -080056 compat_long_t futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070057 int rc;
Ingo Molnar34f192c2006-03-27 01:16:24 -080058
Thomas Gleixnera0c1e902008-02-23 15:23:57 -080059 if (!futex_cmpxchg_enabled)
60 return;
61
Ingo Molnar34f192c2006-03-27 01:16:24 -080062 /*
63 * Fetch the list head (which was registered earlier, via
64 * sys_set_robust_list()):
65 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +020066 if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
Ingo Molnar34f192c2006-03-27 01:16:24 -080067 return;
Ingo Molnar34f192c2006-03-27 01:16:24 -080068 /*
69 * Fetch the relative futex offset:
70 */
71 if (get_user(futex_offset, &head->futex_offset))
72 return;
73 /*
74 * Fetch any possibly pending lock-add first, and handle it
75 * if it exists:
76 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +020077 if (fetch_robust_entry(&upending, &pending,
Thomas Gleixnerce2c6b52006-08-05 12:15:15 -070078 &head->list_op_pending, &pip))
Ingo Molnar34f192c2006-03-27 01:16:24 -080079 return;
Ingo Molnar34f192c2006-03-27 01:16:24 -080080
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070081 next_entry = NULL; /* avoid warning with gcc */
Arnd Bergmann179c85e2007-09-11 15:23:49 -070082 while (entry != (struct robust_list __user *) &head->list) {
Ingo Molnar34f192c2006-03-27 01:16:24 -080083 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070084 * Fetch the next entry in the list before calling
85 * handle_futex_death:
86 */
87 rc = fetch_robust_entry(&next_uentry, &next_entry,
88 (compat_uptr_t __user *)&entry->next, &next_pi);
89 /*
Ingo Molnar34f192c2006-03-27 01:16:24 -080090 * A pending lock might already be on the list, so
91 * dont process it twice:
92 */
David Miller3c5fd9c72007-11-06 21:13:56 -080093 if (entry != pending) {
94 void __user *uaddr = futex_uaddr(entry, futex_offset);
Ingo Molnar34f192c2006-03-27 01:16:24 -080095
David Miller3c5fd9c72007-11-06 21:13:56 -080096 if (handle_futex_death(uaddr, curr, pi))
97 return;
98 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070099 if (rc)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800100 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -0700101 uentry = next_uentry;
102 entry = next_entry;
103 pi = next_pi;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800104 /*
105 * Avoid excessively long or circular lists:
106 */
107 if (!--limit)
108 break;
109
110 cond_resched();
111 }
David Miller3c5fd9c72007-11-06 21:13:56 -0800112 if (pending) {
113 void __user *uaddr = futex_uaddr(pending, futex_offset);
114
115 handle_futex_death(uaddr, curr, pip);
116 }
Ingo Molnar34f192c2006-03-27 01:16:24 -0800117}
118
119asmlinkage long
120compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
121 compat_size_t len)
122{
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800123 if (!futex_cmpxchg_enabled)
124 return -ENOSYS;
125
Ingo Molnar34f192c2006-03-27 01:16:24 -0800126 if (unlikely(len != sizeof(*head)))
127 return -EINVAL;
128
129 current->compat_robust_list = head;
130
131 return 0;
132}
133
134asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +0100135compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
Ingo Molnar34f192c2006-03-27 01:16:24 -0800136 compat_size_t __user *len_ptr)
137{
Al Viroba46df92006-10-10 22:46:07 +0100138 struct compat_robust_list_head __user *head;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800139 unsigned long ret;
Kees Cookbdbb7762012-03-19 16:12:53 -0700140 struct task_struct *p;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800141
Thomas Gleixnera0c1e902008-02-23 15:23:57 -0800142 if (!futex_cmpxchg_enabled)
143 return -ENOSYS;
144
Kees Cookbdbb7762012-03-19 16:12:53 -0700145 rcu_read_lock();
Ingo Molnar34f192c2006-03-27 01:16:24 -0800146
Kees Cookbdbb7762012-03-19 16:12:53 -0700147 ret = -ESRCH;
148 if (!pid)
149 p = current;
150 else {
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700151 p = find_task_by_vpid(pid);
Ingo Molnar34f192c2006-03-27 01:16:24 -0800152 if (!p)
153 goto err_unlock;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800154 }
155
Kees Cookbdbb7762012-03-19 16:12:53 -0700156 ret = -EPERM;
157 if (!ptrace_may_access(p, PTRACE_MODE_READ))
158 goto err_unlock;
159
160 head = p->compat_robust_list;
161 rcu_read_unlock();
162
Ingo Molnar34f192c2006-03-27 01:16:24 -0800163 if (put_user(sizeof(*head), len_ptr))
164 return -EFAULT;
165 return put_user(ptr_to_compat(head), head_ptr);
166
167err_unlock:
Thomas Gleixnerf409adf2009-12-01 14:02:00 +0100168 rcu_read_unlock();
Ingo Molnar34f192c2006-03-27 01:16:24 -0800169
170 return ret;
171}
172
Ingo Molnar8f17d3a2006-03-27 01:16:27 -0800173asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
Ingo Molnar34f192c2006-03-27 01:16:24 -0800174 struct compat_timespec __user *utime, u32 __user *uaddr2,
Ingo Molnar8f17d3a2006-03-27 01:16:27 -0800175 u32 val3)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800176{
Pierre Peifferc19384b2007-05-09 02:35:02 -0700177 struct timespec ts;
178 ktime_t t, *tp = NULL;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800179 int val2 = 0;
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700180 int cmd = op & FUTEX_CMD_MASK;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800181
Thomas Gleixnercd689982008-02-01 17:45:14 +0100182 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
Dinakar Guniguntala4dc88022009-08-10 18:31:42 +0530183 cmd == FUTEX_WAIT_BITSET ||
184 cmd == FUTEX_WAIT_REQUEUE_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -0700185 if (get_compat_timespec(&ts, utime))
Ingo Molnar34f192c2006-03-27 01:16:24 -0800186 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -0700187 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -0800188 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -0700189
190 t = timespec_to_ktime(ts);
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700191 if (cmd == FUTEX_WAIT)
Thomas Gleixner5a7780e2008-02-13 09:20:43 +0100192 t = ktime_add_safe(ktime_get(), t);
Pierre Peifferc19384b2007-05-09 02:35:02 -0700193 tp = &t;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800194 }
Dinakar Guniguntala4dc88022009-08-10 18:31:42 +0530195 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE ||
196 cmd == FUTEX_CMP_REQUEUE_PI || cmd == FUTEX_WAKE_OP)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800197 val2 = (int) (unsigned long) utime;
198
Pierre Peifferc19384b2007-05-09 02:35:02 -0700199 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Ingo Molnar34f192c2006-03-27 01:16:24 -0800200}