From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: fam@euphon.net, berrange@redhat.com, robert.foley@linaro.org,
"Paolo Bonzini" <pbonzini@redhat.com>,
"Alex Bennée" <alex.bennee@linaro.org>,
richard.henderson@linaro.org, f4bug@amsat.org,
robhenry@microsoft.com, aaron@os.amperecomputing.com,
cota@braap.org, kuhn.chenqun@huawei.com, peter.puhov@linaro.org,
"Eduardo Habkost" <ehabkost@redhat.com>,
aurelien@aurel32.net, "Richard Henderson" <rth@twiddle.net>
Subject: [PATCH v2 06/11] cputlb: ensure we save the IOTLB data in case of reset
Date: Mon, 13 Jul 2020 21:04:10 +0100 [thread overview]
Message-ID: <20200713200415.26214-7-alex.bennee@linaro.org> (raw)
In-Reply-To: <20200713200415.26214-1-alex.bennee@linaro.org>
Any write to a device might cause a re-arrangement of memory
triggering a TLB flush and potential re-size of the TLB invalidating
previous entries. This would cause users of qemu_plugin_get_hwaddr()
to see the warning:
invalid use of qemu_plugin_get_hwaddr
because of the failed tlb_lookup which should always succeed. To
prevent this we save the IOTLB data in case it is later needed by a
plugin doing a lookup.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
v2
- save the entry instead of re-running the tlb_fill.
v3
- don't abuse TLS, use CPUState to store data
- just use g_free_rcu() to avoid ugliness
- verify addr matches before returning data
- ws fix
v4
- don't both with RCU, just store it in CPUState
- clean-up #ifdef'ery
- checkpatch
---
include/hw/core/cpu.h | 16 ++++++++++++++++
include/qemu/typedefs.h | 1 +
accel/tcg/cputlb.c | 38 +++++++++++++++++++++++++++++++++++---
3 files changed, 52 insertions(+), 3 deletions(-)
diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index 5542577d2b..8f145733ce 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -259,6 +259,18 @@ struct CPUWatchpoint {
QTAILQ_ENTRY(CPUWatchpoint) entry;
};
+#ifdef CONFIG_PLUGIN
+/*
+ * For plugins we sometime need to save the resolved iotlb data before
+ * the memory regions get moved around by io_writex.
+ */
+typedef struct SavedIOTLB {
+ hwaddr addr;
+ MemoryRegionSection *section;
+ hwaddr mr_offset;
+} SavedIOTLB;
+#endif
+
struct KVMState;
struct kvm_run;
@@ -417,7 +429,11 @@ struct CPUState {
DECLARE_BITMAP(plugin_mask, QEMU_PLUGIN_EV_MAX);
+#ifdef CONFIG_PLUGIN
GArray *plugin_mem_cbs;
+ /* saved iotlb data from io_writex */
+ SavedIOTLB saved_iotlb;
+#endif
/* TODO Move common fields from CPUArchState here. */
int cpu_index;
diff --git a/include/qemu/typedefs.h b/include/qemu/typedefs.h
index 15f5047bf1..427027a970 100644
--- a/include/qemu/typedefs.h
+++ b/include/qemu/typedefs.h
@@ -116,6 +116,7 @@ typedef struct QObject QObject;
typedef struct QString QString;
typedef struct RAMBlock RAMBlock;
typedef struct Range Range;
+typedef struct SavedIOTLB SavedIOTLB;
typedef struct SHPCDevice SHPCDevice;
typedef struct SSIBus SSIBus;
typedef struct VirtIODevice VirtIODevice;
diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c
index 1e815357c7..d370aedb47 100644
--- a/accel/tcg/cputlb.c
+++ b/accel/tcg/cputlb.c
@@ -1073,6 +1073,24 @@ static uint64_t io_readx(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
return val;
}
+/*
+ * Save a potentially trashed IOTLB entry for later lookup by plugin.
+ *
+ * We also need to track the thread storage address because the RCU
+ * cleanup that runs when we leave the critical region (the current
+ * execution) is actually in a different thread.
+ */
+static void save_iotlb_data(CPUState *cs, hwaddr addr,
+ MemoryRegionSection *section, hwaddr mr_offset)
+{
+#ifdef CONFIG_PLUGIN
+ SavedIOTLB *saved = &cs->saved_iotlb;
+ saved->addr = addr;
+ saved->section = section;
+ saved->mr_offset = mr_offset;
+#endif
+}
+
static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
int mmu_idx, uint64_t val, target_ulong addr,
uintptr_t retaddr, MemOp op)
@@ -1092,6 +1110,12 @@ static void io_writex(CPUArchState *env, CPUIOTLBEntry *iotlbentry,
}
cpu->mem_io_pc = retaddr;
+ /*
+ * The memory_region_dispatch may trigger a flush/resize
+ * so for plugins we save the iotlb_data just in case.
+ */
+ save_iotlb_data(cpu, iotlbentry->addr, section, mr_offset);
+
if (mr->global_locking && !qemu_mutex_iothread_locked()) {
qemu_mutex_lock_iothread();
locked = true;
@@ -1381,8 +1405,11 @@ void *tlb_vaddr_to_host(CPUArchState *env, abi_ptr addr,
* in the softmmu lookup code (or helper). We don't handle re-fills or
* checking the victim table. This is purely informational.
*
- * This should never fail as the memory access being instrumented
- * should have just filled the TLB.
+ * This almost never fails as the memory access being instrumented
+ * should have just filled the TLB. The one corner case is io_writex
+ * which can cause TLB flushes and potential resizing of the TLBs
+ * loosing the information we need. In those cases we need to recover
+ * data from a copy of the io_tlb entry.
*/
bool tlb_plugin_lookup(CPUState *cpu, target_ulong addr, int mmu_idx,
@@ -1406,8 +1433,13 @@ bool tlb_plugin_lookup(CPUState *cpu, target_ulong addr, int mmu_idx,
data->v.ram.hostaddr = addr + tlbe->addend;
}
return true;
+ } else {
+ SavedIOTLB *saved = &cpu->saved_iotlb;
+ data->is_io = true;
+ data->v.io.section = saved->section;
+ data->v.io.offset = saved->mr_offset;
+ return true;
}
- return false;
}
#endif
--
2.20.1
next prev parent reply other threads:[~2020-07-13 20:06 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-13 20:04 [PATCH v2 00/11] misc fixes for rc0 (docker, plugins, softfloat) Alex Bennée
2020-07-13 20:04 ` [PATCH v2 01/11] configure: remove all dependencies on a (re)configure Alex Bennée
2020-07-13 20:15 ` Philippe Mathieu-Daudé
2020-07-13 20:04 ` [PATCH v2 02/11] tests/docker: Remove the libssh workaround from the ubuntu 20.04 image Alex Bennée
2020-07-13 20:04 ` [PATCH v2 03/11] docker.py: fix fetching of FROM layers Alex Bennée
2020-07-13 20:04 ` [PATCH v2 04/11] fpu/softfloat: fix up float16 nan recognition Alex Bennée
2020-07-13 20:04 ` [PATCH v2 05/11] tests/plugins: don't unconditionally add -Wpsabi Alex Bennée
2020-07-14 5:31 ` Thomas Huth
2020-07-13 20:04 ` Alex Bennée [this message]
2020-07-13 21:58 ` [PATCH v2 06/11] cputlb: ensure we save the IOTLB data in case of reset Richard Henderson
2020-07-18 20:51 ` Emilio G. Cota
2020-07-13 20:04 ` [PATCH v2 07/11] plugins: expand the bb plugin to be thread safe and track per-cpu Alex Bennée
2020-07-13 20:04 ` [PATCH v2 08/11] docs/devel: fix grammar in multi-thread-tcg Alex Bennée
2020-07-13 22:01 ` Richard Henderson
2020-07-14 5:41 ` Thomas Huth
2020-07-14 10:20 ` Philippe Mathieu-Daudé
2020-07-13 20:04 ` [PATCH v2 09/11] hw/virtio/pci: include vdev name in registered PCI sections Alex Bennée
2020-07-14 9:07 ` Michael S. Tsirkin
2020-07-14 9:49 ` Philippe Mathieu-Daudé
2020-07-13 20:04 ` [PATCH v2 10/11] plugins: add API to return a name for a IO device Alex Bennée
2020-07-13 22:04 ` Richard Henderson
2020-07-13 20:04 ` [PATCH v2 11/11] plugins: new hwprofile plugin 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=20200713200415.26214-7-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=aaron@os.amperecomputing.com \
--cc=aurelien@aurel32.net \
--cc=berrange@redhat.com \
--cc=cota@braap.org \
--cc=ehabkost@redhat.com \
--cc=f4bug@amsat.org \
--cc=fam@euphon.net \
--cc=kuhn.chenqun@huawei.com \
--cc=pbonzini@redhat.com \
--cc=peter.puhov@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=robert.foley@linaro.org \
--cc=robhenry@microsoft.com \
--cc=rth@twiddle.net \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).