Skip to contentSkip to conversation

Maintainer updates for May (testing, plugins, virtio-gpu)

PATCH Superseded Alex Bennée 7 participants 30 messages Apr 28, 2025 – May 8, 2025

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Maintainer updates for May (testing, plugins, virtio-gpu)

Now the tree is open here is the state of my current maintainer queues. I've tagged the virtio-gpu changes as Cc: qemu-stable but given how close to release we were it didn't seem worth rushing them in for 10.0. I've included a MAINTAINERS patch just to make sure I can route any more fixes until a more full time maintainer volunteers.

The following need review:

MAINTAINERS: add myself to virtio-gpu for Odd Fixes contrib/plugins: allow setting of instructions per quantum contrib/plugins: add a scaling factor to the ips arg tests/tcg: make aarch64 boot.S handle different starting modes
gitlab: disable debug info on CI builds tests/docker: expose $HOME/.cache as docker volume

Alex.

Alex Bennée (7):
tests/docker: expose $HOME/.cache as docker volume
gitlab: disable debug info on CI builds
tests/tcg: make aarch64 boot.S handle different starting modes contrib/plugins: add a scaling factor to the ips arg contrib/plugins: allow setting of instructions per quantum
MAINTAINERS: add myself to virtio-gpu for Odd Fixes hw/display: re-arrange memory region tracking

Manos Pitsidianakis (2):
virtio-gpu: fix hang under TCG when unmapping blob
virtio-gpu: refactor async blob unmapping

 MAINTAINERS                               |   3 +-
 docs/about/emulation.rst                  |   4 +
 include/system/memory.h                   |   1 +
 contrib/plugins/ips.c                     |  33 +++++-
 hw/display/virtio-gpu-virgl.c             |  60 +++++-----
 .gitlab-ci.d/buildtest-template.yml       |   1 +
 tests/docker/Makefile.include             |   1 +
 tests/tcg/aarch64/Makefile.softmmu-target |   3 +-
 tests/tcg/aarch64/system/boot.S           | 127 +++++++++++++++++++++-
 9 files changed, 202 insertions(+), 31 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/9 tests/docker: expose $HOME/.cache as docker volume

If you want to run functional tests we should share .cache so we don't force containers to continually re-download images.

---
 tests/docker/Makefile.include | 1 +
 1 file changed, 1 insertion(+)
tests/docker/Makefile.include +1 -0
index fa1cbb6726..56a8d9f8ff 100644
@@ -224,6 +224,7 @@ docker-run: docker-qemu-src
 			$(if $(NOUSER),,				\
 				-e CCACHE_DIR=/var/tmp/ccache 		\
 				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
+				-v $(HOME)/.cache:$(HOME)/.cache 	\
 			)						\
 			-v $$(readlink -e $(DOCKER_SRC_COPY)):/var/tmp/qemu:z$(COMMA)ro \
 			$(IMAGE) 					\
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/9] tests/docker: expose $HOME/.cache as docker volume

Quoted 13 lines · Alex Bennée

If you want to run functional tests we should share .cache so we don't force containers to continually re-download images.

---
  tests/docker/Makefile.include | 1 +
  1 file changed, 1 insertion(+)
tests/docker/Makefile.include +0 -0
index fa1cbb6726..56a8d9f8ff 100644

tests/docker/Makefile.include docker-run: docker-qemu-src · line 227

Quoted 4 lines
tests/docker/Makefile.include +1 -0
@@ -224,6 +224,7 @@ docker-run: docker-qemu-src
  			$(if $(NOUSER),,				\
  				-e CCACHE_DIR=/var/tmp/ccache 		\
  				-v $(DOCKER_CCACHE_DIR):/var/tmp/ccache:z \
+				-v $(HOME)/.cache:$(HOME)/.cache 	\

Maybe better restrict it to ~/.cache/qemu ? ... to avoid sharing other subfolders there to the container?

Thomas

#3 lore raw

PATCH 2/9 gitlab: disable debug info on CI builds

In reply to Alex Bennée

Our default build enables debug info which adds hugely to the size of the builds as well as the size of cached objects. Disable debug info across the board to save space and reduce pressure on the CI system. We still have a number of builds which explicitly enable debug and related extra asserts like --enable-debug-tcg.

---
 .gitlab-ci.d/buildtest-template.yml | 1 +
 1 file changed, 1 insertion(+)
.gitlab-ci.d/buildtest-template.yml +1 -0
index d4f145fdb5..d9e69c3237 100644
@@ -24,6 +24,7 @@
     - ccache --zero-stats
     - section_start configure "Running configure"
     - ../configure --enable-werror --disable-docs --enable-fdt=system
+          --disable-debug-info
           ${TARGETS:+--target-list="$TARGETS"}
           $CONFIGURE_ARGS ||
       { cat config.log meson-logs/meson-log.txt && exit 1; }
Signature · 1 line
2.39.5
Reviewed-by on v1 2/9
#4 lore raw

Re: [PATCH 2/9] gitlab: disable debug info on CI builds

Quoted 16 lines · Alex Bennée

Our default build enables debug info which adds hugely to the size of the builds as well as the size of cached objects. Disable debug info across the board to save space and reduce pressure on the CI system. We still have a number of builds which explicitly enable debug and related extra asserts like --enable-debug-tcg.

---
  .gitlab-ci.d/buildtest-template.yml | 1 +
  1 file changed, 1 insertion(+)
.gitlab-ci.d/buildtest-template.yml +0 -0
index d4f145fdb5..d9e69c3237 100644

.gitlab-ci.d/buildtest-template.yml line 27

Quoted 4 lines
.gitlab-ci.d/buildtest-template.yml +1 -0
@@ -24,6 +24,7 @@
      - ccache --zero-stats
      - section_start configure "Running configure"
      - ../configure --enable-werror --disable-docs --enable-fdt=system
+          --disable-debug-info

Do we have any jobs that might show stack traces in the console output ? build-oss-fuzz comes to my mind, but that uses a separate script, so we should be fine there?

So assuming there are no other jobs around that might need it:

#5 lore raw

Re: [PATCH 2/9] gitlab: disable debug info on CI builds

Quoted 13 lines · Thomas Huth
Quoted 11 lines · Alex Bennée

Our default build enables debug info which adds hugely to the size of the builds as well as the size of cached objects. Disable debug info across the board to save space and reduce pressure on the CI system. We still have a number of builds which explicitly enable debug and related extra asserts like --enable-debug-tcg.

---
  .gitlab-ci.d/buildtest-template.yml | 1 +
  1 file changed, 1 insertion(+)
Quoted 13 lines

.gitlab-ci.d/buildtest-template.yml line 27

Quoted 9 lines
.gitlab-ci.d/buildtest-template.yml +1 -0
index d4f145fdb5..d9e69c3237 100644
@@ -24,6 +24,7 @@
      - ccache --zero-stats
      - section_start configure "Running configure"
      - ../configure --enable-werror --disable-docs --enable-fdt=system
+          --disable-debug-info

Do we have any jobs that might show stack traces in the console output ? build-oss-fuzz comes to my mind, but that uses a separate script, so we should be fine there?

If you build with Rust enabled, and the Rust code panics, then you get a Rust backtrace. But I don't know if that cares about debug info to get its backtraces: quite possibly it's an entirely different mechanism.

-- PMM

#6 lore raw

Re: [PATCH 2/9] gitlab: disable debug info on CI builds

Quoted 28 lines · Peter Maydell
Quoted 13 lines · Thomas Huth
Quoted 11 lines · Alex Bennée

Our default build enables debug info which adds hugely to the size of the builds as well as the size of cached objects. Disable debug info across the board to save space and reduce pressure on the CI system. We still have a number of builds which explicitly enable debug and related extra asserts like --enable-debug-tcg.

---
  .gitlab-ci.d/buildtest-template.yml | 1 +
  1 file changed, 1 insertion(+)
Quoted 13 lines

.gitlab-ci.d/buildtest-template.yml line 27

Quoted 9 lines
.gitlab-ci.d/buildtest-template.yml +1 -0
index d4f145fdb5..d9e69c3237 100644
@@ -24,6 +24,7 @@
      - ccache --zero-stats
      - section_start configure "Running configure"
      - ../configure --enable-werror --disable-docs --enable-fdt=system
+          --disable-debug-info

Do we have any jobs that might show stack traces in the console output ? build-oss-fuzz comes to my mind, but that uses a separate script, so we should be fine there?

Quoted 4 lines

If you build with Rust enabled, and the Rust code panics, then you get a Rust backtrace. But I don't know if that cares about debug info to get its backtraces: quite possibly it's an entirely different mechanism.

It will show addresses and <unknown> instead of symbols if they are stripped.

#7 lore raw

PATCH 3/9 tests/tcg: make aarch64 boot.S handle different starting modes

In reply to Alex Bennée

Currently the boot.S code assumes everything starts at EL1. This will break things like the memory test which will barf on unaligned memory access when run at a higher level.

Adapt the boot code to do some basic verification of the starting mode and the minimal configuration to move to the lower exception levels. With this we can run the memory test with:

-M virt,secure=on
-M virt,secure=on,virtualization=on
-M virt,virtualisation=on

If a test needs to be at a particular EL it can use the semihosting command line to indicate the level we should execute in.

Cc Julian Armistead <[email protected]>
Cc Jim MacArthur <[email protected]>

---
v2
- allow tests to control the final EL we end up at

---
 tests/tcg/aarch64/Makefile.softmmu-target |   3 +-
 tests/tcg/aarch64/system/boot.S           | 127 +++++++++++++++++++++-
 2 files changed, 128 insertions(+), 2 deletions(-)
tests/tcg/aarch64/Makefile.softmmu-target +2 -1
index 9c52475b7a..f7a7d2b800 100644
@@ -68,7 +68,8 @@ run-plugin-semiconsole-with-%: semiconsole
 
 # vtimer test needs EL2
 QEMU_EL2_MACHINE=-machine virt,virtualization=on,gic-version=2 -cpu cortex-a57 -smp 4
-run-vtimer: QEMU_OPTS=$(QEMU_EL2_MACHINE) $(QEMU_BASE_ARGS) -kernel
+QEMU_EL2_BASE_ARGS=-semihosting-config enable=on,target=native,chardev=output,arg="2"
+run-vtimer: QEMU_OPTS=$(QEMU_EL2_MACHINE) $(QEMU_EL2_BASE_ARGS) -kernel
 
 # Simple Record/Replay Test
 .PHONY: memory-record
tests/tcg/aarch64/system/boot.S +126 -1

155 lines not on this page · raw message

Signature · 1 line
2.39.5
Reviewed-by on v1 3/9
#8 lore raw

Re: [PATCH 3/9] tests/tcg: make aarch64 boot.S handle different starting modes

Quoted 39 lines · Alex Bennée

Currently the boot.S code assumes everything starts at EL1. This will break things like the memory test which will barf on unaligned memory access when run at a higher level.

Adapt the boot code to do some basic verification of the starting mode and the minimal configuration to move to the lower exception levels. With this we can run the memory test with:

-M virt,secure=on
-M virt,secure=on,virtualization=on
-M virt,virtualisation=on

If a test needs to be at a particular EL it can use the semihosting command line to indicate the level we should execute in.

Cc Julian Armistead <[email protected]>
Cc Jim MacArthur <[email protected]>

---
v2
- allow tests to control the final EL we end up at
---
tests/tcg/aarch64/Makefile.softmmu-target | 3 +-
tests/tcg/aarch64/system/boot.S | 127 +++++++++++++++++++++-

2 files changed, 128 insertions(+), 2 deletions(-)
tests/tcg/aarch64/Makefile.softmmu-target +0 -0
index 9c52475b7a..f7a7d2b800 100644
@@ -68,7 +68,8 @@ run-plugin-semiconsole-with-%: semiconsole
 
 # vtimer test needs EL2

QEMU_EL2_MACHINE=-machine virt,virtualization=on,gic-version=2 -cpu cortex-a57 -smp 4

tests/tcg/aarch64/Makefile.softmmu-target +2 -1
-run-vtimer: QEMU_OPTS=$(QEMU_EL2_MACHINE) $(QEMU_BASE_ARGS) -kernel
+QEMU_EL2_BASE_ARGS=-semihosting-config enable=on,target=native,chardev=output,arg="2"
+run-vtimer: QEMU_OPTS=$(QEMU_EL2_MACHINE) $(QEMU_EL2_BASE_ARGS) -kernel
Quoted 2 lines

# Simple Record/Replay Test
.PHONY: memory-record

The insertions in boot.S have mixed spaces/tabs

Quoted 26 lines
tests/tcg/aarch64/system/boot.S +1 -0
index a5df9c173d..19b18bdeec 100644
@@ -16,6 +16,7 @@
 #define semihosting_call hlt 0xf000
 #define SYS_WRITEC	0x03	/* character to debug channel */
 #define SYS_WRITE0	0x04	/* string to debug channel */
+#define SYS_GET_CMDLINE 0x15	/* get command line */
 #define SYS_EXIT	0x18
 
 	.align	12
@@ -81,10 +82,129 @@ lower_a32_serror:

.error:
.string "Terminated by exception.\n"

tests/tcg/aarch64/system/boot.S +5 -0
+	.align 8
+.get_cmd:
+	.quad	cmdline
+	.quad	128
+

.text
.align 4
.global __start
__start:

tests/tcg/aarch64/system/boot.S +1 -0
+	/*

tests/tcg/aarch64/system/boot.S lower_a32_serror: · line 104

Quoted 10 lines
tests/tcg/aarch64/system/boot.S +10 -0
+	 * The test can set the semihosting command line to the target
+	 * EL needed for the test. Keep that in w11.
+	 */
+	mov	x0, SYS_GET_CMDLINE
+	adr	x1, .get_cmd
+	semihosting_call
+	adrp	x10, cmdline
+	add	x10, x10, :lo12:cmdline
+	ldrb	w11, [x10]
+	cbz	w11, 1f

Suggested sanity check:

cmp w11, #'0'
b.lt curr_sp0_sync
cmp w11, #'4'
b.ge curr_sp0_sync

Quoted 107 lines
tests/tcg/aarch64/system/boot.S +103 -0
+	sub 	w11, w11, #'0'
+1:
+
+	/* Determine current Exception Level */
+	mrs     x0, CurrentEL
+	lsr     x0, x0, #2        /* CurrentEL[3:2] contains the current EL */
+
+	/* Branch based on current EL */
+	cmp     x0, #3
+	b.eq    setup_el3
+	cmp     x0, #2
+	b.eq    setup_el2
+	cmp     x0, #1
+	b.eq    at_testel            /* Already at EL1, skip transition */
+	/* Should not be at EL0 - error out */
+	b       curr_sp0_sync
+
+setup_el3:
+	/* Ensure we trap if we get anything wrong */
+	adr	x0, vector_table
+	msr	vbar_el3, x0
+
+	/* Does the test want to be at EL3? */
+	cmp	w11, #3
+	beq	at_testel
+
+	/* Configure EL3 to for lower states (EL2 or EL1) */
+	mrs     x0, scr_el3
+	orr     x0, x0, #(1 << 10)    /* RW = 1: EL2/EL1 execution state is AArch64 */
+	orr     x0, x0, #(1 << 0)     /* NS = 1: Non-secure state */
+	msr     scr_el3, x0
+
+	/*
+	 * We need to check if EL2 is actually enabled via ID_AA64PFR0_EL1,
+	 * otherwise we should just jump straight to EL1.
+	 */
+	mrs     x0, id_aa64pfr0_el1
+	ubfx    x0, x0, #8, #4        /* Extract EL2 field (bits 11:8) */
+	cbz     x0, el2_not_present   /* If field is 0 no EL2 */
+
+
+	/* Prepare SPSR for exception return to EL2 */
+	mov	x0, #0x3c9            /* DAIF bits and EL2h mode (9) */
+	msr     spsr_el3, x0
+
+	/* Set EL2 entry point */
+	adr     x0, setup_el2
+	msr     elr_el3, x0
+
+	/* Return to EL2 */
+	eret
+	nop
+
+el2_not_present:
+	/* Initialize SCTLR_EL1 with reset value */
+	msr     sctlr_el1, xzr
+
+	/* Set EL1 entry point */
+	adr     x0, at_testel
+	msr     elr_el3, x0
+
+	/* Prepare SPSR for exception return to EL1h with interrupts masked */
+	mov     x0, #0x3c5            /* DAIF bits and EL1h mode (5) */
+	msr     spsr_el3, x0
+
+	isb                           /* Synchronization barrier */
+	eret                          /* Jump to EL1 */
+
+setup_el2:
+	/* Ensure we trap if we get anything wrong */
+	adr	x0, vector_table
+	msr	vbar_el2, x0
+
+	/* Does the test want to be at EL2? */
+	cmp	w11, #2
+	beq	at_testel
+
+	/* Configure EL2 to allow transition to EL1 */
+	mrs     x0, hcr_el2
+	orr     x0, x0, #(1 << 31)    /* RW = 1: EL1 execution state is AArch64 */
+	msr     hcr_el2, x0
+
+	/* Initialize SCTLR_EL1 with reset value */
+	msr     sctlr_el1, xzr
+
+	/* Set EL1 entry point */
+	adr     x0, at_testel
+	msr     elr_el2, x0
+
+	/* Prepare SPSR for exception return to EL1 */
+	mov     x0, #(0x5 << 0)       /* EL1h (SPx), with interrupts disabled */
+	msr     spsr_el2, x0
+
+	/* Return to EL1 */
+	eret
+
+	nop
+
+	/*
+	 * At the target EL for the test, usually EL1. Note we still
+	 * set everything up as if we were at EL1.
+	 */
+at_testel:

/* Installs a table of exception vectors to catch and handle all
exceptions by terminating the process with a diagnostic. */
adr x0, vector_table

tests/tcg/aarch64/system/boot.S __start: · line 223

Quoted 5 lines
tests/tcg/aarch64/system/boot.S +1 -1
@@ -100,7 +220,7 @@ __start:
 	 * maps RAM to the first Gb. The stage2 tables have two 2mb
 	 * translation block entries covering a series of adjacent
 	 * 4k pages.
-	*/
+ 	 */

Accidental space

Quoted 17 lines
tests/tcg/aarch64/system/boot.S +5 -0
 	/* Stage 1 entry: indexed by IA[38:30] */
 	adr	x1, .				/* phys address */
@@ -233,6 +353,11 @@ __sys_outc:
 	ret
 
 	.data
+
+	.align 8
+cmdline:
+	.space 128, 0
+
 	.align	12
 
 	/* Translation table
Signature · 1 line
2.39.5

Otherwise,

#9 lore raw

PATCH 4/9 contrib/plugins: add a scaling factor to the ips arg

In reply to Alex Bennée

It's easy to get lost in zeros while setting the numbers of instructions per second. Add a scaling suffix to make things simpler.

---
 contrib/plugins/ips.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
contrib/plugins/ips.c +22 -1
index e5297dbb01..1952e0866d 100644
@@ -20,6 +20,8 @@
 
 QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
 /* how many times do we update time per sec */
 #define NUM_TIME_UPDATE_PER_SEC 10
 #define NSEC_IN_ONE_SEC (1000 * 1000 * 1000)
@@ -129,6 +131,18 @@ static void plugin_exit(qemu_plugin_id_t id, void *udata)
     qemu_plugin_scoreboard_free(vcpus);
 }
 
+typedef struct {
+    const char* suffix;
+    unsigned long multipler;
+} scale_entry;
+
+/* a bit like units.h but not binary */
+static scale_entry scales[] = {
+    { "khz", 1000 },
+    { "mhz", 1000 * 1000 },
+    { "ghz", 1000 * 1000 * 1000 },
+};
+
 QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            const qemu_info_t *info, int argc,
                                            char **argv)
@@ -137,12 +151,19 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
         char *opt = argv[i];
         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
         if (g_strcmp0(tokens[0], "ips") == 0) {
-            max_insn_per_second = g_ascii_strtoull(tokens[1], NULL, 10);
+            char *endptr = NULL;
+            max_insn_per_second = g_ascii_strtoull(tokens[1], &endptr, 10);
             if (!max_insn_per_second && errno) {
                 fprintf(stderr, "%s: couldn't parse %s (%s)\n",
                         __func__, tokens[1], g_strerror(errno));
                 return -1;
             }
+            for (int j = 0; j < ARRAY_SIZE(scales); j++) {
+                if (g_str_has_suffix(endptr, scales[j].suffix)) {
+                        max_insn_per_second *= scales[j].multipler;
+                        break;
+                }
+            }
         } else {
             fprintf(stderr, "option parsing failed: %s\n", opt);
             return -1;
Signature · 1 line
2.39.5
Reviewed-by on v1 4/9
#10 lore raw

Re: [PATCH 4/9] contrib/plugins: add a scaling factor to the ips arg

Quoted 47 lines · Alex Bennée

It's easy to get lost in zeros while setting the numbers of instructions per second. Add a scaling suffix to make things simpler.

---
  contrib/plugins/ips.c | 23 ++++++++++++++++++++++-
  1 file changed, 22 insertions(+), 1 deletion(-)
contrib/plugins/ips.c +16 -1
index e5297dbb01..1952e0866d 100644
@@ -20,6 +20,8 @@
 
  QEMU_PLUGIN_EXPORT int qemu_plugin_version = QEMU_PLUGIN_VERSION;
 
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
  /* how many times do we update time per sec */
  #define NUM_TIME_UPDATE_PER_SEC 10
  #define NSEC_IN_ONE_SEC (1000 * 1000 * 1000)
@@ -129,6 +131,18 @@ static void plugin_exit(qemu_plugin_id_t id, void *udata)
      qemu_plugin_scoreboard_free(vcpus);
  }
 
+typedef struct {
+    const char* suffix;
+    unsigned long multipler;
+} scale_entry;
+
+/* a bit like units.h but not binary */
+static scale_entry scales[] = {
+    { "khz", 1000 },
+    { "mhz", 1000 * 1000 },
+    { "ghz", 1000 * 1000 * 1000 },
+};
+
  QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                             const qemu_info_t *info, int argc,
                                             char **argv)
@@ -137,12 +151,19 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
          char *opt = argv[i];
          g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
          if (g_strcmp0(tokens[0], "ips") == 0) {
-            max_insn_per_second = g_ascii_strtoull(tokens[1], NULL, 10);
+            char *endptr = NULL;
+            max_insn_per_second = g_ascii_strtoull(tokens[1], &endptr, 10);

contrib/plugins/ips.c QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, · line 165

Quoted 10 lines
contrib/plugins/ips.c +5 -0
              if (!max_insn_per_second && errno) {
                  fprintf(stderr, "%s: couldn't parse %s (%s)\n",
                          __func__, tokens[1], g_strerror(errno));
                  return -1;
              }
+            for (int j = 0; j < ARRAY_SIZE(scales); j++) {
+                if (g_str_has_suffix(endptr, scales[j].suffix)) {
+                        max_insn_per_second *= scales[j].multipler;
+                        break;
+                }

Could we add an error in case the present suffix is not recognized, with the list of available ones? As well, can lower the suffix string, so people are free to use any case.

contrib/plugins/ips.c QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id, · line 169

Quoted 4 lines
contrib/plugins/ips.c +1 -0
+            }
          } else {
              fprintf(stderr, "option parsing failed: %s\n", opt);
              return -1;

With the remark above,

#11 lore raw

PATCH 5/9 contrib/plugins: allow setting of instructions per quantum

In reply to Alex Bennée

The default is we update time every 1/10th of a second or so. However for some cases we might want to update time more frequently. Allow this to be set via the command line through the ipq argument.

---
 docs/about/emulation.rst |  4 ++++
 contrib/plugins/ips.c    | 10 +++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)
docs/about/emulation.rst +4 -0
index a72591ee4d..b607183d13 100644
@@ -811,6 +811,10 @@ This plugin can limit the number of Instructions Per Second that are executed::
   * - ips=N
     - Maximum number of instructions per cpu that can be executed in one second.
       The plugin will sleep when the given number of instructions is reached.
+  * - ipq=N
+    - Instructions per quantum. How many instructions before we re-calculate time. 
+      The lower the number the more accurate time will be, but the less efficient the plugin.
+      Defaults to ips/10
 
 Other emulation features
 ------------------------
contrib/plugins/ips.c +9 -1
index 1952e0866d..d83124d71c 100644
@@ -147,6 +147,8 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                                            const qemu_info_t *info, int argc,
                                            char **argv)
 {
+    bool ipq_set = false;
+
     for (int i = 0; i < argc; i++) {
         char *opt = argv[i];
         g_auto(GStrv) tokens = g_strsplit(opt, "=", 2);
@@ -164,6 +166,9 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
                         break;
                 }
             }
+        } else if (g_strcmp0(tokens[0], "ipq") == 0) {
+            max_insn_per_quantum = g_ascii_strtoull(tokens[1], NULL, 10);
+            ipq_set = true;
         } else {
             fprintf(stderr, "option parsing failed: %s\n", opt);
             return -1;
@@ -171,7 +176,10 @@ QEMU_PLUGIN_EXPORT int qemu_plugin_install(qemu_plugin_id_t id,
     }
 
     vcpus = qemu_plugin_scoreboard_new(sizeof(vCPUTime));
-    max_insn_per_quantum = max_insn_per_second / NUM_TIME_UPDATE_PER_SEC;
+
+    if (!ipq_set) {
+        max_insn_per_quantum = max_insn_per_second / NUM_TIME_UPDATE_PER_SEC;
+    }
 
     if (max_insn_per_quantum == 0) {
         fprintf(stderr, "minimum of %d instructions per second needed\n",
Signature · 1 line
2.39.5
Reviewed-by on v1 5/9
#12 lore raw

Re: [PATCH 5/9] contrib/plugins: allow setting of instructions per quantum

Quoted 10 lines · Alex Bennée

The default is we update time every 1/10th of a second or so. However for some cases we might want to update time more frequently. Allow this to be set via the command line through the ipq argument.

---
  docs/about/emulation.rst |  4 ++++
  contrib/plugins/ips.c    | 10 +++++++++-
  2 files changed, 13 insertions(+), 1 deletion(-)

Good idea!

#13 lore raw

PATCH 6/9 MAINTAINERS: add myself to virtio-gpu for Odd Fixes

In reply to Alex Bennée

Seeing as I've taken a few patches to here now I might as well put myself forward to maintain virtio-gpu. I've marked it as Odd Fixes as it's not my core focus. If someone with more GPU experience comes forward we can always update again.

---
 MAINTAINERS | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
MAINTAINERS +2 -1
index 661a47db5a..f67c8edcf6 100644
@@ -2636,7 +2636,8 @@ F: hw/display/ramfb*.c
 F: include/hw/display/ramfb.h
 
 virtio-gpu
-S: Orphan
+M: Alex Bennée <[email protected]>
+M: Odd Fixes
 F: hw/display/virtio-gpu*
 F: hw/display/virtio-vga.*
 F: include/hw/virtio/virtio-gpu.h
Signature · 1 line
2.39.5
Reviewed-by on v1 6/9
#14 lore raw

Re: [PATCH 6/9] MAINTAINERS: add myself to virtio-gpu for Odd Fixes

Quoted 15 lines · Alex Bennée

Seeing as I've taken a few patches to here now I might as well put myself forward to maintain virtio-gpu. I've marked it as Odd Fixes as it's not my core focus. If someone with more GPU experience comes forward we can always update again.

---
  MAINTAINERS | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
MAINTAINERS +0 -0
index 661a47db5a..f67c8edcf6 100644

MAINTAINERS F: hw/display/ramfb*.c · line 2643

Quoted 9 lines
MAINTAINERS +2 -1
@@ -2636,7 +2636,8 @@ F: hw/display/ramfb*.c
  F: include/hw/display/ramfb.h
 
  virtio-gpu
-S: Orphan
+M: Alex Bennée <[email protected]>
+M: Odd Fixes
  F: hw/display/virtio-gpu*
  F: hw/display/virtio-vga.*
  F: include/hw/virtio/virtio-gpu.h
#15 lore raw

Re: [PATCH 6/9] MAINTAINERS: add myself to virtio-gpu for Odd Fixes

In reply to Alex Bennée
Quoted 15 lines · Alex Bennée

Seeing as I've taken a few patches to here now I might as well put myself forward to maintain virtio-gpu. I've marked it as Odd Fixes as it's not my core focus. If someone with more GPU experience comes forward we can always update again.

---
 MAINTAINERS | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
MAINTAINERS +0 -0
index 661a47db5a..f67c8edcf6 100644

MAINTAINERS F: hw/display/ramfb*.c · line 2643

Quoted 9 lines
MAINTAINERS +2 -1
@@ -2636,7 +2636,8 @@ F: hw/display/ramfb*.c
 F: include/hw/display/ramfb.h
 
 virtio-gpu
-S: Orphan
+M: Alex Bennée <[email protected]>
+M: Odd Fixes
 F: hw/display/virtio-gpu*
 F: hw/display/virtio-vga.*
 F: include/hw/virtio/virtio-gpu.h

Thanks a lot for stepping up!

This reminded me that I wanted to propose myself as reviewer for the virtio-gpu patches. Will do it soon.

Akihiko Odaki is also good at reviewing virtio-gpu patches. Wondering if Akihiko would want to be added as reviewer or co-maintainer of virtio-gpu?

Signature · 2 lines
Best regards,
Dmitry
#16 lore raw

Re: [PATCH 6/9] MAINTAINERS: add myself to virtio-gpu for Odd Fixes

Quoted 28 lines · Dmitry Osipenko
Quoted 15 lines · Alex Bennée

Seeing as I've taken a few patches to here now I might as well put myself forward to maintain virtio-gpu. I've marked it as Odd Fixes as it's not my core focus. If someone with more GPU experience comes forward we can always update again.

---
  MAINTAINERS | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
MAINTAINERS +0 -0
index 661a47db5a..f67c8edcf6 100644

MAINTAINERS F: hw/display/ramfb*.c · line 2643

Quoted 9 lines
MAINTAINERS +2 -1
@@ -2636,7 +2636,8 @@ F: hw/display/ramfb*.c
  F: include/hw/display/ramfb.h
 
  virtio-gpu
-S: Orphan
+M: Alex Bennée <[email protected]>
+M: Odd Fixes
  F: hw/display/virtio-gpu*
  F: hw/display/virtio-vga.*
  F: include/hw/virtio/virtio-gpu.h

Thanks a lot for stepping up!

Quoted 2 lines

This reminded me that I wanted to propose myself as reviewer for the virtio-gpu patches. Will do it soon.

Thank both of you for stepping up.

Quoted 4 lines

Akihiko Odaki is also good at reviewing virtio-gpu patches. Wondering if Akihiko would want to be added as reviewer or co-maintainer of virtio-gpu?

Yes, please add me as a reviewer.

I guess it would be better if Alex add you and me with the next version of this patch or add follow-up patches to this to avoid change conflicts.

#17 lore raw

Re: [PATCH 6/9] MAINTAINERS: add myself to virtio-gpu for Odd Fixes

Quoted 39 lines · Akihiko Odaki
Quoted 27 lines · Dmitry Osipenko
Quoted 20 lines · Alex Bennée

Seeing as I've taken a few patches to here now I might as well put myself forward to maintain virtio-gpu. I've marked it as Odd Fixes as it's not my core focus. If someone with more GPU experience comes forward we can always update again.

---
  MAINTAINERS | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
MAINTAINERS +0 -0
index 661a47db5a..f67c8edcf6 100644
@@ -2636,7 +2636,8 @@ F: hw/display/ramfb*.c

  F: include/hw/display/ramfb.h
    virtio-gpu

MAINTAINERS +2 -1
-S: Orphan
+M: Alex Bennée <[email protected]>
+M: Odd Fixes
Quoted 3 lines

  F: hw/display/virtio-gpu*
  F: hw/display/virtio-vga.*
  F: include/hw/virtio/virtio-gpu.h

Thanks a lot for stepping up!

Quoted 2 lines

This reminded me that I wanted to propose myself as reviewer for the virtio-gpu patches. Will do it soon.

Thank both of you for stepping up.

Quoted 5 lines

Akihiko Odaki is also good at reviewing virtio-gpu patches. Wondering if Akihiko would want to be added as reviewer or co-maintainer of virtio- gpu?

Quoted 4 lines

Yes, please add me as a reviewer.

I guess it would be better if Alex add you and me with the next version of this patch or add follow-up patches to this to avoid change conflicts.

+1 Feel free to add us as reviewers in v2 of this patch. Otherwise I'll send the separate maintainers-update patch along with nctx v12 patches.

Signature · 2 lines
Best regards,
Dmitry
#18 lore raw

Re: [PATCH 6/9] MAINTAINERS: add myself to virtio-gpu for Odd Fixes

Quoted 40 lines · Dmitry Osipenko
Quoted 39 lines · Akihiko Odaki
Quoted 27 lines · Dmitry Osipenko
Quoted 20 lines · Alex Bennée

Seeing as I've taken a few patches to here now I might as well put myself forward to maintain virtio-gpu. I've marked it as Odd Fixes as it's not my core focus. If someone with more GPU experience comes forward we can always update again.

---
  MAINTAINERS | 3 ++-

  1 file changed, 2 insertions(+), 1 deletion(-)
MAINTAINERS +0 -0
index 661a47db5a..f67c8edcf6 100644
@@ -2636,7 +2636,8 @@ F: hw/display/ramfb*.c

  F: include/hw/display/ramfb.h
    virtio-gpu

MAINTAINERS +2 -1
-S: Orphan
+M: Alex Bennée <[email protected]>
+M: Odd Fixes
Quoted 3 lines

  F: hw/display/virtio-gpu*
  F: hw/display/virtio-vga.*
  F: include/hw/virtio/virtio-gpu.h

Thanks a lot for stepping up!

Quoted 2 lines

This reminded me that I wanted to propose myself as reviewer for the virtio-gpu patches. Will do it soon.

Thank both of you for stepping up.

Quoted 5 lines

Akihiko Odaki is also good at reviewing virtio-gpu patches. Wondering if Akihiko would want to be added as reviewer or co-maintainer of virtio- gpu?

Quoted 8 lines
Quoted 4 lines

Yes, please add me as a reviewer.

I guess it would be better if Alex add you and me with the next version of this patch or add follow-up patches to this to avoid change conflicts.

+1 Feel free to add us as reviewers in v2 of this patch. Otherwise I'll send the separate maintainers-update patch along with nctx v12 patches.

I shall add you both ;-)

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#19 lore raw

PATCH 7/9 hw/display: re-arrange memory region tracking

In reply to Alex Bennée

QOM objects can be embedded in other QOM objects and managed as part of their lifetime but this isn't the case for virtio_gpu_virgl_hostmem_region. However before we can split it out we need some other way of associating the wider data structure with the memory region.

Fortunately MemoryRegion has an opaque pointer. This is passed down to MemoryRegionOps for device type regions but is unused in the memory_region_init_ram_ptr() case. Use the opaque to carry the reference and allow the final MemoryRegion object to be reaped when its reference count is cleared.

Message-Id: <[email protected]>
Cc: [email protected]

---
 include/system/memory.h       |  1 +
 hw/display/virtio-gpu-virgl.c | 23 ++++++++---------------
 2 files changed, 9 insertions(+), 15 deletions(-)
include/system/memory.h +1 -0
index fbbf4cf911..b3cef1acb5 100644
@@ -783,6 +783,7 @@ struct MemoryRegion {
     DeviceState *dev;
 
     const MemoryRegionOps *ops;
+    /* opaque data, used by backends like @ops */
     void *opaque;
     MemoryRegion *container;
     int mapped_via_alias; /* Mapped via an alias, container might be NULL */
hw/display/virtio-gpu-virgl.c +8 -15
index 145a0b3879..71a7500de9 100644
@@ -52,17 +52,11 @@ virgl_get_egl_display(G_GNUC_UNUSED void *cookie)
 
 #if VIRGL_VERSION_MAJOR >= 1
 struct virtio_gpu_virgl_hostmem_region {
-    MemoryRegion mr;
+    MemoryRegion *mr;
     struct VirtIOGPU *g;
     bool finish_unmapping;
 };
 
-static struct virtio_gpu_virgl_hostmem_region *
-to_hostmem_region(MemoryRegion *mr)
-{
-    return container_of(mr, struct virtio_gpu_virgl_hostmem_region, mr);
-}
-
 static void virtio_gpu_virgl_resume_cmdq_bh(void *opaque)
 {
     VirtIOGPU *g = opaque;
@@ -73,14 +67,12 @@ static void virtio_gpu_virgl_resume_cmdq_bh(void *opaque)
 static void virtio_gpu_virgl_hostmem_region_free(void *obj)
 {
     MemoryRegion *mr = MEMORY_REGION(obj);
-    struct virtio_gpu_virgl_hostmem_region *vmr;
+    struct virtio_gpu_virgl_hostmem_region *vmr = mr->opaque;
     VirtIOGPUBase *b;
     VirtIOGPUGL *gl;
 
-    vmr = to_hostmem_region(mr);
-    vmr->finish_unmapping = true;
-
     b = VIRTIO_GPU_BASE(vmr->g);
+    vmr->finish_unmapping = true;
     b->renderer_blocked--;
 
     /*
@@ -118,8 +110,8 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
 
     vmr = g_new0(struct virtio_gpu_virgl_hostmem_region, 1);
     vmr->g = g;
+    mr = g_new0(MemoryRegion, 1);
 
-    mr = &vmr->mr;
     memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
     memory_region_add_subregion(&b->hostmem, offset, mr);
     memory_region_set_enabled(mr, true);
@@ -131,7 +123,9 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
      * command processing until MR is fully unreferenced and freed.
      */
     OBJECT(mr)->free = virtio_gpu_virgl_hostmem_region_free;
+    mr->opaque = vmr;
 
+    vmr->mr = mr;
     res->mr = mr;
 
     return 0;
@@ -142,16 +136,15 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
                                      struct virtio_gpu_virgl_resource *res,
                                      bool *cmd_suspended)
 {
-    struct virtio_gpu_virgl_hostmem_region *vmr;
     VirtIOGPUBase *b = VIRTIO_GPU_BASE(g);
     MemoryRegion *mr = res->mr;
+    struct virtio_gpu_virgl_hostmem_region *vmr;
     int ret;
 
     if (!mr) {
         return 0;
     }
-
-    vmr = to_hostmem_region(res->mr);
+    vmr = mr->opaque;
 
     /*
      * Perform async unmapping in 3 steps:
Signature · 1 line
2.39.5
#20 lore raw

PATCH 8/9 virtio-gpu: fix hang under TCG when unmapping blob

In reply to Alex Bennée

From: Manos Pitsidianakis <[email protected]>

This commit fixes an indefinite hang when using VIRTIO GPU blob objects under TCG in certain conditions.

The VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB VIRTIO command creates a MemoryRegion and attaches it to an offset on a PCI BAR of the VirtIOGPUdevice. The VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB command unmaps it.

Because virglrenderer commands are not thread-safe they are only called on the main context and QEMU performs the cleanup in three steps to prevent a use-after-free scenario where the guest can access the region after it’s unmapped:

1. From the main context, the region’s field finish_unmapping is false by default, so it sets a variable cmd_suspended, increases the renderer_blocked variable, deletes the blob subregion, and unparents the blob subregion causing its reference count to decrement.

2. From an RCU context, the MemoryView gets freed, the FlatView gets recalculated, the free callback of the blob region virtio_gpu_virgl_hostmem_region_free is called which sets the region’s field finish_unmapping to true, allowing the main thread context to finish replying to the command

3. From the main context, the command is processed again, but this time finish_unmapping is true, so virgl_renderer_resource_unmap can be called and a response is sent to the guest.

It happens so that under TCG, if the guest has no timers configured (and thus no interrupt will cause the CPU to exit), the RCU thread does not have enough time to grab the locks and recalculate the FlatView.

That’s not a big problem in practice since most guests will assume a response will happen later in time and go on to do different things, potentially triggering interrupts and allowing the RCU context to run. If the guest waits for the unmap command to complete though, it blocks indefinitely. Attaching to the QEMU monitor and force quitting the guest allows the cleanup to continue.

There's no reason why the FlatView recalculation can't occur right away when we delete the blob subregion, however. It does not, because when we create the subregion we set the object as its own parent:

    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);

The extra reference is what prevents freeing the memory region object in the memory transaction of deleting the subregion.

This commit changes the owner object to the device, which removes the extra owner reference in the memory region and causes the MR to be freed right away in the main context.

Message-Id: <[email protected]>
Cc: [email protected]

---
 hw/display/virtio-gpu-virgl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +1 -1
index 71a7500de9..8fbe4e70cc 100644
@@ -112,7 +112,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
     vmr->g = g;
     mr = g_new0(MemoryRegion, 1);
 
-    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+    memory_region_init_ram_ptr(mr, OBJECT(g), "blob", size, data);
     memory_region_add_subregion(&b->hostmem, offset, mr);
     memory_region_set_enabled(mr, true);
 
Signature · 1 line
2.39.5
#21 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

Quoted 69 lines · Alex Bennée

From: Manos Pitsidianakis <[email protected]>

This commit fixes an indefinite hang when using VIRTIO GPU blob objects under TCG in certain conditions.

The VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB VIRTIO command creates a MemoryRegion and attaches it to an offset on a PCI BAR of the VirtIOGPUdevice. The VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB command unmaps it.

Because virglrenderer commands are not thread-safe they are only called on the main context and QEMU performs the cleanup in three steps to prevent a use-after-free scenario where the guest can access the region after it’s unmapped:

1. From the main context, the region’s field finish_unmapping is false by default, so it sets a variable cmd_suspended, increases the renderer_blocked variable, deletes the blob subregion, and unparents the blob subregion causing its reference count to decrement.

2. From an RCU context, the MemoryView gets freed, the FlatView gets recalculated, the free callback of the blob region virtio_gpu_virgl_hostmem_region_free is called which sets the region’s field finish_unmapping to true, allowing the main thread context to finish replying to the command

3. From the main context, the command is processed again, but this time finish_unmapping is true, so virgl_renderer_resource_unmap can be called and a response is sent to the guest.

It happens so that under TCG, if the guest has no timers configured (and thus no interrupt will cause the CPU to exit), the RCU thread does not have enough time to grab the locks and recalculate the FlatView.

That’s not a big problem in practice since most guests will assume a response will happen later in time and go on to do different things, potentially triggering interrupts and allowing the RCU context to run. If the guest waits for the unmap command to complete though, it blocks indefinitely. Attaching to the QEMU monitor and force quitting the guest allows the cleanup to continue.

There's no reason why the FlatView recalculation can't occur right away when we delete the blob subregion, however. It does not, because when we create the subregion we set the object as its own parent:

    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);

The extra reference is what prevents freeing the memory region object in the memory transaction of deleting the subregion.

This commit changes the owner object to the device, which removes the extra owner reference in the memory region and causes the MR to be freed right away in the main context.

Message-Id: <[email protected]>
Cc: [email protected]

---
 hw/display/virtio-gpu-virgl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +0 -0
index 71a7500de9..8fbe4e70cc 100644

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, · line 118

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +1 -1
@@ -112,7 +112,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
     vmr->g = g;
     mr = g_new0(MemoryRegion, 1);
 
-    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+    memory_region_init_ram_ptr(mr, OBJECT(g), "blob", size, data);
     memory_region_add_subregion(&b->hostmem, offset, mr);
     memory_region_set_enabled(mr, true);
 

This change makes QEMU to crash.

AFAICT, it effectively reverts code to old bugged version [1] that was rejected in the past.

+Akihiko Odaki

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

Signature · 2 lines
Best regards,
Dmitry
#22 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

Quoted 73 lines · Dmitry Osipenko
Quoted 72 lines · Alex Bennée

From: Manos Pitsidianakis <[email protected]>

This commit fixes an indefinite hang when using VIRTIO GPU blob objects under TCG in certain conditions.

The VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB VIRTIO command creates a MemoryRegion and attaches it to an offset on a PCI BAR of the VirtIOGPUdevice. The VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB command unmaps it.

Because virglrenderer commands are not thread-safe they are only called on the main context and QEMU performs the cleanup in three steps to prevent a use-after-free scenario where the guest can access the region after it’s unmapped:

1. From the main context, the region’s field finish_unmapping is false by default, so it sets a variable cmd_suspended, increases the renderer_blocked variable, deletes the blob subregion, and unparents the blob subregion causing its reference count to decrement.

2. From an RCU context, the MemoryView gets freed, the FlatView gets recalculated, the free callback of the blob region virtio_gpu_virgl_hostmem_region_free is called which sets the region’s field finish_unmapping to true, allowing the main thread context to finish replying to the command

3. From the main context, the command is processed again, but this time finish_unmapping is true, so virgl_renderer_resource_unmap can be called and a response is sent to the guest.

It happens so that under TCG, if the guest has no timers configured (and thus no interrupt will cause the CPU to exit), the RCU thread does not have enough time to grab the locks and recalculate the FlatView.

That’s not a big problem in practice since most guests will assume a response will happen later in time and go on to do different things, potentially triggering interrupts and allowing the RCU context to run. If the guest waits for the unmap command to complete though, it blocks indefinitely. Attaching to the QEMU monitor and force quitting the guest allows the cleanup to continue.

There's no reason why the FlatView recalculation can't occur right away when we delete the blob subregion, however. It does not, because when we create the subregion we set the object as its own parent:

    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);

The extra reference is what prevents freeing the memory region object in the memory transaction of deleting the subregion.

This commit changes the owner object to the device, which removes the extra owner reference in the memory region and causes the MR to be freed right away in the main context.

Message-Id: <[email protected]>
Cc: [email protected]

---
 hw/display/virtio-gpu-virgl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +0 -0
index 71a7500de9..8fbe4e70cc 100644
@@ -112,7 +112,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
     vmr->g = g;
     mr = g_new0(MemoryRegion, 1);
 
Quoted 7 lines

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, · line 118

Quoted 5 lines
hw/display/virtio-gpu-virgl.c +1 -1
-    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+    memory_region_init_ram_ptr(mr, OBJECT(g), "blob", size, data);
     memory_region_add_subregion(&b->hostmem, offset, mr);
     memory_region_set_enabled(mr, true);
 

This change makes QEMU to crash.

What is your command line to cause the crash?

Quoted 8 lines

AFAICT, it effectively reverts code to old bugged version [1] that was rejected in the past.

+Akihiko Odaki

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#23 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

Quoted 2 lines · Alex Bennée
Quoted 1 line

This change makes QEMU to crash.

What is your command line to cause the crash?

I applied this patch on top of native context v11, ran AMD nctx and got a crash on SDDM startup.

(gdb) bt

#0  0x00007ffff5411b54 in __pthread_kill_implementation () at /lib64/libc.so.6
#1  0x00007ffff53b8f9e in raise () at /lib64/libc.so.6
#2  0x00007ffff53a0942 in abort () at /lib64/libc.so.6
#3  0x00007ffff6cbf18c in g_assertion_message[cold] () at /lib64/libglib-2.0.so.0
#4  0x00007ffff6d2ea07 in g_assertion_message_expr () at /lib64/libglib-2.0.so.0
#5  0x0000555555a42820 in object_finalize (data=0x555557c9d290) at ../qom/object.c:732
#6  object_unref (objptr=0x555557c9d290) at ../qom/object.c:1231
#7  0x00005555559f3df3 in memory_region_unref (mr=<optimized out>) at ../system/memory.c:1854
#8  0x0000555555a003a7 in phys_section_destroy (mr=0x555559ef5b60) at ../system/physmem.c:1035
#9  phys_sections_free (map=0x555559c2dd80) at ../system/physmem.c:1048

#10 address_space_dispatch_free (d=0x555559c2dd70) at ../system/physmem.c:2692
#11 0x00005555559f1d33 in flatview_destroy (view=0x55555a54a720) at ../system/memory.c:295 #12 0x0000555555c278cf in call_rcu_thread (opaque=opaque@entry=0x0) at ../util/rcu.c:301 #13 0x0000555555c1cc68 in qemu_thread_start (args=0x555557993d30) at ../util/qemu-thread-posix.c:541 #14 0x00007ffff540fba8 in start_thread () at /lib64/libc.so.6
#15 0x00007ffff5493b8c in __clone3 () at /lib64/libc.so.6

Signature · 2 lines
Best regards,
Dmitry
#24 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

Quoted 6 lines · Dmitry Osipenko
Quoted 2 lines · Alex Bennée
Quoted 1 line

This change makes QEMU to crash.

What is your command line to cause the crash?

I applied this patch on top of native context v11, ran AMD nctx and got a crash on SDDM startup.

Did you also include the pre-cursor patch which splits MemoryRegion out of the container struct. The aim here is to allow MemoryRegion counting to be handled without worrying about other structure lifetimes.

Quoted 18 lines

(gdb) bt

#0  0x00007ffff5411b54 in __pthread_kill_implementation () at /lib64/libc.so.6
#1  0x00007ffff53b8f9e in raise () at /lib64/libc.so.6
#2  0x00007ffff53a0942 in abort () at /lib64/libc.so.6
#3  0x00007ffff6cbf18c in g_assertion_message[cold] () at /lib64/libglib-2.0.so.0
#4  0x00007ffff6d2ea07 in g_assertion_message_expr () at /lib64/libglib-2.0.so.0
#5  0x0000555555a42820 in object_finalize (data=0x555557c9d290) at ../qom/object.c:732
#6  object_unref (objptr=0x555557c9d290) at ../qom/object.c:1231
#7  0x00005555559f3df3 in memory_region_unref (mr=<optimized out>) at ../system/memory.c:1854
#8  0x0000555555a003a7 in phys_section_destroy (mr=0x555559ef5b60) at ../system/physmem.c:1035
#9  phys_sections_free (map=0x555559c2dd80) at ../system/physmem.c:1048

#10 address_space_dispatch_free (d=0x555559c2dd70) at ../system/physmem.c:2692
#11 0x00005555559f1d33 in flatview_destroy (view=0x55555a54a720) at ../system/memory.c:295 #12 0x0000555555c278cf in call_rcu_thread (opaque=opaque@entry=0x0) at ../util/rcu.c:301 #13 0x0000555555c1cc68 in qemu_thread_start (args=0x555557993d30) at ../util/qemu-thread-posix.c:541 #14 0x00007ffff540fba8 in start_thread () at /lib64/libc.so.6
#15 0x00007ffff5493b8c in __clone3 () at /lib64/libc.so.6

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#25 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

Quoted 8 lines · Alex Bennée
Quoted 5 lines · Alex Bennée
Quoted 2 lines
Quoted 1 line

This change makes QEMU to crash.

What is your command line to cause the crash?

I applied this patch on top of native context v11, ran AMD nctx and got a crash on SDDM startup.

Did you also include the pre-cursor patch which splits MemoryRegion out of the container struct. The aim here is to allow MemoryRegion counting to be handled without worrying about other structure lifetimes.

Very good catch, I indeed missed that other patch. Reapplied all the patches and QEMU doesn't crash anymore. Now the code changes look sane to me.

Will be great if Akihiko Odaki could comment too.

Signature · 2 lines
Best regards,
Dmitry
#26 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

In reply to Dmitry Osipenko
Quoted 35 lines · Dmitry Osipenko
Quoted 34 lines · Alex Bennée

From: Manos Pitsidianakis <[email protected]>

This commit fixes an indefinite hang when using VIRTIO GPU blob objects under TCG in certain conditions.

The VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB VIRTIO command creates a MemoryRegion and attaches it to an offset on a PCI BAR of the VirtIOGPUdevice. The VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB command unmaps it.

Because virglrenderer commands are not thread-safe they are only called on the main context and QEMU performs the cleanup in three steps to prevent a use-after-free scenario where the guest can access the region after it’s unmapped:

1. From the main context, the region’s field finish_unmapping is false by default, so it sets a variable cmd_suspended, increases the renderer_blocked variable, deletes the blob subregion, and unparents the blob subregion causing its reference count to decrement.

2. From an RCU context, the MemoryView gets freed, the FlatView gets recalculated, the free callback of the blob region virtio_gpu_virgl_hostmem_region_free is called which sets the region’s field finish_unmapping to true, allowing the main thread context to finish replying to the command

3. From the main context, the command is processed again, but this time finish_unmapping is true, so virgl_renderer_resource_unmap can be called and a response is sent to the guest.

It happens so that under TCG, if the guest has no timers configured (and thus no interrupt will cause the CPU to exit), the RCU thread does not have enough time to grab the locks and recalculate the FlatView.

Quoted 6 lines
Quoted 6 lines

That’s not a big problem in practice since most guests will assume a response will happen later in time and go on to do different things, potentially triggering interrupts and allowing the RCU context to run. If the guest waits for the unmap command to complete though, it blocks indefinitely. Attaching to the QEMU monitor and force quitting the guest allows the cleanup to continue.

The RCU thread should be free to kick in if the guest is waiting and idle. That may be a problem that should be analyzed and fixed.

Quoted 40 lines
Quoted 29 lines

There's no reason why the FlatView recalculation can't occur right away when we delete the blob subregion, however. It does not, because when we create the subregion we set the object as its own parent:

     memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);

The extra reference is what prevents freeing the memory region object in the memory transaction of deleting the subregion.

This commit changes the owner object to the device, which removes the extra owner reference in the memory region and causes the MR to be freed right away in the main context.

Message-Id: <[email protected]>
Cc: [email protected]

---
  hw/display/virtio-gpu-virgl.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +0 -0
index 71a7500de9..8fbe4e70cc 100644

hw/display/virtio-gpu-virgl.c virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g, · line 118

Quoted 8 lines
hw/display/virtio-gpu-virgl.c +1 -1
@@ -112,7 +112,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
      vmr->g = g;
      mr = g_new0(MemoryRegion, 1);
 
-    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+    memory_region_init_ram_ptr(mr, OBJECT(g), "blob", size, data);
      memory_region_add_subregion(&b->hostmem, offset, mr);
      memory_region_set_enabled(mr, true);
 

This change makes QEMU to crash.

Quoted 7 lines

AFAICT, it effectively reverts code to old bugged version [1] that was rejected in the past.

+Akihiko Odaki

[1]
https://lore.kernel.org/qemu-devel/[email protected]/

I think you are right.

Changing the owner indeed makes the MR to be freed right away, but this is because it makes a reference by the FlatView to the MR dangling and can lead to use-after-free.

Setting the owner to the device implies that the device keeps the storage of the MR and. The references to the MR must be counted by the device to keep the storage available in such a case.

The MR itself doesn't count the references. It is fine as long as the MR is kept alive by being parented by the device, but virtio_gpu_virgl_unmap_resource_blob() unparents the MR and breaks the assumption. That's why the device shouldn't be the the owner of the MR.

Regards,
Akihiko Odaki

#27 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

Quoted 93 lines · Akihiko Odaki
Quoted 35 lines · Dmitry Osipenko
Quoted 34 lines · Alex Bennée

From: Manos Pitsidianakis <[email protected]>

This commit fixes an indefinite hang when using VIRTIO GPU blob objects under TCG in certain conditions.

The VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB VIRTIO command creates a MemoryRegion and attaches it to an offset on a PCI BAR of the VirtIOGPUdevice. The VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB command unmaps it.

Because virglrenderer commands are not thread-safe they are only called on the main context and QEMU performs the cleanup in three steps to prevent a use-after-free scenario where the guest can access the region after it’s unmapped:

1. From the main context, the region’s field finish_unmapping is false by default, so it sets a variable cmd_suspended, increases the renderer_blocked variable, deletes the blob subregion, and unparents the blob subregion causing its reference count to decrement.

2. From an RCU context, the MemoryView gets freed, the FlatView gets recalculated, the free callback of the blob region virtio_gpu_virgl_hostmem_region_free is called which sets the region’s field finish_unmapping to true, allowing the main thread context to finish replying to the command

3. From the main context, the command is processed again, but this time finish_unmapping is true, so virgl_renderer_resource_unmap can be called and a response is sent to the guest.

It happens so that under TCG, if the guest has no timers configured (and thus no interrupt will cause the CPU to exit), the RCU thread does not have enough time to grab the locks and recalculate the FlatView.

Quoted 6 lines
Quoted 6 lines

That’s not a big problem in practice since most guests will assume a response will happen later in time and go on to do different things, potentially triggering interrupts and allowing the RCU context to run. If the guest waits for the unmap command to complete though, it blocks indefinitely. Attaching to the QEMU monitor and force quitting the guest allows the cleanup to continue.

The RCU thread should be free to kick in if the guest is waiting and idle. That may be a problem that should be analyzed and fixed.

Quoted 36 lines
Quoted 36 lines

There's no reason why the FlatView recalculation can't occur right away when we delete the blob subregion, however. It does not, because when we create the subregion we set the object as its own parent:

     memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);

The extra reference is what prevents freeing the memory region object in the memory transaction of deleting the subregion.

This commit changes the owner object to the device, which removes the extra owner reference in the memory region and causes the MR to be freed right away in the main context.

Message-Id: <[email protected]>
Cc: [email protected]

---
  hw/display/virtio-gpu-virgl.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +1 -0
index 71a7500de9..8fbe4e70cc 100644
@@ -112,7 +112,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
      vmr->g = g;
      mr = g_new0(MemoryRegion, 1);
  -    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size,
 data);
+    memory_region_init_ram_ptr(mr, OBJECT(g), "blob", size, data);
      memory_region_add_subregion(&b->hostmem, offset, mr);
      memory_region_set_enabled(mr, true);
Quoted 8 lines
Quoted 1 line

This change makes QEMU to crash.
AFAICT, it effectively reverts code to old bugged version [1] that was
rejected in the past.
+Akihiko Odaki
[1]
https://lore.kernel.org/qemu-devel/[email protected]/

I think you are right.

Quoted 7 lines

Changing the owner indeed makes the MR to be freed right away, but this is because it makes a reference by the FlatView to the MR dangling and can lead to use-after-free.

Setting the owner to the device implies that the device keeps the storage of the MR and. The references to the MR must be counted by the device to keep the storage available in such a case.

Well the lifetime of the region is effectively controlled by the virgl resource manager. There is a disconnect between when we can finally free the MR and when the virglrenderer is done with it.

Quoted 5 lines

The MR itself doesn't count the references. It is fine as long as the MR is kept alive by being parented by the device, but virtio_gpu_virgl_unmap_resource_blob() unparents the MR and breaks the assumption. That's why the device shouldn't be the the owner of the MR.

Then who should? Ultimately some object needs to "own" the MR until we know it can be freed (so no longer mapped into the guest and no longer referenced by virglrenderer). The normal MR refcounting isn't enough because the lifetime extends beyond when it is "visible" in the FlatView.

Quoted 3 lines

Regards,
Akihiko Odaki

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#28 lore raw

Re: [PATCH 8/9] virtio-gpu: fix hang under TCG when unmapping blob

Quoted 113 lines · Alex Bennée
Quoted 93 lines · Akihiko Odaki
Quoted 35 lines · Dmitry Osipenko
Quoted 34 lines · Alex Bennée

From: Manos Pitsidianakis <[email protected]>

This commit fixes an indefinite hang when using VIRTIO GPU blob objects under TCG in certain conditions.

The VIRTIO_GPU_CMD_RESOURCE_MAP_BLOB VIRTIO command creates a MemoryRegion and attaches it to an offset on a PCI BAR of the VirtIOGPUdevice. The VIRTIO_GPU_CMD_RESOURCE_UNMAP_BLOB command unmaps it.

Because virglrenderer commands are not thread-safe they are only called on the main context and QEMU performs the cleanup in three steps to prevent a use-after-free scenario where the guest can access the region after it’s unmapped:

1. From the main context, the region’s field finish_unmapping is false by default, so it sets a variable cmd_suspended, increases the renderer_blocked variable, deletes the blob subregion, and unparents the blob subregion causing its reference count to decrement.

2. From an RCU context, the MemoryView gets freed, the FlatView gets recalculated, the free callback of the blob region virtio_gpu_virgl_hostmem_region_free is called which sets the region’s field finish_unmapping to true, allowing the main thread context to finish replying to the command

3. From the main context, the command is processed again, but this time finish_unmapping is true, so virgl_renderer_resource_unmap can be called and a response is sent to the guest.

It happens so that under TCG, if the guest has no timers configured (and thus no interrupt will cause the CPU to exit), the RCU thread does not have enough time to grab the locks and recalculate the FlatView.

Quoted 6 lines
Quoted 6 lines

That’s not a big problem in practice since most guests will assume a response will happen later in time and go on to do different things, potentially triggering interrupts and allowing the RCU context to run. If the guest waits for the unmap command to complete though, it blocks indefinitely. Attaching to the QEMU monitor and force quitting the guest allows the cleanup to continue.

The RCU thread should be free to kick in if the guest is waiting and idle. That may be a problem that should be analyzed and fixed.

Quoted 36 lines
Quoted 36 lines

There's no reason why the FlatView recalculation can't occur right away when we delete the blob subregion, however. It does not, because when we create the subregion we set the object as its own parent:

      memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);

The extra reference is what prevents freeing the memory region object in the memory transaction of deleting the subregion.

This commit changes the owner object to the device, which removes the extra owner reference in the memory region and causes the MR to be freed right away in the main context.

Message-Id: <[email protected]>
Cc: [email protected]

---
   hw/display/virtio-gpu-virgl.c | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +1 -0
index 71a7500de9..8fbe4e70cc 100644
@@ -112,7 +112,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
       vmr->g = g;
       mr = g_new0(MemoryRegion, 1);
   -    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size,
 data);
+    memory_region_init_ram_ptr(mr, OBJECT(g), "blob", size, data);
       memory_region_add_subregion(&b->hostmem, offset, mr);
       memory_region_set_enabled(mr, true);
Quoted 8 lines
Quoted 1 line

This change makes QEMU to crash.
AFAICT, it effectively reverts code to old bugged version [1] that was
rejected in the past.
+Akihiko Odaki
[1]
https://lore.kernel.org/qemu-devel/[email protected]/

I think you are right.

Quoted 7 lines

Changing the owner indeed makes the MR to be freed right away, but this is because it makes a reference by the FlatView to the MR dangling and can lead to use-after-free.

Setting the owner to the device implies that the device keeps the storage of the MR and. The references to the MR must be counted by the device to keep the storage available in such a case.

Well the lifetime of the region is effectively controlled by the virgl resource manager. There is a disconnect between when we can finally free the MR and when the virglrenderer is done with it.

Quoted 5 lines

The MR itself doesn't count the references. It is fine as long as the MR is kept alive by being parented by the device, but virtio_gpu_virgl_unmap_resource_blob() unparents the MR and breaks the assumption. That's why the device shouldn't be the the owner of the MR.

Quoted 4 lines

Then who should? Ultimately some object needs to "own" the MR until we know it can be freed (so no longer mapped into the guest and no longer referenced by virglrenderer). The normal MR refcounting isn't enough because the lifetime extends beyond when it is "visible" in the FlatView.

The current code makes the MR "own" itself. This works because the references are kept counted by the MR's own reference counter just like a normal Object.

Regards,
Akihiko Odaki

#29 lore raw

PATCH 9/9 virtio-gpu: refactor async blob unmapping

In reply to Alex Bennée

From: Manos Pitsidianakis <[email protected]>

Change the 3 part async cleanup of a blob memory mapping to check if the unmapping has finished already after deleting the subregion; this condition allows us to skip suspending the command and responding to the guest right away.

Message-Id: <[email protected]>
Cc: [email protected]

---
 hw/display/virtio-gpu-virgl.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
hw/display/virtio-gpu-virgl.c +25 -10
index 8fbe4e70cc..32a32879f7 100644
@@ -155,7 +155,32 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
      *    asynchronously by virtio_gpu_virgl_hostmem_region_free().
      * 3. Finish the unmapping with final virgl_renderer_resource_unmap().
      */
+
+    /* 1. Check if we should start unmapping now */
+    if (!vmr->finish_unmapping) {
+        /* begin async unmapping. render will be unblocked once MR is freed */
+        b->renderer_blocked++;
+
+        memory_region_set_enabled(mr, false);
+        memory_region_del_subregion(&b->hostmem, mr);
+        object_unparent(OBJECT(mr));
+        /*
+         * The unmapping might have already finished at this point if no one
+         * else held a reference to the MR; if yes, we can skip suspending the
+         * command and unmap the resource right away.
+         */
+        *cmd_suspended = !vmr->finish_unmapping;
+    }
+
+    /*
+     * 2. if virtio_gpu_virgl_hostmem_region_free hasn't been executed yet, we
+     * have marked the command to be re-processed later by setting
+     * cmd_suspended to true. The freeing callback will be called from RCU
+     * context later.
+     */
+
     if (vmr->finish_unmapping) {
+        /* 3. MemoryRegion has been freed, so finish unmapping */
         res->mr = NULL;
         g_free(vmr);
 
@@ -166,16 +191,6 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
                           __func__, strerror(-ret));
             return ret;
         }
-    } else {
-        *cmd_suspended = true;
-
-        /* render will be unblocked once MR is freed */
-        b->renderer_blocked++;
-
-        /* memory region owns self res->mr object and frees it by itself */
-        memory_region_set_enabled(mr, false);
-        memory_region_del_subregion(&b->hostmem, mr);
-        object_unparent(OBJECT(mr));
     }
 
     return 0;
Signature · 1 line
2.39.5