Skip to contentSkip to conversation

linux-user: Fix stale tbs after mmap

PATCH In review Alexander Graf 2 participants 9 messages May 7, 2012 – May 11, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Fix stale tbs after mmap

If we execute linux-user code that does the following:

* A = mmap()
* execute code in A
* munmap(A)
* B = mmap(), but mmap returns the same address as A
* execute code in B

we end up executing a stale cached tb that contains translated code from A, while we want new code from B.

This patch adds a TB flush for mmap'ed regions, before we return them, avoiding the whole issue.

---
 linux-user/mmap.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
linux-user/mmap.c +2 -0
index 2620f88..390e940 100644
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     page_dump(stdout);
     printf("\n");
 #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
     mmap_unlock();
     return start;
 fail:
@@ -768,6 +769,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
         page_set_flags(old_addr, old_addr + old_size, 0);
         page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
     }
+    tb_invalidate_phys_page_range(new_addr, new_addr + new_size, 0);
     mmap_unlock();
     return new_addr;
 }
Signature · 1 line
1.6.0.2
#1 lore raw

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 592

Quoted 7 lines · Alexander Graf
linux-user/mmap.c +1 -0
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     page_dump(stdout);
     printf("\n");
  #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
     mmap_unlock();
     return start;

The comment at the top of tb_invalidate_phys_page_range() says "start and end must refer to the same physical page" -- is it out of date or does that not apply to user-mode?

Do you need to also invalidate the range on munmap() and mprotect-to-not-executable in order to correctly fault on the case of:
map something
execute it
unmap it
try to execute it again

? (haven't tested that case but it seems like it might be an issue)

-- PMM

#2 lore raw
Quoted 12 lines · Peter Maydell

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 592

Quoted 7 lines · Alexander Graf
linux-user/mmap.c +1 -0
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
    page_dump(stdout);
    printf("\n");
 #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
    mmap_unlock();
    return start;

The comment at the top of tb_invalidate_phys_page_range() says "start and end must refer to the same physical page" -- is it out of date or does that not apply to user-mode?

:(

No, you're right. It only flushes the first page.

Quoted 9 lines

Do you need to also invalidate the range on munmap() and mprotect-to-not-executable in order to correctly fault on the case of:
map something
execute it
unmap it
try to execute it again

? (haven't tested that case but it seems like it might be an issue)

I'm not sure. But it's an unrelated issue either way, right? :)
Could you please try to quickly write a test case for this one while I fix the patch?

Alex

#3 lore raw
In reply to Peter Maydell
Quoted 12 lines · Peter Maydell

Review abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 592

Quoted 7 lines · Alexander Graf
(quoted diff) +1 -0
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
    page_dump(stdout);
    printf("\n");
 #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
    mmap_unlock();
    return start;

The comment at the top of tb_invalidate_phys_page_range() says "start and end must refer to the same physical page" -- is it out of date or does that not apply to user-mode?

How about this one?

exec-all.h +2 -0
index c211242..7fcd76f 100644
@@ -96,6 +96,8 @@ void QEMU_NORETURN cpu_loop_exit(CPUState *env1);
 int page_unprotect(target_ulong address, unsigned long pc, void *puc);
 void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
                                    int is_cpu_write_access);
+void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end,
+                              int is_cpu_write_access);
 void tlb_flush_page(CPUState *env, target_ulong addr);
 void tlb_flush(CPUState *env, int flush_global);
 #if !defined(CONFIG_USER_ONLY)
exec.c +17 -0
index e71a82d..532b00c 100644
@@ -1016,6 +1016,23 @@ TranslationBlock *tb_gen_code(CPUState *env,
     return tb;
 }
 
+/*
+ * invalidate all TBs which intersect with the target physical pages
+ * starting in range [start;end[. NOTE: start and end may refer to
+ * different physical pages. 'is_cpu_write_access' should be true if called
+ * from a real cpu write access: the virtual CPU will exit the current
+ * TB if code is modified inside this TB.
+ */
+void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end,
+                              int is_cpu_write_access)
+{
+    while (start < end) {
+        tb_invalidate_phys_page_range(start, end, is_cpu_write_access);
+        start &= TARGET_PAGE_MASK;
+        start += TARGET_PAGE_SIZE;
+    }
+}
+
 /* invalidate all TBs which intersect with the target physical page
    starting in range [start;end[. NOTE: start and end must refer to
    the same physical page. 'is_cpu_write_access' should be true if called
linux-user/mmap.c +2 -0
index 2620f88..3611deb 100644
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     page_dump(stdout);
     printf("\n");
 #endif
+    tb_invalidate_phys_range(start, start + len, 0);
     mmap_unlock();
     return start;
 fail:
@@ -768,6 +769,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size,
         page_set_flags(old_addr, old_addr + old_size, 0);
         page_set_flags(new_addr, new_addr + new_size, prot | PAGE_VALID);
     }
+    tb_invalidate_phys_range(new_addr, new_addr + new_size, 0);
     mmap_unlock();
     return new_addr;
 }
#4 lore raw
In reply to Peter Maydell
Quoted 21 lines · Peter Maydell

linux-user/mmap.c abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 592

Quoted 7 lines · Alexander Graf
linux-user/mmap.c +1 -0
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
    page_dump(stdout);
    printf("\n");
 #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
    mmap_unlock();
    return start;

The comment at the top of tb_invalidate_phys_page_range() says "start and end must refer to the same physical page" -- is it out of date or does that not apply to user-mode?

Do you need to also invalidate the range on munmap() and mprotect-to-not-executable in order to correctly fault on the case of:
map something
execute it
unmap it
try to execute it again

Quoted 1 line

? (haven't tested that case but it seems like it might be an issue)

Yeah, the issue does exist:

#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

static int foo(void)

{
    return 5;
}

int main(int argc, char **argv)

{
    void *p;
    int x;
    int (*f)(void);

    p = mmap(NULL,0x1000,PROT_EXEC|PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,0,0);
    if (!p) {
        printf("Error: mmap returned failure\n");
        exit(1);
    }
    memcpy(p, (void*)foo, 0x10);
    f = p;
    x = f();
    printf("returned %d\n", x);
    munmap(p, 0x1000);
    x = f();
    printf("returned %d\n", x);
}

-----

baur:/> ./test
returned 5
returned 5

#5 lore raw
Quoted 24 lines · Alexander Graf
Quoted 21 lines · Peter Maydell

Review abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 592

Quoted 7 lines · Alexander Graf
(quoted diff) +1 -0
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
   page_dump(stdout);
   printf("\n");
 #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
   mmap_unlock();
   return start;

The comment at the top of tb_invalidate_phys_page_range() says "start and end must refer to the same physical page" -- is it out of date or does that not apply to user-mode?

Do you need to also invalidate the range on munmap() and mprotect-to-not-executable in order to correctly fault on the case of:
map something
execute it
unmap it
try to execute it again

Quoted 3 lines
Quoted 1 line

? (haven't tested that case but it seems like it might be an issue)

Yeah, the issue does exist:

And the below patch on top of my revised patch fixes it. The question is whether we still need to flush on mmap() then?

Alex

linux-user/mmap.c +3 -1
index 3611deb..bb4e752 100644
@@ -690,8 +690,10 @@ int target_munmap(abi_ulong start, abi_ulong len)
         }
     }
 
-    if (ret == 0)
+    if (ret == 0) {
         page_set_flags(start, start + len, 0);
+        tb_invalidate_phys_range(start, start + len, 0);
+    }
     mmap_unlock();
     return ret;
 }
#6 lore raw
Quoted 2 lines · Alexander Graf

And the below patch on top of my revised patch fixes it. The question is whether we still need to flush on mmap() then?

IIRC the kernel will let you MAP_FIXED mmap a file to an address that's already mmap'd for something else without having to munmap the previous mapping. So yes, we still need to flush on mmap I think.

Also I think you need mprotect() for the case of:
mmap(...,PROT_EXEC)
execute
mprotect(not PROT_EXEC)
try to execute again

-- PMM

#7 lore raw
In reply to Alexander Graf
Quoted 27 lines · Alexander Graf
Quoted 24 lines · Alexander Graf
Quoted 21 lines · Peter Maydell

Review abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 592

Quoted 7 lines · Alexander Graf
(quoted diff) +1 -0
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
    page_dump(stdout);
    printf("\n");
 #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
    mmap_unlock();
    return start;

The comment at the top of tb_invalidate_phys_page_range() says "start and end must refer to the same physical page" -- is it out of date or does that not apply to user-mode?

Do you need to also invalidate the range on munmap() and mprotect-to-not-executable in order to correctly fault on the case of:
map something
execute it
unmap it
try to execute it again

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

? (haven't tested that case but it seems like it might be an issue)

Yeah, the issue does exist:

And the below patch on top of my revised patch fixes it.

I think these two patches look correct (and as you pointed out on irc I was wrong about mprotect, which effectively already handles flushing the tb if needed). If you can roll them together into a single patch with a commit message and signed-off-by you can add my Reviewed-by: tag to it.

thanks
-- PMM

#8 lore raw
Quoted 32 lines · Peter Maydell
Quoted 31 lines · Alexander Graf
Quoted 27 lines · Alexander Graf
Quoted 21 lines · Peter Maydell

Review abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, · line 592

Quoted 7 lines · Alexander Graf
(quoted diff) +1 -0
@@ -587,6 +587,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
   page_dump(stdout);
   printf("\n");
 #endif
+    tb_invalidate_phys_page_range(start, start + len, 0);
   mmap_unlock();
   return start;

The comment at the top of tb_invalidate_phys_page_range() says "start and end must refer to the same physical page" -- is it out of date or does that not apply to user-mode?

Do you need to also invalidate the range on munmap() and mprotect-to-not-executable in order to correctly fault on the case of:
map something
execute it
unmap it
try to execute it again

Quoted 1 line

? (haven't tested that case but it seems like it might be an issue)

Yeah, the issue does exist:

Quoted 7 lines
Quoted 1 line

And the below patch on top of my revised patch fixes it.

I think these two patches look correct (and as you pointed out on irc I was wrong about mprotect, which effectively already handles flushing the tb if needed). If you can roll them together into a single patch with a commit message and signed-off-by you can add my Reviewed-by: tag to it.

Well, if we invalidate on unmap, do we need to invalidate on mmap() still? Or is only invalidating on unmap enough? Maybe when you use fixed addresses... hrm

Ah whatever, let's just flush everywhere now and then optimize later.

Alex