blob: 0a43def6fee7de877f43bdd0d7276efc05b8a48e [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>
13
14#include <asm/uaccess.h>
15
Ingo Molnare3f2dde2006-07-29 05:17:57 +020016
17/*
18 * Fetch a robust-list pointer. Bit 0 signals PI futexes:
19 */
20static inline int
21fetch_robust_entry(compat_uptr_t *uentry, struct robust_list __user **entry,
Al Viroba46df92006-10-10 22:46:07 +010022 compat_uptr_t __user *head, int *pi)
Ingo Molnare3f2dde2006-07-29 05:17:57 +020023{
24 if (get_user(*uentry, head))
25 return -EFAULT;
26
27 *entry = compat_ptr((*uentry) & ~1);
28 *pi = (unsigned int)(*uentry) & 1;
29
30 return 0;
31}
32
David Miller3c5fd9c72007-11-06 21:13:56 -080033static void __user *futex_uaddr(struct robust_list *entry,
34 compat_long_t futex_offset)
35{
36 compat_uptr_t base = ptr_to_compat(entry);
37 void __user *uaddr = compat_ptr(base + futex_offset);
38
39 return uaddr;
40}
41
Ingo Molnar34f192c2006-03-27 01:16:24 -080042/*
43 * Walk curr->robust_list (very carefully, it's a userspace list!)
44 * and mark any locks found there dead, and notify any waiters.
45 *
46 * We silently return on any sign of list-walking problem.
47 */
48void compat_exit_robust_list(struct task_struct *curr)
49{
50 struct compat_robust_list_head __user *head = curr->compat_robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070051 struct robust_list __user *entry, *next_entry, *pending;
52 unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
53 compat_uptr_t uentry, next_uentry, upending;
Ingo Molnar34f192c2006-03-27 01:16:24 -080054 compat_long_t futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070055 int rc;
Ingo Molnar34f192c2006-03-27 01:16:24 -080056
57 /*
58 * Fetch the list head (which was registered earlier, via
59 * sys_set_robust_list()):
60 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +020061 if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
Ingo Molnar34f192c2006-03-27 01:16:24 -080062 return;
Ingo Molnar34f192c2006-03-27 01:16:24 -080063 /*
64 * Fetch the relative futex offset:
65 */
66 if (get_user(futex_offset, &head->futex_offset))
67 return;
68 /*
69 * Fetch any possibly pending lock-add first, and handle it
70 * if it exists:
71 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +020072 if (fetch_robust_entry(&upending, &pending,
Thomas Gleixnerce2c6b52006-08-05 12:15:15 -070073 &head->list_op_pending, &pip))
Ingo Molnar34f192c2006-03-27 01:16:24 -080074 return;
Ingo Molnar34f192c2006-03-27 01:16:24 -080075
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070076 next_entry = NULL; /* avoid warning with gcc */
Arnd Bergmann179c85e2007-09-11 15:23:49 -070077 while (entry != (struct robust_list __user *) &head->list) {
Ingo Molnar34f192c2006-03-27 01:16:24 -080078 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070079 * Fetch the next entry in the list before calling
80 * handle_futex_death:
81 */
82 rc = fetch_robust_entry(&next_uentry, &next_entry,
83 (compat_uptr_t __user *)&entry->next, &next_pi);
84 /*
Ingo Molnar34f192c2006-03-27 01:16:24 -080085 * A pending lock might already be on the list, so
86 * dont process it twice:
87 */
David Miller3c5fd9c72007-11-06 21:13:56 -080088 if (entry != pending) {
89 void __user *uaddr = futex_uaddr(entry, futex_offset);
Ingo Molnar34f192c2006-03-27 01:16:24 -080090
David Miller3c5fd9c72007-11-06 21:13:56 -080091 if (handle_futex_death(uaddr, curr, pi))
92 return;
93 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070094 if (rc)
Ingo Molnar34f192c2006-03-27 01:16:24 -080095 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070096 uentry = next_uentry;
97 entry = next_entry;
98 pi = next_pi;
Ingo Molnar34f192c2006-03-27 01:16:24 -080099 /*
100 * Avoid excessively long or circular lists:
101 */
102 if (!--limit)
103 break;
104
105 cond_resched();
106 }
David Miller3c5fd9c72007-11-06 21:13:56 -0800107 if (pending) {
108 void __user *uaddr = futex_uaddr(pending, futex_offset);
109
110 handle_futex_death(uaddr, curr, pip);
111 }
Ingo Molnar34f192c2006-03-27 01:16:24 -0800112}
113
114asmlinkage long
115compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
116 compat_size_t len)
117{
118 if (unlikely(len != sizeof(*head)))
119 return -EINVAL;
120
121 current->compat_robust_list = head;
122
123 return 0;
124}
125
126asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +0100127compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
Ingo Molnar34f192c2006-03-27 01:16:24 -0800128 compat_size_t __user *len_ptr)
129{
Al Viroba46df92006-10-10 22:46:07 +0100130 struct compat_robust_list_head __user *head;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800131 unsigned long ret;
132
133 if (!pid)
134 head = current->compat_robust_list;
135 else {
136 struct task_struct *p;
137
138 ret = -ESRCH;
139 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700140 p = find_task_by_vpid(pid);
Ingo Molnar34f192c2006-03-27 01:16:24 -0800141 if (!p)
142 goto err_unlock;
143 ret = -EPERM;
144 if ((current->euid != p->euid) && (current->euid != p->uid) &&
145 !capable(CAP_SYS_PTRACE))
146 goto err_unlock;
147 head = p->compat_robust_list;
148 read_unlock(&tasklist_lock);
149 }
150
151 if (put_user(sizeof(*head), len_ptr))
152 return -EFAULT;
153 return put_user(ptr_to_compat(head), head_ptr);
154
155err_unlock:
156 read_unlock(&tasklist_lock);
157
158 return ret;
159}
160
Ingo Molnar8f17d3a2006-03-27 01:16:27 -0800161asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
Ingo Molnar34f192c2006-03-27 01:16:24 -0800162 struct compat_timespec __user *utime, u32 __user *uaddr2,
Ingo Molnar8f17d3a2006-03-27 01:16:27 -0800163 u32 val3)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800164{
Pierre Peifferc19384b2007-05-09 02:35:02 -0700165 struct timespec ts;
166 ktime_t t, *tp = NULL;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800167 int val2 = 0;
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700168 int cmd = op & FUTEX_CMD_MASK;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800169
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700170 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -0700171 if (get_compat_timespec(&ts, utime))
Ingo Molnar34f192c2006-03-27 01:16:24 -0800172 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -0700173 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -0800174 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -0700175
176 t = timespec_to_ktime(ts);
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700177 if (cmd == FUTEX_WAIT)
Pierre Peifferc19384b2007-05-09 02:35:02 -0700178 t = ktime_add(ktime_get(), t);
179 tp = &t;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800180 }
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200181 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800182 val2 = (int) (unsigned long) utime;
183
Pierre Peifferc19384b2007-05-09 02:35:02 -0700184 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Ingo Molnar34f192c2006-03-27 01:16:24 -0800185}