Skip to contentSkip to conversation

Misc fixes on vfio display

PATCH Superseded Zhenzhong Duan 2 participants 6 messages Jun 28, 2024 – Jul 1, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Misc fixes on vfio display

Hi,

This is trying to address an issue Cédric found.
See https://www.mail-archive.com/[email protected]/msg1043142.html
While looking into it, also found a potential memory leak.

I'm sorry that I didn't find how to test this fix, because it looks a GFX card is needed. Any idea on how to test or help test are quite appreciated.

Thanks
Zhenzhong

Zhenzhong Duan (2):
vfio/display: Fix potential memleak of edid info vfio/display: Fix vfio_display_edid_init() error path

 hw/vfio/display.c | 15 +++++++++------
 hw/vfio/helpers.c |  1 +
 2 files changed, 10 insertions(+), 6 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/2 vfio/display: Fix potential memleak of edid info

EDID related device region info is leaked in three paths:
1. In vfio_get_dev_region_info(), when edid info isn't find, the last device region info is leaked.
2. In vfio_display_edid_init() error path, edid info is leaked.
3. In VFIODisplay destroying path, edid info is leaked.

Fixes 08479114b0de ("vfio/display: add edid support.")
---
 hw/vfio/display.c | 2 ++
 hw/vfio/helpers.c | 1 +
 2 files changed, 3 insertions(+)
hw/vfio/display.c +2 -0
index 661e921616..5926bd6628 100644
@@ -171,6 +171,7 @@ static void vfio_display_edid_init(VFIOPCIDevice *vdev)
 
 err:
     trace_vfio_display_edid_write_error();
+    g_free(dpy->edid_info);
     g_free(dpy->edid_regs);
     dpy->edid_regs = NULL;
     return;
@@ -182,6 +183,7 @@ static void vfio_display_edid_exit(VFIODisplay *dpy)
         return;
     }
 
+    g_free(dpy->edid_info);
     g_free(dpy->edid_regs);
     g_free(dpy->edid_blob);
     timer_free(dpy->edid_link_timer);
hw/vfio/helpers.c +1 -0
index b14edd46ed..3dd32b26a4 100644
@@ -586,6 +586,7 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type,
         g_free(*info);
     }
 
+    g_free(*info);
     *info = NULL;
     return -ENODEV;
 }
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 1/2] vfio/display: Fix potential memleak of edid info

Hi

Quoted 18 lines · Zhenzhong Duan

EDID related device region info is leaked in three paths:
1. In vfio_get_dev_region_info(), when edid info isn't find, the last device region info is leaked.
2. In vfio_display_edid_init() error path, edid info is leaked.
3. In VFIODisplay destroying path, edid info is leaked.

Fixes 08479114b0de ("vfio/display: add edid support.")
---
 hw/vfio/display.c | 2 ++
 hw/vfio/helpers.c | 1 +
 2 files changed, 3 insertions(+)
hw/vfio/display.c +0 -0
index 661e921616..5926bd6628 100644

hw/vfio/display.c static void vfio_display_edid_init(VFIOPCIDevice *vdev) · line 175

Quoted 5 lines
hw/vfio/display.c +1 -0
@@ -171,6 +171,7 @@ static void vfio_display_edid_init(VFIOPCIDevice *vdev)
 
 err:
     trace_vfio_display_edid_write_error();
+    g_free(dpy->edid_info);
 

It would be better to set it to NULL.

Quoted 16 lines
hw/vfio/display.c +0 -0
     g_free(dpy->edid_regs);
     dpy->edid_regs = NULL;
     return;
hw/vfio/display.c +1 -0
@@ -182,6 +183,7 @@ static void vfio_display_edid_exit(VFIODisplay *dpy)
         return;
     }
 
+    g_free(dpy->edid_info);
     g_free(dpy->edid_regs);
     g_free(dpy->edid_blob);
     timer_free(dpy->edid_link_timer);
hw/vfio/helpers.c +0 -0
index b14edd46ed..3dd32b26a4 100644

hw/vfio/helpers.c int vfio_get_dev_region_info(VFIODevice *vbasedev, · line 591

Quoted 6 lines
hw/vfio/helpers.c +1 -0
@@ -586,6 +586,7 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev,
 uint32_t type,
         g_free(*info);
     }
 
+    g_free(*info);
 

This seems incorrect, it is freed at the end of the loop above if it didn't retun.

Quoted 8 lines
hw/vfio/helpers.c +0 -0
     *info = NULL;
     return -ENODEV;

}
--
2.34.1

Signature · 1 line
Marc-André Lureau
#3 lore raw

Re: [PATCH 1/2] vfio/display: Fix potential memleak of edid info

Hi,

Quoted 29 lines · Marc-André Lureau

Hi

On Fri, Jun 28, 2024 at 1:32 PM Zhenzhong Duan <[email protected]> wrote:

    EDID related device region info is leaked in three paths:
    1. In vfio_get_dev_region_info(), when edid info isn't find, the last
    device region info is leaked.
    2. In vfio_display_edid_init() error path, edid info is leaked.
    3. In VFIODisplay destroying path, edid info is leaked.

    Fixes: 08479114b0de ("vfio/display: add edid support.")
    Signed-off-by: Zhenzhong Duan <[email protected]>
    ---
     hw/vfio/display.c | 2 ++
     hw/vfio/helpers.c | 1 +
     2 files changed, 3 insertions(+)

    diff --git a/hw/vfio/display.c b/hw/vfio/display.c
    index 661e921616..5926bd6628 100644
    --- a/hw/vfio/display.c
    +++ b/hw/vfio/display.c
    @@ -171,6 +171,7 @@ static void
    vfio_display_edid_init(VFIOPCIDevice *vdev)

     err:
         trace_vfio_display_edid_write_error();
    +    g_free(dpy->edid_info);
Quoted 1 line

It would be better to set it to NULL.

Will do.

Quoted 25 lines
         g_free(dpy->edid_regs);
         dpy->edid_regs = NULL;
         return;
    @@ -182,6 +183,7 @@ static void vfio_display_edid_exit(VFIODisplay
    *dpy)
             return;
         }

    +    g_free(dpy->edid_info);
         g_free(dpy->edid_regs);
         g_free(dpy->edid_blob);
         timer_free(dpy->edid_link_timer);
    diff --git a/hw/vfio/helpers.c b/hw/vfio/helpers.c
    index b14edd46ed..3dd32b26a4 100644
    --- a/hw/vfio/helpers.c
    +++ b/hw/vfio/helpers.c
    @@ -586,6 +586,7 @@ int vfio_get_dev_region_info(VFIODevice
    *vbasedev, uint32_t type,
             g_free(*info);
         }

    +    g_free(*info);
Quoted 1 line

This seems incorrect, it is freed at the end of the loop above if it didn't retun.

Good catch! Will remove it.

Thanks

Zhenzhong

Quoted 12 lines
         *info = NULL;
         return -ENODEV;
     }
    -- 
    2.34.1
Signature · 1 line
Marc-André Lureau
#4 lore raw

PATCH 2/2 vfio/display: Fix vfio_display_edid_init() error path

In reply to Zhenzhong Duan

vfio_display_edid_init() can fail for many reasons and return silently. It would be good to report the error.

Old mdev driver may not support vfio edid region and we allow to go through in this case.

vfio_display_edid_update() isn't changed because it can be called at runtime when UI changes (i.e. window resize).

Fixes 08479114b0de ("vfio/display: add edid support.")
---
 hw/vfio/display.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
hw/vfio/display.c +7 -6
index 5926bd6628..462845ce69 100644
@@ -124,7 +124,7 @@ static void vfio_display_edid_ui_info(void *opaque, uint32_t idx,
     }
 }
 
-static void vfio_display_edid_init(VFIOPCIDevice *vdev)
+static bool vfio_display_edid_init(VFIOPCIDevice *vdev, Error **errp)
 {
     VFIODisplay *dpy = vdev->dpy;
     int fd = vdev->vbasedev.fd;
@@ -135,7 +135,8 @@ static void vfio_display_edid_init(VFIOPCIDevice *vdev)
                                    VFIO_REGION_SUBTYPE_GFX_EDID,
                                    &dpy->edid_info);
     if (ret) {
-        return;
+        /* Failed to get GFX edid info, allow to go through without edid. */
+        return true;
     }
 
     trace_vfio_display_edid_available();
@@ -167,14 +168,15 @@ static void vfio_display_edid_init(VFIOPCIDevice *vdev)
                                         vfio_display_edid_link_up, vdev);
 
     vfio_display_edid_update(vdev, true, 0, 0);
-    return;
+    return true;
 
 err:
+    error_setg(errp, "vfio: failed to read GFX edid field");
     trace_vfio_display_edid_write_error();
     g_free(dpy->edid_info);
     g_free(dpy->edid_regs);
     dpy->edid_regs = NULL;
-    return;
+    return false;
 }
 
 static void vfio_display_edid_exit(VFIODisplay *dpy)
@@ -367,8 +369,7 @@ static bool vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp)
             return false;
         }
     }
-    vfio_display_edid_init(vdev);
-    return true;
+    return vfio_display_edid_init(vdev, errp);
 }
 
 static void vfio_display_dmabuf_exit(VFIODisplay *dpy)
Signature · 1 line
2.34.1
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] vfio/display: Fix vfio_display_edid_init() error path

Quoted 6 lines · Zhenzhong Duan

vfio_display_edid_init() can fail for many reasons and return silently. It would be good to report the error.

Old mdev driver may not support vfio edid region and we allow to go through in this case.

Quoted 7 lines

vfio_display_edid_update() isn't changed because it can be called at runtime when UI changes (i.e. window resize).

Fixes 08479114b0de ("vfio/display: add edid support.")

---

Quoted 63 lines
 hw/vfio/display.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
hw/vfio/display.c +1 -1
index 5926bd6628..462845ce69 100644
@@ -124,7 +124,7 @@ static void vfio_display_edid_ui_info(void *opaque,
 uint32_t idx,
     }
 }
 
-static void vfio_display_edid_init(VFIOPCIDevice *vdev)
+static bool vfio_display_edid_init(VFIOPCIDevice *vdev, Error **errp)
 {
     VFIODisplay *dpy = vdev->dpy;
     int fd = vdev->vbasedev.fd;
hw/vfio/display.c +2 -1
@@ -135,7 +135,8 @@ static void vfio_display_edid_init(VFIOPCIDevice *vdev)
                                    VFIO_REGION_SUBTYPE_GFX_EDID,
                                    &dpy->edid_info);
     if (ret) {
-        return;
+        /* Failed to get GFX edid info, allow to go through without edid.
 */
+        return true;
     }
 
     trace_vfio_display_edid_available();
hw/vfio/display.c +3 -2
@@ -167,14 +168,15 @@ static void vfio_display_edid_init(VFIOPCIDevice
 *vdev)
                                         vfio_display_edid_link_up, vdev);
 
     vfio_display_edid_update(vdev, true, 0, 0);
-    return;
+    return true;
 
 err:
+    error_setg(errp, "vfio: failed to read GFX edid field");
     trace_vfio_display_edid_write_error();
     g_free(dpy->edid_info);
     g_free(dpy->edid_regs);
     dpy->edid_regs = NULL;
-    return;
+    return false;
 }
 

static void vfio_display_edid_exit(VFIODisplay *dpy)

hw/vfio/display.c +1 -2
@@ -367,8 +369,7 @@ static bool vfio_display_dmabuf_init(VFIOPCIDevice
 *vdev, Error **errp)
             return false;
         }
     }
-    vfio_display_edid_init(vdev);
-    return true;
+    return vfio_display_edid_init(vdev, errp);
 }
 

static void vfio_display_dmabuf_exit(VFIODisplay *dpy)
--
2.34.1

Signature · 1 line
Marc-André Lureau