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.xenproject.org (lists.xenproject.org [192.237.175.120]) (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 53E8CCD5BD1 for ; Tue, 26 May 2026 09:43:57 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1319670.1587163 (Exim 4.92) (envelope-from ) id 1wRoKC-0008Bw-55; Tue, 26 May 2026 09:43:48 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1319670.1587163; Tue, 26 May 2026 09:43:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wRoKC-00089h-0Y; Tue, 26 May 2026 09:43:48 +0000 Received: by outflank-mailman (input) for mailman id 1319670; Tue, 26 May 2026 09:43:47 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wRoKB-0007xP-19 for xen-devel@lists.xenproject.org; Tue, 26 May 2026 09:43:47 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wRoKA-00C7jc-DD for xen-devel@lists.xenproject.org; Tue, 26 May 2026 11:43:46 +0200 Received: from [10.42.69.12] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a156b50-e002-0a2a0a5209dd-0a2a450cb892-8 for ; Tue, 26 May 2026 11:43:46 +0200 Received: from [209.85.221.51] (helo=mail-wr1-f51.google.com) by tlsNG-d25034.mxtls.expurgate.net with ESMTPS (eXpurgate 4.56.1) (envelope-from ) id 6a156b52-62f1-0a2a450c0019-d155dd33c9c3-3 for ; Tue, 26 May 2026 11:43:46 +0200 Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-44c350a5b87so6133174f8f.3 for ; Tue, 26 May 2026 02:43:46 -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-45eb6d70c51sm36522744f8f.36.2026.05.26.02.43.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 May 2026 02:43:44 -0700 (PDT) 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=1779788626; x=1780393426; 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=X41YOolTDeQXd+76PX/UtsluEUpbnvoPlDNitCDAkx0=; b=gB5xUYCE1HltW/j/zJjRQWw/KbUnrfEW7SqE9UnvP0LB1pAyeJrQqNizDPx8bs6x3W QJq4BlwaKmFCm/rzqMgzKoQMVoW5vxkJU9tWvho2KigtGjdPChzrzS6fT7yIzsCZ0mom a+VEspdI1+CrHYYFtE7zjMOsG+o2URQbasldJ+gI8GlvlNzp4i76jL3zM73fNohvWNru ZQzdqBJHP4qBDQ/zCRLce7zuW0cziKnl9hQGLe096BVht2RzJxd9BI4dKbgxMz4tIq4B HCrYY3bzi/gPCBtRVZnqLS7ntw6pdINdlMb9LIprqlWztuzHV5oJDYrC3XdQYoxXGM+0 zE1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779788626; x=1780393426; 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=X41YOolTDeQXd+76PX/UtsluEUpbnvoPlDNitCDAkx0=; b=eFBqBFTQsoxngARZJqH07Pr3TNX6hzq9hULN2qd0S5T9XBHHhrYPJFT1XRPP6YVMH7 9u+Pqtyi1OUjg4z0gtaPO0O3dPYA1smuBTasQPmMp/kAGAyyIN1i69CdsXa3S81hFjEw oXhWGEVyttKrxzCjsdWUhYnPMYbTdWxaMLUcQkocAxSB2dD7JMcUovSJvxijhDRjKEhj GICdIXf5qhxC8DpbbhkKoQoIVKhkh4EWTruJp8HJm8PmoCSFq4Q582bjIPpWpC4u1Abf b6/Oe957v96eHj1vw9HqaUo2lM+W9KeF9VwxjrRmQlg1OckrtIbBoAYOPsse/Y0bTtag R0ew== X-Gm-Message-State: AOJu0YxYw6nn1zpIu7FrXLqGGWTNBnt5t+jognP8jg2s0FJq18r3fhqn 641IqZLjSg9NJRmbknFZ2G5VD3D1yNUgrH/2ZkhEUk/hWQeyyxNp5T8jDmTDgPCF X-Gm-Gg: Acq92OGbksBBO6ypyVqy65gPZjbXKarDCAQH6MGhxhxI4fp+9b6qXz6Ni2mRxotObka v8A/nkPb1SlsV6Mdq9fYi2EHBLRq4AdNjFxP0IVNhdSkn5pzPHS41+7ltjHBOxyJgvvoRXyBfEW invP/oHWq9ZYF9c731bY+1433wOqdH+eMYMQUdhESc9JyKndlNdZMJ6kJVeY9mfizLBsA8ENpj+ mdVHhxpfExnyG9e9psMkB/2wdVEB3EwXhBUDt4/5Lx4rDD1Fu7eoYQpyltH3zNS+FNi4/FD1+ri XlCqNchcQbmeNMukQo333tUbBcLwpxTBPnOEVxrDP9O1HInN2Zk2RDBIgiTW7ofgWgRhbX/KRMp wJd1sfjOR0Mz9GqbNpYgxw3Mo6DbPbczU/fYrc66v5QV79TXHxw+qGir3PfPNe3Gq1UO0xnPKpr v7uJMY9JMP047BY3A1XChlGH6A6ExWFeT8ZYDJL/U9J8YNKzS/E85QIv4uuqSvOyMpkRzyUGX54 SNlM/VNlhBzTx/EOpI/FtFO+0LWvvUyaZgC X-Received: by 2002:a05:6000:25c2:b0:45d:b14b:23f8 with SMTP id ffacd0b85a97d-45eb369044bmr31282502f8f.18.1779788625655; Tue, 26 May 2026 02:43:45 -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?= , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Juergen Gross , Frediano Ziglio Subject: [PATCH v3 5/9] libs/guest: allocate various migration arrays just once Date: Tue, 26 May 2026 10:43:16 +0100 Message-ID: <20260526094324.12286-6-frediano.ziglio@cloud.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260526094324.12286-1-frediano.ziglio@cloud.com> References: <20260526094324.12286-1-frediano.ziglio@cloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-purgate-ID: tlsNG-d25034/1779788626-F4A7ACF5-98C149B4/0/0 X-purgate-type: clean X-purgate-size: 6461 From: Edwin Török 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örök Signed-off-by: Frediano Ziglio -- Changes since v2: - change prefix in subject. --- 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_common.h index f1573aefcbff..77312bae9cf5 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; } +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; struct /* Restore data. */ diff --git a/tools/libs/guest/xg_sr_save.c b/tools/libs/guest/xg_sr_save.c index 8c4e760f8d0a..7d8055a3f9e8 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -86,16 +86,16 @@ static int write_checkpoint_record(struct xc_sr_context *ctx) static int write_batch(struct xc_sr_context *ctx) { xc_interface *xch = ctx->xch; - xen_pfn_t *mfns = NULL, *types = NULL; + xen_pfn_t *mfns, *types; void *guest_mapping = NULL; - void **guest_data = NULL; - void **local_pages = NULL; - int *errors = NULL, rc = -1; + void **guest_data; + void **local_pages; + int *errors, rc = -1; unsigned int i, p, nr_pages = 0, nr_pages_mapped = 0; unsigned int nr_pfns = ctx->save.nr_batch_pfns; void *page, *orig_page; - uint64_t *rec_pfns = NULL; - struct iovec *iov = NULL; int iovcnt = 0; + uint64_t *rec_pfns; + struct iovec *iov; int iovcnt = 0; struct { struct xc_sr_rhdr rec; struct xc_sr_rec_page_data_header page_data; @@ -105,26 +105,24 @@ static int write_batch(struct xc_sr_context *ctx) }; assert(nr_pfns != 0); + assert(nr_pfns <= MAX_BATCH_SIZE); + assert(ctx->save.buffers); /* Mfns of the batch pfns. */ - mfns = malloc(nr_pfns * sizeof(*mfns)); + mfns = ctx->save.buffers->mfns; /* Types of the batch pfns. */ - types = malloc(nr_pfns * sizeof(*types)); + types = ctx->save.buffers->types; /* Errors from attempting to map the gfns. */ - errors = malloc(nr_pfns * sizeof(*errors)); + errors = ctx->save.buffers->errors; /* Pointers to page data to send. Mapped gfns or local allocations. */ - guest_data = calloc(nr_pfns, sizeof(*guest_data)); + guest_data = ctx->save.buffers->guest_data; + memset(guest_data, 0, sizeof(*guest_data) * nr_pfns); /* Pointers to locally allocated pages. Need freeing. */ - local_pages = calloc(nr_pfns, sizeof(*local_pages)); + local_pages = ctx->save.buffers->local_pages; + memset(local_pages, 0, sizeof(*local_pages) * nr_pfns); /* iovec[] for writev(). */ - iov = 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 = ctx->save.buffers->iov; + rec_pfns = ctx->save.buffers->rec_pfns; for ( i = 0; i < nr_pfns; ++i ) { @@ -210,14 +208,6 @@ static int write_batch(struct xc_sr_context *ctx) } } - rec_pfns = malloc(nr_pfns * sizeof(*rec_pfns)); - if ( !rec_pfns ) - { - ERROR("Unable to allocate %zu bytes of memory for page data pfn list", - nr_pfns * sizeof(*rec_pfns)); - goto err; - } - hdrs.rec.length = sizeof(hdrs.page_data); hdrs.rec.length += nr_pfns * sizeof(*rec_pfns); hdrs.rec.length += nr_pages * PAGE_SIZE; @@ -267,17 +257,13 @@ static int write_batch(struct xc_sr_context *ctx) rc = ctx->save.nr_batch_pfns = 0; err: - free(rec_pfns); if ( guest_mapping ) xenforeignmemory_unmap(xch->fmem, guest_mapping, nr_pages_mapped); for ( i = 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] = NULL; + } return rc; } @@ -805,18 +791,18 @@ static int setup(struct xc_sr_context *ctx) dirty_bitmap = xc_hypercall_buffer_alloc_pages( xch, dirty_bitmap, NRPAGES(bitmap_size(ctx->save.p2m_size))); - ctx->save.batch_pfns = malloc(MAX_BATCH_SIZE * - sizeof(*ctx->save.batch_pfns)); ctx->save.deferred_pages = bitmap_alloc(ctx->save.p2m_size); + ctx->save.buffers = calloc(1, sizeof(*ctx->save.buffers)); - if ( !ctx->save.batch_pfns || !dirty_bitmap || !ctx->save.deferred_pages ) + 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 = -1; errno = ENOMEM; goto err; } + ctx->save.batch_pfns = ctx->save.buffers->batch_pfns; rc = 0; @@ -840,7 +826,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); } /* -- 2.54.0