From: Michael Scherle <michael.scherle@rz.uni-freiburg.de>
To: Vivek Kasireddy <vivek.kasireddy@intel.com>, qemu-devel@nongnu.org
Cc: "Gerd Hoffmann" <kraxel@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Dmitry Osipenko" <dmitry.osipenko@collabora.com>,
"Frediano Ziglio" <freddy77@gmail.com>,
"Dongwon Kim" <dongwon.kim@intel.com>
Subject: [PATCH v4 5/7] ui/console-gl: Add a helper to create a texture with linear memory layout
Date: Mon, 26 May 2025 15:06:44 +0200 [thread overview]
Message-ID: <7dba8ee4-3af2-4bdb-9edb-df49fea1e842@rz.uni-freiburg.de> (raw)
In-Reply-To: <20250515024734.758335-6-vivek.kasireddy@intel.com>
Great to see this patch making progress.
I've tested it extensively, and unfortunately, I’ve noticed a memory
leak in surface_gl_create_texture_from_fd(). The memory leak is hard to
see since the memory is owned by the gpu driver.
On Intel hardware, it's possible to observe the leak using:
cat /sys/module/i915/refcnt
or
xpu-smi ps
In on of my use case—which involves frequent scanout disable/enable
cycles—the leak is quite apparent. However, in more typical scenarios,
it might be difficult to catch.
The issue stems from the mem_obj not being deleted after use. I’ve put
together a minimal modification to address it:
On 15.05.25 04:45, Vivek Kasireddy wrote:
> There are cases where we do not want the memory layout of a texture to
> be tiled as the component processing the texture would not know how to
> de-tile either via software or hardware. Therefore, ensuring that the
> memory backing the texture has a linear layout is absolutely necessary
> in these situations.
>
> Cc: Gerd Hoffmann <kraxel@redhat.com>
> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
> Cc: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> Cc: Frediano Ziglio <freddy77@gmail.com>
> Cc: Dongwon Kim <dongwon.kim@intel.com>
> Signed-off-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> ---
> include/ui/console.h | 2 ++
> ui/console-gl.c | 32 ++++++++++++++++++++++++++++++++
> 2 files changed, 34 insertions(+)
>
> diff --git a/include/ui/console.h b/include/ui/console.h
> index 46b3128185..5cfa6ae215 100644
> --- a/include/ui/console.h
> +++ b/include/ui/console.h
> @@ -422,6 +422,8 @@ bool console_gl_check_format(DisplayChangeListener *dcl,
> pixman_format_code_t format);
> void surface_gl_create_texture(QemuGLShader *gls,
> DisplaySurface *surface);
> +bool surface_gl_create_texture_from_fd(DisplaySurface *surface,
> + int fd, GLuint *texture);
> void surface_gl_update_texture(QemuGLShader *gls,
> DisplaySurface *surface,
> int x, int y, int w, int h);
> diff --git a/ui/console-gl.c b/ui/console-gl.c
> index 103b954017..97f7989651 100644
> --- a/ui/console-gl.c
> +++ b/ui/console-gl.c
> @@ -25,6 +25,7 @@
> * THE SOFTWARE.
> */
> #include "qemu/osdep.h"
> +#include "qemu/error-report.h"
> #include "ui/console.h"
> #include "ui/shader.h"
>
> @@ -96,6 +97,37 @@ void surface_gl_create_texture(QemuGLShader *gls,
> glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
> }
>
> +bool surface_gl_create_texture_from_fd(DisplaySurface *surface,
> + int fd, GLuint *texture)
> +{
> + unsigned long size = surface_stride(surface) * surface_height(surface);
> + GLenum err = glGetError();
> + GLuint mem_obj;
+ GLuint mem_obj = 0;
> +
> + if (!epoxy_has_gl_extension("GL_EXT_memory_object") ||
> + !epoxy_has_gl_extension("GL_EXT_memory_object_fd")) {
> + return false;
> + }
> +
> +#ifdef GL_EXT_memory_object_fd
> + glCreateMemoryObjectsEXT(1, &mem_obj);
> + glImportMemoryFdEXT(mem_obj, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
> +
> + err = glGetError();
> + if (err != GL_NO_ERROR) {
+ if (mem_obj) {
+ glDeleteMemoryObjectsEXT(1, &mem_obj);
+ }
> + error_report("spice: cannot import memory object from fd");
> + return false;
> + }
> +
> + glGenTextures(1, texture);
> + glBindTexture(GL_TEXTURE_2D, *texture);
> + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT, GL_LINEAR_TILING_EXT);
> + glTexStorageMem2DEXT(GL_TEXTURE_2D, 1, GL_RGBA8, surface_width(surface),
> + surface_height(surface), mem_obj, 0);
> +#endif
+ glDeleteMemoryObjectsEXT(1, &mem_obj);
> + return *texture > 0 && glGetError() == GL_NO_ERROR;
> +}
> +
> void surface_gl_update_texture(QemuGLShader *gls,
> DisplaySurface *surface,
> int x, int y, int w, int h)
That said, my OpenGL knowledge is somewhat limited, and the
documentation wasn’t entirely clear to me on whether deleting the memory
object while the texture is still being used, is always safe. Based on a
quick look at the iris and llvmpipe implementations, it appears to be
acceptable.
If that's not the case, an alternative fix could follow this approach
instead:
https://gitlab.uni-freiburg.de/opensourcevdi/qemu/-/commit/4ca806871c141089be16af25c1820d3e04f3e27d
Greetings Michael
next prev parent reply other threads:[~2025-05-26 14:36 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-15 2:45 [PATCH v4 0/7] ui/spice: Enable gl=on option for non-local or remote clients Vivek Kasireddy
2025-05-15 2:45 ` [PATCH v4 1/7] ui/egl-helpers: Error check the fds in egl_dmabuf_export_texture() Vivek Kasireddy
2025-05-15 2:45 ` [PATCH v4 2/7] ui/spice: Add an option for users to provide a preferred codec Vivek Kasireddy
2025-05-15 2:45 ` [PATCH v4 3/7] ui/spice: Enable gl=on option for non-local or remote clients Vivek Kasireddy
2025-05-24 14:44 ` Marc-André Lureau
2025-05-15 2:45 ` [PATCH v4 4/7] ui/spice: Add an option to submit gl_draw requests at fixed rate Vivek Kasireddy
2025-05-24 14:47 ` Marc-André Lureau
2025-05-24 14:57 ` Marc-André Lureau
2025-05-27 4:22 ` Kasireddy, Vivek
2025-05-15 2:45 ` [PATCH v4 5/7] ui/console-gl: Add a helper to create a texture with linear memory layout Vivek Kasireddy
2025-05-25 2:44 ` Dmitry Osipenko
2025-05-26 13:06 ` Michael Scherle [this message]
2025-05-26 15:16 ` Michael Scherle
2025-05-27 4:20 ` Kasireddy, Vivek
2025-05-27 13:45 ` Michael Scherle
2025-05-15 2:45 ` [PATCH v4 6/7] ui/spice: Create a new texture with linear layout when gl=on is enabled Vivek Kasireddy
2025-05-15 2:45 ` [PATCH v4 7/7] ui/spice: Blit the scanout texture if its memory layout is not linear Vivek Kasireddy
2025-05-24 14:43 ` [PATCH v4 0/7] ui/spice: Enable gl=on option for non-local or remote clients Marc-André Lureau
2025-05-27 4:18 ` Kasireddy, Vivek
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=7dba8ee4-3af2-4bdb-9edb-df49fea1e842@rz.uni-freiburg.de \
--to=michael.scherle@rz.uni-freiburg.de \
--cc=dmitry.osipenko@collabora.com \
--cc=dongwon.kim@intel.com \
--cc=freddy77@gmail.com \
--cc=kraxel@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=vivek.kasireddy@intel.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 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).