Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

drm/bridge: add next_bridge pointer to struct drm_bridge

Many bridge drivers store a next_bridge pointer in their private data and
use it for attach and sometimes other purposes. This is going to be risky
when bridge hot-unplug is used.

Considering this example scenario:

1. pipeline: encoder --> bridge A --> bridge B --> bridge C
2. encoder takes a reference to bridge B
3. bridge B takes a next_bridge reference to bridge C
4. encoder calls (bridge B)->b_foo(), which in turns references
next_bridge, e.g.:

b_foo() {
bar(b->next_bridge);
}

If bridges B and C are removed, bridge C can be freed but B is still
allocated because the encoder holds a reference to B. So when step 4
happens, 'b->next-bridge' would be a use-after-free.

Calling drm_bridge_put() in the B bridge .remove function does not solve
the problem as it leaves a (potentially long) risk window between B removal
and the final deallocation of B. A safe moment to put the B reference is in
__drm_bridge_free(), when the last reference has been put. This can be done
by drivers in the .destroy func. However to avoid the need for so many
drivers to implement a .destroy func, just offer a next_bridge pointer to
all bridges that is automatically put it in __drm_bridge_free(), exactly
when the .destroy func is called.

Suggested-by: Maxime Ripard <mripard@kernel.org>
Link: https://lore.kernel.org/all/20251201-thick-jasmine-oarfish-1eceb0@houat/
Reviewed-by: Maxime Ripard <mripard@kernel.org>
Link: https://patch.msgid.link/20251216-drm-bridge-alloc-getput-drm_of_find_bridge-v3-6-b5165fab8058@bootlin.com
Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>

+13
+2
drivers/gpu/drm/drm_bridge.c
··· 275 275 if (bridge->funcs->destroy) 276 276 bridge->funcs->destroy(bridge); 277 277 278 + drm_bridge_put(bridge->next_bridge); 279 + 278 280 kfree(bridge->container); 279 281 } 280 282
+11
include/drm/drm_bridge.h
··· 1279 1279 * @hpd_cb. 1280 1280 */ 1281 1281 void *hpd_data; 1282 + 1283 + /** 1284 + * @next_bridge: Pointer to the following bridge, automatically put 1285 + * when this bridge is freed (i.e. at destroy time). This is for 1286 + * drivers needing to store a pointer to the next bridge in the 1287 + * chain, and ensures any code still holding a reference to this 1288 + * bridge after its removal cannot use-after-free the next 1289 + * bridge. Any other bridge pointers stored by the driver must be 1290 + * put in the .destroy callback by driver code. 1291 + */ 1292 + struct drm_bridge *next_bridge; 1282 1293 }; 1283 1294 1284 1295 static inline struct drm_bridge *