From patchwork Mon Jun 20 05:39:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Leonardo Bras X-Patchwork-Id: 12887004 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6BF7BC433EF for ; Mon, 20 Jun 2022 05:44:53 +0000 (UTC) Received: from localhost ([::1]:58786 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3ADU-0000i0-3o for qemu-devel@archiver.kernel.org; Mon, 20 Jun 2022 01:44:52 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60944) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A94-0006qW-Pw for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:49187) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A91-0001zq-4Z for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655703614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sXyf3lUINAbMGgUajxz86xF+mCWXf+ius0hSj5jixiw=; b=B0OYUcrkOC2BU4E78rNwYnBEEPLWtXd3YCsSLc6wNMdEeiaq6/SPOlR2J2bDbkqkr34Ewd taC/jumCo/xBUkoYi49FbwdtfrRGEibM1aiWa1V+Qg6j4BJB2Axsanj2EewrYWmzsNqd9F BXiiqP5azRwy+eeGfiEwgwXL/jYGVTE= Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-328-Ou149WxyMXmzHEk_wPOGUw-1; Mon, 20 Jun 2022 01:40:10 -0400 X-MC-Unique: Ou149WxyMXmzHEk_wPOGUw-1 Received: by mail-ot1-f71.google.com with SMTP id j13-20020a056830014d00b0060c27fcfe63so5426382otp.9 for ; Sun, 19 Jun 2022 22:40:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sXyf3lUINAbMGgUajxz86xF+mCWXf+ius0hSj5jixiw=; b=HtkxgsmqfO9rfZxxpP5PKKkK+C8hhzNLwrFaE08W0Y5H7qilByF0PucFc9WyslylRK sN2cU6ouyB6FWH+B3RVtBhPM0xT13psHJqepAiK931sMQR+WrEUMmpSmnGYB/vGHhBCU 8JeEcJV+G+DEAnXwEMn6PvaKoW3LIkK6giwofJsgiV/61Tw1uPRzMpifP3GW2JgJd//n ie5f26ZyyyIpyje6zstHk4PYPQijQwhReI7wqDX2HPNiuNjR+0mvqMIbnqttTWZWTw8g a14lrPv0q1AtL2rH6C4VaD1dtBL9g/g0yBcwidUNa9kqvEibSczaoKRG1AC1sLwhbFRc 3KYg== X-Gm-Message-State: AJIora+bdNnVbcu+R7GL+V6Abb8aubOqS0A0IYvcvS89OOoQEYOpk5Mm 7ioduyHl5L9swJZAbK+9J2ve1bx20mNwVOw05VRrSk5EPNjR8ZiOPIUXxR9rh8GrsOWANfJ7MPP 3Y+8vaneRUiQiLiA= X-Received: by 2002:a05:6870:170f:b0:101:c93f:2517 with SMTP id h15-20020a056870170f00b00101c93f2517mr4505805oae.78.1655703609802; Sun, 19 Jun 2022 22:40:09 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vsl0CJKPYaApW1WdGGAYjdhNP6mZBwjF6HwVXQKZ+aGUP/2bIp1y8wsyOpmKoy+WHRRWb+pQ== X-Received: by 2002:a05:6870:170f:b0:101:c93f:2517 with SMTP id h15-20020a056870170f00b00101c93f2517mr4505799oae.78.1655703609647; Sun, 19 Jun 2022 22:40:09 -0700 (PDT) Received: from localhost.localdomain ([2804:431:c7f0:d933:8f52:267a:639c:b4c0]) by smtp.gmail.com with ESMTPSA id e94-20020a9d2ae7000000b0060c2845c175sm7021463otb.8.2022.06.19.22.40.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 22:40:09 -0700 (PDT) From: Leonardo Bras To: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Eric Blake , Markus Armbruster , Peter Xu , =?utf-8?b?5b6Q6Zev?= Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [PATCH v4 1/4] QIOChannelSocket: Introduce assert and reduce ifdefs to improve readability Date: Mon, 20 Jun 2022 02:39:42 -0300 Message-Id: <20220620053944.257547-2-leobras@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620053944.257547-1-leobras@redhat.com> References: <20220620053944.257547-1-leobras@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" During implementation of MSG_ZEROCOPY feature, a lot of #ifdefs were introduced, particularly at qio_channel_socket_writev(). Rewrite some of those changes so it's easier to read. Also, introduce an assert to help detect incorrect zero-copy usage is when it's disabled on build. Signed-off-by: Leonardo Bras Reviewed-by: Daniel P. Berrangé Reviewed-by: Juan Quintela Reviewed-by: Peter Xu --- io/channel-socket.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/io/channel-socket.c b/io/channel-socket.c index dc9c165de1..dac9e60c20 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -578,11 +578,17 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc, memcpy(CMSG_DATA(cmsg), fds, fdsize); } -#ifdef QEMU_MSG_ZEROCOPY if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) { +#ifdef QEMU_MSG_ZEROCOPY sflags = MSG_ZEROCOPY; - } +#else + /* + * We expect QIOChannel class entry point to have + * blocked this code path already + */ + g_assert_unreachable(); #endif + } retry: ret = sendmsg(sioc->fd, &msg, sflags); @@ -592,15 +598,13 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc, return QIO_CHANNEL_ERR_BLOCK; case EINTR: goto retry; -#ifdef QEMU_MSG_ZEROCOPY case ENOBUFS: - if (sflags & MSG_ZEROCOPY) { + if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) { error_setg_errno(errp, errno, "Process can't lock enough memory for using MSG_ZEROCOPY"); return -1; } break; -#endif } error_setg_errno(errp, errno, From patchwork Mon Jun 20 05:39:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Leonardo Bras X-Patchwork-Id: 12887007 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 095F0C433EF for ; Mon, 20 Jun 2022 05:48:52 +0000 (UTC) Received: from localhost ([::1]:36448 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3AHK-0004li-Tt for qemu-devel@archiver.kernel.org; Mon, 20 Jun 2022 01:48:50 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32778) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A99-0006tq-Ch for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:23 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:44236) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A96-00020w-KO for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655703619; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0Uq2VEJQZ9VOMhyczZgNwmedJnriuLY1lhqwmBG90+s=; b=KYgw3orirAkqVFNmXhIKfTvhelLKebdGtk8nq6NZYl1xaRPFsu2Z8tpuinYBi6A3yqlFpZ GNfKl7VEUNn/YAMy3/ak6mOLJtBG0/7VsRgYwH+Az6OOuYX7c2s19mm/Lo0v5KoNRE+NoW CxZ6TVFAlHLmTqRiN5ChsRDo1JWL92c= Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-557-tzKm_x7_Mp2JB3xvegn4CA-1; Mon, 20 Jun 2022 01:40:13 -0400 X-MC-Unique: tzKm_x7_Mp2JB3xvegn4CA-1 Received: by mail-oi1-f197.google.com with SMTP id bq1-20020a05680823c100b0033151511358so4192437oib.4 for ; Sun, 19 Jun 2022 22:40:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0Uq2VEJQZ9VOMhyczZgNwmedJnriuLY1lhqwmBG90+s=; b=5UsI8aUQ/oNLtTFGn8EAFp+97teX2nclVYfn77rseSvfptnCIdk6zjISZR8kQpLdbl DU+ENmJLmIEa7wl22qfpaTzD7zrsw1zU7tlLbnBe5xsByfwOqn5INVIThZaoSc41lsoT FvMEVwLFdqcQD26thT7FMXYUzfz7INpc7vNkufMEFvs1EMZ/cbrvKFnBTE+R42a2Dm2k b0YH5P+EhK9sqpbNEHG4PqkDUWINHal0c1VReWIpYZtD3uDmu4BE89TwzIapLhPdiQEP 1NKltXLFpnhH0iS5GrgrkJfJUI9FoZ626bxSarS8Ix0FWkzo694Ds6KqMx2cmj/L3Py5 WBZw== X-Gm-Message-State: AJIora/R7QX+bDppBLMQr1qlMPDczZ0uwHYSm0I93uprc3ih9sdVnG4m hYub26d472ldG3QDS/EH4CPGs9DxVC0tmsnVsCkwjkH4uakr2uS14WVdg1D31LBr70ONVhnUuhv HbPHtv3tdQVOBA8M= X-Received: by 2002:a05:6808:1898:b0:331:4343:7637 with SMTP id bi24-20020a056808189800b0033143437637mr9333909oib.83.1655703612837; Sun, 19 Jun 2022 22:40:12 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sCTVlQuNaWwdy7xW3ldepRembLilNdxHJXW7iIweOTIwhj0g16P3lR5ZKHAMPB/UjJSKJMww== X-Received: by 2002:a05:6808:1898:b0:331:4343:7637 with SMTP id bi24-20020a056808189800b0033143437637mr9333900oib.83.1655703612668; Sun, 19 Jun 2022 22:40:12 -0700 (PDT) Received: from localhost.localdomain ([2804:431:c7f0:d933:8f52:267a:639c:b4c0]) by smtp.gmail.com with ESMTPSA id e94-20020a9d2ae7000000b0060c2845c175sm7021463otb.8.2022.06.19.22.40.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 22:40:12 -0700 (PDT) From: Leonardo Bras To: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Eric Blake , Markus Armbruster , Peter Xu , =?utf-8?b?5b6Q6Zev?= Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [PATCH v4 2/4] QIOChannelSocket: Fix zero-copy send so socket flush works Date: Mon, 20 Jun 2022 02:39:43 -0300 Message-Id: <20220620053944.257547-3-leobras@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620053944.257547-1-leobras@redhat.com> References: <20220620053944.257547-1-leobras@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Somewhere between v6 and v7 the of the zero-copy-send patchset a crucial part of the flushing mechanism got missing: incrementing zero_copy_queued. Without that, the flushing interface becomes a no-op, and there is no guarantee the buffer is really sent. This can go as bad as causing a corruption in RAM during migration. Fixes: 2bc58ffc2926 ("QIOChannelSocket: Implement io_writev zero copy flag & io_flush for CONFIG_LINUX") Reported-by: 徐闯 Signed-off-by: Leonardo Bras Reviewed-by: Daniel P. Berrangé Reviewed-by: Juan Quintela Reviewed-by: Peter Xu --- io/channel-socket.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/io/channel-socket.c b/io/channel-socket.c index dac9e60c20..4fa0402f54 100644 --- a/io/channel-socket.c +++ b/io/channel-socket.c @@ -611,6 +611,11 @@ static ssize_t qio_channel_socket_writev(QIOChannel *ioc, "Unable to write to socket"); return -1; } + + if (flags & QIO_CHANNEL_WRITE_FLAG_ZERO_COPY) { + sioc->zero_copy_queued++; + } + return ret; } #else /* WIN32 */ From patchwork Mon Jun 20 05:39:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leonardo Bras X-Patchwork-Id: 12887006 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 19921C433EF for ; Mon, 20 Jun 2022 05:44:55 +0000 (UTC) Received: from localhost ([::1]:58966 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3ADW-0000pW-TL for qemu-devel@archiver.kernel.org; Mon, 20 Jun 2022 01:44:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:60982) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A97-0006sW-Oi for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:21 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:49652) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A96-00020r-25 for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655703619; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=589Y4+IyEVMJNkPFIscZrdM6xrEV5umRGCJxERT4tvQ=; b=DOfANsKXO3hcxw+duHJpUSEpxH8Htb42lKfw+Kaf5p+cUVCXby/AsLJ3Jxw8GWvET81QoC Hvsgv5TKgBjBZe8wHOyZDdJkuQ37NLdSXfupk2OjgFocu8NYO4UVzUB6rWhKiTzbv6KNhF IXAXyccrCBa0vNVuHU6h9Rhhgws/log= Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-41-Vl8zP0ASM-iOcyFSg6My6g-1; Mon, 20 Jun 2022 01:40:16 -0400 X-MC-Unique: Vl8zP0ASM-iOcyFSg6My6g-1 Received: by mail-oi1-f199.google.com with SMTP id bd1-20020a056808220100b0032ed33d656cso5705036oib.21 for ; Sun, 19 Jun 2022 22:40:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=589Y4+IyEVMJNkPFIscZrdM6xrEV5umRGCJxERT4tvQ=; b=zAMns5RmZoeLdBSO0us6i6M4DwlFowlXk/76qKancLYMfgXDHlVFTPBvjUBW9vRDF/ Qg9rk1eL9QvJtrqV0PQa1Iy22MzVo4x1he1Pw1nzymPsZXpKoYnzwPgA17X9xyJKil7s Zx3UkjbFaU2PN4uDOW1mSm0OXjfN2jJReR7hlO+uoiJmoKnHrprqoq0QEE+qu9D9MidW rvn/PZpk3810Z28BPNFcZIb6lGcmnn3FF5rP9szPYs+Y9TZi+k0OxZKBgN/m4dnbL82+ yB6Xfdfj7h588ZOzJASGSGLL5in+thERmQgRdmN/Nl8qKkZsR6cGl9RwPKOGVfPfqna5 bkuA== X-Gm-Message-State: AJIora+xqHP/E2RtELAaHt6PL211Unlj5kgFvZZzA8C5WsOX/2d/Qzza /ufK66keI79WvYQBWmHzt9DihdQVXxTo8FYtw0hTk5DMzWehadlzI1BEPg3m6FFEGgq31gzYmyG Q3AtdgchHaqKb07U= X-Received: by 2002:a05:6870:ac27:b0:101:a6aa:ba21 with SMTP id kw39-20020a056870ac2700b00101a6aaba21mr8091099oab.223.1655703615787; Sun, 19 Jun 2022 22:40:15 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sGoInrhNTNwH9nK9bAPTwxHy1meO9laJGUIplYOpRNAFwyZtNyxcp66YTwp5T+6QOSfTZaUw== X-Received: by 2002:a05:6870:ac27:b0:101:a6aa:ba21 with SMTP id kw39-20020a056870ac2700b00101a6aaba21mr8091089oab.223.1655703615598; Sun, 19 Jun 2022 22:40:15 -0700 (PDT) Received: from localhost.localdomain ([2804:431:c7f0:d933:8f52:267a:639c:b4c0]) by smtp.gmail.com with ESMTPSA id e94-20020a9d2ae7000000b0060c2845c175sm7021463otb.8.2022.06.19.22.40.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 22:40:15 -0700 (PDT) From: Leonardo Bras To: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Eric Blake , Markus Armbruster , Peter Xu , =?utf-8?b?5b6Q6Zev?= Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [PATCH v4 3/4] migration: zero-copy flush only at the end of bitmap scanning Date: Mon, 20 Jun 2022 02:39:44 -0300 Message-Id: <20220620053944.257547-4-leobras@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620053944.257547-1-leobras@redhat.com> References: <20220620053944.257547-1-leobras@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" When sending memory pages with MSG_ZEROCOPY, it's necessary to flush to make sure all dirty pages are sent before a future version of them happens to be sent. Currently, the flush happens every time at the end of ram_save_iterate(), which usually happens around 20x per second, due to a timeout. Change so it flushes only after a whole scanning of the dirty bimap, so it never sends a newer version of a page before an older one, while avoiding unnecessary overhead. Signed-off-by: Leonardo Bras --- migration/multifd.h | 1 + migration/multifd.c | 58 ++++++++++++++++++++++++++------------------- migration/ram.c | 7 ++++++ 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/migration/multifd.h b/migration/multifd.h index 4d8d89e5e5..e7cbdf1fb4 100644 --- a/migration/multifd.h +++ b/migration/multifd.h @@ -22,6 +22,7 @@ bool multifd_recv_new_channel(QIOChannel *ioc, Error **errp); void multifd_recv_sync_main(void); int multifd_send_sync_main(QEMUFile *f); int multifd_queue_page(QEMUFile *f, RAMBlock *block, ram_addr_t offset); +int multifd_zero_copy_flush(void); /* Multifd Compression flags */ #define MULTIFD_FLAG_SYNC (1 << 0) diff --git a/migration/multifd.c b/migration/multifd.c index 9282ab6aa4..ce4220a97d 100644 --- a/migration/multifd.c +++ b/migration/multifd.c @@ -566,10 +566,42 @@ void multifd_save_cleanup(void) multifd_send_state = NULL; } +/* + * Set zero_copy_flush = true for every multifd channel + * + * When using zero-copy, it's necessary to flush the pages before any of + * the pages can be sent again, so we'll make sure the new version of the + * pages will always arrive _later_ than the old pages. + * + * Should be called only after we finished one whole scanning of + * all the dirty bitmaps. + */ +int multifd_zero_copy_flush(void) +{ + int i; + Error *local_err = NULL; + + if (!migrate_use_multifd()) { + return 0; + } + + for (i = 0; i < migrate_multifd_channels(); i++) { + MultiFDSendParams *p = &multifd_send_state->params[i]; + int ret; + + ret = qio_channel_flush(p->c, &local_err); + if (ret < 0) { + error_report_err(local_err); + return ret; + } + } + + return 0; +} + int multifd_send_sync_main(QEMUFile *f) { int i; - bool flush_zero_copy; if (!migrate_use_multifd()) { return 0; @@ -581,19 +613,6 @@ int multifd_send_sync_main(QEMUFile *f) } } - /* - * When using zero-copy, it's necessary to flush the pages before any of - * the pages can be sent again, so we'll make sure the new version of the - * pages will always arrive _later_ than the old pages. - * - * Currently we achieve this by flushing the zero-page requested writes - * per ram iteration, but in the future we could potentially optimize it - * to be less frequent, e.g. only after we finished one whole scanning of - * all the dirty bitmaps. - */ - - flush_zero_copy = migrate_use_zero_copy_send(); - for (i = 0; i < migrate_multifd_channels(); i++) { MultiFDSendParams *p = &multifd_send_state->params[i]; @@ -615,17 +634,6 @@ int multifd_send_sync_main(QEMUFile *f) ram_counters.transferred += p->packet_len; qemu_mutex_unlock(&p->mutex); qemu_sem_post(&p->sem); - - if (flush_zero_copy && p->c) { - int ret; - Error *err = NULL; - - ret = qio_channel_flush(p->c, &err); - if (ret < 0) { - error_report_err(err); - return -1; - } - } } for (i = 0; i < migrate_multifd_channels(); i++) { MultiFDSendParams *p = &multifd_send_state->params[i]; diff --git a/migration/ram.c b/migration/ram.c index 5f5e37f64d..514584e44f 100644 --- a/migration/ram.c +++ b/migration/ram.c @@ -2288,6 +2288,13 @@ static int ram_find_and_save_block(RAMState *rs) rs->last_seen_block = pss.block; rs->last_page = pss.page; + if (pss.complete_round && migrate_use_zero_copy_send()) { + int ret = multifd_zero_copy_flush(); + if (ret < 0) { + return ret; + } + } + return pages; } From patchwork Mon Jun 20 05:39:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leonardo Bras X-Patchwork-Id: 12887008 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C2A66C43334 for ; Mon, 20 Jun 2022 05:50:33 +0000 (UTC) Received: from localhost ([::1]:39950 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1o3AIy-00078B-Ne for qemu-devel@archiver.kernel.org; Mon, 20 Jun 2022 01:50:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32782) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A9A-0006us-Kj for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:21607) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1o3A98-000216-2s for qemu-devel@nongnu.org; Mon, 20 Jun 2022 01:40:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655703621; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ta+ktHN2eHLDu5TN2wA+rMHpESjXFGe7m1hALLgkkMI=; b=Glb3m3zmDb74EXex+hvP3sGWz29buhvdzwStZyuvWnX1Ys257+48TKc2PElIttIrlSj0yd PJU6JTz9fk9E3ZmbABu32W2gGqWryO0GCOx1JTchdMtAWQpOfsZ+SRt4BS97f53uJeR8S4 7urFsAdQIUWjrC5PHlBBm+bkcSWdIto= Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-673-_ckG5aTVPJOcaB5bEtksWQ-1; Mon, 20 Jun 2022 01:40:19 -0400 X-MC-Unique: _ckG5aTVPJOcaB5bEtksWQ-1 Received: by mail-oi1-f197.google.com with SMTP id e205-20020acab5d6000000b0032f6c02bea5so5734230oif.9 for ; Sun, 19 Jun 2022 22:40:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ta+ktHN2eHLDu5TN2wA+rMHpESjXFGe7m1hALLgkkMI=; b=a1eR18BdxCsR/JwP9+qi/zhzZpih+uECeN6ec6VXRqdQzgXRUGz5Zt4q1EmuHviAjT pmRXx4JWn0NeNKfT9OhY6v9s0Qw01UDyhXuY9QU/vylHa0qNI2pjT/1ZM7GeoakaLCkv tUuapBxjkkZgPndMBpnwAnEBGYLyj25AjmuNdHUnX3bHwSyiehqu+kb9sP+dK/Ry0GXM CIxpBjhwYsmJsp6h2SHSSCGq/6CAmCxamk3mPAUaM0l1uSwaUTM53uDtO3w0JS2wsauQ dFIMx9az0h1RVs9K2vflN7i7SKuyPcHFeUnUC3jWtO2UEDb20knoQswbmirYTEOzfKTY LMzQ== X-Gm-Message-State: AOAM5336FhoCfy8sP5FMYxGkMKV+cjdVnABZt5weiE24AzZnTyIqHHLx hwWAXhdsx0T4wwBoQFvPTjO+HPAxQVq3nW4yjWhZbzqQ+OKa69d1NnvxMm1hyt3b8YSdtgnMyzn CX8WFFAqo0mrQ4E0= X-Received: by 2002:a05:6808:1b1f:b0:32f:1309:c4c2 with SMTP id bx31-20020a0568081b1f00b0032f1309c4c2mr15944113oib.297.1655703618888; Sun, 19 Jun 2022 22:40:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpGlPjNP2PEAukyMii6dPwguSZ1vXePCKTDZWyzjVQX14unIgB9p6PSthGH6E9IpYLEhcmFQ== X-Received: by 2002:a05:6808:1b1f:b0:32f:1309:c4c2 with SMTP id bx31-20020a0568081b1f00b0032f1309c4c2mr15944102oib.297.1655703618638; Sun, 19 Jun 2022 22:40:18 -0700 (PDT) Received: from localhost.localdomain ([2804:431:c7f0:d933:8f52:267a:639c:b4c0]) by smtp.gmail.com with ESMTPSA id e94-20020a9d2ae7000000b0060c2845c175sm7021463otb.8.2022.06.19.22.40.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Jun 2022 22:40:18 -0700 (PDT) From: Leonardo Bras To: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Juan Quintela , "Dr. David Alan Gilbert" , Eric Blake , Markus Armbruster , Peter Xu , =?utf-8?b?5b6Q6Zev?= Cc: Leonardo Bras , qemu-devel@nongnu.org Subject: [PATCH v4 4/4] migration: Change zero_copy_send from migration parameter to migration capability Date: Mon, 20 Jun 2022 02:39:45 -0300 Message-Id: <20220620053944.257547-5-leobras@redhat.com> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220620053944.257547-1-leobras@redhat.com> References: <20220620053944.257547-1-leobras@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=leobras@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -28 X-Spam_score: -2.9 X-Spam_bar: -- X-Spam_report: (-2.9 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.082, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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-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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" When originally implemented, zero_copy_send was designed as a Migration paramenter. But taking into account how is that supposed to work, and how the difference between a capability and a parameter, it only makes sense that zero-copy-send would work better as a capability. Taking into account how recently the change got merged, it was decided that it's still time to make it right, and convert zero_copy_send into a Migration capability. Signed-off-by: Leonardo Bras Reviewed-by: Juan Quintela Acked-by: Peter Xu Acked-by: Markus Armbruster --- qapi/migration.json | 33 ++++++++------------------- migration/migration.c | 52 ++++++++++++++++--------------------------- monitor/hmp-cmds.c | 6 ----- 3 files changed, 28 insertions(+), 63 deletions(-) diff --git a/qapi/migration.json b/qapi/migration.json index 6130cd9fae..baf8d734de 100644 --- a/qapi/migration.json +++ b/qapi/migration.json @@ -461,6 +461,13 @@ # procedure starts. The VM RAM is saved with running VM. # (since 6.0) # +# @zero-copy-send: Controls behavior on sending memory pages on migration. +# When true, enables a zero-copy mechanism for sending +# memory pages, if host supports it. +# Requires that QEMU be permitted to use locked memory +# for guest RAM pages. +# (since 7.1) +# # Features: # @unstable: Members @x-colo and @x-ignore-shared are experimental. # @@ -474,7 +481,8 @@ 'block', 'return-path', 'pause-before-switchover', 'multifd', 'dirty-bitmaps', 'postcopy-blocktime', 'late-block-activate', { 'name': 'x-ignore-shared', 'features': [ 'unstable' ] }, - 'validate-uuid', 'background-snapshot'] } + 'validate-uuid', 'background-snapshot', + { 'name': 'zero-copy-send', 'if' : 'CONFIG_LINUX'}] } ## # @MigrationCapabilityStatus: @@ -738,12 +746,6 @@ # will consume more CPU. # Defaults to 1. (Since 5.0) # -# @zero-copy-send: Controls behavior on sending memory pages on migration. -# When true, enables a zero-copy mechanism for sending -# memory pages, if host supports it. -# Requires that QEMU be permitted to use locked memory -# for guest RAM pages. -# Defaults to false. (Since 7.1) # # @block-bitmap-mapping: Maps block nodes and bitmaps on them to # aliases for the purpose of dirty bitmap migration. Such @@ -784,7 +786,6 @@ 'xbzrle-cache-size', 'max-postcopy-bandwidth', 'max-cpu-throttle', 'multifd-compression', 'multifd-zlib-level' ,'multifd-zstd-level', - { 'name': 'zero-copy-send', 'if' : 'CONFIG_LINUX'}, 'block-bitmap-mapping' ] } ## @@ -911,13 +912,6 @@ # will consume more CPU. # Defaults to 1. (Since 5.0) # -# @zero-copy-send: Controls behavior on sending memory pages on migration. -# When true, enables a zero-copy mechanism for sending -# memory pages, if host supports it. -# Requires that QEMU be permitted to use locked memory -# for guest RAM pages. -# Defaults to false. (Since 7.1) -# # @block-bitmap-mapping: Maps block nodes and bitmaps on them to # aliases for the purpose of dirty bitmap migration. Such # aliases may for example be the corresponding names on the @@ -972,7 +966,6 @@ '*multifd-compression': 'MultiFDCompression', '*multifd-zlib-level': 'uint8', '*multifd-zstd-level': 'uint8', - '*zero-copy-send': { 'type': 'bool', 'if': 'CONFIG_LINUX' }, '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } } ## @@ -1119,13 +1112,6 @@ # will consume more CPU. # Defaults to 1. (Since 5.0) # -# @zero-copy-send: Controls behavior on sending memory pages on migration. -# When true, enables a zero-copy mechanism for sending -# memory pages, if host supports it. -# Requires that QEMU be permitted to use locked memory -# for guest RAM pages. -# Defaults to false. (Since 7.1) -# # @block-bitmap-mapping: Maps block nodes and bitmaps on them to # aliases for the purpose of dirty bitmap migration. Such # aliases may for example be the corresponding names on the @@ -1178,7 +1164,6 @@ '*multifd-compression': 'MultiFDCompression', '*multifd-zlib-level': 'uint8', '*multifd-zstd-level': 'uint8', - '*zero-copy-send': { 'type': 'bool', 'if': 'CONFIG_LINUX' }, '*block-bitmap-mapping': [ 'BitmapMigrationNodeAlias' ] } } ## diff --git a/migration/migration.c b/migration/migration.c index 31739b2af9..cc253d66e3 100644 --- a/migration/migration.c +++ b/migration/migration.c @@ -163,7 +163,8 @@ INITIALIZE_MIGRATE_CAPS_SET(check_caps_background_snapshot, MIGRATION_CAPABILITY_COMPRESS, MIGRATION_CAPABILITY_XBZRLE, MIGRATION_CAPABILITY_X_COLO, - MIGRATION_CAPABILITY_VALIDATE_UUID); + MIGRATION_CAPABILITY_VALIDATE_UUID, + MIGRATION_CAPABILITY_ZERO_COPY_SEND); /* When we add fault tolerance, we could have several migrations at once. For now we don't need to add @@ -910,10 +911,6 @@ MigrationParameters *qmp_query_migrate_parameters(Error **errp) params->multifd_zlib_level = s->parameters.multifd_zlib_level; params->has_multifd_zstd_level = true; params->multifd_zstd_level = s->parameters.multifd_zstd_level; -#ifdef CONFIG_LINUX - params->has_zero_copy_send = true; - params->zero_copy_send = s->parameters.zero_copy_send; -#endif params->has_xbzrle_cache_size = true; params->xbzrle_cache_size = s->parameters.xbzrle_cache_size; params->has_max_postcopy_bandwidth = true; @@ -1275,6 +1272,18 @@ static bool migrate_caps_check(bool *cap_list, } } +#ifdef CONFIG_LINUX + if (cap_list[MIGRATION_CAPABILITY_ZERO_COPY_SEND] && + (!cap_list[MIGRATION_CAPABILITY_MULTIFD] || + migrate_use_compression() || + migrate_use_tls())) { + error_setg(errp, + "Zero copy only available for non-compressed non-TLS multifd migration"); + return false; + } +#endif + + /* incoming side only */ if (runstate_check(RUN_STATE_INMIGRATE) && !migrate_multi_channels_is_allowed() && @@ -1497,16 +1506,6 @@ static bool migrate_params_check(MigrationParameters *params, Error **errp) error_prepend(errp, "Invalid mapping given for block-bitmap-mapping: "); return false; } -#ifdef CONFIG_LINUX - if (params->zero_copy_send && - (!migrate_use_multifd() || - params->multifd_compression != MULTIFD_COMPRESSION_NONE || - (params->tls_creds && *params->tls_creds))) { - error_setg(errp, - "Zero copy only available for non-compressed non-TLS multifd migration"); - return false; - } -#endif return true; } @@ -1580,11 +1579,6 @@ static void migrate_params_test_apply(MigrateSetParameters *params, if (params->has_multifd_compression) { dest->multifd_compression = params->multifd_compression; } -#ifdef CONFIG_LINUX - if (params->has_zero_copy_send) { - dest->zero_copy_send = params->zero_copy_send; - } -#endif if (params->has_xbzrle_cache_size) { dest->xbzrle_cache_size = params->xbzrle_cache_size; } @@ -1697,11 +1691,6 @@ static void migrate_params_apply(MigrateSetParameters *params, Error **errp) if (params->has_multifd_compression) { s->parameters.multifd_compression = params->multifd_compression; } -#ifdef CONFIG_LINUX - if (params->has_zero_copy_send) { - s->parameters.zero_copy_send = params->zero_copy_send; - } -#endif if (params->has_xbzrle_cache_size) { s->parameters.xbzrle_cache_size = params->xbzrle_cache_size; xbzrle_cache_resize(params->xbzrle_cache_size, errp); @@ -2593,7 +2582,7 @@ bool migrate_use_zero_copy_send(void) s = migrate_get_current(); - return s->parameters.zero_copy_send; + return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_COPY_SEND]; } #endif @@ -4249,10 +4238,6 @@ static Property migration_properties[] = { DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState, parameters.multifd_zstd_level, DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL), -#ifdef CONFIG_LINUX - DEFINE_PROP_BOOL("zero_copy_send", MigrationState, - parameters.zero_copy_send, false), -#endif DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState, parameters.xbzrle_cache_size, DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE), @@ -4290,6 +4275,10 @@ static Property migration_properties[] = { DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD), DEFINE_PROP_MIG_CAP("x-background-snapshot", MIGRATION_CAPABILITY_BACKGROUND_SNAPSHOT), +#ifdef CONFIG_LINUX + DEFINE_PROP_MIG_CAP("x-zero-copy-send", + MIGRATION_CAPABILITY_ZERO_COPY_SEND), +#endif DEFINE_PROP_END_OF_LIST(), }; @@ -4350,9 +4339,6 @@ static void migration_instance_init(Object *obj) params->has_multifd_compression = true; params->has_multifd_zlib_level = true; params->has_multifd_zstd_level = true; -#ifdef CONFIG_LINUX - params->has_zero_copy_send = true; -#endif params->has_xbzrle_cache_size = true; params->has_max_postcopy_bandwidth = true; params->has_max_cpu_throttle = true; diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c index 47a27326ee..ca98df0495 100644 --- a/monitor/hmp-cmds.c +++ b/monitor/hmp-cmds.c @@ -1311,12 +1311,6 @@ void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict) p->has_multifd_zstd_level = true; visit_type_uint8(v, param, &p->multifd_zstd_level, &err); break; -#ifdef CONFIG_LINUX - case MIGRATION_PARAMETER_ZERO_COPY_SEND: - p->has_zero_copy_send = true; - visit_type_bool(v, param, &p->zero_copy_send, &err); - break; -#endif case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE: p->has_xbzrle_cache_size = true; if (!visit_type_size(v, param, &cache_size, &err)) {