Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp842592rdb; Wed, 6 Dec 2023 01:09:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IF6SAGuu/PWYHraVWbAZ6ok3FQU9ryngoULQrMbDbxh6QNo5bJiYxOajLn9rZ5oX7n5JtUZ X-Received: by 2002:a17:90b:30d7:b0:286:f3d8:de2a with SMTP id hi23-20020a17090b30d700b00286f3d8de2amr2287123pjb.45.1701853791803; Wed, 06 Dec 2023 01:09:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701853791; cv=none; d=google.com; s=arc-20160816; b=IUFXIGKApH8se4JG9RaKF1LWO7Udj58LyNW3J9/RfAooDuXxUsVOus5vY18UsQvYMf BjTDkODLobtTATt8Ji/4D1+x3qlW0Axk+qcTdfGgl20h8IiMJ/TW5qFhsMO5LLEqG+Yw h6wS61opmI0OExw/oHzkN2RL3H8gC5xrJr7xLExEm2QnpEFiONJAgL2JqAXII0YiOy5h LxNyi+/PWEDGngwuWtKbpSw4zGvk+XamUIj+Qhh9DyEvqu6esdzyvHfjzEwbvvUP2ncT RJNj0eO+QbRh2J3wES3SFyyWUUdIfzyca8+q9IABLeDL9hy+tn9YRIGIn2CvruqC827x 6a8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=wP++sDZ0D6RnqxEDsIfOfMZaHshu8C2nXxTEhtKdeFg=; fh=KqA3KQxqsV7j1lDFI2zoNeebJBId0BVG0sFxvgNA1po=; b=i11pGZEcY+o1WiQD/w3q5ufrGBEhe45fSDDpgcjUjEA59UEoftp95dZLvBf6GULhQG HRUAc6HQe/4iHvxGR4itxqlQAjD3HbIaPGH/0NO/ekVv5actffrwHsZv7EZbFDSlEvi3 RybrBLuGIPJKKAZJ6ojWJ4I+8tud9JFJW8qzmI741grAE89SgQheJNYDm/xAnqlblEtw CxWyURy7m3YC3YrwW6Rgk9wlRDBCDFquavUWGGH3a3MKqcBiGIfxiUWdU5dp0sDWtSNS pxvgBU8biWAi+y0Pc7AdUQKi/KXEVQdT9D9aG0RajZeCSRmOMIZhvXGJVdy18U/Y40mi b4bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XGaGB5S0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id f8-20020a17090ace0800b00285864ca8a0si2425121pju.180.2023.12.06.01.09.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 01:09:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=XGaGB5S0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id C6A818042387; Wed, 6 Dec 2023 01:09:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346709AbjLFJJc (ORCPT + 99 others); Wed, 6 Dec 2023 04:09:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346664AbjLFJJc (ORCPT ); Wed, 6 Dec 2023 04:09:32 -0500 Received: from mail-yb1-xb36.google.com (mail-yb1-xb36.google.com [IPv6:2607:f8b0:4864:20::b36]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5F97D71 for ; Wed, 6 Dec 2023 01:09:37 -0800 (PST) Received: by mail-yb1-xb36.google.com with SMTP id 3f1490d57ef6-db539f21712so594677276.1 for ; Wed, 06 Dec 2023 01:09:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1701853777; x=1702458577; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=wP++sDZ0D6RnqxEDsIfOfMZaHshu8C2nXxTEhtKdeFg=; b=XGaGB5S0ho5Kv2fj6FKTKgltohzZKE5geOFxJbJyuOp53bjxzUXqHJJJYUSz9rT9ef iDVeWbUY+VzG1H9NKEY5ikT+wVqqWvooxRElK2rdfniJsceqN8MgATPDFE25rQzCudlY getlh8t+nNOQG6rAfu+xMWkqN+KDBF0rVT60v25JXwDtCNTfnIKJ1vtNuEauPcMT9H16 iN6zuCQoGZ0gQD42UDOKZyMhr3/nLQ/mZGKYOPWnzW//YSDidSK44Eq8cQZ/8rtqH3mL IsZwePz4woPgYUGOKxWfdmeS9bGKDcadfOKBo6jvhr70C4NtNAaxRu/zT5QTQNkK8FuK RGmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701853777; x=1702458577; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wP++sDZ0D6RnqxEDsIfOfMZaHshu8C2nXxTEhtKdeFg=; b=qNstQH40Bh1/N0DX7kEPDeCwypemmjlIjxAVqhRe8o2uniVGa7cA/TmYB3K8DcyK6G Lr2yb7bZLhUjE1APJoQ+QJzON/cY3MoDWAT2S2IuSO+zUpYJu25Wwx5Xnt+b10Wn60Ac gfsBRB23PX8ytFN2h0EjkNOskTVfIxUWlrOk+AQCYMgR7Q00STeo6dGSts7MTGMhKSLc AowLd6ZNAXQ5c1BjFPWJr1Uvd/kqg4kSbOk5CNAtH+WcoBRDqb/ODePtnQgPuyKz/ml9 yguVr984uEWZ6cyGJiIeSkKwbPwmBY6EWns6whSBqPis0waKvBVj35rzw6cE17B43XMg Ubhg== X-Gm-Message-State: AOJu0YxeeFfJ6gNyc/IvZo/8py7M/NXg94/cGJlQBlo79j4SmrZgESRs qiQzcshlydNjCZhiuU3yAK1pQ1qUwzzzj6zy2GSOdQ== X-Received: by 2002:a05:6902:4e1:b0:db9:85c7:b052 with SMTP id w1-20020a05690204e100b00db985c7b052mr1761994ybs.9.1701853776943; Wed, 06 Dec 2023 01:09:36 -0800 (PST) MIME-Version: 1.0 References: <20231206072701.13276-1-dinghao.liu@zju.edu.cn> <77925c2e-8cb2-473b-9dd0-3ce60a909066@suse.de> In-Reply-To: <77925c2e-8cb2-473b-9dd0-3ce60a909066@suse.de> From: Dmitry Baryshkov Date: Wed, 6 Dec 2023 11:09:25 +0200 Message-ID: Subject: Re: [PATCH] drm/plane: fix error handling in __drm_universal_plane_init To: Thomas Zimmermann Cc: Dinghao Liu , Maxime Ripard , linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Wed, 06 Dec 2023 01:09:48 -0800 (PST) On Wed, 6 Dec 2023 at 10:22, Thomas Zimmermann wrote: > > 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 > > Reviewed-by: Thomas Zimmermann > > 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.) But there is no internal copy. The issue is in the mdp5 code calling drm_plane_cleanup (indirectly) even though the plane init has failed. > > 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) -- With best wishes Dmitry