From nobody Thu Jul 23 21:16:27 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=1783804942; cv=none; d=zohomail.com; s=zohoarc; b=ZnB3DJp7NbVWczt/tMNGErMTx/BzvV1buE7Trzs09XIoFFvHhhAHmVZjdr9ER8Hr60st9SVHerPO26A7VxwYpQFB/sLvkB0GjRvP1Tav3QhSAmOgqC29BeWcrDNYNaOx0hwXmnnAUSgkMo8mwAYZ17fp00spq4375sY1LXcPsV0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783804942; 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=yDwLVp2T+0d9NzRyPiF1z4X+HynXJvUa2aJRE8bIVuE=; b=gxIaRb5RQiFwOAOc5NgzfOYmZnfyct9VpSZDuuDtFMKg+4Y+Ubk6plJmBs4X5Lk6LxCBU38tMs9393mgShEiZshJ5GkZuiyjOhiZmAjGqqJcyPErSL0IncSTiJxygyqoluGjtvHKoxLZ6KPX5SGD9gq8iPk0tEWcPDXUCFt64pQ= 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 17838049422552.820553792017563; Sat, 11 Jul 2026 14:22:22 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1360642.1613390 (Exim 4.92) (envelope-from ) id 1wif8l-0007hd-Pi; Sat, 11 Jul 2026 21:21:39 +0000 Received: by outflank-mailman (output) from mailman id 1360642.1613390; Sat, 11 Jul 2026 21:21:39 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8l-0007hW-M6; Sat, 11 Jul 2026 21:21:39 +0000 Received: by outflank-mailman (input) for mailman id 1360642; Sat, 11 Jul 2026 21:21:38 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8k-0007Uj-Pk for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 21:21:38 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wif8j-002lyX-Is for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 23:21:37 +0200 Received: from [10.42.69.7] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a52b3c8-2eae-0a2a0a5409dd-0a2a4507a3a0-4 for ; Sat, 11 Jul 2026 23:21:37 +0200 Received: from [209.85.221.51] (helo=mail-wr1-f51.google.com) by tlsNG-ef75cf.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a52b3e1-ac46-0a2a45070019-d155dd33a40f-3 for ; Sat, 11 Jul 2026 23:21:37 +0200 Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-4758bd3731bso1326941f8f.0 for ; Sat, 11 Jul 2026 14:21:37 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47a9de1d8cdsm74809460f8f.1.2026.07.11.14.21.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 14:21:36 -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:Content-Type: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=1783804897; x=1784409697; darn=lists.xenproject.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=yDwLVp2T+0d9NzRyPiF1z4X+HynXJvUa2aJRE8bIVuE=; b=lyMN//Ky5+EBkXOjZSXcQP5Gc8zGSaH1IJK7/F/CJ/HU8PirQWMKmqMv4Nt2MMgXoz nlj8qoXPaX0TmZc6Tb7kUupIB9FxfR7yju24pyimb0W5rCd8MMPQC3J24d06P4DGxMtR nO0cAeQ80yRxIZsmtuFUqcAl/WfKcXPw3yygJ24p8azmHMUTFAPL16IqM2hSzwX3frMi im/EqDX6dsMOTqKNBiW80xPgkwU2zCf08xY0W+jJ/zJeyIoXx8Ql8JCiO6OI1Z9Y42Mf vECgXKCGYyJ0eZg79O2AaFvif2YwoYVoDONmK4xEGHHljJ2wZKd0flk/MPOUcgAqF1PX EGNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783804897; x=1784409697; h=content-transfer-encoding:content-type: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 :content-type; bh=yDwLVp2T+0d9NzRyPiF1z4X+HynXJvUa2aJRE8bIVuE=; b=pw2INfSQ3+0pmDJjRVLqdRu559pOR5OwV2yBb0+eBLSkPaB4R/hW07wBVqWIUFRfq4 /XaeKZzeymvChTARkDr0WcogseB4yg6PtPL3h9I8lneCxiRs4Ke8PYUBPF6YIerjMguF dpgPzLXyjiuRt1u+9s2IZTHK7Y0G/MslawD75b/kpOPiWlnqzxtu1OIwXgheSCrLXjwS wr8v0pBNvpHGtWu1ylWQxhxdeMAEG+LhQs3pV0pdZ4bNSzfJQPphn8m2CWU55GDgH1LV /FdFFWGHq9Us0iozraOEy3pLcZfREdQ7130mtzIn/tq1Q8xNGadvz9R7PaMnxfaHquci P+2w== X-Gm-Message-State: AOJu0YzzJhe9zL/kWDB/e5dB7n5aNQAV6tFpv7rB3V0YmKgh9N4s9AJc WaSuzqIMgWRLHuA+WfQ3tCTEyUqP3FF736HUzKMFKBFbjG44UksNxDvh8DJRWa7M X-Gm-Gg: AfdE7ckFNMlaKQ5wauoUScyopOFqb84U4qSYhvRugNDkc9Kl5VnM3AeHL2ct8ugif7F R6Rqdh7nHeBc1cWo3bl/mgpQMUtRhztsqhklqo/sqGS2FRCmSCQYy3mN6BT4DQxzqJ5S9HOlpZF /PrkLy7ym+MZFzouHFtgr6Vp3oCGJkuYa4JGYzYm3Wnleii/Ik27lKmO2Hz8pp4EDrB7nNNkgmb 4bOogOXrak2qsKp2WoJUZpLp9NldRE5Tqa6aHMWDmV4wtG2EDB4P1LnBD9HuIYD2UtNR46S/9yh xA9PBMBcBt7MaQXYm2AOxgBizFEmnW9aUw7ON4aunEZmVjWY2cPwVr6J+aTgeADYqv8TwqZhaR/ uerwNXQ/XVHsD5FByuSn9n3UL4ZYYDz+z1wlyP1/RoAa1j2KQb+/09uJQKfPq2Sbb+4MoqEQvTK /PefuQrF0U2OYRuIqR6Pj8WEFI1c1iCrjyW9rSDPTq3I9pZsXjI4XtwSmUUjhrBRVpqzGPe3Joo 6op4fMTRYwYA4f1bcM= X-Received: by 2002:a05:6000:2382:b0:472:1424:fc7c with SMTP id ffacd0b85a97d-47ef695ba2fmr10411461f8f.10.1783804896778; Sat, 11 Jul 2026 14:21:36 -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 v8 1/4] libs/call: cache up to 4 pages in hypercall bounce buffers Date: Sat, 11 Jul 2026 22:21:25 +0100 Message-ID: <20260711212128.505727-2-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260711212128.505727-1-frediano.ziglio@citrix.com> References: <20260711212128.505727-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ef75cf/1783804897-D8E36201-76D78473/0/0 X-purgate-type: clean X-purgate-size: 5606 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783804944637154100 From: Edwin T=C3=B6r=C3=B6k During migration there are a lot of mmap/munmap calls, because xc_get_pfn_type_batch() exceeds the default hypercall bounce buffer cache size, and needs to allocate every time it is called. munmap() is slow, especially in a PV Dom0 (takes an emulation fault), so is best avoided. Eventually it'd be good if the memory pool from xmalloc_tlsf.c was reused here, but for now make it handle the commonly encountered sizes (so far up to 4 pages). Signed-off-by: Edwin T=C3=B6r=C3=B6k Signed-off-by: Frediano Ziglio Reviewed-by: Anthony PERARD --- Changes since v2: - change prefix in subject. Changes since v4: - fix off-by-one bug. Changes since v5: - avoids potential buffer underflow if nr_pages is 0 calling cache_alloc. Changes since v6: - align changes made to cache_alloc to cache_free. Changes since v7: - use "unsigned int" instead of "unsigned". --- tools/libs/call/buffer.c | 34 +++++++++++++++++++++++----------- tools/libs/call/core.c | 3 ++- tools/libs/call/private.h | 8 +++++--- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/tools/libs/call/buffer.c b/tools/libs/call/buffer.c index 155e4f9d43..b7d00185c4 100644 --- a/tools/libs/call/buffer.c +++ b/tools/libs/call/buffer.c @@ -49,6 +49,9 @@ static void *cache_alloc(xencall_handle *xcall, size_t nr= _pages) { void *p =3D NULL; =20 + if ( nr_pages =3D=3D 0 ) + return NULL; + cache_lock(xcall); =20 xcall->buffer_total_allocations++; @@ -56,13 +59,13 @@ static void *cache_alloc(xencall_handle *xcall, size_t = nr_pages) if ( xcall->buffer_current_allocations > xcall->buffer_maximum_allocat= ions ) xcall->buffer_maximum_allocations =3D xcall->buffer_current_alloca= tions; =20 - if ( nr_pages > 1 ) + if ( nr_pages > ARRAY_SIZE(xcall->buffer_cache) ) { xcall->buffer_cache_toobig++; } - else if ( xcall->buffer_cache_nr > 0 ) + else if ( xcall->buffer_cache_nr[nr_pages-1] > 0 ) { - p =3D xcall->buffer_cache[--xcall->buffer_cache_nr]; + p =3D xcall->buffer_cache[nr_pages-1][--xcall->buffer_cache_nr[nr_= pages-1]]; xcall->buffer_cache_hits++; } else @@ -79,15 +82,18 @@ static int cache_free(xencall_handle *xcall, void *p, s= ize_t nr_pages) { int rc =3D 0; =20 + if ( nr_pages =3D=3D 0 ) + return 0; + cache_lock(xcall); =20 xcall->buffer_total_releases++; xcall->buffer_current_allocations--; =20 - if ( nr_pages =3D=3D 1 && - xcall->buffer_cache_nr < BUFFER_CACHE_SIZE ) + if ( nr_pages && nr_pages <=3D ARRAY_SIZE(xcall->buffer_cache) && + xcall->buffer_cache_nr[nr_pages-1] < BUFFER_CACHE_SIZE ) { - xcall->buffer_cache[xcall->buffer_cache_nr++] =3D p; + xcall->buffer_cache[nr_pages-1][xcall->buffer_cache_nr[nr_pages-1]= ++] =3D p; rc =3D 1; } =20 @@ -108,17 +114,23 @@ void buffer_release_cache(xencall_handle *xcall) DBGPRINTF("current allocations:%d maximum allocations:%d", xcall->buffer_current_allocations, xcall->buffer_maximum_allocations); - DBGPRINTF("cache current size:%d", - xcall->buffer_cache_nr); + for ( unsigned int i =3D 0; i < ARRAY_SIZE(xcall->buffer_cache_nr); ++= i ) + { + DBGPRINTF("cache current size[%u pages]:%d", i+1, + xcall->buffer_cache_nr[i]); + } DBGPRINTF("cache hits:%d misses:%d toobig:%d", xcall->buffer_cache_hits, xcall->buffer_cache_misses, xcall->buffer_cache_toobig); =20 - while ( xcall->buffer_cache_nr > 0 ) + for ( unsigned int i =3D 0; i < ARRAY_SIZE(xcall->buffer_cache_nr); ++= i ) { - p =3D xcall->buffer_cache[--xcall->buffer_cache_nr]; - osdep_free_pages(xcall, p, 1); + while ( xcall->buffer_cache_nr[i] > 0 ) + { + p =3D xcall->buffer_cache[i][--xcall->buffer_cache_nr[i]]; + osdep_free_pages(xcall, p, i + 1); + } } =20 cache_unlock(xcall); diff --git a/tools/libs/call/core.c b/tools/libs/call/core.c index 02c4f8e1ae..dd8877c1a0 100644 --- a/tools/libs/call/core.c +++ b/tools/libs/call/core.c @@ -14,6 +14,7 @@ */ =20 #include +#include =20 #include "private.h" =20 @@ -44,7 +45,7 @@ xencall_handle *xencall_open(xentoollog_logger *logger, u= nsigned open_flags) xentoolcore__register_active_handle(&xcall->tc_ah); =20 xcall->flags =3D open_flags; - xcall->buffer_cache_nr =3D 0; + memset(xcall->buffer_cache_nr, 0, sizeof(xcall->buffer_cache_nr)); =20 xcall->buffer_total_allocations =3D 0; xcall->buffer_total_releases =3D 0; diff --git a/tools/libs/call/private.h b/tools/libs/call/private.h index 9c3aa432ef..8e6a208975 100644 --- a/tools/libs/call/private.h +++ b/tools/libs/call/private.h @@ -31,13 +31,15 @@ struct xencall_handle { Xentoolcore__Active_Handle tc_ah; =20 /* - * A simple cache of unused, single page, hypercall buffers + * A simple cache of unused, small, hypercall buffers + * buffer_cache[i]'s size is (i+1) pages * * Protected by a global lock. */ #define BUFFER_CACHE_SIZE 4 - int buffer_cache_nr; - void *buffer_cache[BUFFER_CACHE_SIZE]; +#define BUFFER_CACHE_NRPAGES 4 + int buffer_cache_nr[BUFFER_CACHE_NRPAGES]; + void *buffer_cache[BUFFER_CACHE_NRPAGES][BUFFER_CACHE_SIZE]; =20 /* * Hypercall buffer statistics. All protected by the global --=20 2.43.0 From nobody Thu Jul 23 21:16:27 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=1783804940; cv=none; d=zohomail.com; s=zohoarc; b=Ijz2BgNsTvznB7YqKHZU5TBBV9ozkCy1YGL6A1PnvUh+U7DQ0ZqGUQxIX54dWY4rUFaO9n9w3iy+jBTjXWO/P55RGtYaiXUdPvv/r9GcChZD1phBZhTDZqNcGeF0SRM+mMbXsvqABabL2RyTQb/BScxM25caGjbCPFagTiox4ss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783804940; h=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=nLlv2l5waNuak4AeyH2dZJf/IpZoXiiZH4C/lX/O+/Y=; b=FM+gCmrC+v8DnqrvHZvnyzCPGMri5E82UNy2p5SbTvCJnp045ZZ0Q49tNDPZcdf3aOWisfcVSokL1ouxw0kE6ZfKS8ksjEi/Qky8+OCPs6t+yrTwkc42uZOCYg3zCptbbWSceKzO4Zr1LSFIETP75AnXTgqUb19KfX4Ue70ZiKo= 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 1783804940260679.9256793085033; Sat, 11 Jul 2026 14:22:20 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1360643.1613398 (Exim 4.92) (envelope-from ) id 1wif8m-0007uX-VF; Sat, 11 Jul 2026 21:21:40 +0000 Received: by outflank-mailman (output) from mailman id 1360643.1613398; Sat, 11 Jul 2026 21:21:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8m-0007uQ-SL; Sat, 11 Jul 2026 21:21:40 +0000 Received: by outflank-mailman (input) for mailman id 1360643; Sat, 11 Jul 2026 21:21:39 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8l-0007bl-4N for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 21:21:39 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wif8k-00AIiB-HP for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 23:21:38 +0200 Received: from [10.42.69.9] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a52b3e2-e002-0a2a0a5209dd-0a2a4509cca6-0 for ; Sat, 11 Jul 2026 23:21:38 +0200 Received: from [209.85.221.52] (helo=mail-wr1-f52.google.com) by tlsNG-bad1c0.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a52b3e2-b440-0a2a45090019-d155dd34a4d7-3 for ; Sat, 11 Jul 2026 23:21:38 +0200 Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-4758bd3731bso1326947f8f.0 for ; Sat, 11 Jul 2026 14:21:38 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47a9de1d8cdsm74809460f8f.1.2026.07.11.14.21.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 14:21:37 -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=1783804898; x=1784409698; 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:content-type; bh=nLlv2l5waNuak4AeyH2dZJf/IpZoXiiZH4C/lX/O+/Y=; b=e8mt0cYJlp5mqJLBR1UcyjmHZxykk3FdZX6EKcYvwukASf1kHtAzwR6+88qwbILcyX YtydG7MB6TgV4gA2hgwgBjN1s3uGcWVQdWLgeBJR9SKgn+Dg6OtUeSrcwfZFZUbbTjaN LCyo7HCrcxu070sNbkHRdBNey4e+Fj4pYk8S337eCwuG3JCfL19MTImqtEa57IRYIP+t oVq4RlwWSc2byMPoDyuPzERCwqOh86yOjQtczYmjG6vC+SY7dIKXyKalAfXGdRfimijY 5Q3XlULo42rWFP3qy7O9JRn4KWuIFwf24YluZeuGYu3TM7KLoyvRUVOfUqaNQ4uQkfvq hGqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783804898; x=1784409698; 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:content-type; bh=nLlv2l5waNuak4AeyH2dZJf/IpZoXiiZH4C/lX/O+/Y=; b=Ds1IJWl1QeX11XzxldnxndzXCUzQamyAmYoasp6Ukx3kPonu1Fk4e+0bJAwK56GRPB QQJ5FDRdpBW8NzFCYtEEwvjGUEUeVPGVZ0Gr/7sUkYv+ulpoLs4rc6sZxWdplo/2lLca yqj8o83iPnq7UZZVgYAbR3C0pRgmYwn2kIMYM+R7l5CJyfl/eTLd4GCx0aHHIlpSZmmf rniwTNtKQeXuUEAKDRQpFEigijqYlfjNk/kXOsad6FA9PCs0jqOX10mvpt28FcCddna7 ptK1V3X1gQiyV1f5cH0fmq4n5rJzjIfr1pNc2H0wAs4jd9YPI88ZwyKBBHhvDfXsEQ3q k6Ug== X-Gm-Message-State: AOJu0YxYs+2uHJzIKOokwZpunnHVJl02lLvtDHvQ9fg/l8xCKCqIgYXu kWcP5JbOLgqD5q8jF3LPq+Bx7A2QTzkKeWueOhRgqNzLmyD0gCRT3DlqBmEemRhkMWE= X-Gm-Gg: AfdE7cke8Wk9Dh9t8kfbGiQZlzefS+vB/1QVXUA5GazTitzAZwZsOJzq5E7vlk6zNVd xeumYrTKjFCqhFdmJc8b5A3qXKAgG11vqEJbCiNUGbWTusuH+JgT+gMYGGxoIZIr1kch9bkCUMe yhFThsTxyJJW0Kl2XFM0Go/DC7okMIxzJ2xaY/pX0P/mZfmcglkJQl+406Nz0y/Dj3l6Oq7xv6R +5mR6QGJ4blT15lcS/JiIGTVvGFegmxBpm+wLa1Q5JPcNc0xDH4SrjAWDJB6gPGe1V/8RtF///K yQ5k/uxPivSU0fKU4YZNI7G1cjeFkuF8CLvJHtWqukJPpVctHepkYtNzLX0lIz9xD0BsWEy9x/x awDtUOut0Gnm3wXgWifynxPil52U7q9qG766sPrwow0nGWW1kF1gZrpQVBH5/0JOpSVu6GnWzXN cXXptzfWbmt9mzxuFzp16TMxYXjU32dpsw55Retsm3na5ftwfJz7Z5TrX/je+4rKGQyGRULmOah qiKxX5P0hGvE90LgHUneH1IZKTneA== X-Received: by 2002:adf:e19a:0:b0:47e:96f2:52ab with SMTP id ffacd0b85a97d-47f2dc85da3mr4414795f8f.7.1783804897779; Sat, 11 Jul 2026 14:21:37 -0700 (PDT) From: Frediano Ziglio X-Google-Original-From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Juergen Gross Subject: [PATCH v8 2/4] libs/guest: move batch_pfns into a separate structure Date: Sat, 11 Jul 2026 22:21:26 +0100 Message-ID: <20260711212128.505727-3-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260711212128.505727-1-frediano.ziglio@citrix.com> References: <20260711212128.505727-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-bad1c0/1783804898-5E56BA0B-A32ED01D/0/0 X-purgate-type: clean X-purgate-size: 5971 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783804942780154100 Content-Type: text/plain; charset="utf-8" Prepare for following change. Signed-off-by: Frediano Ziglio Reviewed-by: Anthony PERARD --- Changes since v6: - split from "libs/guest: allocate various migration arrays just once". Changes since v7: - initialize "batch_pfns" on declaration. --- tools/libs/guest/xg_sr_common.h | 5 ++++- tools/libs/guest/xg_sr_save.c | 29 +++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/tools/libs/guest/xg_sr_common.h b/tools/libs/guest/xg_sr_commo= n.h index f1573aefcb..7574c9f5b6 100644 --- a/tools/libs/guest/xg_sr_common.h +++ b/tools/libs/guest/xg_sr_common.h @@ -239,11 +239,14 @@ struct xc_sr_context =20 struct precopy_stats stats; =20 - xen_pfn_t *batch_pfns; unsigned int nr_batch_pfns; unsigned long *deferred_pages; unsigned long nr_deferred_pages; xc_hypercall_buffer_t dirty_bitmap_hbuf; + struct xc_sr_context_save_buffers + { + xen_pfn_t batch_pfns[MAX_BATCH_SIZE]; + } *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 84fdbe4140..e7c22e6500 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -75,7 +75,7 @@ static int write_checkpoint_record(struct xc_sr_context *= ctx) =20 /* * Writes a batch of memory as a PAGE_DATA record into the stream. The ba= tch - * is constructed in ctx->save.batch_pfns. + * is constructed in ctx->save.buffers->batch_pfns. * * This function: * - gets the types for each pfn in the batch. @@ -95,6 +95,7 @@ static int write_batch(struct xc_sr_context *ctx) void *page, *orig_page; uint64_t *rec_pfns =3D NULL; struct iovec *iov =3D NULL; int iovcnt =3D 0; + xen_pfn_t *const batch_pfns =3D ctx->save.buffers->batch_pfns; struct { struct xc_sr_rhdr rec; struct xc_sr_rec_page_data_header page_data; @@ -110,6 +111,8 @@ 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)); @@ -141,13 +144,12 @@ static int write_batch(struct xc_sr_context *ctx) =20 for ( i =3D 0; i < nr_pfns; ++i ) { - types[i] =3D mfns[i] =3D ctx->save.ops.pfn_to_gfn(ctx, - ctx->save.batch_pfns= [i]); + types[i] =3D mfns[i] =3D ctx->save.ops.pfn_to_gfn(ctx, batch_pfns[= i]); =20 /* Likely a ballooned page. */ if ( mfns[i] =3D=3D INVALID_MFN ) { - set_bit(ctx->save.batch_pfns[i], ctx->save.deferred_pages); + set_bit(batch_pfns[i], ctx->save.deferred_pages); ++ctx->save.nr_deferred_pages; } } @@ -193,7 +195,7 @@ static int write_batch(struct xc_sr_context *ctx) if ( errors[p] ) { ERROR("Mapping of pfn %#"PRIpfn" (mfn %#"PRIpfn") failed %= d", - ctx->save.batch_pfns[i], mfns[p], errors[p]); + batch_pfns[i], mfns[p], errors[p]); goto err; } =20 @@ -207,7 +209,7 @@ static int write_batch(struct xc_sr_context *ctx) { if ( rc =3D=3D -1 && errno =3D=3D EAGAIN ) { - set_bit(ctx->save.batch_pfns[i], ctx->save.deferred_pa= ges); + set_bit(batch_pfns[i], ctx->save.deferred_pages); ++ctx->save.nr_deferred_pages; types[i] =3D XEN_DOMCTL_PFINFO_XTAB; --nr_pages; @@ -235,7 +237,7 @@ static int write_batch(struct xc_sr_context *ctx) hdrs.rec.length +=3D nr_pages * PAGE_SIZE; =20 for ( i =3D 0; i < nr_pfns; ++i ) - rec_pfns[i] =3D ((uint64_t)(types[i]) << 32) | ctx->save.batch_pfn= s[i]; + rec_pfns[i] =3D ((uint64_t)(types[i]) << 32) | batch_pfns[i]; =20 if ( writev_exact(ctx->fd, iov, iovcnt) ) { @@ -274,9 +276,9 @@ static int flush_batch(struct xc_sr_context *ctx) =20 if ( !rc ) { - VALGRIND_MAKE_MEM_UNDEFINED(ctx->save.batch_pfns, + VALGRIND_MAKE_MEM_UNDEFINED(ctx->save.buffers->batch_pfns, MAX_BATCH_SIZE * - sizeof(*ctx->save.batch_pfns)); + sizeof(*ctx->save.buffers->batch_pfns)= ); } =20 return rc; @@ -293,7 +295,7 @@ static int add_to_batch(struct xc_sr_context *ctx, xen_= pfn_t pfn) rc =3D flush_batch(ctx); =20 if ( rc =3D=3D 0 ) - ctx->save.batch_pfns[ctx->save.nr_batch_pfns++] =3D pfn; + ctx->save.buffers->batch_pfns[ctx->save.nr_batch_pfns++] =3D pfn; =20 return rc; } @@ -784,11 +786,10 @@ 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 ( !ctx->save.buffers || !dirty_bitmap || !ctx->save.deferred_pages ) { ERROR("Unable to allocate memory for dirty bitmaps, batch pfns and" " deferred pages"); @@ -819,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 From nobody Thu Jul 23 21:16:27 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=1783804935; cv=none; d=zohomail.com; s=zohoarc; b=UlrLw3gfpSxzW4VXT6Vup5IKf0PPEwEYGcl+mNu9qd0HG9o3RZ1+RTe1qnzkPHDHA8PHnIoOZVS8A3cCMUe2v4q6C2YRjdlhoyFIGgcjhAUzFMe7OihvX7pTUAcecOJoydqNiLJ6j3ASIMhS8UW8q0b9yS3Ci8frBvTWbkPUK8M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783804935; 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=nPb7cp3xwXBCEs3XOqo7Tl5/hkWI0YjWiYbVfExIpM4=; b=m2fKGSymLBNvcI8y+cdQQDrvluHARDltg4Qawk8lZJWtw8XEQKQlJP6o96Bl6wz3yNh0xFufszEO5wGldurQGcvnMb2EGiR4FhSOg2jWAqJu+c9/ETz3OiZNzus0XC92Vq5Yox8mPsn4vOnccie+qlpp+zTLlP32qajCUiYYcUM= 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 1783804935529662.523747704182; Sat, 11 Jul 2026 14:22:15 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1360644.1613407 (Exim 4.92) (envelope-from ) id 1wif8o-00087t-9Z; Sat, 11 Jul 2026 21:21:42 +0000 Received: by outflank-mailman (output) from mailman id 1360644.1613407; Sat, 11 Jul 2026 21:21:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8o-00087j-6e; Sat, 11 Jul 2026 21:21:42 +0000 Received: by outflank-mailman (input) for mailman id 1360644; Sat, 11 Jul 2026 21:21:40 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8m-0007nS-7a for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 21:21:40 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wif8l-002mBf-L1 for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 23:21:39 +0200 Received: from [10.42.69.7] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a52b3c8-2eae-0a2a0a5409dd-0a2a4507a3a0-8 for ; Sat, 11 Jul 2026 23:21:39 +0200 Received: from [209.85.221.45] (helo=mail-wr1-f45.google.com) by tlsNG-ef75cf.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a52b3e3-ac46-0a2a45070019-d155dd2de920-3 for ; Sat, 11 Jul 2026 23:21:39 +0200 Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-47defd0c1c5so1305582f8f.3 for ; Sat, 11 Jul 2026 14:21:39 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47a9de1d8cdsm74809460f8f.1.2026.07.11.14.21.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 14:21:38 -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:Content-Type: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=1783804899; x=1784409699; darn=lists.xenproject.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=nPb7cp3xwXBCEs3XOqo7Tl5/hkWI0YjWiYbVfExIpM4=; b=eNPfwxmQnKBvR3NEDh1IBCOK2sLuk5NXeHegBAOL5iqpAWGMaMwwaq7W0Z0esPpELm Pa7RgkZ/ZnU5PZ5ochk+UbUDgdfg/PumZc1cSgQRu/IKOt0yv0Vr3oZ7aDrKg6LbmAIr pgU/Y4hMFftj0BAQgKfBayHk1CJXVPMY1s+hrBz/VucC2YOKJYwlbc7J3+SApy/PzSjR MPDtqm96eYag8WMwmjRsgWfphCfVcHh/gCYvcRwQ4SXkjygw6mU4nFzcH5F6sjqw2QDK NJncEvk6O7K0vwWiV9MdP/8TLfzsri+NVcUX3faNX2v3qG1v1VmjZqJOXeQU3g5p0orM AWMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783804899; x=1784409699; h=content-transfer-encoding:content-type: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 :content-type; bh=nPb7cp3xwXBCEs3XOqo7Tl5/hkWI0YjWiYbVfExIpM4=; b=VB4C5e1MwDE7ZYCLM8Q5X8+WYUw8CHSlRNzfTCF6aXhjL4X3QO184P2OMmHsLwsb/t +iJOmpqzHw3xjS26Duz/D4sgV0AKkKkyRguMCg/p2PtarWj2NELngC6Tjt9jt30iWI5K uUAFPVdSMo8TBtzBj8DrCw0LptXtkC5aib/iacaZyssnDleO5bpK72a+/uGqfDm8t1t6 di7OLVRkEqxqQf90eN2zHW9BaSRZR0fywn480AjU5p8v41uww++aJDqRLg5cnnakiE1x DLDwB00zJAP/Yx0ER0yhBZIPykmJ5O388A61TFavcq8GGMzm5MjhkVgCnkizuRyXLBii fJVg== X-Gm-Message-State: AOJu0YxE5MqdqX62MpMHcc76dMcpLBGSMFdCNXVlPImzRCHoi0ibVQGn XkFBUwzNgizNbkex6+09n6ZWrjsXEqZHZ2BNeQ07J7Kj+PfL/SIMP4nKMK/xMNeWRyY= X-Gm-Gg: AfdE7cmvvGiwrL9yDP9qppPEFbhvux64wqFXQKBXSPguGLc8mZOqZatOBXQhykUMSU/ FXSdUXz4rtYXWmq0na/kltoS290u7UVXzVa4B5fZEnNslocOsD5ve84djR9NLTE1D2sdWEQDqED U/G6HVA/wunozt8Koy+BmkIO6pa0XOT/N0CogSJLI7ATf+lWOIw0oge/iG/DZiUaD7WajONkPFS Bvuvgs2FOeZ/GISqWf3IzUC0tSo/ISVWg3ukV90DhAyuxVnk8U+GDzuDnVIO8ECxUyKK/eNSRMb vNPLoYIPA063oGSjrQ34S8A7JwthzrVbzg2sahyh0EI2e+DY/uPycK9cJpGVd20z2Zh+Hl9PpKq cB5TXp/2Ge7IJxch3QTZAkmrWc5QDEVYpo0oZHWzSbrunLcRjA8zrGJk294rXAcJbNQcYfSC533 2esOpwTqW1X3l5hxoOr/pGUVO+b1hnyZ8qyib7K/5xEHU9GeTFnwd1dAX7BiU8KvgDThHSC0qJr d8vHBglbAtUKnjPMCY= X-Received: by 2002:a05:6000:290f:b0:46d:d693:88cb with SMTP id ffacd0b85a97d-47f2dce2db6mr3974180f8f.46.1783804898983; Sat, 11 Jul 2026 14:21:38 -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 v8 3/4] libs/guest: allocate various migration arrays just once Date: Sat, 11 Jul 2026 22:21:27 +0100 Message-ID: <20260711212128.505727-4-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260711212128.505727-1-frediano.ziglio@citrix.com> References: <20260711212128.505727-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ef75cf/1783804899-D3F67201-D3630C6A/0/0 X-purgate-type: clean X-purgate-size: 3795 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783804938629154100 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 --- Changes since v2: - change prefix in subject. Changes since v3: - fix comment style Changes since v4: - change order of fields in structure. Changes since v6: - split preparation commit. --- tools/libs/guest/xg_sr_common.h | 6 ++++++ tools/libs/guest/xg_sr_save.c | 33 ++++++++++++--------------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/tools/libs/guest/xg_sr_common.h b/tools/libs/guest/xg_sr_commo= n.h index 7574c9f5b6..c07c6db59e 100644 --- a/tools/libs/guest/xg_sr_common.h +++ b/tools/libs/guest/xg_sr_common.h @@ -246,6 +246,12 @@ struct xc_sr_context 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]; + void *local_pages[MAX_BATCH_SIZE]; + struct iovec iov[MAX_BATCH_SIZE + 2]; /* Headers + data. */ + uint64_t rec_pfns[MAX_BATCH_SIZE]; + int errors[MAX_BATCH_SIZE]; } *buffers; } save; =20 diff --git a/tools/libs/guest/xg_sr_save.c b/tools/libs/guest/xg_sr_save.c index e7c22e6500..34e41cf410 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -115,24 +115,18 @@ static int write_batch(struct xc_sr_context *ctx) 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 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)); + iov =3D ctx->save.buffers->iov; /* page_data record PFNs list */ - rec_pfns =3D malloc(nr_pfns * sizeof(*rec_pfns)); - - if ( !mfns || !types || !errors || !local_pages || !iov || !rec_pfns ) - { - ERROR("Unable to allocate arrays for a batch of %u pages", - nr_pfns); - goto err; - } + rec_pfns =3D ctx->save.buffers->rec_pfns; =20 iov[0].iov_base =3D &hdrs; iov[0].iov_len =3D sizeof(hdrs); @@ -251,13 +245,10 @@ static int write_batch(struct xc_sr_context *ctx) 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(rec_pfns); - free(iov); - free(local_pages); - free(errors); - free(types); - free(mfns); + local_pages[i] =3D NULL; + } =20 return rc; } @@ -791,8 +782,8 @@ static int setup(struct xc_sr_context *ctx) =20 if ( !ctx->save.buffers || !dirty_bitmap || !ctx->save.deferred_pages ) { - 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; --=20 2.43.0 From nobody Thu Jul 23 21:16:27 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=1783804946; cv=none; d=zohomail.com; s=zohoarc; b=hqjbQq09TCK0gq07vHH3dmoQ3ssWoarFDQz9LInxFCDsou1c5BB6sD+cpLdzrQtBBAYphpblvTKVfj0g9Pp1IuL2b3i6eVhnzfyX4hGoKf95+m88Nl5KywXzBdH5speZAxUHh7htNuO5oxMxaFueI2lM5QgFsH/OkpVTFtSVd28= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783804946; h=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=jLiExMbqfLeimgkvhGVcWHLdFUDKqJKlc0Jm68oRzVw=; b=b8jFDNOrWMYKwM0uRk1D+NjRF62Z6fx/hB2MovXrKuzUn5Yg1P5QKgifGEL+F3rAQZGBInMCv2E8oYklD8QhwJ4uGbviwG5Y96UUMKLGqP8TVN1QLZSCkjnyRDERQ81tDuGulKI3kDcNPHEaNaE/eM7BrHhc4j9utQ8h41KlB7k= 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 1783804946737627.7436165267516; Sat, 11 Jul 2026 14:22:26 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1360645.1613411 (Exim 4.92) (envelope-from ) id 1wif8o-0008Ao-Ix; Sat, 11 Jul 2026 21:21:42 +0000 Received: by outflank-mailman (output) from mailman id 1360645.1613411; Sat, 11 Jul 2026 21:21:42 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8o-00089s-EX; Sat, 11 Jul 2026 21:21:42 +0000 Received: by outflank-mailman (input) for mailman id 1360645; Sat, 11 Jul 2026 21:21:41 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wif8n-00083H-OR for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 21:21:41 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wif8n-00Ac2k-5L for xen-devel@lists.xenproject.org; Sat, 11 Jul 2026 23:21:41 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a52b3bd-5cb7-0a2a0a5109dd-0a2a4508bcba-4 for ; Sat, 11 Jul 2026 23:21:41 +0200 Received: from [209.85.128.45] (helo=mail-wm1-f45.google.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.2) (envelope-from ) id 6a52b3e4-ee29-0a2a45080019-d155802da550-3 for ; Sat, 11 Jul 2026 23:21:41 +0200 Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-4938d5f86f3so14815375e9.1 for ; Sat, 11 Jul 2026 14:21:41 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-47a9de1d8cdsm74809460f8f.1.2026.07.11.14.21.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 14:21:39 -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=1783804900; x=1784409700; 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:content-type; bh=jLiExMbqfLeimgkvhGVcWHLdFUDKqJKlc0Jm68oRzVw=; b=qKOmbQSnonOZqgUT0DSCYCeFR4DGS3A+5LtGSPToBckEZQQ4PdEft7c7loD27vgJR2 Nk5PIqcU7gVH3IEmnSRxnaPBMYXyWOAgsc+5Sny2rufiywI6YIRlAbNTdG1E71eMKK/0 wViJW7jqArTW72lRorwOFJSgXiR7dhEJZomBqml2d3kpRZJg8GbRsH3c0ceNGVbMi01g kzlQxXbwAhHTkLOmAGO4cDdSi8ZomLyVZ+TsxgmkFgHibZFlTX5+/ap3MUOyLGQqdmi0 M6wQbd7WtF5jH3gc5sInILU+67DkYc8cxn/mQHtWYzcQtDZtxOgnYmUZ6LDiQ+5huPnS GXPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783804900; x=1784409700; 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:content-type; bh=jLiExMbqfLeimgkvhGVcWHLdFUDKqJKlc0Jm68oRzVw=; b=dBUFBjlFN9CtECmrobEQrVLtA2d4p1H2qYhZit2NtutYmzlcfnEDqJuZ8SBSjXKPqh 8/IZo5kbt5cNKp633gAlqRBOYK2Vd3L/9Fga7eVL9nWDjm3Rlq1pOLOH8Esa4jearnFk YdGX73JnMu3ChvR0FE4MAp4zLveNjR9dLEwCEuydaAPB9MVBGKVn6cx7je17X/16S+l1 Fc5L278YOWSl01kp/h65BMz9+nKHm4ivbQInXKSPohk59riOFuT/aYxLN67Xy0CxJitl TrgMjz6B++2e6pnHtwkXdmkfPPdH8PaOSPVYgZ/eKfaYiQgcVvJJ/N5QCJ/MP+dq3Z9G PjnA== X-Gm-Message-State: AOJu0Yz3hJ8CJopmHsvXG9WziSX6ZWQyOb2xAgdpw0zUC6yG+YW/Do2S nclyNXl+IWeBW7eM8bYhJ35KT9qRLk6hskWGeemZbgKUak+G8K3HL2MPVOjYAl4k54Q= X-Gm-Gg: AfdE7ckrRYtjB813tepjrLlzguNCl5mjrbXdygCeprWHWN4uMzGaZ0xwcIxr3aTktyn y04oHvMFKg4yENqXuliKwrUOb9XTDK/axW8qJA6jucn1UsRJ+BvoQpY0JZINk0NKseyAJbcykXa nLCvUe/zUxM+oS8p7Q3hKPJcIDRca3RGtHvnlH+BtNoxhHusFw7M27hZJ8Flk3A0af2X8+OLuMb toxiyC9vpdORNMZajV8i0QyPBKmUgRvhOurFfD5Vj5xE+4XQZJpYK6Y9xOS1K+2GWGVQpkS7gqR hLdJIZThrSNC2Ycd4wRO1cGi2zA/xnb2AnzHVVX+/+2W+d23y+/nuyXakMDV92OfDYqQ7+XhJTK 9hOLod99asy8zgPWLyXJKiA1cuuOsUPnF9XfNAnbR9UwL3zIXn6gH48JdP+BFGZpdiOLNyZIwPP 8LAm8cgCuBfXYPW7XtjasH/xtWgprtlLCzM/ZPV3iSzbiQ4SHc/OF/s4U7aj78dQUiv4GgRm6uQ NGhMijx6e2T00IniFw= X-Received: by 2002:a05:600c:6749:b0:493:edde:54c8 with SMTP id 5b1f17b1804b1-493f8885b92mr39245425e9.8.1783804900465; Sat, 11 Jul 2026 14:21:40 -0700 (PDT) From: Frediano Ziglio X-Google-Original-From: Frediano Ziglio To: xen-devel@lists.xenproject.org Cc: Frediano Ziglio , Jan Beulich , Andrew Cooper , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Teddy Astie , Anthony PERARD , Juergen Gross Subject: [PATCH v8 4/4] libs/guest: use Valgrind to detect various buffer overflows Date: Sat, 11 Jul 2026 22:21:28 +0100 Message-ID: <20260711212128.505727-5-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260711212128.505727-1-frediano.ziglio@citrix.com> References: <20260711212128.505727-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c1860d/1783804901-11D7201C-55CF5499/0/0 X-purgate-type: clean X-purgate-size: 4031 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783804948846154100 Content-Type: text/plain; charset="utf-8" Previously this was done as buffers were allocated separately. Signed-off-by: Frediano Ziglio --- tools/libs/ctrl/xc_private.h | 8 ++++++++ tools/libs/guest/xg_sr_common.h | 8 ++++++++ tools/libs/guest/xg_sr_save.c | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/tools/libs/ctrl/xc_private.h b/tools/libs/ctrl/xc_private.h index 8a325c17b0..bef96521c3 100644 --- a/tools/libs/ctrl/xc_private.h +++ b/tools/libs/ctrl/xc_private.h @@ -45,8 +45,16 @@ #if defined(HAVE_VALGRIND_MEMCHECK_H) && !defined(NDEBUG) && !defined(__MI= NIOS__) /* Compile in Valgrind client requests? */ #include +#define MEM_NOACCESS_BUFFER(name, size) uint8_t name[size]; +#define MEM_NOACCESS_INIT(field) \ + VALGRIND_MAKE_MEM_NOACCESS(field, sizeof(field)) +#define MEM_UNDEFINED_INIT(field) \ + VALGRIND_MAKE_MEM_UNDEFINED(field, sizeof(field)) #else #define VALGRIND_MAKE_MEM_UNDEFINED(addr, len) /* addr, len */ +#define MEM_NOACCESS_BUFFER(name, size) +#define MEM_NOACCESS_INIT(field) do {} while(0) +#define MEM_UNDEFINED_INIT(field) do {} while(0) #endif =20 #if defined(__MINIOS__) diff --git a/tools/libs/guest/xg_sr_common.h b/tools/libs/guest/xg_sr_commo= n.h index c07c6db59e..d3fc7f363e 100644 --- a/tools/libs/guest/xg_sr_common.h +++ b/tools/libs/guest/xg_sr_common.h @@ -245,13 +245,21 @@ struct xc_sr_context xc_hypercall_buffer_t dirty_bitmap_hbuf; struct xc_sr_context_save_buffers { + MEM_NOACCESS_BUFFER(na0, 16); xen_pfn_t batch_pfns[MAX_BATCH_SIZE]; + MEM_NOACCESS_BUFFER(na1, 16); xen_pfn_t mfns[MAX_BATCH_SIZE]; + MEM_NOACCESS_BUFFER(na2, 16); xen_pfn_t types[MAX_BATCH_SIZE]; + MEM_NOACCESS_BUFFER(na3, 16); void *local_pages[MAX_BATCH_SIZE]; + MEM_NOACCESS_BUFFER(na4, 16); struct iovec iov[MAX_BATCH_SIZE + 2]; /* Headers + data. */ + MEM_NOACCESS_BUFFER(na5, 16); uint64_t rec_pfns[MAX_BATCH_SIZE]; + MEM_NOACCESS_BUFFER(na6, 16); int errors[MAX_BATCH_SIZE]; + MEM_NOACCESS_BUFFER(na7, 16); } *buffers; } save; =20 diff --git a/tools/libs/guest/xg_sr_save.c b/tools/libs/guest/xg_sr_save.c index 34e41cf410..c5022b2a1d 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -115,8 +115,10 @@ static int write_batch(struct xc_sr_context *ctx) assert(ctx->save.buffers); =20 /* Mfns of the batch pfns. */ + MEM_UNDEFINED_INIT(ctx->save.buffers->mfns); mfns =3D ctx->save.buffers->mfns; /* Types of the batch pfns. */ + MEM_UNDEFINED_INIT(ctx->save.buffers->types); types =3D ctx->save.buffers->types; /* Errors from attempting to map the gfns. */ errors =3D ctx->save.buffers->errors; @@ -124,8 +126,10 @@ static int write_batch(struct xc_sr_context *ctx) local_pages =3D ctx->save.buffers->local_pages; memset(local_pages, 0, sizeof(*local_pages) * nr_pfns); /* iovec[] for writev(). */ + MEM_UNDEFINED_INIT(ctx->save.buffers->iov); iov =3D ctx->save.buffers->iov; /* page_data record PFNs list */ + MEM_UNDEFINED_INIT(ctx->save.buffers->rec_pfns); rec_pfns =3D ctx->save.buffers->rec_pfns; =20 iov[0].iov_base =3D &hdrs; @@ -788,6 +792,14 @@ static int setup(struct xc_sr_context *ctx) errno =3D ENOMEM; goto err; } + MEM_NOACCESS_INIT(ctx->save.buffers->na0); + MEM_NOACCESS_INIT(ctx->save.buffers->na1); + MEM_NOACCESS_INIT(ctx->save.buffers->na2); + MEM_NOACCESS_INIT(ctx->save.buffers->na3); + MEM_NOACCESS_INIT(ctx->save.buffers->na4); + MEM_NOACCESS_INIT(ctx->save.buffers->na5); + MEM_NOACCESS_INIT(ctx->save.buffers->na6); + MEM_NOACCESS_INIT(ctx->save.buffers->na7); =20 rc =3D 0; =20 --=20 2.43.0