Skip to content

Commit 2097325

Browse files
committed
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 f5a9936 commit 2097325

File tree

8 files changed

+296
-22
lines changed

8 files changed

+296
-22
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: 30 additions & 10 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();
@@ -1129,7 +1133,6 @@ int amdgpu_restore_init(void)
11291133
if (d) {
11301134
while ((dir = readdir(d)) != NULL) {
11311135
if (strncmp("amdgpu-kfd-", dir->d_name, strlen("amdgpu-kfd-")) == 0) {
1132-
pr_info("CC3: Found kfd file\n");
11331136
img_fp = open_img_file(dir->d_name, false, &img_size);
11341137
buf = xmalloc(img_size);
11351138
if (!buf) {
@@ -1152,7 +1155,6 @@ int amdgpu_restore_init(void)
11521155
xfree(buf);
11531156
}
11541157
if (strncmp("amdgpu-renderD-", dir->d_name, strlen("amdgpu-renderD-")) == 0) {
1155-
pr_info("CC3: Found drm file\n");
11561158
img_fp = open_img_file(dir->d_name, false, &img_size);
11571159
buf = xmalloc(img_size);
11581160
if (!buf) {
@@ -1403,7 +1405,17 @@ int amdgpu_plugin_dump_file(int fd, int id)
14031405
return -1;
14041406
}
14051407

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

14091421
/* This is RenderD dumper plugin, for now just save renderD
@@ -1419,7 +1431,7 @@ int amdgpu_plugin_dump_file(int fd, int id)
14191431
return ret;
14201432

14211433
/* Need to return success here so that criu can call plugins for renderD nodes */
1422-
return ret;
1434+
return try_dump_dmabuf_list();
14231435
}
14241436

14251437
pr_info("%s() called for fd = %d\n", __func__, major(st.st_rdev));
@@ -1541,7 +1553,6 @@ static int restore_devices(struct kfd_ioctl_criu_args *args, CriuKfd *e)
15411553
int ret = 0, bucket_index = 0;
15421554

15431555
pr_debug("Restoring %d devices\n", e->num_of_gpus);
1544-
15451556
args->num_devices = e->num_of_gpus;
15461557
device_buckets = xzalloc(sizeof(*device_buckets) * args->num_devices);
15471558
if (!device_buckets)
@@ -1826,12 +1837,21 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
18261837
* first as we assume restore_maps is already filled. Need to fix this later.
18271838
*/
18281839
snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
1829-
pr_info("Restoring RenderD %s\n", img_path);
18301840

18311841
img_fp = open_img_file(img_path, false, &img_size);
1832-
if (!img_fp)
1833-
return -EINVAL;
1834-
1842+
if (!img_fp) {
1843+
ret = amdgpu_plugin_dmabuf_restore(id);
1844+
if (ret == 1) {
1845+
/* This is a dmabuf fd, but the corresponding buffer object that was
1846+
* exported to make it has not yet been restored. Need to try again
1847+
* later when the buffer object exists, so it can be re-exported.
1848+
*/
1849+
*retry_needed = true;
1850+
return 0;
1851+
}
1852+
return ret;
1853+
}
1854+
pr_info("Restoring RenderD %s\n", img_path);
18351855
pr_debug("RenderD Image file size:%ld\n", img_size);
18361856
buf = xmalloc(img_size);
18371857
if (!buf) {
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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+
29+
struct dmabuf {
30+
int id;
31+
int dmabuf_fd;
32+
struct list_head node;
33+
};
34+
35+
static LIST_HEAD(dmabuf_list);
36+
37+
/* Return < 0 for error, > 0 for "not a dmabuf" and 0 "is a dmabuf" */
38+
int get_dmabuf_info(int fd, struct stat *st)
39+
{
40+
char path[PATH_MAX];
41+
42+
if (read_fd_link(fd, path, sizeof(path)) < 0)
43+
return -1;
44+
45+
if (strncmp(path, DMABUF_LINK, strlen(DMABUF_LINK)) != 0)
46+
return 1;
47+
48+
return 0;
49+
}
50+
51+
int __amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id) {
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+
59+
gem_handle = handle_for_shared_bo_fd(dmabuf_fd);
60+
if (gem_handle < 0) {
61+
pr_err("Failed to get handle for dmabuf_fd = %d\n", dmabuf_fd);
62+
return -EAGAIN; /* Retry needed */
63+
}
64+
65+
CriuDmabufNode *node = xmalloc(sizeof(*node));
66+
if (!node) {
67+
pr_err("Failed to allocate memory for dmabuf node\n");
68+
return -ENOMEM;
69+
}
70+
criu_dmabuf_node__init(node);
71+
72+
node->gem_handle = gem_handle;
73+
74+
if (node->gem_handle < 0) {
75+
pr_err("Failed to get handle for dmabuf_fd\n");
76+
xfree(node);
77+
return -EINVAL;
78+
}
79+
80+
/* Serialize metadata to a file */
81+
snprintf(path, sizeof(path), IMG_DMABUF_FILE, id);
82+
len = criu_dmabuf_node__get_packed_size(node);
83+
buf = xmalloc(len);
84+
if (!buf) {
85+
pr_err("Failed to allocate buffer for dmabuf metadata\n");
86+
xfree(node);
87+
return -ENOMEM;
88+
}
89+
criu_dmabuf_node__pack(node, buf);
90+
ret = write_img_file(path, buf, len);
91+
92+
xfree(buf);
93+
xfree(node);
94+
return ret;
95+
}
96+
97+
int amdgpu_plugin_dmabuf_restore(int id) {
98+
char path[PATH_MAX];
99+
size_t img_size;
100+
FILE *img_fp = NULL;
101+
int ret = 0;
102+
CriuDmabufNode *rd = NULL;
103+
unsigned char *buf = NULL;
104+
int fd_id;
105+
106+
snprintf(path, sizeof(path), IMG_DMABUF_FILE, id);
107+
108+
/* Read serialized metadata */
109+
img_fp = open_img_file(path, false, &img_size);
110+
if (!img_fp) {
111+
pr_err("Failed to open dmabuf metadata file: %s\n", path);
112+
return -EINVAL;
113+
}
114+
115+
pr_debug("dmabuf Image file size:%ld\n", img_size);
116+
buf = xmalloc(img_size);
117+
if (!buf) {
118+
pr_perror("Failed to allocate memory");
119+
return -ENOMEM;
120+
}
121+
122+
ret = read_fp(img_fp, buf, img_size);
123+
if (ret) {
124+
pr_perror("Unable to read from %s", path);
125+
xfree(buf);
126+
return ret;
127+
}
128+
129+
rd = criu_dmabuf_node__unpack(NULL, img_size, buf);
130+
if (rd == NULL) {
131+
pr_perror("Unable to parse the dmabuf message %d", id);
132+
xfree(buf);
133+
fclose(img_fp);
134+
return -1;
135+
}
136+
fclose(img_fp);
137+
138+
/* Match GEM handle with shared_dmabuf list */
139+
fd_id = amdgpu_id_for_handle(rd->gem_handle);
140+
if (fd_id == -1) {
141+
pr_err("Failed to find dmabuf_fd for GEM handle = %d\n",
142+
rd->gem_handle);
143+
return 1;
144+
}
145+
int dmabuf_fd = fdstore_get(fd_id);
146+
if (dmabuf_fd == -1) {
147+
pr_err("Failed to find dmabuf_fd for GEM handle = %d\n",
148+
rd->gem_handle);
149+
return 1; /* Retry needed */
150+
} else {
151+
pr_info("Restored dmabuf_fd = %d for GEM handle = %d\n",
152+
dmabuf_fd, rd->gem_handle);
153+
}
154+
ret = dmabuf_fd;
155+
156+
pr_info("Successfully restored dmabuf_fd %d\n",
157+
dmabuf_fd);
158+
criu_dmabuf_node__free_unpacked(rd, NULL);
159+
xfree(buf);
160+
return ret;
161+
}
162+
163+
164+
int amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id)
165+
{
166+
int ret;
167+
168+
ret = __amdgpu_plugin_dmabuf_dump(dmabuf_fd, id);
169+
if (ret == -EAGAIN) {
170+
struct dmabuf *b = xmalloc(sizeof(*b));
171+
b->id = id;
172+
b->dmabuf_fd = dmabuf_fd;
173+
list_add(&b->node, &dmabuf_list);
174+
return 0;
175+
}
176+
return ret;
177+
}
178+
179+
int try_dump_dmabuf_list()
180+
{
181+
struct dmabuf *b, *t;
182+
list_for_each_entry_safe(b, t, &dmabuf_list, node) {
183+
int ret = __amdgpu_plugin_dmabuf_dump(b->dmabuf_fd, b->id);
184+
if (ret == -EAGAIN)
185+
continue;
186+
if (ret)
187+
return ret;
188+
list_del(&b->node);
189+
xfree(b);
190+
}
191+
return 0;
192+
}
193+
194+
int post_dump_dmabuf_check()
195+
{
196+
if (!list_empty(&dmabuf_list)) {
197+
pr_err("Not all dma buffers have been dumped\n");
198+
return -1;
199+
}
200+
return 0;
201+
}
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ int get_gem_handle(amdgpu_device_handle h_dev, int dmabuf_fd)
4848
return -1;
4949
}
5050

51-
drmPrimeFDToHandle(fd, dmabuf_fd, &handle);
51+
if (drmPrimeFDToHandle(fd, dmabuf_fd, &handle))
52+
return -1;
5253

5354
return handle;
5455
}

0 commit comments

Comments
 (0)