diff options
author | Shawn Bohrer <sbohrer@rgmadvisors.com> | 2011-06-30 12:21:32 -0400 |
---|---|---|
committer | Thomas Gleixner <tglx@linutronix.de> | 2011-07-26 14:59:35 -0400 |
commit | 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae (patch) | |
tree | 0d1ae4f7cee3af9b5ccb82a63e4e58fa091ae5ac /kernel | |
parent | d1e9ae47a0285d3f1699e8219ce50f656243b93f (diff) |
futex: Fix regression with read only mappings
commit 7485d0d3758e8e6491a5c9468114e74dc050785d (futexes: Remove rw
parameter from get_futex_key()) in 2.6.33 fixed two problems: First, It
prevented a loop when encountering a ZERO_PAGE. Second, it fixed RW
MAP_PRIVATE futex operations by forcing the COW to occur by
unconditionally performing a write access get_user_pages_fast() to get
the page. The commit also introduced a user-mode regression in that it
broke futex operations on read-only memory maps. For example, this
breaks workloads that have one or more reader processes doing a
FUTEX_WAIT on a futex within a read only shared file mapping, and a
writer processes that has a writable mapping issuing the FUTEX_WAKE.
This fixes the regression for valid futex operations on RO mappings by
trying a RO get_user_pages_fast() when the RW get_user_pages_fast()
fails. This change makes it necessary to also check for invalid use
cases, such as anonymous RO mappings (which can never change) and the
ZERO_PAGE which the commit referenced above was written to address.
This patch does restore the original behavior with RO MAP_PRIVATE
mappings, which have inherent user-mode usage problems and don't really
make sense. With this patch performing a FUTEX_WAIT within a RO
MAP_PRIVATE mapping will be successfully woken provided another process
updates the region of the underlying mapped file. However, the mmap()
man page states that for a MAP_PRIVATE mapping:
It is unspecified whether changes made to the file after
the mmap() call are visible in the mapped region.
So user-mode users attempting to use futex operations on RO MAP_PRIVATE
mappings are depending on unspecified behavior. Additionally a
RO MAP_PRIVATE mapping could fail to wake up in the following case.
Thread-A: call futex(FUTEX_WAIT, memory-region-A).
get_futex_key() return inode based key.
sleep on the key
Thread-B: call mprotect(PROT_READ|PROT_WRITE, memory-region-A)
Thread-B: write memory-region-A.
COW happen. This process's memory-region-A become related
to new COWed private (ie PageAnon=1) page.
Thread-B: call futex(FUETX_WAKE, memory-region-A).
get_futex_key() return mm based key.
IOW, we fail to wake up Thread-A.
Once again doing something like this is just silly and users who do
something like this get what they deserve.
While RO MAP_PRIVATE mappings are nonsensical, checking for a private
mapping requires walking the vmas and was deemed too costly to avoid a
userspace hang.
This Patch is based on Peter Zijlstra's initial patch with modifications to
only allow RO mappings for futex operations that need VERIFY_READ access.
Reported-by: David Oliver <david@rgmadvisors.com>
Signed-off-by: Shawn Bohrer <sbohrer@rgmadvisors.com>
Acked-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
Signed-off-by: Darren Hart <dvhart@linux.intel.com>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: peterz@infradead.org
Cc: eric.dumazet@gmail.com
Cc: zvonler@rgmadvisors.com
Cc: hughd@google.com
Link: http://lkml.kernel.org/r/1309450892-30676-1-git-send-email-sbohrer@rgmadvisors.com
Cc: stable@kernel.org
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Diffstat (limited to 'kernel')
-rw-r--r-- | kernel/futex.c | 54 |
1 files changed, 42 insertions, 12 deletions
diff --git a/kernel/futex.c b/kernel/futex.c index fe28dc282eae..70bb54bd2468 100644 --- a/kernel/futex.c +++ b/kernel/futex.c | |||
@@ -218,6 +218,8 @@ static void drop_futex_key_refs(union futex_key *key) | |||
218 | * @uaddr: virtual address of the futex | 218 | * @uaddr: virtual address of the futex |
219 | * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED | 219 | * @fshared: 0 for a PROCESS_PRIVATE futex, 1 for PROCESS_SHARED |
220 | * @key: address where result is stored. | 220 | * @key: address where result is stored. |
221 | * @rw: mapping needs to be read/write (values: VERIFY_READ, | ||
222 | * VERIFY_WRITE) | ||
221 | * | 223 | * |
222 | * Returns a negative error code or 0 | 224 | * Returns a negative error code or 0 |
223 | * The key words are stored in *key on success. | 225 | * The key words are stored in *key on success. |
@@ -229,12 +231,12 @@ static void drop_futex_key_refs(union futex_key *key) | |||
229 | * lock_page() might sleep, the caller should not hold a spinlock. | 231 | * lock_page() might sleep, the caller should not hold a spinlock. |
230 | */ | 232 | */ |
231 | static int | 233 | static int |
232 | get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key) | 234 | get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw) |
233 | { | 235 | { |
234 | unsigned long address = (unsigned long)uaddr; | 236 | unsigned long address = (unsigned long)uaddr; |
235 | struct mm_struct *mm = current->mm; | 237 | struct mm_struct *mm = current->mm; |
236 | struct page *page, *page_head; | 238 | struct page *page, *page_head; |
237 | int err; | 239 | int err, ro = 0; |
238 | 240 | ||
239 | /* | 241 | /* |
240 | * The futex address must be "naturally" aligned. | 242 | * The futex address must be "naturally" aligned. |
@@ -262,8 +264,18 @@ get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key) | |||
262 | 264 | ||
263 | again: | 265 | again: |
264 | err = get_user_pages_fast(address, 1, 1, &page); | 266 | err = get_user_pages_fast(address, 1, 1, &page); |
267 | /* | ||
268 | * If write access is not required (eg. FUTEX_WAIT), try | ||
269 | * and get read-only access. | ||
270 | */ | ||
271 | if (err == -EFAULT && rw == VERIFY_READ) { | ||
272 | err = get_user_pages_fast(address, 1, 0, &page); | ||
273 | ro = 1; | ||
274 | } | ||
265 | if (err < 0) | 275 | if (err < 0) |
266 | return err; | 276 | return err; |
277 | else | ||
278 | err = 0; | ||
267 | 279 | ||
268 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | 280 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
269 | page_head = page; | 281 | page_head = page; |
@@ -305,6 +317,13 @@ again: | |||
305 | if (!page_head->mapping) { | 317 | if (!page_head->mapping) { |
306 | unlock_page(page_head); | 318 | unlock_page(page_head); |
307 | put_page(page_head); | 319 | put_page(page_head); |
320 | /* | ||
321 | * ZERO_PAGE pages don't have a mapping. Avoid a busy loop | ||
322 | * trying to find one. RW mapping would have COW'd (and thus | ||
323 | * have a mapping) so this page is RO and won't ever change. | ||
324 | */ | ||
325 | if ((page_head == ZERO_PAGE(address))) | ||
326 | return -EFAULT; | ||
308 | goto again; | 327 | goto again; |
309 | } | 328 | } |
310 | 329 | ||
@@ -316,6 +335,15 @@ again: | |||
316 | * the object not the particular process. | 335 | * the object not the particular process. |
317 | */ | 336 | */ |
318 | if (PageAnon(page_head)) { | 337 | if (PageAnon(page_head)) { |
338 | /* | ||
339 | * A RO anonymous page will never change and thus doesn't make | ||
340 | * sense for futex operations. | ||
341 | */ | ||
342 | if (ro) { | ||
343 | err = -EFAULT; | ||
344 | goto out; | ||
345 | } | ||
346 | |||
319 | key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */ | 347 | key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */ |
320 | key->private.mm = mm; | 348 | key->private.mm = mm; |
321 | key->private.address = address; | 349 | key->private.address = address; |
@@ -327,9 +355,10 @@ again: | |||
327 | 355 | ||
328 | get_futex_key_refs(key); | 356 | get_futex_key_refs(key); |
329 | 357 | ||
358 | out: | ||
330 | unlock_page(page_head); | 359 | unlock_page(page_head); |
331 | put_page(page_head); | 360 | put_page(page_head); |
332 | return 0; | 361 | return err; |
333 | } | 362 | } |
334 | 363 | ||
335 | static inline void put_futex_key(union futex_key *key) | 364 | static inline void put_futex_key(union futex_key *key) |
@@ -940,7 +969,7 @@ futex_wake(u32 __user *uaddr, unsigned int flags, int nr_wake, u32 bitset) | |||
940 | if (!bitset) | 969 | if (!bitset) |
941 | return -EINVAL; | 970 | return -EINVAL; |
942 | 971 | ||
943 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key); | 972 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_READ); |
944 | if (unlikely(ret != 0)) | 973 | if (unlikely(ret != 0)) |
945 | goto out; | 974 | goto out; |
946 | 975 | ||
@@ -986,10 +1015,10 @@ futex_wake_op(u32 __user *uaddr1, unsigned int flags, u32 __user *uaddr2, | |||
986 | int ret, op_ret; | 1015 | int ret, op_ret; |
987 | 1016 | ||
988 | retry: | 1017 | retry: |
989 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1); | 1018 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ); |
990 | if (unlikely(ret != 0)) | 1019 | if (unlikely(ret != 0)) |
991 | goto out; | 1020 | goto out; |
992 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2); | 1021 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE); |
993 | if (unlikely(ret != 0)) | 1022 | if (unlikely(ret != 0)) |
994 | goto out_put_key1; | 1023 | goto out_put_key1; |
995 | 1024 | ||
@@ -1243,10 +1272,11 @@ retry: | |||
1243 | pi_state = NULL; | 1272 | pi_state = NULL; |
1244 | } | 1273 | } |
1245 | 1274 | ||
1246 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1); | 1275 | ret = get_futex_key(uaddr1, flags & FLAGS_SHARED, &key1, VERIFY_READ); |
1247 | if (unlikely(ret != 0)) | 1276 | if (unlikely(ret != 0)) |
1248 | goto out; | 1277 | goto out; |
1249 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2); | 1278 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, |
1279 | requeue_pi ? VERIFY_WRITE : VERIFY_READ); | ||
1250 | if (unlikely(ret != 0)) | 1280 | if (unlikely(ret != 0)) |
1251 | goto out_put_key1; | 1281 | goto out_put_key1; |
1252 | 1282 | ||
@@ -1790,7 +1820,7 @@ static int futex_wait_setup(u32 __user *uaddr, u32 val, unsigned int flags, | |||
1790 | * while the syscall executes. | 1820 | * while the syscall executes. |
1791 | */ | 1821 | */ |
1792 | retry: | 1822 | retry: |
1793 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key); | 1823 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q->key, VERIFY_READ); |
1794 | if (unlikely(ret != 0)) | 1824 | if (unlikely(ret != 0)) |
1795 | return ret; | 1825 | return ret; |
1796 | 1826 | ||
@@ -1941,7 +1971,7 @@ static int futex_lock_pi(u32 __user *uaddr, unsigned int flags, int detect, | |||
1941 | } | 1971 | } |
1942 | 1972 | ||
1943 | retry: | 1973 | retry: |
1944 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key); | 1974 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &q.key, VERIFY_WRITE); |
1945 | if (unlikely(ret != 0)) | 1975 | if (unlikely(ret != 0)) |
1946 | goto out; | 1976 | goto out; |
1947 | 1977 | ||
@@ -2060,7 +2090,7 @@ retry: | |||
2060 | if ((uval & FUTEX_TID_MASK) != vpid) | 2090 | if ((uval & FUTEX_TID_MASK) != vpid) |
2061 | return -EPERM; | 2091 | return -EPERM; |
2062 | 2092 | ||
2063 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key); | 2093 | ret = get_futex_key(uaddr, flags & FLAGS_SHARED, &key, VERIFY_WRITE); |
2064 | if (unlikely(ret != 0)) | 2094 | if (unlikely(ret != 0)) |
2065 | goto out; | 2095 | goto out; |
2066 | 2096 | ||
@@ -2249,7 +2279,7 @@ static int futex_wait_requeue_pi(u32 __user *uaddr, unsigned int flags, | |||
2249 | debug_rt_mutex_init_waiter(&rt_waiter); | 2279 | debug_rt_mutex_init_waiter(&rt_waiter); |
2250 | rt_waiter.task = NULL; | 2280 | rt_waiter.task = NULL; |
2251 | 2281 | ||
2252 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2); | 2282 | ret = get_futex_key(uaddr2, flags & FLAGS_SHARED, &key2, VERIFY_WRITE); |
2253 | if (unlikely(ret != 0)) | 2283 | if (unlikely(ret != 0)) |
2254 | goto out; | 2284 | goto out; |
2255 | 2285 | ||