blob: 00b572666cc76178d81979f512dc9e3078b08fda [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
Ingo Molnar34f192c2006-03-27 01:16:24 -080033/*
34 * Walk curr->robust_list (very carefully, it's a userspace list!)
35 * and mark any locks found there dead, and notify any waiters.
36 *
37 * We silently return on any sign of list-walking problem.
38 */
39void compat_exit_robust_list(struct task_struct *curr)
40{
41 struct compat_robust_list_head __user *head = curr->compat_robust_list;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070042 struct robust_list __user *entry, *next_entry, *pending;
43 unsigned int limit = ROBUST_LIST_LIMIT, pi, next_pi, pip;
44 compat_uptr_t uentry, next_uentry, upending;
Ingo Molnar34f192c2006-03-27 01:16:24 -080045 compat_long_t futex_offset;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070046 int rc;
Ingo Molnar34f192c2006-03-27 01:16:24 -080047
48 /*
49 * Fetch the list head (which was registered earlier, via
50 * sys_set_robust_list()):
51 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +020052 if (fetch_robust_entry(&uentry, &entry, &head->list.next, &pi))
Ingo Molnar34f192c2006-03-27 01:16:24 -080053 return;
Ingo Molnar34f192c2006-03-27 01:16:24 -080054 /*
55 * Fetch the relative futex offset:
56 */
57 if (get_user(futex_offset, &head->futex_offset))
58 return;
59 /*
60 * Fetch any possibly pending lock-add first, and handle it
61 * if it exists:
62 */
Ingo Molnare3f2dde2006-07-29 05:17:57 +020063 if (fetch_robust_entry(&upending, &pending,
Thomas Gleixnerce2c6b52006-08-05 12:15:15 -070064 &head->list_op_pending, &pip))
Ingo Molnar34f192c2006-03-27 01:16:24 -080065 return;
Ingo Molnar34f192c2006-03-27 01:16:24 -080066
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070067 next_entry = NULL; /* avoid warning with gcc */
Arnd Bergmann179c85e2007-09-11 15:23:49 -070068 while (entry != (struct robust_list __user *) &head->list) {
Ingo Molnar34f192c2006-03-27 01:16:24 -080069 /*
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070070 * Fetch the next entry in the list before calling
71 * handle_futex_death:
72 */
73 rc = fetch_robust_entry(&next_uentry, &next_entry,
74 (compat_uptr_t __user *)&entry->next, &next_pi);
75 /*
Ingo Molnar34f192c2006-03-27 01:16:24 -080076 * A pending lock might already be on the list, so
77 * dont process it twice:
78 */
79 if (entry != pending)
Al Viroba46df92006-10-10 22:46:07 +010080 if (handle_futex_death((void __user *)entry + futex_offset,
Ingo Molnare3f2dde2006-07-29 05:17:57 +020081 curr, pi))
Ingo Molnar34f192c2006-03-27 01:16:24 -080082 return;
83
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070084 if (rc)
Ingo Molnar34f192c2006-03-27 01:16:24 -080085 return;
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070086 uentry = next_uentry;
87 entry = next_entry;
88 pi = next_pi;
Ingo Molnar34f192c2006-03-27 01:16:24 -080089 /*
90 * Avoid excessively long or circular lists:
91 */
92 if (!--limit)
93 break;
94
95 cond_resched();
96 }
Martin Schwidefsky9f96cb12007-10-01 01:20:13 -070097 if (pending)
98 handle_futex_death((void __user *)pending + futex_offset,
99 curr, pip);
Ingo Molnar34f192c2006-03-27 01:16:24 -0800100}
101
102asmlinkage long
103compat_sys_set_robust_list(struct compat_robust_list_head __user *head,
104 compat_size_t len)
105{
106 if (unlikely(len != sizeof(*head)))
107 return -EINVAL;
108
109 current->compat_robust_list = head;
110
111 return 0;
112}
113
114asmlinkage long
Al Viroba46df92006-10-10 22:46:07 +0100115compat_sys_get_robust_list(int pid, compat_uptr_t __user *head_ptr,
Ingo Molnar34f192c2006-03-27 01:16:24 -0800116 compat_size_t __user *len_ptr)
117{
Al Viroba46df92006-10-10 22:46:07 +0100118 struct compat_robust_list_head __user *head;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800119 unsigned long ret;
120
121 if (!pid)
122 head = current->compat_robust_list;
123 else {
124 struct task_struct *p;
125
126 ret = -ESRCH;
127 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -0700128 p = find_task_by_vpid(pid);
Ingo Molnar34f192c2006-03-27 01:16:24 -0800129 if (!p)
130 goto err_unlock;
131 ret = -EPERM;
132 if ((current->euid != p->euid) && (current->euid != p->uid) &&
133 !capable(CAP_SYS_PTRACE))
134 goto err_unlock;
135 head = p->compat_robust_list;
136 read_unlock(&tasklist_lock);
137 }
138
139 if (put_user(sizeof(*head), len_ptr))
140 return -EFAULT;
141 return put_user(ptr_to_compat(head), head_ptr);
142
143err_unlock:
144 read_unlock(&tasklist_lock);
145
146 return ret;
147}
148
Ingo Molnar8f17d3a2006-03-27 01:16:27 -0800149asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
Ingo Molnar34f192c2006-03-27 01:16:24 -0800150 struct compat_timespec __user *utime, u32 __user *uaddr2,
Ingo Molnar8f17d3a2006-03-27 01:16:27 -0800151 u32 val3)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800152{
Pierre Peifferc19384b2007-05-09 02:35:02 -0700153 struct timespec ts;
154 ktime_t t, *tp = NULL;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800155 int val2 = 0;
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700156 int cmd = op & FUTEX_CMD_MASK;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800157
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700158 if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI)) {
Pierre Peifferc19384b2007-05-09 02:35:02 -0700159 if (get_compat_timespec(&ts, utime))
Ingo Molnar34f192c2006-03-27 01:16:24 -0800160 return -EFAULT;
Pierre Peifferc19384b2007-05-09 02:35:02 -0700161 if (!timespec_valid(&ts))
Thomas Gleixner9741ef962006-03-31 02:31:32 -0800162 return -EINVAL;
Pierre Peifferc19384b2007-05-09 02:35:02 -0700163
164 t = timespec_to_ktime(ts);
Ulrich Drepperf0ede662007-06-01 00:46:41 -0700165 if (cmd == FUTEX_WAIT)
Pierre Peifferc19384b2007-05-09 02:35:02 -0700166 t = ktime_add(ktime_get(), t);
167 tp = &t;
Ingo Molnar34f192c2006-03-27 01:16:24 -0800168 }
Thomas Gleixnerbd197232007-06-17 21:11:10 +0200169 if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE)
Ingo Molnar34f192c2006-03-27 01:16:24 -0800170 val2 = (int) (unsigned long) utime;
171
Pierre Peifferc19384b2007-05-09 02:35:02 -0700172 return do_futex(uaddr, op, val, tp, uaddr2, val2, val3);
Ingo Molnar34f192c2006-03-27 01:16:24 -0800173}