Skip to contentSkip to conversation

accel/tcg: Fixes for user-only page tracking

PATCH In review Richard Henderson 4 participants 26 messages Dec 24, 2022 – Jan 20, 2023

lore
Share & tools
lore
#0 lore raw

cover accel/tcg: Fixes for user-only page tracking

Fix all three bugs pointed out by Ilya, and for the moment at least, adjust the testcase to set read+write for writability.

r~

Ilya Leoshkevich (1):
tests/tcg/multiarch: add vma-pthread.c

Richard Henderson (3):
accel/tcg: Fix tb_invalidate_phys_page_unwind accel/tcg: Use g_free_rcu for user-exec interval trees accel/tcg: Handle false negative lookup in page_check_range

 tests/tcg/multiarch/nop_func.h       |  25 ++++
 accel/tcg/tb-maint.c                 |  78 +++++-----
 accel/tcg/user-exec.c                |  57 ++++++--
 tests/tcg/multiarch/munmap-pthread.c |  16 +--
 tests/tcg/multiarch/vma-pthread.c    | 207 +++++++++++++++++++++++++++
 tests/tcg/multiarch/Makefile.target  |   3 +
 6 files changed, 321 insertions(+), 65 deletions(-)
 create mode 100644 tests/tcg/multiarch/nop_func.h
 create mode 100644 tests/tcg/multiarch/vma-pthread.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 accel/tcg: Fix tb_invalidate_phys_page_unwind

When called from syscall(), we are not within a TB and pc == 0. We can skip the check for invalidating the current TB.

---
 accel/tcg/tb-maint.c | 78 ++++++++++++++++++++++++--------------------
 1 file changed, 43 insertions(+), 35 deletions(-)
accel/tcg/tb-maint.c +43 -35
index 1b8e860647..b3d6529ae2 100644
@@ -1024,43 +1024,51 @@ void tb_invalidate_phys_page(tb_page_addr_t addr)
  */
 bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
 {
-    assert(pc != 0);
-#ifdef TARGET_HAS_PRECISE_SMC
-    assert_memory_lock();
-    {
-        TranslationBlock *current_tb = tcg_tb_lookup(pc);
-        bool current_tb_modified = false;
-        TranslationBlock *tb;
-        PageForEachNext n;
+    TranslationBlock *current_tb;
+    bool current_tb_modified;
+    TranslationBlock *tb;
+    PageForEachNext n;
 
-        addr &= TARGET_PAGE_MASK;
-
-        PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) {
-            if (current_tb == tb &&
-                (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
-                /*
-                 * If we are modifying the current TB, we must stop its
-                 * execution. We could be more precise by checking that
-                 * the modification is after the current PC, but it would
-                 * require a specialized function to partially restore
-                 * the CPU state.
-                 */
-                current_tb_modified = true;
-                cpu_restore_state_from_tb(current_cpu, current_tb, pc);
-            }
-            tb_phys_invalidate__locked(tb);
-        }
-
-        if (current_tb_modified) {
-            /* Force execution of one insn next time.  */
-            CPUState *cpu = current_cpu;
-            cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
-            return true;
-        }
+    /*
+     * Without precise smc semantics, or when outside of a TB,
+     * we can skip to invalidate.
+     */
+#ifndef TARGET_HAS_PRECISE_SMC
+    pc = 0;
+#endif
+    if (!pc) {
+        tb_invalidate_phys_page(addr);
+        return false;
+    }
+
+    assert_memory_lock();
+    current_tb = tcg_tb_lookup(pc);
+
+    addr &= TARGET_PAGE_MASK;
+    current_tb_modified = false;
+
+    PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) {
+        if (current_tb == tb &&
+            (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
+            /*
+             * If we are modifying the current TB, we must stop its
+             * execution. We could be more precise by checking that
+             * the modification is after the current PC, but it would
+             * require a specialized function to partially restore
+             * the CPU state.
+             */
+            current_tb_modified = true;
+            cpu_restore_state_from_tb(current_cpu, current_tb, pc);
+        }
+        tb_phys_invalidate__locked(tb);
+    }
+
+    if (current_tb_modified) {
+        /* Force execution of one insn next time.  */
+        CPUState *cpu = current_cpu;
+        cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
+        return true;
     }
-#else
-    tb_invalidate_phys_page(addr);
-#endif /* TARGET_HAS_PRECISE_SMC */
     return false;
 }
 #else
Signature · 1 line
2.34.1
#2 lore raw

PATCH 1/1 accel/tcg: Fix tb_invalidate_phys_page_unwind

When called from syscall(), we are not within a TB and pc == 0. We can skip the check for invalidating the current TB.

---
 accel/tcg/tb-maint.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
accel/tcg/tb-maint.c +12 -5
index 1b8e860647..c9b8d3c6c3 100644
@@ -1024,8 +1024,18 @@ void tb_invalidate_phys_page(tb_page_addr_t addr)
  */
 bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
 {
-    assert(pc != 0);
-#ifdef TARGET_HAS_PRECISE_SMC
+    /*
+     * Without precise smc semantics, or when outside of a TB,
+     * we can skip to invalidate.
+     */
+#ifndef TARGET_HAS_PRECISE_SMC
+    pc = 0;
+#endif
+    if (!pc) {
+        tb_invalidate_phys_page(addr);
+        return false;
+    }
+
     assert_memory_lock();
     {
         TranslationBlock *current_tb = tcg_tb_lookup(pc);
@@ -1058,9 +1068,6 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
             return true;
         }
     }
-#else
-    tb_invalidate_phys_page(addr);
-#endif /* TARGET_HAS_PRECISE_SMC */
     return false;
 }
 #else
Signature · 1 line
2.38.1
Reviewed-by on v1 1/1
#3 lore raw

Re: [PATCH 1a/4] accel/tcg: Fix tb_invalidate_phys_page_unwind

Quoted 9 lines · Philippe Mathieu-Daudé

When called from syscall(), we are not within a TB and pc == 0. We can skip the check for invalidating the current TB.

---
  accel/tcg/tb-maint.c | 17 ++++++++++++-----
  1 file changed, 12 insertions(+), 5 deletions(-)
#4 lore raw

PATCH 1/1 accel/tcg: Unindent tb_invalidate_phys_page_unwind

In reply to Philippe Mathieu-Daudé
---
 accel/tcg/tb-maint.c | 53 ++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 26 deletions(-)
accel/tcg/tb-maint.c +27 -26
index c9b8d3c6c3..b3d6529ae2 100644
@@ -1024,6 +1024,11 @@ void tb_invalidate_phys_page(tb_page_addr_t addr)
  */
 bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
 {
+    TranslationBlock *current_tb;
+    bool current_tb_modified;
+    TranslationBlock *tb;
+    PageForEachNext n;
+
     /*
      * Without precise smc semantics, or when outside of a TB,
      * we can skip to invalidate.
@@ -1037,36 +1042,32 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc)
     }
 
     assert_memory_lock();
-    {
-        TranslationBlock *current_tb = tcg_tb_lookup(pc);
-        bool current_tb_modified = false;
-        TranslationBlock *tb;
-        PageForEachNext n;
+    current_tb = tcg_tb_lookup(pc);
 
-        addr &= TARGET_PAGE_MASK;
+    addr &= TARGET_PAGE_MASK;
+    current_tb_modified = false;
 
-        PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) {
-            if (current_tb == tb &&
-                (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
-                /*
-                 * If we are modifying the current TB, we must stop its
-                 * execution. We could be more precise by checking that
-                 * the modification is after the current PC, but it would
-                 * require a specialized function to partially restore
-                 * the CPU state.
-                 */
-                current_tb_modified = true;
-                cpu_restore_state_from_tb(current_cpu, current_tb, pc);
-            }
-            tb_phys_invalidate__locked(tb);
+    PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) {
+        if (current_tb == tb &&
+            (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) {
+            /*
+             * If we are modifying the current TB, we must stop its
+             * execution. We could be more precise by checking that
+             * the modification is after the current PC, but it would
+             * require a specialized function to partially restore
+             * the CPU state.
+             */
+            current_tb_modified = true;
+            cpu_restore_state_from_tb(current_cpu, current_tb, pc);
         }
+        tb_phys_invalidate__locked(tb);
+    }
 
-        if (current_tb_modified) {
-            /* Force execution of one insn next time.  */
-            CPUState *cpu = current_cpu;
-            cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
-            return true;
-        }
+    if (current_tb_modified) {
+        /* Force execution of one insn next time.  */
+        CPUState *cpu = current_cpu;
+        cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu);
+        return true;
     }
     return false;
 }
Signature · 1 line
2.38.1
Reviewed-by on v1 1/1
#5 lore raw

Re: [PATCH 1b/4] accel/tcg: Unindent tb_invalidate_phys_page_unwind

Quoted 6 lines · Philippe Mathieu-Daudé
---
  accel/tcg/tb-maint.c | 53 ++++++++++++++++++++++----------------------
  1 file changed, 27 insertions(+), 26 deletions(-)
#6 lore raw

PATCH 2/4 accel/tcg: Use g_free_rcu for user-exec interval trees

In reply to Richard Henderson

Because we allow lockless lookups, we have to be careful when it is freed. Use rcu to delay the free until safe.

---
 accel/tcg/user-exec.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
accel/tcg/user-exec.c +10 -8
index a3cecda405..2c5c10d2e6 100644
@@ -22,6 +22,7 @@
 #include "exec/exec-all.h"
 #include "tcg/tcg.h"
 #include "qemu/bitops.h"
+#include "qemu/rcu.h"
 #include "exec/cpu_ldst.h"
 #include "exec/translate-all.h"
 #include "exec/helper-proto.h"
@@ -136,6 +137,7 @@ bool handle_sigsegv_accerr_write(CPUState *cpu, sigset_t *old_set,
 }
 
 typedef struct PageFlagsNode {
+    struct rcu_head rcu;
     IntervalTreeNode itree;
     int flags;
 } PageFlagsNode;
@@ -266,7 +268,7 @@ static bool pageflags_unset(target_ulong start, target_ulong last)
             }
         } else if (p_last <= last) {
             /* Range completely covers node -- remove it. */
-            g_free(p);
+            g_free_rcu(p, rcu);
         } else {
             /* Truncate the node from the start. */
             p->itree.start = last + 1;
@@ -311,7 +313,7 @@ static void pageflags_create_merge(target_ulong start, target_ulong last,
     if (prev) {
         if (next) {
             prev->itree.last = next->itree.last;
-            g_free(next);
+            g_free_rcu(next, rcu);
         } else {
             prev->itree.last = last;
         }
@@ -376,7 +378,7 @@ static bool pageflags_set_clear(target_ulong start, target_ulong last,
             p->flags = merge_flags;
         } else {
             interval_tree_remove(&p->itree, &pageflags_root);
-            g_free(p);
+            g_free_rcu(p, rcu);
         }
         goto done;
     }
@@ -421,7 +423,7 @@ static bool pageflags_set_clear(target_ulong start, target_ulong last,
                     p->flags = merge_flags;
                 } else {
                     interval_tree_remove(&p->itree, &pageflags_root);
-                    g_free(p);
+                    g_free_rcu(p, rcu);
                 }
                 if (p_last < last) {
                     start = p_last + 1;
@@ -462,7 +464,7 @@ static bool pageflags_set_clear(target_ulong start, target_ulong last,
         p->itree.start = last + 1;
         interval_tree_insert(&p->itree, &pageflags_root);
     } else {
-        g_free(p);
+        g_free_rcu(p, rcu);
         goto restart;
     }
     if (set_flags) {
@@ -779,6 +781,7 @@ tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
 #define TBD_MASK   (TARGET_PAGE_MASK * TPD_PAGES)
 
 typedef struct TargetPageDataNode {
+    struct rcu_head rcu;
     IntervalTreeNode itree;
     char data[TPD_PAGES][TARGET_PAGE_DATA_SIZE] __attribute__((aligned));
 } TargetPageDataNode;
@@ -801,11 +804,11 @@ void page_reset_target_data(target_ulong start, target_ulong end)
          n = next,
          next = next ? interval_tree_iter_next(n, start, last) : NULL) {
         target_ulong n_start, n_last, p_ofs, p_len;
-        TargetPageDataNode *t;
+        TargetPageDataNode *t = container_of(n, TargetPageDataNode, itree);
 
         if (n->start >= start && n->last <= last) {
             interval_tree_remove(n, &targetdata_root);
-            g_free(n);
+            g_free_rcu(t, rcu);
             continue;
         }
 
@@ -819,7 +822,6 @@ void page_reset_target_data(target_ulong start, target_ulong end)
         n_last = MIN(last, n->last);
         p_len = (n_last + 1 - n_start) >> TARGET_PAGE_BITS;
 
-        t = container_of(n, TargetPageDataNode, itree);
         memset(t->data[p_ofs], 0, p_len * TARGET_PAGE_DATA_SIZE);
     }
 }
Signature · 1 line
2.34.1
Reviewed-by on v1 2/4
#7 lore raw

Re: [PATCH 2/4] accel/tcg: Use g_free_rcu for user-exec interval trees

Quoted 7 lines · Richard Henderson

Because we allow lockless lookups, we have to be careful when it is freed. Use rcu to delay the free until safe.

---
  accel/tcg/user-exec.c | 18 ++++++++++--------
  1 file changed, 10 insertions(+), 8 deletions(-)
#8 lore raw

PATCH 3/4 accel/tcg: Handle false negative lookup in page_check_range

In reply to Richard Henderson

As in page_get_flags, we need to try again with the mmap lock held if we fail a page lookup.

---
 accel/tcg/user-exec.c | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)
accel/tcg/user-exec.c +32 -7
index 2c5c10d2e6..c72a806203 100644
@@ -525,6 +525,7 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
 int page_check_range(target_ulong start, target_ulong len, int flags)
 {
     target_ulong last;
+    int locked, ret;
 
     if (len == 0) {
         return 0;  /* trivial length */
@@ -535,42 +536,66 @@ int page_check_range(target_ulong start, target_ulong len, int flags)
         return -1; /* wrap around */
     }
 
+    locked = have_mmap_lock();
     while (true) {
         PageFlagsNode *p = pageflags_find(start, last);
         int missing;
 
         if (!p) {
-            return -1; /* entire region invalid */
+            if (!locked) {
+                /*
+                 * Lockless lookups have false negatives.
+                 * Retry with the lock held.
+                 */
+                mmap_lock();
+                locked = -1;
+                p = pageflags_find(start, last);
+            }
+            if (!p) {
+                ret = -1; /* entire region invalid */
+                break;
+            }
         }
         if (start < p->itree.start) {
-            return -1; /* initial bytes invalid */
+            ret = -1; /* initial bytes invalid */
+            break;
         }
 
         missing = flags & ~p->flags;
         if (missing & PAGE_READ) {
-            return -1; /* page not readable */
+            ret = -1; /* page not readable */
+            break;
         }
         if (missing & PAGE_WRITE) {
             if (!(p->flags & PAGE_WRITE_ORG)) {
-                return -1; /* page not writable */
+                ret = -1; /* page not writable */
+                break;
             }
             /* Asking about writable, but has been protected: undo. */
             if (!page_unprotect(start, 0)) {
-                return -1;
+                ret = -1;
+                break;
             }
             /* TODO: page_unprotect should take a range, not a single page. */
             if (last - start < TARGET_PAGE_SIZE) {
-                return 0; /* ok */
+                ret = 0; /* ok */
+                break;
             }
             start += TARGET_PAGE_SIZE;
             continue;
         }
 
         if (last <= p->itree.last) {
-            return 0; /* ok */
+            ret = 0; /* ok */
+            break;
         }
         start = p->itree.last + 1;
     }
+
+    if (locked < 0) {
+        mmap_unlock();
+    }
+    return ret;
 }
 
 void page_protect(tb_page_addr_t address)
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range

Quoted 13 lines · Richard Henderson

As in page_get_flags, we need to try again with the mmap lock held if we fail a page lookup.

---
  accel/tcg/user-exec.c | 39 ++++++++++++++++++++++++++++++++-------
  1 file changed, 32 insertions(+), 7 deletions(-)
accel/tcg/user-exec.c +0 -0
index 2c5c10d2e6..c72a806203 100644

accel/tcg/user-exec.c void page_set_flags(target_ulong start, target_ulong end, int flags) · line 528

Quoted 4 lines
accel/tcg/user-exec.c +1 -0
@@ -525,6 +525,7 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
  int page_check_range(target_ulong start, target_ulong len, int flags)
  {
      target_ulong last;
+    int locked, ret;

have_mmap_lock() returns a boolean, can we declare 'locked' as such, ...

Quoted 11 lines
accel/tcg/user-exec.c +1 -0
      if (len == 0) {
          return 0;  /* trivial length */
@@ -535,42 +536,66 @@ int page_check_range(target_ulong start, target_ulong len, int flags)
          return -1; /* wrap around */
      }
 
+    locked = have_mmap_lock();
      while (true) {
          PageFlagsNode *p = pageflags_find(start, last);
          int missing;

accel/tcg/user-exec.c int page_check_range(target_ulong start, target_ulong len, int flags) · line 551

Quoted 10 lines
accel/tcg/user-exec.c +7 -1
 
          if (!p) {
-            return -1; /* entire region invalid */
+            if (!locked) {
+                /*
+                 * Lockless lookups have false negatives.
+                 * Retry with the lock held.
+                 */
+                mmap_lock();
+                locked = -1;

... skip this confusing assignation, ...

Quoted 40 lines
accel/tcg/user-exec.c +16 -5
+                p = pageflags_find(start, last);
+            }
+            if (!p) {
+                ret = -1; /* entire region invalid */
+                break;
+            }
          }
          if (start < p->itree.start) {
-            return -1; /* initial bytes invalid */
+            ret = -1; /* initial bytes invalid */
+            break;
          }
 
          missing = flags & ~p->flags;
          if (missing & PAGE_READ) {
-            return -1; /* page not readable */
+            ret = -1; /* page not readable */
+            break;
          }
          if (missing & PAGE_WRITE) {
              if (!(p->flags & PAGE_WRITE_ORG)) {
-                return -1; /* page not writable */
+                ret = -1; /* page not writable */
+                break;
              }
              /* Asking about writable, but has been protected: undo. */
              if (!page_unprotect(start, 0)) {
-                return -1;
+                ret = -1;
+                break;
              }
              /* TODO: page_unprotect should take a range, not a single page. */
              if (last - start < TARGET_PAGE_SIZE) {
-                return 0; /* ok */
+                ret = 0; /* ok */
+                break;
              }
              start += TARGET_PAGE_SIZE;
              continue;
          }

accel/tcg/user-exec.c int page_check_range(target_ulong start, target_ulong len, int flags) · line 595

Quoted 10 lines
accel/tcg/user-exec.c +4 -1
 
          if (last <= p->itree.last) {
-            return 0; /* ok */
+            ret = 0; /* ok */
+            break;
          }
          start = p->itree.last + 1;
      }
+
+    if (locked < 0) {

... and check for !locked here?

Quoted 4 lines
accel/tcg/user-exec.c +3 -0
+        mmap_unlock();
+    }
+    return ret;
  }
#10 lore raw

Re: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range

Quoted 94 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Richard Henderson

As in page_get_flags, we need to try again with the mmap lock held if we fail a page lookup.

---
  accel/tcg/user-exec.c | 39 ++++++++++++++++++++++++++++++++-------

  1 file changed, 32 insertions(+), 7 deletions(-)
accel/tcg/user-exec.c +0 -0
index 2c5c10d2e6..c72a806203 100644

accel/tcg/user-exec.c void page_set_flags(target_ulong start, · line 529

Quoted 5 lines
accel/tcg/user-exec.c +1 -0
@@ -525,6 +525,7 @@ void page_set_flags(target_ulong start, 
 target_ulong end, int flags)
   int page_check_range(target_ulong start, target_ulong len, int flags)
   {
       target_ulong last;
+    int locked, ret;

have_mmap_lock() returns a boolean, can we declare 'locked' as such, ...

Quoted 11 lines
accel/tcg/user-exec.c +0 -0
       if (len == 0) {

          return 0;  /* trivial length */

accel/tcg/user-exec.c +0 -0
@@ -535,42 +536,66 @@ int page_check_range(target_ulong start, 
 target_ulong len, int flags)

          return -1; /* wrap around */
      }
+    locked = have_mmap_lock();
      while (true) {
          PageFlagsNode *p = pageflags_find(start, last); int missing;
          if (!p) {

Quoted 8 lines

-            return -1; /* entire region invalid */ +            if (!locked) {
+                /*
+                 * Lockless lookups have false negatives. +                 * Retry with the lock held.
+                 */
+                mmap_lock();
+                locked = -1;

... skip this confusing assignation, ...

Quoted 40 lines

+                p = pageflags_find(start, last);
+            }
+            if (!p) {
+                ret = -1; /* entire region invalid */
+                break;
+            }
          }
          if (start < p->itree.start) {
-            return -1; /* initial bytes invalid */
+            ret = -1; /* initial bytes invalid */
+            break;
          }
          missing = flags & ~p->flags;
          if (missing & PAGE_READ) {
-            return -1; /* page not readable */
+            ret = -1; /* page not readable */
+            break;
          }
          if (missing & PAGE_WRITE) {
              if (!(p->flags & PAGE_WRITE_ORG)) {
-                return -1; /* page not writable */
+                ret = -1; /* page not writable */
+                break;
              }
              /* Asking about writable, but has been protected: undo. */ if (!page_unprotect(start, 0)) {
-                return -1;
+                ret = -1;
+                break;
              }
              /* TODO: page_unprotect should take a range, not a single page. */ if (last - start < TARGET_PAGE_SIZE) {
-                return 0; /* ok */
+                ret = 0; /* ok */
+                break;
              }
              start += TARGET_PAGE_SIZE;
              continue;
          }
          if (last <= p->itree.last) {

Quoted 8 lines

-            return 0; /* ok */
+            ret = 0; /* ok */
+            break;
          }
          start = p->itree.last + 1;
      }
+
+    if (locked < 0) {

Quoted 6 lines

... and check for !locked here?

Quoted 4 lines

+        mmap_unlock();
+    }
+    return ret;
  }

Modulo using a boolean:

#11 lore raw

Re: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range

In reply to Philippe Mathieu-Daudé
Quoted 92 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Richard Henderson

As in page_get_flags, we need to try again with the mmap lock held if we fail a page lookup.

---
  accel/tcg/user-exec.c | 39 ++++++++++++++++++++++++++++++++-------

  1 file changed, 32 insertions(+), 7 deletions(-)
accel/tcg/user-exec.c +0 -0
index 2c5c10d2e6..c72a806203 100644

accel/tcg/user-exec.c void page_set_flags(target_ulong start, target_ulong end, int flags) · line 528

Quoted 4 lines
accel/tcg/user-exec.c +1 -0
@@ -525,6 +525,7 @@ void page_set_flags(target_ulong start, target_ulong end, int flags)
   int page_check_range(target_ulong start, target_ulong len, int flags)
   {
       target_ulong last;
+    int locked, ret;

have_mmap_lock() returns a boolean, can we declare 'locked' as such, ...

Quoted 10 lines
accel/tcg/user-exec.c +0 -0
       if (len == 0) {

          return 0;  /* trivial length */

accel/tcg/user-exec.c +0 -0
@@ -535,42 +536,66 @@ int page_check_range(target_ulong start, target_ulong len, int flags)

          return -1; /* wrap around */
      }
+    locked = have_mmap_lock();
      while (true) {
          PageFlagsNode *p = pageflags_find(start, last); int missing;
          if (!p) {

Quoted 8 lines

-            return -1; /* entire region invalid */ +            if (!locked) {
+                /*
+                 * Lockless lookups have false negatives. +                 * Retry with the lock held.
+                 */
+                mmap_lock();
+                locked = -1;

... skip this confusing assignation, ...

Quoted 40 lines

+                p = pageflags_find(start, last);
+            }
+            if (!p) {
+                ret = -1; /* entire region invalid */
+                break;
+            }
          }
          if (start < p->itree.start) {
-            return -1; /* initial bytes invalid */
+            ret = -1; /* initial bytes invalid */
+            break;
          }
          missing = flags & ~p->flags;
          if (missing & PAGE_READ) {
-            return -1; /* page not readable */
+            ret = -1; /* page not readable */
+            break;
          }
          if (missing & PAGE_WRITE) {
              if (!(p->flags & PAGE_WRITE_ORG)) {
-                return -1; /* page not writable */
+                ret = -1; /* page not writable */
+                break;
              }
              /* Asking about writable, but has been protected: undo. */ if (!page_unprotect(start, 0)) {
-                return -1;
+                ret = -1;
+                break;
              }
              /* TODO: page_unprotect should take a range, not a single page. */ if (last - start < TARGET_PAGE_SIZE) {
-                return 0; /* ok */
+                ret = 0; /* ok */
+                break;
              }
              start += TARGET_PAGE_SIZE;
              continue;
          }
          if (last <= p->itree.last) {

Quoted 8 lines

-            return 0; /* ok */
+            ret = 0; /* ok */
+            break;
          }
          start = p->itree.last + 1;
      }
+
+    if (locked < 0) {

Quoted 1 line

... and check for !locked here?

No, we may have entered with mmap_locked. Only unlocking if the lock was taken locally.

r~

#12 lore raw

Re: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range

Quoted 95 lines · Richard Henderson
Quoted 94 lines · Philippe Mathieu-Daudé
Quoted 13 lines · Richard Henderson

As in page_get_flags, we need to try again with the mmap lock held if we fail a page lookup.

---
  accel/tcg/user-exec.c | 39 ++++++++++++++++++++++++++++++++-------

  1 file changed, 32 insertions(+), 7 deletions(-)
accel/tcg/user-exec.c +0 -0
index 2c5c10d2e6..c72a806203 100644

accel/tcg/user-exec.c void page_set_flags(target_ulong start, · line 529

Quoted 5 lines
accel/tcg/user-exec.c +1 -0
@@ -525,6 +525,7 @@ void page_set_flags(target_ulong start, 
 target_ulong end, int flags)
   int page_check_range(target_ulong start, target_ulong len, int flags)
   {
       target_ulong last;
+    int locked, ret;

have_mmap_lock() returns a boolean, can we declare 'locked' as such, ...

Quoted 11 lines
accel/tcg/user-exec.c +0 -0
       if (len == 0) {

          return 0;  /* trivial length */

accel/tcg/user-exec.c +0 -0
@@ -535,42 +536,66 @@ int page_check_range(target_ulong start, 
 target_ulong len, int flags)

          return -1; /* wrap around */
      }
+    locked = have_mmap_lock();
      while (true) {
          PageFlagsNode *p = pageflags_find(start, last); int missing;
          if (!p) {

Quoted 8 lines

-            return -1; /* entire region invalid */ +            if (!locked) {
+                /*
+                 * Lockless lookups have false negatives. +                 * Retry with the lock held.
+                 */
+                mmap_lock();
+                locked = -1;

... skip this confusing assignation, ...

Quoted 48 lines

+                p = pageflags_find(start, last);
+            }
+            if (!p) {
+                ret = -1; /* entire region invalid */
+                break;
+            }
          }
          if (start < p->itree.start) {
-            return -1; /* initial bytes invalid */
+            ret = -1; /* initial bytes invalid */
+            break;
          }
          missing = flags & ~p->flags;
          if (missing & PAGE_READ) {
-            return -1; /* page not readable */
+            ret = -1; /* page not readable */
+            break;
          }
          if (missing & PAGE_WRITE) {
              if (!(p->flags & PAGE_WRITE_ORG)) {
-                return -1; /* page not writable */
+                ret = -1; /* page not writable */
+                break;
              }
              /* Asking about writable, but has been protected: undo. */ if (!page_unprotect(start, 0)) {
-                return -1;
+                ret = -1;
+                break;
              }
              /* TODO: page_unprotect should take a range, not a single page. */ if (last - start < TARGET_PAGE_SIZE) {
-                return 0; /* ok */
+                ret = 0; /* ok */
+                break;
              }
              start += TARGET_PAGE_SIZE;
              continue;
          }
          if (last <= p->itree.last) {
-            return 0; /* ok */
+            ret = 0; /* ok */
+            break;
          }
          start = p->itree.last + 1;
      }
+
+    if (locked < 0) {

Quoted 3 lines
Quoted 1 line

... and check for !locked here?

No, we may have entered with mmap_locked.  Only unlocking if the lock was taken locally.

Oh, so you are using this variable as tri-state?

#13 lore raw

Re: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range

Quoted 1 line · Philippe Mathieu-Daudé

Oh, so you are using this variable as tri-state?

Yes. Perhaps a comment on the -1?

r~

#14 lore raw

Re: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range

Quoted 4 lines · Richard Henderson
Quoted 1 line · Philippe Mathieu-Daudé

Oh, so you are using this variable as tri-state?

Yes.  Perhaps a comment on the -1?

Or name the variable tristate_lock? And a comment :) (No need to respin, you can keep my R-b).

#15 lore raw

PATCH 4/4 tests/tcg/multiarch: add vma-pthread.c

In reply to Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

---
 tests/tcg/multiarch/nop_func.h       |  25 ++++
 tests/tcg/multiarch/munmap-pthread.c |  16 +--
 tests/tcg/multiarch/vma-pthread.c    | 207 +++++++++++++++++++++++++++
 tests/tcg/multiarch/Makefile.target  |   3 +
 4 files changed, 236 insertions(+), 15 deletions(-)
 create mode 100644 tests/tcg/multiarch/nop_func.h
 create mode 100644 tests/tcg/multiarch/vma-pthread.c
tests/tcg/multiarch/nop_func.h new file +25 -0
new file mode 100644
index 0000000000..f714d21000
@@ -0,0 +1,25 @@
+/*
+ * No-op functions that can be safely copied.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#ifndef NOP_FUNC_H
+#define NOP_FUNC_H
+
+static const char nop_func[] = {
+#if defined(__aarch64__)
+    0xc0, 0x03, 0x5f, 0xd6,     /* ret */
+#elif defined(__alpha__)
+    0x01, 0x80, 0xFA, 0x6B,     /* ret */
+#elif defined(__arm__)
+    0x1e, 0xff, 0x2f, 0xe1,     /* bx lr */
+#elif defined(__riscv)
+    0x67, 0x80, 0x00, 0x00,     /* ret */
+#elif defined(__s390__)
+    0x07, 0xfe,                 /* br %r14 */
+#elif defined(__i386__) || defined(__x86_64__)
+    0xc3,                       /* ret */
+#endif
+};
+
+#endif
tests/tcg/multiarch/munmap-pthread.c +1 -15
index d7143b00d5..1c79005846 100644
@@ -7,21 +7,7 @@
 #include <sys/mman.h>
 #include <unistd.h>
 
-static const char nop_func[] = {
-#if defined(__aarch64__)
-    0xc0, 0x03, 0x5f, 0xd6,     /* ret */
-#elif defined(__alpha__)
-    0x01, 0x80, 0xFA, 0x6B,     /* ret */
-#elif defined(__arm__)
-    0x1e, 0xff, 0x2f, 0xe1,     /* bx lr */
-#elif defined(__riscv)
-    0x67, 0x80, 0x00, 0x00,     /* ret */
-#elif defined(__s390__)
-    0x07, 0xfe,                 /* br %r14 */
-#elif defined(__i386__) || defined(__x86_64__)
-    0xc3,                       /* ret */
-#endif
-};
+#include "nop_func.h"
 
 static void *thread_mmap_munmap(void *arg)
 {
tests/tcg/multiarch/vma-pthread.c new file +207 -0

207 lines not on this page · raw message

tests/tcg/multiarch/Makefile.target +3 -0
index 5f0fee1aad..e7213af492 100644
@@ -39,6 +39,9 @@ signals: LDFLAGS+=-lrt -lpthread
 munmap-pthread: CFLAGS+=-pthread
 munmap-pthread: LDFLAGS+=-pthread
 
+vma-pthread: CFLAGS+=-pthread
+vma-pthread: LDFLAGS+=-pthread
+
 # We define the runner for test-mmap after the individual
 # architectures have defined their supported pages sizes. If no
 # additional page sizes are defined we only run the default test.
Signature · 1 line
2.34.1
Acked-by on v1 4/4
#16 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

Quoted 9 lines · Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#17 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

In reply to Richard Henderson
Quoted 10 lines · Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

I've noticed that this newly added vma-pthread test seems to be flaky. Here's an example from a clang-user job:
https://gitlab.com/qemu-project/qemu/-/jobs/3600385176

TEST vma-pthread-with-libbb.so on aarch64
fail indirect write 0x5500b1eff0 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libbb.so] Error 134

and another from a few days earlier:
https://gitlab.com/qemu-project/qemu/-/jobs/3572970612

TEST vma-pthread-with-libsyscall.so on s390x
fail indirect read 0x4000999000 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libsyscall.so] Error 134

thanks
-- PMM

#18 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

Quoted 26 lines · Peter Maydell
Quoted 10 lines · Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

I've noticed that this newly added vma-pthread test seems to be flaky. Here's an example from a clang-user job:
https://gitlab.com/qemu-project/qemu/-/jobs/3600385176

TEST vma-pthread-with-libbb.so on aarch64
fail indirect write 0x5500b1eff0 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libbb.so] Error 134

and another from a few days earlier:
https://gitlab.com/qemu-project/qemu/-/jobs/3572970612

Quoted 8 lines

TEST vma-pthread-with-libsyscall.so on s390x
fail indirect read 0x4000999000 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libsyscall.so] Error 134

thanks
-- PMM

Interesting those are both with plugins. I wonder if the tsan plugin fixes in my latest tree help?

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#19 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

Quoted 26 lines · Alex Bennée
Quoted 23 lines · Peter Maydell
Quoted 10 lines · Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

I've noticed that this newly added vma-pthread test seems to be flaky. Here's an example from a clang-user job:
https://gitlab.com/qemu-project/qemu/-/jobs/3600385176

TEST vma-pthread-with-libbb.so on aarch64
fail indirect write 0x5500b1eff0 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libbb.so] Error 134

Quoted 8 lines
Quoted 8 lines

and another from a few days earlier:
https://gitlab.com/qemu-project/qemu/-/jobs/3572970612

TEST vma-pthread-with-libsyscall.so on s390x
fail indirect read 0x4000999000 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libsyscall.so] Error 134

Yet again:
https://gitlab.com/qemu-project/qemu/-/jobs/3608436731

Quoted 3 lines

Interesting those are both with plugins. I wonder if the tsan plugin fixes in my latest tree help?

#20 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

Quoted 31 lines · Philippe Mathieu-Daudé
Quoted 29 lines · Alex Bennée
Quoted 26 lines · Peter Maydell
Quoted 10 lines · Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

I've noticed that this newly added vma-pthread test seems to be flaky. Here's an example from a clang-user job:
https://gitlab.com/qemu-project/qemu/-/jobs/3600385176

TEST vma-pthread-with-libbb.so on aarch64
fail indirect write 0x5500b1eff0 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libbb.so] Error 134

and another from a few days earlier:
https://gitlab.com/qemu-project/qemu/-/jobs/3572970612

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

TEST vma-pthread-with-libsyscall.so on s390x
fail indirect read 0x4000999000 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libsyscall.so] Error 134

Yet again:
https://gitlab.com/qemu-project/qemu/-/jobs/3608436731

Yep. Could somebody write a patch to disable this test while we figure out why it's flaky, please?

thanks
-- PMM

#21 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

Quoted 38 lines · Peter Maydell
Quoted 37 lines · Philippe Mathieu-Daudé
Quoted 34 lines · Alex Bennée
Quoted 31 lines · Peter Maydell
Quoted 10 lines · Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

I've noticed that this newly added vma-pthread test seems to be flaky. Here's an example from a clang-user job:
https://gitlab.com/qemu-project/qemu/-/jobs/3600385176

TEST vma-pthread-with-libbb.so on aarch64
fail indirect write 0x5500b1eff0 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libbb.so] Error 134

and another from a few days earlier:
https://gitlab.com/qemu-project/qemu/-/jobs/3572970612

TEST vma-pthread-with-libsyscall.so on s390x
fail indirect read 0x4000999000 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libsyscall.so] Error 134

Quoted 5 lines
Quoted 2 lines

Yet again:
https://gitlab.com/qemu-project/qemu/-/jobs/3608436731

Yep. Could somebody write a patch to disable this test while we figure out why it's flaky, please?

I don't think the test is flaky - I think it is triggering a race in QEMU code. I have not however been able to replicate it in anything other than CI.

Although looking at the test I'm beginning to wonder what the sync point is between the mutator and the read/write threads?

Quoted 3 lines

thanks
-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#22 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

Quoted 6 lines · Alex Bennée
Quoted 2 lines · Peter Maydell
Quoted 1 line · Philippe Mathieu-Daudé
Quoted 7 lines
Quoted 3 lines
Quoted 1 line · Alex Bennée

Yep. Could somebody write a patch to disable this test while we figure out why it's flaky, please?

I don't think the test is flaky - I think it is triggering a race in QEMU code. I have not however been able to replicate it in anything other than CI.

My definition of "flaky" here is "the CI fails randomly". Unless you have a patch to fix the underlying bug ready to go right now, please can we disable this so we don't keep getting CI failures trying to test merges?

thanks
-- PMM

#23 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

Quoted 11 lines · Peter Maydell
Quoted 10 lines · Alex Bennée
Quoted 5 lines · Peter Maydell
Quoted 2 lines · Philippe Mathieu-Daudé

On 13/1/23 18:10, Alex Bennée wrote:

Yep. Could somebody write a patch to disable this test while we figure out why it's flaky, please?

Quoted 8 lines
Quoted 3 lines

I don't think the test is flaky - I think it is triggering a race in QEMU code. I have not however been able to replicate it in anything other than CI.

My definition of "flaky" here is "the CI fails randomly". Unless you have a patch to fix the underlying bug ready to go right now, please can we disable this so we don't keep getting CI failures trying to test merges?

Yes - just testing the patch now.

Quoted 3 lines

thanks
-- PMM

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#24 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

In reply to Alex Bennée
Quoted 2 lines · Alex Bennée

Although looking at the test I'm beginning to wonder what the sync point is between the mutator and the read/write threads?

What do you mean?

There is no explicit sync, because the mutator always leaves each page with the (one) permission that the read/write/execute thread requires.

Within qemu... the sync point is primarily the syscall for read/write, and the tb invalidate (during the mprotect) or the mmap lock for new translation for execute.

r~

#25 lore raw

Re: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c

In reply to Alex Bennée
Quoted 36 lines · Alex Bennée
Quoted 32 lines · Peter Maydell
Quoted 10 lines · Richard Henderson

From: Ilya Leoshkevich <[email protected]>

Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling.

Message-Id: <[email protected]>

I've noticed that this newly added vma-pthread test seems to be flaky. Here's an example from a clang-user job:
https://gitlab.com/qemu-project/qemu/-/jobs/3600385176

TEST vma-pthread-with-libbb.so on aarch64
fail indirect write 0x5500b1eff0 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libbb.so] Error 134

and another from a few days earlier:
https://gitlab.com/qemu-project/qemu/-/jobs/3572970612

TEST vma-pthread-with-libsyscall.so on s390x
fail indirect read 0x4000999000 (Bad address)
timeout: the monitored command dumped core
Aborted
make[1]: *** [Makefile:173: run-plugin-vma-pthread-with-libsyscall.so] Error 134

Quoted 5 lines
Quoted 2 lines

thanks
-- PMM

Interesting those are both with plugins. I wonder if the tsan plugin fixes in my latest tree help?

I think this is a failure in the non-plugin case:
https://gitlab.com/qemu-project/qemu/-/jobs/3636082364

-- PMM