Skip to contentSkip to conversation

fix bugs involving linux-user signal handling

PATCH Superseded Peter Maydell 3 participants 8 messages Apr 3, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover fix bugs involving linux-user signal handling

This patch series fixes bugs reported by Andrei Warkentin involving signal handling in linux-user mode. The first is Andrei's first patch (though I have tweaked the commit message a little). The second patch is aimed at fixing the locking bug that Andrei noted, in a somewhat simpler way than his patches use.

The test cases Andrei provided both pass with these patches.

(The tb_lock is a completely ill-thought-out idea anyway; maybe one day we'll actually sort out how multiple linux-user threads should interact with the TCG data structures. But for now this fixes an obvious bug.)

Andrei Warkentin (1):
page_check_range: don't bail out early after unprotecting page

Peter Maydell (1):
cpu-exec: Unlock tb_lock if we longjmp out of code generation

 cpu-exec.c      | 7 +++++++
 translate-all.c | 1 -
 2 files changed, 7 insertions(+), 1 deletion(-)
Signature · 1 line
1.9.0
#1 lore raw

PATCH 1/2 page_check_range: don't bail out early after unprotecting page

From: Andrei Warkentin <[email protected]>

When checking a page range, if we found that a page was made read-only by QEMU because it contained translated code, we were incorrectly returning immediately after unprotecting that page, rather than continuing to check the entire range, so we might fail to unprotect pages later in the range, or might incorrectly return a "success" result even if later pages were not writable.

In particular, this could cause segfaults in a case where signals are delivered back to back on a target architecture which uses trampoline code in the stack frame (as AArch64 currently does). The second signal causes a segfault because the frame cannot be written to (it was protected because we translated and executed the restorer trampoline, and the unprotect logic did not unprotect the whole range).

[PMM: expanded commit message a bit]
---
 translate-all.c | 1 -
 1 file changed, 1 deletion(-)
translate-all.c +0 -1
index f243c10..5759974 100644
@@ -1777,7 +1777,6 @@ int page_check_range(target_ulong start, target_ulong len, int flags)
                     return -1;
                 }
             }
-            return 0;
         }
     }
     return 0;
Signature · 1 line
1.9.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH for-2.0? 1/2] page_check_range: don't bail out early after unprotecting page

Quoted 18 lines · Peter Maydell

From: Andrei Warkentin <[email protected]>

When checking a page range, if we found that a page was made read-only by QEMU because it contained translated code, we were incorrectly returning immediately after unprotecting that page, rather than continuing to check the entire range, so we might fail to unprotect pages later in the range, or might incorrectly return a "success" result even if later pages were not writable.

In particular, this could cause segfaults in a case where signals are delivered back to back on a target architecture which uses trampoline code in the stack frame (as AArch64 currently does). The second signal causes a segfault because the frame cannot be written to (it was protected because we translated and executed the restorer trampoline, and the unprotect logic did not unprotect the whole range).

Quoted 6 lines
[PMM: expanded commit message a bit]
---
 translate-all.c | 1 -
 1 file changed, 1 deletion(-)

r~

#3 lore raw

PATCH 2/2 cpu-exec: Unlock tb_lock if we longjmp out of code generation

In reply to Peter Maydell

If the guest attempts to execute from unreadable memory, this will cause us to longjmp back to the main loop from inside the target frontend decoder. For linux-user mode, this means we will still hold the tb_ctx.tb_lock, and will deadlock when we try to start executing code again. Unlock the lock in the return-from-longjmp code path to avoid this.

---
 cpu-exec.c | 7 +++++++
 1 file changed, 7 insertions(+)
cpu-exec.c +7 -0
index 0914d3c..02168d9 100644
@@ -227,6 +227,8 @@ int cpu_exec(CPUArchState *env)
     TranslationBlock *tb;
     uint8_t *tc_ptr;
     uintptr_t next_tb;
+    /* This must be volatile so it is not trashed by longjmp() */
+    volatile bool have_tb_lock = false;
 
     if (cpu->halted) {
         if (!cpu_has_work(cpu)) {
@@ -600,6 +602,7 @@ int cpu_exec(CPUArchState *env)
                     cpu_loop_exit(cpu);
                 }
                 spin_lock(&tcg_ctx.tb_ctx.tb_lock);
+                have_tb_lock = true;
                 tb = tb_find_fast(env);
                 /* Note: we do it here to avoid a gcc bug on Mac OS X when
                    doing it in tb_find_slow */
@@ -621,6 +624,7 @@ int cpu_exec(CPUArchState *env)
                     tb_add_jump((TranslationBlock *)(next_tb & ~TB_EXIT_MASK),
                                 next_tb & TB_EXIT_MASK, tb);
                 }
+                have_tb_lock = false;
                 spin_unlock(&tcg_ctx.tb_ctx.tb_lock);
 
                 /* cpu_interrupt might be called while translating the
@@ -692,6 +696,9 @@ int cpu_exec(CPUArchState *env)
 #ifdef TARGET_I386
             x86_cpu = X86_CPU(cpu);
 #endif
+            if (have_tb_lock) {
+                spin_unlock(&tcg_ctx.tb_ctx.tb_lock);
+            }
         }
     } /* for(;;) */
 
Signature · 1 line
1.9.0
#4 lore raw

Re: [Qemu-devel] [PATCH for-2.0? 2/2] cpu-exec: Unlock tb_lock if we longjmp out of code generation

cpu-exec.c int cpu_exec(CPUArchState *env) · line 701

Quoted 3 lines · Peter Maydell
cpu-exec.c +3 -0
+            if (have_tb_lock) {
+                spin_unlock(&tcg_ctx.tb_ctx.tb_lock);
+            }

It ought not matter, since we ought to exit the loop on the next round, but i have a strong preference for resetting have_tb_lock here.

Otherwise, this is nicely self-contained.

r~

#5 lore raw

Re: [Qemu-devel] [PATCH for-2.0? 2/2] cpu-exec: Unlock tb_lock if we longjmp out of code generation

Quoted 8 lines · Richard Henderson

cpu-exec.c int cpu_exec(CPUArchState *env) · line 701

Quoted 3 lines · Peter Maydell
cpu-exec.c +3 -0
+            if (have_tb_lock) {
+                spin_unlock(&tcg_ctx.tb_ctx.tb_lock);
+            }

It ought not matter, since we ought to exit the loop on the next round, but i have a strong preference for resetting have_tb_lock here.

Absolutely -- dumb oversight on my part.

thanks
-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH for-2.0? 2/2] cpu-exec: Unlock tb_lock if we longjmp out of code generation

Hiya,

Cool. Definitely more compact and less intrusive, and definitely should catch more issues than the original page->flags check. The only possible cost is maintenance and debugging (implicit state and all that)... so... How about adding a comment around the "if (have_tb_lock)" to explain how we can get there?

2014-04-03 12:53 GMT-04:00 Peter Maydell <[email protected]>:

Quoted 14 lines · Richard Henderson
Quoted 8 lines · Peter Maydell

cpu-exec.c int cpu_exec(CPUArchState *env) · line 701

Quoted 3 lines
cpu-exec.c +3 -0
+            if (have_tb_lock) {
+                spin_unlock(&tcg_ctx.tb_ctx.tb_lock);
+            }

It ought not matter, since we ought to exit the loop on the next round, but i have a strong preference for resetting have_tb_lock here.

Absolutely -- dumb oversight on my part.

thanks
-- PMM

Signature · 1 line
A
#7 lore raw

Re: [Qemu-devel] [PATCH for-2.0? 2/2] cpu-exec: Unlock tb_lock if we longjmp out of code generation

Quoted 7 lines · Andrei E. Warkentin

Hiya,

Cool. Definitely more compact and less intrusive, and definitely should catch more issues than the original page->flags check. The only possible cost is maintenance and debugging (implicit state and all that)... so... How about adding a comment around the "if (have_tb_lock)" to explain how we can get there?

I dunno, it seems fairly obvious to me that if you get to this point with have_tb_lock true then it must be because you longjumped out of the codegen. (This happens for softmmu as well as linux-user, it's just softmmu doesn't actually do any tb locking).

thanks
-- PMM