[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v4 28/30] contrib/plugins: protect execlog's last_exec expansion
From: |
Alex Bennée |
Subject: |
[PATCH v4 28/30] contrib/plugins: protect execlog's last_exec expansion |
Date: |
Thu, 27 Oct 2022 19:36:34 +0100 |
We originally naively treated expansion as safe because we expected
each new CPU/thread to appear in order. However the -M raspi2 model
triggered a case where a new high cpu_index thread started executing
just before a smaller one.
Clean this up by converting the GArray into the simpler GPtrArray and
then holding a lock for the expansion.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
Cc: Alexandre Iooss <erdnaxe@crans.org>
---
contrib/plugins/execlog.c | 38 ++++++++++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 8 deletions(-)
diff --git a/contrib/plugins/execlog.c b/contrib/plugins/execlog.c
index 1b3bb7ebba..e255bd21fd 100644
--- a/contrib/plugins/execlog.c
+++ b/contrib/plugins/execlog.c
@@ -18,11 +18,30 @@
QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
/* Store last executed instruction on each vCPU as a GString */
-GArray *last_exec;
+static GPtrArray *last_exec;
+static GMutex expand_array_lock;
static GPtrArray *imatches;
static GArray *amatches;
+/*
+ * Expand last_exec array.
+ *
+ * As we could have multiple threads trying to do this we need to
+ * serialise the expansion under a lock. Threads accessing already
+ * created entries can continue without issue even if the ptr array
+ * gets reallocated during resize.
+ */
+static void expand_last_exec(int cpu_index)
+{
+ g_mutex_lock(&expand_array_lock);
+ while (cpu_index >= last_exec->len) {
+ GString *s = g_string_new(NULL);
+ g_ptr_array_add(last_exec, s);
+ }
+ g_mutex_unlock(&expand_array_lock);
+}
+
/**
* Add memory read or write information to current instruction log
*/
@@ -33,7 +52,7 @@ static void vcpu_mem(unsigned int cpu_index,
qemu_plugin_meminfo_t info,
/* Find vCPU in array */
g_assert(cpu_index < last_exec->len);
- s = g_array_index(last_exec, GString *, cpu_index);
+ s = g_ptr_array_index(last_exec, cpu_index);
/* Indicate type of memory access */
if (qemu_plugin_mem_is_store(info)) {
@@ -61,11 +80,10 @@ static void vcpu_insn_exec(unsigned int cpu_index, void
*udata)
GString *s;
/* Find or create vCPU in array */
- while (cpu_index >= last_exec->len) {
- s = g_string_new(NULL);
- g_array_append_val(last_exec, s);
+ if (cpu_index >= last_exec->len) {
+ expand_last_exec(cpu_index);
}
- s = g_array_index(last_exec, GString *, cpu_index);
+ s = g_ptr_array_index(last_exec, cpu_index);
/* Print previous instruction in cache */
if (s->len) {
@@ -163,7 +181,7 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
guint i;
GString *s;
for (i = 0; i < last_exec->len; i++) {
- s = g_array_index(last_exec, GString *, i);
+ s = g_ptr_array_index(last_exec, i);
if (s->str) {
qemu_plugin_outs(s->str);
qemu_plugin_outs("\n");
@@ -201,7 +219,11 @@ QEMU_PLUGIN_EXPORT int
qemu_plugin_install(qemu_plugin_id_t id,
* Initialize dynamic array to cache vCPU instruction. In user mode
* we don't know the size before emulation.
*/
- last_exec = g_array_new(FALSE, FALSE, sizeof(GString *));
+ if (info->system_emulation) {
+ last_exec = g_ptr_array_sized_new(info->system.max_vcpus);
+ } else {
+ last_exec = g_ptr_array_new();
+ }
for (int i = 0; i < argc; i++) {
char *opt = argv[i];
--
2.34.1
- [PATCH v4 06/30] tests/avocado: extend the timeout for x86_64 tcg tests, (continued)
- [PATCH v4 06/30] tests/avocado: extend the timeout for x86_64 tcg tests, Alex Bennée, 2022/10/27
- [PATCH v4 13/30] block/vvfat: Unify the mkdir() call, Alex Bennée, 2022/10/27
- [PATCH v4 22/30] tests/tcg: re-enable threadcount for sh4, Alex Bennée, 2022/10/27
- [PATCH v4 26/30] tests/tcg: include CONFIG_PLUGIN in config-host.mak, Alex Bennée, 2022/10/27
- [PATCH v4 16/30] MAINTAINERS: add features_to_c.sh to gdbstub files, Alex Bennée, 2022/10/27
- [PATCH v4 23/30] target/s390x: don't use ld_code2 to probe next pc, Alex Bennée, 2022/10/27
- [PATCH v4 20/30] tests/avocado: disable sh4 rd2 tests on Gitlab, Alex Bennée, 2022/10/27
- [PATCH v4 15/30] MAINTAINERS: add entries for the key build bits, Alex Bennée, 2022/10/27
- [PATCH v4 18/30] tests/avocado: set -machine none for userfwd and vnc tests, Alex Bennée, 2022/10/27
- [PATCH v4 17/30] MAINTAINERS: fix-up for check-tcg Makefile changes, Alex Bennée, 2022/10/27
- [PATCH v4 28/30] contrib/plugins: protect execlog's last_exec expansion,
Alex Bennée <=
- [PATCH v4 25/30] target/s390x: fake instruction loading when handling 'ex', Alex Bennée, 2022/10/27
- [PATCH v4 27/30] contrib/plugins: enable debug on CONFIG_DEBUG_TCG, Alex Bennée, 2022/10/27
- [PATCH v4 29/30] tests/unit: cleanups for test-io-channel-command, Alex Bennée, 2022/10/27
- [PATCH v4 24/30] target/s390x: don't probe next pc for EXecuted insns, Alex Bennée, 2022/10/27
- [PATCH v4 30/30] tests/vm: use -o IdentitiesOnly=yes for ssh, Alex Bennée, 2022/10/27
- [PATCH v4 19/30] tests/avocado: raspi2_initrd: Wait for guest shutdown message before stopping, Alex Bennée, 2022/10/27