All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: mttcg@listserver.greensocs.com, qemu-devel@nongnu.org,
	fred.konrad@greensocs.com, a.rigo@virtualopensystems.com,
	cota@braap.org, bobby.prani@gmail.com, nikunj@linux.vnet.ibm.com
Cc: mark.burton@greensocs.com, pbonzini@redhat.com,
	jan.kiszka@siemens.com, serge.fdrv@gmail.com, rth@twiddle.net,
	peter.maydell@linaro.org, claudio.fontana@huawei.com,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Peter Crosthwaite" <crosthwaite.peter@gmail.com>
Subject: [Qemu-devel] [RFC v4 25/28] cputlb: introduce tlb_flush_* async work.
Date: Thu, 11 Aug 2016 16:24:21 +0100	[thread overview]
Message-ID: <1470929064-4092-26-git-send-email-alex.bennee@linaro.org> (raw)
In-Reply-To: <1470929064-4092-1-git-send-email-alex.bennee@linaro.org>

From: KONRAD Frederic <fred.konrad@greensocs.com>

Some architectures allow to flush the tlb of other VCPUs. This is not a problem
when we have only one thread for all VCPUs but it definitely needs to be an
asynchronous work when we are in true multithreaded work.

This patch doesn't do anything to protect other cputlb function being
called in MTTCG mode making cross vCPU changes.

Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
[AJB: remove need for g_malloc on defer, make check fixes]
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>

---
v4 (base_patches)
  - brought forward from arm enabling series
  - restore pending_tlb_flush flag
v1
  - Remove tlb_flush_all just do the check in tlb_flush.
  - remove the need to g_malloc
  - tlb_flush calls direct if !cpu->created
---
 cputlb.c                | 64 +++++++++++++++++++++++++++++++++++++++----------
 include/exec/exec-all.h |  1 +
 include/qom/cpu.h       |  6 +++++
 3 files changed, 58 insertions(+), 13 deletions(-)

diff --git a/cputlb.c b/cputlb.c
index b6833fe..2975bbe 100644
--- a/cputlb.c
+++ b/cputlb.c
@@ -65,22 +65,14 @@
         }                                                         \
     } while (0)
 
+/* We need a solution for stuffing 64 bit pointers in 32 bit ones if
+ * we care about this combination */
+QEMU_BUILD_BUG_ON(sizeof(target_ulong) > sizeof(void *));
+
 /* statistics */
 int tlb_flush_count;
 
-/* NOTE:
- * If flush_global is true (the usual case), flush all tlb entries.
- * If flush_global is false, flush (at least) all tlb entries not
- * marked global.
- *
- * Since QEMU doesn't currently implement a global/not-global flag
- * for tlb entries, at the moment tlb_flush() will also flush all
- * tlb entries in the flush_global == false case. This is OK because
- * CPU architectures generally permit an implementation to drop
- * entries from the TLB at any time, so flushing more entries than
- * required is only an efficiency issue, not a correctness issue.
- */
-void tlb_flush(CPUState *cpu, int flush_global)
+static void tlb_flush_nocheck(CPUState *cpu, int flush_global)
 {
     CPUArchState *env = cpu->env_ptr;
 
@@ -95,6 +87,37 @@ void tlb_flush(CPUState *cpu, int flush_global)
     env->tlb_flush_addr = -1;
     env->tlb_flush_mask = 0;
     tlb_flush_count++;
+
+    atomic_mb_set(&cpu->pending_tlb_flush, false);
+}
+
+static void tlb_flush_global_async_work(CPUState *cpu, void *opaque)
+{
+    tlb_flush_nocheck(cpu, GPOINTER_TO_INT(opaque));
+}
+
+/* NOTE:
+ * If flush_global is true (the usual case), flush all tlb entries.
+ * If flush_global is false, flush (at least) all tlb entries not
+ * marked global.
+ *
+ * Since QEMU doesn't currently implement a global/not-global flag
+ * for tlb entries, at the moment tlb_flush() will also flush all
+ * tlb entries in the flush_global == false case. This is OK because
+ * CPU architectures generally permit an implementation to drop
+ * entries from the TLB at any time, so flushing more entries than
+ * required is only an efficiency issue, not a correctness issue.
+ */
+void tlb_flush(CPUState *cpu, int flush_global)
+{
+    if (cpu->created && !qemu_cpu_is_self(cpu)) {
+        if (atomic_bool_cmpxchg(&cpu->pending_tlb_flush, false, true)) {
+            async_run_on_cpu(cpu, tlb_flush_global_async_work,
+                             GINT_TO_POINTER(flush_global));
+        }
+    } else {
+        tlb_flush_nocheck(cpu, flush_global);
+    }
 }
 
 static inline void v_tlb_flush_by_mmuidx(CPUState *cpu, va_list argp)
@@ -222,6 +245,21 @@ void tlb_flush_page_by_mmuidx(CPUState *cpu, target_ulong addr, ...)
     tb_flush_jmp_cache(cpu, addr);
 }
 
+static void tlb_flush_page_async_work(CPUState *cpu, void *opaque)
+{
+    tlb_flush_page(cpu, GPOINTER_TO_UINT(opaque));
+}
+
+void tlb_flush_page_all(target_ulong addr)
+{
+    CPUState *cpu;
+
+    CPU_FOREACH(cpu) {
+        async_run_on_cpu(cpu, tlb_flush_page_async_work,
+                         GUINT_TO_POINTER(addr));
+    }
+}
+
 /* update the TLBs so that writes to code in the virtual page 'addr'
    can be detected */
 void tlb_protect_code(ram_addr_t ram_addr)
diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h
index 27d1a24..14a7b5e 100644
--- a/include/exec/exec-all.h
+++ b/include/exec/exec-all.h
@@ -159,6 +159,7 @@ void tlb_set_page(CPUState *cpu, target_ulong vaddr,
 void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr);
 void probe_write(CPUArchState *env, target_ulong addr, int mmu_idx,
                  uintptr_t retaddr);
+void tlb_flush_page_all(target_ulong addr);
 #else
 static inline void tlb_flush_page(CPUState *cpu, target_ulong addr)
 {
diff --git a/include/qom/cpu.h b/include/qom/cpu.h
index d0db846..6b35f37 100644
--- a/include/qom/cpu.h
+++ b/include/qom/cpu.h
@@ -376,6 +376,12 @@ struct CPUState {
        (absolute value) offset as small as possible.  This reduces code
        size, especially for hosts without large memory offsets.  */
     uint32_t tcg_exit_req;
+
+    /* The pending_tlb_flush flag is set and cleared atomically to
+     * avoid potential races. The aim of the flag is to avoid
+     * unnecessary flushes.
+     */
+    bool pending_tlb_flush;
 };
 
 QTAILQ_HEAD(CPUTailQ, CPUState);
-- 
2.7.4

  parent reply	other threads:[~2016-08-11 15:24 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-11 15:23 [Qemu-devel] [RFC v4 00/28] Base enabling patches for MTTCG Alex Bennée
2016-08-11 15:23 ` [Qemu-devel] [RFC v4 01/28] cpus: make all_vcpus_paused() return bool Alex Bennée
2016-08-11 15:23 ` [Qemu-devel] [RFC v4 02/28] translate_all: DEBUG_FLUSH -> DEBUG_TB_FLUSH Alex Bennée
2016-08-11 15:23 ` [Qemu-devel] [RFC v4 03/28] translate-all: add DEBUG_LOCKING asserts Alex Bennée
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 04/28] cpu-exec: include cpu_index in CPU_LOG_EXEC messages Alex Bennée
2016-09-07  2:21   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 05/28] docs: new design document multi-thread-tcg.txt (DRAFTING) Alex Bennée
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 06/28] tcg: comment on which functions have to be called with tb_lock held Alex Bennée
2016-09-07  2:30   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 07/28] linux-user/elfload: ensure mmap_lock() held while setting up Alex Bennée
2016-09-07  2:34   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 08/28] translate-all: Add assert_(memory|tb)_lock annotations Alex Bennée
2016-09-07  2:41   ` Richard Henderson
2016-09-07  7:08     ` Alex Bennée
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 09/28] tcg: protect TBContext with tb_lock Alex Bennée
2016-09-07  2:48   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 10/28] target-arm/arm-powerctl: wake up sleeping CPUs Alex Bennée
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 11/28] tcg: move tcg_exec_all and helpers above thread fn Alex Bennée
2016-09-07  2:53   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 12/28] tcg: cpus rm tcg_exec_all() Alex Bennée
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 13/28] tcg: add options for enabling MTTCG Alex Bennée
2016-09-07  3:06   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 14/28] tcg: add kick timer for single-threaded vCPU emulation Alex Bennée
2016-09-07  3:25   ` Richard Henderson
2016-09-07  5:40     ` Paolo Bonzini
2016-09-07 10:15       ` Alex Bennée
2016-09-07 10:19     ` Alex Bennée
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 15/28] tcg: rename tcg_current_cpu to tcg_current_rr_cpu Alex Bennée
2016-09-07  3:34   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 16/28] tcg: drop global lock during TCG code execution Alex Bennée
2016-09-07  4:03   ` Richard Henderson
2016-09-07  5:43     ` Paolo Bonzini
2016-09-07  6:43       ` Richard Henderson
2016-09-07 15:15         ` Paolo Bonzini
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 17/28] cpus: re-factor out handle_icount_deadline Alex Bennée
2016-09-07  4:06   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 18/28] tcg: remove global exit_request Alex Bennée
2016-09-07  4:11   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 19/28] tcg: move locking for tb_invalidate_phys_page_range up Alex Bennée
2016-09-27 15:56   ` Paolo Bonzini
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 20/28] cpus: tweak sleeping and safe_work rules for MTTCG Alex Bennée
2016-09-07  4:22   ` Richard Henderson
2016-09-07 10:05   ` Paolo Bonzini
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 21/28] tcg: enable tb_lock() for SoftMMU Alex Bennée
2016-09-07  4:26   ` Richard Henderson
2016-09-27 16:16   ` Paolo Bonzini
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 22/28] tcg: enable thread-per-vCPU Alex Bennée
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 23/28] atomic: introduce cmpxchg_bool Alex Bennée
2016-09-08  0:12   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 24/28] cputlb: add assert_cpu_is_self checks Alex Bennée
2016-09-08 17:19   ` Richard Henderson
2016-08-11 15:24 ` Alex Bennée [this message]
2016-09-07 10:08   ` [Qemu-devel] [RFC v4 25/28] cputlb: introduce tlb_flush_* async work Paolo Bonzini
2016-09-08 17:23   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 26/28] cputlb: tweak qemu_ram_addr_from_host_nofail reporting Alex Bennée
2016-09-08 17:24   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 27/28] cputlb: make tlb_reset_dirty safe for MTTCG Alex Bennée
2016-09-08 17:34   ` Richard Henderson
2016-08-11 15:24 ` [Qemu-devel] [RFC v4 28/28] cputlb: make tlb_flush_by_mmuidx " Alex Bennée
2016-09-07 10:09   ` Paolo Bonzini
2016-09-08 17:54   ` Richard Henderson
2016-08-11 17:22 ` [Qemu-devel] [RFC v4 00/28] Base enabling patches " Alex Bennée
2016-08-12  8:02   ` Alex Bennée
2016-09-06  9:24 ` Alex Bennée

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=1470929064-4092-26-git-send-email-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=a.rigo@virtualopensystems.com \
    --cc=bobby.prani@gmail.com \
    --cc=claudio.fontana@huawei.com \
    --cc=cota@braap.org \
    --cc=crosthwaite.peter@gmail.com \
    --cc=fred.konrad@greensocs.com \
    --cc=jan.kiszka@siemens.com \
    --cc=mark.burton@greensocs.com \
    --cc=mttcg@listserver.greensocs.com \
    --cc=nikunj@linux.vnet.ibm.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    --cc=serge.fdrv@gmail.com \
    /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.