From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796306864; b=GQg06MzBJj6pIwVansefRmx1JUlF+LZONuQz0erE7Qxnp8WM93vN9nk52C48zxv8JzX8 ErNiWeYRdqBEPVU/0m8APMqyqTotm6YXh1YrZ61rMaiJrJ8GFmsR5GhzifGfRG7n3K8HJ +exLLJmDf/A/bsOUnDT57L2sy8rrsyC2u99xfuAxubWWhGtarVE/xfxI4EKKBCT0BGYkn IaB42UdlAjYbG6dsQhjpI+KvtC5As/s/AaAdodLTApYAUUVe9mSodcEn+Bi/0TN+N17p2 hhAIFgC17IuWJ+W+SWNpWbjbA0TOsjSInpuHchg4cE5Zj9VeNxbMnpPVwUQsPoBsx4w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796306864; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=bTiHNM5leW8s9TvfngmbMET42B9ExsFKVDeU2TB0iXI=; b=wTbxxI+Zy2I7dz0ngVARzwvu1dJrFjOUVEdfq4gKmSzjwP/qovK8sy5ET+ArDrF9o7R7 6d4QrBYstS4nIkOYEeFviE7zQITappk5F0v/nTiInMIxqy2DRi2YinSpsDYkrre5HuBHw Vm6NYnYSao14NFxWBRo0LYAsthMBVSpuelYyzQZ4VYFd9eHkLPeyqZCVSRc1Pb2mAQSwL MNSuj179+na8OaIAqUKGxPbgp5AKrpjag0/G6o4hV9Y42mM7n/kWuylZtubjcAaH7P15L 2DicDGbZpSl8zxhFlpr4Aq+ulftUCxsKrue6Ha9lf8tJOOM1biUJ2yCcVAAUilheaSQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="aCdZG3/p"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B37D606A330 for ; Thu, 3 Dec 2026 14:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aCdZG3/p Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCF6B1F00896; Thu, 3 Dec 2026 14:07:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796306864; bh=bTiHNM5leW8s9TvfngmbMET42B9ExsFKVDeU2TB0iXI=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=aCdZG3/pRr+6vedJr3UUykbt3wa+3Ha+F/+GLVhnpet709XrOSr6QjDJXciRvsC6c 8lUtj2dUygvqFzu8IFfbdSTx2fTcQY5UCvIhkPclLKjwi2lUvcTQ8bCHp4oRzZjnKz 5Nqg9XEC8+c9Xv0jTS0hMpGrZM66YPiagz6FWH3UpVojNxQcQ4ZlLttB2YPa7x5p/T /2PJTJcmitd4gmkQ9VayKlz3JaggmAvLsrz7x3T7RTmbED9GjMdkNgaxpXNvsjnFBN vLFBbBvMYwBCNmioWWcU1o+Sfx7F9vtok39OPhvZ55ULyPe8TNw8XDJQosCKtui96k eWkHSQrCVYmIw== From: Maxime Ripard Date: Thu, 03 Dec 2026 15:07:23 +0100 Subject: [PATCH v13 5/9] drm/amd/display: Take over the current stream and dc_state X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20261203-drm-reset-state-flag-v13-5-b09f19f64e64@kernel.org> References: <20261203-drm-reset-state-flag-v13-0-b09f19f64e64@kernel.org> In-Reply-To: <20261203-drm-reset-state-flag-v13-0-b09f19f64e64@kernel.org> To: Maarten Lankhorst , Thomas Zimmermann , David Airlie , Simona Vetter , Louis Chauvet , Haneen Mohammed , Melissa Wen Cc: Daniel Stone , Harry Wentland , Jocelyn Falempe , =?utf-8?q?Jonas_=C3=85dahl?= , =?utf-8?q?Michel_D=C3=A4nzer?= , Pekka Paalanen , Sebastian Wick , Simon Ser , Victoria Brekenfeld , Xaver Hugl , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Maxime Ripard , Leo Li , Rodrigo Siqueira , Alex Deucher , =?utf-8?q?Christian_K=C3=B6nig?= , amd-gfx@lists.freedesktop.org X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=4875; i=mripard@kernel.org; h=from:subject:message-id; bh=jbeJTM+fmSo0QIFaCb8HKAixfrvcORMJ3Pn+gYCQUAY=; b=owGbwMvMwCmsHn9OcpHtvjLG02pJDNmC5bMjBdUsWefuv7FJ9X+WVKnHfuf9kxXOKvY61x/ef 1SG23F6x1QWBmFOBlkxRZYnMmGnl7cvrnKwX/kDZg4rE8gQBi5OAZjIr9WMDUeWOH94VvLtalLZ yXk+RS+FElmWPNHdJKqleuOlBveU3BVz9TvqF3Q0eq17ntOyKfiZLWPD6g9SMscdEv5zWH2+eY1 jU0uz7axwUfG45jfMP6wK06XK9J99c9+ovvHnl616jhdErioBAA== X-Developer-Key: i=mripard@kernel.org; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D drm_atomic_commit_fill_with_defaults() builds its commits out of states created through atomic_create_state() instead of duplicating the current state. A created state comes with no stream nor dc_state, while a duplicated state carries the ones in use, with their reference. The update passes of amdgpu_dm_atomic_check() release the stream and dc_state in use and clear the pointer of the new state, on top of the reference the current state drops when it gets destroyed. Those releases are only balanced if the new state brought a reference of its own, so a commit that disables or reconfigures a live CRTC or plane through a created state over-releases and frees the stream early, leading to a use-after-free when the current state gets destroyed. The paths that keep a CRTC or a plane enabled without a modeset also work on the stream and dc_state through the new state, and hit BUG_ON(dm_new_crtc_state->stream == NULL) since it never had one. Take the stream and dc_state of the current state over, with a reference, at the beginning of amdgpu_dm_atomic_check(), like a duplicated state would have carried them. This has to happen before the update passes clear the stream of the new states: past that point, a state whose stream was cleared can't be told apart from a created one. Signed-off-by: Maxime Ripard --- To: Harry Wentland To: Leo Li To: Rodrigo Siqueira To: Alex Deucher To: Christian König Cc: amd-gfx@lists.freedesktop.org --- drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c | 50 +++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c index 019022fba6f1..91d0ce7830f0 100644 --- a/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c +++ b/drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm.c @@ -6279,10 +6279,60 @@ STATIC_IFN_KUNIT int amdgpu_dm_atomic_check(struct drm_device *dev, struct drm_dp_mst_topology_state *mst_state; struct dsc_mst_fairness_vars vars[MAX_PIPES] = {0}; trace_amdgpu_dm_atomic_check_begin(state); + /* + * A CRTC or a plane that stays enabled keeps its stream and + * dc_state: a duplicated state carries them over, with their + * reference, but a state created through atomic_create_state() + * comes with neither. Take them over from the current state, + * with a reference, like a duplicated state would. + * + * The reference is needed when the update passes below + * reconfigure the CRTC or plane: they release the stream and + * dc_state in use and clear the pointer of the new state then, + * on top of the reference the current state drops when it gets + * destroyed, so that release is only balanced if the new state + * brought a reference of its own. The pointer is needed when + * they don't: the paths that keep a CRTC or a plane enabled + * without a modeset work on the stream and dc_state through + * the new state. + * + * A stream or a dc_state in the current state means the CRTC + * or the plane is enabled, since they are cleared when it gets + * disabled, so the conditions below already single out the + * live objects. They deliberately don't check whether this + * commit keeps them enabled on top of that: the update passes + * release the stream and dc_state of the CRTC or plane being + * disabled too, so their new state has to bring the reference + * just the same. + * + * This has to run before the update passes below clear the + * stream of the new states: past that point, a state whose + * stream was cleared can't be told apart from a created one. + */ + for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) { + dm_old_crtc_state = to_dm_crtc_state(old_crtc_state); + dm_new_crtc_state = to_dm_crtc_state(new_crtc_state); + + if (!dm_new_crtc_state->stream && dm_old_crtc_state->stream) { + dm_new_crtc_state->stream = dm_old_crtc_state->stream; + dc_stream_retain(dm_new_crtc_state->stream); + } + } + + for_each_oldnew_plane_in_state(state, plane, old_plane_state, new_plane_state, i) { + struct dm_plane_state *dm_old_plane_state = to_dm_plane_state(old_plane_state); + struct dm_plane_state *dm_new_plane_state = to_dm_plane_state(new_plane_state); + + if (!dm_new_plane_state->dc_state && dm_old_plane_state->dc_state) { + dm_new_plane_state->dc_state = dm_old_plane_state->dc_state; + dc_plane_state_retain(dm_new_plane_state->dc_state); + } + } + ret = drm_atomic_helper_check_modeset(dev, state); if (ret) { drm_dbg_atomic(dev, "drm_atomic_helper_check_modeset() failed: %pe\n", ERR_PTR(ret)); goto fail; } -- 2.56.1