From: "Michael S. Tsirkin" <mst@redhat.com>
To: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Cc: qemu-devel@nongnu.org, "Alex Bennée" <alex.bennee@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@linaro.org>,
"Richard Henderson" <richard.henderson@linaro.org>
Subject: Re: [PATCH v1 1/2] virtio-gpu: fix hang under TCG when unmapping blob
Date: Thu, 3 Apr 2025 09:08:29 -0400 [thread overview]
Message-ID: <20250403090717-mutt-send-email-mst@kernel.org> (raw)
In-Reply-To: <20250403121704.2754589-2-manos.pitsidianakis@linaro.org>
On Thu, Apr 03, 2025 at 03:17:01PM +0300, Manos Pitsidianakis wrote:
> 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
> has no other interrupts that 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 self-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.
>
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> hw/display/virtio-gpu-virgl.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/hw/display/virtio-gpu-virgl.c b/hw/display/virtio-gpu-virgl.c
> index 145a0b3879..ad600ccc9c 100644
> --- a/hw/display/virtio-gpu-virgl.c
> +++ b/hw/display/virtio-gpu-virgl.c
> @@ -120,7 +120,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
> vmr->g = g;
>
> mr = &vmr->mr;
> - 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);
>
> --
> γαῖα πυρί μιχθήτω
next prev parent reply other threads:[~2025-04-03 13:08 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-03 12:17 [PATCH v1 0/2] virtio-gpu: fix blob unmapping sequence Manos Pitsidianakis
2025-04-03 12:17 ` [PATCH v1 1/2] virtio-gpu: fix hang under TCG when unmapping blob Manos Pitsidianakis
2025-04-03 13:08 ` Michael S. Tsirkin [this message]
2025-04-03 12:17 ` [PATCH v1 2/2] virtio-gpu: refactor async blob unmapping Manos Pitsidianakis
2025-04-09 10:32 ` [PATCH v1 0/2] virtio-gpu: fix blob unmapping sequence 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=20250403090717-mutt-send-email-mst@kernel.org \
--to=mst@redhat.com \
--cc=alex.bennee@linaro.org \
--cc=manos.pitsidianakis@linaro.org \
--cc=philmd@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
/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).