From nobody Tue Nov 26 22:12:03 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=bytedance.com ARC-Seal: i=1; a=rsa-sha256; t=1704057793; cv=none; d=zohomail.com; s=zohoarc; b=f0Mw/F1ZI1BOhOafsIqLobFQn8ScNmmhYBdXJYqpNcQWAb7Ndq6OSExAWyq1sv/TOl1sqA6gaNDF+BWYYhr53tDbmrgtT5Mc0KDKMt0AJlqEtsD96tpVuYTpbuaqp133SWgha4XI0u6pYN6LmyTLytAAurchJIaVB5NeIOzGQKA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1704057793; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=lfKrdv08TX0rJ6x8vnK6oFLE9GkUtVpHlgcxnHxsOPE=; b=ZUnMmo+tOuLuHEgWAiH7Zw252yrpUNyZKkz8WLxyyGCtzDSHJ+IYOl8QE1KTVpIGjMOGHaAS4xDmWMHLSfO5SZv8ULnUuBpoHCogwSLTYwVcww1UN9BGYlkJrP+g8jxcX5GeFy78rS8XzViG2DQoSwinrNnoIQXAUXvBl/zUBP8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1704057793199757.9549756034746; Sun, 31 Dec 2023 13:23:13 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rK3GD-0001Ih-8r; Sun, 31 Dec 2023 16:22:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rK2ux-00078n-1H for qemu-devel@nongnu.org; Sun, 31 Dec 2023 16:00:20 -0500 Received: from mail-qk1-x72e.google.com ([2607:f8b0:4864:20::72e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rK2uu-0007QM-SO for qemu-devel@nongnu.org; Sun, 31 Dec 2023 16:00:18 -0500 Received: by mail-qk1-x72e.google.com with SMTP id af79cd13be357-781b9922f44so67229085a.2 for ; Sun, 31 Dec 2023 13:00:16 -0800 (PST) Received: from n36-186-108.byted.org. ([147.160.184.90]) by smtp.gmail.com with ESMTPSA id pb21-20020a05620a839500b007811da87cefsm8111750qkn.127.2023.12.31.13.00.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 31 Dec 2023 13:00:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1704056415; x=1704661215; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lfKrdv08TX0rJ6x8vnK6oFLE9GkUtVpHlgcxnHxsOPE=; b=SGu24BRZLNAI4c7zUB1c6vjVpk1aAJtHdh6637v/8NJH5BWnjufXRydaf1r6sJJZ/j E4zn/KCB1kmXWT3IkIILzRReKRcQ7/tqAFoCa7ykzpBT5TNV8cHM/BmZUX6oDFFSAj8Y Ag+nXK7412nfXpEwrd5QhARL3UNqrdjuGH7eN9InnSVhesu/JCYl3x9tDB9qxF6e4zbo O0nlWKtbpCmZiKv+addga/g4wW/gugVJnsT/SyYPgvGmlAIRFhh9SPCFcdLcMNGsPX8X txhOdf73xjH02tCgVlVXc/jkHjs7bUUyd2h9HHDrandD0l3X7tgiLJ8aO2Czebl0g7MO 0CrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704056415; x=1704661215; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lfKrdv08TX0rJ6x8vnK6oFLE9GkUtVpHlgcxnHxsOPE=; b=CzsjenjrINnD5bBoWzUoCfQv+nYNy5AoldB8wz25TYiHW9p+778pMnPer4hN3OQNRa 5JTeJlDh4x6EbMRC1GLi/kS/Kel8+BiD+QWXQjMfK5yVJG0ev4ssHujxH6qs99ID2H21 x8H1j0UJcu7in/Ku6NePGD3JmA7cDq60NokyYMJxuVTGxhum6tjNrEkP+p8j1oHioct2 WkJ/jFzfC21aaKTrx2leWo9946tOiLswMdpm0xvYEMgDYVI519oOsf/CrfDc/giR0akE j/L/m1azAVd/BzVXoLYf7HFljDQzXtgwpaQ9KLbJxYyDGos99nb3hFqwpI0jzdbeR9GK WOnQ== X-Gm-Message-State: AOJu0YxIdk4F8kNaAcYxf7WyG3M8qmGCwnHYZXvMinotdC57+Ih/tTRk PLuUleIew/cXZWf583mQHcNtHjyEQ34Eu52158aZiRwd6PA= X-Google-Smtp-Source: AGHT+IH+sh1HJmKRIlFWbluEJY8n63jrcvOjUO1TOhn9YClUNXLf6Mi4B9Opqd//pPVlbpbGpS6EGQ== X-Received: by 2002:a05:620a:22ef:b0:781:7ac2:630f with SMTP id p15-20020a05620a22ef00b007817ac2630fmr6706290qki.98.1704056415338; Sun, 31 Dec 2023 13:00:15 -0800 (PST) From: Bryan Zhang To: qemu-devel@nongnu.org, farosas@suse.de, marcandre.lureau@redhat.com, peterx@redhat.com, quintela@redhat.com, peter.maydell@linaro.org, hao.xiang@bytedance.com Cc: bryan.zhang@bytedance.com Subject: [PATCH 4/5] migration: Implement 'qatzip' methods using QAT Date: Sun, 31 Dec 2023 20:58:03 +0000 Message-Id: <20231231205804.2366509-5-bryan.zhang@bytedance.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20231231205804.2366509-1-bryan.zhang@bytedance.com> References: <20231231205804.2366509-1-bryan.zhang@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::72e; envelope-from=bryan.zhang@bytedance.com; helo=mail-qk1-x72e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sun, 31 Dec 2023 16:22:09 -0500 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bytedance.com) X-ZM-MESSAGEID: 1704057795263100001 Content-Type: text/plain; charset="utf-8" Uses QAT to offload deflate compression in the 'qatzip' compression method for multifd migration. Signed-off-by: Bryan Zhang Signed-off-by: Hao Xiang --- migration/multifd-qatzip.c | 314 +++++++++++++++++++++++++++++++++++-- 1 file changed, 301 insertions(+), 13 deletions(-) diff --git a/migration/multifd-qatzip.c b/migration/multifd-qatzip.c index 1733bbddb7..3fb0bb5b27 100644 --- a/migration/multifd-qatzip.c +++ b/migration/multifd-qatzip.c @@ -18,50 +18,338 @@ #include "migration.h" #include "options.h" #include "multifd.h" +#include =20 +struct qatzip_data { + /* + * Unique session for use with QATzip API + */ + QzSession_T sess; + + /* + * For compression: Buffer for pages to compress + * For decompression: Buffer for data to decompress + */ + uint8_t *in_buf; + uint32_t in_len; + + /* + * For compression: Output buffer of compressed data + * For decompression: Output buffer of decompressed data + */ + uint8_t *out_buf; + uint32_t out_len; +}; + +/** + * qatzip_send_setup: Set up QATzip session and private buffers. + * + * @param p Multifd channel params + * @param errp Pointer to error, which will be set in case of error + * @return 0 on success, -1 on error (and *errp will be set) + */ static int qatzip_send_setup(MultiFDSendParams *p, Error **errp) { + struct qatzip_data *q; + QzSessionParamsDeflate_T params; + const char *err_msg; + int ret; + + q =3D g_new0(struct qatzip_data, 1); + p->data =3D q; + + ret =3D qzInit(&q->sess, 0); + if (ret !=3D QZ_OK && ret !=3D QZ_DUPLICATE) { + err_msg =3D "qzInit failed"; + goto err_free_q; + } + + ret =3D qzGetDefaultsDeflate(¶ms); + if (ret !=3D QZ_OK) { + err_msg =3D "qzGetDefaultsDeflate failed"; + goto err_close; + } + + /* Use maximum hardware buffer size to improve batching. */ + params.common_params.hw_buff_sz =3D QZ_HW_BUFF_MAX_SZ; + + /* Make sure to use configured QATzip compression level. */ + params.common_params.comp_lvl =3D migrate_multifd_qatzip_level(); + + ret =3D qzSetupSessionDeflate(&q->sess, ¶ms); + if (ret !=3D QZ_OK && ret !=3D QZ_DUPLICATE) { + err_msg =3D "qzSetupSessionDeflate failed"; + goto err_close; + } + + /* TODO Add support for larger packets. */ + if (MULTIFD_PACKET_SIZE > UINT32_MAX) { + err_msg =3D "packet size too large for QAT"; + goto err_close; + } + + q->in_len =3D MULTIFD_PACKET_SIZE; + q->in_buf =3D g_try_malloc(q->in_len); + if (!q->in_buf) { + err_msg =3D "malloc failed"; + goto err_close; + } + + q->out_len =3D qzMaxCompressedLength(MULTIFD_PACKET_SIZE, &q->sess); + q->out_buf =3D g_try_malloc(q->out_len); + if (!q->out_buf) { + err_msg =3D "malloc failed"; + goto err_free_inbuf; + } + return 0; + +err_free_inbuf: + g_free(q->in_buf); +err_close: + qzClose(&q->sess); +err_free_q: + g_free(q); + error_setg(errp, "multifd %u: %s", p->id, err_msg); + return -1; } =20 -static void qatzip_send_cleanup(MultiFDSendParams *p, Error **errp) {}; +/** + * qatzip_send_cleanup: Tear down QATzip session and release private buffe= rs. + * + * @param p Multifd channel params + * @param errp Pointer to error, which will be set in case of error + * @return None + */ +static void qatzip_send_cleanup(MultiFDSendParams *p, Error **errp) +{ + struct qatzip_data *q =3D p->data; + const char *err_msg; + int ret; + + ret =3D qzTeardownSession(&q->sess); + if (ret !=3D QZ_OK) { + err_msg =3D "qzTeardownSession failed"; + goto err; + } + + ret =3D qzClose(&q->sess); + if (ret !=3D QZ_OK) { + err_msg =3D "qzClose failed"; + goto err; + } + + g_free(q->in_buf); + q->in_buf =3D NULL; + g_free(q->out_buf); + q->out_buf =3D NULL; + g_free(p->data); + p->data =3D NULL; + return; + +err: + error_setg(errp, "multifd %u: %s", p->id, err_msg); +} =20 +/** + * qatzip_send_prepare: Compress pages and update IO channel info. + * + * @param p Multifd channel params + * @param errp Pointer to error, which will be set in case of error + * @return 0 on success, -1 on error (and *errp will be set) + */ static int qatzip_send_prepare(MultiFDSendParams *p, Error **errp) { - MultiFDPages_t *pages =3D p->pages; + struct qatzip_data *q =3D p->data; + int ret; + unsigned int in_len, out_len; =20 + /* memcpy all the pages into one buffer. */ for (int i =3D 0; i < p->normal_num; i++) { - p->iov[p->iovs_num].iov_base =3D pages->block->host + p->normal[i]; - p->iov[p->iovs_num].iov_len =3D p->page_size; - p->iovs_num++; + memcpy(q->in_buf + (i * p->page_size), + p->pages->block->host + p->normal[i], + p->page_size); + } + + in_len =3D p->normal_num * p->page_size; + if (in_len > q->in_len) { + error_setg(errp, "multifd %u: unexpectedly large input", p->id); + return -1; + } + out_len =3D q->out_len; + + /* + * Unlike other multifd compression implementations, we use a non-stre= aming + * API and place all the data into one buffer, rather than sending eac= h page + * to the compression API at a time. Based on initial benchmarks, the + * non-streaming API outperforms the streaming API. Plus, the logic in= QEMU + * is friendly to using the non-streaming API anyway. If either of the= se + * statements becomes no longer true, we can revisit adding a streaming + * implementation. + */ + ret =3D qzCompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_len,= 1); + if (ret !=3D QZ_OK) { + error_setg(errp, "multifd %u: QATzip returned %d instead of QZ_OK", + p->id, ret); + return -1; + } + if (in_len !=3D p->normal_num * p->page_size) { + error_setg(errp, "multifd %u: QATzip failed to compress all input", + p->id); + return -1; } =20 - p->next_packet_size =3D p->normal_num * p->page_size; - p->flags |=3D MULTIFD_FLAG_NOCOMP; + p->iov[p->iovs_num].iov_base =3D q->out_buf; + p->iov[p->iovs_num].iov_len =3D out_len; + p->iovs_num++; + p->next_packet_size =3D out_len; + p->flags |=3D MULTIFD_FLAG_QATZIP; return 0; } =20 +/** + * qatzip_recv_setup: Set up QATzip session and allocate private buffers. + * + * @param p Multifd channel params + * @param errp Pointer to error, which will be set in case of error + * @return 0 on success, -1 on error (and *errp will be set) + */ static int qatzip_recv_setup(MultiFDRecvParams *p, Error **errp) { + struct qatzip_data *q; + QzSessionParamsDeflate_T params; + const char *err_msg; + int ret; + + q =3D g_new0(struct qatzip_data, 1); + p->data =3D q; + + ret =3D qzInit(&q->sess, 0); + if (ret !=3D QZ_OK && ret !=3D QZ_DUPLICATE) { + err_msg =3D "qzInit failed"; + goto err_free_q; + } + + ret =3D qzGetDefaultsDeflate(¶ms); + if (ret !=3D QZ_OK) { + err_msg =3D "qzGetDefaultsDeflate failed"; + goto err_close; + } + + /* Set maximum hardware buffer size for improved batching. */ + params.common_params.hw_buff_sz =3D QZ_HW_BUFF_MAX_SZ; + + /* Make sure to use configured QATzip compression level. */ + params.common_params.comp_lvl =3D migrate_multifd_qatzip_level(); + + ret =3D qzSetupSessionDeflate(&q->sess, ¶ms); + if (ret !=3D QZ_OK && ret !=3D QZ_DUPLICATE) { + err_msg =3D "qzSetupSessionDeflate failed"; + goto err_close; + } + + /* + * Mimic multifd-zlib, which reserves extra space for the incoming pac= ket. + */ + q->in_len =3D MULTIFD_PACKET_SIZE * 2; + q->in_buf =3D g_try_malloc(q->in_len); + if (!q->in_buf) { + err_msg =3D "malloc failed"; + goto err_close; + } + + q->out_len =3D MULTIFD_PACKET_SIZE; + q->out_buf =3D g_try_malloc(q->out_len); + if (!q->out_buf) { + err_msg =3D "malloc failed"; + goto err_free_inbuf; + } + return 0; + +err_free_inbuf: + g_free(q->in_buf); +err_close: + qzClose(&q->sess); +err_free_q: + g_free(q); + error_setg(errp, "multifd %u: %s", p->id, err_msg); + return -1; } =20 -static void qatzip_recv_cleanup(MultiFDRecvParams *p) {}; +/** + * qatzip_recv_cleanup: Tear down QATzip session and release private buffe= rs. + * + * @param p Multifd channel params + * @return None + */ +static void qatzip_recv_cleanup(MultiFDRecvParams *p) +{ + struct qatzip_data *q =3D p->data; + + /* Ignoring return values here due to function signature. */ + qzTeardownSession(&q->sess); + qzClose(&q->sess); + g_free(q->in_buf); + g_free(q->out_buf); + g_free(p->data); +} =20 + +/** + * qatzip_recv_pages: Decompress pages and copy them to the appropriate + * locations. + * + * @param p Multifd channel params + * @param errp Pointer to error, which will be set in case of error + * @return 0 on success, -1 on error (and *errp will be set) + */ static int qatzip_recv_pages(MultiFDRecvParams *p, Error **errp) { + struct qatzip_data *q =3D p->data; + int ret; + unsigned int in_len, out_len; + uint32_t in_size =3D p->next_packet_size; + uint32_t expected_size =3D p->normal_num * p->page_size; uint32_t flags =3D p->flags & MULTIFD_FLAG_COMPRESSION_MASK; =20 - if (flags !=3D MULTIFD_FLAG_NOCOMP) { + if (in_size > q->in_len) { + error_setg(errp, "multifd %u: received unexpectedly large packet", + p->id); + return -1; + } + + if (flags !=3D MULTIFD_FLAG_QATZIP) { error_setg(errp, "multifd %u: flags received %x flags expected %x", - p->id, flags, MULTIFD_FLAG_NOCOMP); + p->id, flags, MULTIFD_FLAG_QATZIP); + return -1; + } + + ret =3D qio_channel_read_all(p->c, (void *)q->in_buf, in_size, errp); + if (ret !=3D 0) { + return ret; + } + + in_len =3D in_size; + out_len =3D q->out_len; + ret =3D qzDecompress(&q->sess, q->in_buf, &in_len, q->out_buf, &out_le= n); + if (ret !=3D QZ_OK) { + error_setg(errp, "multifd %u: qzDecompress failed", p->id); + return -1; + } + if (out_len !=3D expected_size) { + error_setg(errp, "multifd %u: packet size received %u size expecte= d %u", + p->id, out_len, expected_size); return -1; } + + /* Copy each page to its appropriate location. */ for (int i =3D 0; i < p->normal_num; i++) { - p->iov[i].iov_base =3D p->host + p->normal[i]; - p->iov[i].iov_len =3D p->page_size; + memcpy(p->host + p->normal[i], + q->out_buf + p->page_size * i, + p->page_size); } - return qio_channel_readv_all(p->c, p->iov, p->normal_num, errp); + return 0; } =20 static MultiFDMethods multifd_qatzip_ops =3D { --=20 2.30.2