Re: [PATCH v2] drm/nouveau: Fix memory leaks in debugfs and hwmon init error paths
From: lyude
Date: Thu Oct 08 2026 - 17:45:57 EST
One nit-pick below:
On Tue, 2026-09-22 at 12:40 +0800, liupeng wrote:
> In nouveau_debugfs_init(), if nvif_object_ctor() fails, the
> previously
> allocated drm->debugfs is leaked because the function returns the
> error code directly.
>
> In nouveau_hwmon_init(), if hwmon_device_register_with_info() fails,
> the allocated hwmon structure is leaked because the function returns
> the error code directly.
>
> Fix both by freeing the allocated memory and clearing the pointer on
> the error paths.
>
> Fixes: b126a200e9db ("drm/nouveau/debugfs: we need a ctrl object for
> debugfs")
> Fixes: b9ed919f1c8f ("drm/nouveau/drm/pm: remove everything except
> the hwmon interfaces to THERM")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: liupeng <liupeng01@xxxxxxxxxx>
> ---
> Changes in v2:
> - Add Fixes: tags for the commits that introduced the leaks
> - Add Cc: stable@xxxxxxxxxxxxxxx for stable backporting
>
> drivers/gpu/drm/nouveau/nouveau_debugfs.c | 15 ++++++++++++---
> drivers/gpu/drm/nouveau/nouveau_hwmon.c | 2 ++
> 2 files changed, 14 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
> b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
> index 47d5579c568d..88223931f382 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_debugfs.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_debugfs.c
> @@ -295,13 +295,22 @@ nouveau_drm_debugfs_init(struct drm_minor
> *minor)
> int
> nouveau_debugfs_init(struct nouveau_drm *drm)
> {
> + int ret;
> +
> drm->debugfs = kzalloc_obj(*drm->debugfs);
> if (!drm->debugfs)
> return -ENOMEM;
>
> - return nvif_object_ctor(&drm->client.device.object,
> "debugfsCtrl", 0,
> - NVIF_CLASS_CONTROL, NULL, 0,
> - &drm->debugfs->ctrl);
> + ret = nvif_object_ctor(&drm->client.device.object,
> "debugfsCtrl", 0,
> + NVIF_CLASS_CONTROL, NULL, 0,
> + &drm->debugfs->ctrl);
> + if (ret) {
> + kfree(drm->debugfs);
> + drm->debugfs = NULL;
> + return ret;
^ We could get rid of this extra return…
> + }
> +
> + return 0;
…and just return ret here
With that fixed:
Reviewed-by: Lyude Paul <lyude@xxxxxxxxxx>
> }
>
> void
> diff --git a/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> index 726397ab035d..ffbe7f542ab0 100644
> --- a/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> +++ b/drivers/gpu/drm/nouveau/nouveau_hwmon.c
> @@ -697,6 +697,8 @@ nouveau_hwmon_init(struct drm_device *dev)
> if (IS_ERR(hwmon_dev)) {
> ret = PTR_ERR(hwmon_dev);
> NV_ERROR(drm, "Unable to register hwmon device:
> %d\n", ret);
> + drm->hwmon = NULL;
> + kfree(hwmon);
> return ret;
> }
>