Skip to contentSkip to conversation

accel/tcg: Take mmap_lock in load_atomic16_or_exit

PATCH Superseded Richard Henderson 3 participants 7 messages Jul 16, 2023 – Jul 20, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/tcg: Take mmap_lock in load_atomic16_or_exit

For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback.

Remove the write probe in load_atomic8_or_exit. There we don't have the same machinery for testing the existance of an 8-byte cmpxchg.

---
 accel/tcg/ldst_atomicity.c.inc | 54 +++++++++++++++-------------------
 1 file changed, 24 insertions(+), 30 deletions(-)
accel/tcg/ldst_atomicity.c.inc +24 -30
index 4de0a80492..e7170f8ba2 100644
@@ -152,19 +152,6 @@ static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
         return load_atomic8(pv);
     }
 
-#ifdef CONFIG_USER_ONLY
-    /*
-     * If the page is not writable, then assume the value is immutable
-     * and requires no locking.  This ignores the case of MAP_SHARED with
-     * another process, because the fallback start_exclusive solution
-     * provides no protection across processes.
-     */
-    if (page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) {
-        uint64_t *p = __builtin_assume_aligned(pv, 8);
-        return *p;
-    }
-#endif
-
     /* Ultimate fallback: re-execute in serial context. */
     cpu_loop_exit_atomic(env_cpu(env), ra);
 }
@@ -186,25 +173,32 @@ static Int128 load_atomic16_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
         return atomic16_read_ro(p);
     }
 
-#ifdef CONFIG_USER_ONLY
-    /*
-     * We can only use cmpxchg to emulate a load if the page is writable.
-     * If the page is not writable, then assume the value is immutable
-     * and requires no locking.  This ignores the case of MAP_SHARED with
-     * another process, because the fallback start_exclusive solution
-     * provides no protection across processes.
-     */
-    if (page_check_range(h2g(p), 16, PAGE_WRITE_ORG)) {
-        return *p;
-    }
-#endif
-
-    /*
-     * In system mode all guest pages are writable, and for user-only
-     * we have just checked writability.  Try cmpxchg.
-     */
+    /* We can only use cmpxchg to emulate a load if the page is writable. */
     if (HAVE_ATOMIC128_RW) {
+#ifdef CONFIG_USER_ONLY
+        /*
+         * If the page is not writable, then assume the value is immutable
+         * and requires no locking.  This ignores the case of MAP_SHARED with
+         * another process, because the fallback start_exclusive solution
+         * provides no protection across processes.
+         * We must take mmap_lock so that the query remains valid until
+         * the write is complete -- tests/tcg/multiarch/munmap-pthread.c
+         * is an example that can race.
+         */
+        Int128 r;
+
+        mmap_lock();
+        if (page_get_flags(h2g(p)) & PAGE_WRITE_ORG) {
+            r = atomic16_read_rw(p);
+        } else {
+            r = *p;
+        }
+        mmap_unlock();
+        return r;
+#else
+        /* In system mode all guest pages are host writable. */
         return atomic16_read_rw(p);
+#endif
     }
 
     /* Ultimate fallback: re-execute in serial context. */
Signature · 1 line
2.34.1
#1 lore raw
Quoted 7 lines · Richard Henderson

For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback.

Remove the write probe in load_atomic8_or_exit. There we don't have the same machinery for testing the existance of an 8-byte cmpxchg.

"existence"

Quoted 16 lines
---
 accel/tcg/ldst_atomicity.c.inc | 54 +++++++++++++++-------------------
 1 file changed, 24 insertions(+), 30 deletions(-)
accel/tcg/ldst_atomicity.c.inc +0 -2
index 4de0a80492..e7170f8ba2 100644
@@ -152,19 +152,6 @@ static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
         return load_atomic8(pv);
     }
 
-#ifdef CONFIG_USER_ONLY
-    /*

accel/tcg/ldst_atomicity.c.inc static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv) · line 166

Quoted 10 lines
accel/tcg/ldst_atomicity.c.inc +0 -10
-     * If the page is not writable, then assume the value is immutable
-     * and requires no locking.  This ignores the case of MAP_SHARED with
-     * another process, because the fallback start_exclusive solution
-     * provides no protection across processes.
-     */
-    if (page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) {
-        uint64_t *p = __builtin_assume_aligned(pv, 8);
-        return *p;
-    }
-#endif

I don't really understand the comment in the commit message:
why would it be wrong to wrap this "test writeability and do the operation" in the mmap-lock, the same way we do for the 16-byte case?

thanks
-- PMM

#2 lore raw
Quoted 35 lines · Peter Maydell
Quoted 7 lines · Richard Henderson

For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback.

Remove the write probe in load_atomic8_or_exit. There we don't have the same machinery for testing the existance of an 8-byte cmpxchg.

"existence"

Quoted 23 lines
---
  accel/tcg/ldst_atomicity.c.inc | 54 +++++++++++++++-------------------
  1 file changed, 24 insertions(+), 30 deletions(-)
accel/tcg/ldst_atomicity.c.inc +0 -9
index 4de0a80492..e7170f8ba2 100644
@@ -152,19 +152,6 @@ static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
          return load_atomic8(pv);
      }
 
-#ifdef CONFIG_USER_ONLY
-    /*
-     * If the page is not writable, then assume the value is immutable
-     * and requires no locking.  This ignores the case of MAP_SHARED with
-     * another process, because the fallback start_exclusive solution
-     * provides no protection across processes.
-     */
-    if (page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) {
-        uint64_t *p = __builtin_assume_aligned(pv, 8);
Quoted 8 lines

accel/tcg/ldst_atomicity.c.inc static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv) · line 166

Quoted 3 lines
accel/tcg/ldst_atomicity.c.inc +0 -3
-        return *p;
-    }
-#endif

I don't really understand the comment in the commit message:
why would it be wrong to wrap this "test writeability and do the operation" in the mmap-lock, the same way we do for the 16-byte case?

It would not be wrong. I was just thinking of the cmpxchg8 part, for which we do not have a configure probe, and for which I *think* there's no call, because there are no 32-bit hosts that have cmpxchg8 but not the full CONFIG_ATOMIC64.

r~

#3 lore raw
Quoted 46 lines · Richard Henderson
Quoted 35 lines · Peter Maydell
Quoted 7 lines · Richard Henderson

For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback.

Remove the write probe in load_atomic8_or_exit. There we don't have the same machinery for testing the existance of an 8-byte cmpxchg.

"existence"

Quoted 23 lines
---
  accel/tcg/ldst_atomicity.c.inc | 54 +++++++++++++++-------------------
  1 file changed, 24 insertions(+), 30 deletions(-)
accel/tcg/ldst_atomicity.c.inc +0 -9
index 4de0a80492..e7170f8ba2 100644
@@ -152,19 +152,6 @@ static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv)
          return load_atomic8(pv);
      }
 
-#ifdef CONFIG_USER_ONLY
-    /*
-     * If the page is not writable, then assume the value is immutable
-     * and requires no locking.  This ignores the case of MAP_SHARED with
-     * another process, because the fallback start_exclusive solution
-     * provides no protection across processes.
-     */
-    if (page_check_range(h2g(pv), 8, PAGE_WRITE_ORG)) {
-        uint64_t *p = __builtin_assume_aligned(pv, 8);
Quoted 8 lines

accel/tcg/ldst_atomicity.c.inc static uint64_t load_atomic8_or_exit(CPUArchState *env, uintptr_t ra, void *pv) · line 166

Quoted 3 lines
accel/tcg/ldst_atomicity.c.inc +0 -3
-        return *p;
-    }
-#endif

I don't really understand the comment in the commit message:
why would it be wrong to wrap this "test writeability and do the operation" in the mmap-lock, the same way we do for the 16-byte case?

Quoted 3 lines

It would not be wrong. I was just thinking of the cmpxchg8 part, for which we do not have a configure probe, and for which I *think* there's no call, because there are no 32-bit hosts that have cmpxchg8 but not the full CONFIG_ATOMIC64.

But this piece of code the patch deletes isn't doing a cmpxchg8, it just does a plain load. If "take the lock and do the operation" is faster than always using the fallback code for the atomic16 case, why don't we make the same tradeoff choice in atomic8 ?

thanks
-- PMM

Reviewed-by on v1 1/1
#4 lore raw
In reply to Richard Henderson
Quoted 3 lines · Richard Henderson

For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback.

Did we have a bug report or replication for this?

Quoted 5 lines

Remove the write probe in load_atomic8_or_exit. There we don't have the same machinery for testing the existance of an 8-byte cmpxchg.

Otherwise makes sense to me:

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#5 lore raw
Quoted 8 lines · Alex Bennée
Quoted 3 lines · Richard Henderson

For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback.

Did we have a bug report or replication for this?

See the comment:

accel/tcg/ldst_atomicity.c.inc static Int128 load_atomic16_or_exit(CPUArchState *env, uintptr_t ra, void *pv) · line 186

Quoted 3 lines
accel/tcg/ldst_atomicity.c.inc +3 -0
+         * We must take mmap_lock so that the query remains valid until
+         * the write is complete -- tests/tcg/multiarch/munmap-pthread.c
+         * is an example that can race.

r~

#6 lore raw
Quoted 13 lines · Richard Henderson
Quoted 6 lines · Alex Bennée
Quoted 3 lines · Richard Henderson

For user-only, the probe for page writability may race with another thread's mprotect. Take the mmap_lock around the operation. This is still faster than the start/end_exclusive fallback.

Did we have a bug report or replication for this?

See the comment:

Quoted 3 lines
accel/tcg/ldst_atomicity.c.inc +3 -0
+         * We must take mmap_lock so that the query remains valid until
+         * the write is complete -- tests/tcg/multiarch/munmap-pthread.c
+         * is an example that can race.

doh...

Quoted 3 lines

r~

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro