Skip to contentSkip to conversation

amdgpu plugin render node only discussion

RFC In review Tvrtko Ursulin 3 participants 45 messages Feb 20 – Mar 10

lore
Share & tools
lore
#0 lore raw

cover amdgpu plugin render node only discussion

Hi all,

Long story short, I am trying to make some inroads towards having a working checkpoint and restore for render node only applications (ie. no KFD).

Plan is to write test scenarios in an increasing order of complexity and make them work. Those tests can be found here:

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

For those unfamiliar, once built the list of subtest can be obtained like this:

$ amd_criu --l
open
one-bo
map-one
mmap-one
map-and-mmap-one
map-mmap-fork

Each test will pause after the setup phase waiting for a return key. This is the point when they can be checkpointed and restored. The tests are in the order of how I was fixing issues one by one, give or take some details.

Without this series nothing works, while with it, they all pass (last one is the superset of all previous ones, or the most complex test):

$ amd_criu --r map-mmap-fork
...
$ 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
$ 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 map-mmap-fork: SUCCESS (6.310s)

However trivial the tests may currently be, the combination of them and this series is supposed to be a starting point for a design discussion since it appears there are many issues to address.

Having said that, this series actually starts with a fair number of cleanup patches which can be reviewed and merged independently. Or I can easily send them as a standalone pull request if that would work better.

Only the last six patches, plus "plugins/amdgpu: Add plugin to inventory even if process has no vmas", are the ones with fixes and hacks which make the tests pass.

In no particular order the main issues I currently see are:

* GPU discovery with no KFD open or present
* Rendernode VMA handling
* VMA handling with forked processes
* GPU VA conflicts during restore

Some of these may require changes to CRIU plugin parameters and input from CRIU experts.

More details on each of these topics can be found in the respective patch commit messages. I will also gladly expand on any.

For now I wanted to test the waters first by sending this early and see who is interested and to what extent. This is far from a working render node support but it illustrates that such basic scenarios currently do not work, that I think those basics need to be fixed first before proceeding with anything more complex.

Also, nothing in this series is supposed to negatively influence the KFD paths, but with a disclaimer that I did not test that side at all. Suggestions on a suitable KFD test program would be welcomed.

Tvrtko

Tvrtko Ursulin (26):
plugins/amgdpu: Fix one error message
plugins/amdgpu: Remove unused current_pid global variable plugins/amdgpu: Remove unused new_minor from struct vma_metadata plugins/amdgpu: Fix drm pages size header
plugins/amdgpu: Fix logging of failures to open files during restore
init
plugins/amdgpu: Propagate failure to save buffer object content plugins/amdgpu: Close the directory when image probing fails plugins/amdgpu: Close dma-buf image file if the read fails plugins/amdgpu: Check images were opened successfully plugins/amdgpu: Flatten amdgpu_restore_init a bit plugins/amdgpu: Fix error handling in amdgpu_plugin_drm_restore_file() plugins/amdgpu: Add error handling for seek operations plugins/amdgpu: Consolidate vm_info collection plugins/amdgpu: Remove plugin_log_msg()
plugins/amdgpu: Add plugin to inventory even if process has no vmas plugins/amdgpu: Move drm file dump and restore into helpers plugins/amdgpu: Use the load_img helper in drm file restore plugins/amdgpu: Convert away from libc buffered file IO plugins/amdgpu: Use save_vma_updates for all call sites plugins/amdgpu: amdgpu_plugin_drm_restore_file() does not need to use
libdrm
plugins/amdgpu: Enable restore with only the drm node open plugins/amdgpu: Handle DRM render node mmaps plugins/amdgpu: Consolidate shared memory handling plugins/amdgpu: Store VMA data in shared memory plugins/amdgpu: Hack to save a fd number which works for vma restore plugins/amdgpu: Hack to work around GPU virtual address conflicts

 plugins/amdgpu/amdgpu_plugin.c          | 856 ++++++++++++++----------
 plugins/amdgpu/amdgpu_plugin_dmabuf.c   |  17 +-
 plugins/amdgpu/amdgpu_plugin_drm.c      | 125 ++--
 plugins/amdgpu/amdgpu_plugin_drm.h      |   5 +-
 plugins/amdgpu/amdgpu_plugin_topology.c |   2 +
 plugins/amdgpu/amdgpu_plugin_topology.h |   2 +
 plugins/amdgpu/amdgpu_plugin_util.c     | 106 +--
 plugins/amdgpu/amdgpu_plugin_util.h     |  17 +-
 8 files changed, 636 insertions(+), 494 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

RFC 1/26 plugins/amgdpu: Fix one error message

Instead of logging a zero failure code lets log the actual GPU id which was not found.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
plugins/amdgpu/amdgpu_plugin_drm.c +1 -1
index 3520bca7a317..e2fe4bd8a823 100644
@@ -416,7 +416,7 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 	/* Get the GPU_ID of the DRM device */
 	rd->gpu_id = maps_get_dest_gpu(&checkpoint_maps, tp_node->gpu_id);
 	if (!rd->gpu_id) {
-		pr_err("Failed to find valid gpu_id for the device = %d\n", rd->gpu_id);
+		pr_err("Failed to find valid gpu_id for the device = %d\n", tp_node->gpu_id);
 		return -ENODEV;
 	}
 
Signature · 1 line
2.52.0
#2 lore raw

RFC 2/26 plugins/amdgpu: Remove unused current_pid global variable

In reply to Tvrtko Ursulin

Remove unused current_pid global variable.

---
 plugins/amdgpu/amdgpu_plugin.c | 1 -
 1 file changed, 1 deletion(-)
plugins/amdgpu/amdgpu_plugin.c +0 -1
index ae57fb2f42e1..505f1f98abb9 100644
@@ -80,7 +80,6 @@ struct shared_handle_ids *shared_memory = NULL;
 
 static mutex_t *shared_memory_mutex;
 
-int current_pid;
 /*
  * In the case of a single process (common case), this optimization can effectively
  * reduce the restore latency with parallel restore. In the case of multiple processes,
Signature · 1 line
2.52.0
#3 lore raw

RFC 3/26 plugins/amdgpu: Remove unused new_minor from struct vma_metadata

In reply to Tvrtko Ursulin

The new_minor field in struct vma_metadata is effectively unused. Remove it.

---
 plugins/amdgpu/amdgpu_plugin.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +1 -3
index 505f1f98abb9..5bcfbe13b1d7 100644
@@ -54,7 +54,6 @@ struct vma_metadata {
 	uint64_t old_pgoff;
 	uint64_t new_pgoff;
 	uint64_t vma_entry;
-	uint32_t new_minor;
 	int fd;
 };
 
@@ -1690,13 +1689,12 @@ static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKf
 				goto exit;
 			}
 
-			vma_md->new_minor = tp_node->drm_render_minor;
 			vma_md->new_pgoff = bo_bucket->restored_offset;
 			vma_md->fd = node_get_drm_render_device(tp_node);
 
 			plugin_log_msg("adding vma_entry:addr:0x%lx old-off:0x%lx "
 				       "new_off:0x%lx new_minor:%d\n",
-				       vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff, vma_md->new_minor);
+				       vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff, tp_node->drm_render_minor);
 
 			list_add_tail(&vma_md->list, &update_vma_info_list);
 		}
Signature · 1 line
2.52.0
#4 lore raw

RFC 4/26 plugins/amdgpu: Fix drm pages size header

In reply to Tvrtko Ursulin

Size needs to be initialized before opening the file for write, otherwise garbage is written into the file.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 1 +
 1 file changed, 1 insertion(+)
plugins/amdgpu/amdgpu_plugin_drm.c +1 -0
index e2fe4bd8a823..1ec133de262d 100644
@@ -378,6 +378,7 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 		drmPrimeHandleToFD(device_fd, boinfo->handle, 0, &dmabuf_fd);
 
 		snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, rd->drm_render_minor, i);
+		image_size = handle_entry.size;
 		bo_contents_fp = open_img_file(img_path, true, &image_size);
 
 		posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), handle_entry.size);
Signature · 1 line
2.52.0
#5 lore raw

RFC 5/26 plugins/amdgpu: Fix logging of failures to open files during restore init

In reply to Tvrtko Ursulin

Currently failure to open files logs an unitialized stack string as the failed path. Lets just log the basename and drop the unused stack buffer.

---
 plugins/amdgpu/amdgpu_plugin.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +2 -3
index 5bcfbe13b1d7..4970fa0d5afd 100644
@@ -1128,7 +1128,6 @@ int amdgpu_restore_init(void)
 		int ret;
 		unsigned char *buf;
 		int num_handles = 0;
-		char img_path[PATH_MAX];
 		CriuRenderNode *rd = NULL;
 		CriuKfd *e = NULL;
 
@@ -1147,7 +1146,7 @@ int amdgpu_restore_init(void)
 
 					ret = read_fp(img_fp, buf, img_size);
 					if (ret) {
-						pr_perror("Unable to read from %s", img_path);
+						pr_perror("Unable to read from %s", dir->d_name);
 						fclose(img_fp);
 						xfree(buf);
 						return ret;
@@ -1169,7 +1168,7 @@ int amdgpu_restore_init(void)
 
 					ret = read_fp(img_fp, buf, img_size);
 					if (ret) {
-						pr_perror("Unable to read from %s", img_path);
+						pr_perror("Unable to read from %s", dir->d_name);
 						fclose(img_fp);
 						xfree(buf);
 						return ret;
Signature · 1 line
2.52.0
#6 lore raw

RFC 6/26 plugins/amdgpu: Propagate failure to save buffer object content

In reply to Tvrtko Ursulin

Currently the failure to save content of a buffer object is only logged, while the checkpointing will continue unaware due the error code being "eaten" inside sdma_copy_bo() cleanup path, together with the caller not checking the return value to begin with.

Fix it by preserving and returning the original error code inside sdma_copy_bo(), and abort the checkpointing process in case of a failure.

---
 plugins/amdgpu/amdgpu_plugin.c     | 31 +++++++++++++++---------------
 plugins/amdgpu/amdgpu_plugin_drm.c |  2 ++
 2 files changed, 18 insertions(+), 15 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +16 -15
index 4970fa0d5afd..b13ec66e3336 100644
@@ -563,7 +563,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
 	uint32_t expired;
 	amdgpu_context_handle h_ctx;
 	uint32_t *ib = NULL;
-	int j, err, packets_per_buffer;
+	int j, err, err2, packets_per_buffer;
 
 	buffer_bo_size = min(size, buffer_size);
 	packets_per_buffer = ((buffer_bo_size - 1) / max_copy_size) + 1;
@@ -767,29 +767,30 @@ err_ctx:
 err_bo_list:
 	free_and_unmap(packets_per_buffer * 28, h_bo_ib, h_va_ib, gpu_addr_ib, ib);
 err_ib_gpu_alloc:
-	err = amdgpu_bo_va_op(h_bo_dst, 0, size, gpu_addr_dst, 0, AMDGPU_VA_OP_UNMAP);
-	if (err)
+	err2 = amdgpu_bo_va_op(h_bo_dst, 0, size, gpu_addr_dst, 0, AMDGPU_VA_OP_UNMAP);
+	if (err2)
 		pr_perror("failed to GPU unmap the dest BO %lx, size = %lx", gpu_addr_dst, size);
 err_dst_bo_map:
-	err = amdgpu_va_range_free(h_va_dst);
-	if (err)
+	err2 = amdgpu_va_range_free(h_va_dst);
+	if (err2)
 		pr_perror("dest range free failed");
 err_dst_va:
-	if (!do_not_free)
-		err = amdgpu_bo_free(h_bo_dst);
-	if (err)
-		pr_perror("dest bo free failed");
+	if (!do_not_free) {
+		err2 = amdgpu_bo_free(h_bo_dst);
+		if (err2)
+			pr_perror("dest bo free failed");
+	}
 err_dst_bo_prep:
-	err = amdgpu_bo_va_op(h_bo_src, 0, size, gpu_addr_src, 0, AMDGPU_VA_OP_UNMAP);
-	if (err)
+	err2 = amdgpu_bo_va_op(h_bo_src, 0, size, gpu_addr_src, 0, AMDGPU_VA_OP_UNMAP);
+	if (err2)
 		pr_perror("failed to GPU unmap the src BO %lx, size = %lx", gpu_addr_src, size);
 err_src_bo_map:
-	err = amdgpu_va_range_free(h_va_src);
-	if (err)
+	err2 = amdgpu_va_range_free(h_va_src);
+	if (err2)
 		pr_perror("src range free failed");
 err_src_va:
-	err = amdgpu_bo_free(h_bo_src);
-	if (err)
+	err2 = amdgpu_bo_free(h_bo_src);
+	if (err2)
 		pr_perror("src bo free failed");
 	plugin_log_msg("Leaving sdma_copy_bo, err = %d\n", err);
 	return err;
plugins/amdgpu/amdgpu_plugin_drm.c +2 -0
index 1ec133de262d..34014783c2d4 100644
@@ -385,6 +385,8 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
 		ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fp, buffer, handle_entry.size, h_dev, 0x1000,
 				   SDMA_OP_VRAM_READ, false);
+		if (ret)
+			goto exit;
 
 		if (dmabuf_fd != KFD_INVALID_FD)
 			close(dmabuf_fd);
Signature · 1 line
2.52.0
#7 lore raw

RFC 7/26 plugins/amdgpu: Close the directory when image probing fails

In reply to Tvrtko Ursulin

Close the directory when probing images during restore init fails.

While at it, consolidate the identical code between the DRM and KFD images into a common helper.

---
 plugins/amdgpu/amdgpu_plugin.c | 76 ++++++++++++++++++++--------------
 1 file changed, 46 insertions(+), 30 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +46 -30
index b13ec66e3336..dcc8940e735b 100644
@@ -1119,15 +1119,47 @@ int amdgpu_id_for_handle(int handle)
 	return -1;
 }
 
+static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
+{
+	unsigned char *buf;
+	FILE *img_fp;
+	size_t len;
+	int ret;
+
+	img_fp = open_img_file(filename, false, &len);
+	if (!img_fp) {
+		ret = -ENOENT;
+		goto out;
+	}
+
+	buf = xmalloc(len);
+	if (!buf) {
+		ret = -ENOMEM;
+		goto out_close;
+	}
+
+	ret = read_fp(img_fp, buf, len);
+	if (ret) {
+		xfree(buf);
+	} else {
+		*out_buf = buf;
+		*out_len = len;
+	}
+
+out_close:
+	fclose(img_fp);
+out:
+	if (ret < 0)
+		pr_err("Unable to read from %s", filename);
+
+	return ret;
+}
+
 int amdgpu_restore_init(void)
 {
 	if (!shared_memory) {
 		int protection = PROT_READ | PROT_WRITE;
 		int visibility = MAP_SHARED | MAP_ANONYMOUS;
-		size_t img_size;
-		FILE *img_fp = NULL;
-		int ret;
-		unsigned char *buf;
 		int num_handles = 0;
 		CriuRenderNode *rd = NULL;
 		CriuKfd *e = NULL;
@@ -1137,45 +1169,29 @@ int amdgpu_restore_init(void)
 		d = opendir(".");
 		if (d) {
 			while ((dir = readdir(d)) != NULL) {
+				unsigned char *buf;
+				size_t img_size;
+				int ret;
+
 				if (strncmp("amdgpu-kfd-", dir->d_name, strlen("amdgpu-kfd-")) == 0) {
-					img_fp = open_img_file(dir->d_name, false, &img_size);
-					buf = xmalloc(img_size);
-					if (!buf) {
-						fclose(img_fp);
-						return -ENOMEM;
-					}
-
-					ret = read_fp(img_fp, buf, img_size);
-					if (ret) {
-						pr_perror("Unable to read from %s", dir->d_name);
-						fclose(img_fp);
-						xfree(buf);
+					ret = load_img(dir->d_name, &buf, &img_size);
+					if (ret < 0) {
+						closedir(d);
 						return ret;
 					}
 
-					fclose(img_fp);
 					e = criu_kfd__unpack(NULL, img_size, buf);
 					num_handles += e->num_of_bos;
 					criu_kfd__free_unpacked(e, NULL);
 					xfree(buf);
 				}
 				if (strncmp("amdgpu-renderD-", dir->d_name, strlen("amdgpu-renderD-")) == 0) {
-					img_fp = open_img_file(dir->d_name, false, &img_size);
-					buf = xmalloc(img_size);
-					if (!buf) {
-						fclose(img_fp);
-						return -ENOMEM;
-					}
-
-					ret = read_fp(img_fp, buf, img_size);
-					if (ret) {
-						pr_perror("Unable to read from %s", dir->d_name);
-						fclose(img_fp);
-						xfree(buf);
+					ret = load_img(dir->d_name, &buf, &img_size);
+					if (ret < 0) {
+						closedir(d);
 						return ret;
 					}
 
-					fclose(img_fp);
 					rd = criu_render_node__unpack(NULL, img_size, buf);
 					num_handles += rd->num_of_bos;
 					criu_render_node__free_unpacked(rd, NULL);
Signature · 1 line
2.52.0
#8 lore raw

RFC 8/26 plugins/amdgpu: Close dma-buf image file if the read fails

In reply to Tvrtko Ursulin

Consolidate to one fclose() call with the extra bonus the inconsequential file descriptor leak is eliminated.

---
 plugins/amdgpu/amdgpu_plugin_dmabuf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
plugins/amdgpu/amdgpu_plugin_dmabuf.c +1 -2
index 11c9792e3649..93a975e16e83 100644
@@ -120,6 +120,7 @@ int amdgpu_plugin_dmabuf_restore(int id)
 	}
 
 	ret = read_fp(img_fp, buf, img_size);
+	fclose(img_fp);
 	if (ret) {
 		pr_perror("Unable to read from %s", path);
 		xfree(buf);
@@ -130,10 +131,8 @@ int amdgpu_plugin_dmabuf_restore(int id)
 	if (rd == NULL) {
 		pr_perror("Unable to parse the dmabuf message %d", id);
 		xfree(buf);
-		fclose(img_fp);
 		return -1;
 	}
-	fclose(img_fp);
 
 	/* Match GEM handle with shared_dmabuf list */
 	fd_id = amdgpu_id_for_handle(rd->gem_handle);
Signature · 1 line
2.52.0
#9 lore raw

RFC 9/26 plugins/amdgpu: Check images were opened successfully

In reply to Tvrtko Ursulin

Avoid potentially passing a NULL file pointer to fread/fwrite inside sdma_copy_bo() and therefore invoking undefined behaviour.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 9 +++++++++
 1 file changed, 9 insertions(+)
plugins/amdgpu/amdgpu_plugin_drm.c +9 -0
index 34014783c2d4..06976a81ce1a 100644
@@ -210,6 +210,11 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
 		snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, drm_render_minor, i);
 
 		bo_contents_fp = open_img_file(img_path, false, &image_size);
+		if (!bo_contents_fp) {
+			ret = -EIO;
+			pr_err("Failed to open BO image file %s\n", img_path);
+			break;
+		}
 
 		ret = sdma_copy_bo(dmabufs[i], rd->bo_entries[i]->size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
 				   SDMA_OP_VRAM_WRITE, true);
@@ -380,6 +385,10 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 		snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, rd->drm_render_minor, i);
 		image_size = handle_entry.size;
 		bo_contents_fp = open_img_file(img_path, true, &image_size);
+		if (!bo_contents_fp) {
+			ret = -EIO;
+			goto exit;
+		}
 
 		posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), handle_entry.size);
 
Signature · 1 line
2.52.0
#10 lore raw

RFC 10/26 plugins/amdgpu: Flatten amdgpu_restore_init a bit

In reply to Tvrtko Ursulin

Shallower indentation makes a heavily indented function hopefully a bit more readable.

---
 plugins/amdgpu/amdgpu_plugin.c | 115 +++++++++++++++++----------------
 1 file changed, 60 insertions(+), 55 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +60 -55
index dcc8940e735b..1b531f351c55 100644
@@ -1157,67 +1157,72 @@ out:
 
 int amdgpu_restore_init(void)
 {
-	if (!shared_memory) {
-		int protection = PROT_READ | PROT_WRITE;
-		int visibility = MAP_SHARED | MAP_ANONYMOUS;
-		int num_handles = 0;
-		CriuRenderNode *rd = NULL;
-		CriuKfd *e = NULL;
-
-		DIR *d;
-		struct dirent *dir;
-		d = opendir(".");
-		if (d) {
-			while ((dir = readdir(d)) != NULL) {
-				unsigned char *buf;
-				size_t img_size;
-				int ret;
-
-				if (strncmp("amdgpu-kfd-", dir->d_name, strlen("amdgpu-kfd-")) == 0) {
-					ret = load_img(dir->d_name, &buf, &img_size);
-					if (ret < 0) {
-						closedir(d);
-						return ret;
-					}
-
-					e = criu_kfd__unpack(NULL, img_size, buf);
-					num_handles += e->num_of_bos;
-					criu_kfd__free_unpacked(e, NULL);
-					xfree(buf);
-				}
-				if (strncmp("amdgpu-renderD-", dir->d_name, strlen("amdgpu-renderD-")) == 0) {
-					ret = load_img(dir->d_name, &buf, &img_size);
-					if (ret < 0) {
-						closedir(d);
-						return ret;
-					}
-
-					rd = criu_render_node__unpack(NULL, img_size, buf);
-					num_handles += rd->num_of_bos;
-					criu_render_node__free_unpacked(rd, NULL);
-					xfree(buf);
-				}
-			}
-			closedir(d);
-		}
+	int num_handles = 0;
+	struct dirent *dir;
+	DIR *d;
+
+	if (shared_memory)
+		return 0;
+
+	d = opendir(".");
+	if (!d)
+		return -1;
 
-		if (num_handles > 0) {
-			shared_memory = mmap(NULL, sizeof(shared_memory), protection, visibility, -1, 0);
-			shared_memory->num_handles = num_handles;
-			shared_memory->handles = mmap(NULL, sizeof(struct handle_id) * num_handles, protection, visibility, -1, 0);
+	while ((dir = readdir(d)) != NULL) {
+		unsigned char *buf;
+		size_t img_size;
+		int ret;
 
-			for (int i = 0; i < num_handles; i++) {
-				shared_memory->handles[i].handle = -1;
-				shared_memory->handles[i].fdstore_id = -1;
+		if (strncmp("amdgpu-kfd-", dir->d_name, strlen("amdgpu-kfd-")) == 0) {
+			CriuKfd *e;
+
+			ret = load_img(dir->d_name, &buf, &img_size);
+			if (ret < 0) {
+				closedir(d);
+				return ret;
 			}
 
-			shared_memory_mutex = shmalloc(sizeof(*shared_memory_mutex));
-			if (!shared_memory_mutex) {
-				pr_err("Can't create amdgpu mutex\n");
-				return -1;
+			e = criu_kfd__unpack(NULL, img_size, buf);
+			num_handles += e->num_of_bos;
+			criu_kfd__free_unpacked(e, NULL);
+			xfree(buf);
+		}
+		if (strncmp("amdgpu-renderD-", dir->d_name, strlen("amdgpu-renderD-")) == 0) {
+			CriuRenderNode *rd;
+
+			ret = load_img(dir->d_name, &buf, &img_size);
+			if (ret < 0) {
+				closedir(d);
+				return ret;
 			}
-			mutex_init(shared_memory_mutex);
+
+			rd = criu_render_node__unpack(NULL, img_size, buf);
+			num_handles += rd->num_of_bos;
+			criu_render_node__free_unpacked(rd, NULL);
+			xfree(buf);
+		}
+	}
+	closedir(d);
+
+	if (num_handles > 0) {
+		const int protection = PROT_READ | PROT_WRITE;
+		const int visibility = MAP_SHARED | MAP_ANONYMOUS;
+
+		shared_memory = mmap(NULL, sizeof(shared_memory), protection, visibility, -1, 0);
+		shared_memory->num_handles = num_handles;
+		shared_memory->handles = mmap(NULL, sizeof(struct handle_id) * num_handles, protection, visibility, -1, 0);
+
+		for (int i = 0; i < num_handles; i++) {
+			shared_memory->handles[i].handle = -1;
+			shared_memory->handles[i].fdstore_id = -1;
+		}
+
+		shared_memory_mutex = shmalloc(sizeof(*shared_memory_mutex));
+		if (!shared_memory_mutex) {
+			pr_err("Can't create amdgpu mutex\n");
+			return -1;
 		}
+		mutex_init(shared_memory_mutex);
 	}
 
 	return 0;
Signature · 1 line
2.52.0
#11 lore raw

RFC 11/26 plugins/amdgpu: Fix error handling in amdgpu_plugin_drm_restore_file()

In reply to Tvrtko Ursulin

Add missing error checking for failure to allocate memory, plus, actually free the memory on the failure exit path.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
plugins/amdgpu/amdgpu_plugin_drm.c +9 -4
index 06976a81ce1a..9df755450dc9 100644
@@ -454,8 +454,13 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 	bool retry_needed = false;
 	uint32_t major, minor;
 	amdgpu_device_handle h_dev;
-	int device_fd;
-	int *dmabufs = xzalloc(sizeof(int) * rd->num_of_bos);
+	int device_fd, *dmabufs;
+
+	dmabufs = xzalloc(sizeof(int) * rd->num_of_bos);
+	if (!dmabufs) {
+		pr_err("Failed allocate memory for drm restore\n");
+		return -ENOMEM;
+	}
 
 	ret = amdgpu_device_initialize(fd, &major, &minor, &h_dev);
 	if (ret) {
@@ -573,9 +578,9 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 	}
 
 exit:
-	if (ret < 0)
-		return ret;
 	xfree(dmabufs);
+	if (ret < 0)
+		return ret;
 
 	return retry_needed;
 }
Signature · 1 line
2.52.0
#12 lore raw

RFC 12/26 plugins/amdgpu: Add error handling for seek operations

In reply to Tvrtko Ursulin

Extra safety so the calls do not stick out as the only ones with no error checking.

---
 plugins/amdgpu/amdgpu_plugin.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
plugins/amdgpu/amdgpu_plugin.c +12 -1
index 1b531f351c55..06df63de2434 100644
@@ -2297,6 +2297,11 @@ void *parallel_restore_bo_contents(void *_thread_data)
 		goto err_sdma;
 	}
 	offset = ftell(bo_contents_fp);
+	if (offset < 0) {
+		ret = -errno;
+		pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE.");
+		goto err_sdma;
+	}
 
 	posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), buffer_size);
 	if (!buffer) {
@@ -2310,7 +2315,13 @@ void *parallel_restore_bo_contents(void *_thread_data)
 			continue;
 
 		entry = &restore_cmd->entries[i];
-		fseeko(bo_contents_fp, entry->read_offset + offset, SEEK_SET);
+		ret = fseeko(bo_contents_fp, entry->read_offset + offset,
+			     SEEK_SET);
+		if (ret < 0) {
+			ret = -errno;
+			pr_err("Failed to seek for BO using sDMA: bo_buckets[%d]\n", i);
+			goto err_sdma;
+		}
 		ret = sdma_copy_bo(restore_cmd->fds_write[entry->write_id], entry->size, bo_contents_fp,
 				   buffer, buffer_size, h_dev,
 				   max_copy_size, SDMA_OP_VRAM_WRITE, false);
Signature · 1 line
2.52.0
#13 lore raw

RFC 13/26 plugins/amdgpu: Consolidate vm_info collection

In reply to Tvrtko Ursulin

Instead of open coding the same ioctl twice we can put it in a loop from which we break out once we have allocated enough space for all objects.

While at it we add error handling for the memory allocation.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 47 ++++++++++++++++--------------
 1 file changed, 25 insertions(+), 22 deletions(-)
plugins/amdgpu/amdgpu_plugin_drm.c +25 -22
index 9df755450dc9..baa560537813 100644
@@ -302,8 +302,7 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
 	for (int i = 0; i < num_bos; i++) {
 		int num_vm_entries = 8;
-		struct drm_amdgpu_gem_vm_entry *vm_info_entries;
-		struct drm_amdgpu_gem_op vm_info_args = { 0 };
+		struct drm_amdgpu_gem_vm_entry *vm_info_entries = NULL;
 		DrmBoEntry *boinfo = rd->bo_entries[i];
 		struct drm_amdgpu_gem_list_handles_entry handle_entry = list_handles_entries[i];
 		union drm_amdgpu_gem_mmap mmap_args = { 0 };
@@ -333,32 +332,36 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
 		boinfo->offset = mmap_args.out.addr_ptr;
 
-		vm_info_entries = xzalloc(sizeof(struct drm_amdgpu_gem_vm_entry) * num_vm_entries);
-		vm_info_args.handle = handle_entry.gem_handle;
-		vm_info_args.num_entries = num_vm_entries;
-		vm_info_args.value = (uintptr_t)vm_info_entries;
-		vm_info_args.op = AMDGPU_GEM_OP_GET_MAPPING_INFO;
-		ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP, &vm_info_args);
-		if (ret) {
-			pr_perror("Failed to call vm info ioctl");
-			goto exit;
-		}
+		for (;;) {
+			struct drm_amdgpu_gem_op vm_info_args = {
+				.handle = handle_entry.gem_handle,
+				.num_entries = num_vm_entries,
+				.op = AMDGPU_GEM_OP_GET_MAPPING_INFO,
+			};
+
+			if (vm_info_entries)
+				xfree(vm_info_entries);
+			vm_info_entries = xzalloc(sizeof(*vm_info_entries) *
+						  num_vm_entries);
+			if (!vm_info_entries) {
+				ret = -ENOMEM;
+				goto exit;
+			}
 
-		if (vm_info_args.num_entries > num_vm_entries) {
-			num_vm_entries = vm_info_args.num_entries;
-			xfree(vm_info_entries);
-			vm_info_entries = xzalloc(sizeof(struct drm_amdgpu_gem_vm_entry) * num_vm_entries);
-			vm_info_args.handle = handle_entry.gem_handle;
-			vm_info_args.num_entries = num_vm_entries;
 			vm_info_args.value = (uintptr_t)vm_info_entries;
-			vm_info_args.op = AMDGPU_GEM_OP_GET_MAPPING_INFO;
-			ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP, &vm_info_args);
+			ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP,
+				       &vm_info_args);
 			if (ret) {
 				pr_perror("Failed to call vm info ioctl");
 				goto exit;
 			}
-		} else {
-			num_vm_entries = vm_info_args.num_entries;
+
+			if (vm_info_args.num_entries <= num_vm_entries) {
+				num_vm_entries = vm_info_args.num_entries;
+				break;
+			} else {
+				num_vm_entries = vm_info_args.num_entries;
+			}
 		}
 
 		boinfo->num_of_vms = num_vm_entries;
Signature · 1 line
2.52.0
Reviewed-by on v1 13/26
#14 lore raw

Re: [RFC 13/26] plugins/amdgpu: Consolidate vm_info collection

(Re-sending, hopefully without html content)

This ioctl shouldn't ever need more than two calls (the first call should give the right size for the second) but this is cleaner.
Nitpick: criu seems to use "while (1)" more than "for (;;)". Could this be changed here?

With that change, this is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 13/26] plugins/amdgpu: Consolidate vm_info collection

Instead of open coding the same ioctl twice we can put it in a loop from which we break out once we have allocated enough space for all objects.

While at it we add error handling for the memory allocation.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 47 ++++++++++++++++--------------
 1 file changed, 25 insertions(+), 22 deletions(-)
plugins/amdgpu/amdgpu_plugin_drm.c +25 -22
index 9df755450dc9..baa560537813 100644
@@ -302,8 +302,7 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
        for (int i = 0; i < num_bos; i++) {
                int num_vm_entries = 8;
-               struct drm_amdgpu_gem_vm_entry *vm_info_entries;
-               struct drm_amdgpu_gem_op vm_info_args = { 0 };
+               struct drm_amdgpu_gem_vm_entry *vm_info_entries = NULL;
                DrmBoEntry *boinfo = rd->bo_entries[i];
                struct drm_amdgpu_gem_list_handles_entry handle_entry = list_handles_entries[i];
                union drm_amdgpu_gem_mmap mmap_args = { 0 };
@@ -333,32 +332,36 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
                boinfo->offset = mmap_args.out.addr_ptr;
 
-               vm_info_entries = xzalloc(sizeof(struct drm_amdgpu_gem_vm_entry) * num_vm_entries);
-               vm_info_args.handle = handle_entry.gem_handle;
-               vm_info_args.num_entries = num_vm_entries;
-               vm_info_args.value = (uintptr_t)vm_info_entries;
-               vm_info_args.op = AMDGPU_GEM_OP_GET_MAPPING_INFO;
-               ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP, &vm_info_args);
-               if (ret) {
-                       pr_perror("Failed to call vm info ioctl");
-                       goto exit;
-               }
+               for (;;) {
+                       struct drm_amdgpu_gem_op vm_info_args = {
+                               .handle = handle_entry.gem_handle,
+                               .num_entries = num_vm_entries,
+                               .op = AMDGPU_GEM_OP_GET_MAPPING_INFO,
+                       };
+
+                       if (vm_info_entries)
+                               xfree(vm_info_entries);
+                       vm_info_entries = xzalloc(sizeof(*vm_info_entries) *
+                                                 num_vm_entries);
+                       if (!vm_info_entries) {
+                               ret = -ENOMEM;
+                               goto exit;
+                       }
 
-               if (vm_info_args.num_entries > num_vm_entries) {
-                       num_vm_entries = vm_info_args.num_entries;
-                       xfree(vm_info_entries);
-                       vm_info_entries = xzalloc(sizeof(struct drm_amdgpu_gem_vm_entry) * num_vm_entries);
-                       vm_info_args.handle = handle_entry.gem_handle;
-                       vm_info_args.num_entries = num_vm_entries;
                        vm_info_args.value = (uintptr_t)vm_info_entries;
-                       vm_info_args.op = AMDGPU_GEM_OP_GET_MAPPING_INFO;
-                       ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP, &vm_info_args);
+                       ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP,
+                                      &vm_info_args);
                        if (ret) {
                                pr_perror("Failed to call vm info ioctl");
                                goto exit;
                        }
-               } else {
-                       num_vm_entries = vm_info_args.num_entries;
+
+                       if (vm_info_args.num_entries <= num_vm_entries) {
+                               num_vm_entries = vm_info_args.num_entries;
+                               break;
+                       } else {
+                               num_vm_entries = vm_info_args.num_entries;
+                       }
                }
 
                boinfo->num_of_vms = num_vm_entries;

--
2.52.0

#15 lore raw

Re: [RFC 13/26] plugins/amdgpu: Consolidate vm_info collection

Quoted 3 lines · David

(Re-sending, hopefully without html content)

This ioctl shouldn't ever need more than two calls (the first call should give the right size for the second) but this is cleaner.

Right, I also thought it is cleaner to to repeat the args init block and all twice, glad you agree.

Quoted 1 line

Nitpick: criu seems to use "while (1)" more than "for (;;)". Could this be changed here?

Ack, I marked this as a todo for when it is clearer how things will proceed.

Quoted 3 lines

With that change, this is

Thank you!

Tvrtko

Quoted 91 lines

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 13/26] plugins/amdgpu: Consolidate vm_info collection

Instead of open coding the same ioctl twice we can put it in a loop from which we break out once we have allocated enough space for all objects.

While at it we add error handling for the memory allocation.

---
  plugins/amdgpu/amdgpu_plugin_drm.c | 47 ++++++++++++++++--------------
  1 file changed, 25 insertions(+), 22 deletions(-)
plugins/amdgpu/amdgpu_plugin_drm.c +25 -23
index 9df755450dc9..baa560537813 100644
@@ -302,8 +302,7 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
         for (int i = 0; i < num_bos; i++) {
                 int num_vm_entries = 8;
-               struct drm_amdgpu_gem_vm_entry *vm_info_entries;
-               struct drm_amdgpu_gem_op vm_info_args = { 0 };
+               struct drm_amdgpu_gem_vm_entry *vm_info_entries = NULL;
                 DrmBoEntry *boinfo = rd->bo_entries[i];
                 struct drm_amdgpu_gem_list_handles_entry handle_entry = list_handles_entries[i];
                 union drm_amdgpu_gem_mmap mmap_args = { 0 };
@@ -333,32 +332,36 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
                 boinfo->offset = mmap_args.out.addr_ptr;
 
-               vm_info_entries = xzalloc(sizeof(struct drm_amdgpu_gem_vm_entry) * num_vm_entries);
-               vm_info_args.handle = handle_entry.gem_handle;
-               vm_info_args.num_entries = num_vm_entries;
-               vm_info_args.value = (uintptr_t)vm_info_entries;
-               vm_info_args.op = AMDGPU_GEM_OP_GET_MAPPING_INFO;
-               ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP, &vm_info_args);
-               if (ret) {
-                       pr_perror("Failed to call vm info ioctl");
-                       goto exit;
-               }
+               for (;;) {
+                       struct drm_amdgpu_gem_op vm_info_args = {
+                               .handle = handle_entry.gem_handle,
+                               .num_entries = num_vm_entries,
+                               .op = AMDGPU_GEM_OP_GET_MAPPING_INFO,
+                       };
+
+                       if (vm_info_entries)
+                               xfree(vm_info_entries);
+                       vm_info_entries = xzalloc(sizeof(*vm_info_entries) *
+                                                 num_vm_entries);
+                       if (!vm_info_entries) {
+                               ret = -ENOMEM;
+                               goto exit;
+                       }
 
-               if (vm_info_args.num_entries > num_vm_entries) {
-                       num_vm_entries = vm_info_args.num_entries;
-                       xfree(vm_info_entries);
-                       vm_info_entries = xzalloc(sizeof(struct drm_amdgpu_gem_vm_entry) * num_vm_entries);
-                       vm_info_args.handle = handle_entry.gem_handle;
-                       vm_info_args.num_entries = num_vm_entries;
                         vm_info_args.value = (uintptr_t)vm_info_entries;
-                       vm_info_args.op = AMDGPU_GEM_OP_GET_MAPPING_INFO;
-                       ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP, &vm_info_args);
+                       ret = drmIoctl(fd, DRM_IOCTL_AMDGPU_GEM_OP,
+                                      &vm_info_args);
                         if (ret) {
                                 pr_perror("Failed to call vm info ioctl");
                                 goto exit;
                         }
-               } else {
-                       num_vm_entries = vm_info_args.num_entries;
+
+                       if (vm_info_args.num_entries <= num_vm_entries) {
+                               num_vm_entries = vm_info_args.num_entries;
+                               break;
+                       } else {
+                               num_vm_entries = vm_info_args.num_entries;
+                       }
                 }
 
                 boinfo->num_of_vms = num_vm_entries;
--

2.52.0

#16 lore raw

RFC 14/26 plugins/amdgpu: Remove plugin_log_msg()

In reply to Tvrtko Ursulin

The extra debug build log helper does not seem very useful and some of them do not even compile. Lets just remove it and replace with the standard pr_debug. In case of too much noise, we can later re-evaluate to remove some of the not very useful log messages.

---
 plugins/amdgpu/amdgpu_plugin.c      | 42 ++++++++++++++++-------------
 plugins/amdgpu/amdgpu_plugin_drm.c  |  5 ++--
 plugins/amdgpu/amdgpu_plugin_util.h |  9 -------
 3 files changed, 27 insertions(+), 29 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +24 -18
index 06df63de2434..b3cc3a12ce2e 100644
@@ -570,7 +570,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
 	src_bo_size = (type == SDMA_OP_VRAM_WRITE) ? buffer_bo_size : size;
 	dst_bo_size = (type == SDMA_OP_VRAM_READ) ? buffer_bo_size : size;
 
-	plugin_log_msg("Enter %s\n", __func__);
+	pr_debug("Enter %s\n", __func__);
 
 	/* prepare src buffer */
 	switch (type) {
@@ -605,7 +605,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
 		pr_perror("failed to GPU map the src BO");
 		goto err_src_bo_map;
 	}
-	plugin_log_msg("Source BO: GPU VA: %lx, size: %lx\n", gpu_addr_src, src_bo_size);
+	pr_debug("Source BO: GPU VA: %lx, size: %lx\n",
+		 gpu_addr_src, src_bo_size);
 
 	/* prepare dest buffer */
 	switch (type) {
@@ -640,7 +641,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
 		pr_perror("failed to GPU map the dest BO");
 		goto err_dst_bo_map;
 	}
-	plugin_log_msg("Dest BO: GPU VA: %lx, size: %lx\n", gpu_addr_dst, dst_bo_size);
+	pr_debug("Dest BO: GPU VA: %lx, size: %lx\n",
+		 gpu_addr_dst, dst_bo_size);
 
 	/* prepare ring buffer/indirect buffer for command submission
 	 * each copy packet is 7 dwords so we need to alloc 28x size for ib
@@ -651,7 +653,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
 		pr_perror("failed to allocate and map ib/rb");
 		goto err_ib_gpu_alloc;
 	}
-	plugin_log_msg("Indirect BO: GPU VA: %lx, size: %lx\n", gpu_addr_ib, packets_per_buffer * 28);
+	pr_debug("Indirect BO: GPU VA: %" PRIx64 ", size: %d\n",
+		 gpu_addr_ib, packets_per_buffer * 28);
 
 	resources[0] = h_bo_src;
 	resources[1] = h_bo_dst;
@@ -792,7 +795,7 @@ err_src_va:
 	err2 = amdgpu_bo_free(h_bo_src);
 	if (err2)
 		pr_perror("src bo free failed");
-	plugin_log_msg("Leaving sdma_copy_bo, err = %d\n", err);
+	pr_debug("Leaving sdma_copy_bo, err = %d\n", err);
 	return err;
 }
 
@@ -818,7 +821,7 @@ void *dump_bo_contents(void *_thread_data)
 		pr_perror("failed to initialize device");
 		goto exit;
 	}
-	plugin_log_msg("libdrm initialized successfully\n");
+	pr_debug("libdrm initialized successfully\n");
 
 	ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
 	if (ret) {
@@ -910,7 +913,7 @@ void *restore_bo_contents(void *_thread_data)
 		pr_perror("failed to initialize device");
 		goto exit;
 	}
-	plugin_log_msg("libdrm initialized successfully\n");
+	pr_debug("libdrm initialized successfully\n");
 
 	ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
 	if (ret) {
@@ -970,7 +973,8 @@ void *restore_bo_contents(void *_thread_data)
 			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
 			break;
 		}
-		plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+		pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+			 i);
 	}
 
 exit:
@@ -1004,7 +1008,7 @@ int check_hsakmt_shared_mem(uint64_t *shared_mem_size, uint32_t *shared_mem_magi
 	if (ret)
 		pr_perror("Failed to read shared mem magic");
 	else
-		plugin_log_msg("Shared mem magic:0x%x\n", *shared_mem_magic);
+		pr_debug("Shared mem magic:0x%x\n", *shared_mem_magic);
 
 	return 0;
 }
@@ -1644,8 +1648,9 @@ static int restore_bos(struct kfd_ioctl_criu_args *args, CriuKfd *e)
 		bo_bucket->offset = bo_entry->offset;
 		bo_bucket->alloc_flags = bo_entry->alloc_flags;
 
-		plugin_log_msg("BO [%d] gpu_id:%x addr:%llx size:%llx offset:%llx\n", i, bo_bucket->gpu_id,
-			       bo_bucket->addr, bo_bucket->size, bo_bucket->offset);
+		pr_debug("BO [%d] gpu_id:%x addr:%" PRIx64 " size:%" PRIx64 " offset:%" PRIx64 "\n",
+			 i, bo_bucket->gpu_id, bo_bucket->addr, bo_bucket->size,
+			 bo_bucket->offset);
 	}
 
 	pr_info("Restore BOs Ok\n");
@@ -1713,9 +1718,9 @@ static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKf
 			vma_md->new_pgoff = bo_bucket->restored_offset;
 			vma_md->fd = node_get_drm_render_device(tp_node);
 
-			plugin_log_msg("adding vma_entry:addr:0x%lx old-off:0x%lx "
-				       "new_off:0x%lx new_minor:%d\n",
-				       vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff, tp_node->drm_render_minor);
+			pr_debug("adding vma_entry:addr:0x%lx old-off:0x%lx new_off:0x%lx new_minor:%d\n",
+				 vma_md->vma_entry, vma_md->old_pgoff,
+				 vma_md->new_pgoff, tp_node->drm_render_minor);
 
 			list_add_tail(&vma_md->list, &update_vma_info_list);
 		}
@@ -1987,7 +1992,7 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 		return -1;
 	}
 
-	plugin_log_msg("read image file data\n");
+	pr_debug("read image file data\n");
 
 	/*
 	 * Initialize fd_next to be 1 greater than the biggest file descriptor in use by the target restore process.
@@ -2094,7 +2099,7 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
 	if (plugin_disabled)
 		return -ENOTSUP;
 
-	plugin_log_msg("Enter %s\n", __func__);
+	pr_debug("Enter %s\n", __func__);
 
 	strncpy(path, in_path, sizeof(path));
 
@@ -2137,8 +2142,9 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
 				*updated_fd = fd;
 			}
 
-			plugin_log_msg("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n", vma_md->old_pgoff, vma_md->new_pgoff,
-				       *updated_fd);
+			pr_debug("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n",
+				 vma_md->old_pgoff, vma_md->new_pgoff,
+				 *updated_fd);
 
 			return 1;
 		}
plugins/amdgpu/amdgpu_plugin_drm.c +3 -2
index baa560537813..d0f84b46997c 100644
@@ -173,7 +173,7 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
 		pr_perror("failed to initialize device");
 		goto exit;
 	}
-	plugin_log_msg("libdrm initialized successfully\n");
+	pr_debug("libdrm initialized successfully\n");
 
 	ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
 	if (ret) {
@@ -222,7 +222,8 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
 			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
 			break;
 		}
-		plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+		pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+			 i);
 
 		if (bo_contents_fp)
 			fclose(bo_contents_fp);
plugins/amdgpu/amdgpu_plugin_util.h +0 -9
index f5f752d0b0a6..014abf35ccb6 100644
@@ -25,15 +25,6 @@
 #endif
 #define LOG_PREFIX "amdgpu_plugin: "
 
-#ifdef DEBUG
-#define plugin_log_msg(fmt, ...) pr_debug(fmt, ##__VA_ARGS__)
-#else
-#define plugin_log_msg(fmt, ...) \
-	{                        \
-	}
-#endif
-
-
 /* Path where KFD device is surfaced */
 #define AMDGPU_KFD_DEVICE		"/dev/kfd"
 
Signature · 1 line
2.52.0
Reviewed-by on v1 14/26
#17 lore raw

Re: [RFC 14/26] plugins/amdgpu: Remove plugin_log_msg()

I suspect the sdma_copy_bo ones will end up being spammy but they can live for now.

This patch is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 14/26] plugins/amdgpu: Remove plugin_log_msg()

The extra debug build log helper does not seem very useful and some of them do not even compile. Lets just remove it and replace with the standard pr_debug. In case of too much noise, we can later re-evaluate to remove some of the not very useful log messages.

---
 plugins/amdgpu/amdgpu_plugin.c      | 42 ++++++++++++++++-------------
 plugins/amdgpu/amdgpu_plugin_drm.c  |  5 ++--
 plugins/amdgpu/amdgpu_plugin_util.h |  9 -------
 3 files changed, 27 insertions(+), 29 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +24 -18
index 06df63de2434..b3cc3a12ce2e 100644
@@ -570,7 +570,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
        src_bo_size = (type == SDMA_OP_VRAM_WRITE) ? buffer_bo_size : size;
        dst_bo_size = (type == SDMA_OP_VRAM_READ) ? buffer_bo_size : size;
 
-       plugin_log_msg("Enter %s\n", __func__);
+       pr_debug("Enter %s\n", __func__);
 
        /* prepare src buffer */
        switch (type) {
@@ -605,7 +605,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                pr_perror("failed to GPU map the src BO");
                goto err_src_bo_map;
        }
-       plugin_log_msg("Source BO: GPU VA: %lx, size: %lx\n", gpu_addr_src, src_bo_size);
+       pr_debug("Source BO: GPU VA: %lx, size: %lx\n",
+                gpu_addr_src, src_bo_size);
 
        /* prepare dest buffer */
        switch (type) {
@@ -640,7 +641,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                pr_perror("failed to GPU map the dest BO");
                goto err_dst_bo_map;
        }
-       plugin_log_msg("Dest BO: GPU VA: %lx, size: %lx\n", gpu_addr_dst, dst_bo_size);
+       pr_debug("Dest BO: GPU VA: %lx, size: %lx\n",
+                gpu_addr_dst, dst_bo_size);
 
        /* prepare ring buffer/indirect buffer for command submission
         * each copy packet is 7 dwords so we need to alloc 28x size for ib
@@ -651,7 +653,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                pr_perror("failed to allocate and map ib/rb");
                goto err_ib_gpu_alloc;
        }
-       plugin_log_msg("Indirect BO: GPU VA: %lx, size: %lx\n", gpu_addr_ib, packets_per_buffer * 28);
+       pr_debug("Indirect BO: GPU VA: %" PRIx64 ", size: %d\n",
+                gpu_addr_ib, packets_per_buffer * 28);
 
        resources[0] = h_bo_src;
        resources[1] = h_bo_dst;
@@ -792,7 +795,7 @@ err_src_va:
        err2 = amdgpu_bo_free(h_bo_src);
        if (err2)
                pr_perror("src bo free failed");
-       plugin_log_msg("Leaving sdma_copy_bo, err = %d\n", err);
+       pr_debug("Leaving sdma_copy_bo, err = %d\n", err);
        return err;
 }
 
@@ -818,7 +821,7 @@ void *dump_bo_contents(void *_thread_data)
                pr_perror("failed to initialize device");
                goto exit;
        }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
        ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
        if (ret) {
@@ -910,7 +913,7 @@ void *restore_bo_contents(void *_thread_data)
                pr_perror("failed to initialize device");
                goto exit;
        }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
        ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
        if (ret) {
@@ -970,7 +973,8 @@ void *restore_bo_contents(void *_thread_data)
                        pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                        break;
                }
-               plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+               pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+                        i);
        }
 
 exit:
@@ -1004,7 +1008,7 @@ int check_hsakmt_shared_mem(uint64_t *shared_mem_size, uint32_t *shared_mem_magi
        if (ret)
                pr_perror("Failed to read shared mem magic");
        else
-               plugin_log_msg("Shared mem magic:0x%x\n", *shared_mem_magic);
+               pr_debug("Shared mem magic:0x%x\n", *shared_mem_magic);
 
        return 0;
 }
@@ -1644,8 +1648,9 @@ static int restore_bos(struct kfd_ioctl_criu_args *args, CriuKfd *e)
                bo_bucket->offset = bo_entry->offset;
                bo_bucket->alloc_flags = bo_entry->alloc_flags;
 
-               plugin_log_msg("BO [%d] gpu_id:%x addr:%llx size:%llx offset:%llx\n", i, bo_bucket->gpu_id,
-                              bo_bucket->addr, bo_bucket->size, bo_bucket->offset);
+               pr_debug("BO [%d] gpu_id:%x addr:%" PRIx64 " size:%" PRIx64 " offset:%" PRIx64 "\n",
+                        i, bo_bucket->gpu_id, bo_bucket->addr, bo_bucket->size,
+                        bo_bucket->offset);
        }
 
        pr_info("Restore BOs Ok\n");
@@ -1713,9 +1718,9 @@ static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKf
                        vma_md->new_pgoff = bo_bucket->restored_offset;
                        vma_md->fd = node_get_drm_render_device(tp_node);
 
-                       plugin_log_msg("adding vma_entry:addr:0x%lx old-off:0x%lx "
-                                      "new_off:0x%lx new_minor:%d\n",
-                                      vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff, tp_node->drm_render_minor);
+                       pr_debug("adding vma_entry:addr:0x%lx old-off:0x%lx new_off:0x%lx new_minor:%d\n",
+                                vma_md->vma_entry, vma_md->old_pgoff,
+                                vma_md->new_pgoff, tp_node->drm_render_minor);
 
                        list_add_tail(&vma_md->list, &update_vma_info_list);
                }
@@ -1987,7 +1992,7 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
                return -1;
        }
 
-       plugin_log_msg("read image file data\n");
+       pr_debug("read image file data\n");
 
        /*
         * Initialize fd_next to be 1 greater than the biggest file descriptor in use by the target restore process.
@@ -2094,7 +2099,7 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
        if (plugin_disabled)
                return -ENOTSUP;
 
-       plugin_log_msg("Enter %s\n", __func__);
+       pr_debug("Enter %s\n", __func__);
 
        strncpy(path, in_path, sizeof(path));
 
@@ -2137,8 +2142,9 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
                                *updated_fd = fd;
                        }
 
-                       plugin_log_msg("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n", vma_md->old_pgoff, vma_md->new_pgoff,
-                                      *updated_fd);
+                       pr_debug("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n",
+                                vma_md->old_pgoff, vma_md->new_pgoff,
+                                *updated_fd);
 
                        return 1;
                }
plugins/amdgpu/amdgpu_plugin_drm.c +3 -2
index baa560537813..d0f84b46997c 100644
@@ -173,7 +173,7 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
                pr_perror("failed to initialize device");
                goto exit;
        }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
        ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
        if (ret) {
@@ -222,7 +222,8 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
                        pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                        break;
                }
-               plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+               pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+                        i);
 
                if (bo_contents_fp)
                        fclose(bo_contents_fp);
plugins/amdgpu/amdgpu_plugin_util.h +0 -9
index f5f752d0b0a6..014abf35ccb6 100644
@@ -25,15 +25,6 @@
 #endif
 #define LOG_PREFIX "amdgpu_plugin: "
 
-#ifdef DEBUG
-#define plugin_log_msg(fmt, ...) pr_debug(fmt, ##__VA_ARGS__)
-#else
-#define plugin_log_msg(fmt, ...) \
-       {                        \
-       }
-#endif
-
-
 /* Path where KFD device is surfaced */
 #define AMDGPU_KFD_DEVICE              "/dev/kfd"
 

--
2.52.0

#18 lore raw

Re: [RFC 14/26] plugins/amdgpu: Remove plugin_log_msg()

Quoted 1 line · David

I suspect the sdma_copy_bo ones will end up being spammy but they can live for now.

Perhaps it would be enough to move those pr_debugs under the relevant failure paths? That is, only for the error logs which can fail due VA conflicts. For the success case I don't think it is too useful to have them. And the "entering and leaving" function remove altogether?

Regards,

Tvrtko

Quoted 212 lines

This patch is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 14/26] plugins/amdgpu: Remove plugin_log_msg()

The extra debug build log helper does not seem very useful and some of them do not even compile. Lets just remove it and replace with the standard pr_debug. In case of too much noise, we can later re-evaluate to remove some of the not very useful log messages.

---
  plugins/amdgpu/amdgpu_plugin.c      | 42 ++++++++++++++++-------------
  plugins/amdgpu/amdgpu_plugin_drm.c  |  5 ++--
  plugins/amdgpu/amdgpu_plugin_util.h |  9 -------
  3 files changed, 27 insertions(+), 29 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +24 -18
index 06df63de2434..b3cc3a12ce2e 100644
@@ -570,7 +570,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
         src_bo_size = (type == SDMA_OP_VRAM_WRITE) ? buffer_bo_size : size;
         dst_bo_size = (type == SDMA_OP_VRAM_READ) ? buffer_bo_size : size;
 
-       plugin_log_msg("Enter %s\n", __func__);
+       pr_debug("Enter %s\n", __func__);
 
         /* prepare src buffer */
         switch (type) {
@@ -605,7 +605,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                 pr_perror("failed to GPU map the src BO");
                 goto err_src_bo_map;
         }
-       plugin_log_msg("Source BO: GPU VA: %lx, size: %lx\n", gpu_addr_src, src_bo_size);
+       pr_debug("Source BO: GPU VA: %lx, size: %lx\n",
+                gpu_addr_src, src_bo_size);
 
         /* prepare dest buffer */
         switch (type) {
@@ -640,7 +641,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                 pr_perror("failed to GPU map the dest BO");
                 goto err_dst_bo_map;
         }
-       plugin_log_msg("Dest BO: GPU VA: %lx, size: %lx\n", gpu_addr_dst, dst_bo_size);
+       pr_debug("Dest BO: GPU VA: %lx, size: %lx\n",
+                gpu_addr_dst, dst_bo_size);
 
         /* prepare ring buffer/indirect buffer for command submission
          * each copy packet is 7 dwords so we need to alloc 28x size for ib
@@ -651,7 +653,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                 pr_perror("failed to allocate and map ib/rb");
                 goto err_ib_gpu_alloc;
         }
-       plugin_log_msg("Indirect BO: GPU VA: %lx, size: %lx\n", gpu_addr_ib, packets_per_buffer * 28);
+       pr_debug("Indirect BO: GPU VA: %" PRIx64 ", size: %d\n",
+                gpu_addr_ib, packets_per_buffer * 28);
 
         resources[0] = h_bo_src;
         resources[1] = h_bo_dst;
@@ -792,7 +795,7 @@ err_src_va:
         err2 = amdgpu_bo_free(h_bo_src);
         if (err2)
                 pr_perror("src bo free failed");
-       plugin_log_msg("Leaving sdma_copy_bo, err = %d\n", err);
+       pr_debug("Leaving sdma_copy_bo, err = %d\n", err);
         return err;
  }
 
@@ -818,7 +821,7 @@ void *dump_bo_contents(void *_thread_data)
                 pr_perror("failed to initialize device");
                 goto exit;
         }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
         ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
         if (ret) {
@@ -910,7 +913,7 @@ void *restore_bo_contents(void *_thread_data)
                 pr_perror("failed to initialize device");
                 goto exit;
         }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
         ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
         if (ret) {
@@ -970,7 +973,8 @@ void *restore_bo_contents(void *_thread_data)
                         pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                         break;
                 }
-               plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+               pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+                        i);
         }
 
  exit:
@@ -1004,7 +1008,7 @@ int check_hsakmt_shared_mem(uint64_t *shared_mem_size, uint32_t *shared_mem_magi
         if (ret)
                 pr_perror("Failed to read shared mem magic");
         else
-               plugin_log_msg("Shared mem magic:0x%x\n", *shared_mem_magic);
+               pr_debug("Shared mem magic:0x%x\n", *shared_mem_magic);
 
         return 0;
  }
@@ -1644,8 +1648,9 @@ static int restore_bos(struct kfd_ioctl_criu_args *args, CriuKfd *e)
                 bo_bucket->offset = bo_entry->offset;
                 bo_bucket->alloc_flags = bo_entry->alloc_flags;
 
-               plugin_log_msg("BO [%d] gpu_id:%x addr:%llx size:%llx offset:%llx\n", i, bo_bucket->gpu_id,
-                              bo_bucket->addr, bo_bucket->size, bo_bucket->offset);
+               pr_debug("BO [%d] gpu_id:%x addr:%" PRIx64 " size:%" PRIx64 " offset:%" PRIx64 "\n",
+                        i, bo_bucket->gpu_id, bo_bucket->addr, bo_bucket->size,
+                        bo_bucket->offset);
         }
 
         pr_info("Restore BOs Ok\n");
@@ -1713,9 +1718,9 @@ static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKf
                         vma_md->new_pgoff = bo_bucket->restored_offset;
                         vma_md->fd = node_get_drm_render_device(tp_node);
 
-                       plugin_log_msg("adding vma_entry:addr:0x%lx old-off:0x%lx "
-                                      "new_off:0x%lx new_minor:%d\n",
-                                      vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff, tp_node->drm_render_minor);
+                       pr_debug("adding vma_entry:addr:0x%lx old-off:0x%lx new_off:0x%lx new_minor:%d\n",
+                                vma_md->vma_entry, vma_md->old_pgoff,
+                                vma_md->new_pgoff, tp_node->drm_render_minor);
 
                         list_add_tail(&vma_md->list, &update_vma_info_list);
                 }
@@ -1987,7 +1992,7 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
                 return -1;
         }
 
-       plugin_log_msg("read image file data\n");
+       pr_debug("read image file data\n");
 
         /*
          * Initialize fd_next to be 1 greater than the biggest file descriptor in use by the target restore process.
@@ -2094,7 +2099,7 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
         if (plugin_disabled)
                 return -ENOTSUP;
 
-       plugin_log_msg("Enter %s\n", __func__);
+       pr_debug("Enter %s\n", __func__);
 
         strncpy(path, in_path, sizeof(path));
 
@@ -2137,8 +2142,9 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
                                 *updated_fd = fd;
                         }
 
-                       plugin_log_msg("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n", vma_md->old_pgoff, vma_md->new_pgoff,
-                                      *updated_fd);
+                       pr_debug("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n",
+                                vma_md->old_pgoff, vma_md->new_pgoff,
+                                *updated_fd);
 
                         return 1;
                 }
plugins/amdgpu/amdgpu_plugin_drm.c +3 -2
index baa560537813..d0f84b46997c 100644
@@ -173,7 +173,7 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
                 pr_perror("failed to initialize device");
                 goto exit;
         }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
         ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
         if (ret) {
@@ -222,7 +222,8 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
                         pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                         break;
                 }
-               plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+               pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+                        i);
 
                 if (bo_contents_fp)
                         fclose(bo_contents_fp);
plugins/amdgpu/amdgpu_plugin_util.h +0 -10
index f5f752d0b0a6..014abf35ccb6 100644
@@ -25,15 +25,6 @@
  #endif
  #define LOG_PREFIX "amdgpu_plugin: "
 
-#ifdef DEBUG
-#define plugin_log_msg(fmt, ...) pr_debug(fmt, ##__VA_ARGS__)
-#else
-#define plugin_log_msg(fmt, ...) \
-       {                        \
-       }
-#endif
-
-
  /* Path where KFD device is surfaced */
  #define AMDGPU_KFD_DEVICE              "/dev/kfd"
 
--

2.52.0

#19 lore raw

Re: [RFC 14/26] plugins/amdgpu: Remove plugin_log_msg()

Quoted 8 lines
Quoted 3 lines

I suspect the sdma_copy_bo ones will end up being spammy but they can live for now.

Perhaps it would be enough to move those pr_debugs under the relevant failure paths? That is, only for the error logs which can fail due VA conflicts. For the success case I don't think it is too useful to have them. And the "entering and leaving" function remove altogether?

Sounds good to me. As far as I know, these debug prints never get enabled, so no one will raise a fuss at their removal.

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Monday, March 2, 2026 4:09 AM
To: Francis, David; [email protected]
Subject: Re: [RFC 14/26] plugins/amdgpu: Remove plugin_log_msg()

Quoted 1 line · David

I suspect the sdma_copy_bo ones will end up being spammy but they can live for now.

Perhaps it would be enough to move those pr_debugs under the relevant failure paths? That is, only for the error logs which can fail due VA conflicts. For the success case I don't think it is too useful to have them. And the "entering and leaving" function remove altogether?

Regards,

Tvrtko

Quoted 212 lines

This patch is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 14/26] plugins/amdgpu: Remove plugin_log_msg()

The extra debug build log helper does not seem very useful and some of them do not even compile. Lets just remove it and replace with the standard pr_debug. In case of too much noise, we can later re-evaluate to remove some of the not very useful log messages.

---
  plugins/amdgpu/amdgpu_plugin.c      | 42 ++++++++++++++++-------------
  plugins/amdgpu/amdgpu_plugin_drm.c  |  5 ++--
  plugins/amdgpu/amdgpu_plugin_util.h |  9 -------
  3 files changed, 27 insertions(+), 29 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +24 -18
index 06df63de2434..b3cc3a12ce2e 100644
@@ -570,7 +570,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
         src_bo_size = (type == SDMA_OP_VRAM_WRITE) ? buffer_bo_size : size;
         dst_bo_size = (type == SDMA_OP_VRAM_READ) ? buffer_bo_size : size;
 
-       plugin_log_msg("Enter %s\n", __func__);
+       pr_debug("Enter %s\n", __func__);
 
         /* prepare src buffer */
         switch (type) {
@@ -605,7 +605,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                 pr_perror("failed to GPU map the src BO");
                 goto err_src_bo_map;
         }
-       plugin_log_msg("Source BO: GPU VA: %lx, size: %lx\n", gpu_addr_src, src_bo_size);
+       pr_debug("Source BO: GPU VA: %lx, size: %lx\n",
+                gpu_addr_src, src_bo_size);
 
         /* prepare dest buffer */
         switch (type) {
@@ -640,7 +641,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                 pr_perror("failed to GPU map the dest BO");
                 goto err_dst_bo_map;
         }
-       plugin_log_msg("Dest BO: GPU VA: %lx, size: %lx\n", gpu_addr_dst, dst_bo_size);
+       pr_debug("Dest BO: GPU VA: %lx, size: %lx\n",
+                gpu_addr_dst, dst_bo_size);
 
         /* prepare ring buffer/indirect buffer for command submission
          * each copy packet is 7 dwords so we need to alloc 28x size for ib
@@ -651,7 +653,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
                 pr_perror("failed to allocate and map ib/rb");
                 goto err_ib_gpu_alloc;
         }
-       plugin_log_msg("Indirect BO: GPU VA: %lx, size: %lx\n", gpu_addr_ib, packets_per_buffer * 28);
+       pr_debug("Indirect BO: GPU VA: %" PRIx64 ", size: %d\n",
+                gpu_addr_ib, packets_per_buffer * 28);
 
         resources[0] = h_bo_src;
         resources[1] = h_bo_dst;
@@ -792,7 +795,7 @@ err_src_va:
         err2 = amdgpu_bo_free(h_bo_src);
         if (err2)
                 pr_perror("src bo free failed");
-       plugin_log_msg("Leaving sdma_copy_bo, err = %d\n", err);
+       pr_debug("Leaving sdma_copy_bo, err = %d\n", err);
         return err;
  }
 
@@ -818,7 +821,7 @@ void *dump_bo_contents(void *_thread_data)
                 pr_perror("failed to initialize device");
                 goto exit;
         }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
         ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
         if (ret) {
@@ -910,7 +913,7 @@ void *restore_bo_contents(void *_thread_data)
                 pr_perror("failed to initialize device");
                 goto exit;
         }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
         ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
         if (ret) {
@@ -970,7 +973,8 @@ void *restore_bo_contents(void *_thread_data)
                         pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                         break;
                 }
-               plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+               pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+                        i);
         }
 
  exit:
@@ -1004,7 +1008,7 @@ int check_hsakmt_shared_mem(uint64_t *shared_mem_size, uint32_t *shared_mem_magi
         if (ret)
                 pr_perror("Failed to read shared mem magic");
         else
-               plugin_log_msg("Shared mem magic:0x%x\n", *shared_mem_magic);
+               pr_debug("Shared mem magic:0x%x\n", *shared_mem_magic);
 
         return 0;
  }
@@ -1644,8 +1648,9 @@ static int restore_bos(struct kfd_ioctl_criu_args *args, CriuKfd *e)
                 bo_bucket->offset = bo_entry->offset;
                 bo_bucket->alloc_flags = bo_entry->alloc_flags;
 
-               plugin_log_msg("BO [%d] gpu_id:%x addr:%llx size:%llx offset:%llx\n", i, bo_bucket->gpu_id,
-                              bo_bucket->addr, bo_bucket->size, bo_bucket->offset);
+               pr_debug("BO [%d] gpu_id:%x addr:%" PRIx64 " size:%" PRIx64 " offset:%" PRIx64 "\n",
+                        i, bo_bucket->gpu_id, bo_bucket->addr, bo_bucket->size,
+                        bo_bucket->offset);
         }
 
         pr_info("Restore BOs Ok\n");
@@ -1713,9 +1718,9 @@ static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKf
                         vma_md->new_pgoff = bo_bucket->restored_offset;
                         vma_md->fd = node_get_drm_render_device(tp_node);
 
-                       plugin_log_msg("adding vma_entry:addr:0x%lx old-off:0x%lx "
-                                      "new_off:0x%lx new_minor:%d\n",
-                                      vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff, tp_node->drm_render_minor);
+                       pr_debug("adding vma_entry:addr:0x%lx old-off:0x%lx new_off:0x%lx new_minor:%d\n",
+                                vma_md->vma_entry, vma_md->old_pgoff,
+                                vma_md->new_pgoff, tp_node->drm_render_minor);
 
                         list_add_tail(&vma_md->list, &update_vma_info_list);
                 }
@@ -1987,7 +1992,7 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
                 return -1;
         }
 
-       plugin_log_msg("read image file data\n");
+       pr_debug("read image file data\n");
 
         /*
          * Initialize fd_next to be 1 greater than the biggest file descriptor in use by the target restore process.
@@ -2094,7 +2099,7 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
         if (plugin_disabled)
                 return -ENOTSUP;
 
-       plugin_log_msg("Enter %s\n", __func__);
+       pr_debug("Enter %s\n", __func__);
 
         strncpy(path, in_path, sizeof(path));
 
@@ -2137,8 +2142,9 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
                                 *updated_fd = fd;
                         }
 
-                       plugin_log_msg("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n", vma_md->old_pgoff, vma_md->new_pgoff,
-                                      *updated_fd);
+                       pr_debug("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n",
+                                vma_md->old_pgoff, vma_md->new_pgoff,
+                                *updated_fd);
 
                         return 1;
                 }
plugins/amdgpu/amdgpu_plugin_drm.c +3 -2
index baa560537813..d0f84b46997c 100644
@@ -173,7 +173,7 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
                 pr_perror("failed to initialize device");
                 goto exit;
         }
-       plugin_log_msg("libdrm initialized successfully\n");
+       pr_debug("libdrm initialized successfully\n");
 
         ret = amdgpu_query_gpu_info(h_dev, &gpu_info);
         if (ret) {
@@ -222,7 +222,8 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
                         pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                         break;
                 }
-               plugin_log_msg("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n", i);
+               pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
+                        i);
 
                 if (bo_contents_fp)
                         fclose(bo_contents_fp);
plugins/amdgpu/amdgpu_plugin_util.h +0 -10
index f5f752d0b0a6..014abf35ccb6 100644
@@ -25,15 +25,6 @@
  #endif
  #define LOG_PREFIX "amdgpu_plugin: "
 
-#ifdef DEBUG
-#define plugin_log_msg(fmt, ...) pr_debug(fmt, ##__VA_ARGS__)
-#else
-#define plugin_log_msg(fmt, ...) \
-       {                        \
-       }
-#endif
-
-
  /* Path where KFD device is surfaced */
  #define AMDGPU_KFD_DEVICE              "/dev/kfd"
 
--

2.52.0

#20 lore raw

RFC 15/26 plugins/amdgpu: Add plugin to inventory even if process has no vmas

In reply to Tvrtko Ursulin

Currently the plugin is added to the inventory only if there were amdgpu mapped vmas in the target process. This makes restoring a simple process with only an open DRM fd and some buffer objects not work.

Fix it by adding the plugin to the inventory when there were open DRM files as well.

---
 plugins/amdgpu/amdgpu_plugin.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +24 -9
index b3cc3a12ce2e..c9c1c8278d74 100644
@@ -448,6 +448,22 @@ struct thread_data {
 	int id; /* File ID used by CRIU to identify KFD image for this process */
 };
 
+static int amdgpu_add_to_inventory(void)
+{
+	int ret;
+
+	if (plugin_added_to_inventory)
+		return 0;
+
+	ret = add_inventory_plugin(CR_PLUGIN_DESC.name);
+	if (ret)
+		pr_err("Failed to add AMDGPU plugin to inventory image\n");
+	else
+		plugin_added_to_inventory = true;
+
+	return 0;
+}
+
 int amdgpu_plugin_handle_device_vma(int fd, const struct stat *st_buf)
 {
 	struct stat st_kfd;
@@ -471,13 +487,8 @@ int amdgpu_plugin_handle_device_vma(int fd, const struct stat *st_buf)
 	if (ret)
 		pr_perror("%s(), Can't handle VMAs of input device", __func__);
 
-	if (!ret && !plugin_added_to_inventory) {
-		ret = add_inventory_plugin(CR_PLUGIN_DESC.name);
-		if (ret)
-			pr_err("Failed to add AMDGPU plugin to inventory image\n");
-		else
-			plugin_added_to_inventory = true;
-	}
+	if (!ret)
+		ret = amdgpu_add_to_inventory();
 
 	return ret;
 }
@@ -1458,8 +1469,12 @@ int amdgpu_plugin_dump_file(int fd, int id)
 		if (ret)
 			return ret;
 
-		/* Need to return success here so that criu can call plugins for renderD nodes */
-		return try_dump_dmabuf_list();
+		ret = try_dump_dmabuf_list();
+
+		if (!ret)
+			ret = amdgpu_add_to_inventory();
+
+		return ret;
 	}
 
 	pr_info("%s() called for fd = %d\n", __func__, major(st.st_rdev));
Signature · 1 line
2.52.0
Reviewed-by on v1 15/26
#21 lore raw

Re: [RFC 15/26] plugins/amdgpu: Add plugin to inventory even if process has no vmas

This should also help in the case where kfd is open but no mapping exists

This patch is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 15/26] plugins/amdgpu: Add plugin to inventory even if process has no vmas

Currently the plugin is added to the inventory only if there were amdgpu mapped vmas in the target process. This makes restoring a simple process with only an open DRM fd and some buffer objects not work.

Fix it by adding the plugin to the inventory when there were open DRM files as well.

---
 plugins/amdgpu/amdgpu_plugin.c | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +24 -9
index b3cc3a12ce2e..c9c1c8278d74 100644
@@ -448,6 +448,22 @@ struct thread_data {
        int id; /* File ID used by CRIU to identify KFD image for this process */
 };
 
+static int amdgpu_add_to_inventory(void)
+{
+       int ret;
+
+       if (plugin_added_to_inventory)
+               return 0;
+
+       ret = add_inventory_plugin(CR_PLUGIN_DESC.name);
+       if (ret)
+               pr_err("Failed to add AMDGPU plugin to inventory image\n");
+       else
+               plugin_added_to_inventory = true;
+
+       return 0;
+}
+
 int amdgpu_plugin_handle_device_vma(int fd, const struct stat *st_buf)
 {
        struct stat st_kfd;
@@ -471,13 +487,8 @@ int amdgpu_plugin_handle_device_vma(int fd, const struct stat *st_buf)
        if (ret)
                pr_perror("%s(), Can't handle VMAs of input device", __func__);
 
-       if (!ret && !plugin_added_to_inventory) {
-               ret = add_inventory_plugin(CR_PLUGIN_DESC.name);
-               if (ret)
-                       pr_err("Failed to add AMDGPU plugin to inventory image\n");
-               else
-                       plugin_added_to_inventory = true;
-       }
+       if (!ret)
+               ret = amdgpu_add_to_inventory();
 
        return ret;
 }
@@ -1458,8 +1469,12 @@ int amdgpu_plugin_dump_file(int fd, int id)
                if (ret)
                        return ret;
 
-               /* Need to return success here so that criu can call plugins for renderD nodes */
-               return try_dump_dmabuf_list();
+               ret = try_dump_dmabuf_list();
+
+               if (!ret)
+                       ret = amdgpu_add_to_inventory();
+
+               return ret;
        }
 
        pr_info("%s() called for fd = %d\n", __func__, major(st.st_rdev));

--
2.52.0

#22 lore raw

RFC 16/26 plugins/amdgpu: Move drm file dump and restore into helpers

In reply to Tvrtko Ursulin

Given that dump and restore of KFD vs DRM forks into completely parallel flows inside the respective plugin callbacks, lets split it out by extracting into helpers. This de-clutters the code flow and makes it obvious which part is done where.

---
 plugins/amdgpu/amdgpu_plugin.c        | 273 ++++++++++++++------------
 plugins/amdgpu/amdgpu_plugin_dmabuf.c |   2 +
 2 files changed, 145 insertions(+), 130 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +143 -130
index c9c1c8278d74..9ba12bd3a466 100644
@@ -1412,6 +1412,30 @@ exit:
 	return ret;
 }
 
+static int amdgpu_plugin_dump_drm_file(int fd, int id, struct stat *st)
+{
+	int ret;
+
+	/* This is RenderD dumper plugin, for now just save renderD
+	 * minor number to be used during restore. In later phases this
+	 * needs to save more data for video decode etc.
+	 */
+	ret = amdgpu_plugin_drm_dump_file(fd, id, st);
+	if (ret)
+		return ret;
+
+	ret = record_dumped_fd(fd, true);
+	if (ret)
+		return ret;
+
+	ret = try_dump_dmabuf_list();
+
+	if (!ret)
+		ret = amdgpu_add_to_inventory();
+
+	return ret;
+}
+
 int amdgpu_plugin_dump_file(int fd, int id)
 {
 	struct kfd_ioctl_criu_args args = { 0 };
@@ -1450,35 +1474,14 @@ int amdgpu_plugin_dump_file(int fd, int id)
 		pr_perror("Failed to get dmabuf info");
 		return -1;
 	}
-	if (ret == 0) {
-		pr_info("Dumping dmabuf fd = %d\n", fd);
-		return amdgpu_plugin_dmabuf_dump(fd, id);
-	}
-
-	if (major(st.st_rdev) != major(st_kfd.st_rdev) || minor(st.st_rdev) != 0) {
-
-		/* This is RenderD dumper plugin, for now just save renderD
-		 * minor number to be used during restore. In later phases this
-		 * needs to save more data for video decode etc.
-		 */
-		ret = amdgpu_plugin_drm_dump_file(fd, id, &st);
-		if (ret)
-			return ret;
-
-		ret = record_dumped_fd(fd, true);
-		if (ret)
-			return ret;
-
-		ret = try_dump_dmabuf_list();
-
-		if (!ret)
-			ret = amdgpu_add_to_inventory();
-
-		return ret;
-	}
 
 	pr_info("%s() called for fd = %d\n", __func__, major(st.st_rdev));
 
+	if (ret == 0)
+		return amdgpu_plugin_dmabuf_dump(fd, id);
+	else if (major(st.st_rdev) != major(st_kfd.st_rdev) || minor(st.st_rdev) != 0)
+		return amdgpu_plugin_dump_drm_file(fd, id, &st);
+
 	/* KFD only allows ioctl calls from the same process that opened the KFD file descriptor.
 	 * The existing /dev/kfd file descriptor that is passed in is only allowed to do IOCTL calls with
 	 * CAP_CHECKPOINT_RESTORE/CAP_SYS_ADMIN. So kernel_supports_criu() needs to open its own file descriptor to
@@ -1848,12 +1851,123 @@ exit:
 	return ret;
 }
 
+static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
+{
+	char img_path[PATH_MAX];
+	struct tp_node *tp_node;
+	uint32_t target_gpu_id;
+	CriuRenderNode *rd;
+	unsigned char *buf;
+	size_t img_size;
+	FILE *img_fp;
+	int fd, ret;
+
+	/* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
+	 * be called before the plugin is called for kfd file descriptor.
+	 * TODO: Currently, this code will only work if this function is called for /dev/kfd
+	 * first as we assume restore_maps is already filled. Need to fix this later.
+	 */
+	snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
+
+	img_fp = open_img_file(img_path, false, &img_size);
+	if (!img_fp) {
+		ret = amdgpu_plugin_dmabuf_restore(id);
+		if (ret == 1) {
+			/* This is a dmabuf fd, but the corresponding buffer object that was
+			 * exported to make it has not yet been restored. Need to try again
+			 * later when the buffer object exists, so it can be re-exported.
+			 */
+			*retry_needed = true;
+			return 0;
+		}
+		return ret;
+	}
+	pr_info("Restoring RenderD %s\n", img_path);
+	pr_debug("RenderD Image file size:%ld\n", img_size);
+	buf = xmalloc(img_size);
+	if (!buf) {
+		pr_perror("Failed to allocate memory");
+		return -ENOMEM;
+	}
+
+	ret = read_fp(img_fp, buf, img_size);
+	if (ret) {
+		pr_perror("Unable to read from %s", img_path);
+		xfree(buf);
+		return -1;
+	}
+
+	rd = criu_render_node__unpack(NULL, img_size, buf);
+	if (rd == NULL) {
+		pr_perror("Unable to parse the RenderD message %d", id);
+		xfree(buf);
+		fclose(img_fp);
+		return -1;
+	}
+	fclose(img_fp);
+
+	pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
+
+	target_gpu_id = maps_get_dest_gpu(&restore_maps, rd->gpu_id);
+	if (!target_gpu_id) {
+		fd = -ENODEV;
+		goto fail;
+	}
+
+	tp_node = sys_get_node_by_gpu_id(&dest_topology, target_gpu_id);
+	if (!tp_node) {
+		fd = -ENODEV;
+		goto fail;
+	}
+
+	pr_info("render node destination gpu_id = 0x%04x\n", tp_node->gpu_id);
+
+	fd = node_get_drm_render_device(tp_node);
+	if (fd < 0) {
+		pr_err("Failed to open render device (minor:%d)\n", tp_node->drm_render_minor);
+		return -1;
+	}
+
+	ret = amdgpu_plugin_drm_restore_file(fd, rd);
+	if (ret == 1)
+		*retry_needed = true;
+	if (ret < 0) {
+		fd = ret;
+		goto fail;
+	}
+fail:
+	criu_render_node__free_unpacked(rd, NULL);
+	xfree(buf);
+	/*
+	 * We need to use the file descriptor used to create the BOs for mmap later, otherwise the kernel DRM
+	 * drivers will not allow the mmap. Therefore, we keep a copy of the file descriptor (stored in tp_node)
+	 * so that we can return it in amdgpu_plugin_update_vmamap later. Also, CRIU core will dup and close the
+	 * returned fd after this function returns, and this will make our fd invalid. So we return a dup'ed
+	 * copy of the fd. CRIU core owns the duplicated returned fd, and amdgpu_plugin owns the fd stored in
+	 * tp_node.
+	 */
+
+	if (fd < 0)
+		return fd;
+
+	if (!(*retry_needed)) {
+		fd = dup(fd);
+		if (fd == -1) {
+			pr_perror("unable to duplicate the render fd");
+			return -1;
+		}
+		return fd;
+	}
+
+	return 0;
+
+}
+
 int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 {
 	int ret = 0, fd;
 	char img_path[PATH_MAX];
 	unsigned char *buf;
-	CriuRenderNode *rd;
 	CriuKfd *e = NULL;
 	struct kfd_ioctl_criu_args args = { 0 };
 	size_t img_size;
@@ -1869,109 +1983,8 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 	snprintf(img_path, sizeof(img_path), IMG_KFD_FILE, id);
 
 	img_fp = open_img_file(img_path, false, &img_size);
-	if (!img_fp) {
-		struct tp_node *tp_node;
-		uint32_t target_gpu_id;
-
-		/* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
-		 * be called before the plugin is called for kfd file descriptor.
-		 * TODO: Currently, this code will only work if this function is called for /dev/kfd
-		 * first as we assume restore_maps is already filled. Need to fix this later.
-		 */
-		snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
-
-		img_fp = open_img_file(img_path, false, &img_size);
-		if (!img_fp) {
-			ret = amdgpu_plugin_dmabuf_restore(id);
-			if (ret == 1) {
-				/* This is a dmabuf fd, but the corresponding buffer object that was
-				 * exported to make it has not yet been restored. Need to try again
-				 * later when the buffer object exists, so it can be re-exported.
-				 */
-				*retry_needed = true;
-				return 0;
-			}
-			return ret;
-		}
-		pr_info("Restoring RenderD %s\n", img_path);
-		pr_debug("RenderD Image file size:%ld\n", img_size);
-		buf = xmalloc(img_size);
-		if (!buf) {
-			pr_perror("Failed to allocate memory");
-			return -ENOMEM;
-		}
-
-		ret = read_fp(img_fp, buf, img_size);
-		if (ret) {
-			pr_perror("Unable to read from %s", img_path);
-			xfree(buf);
-			return -1;
-		}
-
-		rd = criu_render_node__unpack(NULL, img_size, buf);
-		if (rd == NULL) {
-			pr_perror("Unable to parse the RenderD message %d", id);
-			xfree(buf);
-			fclose(img_fp);
-			return -1;
-		}
-		fclose(img_fp);
-
-		pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
-
-		target_gpu_id = maps_get_dest_gpu(&restore_maps, rd->gpu_id);
-		if (!target_gpu_id) {
-			fd = -ENODEV;
-			goto fail;
-		}
-
-		tp_node = sys_get_node_by_gpu_id(&dest_topology, target_gpu_id);
-		if (!tp_node) {
-			fd = -ENODEV;
-			goto fail;
-		}
-
-		pr_info("render node destination gpu_id = 0x%04x\n", tp_node->gpu_id);
-
-		fd = node_get_drm_render_device(tp_node);
-		if (fd < 0) {
-			pr_err("Failed to open render device (minor:%d)\n", tp_node->drm_render_minor);
-			return -1;
-		}
-
-		ret = amdgpu_plugin_drm_restore_file(fd, rd);
-		if (ret == 1)
-			*retry_needed = true;
-		if (ret < 0) {
-			fd = ret;
-			goto fail;
-		}
-	fail:
-		criu_render_node__free_unpacked(rd, NULL);
-		xfree(buf);
-		/*
-		 * We need to use the file descriptor used to create the BOs for mmap later, otherwise the kernel DRM
-		 * drivers will not allow the mmap. Therefore, we keep a copy of the file descriptor (stored in tp_node)
-		 * so that we can return it in amdgpu_plugin_update_vmamap later. Also, CRIU core will dup and close the
-		 * returned fd after this function returns, and this will make our fd invalid. So we return a dup'ed
-		 * copy of the fd. CRIU core owns the duplicated returned fd, and amdgpu_plugin owns the fd stored in
-		 * tp_node.
-		 */
-
-		if (fd < 0)
-			return fd;
-
-		if (!(*retry_needed)) {
-			fd = dup(fd);
-			if (fd == -1) {
-				pr_perror("unable to duplicate the render fd");
-				return -1;
-			}
-			return fd;
-		}
-
-		return 0;
-	}
+	if (!img_fp)
+		return amdgpu_plugin_restore_drm_file(id, retry_needed);
 
 	fd = open(AMDGPU_KFD_DEVICE, O_RDWR | O_CLOEXEC);
 	if (fd < 0) {
plugins/amdgpu/amdgpu_plugin_dmabuf.c +2 -0
index 93a975e16e83..859ff42bb76a 100644
@@ -160,6 +160,8 @@ int amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
 {
 	int ret;
 
+	pr_info("Dumping dmabuf fd %d\n", dmabuf_fd);
+
 	ret = __amdgpu_plugin_dmabuf_dump(dmabuf_fd, id);
 	if (ret == -EAGAIN) {
 		struct dmabuf *b = xmalloc(sizeof(*b));
Signature · 1 line
2.52.0
Reviewed-by on v1 16/26
#23 lore raw

Re: [RFC 16/26] plugins/amdgpu: Move drm file dump and restore into helpers

Refactor makes sense

This patch is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 16/26] plugins/amdgpu: Move drm file dump and restore into helpers

Given that dump and restore of KFD vs DRM forks into completely parallel flows inside the respective plugin callbacks, lets split it out by extracting into helpers. This de-clutters the code flow and makes it obvious which part is done where.

---
 plugins/amdgpu/amdgpu_plugin.c        | 273 ++++++++++++++------------
 plugins/amdgpu/amdgpu_plugin_dmabuf.c |   2 +
 2 files changed, 145 insertions(+), 130 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +143 -130
index c9c1c8278d74..9ba12bd3a466 100644
@@ -1412,6 +1412,30 @@ exit:
        return ret;
 }
 
+static int amdgpu_plugin_dump_drm_file(int fd, int id, struct stat *st)
+{
+       int ret;
+
+       /* This is RenderD dumper plugin, for now just save renderD
+        * minor number to be used during restore. In later phases this
+        * needs to save more data for video decode etc.
+        */
+       ret = amdgpu_plugin_drm_dump_file(fd, id, st);
+       if (ret)
+               return ret;
+
+       ret = record_dumped_fd(fd, true);
+       if (ret)
+               return ret;
+
+       ret = try_dump_dmabuf_list();
+
+       if (!ret)
+               ret = amdgpu_add_to_inventory();
+
+       return ret;
+}
+
 int amdgpu_plugin_dump_file(int fd, int id)
 {
        struct kfd_ioctl_criu_args args = { 0 };
@@ -1450,35 +1474,14 @@ int amdgpu_plugin_dump_file(int fd, int id)
                pr_perror("Failed to get dmabuf info");
                return -1;
        }
-       if (ret == 0) {
-               pr_info("Dumping dmabuf fd = %d\n", fd);
-               return amdgpu_plugin_dmabuf_dump(fd, id);
-       }
-
-       if (major(st.st_rdev) != major(st_kfd.st_rdev) || minor(st.st_rdev) != 0) {
-
-               /* This is RenderD dumper plugin, for now just save renderD
-                * minor number to be used during restore. In later phases this
-                * needs to save more data for video decode etc.
-                */
-               ret = amdgpu_plugin_drm_dump_file(fd, id, &st);
-               if (ret)
-                       return ret;
-
-               ret = record_dumped_fd(fd, true);
-               if (ret)
-                       return ret;
-
-               ret = try_dump_dmabuf_list();
-
-               if (!ret)
-                       ret = amdgpu_add_to_inventory();
-
-               return ret;
-       }
 
        pr_info("%s() called for fd = %d\n", __func__, major(st.st_rdev));
 
+       if (ret == 0)
+               return amdgpu_plugin_dmabuf_dump(fd, id);
+       else if (major(st.st_rdev) != major(st_kfd.st_rdev) || minor(st.st_rdev) != 0)
+               return amdgpu_plugin_dump_drm_file(fd, id, &st);
+
        /* KFD only allows ioctl calls from the same process that opened the KFD file descriptor.
         * The existing /dev/kfd file descriptor that is passed in is only allowed to do IOCTL calls with
         * CAP_CHECKPOINT_RESTORE/CAP_SYS_ADMIN. So kernel_supports_criu() needs to open its own file descriptor to
@@ -1848,12 +1851,123 @@ exit:
        return ret;
 }
 
+static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
+{
+       char img_path[PATH_MAX];
+       struct tp_node *tp_node;
+       uint32_t target_gpu_id;
+       CriuRenderNode *rd;
+       unsigned char *buf;
+       size_t img_size;
+       FILE *img_fp;
+       int fd, ret;
+
+       /* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
+        * be called before the plugin is called for kfd file descriptor.
+        * TODO: Currently, this code will only work if this function is called for /dev/kfd
+        * first as we assume restore_maps is already filled. Need to fix this later.
+        */
+       snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
+
+       img_fp = open_img_file(img_path, false, &img_size);
+       if (!img_fp) {
+               ret = amdgpu_plugin_dmabuf_restore(id);
+               if (ret == 1) {
+                       /* This is a dmabuf fd, but the corresponding buffer object that was
+                        * exported to make it has not yet been restored. Need to try again
+                        * later when the buffer object exists, so it can be re-exported.
+                        */
+                       *retry_needed = true;
+                       return 0;
+               }
+               return ret;
+       }
+       pr_info("Restoring RenderD %s\n", img_path);
+       pr_debug("RenderD Image file size:%ld\n", img_size);
+       buf = xmalloc(img_size);
+       if (!buf) {
+               pr_perror("Failed to allocate memory");
+               return -ENOMEM;
+       }
+
+       ret = read_fp(img_fp, buf, img_size);
+       if (ret) {
+               pr_perror("Unable to read from %s", img_path);
+               xfree(buf);
+               return -1;
+       }
+
+       rd = criu_render_node__unpack(NULL, img_size, buf);
+       if (rd == NULL) {
+               pr_perror("Unable to parse the RenderD message %d", id);
+               xfree(buf);
+               fclose(img_fp);
+               return -1;
+       }
+       fclose(img_fp);
+
+       pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
+
+       target_gpu_id = maps_get_dest_gpu(&restore_maps, rd->gpu_id);
+       if (!target_gpu_id) {
+               fd = -ENODEV;
+               goto fail;
+       }
+
+       tp_node = sys_get_node_by_gpu_id(&dest_topology, target_gpu_id);
+       if (!tp_node) {
+               fd = -ENODEV;
+               goto fail;
+       }
+
+       pr_info("render node destination gpu_id = 0x%04x\n", tp_node->gpu_id);
+
+       fd = node_get_drm_render_device(tp_node);
+       if (fd < 0) {
+               pr_err("Failed to open render device (minor:%d)\n", tp_node->drm_render_minor);
+               return -1;
+       }
+
+       ret = amdgpu_plugin_drm_restore_file(fd, rd);
+       if (ret == 1)
+               *retry_needed = true;
+       if (ret < 0) {
+               fd = ret;
+               goto fail;
+       }
+fail:
+       criu_render_node__free_unpacked(rd, NULL);
+       xfree(buf);
+       /*
+        * We need to use the file descriptor used to create the BOs for mmap later, otherwise the kernel DRM
+        * drivers will not allow the mmap. Therefore, we keep a copy of the file descriptor (stored in tp_node)
+        * so that we can return it in amdgpu_plugin_update_vmamap later. Also, CRIU core will dup and close the
+        * returned fd after this function returns, and this will make our fd invalid. So we return a dup'ed
+        * copy of the fd. CRIU core owns the duplicated returned fd, and amdgpu_plugin owns the fd stored in
+        * tp_node.
+        */
+
+       if (fd < 0)
+               return fd;
+
+       if (!(*retry_needed)) {
+               fd = dup(fd);
+               if (fd == -1) {
+                       pr_perror("unable to duplicate the render fd");
+                       return -1;
+               }
+               return fd;
+       }
+
+       return 0;
+
+}
+
 int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 {
        int ret = 0, fd;
        char img_path[PATH_MAX];
        unsigned char *buf;
-       CriuRenderNode *rd;
        CriuKfd *e = NULL;
        struct kfd_ioctl_criu_args args = { 0 };
        size_t img_size;
@@ -1869,109 +1983,8 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
        snprintf(img_path, sizeof(img_path), IMG_KFD_FILE, id);
 
        img_fp = open_img_file(img_path, false, &img_size);
-       if (!img_fp) {
-               struct tp_node *tp_node;
-               uint32_t target_gpu_id;
-
-               /* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
-                * be called before the plugin is called for kfd file descriptor.
-                * TODO: Currently, this code will only work if this function is called for /dev/kfd
-                * first as we assume restore_maps is already filled. Need to fix this later.
-                */
-               snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
-
-               img_fp = open_img_file(img_path, false, &img_size);
-               if (!img_fp) {
-                       ret = amdgpu_plugin_dmabuf_restore(id);
-                       if (ret == 1) {
-                               /* This is a dmabuf fd, but the corresponding buffer object that was
-                                * exported to make it has not yet been restored. Need to try again
-                                * later when the buffer object exists, so it can be re-exported.
-                                */
-                               *retry_needed = true;
-                               return 0;
-                       }
-                       return ret;
-               }
-               pr_info("Restoring RenderD %s\n", img_path);
-               pr_debug("RenderD Image file size:%ld\n", img_size);
-               buf = xmalloc(img_size);
-               if (!buf) {
-                       pr_perror("Failed to allocate memory");
-                       return -ENOMEM;
-               }
-
-               ret = read_fp(img_fp, buf, img_size);
-               if (ret) {
-                       pr_perror("Unable to read from %s", img_path);
-                       xfree(buf);
-                       return -1;
-               }
-
-               rd = criu_render_node__unpack(NULL, img_size, buf);
-               if (rd == NULL) {
-                       pr_perror("Unable to parse the RenderD message %d", id);
-                       xfree(buf);
-                       fclose(img_fp);
-                       return -1;
-               }
-               fclose(img_fp);
-
-               pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
-
-               target_gpu_id = maps_get_dest_gpu(&restore_maps, rd->gpu_id);
-               if (!target_gpu_id) {
-                       fd = -ENODEV;
-                       goto fail;
-               }
-
-               tp_node = sys_get_node_by_gpu_id(&dest_topology, target_gpu_id);
-               if (!tp_node) {
-                       fd = -ENODEV;
-                       goto fail;
-               }
-
-               pr_info("render node destination gpu_id = 0x%04x\n", tp_node->gpu_id);
-
-               fd = node_get_drm_render_device(tp_node);
-               if (fd < 0) {
-                       pr_err("Failed to open render device (minor:%d)\n", tp_node->drm_render_minor);
-                       return -1;
-               }
-
-               ret = amdgpu_plugin_drm_restore_file(fd, rd);
-               if (ret == 1)
-                       *retry_needed = true;
-               if (ret < 0) {
-                       fd = ret;
-                       goto fail;
-               }
-       fail:
-               criu_render_node__free_unpacked(rd, NULL);
-               xfree(buf);
-               /*
-                * We need to use the file descriptor used to create the BOs for mmap later, otherwise the kernel DRM
-                * drivers will not allow the mmap. Therefore, we keep a copy of the file descriptor (stored in tp_node)
-                * so that we can return it in amdgpu_plugin_update_vmamap later. Also, CRIU core will dup and close the
-                * returned fd after this function returns, and this will make our fd invalid. So we return a dup'ed
-                * copy of the fd. CRIU core owns the duplicated returned fd, and amdgpu_plugin owns the fd stored in
-                * tp_node.
-                */
-
-               if (fd < 0)
-                       return fd;
-
-               if (!(*retry_needed)) {
-                       fd = dup(fd);
-                       if (fd == -1) {
-                               pr_perror("unable to duplicate the render fd");
-                               return -1;
-                       }
-                       return fd;
-               }
-
-               return 0;
-       }
+       if (!img_fp)
+               return amdgpu_plugin_restore_drm_file(id, retry_needed);
 
        fd = open(AMDGPU_KFD_DEVICE, O_RDWR | O_CLOEXEC);
        if (fd < 0) {
plugins/amdgpu/amdgpu_plugin_dmabuf.c +2 -0
index 93a975e16e83..859ff42bb76a 100644
@@ -160,6 +160,8 @@ int amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
 {
        int ret;
 
+       pr_info("Dumping dmabuf fd %d\n", dmabuf_fd);
+
        ret = __amdgpu_plugin_dmabuf_dump(dmabuf_fd, id);
        if (ret == -EAGAIN) {
                struct dmabuf *b = xmalloc(sizeof(*b));

--
2.52.0

#24 lore raw

RFC 17/26 plugins/amdgpu: Use the load_img helper in drm file restore

In reply to Tvrtko Ursulin

Use the recently added image load helper in one more function which contains the exactly same flow of open-alloc-read.

---
 plugins/amdgpu/amdgpu_plugin.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +5 -20
index 9ba12bd3a466..a653ba1aa2d9 100644
@@ -1859,7 +1859,6 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
 	CriuRenderNode *rd;
 	unsigned char *buf;
 	size_t img_size;
-	FILE *img_fp;
 	int fd, ret;
 
 	/* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
@@ -1868,9 +1867,8 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
 	 * first as we assume restore_maps is already filled. Need to fix this later.
 	 */
 	snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
-
-	img_fp = open_img_file(img_path, false, &img_size);
-	if (!img_fp) {
+	ret = load_img(img_path, &buf, &img_size);
+	if (ret < 0) {
 		ret = amdgpu_plugin_dmabuf_restore(id);
 		if (ret == 1) {
 			/* This is a dmabuf fd, but the corresponding buffer object that was
@@ -1882,29 +1880,16 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
 		}
 		return ret;
 	}
-	pr_info("Restoring RenderD %s\n", img_path);
-	pr_debug("RenderD Image file size:%ld\n", img_size);
-	buf = xmalloc(img_size);
-	if (!buf) {
-		pr_perror("Failed to allocate memory");
-		return -ENOMEM;
-	}
 
-	ret = read_fp(img_fp, buf, img_size);
-	if (ret) {
-		pr_perror("Unable to read from %s", img_path);
-		xfree(buf);
-		return -1;
-	}
+	pr_info("Restoring RenderD %s image of %lu bytes\n",
+		img_path, img_size);
 
 	rd = criu_render_node__unpack(NULL, img_size, buf);
 	if (rd == NULL) {
-		pr_perror("Unable to parse the RenderD message %d", id);
+		pr_perror("Unable to parse the RenderD image %d", id);
 		xfree(buf);
-		fclose(img_fp);
 		return -1;
 	}
-	fclose(img_fp);
 
 	pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
 
Signature · 1 line
2.52.0
#25 lore raw

Re: [RFC 17/26] plugins/amdgpu: Use the load_img helper in drm file restore

Quoted 3 lines

Use the recently added image load helper in one more function which contains the exactly same flow of open-alloc-read.

Code looks cleaner for sure. How recently was this recently added image load helper added, and to what? Does this change CRIU's dependencies?

David

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 17/26] plugins/amdgpu: Use the load_img helper in drm file restore

Use the recently added image load helper in one more function which contains the exactly same flow of open-alloc-read.

---
 plugins/amdgpu/amdgpu_plugin.c | 25 +++++--------------------
 1 file changed, 5 insertions(+), 20 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +5 -20
index 9ba12bd3a466..a653ba1aa2d9 100644
@@ -1859,7 +1859,6 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
        CriuRenderNode *rd;
        unsigned char *buf;
        size_t img_size;
-       FILE *img_fp;
        int fd, ret;
 
        /* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
@@ -1868,9 +1867,8 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
         * first as we assume restore_maps is already filled. Need to fix this later.
         */
        snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
-
-       img_fp = open_img_file(img_path, false, &img_size);
-       if (!img_fp) {
+       ret = load_img(img_path, &buf, &img_size);
+       if (ret < 0) {
                ret = amdgpu_plugin_dmabuf_restore(id);
                if (ret == 1) {
                        /* This is a dmabuf fd, but the corresponding buffer object that was
@@ -1882,29 +1880,16 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
                }
                return ret;
        }
-       pr_info("Restoring RenderD %s\n", img_path);
-       pr_debug("RenderD Image file size:%ld\n", img_size);
-       buf = xmalloc(img_size);
-       if (!buf) {
-               pr_perror("Failed to allocate memory");
-               return -ENOMEM;
-       }
 
-       ret = read_fp(img_fp, buf, img_size);
-       if (ret) {
-               pr_perror("Unable to read from %s", img_path);
-               xfree(buf);
-               return -1;
-       }
+       pr_info("Restoring RenderD %s image of %lu bytes\n",
+               img_path, img_size);
 
        rd = criu_render_node__unpack(NULL, img_size, buf);
        if (rd == NULL) {
-               pr_perror("Unable to parse the RenderD message %d", id);
+               pr_perror("Unable to parse the RenderD image %d", id);
                xfree(buf);
-               fclose(img_fp);
                return -1;
        }
-       fclose(img_fp);
 
        pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
 

--
2.52.0

#26 lore raw

Re: [RFC 17/26] plugins/amdgpu: Use the load_img helper in drm file restore

Quoted 6 lines · David
Quoted 3 lines

Use the recently added image load helper in one more function which contains the exactly same flow of open-alloc-read.

Code looks cleaner for sure. How recently was this recently added image load helper added, and to what? Does this change CRIU's dependencies?

In patch 7/26. ;) Helper is simply a result of code consolidation.

Regards,

Tvrtko

Quoted 75 lines

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 17/26] plugins/amdgpu: Use the load_img helper in drm file restore

Use the recently added image load helper in one more function which contains the exactly same flow of open-alloc-read.

---
  plugins/amdgpu/amdgpu_plugin.c | 25 +++++--------------------
  1 file changed, 5 insertions(+), 20 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +5 -21
index 9ba12bd3a466..a653ba1aa2d9 100644
@@ -1859,7 +1859,6 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
         CriuRenderNode *rd;
         unsigned char *buf;
         size_t img_size;
-       FILE *img_fp;
         int fd, ret;
 
         /* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
@@ -1868,9 +1867,8 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
          * first as we assume restore_maps is already filled. Need to fix this later.
          */
         snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
-
-       img_fp = open_img_file(img_path, false, &img_size);
-       if (!img_fp) {
+       ret = load_img(img_path, &buf, &img_size);
+       if (ret < 0) {
                 ret = amdgpu_plugin_dmabuf_restore(id);
                 if (ret == 1) {
                         /* This is a dmabuf fd, but the corresponding buffer object that was
@@ -1882,29 +1880,16 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
                 }
                 return ret;
         }
-       pr_info("Restoring RenderD %s\n", img_path);
-       pr_debug("RenderD Image file size:%ld\n", img_size);
-       buf = xmalloc(img_size);
-       if (!buf) {
-               pr_perror("Failed to allocate memory");
-               return -ENOMEM;
-       }
 
-       ret = read_fp(img_fp, buf, img_size);
-       if (ret) {
-               pr_perror("Unable to read from %s", img_path);
-               xfree(buf);
-               return -1;
-       }
+       pr_info("Restoring RenderD %s image of %lu bytes\n",
+               img_path, img_size);
 
         rd = criu_render_node__unpack(NULL, img_size, buf);
         if (rd == NULL) {
-               pr_perror("Unable to parse the RenderD message %d", id);
+               pr_perror("Unable to parse the RenderD image %d", id);
                 xfree(buf);
-               fclose(img_fp);
                 return -1;
         }
-       fclose(img_fp);
 
         pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
 
--

2.52.0

#27 lore raw

RFC 18/26 plugins/amdgpu: Convert away from libc buffered file IO

In reply to Tvrtko Ursulin

Currently the code contains multiple places where callers expect the errno to be valid (implied by calling pr_perror to log file IO errors) after calling open_img_file(), read_fp() and write_fp() helpers. Problem there is that those helpers can destroy the errno courtesy of themselves emitting log messages via fprintf.

Furthermore, the callers then sometimes invent the errors to return back to the caller, or even outside of the plugin.

On top of that there is no benefit to buffered file IO given the plugin reads and writes in buffer object size chunks, so it it preferrable to just go direct and avoid any possibility of extra copying to and from libc temporary buffers.

So lets just convert it all in one swoop to POSIX IO and make sure correct errnos are always propagated to the caller (including to CRIU core).

Hopefully this removes all instances of incorrect pr_perror log messages.

---
 plugins/amdgpu/amdgpu_plugin.c        | 126 ++++++++++++--------------
 plugins/amdgpu/amdgpu_plugin_dmabuf.c |  14 ++-
 plugins/amdgpu/amdgpu_plugin_drm.c    |  32 +++----
 plugins/amdgpu/amdgpu_plugin_util.c   | 106 ++++++++++++----------
 plugins/amdgpu/amdgpu_plugin_util.h   |   8 +-
 5 files changed, 140 insertions(+), 146 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +58 -68
index a653ba1aa2d9..dc808fe32373 100644
@@ -557,7 +557,7 @@ void free_and_unmap(uint64_t size, amdgpu_bo_handle h_bo, amdgpu_va_handle h_va,
 	amdgpu_bo_free(h_bo);
 }
 
-int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
+int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
 		 void *buffer, size_t buffer_size, amdgpu_device_handle h_dev,
 		 uint64_t max_copy_size, enum sdma_op_type type, bool do_not_free)
 {
@@ -689,7 +689,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
 		memset(ib, 0, packets_per_buffer * 28);
 
 		if (type == SDMA_OP_VRAM_WRITE) {
-			err = read_fp(storage_fp, buffer, min(bytes_remain, buffer_bo_size));
+			err = img_read(storage_fd, buffer,
+				       min(bytes_remain, buffer_bo_size));
 			if (err) {
 				pr_perror("failed to read from storage");
 				goto err_bo_list;
@@ -764,7 +765,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
 		}
 
 		if (type == SDMA_OP_VRAM_READ) {
-			err = write_fp(storage_fp, buffer, buffer_bo_size - buffer_space_remain);
+			err = img_write(storage_fd, buffer,
+					buffer_bo_size - buffer_space_remain);
 			if (err) {
 				pr_perror("failed to write out to storage");
 				goto err_cs_submit_ib;
@@ -817,11 +819,11 @@ void *dump_bo_contents(void *_thread_data)
 	struct amdgpu_gpu_info gpu_info = { 0 };
 	amdgpu_device_handle h_dev;
 	size_t max_bo_size = 0, image_size = 0, buffer_size;
+	int bo_contents_fd = -1;
 	uint64_t max_copy_size;
 	uint32_t major, minor;
 	int num_bos = 0;
 	int i, ret = 0;
-	FILE *bo_contents_fp = NULL;
 	void *buffer = NULL;
 	char img_path[40];
 
@@ -862,10 +864,9 @@ void *dump_bo_contents(void *_thread_data)
 	}
 
 	snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, thread_data->id, thread_data->gpu_id);
-	bo_contents_fp = open_img_file(img_path, true, &image_size);
-	if (!bo_contents_fp) {
-		pr_perror("Cannot fopen %s", img_path);
-		ret = -EIO;
+	bo_contents_fd = open_img_file(img_path, true, &image_size);
+	if (bo_contents_fd < 0) {
+		ret = bo_contents_fd;
 		goto exit;
 	}
 
@@ -879,8 +880,9 @@ void *dump_bo_contents(void *_thread_data)
 		num_bos++;
 
 		/* perform sDMA based vram copy */
-		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-				   SDMA_OP_VRAM_READ, false);
+		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size,
+				   bo_contents_fd, buffer, buffer_size, h_dev,
+				   max_copy_size, SDMA_OP_VRAM_READ, false);
 
 		if (ret) {
 			pr_err("Failed to drain the BO using sDMA: bo_buckets[%d]\n", i);
@@ -891,8 +893,8 @@ void *dump_bo_contents(void *_thread_data)
 exit:
 	pr_info("Thread[0x%x] done num_bos:%d ret:%d\n", thread_data->gpu_id, num_bos, ret);
 
-	if (bo_contents_fp)
-		fclose(bo_contents_fp);
+	if (bo_contents_fd >= 0)
+		close(bo_contents_fd);
 
 	xfree(buffer);
 
@@ -909,9 +911,9 @@ void *restore_bo_contents(void *_thread_data)
 	size_t image_size = 0, total_bo_size = 0, max_bo_size = 0, buffer_size;
 	struct amdgpu_gpu_info gpu_info = { 0 };
 	amdgpu_device_handle h_dev;
+	int bo_contents_fd = -1;
 	uint64_t max_copy_size;
 	uint32_t major, minor;
-	FILE *bo_contents_fp = NULL;
 	void *buffer = NULL;
 	char img_path[40];
 	int num_bos = 0;
@@ -936,10 +938,9 @@ void *restore_bo_contents(void *_thread_data)
 								   SDMA_LINEAR_COPY_MAX_SIZE - 1;
 
 	snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, thread_data->id, thread_data->gpu_id);
-	bo_contents_fp = open_img_file(img_path, false, &image_size);
-	if (!bo_contents_fp) {
-		pr_perror("Cannot fopen %s", img_path);
-		ret = -errno;
+	bo_contents_fd = open_img_file(img_path, false, &image_size);
+	if (bo_contents_fd < 0) {
+		ret = bo_contents_fd;
 		goto exit;
 	}
 
@@ -978,8 +979,9 @@ void *restore_bo_contents(void *_thread_data)
 
 		num_bos++;
 
-		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-				   SDMA_OP_VRAM_WRITE, false);
+		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size,
+				   bo_contents_fd, buffer, buffer_size, h_dev,
+				   max_copy_size, SDMA_OP_VRAM_WRITE, false);
 		if (ret) {
 			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
 			break;
@@ -991,8 +993,8 @@ void *restore_bo_contents(void *_thread_data)
 exit:
 	pr_info("Thread[0x%x] done num_bos:%d ret:%d\n", thread_data->gpu_id, num_bos, ret);
 
-	if (bo_contents_fp)
-		fclose(bo_contents_fp);
+	if (bo_contents_fd >= 0)
+		close(bo_contents_fd);
 
 	xfree(buffer);
 
@@ -1016,9 +1018,7 @@ int check_hsakmt_shared_mem(uint64_t *shared_mem_size, uint32_t *shared_mem_magi
 
 	/* First 4 bytes of shared file is the magic */
 	ret = read_file(HSAKMT_SHM_PATH, shared_mem_magic, sizeof(*shared_mem_magic));
-	if (ret)
-		pr_perror("Failed to read shared mem magic");
-	else
+	if (!ret)
 		pr_debug("Shared mem magic:0x%x\n", *shared_mem_magic);
 
 	return 0;
@@ -1137,15 +1137,12 @@ int amdgpu_id_for_handle(int handle)
 static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
 {
 	unsigned char *buf;
-	FILE *img_fp;
+	int fd, ret;
 	size_t len;
-	int ret;
 
-	img_fp = open_img_file(filename, false, &len);
-	if (!img_fp) {
-		ret = -ENOENT;
-		goto out;
-	}
+	fd = open_img_file(filename, false, &len);
+	if (fd < 0)
+		return fd;
 
 	buf = xmalloc(len);
 	if (!buf) {
@@ -1153,7 +1150,7 @@ static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
 		goto out_close;
 	}
 
-	ret = read_fp(img_fp, buf, len);
+	ret = img_read(fd, buf, len);
 	if (ret) {
 		xfree(buf);
 	} else {
@@ -1162,11 +1159,7 @@ static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
 	}
 
 out_close:
-	fclose(img_fp);
-out:
-	if (ret < 0)
-		pr_err("Unable to read from %s", filename);
-
+	close(fd);
 	return ret;
 }
 
@@ -1956,7 +1949,7 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 	CriuKfd *e = NULL;
 	struct kfd_ioctl_criu_args args = { 0 };
 	size_t img_size;
-	FILE *img_fp = NULL;
+	int img_fd;
 
 	*retry_needed = false;
 
@@ -1967,8 +1960,8 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 
 	snprintf(img_path, sizeof(img_path), IMG_KFD_FILE, id);
 
-	img_fp = open_img_file(img_path, false, &img_size);
-	if (!img_fp)
+	img_fd = open_img_file(img_path, false, &img_size);
+	if (img_fd < 0)
 		return amdgpu_plugin_restore_drm_file(id, retry_needed);
 
 	fd = open(AMDGPU_KFD_DEVICE, O_RDWR | O_CLOEXEC);
@@ -1985,19 +1978,18 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 	pr_info("KFD Image file size:%ld\n", img_size);
 	buf = xmalloc(img_size);
 	if (!buf) {
-		fclose(img_fp);
+		close(img_fd);
 		return -ENOMEM;
 	}
 
-	ret = read_fp(img_fp, buf, img_size);
+	ret = img_read(img_fd, buf, img_size);
+	close(img_fd);
 	if (ret) {
 		pr_perror("Unable to read from %s", img_path);
-		fclose(img_fp);
 		xfree(buf);
 		return ret;
 	}
 
-	fclose(img_fp);
 	e = criu_kfd__unpack(NULL, img_size, buf);
 	if (e == NULL) {
 		pr_err("Unable to parse the KFD message %#x\n", id);
@@ -2254,25 +2246,23 @@ err:
 	return ret;
 }
 
-FILE *get_bo_contents_fp(int id, int gpu_id, size_t tot_size)
+static int get_bo_contents_fd(int id, int gpu_id, size_t tot_size)
 {
 	char img_path[PATH_MAX];
 	size_t image_size = 0;
-	FILE *bo_contents_fp = NULL;
+	int fd;
 
 	snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, id, gpu_id);
-	bo_contents_fp = open_img_file(img_path, false, &image_size);
-	if (!bo_contents_fp) {
-		pr_perror("Cannot fopen %s", img_path);
-		return NULL;
-	}
+	fd = open_img_file(img_path, false, &image_size);
+	if (fd < 0)
+		return fd;
 
 	if (tot_size != image_size) {
 		pr_err("%s size mismatch (current:%ld:expected:%ld)\n", img_path, image_size, tot_size);
-		fclose(bo_contents_fp);
-		return NULL;
+		close(fd);
+		return -EINVAL;
 	}
-	return bo_contents_fp;
+	return fd;
 }
 
 struct parallel_thread_data {
@@ -2289,7 +2279,7 @@ void *parallel_restore_bo_contents(void *_thread_data)
 	amdgpu_device_handle h_dev;
 	uint64_t max_copy_size;
 	size_t total_bo_size = 0, max_bo_size = 0, buffer_size = 0;
-	FILE *bo_contents_fp = NULL;
+	int bo_contents_fd = -1;
 	parallel_restore_entry *entry;
 	parallel_restore_cmd *restore_cmd = thread_data->restore_cmd;
 	int ret = 0;
@@ -2310,12 +2300,12 @@ void *parallel_restore_bo_contents(void *_thread_data)
 
 	buffer_size = kfd_max_buffer_size > 0 ? min(kfd_max_buffer_size, max_bo_size) : max_bo_size;
 
-	bo_contents_fp = get_bo_contents_fp(restore_cmd->cmd_head.id, thread_data->gpu_id, total_bo_size);
-	if (bo_contents_fp == NULL) {
-		ret = -1;
+	bo_contents_fd = get_bo_contents_fd(restore_cmd->cmd_head.id, thread_data->gpu_id, total_bo_size);
+	if (bo_contents_fd < 0) {
+		ret = bo_contents_fd;
 		goto err_sdma;
 	}
-	offset = ftell(bo_contents_fp);
+	offset = lseek(bo_contents_fd, 0, SEEK_CUR);
 	if (offset < 0) {
 		ret = -errno;
 		pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE.");
@@ -2334,17 +2324,17 @@ void *parallel_restore_bo_contents(void *_thread_data)
 			continue;
 
 		entry = &restore_cmd->entries[i];
-		ret = fseeko(bo_contents_fp, entry->read_offset + offset,
-			     SEEK_SET);
-		if (ret < 0) {
+		ret = lseek(bo_contents_fd, entry->read_offset + offset,
+			    SEEK_SET);
+		if (ret) {
 			ret = -errno;
 			pr_err("Failed to seek for BO using sDMA: bo_buckets[%d]\n", i);
 			goto err_sdma;
 		}
-		ret = sdma_copy_bo(restore_cmd->fds_write[entry->write_id], entry->size, bo_contents_fp,
-				   buffer, buffer_size, h_dev,
-				   max_copy_size, SDMA_OP_VRAM_WRITE, false);
-
+		ret = sdma_copy_bo(restore_cmd->fds_write[entry->write_id],
+				   entry->size, bo_contents_fd, buffer,
+				   buffer_size, h_dev, max_copy_size,
+				   SDMA_OP_VRAM_WRITE, false);
 		if (ret) {
 			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
 			goto err_sdma;
@@ -2352,8 +2342,8 @@ void *parallel_restore_bo_contents(void *_thread_data)
 	}
 
 err_sdma:
-	if (bo_contents_fp)
-		fclose(bo_contents_fp);
+	if (bo_contents_fd >= 0)
+		close(bo_contents_fd);
 	if (buffer)
 		xfree(buffer);
 	amdgpu_device_deinitialize(h_dev);
plugins/amdgpu/amdgpu_plugin_dmabuf.c +6 -8
index 859ff42bb76a..fb894de16d75 100644
@@ -96,20 +96,19 @@ int __amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
 int amdgpu_plugin_dmabuf_restore(int id)
 {
 	char path[PATH_MAX];
+	int fd_id, img_fd;
 	size_t img_size;
-	FILE *img_fp = NULL;
 	int ret = 0;
 	CriuDmabufNode *rd = NULL;
 	unsigned char *buf = NULL;
-	int fd_id;
 
 	snprintf(path, sizeof(path), IMG_DMABUF_FILE, id);
 
 	/* Read serialized metadata */
-	img_fp = open_img_file(path, false, &img_size);
-	if (!img_fp) {
+	img_fd = open_img_file(path, false, &img_size);
+	if (img_fd < 0) {
 		pr_err("Failed to open dmabuf metadata file: %s\n", path);
-		return -EINVAL;
+		return img_fd;
 	}
 
 	pr_debug("dmabuf Image file size:%ld\n", img_size);
@@ -119,10 +118,9 @@ int amdgpu_plugin_dmabuf_restore(int id)
 		return -ENOMEM;
 	}
 
-	ret = read_fp(img_fp, buf, img_size);
-	fclose(img_fp);
+	ret = img_read(img_fd, buf, img_size);
+	close(img_fd);
 	if (ret) {
-		pr_perror("Unable to read from %s", path);
 		xfree(buf);
 		return ret;
 	}
plugins/amdgpu/amdgpu_plugin_drm.c +15 -17
index d0f84b46997c..070b5b19ec47 100644
@@ -163,8 +163,8 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
 	amdgpu_device_handle h_dev;
 	uint64_t max_copy_size;
 	uint32_t major, minor;
-	FILE *bo_contents_fp = NULL;
 	void *buffer = NULL;
+	int bo_contents_fd;
 	char img_path[40];
 	int i, ret = 0;
 
@@ -209,24 +209,23 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
 
 		snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, drm_render_minor, i);
 
-		bo_contents_fp = open_img_file(img_path, false, &image_size);
-		if (!bo_contents_fp) {
-			ret = -EIO;
+		bo_contents_fd = open_img_file(img_path, false, &image_size);
+		if (bo_contents_fd < 0) {
+			ret = bo_contents_fd;
 			pr_err("Failed to open BO image file %s\n", img_path);
 			break;
 		}
 
-		ret = sdma_copy_bo(dmabufs[i], rd->bo_entries[i]->size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-				   SDMA_OP_VRAM_WRITE, true);
+		ret = sdma_copy_bo(dmabufs[i], rd->bo_entries[i]->size,
+				   bo_contents_fd, buffer, buffer_size, h_dev,
+				   max_copy_size, SDMA_OP_VRAM_WRITE, true);
+		close(bo_contents_fd);
 		if (ret) {
 			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
 			break;
 		}
 		pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
 			 i);
-
-		if (bo_contents_fp)
-			fclose(bo_contents_fp);
 	}
 
 exit:
@@ -307,12 +306,12 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 		DrmBoEntry *boinfo = rd->bo_entries[i];
 		struct drm_amdgpu_gem_list_handles_entry handle_entry = list_handles_entries[i];
 		union drm_amdgpu_gem_mmap mmap_args = { 0 };
+		int bo_contents_fd;
 		int dmabuf_fd;
 		uint32_t major, minor;
 		amdgpu_device_handle h_dev;
 		void *buffer = NULL;
 		char img_path[40];
-		FILE *bo_contents_fp = NULL;
 		int device_fd;
 
 		boinfo->size = handle_entry.size;
@@ -388,25 +387,24 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
 		snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, rd->drm_render_minor, i);
 		image_size = handle_entry.size;
-		bo_contents_fp = open_img_file(img_path, true, &image_size);
-		if (!bo_contents_fp) {
-			ret = -EIO;
+		bo_contents_fd = open_img_file(img_path, true, &image_size);
+		if (bo_contents_fd < 0) {
+			ret = bo_contents_fd;
 			goto exit;
 		}
 
 		posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), handle_entry.size);
 
-		ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fp, buffer, handle_entry.size, h_dev, 0x1000,
+		ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fd,
+				   buffer, handle_entry.size, h_dev, 0x1000,
 				   SDMA_OP_VRAM_READ, false);
+		close(bo_contents_fd);
 		if (ret)
 			goto exit;
 
 		if (dmabuf_fd != KFD_INVALID_FD)
 			close(dmabuf_fd);
 
-		if (bo_contents_fp)
-			fclose(bo_contents_fp);
-
 		ret = amdgpu_device_deinitialize(h_dev);
 		if (ret)
 			goto exit;
plugins/amdgpu/amdgpu_plugin_util.c +57 -49

162 lines not on this page · raw message

plugins/amdgpu/amdgpu_plugin_util.h +4 -4
index 014abf35ccb6..eb0c28674d18 100644
@@ -106,11 +106,11 @@ extern bool kfd_vram_size_check;
 extern bool kfd_numa_check;
 extern bool kfd_capability_check;
 
-int read_fp(FILE *fp, void *buf, const size_t buf_len);
-int write_fp(FILE *fp, const void *buf, const size_t buf_len);
+int img_read(int fd, void *buf, const size_t buf_len);
+int img_write(int fd, const void *buf, const size_t buf_len);
 int read_file(const char *file_path, void *buf, const size_t buf_len);
 int write_img_file(char *path, const void *buf, const size_t buf_len);
-FILE *open_img_file(char *path, bool write, size_t *size);
+int open_img_file(char *path, bool write, size_t *size);
 
 int record_dumped_fd(int fd, bool is_drm);
 struct list_head *get_dumped_fds();
@@ -127,7 +127,7 @@ void clear_restore_state();
 
 void print_kfd_bo_stat(int bo_cnt, struct kfd_criu_bo_bucket *bo_list);
 
-int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
+int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
 		 void *buffer, size_t buffer_size, amdgpu_device_handle h_dev,
 		 uint64_t max_copy_size, enum sdma_op_type type, bool do_not_free);
 
Signature · 1 line
2.52.0
#28 lore raw

Re: [RFC 18/26] plugins/amdgpu: Convert away from libc buffered file IO

Gentle ping on this one, and patch 17 as well. If those two are reviewed I can implement the small tweaks as agreed and re-spin the series up to patch 20. That would be purely cleanup and then I keep churning at the real stuff.

Tvrtko

Quoted 682 lines · Tvrtko Ursulin

Currently the code contains multiple places where callers expect the errno to be valid (implied by calling pr_perror to log file IO errors) after calling open_img_file(), read_fp() and write_fp() helpers. Problem there is that those helpers can destroy the errno courtesy of themselves emitting log messages via fprintf.

Furthermore, the callers then sometimes invent the errors to return back to the caller, or even outside of the plugin.

On top of that there is no benefit to buffered file IO given the plugin reads and writes in buffer object size chunks, so it it preferrable to just go direct and avoid any possibility of extra copying to and from libc temporary buffers.

So lets just convert it all in one swoop to POSIX IO and make sure correct errnos are always propagated to the caller (including to CRIU core).

Hopefully this removes all instances of incorrect pr_perror log messages.

---
  plugins/amdgpu/amdgpu_plugin.c        | 126 ++++++++++++--------------
  plugins/amdgpu/amdgpu_plugin_dmabuf.c |  14 ++-
  plugins/amdgpu/amdgpu_plugin_drm.c    |  32 +++----
  plugins/amdgpu/amdgpu_plugin_util.c   | 106 ++++++++++++----------
  plugins/amdgpu/amdgpu_plugin_util.h   |   8 +-
  5 files changed, 140 insertions(+), 146 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +58 -68
index a653ba1aa2d9..dc808fe32373 100644
@@ -557,7 +557,7 @@ void free_and_unmap(uint64_t size, amdgpu_bo_handle h_bo, amdgpu_va_handle h_va,
  	amdgpu_bo_free(h_bo);
  }
 
-int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
+int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
  		 void *buffer, size_t buffer_size, amdgpu_device_handle h_dev,
  		 uint64_t max_copy_size, enum sdma_op_type type, bool do_not_free)
  {
@@ -689,7 +689,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
  		memset(ib, 0, packets_per_buffer * 28);
 
  		if (type == SDMA_OP_VRAM_WRITE) {
-			err = read_fp(storage_fp, buffer, min(bytes_remain, buffer_bo_size));
+			err = img_read(storage_fd, buffer,
+				       min(bytes_remain, buffer_bo_size));
  			if (err) {
  				pr_perror("failed to read from storage");
  				goto err_bo_list;
@@ -764,7 +765,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
  		}
 
  		if (type == SDMA_OP_VRAM_READ) {
-			err = write_fp(storage_fp, buffer, buffer_bo_size - buffer_space_remain);
+			err = img_write(storage_fd, buffer,
+					buffer_bo_size - buffer_space_remain);
  			if (err) {
  				pr_perror("failed to write out to storage");
  				goto err_cs_submit_ib;
@@ -817,11 +819,11 @@ void *dump_bo_contents(void *_thread_data)
  	struct amdgpu_gpu_info gpu_info = { 0 };
  	amdgpu_device_handle h_dev;
  	size_t max_bo_size = 0, image_size = 0, buffer_size;
+	int bo_contents_fd = -1;
  	uint64_t max_copy_size;
  	uint32_t major, minor;
  	int num_bos = 0;
  	int i, ret = 0;
-	FILE *bo_contents_fp = NULL;
  	void *buffer = NULL;
  	char img_path[40];
 
@@ -862,10 +864,9 @@ void *dump_bo_contents(void *_thread_data)
  	}
 
  	snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, thread_data->id, thread_data->gpu_id);
-	bo_contents_fp = open_img_file(img_path, true, &image_size);
-	if (!bo_contents_fp) {
-		pr_perror("Cannot fopen %s", img_path);
-		ret = -EIO;
+	bo_contents_fd = open_img_file(img_path, true, &image_size);
+	if (bo_contents_fd < 0) {
+		ret = bo_contents_fd;
  		goto exit;
  	}
 
@@ -879,8 +880,9 @@ void *dump_bo_contents(void *_thread_data)
  		num_bos++;
 
  		/* perform sDMA based vram copy */
-		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-				   SDMA_OP_VRAM_READ, false);
+		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size,
+				   bo_contents_fd, buffer, buffer_size, h_dev,
+				   max_copy_size, SDMA_OP_VRAM_READ, false);
 
  		if (ret) {
  			pr_err("Failed to drain the BO using sDMA: bo_buckets[%d]\n", i);
@@ -891,8 +893,8 @@ void *dump_bo_contents(void *_thread_data)
  exit:
  	pr_info("Thread[0x%x] done num_bos:%d ret:%d\n", thread_data->gpu_id, num_bos, ret);
 
-	if (bo_contents_fp)
-		fclose(bo_contents_fp);
+	if (bo_contents_fd >= 0)
+		close(bo_contents_fd);
 
  	xfree(buffer);
 
@@ -909,9 +911,9 @@ void *restore_bo_contents(void *_thread_data)
  	size_t image_size = 0, total_bo_size = 0, max_bo_size = 0, buffer_size;
  	struct amdgpu_gpu_info gpu_info = { 0 };
  	amdgpu_device_handle h_dev;
+	int bo_contents_fd = -1;
  	uint64_t max_copy_size;
  	uint32_t major, minor;
-	FILE *bo_contents_fp = NULL;
  	void *buffer = NULL;
  	char img_path[40];
  	int num_bos = 0;
@@ -936,10 +938,9 @@ void *restore_bo_contents(void *_thread_data)
  								   SDMA_LINEAR_COPY_MAX_SIZE - 1;
 
  	snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, thread_data->id, thread_data->gpu_id);
-	bo_contents_fp = open_img_file(img_path, false, &image_size);
-	if (!bo_contents_fp) {
-		pr_perror("Cannot fopen %s", img_path);
-		ret = -errno;
+	bo_contents_fd = open_img_file(img_path, false, &image_size);
+	if (bo_contents_fd < 0) {
+		ret = bo_contents_fd;
  		goto exit;
  	}
 
@@ -978,8 +979,9 @@ void *restore_bo_contents(void *_thread_data)
 
  		num_bos++;
 
-		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-				   SDMA_OP_VRAM_WRITE, false);
+		ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size,
+				   bo_contents_fd, buffer, buffer_size, h_dev,
+				   max_copy_size, SDMA_OP_VRAM_WRITE, false);
  		if (ret) {
  			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
  			break;
@@ -991,8 +993,8 @@ void *restore_bo_contents(void *_thread_data)
  exit:
  	pr_info("Thread[0x%x] done num_bos:%d ret:%d\n", thread_data->gpu_id, num_bos, ret);
 
-	if (bo_contents_fp)
-		fclose(bo_contents_fp);
+	if (bo_contents_fd >= 0)
+		close(bo_contents_fd);
 
  	xfree(buffer);
 
@@ -1016,9 +1018,7 @@ int check_hsakmt_shared_mem(uint64_t *shared_mem_size, uint32_t *shared_mem_magi
 
  	/* First 4 bytes of shared file is the magic */
  	ret = read_file(HSAKMT_SHM_PATH, shared_mem_magic, sizeof(*shared_mem_magic));
-	if (ret)
-		pr_perror("Failed to read shared mem magic");
-	else
+	if (!ret)
  		pr_debug("Shared mem magic:0x%x\n", *shared_mem_magic);
 
  	return 0;
@@ -1137,15 +1137,12 @@ int amdgpu_id_for_handle(int handle)
  static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
  {
  	unsigned char *buf;
-	FILE *img_fp;
+	int fd, ret;
  	size_t len;
-	int ret;
 
-	img_fp = open_img_file(filename, false, &len);
-	if (!img_fp) {
-		ret = -ENOENT;
-		goto out;
-	}
+	fd = open_img_file(filename, false, &len);
+	if (fd < 0)
+		return fd;
 
  	buf = xmalloc(len);
  	if (!buf) {
@@ -1153,7 +1150,7 @@ static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
  		goto out_close;
  	}
 
-	ret = read_fp(img_fp, buf, len);
+	ret = img_read(fd, buf, len);
  	if (ret) {
  		xfree(buf);
  	} else {
@@ -1162,11 +1159,7 @@ static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
  	}
 
  out_close:
-	fclose(img_fp);
-out:
-	if (ret < 0)
-		pr_err("Unable to read from %s", filename);
-
+	close(fd);
  	return ret;
  }
 
@@ -1956,7 +1949,7 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
  	CriuKfd *e = NULL;
  	struct kfd_ioctl_criu_args args = { 0 };
  	size_t img_size;
-	FILE *img_fp = NULL;
+	int img_fd;
 
  	*retry_needed = false;
 
@@ -1967,8 +1960,8 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 
  	snprintf(img_path, sizeof(img_path), IMG_KFD_FILE, id);
 
-	img_fp = open_img_file(img_path, false, &img_size);
-	if (!img_fp)
+	img_fd = open_img_file(img_path, false, &img_size);
+	if (img_fd < 0)
  		return amdgpu_plugin_restore_drm_file(id, retry_needed);
 
  	fd = open(AMDGPU_KFD_DEVICE, O_RDWR | O_CLOEXEC);
@@ -1985,19 +1978,18 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
  	pr_info("KFD Image file size:%ld\n", img_size);
  	buf = xmalloc(img_size);
  	if (!buf) {
-		fclose(img_fp);
+		close(img_fd);
  		return -ENOMEM;
  	}
 
-	ret = read_fp(img_fp, buf, img_size);
+	ret = img_read(img_fd, buf, img_size);
+	close(img_fd);
  	if (ret) {
  		pr_perror("Unable to read from %s", img_path);
-		fclose(img_fp);
  		xfree(buf);
  		return ret;
  	}
 
-	fclose(img_fp);
  	e = criu_kfd__unpack(NULL, img_size, buf);
  	if (e == NULL) {
  		pr_err("Unable to parse the KFD message %#x\n", id);
@@ -2254,25 +2246,23 @@ err:
  	return ret;
  }
 
-FILE *get_bo_contents_fp(int id, int gpu_id, size_t tot_size)
+static int get_bo_contents_fd(int id, int gpu_id, size_t tot_size)
  {
  	char img_path[PATH_MAX];
  	size_t image_size = 0;
-	FILE *bo_contents_fp = NULL;
+	int fd;
 
  	snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, id, gpu_id);
-	bo_contents_fp = open_img_file(img_path, false, &image_size);
-	if (!bo_contents_fp) {
-		pr_perror("Cannot fopen %s", img_path);
-		return NULL;
-	}
+	fd = open_img_file(img_path, false, &image_size);
+	if (fd < 0)
+		return fd;
 
  	if (tot_size != image_size) {
  		pr_err("%s size mismatch (current:%ld:expected:%ld)\n", img_path, image_size, tot_size);
-		fclose(bo_contents_fp);
-		return NULL;
+		close(fd);
+		return -EINVAL;
  	}
-	return bo_contents_fp;
+	return fd;
  }
 
  struct parallel_thread_data {
@@ -2289,7 +2279,7 @@ void *parallel_restore_bo_contents(void *_thread_data)
  	amdgpu_device_handle h_dev;
  	uint64_t max_copy_size;
  	size_t total_bo_size = 0, max_bo_size = 0, buffer_size = 0;
-	FILE *bo_contents_fp = NULL;
+	int bo_contents_fd = -1;
  	parallel_restore_entry *entry;
  	parallel_restore_cmd *restore_cmd = thread_data->restore_cmd;
  	int ret = 0;
@@ -2310,12 +2300,12 @@ void *parallel_restore_bo_contents(void *_thread_data)
 
  	buffer_size = kfd_max_buffer_size > 0 ? min(kfd_max_buffer_size, max_bo_size) : max_bo_size;
 
-	bo_contents_fp = get_bo_contents_fp(restore_cmd->cmd_head.id, thread_data->gpu_id, total_bo_size);
-	if (bo_contents_fp == NULL) {
-		ret = -1;
+	bo_contents_fd = get_bo_contents_fd(restore_cmd->cmd_head.id, thread_data->gpu_id, total_bo_size);
+	if (bo_contents_fd < 0) {
+		ret = bo_contents_fd;
  		goto err_sdma;
  	}
-	offset = ftell(bo_contents_fp);
+	offset = lseek(bo_contents_fd, 0, SEEK_CUR);
  	if (offset < 0) {
  		ret = -errno;
  		pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE.");
@@ -2334,17 +2324,17 @@ void *parallel_restore_bo_contents(void *_thread_data)
  			continue;
 
  		entry = &restore_cmd->entries[i];
-		ret = fseeko(bo_contents_fp, entry->read_offset + offset,
-			     SEEK_SET);
-		if (ret < 0) {
+		ret = lseek(bo_contents_fd, entry->read_offset + offset,
+			    SEEK_SET);
+		if (ret) {
  			ret = -errno;
  			pr_err("Failed to seek for BO using sDMA: bo_buckets[%d]\n", i);
  			goto err_sdma;
  		}
-		ret = sdma_copy_bo(restore_cmd->fds_write[entry->write_id], entry->size, bo_contents_fp,
-				   buffer, buffer_size, h_dev,
-				   max_copy_size, SDMA_OP_VRAM_WRITE, false);
-
+		ret = sdma_copy_bo(restore_cmd->fds_write[entry->write_id],
+				   entry->size, bo_contents_fd, buffer,
+				   buffer_size, h_dev, max_copy_size,
+				   SDMA_OP_VRAM_WRITE, false);
  		if (ret) {
  			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
  			goto err_sdma;
@@ -2352,8 +2342,8 @@ void *parallel_restore_bo_contents(void *_thread_data)
  	}
 
  err_sdma:
-	if (bo_contents_fp)
-		fclose(bo_contents_fp);
+	if (bo_contents_fd >= 0)
+		close(bo_contents_fd);
  	if (buffer)
  		xfree(buffer);
  	amdgpu_device_deinitialize(h_dev);
plugins/amdgpu/amdgpu_plugin_dmabuf.c +6 -8
index 859ff42bb76a..fb894de16d75 100644
@@ -96,20 +96,19 @@ int __amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
  int amdgpu_plugin_dmabuf_restore(int id)
  {
  	char path[PATH_MAX];
+	int fd_id, img_fd;
  	size_t img_size;
-	FILE *img_fp = NULL;
  	int ret = 0;
  	CriuDmabufNode *rd = NULL;
  	unsigned char *buf = NULL;
-	int fd_id;
 
  	snprintf(path, sizeof(path), IMG_DMABUF_FILE, id);
 
  	/* Read serialized metadata */
-	img_fp = open_img_file(path, false, &img_size);
-	if (!img_fp) {
+	img_fd = open_img_file(path, false, &img_size);
+	if (img_fd < 0) {
  		pr_err("Failed to open dmabuf metadata file: %s\n", path);
-		return -EINVAL;
+		return img_fd;
  	}
 
  	pr_debug("dmabuf Image file size:%ld\n", img_size);
@@ -119,10 +118,9 @@ int amdgpu_plugin_dmabuf_restore(int id)
  		return -ENOMEM;
  	}
 
-	ret = read_fp(img_fp, buf, img_size);
-	fclose(img_fp);
+	ret = img_read(img_fd, buf, img_size);
+	close(img_fd);
  	if (ret) {
-		pr_perror("Unable to read from %s", path);
  		xfree(buf);
  		return ret;
  	}
plugins/amdgpu/amdgpu_plugin_drm.c +15 -17
index d0f84b46997c..070b5b19ec47 100644
@@ -163,8 +163,8 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
  	amdgpu_device_handle h_dev;
  	uint64_t max_copy_size;
  	uint32_t major, minor;
-	FILE *bo_contents_fp = NULL;
  	void *buffer = NULL;
+	int bo_contents_fd;
  	char img_path[40];
  	int i, ret = 0;
 
@@ -209,24 +209,23 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
 
  		snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, drm_render_minor, i);
 
-		bo_contents_fp = open_img_file(img_path, false, &image_size);
-		if (!bo_contents_fp) {
-			ret = -EIO;
+		bo_contents_fd = open_img_file(img_path, false, &image_size);
+		if (bo_contents_fd < 0) {
+			ret = bo_contents_fd;
  			pr_err("Failed to open BO image file %s\n", img_path);
  			break;
  		}
 
-		ret = sdma_copy_bo(dmabufs[i], rd->bo_entries[i]->size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-				   SDMA_OP_VRAM_WRITE, true);
+		ret = sdma_copy_bo(dmabufs[i], rd->bo_entries[i]->size,
+				   bo_contents_fd, buffer, buffer_size, h_dev,
+				   max_copy_size, SDMA_OP_VRAM_WRITE, true);
+		close(bo_contents_fd);
  		if (ret) {
  			pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
  			break;
  		}
  		pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
  			 i);
-
-		if (bo_contents_fp)
-			fclose(bo_contents_fp);
  	}
 
  exit:
@@ -307,12 +306,12 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
  		DrmBoEntry *boinfo = rd->bo_entries[i];
  		struct drm_amdgpu_gem_list_handles_entry handle_entry = list_handles_entries[i];
  		union drm_amdgpu_gem_mmap mmap_args = { 0 };
+		int bo_contents_fd;
  		int dmabuf_fd;
  		uint32_t major, minor;
  		amdgpu_device_handle h_dev;
  		void *buffer = NULL;
  		char img_path[40];
-		FILE *bo_contents_fp = NULL;
  		int device_fd;
 
  		boinfo->size = handle_entry.size;
@@ -388,25 +387,24 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
  		snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, rd->drm_render_minor, i);
  		image_size = handle_entry.size;
-		bo_contents_fp = open_img_file(img_path, true, &image_size);
-		if (!bo_contents_fp) {
-			ret = -EIO;
+		bo_contents_fd = open_img_file(img_path, true, &image_size);
+		if (bo_contents_fd < 0) {
+			ret = bo_contents_fd;
  			goto exit;
  		}
 
  		posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), handle_entry.size);
 
-		ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fp, buffer, handle_entry.size, h_dev, 0x1000,
+		ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fd,
+				   buffer, handle_entry.size, h_dev, 0x1000,
  				   SDMA_OP_VRAM_READ, false);
+		close(bo_contents_fd);
  		if (ret)
  			goto exit;
 
  		if (dmabuf_fd != KFD_INVALID_FD)
  			close(dmabuf_fd);
 
-		if (bo_contents_fp)
-			fclose(bo_contents_fp);
-
  		ret = amdgpu_device_deinitialize(h_dev);
  		if (ret)
  			goto exit;
plugins/amdgpu/amdgpu_plugin_util.c +57 -49
index 5925624740e7..411348b2ce0e 100644
@@ -192,26 +192,38 @@ void clear_dumped_fds()
  	}
  }
 
-int read_fp(FILE *fp, void *buf, const size_t buf_len)
+int img_read(int fd, void *buf, const size_t buf_len)
  {
-	size_t len_read;
+	ssize_t len;
 
-	len_read = fread(buf, 1, buf_len, fp);
-	if (len_read != buf_len) {
-		pr_err("Unable to read file (read:%ld buf_len:%ld)\n", len_read, buf_len);
-		return -EIO;
+	len = read(fd, buf, buf_len);
+	if (len < 0 || len != buf_len) {
+		int ret;
+
+		if (len >= 0)
+			errno = EIO;
+		ret = -errno;
+		pr_perror("Unable to read file (read:%ld buf_len:%ld)\n",
+			  len, buf_len);
+		return ret;
  	}
  	return 0;
  }
 
-int write_fp(FILE *fp, const void *buf, const size_t buf_len)
+int img_write(int fd, const void *buf, const size_t buf_len)
  {
-	size_t len_write;
+	ssize_t len;
 
-	len_write = fwrite(buf, 1, buf_len, fp);
-	if (len_write != buf_len) {
-		pr_err("Unable to write file (wrote:%ld buf_len:%ld)\n", len_write, buf_len);
-		return -EIO;
+	len = write(fd, buf, buf_len);
+	if (len < 0 || len != buf_len) {
+		int ret;
+
+		if (len >= 0)
+			errno = EIO;
+		ret = -errno;
+		pr_perror("Unable to write file (wrote:%ld buf_len:%ld)\n",
+			  len, buf_len);
+		return ret;
  	}
  	return 0;
  }
@@ -222,63 +234,60 @@ int write_fp(FILE *fp, const void *buf, const size_t buf_len)
   * We store the size of the actual contents in the first 8-bytes of
   * the file. This allows us to determine the file size when using
   * criu_image_streamer when fseek and fstat are not available. The
- * FILE * returned is already at the location of the first actual
+ * file descriptor returned is already at the location of the first actual
   * contents.
   *
   * @param path The file path
   * @param write False for read, true for write
   * @param size Size of actual contents
- * @return FILE *if successful, NULL if failed
+ * @return file descriptor if successful, negative integer if failed
   */
-FILE *open_img_file(char *path, bool write, size_t *size)
+int open_img_file(char *path, bool write, size_t *size)
  {
-	FILE *fp = NULL;
  	int fd, ret;
 
-	if (opts.stream)
+	if (opts.stream) {
  		fd = img_streamer_open(path, write ? O_DUMP : O_RSTR);
-	else
-		fd = openat(criu_get_image_dir(), path, write ? (O_WRONLY | O_CREAT) : O_RDONLY, 0600);
-
-	if (fd < 0) {
-		pr_err("%s: Failed to open for %s\n", path, write ? "write" : "read");
-		return NULL;
+		if (fd < 0)
+			errno = -fd;
+	} else {
+		fd = openat(criu_get_image_dir(), path,
+			    write ? (O_WRONLY | O_CREAT) : O_RDONLY, 0600);
  	}
 
-	fp = fdopen(fd, write ? "w" : "r");
-	if (!fp) {
-		pr_err("%s: Failed get pointer for %s\n", path, write ? "write" : "read");
-		return NULL;
+	if (fd < 0) {
+		pr_perror("%s: Failed to open for %s\n",
+			  path, write ? "write" : "read");
+		return fd;
  	}
 
  	if (write)
-		ret = write_fp(fp, size, sizeof(*size));
+		ret = img_write(fd, size, sizeof(*size));
  	else
-		ret = read_fp(fp, size, sizeof(*size));
-
+		ret = img_read(fd, size, sizeof(*size));
  	if (ret) {
-		pr_err("%s:Failed to access file size\n", path);
-		fclose(fp);
-		return NULL;
+		close(fd);
+		return ret;
  	}
 
  	pr_debug("%s:Opened file for %s with size:%ld\n", path, write ? "write" : "read", *size);
-	return fp;
+	return fd;
  }
 
  int read_file(const char *file_path, void *buf, const size_t buf_len)
  {
-	int ret;
-	FILE *fp;
+	int ret, fd;
 
-	fp = fopen(file_path, "r");
-	if (!fp) {
-		pr_err("Cannot fopen %s\n", file_path);
-		return -errno;
+	fd = open(file_path, O_RDONLY);
+	if (fd < 0) {
+		int ret = errno;
+
+		pr_perror("Cannot open %s\n", file_path);
+		return -ret;
  	}
 
-	ret = read_fp(fp, buf, buf_len);
-	fclose(fp); /* this will also close fd */
+	ret = img_read(fd, buf, buf_len);
+	close(fd);
  	return ret;
  }
 
@@ -296,16 +305,15 @@ int read_file(const char *file_path, void *buf, const size_t buf_len)
   */
  int write_img_file(char *path, const void *buf, const size_t buf_len)
  {
-	int ret;
-	FILE *fp;
+	int ret, fd;
  	size_t len = buf_len;
 
-	fp = open_img_file(path, true, &len);
-	if (!fp)
-		return -errno;
+	fd = open_img_file(path, true, &len);
+	if (fd < 0)
+		return fd;
 
-	ret = write_fp(fp, buf, buf_len);
-	fclose(fp); /* this will also close fd */
+	ret = img_write(fd, buf, buf_len);
+	close(fd);
  	return ret;
  }
 
plugins/amdgpu/amdgpu_plugin_util.h +4 -4
index 014abf35ccb6..eb0c28674d18 100644
@@ -106,11 +106,11 @@ extern bool kfd_vram_size_check;
  extern bool kfd_numa_check;
  extern bool kfd_capability_check;
 
-int read_fp(FILE *fp, void *buf, const size_t buf_len);
-int write_fp(FILE *fp, const void *buf, const size_t buf_len);
+int img_read(int fd, void *buf, const size_t buf_len);
+int img_write(int fd, const void *buf, const size_t buf_len);
  int read_file(const char *file_path, void *buf, const size_t buf_len);
  int write_img_file(char *path, const void *buf, const size_t buf_len);
-FILE *open_img_file(char *path, bool write, size_t *size);
+int open_img_file(char *path, bool write, size_t *size);
 
  int record_dumped_fd(int fd, bool is_drm);
  struct list_head *get_dumped_fds();
@@ -127,7 +127,7 @@ void clear_restore_state();
 
  void print_kfd_bo_stat(int bo_cnt, struct kfd_criu_bo_bucket *bo_list);
 
-int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
+int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
  		 void *buffer, size_t buffer_size, amdgpu_device_handle h_dev,
  		 uint64_t max_copy_size, enum sdma_op_type type, bool do_not_free);
 
Reviewed-by
#29 lore raw

Re: [RFC 18/26] plugins/amdgpu: Convert away from libc buffered file IO

Right, forgot to reply after clarification

This patch and patch 17 are

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Monday, March 9, 2026 11:30 AM
To: [email protected]
Cc: Francis, David
Subject: Re: [RFC 18/26] plugins/amdgpu: Convert away from libc buffered file IO

Gentle ping on this one, and patch 17 as well. If those two are reviewed I can implement the small tweaks as agreed and re-spin the series up to patch 20. That would be purely cleanup and then I keep churning at the real stuff.

Tvrtko

Quoted 682 lines · Tvrtko Ursulin

Currently the code contains multiple places where callers expect the errno to be valid (implied by calling pr_perror to log file IO errors) after calling open_img_file(), read_fp() and write_fp() helpers. Problem there is that those helpers can destroy the errno courtesy of themselves emitting log messages via fprintf.

Furthermore, the callers then sometimes invent the errors to return back to the caller, or even outside of the plugin.

On top of that there is no benefit to buffered file IO given the plugin reads and writes in buffer object size chunks, so it it preferrable to just go direct and avoid any possibility of extra copying to and from libc temporary buffers.

So lets just convert it all in one swoop to POSIX IO and make sure correct errnos are always propagated to the caller (including to CRIU core).

Hopefully this removes all instances of incorrect pr_perror log messages.

---
  plugins/amdgpu/amdgpu_plugin.c        | 126 ++++++++++++--------------
  plugins/amdgpu/amdgpu_plugin_dmabuf.c |  14 ++-
  plugins/amdgpu/amdgpu_plugin_drm.c    |  32 +++----
  plugins/amdgpu/amdgpu_plugin_util.c   | 106 ++++++++++++----------
  plugins/amdgpu/amdgpu_plugin_util.h   |   8 +-
  5 files changed, 140 insertions(+), 146 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +58 -68
index a653ba1aa2d9..dc808fe32373 100644
@@ -557,7 +557,7 @@ void free_and_unmap(uint64_t size, amdgpu_bo_handle h_bo, amdgpu_va_handle h_va,
      amdgpu_bo_free(h_bo);
  }
 
-int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
+int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
               void *buffer, size_t buffer_size, amdgpu_device_handle h_dev,
               uint64_t max_copy_size, enum sdma_op_type type, bool do_not_free)
  {
@@ -689,7 +689,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
              memset(ib, 0, packets_per_buffer * 28);
 
              if (type == SDMA_OP_VRAM_WRITE) {
-                     err = read_fp(storage_fp, buffer, min(bytes_remain, buffer_bo_size));
+                     err = img_read(storage_fd, buffer,
+                                    min(bytes_remain, buffer_bo_size));
                      if (err) {
                              pr_perror("failed to read from storage");
                              goto err_bo_list;
@@ -764,7 +765,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
              }
 
              if (type == SDMA_OP_VRAM_READ) {
-                     err = write_fp(storage_fp, buffer, buffer_bo_size - buffer_space_remain);
+                     err = img_write(storage_fd, buffer,
+                                     buffer_bo_size - buffer_space_remain);
                      if (err) {
                              pr_perror("failed to write out to storage");
                              goto err_cs_submit_ib;
@@ -817,11 +819,11 @@ void *dump_bo_contents(void *_thread_data)
      struct amdgpu_gpu_info gpu_info = { 0 };
      amdgpu_device_handle h_dev;
      size_t max_bo_size = 0, image_size = 0, buffer_size;
+     int bo_contents_fd = -1;
      uint64_t max_copy_size;
      uint32_t major, minor;
      int num_bos = 0;
      int i, ret = 0;
-     FILE *bo_contents_fp = NULL;
      void *buffer = NULL;
      char img_path[40];
 
@@ -862,10 +864,9 @@ void *dump_bo_contents(void *_thread_data)
      }
 
      snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, thread_data->id, thread_data->gpu_id);
-     bo_contents_fp = open_img_file(img_path, true, &image_size);
-     if (!bo_contents_fp) {
-             pr_perror("Cannot fopen %s", img_path);
-             ret = -EIO;
+     bo_contents_fd = open_img_file(img_path, true, &image_size);
+     if (bo_contents_fd < 0) {
+             ret = bo_contents_fd;
              goto exit;
      }
 
@@ -879,8 +880,9 @@ void *dump_bo_contents(void *_thread_data)
              num_bos++;
 
              /* perform sDMA based vram copy */
-             ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-                                SDMA_OP_VRAM_READ, false);
+             ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size,
+                                bo_contents_fd, buffer, buffer_size, h_dev,
+                                max_copy_size, SDMA_OP_VRAM_READ, false);
 
              if (ret) {
                      pr_err("Failed to drain the BO using sDMA: bo_buckets[%d]\n", i);
@@ -891,8 +893,8 @@ void *dump_bo_contents(void *_thread_data)
  exit:
      pr_info("Thread[0x%x] done num_bos:%d ret:%d\n", thread_data->gpu_id, num_bos, ret);
 
-     if (bo_contents_fp)
-             fclose(bo_contents_fp);
+     if (bo_contents_fd >= 0)
+             close(bo_contents_fd);
 
      xfree(buffer);
 
@@ -909,9 +911,9 @@ void *restore_bo_contents(void *_thread_data)
      size_t image_size = 0, total_bo_size = 0, max_bo_size = 0, buffer_size;
      struct amdgpu_gpu_info gpu_info = { 0 };
      amdgpu_device_handle h_dev;
+     int bo_contents_fd = -1;
      uint64_t max_copy_size;
      uint32_t major, minor;
-     FILE *bo_contents_fp = NULL;
      void *buffer = NULL;
      char img_path[40];
      int num_bos = 0;
@@ -936,10 +938,9 @@ void *restore_bo_contents(void *_thread_data)
                                                                 SDMA_LINEAR_COPY_MAX_SIZE - 1;
 
      snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, thread_data->id, thread_data->gpu_id);
-     bo_contents_fp = open_img_file(img_path, false, &image_size);
-     if (!bo_contents_fp) {
-             pr_perror("Cannot fopen %s", img_path);
-             ret = -errno;
+     bo_contents_fd = open_img_file(img_path, false, &image_size);
+     if (bo_contents_fd < 0) {
+             ret = bo_contents_fd;
              goto exit;
      }
 
@@ -978,8 +979,9 @@ void *restore_bo_contents(void *_thread_data)
 
              num_bos++;
 
-             ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-                                SDMA_OP_VRAM_WRITE, false);
+             ret = sdma_copy_bo(bo_buckets[i].dmabuf_fd, bo_buckets[i].size,
+                                bo_contents_fd, buffer, buffer_size, h_dev,
+                                max_copy_size, SDMA_OP_VRAM_WRITE, false);
              if (ret) {
                      pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                      break;
@@ -991,8 +993,8 @@ void *restore_bo_contents(void *_thread_data)
  exit:
      pr_info("Thread[0x%x] done num_bos:%d ret:%d\n", thread_data->gpu_id, num_bos, ret);
 
-     if (bo_contents_fp)
-             fclose(bo_contents_fp);
+     if (bo_contents_fd >= 0)
+             close(bo_contents_fd);
 
      xfree(buffer);
 
@@ -1016,9 +1018,7 @@ int check_hsakmt_shared_mem(uint64_t *shared_mem_size, uint32_t *shared_mem_magi
 
      /* First 4 bytes of shared file is the magic */
      ret = read_file(HSAKMT_SHM_PATH, shared_mem_magic, sizeof(*shared_mem_magic));
-     if (ret)
-             pr_perror("Failed to read shared mem magic");
-     else
+     if (!ret)
              pr_debug("Shared mem magic:0x%x\n", *shared_mem_magic);
 
      return 0;
@@ -1137,15 +1137,12 @@ int amdgpu_id_for_handle(int handle)
  static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
  {
      unsigned char *buf;
-     FILE *img_fp;
+     int fd, ret;
      size_t len;
-     int ret;
 
-     img_fp = open_img_file(filename, false, &len);
-     if (!img_fp) {
-             ret = -ENOENT;
-             goto out;
-     }
+     fd = open_img_file(filename, false, &len);
+     if (fd < 0)
+             return fd;
 
      buf = xmalloc(len);
      if (!buf) {
@@ -1153,7 +1150,7 @@ static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
              goto out_close;
      }
 
-     ret = read_fp(img_fp, buf, len);
+     ret = img_read(fd, buf, len);
      if (ret) {
              xfree(buf);
      } else {
@@ -1162,11 +1159,7 @@ static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
      }
 
  out_close:
-     fclose(img_fp);
-out:
-     if (ret < 0)
-             pr_err("Unable to read from %s", filename);
-
+     close(fd);
      return ret;
  }
 
@@ -1956,7 +1949,7 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
      CriuKfd *e = NULL;
      struct kfd_ioctl_criu_args args = { 0 };
      size_t img_size;
-     FILE *img_fp = NULL;
+     int img_fd;
 
      *retry_needed = false;
 
@@ -1967,8 +1960,8 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
 
      snprintf(img_path, sizeof(img_path), IMG_KFD_FILE, id);
 
-     img_fp = open_img_file(img_path, false, &img_size);
-     if (!img_fp)
+     img_fd = open_img_file(img_path, false, &img_size);
+     if (img_fd < 0)
              return amdgpu_plugin_restore_drm_file(id, retry_needed);
 
      fd = open(AMDGPU_KFD_DEVICE, O_RDWR | O_CLOEXEC);
@@ -1985,19 +1978,18 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
      pr_info("KFD Image file size:%ld\n", img_size);
      buf = xmalloc(img_size);
      if (!buf) {
-             fclose(img_fp);
+             close(img_fd);
              return -ENOMEM;
      }
 
-     ret = read_fp(img_fp, buf, img_size);
+     ret = img_read(img_fd, buf, img_size);
+     close(img_fd);
      if (ret) {
              pr_perror("Unable to read from %s", img_path);
-             fclose(img_fp);
              xfree(buf);
              return ret;
      }
 
-     fclose(img_fp);
      e = criu_kfd__unpack(NULL, img_size, buf);
      if (e == NULL) {
              pr_err("Unable to parse the KFD message %#x\n", id);
@@ -2254,25 +2246,23 @@ err:
      return ret;
  }
 
-FILE *get_bo_contents_fp(int id, int gpu_id, size_t tot_size)
+static int get_bo_contents_fd(int id, int gpu_id, size_t tot_size)
  {
      char img_path[PATH_MAX];
      size_t image_size = 0;
-     FILE *bo_contents_fp = NULL;
+     int fd;
 
      snprintf(img_path, sizeof(img_path), IMG_KFD_PAGES_FILE, id, gpu_id);
-     bo_contents_fp = open_img_file(img_path, false, &image_size);
-     if (!bo_contents_fp) {
-             pr_perror("Cannot fopen %s", img_path);
-             return NULL;
-     }
+     fd = open_img_file(img_path, false, &image_size);
+     if (fd < 0)
+             return fd;
 
      if (tot_size != image_size) {
              pr_err("%s size mismatch (current:%ld:expected:%ld)\n", img_path, image_size, tot_size);
-             fclose(bo_contents_fp);
-             return NULL;
+             close(fd);
+             return -EINVAL;
      }
-     return bo_contents_fp;
+     return fd;
  }
 
  struct parallel_thread_data {
@@ -2289,7 +2279,7 @@ void *parallel_restore_bo_contents(void *_thread_data)
      amdgpu_device_handle h_dev;
      uint64_t max_copy_size;
      size_t total_bo_size = 0, max_bo_size = 0, buffer_size = 0;
-     FILE *bo_contents_fp = NULL;
+     int bo_contents_fd = -1;
      parallel_restore_entry *entry;
      parallel_restore_cmd *restore_cmd = thread_data->restore_cmd;
      int ret = 0;
@@ -2310,12 +2300,12 @@ void *parallel_restore_bo_contents(void *_thread_data)
 
      buffer_size = kfd_max_buffer_size > 0 ? min(kfd_max_buffer_size, max_bo_size) : max_bo_size;
 
-     bo_contents_fp = get_bo_contents_fp(restore_cmd->cmd_head.id, thread_data->gpu_id, total_bo_size);
-     if (bo_contents_fp == NULL) {
-             ret = -1;
+     bo_contents_fd = get_bo_contents_fd(restore_cmd->cmd_head.id, thread_data->gpu_id, total_bo_size);
+     if (bo_contents_fd < 0) {
+             ret = bo_contents_fd;
              goto err_sdma;
      }
-     offset = ftell(bo_contents_fp);
+     offset = lseek(bo_contents_fd, 0, SEEK_CUR);
      if (offset < 0) {
              ret = -errno;
              pr_perror("Failed to alloc aligned memory. Consider setting KFD_MAX_BUFFER_SIZE.");
@@ -2334,17 +2324,17 @@ void *parallel_restore_bo_contents(void *_thread_data)
                      continue;
 
              entry = &restore_cmd->entries[i];
-             ret = fseeko(bo_contents_fp, entry->read_offset + offset,
-                          SEEK_SET);
-             if (ret < 0) {
+             ret = lseek(bo_contents_fd, entry->read_offset + offset,
+                         SEEK_SET);
+             if (ret) {
                      ret = -errno;
                      pr_err("Failed to seek for BO using sDMA: bo_buckets[%d]\n", i);
                      goto err_sdma;
              }
-             ret = sdma_copy_bo(restore_cmd->fds_write[entry->write_id], entry->size, bo_contents_fp,
-                                buffer, buffer_size, h_dev,
-                                max_copy_size, SDMA_OP_VRAM_WRITE, false);
-
+             ret = sdma_copy_bo(restore_cmd->fds_write[entry->write_id],
+                                entry->size, bo_contents_fd, buffer,
+                                buffer_size, h_dev, max_copy_size,
+                                SDMA_OP_VRAM_WRITE, false);
              if (ret) {
                      pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                      goto err_sdma;
@@ -2352,8 +2342,8 @@ void *parallel_restore_bo_contents(void *_thread_data)
      }
 
  err_sdma:
-     if (bo_contents_fp)
-             fclose(bo_contents_fp);
+     if (bo_contents_fd >= 0)
+             close(bo_contents_fd);
      if (buffer)
              xfree(buffer);
      amdgpu_device_deinitialize(h_dev);
plugins/amdgpu/amdgpu_plugin_dmabuf.c +6 -8
index 859ff42bb76a..fb894de16d75 100644
@@ -96,20 +96,19 @@ int __amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
  int amdgpu_plugin_dmabuf_restore(int id)
  {
      char path[PATH_MAX];
+     int fd_id, img_fd;
      size_t img_size;
-     FILE *img_fp = NULL;
      int ret = 0;
      CriuDmabufNode *rd = NULL;
      unsigned char *buf = NULL;
-     int fd_id;
 
      snprintf(path, sizeof(path), IMG_DMABUF_FILE, id);
 
      /* Read serialized metadata */
-     img_fp = open_img_file(path, false, &img_size);
-     if (!img_fp) {
+     img_fd = open_img_file(path, false, &img_size);
+     if (img_fd < 0) {
              pr_err("Failed to open dmabuf metadata file: %s\n", path);
-             return -EINVAL;
+             return img_fd;
      }
 
      pr_debug("dmabuf Image file size:%ld\n", img_size);
@@ -119,10 +118,9 @@ int amdgpu_plugin_dmabuf_restore(int id)
              return -ENOMEM;
      }
 
-     ret = read_fp(img_fp, buf, img_size);
-     fclose(img_fp);
+     ret = img_read(img_fd, buf, img_size);
+     close(img_fd);
      if (ret) {
-             pr_perror("Unable to read from %s", path);
              xfree(buf);
              return ret;
      }
plugins/amdgpu/amdgpu_plugin_drm.c +15 -17
index d0f84b46997c..070b5b19ec47 100644
@@ -163,8 +163,8 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
      amdgpu_device_handle h_dev;
      uint64_t max_copy_size;
      uint32_t major, minor;
-     FILE *bo_contents_fp = NULL;
      void *buffer = NULL;
+     int bo_contents_fd;
      char img_path[40];
      int i, ret = 0;
 
@@ -209,24 +209,23 @@ static int restore_bo_contents_drm(int drm_render_minor, CriuRenderNode *rd, int
 
              snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, drm_render_minor, i);
 
-             bo_contents_fp = open_img_file(img_path, false, &image_size);
-             if (!bo_contents_fp) {
-                     ret = -EIO;
+             bo_contents_fd = open_img_file(img_path, false, &image_size);
+             if (bo_contents_fd < 0) {
+                     ret = bo_contents_fd;
                      pr_err("Failed to open BO image file %s\n", img_path);
                      break;
              }
 
-             ret = sdma_copy_bo(dmabufs[i], rd->bo_entries[i]->size, bo_contents_fp, buffer, buffer_size, h_dev, max_copy_size,
-                                SDMA_OP_VRAM_WRITE, true);
+             ret = sdma_copy_bo(dmabufs[i], rd->bo_entries[i]->size,
+                                bo_contents_fd, buffer, buffer_size, h_dev,
+                                max_copy_size, SDMA_OP_VRAM_WRITE, true);
+             close(bo_contents_fd);
              if (ret) {
                      pr_err("Failed to fill the BO using sDMA: bo_buckets[%d]\n", i);
                      break;
              }
              pr_debug("** Successfully filled the BO using sDMA: bo_buckets[%d] **\n",
                       i);
-
-             if (bo_contents_fp)
-                     fclose(bo_contents_fp);
      }
 
  exit:
@@ -307,12 +306,12 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
              DrmBoEntry *boinfo = rd->bo_entries[i];
              struct drm_amdgpu_gem_list_handles_entry handle_entry = list_handles_entries[i];
              union drm_amdgpu_gem_mmap mmap_args = { 0 };
+             int bo_contents_fd;
              int dmabuf_fd;
              uint32_t major, minor;
              amdgpu_device_handle h_dev;
              void *buffer = NULL;
              char img_path[40];
-             FILE *bo_contents_fp = NULL;
              int device_fd;
 
              boinfo->size = handle_entry.size;
@@ -388,25 +387,24 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 
              snprintf(img_path, sizeof(img_path), IMG_DRM_PAGES_FILE, rd->id, rd->drm_render_minor, i);
              image_size = handle_entry.size;
-             bo_contents_fp = open_img_file(img_path, true, &image_size);
-             if (!bo_contents_fp) {
-                     ret = -EIO;
+             bo_contents_fd = open_img_file(img_path, true, &image_size);
+             if (bo_contents_fd < 0) {
+                     ret = bo_contents_fd;
                      goto exit;
              }
 
              posix_memalign(&buffer, sysconf(_SC_PAGE_SIZE), handle_entry.size);
 
-             ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fp, buffer, handle_entry.size, h_dev, 0x1000,
+             ret = sdma_copy_bo(dmabuf_fd, handle_entry.size, bo_contents_fd,
+                                buffer, handle_entry.size, h_dev, 0x1000,
                                 SDMA_OP_VRAM_READ, false);
+             close(bo_contents_fd);
              if (ret)
                      goto exit;
 
              if (dmabuf_fd != KFD_INVALID_FD)
                      close(dmabuf_fd);
 
-             if (bo_contents_fp)
-                     fclose(bo_contents_fp);
-
              ret = amdgpu_device_deinitialize(h_dev);
              if (ret)
                      goto exit;
plugins/amdgpu/amdgpu_plugin_util.c +57 -49
index 5925624740e7..411348b2ce0e 100644
@@ -192,26 +192,38 @@ void clear_dumped_fds()
      }
  }
 
-int read_fp(FILE *fp, void *buf, const size_t buf_len)
+int img_read(int fd, void *buf, const size_t buf_len)
  {
-     size_t len_read;
+     ssize_t len;
 
-     len_read = fread(buf, 1, buf_len, fp);
-     if (len_read != buf_len) {
-             pr_err("Unable to read file (read:%ld buf_len:%ld)\n", len_read, buf_len);
-             return -EIO;
+     len = read(fd, buf, buf_len);
+     if (len < 0 || len != buf_len) {
+             int ret;
+
+             if (len >= 0)
+                     errno = EIO;
+             ret = -errno;
+             pr_perror("Unable to read file (read:%ld buf_len:%ld)\n",
+                       len, buf_len);
+             return ret;
      }
      return 0;
  }
 
-int write_fp(FILE *fp, const void *buf, const size_t buf_len)
+int img_write(int fd, const void *buf, const size_t buf_len)
  {
-     size_t len_write;
+     ssize_t len;
 
-     len_write = fwrite(buf, 1, buf_len, fp);
-     if (len_write != buf_len) {
-             pr_err("Unable to write file (wrote:%ld buf_len:%ld)\n", len_write, buf_len);
-             return -EIO;
+     len = write(fd, buf, buf_len);
+     if (len < 0 || len != buf_len) {
+             int ret;
+
+             if (len >= 0)
+                     errno = EIO;
+             ret = -errno;
+             pr_perror("Unable to write file (wrote:%ld buf_len:%ld)\n",
+                       len, buf_len);
+             return ret;
      }
      return 0;
  }
@@ -222,63 +234,60 @@ int write_fp(FILE *fp, const void *buf, const size_t buf_len)
   * We store the size of the actual contents in the first 8-bytes of
   * the file. This allows us to determine the file size when using
   * criu_image_streamer when fseek and fstat are not available. The
- * FILE * returned is already at the location of the first actual
+ * file descriptor returned is already at the location of the first actual
   * contents.
   *
   * @param path The file path
   * @param write False for read, true for write
   * @param size Size of actual contents
- * @return FILE *if successful, NULL if failed
+ * @return file descriptor if successful, negative integer if failed
   */
-FILE *open_img_file(char *path, bool write, size_t *size)
+int open_img_file(char *path, bool write, size_t *size)
  {
-     FILE *fp = NULL;
      int fd, ret;
 
-     if (opts.stream)
+     if (opts.stream) {
              fd = img_streamer_open(path, write ? O_DUMP : O_RSTR);
-     else
-             fd = openat(criu_get_image_dir(), path, write ? (O_WRONLY | O_CREAT) : O_RDONLY, 0600);
-
-     if (fd < 0) {
-             pr_err("%s: Failed to open for %s\n", path, write ? "write" : "read");
-             return NULL;
+             if (fd < 0)
+                     errno = -fd;
+     } else {
+             fd = openat(criu_get_image_dir(), path,
+                         write ? (O_WRONLY | O_CREAT) : O_RDONLY, 0600);
      }
 
-     fp = fdopen(fd, write ? "w" : "r");
-     if (!fp) {
-             pr_err("%s: Failed get pointer for %s\n", path, write ? "write" : "read");
-             return NULL;
+     if (fd < 0) {
+             pr_perror("%s: Failed to open for %s\n",
+                       path, write ? "write" : "read");
+             return fd;
      }
 
      if (write)
-             ret = write_fp(fp, size, sizeof(*size));
+             ret = img_write(fd, size, sizeof(*size));
      else
-             ret = read_fp(fp, size, sizeof(*size));
-
+             ret = img_read(fd, size, sizeof(*size));
      if (ret) {
-             pr_err("%s:Failed to access file size\n", path);
-             fclose(fp);
-             return NULL;
+             close(fd);
+             return ret;
      }
 
      pr_debug("%s:Opened file for %s with size:%ld\n", path, write ? "write" : "read", *size);
-     return fp;
+     return fd;
  }
 
  int read_file(const char *file_path, void *buf, const size_t buf_len)
  {
-     int ret;
-     FILE *fp;
+     int ret, fd;
 
-     fp = fopen(file_path, "r");
-     if (!fp) {
-             pr_err("Cannot fopen %s\n", file_path);
-             return -errno;
+     fd = open(file_path, O_RDONLY);
+     if (fd < 0) {
+             int ret = errno;
+
+             pr_perror("Cannot open %s\n", file_path);
+             return -ret;
      }
 
-     ret = read_fp(fp, buf, buf_len);
-     fclose(fp); /* this will also close fd */
+     ret = img_read(fd, buf, buf_len);
+     close(fd);
      return ret;
  }
 
@@ -296,16 +305,15 @@ int read_file(const char *file_path, void *buf, const size_t buf_len)
   */
  int write_img_file(char *path, const void *buf, const size_t buf_len)
  {
-     int ret;
-     FILE *fp;
+     int ret, fd;
      size_t len = buf_len;
 
-     fp = open_img_file(path, true, &len);
-     if (!fp)
-             return -errno;
+     fd = open_img_file(path, true, &len);
+     if (fd < 0)
+             return fd;
 
-     ret = write_fp(fp, buf, buf_len);
-     fclose(fp); /* this will also close fd */
+     ret = img_write(fd, buf, buf_len);
+     close(fd);
      return ret;
  }
 
plugins/amdgpu/amdgpu_plugin_util.h +4 -4
index 014abf35ccb6..eb0c28674d18 100644
@@ -106,11 +106,11 @@ extern bool kfd_vram_size_check;
  extern bool kfd_numa_check;
  extern bool kfd_capability_check;
 
-int read_fp(FILE *fp, void *buf, const size_t buf_len);
-int write_fp(FILE *fp, const void *buf, const size_t buf_len);
+int img_read(int fd, void *buf, const size_t buf_len);
+int img_write(int fd, const void *buf, const size_t buf_len);
  int read_file(const char *file_path, void *buf, const size_t buf_len);
  int write_img_file(char *path, const void *buf, const size_t buf_len);
-FILE *open_img_file(char *path, bool write, size_t *size);
+int open_img_file(char *path, bool write, size_t *size);
 
  int record_dumped_fd(int fd, bool is_drm);
  struct list_head *get_dumped_fds();
@@ -127,7 +127,7 @@ void clear_restore_state();
 
  void print_kfd_bo_stat(int bo_cnt, struct kfd_criu_bo_bucket *bo_list);
 
-int sdma_copy_bo(int shared_fd, uint64_t size, FILE *storage_fp,
+int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
               void *buffer, size_t buffer_size, amdgpu_device_handle h_dev,
               uint64_t max_copy_size, enum sdma_op_type type, bool do_not_free);
 
#30 lore raw

RFC 19/26 plugins/amdgpu: Use save_vma_updates for all call sites

In reply to Tvrtko Ursulin

Consolidate KFD and DRM VMA handling path to the same (existing) helper.

---
 plugins/amdgpu/amdgpu_plugin.c     | 46 ++++++++++++------------------
 plugins/amdgpu/amdgpu_plugin_drm.c |  3 +-
 plugins/amdgpu/amdgpu_plugin_drm.h |  3 +-
 3 files changed, 22 insertions(+), 30 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +18 -28
index dc808fe32373..5ab3420c1371 100644
@@ -1669,7 +1669,8 @@ static int restore_bos(struct kfd_ioctl_criu_args *args, CriuKfd *e)
 	return 0;
 }
 
-int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset, int fd)
+int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset,
+		     int fd)
 {
 	struct vma_metadata *vma_md;
 
@@ -1678,14 +1679,15 @@ int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset, i
 		return -ENOMEM;
 	}
 
-	memset(vma_md, 0, sizeof(*vma_md));
-
 	vma_md->old_pgoff = offset;
 	vma_md->vma_entry = addr;
-
 	vma_md->new_pgoff = restored_offset;
 	vma_md->fd = fd;
 
+	pr_debug("adding vma_entry:addr:%"  PRIx64 " old-off:%" PRIx64 " new_off:%" PRIx64 " fd:%d\n",
+		 vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff,
+		 vma_md->fd);
+
 	list_add_tail(&vma_md->list, &update_vma_info_list);
 
 	return 0;
@@ -1703,37 +1705,25 @@ static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKf
 
 		if (bo_bucket->alloc_flags & (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT |
 					      KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP | KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL)) {
-			struct vma_metadata *vma_md;
 			uint32_t target_gpu_id; /* actual gpu_id where the BO will be restored */
 
-			vma_md = xmalloc(sizeof(*vma_md));
-			if (!vma_md) {
-				ret = -ENOMEM;
-				goto exit;
-			}
-
-			memset(vma_md, 0, sizeof(*vma_md));
-
-			vma_md->old_pgoff = bo_bucket->offset;
-			vma_md->vma_entry = bo_bucket->addr;
-
-			target_gpu_id = maps_get_dest_gpu(&restore_maps, bo_bucket->gpu_id);
-
-			tp_node = sys_get_node_by_gpu_id(&dest_topology, target_gpu_id);
+			target_gpu_id = maps_get_dest_gpu(&restore_maps,
+							  bo_bucket->gpu_id);
+			tp_node = sys_get_node_by_gpu_id(&dest_topology,
+							 target_gpu_id);
 			if (!tp_node) {
-				pr_err("Failed to find node with gpu_id:0x%04x\n", target_gpu_id);
+				pr_err("Failed to find node with gpu_id:0x%04x\n",
+				       target_gpu_id);
 				ret = -ENODEV;
 				goto exit;
 			}
 
-			vma_md->new_pgoff = bo_bucket->restored_offset;
-			vma_md->fd = node_get_drm_render_device(tp_node);
-
-			pr_debug("adding vma_entry:addr:0x%lx old-off:0x%lx new_off:0x%lx new_minor:%d\n",
-				 vma_md->vma_entry, vma_md->old_pgoff,
-				 vma_md->new_pgoff, tp_node->drm_render_minor);
-
-			list_add_tail(&vma_md->list, &update_vma_info_list);
+			ret = save_vma_updates(bo_bucket->offset,
+					       bo_bucket->addr,
+					       bo_bucket->restored_offset,
+					       node_get_drm_render_device(tp_node));
+			if (ret)
+				goto exit;
 		}
 	}
 
plugins/amdgpu/amdgpu_plugin_drm.c +2 -1
index 070b5b19ec47..de6d6f178d51 100644
@@ -557,7 +557,8 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 			}
 		}
 
-		ret = save_vma_updates(boinfo->offset, boinfo->addr, mmap_args.out.addr_ptr, fd);
+		ret = save_vma_updates(boinfo->offset, boinfo->addr,
+				       mmap_args.out.addr_ptr, fd);
 		if (ret < 0)
 			goto exit;
 	}
plugins/amdgpu/amdgpu_plugin_drm.h +2 -1
index c766def563f6..f76e29f7f73f 100644
@@ -34,7 +34,8 @@ int store_dmabuf_fd(int handle, int fd);
 
 int get_gem_handle(amdgpu_device_handle h_dev, int dmabuf_fd);
 
-int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset, int gpu_id);
+int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset,
+		     int fd);
 
 #endif		/* __AMDGPU_PLUGIN_DRM_H__ */
 
Signature · 1 line
2.52.0
Reviewed-by on v1 19/26
#31 lore raw

Re: [RFC 19/26] plugins/amdgpu: Use save_vma_updates for all call sites

Looks good to me

This patch is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 19/26] plugins/amdgpu: Use save_vma_updates for all call sites

Consolidate KFD and DRM VMA handling path to the same (existing) helper.

---
 plugins/amdgpu/amdgpu_plugin.c     | 46 ++++++++++++------------------
 plugins/amdgpu/amdgpu_plugin_drm.c |  3 +-
 plugins/amdgpu/amdgpu_plugin_drm.h |  3 +-
 3 files changed, 22 insertions(+), 30 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +18 -28
index dc808fe32373..5ab3420c1371 100644
@@ -1669,7 +1669,8 @@ static int restore_bos(struct kfd_ioctl_criu_args *args, CriuKfd *e)
        return 0;
 }
 
-int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset, int fd)
+int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset,
+                    int fd)
 {
        struct vma_metadata *vma_md;
 
@@ -1678,14 +1679,15 @@ int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset, i
                return -ENOMEM;
        }
 
-       memset(vma_md, 0, sizeof(*vma_md));
-
        vma_md->old_pgoff = offset;
        vma_md->vma_entry = addr;
-
        vma_md->new_pgoff = restored_offset;
        vma_md->fd = fd;
 
+       pr_debug("adding vma_entry:addr:%"  PRIx64 " old-off:%" PRIx64 " new_off:%" PRIx64 " fd:%d\n",
+                vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff,
+                vma_md->fd);
+
        list_add_tail(&vma_md->list, &update_vma_info_list);
 
        return 0;
@@ -1703,37 +1705,25 @@ static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKf
 
                if (bo_bucket->alloc_flags & (KFD_IOC_ALLOC_MEM_FLAGS_VRAM | KFD_IOC_ALLOC_MEM_FLAGS_GTT |
                                              KFD_IOC_ALLOC_MEM_FLAGS_MMIO_REMAP | KFD_IOC_ALLOC_MEM_FLAGS_DOORBELL)) {
-                       struct vma_metadata *vma_md;
                        uint32_t target_gpu_id; /* actual gpu_id where the BO will be restored */
 
-                       vma_md = xmalloc(sizeof(*vma_md));
-                       if (!vma_md) {
-                               ret = -ENOMEM;
-                               goto exit;
-                       }
-
-                       memset(vma_md, 0, sizeof(*vma_md));
-
-                       vma_md->old_pgoff = bo_bucket->offset;
-                       vma_md->vma_entry = bo_bucket->addr;
-
-                       target_gpu_id = maps_get_dest_gpu(&restore_maps, bo_bucket->gpu_id);
-
-                       tp_node = sys_get_node_by_gpu_id(&dest_topology, target_gpu_id);
+                       target_gpu_id = maps_get_dest_gpu(&restore_maps,
+                                                         bo_bucket->gpu_id);
+                       tp_node = sys_get_node_by_gpu_id(&dest_topology,
+                                                        target_gpu_id);
                        if (!tp_node) {
-                               pr_err("Failed to find node with gpu_id:0x%04x\n", target_gpu_id);
+                               pr_err("Failed to find node with gpu_id:0x%04x\n",
+                                      target_gpu_id);
                                ret = -ENODEV;
                                goto exit;
                        }
 
-                       vma_md->new_pgoff = bo_bucket->restored_offset;
-                       vma_md->fd = node_get_drm_render_device(tp_node);
-
-                       pr_debug("adding vma_entry:addr:0x%lx old-off:0x%lx new_off:0x%lx new_minor:%d\n",
-                                vma_md->vma_entry, vma_md->old_pgoff,
-                                vma_md->new_pgoff, tp_node->drm_render_minor);
-
-                       list_add_tail(&vma_md->list, &update_vma_info_list);
+                       ret = save_vma_updates(bo_bucket->offset,
+                                              bo_bucket->addr,
+                                              bo_bucket->restored_offset,
+                                              node_get_drm_render_device(tp_node));
+                       if (ret)
+                               goto exit;
                }
        }
 
plugins/amdgpu/amdgpu_plugin_drm.c +2 -1
index 070b5b19ec47..de6d6f178d51 100644
@@ -557,7 +557,8 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
                        }
                }
 
-               ret = save_vma_updates(boinfo->offset, boinfo->addr, mmap_args.out.addr_ptr, fd);
+               ret = save_vma_updates(boinfo->offset, boinfo->addr,
+                                      mmap_args.out.addr_ptr, fd);
                if (ret < 0)
                        goto exit;
        }
plugins/amdgpu/amdgpu_plugin_drm.h +2 -1
index c766def563f6..f76e29f7f73f 100644
@@ -34,7 +34,8 @@ int store_dmabuf_fd(int handle, int fd);
 
 int get_gem_handle(amdgpu_device_handle h_dev, int dmabuf_fd);
 
-int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset, int gpu_id);
+int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset,
+                    int fd);
 
 #endif         /* __AMDGPU_PLUGIN_DRM_H__ */
 

--
2.52.0

#32 lore raw

RFC 20/26 plugins/amdgpu: amdgpu_plugin_drm_restore_file() does not need to use libdrm

In reply to Tvrtko Ursulin

There is no need to initialize and de-initialize libdrm only to use the duplicated fd for calls to drmPrimeFDToHandle(). From the kernel's perspective it is exactly the same file so operation is identical to just using the passed in file descriptor.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)
plugins/amdgpu/amdgpu_plugin_drm.c +3 -15
index de6d6f178d51..99a6804f51eb 100644
@@ -454,9 +454,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 {
 	int ret = 0;
 	bool retry_needed = false;
-	uint32_t major, minor;
-	amdgpu_device_handle h_dev;
-	int device_fd, *dmabufs;
+	int *dmabufs;
 
 	dmabufs = xzalloc(sizeof(int) * rd->num_of_bos);
 	if (!dmabufs) {
@@ -464,14 +462,6 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 		return -ENOMEM;
 	}
 
-	ret = amdgpu_device_initialize(fd, &major, &minor, &h_dev);
-	if (ret) {
-		pr_info("Error in init amdgpu device\n");
-		goto exit;
-	}
-
-	device_fd = amdgpu_device_get_fd(h_dev);
-
 	for (int i = 0; i < rd->num_of_bos; i++) {
 		DrmBoEntry *boinfo = rd->bo_entries[i];
 		int dmabuf_fd = -1;
@@ -495,7 +485,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 		}
 
 		if (boinfo->is_import) {
-			drmPrimeFDToHandle(device_fd, dmabuf_fd, &handle);
+			drmPrimeFDToHandle(fd, dmabuf_fd, &handle);
 		} else {
 			union drm_amdgpu_gem_create create_args = { 0 };
 
@@ -511,7 +501,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 			}
 			handle = create_args.out.handle;
 
-			drmPrimeHandleToFD(device_fd, handle, 0, &dmabuf_fd);
+			drmPrimeHandleToFD(fd, handle, 0, &dmabuf_fd);
 		}
 
 		change_args.handle = handle;
@@ -572,8 +562,6 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 	if (ret)
 		goto exit;
 
-	ret = amdgpu_device_deinitialize(h_dev);
-
 	if (rd->num_of_bos > 0) {
 		ret = restore_bo_contents_drm(rd->drm_render_minor, rd, fd, dmabufs);
 		if (ret)
Signature · 1 line
2.52.0
Reviewed-by on v1 20/26
#33 lore raw

Re: [RFC 20/26] plugins/amdgpu: amdgpu_plugin_drm_restore_file() does not need to use libdrm

I think we used those major, minor values somewhere once but now that we don't, this looks sensible

This patch is

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 20/26] plugins/amdgpu: amdgpu_plugin_drm_restore_file() does not need to use libdrm

There is no need to initialize and de-initialize libdrm only to use the duplicated fd for calls to drmPrimeFDToHandle(). From the kernel's perspective it is exactly the same file so operation is identical to just using the passed in file descriptor.

---
 plugins/amdgpu/amdgpu_plugin_drm.c | 18 +++---------------
 1 file changed, 3 insertions(+), 15 deletions(-)
plugins/amdgpu/amdgpu_plugin_drm.c +3 -15
index de6d6f178d51..99a6804f51eb 100644
@@ -454,9 +454,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 {
        int ret = 0;
        bool retry_needed = false;
-       uint32_t major, minor;
-       amdgpu_device_handle h_dev;
-       int device_fd, *dmabufs;
+       int *dmabufs;
 
        dmabufs = xzalloc(sizeof(int) * rd->num_of_bos);
        if (!dmabufs) {
@@ -464,14 +462,6 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
                return -ENOMEM;
        }
 
-       ret = amdgpu_device_initialize(fd, &major, &minor, &h_dev);
-       if (ret) {
-               pr_info("Error in init amdgpu device\n");
-               goto exit;
-       }
-
-       device_fd = amdgpu_device_get_fd(h_dev);
-
        for (int i = 0; i < rd->num_of_bos; i++) {
                DrmBoEntry *boinfo = rd->bo_entries[i];
                int dmabuf_fd = -1;
@@ -495,7 +485,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
                }
 
                if (boinfo->is_import) {
-                       drmPrimeFDToHandle(device_fd, dmabuf_fd, &handle);
+                       drmPrimeFDToHandle(fd, dmabuf_fd, &handle);
                } else {
                        union drm_amdgpu_gem_create create_args = { 0 };
 
@@ -511,7 +501,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
                        }
                        handle = create_args.out.handle;
 
-                       drmPrimeHandleToFD(device_fd, handle, 0, &dmabuf_fd);
+                       drmPrimeHandleToFD(fd, handle, 0, &dmabuf_fd);
                }
 
                change_args.handle = handle;
@@ -572,8 +562,6 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
        if (ret)
                goto exit;
 
-       ret = amdgpu_device_deinitialize(h_dev);
-
        if (rd->num_of_bos > 0) {
                ret = restore_bo_contents_drm(rd->drm_render_minor, rd, fd, dmabufs);
                if (ret)

--
2.52.0

#34 lore raw

Re: [RFC 20/26] plugins/amdgpu: amdgpu_plugin_drm_restore_file() does not need to use libdrm

Quoted 6 lines · David

I think we used those major, minor values somewhere once but now that we don't, this looks sensible

This patch is

Thank you!

And if the major and minor will be needed in the future I think they can be obtained more simply by fstat.

Regards,

Tvrtko

Quoted 77 lines

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 20/26] plugins/amdgpu: amdgpu_plugin_drm_restore_file() does not need to use libdrm

There is no need to initialize and de-initialize libdrm only to use the duplicated fd for calls to drmPrimeFDToHandle(). From the kernel's perspective it is exactly the same file so operation is identical to just using the passed in file descriptor.

---
  plugins/amdgpu/amdgpu_plugin_drm.c | 18 +++---------------
  1 file changed, 3 insertions(+), 15 deletions(-)
plugins/amdgpu/amdgpu_plugin_drm.c +3 -16
index de6d6f178d51..99a6804f51eb 100644
@@ -454,9 +454,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
  {
         int ret = 0;
         bool retry_needed = false;
-       uint32_t major, minor;
-       amdgpu_device_handle h_dev;
-       int device_fd, *dmabufs;
+       int *dmabufs;
 
         dmabufs = xzalloc(sizeof(int) * rd->num_of_bos);
         if (!dmabufs) {
@@ -464,14 +462,6 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
                 return -ENOMEM;
         }
 
-       ret = amdgpu_device_initialize(fd, &major, &minor, &h_dev);
-       if (ret) {
-               pr_info("Error in init amdgpu device\n");
-               goto exit;
-       }
-
-       device_fd = amdgpu_device_get_fd(h_dev);
-
         for (int i = 0; i < rd->num_of_bos; i++) {
                 DrmBoEntry *boinfo = rd->bo_entries[i];
                 int dmabuf_fd = -1;
@@ -495,7 +485,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
                 }
 
                 if (boinfo->is_import) {
-                       drmPrimeFDToHandle(device_fd, dmabuf_fd, &handle);
+                       drmPrimeFDToHandle(fd, dmabuf_fd, &handle);
                 } else {
                         union drm_amdgpu_gem_create create_args = { 0 };
 
@@ -511,7 +501,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
                         }
                         handle = create_args.out.handle;
 
-                       drmPrimeHandleToFD(device_fd, handle, 0, &dmabuf_fd);
+                       drmPrimeHandleToFD(fd, handle, 0, &dmabuf_fd);
                 }
 
                 change_args.handle = handle;
@@ -572,8 +562,6 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
         if (ret)
                 goto exit;
 
-       ret = amdgpu_device_deinitialize(h_dev);
-
         if (rd->num_of_bos > 0) {
                 ret = restore_bo_contents_drm(rd->drm_render_minor, rd, fd, dmabufs);
                 if (ret)
--

2.52.0

#35 lore raw

RFC 21/26 plugins/amdgpu: Enable restore with only the drm node open

In reply to Tvrtko Ursulin

It is currently not possible to restore a process which did not have a KFD device open.

We can work around that by assuming the restore is being done on the same system with the same GPU id. This can fail over reboots when the GPU id changes, but that also could be worked around with perhaps something like a "--ignore-gpu-id" command line switch.

In the long term we probably need to think of adding the relevant GPU discovery APIs to the amdgpu kernel driver, which would enable this to work more smoothly.

---
 plugins/amdgpu/amdgpu_plugin.c          | 62 +++++++++++++++++++++----
 plugins/amdgpu/amdgpu_plugin_drm.c      |  5 +-
 plugins/amdgpu/amdgpu_plugin_topology.c |  2 +
 plugins/amdgpu/amdgpu_plugin_topology.h |  2 +
 4 files changed, 62 insertions(+), 9 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +54 -8
index 5ab3420c1371..693df0ea7e39 100644
@@ -1844,11 +1844,11 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
 	size_t img_size;
 	int fd, ret;
 
-	/* This is restorer plugin for renderD nodes. Criu doesn't guarantee that they will
-	 * be called before the plugin is called for kfd file descriptor.
-	 * TODO: Currently, this code will only work if this function is called for /dev/kfd
-	 * first as we assume restore_maps is already filled. Need to fix this later.
+	/* This is restorer plugin for renderD nodes. Criu doesn't guarantee
+	 * that they will be called before the plugin is called for kfd file
+	 * descriptor.
 	 */
+
 	snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
 	ret = load_img(img_path, &buf, &img_size);
 	if (ret < 0) {
@@ -1876,14 +1876,60 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
 
 	pr_info("render node gpu_id = 0x%04x\n", rd->gpu_id);
 
-	target_gpu_id = maps_get_dest_gpu(&restore_maps, rd->gpu_id);
-	if (!target_gpu_id) {
-		fd = -ENODEV;
-		goto fail;
+	if (fd_next == -1) {
+		ret = find_unused_fd_pid(getpid());
+		if (ret < 0) {
+			pr_err("Failed to find unused fd (fd:%d)\n", ret);
+			goto fail;
+		}
+		fd_next = ret;
+	}
+
+	if (!dest_topology.parsed) {
+		pr_info("Parsing local topology for render node restore\n");
+		ret = topology_parse(&dest_topology, "Local");
+		if (ret) {
+			pr_err("Failed to parse local system topology %d\n",
+			       ret);
+			goto fail;
+		}
+	}
+
+	if (restore_maps.mapped_cnt) {
+		target_gpu_id = maps_get_dest_gpu(&restore_maps, rd->gpu_id);
+		if (!target_gpu_id) {
+			pr_perror("Unable to map gpu_id 0x%04x\n", rd->gpu_id);
+			fd = -ENODEV;
+			goto fail;
+		}
+	} else {
+		unsigned int num_gpus = 0;
+
+		pr_info("Assuming same system with a single gpu_id 0x%04x\n",
+			rd->gpu_id);
+		list_for_each_entry(tp_node, &dest_topology.nodes,
+				    listm_system) {
+			if (NODE_IS_GPU(tp_node)) {
+				num_gpus++;
+				target_gpu_id = tp_node->gpu_id;
+			}
+		}
+		if (num_gpus != 1) {
+			pr_perror("Unexpectedly found %u GPUs\n", num_gpus);
+			fd = -EINVAL;
+			goto fail;
+		} else if (target_gpu_id != rd->gpu_id) {
+			pr_perror("Unexpectedly found gpu_id 0x%04x (expected 0x%04x)\n",
+				  target_gpu_id, rd->gpu_id);
+			fd = -EINVAL;
+			goto fail;
+		}
 	}
 
 	tp_node = sys_get_node_by_gpu_id(&dest_topology, target_gpu_id);
 	if (!tp_node) {
+		pr_perror("Unable to find target gpu_id=0x%04x\n",
+			  target_gpu_id);
 		fd = -ENODEV;
 		goto fail;
 	}
plugins/amdgpu/amdgpu_plugin_drm.c +4 -1
index 99a6804f51eb..30a0626b7b5f 100644
@@ -428,7 +428,10 @@ int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm)
 	}
 
 	/* Get the GPU_ID of the DRM device */
-	rd->gpu_id = maps_get_dest_gpu(&checkpoint_maps, tp_node->gpu_id);
+	if (checkpoint_maps.mapped_cnt)
+		rd->gpu_id = maps_get_dest_gpu(&checkpoint_maps, tp_node->gpu_id);
+	else
+		rd->gpu_id = tp_node->gpu_id; /* Render node only */
 	if (!rd->gpu_id) {
 		pr_err("Failed to find valid gpu_id for the device = %d\n", tp_node->gpu_id);
 		return -ENODEV;
plugins/amdgpu/amdgpu_plugin_topology.c +2 -0
index 730f2e028430..b26c12e4c7d4 100644
@@ -307,6 +307,7 @@ void maps_init(struct device_maps *maps)
 	INIT_LIST_HEAD(&maps->gpu_maps);
 	maps->tail_cpu = 0;
 	maps->tail_gpu = 0;
+	maps->mapped_cnt = 0;
 }
 
 void maps_free(struct device_maps *maps)
@@ -1220,6 +1221,7 @@ static bool map_devices(struct tp_system *src_sys, struct tp_system *dest_sys, s
 
 			if (map_devices(src_sys, dest_sys, src_nodes, dest_nodes, maps)) {
 				pr_debug("Matched nodes 0x%04X and after\n", dest_node->gpu_id);
+				maps->mapped_cnt++;
 				return true;
 			} else {
 				/* We could not map remaining nodes in the list. Add dest node back
plugins/amdgpu/amdgpu_plugin_topology.h +2 -0
index e19f8e7ce9af..ab850d6bdce6 100644
@@ -92,6 +92,8 @@ struct id_map {
 };
 
 struct device_maps {
+	unsigned int mapped_cnt;
+
 	struct list_head cpu_maps; /* CPUs are mapped using node_id */
 	struct list_head gpu_maps;
 
Signature · 1 line
2.52.0
#36 lore raw

RFC 22/26 plugins/amdgpu: Handle DRM render node mmaps

In reply to Tvrtko Ursulin

Outside the KFD world the CPU virtual memory address of a mmapp-ed buffer object is not guaranteed to be same as the GPU virtual address. Therefore the address check in VMA restore will prevent any restore of a render node process.

For now we relax the criteria in case of render node to match only on the offset. In the future this will need to be made more robust so strange partial mmaps can be handled as well.

---
 plugins/amdgpu/amdgpu_plugin.c | 35 ++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +19 -16
index 693df0ea7e39..664536850c42 100644
@@ -2170,25 +2170,28 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
 	}
 
 	list_for_each_entry(vma_md, &update_vma_info_list, list) {
-		if (addr == vma_md->vma_entry && old_offset == vma_md->old_pgoff) {
-			*new_offset = vma_md->new_pgoff;
+		if (old_offset != vma_md->old_pgoff)
+			continue;
+		if (is_kfd && addr != vma_md->vma_entry)
+			continue;
 
+		*new_offset = vma_md->new_pgoff;
+		if (is_renderD) {
+			int fd = dup(vma_md->fd);
+
+			if (fd == -1) {
+				pr_perror("unable to duplicate the render fd");
+				return -1;
+			}
+			*updated_fd = fd;
+		} else {
 			*updated_fd = -1;
-			if (is_renderD) {
-				int fd = dup(vma_md->fd);
-				if (fd == -1) {
-					pr_perror("unable to duplicate the render fd");
-					return -1;
-				}
-				*updated_fd = fd;
-			}
-
-			pr_debug("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n",
-				 vma_md->old_pgoff, vma_md->new_pgoff,
-				 *updated_fd);
-
-			return 1;
 		}
+
+		pr_debug("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n",
+			 vma_md->old_pgoff, vma_md->new_pgoff, *updated_fd);
+
+		return 1;
 	}
 	pr_info("No match for addr:0x%lx offset:%lx\n", addr, old_offset);
 	return 0;
Signature · 1 line
2.52.0
#37 lore raw

RFC 23/26 plugins/amdgpu: Consolidate shared memory handling

In reply to Tvrtko Ursulin

Shortly we will need to share more data between the processes being restored so lets move all of it under the same top level object to make it easier to add new struct members.

---
 plugins/amdgpu/amdgpu_plugin.c | 85 ++++++++++++++++++++--------------
 1 file changed, 49 insertions(+), 36 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +49 -36
index 664536850c42..f4bda11b3de5 100644
@@ -71,13 +71,12 @@ struct handle_id {
 	int handle;
 	int fdstore_id;
 };
-struct shared_handle_ids {
-	int num_handles;
-	struct handle_id *handles;
-};
-struct shared_handle_ids *shared_memory = NULL;
 
-static mutex_t *shared_memory_mutex;
+static struct shared_memory {
+	mutex_t			mutex;
+	int 			num_handles;
+	struct handle_id	*handles;
+} *shared_memory;
 
 /*
  * In the case of a single process (common case), this optimization can effectively
@@ -1100,38 +1099,47 @@ CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__DUMP_DEVICES_LATE, amdgpu_unpause_proces
 
 int store_dmabuf_fd(int handle, int fd)
 {
+	int ret = -1;
 	int id;
 
+	if (!shared_memory)
+		return -1;
+
 	id = fdstore_add(fd);
-	mutex_lock(shared_memory_mutex);
+	mutex_lock(&shared_memory->mutex);
 	for (int i = 0; i < shared_memory->num_handles; i++) {
 		if (shared_memory->handles[i].handle == handle) {
-			mutex_unlock(shared_memory_mutex);
-			return 0;
-		}
-		if (shared_memory->handles[i].handle == -1) {
+			ret = 0;
+			break;
+		} else if (shared_memory->handles[i].handle == -1) {
 			shared_memory->handles[i].handle = handle;
 			shared_memory->handles[i].fdstore_id = id;
-			mutex_unlock(shared_memory_mutex);
-			return 0;
+			ret = 0;
+			break;
 		}
 	}
-	mutex_unlock(shared_memory_mutex);
+	mutex_unlock(&shared_memory->mutex);
 
-	return -1;
+	return ret;
 }
 
 int amdgpu_id_for_handle(int handle)
 {
-	mutex_lock(shared_memory_mutex);
+	int ret = -1;
+
+	if (!shared_memory)
+		return -1;
+
+	mutex_lock(&shared_memory->mutex);
 	for (int i = 0; i < shared_memory->num_handles; i++) {
 		if (shared_memory->handles[i].handle == handle) {
-			mutex_unlock(shared_memory_mutex);
-			return shared_memory->handles[i].fdstore_id;
+			ret = shared_memory->handles[i].fdstore_id;
+			break;
 		}
 	}
-	mutex_unlock(shared_memory_mutex);
-	return -1;
+	mutex_unlock(&shared_memory->mutex);
+
+	return ret;
 }
 
 static int load_img(char *filename, unsigned char **out_buf, size_t *out_len)
@@ -1212,27 +1220,32 @@ int amdgpu_restore_init(void)
 	}
 	closedir(d);
 
-	if (num_handles > 0) {
-		const int protection = PROT_READ | PROT_WRITE;
-		const int visibility = MAP_SHARED | MAP_ANONYMOUS;
+	if (!num_handles)
+		return 0;
 
-		shared_memory = mmap(NULL, sizeof(shared_memory), protection, visibility, -1, 0);
-		shared_memory->num_handles = num_handles;
-		shared_memory->handles = mmap(NULL, sizeof(struct handle_id) * num_handles, protection, visibility, -1, 0);
+	shared_memory = shmalloc(sizeof(*shared_memory));
+	if (!shared_memory) {
+		pr_err("Can't create amdgpu shared memory\n");
+		return -1;
+	}
 
-		for (int i = 0; i < num_handles; i++) {
-			shared_memory->handles[i].handle = -1;
-			shared_memory->handles[i].fdstore_id = -1;
-		}
+	shared_memory->handles = shmalloc(num_handles *
+					  sizeof(*shared_memory->handles));
+	if (!shared_memory->handles) {
+		shfree_last(shared_memory);
+		pr_err("Can't create amdgpu shared memory handles\n");
+		return -1;
+	}
 
-		shared_memory_mutex = shmalloc(sizeof(*shared_memory_mutex));
-		if (!shared_memory_mutex) {
-			pr_err("Can't create amdgpu mutex\n");
-			return -1;
-		}
-		mutex_init(shared_memory_mutex);
+	for (int i = 0; i < num_handles; i++) {
+		shared_memory->handles[i].handle = -1;
+		shared_memory->handles[i].fdstore_id = -1;
 	}
 
+	mutex_init(&shared_memory->mutex);
+
+	shared_memory->num_handles = num_handles;
+
 	return 0;
 }
 CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__RESTORE_INIT, amdgpu_restore_init)
Signature · 1 line
2.52.0
#38 lore raw

RFC 24/26 plugins/amdgpu: Store VMA data in shared memory

In reply to Tvrtko Ursulin

If a process mmap()'s a device VMA and then forks it will currently fail to restore because the saved VMA metadata is populated after the target processes are forked. That is, only one process will see the list of struct vma_metadata needed to correctly restore. The other one will see an empty list and fail the whole process.

Lets move the VMA metadata into the shared memory, protected by the existing mutex. This enables both processes to see the full list.

Caveat is that at the moment we only allow for 128 total VMAs. Problem being is how to allocate a dynamicly size shared memory area at this stage of the restore process. Shmalloc() helper is out of reach, being already marked as private, and allocating anonymous shared memory appears to mess up the process state leading to crashes during restore.

---
 plugins/amdgpu/amdgpu_plugin.c | 41 +++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 13 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +28 -13
index f4bda11b3de5..83d16272237f 100644
@@ -50,7 +50,6 @@
 #include "util.h"
 
 struct vma_metadata {
-	struct list_head list;
 	uint64_t old_pgoff;
 	uint64_t new_pgoff;
 	uint64_t vma_entry;
@@ -59,8 +58,6 @@ struct vma_metadata {
 
 /************************************ Global Variables ********************************************/
 
-static LIST_HEAD(update_vma_info_list);
-
 size_t kfd_max_buffer_size;
 
 bool plugin_added_to_inventory = false;
@@ -76,6 +73,8 @@ static struct shared_memory {
 	mutex_t			mutex;
 	int 			num_handles;
 	struct handle_id	*handles;
+	int			num_vmas;
+	struct vma_metadata	vma[128];
 } *shared_memory;
 
 /*
@@ -1245,6 +1244,7 @@ int amdgpu_restore_init(void)
 	mutex_init(&shared_memory->mutex);
 
 	shared_memory->num_handles = num_handles;
+	shared_memory->num_vmas = 0;
 
 	return 0;
 }
@@ -1686,12 +1686,19 @@ int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset,
 		     int fd)
 {
 	struct vma_metadata *vma_md;
+	int ret = 0;
 
-	vma_md = xmalloc(sizeof(*vma_md));
-	if (!vma_md) {
-		return -ENOMEM;
+	if (!shared_memory)
+		return -ENXIO;
+
+	mutex_lock(&shared_memory->mutex);
+
+	if (shared_memory->num_vmas == ARRAY_SIZE(shared_memory->vma)) {
+		ret = -E2BIG;
+		goto out;
 	}
 
+	vma_md = &shared_memory->vma[shared_memory->num_vmas++];
 	vma_md->old_pgoff = offset;
 	vma_md->vma_entry = addr;
 	vma_md->new_pgoff = restored_offset;
@@ -1701,9 +1708,10 @@ int save_vma_updates(uint64_t offset, uint64_t addr, uint64_t restored_offset,
 		 vma_md->vma_entry, vma_md->old_pgoff, vma_md->new_pgoff,
 		 vma_md->fd);
 
-	list_add_tail(&vma_md->list, &update_vma_info_list);
+out:
+	mutex_unlock(&shared_memory->mutex);
 
-	return 0;
+	return ret;
 }
 
 static int restore_bo_data(int id, struct kfd_criu_bo_bucket *bo_buckets, CriuKfd *e)
@@ -2144,15 +2152,17 @@ CR_PLUGIN_REGISTER_HOOK(CR_PLUGIN_HOOK__RESTORE_EXT_FILE, amdgpu_plugin_restore_
 int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const uint64_t old_offset,
 				uint64_t *new_offset, int *updated_fd)
 {
-	struct vma_metadata *vma_md;
-	char path[PATH_MAX];
-	char *p_begin;
-	char *p_end;
 	bool is_kfd = false, is_renderD = false;
+	char *p_begin, *p_end;
+	char path[PATH_MAX];
+	int i;
 
 	if (plugin_disabled)
 		return -ENOTSUP;
 
+	if (!shared_memory)
+		return -ENXIO;
+
 	pr_debug("Enter %s\n", __func__);
 
 	strncpy(path, in_path, sizeof(path));
@@ -2182,7 +2192,10 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
 		return 0;
 	}
 
-	list_for_each_entry(vma_md, &update_vma_info_list, list) {
+	mutex_lock(&shared_memory->mutex);
+	for (i = 0; i < shared_memory->num_vmas; i++) {
+		struct vma_metadata *vma_md = &shared_memory->vma[i];
+
 		if (old_offset != vma_md->old_pgoff)
 			continue;
 		if (is_kfd && addr != vma_md->vma_entry)
@@ -2201,11 +2214,13 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const
 			*updated_fd = -1;
 		}
 
+		mutex_unlock(&shared_memory->mutex);
 		pr_debug("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n",
 			 vma_md->old_pgoff, vma_md->new_pgoff, *updated_fd);
 
 		return 1;
 	}
+	mutex_unlock(&shared_memory->mutex);
 	pr_info("No match for addr:0x%lx offset:%lx\n", addr, old_offset);
 	return 0;
 }
Signature · 1 line
2.52.0
#39 lore raw

RFC 25/26 plugins/amdgpu: Hack to save a fd number which works for vma restore

In reply to Tvrtko Ursulin

Another problem with VMA restore inside a forked process is that the file descriptor plugin creates to pass to CRIU core to mmap() against is present only in the process enumerating the VMAs. If there is a child process this fd will be invalid and the restore will fail.

In this patch we work around that by trying to guess the real (restored) file descriptor associated with this VMA and store that in the VMA metadata used for restore.

This works for a simple case when processes have identical set of file descriptor in a linear growing order, but probably not much more that that.

To fix this properly a deeper discussion on the design is required.

---
 plugins/amdgpu/amdgpu_plugin.c     | 17 +++++++++++++++--
 plugins/amdgpu/amdgpu_plugin_drm.c |  4 ++--
 plugins/amdgpu/amdgpu_plugin_drm.h |  2 +-
 3 files changed, 18 insertions(+), 5 deletions(-)
plugins/amdgpu/amdgpu_plugin.c +15 -2
index 83d16272237f..239eeebc5111 100644
@@ -1863,7 +1863,7 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
 	CriuRenderNode *rd;
 	unsigned char *buf;
 	size_t img_size;
-	int fd, ret;
+	int probed_fd, fd, ret;
 
 	/* This is restorer plugin for renderD nodes. Criu doesn't guarantee
 	 * that they will be called before the plugin is called for kfd file
@@ -1963,7 +1963,20 @@ static int amdgpu_plugin_restore_drm_file(int id, bool *retry_needed)
 		return -1;
 	}
 
-	ret = amdgpu_plugin_drm_restore_file(fd, rd);
+	/*
+	 * HACK! to figure out the final/restored fd we need to restore vmas.
+	 * It will not work if non-linear fd space, neither inside a forked
+	 * process which inherits the vma but does not have the same device
+	 * kept open or under the same fd.
+	 */
+	probed_fd = dup(fd);
+	if (probed_fd < 0) {
+		pr_perror("Failed to probe fd\n");
+		return -1;
+	}
+	close(probed_fd);
+
+	ret = amdgpu_plugin_drm_restore_file(fd, rd, probed_fd);
 	if (ret == 1)
 		*retry_needed = true;
 	if (ret < 0) {
plugins/amdgpu/amdgpu_plugin_drm.c +2 -2
index 30a0626b7b5f..6fb523e31720 100644
@@ -453,7 +453,7 @@ exit:
 	return ret;
 }
 
-int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
+int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd, int probed_fd)
 {
 	int ret = 0;
 	bool retry_needed = false;
@@ -551,7 +551,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
 		}
 
 		ret = save_vma_updates(boinfo->offset, boinfo->addr,
-				       mmap_args.out.addr_ptr, fd);
+				       mmap_args.out.addr_ptr, probed_fd);
 		if (ret < 0)
 			goto exit;
 	}
plugins/amdgpu/amdgpu_plugin_drm.h +1 -1
index f76e29f7f73f..7a05e01dcebc 100644
@@ -24,7 +24,7 @@ int amdgpu_plugin_drm_handle_device_vma(int fd, const struct stat *drm);
  */
 int amdgpu_plugin_drm_dump_file(int fd, int id, struct stat *drm);
 
-int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd);
+int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd, int probed_fd);
 
 int amdgpu_plugin_drm_unpause_file(int fd);
 
Signature · 1 line
2.52.0
#40 lore raw

RFC 26/26 plugins/amdgpu: Hack to work around GPU virtual address conflicts

In reply to Tvrtko Ursulin

During the restore process buffer objects are created and mapped into the GPU virtual address space to their final addresses, and then their content is attempted to be restored.

This step is done by instantiating a libdrm instance and the associated userspace virtual address range manager. This manager is unaware of the previous address range allocations on the same file, which in practice means restore will simply fail.

To work around it we add some retries to try different addresses a few times, but for a proper solution we will need to either add a mechanism for reserving ranges in libdrm va manager, or rework the order of the restore operations so that the buffer objects are only mapped into their final locations once their content has been restored.

---
 plugins/amdgpu/amdgpu_plugin.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
plugins/amdgpu/amdgpu_plugin.c +10 -0
index 239eeebc5111..b2f192a80f65 100644
@@ -573,6 +573,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
 	amdgpu_context_handle h_ctx;
 	uint32_t *ib = NULL;
 	int j, err, err2, packets_per_buffer;
+	unsigned int retries = 0;
 
 	buffer_bo_size = min(size, buffer_size);
 	packets_per_buffer = ((buffer_bo_size - 1) / max_copy_size) + 1;
@@ -603,6 +604,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
 		return -EINVAL;
 	}
 
+retry_src_va:
+	retries++;
 	err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, src_bo_size, 0x1000, 0, &gpu_addr_src,
 				    &h_va_src, 0);
 	if (err) {
@@ -611,6 +614,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
 	}
 	err = amdgpu_bo_va_op(h_bo_src, 0, src_bo_size, gpu_addr_src, 0, AMDGPU_VA_OP_MAP);
 	if (err) {
+		if (retries < 1000)
+			goto retry_src_va;
 		pr_perror("failed to GPU map the src BO");
 		goto err_src_bo_map;
 	}
@@ -639,6 +644,9 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
 		goto err_dst_bo_prep;
 	}
 
+	retries = 0;
+retry_dst_va:
+	retries++;
 	err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, dst_bo_size, 0x1000, 0, &gpu_addr_dst,
 				    &h_va_dst, 0);
 	if (err) {
@@ -647,6 +655,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
 	}
 	err = amdgpu_bo_va_op(h_bo_dst, 0, dst_bo_size, gpu_addr_dst, 0, AMDGPU_VA_OP_MAP);
 	if (err) {
+		if (retries < 1000)
+			goto retry_dst_va;
 		pr_perror("failed to GPU map the dest BO");
 		goto err_dst_bo_map;
 	}
Signature · 1 line
2.52.0
Reviewed-by on v1 26/26
#41 lore raw

Re: [RFC 26/26] plugins/amdgpu: Hack to work around GPU virtual address conflicts

Patches 22, 23, and 24 are
This patch is

Patches 21, 25, and 26 somewhat depend on what the future plan is for this feature. Full support of no-kfd amdgpu with CRIU is going to involve changes to the kernel interfaces that will replace the code added in these patches. If these are allowing real workloads to complete that would otherwise fail, go ahead. But if they are meant to be first drafts, I might want to wait until the work can be done properly.

David

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 26/26] plugins/amdgpu: Hack to work around GPU virtual address conflicts

During the restore process buffer objects are created and mapped into the GPU virtual address space to their final addresses, and then their content is attempted to be restored.

This step is done by instantiating a libdrm instance and the associated userspace virtual address range manager. This manager is unaware of the previous address range allocations on the same file, which in practice means restore will simply fail.

To work around it we add some retries to try different addresses a few times, but for a proper solution we will need to either add a mechanism for reserving ranges in libdrm va manager, or rework the order of the restore operations so that the buffer objects are only mapped into their final locations once their content has been restored.

---
 plugins/amdgpu/amdgpu_plugin.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
plugins/amdgpu/amdgpu_plugin.c +10 -0
index 239eeebc5111..b2f192a80f65 100644
@@ -573,6 +573,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
        amdgpu_context_handle h_ctx;
        uint32_t *ib = NULL;
        int j, err, err2, packets_per_buffer;
+       unsigned int retries = 0;
 
        buffer_bo_size = min(size, buffer_size);
        packets_per_buffer = ((buffer_bo_size - 1) / max_copy_size) + 1;
@@ -603,6 +604,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
                return -EINVAL;
        }
 
+retry_src_va:
+       retries++;
        err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, src_bo_size, 0x1000, 0, &gpu_addr_src,
                                    &h_va_src, 0);
        if (err) {
@@ -611,6 +614,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
        }
        err = amdgpu_bo_va_op(h_bo_src, 0, src_bo_size, gpu_addr_src, 0, AMDGPU_VA_OP_MAP);
        if (err) {
+               if (retries < 1000)
+                       goto retry_src_va;
                pr_perror("failed to GPU map the src BO");
                goto err_src_bo_map;
        }
@@ -639,6 +644,9 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
                goto err_dst_bo_prep;
        }
 
+       retries = 0;
+retry_dst_va:
+       retries++;
        err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, dst_bo_size, 0x1000, 0, &gpu_addr_dst,
                                    &h_va_dst, 0);
        if (err) {
@@ -647,6 +655,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
        }
        err = amdgpu_bo_va_op(h_bo_dst, 0, dst_bo_size, gpu_addr_dst, 0, AMDGPU_VA_OP_MAP);
        if (err) {
+               if (retries < 1000)
+                       goto retry_dst_va;
                pr_perror("failed to GPU map the dest BO");
                goto err_dst_bo_map;
        }

--
2.52.0

#42 lore raw

Re: [RFC 26/26] plugins/amdgpu: Hack to work around GPU virtual address conflicts

Quoted 9 lines · David

Patches 22, 23, and 24 are
This patch is

Patches 21, 25, and 26 somewhat depend on what the future plan is for this feature. Full support of no-kfd amdgpu with CRIU is going to involve changes to the kernel interfaces that will replace the code added in these patches. If these are allowing real workloads to complete that would otherwise fail, go ahead. But if they are meant to be first drafts, I might want to wait until the work can be done properly.

Yep, the tail of the series opens up many design questions. I am not suggesting to merge anything after 22, 23 the latest.

Some of the design questions, and you do not have to discuss them, I am only mentioning what needs solving to make this work:

Patch 21:
Probably acceptable on it's own, even without new kernel UAPIs, *if* we say "Lets allow users to force restore on a single GPU system". Because why not? I checkpoint on my system, I can restore on my system, and if things go bad so what. Probably gated behind a command line flag like --plugin-opt=amdgpu:force-single-gpu-restore.

Opens:

1. Does this sound acceptable?
2. Is there a way to pass plugin specific command line options?

Patches 23-25:

Patch 23 is not very useful on it's own. It's premise is that the 24 (the topic of how to reliably handle VMAs) can be converted from a hack to something better. But I am not sure if that is the case. 25 is similarly a hack which may be avoided by rewriting the VMA handling completely.

Opens:

1. Is there even a way to allocate shared memory at the point of CR_PLUGIN_HOOK__RESTORE_EXT_FILE? It cannot be done via shmalloc and manually via mmap crashes the process.

2. Even if 1. is possible, could it handle all scenarios, or perhaps we need to re-think VMA handling more extensively.

For examples keying on offsets is not good enough if process has multiple GPU devices open. Some sort of the device id + offset would be required instead.

Or should we save a more complete view of the VMAs during checkpoint time rather than building the metadata during restore.

I will focus on the above questions next.

Patch 26:

Also a hack. I need to check if the order of restore operations can be shuffled so that it wouldn't be needed. For example can we afford to not map the BO in GPU VA space until after the content has been restored. That would avoid sdma_copy_bo having GPU VA conflicts. All objects would only be mapped as the last step.

If the above is not possible we will need a way to initialize libdrm's VA manager with a list of reserved ranges.

In closing, once the patches up to 21 or so are reviewed I can re-send that portion with a minor update or two. But the rest were just hacks to demonstrate what does not work and I will continue working on those until I have something more robust.

Regards,

Tvrtko

Quoted 79 lines

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 26/26] plugins/amdgpu: Hack to work around GPU virtual address conflicts

During the restore process buffer objects are created and mapped into the GPU virtual address space to their final addresses, and then their content is attempted to be restored.

This step is done by instantiating a libdrm instance and the associated userspace virtual address range manager. This manager is unaware of the previous address range allocations on the same file, which in practice means restore will simply fail.

To work around it we add some retries to try different addresses a few times, but for a proper solution we will need to either add a mechanism for reserving ranges in libdrm va manager, or rework the order of the restore operations so that the buffer objects are only mapped into their final locations once their content has been restored.

---
  plugins/amdgpu/amdgpu_plugin.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
plugins/amdgpu/amdgpu_plugin.c +10 -1
index 239eeebc5111..b2f192a80f65 100644
@@ -573,6 +573,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
         amdgpu_context_handle h_ctx;
         uint32_t *ib = NULL;
         int j, err, err2, packets_per_buffer;
+       unsigned int retries = 0;
 
         buffer_bo_size = min(size, buffer_size);
         packets_per_buffer = ((buffer_bo_size - 1) / max_copy_size) + 1;
@@ -603,6 +604,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
                 return -EINVAL;
         }
 
+retry_src_va:
+       retries++;
         err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, src_bo_size, 0x1000, 0, &gpu_addr_src,
                                     &h_va_src, 0);
         if (err) {
@@ -611,6 +614,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
         }
         err = amdgpu_bo_va_op(h_bo_src, 0, src_bo_size, gpu_addr_src, 0, AMDGPU_VA_OP_MAP);
         if (err) {
+               if (retries < 1000)
+                       goto retry_src_va;
                 pr_perror("failed to GPU map the src BO");
                 goto err_src_bo_map;
         }
@@ -639,6 +644,9 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
                 goto err_dst_bo_prep;
         }
 
+       retries = 0;
+retry_dst_va:
+       retries++;
         err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, dst_bo_size, 0x1000, 0, &gpu_addr_dst,
                                     &h_va_dst, 0);
         if (err) {
@@ -647,6 +655,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
         }
         err = amdgpu_bo_va_op(h_bo_dst, 0, dst_bo_size, gpu_addr_dst, 0, AMDGPU_VA_OP_MAP);
         if (err) {
+               if (retries < 1000)
+                       goto retry_dst_va;
                 pr_perror("failed to GPU map the dest BO");
                 goto err_dst_bo_map;
         }
--

2.52.0

#43 lore raw

Re: [RFC 26/26] plugins/amdgpu: Hack to work around GPU virtual address conflicts

Quoted 14 lines

Yep, the tail of the series opens up many design questions. I am not suggesting to merge anything after 22, 23 the latest.

Some of the design questions, and you do not have to discuss them, I am only mentioning what needs solving to make this work:

Patch 21:
Probably acceptable on it's own, even without new kernel UAPIs, *if* we say "Lets allow users to force restore on a single GPU system". Because why not? I checkpoint on my system, I can restore on my system, and if things go bad so what. Probably gated behind a command line flag like --plugin-opt=amdgpu:force-single-gpu-restore.

Quoted 4 lines

Opens:

1. Does this sound acceptable?
2. Is there a way to pass plugin specific command line options?

Sounds acceptable to me. Plugin options are usually set as environment variables, see amdgpu_plugin_init. But best to gate it behind an option so we don't support restore in situations where later we have to declare a failure due to topology mismatch.

Quoted 8 lines

Patches 23-25:

Patch 23 is not very useful on it's own. It's premise is that the 24 (the topic of how to reliably handle VMAs) can be converted from a hack to something better. But I am not sure if that is the case. 25 is similarly a hack which may be avoided by rewriting the VMA handling completely.

Quoted 5 lines

Opens:

1. Is there even a way to allocate shared memory at the point of CR_PLUGIN_HOOK__RESTORE_EXT_FILE? It cannot be done via shmalloc and manually via mmap crashes the process.

xmalloc works fine, but otherwise yes for persistent memory we'd need to use amdgpu_restore_init

Quoted 21 lines

2. Even if 1. is possible, could it handle all scenarios, or perhaps we need to re-think VMA handling more extensively.

For examples keying on offsets is not good enough if process has multiple GPU devices open. Some sort of the device id + offset would be required instead.

Or should we save a more complete view of the VMAs during checkpoint time rather than building the metadata during restore.

I will focus on the above questions next.

Patch 26:

Also a hack. I need to check if the order of restore operations can be shuffled so that it wouldn't be needed. For example can we afford to not map the BO in GPU VA space until after the content has been restored. That would avoid sdma_copy_bo having GPU VA conflicts. All objects would only be mapped as the last step.

Quoted 7 lines

If the above is not possible we will need a way to initialize libdrm's VA manager with a list of reserved ranges.

In closing, once the patches up to 21 or so are reviewed I can re-send that portion with a minor update or two. But the rest were just hacks to demonstrate what does not work and I will continue working on those until I have something more robust.

Understood. Good to have them around for testing but not to push. Thanks.

David Francis

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Tuesday, March 3, 2026 8:50 AM
To: Francis, David; [email protected]
Subject: Re: [RFC 26/26] plugins/amdgpu: Hack to work around GPU virtual address conflicts

Quoted 9 lines · David

Patches 22, 23, and 24 are
This patch is

Patches 21, 25, and 26 somewhat depend on what the future plan is for this feature. Full support of no-kfd amdgpu with CRIU is going to involve changes to the kernel interfaces that will replace the code added in these patches. If these are allowing real workloads to complete that would otherwise fail, go ahead. But if they are meant to be first drafts, I might want to wait until the work can be done properly.

Yep, the tail of the series opens up many design questions. I am not suggesting to merge anything after 22, 23 the latest.

Some of the design questions, and you do not have to discuss them, I am only mentioning what needs solving to make this work:

Patch 21:
Probably acceptable on it's own, even without new kernel UAPIs, *if* we say "Lets allow users to force restore on a single GPU system". Because why not? I checkpoint on my system, I can restore on my system, and if things go bad so what. Probably gated behind a command line flag like --plugin-opt=amdgpu:force-single-gpu-restore.

Opens:

1. Does this sound acceptable?
2. Is there a way to pass plugin specific command line options?

Patches 23-25:

Patch 23 is not very useful on it's own. It's premise is that the 24 (the topic of how to reliably handle VMAs) can be converted from a hack to something better. But I am not sure if that is the case. 25 is similarly a hack which may be avoided by rewriting the VMA handling completely.

Opens:

1. Is there even a way to allocate shared memory at the point of CR_PLUGIN_HOOK__RESTORE_EXT_FILE? It cannot be done via shmalloc and manually via mmap crashes the process.

2. Even if 1. is possible, could it handle all scenarios, or perhaps we need to re-think VMA handling more extensively.

For examples keying on offsets is not good enough if process has multiple GPU devices open. Some sort of the device id + offset would be required instead.

Or should we save a more complete view of the VMAs during checkpoint time rather than building the metadata during restore.

I will focus on the above questions next.

Patch 26:

Also a hack. I need to check if the order of restore operations can be shuffled so that it wouldn't be needed. For example can we afford to not map the BO in GPU VA space until after the content has been restored. That would avoid sdma_copy_bo having GPU VA conflicts. All objects would only be mapped as the last step.

If the above is not possible we will need a way to initialize libdrm's VA manager with a list of reserved ranges.

In closing, once the patches up to 21 or so are reviewed I can re-send that portion with a minor update or two. But the rest were just hacks to demonstrate what does not work and I will continue working on those until I have something more robust.

Regards,

Tvrtko

Quoted 79 lines

________________________________________
From: Tvrtko Ursulin <[email protected]>
Sent: Friday, February 20, 2026 7:05 AM
To: [email protected]
Cc: Francis, David; Tvrtko Ursulin
Subject: [RFC 26/26] plugins/amdgpu: Hack to work around GPU virtual address conflicts

During the restore process buffer objects are created and mapped into the GPU virtual address space to their final addresses, and then their content is attempted to be restored.

This step is done by instantiating a libdrm instance and the associated userspace virtual address range manager. This manager is unaware of the previous address range allocations on the same file, which in practice means restore will simply fail.

To work around it we add some retries to try different addresses a few times, but for a proper solution we will need to either add a mechanism for reserving ranges in libdrm va manager, or rework the order of the restore operations so that the buffer objects are only mapped into their final locations once their content has been restored.

---
  plugins/amdgpu/amdgpu_plugin.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
plugins/amdgpu/amdgpu_plugin.c +10 -1
index 239eeebc5111..b2f192a80f65 100644
@@ -573,6 +573,7 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
         amdgpu_context_handle h_ctx;
         uint32_t *ib = NULL;
         int j, err, err2, packets_per_buffer;
+       unsigned int retries = 0;
 
         buffer_bo_size = min(size, buffer_size);
         packets_per_buffer = ((buffer_bo_size - 1) / max_copy_size) + 1;
@@ -603,6 +604,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
                 return -EINVAL;
         }
 
+retry_src_va:
+       retries++;
         err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, src_bo_size, 0x1000, 0, &gpu_addr_src,
                                     &h_va_src, 0);
         if (err) {
@@ -611,6 +614,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
         }
         err = amdgpu_bo_va_op(h_bo_src, 0, src_bo_size, gpu_addr_src, 0, AMDGPU_VA_OP_MAP);
         if (err) {
+               if (retries < 1000)
+                       goto retry_src_va;
                 pr_perror("failed to GPU map the src BO");
                 goto err_src_bo_map;
         }
@@ -639,6 +644,9 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
                 goto err_dst_bo_prep;
         }
 
+       retries = 0;
+retry_dst_va:
+       retries++;
         err = amdgpu_va_range_alloc(h_dev, amdgpu_gpu_va_range_general, dst_bo_size, 0x1000, 0, &gpu_addr_dst,
                                     &h_va_dst, 0);
         if (err) {
@@ -647,6 +655,8 @@ int sdma_copy_bo(int shared_fd, uint64_t size, int storage_fd,
         }
         err = amdgpu_bo_va_op(h_bo_dst, 0, dst_bo_size, gpu_addr_dst, 0, AMDGPU_VA_OP_MAP);
         if (err) {
+               if (retries < 1000)
+                       goto retry_dst_va;
                 pr_perror("failed to GPU map the dest BO");
                 goto err_dst_bo_map;
         }
--

2.52.0

#44 lore raw
In reply to Tvrtko Ursulin

David, could you or someone else form your team please review this series? All changes are just in the amd plugin, so I am ready to take them after your review.

Thanks,
Andrei

Quoted 116 lines · Tvrtko Ursulin

Hi all,

Long story short, I am trying to make some inroads towards having a working checkpoint and restore for render node only applications (ie. no KFD).

Plan is to write test scenarios in an increasing order of complexity and make them work. Those tests can be found here:

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

For those unfamiliar, once built the list of subtest can be obtained like this:

$ amd_criu --l
open
one-bo
map-one
mmap-one
map-and-mmap-one
map-mmap-fork

Each test will pause after the setup phase waiting for a return key. This is the point when they can be checkpointed and restored. The tests are in the order of how I was fixing issues one by one, give or take some details.

Without this series nothing works, while with it, they all pass (last one is the superset of all previous ones, or the most complex test):

$ amd_criu --r map-mmap-fork
...
$ 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
$ 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 map-mmap-fork: SUCCESS (6.310s)

However trivial the tests may currently be, the combination of them and this series is supposed to be a starting point for a design discussion since it appears there are many issues to address.

Having said that, this series actually starts with a fair number of cleanup patches which can be reviewed and merged independently. Or I can easily send them as a standalone pull request if that would work better.

Only the last six patches, plus "plugins/amdgpu: Add plugin to inventory even if process has no vmas", are the ones with fixes and hacks which make the tests pass.

In no particular order the main issues I currently see are:

* GPU discovery with no KFD open or present
* Rendernode VMA handling
* VMA handling with forked processes
* GPU VA conflicts during restore

Some of these may require changes to CRIU plugin parameters and input from CRIU experts.

More details on each of these topics can be found in the respective patch commit messages. I will also gladly expand on any.

For now I wanted to test the waters first by sending this early and see who is interested and to what extent. This is far from a working render node support but it illustrates that such basic scenarios currently do not work, that I think those basics need to be fixed first before proceeding with anything more complex.

Also, nothing in this series is supposed to negatively influence the KFD paths, but with a disclaimer that I did not test that side at all. Suggestions on a suitable KFD test program would be welcomed.

Tvrtko

Tvrtko Ursulin (26):
plugins/amgdpu: Fix one error message
plugins/amdgpu: Remove unused current_pid global variable plugins/amdgpu: Remove unused new_minor from struct vma_metadata plugins/amdgpu: Fix drm pages size header
plugins/amdgpu: Fix logging of failures to open files during restore
init
plugins/amdgpu: Propagate failure to save buffer object content plugins/amdgpu: Close the directory when image probing fails plugins/amdgpu: Close dma-buf image file if the read fails plugins/amdgpu: Check images were opened successfully plugins/amdgpu: Flatten amdgpu_restore_init a bit plugins/amdgpu: Fix error handling in amdgpu_plugin_drm_restore_file() plugins/amdgpu: Add error handling for seek operations plugins/amdgpu: Consolidate vm_info collection plugins/amdgpu: Remove plugin_log_msg()
plugins/amdgpu: Add plugin to inventory even if process has no vmas plugins/amdgpu: Move drm file dump and restore into helpers plugins/amdgpu: Use the load_img helper in drm file restore plugins/amdgpu: Convert away from libc buffered file IO plugins/amdgpu: Use save_vma_updates for all call sites plugins/amdgpu: amdgpu_plugin_drm_restore_file() does not need to use
libdrm
plugins/amdgpu: Enable restore with only the drm node open plugins/amdgpu: Handle DRM render node mmaps plugins/amdgpu: Consolidate shared memory handling plugins/amdgpu: Store VMA data in shared memory plugins/amdgpu: Hack to save a fd number which works for vma restore plugins/amdgpu: Hack to work around GPU virtual address conflicts

 plugins/amdgpu/amdgpu_plugin.c          | 856 ++++++++++++++----------
 plugins/amdgpu/amdgpu_plugin_dmabuf.c   |  17 +-
 plugins/amdgpu/amdgpu_plugin_drm.c      | 125 ++--
 plugins/amdgpu/amdgpu_plugin_drm.h      |   5 +-
 plugins/amdgpu/amdgpu_plugin_topology.c |   2 +
 plugins/amdgpu/amdgpu_plugin_topology.h |   2 +
 plugins/amdgpu/amdgpu_plugin_util.c     | 106 +--
 plugins/amdgpu/amdgpu_plugin_util.h     |  17 +-
 8 files changed, 636 insertions(+), 494 deletions(-)

--
2.52.0