Skip to content

Commit f9db1fc

Browse files
fdavid-amdalexdeucher
authored andcommitted
drm/amdgpu: Add ioctl to get all gem handles for a process
Add new ioctl DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES. This ioctl returns a list of bos with their handles, sizes, and flags and domains. This ioctl is meant to be used during CRIU checkpoint and provide information needed to reconstruct the bos in CRIU restore. Userspace for this and the next change can be found at checkpoint-restore/criu#2613 Signed-off-by: David Francis <David.Francis@amd.com> Reviewed-by: Christian König <christian.koenig@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
1 parent 0317e0e commit f9db1fc

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3051,6 +3051,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
30513051
DRM_IOCTL_DEF_DRV(AMDGPU_USERQ, amdgpu_userq_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
30523052
DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_SIGNAL, amdgpu_userq_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
30533053
DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
3054+
DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_HANDLES, amdgpu_gem_list_handles_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
30543055
};
30553056

30563057
static const struct drm_driver amdgpu_kms_driver = {

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,85 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
10241024
return r;
10251025
}
10261026

1027+
/**
1028+
* drm_amdgpu_gem_list_handles_ioctl - get information about a process' buffer objects
1029+
*
1030+
* @dev: drm device pointer
1031+
* @data: drm_amdgpu_gem_list_handles
1032+
* @filp: drm file pointer
1033+
*
1034+
* num_entries is set as an input to the size of the entries array.
1035+
* num_entries is sent back as output as the number of bos in the process.
1036+
* If that number is larger than the size of the array, the ioctl must
1037+
* be retried.
1038+
*
1039+
* Returns:
1040+
* 0 for success, -errno for errors.
1041+
*/
1042+
int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
1043+
struct drm_file *filp)
1044+
{
1045+
struct drm_amdgpu_gem_list_handles *args = data;
1046+
struct drm_amdgpu_gem_list_handles_entry *bo_entries;
1047+
struct drm_gem_object *gobj;
1048+
int id, ret = 0;
1049+
int bo_index = 0;
1050+
int num_bos = 0;
1051+
1052+
spin_lock(&filp->table_lock);
1053+
idr_for_each_entry(&filp->object_idr, gobj, id)
1054+
num_bos += 1;
1055+
spin_unlock(&filp->table_lock);
1056+
1057+
if (args->num_entries < num_bos) {
1058+
args->num_entries = num_bos;
1059+
return 0;
1060+
}
1061+
1062+
if (num_bos == 0) {
1063+
args->num_entries = 0;
1064+
return 0;
1065+
}
1066+
1067+
bo_entries = kvcalloc(num_bos, sizeof(*bo_entries), GFP_KERNEL);
1068+
if (!bo_entries)
1069+
return -ENOMEM;
1070+
1071+
spin_lock(&filp->table_lock);
1072+
idr_for_each_entry(&filp->object_idr, gobj, id) {
1073+
struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1074+
struct drm_amdgpu_gem_list_handles_entry *bo_entry;
1075+
1076+
if (bo_index >= num_bos) {
1077+
ret = -EAGAIN;
1078+
break;
1079+
}
1080+
1081+
bo_entry = &bo_entries[bo_index];
1082+
1083+
bo_entry->size = amdgpu_bo_size(bo);
1084+
bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
1085+
bo_entry->preferred_domains = bo->preferred_domains;
1086+
bo_entry->gem_handle = id;
1087+
bo_entry->alignment = bo->tbo.page_alignment;
1088+
1089+
if (bo->tbo.base.import_attach)
1090+
bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
1091+
1092+
bo_index += 1;
1093+
}
1094+
spin_unlock(&filp->table_lock);
1095+
1096+
args->num_entries = bo_index;
1097+
1098+
if (!ret)
1099+
ret = copy_to_user(u64_to_user_ptr(args->entries), bo_entries, num_bos * sizeof(*bo_entries));
1100+
1101+
kvfree(bo_entries);
1102+
1103+
return ret;
1104+
}
1105+
10271106
static int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
10281107
int width,
10291108
int cpp,

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
6767
struct drm_file *filp);
6868
int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
6969
struct drm_file *filp);
70+
int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
71+
struct drm_file *filp);
7072

7173
int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
7274
struct drm_file *filp);

include/uapi/drm/amdgpu_drm.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern "C" {
5757
#define DRM_AMDGPU_USERQ 0x16
5858
#define DRM_AMDGPU_USERQ_SIGNAL 0x17
5959
#define DRM_AMDGPU_USERQ_WAIT 0x18
60+
#define DRM_AMDGPU_GEM_LIST_HANDLES 0x19
6061

6162
#define DRM_IOCTL_AMDGPU_GEM_CREATE DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
6263
#define DRM_IOCTL_AMDGPU_GEM_MMAP DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -77,6 +78,7 @@ extern "C" {
7778
#define DRM_IOCTL_AMDGPU_USERQ DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ, union drm_amdgpu_userq)
7879
#define DRM_IOCTL_AMDGPU_USERQ_SIGNAL DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_SIGNAL, struct drm_amdgpu_userq_signal)
7980
#define DRM_IOCTL_AMDGPU_USERQ_WAIT DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
81+
#define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)
8082

8183
/**
8284
* DOC: memory domains
@@ -811,6 +813,38 @@ struct drm_amdgpu_gem_op {
811813
__u64 value;
812814
};
813815

816+
#define AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT (1 << 0)
817+
818+
struct drm_amdgpu_gem_list_handles {
819+
/* User pointer to array of drm_amdgpu_gem_bo_info_entry */
820+
__u64 entries;
821+
822+
/* Size of entries buffer / Number of handles in process (if larger than size of buffer, must retry) */
823+
__u32 num_entries;
824+
825+
__u32 padding;
826+
};
827+
828+
struct drm_amdgpu_gem_list_handles_entry {
829+
/* gem handle of buffer object */
830+
__u32 gem_handle;
831+
832+
/* Currently just one flag: IS_IMPORT */
833+
__u32 flags;
834+
835+
/* Size of bo */
836+
__u64 size;
837+
838+
/* Preferred domains for GEM_CREATE */
839+
__u64 preferred_domains;
840+
841+
/* GEM_CREATE flags for re-creation of buffer */
842+
__u64 alloc_flags;
843+
844+
/* physical start_addr alignment in bytes for some HW requirements */
845+
__u64 alignment;
846+
};
847+
814848
#define AMDGPU_VA_OP_MAP 1
815849
#define AMDGPU_VA_OP_UNMAP 2
816850
#define AMDGPU_VA_OP_CLEAR 3

0 commit comments

Comments
 (0)