|
| 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 | +/* Return < 0 for error, > 0 for "not a dmabuf" and 0 "is a dmabuf" */ |
| 29 | +int get_dmabuf_info(int fd, struct stat *st) |
| 30 | +{ |
| 31 | + char path[PATH_MAX]; |
| 32 | + |
| 33 | + if (read_fd_link(fd, path, sizeof(path)) < 0) |
| 34 | + return -1; |
| 35 | + |
| 36 | + if (strncmp(path, DMABUF_LINK, strlen(DMABUF_LINK)) != 0) |
| 37 | + return 1; |
| 38 | + |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +int amdgpu_plugin_dmabuf_dump(int dmabuf_fd, int id, struct stat *dmabuf_stat) { |
| 43 | + int ret = 0; |
| 44 | + char path[PATH_MAX]; |
| 45 | + size_t len = 0; |
| 46 | + unsigned char *buf = NULL; |
| 47 | + int gem_handle; |
| 48 | + |
| 49 | + pr_info("TWI: Dumping dmabuf fd = %d\n", dmabuf_fd); |
| 50 | + |
| 51 | + gem_handle = handle_for_shared_bo_fd(dmabuf_fd); |
| 52 | + if (gem_handle < 0) { |
| 53 | + pr_err("Failed to get handle for dmabuf_fd = %d\n", dmabuf_fd); |
| 54 | + return -EAGAIN; /* Retry needed */ |
| 55 | + } |
| 56 | + |
| 57 | + CriuDmabufNode *node = xmalloc(sizeof(*node)); |
| 58 | + if (!node) { |
| 59 | + pr_err("Failed to allocate memory for dmabuf node\n"); |
| 60 | + return -ENOMEM; |
| 61 | + } |
| 62 | + criu_dmabuf_node__init(node); |
| 63 | + |
| 64 | + node->gem_handle = gem_handle; |
| 65 | + |
| 66 | + if (node->gem_handle < 0) { |
| 67 | + pr_err("Failed to get handle for dmabuf_fd\n"); |
| 68 | + xfree(node); |
| 69 | + return -EINVAL; |
| 70 | + } |
| 71 | + |
| 72 | + /* Serialize metadata to a file */ |
| 73 | + snprintf(path, sizeof(path), IMG_DMABUF_FILE, id); |
| 74 | + len = criu_dmabuf_node__get_packed_size(node); |
| 75 | + buf = xmalloc(len); |
| 76 | + if (!buf) { |
| 77 | + pr_err("Failed to allocate buffer for dmabuf metadata\n"); |
| 78 | + xfree(node); |
| 79 | + return -ENOMEM; |
| 80 | + } |
| 81 | + criu_dmabuf_node__pack(node, buf); |
| 82 | + ret = write_img_file(path, buf, len); |
| 83 | + |
| 84 | + xfree(buf); |
| 85 | + xfree(node); |
| 86 | + return ret; |
| 87 | +} |
| 88 | + |
| 89 | +int amdgpu_plugin_dmabuf_restore(int id) { |
| 90 | + char path[PATH_MAX]; |
| 91 | + size_t img_size; |
| 92 | + FILE *img_fp = NULL; |
| 93 | + int ret = 0; |
| 94 | + CriuDmabufNode *rd = NULL; |
| 95 | + unsigned char *buf = NULL; |
| 96 | + int fd_id; |
| 97 | + |
| 98 | + snprintf(path, sizeof(path), IMG_DMABUF_FILE, id); |
| 99 | + |
| 100 | + pr_info("TWI: Restoring dmabuf fd, id = %d\n", id); |
| 101 | + |
| 102 | + /* Read serialized metadata */ |
| 103 | + img_fp = open_img_file(path, false, &img_size); |
| 104 | + if (!img_fp) { |
| 105 | + pr_err("Failed to open dmabuf metadata file: %s\n", path); |
| 106 | + return -EINVAL; |
| 107 | + } |
| 108 | + |
| 109 | + pr_debug("dmabuf Image file size:%ld\n", img_size); |
| 110 | + buf = xmalloc(img_size); |
| 111 | + if (!buf) { |
| 112 | + pr_perror("Failed to allocate memory"); |
| 113 | + return -ENOMEM; |
| 114 | + } |
| 115 | + |
| 116 | + ret = read_fp(img_fp, buf, img_size); |
| 117 | + if (ret) { |
| 118 | + pr_perror("Unable to read from %s", path); |
| 119 | + xfree(buf); |
| 120 | + return ret; |
| 121 | + } |
| 122 | + |
| 123 | + rd = criu_dmabuf_node__unpack(NULL, img_size, buf); |
| 124 | + if (rd == NULL) { |
| 125 | + pr_perror("Unable to parse the dmabuf message %d", id); |
| 126 | + xfree(buf); |
| 127 | + fclose(img_fp); |
| 128 | + return -1; |
| 129 | + } |
| 130 | + fclose(img_fp); |
| 131 | + |
| 132 | + pr_info("TWI: dmabuf node gem_handle = %d\n", rd->gem_handle); |
| 133 | + |
| 134 | + /* Match GEM handle with shared_dmabuf list */ |
| 135 | + fd_id = amdgpu_id_for_handle(rd->gem_handle); |
| 136 | + if (fd_id == -1) { |
| 137 | + pr_err("Failed to find dmabuf_fd for GEM handle = %d\n", |
| 138 | + rd->gem_handle); |
| 139 | + return 1; |
| 140 | + } |
| 141 | + int dmabuf_fd = fdstore_get(fd_id); |
| 142 | + pr_info("TWI: dmabuf node fd_id = %d, dmabuf_fd = %d\n", fd_id, dmabuf_fd); |
| 143 | + if (dmabuf_fd == -1) { |
| 144 | + pr_err("Failed to find dmabuf_fd for GEM handle = %d\n", |
| 145 | + rd->gem_handle); |
| 146 | + return 1; /* Retry needed */ |
| 147 | + } else { |
| 148 | + pr_info("Restored dmabuf_fd = %d for GEM handle = %d\n", |
| 149 | + dmabuf_fd, rd->gem_handle); |
| 150 | + } |
| 151 | + ret = dmabuf_fd; |
| 152 | + |
| 153 | + pr_info("Successfully restored dmabuf_fd %d\n", |
| 154 | + dmabuf_fd); |
| 155 | + criu_dmabuf_node__free_unpacked(rd, NULL); |
| 156 | + xfree(buf); |
| 157 | + return ret; |
| 158 | +} |
0 commit comments