Hej, this series is triggered by the discussion of gem/shmem.rs SGTableMap issues in [1] which has an additional issue with imported objects. Imported objects are supported since GEM shmem's AllocOps use drm_gem_shmem_prime_import_sg_table() as gem_prime_import_sg_table which sets the same function pointer field in struct drm_driver.
The code is broken in two ways. SGTableMap::drop() calls __drm_gem_shmem_free_sgt_locked() uncondiftionally which it must not do for imported objects as the object does not own `sgt`. The second issue is that the imported object is allocated by __drm_gem_shmem_create() with sizeof(struct drm_gem_shmem_object) as size instead of the size of Rust's Object<T>. The first issue is fixed in the Asahi downstream driver together with a drm_gem_is_imported() wrapper change taken from the Tyr downstream driver. The second issue is fixed by avoiding to allocate Rust GEM objects from C. In addition to size issue v1 addressed Rust gem objects may contain fields with non-trivial `Drop` implementations. Those fields won't be destroyed in error paths in C code which "destroys" the objects with `kfree(obj)`. Solve this by re-implementing gem_prime_import_sg_table() in Rust. The same issues apply to drm_gem_shmem_dumb_create(). As Asahi and Tyr as render only devices do not use the `dumb_create` callback just drop it. When a future driver uses the callback drm_gem_shmem_dumb_create() needs to be re-implemented in Rust as well. The series is tested in the Asahi downstream driver. As the prime import code path is unused in-tree I marked the fixes as not suitable for stable backports. The known downstream users of the code are aware of the issues and carry fixes downstream. I don't expect new Rust DRM drivers to be based on (old) stable trees. Link: https://lore.kernel.org/rust-for-linux/[email protected]/ [1] Signed-off-by: Janne Grunau <[email protected]> --- Changes in v4: - Update shmem documentation as proposed by Alice - reordered commits so that the documentation update folded into the "Do not free SG table of imported objects" change preceedes the prime_import_sg_table() implementation - use "`private` parameter" in commit message as suggested by Daniel - imported Alice's and Daniel's Reviewed-by tags - rebased onto drm-rustr-next commit ab51a68d2c0e ("drm/tyr: vm: check the remaining length in pt_unmap()") to avoid resolve in SGTableMap<T>::new() documentation - Link to v3: https://lore.kernel.org/r/20261203-rust-drm-gem-shmem-imported-sgt-v3-0-8ca393e83...@jannau.net Changes in v3: - Bind AllocImpl's Driver to Self - Import Alice's Reviewed-by and Deborah's Tested-by: - Link to v2: https://lore.kernel.org/r/20261124-rust-drm-gem-shmem-imported-sgt-v2-0-000f7a9cc...@jannau.net Changes in v2: - Switch to Rust gem_prime_import_sg_table() implementation - Drop unsound drm_gem_shmem_dumb_create() as well - Add `#[inline] annotation to BaseObject::is_imported() - Drop typoed mail address from To: - Added Alice's Reviewed-by tags - Link to v1: https://lore.kernel.org/r/20261026-rust-drm-gem-shmem-imported-sgt-v1-0-36f15ace0...@jannau.net --- Janne Grunau (4): rust: drm: gem: shmem: Do not use drm_gem_shmem_dumb_create() rust: drm: gem: shmem: Do not free SG table of imported objects drm/gem/shmem: Initialize private objects via drm_gem_shmem_init() too rust: drm: gem: shmem: Implement prime_import_sg_table callback Sami Tolvanen (1): rust: drm: gem: add an is_imported() accessor drivers/gpu/drm/drm_gem_shmem_helper.c | 34 +++++----- drivers/gpu/drm/nova/gem.rs | 4 ++ drivers/gpu/drm/tyr/gem.rs | 4 ++ include/drm/drm_gem_shmem_helper.h | 3 +- rust/helpers/drm.c | 6 ++ rust/kernel/drm/driver.rs | 2 +- rust/kernel/drm/gem/mod.rs | 17 +++++ rust/kernel/drm/gem/shmem.rs | 116 +++++++++++++++++++++++++++++---- 8 files changed, 152 insertions(+), 34 deletions(-) --- base-commit: ab51a68d2c0e39c3df01d48e5c19aefc2ae6b50a change-id: 20261026-rust-drm-gem-shmem-imported-sgt-85c4a45be283 Best regards, -- Janne Grunau <[email protected]>
