From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2ED783793BB for ; Thu, 23 Apr 2026 10:18:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776939538; cv=none; b=rFhzAr8kYZP9rKovm9E9zs12juJWRHYNzzigD3mY2iircAnVWtcQgZOixzIp160c1xwbBo4HeFWCg6HDVa6GW83depVpCNUQ+iDpTD1rLDKFzHLPsdU0YYWM7xJ87KL6txjE2D5vwJD3vvwhwcDyqaiyNddTsJ+SrT7+oedY5XQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776939538; c=relaxed/simple; bh=XLthABTvu6wZbcVZCsOBaQZVrcObStJws8bV4+2Va+Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lxRs7ENkUoCyn3PHGnxmMOilIp0Bh0wZmC9f2R5+5nf3OvcZB6XQYj0pxV99hEF6XnQ72MofIoY+nVBi1Y7F61am0B7IxcsaG7lEU2QRT+zQ3ERq15bqwngf17WfsGtDGkW1DI1NoLd1d029xWf3/OIqltd6LpLGIcUGsu1PVrw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eXcF9Y06; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="eXcF9Y06" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 76641C2BCAF; Thu, 23 Apr 2026 10:18:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776939537; bh=XLthABTvu6wZbcVZCsOBaQZVrcObStJws8bV4+2Va+Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=eXcF9Y06ANgUBztd5+iSgGwBKzTQ2LH+RO8xMP6OPb9BfWcOREMwOpeoy2cm+ps3i uNlRrgb2SSOGzYijYwwUTHyyCslPAaqYahWZWFSeT8RGs2oMrnDizTKE9Hg3pUKxTj EGDJip1l5K2TScr3dvdvP0M7/cI8OAFGtLwDlWmXyBt20/4CPwDq71IGLhi8cDAYW7 ACEncZI32EtRgoUTIZtmSKq6uCtbZNM9jTG8wrJuExHRUjZTM1Iar3+eX6zNtqTMvo gGYVzJGjIjAlG0bBhwUf/+LmM1rrfl3YIKBm1BAHQq3IvCTlSGLzZvG28MVMdEnqr7 Kb0cghSqn4W5A== From: Maxime Ripard Date: Thu, 23 Apr 2026 12:18:22 +0200 Subject: [PATCH v2 09/28] drm/atomic: Only call atomic_destroy_state on a !NULL pointer Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260423-drm-state-readout-v2-9-8549f87cb978@kernel.org> References: <20260423-drm-state-readout-v2-0-8549f87cb978@kernel.org> In-Reply-To: <20260423-drm-state-readout-v2-0-8549f87cb978@kernel.org> To: Maarten Lankhorst , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec , Jyri Sarha , Tomi Valkeinen Cc: Devarsh Thakkar , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Maxime Ripard , Laurent Pinchart X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=3793; i=mripard@kernel.org; h=from:subject:message-id; bh=XLthABTvu6wZbcVZCsOBaQZVrcObStJws8bV4+2Va+Y=; b=owGbwMvMwCmsHn9OcpHtvjLG02pJDJkvP75mjLL+2ncmS4XhfcLk/KMb3LPTO80OCrBwKRTP2 tcwX7m5YyoLgzAng6yYIssTmbDTy9sXVznYr/wBM4eVCWQIAxenAExkRQFjw+vydayzHvb/7BVd OSFjxcIG1xp1G/45Nhm/r76Lijq0o00l2shgf8Pe/5FlB3bMT7BiYGyYrldsWWeY1XJ5R4G2OeO KkyGMrXcLj69fvDSCIX/hig9/2aSv+s17c/egU8/EPD75DR8A X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D The drm_atomic_state structure is freed through the drm_atomic_state_put() function, that eventually calls drm_atomic_state_default_clear() by default when there's no active users of that state. It then iterates over all objects with a state, and will call the atomic_destroy_state callback on the state pointer. The state pointer is mostly used these days to point to which of the old or new state needs to be freed, depending on whether the state was committed or not. So it all makes sense. However, with the hardware state readout support approaching, we might have a state, with multiple objects in it, but no state to free because we want them to persist. In such a case, the state pointer is going to be NULL, and thus we'll end up with NULL pointer dereference. Test if the state pointer is non-NULL before calling atomic_destroy_state on it. Reviewed-by: Laurent Pinchart Reviewed-by: Thomas Zimmermann Signed-off-by: Maxime Ripard --- drivers/gpu/drm/drm_atomic.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/drm_atomic.c b/drivers/gpu/drm/drm_atomic.c index c714a6e6e9ae..6449a4fd4ae0 100644 --- a/drivers/gpu/drm/drm_atomic.c +++ b/drivers/gpu/drm/drm_atomic.c @@ -274,12 +274,14 @@ void drm_atomic_state_default_clear(struct drm_atomic_state *state) struct drm_connector *connector = state->connectors[i].ptr; if (!connector) continue; - connector->funcs->atomic_destroy_state(connector, - state->connectors[i].state_to_destroy); + if (state->connectors[i].state_to_destroy) + connector->funcs->atomic_destroy_state(connector, + state->connectors[i].state_to_destroy); + state->connectors[i].ptr = NULL; state->connectors[i].state_to_destroy = NULL; state->connectors[i].old_state = NULL; state->connectors[i].new_state = NULL; drm_connector_put(connector); @@ -289,12 +291,13 @@ void drm_atomic_state_default_clear(struct drm_atomic_state *state) struct drm_crtc *crtc = state->crtcs[i].ptr; if (!crtc) continue; - crtc->funcs->atomic_destroy_state(crtc, - state->crtcs[i].state_to_destroy); + if (state->crtcs[i].state_to_destroy) + crtc->funcs->atomic_destroy_state(crtc, + state->crtcs[i].state_to_destroy); state->crtcs[i].ptr = NULL; state->crtcs[i].state_to_destroy = NULL; state->crtcs[i].old_state = NULL; state->crtcs[i].new_state = NULL; @@ -309,12 +312,14 @@ void drm_atomic_state_default_clear(struct drm_atomic_state *state) struct drm_plane *plane = state->planes[i].ptr; if (!plane) continue; - plane->funcs->atomic_destroy_state(plane, - state->planes[i].state_to_destroy); + if (state->planes[i].state_to_destroy) + plane->funcs->atomic_destroy_state(plane, + state->planes[i].state_to_destroy); + state->planes[i].ptr = NULL; state->planes[i].state_to_destroy = NULL; state->planes[i].old_state = NULL; state->planes[i].new_state = NULL; } @@ -337,12 +342,14 @@ void drm_atomic_state_default_clear(struct drm_atomic_state *state) struct drm_private_obj *obj = state->private_objs[i].ptr; if (!obj) continue; - obj->funcs->atomic_destroy_state(obj, - state->private_objs[i].state_to_destroy); + if (state->private_objs[i].state_to_destroy) + obj->funcs->atomic_destroy_state(obj, + state->private_objs[i].state_to_destroy); + state->private_objs[i].ptr = NULL; state->private_objs[i].state_to_destroy = NULL; state->private_objs[i].old_state = NULL; state->private_objs[i].new_state = NULL; } -- 2.53.0