drm/amdgpu: use drm_gem_ttm_dumb_map_offset() for dumb_map_offset
Subsystems (2)
PATCH v2 1/1 drm/amdgpu: use drm_gem_ttm_dumb_map_offset() for dumb_map_offset
As part of the DRM TODO item "Remove custom dumb_map_offset implementations", switch amdgpu's &drm_driver.dumb_map_offset callback from the driver-specific amdgpu_mode_dumb_mmap() to the generic TTM helper drm_gem_ttm_dumb_map_offset(), for both amdgpu_kms_driver and amdgpu_partition_driver.
amdgpu_mode_dumb_mmap() additionally rejected userptr BOs and BOs created with AMDGPU_GEM_CREATE_NO_CPU_ACCESS with -EPERM. Dropping those checks from the dumb path does not allow such BOs to be mapped:
returning an offset does not create a mapping, and the actual mmap() goes through drm_gem_mmap() -> amdgpu_gem_object_mmap(), which performs the same two checks and still returns -EPERM. The only user-visible difference is that DRM_IOCTL_MODE_MAP_DUMB on such a handle now succeeds and the subsequent mmap() fails, instead of the ioctl failing. Dumb buffers created by amdgpu_mode_dumb_create() are never userptr BOs and always have AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED set, so they are not affected.
With that, amdgpu_gem_mmap_ioctl() is the only remaining user of amdgpu_mode_dumb_mmap(), so fold it into the ioctl and drop its prototype from amdgpu_gem.h. DRM_IOCTL_AMDGPU_GEM_MMAP, used by libdrm_amdgpu and Mesa, keeps the userptr and NO_CPU_ACCESS checks and behaves as before.
Build-tested with CONFIG_DRM_AMDGPU=m (W=1 for amdgpu_drv.o and amdgpu_gem.o), no new warnings. Not runtime tested.
The code change was written by hand and reviewed with the help of Claude (AI coding assistant); the commit message was written by Claude.
---
Changes in v2:
- Fold amdgpu_mode_dumb_mmap() into amdgpu_gem_mmap_ioctl() and drop its
prototype, as it has no other users (Sashiko)
- Link to v1: https://lore.kernel.org/r/[email protected]
--- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 5 +++-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 23 +++++++++-------------- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h | 3 --- 3 files changed, 12 insertions(+), 19 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +3 -2
@@ -27,6 +27,7 @@#include <drm/drm_drv.h> #include <drm/drm_fbdev_ttm.h> #include <drm/drm_gem.h> +#include <drm/drm_gem_ttm_helper.h> #include <drm/drm_managed.h> #include <drm/drm_pciids.h> #include <drm/drm_probe_helper.h>
@@ -3151,7 +3152,7 @@ static const struct drm_driver amdgpu_kms_driver = { .ioctls = amdgpu_ioctls_kms, .num_ioctls = ARRAY_SIZE(amdgpu_ioctls_kms), .dumb_create = amdgpu_mode_dumb_create, - .dumb_map_offset = amdgpu_mode_dumb_mmap, + .dumb_map_offset = drm_gem_ttm_dumb_map_offset,DRM_FBDEV_TTM_DRIVER_OPS , .fops = &amdgpu_driver_kms_fops, .release = &amdgpu_driver_release_kms,
@@ -3177,7 +3178,7 @@ const struct drm_driver amdgpu_partition_driver = { .ioctls = amdgpu_ioctls_kms, .num_ioctls = ARRAY_SIZE(amdgpu_ioctls_kms), .dumb_create = amdgpu_mode_dumb_create, - .dumb_map_offset = amdgpu_mode_dumb_mmap, + .dumb_map_offset = drm_gem_ttm_dumb_map_offset,DRM_FBDEV_TTM_DRIVER_OPS , .fops = &amdgpu_driver_kms_fops, .release = &amdgpu_driver_release_kms,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9 -14
@@ -644,13 +644,16 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,return r; } -int amdgpu_mode_dumb_mmap (struct drm_file *filp, -struct drm_device *dev, -uint32_t handle,uint64_t *offset_p) +int amdgpu_gem_mmap_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) { +union drm_amdgpu_gem_mmap *args = data; +uint32_t handle = args->in.handle;struct drm_gem_object *gobj;struct amdgpu_bo *robj; +memset (args,0 ,sizeof (*args)); + gobj =drm_gem_object_lookup (filp, handle);if (!gobj)return -ENOENT ;
@@ -661,19 +664,11 @@ int amdgpu_mode_dumb_mmap(struct drm_file *filp,drm_gem_object_put (gobj);return -EPERM ; } - *offset_p =amdgpu_bo_mmap_offset (robj); -drm_gem_object_put (gobj); -return 0 ; -} -int amdgpu_gem_mmap_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) -{ -union drm_amdgpu_gem_mmap *args = data; -uint32_t handle = args->in.handle; + args->out.addr_ptr =amdgpu_bo_mmap_offset (robj); +drm_gem_object_put (gobj); -memset (args,0 ,sizeof (*args)); -return amdgpu_mode_dumb_mmap (filp, dev, handle, &args->out.addr_ptr); +return 0 ; }/**
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +0 -3
@@ -49,9 +49,6 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,int amdgpu_mode_dumb_create (struct drm_file *file_priv,struct drm_device *dev,struct drm_mode_create_dumb *args); -int amdgpu_mode_dumb_mmap (struct drm_file *filp, -struct drm_device *dev, -uint32_t handle,uint64_t *offset_p);int amdgpu_gem_create_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);
---
base-commit: 279235273fc6c0a33df863b4a1cc52ec1e73aba5
change-id: 20261105-amdgpu-dumb-map-offset-6fa41e8d3fb5
Best regards,
Signature · 1 line
saad <[email protected]>
Reviewed and applied. Thanks!
Alex
Quoted 143 lines · saad
As part of the DRM TODO item "Remove custom
dumb_map_offsetimplementations", switch amdgpu's &drm_driver.dumb_map_offset callback from the driver-specificamdgpu_mode_dumb_mmap()to the generic TTM helperdrm_gem_ttm_dumb_map_offset(), for bothamdgpu_kms_driverandamdgpu_partition_driver.
amdgpu_mode_dumb_mmap()additionally rejected userptr BOs and BOs created withAMDGPU_GEM_CREATE_NO_CPU_ACCESSwith-EPERM. Dropping those checks from the dumb path does not allow such BOs to be mapped:
returning an offset does not create a mapping, and the actualmmap()goes throughdrm_gem_mmap()->amdgpu_gem_object_mmap(), which performs the same two checks and still returns-EPERM. The only user-visible difference is thatDRM_IOCTL_MODE_MAP_DUMBon such a handle now succeeds and the subsequentmmap()fails, instead of the ioctl failing. Dumb buffers created byamdgpu_mode_dumb_create()are never userptr BOs and always haveAMDGPU_GEM_CREATE_CPU_ACCESS_REQUIREDset, so they are not affected.With that,
amdgpu_gem_mmap_ioctl()is the only remaining user ofamdgpu_mode_dumb_mmap(), so fold it into the ioctl and drop its prototype fromamdgpu_gem.h.DRM_IOCTL_AMDGPU_GEM_MMAP, used bylibdrm_amdgpuand Mesa, keeps the userptr andNO_CPU_ACCESSchecks and behaves as before.Build-tested with
CONFIG_DRM_AMDGPU=m(W=1 for amdgpu_drv.o and amdgpu_gem.o), no new warnings. Not runtime tested.The code change was written by hand and reviewed with the help of Claude (AI coding assistant); the commit message was written by Claude.
Signed-off-by saad <[email protected]>---
Changes in v2:
- Foldamdgpu_mode_dumb_mmap()intoamdgpu_gem_mmap_ioctl()and drop its
prototype, as it has no other users (Sashiko)
- Link to v1: https://lore.kernel.org/r/[email protected]--- drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 5 +++-- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 23 +++++++++-------------- drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h | 3 --- 3 files changed, 12 insertions(+), 19 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +3
-2@@ -27,6 +27,7 @@#include <drm/drm_drv.h> #include <drm/drm_fbdev_ttm.h> #include <drm/drm_gem.h> +#include <drm/drm_gem_ttm_helper.h> #include <drm/drm_managed.h> #include <drm/drm_pciids.h> #include <drm/drm_probe_helper.h> @@ -3151,7 +3152,7 @@ static const struct drm_driver amdgpu_kms_driver = { .ioctls = amdgpu_ioctls_kms, .num_ioctls = ARRAY_SIZE(amdgpu_ioctls_kms), .dumb_create = amdgpu_mode_dumb_create, - .dumb_map_offset = amdgpu_mode_dumb_mmap, + .dumb_map_offset = drm_gem_ttm_dumb_map_offset,DRM_FBDEV_TTM_DRIVER_OPS , .fops = &amdgpu_driver_kms_fops, .release = &amdgpu_driver_release_kms,@@ -3177,7 +3178,7 @@ const struct drm_driver amdgpu_partition_driver = { .ioctls = amdgpu_ioctls_kms, .num_ioctls = ARRAY_SIZE(amdgpu_ioctls_kms), .dumb_create = amdgpu_mode_dumb_create, - .dumb_map_offset = amdgpu_mode_dumb_mmap, + .dumb_map_offset = drm_gem_ttm_dumb_map_offset,DRM_FBDEV_TTM_DRIVER_OPS , .fops = &amdgpu_driver_kms_fops, .release = &amdgpu_driver_release_kms,drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +9
-14@@ -644,13 +644,16 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,return r; } -int amdgpu_mode_dumb_mmap (struct drm_file *filp, -struct drm_device *dev, -uint32_t handle,uint64_t *offset_p) +int amdgpu_gem_mmap_ioctl (struct drm_device *dev,void *data, +struct drm_file *filp) { +union drm_amdgpu_gem_mmap *args = data; +uint32_t handle = args->in.handle;struct drm_gem_object *gobj;struct amdgpu_bo *robj; +memset (args,0 ,sizeof (*args)); + gobj =drm_gem_object_lookup (filp, handle);if (!gobj)return -ENOENT ;@@ -661,19 +664,11 @@ int amdgpu_mode_dumb_mmap(struct drm_file *filp,drm_gem_object_put (gobj);return -EPERM ; } - *offset_p =amdgpu_bo_mmap_offset (robj); -drm_gem_object_put (gobj); -return 0 ; -} -int amdgpu_gem_mmap_ioctl (struct drm_device *dev,void *data, -struct drm_file *filp) -{ -union drm_amdgpu_gem_mmap *args = data; -uint32_t handle = args->in.handle; + args->out.addr_ptr =amdgpu_bo_mmap_offset (robj); +drm_gem_object_put (gobj); -memset (args,0 ,sizeof (*args)); -return amdgpu_mode_dumb_mmap (filp, dev, handle, &args->out.addr_ptr); +return 0 ; }/** drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +0
-4@@ -49,9 +49,6 @@ int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,int amdgpu_mode_dumb_create (struct drm_file *file_priv,struct drm_device *dev,struct drm_mode_create_dumb *args); -int amdgpu_mode_dumb_mmap (struct drm_file *filp, -struct drm_device *dev, -uint32_t handle,uint64_t *offset_p);int amdgpu_gem_create_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); ---base-commit:
279235273fc6c0a33df863b4a1cc52ec1e73aba5
change-id: 20261105-amdgpu-dumb-map-offset-6fa41e8d3fb5Best regards,
--
saad <[email protected]>