mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thomas Zimmermann <tzimmermann@suse.de>
To: Dinghao Liu <dinghao.liu@zju.edu.cn>
Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org,
	Maxime Ripard <mripard@kernel.org>
Subject: Re: [PATCH] drm/plane: fix error handling in __drm_universal_plane_init
Date: Wed, 6 Dec 2023 09:22:04 +0100	[thread overview]
Message-ID: <77925c2e-8cb2-473b-9dd0-3ce60a909066@suse.de> (raw)
In-Reply-To: <20231206072701.13276-1-dinghao.liu@zju.edu.cn>


[-- Attachment #1.1: Type: text/plain, Size: 2547 bytes --]

Hi

Am 06.12.23 um 08:27 schrieb Dinghao Liu:
> __drm_universal_plane_init() frees plane->format_types and
> plane->modifiers on failure. However, sometimes its callers
> will free these two pointers again, which may lead to a
> double-free. One possible call chain is:
> 
> mdp5_plane_init
>    |-> drm_universal_plane_init
>    |     |-> __drm_universal_plane_init (first free)
>    |
>    |-> mdp5_plane_destroy
>          |-> drm_plane_cleanup (second free)
> 
> Fix this by setting the two pointers to NULL after kfree.
> 
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>

Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de>

to get the immediate bug fixed.

However, I don't think it's the correct way of doing things in general. 
Planes should probably not attempt to even make a copy, but use the 
supplied pointers. Lifetime of the arrays is the same as of the plane. 
That's for a different patch set, of course.  (I did not review the DRM 
code whether the internal copy is required.)

For now, maybe drm_plane_cleanup() could warn if format_types equals 
NULL. [1] It indicates that the plane has not been initialized correctly 
and the driver's memory lifetime handling is somehow broken.

Best regards
Thomas

[1] 
https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/drm_plane.c#L542

> ---
>   drivers/gpu/drm/drm_plane.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/gpu/drm/drm_plane.c b/drivers/gpu/drm/drm_plane.c
> index 24e7998d1731..1331b8224920 100644
> --- a/drivers/gpu/drm/drm_plane.c
> +++ b/drivers/gpu/drm/drm_plane.c
> @@ -304,6 +304,7 @@ static int __drm_universal_plane_init(struct drm_device *dev,
>   	if (format_modifier_count && !plane->modifiers) {
>   		DRM_DEBUG_KMS("out of memory when allocating plane\n");
>   		kfree(plane->format_types);
> +		plane->format_types = NULL;
>   		drm_mode_object_unregister(dev, &plane->base);
>   		return -ENOMEM;
>   	}
> @@ -317,6 +318,8 @@ static int __drm_universal_plane_init(struct drm_device *dev,
>   	if (!plane->name) {
>   		kfree(plane->format_types);
>   		kfree(plane->modifiers);
> +		plane->format_types = NULL;
> +		plane->modifiers = NULL;
>   		drm_mode_object_unregister(dev, &plane->base);
>   		return -ENOMEM;
>   	}

-- 
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

  reply	other threads:[~2023-12-06  8:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-06  7:27 Dinghao Liu
2023-12-06  8:22 ` Thomas Zimmermann [this message]
2023-12-06  9:09   ` Dmitry Baryshkov
2023-12-06  9:32     ` Thomas Zimmermann

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=77925c2e-8cb2-473b-9dd0-3ce60a909066@suse.de \
    --to=tzimmermann@suse.de \
    --cc=dinghao.liu@zju.edu.cn \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mripard@kernel.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

all inboxes | Powered by JetHome®