From nobody Wed Sep 23 00:52:01 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=pass; 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1776974685; cv=none; d=zohomail.com; s=zohoarc; b=IHKj3ZU5b3Zmrl4dFZFhXimxKcDfaq9Vpg8jZLLowvedi8C7mb2QCjxjN7kerFtVeyEBXQvlwDgesA0SNqam+Acf6WpC5nz1sQkGp4iWxw9Bbn7t8pAnXh1DxBJki4IJSO/ZNZSp7eIlc6LcXUctu265/9K7MguJpw17MK4rO8s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1776974685; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=0sgCfk1ugKuxjrB/yyEc4hgcj8W8SrxyN2ccMLOE0JE=; b=aV3d12R/tCcf+MjTmZWqYxvUfPvwydndWko3zS+yX2FIGSMhXvpOUkO9qfMohKAW4vkxKunDUY77rwsbMANMRvrG29Oh14GIT4cOGSiMeqckfi1n4rDAUfEySHhSvJwfgjBPHKNZPoJzEtW9pMK6qes/K8I5vrPy9cJF+nbTZsA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1776974685911807.6692026405391; Thu, 23 Apr 2026 13:04:45 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1292629.1570997 (Exim 4.92) (envelope-from ) id 1wG0He-0007wY-2O; Thu, 23 Apr 2026 20:04:22 +0000 Received: by outflank-mailman (output) from mailman id 1292629.1570997; Thu, 23 Apr 2026 20:04:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wG0Hd-0007wC-TN; Thu, 23 Apr 2026 20:04:21 +0000 Received: by outflank-mailman (input) for mailman id 1292629; Thu, 23 Apr 2026 20:04:21 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wG0Hd-0007ic-3T for xen-devel@lists.xenproject.org; Thu, 23 Apr 2026 20:04:21 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wG0Hc-008AbV-GX for xen-devel@lists.xenproject.org; Thu, 23 Apr 2026 22:04:20 +0200 Received: from [10.42.69.7] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 69ea7b23-bab6-0a2a0a5309dd-0a2a4507ec64-26 for ; Thu, 23 Apr 2026 22:04:20 +0200 Received: from [209.85.221.42] (helo=mail-wr1-f42.google.com) by tlsNG-ef75cf.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 69ea7b44-229c-0a2a45070019-d155dd2aa98d-3 for ; Thu, 23 Apr 2026 22:04:20 +0200 Received: by mail-wr1-f42.google.com with SMTP id ffacd0b85a97d-43d75312379so5163604f8f.1 for ; Thu, 23 Apr 2026 13:04:20 -0700 (PDT) Received: from localhost.localdomain (5.116.208.46.dyn.plus.net. [46.208.116.5]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43fe4e3a341sm60065442f8f.24.2026.04.23.13.04.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 13:04:18 -0700 (PDT) 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" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20251104 header.d=gmail.com header.i="@gmail.com" header.h="Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776974660; x=1777579460; darn=lists.xenproject.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=0sgCfk1ugKuxjrB/yyEc4hgcj8W8SrxyN2ccMLOE0JE=; b=e8vzlf144FcZgCJw25hC3LekHi/M5vQK86eIo+5Ri5J6+ZM/NOq1EniM9CWWcjsYNI +nVwxnbQgxBf+KPIObPrP4hwiabdxob7ckTt05ft+hFga4Wrc6s6vgCv75fgH/Jn7kAA spjpWblnr3F4qqaLsLMxnswOvlJNm7P/onx1zvlYUkXZ5KMSdGAadhM6k0Mvx5fTqusO Mr4QLHYJaGmi0t8mlaphAPlGQSs2MGmshM5bWh0Z7lpfOHd2kNjwHV+5vZfAdK4F6pNO Av5tQ9zivigeTfyuU/o48UwgegZzB3s5wTCjei3HLRl7kWg0Ed2zc31lajadmFIw4bvZ LDLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776974660; x=1777579460; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=0sgCfk1ugKuxjrB/yyEc4hgcj8W8SrxyN2ccMLOE0JE=; b=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44130d8f040mr2937021f8f.6.1776974659411; Thu, 23 Apr 2026 13:04:19 -0700 (PDT) From: Frediano Ziglio X-Google-Original-From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: =?UTF-8?q?Edwin=20T=C3=B6r=C3=B6k?= , Anthony PERARD , Juergen Gross , Frediano Ziglio Subject: [PATCH 4/4] tools/libs/guest: allocate various migration arrays just once Date: Thu, 23 Apr 2026 21:03:59 +0100 Message-ID: <20260423200406.28178-5-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260423200406.28178-1-frediano.ziglio@cloud.com> References: <20260423200406.28178-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ef75cf/1776974660-2BD7FC48-7DD3B721/0/0 X-purgate-type: clean X-purgate-size: 6401 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1776974688015154100 From: Edwin T=C3=B6r=C3=B6k Allocate these array just once at the start of migration, using the maximum batch size, and free them at the end. Signed-off-by: Edwin T=C3=B6r=C3=B6k Signed-off-by: Frediano Ziglio --- tools/libs/guest/xg_sr_common.h | 13 +++++++ tools/libs/guest/xg_sr_save.c | 66 +++++++++++++-------------------- 2 files changed, 39 insertions(+), 40 deletions(-) diff --git a/tools/libs/guest/xg_sr_common.h b/tools/libs/guest/xg_sr_commo= n.h index f1573aefcb..77312bae9c 100644 --- a/tools/libs/guest/xg_sr_common.h +++ b/tools/libs/guest/xg_sr_common.h @@ -209,6 +209,18 @@ static inline int update_blob(struct xc_sr_blob *blob, return 0; } =20 +struct xc_sr_context_save_buffers +{ + xen_pfn_t batch_pfns[MAX_BATCH_SIZE]; + xen_pfn_t mfns[MAX_BATCH_SIZE]; + xen_pfn_t types[MAX_BATCH_SIZE]; + int errors[MAX_BATCH_SIZE]; + void *guest_data[MAX_BATCH_SIZE]; + void *local_pages[MAX_BATCH_SIZE]; + struct iovec iov[MAX_BATCH_SIZE + 2]; /* headers + data */ + uint64_t rec_pfns[MAX_BATCH_SIZE]; +}; + struct xc_sr_context { xc_interface *xch; @@ -244,6 +256,7 @@ struct xc_sr_context unsigned long *deferred_pages; unsigned long nr_deferred_pages; xc_hypercall_buffer_t dirty_bitmap_hbuf; + struct xc_sr_context_save_buffers *buffers; } save; =20 struct /* Restore data. */ diff --git a/tools/libs/guest/xg_sr_save.c b/tools/libs/guest/xg_sr_save.c index 1700d81905..64014dcdbd 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -87,16 +87,16 @@ static int write_checkpoint_record(struct xc_sr_context= *ctx) static int write_batch(struct xc_sr_context *ctx) { xc_interface *xch =3D ctx->xch; - xen_pfn_t *mfns =3D NULL, *types =3D NULL; + xen_pfn_t *mfns, *types; void *guest_mapping =3D NULL; - void **guest_data =3D NULL; - void **local_pages =3D NULL; - int *errors =3D NULL, rc =3D -1; + void **guest_data; + void **local_pages; + int *errors, rc =3D -1; unsigned int i, p, nr_pages =3D 0, nr_pages_mapped =3D 0; 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; + uint64_t *rec_pfns; + struct iovec *iov; int iovcnt =3D 0; struct { struct xc_sr_rhdr rec; struct xc_sr_rec_page_data_header page_data; @@ -106,26 +106,24 @@ static int write_batch(struct xc_sr_context *ctx) }; =20 assert(nr_pfns !=3D 0); + assert(nr_pfns <=3D MAX_BATCH_SIZE); + assert(ctx->save.buffers); =20 /* Mfns of the batch pfns. */ - mfns =3D malloc(nr_pfns * sizeof(*mfns)); + mfns =3D ctx->save.buffers->mfns; /* Types of the batch pfns. */ - types =3D malloc(nr_pfns * sizeof(*types)); + types =3D ctx->save.buffers->types; /* Errors from attempting to map the gfns. */ - errors =3D malloc(nr_pfns * sizeof(*errors)); + errors =3D ctx->save.buffers->errors; /* Pointers to page data to send. Mapped gfns or local allocations. */ - guest_data =3D calloc(nr_pfns, sizeof(*guest_data)); + guest_data =3D ctx->save.buffers->guest_data; + memset(guest_data, 0, sizeof(*guest_data) * nr_pfns); /* Pointers to locally allocated pages. Need freeing. */ - local_pages =3D calloc(nr_pfns, sizeof(*local_pages)); + local_pages =3D ctx->save.buffers->local_pages; + memset(local_pages, 0, sizeof(*local_pages) * nr_pfns); /* iovec[] for writev(). */ - iov =3D malloc((nr_pfns + 2) * sizeof(*iov)); - - if ( !mfns || !types || !errors || !guest_data || !local_pages || !iov= ) - { - ERROR("Unable to allocate arrays for a batch of %u pages", - nr_pfns); - goto err; - } + iov =3D ctx->save.buffers->iov; + rec_pfns =3D ctx->save.buffers->rec_pfns; =20 for ( i =3D 0; i < nr_pfns; ++i ) { @@ -211,14 +209,6 @@ static int write_batch(struct xc_sr_context *ctx) } } =20 - rec_pfns =3D malloc(nr_pfns * sizeof(*rec_pfns)); - if ( !rec_pfns ) - { - ERROR("Unable to allocate %zu bytes of memory for page data pfn li= st", - nr_pfns * sizeof(*rec_pfns)); - goto err; - } - hdrs.rec.length =3D sizeof(hdrs.page_data); hdrs.rec.length +=3D nr_pfns * sizeof(*rec_pfns); hdrs.rec.length +=3D nr_pages * PAGE_SIZE; @@ -261,17 +251,13 @@ static int write_batch(struct xc_sr_context *ctx) rc =3D ctx->save.nr_batch_pfns =3D 0; =20 err: - free(rec_pfns); if ( guest_mapping ) 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); - free(errors); - free(types); - free(mfns); + local_pages[i] =3D NULL; + } =20 return rc; } @@ -799,18 +785,18 @@ static int setup(struct xc_sr_context *ctx) =20 dirty_bitmap =3D xc_hypercall_buffer_alloc_pages( xch, dirty_bitmap, NRPAGES(bitmap_size(ctx->save.p2m_size))); - ctx->save.batch_pfns =3D malloc(MAX_BATCH_SIZE * - sizeof(*ctx->save.batch_pfns)); ctx->save.deferred_pages =3D bitmap_alloc(ctx->save.p2m_size); + ctx->save.buffers =3D calloc(1, sizeof(*ctx->save.buffers)); =20 - if ( !ctx->save.batch_pfns || !dirty_bitmap || !ctx->save.deferred_pag= es ) + if ( !dirty_bitmap || !ctx->save.deferred_pages || !ctx->save.buffers) { - ERROR("Unable to allocate memory for dirty bitmaps, batch pfns and" - " deferred pages"); + ERROR("Unable to allocate memory for dirty bitmaps, deferred pages" + " and various batch buffers"); rc =3D -1; errno =3D ENOMEM; goto err; } + ctx->save.batch_pfns =3D ctx->save.buffers->batch_pfns; =20 rc =3D 0; =20 @@ -834,7 +820,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.batch_pfns); + free(ctx->save.buffers); } =20 /* --=20 2.43.0