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 7930B330B3A for ; Fri, 20 Feb 2026 12:05:43 +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=1771589144; cv=none; b=PwvwDko4tODiLxYCrVuHMhDX7RdNStWRWs12ejYnxQ7uKYf6KiYD6K98exlqrTchsir8p//VqnEF+6vouEAslfGKqJ0WZZn1qyCG8RV0/x692Kvl/vjCn4DIr2qlsBU9NFFvuhii1SKlcf8D/SiiHMMNezAFH2nu9sNn590VZxw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771589144; c=relaxed/simple; bh=WGHPZxyP3khnwuXmvwt+nnyRnk76AvVXEfGAEUF21RM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lzLAXNdO88htRPcIteJm6k+k4QHzBFWQflqVAP/3Qkg+cZno9YponvX4ph2iGRcNI4uAErsWebGi4HvN74g7F4wM/FSL/2QspUByoGl7KQBoMZMXXX0MwosAccCvp2hjHe0mhiVRWMn7C6LfhGGhUJA1wUlmEpdkhZrkdNahAnQ= 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=Oy2VeSq7; 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="Oy2VeSq7" 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=fABE0PBl3DztYDHwO1RcSJ5GzwqbKSIFaiw0UnlyXEs=; b=Oy2VeSq7ctYFNRgu+B/zXiUV0o I9SVheEgnfKt3v7Fu/d2wzcpS+/qDOviUdWZjQnCb+iumeI2p4W4k7Oj77xoppJEpTEYcSt+cX3Qp bPEnG47p2eNDD8Mbdd/cNL5Hml+4iUpYV3NdcjZTX8HtsNWTVj64wjKaeT8ArOpvAXFUEnJnSlaR8 00xkuUueLYFit3BpqhyDKw5dN5ukSva3pSeLIFYEEv8MFXMJJJY3mhMwcNSZQq4RpEzLpyTR6f8PC QHjqKrPjsdb4CqQbGA7YlkzDO7BhVrtzx5fHi9ivy8yDH3xp00MiviSveQrmj2nUMGFwP1AR8Ia+u Vs201BgQ==; 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 1vtPGP-002v1W-JQ; Fri, 20 Feb 2026 13:05:41 +0100 From: Tvrtko Ursulin To: criu@lists.linux.dev Cc: David Francis , Tvrtko Ursulin Subject: [RFC 23/26] plugins/amdgpu: Consolidate shared memory handling Date: Fri, 20 Feb 2026 12:05:11 +0000 Message-ID: <20260220120514.4540-24-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 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. Signed-off-by: Tvrtko Ursulin --- plugins/amdgpu/amdgpu_plugin.c | 85 ++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/plugins/amdgpu/amdgpu_plugin.c b/plugins/amdgpu/amdgpu_plugin.c index 664536850c42..f4bda11b3de5 100644 --- a/plugins/amdgpu/amdgpu_plugin.c +++ b/plugins/amdgpu/amdgpu_plugin.c @@ -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) -- 2.52.0