From nobody Mon Feb 9 21:21:36 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; arc=fail (Bad Signature) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1622563939707488.9884319706247; Tue, 1 Jun 2021 09:12:19 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.134998.251128 (Exim 4.92) (envelope-from ) id 1lo6zt-0002bQ-Qm; Tue, 01 Jun 2021 16:12:05 +0000 Received: by outflank-mailman (output) from mailman id 134998.251128; Tue, 01 Jun 2021 16:12:05 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1lo6zt-0002Zn-IE; Tue, 01 Jun 2021 16:12:05 +0000 Received: by outflank-mailman (input) for mailman id 134998; Tue, 01 Jun 2021 16:12:04 +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 1lo6zr-0005X1-WB for xen-devel@lists.xenproject.org; Tue, 01 Jun 2021 16:12:04 +0000 Received: from mo4-p02-ob.smtp.rzone.de (unknown [85.215.255.82]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 1163992d-2102-4ff6-ab5b-775defa88b61; Tue, 01 Jun 2021 16:11:38 +0000 (UTC) Received: from sender by smtp.strato.de (RZmta 47.27.2 AUTH) with ESMTPSA id j0415bx51GBW1BM (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Tue, 1 Jun 2021 18:11:32 +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: 1163992d-2102-4ff6-ab5b-775defa88b61 ARC-Seal: i=1; a=rsa-sha256; t=1622563893; cv=none; d=strato.com; s=strato-dkim-0002; b=lWWIEL9HC5km8WOt/x3R4+rqn5v1bNFWvkPAbSkrUqYNkp40AlmE6jjiYWVCydo/ob csPuquEqrRfkmBqzA2eOKcigfbX13plTo6qbUGjDiRI4GKM2sgI7cI78zMFo42m/q9CX 6s0EkpCc7ePlmNhxXycWZKTN2DRHoWSNS2/2Ej/YhTGanXeMyP4FO7OlGTrncwoukc5u YZufV3hndVO0e3fWcdoXqwWNQNmfD0tDw8Y/AKVi5AQRnOy9MZ+fsStZIlvZJgzYNR9/ neFvRkC/JRKGOI0Jd7M4pbOdnqmClisEC8v9ruyrOb4GKHSfBOTUTk3EX4+8mrw36AR2 daKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1622563893; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=Dd4NjKNzHYeI+VwgjA8Sp8NfOu58WGHDpSZRBu7+Wsk=; b=G5Ov9oYy7ggp6gy5d/idYUXaeC4ExsmevB8T8+Tk5hYvfGjjVfWqU9hg5pih+WPZU0 UGGfxm+qTKuIzEXYbk7ZnS4k2cTZzxigRIp6WwHub37qlKRhSfhSiS3xy33pvf+2cRtH K9WYQoZa3WTI5Ka+VX7PLWfRAieXMU6UA3YotYlavahITjflfpyO1Orc/sC1sDvN3xfK ySNUQy3fxfLW3LxQNQvgi/JznZAl/TsSTlPo1K4bQAGSRqxPtk2V4XjMN76VbF+p1+iB U+GkoeB0ngJ02Nu+7FSHlXkNI/VSwwZP7fEFfWrCltKyiXlmzvKexU0P++hfxYLx4lpa qWaw== ARC-Authentication-Results: i=1; strato.com; dkim=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1622563893; 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=Dd4NjKNzHYeI+VwgjA8Sp8NfOu58WGHDpSZRBu7+Wsk=; b=l5ArMBsKjU3Cb1YbyVURF8KFBTeNuE4kMQPXKk54t54hF8bKl8uN5LntfGydG/oOXT sjjbN7zNm6V+sjUOlxEFhYY5Wowydxq/jH4fHpbS8zrvVRWcXMn/8DX9l/gS3cD9h7K8 4C2uaU6MIHe9c/qQHO6OqvNinsxN7g38ru7a3wM3G3qi1EYxD+D6AUtXVxNGjKeErbVi K0myJDlx3iOOWToTjmcUqVEJb6Z4P1SXAbYvjOU5ROPMfRNw0heamWJ5zde9HcmjWVcx fwSjDvdVkySrA3GUophNuENcxZIwwoBjv6AzQP9iQeeBywPAESZtn31N+0BhWGUY2/OO kmvg== Authentication-Results: strato.com; dkim=none X-RZG-AUTH: ":P2EQZWCpfu+qG7CngxMFH1J+3q8wa/QXkBR9MXjAuzpIG0mv9coXAgs09ValFcstyKtnZMLOo4jr88Zf5nXI1mYJUK+h" X-RZG-CLASS-ID: mo00 From: Olaf Hering To: xen-devel@lists.xenproject.org Cc: Olaf Hering , Ian Jackson , Wei Liu Subject: [PATCH v20210601 17/38] tools/guest: save: move local_pages array Date: Tue, 1 Jun 2021 18:10:57 +0200 Message-Id: <20210601161118.18986-18-olaf@aepfle.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210601161118.18986-1-olaf@aepfle.de> References: <20210601161118.18986-1-olaf@aepfle.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Remove allocation from hotpath, move local_pages array into preallocated sp= ace. Adjust the code to use the src page as is in case of HVM. In case of PV the page may need to be normalised, use an private memory area for this purpose. Signed-off-by: Olaf Hering Reviewed-by: Juergen Gross --- tools/libs/saverestore/common.h | 22 ++++++++++--------- tools/libs/saverestore/save.c | 25 +++------------------ tools/libs/saverestore/save_x86_hvm.c | 5 +++-- tools/libs/saverestore/save_x86_pv.c | 31 ++++++++++++++++++--------- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/tools/libs/saverestore/common.h b/tools/libs/saverestore/commo= n.h index 098aa39667..0e03b0731c 100644 --- a/tools/libs/saverestore/common.h +++ b/tools/libs/saverestore/common.h @@ -45,16 +45,12 @@ struct xc_sr_save_ops * Optionally transform the contents of a page from being specific to = the * sending environment, to being generic for the stream. * - * The page of data at the end of 'page' may be a read-only mapping of= a - * running guest; it must not be modified. If no transformation is - * required, the callee should leave '*pages' untouched. + * The page of data '*src' may be a read-only mapping of a running gue= st; + * it must not be modified. If no transformation is required, the call= ee + * should leave '*src' untouched, and return it via '**ptr'. * - * If a transformation is required, the callee should allocate themsel= ves - * a local page using malloc() and return it via '*page'. - * - * The caller shall free() '*page' in all cases. In the case that the - * callee encounters an error, it should *NOT* free() the memory it - * allocated for '*page'. + * If a transformation is required, the callee should provide the + * transformed page in a private buffer and return it via '**ptr'. * * It is valid to fail with EAGAIN if the transformation is not able t= o be * completed at this point. The page shall be retried later. @@ -62,7 +58,7 @@ struct xc_sr_save_ops * @returns 0 for success, -1 for failure, with errno appropriately se= t. */ int (*normalise_page)(struct xc_sr_context *ctx, xen_pfn_t type, - void **page); + void *src, unsigned int idx, void **ptr); =20 /** * Set up local environment to save a domain. (Typically querying @@ -383,6 +379,12 @@ struct xc_sr_context =20 union { + struct + { + /* Used by write_batch for modified pages. */ + void *normalised_pages; + } save; + struct { /* State machine for the order of received records= . */ diff --git a/tools/libs/saverestore/save.c b/tools/libs/saverestore/save.c index c4fd9a15f0..c4876ba24c 100644 --- a/tools/libs/saverestore/save.c +++ b/tools/libs/saverestore/save.c @@ -91,11 +91,10 @@ static int write_batch(struct xc_sr_context *ctx) xen_pfn_t *mfns =3D ctx->save.m->mfns, *types =3D ctx->save.m->types; void *guest_mapping =3D NULL; void **guest_data =3D ctx->save.m->guest_data; - void **local_pages =3D NULL; int *errors =3D ctx->save.m->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; + void *src; uint64_t *rec_pfns =3D ctx->save.m->rec_pfns; struct iovec *iov =3D ctx->save.m->iov; int iovcnt =3D 0; struct xc_sr_rec_page_data_header hdr =3D { 0 }; @@ -105,16 +104,6 @@ static int write_batch(struct xc_sr_context *ctx) =20 assert(nr_pfns !=3D 0); =20 - /* Pointers to locally allocated pages. Need freeing. */ - local_pages =3D calloc(nr_pfns, sizeof(*local_pages)); - - if ( !local_pages ) - { - ERROR("Unable to allocate arrays for a batch of %u pages", - nr_pfns); - goto err; - } - for ( i =3D 0; i < nr_pfns; ++i ) { types[i] =3D mfns[i] =3D ctx->save.ops.pfn_to_gfn(ctx, @@ -176,11 +165,8 @@ static int write_batch(struct xc_sr_context *ctx) goto err; } =20 - orig_page =3D page =3D guest_mapping + (p * PAGE_SIZE); - rc =3D ctx->save.ops.normalise_page(ctx, types[i], &page); - - if ( orig_page !=3D page ) - local_pages[i] =3D page; + src =3D guest_mapping + (p * PAGE_SIZE); + rc =3D ctx->save.ops.normalise_page(ctx, types[i], src, i, &gu= est_data[i]); =20 if ( rc ) { @@ -195,8 +181,6 @@ static int write_batch(struct xc_sr_context *ctx) else goto err; } - else - guest_data[i] =3D page; =20 rc =3D -1; ++p; @@ -255,9 +239,6 @@ static int write_batch(struct xc_sr_context *ctx) err: 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(local_pages); =20 return rc; } diff --git a/tools/libs/saverestore/save_x86_hvm.c b/tools/libs/saverestore= /save_x86_hvm.c index 91c2cb99ab..26f49ee267 100644 --- a/tools/libs/saverestore/save_x86_hvm.c +++ b/tools/libs/saverestore/save_x86_hvm.c @@ -129,9 +129,10 @@ static xen_pfn_t x86_hvm_pfn_to_gfn(const struct xc_sr= _context *ctx, return pfn; } =20 -static int x86_hvm_normalise_page(struct xc_sr_context *ctx, - xen_pfn_t type, void **page) +static int x86_hvm_normalise_page(struct xc_sr_context *ctx, xen_pfn_t typ= e, + void *src, unsigned int idx, void **ptr) { + *ptr =3D src; return 0; } =20 diff --git a/tools/libs/saverestore/save_x86_pv.c b/tools/libs/saverestore/= save_x86_pv.c index 92f77fad0f..159ff59480 100644 --- a/tools/libs/saverestore/save_x86_pv.c +++ b/tools/libs/saverestore/save_x86_pv.c @@ -999,29 +999,31 @@ static xen_pfn_t x86_pv_pfn_to_gfn(const struct xc_sr= _context *ctx, * save_ops function. Performs pagetable normalisation on appropriate pag= es. */ static int x86_pv_normalise_page(struct xc_sr_context *ctx, xen_pfn_t type, - void **page) + void *src, unsigned int idx, void **ptr) { xc_interface *xch =3D ctx->xch; - void *local_page; int rc; + void *dst; =20 type &=3D XEN_DOMCTL_PFINFO_LTABTYPE_MASK; =20 if ( type < XEN_DOMCTL_PFINFO_L1TAB || type > XEN_DOMCTL_PFINFO_L4TAB ) + { + *ptr =3D src; return 0; + } =20 - local_page =3D malloc(PAGE_SIZE); - if ( !local_page ) + if ( idx >=3D MAX_BATCH_SIZE ) { - ERROR("Unable to allocate scratch page"); - rc =3D -1; - goto out; + ERROR("idx %u out of range", idx); + errno =3D ERANGE; + return -1; } =20 - rc =3D normalise_pagetable(ctx, *page, local_page, type); - *page =3D local_page; + dst =3D ctx->x86.pv.save.normalised_pages + idx * PAGE_SIZE; + rc =3D normalise_pagetable(ctx, src, dst, type); + *ptr =3D dst; =20 - out: return rc; } =20 @@ -1031,8 +1033,16 @@ static int x86_pv_normalise_page(struct xc_sr_contex= t *ctx, xen_pfn_t type, */ static int x86_pv_setup(struct xc_sr_context *ctx) { + xc_interface *xch =3D ctx->xch; int rc; =20 + ctx->x86.pv.save.normalised_pages =3D malloc(MAX_BATCH_SIZE * PAGE_SIZ= E); + if ( !ctx->x86.pv.save.normalised_pages ) + { + PERROR("Failed to allocate normalised_pages"); + return -1; + } + rc =3D x86_pv_domain_info(ctx); if ( rc ) return rc; @@ -1118,6 +1128,7 @@ static int x86_pv_check_vm_state(struct xc_sr_context= *ctx) =20 static int x86_pv_cleanup(struct xc_sr_context *ctx) { + free(ctx->x86.pv.save.normalised_pages); free(ctx->x86.pv.p2m_pfns); =20 if ( ctx->x86.pv.p2m )