multifd-qatzip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Multifd QATzip compression implementation
  3. *
  4. * Copyright (c) Bytedance
  5. *
  6. * Authors:
  7. * Bryan Zhang <bryan.zhang@bytedance.com>
  8. * Hao Xiang <hao.xiang@bytedance.com>
  9. * Yichen Wang <yichen.wang@bytedance.com>
  10. *
  11. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. */
  14. #include "qemu/osdep.h"
  15. #include "exec/ramblock.h"
  16. #include "qapi/error.h"
  17. #include "qemu/error-report.h"
  18. #include "qapi/qapi-types-migration.h"
  19. #include "options.h"
  20. #include "multifd.h"
  21. #include <qatzip.h>
  22. typedef struct {
  23. /*
  24. * Unique session for use with QATzip API
  25. */
  26. QzSession_T sess;
  27. /*
  28. * For compression: Buffer for pages to compress
  29. * For decompression: Buffer for data to decompress
  30. */
  31. uint8_t *in_buf;
  32. uint32_t in_len;
  33. /*
  34. * For compression: Output buffer of compressed data
  35. * For decompression: Output buffer of decompressed data
  36. */
  37. uint8_t *out_buf;
  38. uint32_t out_len;
  39. } QatzipData;
  40. /**
  41. * qatzip_send_setup: Set up QATzip session and private buffers.
  42. *
  43. * @param p Multifd channel params
  44. * @param errp Pointer to error, which will be set in case of error
  45. * @return 0 on success, -1 on error (and *errp will be set)
  46. */
  47. static int qatzip_send_setup(MultiFDSendParams *p, Error **errp)
  48. {
  49. QatzipData *q;
  50. QzSessionParamsDeflate_T params;
  51. const char *err_msg;
  52. int ret;
  53. q = g_new0(QatzipData, 1);
  54. p->compress_data = q;
  55. /* We need one extra place for the packet header */
  56. p->iov = g_new0(struct iovec, 2);
  57. /*
  58. * Initialize QAT device with software fallback by default. This allows
  59. * QATzip to use CPU path when QAT hardware reaches maximum throughput.
  60. */
  61. ret = qzInit(&q->sess, true);
  62. if (ret != QZ_OK && ret != QZ_DUPLICATE) {
  63. err_msg = "qzInit failed";
  64. goto err;
  65. }
  66. ret = qzGetDefaultsDeflate(&params);
  67. if (ret != QZ_OK) {
  68. err_msg = "qzGetDefaultsDeflate failed";
  69. goto err;
  70. }
  71. /* Make sure to use configured QATzip compression level. */
  72. params.common_params.comp_lvl = migrate_multifd_qatzip_level();
  73. ret = qzSetupSessionDeflate(&q->sess, &params);
  74. if (ret != QZ_OK && ret != QZ_DUPLICATE) {
  75. err_msg = "qzSetupSessionDeflate failed";
  76. goto err;
  77. }
  78. if (MULTIFD_PACKET_SIZE > UINT32_MAX) {
  79. err_msg = "packet size too large for QAT";
  80. goto err;
  81. }
  82. q->in_len = MULTIFD_PACKET_SIZE;
  83. /*
  84. * PINNED_MEM is an enum from qatzip headers, which means to use
  85. * kzalloc_node() to allocate memory for QAT DMA purposes. When QAT device
  86. * is not available or software fallback is used, the malloc flag needs to
  87. * be set as COMMON_MEM.
  88. */
  89. q->in_buf = qzMalloc(q->in_len, 0, PINNED_MEM);
  90. if (!q->in_buf) {
  91. q->in_buf = qzMalloc(q->in_len, 0, COMMON_MEM);
  92. if (!q->in_buf) {
  93. err_msg = "qzMalloc failed";
  94. goto err;
  95. }
  96. }
  97. q->out_len = qzMaxCompressedLength(MULTIFD_PACKET_SIZE, &q->sess);
  98. q->out_buf = qzMalloc(q->out_len, 0, PINNED_MEM);
  99. if (!q->out_buf) {
  100. q->out_buf = qzMalloc(q->out_len, 0, COMMON_MEM);
  101. if (!q->out_buf) {
  102. err_msg = "qzMalloc failed";
  103. goto err;
  104. }
  105. }
  106. return 0;
  107. err:
  108. error_setg(errp, "multifd %u: [sender] %s", p->id, err_msg);
  109. return -1;
  110. }
  111. /**
  112. * qatzip_send_cleanup: Tear down QATzip session and release private buffers.
  113. *
  114. * @param p Multifd channel params
  115. * @param errp Pointer to error, which will be set in case of error
  116. * @return None
  117. */
  118. static void qatzip_send_cleanup(MultiFDSendParams *p, Error **errp)
  119. {
  120. QatzipData *q = p->compress_data;
  121. if (q) {
  122. if (q->in_buf) {
  123. qzFree(q->in_buf);
  124. }
  125. if (q->out_buf) {
  126. qzFree(q->out_buf);
  127. }
  128. (void)qzTeardownSession(&q->sess);
  129. (void)qzClose(&q->sess);
  130. g_free(q);
  131. }
  132. g_free(p->iov);
  133. p->iov = NULL;
  134. p->compress_data = NULL;
  135. }
  136. /**
  137. * qatzip_send_prepare: Compress pages and update IO channel info.
  138. *
  139. * @param p Multifd channel params
  140. * @param errp Pointer to error, which will be set in case of error
  141. * @return 0 on success, -1 on error (and *errp will be set)
  142. */
  143. static int qatzip_send_prepare(MultiFDSendParams *p, Error **errp)
  144. {
  145. uint32_t page_size = multifd_ram_page_size();
  146. MultiFDPages_t *pages = &p->data->u.ram;
  147. QatzipData *q = p->compress_data;
  148. int ret;
  149. unsigned int in_len, out_len;
  150. if (!multifd_send_prepare_common(p)) {
  151. goto out;
  152. }
  153. /*
  154. * Unlike other multifd compression implementations, we use a non-streaming
  155. * API and place all the data into one buffer, rather than sending each
  156. * page to the compression API at a time. Based on initial benchmarks, the
  157. * non-streaming API outperforms the streaming API. Plus, the logic in QEMU
  158. * is friendly to using the non-streaming API anyway. If either of these
  159. * statements becomes no longer true, we can revisit adding a streaming
  160. * implementation.
  161. */
  162. for (int i = 0; i < pages->normal_num; i++) {
  163. memcpy(q->in_buf + (i * page_size),
  164. pages->block->host + pages->offset[i],
  165. page_size);
  166. }
  167. in_len = pages->normal_num * page_size;
  168. if (in_len > q->in_len) {
  169. error_setg(errp, "multifd %u: unexpectedly large input", p->id);
  170. return -1;
  171. }
  172. out_len = q->out_len;
  173. ret = qzCompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_len, 1);
  174. if (ret != QZ_OK) {
  175. error_setg(errp, "multifd %u: QATzip returned %d instead of QZ_OK",
  176. p->id, ret);
  177. return -1;
  178. }
  179. if (in_len != pages->normal_num * page_size) {
  180. error_setg(errp, "multifd %u: QATzip failed to compress all input",
  181. p->id);
  182. return -1;
  183. }
  184. p->iov[p->iovs_num].iov_base = q->out_buf;
  185. p->iov[p->iovs_num].iov_len = out_len;
  186. p->iovs_num++;
  187. p->next_packet_size = out_len;
  188. out:
  189. p->flags |= MULTIFD_FLAG_QATZIP;
  190. multifd_send_fill_packet(p);
  191. return 0;
  192. }
  193. /**
  194. * qatzip_recv_setup: Set up QATzip session and allocate private buffers.
  195. *
  196. * @param p Multifd channel params
  197. * @param errp Pointer to error, which will be set in case of error
  198. * @return 0 on success, -1 on error (and *errp will be set)
  199. */
  200. static int qatzip_recv_setup(MultiFDRecvParams *p, Error **errp)
  201. {
  202. QatzipData *q;
  203. QzSessionParamsDeflate_T params;
  204. const char *err_msg;
  205. int ret;
  206. q = g_new0(QatzipData, 1);
  207. p->compress_data = q;
  208. /*
  209. * Initialize QAT device with software fallback by default. This allows
  210. * QATzip to use CPU path when QAT hardware reaches maximum throughput.
  211. */
  212. ret = qzInit(&q->sess, true);
  213. if (ret != QZ_OK && ret != QZ_DUPLICATE) {
  214. err_msg = "qzInit failed";
  215. goto err;
  216. }
  217. ret = qzGetDefaultsDeflate(&params);
  218. if (ret != QZ_OK) {
  219. err_msg = "qzGetDefaultsDeflate failed";
  220. goto err;
  221. }
  222. ret = qzSetupSessionDeflate(&q->sess, &params);
  223. if (ret != QZ_OK && ret != QZ_DUPLICATE) {
  224. err_msg = "qzSetupSessionDeflate failed";
  225. goto err;
  226. }
  227. /*
  228. * Reserve extra spaces for the incoming packets. Current implementation
  229. * doesn't send uncompressed pages in case the compression gets too big.
  230. */
  231. q->in_len = MULTIFD_PACKET_SIZE * 2;
  232. /*
  233. * PINNED_MEM is an enum from qatzip headers, which means to use
  234. * kzalloc_node() to allocate memory for QAT DMA purposes. When QAT device
  235. * is not available or software fallback is used, the malloc flag needs to
  236. * be set as COMMON_MEM.
  237. */
  238. q->in_buf = qzMalloc(q->in_len, 0, PINNED_MEM);
  239. if (!q->in_buf) {
  240. q->in_buf = qzMalloc(q->in_len, 0, COMMON_MEM);
  241. if (!q->in_buf) {
  242. err_msg = "qzMalloc failed";
  243. goto err;
  244. }
  245. }
  246. q->out_len = MULTIFD_PACKET_SIZE;
  247. q->out_buf = qzMalloc(q->out_len, 0, PINNED_MEM);
  248. if (!q->out_buf) {
  249. q->out_buf = qzMalloc(q->out_len, 0, COMMON_MEM);
  250. if (!q->out_buf) {
  251. err_msg = "qzMalloc failed";
  252. goto err;
  253. }
  254. }
  255. return 0;
  256. err:
  257. error_setg(errp, "multifd %u: [receiver] %s", p->id, err_msg);
  258. return -1;
  259. }
  260. /**
  261. * qatzip_recv_cleanup: Tear down QATzip session and release private buffers.
  262. *
  263. * @param p Multifd channel params
  264. * @return None
  265. */
  266. static void qatzip_recv_cleanup(MultiFDRecvParams *p)
  267. {
  268. QatzipData *q = p->compress_data;
  269. if (q) {
  270. if (q->in_buf) {
  271. qzFree(q->in_buf);
  272. }
  273. if (q->out_buf) {
  274. qzFree(q->out_buf);
  275. }
  276. (void)qzTeardownSession(&q->sess);
  277. (void)qzClose(&q->sess);
  278. g_free(q);
  279. }
  280. p->compress_data = NULL;
  281. }
  282. /**
  283. * qatzip_recv: Decompress pages and copy them to the appropriate
  284. * locations.
  285. *
  286. * @param p Multifd channel params
  287. * @param errp Pointer to error, which will be set in case of error
  288. * @return 0 on success, -1 on error (and *errp will be set)
  289. */
  290. static int qatzip_recv(MultiFDRecvParams *p, Error **errp)
  291. {
  292. QatzipData *q = p->compress_data;
  293. int ret;
  294. unsigned int in_len, out_len;
  295. uint32_t in_size = p->next_packet_size;
  296. uint32_t page_size = multifd_ram_page_size();
  297. uint32_t expected_size = p->normal_num * page_size;
  298. uint32_t flags = p->flags & MULTIFD_FLAG_COMPRESSION_MASK;
  299. if (in_size > q->in_len) {
  300. error_setg(errp, "multifd %u: received unexpectedly large packet",
  301. p->id);
  302. return -1;
  303. }
  304. if (flags != MULTIFD_FLAG_QATZIP) {
  305. error_setg(errp, "multifd %u: flags received %x flags expected %x",
  306. p->id, flags, MULTIFD_FLAG_QATZIP);
  307. return -1;
  308. }
  309. multifd_recv_zero_page_process(p);
  310. if (!p->normal_num) {
  311. assert(in_size == 0);
  312. return 0;
  313. }
  314. ret = qio_channel_read_all(p->c, (void *)q->in_buf, in_size, errp);
  315. if (ret != 0) {
  316. return ret;
  317. }
  318. in_len = in_size;
  319. out_len = q->out_len;
  320. ret = qzDecompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_len);
  321. if (ret != QZ_OK) {
  322. error_setg(errp, "multifd %u: qzDecompress failed", p->id);
  323. return -1;
  324. }
  325. if (out_len != expected_size) {
  326. error_setg(errp, "multifd %u: packet size received %u size expected %u",
  327. p->id, out_len, expected_size);
  328. return -1;
  329. }
  330. /* Copy each page to its appropriate location. */
  331. for (int i = 0; i < p->normal_num; i++) {
  332. memcpy(p->host + p->normal[i], q->out_buf + page_size * i, page_size);
  333. ramblock_recv_bitmap_set_offset(p->block, p->normal[i]);
  334. }
  335. return 0;
  336. }
  337. static MultiFDMethods multifd_qatzip_ops = {
  338. .send_setup = qatzip_send_setup,
  339. .send_cleanup = qatzip_send_cleanup,
  340. .send_prepare = qatzip_send_prepare,
  341. .recv_setup = qatzip_recv_setup,
  342. .recv_cleanup = qatzip_recv_cleanup,
  343. .recv = qatzip_recv
  344. };
  345. static void multifd_qatzip_register(void)
  346. {
  347. multifd_register_ops(MULTIFD_COMPRESSION_QATZIP, &multifd_qatzip_ops);
  348. }
  349. migration_init(multifd_qatzip_register);