From 69a2f13c9e2628aea9e04827abe8f4b620324da7 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Thu, 28 Sep 2023 21:38:45 -0700 Subject: [PATCH 1/3] plugins: the UPDATE_VMA_MAP callback returns fd with the full control It means CRIU has to close it when it is not needed. It looks more logically correct and matches the behaviour of the RESTORE_EXT_FILE callback. Signed-off-by: Andrei Vagin --- criu/files-reg.c | 2 +- plugins/amdgpu/amdgpu_plugin.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/criu/files-reg.c b/criu/files-reg.c index cf0c84b52e..c80da1d8ce 100644 --- a/criu/files-reg.c +++ b/criu/files-reg.c @@ -2506,7 +2506,7 @@ static int open_filemap(int pid, struct vma_area *vma) * using dup because dup returns a reference to the same struct file inside kernel, but we * cannot open a new FD. */ - ret = dup(plugin_fd); + ret = plugin_fd; } else if (vma->e->status & VMA_AREA_MEMFD) { if (!inherited_fd(vma->vmfd, &ret)) ret = memfd_open(vma->vmfd, &flags); diff --git a/plugins/amdgpu/amdgpu_plugin.c b/plugins/amdgpu/amdgpu_plugin.c index 6a79f8b19d..9dae8861cb 100644 --- a/plugins/amdgpu/amdgpu_plugin.c +++ b/plugins/amdgpu/amdgpu_plugin.c @@ -1955,10 +1955,15 @@ int amdgpu_plugin_update_vmamap(const char *in_path, const uint64_t addr, const if (addr == vma_md->vma_entry && old_offset == vma_md->old_pgoff) { *new_offset = vma_md->new_pgoff; - if (is_renderD) - *updated_fd = vma_md->fd; - else - *updated_fd = -1; + *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; + } plugin_log_msg("old_pgoff=0x%lx new_pgoff=0x%lx fd=%d\n", vma_md->old_pgoff, vma_md->new_pgoff, *updated_fd); From 506a944e753c43c0c4624a8ea1472e4255b1d93a Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Thu, 28 Sep 2023 21:47:50 -0700 Subject: [PATCH 2/3] amdgpu: don't leak fd on an error path in open_img_file Signed-off-by: Andrei Vagin --- plugins/amdgpu/amdgpu_plugin.c | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/amdgpu/amdgpu_plugin.c b/plugins/amdgpu/amdgpu_plugin.c index 9dae8861cb..e22168d931 100644 --- a/plugins/amdgpu/amdgpu_plugin.c +++ b/plugins/amdgpu/amdgpu_plugin.c @@ -165,6 +165,7 @@ FILE *open_img_file(char *path, bool write, size_t *size) fp = fdopen(fd, write ? "w" : "r"); if (!fp) { pr_perror("%s: Failed get pointer for %s", path, write ? "write" : "read"); + close(fd); return NULL; } From cd3b4de7c2866f3739b575e97bcd4034791791e4 Mon Sep 17 00:00:00 2001 From: Andrei Vagin Date: Thu, 28 Sep 2023 21:49:39 -0700 Subject: [PATCH 3/3] amdgpu: print an error if the dup syscall fails Signed-off-by: Andrei Vagin --- plugins/amdgpu/amdgpu_plugin.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugins/amdgpu/amdgpu_plugin.c b/plugins/amdgpu/amdgpu_plugin.c index e22168d931..2ebc5e1786 100644 --- a/plugins/amdgpu/amdgpu_plugin.c +++ b/plugins/amdgpu/amdgpu_plugin.c @@ -1796,7 +1796,12 @@ int amdgpu_plugin_restore_file(int id) * copy of the fd. CRIU core owns the duplicated returned fd, and amdgpu_plugin owns the fd stored in * tp_node. */ - return dup(fd); + fd = dup(fd); + if (fd == -1) { + pr_perror("unable to duplicate the render fd"); + return -1; + } + return fd; } fd = open(AMDGPU_KFD_DEVICE, O_RDWR | O_CLOEXEC);