Received: from fanzine2.igalia.com (fanzine2.igalia.com [213.97.179.56]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D018C330B3A for ; Fri, 20 Feb 2026 12:05:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=213.97.179.56 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771589136; cv=none; b=uWYNBJBea5sx2JG2agv9fTh0rOn5TyIKOuYOSV+E4j2tOr8YynXRqBnjz0fdsPWfongU1x23/JDSoxFv9PsXBJE19/s4G3j1pB66aQ17VXTeyzR47SzDzLmbk0ELORFjn4wRnP7sHuf27/7tDQHw70YOrNt20YC0TFOsw/y499E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771589136; c=relaxed/simple; bh=KP1XUpOgxLLJA48sQXWXWt68EqinPuE56tuzbx9v3Co=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o1+lOW3bFgAL6ho1Fya1W63VUdUcRZ4opfX/igCDKcDlPnchCF2HIb2FEKRjdHOLzvK3NuBVSv/FiJ9aI+FZORFPSUP+t09Aljy6Z2QlSmvNdKry1ezFq7PkA0b2Bn5jAGKtlvFk3a1jwgx6ZkBamzRTPLg4h8Y6OSrP7nURcQo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=q98d+4bt; arc=none smtp.client-ip=213.97.179.56 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="q98d+4bt" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=UOyc6cMc3uoRuPfZUz2KLBMVznJPVYLR565IQ8xYjOE=; b=q98d+4btKXfcwp+2r6HrHMRPIj 0SHRRVYMN8PV0hLApz0SQBwkl4TeYqXrvO9ZMTLgmeC8CoVDJXz6LBuXPZ1XmgbwM5ElXYy5KuWDY eCaNBURMlGe+WCpomNSiklFfKfkNAT+oWdS83RBIhncXnyCOnMGKh1uT87t0yKt6UJnzrVQddMHXY 6hMYBx4HlfiMOrhnG2+vESCIArwPQE0MLy17MasS90q8B7xaTN6OUiM4Qr07AndpQhw4lSmREJK+L HDk0l119jTxYpwVLtbDs+qWD8NhB5nl3uWUx8cS6yat+Ff37eibeATc0RKCjLRO6i11OlqNbrGzTQ g0FfRbAg==; Received: from [90.240.106.137] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1vtPGG-002uz9-AY; Fri, 20 Feb 2026 13:05:32 +0100 From: Tvrtko Ursulin To: criu@lists.linux.dev Cc: David Francis , Tvrtko Ursulin Subject: [RFC 10/26] plugins/amdgpu: Flatten amdgpu_restore_init a bit Date: Fri, 20 Feb 2026 12:04:58 +0000 Message-ID: <20260220120514.4540-11-tvrtko.ursulin@igalia.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260220120514.4540-1-tvrtko.ursulin@igalia.com> References: <20260220120514.4540-1-tvrtko.ursulin@igalia.com> Precedence: bulk X-Mailing-List: criu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Shallower indentation makes a heavily indented function hopefully a bit more readable. Signed-off-by: Tvrtko Ursulin --- 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 dcc8940e735b..1b531f351c55 100644 --- a/plugins/amdgpu/amdgpu_plugin.c +++ b/plugins/amdgpu/amdgpu_plugin.c @@ -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; -- 2.52.0