From nobody Thu Jul 23 21:53:05 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=1783583093; cv=none; d=zohomail.com; s=zohoarc; b=I9stuBHCsxbue7v7e/Sjk2P+aq2D3mvt+JBrVfukEkn9g33TU3aEkZKcG+osGFiiW5qrkt1xZkYzdclmrpTHVTNHlyMnU2hePFv3OmrVCVazEE82XamixlOELNnzb6UwiG2yYy/IwZ+Wv5xk8X+mpl6ysGnESc8r57VNQiPxYCE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783583093; 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=baowJbayltLwF4aiXcTZUWXDEcONYgkCdLRWeXEMrP8=; b=m109LC8oQG6RKAdV8Sm4RS8im0H87ldzPzPmlvDzsVatilt4frTkEMU5NXfFHEyVQGk+sBuoXWnoUf6oWot+sP0v9OE2KZ2PASyI/qygNL90dvww3SpIS06C1rbDngqLMD3dFLOdkq7vTigsSKCosEk7RM7WDuQwxkanXEomwT8= 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 1783583093615823.8847498566171; Thu, 9 Jul 2026 00:44:53 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1357590.1611968 (Exim 4.92) (envelope-from ) id 1whjQo-0007Dj-De; Thu, 09 Jul 2026 07:44:26 +0000 Received: by outflank-mailman (output) from mailman id 1357590.1611968; Thu, 09 Jul 2026 07:44:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQo-0007Db-8t; Thu, 09 Jul 2026 07:44:26 +0000 Received: by outflank-mailman (input) for mailman id 1357590; Thu, 09 Jul 2026 07:44:24 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQm-00070n-H1 for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 07:44:24 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1whjQl-00B0qw-U0 for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 09:44:23 +0200 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4f514d-bab6-0a2a0a5309dd-0a2a4505a858-24 for ; Thu, 09 Jul 2026 09:44:23 +0200 Received: from [209.85.128.49] (helo=mail-wm1-f49.google.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4f5157-3cb2-0a2a45050019-d1558031d459-3 for ; Thu, 09 Jul 2026 09:44:23 +0200 Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-493e8d4f4dcso7713985e9.0 for ; Thu, 09 Jul 2026 00:44:23 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493eb6f3b85sm69114695e9.2.2026.07.09.00.44.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2026 00:44:22 -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=1783583063; x=1784187863; 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=baowJbayltLwF4aiXcTZUWXDEcONYgkCdLRWeXEMrP8=; b=erajTBBua8HIEMoyTXH9Ib7I1zEkR5PCRYB0ocAA3NvFV2/CnB+fEJghtm+180+75x Ik/XCOdh52+7DhTo/kGa6ueZbGfXgm4cC54SL9cLJdi5ab0E9WD7EwXBZy+jGLkWa8fg pQjjXILbYxKDbQPtN3Vd2gNGAlQPXn0Ip3AbCyg7x3RyGXbMFRri91QbnxnvmYudbBqJ Wot1mvLvT0kNJydbcGX5WNJJco+Vg/vdMCwAEPS/EOFm3GwODyOBp8hHeo7XPmAt6HJJ ZT2R/vCwlYgvf32ZA4+vH//KfT6wmcivBjkM+inErfcpLKQaI+ATRwG0s1StIiQgDRXo xC8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783583063; x=1784187863; 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=baowJbayltLwF4aiXcTZUWXDEcONYgkCdLRWeXEMrP8=; b=KVE9HOcpz8jWnjpNsnm7h9+RypwMBEq3TNWmWa6MEyz3tXaePujchljDuiFMs1wlyL Amyr1gk3NHtwPcPw25wvDr+kI7XyQKA6rFgP76WAJLT5UUD9J/LpHFGnwA7ol54xIC2i zXe4sHOfQoeuoN2CtkGnLN/zLR7nvzlEU3sp9eS8mdnn9Y79i2q45twbHe/ARXH21K2x K/hje2v/c8vkfdaURz13+o0Wuy0pdruV0F9/AdQ4YWHv7vwLQjLZYjJaLfKVxEYGWxBS DshTpx04VGDfxDAWRHBcIjc1AaNqbSbVjuUmR/6H4eJKtb9Ec/ZtnTpfokmhT2ejPt4N fHmQ== X-Gm-Message-State: AOJu0YySjtff9m1xHTS2pepvnFq85H2Quw3qApywhL7gkljnbl0BavZj oNGCxWj5H7WzsBWNjwPMgiSrScMYQNOvY61DXYqnXodhTLvuYxFpfSzP5WNPOX6YkaU= X-Gm-Gg: AfdE7cnm8Cbmkkw5zwYT7UFF/YNL4I900AIMMrP7aBHlCgwQQS0Z0vwDXhlDdavIcPU Dq9ndMAYbcfcsl9AGJRHYmkFLLDfUlmkEtClgSVhW5voFKdPNvZh+r8zLZkGpVmhCLqFfMTeYyT 4CKo+VjFn8CXBLPuwGag5BTtkP5r3c0LLdr85S0GIiiOitGmD1cqLUess096MrMK8wkqro+Beuh q8rjwoZoCXsDHh8YExvTT1cz3gnbYWvL247Yk72kTgsaOH20SLaUThI6he2LEVnxgTJY2dhAgP5 qIegKyYH6Jzt2YfzAIO5uMa+KnL8ngDeYti6LWn2FQAzrXDEXwkNHEhv8+yRqL2mHgrcy2WQrqK 0gAOjsfcbQhenGtJK9RO07EV9l0Cf8FUi7RC+1OQefRe7CwhrZlnKCHG4cy9P5MunwASlFHnXDr zRkipuAuK6uKttudfQsDTVgX/Ks1cHBvtfCIcgAyaMQSpAi336FyIx0knGzJUHtaTtTuJANqXQ4 FTCK0lQfWYi/LTVOBc= X-Received: by 2002:a05:600c:4e48:b0:493:bb6b:5bb5 with SMTP id 5b1f17b1804b1-493e686efdamr56247905e9.13.1783583063228; Thu, 09 Jul 2026 00:44:23 -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 v7 1/6] libs/call: cache up to 4 pages in hypercall bounce buffers Date: Thu, 9 Jul 2026 08:43:52 +0100 Message-ID: <20260709074358.256084-2-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260709074358.256084-1-frediano.ziglio@citrix.com> References: <20260709074358.256084-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-c201ff/1783583063-0ED162B8-381D6110/0/0 X-purgate-type: clean X-purgate-size: 5532 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783583095326158500 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 --- 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. --- 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..10002faa28 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 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 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:53:05 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=1783583095; cv=none; d=zohomail.com; s=zohoarc; b=SfqTtCvQmJduG/S4Gr7zTMDzV652bTqNLkpZn+HUvbrG23gcYbpVQvHURZqGzypkWuF/Slj5lyDR4gGSz07BHLxsaLI+oHbDT3g6aYUqA7dZxqVOH0WVPDc+aHK6nIRRFfzT+doYTUCUDABQw6su7vIQjjpY0Ufqk1T9xr/wS4c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783583095; 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=Acb+KBsW3mKtkw9510ffwdV80EN7Kf7SNHo6XfWGcqM=; b=kzJVRGN3dSEp7wdLMCwXP1c0lkSvsxZLhBVci0VK/uNeNoZmhV+ZoauNDVI8SEnZWbeCSxCyoqd7SuROHrHQwK4NJY9FrmTd4D3XQvbjAnFt2HxbMTGEqWyGWidYFZpw1rnKRiCP3Wa7OatFncpBLEEbSbpr+94wnNn96gcilDI= 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 1783583095524819.5846191663643; Thu, 9 Jul 2026 00:44:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1357591.1611973 (Exim 4.92) (envelope-from ) id 1whjQo-0007GU-KX; Thu, 09 Jul 2026 07:44:26 +0000 Received: by outflank-mailman (output) from mailman id 1357591.1611973; Thu, 09 Jul 2026 07:44:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQo-0007Fd-GZ; Thu, 09 Jul 2026 07:44:26 +0000 Received: by outflank-mailman (input) for mailman id 1357591; Thu, 09 Jul 2026 07:44:25 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQn-00074a-I9 for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 07:44:25 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1whjQm-00Ca40-VM for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 09:44:24 +0200 Received: from [10.42.69.12] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4f5158-e002-0a2a0a5209dd-0a2a450ca7e4-4 for ; Thu, 09 Jul 2026 09:44:24 +0200 Received: from [209.85.128.44] (helo=mail-wm1-f44.google.com) by tlsNG-d25034.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4f5158-f399-0a2a450c0019-d155802cd443-3 for ; Thu, 09 Jul 2026 09:44:24 +0200 Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-493e8d4f4dcso7714105e9.0 for ; Thu, 09 Jul 2026 00:44:24 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493eb6f3b85sm69114695e9.2.2026.07.09.00.44.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2026 00:44:23 -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=1783583064; x=1784187864; 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=Acb+KBsW3mKtkw9510ffwdV80EN7Kf7SNHo6XfWGcqM=; b=b8MyR2yuRgmpuruk6+Ujgv1BNLrEIiq87Ez+C/btWfjRh2/VOA4yWbS29yh6b1alCR zL7x2HM4MvFUh6+ldhNQtvEYkzvXjDLPON0EFdWMVWUYbFwFXP2mfReUfl68KL6AF6TD CWFquqi7QxW1jcQa0Ba3zkXL1yg3ICK5MxoDbapHH0DMciV4XlI7ebz17PaWkpgL2fHC e1WfWVh6YqmWHESklDH3uYtU832eG7kblvoHTMNSrKNg6XwDdgalSzzJxqXxRSkwrlLw YpRHOvGsEQy72fEjDlcrnCIA6b0VxyYXqguY/FNwET2yPogXcRDPtVhajSq1HT2smBcw QCsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783583064; x=1784187864; 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=Acb+KBsW3mKtkw9510ffwdV80EN7Kf7SNHo6XfWGcqM=; b=ANHvXXOQBIA/HKIG+9Czbgxy1mr9zQsZNZ+atxhdcbRFqUzNiIR3HmiWi8ZGBRzsru Ede6GT4CVL2TFKWFRNrnJnZnM7qJtwGCtJQcJLDjqe0F5znKtk1Evspy5svlao4Nz3D+ r7vYbkTfbJscWoYig4W176LNYrysnL/TFSL+LFcNgrTk/px3TIGBKHkPIHbrxu8WD+8S /L8rGqsWP5WqkFMUheAhX9hpjAwovODYbKExd6oyyM7JiFyCI0I7hBzFo1L1Aoc/7+a5 AGemRJ4R1tcGzNHNO7NJ7hT9bwcPTGlVaXNC9uxoaPTMjjcFohoNJTvwLJ0dUhHpfDqg 2THw== X-Gm-Message-State: AOJu0YzQ4bDdR6TUfgtPvOT0rJGBa+7rqft/WIVimC8rP6dAJCbs7PCc uDiibfN8NsryUnZQ8e/Eh3uwfYmG36prCbHI0rwiPP+GCUfJjsASQvr0GMIhMhV2Png= X-Gm-Gg: AfdE7ckaKKVvClVRRsDW62TD66aCcnBt8TtLb+EtKIVWQ0VUc4SmSCFjnC2nZX6lhBQ NNGzOhlcI7xcqyDPtcWoucXrdoysN8IgyHz0OZRrrS6AjgTtak2xf1ve97HQCQV0BVnMTd3lqD7 06qYMAgElevV5pD83yJb26zeX8SDUmRMf2dJa/zAcYMlo0PkOByWfTJhHNqklPVD8z2yD7ySaK8 NdUZySOGRJiUTspzF+MAE5flUhG8fHY0h1bClkK1wktxVLOk3DbV2rCDOgSYnYR5+mBTXYk8YBY oE+ajLVhd0f8odI2HjSjht0OKlHLeBuAiaVyWn3X5Ocl52ON+iy5JUS3M4iPaVqlwOnzU7p2yjt SCfnzT3Nv2fEvu4X1eisF/a39TV0W1+twuZCxZcHq90fNe1OBC2EB+l3bshdaSzOfxaeQYBrfVn pZlLQ0od3lnxHB3zBF/JamkPdfYnSdpTcR0blsITz2iCQP0NV82aZwp0aH3OpRaWzQKrg4i81Wc uiZKkoHAWRxgXROd1U= X-Received: by 2002:a05:600d:844f:20b0:493:c77c:108a with SMTP id 5b1f17b1804b1-493e68a1dc1mr43255775e9.36.1783583063974; Thu, 09 Jul 2026 00:44:23 -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 v7 2/6] libs/ctrl: Allow writev_exact() to change the iov array Date: Thu, 9 Jul 2026 08:43:53 +0100 Message-ID: <20260709074358.256084-3-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260709074358.256084-1-frediano.ziglio@citrix.com> References: <20260709074358.256084-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-d25034/1783583064-0F737D51-67959FFE/0/0 X-purgate-type: clean X-purgate-size: 4311 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783583097346158501 Content-Type: text/plain; charset="utf-8" Avoid having to allocate and copy the array if a partial write happens. Signed-off-by: Frediano Ziglio --- Changes since v2: - change prefix in subject. Changes since v6: - use a single loop to skip IOVs; - add a comment to the function. --- tools/libs/ctrl/xc_private.c | 65 +++++++++--------------------------- tools/libs/ctrl/xc_private.h | 3 +- 2 files changed, 17 insertions(+), 51 deletions(-) diff --git a/tools/libs/ctrl/xc_private.c b/tools/libs/ctrl/xc_private.c index bb0f81d6f3..89ec68a183 100644 --- a/tools/libs/ctrl/xc_private.c +++ b/tools/libs/ctrl/xc_private.c @@ -635,7 +635,7 @@ int write_exact(int fd, const void *data, size_t size) /* * MiniOS's libc doesn't know about writev(). Implement it as multiple wri= te()s. */ -int writev_exact(int fd, const struct iovec *iov, int iovcnt) +int writev_exact(int fd, struct iovec *iov, int iovcnt) { int rc, i; =20 @@ -649,70 +649,35 @@ int writev_exact(int fd, const struct iovec *iov, int= iovcnt) return 0; } #else -int writev_exact(int fd, const struct iovec *iov, int iovcnt) +int writev_exact(int fd, struct iovec *iov, int iovcnt) { - struct iovec *local_iov =3D NULL; - int rc =3D 0, iov_idx =3D 0, saved_errno =3D 0; - ssize_t len; + int rc =3D 0, iov_idx =3D 0; + ssize_t len =3D 0; =20 while ( iov_idx < iovcnt ) { - /* - * Skip over iov[] entries with 0 length. - * - * This is needed to cover the case where we took a partial write = and - * all remaining vectors are of 0 length. In such a case, the res= ults - * from writev() are indistinguishable from EOF. - */ - while ( iov[iov_idx].iov_len =3D=3D 0 ) - if ( ++iov_idx =3D=3D iovcnt ) - goto out; + /* Check iov[] to see whether we had a partial or complete write. = */ + if ( len >=3D iov[iov_idx].iov_len ) + { + len -=3D iov[iov_idx++].iov_len; + continue; + } + + /* Partial write of iov[iov_idx]. */ + iov[iov_idx].iov_base +=3D len; + iov[iov_idx].iov_len -=3D len; =20 len =3D writev(fd, &iov[iov_idx], min(iovcnt - iov_idx, IOV_MAX)); - saved_errno =3D errno; =20 if ( (len =3D=3D -1) && (errno =3D=3D EINTR) ) continue; if ( len <=3D 0 ) { rc =3D -1; - goto out; - } - - /* Check iov[] to see whether we had a partial or complete write. = */ - while ( (len > 0) && (iov_idx < iovcnt) ) - { - if ( len >=3D iov[iov_idx].iov_len ) - len -=3D iov[iov_idx++].iov_len; - else - { - /* Partial write of iov[iov_idx]. Copy iov so we can adjust - * element iov_idx and resubmit the rest. */ - if ( !local_iov ) - { - local_iov =3D malloc(iovcnt * sizeof(*iov)); - if ( !local_iov ) - { - saved_errno =3D ENOMEM; - rc =3D -1; - goto out; - } - - iov =3D memcpy(local_iov, iov, iovcnt * sizeof(*iov)); - } - - local_iov[iov_idx].iov_base +=3D len; - local_iov[iov_idx].iov_len -=3D len; - break; - } + break; } } =20 - saved_errno =3D 0; - - out: - free(local_iov); - errno =3D saved_errno; return rc; } #endif diff --git a/tools/libs/ctrl/xc_private.h b/tools/libs/ctrl/xc_private.h index b5892ae8dc..8a325c17b0 100644 --- a/tools/libs/ctrl/xc_private.h +++ b/tools/libs/ctrl/xc_private.h @@ -383,7 +383,8 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_m= mu *mmu); /* Return 0 on success; -1 on error setting errno. */ int read_exact(int fd, void *data, size_t size); /* EOF =3D> -1, errno=3D0= */ int write_exact(int fd, const void *data, size_t size); -int writev_exact(int fd, const struct iovec *iov, int iovcnt); +/* May alter @iov to cope with partial writes. */ +int writev_exact(int fd, struct iovec *iov, int iovcnt); =20 int xc_ffs8(uint8_t x); int xc_ffs16(uint16_t x); --=20 2.43.0 From nobody Thu Jul 23 21:53:05 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=1783583098; cv=none; d=zohomail.com; s=zohoarc; b=QQTWbRgsfT4/LUQHonPkclTuqdS8XIF6RAMl8dEhLGjPnQy21J4ZhkW0UMIk2Nnkgm0Elfmt+b/L6daWIzGCui+gR5ir5XtQVaa2R4LCmIYpTxdWocDQ34DSK85rnOklRrkqGBn9pKbIi4sk9ArlAhPfczVkasrJbz8kTuep0K0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783583098; 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=YenDywqbNZBwQc5oagyk/xEUwwKpzwyMPkUM94WQxwg=; b=oIRXSPnhqlfiP0ey6OWcZmLeh3nKgqIHlUEOrffT9Ptm3KnRdnq1CzZYWZ3YruA79rknZ7iPtcmBatHGrj3aUuxy4ukCJTGW7k4X+/1MOzOU5m2S4NN2UbZet9MGVhG1yf9hMmhsPiEk+MXgXvKMpIITSLicUn4u7qxkcydUrZc= 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 1783583098831670.4940947551455; Thu, 9 Jul 2026 00:44:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1357592.1611986 (Exim 4.92) (envelope-from ) id 1whjQp-0007dV-Vm; Thu, 09 Jul 2026 07:44:27 +0000 Received: by outflank-mailman (output) from mailman id 1357592.1611986; Thu, 09 Jul 2026 07:44:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQp-0007dL-SV; Thu, 09 Jul 2026 07:44:27 +0000 Received: by outflank-mailman (input) for mailman id 1357592; Thu, 09 Jul 2026 07:44:26 +0000 Received: from mx.expurgate.net ([194.145.224.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQo-0007Bx-61 for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 07:44:26 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1whjQn-00Ca40-J3 for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 09:44:25 +0200 Received: from [10.42.69.7] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4f514d-e002-0a2a0a5209dd-0a2a4507bdac-42 for ; Thu, 09 Jul 2026 09:44:25 +0200 Received: from [209.85.128.41] (helo=mail-wm1-f41.google.com) by tlsNG-ef75cf.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4f5159-9c8e-0a2a45070019-d1558029ccdd-3 for ; Thu, 09 Jul 2026 09:44:25 +0200 Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-493ec555a26so2512215e9.0 for ; Thu, 09 Jul 2026 00:44:25 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493eb6f3b85sm69114695e9.2.2026.07.09.00.44.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2026 00:44:24 -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=1783583065; x=1784187865; 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=YenDywqbNZBwQc5oagyk/xEUwwKpzwyMPkUM94WQxwg=; b=bNFQ79MU3scsJyHtRQrt+R13/8XsqlDi/+suoLDH0aR9t5+yxy9pviiBXc0ML6I6IB u/TZRH2akVIgqSEV/WX9CsL+VGmSIMYFyg7bZajGqfuWJHTPoXgI93i0+8eVQnREVbUF x9KPydIpeqCj1N9xiTgcNpvsUxK7F/ZrpbzXkaR7taSoy/lMve3ePEKzcV8xAVx2Phfu 8TrAZIkKydiuCY79B19tuyS6OTg7ndmF1BV6+GE/H69IuwnwH7PXqVZz5SCi0jPMlHJR E0M1v8PVj7gLIBOeqsmD39YUKYXpZxQnmyLSg7dh62p8yYvVGJn1syyMfryRRaF2AoHU +6tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783583065; x=1784187865; 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=YenDywqbNZBwQc5oagyk/xEUwwKpzwyMPkUM94WQxwg=; b=b6zEZfMAwwEEjYnH/TutjD5kGxgI1bgQrYEoExG4sB9qAfsGM6KFjQl+XvqXfa3ZX+ jl7Dwl9PvV9Y+K0tio0Ur17RLJr+7t5r6SVrkrAcHNklVhdJjNST9wrhiNk6bJTH5LOp kL1QDy3hZdSIVydz0vhDFPYs4Bk8uVfq9gzhAJjLuD94CFgHqmlFCc66hGOfW7fntOW8 NTAXFdS6IJ+Tv+4KFUvw4iwBBKbIQR/jgUztjhdm9RiDKnnpHjMVkmiAfstmZFBLtllp tyZBeoKnzsh5eDi3zjy6rMa6XiSnnVdHR/hIzbRWoXUDiNMgm8LZJhfSGhDs4kWEWrdy V9YA== X-Gm-Message-State: AOJu0YzvkyHIBnhv0gEwqZnCcNybVIGgM2mDP6wFVVJMA13UX460IB/X hH9V/h3pWav2+IlOB1SQSBtaF9kT7MqM5uTxzJUlfo/gdGJmhWAjBJM3J9K6U1hM/G4= X-Gm-Gg: AfdE7ckbGQsynPM78dLEe1R2EO50g3sSCAwjvPdTPcgVFUihReRLrsGwpMVa51bNUnM B9sEjDcCH5EI3PuFqxo6ogI9tKvYEOmvSF65Xzen+29cHx/MoKNX5trDxQ44Gxc5dtG2n0n/OdY r15oDZeyu/ioxawp0NeRKJxm+CGpVvABkSxsr3DI75xegTE5iyuNPImhNqUE+WCVsK3T4sR7GTj P7E/0Rt5ZO7kpm8PYdiZykdsRGhH1J8Js5pf8i38Iew/DjFer7FpestPGPwwZmUyuzlUr0ECPoS cW6iGaebwSq6TpYsxCG3W2KasnmJcZ9mB/vQjg7YSzvl7XHWKOfOvn3RI5JTbayo9lCeWqbaSi7 oILcpbIx49cPPbzaYCqecrKuOBPeCXTX4GuZRIyxAbcVp5vQA/CJ8qgNoGs+bY50za5YVS1yZbs //FPp+lm8Op4mdXyy8zjvVuC52AYd3cwa/WtgBKW0M4KdenAK/ImSeR604VksRwYA2qQLoJ5rTl x0BIgv+SjJEftdjRy8idZV9phB4IQ== X-Received: by 2002:a05:600c:4753:b0:493:b84f:9c28 with SMTP id 5b1f17b1804b1-493e6892e6dmr55979615e9.4.1783583064828; Thu, 09 Jul 2026 00:44:24 -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 v7 3/6] libs/guest: fill directly iov structure collapsing them Date: Thu, 9 Jul 2026 08:43:54 +0100 Message-ID: <20260709074358.256084-4-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260709074358.256084-1-frediano.ziglio@citrix.com> References: <20260709074358.256084-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-ef75cf/1783583065-7D72125E-F8BA5D33/0/0 X-purgate-type: clean X-purgate-size: 3965 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783583099206158500 Content-Type: text/plain; charset="utf-8" Instead of storing page pointers into an array and lately adding to iov vector add the pages directly to iov to avoid "guest_data" array. Each page was sent using a different iovec item. This potentially exceed Linux maximum (1024). Coalesce adjacent IO vector elements to attempt to reduce the number of overall IO vectors for each operation. Also some implementation (MiniOS) emulate writev with multiple write calls. Signed-off-by: Frediano Ziglio --- Changes since v2: - change prefix in subject. Changes since v4: - added Reviewed-by; - improved commit message; - minor style fix. Changes since v6: - merged 2 commits; - reduced diff. --- tools/libs/guest/xg_sr_save.c | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/tools/libs/guest/xg_sr_save.c b/tools/libs/guest/xg_sr_save.c index a6734579aa..84fdbe4140 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -88,7 +88,6 @@ static int write_batch(struct xc_sr_context *ctx) xc_interface *xch =3D ctx->xch; xen_pfn_t *mfns =3D NULL, *types =3D NULL; void *guest_mapping =3D NULL; - void **guest_data =3D NULL; void **local_pages =3D NULL; int *errors =3D NULL, rc =3D -1; unsigned int i, p, nr_pages =3D 0, nr_pages_mapped =3D 0; @@ -118,8 +117,6 @@ static int write_batch(struct xc_sr_context *ctx) types =3D malloc(nr_pfns * sizeof(*types)); /* Errors from attempting to map the gfns. */ errors =3D malloc(nr_pfns * sizeof(*errors)); - /* Pointers to page data to send. Mapped gfns or local allocations. */ - 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(). */ @@ -127,7 +124,7 @@ static int write_batch(struct xc_sr_context *ctx) /* page_data record PFNs list */ rec_pfns =3D malloc(nr_pfns * sizeof(*rec_pfns)); =20 - if ( !mfns || !types || !errors || !guest_data || !local_pages || !iov= || !rec_pfns ) + if ( !mfns || !types || !errors || !local_pages || !iov || !rec_pfns ) { ERROR("Unable to allocate arrays for a batch of %u pages", nr_pfns); @@ -218,8 +215,17 @@ static int write_batch(struct xc_sr_context *ctx) else goto err; } + else if ( iov[iovcnt - 1].iov_base + iov[iovcnt - 1].iov_len != =3D + page ) + { + iov[iovcnt].iov_base =3D page; + iov[iovcnt].iov_len =3D PAGE_SIZE; + iovcnt++; + } else - guest_data[i] =3D page; + { + iov[iovcnt - 1].iov_len +=3D PAGE_SIZE; + } =20 rc =3D -1; ++p; @@ -231,28 +237,12 @@ static int write_batch(struct xc_sr_context *ctx) for ( i =3D 0; i < nr_pfns; ++i ) rec_pfns[i] =3D ((uint64_t)(types[i]) << 32) | ctx->save.batch_pfn= s[i]; =20 - if ( nr_pages ) - { - for ( i =3D 0; i < nr_pfns; ++i ) - { - if ( guest_data[i] ) - { - iov[iovcnt].iov_base =3D guest_data[i]; - iov[iovcnt].iov_len =3D PAGE_SIZE; - iovcnt++; - --nr_pages; - } - } - } - if ( writev_exact(ctx->fd, iov, iovcnt) ) { PERROR("Failed to write page data to stream"); goto err; } =20 - /* Sanity check we have sent all the pages we expected to. */ - assert(nr_pages =3D=3D 0); rc =3D ctx->save.nr_batch_pfns =3D 0; =20 err: @@ -263,7 +253,6 @@ static int write_batch(struct xc_sr_context *ctx) free(rec_pfns); free(iov); free(local_pages); - free(guest_data); free(errors); free(types); free(mfns); --=20 2.43.0 From nobody Thu Jul 23 21:53:05 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=1783583093; cv=none; d=zohomail.com; s=zohoarc; b=DIqHUAhk8Ojp0V5WL5tSFQ3oipFujRLOegUsj8w1bH6D5Laom8ow9wSi5y92qdpyBqVU/SjRb99HHnG+Ii0fnkF8o1xI+qa1oIWyxeDTW4EXymGOJbRFMxncmZr2l+OSqvPgbAGWea24ByoS7/EjzHxxDDFCkCcHFjcfzw6VyJA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783583093; 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=SbsTLttx8i+LOt+EpVY9O+4mn+TTqNBKH43++Zu9LAk=; b=ARHZbRn433ca+q2i6KggDzxQU15Gs3PRvDsR4/97ZvKDBI9Idki7BhWC2924fUZRkZfmweKMXzfDyHmESahvw8f/6OLJLXed+X+mMSHVhw60+zR20KKGZHExq8QI3SyTvH7w74KahbrQ/jBAVGCNAdO790+ckr2pX2jegSe2VtU= 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 1783583093899886.4623279291096; Thu, 9 Jul 2026 00:44:53 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1357593.1611995 (Exim 4.92) (envelope-from ) id 1whjQr-0007rH-5a; Thu, 09 Jul 2026 07:44:29 +0000 Received: by outflank-mailman (output) from mailman id 1357593.1611995; Thu, 09 Jul 2026 07:44:29 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQr-0007rA-2a; Thu, 09 Jul 2026 07:44:29 +0000 Received: by outflank-mailman (input) for mailman id 1357593; Thu, 09 Jul 2026 07:44:27 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQp-0007bd-N5 for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 07:44:27 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1whjQp-002ZqU-0O for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 09:44:27 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4f5151-5cb7-0a2a0a5109dd-0a2a4508d66a-12 for ; Thu, 09 Jul 2026 09:44:26 +0200 Received: from [209.85.128.52] (helo=mail-wm1-f52.google.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4f515a-edec-0a2a45080019-d1558034f152-3 for ; Thu, 09 Jul 2026 09:44:26 +0200 Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-493ba701891so10932755e9.3 for ; Thu, 09 Jul 2026 00:44:26 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493eb6f3b85sm69114695e9.2.2026.07.09.00.44.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2026 00:44:25 -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=1783583066; x=1784187866; 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=SbsTLttx8i+LOt+EpVY9O+4mn+TTqNBKH43++Zu9LAk=; b=G98FWxkPF9XajoO7l9WWP6odvxwlkZv1TjCALS/yuAinL7d3BTgnrxUDFu8d84cmUU jLxU+GR14/O+/QxaK+zmEcIeOKgARE6zV9PsV5gGW8UdqWM/8XT8cteR0TSlaLKsSkIU PYI7V0RaRvnBAJ1ZVViW91Z+BrIOAT8Q5WtdaiLepu3tHof0pDKyzl7fWa2bJw8ZoA6D bbQwFDPQNJeLd5k21kbYHQ6Uys8XbdEbUy0AstHuGkBRg6wQfjWj6G47Xi9X71VynK5x BHuBPBw8jyAs1+N72RkhYMbAbPOpJMtY4i5PrmsMoDDGAqHc7NetP3WJtuyK9rYZwdF4 6gtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783583066; x=1784187866; 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=SbsTLttx8i+LOt+EpVY9O+4mn+TTqNBKH43++Zu9LAk=; b=j8xM6Ws39RDCqkz0la4+hFQtBUEXMobezpxrUsrYts09Dcm0d6YS0LEI3dxeuEW0Cy ct59nao2vLVHaRfaJo5KQ4YNWO2bEw64fQ2ssc5BXiT5EuGwwlMGJ+zv1e1NeU9kcMoc SQMY3UFBSfsim9MREvN2W1ZGdgD4bzCuhDtt/nuBdtJSFcXB1a2AShQ845PuQoHD9Nac QJEjiZFAV1wMJ9FtJmFM46IyrnhxmTfzfHBbR73f1aKyBDVWzO5boe5E5ugBeutOvlt4 eVqvkIhwx/jqfa9z14kIP6EN1T3trc1BD4BTPtohUnRkwDv9Xrj5tujCr5O8slmyFGxG 62Pw== X-Gm-Message-State: AOJu0YwuHKrc3pc708uVroB4HEv7Uy1NVLXQTYpv6aJ2s+uDkaEb+/fB k2T0u1lhRCdHrm4pYYh3MhlUrn+y1AfMaBfdrnPIHANHkmYBxVsJxuwKN+tyok9uoXQ= X-Gm-Gg: AfdE7clBbRkAl6PgXxp7pjFc/moQRdJUF0LE49K3/BAMbwhTulhbvXjWQL6Jjn8ZVsX rqxWJ8EscmbT3FPYhTnZM0p5d5bHrEZZhsqTwY1jQ7rLshyfl0ZB+OLgQpb1YWfPT0ib1IOGk7t 87jMBPkFWXZIkiFYXTMVyttdctAeJKqZhS1NCNtR5D5s2Xhm8kHk7V35jlKTZI35dKawEts4djv kUsnNrAX5Sh9Rm7m7rUiOpTvBAT2TTRlFog4VybPN44r3bX+zOwMeFEF5wOStQ+0vlp5wrGse/y suPsFCmffMdQ2UYGi8gcmQGj41L/Qftb8E23F2ZIb51bGRA7+8U57MbqxdFq1O1r0dP+hP9mrA1 2+zyWfSP+xriwmmLCiECJhDnEQt8f74B8snIbQ1p/4hQC6zTMVvKUGvQEDSQuelFK564/bInSsH khU/QmWGIqfzH181PN0+8vJufj2CpWP7bUydudDsZJYLTbBKCkBub9UCAdv5LyeXTMNy+8U5bh+ OsI48HbDx9RgE6+HDw= X-Received: by 2002:a05:600d:8497:20b0:493:b55f:bca4 with SMTP id 5b1f17b1804b1-493e68cec61mr42867035e9.29.1783583066271; Thu, 09 Jul 2026 00:44:26 -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 v7 4/6] libs/guest: move batch_pfns into a separate structure Date: Thu, 9 Jul 2026 08:43:55 +0100 Message-ID: <20260709074358.256084-5-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260709074358.256084-1-frediano.ziglio@citrix.com> References: <20260709074358.256084-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-c1860d/1783583066-413283FC-20146C73/0/0 X-purgate-type: clean X-purgate-size: 5973 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783583095321158500 From: Edwin T=C3=B6r=C3=B6k Prepare for following change. Signed-off-by: Frediano Ziglio --- Changes since v6: - split from "libs/guest: allocate various migration arrays just once". --- tools/libs/guest/xg_sr_common.h | 5 ++++- tools/libs/guest/xg_sr_save.c | 31 +++++++++++++++++-------------- 2 files changed, 21 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..8956f4260d 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 *batch_pfns; struct { struct xc_sr_rhdr rec; struct xc_sr_rec_page_data_header page_data; @@ -110,6 +111,10 @@ 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); + + batch_pfns =3D ctx->save.buffers->batch_pfns; =20 /* Mfns of the batch pfns. */ mfns =3D malloc(nr_pfns * sizeof(*mfns)); @@ -141,13 +146,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 +197,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 +211,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 +239,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 +278,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 +297,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 +788,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 +822,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:53:05 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=1783583095; cv=none; d=zohomail.com; s=zohoarc; b=heZ+krMeqibAaAK+5JnAGSJvsVpn439H+F6WpKh6dBd+2ANFn4mT+vTrsVDduJ0kuu803epXvf/TXMxSMHBR7VROJ4anwlPLpJvcog/I/5lmOUNmKRJPcEzw00RU3HT0Vwdv8fDI/4HRsoiapF1thxnmUFDscsbeBgYD2G6Gkns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783583095; 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=VIQbF+Op50T3FNdB6k5+hscux1I4CT+hNhWYmJo58Ts=; b=fgD4oM9bgYBIp//NeMjjVTqY1UccWEqirnG4+JF1kzFGAEvZzMZ0uih+kijJqYAriDhZu1HSsL11udhOw80l85ccsKxia5EtPrL9aSZhrzbXCG5ZFfS42Egg0IcjOGsFIan+dqLXQu9zOhQHRn4K2YoSMKrJn4UUDtAO8igODfw= 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 178358309556349.76505063108925; Thu, 9 Jul 2026 00:44:55 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1357594.1612004 (Exim 4.92) (envelope-from ) id 1whjQt-00086V-Cb; Thu, 09 Jul 2026 07:44:31 +0000 Received: by outflank-mailman (output) from mailman id 1357594.1612004; Thu, 09 Jul 2026 07:44:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQt-00086M-8r; Thu, 09 Jul 2026 07:44:31 +0000 Received: by outflank-mailman (input) for mailman id 1357594; Thu, 09 Jul 2026 07:44:29 +0000 Received: from mx.expurgate.net ([195.190.135.20]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQr-0007rR-CJ for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 07:44:29 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1whjQq-00B0uc-P9 for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 09:44:28 +0200 Received: from [10.42.69.5] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4f514d-bab6-0a2a0a5309dd-0a2a4505a858-36 for ; Thu, 09 Jul 2026 09:44:28 +0200 Received: from [209.85.128.42] (helo=mail-wm1-f42.google.com) by tlsNG-c201ff.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4f515c-3cb2-0a2a45050019-d155802aec98-3 for ; Thu, 09 Jul 2026 09:44:28 +0200 Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-493e497643fso8374575e9.0 for ; Thu, 09 Jul 2026 00:44:28 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493eb6f3b85sm69114695e9.2.2026.07.09.00.44.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2026 00:44:27 -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=1783583068; x=1784187868; 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=VIQbF+Op50T3FNdB6k5+hscux1I4CT+hNhWYmJo58Ts=; b=dFi+2J0V/FQPNGrcsa4PGCk701bLEGOkqd712O7KMfcwv4+yLEVYEjIKKWjXksmKrt jnDCqW0h6o/JLQFCvzwakOrT7mJutLBpUUKYUeQ72bLAT4rLpoPwHkX+GuR82dDmKQsL Jp6XyWt2k6kiOkKVJQr7x+AV2dAwG6d1WvWj1B0PscKfKz10KBhNA+O4G3cSjfB3Mto0 1y+byQ+pLbeOqUAeSDaKlov6F79mseIGyu2wX2KnIk6dLyVWEe1CEFAuXmtBqvlw4MbV JTD5S4ErjwKkImBNknbc74DTG/DBdG+lgwgpfyK5XWUir47z42u+2tgqXAW5jMpIyPf4 kVKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783583068; x=1784187868; 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=VIQbF+Op50T3FNdB6k5+hscux1I4CT+hNhWYmJo58Ts=; b=sfhTFSIKvOuCeSmhlGEUbzzhFxreqSuWy8PMi4NV7MH3WvkOnUFMsbSlpf7o1aeVMZ XAiKo5HqxlV0CEMX1nR780KaK2L7nVcgVqBX4Xs9fxa81gaGXkVUMYi0I/iYOigItHKJ nqQ6xxIHmCIyjnBBnYyG7L1+bMvOcLbS+EI07T5dTG2gD3RVCNuCCM6ltl97XppU091Z +zXTZxlbpqgfT+x/nzHL5ffMOZX3vkZiuS8xdGT4/pvg26mEhwjdFvabhAt6Zddjgjrl XYNxRIFV11i3AHusz+cNwlX5J0YStQ2DC8HqX6+wBGz2BB9oD+Nd+TwsmVQtxpwzD6je X3Hg== X-Gm-Message-State: AOJu0YxDrJ8wYAnY+BDCWL6Z5brazeW7sCKDNAEHDkr+EhEzlqWLg280 IlHN++tQYx0+HaAlQEojKCtg9bPHNrJKm3qB995C6zE8YqtR6HeHMWCIAAudwdawy5E= X-Gm-Gg: AfdE7cnYP6IsmP9aaf1zWf99WKYTZ1TOmzKpWWmOola3eJWGpPz9BIWJsgCv2fKeq0Y laDsRIRhZdQ/4+JrSZkvrxEVqK/qzNatFNs/1tX/twloESC4FEv9yulTGS5co9m/j5VMnyhi4fb hoghypIMY7WC/583JY+V9pFavnHgt0cT96YTMSHRWO68L2NXdpFH2UpXclDpym1gyQ7Mm1X/9Un UvuwmzEt102MaFHofAMyKI1hdIyCIcliGxxyOgDW0/ZTKxTtyPKNmzKxwCbL3mwH1ogRDDd/bir uIZ30yJL1tpERiRrxul78jfc189ror4CYtHAFtgtQWCvTQIvb0pgApLAer0SdKi/0MK5dFduF0y inF8S/XHjwEyg9L9OGZjxQREupgxlLBM6UvMy4L10CA1zZqqiOUmUN5WRc+wzf2+liN2VxB7nZx z5uDWZ4HPhCHRwR3a0ADQ4SZjkSSIP4yqjgZ3vRegdja5zwp/z2EirTionlNzMFr6gF8lzB3CGo Kpz9v8aY591ERfDR+k= X-Received: by 2002:a05:600c:6088:b0:493:d100:b487 with SMTP id 5b1f17b1804b1-493e6828725mr54656495e9.0.1783583067956; Thu, 09 Jul 2026 00:44:27 -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 v7 5/6] libs/guest: allocate various migration arrays just once Date: Thu, 9 Jul 2026 08:43:56 +0100 Message-ID: <20260709074358.256084-6-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260709074358.256084-1-frediano.ziglio@citrix.com> References: <20260709074358.256084-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-c201ff/1783583068-1771B2B8-92FE7BDE/0/0 X-purgate-type: clean X-purgate-size: 3812 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783583097199158500 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 8956f4260d..a49df00971 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -117,24 +117,18 @@ static int write_batch(struct xc_sr_context *ctx) batch_pfns =3D ctx->save.buffers->batch_pfns; =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); @@ -253,13 +247,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; } @@ -793,8 +784,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:53:05 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=1783583099; cv=none; d=zohomail.com; s=zohoarc; b=NrYFxlt0u8DqPC7hAtABQe98pc/xT65x9QAICn0InU/eJPZQOIfV+LM+myRnFLX82CVdF+k33mJRvr/drMS9UQ181Wt2Y0RUkk9zEvDm0TisLs6TQTssZW/b9bIM6QkeLwXwLC/BaswiujHwi5lLoTXpZUoiBY+QI47Cz7x1/cg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1783583099; 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=HdvYbFZJbCfZSA6txvl8zWdgc4tqxqpTHmXXPQRK7cA=; b=hfUEwFwSySWS36T4sMYVt7IHbvPSPozEcdqxSFHwv/aME5tw9k7IO44GQBzRQ5qHnZ7nh1QKYXVJ2cGhkUAIGB7qk9/HludqGX+y+/+49RiLbXCbiHR+sdQjej/6UKqOEe+Zzp+VLgt5pscOba46ldPp3Dah8pnntAPj2/hbi14= 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 1783583099255885.9054433652285; Thu, 9 Jul 2026 00:44:59 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1357595.1612010 (Exim 4.92) (envelope-from ) id 1whjQt-00089q-NM; Thu, 09 Jul 2026 07:44:31 +0000 Received: by outflank-mailman (output) from mailman id 1357595.1612010; Thu, 09 Jul 2026 07:44:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQt-00088Z-GS; Thu, 09 Jul 2026 07:44:31 +0000 Received: by outflank-mailman (input) for mailman id 1357595; Thu, 09 Jul 2026 07:44:30 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1whjQs-00081G-3L for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 07:44:30 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1whjQr-002ZqU-GH for xen-devel@lists.xenproject.org; Thu, 09 Jul 2026 09:44:29 +0200 Received: from [10.42.69.8] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4f5151-5cb7-0a2a0a5109dd-0a2a4508d66a-20 for ; Thu, 09 Jul 2026 09:44:29 +0200 Received: from [209.85.128.42] (helo=mail-wm1-f42.google.com) by tlsNG-c1860d.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4f515d-edec-0a2a45080019-d155802ad5cd-3 for ; Thu, 09 Jul 2026 09:44:29 +0200 Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-490cf322ed0so8763085e9.1 for ; Thu, 09 Jul 2026 00:44:29 -0700 (PDT) Received: from localhost.localdomain (2.115.147.147.dyn.plus.net. [147.147.115.2]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-493eb6f3b85sm69114695e9.2.2026.07.09.00.44.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Jul 2026 00:44:28 -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=1783583069; x=1784187869; 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=HdvYbFZJbCfZSA6txvl8zWdgc4tqxqpTHmXXPQRK7cA=; b=j2kQImmKXur6OBRa+1MyvRdaKob9iVoVX1mDfUMRVWnR0jerXIwniUmEWIYKYFB4l2 FuQWhw7RUw6t1odLp/GVg/3pZlZ3V0jUm5z3/LnYQTEEZVBf+3XYS7ifDSAB9Mk4D2GS jQpaeM0fAE5cbQ8kTB8/PqcLHgyHxZqQpU2y9i0ahmO2f0ws2DERYlL1HzRbvj7ixShm W92rJ1IsPLY2XqVKX4doAr6Dv4PIDmCTixpx872JENebAX75LtAezFjGyHycdv5Lg/BY j8ILnfp0OGKwxwhXtnwDofrx9dEvZTwJIr4LBFiQAk+reQNeY6VwwC+odQ3Z4ODTNZTC +MNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783583069; x=1784187869; 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=HdvYbFZJbCfZSA6txvl8zWdgc4tqxqpTHmXXPQRK7cA=; b=SpnjrB3vDv0wdVZL5Xn8y/YgDoTh8RNcg0ISBYdM5BGVm95UyHfdfQGVqAiQZg32B9 HX6MOVW+vaNAIrHOTAEtvXHPpf1bpWZRqfN+5Xd+R/p3GILD5aZibmkWIK1uHU5GuuSP jZTpWoBTZL2hBUptAIwSoIWNvsuoLtN571P8yRh4Fl0IrtI+ci2eNO/yRDGjcoye+AWP HhoWuff6OkFrp9rU8qPEpaXXi//AGbd3iV3bfV2zma2LjkZA7AvlpHFy+Q2hOTaI4B2E eIQBLQrUi7WRe0voseTdJTdRYN4wz7JuA6Yo8c1JxU3QT/+klIppwiGVm5gmveWKXyWx ULPA== X-Gm-Message-State: AOJu0Yw7hp0G+a1LBOoLEOXIq5KNXutxx0DXryfFvqg6YSVg2TbpLpgy D56sJqx2mqDr4N4pCWFwNabpeniohOghzfiiIwy1VtMwKT/7ho/KcMx1mnq5h6+D5JI= X-Gm-Gg: AfdE7clKDXozvaCg7+O/4BceWt6QuJmoMYGZrHaLwLwQ6hdelrMsGvBG8mIGjAtwm+E BVpp8cARHswt6lRHcUcYXLBeJtH/eTRpeESb3y1aCfvo48XNWI9lA4crs4MkxisqEgadr+VYPmX OvgVhfJoCIJcT9CnO7+mzlEWZseQ5xMFEoAjMBDIwfbhP5AywVjBfzXThWfqIKZt9uhD79tYN0H LRbLX4Hy5jOJxjbqAsAyLF4BCpILTsDbIkVWIKd3uta0rAsd/paAxMfjmwKGnQgciZ8AseB7P6n MVjtw7tbE0H8USgY8MYFX/ABgsvF/l0ItQkNYZzUs3hj97kokbcLKLtWyoaJoLQVHtUGnAaVD+Q QNgYzIll4QvMXqvvu9sXzBRSjJ15M6RbrP2i+Ot6LeM8U9WD1BRPCLyv1bMWQ710UqG5Mix0Gn2 IMOqx25hvN5vxgidhr6n+sCHKwnHqUXNndfeP7o1V3HQrhi8y91Wc33yhStIUeNSx6wAB7l6EWJ DClxGEdtjoKH48cLRI= X-Received: by 2002:a05:600c:4fd4:b0:493:e3ea:b23f with SMTP id 5b1f17b1804b1-493e6861ac2mr55928515e9.10.1783583068770; Thu, 09 Jul 2026 00:44:28 -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 v7 6/6] libs/guest: use Valgrind to detect various buffer overflows Date: Thu, 9 Jul 2026 08:43:57 +0100 Message-ID: <20260709074358.256084-7-frediano.ziglio@citrix.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260709074358.256084-1-frediano.ziglio@citrix.com> References: <20260709074358.256084-1-frediano.ziglio@citrix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-purgate-ID: tlsNG-c1860d/1783583069-A1F223FC-CB3DB1F0/0/0 X-purgate-type: clean X-purgate-size: 4048 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1783583101333158500 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 a49df00971..e03282e385 100644 --- a/tools/libs/guest/xg_sr_save.c +++ b/tools/libs/guest/xg_sr_save.c @@ -117,8 +117,10 @@ static int write_batch(struct xc_sr_context *ctx) batch_pfns =3D ctx->save.buffers->batch_pfns; =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; @@ -126,8 +128,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; @@ -790,6 +794,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