[PATCH v2 09/23] plugins/amdgpu: Flatten amdgpu_restore_init a bit

Tvrtko Ursulin <[email protected]> Fri, 10 Apr 2026 19:55:00 +0100
Newsgroups dev.linux.lists.criu
Message-ID <[email protected]>
Shallower indentation makes a heavily indented function hopefully a bit
more readable.

Signed-off-by: Tvrtko Ursulin <[email protected]>
Reviewed-By: David Francis <[email protected]>
---
 plugins/amdgpu/amdgpu_plugin.c | 115 +++++++++++++++++----------------
 1 file changed, 60 insertions(+), 55 deletions(-)

diff --git a/plugins/amdgpu/amdgpu_plugin.c b/plugins/amdgpu/amdgpu_plugin.c
index 1d95c04d1dbd..821881149d6c 100644
--- a/plugins/amdgpu/amdgpu_plugin.c
+++ b/plugins/amdgpu/amdgpu_plugin.c
@@ -1159,67 +1159,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;
-- 
2.52.0