Skip to content

Commit 11f0917

Browse files
fdavid-amdrst0git
authored andcommitted
plugin/amdgpu: Support for checkpoint of dmabuf fds
amdgpu libraries that use dmabuf fd to share GPU memory between processes close the dmabuf fds immediately after using them. However, it is possible that checkpoint of a process catches one of the dmabuf fds open. In that case, the amdgpu plugin needs to handle it. The checkpoint of the dmabuf fd does require the device file it was exported from to have already been dumped To identify which device this dmabuf fd was exprted from, attempt to import it on each device, then record the dmabuf handle it imports as. This handle can be used to restore it. Signed-off-by: David Francis <David.Francis@amd.com>
1 parent a93a334 commit 11f0917

File tree

8 files changed

+306
-20
lines changed

8 files changed

+306
-20
lines changed

plugins/amdgpu/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ endif
2727
criu-amdgpu.pb-c.c: criu-amdgpu.proto
2828
protoc --proto_path=. --c_out=. criu-amdgpu.proto
2929

30-
amdgpu_plugin.so: amdgpu_plugin.c amdgpu_plugin_drm.c amdgpu_plugin_topology.c amdgpu_plugin_util.c criu-amdgpu.pb-c.c amdgpu_socket_utils.c
30+
amdgpu_plugin.so: amdgpu_plugin.c amdgpu_plugin_drm.c amdgpu_plugin_dmabuf.c amdgpu_plugin_topology.c amdgpu_plugin_util.c criu-amdgpu.pb-c.c amdgpu_socket_utils.c
3131
$(CC) $(PLUGIN_CFLAGS) $(shell $(COMPEL) includes) $^ -o $@ $(PLUGIN_INCLUDE) $(PLUGIN_LDFLAGS) $(LIBDRM_INC)
3232

3333
amdgpu_plugin_clean:

plugins/amdgpu/amdgpu_plugin.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "rst-malloc.h"
3939

4040
#include "common/list.h"
41+
#include "amdgpu_plugin_dmabuf.h"
4142
#include "amdgpu_plugin_drm.h"
4243
#include "amdgpu_plugin_util.h"
4344
#include "amdgpu_plugin_topology.h"
@@ -46,7 +47,7 @@
4647
#include "img-streamer.h"
4748
#include "image.h"
4849
#include "cr_options.h"
49-
50+
#include "util.h"
5051
struct vma_metadata {
5152
struct list_head list;
5253
uint64_t old_pgoff;
@@ -1064,6 +1065,9 @@ int amdgpu_unpause_processes(int pid)
10641065
}
10651066
}
10661067

1068+
if (post_dump_dmabuf_check() < 0)
1069+
ret = -1;
1070+
10671071
exit:
10681072
pr_info("Process unpaused %s (ret:%d)\n", ret ? "Failed" : "Ok", ret);
10691073
clear_dumped_fds();
@@ -1400,7 +1404,17 @@ int amdgpu_plugin_dump_file(int fd, int id)
14001404
return -1;
14011405
}
14021406

1403-
/* Check whether this plugin was called for kfd or render nodes */
1407+
/* Check whether this plugin was called for kfd, dmabuf or render nodes */
1408+
ret = get_dmabuf_info(fd, &st);
1409+
if (ret < 0) {
1410+
pr_perror("Failed to get dmabuf info");
1411+
return -1;
1412+
} else if (ret == 0) {
1413+
pr_info("Dumping dmabuf fd = %d\n", fd);
1414+
ret = amdgpu_plugin_dmabuf_dump(fd, id);
1415+
return ret;
1416+
}
1417+
14041418
if (major(st.st_rdev) != major(st_kfd.st_rdev) || minor(st.st_rdev) != 0) {
14051419

14061420
/* This is RenderD dumper plugin, for now just save renderD
@@ -1414,7 +1428,7 @@ int amdgpu_plugin_dump_file(int fd, int id)
14141428
ret = record_dumped_fd(fd, true);
14151429
if (ret)
14161430
return ret;
1417-
1431+
ret = try_dump_dmabuf_list();
14181432
/* Need to return success here so that criu can call plugins for renderD nodes */
14191433
return ret;
14201434
}
@@ -1538,7 +1552,6 @@ static int restore_devices(struct kfd_ioctl_criu_args *args, CriuKfd *e)
15381552
int ret = 0, bucket_index = 0;
15391553

15401554
pr_debug("Restoring %d devices\n", e->num_of_gpus);
1541-
15421555
args->num_devices = e->num_of_gpus;
15431556
device_buckets = xzalloc(sizeof(*device_buckets) * args->num_devices);
15441557
if (!device_buckets)
@@ -1822,12 +1835,17 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
18221835
* first as we assume restore_maps is already filled. Need to fix this later.
18231836
*/
18241837
snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
1825-
pr_info("Restoring RenderD %s\n", img_path);
18261838

18271839
img_fp = open_img_file(img_path, false, &img_size);
1828-
if (!img_fp)
1829-
return -EINVAL;
1830-
1840+
if (!img_fp) {
1841+
ret = amdgpu_plugin_dmabuf_restore(id);
1842+
if (ret == 1) {
1843+
*retry_needed = true;
1844+
return 0;
1845+
}
1846+
return ret;
1847+
}
1848+
pr_info("Restoring RenderD %s\n", img_path);
18311849
pr_debug("RenderD Image file size:%ld\n", img_size);
18321850
buf = xmalloc(img_size);
18331851
if (!buf) {
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
#include <errno.h>
2+
#include <fcntl.h>
3+
#include <stdlib.h>
4+
#include <stdint.h>
5+
#include <stdbool.h>
6+
#include <string.h>
7+
#include <stdio.h>
8+
#include <unistd.h>
9+
10+
#include <sys/stat.h>
11+
#include <sys/mman.h>
12+
#include <sys/types.h>
13+
#include <linux/limits.h>
14+
15+
#include "common/list.h"
16+
#include "criu-amdgpu.pb-c.h"
17+
18+
#include "xmalloc.h"
19+
#include "criu-log.h"
20+
#include "amdgpu_plugin_drm.h"
21+
#include "amdgpu_plugin_util.h"
22+
#include "amdgpu_plugin_dmabuf.h"
23+
#include "fdstore.h"
24+
25+
#include "util.h"
26+
#include "common/scm.h"
27+
28+
struct dmabuf {
29+
int id;
30+
int dmabuf_fd;
31+
struct list_head node;
32+
};
33+
34+
static LIST_HEAD(dmabuf_list);
35+
36+
/* Return < 0 for error, > 0 for "not a dmabuf" and 0 "is a dmabuf" */
37+
int get_dmabuf_info(int fd, struct stat *st)
38+
{
39+
char path[PATH_MAX];
40+
41+
if (read_fd_link(fd, path, sizeof(path)) < 0)
42+
return -1;
43+
44+
if (strncmp(path, DMABUF_LINK, strlen(DMABUF_LINK)) != 0)
45+
return 1;
46+
47+
return 0;
48+
}
49+
50+
int __amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
51+
{
52+
int ret = 0;
53+
char path[PATH_MAX];
54+
size_t len = 0;
55+
unsigned char *buf = NULL;
56+
int gem_handle;
57+
58+
pr_info("TWI: Dumping dmabuf fd = %d\n", dmabuf_fd);
59+
60+
gem_handle = handle_for_shared_bo_fd(dmabuf_fd);
61+
if (gem_handle < 0) {
62+
pr_err("Failed to get handle for dmabuf_fd = %d\n", dmabuf_fd);
63+
return -EAGAIN; /* Retry needed */
64+
}
65+
66+
CriuDmabufNode *node = xmalloc(sizeof(*node));
67+
if (!node) {
68+
pr_err("Failed to allocate memory for dmabuf node\n");
69+
return -ENOMEM;
70+
}
71+
criu_dmabuf_node__init(node);
72+
73+
node->gem_handle = gem_handle;
74+
75+
if (node->gem_handle < 0) {
76+
pr_err("Failed to get handle for dmabuf_fd\n");
77+
xfree(node);
78+
return -EINVAL;
79+
}
80+
81+
/* Serialize metadata to a file */
82+
snprintf(path, sizeof(path), IMG_DMABUF_FILE, id);
83+
len = criu_dmabuf_node__get_packed_size(node);
84+
buf = xmalloc(len);
85+
if (!buf) {
86+
pr_err("Failed to allocate buffer for dmabuf metadata\n");
87+
xfree(node);
88+
return -ENOMEM;
89+
}
90+
criu_dmabuf_node__pack(node, buf);
91+
ret = write_img_file(path, buf, len);
92+
93+
xfree(buf);
94+
xfree(node);
95+
return ret;
96+
}
97+
98+
int amdgpu_plugin_dmabuf_restore(int id)
99+
{
100+
char path[PATH_MAX];
101+
size_t img_size;
102+
FILE *img_fp = NULL;
103+
int ret = 0;
104+
CriuDmabufNode *rd = NULL;
105+
unsigned char *buf = NULL;
106+
int fd_id;
107+
108+
snprintf(path, sizeof(path), IMG_DMABUF_FILE, id);
109+
110+
pr_info("TWI: Restoring dmabuf fd, id = %d\n", id);
111+
112+
/* Read serialized metadata */
113+
img_fp = open_img_file(path, false, &img_size);
114+
if (!img_fp) {
115+
pr_err("Failed to open dmabuf metadata file: %s\n", path);
116+
return -EINVAL;
117+
}
118+
119+
pr_debug("dmabuf Image file size:%ld\n", img_size);
120+
buf = xmalloc(img_size);
121+
if (!buf) {
122+
pr_perror("Failed to allocate memory");
123+
return -ENOMEM;
124+
}
125+
126+
ret = read_fp(img_fp, buf, img_size);
127+
if (ret) {
128+
pr_perror("Unable to read from %s", path);
129+
xfree(buf);
130+
return ret;
131+
}
132+
133+
rd = criu_dmabuf_node__unpack(NULL, img_size, buf);
134+
if (rd == NULL) {
135+
pr_perror("Unable to parse the dmabuf message %d", id);
136+
xfree(buf);
137+
fclose(img_fp);
138+
return -1;
139+
}
140+
fclose(img_fp);
141+
142+
pr_info("TWI: dmabuf node gem_handle = %d\n", rd->gem_handle);
143+
144+
/* Match GEM handle with shared_dmabuf list */
145+
fd_id = amdgpu_id_for_handle(rd->gem_handle);
146+
if (fd_id == -1) {
147+
pr_err("Failed to find dmabuf_fd for GEM handle = %d\n",
148+
rd->gem_handle);
149+
return 1;
150+
}
151+
int dmabuf_fd = fdstore_get(fd_id);
152+
pr_info("TWI: dmabuf node fd_id = %d, dmabuf_fd = %d\n", fd_id, dmabuf_fd);
153+
if (dmabuf_fd == -1) {
154+
pr_err("Failed to find dmabuf_fd for GEM handle = %d\n",
155+
rd->gem_handle);
156+
return 1; /* Retry needed */
157+
} else {
158+
pr_info("Restored dmabuf_fd = %d for GEM handle = %d\n",
159+
dmabuf_fd, rd->gem_handle);
160+
}
161+
ret = dmabuf_fd;
162+
163+
pr_info("Successfully restored dmabuf_fd %d\n",
164+
dmabuf_fd);
165+
criu_dmabuf_node__free_unpacked(rd, NULL);
166+
xfree(buf);
167+
return ret;
168+
}
169+
170+
int amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
171+
{
172+
int ret;
173+
174+
ret = __amdgpu_plugin_dmabuf_dump(dmabuf_fd, id);
175+
if (ret == -EAGAIN) {
176+
struct dmabuf *b = xmalloc(sizeof(*b));
177+
b->id = id;
178+
b->dmabuf_fd = dmabuf_fd;
179+
list_add(&b->node, &dmabuf_list);
180+
return 0;
181+
}
182+
return ret;
183+
}
184+
185+
int try_dump_dmabuf_list()
186+
{
187+
struct dmabuf *b, *t;
188+
list_for_each_entry_safe(b, t, &dmabuf_list, node) {
189+
int ret = __amdgpu_plugin_dmabuf_dump(b->dmabuf_fd, b->id);
190+
if (ret == -EAGAIN)
191+
continue;
192+
else if (ret)
193+
return ret;
194+
list_del(&b->node);
195+
xfree(b);
196+
}
197+
return 0;
198+
}
199+
200+
int post_dump_dmabuf_check()
201+
{
202+
if (!list_empty(&dmabuf_list)) {
203+
pr_err("Not all dma buffers have been dumped\n");
204+
return -1;
205+
}
206+
return 1;
207+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#ifndef __AMDGPU_PLUGIN_DMABUF_H__
3+
#define __AMDGPU_PLUGIN_DMABUF_H__
4+
5+
#include "amdgpu_plugin_util.h"
6+
#include "criu-amdgpu.pb-c.h"
7+
8+
int amdgpu_plugin_dmabuf_dump(int fd, int id);
9+
int amdgpu_plugin_dmabuf_restore(int id);
10+
11+
int try_dump_dmabuf_list();
12+
int post_dump_dmabuf_check();
13+
14+
int get_dmabuf_info(int fd, struct stat *st);
15+
16+
#endif /* __AMDGPU_PLUGIN_DMABUF_H__ */

plugins/amdgpu/amdgpu_plugin_drm.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ int get_gem_handle(amdgpu_device_handle h_dev, int dmabuf_fd)
4747
return -1;
4848
}
4949

50-
drmPrimeFDToHandle(fd, dmabuf_fd, &handle);
50+
if (drmPrimeFDToHandle(fd, dmabuf_fd, &handle))
51+
return -1;
5152

5253
return handle;
5354
}
@@ -465,18 +466,21 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
465466
if (work_already_completed(boinfo->handle, rd->drm_render_minor)) {
466467
continue;
467468
} else if (boinfo->handle != -1) {
469+
pr_info("TWI: restore bo %d\n", boinfo->handle);
468470
if (boinfo->is_import) {
469471
fd_id = amdgpu_id_for_handle(boinfo->handle);
470472
if (fd_id == -1) {
471473
retry_needed = true;
472474
continue;
473475
}
474476
dmabuf_fd = fdstore_get(fd_id);
477+
pr_info("TWI: restore bo %d: fd_id %d, dmabuf_fd %d\n", boinfo->handle, fd_id, dmabuf_fd);
475478
}
476479
}
477480

478481
if (boinfo->is_import) {
479482
drmPrimeFDToHandle(device_fd, dmabuf_fd, &handle);
483+
pr_info("TWI: restore bo imported to handle %d\n", handle);
480484
} else {
481485
union drm_amdgpu_gem_create create_args = { 0 };
482486

@@ -493,6 +497,7 @@ int amdgpu_plugin_drm_restore_file(int fd, CriuRenderNode *rd)
493497
handle = create_args.out.handle;
494498

495499
drmPrimeHandleToFD(device_fd, handle, 0, &dmabuf_fd);
500+
pr_info("TWI: restore bo created at handle %d and exported to fd %d\n", handle, dmabuf_fd);
496501
}
497502

498503
change_args.handle = handle;

0 commit comments

Comments
 (0)