|
| 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 | + gem_handle = handle_for_shared_bo_fd(dmabuf_fd); |
| 59 | + if (gem_handle < 0) { |
| 60 | + pr_err("Failed to get handle for dmabuf_fd = %d\n", dmabuf_fd); |
| 61 | + return -EAGAIN; /* Retry needed */ |
| 62 | + } |
| 63 | + |
| 64 | + CriuDmabufNode *node = xmalloc(sizeof(*node)); |
| 65 | + if (!node) { |
| 66 | + pr_err("Failed to allocate memory for dmabuf node\n"); |
| 67 | + return -ENOMEM; |
| 68 | + } |
| 69 | + criu_dmabuf_node__init(node); |
| 70 | + |
| 71 | + node->gem_handle = gem_handle; |
| 72 | + |
| 73 | + if (node->gem_handle < 0) { |
| 74 | + pr_err("Failed to get handle for dmabuf_fd\n"); |
| 75 | + xfree(node); |
| 76 | + return -EINVAL; |
| 77 | + } |
| 78 | + |
| 79 | + /* Serialize metadata to a file */ |
| 80 | + snprintf(path, sizeof(path), IMG_DMABUF_FILE, id); |
| 81 | + len = criu_dmabuf_node__get_packed_size(node); |
| 82 | + buf = xmalloc(len); |
| 83 | + if (!buf) { |
| 84 | + pr_err("Failed to allocate buffer for dmabuf metadata\n"); |
| 85 | + xfree(node); |
| 86 | + return -ENOMEM; |
| 87 | + } |
| 88 | + criu_dmabuf_node__pack(node, buf); |
| 89 | + ret = write_img_file(path, buf, len); |
| 90 | + |
| 91 | + xfree(buf); |
| 92 | + xfree(node); |
| 93 | + return ret; |
| 94 | +} |
| 95 | + |
| 96 | +int amdgpu_plugin_dmabuf_restore(int id) |
| 97 | +{ |
| 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", rd->gem_handle); |
| 142 | + return 1; |
| 143 | + } |
| 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", rd->gem_handle); |
| 148 | + return 1; /* Retry needed */ |
| 149 | + } |
| 150 | + |
| 151 | + pr_info("Restored dmabuf_fd = %d for GEM handle = %d\n", dmabuf_fd, rd->gem_handle); |
| 152 | + ret = dmabuf_fd; |
| 153 | + |
| 154 | + pr_info("Successfully restored dmabuf_fd %d\n", dmabuf_fd); |
| 155 | + criu_dmabuf_node__free_unpacked(rd, NULL); |
| 156 | + xfree(buf); |
| 157 | + return ret; |
| 158 | +} |
| 159 | + |
| 160 | +int amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id) |
| 161 | +{ |
| 162 | + int ret; |
| 163 | + |
| 164 | + ret = __amdgpu_plugin_dmabuf_dump(dmabuf_fd, id); |
| 165 | + if (ret == -EAGAIN) { |
| 166 | + struct dmabuf *b = xmalloc(sizeof(*b)); |
| 167 | + b->id = id; |
| 168 | + b->dmabuf_fd = dmabuf_fd; |
| 169 | + list_add(&b->node, &dmabuf_list); |
| 170 | + return 0; |
| 171 | + } |
| 172 | + return ret; |
| 173 | +} |
| 174 | + |
| 175 | +int try_dump_dmabuf_list() |
| 176 | +{ |
| 177 | + struct dmabuf *b, *t; |
| 178 | + list_for_each_entry_safe(b, t, &dmabuf_list, node) { |
| 179 | + int ret = __amdgpu_plugin_dmabuf_dump(b->dmabuf_fd, b->id); |
| 180 | + if (ret == -EAGAIN) |
| 181 | + continue; |
| 182 | + if (ret) |
| 183 | + return ret; |
| 184 | + list_del(&b->node); |
| 185 | + xfree(b); |
| 186 | + } |
| 187 | + return 0; |
| 188 | +} |
| 189 | + |
| 190 | +int post_dump_dmabuf_check() |
| 191 | +{ |
| 192 | + if (!list_empty(&dmabuf_list)) { |
| 193 | + pr_err("Not all dma buffers have been dumped\n"); |
| 194 | + return -1; |
| 195 | + } |
| 196 | + return 0; |
| 197 | +} |
0 commit comments