Skip to contentSkip to conversation

Tiny steps towards amdgpu checkpoint and restore

RFC Superseded Tvrtko Ursulin 2 participants 12 messages Jun 23 – Jul 3

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 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.

v2:
* Dropped the reserved VM ID query.
* Made userptr address an explicit union in the bo list entry uapi.
* Changed copy buffer from synchronous to providing a fence to userspace.
* Rebased for removal of ctx->guilty.

Tvrtko Ursulin (4):
drm/amdgpu: Extend listing of buffer handles with the userptr object
flag
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 | 130 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   2 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 122 ++++++++++++++++++++--
 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 ++++-
 include/uapi/drm/amdgpu_drm.h           |  62 ++++++++++-
 7 files changed, 369 insertions(+), 31 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

RFC v2 1/4 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           | 14 ++++++---
 4 files changed, 67 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12 -6
index 862fbc2e125f..e75026aba874 100644
@@ -1204,14 +1204,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->userptr = 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 16c060badaee..7ce9e0e194c3 100644
@@ -785,6 +785,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
 
 /*
@@ -1392,19 +1419,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 2d72fa217274..e1b3495b902f 100644
@@ -209,12 +209,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
 
 /**
@@ -249,7 +267,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 +10 -4
index 9f3090db2f16..24f2dffc0789 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,11 +861,16 @@ 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 */
-	__u64 alignment;
+	union {
+		/* physical start_addr alignment in bytes for some HW requirements */
+		__u64 alignment;
+
+		/* drm_amdgpu_gem_userptr.addr for userptr objects */
+		__u64 userptr;
+	};
 };
 
 #define AMDGPU_VA_OP_MAP			1
Signature · 1 line
2.54.0
maintainer
#2 lore raw

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

Quoted 54 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           | 14 ++++++---
 4 files changed, 67 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12 -6
index 862fbc2e125f..e75026aba874 100644
@@ -1204,14 +1204,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->userptr = 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 +0 -0
index 16c060badaee..7ce9e0e194c3 100644
@@ -785,6 +785,33 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
 	return r;

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, · line 795

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
 }
 
+/*
+ * 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)

That should actually check for usertask and not userptr.

Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
+		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);
+

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, · line 814

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+	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;
+}
+

This is identical to the existing amdgpu_ttm_tt_get_userptr() function, just without error handling.

I suggest to just extend amdgpu_ttm_tt_get_userptr() to also return the flags.

Apart from that the patch looks good to me of hand.

Regards,
Christian.

Quoted 107 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -13
 #endif
 
 /*
@@ -1392,19 +1419,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 2d72fa217274..e1b3495b902f 100644
@@ -209,12 +209,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
 
 /**
@@ -249,7 +267,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 +10 -4
index 9f3090db2f16..24f2dffc0789 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,11 +861,16 @@ 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 */
-	__u64 alignment;
+	union {
+		/* physical start_addr alignment in bytes for some HW requirements */
+		__u64 alignment;
+
+		/* drm_amdgpu_gem_userptr.addr for userptr objects */
+		__u64 userptr;
+	};
 };
 
 #define AMDGPU_VA_OP_MAP			1
#3 lore raw

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

Quoted 57 lines · Christian König
Quoted 56 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           | 14 ++++++---
  4 files changed, 67 insertions(+), 24 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +12 -6
index 862fbc2e125f..e75026aba874 100644
@@ -1204,14 +1204,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->userptr = 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 +0 -0
index 16c060badaee..7ce9e0e194c3 100644
@@ -785,6 +785,33 @@ int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo,
  	return r;
  }
 
Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, · line 795

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +8 -0
+/*
+ * 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)

That should actually check for usertask and not userptr.

Ack.

Quoted 22 lines
Quoted 9 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +9 -0
+		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);
+

drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, · line 814

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +10 -0
+	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;
+}
+

This is identical to the existing amdgpu_ttm_tt_get_userptr() function, just without error handling.

Quoted 1 line

I suggest to just extend amdgpu_ttm_tt_get_userptr() to also return the flags.

Thank you, it looks like I completely missed that one exists.

Will fix both issues in next respin.

Regards,

Tvrtko

Quoted 114 lines

Apart from that the patch looks good to me of hand.

Regards,
Christian.

Quoted 107 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c +0 -13
  #endif
 
  /*
@@ -1392,19 +1419,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 2d72fa217274..e1b3495b902f 100644
@@ -209,12 +209,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
 
  /**
@@ -249,7 +267,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 +10 -4
index 9f3090db2f16..24f2dffc0789 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,11 +861,16 @@ 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 */
-	__u64 alignment;
+	union {
+		/* physical start_addr alignment in bytes for some HW requirements */
+		__u64 alignment;
+
+		/* drm_amdgpu_gem_userptr.addr for userptr objects */
+		__u64 userptr;
+	};
  };
 
  #define AMDGPU_VA_OP_MAP			1
#4 lore raw

RFC v2 2/4 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 | 100 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   2 +
 include/uapi/drm/amdgpu_drm.h           |  31 ++++++++
 4 files changed, 134 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +100 -0
index ce35b415093d..b9bbf8677174 100644
@@ -768,6 +768,106 @@ 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;
+
+		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 bf4260269681..f61f0ddd3ccf 100644
@@ -3076,6 +3076,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 +31 -0
index 24f2dffc0789..3191ae1a0549 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
@@ -873,6 +875,35 @@ struct drm_amdgpu_gem_list_handles_entry {
 	};
 };
 
+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
maintainer
#5 lore raw

Re: [RFC v2 2/4] drm/amdgpu: Add a new ioctl for listing client contexts

Quoted 50 lines · 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 | 100 ++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   2 +
 include/uapi/drm/amdgpu_drm.h           |  31 ++++++++
 4 files changed, 134 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +24 -0
index ce35b415093d..b9bbf8677174 100644
@@ -768,6 +768,106 @@ 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)

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, · line 804

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -0
+		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;

For the args->num_contexts < num case I would expect an error return.

Quoted 39 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +39 -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;
+
+		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;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, · line 853

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -0
+			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);

Looks good to me of hand, but Alex should probably take a look at this power stuff as well.

Quoted 66 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +17 -0
+
+		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 bf4260269681..f61f0ddd3ccf 100644
@@ -3076,6 +3076,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 +2 -0
index 24f2dffc0789..3191ae1a0549 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

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

Quoted 8 lines
include/uapi/drm/amdgpu_drm.h +5 -0
@@ -873,6 +875,35 @@ struct drm_amdgpu_gem_list_handles_entry {
 	};
 };
 
+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) */

That comment looks to long and needs to be broken into multiple lines.

Apart from that looks really good to me,
Christian.

Quoted 27 lines
include/uapi/drm/amdgpu_drm.h +24 -0
+	__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
#6 lore raw

Re: [RFC v2 2/4] drm/amdgpu: Add a new ioctl for listing client contexts

Quoted 55 lines · Christian König
Quoted 54 lines · 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 | 100 ++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.h |   2 +
  include/uapi/drm/amdgpu_drm.h           |  31 ++++++++
  4 files changed, 134 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +28 -0
index ce35b415093d..b9bbf8677174 100644
@@ -768,6 +768,106 @@ 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)
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, · line 804

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +6 -0
+		num++;
+	mutex_unlock(&mgr->lock);
+
+	if (num == 0 || args->num_contexts < num) {
+		args->num_contexts = num;
+		return 0;

For the args->num_contexts < num case I would expect an error return.

Hm, why, what do you have in mind? It is the same pattern as in list handles by the way.

Quoted 121 lines
Quoted 39 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +39 -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;
+
+		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;

drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c int amdgpu_ctx_ioctl(struct drm_device *dev, void *data, · line 853

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +10 -0
+			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);

Looks good to me of hand, but Alex should probably take a look at this power stuff as well.

Quoted 69 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_ctx.c +17 -0
+
+		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 bf4260269681..f61f0ddd3ccf 100644
@@ -3076,6 +3076,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 +2 -0
index 24f2dffc0789..3191ae1a0549 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
@@ -873,6 +875,35 @@ struct drm_amdgpu_gem_list_handles_entry {
  	};
  };
 
Quoted 7 lines

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

Quoted 5 lines
include/uapi/drm/amdgpu_drm.h +5 -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) */

That comment looks to long and needs to be broken into multiple lines.

I was lazy and copy pasted from struct drm_amdgpu_gem_list_handles. :) Will fix.

Regards,

Tvrtko

Quoted 32 lines

Apart from that looks really good to me,
Christian.

Quoted 27 lines
include/uapi/drm/amdgpu_drm.h +24 -0
+	__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
#7 lore raw

RFC v2 3/4 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 b9bbf8677174..e58c60634d19 100644
@@ -554,6 +554,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)
@@ -761,6 +788,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 3191ae1a0549..7f5c4de7f9a5 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
#8 lore raw

RFC v2 4/4 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 | 104 ++++++++++++++++++++++++
 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           |  16 ++++
 6 files changed, 137 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index f61f0ddd3ccf..993f38d70086 100644
@@ -3077,6 +3077,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 +104 -0

116 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 7ce9e0e194c3..fb1e4d7a87ec 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 e1b3495b902f..0c3aefaca085 100644
@@ -284,4 +284,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 +16 -0
index 7f5c4de7f9a5..b79c3d4c2ec2 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_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_COPY_BUFFER, union drm_amdgpu_gem_copy_buffer)
 
 /**
  * DOC: memory domains
@@ -206,6 +208,20 @@ union drm_amdgpu_gem_create {
 	struct drm_amdgpu_gem_create_out	out;
 };
 
+struct drm_amdgpu_gem_copy_buffer_in {
+	__u32	src_handle;
+	__u32	dst_handle;
+};
+
+struct drm_amdgpu_gem_copy_buffer_out {
+	__u32	fence_fd;
+};
+
+union drm_amdgpu_gem_copy_buffer {
+	struct drm_amdgpu_gem_copy_buffer_in	in;
+	struct drm_amdgpu_gem_copy_buffer_out	out;
+};
+
 /** 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
#9 lore raw

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

Quoted 9 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

Typo "already".

Quoted 10 lines

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.

I think that sentence can be dropped now.

Quoted 70 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
 drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 104 ++++++++++++++++++++++++
 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           |  16 ++++
 6 files changed, 137 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index f61f0ddd3ccf..993f38d70086 100644
@@ -3077,6 +3077,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 +32 -0
index e75026aba874..ccc0ecfe6603 100644
@@ -32,6 +32,7 @@
 #include <linux/pci.h>
 #include <linux/dma-buf.h>
 #include <linux/dma-fence-unwrap.h>
+#include <linux/sync_file.h>
 #include <linux/uaccess.h>
 
 #include <drm/amdgpu_drm.h>
@@ -1234,6 +1235,109 @@ 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 = {};
+	union 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 sync_file *sync_file;
+	struct drm_exec exec;
+	unsigned int e;
+	int r;
+
+	src_gobj = drm_gem_object_lookup(filp, args->in.src_handle);
+	if (!src_gobj)
+		return -ENOENT;
+
+	dst_gobj = drm_gem_object_lookup(filp, args->in.dst_handle);
+	if (!dst_gobj) {
+		r = -ENOENT;

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, · line 1278

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10 -0
+		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;
+	}

No sub range copy? I would expect something like a ~8MiB transfer buffer to copy a 1GiB buffer in chunks of 2MiB.

Quoted 25 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +25 -0
+
+	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),

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, · line 1313

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +10 -0
+				       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);

That should be DMA_RESV_USAGE_BOOKKEEP. READ/WRITE are only for implicit sync here and we clearly don't want that here.

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, · line 1319

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
+
+	r = get_unused_fd_flags(O_CLOEXEC);
+	if (r < 0)
+		goto err_put_fence;
+
+	sync_file = sync_file_create(fence);

Oh, please no sync_file!

Use a drm_syncobj, e.g. drm_syncobj_find() + dma_fence_chain_alloc() before the copy and then drm_syncobj_add_point() when it is done.

Regards,
Christian.

Quoted 119 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +21 -0
+	if (!sync_file) {
+		put_unused_fd(r);
+		r = -ENOMEM;
+		goto err_put_fence;
+	}
+
+	fd_install(r, sync_file->file);
+	args->out.fence_fd = r;
+	r = 0;
+
+err_put_fence:
+	dma_fence_put(fence);
+err_unlock:
+	drm_exec_fini(&exec);
+err_sizes:
+	drm_gem_object_put(dst_gobj);
+err_dst:
+	drm_gem_object_put(src_gobj);
+	return r;
+}
+
 static unsigned int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
 					   unsigned int width,
 					   unsigned 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 7ce9e0e194c3..fb1e4d7a87ec 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 e1b3495b902f..0c3aefaca085 100644
@@ -284,4 +284,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 +16 -0
index 7f5c4de7f9a5..b79c3d4c2ec2 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_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_COPY_BUFFER, union drm_amdgpu_gem_copy_buffer)
 
 /**
  * DOC: memory domains
@@ -206,6 +208,20 @@ union drm_amdgpu_gem_create {
 	struct drm_amdgpu_gem_create_out	out;
 };
 
+struct drm_amdgpu_gem_copy_buffer_in {
+	__u32	src_handle;
+	__u32	dst_handle;
+};
+
+struct drm_amdgpu_gem_copy_buffer_out {
+	__u32	fence_fd;
+};
+
+union drm_amdgpu_gem_copy_buffer {
+	struct drm_amdgpu_gem_copy_buffer_in	in;
+	struct drm_amdgpu_gem_copy_buffer_out	out;
+};
+
 /** Opcode to create new residency list.  */
 #define AMDGPU_BO_LIST_OP_CREATE	0
 /** Opcode to destroy previously created residency list */
#10 lore raw

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

Quoted 4 lines · Christian König
Quoted 3 lines · Tvrtko Ursulin

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

Quoted 8 lines
Quoted 6 lines

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

Typo "already".

Ack.

Quoted 8 lines
Quoted 6 lines

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.

Quoted 4 lines
Quoted 2 lines

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

I think that sentence can be dropped now.

Yep, forgot to update for v2 changes.

Quoted 73 lines
Quoted 73 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c |   1 +
  drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c | 104 ++++++++++++++++++++++++
  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           |  16 ++++
  6 files changed, 137 insertions(+), 7 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
index f61f0ddd3ccf..993f38d70086 100644
@@ -3077,6 +3077,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 +36 -0
index e75026aba874..ccc0ecfe6603 100644
@@ -32,6 +32,7 @@
  #include <linux/pci.h>
  #include <linux/dma-buf.h>
  #include <linux/dma-fence-unwrap.h>
+#include <linux/sync_file.h>
  #include <linux/uaccess.h>
 
  #include <drm/amdgpu_drm.h>
@@ -1234,6 +1235,109 @@ 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 = {};
+	union 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 sync_file *sync_file;
+	struct drm_exec exec;
+	unsigned int e;
+	int r;
+
+	src_gobj = drm_gem_object_lookup(filp, args->in.src_handle);
+	if (!src_gobj)
+		return -ENOENT;
+
+	dst_gobj = drm_gem_object_lookup(filp, args->in.dst_handle);
+	if (!dst_gobj) {
+		r = -ENOENT;
+		goto err_dst;
+	}
+
+	src_bo = gem_to_amdgpu_bo(src_gobj);
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, · line 1278

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
+	dst_bo = gem_to_amdgpu_bo(dst_gobj);
+
+	if (amdgpu_bo_size(src_bo) < amdgpu_bo_size(dst_bo)) {
+		r = -E2BIG;
+		goto err_sizes;
+	}

No sub range copy? I would expect something like a ~8MiB transfer buffer to copy a 1GiB buffer in chunks of 2MiB.

To preemptively make it generic or you already have an use case in mind? But sure, I can add that no problem.

Quoted 29 lines
Quoted 29 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +29 -0
+
+	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) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, · line 1313

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
+		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);

That should be DMA_RESV_USAGE_BOOKKEEP. READ/WRITE are only for implicit sync here and we clearly don't want that here.

Ack.

Quoted 10 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, · line 1319

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
+
+	r = get_unused_fd_flags(O_CLOEXEC);
+	if (r < 0)
+		goto err_put_fence;
+
+	sync_file = sync_file_create(fence);

Oh, please no sync_file!

Use a drm_syncobj, e.g. drm_syncobj_find() + dma_fence_chain_alloc() before the copy and then drm_syncobj_add_point() when it is done.

Why not, is sync_file so out of fashion? Syncobj feels heavy weight for what this is and it would require the CRIU side to create and destroy the syncobj.

Regards,

Tvrtko

Quoted 120 lines
Quoted 119 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +21 -0
+	if (!sync_file) {
+		put_unused_fd(r);
+		r = -ENOMEM;
+		goto err_put_fence;
+	}
+
+	fd_install(r, sync_file->file);
+	args->out.fence_fd = r;
+	r = 0;
+
+err_put_fence:
+	dma_fence_put(fence);
+err_unlock:
+	drm_exec_fini(&exec);
+err_sizes:
+	drm_gem_object_put(dst_gobj);
+err_dst:
+	drm_gem_object_put(src_gobj);
+	return r;
+}
+
  static unsigned int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
  					   unsigned int width,
  					   unsigned 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 7ce9e0e194c3..fb1e4d7a87ec 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 e1b3495b902f..0c3aefaca085 100644
@@ -284,4 +284,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 +16 -0
index 7f5c4de7f9a5..b79c3d4c2ec2 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_IOWR(DRM_COMMAND_BASE + DRM_AMDGPU_GEM_COPY_BUFFER, union drm_amdgpu_gem_copy_buffer)
 
  /**
   * DOC: memory domains
@@ -206,6 +208,20 @@ union drm_amdgpu_gem_create {
  	struct drm_amdgpu_gem_create_out	out;
  };
 
+struct drm_amdgpu_gem_copy_buffer_in {
+	__u32	src_handle;
+	__u32	dst_handle;
+};
+
+struct drm_amdgpu_gem_copy_buffer_out {
+	__u32	fence_fd;
+};
+
+union drm_amdgpu_gem_copy_buffer {
+	struct drm_amdgpu_gem_copy_buffer_in	in;
+	struct drm_amdgpu_gem_copy_buffer_out	out;
+};
+
  /** Opcode to create new residency list.  */
  #define AMDGPU_BO_LIST_OP_CREATE	0
  /** Opcode to destroy previously created residency list */
maintainer
#11 lore raw

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

On 7/3/26 12:37, Tvrtko Ursulin wrote:
...

Quoted 44 lines
Quoted 35 lines
Quoted 35 lines

  +/**
+ * 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.
+ */

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +25 -0
+int amdgpu_gem_copy_buffer_ioctl(struct drm_device *dev, void *data,
+                 struct drm_file *filp)
+{
+    struct amdgpu_copy_mem src_mem = {}, dst_mem = {};
+    union 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 sync_file *sync_file;
+    struct drm_exec exec;
+    unsigned int e;
+    int r;
+
+    src_gobj = drm_gem_object_lookup(filp, args->in.src_handle);
+    if (!src_gobj)
+        return -ENOENT;
+
+    dst_gobj = drm_gem_object_lookup(filp, args->in.dst_handle);
+    if (!dst_gobj) {
+        r = -ENOENT;
+        goto err_dst;
+    }
+
+    src_bo = gem_to_amdgpu_bo(src_gobj);
Quoted 8 lines
Quoted 6 lines

+    dst_bo = gem_to_amdgpu_bo(dst_gobj);
+
+    if (amdgpu_bo_size(src_bo) < amdgpu_bo_size(dst_bo)) { +        r = -E2BIG;
+        goto err_sizes;
+    }

No sub range copy? I would expect something like a ~8MiB transfer buffer to copy a 1GiB buffer in chunks of 2MiB.

Quoted 1 line

To preemptively make it generic or you already have an use case in mind? But sure, I can add that no problem.

If I'm not completely mistaken that will be needed for CRIU of large buffers at some point. Especially for system without a large BAR.

Additional to that we need this for KFD/DRM render node unification as well and IIRC the existing KFD IOCTL can do sub range copies.

Quoted 9 lines
Quoted 9 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data, · line 1319

Quoted 6 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c +6 -0
+
+    r = get_unused_fd_flags(O_CLOEXEC);
+    if (r < 0)
+        goto err_put_fence;
+
+    sync_file = sync_file_create(fence);

Oh, please no sync_file!

Quoted 3 lines
Quoted 1 line

Use a drm_syncobj, e.g. drm_syncobj_find() + dma_fence_chain_alloc() before the copy and then drm_syncobj_add_point() when it is done.

Why not, is sync_file so out of fashion? Syncobj feels heavy weight for what this is and it would require the CRIU side to create and destroy the syncobj.

The sync_file framework is only used by Android. It has quite a number of shortcommings and is pretty much dead everywhere else.

For basically all modern IOCTLs we only use drm_syncobj timeline semaphores.

Regards,
Christian.