Skip to contentSkip to conversation

tcg: Misc clean-up patches from Paolo

PATCH Superseded [email protected] 6 participants 41 messages Mar 17, 2016 – Apr 14, 2016

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover tcg: Misc clean-up patches from Paolo

From: Sergey Fedorov <[email protected]>

This patch series consists of various general TCG clean-up patches extracted from Paolo's MTTCG tree [1]. The idea is to review and merge these patches separately from the MTTCG series to cut the latter and make it easier to review.

[1] https://github.com/bonzini/qemu/tree/mttcg

Paolo Bonzini (5):
tcg: code_bitmap is not used by user-mode emulation
tcg: reorganize tb_find_physical loop
tcg: always keep jump target and tb->jmp_next consistent
tcg: reorder removal from lists in tb_phys_invalidate
tcg: move tb_invalidated_flag to CPUState

 cpu-exec.c              | 61 +++++++++++++++++-----------------
 include/exec/exec-all.h |  2 --
 include/qom/cpu.h       |  2 ++
 translate-all.c         | 87 +++++++++++++++++++++++++------------------------
 4 files changed, 77 insertions(+), 75 deletions(-)
Signature · 1 line
2.7.3
#1 lore raw

PATCH 1/5 tcg: code_bitmap is not used by user-mode emulation

From: Paolo Bonzini <[email protected]>

---
 translate-all.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
translate-all.c +7 -2
index e9f409b762ab..f17ace1ae899 100644
@@ -784,6 +784,9 @@ void tb_free(TranslationBlock *tb)
 
 static inline void invalidate_page_bitmap(PageDesc *p)
 {
+#ifndef CONFIG_SOFTMMU
+    assert(p->code_bitmap == NULL);
+#endif
     g_free(p->code_bitmap);
     p->code_bitmap = NULL;
     p->code_write_count = 0;
@@ -1018,6 +1021,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
     tcg_ctx.tb_ctx.tb_phys_invalidate_count++;
 }
 
+#ifdef CONFIG_SOFTMMU
 static void build_page_bitmap(PageDesc *p)
 {
     int n, tb_start, tb_end;
@@ -1046,6 +1050,7 @@ static void build_page_bitmap(PageDesc *p)
         tb = tb->page_next[n];
     }
 }
+#endif
 
 /* Called with mmap_lock held for user mode emulation.  */
 TranslationBlock *tb_gen_code(CPUState *cpu,
@@ -1294,6 +1299,7 @@ void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
 #endif
 }
 
+#ifdef CONFIG_SOFTMMU
 /* len must be <= 8 and start must be a multiple of len */
 void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
 {
@@ -1331,8 +1337,7 @@ void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len)
         tb_invalidate_phys_page_range(start, start + len, 1);
     }
 }
-
-#if !defined(CONFIG_SOFTMMU)
+#else
 /* Called with mmap_lock held.  */
 static void tb_invalidate_phys_page(tb_page_addr_t addr,
                                     uintptr_t pc, void *puc,
Signature · 1 line
2.7.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] tcg: code_bitmap is not used by user-mode emulation

Quoted 23 lines · On 17 March 2016 at 13:46, <[email protected]> wrote:

From: Paolo Bonzini <[email protected]>

---
 translate-all.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
translate-all.c +3 -0
index e9f409b762ab..f17ace1ae899 100644
@@ -784,6 +784,9 @@ void tb_free(TranslationBlock *tb)
 
 static inline void invalidate_page_bitmap(PageDesc *p)
 {
+#ifndef CONFIG_SOFTMMU
+    assert(p->code_bitmap == NULL);
+#endif
     g_free(p->code_bitmap);
     p->code_bitmap = NULL;
     p->code_write_count = 0;

translate-all.c void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) · line 1022

Quoted 2 lines
translate-all.c +0 -0
@@ -1018,6 +1021,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
     tcg_ctx.tb_ctx.tb_phys_invalidate_count++;
 }

You could pretty much take this another step and actually make the field in the struct be only present ifdef CONFIG_SOFTMMU...

thanks
-- PMM

#3 lore raw

Re: [Qemu-devel] [PATCH 1/5] tcg: code_bitmap is not used by user-mode emulation

Quoted 15 lines · Peter Maydell
Quoted 14 lines · On 17 March 2016 at 13:46, <[email protected]> wrote:

From: Paolo Bonzini <[email protected]>

---
  translate-all.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
translate-all.c +0 -0
index e9f409b762ab..f17ace1ae899 100644
@@ -784,6 +784,9 @@ void tb_free(TranslationBlock *tb)
 
Quoted 13 lines

translate-all.c void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) · line 1022

Quoted 11 lines
translate-all.c +3 -0
  static inline void invalidate_page_bitmap(PageDesc *p)
  {
+#ifndef CONFIG_SOFTMMU
+    assert(p->code_bitmap == NULL);
+#endif
      g_free(p->code_bitmap);
      p->code_bitmap = NULL;
      p->code_write_count = 0;
@@ -1018,6 +1021,7 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
      tcg_ctx.tb_ctx.tb_phys_invalidate_count++;
  }

You could pretty much take this another step and actually make the field in the struct be only present ifdef CONFIG_SOFTMMU...

Sonds a good idea.

Kind regards,
Sergey

#4 lore raw

PATCH 2/5 tcg: reorganize tb_find_physical loop

In reply to [email protected]

From: Paolo Bonzini <[email protected]>

Use a continue statement.

[Sergey Fedorov: Fix moving to list head in case of no TB]
---
 cpu-exec.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
cpu-exec.c +25 -25
index fd92452f16f6..f90482eff778 100644
@@ -225,37 +225,37 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     phys_pc = get_page_addr_code(env, pc);
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
     h = tb_phys_hash_func(phys_pc);
-    ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
-    for(;;) {
-        tb = *ptb1;
-        if (!tb) {
-            return NULL;
+    for (ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
+         (tb = *ptb1) != NULL;
+         ptb1 = &tb->phys_hash_next) {
+        if (tb->pc != pc ||
+            tb->page_addr[0] != phys_page1 ||
+            tb->cs_base != cs_base ||
+            tb->flags != flags) {
+            continue;
         }
-        if (tb->pc == pc &&
-            tb->page_addr[0] == phys_page1 &&
-            tb->cs_base == cs_base &&
-            tb->flags == flags) {
-            /* check next page if needed */
-            if (tb->page_addr[1] != -1) {
-                tb_page_addr_t phys_page2;
-
-                virt_page2 = (pc & TARGET_PAGE_MASK) +
-                    TARGET_PAGE_SIZE;
-                phys_page2 = get_page_addr_code(env, virt_page2);
-                if (tb->page_addr[1] == phys_page2) {
-                    break;
-                }
-            } else {
+
+        /* check next page if needed */
+        if (tb->page_addr[1] != -1) {
+            tb_page_addr_t phys_page2;
+
+            virt_page2 = (pc & TARGET_PAGE_MASK) +
+                TARGET_PAGE_SIZE;
+            phys_page2 = get_page_addr_code(env, virt_page2);
+            if (tb->page_addr[1] == phys_page2) {
                 break;
             }
+        } else {
+            break;
         }
-        ptb1 = &tb->phys_hash_next;
     }
 
-    /* Move the TB to the head of the list */
-    *ptb1 = tb->phys_hash_next;
-    tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
-    tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
+    if (tb) {
+        /* Move the TB to the head of the list */
+        *ptb1 = tb->phys_hash_next;
+        tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
+        tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
+    }
     return tb;
 }
 
Signature · 1 line
2.7.3
Reviewed-by on v1 2/5
#5 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

Quoted 8 lines · On 17 March 2016 at 13:46, <[email protected]> wrote:

From: Paolo Bonzini <[email protected]>

Use a continue statement.

[Sergey Fedorov: Fix moving to list head in case of no TB]

---

(though it would be nice if the commit message gave some motivation for the change)

thanks
-- PMM

#6 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

In reply to [email protected]
Quoted 17 lines · [email protected] writes:

From: Paolo Bonzini <[email protected]>

Use a continue statement.

[Sergey Fedorov: Fix moving to list head in case of no TB]
---
 cpu-exec.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
cpu-exec.c +0 -0
index fd92452f16f6..f90482eff778 100644
@@ -225,37 +225,37 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     phys_pc = get_page_addr_code(env, pc);

cpu-exec.c static TranslationBlock *tb_find_physical(CPUState *cpu, · line 230

Quoted 10 lines
cpu-exec.c +3 -5
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
     h = tb_phys_hash_func(phys_pc);
-    ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
-    for(;;) {
-        tb = *ptb1;
-        if (!tb) {
-            return NULL;
+    for (ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
+         (tb = *ptb1) != NULL;
+         ptb1 = &tb->phys_hash_next) {

I'm not sure I'm keen on the assignment in the for condition clause. I appreciate the cleansing of the if !tb return exit though. Could we be cleaner maybe? Here is my attempt:

    static TranslationBlock *tb_find_physical(CPUState *cpu,
                                              target_ulong pc,
                                              target_ulong cs_base,
                                              uint64_t flags)
    {
        CPUArchState *env = (CPUArchState *)cpu->env_ptr;
        TranslationBlock *tb, **tb_hash_head, **ptb1;
        unsigned int h;
        tb_page_addr_t phys_pc, phys_page1;

        /* find translated block using physical mappings */
        phys_pc = get_page_addr_code(env, pc);
        phys_page1 = phys_pc & TARGET_PAGE_MASK;
        h = tb_phys_hash_func(phys_pc);

        /* Start at head of the hash entry */
        ptb1 = tb_hash_head = &tcg_ctx.tb_ctx.tb_phys_hash[h];
        tb = *ptb1;

        while (tb) {

            if (tb->pc == pc &&
                tb->page_addr[0] == phys_page1 &&
                tb->cs_base == cs_base &&
                tb->flags == flags) {

                if (tb->page_addr[1] == -1) {
                    /* done, we have a match */
                    break;
                } else {
                    /* check next page if needed */
                    target_ulong virt_page2 = (pc & TARGET_PAGE_MASK)
                        + TARGET_PAGE_SIZE;
                    tb_page_addr_t phys_page2 = get_page_addr_code(env, virt_page2);

                    if (tb->page_addr[1] == phys_page2) {
                        break;
                    }
                }
            }

            ptb1 = &tb->phys_hash_next;
            tb = *ptb1;
        }

        if (tb) {
            /* Move the TB to the head of the list */
            *ptb1 = tb->phys_hash_next;
            tb->phys_hash_next = *tb_hash_head;
            *tb_hash_head = tb;
        }
        return tb;
    }

FWIW the compiled code is 9 bytes shorter on my machine.

Quoted 40 lines
cpu-exec.c +16 -18
+        if (tb->pc != pc ||
+            tb->page_addr[0] != phys_page1 ||
+            tb->cs_base != cs_base ||
+            tb->flags != flags) {
+            continue;
         }
-        if (tb->pc == pc &&
-            tb->page_addr[0] == phys_page1 &&
-            tb->cs_base == cs_base &&
-            tb->flags == flags) {
-            /* check next page if needed */
-            if (tb->page_addr[1] != -1) {
-                tb_page_addr_t phys_page2;
-
-                virt_page2 = (pc & TARGET_PAGE_MASK) +
-                    TARGET_PAGE_SIZE;
-                phys_page2 = get_page_addr_code(env, virt_page2);
-                if (tb->page_addr[1] == phys_page2) {
-                    break;
-                }
-            } else {
+
+        /* check next page if needed */
+        if (tb->page_addr[1] != -1) {
+            tb_page_addr_t phys_page2;
+
+            virt_page2 = (pc & TARGET_PAGE_MASK) +
+                TARGET_PAGE_SIZE;
+            phys_page2 = get_page_addr_code(env, virt_page2);
+            if (tb->page_addr[1] == phys_page2) {
                 break;
             }
+        } else {
+            break;
         }
-        ptb1 = &tb->phys_hash_next;
     }
 
-    /* Move the TB to the head of the list */
-    *ptb1 = tb->phys_hash_next;

cpu-exec.c static TranslationBlock *tb_find_physical(CPUState *cpu, · line 260

Quoted 10 lines
cpu-exec.c +6 -2
-    tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
-    tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
+    if (tb) {
+        /* Move the TB to the head of the list */
+        *ptb1 = tb->phys_hash_next;
+        tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
+        tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
+    }
     return tb;
 }

--
Alex Bennée

#7 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

Quoted 6 lines · Alex Bennée
Quoted 3 lines

cpu-exec.c static TranslationBlock *tb_find_physical(CPUState *cpu, · line 230

Quoted 3 lines
cpu-exec.c +3 -0
+    for (ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
+         (tb = *ptb1) != NULL;
+         ptb1 = &tb->phys_hash_next) {

I'm not sure I'm keen on the assignment in the for condition clause. I appreciate the cleansing of the if !tb return exit though. Could we be cleaner maybe? Here is my attempt:

Sure, that would be just fine.

Paolo

#8 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

In reply to Alex Bennée
Quoted 87 lines · Alex Bennée
Quoted 17 lines · [email protected] writes:

From: Paolo Bonzini <[email protected]>

Use a continue statement.

[Sergey Fedorov: Fix moving to list head in case of no TB]
---
 cpu-exec.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
cpu-exec.c +0 -0
index fd92452f16f6..f90482eff778 100644
@@ -225,37 +225,37 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     phys_pc = get_page_addr_code(env, pc);

cpu-exec.c static TranslationBlock *tb_find_physical(CPUState *cpu, · line 230

Quoted 10 lines
cpu-exec.c +3 -5
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
     h = tb_phys_hash_func(phys_pc);
-    ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
-    for(;;) {
-        tb = *ptb1;
-        if (!tb) {
-            return NULL;
+    for (ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
+         (tb = *ptb1) != NULL;
+         ptb1 = &tb->phys_hash_next) {

I'm not sure I'm keen on the assignment in the for condition clause. I appreciate the cleansing of the if !tb return exit though. Could we be cleaner maybe? Here is my attempt:

    static TranslationBlock *tb_find_physical(CPUState *cpu,
                                              target_ulong pc,
                                              target_ulong cs_base,
                                              uint64_t flags)
    {
        CPUArchState *env = (CPUArchState *)cpu->env_ptr;
        TranslationBlock *tb, **tb_hash_head, **ptb1;
        unsigned int h;
        tb_page_addr_t phys_pc, phys_page1;

        /* find translated block using physical mappings */
        phys_pc = get_page_addr_code(env, pc);
        phys_page1 = phys_pc & TARGET_PAGE_MASK;
        h = tb_phys_hash_func(phys_pc);

        /* Start at head of the hash entry */
        ptb1 = tb_hash_head = &tcg_ctx.tb_ctx.tb_phys_hash[h];
        tb = *ptb1;

        while (tb) {

            if (tb->pc == pc &&
                tb->page_addr[0] == phys_page1 &&
                tb->cs_base == cs_base &&
                tb->flags == flags) {

                if (tb->page_addr[1] == -1) {
                    /* done, we have a match */
                    break;
                } else {
                    /* check next page if needed */
                    target_ulong virt_page2 = (pc & TARGET_PAGE_MASK)
                        + TARGET_PAGE_SIZE;
                    tb_page_addr_t phys_page2 = get_page_addr_code(env, virt_page2);

                    if (tb->page_addr[1] == phys_page2) {
                        break;
                    }
                }
            }

            ptb1 = &tb->phys_hash_next;
            tb = *ptb1;
        }

        if (tb) {
            /* Move the TB to the head of the list */
            *ptb1 = tb->phys_hash_next;
            tb->phys_hash_next = *tb_hash_head;
            *tb_hash_head = tb;
        }
        return tb;
    }
Quoted 1 line

FWIW the compiled code is 9 bytes shorter on my machine.

Looks like another attempt to rewrite it. I am wondering whom to attribute as an author, then? :)

Kind regards,
Sergey

Quoted 51 lines
Quoted 51 lines
cpu-exec.c +22 -20
+        if (tb->pc != pc ||
+            tb->page_addr[0] != phys_page1 ||
+            tb->cs_base != cs_base ||
+            tb->flags != flags) {
+            continue;
         }
-        if (tb->pc == pc &&
-            tb->page_addr[0] == phys_page1 &&
-            tb->cs_base == cs_base &&
-            tb->flags == flags) {
-            /* check next page if needed */
-            if (tb->page_addr[1] != -1) {
-                tb_page_addr_t phys_page2;
-
-                virt_page2 = (pc & TARGET_PAGE_MASK) +
-                    TARGET_PAGE_SIZE;
-                phys_page2 = get_page_addr_code(env, virt_page2);
-                if (tb->page_addr[1] == phys_page2) {
-                    break;
-                }
-            } else {
+
+        /* check next page if needed */
+        if (tb->page_addr[1] != -1) {
+            tb_page_addr_t phys_page2;
+
+            virt_page2 = (pc & TARGET_PAGE_MASK) +
+                TARGET_PAGE_SIZE;
+            phys_page2 = get_page_addr_code(env, virt_page2);
+            if (tb->page_addr[1] == phys_page2) {
                 break;
             }
+        } else {
+            break;
         }
-        ptb1 = &tb->phys_hash_next;
     }
 
-    /* Move the TB to the head of the list */
-    *ptb1 = tb->phys_hash_next;
-    tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
-    tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
+    if (tb) {
+        /* Move the TB to the head of the list */
+        *ptb1 = tb->phys_hash_next;
+        tb->phys_hash_next = tcg_ctx.tb_ctx.tb_phys_hash[h];
+        tcg_ctx.tb_ctx.tb_phys_hash[h] = tb;
+    }
     return tb;
 }
#9 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

Quoted 88 lines · Sergey Fedorov
Quoted 87 lines · Alex Bennée
Quoted 17 lines · [email protected] writes:

From: Paolo Bonzini <[email protected]>

Use a continue statement.

[Sergey Fedorov: Fix moving to list head in case of no TB]
---
 cpu-exec.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
cpu-exec.c +0 -0
index fd92452f16f6..f90482eff778 100644
@@ -225,37 +225,37 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     phys_pc = get_page_addr_code(env, pc);

cpu-exec.c static TranslationBlock *tb_find_physical(CPUState *cpu, · line 230

Quoted 10 lines
cpu-exec.c +3 -5
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
     h = tb_phys_hash_func(phys_pc);
-    ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
-    for(;;) {
-        tb = *ptb1;
-        if (!tb) {
-            return NULL;
+    for (ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
+         (tb = *ptb1) != NULL;
+         ptb1 = &tb->phys_hash_next) {

I'm not sure I'm keen on the assignment in the for condition clause. I appreciate the cleansing of the if !tb return exit though. Could we be cleaner maybe? Here is my attempt:

    static TranslationBlock *tb_find_physical(CPUState *cpu,
                                              target_ulong pc,
                                              target_ulong cs_base,
                                              uint64_t flags)
    {
        CPUArchState *env = (CPUArchState *)cpu->env_ptr;
        TranslationBlock *tb, **tb_hash_head, **ptb1;
        unsigned int h;
        tb_page_addr_t phys_pc, phys_page1;

        /* find translated block using physical mappings */
        phys_pc = get_page_addr_code(env, pc);
        phys_page1 = phys_pc & TARGET_PAGE_MASK;
        h = tb_phys_hash_func(phys_pc);

        /* Start at head of the hash entry */
        ptb1 = tb_hash_head = &tcg_ctx.tb_ctx.tb_phys_hash[h];
        tb = *ptb1;

        while (tb) {

            if (tb->pc == pc &&
                tb->page_addr[0] == phys_page1 &&
                tb->cs_base == cs_base &&
                tb->flags == flags) {

                if (tb->page_addr[1] == -1) {
                    /* done, we have a match */
                    break;
                } else {
                    /* check next page if needed */
                    target_ulong virt_page2 = (pc & TARGET_PAGE_MASK)
                        + TARGET_PAGE_SIZE;
                    tb_page_addr_t phys_page2 = get_page_addr_code(env, virt_page2);

                    if (tb->page_addr[1] == phys_page2) {
                        break;
                    }
                }
            }

            ptb1 = &tb->phys_hash_next;
            tb = *ptb1;
        }

        if (tb) {
            /* Move the TB to the head of the list */
            *ptb1 = tb->phys_hash_next;
            tb->phys_hash_next = *tb_hash_head;
            *tb_hash_head = tb;
        }
        return tb;
    }
Quoted 4 lines
Quoted 1 line

FWIW the compiled code is 9 bytes shorter on my machine.

Looks like another attempt to rewrite it. I am wondering whom to attribute as an author, then? :)

I don't really care. :)

Paolo

#10 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

Quoted 90 lines · Paolo Bonzini
Quoted 88 lines · Sergey Fedorov
Quoted 87 lines · Alex Bennée
Quoted 17 lines · [email protected] writes:

From: Paolo Bonzini <[email protected]>

Use a continue statement.

[Sergey Fedorov: Fix moving to list head in case of no TB]
---
 cpu-exec.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
cpu-exec.c +0 -0
index fd92452f16f6..f90482eff778 100644
@@ -225,37 +225,37 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     phys_pc = get_page_addr_code(env, pc);

cpu-exec.c static TranslationBlock *tb_find_physical(CPUState *cpu, · line 230

Quoted 10 lines
cpu-exec.c +3 -5
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
     h = tb_phys_hash_func(phys_pc);
-    ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
-    for(;;) {
-        tb = *ptb1;
-        if (!tb) {
-            return NULL;
+    for (ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
+         (tb = *ptb1) != NULL;
+         ptb1 = &tb->phys_hash_next) {

I'm not sure I'm keen on the assignment in the for condition clause. I appreciate the cleansing of the if !tb return exit though. Could we be cleaner maybe? Here is my attempt:

    static TranslationBlock *tb_find_physical(CPUState *cpu,
                                              target_ulong pc,
                                              target_ulong cs_base,
                                              uint64_t flags)
    {
        CPUArchState *env = (CPUArchState *)cpu->env_ptr;
        TranslationBlock *tb, **tb_hash_head, **ptb1;
        unsigned int h;
        tb_page_addr_t phys_pc, phys_page1;

        /* find translated block using physical mappings */
        phys_pc = get_page_addr_code(env, pc);
        phys_page1 = phys_pc & TARGET_PAGE_MASK;
        h = tb_phys_hash_func(phys_pc);

        /* Start at head of the hash entry */
        ptb1 = tb_hash_head = &tcg_ctx.tb_ctx.tb_phys_hash[h];
        tb = *ptb1;

        while (tb) {

            if (tb->pc == pc &&
                tb->page_addr[0] == phys_page1 &&
                tb->cs_base == cs_base &&
                tb->flags == flags) {

                if (tb->page_addr[1] == -1) {
                    /* done, we have a match */
                    break;
                } else {
                    /* check next page if needed */
                    target_ulong virt_page2 = (pc & TARGET_PAGE_MASK)
                        + TARGET_PAGE_SIZE;
                    tb_page_addr_t phys_page2 = get_page_addr_code(env, virt_page2);

                    if (tb->page_addr[1] == phys_page2) {
                        break;
                    }
                }
            }

            ptb1 = &tb->phys_hash_next;
            tb = *ptb1;
        }

        if (tb) {
            /* Move the TB to the head of the list */
            *ptb1 = tb->phys_hash_next;
            tb->phys_hash_next = *tb_hash_head;
            *tb_hash_head = tb;
        }
        return tb;
    }
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

FWIW the compiled code is 9 bytes shorter on my machine.

Looks like another attempt to rewrite it. I am wondering whom to attribute as an author, then? :)

I don't really care. :)

Alex, could you give your s-o-b for your variant of code? Or would you like to make a patch by yourself?

Kind regards,
Sergey

#11 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

Quoted 91 lines · Sergey Fedorov
Quoted 90 lines · Paolo Bonzini
Quoted 88 lines · Sergey Fedorov
Quoted 87 lines · Alex Bennée
Quoted 17 lines · [email protected] writes:

From: Paolo Bonzini <[email protected]>

Use a continue statement.

[Sergey Fedorov: Fix moving to list head in case of no TB]
---
 cpu-exec.c | 50 +++++++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 25 deletions(-)
cpu-exec.c +0 -0
index fd92452f16f6..f90482eff778 100644
@@ -225,37 +225,37 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     phys_pc = get_page_addr_code(env, pc);

cpu-exec.c static TranslationBlock *tb_find_physical(CPUState *cpu, · line 230

Quoted 10 lines
cpu-exec.c +3 -5
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
     h = tb_phys_hash_func(phys_pc);
-    ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
-    for(;;) {
-        tb = *ptb1;
-        if (!tb) {
-            return NULL;
+    for (ptb1 = &tcg_ctx.tb_ctx.tb_phys_hash[h];
+         (tb = *ptb1) != NULL;
+         ptb1 = &tb->phys_hash_next) {

I'm not sure I'm keen on the assignment in the for condition clause. I appreciate the cleansing of the if !tb return exit though. Could we be cleaner maybe? Here is my attempt:

    static TranslationBlock *tb_find_physical(CPUState *cpu,
                                              target_ulong pc,
                                              target_ulong cs_base,
                                              uint64_t flags)
    {
        CPUArchState *env = (CPUArchState *)cpu->env_ptr;
        TranslationBlock *tb, **tb_hash_head, **ptb1;
        unsigned int h;
        tb_page_addr_t phys_pc, phys_page1;

        /* find translated block using physical mappings */
        phys_pc = get_page_addr_code(env, pc);
        phys_page1 = phys_pc & TARGET_PAGE_MASK;
        h = tb_phys_hash_func(phys_pc);

        /* Start at head of the hash entry */
        ptb1 = tb_hash_head = &tcg_ctx.tb_ctx.tb_phys_hash[h];
        tb = *ptb1;

        while (tb) {

            if (tb->pc == pc &&
                tb->page_addr[0] == phys_page1 &&
                tb->cs_base == cs_base &&
                tb->flags == flags) {

                if (tb->page_addr[1] == -1) {
                    /* done, we have a match */
                    break;
                } else {
                    /* check next page if needed */
                    target_ulong virt_page2 = (pc & TARGET_PAGE_MASK)
                        + TARGET_PAGE_SIZE;
                    tb_page_addr_t phys_page2 = get_page_addr_code(env, virt_page2);

                    if (tb->page_addr[1] == phys_page2) {
                        break;
                    }
                }
            }

            ptb1 = &tb->phys_hash_next;
            tb = *ptb1;
        }

        if (tb) {
            /* Move the TB to the head of the list */
            *ptb1 = tb->phys_hash_next;
            tb->phys_hash_next = *tb_hash_head;
            *tb_hash_head = tb;
        }
        return tb;
    }
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

FWIW the compiled code is 9 bytes shorter on my machine.

Looks like another attempt to rewrite it. I am wondering whom to attribute as an author, then? :)

I don't really care. :)

Alex, could you give your s-o-b for your variant of code? Or would you like to make a patch by yourself?

Sure here:

Quoted 3 lines

Kind regards,
Sergey

--
Alex Bennée

#12 lore raw

Re: [Qemu-devel] [PATCH 2/5] tcg: reorganize tb_find_physical loop

Quoted 6 lines · Alex Bennée
Quoted 2 lines

Alex, could you give your s-o-b for your variant of code? Or would you

Quoted 1 line

like to make a patch by yourself?

Sure here:

So I'm going to respin the series quickly dropping the last two contentious patches so far. It would give this series a chance for to get in 2.6.

Kind regards,
Sergey

#13 lore raw

PATCH 3/5 tcg: always keep jump target and tb->jmp_next consistent

In reply to [email protected]

From: Paolo Bonzini <[email protected]>

Simple code simplification.

---
 translate-all.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
translate-all.c +9 -10
index f17ace1ae899..a1ac9841de48 100644
@@ -927,6 +927,14 @@ static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
     }
 }
 
+/* reset the jump entry 'n' of a TB so that it is not chained to
+   another TB */
+static inline void tb_reset_jump(TranslationBlock *tb, int n)
+{
+    tb_set_jmp_target(tb, n, (uintptr_t)(tb->tc_ptr + tb->tb_next_offset[n]));
+    tb->jmp_next[n] = NULL;
+}
+
 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
 {
     TranslationBlock *tb1, **ptb;
@@ -951,18 +959,10 @@ static inline void tb_jmp_remove(TranslationBlock *tb, int n)
         }
         /* now we can suppress tb(n) from the list */
         *ptb = tb->jmp_next[n];
-
-        tb->jmp_next[n] = NULL;
+        tb_reset_jump(tb, n);
     }
 }
 
-/* reset the jump entry 'n' of a TB so that it is not chained to
-   another TB */
-static inline void tb_reset_jump(TranslationBlock *tb, int n)
-{
-    tb_set_jmp_target(tb, n, (uintptr_t)(tb->tc_ptr + tb->tb_next_offset[n]));
-}
-
 /* invalidate one TB */
 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
 {
@@ -1013,7 +1013,6 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
         tb1 = (TranslationBlock *)((uintptr_t)tb1 & ~3);
         tb2 = tb1->jmp_next[n1];
         tb_reset_jump(tb1, n1);
-        tb1->jmp_next[n1] = NULL;
         tb1 = tb2;
     }
     tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2); /* fail safe */
Signature · 1 line
2.7.3
#14 lore raw

Re: [Qemu-devel] [PATCH 3/5] tcg: always keep jump target and tb->jmp_next consistent

Quoted 30 lines · On 03/17/2016 06:46 AM, [email protected] wrote:

From: Paolo Bonzini <[email protected]>

Simple code simplification.

---
 translate-all.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
translate-all.c +8 -0
index f17ace1ae899..a1ac9841de48 100644
@@ -927,6 +927,14 @@ static inline void tb_page_remove(TranslationBlock **ptb, TranslationBlock *tb)
     }
 }
 
+/* reset the jump entry 'n' of a TB so that it is not chained to
+   another TB */
+static inline void tb_reset_jump(TranslationBlock *tb, int n)
+{
+    tb_set_jmp_target(tb, n, (uintptr_t)(tb->tc_ptr + tb->tb_next_offset[n]));
+    tb->jmp_next[n] = NULL;
+}
+
 static inline void tb_jmp_remove(TranslationBlock *tb, int n)
 {
     TranslationBlock *tb1, **ptb;

translate-all.c static inline void tb_jmp_remove(TranslationBlock *tb, int n) · line 962

Quoted 6 lines
translate-all.c +1 -2
@@ -951,18 +959,10 @@ static inline void tb_jmp_remove(TranslationBlock *tb, int n)
         }
         /* now we can suppress tb(n) from the list */
         *ptb = tb->jmp_next[n];
-
-        tb->jmp_next[n] = NULL;
+        tb_reset_jump(tb, n);

What's the motivation here? This implies an extra cache flush. Where were we resetting the jump previously? Or is this a bug in that we *weren't* resetting the jump previously?

r~

#15 lore raw

Re: [Qemu-devel] [PATCH 3/5] tcg: always keep jump target and tb->jmp_next consistent

Quoted 11 lines · Richard Henderson

translate-all.c static inline void tb_jmp_remove(TranslationBlock *tb, int n) · line 962

Quoted 7 lines
translate-all.c +1 -2
@@ -951,18 +959,10 @@ static inline void tb_jmp_remove(TranslationBlock *tb, int n)
         }
         /* now we can suppress tb(n) from the list */
         *ptb = tb->jmp_next[n];
-
-        tb->jmp_next[n] = NULL;
+        tb_reset_jump(tb, n);

What's the motivation here? This implies an extra cache flush. Where were we resetting the jump previously? Or is this a bug in that we *weren't* resetting the jump previously?

Indeed I think this patch can be removed if it has a performance effect on machines that require icache invalidation. If it doesn't, it would be just a small code simplification.

Paolo

#16 lore raw

Re: [Qemu-devel] [PATCH 3/5] tcg: always keep jump target and tb->jmp_next consistent

Quoted 12 lines · Paolo Bonzini
Quoted 10 lines · Richard Henderson

translate-all.c static inline void tb_jmp_remove(TranslationBlock *tb, int n) · line 962

Quoted 7 lines
translate-all.c +1 -2
@@ -951,18 +959,10 @@ static inline void tb_jmp_remove(TranslationBlock *tb, int n)
          }
          /* now we can suppress tb(n) from the list */
          *ptb = tb->jmp_next[n];
-
-        tb->jmp_next[n] = NULL;
+        tb_reset_jump(tb, n);

What's the motivation here? This implies an extra cache flush. Where were we resetting the jump previously? Or is this a bug in that we *weren't* resetting the jump previously?

Quoted 3 lines

Indeed I think this patch can be removed if it has a performance effect on machines that require icache invalidation. If it doesn't, it would be just a small code simplification.

In fact, tb_jmp_remove() is only supposed to remove the TB from a list of all TB's jumping to the same TB which is n-th jump destination of the given TB. This function is only called in tb_phys_invalidate() for the TB being invalidated. Thus we don't have to patch that TB anymore. We don't even have to do "tb->jmp_next[n] = NULL" here.

Probably it's time to audit the code that handles direct jumping and clean-up/document/rename things to make it more easy to understand? :)

Kind regards,
Sergey

#17 lore raw

Re: [Qemu-devel] [PATCH 3/5] tcg: always keep jump target and tb->jmp_next consistent

Quoted 2 lines · Sergey Fedorov

Probably it's time to audit the code that handles direct jumping and clean-up/document/rename things to make it more easy to understand? :)

Seconded!

r~

#18 lore raw

Re: [Qemu-devel] [PATCH 3/5] tcg: always keep jump target and tb->jmp_next consistent

Quoted 4 lines · Richard Henderson
Quoted 2 lines · Sergey Fedorov

Probably it's time to audit the code that handles direct jumping and clean-up/document/rename things to make it more easy to understand? :)

Seconded!

I'll go for it, then :)

Kind regards,
Sergey

#19 lore raw

Re: [Qemu-devel] [PATCH 3/5] tcg: always keep jump target and tb->jmp_next consistent

In reply to Sergey Fedorov
Quoted 14 lines · Sergey Fedorov
Quoted 13 lines · Paolo Bonzini
Quoted 11 lines · Richard Henderson

translate-all.c static inline void · line 963

Quoted 8 lines
translate-all.c +1 -2
@@ -951,18 +959,10 @@ static inline void 
 tb_jmp_remove(TranslationBlock *tb, int n)
          }
          /* now we can suppress tb(n) from the list */
          *ptb = tb->jmp_next[n];
-
-        tb->jmp_next[n] = NULL;
+        tb_reset_jump(tb, n);

What's the motivation here? This implies an extra cache flush. Where were we resetting the jump previously? Or is this a bug in that we *weren't* resetting the jump previously?

Quoted 5 lines
Quoted 3 lines

Indeed I think this patch can be removed if it has a performance effect on machines that require icache invalidation. If it doesn't, it would be just a small code simplification.

In fact, tb_jmp_remove() is only supposed to remove the TB from a list of all TB's jumping to the same TB which is n-th jump destination of the given TB. This function is only called in tb_phys_invalidate() for the TB being invalidated. Thus we don't have to patch that TB anymore. We don't even have to do "tb->jmp_next[n] = NULL" here.

I'll drop the patch from the series in v2 then.

Kind regards,
Sergey

#20 lore raw

PATCH 4/5 tcg: reorder removal from lists in tb_phys_invalidate

In reply to [email protected]

From: Paolo Bonzini <[email protected]>

First the translation block is invalidated, for which a simple write to tb->pc is enough. This means that cpu-exec will not pick up anymore the block, though it may still execute it through chained jumps. This also replaces the NULLing out of the pointer in the CPUs' local cache.

Then the chained jumps are removed, meaning that CPUs will only execute the translation block once after this point.

Finally, the TB is removed from the per-page list and the phys-hash bucket to clean up the data structure.

This has no effect for now, but it will be the right order when tb_find_fast is moved outside the tb_lock.

---
 translate-all.c | 56 ++++++++++++++++++++++++++++----------------------------
 1 file changed, 28 insertions(+), 28 deletions(-)
translate-all.c +28 -28
index a1ac9841de48..1db5a914d9a3 100644
@@ -966,40 +966,21 @@ static inline void tb_jmp_remove(TranslationBlock *tb, int n)
 /* invalidate one TB */
 void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
 {
-    CPUState *cpu;
     PageDesc *p;
     unsigned int h, n1;
+    tb_page_addr_t pc;
     tb_page_addr_t phys_pc;
     TranslationBlock *tb1, *tb2;
 
-    /* remove the TB from the hash list */
-    phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
-    h = tb_phys_hash_func(phys_pc);
-    tb_hash_remove(&tcg_ctx.tb_ctx.tb_phys_hash[h], tb);
-
-    /* remove the TB from the page list */
-    if (tb->page_addr[0] != page_addr) {
-        p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);
-        invalidate_page_bitmap(p);
-    }
-    if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
-        p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);
-        invalidate_page_bitmap(p);
-    }
-
-    tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
-
-    /* remove the TB from the hash list */
-    h = tb_jmp_cache_hash_func(tb->pc);
-    CPU_FOREACH(cpu) {
-        if (cpu->tb_jmp_cache[h] == tb) {
-            cpu->tb_jmp_cache[h] = NULL;
-        }
-    }
+    /* First invalidate the translation block.  CPUs will not use it anymore
+     * from their local caches.
+     */
+    pc = tb->pc;
+    tb->pc = -1;
 
-    /* suppress this TB from the two jump lists */
+    /* Then suppress this TB from the two jump lists.  CPUs will not jump
+     * anymore into this translation block.
+     */
     tb_jmp_remove(tb, 0);
     tb_jmp_remove(tb, 1);
 
@@ -1017,6 +998,25 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
     }
     tb->jmp_first = (TranslationBlock *)((uintptr_t)tb | 2); /* fail safe */
 
+    /* Now remove the TB from the hash list, so that tb_find_slow
+     * cannot find it anymore.
+     */
+    phys_pc = tb->page_addr[0] + (pc & ~TARGET_PAGE_MASK);
+    h = tb_phys_hash_func(phys_pc);
+    tb_hash_remove(&tcg_ctx.tb_ctx.tb_phys_hash[h], tb);
+
+    /* remove the TB from the page list */
+    if (tb->page_addr[0] != page_addr) {
+        p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
+        tb_page_remove(&p->first_tb, tb);
+        invalidate_page_bitmap(p);
+    }
+    if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
+        p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
+        tb_page_remove(&p->first_tb, tb);
+        invalidate_page_bitmap(p);
+    }
+
     tcg_ctx.tb_ctx.tb_phys_invalidate_count++;
 }
 
Signature · 1 line
2.7.3
#21 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 18 lines · On 17/03/2016 14:46, [email protected] wrote:

void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr) {

translate-all.c +1 -10
-    CPUState *cpu;
     PageDesc *p;
     unsigned int h, n1;
+    tb_page_addr_t pc;
     tb_page_addr_t phys_pc;
     TranslationBlock *tb1, *tb2;
 
-    /* remove the TB from the hash list */
-    phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
-    h = tb_phys_hash_func(phys_pc);
-    tb_hash_remove(&tcg_ctx.tb_ctx.tb_phys_hash[h], tb);
-
-    /* remove the TB from the page list */
-    if (tb->page_addr[0] != page_addr) {
-        p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);

translate-all.c static inline void tb_jmp_remove(TranslationBlock *tb, int n) · line 993

Quoted 10 lines
translate-all.c +0 -10
-        invalidate_page_bitmap(p);
-    }
-    if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
-        p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);
-        invalidate_page_bitmap(p);
-    }
-
-    tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
-

Did you investigate the removal of this setting of tb_invalidated_flag?

My recollection is that it is okay to remove it because at worse it would cause a tb_add_jump from an invalidated source to a valid destination. This should be harmless as long as the source has been tb_phys_invalidated and not tb_flushed. But this needs to be checked.

Paolo

#22 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 32 lines · Paolo Bonzini
Quoted 18 lines · On 17/03/2016 14:46, [email protected] wrote:
  void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr)
  {
translate-all.c +1 -10
-    CPUState *cpu;
      PageDesc *p;
      unsigned int h, n1;
+    tb_page_addr_t pc;
      tb_page_addr_t phys_pc;
      TranslationBlock *tb1, *tb2;
 
-    /* remove the TB from the hash list */
-    phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
-    h = tb_phys_hash_func(phys_pc);
-    tb_hash_remove(&tcg_ctx.tb_ctx.tb_phys_hash[h], tb);
-
-    /* remove the TB from the page list */
-    if (tb->page_addr[0] != page_addr) {
-        p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);

translate-all.c static inline void tb_jmp_remove(TranslationBlock *tb, int n) · line 993

Quoted 10 lines
translate-all.c +0 -10
-        invalidate_page_bitmap(p);
-    }
-    if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
-        p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);
-        invalidate_page_bitmap(p);
-    }
-
-    tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
-

Did you investigate the removal of this setting of tb_invalidated_flag?

Quoted 4 lines

My recollection is that it is okay to remove it because at worse it would cause a tb_add_jump from an invalidated source to a valid destination. This should be harmless as long as the source has been tb_phys_invalidated and not tb_flushed. But this needs to be checked.

Thanks for pointing that. I should investigate it to make sure, although arm32/arm64/x86_64 Linux boots fine as well as the latest Alex's kmv-unit-tests pass.

Kind regards,
Sergey

#23 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 33 lines · Sergey Fedorov
Quoted 32 lines · Paolo Bonzini
Quoted 18 lines · On 17/03/2016 14:46, [email protected] wrote:

void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t
page_addr)
{

translate-all.c +1 -9
-    CPUState *cpu;
      PageDesc *p;
      unsigned int h, n1;
+    tb_page_addr_t pc;
      tb_page_addr_t phys_pc;
      TranslationBlock *tb1, *tb2;
  -    /* remove the TB from the hash list */
-    phys_pc = tb->page_addr[0] + (tb->pc & ~TARGET_PAGE_MASK);
-    h = tb_phys_hash_func(phys_pc);
-    tb_hash_remove(&tcg_ctx.tb_ctx.tb_phys_hash[h], tb);
-
-    /* remove the TB from the page list */
-    if (tb->page_addr[0] != page_addr) {
-        p = page_find(tb->page_addr[0] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);

translate-all.c static inline void tb_jmp_remove(TranslationBlock *tb, int n) · line 993

Quoted 10 lines
translate-all.c +0 -10
-        invalidate_page_bitmap(p);
-    }
-    if (tb->page_addr[1] != -1 && tb->page_addr[1] != page_addr) {
-        p = page_find(tb->page_addr[1] >> TARGET_PAGE_BITS);
-        tb_page_remove(&p->first_tb, tb);
-        invalidate_page_bitmap(p);
-    }
-
-    tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
-

Did you investigate the removal of this setting of tb_invalidated_flag?

Quoted 8 lines
Quoted 4 lines

My recollection is that it is okay to remove it because at worse it would cause a tb_add_jump from an invalidated source to a valid destination. This should be harmless as long as the source has been tb_phys_invalidated and not tb_flushed. But this needs to be checked.

Thanks for pointing that. I should investigate it to make sure, although arm32/arm64/x86_64 Linux boots fine as well as the latest Alex's kmv-unit-tests pass.

The use pattern of 'tb_invalidated_flag' is a bit intricate; correct me, if I'm wrong about the following. Basically, 'tb_invalidated_flag' was meant to catch two events:
* some TB has been invalidated by tb_phys_invalidate();
* the whole translation buffer has been flushed by tb_flush().

Then it is checked to ensure:
* the last executed TB can be safely patched to directly call the next one in cpu_exec();
* the original TB should be provided for further possible invalidation along with the temporarily generated TB when in cpu_exec_nocache().

What, I think, we couldn't be sure in is the cpu_exec_nocache() case. It could look like a kind of corner case, but it could result in stalls, if the original TB isn't invalidated properly, see b4ac20b4df0d ("cpu-exec:
fix cpu_exec_nocache").

So I would suggest the following solution:s
(1) Use 'tb->pc' as an indicator of whether TB is valid; check for it in cpu_exec() when deciding on whether to patch the last executed TB or not
(2) Use 'tcg_ctx.tb_ctx.tb_flush_count' to check for translation buffer flushes; capture it before calling tb_gen_code() and compare to it afterwards to check if tb_flush() has been called in between

What do you think?

Kind regards,
Sergey

#24 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 4 lines · Sergey Fedorov

The use pattern of 'tb_invalidated_flag' is a bit intricate; correct me, if I'm wrong about the following. Basically, 'tb_invalidated_flag' was meant to catch two events:
* some TB has been invalidated by tb_phys_invalidate();

This is patch 4.

Quoted 1 line

* the whole translation buffer has been flushed by tb_flush().

This is patch 5.

Quoted 6 lines

Then it is checked to ensure:
* the last executed TB can be safely patched to directly call the next one in cpu_exec();
* the original TB should be provided for further possible invalidation along with the temporarily generated TB when in cpu_exec_nocache().

Quoted 7 lines

[...] I would suggest the following solution:
(1) Use 'tb->pc' as an indicator of whether TB is valid; check for it in cpu_exec() when deciding on whether to patch the last executed TB or not
(2) Use 'tcg_ctx.tb_ctx.tb_flush_count' to check for translation buffer flushes; capture it before calling tb_gen_code() and compare to it afterwards to check if tb_flush() has been called in between

Of course that would work, but it would be slower. I think it is unnecessary for two reasons:

1) There are two calls to cpu_exec_nocache. One exits immediately with "break;", the other always sets "next_tb = 0;". Therefore it is safe in both cases for cpu_exec_nocache to hijack cpu->tb_invalidated_flag.

2) if it were broken, it would _also_ be broken before these patches because cpu_exec_nocache always runs with tb_lock taken. So I think documenting the assumptions is better than changing them at the same time as doing other changes.

Your observation that tb->pc==-1 is not necessarily safe still holds of course. Probably the best thing is an inline that can do one of:

1) set cs_base to an invalid value (anything nonzero is enough except on x86 and SPARC; SPARC can use all-ones)

2) sets the flags to an invalid combination (x86 can use all ones)

3) sets the PC to an invalid value (no one really needs it)

Paolo

#25 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 17 lines · Paolo Bonzini
Quoted 4 lines · Sergey Fedorov

The use pattern of 'tb_invalidated_flag' is a bit intricate; correct me, if I'm wrong about the following. Basically, 'tb_invalidated_flag' was meant to catch two events:
* some TB has been invalidated by tb_phys_invalidate();

This is patch 4.

Quoted 1 line

* the whole translation buffer has been flushed by tb_flush().

This is patch 5.

Quoted 6 lines

Then it is checked to ensure:
* the last executed TB can be safely patched to directly call the next one in cpu_exec();
* the original TB should be provided for further possible invalidation along with the temporarily generated TB when in cpu_exec_nocache().

Quoted 8 lines
Quoted 7 lines

[...] I would suggest the following solution:
(1) Use 'tb->pc' as an indicator of whether TB is valid; check for it in cpu_exec() when deciding on whether to patch the last executed TB or not
(2) Use 'tcg_ctx.tb_ctx.tb_flush_count' to check for translation buffer flushes; capture it before calling tb_gen_code() and compare to it afterwards to check if tb_flush() has been called in between

Of course that would work, but it would be slower.

What's going to be slower?

Quoted 9 lines

I think it is
unnecessary for two reasons:

1) There are two calls to cpu_exec_nocache. One exits immediately with "break;", the other always sets "next_tb = 0;". Therefore it is safe in both cases for cpu_exec_nocache to hijack cpu->tb_invalidated_flag.

2) if it were broken, it would _also_ be broken before these patches because cpu_exec_nocache always runs with tb_lock taken.

I can't see how cpu_exec_nocache() always runs with tb_lock taken.

Quoted 3 lines

So I think
documenting the assumptions is better than changing them at the same time as doing other changes.

I'm not sure I understand you here exactly, but if implementing my proposal, it'd rather be a separate patch/series, I think.

Quoted 9 lines

Your observation that tb->pc==-1 is not necessarily safe still holds of course. Probably the best thing is an inline that can do one of:

1) set cs_base to an invalid value (anything nonzero is enough except on x86 and SPARC; SPARC can use all-ones)

2) sets the flags to an invalid combination (x86 can use all ones)

3) sets the PC to an invalid value (no one really needs it)

It's a bit tricky. Does it really worth doing so instead of using a separate dedicated flag? Mainly, it should cost one extra compare on TB look-up. I suppose it's a kind of trade-off between performance and code clarity.

Kind regards,
Sergey

#26 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 9 lines · Sergey Fedorov
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines

[...] I would suggest the following solution:
(1) Use 'tb->pc' as an indicator of whether TB is valid; check for it in cpu_exec() when deciding on whether to patch the last executed TB or not
(2) Use 'tcg_ctx.tb_ctx.tb_flush_count' to check for translation buffer flushes; capture it before calling tb_gen_code() and compare to it afterwards to check if tb_flush() has been called in between

Of course that would work, but it would be slower.

What's going to be slower?

Checking tb->pc (or something similar) *and* tb_flush_count in tb_find_physical.

Quoted 11 lines
Quoted 9 lines

I think it is
unnecessary for two reasons:

1) There are two calls to cpu_exec_nocache. One exits immediately with "break;", the other always sets "next_tb = 0;". Therefore it is safe in both cases for cpu_exec_nocache to hijack cpu->tb_invalidated_flag.

2) if it were broken, it would _also_ be broken before these patches because cpu_exec_nocache always runs with tb_lock taken.

I can't see how cpu_exec_nocache() always runs with tb_lock taken.

It takes the lock itself. :)

Quoted 1 line

From Fred's "tcg: protect TBContext with tb_lock", as it is in my mttcg

branch:

@@ -194,17 +194,23 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,
if (max_cycles > CF_COUNT_MASK)
max_cycles = CF_COUNT_MASK;

+    tb_lock();
     cpu->tb_invalidated_flag = 0;
     tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
                      max_cycles | CF_NOCACHE);
     tb->orig_tb = cpu->tb_invalidated_flag ? NULL : orig_tb;
     cpu->current_tb = tb;
+    tb_unlock();
+
     /* execute the generated code */
     trace_exec_tb_nocache(tb, tb->pc);
     cpu_tb_exec(cpu, tb->tc_ptr);
+
+    tb_lock();
     cpu->current_tb = NULL;
     tb_phys_invalidate(tb, -1);
     tb_free(tb);

+ tb_unlock();
}
#endif

It takes the lock before resetting tb_invalidated_flag.

cpu_exec_nocache is not used in user-mode emulation, so it's okay if qemu.git doesn't take the lock yet. (This kind of misunderstanding about which code is thread-safe is going to be common until we have MTTCG. This was the reason for the patch "cpu-exec: elide more icount code if CONFIG_USER_ONLY").

Quoted 6 lines
Quoted 3 lines

So I think
documenting the assumptions is better than changing them at the same time as doing other changes.

I'm not sure I understand you here exactly, but if implementing my proposal, it'd rather be a separate patch/series, I think.

Exactly. For the purpose of these 5 patches, I would just document above cpu_exec_nocache that callers should ensure that next_tb is zero.

Alternatively, you could add a patch that does

    old_tb_invalidated_flag = cpu->tb_invalidated_flag;
    cpu->tb_invalidated_flag = 0;
    ...
    cpu->tb_invalidated_flag |= old_tb_invalidated_flag;

it could use the single global flag (and then it would be between patch 4 and patch 5) or it could use the CPU-specific one (and then it would be after patch 5).

However, I think documenting the requirements is fine.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Your observation that tb->pc==-1 is not necessarily safe still holds of course. Probably the best thing is an inline that can do one of:

1) set cs_base to an invalid value (anything nonzero is enough except on x86 and SPARC; SPARC can use all-ones)

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

2) sets the flags to an invalid combination (x86 can use all ones)

3) sets the PC to an invalid value (no one really needs it)

It's a bit tricky. Does it really worth doing so instead of using a separate dedicated flag? Mainly, it should cost one extra compare on TB look-up. I suppose it's a kind of trade-off between performance and code clarity.

I think a new inline function cpu_make_tb_invalid would not be too tricky. Just setting "tb->cs_base = -1;" is pretty much obvious for all the targets that do not use cs_base at all and for SPARC which sets it to a PC (and thus a multiple of four). x86 is the odd one out.

Thanks,

Paolo

#27 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 5 lines · Paolo Bonzini

cpu_exec_nocache is not used in user-mode emulation, so it's okay if qemu.git doesn't take the lock yet. (This kind of misunderstanding about which code is thread-safe is going to be common until we have MTTCG. This was the reason for the patch "cpu-exec: elide more icount code if CONFIG_USER_ONLY").

So I'd better pull in here the rebased version of this patch from Alex's "Base enabling patches for MTTCG" series.

Kind regards,
Sergey

#28 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 9 lines · Sergey Fedorov
Quoted 5 lines · Paolo Bonzini

cpu_exec_nocache is not used in user-mode emulation, so it's okay if qemu.git doesn't take the lock yet. (This kind of misunderstanding about which code is thread-safe is going to be common until we have MTTCG. This was the reason for the patch "cpu-exec: elide more icount code if CONFIG_USER_ONLY").

So I'd better pull in here the rebased version of this patch from Alex's "Base enabling patches for MTTCG" series.

OK, sounds good.

--
Alex Bennée

#29 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

In reply to Paolo Bonzini
Quoted 10 lines · Paolo Bonzini
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

Your observation that tb->pc==-1 is not necessarily safe still holds of

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

course. Probably the best thing is an inline that can do one of:

1) set cs_base to an invalid value (anything nonzero is enough except on x86 and SPARC; SPARC can use all-ones)

2) sets the flags to an invalid combination (x86 can use all ones)

3) sets the PC to an invalid value (no one really needs it)

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

It's a bit tricky. Does it really worth doing so instead of using a separate dedicated flag? Mainly, it should cost one extra compare on TB look-up. I suppose it's a kind of trade-off between performance and code clarity.

I think a new inline function cpu_make_tb_invalid would not be too tricky. Just setting "tb->cs_base = -1;" is pretty much obvious for all the targets that do not use cs_base at all and for SPARC which sets it to a PC (and thus a multiple of four). x86 is the odd one out.

So what would you suggest to use for x86? I can't think of something that looks like a really compelling combination when I look at cpu_get_tb_cpu_state() in target-i386/cpu.h. Personally, I'm not so happy trying to use pc/cs_base/flags to mark an invalid TB. Are my worries unreasonable? :) Some other thoughts?

Anyway, I am wondering if there is still a way to clear tb_phys_hash and tb_jmp_cache safely. Maybe something like this:
* Remove the TB from physical hash list
* Memory barrier
* Remove the TB from each vCPU's virtual address hash cache

Would that work?

Kind regards,
Sergey

#30 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 3 lines · Sergey Fedorov

So what would you suggest to use for x86? I can't think of something that looks like a really compelling combination when I look at cpu_get_tb_cpu_state() in target-i386/cpu.h.

On x86 I think we should define HF_INVALID_TB to an invalid flag combination. I can think of several solutions:

- #defining HF_INVALID_TB to an invalid combination (e.g. HF_CS64_MASK, because it always appears together with HF_LMA_MASK; see code that updates those hflags in cpu_x86_load_seg_cache, cpu_x86_update_cr0, kvm_get_sregs). Advantage: doesn't waste a bit, reasonably self documenting. Disadvantage: a bit tricky, but still my favorite.

- rename HF_SOFTMMU_MASK to HF_INVALID_MASK (it's always the same as CONFIG_SOFTMMU so we can remove it), then #define HF_INVALID_TB HF_INVALID_MASK. Advantage: obviously correct. Disadvantage: wastes a bit. My second favorite.

- #defining HF_INVALID_TB to -1. Advantage: ?!? Disadvantage:
everything. Looks tame, actually a huge hack

- #defining HF_INVALID_TB to the "wrong" direction HF_SOFTMMU_MASK (i.e. to 0 if CONFIG_SOFTMMU, . Advantage: obviously correct. Disadvantage:
huge hack, HF_SOFTMMU_MASK is unused anyway.

Choose your own favorite. :)

(Setting cs_base to -1 actually would work on 64-bit x86, but not on qemu-system-i386).

Quoted 3 lines

Personally, I'm not so
happy trying to use pc/cs_base/flags to mark an invalid TB. Are my worries unreasonable? :)

Can you explain your worries?

The advantages are that it's O(1) and it obviously doesn't affect other TBs than the invalidated one.

Quoted 5 lines

Anyway, I am wondering if there is still a way to clear tb_phys_hash and tb_jmp_cache safely.

Maybe something like this:
* Remove the TB from physical hash list

So at this point tb_find_slow cannot find it.

Quoted 2 lines

* Memory barrier
* Remove the TB from each vCPU's virtual address hash cache

tb_find_fast then cannot find it either.

Quoted 1 line

Would that work?

This is very similar to the current code. From 10,000 feet, because tb_find_fast calls tb_find_slow, this could indeed work, but I'm a bit concerned about how to order the removal of the jump lists. The usage of "tcg_ctx.tb_ctx.tb_invalidated_flag = 1" in the existing code was what worries me. Indeed the motivation of this patch was removing that single line of code to prepare for the move of tb_invalidated_flag to CPUState.

Also, this loop will not be thread-safe anymore as soon as Fred's "tb_jmp_cache lookup outside tb_lock" goes in:

    CPU_FOREACH(cpu) {
        if (cpu->tb_jmp_cache[h] == tb) {
            cpu->tb_jmp_cache[h] = NULL;
        }
    }

It should use atomic_cmpxchg (slow!) or to unconditionally NULL out cpu->tb_jmp_cache (a bit hacky). Preparing for that change is an added bonus of the tb-hacking approach.

Paolo

#31 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 7 lines · Paolo Bonzini

This is very similar to the current code. From 10,000 feet, because tb_find_fast calls tb_find_slow, this could indeed work, but I'm a bit concerned about how to order the removal of the jump lists. The usage of "tcg_ctx.tb_ctx.tb_invalidated_flag = 1" in the existing code was what worries me. Indeed the motivation of this patch was removing that single line of code to prepare for the move of tb_invalidated_flag to CPUState.

I'm just preparing a patch with a long commit message which describes and removes this setting of tb_invalidated_flag :) I think we can do this safely and even benefit in performance.

Quoted 10 lines

Also, this loop will not be thread-safe anymore as soon as Fred's "tb_jmp_cache lookup outside tb_lock" goes in:

    CPU_FOREACH(cpu) {
        if (cpu->tb_jmp_cache[h] == tb) {
            cpu->tb_jmp_cache[h] = NULL;
        }
    }
Quoted 3 lines

It should use atomic_cmpxchg (slow!) or to unconditionally NULL out cpu->tb_jmp_cache (a bit hacky). Preparing for that change is an added bonus of the tb-hacking approach.

IIUC we always modify tb_jmp_cache/tb_phys_hash under tb_lock. We're just gonna do tb_jmp_cache lookup outside of tb_lock. I think write memory barrier in tb_phys_invalidate() paired with read memory memory barrier in tb_find_fast()/tb_find_slow() would be enough to make it safe. Also we need to use atomic read/set to tb_jmp_cache but cmpxchg is not necessary. So I'm going to give it a thought then.

Kind regards,
Sergey

#32 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 6 lines · Sergey Fedorov

IIUC we always modify tb_jmp_cache/tb_phys_hash under tb_lock. We're just gonna do tb_jmp_cache lookup outside of tb_lock. I think write memory barrier in tb_phys_invalidate() paired with read memory memory barrier in tb_find_fast()/tb_find_slow() would be enough to make it safe. Also we need to use atomic read/set to tb_jmp_cache but cmpxchg is not necessary. So I'm going to give it a thought then.

Sounds good!

Paolo

#33 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

In reply to Paolo Bonzini
Quoted 3 lines · Paolo Bonzini

This is very similar to the current code. From 10,000 feet, because tb_find_fast calls tb_find_slow, this could indeed work, but I'm a bit concerned about how to order the removal of the jump lists.

As long as we always link/unlink TBs under tb_lock the exact order doesn't seem to be important.

Kind regards,
Sergey

#34 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini
Quoted 3 lines · Sergey Fedorov

Personally, I'm not so
happy trying to use pc/cs_base/flags to mark an invalid TB. Are my worries unreasonable? :)

Can you explain your worries?

The advantages are that it's O(1) and it obviously doesn't affect other TBs than the invalidated one.

Well, it looks a bit hucky, I don't like hucky code because it is each time hard to read, modify and be confident in it. I would like to introduce a dedicated boolean field to mark a TB as valid/invalid. But this approach adds one more compare in tb_find_fast() and tb_find_slow(). Luckily, it may be not an issue if my proposal with memory barriers and atomic access to 'tb_jmp_cache' solves the problem.

Kind regards,
Sergey

#35 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

In reply to [email protected]
Quoted 4 lines · On 17/03/16 16:46, [email protected] wrote:

First the translation block is invalidated, for which a simple write to tb->pc is enough. This means that cpu-exec will not pick up anymore the block, though it may still execute it through chained jumps. This also replaces the NULLing out of the pointer in the CPUs' local cache.

Although, using 'tb->pc' to mark a TB as invalid is probably not such a good idea. There may be some cases when PC could become equal to -1. For example, ARMv6-M uses PC >= 0xFFFFFFF0 to perform exception return. So we'd better introduce a separate 'tb->valid' or 'tb->invalid' flag.

Kind regards,
Sergey

#36 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 10 lines · Sergey Fedorov
Quoted 4 lines · On 17/03/16 16:46, [email protected] wrote:

First the translation block is invalidated, for which a simple write to tb->pc is enough. This means that cpu-exec will not pick up anymore the block, though it may still execute it through chained jumps. This also replaces the NULLing out of the pointer in the CPUs' local cache.

Although, using 'tb->pc' to mark a TB as invalid is probably not such a good idea. There may be some cases when PC could become equal to -1. For example, ARMv6-M uses PC >= 0xFFFFFFF0 to perform exception return. So we'd better introduce a separate 'tb->valid' or 'tb->invalid' flag.

It is also possible to use tb->flags for that. I suspect that all-ones tb flags is never valid, but it could also be a #define.

Paolo

#37 lore raw

Re: [Qemu-devel] [PATCH 4/5] tcg: reorder removal from lists in tb_phys_invalidate

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Sergey Fedorov
Quoted 4 lines · On 17/03/16 16:46, [email protected] wrote:

First the translation block is invalidated, for which a simple write to tb->pc is enough. This means that cpu-exec will not pick up anymore the block, though it may still execute it through chained jumps. This also replaces the NULLing out of the pointer in the CPUs' local cache.

Quoted 7 lines
Quoted 4 lines

Although, using 'tb->pc' to mark a TB as invalid is probably not such a good idea. There may be some cases when PC could become equal to -1. For example, ARMv6-M uses PC >= 0xFFFFFFF0 to perform exception return. So we'd better introduce a separate 'tb->valid' or 'tb->invalid' flag.

It is also possible to use tb->flags for that. I suspect that all-ones tb flags is never valid, but it could also be a #define.

That might work by accident, but it might not. You'd need to reserve a bit across all of the targets.

r~

#38 lore raw

PATCH 5/5 tcg: move tb_invalidated_flag to CPUState

In reply to [email protected]

From: Paolo Bonzini <[email protected]>

This is a baby step towards making tb_flush thread safe.

---
 cpu-exec.c              | 11 +++++------
 include/exec/exec-all.h |  2 --
 include/qom/cpu.h       |  2 ++
 translate-all.c         |  3 +--
 4 files changed, 8 insertions(+), 10 deletions(-)
cpu-exec.c +5 -6
index f90482eff778..07545aa91082 100644
@@ -195,10 +195,11 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,
     if (max_cycles > CF_COUNT_MASK)
         max_cycles = CF_COUNT_MASK;
 
+    cpu->tb_invalidated_flag = 0;
     tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
                      max_cycles | CF_NOCACHE
                          | (ignore_icount ? CF_IGNORE_ICOUNT : 0));
-    tb->orig_tb = tcg_ctx.tb_ctx.tb_invalidated_flag ? NULL : orig_tb;
+    tb->orig_tb = cpu->tb_invalidated_flag ? NULL : orig_tb;
     cpu->current_tb = tb;
     /* execute the generated code */
     trace_exec_tb_nocache(tb, tb->pc);
@@ -219,8 +220,6 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     tb_page_addr_t phys_pc, phys_page1;
     target_ulong virt_page2;
 
-    tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
-
     /* find translated block using physical mappings */
     phys_pc = get_page_addr_code(env, pc);
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
@@ -288,6 +287,7 @@ static TranslationBlock *tb_find_slow(CPUState *cpu,
 #endif
 
     /* if no translated code available, then translate it now */
+    cpu->tb_invalidated_flag = 0;
     tb = tb_gen_code(cpu, pc, cs_base, flags, 0);
 
 #ifdef CONFIG_USER_ONLY
@@ -493,12 +493,11 @@ int cpu_exec(CPUState *cpu)
                 tb = tb_find_fast(cpu);
                 /* Note: we do it here to avoid a gcc bug on Mac OS X when
                    doing it in tb_find_slow */
-                if (tcg_ctx.tb_ctx.tb_invalidated_flag) {
+                if (cpu->tb_invalidated_flag) {
                     /* as some TB could have been invalidated because
-                       of memory exceptions while generating the code, we
+                       of a tb_flush while generating the code, we
                        must recompute the hash index here */
                     next_tb = 0;
-                    tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
                 }
                 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
                     qemu_log("Trace %p [" TARGET_FMT_lx "] %s\n",
include/exec/exec-all.h +0 -2
index 05a151da4a54..0ef6ea5cf6dc 100644
@@ -288,8 +288,6 @@ struct TBContext {
     /* statistics */
     int tb_flush_count;
     int tb_phys_invalidate_count;
-
-    int tb_invalidated_flag;
 };
 
 void tb_free(TranslationBlock *tb);
include/qom/cpu.h +2 -0
index 7052eee7b78a..9538f9cc2af3 100644
@@ -240,6 +240,7 @@ struct kvm_run;
  * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
  * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
  *           CPU and return to its top level loop.
+ * @tb_invalidated_flag: Set to tell TCG that tb_flush has been called.
  * @singlestep_enabled: Flags for single-stepping.
  * @icount_extra: Instructions until next timer event.
  * @icount_decr: Number of cycles left, with interrupt flag in high bit.
@@ -291,6 +292,7 @@ struct CPUState {
     bool stopped;
     bool crash_occurred;
     bool exit_request;
+    bool tb_invalidated_flag;
     uint32_t interrupt_request;
     int singlestep_enabled;
     int64_t icount_extra;
translate-all.c +1 -2
index 1db5a914d9a3..8e1edd6bb633 100644
@@ -843,6 +843,7 @@ void tb_flush(CPUState *cpu)
     tcg_ctx.tb_ctx.nb_tbs = 0;
 
     CPU_FOREACH(cpu) {
+        cpu->tb_invalidated_flag = 1;
         memset(cpu->tb_jmp_cache, 0, sizeof(cpu->tb_jmp_cache));
     }
 
@@ -1079,8 +1080,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
         /* cannot fail at this point */
         tb = tb_alloc(pc);
         assert(tb != NULL);
-        /* Don't forget to invalidate previous TB info.  */
-        tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
     }
 
     gen_code_buf = tcg_ctx.code_gen_ptr;
Signature · 1 line
2.7.3
#39 lore raw

Re: [Qemu-devel] [PATCH 5/5] tcg: move tb_invalidated_flag to CPUState

Quoted 18 lines · [email protected] writes:

From: Paolo Bonzini <[email protected]>

This is a baby step towards making tb_flush thread safe.

---
 cpu-exec.c              | 11 +++++------
 include/exec/exec-all.h |  2 --
 include/qom/cpu.h       |  2 ++
 translate-all.c         |  3 +--
 4 files changed, 8 insertions(+), 10 deletions(-)
cpu-exec.c +0 -0
index f90482eff778..07545aa91082 100644

cpu-exec.c static void cpu_exec_nocache(CPUState *cpu, int max_cycles, · line 198

Quoted 4 lines
cpu-exec.c +1 -0
@@ -195,10 +195,11 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,
     if (max_cycles > CF_COUNT_MASK)
         max_cycles = CF_COUNT_MASK;
 
+    cpu->tb_invalidated_flag = 0;

We've declared as bool so lets use true/false instead of 1/0's

Quoted 66 lines
cpu-exec.c +4 -6
     tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
                      max_cycles | CF_NOCACHE
                          | (ignore_icount ? CF_IGNORE_ICOUNT : 0));
-    tb->orig_tb = tcg_ctx.tb_ctx.tb_invalidated_flag ? NULL : orig_tb;
+    tb->orig_tb = cpu->tb_invalidated_flag ? NULL : orig_tb;
     cpu->current_tb = tb;
     /* execute the generated code */
     trace_exec_tb_nocache(tb, tb->pc);
@@ -219,8 +220,6 @@ static TranslationBlock *tb_find_physical(CPUState *cpu,
     tb_page_addr_t phys_pc, phys_page1;
     target_ulong virt_page2;
 
-    tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
-
     /* find translated block using physical mappings */
     phys_pc = get_page_addr_code(env, pc);
     phys_page1 = phys_pc & TARGET_PAGE_MASK;
@@ -288,6 +287,7 @@ static TranslationBlock *tb_find_slow(CPUState *cpu,
 #endif
 
     /* if no translated code available, then translate it now */
+    cpu->tb_invalidated_flag = 0;
     tb = tb_gen_code(cpu, pc, cs_base, flags, 0);
 
 #ifdef CONFIG_USER_ONLY
@@ -493,12 +493,11 @@ int cpu_exec(CPUState *cpu)
                 tb = tb_find_fast(cpu);
                 /* Note: we do it here to avoid a gcc bug on Mac OS X when
                    doing it in tb_find_slow */
-                if (tcg_ctx.tb_ctx.tb_invalidated_flag) {
+                if (cpu->tb_invalidated_flag) {
                     /* as some TB could have been invalidated because
-                       of memory exceptions while generating the code, we
+                       of a tb_flush while generating the code, we
                        must recompute the hash index here */
                     next_tb = 0;
-                    tcg_ctx.tb_ctx.tb_invalidated_flag = 0;
                 }
                 if (qemu_loglevel_mask(CPU_LOG_EXEC)) {
                     qemu_log("Trace %p [" TARGET_FMT_lx "] %s\n",
include/exec/exec-all.h +0 -2
index 05a151da4a54..0ef6ea5cf6dc 100644
@@ -288,8 +288,6 @@ struct TBContext {
     /* statistics */
     int tb_flush_count;
     int tb_phys_invalidate_count;
-
-    int tb_invalidated_flag;
 };
 
 void tb_free(TranslationBlock *tb);
include/qom/cpu.h +1 -0
index 7052eee7b78a..9538f9cc2af3 100644
@@ -240,6 +240,7 @@ struct kvm_run;
  * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
  * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
  *           CPU and return to its top level loop.
+ * @tb_invalidated_flag: Set to tell TCG that tb_flush has been called.
  * @singlestep_enabled: Flags for single-stepping.
  * @icount_extra: Instructions until next timer event.
  * @icount_decr: Number of cycles left, with interrupt flag in high bit.

include/qom/cpu.h struct CPUState { · line 295

Quoted 4 lines
include/qom/cpu.h +1 -0
@@ -291,6 +292,7 @@ struct CPUState {
     bool stopped;
     bool crash_occurred;
     bool exit_request;
+    bool tb_invalidated_flag;

s/_flag// would save a few characters given it should be obvious it is a flag from the setting of true and false?

Quoted 16 lines
include/qom/cpu.h +0 -0
     uint32_t interrupt_request;
     int singlestep_enabled;
     int64_t icount_extra;
translate-all.c +1 -0
index 1db5a914d9a3..8e1edd6bb633 100644
@@ -843,6 +843,7 @@ void tb_flush(CPUState *cpu)
     tcg_ctx.tb_ctx.nb_tbs = 0;
 
     CPU_FOREACH(cpu) {
+        cpu->tb_invalidated_flag = 1;
         memset(cpu->tb_jmp_cache, 0, sizeof(cpu->tb_jmp_cache));
     }
 

translate-all.c TranslationBlock *tb_gen_code(CPUState *cpu, · line 1085

Quoted 8 lines
translate-all.c +0 -2
@@ -1079,8 +1080,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
         /* cannot fail at this point */
         tb = tb_alloc(pc);
         assert(tb != NULL);
-        /* Don't forget to invalidate previous TB info.  */
-        tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
     }
 
     gen_code_buf = tcg_ctx.code_gen_ptr;

I also note there is some code motion about where these flags are set and cleared which should probably be mentioned in the commit message.

--
Alex Bennée

#40 lore raw

Re: [Qemu-devel] [PATCH 5/5] tcg: move tb_invalidated_flag to CPUState

Quoted 1 line · Alex Bennée

[email protected] writes:

(snip)

Quoted 11 lines

cpu-exec.c static void cpu_exec_nocache(CPUState *cpu, int max_cycles, · line 198

Quoted 9 lines
cpu-exec.c +1 -0
index f90482eff778..07545aa91082 100644
@@ -195,10 +195,11 @@ static void cpu_exec_nocache(CPUState *cpu, int max_cycles,
     if (max_cycles > CF_COUNT_MASK)
         max_cycles = CF_COUNT_MASK;
 
+    cpu->tb_invalidated_flag = 0;

We've declared as bool so lets use true/false instead of 1/0's

(snip)

Quoted 21 lines
Quoted 13 lines
include/qom/cpu.h +1 -0
index 7052eee7b78a..9538f9cc2af3 100644
@@ -240,6 +240,7 @@ struct kvm_run;
  * @crash_occurred: Indicates the OS reported a crash (panic) for this CPU
  * @tcg_exit_req: Set to force TCG to stop executing linked TBs for this
  *           CPU and return to its top level loop.
+ * @tb_invalidated_flag: Set to tell TCG that tb_flush has been called.
  * @singlestep_enabled: Flags for single-stepping.
  * @icount_extra: Instructions until next timer event.
  * @icount_decr: Number of cycles left, with interrupt flag in high bit.

include/qom/cpu.h struct CPUState { · line 295

Quoted 4 lines
include/qom/cpu.h +1 -0
@@ -291,6 +292,7 @@ struct CPUState {
     bool stopped;
     bool crash_occurred;
     bool exit_request;
+    bool tb_invalidated_flag;

s/_flag// would save a few characters given it should be obvious it is a flag from the setting of true and false?

Quoted 1 line

(snip)

Quoted 20 lines
Quoted 20 lines
translate-all.c +1 -2
index 1db5a914d9a3..8e1edd6bb633 100644
@@ -843,6 +843,7 @@ void tb_flush(CPUState *cpu)
     tcg_ctx.tb_ctx.nb_tbs = 0;
 
     CPU_FOREACH(cpu) {
+        cpu->tb_invalidated_flag = 1;
         memset(cpu->tb_jmp_cache, 0, sizeof(cpu->tb_jmp_cache));
     }
 
@@ -1079,8 +1080,6 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
         /* cannot fail at this point */
         tb = tb_alloc(pc);
         assert(tb != NULL);
-        /* Don't forget to invalidate previous TB info.  */
-        tcg_ctx.tb_ctx.tb_invalidated_flag = 1;
     }
 
Quoted 4 lines

translate-all.c TranslationBlock *tb_gen_code(CPUState *cpu, · line 1085

Quoted 1 line
translate-all.c +0 -0
     gen_code_buf = tcg_ctx.code_gen_ptr;

I also note there is some code motion about where these flags are set and cleared which should probably be mentioned in the commit message.

Agree with all the comments.

Kind regards,
Sergey