From nobody Sun Feb 8 02:20:51 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 96C7A34F498 for ; Tue, 13 Jan 2026 18:47:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330051; cv=none; b=eI4vwrzmbPwxC55seHcfhhdwoAPgehqBWPq9nJj7OpyP4VYbQUiRoSX9tXZvr7T5U8SkqUfaZCiYmO0SjyU/0OpJguxlHJyzYXx9L5AJUH1hXHV9bHTAeb19fkwEYAbgWhd8TJvvYu3gpnEUuRytgYtuH7gV9aCNmLOsHLufpJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330051; c=relaxed/simple; bh=Ik1Ta8asSSx5KKFD5HidAoDfhg86wbj6pxNUwca2YpY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=T83Kor2yraiTw0w7LmZ/7OR+QBdXtUCbp9tHXHR0ptizPWZbrpnjj23tOuAOCDiYHXvT1T8dovhbEwVoQjfEnih2pfFKMfjwOrMTATa+ii2NemCSYv5P4xna5vbVEyEGw3s005v3PcrBbs3zX7lmLxzSxafCaX1xMsrgIbDO2js= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=jEYYIe6W; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="jEYYIe6W" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id 094051A283D; Tue, 13 Jan 2026 18:47:27 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id D42F460751; Tue, 13 Jan 2026 18:47:26 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 4CE8F103C8707; Tue, 13 Jan 2026 19:47:24 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1768330045; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=DHAxbpmXFQ1iq3WpgZRrDbJUoPE1BIRMc7j1mDaZ/Xo=; b=jEYYIe6WSmwNsjP9/Dg27QouOnhnca9alwgHt9c2BpWU82Id3xDRi9r9rSdZi+kd9JmVGL 88Q5KT0n08kRx8bouqBqJf+7pnu1aoYlQmx9e7zF3ykJnZveDi6bw22ZzmD2E9bWBJ6Mct vAy1hpUirXKIkqQN+X2D6VuTzpkWtUprKbAX3YT0jcQo+F7D9YYh8fdECrhMPN4hHR4erv BaqM82/+sQFXZaPffwkTRtYRgA29zns8yf0Y67OV6OVmYqFkMmc4eD2qowHmtBi6zr8C1i Zm9D8u5XX5A3elIc4guJeKPmKVRiWZAk0J4m8F0E6kJEDWBXIOZywFC/3AAlzA== From: Luca Ceresoli Date: Tue, 13 Jan 2026 19:47:12 +0100 Subject: [PATCH RESEND v4 1/7] drm/encoder: add mutex to protect the bridge chain 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: quoted-printable Message-Id: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-1-60f3135adc45@bootlin.com> References: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> In-Reply-To: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Ian Ray X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 The per-encoder bridge chain is currently assumed to be static once it is fully initialized. Work is in progress to add hot-pluggable bridges, breaking that assumption. With bridge removal, the encoder chain can change without notice, removing tail bridges. This can be problematic while iterating over the chain. Add a mutex to be taken whenever looping or changing the encoder chain. Reviewed-by: Maxime Ripard Signed-off-by: Luca Ceresoli --- Changes in v3: - Removed the drm_encoder_chain_[un]lock() wrappers Changes in v2: - Added documentation to new APIs --- drivers/gpu/drm/drm_encoder.c | 2 ++ include/drm/drm_encoder.h | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/drivers/gpu/drm/drm_encoder.c b/drivers/gpu/drm/drm_encoder.c index 8f2bc6a28482..3261f142baea 100644 --- a/drivers/gpu/drm/drm_encoder.c +++ b/drivers/gpu/drm/drm_encoder.c @@ -129,6 +129,7 @@ static int __drm_encoder_init(struct drm_device *dev, } =20 INIT_LIST_HEAD(&encoder->bridge_chain); + mutex_init(&encoder->bridge_chain_mutex); list_add_tail(&encoder->head, &dev->mode_config.encoder_list); encoder->index =3D dev->mode_config.num_encoder++; =20 @@ -202,6 +203,7 @@ void drm_encoder_cleanup(struct drm_encoder *encoder) kfree(encoder->name); list_del(&encoder->head); dev->mode_config.num_encoder--; + mutex_destroy(&encoder->bridge_chain_mutex); =20 memset(encoder, 0, sizeof(*encoder)); } diff --git a/include/drm/drm_encoder.h b/include/drm/drm_encoder.h index 977a9381c8ba..eded7c34481a 100644 --- a/include/drm/drm_encoder.h +++ b/include/drm/drm_encoder.h @@ -25,6 +25,7 @@ =20 #include #include +#include #include #include #include @@ -189,6 +190,9 @@ struct drm_encoder { */ struct list_head bridge_chain; =20 + /** @bridge_chain_mutex: protect bridge_chain from changes while iteratin= g */ + struct mutex bridge_chain_mutex; + const struct drm_encoder_funcs *funcs; const struct drm_encoder_helper_funcs *helper_private; =20 --=20 2.52.0 From nobody Sun Feb 8 02:20:51 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 8D91234FF59 for ; Tue, 13 Jan 2026 18:47:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330052; cv=none; b=GqX2ie1amUd/6o10mTilNK1llkO58TGUHp1FTJ8EVK+dFBbxci1QEc+trKbli4qI1fTFbpdRw+F8eZ4UP8tx0Vhg7QF6TXj90qXX/j4iDSlTfONmU0tYqyT9qq8Zd7h8GxxkoZ4042bk4zqUbRbrIxnUqmPIX6Xar+0Y1dXusVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330052; c=relaxed/simple; bh=KE/+rROF05DftQSps4R73csP0TOKs9E8qp3VYJxGvj8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QAPTr305feVIzhlH13Qf07ETtsvtMT8OzKmSQFkgspmU9z2W1ng/jUA0oxROY6KUH0B6BOgFaOzWWwhtKZ4qhrvg0WugPgB0veSNI00bMOuD6faKwgoFMw9p6t6eg1GrAr3nf3HjoB79lO982QzGzfzg1LZe6ELiB8wwUUewQ9I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=le2fpYE6; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="le2fpYE6" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id 407CD1A283E; Tue, 13 Jan 2026 18:47:29 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 16A7760759; Tue, 13 Jan 2026 18:47:29 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 3BE64103C870E; Tue, 13 Jan 2026 19:47:26 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1768330048; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=xNVFKbXxe/2KeAacT5Ngu5k5lYzr8lc24av0uJTm25o=; b=le2fpYE6zZpvjbTNugcj8Ci6n2G+BVE+M93t4+Q+3YeR7+xz1xUU7T0xtXvmOR5aQKTEfT Lr2V7X26s4FoECuphpHYhkfhErRfcrWi4HSPkZFuvDNW+LnAVR7mzztSaXYRoNLSAZNy+u mAEdeRH26y6QuW3aGmUnhi0IRLDdVS2L2qTh/OhCtegMWMd896lTbqZwsBt/4bHaLprBCR arvYEoRVvl/8b+xNkzF0t7H7T6ZnGxe8GNwdpKe/x9JOpWLanSD2NNEdbExQD9VCBbbiws 6KsNmXO1rPQQuq/PkuoDOLxKS29cI3x+Ao16ucOMmMmP6rG/NOGduhGVH0FfTQ== From: Luca Ceresoli Date: Tue, 13 Jan 2026 19:47:13 +0100 Subject: [PATCH RESEND v4 2/7] drm/encoder: drm_encoder_cleanup: lock the encoder chain mutex during removal 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: quoted-printable Message-Id: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-2-60f3135adc45@bootlin.com> References: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> In-Reply-To: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Ian Ray X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 drm_encoder_cleanup() modifies the encoder chain by removing bridges via drm_bridge_detach(). Protect this whole operation by taking the mutex, so that: * any users iterating over the chain will not access it during the change * other code willing to modify the list (drm_bridge_attach()) will wait until drm_encoder_cleanup() is done Note that the _safe macro in use here is providing a different and orthogonal kind of protection than the mutex: 1. list_for_each_entry_safe() allows removing the current entry from the list it is iterating on, synchronously; the non-safe version would be unable to find the next entry after the current entry has been removed 2. the mutex being added allows to ensure that the list is not used asynchronously by other code while it is being modified; this prevents such other concurrent code to derail because it is iterating over an element while it is removed The _safe macro, which works by taking the "next" pointer in addition to the "current" one, is insufficient to provide the protection at item 2 above. This is visible e.g. when the "next" element is removed by other concurrent code. This is what would happen without the added mutex: 1. start loop: list_for_each_entry_safe(pos, n, ...) sets: pos =3D list_first_entry() =3D (bridge 1) n =3D list_next_entry(pos) =3D (bridge 2) 2. enter the loop 1st time, do something with *pos (bridge 1) 3. in the meanwhile bridge 2 is hot-unplugged -> another thread removes bridge 2 -> drm_bridge_detach() -> list_del() sets (bridge 2)->next =3D LIST_POISON1 4. loop iteration 1 finishes, list_for_each_entry_safe() sets: pos =3D n (previously set to bridge 2) n =3D (bridge 2)->next =3D LIST_POISON1 5. enter the loop 2nd time, do something with *pos (bridge 2) 6. loop iteration 2 finishes, list_for_each_entry_safe() sets: pos =3D n =3D LIST_POISON1 =3D=3D> bug! However, simply adding mutex_[un]lock(&encoder->bridge_chain_mutex) before/after the list_for_each_entry_safe() seems a simple and good solution, but it is introducing a possible ABBA deadlock (found by PROVE_LOCKING). The two code paths involved are: * drm_encoder_cleanup(): - takes the bridge_chain_mutex (A) - calls drm_bridge_detach -> drm_atomic_private_obj_fini -> DRM_MODESET_LOCK_ALL_BEGIN() which takes all locks in the acquisition context (B) * drm_mode_getconnector() (and other code paths): - calls drm_helper_probe_single_connector_modes() which: - takes a drm_modeset_lock in the acquisition context (B) - calls __drm_helper_update_and_validate -> drm_bridge_chain_mode_valid -> drm_for_each_bridge_in_chain_from() which takes the bridge_chain_mutex (A) To avoid this potential ABBA deadlock, move all list items to a temporary list while holding the bridge_chain_mutex, then detach all elements from the temporary list without the mutex. Signed-off-by: Luca Ceresoli --- Changes in v3: - Prevent ABBA deadlock by using a temporary list - Improve commit message Changes in v2: - Expanded commit messge with rationale, as discussed --- drivers/gpu/drm/drm_encoder.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/drm_encoder.c b/drivers/gpu/drm/drm_encoder.c index 3261f142baea..0d5dbed06db4 100644 --- a/drivers/gpu/drm/drm_encoder.c +++ b/drivers/gpu/drm/drm_encoder.c @@ -189,14 +189,26 @@ void drm_encoder_cleanup(struct drm_encoder *encoder) { struct drm_device *dev =3D encoder->dev; struct drm_bridge *bridge, *next; + LIST_HEAD(tmplist); =20 /* Note that the encoder_list is considered to be static; should we * remove the drm_encoder at runtime we would have to decrement all * the indices on the drm_encoder after us in the encoder_list. */ =20 - list_for_each_entry_safe(bridge, next, &encoder->bridge_chain, - chain_node) + /* + * We need the bridge_chain_mutex to modify the chain, but + * drm_bridge_detach() will call DRM_MODESET_LOCK_ALL_BEGIN() (in + * drm_modeset_lock_fini()), resulting in a possible ABBA circular + * deadlock. Avoid it by first moving all the bridges to a + * temporary list holding the lock, and then calling + * drm_bridge_detach() without the lock. + */ + mutex_lock(&encoder->bridge_chain_mutex); + list_cut_before(&tmplist, &encoder->bridge_chain, &encoder->bridge_chain); + mutex_unlock(&encoder->bridge_chain_mutex); + + list_for_each_entry_safe(bridge, next, &tmplist, chain_node) drm_bridge_detach(bridge); =20 drm_mode_object_unregister(dev, &encoder->base); --=20 2.52.0 From nobody Sun Feb 8 02:20:51 2026 Received: from smtpout-02.galae.net (smtpout-02.galae.net [185.246.84.56]) (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 7B88F350A30 for ; Tue, 13 Jan 2026 18:47:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.84.56 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330057; cv=none; b=A2J4V3tFnHgYBkR3N1SOp6WnBXUls9codXYEG0+KealTrV1nWUCE7/oWKAtjdx92xEC2PbjiQDDVIrRvAQuTyoI4un+h52P7qTZVouv1GCXGjamhlc7vWMPhd40fRBkkPSVDqOMmY6Ch5EPgRmwdlJZZHStgN+tIwWDDomAFsUU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330057; c=relaxed/simple; bh=upvhPfyn+JVl4Hja1gb44M2vssMH5uTTbAx7+q6uksY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nfuhV/wUFwATDyLeUgbs2+qjo42u599yO99OQlpuBy/KD1DitjPqi+G3Jv37CBAhtL0T4GOivqlDXbmOc10QtMm2sF5FOHbrnk3/w9pLwEYBUKG7spNFO5rVkSNt7DHMESJ79AUeRNEIMvbGgkGaGxQXWisQe6T5Z/USHt2bNs8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=GXlrOr4B; arc=none smtp.client-ip=185.246.84.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="GXlrOr4B" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-02.galae.net (Postfix) with ESMTPS id 3CD381A283F; Tue, 13 Jan 2026 18:47:31 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 12C846075E; Tue, 13 Jan 2026 18:47:31 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 87F9D103C86C8; Tue, 13 Jan 2026 19:47:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1768330050; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=f8N3qyeeBYvZN6dxPwZvCgRYBZLTW6nUvSaqWwv7hlc=; b=GXlrOr4BAmTXttD9zipKagl1R3I5eh5jusYVdzJokmF4UDMINVPIk8njA4NXv/6iZuaLe5 P65Iv7tpIayesvyCRc43rXc6R713fh5P1TpqK70Hg+LEizIVzSJsszsDWBtfLG0ZN3F0fx j+OqkicZOCDS0dzEBxXXjizzPGW7cLhoWydX+6wa4X2qmuZGc5g2QOVKMlV2845hRPfmje jKLSn+dM3F7MRhrCoNCuHrZlKdzgGLmIRGjuwsvjV1ZIeIrFjgH4GXiSV/03ujwZ7NWA64 hZmpOchxC2Bs9xtCMr5Kom2Fxmub9THbJLWbu6JgWFSJSopa/OGnFr0bcML6bg== From: Luca Ceresoli Date: Tue, 13 Jan 2026 19:47:14 +0100 Subject: [PATCH RESEND v4 3/7] drm/bridge: drm_bridge_attach: lock the encoder chain mutex during insertion 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: quoted-printable Message-Id: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-3-60f3135adc45@bootlin.com> References: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> In-Reply-To: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Ian Ray X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 drm_bridge_attach() modifies the encoder bridge chain, so take a mutex around such operations to allow users of the chain to protect themselves from chain modifications while iterating. Reviewed-by: Maxime Ripard Signed-off-by: Luca Ceresoli --- Changes in v3: - Lock encoder->bridge_chain_mutex directly, no wrappers Changes in v2: - Removed comment before on drm_bridge_detach() --- drivers/gpu/drm/drm_bridge.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c index 8f355df883d8..c8389383e0d2 100644 --- a/drivers/gpu/drm/drm_bridge.c +++ b/drivers/gpu/drm/drm_bridge.c @@ -462,10 +462,12 @@ int drm_bridge_attach(struct drm_encoder *encoder, st= ruct drm_bridge *bridge, bridge->dev =3D encoder->dev; bridge->encoder =3D encoder; =20 + mutex_lock(&encoder->bridge_chain_mutex); if (previous) list_add(&bridge->chain_node, &previous->chain_node); else list_add(&bridge->chain_node, &encoder->bridge_chain); + mutex_unlock(&encoder->bridge_chain_mutex); =20 if (bridge->funcs->attach) { ret =3D bridge->funcs->attach(bridge, encoder, flags); @@ -496,7 +498,9 @@ int drm_bridge_attach(struct drm_encoder *encoder, stru= ct drm_bridge *bridge, err_reset_bridge: bridge->dev =3D NULL; bridge->encoder =3D NULL; + mutex_lock(&encoder->bridge_chain_mutex); list_del(&bridge->chain_node); + mutex_unlock(&encoder->bridge_chain_mutex); =20 if (ret !=3D -EPROBE_DEFER) DRM_ERROR("failed to attach bridge %pOF to encoder %s: %d\n", --=20 2.52.0 From nobody Sun Feb 8 02:20:51 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (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 AEEEC3502A6 for ; Tue, 13 Jan 2026 18:47:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330059; cv=none; b=ugLYVywSSK7Tmmj5AVyfWdODOrwfmGC3swklsRt6Rvxq82beUibHEFsCFiyD/xyZeEPgWzaXyLs/iKYmLLA++ExiY8/W6MIYzyk2VIwBVLGJLSjfhYtPWiSy786pcJF0BQB1FTE+7axWA4nPDlDC1gsT0Eoh3He7lk8iS9swvg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330059; c=relaxed/simple; bh=2Tq3pu8Dwz71Lw2/EhHOwgiTiuuCRN166Jcv5ho44Ts=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OZS8n19K1ve5zV/pu+ABR23whO2WgwgosPE3FUAanPMoWG6HUiehw7Cu1V20MiniU4uLaPrSi/hnCIha36nJF6jOY+nzp0peBwiZAgnG7U6jhAwjyuvmKTZnApogyFX8IpfnODPFWCfjJ/EKo0uCbA1KkCIeqKZcQfNWmeEo6b0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=DJ5CPyEL; arc=none smtp.client-ip=185.171.202.116 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="DJ5CPyEL" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id 7A82EC1ECBA; Tue, 13 Jan 2026 18:47:08 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 553936075F; Tue, 13 Jan 2026 18:47:33 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 7F57B103C8707; Tue, 13 Jan 2026 19:47:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1768330052; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=7WXp3t4en/N2gz9isbtng0HaQOEi821ijFkC1dJtqcU=; b=DJ5CPyEL4Mz72zlKR+8bHlU0UapbFLkbYAYVOFmYVcoam1pZMGozm6WFWTG9IJZtbvDNMy ia60mf5yPZlnc9jSEZxz/m5bXbR0CwEEqgpGRaFbbh9eRU8YWFRNVoJcpGvZBK/Ig7fgBp 1JTDmpoYCybdJzAlUQuFn5UjwY09+WuDByWdCuTI6k8Lq1LIl6sxbD6cyDvApOJaZkX3gJ JCa2Ooa9rp4K/tVBWzjeDXhUEjld36IeCvNzXrFLoDr20T4B8rMO1bEQd9Ab6wQt23GmNh hJRKRihMlUQDe7uu2vmEncojpoc6NrxRqVWoJ3WTiBuz0fb/ExRjChxcUl1OHQ== From: Luca Ceresoli Date: Tue, 13 Jan 2026 19:47:15 +0100 Subject: [PATCH RESEND v4 4/7] drm/bridge: lock the encoder chain in scoped for_each loops 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: quoted-printable Message-Id: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-4-60f3135adc45@bootlin.com> References: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> In-Reply-To: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Ian Ray X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 drm_for_each_bridge_in_chain_scoped() and drm_for_each_bridge_in_chain_from() currently get/put the bridge at each iteration. But they don't protect the encoder chain, so it could change (bridges added/removed) while some code is iterating over the list itself. Such code can then derail on incorrect pointers. To make iterations safe, augment these for_each macros to lock the encoder chain mutex at the beginning and unlock it at the end of the loop (be it at the end of the list, or earlier due to a 'break' or 'return' statement). This change requires more operations when starting and ending the loop. To avoid making the macros even more complex, move these operations to helper functions. Also remname some of the existing helper functions for consistency. Signed-off-by: Luca Ceresoli --- Changed in v4: - Slightly improve kerneldoc to clarify a bridge reference is held in addition to the mutex Changed in v3: - Re-add drm_bridge_get/put() Changed in v2: - Fixed infinite loop in drm_for_each_bridge_in_chain_scoped() when encoder->bridge_chain is empty, reported here: https://lore.kernel.org/lkml/202509301358.38036b85-lkp@intel.com/ - Slightly improved commit message --- include/drm/drm_bridge.h | 73 +++++++++++++++++++++++++++++++-------------= ---- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/include/drm/drm_bridge.h b/include/drm/drm_bridge.h index 0ff7ab4aa868..3d26dde1ac81 100644 --- a/include/drm/drm_bridge.h +++ b/include/drm/drm_bridge.h @@ -1440,26 +1440,37 @@ drm_bridge_chain_get_last_bridge(struct drm_encoder= *encoder) struct drm_bridge, chain_node)); } =20 -/** - * drm_bridge_get_next_bridge_and_put - Get the next bridge in the chain - * and put the previous - * @bridge: bridge object - * - * Same as drm_bridge_get_next_bridge() but additionally puts the @bridge. - * - * RETURNS: - * the next bridge in the chain after @bridge, or NULL if @bridge is the l= ast. - */ -static inline struct drm_bridge * -drm_bridge_get_next_bridge_and_put(struct drm_bridge *bridge) +/* Internal to drm_for_each_bridge_in_chain*() */ +static inline struct drm_bridge *__drm_for_each_bridge_in_chain_next(struc= t drm_bridge *bridge) { struct drm_bridge *next =3D drm_bridge_get_next_bridge(bridge); =20 + if (!next) + mutex_unlock(&bridge->encoder->bridge_chain_mutex); + drm_bridge_put(bridge); =20 return next; } =20 +/* Internal to drm_for_each_bridge_in_chain*() */ +DEFINE_FREE(__drm_for_each_bridge_in_chain_cleanup, struct drm_bridge *, + if (_T) { mutex_unlock(&_T->encoder->bridge_chain_mutex); drm_bridge_put(= _T); }) + +/* Internal to drm_for_each_bridge_in_chain_scoped() */ +static inline struct drm_bridge * +__drm_for_each_bridge_in_chain_scoped_start(struct drm_encoder *encoder) +{ + mutex_lock(&encoder->bridge_chain_mutex); + + struct drm_bridge *bridge =3D drm_bridge_chain_get_first_bridge(encoder); + + if (!bridge) + mutex_unlock(&encoder->bridge_chain_mutex); + + return bridge; +} + /** * drm_for_each_bridge_in_chain_scoped - iterate over all bridges attached * to an encoder @@ -1469,14 +1480,24 @@ drm_bridge_get_next_bridge_and_put(struct drm_bridg= e *bridge) * * Iterate over all bridges present in the bridge chain attached to @encod= er. * - * Automatically gets/puts the bridge reference while iterating, and puts - * the reference even if returning or breaking in the middle of the loop. + * Automatically gets/puts the bridge reference while iterating and locks + * the encoder chain mutex to prevent chain modifications while iterating. */ -#define drm_for_each_bridge_in_chain_scoped(encoder, bridge) \ - for (struct drm_bridge *bridge __free(drm_bridge_put) =3D \ - drm_bridge_chain_get_first_bridge(encoder); \ - bridge; \ - bridge =3D drm_bridge_get_next_bridge_and_put(bridge)) +#define drm_for_each_bridge_in_chain_scoped(encoder, bridge) \ + for (struct drm_bridge *bridge __free(__drm_for_each_bridge_in_chain_clea= nup) =3D \ + __drm_for_each_bridge_in_chain_scoped_start((encoder)); \ + bridge; \ + bridge =3D __drm_for_each_bridge_in_chain_next(bridge)) \ + +/* Internal to drm_for_each_bridge_in_chain_from() */ +static inline struct drm_bridge * +__drm_for_each_bridge_in_chain_from_start(struct drm_bridge *bridge) +{ + drm_bridge_get(bridge); + mutex_lock(&bridge->encoder->bridge_chain_mutex); + + return bridge; +} =20 /** * drm_for_each_bridge_in_chain_from - iterate over all bridges starting @@ -1488,14 +1509,14 @@ drm_bridge_get_next_bridge_and_put(struct drm_bridg= e *bridge) * Iterate over all bridges in the encoder chain starting from * @first_bridge, included. * - * Automatically gets/puts the bridge reference while iterating, and puts - * the reference even if returning or breaking in the middle of the loop. + * Automatically gets/puts the bridge reference while iterating and locks + * the encoder chain mutex to prevent chain modifications while iterating. */ -#define drm_for_each_bridge_in_chain_from(first_bridge, bridge) \ - for (struct drm_bridge *bridge __free(drm_bridge_put) =3D \ - drm_bridge_get(first_bridge); \ - bridge; \ - bridge =3D drm_bridge_get_next_bridge_and_put(bridge)) +#define drm_for_each_bridge_in_chain_from(first_bridge, bridge) \ + for (struct drm_bridge *bridge __free(__drm_for_each_bridge_in_chain_clea= nup) =3D \ + __drm_for_each_bridge_in_chain_from_start(first_bridge); \ + bridge; \ + bridge =3D __drm_for_each_bridge_in_chain_next(bridge)) \ =20 enum drm_mode_status drm_bridge_chain_mode_valid(struct drm_bridge *bridge, --=20 2.52.0 From nobody Sun Feb 8 02:20:51 2026 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (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 4EF90350A1F for ; Tue, 13 Jan 2026 18:47:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330061; cv=none; b=KhmP6Ipqky21my8W5kL4UyZJNptM3FU2H4qXANaXlUgp5sSgc1EnXVgz06TBYRpbKL2ydoItAJwBq9Y9PrifhWkXf6vYq00rjPhTGB7c5evutpgInPsxdaNK351k9gi5+jNtmknOtb7nlQHV+QlhMRa7YIJLgJvErUDN3QXPoeg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330061; c=relaxed/simple; bh=f4F4j8gy5cE/PMthB0eGZPe9LgLqlgnYOKFT85cMRvE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=c9k8q75lRr6o/rKffGaes97Mq3QDOSqNV+ZgKxUzKwvvzTz/H4DCMIcJWaoBknY8KiOSBXQSkBgJgMgYsaQ0TsVEwUBdGYBFpPtaD9nhj6NE2xWNKWwAPA36DcTUhAT+Br7Z/Ml1lvY3ygjnzA4weTIuYnG9dytFWV0bWuRuYiY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=Psfldq57; arc=none smtp.client-ip=185.246.85.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="Psfldq57" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id C5D614E420B3; Tue, 13 Jan 2026 18:47:35 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 9B7D760701; Tue, 13 Jan 2026 18:47:35 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id B406D103C8719; Tue, 13 Jan 2026 19:47:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1768330054; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=chLpKGcjGPTQRI3u2R4TC4rBXhiSYvk3tlkO394e8XI=; b=Psfldq578EeiR0MeYqABZUX1MK6MPfV9LBKQBuGHFJe+oLCwc1QF6TvMTjMM68svHRPL4e Ckp27rMufKt5D7DlRTiP+cTirO9SpK85fxLTKMckdpfy29qKAKc5sjwLbEZnAmeKmZh1vo raent+a91tk7VsFK9U+d9iOJkeLc9DiV/iQLw1oScjeJpvBMdBUdfy6OrUSrWa9pqHwKbZ OcG3rMZtkC0G1jDZ9DgKwBUuEU6ejPfCYjXxwQt4sF/Pjqg4GceHi/9bjv/uGby1sgTZUL wV1Csh17YY04TX+cr0QRsEhRY9LCAG6TtIJkhAudwVMgjNq6BepJ7szUFsUy7g== From: Luca Ceresoli Date: Tue, 13 Jan 2026 19:47:16 +0100 Subject: [PATCH RESEND v4 5/7] drm/bridge: prevent encoder chain changes while iterating with list_for_each_entry_from() 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: quoted-printable Message-Id: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-5-60f3135adc45@bootlin.com> References: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> In-Reply-To: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Ian Ray X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 These loops in drm_bridge.c iterate over the encoder chain using list_for_each_entry_from(), which does not prevent changes to the bridge chain while iterating over it. Convert most of those loops to instead use drm_for_each_bridge_in_chain_from(), which locks the chain. This also simplifies code. All the "simple" loops are converted here. The only ones not touched are those in drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_post_disable(), because they have nested loops which are not well handled by drm_for_each_bridge_in_chain_from(). Those two functions are handled by a separate commit. Signed-off-by: Luca Ceresoli --- drivers/gpu/drm/drm_bridge.c | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c index c8389383e0d2..9b4a075b5361 100644 --- a/drivers/gpu/drm/drm_bridge.c +++ b/drivers/gpu/drm/drm_bridge.c @@ -627,7 +627,7 @@ void drm_bridge_detach(struct drm_bridge *bridge) /** * drm_bridge_chain_mode_valid - validate the mode against all bridges in = the * encoder chain. - * @bridge: bridge control structure + * @first_bridge: bridge control structure * @info: display info against which the mode shall be validated * @mode: desired mode to be validated * @@ -641,17 +641,14 @@ void drm_bridge_detach(struct drm_bridge *bridge) * MODE_OK on success, drm_mode_status Enum error code on failure */ enum drm_mode_status -drm_bridge_chain_mode_valid(struct drm_bridge *bridge, +drm_bridge_chain_mode_valid(struct drm_bridge *first_bridge, const struct drm_display_info *info, const struct drm_display_mode *mode) { - struct drm_encoder *encoder; - - if (!bridge) + if (!first_bridge) return MODE_OK; =20 - encoder =3D bridge->encoder; - list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { + drm_for_each_bridge_in_chain_from(first_bridge, bridge) { enum drm_mode_status ret; =20 if (!bridge->funcs->mode_valid) @@ -669,7 +666,7 @@ EXPORT_SYMBOL(drm_bridge_chain_mode_valid); /** * drm_bridge_chain_mode_set - set proposed mode for all bridges in the * encoder chain - * @bridge: bridge control structure + * @first_bridge: bridge control structure * @mode: desired mode to be set for the encoder chain * @adjusted_mode: updated mode that works for this encoder chain * @@ -678,20 +675,16 @@ EXPORT_SYMBOL(drm_bridge_chain_mode_valid); * * Note: the bridge passed should be the one closest to the encoder */ -void drm_bridge_chain_mode_set(struct drm_bridge *bridge, +void drm_bridge_chain_mode_set(struct drm_bridge *first_bridge, const struct drm_display_mode *mode, const struct drm_display_mode *adjusted_mode) { - struct drm_encoder *encoder; - - if (!bridge) + if (!first_bridge) return; =20 - encoder =3D bridge->encoder; - list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { + drm_for_each_bridge_in_chain_from(first_bridge, bridge) if (bridge->funcs->mode_set) bridge->funcs->mode_set(bridge, mode, adjusted_mode); - } } EXPORT_SYMBOL(drm_bridge_chain_mode_set); =20 @@ -915,7 +908,7 @@ EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable); =20 /** * drm_atomic_bridge_chain_enable - enables all bridges in the encoder cha= in - * @bridge: bridge control structure + * @first_bridge: bridge control structure * @state: atomic state being committed * * Calls &drm_bridge_funcs.atomic_enable (falls back on @@ -925,22 +918,18 @@ EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable); * * Note: the bridge passed should be the one closest to the encoder */ -void drm_atomic_bridge_chain_enable(struct drm_bridge *bridge, +void drm_atomic_bridge_chain_enable(struct drm_bridge *first_bridge, struct drm_atomic_state *state) { - struct drm_encoder *encoder; - - if (!bridge) + if (!first_bridge) return; =20 - encoder =3D bridge->encoder; - list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { + drm_for_each_bridge_in_chain_from(first_bridge, bridge) if (bridge->funcs->atomic_enable) { bridge->funcs->atomic_enable(bridge, state); } else if (bridge->funcs->enable) { bridge->funcs->enable(bridge); } - } } EXPORT_SYMBOL(drm_atomic_bridge_chain_enable); =20 --=20 2.52.0 From nobody Sun Feb 8 02:20:51 2026 Received: from smtpout-03.galae.net (smtpout-03.galae.net [185.246.85.4]) (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 48A26350D7C for ; Tue, 13 Jan 2026 18:47:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.246.85.4 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330067; cv=none; b=eKDP90J3piBc6meBQ5fOLj/m1nc0r1LTYE3fELCHswrjHtIk73A8r5L6nNx+Egcuw+EO6+qWwh+aB8xtgUAJ/+Yz6fLx3+9IU7PE5XF9RfIfni19JXyDyPr0PZ5GAeMgIColiwdYWOcukrtnNeoZF7SoZ2x4r1V3nS8cFcro0Z8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330067; c=relaxed/simple; bh=T6vjkCTJHKrrfCfENt22C+/WuMCOS6UvN5jMtJ/7I30=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ly0zHAgVTNfurj1QtEWP8TUa4Y6hYf6pj6ykgGZ5+GPGWOumSNzaD5rBiSS2E1UqHJriEZID6/j4/pD4GDmuDMdA02/BqV1S797vYk+qjSg8gmD2sMh1Wm99NEcmBike6TYLXAh+1epsRiphnEu8NR7rketzE6/kuq3uVx4HL4A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=zxn3en61; arc=none smtp.client-ip=185.246.85.4 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="zxn3en61" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-03.galae.net (Postfix) with ESMTPS id DD14F4E420B4; Tue, 13 Jan 2026 18:47:37 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id B20E260701; Tue, 13 Jan 2026 18:47:37 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 107E1103C871A; Tue, 13 Jan 2026 19:47:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1768330056; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=dzUppabWL92ubQUZhFXAzHoT9lyZRb6BD9mDAHq16fA=; b=zxn3en61kmIXe5o7kG+ogBYNR2D5f5ro+DjUuhOwDxpPo3DOgAosWK/FgKPfA4ZHXWs0re voQAP48dEbd5eGLs5X7IC+0a9pXu3Ourep5bMMIpVkJrTzGUr6LuVCsCUTYOubpv85tWHl 0NIMoSYny9DtRF6NZVr3L/6twQKa+zPgMFaI0rc5f4n06YopOwqG8SLl/r2hYBuNOYQG9f h9QrT5aezVUaqFo/NFpt6pvbhbkH/eg7pdQvcfLJ8W84P//v3YWz/9ZJwnKnoUMlmGaP/y iECmH9glhdK4I64pQFrzKU/IE6ZSetdq+u7jS3aZn5KmGzfBaTLw3Fs7DF+5jQ== From: Luca Ceresoli Date: Tue, 13 Jan 2026 19:47:17 +0100 Subject: [PATCH RESEND v4 6/7] drm/bridge: prevent encoder chain changes while iterating with list_for_each_entry_reverse() 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: quoted-printable Message-Id: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-6-60f3135adc45@bootlin.com> References: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> In-Reply-To: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Ian Ray X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 These loops in drm_bridge.c iterate over the encoder chain using list_for_each_entry_reverse(), which does not prevent changes to the bridge chain while iterating over it. Take the encoder chain mutex while iterating to avoid chain changes while iterating. All the "simple" loops are converted. drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_post_disable() are handled by a separate commit. Signed-off-by: Luca Ceresoli --- Changes in v3: - Lock encoder->bridge_chain_mutex directly, no wrappers Changes in v2: none --- drivers/gpu/drm/drm_bridge.c | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c index 9b4a075b5361..eca138eadbc5 100644 --- a/drivers/gpu/drm/drm_bridge.c +++ b/drivers/gpu/drm/drm_bridge.c @@ -710,6 +710,7 @@ void drm_atomic_bridge_chain_disable(struct drm_bridge = *bridge, return; =20 encoder =3D bridge->encoder; + mutex_lock(&encoder->bridge_chain_mutex); list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { if (iter->funcs->atomic_disable) { iter->funcs->atomic_disable(iter, state); @@ -720,6 +721,7 @@ void drm_atomic_bridge_chain_disable(struct drm_bridge = *bridge, if (iter =3D=3D bridge) break; } + mutex_unlock(&encoder->bridge_chain_mutex); } EXPORT_SYMBOL(drm_atomic_bridge_chain_disable); =20 @@ -1224,25 +1226,27 @@ int drm_atomic_bridge_chain_check(struct drm_bridge= *bridge, return ret; =20 encoder =3D bridge->encoder; - list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { - int ret; - - /* - * Bus flags are propagated by default. If a bridge needs to - * tweak the input bus flags for any reason, it should happen - * in its &drm_bridge_funcs.atomic_check() implementation such - * that preceding bridges in the chain can propagate the new - * bus flags. - */ - drm_atomic_bridge_propagate_bus_flags(iter, conn, - crtc_state->state); - - ret =3D drm_atomic_bridge_check(iter, crtc_state, conn_state); - if (ret) - return ret; + scoped_guard(mutex, &encoder->bridge_chain_mutex) { + list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { + int ret; + + /* + * Bus flags are propagated by default. If a bridge needs to + * tweak the input bus flags for any reason, it should happen + * in its &drm_bridge_funcs.atomic_check() implementation such + * that preceding bridges in the chain can propagate the new + * bus flags. + */ + drm_atomic_bridge_propagate_bus_flags(iter, conn, + crtc_state->state); + + ret =3D drm_atomic_bridge_check(iter, crtc_state, conn_state); + if (ret) + return ret; =20 - if (iter =3D=3D bridge) - break; + if (iter =3D=3D bridge) + break; + } } =20 return 0; --=20 2.52.0 From nobody Sun Feb 8 02:20:51 2026 Received: from smtpout-04.galae.net (smtpout-04.galae.net [185.171.202.116]) (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 11B35352C2D for ; Tue, 13 Jan 2026 18:47:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.171.202.116 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330067; cv=none; b=WyMV1h/qqh8BB5FbwCrrF07Ubv2At18r6kkyI5wn8BLZPrrmTQKzpWnZ0DT0qc8A/FNd/HAbcgZZIh3VNgUKv69TpvlMWwSL4ka07poF5nK3IOkSF5cbhx6ZQHXdFV5+7vMCsjNtOq9v0L277aqwdobZRDCPW3r8EQKPCgueMWo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768330067; c=relaxed/simple; bh=wIyjkJuGuBNELHnuKB+mmE0kYmh9zRXfcaF4nIm1Gz4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Hqi/t8Egcy2WlLLdSt8lMH5khN4TAveUWrox0YZJdYG3pZKrBl+aKqrZkZ2kdQ17xiVi3iDqnj7AqW7mYjARZ0DWucAy3hvkfwrFPS8wJJHz/Rynr497BLgB4UgeaE3oApmc9CwCXH1zFeINlG39oZVq0SZLWZ/w54qlXtlOO4g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=uydhV+vM; arc=none smtp.client-ip=185.171.202.116 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="uydhV+vM" Received: from smtpout-01.galae.net (smtpout-01.galae.net [212.83.139.233]) by smtpout-04.galae.net (Postfix) with ESMTPS id F3781C1ECB8; Tue, 13 Jan 2026 18:47:12 +0000 (UTC) Received: from mail.galae.net (mail.galae.net [212.83.136.155]) by smtpout-01.galae.net (Postfix) with ESMTPS id 87EFB60701; Tue, 13 Jan 2026 18:47:39 +0000 (UTC) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 10FD9103C871F; Tue, 13 Jan 2026 19:47:36 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=dkim; t=1768330058; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=dG/J7ABnbSTu61X3mrMuyDFuZTrdXx8E+cS1ACIsDWw=; b=uydhV+vM9q2P5fWZBH+W/KTjwFFJyvp8rsTykluvNinkGHFqdhQF8xsTpB9SqJWc8oPZAX jhZJgXgafJRYZ4D9EfFkDV+Onv9wd9DR8/NDyOAl25u7/CbHRMlKqR2nCYtkGvvySsio/5 scQ3K9bVFgXa7dvs3vecE84PNAKemjXw9J/7EXmOuJl28tOwYG/meSE8MNWOC+GszcHnma NrkACgNI3nxJ3Ld7fERqR4ROWtvlRFHJ72cxeK3TWSdPU9Y3wPXl+8Y3UXco0krtFhRMPY mLkSX7vBOLkwgFkNk+befeN+KBG2wkwrC3fpN38KJiZFtnM0W1SrEkeVEpENQA== From: Luca Ceresoli Date: Tue, 13 Jan 2026 19:47:18 +0100 Subject: [PATCH RESEND v4 7/7] drm/bridge: prevent encoder chain changes in pre_enable/post_disable 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: quoted-printable Message-Id: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-7-60f3135adc45@bootlin.com> References: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> In-Reply-To: <20260113-drm-bridge-alloc-encoder-chain-mutex-v4-0-60f3135adc45@bootlin.com> To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Andrzej Hajda , Neil Armstrong , Robert Foss , Laurent Pinchart , Jonas Karlman , Jernej Skrabec Cc: Hui Pu , Thomas Petazzoni , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, Luca Ceresoli , Ian Ray X-Mailer: b4 0.14.3 X-Last-TLS-Session-Version: TLSv1.3 Take the encoder chain mutex while iterating over the encoder chain in drm_atomic_bridge_chain_pre_enable() and drm_atomic_bridge_chain_post_disable() to ensure the lists won't change while being inspected. These functions have nested list_for_each_*() loops, which makes them complicated. list_for_each_entry_from() loops could be replaced by drm_for_each_bridge_in_chain_from(), but it would not work in a nested way in its current implementation. Besides, there is no "_reverse" variant of drm_for_each_bridge_in_chain_from(). Keep code simple and readable by explicitly locking around the outer loop. Thankfully there are no break or return points inside the loops, so the change is trivial and readable. Reviewed-by: Maxime Ripard Signed-off-by: Luca Ceresoli --- Changes in v3: - Lock encoder->bridge_chain_mutex directly, no wrappers - Improved commit message Changes in v2: - Improved commit message --- drivers/gpu/drm/drm_bridge.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/gpu/drm/drm_bridge.c b/drivers/gpu/drm/drm_bridge.c index eca138eadbc5..76895b0f84d3 100644 --- a/drivers/gpu/drm/drm_bridge.c +++ b/drivers/gpu/drm/drm_bridge.c @@ -769,6 +769,7 @@ void drm_atomic_bridge_chain_post_disable(struct drm_br= idge *bridge, =20 encoder =3D bridge->encoder; =20 + mutex_lock(&encoder->bridge_chain_mutex); list_for_each_entry_from(bridge, &encoder->bridge_chain, chain_node) { limit =3D NULL; =20 @@ -817,6 +818,7 @@ void drm_atomic_bridge_chain_post_disable(struct drm_br= idge *bridge, /* Jump all bridges that we have already post_disabled */ bridge =3D limit; } + mutex_unlock(&encoder->bridge_chain_mutex); } EXPORT_SYMBOL(drm_atomic_bridge_chain_post_disable); =20 @@ -863,6 +865,7 @@ void drm_atomic_bridge_chain_pre_enable(struct drm_brid= ge *bridge, =20 encoder =3D bridge->encoder; =20 + mutex_lock(&encoder->bridge_chain_mutex); list_for_each_entry_reverse(iter, &encoder->bridge_chain, chain_node) { if (iter->pre_enable_prev_first) { next =3D iter; @@ -905,6 +908,7 @@ void drm_atomic_bridge_chain_pre_enable(struct drm_brid= ge *bridge, if (iter =3D=3D bridge) break; } + mutex_unlock(&encoder->bridge_chain_mutex); } EXPORT_SYMBOL(drm_atomic_bridge_chain_pre_enable); =20 --=20 2.52.0