All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: iii@linux.ibm.com
Subject: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range
Date: Sat, 24 Dec 2022 07:18:20 -0800	[thread overview]
Message-ID: <20221224151821.464455-4-richard.henderson@linaro.org> (raw)
In-Reply-To: <20221224151821.464455-1-richard.henderson@linaro.org>

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

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 accel/tcg/user-exec.c | 39 ++++++++++++++++++++++++++++++++-------
 1 file changed, 32 insertions(+), 7 deletions(-)

diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c
index 2c5c10d2e6..c72a806203 100644
--- a/accel/tcg/user-exec.c
+++ b/accel/tcg/user-exec.c
@@ -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)
-- 
2.34.1



  parent reply	other threads:[~2022-12-24 15:19 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-24 15:18 [PATCH 0/4] accel/tcg: Fixes for user-only page tracking Richard Henderson
2022-12-24 15:18 ` [PATCH 1/4] accel/tcg: Fix tb_invalidate_phys_page_unwind Richard Henderson
2022-12-28 12:49   ` [PATCH 1a/4] " Philippe Mathieu-Daudé
2022-12-28 12:49     ` [PATCH 1b/4] accel/tcg: Unindent tb_invalidate_phys_page_unwind Philippe Mathieu-Daudé
2022-12-28 12:52       ` Philippe Mathieu-Daudé
2022-12-28 12:52     ` [PATCH 1a/4] accel/tcg: Fix tb_invalidate_phys_page_unwind Philippe Mathieu-Daudé
2022-12-24 15:18 ` [PATCH 2/4] accel/tcg: Use g_free_rcu for user-exec interval trees Richard Henderson
2022-12-28  7:19   ` Philippe Mathieu-Daudé
2022-12-24 15:18 ` Richard Henderson [this message]
2022-12-28  7:24   ` [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range Philippe Mathieu-Daudé
2022-12-28 12:53     ` Philippe Mathieu-Daudé
2022-12-28 17:36     ` Richard Henderson
2022-12-28 18:27       ` Philippe Mathieu-Daudé
2022-12-28 18:30         ` Richard Henderson
2022-12-28 18:53           ` Philippe Mathieu-Daudé
2022-12-24 15:18 ` [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c Richard Henderson
2022-12-27 17:23   ` Alex Bennée
2023-01-13 15:17   ` Peter Maydell
2023-01-13 17:10     ` Alex Bennée
2023-01-16 12:40       ` Philippe Mathieu-Daudé
2023-01-16 15:07         ` Peter Maydell
2023-01-16 16:27           ` Alex Bennée
2023-01-16 16:48             ` Peter Maydell
2023-01-16 17:09               ` Alex Bennée
2023-01-16 19:21             ` Richard Henderson
2023-01-20 14:58       ` Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221224151821.464455-4-richard.henderson@linaro.org \
    --to=richard.henderson@linaro.org \
    --cc=iii@linux.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.