Skip to contentSkip to conversation

Tiny steps towards amdgpu checkpoint and restore

RFC Superseded Tvrtko Ursulin 2 participants 12 messages May 26 – May 28

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Tiny steps towards amdgpu checkpoint and restore

I am sending this early with the main goal of getting a feel on how it will be received etc.

Cover letter is written mostly for people who already know what checkpoint and restore is, so I will just restate the high level goal - To be able to checkpoint and restore purely a rendernode process (so no kfd in the picture).

And for people not too familiar what it is, probably best to first read about it
here: https://criu.org/Main_Page.

There are three pieces of work here:

1.
This kernel series which adds new uapi to amdgpu.

2.
A new IGT test case which helped me find what doesn't work and to verify what I added.

https://cgit.freedesktop.org/~tursulin/intel-gpu-tools/log/?h=amd-criu

The IGT starts out adding some very basic tests (first commit) which are laid out in order of increasing complexity.

Following commits then add more tests and by the end, with all these series combined, they all pass apart from the forking subtest. But that one I am leaving out of scope for now.

(And to be clear, before this work, none of the tests cases can pass.)

3.
Changes to the amdgpu CRIU plugin which use the above uapi, among other changes.

https://github.com/tursulin/criu/pull/new/amdgpu-render-node-rfc

As a picture is worth a thousand words the best I can do is some from terminal pastes showing it all in action.

Terminal 1:

$ sudo ~/build-holo/tests/amdgpu/amd_criu --r busy-client-content
IGT-Version: 2.3-ge37a85b91 (x86_64) (Linux: 7.1.0-rc2-cfs x86_64)
Using IGT_SRANDOM=1779805687 for randomisation
Opened device: /dev/dri/renderD128
Starting subtest: busy-client-content
Start checkpointing within 10 seconds...

Now switch to terminal 2:

$ sudo /usr/local/sbin/criu dump -t `pgrep amd_criu | head -1` \
  -L /usr/local/lib/criu/ -vvv -o criu.log -j --link-remap --tcp-established \
  --file-locks --ext-unix-sk

Back to terminal 1:

...
Killed # This is normal - CRIU dump has saved and terminated the process

Back to terminal 2, lets restore it:

$ sudo /usr/local/sbin/criu restore  -L /usr/local/lib/criu/ -vvv \
  -o restore.log --shell-job --link-remap --tcp-established --file-locks 
  --ext-unix-sk
Subtest busy-client-content: SUCCESS (10.739s)

And that is it. Client which was busy looping submitting a SDMA_NOP IB was successfuly checkpointed and restored to completion. It both ran for the remainder of the indented duration and we checked buffer content was as expected at the end.

There is definitely more to do. Play more with exported buffers, syncobjs, fences, buffer objects lists, but for now, as said, I am looking for some early feedback etc.

Tvrtko Ursulin (5):
drm/amdgpu: Extend listing of buffer handles with the userptr object
flag
drm/amdgpu: Add a reserved VM ID query
drm/amdgpu: Add a new ioctl for listing client contexts drm/amdgpu: Add context handle renaming operation drm/amdgpu: Add driver managed buffer copy

 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 132 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 120 +++++++++++++++++++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   4 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  54 ++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |  26 ++++-
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c  |   4 +
 include/uapi/drm/amdgpu_drm.h           |  54 +++++++++-
 8 files changed, 365 insertions(+), 31 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

RFC 1/5 drm/amdgpu: Extend listing of buffer handles with the userptr object flag

Add a new flag (AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR) to the DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS ioctl.

This is required to allow for checkpoint and restore of userptr objects.

The existing struct drm_amdgpu_gem_list_handles_entry is used with the userptr creation flags places in alloc_flags, while the alignment field contains the userspace address.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 18 +++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 40 +++++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 19 +++++++++++-
 include/uapi/drm/amdgpu_drm.h           |  7 +++--
 4 files changed, 61 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12 -6
index 0259d81e5157..5eb8433229b4 100644
@@ -1186,14 +1186,20 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 
 		bo_entry = &bo_entries[bo_index];
 
-		bo_entry->size = amdgpu_bo_size(bo);
-		bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
-		bo_entry->preferred_domains = bo->preferred_domains;
 		bo_entry->gem_handle = id;
-		bo_entry->alignment = bo->tbo.page_alignment;
+		bo_entry->size = amdgpu_bo_size(bo);
+		if (amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) {
+			bo_entry->flags = AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR;
+			bo_entry->alloc_flags = amdgpu_ttm_tt_get_userptr_flags(bo->tbo.ttm);
+			bo_entry->alignment = amdgpu_ttm_tt_get_userptr_addr(bo->tbo.ttm);
+		} else {
+			bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
+			bo_entry->preferred_domains = bo->preferred_domains;
+			bo_entry->alignment = bo->tbo.page_alignment;
 
-		if (bo->tbo.base.import_attach)
-			bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
+			if (bo->tbo.base.import_attach)
+				bo_entry->flags = AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
+		}
 
 		bo_index += 1;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -13
index 3d2e00efc741..4023c84fe29d 100644
@@ -767,6 +767,33 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 	return r;
 }
 
+/*
+ * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
+ */
+bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	if (gtt == NULL || !gtt->userptr)
+		return false;
+
+	return true;
+}
+
+u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	return gtt->userflags;
+}
+
+u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	return gtt->userptr;
+}
+
 #endif
 
 /*
@@ -1374,19 +1401,6 @@ bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
 	return true;
 }
 
-/*
- * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
- */
-bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
-{
-	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
-
-	if (gtt == NULL || !gtt->userptr)
-		return false;
-
-	return true;
-}
-
 /*
  * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
  */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +18 -1
index f2f23a42b3cc..8c0424577dea 100644
@@ -211,12 +211,30 @@ uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 				 struct amdgpu_hmm_range *range);
+bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
+u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm);
+u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm);
 #else
 static inline int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 					       struct amdgpu_hmm_range *range)
 {
 	return -EPERM;
 }
+
+static inline bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
+{
+	return false;
+}
+
+static inline u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm)
+{
+	return -1;
+}
+
+static inline u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm)
+{
+	return 0;
+}
 #endif
 
 /**
@@ -251,7 +269,6 @@ bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
 				  unsigned long end, unsigned long *userptr);
 bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
 				       int *last_invalidated);
-bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem);
 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
include/uapi/drm/amdgpu_drm.h +4 -3
index 9f3090db2f16..3069560ac26e 100644
@@ -836,6 +836,7 @@ struct drm_amdgpu_gem_op {
 };
 
 #define AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT	(1 << 0)
+#define AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR	(1 << 1)
 
 struct drm_amdgpu_gem_list_handles {
 	/* User pointer to array of drm_amdgpu_gem_bo_info_entry */
@@ -851,7 +852,7 @@ struct drm_amdgpu_gem_list_handles_entry {
 	/* gem handle of buffer object */
 	__u32 gem_handle;
 
-	/* Currently just one flag: IS_IMPORT */
+	/* AMDGPU_GEM_LIST_HANDLES_FLAG_* */
 	__u32 flags;
 
 	/* Size of bo */
@@ -860,10 +861,10 @@ struct drm_amdgpu_gem_list_handles_entry {
 	/* Preferred domains for GEM_CREATE */
 	__u64 preferred_domains;
 
-	/* GEM_CREATE flags for re-creation of buffer */
+	/* GEM_CREATE flags for re-creation of buffer or drm_amdgpu_gem_userptr.flags */
 	__u64 alloc_flags;
 
-	/* physical start_addr alignment in bytes for some HW requirements */
+	/* physical start_addr alignment in bytes for some HW requirements or drm_amdgpu_gem_userptr.addr */
 	__u64 alignment;
 };
 
Signature · 1 line
2.54.0
maintainer
#2 lore raw

Re: [RFC 1/5] drm/amdgpu: Extend listing of buffer handles with the userptr object flag

Quoted 171 lines · Tvrtko Ursulin

Add a new flag (AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR) to the DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS ioctl.

This is required to allow for checkpoint and restore of userptr objects.

The existing struct drm_amdgpu_gem_list_handles_entry is used with the userptr creation flags places in alloc_flags, while the alignment field contains the userspace address.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 18 +++++++----
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 40 +++++++++++++++++--------
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 19 +++++++++++-
 include/uapi/drm/amdgpu_drm.h           |  7 +++--
 4 files changed, 61 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12 -6
index 0259d81e5157..5eb8433229b4 100644
@@ -1186,14 +1186,20 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 
 		bo_entry = &bo_entries[bo_index];
 
-		bo_entry->size = amdgpu_bo_size(bo);
-		bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
-		bo_entry->preferred_domains = bo->preferred_domains;
 		bo_entry->gem_handle = id;
-		bo_entry->alignment = bo->tbo.page_alignment;
+		bo_entry->size = amdgpu_bo_size(bo);
+		if (amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) {
+			bo_entry->flags = AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR;
+			bo_entry->alloc_flags = amdgpu_ttm_tt_get_userptr_flags(bo->tbo.ttm);
+			bo_entry->alignment = amdgpu_ttm_tt_get_userptr_addr(bo->tbo.ttm);
+		} else {
+			bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
+			bo_entry->preferred_domains = bo->preferred_domains;
+			bo_entry->alignment = bo->tbo.page_alignment;
 
-		if (bo->tbo.base.import_attach)
-			bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
+			if (bo->tbo.base.import_attach)
+				bo_entry->flags = AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
+		}
 
 		bo_index += 1;
 	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -13
index 3d2e00efc741..4023c84fe29d 100644
@@ -767,6 +767,33 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 	return r;
 }
 
+/*
+ * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
+ */
+bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	if (gtt == NULL || !gtt->userptr)
+		return false;
+
+	return true;
+}
+
+u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	return gtt->userflags;
+}
+
+u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	return gtt->userptr;
+}
+
 #endif
 
 /*
@@ -1374,19 +1401,6 @@ bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
 	return true;
 }
 
-/*
- * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
- */
-bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
-{
-	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
-
-	if (gtt == NULL || !gtt->userptr)
-		return false;
-
-	return true;
-}
-
 /*
  * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
  */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +18 -1
index f2f23a42b3cc..8c0424577dea 100644
@@ -211,12 +211,30 @@ uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 				 struct amdgpu_hmm_range *range);
+bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
+u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm);
+u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm);
 #else
 static inline int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 					       struct amdgpu_hmm_range *range)
 {
 	return -EPERM;
 }
+
+static inline bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
+{
+	return false;
+}
+
+static inline u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm)
+{
+	return -1;
+}
+
+static inline u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm)
+{
+	return 0;
+}
 #endif
 
 /**
@@ -251,7 +269,6 @@ bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
 				  unsigned long end, unsigned long *userptr);
 bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
 				       int *last_invalidated);
-bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem);
 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
include/uapi/drm/amdgpu_drm.h +2 -1
index 9f3090db2f16..3069560ac26e 100644
@@ -836,6 +836,7 @@ struct drm_amdgpu_gem_op {
 };
 
 #define AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT	(1 << 0)
+#define AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR	(1 << 1)
 
 struct drm_amdgpu_gem_list_handles {
 	/* User pointer to array of drm_amdgpu_gem_bo_info_entry */
@@ -851,7 +852,7 @@ struct drm_amdgpu_gem_list_handles_entry {
 	/* gem handle of buffer object */
 	__u32 gem_handle;
 
-	/* Currently just one flag: IS_IMPORT */
+	/* AMDGPU_GEM_LIST_HANDLES_FLAG_* */
 	__u32 flags;
 
 	/* Size of bo */

include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_gem_list_handles_entry { · line 868

Quoted 10 lines
include/uapi/drm/amdgpu_drm.h +2 -2
@@ -860,10 +861,10 @@ struct drm_amdgpu_gem_list_handles_entry {
 	/* Preferred domains for GEM_CREATE */
 	__u64 preferred_domains;
 
-	/* GEM_CREATE flags for re-creation of buffer */
+	/* GEM_CREATE flags for re-creation of buffer or drm_amdgpu_gem_userptr.flags */
 	__u64 alloc_flags;
 
-	/* physical start_addr alignment in bytes for some HW requirements */
+	/* physical start_addr alignment in bytes for some HW requirements or drm_amdgpu_gem_userptr.addr */
 	__u64 alignment;

Make that an union, e.g. union { __u64 alignment; __u64 userptr; }, apart looks pretty good to me.

Regards,
Christian.

Quoted 2 lines
include/uapi/drm/amdgpu_drm.h +0 -0
 };
 
#3 lore raw

Re: [RFC 1/5] drm/amdgpu: Extend listing of buffer handles with the userptr object flag

Quoted 179 lines · Christian König
Quoted 178 lines · Tvrtko Ursulin

Add a new flag (AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR) to the DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS ioctl.

This is required to allow for checkpoint and restore of userptr objects.

The existing struct drm_amdgpu_gem_list_handles_entry is used with the userptr creation flags places in alloc_flags, while the alignment field contains the userspace address.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 18 +++++++----
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 40 +++++++++++++++++--------
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 19 +++++++++++-
  include/uapi/drm/amdgpu_drm.h           |  7 +++--
  4 files changed, 61 insertions(+), 23 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12 -6
index 0259d81e5157..5eb8433229b4 100644
@@ -1186,14 +1186,20 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 
  		bo_entry = &bo_entries[bo_index];
 
-		bo_entry->size = amdgpu_bo_size(bo);
-		bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
-		bo_entry->preferred_domains = bo->preferred_domains;
  		bo_entry->gem_handle = id;
-		bo_entry->alignment = bo->tbo.page_alignment;
+		bo_entry->size = amdgpu_bo_size(bo);
+		if (amdgpu_ttm_tt_is_userptr(bo->tbo.ttm)) {
+			bo_entry->flags = AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR;
+			bo_entry->alloc_flags = amdgpu_ttm_tt_get_userptr_flags(bo->tbo.ttm);
+			bo_entry->alignment = amdgpu_ttm_tt_get_userptr_addr(bo->tbo.ttm);
+		} else {
+			bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
+			bo_entry->preferred_domains = bo->preferred_domains;
+			bo_entry->alignment = bo->tbo.page_alignment;
 
-		if (bo->tbo.base.import_attach)
-			bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
+			if (bo->tbo.base.import_attach)
+				bo_entry->flags = AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
+		}
 
  		bo_index += 1;
  	}
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +27 -13
index 3d2e00efc741..4023c84fe29d 100644
@@ -767,6 +767,33 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
  	return r;
  }
 
+/*
+ * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
+ */
+bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	if (gtt == NULL || !gtt->userptr)
+		return false;
+
+	return true;
+}
+
+u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	return gtt->userflags;
+}
+
+u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm)
+{
+	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
+
+	return gtt->userptr;
+}
+
  #endif
 
  /*
@@ -1374,19 +1401,6 @@ bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
  	return true;
  }
 
-/*
- * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
- */
-bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
-{
-	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
-
-	if (gtt == NULL || !gtt->userptr)
-		return false;
-
-	return true;
-}
-
  /*
   * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
   */
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +18 -1
index f2f23a42b3cc..8c0424577dea 100644
@@ -211,12 +211,30 @@ uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type);
  #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
  int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
  				 struct amdgpu_hmm_range *range);
+bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
+u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm);
+u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm);
  #else
  static inline int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
  					       struct amdgpu_hmm_range *range)
  {
  	return -EPERM;
  }
+
+static inline bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
+{
+	return false;
+}
+
+static inline u32 amdgpu_ttm_tt_get_userptr_flags(struct ttm_tt *ttm)
+{
+	return -1;
+}
+
+static inline u64 amdgpu_ttm_tt_get_userptr_addr(struct ttm_tt *ttm)
+{
+	return 0;
+}
  #endif
 
  /**
@@ -251,7 +269,6 @@ bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
  				  unsigned long end, unsigned long *userptr);
  bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
  				       int *last_invalidated);
-bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm);
  bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm);
  uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem);
  uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
include/uapi/drm/amdgpu_drm.h +3 -2
index 9f3090db2f16..3069560ac26e 100644
@@ -836,6 +836,7 @@ struct drm_amdgpu_gem_op {
  };
 
  #define AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT	(1 << 0)
+#define AMDGPU_GEM_LIST_HANDLES_FLAG_IS_USERPTR	(1 << 1)
 
  struct drm_amdgpu_gem_list_handles {
  	/* User pointer to array of drm_amdgpu_gem_bo_info_entry */
@@ -851,7 +852,7 @@ struct drm_amdgpu_gem_list_handles_entry {
  	/* gem handle of buffer object */
  	__u32 gem_handle;
 
-	/* Currently just one flag: IS_IMPORT */
+	/* AMDGPU_GEM_LIST_HANDLES_FLAG_* */
  	__u32 flags;
 
  	/* Size of bo */
@@ -860,10 +861,10 @@ struct drm_amdgpu_gem_list_handles_entry {
  	/* Preferred domains for GEM_CREATE */
  	__u64 preferred_domains;
 
-	/* GEM_CREATE flags for re-creation of buffer */
+	/* GEM_CREATE flags for re-creation of buffer or drm_amdgpu_gem_userptr.flags */
  	__u64 alloc_flags;
 
Quoted 5 lines

include/uapi/drm/amdgpu_drm.h struct drm_amdgpu_gem_list_handles_entry { · line 868

Quoted 3 lines
include/uapi/drm/amdgpu_drm.h +1 -1
-	/* physical start_addr alignment in bytes for some HW requirements */
+	/* physical start_addr alignment in bytes for some HW requirements or drm_amdgpu_gem_userptr.addr */
  	__u64 alignment;

Make that an union, e.g. union { __u64 alignment; __u64 userptr; }, apart looks pretty good to me.

Ack, marking as a TODO for a future respin, thank you!

Regards,

Tvrtko

#4 lore raw

RFC 2/5 drm/amdgpu: Add a reserved VM ID query

In reply to Tvrtko Ursulin

Add a new query (AMDGPU_VM_OP_QUERY_RESERVED_VMID) to the DRM_IOCTL_AMDGPU_VM ioctl.

This is required for the checkpoint and restore to be able to correctly restore the client VM.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 4 ++++
 include/uapi/drm/amdgpu_drm.h          | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
index 9ba9de16a27a..ce068e2d32d3 100644
@@ -2942,6 +2942,10 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 	case AMDGPU_VM_OP_UNRESERVE_VMID:
 		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
 		break;
+	case AMDGPU_VM_OP_QUERY_RESERVED_VMID:
+		if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
+			args->out.flags = AMDGPU_VM_OUT_FLAG_VMID_RESERVED;
+		break;
 	default:
 		return -EINVAL;
 	}
include/uapi/drm/amdgpu_drm.h +4 -1
index 3069560ac26e..8a6f4c558102 100644
@@ -587,6 +587,7 @@ struct drm_amdgpu_userq_wait {
 /* vm ioctl */
 #define AMDGPU_VM_OP_RESERVE_VMID	1
 #define AMDGPU_VM_OP_UNRESERVE_VMID	2
+#define AMDGPU_VM_OP_QUERY_RESERVED_VMID	3
 
 struct drm_amdgpu_vm_in {
 	/** AMDGPU_VM_OP_* */
@@ -594,8 +595,10 @@ struct drm_amdgpu_vm_in {
 	__u32	flags;
 };
 
+/* drm_amdgpu_vm_out flags */
+#define AMDGPU_VM_OUT_FLAG_VMID_RESERVED	(1ul << 63)
+
 struct drm_amdgpu_vm_out {
-	/** For future use, no flags defined so far */
 	__u64	flags;
 };
 
Signature · 1 line
2.54.0
maintainer
#5 lore raw

Re: [RFC 2/5] drm/amdgpu: Add a reserved VM ID query

Quoted 5 lines · Tvrtko Ursulin

Add a new query (AMDGPU_VM_OP_QUERY_RESERVED_VMID) to the DRM_IOCTL_AMDGPU_VM ioctl.

This is required for the checkpoint and restore to be able to correctly restore the client VM.

Well that's a complete corner cases for SPM. Why have you implemented that?

On the other hand the patch looks good, so we can probably keep it like it is.

Regards,
Christian.

Quoted 46 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 4 ++++
 include/uapi/drm/amdgpu_drm.h          | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
index 9ba9de16a27a..ce068e2d32d3 100644
@@ -2942,6 +2942,10 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
 	case AMDGPU_VM_OP_UNRESERVE_VMID:
 		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
 		break;
+	case AMDGPU_VM_OP_QUERY_RESERVED_VMID:
+		if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
+			args->out.flags = AMDGPU_VM_OUT_FLAG_VMID_RESERVED;
+		break;
 	default:
 		return -EINVAL;
 	}
include/uapi/drm/amdgpu_drm.h +4 -1
index 3069560ac26e..8a6f4c558102 100644
@@ -587,6 +587,7 @@ struct drm_amdgpu_userq_wait {
 /* vm ioctl */
 #define AMDGPU_VM_OP_RESERVE_VMID	1
 #define AMDGPU_VM_OP_UNRESERVE_VMID	2
+#define AMDGPU_VM_OP_QUERY_RESERVED_VMID	3
 
 struct drm_amdgpu_vm_in {
 	/** AMDGPU_VM_OP_* */
@@ -594,8 +595,10 @@ struct drm_amdgpu_vm_in {
 	__u32	flags;
 };
 
+/* drm_amdgpu_vm_out flags */
+#define AMDGPU_VM_OUT_FLAG_VMID_RESERVED	(1ul << 63)
+
 struct drm_amdgpu_vm_out {
-	/** For future use, no flags defined so far */
 	__u64	flags;
 };
 
#6 lore raw

Re: [RFC 2/5] drm/amdgpu: Add a reserved VM ID query

Quoted 8 lines · Christian König
Quoted 5 lines · Tvrtko Ursulin

Add a new query (AMDGPU_VM_OP_QUERY_RESERVED_VMID) to the DRM_IOCTL_AMDGPU_VM ioctl.

This is required for the checkpoint and restore to be able to correctly restore the client VM.

Well that's a complete corner cases for SPM. Why have you implemented that?

I wasn't sure what are the use cases but have just saw some low hanging uapi which can be easily handled. Now that you raised it, and I read about on the history of the feature, I agree it is not interesting to checkpoint and restore things under performance profiling. :)

Quoted 1 line

On the other hand the patch looks good, so we can probably keep it like it is.

Thanks, but if I don't hear otherwise I will drop it from a future re-spin since it really does sound like noise now.

Regards,

Tvrtko

Quoted 47 lines
Quoted 46 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c | 4 ++++
  include/uapi/drm/amdgpu_drm.h          | 5 ++++-
  2 files changed, 8 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +4 -0
index 9ba9de16a27a..ce068e2d32d3 100644
@@ -2942,6 +2942,10 @@ int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  	case AMDGPU_VM_OP_UNRESERVE_VMID:
  		amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0));
  		break;
+	case AMDGPU_VM_OP_QUERY_RESERVED_VMID:
+		if (vm->reserved_vmid[AMDGPU_GFXHUB(0)])
+			args->out.flags = AMDGPU_VM_OUT_FLAG_VMID_RESERVED;
+		break;
  	default:
  		return -EINVAL;
  	}
include/uapi/drm/amdgpu_drm.h +4 -1
index 3069560ac26e..8a6f4c558102 100644
@@ -587,6 +587,7 @@ struct drm_amdgpu_userq_wait {
  /* vm ioctl */
  #define AMDGPU_VM_OP_RESERVE_VMID	1
  #define AMDGPU_VM_OP_UNRESERVE_VMID	2
+#define AMDGPU_VM_OP_QUERY_RESERVED_VMID	3
 
  struct drm_amdgpu_vm_in {
  	/** AMDGPU_VM_OP_* */
@@ -594,8 +595,10 @@ struct drm_amdgpu_vm_in {
  	__u32	flags;
  };
 
+/* drm_amdgpu_vm_out flags */
+#define AMDGPU_VM_OUT_FLAG_VMID_RESERVED	(1ul << 63)
+
  struct drm_amdgpu_vm_out {
-	/** For future use, no flags defined so far */
  	__u64	flags;
  };
 
#7 lore raw

RFC 3/5 drm/amdgpu: Add a new ioctl for listing client contexts

In reply to Tvrtko Ursulin

Similar in principle to DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES, we need a new ioctl to list any contexts created by the client.

Lets add it as DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS, and the respective uapi data structures.

Structures contain all information required to re-create the context, modulo the handle id replication, which will be handled in a following patch.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 102 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   2 +
 include/uapi/drm/amdgpu_drm.h           |  33 ++++++++
 4 files changed, 138 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +102 -0
index 7af86a32c0c5..15bf247c38f2 100644
@@ -734,6 +734,108 @@ int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 	return r;
 }
 
+/**
+ * amdgpu_gem_list_contexts_ioctl - get information about clients contexts
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_gem_list_handles
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_gem_list_contexts_ioctl(struct drm_device *dev, void *data,
+				   struct drm_file *filp)
+{
+	struct drm_amdgpu_gem_list_contexts *args = data;
+	struct drm_amdgpu_gem_list_contexts_entry *contexts;
+	struct amdgpu_fpriv *fpriv = filp->driver_priv;
+	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
+	struct amdgpu_device *adev = mgr->adev;
+	unsigned long num = 0, idx = 0;
+	struct amdgpu_ctx *ctx;
+	int ret = 0;
+	u32 id;
+
+	if (args->padding)
+		return -EINVAL;
+
+	mutex_lock(&mgr->lock);
+	idr_for_each_entry(&mgr->ctx_handles, ctx, id)
+		num++;
+	mutex_unlock(&mgr->lock);
+
+	if (num == 0 || args->num_contexts < num) {
+		args->num_contexts = num;
+		return 0;
+	}
+
+	contexts = kvzalloc_objs(*contexts, num);
+	if (!contexts)
+		return -ENOMEM;
+
+	mutex_lock(&mgr->lock);
+	idr_for_each_entry(&mgr->ctx_handles, ctx, id) {
+		struct drm_amdgpu_gem_list_contexts_entry *context;
+		enum amd_dpm_forced_level level;
+
+		if (idx >= num) {
+			ret = -EAGAIN;
+			break;
+		}
+
+		context = &contexts[idx];
+
+		context->handle = id;
+		context->init_priority = ctx->init_priority;
+		context->override_priority = ctx->override_priority;
+		if (atomic_read(&ctx->guilty))
+			context->flags = AMDGPU_GEM_LIST_CONTEXTS_FLAG_GUILTY;
+
+		mutex_lock(&adev->pm.stable_pstate_ctx_lock);
+		if (ctx == adev->pm.stable_pstate_ctx) {
+			level = amdgpu_dpm_get_performance_level(adev);
+
+			switch (level) {
+			case AMD_DPM_FORCED_LEVEL_AUTO:
+				context->pstate_flags = AMDGPU_CTX_STABLE_PSTATE_NONE;
+				break;
+			case AMD_DPM_FORCED_LEVEL_PROFILE_STANDARD:
+				context->pstate_flags = AMDGPU_CTX_STABLE_PSTATE_STANDARD;
+				break;
+			case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_SCLK:
+				context->pstate_flags = AMDGPU_CTX_STABLE_PSTATE_MIN_SCLK;
+				break;
+			case AMD_DPM_FORCED_LEVEL_PROFILE_MIN_MCLK:
+				context->pstate_flags = AMDGPU_CTX_STABLE_PSTATE_MIN_MCLK;
+				break;
+			case AMD_DPM_FORCED_LEVEL_PROFILE_PEAK:
+				context->pstate_flags = AMDGPU_CTX_STABLE_PSTATE_PEAK;
+				break;
+			default:
+				ret = -EIO;
+				break;
+			};
+
+		}
+		mutex_unlock(&adev->pm.stable_pstate_ctx_lock);
+
+		idx++;
+	}
+	mutex_unlock(&mgr->lock);
+
+	args->num_contexts = idx;
+
+	if (!ret)
+		if (copy_to_user(u64_to_user_ptr(args->contexts), contexts,
+				 num * sizeof(*contexts)))
+			ret = -EFAULT;
+
+	kvfree(contexts);
+
+	return ret;
+}
+
 struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
 {
 	struct amdgpu_ctx *ctx;
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index 46aae3fad4bf..2b971de3c189 100644
@@ -3065,6 +3065,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_SIGNAL, amdgpu_userq_signal_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_HANDLES, amdgpu_gem_list_handles_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_CONTEXTS, amdgpu_gem_list_contexts_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +2 -0
index b558336bc4c6..0e17d9fc665f 100644
@@ -69,6 +69,8 @@ int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
 			struct drm_file *filp);
 int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 				  struct drm_file *filp);
+int amdgpu_gem_list_contexts_ioctl(struct drm_device *dev, void *data,
+				   struct drm_file *filp);
 
 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *filp);
include/uapi/drm/amdgpu_drm.h +33 -0
index 8a6f4c558102..15da4d9c44fd 100644
@@ -58,6 +58,7 @@ extern "C" {
 #define DRM_AMDGPU_USERQ_SIGNAL		0x17
 #define DRM_AMDGPU_USERQ_WAIT		0x18
 #define DRM_AMDGPU_GEM_LIST_HANDLES	0x19
+#define DRM_AMDGPU_GEM_LIST_CONTEXTS	0x20
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -79,6 +80,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_USERQ_SIGNAL	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_SIGNAL, struct drm_amdgpu_userq_signal)
 #define DRM_IOCTL_AMDGPU_USERQ_WAIT	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
 #define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)
+#define DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_CONTEXTS, struct drm_amdgpu_gem_list_contexts)
 
 /**
  * DOC: memory domains
@@ -871,6 +873,37 @@ struct drm_amdgpu_gem_list_handles_entry {
 	__u64 alignment;
 };
 
+#define AMDGPU_GEM_LIST_CONTEXTS_FLAG_GUILTY	(1 << 0)
+
+struct drm_amdgpu_gem_list_contexts {
+	/* User pointer to array of drm_amdgpu_gem_list_contexts_entry */
+	__u64 contexts;
+
+	/* Size of the contexts buffer / Number of contexts in the client (if larger than size of buffer, must retry) */
+	__u32 num_contexts;
+
+	__u32 padding;
+};
+
+struct drm_amdgpu_gem_list_contexts_entry {
+	/* gem context handle */
+	__u32 handle;
+
+	/* AMDGPU_GEM_LIST_CONTEXTS_FLAG_* */
+	__u32 flags;
+
+	/* context initial priority */
+	__s32 init_priority;
+
+	/* context override priority */
+	__s32 override_priority;
+
+	/* pstate flags */
+	__u32 pstate_flags;
+
+	__u32 padding;
+};
+
 #define AMDGPU_VA_OP_MAP			1
 #define AMDGPU_VA_OP_UNMAP			2
 #define AMDGPU_VA_OP_CLEAR			3
Signature · 1 line
2.54.0
#8 lore raw

RFC 4/5 drm/amdgpu: Add context handle renaming operation

In reply to Tvrtko Ursulin

Identical in principle to DRM_IOCTL_GEM_CHANGE_HANDLE, we need a method for renaming the driver specific context handles.

We add a new operation (AMDGPU_CTX_OP_CHANGE_HANDLE) to the DRM_IOCTL_AMDGPU_CTX ioctl.

Source handle is given in the existing input handle field, while the destination handle needs to be passed in the existing flags field.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c | 30 +++++++++++++++++++++++++
 include/uapi/drm/amdgpu_drm.h           |  1 +
 2 files changed, 31 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +30 -0
index 15bf247c38f2..01a74eb6e721 100644
@@ -541,6 +541,33 @@ static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
 	return ctx ? 0 : -EINVAL;
 }
 
+static int amdgpu_ctx_change_handle(struct amdgpu_fpriv *fpriv,
+				    uint32_t old, uint32_t id)
+{
+	struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
+	struct amdgpu_ctx *ctx;
+	int r = -EINVAL;
+
+	if (!old || !id || old == id)
+		return -EINVAL;
+
+	mutex_lock(&mgr->lock);
+
+	ctx = idr_find(&mgr->ctx_handles, old);
+	if (!ctx)
+		goto out;
+
+	r = idr_alloc(&mgr->ctx_handles, ctx, id, id + 1, GFP_KERNEL);
+	if (r < 0)
+		goto out;
+
+	idr_remove(&mgr->ctx_handles, old);
+
+out:
+	mutex_unlock(&mgr->lock);
+	return r;
+}
+
 static int amdgpu_ctx_query(struct amdgpu_device *adev,
 			    struct amdgpu_fpriv *fpriv, uint32_t id,
 			    union drm_amdgpu_ctx_out *out)
@@ -727,6 +754,9 @@ int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
 			return -EINVAL;
 		r = amdgpu_ctx_stable_pstate(adev, fpriv, id, true, &stable_pstate);
 		break;
+	case AMDGPU_CTX_OP_CHANGE_HANDLE:
+		r = amdgpu_ctx_change_handle(fpriv, id, args->in.flags);
+		break;
 	default:
 		return -EINVAL;
 	}
include/uapi/drm/amdgpu_drm.h +1 -0
index 15da4d9c44fd..d2a7b4e9daed 100644
@@ -251,6 +251,7 @@ union drm_amdgpu_bo_list {
 #define AMDGPU_CTX_OP_QUERY_STATE2	4
 #define AMDGPU_CTX_OP_GET_STABLE_PSTATE	5
 #define AMDGPU_CTX_OP_SET_STABLE_PSTATE	6
+#define AMDGPU_CTX_OP_CHANGE_HANDLE	7
 
 /* GPU reset status */
 #define AMDGPU_CTX_NO_RESET		0
Signature · 1 line
2.54.0
#9 lore raw

RFC 5/5 drm/amdgpu: Add driver managed buffer copy

In reply to Tvrtko Ursulin

Saving and restoring buffer object content poses a challenge for the checkpoint and restore process for at least two reasons.

For example not all objects can be exported as dma-buf to enable copying from a separate client context, neither can any objects be easily copied from the same context since the injected CRIU code is unaware of the GPU virtual memory free and allocated ranges.

Lets bypass both problems by simply exposing access to the alrady present fast kernel copy via a new DRM_IOCTL_AMDGPU_GEM_COPY_BUFFER ioctl.

By giving the kernel simply the source and destination handles it is able to copy them without the need for objects to be mapped, or shared with a separate client.

For now this is implemented fully synchronous but can easily be extended for more parallelsim.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 102 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  14 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |   7 ++
 include/uapi/drm/amdgpu_drm.h           |   8 ++
 6 files changed, 127 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index 2b971de3c189..b927e3a3089e 100644
@@ -3066,6 +3066,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_HANDLES, amdgpu_gem_list_handles_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_CONTEXTS, amdgpu_gem_list_contexts_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_COPY_BUFFER, amdgpu_gem_copy_buffer_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +102 -0

108 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +2 -0
index 0e17d9fc665f..64529680fdad 100644
@@ -71,6 +71,8 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 				  struct drm_file *filp);
 int amdgpu_gem_list_contexts_ioctl(struct drm_device *dev, void *data,
 				   struct drm_file *filp);
+int amdgpu_gem_copy_buffer_ioctl(struct drm_device *dev, void *data,
+				 struct drm_file *filp);
 
 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *filp);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -7
index 4023c84fe29d..3baad073ed54 100644
@@ -294,13 +294,13 @@ static int amdgpu_ttm_map_buffer(struct amdgpu_ttm_buffer_entity *entity,
  *
  */
 __attribute__((nonnull))
-static int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
-				      struct amdgpu_ttm_buffer_entity *entity,
-				      const struct amdgpu_copy_mem *src,
-				      const struct amdgpu_copy_mem *dst,
-				      uint64_t size, bool tmz,
-				      struct dma_resv *resv,
-				      struct dma_fence **f)
+int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
+			       struct amdgpu_ttm_buffer_entity *entity,
+			       const struct amdgpu_copy_mem *src,
+			       const struct amdgpu_copy_mem *dst,
+			       uint64_t size, bool tmz,
+			       struct dma_resv *resv,
+			       struct dma_fence **f)
 {
 	struct amdgpu_res_cursor src_mm, dst_mm;
 	struct dma_fence *fence = NULL;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +7 -0
index 8c0424577dea..360a860b8280 100644
@@ -286,4 +286,11 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 				    enum dma_data_direction dir,
 				    struct sg_table *sgt);
 
+int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
+			       struct amdgpu_ttm_buffer_entity *entity,
+			       const struct amdgpu_copy_mem *src,
+			       const struct amdgpu_copy_mem *dst,
+			       uint64_t size, bool tmz,
+			       struct dma_resv *resv,
+			       struct dma_fence **f);
 #endif
include/uapi/drm/amdgpu_drm.h +8 -0
index d2a7b4e9daed..1335a181eb39 100644
@@ -59,6 +59,7 @@ extern "C" {
 #define DRM_AMDGPU_USERQ_WAIT		0x18
 #define DRM_AMDGPU_GEM_LIST_HANDLES	0x19
 #define DRM_AMDGPU_GEM_LIST_CONTEXTS	0x20
+#define DRM_AMDGPU_GEM_COPY_BUFFER	0x21
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -81,6 +82,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_USERQ_WAIT	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
 #define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)
 #define DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_CONTEXTS, struct drm_amdgpu_gem_list_contexts)
+#define DRM_IOCTL_AMDGPU_GEM_COPY_BUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_COPY_BUFFER, struct drm_amdgpu_gem_copy_buffer)
 
 /**
  * DOC: memory domains
@@ -206,6 +208,12 @@ union drm_amdgpu_gem_create {
 	struct drm_amdgpu_gem_create_out	out;
 };
 
+struct drm_amdgpu_gem_copy_buffer {
+	__u32	src_handle;
+	__u32	dst_handle;
+	__u64	flags;
+};
+
 /** Opcode to create new residency list.  */
 #define AMDGPU_BO_LIST_OP_CREATE	0
 /** Opcode to destroy previously created residency list */
Signature · 1 line
2.54.0
maintainer
#10 lore raw

Re: [RFC 5/5] drm/amdgpu: Add driver managed buffer copy

Quoted 10 lines · Tvrtko Ursulin

Saving and restoring buffer object content poses a challenge for the checkpoint and restore process for at least two reasons.

For example not all objects can be exported as dma-buf to enable copying from a separate client context, neither can any objects be easily copied from the same context since the injected CRIU code is unaware of the GPU virtual memory free and allocated ranges.

Lets bypass both problems by simply exposing access to the alrady present fast kernel copy via a new DRM_IOCTL_AMDGPU_GEM_COPY_BUFFER ioctl.

Oh nice, that was on the TODO list for KFD integration as well.

Quoted 6 lines

By giving the kernel simply the source and destination handles it is able to copy them without the need for objects to be mapped, or shared with a separate client.

For now this is implemented fully synchronous but can easily be extended for more parallelsim.

Looks reasonable of hand but I would rather like to have an output DMA-fence (drm_syncobj) instead of the dma_fence_wait().

Regards,
Christian.

Quoted 223 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 102 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  14 ++--
 drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |   7 ++
 include/uapi/drm/amdgpu_drm.h           |   8 ++
 6 files changed, 127 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index 2b971de3c189..b927e3a3089e 100644
@@ -3066,6 +3066,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_HANDLES, amdgpu_gem_list_handles_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_CONTEXTS, amdgpu_gem_list_contexts_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_COPY_BUFFER, amdgpu_gem_copy_buffer_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
 };
 
 static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +102 -0
index 5eb8433229b4..4e0440cd6ee2 100644
@@ -1216,6 +1216,108 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 	return ret;
 }
 
+/**
+ * amdgpu_gem_copy_buffer_ioctl - copy buffer object content
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_gem_copy_buffer
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_gem_copy_buffer_ioctl(struct drm_device *dev, void *data,
+				 struct drm_file *filp)
+{
+	struct amdgpu_copy_mem src_mem = {}, dst_mem = {};
+	struct drm_amdgpu_gem_copy_buffer *args = data;
+	struct amdgpu_device *adev = drm_to_adev(dev);
+	struct drm_gem_object *src_gobj, *dst_gobj;
+	struct amdgpu_bo *src_bo, *dst_bo;
+	struct dma_fence *fence = NULL;
+	struct drm_exec exec;
+	unsigned int e;
+	long timeout;
+	int r;
+
+	if (args->flags)
+		return -EINVAL;
+
+	src_gobj = drm_gem_object_lookup(filp, args->src_handle);
+	if (!src_gobj)
+		return -ENOENT;
+
+	dst_gobj = drm_gem_object_lookup(filp, args->dst_handle);
+	if (!dst_gobj) {
+		r = -ENOENT;
+		goto err_dst;
+	}
+
+	src_bo = gem_to_amdgpu_bo(src_gobj);
+	dst_bo = gem_to_amdgpu_bo(dst_gobj);
+
+	if (amdgpu_bo_size(src_bo) < amdgpu_bo_size(dst_bo)) {
+		r = -E2BIG;
+		goto err_sizes;
+	}
+
+	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
+
+	drm_exec_until_all_locked(&exec) {
+		r = drm_exec_prepare_obj(&exec, &src_bo->tbo.base, 1);
+		drm_exec_retry_on_contention(&exec);
+		if (r)
+			goto err_unlock;
+
+		r = drm_exec_prepare_obj(&exec, &dst_bo->tbo.base, 1);
+		drm_exec_retry_on_contention(&exec);
+		if (r)
+			goto err_unlock;
+	}
+
+	src_mem.bo = &src_bo->tbo;
+	src_mem.mem = src_bo->tbo.resource;
+	dst_mem.bo = &dst_bo->tbo;
+	dst_mem.mem = dst_bo->tbo.resource;
+	e = atomic_inc_return(&adev->mman.next_move_entity) %
+			      adev->mman.num_move_entities;
+	r = amdgpu_ttm_copy_mem_to_mem(adev, &adev->mman.move_entities[e],
+				       &src_mem, &dst_mem,
+				       amdgpu_bo_size(src_bo),
+				       amdgpu_bo_encrypted(src_bo),
+				       dst_bo->tbo.base.resv, &fence);
+	if (r) {
+		goto err_unlock;
+	} else if (!fence) {
+		r = -ENXIO;
+		goto err_unlock;
+	}
+
+	dma_resv_add_fence(src_bo->tbo.base.resv, fence, DMA_RESV_USAGE_READ);
+	dma_resv_add_fence(dst_bo->tbo.base.resv, fence, DMA_RESV_USAGE_WRITE);
+	dma_fence_put(fence);
+	drm_exec_fini(&exec);
+
+	timeout = dma_resv_wait_timeout(dst_bo->tbo.base.resv,
+					DMA_RESV_USAGE_WRITE, true,
+					MAX_SCHEDULE_TIMEOUT);
+	if (timeout < 0)
+		return timeout;
+	else if (timeout == 0)
+		return -ETIME;
+	else
+		goto out_put;
+
+err_unlock:
+	drm_exec_fini(&exec);
+out_put:
+err_sizes:
+	drm_gem_object_put(dst_gobj);
+err_dst:
+	drm_gem_object_put(src_gobj);
+	return r;
+}
+
 static int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
 				  int width,
 				  int cpp,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +2 -0
index 0e17d9fc665f..64529680fdad 100644
@@ -71,6 +71,8 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
 				  struct drm_file *filp);
 int amdgpu_gem_list_contexts_ioctl(struct drm_device *dev, void *data,
 				   struct drm_file *filp);
+int amdgpu_gem_copy_buffer_ioctl(struct drm_device *dev, void *data,
+				 struct drm_file *filp);
 
 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *filp);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -7
index 4023c84fe29d..3baad073ed54 100644
@@ -294,13 +294,13 @@ static int amdgpu_ttm_map_buffer(struct amdgpu_ttm_buffer_entity *entity,
  *
  */
 __attribute__((nonnull))
-static int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
-				      struct amdgpu_ttm_buffer_entity *entity,
-				      const struct amdgpu_copy_mem *src,
-				      const struct amdgpu_copy_mem *dst,
-				      uint64_t size, bool tmz,
-				      struct dma_resv *resv,
-				      struct dma_fence **f)
+int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
+			       struct amdgpu_ttm_buffer_entity *entity,
+			       const struct amdgpu_copy_mem *src,
+			       const struct amdgpu_copy_mem *dst,
+			       uint64_t size, bool tmz,
+			       struct dma_resv *resv,
+			       struct dma_fence **f)
 {
 	struct amdgpu_res_cursor src_mm, dst_mm;
 	struct dma_fence *fence = NULL;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +7 -0
index 8c0424577dea..360a860b8280 100644
@@ -286,4 +286,11 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
 				    enum dma_data_direction dir,
 				    struct sg_table *sgt);
 
+int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
+			       struct amdgpu_ttm_buffer_entity *entity,
+			       const struct amdgpu_copy_mem *src,
+			       const struct amdgpu_copy_mem *dst,
+			       uint64_t size, bool tmz,
+			       struct dma_resv *resv,
+			       struct dma_fence **f);
 #endif
include/uapi/drm/amdgpu_drm.h +8 -0
index d2a7b4e9daed..1335a181eb39 100644
@@ -59,6 +59,7 @@ extern "C" {
 #define DRM_AMDGPU_USERQ_WAIT		0x18
 #define DRM_AMDGPU_GEM_LIST_HANDLES	0x19
 #define DRM_AMDGPU_GEM_LIST_CONTEXTS	0x20
+#define DRM_AMDGPU_GEM_COPY_BUFFER	0x21
 
 #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
 #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -81,6 +82,7 @@ extern "C" {
 #define DRM_IOCTL_AMDGPU_USERQ_WAIT	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
 #define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)
 #define DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_CONTEXTS, struct drm_amdgpu_gem_list_contexts)
+#define DRM_IOCTL_AMDGPU_GEM_COPY_BUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_COPY_BUFFER, struct drm_amdgpu_gem_copy_buffer)
 
 /**
  * DOC: memory domains
@@ -206,6 +208,12 @@ union drm_amdgpu_gem_create {
 	struct drm_amdgpu_gem_create_out	out;
 };
 
+struct drm_amdgpu_gem_copy_buffer {
+	__u32	src_handle;
+	__u32	dst_handle;
+	__u64	flags;
+};
+
 /** Opcode to create new residency list.  */
 #define AMDGPU_BO_LIST_OP_CREATE	0
 /** Opcode to destroy previously created residency list */
#11 lore raw

Re: [RFC 5/5] drm/amdgpu: Add driver managed buffer copy

Quoted 9 lines · Christian König
Quoted 8 lines · Tvrtko Ursulin

Saving and restoring buffer object content poses a challenge for the checkpoint and restore process for at least two reasons.

For example not all objects can be exported as dma-buf to enable copying from a separate client context, neither can any objects be easily copied from the same context since the injected CRIU code is unaware of the GPU virtual memory free and allocated ranges.

Quoted 4 lines
Quoted 2 lines

Lets bypass both problems by simply exposing access to the alrady present fast kernel copy via a new DRM_IOCTL_AMDGPU_GEM_COPY_BUFFER ioctl.

Oh nice, that was on the TODO list for KFD integration as well.

That's good to hear! I was worried this one could be shot down in flames.

Quoted 8 lines
Quoted 6 lines

By giving the kernel simply the source and destination handles it is able to copy them without the need for objects to be mapped, or shared with a separate client.

For now this is implemented fully synchronous but can easily be extended for more parallelsim.

Looks reasonable of hand but I would rather like to have an output DMA-fence (drm_syncobj) instead of the dma_fence_wait().

I opted for simplicity for the RFC but I can certainly add an output fence. I guess that way userspace could implement parallel/pipelined save/restore for maximum performance.

Marking as TODO for RFC v2.

Regards,

Tvrtko

Quoted 223 lines
Quoted 222 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 102 ++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   2 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c |  14 ++--
  drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h |   7 ++
  include/uapi/drm/amdgpu_drm.h           |   8 ++
  6 files changed, 127 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index 2b971de3c189..b927e3a3089e 100644
@@ -3066,6 +3066,7 @@ const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
  	DRM_IOCTL_DEF_DRV(AMDGPU_USERQ_WAIT, amdgpu_userq_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_HANDLES, amdgpu_gem_list_handles_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_LIST_CONTEXTS, amdgpu_gem_list_contexts_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_COPY_BUFFER, amdgpu_gem_copy_buffer_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  };
 
  static const struct drm_driver amdgpu_kms_driver = {
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +102 -0
index 5eb8433229b4..4e0440cd6ee2 100644
@@ -1216,6 +1216,108 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
  	return ret;
  }
 
+/**
+ * amdgpu_gem_copy_buffer_ioctl - copy buffer object content
+ *
+ * @dev: drm device pointer
+ * @data: drm_amdgpu_gem_copy_buffer
+ * @filp: drm file pointer
+ *
+ * Returns:
+ * 0 for success, -errno for errors.
+ */
+int amdgpu_gem_copy_buffer_ioctl(struct drm_device *dev, void *data,
+				 struct drm_file *filp)
+{
+	struct amdgpu_copy_mem src_mem = {}, dst_mem = {};
+	struct drm_amdgpu_gem_copy_buffer *args = data;
+	struct amdgpu_device *adev = drm_to_adev(dev);
+	struct drm_gem_object *src_gobj, *dst_gobj;
+	struct amdgpu_bo *src_bo, *dst_bo;
+	struct dma_fence *fence = NULL;
+	struct drm_exec exec;
+	unsigned int e;
+	long timeout;
+	int r;
+
+	if (args->flags)
+		return -EINVAL;
+
+	src_gobj = drm_gem_object_lookup(filp, args->src_handle);
+	if (!src_gobj)
+		return -ENOENT;
+
+	dst_gobj = drm_gem_object_lookup(filp, args->dst_handle);
+	if (!dst_gobj) {
+		r = -ENOENT;
+		goto err_dst;
+	}
+
+	src_bo = gem_to_amdgpu_bo(src_gobj);
+	dst_bo = gem_to_amdgpu_bo(dst_gobj);
+
+	if (amdgpu_bo_size(src_bo) < amdgpu_bo_size(dst_bo)) {
+		r = -E2BIG;
+		goto err_sizes;
+	}
+
+	drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
+
+	drm_exec_until_all_locked(&exec) {
+		r = drm_exec_prepare_obj(&exec, &src_bo->tbo.base, 1);
+		drm_exec_retry_on_contention(&exec);
+		if (r)
+			goto err_unlock;
+
+		r = drm_exec_prepare_obj(&exec, &dst_bo->tbo.base, 1);
+		drm_exec_retry_on_contention(&exec);
+		if (r)
+			goto err_unlock;
+	}
+
+	src_mem.bo = &src_bo->tbo;
+	src_mem.mem = src_bo->tbo.resource;
+	dst_mem.bo = &dst_bo->tbo;
+	dst_mem.mem = dst_bo->tbo.resource;
+	e = atomic_inc_return(&adev->mman.next_move_entity) %
+			      adev->mman.num_move_entities;
+	r = amdgpu_ttm_copy_mem_to_mem(adev, &adev->mman.move_entities[e],
+				       &src_mem, &dst_mem,
+				       amdgpu_bo_size(src_bo),
+				       amdgpu_bo_encrypted(src_bo),
+				       dst_bo->tbo.base.resv, &fence);
+	if (r) {
+		goto err_unlock;
+	} else if (!fence) {
+		r = -ENXIO;
+		goto err_unlock;
+	}
+
+	dma_resv_add_fence(src_bo->tbo.base.resv, fence, DMA_RESV_USAGE_READ);
+	dma_resv_add_fence(dst_bo->tbo.base.resv, fence, DMA_RESV_USAGE_WRITE);
+	dma_fence_put(fence);
+	drm_exec_fini(&exec);
+
+	timeout = dma_resv_wait_timeout(dst_bo->tbo.base.resv,
+					DMA_RESV_USAGE_WRITE, true,
+					MAX_SCHEDULE_TIMEOUT);
+	if (timeout < 0)
+		return timeout;
+	else if (timeout == 0)
+		return -ETIME;
+	else
+		goto out_put;
+
+err_unlock:
+	drm_exec_fini(&exec);
+out_put:
+err_sizes:
+	drm_gem_object_put(dst_gobj);
+err_dst:
+	drm_gem_object_put(src_gobj);
+	return r;
+}
+
  static int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
  				  int width,
  				  int cpp,
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h +2 -0
index 0e17d9fc665f..64529680fdad 100644
@@ -71,6 +71,8 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
  				  struct drm_file *filp);
  int amdgpu_gem_list_contexts_ioctl(struct drm_device *dev, void *data,
  				   struct drm_file *filp);
+int amdgpu_gem_copy_buffer_ioctl(struct drm_device *dev, void *data,
+				 struct drm_file *filp);
 
  int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
  				struct drm_file *filp);
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +7 -7
index 4023c84fe29d..3baad073ed54 100644
@@ -294,13 +294,13 @@ static int amdgpu_ttm_map_buffer(struct amdgpu_ttm_buffer_entity *entity,
   *
   */
  __attribute__((nonnull))
-static int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
-				      struct amdgpu_ttm_buffer_entity *entity,
-				      const struct amdgpu_copy_mem *src,
-				      const struct amdgpu_copy_mem *dst,
-				      uint64_t size, bool tmz,
-				      struct dma_resv *resv,
-				      struct dma_fence **f)
+int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
+			       struct amdgpu_ttm_buffer_entity *entity,
+			       const struct amdgpu_copy_mem *src,
+			       const struct amdgpu_copy_mem *dst,
+			       uint64_t size, bool tmz,
+			       struct dma_resv *resv,
+			       struct dma_fence **f)
  {
  	struct amdgpu_res_cursor src_mm, dst_mm;
  	struct dma_fence *fence = NULL;
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h +7 -0
index 8c0424577dea..360a860b8280 100644
@@ -286,4 +286,11 @@ void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev,
  				    enum dma_data_direction dir,
  				    struct sg_table *sgt);
 
+int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
+			       struct amdgpu_ttm_buffer_entity *entity,
+			       const struct amdgpu_copy_mem *src,
+			       const struct amdgpu_copy_mem *dst,
+			       uint64_t size, bool tmz,
+			       struct dma_resv *resv,
+			       struct dma_fence **f);
  #endif
include/uapi/drm/amdgpu_drm.h +8 -0
index d2a7b4e9daed..1335a181eb39 100644
@@ -59,6 +59,7 @@ extern "C" {
  #define DRM_AMDGPU_USERQ_WAIT		0x18
  #define DRM_AMDGPU_GEM_LIST_HANDLES	0x19
  #define DRM_AMDGPU_GEM_LIST_CONTEXTS	0x20
+#define DRM_AMDGPU_GEM_COPY_BUFFER	0x21
 
  #define DRM_IOCTL_AMDGPU_GEM_CREATE	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_CREATE, union drm_amdgpu_gem_create)
  #define DRM_IOCTL_AMDGPU_GEM_MMAP	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_MMAP, union drm_amdgpu_gem_mmap)
@@ -81,6 +82,7 @@ extern "C" {
  #define DRM_IOCTL_AMDGPU_USERQ_WAIT	DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_USERQ_WAIT, struct drm_amdgpu_userq_wait)
  #define DRM_IOCTL_AMDGPU_GEM_LIST_HANDLES DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_HANDLES, struct drm_amdgpu_gem_list_handles)
  #define DRM_IOCTL_AMDGPU_GEM_LIST_CONTEXTS DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_LIST_CONTEXTS, struct drm_amdgpu_gem_list_contexts)
+#define DRM_IOCTL_AMDGPU_GEM_COPY_BUFFER DRM_IOW(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_COPY_BUFFER, struct drm_amdgpu_gem_copy_buffer)
 
  /**
   * DOC: memory domains
@@ -206,6 +208,12 @@ union drm_amdgpu_gem_create {
  	struct drm_amdgpu_gem_create_out	out;
  };
 
+struct drm_amdgpu_gem_copy_buffer {
+	__u32	src_handle;
+	__u32	dst_handle;
+	__u64	flags;
+};
+
  /** Opcode to create new residency list.  */
  #define AMDGPU_BO_LIST_OP_CREATE	0
  /** Opcode to destroy previously created residency list */