From nobody Tue Feb 10 04:14:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1626199623382572.5211326499672; Tue, 13 Jul 2021 11:07:03 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.155551.287245 (Exim 4.92) (envelope-from ) id 1m3Mnx-0007zy-EL; Tue, 13 Jul 2021 18:06:49 +0000 Received: by outflank-mailman (output) from mailman id 155551.287245; Tue, 13 Jul 2021 18:06:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m3Mnx-0007z7-6i; Tue, 13 Jul 2021 18:06:49 +0000 Received: by outflank-mailman (input) for mailman id 155551; Tue, 13 Jul 2021 18:06:48 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m3Mnv-0002kr-Tv for xen-devel@lists.xenproject.org; Tue, 13 Jul 2021 18:06:47 +0000 Received: from mo4-p02-ob.smtp.rzone.de (unknown [85.215.255.83]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 06e2c55c-e405-11eb-8793-12813bfff9fa; Tue, 13 Jul 2021 18:06:21 +0000 (UTC) Received: from sender by smtp.strato.de (RZmta 47.28.1 AUTH) with ESMTPSA id 30791cx6DI6FtAr (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 13 Jul 2021 20:06:15 +0200 (CEST) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 06e2c55c-e405-11eb-8793-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1626199575; s=strato-dkim-0002; d=aepfle.de; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=VixOpRBMsoM0fDQKIvQSxrobwoztUo22eEi66PpWlks=; b=AVCvEuKrE0LIt1SG29Y5us1wSfMsF0h+fPKTaGaZ1W8adH59yDqT9H6WSPjLOeG3d6 rpKNTUQpwPwQFnoJZHAe244nYJVrojPKzFNAQ+YM9gIwKf0BLXXS69yePFMivqBapQvD FKUhywPEjBhWuktE5tXwjuXXApsN/7w17X4mYRITmpgVLOnSM4+IB+y4tpauw6n9gSQH vdDl7o/FHRHemtvrfZ+6ihl4scNghZeomLPtMWfgUuwPvRHn+oh29q4gJCMchZh5HDHm tou+a0RAQAobxOmqomf0F9VqqfKznq7KPffA66if6z4aQqCaX55dtnXyg0QR4ub1N6q5 LDRQ== Authentication-Results: strato.com; dkim=none X-RZG-AUTH: ":P2EQZWCpfu+qG7CngxMFH1J+3q8wa/QXkBR9MXjAuzpIG0mv9coXAgthqlagRsstpH9XYEN5v9xf7dypFU9ge5p0Oi0V" X-RZG-CLASS-ID: mo00 From: Olaf Hering To: xen-devel@lists.xenproject.org Cc: Olaf Hering , Ian Jackson , Wei Liu , Juergen Gross Subject: [PATCH v20210713 10/31] tools: save: preallocate iov array Date: Tue, 13 Jul 2021 20:05:44 +0200 Message-Id: <20210713180605.12096-11-olaf@aepfle.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210713180605.12096-1-olaf@aepfle.de> References: <20210713180605.12096-1-olaf@aepfle.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1626199624076100002 Content-Type: text/plain; charset="utf-8" Remove repeated allocation from migration loop. There will never be more than MAX_BATCH_SIZE pages to process in a batch. Allocate the space once. Signed-off-by: Olaf Hering --- tools/libs/guest/xg_sr_common.h | 1 + tools/libs/guest/xg_sr_save.c | 34 ++++++++++++++++----------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tools/libs/guest/xg_sr_common.h b/tools/libs/guest/xg_sr_commo= n.h index d2b838ed33..e2a068bb7e 100644 --- a/tools/libs/guest/xg_sr_common.h +++ b/tools/libs/guest/xg_sr_common.h @@ -247,6 +247,7 @@ struct xc_sr_context xen_pfn_t *mfns; xen_pfn_t *types; int *errors; + struct iovec *iov; unsigned int nr_batch_pfns; unsigned long *deferred_pages; unsigned long nr_deferred_pages; diff --git a/tools/libs/guest/xg_sr_save.c b/tools/libs/guest/xg_sr_save.c index 5b416fda60..92eba456e6 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -96,7 +96,7 @@ static int write_batch(struct xc_sr_context *ctx) unsigned int nr_pfns =3D ctx->save.nr_batch_pfns; void *page, *orig_page; uint64_t *rec_pfns =3D NULL; - struct iovec *iov =3D NULL; int iovcnt =3D 0; + int iovcnt =3D 0; struct xc_sr_rec_page_data_header hdr =3D { 0 }; struct xc_sr_record rec =3D { .type =3D REC_TYPE_PAGE_DATA, @@ -108,10 +108,8 @@ static int write_batch(struct xc_sr_context *ctx) guest_data =3D calloc(nr_pfns, sizeof(*guest_data)); /* Pointers to locally allocated pages. Need freeing. */ local_pages =3D calloc(nr_pfns, sizeof(*local_pages)); - /* iovec[] for writev(). */ - iov =3D malloc((nr_pfns + 4) * sizeof(*iov)); =20 - if ( !guest_data || !local_pages || !iov ) + if ( !guest_data || !local_pages ) { ERROR("Unable to allocate arrays for a batch of %u pages", nr_pfns); @@ -221,17 +219,17 @@ static int write_batch(struct xc_sr_context *ctx) for ( i =3D 0; i < nr_pfns; ++i ) rec_pfns[i] =3D ((uint64_t)(ctx->save.types[i]) << 32) | ctx->save= .batch_pfns[i]; =20 - iov[0].iov_base =3D &rec.type; - iov[0].iov_len =3D sizeof(rec.type); + ctx->save.iov[0].iov_base =3D &rec.type; + ctx->save.iov[0].iov_len =3D sizeof(rec.type); =20 - iov[1].iov_base =3D &rec.length; - iov[1].iov_len =3D sizeof(rec.length); + ctx->save.iov[1].iov_base =3D &rec.length; + ctx->save.iov[1].iov_len =3D sizeof(rec.length); =20 - iov[2].iov_base =3D &hdr; - iov[2].iov_len =3D sizeof(hdr); + ctx->save.iov[2].iov_base =3D &hdr; + ctx->save.iov[2].iov_len =3D sizeof(hdr); =20 - iov[3].iov_base =3D rec_pfns; - iov[3].iov_len =3D nr_pfns * sizeof(*rec_pfns); + ctx->save.iov[3].iov_base =3D rec_pfns; + ctx->save.iov[3].iov_len =3D nr_pfns * sizeof(*rec_pfns); =20 iovcnt =3D 4; ctx->save.pages_sent +=3D nr_pages; @@ -243,15 +241,15 @@ static int write_batch(struct xc_sr_context *ctx) { if ( guest_data[i] ) { - iov[iovcnt].iov_base =3D guest_data[i]; - iov[iovcnt].iov_len =3D PAGE_SIZE; + ctx->save.iov[iovcnt].iov_base =3D guest_data[i]; + ctx->save.iov[iovcnt].iov_len =3D PAGE_SIZE; iovcnt++; --nr_pages; } } } =20 - if ( writev_exact(ctx->fd, iov, iovcnt) ) + if ( writev_exact(ctx->fd, ctx->save.iov, iovcnt) ) { PERROR("Failed to write page data to stream"); goto err; @@ -267,7 +265,6 @@ static int write_batch(struct xc_sr_context *ctx) xenforeignmemory_unmap(xch->fmem, guest_mapping, nr_pages_mapped); for ( i =3D 0; local_pages && i < nr_pfns; ++i ) free(local_pages[i]); - free(iov); free(local_pages); free(guest_data); =20 @@ -846,10 +843,12 @@ static int setup(struct xc_sr_context *ctx) ctx->save.mfns =3D malloc(MAX_BATCH_SIZE * sizeof(*ctx->save.mfns)); ctx->save.types =3D malloc(MAX_BATCH_SIZE * sizeof(*ctx->save.types)); ctx->save.errors =3D malloc(MAX_BATCH_SIZE * sizeof(*ctx->save.errors)= ); + ctx->save.iov =3D malloc((4 + MAX_BATCH_SIZE) * sizeof(*ctx->save.iov)= ); ctx->save.deferred_pages =3D bitmap_alloc(ctx->save.p2m_size); =20 if ( !ctx->save.batch_pfns || !ctx->save.mfns || !ctx->save.types || - !ctx->save.errors || !dirty_bitmap || !ctx->save.deferred_pages ) + !ctx->save.errors || !ctx->save.iov || !dirty_bitmap || + !ctx->save.deferred_pages ) { ERROR("Unable to allocate memory for dirty bitmaps, batch pfns and" " deferred pages"); @@ -880,6 +879,7 @@ static void cleanup(struct xc_sr_context *ctx) xc_hypercall_buffer_free_pages(xch, dirty_bitmap, NRPAGES(bitmap_size(ctx->save.p2m_size)= )); free(ctx->save.deferred_pages); + free(ctx->save.iov); free(ctx->save.errors); free(ctx->save.types); free(ctx->save.mfns);