drm: Add CHANGE_HANDLE ioctl for drm gem
Subsystems (2)
This patch adds an ioctl to reassign the gem handle of a bo. It was formerly the first patch of my CRIU with dmabuf patchset.
Would it be possible to merge this on its own? Internal deadlines would prefer this to be in the next kernel release if possible.
The CRIU patches that are the current use case for this can be found at https://github.com/checkpoint-restore/criu/pull/2613
PATCH 1/1 drm: Add DRM prime interface to reassign GEM handle
CRIU restore of drm buffer objects requires the ability to create or import a buffer object with a specific gem handle.
Add new drm ioctl DRM_IOCTL_GEM_CHANGE_HANDLE, which takes the gem handle of an object and moves that object to a specified new gem handle.
This ioctl needs to call drm_prime_remove_buf_handle, but that function acquires the prime lock, which the ioctl needs to hold for other purposes.
Make drm_prime_remove_buf_handle not acquire the prime lock, and change its other caller to reflect this.
--- drivers/gpu/drm/drm_gem.c | 61 ++++++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_internal.h | 4 +++ drivers/gpu/drm/drm_ioctl.c | 1 + drivers/gpu/drm/drm_prime.c | 6 +--- include/uapi/drm/drm.h | 17 ++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-)
drivers/gpu/drm/drm_gem.c +61 -0
@@ -282,7 +282,12 @@ drm_gem_object_release_handle(int id, void *ptr, void *data)if (obj->funcs->close) obj->funcs->close (obj, file_priv); +mutex_lock (&file_priv->prime.lock); +drm_prime_remove_buf_handle (&file_priv->prime, id); + +mutex_unlock (&file_priv->prime.lock); +drm_vma_node_revoke (&obj->vma_node, file_priv);drm_gem_object_handle_put_unlocked (obj);
@@ -888,6 +893,62 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data,return ret; } +/** +* drm_gem_change_handle_ioctl - implementation of the GEM_CHANGE_HANDLE ioctl +* @dev: drm_device +* @data: ioctl data +* @file_priv: drm file-private structure +* +* Change the handle of a GEM object to the specified one. +* The new handle must be unused. On success the old handle is closed +* and all further IOCTL should refer to the new handle only. +* Calls to DRM_IOCTL_PRIME_FD_TO_HANDLE will return the new handle. +*/ +int drm_gem_change_handle_ioctl (struct drm_device *dev,void *data, +struct drm_file *file_priv) +{ +struct drm_gem_change_handle *args = data; +struct drm_gem_object *obj; +int ret; + + obj =drm_gem_object_lookup (file_priv, args->handle); +if (!obj) +return -ENOENT ; + +if (args->handle == args->new_handle) +return 0 ; + +mutex_lock (&file_priv->prime.lock); + +spin_lock (&file_priv->table_lock); + ret =idr_alloc (&file_priv->object_idr, obj, args->new_handle, args->new_handle +1 ,GFP_NOWAIT ); +spin_unlock (&file_priv->table_lock); + +if (ret <0 ) + goto out_unlock; + +if (obj->dma_buf) { + ret =drm_prime_add_buf_handle (&file_priv->prime, obj->dma_buf, args->new_handle); +if (ret <0 ) { +spin_lock (&file_priv->table_lock); +idr_remove (&file_priv->object_idr, args->new_handle); +spin_unlock (&file_priv->table_lock); + goto out_unlock; + } + +drm_prime_remove_buf_handle (&file_priv->prime, args->handle); + } + +spin_lock (&file_priv->table_lock); +idr_remove (&file_priv->object_idr, args->handle); +spin_unlock (&file_priv->table_lock); + +out_unlock: +mutex_unlock (&file_priv->prime.lock); + +return ret; +} +/** * drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl * @dev: drm_device
drivers/gpu/drm/drm_internal.h +4 -0
@@ -85,6 +85,8 @@ int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,void drm_prime_init_file_private (struct drm_prime_file_private *prime_fpriv);void drm_prime_destroy_file_private (struct drm_prime_file_private *prime_fpriv); +int drm_prime_add_buf_handle (struct drm_prime_file_private *prime_fpriv, +struct dma_buf *dma_buf,uint32_t handle);void drm_prime_remove_buf_handle (struct drm_prime_file_private *prime_fpriv,uint32_t handle);
@@ -168,6 +170,8 @@ int drm_gem_close_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);int drm_gem_flink_ioctl (struct drm_device *dev,void *data,struct drm_file *file_priv); +int drm_gem_change_handle_ioctl (struct drm_device *dev,void *data, +struct drm_file *file_priv);int drm_gem_open_ioctl (struct drm_device *dev,void *data,struct drm_file *file_priv);void drm_gem_open (struct drm_device *dev,struct drm_file *file_private);
drivers/gpu/drm/drm_ioctl.c +1 -0
@@ -653,6 +653,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {DRM_IOCTL_DEF (DRM_IOCTL_GEM_CLOSE , drm_gem_close_ioctl,DRM_RENDER_ALLOW ),DRM_IOCTL_DEF (DRM_IOCTL_GEM_FLINK , drm_gem_flink_ioctl,DRM_AUTH ),DRM_IOCTL_DEF (DRM_IOCTL_GEM_OPEN , drm_gem_open_ioctl,DRM_AUTH ), +DRM_IOCTL_DEF (DRM_IOCTL_GEM_CHANGE_HANDLE , drm_gem_change_handle_ioctl,DRM_RENDER_ALLOW ), DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),
drivers/gpu/drm/drm_prime.c +1 -5
@@ -93,7 +93,7 @@ struct drm_prime_member {struct rb_node handle_rb; }; -static int drm_prime_add_buf_handle (struct drm_prime_file_private *prime_fpriv, +int drm_prime_add_buf_handle (struct drm_prime_file_private *prime_fpriv,struct dma_buf *dma_buf,uint32_t handle) {struct drm_prime_member *member;
@@ -190,8 +190,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, {struct rb_node *rb; -mutex_lock (&prime_fpriv->lock); - rb = prime_fpriv->handles.rb_node;while (rb) {struct drm_prime_member *member;
@@ -210,8 +208,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, rb = rb->rb_left; } } - -mutex_unlock (&prime_fpriv->lock); }void drm_prime_init_file_private (struct drm_prime_file_private *prime_fpriv)
include/uapi/drm/drm.h +17 -0
@@ -625,6 +625,15 @@ struct drm_gem_open {__u64 size; }; +/* DRM_IOCTL_GEM_CHANGE_HANDLE ioctl argument type */ +struct drm_gem_change_handle { +/** Current handle of object */ +__u32 handle; + +/** Handle to change that object to */ +__u32 new_handle; +}; +/** * DRM_CAP_DUMB_BUFFER *
@@ -1305,6 +1314,14 @@ extern "C" {*/ #define DRM_IOCTL_SET_CLIENT_NAME DRM_IOWR(0xD1, struct drm_set_client_name) +/** +* DRM_IOCTL_GEM_CHANGE_HANDLE - Move an object to a different handle +* +* Some applications (notably CRIU) need objects to have specific gem handles. +* This ioctl changes the object at one gem handle to use a new gem handle. +*/ +#define DRM_IOCTL_GEM_CHANGE_HANDLE DRM_IOWR(0xD2, struct drm_gem_change_handle) +/* * Device specific ioctls should only be in their respective headers * The device specific ioctl range is from 0x40 to 0x9f.
Signature · 1 line
2.34.1
Re: [PATCH] drm: Add DRM prime interface to reassign GEM handle
Quoted 7 lines · David Francis
CRIU restore of drm buffer objects requires the ability to create or import a buffer object with a specific gem handle.
Add new drm ioctl
DRM_IOCTL_GEM_CHANGE_HANDLE, which takes the gem handle of an object and moves that object to a specified new gem handle.
Quoted 6 lines
This ioctl needs to call
drm_prime_remove_buf_handle, but that function acquires the prime lock, which the ioctl needs to hold for other purposes.Make
drm_prime_remove_buf_handlenot acquire the prime lock, and change its other caller to reflect this.
I think it'd be good to also link to the CRIU patches here so that the use-case for this is properly recorded in git history.
Quoted 1 line
Signed-off-by David Francis <[email protected]>
igt testcases for this would be needed too, see:
https://dri.freedesktop.org/docs/drm/gpu/drm-uapi.html#testing-requirements-for-userspace-api
Quoted 26 lines
--- drivers/gpu/drm/drm_gem.c | 61 ++++++++++++++++++++++++++++++++++ drivers/gpu/drm/drm_internal.h | 4 +++ drivers/gpu/drm/drm_ioctl.c | 1 + drivers/gpu/drm/drm_prime.c | 6 +--- include/uapi/drm/drm.h | 17 ++++++++++ 5 files changed, 84 insertions(+), 5 deletions(-)drivers/gpu/drm/drm_gem.c +5
-0@@ -282,7 +282,12 @@ drm_gem_object_release_handle(int id, void *ptr, void *data)if (obj->funcs->close) obj->funcs->close (obj, file_priv); +mutex_lock (&file_priv->prime.lock); +drm_prime_remove_buf_handle (&file_priv->prime, id); + +mutex_unlock (&file_priv->prime.lock); +drm_vma_node_revoke (&obj->vma_node, file_priv);drm_gem_object_handle_put_unlocked (obj);
drivers/gpu/drm/drm_gem.c drm_gem_flink_ioctl(struct drm_device *dev, void *data, · line 896
Quoted 4 lines
drivers/gpu/drm/drm_gem.c +1
-0@@ -888,6 +893,62 @@ drm_gem_flink_ioctl(struct drm_device *dev, void *data,return ret; } +/**
We generally don't kerneldoc document internal functions in drm that aren't exported to drivers. I think better to move this into the uapi header, which should be documented.
Quoted 128 lines
drivers/gpu/drm/drm_gem.c +55
-0+* drm_gem_change_handle_ioctl - implementation of the GEM_CHANGE_HANDLE ioctl +* @dev: drm_device +* @data: ioctl data +* @file_priv: drm file-private structure +* +* Change the handle of a GEM object to the specified one. +* The new handle must be unused. On success the old handle is closed +* and all further IOCTL should refer to the new handle only. +* Calls to DRM_IOCTL_PRIME_FD_TO_HANDLE will return the new handle. +*/ +int drm_gem_change_handle_ioctl (struct drm_device *dev,void *data, +struct drm_file *file_priv) +{ +struct drm_gem_change_handle *args = data; +struct drm_gem_object *obj; +int ret; + + obj =drm_gem_object_lookup (file_priv, args->handle); +if (!obj) +return -ENOENT ; + +if (args->handle == args->new_handle) +return 0 ; + +mutex_lock (&file_priv->prime.lock); + +spin_lock (&file_priv->table_lock); + ret =idr_alloc (&file_priv->object_idr, obj, args->new_handle, args->new_handle +1 ,GFP_NOWAIT ); +spin_unlock (&file_priv->table_lock); + +if (ret <0 ) + goto out_unlock; + +if (obj->dma_buf) { + ret =drm_prime_add_buf_handle (&file_priv->prime, obj->dma_buf, args->new_handle); +if (ret <0 ) { +spin_lock (&file_priv->table_lock); +idr_remove (&file_priv->object_idr, args->new_handle); +spin_unlock (&file_priv->table_lock); + goto out_unlock; + } + +drm_prime_remove_buf_handle (&file_priv->prime, args->handle); + } + +spin_lock (&file_priv->table_lock); +idr_remove (&file_priv->object_idr, args->handle); +spin_unlock (&file_priv->table_lock); + +out_unlock: +mutex_unlock (&file_priv->prime.lock); + +return ret; +} +/** * drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl * @dev: drm_device drivers/gpu/drm/drm_internal.h +4
-0@@ -85,6 +85,8 @@ int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,void drm_prime_init_file_private (struct drm_prime_file_private *prime_fpriv);void drm_prime_destroy_file_private (struct drm_prime_file_private *prime_fpriv); +int drm_prime_add_buf_handle (struct drm_prime_file_private *prime_fpriv, +struct dma_buf *dma_buf,uint32_t handle);void drm_prime_remove_buf_handle (struct drm_prime_file_private *prime_fpriv,uint32_t handle);@@ -168,6 +170,8 @@ int drm_gem_close_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);int drm_gem_flink_ioctl (struct drm_device *dev,void *data,struct drm_file *file_priv); +int drm_gem_change_handle_ioctl (struct drm_device *dev,void *data, +struct drm_file *file_priv);int drm_gem_open_ioctl (struct drm_device *dev,void *data,struct drm_file *file_priv);void drm_gem_open (struct drm_device *dev,struct drm_file *file_private);drivers/gpu/drm/drm_ioctl.c +1
-0@@ -653,6 +653,7 @@ static const struct drm_ioctl_desc drm_ioctls[] = {DRM_IOCTL_DEF (DRM_IOCTL_GEM_CLOSE , drm_gem_close_ioctl,DRM_RENDER_ALLOW ),DRM_IOCTL_DEF (DRM_IOCTL_GEM_FLINK , drm_gem_flink_ioctl,DRM_AUTH ),DRM_IOCTL_DEF (DRM_IOCTL_GEM_OPEN , drm_gem_open_ioctl,DRM_AUTH ), +DRM_IOCTL_DEF (DRM_IOCTL_GEM_CHANGE_HANDLE , drm_gem_change_handle_ioctl,DRM_RENDER_ALLOW ), DRM_IOCTL_DEF(DRM_IOCTL_MODE_GETRESOURCES, drm_mode_getresources, 0),drivers/gpu/drm/drm_prime.c +1
-5@@ -93,7 +93,7 @@ struct drm_prime_member {struct rb_node handle_rb; }; -static int drm_prime_add_buf_handle (struct drm_prime_file_private *prime_fpriv, +int drm_prime_add_buf_handle (struct drm_prime_file_private *prime_fpriv,struct dma_buf *dma_buf,uint32_t handle) {struct drm_prime_member *member;@@ -190,8 +190,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, {struct rb_node *rb; -mutex_lock (&prime_fpriv->lock); - rb = prime_fpriv->handles.rb_node;while (rb) {struct drm_prime_member *member;@@ -210,8 +208,6 @@ void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, rb = rb->rb_left; } } - -mutex_unlock (&prime_fpriv->lock); }void drm_prime_init_file_private (struct drm_prime_file_private *prime_fpriv)include/uapi/drm/drm.h +0
-0
include/uapi/drm/drm.h struct drm_gem_open { · line 629
Quoted 5 lines
include/uapi/drm/drm.h +2
-0@@ -625,6 +625,15 @@ struct drm_gem_open {__u64 size; }; +/* DRM_IOCTL_GEM_CHANGE_HANDLE ioctl argument type */ +struct drm_gem_change_handle {
Please give this struct a full blown kerneldoc comment that explains how it works and why you want to use this. Also link with the #define below and make sure it all renders correctly with make htmldocs.
Since you're touching this area I think I'd be really nice to also document the related gem ioctls so that the new docs aren't entirely floating around in vacuum.
Thanks, Sima
Quoted 28 lines
include/uapi/drm/drm.h +15
-0+/** Current handle of object */ +__u32 handle; + +/** Handle to change that object to */ +__u32 new_handle; +}; +/** * DRM_CAP_DUMB_BUFFER * @@ -1305,6 +1314,14 @@ extern "C" {*/ #define DRM_IOCTL_SET_CLIENT_NAME DRM_IOWR(0xD1, struct drm_set_client_name) +/** +* DRM_IOCTL_GEM_CHANGE_HANDLE - Move an object to a different handle +* +* Some applications (notably CRIU) need objects to have specific gem handles. +* This ioctl changes the object at one gem handle to use a new gem handle. +*/ +#define DRM_IOCTL_GEM_CHANGE_HANDLE DRM_IOWR(0xD2, struct drm_gem_change_handle) +/* * Device specific ioctls should only be in their respective headers * The device specific ioctl range is from 0x40 to 0x9f. Signature · 1 line
2.34.1
Signature · 3 lines
Simona Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch