Skip to content

Commit 01792f8

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 01792f8

File tree

8 files changed

+292
-20
lines changed

8 files changed

+292
-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();
@@ -1403,7 +1407,17 @@ int amdgpu_plugin_dump_file(int fd, int id)
14031407
return -1;
14041408
}
14051409

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

14091423
/* This is RenderD dumper plugin, for now just save renderD
@@ -1417,7 +1431,7 @@ int amdgpu_plugin_dump_file(int fd, int id)
14171431
ret = record_dumped_fd(fd, true);
14181432
if (ret)
14191433
return ret;
1420-
1434+
ret = try_dump_dmabuf_list();
14211435
/* Need to return success here so that criu can call plugins for renderD nodes */
14221436
return ret;
14231437
}
@@ -1541,7 +1555,6 @@ static int restore_devices(struct kfd_ioctl_criu_args *args, CriuKfd *e)
15411555
int ret = 0, bucket_index = 0;
15421556

15431557
pr_debug("Restoring %d devices\n", e->num_of_gpus);
1544-
15451558
args->num_devices = e->num_of_gpus;
15461559
device_buckets = xzalloc(sizeof(*device_buckets) * args->num_devices);
15471560
if (!device_buckets)
@@ -1826,12 +1839,17 @@ int amdgpu_plugin_restore_file(int id, bool *retry_needed)
18261839
* first as we assume restore_maps is already filled. Need to fix this later.
18271840
*/
18281841
snprintf(img_path, sizeof(img_path), IMG_DRM_FILE, id);
1829-
pr_info("Restoring RenderD %s\n", img_path);
18301842

18311843
img_fp = open_img_file(img_path, false, &img_size);
1832-
if (!img_fp)
1833-
return -EINVAL;
1834-
1844+
if (!img_fp) {
1845+
ret = amdgpu_plugin_dmabuf_restore(id);
1846+
if (ret == 1) {
1847+
*retry_needed = true;
1848+
return 0;
1849+
}
1850+
return ret;
1851+
}
1852+
pr_info("Restoring RenderD %s\n", img_path);
18351853
pr_debug("RenderD Image file size:%ld\n", img_size);
18361854
buf = xmalloc(img_size);
18371855
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+
else 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 1;
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
}

plugins/amdgpu/amdgpu_plugin_util.c

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "amdgpu_drm.h"
3838
#include "amdgpu_plugin_util.h"
3939
#include "amdgpu_plugin_topology.h"
40+
#include "amdgpu_plugin_drm.h"
4041

4142
static LIST_HEAD(dumped_fds);
4243
static LIST_HEAD(shared_bos);
@@ -105,6 +106,42 @@ int record_shared_bo(int handle, bool is_imported) {
105106
return 0;
106107
}
107108

109+
int handle_for_shared_bo_fd(int fd) {
110+
struct dumped_fd *df;
111+
int trial_handle;
112+
amdgpu_device_handle h_dev;
113+
uint32_t major, minor;
114+
struct shared_bo *bo;
115+
116+
list_for_each_entry(df, &dumped_fds, l) {
117+
118+
/* see if the gem handle for fd using the hdev for df->fd is the
119+
same as bo->handle. */
120+
121+
if (!df->is_drm) {
122+
continue;
123+
}
124+
125+
if (amdgpu_device_initialize(df->fd, &major, &minor, &h_dev)) {
126+
pr_err("Failed to initialize amdgpu device\n");
127+
continue;
128+
}
129+
130+
trial_handle = get_gem_handle(h_dev, fd);
131+
if (trial_handle < 0)
132+
continue;
133+
134+
list_for_each_entry(bo, &shared_bos, l) {
135+
if (bo->handle == trial_handle)
136+
return trial_handle;
137+
}
138+
139+
amdgpu_device_deinitialize(h_dev);
140+
}
141+
142+
return -1;
143+
}
144+
108145
int record_completed_work(int handle, int id) {
109146
struct restore_completed_work *work;
110147

@@ -131,13 +168,6 @@ bool work_already_completed(int handle, int id) {
131168
}
132169

133170
void clear_restore_state() {
134-
while (!list_empty(&shared_dmabuf_fds)) {
135-
struct shared_dmabuf *st = list_first_entry(&shared_dmabuf_fds, struct shared_dmabuf, l);
136-
list_del(&st->l);
137-
close(st->dmabuf_fd);
138-
free(st);
139-
}
140-
141171
while (!list_empty(&completed_work)) {
142172
struct restore_completed_work *st = list_first_entry(&completed_work, struct restore_completed_work, l);
143173
list_del(&st->l);

0 commit comments

Comments
 (0)